Special Ops 2.50
[specialops2.git] / con.php
bloba71f6b3438679fd9ed5b63813e8875b38a777831
1 <?php
2 /**
3 * con.php: Generic setup file.
5 * Contains user authentication, database connection and other exciting stuff.
6 * All the configuration stuff is done in res/config.ini now. Edit that, not this.
8 * @author Ant P <p@cpi.merseine.nu>
9 * @licence file://COPYING
10 * @version 2.50
13 define('MT_NOW', microtime(1)); // Used for page load time stats
14 define('T_NOW', time()); // Used to save calling time() everywhere
15 set_time_limit(4); // Prevent infinite loop stuff
17 // PHP version check
18 if ( version_compare(PHP_VERSION, '5.1', '<') ) {
19 include 'res/server-error.inc';
20 throw new Exception('PHP 5.1 or higher is required to run SO2.');
23 // Preload required classes - autoload seems slow for some reason
24 require 'lib/Page.php';
25 require 'lib/SO2_PDO.php';
26 require 'lib/User_Anonymous.php';
27 function __autoload($classname)
29 require 'lib/'.$classname.'.php';
31 // Utility classes for form-handling stuff
32 class InvalidInputException extends Exception {}
33 class RateLimitException extends Exception {}
36 // Namespace container for important bits
37 class SO2 {
38 static $DB;
39 static $Page;
40 static $User;
41 static $Cfg;
42 static $Cache;
45 // Page header/footer/error handling/utility functions (date display/user links)
46 SO2::$Page = new Page;
48 // Config
49 SO2::$Cfg = parse_ini_file('res/config.ini', true);
50 if ( isset(SO2::$Cfg['site']['closed']) ) {
51 define('INVITE_ONLY', true);
54 // Memcache
55 SO2::$Cache = new Memcache;
56 SO2::$Cache->pconnect('localhost');
58 // MySQL
59 try {
60 SO2::$DB = new SO2_PDO('mysql:dbname='.SO2::$Cfg['db']['name'], SO2::$Cfg['db']['uname'], SO2::$Cfg['db']['passwd'],
61 array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_EMULATE_PREPARES => false) );
62 SO2::$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
63 SO2::$Cfg['db']['passwd'] = null;
64 } catch ( PDOException $e ) {
65 include 'res/server-error.inc';
66 throw new Exception('Database connection died');
70 // Error handling
71 if ( defined('DEVELOPER') ) {
72 ob_start();
73 function e_handler($exception)
75 header('HTTP/1.1 500 Internal Server Error');
76 header('Content-Type: text/html; charset=UTF-8');
77 echo "<h1 onmouseover='blank_stare()'>OMFG!!1</h1>\n",
78 '<pre class="error">',$exception,'</pre>';
79 error_log($exception);
80 exit;
82 } else {
83 function e_handler($exception)
85 if ( ! headers_sent() ) {
86 header('HTTP/1.1 500 Internal Server Error');
87 header('Content-Type: text/html; charset=UTF-8');
89 error_log($exception);
90 SO2::$Page->message(Page::ERR_RUNTIME);
93 set_exception_handler('e_handler');
95 // Destroy cookies on logout
96 if ( isset($_POST['logout']) ) {
97 setcookie('u', '', 100);
98 setcookie('p', '', 100);
99 unset($_COOKIE);
101 // Set cookies on login
102 elseif ( isset($_POST['login'], $_POST['u'], $_POST['p']) ) {
103 list($_COOKIE['u'], $_COOKIE['p']) = array($_POST['u'], $_POST['p']);
106 // User authentication
107 if ( isset($_COOKIE['u'], $_COOKIE['p']) ) {
108 $userid = SO2::$DB->q('SELECT @userid := userid FROM users WHERE alias = ? '.
109 ' AND (passwd = AES_ENCRYPT(?, reg_ip) OR passwd IS NULL)',
110 array($_COOKIE['u'], $_COOKIE['p']), SO2_PDO::QVALUE);
111 // Logged in
112 if ( $userid ) {
113 setcookie('u', $_COOKIE['u'], T_NOW+172800);
114 setcookie('p', $_COOKIE['p'], T_NOW+172800);
116 require 'lib/User_Registered.php';
117 require 'lib/User_Authenticated.php';
118 SO2::$User = SO2::$Cache->get('user'.$userid)
119 or SO2::$User = new User_Authenticated($userid);
121 // Failed login attempt
122 else {
123 setcookie('u', '', 1);
124 setcookie('p', '', 1);
125 unset($_COOKIE);
127 SO2::$User = new User_Anonymous;
130 // Not logged in
131 else SO2::$User = new User_Anonymous;