MDL-15841 Removed the default safe overrides for new installs (MDL-8521)
[moodle.git] / repository / boxnet / boxlibphp5.php
CommitLineData
d710e100 1<?php
2/**
3 * Box REST Client Library for PHP5 Developers
aa754fe3 4 *
5 *
d710e100 6 * @author James Levy <james@box.net>
7 * @link http://enabled.box.net
8 * @access public
9 * @version 1.0
aa754fe3 10 * copyright Box.net 2007
d710e100 11 * Available for use and distribution under GPL-license
12 * Go to http://www.gnu.org/licenses/gpl-3.0.txt for full text
13 */
14
15/**
16 * Modified by Dongsheng Cai <dongsheng@cvs.moodle.org>
17 *
18 */
19
d710e100 20class boxclient {
34f210f6 21 public $auth_token = '';
d710e100 22
34f210f6 23 private $_box_api_url = 'http://www.box.net/api/1.0/rest';
24 private $_box_api_upload_url = 'http://upload.box.net/api/1.0/upload';
25 private $_error_code = '';
26 private $_error_msg = '';
aa754fe3 27
a9493cbe 28 public function __construct($api_key, $auth_token = '') {
d710e100 29 $this->api_key = $api_key;
a9493cbe 30 $this->auth_token = $auth_token;
d710e100 31 }
32 // Setup for Functions
33 function makeRequest($method, $params = array()) {
34 $this->_clearErrors();
a9493cbe 35 $c = new curl(array('cache'=>true));
d710e100 36 if ($method == 'upload'){
a9493cbe 37 $request = $this->_box_api_upload_url.'/'.
38 $this->auth_token.'/'.$params['folder_id'];
1afc87ad 39 $xml = $c->post($request, $params);
d710e100 40 }else{
41 $args = array();
34f210f6 42 $xml = $c->get($this->_box_api_url, $params);
d710e100 43 }
d710e100 44 $xml_parser = xml_parser_create();
45 xml_parse_into_struct($xml_parser, $xml, $data);
46 xml_parser_free($xml_parser);
47 return $data;
48 }
49 function getTicket($params = array()) {
34f210f6 50 $params['api_key'] = $this->api_key;
51 $params['action'] = 'get_ticket';
d710e100 52 $ret_array = array();
53 $data = $this->makeRequest('action=get_ticket', $params);
54 if ($this->_checkForError($data)) {
55 return false;
56 }
57 foreach ($data as $a) {
58 switch ($a['tag']) {
a9493cbe 59 case 'STATUS':
60 $ret_array['status'] = $a['value'];
61 break;
62 case 'TICKET':
63 $ret_array['ticket'] = $a['value'];
64 break;
d710e100 65 }
66 }
d710e100 67 return $ret_array;
68 }
aa754fe3 69
34f210f6 70 // $options['username'] and $options['password'] must be
71 // given, we will use them to obtain a valid auth_token
72 // To get a token, you should use following code:
73 //
74 // $box = new boxclient('dmls97d8j3i9tn7av8y71m9eb55vrtj4');
75 // Get a ticket
76 // $t = $box->getTicket();
77 // $box->getAuthToken($t['ticket'], array(
78 // 'username'=>'dongsheng@moodle.com',
79 // 'password'=>'xxx'));
80 //
bb2c046d 81 function getAuthToken($ticket, $username, $password) {
34f210f6 82 $c = new curl;
83 $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>0));
84 $param = array(
85 'login_form1'=>'',
bb2c046d 86 'login'=>$username,
87 'password'=>$password,
34f210f6 88 'dologin'=>1,
89 '__login'=>1
90 );
91 $ret = $c->post('http://www.box.net/api/1.0/auth/'.$ticket, $param);
92 $header = $c->getResponse();
1afc87ad 93 if(empty($header['location'])) {
bf1fccf0 94 throw new repository_exception('invalidpassword', 'repository');
1afc87ad 95 }
34f210f6 96 $location = $header['location'];
97 preg_match('#auth_token=(.*)$#i', $location, $matches);
98 $auth_token = $matches[1];
99 if(!empty($auth_token)) {
100 $this->auth_token = $auth_token;
0eb58cf4 101 return $auth_token;
34f210f6 102 } else {
bf1fccf0 103 throw new repository_exception('invalidtoken', 'repository');
aa754fe3 104 }
d710e100 105 }
aa754fe3 106
34f210f6 107 // Get the file list
d710e100 108 function getAccountTree($params = array()) {
34f210f6 109 $params['auth_token'] = $this->auth_token;
110 $params['folder_id'] = 0;
111 $params['api_key'] = $this->api_key;
112 $params['action'] = 'get_account_tree';
113 $params['onelevel'] = 1;
114 $params['params[]'] = 'nozip';
d710e100 115 $ret_array = array();
116 $data = $this->makeRequest('action=get_account_tree', $params);
117 if ($this->_checkForError($data)) {
118 return false;
119 }
120 $tree_count=count($data);
121 global $tree_count;
122 $entry_count = 0;
123 for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) {
124 $a = $data[$i];
aa754fe3 125 switch ($a['tag'])
d710e100 126 {
a9493cbe 127 case 'FOLDER':
128 if (@is_array($a['attributes'])) {
129 $ret_array['folder_id'][$i] = $a['attributes']['ID'];
130 $ret_array['folder_name'][$i] = $a['attributes']['NAME'];
131 $ret_array['shared'][$i] = $a['attributes']['SHARED'];
132 }
133 break;
134
135 case 'FILE':
136 if (@is_array($a['attributes'])) {
137 $ret_array['file_id'][$i] = $a['attributes']['ID'];
138 @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
139 @$ret_array['file_keyword'][$i] = $a['attributes']['KEYWORD'];
284cf75f 140 @$ret_array['file_size'][$i] = display_size($a['attributes']['SIZE']);
141 @$ret_array['file_date'][$i] = userdate($a['attributes']['UPDATED']);
a9493cbe 142 $entry_count++;
143 }
144 break;
d710e100 145 }
146 }
d710e100 147 return $ret_array;
148 }
34f210f6 149
a9493cbe 150 // Create New Folder
151 function CreateFolder($new_folder_name, $params = array()) {
34f210f6 152 $params['auth_token'] = $this->auth_token;
a9493cbe 153 $params['api_key'] = $this->api_key;
34f210f6 154 $params['action'] = 'create_folder';
155 //Set to '0' by default. Change to create within sub-folder.
156 $params['parent_id'] = 0;
a9493cbe 157 $params['name'] = $new_folder_name;
34f210f6 158 //Set to '1' by default. Set to '0' to make folder private.
159 $params['share'] = 1;
aa754fe3 160
a9493cbe 161 $ret_array = array();
162 $data = $this->makeRequest('action=create_folder', $params);
aa754fe3 163
164
a9493cbe 165 if ($this->_checkForError($data)) {
166 return false;
167 }
168 foreach ($data as $a) {
169 switch ($a['tag']) {
170 case 'FOLDER_ID':
171 $ret_array['folder_id'] = $a['value'];
172 break;
173
174 case 'FOLDER_NAME':
175 $ret_array['folder_type'] = $a['value'];
176 break;
177
178 case 'SHARED':
179 $ret_array['shared'] = $a['value'];
180 break;
181 case 'PASSWORD':
182 $ret_array['password'] = $a['value'];
183 break;
d710e100 184 }
a9493cbe 185 }
186 return $ret_array;
aa754fe3 187 }
a9493cbe 188 // Upload File
189 function UploadFile ($params = array()) {
190 $params['auth_token'] = $this->auth_token;
191 // this param should be the full path of the file
192 $params['new_file1'] = '@'.$params['file'];
b3fac92f 193 $defaults = array(
194 'folder_id' => 0, //Set to '0' by default. Change to create within sub-folder.
195 'share' => 1, //Set to '1' by default. Set to '0' to make folder private.
196 );
197 foreach ($defaults as $key => $value) {
198 if (!array_key_exists($key, $params)) {
199 $params[$key] = $value;
200 }
201 }
a9493cbe 202 $ret_array = array();
1afc87ad 203 $entry_count = 0;
a9493cbe 204 $data = $this->makeRequest('upload', $params);
205 if ($this->_checkForError($data)) {
206 return false;
207 }
208 for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) {
209 $a = $data[$i];
210 switch ($a['tag']) {
211 case 'STATUS':
212 $ret_array['status'] = $a['value'];
213 break;
214
215 case 'FILE':
216 if (is_array($a['attributes'])) {
1afc87ad 217 @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
218 @$ret_array['id'][$i] = $a['attributes']['ID'];
219 @$ret_array['folder_name'][$i] = $a['attributes']['FOLDER_NAME'];
220 @$ret_array['error'][$i] = $a['attributes']['ERROR'];
221 @$ret_array['public_name'][$i] = $a['attributes']['PUBLIC_NAME'];
a9493cbe 222 $entry_count++;
223 }
224 break;
d710e100 225 }
226 }
aa754fe3 227
a9493cbe 228 return $ret_array;
aa754fe3 229 }
aa754fe3 230
a9493cbe 231 // Register New User
232 function RegisterUser($params = array()) {
34f210f6 233 $params['api_key'] = $this->api_key;
234 $params['action'] = 'register_new_user';
235 $params['login'] = $_REQUEST['login'];
236 $params['password'] = $_REQUEST['password'];
a9493cbe 237 $ret_array = array();
238 $data = $this->makeRequest('action=register_new_user', $params);
239 if ($this->_checkForError($data)) {
240 return false;
241 }
242 foreach ($data as $a) {
243 switch ($a['tag']) {
244 case 'STATUS':
245 $ret_array['status'] = $a['value'];
246 break;
247
248 case 'AUTH_TOKEN':
249 $ret_array['auth_token'] = $a['value'];
250 break;
251
252 case 'LOGIN':
253 $ret_array['login'] = $a['value'];
254 break;
255 case 'SPACE_AMOUNT':
256 $ret_array['space_amount'] = $a['value'];
257 break;
258 case 'SPACE_USED':
259 $ret_array['space_used'] = $a['value'];
260 break;
d710e100 261 }
262 }
aa754fe3 263
a9493cbe 264 return $ret_array;
aa754fe3 265 }
266
a9493cbe 267 // Add Tags (http://enabled.box.net/docs/rest#add_to_tag)
aa754fe3 268
d710e100 269 function AddTag($tag, $id, $target_type, $params = array()) {
34f210f6 270 $params['auth_token'] = $this->auth_token;
271 $params['api_key'] = $this->api_key;
272 $params['action'] = 'add_to_tag';
273 $params['target'] = $target_type; // File or folder
274 $params['target_id'] = $id; // Set to ID of file or folder
d710e100 275 $params['tags[]'] = $tag;
276 $ret_array = array();
277 $data = $this->makeRequest('action=add_to_tag', $params);
278 if ($this->_checkForError($data)) {
279 return false;
280 }
281 foreach ($data as $a) {
282 switch ($a['tag']) {
a9493cbe 283 case 'STATUS':
284 $ret_array['status'] = $a['value'];
aa754fe3 285
a9493cbe 286 break;
d710e100 287 }
288 }
d710e100 289 return $ret_array;
d710e100 290 }
aa754fe3 291
d710e100 292 // Public Share (http://enabled.box.net/docs/rest#public_share)
d710e100 293 function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) {
34f210f6 294 $params['auth_token'] = $this->auth_token;
295 $params['api_key'] = $this->api_key;
296 $params['action'] = 'public_share';
297 $params['target'] = $target_type;
298 $params['target_id'] = $id;
299 $params['password'] = $password;
d710e100 300 $params['message'] = $message;
34f210f6 301 $params['emails'] = $emails;
d710e100 302 $ret_array = array();
303 $data = $this->makeRequest('action=public_share', $params);
304 if ($this->_checkForError($data)) {
305 return false;
306 }
307 foreach ($data as $a) {
308 switch ($a['tag']) {
a9493cbe 309 case 'STATUS':
310 $ret_array['status'] = $a['value'];
311 break;
312 case 'PUBLIC_NAME':
313 $ret_array['public_name'] = $a['value'];
314 break;
d710e100 315 }
316 }
aa754fe3 317
d710e100 318 return $ret_array;
319 }
d710e100 320 // Get Friends (http://enabled.box.net/docs/rest#get_friends)
a9493cbe 321 function GetFriends ($params = array()) {
34f210f6 322 $params['auth_token'] = $this->auth_token;
323 $params['action'] = 'get_friends';
324 $params['api_key'] = $this->api_key;
325 $params['params[]'] = 'nozip';
d710e100 326 $ret_array = array();
327 $data = $this->makeRequest('action=get_friends', $params);
328 if ($this->_checkForError($data)) {
329 return false;
330 }
331 foreach ($data as $a) {
332 switch ($a['tag']) {
a9493cbe 333 case 'NAME':
334 $ret_array['name'] = $a['value'];
335 break;
336 case 'EMAIL':
337 $ret_array['email'] = $a['value'];
338 break;
339 case 'ACCEPTED':
340 $ret_array['accepted'] = $a['value'];
341 break;
342 case 'AVATAR_URL':
343 $ret_array['avatar_url'] = $a['value'];
344 break;
345 case 'ID':
346 $ret_array['id'] = $a['value'];
347 break;
348 case 'URL':
349 $ret_array['url'] = $a['value'];
350 break;
351 case 'STATUS':
352 $ret_array['status'] = $a['value'];
353 break;
d710e100 354 }
355 }
d710e100 356 return $ret_array;
357 }
aa754fe3 358
d710e100 359 // Logout User
a9493cbe 360 function Logout($params = array()) {
34f210f6 361 $params['auth_token'] = $this->auth_token;
362 $params['api_key'] = $this->api_key;
363 $params['action'] = 'logout';
a9493cbe 364 $ret_array = array();
365 $data = $this->makeRequest('action=logout', $params);
366 if ($this->_checkForError($data)) {
367 return false;
368 }
369 foreach ($data as $a) {
370 switch ($a['tag']) {
371 case 'ACTION':
372 $ret_array['logout'] = $a['value'];
aa754fe3 373
a9493cbe 374 break;
aa754fe3 375 }
d710e100 376 return $ret_array;
377 }
378 }
a9493cbe 379 function _checkForError($data) {
380 if (@$data[0]['attributes']['STAT'] == 'fail') {
381 $this->_error_code = $data[1]['attributes']['CODE'];
382 $this->_error_msg = $data[1]['attributes']['MSG'];
383 return true;
384 }
385 return false;
aa754fe3 386 }
aa754fe3 387
a9493cbe 388 public function isError() {
389 if ($this->_error_msg != '') {
390 return true;
391 }
392 return false;
aa754fe3 393 }
aa754fe3 394
a9493cbe 395 function getErrorMsg() {
396 return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>';
397 }
aa754fe3 398
a9493cbe 399 function getErrorCode() {
400 return $this->_error_code;
401 }
aa754fe3 402
a9493cbe 403 function _clearErrors() {
404 $this->_error_code = '';
405 $this->_error_msg = '';
406 }
aa754fe3 407
d710e100 408}
409?>