Skip to content

Commit

Permalink
Merge pull request #32 from Tai7sy/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Tai7sy authored Mar 2, 2020
2 parents 58b3342 + 7c2ede2 commit 2f8daa8
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 103 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1 align="center">OneDriveFly</h1>
<p align="center">
<a href="https://github.com/Tai7sy/OneDriveFly/releases"><img src="https://img.shields.io/github/v/release/Tai7sy/OneDriveFly.svg?style=flat-square" alt="License"></a>
<a href="https://github.com/Tai7sy/OneDriveFly/releases"><img src="https://img.shields.io/github/v/release/Tai7sy/OneDriveFly.svg?style=flat-square" alt="release"></a>
<a href="https://github.com/Tai7sy/OneDriveFly/releases"><img src="https://img.shields.io/github/size/Tai7sy/OneDriveFly/dist/index.php.svg?style=flat-square" alt="Travis"></a>
<a href="https://travis-ci.org/Tai7sy/OneDriveFly"><img src="https://img.shields.io/travis/Tai7sy/OneDriveFly.svg?style=flat-square" alt="Travis"></a>
<a href="https://github.com/Tai7sy/OneDriveFly/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square" alt="License"></a>
Expand Down
112 changes: 61 additions & 51 deletions dist/index.php

Large diffs are not rendered by default.

108 changes: 59 additions & 49 deletions php/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function fc_entry($request, $context)
}


