stats.lib.inc.php 21 KB

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