functions.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. <?php
  2. /*****************************************************
  3. * This function is called from almost everywhere at *
  4. * the beggining of the page. It initializes the *
  5. * session variables, connect to the db, enabling *
  6. * the variable $con for futher use everywhere in *
  7. * the php code, and populates the arrays $user *
  8. * and $permission, with info about the user. *
  9. * @params: *
  10. * mode: (string) Indicates required permissions *
  11. * on database. "ro" gives read *
  12. * permissions, and "rw" read and write *
  13. * permissions. Other values will result in *
  14. * errors. *
  15. * @return: (db connection): The connection handler. *
  16. *****************************************************/
  17. function startdb($mode = "ro"){
  18. //Include the db configuration file. It's somehow like this
  19. /*
  20. <?php
  21. $host = "XXXX";
  22. $db_name = "XXXX";
  23. $username_ro = "XXXX";
  24. $username_rw = "XXXX";
  25. $pass_ro = "XXXX";
  26. $pass_rw = "XXXX";
  27. ?>
  28. */
  29. include(".htpasswd");
  30. //Connect to to database
  31. if ($mode == "ro"){
  32. $con = mysqli_connect($host, $username_ro, $pass_ro, $db_name);
  33. }
  34. else if ($mode == "rw"){
  35. $con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
  36. }
  37. else{
  38. $con = mysqli_connect("error", "error", "error", "error");
  39. }
  40. // Check connection
  41. if (mysqli_connect_errno()){
  42. error_log("Failed to connect to database: " . mysqli_connect_error());
  43. return -1;
  44. }
  45. //Set encoding options
  46. mysqli_set_charset($con, "utf-8");
  47. header("Content-Type: text/html; charset=utf8");
  48. mysqli_query($con, "SET NAMES utf8;");
  49. //Return the db connection
  50. return $con;
  51. }
  52. /*****************************************************
  53. * Finds out the protocol the user is connecting to *
  54. * the site with (http or https) *
  55. * @return: (string): "http://" or "https://". *
  56. ****************************************************/
  57. function getProtocol(){
  58. if (isset($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on" || $_SERVER["HTTPS"] == 1) || isset($_SERVER["HTTP_X_FORWARDED_PROTO"]) && $_SERVER["HTTP_X_FORWARDED_PROTO"] == "https") {
  59. $protocol = "https://";
  60. }
  61. else {
  62. $protocol = "http://";
  63. }
  64. return $protocol;
  65. }
  66. /*****************************************************
  67. * This function selects the language the page will *
  68. * be displayed inf the page. Several methods are *
  69. * used: cookie detection, and browser language *
  70. * preferences. *
  71. * @return: (string): Language code. *
  72. *****************************************************/
  73. function selectLanguage(){
  74. //Try to read cookie.
  75. header("Cache-control: private");
  76. if (isSet($_COOKIE["lang"])){
  77. $lang = $_COOKIE["lang"];
  78. if ($lang == "es" || $lang == "en" || $lang == "eu"){
  79. return $lang;
  80. }
  81. else{
  82. return "es";
  83. }
  84. }
  85. //If no cookie, select from client browser preferences.
  86. else{
  87. $available_languages = array("en", "eu", "es");
  88. $langs = prefered_language($available_languages, $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
  89. $lang = $langs[0];
  90. if ($lang != "es" && $lang != "en" && $lang != "eu"){
  91. return "es";
  92. }
  93. else{
  94. return $langs[0];
  95. }
  96. }
  97. }
  98. /*****************************************************
  99. * This function parses the language prefrences of *
  100. * the client broser, comparing them to th languages *
  101. * offered by the site. *
  102. * the variable $con for futher use everywhere in *
  103. * the php code, and populates the arrays $user *
  104. * and $permission, with info about the user. *
  105. * @params: *
  106. * available_languages: (string array) Contains *
  107. * a list of strins offered *
  108. * by the site. *
  109. * http_accept_language: (string) Raw header with *
  110. * info about client *
  111. * language preferences. *
  112. * @return: (string array) List of the languages *
  113. * offered, sorted by prefference. *
  114. *****************************************************/
  115. function prefered_language(array $available_languages, $http_accept_language) {
  116. $available_languages = array_flip($available_languages);
  117. $langs;
  118. preg_match_all("~([\w-]+)(?:[^,\d]+([\d.]+))?~", strtolower($http_accept_language), $matches, PREG_SET_ORDER);
  119. foreach($matches as $match) {
  120. list($a, $b) = explode("-", $match[1]) + array("", "");
  121. $value = isset($match[2]) ? (float) $match[2] : 1.0;
  122. if(isset($available_languages[$match[1]])) {
  123. $langs[$match[1]] = $value;
  124. continue;
  125. }
  126. if(isset($available_languages[$a])) {
  127. $langs[$a] = $value - 0.1;
  128. }
  129. }
  130. arsort($langs);
  131. return $langs;
  132. }
  133. /*****************************************************
  134. * This function turns a date string into a human *
  135. * readable string, deppending o the specified *
  136. * language. *
  137. * @params: *
  138. * strdate: (string): Date string. *
  139. * lang: (string): Language code (es, en, eu). *
  140. * http_accept_language: (string) Raw header with *
  141. * info about client *
  142. * language preferences. *
  143. * time: (boolean): Append the time at the end. *
  144. * @return: (string array) List of the languages *
  145. * offered, sorted by prefference. *
  146. *****************************************************/
  147. function formatDate($strdate, $lang, $time = true){
  148. $date = strtotime($strdate);
  149. $year = date("o", $date);
  150. $month = date("n", $date);
  151. $month --;
  152. $day = date("j", $date);
  153. $wday = date("N", $date);
  154. $wday --;
  155. $hour = date("H", $date);
  156. $minute = date("i", $date);
  157. switch ($lang){
  158. case "en":
  159. $week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
  160. $months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  161. if ($time){
  162. $str = "$week[$wday], $months[$month] $day, $year at $hour:$minute";
  163. }
  164. else{
  165. $str = "$week[$wday], $months[$month] $day, $year";
  166. }
  167. break;
  168. case "eu":
  169. $week = ["astelehena", "asteartea", "asteazkena", "osteguna", "ostirala", "larumbata", "igandea"];
  170. $months = ["urtarrilaren", "otsailaren", "martxoaren", "apirilaren", "maiatzaren", "ekainaren", "uztailaren", "abuztuaren", "irailaren", "urriaren", "azaroaren", "abenduaren"];
  171. if ($time){
  172. $str = $year . "ko $months[$month] $day" . "an, $week[$wday], $hour:$minute";
  173. }
  174. else{
  175. $str = $year . "ko $months[$month] $day" . "an, $week[$wday]";
  176. }
  177. break;
  178. default:
  179. $week = ["Lunes", "Martes", "Mi&eacute;rcoles", "Jueves", "Viernes", "S&aacute;bado", "Domingo"];
  180. $months = ["enero", " febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"];
  181. if ($time){
  182. $str = "$week[$wday] $day de $months[$month] de $year a las $hour:$minute";
  183. }
  184. else{
  185. $str = "$week[$wday] $day de $months[$month] de $year";
  186. }
  187. }
  188. return $str;
  189. }
  190. /*****************************************************
  191. * This function turns a date string into a human *
  192. * readable string, deppending o the specified *
  193. * language. It is designed to be used for festival *
  194. * days only, since it doesnt return the weekday. *
  195. * @params: *
  196. * strdate: (string): Date string. *
  197. * lang: (string): Language code (es, en, eu). *
  198. * http_accept_language: (string) Raw header with *
  199. * info about client *
  200. * language preferences. *
  201. * @return: (string array) List of the languages *
  202. * offered, sorted by prefference. *
  203. *****************************************************/
  204. function formatFestivalDate($strdate, $lang){
  205. $date = strtotime($strdate);
  206. $month = date("n", $date);
  207. $month --;
  208. $day = date("j", $date);
  209. switch ($lang){
  210. case "en":
  211. $months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  212. $str = "$months[$month] $day";
  213. break;
  214. case "eu":
  215. $months = ["urtarrilaren", "otsailaren", "martxoaren", "apirilaren", "maiatzaren", "ekainaren", "uztailaren", "abuztuaren", "irailaren", "urriaren", "azaroaren", "abenduaren"];
  216. $str = "$months[$month] $day";
  217. break;
  218. default:
  219. $months = ["enero", " febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"];
  220. $str = "$day de $months[$month]";
  221. }
  222. return $str;
  223. }
  224. /*****************************************************
  225. * Generates a URL-valid string from a regular one. *
  226. * *
  227. * @params: *
  228. * text: (string): Original string. *
  229. * @return: (string): URL-valid string. *
  230. *****************************************************/
  231. function permalink($text){
  232. $unwanted_array = array("Š"=>"S", "š"=>"s", "Ž"=>"Z", "ž"=>"z", "À"=>"A", "Á"=>"A", "Â"=>"A", "Ã"=>"A", "Ä"=>"A", "Å"=>"A", "Æ"=>"A", "Ç"=>"C", "È"=>"E", "É"=>"E",
  233. "Ê"=>"E", "Ë"=>"E", "Ì"=>"I", "Í"=>"I", "Î"=>"I", "Ï"=>"I", "Ñ"=>"N", "Ò"=>"O", "Ó"=>"O", "Ô"=>"O", "Õ"=>"O", "Ö"=>"O", "Ø"=>"O", "Ù"=>"U",
  234. "Ú"=>"U", "Û"=>"U", "Ü"=>"U", "Ý"=>"Y", "Þ"=>"B", "ß"=>"Ss", "à"=>"a", "á"=>"a", "â"=>"a", "ã"=>"a", "ä"=>"a", "å"=>"a", "æ"=>"a", "ç"=>"c",
  235. "è"=>"e", "é"=>"e", "ê"=>"e", "ë"=>"e", "ì"=>"i", "í"=>"i", "î"=>"i", "ï"=>"i", "ð"=>"o", "ñ"=>"n", "ò"=>"o", "ó"=>"o", "ô"=>"o", "õ"=>"o",
  236. "ö"=>"o", "ø"=>"o", "ù"=>"u", "ú"=>"u", "û"=>"u", "ý"=>"y", "þ"=>"b", "ÿ"=>"y" );
  237. $str = strtr( $text, $unwanted_array );
  238. $str = preg_replace("/[^\da-zA-Z ]/i", "", $str);
  239. $str = str_replace(" ", "-", $str);
  240. return $str;
  241. }
  242. /*****************************************************
  243. * This function closes all the opened HTML tags in *
  244. * a given string. *
  245. * *
  246. * @params: *
  247. * html: (string): The string with HTML tags. *
  248. * @return: (string): HTML with closed tags. *
  249. *****************************************************/
  250. function closeTags($html) {
  251. preg_match_all("#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU", $html, $result);
  252. $openedtags = $result[1];
  253. preg_match_all("#</([a-z]+)>#iU", $html, $result);
  254. $closedtags = $result[1];
  255. $len_opened = count($openedtags);
  256. if (count($closedtags) == $len_opened) {
  257. return $html;
  258. }
  259. $openedtags = array_reverse($openedtags);
  260. for ($i=0; $i < $len_opened; $i++) {
  261. if (!in_array($openedtags[$i], $closedtags)) {
  262. $html .= "</".$openedtags[$i].">";
  263. } else {
  264. unset($closedtags[array_search($openedtags[$i], $closedtags)]);
  265. }
  266. }
  267. return $html;
  268. }
  269. /*****************************************************
  270. * Text shortener. Given a string, it trims in the *
  271. * proximity of the desired string, ut to the next *
  272. * white character. If indicated, it will append a *
  273. * link to the full text. *
  274. * *
  275. * @params: *
  276. * text: (string): The text to shorten. *
  277. * length: (int): The desired length. *
  278. * linktext: (string): Text fot the link. *
  279. * link: (string): URI of the full text. *
  280. * @return: (string): Cutted text. *
  281. *****************************************************/
  282. function cutText($text, $length, $linktext, $link){
  283. if (strlen($text) < $length){
  284. return $text;
  285. }
  286. $cut = substr($text, 0, strpos($text, " ", $length));
  287. $cut = closeTags($cut);
  288. if (strlen($cut) == 0){
  289. $cut = $text;
  290. }
  291. if (strlen($text) != strlen($cut)){
  292. $cut = $cut . "... <a href='$link'>$linktext</a>";
  293. }
  294. return $cut;
  295. }
  296. /*****************************************************
  297. * Shows an ad at random. It does that one in three *
  298. * times, and only one per session (inserts a cookie *
  299. * to know if the ad has been shown. A sponsor has *
  300. * more chances of having his ad popping the more it *
  301. * contributes with Gasteizko Margolariak. *
  302. * @params: *
  303. * con: (Mysql connectrion): DB connection. *
  304. * lang: (string): Two-letter language code. *
  305. * lng: (string array): Literal array for the *
  306. * user language. *
  307. * @return: (int) Id of the shown sponsor, null if *
  308. * none was shown. *
  309. *****************************************************/
  310. function ad($con, $lang, $lng){
  311. $id = -1;
  312. //If the user hasn"t still see an add on this session
  313. if (isset($_SESSION["ad"]) == false){
  314. //25% chance of seeing an add
  315. if (rand (0, 3) == 0){
  316. //Create an array with weighted values
  317. $q = mysqli_query($con, "SELECT id, round(ammount/10) AS value FROM sponsor;");
  318. if (mysqli_num_rows($q) == 0){
  319. return $id;
  320. }
  321. $total = 0;
  322. $sponsors = array();
  323. while ($r = mysqli_fetch_array($q)){
  324. $times = $r["value"];
  325. $id = $r["id"];
  326. for ($i = 0; $i < $times; $i ++) {
  327. array_push($sponsors, $id);
  328. $total ++;
  329. }
  330. }
  331. //Get a random id from the array
  332. $id = $sponsors[rand (0, $total - 1)];
  333. //Get sponsor data
  334. $q = mysqli_query($con, "SELECT name_$lang AS name, text_$lang AS text, image, local, address_$lang AS address, link, lat, lon FROM sponsor WHERE id = $id;");
  335. $r = mysqli_fetch_array($q);
  336. ?>
  337. <div id='ad' class='section'>
  338. <h3 class='section_title'>
  339. <a target='_blank' href='<?=$r["link"]?>'><?=$r["name"]?></a>
  340. </h3>
  341. <div class='entry'>
  342. <div id='ad_details'>
  343. <?=$lng["ad_title"]?>
  344. <img class='pointer' src='/img/misc/slid-close.png' onClick='closeAd();' />
  345. </div>
  346. <?php
  347. if (strlen($r["image"]) > 0){
  348. ?>
  349. <div id='ad_image_container'>
  350. <img src='/img/spo/miniature/<?=$r["image"]?>'/>
  351. </div>
  352. <?php
  353. }
  354. ?>
  355. <?=$r["text"]?>
  356. <?php
  357. if($r["local"] == 1 && strlen($r["address"]) > 0){
  358. ?>
  359. <br/><br/>
  360. <a target='_blank' href='https://www.google.es/maps/@<?=$r["lat"]?>,<?=$r["lon"]?>,14z'>
  361. <img id='ad_pinpoint' src='/img/misc/pinpoint.png'\>
  362. <?=$r["address"]?>
  363. </a>
  364. <?php
  365. }
  366. ?>
  367. <br/><br/><br class='mobile'/><br class='mobile'/>
  368. <div id='ad_policy'>
  369. <a target='_blank' href='/ayuda/#section_ads'><?=$lng["help_ad_title"]?></a>
  370. </div>
  371. </div> <!-- .entry -->
  372. </div> <!-- .section -->
  373. <script type='text/javascript'>showAd();</script>
  374. <?php
  375. //Set ad as seen
  376. $_SESSION["ad"] = 1;
  377. } // if (rand (0, 3) == 0)
  378. } // if (isset($_SESSION["ad"]) == false)
  379. return $id;
  380. }
  381. /*****************************************************
  382. * Finds out the IP address of the client. *
  383. * *
  384. * @return: (String): Client IP address. *
  385. *****************************************************/
  386. function getUserIP(){
  387. $client = @$_SERVER["HTTP_CLIENT_IP"];
  388. $forward = @$_SERVER["HTTP_X_FORWARDED_FOR"];
  389. $remote = $_SERVER["REMOTE_ADDR"];
  390. if(filter_var($client, FILTER_VALIDATE_IP)){
  391. $ip = $client;
  392. }
  393. elseif(filter_var($forward, FILTER_VALIDATE_IP)){
  394. $ip = $forward;
  395. }
  396. else{
  397. $ip = $remote;
  398. }
  399. return $ip;
  400. }
  401. /*****************************************************
  402. * Registers the page viewed by the user. If it is *
  403. * first page shown in his visit, it also registers *
  404. * the visit. It will also increase the ad views *
  405. * count (if any was shown). *
  406. * *
  407. * @params: *
  408. * ad: (int): ID of the shown sponsor, or null. *
  409. * ad_static: (int array): List of the IDs of the *
  410. * sponsors with statis ads. *
  411. * section: (string): Site section being visited. *
  412. * id: (int): ID of the entri being visited *
  413. *****************************************************/
  414. function stats($ad, $ad_static, $section, $id){
  415. // Get client data
  416. $ip = getUserIP();
  417. $browser_data = get_browser(null, true);
  418. $os = $browser_data["platform"];
  419. $browser = $browser_data["browser"];
  420. $uagent = $browser_data["browser_name_pattern"];
  421. //If bot, do nothing
  422. $bot_kw = Array();
  423. $bot_kw[0] = "bot";
  424. $bot_kw[1] = "spider";
  425. $bot_kw[2] = "crawl";
  426. $bot_kw[3] = ".com";
  427. $bot_kw[4] = ".ru";
  428. $bot_kw[5] = "baidu";
  429. $bot_kw[6] = "survey";
  430. $bot_kw[7] = "scan";
  431. $bot_kw[8] = "feed";
  432. $bot_kw[9] = "bing";
  433. $bot_kw[10] = "yahoo";
  434. $bot_kw[11] = "engine";
  435. $bot_kw[12] = "preview";
  436. $bot_kw[13] = "checker";
  437. $bot_kw[14] = "catalog";
  438. $bot_kw[15] = "accelerator";
  439. $bot_kw[16] = "python";
  440. $bot_kw[14] = "qt";
  441. $bot_kw[15] = "webdav";
  442. $bot_kw[16] = "http";
  443. $bot_kw[17] = "url";
  444. $bot_kw[18] = "fake";
  445. $bot_kw[19] = "library";
  446. $bot_kw[20] = "commerce";
  447. $bot_kw[21] = "htmlbot";
  448. $bot_kw[22] = "fetch";
  449. $bot_kw[23] = "googleb";
  450. $bot_kw[24] = "facebook";
  451. $bot_kw[25] = "whatsapp";
  452. $bot_kw[26] = "pinterest";
  453. $bot_kw[27] = "scrapy";
  454. $bot_kw[28] = "libwww";
  455. $bot_kw[29] = "java standard library";
  456. $bot_kw[30] = "default browser";
  457. $bot_kw[31] = "twingly recon";
  458. $bot_kw[32] = "siteexplorer";
  459. $bot_kw[33] = "yandex";
  460. $bot_kw[34] = "wotbox";
  461. $bot_kw[35] = "apache synapse";
  462. $bot_kw[36] = "catexplorador";
  463. $bot_kw[37] = "internet archive";
  464. $i = 0;
  465. while ($i < sizeof($bot_kw)) {
  466. if (strpos(strtolower($uagent), $bot_kw[$i]) !== false || strpos(strtolower($browser), $bot_kw[$i]) !== false){
  467. mysqli_close($con);
  468. return;
  469. }
  470. $i ++;
  471. }
  472. if (strlen($os) == 0 || strlen($browser) == 0){ // Certain chinese hosting provider makes a lot of requests...
  473. mysqli_close($con);
  474. return;
  475. }
  476. //Look for a visit with the same IP in the last 30 mins.
  477. $con = startdb("rw");
  478. $q = mysqli_query($con, "SELECT stat_visit.id AS visitid FROM stat_view, stat_visit WHERE visit = stat_visit.id AND dtime > DATE_SUB(now(), INTERVAL 30 MINUTE) AND ip = '$ip' AND uagent = '$uagent';");
  479. if (mysqli_num_rows($q) == 0){
  480. mysqli_query($con, "INSERT INTO stat_visit (ip, uagent, os, browser) VALUES ('$ip', '$uagent', '$os', '$browser');");
  481. $q = mysqli_query($con, "SELECT stat_visit.id AS visitid FROM stat_visit WHERE ip = '$ip' AND uagent = '$uagent' ORDER BY stat_visit.id DESC LIMIT 1;");
  482. }
  483. $r = mysqli_fetch_array($q);
  484. $visit = $r['visitid'];
  485. mysqli_query($con, "INSERT INTO stat_view (visit, section, entry) VALUES ($visit, '$section', '$id');");
  486. //Increase pop ad advertiser count
  487. if ($ad > 0){
  488. mysqli_query($con, "UPDATE sponsor SET print = print + 1 WHERE id = $ad;");
  489. }
  490. if (is_array($ad_static)){
  491. foreach ($ad_static as $sponsor) {
  492. mysqli_query($con, "UPDATE sponsor SET print_static = print_static + 1 WHERE id = $sponsor;");
  493. }
  494. }
  495. }
  496. /*****************************************************
  497. * Recalculates the global count of statistics. *
  498. * *
  499. * @params: *
  500. * day: (data): Do it for the specified day, or *
  501. * all the stats if it is null. *
  502. *****************************************************/
  503. function recalculateStats($day = null){
  504. $con = startdb("rw");
  505. // Visit count
  506. $s = "SELECT date(dtime) AS day, count(stat_visit.id) AS visit FROM stat_view, stat_visit where stat_visit.id = stat_view.visit";
  507. if (day != null){
  508. $s = $s . " AND date(dtime) = str_to_date('$day', '%Y-%m-%d')";
  509. }
  510. $s = $s . " GROUP BY date(dtime);";
  511. $q = mysqli_query($con, $s);
  512. while ($r = mysqli_fetch-array($q)){
  513. $q_check = mysqli_query("SELECT date FROM stat_daily_visit WHERE day = str_to_date('$day', '%Y-%m-%d')");
  514. if (mysqli_num_rows($q_check) > 0){
  515. // Update
  516. mysqli_query($con, "UPDATE stat_daily_visit SET visit = $r[visit] WHERE day = str_to_date('$day', '%Y-%m-%d')");
  517. }
  518. else{
  519. // Insert
  520. mysqli_query($con, "INSERT INTO stat_daily_visit (day, visit) VALUES (str_to_date('$day', '%Y-%m-%d'), $r[visit])");
  521. }
  522. }
  523. // Visit count by os and browser.
  524. $s = "SELECT date(dtime) AS day, os, browser, count(stat_visit.id) AS visit FROM stat_view, stat_visit where stat_visit.id = stat_view.visit";
  525. if (day != null){
  526. $s = $s . " AND date(dtime) = str_to_date('$day', '%Y-%m-%d')";
  527. }
  528. $s = $s . " GROUP BY date(dtime), os, browser;";
  529. $q = mysqli_query($con, $s);
  530. while ($r = mysqli_fetch-array($q)){
  531. $q_check = mysqli_query("SELECT date FROM stat_daily_visit_browser WHERE day = str_to_date('$day', '%Y-%m-%d') AND os = $r[os] AND browser = $r[browser]");
  532. if (mysqli_num_rows($q_check) > 0){
  533. // Update
  534. mysqli_query($con, "UPDATE stat_daily_visit_browser SET visit = $r[visit] WHERE day = str_to_date('$day', '%Y-%m-%d') AND os = $r[os] AND browser = $r[browser]");
  535. }
  536. else{
  537. // Insert
  538. mysqli_query($con, "INSERT INTO stat_daily_visit_browser (day, os, browser, visit) VALUES (str_to_date('$day', '%Y-%m-%d'), $r[os], $[browser], $r[visit])");
  539. }
  540. }
  541. }
  542. ?>