if (in_array(php_sapi_name(), ['apache2handler', 'cgi-fcgi'])) {
if (in_array(php_sapi_name(), ['apache2handler', 'cgi-fcgi', 'fpm-fcgi'])) {
cgi_entry();
}

Expand Down Expand Up @@ -117,10 +117,13 @@ function ($path) {
$account['path'] = '/';
}


$path = [
'relative' => $relative_path,
'absolute' => path_format($account['path'] . '/' . join('/', $path))
];
$is_admin = $config['is_admin'] = !empty($config['admin_password']) && $request->cookies->get('admin_password') === $config['admin_password'];
$is_image_path = $config['is_image_path'] = in_array($path['absolute'], $account['path_image']);

try {
$account['driver'] = new OneDrive($account['refresh_token'],
Expand Down Expand Up @@ -148,57 +151,65 @@ function ($path) {
// ajax request
if ($request->isXmlHttpRequest()) {
$response = false;
switch ($request->get('action')) {
case trans('Create'):
$file_path = path_format($path['absolute'] . '/' . $request->get('create_name'), true);
switch ($request->get('create_type')) {
default:
case 'file':
$response = $account['driver']->put($file_path, $request->get('create_content'));
break;
case 'folder':
$response = $account['driver']->makeDirectory($file_path);
break;
}
break;
case trans('Encrypt'):
$file_path = path_format($path['absolute'] . '/' . $request->get('encrypt_folder') . '/' . $config['password_file'], true);
$response = $account['driver']->put($file_path, $request->get('encrypt_newpass'));
break;
case trans('Move'):
$file_path = path_format($path['absolute'] . '/' . $request->get('move_name'), true);
if ($request->get('move_folder') === '/../') {
if ($path['absolute'] == '/') {
$response = ['error' => 'cannot move'];
break;
if ($is_admin) {
switch ($request->get('action')) {
case trans('Create'):
$file_path = path_format($path['absolute'] . '/' . $request->get('create_name'), true);
switch ($request->get('create_type')) {
default:
case 'file':
$response = $account['driver']->put($file_path, $request->get('create_content'));
break;
case 'folder':
$response = $account['driver']->makeDirectory($file_path);
break;
}
$new_path = $path['absolute'] . '/../';
break;
case trans('Encrypt'):
$file_path = path_format($path['absolute'] . '/' . $request->get('encrypt_folder') . '/' . $config['password_file'], true);
$response = $account['driver']->put($file_path, $request->get('encrypt_newpass'));
break;
case trans('Move'):
$file_path = path_format($path['absolute'] . '/' . $request->get('move_name'), true);
if ($request->get('move_folder') === '/../') {
if ($path['absolute'] == '/') {
$response = ['error' => 'cannot move'];
break;
}
$new_path = $path['absolute'] . '/../';

} else {
$new_path = $path['absolute'] . '/' . $request->get('move_folder');
}
$new_path = path_format($new_path . '/' . $request->get('move_name'), true);
$response = $account['driver']->move($file_path, $new_path);
break;
case trans('Rename'):
$file_path = path_format($path['absolute'] . '/' . $request->get('rename_oldname'), true);
$response = $account['driver']->move($file_path, path_format($path['absolute'] . '/' . $request->get('rename_newname')));
break;
case trans('Delete'):
$file_path = path_format($path['absolute'] . '/' . $request->get('delete_name'), true);
$response = $account['driver']->delete($file_path);
break;
case 'upload':
$file_path = path_format($path['absolute'] . '/' . $request->get('filename'), true);
$response = $account['driver']->uploadUrl($file_path);
break;
} else {
$new_path = $path['absolute'] . '/' . $request->get('move_folder');
}
$new_path = path_format($new_path . '/' . $request->get('move_name'), true);
$response = $account['driver']->move($file_path, $new_path);
break;
case trans('Rename'):
$file_path = path_format($path['absolute'] . '/' . $request->get('rename_oldname'), true);
$response = $account['driver']->move($file_path, path_format($path['absolute'] . '/' . $request->get('rename_newname')));
break;
case trans('Delete'):
$file_path = path_format($path['absolute'] . '/' . $request->get('delete_name'), true);
$response = $account['driver']->delete($file_path);
break;
}
}

if ($is_admin || $is_image_path) {
switch ($request->get('action')) {
case 'upload': // create a upload session
$file_path = path_format($path['absolute'] . '/' . $request->get('filename'), true);
$response = $account['driver']->uploadUrl($file_path);
break;
}
}

if ($response)
return response($response, !$response || isset($response['error']) ? 500 : 200);
}

// preview -> edit file
if ($request->isMethod('POST')) {
if ($is_admin && $request->isMethod('POST')) {
if ($request->query->has('preview')) {
$account['driver']->put($path['absolute'], $request->get('content'));
}
Expand All @@ -218,12 +229,12 @@ function ($path) {
try {
$error = ['error' => ['message' => $e->getMessage()]];
if ($config['debug']) {
$error['error']['message'] = trace_error($e);
$error['error']['message'] = $e->getMessage() . error_trace($e);
}
return render($account, $path, $error);
} catch (Throwable $e) {
@ob_get_clean();
return message($e->getMessage(), 'Error', $config['debug'] ? trace_error($e) : null, 500);
return message($e->getMessage(), 'Error', $config['debug'] ? error_trace($e) : null, 500);
}
}
}
Expand Down Expand Up @@ -659,12 +670,11 @@ function render($account, $path, $files)
global $config;

$request = request();
$is_admin = $request->cookies->get('admin_password') === $config['admin_password'];
$is_admin = $config['is_admin'] === true;
$is_image_path = $config['is_image_path'] === true;
$base_url = $request->getBaseUrl();
if ($base_url == '') $base_url = '/';
$status_code = 200;

$is_image_path = in_array($path['absolute'], $account['path_image']);
$is_video = false;
$readme = false;
@ob_start();
Expand Down
5 changes: 4 additions & 1 deletion php/library/OneDrive.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ public function __construct($refresh_token, $provider = 'MS', $oauth = [])
$this->oauth = array_merge($this->oauth, $oauth);

// 只能用个文件缓存代替
if (is_writable(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'test.qdrive')) {

if (is_writable(sys_get_temp_dir())) {
$this->cache = new FilesystemCache(sys_get_temp_dir(), '.qdrive');
} elseif (is_writable('/tmp/')) {
$this->cache = new FilesystemCache('/tmp/', '.qdrive');
} else {
$this->cache = new VoidCache();
}
Expand Down
2 changes: 1 addition & 1 deletion php/library/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function get_timezone($timezone = '8')
* @param \Exception $e
* @return string
*/
function trace_error($e)
function error_trace($e)
{
$str = '<pre>' . $e->getTraceAsString() . '</pre>';
$str = str_replace(realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR, '', $str);
Expand Down

0 comments on commit 2f8daa8

Please sign in to comment.