1 |
<?php |
2 |
/*------------------------------------------------------------------------------ |
3 |
--- www.netfrag.org |
4 |
--- Setup and common functions include file. |
5 |
-------------------------------------------------------------------------------- |
6 |
--- rabit, 04:31 24.08.2004 |
7 |
--- $Id: common.php.inc,v 1.10 2004/09/04 03:06:10 rabit Exp $ |
8 |
------------------------------------------------------------------------------*/ |
9 |
|
10 |
//------------------------------------------------------------------------------ |
11 |
//- Benchmarking: |
12 |
|
13 |
// Create the benchmark steps array inside "$common": |
14 |
$common['benchmark'] = array(); |
15 |
|
16 |
function common_benchmark_addstep($caption) { |
17 |
|
18 |
global $common; |
19 |
|
20 |
$step = array($caption, microtime()); |
21 |
|
22 |
array_push($common['benchmark'], $step); |
23 |
|
24 |
} |
25 |
|
26 |
// The starting entry in the benchmark steps list: |
27 |
common_benchmark_addstep('common: start'); |
28 |
|
29 |
//---------------------------------------------------------- |
30 |
//- Developer host setups: |
31 |
|
32 |
$hostsetups = array( |
33 |
|
34 |
'default' => array( |
35 |
'urlrel' => '/nfo/', |
36 |
'devstate' => false, |
37 |
'mysql_host' => 'localhost', |
38 |
'mysql_user' => 'nfo', |
39 |
'mysql_pass' => 'b2-cV5RF', |
40 |
'mysql_db' => 'nfo' |
41 |
), |
42 |
|
43 |
'psl.no-ip.com' => array( |
44 |
'urlrel' => '/work/www.netfrag.org/', |
45 |
'devstate' => true, |
46 |
'mysql_host' => 'localhost', |
47 |
'mysql_user' => 'php', |
48 |
'mysql_pass' => 'A289tpQ1', |
49 |
'mysql_db' => 'nfo' |
50 |
), |
51 |
|
52 |
); |
53 |
|
54 |
// Set the default host setup: |
55 |
$common['hostsetup'] = $hostsetups['default']; |
56 |
|
57 |
// Set the host setup if a listed host name is given: |
58 |
if(isset($hostsetups[$_SERVER['SERVER_NAME']])) $common['hostsetup'] = $hostsetups[$_SERVER['SERVER_NAME']]; |
59 |
|
60 |
unset($hostsetups); |
61 |
|
62 |
common_benchmark_addstep('common: host setup'); |
63 |
|
64 |
//---------------------------------------------------------- |
65 |
//- Site variable setups: |
66 |
|
67 |
// Paths: |
68 |
$common['site']['docroot'] = $_SERVER['DOCUMENT_ROOT'] . $common['hostsetup']['urlrel']; |
69 |
$common['site']['incroot'] = $common['site']['docroot'] . 'inc/'; |
70 |
$common['site']['libroot'] = $common['site']['docroot'] . 'libs/'; |
71 |
|
72 |
// URLs: |
73 |
$common['site']['url'] = 'http://' . $_SERVER['HTTP_HOST'] . $common['hostsetup']['urlrel']; |
74 |
$common['site']['gfxurl'] = $common['site']['url'] . 'gfx/'; |
75 |
|
76 |
//---------------------------------------------------------- |
77 |
//- Page variable setups: |
78 |
|
79 |
$common['page']['filename'] = substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '/') + 1); |
80 |
$common['page']['url'] = $_SERVER['PHP_SELF']; |
81 |
|
82 |
common_benchmark_addstep('common: site/page setup'); |
83 |
|
84 |
//------------------------------------------------------------------------------ |
85 |
//- Includes: |
86 |
|
87 |
//common_include('cfg/cfg.php.inc'); |
88 |
//common_benchmark_addstep('common: CFG included'); |
89 |
|
90 |
include($common['site']['incroot'] . 'xmlcp/xmlcp.php.inc'); |
91 |
common_benchmark_addstep('common: XMLCP included'); |
92 |
|
93 |
include($common['site']['incroot'] . 'cms/cms.php.inc'); |
94 |
common_benchmark_addstep('common: CMS included'); |
95 |
|
96 |
//------------------------------------------------------------------------------ |
97 |
//- MySQL connection: |
98 |
|
99 |
common_dbc_connectdb(); |
100 |
common_benchmark_addstep('common: connect database'); |
101 |
|
102 |
//------------------------------------------------------------------------------ |
103 |
//- Session setup: |
104 |
|
105 |
if(!isset($common['client']['session_enabled'])) { |
106 |
|
107 |
// Neither proxies, nor the clients are allowed to cache session data: |
108 |
session_cache_limiter('nocache'); |
109 |
|
110 |
// This is neccessary to make the $_SESSION global available: |
111 |
session_start(); |
112 |
|
113 |
} |
114 |
|
115 |
common_benchmark_addstep('common: session init'); |
116 |
|
117 |
if(!isset($_SESSION['common_sessiondata'])) { |
118 |
|
119 |
// The session variable isn't set, create it: |
120 |
|
121 |
common_benchmark_addstep('common: New session: start'); |
122 |
|
123 |
$common_sessiondata = array( |
124 |
|
125 |
'birthtime' => time(), |
126 |
'firstrequest' => 1, // Mark the very first page request. |
127 |
|
128 |
// User data and authorisation: |
129 |
'user_auth' => array( |
130 |
'name' => '', |
131 |
'password' => '', |
132 |
'authorised' => false |
133 |
), |
134 |
|
135 |
// User preferences and preferred settings: |
136 |
'user_prefs' => array( |
137 |
'benchlist' => null, |
138 |
'debug' => null, |
139 |
'language_id' => null, |
140 |
'outputtype' => null |
141 |
), |
142 |
|
143 |
// Content related additional data: |
144 |
'additionaldata' => array() |
145 |
|
146 |
); |
147 |
|
148 |
common_benchmark_addstep('common: New session: create session'); |
149 |
|
150 |
// Protocol the visitors hit and store the columns insertion ID: |
151 |
$common_sessiondata['hit_id'] = common_protocolhit(); |
152 |
|
153 |
common_benchmark_addstep('common: New session: protocol hit'); |
154 |
|
155 |
// Store a reference to the session data array: |
156 |
$_SESSION['common_sessiondata'] = &$common_sessiondata; |
157 |
|
158 |
common_benchmark_addstep('common: New session: store session/end'); |
159 |
|
160 |
} else { |
161 |
|
162 |
// Restore the session data array reference: |
163 |
$common_sessiondata = &$_SESSION['common_sessiondata']; |
164 |
|
165 |
// Reset the first page request flag: |
166 |
$common_sessiondata['firstrequest'] = null; |
167 |
|
168 |
// Update the request count in the "hits" table: |
169 |
|
170 |
$sql = "UPDATE hits SET requestcount=requestcount+1, lastrequest=NOW() WHERE id='" . $common_sessiondata['hit_id'] . "';"; |
171 |
|
172 |
common_dbc_query($sql); |
173 |
|
174 |
common_benchmark_addstep('common: session/hit data updated'); |
175 |
|
176 |
} |
177 |
|
178 |
//------------------------------------------------------------------------------ |
179 |
//- Functions: |
180 |
|
181 |
function common_authorise($username, $password) { |
182 |
|
183 |
global $common_sessiondata; |
184 |
|
185 |
if((strlen($username) < 2) || (strlen($password) < 2)) return false; |
186 |
|
187 |
$sql = "SELECT id, rights, logincount, lastlogin FROM users WHERE name='$username' AND password='$password';"; |
188 |
|
189 |
$res = common_dbc_query($sql); |
190 |
|
191 |
if(!$res) return false; |
192 |
|
193 |
if(!($row = mysql_fetch_row($res))) return false; |
194 |
|
195 |
// Congratulations - authorisation suxxessful! |
196 |
|
197 |
$common_sessiondata['user_auth']['authorised'] = true; |
198 |
|
199 |
$common_sessiondata['user_auth']['name'] = $username; |
200 |
$common_sessiondata['user_auth']['password'] = $password; |
201 |
|
202 |
$logintime = time(); |
203 |
|
204 |
$userid = $row[0]; |
205 |
$rights = $row[1]; |
206 |
$logincount = $row[2] + 1; |
207 |
$lastlogin = $row[3]; |
208 |
|
209 |
$common_sessiondata['user_auth']['id'] = $userid; |
210 |
$common_sessiondata['user_auth']['lastlogin'] = $lastlogin; |
211 |
$common_sessiondata['user_auth']['rights'] = $rights; |
212 |
|
213 |
// Break if the user already has authorised in this session: |
214 |
if(isset($common_sessiondata['user_auth']['logintime'])) return false; |
215 |
|
216 |
$common_sessiondata['user_auth']['logincount'] = $logincount; |
217 |
$common_sessiondata['user_auth']['logintime'] = $logintime; |
218 |
|
219 |
// Protocol the login: |
220 |
common_protocollogin(); |
221 |
|
222 |
$sql = "UPDATE users SET logincount='$logincount', lastlogin=FROM_UNIXTIME('$logintime') WHERE id='$userid';"; |
223 |
|
224 |
$res = common_dbc_query($sql); |
225 |
|
226 |
if(!$res) return false; |
227 |
|
228 |
return true; |
229 |
|
230 |
} |
231 |
|
232 |
//------------------------------------------------------------------------------ |
233 |
|
234 |
function common_protocolhit() { |
235 |
|
236 |
global $common, $common_sessiondata; |
237 |
|
238 |
$address = $_SERVER['REMOTE_ADDR'] . ':' . $_SERVER["REMOTE_PORT"]; |
239 |
$entryurl = $_SERVER['REQUEST_URI']; |
240 |
$referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''); |
241 |
$sessionid = session_id(); |
242 |
$unixtime = $common_sessiondata['birthtime']; |
243 |
$useragent = $_SERVER['HTTP_USER_AGENT']; |
244 |
|
245 |
$sql = "INSERT INTO hits (id, timestamp, address, entryurl, referer, sessionid, useragent, requestcount, lastrequest) VALUES (NULL, FROM_UNIXTIME('$unixtime'), '$address', '$entryurl', '$referer', '$sessionid', '$useragent', '1', FROM_UNIXTIME('$unixtime'));"; |
246 |
|
247 |
$res = common_dbc_query($sql); |
248 |
|
249 |
if(!$res) return false; |
250 |
|
251 |
return mysql_insert_id($common['dbc']['h_myqsllink']); |
252 |
|
253 |
} |
254 |
|
255 |
function common_protocollogin() { |
256 |
|
257 |
global $common_sessiondata; |
258 |
|
259 |
$hitid = $common_sessiondata['hit_id']; |
260 |
$sessionid = session_id(); |
261 |
$logintime = $common_sessiondata['user_auth']['logintime']; |
262 |
$userid = $common_sessiondata['user_auth']['id']; |
263 |
|
264 |
$sql = "INSERT INTO logins (id, timestamp, user_id, sessionid, hit_id) VALUES (NULL, FROM_UNIXTIME('$logintime'), '$userid', '$sessionid', '$hitid');"; |
265 |
|
266 |
$res = common_dbc_query($sql); |
267 |
|
268 |
if(!$res) return false; |
269 |
|
270 |
return true; |
271 |
|
272 |
} |
273 |
|
274 |
//---------------------------------------------------------- |
275 |
//- Database functions: |
276 |
|
277 |
function common_dbc_connect() { |
278 |
|
279 |
global $common; |
280 |
|
281 |
$common['dbc']['connected'] = false; |
282 |
|
283 |
$h_myqsllink = mysql_pconnect( |
284 |
$common['hostsetup']['mysql_host'], |
285 |
$common['hostsetup']['mysql_user'], |
286 |
$common['hostsetup']['mysql_pass'] |
287 |
); |
288 |
|
289 |
if(!$h_myqsllink) return false; |
290 |
|
291 |
$common['dbc']['connected'] = true; |
292 |
$common['dbc']['h_myqsllink'] = $h_myqsllink; |
293 |
|
294 |
return true; |
295 |
|
296 |
} |
297 |
|
298 |
function common_dbc_selectdb() { |
299 |
|
300 |
global $common; |
301 |
|
302 |
$common['dbc']['dbselected'] = false; |
303 |
|
304 |
if( |
305 |
!mysql_selectdb($common['hostsetup']['mysql_db']) |
306 |
) return false; |
307 |
|
308 |
$common['dbc']['dbselected'] = true; |
309 |
|
310 |
return true; |
311 |
|
312 |
} |
313 |
|
314 |
function common_dbc_connectdb() { |
315 |
|
316 |
global $common; |
317 |
|
318 |
$common['dbc']['dbconnected'] = (common_dbc_connect() && common_dbc_selectdb()); |
319 |
|
320 |
return $common['dbc']['dbconnected']; |
321 |
|
322 |
} |
323 |
|
324 |
function common_dbc_query($sql) { |
325 |
|
326 |
global $common; |
327 |
|
328 |
if(!$common['dbc']['dbconnected']) return false; |
329 |
|
330 |
$res = mysql_query($sql); |
331 |
|
332 |
if($errormsg = mysql_error()) { |
333 |
|
334 |
// >>> MTC <<< |
335 |
|
336 |
echo $errormsg . '<br /> |
337 |
<br /> |
338 |
'; |
339 |
|
340 |
} |
341 |
|
342 |
if(!$res) return false; |
343 |
|
344 |
return $res; |
345 |
|
346 |
} |
347 |
|
348 |
//---------------------------------------------------------- |
349 |
//- File functions: |
350 |
|
351 |
//---------------------------------------------------------- |
352 |
//- Utility functions: |
353 |
|
354 |
function common_get_baseurl() { |
355 |
|
356 |
global $common; |
357 |
|
358 |
return $common['site']['url']; |
359 |
|
360 |
} |
361 |
|
362 |
//------------------------------------------------------------------------------ |
363 |
|
364 |
common_benchmark_addstep('common: end'); |
365 |
|
366 |
//------------------------------------------------------------------------------ |
367 |
|
368 |
?> |