OneManager-php/disk/Onedrive.php

1142 lines
61 KiB
PHP
Raw Permalink Normal View History

2021-01-15 03:10:08 +00:00
<?php
2021-10-06 11:28:05 +00:00
// https://docs.microsoft.com/en-us/graph/api/driveitem-get?view=graph-rest-1.0
// https://docs.microsoft.com/zh-cn/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http
// https://developer.microsoft.com/zh-cn/graph/graph-explorer
2021-01-15 03:10:08 +00:00
class Onedrive {
protected $access_token;
protected $disktag;
function __construct($tag) {
$this->disktag = $tag;
$this->redirect_uri = 'https://scfonedrive.github.io';
if (getConfig('client_id', $tag) && getConfig('client_secret', $tag)) {
$this->client_id = getConfig('client_id', $tag);
$this->client_secret = getConfig('client_secret', $tag);
} else {
$this->client_id = '734ef928-d74c-4555-8d1b-d942fa0a1a41';
2021-02-06 08:42:15 +00:00
$this->client_secret = '_I5gOpmG5vTC2Ts_K._wCW4nN1km~4Pk52';
2021-01-15 03:10:08 +00:00
}
$this->oauth_url = 'https://login.microsoftonline.com/common/oauth2/v2.0/';
$this->api_url = 'https://graph.microsoft.com/v1.0';
2021-03-28 08:38:37 +00:00
$this->scope = 'https://graph.microsoft.com/Files.ReadWrite.All https://graph.microsoft.com/Sites.ReadWrite.All offline_access';
2021-01-15 03:10:08 +00:00
$this->client_secret = urlencode($this->client_secret);
$this->scope = urlencode($this->scope);
$this->DownurlStrName = '@microsoft.graph.downloadUrl';
$this->ext_api_url = '/me/drive/root';
2021-02-23 09:40:39 +00:00
$res = $this->get_access_token(getConfig('refresh_token', $tag));
2021-01-15 03:10:08 +00:00
}
public function isfine()
{
if (!$this->access_token) return false;
else return true;
}
public function show_base_class()
{
return get_class();
//$tmp[0] = get_class();
//$tmp[1] = get_class($this);
//return $tmp;
}
2021-01-17 11:00:05 +00:00
public function ext_show_innerenv()
{
return [];
}
2021-01-15 03:10:08 +00:00
public function list_files($path = '/')
{
global $exts;
if (!($files = getcache('path_' . $path, $this->disktag))) {
$pos = splitlast($path, '/');
$parentpath = $pos[0];
if ($parentpath=='') $parentpath = '/';
$filename = strtolower($pos[1]);
2021-01-15 03:10:08 +00:00
if ($parentfiles = getcache('path_' . $parentpath, $this->disktag)) {
if (isset($parentfiles['children'][$filename][$this->DownurlStrName])) {
if (in_array(splitlast($filename,'.')[1], $exts['txt'])) {
if (!(isset($parentfiles['children'][$filename]['content'])&&$parentfiles['children'][$filename]['content']['stat']==200)) {
2022-02-18 08:13:38 +00:00
//$content1 = curl('GET', $parentfiles['children'][$filename][$this->DownurlStrName]);
//$parentfiles['children'][$filename]['content'] = $content1;
//savecache('path_' . $parentpath, $parentfiles, $this->disktag);
2022-02-19 05:41:00 +00:00
if ($parentfiles['children'][$filename]['size']<1024*1024) {
if (!(isset($parentfiles['children'][$filename]['content'])&&$parentfiles['children'][$filename]['content']['stat']==200)) {
$content1 = curl('GET', $parentfiles['children'][$filename][$this->DownurlStrName]);
2022-02-18 08:13:38 +00:00
$tmp = null;
$tmp = json_decode(json_encode($content1), true);
if ($tmp['body']===null) {
$txtcode = chkTxtCode($content1['body']);
if ($txtcode!==false) $tmp['body'] = iconv($txtcode, 'UTF-8//TRANSLIT', $content1['body']);
$tmp = json_decode(json_encode($tmp), true);
if ($tmp['body']) $content1['body'] = $tmp['body'];
}
2022-02-19 05:41:00 +00:00
$parentfiles['children'][$filename]['content'] = $content1;
savecache('path_' . $path, $parentfiles['children'][$filename], $this->disktag);
2022-02-18 08:13:38 +00:00
}
} else {
2022-02-19 05:41:00 +00:00
$parentfiles['children'][$filename]['content']['stat'] = 202;
$parentfiles['children'][$filename]['content']['body'] = 'File too large.';
2022-02-18 08:13:38 +00:00
}
2021-01-15 03:10:08 +00:00
}
}
return $this->files_format($parentfiles['children'][$filename]);
}
}
$url = $this->api_url . $this->ext_api_url;
if ($path !== '/') {
$url .= ':' . $path;
if (substr($url,-1)=='/') $url=substr($url,0,-1);
}
$url .= '?expand=children(select=id,name,size,file,folder,parentReference,lastModifiedDateTime,'.$this->DownurlStrName.')';
2021-03-06 07:59:18 +00:00
$arr = $this->MSAPI('GET', $url);
2021-01-15 03:10:08 +00:00
//echo $url . '<br><pre>' . json_encode($arr, JSON_PRETTY_PRINT) . '</pre>';
if ($arr['stat']<500) {
$files = json_decode($arr['body'], true);
//echo '<pre>' . json_encode($files, JSON_PRETTY_PRINT) . '</pre>';
if (isset($files['folder'])) {
if ($files['folder']['childCount']>200) {
// files num > 200 , then get nextlink
$page = $_POST['pagenum']==''?1:$_POST['pagenum'];
if ($page>1)
//if (!($files = getcache('path_1' . $path . '_' . $page, $this->disktag)))
{
$children = $this->fetch_files_children($path, $page);
//echo '<pre>' . json_encode($children, JSON_PRETTY_PRINT) . '</pre>';
$files['children'] = $children['value'];
//$files['children'] = children_name($files['children']);
$files['folder']['page'] = $page;
//savecache('path_' . $path . '_' . $page, $files, $this->disktag);
2021-01-15 03:10:08 +00:00
}
} else {
// files num < 200 , then cache
if (isset($files['children'])) {
$files['children'] = children_name($files['children']);
}
2021-01-15 03:10:08 +00:00
savecache('path_' . $path, $files, $this->disktag);
}
}
if (isset($files['file'])) {
2021-05-13 05:59:36 +00:00
if (in_array(strtolower(splitlast($files['name'],'.')[1]), $exts['txt'])) {
if ($files['size']<1024*1024) {
if (!(isset($files['content'])&&$files['content']['stat']==200)) {
$content1 = curl('GET', $files[$this->DownurlStrName]);
$tmp = null;
$tmp = json_decode(json_encode($content1), true);
if ($tmp['body']===null) {
2022-02-18 08:13:38 +00:00
$txtcode = chkTxtCode($content1['body']);
if ($txtcode!==false) $tmp['body'] = iconv($txtcode, 'UTF-8//TRANSLIT', $content1['body']);
2021-05-13 05:59:36 +00:00
$tmp = json_decode(json_encode($tmp), true);
2022-02-18 08:13:38 +00:00
if ($tmp['body']) $content1['body'] = $tmp['body'];
2021-05-13 05:59:36 +00:00
}
$files['content'] = $content1;
savecache('path_' . $path, $files, $this->disktag);
}
} else {
$files['content']['stat'] = 202;
$files['content']['body'] = 'File too large.';
2021-01-15 03:10:08 +00:00
}
}
}
if (isset($files['error'])) {
$files['error']['stat'] = $arr['stat'];
}
} else {
//error_log1($arr['body']);
2021-01-15 03:10:08 +00:00
$files = json_decode($arr['body'], true);
if (isset($files['error'])) {
$files['error']['stat'] = $arr['stat'];
} else {
$files['error']['stat'] = 503;
$files['error']['code'] = 'unknownError';
2021-10-21 03:40:55 +00:00
$files['error']['message'] = 'unknownError ' . $arr['body'] . " ~";
2021-01-15 03:10:08 +00:00
}
//$files = json_decode( '{"unknownError":{ "stat":'.$arr['stat'].',"message":"'.$arr['body'].'"}}', true);
//error_log1(json_encode($files, JSON_PRETTY_PRINT));
2021-01-15 03:10:08 +00:00
}
}
//echo '<pre>' . json_encode($files, JSON_PRETTY_PRINT) . '</pre>';
return $this->files_format($files);
}
protected function files_format($files)
{
if (isset($files['file'])) {
$tmp['type'] = 'file';
$tmp['id'] = $files['id'];
$tmp['name'] = $files['name'];
$tmp['time'] = $files['lastModifiedDateTime'];
$tmp['size'] = $files['size'];
$tmp['mime'] = $files['file']['mimeType'];
$tmp['url'] = $files[$this->DownurlStrName];
$tmp['content'] = $files['content'];
} elseif (isset($files['folder'])) {
$tmp['type'] = 'folder';
$tmp['id'] = $files['id'];
$tmp['name'] = $files['name'];
$tmp['time'] = $files['lastModifiedDateTime'];
$tmp['size'] = $files['size'];
$tmp['childcount'] = $files['folder']['childCount'];
$tmp['page'] = $files['folder']['page'];
foreach ($files['children'] as $file) {
$filename = strtolower($file['name']);
2021-01-15 03:10:08 +00:00
if (isset($file['file'])) {
$tmp['list'][$filename]['type'] = 'file';
2021-01-15 03:10:08 +00:00
//var_dump($file);
//echo $file['name'] . ':' . $this->DownurlStrName . ':' . $file[$this->DownurlStrName] . PHP_EOL;
$tmp['list'][$filename]['url'] = $file[$this->DownurlStrName];
$tmp['list'][$filename]['mime'] = $file['file']['mimeType'];
2021-01-15 03:10:08 +00:00
} elseif (isset($file['folder'])) {
$tmp['list'][$filename]['type'] = 'folder';
2021-01-15 03:10:08 +00:00
}
$tmp['list'][$filename]['id'] = $file['id'];
$tmp['list'][$filename]['name'] = $file['name'];
$tmp['list'][$filename]['time'] = $file['lastModifiedDateTime'];
$tmp['list'][$filename]['size'] = $file['size'];
2021-01-15 03:10:08 +00:00
}
} elseif (isset($files['error'])) {
return $files;
}
//error_log1(json_encode($tmp));
2021-10-21 03:40:55 +00:00
//echo '<pre>' . json_encode($tmp, JSON_PRETTY_PRINT) . '</pre>';
2021-01-15 03:10:08 +00:00
return $tmp;
}
protected function fetch_files_children($path, $page, $getNextlink = false) {
$children = getcache('files_' . $path . '_page_' . $page, $this->disktag);
if (!$children) {
$url = $this->api_url . $this->ext_api_url;
if ($path !== '/') {
$url .= ':' . $path;
if (substr($url,-1)=='/') $url=substr($url,0,-1);
$url .= ':';
}
$url .= '/children?$top=' . ($page-1)*200 . '&$select=id,name,size,file,folder,parentReference,lastModifiedDateTime,' . $this->DownurlStrName;
2021-03-06 07:59:18 +00:00
$children_tmp = json_decode($this->MSAPI('GET', $url)['body'], true);
//echo $url . '<br><pre>' . json_encode($children_tmp, JSON_PRETTY_PRINT) . '</pre>';
$p = 1;
$i = 0;
foreach ($children_tmp['value'] as $child) {
$i++;
$value_name = 'child_' . $p;
${$value_name}['value'][] = $child;
if ($i==200) {
savecache('files_' . $path . '_page_' . $p, ${$value_name}, $this->disktag);
unset(${$value_name});
$i = 0;
$p++;
}
}
$url = $children_tmp['@odata.nextLink'];
2021-03-06 07:59:18 +00:00
$children_tmp = json_decode($this->MSAPI('GET', $url)['body'], true);
//echo $url . '<br><pre>' . json_encode($children_tmp, JSON_PRETTY_PRINT) . '</pre>';
$p = $page;
$i = 0;
foreach ($children_tmp['value'] as $child) {
$i++;
$value_name = 'child_' . $p;
${$value_name}['value'][] = $child;
if ($i==200) {
savecache('files_' . $path . '_page_' . $p, ${$value_name}, $this->disktag);
//unset(${$value_name});
$i = 0;
$p++;
2021-01-15 03:10:08 +00:00
}
}
if ($i!=0) savecache('files_' . $path . '_page_' . $p, ${$value_name}, $this->disktag);
$value_name = 'child_' . $page;
return ${$value_name};
}
return $children;
/*if ($getNextlink) {
if (isset($children['@odata.nextLink'])) {
return $children;
} else {
if ($page*200>9800) {
$children_tmp = fetch_files_children($path, floor($page/49)*49, 1);
$url = $children_tmp['@odata.nextLink'];
$children = json_decode(curl('GET', $url, false, ['Authorization' => 'Bearer ' . $this->access_token])['body'], true);
}
}
}*/
}
protected function fetch_files_children1($files, $path, $page)
{
$maxpage = ceil($files['folder']['childCount']/200);
if (!($children = getcache('files_' . $path . '_page_' . $page, $this->disktag))) {
2021-01-15 03:10:08 +00:00
$pageinfochange=0;
for ($page1=$page;$page1>=1;$page1--) {
$page3=$page1-1;
$url = getcache('nextlink_' . $path . '_page_' . $page3, $this->disktag);
if ($url == '') {
if ($page1==1) {
$url = $this->api_url . $this->ext_api_url;
if ($path !== '/') {
$url .= ':' . $path;
if (substr($url,-1)=='/') $url=substr($url,0,-1);
$url .= ':';
}
$url .= '/children?$select=id,name,size,file,folder,parentReference,lastModifiedDateTime,'.$this->DownurlStrName;
2021-03-06 07:59:18 +00:00
$children = json_decode($this->MSAPI('GET', $url)['body'], true);
2021-01-15 03:10:08 +00:00
// echo $url . '<br><pre>' . json_encode($children, JSON_PRETTY_PRINT) . '</pre>';
savecache('files_' . $path . '_page_' . $page1, $children, $this->disktag);
2021-01-15 03:10:08 +00:00
$nextlink=getcache('nextlink_' . $path . '_page_' . $page1, $this->disktag);
if ($nextlink!=$children['@odata.nextLink']) {
savecache('nextlink_' . $path . '_page_' . $page1, $children['@odata.nextLink'], $this->disktag);
$pageinfocache['nextlink_' . $path . '_page_' . $page1] = $children['@odata.nextLink'];
$pageinfocache = clearbehindvalue($path,$page1,$maxpage,$pageinfocache);
$pageinfochange = 1;
}
$url = $children['@odata.nextLink'];
for ($page2=$page1+1;$page2<=$page;$page2++) {
sleep(1);
2021-03-06 07:59:18 +00:00
$children = json_decode($this->MSAPI('GET', $url)['body'], true);
savecache('files_' . $path . '_page_' . $page2, $children, $this->disktag);
2021-01-15 03:10:08 +00:00
$nextlink=getcache('nextlink_' . $path . '_page_' . $page2, $this->disktag);
if ($nextlink!=$children['@odata.nextLink']) {
savecache('nextlink_' . $path . '_page_' . $page2, $children['@odata.nextLink'], $this->disktag);
$pageinfocache['nextlink_' . $path . '_page_' . $page2] = $children['@odata.nextLink'];
$pageinfocache = clearbehindvalue($path,$page2,$maxpage,$pageinfocache);
$pageinfochange = 1;
}
$url = $children['@odata.nextLink'];
}
//echo $url . '<br><pre>' . json_encode($children, JSON_PRETTY_PRINT) . '</pre>';
return $children;
/*
2021-01-15 03:10:08 +00:00
$files['children'] = $children['value'];
$files['folder']['page']=$page;
$pageinfocache['filenum'] = $files['folder']['childCount'];
$pageinfocache['dirsize'] = $files['size'];
$pageinfocache['cachesize'] = $cachefile['size'];
$pageinfocache['size'] = $files['size']-$cachefile['size'];
2021-03-06 07:59:18 +00:00
if ($pageinfochange == 1) $this->MSAPI('PUT', path_format($path.'/'.$cachefilename), json_encode($pageinfocache, JSON_PRETTY_PRINT))['body'];
return $files;*/
2021-01-15 03:10:08 +00:00
}
} else {
for ($page2=$page3+1;$page2<=$page;$page2++) {
sleep(1);
2021-03-06 07:59:18 +00:00
$children = json_decode($this->MSAPI('GET', $url)['body'], true);
savecache('files_' . $path . '_page_' . $page2, $children, $this->disktag, 3300);
2021-01-15 03:10:08 +00:00
$nextlink=getcache('nextlink_' . $path . '_page_' . $page2, $this->disktag);
if ($nextlink!=$children['@odata.nextLink']) {
savecache('nextlink_' . $path . '_page_' . $page2, $children['@odata.nextLink'], $this->disktag, 3300);
$pageinfocache['nextlink_' . $path . '_page_' . $page2] = $children['@odata.nextLink'];
$pageinfocache = clearbehindvalue($path,$page2,$maxpage,$pageinfocache);
$pageinfochange = 1;
}
$url = $children['@odata.nextLink'];
}
//echo $url . '<br><pre>' . json_encode($children, JSON_PRETTY_PRINT) . '</pre>';
return $children;
/*$files['children'] = $children['value'];
2021-01-15 03:10:08 +00:00
$files['folder']['page']=$page;
$pageinfocache['filenum'] = $files['folder']['childCount'];
$pageinfocache['dirsize'] = $files['size'];
$pageinfocache['cachesize'] = $cachefile['size'];
$pageinfocache['size'] = $files['size']-$cachefile['size'];
2021-03-06 07:59:18 +00:00
if ($pageinfochange == 1) $this->MSAPI('PUT', path_format($path.'/'.$cachefilename), json_encode($pageinfocache, JSON_PRETTY_PRINT))['body'];
return $files;*/
2021-01-15 03:10:08 +00:00
}
}
}/* else {
2021-01-15 03:10:08 +00:00
$files['folder']['page']=$page;
for ($page4=1;$page4<=$maxpage;$page4++) {
if (!($url = getcache('nextlink_' . $path . '_page_' . $page4, $this->disktag))) {
if ($files['folder'][$path.'_'.$page4]!='') savecache('nextlink_' . $path . '_page_' . $page4, $files['folder'][$path.'_'.$page4], $this->disktag);
} else {
$files['folder'][$path.'_'.$page4] = $url;
}
}
}*/
return $children;
//return $files;
2021-01-15 03:10:08 +00:00
}
public function Rename($file, $newname) {
$oldname = spurlencode($file['name']);
$oldname = path_format($file['path'] . '/' . $oldname);
$data = '{"name":"' . $newname . '"}';
//echo $oldname;
2021-08-04 07:16:08 +00:00
if ($file['id']) $result = $this->MSAPI('PATCH', "/items/" . $file['id'], $data);
else $result = $this->MSAPI('PATCH', $oldname, $data);
2021-01-15 03:10:08 +00:00
return output(json_encode($this->files_format(json_decode($result['body'], true))), $result['stat']);
}
public function Delete($file) {
$filename = spurlencode($file['name']);
$filename = path_format($file['path'] . '/' . $filename);
//echo $filename;
2021-10-06 11:28:05 +00:00
if ($file['id']) $result = $this->MSAPI('DELETE', "/items/" . $file['id']);
else $result = $this->MSAPI('DELETE', $filename);
if ($result['stat']!=204) $r_body = json_encode($this->files_format(json_decode($result['body'], true)));
return output($r_body, $result['stat']);
//return output($result['body'], $result['stat']);
2021-01-15 03:10:08 +00:00
}
public function Encrypt($folder, $passfilename, $pass) {
2021-10-21 03:40:55 +00:00
$filename = '/items/' . $folder['id'] . ':/' . urlencode($passfilename);
2021-01-17 05:33:33 +00:00
if ($pass==='') {
2021-10-06 11:28:05 +00:00
$result = $this->MSAPI('DELETE', $filename);
2021-01-17 05:33:33 +00:00
} else {
2021-03-06 07:59:18 +00:00
$result = $this->MSAPI('PUT', $filename, $pass);
2021-01-17 05:33:33 +00:00
}
2021-01-15 03:10:08 +00:00
$path1 = $folder['path'];
if ($path1!='/'&&substr($path1, -1)=='/') $path1 = substr($path1, 0, -1);
savecache('path_' . $path1 . '/?password', '', $this->disktag, 1);
return output(json_encode($this->files_format(json_decode($result['body'], true))), $result['stat']);
//return output($result['body'], $result['stat']);
2021-01-15 03:10:08 +00:00
}
public function Move($file, $folder) {
$filename = spurlencode($file['name']);
$filename = path_format($file['path'] . '/' . $filename);
$data = '{"parentReference":{"path": "/drive/root:' . $folder['path'] . '"}}';
2021-10-06 11:28:05 +00:00
if ($file['id']) $result = $this->MSAPI('PATCH', "/items/" . $file['id'], $data);
else $result = $this->MSAPI('PATCH', $filename, $data);
2021-01-15 03:10:08 +00:00
$path2 = spurlencode($folder['path'], '/');
if ($path2!='/'&&substr($path2, -1)=='/') $path2 = substr($path2, 0, -1);
savecache('path_' . $path2, json_decode('{}', true), $this->disktag, 1);
return output(json_encode($this->files_format(json_decode($result['body'], true))), $result['stat']);
//return output($result['body'], $result['stat']);
2021-01-15 03:10:08 +00:00
}
public function Copy($file) {
$filename = spurlencode($file['name']);
$filename = path_format($file['path'] . '/' . $filename);
$namearr = splitlast($file['name'], '.');
date_default_timezone_set('UTC');
if ($namearr[0]!='') {
$newname = $namearr[0] . ' (' . date("Ymd\THis\Z") . ')';
if ($namearr[1]!='') $newname .= '.' . $namearr[1];
} else {
$newname = '.' . $namearr[1] . ' (' . date("Ymd\THis\Z") . ')';
}
$data = '{ "name": "' . $newname . '" }';
2021-10-06 11:28:05 +00:00
if ($file['id']) $result = $this->MSAPI('copy', "/items/" . $file['id'], $data);
else $result = $this->MSAPI('copy', $filename, $data);
2021-01-15 03:10:08 +00:00
/*$num = 0;
while ($result['stat']==409 && json_decode($result['body'], true)['error']['code']=='nameAlreadyExists') {
$num++;
if ($namearr[0]!='') {
$newname = $namearr[0] . ' (' . getconstStr('Copy') . ' ' . $num . ')';
if ($namearr[1]!='') $newname .= '.' . $namearr[1];
} else {
$newname = '.' . $namearr[1] . ' ('.getconstStr('Copy'). ' ' . $num .')';
}
//$newname = spurlencode($newname);
$data = '{ "name": "' . $newname . '" }';
2021-03-06 07:59:18 +00:00
$result = $this->MSAPI('copy', $filename, $data);
2021-01-15 03:10:08 +00:00
}*/
return output(json_encode($this->files_format(json_decode($result['body'], true))), $result['stat']);
//return output($result['body'], $result['stat']);
2021-01-15 03:10:08 +00:00
}
public function Edit($file, $content) {
/*TXT一般不会超过4M不用二段上传
$filename = $path1 . ':/createUploadSession';
$response=MSAPI('POST',$filename,'{"item": { "@microsoft.graph.conflictBehavior": "replace" }}',$_SERVER['access_token']);
$uploadurl=json_decode($response,true)['uploadUrl'];
echo MSAPI('PUT',$uploadurl,$data,$_SERVER['access_token']);*/
2021-03-06 07:59:18 +00:00
$result = $this->MSAPI('PUT', $file['path'], $content);
2021-01-15 03:10:08 +00:00
//return output($result['body'], $result['stat']);
//echo $result;
$resultarry = json_decode($result['body'],true);
if (isset($resultarry['error'])) return message($resultarry['error']['message']. '<hr><a href="javascript:history.back(-1)">'.getconstStr('Back').'</a>','Error', 403);
else return output('success', 0);
}
public function Create($parent, $type, $name, $content = '') {
if ($type=='file') {
$filename = spurlencode($name);
$filename = path_format($parent['path'] . '/' . $filename);
2021-03-06 07:59:18 +00:00
$result = $this->MSAPI('PUT', $filename, $content);
2021-01-15 03:10:08 +00:00
}
if ($type=='folder') {
$data = '{ "name": "' . $name . '", "folder": { }, "@microsoft.graph.conflictBehavior": "rename" }';
2021-03-06 07:59:18 +00:00
$result = $this->MSAPI('children', $parent['path'], $data);
2021-01-15 03:10:08 +00:00
}
//savecache('path_' . $path1, json_decode('{}',true), $_SERVER['disktag'], 1);
return output(json_encode($this->files_format(json_decode($result['body'], true))), $result['stat']);
//return output($result['body'], $result['stat']);
2021-01-15 03:10:08 +00:00
}
public function AddDisk() {
global $constStr;
global $EnvConfigs;
$envs = '';
foreach ($EnvConfigs as $env => $v) if (isCommonEnv($env)) $envs .= '\'' . $env . '\', ';
2022-01-16 07:58:42 +00:00
$url = $_SERVER['PHP_SELF'];
2021-01-15 03:10:08 +00:00
//$this->api_url = splitfirst($_SERVER['api_url'], '/v1.0')[0] . '/v1.0';
2021-01-17 11:00:05 +00:00
if (isset($_GET['Finish'])) {
2021-01-15 03:10:08 +00:00
if ($this->access_token == '') {
$refresh_token = getConfig('refresh_token', $this->disktag);
if (!$refresh_token) {
$html = 'No refresh_token config, please AddDisk again or wait minutes.<br>' . $this->disktag;
$title = 'Error';
return message($html, $title, 201);
}
$response = $this->get_access_token($refresh_token);
2021-03-06 07:59:18 +00:00
if (!$response) return message($this->error['body'], $this->error['stat'] . ' Error', $this->error['stat']);
2021-01-15 03:10:08 +00:00
}
$tmp = null;
2021-08-05 10:11:46 +00:00
$tmp['Driver'] = get_class($this);
2021-01-15 03:10:08 +00:00
if ($_POST['DriveType']=='Onedrive') {
2021-01-15 04:48:13 +00:00
/*$api = $this->api_url . '/me';
2021-01-15 03:10:08 +00:00
$arr = curl('GET', $api, '', [ 'Authorization' => 'Bearer ' . $this->access_token ], 1);
if ($arr['stat']==200) {
$userid = json_decode($arr['body'], true)['id'];
$api = $this->api_url . '/users/' . $userid . '/drive';
$arr = curl('GET', $api, '', [ 'Authorization' => 'Bearer ' . $this->access_token ], 1);
if ($arr['stat']!=200) return message($arr['stat'] . '<br>' . $api . '<br>' . $arr['body'], 'Get User Drive ID', $arr['stat']);
$tmp['DriveId'] = json_decode($arr['body'], true)['id'];
} elseif ($arr['stat']==403||$arr['stat']==401) {
// 403世纪不让列me401个人也不给拿
$api = $this->api_url . '/me/drive';
} else {
return message($arr['stat'] . $arr['body'], 'Get User ID', $arr['stat']);
2021-01-15 04:48:13 +00:00
}*/
2022-01-14 02:29:29 +00:00
if ($tmp['Driver']=='Sharepoint') $tmp['Driver'] = 'Onedrive';
elseif ($tmp['Driver']=='SharepointCN') $tmp['Driver'] = 'OnedriveCN';
2021-01-18 07:59:39 +00:00
$tmp['sharepointSite'] = '';
$tmp['siteid'] = '';
2021-01-15 03:10:08 +00:00
} elseif ($_POST['DriveType']=='Custom') {
// sitename计算siteid
$tmp1 = $this->get_siteid($_POST['sharepointSite']);
if (isset($tmp1['stat'])) return message($arr['stat'] . $tmp1['body'], 'Get Sharepoint Site ID ' . $_POST['sharepointSite'], $tmp1['stat']);
$siteid = $tmp1;
//$api = $this->api_url . '/sites/' . $siteid . '/drive/';
//$arr = curl('GET', $api, '', [ 'Authorization' => 'Bearer ' . $this->access_token ], 1);
//if ($arr['stat']!=200) return message($arr['stat'] . $arr['body'], 'Get Sharepoint Drive ID ' . $_POST['DriveType'], $arr['stat']);
$tmp['siteid'] = $siteid;
$tmp['sharepointSite'] = $_POST['sharepointSite'];
2022-01-14 02:29:29 +00:00
if ($tmp['Driver']=='Onedrive') $tmp['Driver'] = 'Sharepoint';
elseif ($tmp['Driver']=='OnedriveCN') $tmp['Driver'] = 'SharepointCN';
2021-01-15 03:10:08 +00:00
} else {
// 直接是siteid
$tmp['siteid'] = $_POST['DriveType'];
$tmp['sharepointSite'] = $_POST['sharepointSiteUrl'];
2022-01-14 02:29:29 +00:00
if ($tmp['Driver']=='Onedrive') $tmp['Driver'] = 'Sharepoint';
elseif ($tmp['Driver']=='OnedriveCN') $tmp['Driver'] = 'SharepointCN';
2021-01-15 03:10:08 +00:00
}
$response = setConfigResponse( setConfig($tmp, $this->disktag) );
if (api_error($response)) {
$html = api_error_msg($response);
$title = 'Error';
return message($html, $title, 201);
} else {
2021-08-17 11:51:17 +00:00
$html .= '<script>
2021-01-15 03:10:08 +00:00
var expd = new Date();
expd.setTime(expd.getTime()+1);
var expires = "expires="+expd.toGMTString();
document.cookie=\'disktag=; path=/; \'+expires;
2021-08-17 11:51:17 +00:00
var i = 0;
2021-08-26 10:09:57 +00:00
var status = "' . $response['DplStatus'] . '";
2021-08-17 11:51:17 +00:00
var uploadList = setInterval(function(){
if (document.getElementById("dis").style.display=="none") {
console.log(i++);
} else {
clearInterval(uploadList);
2022-01-14 02:29:29 +00:00
location.href = "' . $url . '?setup&disktag=' . $_GET['disktag'] . '";
2021-08-17 11:51:17 +00:00
}
}, 1000);
2021-01-15 03:10:08 +00:00
</script>';
2022-01-14 02:29:29 +00:00
return message($html, getconstStr('Wait'), 201, 1);
2021-01-15 03:10:08 +00:00
}
}
2021-01-17 11:00:05 +00:00
if (isset($_GET['SelectDrive'])) {
2021-01-18 07:59:39 +00:00
if (get_class($this)=='Sharelink') return message('Can not change to other.', 'Back', 201);
2021-01-15 03:10:08 +00:00
if ($this->access_token == '') {
$refresh_token = getConfig('refresh_token', $this->disktag);
if (!$refresh_token) {
$html = 'No refresh_token config, please AddDisk again or wait minutes.<br>' . $this->disktag;
$title = 'Error';
return message($html, $title, 201);
}
$response = $this->get_access_token($refresh_token);
2021-03-06 07:59:18 +00:00
if (!$response) return message($this->error['body'], $this->error['stat'] . ' Error', $this->error['stat']);
2021-01-15 03:10:08 +00:00
}
$api = $this->api_url . '/sites/root';
2021-03-14 09:34:18 +00:00
$arr = $this->MSAPI('GET', $api);
2021-01-15 03:10:08 +00:00
$Tenant = json_decode($arr['body'], true)['webUrl'];
$api = $this->api_url . '/me/followedSites';
2021-03-14 09:34:18 +00:00
$arr = $this->MSAPI('GET', $api);
2021-01-28 15:29:40 +00:00
if (!($arr['stat']==200||$arr['stat']==403||$arr['stat']==400||$arr['stat']==404)) return message($arr['stat'] . json_encode(json_decode($arr['body']), JSON_PRETTY_PRINT), 'Get followedSites', $arr['stat']);
error_log1($arr['body']);
2021-01-15 03:10:08 +00:00
$sites = json_decode($arr['body'], true)['value'];
2021-01-17 11:00:05 +00:00
$title = 'Select Driver';
2021-01-15 03:10:08 +00:00
$html = '
<div>
2022-01-14 02:29:29 +00:00
<form name="form1" action="?Finish&disktag=' . $_GET['disktag'] . '&AddDisk=' . get_class($this) . '" method="post" onsubmit="return notnull(this);">
<label><input type="radio" name="DriveType" value="Onedrive">' . 'Use Onedrive ' . getconstStr(' ') . '</label><br>';
2021-01-15 03:10:08 +00:00
if ($sites[0]!='') foreach ($sites as $k => $v) {
$html .= '
<label>
<input type="radio" name="DriveType" value="' . $v['id'] . '" onclick="document.getElementById(\'sharepointSiteUrl\').value=\'' . $v['webUrl'] . '\';">' . 'Use Sharepoint: <br><div style="width:100%;margin:0px 35px">webUrl: ' . $v['webUrl'] . '<br>siteid: ' . $v['id'] . '</div>
2022-01-14 02:29:29 +00:00
<input type="hidden" id="' . $v['id'] . '" name="' . $v['id'] . '" value="' . $v['webUrl'] . '">
2021-01-15 03:10:08 +00:00
</label>';
}
$html .= '
<input type="hidden" id="sharepointSiteUrl" name="sharepointSiteUrl" value="">
<label>
<input type="radio" name="DriveType" value="Custom" id="Custom">' . 'Use Other Sharepoint:' . getconstStr(' ') . '<br>
<div style="width:100%;margin:0px 35px"><a href="' . $Tenant . '/_layouts/15/sharepoint.aspx" target="_blank">' . getconstStr('GetSharepointSiteAddress') . '</a><br>
<input type="text" name="sharepointSite" style="width:100%;" placeholder="' . getconstStr('InputSharepointSiteAddress') . '" onclick="document.getElementById(\'Custom\').checked=\'checked\';">
</div>
</label><br>
';
$html .= '
<input type="submit" value="' . getconstStr('Submit') . '">
</form>
</div>
<script>
2022-01-14 02:29:29 +00:00
var driver = "' . $_GET['AddDisk'] . '";
if (driver=="Onedrive"||driver=="OnedriveCN") {
document.form1.DriveType[0].checked = true;
} else {
var siteid = "' . getConfig('siteid', $this->disktag) . '";
if (siteid==="") {
document.form1.DriveType[document.form1.DriveType.length-1].checked = true;
} else {
for (i=0; i<document.form1.DriveType.length; i++) {
if (document.form1.DriveType[i].value==siteid) {
document.form1.DriveType[i].checked = true;
document.getElementById(\'sharepointSiteUrl\').value = document.getElementById(siteid).value;
2021-01-15 03:10:08 +00:00
}
}
2022-01-14 02:29:29 +00:00
if (document.getElementById(\'sharepointSiteUrl\').value=="") {
document.form1.DriveType[document.form1.DriveType.length-1].checked = true;
document.getElementById(\'sharepointSite\').value = "' . getConfig('sharepointSite', $this->disktag) . '";
}
2021-01-15 03:10:08 +00:00
}
2022-01-14 02:29:29 +00:00
}
function notnull(t)
{
if (t.DriveType.value==\'\') {
alert(\'Select a Disk\');
return false;
}
if (t.DriveType.value==\'Custom\') {
if (t.sharepointSite.value==\'\') {
alert(\'sharepoint Site Address\');
return false;
}
}
return true;
}
</script>
';
2021-01-15 03:10:08 +00:00
return message($html, $title, 201);
}
if (isset($_GET['install2']) && isset($_GET['code'])) {
$tmp = curl('POST', $this->oauth_url . 'token', 'client_id=' . $this->client_id .'&client_secret=' . $this->client_secret . '&grant_type=authorization_code&requested_token_use=on_behalf_of&redirect_uri=' . $this->redirect_uri . '&code=' . $_GET['code']);
if ($tmp['stat']==200) $ret = json_decode($tmp['body'], true);
if (isset($ret['refresh_token'])) {
$refresh_token = $ret['refresh_token'];
$str = '
refresh_token :<br>';
$str .= '
<textarea readonly style="width: 95%">' . $refresh_token . '</textarea><br><br>
' . getconstStr('SavingToken') . '
<script>
var texta=document.getElementsByTagName(\'textarea\');
for(i=0;i<texta.length;i++) {
texta[i].style.height = texta[i].scrollHeight + \'px\';
}
</script>';
2022-01-14 02:29:29 +00:00
$tmptoken['Driver'] = $_GET['AddDisk'];
2021-01-15 03:10:08 +00:00
$tmptoken['refresh_token'] = $refresh_token;
$tmptoken['token_expires'] = time()+7*24*60*60;
$response = setConfigResponse( setConfig($tmptoken, $this->disktag) );
if (api_error($response)) {
$html = api_error_msg($response);
$title = 'Error';
return message($html, $title, 201);
} else {
savecache('access_token', $ret['access_token'], $this->disktag, $ret['expires_in'] - 60);
2021-08-17 11:51:17 +00:00
$html .= '<script>
var i = 0;
2021-08-26 10:09:57 +00:00
var status = "' . $response['DplStatus'] . '";
2021-08-17 11:51:17 +00:00
var uploadList = setInterval(function(){
if (document.getElementById("dis").style.display=="none") {
console.log(i++);
} else {
clearInterval(uploadList);
2022-01-14 02:29:29 +00:00
location.href = "' . $url . '?AddDisk=' . $_GET['AddDisk'] . '&disktag=' . $_GET['disktag'] . '&SelectDrive";
2021-08-17 11:51:17 +00:00
}
}, 1000);
</script>';
return message($html, getconstStr('Wait') . ' 3s', 201, 1);
2021-01-15 03:10:08 +00:00
}
}
return message('<pre>' . json_encode(json_decode($tmp['body']), JSON_PRETTY_PRINT) . '</pre>', $tmp['stat']);
//return message('<pre>' . json_encode($ret, JSON_PRETTY_PRINT) . '</pre>', 500);
}
if (isset($_GET['install1'])) {
2022-01-14 02:29:29 +00:00
//if (get_class($this)=='Onedrive' || get_class($this)=='OnedriveCN') {
2021-01-15 03:10:08 +00:00
return message('
<a href="" id="a1">' . getconstStr('JumptoOffice') . '</a>
<script>
2021-01-17 11:00:05 +00:00
url=location.protocol + "//" + location.host + "' . $url . '?install2&disktag=' . $_GET['disktag'] . '&AddDisk=' . get_class($this) . '";
2021-01-15 03:10:08 +00:00
url="' . $this->oauth_url . 'authorize?scope=' . $this->scope . '&response_type=code&client_id=' . $this->client_id . '&redirect_uri=' . $this->redirect_uri . '&state=' . '"+encodeURIComponent(url);
document.getElementById(\'a1\').href=url;
//window.open(url,"_blank");
location.href = url;
</script>
', getconstStr('Wait') . ' 1s', 201);
2022-01-14 02:29:29 +00:00
//} else {
// return message('Something error, retry after a few seconds.', 'Retry', 201);
//}
2021-01-15 03:10:08 +00:00
}
if (isset($_GET['install0'])) {
if ($_POST['disktag_add']!='') {
$_POST['disktag_add'] = preg_replace('/[^0-9a-zA-Z|_]/i', '', $_POST['disktag_add']);
$f = substr($_POST['disktag_add'], 0, 1);
if (strlen($_POST['disktag_add'])==1) $_POST['disktag_add'] .= '_';
if (isCommonEnv($_POST['disktag_add'])) {
2021-08-17 11:51:17 +00:00
return message('Do not input ' . $envs . '<br><button onclick="location.href = location.href;">'.getconstStr('Refresh').'</button>', 'Error', 400);
2021-01-15 03:10:08 +00:00
} elseif (!(('a'<=$f && $f<='z') || ('A'<=$f && $f<='Z'))) {
return message('Please start with letters<br><button onclick="location.href = location.href;">'.getconstStr('Refresh').'</button>
<script>
var expd = new Date();
expd.setTime(expd.getTime()+1);
var expires = "expires="+expd.toGMTString();
document.cookie=\'disktag=; path=/; \'+expires;
2021-08-17 11:51:17 +00:00
</script>', 'Error', 400);
2021-01-15 03:10:08 +00:00
}
$tmp = null;
2021-01-17 11:00:05 +00:00
// clear envs
2021-01-15 03:10:08 +00:00
foreach ($EnvConfigs as $env => $v) if (isInnerEnv($env)) $tmp[$env] = '';
//$this->disktag = $_POST['disktag_add'];
$tmp['disktag_add'] = $_POST['disktag_add'];
$tmp['diskname'] = $_POST['diskname'];
2022-01-14 02:29:29 +00:00
//$tmp['Driver'] = $_POST['Drive_ver'];
$tmp['Driver'] = $_GET['AddDisk'];
2021-01-15 03:10:08 +00:00
if ($_POST['Drive_ver']=='Sharelink') {
$tmp['shareurl'] = $_POST['shareurl'];
} else {
if ($_POST['Drive_ver']=='Onedrive' && $_POST['NT_Drive_custom']=='on') {
$tmp['client_id'] = $_POST['NT_client_id'];
$tmp['client_secret'] = $_POST['NT_client_secret'];
} elseif ($_POST['Drive_ver']=='OnedriveCN' && $_POST['CN_Drive_custom']=='on') {
$tmp['client_id'] = $_POST['CN_client_id'];
$tmp['client_secret'] = $_POST['CN_client_secret'];
}
}
$response = setConfigResponse( setConfig($tmp, $this->disktag) );
if (api_error($response)) {
$html = api_error_msg($response);
$title = 'Error';
2021-08-17 11:51:17 +00:00
return message($html, $title, 400);
2021-01-15 03:10:08 +00:00
} else {
$title = getconstStr('MayinEnv');
2021-08-17 11:51:17 +00:00
$html = getconstStr('Wait');
2022-01-14 02:29:29 +00:00
if ($_POST['Drive_ver']!='Sharelink') $url .= '?install1&disktag=' . $_GET['disktag'] . '&AddDisk=' . $_GET['AddDisk'];
2021-08-17 11:51:17 +00:00
$html .= '<script>
var i = 0;
2021-08-26 10:09:57 +00:00
var status = "' . $response['DplStatus'] . '";
2021-08-17 11:51:17 +00:00
var uploadList = setInterval(function(){
if (document.getElementById("dis").style.display=="none") {
console.log(i++);
} else {
clearInterval(uploadList);
location.href = "' . $url . '";
}
}, 1000);
</script>';
return message($html, $title, 201, 1);
2021-01-15 03:10:08 +00:00
}
2021-08-17 11:51:17 +00:00
2021-01-15 03:10:08 +00:00
}
}
$html = '
<div>
2022-01-14 02:29:29 +00:00
<form id="form1" name="form1" action="" method="post" onsubmit="return notnull(this);">
2021-01-15 03:10:08 +00:00
' . getconstStr('DiskTag') . ': (' . getConfig('disktag') . ')
<input type="text" name="disktag_add" placeholder="' . getconstStr('EnvironmentsDescription')['disktag'] . '" style="width:100%"><br>
' . getconstStr('DiskName') . ':
<input type="text" name="diskname" placeholder="' . getconstStr('EnvironmentsDescription')['diskname'] . '" style="width:100%"><br>
<br>
<div>
<label><input type="radio" name="Drive_ver" value="Onedrive" onclick="document.getElementById(\'NT_custom\').style.display=\'\';document.getElementById(\'CN_custom\').style.display=\'none\';document.getElementById(\'inputshareurl\').style.display=\'none\';">MS: ' . getconstStr('DriveVerMS') . '</label><br>
<div id="NT_custom" style="display:none;margin:0px 35px">
<label><input type="checkbox" name="NT_Drive_custom" onclick="document.getElementById(\'NT_secret\').style.display=(this.checked?\'\':\'none\');">' . getconstStr('CustomIdSecret') . '</label><br>
<div id="NT_secret" style="display:none;margin:10px 35px">
<a href="https://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps" target="_blank">' . getconstStr('GetSecretIDandKEY') . '</a><br>
2021-12-01 06:24:38 +00:00
return_uri(Reply URL):<br>' . $this->redirect_uri . '<br>
2021-01-15 03:10:08 +00:00
client_id:<input type="text" name="NT_client_id" style="width:100%" placeholder="a1b2c345-90ab-cdef-ghij-klmnopqrstuv"><br>
client_secret:<input type="text" name="NT_client_secret" style="width:100%"><br>
</div>
</div><br>
<label><input type="radio" name="Drive_ver" value="OnedriveCN" onclick="document.getElementById(\'CN_custom\').style.display=\'\';document.getElementById(\'NT_custom\').style.display=\'none\';document.getElementById(\'inputshareurl\').style.display=\'none\';">CN: ' . getconstStr('DriveVerCN') . '</label><br>
<div id="CN_custom" style="display:none;margin:0px 35px">
<label><input type="checkbox" name="CN_Drive_custom" onclick="document.getElementById(\'CN_secret\').style.display=(this.checked?\'\':\'none\');">' . getconstStr('CustomIdSecret') . '</label><br>
<div id="CN_secret" style="display:none;margin:10px 35px">
<a href="https://portal.azure.cn/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps" target="_blank">' . getconstStr('GetSecretIDandKEY') . '</a><br>
2021-12-01 06:24:38 +00:00
return_uri(Reply URL):<br>' . $this->redirect_uri . '<br>
2021-01-15 03:10:08 +00:00
client_id:<input type="text" name="CN_client_id" style="width:100%" placeholder="a1b2c345-90ab-cdef-ghij-klmnopqrstuv"><br>
client_secret:<input type="text" name="CN_client_secret" style="width:100%"><br>
</div>
</div><br>
<label><input type="radio" name="Drive_ver" value="Sharelink" onclick="document.getElementById(\'CN_custom\').style.display=\'none\';document.getElementById(\'inputshareurl\').style.display=\'\';document.getElementById(\'NT_custom\').style.display=\'none\';">Sharelink: ' . getconstStr('DriveVerShareurl') . '</label><br>
<div id="inputshareurl" style="display:none;margin:0px 35px">
' . getconstStr('UseShareLink') . '
<input type="text" name="shareurl" style="width:100%" placeholder="https://xxxx.sharepoint.com/:f:/g/personal/xxxxxxxx/mmmmmmmmm?e=XXXX"><br>
</div>
</div>
<br>';
2021-03-06 07:59:18 +00:00
if ($_SERVER['language']=='zh-cn') $html .= '你要理解 scfonedrive.github.io 是github上的静态网站<br><font color="red">除非github真的挂掉</font>了,<br>不然,稍后你如果<font color="red">连不上</font>,请检查你的运营商或其它“你懂的”问题!<br>';
2021-01-15 03:10:08 +00:00
$html .='
<input type="submit" value="' . getconstStr('Submit') . '">
</form>
</div>
<script>
2022-01-14 02:29:29 +00:00
var driver = "' . get_class($this) . '";
if (driver=="Onedrive" || driver=="Sharepoint") {
document.form1.Drive_ver[0].checked = true;
document.getElementById(\'NT_custom\').style.display=\'\';
//document.getElementById(\'CN_custom\').style.display=\'\';
//document.getElementById(\'inputshareurl\').style.display=\'none\';
}
if (driver=="OnedriveCN" || driver=="SharepointCN") {
document.form1.Drive_ver[1].checked = true;
document.getElementById(\'CN_custom\').style.display=\'\';
}
if (driver=="Sharelink") {
document.form1.Drive_ver[2].checked = true;
document.getElementById(\'inputshareurl\').style.display=\'\';
}
2021-01-15 03:10:08 +00:00
function notnull(t)
{
if (t.disktag_add.value==\'\') {
alert(\'' . getconstStr('DiskTag') . '\');
return false;
}
envs = [' . $envs . '];
if (envs.indexOf(t.disktag_add.value)>-1) {
2022-01-14 02:29:29 +00:00
alert("Do not input \"" + t.disktag_add.value + "\" in ' . getconstStr('DiskTag') . '");
2021-01-15 03:10:08 +00:00
return false;
}
2022-01-29 14:02:04 +00:00
var reg = /^[a-zA-Z]([_a-zA-Z0-9]{1,})$/;
2021-01-15 03:10:08 +00:00
if (!reg.test(t.disktag_add.value)) {
alert(\'' . getconstStr('TagFormatAlert') . '\');
return false;
}
if (t.Drive_ver.value==\'\') {
alert(\'Select a Driver\');
return false;
}
if (t.Drive_ver.value==\'Sharelink\') {
if (t.shareurl.value==\'\') {
alert(\'shareurl\');
return false;
}
2022-01-14 02:29:29 +00:00
driver = \'Sharelink\';
2021-01-15 03:10:08 +00:00
} else {
2022-01-14 02:29:29 +00:00
if (t.Drive_ver.value==\'Onedrive\') {
if (t.NT_Drive_custom.checked==true) {
if (t.NT_client_secret.value==\'\'||t.NT_client_id.value==\'\') {
alert(\'client_id & client_secret\');
return false;
}
2021-01-15 03:10:08 +00:00
}
2022-01-14 02:29:29 +00:00
if (driver!="Onedrive" && driver!="Sharepoint") driver = "Onedrive";
2021-01-15 03:10:08 +00:00
}
2022-01-14 02:29:29 +00:00
if (t.Drive_ver.value==\'OnedriveCN\') {
if (t.CN_Drive_custom.checked==true) {
if (t.CN_client_secret.value==\'\'||t.CN_client_id.value==\'\') {
alert(\'client_id & client_secret\');
return false;
}
2021-01-15 03:10:08 +00:00
}
2022-01-14 02:29:29 +00:00
if (driver!="OnedriveCN" && driver!="SharepointCN") driver = "OnedriveCN";
2021-01-15 03:10:08 +00:00
}
}
2022-01-14 02:29:29 +00:00
document.getElementById("form1").action="?install0&disktag=" + t.disktag_add.value + "&AddDisk=" + driver;
2021-01-15 03:10:08 +00:00
return true;
}
</script>';
$title = 'Select Account Type';
return message($html, $title, 201);
}
protected function get_access_token($refresh_token) {
2021-03-01 09:38:15 +00:00
if (!$refresh_token) {
$tmp['stat'] = 0;
$tmp['body'] = 'No refresh_token';
$this->error = $tmp;
return false;
}
2021-01-15 03:10:08 +00:00
if (!($this->access_token = getcache('access_token', $this->disktag))) {
$p=0;
while ($response['stat']==0&&$p<3) {
$response = curl('POST', $this->oauth_url . 'token', 'client_id=' . $this->client_id . '&client_secret=' . $this->client_secret . '&grant_type=refresh_token&requested_token_use=on_behalf_of&refresh_token=' . $refresh_token );
$p++;
}
if ($response['stat']==200) $ret = json_decode($response['body'], true);
if (!isset($ret['access_token'])) {
error_log1($this->oauth_url . 'token' . '?client_id=' . $this->client_id . '&client_secret=' . $this->client_secret . '&grant_type=refresh_token&requested_token_use=on_behalf_of&refresh_token=' . substr($refresh_token, 0, 20) . '******' . substr($refresh_token, -20));
2021-02-23 09:40:39 +00:00
error_log1('failed to get [' . $this->disktag . '] access_token. response: ' . $response['body']);
2021-01-15 03:10:08 +00:00
$response['body'] = json_encode(json_decode($response['body']), JSON_PRETTY_PRINT);
$response['body'] .= '\nfailed to get [' . $this->disktag . '] access_token.';
2021-02-23 09:40:39 +00:00
$this->error = $response;
return false;
2021-01-15 03:10:08 +00:00
//throw new Exception($response['stat'].', failed to get ['.$this->disktag.'] access_token.'.$response['body']);
}
$tmp = $ret;
2021-03-01 09:38:15 +00:00
$tmp['access_token'] = substr($tmp['access_token'], 0, 10) . '******';
$tmp['refresh_token'] = substr($tmp['refresh_token'], 0, 10) . '******';
error_log1('[' . $this->disktag . '] Get access token:' . json_encode($tmp, JSON_PRETTY_PRINT));
2021-01-15 03:10:08 +00:00
$this->access_token = $ret['access_token'];
savecache('access_token', $this->access_token, $this->disktag, $ret['expires_in'] - 300);
if (time()>getConfig('token_expires', $this->disktag)) setConfig([ 'refresh_token' => $ret['refresh_token'], 'token_expires' => time()+7*24*60*60 ], $this->disktag);
2021-01-16 03:28:19 +00:00
return true;
2021-01-15 03:10:08 +00:00
}
2021-01-15 11:26:39 +00:00
return true;
2021-01-15 03:10:08 +00:00
}
protected function get_siteid($sharepointSite)
{
//$sharepointSite = getConfig('sharepointSite', $this->disktag);
while (substr($sharepointSite, -1)=='/') $sharepointSite = substr($sharepointSite, 0, -1);
$tmp = splitlast($sharepointSite, '/');
if ($tmp[1]==urldecode($tmp[1])) {
$sharepointname = urlencode($tmp[1]);
} else {
$sharepointname = $tmp[1];
}
2021-01-15 03:10:08 +00:00
$tmp = splitlast($tmp[0], '/');
//if (getConfig('Driver', $this->disktag)=='Onedrive') $url = 'https://graph.microsoft.com/v1.0/sites/root:/' . $tmp[1] . '/' . $sharepointname;
//if (getConfig('Driver', $this->disktag)=='OnedriveCN') $url = 'https://microsoftgraph.chinacloudapi.cn/v1.0/sites/root:/' . $tmp[1] . '/' . $sharepointname;
$url = $this->api_url . '/sites/root:/' . $tmp[1] . '/' . $sharepointname;
2021-01-15 03:10:08 +00:00
$i=0;
$response = [];
while ($url!=''&&$response['stat']!=200&&$i<4) {
2021-03-14 09:34:18 +00:00
$response = $this->MSAPI('GET', $url);
2021-01-15 03:10:08 +00:00
$i++;
}
if ($response['stat']!=200) {
error_log1('failed to get siteid. response' . json_encode($response));
2021-01-15 03:10:08 +00:00
$response['body'] .= '\nfailed to get siteid.';
return $response;
//throw new Exception($response['stat'].', failed to get siteid.'.$response['body']);
}
return json_decode($response['body'],true)['id'];
}
2021-01-16 04:17:06 +00:00
public function del_upload_cache($path)
2021-01-15 03:10:08 +00:00
{
error_log1('del.tmp:GET,'.json_encode($_GET,JSON_PRETTY_PRINT));
2021-01-15 03:10:08 +00:00
$tmp = splitlast($_GET['filename'], '/');
if ($tmp[1]!='') {
$filename = $tmp[0] . '/.' . $_GET['filelastModified'] . '_' . $_GET['filesize'] . '_' . $tmp[1] . '.tmp';
} else {
$filename = '.' . $_GET['filelastModified'] . '_' . $_GET['filesize'] . '_' . $_GET['filename'] . '.tmp';
}
$filename = path_format( path_format($_SERVER['list_path'] . path_format($path)) . '/' . spurlencode($filename, '/') );
2021-03-06 07:59:18 +00:00
$tmp = $this->MSAPI('DELETE', $filename, '');
2021-01-15 03:10:08 +00:00
$path1 = path_format($_SERVER['list_path'] . path_format($path));
if ($path1!='/'&&substr($path1,-1)=='/') $path1=substr($path1,0,-1);
savecache('path_' . $path1, json_decode('{}',true), $this->disktag, 1);
return output($tmp['body'],$tmp['stat']);
}
public function get_thumbnails_url($path = '/')
{
$thumb_url = getcache('thumb_'.$path, $this->disktag);
if ($thumb_url=='') {
$url = $this->api_url . $this->ext_api_url;
if ($path !== '/') {
$url .= ':' . $path;
if (substr($url,-1)=='/') $url=substr($url,0,-1);
}
$url .= ':/thumbnails/0/medium';
2021-03-14 09:00:39 +00:00
$files = json_decode($this->MSAPI('GET', $url)['body'], true);
2021-01-15 03:10:08 +00:00
if (isset($files['url'])) {
savecache('thumb_' . $path, $files['url'], $this->disktag);
$thumb_url = $files['url'];
}
}
return $thumb_url;
}
2022-04-16 02:38:01 +00:00
public function smallfileupload($path, $tmpfile) {
if (!$_SERVER['admin']) {
$tmp1 = splitlast($tmpfile['name'], '.');
if ($tmp1[0]==''||$tmp1[1]=='') $filename = md5_file($tmpfile['tmp_name']);
else $filename = md5_file($tmpfile['tmp_name']) . '.' . $tmp1[1];
} else {
$filename = $tmpfile['name'];
}
$content = file_get_contents($tmpfile['tmp_name']);
$result = $this->MSAPI('PUT', path_format($_SERVER['list_path'] . '/' . $path . '/' . $filename), $content);
$res = $this->files_format(json_decode($result['body'], true));
if (isset($res['url'])) $res['url'] = $_SERVER['host'] . path_format($_SERVER['base_disk_path'] . '/' . $path . '/' . $filename);
return output(json_encode($res, JSON_UNESCAPED_SLASHES), $result['stat']);
}
2021-01-15 03:10:08 +00:00
public function bigfileupload($path)
{
if ($_POST['upbigfilename']=='') return output('error: no file name', 400);
if (!is_numeric($_POST['filesize'])) return output('error: no file size', 400);
if (!$_SERVER['admin']) if (!isset($_POST['filemd5'])) return output('error: no file md5', 400);
$tmp = splitlast($_POST['upbigfilename'], '/');
if ($tmp[1]!='') {
$fileinfo['name'] = $tmp[1];
if ($_SERVER['admin']) $fileinfo['path'] = $tmp[0];
} else {
$fileinfo['name'] = $_POST['upbigfilename'];
}
$fileinfo['size'] = $_POST['filesize'];
$fileinfo['filelastModified'] = $_POST['filelastModified'];
if ($_SERVER['admin']) {
$filename = spurlencode($_POST['upbigfilename'], '/');
} else {
$tmp1 = splitlast($fileinfo['name'], '.');
if ($tmp1[0]==''||$tmp1[1]=='') $filename = $_POST['filemd5'];
else $filename = $_POST['filemd5'] . '.' . $tmp1[1];
}
if ($fileinfo['size']>10*1024*1024) {
$cachefilename = spurlencode( $fileinfo['path'] . '/.' . $fileinfo['filelastModified'] . '_' . $fileinfo['size'] . '_' . $fileinfo['name'] . '.tmp', '/');
2021-04-25 03:07:55 +00:00
$getoldupinfo = $this->list_files(path_format($path . '/' . $cachefilename));
//error_log1(json_encode($getoldupinfo, JSON_PRETTY_PRINT));
if (isset($getoldupinfo['url'])&&$getoldupinfo['size']<5120) {
2021-01-15 03:10:08 +00:00
$getoldupinfo_j = curl('GET', $getoldupinfo['url']);
$getoldupinfo = json_decode($getoldupinfo_j['body'], true);
if ( json_decode( curl('GET', $getoldupinfo['uploadUrl'])['body'], true)['@odata.context']!='' ) return output($getoldupinfo_j['body'], $getoldupinfo_j['stat']);
}
}
2021-03-06 07:59:18 +00:00
$response = $this->MSAPI('createUploadSession', path_format($path . '/' . $filename), '{"item": { "@microsoft.graph.conflictBehavior": "fail" }}');
2021-01-15 03:10:08 +00:00
if ($response['stat']<500) {
$responsearry = json_decode($response['body'],true);
if (isset($responsearry['error'])) return output($response['body'], $response['stat']);
$fileinfo['uploadUrl'] = $responsearry['uploadUrl'];
2021-03-06 07:59:18 +00:00
if ($fileinfo['size']>10*1024*1024) $this->MSAPI('PUT', path_format($path . '/' . $cachefilename), json_encode($fileinfo, JSON_PRETTY_PRINT));
2021-01-15 03:10:08 +00:00
}
return output($response['body'], $response['stat']);
}
2021-03-14 09:00:39 +00:00
public function getDiskSpace() {
if (!($diskSpace = getcache('diskSpace', $this->disktag))) {
$url = $this->api_url . $this->ext_api_url;
if (substr($url, -5)=='/root') $url = substr($url, 0, -5);
else return $url;
$response = json_decode($this->MSAPI('GET', $url)['body'], true)['quota'];
$used = size_format($response['used']);
$total = size_format($response['total']);
$diskSpace = $used . ' / ' . $total;
savecache('diskSpace', $diskSpace, $this->disktag);
}
return $diskSpace;
}
2021-01-15 03:10:08 +00:00
2021-06-24 07:55:24 +00:00
protected function MSAPI($method, $path, $data = '', $headers = [])
2021-01-15 03:10:08 +00:00
{
2021-03-06 07:59:18 +00:00
$activeLimit = getConfig('activeLimit', $this->disktag);
if ($activeLimit!='') {
if ($activeLimit>time()) {
$tmp['error']['code'] = 'Retry-After';
$tmp['error']['message'] = 'MS limit until ' . date('Y-m-d H:i:s', $activeLimit);
return [ 'stat' => 429, 'body' => json_encode($tmp) ];
} else {
setConfig(['activeLimit' => ''], $this->disktag);
}
}
2021-01-15 03:10:08 +00:00
if (substr($path,0,7) == 'http://' or substr($path,0,8) == 'https://') {
2021-03-06 07:59:18 +00:00
$url = $path;
2021-01-15 03:10:08 +00:00
} else {
$url = $this->api_url . $this->ext_api_url;
if ($path=='' or $path=='/') {
$url .= '/';
2021-08-04 07:16:08 +00:00
} elseif (substr($path, 0, 6)=="/items") {
$url = substr($url, 0, -5);
$url .= $path;
2021-01-15 03:10:08 +00:00
} else {
$url .= ':' . $path;
if (substr($url,-1)=='/') $url=substr($url,0,-1);
}
2021-06-17 09:18:44 +00:00
if ($method=='GET') {
$method = 'GET'; // do nothing
} elseif ($method=='PUT') {
2021-01-15 03:10:08 +00:00
if ($path=='' or $path=='/') {
$url .= 'content';
} else {
$url .= ':/content';
}
$headers['Content-Type'] = 'text/plain';
} elseif ($method=='PATCH') {
$headers['Content-Type'] = 'application/json';
} elseif ($method=='POST') {
$headers['Content-Type'] = 'application/json';
} elseif ($method=='DELETE') {
$headers['Content-Type'] = 'application/json';
} else {
if ($path=='' or $path=='/') {
$url .= $method;
2021-10-07 04:39:49 +00:00
} elseif (substr($path, 0, 6)=="/items") {
2021-10-06 11:28:05 +00:00
$url .= '/' . $method;
2021-10-07 04:39:49 +00:00
} else {
$url .= ':/' . $method;
2021-01-15 03:10:08 +00:00
}
$method='POST';
$headers['Content-Type'] = 'application/json';
}
}
2021-03-06 07:59:18 +00:00
$headers['Authorization'] = 'Bearer ' . $this->access_token;
2021-01-15 03:10:08 +00:00
if (!isset($headers['Accept'])) $headers['Accept'] = '*/*';
//if (!isset($headers['Referer'])) $headers['Referer'] = $url;*
$sendHeaders = array();
foreach ($headers as $headerName => $headerVal) {
$sendHeaders[] = $headerName . ': ' . $headerVal;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
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_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $sendHeaders);
2021-03-06 07:59:18 +00:00
$retry = 0;
$response = [];
while ($retry<3&&!$response['stat']) {
$response['body'] = curl_exec($ch);
$response['stat'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$retry++;
}
2021-01-15 03:10:08 +00:00
//$response['Location'] = curl_getinfo($ch);
2021-03-06 07:59:18 +00:00
if ($response['stat']==429) {
$res = json_decode($response['body'], true);
$retryAfter = $res['error']['retryAfterSeconds'];
2021-03-14 09:34:18 +00:00
$retryAfter_n = (int)$retryAfter;
if ($retryAfter_n>0) {
$tmp1['activeLimit'] = $retryAfter_n + time();
setConfig($tmp1, $this->disktag);
}
2021-03-06 07:59:18 +00:00
}
2021-01-15 03:10:08 +00:00
curl_close($ch);
2021-04-25 03:07:55 +00:00
/*error_log1($response['stat'].'
2021-01-15 03:10:08 +00:00
'.$response['body'].'
'.$url.'
2021-04-25 03:07:55 +00:00
');*/
2021-01-15 03:10:08 +00:00
return $response;
}
}