Add PEAR server PHP code to repository.
[htmlpurifier-web.git] / xhtml-compiler / htaccess.php
blobc838e889b39e2d86c773135fb4cf5f825666613e
1 <?php
3 /**
4 * Generates the appropriate mod_rewrite rules in the htaccess file.
5 */
7 require 'common.php';
9 $xc = XHTMLCompiler::getInstance();
11 $identifier_begin = '# BEGIN xhtml-compiler/main.php mod_rewrite';
12 $identifier_end = '# END xhtml-compiler/main.php mod_rewrite';
13 $identifier_here = '# HERE xhtml-compiler/main.php mod_rewrite';
15 if (file_exists('.htaccess')) {
17 // do time check
18 $mtime_htaccess = filemtime('.htaccess');
19 $mtime_config = filemtime('xhtml-compiler/config.php');
20 $mtime_dconfig = filemtime('xhtml-compiler/config.default.php');
21 $mtime_sconfig = filemtime('xhtml-compiler/config.smoketest.php');
22 $mtime_gen = filemtime('xhtml-compiler/htaccess.php');
23 if ($mtime_htaccess > $mtime_config &&
24 $mtime_htaccess > $mtime_gen &&
25 $mtime_htaccess > $mtime_dconfig &&
26 $mtime_htaccess > $mtime_sconfig
27 ) {
28 throw new XHTMLCompiler_Exception(503, false,
29 'No changes detected in <tt>xhtml-compiler/config.php</tt>,
30 <tt>xhtml-compiler/config.default.php</tt> or
31 <tt>xhtml-compiler/htaccess.php</tt>.');
34 $contents = file_get_contents('.htaccess');
36 // do writeability check
37 if (
39 strpos($contents, $identifier_begin) === false ||
40 strpos($contents, $identifier_end) === false
41 ) &&
42 strpos($contents, $identifier_here) === false
43 ) {
44 throw new XHTMLCompiler_Exception(503, false,
45 'Pre-existing htaccess not configured to accept new rules');
48 // replace old rules with new set
49 $regex =
50 '/' .
51 preg_quote($identifier_begin, '/') .
52 '.+?' .
53 preg_quote($identifier_end, '/') .
54 '/s';
56 $contents = preg_replace($regex, $identifier_here, $contents);
58 } else {
59 $contents = $identifier_here;
62 // build the new htaccess
63 $new_contents = array();
64 $new_contents[] = $identifier_begin;
65 $new_contents[] = 'RewriteEngine on';
67 $big_exp = array();
68 $directory_index = $xc->getConf('directory_index');
69 $indexed_dirs = $xc->getConf('indexed_dirs');
70 $allowed_dirs = $xc->getConf('allowed_dirs');
71 foreach ($allowed_dirs as $dir => $recursive) {
72 $r = '';
73 if ($recursive) {
74 $r = "([^/]+/)*"; // escaped slashes not necessary
76 $len = strlen($dir);
77 $slash = (!$len || $dir[$len-1] === '/') ? '' : '/';
78 $dir_exp = preg_quote($dir) . $slash . $r;
80 if (is_array($indexed_dirs)) {
81 $intercept = isset($indexed_dirs[$dir]) ? $indexed_dirs[$dir] : true;
82 } else {
83 $intercept = $indexed_dirs;
85 if (is_string($directory_index) && $intercept) {
86 // setup index rewrite
87 $new_contents[] = "RewriteRule ^($dir_exp)$ \$1$directory_index";
89 $big_exp[] = $dir_exp;
92 $full_dir_exp = implode('|', $big_exp);
93 $new_contents[] = 'RewriteCond %{REQUEST_FILENAME} !-f [OR]';
94 $new_contents[] = 'RewriteCond %{QUERY_STRING} purge=1 [OR]';
95 $new_contents[] = 'RewriteCond %{HTTP_COOKIE} purgatory=1';
96 $new_contents[] = "RewriteRule ^(($full_dir_exp)[^/]+\.html)$ xhtml-compiler/main.php?f=\$1 [L,QSA]";
98 // if purge is set, also handle directories
99 $new_contents[] = 'RewriteCond %{QUERY_STRING} purge=1';
100 $new_contents[] = "RewriteRule ^($full_dir_exp)$ xhtml-compiler/main.php?f=\$1 [L,QSA]";
102 // xc-deps are forbidden to outside world
103 $new_contents[] = '<Files ~ "\.xc-deps$">';
104 $new_contents[] = ' Order allow,deny';
105 $new_contents[] = ' Deny from all';
106 $new_contents[] = '</Files>';
108 // set UTF-8
109 $new_contents[] = 'AddCharset UTF-8 .html';
111 $new_contents[] = $identifier_end;
113 $contents = str_replace($identifier_here, implode($new_contents, PHP_EOL), $contents);
115 file_put_contents('.htaccess', $contents);
116 chmod('.htaccess', 0644);
118 ?><h1>200: Okay</h1>New <tt>.htaccess</tt> file successfully written