7b967ba8085b63adfec3c776c8b9c3ab80c38bb8
[moodle.git] / repository / boxnet / repository.class.php
1 <?php
2 /**
3  * repository_boxnet class
4  * This is a subclass of repository class
5  *
6  * @author Dongsheng Cai
7  * @version 0.1 dev
8  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9  */
11 require_once($CFG->dirroot.'/repository/boxnet/'.'boxlibphp5.php');
13 class repository_boxnet extends repository{
14     private $box;
16     public function __construct($repositoryid, $context = SITEID, $options = array()){
17         global $SESSION, $action;
18         $options['username']   = optional_param('username', '', PARAM_RAW);
19         $options['password']   = optional_param('password', '', PARAM_RAW);
20         $options['ticket']     = optional_param('ticket', '', PARAM_RAW);
21         $options['api_key']    = 'dmls97d8j3i9tn7av8y71m9eb55vrtj4';
22         // reset session
23         $reset = optional_param('reset', 0, PARAM_INT);
24         if(!empty($reset)) {
25             unset($SESSION->box_token);
26         }
27         // do login
28         if(!empty($options['username'])
29                     && !empty($options['password'])
30                     && !empty($options['ticket']) )
31         {
32             $this->box = new boxclient($options['api_key']);
33             try{
34                 $SESSION->box_token = $this->box->getAuthToken($options['ticket'], 
35                     $options['username'], $options['password']);
36             } catch (repository_exception $e) {
37                 throw $e;
38             }
39         }
40         // already logged
41         if(!empty($SESSION->box_token)) {
42             if(empty($this->box)) {
43                 $this->box = new boxclient($options['api_key'], $SESSION->box_token);
44             }
45             $options['auth_token'] = $SESSION->box_token;
46             if(empty($action)) {
47                 $action = 'list';
48             }
49         } else {
50             $this->box = new boxclient($options['api_key']);
51             if(!empty($action)) {
52                 $action = '';
53             }
54         }
55         parent::__construct($repositoryid, $context, $options);
56     }
58     public function get_listing($path = '/', $search = ''){
59         global $CFG;
60         $list = array();
61         $ret  = array();
62         $tree = $this->box->getAccountTree();
63         if(!empty($tree)) {
64             $filenames = $tree['file_name'];
65             $fileids   = $tree['file_id'];
66             $filesizes = $tree['file_size'];
67             $filedates = $tree['file_date'];
68             foreach ($filenames as $n=>$v){
69                 // do search
70                 if(!empty($search)) {
71                     if(strstr($v, $search) !== false) {
72                         $list[] = array('title'=>$v, 
73                                 'size'=>$filesizes[$n],
74                                 'date'=>$filedates[$n],
75                                 'source'=>'http://box.net/api/1.0/download/'
76                                     .$this->options['auth_token'].'/'.$fileids[$n],
77                                 'thumbnail'=>$CFG->pixpath.'/f/'.mimeinfo('icon', $v));
78                     }
79                 } else {
80                     $list[] = array('title'=>$v, 
81                             'size'=>$filesizes[$n],
82                             'date'=>$filedates[$n],
83                             'source'=>'http://box.net/api/1.0/download/'
84                                 .$this->options['auth_token'].'/'.$fileids[$n],
85                             'thumbnail'=>$CFG->pixpath.'/f/'.mimeinfo('icon', $v));
86                 }
87             }
88             $this->listing = $list;
89             $ret['list']   = $list;
90             return $ret;
91         } else {
92             throw new repository_exception('nullfilelist', 'repository');
93         }
94     }
96     public function print_login(){
97         if(!empty($this->options['auth_token'])) {
98             if($this->options['ajax']){
99                 return $this->get_listing();
100             } else {
101                 // format file list and 
102                 // print list
103             }
104         } else {
105             $t = $this->box->getTicket();
106             if(empty($this->options['auth_token'])) {
107                 $str = '';
108                 $str .= '<form id="moodle-repo-login">';
109                 $str .= '<input type="hidden" name="ticket" value="'.
110                     $t['ticket'].'" />';
111                 $str .= '<input type="hidden" name="id" value="'.$this->repositoryid.'" />';
112                 $str .= '<label for="box_username">Username: <label><br/>';
113                 $str .= '<input type="text" id="box_username" name="username" />';
114                 $str .= '<br/>';
115                 $str .= '<label for="box_password">Password: <label><br/>';
116                 $str .= '<input type="password" id="box_password" name="password" /><br/>';
117                 $str .= '<input type="button" onclick="repository_client.login()" value="Go" />';
118                 $str .= '</form>';
119                 if($this->options['ajax']){
120                     $ret = array();
121                     $ret['l'] = $str;
122                     return $ret;
123                 } else {
124                     echo $str;
125                 }
126             }
127         }
128     }
130     public function print_search(){
131         return false;
132     }
135 ?>