Fix broken htmlpurifier submodule.
[xhtml-compiler.git] / htaccess.php
blob4392084452416919b0224a9737e54283f2049b68
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 XHTMLCOMPILER . '/config.php',
24 XHTMLCOMPILER . '/config.default.php',
25 XHTMLCOMPILER . '/config.smoketest.php',
26 XHTMLCOMPILER . '/conf/config.php',
27 XHTMLCOMPILER . '/htaccess.php',
28 'redirects.txt',
29 '.htaccess.in',
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 relevant files');
47 if (!file_exists('.htaccess.in')) {
48 $contents = file_get_contents('.htaccess');
49 } else {
50 $contents = file_get_contents('.htaccess.in');
53 // do writeability check
54 if (
56 strpos($contents, $identifier_begin) === false ||
57 strpos($contents, $identifier_end) === false
58 ) &&
59 strpos($contents, $identifier_here) === false
60 ) {
61 throw new XHTMLCompiler_Exception(503, false,
62 'Pre-existing htaccess not configured to accept new rules');
65 // replace old rules with new set
66 $regex =
67 '/' .
68 preg_quote($identifier_begin, '/') .
69 '.+?' .
70 preg_quote($identifier_end, '/') .
71 '/s';
73 $contents = preg_replace($regex, $identifier_here, $contents);
75 } else {
76 $contents = $identifier_here;
79 // build the new htaccess
80 $n = array();
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]';
99 $big_exp = array();
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) {
104 $r = '';
105 if ($recursive) {
106 $r = "([^/]+/)*"; // escaped slashes not necessary
108 $len = strlen($dir);
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;
114 } else {
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';
153 $n[] = '</Files>';
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';
160 $n[] = '</Files>';
162 // setup RSS
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"';
168 $n[] = ' </Files>';
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);
181 if (is_cli()) {
182 echo "Okay: New .htaccess file successfully written\n";
183 exit;
186 ?><h1>200: Okay</h1>New <tt>.htaccess</tt> file successfully written