| 1 |
<?php |
| 2 |
/* |
| 3 |
* Copyright 2011 Google Inc. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
include_once "templates/base.php"; |
| 18 |
session_start(); |
| 19 |
|
| 20 |
require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); |
| 21 |
|
| 22 |
/************************************************ |
| 23 |
We'll setup an empty 1MB file to upload. |
| 24 |
************************************************/ |
| 25 |
DEFINE("TESTFILE", 'testfile-small.txt'); |
| 26 |
if (!file_exists(TESTFILE)) { |
| 27 |
$fh = fopen(TESTFILE, 'w'); |
| 28 |
fseek($fh, 1024 * 1024); |
| 29 |
fwrite($fh, "!", 1); |
| 30 |
fclose($fh); |
| 31 |
} |
| 32 |
|
| 33 |
/************************************************ |
| 34 |
ATTENTION: Fill in these values! Make sure |
| 35 |
the redirect URI is to this page, e.g: |
| 36 |
http://localhost:8080/fileupload.php |
| 37 |
************************************************/ |
| 38 |
$client_id = '<YOUR_CLIENT_ID>'; |
| 39 |
$client_secret = '<YOUR_CLIENT_SECRET>'; |
| 40 |
$redirect_uri = '<YOUR_REDIRECT_URI>'; |
| 41 |
|
| 42 |
$client = new Google_Client(); |
| 43 |
$client->setClientId($client_id); |
| 44 |
$client->setClientSecret($client_secret); |
| 45 |
$client->setRedirectUri($redirect_uri); |
| 46 |
$client->addScope("https://www.googleapis.com/auth/drive"); |
| 47 |
$service = new Google_Service_Drive($client); |
| 48 |
|
| 49 |
if (isset($_REQUEST['logout'])) { |
| 50 |
unset($_SESSION['upload_token']); |
| 51 |
} |
| 52 |
|
| 53 |
if (isset($_GET['code'])) { |
| 54 |
$client->authenticate($_GET['code']); |
| 55 |
$_SESSION['upload_token'] = $client->getAccessToken(); |
| 56 |
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; |
| 57 |
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); |
| 58 |
} |
| 59 |
|
| 60 |
if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) { |
| 61 |
$client->setAccessToken($_SESSION['upload_token']); |
| 62 |
if ($client->isAccessTokenExpired()) { |
| 63 |
unset($_SESSION['upload_token']); |
| 64 |
} |
| 65 |
} else { |
| 66 |
$authUrl = $client->createAuthUrl(); |
| 67 |
} |
| 68 |
|
| 69 |
/************************************************ |
| 70 |
If we're signed in then lets try to upload our |
| 71 |
file. For larger files, see fileupload.php. |
| 72 |
************************************************/ |
| 73 |
if ($client->getAccessToken()) { |
| 74 |
// This is uploading a file directly, with no metadata associated. |
| 75 |
$file = new Google_Service_Drive_DriveFile(); |
| 76 |
$result = $service->files->insert( |
| 77 |
$file, |
| 78 |
array( |
| 79 |
'data' => file_get_contents(TESTFILE), |
| 80 |
'mimeType' => 'application/octet-stream', |
| 81 |
'uploadType' => 'media' |
| 82 |
) |
| 83 |
); |
| 84 |
|
| 85 |
// Now lets try and send the metadata as well using multipart! |
| 86 |
$file = new Google_Service_Drive_DriveFile(); |
| 87 |
$file->setTitle("Hello World!"); |
| 88 |
$result2 = $service->files->insert( |
| 89 |
$file, |
| 90 |
array( |
| 91 |
'data' => file_get_contents(TESTFILE), |
| 92 |
'mimeType' => 'application/octet-stream', |
| 93 |
'uploadType' => 'multipart' |
| 94 |
) |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
echo pageHeader("File Upload - Uploading a small file"); |
| 99 |
if (strpos($client_id, "googleusercontent") == false) { |
| 100 |
echo missingClientSecretsWarning(); |
| 101 |
exit; |
| 102 |
} |
| 103 |
?> |
| 104 |
<div class="box"> |
| 105 |
<div class="request"> |
| 106 |
<?php |
| 107 |
if (isset($authUrl)) { |
| 108 |
echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>"; |
| 109 |
} |
| 110 |
?> |
| 111 |
</div> |
| 112 |
|
| 113 |
<div class="shortened"> |
| 114 |
<?php |
| 115 |
if (isset($result) && $result) { |
| 116 |
var_dump($result->title); |
| 117 |
var_dump($result2->title); |
| 118 |
} |
| 119 |
?> |
| 120 |
</div> |
| 121 |
</div> |
| 122 |
<?php |
| 123 |
echo pageFooter(__FILE__); |
| 124 |
|