- Set svn:eol-style=native
[xhtml-compiler.git] / functions.php
blobad432c7710172f5c1b64a2c17a407ce8c8534395
1 <?php
3 /**
4 * Convenience function that outputs an HTTP status code
5 * @param $code integer HTTP status code
6 * @return string HTTP status code's name
7 */
8 function set_response_code($code) {
9 $php = XHTMLCompiler::getPHPWrapper();
10 $code_descriptions = array( // could be factored out
11 200 => 'Okay',
12 304 => 'Not Modified',
13 401 => 'Unauthorized',
14 403 => 'Forbidden',
15 404 => 'Not Found',
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);
26 return $text;
29 /**
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
40 return $page;
43 /**
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');
52 /**
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;
64 return $page;
67 /**
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 /**
77 * Exception handler, prints a pretty HTTP error message
78 * @param $e The uncaught exception
79 * @todo Add debug mode, which results in copious stack-traces too
81 function xhtmlcompiler_exception_handler(Exception $e) {
82 if ($e instanceof XHTMLCompiler_Exception) {
83 $code = $e->getCode();
84 $text = $e->getMessage();
85 $details = $e->getDetails();
86 } else {
87 $code = 500;
88 $text = false;
89 $details = $e->getMessage();
91 $php = XHTMLCompiler::getPHPWrapper();
92 $default = set_response_code($code);
93 if (!$text) $text = $default;
94 else $text = $code . ' ' . $text;
95 $out = "<h1>Error: $text</h1>";
96 if ($details) $out .= $details;
97 $php->paint($out);
101 * Retrieves the last $limit log entries.
102 * @param $repos_url Repository URL of item to get logs for
103 * @param $limit Integer limit of items
105 function svn_log_limit($repos_url, $limit, $page = null) {
106 $limit = (int) $limit;
107 if ($limit <= 0) return array();
108 // attempt the cache
109 $cache_filename = 'xhtml-compiler/cache/svn/log/' . md5($repos_url) . '.ser';
110 if ($page && file_exists($cache_filename)) {
111 $logs = unserialize(file_get_contents($cache_filename));
112 // determine current revision number
113 $rev = $page->getSVNRevision();
114 if ($logs[0]['rev'] == $rev) return $logs;
116 // -q flag used to prevent server from sending log messages
117 $output = shell_exec("svn log -q --limit $limit $repos_url");
118 preg_match_all('/^r(\d+) /m', $output, $matches);
119 $ret = array();
120 foreach ($matches[1] as $rev) {
121 $log = svn_log($repos_url, (int) $rev);
122 $ret[] = $log[0]; // log is only one item long
124 // save to cache
125 file_put_contents($cache_filename, serialize($ret));
126 return $ret;