コード例 #1
3
ファイル: googledrive.php プロジェクト: xXLXx/ddc
 /**
  * Creates a file to drive
  * @param 
  */
 public static function createFile($title, $ownerEmail, $sourceDoc = null, $service = null)
 {
     if (!$service) {
         $service = static::getService();
     }
     $title = $title ?: time() . '.doc';
     $mime = 'application/vnd.oasis.opendocument.text';
     $data = '';
     if ($sourceDoc && file_exists($sourceDoc)) {
         $finfo = finfo_open(FILEINFO_MIME_TYPE);
         $mime = finfo_file($finfo, $sourceDoc);
         $data = file_get_contents($sourceDoc);
     }
     $file = new Google_Service_Drive_DriveFile();
     $file->setMimeType($mime);
     $file->setTitle($title);
     // try {
     $createdFile = $service->files->insert($file, array('data' => $data, 'mimeType' => $mime, 'convert' => true, 'uploadType' => 'media'));
     $fileId = $createdFile->getId();
     $ownerPermission = new Google_Service_Drive_Permission();
     $ownerPermission->setValue($ownerEmail);
     $ownerPermission->setType('user');
     $ownerPermission->setRole('writer');
     try {
         $service->permissions->insert($fileId, $ownerPermission, ['emailMessage' => 'You add a file to ' . static::$applicationName . ': ' . $title]);
         $publicPermission = new Google_Service_Drive_Permission();
         $publicPermission->setValue(null);
         $publicPermission->setType('anyone');
         $publicPermission->setRole('reader');
         try {
             $service->permissions->insert($fileId, $publicPermission);
             return $createdFile->alternateLink;
         } catch (Exception $e) {
             throw new HttpServerErrorException();
         }
     } catch (Exception $e) {
         throw new HttpServerErrorException();
     }
     return $createdFile;
     // } catch (Exception $e) {
     //     throw new HttpServerErrorException;
     // }
     return '';
 }
コード例 #2
1
 /**
  * Creates the Google Drive CDN folder on the google drive
  * @since 1.0
  *
  * @param null $folder
  *
  */
 public function create_folder($folder = null)
 {
     if (is_null($folder)) {
         $folder = home_url();
         $folder = str_replace('http://', '', $folder);
         $folder = str_replace('https://', '', $folder);
         $folder = sanitize_key($folder);
     }
     $service = new Google_Service_Drive($this->_google_drive_cdn->get_google_client());
     $file = new Google_Service_Drive_DriveFile();
     $file->setTitle($folder);
     $file->setDescription(home_url() . ' to Google Drive CDN Folder');
     $file->setMimeType('application/vnd.google-apps.folder');
     try {
         $createdFile = $service->files->insert($file, array('mimeType' => 'application/vnd.google-apps.folder'));
     } catch (Exception $e) {
         $this->_google_drive_cdn->set_error($e->getMessage() . '(wpbgdc: create_folder 1)', false);
         return;
     }
     WPB_Google_Drive_Cdn_Settings::set_setting('folder_id', $createdFile->getId(), 'wpbgdc_folders');
     // set permissions
     $permission = new Google_Service_Drive_Permission();
     $permission->setValue('');
     $permission->setType('anyone');
     $permission->setRole('reader');
     try {
         $service->permissions->insert($createdFile->getId(), $permission);
     } catch (Exception $e) {
         $this->_google_drive_cdn->set_error($e->getMessage() . '(wpbgdc: create_folder 2)', false);
         return;
     }
     try {
         $created_file_info = $service->files->get($createdFile->getId());
     } catch (Exception $e) {
         $this->_google_drive_cdn->set_error($e->getMessage() . '(wpbgdc: create_folder 3)', false);
         return $createdFile->getId();
     }
     WPB_Google_Drive_Cdn_Settings::set_setting('folder_link', $created_file_info->webViewLink, 'wpbgdc_folders');
     WPB_Google_Drive_Cdn_Settings::set_setting('folder_name', $folder, 'wpbgdc_folders');
     return $createdFile->getId();
 }
