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 Enzebe();
$this->util = new Utility();
}
/**
* Send a NZB to NZBGet.
*
* @param string $guid Release identifier.
*
* @return bool|mixed
*
* @access public
*/
public function sendNZBToNZBGet($guid)
{
$reldata = $this->Releases->getByGuid($guid);
$nzbpath = $this->NZB->getNZBPath($guid);
$string = '';
$nzb = @gzopen($nzbpath, 'rb', 0);
if ($nzb) {
while (!gzeof($nzb)) {
$string .= gzread($nzb, 1024);
}
gzclose($nzb);
}
$header =
'
append
' . $reldata['searchname'] . '
' . $reldata['category_name'] . '
0
>False
' .
base64_encode($string) .
'
';
$this->util->getUrl($this->fullURL . 'append', 'post', $header);
}
/**
* 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
.
'
';
$this->util->getUrl($this->fullURL . 'appendurl', 'post', $header);
}
/**
* Pause download queue on server. This method is equivalent for command "nzbget -P".
*
* @return void
*
* @access public
*/
public function pauseAll()
{
$header =
'
pausedownload2
1
';
$this->util->getUrl($this->fullURL . 'pausedownload2', 'post', $header);
}
/**
* 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
';
$this->util->getUrl($this->fullURL . 'resumedownload2', 'post', $header);
}
/**
* Pause a single NZB from the queue.
*
* @param string $id
*
* @access public
*/
public function pauseFromQueue($id)
{
$header =
'
editqueue
GroupPause
0
""
' . $id . '
';
$this->util->getUrl($this->fullURL . 'editqueue', 'post', $header);
}
/**
* Resume a single NZB from the queue.
*
* @param string $id
*
* @access public
*/
public function resumeFromQueue($id)
{
$header =
'
editqueue
GroupResume
0
""
' . $id . '
';
$this->util->getUrl($this->fullURL . 'editqueue', 'post', $header);
}
/**
* Delete a single NZB from the queue.
*
* @param string $id
*
* @access public
*/
public function delFromQueue($id)
{
$header =
'
editqueue
GroupDelete
0
""
' . $id . '
';
$this->util->getUrl($this->fullURL . 'editqueue', 'post', $header);
}
/**
* 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 . '
';
$this->util->getUrl($this->fullURL . 'rate', 'post', $header);
}
/**
* Get all items in download queue.
*
* @return array|bool
*
* @access public
*/
public function getQueue()
{
$data = $this->util->getUrl($this->fullURL . 'listgroups');
$retVal = false;
if ($data) {
$xml = simplexml_load_string($data);
if ($xml) {
$retVal = array();
$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 = $this->util->getUrl($this->fullURL . 'status');
$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'] : '') .
'xmlrpc/';
} else {
return false;
}
}
}