PageRenderTime 23ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/index.php

https://bitbucket.org/andrew_shadoura/ogregatyr
PHP | 630 lines | 479 code | 105 blank | 46 comment | 72 complexity | 6454e59285363cf2e3a388c3d5362536 MD5 | raw file
  1. <?php
  2. /***************************************************************************
  3. * Copyright (C) 2009-2012 by http://dizel-by.livejournal.com *
  4. * http://dizel-by.livejournal.com/47660.html *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 2 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program; if not, write to the *
  18. * Free Software Foundation, Inc., *
  19. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  20. ***************************************************************************/
  21. $versions = array(
  22. "2012-08-15" => "????????? ????? ????????, ???????? (thanks to Andrew Shadura), ????????? ????? ?????",
  23. "2012-06-30" => "??????? ??????? ?? ????????? ????? 4PX",
  24. "2012-05-21" => "????????? ????? ????????? ? USPS",
  25. "2012-03-01" => "RSS ? ???????? ??????. ????????? ??????????? :)",
  26. "2012-03-01" => "????????? ????????? ????",
  27. "2012-01-24" => "????????? ParcelForce ? ????? ???????",
  28. "2012-01-23" => "?????????? ????? ??????",
  29. "2011-03-15" => "?????????? ????? ?????",
  30. "2011-02-16" => "????????? ????? ?????",
  31. "2011-02-14" => "??????????? ????????? ???????? ??????",
  32. "2011-02-10" => "?????????? ????????",
  33. "2010-03-07" => "????????? EMS ?? HK post",
  34. "2010-03-06" => "????????? ?????????? ?????, ?????????? ? ?????? ????? ??????",
  35. "2010-03-05" => "?????????? ????????",
  36. "2010-02-20" => "?????????? ????????",
  37. "2009-07-08" => "???????? ??? ???? ??? ??????????? ????? (?????? ?? 3), ???????? ?????? HK post",
  38. "2009-06-16" => "?????? ??????",
  39. );
  40. error_reporting(0);
  41. set_time_limit(0);
  42. function showUpdates($url) {
  43. global $versions;
  44. echo @file_get_contents("http://dizel.homeip.net/post/index.php?version=".max(array_keys($versions)));
  45. }
  46. function compareDates($a1, $a2) {
  47. if ($a1['date'] < $a2['date']) return -1;
  48. if ($a1['date'] > $a2['date']) return 1;
  49. if ($a1['step'] < $a2['step']) return -1;
  50. if ($a1['step'] > $a2['step']) return 1;
  51. return 0;
  52. }
  53. function file_post_contents($url, $data, $headers = "") {
  54. $data = http_build_query($data);
  55. $context_options = array(
  56. "http" => array(
  57. "method" => "POST",
  58. "timeout" => 10,
  59. "header" => "Content-Type: application/x-www-form-urlencoded\r\nContent-Length: ". strlen($data). "\r\n" . $headers,
  60. "content" => $data
  61. )
  62. );
  63. $context = stream_context_create($context_options);
  64. return file_get_contents($url, false, $context);
  65. }
  66. function getUS_post($id) {
  67. if (substr($id, -2) != "US")
  68. return array();
  69. $html = file_get_contents("https://tools.usps.com/go/TrackConfirmAction_input?qtc_tLabels1={$id}&qtc_senddate1=&qtc_zipcode1=");
  70. if (!preg_match_all("~<div class=\"td-status\">(.+)</div>.+<div class=\"td-date-time\">(.+)</div>.+<div class=\"td-location\">(.+)</div>~Ums", $html, $tds))
  71. return array();
  72. foreach ($tds[1] as $k => $v)
  73. $data[] = array(
  74. "date" => date('Y-m-d H:i', strtotime(trim(strip_tags($tds[2][$k])))),
  75. "action" => trim(strip_tags($tds[1][$k])),
  76. "src" => trim(strip_tags($tds[3][$k])),
  77. );
  78. return $data;
  79. }
  80. function getSG_post($id) {
  81. if (substr($id, -2) != "SG")
  82. return array();
  83. $html = file_get_contents("http://en.4px.cc/Track.html?objTrackDto.hawbCode={$id}&outerservice=en");
  84. $html = str_replace("&nbsp;", "", $html);
  85. if (!preg_match_all("~<td class=\"track_right[56]\">(.+)</td>~Ums", $html, $tds))
  86. return array();
  87. for ($i = 0; $i < count($tds[1]); $i+= 4)
  88. $data[] = array(
  89. "date" => date('Y-m-d H:i:s', strtotime($tds[1][$i+0])),
  90. "src" => trim(strip_tags($tds[1][$i+2])),
  91. "action" => trim(strip_tags($tds[1][$i+3])),
  92. );
  93. return $data;
  94. }
  95. function getES_post($id) {
  96. if (substr($id, -2) != "ES")
  97. return array();
  98. $post = array("accion" => "LocalizaUno", "numero" => $id);
  99. $html = iconv("iso-8859-1", "utf-8", file_post_contents("http://www.correos.es/comun/Localizador/track.asp", $post));
  100. if (!preg_match('~<table id="Table2".+</table>~Ums', $html, $table))
  101. return array();
  102. preg_match_all("~<td[^>]*>(.*)</td>~Umsi", $table[0], $tds);
  103. $data = array();
  104. for($i = 3; $i < count($tds[1]); $i+=2)
  105. $data[] = array(
  106. "date" => date('Y-m-d', strtotime(str_replace("/", ".", $tds[1][$i]))),
  107. "action" => trim(strip_tags($tds[1][$i+1])),
  108. );
  109. return $data;
  110. }
  111. function getPF_post($id) {
  112. if (substr($id, -2) != "GB")
  113. return array();
  114. $html = file_get_contents("http://www.parcelforce.com/track-trace?trackNumber={$id}&page_type=parcel-tracking-details&parcel_number={$id}");
  115. if (!preg_match('~<table summary="Tracked parcel events">(.+)</table>~Ums', $html, $table))
  116. return array();
  117. if (!preg_match_all("~<td>(.*)</td>~Umsi", $table[1], $tds))
  118. return array();
  119. $data = array();
  120. for ($i = 0; $i < count($tds[0]); $i += 4) {
  121. $data[] = array(
  122. 'date' => date('Y-m-d H:i', strtotime($tds[1][$i+0].' '.$tds[1][$i+1])),
  123. 'src' => $tds[1][$i+2],
  124. 'action' => $tds[1][$i+3],
  125. );
  126. }
  127. return $data;
  128. }
  129. function getBG_post($id) {
  130. if (substr($id, -2) != "BG")
  131. return array();
  132. $html = file_get_contents("http://www.bgpost.bg/IPSWebTracking/IPSWeb_item_events.asp?itemid={$id}&Submit=Submit");
  133. if (!preg_match_all("~<tr class=tabl\d>.+</tr>~Ums", $html, $trs))
  134. return array();
  135. $data = array();
  136. foreach ($trs[0] as $tr) {
  137. if (!preg_match_all("~<td[^>]*>(.*)</td>~Ums", $tr, $tds))
  138. continue;
  139. $data[] = array(
  140. 'date' => date('Y-m-d H:i', strtotime($tds[1][0])),
  141. 'src' => $tds[1][1].' ('.$tds[1][2].')',
  142. 'action' => $tds[1][3],
  143. );
  144. }
  145. return $data;
  146. }
  147. /*
  148. function getCN_post($id) {
  149. if (substr($id, -2) != "CN")
  150. return array();
  151. $data = array();
  152. $html = file_post_contents("http://track-chinapost.com/tracking_chinapost.php", array("itemNo" => $id));
  153. if (preg_match_all("~<tr>(.+)</tr>~Umsi", $html, $tr)) {
  154. unset($tr[1][0]);
  155. foreach ($tr[1] as $t) {
  156. if (preg_match_all("~<td[^>]*>(.*)</td>~Umsi", $t, $td)) {
  157. $data[] = array(
  158. 'date' => $td[1][5],
  159. 'src' => $td[1][4],
  160. 'dst' => $td[1][3],
  161. 'action' => $td[1][2],
  162. );
  163. }
  164. }
  165. }
  166. return $data;
  167. }
  168. */
  169. function getHK_post($id) {
  170. if (substr($id, -2) != "HK")
  171. return array();
  172. $data = array();
  173. $html = file_get_contents("http://app3.hongkongpost.com/CGI/mt/genresult.jsp?tracknbr=".$id);
  174. if (preg_match("~(The item \(.+) (on|as of) (\d+\-\w+\-\d+?)~Umsi", $html, $o)) {
  175. $src = "";
  176. $dst = "";
  177. if (preg_match("~Destination</span> \- (.+)</p>~Umsi", $html, $d)) {
  178. $src = "Hong Kong";
  179. $dst = $d[1];
  180. }
  181. $date = date('Y-m-d H:i', strtotime(str_replace("-", " ", $o[3])));
  182. $data[] =
  183. array(
  184. "date" => $date,
  185. "src" => $src,
  186. "dst" => $dst,
  187. "action" => str_replace("({$id})", "", $o[1]),
  188. );
  189. }
  190. if (preg_match("~Full status~i", $html)) {
  191. if (!preg_match('~<!-- #BeginParcelDetail -->\s*<tr>\s*<td><a href="[^\"]+">[^<]+</a></td>\s*<td>(.+)</td>\s*<td>(.+)</td>~Umsi', $html, $o))
  192. return $data;
  193. list(,$src,$dst) = $o;
  194. $html = file_get_contents("http://app3.hongkongpost.com/CGI/mt/e_detail.jsp?mail_type=parcel_ouw&tracknbr={$id}&localno={$id}");
  195. if (!preg_match_all("~<table class=\"detail\">.+</table>~Umsi", $html, $o))
  196. return $data;
  197. $html = $o[0][1];
  198. preg_match_all("~<td>(.*)</td>~Umsi", $html, $o);
  199. for($i = 0; $i < count($o[1]); $i+=3) {
  200. $date = date('Y-m-d H:i', strtotime($o[1][$i]));
  201. $action = $o[1][$i+2];
  202. $data[] = array(
  203. "date" => $date,
  204. "src" => $src,
  205. "dst" => $dst,
  206. "action" => $action,
  207. "step" => $i,
  208. );
  209. }
  210. }
  211. return $data;
  212. }
  213. function getRU_post($id) {
  214. $url = "http://russianpost.ru/resp_engine.aspx?Path=rp/servise/ru/home/postuslug/trackingpo";
  215. $data = array(
  216. "searchsign" => 1,
  217. "BarCode" => $id,
  218. "entryBarCode" => '',
  219. );
  220. $html = file_post_contents($url, $data);
  221. $html = str_replace("??????????", "????????", $html);
  222. $data = array();
  223. if (preg_match_all("~<tr align=\"center\">(.+)</TR>~Umsi", $html, $tr)) {
  224. $tr = $tr[1];
  225. foreach ($tr as $i => $t) {
  226. if (!preg_match_all("~<TD[^>]*>(.*)</TD>~Umsi", $t, $tds))
  227. continue;
  228. $src = trim(str_replace("&nbsp;", " ", $tds[1][3]));
  229. $date = date('Y-m-d H:i', strtotime($tds[1][1]));
  230. $action = array();
  231. if (trim(str_replace("&nbsp;", " " ,$tds[1][0]))) $action[] = $tds[1][0];
  232. if (trim(str_replace("&nbsp;", " " ,$tds[1][4]))) $action[] = $tds[1][4];
  233. $action = implode(" - ", $action);
  234. $data[] = array(
  235. "date" => $date,
  236. "src" => $src,
  237. "dst" => $dst,
  238. "action" => $action,
  239. "step" => $i,
  240. );
  241. }
  242. }
  243. return $data;
  244. }
  245. function getBY_post($id) {
  246. $url = "http://search.belpost.by/ajax/search/";
  247. $html = file_post_contents($url, array("internal" => "2", "item" => $id), "X-Requested-With: XMLHttpRequest");
  248. $data = array();
  249. $step = 0;
  250. preg_match_all("~<table[^>]*>.+</table>~Ums", $html, $tables);
  251. foreach ($tables[0] as $html) {
  252. preg_match_all("~<td class=\"theader\">~Ums", $html, $headers);
  253. if (preg_match_all("~<td>(.*)</td>~Umsi", $html, $tds)) {
  254. if (count($headers[0]) == 7) {
  255. for ($i = 0; $i < count($tds[1]); $i+=5) {
  256. $date = date('Y-m-d H:i', strtotime($tds[1][$i+0]));
  257. $src = $tds[1][$i+2];
  258. $dst = '';
  259. $action = $tds[1][$i+1];
  260. $data[] = array(
  261. "date" => $date,
  262. "src" => $src,
  263. "dst" => $dst,
  264. "action" => $action,
  265. "step" => $step,
  266. );
  267. $step++;
  268. }
  269. }
  270. if (count($headers[0]) == 2) {
  271. for ($i = 0; $i < count($tds[1]); $i+=2) {
  272. $date = date('Y-m-d H:i', strtotime(preg_replace("~^(\d+)\.(\d+)\.(\d+) (\d+):(\d+):(\d+)$~", "\\3-\\2-\\1 \\4:\\5:\\6", $tds[1][$i])));
  273. $action = preg_replace("~^\d{2}\. ~", "", $tds[1][$i+1]);
  274. $src = $dst = "";
  275. if (preg_match("~?? \(.+\) (.+) ? \(.+\) (.+)$~", $action, $p)) {
  276. $src = $p[1]; $dst = $p[2];
  277. }
  278. $data[] = array(
  279. "date" => $date,
  280. "src" => $src,
  281. "dst" => $dst,
  282. "action" => $action,
  283. "step" => $step,
  284. );
  285. $step++;
  286. }
  287. }
  288. }
  289. }
  290. return $data;
  291. }
  292. function getFunctions() {
  293. $result = array();
  294. $tmp = get_defined_functions();
  295. foreach ($tmp['user'] as $f)
  296. if (preg_match("~^get(.+)_post$~", $f))
  297. $result[] = $f;
  298. return $result;
  299. }
  300. if ($_GET['version']) {
  301. $news = "";
  302. foreach ($versions as $v => $title) {
  303. if ($v > $_GET['version'])
  304. $news .= "<li><b>".date('d.m.Y', strtotime($v))."</b><br/>{$title}</li>";
  305. }
  306. if ($news)
  307. $news = "<h1 style='display: inline'>???????? ??????????</h1> (<a href='http://dizel.homeip.net/post/index.php.txt'>???????</a>)<ul>".$news."</ul>";
  308. echo $news;exit;
  309. }
  310. function getSK_post($id) {
  311. $url = "http://tandt.posta.sk/en?q=";
  312. $html = file_get_contents($url . $id);
  313. $data = array();
  314. $step = 0;
  315. preg_match_all("~<ul class=\"result-item\">.+</ul>~Ums", $html, $ul);
  316. foreach ($ul[0] as $html) {
  317. if (preg_match_all("~<li class=\"event\">(.*)</li>~Umsi", $html, $lis)) {
  318. for ($i = 0; $i < count($lis[1]); $i++) {
  319. preg_match("~<span class=\"event-day\">(.*)</span>~Umsi", $lis[0][$i], $date);
  320. $date = date('Y-m-d H:i', strtotime(preg_replace("~<.*>~", "", $date[1])));
  321. $src = '';
  322. $dst = '';
  323. preg_match("~<span class=\"event-name\">(.*)<span~Umsi", $lis[0][$i], $name);
  324. preg_match("~<span class=\"event-note\">(.*)</span~Umsi", $lis[0][$i], $note);
  325. $action = $name[1].$note[1];
  326. $data[] = array(
  327. "date" => $date,
  328. "src" => $src,
  329. "dst" => $dst,
  330. "action" => $action,
  331. "step" => $i,
  332. );
  333. }
  334. }
  335. }
  336. return $data;
  337. }
  338. $functions = getFunctions();
  339. $ch = array();
  340. if ($_GET['action'] == "part") {
  341. $part = strtolower($_GET['part']);
  342. $row = $_GET['id'];
  343. if (!preg_match("~[0-9A-z]+~", $row, $id))
  344. $data = array();
  345. else {
  346. $id = $id[0];
  347. if (array_search($part, $functions) !== false) {
  348. $data = array('id' => $row, "data" => call_user_func($part, $id));
  349. }
  350. }
  351. echo serialize($data);exit;
  352. }
  353. $multi = function_exists("curl_multi_add_handle");
  354. //$multi = 0;
  355. $i = 0;
  356. $url = "http://".$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];
  357. $time = microtime(true);
  358. foreach (explode("\n", $_GET['id']) as $k => $row) {
  359. $row = trim($row);
  360. if (!preg_match("~[0-9A-z]+~", $row, $id))
  361. continue;
  362. $id = $id[0];
  363. $data[$row] = array();
  364. foreach ($functions as $f) {
  365. if (!$multi) {
  366. $t = time();
  367. if ($tmp = call_user_func($f, $id))
  368. $data[$row] = array_merge($data[$row], $tmp);
  369. // echo $f.' - '.(time()-$t)."<br>\n";flush();
  370. }
  371. else {
  372. $ch[$i] = curl_init();
  373. curl_setopt($ch[$i], CURLOPT_URL, $url."?action=part&part={$f}&id=".urlencode($row));
  374. curl_setopt($ch[$i], CURLOPT_HEADER, 0);
  375. curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
  376. $i++;
  377. }
  378. }
  379. }
  380. if ($multi) {
  381. $mh = curl_multi_init();
  382. foreach ($ch as &$c)
  383. curl_multi_add_handle($mh, $c);
  384. $running=null;
  385. do {
  386. usleep(50);
  387. curl_multi_exec($mh, $running);
  388. } while ($running > 0);
  389. foreach ($ch as &$c) {
  390. $tmp = unserialize(curl_multi_getcontent($c));
  391. $data[$tmp['id']] = array_merge($data[$tmp['id']], $tmp['data']);
  392. curl_multi_remove_handle($mh, $c);
  393. }
  394. curl_multi_close($mh);
  395. }
  396. $time = microtime(true)-$time;
  397. foreach ($data as &$d)
  398. usort($d, 'compareDates');
  399. if ($_GET['output'] == "serialized") {
  400. echo serialize($data);
  401. exit;
  402. }
  403. if ($_GET['output'] == "rss") {
  404. header("Content-type: application/rss+xml");
  405. $ids = implode(", ", array_keys($data));
  406. ?>
  407. <?php echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; ?>
  408. <rss version="2.0">
  409. <channel>
  410. <link>http://<?=$_SERVER['SERVER_NAME'].htmlspecialchars($_SERVER['REQUEST_URI']);?></link>
  411. <title><![CDATA[ ??????????: <?=$ids?>]]></title>
  412. <description></description>
  413. <?php
  414. foreach ($data as $id => $rows) {
  415. foreach ($rows as $row) {
  416. $desc = $row['date']."<br/>";
  417. if ($row['src']) $desc .= "{$row['src']}";
  418. if ($row['dst']) $desc .= "-> {$row['dst']}";
  419. if ($row['action']) $desc .= "<br/>".strip_tags($row['action']);
  420. $desc = str_replace("<br/><br/>", "<br/>", $desc);
  421. echo "<item>";
  422. echo "<title><![CDATA[\n{$id}: {$row['action']}\n]]></title>";
  423. echo "<description><![CDATA[\n{$desc}\n]]></description>";
  424. echo "<guid isPermaLink=\"false\">track{$id}</guid>";
  425. echo "<pubDate>{$row['date']}</pubDate>";
  426. echo "</item>";
  427. }
  428. }
  429. ?>
  430. </channel>
  431. </rss>
  432. <?php
  433. exit;
  434. }
  435. $id = $_GET['id'];
  436. ?>
  437. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  438. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  439. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru">
  440. <head>
  441. <title>??????????</title>
  442. <link href="http://<?=$_SERVER['SERVER_NAME'].htmlspecialchars($_SERVER['REQUEST_URI'])?>&amp;output=rss" rel="alternate" type="application/rss+xml" title="RSS" />
  443. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  444. <style type="text/css">
  445. * {
  446. font-size: 12px;
  447. font-family: tahoma, sans-serif;
  448. }
  449. h1 {
  450. font-size: 13px;
  451. margin-bottom: 0px;
  452. }
  453. ul {
  454. padding: 0px;
  455. }
  456. .updates {
  457. float: right;
  458. width: 400px;
  459. font-size: 11px;
  460. }
  461. table.dataout {
  462. border-color: #000;
  463. border-width: 0 0 1px 1px;
  464. border-style: solid;
  465. border-collapse: collapse;
  466. margin-top: 3px;
  467. }
  468. table.dataout td
  469. {
  470. border-color: #000;
  471. border-width: 1px 1px 0 0;
  472. border-style: solid;
  473. margin: 0;
  474. padding: 4px;
  475. }
  476. table.dataout th
  477. {
  478. border-color: #000;
  479. border-width: 1px 1px 0 0;
  480. border-style: solid;
  481. margin: 0;
  482. padding: 4px;
  483. background-color: #999;
  484. }
  485. </style>
  486. </head>
  487. <body>
  488. <div class="updates"><div style="text-align: right"><a href="mailto:dizel@tut.by">????????? ? ???????</a></div></div>
  489. <?php if (!$_GET) : ?>
  490. <div class="updates"><?=showUpdates()?></div>
  491. <?php endif; ?>
  492. <form method="get" action="index.php">
  493. ???? ??????????? (? ????????????? ????? ??????):<br/>
  494. <textarea name="id" cols="50" rows="5"><?=htmlspecialchars($id)?></textarea><br/>
  495. <input type="submit" value="?????" />
  496. </form>
  497. <?php if ($id) : ?>
  498. <?php if ($data) : ?>
  499. <?php foreach ($data as $title => $rows) : ?>
  500. <h1><?=$title?></h1>
  501. <table class="dataout">
  502. <tr>
  503. <th>????/?????</th>
  504. <th>??????</th>
  505. <th>????</th>
  506. <th>????????</th>
  507. </tr>
  508. <?php foreach ($rows as $row) : ?>
  509. <tr>
  510. <td><?=str_replace(" 00:00", "", $row['date'])?></td>
  511. <td><?=$row['src']?></td>
  512. <td><?=$row['dst']?></td>
  513. <td><?=$row['action']?></td>
  514. </tr>
  515. <?php endforeach; ?>
  516. </table>
  517. <?php endforeach; ?>
  518. <?php endif; ?>
  519. <?php if (!$data) echo "?????? ?? ???????"; ?>
  520. <?php endif; ?>
  521. <font color="white"><?=$time?></font>
  522. </body>