Profile the database using hoplite\data\ProfilingPDO instead of DB_MySQL_PDO.
[bugdar.git] / includes / init.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright (c)2002-2007 Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 error_reporting(E_ALL & ~E_NOTICE);
23
24 // ###################################################################
25 // initialize the database
26
27 define('ISSO_MT_START', microtime());
28 define('ISSO_CHECK_POST_REFERER', 1);
29
30 // TODO(port): Define this in index.php instead.
31 define('BUGDAR_ROOT', dirname(dirname(__FILE__)));
32
33 require_once('./includes/version.php');
34
35 require_once('./framework/kernel.php');
36 $bugsys = new ISSO();
37
38 $bugsys->setAppPath(getcwd());
39 $bugsys->setApplication('Bugdar');
40 $bugsys->setAppVersion(BUGDAR_VERSION);
41
42 if (!file_exists('./includes/config.php'))
43 {
44 echo 'includes/config.php needs to be present!';
45 exit;
46 }
47
48 require_once('./includes/config.php');
49
50 $bugsys->setDebug($debug);
51 define('DEVDEBUG', $debug);
52 unset($debug);
53
54 // ###################################################################
55 // init the big three
56 $bugsys->options = array();
57 $bugsys->userinfo = array();
58
59 class Bugdar
60 {
61 /*! @var \PDO The database connection instance. */
62 static $db = NULL;
63
64 /*! @var \hoplite\http\Input Input filter for the new stack. */
65 static $input = NULL;
66
67 static $options = array();
68 static $user = array();
69 static $datastore = array();
70 }
71
72 // ###################################################################
73 // Hoplite early initialization.
74
75 define('HOPLITE_ROOT', dirname(__FILE__) . '/hoplite');
76
77 if (DEVDEBUG) {
78 require_once HOPLITE_ROOT . '/base/profiling.php';
79 \hoplite\base\Profiling::EnableProfiling();
80 }
81
82 // ###################################################################
83 // Connect to the database.
84
85 $pdo_class = '\\PDO';
86 if (DEVDEBUG) {
87 require_once HOPLITE_ROOT . '/data/profiling_pdo.php';
88 $pdo_class = '\\hoplite\\data\\ProfilingPDO';
89 }
90 Bugdar::$db = new $pdo_class("mysql:dbname=$database;host=$servername", $username, $password);
91 unset($database, $servername, $username, $password);
92
93 // Let ISSO handle the database connection failure message.
94 $bugsys->load('db_mysql_pdo', 'db', true);
95 $db->connect(Bugdar::$db);
96
97 Bugdar::$db->SetAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
98
99 if ($utf8)
100 {
101 $db->query("SET NAMES utf8");
102 }
103
104 // don't use sql strict mode
105 $db->query("SET sql_mode = ''");
106
107 $bugsys->load('functions', 'funct', true);
108 $bugsys->load('xml', 'xml', true);
109
110 // change cookie expiration to one hour
111 $funct->cookieexp = 3600;
112
113 define('TABLE_PREFIX', $tableprefix);
114 define('COOKIE_PREFIX', $cookieprefix);
115
116 unset($usepconnect, $tableprefix, $cookieprefix);
117
118 require_once('./includes/functions_datastore.php');
119 require_once('./includes/functions.php');
120 require_once('./includes/language.php');
121
122 // ###################################################################
123 // Hoplite late initialization.
124
125 require_once HOPLITE_ROOT . '/data/model.php';
126 \hoplite\data\Model::set_db(Bugdar::$db);
127
128 require_once HOPLITE_ROOT . '/http/input.php';
129 Bugdar::$input = new \hoplite\http\Input(\hoplite\http\Input::TYPE_HTML);
130
131 // ###################################################################
132 // send nocache
133 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
134 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
135 header("Cache-Control: no-store, no-cache, must-revalidate");
136 header("Cache-Control: post-check=0, pre-check=0", false);
137 if ($utf8)
138 {
139 header("Content-Type: text/html; charset=\"utf8\"");
140 }
141 header("Pragma: no-cache");
142
143 // ###################################################################
144 // populate our caches
145 $db->showerrors = false;
146 $datastoretemp = $db->query("SELECT * FROM " . TABLE_PREFIX . "datastore");
147 $db->showerrors = true;
148
149 if (!$datastoretemp)
150 {
151 echo '<div style="font-size: 18px"><strong>Notice:</strong> This error could be caused if you have not installed Bugdar. If this is the case, you can run the installer <a href="install/install.php">here</a>.</div><br />';
152 $db->error('Invalid SQL error');
153 }
154
155 while ($store = $db->fetch_array($datastoretemp))
156 {
157 Bugdar::$datastore["$store[title]"] = unserialize($store['data']);
158 }
159
160 $bugsys->options = Bugdar::$datastore['setting'];
161 $bugsys->options['columnoptions'] = unserialize($bugsys->options['columnoptions']);
162 unset(Bugdar::$datastore['setting']);
163 Bugdar::$options = $bugsys->options;
164
165 $bugsys->setAppVersion($bugsys->options['trackerversion']);
166
167 // ###################################################################
168 // load permissions
169
170 require_once('./includes/permissions.php');
171
172 // ###################################################################
173 // load userinfo
174
175 $authMethod = ((defined('USE_DEFAULT_AUTH_METHOD') AND constant('USE_DEFAULT_AUTH_METHOD') == 1) ? 'default' : $bugsys->options['authmethod']);
176 require_once('./includes/auth/auth_' . $authMethod . '.php');
177
178 $authClass = 'Authentication' . str_replace(' ', '', ucwords(str_replace('_', ' ', $authMethod)));
179 $bugsys->auth = $auth = new $authClass();
180
181 if ($auth->authenticateCookies())
182 {
183 $bugsys->userinfo = $auth->fetchBugdarUser();
184 $bugsys->userinfo['permissions'] = FetchUserPermissions($bugsys->userinfo);
185 $bugsys->userinfo['displaytitle'] = Bugdar::$datastore['usergroup'][ $bugsys->userinfo['usergroupid'] ]['displaytitle'];
186 $bugsys->userinfo['columnoptions'] = unserialize($bugsys->userinfo['columnoptions']);
187 }
188 else
189 {
190 $bugsys->userinfo = fetch_guest_user();
191 }
192 Bugdar::$user = $bugsys->userinfo;
193
194 // ###################################################################
195 // initialize localization system
196
197 $language = fetch_user_language();
198
199 $stylevar['lang'] = $language['langcode'];
200 $stylevar['lang_dir'] = $language['direction'];
201 $stylevar['charset'] = $language['charset'];
202 $stylevar['left'] = ($language['direction'] == 'ltr' ? 'left' : 'right');
203 $stylevar['right'] = ($language['direction'] == 'ltr' ? 'right' : 'left');
204
205 require_once('./includes/definitions.php');
206
207 // ###################################################################
208 // initialize the date system
209 $bugsys->load('date', 'datef', true);
210 $datef->usertz = $bugsys->userinfo['timezone'] + ($bugsys->userinfo['usedst'] * 1);
211 $bugsys->debug('user tz = ' . $bugsys->userinfo['timezone'] . '; use version = ' . $datef->usertz);
212 $datef->fetch_offset();
213
214 // ###################################################################
215 // mail system
216 $bugsys->load('mail', 'mail', true);
217 $mail->setFromAddress($bugsys->options['webmasteremail']);
218 $mail->setFromName(T('Bugdar Notification'));
219