File: NzbHandler_abs.php

package info (click to toggle)
spotweb 20130826%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,132 kB
  • ctags: 11,281
  • sloc: php: 31,367; xml: 1,009; sh: 148; makefile: 83
file content (355 lines) | stat: -rwxr-xr-x 8,400 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
abstract class NzbHandler_abs
{
	protected $_name = "Abstract";
	protected $_nameShort = "Abstract";

	protected $_nzbHandling = null;
	protected $_settings = null;

	function __construct(SpotSettings $settings, $name, $nameShort, array $nzbHandling)
	{
		$this->_settings = $settings;
		$this->_nzbHandling = $nzbHandling;
		$this->_name = $name;
		$this->_nameShort = $nameShort;
	} # __construct

	/**
	 * Get the name of the application handling the nzb, e.g. "SabNZBd".
	 */
	public function getName()
	{
		return $this->_name;
	} # getName

	/**
	 * Set the name of the application handling the nzb. This allows template
	 * designers to adapt the application name if necessary
	 */
	public function setName($name)
	{
		$this->_name = $name;
	} # setName

	/**
	 * Get the name of the application handling the nzb, e.g. "SAB".
	 */
	public function getNameShort()
	{
		return $this->_nameShort;
	} # getNameShort

	/**
	 * Set the short name of the application handling the nzb. This allows template
	 * designers to adapt the application name if necessary
	 */

	public function setNameShort($name)
	{
		$this->_nameShort = $name;
	} # setNameShort

	abstract public function processNzb($fullspot, $nzblist);

	public function generateNzbHandlerUrl($spot, $spotwebApiParam)
	{
		$spotwebUrl = $this->_settings->get('spotweburl');
		$action = $this->_nzbHandling['action'];
		$url = $spotwebUrl . '?page=getnzb&amp;action=' . $action . '&amp;messageid=' . $spot['messageid'] . $spotwebApiParam;

		return $url;
	} # generateNzbHandlerUrl

	/*
	 * Genereert een schone filename voor nzb files
	 */
	protected function cleanForFileSystem($title)
	{
		$allowedChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!,@#^()-=+ _{}';
		$newTitle = '';

		for($i = 0; $i < strlen($title); $i++) {
			if (stripos($allowedChars, $title[$i]) === false) {
				$newTitle .= '_';
			} else {
				$newTitle .= $title[$i];
			}
		} # for

		return $newTitle;
	} # cleanForFileSystem

	/*
	 * Genereert het volledige path naar de NZB locatie waar files opgeslagen moeten worden
	 */
	protected function makeNzbLocalPath($fullspot, $path)
	{
		$category = $this->convertCatToSabnzbdCat($fullspot);

		# add category to path als dat gevraagd is
		$path = str_replace('$SABNZBDCAT', $this->cleanForFileSystem($category), $path);

		# als de path niet eindigt met een backslash of forwardslash, voeg die zelf toe
		$path = $this->addTrailingSlash($path);

		return $path;
	} # makeNzbLocalPath

	/*
	 * Voegt, indien nodig, een trailing slash toe
	 */
	protected function addTrailingSlash($path)
	{
		# als de path niet eindigt met een backslash of forwardslash, voeg die zelf toe
		if (strpos('\/', $path[strlen($path) - 1]) === false) {
			$path .= DIRECTORY_SEPARATOR;
		} # if

		return $path;
	} # addTrailingSlash

	protected function sendHttpRequest($method, $url, $header, $content, $timeout = 15, $userAgent = 'Spotweb')
	{
		$stream_options = array('http' =>
			array('timeout' => $timeout,
					'method' => $method,
					'user_agent' => 'Spotweb',
					'header' => $header,
					'content' => $content));

		$ctx = stream_context_create($stream_options);

		return @file_get_contents($url, false, $ctx);
	} # sendHttpRequest

	protected function prepareNzb($fullspot, $nzblist)
	{
		# nu we alle nzb files hebben, trekken we de 'file' secties eruit,
		# en plakken die in onze overkoepelende nzb
		$result = array();
		switch($this->_nzbHandling['prepare_action'])
		{
			case 'zip'	: {
				$result['nzb'] = $this->zipNzbList($nzblist);
				$result['mimetype'] = 'application/x-zip-compressed';
				$result['filename'] = 'SpotWeb_' . microtime(true) . '.zip';
				break;
			} # zip

			default 		: {
				$result['nzb'] = $this->mergeNzbList($nzblist);
				$result['mimetype'] = 'application/x-nzb';
				$result['filename'] = $this->cleanForFileSystem($fullspot['title']) . '.nzb';
				break;
			} # merge
		} # switch

		return $result;
	} # prepareNzb

