Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/boxlib.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00035 class boxclient {
00037     public $auth_token = '';
00039     private $_box_api_url = 'http://box.net/api/1.0/rest';
00040     private $_box_api_upload_url = 'http://upload.box.net/api/1.0/upload';
00041     private $_error_code = '';
00042     private $_error_msg = '';
00044     private $debug = false;
00045 
00051     public function __construct($api_key, $auth_token = '', $debug = false) {
00052         $this->api_key    = $api_key;
00053         $this->auth_token = $auth_token;
00054         if (!empty($debug)) {
00055             $this->debug = true;
00056         } else {
00057             $this->debug = false;
00058         }
00059     }
00067     function makeRequest($method, $params = array()) {
00068         $this->_clearErrors();
00069         $c = new curl(array('debug'=>$this->debug, 'cache'=>true, 'module_cache'=>'repository'));
00070         try {
00071             if ($method == 'upload'){
00072                 $request = $this->_box_api_upload_url.'/'.
00073                     $this->auth_token.'/'.$params['folder_id'];
00074                 $xml = $c->post($request, $params);
00075             }else{
00076                 $args = array();
00077                 $xml = $c->get($this->_box_api_url, $params);
00078             }
00079             $xml_parser = xml_parser_create();
00080             // set $data here
00081             xml_parse_into_struct($xml_parser, $xml, $data);
00082             xml_parser_free($xml_parser);
00083         } catch (moodle_exception $e) {
00084             $this->setError(0, 'connection time-out or invalid url');
00085             return false;
00086         }
00087         return $data;
00088     }
00093     function getTicket($params = array()) {
00094         $params['api_key'] = $this->api_key;
00095         $params['action']  = 'get_ticket';
00096         $ret_array = array();
00097         $data = $this->makeRequest('action=get_ticket', $params);
00098         if ($this->_checkForError($data)) {
00099             return false;
00100         }
00101         foreach ($data as $a) {
00102             switch ($a['tag']) {
00103             case 'STATUS':
00104                 $ret_array['status'] = $a['value'];
00105                 break;
00106             case 'TICKET':
00107                 $ret_array['ticket'] = $a['value'];
00108                 break;
00109             }
00110         }
00111         return $ret_array;
00112     }
00113 
00133     function getAuthToken($ticket, $username, $password) {
00134         $c = new curl(array('debug'=>$this->debug));
00135         $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>0));
00136         $param =  array(
00137             'login_form1'=>'',
00138             'login'=>$username,
00139             'password'=>$password,
00140             'dologin'=>1,
00141             '__login'=>1
00142             );
00143         try {
00144             $ret = $c->post('http://www.box.net/api/1.0/auth/'.$ticket, $param);
00145         } catch (moodle_exception $e) {
00146             $this->setError(0, 'connection time-out or invalid url');
00147             return false;
00148         }
00149         $header = $c->getResponse();
00150         if(empty($header['location'])) {
00151             throw new repository_exception('invalidpassword', 'repository_boxnet');
00152         }
00153         $location = $header['location'];
00154         preg_match('#auth_token=(.*)$#i', $location, $matches);
00155         $auth_token = $matches[1];
00156         if(!empty($auth_token)) {
00157             $this->auth_token = $auth_token;
00158             return $auth_token;
00159         } else {
00160             throw new repository_exception('invalidtoken', 'repository_boxnet');
00161         }
00162     }
00168     function getfiletree($path, $params = array()) {
00169         $this->_clearErrors();
00170         $params['auth_token'] = $this->auth_token;
00171         $params['folder_id']  = 0;
00172         $params['api_key']    = $this->api_key;
00173         $params['action']     = 'get_account_tree';
00174         $params['onelevel']   = 1;
00175         $params['params[]']   = 'nozip';
00176         $c = new curl(array('debug'=>$this->debug, 'cache'=>true, 'module_cache'=>'repository'));
00177         try {
00178             $args = array();
00179             $xml = $c->get($this->_box_api_url, $params);
00180         } catch (Exception $e){
00181         }
00182         $ret = array();
00183         $o = simplexml_load_string(trim($xml));
00184         if($o->status == 'listing_ok') {
00185             $tree = $o->tree->folder;
00186             $this->buildtree($tree, $ret);
00187         }
00188         return $ret;
00189     }
00190 
00195     function buildtree($sax, &$tree){
00196         $sax = (array)$sax;
00197         $count = 0;
00198         foreach($sax as $k=>$v){
00199             if($k == 'folders'){
00200                 $o = $sax[$k];
00201                 foreach($o->folder as $z){
00202                     $tmp = array('title'=>(string)$z->attributes()->name,
00203                         'size'=>0, 'date'=>userdate(time()),
00204                         'thumbnail'=>'http://www.box.net/img/small_folder_icon.gif',
00205                         'path'=>array('name'=>(string)$z->attributes()->name, 'path'=>(int)$z->attributes()->id));
00206                     $tmp['children'] = array();
00207                     $this->buildtree($z, $tmp['children']);
00208                     $tree[] = $tmp;
00209                 }
00210             } elseif ($k == 'files') {
00211                 $val = $sax[$k]->file;
00212                 foreach($val as $file){
00213                     $thumbnail = (string)$file->attributes()->thumbnail;
00214                     if (!preg_match('#^(?:http://)?([^/]+)#i', $thumbnail)) {
00215                         $thumbnail =  'http://www.box.net'.$thumbnail;
00216                     }
00217                     $tmp = array('title'=>(string)$file->attributes()->file_name,
00218                         'size'=>display_size((int)$file->attributes()->size),
00219                         'thumbnail'=>$thumbnail,
00220                         'date'=>userdate((int)$file->attributes()->updated),
00221                         'source'=>'http://box.net/api/1.0/download/'
00222                             .$this->auth_token.'/'.(string)$file->attributes()->id,
00223                         'url'=>(string)$file->attributes()->shared_link);
00224                     $tree[] = $tmp;
00225                 }
00226             }
00227             $count++;
00228         }
00229     }
00234     function getAccountTree($params = array()) {
00235         $params['auth_token'] = $this->auth_token;
00236         $params['folder_id']  = 0;
00237         $params['api_key']    = $this->api_key;
00238         $params['action']     = 'get_account_tree';
00239         $params['onelevel']   = 1;
00240         $params['params[]']   = 'nozip';
00241         $ret_array = array();
00242         $data = $this->makeRequest('action=get_account_tree', $params);
00243         if ($this->_checkForError($data)) {
00244             return false;
00245         }
00246         $tree_count=count($data);
00247         $entry_count = 0;
00248         for ($i=0; $i<$tree_count; $i++) {
00249             $a = $data[$i];
00250             switch ($a['tag'])
00251             {
00252             case 'FOLDER':
00253                 if (@is_array($a['attributes'])) {
00254                     $ret_array['folder_id'][$i] = $a['attributes']['ID'];
00255                     $ret_array['folder_name'][$i] = $a['attributes']['NAME'];
00256                     $ret_array['shared'][$i] = $a['attributes']['SHARED'];
00257                 }
00258                 break;
00259 
00260             case 'FILE':
00261                 if (@is_array($a['attributes'])) {
00262                     $ret_array['file_id'][$i] = $a['attributes']['ID'];
00263                     @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
00264                     @$ret_array['file_keyword'][$i] = $a['attributes']['KEYWORD'];
00265                     @$ret_array['file_size'][$i] = display_size($a['attributes']['SIZE']);
00266                     @$ret_array['file_date'][$i] = userdate($a['attributes']['UPDATED']);
00267                     if (preg_match('#^(?:http://)?([^/]+)#i', $a['attributes']['THUMBNAIL'])) {
00268                         @$ret_array['thumbnail'][$i] =  $a['attributes']['THUMBNAIL'];
00269                     } else {
00270                         @$ret_array['thumbnail'][$i] =  'http://www.box.net'.$a['attributes']['THUMBNAIL'];
00271                     }
00272                     $entry_count++;
00273                 }
00274                 break;
00275             }
00276         }
00277         return $ret_array;
00278     }
00279 
00285     function CreateFolder($new_folder_name, $params = array()) {
00286         $params['auth_token'] =  $this->auth_token;
00287         $params['api_key']    = $this->api_key;
00288         $params['action']     = 'create_folder';
00289         $params['name']       = $new_folder_name;
00290         $defaults = array(
00291             'parent_id'  => 0, //Set to '0' by default. Change to create within sub-folder.
00292             'share'     => 1, //Set to '1' by default. Set to '0' to make folder private.
00293         );
00294         foreach ($defaults as $key => $value) {
00295             if (!array_key_exists($key, $params)) {
00296                 $params[$key] = $value;
00297             }
00298         }
00299 
00300         $ret_array = array();
00301         $data = $this->makeRequest('action=create_folder', $params);
00302         if ($this->_checkForError($data)) {
00303             return false;
00304         }
00305         foreach ($data as $a) {
00306             if (!empty($a['value'])) {
00307                 switch ($a['tag']) {
00308 
00309                 case 'FOLDER_ID':
00310                     $ret_array['folder_id'] = $a['value'];
00311                     break;
00312 
00313                 case 'FOLDER_NAME':
00314                     $ret_array['folder_name'] = $a['value'];
00315                     break;
00316 
00317                 case 'FOLDER_TYPE_ID':
00318                     $ret_array['folder_type_id'] = $a['value'];
00319                     break;
00320 
00321                 case 'SHARED':
00322                     $ret_array['shared'] = $a['value'];
00323                     break;
00324 
00325                 case 'PASSWORD':
00326                     $ret_array['password'] = $a['value'];
00327                     break;
00328                 }
00329             } else {
00330                 $ret_array[strtolower($a['tag'])] = null;
00331             }
00332         }
00333         return $ret_array;
00334     }
00335 
00341     function UploadFile ($params = array()) {
00342         $params['auth_token'] = $this->auth_token;
00343         // this param should be the full path of the file
00344         $params['new_file1']  = $params['file'];
00345         unset($params['file']);
00346         $defaults = array(
00347             'folder_id' => 0, //Set to '0' by default. Change to create within sub-folder.
00348             'share'     => 1, //Set to '1' by default. Set to '0' to make folder private.
00349         );
00350         foreach ($defaults as $key => $value) {
00351             if (!array_key_exists($key, $params)) {
00352                 $params[$key] = $value;
00353             }
00354         }
00355         $ret_array = array();
00356         $entry_count = 0;
00357         $data = $this->makeRequest('upload', $params);
00358         if ($this->_checkForError($data)) {
00359             return false;
00360         }
00361         for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) {
00362             $a = $data[$i];
00363             switch ($a['tag']) {
00364             case 'STATUS':
00365                 $ret_array['status'] = $a['value'];
00366                 break;
00367 
00368             case 'FILE':
00369                 if (is_array($a['attributes'])) {
00370                     @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
00371                     @$ret_array['id'][$i] = $a['attributes']['ID'];
00372                     @$ret_array['folder_name'][$i] = $a['attributes']['FOLDER_NAME'];
00373                     @$ret_array['error'][$i] = $a['attributes']['ERROR'];
00374                     @$ret_array['public_name'][$i] = $a['attributes']['PUBLIC_NAME'];
00375                     $entry_count++;
00376                 }
00377                 break;
00378             }
00379         }
00380 
00381         return $ret_array;
00382     }
00388     function RenameFile($fileid, $newname) {
00389         $params = array(
00390             'api_key'    => $this->api_key,
00391             'auth_token' => $this->auth_token,
00392             'action'     => 'rename',
00393             'target'     => 'file',
00394             'target_id'  => $fileid,
00395             'new_name'   => $newname,
00396         );
00397         $data = $this->makeRequest('action=rename', $params);
00398         if ($this->_checkForError($data)) {
00399             return false;
00400         }
00401         foreach ($data as $a) {
00402             switch ($a['tag']) {
00403                 case 'STATUS':
00404                     if ($a['value'] == 's_rename_node') {
00405                         return true;
00406                     }
00407             }
00408         }
00409         return false;
00410     }
00411 
00418     function RegisterUser($params = array()) {
00419         $params['api_key'] = $this->api_key;
00420         $params['action']  = 'register_new_user';
00421         $params['login']   = $_REQUEST['login'];
00422         $params['password'] = $_REQUEST['password'];
00423         $ret_array = array();
00424         $data = $this->makeRequest('action=register_new_user', $params);
00425         if ($this->_checkForError($data)) {
00426             return false;
00427         }
00428         foreach ($data as $a) {
00429             switch ($a['tag']) {
00430             case 'STATUS':
00431                 $ret_array['status'] = $a['value'];
00432                 break;
00433 
00434             case 'AUTH_TOKEN':
00435                 $ret_array['auth_token'] = $a['value'];
00436                 break;
00437 
00438             case 'LOGIN':
00439                 $ret_array['login'] = $a['value'];
00440                 break;
00441             case 'SPACE_AMOUNT':
00442                 $ret_array['space_amount'] = $a['value'];
00443                 break;
00444             case 'SPACE_USED':
00445                 $ret_array['space_used'] = $a['value'];
00446                 break;
00447             }
00448         }
00449 
00450         return $ret_array;
00451     }
00452 
00462     function AddTag($tag, $id, $target_type, $params = array()) {
00463         $params['auth_token'] = $this->auth_token;
00464         $params['api_key']    = $this->api_key;
00465         $params['action']     = 'add_to_tag';
00466         $params['target']     = $target_type; // File or folder
00467         $params['target_id']  = $id; // Set to ID of file or folder
00468         $params['tags[]']     = $tag;
00469         $ret_array = array();
00470         $data = $this->makeRequest('action=add_to_tag', $params);
00471         if ($this->_checkForError($data)) {
00472             return false;
00473         }
00474         foreach ($data as $a) {
00475             switch ($a['tag']) {
00476             case 'STATUS':
00477                 $ret_array['status'] = $a['value'];
00478 
00479                 break;
00480             }
00481         }
00482         return $ret_array;
00483     }
00484 
00496     function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) {
00497         $params['auth_token'] = $this->auth_token;
00498         $params['api_key']    = $this->api_key;
00499         $params['action']     = 'public_share';
00500         $params['target']     = $target_type;
00501         $params['target_id']  = $id;
00502         $params['password']   =  $password;
00503         $params['message']    = $message;
00504         $params['emails']     = $emails;
00505         $ret_array = array();
00506         $data = $this->makeRequest('action=public_share', $params);
00507         if ($this->_checkForError($data)) {
00508             return false;
00509         }
00510         foreach ($data as $a) {
00511             switch ($a['tag']) {
00512             case 'STATUS':
00513                 $ret_array['status'] = $a['value'];
00514                 break;
00515             case 'PUBLIC_NAME':
00516                 $ret_array['public_name'] = $a['value'];
00517                 break;
00518             }
00519         }
00520 
00521         return $ret_array;
00522     }
00529     function GetFriends ($params = array()) {
00530         $params['auth_token'] = $this->auth_token;
00531         $params['action']     = 'get_friends';
00532         $params['api_key']    = $this->api_key;
00533         $params['params[]']   = 'nozip';
00534         $ret_array = array();
00535         $data = $this->makeRequest('action=get_friends', $params);
00536         if ($this->_checkForError($data)) {
00537             return false;
00538         }
00539         foreach ($data as $a) {
00540             switch ($a['tag']) {
00541             case 'NAME':
00542                 $ret_array['name'] = $a['value'];
00543                 break;
00544             case 'EMAIL':
00545                 $ret_array['email'] = $a['value'];
00546                 break;
00547             case 'ACCEPTED':
00548                 $ret_array['accepted'] = $a['value'];
00549                 break;
00550             case 'AVATAR_URL':
00551                 $ret_array['avatar_url'] = $a['value'];
00552                 break;
00553             case 'ID':
00554                 $ret_array['id'] = $a['value'];
00555                 break;
00556             case 'URL':
00557                 $ret_array['url'] = $a['value'];
00558                 break;
00559             case 'STATUS':
00560                 $ret_array['status'] = $a['value'];
00561                 break;
00562             }
00563         }
00564         return $ret_array;
00565     }
00566 
00573     function Logout($params = array()) {
00574         $params['auth_token'] = $this->auth_token;
00575         $params['api_key']    = $this->api_key;
00576         $params['action']     = 'logout';
00577         $ret_array = array();
00578         $data = $this->makeRequest('action=logout', $params);
00579         if ($this->_checkForError($data)) {
00580             return false;
00581         }
00582         foreach ($data as $a) {
00583             switch ($a['tag']) {
00584             case 'ACTION':
00585                 $ret_array['logout'] = $a['value'];
00586 
00587                 break;
00588             }
00589             return $ret_array;
00590         }
00591     }
00596     function _checkForError($data) {
00597         if ($this->_error_msg != '') {
00598             return true;
00599         }
00600         if (@$data[0]['attributes']['STAT'] == 'fail') {
00601             $this->_error_code = $data[1]['attributes']['CODE'];
00602             $this->_error_msg = $data[1]['attributes']['MSG'];
00603             return true;
00604         }
00605         return false;
00606     }
00607 
00611     public function isError() {
00612         if  ($this->_error_msg != '') {
00613             return true;
00614         }
00615         return false;
00616     }
00620     public function setError($code = 0, $msg){
00621         $this->_error_code = $code;
00622         $this->_error_msg  = $msg;
00623     }
00627     function getErrorMsg() {
00628         return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>';
00629     }
00633     function getErrorCode() {
00634         return $this->_error_code;
00635     }
00639     function _clearErrors() {
00640         $this->_error_code = '';
00641         $this->_error_msg = '';
00642     }
00643 
00644 }
 All Data Structures Namespaces Files Functions Variables Enumerations