Make the stats page slightly less confusing
[specialops2.git] / lib / class.Page.php
blob4eed5a2b637164025400870f7afb370ef75abfb3
1 <?php
2 /**
3 * SO2 page class
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 class Page
12 public $title;
13 public $nav = array();
14 public $mtime = null;
15 public $cacheable = false;
17 private $errors = array();
19 const Pageheader = 1;
20 const Userheader = 2;
21 public $headers = 0;
23 function __construct()
25 set_error_handler(array($this, 'error_handler'));
27 if ( strpos($_SERVER['PHP_SELF'], 'index') === false ) {
28 $this->nav['Board List'] = '.';
32 public function pageheader()
34 global $user, $DB;
36 switch ( $user->theme ) {
37 case -1:
38 $css = array('default', 'Default Theme'); break;
39 case 0:
40 $css = array('u'.$user->userid, 'User Theme'); break;
41 default:
42 $css = $DB->query('SELECT `css_file`, `theme_name` FROM `themes` WHERE `themeid` = '.$user->theme)->fetch_row();
44 if ( basename($_SERVER['SCRIPT_NAME'], '.php') == 'theme' && isset($_POST['preview'], $_POST['user_css']) ) {
45 $css = array('css.php?'.base64_encode($_POST['user_css']).'#', 'Theme Preview');
48 if ( isset($_POST['login'], $_POST['u'], $_POST['p']) && ! ($user instanceof User_Authenticated) ) {
49 header('HTTP/1.1 400 Bad Request');
52 /* HTTP 1.1 caching bit: Page needs a mtime set for this to work */
53 if ( !empty($this->mtime) && strpos($user->options, 'cache') !== false && 'HTTP/1.1' == $_SERVER['SERVER_PROTOCOL'] ) {
54 $tmp = apache_request_headers();
56 /* only allow 304 response if the page is marked as cacheable */
57 if ( false == $this->cacheable ) {
58 header('Cache-Control: no-cache');
59 } elseif ( isset($tmp['If-Modified-Since']) && strtotime($tmp['If-Modified-Since']) == $this->mtime ) {
60 header('HTTP/1.1 304 Not Modified');
61 header('Cache-Control: must-revalidate');
62 header('Expires: '.date('r', $this->mtime+1));
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));
68 header('Expires: '.date('r', $this->mtime+1));
71 header('Content-Type: application/xhtml+xml; charset=UTF-8');
72 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',"\n",
73 '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB">',"\n",
74 "<head>\n",
75 ' <title>',( $this->title ? $this->title.' :: '.SITE_NAME : SITE_NAME ),"</title>\n",
76 ' <link rel="stylesheet" type="text/css" href="css/',$css[0],'.css" title="',$css[1],"\"/>\n";
78 if ( strpos($user->options, 'javascript') !== false ) {
79 // Global JS
80 echo ' <script src="js/global.js" type="application/x-javascript"/>',"\n";
82 // Page JS
83 if ( file_exists('js/'.basename($_SERVER['SCRIPT_NAME'], '.php').'.js') ) {
84 echo ' <script src="js/',basename($_SERVER['SCRIPT_NAME'], '.php'),'.js" type="application/x-javascript"/>',"\n";
88 echo
89 "</head>\n\n",
90 '<body id="so2-',basename($_SERVER['SCRIPT_NAME'], '.php'),"\">\n",
91 '<h1><span>',( $this->title ? $this->title : SITE_NAME ),"</span></h1>\n";
93 if ( isset($_POST['logout']) && $user instanceof User_Anonymous ) {
94 echo '<p class="notice">You are now logged out.</p>',"\n";
95 } elseif ( isset($_POST['login'], $_POST['u'], $_POST['p']) ) {
96 if ( $user instanceof User_Authenticated ) {
97 $DB->query('UPDATE `users` SET `last_login_ip` = INET_ATON("'.$_SERVER['REMOTE_ADDR'].'") WHERE `userid` = @userid');
98 echo '<p class="notice">You are now logged in.</p>',"\n";
99 if ( $user instanceof User_Authenticated && $user->check_nullpass() ) {
100 echo '<p class="notice">Your password is not set. You should <a href="passwd">set a new one</a>.</p>';
102 } else {
103 echo '<p class="error">Invalid login attempt.</p>',"\n";
107 if ( $this->nav ) {
108 echo '<ul id="navbar" class="nl">',"\n";
109 foreach ( $this->nav as $title => $url ) {
110 echo "\t",'<li><a href="',$url,'">',$title,"</a></li>\n";
112 echo "</ul>\n\n";
115 $this->headers |= self::Pageheader;
118 private function finish_headers()
120 if ( ! ($this->headers & self::Pageheader) ) {
121 $this->pageheader();
123 if ( ! ($this->headers & self::Userheader) ) {
124 $GLOBALS['user']->userheader();
128 public function errorfooter($type = 'login')
130 if ( !headers_sent() ) {
131 switch ( $type ) {
132 case 'level':
133 header('HTTP/1.1 403 Forbidden');
134 break;
135 default:
136 header('HTTP/1.1 400 Bad Request');
140 $this->finish_headers();
142 $messages = array(
143 'level' => 'You can\'t do this.',
144 'points' => 'You don\'t have enough points to use this feature.',
145 'login' => 'You must be logged in to do this.',
146 'logout' => 'You must be logged out to do this.',
147 'boardid' => 'Invalid board ID given.',
148 'topicid' => 'Invalid topic ID given.',
149 'messageid' => 'Invalid message ID given.',
150 'userid' => 'Invalid user ID given.',
151 'runtime' => 'A serious error has occured in the page. '.
152 'If this happens again and you don\'t think it\'s your fault, let the site owner know and they\'ll probably fix it. '.
153 'Unless the site owner is Aquatakat.', // yeah right, like he'd ever get off his ass and replace that shitty code.
154 'request' => 'Invalid request.'
157 echo '<p class="error">',$messages[$type],'</p>';
158 $this->pagefooter();
161 public function pagefooter()
163 $this->finish_headers();
165 if ( $GLOBALS['user'] instanceof User_Authenticated ) {
166 echo "\n\n",
167 '<form id="footer" action="." method="post">',"\n",
168 '<fieldset><legend>Session:</legend>',"\n",
169 ' <button type="submit" name="logout" onclick="return confirm(\'o rly?\');">Log Out</button>';
170 } else {
171 echo "\n\n",
172 '<form id="footer" action="',$_SERVER['PHP_SELF'],'?',htmlspecialchars($_SERVER['QUERY_STRING']),'" method="post">',"\n",
173 '<fieldset><legend>Login:</legend>',"\n",
174 ' <label>Username: <input type="text" name="u"/></label>',"\n",
175 ' <label>Password: <input type="password" name="p"/></label>',"\n",
176 ' <button type="submit" name="login">Log In</button>';
179 echo "\n</fieldset>\n";
181 if ( defined('DEVELOPER') && count($this->errors) ) {
182 echo "<h2>Errors on page:</h2>\n<dl>\n";
183 foreach ( $this->errors as $e ) {
184 vprintf('<dt>File %s at line <var>%s</var></dt><dd>errcode <var>%s</var>: <tt>%s</tt></dd>', $e);
186 echo "</dl>\n";
189 /* *wanks* */
190 printf('<p><a href="//specialops.ath.cx/p/">Special Ops 2</a>'.
191 ' | <a href="COPYING" rel="copyright">© 2004-06</a> Ant P | %.4f</p></form>', microtime(1)-T_START);
192 echo "\n\n</body>\n</html>";
194 exit;
197 public function error_handler($number, $string, $file, $line)
199 // If errors are being returned as unencoded text then try to fix them before they fuck the XHTML
200 if ( ! ini_get('html_errors') ) {
201 $string = new Message_Plaintext($string);
202 $string = $string->getOutput();
204 $this->errors[] = array($file, $line, $number, $string);