	/*
	 * Zet een Spot category om naar een sabnzbd category
	 */
	protected function convertCatToSabnzbdCat($spot) {
		# fix de category
		$spot['category'] = (int) $spot['category'];

		# vind een geschikte category
		$sabnzbd = $this->_settings->get('sabnzbd');

		if (isset($sabnzbd['categories'][$spot['category']]['default'])) {
			$category = $sabnzbd['categories'][$spot['category']]['default'];
		} else {
			$category = '';
		} # else

		foreach($spot['subcatlist'] as $cat) {
			if (isset($sabnzbd['categories'][$spot['category']][$cat])) {
				$category = $sabnzbd['categories'][$spot['category']][$cat];
			} # if
		} # foreach

		return $category;
	} # convertCatToSabnzbdCat

	/*
	 * Voeg een lijst van NZB XML files samen tot 1 XML file
	 */
	protected function mergeNzbList($nzbList) {
		$nzbXml = simplexml_load_string('<?xml version="1.0" encoding="iso-8859-1" ?>
											<!DOCTYPE nzb PUBLIC "-//newzBin//DTD NZB 1.0//EN" "http://www.newzbin.com/DTD/nzb/nzb-1.0.dtd">
											<nzb xmlns="http://www.newzbin.com/DTD/2003/nzb"></nzb>');

		$domNzbXml = dom_import_simplexml($nzbXml);
		foreach($nzbList as $nzb) {
			$oneNzbFile = simplexml_load_string($nzb['nzb']);

			# add each file section to the larger XML object
			foreach($oneNzbFile->file as $file) {
				# Import the file into the larger NZB object
				$domFile = $domNzbXml->ownerDocument->importNode(dom_import_simplexml($file), TRUE);
				$domNzbXml->appendChild($domFile);
			} # foreach
		} # foreach
		return $nzbXml->asXml();
	} # mergeNzbList

	/*
	 * Stop de lijst van NZB XML files in 1 zip file
	 */
	protected function zipNzbList($nzbList) {
		$tmpZip = tempnam(sys_get_temp_dir(), 'SpotWebZip');
		$zip = new ZipArchive;
		$res = $zip->open($tmpZip, ZipArchive::CREATE);
		if ($res !== TRUE) {
			throw new Exception("Unable to create temporary ZIP file: " . $res);
		} # if

		foreach($nzbList as $nzb) {
			$zip->addFromString($this->cleanForFileSystem($nzb['spot']['title']) . '.nzb', $nzb['nzb']);
		} # foreach
		$zip->close();

		# lees de tempfile uit
		$zipFile = file_get_contents($tmpZip);

		# en wis de tijdelijke file
		unlink($tmpZip);

		return $zipFile;
	} # zipNzbList

	# NzbHandler API functions
	public function hasApiSupport()
	{
		return false;
	} # hasApiSupport

	public function getStatus()
	{
		# do nothing
		return false;
	} # getStatus

        public function isAvailable()
        {
            return true;
        } # isAvailable

	public function pauseQueue()
	{
		# do nothing
		return false;
	} #pauseQueue

	public function resumeQueue()
	{
		# do nothing
		return false;
	} # resumeQueue

	public function setSpeedLimit(int $limit)
	{
		# do nothing
		return false;
	} # setSpeedLimit

	public function moveDown($id)
	{
		# do nothing
		return false;
	} # moveDown

	public function moveUp($id)
	{
		# do nothing
		return false;
	} # moveUp

	public function moveTop($id)
	{
		# do nothing
		return false;
	} # moveTop

	public function moveBottom($id)
	{
		# do nothing
		return false;
	} # moveBottom

	public function setCategory($id, $category)
	{
		# do nothing
		return false;
	} # setCategory

	public function setPriority($id, $priority)
	{
		# do nothing
		return false;
	} # setPriority

	public function setPassword($id, $password)
	{
		# do nothing
		return false;
	} # setPassword

	public function delete($id)
	{
		# do nothing
		return false;
	} # delete

	public function rename($id, $name)
	{
		# do nothing
		return false;
	} # rename

	public function pause($id)
	{
		# do nothing
		return false;
	} # pause

	public function resume($id)
	{
		# do nothing
		return false;
	} # resume

	public function getCategories()
	{
		# For NzbHandlers that do not use configurable categories, but simply create
		# category directories on demand (e.g. NZBGet) we'll just use the categories
		# that are configured in SpotWeb.

		$sabnzbd = $this->_settings->get('sabnzbd');

		$allcategories = array();
		foreach($sabnzbd['categories'] as $categories)
		{
			$allcategories = array_merge($allcategories, array_values($categories));
		}

		$allcategories = array_unique($allcategories);

		$result = array();
		$result['readonly'] = true;	// inform the GUI to not allow adding of adhoc categories
		$result['categories'] = $allcategories;

		return $result;
	} # getCategories


	public function getVersion()
	{
		# do nothing
		return false;
	} # getVersion

} # class NzbHandler_abs