Added an admin flag in users table,
[specialops2.git] / lib / class.page.php
blob707479df1ff0208221dad7b8e9960174f7cb6be0
1 <?php
2 // $Id$
4 class page
6 public $sitename = 'Special Ops';
7 public $title;
8 public $lang;
9 public $nav = array();
11 private $errors = array();
13 const Pageheader = 1;
14 const Userheader = 2;
15 public $headers = 0;
17 function __construct()
19 set_error_handler(array($this, 'error_handler'));
21 if ( strpos($_SERVER['PHP_SELF'], 'index') === false ) {
22 $this->nav['Board List'] = '.';
26 public function pageheader()
28 global $user, $DB;
30 switch ( $user->theme ) {
31 case -1:
32 $css = array('default', 'Default Theme'); break;
33 case 0:
34 $css = array('u'.$user->userid, 'User Theme'); break;
35 default:
36 $css = $DB->query('SELECT `css_file`, `theme_name` FROM `themes` WHERE `themeid` = '.$user->theme)->fetch_row();
39 header('Content-Type: application/xhtml+xml; charset=UTF-8');
40 if ( isset($_POST['login'], $_POST['u'], $_POST['p']) && ! ($user instanceof authuser) ) {
41 header('HTTP/1.1 400 Bad Request');
44 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',"\n",
45 '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB">',"\n",
46 "<head>\n",
47 ' <title>',( $this->title ? $this->title.' :: '.SITE_NAME : SITE_NAME ),"</title>\n",
48 ' <link rel="stylesheet" type="text/css" href="css/',$css[0],'.css" title="',$css[1],'"/>',"\n",
49 "</head>\n\n",
50 '<body id="so2-',basename($_SERVER['SCRIPT_NAME'], '.php'),"\">\n",
51 '<h1>',( $this->title ? $this->title : SITE_NAME ),"</h1>\n";
53 if ( isset($_POST['logout']) && $user instanceof anonuser ) {
54 echo '<p class="notice">You are now logged out.</p>',"\n";
55 } elseif ( isset($_POST['login'], $_POST['u'], $_POST['p']) ) {
56 if ( $user instanceof authuser ) {
57 $DB->query('UPDATE `users`
58 SET `last_login_ip` = INET_ATON(\''.$_SERVER['REMOTE_ADDR'].'\')
59 WHERE `userid` = @userid');
60 echo '<p class="notice">You are now logged in.</p>',"\n";
61 if ( $user instanceof authuser && $user->check_nullpass() ) {
62 echo '<p class="notice">Your password has been reset by an admin. You should <a href="passwd">set a new one</a>.</p>';
64 } else {
65 echo '<p class="error">Invalid password or username.</p>',"\n";
69 if ( $this->nav ) {
70 echo '<ul id="navbar" class="nl">',"\n";
71 foreach ( $this->nav as $title => $url ) {
72 echo "\t",'<li><a href="',$url,'">',$title,"</a></li>\n";
74 echo "</ul>\n\n";
77 $this->headers |= self::Pageheader;
80 private function finish_headers()
82 if ( ! ($this->headers & self::Pageheader) ) {
83 $this->pageheader();
85 if ( ! ($this->headers & self::Userheader) ) {
86 $GLOBALS['user']->userheader();
90 public function errorfooter($type = 'login')
92 if ( !headers_sent() ) {
93 switch ( $type ) {
94 case 'level':
95 header('HTTP/1.1 403 Forbidden');
96 break;
97 default:
98 header('HTTP/1.1 400 Bad Request');
102 $this->finish_headers();
104 $messages = array(
105 'level' => 'You do not have permission to view this page.',
106 'login' => 'You must be logged in to view this page.',
107 'logout' => 'You must be logged out to view this page.',
108 'boardid' => 'Invalid board ID given.',
109 'topicid' => 'Invalid topic ID given.',
110 'messageid' => 'Invalid message ID given.',
111 'userid' => 'Invalid user ID given.',
112 'runtime' => 'A serious error has occured in the page. '.
113 'If this happens again and you can\'t figure out what you did wrong, report it to the site owner.',
114 'request' => 'Invalid request.'
117 echo '<p class="error">',$messages[$type],'</p>';
118 $this->pagefooter();
121 public function pagefooter()
123 $this->finish_headers();
125 echo
126 "\n\n",
127 '<form id="footer" action="',
128 $_SERVER['PHP_SELF'],'?',htmlspecialchars($_SERVER['QUERY_STRING']),'" method="post">',"\n",
129 ' <fieldset><legend>Session:</legend>',"\n";
131 if ( $GLOBALS['user'] instanceof authuser ) {
132 echo
133 ' <button type="submit" name="logout" onclick="return confirm(\'o rly?\');">Log Out</button>';
134 } else {
135 echo
136 ' <label>Username: <input type="text" name="u"/></label>',"\n",
137 ' <label>Password: <input type="password" name="p"/></label>',"\n",
138 ' <button type="submit" name="login">Log In</button>';
141 echo "\n</fieldset>\n";
143 if ( defined('DEVELOPER') && count($this->errors) ) {
144 echo "<h2>Errors on page:</h2>\n<dl>\n";
145 foreach ( $this->errors as $e ) {
146 vprintf('<dt>File %s at line <var>%s</var></dt><dd>code <var>%s</var>: <q>%s</q></dd>', $e);
148 echo "</dl>\n";
151 /* An error count for normal users serves little practical value, but I can laugh at people
152 who install this thinking they can run it without reading the fucking instructions. */
153 echo
154 '<p>Special Ops Two: <a href="COPYING" rel="copyright">© 2004-2005 Ant P</a> | ',sprintf('%.3F', microtime(1) - CLOCK),'s | ',
155 ( defined('DEVELOPER') ? $GLOBALS['DB']->c.' queries' : count($this->errors).' errors' ),
156 ' | <a href="http://specialops.ath.cx/repos/so2/">',SO2VER,"</a></p>\n</form>\n",
157 '</body></html>';
159 exit;
162 public function error_handler($number, $string, $file, $line)
164 // If errors are being returned as unencoded text then try to fix them before they fuck the XHTML
165 if ( ! ini_get('html_errors') ) {
166 $string = new message($string);
167 $string = $string->output;
169 $this->errors[] = array($file, $line, $number, $string);