Give official name, move to a subdirectory.
[xhtml-compiler.git] / htaccess.php
blobb637863c10eab981b8f75272978d45dfb2063bc9
1 <?php
3 /**
4 * Generates the appropriate mod_rewrite rules in the htaccess file.
5 */
7 require 'common.php';
9 $identifier_begin = '# BEGIN xhtml-compiler/main.php mod_rewrite';
10 $identifier_end = '# END xhtml-compiler/main.php mod_rewrite';
11 $identifier_here = '# HERE xhtml-compiler/main.php mod_rewrite';
13 if (file_exists('.htaccess')) {
15 // do time check
16 $mtime_htaccess = filemtime('.htaccess');
17 $mtime_config = filemtime('xhtml-compiler/config.php');
18 $mtime_dconfig = filemtime('xhtml-compiler/config.default.php');
19 $mtime_gen = filemtime('xhtml-compiler/htaccess.php');
20 if ($mtime_htaccess > $mtime_config &&
21 $mtime_htaccess > $mtime_gen &&
22 $mtime_htaccess > $mtime_dconfig
23 ) {
24 display_error_and_quit(503, false,
25 'No changes detected in <tt>xhtml-compiler/config.php</tt> or <tt>xhtml-compiler/htaccess.php</tt>.');
28 $contents = file_get_contents('.htaccess');
30 // do writeability check
31 if (
33 strpos($contents, $identifier_begin) === false ||
34 strpos($contents, $identifier_end) === false
35 ) &&
36 strpos($contents, $identifier_here) === false
37 ) {
38 display_error_and_quit(503, false,
39 'Pre-existing htaccess not configured to accept new rules');
42 // replace old rules with new set
43 $regex =
44 '/' .
45 preg_quote($identifier_begin, '/') .
46 '.+?' .
47 preg_quote($identifier_end, '/') .
48 '/s';
50 $contents = preg_replace($regex, $identifier_here, $contents);
52 } else {
53 $contents = $identifier_here;
56 // build the new htaccess
57 $new_contents = array();
58 $new_contents[] = $identifier_begin;
59 $new_contents[] = 'RewriteEngine on';
61 $big_exp = array();
62 foreach ($allowed_dirs as $dir => $recursive) {
63 $r = '';
64 if ($recursive) {
65 $r = "([$filename_chars]+\/)*";
67 $dir_exp = $dir . $r;
68 $new_contents[] = "RewriteRule ^($dir_exp)$ \$1$directory_index";
69 $big_exp[] = $dir_exp;
72 $full_dir_exp = implode('|', $big_exp);
73 $new_contents[] = 'RewriteCond %{REQUEST_FILENAME} !-f [OR]';
74 $new_contents[] = 'RewriteCond %{QUERY_STRING} purge=1';
75 $new_contents[] = "RewriteRule ^(($full_dir_exp)[$filename_chars]+\.html)$ xhtml-compiler/main.php?f=\$1 [L,QSA]";
77 $new_contents[] = $identifier_end;
79 $contents = str_replace($identifier_here, implode($new_contents, PHP_EOL), $contents);
81 file_put_contents('.htaccess', $contents);
83 ?><h1>200: Okay</h1>New <tt>.htaccess</tt> file successfully written