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.6 2004/08/31 02:41:40 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 |
// Neither proxies, nor the clients are allowed to cache session data: |
106 |
session_cache_limiter('nocache'); |
107 |
|
108 |
// This is neccessary to make the $_SESSION global available: |
109 |
session_start(); |
110 |
|
111 |
common_benchmark_addstep('common: session init'); |
112 |
|
113 |
if(!isset($_SESSION['common_sessiondata'])) { |
114 |
|
115 |
// The session variable isn't set, create it: |
116 |
|
117 |
common_benchmark_addstep('common: New session: start'); |
118 |
|
119 |
$common_sessiondata = array( |
120 |
|
121 |
'birthtime' => time(), |
122 |
'firstrequest' => 1, // Mark the very first page request. |
123 |
|
124 |
// Empty user data sub array: |
125 |
'userdata' => array( |
126 |
'name' => '', |
127 |
'password' => '', |
128 |
'authorised' => false, |
129 |
), |
130 |
|
131 |
// Content related additional data: |
132 |
'additionaldata' => array() |
133 |
|
134 |
); |
135 |
|
136 |
common_benchmark_addstep('common: New session: create session'); |
137 |
|
138 |
// Protocol the visitors hit and store the columns insertion ID: |
139 |
$common_sessiondata['hit_id'] = common_protocolhit(); |
140 |
|
141 |
common_benchmark_addstep('common: New session: protocol hit'); |
142 |
|
143 |
// Store a reference to the session data array: |
144 |
$_SESSION['common_sessiondata'] = &$common_sessiondata; |
145 |
|
146 |
common_benchmark_addstep('common: New session: store session/end'); |
147 |
|
148 |
} else { |
149 |
|
150 |
// Restore the session data array reference: |
151 |
$common_sessiondata = &$_SESSION['common_sessiondata']; |
152 |
|
153 |
// Reset the first page request flag: |
154 |
$common_sessiondata['firstrequest'] = null; |
155 |
|
156 |
// Update the request count in the "hits" table: |
157 |
|
158 |
$sql = "UPDATE hits SET requestcount=requestcount+1 WHERE id='" . $common_sessiondata['hit_id'] . "';"; |
159 |
|
160 |
common_dbc_query($sql); |
161 |
|
162 |
common_benchmark_addstep('common: session/hit data updated'); |
163 |
|
164 |
} |
165 |
|
166 |
//------------------------------------------------------------------------------ |
167 |
//- URL parameter flags and variables: |
168 |
|
169 |
if(isset($_GET['devstate'])) $common_sessiondata['userdata']['devstate'] = $_GET['devstate']; |
170 |
|
171 |
if(isset($common_sessiondata['userdata']['devstate'])) $common['hostsetup']['devstate'] = $common_sessiondata['userdata']['devstate']; |
172 |
|
173 |
//------------------------------------------------------------------------------ |
174 |
//- Functions: |
175 |
|
176 |
function common_checkauthorisation() { |
177 |
|
178 |
global $common_sessiondata; |
179 |
|
180 |
$username = $common_sessiondata['userdata']['name']; |
181 |
$password = $common_sessiondata['userdata']['password']; |
182 |
|
183 |
if((strlen($username) < 2) || (strlen($password) < 2)) return false; |
184 |
|
185 |
$sql = "SELECT id, rights, logincount, lastlogin FROM users WHERE name='$username' AND password='$password';"; |
186 |
|
187 |
$res = common_dbc_query($sql); |
188 |
|
189 |
if(!$res) return false; |
190 |
|
191 |
if(!($row = mysql_fetch_row($res))) return false; |
192 |
|
193 |
// Congratulations - authorisation suxxessful! |
194 |
|
195 |
$logintime = time(); |
196 |
|
197 |
$userid = $row[0]; |
198 |
$rights = $row[1]; |
199 |
$logincount = $row[2] + 1; |
200 |
$lastlogin = $row[3]; |
201 |
|
202 |
$common_sessiondata['userdata']['authorised'] = true; |
203 |
|
204 |
$common_sessiondata['userdata']['id'] = $userid; |
205 |
$common_sessiondata['userdata']['lastlogin'] = $lastlogin; |
206 |
$common_sessiondata['userdata']['rights'] = $rights; |
207 |
|
208 |
// Break if the user already has authorised in this session: |
209 |
if(isset($common_sessiondata['userdata']['logintime'])) return false; |
210 |
|
211 |
$common_sessiondata['userdata']['logincount'] = $logincount; |
212 |
$common_sessiondata['userdata']['logintime'] = $logintime; |
213 |
|
214 |
// print_r($common_sessiondata['userdata']); |
215 |
|
216 |
$sql = "UPDATE users SET logincount='$logincount', lastlogin=FROM_UNIXTIME('$logintime') WHERE id='$userid';"; |
217 |
|
218 |
$res = common_dbc_query($sql); |
219 |
|
220 |
if(!$res) return false; |
221 |
|
222 |
return true; |
223 |
|
224 |
} |
225 |
|
226 |
//------------------------------------------------------------------------------ |
227 |
|
228 |
function common_protocolhit() { |
229 |
|
230 |
global $common, $common_sessiondata; |
231 |
|
232 |
$address = $_SERVER['REMOTE_ADDR'] . ':' . $_SERVER["REMOTE_PORT"]; |
233 |
$entryurl = $_SERVER['REQUEST_URI']; |
234 |
$referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''); |
235 |
$sessionid = session_id(); |
236 |
$unixtime = $common_sessiondata['birthtime']; |
237 |
$useragent = $_SERVER['HTTP_USER_AGENT']; |
238 |
|
239 |
$sql = "INSERT INTO hits (id, timestamp, address, entryurl, referer, sessionid, useragent, requestcount) VALUES (NULL, FROM_UNIXTIME('$unixtime'), '$address', '$entryurl', '$referer', '$sessionid', '$useragent', '1');"; |
240 |
|
241 |
$res = common_dbc_query($sql); |
242 |
|
243 |
if(!$res) return false; |
244 |
|
245 |
return mysql_insert_id($common['dbc']['h_myqsllink']); |
246 |
|
247 |
} |
248 |
|
249 |
//---------------------------------------------------------- |
250 |
//- Database functions: |
251 |
|
252 |
function common_dbc_connect() { |
253 |
|
254 |
global $common; |
255 |
|
256 |
$common['dbc']['connected'] = false; |
257 |
|
258 |
$h_myqsllink = mysql_pconnect( |
259 |
$common['hostsetup']['mysql_host'], |
260 |
$common['hostsetup']['mysql_user'], |
261 |
$common['hostsetup']['mysql_pass'] |
262 |
); |
263 |
|
264 |
if(!$h_myqsllink) return false; |
265 |
|
266 |
$common['dbc']['connected'] = true; |
267 |
$common['dbc']['h_myqsllink'] = $h_myqsllink; |
268 |
|
269 |
return true; |
270 |
|
271 |
} |
272 |
|
273 |
function common_dbc_selectdb() { |
274 |
|
275 |
global $common; |
276 |
|
277 |
$common['dbc']['dbselected'] = false; |
278 |
|
279 |
if( |
280 |
!mysql_selectdb($common['hostsetup']['mysql_db']) |
281 |
) return false; |
282 |
|
283 |
$common['dbc']['dbselected'] = true; |
284 |
|
285 |
return true; |
286 |
|
287 |
} |
288 |
|
289 |
function common_dbc_connectdb() { |
290 |
|
291 |
global $common; |
292 |
|
293 |
$common['dbc']['dbconnected'] = (common_dbc_connect() && common_dbc_selectdb()); |
294 |
|
295 |
return $common['dbc']['dbconnected']; |
296 |
|
297 |
} |
298 |
|
299 |
function common_dbc_query($sql) { |
300 |
|
301 |
global $common; |
302 |
|
303 |
if(!$common['dbc']['dbconnected']) return false; |
304 |
|
305 |
$res = mysql_query($sql); |
306 |
|
307 |
if($errormsg = mysql_error()) { |
308 |
|
309 |
// >>> MTC <<< |
310 |
|
311 |
echo $errormsg . '<br /> |
312 |
<br /> |
313 |
'; |
314 |
|
315 |
} |
316 |
|
317 |
if(!$res) return false; |
318 |
|
319 |
return $res; |
320 |
|
321 |
} |
322 |
|
323 |
//---------------------------------------------------------- |
324 |
//- File functions: |
325 |
|
326 |
|
327 |
|
328 |
//------------------------------------------------------------------------------ |
329 |
|
330 |
common_benchmark_addstep('common: end'); |
331 |
|
332 |
//------------------------------------------------------------------------------ |
333 |
|
334 |
?> |