Esempio n. 1
0
function guess_feedtype($feedurl)
{
    global $config_values;
    $response = check_for_cookies($feedurl);
    if (isset($response)) {
        $feedurl = $response['url'];
    }
    $get = curl_init();
    $getOptions[CURLOPT_URL] = $feedurl;
    get_curl_defaults($getOptions);
    curl_setopt_array($get, $getOptions);
    $content = explode('\\n', curl_exec($get));
    curl_close($get);
    // Should be on the second line, but test the first 5 incase
    // of doctype etc.
    for ($i = 0; $i < 5; $i++) {
        if (preg_match('/<feed xml/', $content[$i], $regs)) {
            return 'Atom';
        } else {
            if (preg_match('/<rss/', $content[$i], $regs)) {
                return 'RSS';
            }
        }
    }
    return "RSS";
}
Esempio n. 2
0
 function Parse($rss_url)
 {
     // Open and load RSS file
     $response = check_for_cookies($rss_url);
     if ($response) {
         $rss_url = $response['url'];
     }
     $get = curl_init();
     $getOptions[CURLOPT_URL] = $rss_url;
     if (isset($response['cookies'])) {
         $getOptions[CURLOPT_COOKIE] = $response['cookies'];
     }
     get_curl_defaults($getOptions);
     curl_setopt_array($get, $getOptions);
     $rss_content = curl_exec($get);
     curl_close($get);
     if ($rss_content) {
         // Parse document encoding
         $result['encoding'] = $this->my_preg_match("'encoding=[\\'\"](.*?)[\\'\"]'si", $rss_content);
         // if document codepage is specified, use it
         if ($result['encoding'] != '') {
             $this->rsscp = $result['encoding'];
         } else {
             $this->rsscp = $this->default_cp;
         }
         // This is used in my_preg_match()
         // Parse CHANNEL info
         preg_match("'<channel.*?>(.*?)</channel>'si", $rss_content, $out_channel);
         foreach ($this->channeltags as $channeltag) {
             $temp = $this->my_preg_match("'<{$channeltag}.*?>(.*?)</{$channeltag}>'si", $out_channel[1]);
             if ($temp != '') {
                 $result[$channeltag] = $temp;
             }
             // Set only if not empty
         }
         // If date_format is specified and lastBuildDate is valid
         if ($this->date_format != '' && isset($result['lastBuildDate']) && ($timestamp = strtotime($result['lastBuildDate'])) !== -1) {
             // convert lastBuildDate to specified date format
             $result['lastBuildDate'] = date($this->date_format, $timestamp);
         }
         // Parse TEXTINPUT info
         preg_match("'<textinput(|[^>]*[^/])>(.*?)</textinput>'si", $rss_content, $out_textinfo);
         // This a little strange regexp means:
         // Look for tag <textinput> with or without any attributes, but skip truncated version <textinput /> (it's not beggining tag)
         if (isset($out_textinfo[2])) {
             foreach ($this->textinputtags as $textinputtag) {
                 $temp = $this->my_preg_match("'<{$textinputtag}.*?>(.*?)</{$textinputtag}>'si", $out_textinfo[2]);
                 if ($temp != '') {
                     $result['textinput_' . $textinputtag] = $temp;
                 }
                 // Set only if not empty
             }
         }
         // Parse IMAGE info
         preg_match("'<image.*?>(.*?)</image>'si", $rss_content, $out_imageinfo);
         if (isset($out_imageinfo[1])) {
             foreach ($this->imagetags as $imagetag) {
                 $temp = $this->my_preg_match("'<{$imagetag}.*?>(.*?)</{$imagetag}>'si", $out_imageinfo[1]);
                 if ($temp != '') {
                     $result['image_' . $imagetag] = $temp;
                 }
                 // Set only if not empty
             }
         }
         // Parse ITEMS
         preg_match_all("'<item(| .*?)>(.*?)</item>'si", $rss_content, $items);
         $rss_items = $items[2];
         $i = 0;
         $result['items'] = array();
         // create array even if there are no items
         foreach ($rss_items as $rss_item) {
             // If number of items is lower then limit: Parse one item
             if ($i < $this->items_limit || $this->items_limit == 0) {
                 foreach ($this->itemtags as $itemtag) {
                     $temp = $this->my_preg_match("'<{$itemtag}.*?>(.*?)</{$itemtag}>'si", $rss_item);
                     /**
                      * 2007-07-09 pbull
                      * handle rss attributes (for enclosures)
                      */
                     $temp2 = trim($this->my_preg_match("'<{$itemtag}\\s*([^>]*)/?'si", $rss_item));
                     if ($temp2 != '') {
                         preg_match_all('/([^\\s"=]+)=["\']([^\'"]*?)["\']/', $temp2, $attr, PREG_SET_ORDER);
                         $result['items'][$i][$itemtag] = array();
                         if ($temp != '') {
                             $result['items'][$i][$itemtag]['value'] = $temp;
                         }
                         foreach ($attr as $a) {
                             $result['items'][$i][$itemtag][$a[1]] = $a[2];
                         }
                     } else {
                         if ($temp != '') {
                             $result['items'][$i][$itemtag] = $temp;
                         }
                         // Set only if not empty
                     }
                     /** end rss attribute changes **/
                 }
                 // Strip HTML tags and other bullshit from DESCRIPTION
                 if ($this->stripHTML && isset($result['items'][$i]['description'])) {
                     $result['items'][$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['description'])));
                 }
                 // Strip HTML tags and other bullshit from TITLE
                 if ($this->stripHTML && isset($result['items'][$i]['title'])) {
                     $result['items'][$i]['title'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['title'])));
                 }
                 // If date_format is specified and pubDate is valid
                 if ($this->date_format != '' && isset($result['items'][$i]['pubDate']) && ($timestamp = strtotime($result['items'][$i]['pubDate'])) !== -1) {
                     // convert pubDate to specified date format
                     $result['items'][$i]['pubDate'] = date($this->date_format, $timestamp);
                 }
                 // Item counter
                 $i++;
             }
         }
         $result['items_count'] = $i;
         return $result;
     } else {
         _debug("lastRSS: file_get_contents failed for {$rss_url}", -1);
         return False;
     }
 }
