first commit

This commit is contained in:
huangjx
2021-09-09 22:03:08 +08:00
commit f2a2365102
74 changed files with 33916 additions and 0 deletions

373
lib/Tools/Aria2.php Normal file
View File

@@ -0,0 +1,373 @@
<?php
namespace OCA\NcDownloader\Tools;
//use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class Aria2
{
//extra Aria2 download options
private $options = array();
//optional token for authenticating with Aria2
private $token = null;
//the aria2 method being invoked
private $method = null;
//the aria2c binary path
private $bin = null;
// return the following items when getting downloads info by default
private $dataFilter = array(
'status', 'followedBy', 'totalLength', 'errorMessage', 'dir', 'uploadLength', 'completedLength', 'downloadSpeed', 'files', 'numSeeders', 'connections', 'gid', 'following', 'bittorrent',
);
//whether to filter the response returned by aria2
private $filterResponse = true;
//absolute download path
private $downloadDir;
//the path to where hook scripts is stored
public function __construct($options = array())
{
$options += array(
'host' => '127.0.0.1',
'port' => 6800,
'dir' => '/tmp/Downloads',
'token' => null,
'conf_dir' => '/tmp/aria2',
'settings' => [],
);
//turn keys in $options into variables
extract($options);
$this->setDownloadDir($dir);
if (!empty($settings)) {
foreach ($settings as $key => $value) {
$this->setOption($key, $value);
}
}
$this->bin = Helper::findBinaryPath('aria2c');
$this->rpcUrl = sprintf("http://%s:%s/jsonrpc", $host, $port);
$this->tokenString = $token ?? 'ncdownloader123';
$this->setToken($this->tokenString);
$this->confDir = $conf_dir;
$this->sessionFile = $this->confDir . "/aria2.session";
$this->confFile = $this->confDir . "/aria2.conf";
$this->logFile = $this->confDir . "/aria2.log";
}
public function init()
{
$this->ch = curl_init($this->rpcUrl);
curl_setopt_array($this->ch, array(
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => false,
));
$this->configure();
}
public function setonDownloadStart($path)
{
$this->onDownloadStart = $path;
}
public function reset()
{
$this->init();
}
private function hasOption($key)
{
return (bool) isset($this->options[$key]);
}
private function configure()
{
if (!is_dir($this->confDir)) {
mkdir($this->confDir, 0755, true);
}
if (!file_exists($this->confDir . "/aria2.conf")) {
file_put_contents($this->confDir . "/aria2.conf", $this->confTemplate());
}
if (!is_dir($dir = $this->getDownloadDir())) {
mkdir($dir, 0755, true);
}
$this->followTorrent(true);
}
public function setToken($token)
{
$this->token = "token:$token";
return $this;
}
public function setOption($key, $value)
{
$this->options[$key] = $value;
return $this;
}
public function getOptions()
{
return $this->options;
}
public function setDownloadDir($dir)
{
$this->setOption('dir', $dir);
$this->downloadDir = $dir;
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
return $this;
}
public function getDownloadDir()
{
return $this->downloadDir;
}
public function setFileName($file)
{
$this->options['out'] = $file;
return $this;
}
public function followTorrent($follow)
{
$this->options['follow-torrent'] = $follow;
return $this;
}
private function request($data)
{
$this->init();
$defaults = array(
'jsonrpc' => '2.0',
'id' => 'ncdownloader',
'method' => 'aria2.addUri',
'params' => null,
);
$data += $defaults;
$this->content = json_encode($data);
if (isset($this->content)) {
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->content);
}
$resp = curl_exec($this->ch);
curl_close($this->ch);
return json_decode($resp, 1);
}
public function getFollowingGid($gid)
{
$data = $this->tellStatus($gid);
if (!is_array($data)) {
return 0;
}
$data = reset($data);
return ($data['following'] ?? 0);
}
public function tellFail($range = [0, 999])
{
$this->filterResponse = false;
$resp = $this->tellStopped($range);
$result = $this->sortDownloadsResult($resp['result'], ['complete', 'removed']);
$this->filterResponse = true;;
return $result;
}
public function getCounters()
{
$active = is_array($data = $this->tellActive([])) ? count($data) : 0;
$waiting = is_array($data = $this->tellWaiting([0, 999])) ? count($data) : 0;
$stopped = is_array($data = $this->tellStopped([0, 999])) ? count($data) : 0;
$fail = is_array($data = $this->tellFail([0, 999])) ? count($data) : 0;
return ['active' => $active, 'waiting' => $waiting, 'complete' => $stopped, 'fail' => $fail];
}
public function tellAll()
{
$this->filterResponse = false;
return array_merge($this->tellActive([]), $this->tellWaiting([0, 999]), $this->tellStopped([0, 999]));
}
public function __call($name, $args)
{
$this->methodName = $name;
$data = array();
if (isset($args[0]) && is_array($args[0]) && count($args) == 1 && strtolower($name) !== "adduri") {
$args = reset($args);
}
switch ($name) {
case "addUri":
array_push($args, $this->options);
break;
case "tellActive":
case "tellWaiting":
case "tellStopped":
array_push($args, $this->dataFilter);
break;
case "tellStatus":
case "getFiles":
array_push($args, $this->dataFilter);
break;
}
if (isset($this->token)) {
array_unshift($args, $this->token);
}
$data = array('params' => $args, 'method' => 'aria2.' . $name);
$rawResp = $this->request($data);
if (!$this->filterResponse) {
return $rawResp;
}
return $this->parseResp($rawResp);
}
private function sortDownloadsResult($result, $statusFilter = null)
{
$data = [];
if (!isset($statusFilter)) {
$statusFilter = ['error'];
}
if (empty($result)) {
return [];
}
foreach ($result as $info) {
$info = Helper::filterData($info);
if (isset($info['files'])) {
foreach ($info['files'] as $key => &$files) {
$files = Helper::filterData($files, array('path', 'length'));
}
}
if (in_array($info['status'], $statusFilter)) {
continue;
}
array_push($data, $info);
}
return $data;
}
public function parseResp($resp = array())
{
$data = array();
if (isset($resp['error']) && isset($resp['error']['message'])) {
$data['error'] = $resp['error']['message'];
return $data;
}
$result = $resp['result'] ?? null;
if (!isset($result)) {
return $data;
}
if ($this->methodName === 'tellStatus' && isset($result['files'])) {
foreach ($result['files'] as $key => &$files) {
$files = Helper::filterData($files, array('path', 'length'));
}
array_push($data, $result);
return $data;
}
// parse response for tellActive,tellWaiting,and tellStopped
if (strpos($this->methodName, "tell") !== false && is_array($result)) {
return $this->sortDownloadsResult($result);
}
return $resp;
}
public function getStatus($gid)
{
return $this->tellStatus($gid);
}
public function restart()
{
$this->stop();
$this->start();
}
public function getDefaults()
{
return [
'--continue',
'--daemon=true',
'--enable-rpc=true',
'--rpc-secret=' . $this->tokenString,
'--listen-port=51413',
'--save-session=' . $this->sessionFile,
'--input-file=' . $this->sessionFile,
'--log=' . $this->logFile,
'--rpc-listen-port=6800',
'--follow-torrent=true',
'--enable-dht=true',
'--enable-peer-exchange=true',
'--peer-id-prefix=-TR2770-',
'--user-agent=Transmission/2.77',
'--log-level=info',
'--seed-ratio=1.0',
'--bt-seed-unverified=true',
'--max-overall-upload-limit=1M',
'--max-overall-download-limit=0',
'--max-connection-per-server=4',
'--max-concurrent-downloads=5',
];
}
public function start($bin = null)
{
//aria2c wont't start without any errors when input-file is set but missing
if (!file_exists($this->sessionFile)) {
file_put_contents($this->sessionFile, '');
}
//$process = new Process([$this->bin, "--conf-path=" . $this->confFile]);
$defaults = $this->getDefaults();
array_unshift($defaults, $this->bin);
$process = new Process($defaults);
try {
$process->mustRun();
$output = $process->getOutput();
} catch (ProcessFailedException $exception) {
$error = $exception->getMessage();
}
$resp = [];
if (isset($error)) {
$resp['error'] = $error;
$resp['status'] = false;
} else {
$resp['status'] = true;
}
return $resp;
}
public function isInstalled()
{
return (bool) isset($this->bin);
}
public function isRunning()
{
$resp = $this->getSessionInfo();
return (bool) $resp;
}
public function stop()
{
$resp = $this->shutdown();
return $resp ?? null;
}
private function confTemplate()
{
return <<<EOF
continue
daemon=true
#dir=/home/aria2/Downloads
#file-allocation=falloc
log-level=info
max-connection-per-server=4
max-concurrent-downloads=5
max-overall-download-limit=0
min-split-size=5M
enable-http-pipelining=true
#interface=127.0.0.1
enable-rpc=true
rpc-secret=$this->tokenString
rpc-listen-all=true
rpc-listen-port=6800
follow-torrent=true
listen-port=51413
enable-dht=true
enable-peer-exchange=true
peer-id-prefix=-TR2770-
user-agent=Transmission/2.77
seed-ratio=0.1
bt-seed-unverified=true
max-overall-upload-limit=1M
#on-download-complete=$this->onDownloadComplete
#on-download-error=$this->onDownloadError
#on-download-start=$this->onDownloadStart
save-session=$this->sessionFile
input-file=$this->sessionFile
log=$this->logFile
EOF;
}
}

