stats.lib.inc.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. <?php // $Id: stats.lib.inc.php 10082 2006-11-21 19:08:15Z pcool $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. For a full list of contributors, see "credits.txt".
  9. The full license can be read in "license.txt".
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation; either version 2
  13. of the License, or (at your option) any later version.
  14. See the GNU General Public License for more details.
  15. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  16. ==============================================================================
  17. */
  18. /**
  19. ==============================================================================
  20. * This is the statistics library for Dokeos.
  21. * Include/require it in your code to use its functionality.
  22. *
  23. * @author Sebastien Piraux
  24. * @package dokeos.library
  25. *
  26. * @todo use the Database libraries
  27. ==============================================================================
  28. */
  29. /*
  30. List of functions :
  31. -------------------
  32. addBrowser -- OK
  33. addCountry -- OK
  34. addOs -- OK
  35. addProvider -- OK
  36. addReferer -- OK
  37. cleanProcessedRecords -- OK
  38. decodeOpenInfos -- OK
  39. extractAgent -- OK MUST BE IMPROVED
  40. extractCountry -- OK MUST BE IMPROVED
  41. extractProvider -- OK MUST BE IMPROVED
  42. fillCountriesTable -- OK
  43. fillBrowsersTable -- OK
  44. fillOsTable -- OK
  45. fillProvidersTable -- OK
  46. fillReferersTable -- OK
  47. loadCountries -- OK
  48. loadOs -- OK
  49. loadBrowsers -- OK
  50. ----------
  51. OK BUT MAY BE OPTIMIZED
  52. */
  53. /*
  54. ==============================================================================
  55. Variables
  56. ==============================================================================
  57. */
  58. // regroup table names for maintenance purpose
  59. $TABLETRACK_OPEN = $_configuration['statistics_database']."`.`track_e_open";
  60. $TABLESTATS_PROVIDERS = $_configuration['statistics_database']."`.`track_c_providers";
  61. $TABLESTATS_COUNTRIES = $_configuration['statistics_database']."`.`track_c_countries";
  62. $TABLESTATS_BROWSERS = $_configuration['statistics_database']."`.`track_c_browsers";
  63. $TABLESTATS_OS = $_configuration['statistics_database']."`.`track_c_os";
  64. $TABLESTATS_REFERERS = $_configuration['statistics_database']."`.`track_c_referers";
  65. /*
  66. ==============================================================================
  67. Main : decodeOpenInfos launch all processes
  68. ==============================================================================
  69. */
  70. /**
  71. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  72. * @desc uses `$TABLETRACK_OPEN` to split recorded
  73. information, to count occurences (for os, provider,...)
  74. and to increment the number of occurrences of each
  75. different element into the corresponding tables
  76. */
  77. function decodeOpenInfos()
  78. {
  79. global $TABLETRACK_OPEN;
  80. // record initial value of ignore_user_abort
  81. $ignore = ignore_user_abort();
  82. // prevent script from being stopped while executing, the following can be considered
  83. // as a transaction
  84. ignore_user_abort(1) ;
  85. // we take the last event id to prevent miss of some recorded event
  86. // only processed record have to be cleaned
  87. $sql = "SELECT open_id
  88. FROM `$TABLETRACK_OPEN`
  89. WHERE open_date <= NOW()
  90. ORDER BY open_id DESC
  91. LIMIT 1";
  92. //$processBegin = getOneResult($sql);
  93. $query = @mysql_query($sql);
  94. if (mysql_errno())
  95. {
  96. echo "\n<!-- **** ".mysql_errno().": ".mysql_error()." In : $sql **** -->\n";
  97. }
  98. $res = @mysql_fetch_array($query);
  99. $processBegin = $res[0];
  100. // process
  101. //--Providers And Countries-------------------------------------------//
  102. $sql = "SELECT open_remote_host
  103. FROM `$TABLETRACK_OPEN`
  104. WHERE open_remote_host != ''
  105. AND open_id <= '".$processBegin."' ";
  106. $query = mysql_query( $sql );
  107. if( mysql_num_rows($query) != 0 )
  108. {
  109. // load list of countries
  110. $list_countries = loadCountries();
  111. while ($row = mysql_fetch_row ($query) )
  112. {
  113. $remote_host = $row[0];
  114. /*****Provider*****/
  115. //extract provider
  116. $provider = extractProvider( $remote_host );
  117. // add or increment provider in the providers array
  118. $providers_array = addProvider( $provider,$providers_array );
  119. /*****Countries*****/
  120. // extract country
  121. $country = extractCountry( $remote_host, $list_countries );
  122. // increment country in the countries table
  123. $countries_array = addCountry( $country, $countries_array );
  124. }
  125. // update tables
  126. fillProvidersTable( $providers_array );
  127. fillCountriesTable( $countries_array );
  128. }
  129. // provider and countries done
  130. //--Browsers and OS---------------------------------------------------//
  131. $sql = "SELECT open_agent
  132. FROM `$TABLETRACK_OPEN`
  133. WHERE open_remote_host != ''
  134. AND open_id <= '".$processBegin."' ";
  135. $query = mysql_query( $sql );
  136. if( mysql_num_rows($query) != 0 )
  137. {
  138. // load lists
  139. // of browsers
  140. $list_browsers = loadBrowsers();
  141. // of OS
  142. $list_os = loadOs();
  143. while ( $row = mysql_fetch_row ($query) )
  144. {
  145. $agent = $row[0];
  146. /*****Browser and OS*****/
  147. // extract browser and OS
  148. list( $browser,$os ) = split( "[|]",extractAgent( $agent , $list_browsers , $list_os ) );
  149. // increment browser and OS in the corresponding arrays
  150. $browsers_array = addBrowser( $browser , $browsers_array );
  151. $os_array = addOs( $os , $os_array );
  152. }
  153. fillBrowsersTable( $browsers_array );
  154. fillOsTable( $os_array );
  155. }
  156. // browsers and OS done
  157. //--Referers----------------------------------------------------------//
  158. $sql = "SELECT open_referer
  159. FROM `$TABLETRACK_OPEN`
  160. WHERE open_referer != ''
  161. AND open_id <= '".$processBegin."' ";
  162. $query = mysql_query( $sql );
  163. if( mysql_num_rows($query) != 0 )
  164. {
  165. $i=0;
  166. while ($row = mysql_fetch_row ($query) )
  167. {
  168. $ref = $row[0];
  169. $referers_array = addReferer( $ref , $referers_array );
  170. }
  171. fillReferersTable( $referers_array );
  172. }
  173. // referers done
  174. //-------------------------------------------------------------------//
  175. // end of process
  176. // cleaning of $TABLETRACK_OPEN table
  177. cleanProcessedRecords($processBegin);
  178. // reset to the initial value
  179. ignore_user_abort($ignore);
  180. }
  181. /***************************************************************************
  182. *
  183. * Utils
  184. *
  185. ***************************************************************************/
  186. /**
  187. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  188. * @param limit : all records BEFORE $limit will be affected
  189. * @desc this function will delete the remote_host, user_agent
  190. and referer rows from the track_open table recorded before
  191. the date $limit. OPTIMIZE is called to get back the memory
  192. espaces deleted
  193. */
  194. function cleanProcessedRecords( $limit )
  195. {
  196. global $TABLETRACK_OPEN;
  197. $sql = "UPDATE `".$TABLETRACK_OPEN."`
  198. SET open_remote_host = '',
  199. open_agent = '',
  200. open_referer =''
  201. WHERE open_id <= '".$limit."'";
  202. $query = mysql_query( $sql );
  203. mysql_query("OPTIMIZE TABLE $TABLETRACK_OPEN");
  204. }
  205. /***************************************************************************
  206. *
  207. * Provider
  208. *
  209. ***************************************************************************/
  210. /**
  211. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  212. * @param remhost : must be @getHostByAddr($_SERVER['REMOTE_ADDR']
  213. * @desc this function will extract the provider name from a given
  214. remote host and record this occurence in the corresponding
  215. table
  216. */
  217. function extractProvider($remhost)
  218. {
  219. if($remhost == "Unknown")
  220. return $remhost;
  221. $explodedRemhost = explode(".", $remhost);
  222. $provider = $explodedRemhost[sizeof( $explodedRemhost )-2]
  223. ."."
  224. .$explodedRemhost[sizeof( $explodedRemhost )-1];
  225. if($provider == "co.uk" || $provider == "co.jp")
  226. return $explodedRemhost[sizeof( $explodedRemhost )-3].$provider;
  227. else return $provider;
  228. }
  229. /**
  230. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  231. * @param provider : name of the provider
  232. * @param providers_array : list of providers and their counter
  233. * @desc this function will :
  234. - if the provider is already in the array it will increment
  235. the corresponding value
  236. - if the provider doesn't exist it will be added and set to 1
  237. */
  238. function addProvider($provider,$providers_array)
  239. {
  240. if( isset( $providers_array[$provider] ) )
  241. {
  242. // add one unity to this provider occurrences
  243. $providers_array[$provider] = $providers_array[$provider] + 1;
  244. }
  245. else
  246. {
  247. // first occurrence of this provider
  248. $providers_array[$provider] = 1;
  249. }
  250. return $providers_array;
  251. }
  252. /**
  253. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  254. * @param providers_array : list of providers and their counter
  255. * @desc update the providers'table with new values
  256. */
  257. function fillProvidersTable($providers_array)
  258. {
  259. global $TABLESTATS_PROVIDERS;
  260. if(is_array($providers_array))
  261. {
  262. foreach ( $providers_array as $prov=>$number )
  263. {
  264. $sql = "SELECT counter
  265. FROM `".$TABLESTATS_PROVIDERS."`
  266. WHERE `provider` = '".$prov."'";
  267. $res = mysql_query($sql);
  268. // if this provider already exists in the DB
  269. if( $row = mysql_num_rows($res) )
  270. {
  271. // update
  272. $sql2 = "UPDATE `".$TABLESTATS_PROVIDERS."`
  273. SET `counter` = counter + '$number'
  274. WHERE `provider` = '".$prov."'";
  275. }
  276. else
  277. {
  278. // insert
  279. $sql2 = "INSERT INTO `".$TABLESTATS_PROVIDERS."`
  280. (`provider`,`counter`)
  281. VALUES ('".$prov."','".$number."')";
  282. }
  283. mysql_query($sql2);
  284. }
  285. }
  286. }
  287. /***************************************************************************
  288. *
  289. * Country
  290. *
  291. ***************************************************************************/
  292. /**
  293. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  294. * @return a 2D array filled with code and name of countries
  295. * @desc This function is used to build an array containing
  296. countries informations
  297. */
  298. function loadCountries()
  299. {
  300. global $TABLESTATS_COUNTRIES;
  301. $sql = "SELECT code, country
  302. FROM `".$TABLESTATS_COUNTRIES."`";
  303. $res = mysql_query( $sql );
  304. $i = 0 ;
  305. while( $row = mysql_fetch_array( $res ) ) {
  306. $list_countries[$i][0] = $row["code"];
  307. $list_countries[$i][1] = $row["country"];
  308. $i++;
  309. }
  310. return $list_countries;
  311. mysql_free_result($res);
  312. }
  313. /**
  314. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  315. * @param remhost : must be @getHostByAddr($_SERVER['REMOTE_ADDR']
  316. * @param list_countries : list of countries -__-
  317. * @return Name of the country or "Unknown" if not found
  318. * @desc this function will extract the country from a given remote
  319. host and increment the good value in the corresponding table
  320. */
  321. function extractCountry($remhost,$list_countries)
  322. {
  323. if($remhost == "Unknown")
  324. return $remhost;
  325. // country code is the last value of remote host
  326. $explodedRemhost = explode(".",$remhost);
  327. $countryCode = $explodedRemhost[sizeof( $explodedRemhost )-1];
  328. for($i = 0 ; $i < sizeof( $list_countries );$i++)
  329. {
  330. if($list_countries[$i][0] == $countryCode)
  331. return $list_countries[$i][1];
  332. }
  333. }
  334. /**
  335. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  336. * @param country : name of the country or 'Unknown'
  337. * @param countries_array : list of countries and their
  338. number of occurence
  339. * @desc this function will increment number of occurrence
  340. for $country in the countries' tables
  341. */
  342. function addCountry($country,$countries_array)
  343. {
  344. if( isset( $countries_array[$country] ) )
  345. {
  346. // add one unity to this provider occurrences
  347. $countries_array[$country] = $countries_array[$country] + 1;
  348. }
  349. else
  350. {
  351. // first occurrence of this provider
  352. $countries_array[$country] = 1;
  353. }
  354. return $countries_array;
  355. }
  356. /**
  357. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  358. * @param countries_array : list of countries and their counter
  359. * @desc update the countries'table with new values
  360. */
  361. function fillCountriesTable($countries_array)
  362. {
  363. global $TABLESTATS_COUNTRIES;
  364. if(is_array($countries_array) )
  365. {
  366. foreach ( $countries_array as $country=>$number )
  367. {
  368. // update
  369. $sql = "UPDATE `".$TABLESTATS_COUNTRIES."`
  370. SET `counter` = counter + '$number'
  371. WHERE `country` = '".$country."'";
  372. mysql_query($sql);
  373. }
  374. }
  375. }
  376. /***************************************************************************
  377. *
  378. * Agent : Browser and OS
  379. *
  380. ***************************************************************************/
  381. /**
  382. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  383. * @return a 2D array filled with code and name of browsers
  384. * @desc This function is used to build an array containing
  385. browser informations
  386. */
  387. function loadBrowsers()
  388. {
  389. $buffer = split ("#","Gecko|Gecko#Mozilla/3|Mozilla 3.x#Mozilla/4.0|Mozilla 4.0x#Mozilla/4.5|Mozilla 4.5x#Mozilla/4.6|Mozilla 4.6x#Mozilla/4.7|Mozilla 4.7x#Mozilla/5.0|Mozilla 5.0x#MSIE 1.2|MSIE 1.2#MSIE 3.01|MSIE 3.x#MSIE 3.02|MSIE 3.x#MSIE 4.0|MSIE 4.x#MSIE 4.01|MSIE 4.x#MSIE 4.5|MSIE 4.5#MSIE 5.0b1|MSIE 5.0x#MSIE 5.0b2|MSIE 5.0x#MSIE 5.0|MSIE 5.0x#MSIE 5.01|MSIE 5.0x#MSIE 5.1|MSIE 5.1#MSIE 5.1b1|MSIE 5.1#MSIE 5.5|MSIE 5.5#MSIE 5.5b1|MSIE 5.5#MSIE 5.5b2|MSIE 5.5#MSIE 6.0|MSIE 6#MSIE 6.0b|MSIE 6#MSIE 6.5a|MSIE 6.5#Lynx/2.8.0|Lynx 2#Lynx/2.8.1|Lynx 2#Lynx/2.8.2|Lynx 2#Lynx/2.8.3|Lynx 2#Lynx/2.8.4|Lynx 2#Lynx/2.8.5|Lynx 2#HTTrack 3.0x|HTTrack#OmniWeb/4.0.1|OmniWeb#Opera 3.60|Opera 3.60#Opera 4.0|Opera 4#Opera 4.01|Opera 4#Opera 4.02|Opera 4#Opera 5|Opera 5#Opera/3.60|Opera 3.60#Opera/4|Opera 4#Opera/5|Opera 5#Opera/6|Opera 6#Opera 6|Opera 6#Netscape6|NS 6#Netscape/6|NS 6#Netscape7|NS 7#Netscape/7|NS 7#Konqueror/2.0|Konqueror 2#Konqueror/2.0.1|Konqueror 2#Konqueror/2.1|Konqueror 2#Konqueror/2.1.1|Konqueror 2#Konqueror/2.1.2|Konqueror 2#Konqueror/2.2|Konqueror 2#Teleport Pro|Teleport Pro#WebStripper|WebStripper#WebZIP|WebZIP#Netcraft Web|NetCraft#Googlebot|Googlebot#WebCrawler|WebCrawler#InternetSeer|InternetSeer#ia_archiver|ia archiver");
  390. //$list_browser[x][0] is the name of browser as in $_SERVER['HTTP_USER_AGENT']
  391. //$list_browser[x][1] is the name of browser that will be used in display and tables
  392. $i=0;
  393. foreach( $buffer as $buffer1 ) {
  394. list ( $list_browsers[$i][0], $list_browsers[$i][1]) = split ('[|]', $buffer1 );
  395. $i++;
  396. }
  397. return $list_browsers;
  398. }
  399. /**
  400. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  401. * @return a 2D array filled with code and name of OS
  402. * @desc This function is used to build an array containing
  403. OS informations
  404. */
  405. function loadOs()
  406. {
  407. $buffer = split ("#","Windows 95|Win 95#Windows_95|Win 95#Windows 98|Win 98#Windows NT|Win NT#Windows NT 5.0|Win 2000#Windows NT 5.1|Win XP#Windows 2000|Win 2000#Windows XP|Win XP#Windows ME|Win Me#Win95|Win 95#Win98|Win 98#WinNT|Win NT#linux-2.2|Linux 2#Linux|Linux#Linux 2|Linux 2#Macintosh|Mac#Mac_PPC|Mac#Mac_PowerPC|Mac#SunOS 5|SunOS 5#SunOS 6|SunOS 6#FreeBSD|FreeBSD#beOS|beOS#InternetSeer|InternetSeer#Googlebot|Googlebot#Teleport Pro|Teleport Pro");
  408. $i=0;
  409. foreach( $buffer as $buffer1 ) {
  410. list ( $list_os[$i][0], $list_os[$i][1]) = split ('[|]', $buffer1 );
  411. $i+=1;
  412. }
  413. return $list_os;
  414. }
  415. /**
  416. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  417. * @param remhost : must be $_SERVER['HTTP_USER_AGENT']
  418. * @param list_browsers : browsers list :x
  419. * @param list_os : os list :x
  420. * @return a string formatted like : browser|OS
  421. browser and OS are the 'viewable' names
  422. * @desc this function will extract browser and OS from
  423. $_SERVER['HTTP_USER_AGENT']
  424. */
  425. function extractAgent( $user_agent, $list_browsers, $list_os )
  426. {
  427. // default values, if nothing corresponding found
  428. $viewable_browser = "Unknown";
  429. $viewable_os = "Unknown";
  430. // search for corresponding pattern in $_SERVER['HTTP_USER_AGENT']
  431. // for browser
  432. for($i = 0; $i < count( $list_browsers ); $i++)
  433. {
  434. $pos = strpos( $user_agent, $list_browsers[$i][0] );
  435. if( $pos !== false )
  436. {
  437. $viewable_browser = $list_browsers[$i][1];
  438. }
  439. }
  440. // for os
  441. for($i = 0; $i < count($list_os); $i++)
  442. {
  443. $pos = strpos( $user_agent, $list_os[$i][0] );
  444. if( $pos !== false )
  445. {
  446. $viewable_os = $list_os[$i][1];
  447. }
  448. }
  449. return $viewable_browser."|".$viewable_os;
  450. }
  451. /**
  452. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  453. * @param browser : name of the browser or 'Unknown'
  454. * @param browsers_array :
  455. * @desc this function will :
  456. - if the browser is already in the table it will increment
  457. the corresponding value
  458. - if the browser doesn't exist it will be added and set to 1
  459. */
  460. function addBrowser($browser,$browsers_array)
  461. {
  462. if( isset( $browsers_array[$browser] ) )
  463. {
  464. // add one unity to this provider occurrences
  465. $browsers_array[$browser] = $browsers_array[$browser] + 1;
  466. }
  467. else
  468. {
  469. // first occurrence of this provider
  470. $browsers_array[$browser] = 1;
  471. }
  472. return $browsers_array;
  473. }
  474. /**
  475. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  476. * @param os : name of the OS or 'Unknown'
  477. * @param os_array : list of os and number of occurences
  478. * @desc this function will :
  479. - if the os is already in the table it will increment
  480. the corresponding value
  481. - if the os doesn't exist it will be added and set to 1
  482. */
  483. function addOs($os,$os_array)
  484. {
  485. if( isset( $os_array[$os] ) )
  486. {
  487. // add one unity to this provider occurrences
  488. $os_array[$os] = $os_array[$os] + 1;
  489. }
  490. else
  491. {
  492. // first occurrence of this provider
  493. $os_array[$os] = 1;
  494. }
  495. return $os_array;
  496. }
  497. /**
  498. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  499. * @param browsers_array : list of browsers and their counter
  500. * @desc update the browsers'table with new values
  501. */
  502. function fillBrowsersTable($browsers_array)
  503. {
  504. global $TABLESTATS_BROWSERS;
  505. if ( is_array($browsers_array ) )
  506. {
  507. foreach ( $browsers_array as $browser=>$number )
  508. {
  509. $sql = "SELECT counter
  510. FROM `".$TABLESTATS_BROWSERS."`
  511. WHERE `browser` = '".$browser."'";
  512. $res = mysql_query($sql);
  513. // if this provider already exists in the DB
  514. if( $row = mysql_num_rows($res) )
  515. {
  516. // update
  517. $sql2 = "UPDATE `".$TABLESTATS_BROWSERS."`
  518. SET `counter` = counter + '$number'
  519. WHERE `browser` = '".$browser."'";
  520. }
  521. else
  522. {
  523. // insert
  524. $sql2 = "INSERT INTO `".$TABLESTATS_BROWSERS."`
  525. (`browser`,`counter`)
  526. VALUES ('".$browser."','".$number."')";
  527. }
  528. mysql_query($sql2);
  529. }
  530. }
  531. }
  532. /**
  533. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  534. * @param os_array : list of os and their counter
  535. * @desc update the os'table with new values
  536. */
  537. function fillOsTable($os_array)
  538. {
  539. global $TABLESTATS_OS;
  540. if ( is_array($os_array) )
  541. {
  542. foreach ( $os_array as $os=>$number )
  543. {
  544. $sql = "SELECT counter
  545. FROM `".$TABLESTATS_OS."`
  546. WHERE `os` = '".$os."'";
  547. $res = mysql_query($sql);
  548. // if this provider already exists in the DB
  549. if( $row = mysql_num_rows($res) )
  550. {
  551. // update
  552. $sql2 = "UPDATE `".$TABLESTATS_OS."`
  553. SET `counter` = counter + '$number'
  554. WHERE `os` = '".$os."'";
  555. }
  556. else
  557. {
  558. // insert
  559. $sql2 = "INSERT INTO `".$TABLESTATS_OS."`
  560. (`os`,`counter`)
  561. VALUES ('".$os."','".$number."')";
  562. }
  563. mysql_query($sql2);
  564. }
  565. }
  566. }
  567. /***************************************************************************
  568. *
  569. * Referers
  570. *
  571. ***************************************************************************/
  572. /**
  573. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  574. * @param referer : name of the referer
  575. * @param referers_array : list of referer and number of occurences
  576. * @desc this function will :
  577. - if the referer is already in the table it will increment
  578. the corresponding value
  579. - if the referer doesn't exist it will be added and set to 1
  580. */
  581. function addReferer($referer,$referers_array)
  582. {
  583. if( isset( $referers_array[$referer] ) )
  584. {
  585. // add one unity to this provider occurrences
  586. $referers_array[$referer] = $referers_array[$referer] + 1;
  587. }
  588. else
  589. {
  590. // first occurrence of this provider
  591. $referers_array[$referer] = 1;
  592. }
  593. return $referers_array;
  594. }
  595. /**
  596. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  597. * @param referers_array : list of referers and their counter
  598. * @desc update the referers'table with new values
  599. */
  600. function fillReferersTable($referers_array)
  601. {
  602. global $TABLESTATS_REFERERS;
  603. if (is_array($referers_array) )
  604. {
  605. foreach ( $referers_array as $referer=>$number )
  606. {
  607. $sql = "SELECT counter
  608. FROM `".$TABLESTATS_REFERERS."`
  609. WHERE `referer` = '".$referer."'";
  610. $res = mysql_query($sql);
  611. // if this provider already exists in the DB
  612. if( $row = mysql_num_rows($res) )
  613. {
  614. // update
  615. $sql2 = "UPDATE `".$TABLESTATS_REFERERS."`
  616. SET `counter` = counter + '$number'
  617. WHERE `referer` = '".$referer."'";
  618. }
  619. else
  620. {
  621. // insert
  622. $sql2 = "INSERT INTO `".$TABLESTATS_REFERERS."`
  623. (`referer`,`counter`)
  624. VALUES ('".$referer."','".$number."')";
  625. }
  626. mysql_query($sql2);
  627. }
  628. }
  629. }
  630. ?>