MDL-28557 Calendar filters: show all group events in course to teacher
[moodle.git] / repository / dropbox / locallib.php
bloba86899c41a3afc3d3bbf832f9f84758932eb54d9
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * dropbox class
20 * A helper class to access dropbox resources
22 * @since 2.0
23 * @package repository
24 * @subpackage dropbox
25 * @copyright 2010 Dongsheng Cai
26 * @author Dongsheng Cai <dongsheng@moodle.com>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
32 require_once($CFG->libdir.'/oauthlib.php');
34 class dropbox extends oauth_helper {
35 /** dropbox access type, can be dropbox or sandbox */
36 private $mode = 'dropbox';
37 /** dropbox api url*/
38 private $dropbox_api = 'https://api.dropbox.com/1';
39 /** dropbox content api url*/
40 private $dropbox_content_api = 'https://api-content.dropbox.com/1';
42 function __construct($args) {
43 parent::__construct($args);
45 /**
46 * Get file listing from dropbox
48 public function get_listing($path='/', $token='', $secret='') {
49 $url = $this->dropbox_api.'/metadata/'.$this->mode.$path;
50 $content = $this->get($url, array(), $token, $secret);
51 $data = json_decode($content);
52 return $data;
55 /**
56 * Download a file
58 public function get_file($filepath, $saveas = '') {
59 $info = pathinfo($filepath);
60 $dirname = $info['dirname'];
61 $basename = $info['basename'];
62 $filepath = $dirname . rawurlencode($basename);
63 if ($dirname != '/') {
64 $filepath = $dirname . '/' . $basename;
65 $filepath = str_replace("%2F", "/", rawurlencode($filepath));
68 $url = $this->dropbox_content_api.'/files/'.$this->mode.$filepath;
69 $content = $this->get($url, array());
70 file_put_contents($saveas, $content);
71 return array('path'=>$saveas, 'url'=>$url);
74 public function set_mode($mode) {
75 $this->mode = $mode;