uid = $page->userdata['id']; $this->api_token = $page->userdata['api_token']; $this->serverurl = url('/'); $this->client = new Client(['verify' => false]); // Set up properties. switch (Settings::settingValue('apps.sabnzbplus.integrationtype')) { case self::INTEGRATION_TYPE_USER: if (! empty($_COOKIE['sabnzbd_'.$this->uid.'__apikey']) && ! empty($_COOKIE['sabnzbd_'.$this->uid.'__host'])) { $this->url = $_COOKIE['sabnzbd_'.$this->uid.'__host']; $this->apikey = $_COOKIE['sabnzbd_'.$this->uid.'__apikey']; $this->priority = $_COOKIE['sabnzbd_'.$this->uid.'__priority'] ?? 0; $this->apikeytype = $_COOKIE['sabnzbd_'.$this->uid.'__apitype'] ?? 1; } elseif (! empty($page->userdata['sabapikey']) && ! empty($page->userdata['saburl'])) { $this->url = $page->userdata['saburl']; $this->apikey = $page->userdata['sabapikey']; $this->priority = $page->userdata['sabpriority']; $this->apikeytype = $page->userdata['sabapikeytype']; } $this->integrated = self::INTEGRATION_TYPE_USER; switch ((int) $page->userdata['queuetype']) { case 1: case 2: $this->integratedBool = true; break; default: $this->integratedBool = false; break; } break; case self::INTEGRATION_TYPE_NONE: $this->integrated = self::INTEGRATION_TYPE_NONE; // This is for nzbget. if ($page->userdata['queuetype'] === 2) { $this->integratedBool = true; } break; } // Verify the URL is good, fix it if not. if ($this->url !== '' && preg_match('/(?P\/)?(?P[a-z]+)?(?P\/)?$/i', $this->url, $hits)) { if (! isset($hits['first'])) { $this->url .= '/'; } if (! isset($hits['sab'])) { $this->url .= 'sabnzbd'; } elseif ($hits['sab'] !== 'sabnzbd') { $this->url .= 'sabnzbd'; } if (! isset($hits['last'])) { $this->url .= '/'; } } } /** * @param $guid * @return string * @throws \RuntimeException */ public function sendToSab($guid): string { return $this->client->post( $this->url. 'api?mode=addurl&priority='. $this->priority. '&apikey='. $this->apikey. '&name='. urlencode( $this->serverurl. 'getnzb?id='. $guid. '&r='. $this->api_token ) )->getBody()->getContents(); } /** * @return string * @throws \RuntimeException */ public function getAdvQueue(): string { return $this->client->get( $this->url. 'api?mode=queue&start=START&limit=LIMIT&output=json&apikey='. $this->apikey )->getBody()->getContents(); } /** * @return string * @throws \RuntimeException */ public function getHistory(): string { return $this->client->get( $this->url. 'api?mode=history&start=START&limit=LIMIT&category=CATEGORY&search=SEARCH&failed_only=0&output=json&apikey='. $this->apikey )->getBody()->getContents(); } /** * @param $id * @return string * @throws \RuntimeException */ public function delFromQueue($id): string { return $this->client->get( $this->url. 'api?mode=queue&name=delete&value='. $id. '&apikey='. $this->apikey )->getBody()->getContents(); } /** * @param $id * @return string * @throws \RuntimeException */ public function pauseFromQueue($id): string { return $this->client->get( $this->url. 'api?mode=queue&name=pause&value='. $id. '&apikey='. $this->apikey )->getBody()->getContents(); } /** * @param $id * @return string * @throws \RuntimeException */ public function resumeFromQueue($id) { return $this->client->get( $this->url. 'api?mode=queue&name=resume&value='. $id. '&apikey='. $this->apikey )->getBody()->getContents(); } /** * @return string * @throws \RuntimeException */ public function pauseAll(): string { return $this->client->get( $this->url. 'api?mode=pause'. '&apikey='. $this->apikey )->getBody()->getContents(); } /** * Resume all NZB's in the SAB queue. * * @return string * @throws \RuntimeException */ public function resumeAll(): string { return $this->client->get( $this->url. 'api?mode=resume'. '&apikey='. $this->apikey )->getBody()->getContents(); } /** * Check if the SAB cookies are in the User's browser. * * @return bool */ public function checkCookie() { $res = false; if (isset($_COOKIE['sabnzbd_'.$this->uid.'__apikey'])) { $res = true; } if (isset($_COOKIE['sabnzbd_'.$this->uid.'__host'])) { $res = true; } if (isset($_COOKIE['sabnzbd_'.$this->uid.'__priority'])) { $res = true; } if (isset($_COOKIE['sabnzbd_'.$this->uid.'__apitype'])) { $res = true; } return $res; } /** * Creates the SAB cookies for the user's browser. * * @param $host * @param $apikey * @param $priority * @param $apitype */ public function setCookie($host, $apikey, $priority, $apitype) { setcookie('sabnzbd_'.$this->uid.'__host', $host, now()->addDays(30)->timestamp); setcookie('sabnzbd_'.$this->uid.'__apikey', $apikey, now()->addDays(30)->timestamp); setcookie('sabnzbd_'.$this->uid.'__priority', $priority, now()->addDays(30)->timestamp); setcookie('sabnzbd_'.$this->uid.'__apitype', $apitype, now()->addDays(30)->timestamp); } /** * Deletes the SAB cookies from the user's browser. */ public function unsetCookie() { setcookie('sabnzbd_'.$this->uid.'__host', '', now()->subDays(30)->timestamp); setcookie('sabnzbd_'.$this->uid.'__apikey', '', now()->subDays(30)->timestamp); setcookie('sabnzbd_'.$this->uid.'__priority', '', now()->subDays(30)->timestamp); setcookie('sabnzbd_'.$this->uid.'__apitype', '', now()->subDays(30)->timestamp); } }