90
lib/Tools/DBConn.php Normal file
View File

@@ -0,0 +1,90 @@
<?php
namespace OCA\NcDownloader\Tools;
class DBConn
{
//@var OC\DB\ConnectionAdapter
private $conn;
private $table = "ncdownloader_info";
public function __construct()
{
$this->conn = \OC::$server->getDatabaseConnection();
$this->queryBuilder = $this->conn->getQueryBuilder();
//$container = \OC::$server->query(\OCP\IServerContainer::class);
//Helper::debug(get_class($container->query(\OCP\RichObjectStrings\IValidator::class)));
//$this->conn = \OC::$server->query(Connection::class);//working only with 22
//$this->connAdapter = \OC::$server->getDatabaseConnection();
//$this->conn = $this->connAdapter->getInner();
}
public function create($insert)
{
$inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*' . $this->table, $insert, [
'gid',
]);
return $inserted;
}
public function getAll()
{
//OC\DB\QueryBuilder\QueryBuilder
$queryBuilder = $this->queryBuilder
->select('filename', 'type', 'gid', 'timestamp', 'status')
->from($this->table)
->execute();
return $queryBuilder->fetchAll();
}
public function getByUid($uid)
{
$queryBuilder = $this->queryBuilder
->select('*')
->from($this->table)
->where('uid = :uid')
->setParameter('uid', $uid)
->execute();
return $queryBuilder->fetchAll();
}
public function getByGid($gid)
{
$queryBuilder = $this->queryBuilder
->select('*')
->from($this->table)
->where('gid = :gid')
->setParameter('gid', $gid)
->execute();
return $queryBuilder->fetch();
}
public function save(array $keys, $values = array())
{
return $this->conn->setValues($this->table, $keys, $values);
}
public function deleteByGid($gid)
{
// $sql = sprintf("DELETE FROM %s WHERE gid = ?",'*PREFIX*'.$this->table);
// return $this->conn->executeStatement($sql,array($gid));
$qb = $this->queryBuilder
->delete($this->table)
->where('gid = :gid')
->setParameter('gid', $gid);
return $qb->execute();
}
public function execute($sql, $values)
{
return $this->conn->executeStatement($sql, $values);
// for some reason this doesn't work
$query = $this->queryBuilder;
$query->update('ncdownloader_info')
->set("data", $query->createNamedParameter($value))
->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
// ->setParameter('gid', $gid);
// return $query->execute();
//return $query->getSQL();
return $this->queryBuilder->getSQL();
}
}

