Forgot about this bit
[specialops2.git] / con.php
blobc3ec234761b74d0f251c189fd68ea360ebc0c33c
1 <?php
2 /**
3 * con.php: Generic setup file.
5 * Contains user authentication, database connection and other exciting stuff.
6 * Everything else depends on this file. Don't touch it unless you know what
7 * you're doing, all the config is done in other places (mostly mysql.php).
9 * @author Anthony Parsons (xmpp:ant@specialops.ath.cx)
10 * @license file://COPYING
11 * @version $Id$
14 // Numbers that go in the footer
15 define('SO2VER', '$Rev$');
16 define('CLOCK', microtime(1));
19 // Line noise
20 error_reporting(E_ALL|E_STRICT);
23 // SO2 loses its magic on anything less than PHP 5.1
24 if ( version_compare(PHP_VERSION, '5.1', '<') ) {
25 include 'res/server-error.inc';
26 throw new Exception('PHP 5.1 or higher is required to run SO2.');
30 // Classes
31 require 'lib/class.Page.php';
32 require 'lib/class.so2mysqli.php';
33 require 'lib/class.User_Anonymous.php';
34 class InvalidInputException extends Exception {}
35 class DatabaseException extends Exception {}
36 class RateLimitException extends Exception {}
37 function __autoload($classname)
39 require 'lib/class.'.$classname.'.php';
43 // Turn Dev mode on
44 if ( $_SERVER['REMOTE_ADDR'] === $_SERVER['SERVER_ADDR'] ) {
45 define('DEVELOPER', 1);
49 // Create page object, contains the error handler stuff.
50 $page = new page;
53 // Set up exception handler and database connection here
54 if ( defined('DEVELOPER') ) {
55 ob_start();
57 function e_handler($exception)
59 header('HTTP/1.1 500 Internal Server Error');
60 header('Content-Type: text/html; charset=UTF-8');
61 echo '<pre class="error">',$exception,'</pre>';
62 exit;
65 // DB
66 $dbtype = 'debugmysqli';
67 } else {
68 function e_handler($exception)
70 if ( !headers_sent() ) {
71 header('HTTP/1.1 500 Internal Server Error');
72 header('Content-Type: text/html; charset=UTF-8');
75 $GLOBALS['page']->errorfooter('runtime');
78 $dbtype = 'so2mysqli';
81 set_exception_handler('e_handler');
82 require 'mysql.php';
84 if ( mysqli_connect_errno() ) {
85 include 'res/server-error.inc';
86 throw new Exception('No database connection.');
90 // On-Login cookie setting hack
91 if ( isset($_POST['login'], $_POST['u'], $_POST['p']) ) {
92 list($_COOKIE['u'], $_COOKIE['p']) = array($_POST['u'], $_POST['p']);
95 // Destroy user cookie details on logout
96 if ( isset($_POST['logout']) ) {
97 setcookie('u', null, 1, '/');
98 setcookie('p', null, 1, '/');
99 unset($_COOKIE);
102 // Auth bit
103 if ( isset($_COOKIE['u'], $_COOKIE['p']) ) {
104 /* Try to get the user ID from the DB, and shove it into a MySQL var. */
105 $q = $DB->query('SELECT @userid := `userid` FROM `users`
106 WHERE `alias` = '.$DB->string($_COOKIE['u']).'
107 AND (`password` = AES_ENCRYPT('.$DB->string($_COOKIE['p']).', `reg_ip`)
108 OR `password` IS NULL)');
110 // orly
111 if ( 1 === $q->num_rows ) {
112 // Keep login cookie valid
113 setcookie('u', $_COOKIE['u'], time()+86400, '/');
114 setcookie('p', $_COOKIE['p'], time()+86400, '/');
116 $user = new User_Authenticated(isset($prefetch) ? $prefetch : null);
117 } else {
118 // Wipe cookies if bad login
119 setcookie('u', null, 1, '/');
120 setcookie('p', null, 1, '/');
121 unset($_COOKIE);
123 $user = new User_Anonymous;
125 } else
126 $user = new User_Anonymous;