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
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
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
45 // Page header/footer/error handling/utility functions (date display/user links)
46 SO2
::$Page = new Page
;
49 SO2
::$Cfg = parse_ini_file('res/config.ini', true);
50 if ( isset(SO2
::$Cfg['site']['closed']) ) {
51 define('INVITE_ONLY', true);
55 SO2
::$Cache = new Memcache
;
56 SO2
::$Cache->pconnect('localhost');
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');
71 if ( defined('DEVELOPER') ) {
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);
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);
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
);
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
123 setcookie('u', '', 1);
124 setcookie('p', '', 1);
127 SO2
::$User = new User_Anonymous
;
131 else SO2
::$User = new User_Anonymous
;