serverurl = url('/'); $this->uid = $page->userdata['id']; $this->api_token = $page->userdata['api_token']; 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(); $this->client = new Client(); } /** * @param $guid * @return \GuzzleHttp\Psr7\Request */ public function sendNZBToNZBGet($guid) { $relData = Release::getByGuid($guid); $gzipFile = Utility::unzipGzipFile($this->nzb->NZBPath($guid)); $string = $gzipFile === false ? '' : $gzipFile; $header = ' append '.$relData['searchname'].' '.$relData['category_name'].' 0 >False '. base64_encode($string). ' '; return new Request('POST', $this->fullUrl.'append', ['Content-Type' => 'text/xml; charset=UTF8'], $header); } /** * Send a NZB URL to NZBGet. * * @param string $guid Release identifier. * * @return bool|mixed */ public function sendURLToNZBGet($guid) { $reldata = Release::getByGuid($guid); $header = ' appendurl '.$reldata['searchname'].'.nzb'.' '.$reldata['category_name'].' 0 >False '. $this->serverurl. 'getnzb?id='. $guid. '%26r%3D'. $this->api_token . ' '; return new Request('POST', $this->fullUrl.'append', ['Content-Type' => 'text/xml; charset=UTF8'], $header); } /** * Pause download queue on server. This method is equivalent for command "nzbget -P". * * @return void */ public function pauseAll() { $header = ' pausedownload2 1 '; new Request('POST', $this->fullUrl.'pausedownload2', ['Content-Type' => 'text/xml; charset=UTF8'], $header); } /** * Resume (previously paused) download queue on server. This method is equivalent for command "nzbget -U". * * @return void */ public function resumeAll() { $header = ' resumedownload2 1 '; new Request('POST', $this->fullUrl.'resumedownload2', ['Content-Type' => 'text/xml; charset=UTF8'], $header); } /** * Pause a single NZB from the queue. * * @param string $id */ public function pauseFromQueue($id) { $header = ' editqueue GroupPause 0 "" '.$id.' '; new Request('POST', $this->fullUrl.'editqueue', ['Content-Type' => 'text/xml; charset=UTF8'], $header); } /** * Resume a single NZB from the queue. * * @param string $id */ public function resumeFromQueue($id) { $header = ' editqueue GroupResume 0 "" '.$id.' '; new Request('POST', $this->fullUrl.'editqueue', ['Content-Type' => 'text/xml; charset=UTF8'], $header); } /** * Delete a single NZB from the queue. * * @param string $id */ public function delFromQueue($id) { $header = ' editqueue GroupDelete 0 "" '.$id.' '; new Request('POST', $this->fullUrl.'editqueue', ['Content-Type' => 'text/xml; charset=UTF8'], $header); } /** * Set download speed limit. This method is equivalent for command "nzbget -R ". * * @param int $limit The speed to limit it to. * * @return void */ public function rate($limit) { $header = ' rate '.$limit.' '; new Request('POST', $this->fullUrl.'rate', ['Content-Type' => 'text/xml; charset=UTF8'], $header); } /** * Get all items in download queue. * * * @return array|false */ public function getQueue() { $data = $this->client->get($this->fullUrl.'listgroups')->getBody()->getContents(); $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|false The status. * @throws \RuntimeException */ public function status() { $data = $this->client->get($this->fullUrl.'status')->getBody()->getContents(); $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 */ public function verifyURL($url) { if (preg_match('/(?Phttps?):\/\/(?P.+?)(:(?P\d+\/)|\/)$/i', $url, $hits)) { return $hits['protocol'].'://'.$this->userName.':'.$this->password.'@'.$hits['url'].(isset($hits['port']) ? ':'.$hits['port'] : (substr($hits['url'], -1) === '/' ? '' : '/')).'xmlrpc/'; } return false; } }