Rewrote every file for readability,
[specialops2.git] / con.php
blob271d13a47817ba66c87a4ec984b39b0fd9849af2
1 <?php
2 // $Id$
4 if ( version_compare(PHP_VERSION, '5.0', '<') ) {
5 header('HTTP/1.1 500 Internal Server Error');
6 die('Fatal Server Error: PHP 5.0 or higher is required.');
9 error_reporting(E_ALL|E_STRICT);
11 define('CLOCK', microtime(1));
12 define('SO2VER', '$Rev$');
15 // Empty exception types
16 class InvalidInputException extends Exception {}
17 class DatabaseException extends Exception {}
18 class RateLimitException extends Exception {}
20 // SPL exception names only in >=5.1
21 if (version_compare(PHP_VERSION, '5.1', '<')) {
22 class OutOfBoundsException extends Exception {}
23 class RuntimeException extends Exception {}
24 class LengthException extends Exception {}
28 // Keep these numbers in order, and don't change them unless your DB is empty
29 define('LVL_RESTRICTED', 0);
30 define('LVL_USER', 10);
31 define('LVL_ADMIN', 60);
32 define('LVL_DEV', 70);
34 // Class loader
35 function __autoload($classname)
37 require 'lib/class.'.$classname.'.php';
41 // Debug settings
42 if ( !isset($_SERVER['REMOTE_ADDR']) || $_SERVER['REMOTE_ADDR'] === $_SERVER['SERVER_ADDR'] )
43 define('DEVELOPER', 1);
46 // Doing this early to register the error handler
47 $page = new page;
50 // Exception handler
51 if ( defined('DEVELOPER') ) {
52 ob_start();
54 function e_handler($exception)
56 header('HTTP/1.1 500 Internal Server Error');
57 header('Content-Type: text/html; charset=UTF-8');
58 echo '<pre class="error">',$exception->__toString(),'</pre>';
59 exit;
61 } else {
62 function e_handler($exception)
64 restore_error_handler();
65 ini_set('display_errors', 0);
67 if ( !headers_sent() ) {
68 header('HTTP/1.1 500 Internal Server Error');
69 header('Content-Type: text/html; charset=UTF-8');
72 echo '<p class="error">An unrecoverable error has been detected and page execution has been halted.</p>',"\n",
73 '<p class="error">Try reloading the page, or tell the site owner if this happens again.</p>';
74 trigger_error($exception->getMessage(), E_USER_ERROR);
75 exit;
78 set_exception_handler('e_handler');
81 // Database thingers
82 require 'mysql_ident.php';
84 if ( defined('DEVELOPER') ) {
85 mysqli_report(MYSQLI_REPORT_ALL);
86 $DB = new debugmysqli($DB['host'], $DB['user'], $DB['pass'], DATABASE_NAME);
87 } else
88 $DB = new mysqli($DB['host'], $DB['user'], $DB['pass'], DATABASE_NAME);
90 if ( mysqli_connect_errno() ) {
91 header('HTTP/1.1 500 Internal Server Error');
92 die('Server error connecting to DB');
93 } elseif ( version_compare($DB->server_info, '5.0', '<') ) {
94 header('HTTP/1.1 500 Internal Server Error');
95 die('Server error: MySQL 5.0 not found');
98 // Login cookie setting hack
99 if ( isset($_POST['login'], $_POST['u'], $_POST['p']) )
100 list($_COOKIE['u'], $_COOKIE['p']) = array($_POST['u'], $_POST['p']);
103 // User Auth+object
104 if ( isset($_POST['logout']) ) {
105 setcookie('u', null, 1);
106 setcookie('p', null, 1);
107 unset($_COOKIE);
108 $user = new anonuser;
109 } elseif ( isset($_COOKIE['u'], $_COOKIE['p']) ) {
110 $q = $DB->query('SELECT `userid` FROM `users`
111 WHERE `alias` = \''.$DB->escape_string($_COOKIE['u']).'\'
112 AND (`password` = AES_ENCRYPT(\''.$DB->escape_string($_COOKIE['p']).'\', `reg_ip`) OR
113 `password` IS NULL)');
115 if ( 1 === $q->num_rows ) {
116 // Tell the DB your userid
117 $DB->query('SET @userid = '.$q->fetch_object()->userid);
119 setcookie('u', $_COOKIE['u'], time()+86400*7);
120 setcookie('p', $_COOKIE['p'], time()+86400*7);
122 $user = new authuser(isset($prefetch) ? $prefetch : null);
123 } else {
124 setcookie('u', null, 1);
125 setcookie('p', null, 1);
127 $user = new anonuser;
129 } else
130 $user = new anonuser;