--- 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/09/04 03:06:10 1.10 @@ -4,106 +4,343 @@ --- Setup and common functions include file. -------------------------------------------------------------------------------- --- rabit, 04:31 24.08.2004 ---- $$id$$ +--- $Id: common.php.inc,v 1.10 2004/09/04 03:06:10 rabit Exp $ ------------------------------------------------------------------------------*/ +//------------------------------------------------------------------------------ +//- Benchmarking: + +// Create the benchmark steps array inside "$common": +$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']; +common_benchmark_addstep('common: host setup'); -} +//---------------------------------------------------------- +//- Site variable setups: -$documentroot = $_SERVER['DOCUMENT_ROOT'] . $cfg['hostsetup']['urlrel']; +// Paths: +$common['site']['docroot'] = $_SERVER['DOCUMENT_ROOT'] . $common['hostsetup']['urlrel']; +$common['site']['incroot'] = $common['site']['docroot'] . 'inc/'; +$common['site']['libroot'] = $common['site']['docroot'] . 'libs/'; + +// URLs: +$common['site']['url'] = 'http://' . $_SERVER['HTTP_HOST'] . $common['hostsetup']['urlrel']; +$common['site']['gfxurl'] = $common['site']['url'] . 'gfx/'; + +//---------------------------------------------------------- +//- 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: site/page setup'); //------------------------------------------------------------------------------ -//- Configuration include file: +//- Includes: + +//common_include('cfg/cfg.php.inc'); +//common_benchmark_addstep('common: CFG included'); + +include($common['site']['incroot'] . 'xmlcp/xmlcp.php.inc'); +common_benchmark_addstep('common: XMLCP included'); + +include($common['site']['incroot'] . 'cms/cms.php.inc'); +common_benchmark_addstep('common: CMS included'); + +//------------------------------------------------------------------------------ +//- MySQL connection: + +common_dbc_connectdb(); +common_benchmark_addstep('common: connect database'); + +//------------------------------------------------------------------------------ +//- Session setup: + +if(!isset($common['client']['session_enabled'])) { + + // Neither proxies, nor the clients are allowed to cache session data: + session_cache_limiter('nocache'); -include($documentroot . 'inc/cfg/cfg.php.inc'); + // 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. + + // User data and authorisation: + 'user_auth' => array( + 'name' => '', + 'password' => '', + 'authorised' => false + ), + + // User preferences and preferred settings: + 'user_prefs' => array( + 'benchlist' => null, + 'debug' => null, + 'language_id' => null, + 'outputtype' => null + ), + + // Content related additional data: + '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'] . "';"; + + common_dbc_query($sql); + + common_benchmark_addstep('common: session/hit data updated'); + +} //------------------------------------------------------------------------------ //- Functions: -function common_headline($caption) { +function common_authorise($username, $password) { - echo '
' . $caption . '
+global $common_sessiondata; -'; + if((strlen($username) < 2) || (strlen($password) < 2)) return false; + + $sql = "SELECT id, rights, logincount, lastlogin FROM users WHERE name='$username' AND password='$password';"; + + $res = common_dbc_query($sql); + + if(!$res) return false; + + if(!($row = mysql_fetch_row($res))) return false; + + // Congratulations - authorisation suxxessful! + + $common_sessiondata['user_auth']['authorised'] = true; + + $common_sessiondata['user_auth']['name'] = $username; + $common_sessiondata['user_auth']['password'] = $password; + + $logintime = time(); + + $userid = $row[0]; + $rights = $row[1]; + $logincount = $row[2] + 1; + $lastlogin = $row[3]; + + $common_sessiondata['user_auth']['id'] = $userid; + $common_sessiondata['user_auth']['lastlogin'] = $lastlogin; + $common_sessiondata['user_auth']['rights'] = $rights; + + // Break if the user already has authorised in this session: + if(isset($common_sessiondata['user_auth']['logintime'])) return false; + + $common_sessiondata['user_auth']['logincount'] = $logincount; + $common_sessiondata['user_auth']['logintime'] = $logintime; + + $sql = "UPDATE users SET logincount='$logincount', lastlogin=FROM_UNIXTIME('$logintime') WHERE id='$userid';"; + + $res = common_dbc_query($sql); + + if(!$res) return false; + + return true; + +} + +//------------------------------------------------------------------------------ + +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']; + $useragent = $_SERVER['HTTP_USER_AGENT']; + + $sql = "INSERT INTO hits (id, timestamp, address, entryurl, referer, sessionid, useragent, requestcount) VALUES (NULL, FROM_UNIXTIME('$unixtime'), '$address', '$entryurl', '$referer', '$sessionid', '$useragent', '1');"; + + $res = common_dbc_query($sql); + + if(!$res) return false; + + return mysql_insert_id($common['dbc']['h_myqsllink']); } //---------------------------------------------------------- +//- Database functions: -function common_pageheader() { +function common_dbc_connect() { - echo ' +global $common; - + $common['dbc']['connected'] = false; - -www.netfrag.org - - + $h_myqsllink = mysql_pconnect( + $common['hostsetup']['mysql_host'], + $common['hostsetup']['mysql_user'], + $common['hostsetup']['mysql_pass'] + ); - + if(!$h_myqsllink) return false; -

www.netfrag.org

+ $common['dbc']['connected'] = true; + $common['dbc']['h_myqsllink'] = $h_myqsllink; -

• Home

+ 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() { + +global $common; -function common_pagefooter() { + $common['dbc']['dbconnected'] = (common_dbc_connect() && common_dbc_selectdb()); + + return $common['dbc']['dbconnected']; + +} - echo '

-(foot notes) -

+function common_dbc_query($sql) { - +global $common; - + if(!$common['dbc']['dbconnected']) return false; + + $res = mysql_query($sql); + + if($errormsg = mysql_error()) { + + // >>> MTC <<< + + echo $errormsg . '
+
'; + } + + if(!$res) return false; + + return $res; + } //---------------------------------------------------------- +//- File functions: -function common_paragraph($contents, $class = '') { +//---------------------------------------------------------- +//- Utility functions: - echo '

-' . $contents . ' -

+function common_get_baseurl() { -'; +global $common; + + return $common['site']['url']; } //------------------------------------------------------------------------------ -?> \ No newline at end of file +common_benchmark_addstep('common: end'); + +//------------------------------------------------------------------------------ + +?>