/[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.5 - (hide annotations)
Mon Aug 30 11:47:08 2004 UTC (20 years ago) by rabit
Branch: MAIN
Changes since 1.4: +8 -1 lines
+ Development state flag ("...?devstate=[0|1]") and it's storage in the session data.

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

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