2009-02-11 06:57:30 +00:00
|
|
|
<?php
|
2009-10-12 21:46:16 +00:00
|
|
|
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2009-02-11 06:57:30 +00:00
|
|
|
/**
|
2009-10-12 21:46:16 +00:00
|
|
|
* Web services utility functions and classes
|
2009-02-11 06:57:30 +00:00
|
|
|
*
|
|
|
|
* @package webservice
|
2009-10-07 10:15:07 +00:00
|
|
|
* @copyright 2009 Moodle Pty Ltd (http://moodle.com)
|
2009-10-12 21:46:16 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2009-02-11 06:57:30 +00:00
|
|
|
*/
|
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
require_once($CFG->libdir.'/externallib.php');
|
2009-09-16 19:03:21 +00:00
|
|
|
|
2010-04-28 13:16:58 +00:00
|
|
|
define('WEBSERVICE_AUTHMETHOD_USERNAME', 0);
|
|
|
|
define('WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN', 1);
|
|
|
|
define('WEBSERVICE_AUTHMETHOD_SESSION_TOKEN', 2);
|
|
|
|
|
2010-05-12 07:53:07 +00:00
|
|
|
/**
|
|
|
|
* General web service library
|
|
|
|
*/
|
|
|
|
class webservice {
|
|
|
|
|
2010-07-02 06:40:36 +00:00
|
|
|
/**
|
|
|
|
* Add a user to the list of authorised user of a given service
|
|
|
|
* @param object $user
|
|
|
|
*/
|
|
|
|
public function add_ws_authorised_user($user) {
|
|
|
|
global $DB;
|
|
|
|
$serviceuser->timecreated = mktime();
|
|
|
|
$DB->insert_record('external_services_users', $user);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a user from a list of allowed user of a service
|
|
|
|
* @param object $user
|
|
|
|
* @param int $serviceid
|
|
|
|
*/
|
|
|
|
public function remove_ws_authorised_user($user, $serviceid) {
|
|
|
|
global $DB;
|
|
|
|
$DB->delete_records('external_services_users',
|
|
|
|
array('externalserviceid' => $serviceid, 'userid' => $user->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update service allowed user settings
|
|
|
|
* @param object $user
|
|
|
|
*/
|
|
|
|
public function update_ws_authorised_user($user) {
|
|
|
|
global $DB;
|
|
|
|
$DB->update_record('external_services_users', $user);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return list of allowed users with their options (ip/timecreated / validuntil...)
|
|
|
|
* for a given service
|
|
|
|
* @param int $serviceid
|
|
|
|
* @return array $users
|
|
|
|
*/
|
|
|
|
public function get_ws_authorised_users($serviceid) {
|
|
|
|
global $DB;
|
|
|
|
$params = array($serviceid);
|
|
|
|
$sql = " SELECT u.id as id, esu.id as serviceuserid, u.email as email, u.firstname as firstname,
|
|
|
|
u.lastname as lastname,
|
|
|
|
esu.iprestriction as iprestriction, esu.validuntil as validuntil,
|
|
|
|
esu.timecreated as timecreated
|
|
|
|
FROM {user} u, {external_services_users} esu
|
|
|
|
WHERE username <> 'guest' AND deleted = 0 AND confirmed = 1
|
|
|
|
AND esu.userid = u.id
|
|
|
|
AND esu.externalserviceid = ?";
|
|
|
|
if (!empty($userid)) {
|
|
|
|
$sql .= ' AND u.id = ?';
|
|
|
|
$params[] = $userid;
|
|
|
|
}
|
|
|
|
|
|
|
|
$users = $DB->get_records_sql($sql, $params);
|
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a authorised user with his options (ip/timecreated / validuntil...)
|
|
|
|
* @param int $serviceid
|
|
|
|
* @param int $userid
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function get_ws_authorised_user($serviceid, $userid) {
|
|
|
|
global $DB;
|
|
|
|
$params = array($serviceid, $userid);
|
|
|
|
$sql = " SELECT u.id as id, esu.id as serviceuserid, u.email as email, u.firstname as firstname,
|
|
|
|
u.lastname as lastname,
|
|
|
|
esu.iprestriction as iprestriction, esu.validuntil as validuntil,
|
|
|
|
esu.timecreated as timecreated
|
|
|
|
FROM {user} u, {external_services_users} esu
|
|
|
|
WHERE username <> 'guest' AND deleted = 0 AND confirmed = 1
|
|
|
|
AND esu.userid = u.id
|
|
|
|
AND esu.externalserviceid = ?
|
|
|
|
AND u.id = ?";
|
|
|
|
$user = $DB->get_record_sql($sql, $params);
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2010-05-12 07:53:07 +00:00
|
|
|
/**
|
|
|
|
* Generate all ws token needed by a user
|
|
|
|
* @param int $userid
|
|
|
|
*/
|
|
|
|
public function generate_user_ws_tokens($userid) {
|
|
|
|
global $CFG, $DB;
|
|
|
|
|
|
|
|
/// generate a token for non admin if web service are enable and the user has the capability to create a token
|
|
|
|
if (!is_siteadmin() && has_capability('moodle/webservice:createtoken', get_context_instance(CONTEXT_SYSTEM), $userid) && !empty($CFG->enablewebservices)) {
|
|
|
|
/// for every service than the user is authorised on, create a token (if it doesn't already exist)
|
|
|
|
|
|
|
|
///get all services which are set to all user (no restricted to specific users)
|
|
|
|
$norestrictedservices = $DB->get_records('external_services', array('restrictedusers' => 0));
|
|
|
|
$serviceidlist = array();
|
|
|
|
foreach ($norestrictedservices as $service) {
|
|
|
|
$serviceidlist[] = $service->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
//get all services which are set to the current user (the current user is specified in the restricted user list)
|
|
|
|
$servicesusers = $DB->get_records('external_services_users', array('userid' => $userid));
|
|
|
|
foreach ($servicesusers as $serviceuser) {
|
|
|
|
if (!in_array($serviceuser->externalserviceid,$serviceidlist)) {
|
|
|
|
$serviceidlist[] = $serviceuser->externalserviceid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//get all services which already have a token set for the current user
|
|
|
|
$usertokens = $DB->get_records('external_tokens', array('userid' => $userid, 'tokentype' => EXTERNAL_TOKEN_PERMANENT));
|
|
|
|
$tokenizedservice = array();
|
|
|
|
foreach ($usertokens as $token) {
|
|
|
|
$tokenizedservice[] = $token->externalserviceid;
|
|
|
|
}
|
|
|
|
|
|
|
|
//create a token for the service which have no token already
|
|
|
|
foreach ($serviceidlist as $serviceid) {
|
|
|
|
if (!in_array($serviceid, $tokenizedservice)) {
|
|
|
|
//create the token for this service
|
|
|
|
$newtoken = new object();
|
|
|
|
$newtoken->token = md5(uniqid(rand(),1));
|
|
|
|
//check that the user has capability on this service
|
|
|
|
$newtoken->tokentype = EXTERNAL_TOKEN_PERMANENT;
|
|
|
|
$newtoken->userid = $userid;
|
|
|
|
$newtoken->externalserviceid = $serviceid;
|
|
|
|
//TODO: find a way to get the context - UPDATE FOLLOWING LINE
|
|
|
|
$newtoken->contextid = get_context_instance(CONTEXT_SYSTEM)->id;
|
|
|
|
$newtoken->creatorid = $userid;
|
|
|
|
$newtoken->timecreated = time();
|
|
|
|
|
|
|
|
$DB->insert_record('external_tokens', $newtoken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all ws user token
|
|
|
|
* @param integer $userid
|
|
|
|
* @return array of token
|
|
|
|
*/
|
|
|
|
public function get_user_ws_tokens($userid) {
|
|
|
|
global $DB;
|
|
|
|
//here retrieve token list (including linked users firstname/lastname and linked services name)
|
|
|
|
$sql = "SELECT
|
|
|
|
t.id, t.creatorid, t.token, u.firstname, u.lastname, s.name, t.validuntil
|
|
|
|
FROM
|
|
|
|
{external_tokens} t, {user} u, {external_services} s
|
|
|
|
WHERE
|
|
|
|
t.userid=? AND t.tokentype = ".EXTERNAL_TOKEN_PERMANENT." AND s.id = t.externalserviceid AND t.userid = u.id";
|
|
|
|
$tokens = $DB->get_records_sql($sql, array( $userid));
|
|
|
|
return $tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a user token that has been created by the user
|
|
|
|
* If doesn't exist a exception is thrown
|
|
|
|
* @param integer $userid
|
|
|
|
* @param integer $tokenid
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function get_created_by_user_ws_token($userid, $tokenid) {
|
|
|
|
global $DB;
|
|
|
|
$sql = "SELECT
|
|
|
|
t.id, t.token, u.firstname, u.lastname, s.name
|
|
|
|
FROM
|
|
|
|
{external_tokens} t, {user} u, {external_services} s
|
|
|
|
WHERE
|
|
|
|
t.creatorid=? AND t.id=? AND t.tokentype = ".EXTERNAL_TOKEN_PERMANENT." AND s.id = t.externalserviceid AND t.userid = u.id";
|
|
|
|
$token = $DB->get_record_sql($sql, array($userid, $tokenid), MUST_EXIST); //must be the token creator
|
|
|
|
return $token;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a user token
|
|
|
|
* @param int $tokenid
|
|
|
|
*/
|
|
|
|
public function delete_user_ws_token($tokenid) {
|
|
|
|
global $DB;
|
|
|
|
$DB->delete_records('external_tokens', array('id'=>$tokenid));
|
|
|
|
}
|
2010-06-03 09:16:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a user token by token
|
|
|
|
* @param string $token
|
|
|
|
* @throws moodle_exception if there is multiple result
|
|
|
|
*/
|
|
|
|
public function get_user_ws_token($token) {
|
|
|
|
global $DB;
|
|
|
|
return $DB->get_record('external_tokens', array('token'=>$token), '*', MUST_EXIST);
|
|
|
|
}
|
2010-05-12 07:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-20 22:30:18 +00:00
|
|
|
/**
|
|
|
|
* Exception indicating access control problem in web service call
|
2009-10-26 21:44:53 +00:00
|
|
|
* @author Petr Skoda (skodak)
|
2009-10-20 22:30:18 +00:00
|
|
|
*/
|
|
|
|
class webservice_access_exception extends moodle_exception {
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
function __construct($debuginfo) {
|
2009-10-20 22:49:39 +00:00
|
|
|
parent::__construct('accessexception', 'webservice', '', null, $debuginfo);
|
2009-10-20 22:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-21 18:25:00 +00:00
|
|
|
/**
|
|
|
|
* Is protocol enabled?
|
|
|
|
* @param string $protocol name of WS protocol
|
|
|
|
* @return bool
|
|
|
|
*/
|
2009-10-12 21:46:16 +00:00
|
|
|
function webservice_protocol_is_enabled($protocol) {
|
|
|
|
global $CFG;
|
2009-09-16 19:03:21 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
if (empty($CFG->enablewebservices)) {
|
|
|
|
return false;
|
2009-09-16 19:03:21 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
$active = explode(',', $CFG->webserviceprotocols);
|
2009-09-16 19:03:21 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
return(in_array($protocol, $active));
|
|
|
|
}
|
2009-09-16 19:03:21 +00:00
|
|
|
|
2009-10-26 21:44:53 +00:00
|
|
|
//=== WS classes ===
|
2009-09-16 19:03:21 +00:00
|
|
|
|
2009-10-21 18:25:00 +00:00
|
|
|
/**
|
2009-11-10 09:06:42 +00:00
|
|
|
* Mandatory interface for all test client classes.
|
2009-10-26 21:44:53 +00:00
|
|
|
* @author Petr Skoda (skodak)
|
2009-10-21 18:25:00 +00:00
|
|
|
*/
|
|
|
|
interface webservice_test_client_interface {
|
|
|
|
/**
|
|
|
|
* Execute test client WS request
|
|
|
|
* @param string $serverurl
|
|
|
|
* @param string $function
|
|
|
|
* @param array $params
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function simpletest($serverurl, $function, $params);
|
|
|
|
}
|
|
|
|
|
2009-02-11 06:57:30 +00:00
|
|
|
/**
|
2009-11-10 09:06:42 +00:00
|
|
|
* Mandatory interface for all web service protocol classes
|
2009-10-26 21:44:53 +00:00
|
|
|
* @author Petr Skoda (skodak)
|
2009-02-11 06:57:30 +00:00
|
|
|
*/
|
2009-10-26 21:44:53 +00:00
|
|
|
interface webservice_server_interface {
|
|
|
|
/**
|
|
|
|
* Process request from client.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function run();
|
|
|
|
}
|
2009-10-14 16:48:38 +00:00
|
|
|
|
2009-10-26 21:44:53 +00:00
|
|
|
/**
|
|
|
|
* Abstract web service base class.
|
|
|
|
* @author Petr Skoda (skodak)
|
|
|
|
*/
|
|
|
|
abstract class webservice_server implements webservice_server_interface {
|
2009-10-14 16:48:38 +00:00
|
|
|
|
|
|
|
/** @property string $wsname name of the web server plugin */
|
|
|
|
protected $wsname = null;
|
|
|
|
|
2009-10-24 16:25:31 +00:00
|
|
|
/** @property string $username name of local user */
|
|
|
|
protected $username = null;
|
|
|
|
|
|
|
|
/** @property string $password password of the local user */
|
|
|
|
protected $password = null;
|
2009-10-24 20:59:59 +00:00
|
|
|
|
2010-01-22 08:21:08 +00:00
|
|
|
/** @property int $userid the local user */
|
|
|
|
protected $userid = null;
|
|
|
|
|
2010-04-28 13:16:58 +00:00
|
|
|
/** @property integer $authmethod authentication method one of WEBSERVICE_AUTHMETHOD_* */
|
|
|
|
protected $authmethod;
|
2009-10-14 16:48:38 +00:00
|
|
|
|
2009-10-26 21:44:53 +00:00
|
|
|
/** @property string $token authentication token*/
|
|
|
|
protected $token = null;
|
2009-10-14 16:48:38 +00:00
|
|
|
|
|
|
|
/** @property object restricted context */
|
|
|
|
protected $restricted_context;
|
|
|
|
|
2009-10-26 21:44:53 +00:00
|
|
|
/** @property int restrict call to one service id*/
|
|
|
|
protected $restricted_serviceid = null;
|
|
|
|
|
2010-04-28 13:16:58 +00:00
|
|
|
/**
|
|
|
|
* Contructor
|
|
|
|
* @param integer $authmethod authentication method one of WEBSERVICE_AUTHMETHOD_*
|
|
|
|
*/
|
|
|
|
public function __construct($authmethod) {
|
|
|
|
$this->authmethod = $authmethod;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-26 21:44:53 +00:00
|
|
|
/**
|
|
|
|
* Authenticate user using username+password or token.
|
|
|
|
* This function sets up $USER global.
|
|
|
|
* It is safe to use has_capability() after this.
|
|
|
|
* This method also verifies user is allowed to use this
|
|
|
|
* server.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function authenticate_user() {
|
|
|
|
global $CFG, $DB;
|
|
|
|
|
|
|
|
if (!NO_MOODLE_COOKIES) {
|
|
|
|
throw new coding_exception('Cookies must be disabled in WS servers!');
|
|
|
|
}
|
|
|
|
|
2010-04-28 13:16:58 +00:00
|
|
|
if ($this->authmethod == WEBSERVICE_AUTHMETHOD_USERNAME) {
|
2009-10-26 21:44:53 +00:00
|
|
|
|
2010-02-01 03:38:28 +00:00
|
|
|
//we check that authentication plugin is enabled
|
|
|
|
//it is only required by simple authentication
|
|
|
|
if (!is_enabled_auth('webservice')) {
|
|
|
|
throw new webservice_access_exception(get_string('wsauthnotenabled', 'webservice'));
|
|
|
|
}
|
2009-10-26 21:44:53 +00:00
|
|
|
|
2010-02-01 03:38:28 +00:00
|
|
|
if (!$auth = get_auth_plugin('webservice')) {
|
|
|
|
throw new webservice_access_exception(get_string('wsauthmissing', 'webservice'));
|
|
|
|
}
|
2009-10-26 21:44:53 +00:00
|
|
|
|
|
|
|
$this->restricted_context = get_context_instance(CONTEXT_SYSTEM);
|
|
|
|
|
|
|
|
if (!$this->username) {
|
2010-01-29 02:15:22 +00:00
|
|
|
throw new webservice_access_exception(get_string('missingusername', 'webservice'));
|
2009-10-26 21:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->password) {
|
2010-01-29 02:15:22 +00:00
|
|
|
throw new webservice_access_exception(get_string('missingpassword', 'webservice'));
|
2009-10-26 21:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$auth->user_login_webservice($this->username, $this->password)) {
|
2010-01-25 02:45:56 +00:00
|
|
|
// log failed login attempts
|
2010-01-25 02:53:03 +00:00
|
|
|
add_to_log(1, 'webservice', get_string('simpleauthlog', 'webservice'), '' , get_string('failedtolog', 'webservice').": ".$this->username."/".$this->password." - ".getremoteaddr() , 0);
|
2010-01-29 02:15:22 +00:00
|
|
|
throw new webservice_access_exception(get_string('wrongusernamepassword', 'webservice'));
|
2009-10-26 21:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$user = $DB->get_record('user', array('username'=>$this->username, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0), '*', MUST_EXIST);
|
|
|
|
|
2010-04-28 13:16:58 +00:00
|
|
|
} else if ($this->authmethod == WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN){
|
|
|
|
$user = $this->authenticate_by_token(EXTERNAL_TOKEN_PERMANENT);
|
2009-10-26 21:44:53 +00:00
|
|
|
} else {
|
2010-04-28 13:16:58 +00:00
|
|
|
$user = $this->authenticate_by_token(EXTERNAL_TOKEN_EMBEDDED);
|
2009-10-26 21:44:53 +00:00
|
|
|
}
|
2010-04-28 13:16:58 +00:00
|
|
|
|
2009-10-26 21:44:53 +00:00
|
|
|
// now fake user login, the session is completely empty too
|
|
|
|
session_set_user($user);
|
2010-01-22 08:21:08 +00:00
|
|
|
$this->userid = $user->id;
|
2009-10-26 21:44:53 +00:00
|
|
|
|
2010-04-30 06:37:43 +00:00
|
|
|
if ($this->authmethod != WEBSERVICE_AUTHMETHOD_SESSION_TOKEN && !has_capability("webservice/$this->wsname:use", $this->restricted_context)) {
|
2010-01-29 02:15:22 +00:00
|
|
|
throw new webservice_access_exception(get_string('accessnotallowed', 'webservice'));
|
2009-10-26 21:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
external_api::set_context_restriction($this->restricted_context);
|
|
|
|
}
|
2010-04-28 13:16:58 +00:00
|
|
|
|
|
|
|
protected function authenticate_by_token($tokentype){
|
|
|
|
global $DB;
|
|
|
|
if (!$token = $DB->get_record('external_tokens', array('token'=>$this->token, 'tokentype'=>$tokentype))) {
|
|
|
|
// log failed login attempts
|
|
|
|
add_to_log(1, 'webservice', get_string('tokenauthlog', 'webservice'), '' , get_string('failedtolog', 'webservice').": ".$this->token. " - ".getremoteaddr() , 0);
|
|
|
|
throw new webservice_access_exception(get_string('invalidtoken', 'webservice'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($token->validuntil and $token->validuntil < time()) {
|
|
|
|
$DB->delete_records('external_tokens', array('token'=>$this->token, 'tokentype'=>$tokentype));
|
|
|
|
throw new webservice_access_exception(get_string('invalidtimedtoken', 'webservice'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($token->sid){//assumes that if sid is set then there must be a valid associated session no matter the token type
|
|
|
|
$session = session_get_instance();
|
|
|
|
if (!$session->session_exists($token->sid)){
|
|
|
|
$DB->delete_records('external_tokens', array('sid'=>$token->sid));
|
|
|
|
throw new webservice_access_exception(get_string('invalidtokensession', 'webservice'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($token->iprestriction and !address_in_subnet(getremoteaddr(), $token->iprestriction)) {
|
|
|
|
add_to_log(1, 'webservice', get_string('tokenauthlog', 'webservice'), '' , get_string('failedtolog', 'webservice').": ".getremoteaddr() , 0);
|
|
|
|
throw new webservice_access_exception(get_string('invalidiptoken', 'webservice'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->restricted_context = get_context_instance_by_id($token->contextid);
|
|
|
|
$this->restricted_serviceid = $token->externalserviceid;
|
|
|
|
|
|
|
|
$user = $DB->get_record('user', array('id'=>$token->userid, 'deleted'=>0), '*', MUST_EXIST);
|
|
|
|
|
|
|
|
// log token access
|
|
|
|
$DB->set_field('external_tokens', 'lastaccess', time(), array('id'=>$token->id));
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
|
|
|
|
}
|
2009-10-26 21:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Special abstraction of our srvices that allows
|
|
|
|
* interaction with stock Zend ws servers.
|
|
|
|
* @author Petr Skoda (skodak)
|
|
|
|
*/
|
|
|
|
abstract class webservice_zend_server extends webservice_server {
|
|
|
|
|
2009-12-18 03:19:22 +00:00
|
|
|
/** @property string name of the zend server class : Zend_XmlRpc_Server, Zend_Soap_Server, Zend_Soap_AutoDiscover, ...*/
|
2009-10-26 21:44:53 +00:00
|
|
|
protected $zend_class;
|
|
|
|
|
|
|
|
/** @property object Zend server instance */
|
|
|
|
protected $zend_server;
|
|
|
|
|
|
|
|
/** @property string $service_class virtual web service class with all functions user name execute, created on the fly */
|
|
|
|
protected $service_class;
|
|
|
|
|
2009-10-14 16:48:38 +00:00
|
|
|
/**
|
|
|
|
* Contructor
|
2010-04-28 13:16:58 +00:00
|
|
|
* @param integer $authmethod authentication method - one of WEBSERVICE_AUTHMETHOD_*
|
2009-10-14 16:48:38 +00:00
|
|
|
*/
|
2010-04-28 13:16:58 +00:00
|
|
|
public function __construct($authmethod, $zend_class) {
|
|
|
|
parent::__construct($authmethod);
|
2009-10-14 16:48:38 +00:00
|
|
|
$this->zend_class = $zend_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process request from client.
|
|
|
|
* @param bool $simple use simple authentication
|
|
|
|
* @return void
|
|
|
|
*/
|
2009-10-24 15:28:01 +00:00
|
|
|
public function run() {
|
2009-10-14 16:48:38 +00:00
|
|
|
// we will probably need a lot of memory in some functions
|
|
|
|
@raise_memory_limit('128M');
|
|
|
|
|
|
|
|
// set some longer timeout, this script is not sending any output,
|
|
|
|
// this means we need to manually extend the timeout operations
|
|
|
|
// that need longer time to finish
|
|
|
|
external_api::set_timeout();
|
|
|
|
|
2009-10-20 22:49:39 +00:00
|
|
|
// now create the instance of zend server
|
|
|
|
$this->init_zend_server();
|
|
|
|
|
2009-10-14 16:48:38 +00:00
|
|
|
// set up exception handler first, we want to sent them back in correct format that
|
|
|
|
// the other system understands
|
|
|
|
// we do not need to call the original default handler because this ws handler does everything
|
|
|
|
set_exception_handler(array($this, 'exception_handler'));
|
|
|
|
|
2009-10-24 16:25:31 +00:00
|
|
|
// init all properties from the request data
|
|
|
|
$this->parse_request();
|
|
|
|
|
2009-10-14 16:48:38 +00:00
|
|
|
// this sets up $USER and $SESSION and context restrictions
|
|
|
|
$this->authenticate_user();
|
|
|
|
|
|
|
|
// make a list of all functions user is allowed to excecute
|
|
|
|
$this->init_service_class();
|
|
|
|
|
2009-10-24 15:28:01 +00:00
|
|
|
// tell server what functions are available
|
2009-10-14 16:48:38 +00:00
|
|
|
$this->zend_server->setClass($this->service_class);
|
2010-07-08 08:40:21 +00:00
|
|
|
|
2010-01-25 02:45:56 +00:00
|
|
|
//log the web service request
|
2010-01-25 02:53:03 +00:00
|
|
|
add_to_log(1, 'webservice', '', '' , $this->zend_class." ".getremoteaddr() , 0, $this->userid);
|
2010-01-25 02:45:56 +00:00
|
|
|
|
2009-10-24 15:28:01 +00:00
|
|
|
// execute and return response, this sends some headers too
|
2009-10-14 16:48:38 +00:00
|
|
|
$response = $this->zend_server->handle();
|
2009-10-24 15:28:01 +00:00
|
|
|
|
2009-10-14 16:48:38 +00:00
|
|
|
// session cleanup
|
|
|
|
$this->session_cleanup();
|
|
|
|
|
2009-10-20 22:59:10 +00:00
|
|
|
//finally send the result
|
|
|
|
$this->send_headers();
|
2009-10-14 16:48:38 +00:00
|
|
|
echo $response;
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load virtual class needed for Zend api
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function init_service_class() {
|
|
|
|
global $USER, $DB;
|
|
|
|
|
|
|
|
// first ofall get a complete list of services user is allowed to access
|
|
|
|
|
2009-10-26 21:44:53 +00:00
|
|
|
if ($this->restricted_serviceid) {
|
|
|
|
$params = array('sid1'=>$this->restricted_serviceid, 'sid2'=>$this->restricted_serviceid);
|
|
|
|
$wscond1 = 'AND s.id = :sid1';
|
|
|
|
$wscond2 = 'AND s.id = :sid2';
|
|
|
|
} else {
|
|
|
|
$params = array();
|
|
|
|
$wscond1 = '';
|
|
|
|
$wscond2 = '';
|
|
|
|
}
|
2009-10-14 16:48:38 +00:00
|
|
|
|
2009-10-26 21:44:53 +00:00
|
|
|
// now make sure the function is listed in at least one service user is allowed to use
|
|
|
|
// allow access only if:
|
|
|
|
// 1/ entry in the external_services_users table if required
|
|
|
|
// 2/ validuntil not reached
|
|
|
|
// 3/ has capability if specified in service desc
|
|
|
|
// 4/ iprestriction
|
2009-10-14 16:48:38 +00:00
|
|
|
|
2009-10-26 21:44:53 +00:00
|
|
|
$sql = "SELECT s.*, NULL AS iprestriction
|
|
|
|
FROM {external_services} s
|
|
|
|
JOIN {external_services_functions} sf ON (sf.externalserviceid = s.id AND s.restrictedusers = 0)
|
|
|
|
WHERE s.enabled = 1 $wscond1
|
2009-10-14 16:48:38 +00:00
|
|
|
|
2009-10-26 21:44:53 +00:00
|
|
|
UNION
|
|
|
|
|
|
|
|
SELECT s.*, su.iprestriction
|
|
|
|
FROM {external_services} s
|
|
|
|
JOIN {external_services_functions} sf ON (sf.externalserviceid = s.id AND s.restrictedusers = 1)
|
|
|
|
JOIN {external_services_users} su ON (su.externalserviceid = s.id AND su.userid = :userid)
|
|
|
|
WHERE s.enabled = 1 AND su.validuntil IS NULL OR su.validuntil < :now $wscond2";
|
|
|
|
|
|
|
|
$params = array_merge($params, array('userid'=>$USER->id, 'now'=>time()));
|
2009-10-14 16:48:38 +00:00
|
|
|
|
|
|
|
$serviceids = array();
|
|
|
|
$rs = $DB->get_recordset_sql($sql, $params);
|
|
|
|
|
|
|
|
// now make sure user may access at least one service
|
|
|
|
$remoteaddr = getremoteaddr();
|
|
|
|
$allowed = false;
|
|
|
|
foreach ($rs as $service) {
|
|
|
|
if (isset($serviceids[$service->id])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($service->requiredcapability and !has_capability($service->requiredcapability, $this->restricted_context)) {
|
|
|
|
continue; // cap required, sorry
|
|
|
|
}
|
|
|
|
if ($service->iprestriction and !address_in_subnet($remoteaddr, $service->iprestriction)) {
|
|
|
|
continue; // wrong request source ip, sorry
|
|
|
|
}
|
|
|
|
$serviceids[$service->id] = $service->id;
|
|
|
|
}
|
|
|
|
$rs->close();
|
|
|
|
|
|
|
|
// now get the list of all functions
|
|
|
|
if ($serviceids) {
|
|
|
|
list($serviceids, $params) = $DB->get_in_or_equal($serviceids);
|
|
|
|
$sql = "SELECT f.*
|
|
|
|
FROM {external_functions} f
|
|
|
|
WHERE f.name IN (SELECT sf.functionname
|
|
|
|
FROM {external_services_functions} sf
|
|
|
|
WHERE sf.externalserviceid $serviceids)";
|
|
|
|
$functions = $DB->get_records_sql($sql, $params);
|
|
|
|
} else {
|
|
|
|
$functions = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
// now make the virtual WS class with all the fuctions for this particular user
|
|
|
|
$methods = '';
|
|
|
|
foreach ($functions as $function) {
|
|
|
|
$methods .= $this->get_virtual_method_code($function);
|
|
|
|
}
|
|
|
|
|
2009-10-20 22:30:18 +00:00
|
|
|
// let's use unique class name, there might be problem in unit tests
|
2009-10-14 16:48:38 +00:00
|
|
|
$classname = 'webservices_virtual_class_000000';
|
|
|
|
while(class_exists($classname)) {
|
|
|
|
$classname++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$code = '
|
|
|
|
/**
|
|
|
|
* Virtual class web services for user id '.$USER->id.' in context '.$this->restricted_context->id.'.
|
|
|
|
*/
|
|
|
|
class '.$classname.' {
|
|
|
|
'.$methods.'
|
|
|
|
}
|
|
|
|
';
|
2009-10-21 18:25:00 +00:00
|
|
|
|
2009-10-14 16:48:38 +00:00
|
|
|
// load the virtual class definition into memory
|
|
|
|
eval($code);
|
|
|
|
$this->service_class = $classname;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns virtual method code
|
|
|
|
* @param object $function
|
|
|
|
* @return string PHP code
|
|
|
|
*/
|
|
|
|
protected function get_virtual_method_code($function) {
|
|
|
|
global $CFG;
|
|
|
|
|
2009-10-20 22:30:18 +00:00
|
|
|
$function = external_function_info($function);
|
2009-10-14 16:48:38 +00:00
|
|
|
|
2010-07-12 13:01:56 +00:00
|
|
|
//arguments in function declaration line with defaults.
|
|
|
|
$paramanddefaults = array();
|
|
|
|
//arguments used as parameters for external lib call.
|
2009-10-14 16:48:38 +00:00
|
|
|
$params = array();
|
|
|
|
$params_desc = array();
|
2009-10-16 07:25:46 +00:00
|
|
|
foreach ($function->parameters_desc->keys as $name=>$keydesc) {
|
2010-04-01 03:17:34 +00:00
|
|
|
$param = '$'.$name;
|
2010-07-12 13:01:56 +00:00
|
|
|
$paramanddefault = $param;
|
2010-04-01 03:17:34 +00:00
|
|
|
//need to generate the default if there is any
|
|
|
|
if ($keydesc instanceof external_value) {
|
|
|
|
if ($keydesc->required == VALUE_DEFAULT) {
|
|
|
|
if ($keydesc->default===null) {
|
2010-07-12 13:01:56 +00:00
|
|
|
$paramanddefault .= '=null';
|
2010-04-01 03:17:34 +00:00
|
|
|
} else {
|
|
|
|
switch($keydesc->type) {
|
|
|
|
case PARAM_BOOL:
|
2010-07-12 13:01:56 +00:00
|
|
|
$paramanddefault .= '='.$keydesc->default; break;
|
2010-04-01 03:17:34 +00:00
|
|
|
case PARAM_INT:
|
2010-07-12 13:01:56 +00:00
|
|
|
$paramanddefault .= '='.$keydesc->default; break;
|
2010-04-01 03:17:34 +00:00
|
|
|
case PARAM_FLOAT;
|
2010-07-12 13:01:56 +00:00
|
|
|
$paramanddefault .= '='.$keydesc->default; break;
|
2010-04-01 03:17:34 +00:00
|
|
|
default:
|
2010-07-12 13:01:56 +00:00
|
|
|
$paramanddefault .= '=\''.$keydesc->default.'\'';
|
2010-04-01 03:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ($keydesc->required == VALUE_OPTIONAL) {
|
|
|
|
//it does make sens to declare a parameter VALUE_OPTIONAL
|
|
|
|
//VALUE_OPTIONAL is used only for array/object key
|
|
|
|
throw new moodle_exception('parametercannotbevalueoptional');
|
|
|
|
}
|
|
|
|
} else { //for the moment we do not support default for other structure types
|
2010-07-07 08:15:26 +00:00
|
|
|
if ($keydesc->required == VALUE_DEFAULT) {
|
|
|
|
//accept empty array as default
|
|
|
|
if (isset($keydesc->default) and is_array($keydesc->default)
|
|
|
|
and empty($keydesc->default)) {
|
2010-07-14 05:42:21 +00:00
|
|
|
$paramanddefault .= '=array()';
|
2010-07-07 08:15:26 +00:00
|
|
|
} else {
|
|
|
|
throw new moodle_exception('errornotemptydefaultparamarray', 'webservice', '', $name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($keydesc->required == VALUE_OPTIONAL) {
|
|
|
|
throw new moodle_exception('erroroptionalparamarray', 'webservice', '', $name);
|
2010-04-01 03:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-12 13:01:56 +00:00
|
|
|
$params[] = $param;
|
|
|
|
$paramanddefaults[] = $paramanddefault;
|
2009-10-16 07:25:46 +00:00
|
|
|
$type = 'string';
|
|
|
|
if ($keydesc instanceof external_value) {
|
|
|
|
switch($keydesc->type) {
|
|
|
|
case PARAM_BOOL: // 0 or 1 only for now
|
|
|
|
case PARAM_INT:
|
|
|
|
$type = 'int'; break;
|
|
|
|
case PARAM_FLOAT;
|
|
|
|
$type = 'double'; break;
|
|
|
|
default:
|
|
|
|
$type = 'string';
|
|
|
|
}
|
|
|
|
} else if ($keydesc instanceof external_single_structure) {
|
2010-07-09 04:07:37 +00:00
|
|
|
$type = 'object|struct';
|
2009-10-16 07:25:46 +00:00
|
|
|
} else if ($keydesc instanceof external_multiple_structure) {
|
|
|
|
$type = 'array';
|
|
|
|
}
|
|
|
|
$params_desc[] = ' * @param '.$type.' $'.$name.' '.$keydesc->desc;
|
2009-10-14 16:48:38 +00:00
|
|
|
}
|
2010-07-12 13:01:56 +00:00
|
|
|
$params = implode(', ', $params);
|
|
|
|
$paramanddefaults = implode(', ', $paramanddefaults);
|
|
|
|
$params_desc = implode("\n", $params_desc);
|
2010-07-14 05:42:21 +00:00
|
|
|
|
2010-02-10 08:44:46 +00:00
|
|
|
$serviceclassmethodbody = $this->service_class_method_body($function, $params);
|
2009-10-14 16:48:38 +00:00
|
|
|
|
2009-10-16 07:25:46 +00:00
|
|
|
if (is_null($function->returns_desc)) {
|
|
|
|
$return = ' * @return void';
|
|
|
|
} else {
|
|
|
|
$type = 'string';
|
|
|
|
if ($function->returns_desc instanceof external_value) {
|
|
|
|
switch($function->returns_desc->type) {
|
|
|
|
case PARAM_BOOL: // 0 or 1 only for now
|
|
|
|
case PARAM_INT:
|
|
|
|
$type = 'int'; break;
|
|
|
|
case PARAM_FLOAT;
|
|
|
|
$type = 'double'; break;
|
|
|
|
default:
|
|
|
|
$type = 'string';
|
|
|
|
}
|
|
|
|
} else if ($function->returns_desc instanceof external_single_structure) {
|
2010-07-05 08:51:57 +00:00
|
|
|
$type = 'object|struct'; //only 'object' is supported by SOAP, 'struct' by XML-RPC MDL-23083
|
2009-10-16 07:25:46 +00:00
|
|
|
} else if ($function->returns_desc instanceof external_multiple_structure) {
|
|
|
|
$type = 'array';
|
|
|
|
}
|
|
|
|
$return = ' * @return '.$type.' '.$function->returns_desc->desc;
|
|
|
|
}
|
2009-10-19 23:14:26 +00:00
|
|
|
|
2009-10-26 21:44:53 +00:00
|
|
|
// now crate the virtual method that calls the ext implementation
|
2009-10-14 16:48:38 +00:00
|
|
|
|
|
|
|
$code = '
|
|
|
|
/**
|
2009-10-20 22:30:18 +00:00
|
|
|
* '.$function->description.'
|
|
|
|
*
|
2009-10-14 16:48:38 +00:00
|
|
|
'.$params_desc.'
|
2009-10-16 07:25:46 +00:00
|
|
|
'.$return.'
|
2009-10-14 16:48:38 +00:00
|
|
|
*/
|
2010-07-12 13:01:56 +00:00
|
|
|
public function '.$function->name.'('.$paramanddefaults.') {
|
2010-02-11 03:39:02 +00:00
|
|
|
'.$serviceclassmethodbody.'
|
2009-10-14 16:48:38 +00:00
|
|
|
}
|
|
|
|
';
|
|
|
|
return $code;
|
|
|
|
}
|
2010-02-10 08:44:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* You can override this function in your child class to add extra code into the dynamically
|
|
|
|
* created service class. For example it is used in the amf server to cast types of parameters and to
|
|
|
|
* cast the return value to the types as specified in the return value description.
|
|
|
|
* @param unknown_type $function
|
|
|
|
* @param unknown_type $params
|
|
|
|
* @return string body of the method for $function ie. everything within the {} of the method declaration.
|
|
|
|
*/
|
|
|
|
protected function service_class_method_body($function, $params){
|
2010-07-09 04:07:37 +00:00
|
|
|
//cast the param from object to array (validate_parameters except array only)
|
|
|
|
$castingcode = '';
|
|
|
|
if ($params){
|
2010-07-20 01:38:54 +00:00
|
|
|
$paramstocast = explode(',', $params);
|
2010-07-09 04:07:37 +00:00
|
|
|
foreach ($paramstocast as $paramtocast) {
|
2010-07-12 07:06:08 +00:00
|
|
|
//clean the parameter from any white space
|
|
|
|
$paramtocast = trim($paramtocast);
|
2010-07-09 04:07:37 +00:00
|
|
|
$castingcode .= $paramtocast .
|
|
|
|
'=webservice_zend_server::cast_objects_to_array('.$paramtocast.');';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-02-22 07:07:44 +00:00
|
|
|
$descriptionmethod = $function->methodname.'_returns()';
|
2010-04-28 13:16:58 +00:00
|
|
|
$callforreturnvaluedesc = $function->classname.'::'.$descriptionmethod;
|
2010-07-09 04:07:37 +00:00
|
|
|
return $castingcode . ' if ('.$callforreturnvaluedesc.' == null) {'.
|
|
|
|
$function->classname.'::'.$function->methodname.'('.$params.');
|
2010-03-29 06:48:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return external_api::clean_returnvalue('.$callforreturnvaluedesc.', '.$function->classname.'::'.$function->methodname.'('.$params.'));';
|
2010-02-10 08:44:46 +00:00
|
|
|
}
|
2010-02-22 07:07:44 +00:00
|
|
|
|
2009-10-14 16:48:38 +00:00
|
|
|
/**
|
2010-07-09 04:07:37 +00:00
|
|
|
* Recursive function to recurse down into a complex variable and convert all
|
|
|
|
* objects to arrays.
|
|
|
|
* @param mixed $param value to cast
|
|
|
|
* @return mixed Cast value
|
|
|
|
*/
|
|
|
|
public static function cast_objects_to_array($param){
|
|
|
|
if (is_object($param)){
|
|
|
|
$param = (array)$param;
|
|
|
|
}
|
|
|
|
if (is_array($param)){
|
|
|
|
$toreturn = array();
|
|
|
|
foreach ($param as $key=> $param){
|
|
|
|
$toreturn[$key] = self::cast_objects_to_array($param);
|
|
|
|
}
|
|
|
|
return $toreturn;
|
|
|
|
} else {
|
|
|
|
return $param;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-10-24 20:59:59 +00:00
|
|
|
* Set up zend service class
|
2009-10-14 16:48:38 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function init_zend_server() {
|
|
|
|
$this->zend_server = new $this->zend_class();
|
|
|
|
}
|
|
|
|
|
2009-10-24 16:25:31 +00:00
|
|
|
/**
|
|
|
|
* This method parses the $_REQUEST superglobal and looks for
|
|
|
|
* the following information:
|
|
|
|
* 1/ user authentication - username+password or token (wsusername, wspassword and wstoken parameters)
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function parse_request() {
|
2010-04-28 13:16:58 +00:00
|
|
|
if ($this->authmethod == WEBSERVICE_AUTHMETHOD_USERNAME) {
|
2009-10-26 21:44:53 +00:00
|
|
|
//note: some clients have problems with entity encoding :-(
|
2009-10-24 16:25:31 +00:00
|
|
|
if (isset($_REQUEST['wsusername'])) {
|
|
|
|
$this->username = $_REQUEST['wsusername'];
|
|
|
|
}
|
|
|
|
if (isset($_REQUEST['wspassword'])) {
|
|
|
|
$this->password = $_REQUEST['wspassword'];
|
|
|
|
}
|
|
|
|
} else {
|
2009-10-26 21:44:53 +00:00
|
|
|
if (isset($_REQUEST['wstoken'])) {
|
|
|
|
$this->token = $_REQUEST['wstoken'];
|
2009-10-14 16:48:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-20 22:59:10 +00:00
|
|
|
/**
|
|
|
|
* Internal implementation - sending of page headers.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function send_headers() {
|
|
|
|
header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
|
|
|
|
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
|
|
|
|
header('Pragma: no-cache');
|
|
|
|
header('Accept-Ranges: none');
|
|
|
|
}
|
|
|
|
|
2009-10-14 16:48:38 +00:00
|
|
|
/**
|
|
|
|
* Specialised exception handler, we can not use the standard one because
|
|
|
|
* it can not just print html to output.
|
|
|
|
*
|
|
|
|
* @param exception $ex
|
|
|
|
* @return void does not return
|
|
|
|
*/
|
|
|
|
public function exception_handler($ex) {
|
|
|
|
// detect active db transactions, rollback and log as error
|
2009-11-01 10:07:58 +00:00
|
|
|
abort_all_db_transactions();
|
2009-10-14 16:48:38 +00:00
|
|
|
|
|
|
|
// some hacks might need a cleanup hook
|
|
|
|
$this->session_cleanup($ex);
|
|
|
|
|
2009-10-20 22:59:10 +00:00
|
|
|
// now let the plugin send the exception to client
|
2009-10-24 20:59:59 +00:00
|
|
|
$this->send_error($ex);
|
2009-10-20 22:59:10 +00:00
|
|
|
|
2009-10-14 16:48:38 +00:00
|
|
|
// not much else we can do now, add some logging later
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2009-10-24 20:59:59 +00:00
|
|
|
/**
|
|
|
|
* Send the error information to the WS client
|
|
|
|
* formatted as XML document.
|
|
|
|
* @param exception $ex
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function send_error($ex=null) {
|
|
|
|
$this->send_headers();
|
|
|
|
echo $this->zend_server->fault($ex);
|
|
|
|
}
|
2009-10-26 21:44:53 +00:00
|
|
|
|
2009-10-14 16:48:38 +00:00
|
|
|
/**
|
|
|
|
* Future hook needed for emulated sessions.
|
|
|
|
* @param exception $exception null means normal termination, $exception received when WS call failed
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function session_cleanup($exception=null) {
|
2010-04-28 13:16:58 +00:00
|
|
|
if ($this->authmethod == WEBSERVICE_AUTHMETHOD_USERNAME) {
|
2009-10-14 16:48:38 +00:00
|
|
|
// nothing needs to be done, there is no persistent session
|
|
|
|
} else {
|
|
|
|
// close emulated session if used
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
}
|
|
|
|
|
2009-09-04 09:25:55 +00:00
|
|
|
/**
|
2009-10-12 21:46:16 +00:00
|
|
|
* Web Service server base class, this class handles both
|
|
|
|
* simple and token authentication.
|
|
|
|
* @author Petr Skoda (skodak)
|
2009-09-04 09:25:55 +00:00
|
|
|
*/
|
2009-10-26 21:44:53 +00:00
|
|
|
abstract class webservice_base_server extends webservice_server {
|
2009-10-14 16:48:38 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
/** @property array $parameters the function parameters - the real values submitted in the request */
|
|
|
|
protected $parameters = null;
|
|
|
|
|
|
|
|
/** @property string $functionname the name of the function that is executed */
|
|
|
|
protected $functionname = null;
|
|
|
|
|
|
|
|
/** @property object $function full function description */
|
|
|
|
protected $function = null;
|
|
|
|
|
|
|
|
/** @property mixed $returns function return value */
|
|
|
|
protected $returns = null;
|
2009-02-11 06:57:30 +00:00
|
|
|
|
2009-02-13 03:08:35 +00:00
|
|
|
/**
|
2009-10-12 21:46:16 +00:00
|
|
|
* This method parses the request input, it needs to get:
|
|
|
|
* 1/ user authentication - username+password or token
|
|
|
|
* 2/ function name
|
|
|
|
* 3/ function parameters
|
|
|
|
*
|
|
|
|
* @return void
|
2009-02-13 03:08:35 +00:00
|
|
|
*/
|
2009-10-12 21:46:16 +00:00
|
|
|
abstract protected function parse_request();
|
2009-02-13 03:08:35 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
/**
|
|
|
|
* Send the result of function call to the WS client.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
abstract protected function send_response();
|
2009-02-13 03:08:35 +00:00
|
|
|
|
2009-03-04 03:18:14 +00:00
|
|
|
/**
|
2009-10-12 21:46:16 +00:00
|
|
|
* Send the error information to the WS client.
|
|
|
|
* @param exception $ex
|
|
|
|
* @return void
|
2009-03-04 03:18:14 +00:00
|
|
|
*/
|
2009-10-12 21:46:16 +00:00
|
|
|
abstract protected function send_error($ex=null);
|
2009-03-04 03:18:14 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
/**
|
|
|
|
* Process request from client.
|
|
|
|
* @return void
|
|
|
|
*/
|
2009-10-24 15:28:01 +00:00
|
|
|
public function run() {
|
2009-10-12 21:46:16 +00:00
|
|
|
// we will probably need a lot of memory in some functions
|
|
|
|
@raise_memory_limit('128M');
|
2009-03-04 03:18:14 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
// set some longer timeout, this script is not sending any output,
|
|
|
|
// this means we need to manually extend the timeout operations
|
|
|
|
// that need longer time to finish
|
|
|
|
external_api::set_timeout();
|
2009-03-04 03:18:14 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
// set up exception handler first, we want to sent them back in correct format that
|
|
|
|
// the other system understands
|
|
|
|
// we do not need to call the original default handler because this ws handler does everything
|
|
|
|
set_exception_handler(array($this, 'exception_handler'));
|
2009-02-11 06:57:30 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
// init all properties from the request data
|
|
|
|
$this->parse_request();
|
2009-02-11 06:57:30 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
// authenticate user, this has to be done after the request parsing
|
|
|
|
// this also sets up $USER and $SESSION
|
|
|
|
$this->authenticate_user();
|
2009-02-11 06:57:30 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
// find all needed function info and make sure user may actually execute the function
|
|
|
|
$this->load_function_info();
|
2010-01-22 08:21:08 +00:00
|
|
|
|
|
|
|
//log the web service request
|
2010-01-25 02:53:03 +00:00
|
|
|
add_to_log(1, 'webservice', $this->functionname, '' , getremoteaddr() , 0, $this->userid);
|
2009-03-31 03:29:01 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
// finally, execute the function - any errors are catched by the default exception handler
|
|
|
|
$this->execute();
|
2009-02-11 06:57:30 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
// send the results back in correct format
|
|
|
|
$this->send_response();
|
2009-02-11 06:57:30 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
// session cleanup
|
|
|
|
$this->session_cleanup();
|
2009-02-11 06:57:30 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
die;
|
2009-03-31 03:29:01 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
/**
|
|
|
|
* Specialised exception handler, we can not use the standard one because
|
|
|
|
* it can not just print html to output.
|
|
|
|
*
|
|
|
|
* @param exception $ex
|
|
|
|
* @return void does not return
|
|
|
|
*/
|
|
|
|
public function exception_handler($ex) {
|
|
|
|
// detect active db transactions, rollback and log as error
|
2009-11-01 10:07:58 +00:00
|
|
|
abort_all_db_transactions();
|
2009-02-11 06:57:30 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
// some hacks might need a cleanup hook
|
|
|
|
$this->session_cleanup($ex);
|
2009-02-11 06:57:30 +00:00
|
|
|
|
2009-10-20 22:59:10 +00:00
|
|
|
// now let the plugin send the exception to client
|
|
|
|
$this->send_error($ex);
|
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
// not much else we can do now, add some logging later
|
|
|
|
exit(1);
|
2009-03-31 03:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-10-12 21:46:16 +00:00
|
|
|
* Future hook needed for emulated sessions.
|
|
|
|
* @param exception $exception null means normal termination, $exception received when WS call failed
|
|
|
|
* @return void
|
2009-03-31 03:29:01 +00:00
|
|
|
*/
|
2009-10-12 21:46:16 +00:00
|
|
|
protected function session_cleanup($exception=null) {
|
2010-05-25 03:41:24 +00:00
|
|
|
if ($this->authmethod == WEBSERVICE_AUTHMETHOD_USERNAME) {
|
2009-10-12 21:46:16 +00:00
|
|
|
// nothing needs to be done, there is no persistent session
|
|
|
|
} else {
|
|
|
|
// close emulated session if used
|
|
|
|
}
|
2009-03-31 03:29:01 +00:00
|
|
|
}
|
|
|
|
|
2009-02-13 03:08:35 +00:00
|
|
|
/**
|
2009-10-12 21:46:16 +00:00
|
|
|
* Fetches the function description from database,
|
|
|
|
* verifies user is allowed to use this function and
|
|
|
|
* loads all paremeters and return descriptions.
|
|
|
|
* @return void
|
2009-02-13 03:08:35 +00:00
|
|
|
*/
|
2009-10-12 21:46:16 +00:00
|
|
|
protected function load_function_info() {
|
|
|
|
global $DB, $USER, $CFG;
|
2009-02-24 05:11:04 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
if (empty($this->functionname)) {
|
|
|
|
throw new invalid_parameter_exception('Missing function name');
|
|
|
|
}
|
2009-02-13 03:08:35 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
// function must exist
|
2009-10-20 22:30:18 +00:00
|
|
|
$function = external_function_info($this->functionname);
|
2009-10-12 21:46:16 +00:00
|
|
|
|
2009-10-26 21:44:53 +00:00
|
|
|
if ($this->restricted_serviceid) {
|
|
|
|
$params = array('sid1'=>$this->restricted_serviceid, 'sid2'=>$this->restricted_serviceid);
|
|
|
|
$wscond1 = 'AND s.id = :sid1';
|
|
|
|
$wscond2 = 'AND s.id = :sid2';
|
|
|
|
} else {
|
|
|
|
$params = array();
|
|
|
|
$wscond1 = '';
|
|
|
|
$wscond2 = '';
|
|
|
|
}
|
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
// now let's verify access control
|
2010-01-14 08:17:00 +00:00
|
|
|
|
|
|
|
// now make sure the function is listed in at least one service user is allowed to use
|
|
|
|
// allow access only if:
|
|
|
|
// 1/ entry in the external_services_users table if required
|
|
|
|
// 2/ validuntil not reached
|
|
|
|
// 3/ has capability if specified in service desc
|
|
|
|
// 4/ iprestriction
|
|
|
|
|
|
|
|
$sql = "SELECT s.*, NULL AS iprestriction
|
|
|
|
FROM {external_services} s
|
|
|
|
JOIN {external_services_functions} sf ON (sf.externalserviceid = s.id AND s.restrictedusers = 0 AND sf.functionname = :name1)
|
|
|
|
WHERE s.enabled = 1 $wscond1
|
|
|
|
|
|
|
|
UNION
|
|
|
|
|
|
|
|
SELECT s.*, su.iprestriction
|
|
|
|
FROM {external_services} s
|
|
|
|
JOIN {external_services_functions} sf ON (sf.externalserviceid = s.id AND s.restrictedusers = 1 AND sf.functionname = :name2)
|
|
|
|
JOIN {external_services_users} su ON (su.externalserviceid = s.id AND su.userid = :userid)
|
|
|
|
WHERE s.enabled = 1 AND su.validuntil IS NULL OR su.validuntil < :now $wscond2";
|
|
|
|
$params = array_merge($params, array('userid'=>$USER->id, 'name1'=>$function->name, 'name2'=>$function->name, 'now'=>time()));
|
2009-10-14 16:48:38 +00:00
|
|
|
|
|
|
|
$rs = $DB->get_recordset_sql($sql, $params);
|
|
|
|
// now make sure user may access at least one service
|
|
|
|
$remoteaddr = getremoteaddr();
|
|
|
|
$allowed = false;
|
|
|
|
foreach ($rs as $service) {
|
|
|
|
if ($service->requiredcapability and !has_capability($service->requiredcapability, $this->restricted_context)) {
|
|
|
|
continue; // cap required, sorry
|
2009-10-12 21:46:16 +00:00
|
|
|
}
|
2009-10-14 16:48:38 +00:00
|
|
|
if ($service->iprestriction and !address_in_subnet($remoteaddr, $service->iprestriction)) {
|
|
|
|
continue; // wrong request source ip, sorry
|
2009-10-12 21:46:16 +00:00
|
|
|
}
|
2009-10-14 16:48:38 +00:00
|
|
|
$allowed = true;
|
|
|
|
break; // one service is enough, no need to continue
|
|
|
|
}
|
|
|
|
$rs->close();
|
|
|
|
if (!$allowed) {
|
2009-10-20 23:00:57 +00:00
|
|
|
throw new webservice_access_exception('Access to external function not allowed');
|
2009-10-12 21:46:16 +00:00
|
|
|
}
|
2009-09-09 07:55:03 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
// we have all we need now
|
|
|
|
$this->function = $function;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute previously loaded function using parameters parsed from the request data.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function execute() {
|
|
|
|
// validate params, this also sorts the params properly, we need the correct order in the next part
|
|
|
|
$params = call_user_func(array($this->function->classname, 'validate_parameters'), $this->function->parameters_desc, $this->parameters);
|
2009-09-09 07:55:03 +00:00
|
|
|
|
2009-10-12 21:46:16 +00:00
|
|
|
// execute - yay!
|
|
|
|
$this->returns = call_user_func_array(array($this->function->classname, $this->function->methodname), array_values($params));
|
2009-09-09 07:55:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|