📄 stats.lib.inc.php
字号:
<?php // $Id: stats.lib.inc.php 10082 2006-11-21 19:08:15Z pcool $ /*============================================================================== Dokeos - elearning and course management software Copyright (c) 2004 Dokeos S.A. Copyright (c) 2003 Ghent University (UGent) Copyright (c) 2001 Universite catholique de Louvain (UCL) For a full list of contributors, see "credits.txt". The full license can be read in "license.txt". This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See the GNU General Public License for more details. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com============================================================================== *//**============================================================================== * This is the statistics library for Dokeos.* Include/require it in your code to use its functionality.** @author Sebastien Piraux* @package dokeos.library* * @todo use the Database libraries============================================================================== *//* List of functions : ------------------- addBrowser -- OK addCountry -- OK addOs -- OK addProvider -- OK addReferer -- OK cleanProcessedRecords -- OK decodeOpenInfos -- OK extractAgent -- OK MUST BE IMPROVED extractCountry -- OK MUST BE IMPROVED extractProvider -- OK MUST BE IMPROVED fillCountriesTable -- OK fillBrowsersTable -- OK fillOsTable -- OK fillProvidersTable -- OK fillReferersTable -- OK loadCountries -- OK loadOs -- OK loadBrowsers -- OK ---------- OK BUT MAY BE OPTIMIZED*//*============================================================================== Variables============================================================================== */ // regroup table names for maintenance purpose$TABLETRACK_OPEN = $_configuration['statistics_database']."`.`track_e_open";$TABLESTATS_PROVIDERS = $_configuration['statistics_database']."`.`track_c_providers";$TABLESTATS_COUNTRIES = $_configuration['statistics_database']."`.`track_c_countries";$TABLESTATS_BROWSERS = $_configuration['statistics_database']."`.`track_c_browsers";$TABLESTATS_OS = $_configuration['statistics_database']."`.`track_c_os";$TABLESTATS_REFERERS = $_configuration['statistics_database']."`.`track_c_referers";/*============================================================================== Main : decodeOpenInfos launch all processes============================================================================== */ /** * @author Sebastien Piraux <piraux_seb@hotmail.com> * @desc uses `$TABLETRACK_OPEN` to split recorded information, to count occurences (for os, provider,...) and to increment the number of occurrences of each different element into the corresponding tables */ function decodeOpenInfos(){ global $TABLETRACK_OPEN; // record initial value of ignore_user_abort $ignore = ignore_user_abort(); // prevent script from being stopped while executing, the following can be considered // as a transaction ignore_user_abort(1) ; // we take the last event id to prevent miss of some recorded event // only processed record have to be cleaned $sql = "SELECT open_id FROM `$TABLETRACK_OPEN` WHERE open_date <= NOW() ORDER BY open_id DESC LIMIT 1"; //$processBegin = getOneResult($sql); $query = @mysql_query($sql); if (mysql_errno()) { echo "\n<!-- **** ".mysql_errno().": ".mysql_error()." In : $sql **** -->\n"; } $res = @mysql_fetch_array($query); $processBegin = $res[0]; // process //--Providers And Countries-------------------------------------------// $sql = "SELECT open_remote_host FROM `$TABLETRACK_OPEN` WHERE open_remote_host != '' AND open_id <= '".$processBegin."' "; $query = mysql_query( $sql ); if( mysql_num_rows($query) != 0 ) { // load list of countries $list_countries = loadCountries(); while ($row = mysql_fetch_row ($query) ) { $remote_host = $row[0]; /*****Provider*****/ //extract provider $provider = extractProvider( $remote_host ); // add or increment provider in the providers array $providers_array = addProvider( $provider,$providers_array ); /*****Countries*****/ // extract country $country = extractCountry( $remote_host, $list_countries ); // increment country in the countries table $countries_array = addCountry( $country, $countries_array ); } // update tables fillProvidersTable( $providers_array ); fillCountriesTable( $countries_array ); } // provider and countries done //--Browsers and OS---------------------------------------------------// $sql = "SELECT open_agent FROM `$TABLETRACK_OPEN` WHERE open_remote_host != '' AND open_id <= '".$processBegin."' "; $query = mysql_query( $sql ); if( mysql_num_rows($query) != 0 ) { // load lists // of browsers $list_browsers = loadBrowsers(); // of OS $list_os = loadOs(); while ( $row = mysql_fetch_row ($query) ) { $agent = $row[0]; /*****Browser and OS*****/ // extract browser and OS list( $browser,$os ) = split( "[|]",extractAgent( $agent , $list_browsers , $list_os ) ); // increment browser and OS in the corresponding arrays $browsers_array = addBrowser( $browser , $browsers_array ); $os_array = addOs( $os , $os_array ); } fillBrowsersTable( $browsers_array ); fillOsTable( $os_array ); } // browsers and OS done //--Referers----------------------------------------------------------// $sql = "SELECT open_referer FROM `$TABLETRACK_OPEN` WHERE open_referer != '' AND open_id <= '".$processBegin."' "; $query = mysql_query( $sql ); if( mysql_num_rows($query) != 0 ) { $i=0; while ($row = mysql_fetch_row ($query) ) { $ref = $row[0]; $referers_array = addReferer( $ref , $referers_array ); } fillReferersTable( $referers_array ); } // referers done //-------------------------------------------------------------------// // end of process // cleaning of $TABLETRACK_OPEN table cleanProcessedRecords($processBegin); // reset to the initial value ignore_user_abort($ignore);}/*************************************************************************** * * Utils * ***************************************************************************//** * @author Sebastien Piraux <piraux_seb@hotmail.com> * @param limit : all records BEFORE $limit will be affected * @desc this function will delete the remote_host, user_agent and referer rows from the track_open table recorded before the date $limit. OPTIMIZE is called to get back the memory espaces deleted*/function cleanProcessedRecords( $limit ){ global $TABLETRACK_OPEN; $sql = "UPDATE `".$TABLETRACK_OPEN."` SET open_remote_host = '', open_agent = '', open_referer ='' WHERE open_id <= '".$limit."'"; $query = mysql_query( $sql ); mysql_query("OPTIMIZE TABLE $TABLETRACK_OPEN"); }/*************************************************************************** * * Provider * ***************************************************************************//** * @author Sebastien Piraux <piraux_seb@hotmail.com> * @param remhost : must be @getHostByAddr($_SERVER['REMOTE_ADDR'] * @desc this function will extract the provider name from a given remote host and record this occurence in the corresponding table*/function extractProvider($remhost){ if($remhost == "Unknown") return $remhost; $explodedRemhost = explode(".", $remhost); $provider = $explodedRemhost[sizeof( $explodedRemhost )-2] ."." .$explodedRemhost[sizeof( $explodedRemhost )-1]; if($provider == "co.uk" || $provider == "co.jp") return $explodedRemhost[sizeof( $explodedRemhost )-3].$provider; else return $provider; }/** * @author Sebastien Piraux <piraux_seb@hotmail.com> * @param provider : name of the provider * @param providers_array : list of providers and their counter * @desc this function will : - if the provider is already in the array it will increment the corresponding value - if the provider doesn't exist it will be added and set to 1*/function addProvider($provider,$providers_array){ if( isset( $providers_array[$provider] ) ) { // add one unity to this provider occurrences $providers_array[$provider] = $providers_array[$provider] + 1; } else { // first occurrence of this provider $providers_array[$provider] = 1; } return $providers_array;}/** * @author Sebastien Piraux <piraux_seb@hotmail.com> * @param providers_array : list of providers and their counter * @desc update the providers'table with new values*/function fillProvidersTable($providers_array){ global $TABLESTATS_PROVIDERS; if(is_array($providers_array)) { foreach ( $providers_array as $prov=>$number ) { $sql = "SELECT counter FROM `".$TABLESTATS_PROVIDERS."` WHERE `provider` = '".$prov."'"; $res = mysql_query($sql); // if this provider already exists in the DB if( $row = mysql_num_rows($res) ) { // update $sql2 = "UPDATE `".$TABLESTATS_PROVIDERS."` SET `counter` = counter + '$number' WHERE `provider` = '".$prov."'"; } else { // insert $sql2 = "INSERT INTO `".$TABLESTATS_PROVIDERS."` (`provider`,`counter`) VALUES ('".$prov."','".$number."')"; } mysql_query($sql2); } } }/*************************************************************************** * * Country * ***************************************************************************//** * @author Sebastien Piraux <piraux_seb@hotmail.com> * @return a 2D array filled with code and name of countries * @desc This function is used to build an array containing countries informations*/function loadCountries(){ global $TABLESTATS_COUNTRIES; $sql = "SELECT code, country FROM `".$TABLESTATS_COUNTRIES."`"; $res = mysql_query( $sql ); $i = 0 ; while( $row = mysql_fetch_array( $res ) ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -