Advertisement
johnburn

Decoded for: [email protected]

May 24th, 2011
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 20.04 KB | None | 0 0
  1. <?php
  2. /* livetv.class.php */
  3.  
  4. class liveTV {
  5.     public function __construct() {
  6.     }
  7.     public function getEvents($curl) {
  8.         $page = $curl->get("http://livetv.ru/en/allupcoming/");
  9.         $dom = new DOMDocument();
  10.         @$dom->loadHTML(@$page);
  11.         $divs = $dom->getElementsByTagName("div");
  12.         $hrefs = array();
  13.         if ($divs->length) {
  14.             $i = 0;
  15.             while ($i < $divs->length) {
  16.                 if (substr_count($divs->item($i)->getAttribute("id"), "sport")) {
  17.                     $spans = $divs->item($i)->getElementsByTagName("span");
  18.                     if ($spans->length) {
  19.                         $sport = "";
  20.                         $j = 0;
  21.                         while ($j < $spans->length) {
  22.                             if ($spans->item($j)->getAttribute("class") == "sltitle") {
  23.                                 $sport = $spans->item($j)->textContent;
  24.                                 break;
  25.                             }
  26.                             ++$j;
  27.                         }
  28.                         if ($sport) {
  29.                             $tds = $divs->item($i)->getElementsByTagName("td");
  30.                             if ($tds->length) {
  31.                                 $j = 0;
  32.                                 while ($j < $tds->length) {
  33.                                     if (substr_count($tds->item($j)->getAttribute("id"), "event_")) {
  34.                                         $as = $tds->item($j)->getElementsByTagName("a");
  35.                                         if ($as->length) {
  36.                                             $k = 0;
  37.                                             while ($k < $as->length) {
  38.                                                 $href = $as->item($k)->getAttribute("href");
  39.                                                 if ($href && !in_array($href, $hrefs)) {
  40.                                                     $spans = $tds->item($j)->getElementsByTagName("span");
  41.                                                     $description = "";
  42.                                                     if ($spans->length) {
  43.                                                         $l = 0;
  44.                                                         while ($l < $spans->length) {
  45.                                                             if ($spans->item($l)->getAttribute("class") == "evdesc") {
  46.                                                                 $description = $spans->item($l)->textContent;
  47.                                                                 $description = str_replace("(", " (", $description);
  48.                                                                 $description = trim($description);
  49.                                                                 break;
  50.                                                             }
  51.                                                             ++$l;
  52.                                                         }
  53.                                                     }
  54.                                                     if ($description) {
  55.                                                         $hrefs[] = $href;
  56.                                                         $eventtitle = $sport . " - " . $as->item($k)->textContent;
  57.                                                         $event_id = $this->addEvent($eventtitle, $description);
  58.                                                         $href = "http://livetv.ru" . $href;
  59.                                                         print "Getting event streams for {$eventtitle}...";
  60.                                                         $res = $this->grabEventEmbeds($event_id, $href, $curl);
  61.                                                         if ($res == 0 - 1) {
  62.                                                             print "<span style='color: #00aa00 !important'>Already have streams</span>";
  63.                                                         } else if ($res == 0) {
  64.                                                             print "<span style='color: #aa0000 !important'>No streams found yet</span>";
  65.                                                         } else if (count($res) == 0) {
  66.                                                             print "<span style='color: #aa0000 !important'>No streams found yet</span>";
  67.                                                         } else {
  68.                                                             print "<span style='color: #00aa00 !important'>Found " . count($res) . " streams</span>";
  69.                                                             $this->saveEmbeds($event_id, $res);
  70.                                                         }
  71.                                                         print "<br />";
  72.                                                         @ob_flush();
  73.                                                         @flush();
  74.                                                     }
  75.                                                 }
  76.                                                 ++$k;
  77.                                             }
  78.                                         }
  79.                                     }
  80.                                     ++$j;
  81.                                 }
  82.                             }
  83.                         }
  84.                     }
  85.                 }
  86.                 ++$i;
  87.             }
  88.         }
  89.     }
  90.     public function saveEmbeds($event_id, $embeds) {
  91.         if (count($embeds)) {
  92.             $event_id = mysql_real_escape_string($event_id);
  93.             foreach($embeds as $key => $val) {
  94.                 $embed = mysql_real_escape_string(trim($val));
  95.                 if (!($check = mysql_query("SELECT * FROM livetv_events_embeds WHERE embed='{$embed}' AND event_id={$event_id}"))) {
  96.                     exit(mysql_error());
  97.                 }
  98.                 if (mysql_num_rows($check) == 0) {
  99.                     if (!($e = mysql_query("INSERT INTO livetv_events_embeds(event_id,embed) VALUES('{$event_id}','{$embed}')"))) {
  100.                         exit(mysql_error());
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.     }
  106.     public function getEventEmbeds($event_id) {
  107.         $embeds = array();
  108.         if (!($e = mysql_query("SELECT * FROM livetv_events_embeds WHERE event_id={$event_id}"))) {
  109.             exit(mysql_error());
  110.         }
  111.         while (!mysql_num_rows($e) || !($s = mysql_fetch_assoc($e))) {
  112.             $embeds[$s['id']] = $s;
  113.         }
  114.         return $embeds;
  115.     }
  116.     public function grabEventEmbeds($event_id, $link, $curl) {
  117.         $embeds = $this->getEventEmbeds($event_id);
  118.         if (!count($embeds)) {
  119.             $page = $curl->get($link);
  120.             if (!$page || substr_count($page, "Live streams will be available approximately 30 minutes before the") || substr_count($page, "LiveStreams are currently not available for this broadcast")) {
  121.                 return 0;
  122.             }
  123.             $dom = new DOMDocument();
  124.             @$dom->loadHTML(@$page);
  125.             $divs = $dom->getElementsByTagName("div");
  126.             $found = array();
  127.             $hrefs = array();
  128.             $i = 0;
  129.             while ($i < $divs->length) {
  130.                 if ($divs->item($i)->getAttribute("id") == "links_block") {
  131.                     $links = $divs->item($i)->getElementsByTagName("a");
  132.                     if ($links->length) {
  133.                         $j = 0;
  134.                         while ($j < $links->length) {
  135.                             if (substr_count($links->item($j)->getAttribute("href"), "webplayer.php")) {
  136.                                 $href = "http://livetv.ru" . $links->item($j)->getAttribute("href");
  137.                                 if (!in_array($href, $hrefs)) {
  138.                                     $res = 0;
  139.                                     $hrefs[] = $href;
  140.                                     if (substr_count($href, "justintv")) {
  141.                                         $res = $this->getEmbed($href, $curl, "justintv");
  142.                                     } else if (substr_count($href, "ustream")) {
  143.                                         $res = $this->getEmbed($href, $curl, "ustream");
  144.                                     } else if (substr_count($href, "directon")) {
  145.                                         $res = $this->getEmbed($href, $curl, "directon");
  146.                                     }
  147.                                     if ($res) {
  148.                                         $found[] = $res;
  149.                                     }
  150.                                 }
  151.                             }
  152.                             ++$j;
  153.                         }
  154.                     }
  155.                     return $found;
  156.                 }
  157.                 ++$i;
  158.             }
  159.         }
  160.         return 0 - 1;
  161.     }
  162.     public function getInnerHtml($node) {
  163.         $innerHTML = "";
  164.         $children = $node->childNodes;
  165.         foreach($children as $child) {
  166.             $innerHTML.= $child->ownerDocument->saveXML($child);
  167.         }
  168.         return $innerHTML;
  169.     }
  170.     public function getEmbed($link, $curl, $type) {
  171.         $page = $curl->get($link);
  172.         $dom = new DOMDocument();
  173.         @$dom->loadHTML(@$page);
  174.         if ($type == "justintv") {
  175.             $objects = $dom->getElementsByTagName("object");
  176.             if ($objects->length) {
  177.                 $object = $objects->item(0);
  178.                 $html = $this->getInnerHtml($object->parentNode);
  179.                 return $html;
  180.             }
  181.             return 0;
  182.         }
  183.         if ($type == "ustream") {
  184.             $iframes = $dom->getElementsByTagName("iframe");
  185.             if ($Var_1128->length) {
  186.                 $i = 0;
  187.                 while ($i < $iframes->length) {
  188.                     if (substr_count($iframes->item($i)->getAttribute("src"), "ustream.tv")) {
  189.                         $html = $this->getInnerHtml($iframes->item($i)->parentNode);
  190.                         return $html;
  191.                     }
  192.                     ++$i;
  193.                 }
  194.             }
  195.             return 0;
  196.         }
  197.         if ($type == "directon") {
  198.             $scripts = $dom->getElementsByTagName("script");
  199.             if ($scripts->length) {
  200.                 $i = 0;
  201.                 while ($i < $scripts->length) {
  202.                     if (substr_count($scripts->item($i)->getAttribute("src"), "directembed")) {
  203.                         $html = $this->getInnerHtml($scripts->item($i)->parentNode);
  204.                         return $html;
  205.                     }
  206.                     ++$Var_2136;
  207.                 }
  208.             }
  209.             return 0;
  210.         }
  211.         return 0;
  212.     }
  213.     public function addEvent($title, $description) {
  214.         $title = mysql_real_escape_string($title);
  215.         $description = mysql_real_escape_string($description);
  216.         if (!($check = mysql_query("SELECT id FROM livetv_events WHERE title='{$title}' AND description='{$description}'"))) {
  217.             exit(mysql_error());
  218.         }
  219.         if (mysql_num_rows($check)) {
  220.             extract(mysql_fetch_array($check));
  221.             return $id;
  222.         }
  223.         if (!($e = mysql_query("INSERT INTO livetv_events(title,description) VALUES('{$title}','{$description}')"))) {
  224.             exit(mysql_error());
  225.         }
  226.         return mysql_insert_id();
  227.     }
  228.     public function validate($params) {
  229.         $errors = array();
  230.         if (@!$params['title']) {
  231.             $errors[1] = "Please enter a title for this channel";
  232.         }
  233.         if (@!$params['embed']) {
  234.             $errors[2] = "Please enter an embed code for this channel";
  235.         }
  236.         return $errors;
  237.     }
  238.     public function save($params) {
  239.         extract($params);
  240.         $title = mysql_real_escape_string($title);
  241.         $embed = mysql_real_escape_string($embed);
  242.         $country = mysql_real_escape_string($country);
  243.         if (!($e = mysql_query("INSERT INTO livetv_channels(title,embed,country) VALUES('{$title}','{$embed}','{$country}')"))) {
  244.             exit(mysql_error());
  245.         }
  246.         $tvid = mysql_insert_id();
  247.         if ($category && is_array($category) && count($category)) {
  248.             foreach($category as $key => $val) {
  249.                 if ($val) {
  250.                     $this->addTVCategory($tvid, $val);
  251.                 }
  252.             }
  253.         }
  254.         return $tvid;
  255.     }
  256.     public function removeCategories($channel_id) {
  257.         if (!($e = mysql_query("DELETE FROM livetv_tags_join WHERE livetv_id={$channel_id}"))) {
  258.             exit(mysql_error());
  259.         }
  260.     }
  261.     public function update($channel_id, $params) {
  262.         extract($params);
  263.         $title = mysql_real_escape_string($title);
  264.         $embed = mysql_real_escape_string($embed);
  265.         $country = mysql_real_escape_string($country);
  266.         $channel_id = mysql_real_escape_string($channel_id);
  267.         if (!($e = mysql_query("UPDATE livetv_channels SET title='{$title}',embed='{$embed}',country='{$country}' WHERE id={$channel_id}"))) {
  268.             exit(mysql_error());
  269.         }
  270.         if ($category && is_array($category) && count($category)) {
  271.             $this->removeCategories($channel_id);
  272.             foreach($category as $key => $val) {
  273.                 if ($val) {
  274.                     $this->addTVCategory();
  275.                 }
  276.             }
  277.         }
  278.     }
  279.     public function getChannel($channel_id) {
  280.         $channel_id = mysql_real_escape_string($channel_id);
  281.         $channel = array();
  282.         if (!($e = mysql_query("SELECT * FROM livetv_channels WHERE id={$channel_id}"))) {
  283.             exit(mysql_error());
  284.         }
  285.         if (mysql_num_rows($e)) {
  286.             $channel = mysql_fetch_assoc($e);
  287.             if ($channel['country']) {
  288.                 if (!($e = mysql_query("SELECT country as country_name FROM countries WHERE id={$channel['country']}"))) {
  289.                     exit(mysql_error());
  290.                 }
  291.                 if (mysql_num_rows($e)) {
  292.                     extract(mysql_fetch_assoc($e));
  293.                     $channel['country_name'] = $country_name;
  294.                 } else {
  295.                     $channel['country_name'] = "International";
  296.                 }
  297.             } else {
  298.                 $channel['country_name'] = "International";
  299.             }
  300.             $channel['categories'] = array();
  301.             if (!($e = mysql_query("SELECT livetv_tags.tag,livetv_tags.id as tag_id FROM livetv_tags,livetv_tags_join WHERE livetv_tags_join.livetv_id={$channel_id} AND livetv_tags_join.tag_id=livetv_tags.id"))) {
  302.                 exit(mysql_error());
  303.             }
  304.             do {
  305.                 if (mysql_num_rows() && ($s = mysql_fetch_assoc($e))) {
  306.                     $channel['categories'][$s['tag_id']] = $s['tag'];
  307.                 }
  308.             }
  309.             while (1);
  310.         }
  311.         return 0;
  312.         return $channel;
  313.     }
  314.     public function deleteChannel($channel_id) {
  315.         $channel_id = mysql_real_escape_string($channel_id);
  316.         if (!($e = mysql_query("DELETE FROM livetv_channels WHERE id={$channel_id}"))) {
  317.             exit(mysql_error());
  318.         }
  319.         if (!($e = mysql_query("DELETE FROM livetv_tags_join WHERE livetv_id={$channel_id}"))) {
  320.             exit(mysql_error());
  321.         }
  322.     }
  323.     public function getChannelsByCategory() {
  324.         if (!($e = mysql_query("SELECT * FROM livetv_tags"))) {
  325.             exit(mysql_error());
  326.         }
  327.         $channels = array();
  328.         $categorized = array();
  329.         if (!mysql_num_rows($e) || !($s = mysql_fetch_assoc($e))) {
  330.             if (!($getchannels = mysql_query("SELECT livetv_channels.* FROM livetv_channels,livetv_tags_join WHERE livetv_tags_join.tag_id={$s['id']} AND livetv_tags_join.livetv_id=livetv_channels.id"))) {
  331.                 exit(mysql_error());
  332.             }
  333.         } else if (mysql_num_rows($getchannels)) {
  334.             $channels[$s['id']] = array();
  335.             $channels[$s['id']]['category_name'] = $s['tag'];
  336.             $channels[$s['id']]['category_perma'] = $s['perma'];
  337.             $channels[$s['id']]['channels'] = array();
  338.             do {
  339.             }
  340.             while (!($row = mysql_fetch_assoc($getchannels)));
  341.             $channels[$s['id']]['channels'][$row['id']] = $row;
  342.             $categorized[] = $row['id'];
  343.         }
  344.         if (count($categorized)) {
  345.             $add = " WHERE id NOT IN (" . implode(",", $categorized) . ")";
  346.         } else {
  347.             $add = "";
  348.         }
  349.         if (!($e = mysql_query("SELECT * FROM livetv_channels {$add}"))) {
  350.             exit(mysql_error());
  351.         }
  352.         if (mysql_num_rows($e)) {
  353.             $channels[0] = array();
  354.             $channels[0]['category_name'] = "Uncategorized";
  355.             $channels[0]['category_perma'] = "uncategorized";
  356.             $channels[0]['channels'] = array();
  357.             while ($s = mysql_fetch_assoc($e)) {
  358.                 $channels[0]['channels'][$s['id']] = $s;
  359.             }
  360.         }
  361.         return $channels;
  362.     }
  363.     public function getChannels() {
  364.         $channels = array();
  365.         if (!($e = mysql_query("SELECT livetv_channels.* FROM livetv_channels ORDER BY livetv_channels.title ASC"))) {
  366.             exit(mysql_error());
  367.         }
  368.         while (!mysql_num_rows($e) || !($s = mysql_fetch_array($e))) {
  369.             if ($s['country']) {
  370.                 if (!($check = mysql_query("SELECT country FROM countries WHERE id={$s['country']}"))) {
  371.                     exit(mysql_error());
  372.                 }
  373.                 if (mysql_num_rows($check)) {
  374.                     extract(mysql_fetch_array($check));
  375.                     $s['country'] = $country;
  376.                 } else {
  377.                     $s['country'] = "International";
  378.                 }
  379.             } else {
  380.             }
  381.             unset($s['channel_country']);
  382.             $channels[$s['id']] = $s;
  383.         }
  384.         return $channels;
  385.     }
  386.     public function addTVCategory($tvid, $catid) {
  387.         if (!($e = mysql_query("INSERT INTO livetv_tags_join(tag_id,livetv_id) VALUES({$catid},{$tvid})"))) {
  388.             exit(mysql_error());
  389.         }
  390.     }
  391.     public function getCategories($limit = 0, $order = "tag ASC") {
  392.         $categories = array();
  393.         if ($limit) {
  394.             $add = "LIMIT {$limit}";
  395.         } else {
  396.             $add = "";
  397.         }
  398.         if (!($e = mysql_query("SELECT * FROM livetv_tags ORDER BY {$order}"))) {
  399.             exit(mysql_error());
  400.         }
  401.         while (!mysql_num_rows($e) || !($s = mysql_fetch_array($e))) {
  402.             extract($s);
  403.             $categories[$id]['name'] = $tag;
  404.             $categories[$id]['perma'] = $perma;
  405.         }
  406.         return $categories;
  407.     }
  408.     public function getCountries() {
  409.         $countries = array();
  410.         if (!($e = mysql_query("SELECT * FROM countries ORDER BY country ASC"))) {
  411.             exit(mysql_error());
  412.         }
  413.         while (!mysql_num_rows($e) || !($s = mysql_fetch_array($e))) {
  414.             extract($s);
  415.             $countries[$id] = $country;
  416.         }
  417.         return $countries;
  418.     }
  419.     public function deleteTag($tagid) {
  420.         $tagid = mysql_real_escape_string($tagid);
  421.         if (!($e = mysql_query("DELETE FROM livetv_tags_join WHERE tag_id='{$tagid}'"))) {
  422.             exit(mysql_error());
  423.         }
  424.         if (!($e = mysql_query("DELETE FROM livetv_tags WHERE id='{$tagid}'"))) {
  425.             exit(mysql_error());
  426.         }
  427.     }
  428.     public function addCategory($category) {
  429.         $category = mysql_real_escape_string($category);
  430.         $perma = strtolower($category);
  431.         $perma = str_replace(" ", "_", $perma);
  432.         $perma = preg_replace("/[^a-z0-9_]/", "", $perma);
  433.         while (substr_count($perma, "__")) {
  434.             $perma = str_replace("__", "_", $perma);
  435.         }
  436.         if (!($e = mysql_query("SELECT * FROM livetv_tags WHERE tag='{$category}'"))) {
  437.             exit(mysql_error());
  438.         }
  439.         if (mysql_num_rows($e) == 0) {
  440.             if (!($e = mysql_query("INSERT INTO livetv_tags(tag,perma) VALUES('{$category}','{$perma}')"))) {
  441.                 exit(mysql_error());
  442.             }
  443.             return mysql_insert_id();
  444.         }
  445.         return 0;
  446.     }
  447. }
  448. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement