Remove trailing ?>
[xhtml-compiler.git] / XHTMLCompiler / Page.php
blob8c7c7cfa31c001f3634a8113044b37cb4e3b21d8
1 <?php
3 /**
4 * Represents a page in our content management system. This is loosely
5 * bound to the filesystem, although it doesn't actually refer to a
6 * specific file, just a class of files.
7 */
8 class XHTMLCompiler_Page
11 /**
12 * Filename identifier of this page without extension
14 protected $pathStem;
16 /** File extension of source files (no period) */
17 protected $sourceExt = 'xhtml';
18 /** File extension of cache/served files */
19 protected $cacheExt = 'html';
20 /** File extension of dependency files */
21 protected $depsExt = 'xc-deps';
23 /** Instance of XHTMLCompiler_File for source file */
24 protected $source;
25 /** Instance of XHTMLCompiler_File for cache file */
26 protected $cache;
27 /** Instance of XHTMLCompiler_File for dependency file */
28 protected $deps;
30 /** Instance of XHTMLCompiler_Directory for all of the above files*/
31 protected $dir;
33 /**
34 * Constructs a page object, validates filename for correctness
35 * @param $path String path filename, can be from untrusted source
36 * @param $mute Whether or not to stop the class from complaining when
37 * the source file doesn't exist. This is a stopgap measure,
38 * please replace with better exception handling.
39 * @todo Cleanup into subroutines
40 * @todo Factor out allowed_directories realpath'ing to config class
42 public function __construct($path, $mute = false) {
44 $xc = XHTMLCompiler::getInstance();
45 $php = XHTMLCompiler::getPHPWrapper();
47 // test file extension
48 $info = pathinfo($path);
49 if (
50 empty($info['extension']) || (
51 $info['extension'] !== $this->sourceExt &&
52 $info['extension'] !== $this->cacheExt
54 ) {
55 throw new XHTMLCompiler_Exception(403, 'Forbidden extension',
56 'File extension cannot be processed by XHTML Compiler, check
57 for faulty <code>.htaccess</code> rules.');
60 // test for directory's existence and resolve to real path
61 $dir = $info['dirname'];
62 if ($dir == '.') $dir .= '/';
63 $dir = $php->realpath($dir);
64 if ($dir === false) {
65 throw new XHTMLCompiler_Exception(404, 'Missing directory',
66 'Requested directory cannot be found; check your file
67 path and try again.' );
69 if ($dir[strlen($dir)-1] == '/') $dir = substr($dir, 0, -1);
71 $allowed_dirs = $xc->getConf('allowed_dirs');
72 $ok = false;
74 foreach ($allowed_dirs as $allowed_dir => $recursive) {
75 $allowed_dir = $php->realpath($allowed_dir); // factor out!
76 if (!is_string($allowed_dir)) continue;
77 if ($dir === $allowed_dir) {
78 $ok = true;
79 break;
80 // slash is required to prevent $allowed_dir = 'subdir' from
81 // matching $dir = 'subdirectory', thanks Mordred!
82 } elseif (strpos($dir, $allowed_dir . '/') === 0 && $recursive) {
83 $ok = true;
84 break;
88 if (!$ok) throw new XHTMLCompiler_Exception(403, 'Forbidden directory',
89 'Requested directory is forbidden to XHTML Compiler; try
90 accessing it directly or check for faulty <code>.htaccess</code> rules.');
92 // cannot use pathinfo, since PATHINFO_FILENAME is PHP 5.2.0
93 $this->pathStem = substr($path, 0, strrpos($path, '.'));
95 // setup the files
96 $this->source = new XHTMLCompiler_File($this->pathStem . '.' . $this->sourceExt);
97 $this->cache = new XHTMLCompiler_File($this->pathStem . '.' . $this->cacheExt);
98 $this->deps = new XHTMLCompiler_File($this->pathStem . '.' . $this->depsExt);
100 $this->dir = new XHTMLCompiler_Directory(dirname($this->pathStem));
102 if (!$mute && !$this->source->exists()) {
103 // Apache may have redirected to an ErrorDocument which got directed
104 // via mod_rewrite to us, in that case, output the corresponding
105 // status code. Otherwise, we can give the regular 404.
106 $code = $php->getRedirectStatus();
107 if (!$code || $code == 200) $code = 404;
108 throw new XHTMLCompiler_Exception($code, 'Page not found', 'Requested page not found; check the URL in your address bar.');
112 // Note: Do not use this functions internally inside the class
114 /** Returns path stem, full filename without file extension */
115 public function getPathStem() { return $this->pathStem; }
116 /** Returns relative path to cache */
117 public function getCachePath() { return $this->cache->getName(); }
118 /** Returns relative path to source */
119 public function getSourcePath() { return $this->source->getName(); }
120 /** Returns XHTMLCompiler_Directory representation of directory */
121 public function getDir() { return $this->dir; }
122 /** Returns directory of the files without trailing slash */
123 public function getDirName() { return $this->dir->getName(); }
124 /** Returns directory of the files with trailing slash (unless there is none) */
125 public function getDirSName() { return $this->dir->getSName(); }
126 /** Returns how deep from the root the file is */
127 public function getDepth() { return substr_count($this->getSourcePath(), '/'); }
129 /** Normalizes a relative path as if it were from this page's directory */
130 public function normalizePath($path) {
131 return $this->getDirName() . '/' . $path;
135 * Returns a fully formed web path with web domain to the file. This path
136 * is valid anywhere on the web.
138 public function getWebPath() {
139 $xc = XHTMLCompiler::getInstance();
140 $domain = $xc->getConf('web_domain');
141 if (!$domain) {
142 throw new Exception('Configuration value web_domain must be set for command line');
144 return 'http://' . $domain . $this->getAbsolutePath();
148 * Returns a fully formed absolute web path valid anywhere on the
149 * current domain to the cached file.
151 public function getAbsolutePath() {
152 $xc = XHTMLCompiler::getInstance();
153 return $xc->getConf('web_path') . '/' . $this->cache->getName();
156 /** Returns contents of the cache/served file */
157 public function getCache() { return $this->cache->get(); }
158 /** Returns contents of the source file */
159 public function getSource() { return $this->source->get(); }
161 /** Reports whether or not cache file exists and is a file */
162 public function isCacheExistent() { return $this->cache->exists(); }
163 /** Reports whether or not source file exists and is a file */
164 public function isSourceExistent() { return $this->source->exists(); }
167 * Reports whether or not the cache is stale by comparing the file
168 * modification times between the source file and the cache file.
169 * @warning You must not call this function until you've also called
170 * isCacheExistent().
172 public function isCacheStale() {
173 if (!$this->cache->exists()) {
174 throw new Exception('Cannot check for stale cache when cache
175 does not exist, please call isCacheExistent and take
176 appropriate action with the result');
178 if ($this->source->getMTime() > $this->cache->getMTime()) return true;
179 // check dependencies
180 if (!$this->deps->exists()) return true; // we need a dependency file!
181 $deps = unserialize($this->deps->get());
182 foreach ($deps as $filename => $time) {
183 if ($time < filemtime($filename)) return true;
185 return false;
189 * Writes text to the cache file, overwriting any previous contents
190 * and creating the cache file if it doesn't exist.
191 * @param $contents String contents to write to cache
193 public function writeCache($contents) {$this->cache->write($contents);}
196 * Attempts to display contents from the cache, otherwise returns false
197 * @return True if successful, false if not.
198 * @todo Purge check needs to be factored into XHTMLCompiler
200 public function tryCache() {
201 if (
202 !isset($_GET['purge']) &&
203 $this->cache->exists() &&
204 !$this->isCacheStale()
206 // cached version is fresh, serve it. This shouldn't happen normally
207 set_response_code(200); // if we used ErrorDocument, override
208 readfile($this->getCachePath());
209 return true;
211 return false;
215 * Generates the final version of a page from the source file and writes
216 * it to the cache.
217 * @note This function needs to be extended greatly
218 * @return Generated contents from source
220 public function generate() {
221 $source = $this->source->get();
222 $xc = XHTMLCompiler::getInstance();
223 $filters = $xc->getFilterManager();
224 $contents = $filters->process($source, $this);
225 $deps = $filters->getDeps();
226 if (empty($contents)) return ''; // don't write, probably an error
227 $contents .= '<!-- generated by XHTML Compiler -->';
228 $this->cache->write($contents);
229 $this->cache->chmod(0664);
230 $this->deps->write(serialize($deps));
231 return $contents;
235 * Displays the page, either from cache or fresh regeneration.
237 public function display() {
238 if($this->tryCache()) return;
239 $ret = $this->generate();
240 if ($ret) {
241 if (stripos($_SERVER["HTTP_ACCEPT"], 'application/xhtml+xml') !== false) {
242 header("Content-type: application/xhtml+xml");
243 } else {
244 header("Content-type: text/html");
247 echo $ret;
251 * Retrieves the Git_Repo that represents this page.
253 public function getRepo() {
254 return new Git_Repo($this->source->getDirectory());
258 * Retrieves the filename relative to the Git repository root.
260 public function getGitPath() {
261 $repo = $this->getRepo();
262 // This won't work with bare repositories
263 return $name = str_replace(
264 '\\', '/', // account for Windows
265 substr(
266 realpath($this->source->getName()), // $repo->path is full
267 strlen(dirname($repo->path))+1 // chop off "repo" path (w/o .git) + leading slash
273 * Retrieves the log that represents this page.
275 public function getLog() {
276 // This doesn't account for sub-repositories
277 $repo = $this->getRepo();
278 return $repo->log('master', array($this->getGitPath()), array('follow' => true));
281 // this is metadata stuff that needs to be moved and cached
284 * Retrieves the DateTime this page was created, according to Git's logs.
286 public function getCreatedTime() {
287 $repo = $this->getRepo();
288 // This is extremely memory inefficient, but I can't figure out
289 // how to get Git to limit the commits (-n) without undoing
290 // --reverse.
291 $log = $repo->log('master', array($this->getGitPath()), array(
292 'reverse' => true,
294 if (empty($log)) return;
295 return $log[0]->authoredDate;
299 * Retrieves the DateTime this page was last updated, according to Git's logs.
301 public function getLastModifiedTime() {
302 $repo = $this->getRepo();
303 $log = $repo->log('master', array($this->getGitPath()), array(
304 'n' => 1,
306 if (empty($log)) return;
307 // or committedDate?
308 return $log[0]->authoredDate;