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
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')) {
22 $files_to_check = array(
23 XHTMLCOMPILER
. '/config.php',
24 XHTMLCOMPILER
. '/config.default.php',
25 XHTMLCOMPILER
. '/config.smoketest.php',
26 XHTMLCOMPILER
. '/conf/config.php',
27 XHTMLCOMPILER
. '/htaccess.php',
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;
42 if ($no_changes_needed) {
43 throw new XHTMLCompiler_Exception(503, false,
44 'No changes detected in relevant files');
47 if (!file_exists('.htaccess.in')) {
48 $contents = file_get_contents('.htaccess');
50 $contents = file_get_contents('.htaccess.in');
53 // do writeability check
56 strpos($contents, $identifier_begin) === false ||
57 strpos($contents, $identifier_end) === false
59 strpos($contents, $identifier_here) === false
61 throw new XHTMLCompiler_Exception(503, false,
62 'Pre-existing htaccess not configured to accept new rules');
65 // replace old rules with new set
68 preg_quote($identifier_begin, '/') .
70 preg_quote($identifier_end, '/') .
73 $contents = preg_replace($regex, $identifier_here, $contents);
76 $contents = $identifier_here;
79 // build the new htaccess
81 $n[] = $identifier_begin;
82 $n[] = 'RewriteEngine on';
83 $n[] = 'RewriteBase ' . $xc->getConf('web_path') . '/';
85 // create permanent redirects
86 if (file_exists('redirects.txt')) {
87 $redirects = explode("\n", file_get_contents('redirects.txt'));
88 foreach ($redirects as $redirect) {
89 $redirect = trim($redirect) . ' ';
90 if ($redirect === ' ') continue;
91 if ($redirect[0] === '#') continue;
92 list($src, $dest, $p) = explode(' ', $redirect);
93 if ($p !== '[P]') $src = '^' . preg_quote($src) . '$';
94 // We use rewrite to prevent the appending of ?f= querystring
95 $n[] = 'RewriteRule ' . $src . ' ' . $dest . ' [R=permanent,L]';
100 $directory_index = $xc->getConf('directory_index');
101 $indexed_dirs = $xc->getConf('indexed_dirs');
102 $allowed_dirs = $xc->getConf('allowed_dirs');
103 foreach ($allowed_dirs as $dir => $recursive) {
106 $r = "([^/]+/)*"; // escaped slashes not necessary
109 $slash = (!$len ||
$dir[$len-1] === '/') ?
'' : '/';
110 $dir_exp = preg_quote($dir) . $slash . $r;
112 if (is_array($indexed_dirs)) {
113 $intercept = isset($indexed_dirs[$dir]) ?
$indexed_dirs[$dir] : true;
115 $intercept = $indexed_dirs;
117 if (is_string($directory_index) && $intercept) {
118 // setup index rewrite
119 $n[] = "RewriteRule ^($dir_exp)$ \$1$directory_index";
121 $big_exp[] = $dir_exp;
124 $full_dir_exp = implode('|', $big_exp);
126 // allow pretty extension-less URLs for HTML pages
127 // this could be generalized for other URLs
128 $n[] = 'RewriteCond %{REQUEST_FILENAME} !-f';
129 $n[] = 'RewriteCond %{HTTP_ACCEPT} text/html';
130 $n[] = "RewriteRule ^(($full_dir_exp)[^/.]+)$ \$1.html [N]";
132 // prefer the extension-less URL
133 $n[] = "RewriteRule ^(($full_dir_exp)[^/]+)\.html$ \$1 [NS,R=301]";
135 // basic redirection if it doesn't exist
136 $n[] = 'RewriteCond %{REQUEST_FILENAME} !-f [OR]';
137 $n[] = 'RewriteCond %{QUERY_STRING} purge=1 [OR]';
138 $n[] = 'RewriteCond %{HTTP_COOKIE} purgatory=1';
139 $n[] = "RewriteRule ^(($full_dir_exp)[^/]+\.html)$ xhtml-compiler/main.php?f=\$1 [L,QSA]";
141 // if purge is set, also handle directories
142 $n[] = 'RewriteCond %{QUERY_STRING} purge=1';
143 $n[] = "RewriteRule ^($full_dir_exp)$ xhtml-compiler/main.php?f=\$1 [L,QSA]";
145 // add application/xhtml+xml if the browser supports it
146 $n[] = 'RewriteCond %{HTTP_ACCEPT} application/xhtml\\+xml';
147 $n[] = "RewriteRule ^(($full_dir_exp)[^/]+\.html)$ - \"[T=application/xhtml+xml,L]\"";
149 // xc-deps are forbidden to outside world
150 $n[] = '<Files ~ "\.xc-deps$">';
151 $n[] = ' Order allow,deny';
152 $n[] = ' Deny from all';
155 // errors.log is forbidden to outside world. In theory, this will occur only
156 // in xhtml-compiler/, but it won't hurt to deny it everywhere.
157 $n[] = '<Files errors.log>';
158 $n[] = ' Order allow,deny';
159 $n[] = ' Deny from all';
163 $n[] = 'AddType application/rss+xml rss';
164 $n[] = 'AddCharset UTF-8 .rss';
165 $n[] = '<IfModule mod_headers.c>';
166 $n[] = ' <Files ~ "\.rss$">';
167 $n[] = ' Header append Cache-Control "no-cache, must-revalidate"';
169 $n[] = '</IfModule>';
171 // set UTF-8 for HTML pages
172 $n[] = 'AddCharset UTF-8 .html';
174 $n[] = $identifier_end;
176 $contents = str_replace($identifier_here, implode($n, PHP_EOL
), $contents);
178 file_put_contents('.htaccess', $contents);
179 chmod('.htaccess', 0644);
182 echo "Okay: New .htaccess file successfully written\n";
186 ?
><h1
>200: Okay
</h1
>New <tt
>.htaccess
</tt
> file successfully written