View File

@@ -0,0 +1,86 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace OCA\NcDownloader\Tools;
/**
* Generic executable finder.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class ExecutableFinder
{
private $suffixes = ['.exe', '.bat', '.cmd', '.com'];
/**
* Replaces default suffixes of executable.
*/
public function setSuffixes(array $suffixes)
{
$this->suffixes = $suffixes;
}
/**
* Adds new possible suffix to check for executable.
*/
public function addSuffix(string $suffix)
{
$this->suffixes[] = $suffix;
}
/**
* Finds an executable by name.
*
* @param string $name The executable name (without the extension)
* @param string|null $default The default to return if no executable is found
* @param array $extraDirs Additional dirs to check into
*
* @return string|null
*/
public function find(string $name, string $default = null, array $extraDirs = [])
{
if (ini_get('open_basedir')) {
$searchPath = array_merge(explode(\PATH_SEPARATOR, ini_get('open_basedir')), $extraDirs);
$dirs = [];
foreach ($searchPath as $path) {
// Silencing against https://bugs.php.net/69240
if (@is_dir($path)) {
$dirs[] = $path;
} else {
if (basename($path) == $name && @is_executable($path)) {
return $path;
}
}
}
} else {
$dirs = array_merge(
explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
$extraDirs
);
}
$suffixes = [''];
if ('\\' === \DIRECTORY_SEPARATOR) {
$pathExt = getenv('PATHEXT');
$suffixes = array_merge($pathExt ? explode(\PATH_SEPARATOR, $pathExt) : $this->suffixes, $suffixes);
}
foreach ($suffixes as $suffix) {
foreach ($dirs as $dir) {
if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) {
return $file;
}
}
}
return $default;
}
}

