MDL-15991 - made box.net client library support change to curl integration with files api
[moodle.git] / portfolio / type / boxnet / lib.php
CommitLineData
b3abd2c6 1<?php
67a87e7d 2require_once($CFG->libdir.'/filelib.php');
3require_once($CFG->dirroot.'/repository/boxnet/boxlibphp5.php');
4
5class portfolio_plugin_boxnet extends portfolio_plugin_base {
6
7 private $boxclient;
8 private $ticket;
9 private $authtoken;
10 private $workdir;
11 private $folders;
12
67a87e7d 13 public function prepare_package($tempdir) {
14 $this->workdir = $tempdir;
15 return true; // don't do anything else for this plugin, we want to send all files as they are.
16 }
17
18 public function send_package() {
19 $ret = array();
20 foreach (get_directory_list($this->workdir) as $file) {
21 $file = $this->workdir . '/' . $file;
22 $ret[] = $this->boxclient->uploadFile(
23 array(
24 'file' => $file,
25 'folder_id' => $this->get_export_config('folder')
26 )
27 );
28 }
29 if ($this->boxclient->isError()) {
30 return false;
31 }
32 return is_array($ret) && !empty($ret);
33 }
34
35 public function set_export_config($config) {
36 parent::set_export_config($config);
37 if (array_key_exists('newfolder', $config) && !empty($config['newfolder'])) {
38 if (!$created = $this->boxclient->createFolder($config['newfolder'])) {
39 portfolio_exporter::raise_error('foldercreatefailed', 'portfolio_boxnet');
40 }
41 $this->folders[$created['folder_id']] = $created['folder_type'];
42 parent::set_export_config(array('folder' => $created['folder_id']));
43 }
44 }
45
46 public function get_export_summary() {
47 $allfolders = $this->get_folder_list();
48 return array(
49 get_string('targetfolder', 'portfolio_boxnet') => $allfolders[$this->get_export_config('folder')]
50 );
51 }
52
53 public function get_continue_url() {
54 // @todo this was a *guess* based on what urls I got clicking around the interface.
55 // the #0:f:<folderid> part seems fragile...
56 // but I couldn't find a documented permalink scheme.
57 return 'http://box.net/files#0:f:' . $this->get_export_config('folder');
58 }
59
60 public function expected_time($callertime) {
61 return $callertime;
62 }
63
64 public static function has_admin_config() {
65 return true;
66 }
67
68 public static function get_allowed_config() {
69 return array('apikey');
70 }
71
72 public function has_export_config() {
73 return true;
74 }
75
76 public function get_allowed_user_config() {
77 return array('authtoken', 'authtokenctime');
78 }
79
80 public function get_allowed_export_config() {
81 return array('folder', 'newfolder');
82 }
83
84 public function export_config_form(&$mform) {
85 $folders = $this->get_folder_list();
86 $strrequired = get_string('required');
87 $mform->addElement('text', 'plugin_newfolder', get_string('newfolder', 'portfolio_boxnet'));
88 if (empty($folders)) {
89 $mform->addRule('plugin_newfolder', $strrequired, 'required', null, 'client');
90 }
91 else {
92 $mform->addElement('select', 'plugin_folder', get_string('existingfolder', 'portfolio_boxnet'), $folders);
93 }
94 }
95
96 public function export_config_validation($data) {
97 if ((!array_key_exists('plugin_folder', $data) || empty($data['plugin_folder']))
98 && (!array_key_exists('plugin_newfolder', $data) || empty($data['plugin_newfolder']))) {
99 return array(
100 'plugin_folder' => get_string('notarget', 'portfolio_boxnet'),
101 'plugin_newfolder' => get_string('notarget', 'portfolio_boxnet'));
102 }
103 $allfolders = $this->get_folder_list();
104 if (in_array($data['plugin_newfolder'], $allfolders)) {
105 return array('plugin_newfolder' => get_string('folderclash', 'portfolio_boxnet'));
106 }
107 }
108
109 public function admin_config_form(&$mform) {
110 $strrequired = get_string('required');
111 $mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_boxnet'));
112 $mform->addRule('apikey', $strrequired, 'required', null, 'client');
113 }
114
115 public function steal_control($stage) {
116 if ($stage != PORTFOLIO_STAGE_CONFIG) {
117 return false;
118 }
119 if ($this->authtoken) {
120 return false;
121 }
122 if (!$this->ensure_ticket()) {
123 portfolio_exporter::raise_error('noticket', 'portfolio_boxnet');
124 }
125 $token = $this->get_user_config('authtoken', $this->get('user')->id);
126 $ctime= $this->get_user_config('authtokenctime', $this->get('user')->id);
127 if (!empty($token) && (($ctime + 60*60*20) > time())) {
128 $this->authtoken = $token;
129 $this->boxclient->auth_token = $token;
130 return false;
131 }
132 return 'http://www.box.net/api/1.0/auth/'.$this->ticket;
133 }
134
135 public function post_control($stage, $params) {
136 if ($stage != PORTFOLIO_STAGE_CONFIG) {
137 return;
138 }
139 if (!array_key_exists('auth_token', $params) || empty($params['auth_token'])) {
140 portfolio_exporter::raise_error('noauthtoken', 'portfolio_boxnet');
141 }
142 $this->authtoken = $params['auth_token'];
143 $this->boxclient->auth_token = $this->authtoken;
144 $this->set_user_config(array('authtoken' => $this->authtoken, 'authtokenctime' => time()), $this->get('user')->id);
145 }
146
147 private function ensure_ticket() {
148 if (!empty($this->boxclient)) {
149 return true;
150 }
151 $this->boxclient = new boxclient($this->get_config('apikey'), '');
152 $ticket_return = $this->boxclient->getTicket();
153 if ($this->boxclient->isError() || empty($ticket_return)) {
154 portfolio_exporter::raise_error('noticket', 'portfolio_boxnet');
155 }
156 $this->ticket = $ticket_return['ticket'];
8801ab06 157 return $this->ticket;
67a87e7d 158 }
159
160 private function get_folder_list() {
161 if (!empty($this->folders)) {
162 return $this->folders;
163 }
164 if (empty($this->ticket)
165 || empty($this->authtoken)
166 || empty($this->boxclient)) {
167 // if we don't have these we're pretty much screwed
168 portfolio_exporter::raise_error('folderlistfailed', 'portfolio_boxnet');
169 return false;
170 }
171 $rawfolders = $this->boxclient->getAccountTree();
172 if ($this->boxclient->isError()) {
173 portfolio_exporter::raise_error('folderlistfailed', 'portfolio_boxnet');
174 }
175 if (!is_array($rawfolders)) {
176 return false;
177 }
178 $folders = array();
179 foreach ($rawfolders['folder_id'] as $key => $id) {
180 if (empty($id)) {
181 continue;
182 }
183 $name = $rawfolders['folder_name'][$key];
184 if (!empty($rawfolders['shared'][$key])) {
185 $name .= ' (' . get_string('sharedfolder', 'portfolio_boxnet') . ')';
186 }
187 $folders[$id] = $name;
188 }
189 $this->folders = $folders;
190 return $folders;
191 }
192
193 public function instance_sanity_check() {
194 if (!$this->get_config('apikey')) {
195 return 'err_noapikey';
196 }
197 //@TODO see if we can verify the api key without actually getting an authentication token
198 }
2f68e760 199
200 public static function allows_multiple() {
201 return false;
202 }
67a87e7d 203}