/[cvs]/nfo/site/htdocs/inc/common/common.php.inc
ViewVC logotype

Annotation of /nfo/site/htdocs/inc/common/common.php.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (hide annotations)
Sat Sep 4 03:06:10 2004 UTC (20 years ago) by rabit
Branch: MAIN
CVS Tags: alpha-20040904-1
Changes since 1.9: +24 -25 lines
U Function: common_checkauthorisation() renamed to common_authorise() and modified; U User session data structure; - URL and post variable processing (moved to index.php).

1 rabit 1.1 <?php
2     /*------------------------------------------------------------------------------
3     --- www.netfrag.org
4     --- Setup and common functions include file.
5     --------------------------------------------------------------------------------
6     --- rabit, 04:31 24.08.2004
7 rabit 1.10 --- $Id: common.php.inc,v 1.9 2004/09/04 01:07:29 rabit Exp $
8 rabit 1.1 ------------------------------------------------------------------------------*/
9    
10 rabit 1.2 //------------------------------------------------------------------------------
11     //- Benchmarking:
12    
13 rabit 1.3 // Create the benchmark steps array inside "$common":
14 rabit 1.2 $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 rabit 1.7 common_benchmark_addstep('common: start');
28 rabit 1.2
29 rabit 1.1 //----------------------------------------------------------
30 rabit 1.2 //- Developer host setups:
31 rabit 1.1
32 rabit 1.2 $hostsetups = array(
33 rabit 1.1
34     'default' => array(
35     'urlrel' => '/nfo/',
36 rabit 1.2 'devstate' => false,
37     'mysql_host' => 'localhost',
38     'mysql_user' => 'nfo',
39     'mysql_pass' => 'b2-cV5RF',
40     'mysql_db' => 'nfo'
41 rabit 1.1 ),
42    
43     'psl.no-ip.com' => array(
44     'urlrel' => '/work/www.netfrag.org/',
45 rabit 1.2 'devstate' => true,
46     'mysql_host' => 'localhost',
47     'mysql_user' => 'php',
48     'mysql_pass' => 'A289tpQ1',
49     'mysql_db' => 'nfo'
50 rabit 1.1 ),
51    
52     );
53    
54 rabit 1.2 // 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 rabit 1.7 common_benchmark_addstep('common: host setup');
63    
64 rabit 1.2 //----------------------------------------------------------
65     //- Site variable setups:
66    
67 rabit 1.6 // Paths:
68 rabit 1.2 $common['site']['docroot'] = $_SERVER['DOCUMENT_ROOT'] . $common['hostsetup']['urlrel'];
69     $common['site']['incroot'] = $common['site']['docroot'] . 'inc/';
70 rabit 1.6 $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 rabit 1.2
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 rabit 1.7 common_benchmark_addstep('common: site/page setup');
83 rabit 1.2
84     //------------------------------------------------------------------------------
85     //- Includes:
86    
87     //common_include('cfg/cfg.php.inc');
88 rabit 1.7 //common_benchmark_addstep('common: CFG included');
89 rabit 1.2
90 rabit 1.7 include($common['site']['incroot'] . 'xmlcp/xmlcp.php.inc');
91     common_benchmark_addstep('common: XMLCP included');
92 rabit 1.2
93 rabit 1.7 include($common['site']['incroot'] . 'cms/cms.php.inc');
94     common_benchmark_addstep('common: CMS included');
95 rabit 1.2
96     //------------------------------------------------------------------------------
97     //- MySQL connection:
98 rabit 1.1
99 rabit 1.7 common_dbc_connectdb();
100     common_benchmark_addstep('common: connect database');
101 rabit 1.2
102     //------------------------------------------------------------------------------
103     //- Session setup:
104    
105 rabit 1.9 if(!isset($common['client']['session_enabled'])) {
106 rabit 1.2
107 joko 1.8 // 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 rabit 1.2
115 rabit 1.7 common_benchmark_addstep('common: session init');
116 rabit 1.2
117     if(!isset($_SESSION['common_sessiondata'])) {
118    
119     // The session variable isn't set, create it:
120    
121 rabit 1.7 common_benchmark_addstep('common: New session: start');
122 rabit 1.2
123     $common_sessiondata = array(
124    
125     'birthtime' => time(),
126     'firstrequest' => 1, // Mark the very first page request.
127    
128 rabit 1.10 // User data and authorisation:
129     'user_auth' => array(
130 rabit 1.6 'name' => '',
131     'password' => '',
132 rabit 1.10 '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 rabit 1.6 ),
142    
143     // Content related additional data:
144 rabit 1.2 'additionaldata' => array()
145    
146     );
147    
148 rabit 1.7 common_benchmark_addstep('common: New session: create session');
149 rabit 1.2
150     // Protocol the visitors hit and store the columns insertion ID:
151     $common_sessiondata['hit_id'] = common_protocolhit();
152    
153 rabit 1.7 common_benchmark_addstep('common: New session: protocol hit');
154 rabit 1.2
155     // Store a reference to the session data array:
156     $_SESSION['common_sessiondata'] = &$common_sessiondata;
157    
158 rabit 1.7 common_benchmark_addstep('common: New session: store session/end');
159 rabit 1.1
160     } else {
161    
162 rabit 1.2 // 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 rabit 1.1
168 rabit 1.2 // Update the request count in the "hits" table:
169    
170     $sql = "UPDATE hits SET requestcount=requestcount+1 WHERE id='" . $common_sessiondata['hit_id'] . "';";
171 rabit 1.1
172 rabit 1.7 common_dbc_query($sql);
173 rabit 1.1
174 rabit 1.7 common_benchmark_addstep('common: session/hit data updated');
175 rabit 1.1
176 rabit 1.2 }
177 rabit 1.5
178     //------------------------------------------------------------------------------
179 rabit 1.1 //- Functions:
180    
181 rabit 1.10 function common_authorise($username, $password) {
182 rabit 1.6
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 rabit 1.7 $res = common_dbc_query($sql);
190 rabit 1.6
191     if(!$res) return false;
192    
193     if(!($row = mysql_fetch_row($res))) return false;
194    
195     // Congratulations - authorisation suxxessful!
196    
197 rabit 1.10 $common_sessiondata['user_auth']['authorised'] = true;
198    
199     $common_sessiondata['user_auth']['name'] = $username;
200     $common_sessiondata['user_auth']['password'] = $password;
201    
202 rabit 1.6 $logintime = time();
203    
204     $userid = $row[0];
205     $rights = $row[1];
206     $logincount = $row[2] + 1;
207     $lastlogin = $row[3];
208    
209 rabit 1.10 $common_sessiondata['user_auth']['id'] = $userid;
210     $common_sessiondata['user_auth']['lastlogin'] = $lastlogin;
211     $common_sessiondata['user_auth']['rights'] = $rights;
212 rabit 1.6
213     // Break if the user already has authorised in this session:
214 rabit 1.10 if(isset($common_sessiondata['user_auth']['logintime'])) return false;
215 rabit 1.6
216 rabit 1.10 $common_sessiondata['user_auth']['logincount'] = $logincount;
217     $common_sessiondata['user_auth']['logintime'] = $logintime;
218 rabit 1.6
219     $sql = "UPDATE users SET logincount='$logincount', lastlogin=FROM_UNIXTIME('$logintime') WHERE id='$userid';";
220    
221 rabit 1.7 $res = common_dbc_query($sql);
222 rabit 1.6
223     if(!$res) return false;
224    
225     return true;
226    
227     }
228    
229     //------------------------------------------------------------------------------
230    
231 rabit 1.2 function common_protocolhit() {
232    
233     global $common, $common_sessiondata;
234    
235     $address = $_SERVER['REMOTE_ADDR'] . ':' . $_SERVER["REMOTE_PORT"];
236     $entryurl = $_SERVER['REQUEST_URI'];
237     $referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');
238     $sessionid = session_id();
239     $unixtime = $common_sessiondata['birthtime'];
240     $useragent = $_SERVER['HTTP_USER_AGENT'];
241    
242 bd 1.4 $sql = "INSERT INTO hits (id, timestamp, address, entryurl, referer, sessionid, useragent, requestcount) VALUES (NULL, FROM_UNIXTIME('$unixtime'), '$address', '$entryurl', '$referer', '$sessionid', '$useragent', '1');";
243 rabit 1.2
244 rabit 1.7 $res = common_dbc_query($sql);
245 rabit 1.2
246     if(!$res) return false;
247    
248     return mysql_insert_id($common['dbc']['h_myqsllink']);
249    
250     }
251    
252     //----------------------------------------------------------
253     //- Database functions:
254    
255     function common_dbc_connect() {
256    
257     global $common;
258    
259     $common['dbc']['connected'] = false;
260    
261     $h_myqsllink = mysql_pconnect(
262     $common['hostsetup']['mysql_host'],
263     $common['hostsetup']['mysql_user'],
264     $common['hostsetup']['mysql_pass']
265     );
266    
267     if(!$h_myqsllink) return false;
268    
269     $common['dbc']['connected'] = true;
270     $common['dbc']['h_myqsllink'] = $h_myqsllink;
271    
272     return true;
273    
274     }
275    
276     function common_dbc_selectdb() {
277    
278     global $common;
279    
280     $common['dbc']['dbselected'] = false;
281    
282     if(
283     !mysql_selectdb($common['hostsetup']['mysql_db'])
284     ) return false;
285    
286     $common['dbc']['dbselected'] = true;
287    
288     return true;
289    
290     }
291    
292     function common_dbc_connectdb() {
293    
294     global $common;
295    
296 rabit 1.7 $common['dbc']['dbconnected'] = (common_dbc_connect() && common_dbc_selectdb());
297 rabit 1.2
298 rabit 1.7 return $common['dbc']['dbconnected'];
299 rabit 1.2
300     }
301    
302 rabit 1.7 function common_dbc_query($sql) {
303 rabit 1.2
304     global $common;
305    
306 rabit 1.7 if(!$common['dbc']['dbconnected']) return false;
307 rabit 1.2
308 rabit 1.7 $res = mysql_query($sql);
309 rabit 1.3
310 rabit 1.7 if($errormsg = mysql_error()) {
311 rabit 1.3
312 rabit 1.7 // >>> MTC <<<
313 rabit 1.2
314 rabit 1.7 echo $errormsg . '<br />
315 rabit 1.2 <br />
316 rabit 1.1 ';
317    
318 rabit 1.2 }
319    
320 rabit 1.7 if(!$res) return false;
321 rabit 1.1
322 rabit 1.7 return $res;
323 rabit 1.1
324     }
325    
326     //----------------------------------------------------------
327 rabit 1.7 //- File functions:
328 rabit 1.1
329 joko 1.8 //----------------------------------------------------------
330     //- Utility functions:
331 rabit 1.9
332 joko 1.8 function common_get_baseurl() {
333 rabit 1.9
334     global $common;
335    
336     return $common['site']['url'];
337    
338 joko 1.8 }
339 rabit 1.1
340     //------------------------------------------------------------------------------
341    
342 rabit 1.7 common_benchmark_addstep('common: end');
343 rabit 1.2
344     //------------------------------------------------------------------------------
345    
346     ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed