Add oodles of documentation to Page class, do refactoring, add smoketest guidelines.
[htmlpurifier-web.git] / xhtml-compiler / XHTMLCompiler.php
blob2481e51559c482dac496fbec555b180ad34f0d31
1 <?php
3 /**
4 * Super singleton object that does everything. Used for unit testing.
5 */
6 // TODO: Factor out configuration and PHP wrapper/gateway, possibly
7 // also creating Filesystem subclass
8 class XHTMLCompiler
11 /** Private instance of singleton */
12 private static $_instance;
14 /** Private constructor, prevents other people from making it */
15 private function __construct() {
16 $this->loadConf();
19 /** Retrieves the single instance of the object */
20 static public function getInstance() {
21 if(is_null(self::$_instance)) {
22 self::$_instance = new self();
24 return self::$_instance;
27 /**
28 * Overloads the instance with another one, usually a mock object
29 * @param Object substitute for XHTMLCompiler
31 static public function setInstance($stub) {
32 // unit testing only!
33 self::$_instance = $stub;
36 protected $configKeys = array('allowed_dirs', 'directory_index', 'filename_chars');
37 protected $config = array();
39 public function loadConf() {
40 require 'config.default.php'; // defaults
41 require 'config.php'; // user
42 $this->config = compact($this->configKeys);
44 public function getConf($key) {
45 if (!isset($this->config[$key])) throw new Exception('No such configuration keypair');
46 return $this->config[$key];
49 /** Aborts script execution, equivalent to exit */
50 public function quit() {exit;}
52 /** Sends an HTTP header, equivalent to header() */
53 public function header() {
54 $args = func_get_args();
55 call_user_func_array('header', $args);
58 /** Outputs text, equivalent to echo */
59 public function paint($text) {echo $text;}
61 /** Retrieves the relative URI with which the page was requested. */
62 public function getRequestURI() {return $_SERVER['REQUEST_URI'];}
64 /** Retrieves the relative URI which denotes the frontend PHP file */
65 public function getPHPSelf() {return $_SERVER['PHP_SELF'];}
67 /** Retrieves a parameter from GET superglobal, does magic quote cleaning */
68 public function getGVal($key) {
69 if (!isset($_GET[$key])) return false;
70 $val = $_GET[$key];
71 // ad hoc magic_quotes protection, not comprehensive because
72 // we don't deal with very complicated parameters.
73 if (is_string($val) && get_magic_quotes_gpc()) $val = stripslashes($val);
74 return $val;
77 /** Returns true if passed filename is directory */
78 public function isDir($dir) {return is_dir($dir);}
80 /** Returns true if passed filename is file */
81 public function isFile($file) {return is_file($file);}
83 /** Resolves a relative path into an absolute one */
84 public function realpath($path) {return realpath($path);}
86 /** Returns HTTP status code server was originally going to send */
87 public function getRedirectStatus() {return (int) getenv("REDIRECT_STATUS");}