function check_for_torrent(&$item, $key, $opts) { global $matched, $test_run, $config_values; if (!(strtolower($item['Feed']) == 'all' || $item['Feed'] === '' || $item['Feed'] == $opts['URL'])) { return; } $rs = $opts['Obj']; $title = strtolower($rs['title']); switch (_isset($config_values['Settings'], 'MatchStyle')) { case 'simple': $hit = $item['Filter'] != '' && strpos(strtr($title, " .", "__"), strtr(strtolower($item['Filter']), " .", "__")) === 0 && ($item['Not'] == '' or my_strpos($title, strtolower($item['Not'])) === FALSE) && ($item['Quality'] == 'All' or $item['Quality'] == '' or my_strpos($title, strtolower($item['Quality'])) !== FALSE); break; case 'glob': $hit = $item['Filter'] != '' && fnmatch(strtolower($item['Filter']), $title) && ($item['Not'] == '' or !fnmatch(strtolower($item['Not']), $title)) && ($item['Quality'] == 'All' or $item['Quality'] == '' or strpos($title, strtolower($item['Quality'])) !== FALSE); break; case 'regexp': default: $hit = $item['Filter'] != '' && preg_match('/\\b' . strtolower(str_replace(' ', '[\\s._]', $item['Filter'])) . '\\b/', $title) && ($item['Not'] == '' or !preg_match('/' . strtolower($item['Not']) . '/', $title)) && ($item['Quality'] == 'All' or $item['Quality'] == '' or preg_match('/' . strtolower($item['Quality']) . '/', $title)); break; } if (strtolower($item['Filter']) == "any") { $hit = 1; $any = 1; } if ($hit) { $guess = guess_match($title, TRUE); } if ($hit && episode_filter($guess, $item['Episodes']) == true) { $matched = 'match'; if (preg_match('/^\\d+p$/', $item['Episode'])) { $item['Episode'] = preg_replace('/^(\\d+)p/', '\\1', $item['Episode']); $PROPER = 1; } if (check_cache($rs['title'])) { if (!$any && _isset($config_values['Settings'], 'Only Newer') == 1) { if (!empty($guess['episode']) && preg_match('/^(\\d+)x(\\d+)p?$|^(\\d{8})p?$/i', $guess['episode'], $regs)) { if (isset($regs[3]) && preg_match('/^(\\d{8})$/', $regs[3]) && $item['Episode'] >= $regs[3]) { _debug($item['Name'] . ": " . $item['Episode'] . ' >= ' . $regs[3] . "\r\n", 1); $matched = "old"; return FALSE; } else { if (isset($regs[1]) && preg_match('/^(\\d{1,3})$/', $regs[1]) && $item['Season'] > $regs[1]) { _debug($item['Name'] . ": " . $item['Season'] . ' > ' . $regs[1] . "\r\n", 1); $matched = "old"; return FALSE; } else { if (isset($regs[2]) && preg_match('/^(\\d{1,3})$/', $regs[1]) && $item['Season'] == $regs[1] && $item['Episode'] >= $regs[2]) { if (!preg_match('/proper|repack|rerip/i', $rs['title'])) { _debug($item['Name'] . ": " . $item['Episode'] . ' >= ' . $regs[2] . "\r\n", 1); $matched = "old"; return FALSE; } else { if ($PROPER == 1) { _debug("Allready downloaded this Proper, Repack or Rerip of " . $item['Name'] . " {$regs['1']}x{$regs['2']}{$regs['3']}\r\n"); $matched = "old"; return FALSE; } } } } } } else { if ($guess['episode'] == 'fullSeason') { $matched = "season"; return FALSE; } else { if ($guess['episode'] != 'noShow' && !preg_match('/^(\\d{1,2} \\d{1,2} \\d{2,4})$/', $guess['episode']) || $config_values['Settings']['Require Episode Info'] == 1) { _debug("{$item} is not in a workable format."); $matched = "nomatch"; return FALSE; } } } } _debug('Match found for ' . $rs['title'] . "\n"); if ($test_run) { $matched = 'test'; return; } if ($link = get_torrent_link($rs)) { $response = client_add_torrent($link, NULL, $rs['title'], $opts['URL'], $item); if (preg_match('/^Error:/', $response)) { _debug("Failed adding torrent {$link}\n", -1); return FALSE; } else { add_cache($rs['title']); } } else { _debug("Unable to find URL for " . $rs['title'] . "\n", -1); $matched = "nourl"; } } } }
function parse_options() { global $html_out, $config_values; $filler = "<br>"; array_keys($_GET); $commands = array_keys($_GET); if (empty($commands)) { return FALSE; } if (preg_match("/^\\//", $commands[0])) { $commands[0] = preg_replace("/^\\//", '', $commands[0]); } switch ($commands[0]) { case 'getClientData': if ($_REQUEST['recent']) { $response = getClientData(1); } else { $response = getClientData(0); } echo $response; exit; case 'delTorrent': $response = delTorrent($_REQUEST['delTorrent'], $_REQUEST['trash'], $_REQUEST['batch']); echo "{$response}"; exit; case 'stopTorrent': $response = stopTorrent($_REQUEST['stopTorrent'], $_REQUEST['batch']); echo "{$response}"; exit; case 'startTorrent': $response = startTorrent($_REQUEST['startTorrent'], $_REQUEST['batch']); echo "{$response}"; exit; case 'moveTo': $response = moveTorrent($_REQUEST['moveTo'], $_REQUEST['torHash'], $_REQUEST['batch']); echo "{$response}"; exit; case 'updateFavorite': $response = update_favorite(); if (preg_match("/^Error:", $response)) { echo "<div id=\"fav_error\" class=\"dialog_window\" style=\"display: block\">{$response}</div>"; } break; case 'updateFeed': update_feed(); break; case 'clearCache': clear_cache(); break; case 'setGlobals': update_global_config(); write_config_file(); break; case 'matchTitle': $feedLink = $_GET['rss']; foreach ($config_values['Feeds'] as $key => $feed) { if ($feed['Link'] == "{$feedLink}") { $idx = $key; } } if ($config_values['Feeds'][$idx]['seedRatio']) { $seedRatio = $config_values['Feeds'][$idx]['seedRatio']; } else { $seedRatio = $config_values['Settings']['Default Seed Ratio']; } if (!$seedRatio) { $seedRatio = -1; } if ($tmp = guess_match(html_entity_decode($_GET['title']), TRUE)) { $_GET['name'] = trim(strtr($tmp['key'], "._", " ")); if ($config_values['Settings']['MatchStyle'] == "glob") { $_GET['filter'] = trim(strtr($tmp['key'], " ._", "???")); $_GET['filter'] .= '*'; } else { $_GET['filter'] = trim($tmp['key']); } $_GET['quality'] = $tmp['data']; $_GET['feed'] = $_GET['rss']; $_GET['button'] = 'Add'; $_GET['savein'] = 'Default'; $_GET['seedratio'] = $seedRatio; } else { $_GET['name'] = $_GET['title']; $_GET['filter'] = $_GET['title']; $_GET['quality'] = 'All'; $_GET['feed'] = $_GET['rss']; $_GET['button'] = 'Add'; $_GET['savein'] = 'Default'; $_GET['seedratio'] = $seedRatio; } if ($config_values['Settings']['Default Feed All'] && preg_match('/^(\\d+)x(\\d+)p?$|^(\\d{8})$/i', $tmp['episode'])) { $_GET['feed'] = 'All'; } $response = update_favorite(); if ($response) { echo "{$response}"; } //break; exit; case 'hide': $response = add_hidden(ucwords($_GET['hide'])); if ($response) { echo "ERROR:{$response}"; } else { $guess = guess_match(html_entity_decode($_GET['hide']), TRUE); echo $guess['key']; } exit; case 'delHidden': del_hidden($_GET['unhide']); break; case 'dlTorrent': // Loaded via ajax foreach ($config_values['Favorites'] as $fav) { $guess = guess_match(html_entity_decode($_GET['title'])); $name = trim(strtr($guess['key'], "._", " ")); if ($name == $fav['Name']) { $downloadDir = $fav['Save In']; } } if ((!isset($downloadDir) || $downloadDir == "Default") && isset($config_values['Settings']['Download Dir'])) { $downloadDir = $config_values['Settings']['Download Dir']; } $r = client_add_torrent(preg_replace('/ /', '%20', trim($_GET['link'])), $downloadDir, $_GET['title'], $_GET['feed']); if ($r == "Success") { $torHash = get_torHash(add_cache($_GET['title'])); } if (isset($torHash)) { echo $torHash; } else { echo $r; } exit(0); break; case 'clearHistory': // Loaded via ajax if (file_exists($config_values['Settings']['History'])) { unlink($config_values['Settings']['History']); } display_history(); close_html(); exit(0); break; case 'get_client': global $config_values; echo $config_values['Settings']['Client']; exit; case 'version_check': echo version_check(); exit; case 'post_bug': global $tw_version; $response = post_bug($_POST['Summary'], $_POST['Name'], $_POST['Email'], $_POST['Priority'], $_POST['Description']); echo $response; exit; case 'get_dialog_data': switch ($_GET['get_dialog_data']) { case '#favorites': display_favorites(); exit; case '#configuration': display_global_config(); exit; case '#hidelist': display_hidelist(); exit; case '#feeds': display_feeds(); exit; case '#history': display_history(); exit; case '#show_legend': display_legend(); exit; case '#report_bug': report_bug(); exit; case '#clear_cache': display_clearCache(); exit; case '#show_transmission': display_transmission(); exit; case '#show_info': show_info(urldecode($_GET['episode_name'])); exit; default: exit; } default: $output = "<script type='text/javascript'>alert('Bad Paramaters passed to " . $_SERVER['PHP_SELF'] . ": " . $_SERVER['REQUEST_URI'] . "');</script>"; } if (isset($exec)) { exec($exec, $output); } if (isset($output)) { if (is_array($output)) { $output = implode($filler, $output); } $html_out .= str_replace("\n", "<br>", "<div class='execoutput'>{$output}</div>"); echo $html_out; $html_out = ""; } return; }
function check_for_torrents($directory, $dest) { if ($handle = opendir($directory)) { while (false !== ($file = readdir($handle))) { $title = substr($file, 0, strrpos($file, '.') - 1); if (preg_match('/\\.torrent$/', $file) && client_add_torrent("{$directory}/{$file}", $dest, $title)) { unlink("{$directory}/{$file}"); } } closedir($handle); } else { _debug("check_for_torrents: Couldn't read Directory: {$directory}\n", 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}"; } }