/** * 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->NZBPath($guid); if ($nzbPath === false) { if ($this->echooutput) { echo PHP_EOL . $guid . ' appears to be missing the nzb file, skipping.' . PHP_EOL; } return false; } $nzbContents = nzedb\utility\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->NZBPath($this->_release['guid']); if ($nzbPath === false) { $this->_echo('NZB not found for GUID: ' . $this->_release['guid'], 'warning'); // The nzb was not located. decrement the password status. $this->pdo->queryExec(sprintf('UPDATE releases SET passwordstatus = passwordstatus - 1 WHERE id = %d', $this->_release['id'])); return false; } $nzbContents = \nzedb\utility\Utility::unzipGzipFile($nzbPath); // Get a list of files in the nzb. $this->_nzbContents = $this->_nzb->nzbFileList($nzbContents); if (count($this->_nzbContents) === 0) { $this->_echo('NZB is empty or broken for GUID: ' . $this->_release['guid'], 'warning'); // There does not appear to be any files in the nzb, decrement password status. $this->pdo->queryExec(sprintf('UPDATE releases SET passwordstatus = passwordstatus - 1 WHERE id = %d', $this->_release['id'])); return false; } // Sort the files inside the NZB. usort($this->_nzbContents, ['\\nzedb\\processing\\post\\ProcessAdditional', '_sortNZB']); return true; }
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->NZBPath($relrec['guid']); if ($nzbpath !== false) { $nzbfile = nzedb\utility\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 = array(); 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 = " . $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.")); } }
<?php /* Fixes NZB files with a blank first line. */ require dirname(__FILE__) . '/../../../www/config.php'; use nzedb\db\Settings; $pdo = new Settings(); if (isset($argv[1]) && $argv[1] == "true") { $timestart = time(); $nzbcount = $brokencount = 0; $guids = $pdo->queryDirect("SELECT guid FROM releases WHERE nzbstatus = 1 ORDER BY postdate DESC"); echo $pdo->log->primary("Be patient, this WILL take a very long time, make sure to kill all nZEDb scripts first. There are " . number_format($guids->rowCount()) . " NZB files to scan."); $nzb = new NZB($pdo); if ($guids instanceof Traversable) { foreach ($guids as $guid) { $nzbpath = $nzb->NZBPath($guid["guid"]); if ($nzbpath !== false) { $nzbcount++; $nzbfile = nzedb\utility\Utility::unzipGzipFile($nzbpath); if ($nzbfile && preg_match('/^[\\r\\n]+<\\?xml/', $nzbfile)) { $brokencount++; $nzbfile = preg_replace('/^[\\r\\n]+<\\?xml/i', '<?xml', $nzbfile); $nzb = preg_replace('/<\\/nzb>.+/i', '</nzb>', $nzbfile); unlink($nzbpath); $fp = gzopen($nzbpath, 'w6'); if ($fp) { gzwrite($fp, $nzb, strlen($nzb)); gzclose($fp); chmod($nzbpath, 0777); } } if ($nzbcount % 5000 == 0) {
/** * @param $guids * * @return string */ public function getZipped($guids) { $nzb = new NZB($this->pdo); $zipFile = new ZipFile(); foreach ($guids as $guid) { $nzbPath = $nzb->NZBPath($guid); if ($nzbPath) { $nzbContents = nzedb\utility\Utility::unzipGzipFile($nzbPath); if ($nzbContents) { $filename = $guid; $r = $this->getByGuid($guid); if ($r) { $filename = $r['searchname']; } $zipFile->addFile($nzbContents, $filename . '.nzb'); } } } return $zipFile->file(); }
/** * 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->NZBPath($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 = nzedb\utility\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(); }