/[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.4 - (hide annotations)
Mon Aug 30 10:42:20 2004 UTC (20 years ago) by bd
Branch: MAIN
Changes since 1.3: +5 -2 lines
U fix for mysql 3.x: does not understand "DEFAULT"

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 bd 1.4 --- $Id: common.php.inc,v 1.3 2004/08/30 04:19:20 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     $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.1
151     //------------------------------------------------------------------------------
152     //- Functions:
153    
154 rabit 1.2 function common_protocolhit() {
155    
156     global $common, $common_sessiondata;
157    
158     $address = $_SERVER['REMOTE_ADDR'] . ':' . $_SERVER["REMOTE_PORT"];
159     $entryurl = $_SERVER['REQUEST_URI'];
160     $referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');
161     $sessionid = session_id();
162     $unixtime = $common_sessiondata['birthtime'];
163     $timestamp = date('YmdHis', $common_sessiondata['birthtime']);
164     $useragent = $_SERVER['HTTP_USER_AGENT'];
165    
166 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');";
167 rabit 1.2
168     $res = mysql_query($sql);
169 bd 1.4 if ($errstr = mysql_error()) {
170     print $errstr . "\n";
171     }
172 rabit 1.2
173     if(!$res) return false;
174    
175     return mysql_insert_id($common['dbc']['h_myqsllink']);
176    
177     }
178    
179     //----------------------------------------------------------
180    
181     //- Database functions:
182    
183     function common_dbc_connect() {
184    
185     global $common;
186    
187     $common['dbc']['connected'] = false;
188    
189     $h_myqsllink = mysql_pconnect(
190     $common['hostsetup']['mysql_host'],
191     $common['hostsetup']['mysql_user'],
192     $common['hostsetup']['mysql_pass']
193     );
194    
195     if(!$h_myqsllink) return false;
196    
197     $common['dbc']['connected'] = true;
198     $common['dbc']['h_myqsllink'] = $h_myqsllink;
199    
200     return true;
201    
202     }
203    
204     function common_dbc_selectdb() {
205    
206     global $common;
207    
208     $common['dbc']['dbselected'] = false;
209    
210     if(
211     !mysql_selectdb($common['hostsetup']['mysql_db'])
212     ) return false;
213    
214     $common['dbc']['dbselected'] = true;
215    
216     return true;
217    
218     }
219    
220     function common_dbc_connectdb() {
221    
222     return (common_dbc_connect() && common_dbc_selectdb());
223    
224     }
225    
226     //----------------------------------------------------------
227    
228     //- File functions:
229    
230     function common_include($filename) {
231    
232     global $common;
233    
234     return include($common['site']['incroot'] . $filename);
235    
236     }
237    
238     //----------------------------------------------------------
239    
240     //- XMLCP setup:
241    
242     xmlcp_registertagcallbacks('b', 'common_cb_xmlcp_start_bold', 'common_cb_xmlcp_end_bold');
243     xmlcp_registertagcallbacks('h', 'common_cb_xmlcp_start_headline', 'common_cb_xmlcp_end_headline');
244     xmlcp_registertagcallbacks('p', 'common_cb_xmlcp_start_paragraph', 'common_cb_xmlcp_end_paragraph');
245     xmlcp_registertagcallbacks('page', 'common_cb_xmlcp_start_page', 'common_cb_xmlcp_end_page');
246    
247     function common_cb_xmlcp_end_bold($h_parser, $tagname) {
248    
249     global $xmlcp_cdata;
250    
251     $xmlcp_cdata .= '</b>';
252    
253     }
254    
255     function common_cb_xmlcp_start_bold($h_parser, $tagname, $attribs) {
256    
257     global $xmlcp_cdata;
258    
259     $xmlcp_cdata .= '<b>';
260    
261     }
262    
263     function common_cb_xmlcp_end_page($h_parser, $tagname) {
264    
265     }
266    
267     function common_cb_xmlcp_start_page($h_parser, $tagname, $attribs) {
268    
269     }
270    
271     function common_cb_xmlcp_end_headline($h_parser, $tagname) {
272    
273     global $xmlcp_cdata;
274    
275     common_headline(trim($xmlcp_cdata));
276    
277     $xmlcp_cdata = '';
278    
279     }
280    
281     function common_cb_xmlcp_start_headline($h_parser, $tagname, $attribs) {
282    
283     global $xmlcp_cdata;
284    
285     $xmlcp_cdata = '';
286    
287     }
288    
289     function common_cb_xmlcp_end_paragraph($h_parser, $tagname) {
290    
291     global $xmlcp_cdata;
292    
293     common_paragraph(trim($xmlcp_cdata));
294    
295     $xmlcp_cdata = '';
296    
297     }
298    
299     function common_cb_xmlcp_start_paragraph($h_parser, $tagname, $attribs) {
300    
301     global $xmlcp_cdata;
302    
303     $xmlcp_cdata = '';
304    
305     }
306    
307     //----------------------------------------------------------
308    
309     //- ML functions:
310    
311     function common_codeparagraph($contents) {
312    
313     echo '<p align="justify" class="hl">
314     <code>
315     ' . $contents . '
316     </code>
317     </p>
318    
319     ';
320    
321     }
322    
323     //----------------------------------------------------------
324    
325 rabit 1.1 function common_headline($caption) {
326    
327 rabit 1.2 echo '<h4>' . $caption . '</h4>
328 rabit 1.1
329     ';
330    
331     }
332    
333     //----------------------------------------------------------
334    
335 rabit 1.2 function common_page($keyname, $language_id = 0) {
336    
337     global $common;
338    
339     common_benchmark_addstep('COMMON: PAGE: start');
340    
341 rabit 1.3 $list = cms_getlist('xmlpage', $keyname);
342    
343     common_benchmark_addstep('COMMON: PAGE: CMS get list');
344    
345     $contentdata = cms_getcontent('xmlpage', $keyname, $language_id);
346 rabit 1.2
347     common_benchmark_addstep('COMMON: PAGE: CMS get content');
348    
349     $xml = $contentdata['content'];
350    
351     common_pageheader();
352    
353     common_benchmark_addstep('COMMON: PAGE: header');
354    
355     if($xml) {
356    
357 rabit 1.3 $titledata = '';
358    
359     for($i = 0; $i < count($list); $i++) {
360    
361     $titledata .= ($i ? ' · ' : '') . '<a href="?li=' . $list[$i][3] . '">' . $list[$i][4] . '</a>';
362    
363     }
364    
365     common_pagetitle($contentdata['description'], $titledata);
366 rabit 1.2
367     xmlcp_xmlpage2html($xml);
368    
369     common_benchmark_addstep('COMMON: PAGE: xmlpage2html');
370    
371     /*
372 rabit 1.3 common_headline('$list array, readable:');
373     common_codeparagraph('$list = ' . nl2br(htmlentities(print_r($list, true))));
374    
375 rabit 1.2 common_headline('$contentdata array, readable:');
376     common_codeparagraph('$contentdata = ' . nl2br(htmlentities(print_r($contentdata, true))));
377    
378     common_headline('$common array, readable:');
379     common_codeparagraph('$common = ' . nl2br(htmlentities(print_r($common, true))));
380    
381     common_headline('$common_sessiondata array, readable:');
382     common_codeparagraph('$common_sessiondata = ' . nl2br(htmlentities(print_r($common_sessiondata, true))));
383    
384     common_headline('Content XML:');
385     common_codeparagraph(nl2br(htmlentities($xml)));
386    
387     */
388    
389     // Only show the informations when "devstate" is set:
390     if($common['hostsetup']['devstate']) {
391    
392     common_paragraph('<small>
393     <b>Content informations:</b><br />
394     <br />
395     Description: "<b>' . $contentdata['description'] . '</b>" (Type: "xmlpage")<br />
396     Creator: "<b>' . $contentdata['creator_name'] . '</b>"<br />
397     Date, Time: "<b>' . date('d.m.Y, H:i:s', $contentdata['unixtime']) . '</b>"<br />
398     Language: "<b>' . $contentdata['language_name'] . '</b>"<br />
399     XML content size: <b>' . strlen($xml) . '</b> bytes
400     </small>', 'box2');
401    
402     }
403    
404     } else {
405    
406     common_pagetitle('Bad content request');
407    
408     common_paragraph('Sorry, but the requested content is unknown.');
409    
410     }
411    
412     common_benchmark_addstep('COMMON: PAGE: content');
413    
414     common_pagefooter();
415    
416     }
417    
418     //----------------------------------------------------------
419    
420 rabit 1.1 function common_pageheader() {
421    
422 rabit 1.2 global $common;
423    
424 rabit 1.1 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
425    
426     <html xmlns="http://www.w3.org/1999/xhtml">
427    
428     <head>
429     <title>www.netfrag.org</title>
430     <style type="text/css"><!--
431 rabit 1.2 body { font: 11pt verdana, serif; }
432     h2 { text-align:center; }
433     h2, .box, .box2, .hl { padding:8px; }
434     h2, .box, .hl { background-color:#40f0f0; }
435     h2, .box { border:2px solid #20a0c0; }
436     h4 { font-size:12pt; }
437     small { font-size:8pt; }
438     .box2 { background-color:#80fff0; border:1px solid #40c080; }
439 rabit 1.1 --></style>
440     </head>
441    
442     <body>
443    
444     <h2>www.netfrag.org</h2>
445    
446     ';
447    
448     }
449    
450     //----------------------------------------------------------
451    
452     function common_pagefooter() {
453    
454 rabit 1.2 global $common;
455    
456     // Only show the benchmark list when "devstate" is set:
457     if($common['hostsetup']['devstate']) {
458    
459     $contents = '';
460    
461     for($i = 0; $i < count($common['benchmark']); $i++) {
462    
463     $mtimesegs = explode(' ', $common['benchmark'][$i][1]);
464     $contents .= '"<b>' . $common['benchmark'][$i][0] . '</b>": ';
465    
466     if($i) {
467    
468     $timediff = (float)($mtimesegs[1] - $lastmtimesegs[1]);
469     $timediff += $mtimesegs[0] - $lastmtimesegs[0];
470    
471     $contents .= '<b>+' . round($timediff * 1000000) / 1000 . '</b> ms<br />
472     ';
473    
474     } else {
475    
476     $contents .= 'Start time: <b>' . date('H:i:s', $mtimesegs[1]) . substr($mtimesegs[0], 1, 4) . '</b><br />
477     ';
478    
479     }
480    
481     $lastmtimesegs = $mtimesegs;
482    
483     }
484    
485     common_paragraph('<small>
486     <b>Partial execution times:</b><br />
487     <br />
488     ' . $contents . '</small>', 'box2');
489    
490     }
491    
492     common_benchmark_addstep('COMMON: page footer');
493    
494     $endmtimesegs = explode(' ', microtime());
495     $startmtimesegs = explode(' ', $common['benchmark'][0][1]);
496     $timediff = (float)($endmtimesegs[1] - $startmtimesegs[1]);
497     $timediff += $endmtimesegs[0] - $startmtimesegs[0];
498    
499     echo '<table cellspacing="0" cellpadding="0" class="box" width="100%">
500     <tr>
501     <td valign="top">
502     Page execution time: <code>' . round($timediff * 100000) / 100 . '</code> ms.
503     </td>
504     <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>
505     <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>
506     </tr>
507     </table>
508 rabit 1.1
509     </body>
510    
511     </html>
512     ';
513    
514     }
515    
516     //----------------------------------------------------------
517    
518 rabit 1.2 function common_pagetitle($title, $additionalcontents = '') {
519    
520     echo '<p class="box2">
521     <big><b>&bull; ' . $title . '</b></big><br />
522     ' . ($additionalcontents ? $additionalcontents . '
523     ' : '') . '</p>
524    
525     ';
526    
527     }
528    
529     //----------------------------------------------------------
530    
531 rabit 1.1 function common_paragraph($contents, $class = '') {
532    
533     echo '<p align="justify"' . ($class ? ' class="' . $class . '"' : '') . '>
534     ' . $contents . '
535     </p>
536    
537     ';
538    
539     }
540    
541     //------------------------------------------------------------------------------
542    
543 rabit 1.2 common_benchmark_addstep('COMMON: end');
544    
545     //------------------------------------------------------------------------------
546    
547     ?>

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