コード例 #3
0
 public function setPermissions($fileId, $value, $role = 'writer', $type = 'user')
 {
     $perm = new Google_Service_Drive_Permission();
     $perm->setValue($value);
     $perm->setType($type);
     $perm->setRole($role);
     $this->_service->permissions->insert($fileId, $perm);
 }
コード例 #4
0
ファイル: gdrive_file.php プロジェクト: alin96/MergeBox
 function set_permissions($old_role, $old_type, $old_value, $service, $folderid, $file)
 {
     $permissions = $service->permissions->listPermissions($folderid);
     $newPermission = new Google_Service_Drive_Permission();
     $newPermission->setValue("default");
     $newPermission->setType("anyone");
     $newPermission->setRole("reader");
     try {
         $service->permissions->insert($folderid, $newPermission);
     } catch (Exception $e) {
         print "An error occurred: " . $e->getMessage();
     }
     $link = array('type' => 'file');
     $link['service'] = '3';
     $link['fileid'] = $folderid;
     $link['reset_permission'] = array('role' => $old_role, 'type' => $old_type, 'value' => $old_value);
     $link['permissionid'] = $permissions->getItems();
     $link['link'] = $file->getWebContentLink();
     print json_encode($link);
 }
 function w3tc_cdn_google_drive_auth_set()
 {
     // thanks wp core for wp_magic_quotes hell
     $client_id = stripslashes($_POST['client_id']);
     $access_token = stripslashes($_POST['access_token']);
     $refresh_token = stripslashes($_POST['refresh_token']);
     $client = new \Google_Client();
     $client->setClientId($client_id);
     $client->setAccessToken($access_token);
     //
     // get folder details
     //
     $service = new \Google_Service_Drive($client);
     if (empty($_POST['folder'])) {
         $file = new \Google_Service_Drive_DriveFile(array('title' => $_POST['folder_new'], 'mimeType' => 'application/vnd.google-apps.folder'));
         $created_file = $service->files->insert($file);
         $used_folder_id = $created_file->id;
     } else {
         $used_folder_id = $_POST['folder'];
     }
     $permission = new \Google_Service_Drive_Permission();
     $permission->setValue('');
     $permission->setType('anyone');
     $permission->setRole('reader');
     $service->permissions->insert($used_folder_id, $permission);
     $used_folder = $service->files->get($used_folder_id);
     //
     // save new configuration
     //
     delete_transient('w3tc_cdn_google_drive_folder_ids');
     $this->_config->set('cdn.google_drive.client_id', $client_id);
     $this->_config->set('cdn.google_drive.refresh_token', $refresh_token);
     $this->_config->set('cdn.google_drive.folder.id', $used_folder->id);
     $this->_config->set('cdn.google_drive.folder.title', $used_folder->title);
     $this->_config->set('cdn.google_drive.folder.url', $used_folder->webViewLink);
     $this->_config->save();
     $cs = Dispatcher::config_state();
     $cs->set('cdn.google_drive.access_token', $access_token);
     $cs->save();
     wp_redirect('admin.php?page=w3tc_cdn', false);
 }
function shareDocument($service, $emailadrress, $role_array, $CONFIG_SRCH_UPD_data)
{
    $value = $emailadrress;
    $type = 'user';
    $role = $role_array;
    $email = $emailadrress;
    $newPermission = new Google_Service_Drive_Permission();
    $newPermission->setValue($value);
    $newPermission->setType($type);
    $newPermission->setRole($role);
    $newPermission->setEmailAddress($email);
    try {
        $service->permissions->insert($CONFIG_SRCH_UPD_data, $newPermission);
    } catch (Exception $e) {
    }
}
コード例 #7
0
/**
 * Insert a new permission.
 *
 * @param Google_Service_Drive $service Drive API service instance.
 * @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.
 */
