Welcome to mirror list, hosted at ThFree Co, Russian Federation.

google.php « filestorage « lib - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dd898b7955de26184198a49e2850cb64e4f8e3c8 (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php

/**
* ownCloud
*
* @author Michael Gapczynski
* @copyright 2012 Michael Gapczynski mtgap@owncloud.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

class OC_Filestorage_Google {

	private $datadir;
	private $consumer;
	private $oauth_token;
	private $sig_method;

	public function __construct($arguments) {
		$this->datadir = $arguments['datadir'];
		$consumer_key = isset($arguments['consumer_key']) ? $arguments['consumer_key'] : 'anonymous';
		$consumer_secret = isset($arguments['consumer_secret']) ? $arguments['consumer_secret'] : 'anonymous';
		$this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
		$this->oauth_token = new OAuthToken($arguments['token'], $arguments['token_secret']);
		$this->sig_method = new OAuthSignatureMethod_HMAC_SHA1();
	}

	private function sendRequest($feedUri, $http_method, $postData = null) {
		$feedUri = trim($feedUri);
		// create an associative array from each key/value url query param pair.
		$params = array();
		$pieces = explode('?', $feedUri);
		if (isset($pieces[1])) {
			$params = explode_assoc('=', '&', $pieces[1]);
		}
		// urlencode each url parameter key/value pair
		$tempStr = $pieces[0];
		foreach ($params as $key => $value) {
			$tempStr .= '&' . urlencode($key) . '=' . urlencode($value);
		}
		$feedUri = preg_replace('/&/', '?', $tempStr, 1);
		$req = OAuthRequest::from_consumer_and_token($this->consumer, $this->oauth_token, $http_method, $feedUri, $params);
		$req->sign_request($this->sig_method, $this->consumer, $this->oauth_token);
		$auth_header = $req->to_header();
		$result = send_signed_request($http_method, $feedUri, array($auth_header, 'Content-Type: application/atom+xml', 'GData-Version: 3.0'), $postData);
		// TODO Return false if error is received
		if (!$result) {
			return false;
		}
		$result = explode('<', $result, 2);
		$result = isset($result[1]) ? '<'.$result[1] : $result[0];
		$dom = new DOMDocument();
		$dom->loadXML($result);
		return $dom;
	}

	private function getResource($path) {
		// TODO Look up google docs query caching/only send back if changes occured
		// TODO Look inside of collections for specific file
		// TODO Strip extension
		$title = basename($path);
		return $this->sendRequest('https://docs.google.com/feeds/default/private/full?showfolders=true&title='.$title.'&title-exact=true', 'GET');
	}

	public function mkdir($path) {
		$dir = dirname($path);
		// Check if path parent is root directory
		if ($dir == '/' || $dir == '\.' || $dir == '.') {
			$feedUri = 'https://docs.google.com/feeds/default/private/full';
		// Get parent content link
		} else {
			$dom = $this->getResource(basename($dir));
			$feedUri = $dom->getElementsByTagName('content')->item(0)->getAttribute('src');
		}
		$title = basename($path);
		// Construct post data
		$postData = '<?xml version="1.0" encoding="UTF-8"?>';
		$postData .= '<entry xmlns="http://www.w3.org/2005/Atom">';
		$postData .= '<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/docs/2007#folder"/>';
		$postData .= '<title>'.$title.'</title>';
		$postData .= '</entry>';
		if ($dom = $this->sendRequest($feedUri, 'POST', $postData)) {
			return true;
		}
		return false;
	}

	public function rmdir($path) {
		return $this->unlink($path);
	}

	public function opendir($path) {
		if ($path == '' || $path == '/') {
			$resource = 'https://docs.google.com/feeds/default/private/full/folder%3Aroot/contents';
			$dom = $this->sendRequest('https://docs.google.com/feeds/default/private/full/folder%3Aroot/contents', 'GET');
		} else {
			$dom = $this->getResource($path); 
		}
		global $FAKEDIRS;
		$files = array();
// 		while ($next) {
// 			// send request $next link
// 			$links = $dom->getElementsByTagName('link');
// 			foreach ($links as $link) {
// 				if ($link->getAttribute('rel') == 'next') {
// 					$next = $link->getAttribute('src');
// 					break;
// 				} else {
// 					$next = false;
// 				}
// 			}
			$entries = $dom->getElementsByTagName('entry');
			foreach($entries as $entry) {
				$name = $entry->getElementsByTagName('title')->item(0)->nodeValue;
				// Native Google resources don't include extensions in title
				if (!strpos($name, '.')) {
					if ($ext = $this->filetype('', $entry)) {
						$name .= '.'.$ext;
					}
				}
				$files[] = $name;
			}
// 		}
		$FAKEDIRS['google'] = $files;
		return opendir('fakedir://google');
	}

	public function is_dir($path) {
		if ($entry = $this->getResource($path)) {
			$categories = $entry->getElementsByTagName('category');
			foreach ($categories as $category) {
				if ($category->getAttribute('scheme') == 'http://schemas.google.com/g/2005#kind') {
					// Check if label is equal to folder
					$type = $category->getAttribute('label');
					if (strlen(strstr($type, 'folder')) > 0) {
						return true;
					}
				}
			}
		}
		return false; 
	}

	public function is_file($path) {
		if ($this->getResource($path)) {
			return true;
		}
		return false;
	}

	public function stat($path) {
		if ($dom = $this->getResource($path)) {
			// TODO Native resources don't have a file size
			$stat['size'] =  $dom->getElementsByTagNameNS('http://schemas.google.com/g/2005', 'quotaBytesUsed')->item(0)->nodeValue;
			$stat['mtime'] = strtotime($dom->getElementsByTagName('updated')->item(1)->nodeValue);
			$stat['ctime'] = strtotime($dom->getElementsByTagName('published')->item(0)->nodeValue);
			return $stat;
		}
		return false; 
		
	}

	public function filetype($path, $entry = null) {
		if ($entry == null) {
			$entry = $this->getResource($path);
		}
		$categories = $entry->getElementsByTagName('category');
		foreach ($categories as $category) {
			if ($category->getAttribute('scheme') == 'http://schemas.google.com/g/2005#kind') {
				// Guess extension from label, default to ODF extensions
				$type = $category->getAttribute('label');
				if (strlen(strstr($type, 'folder')) > 0) {
					return '';
				} else if (strlen(strstr($type, 'document')) > 0) {
					return 'odt';
				} else if (strlen(strstr($type, 'presentation')) > 0) {
					return 'odp';
				} else if (strlen(strstr($type, 'spreadsheet')) > 0) {
					return 'ods';
				} else {
					return $type;
				}
			}
		}
	}

	public function is_readable($path) {
		return true;
	}

	public function is_writable($path) {
		// Check if edit or edit-media links exist
		if ($entry = $this->getResource($path)) {
			$links = $entry->getElementsByTagName('link');
			foreach ($links as $link) {
				if ($link->getAttribute('rel') == 'edit') {
					return true;
				} else if ($link->getAttribute('rel') == 'edit-media') {
					return true;
				}
			}
		}
		return false;
	}
	
	public function file_exists($path) {
		if ($this->getResource($path)) {
			return true;
		}
		return false;
	}
	
	public function unlink($path) {
		// Get resource self link to trash resource
		if ($entry = $this->getResource($path)) {
			$links = $entry->getElementsByTagName('link');
			foreach ($links as $link) {
				if ($link->getAttribute('rel') == 'self') {
					$feedUri = $link->getAttribute('href');
				}
			}
		}
		if (isset($feedUri)) {
			$this->sendRequest($feedUri, 'DELETE');
			return true;
		}
		return false;
	}

	public function rename($path1,$path2) {

	}

	public function fopen($path,$mode){}

	public function toTmpFile($path) {
		$dom = $this->getResource($path);
		$url = $dom->getElementsByTagName('content')->getAttribute('src');		
	}

	public function fromTmpFile($tmpPath,$path){}
	public function fromUploadedFile($tmpPath,$path){}
	public function getMimeType($path){}
	public function hash($type,$path,$raw){}
	
	public function free_space($path) {
		if ($dom = $this->sendRequest('https://docs.google.com/feeds/metadata/default', 'GET')) {
			$total = $dom->getElementsByTagNameNS('http://schemas.google.com/g/2005', 'quotaBytesTotal')->item(0)->nodeValue;
			$used = $dom->getElementsByTagNameNS('http://schemas.google.com/g/2005', 'quotaBytesUsed')->item(0)->nodeValue;
			return $total - $used;
		}
		return false;
	}
  
	public function search($query){}
	
	public function getLocalFile($path) {
		return false;
	}
}