Reduce the JS loop speed to 60 seconds, since it's not likely to be needed anyway
[specialops2.git] / lib / class.XPage.php
blob67125478dc006a828442b8af83b0f5132caa5a17
1 <?php
2 /**
3 * SO2 page class using DOM which doesn't work
4 * Handles the headers, footers and error output
6 * @author Anthony Parsons (xmpp:ant@specialops.ath.cx)
7 * @license file://../COPYING
8 * @version $Id$
9 */
10 abstract class XPage
12 public $title;
13 public $nav = array();
14 public $mtime = null;
15 public $cacheable = false;
17 private $errors = array();
19 private $htmlhead;
21 const Pageheader = 1;
22 const Userheader = 2;
23 public $headers = 0;
25 function __construct()
27 set_error_handler(array($this, 'error_handler'));
29 if ( strpos($_SERVER['PHP_SELF'], 'index') === false ) {
30 $this->nav['Board List'] = '.';
33 $htmlhead = new SimpleXML;
36 public function pageheader()
38 global $user, $DB;
40 switch ( $user->theme ) {
41 case -1:
42 $css = array('default', 'Default Theme'); break;
43 case 0:
44 $css = array('u'.$user->userid, 'User Theme'); break;
45 default:
46 $css = $DB->query('SELECT `css_file`, `theme_name` FROM `themes` WHERE `themeid` = '.$user->theme)->fetch_row();
49 if ( isset($_POST['login'], $_POST['u'], $_POST['p']) && ! ($user instanceof User_Authenticated) ) {
50 header('HTTP/1.1 400 Bad Request');
53 /* HTTP 1.1 caching bit: Page needs a mtime set for this to work */
54 if ( !empty($this->mtime) && strpos($user->options, 'cache') !== false && 'HTTP/1.1' == $_SERVER['SERVER_PROTOCOL'] ) {
55 $tmp = apache_request_headers();
57 /* only allow 304 response if the page is marked as cacheable */
58 if ( false == $this->cacheable ) {
59 header('Cache-Control: no-cache');
60 } elseif ( isset($tmp['If-Modified-Since']) && strtotime($tmp['If-Modified-Since']) == $this->mtime ) {
61 header('HTTP/1.1 304 Not Modified');
62 header('Cache-Control: must-revalidate');
63 exit;
66 /* Send the mtime anyway if it's available since someone might want to use it for something */
67 header('Last-Modified: '.date('r', $this->mtime));
70 header('Content-Type: application/xhtml+xml; charset=UTF-8');
71 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',"\n",
72 '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB">',"\n",
73 "<head>\n",
74 ' <title>',( $this->title ? $this->title.' :: '.SITE_NAME : SITE_NAME ),"</title>\n",
75 ' <link rel="stylesheet" type="text/css" href="css/',$css[0],'.css" title="',$css[1],"\"/>\n";
77 if ( strpos($user->options, 'javascript') !== false ) {
78 // Global JS
79 echo ' <script src="js/global.js" type="application/x-javascript"/>',"\n";
81 // Page JS
82 if ( file_exists('js/'.basename($_SERVER['SCRIPT_NAME'], '.php').'.js') ) {
83 echo ' <script src="js/',basename($_SERVER['SCRIPT_NAME'], '.php'),'.js" type="application/x-javascript"/>',"\n";
87 echo
88 "</head>\n\n",
89 '<body id="so2-',basename($_SERVER['SCRIPT_NAME'], '.php'),"\">\n",
90 '<h1><span>',( $this->title ? $this->title : SITE_NAME ),"</span></h1>\n";
92 if ( isset($_POST['logout']) && $user instanceof User_Anonymous ) {
93 echo '<p class="notice">You are now logged out.</p>',"\n";
94 } elseif ( isset($_POST['login'], $_POST['u'], $_POST['p']) ) {
95 if ( $user instanceof User_Authenticated ) {
96 $DB->query('UPDATE `users` SET `last_login_ip` = INET_ATON("'.$_SERVER['REMOTE_ADDR'].'") WHERE `userid` = @userid');
97 echo '<p class="notice">You are now logged in.</p>',"\n";
98 if ( $user instanceof User_Authenticated && $user->check_nullpass() ) {
99 echo '<p class="notice">Your password is not set. You should <a href="passwd">set a new one</a>.</p>';
101 } else {
102 echo '<p class="error">Invalid login attempt.</p>',"\n";
106 if ( $this->nav ) {
107 echo '<ul id="navbar" class="nl">',"\n";
108 foreach ( $this->nav as $title => $url ) {
109 echo "\t",'<li><a href="',$url,'">',$title,"</a></li>\n";
111 echo "</ul>\n\n";
114 $this->headers |= self::Pageheader;
117 private function finish_headers()
119 if ( ! ($this->headers & self::Pageheader) ) {
120 $this->pageheader();
122 if ( ! ($this->headers & self::Userheader) ) {
123 $GLOBALS['user']->userheader();
127 public function errorfooter($type = 'login')
129 if ( !headers_sent() ) {
130 switch ( $type ) {
131 case 'level':
132 header('HTTP/1.1 403 Forbidden');
133 break;
134 default:
135 header('HTTP/1.1 400 Bad Request');
139 $this->finish_headers();
141 $messages = array(
142 'level' => 'You can\'t do this.',
143 'login' => 'You must be logged in to do this.',
144 'logout' => 'You must be logged out to do this.',
145 'boardid' => 'Invalid board ID given.',
146 'topicid' => 'Invalid topic ID given.',
147 'messageid' => 'Invalid message ID given.',
148 'userid' => 'Invalid user ID given.',
149 'runtime' => 'A serious error has occured in the page. '.
150 'If this happens again and you don\'t think it\'s your fault, let the site owner know and they\'ll probably fix it. '.
151 'Unless the site owner is Aquatakat.', // yeah right, like he'd ever get off his ass and replace that shitty code.
152 'request' => 'Invalid request.'
155 echo '<p class="error">',$messages[$type],'</p>';
156 $this->pagefooter();
159 public function pagefooter()
161 $this->finish_headers();
163 if ( $GLOBALS['user'] instanceof User_Authenticated ) {
164 echo "\n\n",
165 '<form id="footer" action="." method="post">',"\n",
166 '<fieldset><legend>Session:</legend>',"\n",
167 ' <button type="submit" name="logout" onclick="return confirm(\'o rly?\');">Log Out</button>';
168 } else {
169 echo "\n\n",
170 '<form id="footer" action="',$_SERVER['PHP_SELF'],'?',htmlspecialchars($_SERVER['QUERY_STRING']),'" method="post">',"\n",
171 '<fieldset><legend>Login:</legend>',"\n",
172 ' <label>Username: <input type="text" name="u"/></label>',"\n",
173 ' <label>Password: <input type="password" name="p"/></label>',"\n",
174 ' <button type="submit" name="login">Log In</button>';
177 echo "\n</fieldset>\n";
179 if ( defined('DEVELOPER') && count($this->errors) ) {
180 echo "<h2>Errors on page:</h2>\n<dl>\n";
181 foreach ( $this->errors as $e ) {
182 vprintf('<dt>File %s at line <var>%s</var></dt><dd>errcode <var>%s</var>: <tt>%s</tt></dd>', $e);
184 echo "</dl>\n";
187 /* *wanks* */
188 echo
189 '<p><a href="//specialops.ath.cx">Special Ops Two</a>: <a href="COPYING" rel="copyright">© 2004-2006</a> Ant P | ',
190 '<a href="//specialops.ath.cx/repos/so2/">',SO2VER,"</a></p>\n</form>\n",
191 '</body></html>';
193 exit;
196 public function error_handler($number, $string, $file, $line)
198 // If errors are being returned as unencoded text then try to fix them before they fuck the XHTML
199 if ( ! ini_get('html_errors') ) {
200 $string = new Message_Plaintext($string);
201 $string = $string->getOutput();
203 $this->errors[] = array($file, $line, $number, $string);