Add rclone class

parent 6e8e3146
<?php
/**
*
* This Google Drive API handler class is a custom PHP library to handle the Google Drive API calls.
*
* @class GoogleDriveApi
* @author CodexWorld
* @link http://www.codexworld.com
* @version 1.0
*/
class GoogleDriveApi {
const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token';
const DRIVE_FILE_UPLOAD_URI = 'https://www.googleapis.com/upload/drive/v3/files';
const DRIVE_FILES_URI = 'https://www.googleapis.com/drive/v3/files/';
function __construct($params = array()) {
if (count($params) > 0){
$this->initialize($params);
}
}
function initialize($params = array()) {
if (count($params) > 0){
foreach ($params as $key => $val){
if (isset($this->$key)){
$this->$key = $val;
}
}
}
}
public function GetAccessToken($client_id, $redirect_uri, $client_secret, $code) {
$curlPost = 'client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret . '&code='. $code . '&grant_type=authorization_code';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::OAUTH2_TOKEN_URI);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if ($http_code != 200) {
$error_msg = 'Failed to receieve access token';
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}else{
$error_msg = !empty($data['error']['message'])?$data['error']['message']:'';
}
throw new Exception('Error '.$http_code.': '.$error_msg);
}
return $data;
}
public function DeleteFile($access_token, $file_id, $googleOauthURL = '') {
$apiURL = self::DRIVE_FILES_URI . $file_id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer '. $access_token));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($http_code != 200 && $http_code != 204){
$error_msg = 'Failed to delete file.';
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}else{
$error_msg = !empty($data['error']['message'])?$data['error']['message']:'';
}
if($http_code == 401 && !empty($googleOauthURL)){
unset($_SESSION['google_access_token']);
$error_msg .= '<br/>Click to <a href="'.$googleOauthURL.'">authenticate with Google Drive</a>';
}
throw new Exception('Error '.$http_code.': '.$error_msg);
}else{
return true;
}
}
}
?>
<?php
/**
* Copyright: 2022 (c)Franco (nextime) Lanza <franco@nexlab.it>
* License: GNU/GPL version 3.0
*
* This file is part of SexHackMe Wordpress Plugin.
*
* SexHackMe Wordpress Plugin is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* SexHackMe Wordpress Plugin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SexHackMe Wordpress Plugin. If not, see <https://www.gnu.org/licenses/>.
*/
namespace wp_SexHackMe;
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
if(!class_exists('SH_RClone')) {
class SH_RClone
{
protected $rclone;
function __construct()
{
if(get_option('sexhack_rclone_path', false)) $this->rclone = get_option('sexhack_rclone_path');
else $this->rclone = false;
if(!$this->rclone && is_callable('shell_exec') && false === stripos(ini_get('disable_functions'), 'shell_exec'))
{
$rclone_path = str_replace("\n", "", shell_exec("which rclone"));
if($rclone_path && is_executable($rclone_path))
{
$this->rclone = $rclone_path;
//set_option('sexhack_rclone_path', $this->rclone);
}
}
if($this->rclone && is_callable('shell_exec') && false === stripos(ini_get('disable_functions'), 'shell_exec')) {
add_filter('sh_download_url_filter', array($this, 'get_gdrive_download_url'));
}
}
function is_enabled()
{
return ((bool)$this->rclone && is_callable('shell_exec') && false === stripos(ini_get('disable_functions'), 'shell_exec'));
}
function get_remotes()
{
$res = array();
if($this->is_enabled())
{
$res = explode("\n", shell_exec($this->rclone." -q listremotes"));
}
return $res;
}
function get_path()
{
if(!$this->rclone) return '';
return $this->rclone;
}
function get_download_url($file)
{
if(!($file && is_string($file) && strlen($file)>3)) return $file;
$drivename = get_option('sexhack_rclone_gdrive_name', false);
$shared = get_option('sexhack_rclone_gdrive_shared', false);
if(!$drivename) return $file;
$add="";
if($this->is_enabled())
{
if((strlen($file) > 9) && (strncmp($file, "gdrive://", 9) === 0))
{
$gpath=substr($file, 9);
if(!strncmp($gpath, '/', 1)===0) $gpath='/'.$gpath;
if($shared) $add = "--drive-shared-with-me";
$shres = shell_exec($this->rclone." -q lsf $add ".$drivename.$gpath." -F ip");
if($shres)
{
$split = explode(";", $shres);
if(count($split) > 1)
{
if($split[0]) return $file="https://drive.google.com/uc?export=download&confirm=t&id=".$split[0];
}
}
}
}
return $file;
}
}
$GLOBALS['sh_rclone'] = new SH_RClone();
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment