3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Web services utility functions and classes
22 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once($CFG->libdir.'/externallib.php');
29 * Exception indicating access control problem in web service call
31 class webservice_access_exception extends moodle_exception {
35 function __construct($debuginfo) {
36 parent::__construct('accessexception', 'exception', '', null, $debuginfo);
40 function webservice_protocol_is_enabled($protocol) {
43 if (empty($CFG->enablewebservices)) {
47 $active = explode(',', $CFG->webserviceprotocols);
49 return(in_array($protocol, $active));
53 * Mandatory web service server interface
54 * @author Petr Skoda (skodak)
56 interface webservice_server {
58 * Process request from client.
59 * @param bool $simple use simple authentication
62 public function run($simple);
66 * Special abstraction of our srvices that allows
67 * interaction with stock Zend ws servers.
70 abstract class webservice_zend_server implements webservice_server {
72 /** @property string name of the zend server class */
73 protected $zend_class;
75 /** @property object Zend server instance */
76 protected $zend_server;
78 /** @property string $wsname name of the web server plugin */
79 protected $wsname = null;
81 /** @property bool $simple true if simple auth used */
84 /** @property string $service_class virtual web service class with all functions user name execute, created on the fly */
85 protected $service_class;
87 /** @property object restricted context */
88 protected $restricted_context;
93 public function __construct($zend_class) {
94 $this->zend_class = $zend_class;
98 * Process request from client.
99 * @param bool $simple use simple authentication
102 public function run($simple) {
103 $this->simple = $simple;
105 // we will probably need a lot of memory in some functions
106 @raise_memory_limit('128M');
108 // set some longer timeout, this script is not sending any output,
109 // this means we need to manually extend the timeout operations
110 // that need longer time to finish
111 external_api::set_timeout();
113 // set up exception handler first, we want to sent them back in correct format that
114 // the other system understands
115 // we do not need to call the original default handler because this ws handler does everything
116 set_exception_handler(array($this, 'exception_handler'));
118 // now create the instance of zend server
119 $this->init_zend_server();
121 // this sets up $USER and $SESSION and context restrictions
122 $this->authenticate_user();
124 // make a list of all functions user is allowed to excecute
125 $this->init_service_class();
127 // TODO: solve debugging level somehow
128 Zend_XmlRpc_Server_Fault::attachFaultException('moodle_exception');
131 $this->zend_server->setClass($this->service_class);
132 $response = $this->zend_server->handle();
134 $grrr = ob_get_clean();
136 error_log($response);
139 $this->session_cleanup();
141 //TODO: we need to send some headers too I guess
147 * Load virtual class needed for Zend api
150 protected function init_service_class() {
153 // first ofall get a complete list of services user is allowed to access
155 // now make sure the function is listed in at least one service user is allowed to use
156 // allow access only if:
157 // 1/ entry in the external_services_users table if required
158 // 2/ validuntil not reached
159 // 3/ has capability if specified in service desc
162 $sql = "SELECT s.*, NULL AS iprestriction
163 FROM {external_services} s
164 JOIN {external_services_functions} sf ON (sf.externalserviceid = s.id AND s.restrictedusers = 0)
169 SELECT s.*, su.iprestriction
170 FROM {external_services} s
171 JOIN {external_services_functions} sf ON (sf.externalserviceid = s.id AND s.restrictedusers = 1)
172 JOIN {external_services_users} su ON (su.externalserviceid = s.id AND su.userid = :userid)
173 WHERE s.enabled = 1 AND su.validuntil IS NULL OR su.validuntil < :now";
174 $params = array('userid'=>$USER->id, 'now'=>time());
177 //TODO: token may restrict access to one service only
178 die('not implemented yet');
181 $serviceids = array();
182 $rs = $DB->get_recordset_sql($sql, $params);
184 // now make sure user may access at least one service
185 $remoteaddr = getremoteaddr();
187 foreach ($rs as $service) {
188 if (isset($serviceids[$service->id])) {
191 if ($service->requiredcapability and !has_capability($service->requiredcapability, $this->restricted_context)) {
192 continue; // cap required, sorry
194 if ($service->iprestriction and !address_in_subnet($remoteaddr, $service->iprestriction)) {
195 continue; // wrong request source ip, sorry
197 $serviceids[$service->id] = $service->id;
201 // now get the list of all functions
203 list($serviceids, $params) = $DB->get_in_or_equal($serviceids);
205 FROM {external_functions} f
206 WHERE f.name IN (SELECT sf.functionname
207 FROM {external_services_functions} sf
208 WHERE sf.externalserviceid $serviceids)";
209 $functions = $DB->get_records_sql($sql, $params);
211 $functions = array();
214 // now make the virtual WS class with all the fuctions for this particular user
216 foreach ($functions as $function) {
217 $methods .= $this->get_virtual_method_code($function);
220 // let's use unique class name, there might be problem in unit tests
221 $classname = 'webservices_virtual_class_000000';
222 while(class_exists($classname)) {
228 * Virtual class web services for user id '.$USER->id.' in context '.$this->restricted_context->id.'.
230 class '.$classname.' {
234 // load the virtual class definition into memory
236 $this->service_class = $classname;
240 * returns virtual method code
241 * @param object $function
242 * @return string PHP code
244 protected function get_virtual_method_code($function) {
247 $function = external_function_info($function);
250 $params_desc = array();
251 foreach ($function->parameters_desc->keys as $name=>$keydesc) {
252 $params[] = '$'.$name;
254 if ($keydesc instanceof external_value) {
255 switch($keydesc->type) {
256 case PARAM_BOOL: // 0 or 1 only for now
258 $type = 'int'; break;
260 $type = 'double'; break;
264 } else if ($keydesc instanceof external_single_structure) {
266 } else if ($keydesc instanceof external_multiple_structure) {
269 $params_desc[] = ' * @param '.$type.' $'.$name.' '.$keydesc->desc;
271 $params = implode(', ', $params);
272 $params_desc = implode("\n", $params_desc);
274 if (is_null($function->returns_desc)) {
275 $return = ' * @return void';
278 if ($function->returns_desc instanceof external_value) {
279 switch($function->returns_desc->type) {
280 case PARAM_BOOL: // 0 or 1 only for now
282 $type = 'int'; break;
284 $type = 'double'; break;
288 } else if ($function->returns_desc instanceof external_single_structure) {
290 } else if ($function->returns_desc instanceof external_multiple_structure) {
293 $return = ' * @return '.$type.' '.$function->returns_desc->desc;
296 // now crate a virtual method that calls the ext implemenation
297 // TODO: add PHP docs and all missing info here
301 * '.$function->description.'
306 public function '.$function->name.'('.$params.') {
307 return '.$function->classname.'::'.$function->methodname.'('.$params.');
314 * Set up zend serice class
317 protected function init_zend_server() {
318 include "Zend/Loader.php";
319 Zend_Loader::registerAutoload();
320 //TODO: set up some server options and debugging too - maybe a new method
321 //TODO: add some zend exeption handler too
322 $this->zend_server = new $this->zend_class();
326 * Send the error information to the WS client.
327 * @param exception $ex
330 protected function send_error($ex=null) {
333 // TODO: find some way to send the error back through the Zend
337 * Authenticate user using username+password or token.
338 * This function sets up $USER global.
339 * It is safe to use has_capability() after this.
340 * This method also verifies user is allowed to use this
344 protected function authenticate_user() {
347 if (!NO_MOODLE_COOKIES) {
348 throw new coding_exception('Cookies must be disabled in WS servers!');
352 $this->restricted_context = get_context_instance(CONTEXT_SYSTEM);
354 if (!is_enabled_auth('webservice')) {
355 throw new webservice_access_exception('WS auth not enabled');
358 if (!$auth = get_auth_plugin('webservice')) {
359 throw new webservice_access_exception('WS auth missing');
362 // the username is hardcoded as URL parameter because we can not easily parse the request data :-(
363 if (!$username = optional_param('wsusername', '', PARAM_RAW)) {
364 throw new webservice_access_exception('Missing username');
367 // the password is hardcoded as URL parameter because we can not easily parse the request data :-(
368 if (!$password = optional_param('wspassword', '', PARAM_RAW)) {
369 throw new webservice_access_exception('Missing password');
372 if (!$auth->user_login_webservice($username, $password)) {
373 throw new webservice_access_exception('Wrong username or password');
376 $user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0), '*', MUST_EXIST);
378 // now fake user login, the session is completely empty too
379 session_set_user($user);
383 //TODO: not implemented yet
384 die('token login not implemented yet');
385 //TODO: $this->restricted_context is derived from the token context
388 if (!has_capability("webservice/$this->wsname:use", $this->restricted_context)) {
389 throw new webservice_access_exception('Access to web service not allowed');
392 external_api::set_context_restriction($this->restricted_context);
396 * Specialised exception handler, we can not use the standard one because
397 * it can not just print html to output.
399 * @param exception $ex
400 * @return void does not return
402 public function exception_handler($ex) {
403 global $CFG, $DB, $SCRIPT;
405 // detect active db transactions, rollback and log as error
406 if ($DB->is_transaction_started()) {
407 error_log('Database transaction aborted by exception in ' . $CFG->dirroot . $SCRIPT);
409 // note: transaction blocks should never change current $_SESSION
411 } catch (Exception $ignored) {
415 // now let the plugin send the exception to client
416 $this->send_error($ex);
418 // some hacks might need a cleanup hook
419 $this->session_cleanup($ex);
421 // not much else we can do now, add some logging later
426 * Future hook needed for emulated sessions.
427 * @param exception $exception null means normal termination, $exception received when WS call failed
430 protected function session_cleanup($exception=null) {
432 // nothing needs to be done, there is no persistent session
434 // close emulated session if used
442 * Web Service server base class, this class handles both
443 * simple and token authentication.
444 * @author Petr Skoda (skodak)
446 abstract class webservice_base_server implements webservice_server {
448 /** @property string $wsname name of the web server plugin */
449 protected $wsname = null;
451 /** @property bool $simple true if simple auth used */
454 /** @property string $username name of local user */
455 protected $username = null;
457 /** @property string $password password of the local user */
458 protected $password = null;
460 /** @property string $token authentication token*/
461 protected $token = null;
463 /** @property object restricted context */
464 protected $restricted_context;
466 /** @property array $parameters the function parameters - the real values submitted in the request */
467 protected $parameters = null;
469 /** @property string $functionname the name of the function that is executed */
470 protected $functionname = null;
472 /** @property object $function full function description */
473 protected $function = null;
475 /** @property mixed $returns function return value */
476 protected $returns = null;
481 public function __construct() {
485 * This method parses the request input, it needs to get:
486 * 1/ user authentication - username+password or token
488 * 3/ function parameters
492 abstract protected function parse_request();
495 * Send the result of function call to the WS client.
498 abstract protected function send_response();
501 * Send the error information to the WS client.
502 * @param exception $ex
505 abstract protected function send_error($ex=null);
509 * Process request from client.
510 * @param bool $simple use simple authentication
513 public function run($simple) {
514 $this->simple = $simple;
516 // we will probably need a lot of memory in some functions
517 @raise_memory_limit('128M');
519 // set some longer timeout, this script is not sending any output,
520 // this means we need to manually extend the timeout operations
521 // that need longer time to finish
522 external_api::set_timeout();
524 // set up exception handler first, we want to sent them back in correct format that
525 // the other system understands
526 // we do not need to call the original default handler because this ws handler does everything
527 set_exception_handler(array($this, 'exception_handler'));
529 // init all properties from the request data
530 $this->parse_request();
532 // authenticate user, this has to be done after the request parsing
533 // this also sets up $USER and $SESSION
534 $this->authenticate_user();
536 // find all needed function info and make sure user may actually execute the function
537 $this->load_function_info();
539 // finally, execute the function - any errors are catched by the default exception handler
542 // send the results back in correct format
543 $this->send_response();
546 $this->session_cleanup();
552 * Specialised exception handler, we can not use the standard one because
553 * it can not just print html to output.
555 * @param exception $ex
556 * @return void does not return
558 public function exception_handler($ex) {
559 global $CFG, $DB, $SCRIPT;
561 // detect active db transactions, rollback and log as error
562 if ($DB->is_transaction_started()) {
563 error_log('Database transaction aborted by exception in ' . $CFG->dirroot . $SCRIPT);
565 // note: transaction blocks should never change current $_SESSION
567 } catch (Exception $ignored) {
571 // now let the plugin send the exception to client
572 $this->send_error($ex);
574 // some hacks might need a cleanup hook
575 $this->session_cleanup($ex);
577 // not much else we can do now, add some logging later
582 * Future hook needed for emulated sessions.
583 * @param exception $exception null means normal termination, $exception received when WS call failed
586 protected function session_cleanup($exception=null) {
588 // nothing needs to be done, there is no persistent session
590 // close emulated session if used
595 * Authenticate user using username+password or token.
596 * This function sets up $USER global.
597 * It is safe to use has_capability() after this.
598 * This method also verifies user is allowed to use this
602 protected function authenticate_user() {
605 if (!NO_MOODLE_COOKIES) {
606 throw new coding_exception('Cookies must be disabled in WS servers!');
610 $this->restricted_context = get_context_instance(CONTEXT_SYSTEM);
612 if (!is_enabled_auth('webservice')) {
613 throw new webservice_access_exception('WS auth not enabled');
616 if (!$auth = get_auth_plugin('webservice')) {
617 throw new webservice_access_exception('WS auth missing');
620 if (!$this->username) {
621 throw new webservice_access_exception('Missing username');
624 if (!$this->password) {
625 throw new webservice_access_exception('Missing password');
628 if (!$auth->user_login_webservice($this->username, $this->password)) {
629 throw new webservice_access_exception('Wrong username or password');
632 $user = $DB->get_record('user', array('username'=>$this->username, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0), '*', MUST_EXIST);
634 // now fake user login, the session is completely empty too
635 session_set_user($user);
638 //TODO: not implemented yet
639 die('token login not implemented yet');
640 //TODO: $this->restricted_context is derived from the token context
643 if (!has_capability("webservice/$this->wsname:use", $this->restricted_context)) {
644 throw new webservice_access_exception('Access to web service not allowed');
647 external_api::set_context_restriction($this->restricted_context);
651 * Fetches the function description from database,
652 * verifies user is allowed to use this function and
653 * loads all paremeters and return descriptions.
656 protected function load_function_info() {
657 global $DB, $USER, $CFG;
659 if (empty($this->functionname)) {
660 throw new invalid_parameter_exception('Missing function name');
663 // function must exist
664 $function = external_function_info($this->functionname);
666 // now let's verify access control
668 // now make sure the function is listed in at least one service user is allowed to use
669 // allow access only if:
670 // 1/ entry in the external_services_users table if required
671 // 2/ validuntil not reached
672 // 3/ has capability if specified in service desc
675 $sql = "SELECT s.*, NULL AS iprestriction
676 FROM {external_services} s
677 JOIN {external_services_functions} sf ON (sf.externalserviceid = s.id AND s.restrictedusers = 0 AND sf.functionname = :name1)
682 SELECT s.*, su.iprestriction
683 FROM {external_services} s
684 JOIN {external_services_functions} sf ON (sf.externalserviceid = s.id AND s.restrictedusers = 1 AND sf.functionname = :name2)
685 JOIN {external_services_users} su ON (su.externalserviceid = s.id AND su.userid = :userid)
686 WHERE s.enabled = 1 AND su.validuntil IS NULL OR su.validuntil < :now";
687 $params = array('userid'=>$USER->id, 'name1'=>$function->name, 'name2'=>$function->name, 'now'=>time());
690 //TODO: token may restrict access to one service only
691 die('not implemented yet');
694 $rs = $DB->get_recordset_sql($sql, $params);
695 // now make sure user may access at least one service
696 $remoteaddr = getremoteaddr();
698 foreach ($rs as $service) {
699 if ($service->requiredcapability and !has_capability($service->requiredcapability, $this->restricted_context)) {
700 continue; // cap required, sorry
702 if ($service->iprestriction and !address_in_subnet($remoteaddr, $service->iprestriction)) {
703 continue; // wrong request source ip, sorry
706 break; // one service is enough, no need to continue
710 throw new invalid_parameter_exception('Access to external function not allowed');
713 // we have all we need now
714 $this->function = $function;
718 * Execute previously loaded function using parameters parsed from the request data.
721 protected function execute() {
722 // validate params, this also sorts the params properly, we need the correct order in the next part
723 $params = call_user_func(array($this->function->classname, 'validate_parameters'), $this->function->parameters_desc, $this->parameters);
726 $this->returns = call_user_func_array(array($this->function->classname, $this->function->methodname), array_values($params));