Skip to content
This repository was archived by the owner on Dec 2, 2018. It is now read-only.

Commit d7bbaca

Browse files
committed
Remove .htaccess support.
While useful in ensuring parity between development and production environment htaccess files have a few problems. The primary is that they are not univerally supported. The host may have them disabled for security reasons. Or the application may be hosted where Apache is not driving PHP. mod_php has security issues on shared host environments so many hosts are moving to more secure setups (suphp, php-cgi, php + fastcgi). All these environments scan .user.ini files instead. I now consider .htaccess part of the deployment config and most apps should provide a way to accomplish the same needs in a non-.htaccess environemnt.
1 parent f06b877 commit d7bbaca

File tree

2 files changed

+2
-96
lines changed

2 files changed

+2
-96
lines changed

lib/rack/legacy/php.rb

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ class Rack::Legacy::Php < Rack::Legacy::Cgi
77
# of which executable to use to run the PHP code.
88
#
99
# use Rack::Legacy::Php, 'public', 'php5-cgi'
10-
def initialize(app, public_dir=FileUtils.pwd, php_exe='php-cgi', htaccess_enabled=true)
10+
def initialize(app, public_dir=FileUtils.pwd, php_exe='php-cgi')
1111
super app, public_dir
1212
@php_exe = php_exe
13-
@htaccess_enabled = htaccess_enabled
1413
end
1514

1615
# Override to check for php extension. Still checks if
@@ -30,11 +29,6 @@ def valid?(path)
3029
# Monkeys with the arguments so that it actually runs PHP's cgi
3130
# program with the path as an argument to that program.
3231
def run(env, path)
33-
config = {'cgi.force_redirect' => 0}
34-
config.merge! HtAccess.merge_all(path, public_dir) if @htaccess_enabled
35-
config = config.collect {|(key, value)| "#{key}=#{value}"}
36-
config.collect! {|kv| ['-d', kv]}
37-
3832
script, info = *path_parts(path)
3933
if ::File.directory? script
4034
# If directory then assume index.php
@@ -48,7 +42,7 @@ def run(env, path)
4842
env['REQUEST_URI'] = strip_public path
4943
env['REQUEST_URI'] += '?' + env['QUERY_STRING'] if
5044
env.has_key?('QUERY_STRING') && !env['QUERY_STRING'].empty?
51-
super env, @php_exe, *config.flatten
45+
super env, @php_exe, "-d cgi.force_redirect=0"
5246
end
5347

5448
private
@@ -79,46 +73,4 @@ def path_parts(path)
7973
def info_path(path)
8074
path.split('.php', 2)[1].to_s
8175
end
82-
83-
# For processing .htaccess files to tweak PHP environment.
84-
# Represents a single .htaccess file that might affect PHP
85-
class HtAccess
86-
87-
# The .htaccess file being processed
88-
attr_reader :file
89-
90-
# New instance to process the given file for PHP config
91-
def initialize(file)
92-
@file = file
93-
end
94-
95-
# Returns a hash of the PHP config that needs to be set.
96-
def to_hash
97-
ret = {}
98-
::File.readlines(@file).each do |line|
99-
ret[$1] = $2 if line.chomp =~ /^php_\S+ (\S+) (.*)$/
100-
end
101-
ret
102-
end
103-
104-
# Will return all .htaccess files that affect a given path
105-
# stopping when it reaches the root directory.
106-
def self.find_all(path, root)
107-
dir = ::File.dirname(path)
108-
ret = if dir.start_with?(root)
109-
find_all(dir, root)
110-
else
111-
[]
112-
end
113-
ret << new("#{dir}/.htaccess") if ::File.exist? "#{dir}/.htaccess"
114-
ret
115-
end
116-
117-
# Finds all .htaccess files that affect the given path stopping
118-
# at the given root and merge them into one big hash.
119-
def self.merge_all(path, root)
120-
find_all(path, root).inject({}) {|ret, hsh| ret.merge hsh}
121-
end
122-
123-
end
12476
end

test/unit/php_test.rb

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -96,52 +96,6 @@ def test_environment
9696
assert 'Rack Legacy', env['SERVER_SOFTWARE']
9797
end
9898

99-
def test_parse_htaccess
100-
file = File.join(File.dirname(__FILE__), '../fixtures/dir1/dir2/.htaccess')
101-
assert_equal({
102-
'include_path' => 'backend:ext:.',
103-
'auto_prepend_file' => 'backend/lib/setup.php',
104-
'auto_append_file' => 'backend/lib/teardown.php',
105-
'output_buffering' => 'off',
106-
}, Rack::Legacy::Php::HtAccess.new(file).to_hash)
107-
end
108-
109-
def test_htaccess_search
110-
file = File.join(File.dirname(__FILE__), '../fixtures/dir1/dir2/test.php')
111-
root = File.join(File.dirname(__FILE__), '../fixtures')
112-
assert_equal [
113-
File.join(File.dirname(__FILE__), '../fixtures/.htaccess'),
114-
File.join(File.dirname(__FILE__), '../fixtures/dir1/dir2/.htaccess'),
115-
], Rack::Legacy::Php::HtAccess.find_all(file, root).collect(&:file)
116-
end
117-
118-
def test_merge_all
119-
file = File.join(File.dirname(__FILE__), '../fixtures/dir1/dir2/test.php')
120-
root = File.join(File.dirname(__FILE__), '../fixtures')
121-
assert_equal({
122-
'include_path' => 'backend:ext:.',
123-
'auto_prepend_file' => 'backend/lib/setup.php',
124-
'auto_append_file' => 'backend/lib/teardown.php',
125-
'output_buffering' => 'off',
126-
'foo' => 'bar',
127-
'baz' => 'boo',
128-
}, Rack::Legacy::Php::HtAccess.merge_all(file, root))
129-
130-
assert_equal({},
131-
Rack::Legacy::Php::HtAccess.merge_all(__FILE__, File.dirname(__FILE__)))
132-
end
133-
134-
def test_htaccess_flag
135-
status, headers, body = *app.call({'PATH_INFO' => 'ini.php'})
136-
assert_equal '1', body[0]
137-
138-
app = Rack::Legacy::Php.new \
139-
proc {[200, {'Content-Type' => 'text/html'}, 'Endpoint']},
140-
File.join(File.dirname(__FILE__), '../fixtures'), 'php-cgi', false
141-
status, headers, body = *app.call({'PATH_INFO' => 'ini.php'})
142-
assert_equal '4096', body[0]
143-
end
144-
14599
private
146100

147101
def app

0 commit comments

Comments
 (0)