Просмотр файла includes/counter.php

Размер файла: 10.2Kb
  1. <?php
  2. error_reporting(0);
  3. $site=explode('/',$_SERVER['QUERY_STRING']);
  4. $ip=$site[2];
  5. $pkey='/'.$site[1];
  6. $site=$site[0];
  7.  
  8. $name=explode('.',$_SERVER['HTTP_X_FORWARDED_HOST']);
  9. $domain=strtolower($name[1]);
  10.  
  11. $counterfile='/home/kmx/www/sites/'.$domain.'/'.$site.'/counter.dat';
  12. if ($site=='top') $counterfile="/home/kmx/includes/sluzhebnye/top.counter";
  13. elseif ($site=='wap') $counterfile="/home/kmx/includes/sluzhebnye/wap.counter";
  14. elseif ($site=='catalog') $counterfile="/home/kmx/includes/sluzhebnye/catalog.counter";
  15. elseif ($site=='forum') $counterfile="/home/kmx/includes/sluzhebnye/forum.counter";
  16. elseif (!is_dir('/home/kmx/www/sites/'.$domain.'/'.$site))
  17. {
  18. header("Content-type: image/vnd.wap.wbmp");
  19. $im=ImageCreate(76,8);
  20. $white=ImageColorAllocate($im, 255,255,255);
  21. $black=ImageColorAllocate($im, 0,0,0);
  22. imageString($im, 1, 1, 0, 'Site not found!', $black);
  23. imagewbmp($im);
  24. imageDestroy($im);
  25. }
  26.  
  27. $page=file('/home/kmx/www/sites/'.$domain.'/'.$site.'/index.wml');
  28. for ($i=0;$i<sizeof($page);$i++)
  29. {
  30. if (substr(trim($page[$i]),0,12)!='<!-- cnt -->') continue;
  31. $stra=explode('cnt.'.$domain.'.ru/'.$site,$page[$i]);
  32. $stra=explode('"',$stra[1]);
  33. $pkey_=$stra[0];
  34. break;
  35. }
  36. unset($page);
  37.  
  38. $y2k = mktime(23,59,59,date('n'),date('j'),date('Y'));
  39. setcookie($site, 1, $y2k);
  40. $vizit=$HTTP_COOKIE_VARS[$site];
  41.  
  42. $hua="*".strtoupper($_SERVER['HTTP_USER_AGENT']);
  43.  
  44. $browser=$_SERVER['HTTP_USER_AGENT'];
  45.  
  46. $komp=false;
  47.  
  48. $komp=(trim($hua)=='*')
  49. ||strpos($hua,'MOZILLA')
  50. ||strpos($hua,'MSIE')
  51. ||strpos($hua,'WINDOWS')
  52. ||strpos($hua,'KLONDIKE')
  53. #||strpos($hua,'OPERA')
  54. ||strpos($hua,'M3GATE')
  55. ||strpos($hua,'EMULATOR')
  56. ||strpos($hua,'TAGTAG')
  57. ||strpos($hua,'ALPHACELL')
  58. ||strpos($hua,'WAPSILON')
  59. ||strpos($hua,'EZOS')
  60. ||strpos($hua,'BILBO')
  61. ||strpos($hua,'WINWAP')
  62. ||strpos($hua,'MYSOCK')
  63. ||strpos($hua,'BERCUT')
  64. ||strpos($hua,'WAPPER')
  65. ||strpos($hua,'TTPCOM')
  66. ||strpos($hua,'UNKNOWN')
  67. ||strpos($hua,'PORTALMMM')
  68. ||strpos($hua,'EZZE')
  69. ||strpos($hua,'JAVA')
  70. ||strpos($hua,'JBROWSER')
  71. ||strpos($hua,'SEND')
  72. ||strpos($hua,'WINSIM')
  73. ||strpos($hua,'WAPALIZER')
  74. ||strpos($hua,'TELEPORT')
  75. ||strpos($hua,'TSM')
  76. ||strpos($hua,'TCL')
  77. ||strpos($hua,'OPWV')
  78. ||strpos($hua,'SKT');
  79. if ($komp) $browser='-komp-';
  80.  
  81. if (!file_exists($counterfile))
  82. {
  83. # Создание файла данных счетчика
  84. $f=@fopen($counterfile,"w+");
  85. fseek($f,0,SEEK_SET); fwrite($f,str_repeat(' ',1500));
  86. fseek($f,0,SEEK_SET); fwrite($f,substr('ID='.$n.' ',0,9)."\n");
  87. fseek($f,10,SEEK_SET); fwrite($f,substr(''.$counter[5].str_repeat(' ',55),0,60)."\n");
  88. fseek($f,71,SEEK_SET); fwrite($f,substr(''.$counter[6].str_repeat(' ',25),0,20)."\n");
  89. fseek($f,92,SEEK_SET); fwrite($f,substr(''.$counter[7].str_repeat(' ',75),0,60)."\n");
  90. fseek($f,153,SEEK_SET); fwrite($f,substr(''.trim($counter[8]).str_repeat(' ',75),0,23)."\n");
  91. fseek($f,177,SEEK_SET); fwrite($f,date('d.m.Y H:i:s (----------)')."\n");
  92. fseek($f,210,SEEK_SET); fwrite($f,str_repeat('=',29)."\n");
  93. fseek($f,240,SEEK_SET); fwrite($f,substr('ВСЕГО: '.$counter[0].str_repeat(' ',9),0,16)."\n");
  94. fseek($f,257,SEEK_SET); fwrite($f,str_repeat('-',29)."\n");
  95. fseek($f,287,SEEK_SET); fwrite($f," ПО ЧАСАМ \n");
  96. fseek($f,310,SEEK_SET); fwrite($f,"HOUR HIT HOST TIMESTAMP \n");
  97. fseek($f,1060,SEEK_SET); fwrite($f,str_repeat('-',29)."\n");
  98. fseek($f,1090,SEEK_SET); fwrite($f," ПО ДНЯМ\n");
  99. fseek($f,1110,SEEK_SET); fwrite($f,"DAY HIT HOST TIMESTAMP \n");
  100. fclose($f);
  101. @chmod($counterfile,0777);
  102. }
  103.  
  104. #========= РАБОТА С ФАЙЛОМ СЧЕТЧИКА ============================================
  105. if ($f=@fopen($counterfile,"r+"))
  106. {
  107. flock($f,2);
  108. fseek($f,0,SEEK_SET);
  109.  
  110. // Время сейчас
  111. $hour=date('G');
  112. $day=date('w');
  113. $timestamp=time();
  114.  
  115. // Время на счетчике
  116. fseek($f,198,SEEK_SET); $timestamp_=fread($f,10);
  117. $hour_=date('G',$timestamp_);
  118. $day_=date('w',$timestamp_);
  119.  
  120. # Если большая пауза счета, то сбрасываем просроченые значения
  121. if ($timestamp-$timestamp_>3600)
  122. {
  123. for ($_hour=0;$_hour<24;$_hour++)
  124. {
  125. fseek($f,359+$_hour*30,SEEK_SET);
  126. $_timestamp=fread($f,10);
  127. fseek($f,340+$_hour*30,SEEK_SET);
  128. if ($timestamp-$_timestamp>86400) fwrite($f,substr('0'.$_hour,-2)." 0 0 ----------\n");
  129. }
  130. for ($_day=0;$_day<7;$_day++)
  131. {
  132. fseek($f,1159+$_day*30,SEEK_SET);
  133. $_timestamp=fread($f,10);
  134. fseek($f,1140+$_day*30,SEEK_SET);
  135. if ($timestamp-$_timestamp>604800) fwrite($f,$_day." 0 0 ----------\n");
  136. }
  137. }
  138.  
  139. # Счетчик за час
  140. if ($hour_==$hour)
  141. {
  142. fseek($f,342+$hour*30,SEEK_SET);
  143. $hit_hour=trim(fread($f,9));
  144. $host_hour=trim(fread($f,7));
  145. }
  146. else
  147. {
  148. $hit_hour=0;
  149. $host_hour=0;
  150. }
  151.  
  152. # Счетчик за день
  153. if ($day_==$day)
  154. {
  155. fseek($f,1142+$day*30,SEEK_SET);
  156. $hit_day=trim(fread($f,9));
  157. $host_day=trim(fread($f,7));
  158. }
  159. else
  160. {
  161. $hit_day=0;
  162. $host_day=0;
  163. }
  164.  
  165. # Общее хитов
  166. fseek($f,247,SEEK_SET);
  167. $hit_all=trim(fread($f,9));
  168.  
  169. if (($site=='top')||($site=='wap')||($site=='catalog')||($site=='forum')) $pkey=$pkey_;
  170.  
  171. if ($pkey==$pkey_)
  172. {
  173. # Проверка уникальности
  174. $unical=true;
  175. $data_unical='';
  176. $userinfo=trim(substr(substr($_SERVER['REMOTE_ADDR'].str_repeat(' ',15),0,15).' '.$browser,0,68));
  177. fseek($f,1450,SEEK_SET);
  178. while (!feof($f))
  179. {
  180. $_timestamp=fread($f,10);
  181. $_userinfo=trim(fread($f,70));
  182. if ($timestamp-$_timestamp>3600) continue;
  183. $data_unical.=$_timestamp.' '.substr($_userinfo.str_repeat(' ',70),0,68)."\n";
  184. if ((substr($userinfo,0,15)==substr($_userinfo,0,15))&&(substr($_userinfo,15,6)=='-komp-')) $unical=false;
  185. if ($userinfo==$_userinfo) $unical=false;
  186. if ($vizit==1) $unical=false;
  187. }
  188. if ($unical) $data_unical.=$timestamp.' '.substr($userinfo.str_repeat(' ',70),0,68)."\n";
  189. fseek($f,1350,SEEK_SET); fwrite($f,str_repeat('-',39)."\n");
  190. fseek($f,1390,SEEK_SET); fwrite($f," УНИКАЛЬНЫЕ \n");
  191. fseek($f,1410,SEEK_SET); fwrite($f,"TIMESTAMP IP USER_AGENT \n");
  192. fseek($f,1450,SEEK_SET); fwrite($f,$data_unical);
  193.  
  194. # Увеличение хостов
  195. if ($unical)
  196. {
  197. $host_day++;
  198. $host_hour++;
  199. }
  200.  
  201. # Увеличение хитов
  202. $hit_all++;
  203. $hit_day++;
  204. $hit_hour++;
  205.  
  206. # Сохраняем
  207. fseek($f,340+$hour*30,SEEK_SET); fwrite($f,substr('0'.$hour,-2).' '.substr(' '.$hit_hour,-8).' '.substr(' '.$host_hour,-6).' '.time()."\n");
  208. fseek($f,1140+$day*30,SEEK_SET); fwrite($f,$day.' '.substr(' '.$hit_day,-9).' '.substr(' '.$host_day,-6).' '.time()."\n");
  209. fseek($f,247,SEEK_SET); fwrite($f,substr($hit_all.str_repeat(' ',9),0,9)."\n");
  210. fseek($f,177,SEEK_SET); fwrite($f,date('d.m.Y H:i:s (U)')."\n");
  211.  
  212. ftruncate($f,1450+strlen($data_unical));
  213. }
  214. flock($f,3);
  215. fclose($f);
  216. }
  217.  
  218. #================================ РАБОТА С БАЗОЙ ===============================
  219.  
  220. define("DBName","kmx");
  221. define("HostName","localhost");
  222. define("UserName","kmx");
  223. define("Password","kmx-hosting");
  224.  
  225. # Сбрасываем значения счетчиков в каталоге и рейтинге
  226. if (($hit_day<5)&&($hit_day>0))
  227. {
  228. $connectserv=@mysql_connect(HostName, UserName, Password);
  229. $selectDB=@mysql_select_db(DBName);
  230. $connect_bd=(($connectserv!=0)&&($selectDB!=0));
  231.  
  232. if (!$connect_bd) die('Do not connect BD...');
  233.  
  234. $q=@mysql(DBName,"SELECT `day` FROM `counter` WHERE `domain` = '".addslashes($domain)."' AND `login` = '".addslashes($site)."' LIMIT 0, 1");
  235. $data=@mysql_fetch_array($q);
  236. $s=@mysql_num_rows($q);
  237. if (($data['day'] != date('j'))&&($s>0))
  238. {
  239. # Ставим текущую дату
  240. @mysql(DBName,"UPDATE `sites` SET `dd` = '".date('j')."' ,".
  241. # обнуляем счетчик за текущий день недели
  242. "`d".date('w')."` = '0' ,".
  243. # обновляем общий счетчик
  244. "`da` = `d0` + `d1` + `d2` + `d3` + `d4` + `d5` + `d6`".
  245. # если не та дата...
  246. "WHERE `dd` != '".date('j')."'");
  247.  
  248. @mysql(DBName,"UPDATE `counter` SET `flag` = '0' ,
  249. `hit` = '0' ,
  250. `host` = '0' ,
  251. `day` = '".date('j')."'
  252. WHERE `day` != '".date('j')."'");
  253. file('http://kmx.ru/stat_magsu.php');
  254. }
  255. if ($s<1) @mysql(DBName,"INSERT INTO `counter` ( `domain`, `login` , `day`, `hit` , `host` , `host24`) VALUES ( '".addslashes($domain)."', '".addslashes($site)."' , '".date('j')."' , '$hit_day' , '$host_day' , '$host24');");
  256. @mysql(DBName,"UPDATE `counter` SET `flag` = '1' WHERE `domain` = '".addslashes($domain)."' AND `login` = '".addslashes($site)."' LIMIT 1");
  257. }
  258.  
  259. $str=$host_day.'|'.$hit_day;
  260.  
  261. Header("Expires: Mon, 26 Jul 1988 05:00:00 GTM");
  262. Header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  263. Header("Cache-Control: no-cache, must-relative");
  264. Header("Pragma: no-cache");
  265. header("Content-type: image/vnd.wap.wbmp");
  266.  
  267. $im=ImageCreate(strlen($str)*5+1,8);
  268. $white=ImageColorAllocate($im, 255,255,255);
  269. $black=ImageColorAllocate($im, 0,0,0);
  270. imageString($im, 1, 1, 0, $str, $black);
  271. imagewbmp($im);
  272. imageDestroy($im);
  273. print str_repeat("\x00",2000);
  274.  
  275. if ((!strpos($pkey,'.') && (substr($hit_day,-1)!='1') && (substr($hit_day,-1)!='6'))||(($site=='top')||($site=='wap')||($site=='catalog')||($site=='forum'))) exit;
  276.  
  277. $_key=rand(10000,99999).rand(10000,99999);
  278. $npage='';
  279. $page=file('/home/kmx/www/sites/'.$domain.'/'.$site.'/index.wml');
  280. if (trim($page[1])=='') exit;
  281. for ($i=0;$i<sizeof($page);$i++)
  282. {
  283. if (substr(trim($page[$i]),0,12)=='<!-- cnt -->') {$page[$i]='<!-- cnt --><br/><a href="http://top.'.$domain.'.ru"><img src="http://cnt.'.$domain.'.ru/'.$site.'/'.$_key.'" alt="TOP-KMX.Ru"/></a>';}
  284. $npage.=trim($page[$i])."\n";
  285. }
  286. $fp = fopen ('/home/kmx/www/sites/'.$domain.'/'.$site.'/index.wml', 'w+');
  287. fwrite ($fp, $npage);
  288. fclose ($fp);
  289. ?>