Skip to content

Commit

Permalink
Inject GET and POST before PHP gets parsed.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcavallo committed May 11, 2014
1 parent bc70474 commit 52ebf31
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/middleman-php/middleware.rb
Expand Up @@ -10,13 +10,34 @@ def call(env)

if env['REQUEST_PATH'] =~ /\.php$/
response.body.map! do |item|
`echo #{Shellwords.escape(item)} | php`
`echo #{Shellwords.escape(inject_params(env) + item)} | php`
end
headers['Content-Length'] = response.body.join.length.to_s
headers['Content-Type'] = 'text/html'
end

[status, headers, response]
end

private

def inject_params env
injections = []
unless env['QUERY_STRING'].empty?
injections << { values: env['QUERY_STRING'], array: '$_GET' }
end
if env['REQUEST_METHOD'] == "POST"
input = env["rack.input"].read
unless input.length == 0
injections << { values: input, array: '$_POST' }
end
end
return '' unless injections.any?
injections.collect! do |inj|
"parse_str('#{inj[:values]}', #{inj[:array]});"
end
"<?php #{injections.join(' ')} ?>"
end

end
end

0 comments on commit 52ebf31

Please sign in to comment.