PageRenderTime 41ms CodeModel.GetById 15ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

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