4 * Convenience function that outputs an HTTP status code
5 * @param $code integer HTTP status code
6 * @return string HTTP status code's name
8 function set_response_code($code) {
9 $php = XHTMLCompiler
::getPHPWrapper();
10 $code_descriptions = array( // could be factored out
12 304 => 'Not Modified',
13 401 => 'Unauthorized',
16 500 => 'Internal Server Error',
17 503 => 'Service Unavailable',
19 $code = (int) $code; // enforce integer
20 $text = (string) $code;
21 if (isset($code_descriptions[$code])) {
22 $text .= ' ' . $code_descriptions[$code];
24 $php->header($_SERVER['SERVER_PROTOCOL'] . ' ' . $text, true, $code);
25 $php->header('Status: ' . $text);
30 * Determines the requested page from server environment files
31 * @return string page name
33 function get_page_from_server() {
34 $php = XHTMLCompiler
::getPHPWrapper();
35 // load up page from environment variables, if using ErrorDocument impl.
36 $page = $php->getRequestURI();
37 $self = dirname($php->getPHPSelf());
38 $root = strlen(substr($self, 0, strrpos($self, '/')));
39 $page = substr($page, $root +
1); // remove leading slash and root
44 * Determines the requested page from get parameter
45 * @return string page name
47 function get_page_from_get() {
48 $php = XHTMLCompiler
::getPHPWrapper();
49 return $php->getGVal('f');
53 * Takes a page name and appends index filename if necessary
54 * @param $page string page name
55 * @param $directory_index string name of file to use as directory index
57 function normalize_index($page, $directory_index) {
58 $php = XHTMLCompiler
::getPHPWrapper();
59 if ($page == '') $page = $directory_index;
60 if ($php->isDir($page)) {
61 if ($page[strlen($page)-1] !== '/') $page .= '/';
62 $page .= $directory_index;
68 * Determines whether or not an .html file was generated by us
69 * @param $page filename of page, must exist
71 function is_created_by_us($page) {
72 $contents = file_get_contents($page);
73 return (strpos($contents, '<!-- generated by XHTML Compiler -->') !== false);
76 // todo: use SPL's RecursiveDirectoryIterator, using implementation as
77 // described here: http://forums.devnetwork.net/viewtopic.php?t=47288
80 * Traverses a directory looking for pages with a particular extension.
81 * @param $ext Extension we're looking for, needs period
82 * @param $dir Directory we're looking in
83 * @param $recursive Whether or not to go recursively
85 function scan_for_pages($ext, $dir, $recursive) {
86 $scandir = ($dir === '') ?
'./' : $dir; // scandir barfs on ''
87 // append trailing slash if necessary
88 if ($dir !== '' && $dir[strlen($dir)-1] !== '/') $dir .= '/';
91 foreach ($ext as $i => $e) $files[$i] = array();
93 $contents = scandir($scandir);
94 foreach ($contents as $file) {
95 if (empty($file) ||
$file[0] == '.') continue;
96 if (is_dir($dir . $file) && $recursive) {
97 $files = array_merge($files, scan_for_pages($ext, $dir . $file . '/', 1));
100 $fileext = strrchr($file, ".");
101 if (is_array($ext)) {
102 foreach ($ext as $i => $ext_single) {
103 if ($fileext === $ext_single) {
104 $files[$i][] = $dir . $file;
108 if ($fileext === $ext) {
109 $files[] = $dir . $file;
117 * Traverses an array of directories for files with extension
118 * @param $ext Extension we're looking for, needs period
119 * @param $dir Directories that are allowed, see hc-config.default.php
121 function scan_dirs_for_pages($ext, $allowed_dirs) {
123 if (is_array($ext)) {
124 foreach ($ext as $i => $e) $files[$i] = array();
126 foreach ($allowed_dirs as $dir => $recursive) {
127 $pages = scan_for_pages($ext, $dir, $recursive);
128 if (is_array($ext)) {
129 foreach ($ext as $i => $e) {
130 $files[$i] = array_merge($pages[$i], $files[$i]);
133 $files = array_merge($pages, $files);
140 * Exception handler, prints a pretty HTTP error message
141 * @param $e The uncaught exception
142 * @todo Add debug mode, which results in copious stack-traces too
144 function xhtmlcompiler_exception_handler(Exception
$e) {
145 if ($e instanceof XHTMLCompiler_Exception
) {
146 $code = $e->getCode();
147 $text = $e->getMessage();
148 $details = $e->getDetails();
152 $details = $e->getMessage();
154 $php = XHTMLCompiler
::getPHPWrapper();
155 $default = set_response_code($code);
156 if (!$text) $text = $default;
157 else $text = $code . ' ' . $text;
158 $out = "<h1>Error: $text</h1>";
159 if ($details) $out .= $details;