/[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.6 - (hide annotations)
Tue Aug 31 02:41:40 2004 UTC (20 years ago) by rabit
Branch: MAIN
Changes since 1.5: +116 -17 lines
+ Function: common_checkauthorisation() - tests if user is valid and updates table(s) and session user data; + Login box; + Language flag icons; + Site vars: site URL and paths URLs; + "devstate" switching link (temp.); U CSS/Design

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.6 --- $Id: common.php.inc,v 1.5 2004/08/30 11:47:08 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     common_benchmark_addstep('COMMON: start');
28    
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     //----------------------------------------------------------
63     //- Site variable setups:
64    
65 rabit 1.6 // Paths:
66 rabit 1.2 $common['site']['docroot'] = $_SERVER['DOCUMENT_ROOT'] . $common['hostsetup']['urlrel'];
67     $common['site']['incroot'] = $common['site']['docroot'] . 'inc/';
68 rabit 1.6 $common['site']['libroot'] = $common['site']['docroot'] . 'libs/';
69    
70     // URLs:
71     $common['site']['url'] = 'http://' . $_SERVER['HTTP_HOST'] . $common['hostsetup']['urlrel'];
72     $common['site']['gfxurl'] = $common['site']['url'] . 'gfx/';
73 rabit 1.2
74     //----------------------------------------------------------
75     //- Page variable setups:
76    
77     $common['page']['filename'] = substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '/') + 1);
78     $common['page']['url'] = $_SERVER['PHP_SELF'];
79    
80     common_benchmark_addstep('COMMON: setup');
81    
82     //------------------------------------------------------------------------------
83     //- Includes:
84    
85     //common_include('cfg/cfg.php.inc');
86     //common_benchmark_addstep('COMMON: CFG included');
87    
88     common_include('xmlcp/xmlcp.php.inc');
89     common_benchmark_addstep('COMMON: XMLCP included');
90    
91     common_include('cms/cms.php.inc');
92     common_benchmark_addstep('COMMON: CMS included');
93    
94     //------------------------------------------------------------------------------
95     //- MySQL connection:
96 rabit 1.1
97 rabit 1.2 $common['dbc']['dbconnected'] = common_dbc_connectdb();
98    
99     common_benchmark_addstep('COMMON: connect database');
100    
101     //------------------------------------------------------------------------------
102     //- Session setup:
103    
104     // Neither proxies, nor the clients are allowed to cache session data:
105     session_cache_limiter('nocache');
106    
107     // This is neccessary to make the $_SESSION global available:
108     session_start();
109    
110     common_benchmark_addstep('COMMON: session init');
111    
112     if(!isset($_SESSION['common_sessiondata'])) {
113    
114     // The session variable isn't set, create it:
115    
116     common_benchmark_addstep('COMMON: New session: start');
117    
118     $common_sessiondata = array(
119    
120     'birthtime' => time(),
121     'firstrequest' => 1, // Mark the very first page request.
122    
123 rabit 1.6 // Empty user data sub array:
124     'userdata' => array(
125     'name' => '',
126     'password' => '',
127     'authorised' => false,
128     ),
129    
130     // Content related additional data:
131 rabit 1.2 'additionaldata' => array()
132    
133     );
134    
135     common_benchmark_addstep('COMMON: New session: create session');
136    
137     // Protocol the visitors hit and store the columns insertion ID:
138     $common_sessiondata['hit_id'] = common_protocolhit();
139    
140     common_benchmark_addstep('COMMON: New session: protocol hit');
141    
142     // Store a reference to the session data array:
143     $_SESSION['common_sessiondata'] = &$common_sessiondata;
144    
145     common_benchmark_addstep('COMMON: New session: store session/end');
146 rabit 1.1
147     } else {
148    
149 rabit 1.2 // Restore the session data array reference:
150     $common_sessiondata = &$_SESSION['common_sessiondata'];
151    
152     // Reset the first page request flag:
153     $common_sessiondata['firstrequest'] = null;
154 rabit 1.1
155 rabit 1.2 // Update the request count in the "hits" table:
156    
157     $sql = "UPDATE hits SET requestcount=requestcount+1 WHERE id='" . $common_sessiondata['hit_id'] . "';";
158 rabit 1.1
159 rabit 1.2 mysql_query($sql);
160 rabit 1.1
161 rabit 1.2 common_benchmark_addstep('COMMON: session/hit data updated');
162 rabit 1.1
163 rabit 1.2 }
164 rabit 1.5
165     //------------------------------------------------------------------------------
166     //- URL parameter flags and variables:
167    
168     if(isset($_GET['devstate'])) $common_sessiondata['userdata']['devstate'] = $_GET['devstate'];
169    
170     if(isset($common_sessiondata['userdata']['devstate'])) $common['hostsetup']['devstate'] = $common_sessiondata['userdata']['devstate'];
171 rabit 1.1
172     //------------------------------------------------------------------------------
173     //- Functions:
174    
175 rabit 1.6 function common_checkauthorisation() {
176    
177     global $common_sessiondata;
178    
179     $username = $common_sessiondata['userdata']['name'];
180     $password = $common_sessiondata['userdata']['password'];
181    
182     if((strlen($username) < 2) || (strlen($password) < 2)) return false;
183    
184     $sql = "SELECT id, rights, logincount, lastlogin FROM users WHERE name='$username' AND password='$password';";
185    
186     $res = mysql_query($sql);
187    
188     if(!$res) return false;
189    
190     if(!($row = mysql_fetch_row($res))) return false;
191    
192     // Congratulations - authorisation suxxessful!
193    
194     $logintime = time();
195    
196     $userid = $row[0];
197     $rights = $row[1];
198     $logincount = $row[2] + 1;
199     $lastlogin = $row[3];
200    
201     $common_sessiondata['userdata']['authorised'] = true;
202    
203     $common_sessiondata['userdata']['id'] = $userid;
204     $common_sessiondata['userdata']['lastlogin'] = $lastlogin;
205     $common_sessiondata['userdata']['rights'] = $rights;
206    
207     // Break if the user already has authorised in this session:
208     if(isset($common_sessiondata['userdata']['logintime'])) return false;
209    
210     $common_sessiondata['userdata']['logincount'] = $logincount;
211     $common_sessiondata['userdata']['logintime'] = $logintime;
212    
213     // print_r($common_sessiondata['userdata']);
214    
215     $sql = "UPDATE users SET logincount='$logincount', lastlogin=FROM_UNIXTIME('$logintime') WHERE id='$userid';";
216    
217     $res = mysql_query($sql);
218    
219     if(!$res) return false;
220    
221     return true;
222    
223     }
224    
225     //------------------------------------------------------------------------------
226    
227 rabit 1.2 function common_protocolhit() {
228    
229     global $common, $common_sessiondata;
230    
231     $address = $_SERVER['REMOTE_ADDR'] . ':' . $_SERVER["REMOTE_PORT"];
232     $entryurl = $_SERVER['REQUEST_URI'];
233     $referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');
234     $sessionid = session_id();
235     $unixtime = $common_sessiondata['birthtime'];
236     $useragent = $_SERVER['HTTP_USER_AGENT'];
237    
238 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');";
239 rabit 1.2
240     $res = mysql_query($sql);
241 rabit 1.6
242 bd 1.4 if ($errstr = mysql_error()) {
243 rabit 1.6
244 bd 1.4 print $errstr . "\n";
245 rabit 1.6
246 bd 1.4 }
247 rabit 1.2
248     if(!$res) return false;
249    
250     return mysql_insert_id($common['dbc']['h_myqsllink']);
251    
252     }
253    
254     //----------------------------------------------------------
255    
256     //- Database functions:
257    
258     function common_dbc_connect() {
259    
260     global $common;
261    
262     $common['dbc']['connected'] = false;
263    
264     $h_myqsllink = mysql_pconnect(
265     $common['hostsetup']['mysql_host'],
266     $common['hostsetup']['mysql_user'],
267     $common['hostsetup']['mysql_pass']
268     );
269    
270     if(!$h_myqsllink) return false;
271    
272     $common['dbc']['connected'] = true;
273     $common['dbc']['h_myqsllink'] = $h_myqsllink;
274    
275     return true;
276    
277     }
278    
279     function common_dbc_selectdb() {
280    
281     global $common;
282    
283     $common['dbc']['dbselected'] = false;
284    
285     if(
286     !mysql_selectdb($common['hostsetup']['mysql_db'])
287     ) return false;
288    
289     $common['dbc']['dbselected'] = true;
290    
291     return true;
292    
293     }
294    
295     function common_dbc_connectdb() {
296    
297     return (common_dbc_connect() && common_dbc_selectdb());
298    
299     }
300    
301     //----------------------------------------------------------
302    
303     //- File functions:
304    
305     function common_include($filename) {
306    
307     global $common;
308    
309     return include($common['site']['incroot'] . $filename);
310    
311     }
312    
313     //----------------------------------------------------------
314    
315     //- XMLCP setup:
316    
317     xmlcp_registertagcallbacks('b', 'common_cb_xmlcp_start_bold', 'common_cb_xmlcp_end_bold');
318     xmlcp_registertagcallbacks('h', 'common_cb_xmlcp_start_headline', 'common_cb_xmlcp_end_headline');
319     xmlcp_registertagcallbacks('p', 'common_cb_xmlcp_start_paragraph', 'common_cb_xmlcp_end_paragraph');
320     xmlcp_registertagcallbacks('page', 'common_cb_xmlcp_start_page', 'common_cb_xmlcp_end_page');
321    
322     function common_cb_xmlcp_end_bold($h_parser, $tagname) {
323    
324     global $xmlcp_cdata;
325    
326     $xmlcp_cdata .= '</b>';
327    
328     }
329    
330     function common_cb_xmlcp_start_bold($h_parser, $tagname, $attribs) {
331    
332     global $xmlcp_cdata;
333    
334     $xmlcp_cdata .= '<b>';
335    
336     }
337    
338     function common_cb_xmlcp_end_page($h_parser, $tagname) {
339    
340     }
341    
342     function common_cb_xmlcp_start_page($h_parser, $tagname, $attribs) {
343    
344     }
345    
346     function common_cb_xmlcp_end_headline($h_parser, $tagname) {
347    
348     global $xmlcp_cdata;
349    
350     common_headline(trim($xmlcp_cdata));
351    
352     $xmlcp_cdata = '';
353    
354     }
355    
356     function common_cb_xmlcp_start_headline($h_parser, $tagname, $attribs) {
357    
358     global $xmlcp_cdata;
359    
360     $xmlcp_cdata = '';
361    
362     }
363    
364     function common_cb_xmlcp_end_paragraph($h_parser, $tagname) {
365    
366     global $xmlcp_cdata;
367    
368     common_paragraph(trim($xmlcp_cdata));
369    
370     $xmlcp_cdata = '';
371    
372     }
373    
374     function common_cb_xmlcp_start_paragraph($h_parser, $tagname, $attribs) {
375    
376     global $xmlcp_cdata;
377    
378     $xmlcp_cdata = '';
379    
380     }
381    
382     //----------------------------------------------------------
383    
384     //- ML functions:
385    
386     function common_codeparagraph($contents) {
387    
388 rabit 1.6 echo '<p align="justify" class="hl11">
389 rabit 1.2 <code>
390     ' . $contents . '
391     </code>
392     </p>
393    
394     ';
395    
396     }
397    
398     //----------------------------------------------------------
399    
400 rabit 1.1 function common_headline($caption) {
401    
402 rabit 1.2 echo '<h4>' . $caption . '</h4>
403 rabit 1.1
404     ';
405    
406     }
407    
408     //----------------------------------------------------------
409    
410 rabit 1.2 function common_page($keyname, $language_id = 0) {
411    
412     global $common;
413    
414     common_benchmark_addstep('COMMON: PAGE: start');
415    
416 rabit 1.3 $list = cms_getlist('xmlpage', $keyname);
417    
418     common_benchmark_addstep('COMMON: PAGE: CMS get list');
419    
420     $contentdata = cms_getcontent('xmlpage', $keyname, $language_id);
421 rabit 1.2
422     common_benchmark_addstep('COMMON: PAGE: CMS get content');
423    
424     $xml = $contentdata['content'];
425    
426     common_pageheader();
427    
428     common_benchmark_addstep('COMMON: PAGE: header');
429    
430     if($xml) {
431    
432 rabit 1.3 $titledata = '';
433    
434     for($i = 0; $i < count($list); $i++) {
435    
436 rabit 1.6 $imgtag = '<img border="0" height="12" src="' . $common['site']['gfxurl'] . 'icons/flags/' . $list[$i][3] . '.gif" width="17" />';
437    
438     $titledata .= ($i ? '
439     ' : '') . (($language_id == $list[$i][3]) ? $imgtag : '<a href="?li=' . $list[$i][3] . '">' . $imgtag . '</a>');
440 rabit 1.3
441     }
442    
443 rabit 1.6 common_pagetitle($contentdata['description'], $titledata . '<br />');
444 rabit 1.2
445     xmlcp_xmlpage2html($xml);
446    
447     common_benchmark_addstep('COMMON: PAGE: xmlpage2html');
448    
449     /*
450 rabit 1.3 common_headline('$list array, readable:');
451     common_codeparagraph('$list = ' . nl2br(htmlentities(print_r($list, true))));
452    
453 rabit 1.2 common_headline('$contentdata array, readable:');
454     common_codeparagraph('$contentdata = ' . nl2br(htmlentities(print_r($contentdata, true))));
455    
456     common_headline('$common array, readable:');
457     common_codeparagraph('$common = ' . nl2br(htmlentities(print_r($common, true))));
458    
459     common_headline('$common_sessiondata array, readable:');
460     common_codeparagraph('$common_sessiondata = ' . nl2br(htmlentities(print_r($common_sessiondata, true))));
461    
462     common_headline('Content XML:');
463     common_codeparagraph(nl2br(htmlentities($xml)));
464    
465     */
466    
467     // Only show the informations when "devstate" is set:
468     if($common['hostsetup']['devstate']) {
469    
470     common_paragraph('<small>
471     <b>Content informations:</b><br />
472     <br />
473     Description: "<b>' . $contentdata['description'] . '</b>" (Type: "xmlpage")<br />
474     Creator: "<b>' . $contentdata['creator_name'] . '</b>"<br />
475     Date, Time: "<b>' . date('d.m.Y, H:i:s', $contentdata['unixtime']) . '</b>"<br />
476     Language: "<b>' . $contentdata['language_name'] . '</b>"<br />
477     XML content size: <b>' . strlen($xml) . '</b> bytes
478     </small>', 'box2');
479    
480     }
481    
482     } else {
483    
484     common_pagetitle('Bad content request');
485    
486     common_paragraph('Sorry, but the requested content is unknown.');
487    
488     }
489    
490     common_benchmark_addstep('COMMON: PAGE: content');
491    
492     common_pagefooter();
493    
494     }
495    
496     //----------------------------------------------------------
497    
498 rabit 1.1 function common_pageheader() {
499    
500 rabit 1.6 global $common, $common_sessiondata;
501 rabit 1.2
502 rabit 1.1 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
503    
504     <html xmlns="http://www.w3.org/1999/xhtml">
505    
506     <head>
507     <title>www.netfrag.org</title>
508     <style type="text/css"><!--
509 rabit 1.6 body, input, table { font: 10pt verdana, serif; }
510 rabit 1.2 h4 { font-size:12pt; }
511 rabit 1.6 input { border:1px; }
512 rabit 1.2 small { font-size:8pt; }
513 rabit 1.6 .bg11, .box1, .f11, .hl11 { background-color:#50f8f0; }
514     .bg12, .f12 { background-color:#40e8e0; }
515     .bg13 { background-color:#20a0c0; }
516     .bg21, .box2, .f21, .hl21 { background-color:#90fff0; }
517     .bg22, .f22 { background-color:#80f0e0; }
518     .bg23 { background-color:#40c080; }
519     .box1, .f11, .f12 { border:2px solid #20a0c0; }
520     .box2, .f21, .f22 { border:1px solid #40c080; }
521     .box1, .box2, .hl11, .hl21 { padding:4px; }
522 rabit 1.1 --></style>
523     </head>
524    
525     <body>
526    
527 rabit 1.6 <form action="" method="post">
528     <table cellspacing="0" cellpadding="0" width="100%">
529     <tr>
530     <th class="box1" width="100%">
531     <font size="5" style="letter-spacing:4pt;"><b>www.netfrag.org</b></font>
532     </td>
533     <td>&nbsp;</td>
534     <td>
535     <table cellspacing="0" cellpadding="2" class="f22">
536     <tr>
537     <th class="box2" rowspan="2"><b>User<br />login</b></th>
538     <td>&nbsp;User:</td><td><input name="un" size="8" type="text" value="' . $common_sessiondata['userdata']['name'] . '" /></td>
539     <td rowspan="2"><input type="submit" value="Go" />&nbsp;</td>
540     <tr>
541     <td>&nbsp;Pass:</td><td><input name="pw" size="8" type="password" /></td>
542     </tr>
543     </table>
544     </td>
545     </tr>
546     </table>
547     </form>
548 rabit 1.1
549     ';
550    
551     }
552    
553     //----------------------------------------------------------
554    
555     function common_pagefooter() {
556    
557 rabit 1.2 global $common;
558    
559     // Only show the benchmark list when "devstate" is set:
560     if($common['hostsetup']['devstate']) {
561    
562     $contents = '';
563    
564     for($i = 0; $i < count($common['benchmark']); $i++) {
565    
566     $mtimesegs = explode(' ', $common['benchmark'][$i][1]);
567     $contents .= '"<b>' . $common['benchmark'][$i][0] . '</b>": ';
568    
569     if($i) {
570    
571     $timediff = (float)($mtimesegs[1] - $lastmtimesegs[1]);
572     $timediff += $mtimesegs[0] - $lastmtimesegs[0];
573    
574     $contents .= '<b>+' . round($timediff * 1000000) / 1000 . '</b> ms<br />
575     ';
576    
577     } else {
578    
579     $contents .= 'Start time: <b>' . date('H:i:s', $mtimesegs[1]) . substr($mtimesegs[0], 1, 4) . '</b><br />
580     ';
581    
582     }
583    
584     $lastmtimesegs = $mtimesegs;
585    
586     }
587    
588     common_paragraph('<small>
589     <b>Partial execution times:</b><br />
590     <br />
591     ' . $contents . '</small>', 'box2');
592    
593     }
594    
595     common_benchmark_addstep('COMMON: page footer');
596    
597     $endmtimesegs = explode(' ', microtime());
598     $startmtimesegs = explode(' ', $common['benchmark'][0][1]);
599     $timediff = (float)($endmtimesegs[1] - $startmtimesegs[1]);
600     $timediff += $endmtimesegs[0] - $startmtimesegs[0];
601    
602 rabit 1.6 $devstate = $common['hostsetup']['devstate'];
603    
604     echo '<table cellspacing="0" cellpadding="0" class="box1" width="100%">
605 rabit 1.2 <tr>
606     <td valign="top">
607 rabit 1.6 Page execution time: <code>' . round($timediff * 100000) / 100 . '</code> ms.<br />
608     [ ' . ($devstate ? '<a href="?devstate=0">devstate off</a>' : '<a href="?devstate=1">devstate on</a>') . ' ]
609 rabit 1.2 </td>
610 rabit 1.6 <td align="right" valign="middle"><a href="http://validator.w3.org/check?uri=referer" target="_blank"><img alt="This page is valid XHTML 1.0" border="0" height="31" src="http://www.w3.org/Icons/valid-xhtml10" width="88" /></a>
611 rabit 1.2 <a href="http://jigsaw.w3.org/css-validator/check/referer" target="_blank"><img style="border:0;width:88px;height:31px" src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" /></a></td>
612     </tr>
613     </table>
614 rabit 1.1
615     </body>
616    
617     </html>
618     ';
619    
620     }
621    
622     //----------------------------------------------------------
623    
624 rabit 1.2 function common_pagetitle($title, $additionalcontents = '') {
625    
626     echo '<p class="box2">
627 rabit 1.6 <big><b>&bull; ' . $title . '</b>&nbsp;</big>
628 rabit 1.2 ' . ($additionalcontents ? $additionalcontents . '
629     ' : '') . '</p>
630    
631     ';
632    
633     }
634    
635     //----------------------------------------------------------
636    
637 rabit 1.1 function common_paragraph($contents, $class = '') {
638    
639     echo '<p align="justify"' . ($class ? ' class="' . $class . '"' : '') . '>
640     ' . $contents . '
641     </p>
642    
643     ';
644    
645     }
646    
647     //------------------------------------------------------------------------------
648    
649 rabit 1.2 common_benchmark_addstep('COMMON: end');
650    
651     //------------------------------------------------------------------------------
652    
653     ?>

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