瀏覽代碼

Merge pull request #1897 from rix1337/v2-master

Implement JDownloader Homepage Basic Auth #1830
causefx 3 年之前
父節點
當前提交
5896bf31ae
共有 2 個文件被更改,包括 148 次插入132 次删除
  1. 2 0
      api/config/default.php
  2. 146 132
      api/homepage/jdownloader.php

+ 2 - 0
api/config/default.php

@@ -129,6 +129,8 @@ return [
 	'sickrageDisableCertCheck' => false,
 	'sickrageUseCustomCertificate' => false,
 	'jdownloaderURL' => '',
+	'jdownloaderUsername' => '',
+	'jdownloaderPassword' => '',
 	'jdownloaderCombine' => false,
 	'jdownloaderRefresh' => '60000',
 	'jdownloaderUseCustomCertificate' => false,

+ 146 - 132
api/homepage/jdownloader.php

@@ -2,23 +2,23 @@
 
 trait JDownloaderHomepageItem
 {
-	public function jDownloaderSettingsArray($infoOnly = false)
-	{
-		$homepageInformation = [
-			'name' => 'JDownloader',
-			'enabled' => strpos('personal', $this->config['license']) !== false,
-			'image' => 'plugins/images/tabs/jdownloader.png',
-			'category' => 'Downloader',
-			'settingsArray' => __FUNCTION__
-		];
-		if ($infoOnly) {
-			return $homepageInformation;
-		}
-		$homepageSettings = [
-			'debug' => true,
-			'settings' => [
-				'FYI' => [
-					$this->settingsOption('html', null, ['override' => 12, 'html' => '
+    public function jDownloaderSettingsArray($infoOnly = false)
+    {
+        $homepageInformation = [
+            'name' => 'JDownloader',
+            'enabled' => strpos('personal', $this->config['license']) !== false,
+            'image' => 'plugins/images/tabs/jdownloader.png',
+            'category' => 'Downloader',
+            'settingsArray' => __FUNCTION__
+        ];
+        if ($infoOnly) {
+            return $homepageInformation;
+        }
+        $homepageSettings = [
+            'debug' => true,
+            'settings' => [
+                'FYI' => [
+                    $this->settingsOption('html', null, ['override' => 12, 'html' => '
 						<div class="row">
 							<div class="col-lg-12">
 								<div class="panel panel-info">
@@ -36,79 +36,87 @@ trait JDownloaderHomepageItem
 								</div>
 							</div>
 						</div>']
-					),
-				],
-				'Enable' => [
-					$this->settingsOption('enable', 'homepageJdownloaderEnabled'),
-					$this->settingsOption('auth', 'homepageJdownloaderAuth'),
-				],
-				'Connection' => [
-					$this->settingsOption('url', 'jdownloaderURL'),
-					$this->settingsOption('blank'),
-					$this->settingsOption('disable-cert-check', 'jdownloaderDisableCertCheck'),
-					$this->settingsOption('use-custom-certificate', 'jdownloaderUseCustomCertificate'),
-				],
-				'Misc Options' => [
-					$this->settingsOption('refresh', 'jdownloaderRefresh'),
-					$this->settingsOption('combine', 'jdownloaderCombine'),
-				],
-				'Test Connection' => [
-					$this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
-					$this->settingsOption('test', 'jdownloader'),
-				]
-			]
-		];
-		return array_merge($homepageInformation, $homepageSettings);
-	}
+                    ),
+                ],
+                'Enable' => [
+                    $this->settingsOption('enable', 'homepageJdownloaderEnabled'),
+                    $this->settingsOption('auth', 'homepageJdownloaderAuth'),
+                ],
+                'Connection' => [
+                    $this->settingsOption('url', 'jdownloaderURL'),
+                    $this->settingsOption('url', 'jdownloaderUsername'),
+                    $this->settingsOption('url', 'jdownloaderPassword'),
+                    $this->settingsOption('blank'),
+                    $this->settingsOption('disable-cert-check', 'jdownloaderDisableCertCheck'),
+                    $this->settingsOption('use-custom-certificate', 'jdownloaderUseCustomCertificate'),
+                ],
+                'Misc Options' => [
+                    $this->settingsOption('refresh', 'jdownloaderRefresh'),
+                    $this->settingsOption('combine', 'jdownloaderCombine'),
+                ],
+                'Test Connection' => [
+                    $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
+                    $this->settingsOption('test', 'jdownloader'),
+                ]
+            ]
+        ];
+        return array_merge($homepageInformation, $homepageSettings);
+    }
 
-	public function testConnectionJDownloader()
-	{
-		if (empty($this->config['jdownloaderURL'])) {
-			$this->setAPIResponse('error', 'JDownloader URL is not defined', 422);
-			return false;
-		}
-		$url = $this->qualifyURL($this->config['jdownloaderURL']);
-		try {
-			$options = $this->requestOptions($url, $this->config['jdownloaderRefresh'], $this->config['jdownloaderDisableCertCheck'], $this->config['jdownloaderUseCustomCertificate']);
-			$response = Requests::get($url, [], $options);
-			if ($response->success) {
-				$this->setAPIResponse('success', 'API Connection succeeded', 200);
-				return true;
-			} else {
-				$this->setAPIResponse('success', 'JDownloader Error Occurred', 500);
-				return false;
-			}
-		} catch (Requests_Exception $e) {
-			$this->setLoggerChannel('JDownloader')->error($e);
-			$this->setResponse(500, $e->getMessage());
-			return false;
-		}
-	}
+    public function testConnectionJDownloader()
+    {
+        if (empty($this->config['jdownloaderURL'])) {
+            $this->setAPIResponse('error', 'JDownloader URL is not defined', 422);
+            return false;
+        }
+        if (!empty($this->config['jdownloaderUsername']) || !empty($this->config['jdownloaderPassword'])) {
+            $authHeader = 'Authorization: Basic ' . base64_encode($this->config['jdownloaderUsername'] . ':' . $this->config['jdownloaderPassword']);
+            $headers = array('headers' => array($authHeader));
+        } else {
+            $headers = [];
+        }
+        $url = $this->qualifyURL($this->config['jdownloaderURL']);
+        try {
+            $options = $this->requestOptions($url, $this->config['jdownloaderRefresh'], $this->config['jdownloaderDisableCertCheck'], $this->config['jdownloaderUseCustomCertificate']);
+            $response = Requests::get($url, $headers, $options);
+            if ($response->success) {
+                $this->setAPIResponse('success', 'API Connection succeeded', 200);
+                return true;
+            } else {
+                $this->setAPIResponse('success', 'JDownloader Error Occurred', 500);
+                return false;
+            }
+        } catch (Requests_Exception $e) {
+            $this->setLoggerChannel('JDownloader')->error($e);
+            $this->setResponse(500, $e->getMessage());
+            return false;
+        }
+    }
 
-	public function jDownloaderHomepagePermissions($key = null)
-	{
-		$permissions = [
-			'main' => [
-				'enabled' => [
-					'homepageJdownloaderEnabled'
-				],
-				'auth' => [
-					'homepageJdownloaderAuth'
-				],
-				'not_empty' => [
-					'jdownloaderURL'
-				]
-			]
-		];
-		return $this->homepageCheckKeyPermissions($key, $permissions);
-	}
+    public function jDownloaderHomepagePermissions($key = null)
+    {
+        $permissions = [
+            'main' => [
+                'enabled' => [
+                    'homepageJdownloaderEnabled'
+                ],
+                'auth' => [
+                    'homepageJdownloaderAuth'
+                ],
+                'not_empty' => [
+                    'jdownloaderURL'
+                ]
+            ]
+        ];
+        return $this->homepageCheckKeyPermissions($key, $permissions);
+    }
 
-	public function homepageOrderjdownloader()
-	{
-		if ($this->homepageItemPermissions($this->jDownloaderHomepagePermissions('main'))) {
-			$loadingBox = ($this->config['jdownloaderCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
-			$builder = ($this->config['jdownloaderCombine']) ? 'buildDownloaderCombined(\'jdownloader\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("jdownloader"));';
-			return '
+    public function homepageOrderjdownloader()
+    {
+        if ($this->homepageItemPermissions($this->jDownloaderHomepagePermissions('main'))) {
+            $loadingBox = ($this->config['jdownloaderCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
+            $builder = ($this->config['jdownloaderCombine']) ? 'buildDownloaderCombined(\'jdownloader\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("jdownloader"));';
+            return '
 				<div id="' . __FUNCTION__ . '">
 					' . $loadingBox . '
 					<script>
@@ -119,50 +127,56 @@ trait JDownloaderHomepageItem
 					</script>
 				</div>
 				';
-		}
-	}
+        }
+    }
 
-	public function getJdownloaderHomepageQueue()
-	{
-		if (!$this->homepageItemPermissions($this->jDownloaderHomepagePermissions('main'), true)) {
-			return false;
-		}
-		$url = $this->qualifyURL($this->config['jdownloaderURL']);
-		try {
-			$options = $this->requestOptions($url, $this->config['jdownloaderRefresh'], $this->config['jdownloaderDisableCertCheck'], $this->config['jdownloaderUseCustomCertificate']);
-			$response = Requests::get($url, [], $options);
-			if ($response->success) {
-				$temp = json_decode($response->body, true);
-				$packages = $temp['packages'];
-				if ($packages['downloader']) {
-					$api['content']['queueItems'] = $packages['downloader'];
-				} else {
-					$api['content']['queueItems'] = [];
-				}
-				if ($packages['linkgrabber_decrypted']) {
-					$api['content']['grabberItems'] = $packages['linkgrabber_decrypted'];
-				} else {
-					$api['content']['grabberItems'] = [];
-				}
-				if ($packages['linkgrabber_failed']) {
-					$api['content']['encryptedItems'] = $packages['linkgrabber_failed'];
-				} else {
-					$api['content']['encryptedItems'] = [];
-				}
-				if ($packages['linkgrabber_offline']) {
-					$api['content']['offlineItems'] = $packages['linkgrabber_offline'];
-				} else {
-					$api['content']['offlineItems'] = [];
-				}
-				$api['content']['$status'] = array($temp['downloader_state'], $temp['grabber_collecting'], $temp['update_ready']);
-			}
-		} catch (Requests_Exception $e) {
-			$this->setLoggerChannel('JDownloader')->error($e);
-			$this->setResponse(500, $e->getMessage());
-			return false;
-		};
-		$api['content'] = isset($api['content']) ? $api['content'] : false;
-		$this->setAPIResponse('success', null, 200, $api);
-		return $api;
-	}
+    public function getJdownloaderHomepageQueue()
+    {
+        if (!$this->homepageItemPermissions($this->jDownloaderHomepagePermissions('main'), true)) {
+            return false;
+        }
+        $url = $this->qualifyURL($this->config['jdownloaderURL']);
+        if (!empty($this->config['jdownloaderUsername']) || !empty($this->config['jdownloaderPassword'])) {
+            $authHeader = 'Authorization: Basic ' . base64_encode($this->config['jdownloaderUsername'] . ':' . $this->config['jdownloaderPassword']);
+            $headers = array('headers' => array($authHeader));
+        } else {
+            $headers = [];
+        }
+        try {
+            $options = $this->requestOptions($url, $this->config['jdownloaderRefresh'], $this->config['jdownloaderDisableCertCheck'], $this->config['jdownloaderUseCustomCertificate']);
+            $response = Requests::get($url, $headers, $options);
+            if ($response->success) {
+                $temp = json_decode($response->body, true);
+                $packages = $temp['packages'];
+                if ($packages['downloader']) {
+                    $api['content']['queueItems'] = $packages['downloader'];
+                } else {
+                    $api['content']['queueItems'] = [];
+                }
+                if ($packages['linkgrabber_decrypted']) {
+                    $api['content']['grabberItems'] = $packages['linkgrabber_decrypted'];
+                } else {
+                    $api['content']['grabberItems'] = [];
+                }
+                if ($packages['linkgrabber_failed']) {
+                    $api['content']['encryptedItems'] = $packages['linkgrabber_failed'];
+                } else {
+                    $api['content']['encryptedItems'] = [];
+                }
+                if ($packages['linkgrabber_offline']) {
+                    $api['content']['offlineItems'] = $packages['linkgrabber_offline'];
+                } else {
+                    $api['content']['offlineItems'] = [];
+                }
+                $api['content']['$status'] = array($temp['downloader_state'], $temp['grabber_collecting'], $temp['update_ready']);
+            }
+        } catch (Requests_Exception $e) {
+            $this->setLoggerChannel('JDownloader')->error($e);
+            $this->setResponse(500, $e->getMessage());
+            return false;
+        };
+        $api['content'] = isset($api['content']) ? $api['content'] : false;
+        $this->setAPIResponse('success', null, 200, $api);
+        return $api;
+    }
 }