PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/nzbhandler/NzbHandler_Pushsabnzbd.php

http://github.com/spotweb/spotweb
PHP | 407 lines | 223 code | 72 blank | 112 comment | 26 complexity | cd890be0fbec71a49c9f4f728f2bb3c5 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Apache-2.0, LGPL-3.0
  1. <?php
  2. define("SABNZBD_TIMEOUT",15);
  3. class NzbHandler_Pushsabnzbd extends NzbHandler_abs
  4. {
  5. private $_url = null;
  6. private $_sabnzbd = null;
  7. function __construct(SpotSettings $settings, array $nzbHandling)
  8. {
  9. parent::__construct($settings, 'SABnzbd', 'SAB', $nzbHandling);
  10. $sabnzbd = $nzbHandling['sabnzbd'];
  11. $this->_sabnzbd = $sabnzbd;
  12. # prepare sabnzbd url
  13. $this->_url = $sabnzbd['url'] . 'sabnzbd/api?mode=addfile&apikey=' . $sabnzbd['apikey'] . '&output=text';
  14. } # __construct
  15. public function processNzb($fullspot, $nzblist)
  16. {
  17. $nzb = $this->prepareNzb($fullspot, $nzblist);
  18. $title = urlencode($this->cleanForFileSystem($fullspot['title']));
  19. $category = urlencode($this->convertCatToSabnzbdCat($fullspot));
  20. # yes, using a local variable instead of the member variable is intentional
  21. $url = $this->_url . '&nzbname=' . $title . '&cat=' . $category;
  22. @define('MULTIPART_BOUNDARY', '--------------------------'.microtime(true));
  23. # equivalent to <input type="file" name="nzbfile"/>
  24. @define('FORM_FIELD', 'nzbfile');
  25. # dit is gecopieerd van:
  26. # http://stackoverflow.com/questions/4003989/upload-a-file-using-file-get-contents
  27. # creeer de header
  28. $header = array('Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY);
  29. # bouw nu de content
  30. $content = "--" . MULTIPART_BOUNDARY . "\r\n";
  31. $content .=
  32. "Content-Disposition: form-data; name=\"" . FORM_FIELD . "\"; filename=\"" . $nzb['filename'] . "\"\r\n" .
  33. "Content-Type: " . $nzb['mimetype'] . "\r\n\r\n" .
  34. $nzb['nzb'] ."\r\n";
  35. # signal end of request (note the trailing "--")
  36. $content .= "--".MULTIPART_BOUNDARY."--\r\n";
  37. $output = $this->sendHttpRequest('POST', $url, $header, $content, SABNZBD_TIMEOUT);
  38. if ($output === false)
  39. {
  40. error_log("Unable to open sabnzbd url: " . $url);
  41. throw new Exception("Unable to open sabnzbd url: " . $url);
  42. } # if
  43. if (strtolower(trim($output)) != 'ok')
  44. {
  45. error_log("sabnzbd returned: " . $output);
  46. throw new Exception("sabnzbd returned: " . $output);
  47. } # if
  48. } # processNzb
  49. # --------------------------
  50. # - NzbHandler API methods -
  51. # --------------------------
  52. /*
  53. * Return the supported API functions for this NzbHandler imlementation
  54. */
  55. public function hasApiSupport()
  56. {
  57. $api = "getStatus,pauseQueue,resumeQueue,setSpeedLimit,moveDown,moveUp"
  58. + ",moveTop,moveBottom,setCategory,setPriority,delete,rename"
  59. + ",pause,resume,getVersion";
  60. return $api;
  61. } # hasApiSupport
  62. /*
  63. * SABnzbd API method: queue
  64. * The getStatus() method returns a JSON object containing the following
  65. * name/value pairs:
  66. *
  67. * queue.status
  68. * queue.paused
  69. * queue.speedlimit
  70. * queue.freediskspace
  71. * queue.totaldiskspace
  72. * queue.bytepersec
  73. * queue.secondsremaining
  74. * queue.mbremaining
  75. * queue.nrofdownloads
  76. * download[].paused
  77. * download[].id
  78. * download[].filename
  79. * download[].category
  80. * download[].mbsize
  81. * download[].mbremaining
  82. * download[].percentage
  83. */
  84. public function getStatus()
  85. {
  86. $output = $this->querySabnzbd("mode=queue&output=json");
  87. $response = json_decode($output, true);
  88. $status = $response['queue'];
  89. $result = array();
  90. $result['queue']['status'] = $status['status'];
  91. $result['queue']['paused'] = $status['paused'];
  92. $result['queue']['speedlimit'] = (int)$status['speedlimit'];
  93. $result['queue']['freediskspace'] = $status['diskspace2'];
  94. $result['queue']['totaldiskspace'] = $status['diskspacetotal2'];
  95. $result['queue']['bytepersec'] = (int)($status['kbpersec'] * 1024);
  96. $result['queue']['mbsize'] = $status['mb'];
  97. $result['queue']['mbremaining'] = $status['mbleft'];
  98. $timeleft = explode(":", $status['timeleft']);
  99. $secondsremaining = $timeleft[0] * 3600 + $timeleft[1] * 60 + $timeleft[2];
  100. $result['queue']['secondsremaining'] = $secondsremaining;
  101. $slots = $status['slots'];
  102. $downloads = array();
  103. for ($i = 0; $i < count($slots); $i++)
  104. {
  105. $downloads[$i]['paused'] = ($slots[$i]['status'] == 'Paused');
  106. $downloads[$i]['id'] = $slots[$i]['nzo_id'];
  107. $downloads[$i]['filename'] = $slots[$i]['filename'];
  108. $downloads[$i]['category'] = $slots[$i]['cat'];
  109. $downloads[$i]['mbsize'] = $slots[$i]['mb'];
  110. $downloads[$i]['mbremaining'] = $slots[$i]['mbleft'];
  111. $downloads[$i]['percentage'] = $slots[$i]['percentage'];
  112. }
  113. $result['queue']['slots'] = $downloads;
  114. $result['queue']['nrofdownloads'] = count($downloads);
  115. return $result;
  116. } # status
  117. /*
  118. * SABnzbd API method: pause
  119. * Pause the download queue
  120. */
  121. public function pauseQueue()
  122. {
  123. $output = $this->querySabnzbd("mode=pause&output=json");
  124. $response = json_decode($output, true);
  125. return ($response['status'] == true);
  126. } # pauseQueue
  127. /*
  128. * SABnzbd API method: resume
  129. * Resume the download queue when paused
  130. */
  131. public function resumeQueue()
  132. {
  133. $output = $this->querySabnzbd("mode=resume&output=json");
  134. $response = json_decode($output, true);
  135. return ($response['status'] == true);
  136. } # resumeQueue
  137. /*
  138. * SABnzbd API method: config
  139. * Set the maximum download rate
  140. */
  141. public function setSpeedLimit($limit)
  142. {
  143. $output = $this->querySabnzbd("mode=config&name=speedlimit&value=" . $limit . "&output=json");
  144. $response = json_decode($output, true);
  145. return ($response['status'] == true);
  146. } # setSpeedLimit
  147. /*
  148. * SABnzbd API method: switch
  149. * Move a download one position down in the queue
  150. */
  151. public function moveDown($id)
  152. {
  153. $output = $this->querySabnzbd("mode=queue&output=json");
  154. $response = json_decode($output, true);
  155. $slots = $response['queue']['slots'];
  156. $totaldownloads = count($slots);
  157. $position = -1;
  158. for ($i = 0; $i < $totaldownloads; $i++)
  159. {
  160. if ($slots[$i]['nzo_id'] == $id)
  161. {
  162. $index = $slots[$i]['index'];
  163. if ($index <= ($totaldownloads - 1)) // we can't go lower than the bottom of the queue
  164. {
  165. $output = $this->querySabnzbd("mode=switch&value=" . $id . "&value2=" . ($index+1));
  166. $response = json_decode($output, true);
  167. $position = $response['result']['position'];
  168. }
  169. break; // we're done, exit loop
  170. }
  171. }
  172. return ($position != -1);
  173. } # moveDown
  174. /*
  175. * SABnzbd API method: switch
  176. * Move a download one position up in the queue
  177. */
  178. public function moveUp($id)
  179. {
  180. $output = $this->querySabnzbd("mode=queue&output=json");
  181. $response = json_decode($output, true);
  182. $status = $response['queue'];
  183. $slots = $status['slots'];
  184. $totaldownloads = count($slots);
  185. $position = -1;
  186. for ($i = 0; $i < $totaldownloads; $i++)
  187. {
  188. if ($slots[$i]['nzo_id'] == $id)
  189. {
  190. $index = $slots[$i]['index'];
  191. if ($index >= 0) // we can't go beyond the top of the queue
  192. {
  193. $output = $this->querySabnzbd("mode=switch&value=" . $id . "&value2=" . ($index-1));
  194. $response = json_decode($output, true);
  195. $position = $response['result']['position'];
  196. }
  197. break; // we're done, exit loop
  198. }
  199. }
  200. return ($position != -1);
  201. } # moveUp
  202. /*
  203. * SABnzbd API method: switch
  204. * Move a download to the top of the queue
  205. */
  206. public function moveTop($id)
  207. {
  208. $output = $this->querySabnzbd("mode=switch&value=" . $id . "&value2=0");
  209. $response = json_decode($output, true);
  210. return ($response['result']['position'] == 0);
  211. } # moveTop
  212. /*
  213. * SABnzbd API method: switch
  214. * Move a download to the bottom of the queue
  215. */
  216. public function moveBottom($id)
  217. {
  218. $output = $this->querySabnzbd("mode=switch&value=" . $id . "&value2=-1");
  219. $response = json_decode($output, true);
  220. # Sabnzbd returns the new position. So to verify whether or not the call was successful
  221. # we'd need to first query the number of items that are in the queue. This seems to be
  222. # a bit over overkill so we'll just asume that the call was successful
  223. return true;
  224. } # moveBottom
  225. /*
  226. * SABnzbd API method: change_cat
  227. * Set the category for a download
  228. */
  229. public function setCategory($id, $category)
  230. {
  231. $category = encodeurl($category);
  232. $output = $this->querySabnzbd("mode=change_cat&value=" .$id . "&value2=" . $category);
  233. $response = json_decode($output, true);
  234. return ($response['status'] == true);
  235. } # setCategory
  236. /*
  237. * SABnzbd API method: queue
  238. * Set the priority for a download
  239. * Low Priority: -1
  240. * Normal Priority: 0
  241. * High Priority: 1
  242. */
  243. public function setPriority($id, $priority)
  244. {
  245. if (($priority < -1) || ($priority > 1)) return false;
  246. $output = $this->querySabnzbd("mode=queue&name=priority&value=" . $id . "&value2=" . $priority);
  247. $response = json_decode($output, true);
  248. # Sabnzbd returns the new position, so we simply return true.
  249. return true;
  250. } # setPriority
  251. /*
  252. * SABnzbd API method: -
  253. * Not implemented yet.
  254. */
  255. public function setPassword($id, $password)
  256. {
  257. return false;
  258. } # setPassword
  259. /*
  260. * SABnzbd API method: queue
  261. * Delete a download from the queue
  262. */
  263. public function delete($id)
  264. {
  265. $output = $this->querySabnzbd("mode=queue&name=delete&value=" . $id . "&output=json");
  266. $response = json_decode($output, true);
  267. return ($response['status'] == true);
  268. } # delete
  269. /*
  270. * SABnzbd API method: queue
  271. * Rename a download
  272. */
  273. public function rename($id, $name)
  274. {
  275. $name = urlencode($name);
  276. $output = $this->querySabnzbd("mode=queue&name=rename&value=" . $id . "&value2=" . $name);
  277. $response = json_decode($output, true);
  278. return ($response['status'] == true);
  279. } # rename
  280. /*
  281. * SABnzbd API method: queue
  282. * Pause a download in the queue
  283. */
  284. public function pause($id)
  285. {
  286. $output = $this->querySabnzbd("mode=queue&name=pause&value=" . $id);
  287. $response = json_decode($output, true);
  288. return ($response['status'] == true);
  289. } # pause
  290. /*
  291. * SABnzbd API method: queue
  292. * Resume a paused download in the queue
  293. */
  294. public function resume($id)
  295. {
  296. $output = $this->querySabnzbd("mode=queue&name=resume&value=" . $id);
  297. $response = json_decode($output, true);
  298. return ($response['status'] == true);
  299. } # resume
  300. /*
  301. * SABnzbd API method: queue
  302. * Get list of categories from SABnzbd
  303. * The 'readonly' name/value pair is set to true to inform a template that SABnzbd
  304. * does not support assigning ad-hoc categories.
  305. */
  306. public function getCategories()
  307. {
  308. $output = $this->querySabnzbd("mode=queue&output=json");
  309. $response = json_decode($output, true);
  310. $categories = $response['queue']['categories'];
  311. $result = array();
  312. $result['readonly'] = true; // inform the GUI to not allow adding of adhoc categories
  313. $result['categories'] = $categories;
  314. return $result;
  315. } # getCategories
  316. /*
  317. * SABnzbd API method: version
  318. * Returns the version of SABnzbd
  319. */
  320. public function getVersion()
  321. {
  322. $output = $this->querySabnzbd("mode=version&output=json");
  323. $response = json_decode($output, true);
  324. return $response['version'];
  325. } # getVersion
  326. /*
  327. * Method used to query the SABnzbd API methods
  328. */
  329. private function querySabnzbd($request)
  330. {
  331. $url = $this->_sabnzbd['url'] . "sabnzbd/api?" . $request . '&apikey=' . $this->_sabnzbd['apikey'] . '&output=json';
  332. $output = @file_get_contents($url);
  333. return $output;
  334. } # querySabnzbd
  335. } # class NzbHandler_Pushsabnzbd