Example #1
0
function run_script($param, $torrent, $error = "")
{
    global $config_values;
    $torrent = escapeshellarg($torrent);
    $error = escapeshellarg($error);
    $script = $config_values['Settings']['Script'];
    if ($script) {
        if (!is_file($script)) {
            $msg = "The configured script is not a single file. Parameters are not allowed because of security reasons.";
            $subject = "TW-X: security error";
            MailNotify($msg, $subject);
            return;
        }
        _debug("Running {$script} {$param} {$torrent} {$error} \n", -1);
        exec("{$script} {$param} {$torrent} {$error} 2>&1", $response, $return);
        if ($return && $config_values['Settings']['Email Address']) {
            $msg = "Something went wrong while running {$script}:\n";
            foreach ($response as $line) {
                $msg .= $line . "\n";
            }
            $msg .= "\n";
            $msg .= "Please read 'https://code.google.com/p/torrentwatch-x/wiki/Script' for more info about how to make a compatible script.";
            _debug("{$msg}\n");
            $subject = "TW-X: {$script} returned error.";
            MailNotify($msg, $subject);
        }
    }
}
Example #2
0
function updateFavoriteEpisode(&$fav, $title)
{
    global $config_values;
    if (!($guess = guess_match($title, TRUE))) {
        return;
    }
    if (preg_match('/^((\\d+x)?\\d+)p$/', $guess['episode'])) {
        $guess['episode'] = preg_replace('/^((?:\\d+x)?\\d+)p$/', '\\1', $guess['episode']);
        $PROPER = "p";
    } else {
        $PROPER = '';
    }
    if (preg_match('/(^)(\\d{8})$/', $guess['episode'], $regs)) {
        $curEpisode = $regs[2];
        $expectedEpisode = $regs[2] + 1;
    } else {
        if (preg_match('/^(\\d+)x(\\d+)$/i', $guess['episode'], $regs)) {
            $curEpisode = preg_replace('/(\\d+)x/i', "", $guess['episode']);
            $curSeason = preg_replace('/x(\\d+)/i', "", $guess['episode']);
            $expectedEpisode = sprintf('%02d', $fav['Episode'] + 1);
        } else {
            return;
        }
    }
    if ($fav['Episode'] && $curEpisode > $expectedEpisode) {
        $show = $guess['key'];
        $episode = $guess['episode'];
        $expected = $curSeason . "x" . $expectedEpisode;
        $oldEpisode = $fav['Episode'];
        $oldSeason = $fav['Season'];
        $newEpisode = $curEpisode + 1;
        $newSeason = $curSeason + 1;
        $msg = "Matched \"{$show} {$episode}\" but expected \"{$expected}\".\n";
        $msg .= "This usualy means that a double episode is downloaded before this one.\n";
        $msg .= "But it could mean that you missed an episode or that \"{$episode}\" is a special episode.\n";
        $msg .= "If this is the case you need to reset the \"Last Downloaded Episode\" setting to \"{$oldSeason} x {$oldEpisode}\" in the Favorites menu.\n";
        $msg .= "If you don't, the next match wil be \"Season: {$curSeason} Episode: {$newEpisode}\" or \"Season {$newSeason} Episode: 1\".\n";
        $subject = "TorrentWatch-X: got {$show} {$episode}, expected {$expected}";
        MailNotify($msg, $subject);
        $msg = escapeshellarg($msg);
        run_script('error', $title, $msg);
    }
    if (!isset($fav['Season'], $fav['Episode']) || $regs[1] > $fav['Season']) {
        $fav['Season'] = $regs[1];
        $fav['Episode'] = $regs[2] . $PROPER;
    } else {
        if ($regs[1] == $fav['Season'] && $regs[2] > $fav['Episode']) {
            $fav['Episode'] = $regs[2] . $PROPER;
        } else {
            $fav['Episode'] .= $PROPER;
        }
    }
    write_config_file();
}
Example #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}";
    }
}