/plugin/box-api/boxlibphp5.php

https://bitbucket.org/chamilo/chamilo-ext-repo-box-dev/ · PHP · 850 lines · 632 code · 184 blank · 34 comment · 62 complexity · d2423477a8f4b081ff6e86fae9c57997 MD5 · raw file

  1. <?php
  2. /**
  3. * Box REST Client Library for PHP5 Developers
  4. *
  5. *
  6. * @author James Levy <james@box.net>
  7. * @link http://enabled.box.net
  8. * @access public
  9. * @version 1.0
  10. * copyright Box.net 2007
  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. require_once 'class.curl.php';
  15. class boxclient
  16. {
  17. public function __construct($api_key, $auth_token)
  18. {
  19. $this->api_key = $api_key;
  20. $this->auth_token = $auth_token;
  21. }
  22. // Toggle Debug Mode
  23. var $_debug = false;
  24. // Setup variables
  25. var $_box_api_url = 'http://www.box.net/api/1.0/rest';
  26. var $_box_api_upload_url = 'http://upload.box.net/api/1.0/upload';
  27. var $_box_api_download_url = 'https://www.box.net/api/1.0/download';
  28. var $_error_code = '';
  29. var $_error_msg = '';
  30. // Setup for Functions
  31. function makeRequest($method, $params = array())
  32. {
  33. $this->_clearErrors();
  34. $useCURL = in_array('curl', get_loaded_extensions());
  35. if ($method == 'upload')
  36. {
  37. $args = array();
  38. foreach ($params as $k => $v)
  39. {
  40. array_push($args, urlencode($v));
  41. $query_str = implode('/', $args);
  42. }
  43. $query_str = rtrim($query_str, '/');
  44. $request = $this->_box_api_upload_url . '/' . $query_str;
  45. if ($this->_debug)
  46. {
  47. echo "Upload Request: " . $request;
  48. }
  49. }
  50. else
  51. if ($method == 'download')
  52. {
  53. $args = array();
  54. foreach ($params as $k => $v)
  55. {
  56. array_push($args, urlencode($v));
  57. $query_str = implode('/', $args);
  58. }
  59. $request = $this->_box_api_download_url . '/' . $query_str;
  60. if ($this->_debug)
  61. {
  62. echo "Download Request: " . $request;
  63. }
  64. }
  65. else
  66. {
  67. $args = array();
  68. foreach ($params as $k => $v)
  69. {
  70. array_push($args, urlencode($k) . '=' . urlencode($v));
  71. $query_str = implode('&', $args);
  72. }
  73. $request = $this->_box_api_url . '?' . $method . '&' . $query_str;
  74. if ($this->_debug)
  75. {
  76. echo "Request: " . $request;
  77. }
  78. }
  79. if ($useCURL)
  80. {
  81. $c = &new curl($request);
  82. $c->setopt(CURLOPT_FOLLOWLOCATION, true);
  83. $xml = $c->exec();
  84. $error = $c->hasError();
  85. if ($error)
  86. {
  87. $this->_error_msg = $error;
  88. return false;
  89. }
  90. $c->close();
  91. }
  92. else
  93. {
  94. $url_parsed = parse_url($request);
  95. $host = $url_parsed["host"];
  96. $port = ($url_parsed['port'] == 0) ? 80 : $url_parsed['port'];
  97. $path = $url_parsed["path"] . (($url_parsed['query'] != '') ? $path .= "?{$url_parsed[query]}" : '');
  98. $headers = "GET $path HTTP/1.0\r\n";
  99. $headers .= "Host: $host\r\n\r\n";
  100. $fp = fsockopen($host, $port, $errno, $errstr, 30);
  101. if (! $fp)
  102. {
  103. $this->_error_msg = $errstr;
  104. $this->_error_code = $errno;
  105. return false;
  106. }
  107. else
  108. {
  109. fwrite($fp, $headers);
  110. while (! feof($fp))
  111. {
  112. $xml .= fgets($fp, 1024);
  113. }
  114. fclose($fp);
  115. $xml_start = strpos($xml, '<?xml');
  116. $xml = substr($xml, $xml_start, strlen($xml));
  117. }
  118. }
  119. if ($this->_debug)
  120. {
  121. echo '<h2>XML Response</h2>';
  122. echo '<pre class="xml">';
  123. echo htmlspecialchars($xml);
  124. echo '</pre>';
  125. }
  126. $xml_parser = xml_parser_create();
  127. xml_parse_into_struct($xml_parser, $xml, $data);
  128. xml_parser_free($xml_parser);
  129. return $data;
  130. }
  131. //
  132. //////// Functions
  133. // Get Ticket
  134. function getTicket($params = array())
  135. {
  136. $params['api_key'] = $this->api_key;
  137. $ret_array = array();
  138. $data = $this->makeRequest('action=get_ticket', $params);
  139. if ($this->_checkForError($data))
  140. {
  141. return false;
  142. }
  143. foreach ($data as $a)
  144. {
  145. switch ($a['tag'])
  146. {
  147. case 'STATUS' :
  148. $ret_array['status'] = $a['value'];
  149. break;
  150. case 'TICKET' :
  151. $ret_array['ticket'] = $a['value'];
  152. break;
  153. }
  154. }
  155. if ($this->_debug)
  156. {
  157. echo '<h2>Ticket Return</h2>';
  158. $this->_a($ret_array);
  159. print_r($a);
  160. echo '<hr />';
  161. }
  162. return $ret_array;
  163. }
  164. // Get Auth Token
  165. function getAuthToken($ticket)
  166. {
  167. $params['api_key'] = $this->api_key;
  168. $params['ticket'] = $ticket;
  169. $ret_array = array();
  170. $data = $this->makeRequest('action=get_auth_token', $params);
  171. if ($this->_checkForError($data))
  172. {
  173. return false;
  174. }
  175. foreach ($data as $a)
  176. {
  177. switch ($a['tag'])
  178. {
  179. case 'STATUS' :
  180. $ret_array['status'] = $a['value'];
  181. break;
  182. case 'AUTH_TOKEN' :
  183. $ret_array['auth_token'] = $a['value'];
  184. break;
  185. }
  186. }
  187. if ($ret_array['status'] == 'get_auth_token_ok')
  188. {
  189. $auth_token = $ret_array['auth_token'];
  190. global $auth_token;
  191. }
  192. else
  193. {
  194. header('location: http://www.box.net/api/1.0/auth/' . $ticket);
  195. }
  196. }
  197. // Retrieve Account Tree (http://enabled.box.net/docs/rest#get_account_tree)
  198. function getAccountTree($params = array())
  199. {
  200. $params['api_key'] = $this->api_key;
  201. $params['auth_token'] = $this->auth_token;
  202. $params['folder_id'] = 0;
  203. $params['params[]'] = 'nozip';
  204. $ret_array = array();
  205. $data = $this->makeRequest('action=get_account_tree', $params);
  206. if ($this->_checkForError($data))
  207. {
  208. return false;
  209. }
  210. $tree_count = count($data);
  211. global $tree_count;
  212. for($i = 0, $tree_count = count($data); $i < $tree_count; $i ++)
  213. {
  214. $a = $data[$i];
  215. switch ($a['tag'])
  216. {
  217. case 'FOLDER' :
  218. if (is_array($a['attributes']))
  219. {
  220. $ret_array[$i]['folder_id'] = $a['attributes']['ID'];
  221. $ret_array[$i]['folder_name'] = $a['attributes']['NAME'];
  222. }
  223. break;
  224. case 'FILE' :
  225. if (is_array($a['attributes']))
  226. {
  227. $ret_array[$i]['file_id'] = $a['attributes']['ID'];
  228. $ret_array[$i]['file_name'] = $a['attributes']['FILE_NAME'];
  229. $ret_array[$i]['description'] = $a['attributes']['DESCRIPTION'];
  230. $ret_array[$i]['created'] = $a['attributes']['CREATED'];
  231. $ret_array[$i]['updated'] = $a['attributes']['UPDATED'];
  232. $ret_array[$i]['size'] = $a['attributes']['SIZE'];
  233. $tree_count ++;
  234. }
  235. break;
  236. }
  237. }
  238. if ($this->_debug)
  239. {
  240. echo '<h2>Account Tree Return</h2>';
  241. $this->_a($ret_array);
  242. "<br/>";
  243. print_r($a);
  244. echo '<hr />';
  245. }
  246. return $ret_array;
  247. }
  248. function get_file_info($file_id, $params = array())
  249. {
  250. $params['api_key'] = $this->api_key;
  251. $params['auth_token'] = $this->auth_token;
  252. $params['file_id'] = $file_id;
  253. $params['params[]'] = 'nozip';
  254. $ret_array = array();
  255. $data = $this->makeRequest('action=get_file_info', $params);
  256. if ($this->_checkForError($data))
  257. {
  258. return false;
  259. }
  260. foreach ($data as $d)
  261. {
  262. switch ($d['tag'])
  263. {
  264. case 'FILE_ID' :
  265. $ret_array['file_id'] = $d['value'];
  266. break;
  267. case 'FILE_NAME' :
  268. $ret_array['file_name'] = $d['value'];
  269. break;
  270. case 'FOLDER_ID' :
  271. $ret_array['folder_id'] = $d['value'];
  272. break;
  273. case 'CREATED' :
  274. $ret_array['created'] = $d['value'];
  275. break;
  276. case 'UPDATED' :
  277. $ret_array['updated'] = $d['value'];
  278. break;
  279. }
  280. }
  281. return $ret_array;
  282. }
  283. function get_files($folder_id, $params = array())
  284. {
  285. $params['api_key'] = $this->api_key;
  286. $params['auth_token'] = $this->auth_token;
  287. $params['folder_id'] = 0;
  288. $params['params[]'] = 'nozip';
  289. $ret_array = array();
  290. $data = $this->makeRequest('action=get_account_tree', $params);
  291. if ($this->_checkForError($data))
  292. {
  293. return false;
  294. }
  295. $tree_count = count($data);
  296. $files = array();
  297. global $tree_count;
  298. for($i = 0, $tree_count = count($data); $i < $tree_count; $i ++)
  299. {
  300. $a = $data[$i];
  301. switch ($a['tag'])
  302. {
  303. case 'FILE' :
  304. if (is_array($a['attributes']))
  305. {
  306. $file = $this->get_file_info($a['attributes']['ID']);
  307. if ($file['folder_id'] == $folder_id)
  308. {
  309. $files[] = $file;
  310. }
  311. }
  312. break;
  313. }
  314. }
  315. return $files;
  316. }
  317. function delete_file($file_id, $params = array())
  318. {
  319. $params['api_key'] = $this->api_key;
  320. $params['auth_token'] = $this->auth_token;
  321. $params['target'] = 'file';
  322. $params['target_id'] = $file_id;
  323. $ret_array = array();
  324. $data = $this->makeRequest('action=delete', $params);
  325. if ($this->_checkForError($data))
  326. {
  327. return false;
  328. }
  329. return true;
  330. }
  331. function download_file($file_id, $params = array())
  332. {
  333. $params['auth_token'] = $this->auth_token;
  334. $params['file_id'] = $file_id;
  335. $handle = fopen($this->_box_api_download_url . '/' . $this->auth_token . '/' . $file_id, 'rb');
  336. $contents = stream_get_contents($handle);
  337. fclose($handle);
  338. return $contents;
  339. }
  340. function rename_file($file_id, $new_name, $params = array())
  341. {
  342. $params['api_key'] = $this->api_key;
  343. $params['auth_token'] = $this->auth_token;
  344. $params['target'] = 'file';
  345. $params['target_id'] = $file_id;
  346. $params['new_name'] = $new_name;
  347. $ret_array = array();
  348. $data = $this->makeRequest('', $params);
  349. if ($this->_checkForError($data))
  350. {
  351. return false;
  352. }
  353. return true;
  354. }
  355. // Create New Folder
  356. function createFolder($new_folder_name, $parent_id, $params = array())
  357. {
  358. $params['api_key'] = $this->api_key;
  359. $params['auth_token'] = $this->auth_token;
  360. $params['parent_id'] = $parent_id;
  361. $params['name'] = $new_folder_name;
  362. $params['share'] = 1; //Set to '1' by default. Set to '0' to make folder private.
  363. $ret_array = array();
  364. $data = $this->makeRequest('action=create_folder', $params);
  365. if ($this->_checkForError($data))
  366. {
  367. return false;
  368. }
  369. foreach ($data as $a)
  370. {
  371. switch ($a['tag'])
  372. {
  373. case 'FOLDER_ID' :
  374. $ret_array['folder_id'] = $a['value'];
  375. break;
  376. case 'FOLDER_NAME' :
  377. $ret_array['folder_type'] = $a['value'];
  378. break;
  379. case 'SHARED' :
  380. $ret_array['shared'] = $a['value'];
  381. break;
  382. case 'PASSWORD' :
  383. $ret_array['password'] = $a['value'];
  384. break;
  385. }
  386. }
  387. if ($this->_debug)
  388. {
  389. echo '<h2>Account Tree Return</h2>';
  390. $this->_a($ret_array);
  391. "<br/>";
  392. print_r($a);
  393. echo '<hr />';
  394. }
  395. return $ret_array;
  396. }
  397. function ExportFile($file, $params = array())
  398. {
  399. $curl = curl_init($this->_box_api_upload_url . '/' . $this->auth_token . '/0');
  400. curl_setopt($curl, CURLOPT_POST, true);
  401. curl_setopt($curl, CURLOPT_POSTFIELDS, array('file' => ('@' . $file)));
  402. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  403. curl_setopt($curl, CURLINFO_HEADER_OUT, true);
  404. $response = curl_exec($curl);
  405. curl_close($curl);
  406. if (strpos($response, 'upload_ok'))
  407. {
  408. return true;
  409. }
  410. else
  411. return false;
  412. }
  413. function UploadFile($file, $params = array())
  414. {
  415. //$filename_header = "Content-Disposition: form-data; name=\"Filename\"\r\n\r\n" . $file['name'] ."\r\nBoundary:";
  416. //$filename_header1 = "Content-Disposition: form-data name=\"Filedata\";filename=\"". rawurlencode($file['name']) ."\"\r\n\r\n";
  417. $curl = curl_init($this->_box_api_upload_url . '/' . $this->auth_token . '/0');
  418. //curl_setopt($curl, CURLOPT_HTTPHEADER, array($filename_header, $filename_header1));
  419. curl_setopt($curl, CURLOPT_POST, true);
  420. curl_setopt($curl, CURLOPT_POSTFIELDS, array('file' => ('@' . $file['tmp_name'])));
  421. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  422. $response = curl_exec($curl);
  423. curl_close($curl);
  424. if (strpos($response, 'upload_ok'))
  425. {
  426. return true;
  427. }
  428. else
  429. return false;
  430. }
  431. // Register New User
  432. function RegisterUser($params = array())
  433. {
  434. $params['api_key'] = $this->api_key;
  435. $params['login'] = $_REQUEST['login'];
  436. $params['password'] = $_REQUEST['password'];
  437. $ret_array = array();
  438. $data = $this->makeRequest('action=register_new_user', $params);
  439. if ($this->_checkForError($data))
  440. {
  441. return false;
  442. }
  443. foreach ($data as $a)
  444. {
  445. switch ($a['tag'])
  446. {
  447. case 'STATUS' :
  448. $ret_array['status'] = $a['value'];
  449. break;
  450. case 'AUTH_TOKEN' :
  451. $ret_array['auth_token'] = $a['value'];
  452. break;
  453. case 'LOGIN' :
  454. $ret_array['login'] = $a['value'];
  455. break;
  456. case 'SPACE_AMOUNT' :
  457. $ret_array['space_amount'] = $a['value'];
  458. break;
  459. case 'SPACE_USED' :
  460. $ret_array['space_used'] = $a['value'];
  461. break;
  462. }
  463. }
  464. if ($this->_debug)
  465. {
  466. echo '<h2>Registration Return</h2>';
  467. $this->_a($ret_array);
  468. print_r($a);
  469. echo '<hr />';
  470. }
  471. return $ret_array;
  472. }
  473. // Add Tags (http://enabled.box.net/docs/rest#add_to_tag)
  474. function AddTag($tag, $id, $target_type, $params = array())
  475. {
  476. $params['api_key'] = $this->api_key;
  477. $params['auth_token'] = $this->auth_token;
  478. $params['target'] = $target_type; // File or folder
  479. $params['target_id'] = $id; // Set to ID of file or folder
  480. $params['tags[]'] = $tag;
  481. $ret_array = array();
  482. $data = $this->makeRequest('action=add_to_tag', $params);
  483. if ($this->_checkForError($data))
  484. {
  485. return false;
  486. }
  487. foreach ($data as $a)
  488. {
  489. switch ($a['tag'])
  490. {
  491. case 'STATUS' :
  492. $ret_array['status'] = $a['value'];
  493. break;
  494. }
  495. }
  496. if ($this->_debug)
  497. {
  498. echo '<h2>Tag Return</h2>';
  499. $this->_a($ret_array);
  500. print_r($a);
  501. echo '<hr />';
  502. }
  503. return $ret_array;
  504. }
  505. // Public Share (http://enabled.box.net/docs/rest#public_share)
  506. function PublicShare($message, $emails, $id, $target_type, $password, $params = array())
  507. {
  508. $params['api_key'] = $this->api_key;
  509. $params['auth_token'] = $this->auth_token;
  510. $params['target'] = $target_type; // File or folder
  511. $params['target_id'] = $id; // Set to ID of file or folder
  512. $params['password'] = $password; //optional
  513. $params['message'] = $message;
  514. $params['emails'] = $emails;
  515. $ret_array = array();
  516. $data = $this->makeRequest('action=public_share', $params);
  517. if ($this->_checkForError($data))
  518. {
  519. return false;
  520. }
  521. foreach ($data as $a)
  522. {
  523. switch ($a['tag'])
  524. {
  525. case 'STATUS' :
  526. $ret_array['status'] = $a['value'];
  527. break;
  528. case 'PUBLIC_NAME' :
  529. $ret_array['public_name'] = $a['value'];
  530. break;
  531. }
  532. }
  533. if ($this->_debug)
  534. {
  535. echo '<h2>Public Share Return</h2>';
  536. $this->_a($ret_array);
  537. print_r($a);
  538. echo '<hr />';
  539. }
  540. return $ret_array;
  541. }
  542. // Get Friends (http://enabled.box.net/docs/rest#get_friends)
  543. function GetFriends($params = array())
  544. {
  545. $params['api_key'] = $this->api_key;
  546. $params['auth_token'] = $this->auth_token;
  547. $params['params[]'] = 'nozip';
  548. $ret_array = array();
  549. $data = $this->makeRequest('action=get_friends', $params);
  550. if ($this->_checkForError($data))
  551. {
  552. return false;
  553. }
  554. foreach ($data as $a)
  555. {
  556. switch ($a['tag'])
  557. {
  558. case 'NAME' :
  559. $ret_array['name'] = $a['value'];
  560. break;
  561. case 'EMAIL' :
  562. $ret_array['email'] = $a['value'];
  563. break;
  564. case 'ACCEPTED' :
  565. $ret_array['accepted'] = $a['value'];
  566. break;
  567. case 'AVATAR_URL' :
  568. $ret_array['avatar_url'] = $a['value'];
  569. break;
  570. case 'ID' :
  571. $ret_array['id'] = $a['value'];
  572. break;
  573. case 'URL' :
  574. $ret_array['url'] = $a['value'];
  575. break;
  576. case 'STATUS' :
  577. $ret_array['status'] = $a['value'];
  578. break;
  579. }
  580. }
  581. if ($this->_debug)
  582. {
  583. echo '<h2>Get Friend Return</h2>';
  584. $this->_a($ret_array);
  585. print_r($a);
  586. echo '<hr />';
  587. }
  588. return $ret_array;
  589. }
  590. // Logout User
  591. function Logout($params = array())
  592. {
  593. $params['api_key'] = $this->api_key;
  594. $params['auth_token'] = $this->auth_token;
  595. $ret_array = array();
  596. $data = $this->makeRequest('action=logout', $params);
  597. if ($this->_checkForError($data))
  598. {
  599. return false;
  600. }
  601. foreach ($data as $a)
  602. {
  603. switch ($a['tag'])
  604. {
  605. case 'ACTION' :
  606. $ret_array['logout'] = $a['value'];
  607. break;
  608. }
  609. if ($this->_debug)
  610. {
  611. echo '<h2>Logout Return</h2>';
  612. $this->_a($ret_array);
  613. print_r($a);
  614. echo '<hr />';
  615. }
  616. return $ret_array;
  617. }
  618. }
  619. /*
  620. /
  621. / Debugging & Error Codes
  622. /
  623. */
  624. function _checkForError($data)
  625. {
  626. if ($data[0]['attributes']['STAT'] == 'fail')
  627. {
  628. $this->_error_code = $data[1]['attributes']['CODE'];
  629. $this->_error_msg = $data[1]['attributes']['MSG'];
  630. return true;
  631. }
  632. return false;
  633. }
  634. function isError()
  635. {
  636. if ($this->_error_msg != '')
  637. {
  638. return true;
  639. }
  640. return false;
  641. }
  642. function getErrorMsg()
  643. {
  644. return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>';
  645. }
  646. function getErrorCode()
  647. {
  648. return $this->_error_code;
  649. }
  650. function _clearErrors()
  651. {
  652. $this->_error_code = '';
  653. $this->_error_msg = '';
  654. }
  655. function setDebug($debug)
  656. {
  657. $this->_debug = $debug;
  658. }
  659. function _a($array)
  660. {
  661. echo '<pre>';
  662. print_r($array);
  663. echo '</pre>';
  664. }
  665. }
  666. ?>