Updated the HEAD build version to 20091212
[moodle.git] / portfolio / mahara / lib.php
CommitLineData
254f2d05 1<?php
2
3define('PORTFOLIO_MAHARA_ERR_NETWORKING_OFF', 'err_networkingoff');
4define('PORTFOLIO_MAHARA_ERR_NOHOSTS', 'err_nomnethosts');
aae8cfdc 5define('PORTFOLIO_MAHARA_ERR_INVALIDHOST', 'err_invalidhost');
7254f56e 6define('PORTFOLIO_MAHARA_ERR_NOMNETAUTH', 'err_nomnetauth');
254f2d05 7
8require_once($CFG->dirroot . '/lib/portfoliolib.php');
9require_once($CFG->dirroot . '/mnet/lib.php');
10
11define('PORTFOLIO_MAHARA_QUEUE', PORTFOLIO_TIME_HIGH);
12define('PORTFOLIO_MAHARA_IMMEDIATE', PORTFOLIO_TIME_MODERATE);
13
14class portfolio_plugin_mahara extends portfolio_plugin_pull_base {
15
16 private $hosts; // used in the admin config form
17 private $mnethost; // privately set during export from the admin config value (mnethostid)
18 private $hostrecord; // the host record that corresponds to the peer
19 private $token; // during-transfer token
20 private $sendtype; // whatever mahara has said it can handle (immediate or queued)
21 private $filesmanifest; // manifest of files to send to mahara (set during prepare_package and sent later)
2f6bd2a9 22 private $totalsize; // total size of all included files added together
14cb94eb 23 private $continueurl; // if we've been sent back a specific url to continue to (eg folder id)
254f2d05 24
0f71f48b 25 public static function get_name() {
26 return get_string('pluginname', 'portfolio_mahara');
27 }
28
254f2d05 29 public static function get_allowed_config() {
30 return array('mnethostid');
31 }
32
33 public static function supported_formats() {
34 return array(PORTFOLIO_FORMAT_FILE);
59dd457e
PL
35 // TODO remove above line once leap over mnet is tested
36 return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_LEAP2A);
254f2d05 37 }
38
39 public function expected_time($callertime) {
40 if ($this->sendtype == PORTFOLIO_MAHARA_QUEUE) {
41 return PORTFOLIO_TIME_FORCEQUEUE;
42 }
43 return $callertime;
44 }
45
46 public static function has_admin_config() {
47 return true;
48 }
49
50 public function admin_config_form(&$mform) {
254f2d05 51 $strrequired = get_string('required');
52 $hosts = self::get_mnet_hosts(); // this is called by sanity check but it's ok because it's cached
53 foreach ($hosts as $host) {
54 $hosts[$host->id] = $host->name;
55 }
56 $mform->addElement('select', 'mnethostid', get_string('mnethost', 'portfolio_mahara'), $hosts);
57 $mform->addRule('mnethostid', $strrequired, 'required', null, 'client');
58 }
59
aae8cfdc 60 public function instance_sanity_check() {
61 // make sure the host record exists since we don't have referential integrity
b0482154 62 if (!is_enabled_auth('mnet')) {
63 return PORTFOLIO_MAHARA_ERR_NOMNETAUTH;
64 }
aae8cfdc 65 try {
66 $this->ensure_mnethost();
67 }
68 catch (portfolio_exception $e) {
69 return PORTFOLIO_MAHARA_ERR_INVALIDHOST;
70 }
71 // make sure we have the right services
72 $hosts = $this->get_mnet_hosts();
73 if (!array_key_exists($this->get_config('mnethostid'), $hosts)) {
74 return PORTFOLIO_MAHARA_ERR_INVALIDHOST;
75 }
76 return 0;
77 }
254f2d05 78
79 public static function plugin_sanity_check() {
254f2d05 80 global $CFG, $DB;
81 $errorcode = 0;
82 if (!isset($CFG->mnet_dispatcher_mode) || $CFG->mnet_dispatcher_mode != 'strict') {
83 $errorcode = PORTFOLIO_MAHARA_ERR_NETWORKING_OFF;
84 }
7254f56e 85 if (!is_enabled_auth('mnet')) {
86 $errorcode = PORTFOLIO_MAHARA_ERR_NOMNETAUTH;
87 }
254f2d05 88 if (!self::get_mnet_hosts()) {
89 $errorcode = PORTFOLIO_MAHARA_ERR_NOHOSTS;
90 }
254f2d05 91 return $errorcode;
92 }
93
94 private static function get_mnet_hosts() {
95 global $DB, $CFG;
96 static $hosts;
97 if (isset($this) && is_object($this) && isset($this->hosts)) {
98 return $this->hosts;
99 } else if (!isset($this) && isset($hosts)) {
100 return $hosts;
101 }
102 $hosts = $DB->get_records_sql(' SELECT
103 h.id,
104 h.wwwroot,
105 h.ip_address,
106 h.name,
107 h.public_key,
108 h.public_key_expires,
109 h.transport,
110 h.portno,
111 h.last_connect_time,
112 h.last_log_id,
113 h.applicationid,
114 a.name as app_name,
115 a.display_name as app_display_name,
116 a.xmlrpc_server_url
117 FROM {mnet_host} h
118 JOIN {mnet_application} a ON h.applicationid=a.id
119 JOIN {mnet_host2service} hs1 ON hs1.hostid = h.id
120 JOIN {mnet_service} s1 ON hs1.serviceid = s1.id
121 JOIN {mnet_host2service} hs2 ON hs2.hostid = h.id
122 JOIN {mnet_service} s2 ON hs2.serviceid = s2.id
123 JOIN {mnet_host2service} hs3 ON hs3.hostid = h.id
124 JOIN {mnet_service} s3 ON hs3.serviceid = s3.id
125 WHERE
126 h.id <> ? AND
127 h.deleted = 0 AND
128 a.name = ? AND
129 s1.name = ? AND hs1.publish = ? AND
130 s2.name = ? AND hs2.subscribe = ? AND
8a709ecf
PL
131 s3.name = ? AND hs3.subscribe = ? AND
132 s3.name = ? AND hs3.publish = ?',
133 array($CFG->mnet_localhost_id, 'mahara', 'sso_idp', 1, 'sso_sp', 1, 'pf', 1, 'pf', 1));
254f2d05 134 if (empty($hosts)) { $hosts = array(); }
135 if (isset($this) && is_object($this)) {
136 $this->hosts = $hosts;
137 }
138 return $hosts;
139 }
140
141 public function prepare_package() {
142 $files = $this->exporter->get_tempfiles();
2f6bd2a9 143 $this->totalsize = 0;
254f2d05 144 foreach ($files as $f) {
145 $this->filesmanifest[$f->get_contenthash()] = array(
146 'filename' => $f->get_filename(),
147 'sha1' => $f->get_contenthash(),
2f6bd2a9 148 'size' => $f->get_filesize(),
254f2d05 149 );
2f6bd2a9 150 $this->totalsize += $f->get_filesize();
254f2d05 151 }
254f2d05 152
37f03ea0 153 $this->set('file', $this->exporter->zip_tempfiles()); // this will throw a file_exception which the exporter catches separately.
254f2d05 154 }
155
aed2937f 156 private function ensure_environment() {
254f2d05 157 global $MNET;
158 if (empty($MNET)) {
159 $MNET = new mnet_environment();
160 $MNET->init();
161 } // no idea why this happens :(
aed2937f 162 }
163
164 public function send_package() {
165 global $CFG;
166 $this->ensure_environment();
254f2d05 167 // send the 'content_ready' request to mahara
168 require_once($CFG->dirroot . '/mnet/xmlrpc/client.php');
169 $client = new mnet_xmlrpc_client();
170 $client->set_method('portfolio/mahara/lib.php/send_content_ready');
171 $client->add_param($this->token);
172 $client->add_param($this->get('user')->username);
173 $client->add_param($this->resolve_format());
1c597211 174 $client->add_param(array(
175 'filesmanifest' => $this->filesmanifest,
2f6bd2a9 176 'zipfilesha1' => $this->get('file')->get_contenthash(),
177 'zipfilesize' => $this->get('file')->get_filesize(),
178 'totalsize' => $this->totalsize,
1c597211 179 ));
254f2d05 180 $client->add_param($this->get_export_config('wait'));
181 $this->ensure_mnethost();
182 if (!$client->send($this->mnethost)) {
183 foreach ($client->error as $errormessage) {
184 list($code, $message) = array_map('trim',explode(':', $errormessage, 2));
185 $message .= "ERROR $code:<br/>$errormessage<br/>";
186 }
187 throw new portfolio_export_exception($this->get('exporter'), 'failedtoping', 'portfolio_mahara', '', $message);
188 }
189 // we should get back... an ok and a status
190 // either we've been waiting a while and mahara has fetched the file or has queued it.
191 $response = (object)$client->response;
192 if (!$response->status) {
193 throw new portfolio_export_exception($this->get('exporter'), 'failedtoping', 'portfolio_mahara');
194 }
37f03ea0 195 if ($response->type =='queued') {
f2e933bb 196 $this->exporter->set_forcequeue();
37f03ea0 197 }
14cb94eb 198 if (isset($response->querystring)) {
199 $this->continueurl = $response->querystring;
200 }
5d0dbf13
PL
201 // if we're not queuing the logging might have already happened
202 $this->exporter->update_log_url($this->get_static_continue_url());
254f2d05 203 }
204
5d0dbf13 205 public function get_static_continue_url() {
aed2937f 206 $remoteurl = '/artefact/file/';// @todo penny this might change later when we change formats.
14cb94eb 207 if (isset($this->continueurl)) {
208 $remoteurl .= $this->continueurl;
209 }
5d0dbf13
PL
210 return $remoteurl;
211 }
212
213 public function resolve_static_continue_url($remoteurl) {
214 static $sessions = array();
215 // if this is called mutliple times for the same host, stuff breaks
216 // so we have to keep track and just replace the wantsurl bit
217 // in case things go to different plugins or whatever
218 if (array_key_exists($this->get_config('mnethostid'), $sessions)) {
219 return preg_replace('/wantsurl=[^&]*&/', 'wantsurl=' . urlencode($remoteurl) . '&', $sessions[$this->get_config('mnethostid')]);
220 }
221 $this->ensure_mnethost();
222 $this->ensure_environment();
223 $mnetauth = get_auth_plugin('mnet');
aed2937f 224 if (!$url = $mnetauth->start_jump_session($this->get_config('mnethostid'), $remoteurl)) {
225 return false;
226 }
5d0dbf13 227 $sessions[$this->get_config('mnethostid')] = $url;
aed2937f 228 return $url;
254f2d05 229 }
230
5d0dbf13
PL
231 public function get_interactive_continue_url() {
232 return $this->resolve_static_continue_url($this->get_static_continue_url());
233 }
234
254f2d05 235 public function steal_control($stage) {
236 if ($stage != PORTFOLIO_STAGE_CONFIG) {
237 return false;
238 }
239 global $CFG;
edf1fc35 240 return $CFG->wwwroot . '/portfolio/mahara/preconfig.php?id=' . $this->exporter->get('id');
254f2d05 241 }
242
243 public function verify_file_request_params($params) {
244 return false;
245 // the data comes from an xmlrpc request,
246 // not a request to file.php
247 }
248
249 /**
250 * sends the 'content_intent' ping to mahara
251 * if all goes well, this will set the 'token' and 'sendtype' member variables.
252 */
253 public function send_intent() {
254 global $CFG, $DB;
255 require_once($CFG->dirroot . '/mnet/xmlrpc/client.php');
256 $client = new mnet_xmlrpc_client();
257 $client->set_method('portfolio/mahara/lib.php/send_content_intent');
258 $client->add_param($this->get('user')->username);
259 $this->ensure_mnethost();
260 if (!$client->send($this->mnethost)) {
261 foreach ($client->error as $errormessage) {
262 list($code, $message) = array_map('trim',explode(':', $errormessage, 2));
263 $message .= "ERROR $code:<br/>$errormessage<br/>";
264 }
265 throw new portfolio_export_exception($this->get('exporter'), 'failedtoping', 'portfolio_mahara', '', $message);
266 }
267 // we should get back... the send type and a shared token
268 $response = (object)$client->response;
269 if (empty($response->sendtype) || empty($response->token)) {
270 throw new portfolio_export_exception($this->get('exporter'), 'senddisallowed', 'portfolio_mahara');
271 }
272 switch ($response->sendtype) {
273 case 'immediate':
274 $this->sendtype = PORTFOLIO_MAHARA_IMMEDIATE;
275 break;
276 case 'queue':
277 $this->sendtype = PORTFOLIO_MAHARA_QUEUE;
278 break;
279 case 'none':
280 default:
281 throw new portfolio_export_exception($this->get('exporter'), 'senddisallowed', 'portfolio_mahara');
282 }
283 $this->token = $response->token;
284 $this->get('exporter')->save();
285 // put the entry in the mahara queue table now too
286 $q = new stdClass;
287 $q->token = $this->token;
288 $q->transferid = $this->get('exporter')->get('id');
289 $DB->insert_record('portfolio_mahara_queue', $q);
290 }
291
292 private function ensure_mnethost() {
293 if (!empty($this->hostrecord) && !empty($this->mnethost)) {
294 return;
295 }
296 global $DB;
aae8cfdc 297 if (!$this->hostrecord = $DB->get_record('mnet_host', array('id' => $this->get_config('mnethostid')))) {
298 throw new portfolio_plugin_exception(PORTFOLIO_MAHARA_ERR_INVALIDHOST, 'portfolio_mahara');
299 }
254f2d05 300 $this->mnethost = new mnet_peer();
301 $this->mnethost->set_wwwroot($this->hostrecord->wwwroot);
302 }
303
304 public static function mnet_publishes() {
305 $pf= array();
306 $pf['name'] = 'pf'; // Name & Description go in lang file
307 $pf['apiversion'] = 1;
308 $pf['methods'] = array('send_content_intent', 'send_content_ready', 'fetch_file');
309
310 return array($pf);
311 }
312
313 /**
314 * xmlrpc (mnet) function to get the file.
315 * reads in the file and returns it base_64 encoded
316 * so that it can be enrypted by mnet.
317 *
318 * @param string $token the token recieved previously during send_content_intent
319 */
320 public static function fetch_file($token) {
321 global $DB, $MNET_REMOTE_CLIENT;;
322 try {
aed2937f 323 if (!$transferid = $DB->get_field('portfolio_mahara_queue', 'transferid', array('token' => $token))) {
2f6bd2a9 324 exit(mnet_server_fault(8009, get_string('mnet_notoken', 'portfolio_mahara')));
aed2937f 325 }
254f2d05 326 $exporter = portfolio_exporter::rewaken_object($transferid);
327 } catch (portfolio_exception $e) {
2f6bd2a9 328 exit(mnet_server_fault(8010, get_string('mnet_noid', 'portfolio_mahara')));
254f2d05 329 }
330 if ($exporter->get('instance')->get_config('mnethostid') != $MNET_REMOTE_CLIENT->id) {
2f6bd2a9 331 exit(mnet_server_fault(8011, get_string('mnet_wronghost', 'portfolio_mahara')));
254f2d05 332 }
333 global $CFG;
d5dfe1b3 334 try {
335 $i = $exporter->get('instance');
336 $f = $i->get('file');
432ad8bf 337 if (empty($f) || !($f instanceof stored_file)) {
2f6bd2a9 338 exit(mnet_server_fault(8012, get_string('mnet_nofile', 'portfolio_mahara')));
d5dfe1b3 339 }
432ad8bf 340 try {
341 $c = $f->get_content();
342 } catch (file_exception $e) {
2f6bd2a9 343 exit(mnet_server_fault(8013, get_string('mnet_nofilecontents', 'portfolio_mahara', $e->getMessage())));
432ad8bf 344 }
d5dfe1b3 345 $contents = base64_encode($c);
346 } catch (Exception $e) {
2f6bd2a9 347 exit(mnet_server_fault(8013, get_string('mnet_nofile', 'portfolio_mahara')));
d5dfe1b3 348 }
fae69685 349 $exporter->log_transfer();
254f2d05 350 $exporter->process_stage_cleanup(true);
351 return $contents;
352 }
353
354 public function cleanup() {
355 global $DB;
356 $DB->delete_records('portfolio_mahara_queue', array('transferid' => $this->get('exporter')->get('id'), 'token' => $this->token));
357 }
358
359
e862c50a 360 /**
361 * internal helper function, that converts between the format constant,
362 * which might be too specific (eg 'image') and the class in our *supported* list
363 * which might be higher up the format hierarchy tree (eg 'file')
364 */
254f2d05 365 private function resolve_format() {
366 $thisformat = $this->get_export_config('format');
367 $allformats = portfolio_supported_formats();
368 $thisobj = new $allformats[$thisformat];
369 foreach ($this->supported_formats() as $f) {
370 $class = $allformats[$f];
371 if ($thisobj instanceof $class) {
372 return $f;
373 }
374 }
375 }
254f2d05 376}
377
4317f92f 378