PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/services/NzbHandler/Services_NzbHandler_abs.php

http://github.com/spotweb/spotweb
PHP | 441 lines | 218 code | 92 blank | 131 comment | 9 complexity | e8da8f0a400c3e7174ad7739f6f520cd MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Apache-2.0, LGPL-3.0
  1. <?php
  2. abstract class Services_NzbHandler_abs
  3. {
  4. protected $_name = 'Abstract';
  5. protected $_nameShort = 'Abstract';
  6. protected $_nzbHandling = null;
  7. protected $_settings = null;
  8. public function __construct(Services_Settings_Container $settings, $name, $nameShort, array $nzbHandling)
  9. {
  10. $this->_settings = $settings;
  11. $this->_nzbHandling = $nzbHandling;
  12. $this->_name = $name;
  13. $this->_nameShort = $nameShort;
  14. }
  15. // __construct
  16. /**
  17. * Actually process the spot.
  18. *
  19. * @param $fullspot Array with fullspot information, needed for title and category
  20. * @param $nzblist array List of NZB's (or one) we need to process
  21. *
  22. * @return mixed
  23. */
  24. abstract public function processNzb($fullspot, $nzblist);
  25. /**
  26. * Get the name of the application handling the nzb, e.g. "SabNZBd".
  27. */
  28. public function getName()
  29. {
  30. return $this->_name;
  31. }
  32. // getName
  33. /**
  34. * Set the name of the application handling the nzb. This allows template
  35. * designers to adapt the application name if necessary.
  36. */
  37. public function setName($name)
  38. {
  39. $this->_name = $name;
  40. }
  41. // setName
  42. /**
  43. * Get the name of the application handling the nzb, e.g. "SAB".
  44. */
  45. public function getNameShort()
  46. {
  47. return $this->_nameShort;
  48. }
  49. // getNameShort
  50. /**
  51. * Set the short name of the application handling the nzb. This allows template
  52. * designers to adapt the application name if necessary.
  53. */
  54. public function setNameShort($name)
  55. {
  56. $this->_nameShort = $name;
  57. }
  58. // setNameShort
  59. public function generateNzbHandlerUrl($spot, $spotwebApiParam)
  60. {
  61. $spotwebUrl = $this->_settings->get('spotweburl');
  62. $action = $this->_nzbHandling['action'];
  63. $url = $spotwebUrl.'?page=getnzb&amp;action='.$action.'&amp;messageid='.$spot['messageid'].$spotwebApiParam;
  64. return $url;
  65. }
  66. // generateNzbHandlerUrl
  67. /*
  68. * Generates a clean filename with characters which are allowed
  69. * on most operating systems' filesystems
  70. */
  71. protected function cleanForFileSystem($title)
  72. {
  73. $allowedChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!,@#^()-=+ _{}';
  74. $newTitle = '';
  75. for ($i = 0; $i < strlen($title); $i++) {
  76. if (stripos($allowedChars, $title[$i]) === false) {
  77. $newTitle .= '_';
  78. } else {
  79. $newTitle .= $title[$i];
  80. }
  81. } // for
  82. return $newTitle;
  83. }
  84. // cleanForFileSystem
  85. /*
  86. * Generates the full path where the NZB files are to be stored
  87. */
  88. protected function makeNzbLocalPath($fullspot, $path)
  89. {
  90. $category = $this->convertCatToSabnzbdCat($fullspot);
  91. // add category to path when asked for
  92. $path = str_replace('$SABNZBDCAT', $this->cleanForFileSystem($category), $path);
  93. // make sure the path adds with a trailing slash so we can just append the filename to it
  94. $path = $this->addTrailingSlash($path);
  95. return $path;
  96. }
  97. // makeNzbLocalPath
  98. /*
  99. * Adds, when necessary, a trailing slash to the directory path
  100. */
  101. protected function addTrailingSlash($path)
  102. {
  103. // als de path niet eindigt met een backslash of forwardslash, voeg die zelf toe
  104. if (strpos('\/', $path[strlen($path) - 1]) === false) {
  105. $path .= DIRECTORY_SEPARATOR;
  106. } // if
  107. return $path;
  108. }
  109. // addTrailingSlash
  110. /**
  111. * Either compresses or merges the NZB files.
  112. *
  113. * @param $fullspot array with full spot information
  114. * @param $nzblist array list of nzb files we want to process
  115. *
  116. * @return array contains the meta data and the nzb itself
  117. */
  118. protected function prepareNzb($fullspot, $nzblist)
  119. {
  120. /*
  121. * Depending on the requested action, we make sure this NZB
  122. * file can be sent as one file as current download managers
  123. * cannot process more than one file in one request.
  124. */
  125. $result = [];
  126. switch ($this->_nzbHandling['prepare_action']) {
  127. case 'zip':
  128. $result['nzb'] = $this->zipNzbList($nzblist);
  129. $result['mimetype'] = 'application/x-zip-compressed';
  130. $result['filename'] = 'SpotWeb_'.microtime(true).'.zip';
  131. break;
  132. // zip
  133. default:
  134. $result['nzb'] = $this->mergeNzbList($nzblist);
  135. $result['mimetype'] = 'application/x-nzb';
  136. $result['filename'] = $this->cleanForFileSystem($fullspot['title']).'.nzb';
  137. break;
  138. // merge
  139. } // switch
  140. return $result;
  141. }
  142. // prepareNzb
  143. /*
  144. * Converts a Spot category into a category which can be
  145. * used for sabnzbd or other download manager
  146. */
  147. protected function convertCatToSabnzbdCat($spot)
  148. {
  149. // fix the category
  150. $spot['category'] = (int) $spot['category'];
  151. /*
  152. * Retrieve the list of categories, and we user the default
  153. * category per .. default
  154. */
  155. $sabnzbd = $this->_settings->get('sabnzbd');
  156. if (isset($sabnzbd['categories'][$spot['category']]['default'])) {
  157. $category = $sabnzbd['categories'][$spot['category']]['default'];
  158. } else {
  159. $category = '';
  160. } // else
  161. /*
  162. * If we find a better match, than use that one, but else we use the
  163. * default category defined
  164. */
  165. foreach (['a', 'b', 'c', 'd', 'z'] as $subcatType) {
  166. $subList = explode('|', $spot['subcat'.$subcatType]);
  167. foreach ($subList as $cat) {
  168. if (isset($sabnzbd['categories'][$spot['category']][$cat])) {
  169. $category = $sabnzbd['categories'][$spot['category']][$cat];
  170. } // if
  171. } // foreach
  172. } // foreach
  173. return $category;
  174. }
  175. // convertCatToSabnzbdCat
  176. /*
  177. * Merges a list of XML files into one
  178. */
  179. protected function mergeNzbList($nzbList)
  180. {
  181. $nzbXml = simplexml_load_string('<?xml version="1.0" encoding="iso-8859-1" ?>
  182. <!DOCTYPE nzb PUBLIC "-//newzBin//DTD NZB 1.0//EN" "http://www.newzbin.com/DTD/nzb/nzb-1.0.dtd">
  183. <nzb xmlns="http://www.newzbin.com/DTD/2003/nzb"></nzb>');
  184. $domNzbXml = dom_import_simplexml($nzbXml);
  185. foreach ($nzbList as $nzb) {
  186. $oneNzbFile = simplexml_load_string($nzb['nzb']);
  187. // add each file section to the larger XML object
  188. foreach ($oneNzbFile->file as $file) {
  189. // Import the file into the larger NZB object
  190. $domFile = $domNzbXml->ownerDocument->importNode(dom_import_simplexml($file), true);
  191. $domNzbXml->appendChild($domFile);
  192. } // foreach
  193. } // foreach
  194. return $nzbXml->asXml();
  195. }
  196. // mergeNzbList
  197. /*
  198. * Compresses the NZB files as one, so we can send the list of
  199. * NZB files as one.
  200. */
  201. protected function zipNzbList($nzbList)
  202. {
  203. $tmpZip = tempnam(sys_get_temp_dir(), 'SpotWebZip');
  204. $zip = new ZipArchive();
  205. $res = $zip->open($tmpZip, ZipArchive::CREATE);
  206. if ($res !== true) {
  207. throw new Exception('Unable to create temporary ZIP file: '.$res);
  208. } // if
  209. foreach ($nzbList as $nzb) {
  210. $zip->addFromString($this->cleanForFileSystem($nzb['spot']['title']).'.nzb', $nzb['nzb']);
  211. } // foreach
  212. $zip->close();
  213. /*
  214. * and read the ZIP file, so we can just process it as
  215. * opaque data
  216. */
  217. $zipFile = file_get_contents($tmpZip);
  218. // en wis de tijdelijke file
  219. unlink($tmpZip);
  220. return $zipFile;
  221. }
  222. // zipNzbList
  223. // NzbHandler API functions
  224. public function hasApiSupport()
  225. {
  226. return false;
  227. }
  228. // hasApiSupport
  229. public function getStatus()
  230. {
  231. // do nothing
  232. return false;
  233. }
  234. // getStatus
  235. public function isAvailable()
  236. {
  237. return true;
  238. }
  239. // isAvailable
  240. public function pauseQueue()
  241. {
  242. // do nothing
  243. return false;
  244. }
  245. //pauseQueue
  246. public function resumeQueue()
  247. {
  248. // do nothing
  249. return false;
  250. }
  251. // resumeQueue
  252. public function setSpeedLimit($limit)
  253. {
  254. // do nothing
  255. return false;
  256. }
  257. // setSpeedLimit
  258. public function moveDown($id)
  259. {
  260. // do nothing
  261. return false;
  262. }
  263. // moveDown
  264. public function moveUp($id)
  265. {
  266. // do nothing
  267. return false;
  268. }
  269. // moveUp
  270. public function moveTop($id)
  271. {
  272. // do nothing
  273. return false;
  274. }
  275. // moveTop
  276. public function moveBottom($id)
  277. {
  278. // do nothing
  279. return false;
  280. }
  281. // moveBottom
  282. public function setCategory($id, $category)
  283. {
  284. // do nothing
  285. return false;
  286. }
  287. // setCategory
  288. public function setPriority($id, $priority)
  289. {
  290. // do nothing
  291. return false;
  292. }
  293. // setPriority
  294. public function setPassword($id, $password)
  295. {
  296. // do nothing
  297. return false;
  298. }
  299. // setPassword
  300. public function delete($id)
  301. {
  302. // do nothing
  303. return false;
  304. }
  305. // delete
  306. public function rename($id, $name)
  307. {
  308. // do nothing
  309. return false;
  310. }
  311. // rename
  312. public function pause($id)
  313. {
  314. // do nothing
  315. return false;
  316. }
  317. // pause
  318. public function resume($id)
  319. {
  320. // do nothing
  321. return false;
  322. }
  323. // resume
  324. public function getBuiltinCategories()
  325. {
  326. /*
  327. * For NzbHandlers that do not use configurable categories, but simply create
  328. * category directories on demand (e.g. NZBGet) we'll just use the categories
  329. * that are configured in SpotWeb.
  330. */
  331. $sabnzbd = $this->_settings->get('sabnzbd');
  332. $allcategories = [];
  333. foreach ($sabnzbd['categories'] as $categories) {
  334. $allcategories = array_merge($allcategories, array_values($categories));
  335. }
  336. $allcategories = array_unique($allcategories);
  337. $result = [];
  338. $result['readonly'] = true; // inform the GUI to not allow adding of adhoc categories
  339. $result['categories'] = $allcategories;
  340. return $result;
  341. }
  342. // getBuiltinCategories
  343. public function getVersion()
  344. {
  345. // do nothing
  346. return false;
  347. }
  348. // getVersion
  349. } // class Services_NzbHandler_abs