stats.lib.inc.php 21 KB

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