Cleaned up the user classes and page class a bit
[specialops2.git] / lib / class.page.php
blobecbcded1c0f42e9db1a04066da0e383acbdf5094
1 <?php
2 // $Id$
4 class page
6 public $sitename = 'Special Ops';
7 public $title;
8 public $lang;
9 public $errors = array();
10 public $nav = array();
12 const Pageheader = 1;
13 const Userheader = 2;
14 public $headers = 0;
16 function __construct()
18 set_error_handler(array($this, 'error_handler'));
20 if ( strpos($_SERVER['PHP_SELF'], 'index') === false )
21 $this->nav['Board List'] = '.';
24 function pageheader()
26 global $user, $DB;
28 switch ( $user->theme ) {
29 case -1:
30 $css = 'default.css'; break;
31 case 0:
32 $css = 'u'.$user->userid.'.css'; break;
33 default:
34 list($css) = $DB->query('SELECT `css_file` FROM `themes`
35 WHERE `themeid` = '.$user->theme)->fetch_row();
38 if ( defined('DEVELOPER') && isset($_GET['viewsrc']) )
39 header('Content-Type: text/plain; charset=UTF-8');
40 else
41 header('Content-Type: application/xhtml+xml; charset=UTF-8');
43 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',"\n",
44 '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB">',"\n",
45 "<head>\n",
46 ' <title>',$this->title,' :: ',$this->sitename,"</title>\n",
47 ' <link rel="stylesheet" type="text/css" href="css/',$css,'"/>',"\n",
48 "</head>\n\n",
49 "<body>\n",
50 '<h1>',$this->title,"</h1>\n";
52 if ( isset($_POST['logout']) && $user instanceof anonuser )
53 echo '<p class="notice">You are now logged out.</p>',"\n";
54 elseif ( isset($_POST['login'], $_POST['u'], $_POST['p']) ) {
55 if ( $user instanceof authuser ) {
56 $DB->query('UPDATE `users`
57 SET `last_login_ip` = INET_ATON(\''.$_SERVER['REMOTE_ADDR'].'\')
58 WHERE `userid` = @userid');
59 echo '<p class="notice">You are now logged in.</p>',"\n";
60 } else
61 echo '<p class="error">Username and/or password is invalid.</p>',"\n";
64 if ( $user instanceof authuser && $user->password === null )
65 echo '<p class="notice">Your password has been reset by an admin.',"\n",
66 ' You should <a href="passwd">set a new one</a>.</p>';
68 if ( $this->nav ) {
69 echo '<ul id="navbar" class="nl">',"\n";
70 foreach ( $this->nav as $title => $url )
71 echo "\t",'<li><a href="',$url,'">',$title,"</a></li>\n";
72 echo "</ul>\n\n";
75 $this->headers |= self::Pageheader;
78 function errorfooter($type = 'login', $var = null, $var2 = null)
80 if ( !headers_sent() )
81 switch ( $type ) {
82 case 'level':
83 case 'levelpoints':
84 header('HTTP/1.1 403 Forbidden'); break;
85 default:
86 header('HTTP/1.1 400 Bad Request');
89 if ( ! ($this->headers & self::Pageheader) )
90 $this->pageheader();
91 if ( ! ($this->headers & self::Userheader) )
92 $user->userheader();
93 $messages = array(
94 'level' => 'You must be at least level '.$var.' to view this page.',
95 'levelpoints' => 'You must be at least level '.$var.' and have '.$var2.' points to view this page.',
96 'login' => 'You must be logged in to view this page.',
97 'logout' => 'You must be logged out to view this page.',
98 'boardid' => 'Invalid board ID given.',
99 'topicid' => 'Invalid topic ID given.',
100 'messageid' => 'Invalid message ID given.',
101 'userid' => 'Invalid user ID given.',
102 'runtime' => 'A serious error has occured in the page. '.
103 'If this happens again, please report it to the site owner.'
105 echo '<p class="error">',$messages[$type],'</p>';
106 $this->pagefooter();
109 function pagefooter()
111 echo "\n\n",
112 '<form id="footer" action="',
113 $_SERVER['PHP_SELF'],'?',htmlspecialchars($_SERVER['QUERY_STRING']),'" method="post">',"\n",
114 ' <fieldset><legend>Session:</legend>',"\n";
116 if ( $GLOBALS['user'] instanceof authuser )
117 echo
118 ' <button type="submit" name="logout" onclick="return confirm(\'o rly?\');">Log Out</button>';
119 else
120 echo
121 ' <label>Username: <input type="text" name="u"/></label>',"\n",
122 ' <label>Password: <input type="password" name="p"/></label>',"\n",
123 ' <button type="submit" name="login">Log In</button>';
125 echo "\n</fieldset>\n";
127 if ( defined('DEVELOPER') && count($this->errors) ) {
128 echo "<h2>Errors on page:</h2>\n<dl>\n";
129 foreach ( $this->errors as $e )
130 vprintf('<dt>File %s at line <var>%s</var></dt><dd>code <var>%s</var>: <q>%s</q></dd>', $e);
131 echo "</dl>\n";
134 /* An error count for normal users serves little practical value, but I can laugh at people
135 who install this thinking they can run it without reading the fucking instructions. */
136 echo
137 '<p><a href="COPYING" rel="copyright">© 2004-2005 Ant P</a> | ',round(microtime(1) - CLOCK, 4),'s | ',
138 ( defined('DEVELOPER') ? $GLOBALS['DB']->c.' queries' : count($this->errors).' errors' ),
139 ' | <a href="http://specialops.ath.cx/repos/so2/">',SO2VER,"</a></p>\n</form>\n",
140 '</body></html>';
142 exit;
145 function error_handler($number, $string, $file, $line)
147 // If errors are being returned plaintext with all the <>&s then try to fix them before they fuck the XHTML
148 if ( ! ini_get('html_errors') ) {
149 $string = new message($string);
150 $string = $string->output;
152 $this->errors[] = array($file, $line, $number, $string);