28
lib/Tools/File.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
namespace OCA\NcDownloader\Tools;
use OCA\NcDownloader\Tools\Helper;
use OC\Files\Filesystem;
use OC\Files\Utils\Scanner;
use \OCP\EventDispatcher\IEventDispatcher;
class File
{
public static function syncFolder($dir)
{
$user = \OC::$server->getUserSession()->getUser()->getUID();
$logger = \OC::$server->getLogger();
$scanner = new Scanner($user, \OC::$server->getDatabaseConnection(), \OC::$server->query(IEventDispatcher::class), $logger);
$path = Filesystem::getRoot() . "/" . ltrim($dir, '/\\');
try {
$scanner->scan($path);
// Helper::debug($logger->getLogPath());
//$logger->warning($logger->getLogPath(),['app' =>'Ncdownloader']);
} catch (ForbiddenException $e) {
$logger->warning("Make sure you're running the scan command only as the user the web server runs as");
} catch (\Exception $e) {
$logger->warning("Exception during scan: " . $e->getMessage() . $e->getTraceAsString());
}
}
}

257
lib/Tools/Helper.php Normal file
View File

@@ -0,0 +1,257 @@
<?php
namespace OCA\NcDownloader\Tools;
use OCA\NcDownloader\Tools\aria2Options;
class Helper
{
public static function isUrl($URL)
{
$URLPattern = '%^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}'
. ']+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.'
. '[a-z\x{00a1}-\x{ffff}]{2,6}))(?::\d+)?(?:[^\s]*)?$%iu';
preg_match($URLPattern, $URL, $Matches);
if (count($Matches) === 1) {
return true;
}
return false;
}
public static function isMagnet($url)
{
$scheme = parse_url($url, PHP_URL_SCHEME);
return strtolower($scheme) == "magnet";
}
public static function isHttp($url)
{
$scheme = parse_url($url, PHP_URL_SCHEME);
return (in_array($scheme, array('http', 'https')));
}
public static function isFtp($url)
{
$scheme = parse_url($url, PHP_URL_SCHEME);
return strtolower($scheme) == "ftp";
}
public static function isGetUrlSite($url)
{
$host = parse_url($url, PHP_URL_HOST);
$sites = ['twitter.com', 'youtube.com'];
return (bool) (in_array($host, $sites));
}
public static function parseUrl($url)
{
parse_str(str_replace('tr=', 'tr[]=', parse_url($url, PHP_URL_QUERY)), $query);
return $query;
}
public static function getUrlPath($url)
{
$path = parse_url($url, PHP_URL_PATH);
return self::cleanString(basename($path));
}
public static function getFilename($url)
{
if (self::isMagnet($url)) {
return self::parseUrl($url)['dn'];
} else {
return self::getUrlPath($url);
}
}
public static function formatBytes($size, $precision = 2)
{
if ($size < 1) {
return '0';
}
$base = log($size, 1024);
$suffixes = array('', 'K', 'M', 'G', 'T');
return round(pow(1024, $base - floor($base)), $precision) . ' ' . $suffixes[floor($base)];
}
public static function checkMediaType($url, $type = 'video/mp4')
{
$result = parse_url($url);
if (isset($result['scheme']) && self::isHttp($url)) {
if (isset($result['query'])) {
parse_str($result['query'], $output);
if (!isset($output['mime'])) {
return false;
}
}
return (bool) ($output['mime'] == trim($type));
}
return false;
}
public static function isYoutubeType($url)
{
$regex = '%^(?:(?:https?)://)(?:[a-z0-9_]*\.)?(?:twitter|youtube)\.com/%i';
return (bool) preg_match($regex, $url);
}
public static function cleanString($string)
{
$replace = array
(
'/[áàâãªä]/u' => 'a',
'/[ÁÀÂÃÄ]/u' => 'A',
'/[ÍÌÎÏ]/u' => 'I',
'/[íìîï]/u' => 'i',
'/[éèêë]/u' => 'e',
'/[ÉÈÊË]/u' => 'E',
'/[óòôõºö]/u' => 'o',
'/[ÓÒÔÕÖ]/u' => 'O',
'/[úùûü]/u' => 'u',
'/[ÚÙÛÜ]/u' => 'U',
'/ç/' => 'c',
'/Ç/' => 'C',
'/ñ/' => 'n',
'/Ñ/' => 'N',
'//' => '-', // UTF-8 hyphen to "normal" hyphen
'/[]/u' => '', // Literally a single quote
'/[“”«»„]/u' => '', // Double quote
'/ /' => '_', // nonbreaking space(equiv. to 0x160)
'/[^a-z0-9_\s.-]/i' => '_',
);
return preg_replace(array_keys($replace), array_values($replace), $string);
}
public static function debug($msg)
{
$logger = \OC::$server->getLogger();
$logger->debug($msg, ['app' => 'ncdownloader']);
}
public static function log($msg, $file = "/tmp/nc.log")
{
file_put_contents($file, print_r($msg, true));
}
public static function filterData($data, $filter = null)
{
if (!isset($filter)) {
$filter = array(
'status', 'followedBy', 'totalLength', 'errorMessage', 'dir', 'uploadLength', 'completedLength', 'downloadSpeed', 'files', 'numSeeders', 'connections', 'gid', 'following',
);
}
$value = array_filter($data, function ($k) use ($filter) {
return (in_array($k, $filter));
}, ARRAY_FILTER_USE_KEY);
return $value;
}
public function getFolderName($folder, $prefix)
{
$folder = ltrim($folder, $prefix);
return substr($folder, 0, strpos($folder, '/'));
}
public static function Download($url, $file = null)
{
if (!isset($file)) {
$file = "/tmp/" . self::getFilename($url);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
file_put_contents($file, $result);
}
public static function is_function_enabled($function_name)
{
if (!function_exists($function_name)) {
return false;
}
$ini = \OC::$server->getIniWrapper();
$disabled = explode(',', $ini->get('disable_functions') ?: '');
$disabled = array_map('trim', $disabled);
if (in_array($function_name, $disabled)) {
return false;
}
$disabled = explode(',', $ini->get('suhosin.executor.func.blacklist') ?: '');
$disabled = array_map('trim', $disabled);
if (in_array($function_name, $disabled)) {
return false;
}
return true;
}
public static function findBinaryPath($program)
{
$memcache = \OC::$server->getMemCacheFactory()->createDistributed('findBinaryPath');
if ($memcache->hasKey($program)) {
return $memcache->get($program);
}
$dataPath = \OC::$server->getSystemConfig()->getValue('datadirectory');
$paths = ['/usr/local/sbin', '/usr/local/bin', '/usr/sbin', '/usr/bin', '/sbin', '/bin', '/opt/bin', $dataPath . "/bin"];
$result = null;
if (self::is_function_enabled('exec')) {
$exeSniffer = new ExecutableFinder();
// Returns null if nothing is found
$result = $exeSniffer->find($program, null, $paths);
}
// store the value for 5 minutes
$memcache->set($program, $result, 300);
return $result;
}
public static function formatInterval($interval, $granularity = 2)
{
$units = array(
'1 year|years' => 31536000,
'1 monthmonths' => 2592000,
'1 week|weeks' => 604800,
'1 day|days' => 86400,
'1 hour|hours' => 3600,
'1 min|mins' => 60,
'1 sec|sec' => 1,
);
$output = '';
foreach ($units as $key => $value) {
$key = explode('|', $key);
if ($interval >= $value) {
$output .= ($output ? ' ' : '') . self::formatPlural(floor($interval / $value), $key[0], $key[1]);
$interval %= $value;
$granularity--;
}
if ($granularity == 0) {
break;
}
}
return $output ? $output : '0 sec';
}
public static function formatPlural($count, $singular, $plural)
{
if ($count == 1) {
return $singular;
} else {
return $count . " " . $plural;
}
}
public static function aria2Options()
{
return aria2Options::get();
}
public static function getTableTitles($type = null)
{
$general = ['filename', 'status', 'actions'];
if(!isset($type)){
return $general;
}
$titles = [
'active' => ['filename', 'speed', 'progress', 'actions'],
'waiting' => $general,
'fail' => $general,
'complete' => $general,
];
return $titles[$type];
}
}

235
lib/Tools/Settings.php Normal file
View File

@@ -0,0 +1,235 @@
<?php
namespace OCA\NcDownloader\Tools;
use OC\AllConfig;
class Settings extends AllConfig
{
//@config OC\AppConfig
private $config;
//@OC\SystemConfig
private $sysConfig;
//@OC\AllConfig
private $allConfig;
//type of settings (system = 1 or app =2)
private $type;
const SYSTEM = 0x001;
const USER = 0x010;
const APP = 0x100;
public function __construct($user = null)
{
$this->appConfig = \OC::$server->getAppConfig();
$this->sysConfig = \OC::$server->getSystemConfig();
$this->appName = 'ncdownloader';
$this->type = self::USER;
$this->user = $user;
$this->allConfig = new AllConfig($this->sysConfig);
//$this->connAdapter = \OC::$server->getDatabaseConnection();
//$this->conn = $this->connAdapter->getInner();
}
public function setType($type)
{
$this->type = $type;
return $this;
}
public function get($key, $default = null)
{
if ($this->type == self::USER && isset($this->user)) {
return $this->allConfig->getUserValue($this->user, $this->appName, $key, $default);
} else if ($this->type == self::SYSTEM) {
return $this->allConfig->getSystemValue($key, $default);
} else {
return $this->allConfig->getAppValue($this->appName, $key, $default);
}
}
public function getAria2()
{
$settings = $this->allConfig->getUserValue($this->user, $this->appName, "custom_aria2_settings", '');
return json_decode($settings, 1);
}
public function getAll()
{
if ($this->type === self::APP) {
return $this->getAllAppValues();
} else {
$data = $this->getAllUserSettings();
return $data;
}
}
public function save($key, $value)
{
if ($this->type == self::USER && isset($this->user)) {
return $this->allConfig->setUserValue($this->user, $this->appName, $key, $value);
} else if ($this->type == self::SYSTEM) {
return $this->allConfig->setSystemValue($key, $value);
} else {
return $this->allConfig->setAppValue($this->appName, $key, $value);
}
}
public function getAllAppValues()
{
$keys = $this->getAllKeys();
$value = [];
foreach ($keys as $key) {
$value[$key] = $this->allConfig->getAppValue($this->appName, $key);
}
return $value;
}
public function getAllKeys()
{
return $this->allConfig->getAppKeys($this->appName);
}
public function getAllUserSettings()
{
$keys = $this->allConfig->getUserKeys($this->user, $this->appName);
$value = [];
foreach ($keys as $key) {
$value[$key] = $this->allConfig->getUserValue($this->user, $this->appName, $key);
}
return $value;
}
}
class customSettings
{
private $name = null;
private $dbType = 0;
private $table = 'ncdownloader_settings';
private $uid = null;
const PGSQL = 1, MYSQL = 2, SQL = 3;
/* @var OC\DB\ConnectionAdapter */
private $connAdapter;
/* @var OC\DB\Connection */
private $conn;
private $type = 2; //personal = 2,admin =1
public function __construct()
{
if (\OC::$server->getConfig()->getSystemValue('dbtype') == 'pgsql') {
$this->dbType = PGSQL;
}
$this->connAdapter = \OC::$server->getDatabaseConnection();
$this->conn = $this->connAdapter->getInner();
$this->prefixTable();
}
private function prefixTable()
{
$this->table = '*PREFIX*' . $this->table;
return $this->table;
}
public function set($name, $value)
{
if ($this->have($name)) {
$this->update($name, $value);
} else {
$this->insert($name, $value);
}
}
public function setType($type)
{
$this->type = $type;
}
public function get($name)
{
if (isset($this->uid)) {
$sql = sprintf("SELECT value FROM %s WHERE uid = ? AND name = ? LIMIT 1", $this->table);
$query = \OC_DB::prepare($sql);
$result = $query->execute(array($this->uid, $name));
} else {
$sql = sprintf("SELECT value FROM %s WHERE name = ? LIMIT 1", $this->table);
$query = \OC_DB::prepare($sql);
$result = $query->execute(array($name));
}
if ($query->rowCount() == 1) {
return $result->fetchOne();
}
return null;
}
public function setUID($uid)
{
$this->uid = $uid;
}
public function setTable($table)
{
$this->table = $table;
}
public function getTable()
{
return $this->table;
}
public function have($name)
{
if (isset($this->uid)) {
$sql = sprintf("SELECT value FROM %s WHERE uid = ? AND name = ? AND type = ? LIMIT 1", $this->table);
$query = \OC_DB::prepare($sql);
$query->execute(array($name, $this->uid, $this->type));
} else {
$sql = sprintf("SELECT value FROM %s WHERE name = ? AND type = ? LIMIT 1", $this->table);
$query = \OC_DB::prepare($sql);
$query->execute(array($name, $this->type));
}
if ($query->rowCount() == 1) {
return true;
}
return false;
}
public function getAll()
{
$sql = 'SELECT `name`, `value` FROM `*PREFIX*' . $this->table . '`'
. (!is_null($this->uid) ? ' WHERE `UID` = ?' : '');
if ($this->DbType == 1) {
$sql = 'SELECT "name", "value" FROM *PREFIX*' . $this->table . ''
. (!is_null($this->uid) ? ' WHERE "uid" = ?' : '');
}
$query = \OC_DB::prepare($sql);
if (!is_null($this->uid)) {
return $query->execute(array($this->uid));
} else {
return $query->execute();
}
}
public function update($value)
{
if (isset($this->uid)) {
$sql = sprintf("UPDATE %s SET value = ? WHERE name = ? AND type = ? AND uid = ?", $this->table);
//OCP\DB\IPreparedStatement
$query = \OC_DB::prepare($sql);
$query->execute(array($value, $name, $this->type, $this->uid));
} else {
$sql = sprintf("UPDATE %s SET value = ? WHERE name = ? AND type = ?", $this->table);
//OCP\DB\IPreparedStatement
$query = \OC_DB::prepare($sql);
$query->execute(array($value, $name, $this->type));
}
}
public function insert($name, $value)
{
$sql = sprintf("INSERT INTO %s (name,value,type,uid) VALUES(?,?,?,?)", $this->table);
//OCP\DB\IPreparedStatement
$query = \OC_DB::prepare($sql);
$query->execute(array($name, $value, $this->type, $this->uid));
}
}

127
lib/Tools/YouTube.php Normal file
View File

@@ -0,0 +1,127 @@
<?php
namespace OCA\NcDownloader\Tools;
use OCA\NcDownloader\Tools\Helper;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class YouTube
{
private $ipv4Only;
private $audioOnly = 0;
private $audioFormat, $videoFormat = 'mp4';
private $options = [];
public function __construct()
{
$this->bin = Helper::findBinaryPath('youtube-dl');
}
public function GetUrlOnly()
{
$this->addOption('--get-filename');
$this->addOption('--get-url');
return $this;
}
public static function create()
{
return new self();
}
public function setDownloadDir($dir)
{
$this->downloadDir = rtrim($dir, '/');
}
public function prependOption($option)
{
array_unshift($this->options, $option);
}
public function download($url)
{
$this->downloadDir = $this->downloadDir ?? "/tmp/downloads";
$this->prependOption($this->downloadDir . "/%(id)s-%(title)s.%(ext)s");
$this->prependOption("-o");
$this->setUrl($url);
$this->prependOption($this->bin);
// $this->buildCMD();
$process = new Process($this->options);
try {
$process->mustRun();
$output = $process->getOutput();
} catch (ProcessFailedException $exception) {
$output = $exception->getMessage();
}
return $output;
}
public function getDownloadUrl($url)
{
$this->setUrl($url);
$this->GetUrlOnly();
//$process = new Process($this->options);
$this->buildCMD();
exec($this->cmd, $output, $returnCode);
if (count($output) === 1) {
return ['url' => reset($output)];
}
list($url, $filename) = $output;
return ['url' => $url, 'filename' => Helper::cleanString($filename)];
}
public function setUrl($url)
{
$this->addOption('-i');
$this->addOption($url);
//$index = array_search('-i', $this->options);
//array_splice($this->options, $index + 1, 0, $url);
}
public function addOption($option)
{
array_push($this->options, $option);
}
public function forceIPV4()
{
$this->addOption('-4');
return $this;
}
public function setAudioFormat($format)
{
$this->audioFormat = $format;
}
public function setvideoFormat($format)
{
$this->videoFormat = $format;
}
private function buildCMD()
{
$this->cmd = $this->bin;//. " 2>&1";
foreach ($this->options as $option) {
$this->cmd .= " " . $option;
}
}
public function isInstalled()
{
return (bool) isset($this->bin);
}
public static function install()
{
$url = $this->installUrl();
$path = \OC::$server->getSystemConfig()->getValue('datadirectory');
Helper::Download($url, $path . "/youtube-dl");
}
public function installUrl()
{
return "https://yt-dl.org/downloads/latest/youtube-dl";
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace OCA\NcDownloader\Tools;
class aria2Options
{
public static function get()
{
return ["ca-certificate", "certificate", "dht-file-path", "dht-file-path6", "dir", "input-file", "load-cookies", "log", "metalink-file", "netrc-path", "on-bt-download-complete", "on-download-complete", "on-download-error", "on-download-start", "on-download-stop", "on-download-pause", "out", "private-key", "rpc-certificate", "rpc-private-key", "save-cookies", "save-session", "server-stat-if", "server-stat-of", "torrent-file", "all-proxy", "all-proxy-passwd", "all-proxy-user", "allow-overwrite", "allow-piece-length-change", "always-resume", "async-dns", "auto-file-renaming", "bt-enable-hook-after-hash-check", "bt-enable-lpd", "bt-exclude-tracker", "bt-external-ip", "bt-force-encryption", "bt-hash-check-seed", "bt-load-saved-metadata", "bt-max-peers", "bt-metadata-only", "bt-min-crypto-level", "bt-prioritize-piece", "bt-remove-unselected-file", "bt-request-peer-speed-limit", "bt-require-crypto", "bt-save-metadata", "bt-seed-unverified", "bt-stop-timeout", "bt-tracker", "bt-tracker-connect-timeout", "bt-tracker-interval", "bt-tracker-timeout", "check-integrity", "checksum", "conditional-get", "connect-timeout", "content-disposition-default-utf8", "continue", "dir", "dry-run", "enable-http-keep-alive", "enable-http-pipelining", "enable-mmap", "enable-peer-exchange", "file-allocation", "follow-metalink", "follow-torrent", "force-save", "ftp-passwd", "ftp-pasv", "ftp-proxy", "ftp-proxy-passwd", "ftp-proxy-user", "ftp-reuse-connection", "ftp-type", "ftp-user", "gid", "hash-check-only", "header", "http-accept-gzip", "http-auth-challenge", "http-no-cache", "http-passwd", "http-proxy", "http-proxy-passwd", "http-proxy-user", "http-user", "https-proxy", "https-proxy-passwd", "https-proxy-user", "index-out", "lowest-speed-limit", "max-connection-per-server", "max-download-limit", "max-file-not-found", "max-mmap-limit", "max-resume-failure-tries", "max-tries", "max-upload-limit", "metalink-base-uri", "metalink-enable-unique-protocol", "metalink-language", "metalink-location", "metalink-os", "metalink-preferred-protocol", "metalink-version", "min-split-size", "no-file-allocation-limit", "no-netrc", "no-proxy", "out", "parameterized-uri", "pause", "pause-metadata", "piece-length", "proxy-method", "realtime-chunk-checksum", "referer", "remote-time", "remove-control-file", "retry-wait", "reuse-uri", "rpc-save-upload-metadata", "seed-ratio", "seed-time", "select-file", "split", "ssh-host-key-md", "stream-piece-selector", "timeout", "uri-selector", "use-head", "user-agent", "dry-run", "metalink-base-uri", "parameterized-uri", "pause", "piece-length", "rpc-save-upload-metadata", "bt-max-peers", "bt-request-peer-speed-limit", "bt-remove-unselected-file", "force-save", "max-download-limit", "max-upload-limit", "bt-max-open-files", "download-result", "keep-unfinished-download-result", "log", "log-level", "max-concurrent-downloads", "max-download-result", "max-overall-download-limit", "max-overall-upload-limit", "optimize-concurrent-downloads", "save-cookies", "save-session", "server-stat-of"];
}
}