示例#1
0
 public function setPermissions($fileId, $value, $role = 'writer', $type = 'user')
 {
     $perm = new Google_Permission();
     $perm->setValue($value);
     $perm->setType($type);
     $perm->setRole($role);
     $this->_service->permissions->insert($fileId, $perm);
 }
示例#2
0
function bdn_set_doc_permissions($gdocID = false, $post = array())
{
    if (empty($gdocID) || empty($post)) {
        return;
    }
    //Check and see if there's a drive object stored globally
    global $driveService;
    //If there's not, we should set one up
    if (!is_object($driveService)) {
        $driveService = bdn_is_user_auth();
    }
    //If the current user selected someone else as the author, change the owner of the doc
    if ($post['post_author'] != get_current_user_id()) {
        $user = get_user_by('id', $post['post_author']);
        //Make sure it's a BDN email first, tho
        if (strpos($user->user_email, '@' . GOOGLE_LOGIN_DOMAIN) !== false) {
            $changeOwner = new Google_Permission();
            $changeOwner->setValue($user->user_email);
            $changeOwner->setType('user');
            $changeOwner->setRole('owner');
        }
    }
    //Anybody at BDN with link can edit
    $filePermissions = new Google_Permission();
    $filePermissions->setValue(GOOGLE_LOGIN_DOMAIN);
    $filePermissions->setType('domain');
    $filePermissions->setRole('writer');
    $filePermissions->setWithLink(true);
    //Exponential backoff
    $permissions = false;
    for ($i = 0; $i < 10; $i++) {
        if (!empty($permissions)) {
            continue;
        }
        sleep($i * 2);
        try {
            //Set the permissions of the doc to anyone at BDN with link
            $driveService->permissions->insert($gdocID, $filePermissions);
            //If this person isn't the owner of the budget item, change the owner of the Doc
            if ($post['post_author'] != get_current_user_id()) {
                $driveService->permissions->insert($gdocID, $changeOwner, array('sendNotificationEmails' => false));
            }
            $permissions = true;
        } catch (Exception $e) {
            //@TODO: Check if it's an error we should be retrying for
            error_log('DRIVE API ERROR: ' . $e->getMessage() . ' : ' . json_encode($e));
            $permissions = false;
        }
    }
}
示例#3
0
function createPublicFolder($service, $folderName)
{
    $file = new Google_DriveFile();
    $file->setTitle($folderName);
    $file->setMimeType('application/vnd.google-apps.folder');
    $createdFile = $service->files->insert($file, array('mimeType' => 'application/vnd.google-apps.folder'));
    $permission = new Google_Permission();
    $permission->setValue('');
    $permission->setType('anyone');
    $permission->setRole('reader');
    try {
        $returnVal = $service->permissions->insert($createdFile->getId(), $permission);
        //pr($returnVal);
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
        exit;
    }
    return $returnVal;
}
示例#4
0
 public function makePublic($filename)
 {
     $permission = new Google_Permission();
     $permission->setValue('');
     $permission->setType('anyone');
     $permission->setRole('reader');
     $this->drive_service->permissions->insert($filename, $permission);
     // the "official" webContentLink requires some form of auth, even for public. dumb
     //
     // $file = $this->drive_service->files->get($filename);
     // return $file['webContentLink'];
     $public_link = 'https://drive.google.com/uc?export=download&id=' . $filename;
     return $public_link;
 }
示例#5
0
} else {
    //Build the Drive Service object authorized with your Service Account
    $auth = new Google_AssertionCredentials($SERVICE_ACCOUNT_EMAIL, array($DRIVE_SCOPE), file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH));
    $client = new Google_Client();
    $client->setUseObjects(true);
    $client->setAssertionCredentials($auth);
    $service = new Google_DriveService($client);
    //Create the file
    $file = new Google_DriveFile();
    $file->setTitle($vidName);
    //$file->setDescription("Please feel free to write to this document and collaborate");
    // set the default description of the document
    $file->setMimeType('application/vnd.google-apps.document');
    $file = $service->files->insert($file);
    //Give everyone permission to read and write the file
    $permission = new Google_Permission();
    $permission->setRole('writer');
    $permission->setType('anyone');
    $permission->setValue('me');
    $permission->setwithLink(true);
    $service->permissions->insert($file->getId(), $permission);
    $headURL = $file->getalternateLink();
    mysql_query("UPDATE videonodes set gdurl='{$headURL}' WHERE id='{$vid}'") or die(mysql_error());
    //echo $file->getalternateLink();
    //print_r( $file);
}
// end of gdurl
echo "<br> <font color =\"red\" size = \"5\" > If the page is not redirected automatically then please click <a href=\"" . $headURL . "\">here</a> to open the document </font>";
$hstr = "Location: " . $headURL;
header($hstr);
/* Make sure that code below does not get executed when we redirect. */