--- nfo/site/htdocs/inc/common/common.php.inc 2004/08/24 03:27:47 1.1 +++ nfo/site/htdocs/inc/common/common.php.inc 2004/08/30 02:16:19 1.2 @@ -7,44 +7,320 @@ --- $$id$$ ------------------------------------------------------------------------------*/ +//------------------------------------------------------------------------------ +//- Benchmarking: + +$common['benchmark'] = array(); + +function common_benchmark_addstep($caption) { + +global $common; + + $step = array($caption, microtime()); + + array_push($common['benchmark'], $step); + +} + +// The starting entry in the benchmark steps list: +common_benchmark_addstep('COMMON: start'); + //---------------------------------------------------------- -//- Developer host setup: +//- Developer host setups: -$cfg['hostsetups'] = array( +$hostsetups = array( 'default' => array( 'urlrel' => '/nfo/', + 'devstate' => false, + 'mysql_host' => 'localhost', + 'mysql_user' => 'nfo', + 'mysql_pass' => 'b2-cV5RF', + 'mysql_db' => 'nfo' ), 'psl.no-ip.com' => array( 'urlrel' => '/work/www.netfrag.org/', + 'devstate' => true, + 'mysql_host' => 'localhost', + 'mysql_user' => 'php', + 'mysql_pass' => 'A289tpQ1', + 'mysql_db' => 'nfo' ), ); -if(isset($cfg['hostsetups'][$_SERVER['SERVER_NAME']])) { +// Set the default host setup: +$common['hostsetup'] = $hostsetups['default']; - $cfg['hostsetup'] = $cfg['hostsetups'][$_SERVER['SERVER_NAME']]; +// Set the host setup if a listed host name is given: +if(isset($hostsetups[$_SERVER['SERVER_NAME']])) $common['hostsetup'] = $hostsetups[$_SERVER['SERVER_NAME']]; -} else { +unset($hostsetups); - $cfg['hostsetup'] = $cfg['hostsetups']['default']; +//---------------------------------------------------------- +//- Site variable setups: -} +$common['site']['docroot'] = $_SERVER['DOCUMENT_ROOT'] . $common['hostsetup']['urlrel']; +$common['site']['incroot'] = $common['site']['docroot'] . 'inc/'; -$documentroot = $_SERVER['DOCUMENT_ROOT'] . $cfg['hostsetup']['urlrel']; +//---------------------------------------------------------- +//- Page variable setups: + +$common['page']['filename'] = substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '/') + 1); +$common['page']['url'] = $_SERVER['PHP_SELF']; + +common_benchmark_addstep('COMMON: setup'); //------------------------------------------------------------------------------ -//- Configuration include file: +//- Includes: + +//common_include('cfg/cfg.php.inc'); +//common_benchmark_addstep('COMMON: CFG included'); -include($documentroot . 'inc/cfg/cfg.php.inc'); +common_include('xmlcp/xmlcp.php.inc'); +common_benchmark_addstep('COMMON: XMLCP included'); + +common_include('cms/cms.php.inc'); +common_benchmark_addstep('COMMON: CMS included'); + +//------------------------------------------------------------------------------ +//- MySQL connection: + +$common['dbc']['dbconnected'] = common_dbc_connectdb(); + +common_benchmark_addstep('COMMON: connect database'); + +//------------------------------------------------------------------------------ +//- Session setup: + +// Neither proxies, nor the clients are allowed to cache session data: +session_cache_limiter('nocache'); + +// This is neccessary to make the $_SESSION global available: +session_start(); + +common_benchmark_addstep('COMMON: session init'); + +if(!isset($_SESSION['common_sessiondata'])) { + + // The session variable isn't set, create it: + + common_benchmark_addstep('COMMON: New session: start'); + + $common_sessiondata = array( + + 'birthtime' => time(), + 'firstrequest' => 1, // Mark the very first page request. + + 'additionaldata' => array() + + ); + + common_benchmark_addstep('COMMON: New session: create session'); + + // Protocol the visitors hit and store the columns insertion ID: + $common_sessiondata['hit_id'] = common_protocolhit(); + + common_benchmark_addstep('COMMON: New session: protocol hit'); + + // Store a reference to the session data array: + $_SESSION['common_sessiondata'] = &$common_sessiondata; + + common_benchmark_addstep('COMMON: New session: store session/end'); + +} else { + + // Restore the session data array reference: + $common_sessiondata = &$_SESSION['common_sessiondata']; + + // Reset the first page request flag: + $common_sessiondata['firstrequest'] = null; + + // Update the request count in the "hits" table: + + $sql = "UPDATE hits SET requestcount=requestcount+1 WHERE id='" . $common_sessiondata['hit_id'] . "';"; + + mysql_query($sql); + + common_benchmark_addstep('COMMON: session/hit data updated'); + +} //------------------------------------------------------------------------------ //- Functions: +function common_protocolhit() { + +global $common, $common_sessiondata; + + $address = $_SERVER['REMOTE_ADDR'] . ':' . $_SERVER["REMOTE_PORT"]; + $entryurl = $_SERVER['REQUEST_URI']; + $referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''); + $sessionid = session_id(); + $unixtime = $common_sessiondata['birthtime']; + $timestamp = date('YmdHis', $common_sessiondata['birthtime']); + $useragent = $_SERVER['HTTP_USER_AGENT']; + + $sql = "INSERT INTO hits (id, timestamp, address, entryurl, referer, sessionid, useragent, requestcount) VALUES (DEFAULT, FROM_UNIXTIME('$unixtime'), '$address', '$entryurl', '$referer', '$sessionid', '$useragent', '1');"; + + $res = mysql_query($sql); + + if(!$res) return false; + + return mysql_insert_id($common['dbc']['h_myqsllink']); + +} + +//---------------------------------------------------------- + +//- Database functions: + +function common_dbc_connect() { + +global $common; + + $common['dbc']['connected'] = false; + + $h_myqsllink = mysql_pconnect( + $common['hostsetup']['mysql_host'], + $common['hostsetup']['mysql_user'], + $common['hostsetup']['mysql_pass'] + ); + + if(!$h_myqsllink) return false; + + $common['dbc']['connected'] = true; + $common['dbc']['h_myqsllink'] = $h_myqsllink; + + return true; + +} + +function common_dbc_selectdb() { + +global $common; + + $common['dbc']['dbselected'] = false; + + if( + !mysql_selectdb($common['hostsetup']['mysql_db']) + ) return false; + + $common['dbc']['dbselected'] = true; + + return true; + +} + +function common_dbc_connectdb() { + + return (common_dbc_connect() && common_dbc_selectdb()); + +} + +//---------------------------------------------------------- + +//- File functions: + +function common_include($filename) { + +global $common; + + return include($common['site']['incroot'] . $filename); + +} + +//---------------------------------------------------------- + +//- XMLCP setup: + +xmlcp_registertagcallbacks('b', 'common_cb_xmlcp_start_bold', 'common_cb_xmlcp_end_bold'); +xmlcp_registertagcallbacks('h', 'common_cb_xmlcp_start_headline', 'common_cb_xmlcp_end_headline'); +xmlcp_registertagcallbacks('p', 'common_cb_xmlcp_start_paragraph', 'common_cb_xmlcp_end_paragraph'); +xmlcp_registertagcallbacks('page', 'common_cb_xmlcp_start_page', 'common_cb_xmlcp_end_page'); + +function common_cb_xmlcp_end_bold($h_parser, $tagname) { + +global $xmlcp_cdata; + + $xmlcp_cdata .= ''; + +} + +function common_cb_xmlcp_start_bold($h_parser, $tagname, $attribs) { + +global $xmlcp_cdata; + + $xmlcp_cdata .= ''; + +} + +function common_cb_xmlcp_end_page($h_parser, $tagname) { + +} + +function common_cb_xmlcp_start_page($h_parser, $tagname, $attribs) { + +} + +function common_cb_xmlcp_end_headline($h_parser, $tagname) { + +global $xmlcp_cdata; + + common_headline(trim($xmlcp_cdata)); + + $xmlcp_cdata = ''; + +} + +function common_cb_xmlcp_start_headline($h_parser, $tagname, $attribs) { + +global $xmlcp_cdata; + + $xmlcp_cdata = ''; + +} + +function common_cb_xmlcp_end_paragraph($h_parser, $tagname) { + +global $xmlcp_cdata; + + common_paragraph(trim($xmlcp_cdata)); + + $xmlcp_cdata = ''; + +} + +function common_cb_xmlcp_start_paragraph($h_parser, $tagname, $attribs) { + +global $xmlcp_cdata; + + $xmlcp_cdata = ''; + +} + +//---------------------------------------------------------- + +//- ML functions: + +function common_codeparagraph($contents) { + + echo '
+
+' . $contents . '
+
+
-(foot notes) -
+global $common; + + // Only show the benchmark list when "devstate" is set: + if($common['hostsetup']['devstate']) { + + $contents = ''; + + for($i = 0; $i < count($common['benchmark']); $i++) { + + $mtimesegs = explode(' ', $common['benchmark'][$i][1]); + $contents .= '"' . $common['benchmark'][$i][0] . '": '; + + if($i) { + + $timediff = (float)($mtimesegs[1] - $lastmtimesegs[1]); + $timediff += $mtimesegs[0] - $lastmtimesegs[0]; + + $contents .= '+' . round($timediff * 1000000) / 1000 . ' ms
+Page execution time: ' . round($timediff * 100000) / 100 . ' ms.
+ |
+
+• ' . $title . '
+' . ($additionalcontents ? $additionalcontents . '
+' : '') . '
@@ -106,4 +521,8 @@ //------------------------------------------------------------------------------ -?> \ No newline at end of file +common_benchmark_addstep('COMMON: end'); + +//------------------------------------------------------------------------------ + +?>