OneManager-php/function/common.php

314 lines
11 KiB
PHP

<?php
function getcache($str)
{
$cache = null;
$cache = new \Doctrine\Common\Cache\FilesystemCache(sys_get_temp_dir(), '.Onedrive');
return $cache->fetch($str);
}
function savecache($key, $value, $exp = 3300)
{
$cache = null;
$cache = new \Doctrine\Common\Cache\FilesystemCache(sys_get_temp_dir(), '.Onedrive');
$cache->save($key, $value, $exp);
}
function config_oauth()
{
global $constStr;
$constStr['language'] = $_COOKIE['language'];
if ($constStr['language']=='') $constStr['language'] = getConfig('language');
if ($constStr['language']=='') $constStr['language'] = 'en-us';
$_SERVER['sitename'] = getConfig('sitename');
if (empty($_SERVER['sitename'])) $_SERVER['sitename'] = $constStr['defaultSitename'][$constStr['language']];
$_SERVER['redirect_uri'] = 'https://scfonedrive.github.io';
if (getConfig('Onedrive_ver')=='MS') {
// MS
// https://portal.azure.com
$_SERVER['client_id'] = '4da3e7f2-bf6d-467c-aaf0-578078f0bf7c';
$_SERVER['client_secret'] = '7/+ykq2xkfx:.DWjacuIRojIaaWL0QI6';
$_SERVER['oauth_url'] = 'https://login.microsoftonline.com/common/oauth2/v2.0/';
$_SERVER['api_url'] = 'https://graph.microsoft.com/v1.0/me/drive/root';
$_SERVER['scope'] = 'https://graph.microsoft.com/Files.ReadWrite.All offline_access';
}
if (getConfig('Onedrive_ver')=='CN') {
// CN
// https://portal.azure.cn
$_SERVER['client_id'] = '04c3ca0b-8d07-4773-85ad-98b037d25631';
$_SERVER['client_secret'] = 'h8@B7kFVOmj0+8HKBWeNTgl@pU/z4yLB';
$_SERVER['oauth_url'] = 'https://login.partner.microsoftonline.cn/common/oauth2/v2.0/';
$_SERVER['api_url'] = 'https://microsoftgraph.chinacloudapi.cn/v1.0/me/drive/root';
$_SERVER['scope'] = 'https://microsoftgraph.chinacloudapi.cn/Files.ReadWrite.All offline_access';
}
if (getConfig('Onedrive_ver')=='MSC') {
// MS Customer
// https://portal.azure.com
$_SERVER['client_id'] = getConfig('client_id');
$_SERVER['client_secret'] = getConfig('client_secret');
$_SERVER['oauth_url'] = 'https://login.microsoftonline.com/common/oauth2/v2.0/';
$_SERVER['api_url'] = 'https://graph.microsoft.com/v1.0/me/drive/root';
$_SERVER['scope'] = 'https://graph.microsoft.com/Files.ReadWrite.All offline_access';
}
$_SERVER['client_secret'] = urlencode($_SERVER['client_secret']);
$_SERVER['scope'] = urlencode($_SERVER['scope']);
}
function getListpath($domain)
{
$domain_path = getConfig('domain_path');
/*$tmp_path='';
if ($domain_path!='') {
$tmp = explode("|",$domain_path);
foreach ($tmp as $multidomain_paths){
$pos = strpos($multidomain_paths,":");
$tmp_path = path_format(substr($multidomain_paths,$pos+1));
if (substr($multidomain_paths,0,$pos)==$host_name) $private_path=$tmp_path;
}
}*/
if (isset($domain_path[$domain])) return spurlencode($domain_path[$domain],'/');
return spurlencode(getConfig('public_path'),'/');
}
function path_format($path)
{
$path = '/' . $path;
while (strpos($path, '//') !== FALSE) {
$path = str_replace('//', '/', $path);
}
return $path;
}
function spurlencode($str,$splite='')
{
$str = str_replace(' ', '%20',$str);
$tmp='';
if ($splite!='') {
$tmparr=explode($splite,$str);
for($x=0;$x<count($tmparr);$x++) {
if ($tmparr[$x]!='') $tmp .= $splite . urlencode($tmparr[$x]);
}
} else {
$tmp = urlencode($str);
}
$tmp = str_replace('%2520', '%20',$tmp);
return $tmp;
}
function is_guestup_path($path)
{
if (path_format('/'.path_format(urldecode($_SERVER['list_path'].path_format($path))).'/')==path_format('/'.path_format(getConfig('guestup_path')).'/')&&getConfig('guestup_path')!='') return 1;
return 0;
}
function curl_request($url, $data = false, $headers = [])
{
if (!isset($headers['Accept'])) $headers['Accept'] = '*/*';
if (!isset($headers['Referer'])) $headers['Referer'] = $url;
if (!isset($headers['Content-Type'])) $headers['Content-Type'] = 'application/x-www-form-urlencoded';
$sendHeaders = array();
foreach ($headers as $headerName => $headerVal) {
$sendHeaders[] = $headerName . ': ' . $headerVal;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if ($data !== false) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $sendHeaders);
$response['body'] = curl_exec($ch);
$response['stat'] = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
return $response;
}
function clearbehindvalue($path,$page1,$maxpage,$pageinfocache)
{
for ($page=$page1+1;$page<$maxpage;$page++) {
$pageinfocache['nextlink_' . $path . '_page_' . $page] = '';
}
return $pageinfocache;
}
function comppass($pass)
{
if ($_POST['password1'] !== '') if (md5($_POST['password1']) === $pass ) {
date_default_timezone_set('UTC');
$_SERVER['Set-Cookie'] = 'password='.$pass.'; expires='.date(DATE_COOKIE,strtotime('+1hour'));
date_default_timezone_set(get_timezone($_COOKIE['timezone']));
return 2;
}
if ($_COOKIE['password'] !== '') if ($_COOKIE['password'] === $pass ) return 3;
return 4;
}
function encode_str_replace($str)
{
$str = str_replace('&','&amp;',$str);
$str = str_replace('+','%2B',$str);
$str = str_replace('#','%23',$str);
return $str;
}
function gethiddenpass($path,$passfile)
{
$ispassfile = fetch_files(spurlencode(path_format($path . '/' . $passfile),'/'));
//echo $path . '<pre>' . json_encode($ispassfile, JSON_PRETTY_PRINT) . '</pre>';
if (isset($ispassfile['file'])) {
$arr = curl_request($ispassfile['@microsoft.graph.downloadUrl']);
if ($arr['stat']==200) {
$passwordf=explode("\n",$arr['body']);
$password=$passwordf[0];
$password=md5($password);
return $password;
} else {
//return md5('DefaultP@sswordWhenNetworkError');
return md5( md5(time()).rand(1000,9999) );
}
} else {
if ($path !== '' ) {
$path = substr($path,0,strrpos($path,'/'));
return gethiddenpass($path,$passfile);
} else {
return '';
}
}
return md5('DefaultP@sswordWhenNetworkError');
}
function get_timezone($timezone = '8')
{
$timezones = array(
'-12'=>'Pacific/Kwajalein',
'-11'=>'Pacific/Samoa',
'-10'=>'Pacific/Honolulu',
'-9'=>'America/Anchorage',
'-8'=>'America/Los_Angeles',
'-7'=>'America/Denver',
'-6'=>'America/Mexico_City',
'-5'=>'America/New_York',
'-4'=>'America/Caracas',
'-3.5'=>'America/St_Johns',
'-3'=>'America/Argentina/Buenos_Aires',
'-2'=>'America/Noronha',
'-1'=>'Atlantic/Azores',
'0'=>'UTC',
'1'=>'Europe/Paris',
'2'=>'Europe/Helsinki',
'3'=>'Europe/Moscow',
'3.5'=>'Asia/Tehran',
'4'=>'Asia/Baku',
'4.5'=>'Asia/Kabul',
'5'=>'Asia/Karachi',
'5.5'=>'Asia/Calcutta', //Asia/Colombo
'6'=>'Asia/Dhaka',
'6.5'=>'Asia/Rangoon',
'7'=>'Asia/Bangkok',
'8'=>'Asia/Shanghai',
'9'=>'Asia/Tokyo',
'9.5'=>'Australia/Darwin',
'10'=>'Pacific/Guam',
'11'=>'Asia/Magadan',
'12'=>'Asia/Kamchatka'
);
if ($timezone=='') $timezone = '8';
return $timezones[$timezone];
}
function message($message, $title = 'Message', $statusCode = 200)
{
return output('<html><meta charset=utf-8><body><h1>' . $title . '</h1><p>' . $message . '</p></body></html>', $statusCode);
}
function needUpdate()
{
if ($_SERVER['admin']) {
$current_ver = file_get_contents(__DIR__ . '/version');
$current_ver = substr($current_ver, strpos($current_ver, '.')+1);
$current_ver = explode(urldecode('%0A'),$current_ver)[0];
$current_ver = explode(urldecode('%0D'),$current_ver)[0];
$github_version = file_get_contents('https://raw.githubusercontent.com/qkqpttgf/OneManager-php/master/version');
$github_ver = substr($github_version, strpos($github_version, '.')+1);
$github_ver = explode(urldecode('%0A'),$github_ver)[0];
$github_ver = explode(urldecode('%0D'),$github_ver)[0];
if ($current_ver != $github_ver) {
$_SERVER['github_version'] = $github_version;
return 1;
}
}
return 0;
}
function output($body, $statusCode = 200, $headers = ['Content-Type' => 'text/html'], $isBase64Encoded = false)
{
return [
'isBase64Encoded' => $isBase64Encoded,
'statusCode' => $statusCode,
'headers' => $headers,
'body' => $body
];
}
function passhidden($path)
{
$path = str_replace('+','%2B',$path);
$path = str_replace('&amp;','&', path_format(urldecode($path)));
if (getConfig('passfile') != '') {
if (substr($path,-1)=='/') $path=substr($path,0,-1);
$hiddenpass=gethiddenpass($path,getConfig('passfile'));
if ($hiddenpass != '') {
return comppass($hiddenpass);
} else {
return 1;
}
} else {
return 0;
}
return 4;
}
function size_format($byte)
{
$i = 0;
while (abs($byte) >= 1024) {
$byte = $byte / 1024;
$i++;
if ($i == 3) break;
}
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$ret = round($byte, 2);
return ($ret . ' ' . $units[$i]);
}
function time_format($ISO)
{
$ISO = str_replace('T', ' ', $ISO);
$ISO = str_replace('Z', ' ', $ISO);
//return $ISO;
return date('Y-m-d H:i:s',strtotime($ISO . " UTC"));
}
function get_thumbnails_url($path = '/')
{
$path1 = path_format($path);
$path = path_format($_SERVER['list_path'] . path_format($path));
$url = $_SERVER['api_url'];
if ($path !== '/') {
$url .= ':' . $path;
if (substr($url,-1)=='/') $url=substr($url,0,-1);
}
$url .= ':/thumbnails/0/medium';
$files = json_decode(curl_request($url, false, ['Authorization' => 'Bearer ' . $_SERVER['access_token']])['body'], true);
if (isset($files['url'])) return output($files['url']);
return output('', 404);
}