Example #1
0
 /**
  * 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 = 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;
 }
Example #2
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 = Utility::unzipGzipFile($nzbPath);
             if ($nzbContents) {
                 $filename = $guid;
                 $r = $this->getByGuid($guid);
                 if ($r) {
                     $filename = $r['searchname'];
                 }
                 $zipFile->addFile($nzbContents, $filename . '.nzb');
             }
         }
     }
     return $zipFile->file();
 }
Example #3
0
use nzedb\utility\Utility;
if (!$page->users->isLoggedIn()) {
    $page->show403();
}
if (isset($_GET["id"])) {
    $releases = new Releases(['Settings' => $page->settings]);
    $rel = $releases->getByGuid($_GET["id"]);
    if (!$rel) {
        $page->show404();
    }
    $nzb = new NZB($page->settings);
    $nzbpath = $nzb->getNZBPath($_GET["id"]);
    if (!file_exists($nzbpath)) {
        $page->show404();
    }
    $nzbfile = Utility::unzipGzipFile($nzbpath);
    $ret = $nzb->nzbFileList($nzbfile);
    $offset = isset($_REQUEST["offset"]) && ctype_digit($_REQUEST['offset']) ? $_REQUEST["offset"] : 0;
    $page->smarty->assign('pagertotalitems', sizeof($ret));
    $page->smarty->assign('pageroffset', $offset);
    $page->smarty->assign('pageritemsperpage', ITEMS_PER_PAGE);
    $page->smarty->assign('pagerquerybase', WWW_TOP . "/filelist/" . $_GET["id"] . "/&offset=");
    $page->smarty->assign('pagerquerysuffix', "#results");
    $pager = $page->smarty->fetch("pager.tpl");
    $page->smarty->assign('pager', $pager);
    $page->smarty->assign('rel', $rel);
    $page->smarty->assign('files', array_slice($ret, $offset, ITEMS_PER_PAGE));
    $page->title = "File List";
    $page->meta_title = "View Nzb file list";
    $page->meta_keywords = "view,nzb,file,list,description,details";
    $page->meta_description = "View Nzb File List";
Example #4
0
 /**
  * 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 = 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();
 }
Example #5
0
 /**
  * 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');
         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 the files inside the NZB.
     usort($this->_nzbContents, ['\\nzedb\\processing\\post\\ProcessAdditional', '_sortNZB']);
     return true;
 }
Example #6
0
    /**
     * 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;
        $encoded = base64_encode($string);
        $header = <<<NZBGet_NZB
<?xml version="1.0"?>
<methodCall>
\t<methodName>append</methodName>
\t<params>
\t\t<param>
\t\t\t<value><string>{$relData['searchname']}</string></value>
\t\t</param>
\t\t<param>
\t\t\t<value><string>{$relData['category_name']}</string></value>
\t\t</param>
\t\t<param>
\t\t\t<value><i4>0</i4></value>
\t\t</param>
\t\t<param>
\t\t\t<value><boolean>>False</boolean></value>
\t\t</param>
\t\t<param>
\t\t\t<value>
\t\t\t\t<string>
{$encoded}
\t\t\t\t</string>
\t\t\t</value>
\t\t</param>
\t</params>
</methodCall>
NZBGet_NZB;
        Utility::getUrl(['url' => $this->fullURL . 'append', 'method' => 'post', 'postdata' => $header, 'verifycert' => false]);
    }
Example #7
0
use nzedb\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, nZEDb_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']));
                    }
Example #8
0
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 = 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."));
    }
}
Example #9
0
 /**
  * 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;
 }
Example #10
0
 /**
  * @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;
     }
 }
Example #11
0
require_once './config.php';
use nzedb\Releases;
use nzedb\NZB;
use nzedb\utility\Utility;
$page = new AdminPage();
if (!$page->users->isLoggedIn()) {
    $page->show403();
}
if (isset($_GET['id'])) {
    $releases = new Releases(['Settings' => $page->settings]);
    $release = $releases->getByGuid($_GET['id']);
    if ($release === false) {
        $page->show404();
    }
    $nzb = new NZB($page->settings);
    $nzbPath = $nzb->getNZBPath($_GET['id']);
    if (!file_exists($nzbPath)) {
        $page->show404();
    }
    $nzbFile = Utility::unzipGzipFile($nzbPath);
    $files = $nzb->nzbFileList($nzbFile);
    $page->smarty->assign('release', $release);
    $page->smarty->assign('files', $files);
    $page->title = "File List";
    $page->meta_title = "View Nzb file list";
    $page->meta_keywords = "view,nzb,file,list,description,details";
    $page->meta_description = "View Nzb File List";
    $page->content = $page->smarty->fetch('release-files.tpl');
    $page->render();
}