function insertPermission($service, $fileId, $value, $type, $role)
{
    $newPermission = new Google_Service_Drive_Permission();
    $newPermission->setValue($value);
    $newPermission->setType($type);
    $newPermission->setRole($role);
    try {
        return $service->permissions->insert($fileId, $newPermission);
    } catch (Exception $e) {
        catchGoogleExceptions($e);
    }
    return null;
}
コード例 #8
0
 function insertPermission($fileId, $value, $type, $role)
 {
     $wpdm_google_drive = maybe_unserialize(get_option('__wpdm_google_drive', array()));
     $client = new Google_Client();
     $client->setClientId($wpdm_google_drive['client_id']);
     $client->setClientSecret($wpdm_google_drive['client_secret']);
     $client->addScope(Google_Service_Drive::DRIVE);
     $client->addScope(Google_Service_Drive::DRIVE_FILE);
     $client->addScope(Google_Service_Drive::DRIVE_READONLY);
     $client->addScope(Google_Service_Drive::DRIVE_APPDATA);
     $client->addScope(Google_Service_Drive::DRIVE_APPS_READONLY);
     $client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
     $client->setRedirectUri(admin_url('/?page=wpdm-google-drive'));
     $access_token = isset($_SESSION['wpdmgd_access_token']) ? $_SESSION['wpdmgd_access_token'] : '';
     $client->setAccessToken($access_token);
     $service = new Google_Service_Drive($client);
     $newPermission = new Google_Service_Drive_Permission();
     $newPermission->setValue($value);
     $newPermission->setId($fileId);
     $newPermission->setType($type);
     $newPermission->setRole($role);
     try {
         $ret = $service->permissions->insert($fileId, $newPermission);
     } catch (Exception $e) {
         $ret = "An error occurred: " . $e->getMessage();
     }
     return $ret;
 }
