--- /dev/null
+<?php
+/**
+ * Box REST Client Library for PHP5 Developers
+ *
+ *
+ * @author James Levy <james@box.net>
+ * @link http://enabled.box.net
+ * @access public
+ * @version 1.0
+ * copyright Box.net 2007
+ * Available for use and distribution under GPL-license
+ * Go to http://www.gnu.org/licenses/gpl-3.0.txt for full text
+ */
+
+/**
+ * Modified by Dongsheng Cai <dongsheng@cvs.moodle.org>
+ *
+ */
+
+require_once 'class.curl.php';
+
+class boxclient {
+
+ var $_debug = false;
+ var $_box_api_url = 'http://www.box.net/api/1.0/rest';
+ var $_box_api_upload_url = 'http://upload.box.net/api/1.0/upload';
+ var $_error_code = '';
+ var $_error_msg = '';
+ var $auth_token = '';
+
+ public function setAuth($str){
+ $this->auth_token = $str;
+
+ }
+
+ public function __construct($api_key, $auth_token) {
+ $this->api_key = $api_key;
+ $this->auth_token = $auth_token;
+ }
+ // Setup for Functions
+ function makeRequest($method, $params = array()) {
+ $this->_clearErrors();
+ $useCURL = in_array('curl', get_loaded_extensions());
+ if ($method == 'upload'){
+ $args = array();
+ foreach($params as $k => $v){
+ array_push($args, urlencode($v));
+ $query_str = implode('/', $args);
+ }
+ $request = $this->_box_api_upload_url .'/'. $query_str;
+ if ($this->_debug){
+ echo "Upload Request: ".$request;
+ }
+ }else{
+ $args = array();
+ foreach($params as $k => $v){
+ array_push($args, urlencode($k).'='.urlencode($v));
+ $query_str = implode('&', $args);
+ }
+ $request = $this->_box_api_url .'?'. $method . '&' . $query_str;
+ if ($this->_debug){ echo "Request: ".$request; }
+ }
+ if ($useCURL) {
+ $c = &new curl($request );
+ $c->setopt(CURLOPT_FOLLOWLOCATION, true);
+ $xml = $c->exec();
+ $error = $c->hasError();
+ if ($error) {
+ $this->_error_msg = $error;
+ return false;
+ }
+ $c->close() ;
+ } else {
+ $url_parsed = parse_url($request);
+ $host = $url_parsed["host"];
+ $port = ($url_parsed['port'] == 0) ? 80 : $url_parsed['port'];
+ $path = $url_parsed["path"] . (($url_parsed['query'] != '') ? $path .= "?{$url_parsed[query]}" : '');
+ $headers = "GET $path HTTP/1.0\r\n";
+ $headers .= "Host: $host\r\n\r\n";
+ $fp = fsockopen($host, $port, $errno, $errstr, 30);
+ if (!$fp) {
+ $this->_error_msg = $errstr;
+ $this->_error_code = $errno;
+ return false;
+ } else {
+ fwrite($fp, $headers);
+ while (!feof($fp)) {
+ $xml .= fgets($fp, 1024);
+ }
+ fclose($fp);
+
+
+ $xml_start = strpos($xml, '<?xml');
+ $xml = substr($xml, $xml_start, strlen($xml));
+ }
+ }
+
+ if ($this->_debug) {
+ echo '<h2>XML Response</h2>';
+ echo '<pre class="xml">';
+ echo htmlspecialchars($xml);
+ echo '</pre>';
+ }
+
+ $xml_parser = xml_parser_create();
+ xml_parse_into_struct($xml_parser, $xml, $data);
+ xml_parser_free($xml_parser);
+ return $data;
+ }
+ function getTicket($params = array()) {
+ $params['api_key'] = $this->api_key;
+ $ret_array = array();
+ $data = $this->makeRequest('action=get_ticket', $params);
+ if ($this->_checkForError($data)) {
+ return false;
+ }
+ foreach ($data as $a) {
+ switch ($a['tag']) {
+ case 'STATUS':
+ $ret_array['status'] = $a['value'];
+ break;
+ case 'TICKET':
+ $ret_array['ticket'] = $a['value'];
+ break;
+ }
+ }
+ if ($this->_debug) {
+ echo '<h2>Ticket Return</h2>';
+ $this->_a($ret_array);
+ var_dump ($a);
+ }
+ return $ret_array;
+ }
+ // Get Auth Token
+ function getAuthToken($ticket) {
+ $params['api_key'] = $this->api_key;
+ $params['ticket'] = $ticket;
+ $ret_array = array();
+ $data = $this->makeRequest('action=get_auth_token', $params);
+ if ($this->_checkForError($data)) {
+ return false;
+ }
+ foreach ($data as $a) {
+ switch ($a['tag'])
+ {
+ case 'STATUS':
+ $ret_array['status'] = $a['value'];
+ break;
+ case 'AUTH_TOKEN':
+ $ret_array['auth_token'] = $a['value'];
+ break;
+ }
+ }
+
+ if ($ret_array['status'] == 'get_auth_token_ok'){
+ $this->auth_token = $ret_array['auth_token'];
+ $auth_token = $ret_array['auth_token'];
+ global $auth_token;
+ }else{
+ echo '<a href="http://www.box.net/api/1.0/auth/'.$ticket.'">Login</a>';
+ //header ('location: http://www.box.net/api/1.0/auth/'.$ticket) ;
+ }
+ }
+
+ // Retrieve Account Tree (http://enabled.box.net/docs/rest#get_account_tree)
+ function getAccountTree($params = array()) {
+ $params['api_key'] = $this->api_key;
+ $params['auth_token'] = $this->auth_token;
+ $params['folder_id'] = 0;
+ $params['onelevel'] = 1;
+ $params['params[]'] = 'nozip';
+ $ret_array = array();
+ $data = $this->makeRequest('action=get_account_tree', $params);
+ if ($this->_checkForError($data)) {
+ return false;
+ }
+ $tree_count=count($data);
+ global $tree_count;
+ $entry_count = 0;
+ for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) {
+ $a = $data[$i];
+ switch ($a['tag'])
+ {
+ case 'FOLDER':
+ if (@is_array($a['attributes'])) {
+ $ret_array['folder_id'][$i] = $a['attributes']['ID'];
+ $ret_array['folder_name'][$i] = $a['attributes']['NAME'];
+ $ret_array['shared'][$i] = $a['attributes']['SHARED'];
+ }
+ break;
+
+ case 'FILE':
+ if (@is_array($a['attributes'])) {
+ $ret_array['file_id'][$i] = $a['attributes']['ID'];
+ @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
+ @$ret_array['file_keyword'][$i] = $a['attributes']['KEYWORD'];
+ $entry_count++;
+ }
+ break;
+ }
+ }
+ if ($this->_debug) {
+ echo '<h2>Account Tree Return</h2>';
+ $this->_a($ret_array);
+ var_dump ($a);
+ }
+ return $ret_array;
+ }
+ // Create New Folder
+ function CreateFolder($new_folder_name, $params = array()) {
+ $params['api_key'] = $this->api_key;
+ $params['auth_token'] = $this->auth_token;
+ $params['parent_id'] = 0; //Set to '0' by default. Change to create within sub-folder.
+ $params['name'] = $new_folder_name;
+ $params['share'] = 1; //Set to '1' by default. Set to '0' to make folder private.
+
+ $ret_array = array();
+ $data = $this->makeRequest('action=create_folder', $params);
+
+
+ if ($this->_checkForError($data)) {
+ return false;
+ }
+ foreach ($data as $a) {
+ switch ($a['tag']) {
+ case 'FOLDER_ID':
+ $ret_array['folder_id'] = $a['value'];
+ break;
+
+ case 'FOLDER_NAME':
+ $ret_array['folder_type'] = $a['value'];
+ break;
+
+ case 'SHARED':
+ $ret_array['shared'] = $a['value'];
+ break;
+ case 'PASSWORD':
+ $ret_array['password'] = $a['value'];
+ break;
+ }
+ }
+ if ($this->_debug) {
+ echo '<h2>Account Tree Return</h2>';
+ $this->_a($ret_array);
+ var_dump ($a);
+ }
+ return $ret_array;
+ }
+ // Upload File
+ function UploadFile ($params = array()) {
+ $params['auth_token'] = $this->auth_token;
+ $params['new_file1'] = $_FILES['new_file1'];
+ $params['folder id'] = 0; //Set to '0' by default. Change to create within sub-folder.
+ $params['share'] = 1; //Set to '1' by default. Set to '0' to make folder private.
+ $ret_array = array();
+ $data = $this->makeUploadRequst($params);
+ if ($this->_checkForError($data)) {
+ return false;
+ }
+ for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) {
+ $a = $data[$i];
+ switch ($a['tag']) {
+ case 'STATUS':
+ $ret_array['status'] = $a['value'];
+
+ break;
+
+ case 'FILE':
+ if (is_array($a['attributes'])) {
+ $ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
+ $ret_array['id'][$i] = $a['attributes']['ID'];
+ $ret_array['folder_name'][$i] = $a['attributes']['FOLDER_NAME'];
+ $ret_array['error'][$i] = $a['attributes']['ERROR'];
+ $ret_array['public_name'][$i] = $a['attributes']['PUBLIC_NAME'];
+ $entry_count++;
+ }
+ break;
+ }
+ }
+
+ if ($this->_debug) {
+ echo '<h2>Account Tree Return</h2>';
+ $this->_a($ret_array);
+ var_dump ($a);
+ }
+ return $ret_array;
+ }
+
+
+ // Set Description of File or Folder
+
+
+
+
+ // Register New User
+
+ function RegisterUser($params = array()) {
+
+ $params['api_key'] = $this->api_key;
+ $params['login'] = $_REQUEST['login'];
+ $params['password'] = $_REQUEST['password'];
+ $ret_array = array();
+ $data = $this->makeRequest('action=register_new_user', $params);
+ if ($this->_checkForError($data)) {
+ return false;
+ }
+ foreach ($data as $a) {
+ switch ($a['tag']) {
+ case 'STATUS':
+ $ret_array['status'] = $a['value'];
+ break;
+
+ case 'AUTH_TOKEN':
+ $ret_array['auth_token'] = $a['value'];
+ break;
+
+ case 'LOGIN':
+ $ret_array['login'] = $a['value'];
+ break;
+ case 'SPACE_AMOUNT':
+ $ret_array['space_amount'] = $a['value'];
+ break;
+ case 'SPACE_USED':
+ $ret_array['space_used'] = $a['value'];
+ break;
+ }
+ }
+
+ if ($this->_debug) {
+ echo '<h2>Registration Return</h2>';
+ $this->_a($ret_array);
+ var_dump ($a);
+ }
+
+ return $ret_array;
+ }
+
+ // Add Tags (http://enabled.box.net/docs/rest#add_to_tag)
+
+ function AddTag($tag, $id, $target_type, $params = array()) {
+
+ $params['api_key'] = $this->api_key;
+ $params['auth_token'] = $this->auth_token;
+ $params['target'] = $target_type; // File or folder
+ $params['target_id'] = $id; // Set to ID of file or folder
+ $params['tags[]'] = $tag;
+ $ret_array = array();
+ $data = $this->makeRequest('action=add_to_tag', $params);
+ if ($this->_checkForError($data)) {
+ return false;
+ }
+ foreach ($data as $a) {
+ switch ($a['tag']) {
+ case 'STATUS':
+ $ret_array['status'] = $a['value'];
+
+ break;
+ }
+ }
+
+ if ($this->_debug) {
+ echo '<h2>Tag Return</h2>';
+ $this->_a($ret_array);
+ var_dump ($a);
+ }
+
+ return $ret_array;
+
+
+ }
+
+ // Public Share (http://enabled.box.net/docs/rest#public_share)
+
+ function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) {
+ $params['api_key'] = $this->api_key;
+ $params['auth_token'] = $this->auth_token;
+ $params['target'] = $target_type; // File or folder
+ $params['target_id'] = $id; // Set to ID of file or folder
+ $params['password'] = $password; //optional
+ $params['message'] = $message;
+ $params['emails'] = $emails;
+ $ret_array = array();
+ $data = $this->makeRequest('action=public_share', $params);
+ if ($this->_checkForError($data)) {
+ return false;
+ }
+ foreach ($data as $a) {
+ switch ($a['tag']) {
+ case 'STATUS':
+ $ret_array['status'] = $a['value'];
+ break;
+ case 'PUBLIC_NAME':
+ $ret_array['public_name'] = $a['value'];
+ break;
+ }
+ }
+
+ if ($this->_debug) {
+ echo '<h2>Public Share Return</h2>';
+ $this->_a($ret_array);
+ var_dump ($a);
+ }
+ return $ret_array;
+ }
+
+
+ // Get Friends (http://enabled.box.net/docs/rest#get_friends)
+
+ function GetFriends ($params = array()) {
+
+ $params['api_key'] = $this->api_key;
+ $params['auth_token'] = $this->auth_token;
+ $params['params[]'] = 'nozip';
+ $ret_array = array();
+ $data = $this->makeRequest('action=get_friends', $params);
+ if ($this->_checkForError($data)) {
+ return false;
+ }
+ foreach ($data as $a) {
+ switch ($a['tag']) {
+ case 'NAME':
+ $ret_array['name'] = $a['value'];
+ break;
+ case 'EMAIL':
+ $ret_array['email'] = $a['value'];
+ break;
+ case 'ACCEPTED':
+ $ret_array['accepted'] = $a['value'];
+ break;
+ case 'AVATAR_URL':
+ $ret_array['avatar_url'] = $a['value'];
+ break;
+ case 'ID':
+ $ret_array['id'] = $a['value'];
+ break;
+ case 'URL':
+ $ret_array['url'] = $a['value'];
+ break;
+ case 'STATUS':
+ $ret_array['status'] = $a['value'];
+ break;
+ }
+ }
+ if ($this->_debug) {
+ echo '<h2>Get Friend Return</h2>';
+ $this->_a($ret_array);
+ var_dump ($a);
+ }
+ return $ret_array;
+ }
+
+
+ // Logout User
+
+ function Logout($params = array()) {
+
+ $params['api_key'] = $this->api_key;
+ $params['auth_token'] = $this->auth_token;
+ $ret_array = array();
+ $data = $this->makeRequest('action=logout', $params);
+ if ($this->_checkForError($data)) {
+ return false;
+ }
+ foreach ($data as $a) {
+ switch ($a['tag']) {
+ case 'ACTION':
+ $ret_array['logout'] = $a['value'];
+
+ break;
+ }
+ if ($this->_debug) {
+ echo '<h2>Logout Return</h2>';
+ $this->_a($ret_array);
+ var_dump ($a);
+ }
+ return $ret_array;
+ }
+ }
+ function _checkForError($data) {
+ if (@$data[0]['attributes']['STAT'] == 'fail') {
+ $this->_error_code = $data[1]['attributes']['CODE'];
+ $this->_error_msg = $data[1]['attributes']['MSG'];
+ return true;
+ }
+ return false;
+ }
+
+
+ public function isError() {
+ if ($this->_error_msg != '') {
+ return true;
+ }
+ return false;
+ }
+
+
+ function getErrorMsg() {
+ return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>';
+ }
+
+ function getErrorCode() {
+ return $this->_error_code;
+ }
+
+
+ function _clearErrors() {
+ $this->_error_code = '';
+ $this->_error_msg = '';
+ }
+
+ public function setDebug($debug) {
+ $this->_debug = $debug;
+ }
+
+ private function _a($array) {
+ var_dump($array);
+ }
+}
+?>
--- /dev/null
+<?php\r
+/**\r
+ * @author Dick Munroe (munroe@csworks.com)\r
+ * @copyright copyright @ 2004, Dick Munroe, released under the GPL.\r
+ *\r
+ * The cURL class is a thin wrapper around the procedural interface\r
+ * to cURL provided by PHP. I use it mostly as a base class for\r
+ * web services whose low level interface is, literally, web pages.\r
+ *\r
+ * There are a few differences (value added, I guess) between the interface\r
+ * provided by this class and the procedural cURL interface. Most\r
+ * noticable are:\r
+ *\r
+ * 1. The curl::exec function (when returning data to the caller rather\r
+ * than simply outputing it) always parses the HTTP header and returns\r
+ * only the body portion of the reqeust. The header is available via\r
+ * the curl::getHeader method.\r
+ * 2. The status of the last curl::exec is always maintained. It is\r
+ * available via the curl::getStatus method. In addition to the information\r
+ * returned by curl_getinfo, that of curl_error and curl_errno is folded\r
+ * in as well.\r
+ *\r
+ * @example ./example.class.curl.php\r
+ */\r
+\r
+//\r
+// Edit History:\r
+//\r
+// Dick Munroe munroe@csworks.com 30-Nov-2004\r
+// Initial Version Created.\r
+//\r
+// Dick Munroe munroe@csworks.com 01-Dec-2004\r
+// Forgot to check for cURL actually being in this instance of PHP.\r
+//\r
+\r
+class curl\r
+{\r
+ /**\r
+ * The mapping to caseless header names.\r
+ *\r
+ * @access private\r
+ * @var array\r
+ */\r
+\r
+ var $m_caseless ;\r
+\r
+ /**\r
+ * The handle for the current curl session.\r
+ *\r
+ * @access private\r
+ * @var resource\r
+ */\r
+ \r
+ var $m_handle ;\r
+\r
+ /**\r
+ * The parsed contents of the HTTP header if one happened in the\r
+ * message. All repeated elements appear as arrays.\r
+ *\r
+ * The headers are stored as an associative array, the key of which\r
+ * is the name of the header, e.g., Set-Cookie, and the values of which\r
+ * are the bodies of the header in the order in which they occurred.\r
+ * \r
+ * Some headers can be repeated in a single header, e.g., Set-Cookie and\r
+ * pragma, so each type of header has an array containing one or more\r
+ * headers of the same type.\r
+ *\r
+ * The names of the headers can, potentially, vary in spelling from\r
+ * server to server and client to client. No attempt to regulate this\r
+ * is made, i.e., the curl class does not force all headers to lower\r
+ * or upper class, but it DOES collect all headers of the same type\r
+ * under the spelling of the type of header used by the FIRST header\r
+ * of that type.\r
+ *\r
+ * For example, two headers:\r
+ *\r
+ * 1. Set-Cookie: ...\r
+ * 2. set-cookie: ...\r
+ *\r
+ * Would appear as $this->m_header['Set-Cookie'][0] and ...[1]\r
+ *\r
+ * @access private\r
+ * @var mixed\r
+ */\r
+\r
+ var $m_header ;\r
+\r
+ /**\r
+ * Current setting of the curl options.\r
+ *\r
+ * @access private\r
+ * @var mixed\r
+ */\r
+\r
+ var $m_options ;\r
+\r
+ /**\r
+ * Status information for the last executed http request. Includes the errno and error\r
+ * in addition to the information returned by curl_getinfo.\r
+ *\r
+ * The keys defined are those returned by curl_getinfo with two additional\r
+ * ones specified, 'error' which is the value of curl_error and 'errno' which\r
+ * is the value of curl_errno.\r
+ *\r
+ * @link http://www.php.net/curl_getinfo\r
+ * @link http://www.php.net/curl_errno\r
+ * @link http://www.php.net/curl_error\r
+ * @access private\r
+ * @var mixed\r
+ */\r
+\r
+ var $m_status ;\r
+\r
+ /**\r
+ * curl class constructor\r
+ *\r
+ * Initializes the curl class for it's default behavior:\r
+ * o no HTTP headers.\r
+ * o return the transfer as a string.\r
+ * o URL to access.\r
+ * By default, the curl class will simply read the URL provided\r
+ * in the constructor.\r
+ *\r
+ * @link http://www.php.net/curl_init\r
+ * @param string $theURL [optional] the URL to be accessed by this instance of the class.\r
+ */\r
+\r
+ function curl($theURL=null)\r
+ {\r
+ if (!function_exists('curl_init'))\r
+ {\r
+ trigger_error('PHP was not built with --with-curl, rebuild PHP to use the curl class.', E_USER_ERROR) ;\r
+ }\r
+\r
+ $this->m_handle = curl_init() ;\r
+ \r
+ $this->m_caseless = null ;\r
+ $this->m_header = null ;\r
+ $this->m_options = null ;\r
+ $this->m_status = null ;\r
+\r
+ if (!empty($theURL))\r
+ {\r
+ $this->setopt(CURLOPT_URL, $theURL) ; \r
+ }\r
+ $this->setopt(CURLOPT_HEADER, false) ;\r
+ $this->setopt(CURLOPT_RETURNTRANSFER, true) ;\r
+ }\r
+\r
+ /**\r
+ * Free the resources associated with the curl session.\r
+ *\r
+ * @link http://www.php.net/curl_close\r
+ */\r
+\r
+ function close()\r
+ {\r
+ curl_close($this->m_handle) ;\r
+ $this->m_handle = null ;\r
+ }\r
+\r
+ /**\r
+ * Execute the curl request and return the result.\r
+ *\r
+ * @link http://www.php.net/curl_exec\r
+ * @link http://www.php.net/curl_getinfo\r
+ * @link http://www.php.net/curl_errno\r
+ * @link http://www.php.net/curl_error\r
+ * @return string The contents of the page (or other interaction as defined by the\r
+ * settings of the various curl options).\r
+ */\r
+\r
+ function exec()\r
+ {\r
+ $theReturnValue = curl_exec($this->m_handle) ;\r
+ \r
+ $this->m_status = curl_getinfo($this->m_handle) ;\r
+ $this->m_status['errno'] = curl_errno($this->m_handle) ;\r
+ $this->m_status['error'] = curl_error($this->m_handle) ;\r
+ \r
+ //\r
+ // Parse out the http header (if any).\r
+ //\r
+\r
+ $this->m_header = null ;\r
+\r
+ if ($this->getOption(CURLOPT_HEADER))\r
+ {\r
+ $theArray = preg_split("/(\r\n){2,2}/", $theReturnValue, 2) ;\r
+\r
+ $this->parseHeader($theArray[0]) ;\r
+\r
+ return $theArray[1] ;\r
+ }\r
+\r
+ return $theReturnValue ;\r
+ }\r
+\r
+ /**\r
+ * Returns the parsed http header.\r
+ *\r
+ * @param string $theHeader [optional] the name of the header to be returned.\r
+ * The name of the header is case insensitive. If\r
+ * the header name is omitted the parsed header is\r
+ * returned. If the requested header doesn't exist\r
+ * false is returned.\r
+ * @returns mixed\r
+ */\r
+\r
+ function getHeader($theHeader=null)\r
+ {\r
+ if (empty($theHeader))\r
+ {\r
+ return $this->m_header ;\r
+ }\r
+ else\r
+ {\r
+ $theHeader = strtoupper($theHeader) ;\r
+ if (isset($this->m_caseless[$theHeader]))\r
+ {\r
+ return $this->m_header[$this->m_caseless[$theHeader]] ;\r
+ }\r
+ else\r
+ {\r
+ return false ;\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Returns the current setting of the request option. If no \r
+ * option has been set, it return null.\r
+ *\r
+ * @param integer the requested CURLOPT.\r
+ * @returns mixed\r
+ */\r
+\r
+ function getOption($theOption)\r
+ {\r
+ if (isset($this->m_options[$theOption]))\r
+ {\r
+ return $this->m_options[$theOption] ;\r
+ }\r
+\r
+ return null ;\r
+ }\r
+\r
+ /**\r
+ * Did the last curl exec operation have an error?\r
+ *\r
+ * @return mixed The error message associated with the error if an error \r
+ * occurred, false otherwise.\r
+ */\r
+\r
+ function hasError()\r
+ {\r
+ if (isset($this->m_status['error']))\r
+ {\r
+ return (empty($this->m_status['error']) ? false : $this->m_status['error']) ;\r
+ }\r
+ else\r
+ {\r
+ return false ;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Parse an HTTP header.\r
+ *\r
+ * As a side effect it stores the parsed header in the\r
+ * m_header instance variable. The header is stored as\r
+ * an associative array and the case of the headers \r
+ * as provided by the server is preserved and all\r
+ * repeated headers (pragma, set-cookie, etc) are grouped\r
+ * with the first spelling for that header\r
+ * that is seen.\r
+ *\r
+ * All headers are stored as if they COULD be repeated, so\r
+ * the headers are really stored as an array of arrays.\r
+ *\r
+ * @param string $theHeader The HTTP data header.\r
+ */\r
+\r
+ function parseHeader($theHeader)\r
+ {\r
+ $this->m_caseless = array() ;\r
+\r
+ $theArray = preg_split("/(\r\n)+/", $theHeader) ;\r
+\r
+ //\r
+ // Ditch the HTTP status line.\r
+ //\r
+\r
+ if (preg_match('/^HTTP/', $theArray[0]))\r
+ {\r
+ $theArray = array_slice($theArray, 1) ;\r
+ }\r
+\r
+ foreach ($theArray as $theHeaderString)\r
+ {\r
+ $theHeaderStringArray = preg_split("/\s*:\s*/", $theHeaderString, 2) ;\r
+\r
+ $theCaselessTag = strtoupper($theHeaderStringArray[0]) ;\r
+\r
+ if (!isset($this->m_caseless[$theCaselessTag]))\r
+ {\r
+ $this->m_caseless[$theCaselessTag] = $theHeaderStringArray[0] ;\r
+ }\r
+ \r
+ $this->m_header[$this->m_caseless[$theCaselessTag]][] = $theHeaderStringArray[1] ;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Return the status information of the last curl request.\r
+ *\r
+ * @param string $theField [optional] the particular portion\r
+ * of the status information desired.\r
+ * If omitted the array of status\r
+ * information is returned. If a non-existant\r
+ * status field is requested, false is returned.\r
+ * @returns mixed\r
+ */\r
+\r
+ function getStatus($theField=null)\r
+ {\r
+ if (empty($theField))\r
+ {\r
+ return $this->m_status ;\r
+ }\r
+ else\r
+ {\r
+ if (isset($this->m_status[$theField]))\r
+ {\r
+ return $this->m_status[$theField] ;\r
+ }\r
+ else\r
+ {\r
+ return false ;\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Set a curl option.\r
+ *\r
+ * @link http://www.php.net/curl_setopt\r
+ * @param mixed $theOption One of the valid CURLOPT defines.\r
+ * @param mixed $theValue the value of the curl option.\r
+ */\r
+\r
+ function setopt($theOption, $theValue)\r
+ {\r
+ curl_setopt($this->m_handle, $theOption, $theValue) ;\r
+ $this->m_options[$theOption] = $theValue ;\r
+ }\r
+}\r
+?>
\ No newline at end of file
--- /dev/null
+<?php
+/**
+ * repository_box class
+ * This is a subclass of repository class
+ *
+ * @author Dongsheng Cai
+ * @version 0.1 dev
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ */
+require_once($CFG->dirroot.'/repository/'.'lib.php');
+require_once($CFG->dirroot.'/repository/box/'.'boxlibphp5.php');
+
+class repository_box extends repository{
+
+ var $api_key = 'dmls97d8j3i9tn7av8y71m9eb55vrtj4';
+ var $box;
+ var $ticket;
+
+ public function __construct($repositoryid, $context = SITEID, $options = array()){
+ $repositoryid = -1;
+ parent::__construct($repositoryid, $context, $options);
+ if(!empty($options['api_key'])){
+ $this->api_key = $options['api_key'];
+ }
+ if(empty($this->options['auth_token'])) {
+ $this->box = new boxclient($this->api_key, '');
+ } else {
+ $this->box = new boxclient($this->api_key, $this->options['auth_token']);
+ }
+ }
+
+ public function get_listing($path = '0', $search = ''){
+ $ret = array();
+ if($this->box){
+ $tree = $this->box->getAccountTree();
+ if($tree) {
+ $filenames = $tree['file_name'];
+ $fileids = $tree['file_id'];
+ foreach ($filenames as $n=>$v){
+ $ret[] = array('name'=>$v, 'size'=>0, 'date'=>'',
+ 'url'=>'http://box.net/api/1.0/download/'.$this->options['auth_token'].'/'.$fileids[$n]);
+ }
+ return $ret;
+ } else {
+ return null;
+ }
+ }
+ }
+
+ public function print_login(){
+ if($this->box){
+ // get a ticket from box.net
+ $ticket_return = $this->box->getTicket();
+ if($this->box->isError()) {
+ echo $this->box->getErrorMsg();
+ } else {
+ $this->ticket = $ticket_return['ticket'];
+ }
+ // use the ticket to get a auth_token
+ // auth_token is the key to access the resources
+ // of box.net
+ // WARNING: this function won't return a auth_token
+ // if auth_token is not existed, this function will
+ // direct user to authentication page of box.net
+ // If the user has been authenticated, box.net will
+ // direct to a callback page (can be set in box.net)
+ // the call back page will obtain the auth_token
+ // ===============================================
+ // Because the authentication process will be done
+ // in box.net, so we need print a login link in this
+ // function instead a login screen.
+
+ if($this->ticket && ($this->options['auth_token'] == '')){
+ $this->box->getAuthToken($this->ticket);
+ return false;
+ } else {
+ echo 'Logged';
+ return true;
+ }
+ } else {
+ return false;
+ }
+ }
+
+ public function print_search(){
+ return false;
+ }
+}
+
+?>
--- /dev/null
+<?PHP // $Id$
+
+/////////////////////////////////////////////////////////////////////////////////
+/// Code fragment to define the version of repository plug-in (box.net)
+/////////////////////////////////////////////////////////////////////////////////
+
+$plugin->version = 2008062701; // The current plug-in version (Date: YYYYMMDDXX)
+$plugin->requires = 2007101509; // The current plug-in version (Date: YYYYMMDDXX)
+$plugin->cron = 3600; // Period for cron to check this plug-in (secs)
+
+?>