Remove hard-coded HTML Purifier includes; config.php has them instead
[xhtml-compiler.git] / htaccess.php
blob74c97558a57d378a48a25faf3943a22a62baaae6
1 <?php
3 /**
4 * Generates the appropriate mod_rewrite rules in the htaccess file.
5 * @note If a .htaccess.in prototype file is present in the directory,
6 * it will be used as the "base" htaccess to determine the new
7 * one, and local changes to .htaccess will ALWAYS be overwritten
8 * @todo Create an HTAccess object to edit
9 */
11 require 'common.php';
13 $xc = XHTMLCompiler::getInstance();
15 $identifier_begin = '# BEGIN xhtml-compiler/main.php mod_rewrite';
16 $identifier_end = '# END xhtml-compiler/main.php mod_rewrite';
17 $identifier_here = '# HERE xhtml-compiler/main.php mod_rewrite';
19 if (file_exists('.htaccess')) {
21 // do time check
22 $files_to_check = array(
23 '.htaccess.in',
24 'xhtml-compiler/config.php',
25 'xhtml-compiler/config.default.php',
26 'xhtml-compiler/config.smoketest.php',
27 'xhtml-compiler/conf/config.php',
28 'xhtml-compiler/htaccess.php',
29 'redirects.txt',
32 $mtime_htaccess = filemtime('.htaccess');
34 $no_changes_needed = true;
35 foreach ($files_to_check as $file) {
36 if (file_exists($file) && filemtime($file) > $mtime_htaccess) {
37 $no_changes_needed = false;
38 break;
42 if ($no_changes_needed) {
43 throw new XHTMLCompiler_Exception(503, false,
44 'No changes detected in <tt>xhtml-compiler/config.php</tt>,
45 <tt>xhtml-compiler/config.default.php</tt> or
46 <tt>xhtml-compiler/htaccess.php</tt>.');
49 if (!file_exists('.htaccess.in')) {
50 $contents = file_get_contents('.htaccess');
51 } else {
52 $contents = file_get_contents('.htaccess.in');
55 // do writeability check
56 if (
58 strpos($contents, $identifier_begin) === false ||
59 strpos($contents, $identifier_end) === false
60 ) &&
61 strpos($contents, $identifier_here) === false
62 ) {
63 throw new XHTMLCompiler_Exception(503, false,
64 'Pre-existing htaccess not configured to accept new rules');
67 // replace old rules with new set
68 $regex =
69 '/' .
70 preg_quote($identifier_begin, '/') .
71 '.+?' .
72 preg_quote($identifier_end, '/') .
73 '/s';
75 $contents = preg_replace($regex, $identifier_here, $contents);
77 } else {
78 $contents = $identifier_here;
81 // build the new htaccess
82 $new_contents = array();
83 $new_contents[] = $identifier_begin;
84 $new_contents[] = 'RewriteEngine on';
85 $new_contents[] = 'RewriteBase ' . $xc->getConf('web_path') . '/';
87 // create permanent redirects
88 if (file_exists('redirects.txt')) {
89 $redirects = explode("\n", file_get_contents('redirects.txt'));
90 foreach ($redirects as $redirect) {
91 $redirect = trim($redirect) . ' ';
92 if ($redirect === ' ') continue;
93 if ($redirect[0] === '#') continue;
94 list($src, $dest, $p) = explode(' ', $redirect);
95 if ($p !== '[P]') $src = '^' . preg_quote($src) . '$';
96 // We use rewrite to prevent the appending of ?f= querystring
97 $new_contents[] = 'RewriteRule ' . $src . ' ' . $dest . ' [R=permanent,L]';
101 $big_exp = array();
102 $directory_index = $xc->getConf('directory_index');
103 $indexed_dirs = $xc->getConf('indexed_dirs');
104 $allowed_dirs = $xc->getConf('allowed_dirs');
105 foreach ($allowed_dirs as $dir => $recursive) {
106 $r = '';
107 if ($recursive) {
108 $r = "([^/]+/)*"; // escaped slashes not necessary
110 $len = strlen($dir);
111 $slash = (!$len || $dir[$len-1] === '/') ? '' : '/';
112 $dir_exp = preg_quote($dir) . $slash . $r;
114 if (is_array($indexed_dirs)) {
115 $intercept = isset($indexed_dirs[$dir]) ? $indexed_dirs[$dir] : true;
116 } else {
117 $intercept = $indexed_dirs;
119 if (is_string($directory_index) && $intercept) {
120 // setup index rewrite
121 $new_contents[] = "RewriteRule ^($dir_exp)$ \$1$directory_index";
123 $big_exp[] = $dir_exp;
126 $full_dir_exp = implode('|', $big_exp);
127 $new_contents[] = 'RewriteCond %{REQUEST_FILENAME} !-f [OR]';
128 $new_contents[] = 'RewriteCond %{QUERY_STRING} purge=1 [OR]';
129 $new_contents[] = 'RewriteCond %{HTTP_COOKIE} purgatory=1';
130 $new_contents[] = "RewriteRule ^(($full_dir_exp)[^/]+\.html)$ xhtml-compiler/main.php?f=\$1 [L,QSA]";
132 // if purge is set, also handle directories
133 $new_contents[] = 'RewriteCond %{QUERY_STRING} purge=1';
134 $new_contents[] = "RewriteRule ^($full_dir_exp)$ xhtml-compiler/main.php?f=\$1 [L,QSA]";
136 // add application/xhtml+xml if the browser supports it
137 $new_contents[] = 'RewriteCond %{HTTP_ACCEPT} application/xhtml\\+xml';
138 $new_contents[] = "RewriteRule ^(($full_dir_exp)[^/]+\.html)$ - \"[T=application/xhtml+xml,L]\"";
140 // xc-deps are forbidden to outside world
141 $new_contents[] = '<Files ~ "\.xc-deps$">';
142 $new_contents[] = ' Order allow,deny';
143 $new_contents[] = ' Deny from all';
144 $new_contents[] = '</Files>';
146 // errors.log is forbidden to outside world. In theory, this will occur only
147 // in xhtml-compiler/, but it won't hurt to deny it everywhere.
148 $new_contents[] = '<Files errors.log>';
149 $new_contents[] = ' Order allow,deny';
150 $new_contents[] = ' Deny from all';
151 $new_contents[] = '</Files>';
153 // setup RSS
154 $new_contents[] = 'AddType application/rss+xml rss';
155 $new_contents[] = 'AddCharset UTF-8 .rss';
156 $new_contents[] = '<IfModule mod_headers.c>';
157 $new_contents[] = ' <Files ~ "\.rss$">';
158 $new_contents[] = ' Header append Cache-Control "no-cache, must-revalidate"';
159 $new_contents[] = ' </Files>';
160 $new_contents[] = '</IfModule>';
162 // set UTF-8 for HTML pages
163 $new_contents[] = 'AddCharset UTF-8 .html';
165 $new_contents[] = $identifier_end;
167 $contents = str_replace($identifier_here, implode($new_contents, PHP_EOL), $contents);
169 file_put_contents('.htaccess', $contents);
170 chmod('.htaccess', 0644);
172 ?><h1>200: Okay</h1>New <tt>.htaccess</tt> file successfully written