'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]; } }