/[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.4 by bd, Mon Aug 30 10:42: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 (NULL, FROM_UNIXTIME('$unixtime'), '$address', '$entryurl', '$referer', '$sessionid', '$useragent', '1');";
167    
168      $res = mysql_query($sql);
169      if ($errstr = mysql_error()) {
170        print $errstr . "\n";
171      }
172    
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  function common_headline($caption) {  function common_headline($caption) {
326    
327    echo '<h5>' . $caption . '</h5>    echo '<h4>' . $caption . '</h4>
328    
329  ';  ';
330    
# Line 52  function common_headline($caption) { Line 332  function common_headline($caption) {
332    
333  //----------------------------------------------------------  //----------------------------------------------------------
334    
335    function common_page($keyname, $language_id = 0) {
336    
337    global $common;
338    
339      common_benchmark_addstep('COMMON: PAGE: start');
340    
341      $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    
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        $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    
367        xmlcp_xmlpage2html($xml);
368    
369        common_benchmark_addstep('COMMON: PAGE: xmlpage2html');
370    
371    /*
372        common_headline('$list array, readable:');
373        common_codeparagraph('$list = ' . nl2br(htmlentities(print_r($list, true))));
374    
375        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  function common_pageheader() {  function common_pageheader() {
421    
422    global $common;
423    
424    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">
425    
426  <html xmlns="http://www.w3.org/1999/xhtml">  <html xmlns="http://www.w3.org/1999/xhtml">
# Line 61  function common_pageheader() { Line 428  function common_pageheader() {
428  <head>  <head>
429  <title>www.netfrag.org</title>  <title>www.netfrag.org</title>
430  <style type="text/css"><!--  <style type="text/css"><!--
431  body { font: 10pt tahoma, serif; }  body { font: 11pt verdana, serif; }
432  h2, .box, .hl { background-color:#e0e0e0; padding:8px; }  h2 { text-align:center; }
433  h2, .box { border:1px solid #a0a0a0; }  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  --></style>  --></style>
440  </head>  </head>
441    
# Line 71  h2, .box { border:1px solid #a0a0a0; } Line 443  h2, .box { border:1px solid #a0a0a0; }
443    
444  <h2>www.netfrag.org</h2>  <h2>www.netfrag.org</h2>
445    
 <h3>&bull; Home</h3>  
   
446  ';  ';
447    
448  }  }
# Line 81  h2, .box { border:1px solid #a0a0a0; } Line 451  h2, .box { border:1px solid #a0a0a0; }
451    
452  function common_pagefooter() {  function common_pagefooter() {
453    
454    echo '<p align="center" class="box">  global $common;
455  (foot notes)  
456  </p>    // 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    
509  </body>  </body>
510    
# Line 94  function common_pagefooter() { Line 515  function common_pagefooter() {
515    
516  //----------------------------------------------------------  //----------------------------------------------------------
517    
518    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  function common_paragraph($contents, $class = '') {  function common_paragraph($contents, $class = '') {
532    
533    echo '<p align="justify"' . ($class ? ' class="' . $class . '"' : '') . '>    echo '<p align="justify"' . ($class ? ' class="' . $class . '"' : '') . '>
# Line 106  function common_paragraph($contents, $cl Line 540  function common_paragraph($contents, $cl
540    
541  //------------------------------------------------------------------------------  //------------------------------------------------------------------------------
542    
 ?>  
543    common_benchmark_addstep('COMMON: end');
544    
545    //------------------------------------------------------------------------------
546    
547    ?>

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

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