コード例 #9
0
function URSRC_calendar_create($loggin, $fileId, $loginid_name, $URSC_uld_id, $finaldate, $calenderid, $status, $form, $filesarray, $URSRC_firstname, $URSRC_lastname, $folderid)
{
    global $con, $ClientId, $ClientSecret, $RedirectUri, $DriveScopes, $CalenderScopes, $Refresh_Token;
    $drive = new Google_Client();
    $Client = get_servicedata();
    $ClientId = "651424197810-72ki9kr1k58v3qec0034q1bfj2og6ss8.apps.googleusercontent.com";
    //$Client[0];
    $ClientSecret = "NRY1PYuUK-NeJAZVJxUvXBeb";
    //$Client[1];
    $RedirectUri = "https://developers.google.com/oauthplayground";
    //$Client[2];
    $DriveScopes = "https://www.googleapis.com/auth/drive";
    //$Client[3];
    $CalenderScopes = "https://www.googleapis.com/auth/calendar";
    //$Client[4];
    $Refresh_Token = "1/YkIO518mEEVO9z5bdieivdpf9XVBnZN4PWZPLStyGzk";
    //$Client[5];
    $drive->setClientId($ClientId);
    $drive->setClientSecret($ClientSecret);
    $drive->setRedirectUri($RedirectUri);
    $drive->setScopes(array($DriveScopes, $CalenderScopes));
    $drive->setAccessType('online');
    $authUrl = $drive->createAuthUrl();
    $refresh_token = $Refresh_Token;
    $drive->refreshToken($refresh_token);
    $service = new Google_Service_Drive($drive);
    //    print_r($service);
    if ($form == 'TERMINATE') {
        $file_arraycount = 1;
        try {
            $permissions = $service->permissions->listPermissions($fileId);
            $return_value = $permissions->getItems();
        } catch (Exception $e) {
            $ss_flag = 0;
        }
        foreach ($return_value as $key => $value) {
            if ($value->emailAddress == $loggin) {
                $permission_id = $value->id;
            }
        }
        if ($permission_id != '') {
            try {
                $service->permissions->delete($fileId, $permission_id);
                $ss_flag = 1;
            } catch (Exception $e) {
                $ss_flag = 0;
            }
        } else {
            $ss_flag = 1;
        }
    } else {
        $value = $loggin;
        $type = 'user';
        $role = 'reader';
        $email = $loggin;
        $newPermission = new Google_Service_Drive_Permission();
        $newPermission->setValue($value);
        $newPermission->setType($type);
        $newPermission->setRole($role);
        $newPermission->setEmailAddress($email);
        try {
            $service->permissions->insert($fileId, $newPermission);
            $ss_flag = 1;
        } catch (Exception $e) {
            $ss_flag = 0;
        }
        if ($ss_flag == 1) {
            if ($filesarray != '') {
                $file_array = array();
                $allfilearray = explode(",", $filesarray);
                foreach ($allfilearray as $value) {
                    $uploadfilename = $value;
                    $drivefilename = $URSRC_firstname . ' ' . $URSRC_lastname . '-' . $uploadfilename;
                    $extension = explode(".", $uploadfilename);
                    if ($extension[1] == 'pdf') {
                        $mimeType = 'application/pdf';
                    }
                    if ($extension[1] == 'jpg') {
                        $mimeType = 'image/jpeg';
                    }
                    if ($extension[1] == 'png') {
                        $mimeType = 'image/png';
                    }
                    $file_id_value = insertFile($service, $drivefilename, 'PersonalDetails', $folderid, $mimeType, $uploadfilename);
                    if ($file_id_value != '') {
                        array_push($file_array, $file_id_value);
                    }
                }
                $file_arraycount = count($file_array);
            } else {
                $file_arraycount = 1;
            }
        }
    }
    if ($ss_flag == 1 && $file_arraycount > 0) {
        $cal = new Google_Service_Calendar($drive);
        $event = new Google_Service_Calendar_Event();
        $event->setsummary($loginid_name . '  ' . $status);
        $event->setDescription($URSC_uld_id);
        $start = new Google_Service_Calendar_EventDateTime();
        $start->setDate($finaldate);
        //setDate('2014-11-18');
        $event->setStart($start);
        $event->setEnd($start);
        try {
            $createdEvent = $cal->events->insert($calenderid, $event);
            $cal_flag = 1;
        } catch (Exception $e) {
            $cal_flag = 0;
        }
    }
    $flag_array = [$ss_flag, $cal_flag, $file_id_value, $file_array];
    return $flag_array;
}
コード例 #10
0
ファイル: lib.php プロジェクト: bobpuffer/morsle-google
/**
* 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.
*/
function assign_permissions($morsle, $fileId, $value = array(), $role = array(), $type = 'user')
{
    $service = new Google_Service_Drive($morsle->client);
    foreach ($value as $id => $share) {
        $newPermission = new Google_Service_Drive_Permission();
        $newPermission->setValue($share);
        $newPermission->setType($type);
        $newPermission->setRole($role[$id]);
        try {
            $service->permissions->insert($fileId, $newPermission, array('sendNotificationEmails' => 0));
        } catch (Exception $e) {
            print "An error occurred: " . $e->getMessage();
        }
    }
    return NULL;
}
コード例 #11
0
ファイル: mexp-service.php プロジェクト: hwdsbcommons/gdrive
 /**
  * Set Google Doc to share with anyone.
  *
  * @link https://developers.google.com/drive/web/manage-sharing
  * @link https://developers.google.com/drive/v2/reference/permissions
  * @link http://stackoverflow.com/questions/11155441/set-file-sharing-level-to-anyone-with-the-link-through-google-drive-api
  *
  * @param Google_Service_Drive $service Drive API service instance.
  * @param string               $file_id File ID for Google Drive document.
  */
 protected function share_doc_with_anyone(Google_Service_Drive $service, $file_id)
 {
     $new_perm = new Google_Service_Drive_Permission();
     $new_perm->setRole('reader');
     $new_perm->setType('anyone');
     // unsure about this...
     $new_perm->setWithLink(true);
     // either id or value can be set; not both
     //$new_perm->setId( 'anyoneWithLink' );
     $new_perm->setValue('');
     // insert the permission
     $service->permissions->insert($file_id, $new_perm);
 }
コード例 #12
0
 public  function SetDocOwnerGivenId($service,$fileid,$URSRC_loginid){
     $value=$URSRC_loginid;
     $type='user';
     $role='owner';
     $email=$URSRC_loginid;
     $newPermission = new Google_Service_Drive_Permission();
     $newPermission->setValue($value);
     $newPermission->setType($type);
     $newPermission->setRole($role);
     $newPermission->setEmailAddress($email);
     // try {
     $service->permissions->insert($fileid, $newPermission);
     // } catch (Exception $e) {
     //     echo $e->getMessage();
     // }
 }
