navigation MDL-22732 Removed add resources and activities branches from nav when...
[moodle.git] / repository / remotemoodle / repository.class.php
CommitLineData
d3dfe9ae 1<?php
10d53fd3
DC
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
d3dfe9ae 18/**
bcd41452 19 * repository_remotemoodle class
709c5495 20 * This plugin allowed to connect a retrieve a file from another Moodle site
d3dfe9ae 21 * This is a subclass of repository class
10d53fd3
DC
22 *
23 * @since 2.0
24 * @package moodlecore
25 * @subpackage repository
26 * @copyright 2009 Jerome Mouneyrac
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d3dfe9ae 28 */
bcd41452 29
ef378cdc 30require_once($CFG->dirroot.'/repository/lib.php');
bcd41452 31
32
d3dfe9ae 33class repository_remotemoodle extends repository {
34
35 /**
447c7a19
DC
36 * Constructor of remotemoodle plugin, used to setup mnet environment
37 * @global object $SESSION
38 * @global object $CFG
39 * @param int $repositoryid
40 * @param object $context
41 * @param array $options
d3dfe9ae 42 */
447c7a19
DC
43 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
44 global $SESSION, $CFG;
d3dfe9ae 45 parent::__construct($repositoryid, $context, $options);
287efec6 46 $this->mnet = get_mnet_environment();
d3dfe9ae 47 }
48
bcd41452 49 /**
709c5495 50 * Retrieve a file for a user of the Moodle client calling this function
51 * The file is encoded in base64
447c7a19
DC
52 * @global object $DB
53 * @global object $USER
54 * @param string $username
55 * @param string $source
56 * @return array
bcd41452 57 */
571dde8f 58 public function retrieveFile($username, $source) {
287efec6
PL
59 global $DB, $USER;
60
61 $remoteclient = get_mnet_remote_client();
709c5495 62
63 ///check the the user is known
64 ///he has to be previously connected to the server site in order to be in the database
0271a05d
PS
65 //TODO: MDL-21318 this looks problematic, because global $USER would need to be set back after this,
66 // also is the user allowed to roam?
287efec6 67 $USER = $DB->get_record('user',array('username' => $username, 'mnethostid' => $remoteclient->id));
709c5495 68 if (empty($USER)) {
d234faf3 69 throw new mnet_server_exception(9012, 'usernotfound', 'repository_remotemoodle', $username);
709c5495 70 }
71
dbc01944 72 $file = unserialize(base64_decode($source));
571dde8f 73 $contextid = $file[0];
74 $filearea = $file[1];
75 $itemid = $file[2];
76 $filepath = $file[3];
77 $filename = $file[4];
dbc01944 78
709c5495 79 ///check that the user has read permission on this file
571dde8f 80 $browser = get_file_browser();
81 $fileinfo = $browser->get_file_info(get_context_instance_by_id($contextid), $filearea, $itemid, $filepath, $filename);
571dde8f 82 if (empty($fileinfo)) {
d234faf3 83 throw new mnet_server_exception(9013, 'usercannotaccess', 'repository_remotemoodle', $file);
571dde8f 84 }
709c5495 85
86 ///retrieve the file with file API functions and return it encoded in base64
ef378cdc 87 $fs = get_file_storage();
571dde8f 88 $sf = $fs->get_file($contextid, $filearea, $itemid, $filepath, $filename);
ef378cdc 89 $contents = base64_encode($sf->get_content());
ef378cdc 90 return array($contents, $sf->get_filename());
91 }
92
bcd41452 93 /**
709c5495 94 * Retrieve file list for a user of the Moodle client calling this function
447c7a19
DC
95 * @global object $DB
96 * @global object $USER
97 * @global object $CFG
98 * @param string $username
99 * @param string $search
100 * @return array
bcd41452 101 */
e3301494 102 public function getFileList($username, $search) {
287efec6 103 global $DB, $USER, $CFG;
709c5495 104
287efec6 105 $remoteclient = get_mnet_remote_client();
709c5495 106 ///check the the user is known
107 ///he has to be previously connected to the server site in order to be in the database
0271a05d
PS
108 //TODO: MDL-21318 this looks problematic, because global $USER would need to be set back after this,
109 // also is the user allowed to roam?
287efec6 110 $USER = $DB->get_record('user',array('username' => $username, 'mnethostid' => $remoteclient->id));
571dde8f 111 if (empty($USER)) {
d234faf3 112 throw new mnet_server_exception(9012, 'usernotfound', 'repository_remotemoodle', $username);
571dde8f 113 }
dbc01944 114
e3ca0f3a 115 try {
dbc01944 116 return repository::get_user_file_tree($search);
ef378cdc 117 }
e3ca0f3a 118 catch (Exception $e) {
d234faf3 119 throw new mnet_server_exception(9014, 'failtoretrievelist', 'repository_remotemoodle');
ef378cdc 120 }
ef378cdc 121 }
122
d3dfe9ae 123 /**
709c5495 124 * Display the file listing - no login required
447c7a19 125 * @return array
d3dfe9ae 126 */
447c7a19 127 public function print_login() {
d3dfe9ae 128 return $this->get_listing();
129 }
130
d3dfe9ae 131 /**
709c5495 132 * Display the file listing for the search term
447c7a19
DC
133 * @param string $search_text
134 * @return array
d3dfe9ae 135 */
dbc01944 136 public function search($search_text) {
5fa358d1 137 return $this->get_listing('', '', $search_text);
d3dfe9ae 138 }
139
d3dfe9ae 140 /**
709c5495 141 * Retrieve the file listing - file picker function
447c7a19
DC
142 * @global object $CFG
143 * @global object $DB
144 * @global object $USER
145 * @param string $encodedpath
d0e36ce5 146 * @param int $page
447c7a19
DC
147 * @param string $search
148 * @return array
d3dfe9ae 149 */
5fa358d1 150 public function get_listing($encodedpath = '', $page = '', $search = '') {
d3dfe9ae 151 global $CFG, $DB, $USER;
ef378cdc 152
709c5495 153 ///check that the host has a version >2.0
154 ///for that we check that the host has the getFileList() method implemented
155 ///We also check that this method has been activated (if it is not
156 ///the method will not be returned by the system method system/listMethods)
ef378cdc 157 require_once($CFG->dirroot . '/mnet/xmlrpc/client.php');
c4f2cf4a 158
159 ///check that the peer has been setup
160 if (!array_key_exists('peer',$this->options)) {
161 echo json_encode(array('e'=>get_string('error').' 9010: '.get_string('hostnotfound','repository_remotemoodle')));
162 exit;
163 }
164
709c5495 165 $host = $DB->get_record('mnet_host',array('id' => $this->options['peer'])); //need to retrieve the host url
c4f2cf4a 166
167 ///check that the peer host exists into the database
168 if (empty($host)) {
169 echo json_encode(array('e'=>get_string('error').' 9011: '.get_string('hostnotfound','repository_remotemoodle')));
170 exit;
171 }
172
ef378cdc 173 $mnet_peer = new mnet_peer();
174 $mnet_peer->set_wwwroot($host->wwwroot);
175 $client = new mnet_xmlrpc_client();
176 $client->set_method('system/listMethods');
177 $client->send($mnet_peer);
178 $services = $client->response;
179 if (array_search('repository/remotemoodle/repository.class.php/getFileList', $services) === false) {
180 echo json_encode(array('e'=>get_string('connectionfailure','repository_remotemoodle')));
181 exit;
182 }
dbc01944 183
709c5495 184 ///connect to the remote moodle and retrieve the list of files
ef378cdc 185 $client->set_method('repository/remotemoodle/repository.class.php/getFileList');
e3301494 186 $client->add_param($USER->username);
187 $client->add_param($search);
709c5495 188
189 ///call the method and manage host error
a1c9c2bd 190 if (!$client->send($mnet_peer)) {
191 $message =" ";
192 foreach ($client->error as $errormessage) {
193 $message .= "ERROR: $errormessage . ";
dbc01944 194 }
709c5495 195 echo json_encode(array('e'=>$message)); //display all error messages
a1c9c2bd 196 exit;
197 }
dbc01944 198
d3dfe9ae 199 $services = $client->response;
709c5495 200 ///display error message if we could retrieve the list or if nothing were returned
201 if (empty($services)) {
46c8e3b0 202 echo json_encode(array('e'=>get_string('failtoretrievelist','repository_remotemoodle')));
203 exit;
204 }
205
d3dfe9ae 206 return $services;
207 }
208
dbc01944 209
d3dfe9ae 210
bcd41452 211 /**
d3dfe9ae 212 * Download a file
213 * @global object $CFG
214 * @param string $url the url of file
215 * @param string $file save location
216 * @return string the location of the file
217 * @see curl package
218 */
219 public function get_file($url, $file = '') {
220 global $CFG, $DB, $USER;
d3dfe9ae 221
709c5495 222 ///set mnet environment and set the mnet host
dbc01944 223 require_once($CFG->dirroot . '/mnet/xmlrpc/client.php');
709c5495 224 $host = $DB->get_record('mnet_host',array('id' => $this->options['peer'])); //retrieve the host url
d3dfe9ae 225 $mnet_peer = new mnet_peer();
226 $mnet_peer->set_wwwroot($host->wwwroot);
227
709c5495 228 ///create the client and set the method to call
d3dfe9ae 229 $client = new mnet_xmlrpc_client();
ef378cdc 230 $client->set_method('repository/remotemoodle/repository.class.php/retrieveFile');
d3dfe9ae 231 $client->add_param($USER->username);
232 $client->add_param($url);
233
709c5495 234 ///call the method and manage host error
4ad46f21 235 if (!$client->send($mnet_peer)) {
236 $message =" ";
237 foreach ($client->error as $errormessage) {
238 $message .= "ERROR: $errormessage . ";
239 }
240 echo json_encode(array('e'=>$message));
241 exit;
242 }
d3dfe9ae 243
dbc01944 244 $services = $client->response; //service contains the file content in the first case of the array,
709c5495 245 //and the filename in the second
246
247 //the content has been encoded in base64, need to decode it
d3dfe9ae 248 $content = base64_decode($services[0]);
709c5495 249 $file = $services[1]; //filename
d3dfe9ae 250
709c5495 251 ///create a temporary folder with a file
a53d4f45 252 $path = $this->prepare_file($file);
709c5495 253 ///fill the file with the content
a53d4f45 254 $fp = fopen($path, 'w');
d3dfe9ae 255 fwrite($fp,$content);
256 fclose($fp);
dbc01944 257
1dce6261 258 return array('path'=>$path);
dbc01944 259
d3dfe9ae 260 }
261
262 /**
263 * Add Instance settings input to Moodle form
447c7a19
DC
264 * @global object $CFG
265 * @global object $DB
266 * @param object $mform
d3dfe9ae 267 */
aea5595c 268 public function instance_config_form($mform) {
d3dfe9ae 269 global $CFG, $DB;
dbc01944 270
709c5495 271 //retrieve only Moodle peers
d3dfe9ae 272 $hosts = $DB->get_records_sql(' SELECT
273 h.id,
274 h.wwwroot,
275 h.ip_address,
276 h.name,
277 h.public_key,
278 h.public_key_expires,
279 h.transport,
280 h.portno,
281 h.last_connect_time,
282 h.last_log_id,
283 h.applicationid,
284 a.name as app_name,
285 a.display_name as app_display_name,
286 a.xmlrpc_server_url
287 FROM {mnet_host} h
288 JOIN {mnet_application} a ON h.applicationid=a.id
289 WHERE
290 h.id <> ? AND
291 h.deleted = 0 AND
ef378cdc 292 a.name = ? AND
293 h.name <> ?',
294 array($CFG->mnet_localhost_id, 'moodle', 'All Hosts'));
d3dfe9ae 295 $peers = array();
296 foreach($hosts as $host) {
dbc01944 297 $peers[$host->id] = $host->name;
d3dfe9ae 298 }
299
dbc01944 300
d3dfe9ae 301 $mform->addElement('select', 'peer', get_string('peer', 'repository_remotemoodle'),$peers);
302 $mform->addRule('peer', get_string('required'), 'required', null, 'client');
dbc01944 303
d8a560c6 304 if (empty($peers)) {
305 $mform->addElement('static', null, '', get_string('nopeer','repository_remotemoodle'));
306 }
d3dfe9ae 307 }
308
309 /**
310 * Names of the instance settings
447c7a19 311 * @return array
d3dfe9ae 312 */
313 public static function get_instance_option_names() {
709c5495 314 ///the administrator just need to set a peer
d3dfe9ae 315 return array('peer');
316 }
41076c58
DC
317 public function supported_returntypes() {
318 return FILE_INTERNAL;
319 }
d3dfe9ae 320}
4317f92f 321