' . json_encode($ispassfile, JSON_PRETTY_PRINT) . ''; + 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('
' . $message . '
', $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('&','&', 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); +} + +function bigfileupload($path) +{ + $path1 = path_format($_SERVER['list_path'] . path_format($path)); + if (substr($path1,-1)=='/') $path1=substr($path1,0,-1); + if ($_GET['upbigfilename']!=''&&$_GET['filesize']>0) { + $fileinfo['name'] = $_GET['upbigfilename']; + $fileinfo['size'] = $_GET['filesize']; + $fileinfo['lastModified'] = $_GET['lastModified']; + $filename = spurlencode( $fileinfo['name'] ); + $cachefilename = '.' . $fileinfo['lastModified'] . '_' . $fileinfo['size'] . '_' . $filename . '.tmp'; + $getoldupinfo=fetch_files(path_format($path . '/' . $cachefilename)); + //echo json_encode($getoldupinfo, JSON_PRETTY_PRINT); + if (isset($getoldupinfo['file'])&&$getoldupinfo['size']<5120) { + $getoldupinfo_j = curl_request($getoldupinfo['@microsoft.graph.downloadUrl']); + $getoldupinfo = json_decode($getoldupinfo_j , true); + if ( json_decode( curl_request($getoldupinfo['uploadUrl']), true)['@odata.context']!='' ) return output($getoldupinfo_j); + } + if (!$_SERVER['admin']) $filename = spurlencode( $fileinfo['name'] ) . '.scfupload'; + $response=MSAPI('createUploadSession',path_format($path1 . '/' . $filename),'{"item": { "@microsoft.graph.conflictBehavior": "fail" }}',$_SERVER['access_token']); + $responsearry = json_decode($response['body'],true); + if (isset($responsearry['error'])) return output($response['body'], $response['stat']); + $fileinfo['uploadUrl'] = $responsearry['uploadUrl']; + MSAPI('PUT', path_format($path1 . '/' . $cachefilename), json_encode($fileinfo, JSON_PRETTY_PRINT), $_SERVER['access_token'])['body']; + return output($response['body'], $response['stat']); + } + return output('error', 400); +} + +function main($path) +{ + global $exts; + global $constStr; + config_oauth(); + $_SERVER['list_path'] = getListpath($_SERVER['HTTP_HOST']); + if ($_SERVER['list_path']=='') $_SERVER['list_path'] = '/'; + $_SERVER['is_guestup_path'] = is_guestup_path($path); + $_SERVER['PHP_SELF'] = path_format($_SERVER['base_path'] . $path); + $_SERVER['ajax']=0; + if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) if ($_SERVER['HTTP_X_REQUESTED_WITH']=='XMLHttpRequest') $_SERVER['ajax']=1; + + $refresh_token = getConfig('refresh_token'); + if (!$refresh_token) return get_refresh_token(); + + if (getConfig('adminloginpage')=='') { + $adminloginpage = 'admin'; + } else { + $adminloginpage = getConfig('adminloginpage'); + } + if ($_GET[$adminloginpage]) { + if ($_GET['preview']) { + $url = $_SERVER['PHP_SELF'] . '?preview'; + } else { + $url = path_format($_SERVER['PHP_SELF'] . '/'); + } + if (getConfig('admin')!='') { + if ($_POST['password1']==getConfig('admin')) { + return adminform('admin',md5($_POST['password1']),$url); + } else return adminform(); + } else { + return output('', 302, [ 'Location' => $url ]); + } + } + if (getConfig('admin')!='') + if ( $_COOKIE['admin']==md5(getConfig('admin')) || $_POST['password1']==getConfig('admin') ) { + $_SERVER['admin']=1; + $_SERVER['needUpdate'] = needUpdate(); + } else { + $_SERVER['admin']=0; + } + if ($_GET['setup']) + if ($_SERVER['admin']) { + // setup Environments. 设置,对环境变量操作 + return EnvOpt($_SERVER['function_name'], $_SERVER['needUpdate']); + } else { + $url = path_format($_SERVER['PHP_SELF'] . '/'); + return output('', 302, [ 'Location' => $url ]); + } + $_SERVER['retry'] = 0; + + if (!($_SERVER['access_token'] = getcache('access_token'))) { + $ret = json_decode(curl_request( + $_SERVER['oauth_url'] . 'token', + 'client_id='. $_SERVER['client_id'] .'&client_secret='. $_SERVER['client_secret'] .'&grant_type=refresh_token&requested_token_use=on_behalf_of&refresh_token=' . $refresh_token + )['body'], true); + if (!isset($ret['access_token'])) { + error_log('failed to get access_token. response' . json_encode($ret)); + throw new Exception('failed to get access_token.'); + } + $_SERVER['access_token'] = $ret['access_token']; + savecache('access_token', $_SERVER['access_token']); + } + + if ($_SERVER['ajax']) { + if ($_GET['action']=='del_upload_cache'&&substr($_GET['filename'],-4)=='.tmp') { + // del '.tmp' without login. 无需登录即可删除.tmp后缀文件 + $tmp = MSAPI('DELETE',path_format(path_format($_SERVER['list_path'] . path_format($path)) . '/' . spurlencode($_GET['filename']) ),'',$_SERVER['access_token']); + $path1 = path_format($_SERVER['list_path'] . path_format($path)); + savecache('path_' . $path1, json_decode('{}',true), 1); + return output($tmp['body'],$tmp['stat']); + } + if ($_GET['action']=='uploaded_rename') { + // rename .scfupload file without login. + // 无需登录即可重命名.scfupload后缀文件,filemd5为用户提交,可被构造,问题不大,以后处理 + $oldname = spurlencode($_GET['filename']); + $pos = strrpos($oldname, '.'); + if ($pos>0) $ext = strtolower(substr($oldname, $pos)); + $oldname = path_format(path_format($_SERVER['list_path'] . path_format($path)) . '/' . $oldname . '.scfupload' ); + $data = '{"name":"' . $_GET['filemd5'] . $ext . '"}'; + //echo $oldname .'