/**
  * Writes out the nzb when processing releases. Performed outside of smarty due to memory issues
  * of holding all parts in an array.
  */
 function writeNZBforReleaseId($relid, $name, $catId, $path)
 {
     $db = new DB();
     $cat = new Category();
     $catrow = $cat->getById($catId);
     $site = new Sites();
     $fp = _gzopen($path, "w");
     if ($fp) {
         gzwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
         gzwrite($fp, "<!DOCTYPE nzb PUBLIC \"-//newzBin//DTD NZB 1.1//EN\" \"http://www.newzbin.com/DTD/nzb/nzb-1.1.dtd\">\n");
         gzwrite($fp, "<nzb xmlns=\"http://www.newzbin.com/DTD/2003/nzb\">\n\n");
         gzwrite($fp, "<head>\n");
         if ($catrow) {
             gzwrite($fp, " <meta type=\"category\">" . htmlspecialchars($catrow["title"], ENT_QUOTES, 'utf-8') . "</meta>\n");
         }
         if ($name != "") {
             gzwrite($fp, " <meta type=\"name\">" . htmlspecialchars($name, ENT_QUOTES, 'utf-8') . "</meta>\n");
         }
         gzwrite($fp, "</head>\n\n");
         $result = $db->queryDirect(sprintf("SELECT binaries.*, UNIX_TIMESTAMP(date) AS unixdate, groups.name as groupname FROM binaries inner join groups on binaries.groupID = groups.ID WHERE binaries.releaseID = %d ORDER BY binaries.name", $relid));
         while ($binrow = $db->getAssocArray($result)) {
             $groups = array();
             $groupsRaw = explode(' ', $binrow['xref']);
             foreach ($groupsRaw as $grp) {
                 if (preg_match('/^([a-z0-9\\.\\-_]+):(\\d+)?$/i', $grp, $match) && strtolower($grp) !== 'xref') {
                     $groups[] = $match[1];
                 }
             }
             if (count($groups) == 0) {
                 $groups[] = $binrow["groupname"];
             }
             gzwrite($fp, "<file poster=\"" . htmlspecialchars($binrow["fromname"], ENT_QUOTES, 'utf-8') . "\" date=\"" . $binrow["unixdate"] . "\" subject=\"" . htmlspecialchars($binrow["name"], ENT_QUOTES, 'utf-8') . " (1/" . $binrow["totalParts"] . ")\">\n");
             gzwrite($fp, " <groups>\n");
             foreach ($groups as $group) {
                 gzwrite($fp, "  <group>" . $group . "</group>\n");
             }
             gzwrite($fp, " </groups>\n");
             gzwrite($fp, " <segments>\n");
             $resparts = $db->queryDirect(sprintf("SELECT DISTINCT(messageID), size, partnumber FROM parts WHERE binaryID = %d ORDER BY partnumber", $binrow["ID"]));
             while ($partsrow = $db->getAssocArray($resparts)) {
                 gzwrite($fp, "  <segment bytes=\"" . $partsrow["size"] . "\" number=\"" . $partsrow["partnumber"] . "\">" . htmlspecialchars($partsrow["messageID"], ENT_QUOTES, 'utf-8') . "</segment>\n");
             }
             gzwrite($fp, " </segments>\n</file>\n");
         }
         gzwrite($fp, "<!-- generated by newznab " . $site->version() . " -->\n</nzb>");
         gzclose($fp);
     }
 }
                        if (array_key_exists($group, $siteGroups)) {
                            $groupID = $siteGroups[$group];
                            $groupName = $group;
                            break;
                        }
                    }
                    if ($groupID == -1) {
                        $retval .= "Missing Group for NZB : " . $browserpostednames[$nzbFile] . " - " . join(", ", $nzbInfo->groups) . "<br/>";
                        continue;
                    }
                    $relguid = md5(uniqid());
                    $name = $releases->cleanReleaseName(str_replace(".nzb", "", $browserpostednames[$nzbFile]));
                    $catId = $cat->determineCategory($groupName, $name);
                    $relid = $releases->insertRelease($name, $nzbInfo->filecount, $groupID, $relguid, $catId, "", date("Y-m-d H:i:s", $nzbInfo->postedlast), $nzbInfo->poster, "", $page->site);
                    $db->exec(sprintf("update releases set totalpart = %d, size = %s, completion = %d, GID=%s where ID = %d", $nzbInfo->filecount, $nzbInfo->filesize, $nzbInfo->completion, $db->escapeString($nzbInfo->gid), $relid));
                    $nzbfilename = $nzb->getNZBPath($relguid, $page->site->nzbpath, true);
                    $fp = _gzopen($nzbfilename, "w");
                    if ($fp) {
                        gzwrite($fp, $nzbInfo->toNzb());
                        gzclose($fp);
                        $retval .= sprintf(" Imported %s<br/>", $name);
                    }
                }
            }
        }
    }
    $page->smarty->assign('output', $retval);
}
$page->title = "Import Nzbs";
$page->content = $page->smarty->fetch('nzb-import.tpl');
$page->render();
<?php

require_once dirname(__FILE__) . '/../../www/config.php';
use newznab\db\Settings;
$pdo = new Settings();
$n = new NZB();
$s = new Sites();
$items = $pdo->query("SELECT guid FROM releases");
while ($item = array_pop($items)) {
    $guid = $item['guid'];
    $nzbpath = $n->getNZBPath($guid, $pdo->getSetting('nzbpath'));
    $zd = _gzopen($nzbpath, "r");
    $s = gzread($zd, 10);
    if ($s != "<?xml vers") {
        echo "f****d - " . $guid . "\n";
    }
}