stats.lib.inc.php 21 KB

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