2012-02-18 06:56:20 +04:00
|
|
|
<?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/>.
|
|
|
|
*/
|
|
|
|
|
2012-09-07 20:30:48 +04:00
|
|
|
namespace OC\Files\Storage;
|
|
|
|
|
2012-06-13 21:54:32 +04:00
|
|
|
require_once 'Google/common.inc.php';
|
2012-02-29 04:15:49 +04:00
|
|
|
|
2012-09-07 20:30:48 +04:00
|
|
|
class Google extends \OC\Files\Storage\Common {
|
2012-02-27 23:59:41 +04:00
|
|
|
|
|
|
|
private $consumer;
|
|
|
|
private $oauth_token;
|
|
|
|
private $sig_method;
|
2012-02-29 04:15:49 +04:00
|
|
|
private $entries;
|
2012-10-12 01:06:57 +04:00
|
|
|
private $id;
|
2012-02-27 23:59:41 +04:00
|
|
|
|
2012-05-02 22:38:22 +04:00
|
|
|
private static $tempFiles = array();
|
|
|
|
|
2012-08-14 01:07:56 +04:00
|
|
|
public function __construct($params) {
|
2012-11-30 19:27:11 +04:00
|
|
|
if (isset($params['configured']) && $params['configured'] == 'true'
|
|
|
|
&& isset($params['token'])
|
|
|
|
&& isset($params['token_secret'])
|
|
|
|
) {
|
2012-08-14 01:07:56 +04:00
|
|
|
$consumer_key = isset($params['consumer_key']) ? $params['consumer_key'] : 'anonymous';
|
|
|
|
$consumer_secret = isset($params['consumer_secret']) ? $params['consumer_secret'] : 'anonymous';
|
2013-01-10 07:17:39 +04:00
|
|
|
$this->id = 'google::' . $params['token'];
|
2012-09-07 20:30:48 +04:00
|
|
|
$this->consumer = new \OAuthConsumer($consumer_key, $consumer_secret);
|
|
|
|
$this->oauth_token = new \OAuthToken($params['token'], $params['token_secret']);
|
|
|
|
$this->sig_method = new \OAuthSignatureMethod_HMAC_SHA1();
|
2012-08-14 01:07:56 +04:00
|
|
|
$this->entries = array();
|
|
|
|
} else {
|
2012-09-07 20:30:48 +04:00
|
|
|
throw new \Exception('Creating \OC\Files\Storage\Google storage failed');
|
2012-08-14 01:07:56 +04:00
|
|
|
}
|
2012-02-27 23:59:41 +04:00
|
|
|
}
|
|
|
|
|
2012-11-30 19:27:11 +04:00
|
|
|
private function sendRequest($uri,
|
|
|
|
$httpMethod,
|
|
|
|
$postData = null,
|
|
|
|
$extraHeaders = null,
|
|
|
|
$isDownload = false,
|
|
|
|
$returnHeaders = false,
|
|
|
|
$isContentXML = true,
|
|
|
|
$returnHTTPCode = false) {
|
2012-05-02 22:38:22 +04:00
|
|
|
$uri = trim($uri);
|
2012-02-27 23:59:41 +04:00
|
|
|
// create an associative array from each key/value url query param pair.
|
|
|
|
$params = array();
|
2012-05-02 22:38:22 +04:00
|
|
|
$pieces = explode('?', $uri);
|
2012-02-27 23:59:41 +04:00
|
|
|
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);
|
|
|
|
}
|
2012-05-02 22:38:22 +04:00
|
|
|
$uri = preg_replace('/&/', '?', $tempStr, 1);
|
2012-12-03 21:02:22 +04:00
|
|
|
$request = \OAuthRequest::from_consumer_and_token($this->consumer,
|
2012-11-30 19:27:11 +04:00
|
|
|
$this->oauth_token,
|
|
|
|
$httpMethod,
|
|
|
|
$uri,
|
|
|
|
$params);
|
2012-03-24 22:49:44 +04:00
|
|
|
$request->sign_request($this->sig_method, $this->consumer, $this->oauth_token);
|
|
|
|
$auth_header = $request->to_header();
|
2012-05-02 22:38:22 +04:00
|
|
|
$headers = array($auth_header, 'GData-Version: 3.0');
|
|
|
|
if ($isContentXML) {
|
|
|
|
$headers = array_merge($headers, array('Content-Type: application/atom+xml'));
|
|
|
|
}
|
|
|
|
if (is_array($extraHeaders)) {
|
|
|
|
$headers = array_merge($headers, $extraHeaders);
|
|
|
|
}
|
|
|
|
$curl = curl_init($uri);
|
2012-03-24 22:49:44 +04:00
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($curl, CURLOPT_FAILONERROR, false);
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
2012-05-02 22:38:22 +04:00
|
|
|
switch ($httpMethod) {
|
2012-03-24 22:49:44 +04:00
|
|
|
case 'GET':
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
break;
|
|
|
|
case 'POST':
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
curl_setopt($curl, CURLOPT_POST, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
|
|
|
|
break;
|
|
|
|
case 'PUT':
|
|
|
|
$headers[] = 'If-Match: *';
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
2012-05-02 22:38:22 +04:00
|
|
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $httpMethod);
|
2012-03-24 22:49:44 +04:00
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
|
|
|
|
break;
|
|
|
|
case 'DELETE':
|
|
|
|
$headers[] = 'If-Match: *';
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
2012-05-02 22:38:22 +04:00
|
|
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $httpMethod);
|
2012-03-24 22:49:44 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
}
|
|
|
|
if ($isDownload) {
|
2012-09-07 20:30:48 +04:00
|
|
|
$tmpFile = \OC_Helper::tmpFile();
|
2012-05-02 22:38:22 +04:00
|
|
|
$handle = fopen($tmpFile, 'w');
|
|
|
|
curl_setopt($curl, CURLOPT_FILE, $handle);
|
|
|
|
}
|
|
|
|
if ($returnHeaders) {
|
|
|
|
curl_setopt($curl, CURLOPT_HEADER, true);
|
2012-02-27 23:59:41 +04:00
|
|
|
}
|
2012-03-24 22:49:44 +04:00
|
|
|
$result = curl_exec($curl);
|
2012-05-02 22:38:22 +04:00
|
|
|
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
2012-03-24 22:49:44 +04:00
|
|
|
curl_close($curl);
|
2012-05-02 22:38:22 +04:00
|
|
|
if ($result) {
|
|
|
|
// TODO https://developers.google.com/google-apps/documents-list/#handling_api_errors
|
|
|
|
// TODO Log error messages
|
|
|
|
if ($httpCode <= 308) {
|
|
|
|
if ($isDownload) {
|
|
|
|
return $tmpFile;
|
2012-06-15 04:56:48 +04:00
|
|
|
} else if ($returnHTTPCode) {
|
|
|
|
return array('result' => $result, 'code' => $httpCode);
|
2012-05-02 22:38:22 +04:00
|
|
|
} else {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-29 10:42:49 +04:00
|
|
|
return false;
|
2012-05-02 22:38:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getFeed($feedUri, $httpMethod, $postData = null) {
|
|
|
|
$result = $this->sendRequest($feedUri, $httpMethod, $postData);
|
|
|
|
if ($result) {
|
2012-10-12 01:17:59 +04:00
|
|
|
$dom = new \DOMDocument();
|
2012-05-02 22:38:22 +04:00
|
|
|
$dom->loadXML($result);
|
|
|
|
return $dom;
|
|
|
|
}
|
|
|
|
return false;
|
2012-02-27 23:59:41 +04:00
|
|
|
}
|
|
|
|
|
2012-11-30 19:27:11 +04:00
|
|
|
/**
|
|
|
|
* Base url for google docs feeds
|
|
|
|
*/
|
|
|
|
const BASE_URI='https://docs.google.com/feeds';
|
|
|
|
|
2012-02-27 23:59:41 +04:00
|
|
|
private function getResource($path) {
|
2012-03-24 22:49:44 +04:00
|
|
|
$file = basename($path);
|
|
|
|
if (array_key_exists($file, $this->entries)) {
|
|
|
|
return $this->entries[$file];
|
2012-02-29 04:15:49 +04:00
|
|
|
} else {
|
2012-03-24 22:49:44 +04:00
|
|
|
// Strip the file extension; file could be a native Google Docs resource
|
|
|
|
if ($pos = strpos($file, '.')) {
|
|
|
|
$title = substr($file, 0, $pos);
|
2012-11-30 19:27:11 +04:00
|
|
|
$dom = $this->getFeed(self::BASE_URI.'/default/private/full?showfolders=true&title='.$title, 'GET');
|
2012-03-24 22:49:44 +04:00
|
|
|
// Check if request was successful and entry exists
|
|
|
|
if ($dom && $entry = $dom->getElementsByTagName('entry')->item(0)) {
|
|
|
|
$this->entries[$file] = $entry;
|
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
}
|
2012-11-30 19:27:11 +04:00
|
|
|
$dom = $this->getFeed(self::BASE_URI.'/default/private/full?showfolders=true&title='.$file, 'GET');
|
2012-02-29 04:15:49 +04:00
|
|
|
// Check if request was successful and entry exists
|
|
|
|
if ($dom && $entry = $dom->getElementsByTagName('entry')->item(0)) {
|
2012-03-24 22:49:44 +04:00
|
|
|
$this->entries[$file] = $entry;
|
2012-02-29 04:15:49 +04:00
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-27 23:59:41 +04:00
|
|
|
}
|
|
|
|
|
2012-02-29 04:15:49 +04:00
|
|
|
private function getExtension($entry) {
|
|
|
|
$mimetype = $this->getMimeType('', $entry);
|
2012-03-24 22:49:44 +04:00
|
|
|
switch ($mimetype) {
|
2012-02-29 04:15:49 +04:00
|
|
|
case 'httpd/unix-directory':
|
|
|
|
return '';
|
|
|
|
case 'application/vnd.oasis.opendocument.text':
|
|
|
|
return 'odt';
|
|
|
|
case 'application/vnd.oasis.opendocument.spreadsheet':
|
|
|
|
return 'ods';
|
|
|
|
case 'application/vnd.oasis.opendocument.presentation':
|
|
|
|
return 'pptx';
|
|
|
|
case 'text/html':
|
|
|
|
return 'html';
|
|
|
|
default:
|
|
|
|
return 'html';
|
|
|
|
}
|
|
|
|
}
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2012-10-12 01:06:57 +04:00
|
|
|
public function getId(){
|
|
|
|
return $this->id;
|
|
|
|
}
|
2012-02-29 04:15:49 +04:00
|
|
|
|
2012-02-27 23:59:41 +04:00
|
|
|
public function mkdir($path) {
|
2012-05-02 22:38:22 +04:00
|
|
|
$collection = dirname($path);
|
2012-02-27 23:59:41 +04:00
|
|
|
// Check if path parent is root directory
|
2012-05-02 22:38:22 +04:00
|
|
|
if ($collection == '/' || $collection == '\.' || $collection == '.') {
|
2012-11-30 19:27:11 +04:00
|
|
|
$uri = self::BASE_URI.'/default/private/full';
|
|
|
|
} else {
|
|
|
|
// Get parent content link
|
|
|
|
$dom = $this->getResource(basename($collection));
|
|
|
|
if ($dom) {
|
|
|
|
$uri = $dom->getElementsByTagName('content')->item(0)->getAttribute('src');
|
|
|
|
}
|
2012-02-27 23:59:41 +04:00
|
|
|
}
|
2012-05-02 22:38:22 +04:00
|
|
|
if (isset($uri)) {
|
2012-02-29 04:15:49 +04:00
|
|
|
$title = basename($path);
|
|
|
|
// Construct post data
|
|
|
|
$postData = '<?xml version="1.0" encoding="UTF-8"?>';
|
|
|
|
$postData .= '<entry xmlns="http://www.w3.org/2005/Atom">';
|
2012-11-30 19:27:11 +04:00
|
|
|
$postData .= '<category scheme="http://schemas.google.com/g/2005#kind"';
|
|
|
|
$postData .= ' term="http://schemas.google.com/docs/2007#folder"/>';
|
2012-02-29 04:15:49 +04:00
|
|
|
$postData .= '<title>'.$title.'</title>';
|
|
|
|
$postData .= '</entry>';
|
2012-11-30 19:27:11 +04:00
|
|
|
$dom = $this->sendRequest($uri, 'POST', $postData);
|
|
|
|
if ($dom) {
|
2012-02-29 04:15:49 +04:00
|
|
|
return true;
|
|
|
|
}
|
2012-02-27 23:59:41 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-18 06:56:20 +04:00
|
|
|
|
2012-02-27 23:59:41 +04:00
|
|
|
public function rmdir($path) {
|
|
|
|
return $this->unlink($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function opendir($path) {
|
|
|
|
if ($path == '' || $path == '/') {
|
2012-11-30 19:27:11 +04:00
|
|
|
$next = self::BASE_URI.'/default/private/full/folder%3Aroot/contents';
|
2012-02-27 23:59:41 +04:00
|
|
|
} else {
|
2012-11-30 19:27:11 +04:00
|
|
|
$entry = $this->getResource($path);
|
|
|
|
if ($entry) {
|
2012-02-29 22:18:19 +04:00
|
|
|
$next = $entry->getElementsByTagName('content')->item(0)->getAttribute('src');
|
2012-02-29 04:15:49 +04:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-27 23:59:41 +04:00
|
|
|
}
|
|
|
|
$files = array();
|
2012-02-29 04:15:49 +04:00
|
|
|
while ($next) {
|
2012-05-02 22:38:22 +04:00
|
|
|
$dom = $this->getFeed($next, 'GET');
|
2012-02-29 04:15:49 +04:00
|
|
|
$links = $dom->getElementsByTagName('link');
|
|
|
|
foreach ($links as $link) {
|
|
|
|
if ($link->getAttribute('rel') == 'next') {
|
|
|
|
$next = $link->getAttribute('src');
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$next = false;
|
|
|
|
}
|
|
|
|
}
|
2012-02-27 23:59:41 +04:00
|
|
|
$entries = $dom->getElementsByTagName('entry');
|
2012-02-29 04:15:49 +04:00
|
|
|
foreach ($entries as $entry) {
|
2012-02-27 23:59:41 +04:00
|
|
|
$name = $entry->getElementsByTagName('title')->item(0)->nodeValue;
|
2012-02-29 04:15:49 +04:00
|
|
|
// Google Docs resources don't always include extensions in title
|
2012-11-30 19:27:11 +04:00
|
|
|
if ( ! strpos($name, '.')) {
|
2012-03-24 22:49:44 +04:00
|
|
|
$extension = $this->getExtension($entry);
|
|
|
|
if ($extension != '') {
|
|
|
|
$name .= '.'.$extension;
|
|
|
|
}
|
2012-02-27 23:59:41 +04:00
|
|
|
}
|
2013-02-12 02:55:44 +04:00
|
|
|
$files[] = basename($name);
|
2012-02-29 04:15:49 +04:00
|
|
|
// Cache entry for future use
|
|
|
|
$this->entries[$name] = $entry;
|
2012-02-27 23:59:41 +04:00
|
|
|
}
|
2012-02-29 04:15:49 +04:00
|
|
|
}
|
2013-01-28 18:34:15 +04:00
|
|
|
\OC\Files\Stream\Dir::register('google'.$path, $files);
|
2012-06-27 20:04:11 +04:00
|
|
|
return opendir('fakedir://google'.$path);
|
2012-02-27 23:59:41 +04:00
|
|
|
}
|
|
|
|
|
2012-02-29 04:15:49 +04:00
|
|
|
public function stat($path) {
|
|
|
|
if ($path == '' || $path == '/') {
|
|
|
|
$stat['size'] = $this->free_space($path);
|
|
|
|
$stat['atime'] = time();
|
|
|
|
$stat['mtime'] = time();
|
|
|
|
$stat['ctime'] = time();
|
2012-11-30 19:27:11 +04:00
|
|
|
} else {
|
|
|
|
$entry = $this->getResource($path);
|
|
|
|
if ($entry) {
|
|
|
|
// NOTE: Native resources don't have a file size
|
|
|
|
$stat['size'] = $entry->getElementsByTagNameNS('http://schemas.google.com/g/2005',
|
|
|
|
'quotaBytesUsed')->item(0)->nodeValue;
|
|
|
|
//if (isset($atime = $entry->getElementsByTagNameNS('http://schemas.google.com/g/2005',
|
|
|
|
// 'lastViewed')->item(0)->nodeValue))
|
|
|
|
//$stat['atime'] = strtotime($entry->getElementsByTagNameNS('http://schemas.google.com/g/2005',
|
|
|
|
// 'lastViewed')->item(0)->nodeValue);
|
|
|
|
$stat['mtime'] = strtotime($entry->getElementsByTagName('updated')->item(0)->nodeValue);
|
|
|
|
}
|
2012-02-29 04:15:49 +04:00
|
|
|
}
|
2012-03-24 22:49:44 +04:00
|
|
|
if (isset($stat)) {
|
|
|
|
return $stat;
|
|
|
|
}
|
|
|
|
return false;
|
2012-02-29 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function filetype($path) {
|
|
|
|
if ($path == '' || $path == '/') {
|
|
|
|
return 'dir';
|
2012-11-30 19:27:11 +04:00
|
|
|
} else {
|
|
|
|
$entry = $this->getResource($path);
|
|
|
|
if ($entry) {
|
|
|
|
$categories = $entry->getElementsByTagName('category');
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
if ($category->getAttribute('scheme') == 'http://schemas.google.com/g/2005#kind') {
|
|
|
|
$type = $category->getAttribute('label');
|
|
|
|
if (strlen(strstr($type, 'folder')) > 0) {
|
|
|
|
return 'dir';
|
|
|
|
} else {
|
|
|
|
return 'file';
|
|
|
|
}
|
2012-02-27 23:59:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-25 01:42:07 +04:00
|
|
|
public function isReadable($path) {
|
2012-02-27 23:59:41 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-25 01:42:07 +04:00
|
|
|
public function isUpdatable($path) {
|
2012-02-29 04:15:49 +04:00
|
|
|
if ($path == '' || $path == '/') {
|
|
|
|
return true;
|
2012-11-30 19:27:11 +04:00
|
|
|
} else {
|
|
|
|
$entry = $this->getResource($path);
|
|
|
|
if ($entry) {
|
|
|
|
// Check if edit or edit-media links exist
|
|
|
|
$links = $entry->getElementsByTagName('link');
|
|
|
|
foreach ($links as $link) {
|
|
|
|
if ($link->getAttribute('rel') == 'edit') {
|
|
|
|
return true;
|
|
|
|
} else if ($link->getAttribute('rel') == 'edit-media') {
|
|
|
|
return true;
|
|
|
|
}
|
2012-02-27 23:59:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2012-02-27 23:59:41 +04:00
|
|
|
public function file_exists($path) {
|
2012-02-29 04:15:49 +04:00
|
|
|
if ($path == '' || $path == '/') {
|
|
|
|
return true;
|
|
|
|
} else if ($this->getResource($path)) {
|
2012-02-27 23:59:41 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2012-02-27 23:59:41 +04:00
|
|
|
public function unlink($path) {
|
|
|
|
// Get resource self link to trash resource
|
2012-11-30 19:27:11 +04:00
|
|
|
$entry = $this->getResource($path);
|
|
|
|
if ($entry) {
|
2012-02-27 23:59:41 +04:00
|
|
|
$links = $entry->getElementsByTagName('link');
|
|
|
|
foreach ($links as $link) {
|
|
|
|
if ($link->getAttribute('rel') == 'self') {
|
2012-05-02 22:38:22 +04:00
|
|
|
$uri = $link->getAttribute('href');
|
|
|
|
break;
|
2012-02-27 23:59:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-02 22:38:22 +04:00
|
|
|
if (isset($uri)) {
|
|
|
|
$this->sendRequest($uri, 'DELETE');
|
2012-02-27 23:59:41 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-29 04:15:49 +04:00
|
|
|
public function rename($path1, $path2) {
|
2012-11-30 19:27:11 +04:00
|
|
|
$entry = $this->getResource($path1);
|
|
|
|
if ($entry) {
|
2012-05-02 22:38:22 +04:00
|
|
|
$collection = dirname($path2);
|
|
|
|
if (dirname($path1) == $collection) {
|
|
|
|
// Get resource edit link to rename resource
|
|
|
|
$etag = $entry->getAttribute('gd:etag');
|
|
|
|
$links = $entry->getElementsByTagName('link');
|
|
|
|
foreach ($links as $link) {
|
|
|
|
if ($link->getAttribute('rel') == 'edit') {
|
|
|
|
$uri = $link->getAttribute('href');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-07-20 18:47:26 +04:00
|
|
|
$title = basename($path2);
|
2012-05-02 22:38:22 +04:00
|
|
|
// Construct post data
|
|
|
|
$postData = '<?xml version="1.0" encoding="UTF-8"?>';
|
2012-11-30 19:27:11 +04:00
|
|
|
$postData .= '<entry xmlns="http://www.w3.org/2005/Atom"';
|
|
|
|
$postData .= ' xmlns:docs="http://schemas.google.com/docs/2007"';
|
|
|
|
$postData .= ' xmlns:gd="http://schemas.google.com/g/2005"';
|
|
|
|
$postData .= ' gd:etag='.$etag.'>';
|
2012-05-02 22:38:22 +04:00
|
|
|
$postData .= '<title>'.$title.'</title>';
|
|
|
|
$postData .= '</entry>';
|
|
|
|
$this->sendRequest($uri, 'PUT', $postData);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Move to different collection
|
2012-11-30 19:27:11 +04:00
|
|
|
$collectionEntry = $this->getResource($collection);
|
|
|
|
if ($collectionEntry) {
|
2012-07-20 18:47:26 +04:00
|
|
|
$feedUri = $collectionEntry->getElementsByTagName('content')->item(0)->getAttribute('src');
|
2012-05-02 22:38:22 +04:00
|
|
|
// Construct post data
|
|
|
|
$postData = '<?xml version="1.0" encoding="UTF-8"?>';
|
|
|
|
$postData .= '<entry xmlns="http://www.w3.org/2005/Atom">';
|
|
|
|
$postData .= '<id>'.$entry->getElementsByTagName('id')->item(0).'</id>';
|
|
|
|
$postData .= '</entry>';
|
2012-07-20 18:47:26 +04:00
|
|
|
$this->sendRequest($feedUri, 'POST', $postData);
|
2012-05-02 22:38:22 +04:00
|
|
|
return true;
|
2012-02-29 04:15:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2012-02-27 23:59:41 +04:00
|
|
|
}
|
|
|
|
|
2012-02-29 04:15:49 +04:00
|
|
|
public function fopen($path, $mode) {
|
2012-05-02 22:38:22 +04:00
|
|
|
switch ($mode) {
|
|
|
|
case 'r':
|
|
|
|
case 'rb':
|
2012-11-30 19:27:11 +04:00
|
|
|
$entry = $this->getResource($path);
|
|
|
|
if ($entry) {
|
2012-03-24 22:49:44 +04:00
|
|
|
$extension = $this->getExtension($entry);
|
|
|
|
$downloadUri = $entry->getElementsByTagName('content')->item(0)->getAttribute('src');
|
|
|
|
// TODO Non-native documents don't need these additional parameters
|
|
|
|
$downloadUri .= '&exportFormat='.$extension.'&format='.$extension;
|
2012-05-02 22:38:22 +04:00
|
|
|
$tmpFile = $this->sendRequest($downloadUri, 'GET', null, null, true);
|
2012-03-24 22:49:44 +04:00
|
|
|
return fopen($tmpFile, 'r');
|
2012-05-02 22:38:22 +04:00
|
|
|
}
|
|
|
|
case 'w':
|
|
|
|
case 'wb':
|
|
|
|
case 'a':
|
|
|
|
case 'ab':
|
|
|
|
case 'r+':
|
|
|
|
case 'w+':
|
|
|
|
case 'wb+':
|
|
|
|
case 'a+':
|
|
|
|
case 'x':
|
|
|
|
case 'x+':
|
|
|
|
case 'c':
|
|
|
|
case 'c+':
|
2012-11-04 14:10:46 +04:00
|
|
|
if (strrpos($path, '.') !== false) {
|
|
|
|
$ext = substr($path, strrpos($path, '.'));
|
2012-05-02 22:38:22 +04:00
|
|
|
} else {
|
|
|
|
$ext = '';
|
|
|
|
}
|
2012-09-07 20:30:48 +04:00
|
|
|
$tmpFile = \OC_Helper::tmpFile($ext);
|
2013-01-28 18:34:15 +04:00
|
|
|
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
|
2012-05-02 22:38:22 +04:00
|
|
|
if ($this->file_exists($path)) {
|
|
|
|
$source = $this->fopen($path, 'r');
|
|
|
|
file_put_contents($tmpFile, $source);
|
|
|
|
}
|
|
|
|
self::$tempFiles[$tmpFile] = $path;
|
|
|
|
return fopen('close://'.$tmpFile, $mode);
|
2012-02-29 04:15:49 +04:00
|
|
|
}
|
2012-03-24 22:49:44 +04:00
|
|
|
return false;
|
2012-02-27 23:59:41 +04:00
|
|
|
}
|
|
|
|
|
2012-05-02 22:38:22 +04:00
|
|
|
public function writeBack($tmpFile) {
|
|
|
|
if (isset(self::$tempFiles[$tmpFile])) {
|
|
|
|
$this->uploadFile($tmpFile, self::$tempFiles[$tmpFile]);
|
|
|
|
unlink($tmpFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function uploadFile($path, $target) {
|
|
|
|
$entry = $this->getResource($target);
|
2012-11-30 19:27:11 +04:00
|
|
|
if ( ! $entry) {
|
2012-05-02 22:38:22 +04:00
|
|
|
if (dirname($target) == '.' || dirname($target) == '/') {
|
2012-11-30 19:27:11 +04:00
|
|
|
$uploadUri = self::BASE_URI.'/upload/create-session/default/private/full/folder%3Aroot/contents';
|
2012-05-02 22:38:22 +04:00
|
|
|
} else {
|
|
|
|
$entry = $this->getResource(dirname($target));
|
|
|
|
}
|
|
|
|
}
|
2012-11-30 19:27:11 +04:00
|
|
|
if ( ! isset($uploadUri) && $entry) {
|
2012-05-02 22:38:22 +04:00
|
|
|
$links = $entry->getElementsByTagName('link');
|
|
|
|
foreach ($links as $link) {
|
2012-06-15 04:56:48 +04:00
|
|
|
if ($link->getAttribute('rel') == 'http://schemas.google.com/g/2005#resumable-create-media') {
|
2012-05-02 22:38:22 +04:00
|
|
|
$uploadUri = $link->getAttribute('href');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($uploadUri) && $handle = fopen($path, 'r')) {
|
|
|
|
$uploadUri .= '?convert=false';
|
2012-09-07 20:30:48 +04:00
|
|
|
$mimetype = \OC_Helper::getMimeType($path);
|
2012-05-02 22:38:22 +04:00
|
|
|
$size = filesize($path);
|
|
|
|
$headers = array('X-Upload-Content-Type: ' => $mimetype, 'X-Upload-Content-Length: ' => $size);
|
|
|
|
$postData = '<?xml version="1.0" encoding="UTF-8"?>';
|
|
|
|
$postData .= '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:docs="http://schemas.google.com/docs/2007">';
|
|
|
|
$postData .= '<title>'.basename($target).'</title>';
|
|
|
|
$postData .= '</entry>';
|
|
|
|
$result = $this->sendRequest($uploadUri, 'POST', $postData, $headers, false, true);
|
|
|
|
if ($result) {
|
|
|
|
// Get location to upload file
|
|
|
|
if (preg_match('@^Location: (.*)$@m', $result, $matches)) {
|
|
|
|
$uploadUri = trim($matches[1]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 512 kB chunks
|
|
|
|
$chunkSize = 524288;
|
|
|
|
$i = 0;
|
|
|
|
while (!feof($handle)) {
|
|
|
|
if ($i + $chunkSize > $size) {
|
|
|
|
if ($i == 0) {
|
|
|
|
$chunkSize = $size;
|
|
|
|
} else {
|
|
|
|
$chunkSize = $size % $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$end = $i + $chunkSize - 1;
|
2012-11-30 19:27:11 +04:00
|
|
|
$headers = array('Content-Length: '.$chunkSize,
|
|
|
|
'Content-Type: '.$mimetype,
|
|
|
|
'Content-Range: bytes '.$i.'-'.$end.'/'.$size);
|
2012-05-02 22:38:22 +04:00
|
|
|
$postData = fread($handle, $chunkSize);
|
2012-06-15 04:56:48 +04:00
|
|
|
$result = $this->sendRequest($uploadUri, 'PUT', $postData, $headers, false, true, false, true);
|
|
|
|
if ($result['code'] == '308') {
|
|
|
|
if (preg_match('@^Location: (.*)$@m', $result['result'], $matches)) {
|
|
|
|
// Get next location to upload file chunk
|
2012-05-02 22:38:22 +04:00
|
|
|
$uploadUri = trim($matches[1]);
|
|
|
|
}
|
|
|
|
$i += $chunkSize;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO Wait for resource entry
|
|
|
|
}
|
|
|
|
}
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2012-02-29 04:15:49 +04:00
|
|
|
public function getMimeType($path, $entry = null) {
|
2012-11-30 19:27:11 +04:00
|
|
|
// Entry can be passed, because extension is required for opendir
|
|
|
|
// and the entry can't be cached without the extension
|
2012-02-29 04:15:49 +04:00
|
|
|
if ($entry == null) {
|
|
|
|
if ($path == '' || $path == '/') {
|
|
|
|
return 'httpd/unix-directory';
|
|
|
|
} else {
|
|
|
|
$entry = $this->getResource($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($entry) {
|
|
|
|
$mimetype = $entry->getElementsByTagName('content')->item(0)->getAttribute('type');
|
2012-11-30 19:27:11 +04:00
|
|
|
// Native Google Docs resources often default to text/html,
|
|
|
|
// but it may be more useful to default to a corresponding ODF mimetype
|
|
|
|
// Collections get reported as application/atom+xml,
|
|
|
|
// make sure it actually is a folder and fix the mimetype
|
2012-03-24 22:49:44 +04:00
|
|
|
if ($mimetype == 'text/html' || $mimetype == 'application/atom+xml;type=feed') {
|
2012-02-29 04:15:49 +04:00
|
|
|
$categories = $entry->getElementsByTagName('category');
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
if ($category->getAttribute('scheme') == 'http://schemas.google.com/g/2005#kind') {
|
|
|
|
$type = $category->getAttribute('label');
|
|
|
|
if (strlen(strstr($type, 'folder')) > 0) {
|
|
|
|
return 'httpd/unix-directory';
|
|
|
|
} else if (strlen(strstr($type, 'document')) > 0) {
|
|
|
|
return 'application/vnd.oasis.opendocument.text';
|
|
|
|
} else if (strlen(strstr($type, 'spreadsheet')) > 0) {
|
|
|
|
return 'application/vnd.oasis.opendocument.spreadsheet';
|
|
|
|
} else if (strlen(strstr($type, 'presentation')) > 0) {
|
|
|
|
return 'application/vnd.oasis.opendocument.presentation';
|
|
|
|
} else if (strlen(strstr($type, 'drawing')) > 0) {
|
|
|
|
return 'application/vnd.oasis.opendocument.graphics';
|
|
|
|
} else {
|
2012-11-30 19:27:11 +04:00
|
|
|
// If nothing matches return text/html,
|
|
|
|
// all native Google Docs resources can be exported as text/html
|
2012-02-29 04:15:49 +04:00
|
|
|
return 'text/html';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $mimetype;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2012-02-27 23:59:41 +04:00
|
|
|
public function free_space($path) {
|
2012-11-30 19:27:11 +04:00
|
|
|
$dom = $this->getFeed(self::BASE_URI.'/metadata/default', 'GET');
|
|
|
|
if ($dom) {
|
2012-02-29 04:15:49 +04:00
|
|
|
// NOTE: Native Google Docs resources don't count towards quota
|
2012-11-30 19:27:11 +04:00
|
|
|
$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;
|
2012-02-27 23:59:41 +04:00
|
|
|
return $total - $used;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2012-03-24 22:49:44 +04:00
|
|
|
public function touch($path, $mtime = null) {
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2012-02-27 23:59:41 +04:00
|
|
|
}
|
2012-02-29 04:15:49 +04:00
|
|
|
|
2012-12-28 21:00:48 +04:00
|
|
|
public function test() {
|
|
|
|
if ($this->free_space('')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-12 01:06:57 +04:00
|
|
|
}
|