MDL-62388 tool_dataprivacy: Fixing bs4stable table layout
[moodle.git] / lib / boxlib.php
blob5e03c41354bb5994450185d016e1241cbacf419e
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Box.net client.
20 * @package core
21 * @author James Levy <james@box.net>
22 * @link http://enabled.box.net
23 * @access public
24 * @version 1.0
25 * @copyright copyright Box.net 2007
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->libdir . '/oauthlib.php');
32 /**
33 * Box.net client class.
35 * @package core
36 * @copyright 2013 Frédéric Massart
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class boxnet_client extends oauth2_client {
41 /** @const API URL */
42 const API = 'https://api.box.com/2.0';
44 /** @const UPLOAD_API URL */
45 const UPLOAD_API = 'https://upload.box.com/api/2.0';
47 /**
48 * Return authorize URL.
50 * @return string
52 protected function auth_url() {
53 return 'https://www.box.com/api/oauth2/authorize';
56 /**
57 * Create a folder.
59 * @param string $foldername The folder name.
60 * @param int $parentid The ID of the parent folder.
61 * @return array Information about the new folder.
63 public function create_folder($foldername, $parentid = 0) {
64 $params = array('name' => $foldername, 'parent' => array('id' => (string) $parentid));
65 $this->reset_state();
66 $result = $this->post($this->make_url("/folders"), json_encode($params));
67 $result = json_decode($result);
68 return $result;
71 /**
72 * Download the file.
74 * @param int $fileid File ID.
75 * @param string $path Path to download the file to.
76 * @return bool Success or not.
78 public function download_file($fileid, $path) {
79 $this->reset_state();
80 $result = $this->download_one($this->make_url("/files/$fileid/content"), array(),
81 array('filepath' => $path, 'CURLOPT_FOLLOWLOCATION' => true));
82 return ($result === true && $this->info['http_code'] === 200);
85 /**
86 * Get info of a file.
88 * @param int $fileid File ID.
89 * @return object
91 public function get_file_info($fileid) {
92 $this->reset_state();
93 $result = $this->request($this->make_url("/files/$fileid"));
94 return json_decode($result);
97 /**
98 * Get a folder content.
100 * @param int $folderid Folder ID.
101 * @return object
103 public function get_folder_items($folderid = 0) {
104 $this->reset_state();
105 $result = $this->request($this->make_url("/folders/$folderid/items",
106 array('fields' => 'id,name,type,modified_at,size,owned_by')));
107 return json_decode($result);
111 * Log out.
113 * @return void
115 public function log_out() {
116 if ($accesstoken = $this->get_accesstoken()) {
117 $params = array(
118 'client_id' => $this->get_clientid(),
119 'client_secret' => $this->get_clientsecret(),
120 'token' => $accesstoken->token
122 $this->reset_state();
123 $this->post($this->revoke_url(), $params);
125 parent::log_out();
129 * Build a request URL.
131 * @param string $uri The URI to request.
132 * @param array $params Query string parameters.
133 * @param bool $uploadapi Whether this works with the upload API or not.
134 * @return string
136 protected function make_url($uri, $params = array(), $uploadapi = false) {
137 $api = $uploadapi ? self::UPLOAD_API : self::API;
138 $url = new moodle_url($api . '/' . ltrim($uri, '/'), $params);
139 return $url->out(false);
143 * Rename a file.
145 * @param int $fileid The file ID.
146 * @param string $newname The new file name.
147 * @return object Box.net file object.
149 public function rename_file($fileid, $newname) {
150 // This requires a PUT request with data within it. We cannot use
151 // the standard PUT request 'CURLOPT_PUT' because it expects a file.
152 $data = array('name' => $newname);
153 $options = array(
154 'CURLOPT_CUSTOMREQUEST' => 'PUT',
155 'CURLOPT_POSTFIELDS' => json_encode($data)
157 $url = $this->make_url("/files/$fileid");
158 $this->reset_state();
159 $result = $this->request($url, $options);
160 $result = json_decode($result);
161 return $result;
165 * Resets curl for multiple requests.
167 * @return void
169 public function reset_state() {
170 $this->cleanopt();
171 $this->resetHeader();
175 * Return the revoke URL.
177 * @return string
179 protected function revoke_url() {
180 return 'https://www.box.com/api/oauth2/revoke';
184 * Share a file and return the link to it.
186 * @param string $fileid The file ID.
187 * @param bool $businesscheck Whether or not to check if the user can share files, has a business account.
188 * @return object
190 public function share_file($fileid, $businesscheck = true) {
191 // Sharing the file, this requires a PUT request with data within it. We cannot use
192 // the standard PUT request 'CURLOPT_PUT' because it expects a file.
193 $data = array('shared_link' => array('access' => 'open', 'permissions' =>
194 array('can_download' => true, 'can_preview' => true)));
195 $options = array(
196 'CURLOPT_CUSTOMREQUEST' => 'PUT',
197 'CURLOPT_POSTFIELDS' => json_encode($data)
199 $this->reset_state();
200 $result = $this->request($this->make_url("/files/$fileid"), $options);
201 $result = json_decode($result);
203 if ($businesscheck) {
204 // Checks that the user has the right to share the file. If not, throw an exception.
205 $this->reset_state();
206 $this->head($result->shared_link->download_url);
207 $info = $this->get_info();
208 if ($info['http_code'] == 403) {
209 throw new moodle_exception('No permission to share the file');
213 return $result->shared_link;
217 * Search.
219 * @return object
221 public function search($query) {
222 $this->reset_state();
223 $result = $this->request($this->make_url('/search', array('query' => $query, 'limit' => 50, 'offset' => 0)));
224 return json_decode($result);
228 * Return token URL.
230 * @return string
232 protected function token_url() {
233 return 'https://www.box.com/api/oauth2/token';
237 * Upload a file.
239 * Please note that the file is named on Box.net using the path we are providing, and so
240 * the file has the name of the stored_file hash.
242 * @param stored_file $storedfile A stored_file.
243 * @param integer $parentid The ID of the parent folder.
244 * @return object Box.net file object.
246 public function upload_file(stored_file $storedfile, $parentid = 0) {
247 $url = $this->make_url('/files/content', array(), true);
248 $options = array(
249 'filename' => $storedfile,
250 'parent_id' => $parentid
252 $this->reset_state();
253 $result = $this->post($url, $options);
254 $result = json_decode($result);
255 return $result;
261 * @deprecated since 2.6, 2.5.3, 2.4.7
263 class boxclient {
264 public function __construct() {
265 throw new coding_exception(__CLASS__ . ' has been removed. Please update your code to use boxnet_client.',
266 DEBUG_DEVELOPER);