2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * A helper class to access dropbox resources
21 * @package repository_dropbox
22 * @copyright 2012 Marina Glancy
23 * @copyright 2010 Dongsheng Cai
24 * @author Dongsheng Cai <dongsheng@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->libdir
.'/oauthlib.php');
32 * Authentication class to access Dropbox API
34 * @package repository_dropbox
35 * @copyright 2010 Dongsheng Cai
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class dropbox
extends oauth_helper
{
39 /** @var string dropbox access type, can be dropbox or sandbox */
40 private $mode = 'dropbox';
41 /** @var string dropbox api url*/
42 private $dropbox_api = 'https://api.dropbox.com/1';
43 /** @var string dropbox content api url*/
44 private $dropbox_content_api = 'https://api-content.dropbox.com/1';
47 * Constructor for dropbox class
51 function __construct($args) {
52 parent
::__construct($args);
56 * Get file listing from dropbox
59 * @param string $token
60 * @param string $secret
63 public function get_listing($path='/', $token='', $secret='') {
64 $url = $this->dropbox_api
.'/metadata/'.$this->mode
.$path;
65 $content = $this->get($url, array(), $token, $secret);
66 $data = json_decode($content);
71 * Prepares the filename to pass to Dropbox API as part of URL
73 * @param string $filepath
76 protected function prepare_filepath($filepath) {
77 $info = pathinfo($filepath);
78 $dirname = $info['dirname'];
79 $basename = $info['basename'];
80 $filepath = $dirname . rawurlencode($basename);
81 if ($dirname != '/') {
82 $filepath = $dirname . '/' . $basename;
83 $filepath = str_replace("%2F", "/", rawurlencode($filepath));
89 * Retrieves the default (64x64) thumbnail for dropbox file
91 * @throws moodle_exception when file could not be downloaded
93 * @param string $filepath local path in Dropbox
94 * @param string $saveas path to file to save the result
95 * @param int $timeout request timeout in seconds, 0 means no timeout
96 * @return array with attributes 'path' and 'url'
98 public function get_thumbnail($filepath, $saveas, $timeout = 0) {
99 $url = $this->dropbox_content_api
.'/thumbnails/'.$this->mode
.$this->prepare_filepath($filepath);
100 if (!($fp = fopen($saveas, 'w'))) {
101 throw new moodle_exception('cannotwritefile', 'error', '', $saveas);
103 $this->setup_oauth_http_options(array('timeout' => $timeout, 'file' => $fp, 'BINARYTRANSFER' => true));
104 $result = $this->get($url);
106 if ($result === true) {
107 return array('path'=>$saveas, 'url'=>$url);
110 throw new moodle_exception('errorwhiledownload', 'repository', '', $result);
115 * Downloads a file from Dropbox and saves it locally
117 * @throws moodle_exception when file could not be downloaded
119 * @param string $filepath local path in Dropbox
120 * @param string $saveas path to file to save the result
121 * @param int $timeout request timeout in seconds, 0 means no timeout
122 * @return array with attributes 'path' and 'url'
124 public function get_file($filepath, $saveas, $timeout = 0) {
125 $url = $this->dropbox_content_api
.'/files/'.$this->mode
.$this->prepare_filepath($filepath);
126 if (!($fp = fopen($saveas, 'w'))) {
127 throw new moodle_exception('cannotwritefile', 'error', '', $saveas);
129 $this->setup_oauth_http_options(array('timeout' => $timeout, 'file' => $fp, 'BINARYTRANSFER' => true));
130 $result = $this->get($url);
132 if ($result === true) {
133 return array('path'=>$saveas, 'url'=>$url);
136 throw new moodle_exception('errorwhiledownload', 'repository', '', $result);
141 * Returns direct link to Dropbox file
143 * @param string $filepath local path in Dropbox
144 * @param int $timeout request timeout in seconds, 0 means no timeout
145 * @return string|null information object or null if request failed with an error
147 public function get_file_share_link($filepath, $timeout = 0) {
148 $url = $this->dropbox_api
.'/shares/'.$this->mode
.$this->prepare_filepath($filepath);
149 $this->setup_oauth_http_options(array('timeout' => $timeout));
150 $result = $this->post($url, array('short_url'=>0));
151 if (!$this->http
->get_errno()) {
152 $data = json_decode($result);
153 if (isset($data->url
)) {
161 * Sets Dropbox API mode (dropbox or sandbox, default dropbox)
163 * @param string $mode
165 public function set_mode($mode) {