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 if (isset($_SERVER['SERVER_PROTOCOL'])) {
25 $php->header($_SERVER['SERVER_PROTOCOL'] . ' ' . $text, true, $code);
26 $php->header('Status: ' . $text);
32 * Determines the requested page from server environment files
33 * @return string page name
35 function get_page_from_server() {
36 $php = XHTMLCompiler
::getPHPWrapper();
37 // load up page from environment variables, if using ErrorDocument impl.
38 $page = $php->getRequestURI();
39 $self = dirname($php->getPHPSelf());
40 $root = strlen(substr($self, 0, strrpos($self, '/')));
41 $page = substr($page, $root +
1); // remove leading slash and root
46 * Determines the requested page from get parameter
47 * @return string page name
49 function get_page_from_get() {
50 $php = XHTMLCompiler
::getPHPWrapper();
51 return $php->getGVal('f');
55 * Takes a page name and appends index filename if necessary
56 * @param $page string page name
57 * @param $directory_index string name of file to use as directory index
59 function normalize_index($page, $directory_index) {
60 $php = XHTMLCompiler
::getPHPWrapper();
61 if ($page == '') $page = $directory_index;
62 if ($php->isDir($page)) {
63 if ($page[strlen($page)-1] !== '/') $page .= '/';
64 $page .= $directory_index;
70 * Determines whether or not an .html file was generated by us
71 * @param $page filename of page, must exist
73 function is_created_by_us($page) {
74 $contents = file_get_contents($page);
75 return (strpos($contents, '<!-- generated by XHTML Compiler -->') !== false);
79 * Exception handler, prints a pretty HTTP error message
80 * @param $e The uncaught exception
81 * @todo Add debug mode, which results in copious stack-traces too
82 * @todo Make more friendly to command-line
84 function xhtmlcompiler_exception_handler(Exception
$e) {
85 if ($e instanceof XHTMLCompiler_Exception
) {
86 $code = $e->getCode();
87 $text = $e->getMessage();
88 $details = $e->getDetails();
92 $details = $e->getMessage();
94 $php = XHTMLCompiler
::getPHPWrapper();
95 $default = set_response_code($code);
96 if (!$text) $text = $default;
97 else $text = $code . ' ' . $text;
98 $out = "<h1>Error: $text</h1>";
99 if ($details) $out .= $details;
104 * Retrieves the last $limit log entries.
105 * @param $repos_url Repository URL of item to get logs for
106 * @param $limit Integer limit of items
108 function svn_log_limit($repos_url, $limit, $page = null) {
109 $limit = (int) $limit;
110 if ($limit <= 0) return array();
112 $cache_filename = 'xhtml-compiler/cache/svn/log/' . md5($repos_url) . '.ser';
113 if ($page && file_exists($cache_filename)) {
114 $logs = unserialize(file_get_contents($cache_filename));
115 // determine current revision number
116 $rev = $page->getSVNRevision();
117 if ($logs[0]['rev'] == $rev) return $logs;
119 // -q flag used to prevent server from sending log messages
120 $output = shell_exec("svn log -q --limit $limit $repos_url");
121 preg_match_all('/^r(\d+) /m', $output, $matches);
123 foreach ($matches[1] as $rev) {
124 $log = svn_log($repos_url, (int) $rev);
125 $ret[] = $log[0]; // log is only one item long
128 file_put_contents($cache_filename, serialize($ret));