コード例 #13
0
 /**
  * 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));
     } catch (Exception $e) {
         error_log(G::LoadTranslation("ID_MSG_AJAX_FAILURE") . $e->getMessage());
     }
     return $permission;
 }
コード例 #14
0
ファイル: main.php プロジェクト: eliagbayani/g_api_php_c
function update_permission($client, $tableID)
{
    $permissionsService = new Google_Service_Drive($client);
    $permissionsService = $permissionsService->permissions;
    $permission = new Google_Service_Drive_Permission();
    // $permission = new Google_Service_Drive_Permissions_Resource();
    $permission->setRole('writer');
    //Valid values are 'reader', 'commenter', 'writer', and 'owner'
    $permission->setType('anyone');
    $result = $permissionsService->create($tableID, $permission);
    return $result;
    /*
    Google_Service_Drive_Permission Object
    (   [internal_gapi_mappings:protected] => Array()
        [allowFileDiscovery] => 
        [displayName] => 
        [domain] => 
        [emailAddress] => 
        [id] => anyoneWithLink
        [kind] => drive#permission
        [photoLink] => 
        [role] => writer
        [type] => anyone
        [modelData:protected] => Array()
        [processed:protected] => Array()
    )
    */
}
コード例 #15
0
ファイル: test5.php プロジェクト: eliagbayani/g_api_php_c
like this: 
$permission = new Google_Permission(); 
$permission->setRole('reader'); 
$permission->setType('anyone'); 
Then add this permission to your fusion table: 
$permissionsService->insert($tableId, $permission); 

