Skip to content

Commit

Permalink
Config library: escape JSON backslash and quotes for PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
zuazo committed Sep 17, 2015
1 parent 807e9dc commit 4941aea
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions libraries/config.rb
Expand Up @@ -11,6 +11,14 @@ def initialize(file)
read
end

protected

def options_php_json
@options.to_json.gsub('\\', '\\\\\\').gsub("'", "\\\\'")
end

public

def [](index)
@options[index]
end
Expand Down Expand Up @@ -38,7 +46,7 @@ def read()
begin
return unless ::File.exists?(@file)
f = IO.popen('php', 'r+')
f.write "<?php require('#{@file}'); echo json_encode($CONFIG);"
f.write "<?php require('#{@file}'); echo json_encode($CONFIG);\n"
f.close_write
data = f.read
f.close
Expand All @@ -53,7 +61,7 @@ def write()
begin
return if @options == @original_options
f = IO.popen('php', 'r+')
f.write "<?php var_export(json_decode('#{@options.to_json}', true));"
f.write "<?php var_export(json_decode('#{options_php_json}', true));\n"
f.close_write
data = f.read
f.close
Expand Down

0 comments on commit 4941aea

Please sign in to comment.