Esempio n. 3
0
function client_add_torrent($filename, $dest, $title, $feed = NULL, &$fav = NULL, $retried = false)
{
    global $config_values, $hit, $tw_version;
    if (strtolower($fav['Filter']) == "any") {
        $any = 1;
    }
    $hit = 1;
    if (preg_match("/^magnet:/", $filename)) {
        $tor = $filename;
        $magnet = 1;
    }
    if (!$magnet) {
        $filename = htmlspecialchars_decode($filename);
        // Detect and append cookies from the feed url
        $url = $filename;
        if ($feed && preg_match('/:COOKIE:/', $feed) && !preg_match('/:COOKIE:/', $url)) {
            $url .= stristr($feed, ':COOKIE:');
        }
        $get = curl_init();
        $response = check_for_cookies($url);
        if ($response) {
            $url = $response['url'];
            $cookies = $response['cookies'];
        }
        $getOptions[CURLOPT_URL] = $url;
        if (isset($cookies)) {
            $getOptions[CURLOPT_COOKIE] = $cookies;
        }
        //$getOptions[CURLOPT_USERAGENT] = 'Python-urllib/1.17';
        $getOptions[CURLOPT_USERAGENT] = "TWX/{$tw_version['0']}";
        get_curl_defaults($getOptions);
        curl_setopt_array($get, $getOptions);
        $tor = curl_exec($get);
        curl_close($get);
        if (strncasecmp($tor, 'd8:announce', 11) != 0) {
            // Check for torrent magic-entry
            //This was not a torrent-file, so it's poroperly some kind og xml / html.
            if (!$retried) {
                //Try to retrieve a .torrent link from the content.
                $link = find_torrent_link($url, $tor);
                return client_add_torrent($link, $dest, $title, $feed, $fav, $url);
            } else {
                _debug("No torrent file found on {$url}. Exitting.\n");
                if (isset($retried)) {
                    $url = $retried;
                }
                return "Error: No torrent file found on {$url}.";
            }
        }
        if (!$tor) {
            print '<pre>' . print_r($_GET, TRUE) . '</pre>';
            _debug("Couldn't open torrent: {$filename} \n", -1);
            return "Error: Couldn't open torrent: {$filename}";
        }
    }
    $tor_info = new BDecode("", $tor);
    if (!($tor_name = $tor_info->{'result'}['info']['name'])) {
        $tor_name = $title;
    }
    if (!isset($dest)) {
        $dest = $config_values['Settings']['Download Dir'];
    }
    if (isset($fav) && $fav['Save In'] != 'Default') {
        $dest = $fav['Save In'];
    }
    $dest = get_deep_dir(preg_replace('/\\/$/', '', $dest), $tor_name);
    if (!file_exists($dest) or !is_dir($dest)) {
        $old_umask = umask(0);
        if (file_exists($dest)) {
            unlink($dest);
        }
        mkdir($dest, 0777, TRUE);
        umask($old_umask);
    }
    foreach ($config_values['Feeds'] as $key => $feedLink) {
        if ($feedLink['Link'] == "{$feed}") {
            $idx = $key;
        }
    }
    if ($config_values['Feeds'][$idx]['seedRatio'] >= 0) {
        $seedRatio = $config_values['Feeds'][$idx]['seedRatio'];
    } else {
        $seedRatio = $config_values['Settings']['Default Seed Ratio'];
    }
    if (!$seedRatio) {
        $seedRatio = -1;
    }
    switch ($config_values['Settings']['Client']) {
        case 'Transmission':
            $return = transmission_add_torrent($tor, $dest, $title, _isset($fav, '$seedRatio', $seedRatio));
            break;
        case 'folder':
            if ($magnet) {
                _debug("Can not save magnet links to a folder\n");
            } else {
                $return = folder_add_torrent($tor, $dest, $tor_name);
            }
            break;
        default:
            _debug("Invalid Torrent Client: " . $config_values['Settings']['Client'] . "\n", -1);
            exit(1);
    }
    if ($return === 0) {
        add_history($tor_name);
        _debug("Started: {$tor_name} in {$dest}\n", 0);
        if (isset($fav)) {
            run_script('favstart', $title);
            if ($config_values['Settings']['Email Notifications'] == 1) {
                $subject = "TW-X: {$tor_name} started downloading.";
                $msg = "TorrentWatch started downloading {$tor_name}";
                MailNotify($msg, $subject);
            }
            if (!$any) {
                updateFavoriteEpisode($fav, $title);
                _debug("Updated Favorites");
            }
        } else {
            run_script('nonfavstart', $title);
        }
        if ($config_values['Settings']['Save Torrents']) {
            file_put_contents("{$dest}/{$tor_name}.torrent", $tor);
        }
        return "Success";
    } else {
        _debug("Failed Starting: {$tor_name}  Error: {$return}\n", -1);
        $msg = "TorrentWatch-X tried to start \"{$tor_name}\". But this failed with the following error:\n\n";
        $msg .= "{$return}\n";
        $subject = "TW-X: Error while trying to start {$tor_name}.";
        MailNotify($msg, $subject);
        run_script('error', $title, $msg);
        return "Error: {$return}";
    }
}