Warning: chmod(): Operation not permitted in /var/www/hopeinstoughton_www/wp-content/plugins/simple-universal-google-analytics/main.php on line 8 [0] in function chmod in /var/www/hopeinstoughton_www/wp-content/plugins/simple-universal-google-analytics/main.php on line 8 [1] in function include_once in /var/www/hopeinstoughton_www/wp-settings.php on line 560 [2] in function require_once in /var/www/hopeinstoughton_www/wp-config.php on line 85 [3] in function require_once in /var/www/hopeinstoughton_www/wp-load.php on line 50 [4] in function require_once in /var/www/hopeinstoughton_www/wp-blog-header.php on line 13 [5] in function require in /var/www/hopeinstoughton_www/index.php on line 17
| Server IP : 94.177.8.99 / Your IP : 216.73.217.165 Web Server : Apache/2.4.58 (Ubuntu) System : Linux aries 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64 User : www-data ( 33) PHP Version : 8.1.34 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/pjy_hub/modules/googleVideo/model/ |
Upload File : |
<?php
use \Google\Client;
use \Google\Service\Drive;
// class_alias(FileList::class, 'Google_Service_Drive_FileList');
class Google2_Model extends Database_model {
private $keyfile;
private $driveId;
const CURL = '/usr/bin/curl';
public function __construct() {
parent::__construct();
}
public function KeyFile() {
if (func_num_args()) {
$this->keyfile = func_get_arg(0);
return $this;
}
return $this->keyfile;
}
public function DriveId() {
if (func_num_args()) {
$this->driveId = func_get_arg(0);
return $this;
}
return $this->driveId;
}
public function download($fileobject) {
if ($fileobject->location == 'drive') {
$client = $this->getGoogleClient();
$access_token = $client->fetchAccessTokenWithAssertion();
$tempfile = tempnam(sys_get_temp_dir(), 'gdA');
$safefile = escapeshellarg($tempfile);
$token = $access_token['access_token'];
$uri = sprintf('/drive/v3/files/%s', $fileobject->id);
$command = static::CURL . " --silent --header \"Authorization: Bearer {$token}\" \"https://www.googleapis.com{$uri}?alt=media\" -o {$safefile}";
`$command > /dev/null 2>&1`;
$fileobject->temp = $tempfile;
error_log("downloading {$fileobject->name} to $tempfile");
}
}
public function inline_download($config) {
// download to temp file (add range)
$client = $this->getGoogleClient();
if (1) {
$param = [];
$filename = 'CDN/' . $config->id[0] . '/' . $config->id . '.' . $config->ext;
try{
$fileId = 0;
$res = $this->Search($filename);
if (count($res)) $res = $res[0];
$fileId = $res->id;
} catch(\Exception $e) {
$param['message'] = $e->getMessage();
}
} else {
$service = new Drive($client);
$param = $this->getFileId($service, $config->id . '.' . $config->ext);
$fileId = $param['fileId'];
}
$echo = property_exists($config, 'echo') ? $config->echo : true;
if ($param['message']) {
if ($echo) {
header('Content-type: text/plain');
die('Message: ' . $param['message']);
} else {
return $param['message'];
}
} elseif (!$fileId) {
return FALSE; // not found
}
$uri = sprintf('/drive/v3/files/%s', $fileId);
if (is_null($config->range) && $echo) {
$access_token = $client->fetchAccessTokenWithAssertion();
$tempfile = tempnam(sys_get_temp_dir(), 'gdB');
$safefile = escapeshellarg($tempfile);
$token = $access_token['access_token'];
$command = static::CURL . " --silent --header \"Authorization: Bearer {$token}\" \"https://www.googleapis.com{$uri}?alt=media\" -o {$safefile}";
`$command > /dev/null 2>&1`;
header('Content-type: ' . $config->mimetype);
return $tempfile;
}
$http = $client->authorize();
$options = [
'query' => ['alt' => 'media'],
'headers' => [],
];
if (!is_null($config->range)) {
$options['headers']['Range'] = $config->range;
}
$response = $http->request('GET', $uri, $options);
$content = $response->getBody()->getContents();
if ($echo) {
while (ob_get_level())
ob_end_clean();
if (!is_null($config->range)) {
$full = 0;
header('HTTP/1.1 206 Partial Content');
if ($contentrange = $response->getHeader('Content-Range')) {
$contentrange = $contentrange[0];
header('Content-Range: ' . $contentrange);
if (preg_match('~bytes 0-(\d+)/(\d+)~', $contentrange, $value)) {
$last = $value[1];
$total = $value[2];
if ($last + 1 == $total) {
$full = 1;
}
}
}
header('Content-type: ' , $config->mimetype);
if ($full) {
$tempfile = tempnam(sys_get_temp_dir(), 'gdC');
file_put_contents($tempfile, $content);
return $tempfile;
}
die($content);
}
header('HTTP/1.1 200 OK');
header('Content-type: ' , $config->mimetype);
$tempfile = tempnam(sys_get_temp_dir(), 'gdD');
file_put_contents($tempfile, $content);
return $tempfile;
} else {
return $content;
}
}
protected function getFileId($driveservice, $filename) {
$file_id = $name = $mime = NULL;
$size = 0; $message = FALSE;
try{
$optParams = [
'corpora' => 'drive',
'pageSize' => 10,
'driveId' => $this->driveId, // this is from the URL of sharedDrive homeFolder
'fields' => 'files(id,name,size)',
'q' => "name = '" . $filename . "'",
'orderBy' => 'name',
'supportsAllDrives' => True,
'includeItemsFromAllDrives' => True,
];
$results = $driveservice->files->listFiles($optParams);
$files = $results->getFiles();
if (!empty($files)) {
$file_id = $files[0]->id;
$size = intval($files[0]->size);
$name = $files[0]->getName();
}
}
catch(\Exception $e) {
$message = $e->getMessage();
}
return [
'fileId' => $file_id,
'name' => $name,
'mime' => $mime,
'size' => $size,
'message' => $message,
];
}
public function fetchAccessToken() {
$client = $this->getGoogleClient();
$access_token = $client->fetchAccessTokenWithAssertion();
return $access_token;
}
protected function getGoogleClient() {
$client = new \Google\Client();
$client->setAuthConfig(HOME . '/storage/' . $this->keyfile);
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$client->setApplicationName('drivestuff'); // change app name whenever scope permissions change for Domain-wide Delegation
return $client;
}
/*
$response = $service->files->get($file_id, [ 'supportsAllDrives' => TRUE ]); // metadata
$url = $response->getWebContentLink(); // only for binary content - not video
*/
public function ScanDrive() {
set_time_limit(0);
$client = $this->getGoogleClient();
$service = new Drive($client);
$pageToken = null;
$this->Query('update googledrive set present=0 ')
->Where(1)
->Get();
$this->TableName('googledrive');
$block = 0;
do {
echo "\nBlock $block ";
$optParams = [
'corpora' => 'drive',
'pageSize' => 500,
'driveId' => $this->driveId, // this is from the URL of sharedDrive homeFolder
'fields' => 'nextPageToken, files(id,name,size,parents,mimeType)',
'q' => 'trashed=false',
'pageToken' => $pageToken,
'supportsAllDrives' => True,
'includeItemsFromAllDrives' => True,
];
$results = $service->files->listFiles($optParams);
$files = $results->getFiles();
if (!empty($files)) {
foreach($files as $index => $file) {
$file_id = $file->id;
$size = intval($file->size);
$name = $file->getName();
$parent = $file->parents[0];
$isFolder = $file->mimeType == 'application/vnd.google-apps.folder';
$this->saveItem($parent, $name, $file_id, $size, $isFolder);
// echo "$file_id ... $name ... $size .. $parent .. {$file->mimeType} \n";
if (!($index & 3)) echo '.';
flush();
}
}
// $pageToken = NULL;
$pageToken = $results->getNextPageToken();
$block++;
} while (!is_null($pageToken));
$this->Query('delete from googledrive')
->Where('present=0')
->Get();
}
private function saveItem($parent, $name, $file_id, $size, $isFolder) {
$this->InsertOrUpdate([
'fileId' => $file_id,
], [
'parentId' => $parent,
'folder' => $isFolder ? 1 : 0,
'filename' => $name,
'filesize' => $size,
'present' => 1,
]);
}
private function createFolder($currentfolder, $element) {
// create folder using api, get new folder id
$client = $this->getGoogleClient();
$service = new Drive($client);
$fileMetadata = new Drive\DriveFile([
'name' => $element,
'corpora' => 'drive',
'driveId' => $this->driveId,
'mimeType' => 'application/vnd.google-apps.folder',
'parents' => [ $currentfolder ],
]);
$file = $service->files->create($fileMetadata, [
'fields' => 'id',
'supportsAllDrives' => TRUE,
]);
$folder_id = $file->id;
// add entry to TableName
$this->TableName('googledrive')
->InsertOrUpdate([
'fileId' => $folder_id,
],[
'parentId' => $currentfolder,
'folder' => 1,
'filesize' => 0,
'filename' => $element,
]);
return folder_id;
}
public function Search($path, $create_if_missing = FALSE) {
$result = [];
$currentfolder = $this->driveId;
$path = trim($path, '/');
$elements = explode('/', $path);
$name = [];
foreach($elements as $index => $element) {
if (strpos($element, '*') === FALSE) {
$name[] = $element;
$data = $this->Query('select * from googledrive')
->Where('parentId=%s', $currentfolder)
->Where('filename=%s', $element)
->Get()
->First();
if (empty($data) || $data->empty()) {
if ($create_if_missing) {
$fileId = $this->createFolder($currentfolder, $element);
} else {
throw new \Exception('path not found: ' . join('/', $name));
}
} else {
$isFolder = intval($data->folder);
if (!$isFolder) {
$ext = pathinfo($data->filename, PATHINFO_EXTENSION);
$node = new \StdClass;
$node->name = join('/', $name);
$node->location = 'drive';
$node->ext = $ext;
$node->id = $data->fileId;
$result[] = $node;
return $result;
}
$currentfolder = $data->fileId;
}
} else {
// now get matching list of files from the $currentfolder
$mask = str_replace('*', '%', $element);
$rows = $this->Query('select * from googledrive')
->Where('parentId=%s', $currentfolder)
->Where('filename like %s', $mask)
->Get();
foreach($rows as $data) {
$isFolder = intval($data->folder);
if (!$isFolder) {
$ext = pathinfo($data->filename, PATHINFO_EXTENSION);
$node = new \StdClass;
$node->name = join('/', $name) . '/' . $data->filename;
$node->location = 'drive';
$node->ext = $ext;
$node->id = $data->fileId;
$result[] = $node;
}
}
return $result;
}
}
if (!count($result) && $create_if_missing) { // we are trying to locate a specific folder
$node = new \StdClass;
$node->name = join('/', $name);
$node->location = 'folder';
$node->id = $currentfolder;
$result[] = $node;
}
return $result;
}
public function upload($fileobject) {
$client = $this->getGoogleClient();
$service = new Drive($client);
$fileMetadata = new Drive\DriveFile([
'name' => $fileobject->name,
'corpora' => 'drive',
'driveId' => $this->driveId,
'parents' => [ $fileobject->folderId ],
]);
$content = file_get_contents($fileobject->source);
$file = $service->files->create($fileMetadata, [
'fields' => 'id',
'data' => $content,
'uploadType' => 'multipart',
'supportsAllDrives' => TRUE,
]);
unset($content);
$file_id = $file->id;
// add entry to database
$this->TableName('googledrive')
->InsertOrUpdate([
'fileId' => $file_id,
],[
'parentId' => $fileobject->folderId,
'folder' => 0,
'filesize' => filesize($fileobject->source),
'filename' => $fileobject->name,
]);
$fileobject->id = $file_id;
return $file_id;
}
public function delete($fileobject) {
// moves the shared file to trash
$client = $this->getGoogleClient();
$service = new Drive($client);
$fileMetadata = new Drive\DriveFile([
'corpora' => 'drive',
'driveId' => $this->driveId,
'supportsAllDrives' => TRUE,
]);
$fileMetadata->setTrashed(TRUE);
$service->files->update($fileobject->id, $fileMetadata, [
'fields' => 'id',
'supportsAllDrives' => TRUE,
]);
// delete entry from table
$this->Query('delete from googledrive')
->Where('fileId=%s', $fileobject->id)
->Get();
$this->DbErr();
}
private function _build_path($index, $fileitem) {
$output = $fileitem->filename;
$parentId = $fileitem->parentId;
while ($parentId != '') {
if (!isset($index[$parentId])) {
break;
}
$node = $index[$parentId];
$output = $node['name'] . '/' . $output;
$parentId = $node['parent'];
}
return $output;
}
public function locate($path) {
$index = [];
$rows = $this->Query("select parentId,fileId,filename from `googledrive`")
->Where('folder=%d', 1)
->Get();
foreach($rows as $row) {
$index[$row->fileId] = [
'name' => $row->filename,
'parent' => $row->parentId,
];
}
$result = [];
$rows = $this->Query("select parentId,fileId,filename from `googledrive`")
->Where('(filename like %s)', $path . '%')
->Get();
foreach($rows as $row) {
$ext = pathinfo($row->filename, PATHINFO_EXTENSION);
$node = new \StdClass;
$node->name = $row->filename;
$node->location = 'drive'; // definitely on google drive, we'll change this later if it cached
$node->ext = $ext;
$node->id = $row->fileId;
$node->path = $this->_build_path($index, $row);
$node->folderId = $row->parentId;
$result[] = $node;
}
return $result;
}
}