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->pdo = new DB();
$this->NZB = new NZB($this->pdo);
$this->client = new Client();
}
/**
* Send a NZB to NZBGet.
*
* @param string $guid Release identifier.
*
* @return bool|mixed
*/
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).
'
';
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 = $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
.
'
';
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 bool
*/
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|bool
*/
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|bool The status.
*/
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, $matches)) {
return
$matches['protocol'].
'://'.
$this->userName.
':'.
$this->password.
'@'.
$matches['url'].
(isset($matches['port']) ? ':'.$matches['port'] : (substr($matches['url'], -1) === '/' ? '' : '/')).
'xmlrpc/';
} else {
return false;
}
}
}