"MDL-13766, repository settings page"
[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;
15     public $type = 'boxnet';
17     public function __construct($repositoryid, $context = SITEID, $options = array()){
18         global $SESSION, $action;
19         $options['username']   = optional_param('username', '', PARAM_RAW);
20         $options['password']   = optional_param('password', '', PARAM_RAW);
21         $options['ticket']     = optional_param('ticket', '', PARAM_RAW);
22         $options['api_key']    = 'dmls97d8j3i9tn7av8y71m9eb55vrtj4';
23         // reset session
24         $reset = optional_param('reset', 0, PARAM_INT);
25         if(!empty($reset)) {
26             unset($SESSION->box_token);
27         }
28         // do login
29         if(!empty($options['username'])
30                     && !empty($options['password'])
31                     && !empty($options['ticket']) )
32         {
33             $this->box = new boxclient($options['api_key']);
34             try{
35                 $SESSION->box_token = $this->box->getAuthToken($options['ticket'], 
36                     $options['username'], $options['password']);
37             } catch (repository_exception $e) {
38                 throw $e;
39             }
40         }
41         // already logged
42         if(!empty($SESSION->box_token)) {
43             if(empty($this->box)) {
44                 $this->box = new boxclient($options['api_key'], $SESSION->box_token);
45             }
46             $options['auth_token'] = $SESSION->box_token;
47             if(empty($action)) {
48                 $action = 'list';
49             }
50         } else {
51             $this->box = new boxclient($options['api_key']);
52             if(!empty($action)) {
53                 $action = '';
54             }
55         }
56         parent::__construct($repositoryid, $context, $options);
57     }
59     public function get_login(){
60         global $DB;
61         $repository = new stdclass;
62         $repository->id = $this->repositoryid;
63         if ($entry = $DB->get_record('repository', $repository)) {
64             $ret->username = $entry->username;
65             $ret->password = $entry->password;
66         }
67         return $ret;
68     }
69     public function get_listing($path = '/', $search = ''){
70         global $CFG;
71         $list = array();
72         $ret  = array();
73         $tree = $this->box->getAccountTree();
74         if(!empty($tree)) {
75             $filenames = $tree['file_name'];
76             $fileids   = $tree['file_id'];
77             $filesizes = $tree['file_size'];
78             $filedates = $tree['file_date'];
79             foreach ($filenames as $n=>$v){
80                 // do search
81                 if(!empty($search)) {
82                     if(strstr($v, $search) !== false) {
83                         $list[] = array('title'=>$v, 
84                                 'size'=>$filesizes[$n],
85                                 'date'=>$filedates[$n],
86                                 'source'=>'http://box.net/api/1.0/download/'
87                                     .$this->options['auth_token'].'/'.$fileids[$n],
88                                 'thumbnail'=>$CFG->pixpath.'/f/'.mimeinfo('icon', $v));
89                     }
90                 } else {
91                     $list[] = array('title'=>$v, 
92                             'size'=>$filesizes[$n],
93                             'date'=>$filedates[$n],
94                             'source'=>'http://box.net/api/1.0/download/'
95                                 .$this->options['auth_token'].'/'.$fileids[$n],
96                             'thumbnail'=>$CFG->pixpath.'/f/'.mimeinfo('icon', $v));
97                 }
98             }
99             $this->listing = $list;
100             $ret['list']   = $list;
101             return $ret;
102         } else {
103             throw new repository_exception('nullfilelist', 'repository');
104         }
105     }
107     public function print_login(){
108         if(!empty($this->options['auth_token'])) {
109             if($this->options['ajax']){
110                 return $this->get_listing();
111             } else {
112                 // format file list and 
113                 // print list
114             }
115         } else {
116             $t = $this->box->getTicket();
117             if(empty($this->options['auth_token'])) {
118                 $ret = $this->get_login();
119                 $str = '';
120                 $str .= '<form id="moodle-repo-login">';
121                 $str .= '<input type="hidden" name="ticket" value="'.
122                     $t['ticket'].'" />';
123                 $str .= '<input type="hidden" name="id" value="'.$this->repositoryid.'" />';
124                 $str .= '<label for="box_username">Username: <label><br/>';
125                 $str .= '<input type="text" id="box_username" name="username" value="'.$ret->username.'" />';
126                 $str .= '<br/>';
127                 $str .= '<label for="box_password">Password: <label><br/>';
128                 $str .= '<input type="password" value="'.$ret->password.'" id="box_password" name="password" /><br/>';
129                 $str .= '<input type="button" onclick="repository_client.login()" value="Go" />';
130                 $str .= '</form>';
131                 if($this->options['ajax']){
132                     $ret = array();
133                     $ret['l'] = $str;
134                     return $ret;
135                 } else {
136                     echo $str;
137                 }
138             }
139         }
140     }
142     public function print_search(){
143         return false;
144     }
147 ?>