serverurl = $page->serverurl; $this->uid = $page->userdata['id']; $this->rsstoken = $page->userdata['rsstoken']; if (!empty($page->userdata['nzbgeturl'])) { $this->url = $page->userdata['nzbgeturl']; $this->userName = (empty($page->userdata['nzbgetusername']) ? '' : $page->userdata['nzbgetusername']); $this->password = (empty($page->userdata['nzbgetpassword']) ? '' : $page->userdata['nzbgetpassword']); } $this->fullURL = $this->verifyURL($this->url); $this->Releases = new \Releases(); $this->NZB = new \NZB(); } /** * 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->NZBPath($guid)); $string = ($string === false ? '' : $string); $header = ' append ' . $relData['searchname'] . ' ' . $relData['category_name'] . ' 0 >False ' . base64_encode($string) . ' '; Utility::getUrl(['url' => $this->fullURL . 'append', 'method' => 'post', 'postdata' => $header, 'verifycert' => false]); } /** * Send a NZB URL to NZBGet. * * @param string $guid Release identifier. * * @return bool|mixed * * @access public */ public function sendURLToNZBGet($guid) { $reldata = $this->Releases->getByGuid($guid); $header = ' appendurl ' . $reldata['searchname'] . '.nzb' . ' ' . $reldata['category_name'] . ' 0 >False ' . $this->serverurl . 'getnzb/' . $guid . '%26i%3D' . $this->uid . '%26r%3D' . $this->rsstoken . ' '; Utility::getUrl(['url' => $this->fullURL . 'appendurl', 'method' => 'post', 'postdata' => $header, 'verifycert' => false]); } /** * Pause download queue on server. This method is equivalent for command "nzbget -P". * * @return void * * @access public */ public function pauseAll() { $header = ' pausedownload2 1 '; Utility::getUrl(['url' => $this->fullURL . 'pausedownload2', 'method' => 'post', 'postdata' => $header, 'verifycert' => false]); } /** * Resume (previously paused) download queue on server. This method is equivalent for command "nzbget -U". * * @return void * * @access public */ public function resumeAll() { $header = ' resumedownload2 1 '; Utility::getUrl(['url' => $this->fullURL . 'resumedownload2', 'method' => 'post', 'postdata' => $header, 'verifycert' => false]); } /** * Pause a single NZB from the queue. * * @param string $id * * @access public */ public function pauseFromQueue($id) { $header = ' editqueue GroupPause 0 "" ' . $id . ' '; Utility::getUrl(['url' => $this->fullURL . 'editqueue', 'method' => 'post', 'postdata' => $header, 'verifycert' => false]); } /** * Resume a single NZB from the queue. * * @param string $id * * @access public */ public function resumeFromQueue($id) { $header = ' editqueue GroupResume 0 "" ' . $id . ' '; Utility::getUrl(['url' => $this->fullURL . 'editqueue', 'method' => 'post', 'postdata' => $header, 'verifycert' => false]); } /** * Delete a single NZB from the queue. * * @param string $id * * @access public */ public function delFromQueue($id) { $header = ' editqueue GroupDelete 0 "" ' . $id . ' '; Utility::getUrl(['url' => $this->fullURL . 'editqueue', 'method' => 'post', 'postdata' => $header, 'verifycert' => false]); } /** * Set download speed limit. This method is equivalent for command "nzbget -R ". * * @param int $limit The speed to limit it to. * * @return bool * * @access public */ public function rate($limit) { $header = ' rate ' . $limit . ' '; Utility::getUrl(['url' => $this->fullURL . 'rate', 'method' => 'post', 'postdata' => $header, 'verifycert' => false]); } /** * Get all items in download queue. * * @return array|bool * * @access public */ public function getQueue() { $data = Utility::getUrl(['url' => $this->fullURL . 'listgroups', 'verifycert' => false]); $retVal = false; if ($data) { $xml = simplexml_load_string($data); if ($xml) { $retVal = []; $i = 0; foreach ($xml->params->param->value->array->data->value as $value) { foreach ($value->struct->member as $member) { $value = (array)$member->value; $value = array_shift($value); if (!is_object($value)) { $retVal[$i][(string)$member->name] = $value; } } $i++; } } } return $retVal; } /** * Request for current status (summary) information. Parts of informations returned by this method can be printed by command "nzbget -L". * * @return array The status. * * @access public */ public function status() { $data = Utility::getUrl(['url' => $this->fullURL . 'status', 'verifycert' => false]); $retVal = false; if ($data) { $xml = simplexml_load_string($data); if ($xml) { foreach ($xml->params->param->value->struct->member as $member) { $value = (array)$member->value; $value = array_shift($value); if (!is_object($value)) { $retVal[(string)$member->name] = $value; } } } } return $retVal; } /** * Verify if the NZBGet URL is correct. * * @param string $url NZBGet URL to verify. * * @return bool|string * * @access public */ public function verifyURL($url) { if (preg_match('/(?Phttps?):\/\/(?P.+?)(:(?P\d+\/)|\/)$/i', $url, $matches)) { return $matches['protocol'] . '://' . $this->userName . ':' . $this->password . '@' . $matches['url'] . (isset($matches['port']) ? ':' . $matches['port'] : (substr($matches['url'], -1) === '/' ? '' : '/')) . 'xmlrpc/'; } else { return false; } } }