/** * Decompress a NZB, load it into simplexml and return. * * @param string $guid Release guid. * * @return bool|SimpleXMLElement * * @access public */ public function LoadNZB($guid) { // Fetch the NZB location using the GUID. $nzbPath = $this->nzb->getNZBPath($guid); if ($nzbPath === false) { if ($this->echooutput) { echo PHP_EOL . $guid . ' appears to be missing the nzb file, skipping.' . PHP_EOL; } return false; } $nzbContents = Utility::unzipGzipFile($nzbPath); if (!$nzbContents) { if ($this->echooutput) { echo PHP_EOL . 'Unable to decompress: ' . $nzbPath . ' - ' . fileperms($nzbPath) . ' - may have bad file permissions, skipping.' . PHP_EOL; } return false; } $nzbFile = @simplexml_load_string($nzbContents); if (!$nzbFile) { if ($this->echooutput) { echo PHP_EOL . "Unable to load NZB: {$guid} appears to be an invalid NZB, skipping." . PHP_EOL; } return false; } return $nzbFile; }
/** * Get list of contents inside a release's NZB file. * * @return bool */ protected function _getNZBContents() { $nzbPath = $this->_nzb->getNZBPath($this->_release['guid']); if ($nzbPath === false) { $this->_echo('NZB not found for GUID: ' . $this->_release['guid'], 'warning'); return $this->_decrementPasswordStatus(); } $nzbContents = Utility::unzipGzipFile($nzbPath); if (!$nzbContents) { $this->_echo('NZB is empty or broken for GUID: ' . $this->_release['guid'], 'warning'); return $this->_decrementPasswordStatus(); } // Get a list of files in the nzb. $this->_nzbContents = $this->_nzb->nzbFileList($nzbContents); if (count($this->_nzbContents) === 0) { $this->_echo('NZB is potentially broken for GUID: ' . $this->_release['guid'], 'warning'); return $this->_decrementPasswordStatus(); } // Sort keys. ksort($this->_nzbContents, SORT_NATURAL); return true; }
/** * Send a NZB to NZBGet. * * @param string $guid Release identifier. * * @return bool|mixed * * @access public */ public function sendNZBToNZBGet($guid) { $relData = $this->Releases->getByGuid($guid); $string = Utility::unzipGzipFile($this->NZB->getNZBPath($guid)); $string = $string === false ? '' : $string; $header = '<?xml version="1.0"?> <methodCall> <methodName>append</methodName> <params> <param> <value><string>' . $relData['searchname'] . '</string></value> </param> <param> <value><string>' . $relData['category_name'] . '</string></value> </param> <param> <value><i4>0</i4></value> </param> <param> <value><boolean>>False</boolean></value> </param> <param> <value> <string>' . base64_encode($string) . '</string> </value> </param> </params> </methodCall>'; Utility::getUrl(['url' => $this->fullURL . 'append', 'method' => 'post', 'postdata' => $header, 'verifycert' => false]); }
/** * Export to user specified folder. * * @param array $params * * @return bool * * @access public */ public function beginExport($params) { $gzip = false; if ($params[4] === true) { $gzip = true; } $fromDate = $toDate = ''; $path = $params[0]; // Check if the path ends with dir separator. if (substr($path, -1) !== DS) { $path .= DS; } // Check if it's a directory. if (!is_dir($path)) { $this->echoOut('Folder does not exist: ' . $path); return $this->returnValue(); } // Check if we can write to it. if (!is_writable($path)) { $this->echoOut('Folder is not writable: ' . $path); return $this->returnValue(); } // Check if the from date is the proper format. if (isset($params[1]) && $params[1] !== '') { if (!$this->checkDate($params[1])) { return $this->returnValue(); } $fromDate = $params[1]; } // Check if the to date is the proper format. if (isset($params[2]) && $params[2] !== '') { if (!$this->checkDate($params[2])) { return $this->returnValue(); } $toDate = $params[2]; } // Check if the group_id exists. if (isset($params[3]) && $params[3] !== 0) { if (!is_numeric($params[3])) { $this->echoOut('The group ID is not a number: ' . $params[3]); return $this->returnValue(); } $groups = $this->pdo->query('SELECT id, name FROM groups WHERE id = ' . $params[3]); if (count($groups) === 0) { $this->echoOut('The group ID is not in the DB: ' . $params[3]); return $this->returnValue(); } } else { $groups = $this->pdo->query('SELECT id, name FROM groups'); } $exported = 0; // Loop over groups to take less RAM. foreach ($groups as $group) { $currentExport = 0; // Get all the releases based on the parameters. $releases = $this->releases->getForExport($fromDate, $toDate, $group['id']); $totalFound = count($releases); if ($totalFound === 0) { if ($this->echoCLI) { echo 'No releases found to export for group: ' . $group['name'] . PHP_EOL; } continue; } if ($this->echoCLI) { echo 'Found ' . $totalFound . ' releases to export for group: ' . $group['name'] . PHP_EOL; } // Create a path to store the new NZB files. $currentPath = $path . $this->safeFilename($group['name']) . DS; if (!is_dir($currentPath)) { mkdir($currentPath); } foreach ($releases as $release) { // Get path to the NZB file. $nzbFile = $this->nzb->getNZBPath($release["guid"]); // Check if it exists. if ($nzbFile === false) { if ($this->echoCLI) { echo 'Unable to find NZB for release with GUID: ' . $release['guid']; } continue; } // Create path to current file. $currentFile = $currentPath . $this->safeFilename($release['searchname']); // Check if the user wants them in gzip, copy it if so. if ($gzip) { if (!copy($nzbFile, $currentFile . '.nzb.gz')) { if ($this->echoCLI) { echo 'Unable to export NZB with GUID: ' . $release['guid']; } continue; } // If not, decompress it and create a file to store it in. } else { $nzbContents = Utility::unzipGzipFile($nzbFile); if (!$nzbContents) { if ($this->echoCLI) { echo 'Unable to export NZB with GUID: ' . $release['guid']; } continue; } $fh = fopen($currentFile . '.nzb', 'w'); fwrite($fh, $nzbContents); fclose($fh); } $currentExport++; if ($this->echoCLI && $currentExport % 10 === 0) { echo 'Exported ' . $currentExport . ' of ' . $totalFound . ' nzbs for group: ' . $group['name'] . "\r"; } } if ($this->echoCLI && $currentExport > 0) { echo 'Exported ' . $currentExport . ' of ' . $totalFound . ' nzbs for group: ' . $group['name'] . PHP_EOL; } $exported += $currentExport; } if ($exported > 0) { $this->echoOut('Exported total of ' . $exported . ' NZB files to ' . $path); } return $this->returnValue(); }
function create_guids($live, $delete = false) { $pdo = new Settings(); $consoletools = new \ConsoleTools(['ColorCLI' => $pdo->log]); $timestart = TIME(); $relcount = $deleted = $total = 0; $relrecs = false; if ($live == "true") { $relrecs = $pdo->queryDirect(sprintf("SELECT id, guid FROM releases WHERE nzbstatus = 1 AND nzb_guid IS NULL ORDER BY id DESC")); } else { if ($live == "limited") { $relrecs = $pdo->queryDirect(sprintf("SELECT id, guid FROM releases WHERE nzbstatus = 1 AND nzb_guid IS NULL ORDER BY id DESC LIMIT 10000")); } } if ($relrecs) { $total = $relrecs->rowCount(); } if ($total > 0) { echo $pdo->log->header("Creating nzb_guids for " . number_format($total) . " releases."); $releases = new \Releases(['Settings' => $pdo]); $nzb = new \NZB($pdo); $releaseImage = new \ReleaseImage($pdo); $reccnt = 0; if ($relrecs instanceof \Traversable) { foreach ($relrecs as $relrec) { $reccnt++; $nzbpath = $nzb->getNZBPath($relrec['guid']); if ($nzbpath !== false) { $nzbfile = Utility::unzipGzipFile($nzbpath); if ($nzbfile) { $nzbfile = @simplexml_load_string($nzbfile); } if (!$nzbfile) { if (isset($delete) && $delete == 'delete') { //echo "\n".$nzb->NZBPath($relrec['guid'])." is not a valid xml, deleting release.\n"; $releases->deleteSingle(['g' => $relrec['guid'], 'i' => $relrec['id']], $nzb, $releaseImage); $deleted++; } continue; } $binary_names = []; foreach ($nzbfile->file as $file) { $binary_names[] = $file["subject"]; } if (count($binary_names) == 0) { if (isset($delete) && $delete == 'delete') { //echo "\n".$nzb->NZBPath($relrec['guid'])." has no binaries, deleting release.\n"; $releases->deleteSingle(['g' => $relrec['guid'], 'i' => $relrec['id']], $nzb, $releaseImage); $deleted++; } continue; } asort($binary_names); foreach ($nzbfile->file as $file) { if ($file["subject"] == $binary_names[0]) { $segment = $file->segments->segment; $nzb_guid = md5($segment); $pdo->queryExec("UPDATE releases set nzb_guid = UNHEX(" . $pdo->escapestring($nzb_guid) . ") WHERE id = " . $relrec["id"]); $relcount++; $consoletools->overWritePrimary("Created: [" . $deleted . "] " . $consoletools->percentString($reccnt, $total) . " Time:" . $consoletools->convertTimer(TIME() - $timestart)); break; } } } else { if (isset($delete) && $delete == 'delete') { //echo $pdo->log->primary($nzb->NZBPath($relrec['guid']) . " does not have an nzb, deleting."); $releases->deleteSingle(['g' => $relrec['guid'], 'i' => $relrec['id']], $nzb, $releaseImage); } } } } if ($relcount > 0) { echo "\n"; } echo $pdo->log->header("Updated " . $relcount . " release(s). This script ran for " . $consoletools->convertTime(TIME() - $timestart)); } else { echo $pdo->log->info('Query time: ' . $consoletools->convertTime(TIME() - $timestart)); exit($pdo->log->info("No releases are missing the guid.")); } }
use newznab\utility\Utility; $pdo = new Settings(); if (isset($argv[1]) && ($argv[1] === "true" || $argv[1] === "delete")) { $releases = new \Releases(['Settings' => $pdo]); $nzb = new \NZB($pdo); $releaseImage = new \ReleaseImage($pdo); $consoletools = new \ConsoleTools(['ColorCLI' => $pdo->log]); $timestart = time(); $checked = $deleted = 0; $couldbe = $argv[1] === "true" ? $couldbe = "could be " : "were "; echo $pdo->log->header('Getting List of nzbs to check against db.'); $dirItr = new \RecursiveDirectoryIterator($pdo->getSetting('nzbpath')); $itr = new \RecursiveIteratorIterator($dirItr, \RecursiveIteratorIterator::LEAVES_ONLY); foreach ($itr as $filePath) { if (is_file($filePath) && preg_match('/([a-f-0-9]+)\\.nzb\\.gz/', $filePath, $guid)) { $nzbfile = Utility::unzipGzipFile($filePath); if ($nzbfile) { $nzbfile = @simplexml_load_string($nzbfile); } if ($nzbfile) { $res = $pdo->queryOneRow(sprintf("SELECT id, guid FROM releases WHERE guid = %s", $pdo->escapeString(stristr($filePath->getFilename(), '.nzb.gz', true)))); if ($res === false) { if ($argv[1] === "delete") { @copy($filePath, NN_ROOT . "pooped/" . $guid[1] . ".nzb.gz"); $releases->deleteSingle(['g' => $guid[1], 'i' => false], $nzb, $releaseImage); $deleted++; } } else { if (isset($res)) { $pdo->queryExec(sprintf("UPDATE releases SET nzbstatus = 1 WHERE id = %s", $res['id'])); }
/** * @param array $filesToProcess List of NZB files to import. * @param bool|string $useNzbName Use the NZB file name as release name? * @param bool $delete Delete the NZB when done? * @param bool $deleteFailed Delete the NZB if failed importing? * * @return string|bool * * @access public */ public function beginImport($filesToProcess, $useNzbName = false, $delete = true, $deleteFailed = true) { // Get all the groups in the DB. if (!$this->getAllGroups()) { if ($this->browser) { return $this->retVal; } else { return false; } } $start = date('Y-m-d H:i:s'); $nzbsImported = $nzbsSkipped = 0; // Loop over the file names. foreach ($filesToProcess as $nzbFile) { // Check if the file is really there. if (is_file($nzbFile)) { // Get the contents of the NZB file as a string. if (strtolower(substr($nzbFile, -7)) === '.nzb.gz') { $nzbString = Utility::unzipGzipFile($nzbFile); } else { $nzbString = file_get_contents($nzbFile); } if ($nzbString === false) { $this->echoOut('ERROR: Unable to read: ' . $nzbFile); if ($deleteFailed) { @unlink($nzbFile); } $nzbsSkipped++; continue; } // Load it as a XML object. $nzbXML = @simplexml_load_string($nzbString); if ($nzbXML === false || strtolower($nzbXML->getName()) != 'nzb') { $this->echoOut('ERROR: Unable to load NZB XML data: ' . $nzbFile); if ($deleteFailed) { @unlink($nzbFile); } $nzbsSkipped++; continue; } // Try to insert the NZB details into the DB. $inserted = $this->scanNZBFile($nzbXML, $useNzbName ? str_ireplace('.nzb', '', basename($nzbFile)) : false); if ($inserted) { // Try to copy the NZB to the NZB folder. $path = $this->nzb->getNZBPath($this->relGuid, 0, true); // Try to compress the NZB file in the NZB folder. $fp = gzopen($path, 'w5'); gzwrite($fp, $nzbString); gzclose($fp); if (!is_file($path)) { $this->echoOut('ERROR: Problem compressing NZB file to: ' . $path); // Remove the release. $this->pdo->queryExec(sprintf("DELETE FROM releases WHERE guid = %s", $this->pdo->escapeString($this->relGuid))); if ($deleteFailed) { @unlink($nzbFile); } $nzbsSkipped++; continue; } else { if ($delete) { // Remove the nzb file. @unlink($nzbFile); } $nzbsImported++; continue; } } else { if ($deleteFailed) { @unlink($nzbFile); } $nzbsSkipped++; continue; } } else { $this->echoOut('ERROR: Unable to fetch: ' . $nzbFile); $nzbsSkipped++; continue; } } $this->echoOut('Proccessed ' . $nzbsImported . ' NZBs in ' . (strtotime(date('Y-m-d H:i:s')) - strtotime($start)) . ' seconds, ' . $nzbsSkipped . ' NZBs were skipped.'); if ($this->browser) { return $this->retVal; } else { return true; } }