Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 51 lines (39 sloc) 1.46 KB
#!/usr/bin/env ruby
require 'rubygems'
require 'phuby'
require 'rack'
##
# Rack::Phrack is a Rack handler that will evaulate and serve PHP files.
class Rack::Phrack < Rack::File
class Events < Struct.new(:code, :headers, :body)
def write string; body << string; end
def send_headers response_code; end
def header value, op
k, v = value.split(': ', 2)
self.code = 302 if k == 'Location'
headers[k] = [headers[k], Rack::Utils.unescape(v)].compact.join "\n"
end
end
def call env
events = Events.new 200, {}, ''
file = File.join @root, env['PATH_INFO']
file = File.join file, "index.php" if File.directory?(file)
return super unless file =~ /php$/
Dir.chdir(File.dirname(file)) do
Phuby::Runtime.php do |rt|
rt.eval "date_default_timezone_set('America/Los_Angeles');" # *shrug*
{ Rack::Utils.parse_query(env['QUERY_STRING']) => "_GET",
Rack::Utils.parse_query(env['rack.input'].read) => "_POST",
Rack::Utils.parse_query(env['HTTP_COOKIE'], ';') => "_COOKIE",
}.each do |from, to|
from.each { |k,v| rt[to][k] = v }
end
env.each { |k,v| rt['_SERVER'][k] = v || '' unless k =~ /^rack/ }
rt["_SERVER"]['REQUEST_URI'] = env['PATH_INFO']
rt.with_events(events) { open(file) { |f| rt.eval f } } # RUN!
end
end
events.to_a
end
end
Rack::Handler::WEBrick.run(Rack::Phrack.new(ARGV[0] || Dir.pwd), :Port => 10101)