Add bug report for Entity dependencies in XML included files.
[xhtml-compiler.git] / htaccess.php
blobb83738deec27e7c83c9f19f3acaae4cd6749cb0d
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/htaccess.php',
30 $mtime_htaccess = filemtime('.htaccess');
32 $no_changes_needed = true;
33 foreach ($files_to_check as $file) {
34 if (file_exists($file) && filemtime($file) > $mtime_htaccess) {
35 $no_changes_needed = false;
36 break;
40 if ($no_changes_needed) {
41 throw new XHTMLCompiler_Exception(503, false,
42 'No changes detected in <tt>xhtml-compiler/config.php</tt>,
43 <tt>xhtml-compiler/config.default.php</tt> or
44 <tt>xhtml-compiler/htaccess.php</tt>.');
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 $new_contents = array();
81 $new_contents[] = $identifier_begin;
82 $new_contents[] = 'RewriteEngine on';
83 $new_contents[] = 'RewriteBase ' . $xc->getConf('web_path') . '/';
85 $big_exp = array();
86 $directory_index = $xc->getConf('directory_index');
87 $indexed_dirs = $xc->getConf('indexed_dirs');
88 $allowed_dirs = $xc->getConf('allowed_dirs');
89 foreach ($allowed_dirs as $dir => $recursive) {
90 $r = '';
91 if ($recursive) {
92 $r = "([^/]+/)*"; // escaped slashes not necessary
94 $len = strlen($dir);
95 $slash = (!$len || $dir[$len-1] === '/') ? '' : '/';
96 $dir_exp = preg_quote($dir) . $slash . $r;
98 if (is_array($indexed_dirs)) {
99 $intercept = isset($indexed_dirs[$dir]) ? $indexed_dirs[$dir] : true;
100 } else {
101 $intercept = $indexed_dirs;
103 if (is_string($directory_index) && $intercept) {
104 // setup index rewrite
105 $new_contents[] = "RewriteRule ^($dir_exp)$ \$1$directory_index";
107 $big_exp[] = $dir_exp;
110 $full_dir_exp = implode('|', $big_exp);
111 $new_contents[] = 'RewriteCond %{REQUEST_FILENAME} !-f [OR]';
112 $new_contents[] = 'RewriteCond %{QUERY_STRING} purge=1 [OR]';
113 $new_contents[] = 'RewriteCond %{HTTP_COOKIE} purgatory=1';
114 $new_contents[] = "RewriteRule ^(($full_dir_exp)[^/]+\.html)$ xhtml-compiler/main.php?f=\$1 [L,QSA]";
116 // if purge is set, also handle directories
117 $new_contents[] = 'RewriteCond %{QUERY_STRING} purge=1';
118 $new_contents[] = "RewriteRule ^($full_dir_exp)$ xhtml-compiler/main.php?f=\$1 [L,QSA]";
120 // add application/xhtml+xml if the browser supports it
121 $new_contents[] = 'RewriteCond %{HTTP_ACCEPT} application/xhtml\\+xml';
122 $new_contents[] = "RewriteRule ^(($full_dir_exp)[^/]+\.html)$ - \"[T=application/xhtml+xml,L]\"";
124 // xc-deps are forbidden to outside world
125 $new_contents[] = '<Files ~ "\.xc-deps$">';
126 $new_contents[] = ' Order allow,deny';
127 $new_contents[] = ' Deny from all';
128 $new_contents[] = '</Files>';
130 // setup RSS
131 $new_contents[] = 'AddType application/rss+xml rss';
132 $new_contents[] = 'AddCharset UTF-8 .rss';
133 $new_contents[] = '<IfModule mod_headers.c>';
134 $new_contents[] = ' <Files ~ "\.rss$">';
135 $new_contents[] = ' Header append Cache-Control "no-cache, must-revalidate"';
136 $new_contents[] = ' </Files>';
137 $new_contents[] = '</IfModule>';
139 // set UTF-8 for HTML pages
140 $new_contents[] = 'AddCharset UTF-8 .html';
142 $new_contents[] = $identifier_end;
144 $contents = str_replace($identifier_here, implode($new_contents, PHP_EOL), $contents);
146 file_put_contents('.htaccess', $contents);
147 chmod('.htaccess', 0644);
149 ?><h1>200: Okay</h1>New <tt>.htaccess</tt> file successfully written