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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Mon Aug 30 02:16:19 2004 UTC (20 years ago) by rabit
Branch: MAIN
Changes since 1.1: +439 -20 lines
+ MySQL support and functions; + Session mechanism; + XMLCP include, config and HTML callback functions; + Hit logging; + Benchmarking (common_benchmark_*, Summary list at the page footer); + xmlpage content to HTML render function (common_page()); + Request count logging; + Site and page variable setups; + ML functions: common_pagetitle(), common_codeparagraph(); U Page footer: + W3C markup and CSS validation icons, execution time; U Improved developer host switching; U Page header: CSS.

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

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