-
Notifications
You must be signed in to change notification settings - Fork 0
/
formAction.php
208 lines (183 loc) · 7.8 KB
/
formAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/*
* Created on Mon Jan 17 2022
*
* Devlan - devlan.co.ke
*
* hello@devlan.info
*
*
* The Devlan End User License Agreement
*
* Copyright (c) 2022 Devlan
*
* 1. GRANT OF LICENSE
* Devlan hereby grants to you (an individual) the revocable, personal, non-exclusive, and nontransferable right to
* install and activate this system on two separated computers solely for your personal and non-commercial use,
* unless you have purchased a commercial license from Devlan. Sharing this Software with other individuals,
* or allowing other individuals to view the contents of this Software, is in violation of this license.
* You may not make the Software available on a network, or in any way provide the Software to multiple users
* unless you have first purchased at least a multi-user license from Devlan.
*
* 2. COPYRIGHT
* The Software is owned by Devlan and protected by copyright law and international copyright treaties.
* You may not remove or conceal any proprietary notices, labels or marks from the Software.
*
* 3. RESTRICTIONS ON USE
* You may not, and you may not permit others to
* (a) reverse engineer, decompile, decode, decrypt, disassemble, or in any way derive source code from, the Software;
* (b) modify, distribute, or create derivative works of the Software;
* (c) copy (other than one back-up copy), distribute, publicly display, transmit, sell, rent, lease or
* otherwise exploit the Software.
*
* 4. TERM
* This License is effective until terminated.
* You may terminate it at any time by destroying the Software, together with all copies thereof.
* This License will also terminate if you fail to comply with any term or condition of this Agreement.
* Upon such termination, you agree to destroy the Software, together with all copies thereof.
*
* 5. NO OTHER WARRANTIES.
* Devlan DOES NOT WARRANT THAT THE SOFTWARE IS ERROR FREE.
* Devlan SOFTWARE DISCLAIMS ALL OTHER WARRANTIES WITH RESPECT TO THE SOFTWARE,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
* SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES OR LIMITATIONS
* ON HOW LONG AN IMPLIED WARRANTY MAY LAST, OR THE EXCLUSION OR LIMITATION OF
* INCIDENTAL OR CONSEQUENTIAL DAMAGES,
* SO THE ABOVE LIMITATIONS OR EXCLUSIONS MAY NOT APPLY TO YOU.
* THIS WARRANTY GIVES YOU SPECIFIC LEGAL RIGHTS AND YOU MAY ALSO
* HAVE OTHER RIGHTS WHICH VARY FROM JURISDICTION TO JURISDICTION.
*
* 6. SEVERABILITY
* In the event of invalidity of any provision of this license, the parties agree that such invalidity shall not
* affect the validity of the remaining portions of this license.
*
* 7. NO LIABILITY FOR CONSEQUENTIAL DAMAGES IN NO EVENT SHALL DEVLAN OR ITS SUPPLIERS BE LIABLE TO YOU FOR ANY
* CONSEQUENTIAL, SPECIAL, INCIDENTAL OR INDIRECT DAMAGES OF ANY KIND ARISING OUT OF THE DELIVERY, PERFORMANCE OR
* USE OF THE SOFTWARE, EVEN IF DEVLAN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
* IN NO EVENT WILL DEVLAN LIABILITY FOR ANY CLAIM, WHETHER IN CONTRACT
* TORT OR ANY OTHER THEORY OF LIABILITY, EXCEED THE LICENSE FEE PAID BY YOU, IF ANY.
*/
require_once 'google-api-php-client/src/Google/Client.php';
require_once 'google-api-php-client/src/Google/Service/Oauth2.php';
require_once 'google-api-php-client/src/Google/Service/Drive.php';
session_start();
header('Content-Type: text/html; charset=utf-8');
// Init the variables
$driveInfo = "";
$folderName = "";
$folderDesc = "";
// Get the file path from the variable
$file_tmp_name = $_FILES["file"]["tmp_name"];
// Get the client Google credentials
$credentials = $_COOKIE["credentials"];
// Get your app info from JSON downloaded from google dev console
$json = json_decode(file_get_contents("GoogleCloud.json"), true);
$CLIENT_ID = '';
$CLIENT_SECRET = '';
$REDIRECT_URI = '';
// Create a new Client
$client = new Google_Client();
$client->setClientId($CLIENT_ID);
$client->setClientSecret($CLIENT_SECRET);
$client->setRedirectUri($REDIRECT_URI);
$client->addScope(
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.appfolder"
);
// Refresh the user token and grand the privileges
$client->setAccessToken($credentials);
$service = new Google_Service_Drive($client);
// Set the file metadata for drive
$mimeType = $_FILES["file"]["type"];
$title = $_FILES["file"]["name"];
$description = "Uploaded from your very first google drive application!";
// Get the folder metadata
if (!empty($_POST["folderName"]))
$folderName = $_POST["folderName"];
if (!empty($_POST["folderDesc"]))
$folderDesc = $_POST["folderDesc"];
// Call the insert function with parameters listed below
$driveInfo = insertFile($service, $title, $description, $mimeType, $file_tmp_name, $folderName, $folderDesc);
/**
* Get the folder ID if it exists, if it doesnt exist, create it and return the ID
*
* @param Google_DriveService $service Drive API service instance.
* @param String $folderName Name of the folder you want to search or create
* @param String $folderDesc Description metadata for Drive about the folder (optional)
* @return Google_Drivefile that was created or got. Returns NULL if an API error occured
*/
function getFolderExistsCreate($service, $folderName, $folderDesc)
{
// List all user files (and folders) at Drive root
$files = $service->files->listFiles();
$found = false;
// Go through each one to see if there is already a folder with the specified name
foreach ($files['items'] as $item) {
if ($item['title'] == $folderName) {
$found = true;
return $item['id'];
break;
}
}
// If not, create one
if ($found == false) {
$folder = new Google_Service_Drive_DriveFile();
//Setup the folder to create
$folder->setTitle($folderName);
if (!empty($folderDesc))
$folder->setDescription($folderDesc);
$folder->setMimeType('application/vnd.google-apps.folder');
//Create the Folder
try {
$createdFile = $service->files->insert($folder, array(
'mimeType' => 'application/vnd.google-apps.folder',
));
// Return the created folder's id
return $createdFile->id;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
}
/**
* Insert new file in the Application Data folder.
*
* @param Google_DriveService $service Drive API service instance.
* @param string $title Title of the file to insert, including the extension.
* @param string $description Description of the file to insert.
* @param string $mimeType MIME type of the file to insert.
* @param string $filename Filename of the file to insert.
* @return Google_DriveFile The file that was inserted. NULL is returned if an API error occurred.
*/
function insertFile($service, $title, $description, $mimeType, $filename, $folderName, $folderDesc)
{
$file = new Google_Service_Drive_DriveFile();
// Set the metadata
$file->setTitle($title);
$file->setDescription($description);
$file->setMimeType($mimeType);
// Setup the folder you want the file in, if it is wanted in a folder
if (isset($folderName)) {
if (!empty($folderName)) {
$parent = new Google_Service_Drive_ParentReference();
$parent->setId(getFolderExistsCreate($service, $folderName, $folderDesc));
$file->setParents(array($parent));
}
}
try {
// Get the contents of the file uploaded
$data = file_get_contents($filename);
// Try to upload the file, you can add the parameters e.g. if you want to convert a .doc to editable google format, add 'convert' = 'true'
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => $mimeType,
'uploadType' => 'multipart'
));
// Return a bunch of data including the link to the file we just uploaded
return $createdFile;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
echo "<br>Link to file: " . $driveInfo["alternateLink"];