That's it, you're done. From now, your table is readable(but for everyone, not just you) and public. 
Now you can put it on a Google Maps "Fusion Layer". – sanya Dec 17 '12 at 19:09
*/
// /* //working OK =========================== Updating permissions to Google files
$permissionsService = new Google_Service_Drive($client);
$permissionsService = $permissionsService->permissions;
$permission = new Google_Service_Drive_Permission();
// $permission = new Google_Service_Drive_Permissions_Resource();
$permission->setRole('writer');
//Valid values are 'reader', 'commenter', 'writer', and 'owner'
$permission->setType('anyone');
$result = $permissionsService->create($my_table, $permission);
echo "<pre>";
print_r($result);
echo "</pre>";
exit;
// =========================== */
/* another option for inserting permissions found here: https://developers.google.com/drive/v2/reference/permissions/insert */
/* //working OK  =========================== Showing File's metadata
$service = new Google_Service_Drive($client);
printFile($service, $my_table);
function printFile($service, $fileId) {
  try {
    $file = $service->files->get($fileId);
コード例 #16
0
 function set_folderpermissions($readwrite, $members, $folderid)
 {
     // what does google say we have for permissions on this folder
     $return = $this->service->permissions->listPermissions($folderid);
     $gmembers = array();
     foreach ($return as $key => $value) {
         if ($value === "") {
             continue;
         }
         $gmembers[$value->emailAddress] = strtolower($value->role);
         $permissonId[$value->emailAddress] = $value->id;
     }
     $deleted = array_diff_assoc($gmembers, $members);
     unset($deleted[$this->useremail]);
     $added = array_diff_assoc($members, $gmembers);
     /************************************************
         To actually make the batch call we need to 
         enable batching on the client - this will apply 
         globally until we set it to false. This causes
         call to the service methods to return the query
         rather than immediately executing.
        ************************************************/
     $this->client->setUseBatch(true);
     $batch = new Google_Http_Batch($this->client);
     // add new members first in case we need an owner
     foreach ($added as $emailAddress => $role) {
         $permission = new Google_Service_Drive_Permission();
         $permission->setValue($emailAddress);
         $permission->setType('user');
         $permission->setRole($role);
         $return = $this->service->permissions->insert($folderid, $permission, array('sendNotificationEmails' => 0));
         $batch->add($return, $emailAddress);
     }
     if (count($deleted) + count($added) == 0) {
         return true;
     }
     // delete
     foreach ($deleted as $emailAddress => $role) {
         $return = $this->service->permissions->delete($folderid, $permissonId[$emailAddress]);
         $batch->add($return, $emailAddress);
     }
     /************************************************
         Executing the batch will send all requests off
         at once.
        ************************************************/
     $results = $batch->execute();
     $this->client->setUseBatch(false);
 }
 $drive->setScopes(array($DriveScopes, $CalenderScopes));
 $drive->setAccessType('online');
 $authUrl = $drive->createAuthUrl();
 $refresh_token = $Refresh_Token;
 $drive->refreshToken($refresh_token);
 $service = new Google_Service_Drive($drive);
 $fileId = $ss_fileid;
 $codeopti_fileId = $ss_codefileid;
 $value = $loginid;
 $type = 'user';
 $role = 'reader';
 $email = $loginid;
 $newPermission = new Google_Service_Drive_Permission();
 $newPermission->setValue($value);
 $newPermission->setType($type);
 $newPermission->setRole($role);
 $newPermission->setEmailAddress($email);
 //                echo 'ech 4';
 try {
     $service->permissions->insert($fileId, $newPermission);
     $service->permissions->insert($codeopti_fileId, $newPermission);
     $ss_flag = 1;
 } catch (Exception $e) {
     $ss_flag = 0;
     $con->rollback();
 }
 if ($ss_flag == 1) {
     $upload_flag = 0;
     //File upload function
     $file_array = array();
     //                    for($iv=0;i<count($user_filelist);$iv++)
コード例 #18
0
public  function URSRC_AddEditor($service,$fileid,$URSRC_loginid){
$value=$URSRC_loginid;
$type='user';
$role='writer';
$email=$URSRC_loginid;
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setValue($value);
$newPermission->setType($type);
$newPermission->setRole($role);
$newPermission->setEmailAddress($email);
try {
    $service->permissions->insert($fileid, $newPermission);
} catch (Exception $e) {
}
}
コード例 #19
0
 private function parent_id_resolve_step($root_id, $folder)
 {
     // decode top folder
     $ids_string = get_transient('w3tc_cdn_google_drive_folder_ids');
     $ids = @unserialize($ids_string);
     if (isset($ids[$root_id . '_' . $folder])) {
         return $ids[$root_id . '_' . $folder];
     }
     // find folder
     $items = $this->_service->files->listFiles(array('q' => '"' . $root_id . '" in parents ' . 'and title = "' . $folder . '" ' . 'and mimeType = "application/vnd.google-apps.folder" ' . 'and trashed = false'));
     if (count($items) > 0) {
         $id = $items[0]->id;
     } else {
         // create folder
         $file = new \Google_Service_Drive_DriveFile(array('title' => $folder, 'mimeType' => 'application/vnd.google-apps.folder'));
         $parent = new \Google_Service_Drive_ParentReference();
         $parent->setId($root_id);
         $file->setParents(array($parent));
         $created_file = $this->_service->files->insert($file);
         $id = $created_file->id;
         $permission = new \Google_Service_Drive_Permission();
         $permission->setValue('');
         $permission->setType('anyone');
         $permission->setRole('reader');
         $this->_service->permissions->insert($id, $permission);
     }
     if (!is_array($ids)) {
         $ids = array();
     }
     $ids[$root_id . '_' . $folder] = $id;
     set_transient('w3tc_cdn_google_drive_folder_ids', serialize($ids));
     return $id;
 }
コード例 #20
0
ファイル: formAction.php プロジェクト: amilabandara/GOCR
/**
 * Insert a new permission.
 *
 * @param Google_Service_Drive $service Drive API service instance.
 * @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.
 */
function insertPermission($service, $fileId, $value, $type, $role)
{
    $newPermission = new Google_Service_Drive_Permission();
    $newPermission->setValue($value);
    $newPermission->setType($type);
    $newPermission->setRole($role);
    try {
        return $service->permissions->insert($fileId, $newPermission);
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
    return NULL;
}
function URSRC_calendar_create($loggin,$fileId,$loginid_name,$URSC_uld_id,$finaldate,$calenderid,$status,$form,$filesarray,$URSRC_firstname,$URSRC_lastname,$folderid){
    global $con,$ClientId,$ClientSecret,$RedirectUri,$DriveScopes,$CalenderScopes,$Refresh_Token;
    $drive = new Google_Client();
    $drive->setClientId($ClientId);
    $drive->setClientSecret($ClientSecret);
    $drive->setRedirectUri($RedirectUri);
    $drive->setScopes(array($DriveScopes,$CalenderScopes));
    $drive->setAccessType('online');
    $authUrl = $drive->createAuthUrl();
    $refresh_token= $Refresh_Token;
    $drive->refreshToken($refresh_token);
    $service = new Google_Service_Drive($drive);
    if($form=='TERMINATE'){
        $file_arraycount=1;
        try {
            $permissions = $service->permissions->listPermissions($fileId);
            $return_value= $permissions->getItems();
        } catch (Exception $e) {
            $ss_flag=0;
        }
        foreach ($return_value as $key => $value) {
            if ($value->emailAddress==$loggin) {
                $permission_id=$value->id;
            }
        }
        if($permission_id!=''){
            try {
                $service->permissions->delete($fileId, $permission_id);
                $ss_flag=1;
            } catch (Exception $e) {
                $ss_flag=0;
            }
        }
        else{

            $ss_flag=1;
        }
    }
    else{
        $value=$loggin;
        $type='user';
        $role='reader';
        $email=$loggin;
        $newPermission = new Google_Service_Drive_Permission();
        $newPermission->setValue($value);
        $newPermission->setType($type);
        $newPermission->setRole($role);
        $newPermission->setEmailAddress($email);
        try {
            $service->permissions->insert($fileId, $newPermission);
            $ss_flag=1;
        } catch (Exception $e) {
            $ss_flag=0;
        }

        if($ss_flag==1){
            if($filesarray!='')
            {
                $file_array=array();
                $allfilearray=(explode(",",$filesarray));
                foreach ($allfilearray as $value)
                {
                    $uploadfilename=$value;
                    $drivefilename=$URSRC_firstname.' '.$URSRC_lastname.'-'.$uploadfilename;
                    $extension =(explode(".",$uploadfilename));
                    if($extension[1]=='pdf'){$mimeType='application/pdf';}
                    if($extension[1]=='jpg'){$mimeType='image/jpeg';}
                    if($extension[1]=='png'){$mimeType='image/png';}
                    $file_id_value =insertFile($service,$drivefilename,'PersonalDetails',$folderid,$mimeType,$uploadfilename);
                    if($file_id_value!=''){
                        array_push($file_array,$file_id_value);
                    }
                }
                $file_arraycount=count($file_array);
            }
            else{
                $file_arraycount=1;
            }
        }


    }
    if($ss_flag==1 && $file_arraycount>0){
        $cal = new Google_Service_Calendar($drive);
        $event = new Google_Service_Calendar_Event();
        $event->setsummary($loginid_name.'  '.$status);
        $event->setDescription($URSC_uld_id);
        $start = new Google_Service_Calendar_EventDateTime();
        $start->setDate($finaldate);//setDate('2014-11-18');
        $event->setStart($start);
        $event->setEnd($start);
        try{
            $createdEvent = $cal->events->insert($calenderid, $event);
            $cal_flag=1;
        }
        catch(Exception $e){
            $cal_flag=0;
        }
    }
    $flag_array=[$ss_flag,$cal_flag,$file_id_value,$file_array];
    return $flag_array;
}
コード例 #22
0
 public function createLink(UseyourDrive_Node $cachedentry = null, $shorten = true)
 {
     $link = false;
     $error = false;
     if ($cachedentry === null) {
         /* Check if file is cached and still valid */
         $cached = $this->cache->isCached($this->_requestedEntry);
         /* Get the file if not cached */
         if ($cached === false) {
             $cachedentry = $this->getEntry($this->_requestedEntry);
         } else {
             $cachedentry = $cached;
         }
     }
     if ($cachedentry !== null && $cachedentry !== false) {
         $entry = $cachedentry->getItem();
         /* Check file permission */
         $has_permission = false;
         $permissions = $entry->getPermissions();
         $permission_type = $this->settings['permission_domain'] === '' ? 'anyone' : 'domain';
         $permission_value = $this->settings['permission_domain'] === '' ? '' : $this->settings['permission_domain'];
         if (count($entry->getPermissions()) > 0) {
             if ($this->settings['manage_permissions'] === 'Yes') {
                 foreach ($entry->getPermissions() as $permission) {
                     if ($permission->getType() === $permission_type && in_array($permission->getRole(), array('reader', 'writer')) && $permission->getValue() === $permission_value && $permission->getWithLink()) {
                         $has_permission = true;
                         $shareable = true;
                         break;
                     }
                 }
             } else {
                 $shareable = true;
             }
         }
         /* Set new permission if needed */
         if ($has_permission === false && $this->settings['manage_permissions'] === 'Yes') {
             $newPermission = new Google_Service_Drive_Permission();
             $newPermission->setType($permission_type);
             $newPermission->setRole("reader");
             $newPermission->setValue($permission_value);
             $newPermission->setWithLink(true);
             try {
                 $permission = $this->googleDriveService->permissions->insert($entry->getId(), $newPermission);
                 $cachedentry = $this->getEntry($this->_requestedEntry, true);
                 $entry = $cachedentry->getItem();
                 $shareable = true;
             } catch (Exception $e) {
                 $shareable = false;
             }
         }
         $linkurl = $entry->getWebContentLink();
         if ($shareable) {
             $linkurl = $entry->getAlternateLink();
             if (strpos($entry->getMimeType(), 'video') !== false || strpos($entry->getMimeType(), 'audio') !== false) {
                 $embedlink = $entry->getEmbedLink();
                 if (!empty($embedlink)) {
                     $linkurl = 'https://docs.google.com/file/d/' . $entry->getId() . '/preview';
                 }
             }
         }
         if (!empty($linkurl)) {
             if ($shorten) {
                 $url = new Google_Service_Urlshortener_Url();
                 $url->longUrl = $linkurl;
                 $url = $this->googleUrlshortenerService->url->insert($url, array("userIp" => $this->userip));
                 $link = $url->getId();
             } else {
                 $link = $linkurl;
             }
         } else {
             $error = __("Can't create link", 'useyourdrive');
         }
     }
     $embedlink = $entry->getEmbedLink();
     if (empty($embedlink)) {
         $embedlink = 'https://docs.google.com/viewer?srcid=' . $entry->getId() . '&pid=explorer&embedded=true';
         /* As of 12 November 2014, the Google Doc viewer doesn't display PDF files anymore */
         if (strpos($entry->getMimeType(), 'application/pdf') !== false) {
             $embedlink = 'https://docs.google.com/file/d/' . $entry->getId() . '/preview';
             /* Powerpoints can't be showed embedded */
         } elseif (strpos($entry->getMimeType(), 'google-apps.presentation') !== false) {
             $embedlink = 'https://docs.google.com/presentation/d/' . $entry->getId() . '/preview';
         }
     } else {
         if (strpos($entry->getMimeType(), 'application/vnd.google-apps') === false) {
             $embedlink = 'https://docs.google.com/file/d/' . $entry->getId() . '/preview';
             /* Powerpoints can't be showed embedded */
         } elseif (strpos($entry->getMimeType(), 'google-apps.presentation') !== false) {
         } else {
             $embedlink = $entry->getAlternateLink();
             $embedlink = str_replace('http://', 'https://', $embedlink);
         }
     }
     $resultdata = array('id' => $entry->getId(), 'name' => $entry->getTitle(), 'link' => $link, 'embeddedlink' => $embedlink, 'size' => UseyourDrive_bytesToSize1024($entry->getFileSize()), 'error' => $error);
     return $resultdata;
 }
コード例 #23
0
ファイル: API.php プロジェクト: Cloudoki/google-flow-drive
 public function insert_public_folder($service)
 {
     $parentId = get_option("flowdrive_basefolder");
     $file = new \Google_Service_Drive_DriveFile();
     $file->setTitle('flowdrive');
     $file->setMimeType('application/vnd.google-apps.folder');
     // Set base folder
     if ($parentId) {
         $parent = new \Google_Service_Drive_ParentReference();
         $parent->setId($parentId);
         $file->setParents([$parent]);
     }
     $createdFile = $service->files->insert($file, ['mimeType' => 'application/vnd.google-apps.folder']);
     $permission = new \Google_Service_Drive_Permission();
     $permission->setValue('');
     $permission->setType('anyone');
     $permission->setRole('reader');
     $service->permissions->insert($createdFile->getId(), $permission);
     return $createdFile->getId();
 }