MDL-16180 - make sure mnet auth is enabled in mahara portfolio plugin sanity check
[moodle.git] / portfolio / type / 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)
22
23 public static function get_allowed_config() {
24 return array('mnethostid');
25 }
26
27 public static function supported_formats() {
28 return array(PORTFOLIO_FORMAT_FILE);
29 }
30
31 public function expected_time($callertime) {
32 if ($this->sendtype == PORTFOLIO_MAHARA_QUEUE) {
33 return PORTFOLIO_TIME_FORCEQUEUE;
34 }
35 return $callertime;
36 }
37
38 public static function has_admin_config() {
39 return true;
40 }
41
42 public function admin_config_form(&$mform) {
254f2d05 43 $strrequired = get_string('required');
44 $hosts = self::get_mnet_hosts(); // this is called by sanity check but it's ok because it's cached
45 foreach ($hosts as $host) {
46 $hosts[$host->id] = $host->name;
47 }
48 $mform->addElement('select', 'mnethostid', get_string('mnethost', 'portfolio_mahara'), $hosts);
49 $mform->addRule('mnethostid', $strrequired, 'required', null, 'client');
aae8cfdc 50 if ($errorcode = self::plugin_sanity_check()) {
51 return $errorcode; // processing stops when we return a string.
52 }
53 if (!empty($this) && $errorcode = $this->instance_sanity_check()) {
54 return $errorcode;
55 }
254f2d05 56 }
57
aae8cfdc 58 public function instance_sanity_check() {
59 // make sure the host record exists since we don't have referential integrity
60 try {
61 $this->ensure_mnethost();
62 }
63 catch (portfolio_exception $e) {
64 return PORTFOLIO_MAHARA_ERR_INVALIDHOST;
65 }
66 // make sure we have the right services
67 $hosts = $this->get_mnet_hosts();
68 if (!array_key_exists($this->get_config('mnethostid'), $hosts)) {
69 return PORTFOLIO_MAHARA_ERR_INVALIDHOST;
70 }
71 return 0;
72 }
254f2d05 73
74 public static function plugin_sanity_check() {
254f2d05 75 global $CFG, $DB;
76 $errorcode = 0;
77 if (!isset($CFG->mnet_dispatcher_mode) || $CFG->mnet_dispatcher_mode != 'strict') {
78 $errorcode = PORTFOLIO_MAHARA_ERR_NETWORKING_OFF;
79 }
7254f56e 80 if (!is_enabled_auth('mnet')) {
81 $errorcode = PORTFOLIO_MAHARA_ERR_NOMNETAUTH;
82 }
254f2d05 83 if (!self::get_mnet_hosts()) {
84 $errorcode = PORTFOLIO_MAHARA_ERR_NOHOSTS;
85 }
86 if (!empty($errorcode)) { // disable the plugins // @todo
87 $DB->set_field('portfolio_instance', 'visible', 0, array('plugin' => 'mahara'));
88 }
89 return $errorcode;
90 }
91
92 private static function get_mnet_hosts() {
93 global $DB, $CFG;
94 static $hosts;
95 if (isset($this) && is_object($this) && isset($this->hosts)) {
96 return $this->hosts;
97 } else if (!isset($this) && isset($hosts)) {
98 return $hosts;
99 }
100 $hosts = $DB->get_records_sql(' SELECT
101 h.id,
102 h.wwwroot,
103 h.ip_address,
104 h.name,
105 h.public_key,
106 h.public_key_expires,
107 h.transport,
108 h.portno,
109 h.last_connect_time,
110 h.last_log_id,
111 h.applicationid,
112 a.name as app_name,
113 a.display_name as app_display_name,
114 a.xmlrpc_server_url
115 FROM {mnet_host} h
116 JOIN {mnet_application} a ON h.applicationid=a.id
117 JOIN {mnet_host2service} hs1 ON hs1.hostid = h.id
118 JOIN {mnet_service} s1 ON hs1.serviceid = s1.id
119 JOIN {mnet_host2service} hs2 ON hs2.hostid = h.id
120 JOIN {mnet_service} s2 ON hs2.serviceid = s2.id
121 JOIN {mnet_host2service} hs3 ON hs3.hostid = h.id
122 JOIN {mnet_service} s3 ON hs3.serviceid = s3.id
123 WHERE
124 h.id <> ? AND
125 h.deleted = 0 AND
126 a.name = ? AND
127 s1.name = ? AND hs1.publish = ? AND
128 s2.name = ? AND hs2.subscribe = ? AND
129 s3.name = ? AND hs3.subscribe = ?',
130 array($CFG->mnet_localhost_id, 'mahara', 'sso_idp', 1, 'sso_sp', 1, 'pf', 1));;
131 if (empty($hosts)) { $hosts = array(); }
132 if (isset($this) && is_object($this)) {
133 $this->hosts = $hosts;
134 }
135 return $hosts;
136 }
137
138 public function prepare_package() {
139 $files = $this->exporter->get_tempfiles();
140 foreach ($files as $f) {
141 $this->filesmanifest[$f->get_contenthash()] = array(
142 'filename' => $f->get_filename(),
143 'sha1' => $f->get_contenthash(),
144 );
145 }
146 $zipper = new zip_packer();
147
148 $filename = 'portfolio-export.zip';
149 if ($newfile = $zipper->archive_to_storage($files, SYSCONTEXTID, 'portfolio_exporter', $this->exporter->get('id'), '/final/', $filename, $this->user->id)) {
150 $this->set('file', $newfile);
151 return true;
152 }
153 return false;
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,
176 'zipfilesha1' => $this->get('file')->get_contenthash()
177 ));
254f2d05 178 $client->add_param($this->get_export_config('wait'));
179 $this->ensure_mnethost();
180 if (!$client->send($this->mnethost)) {
181 foreach ($client->error as $errormessage) {
182 list($code, $message) = array_map('trim',explode(':', $errormessage, 2));
183 $message .= "ERROR $code:<br/>$errormessage<br/>";
184 }
185 throw new portfolio_export_exception($this->get('exporter'), 'failedtoping', 'portfolio_mahara', '', $message);
186 }
187 // we should get back... an ok and a status
188 // either we've been waiting a while and mahara has fetched the file or has queued it.
189 $response = (object)$client->response;
190 if (!$response->status) {
191 throw new portfolio_export_exception($this->get('exporter'), 'failedtoping', 'portfolio_mahara');
192 }
193 return true;
194 }
195
196 public function get_continue_url() {
197 $this->ensure_mnethost();
aed2937f 198 $this->ensure_environment();
199 $mnetauth = get_auth_plugin('mnet');
200 $remoteurl = '/artefact/file/';// @todo penny this might change later when we change formats.
201 if (!$url = $mnetauth->start_jump_session($this->get_config('mnethostid'), $remoteurl)) {
202 return false;
203 }
204 return $url;
254f2d05 205 }
206
207 public function steal_control($stage) {
208 if ($stage != PORTFOLIO_STAGE_CONFIG) {
209 return false;
210 }
211 global $CFG;
212 return $CFG->wwwroot . '/portfolio/type/mahara/preconfig.php?id=' . $this->exporter->get('id');
213 }
214
215 public function verify_file_request_params($params) {
216 return false;
217 // the data comes from an xmlrpc request,
218 // not a request to file.php
219 }
220
221 /**
222 * sends the 'content_intent' ping to mahara
223 * if all goes well, this will set the 'token' and 'sendtype' member variables.
224 */
225 public function send_intent() {
226 global $CFG, $DB;
227 require_once($CFG->dirroot . '/mnet/xmlrpc/client.php');
228 $client = new mnet_xmlrpc_client();
229 $client->set_method('portfolio/mahara/lib.php/send_content_intent');
230 $client->add_param($this->get('user')->username);
231 $this->ensure_mnethost();
232 if (!$client->send($this->mnethost)) {
233 foreach ($client->error as $errormessage) {
234 list($code, $message) = array_map('trim',explode(':', $errormessage, 2));
235 $message .= "ERROR $code:<br/>$errormessage<br/>";
236 }
237 throw new portfolio_export_exception($this->get('exporter'), 'failedtoping', 'portfolio_mahara', '', $message);
238 }
239 // we should get back... the send type and a shared token
240 $response = (object)$client->response;
241 if (empty($response->sendtype) || empty($response->token)) {
242 throw new portfolio_export_exception($this->get('exporter'), 'senddisallowed', 'portfolio_mahara');
243 }
244 switch ($response->sendtype) {
245 case 'immediate':
246 $this->sendtype = PORTFOLIO_MAHARA_IMMEDIATE;
247 break;
248 case 'queue':
249 $this->sendtype = PORTFOLIO_MAHARA_QUEUE;
250 break;
251 case 'none':
252 default:
253 throw new portfolio_export_exception($this->get('exporter'), 'senddisallowed', 'portfolio_mahara');
254 }
255 $this->token = $response->token;
256 $this->get('exporter')->save();
257 // put the entry in the mahara queue table now too
258 $q = new stdClass;
259 $q->token = $this->token;
260 $q->transferid = $this->get('exporter')->get('id');
261 $DB->insert_record('portfolio_mahara_queue', $q);
262 }
263
264 private function ensure_mnethost() {
265 if (!empty($this->hostrecord) && !empty($this->mnethost)) {
266 return;
267 }
268 global $DB;
aae8cfdc 269 if (!$this->hostrecord = $DB->get_record('mnet_host', array('id' => $this->get_config('mnethostid')))) {
270 throw new portfolio_plugin_exception(PORTFOLIO_MAHARA_ERR_INVALIDHOST, 'portfolio_mahara');
271 }
254f2d05 272 $this->mnethost = new mnet_peer();
273 $this->mnethost->set_wwwroot($this->hostrecord->wwwroot);
274 }
275
276 public static function mnet_publishes() {
277 $pf= array();
278 $pf['name'] = 'pf'; // Name & Description go in lang file
279 $pf['apiversion'] = 1;
280 $pf['methods'] = array('send_content_intent', 'send_content_ready', 'fetch_file');
281
282 return array($pf);
283 }
284
285 /**
286 * xmlrpc (mnet) function to get the file.
287 * reads in the file and returns it base_64 encoded
288 * so that it can be enrypted by mnet.
289 *
290 * @param string $token the token recieved previously during send_content_intent
291 */
292 public static function fetch_file($token) {
293 global $DB, $MNET_REMOTE_CLIENT;;
294 try {
aed2937f 295 if (!$transferid = $DB->get_field('portfolio_mahara_queue', 'transferid', array('token' => $token))) {
296 exit(mnet_server_fault(8009, 'could not find token'));
297 }
254f2d05 298 $exporter = portfolio_exporter::rewaken_object($transferid);
299 } catch (portfolio_exception $e) {
d5dfe1b3 300 exit(mnet_server_fault(8010, 'invalid transfer id'));
254f2d05 301 }
302 if ($exporter->get('instance')->get_config('mnethostid') != $MNET_REMOTE_CLIENT->id) {
d5dfe1b3 303 exit(mnet_server_fault(8011, "remote host didn't match saved host"));
254f2d05 304 }
305 global $CFG;
d5dfe1b3 306 try {
307 $i = $exporter->get('instance');
308 $f = $i->get('file');
309 if (empty($f)) {
310 exit(mnet_server_fault(8012, 'could not find file in transfer object - weird error'));
311 }
312 $c = $f->get_content();
313 $contents = base64_encode($c);
314 } catch (Exception $e) {
315 exit(mnet_server_fault(8013, 'could not get file to send'));
316 }
254f2d05 317 $exporter->process_stage_cleanup(true);
318 return $contents;
319 }
320
321 public function cleanup() {
322 global $DB;
323 $DB->delete_records('portfolio_mahara_queue', array('transferid' => $this->get('exporter')->get('id'), 'token' => $this->token));
324 }
325
326
327 private function resolve_format() {
328 $thisformat = $this->get_export_config('format');
329 $allformats = portfolio_supported_formats();
330 $thisobj = new $allformats[$thisformat];
331 foreach ($this->supported_formats() as $f) {
332 $class = $allformats[$f];
333 if ($thisobj instanceof $class) {
334 return $f;
335 }
336 }
337 }
254f2d05 338}
339
340?>