stats.lib.inc.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. global $TABLETRACK_OPEN;
  79. // record initial value of ignore_user_abort
  80. $ignore = ignore_user_abort();
  81. // prevent script from being stopped while executing, the following can be considered
  82. // as a transaction
  83. ignore_user_abort(1) ;
  84. // we take the last event id to prevent miss of some recorded event
  85. // only processed record have to be cleaned
  86. $sql = "SELECT open_id
  87. FROM `$TABLETRACK_OPEN`
  88. WHERE open_date <= NOW()
  89. ORDER BY open_id DESC
  90. LIMIT 1";
  91. //$processBegin = getOneResult($sql);
  92. $query = Database::query($sql);
  93. $res = @Database::fetch_array($query);
  94. $processBegin = $res[0];
  95. // process
  96. //--Providers And Countries-------------------------------------------//
  97. $sql = "SELECT open_remote_host
  98. FROM `$TABLETRACK_OPEN`
  99. WHERE open_remote_host != ''
  100. AND open_id <= '".$processBegin."' ";
  101. $query = Database::query($sql);
  102. if(Database::num_rows($query) != 0) {
  103. // load list of countries
  104. $list_countries = loadCountries();
  105. while ($row = Database::fetch_row ($query)) {
  106. $remote_host = $row[0];
  107. /*****Provider*****/
  108. //extract provider
  109. $provider = extractProvider( $remote_host );
  110. // add or increment provider in the providers array
  111. $providers_array = addProvider( $provider,$providers_array );
  112. /*****Countries*****/
  113. // extract country
  114. $country = extractCountry( $remote_host, $list_countries );
  115. // increment country in the countries table
  116. $countries_array = addCountry( $country, $countries_array );
  117. }
  118. // update tables
  119. fillProvidersTable( $providers_array );
  120. fillCountriesTable( $countries_array );
  121. }
  122. // provider and countries done
  123. //--Browsers and OS---------------------------------------------------//
  124. $sql = "SELECT open_agent
  125. FROM `$TABLETRACK_OPEN`
  126. WHERE open_remote_host != ''
  127. AND open_id <= '".$processBegin."' ";
  128. $query =Database::query( $sql );
  129. if(Database::num_rows($query) != 0) {
  130. // load lists
  131. // of browsers
  132. $list_browsers = loadBrowsers();
  133. // of OS
  134. $list_os = loadOs();
  135. while ($row = Database::fetch_row ($query)) {
  136. $agent = $row[0];
  137. /*****Browser and OS*****/
  138. // extract browser and OS
  139. list( $browser,$os ) = split( "[|]",extractAgent( $agent , $list_browsers , $list_os ) );
  140. // increment browser and OS in the corresponding arrays
  141. $browsers_array = addBrowser( $browser , $browsers_array );
  142. $os_array = addOs( $os , $os_array );
  143. }
  144. fillBrowsersTable( $browsers_array );
  145. fillOsTable( $os_array );
  146. }
  147. // browsers and OS done
  148. //--Referers----------------------------------------------------------//
  149. $sql = "SELECT open_referer
  150. FROM `$TABLETRACK_OPEN`
  151. WHERE open_referer != ''
  152. AND open_id <= '".$processBegin."' ";
  153. $query = Database::query( $sql );
  154. if(Database::num_rows($query) != 0) {
  155. $i=0;
  156. while ($row = Database::fetch_row ($query)) {
  157. $ref = $row[0];
  158. $referers_array = addReferer( $ref , $referers_array );
  159. }
  160. fillReferersTable( $referers_array );
  161. }
  162. // referers done
  163. //-------------------------------------------------------------------//
  164. // end of process
  165. // cleaning of $TABLETRACK_OPEN table
  166. cleanProcessedRecords($processBegin);
  167. // reset to the initial value
  168. ignore_user_abort($ignore);
  169. }
  170. /***************************************************************************
  171. *
  172. * Utils
  173. *
  174. ***************************************************************************/
  175. /**
  176. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  177. * @param limit : all records BEFORE $limit will be affected
  178. * @desc this function will delete the remote_host, user_agent
  179. and referer rows from the track_open table recorded before
  180. the date $limit. OPTIMIZE is called to get back the memory
  181. espaces deleted
  182. */
  183. function cleanProcessedRecords( $limit ) {
  184. global $TABLETRACK_OPEN;
  185. $sql = "UPDATE `".$TABLETRACK_OPEN."`
  186. SET open_remote_host = '',
  187. open_agent = '',
  188. open_referer =''
  189. WHERE open_id <= '".$limit."'";
  190. $query = Database::query( $sql );
  191. Database::query("OPTIMIZE TABLE $TABLETRACK_OPEN");
  192. }
  193. /***************************************************************************
  194. *
  195. * Provider
  196. *
  197. ***************************************************************************/
  198. /**
  199. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  200. * @param remhost : must be @getHostByAddr($_SERVER['REMOTE_ADDR']
  201. * @desc this function will extract the provider name from a given
  202. remote host and record this occurence in the corresponding
  203. table
  204. */
  205. function extractProvider($remhost) {
  206. if($remhost == "Unknown")
  207. return $remhost;
  208. $explodedRemhost = explode(".", $remhost);
  209. $provider = $explodedRemhost[sizeof( $explodedRemhost )-2]
  210. ."."
  211. .$explodedRemhost[sizeof( $explodedRemhost )-1];
  212. if($provider == "co.uk" || $provider == "co.jp")
  213. return $explodedRemhost[sizeof( $explodedRemhost )-3].$provider;
  214. else return $provider;
  215. }
  216. /**
  217. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  218. * @param provider : name of the provider
  219. * @param providers_array : list of providers and their counter
  220. * @desc this function will :
  221. - if the provider is already in the array it will increment
  222. the corresponding value
  223. - if the provider doesn't exist it will be added and set to 1
  224. */
  225. function addProvider($provider,$providers_array) {
  226. if(isset($providers_array[$provider])) {
  227. // add one unity to this provider occurrences
  228. $providers_array[$provider] = $providers_array[$provider] + 1;
  229. } else {
  230. // first occurrence of this provider
  231. $providers_array[$provider] = 1;
  232. }
  233. return $providers_array;
  234. }
  235. /**
  236. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  237. * @param providers_array : list of providers and their counter
  238. * @desc update the providers'table with new values
  239. */
  240. function fillProvidersTable($providers_array) {
  241. global $TABLESTATS_PROVIDERS;
  242. if(is_array($providers_array)) {
  243. foreach ( $providers_array as $prov=>$number ) {
  244. $sql = "SELECT counter
  245. FROM `".$TABLESTATS_PROVIDERS."`
  246. WHERE `provider` = '".$prov."'";
  247. $res = Database::query($sql);
  248. // if this provider already exists in the DB
  249. if($row = Database::num_rows($res)) {
  250. // update
  251. $sql2 = "UPDATE `".$TABLESTATS_PROVIDERS."`
  252. SET `counter` = counter + '$number'
  253. WHERE `provider` = '".$prov."'";
  254. } else {
  255. // insert
  256. $sql2 = "INSERT INTO `".$TABLESTATS_PROVIDERS."`
  257. (`provider`,`counter`)
  258. VALUES ('".$prov."','".$number."')";
  259. }
  260. Database::query($sql2);;
  261. }
  262. }
  263. }
  264. /***************************************************************************
  265. *
  266. * Country
  267. *
  268. ***************************************************************************/
  269. /**
  270. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  271. * @return a 2D array filled with code and name of countries
  272. * @desc This function is used to build an array containing
  273. countries informations
  274. */
  275. function loadCountries() {
  276. global $TABLESTATS_COUNTRIES;
  277. $sql = "SELECT code, country
  278. FROM `".$TABLESTATS_COUNTRIES."`";
  279. $res = Database::query($sql);
  280. $i = 0 ;
  281. if (Database::num_rows($res) > 0){
  282. while ($row = Database::fetch_array($res)) {
  283. $list_countries[$i][0] = $row["code"];
  284. $list_countries[$i][1] = $row["country"];
  285. $i++;
  286. }
  287. }
  288. Database::free_result($res);
  289. return $list_countries;
  290. }
  291. /**
  292. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  293. * @param remhost : must be @getHostByAddr($_SERVER['REMOTE_ADDR']
  294. * @param list_countries : list of countries -__-
  295. * @return Name of the country or "Unknown" if not found
  296. * @desc this function will extract the country from a given remote
  297. host and increment the good value in the corresponding table
  298. */
  299. function extractCountry($remhost,$list_countries) {
  300. if($remhost == "Unknown")
  301. return $remhost;
  302. // country code is the last value of remote host
  303. $explodedRemhost = explode(".",$remhost);
  304. $countryCode = $explodedRemhost[sizeof( $explodedRemhost )-1];
  305. for($i = 0 ; $i < sizeof( $list_countries );$i++) {
  306. if($list_countries[$i][0] == $countryCode)
  307. return $list_countries[$i][1];
  308. }
  309. }
  310. /**
  311. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  312. * @param country : name of the country or 'Unknown'
  313. * @param countries_array : list of countries and their
  314. number of occurence
  315. * @desc this function will increment number of occurrence
  316. for $country in the countries' tables
  317. */
  318. function addCountry($country,$countries_array) {
  319. if(isset($countries_array[$country])) {
  320. // add one unity to this provider occurrences
  321. $countries_array[$country] = $countries_array[$country] + 1;
  322. } else {
  323. // first occurrence of this provider
  324. $countries_array[$country] = 1;
  325. }
  326. return $countries_array;
  327. }
  328. /**
  329. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  330. * @param countries_array : list of countries and their counter
  331. * @desc update the countries'table with new values
  332. */
  333. function fillCountriesTable($countries_array) {
  334. global $TABLESTATS_COUNTRIES;
  335. if(is_array($countries_array)) {
  336. foreach ( $countries_array as $country=>$number ) {
  337. // update
  338. $sql = "UPDATE `".$TABLESTATS_COUNTRIES."`
  339. SET `counter` = counter + '$number'
  340. WHERE `country` = '".$country."'";
  341. Database::query($sql);
  342. }
  343. }
  344. }
  345. /***************************************************************************
  346. *
  347. * Agent : Browser and OS
  348. *
  349. ***************************************************************************/
  350. /**
  351. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  352. * @return a 2D array filled with code and name of browsers
  353. * @desc This function is used to build an array containing
  354. browser informations
  355. */
  356. function loadBrowsers() {
  357. $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");
  358. //$list_browser[x][0] is the name of browser as in $_SERVER['HTTP_USER_AGENT']
  359. //$list_browser[x][1] is the name of browser that will be used in display and tables
  360. $i=0;
  361. foreach( $buffer as $buffer1 ) {
  362. list ( $list_browsers[$i][0], $list_browsers[$i][1]) = split ('[|]', $buffer1 );
  363. $i++;
  364. }
  365. return $list_browsers;
  366. }
  367. /**
  368. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  369. * @return a 2D array filled with code and name of OS
  370. * @desc This function is used to build an array containing
  371. OS informations
  372. */
  373. function loadOs() {
  374. $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");
  375. $i=0;
  376. foreach( $buffer as $buffer1 ) {
  377. list ( $list_os[$i][0], $list_os[$i][1]) = split ('[|]', $buffer1 );
  378. $i+=1;
  379. }
  380. return $list_os;
  381. }
  382. /**
  383. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  384. * @param remhost : must be $_SERVER['HTTP_USER_AGENT']
  385. * @param list_browsers : browsers list :x
  386. * @param list_os : os list :x
  387. * @return a string formatted like : browser|OS
  388. browser and OS are the 'viewable' names
  389. * @desc this function will extract browser and OS from
  390. $_SERVER['HTTP_USER_AGENT']
  391. */
  392. function extractAgent( $user_agent, $list_browsers, $list_os ) {
  393. // default values, if nothing corresponding found
  394. $viewable_browser = "Unknown";
  395. $viewable_os = "Unknown";
  396. // search for corresponding pattern in $_SERVER['HTTP_USER_AGENT']
  397. // for browser
  398. for($i = 0; $i < count( $list_browsers ); $i++) {
  399. $pos = strpos( $user_agent, $list_browsers[$i][0] );
  400. if( $pos !== false ) {
  401. $viewable_browser = $list_browsers[$i][1];
  402. }
  403. }
  404. // for os
  405. for($i = 0; $i < count($list_os); $i++) {
  406. $pos = strpos( $user_agent, $list_os[$i][0] );
  407. if( $pos !== false ) {
  408. $viewable_os = $list_os[$i][1];
  409. }
  410. }
  411. return $viewable_browser."|".$viewable_os;
  412. }
  413. /**
  414. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  415. * @param browser : name of the browser or 'Unknown'
  416. * @param browsers_array :
  417. * @desc this function will :
  418. - if the browser is already in the table it will increment
  419. the corresponding value
  420. - if the browser doesn't exist it will be added and set to 1
  421. */
  422. function addBrowser($browser,$browsers_array) {
  423. if(isset( $browsers_array[$browser])) {
  424. // add one unity to this provider occurrences
  425. $browsers_array[$browser] = $browsers_array[$browser] + 1;
  426. } else {
  427. // first occurrence of this provider
  428. $browsers_array[$browser] = 1;
  429. }
  430. return $browsers_array;
  431. }
  432. /**
  433. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  434. * @param os : name of the OS or 'Unknown'
  435. * @param os_array : list of os and number of occurences
  436. * @desc this function will :
  437. - if the os is already in the table it will increment
  438. the corresponding value
  439. - if the os doesn't exist it will be added and set to 1
  440. */
  441. function addOs($os,$os_array) {
  442. if(isset($os_array[$os])) {
  443. // add one unity to this provider occurrences
  444. $os_array[$os] = $os_array[$os] + 1;
  445. } else {
  446. // first occurrence of this provider
  447. $os_array[$os] = 1;
  448. }
  449. return $os_array;
  450. }
  451. /**
  452. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  453. * @param browsers_array : list of browsers and their counter
  454. * @desc update the browsers'table with new values
  455. */
  456. function fillBrowsersTable($browsers_array) {
  457. global $TABLESTATS_BROWSERS;
  458. if (is_array($browsers_array)) {
  459. foreach ( $browsers_array as $browser=>$number ) {
  460. $sql = "SELECT counter
  461. FROM `".$TABLESTATS_BROWSERS."`
  462. WHERE `browser` = '".$browser."'";
  463. $res = Database::query($sql);
  464. // if this provider already exists in the DB
  465. if($row = Database::num_rows($res)) {
  466. // update
  467. $sql2 = "UPDATE `".$TABLESTATS_BROWSERS."`
  468. SET `counter` = counter + '$number'
  469. WHERE `browser` = '".$browser."'";
  470. } else {
  471. // insert
  472. $sql2 = "INSERT INTO `".$TABLESTATS_BROWSERS."`
  473. (`browser`,`counter`)
  474. VALUES ('".$browser."','".$number."')";
  475. }
  476. Database::query($sql2);
  477. }
  478. }
  479. }
  480. /**
  481. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  482. * @param os_array : list of os and their counter
  483. * @desc update the os'table with new values
  484. */
  485. function fillOsTable($os_array) {
  486. global $TABLESTATS_OS;
  487. if (is_array($os_array)) {
  488. foreach ($os_array as $os=>$number) {
  489. $sql = "SELECT counter
  490. FROM `".$TABLESTATS_OS."`
  491. WHERE `os` = '".$os."'";
  492. $res = Database::query($sql);
  493. // if this provider already exists in the DB
  494. if($row = Database::num_rows($res)) {
  495. // update
  496. $sql2 = "UPDATE `".$TABLESTATS_OS."`
  497. SET `counter` = counter + '$number'
  498. WHERE `os` = '".$os."'";
  499. } else {
  500. // insert
  501. $sql2 = "INSERT INTO `".$TABLESTATS_OS."`
  502. (`os`,`counter`)
  503. VALUES ('".$os."','".$number."')";
  504. }
  505. Database::query($sql2);
  506. }
  507. }
  508. }
  509. /***************************************************************************
  510. *
  511. * Referers
  512. *
  513. ***************************************************************************/
  514. /**
  515. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  516. * @param referer : name of the referer
  517. * @param referers_array : list of referer and number of occurences
  518. * @desc this function will :
  519. - if the referer is already in the table it will increment
  520. the corresponding value
  521. - if the referer doesn't exist it will be added and set to 1
  522. */
  523. function addReferer($referer,$referers_array) {
  524. if(isset($referers_array[$referer])) {
  525. // add one unity to this provider occurrences
  526. $referers_array[$referer] = $referers_array[$referer] + 1;
  527. } else {
  528. // first occurrence of this provider
  529. $referers_array[$referer] = 1;
  530. }
  531. return $referers_array;
  532. }
  533. /**
  534. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  535. * @param referers_array : list of referers and their counter
  536. * @desc update the referers'table with new values
  537. */
  538. function fillReferersTable($referers_array) {
  539. global $TABLESTATS_REFERERS;
  540. if (is_array($referers_array)) {
  541. foreach ( $referers_array as $referer=>$number ) {
  542. $sql = "SELECT counter
  543. FROM `".$TABLESTATS_REFERERS."`
  544. WHERE `referer` = '".$referer."'";
  545. $res = Database::query($sql);
  546. // if this provider already exists in the DB
  547. if($row = Database::num_rows($res)) {
  548. // update
  549. $sql2 = "UPDATE `".$TABLESTATS_REFERERS."`
  550. SET `counter` = counter + '$number'
  551. WHERE `referer` = '".$referer."'";
  552. } else {
  553. // insert
  554. $sql2 = "INSERT INTO `".$TABLESTATS_REFERERS."`
  555. (`referer`,`counter`)
  556. VALUES ('".$referer."','".$number."')";
  557. }
  558. Database::query($sql2);
  559. }
  560. }
  561. }