Add files pmgmail
This commit is contained in:
269
workflow/engine/classes/class.pmDrive.php
Executable file
269
workflow/engine/classes/class.pmDrive.php
Executable file
@@ -0,0 +1,269 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* class.pmDrive.php
|
||||
*
|
||||
* @package workflow.engine.class
|
||||
*
|
||||
*/
|
||||
class PMDrive extends PMGoogleApi
|
||||
{
|
||||
private $folderIdPMDrive = '';
|
||||
private $folderNamePMDrive;
|
||||
|
||||
/**
|
||||
* Validate if exist folder PMDrive
|
||||
*
|
||||
* @param $userUid id user
|
||||
*/
|
||||
private function validateFolderPMDrive($usrUid)
|
||||
{
|
||||
if ($this->folderIdPMDrive != '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$user = new Users();
|
||||
$dataUser = $user->load($usrUid);
|
||||
|
||||
if (!empty($dataUser['USR_EMAIL'])) {
|
||||
$this->setDriveUser($dataUser['USR_EMAIL']);
|
||||
}
|
||||
$this->folderIdPMDrive = empty($dataUser['USR_PMDRIVE_FOLDER_UID']) ? '' : $dataUser['USR_PMDRIVE_FOLDER_UID'];
|
||||
|
||||
$conf = $this->getConfigGmail();
|
||||
$this->folderNamePMDrive = empty($conf->aConfig['folderNamePMDrive']) ? 'PMDrive (' . SYS_SYS . ')' : $conf->aConfig['folderNamePMDrive'];
|
||||
|
||||
if ($this->folderIdPMDrive == '') {
|
||||
$folderid = $this->createFolder($this->folderNamePMDrive);
|
||||
|
||||
$this->folderIdPMDrive = $folderid->id;
|
||||
$dataUser['USR_PMDRIVE_FOLDER_UID'] = $folderid->id;
|
||||
$user->update($dataUser);
|
||||
}
|
||||
}
|
||||
|
||||
public function getFolderIdPMDrive($usrUid)
|
||||
{
|
||||
$this->validateFolderPMDrive($usrUid);
|
||||
return $this->folderIdPMDrive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set account user
|
||||
*
|
||||
* @param $user email user
|
||||
*/
|
||||
public function setFolderNamePMDrive($name)
|
||||
{
|
||||
$conf = $this->getConfigGmail();
|
||||
$conf->aConfig['folderNamePMDrive'] = $name;
|
||||
$conf->saveConfig('GOOGLE_API_SETTINGS', '', '', '');
|
||||
|
||||
$this->folderNamePMDrive = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set account user
|
||||
*
|
||||
* @param $user email user
|
||||
*/
|
||||
public function setDriveUser($user)
|
||||
{
|
||||
$this->setUser($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instance google service Drive
|
||||
*
|
||||
* @return Google_Service_Drive $service Drive API service instance.
|
||||
*/
|
||||
private function serviceDrive()
|
||||
{
|
||||
$client = $this->serviceClient();
|
||||
$service = new Google_Service_Drive($client);
|
||||
return $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a list of File resources.
|
||||
*
|
||||
* @param string $fileId uid file
|
||||
* @return Array List of Google_Service_Drive_DriveFile resources.
|
||||
*/
|
||||
public function listFolder($fileId)
|
||||
{
|
||||
$this->setScope('https://www.googleapis.com/auth/drive');
|
||||
$this->setScope('https://www.googleapis.com/auth/drive.file');
|
||||
$this->setScope('https://www.googleapis.com/auth/drive.readonly');
|
||||
$this->setScope('https://www.googleapis.com/auth/drive.metadata.readonly');
|
||||
$this->setScope('https://www.googleapis.com/auth/drive.appdata');
|
||||
$this->setScope('https://www.googleapis.com/auth/drive.metadata');
|
||||
$service = $this->serviceDrive();
|
||||
|
||||
try {
|
||||
$parameters['q'] = "'" . $fileId . "' in parents and trashed = false";
|
||||
$parents = $service->files->listFiles($parameters);
|
||||
|
||||
$rows = array();
|
||||
foreach ($parents->getItems() as $parent) {
|
||||
//echo 'File Id: ' . $parent->getId() . '<br>';
|
||||
$rows = $parent;
|
||||
}
|
||||
return $rows;
|
||||
} catch (Exception $e) {
|
||||
return G::LoadTranslation("ID_MSG_AJAX_FAILURE") . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a list of File resources.
|
||||
*
|
||||
* @param string $name Title of the file to insert, including the extension.
|
||||
* @param string $parentId Parent folder's ID.
|
||||
* @return Google_Service_Drive_DriveFile The file that was inserted. NULL is returned if an API error occurred.
|
||||
*/
|
||||
public function createFolder($name, $parentId = null)
|
||||
{
|
||||
$this->setScope('https://www.googleapis.com/auth/drive.file');
|
||||
|
||||
$service = $this->serviceDrive();
|
||||
|
||||
$file = new Google_Service_Drive_DriveFile();
|
||||
$file->setMimeType("application/vnd.google-apps.folder");
|
||||
$file->setTitle($name);
|
||||
|
||||
if ($parentId != null) {
|
||||
$parent = new Google_Service_Drive_ParentReference();
|
||||
$parent->setId($parentId);
|
||||
$file->setParents(array($parent));
|
||||
}
|
||||
|
||||
try {
|
||||
$createdFolder = $service->files->insert($file);
|
||||
return $createdFolder;
|
||||
} catch (Exception $e) {
|
||||
return "An error occurred: " . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* upload new file
|
||||
*
|
||||
* @param string $mime MIME type of the file to insert.
|
||||
* @param string $src location of the file to insert.
|
||||
* @param string $name Title of the file to insert, including the extension.
|
||||
* @return Google_Service_Drive_DriveFile The file that was inserted. NULL is returned if an API error occurred.
|
||||
*/
|
||||
public function uploadFile($mime, $src, $name, $parentId = null)
|
||||
{
|
||||
//$this->validateFolderPMDrive();
|
||||
$this->setScope('https://www.googleapis.com/auth/drive.file');
|
||||
|
||||
$service = $this->serviceDrive();
|
||||
|
||||
$file = new Google_Service_Drive_DriveFile();
|
||||
$file->setMimeType("*/*");
|
||||
$file->setTitle($name);
|
||||
|
||||
// Set the parent folder.
|
||||
if ($parentId != null) {
|
||||
$parent = new Google_Service_Drive_ParentReference();
|
||||
$parent->setId($parentId);
|
||||
$file->setParents(array($parent));
|
||||
}
|
||||
|
||||
$data = file_get_contents($src);
|
||||
|
||||
try {
|
||||
$createdFile = $service->files->insert(
|
||||
$file,
|
||||
array(
|
||||
'data' => $data,
|
||||
'mimeType' => $mime,
|
||||
'uploadType' => 'media'
|
||||
)
|
||||
);
|
||||
|
||||
return $createdFile;
|
||||
} catch (Exception $e) {
|
||||
return "An error occurred: " . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Download a file's content.
|
||||
*
|
||||
* @param string $fileId id file.
|
||||
* @return String The file's content if successful, null otherwise
|
||||
*/
|
||||
public function downloadFile($fileId)
|
||||
{
|
||||
//$this->validateFolderPMDrive();
|
||||
$this->setScope('https://www.googleapis.com/auth/drive');
|
||||
$this->setScope('https://www.googleapis.com/auth/drive.appdata');
|
||||
$this->setScope('https://www.googleapis.com/auth/drive.apps.readonly');
|
||||
$this->setScope('https://www.googleapis.com/auth/drive.file');
|
||||
$this->setScope('https://www.googleapis.com/auth/drive.metadata');
|
||||
$this->setScope('https://www.googleapis.com/auth/drive.metadata.readonly');
|
||||
$this->setScope('https://www.googleapis.com/auth/drive.readonly');
|
||||
$service = $this->serviceDrive();
|
||||
|
||||
try {
|
||||
$file = $service->files->get($fileId);
|
||||
|
||||
$downloadUrl = $file->getDownloadUrl();
|
||||
if ($downloadUrl) {
|
||||
$request = new Google_Http_Request($downloadUrl, 'GET', null, null);
|
||||
$httpRequest = $service->getClient()->getAuth()->authenticatedRequest($request);
|
||||
if ($httpRequest->getResponseHttpCode() == 200) {
|
||||
return $httpRequest->getResponseBody();
|
||||
} else {
|
||||
// An error occurred.
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
// The file doesn't have any content stored on Drive.
|
||||
return null;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
print "An error occurred: " . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new permission.
|
||||
*
|
||||
* @param String $fileId ID of the file to insert permission for.
|
||||
* @param String $value User or group e-mail address, domain name or NULL for "default" type.
|
||||
* @param String $type The value "user", "group", "domain" or "default".
|
||||
* @param String $role The value "owner", "writer" or "reader".
|
||||
* @return Google_Servie_Drive_Permission The inserted permission. NULL is returned if an API error occurred.
|
||||
*/
|
||||
public function setPermission($fileId, $value, $type = 'user', $role = 'reader', $sendNotification = false)
|
||||
{
|
||||
$this->setScope('https://www.googleapis.com/auth/drive');
|
||||
$this->setScope('https://www.googleapis.com/auth/drive.file');
|
||||
|
||||
$service = $this->serviceDrive();
|
||||
|
||||
$newPermission = new Google_Service_Drive_Permission();
|
||||
$newPermission->setValue($value);
|
||||
$newPermission->setType($type);
|
||||
$newPermission->setRole($role);
|
||||
|
||||
try {
|
||||
$permission = $service->permissions->insert(
|
||||
$fileId,
|
||||
$newPermission,
|
||||
array(
|
||||
'sendNotificationEmails' => $sendNotification
|
||||
)
|
||||
);
|
||||
|
||||
return $permission;
|
||||
} catch (Exception $e) {
|
||||
error_log('permission error: ' . $e->getMessage());
|
||||
return "An error occurred: " . $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
213
workflow/engine/classes/class.pmGoogleApi.php
Executable file
213
workflow/engine/classes/class.pmGoogleApi.php
Executable file
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* class.pmGoogleApi.php
|
||||
*
|
||||
*/
|
||||
|
||||
require_once PATH_TRUNK . 'vendor' . PATH_SEP . 'google' . PATH_SEP . 'apiclient' . PATH_SEP . 'src' . PATH_SEP . 'Google' . PATH_SEP . 'autoload.php';
|
||||
|
||||
class PMGoogleApi
|
||||
{
|
||||
private $scope = array();
|
||||
private $serviceAccountEmail;
|
||||
private $serviceAccountP12;
|
||||
private $statusService;
|
||||
private $domain;
|
||||
private $user;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$licensedFeatures = &PMLicensedFeatures::getSingleton();
|
||||
if (!$licensedFeatures->verifyfeature('7qhYmF1eDJWcEdwcUZpT0k4S0xTRStvdz09')) {
|
||||
G::SendTemporalMessage('ID_USER_HAVENT_RIGHTS_PAGE', 'error', 'labels');
|
||||
G::header('location: ../login/login');
|
||||
die;
|
||||
}
|
||||
$this->loadSettings();
|
||||
}
|
||||
|
||||
public function setScope($scope)
|
||||
{
|
||||
$this->scope[] = $scope;
|
||||
}
|
||||
|
||||
public function getScope()
|
||||
{
|
||||
return $this->scope;
|
||||
}
|
||||
|
||||
public function setUser($user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function getUser()
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
public function setStatusService($status)
|
||||
{
|
||||
$conf = $this->getConfigGmail();
|
||||
|
||||
$conf->aConfig['statusService'] = $status;
|
||||
$conf->saveConfig('GOOGLE_API_SETTINGS', '', '', '');
|
||||
|
||||
$this->statusService = $status;
|
||||
}
|
||||
|
||||
public function getStatusService()
|
||||
{
|
||||
return $this->statusService;
|
||||
}
|
||||
|
||||
public function getConfigGmail()
|
||||
{
|
||||
$conf = new Configurations();
|
||||
$conf->loadConfig($gmail, 'GOOGLE_API_SETTINGS', '');
|
||||
return $conf;
|
||||
}
|
||||
|
||||
public function setServiceAccountEmail($serviceAccountEmail)
|
||||
{
|
||||
$conf = $this->getConfigGmail();
|
||||
|
||||
$conf->aConfig['serviceAccountEmail'] = $serviceAccountEmail;
|
||||
$conf->saveConfig('GOOGLE_API_SETTINGS', '', '', '');
|
||||
|
||||
$this->serviceAccountEmail = $serviceAccountEmail;
|
||||
}
|
||||
|
||||
public function getServiceAccountEmail()
|
||||
{
|
||||
return $this->serviceAccountEmail;
|
||||
}
|
||||
|
||||
public function setServiceAccountP12($serviceAccountP12)
|
||||
{
|
||||
$conf = $this->getConfigGmail();
|
||||
|
||||
$conf->aConfig['serviceAccountP12'] = $serviceAccountP12;
|
||||
$conf->saveConfig('GOOGLE_API_SETTINGS', '', '', '');
|
||||
|
||||
$this->serviceAccountP12 = $serviceAccountP12;
|
||||
}
|
||||
|
||||
public function getserviceAccountP12()
|
||||
{
|
||||
return $this->serviceAccountP12;
|
||||
}
|
||||
|
||||
public function setDomain($domain)
|
||||
{
|
||||
$conf = $this->getConfigGmail();
|
||||
|
||||
$conf->aConfig['domain'] = $domain;
|
||||
$conf->saveConfig('GOOGLE_API_SETTINGS', '', '', '');
|
||||
|
||||
$this->domain = $domain;
|
||||
}
|
||||
|
||||
public function getDomain()
|
||||
{
|
||||
return $this->domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* load configuration gmail service account
|
||||
*
|
||||
*/
|
||||
public function loadSettings()
|
||||
{
|
||||
$conf = $this->getConfigGmail();
|
||||
|
||||
$serviceAccountP12 = empty($conf->aConfig['serviceAccountP12']) ? '' : $conf->aConfig['serviceAccountP12'];
|
||||
$serviceAccountEmail = empty($conf->aConfig['serviceAccountEmail']) ? '' : $conf->aConfig['serviceAccountEmail'];
|
||||
$statusService = empty($conf->aConfig['statusService']) ? '' : $conf->aConfig['statusService'];
|
||||
|
||||
$this->scope = array();
|
||||
$this->setServiceAccountEmail($serviceAccountEmail);
|
||||
$this->setServiceAccountP12($serviceAccountP12);
|
||||
$this->setStatusService($statusService);
|
||||
}
|
||||
|
||||
/**
|
||||
* New service client - Authentication google Api
|
||||
*
|
||||
* @return Google_Service_Client $service API service instance.
|
||||
*/
|
||||
public function serviceClient()
|
||||
{
|
||||
$key = file_get_contents(PATH_DATA_SITE . $this->serviceAccountP12);
|
||||
|
||||
$assertionCredentials = new Google_Auth_AssertionCredentials(
|
||||
$this->serviceAccountEmail,
|
||||
$this->scope,
|
||||
$key
|
||||
);
|
||||
$assertionCredentials->sub = $this->user;
|
||||
|
||||
$client = new Google_Client();
|
||||
$client->setApplicationName("PMDrive");
|
||||
$client->setAssertionCredentials($assertionCredentials);
|
||||
|
||||
return $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* New service client - Authentication google Api
|
||||
*
|
||||
* @return Google_Service_Client $service API service instance.
|
||||
*/
|
||||
public function testService($serviceAccountEmail, $pathServiceAccountP12)
|
||||
{
|
||||
$key = file_get_contents($pathServiceAccountP12);
|
||||
|
||||
$assertionCredentials = new Google_Auth_AssertionCredentials(
|
||||
$serviceAccountEmail,
|
||||
array(
|
||||
'https://www.googleapis.com/auth/drive',
|
||||
'https://www.googleapis.com/auth/drive.file',
|
||||
'https://www.googleapis.com/auth/drive.readonly',
|
||||
'https://www.googleapis.com/auth/drive.metadata.readonly',
|
||||
'https://www.googleapis.com/auth/drive.appdata',
|
||||
'https://www.googleapis.com/auth/drive.metadata',
|
||||
'https://www.googleapis.com/auth/drive.photos.readonly'
|
||||
),
|
||||
$key
|
||||
);
|
||||
$assertionCredentials->sub = $this->user;
|
||||
|
||||
$client = new Google_Client();
|
||||
$client->setApplicationName("PMDrive");
|
||||
$client->setAssertionCredentials($assertionCredentials);
|
||||
|
||||
$service = new Google_Service_Drive($client);
|
||||
|
||||
$result = new StdClass();
|
||||
$result->success = true;
|
||||
|
||||
$result->currentUserName = G::LoadTranslation('ID_SERVER_COMMUNICATION_ERROR');
|
||||
$result->rootFolderId = G::LoadTranslation('ID_SERVER_COMMUNICATION_ERROR');
|
||||
$result->quotaType = G::LoadTranslation('ID_SERVER_COMMUNICATION_ERROR');
|
||||
$result->quotaBytesTotal = G::LoadTranslation('ID_SERVER_COMMUNICATION_ERROR');
|
||||
$result->quotaBytesUsed = G::LoadTranslation('ID_SERVER_COMMUNICATION_ERROR');
|
||||
|
||||
try {
|
||||
$about = $service->about->get();
|
||||
|
||||
$result->currentUserName = $about->getName();
|
||||
$result->rootFolderId = $about->getRootFolderId();
|
||||
$result->quotaType = $about->getQuotaType();
|
||||
$result->quotaBytesTotal = $about->getQuotaBytesTotal();
|
||||
$result->quotaBytesUsed = $about->getQuotaBytesUsed();
|
||||
$result->responseGmailTest = G::LoadTranslation('ID_SUCCESSFUL_CONNECTION');
|
||||
} catch (Exception $e) {
|
||||
$result->success = false;
|
||||
$result->responseGmailTest = G::LoadTranslation('ID_SERVER_COMMUNICATION_ERROR');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user