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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.3

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