web serviceMDL-12886 refactor servers into object + add Zend_soap and Zend_xmlrpc server

This commit is contained in:
jerome 2009-02-11 06:57:30 +00:00
parent 625b51d4e0
commit 06e7fadc43
79 changed files with 10475 additions and 211 deletions

61
webservice/amf/lib.php Normal file
View file

@ -0,0 +1,61 @@
<?php
/**
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* LICENSE
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
require_once('../lib.php');
/*
* AMF server class
*/
final class amf_server extends webservice_server {
public function __construct() {
//set web service proctol name
$this->set_protocolname("Amf");
}
/**
* Run the AMF server
*/
public function run() {
include "Zend/Loader.php";
Zend_Loader::registerAutoload();
//retrieve the api name
$classpath = optional_param(classpath,'user',PARAM_ALPHA);
require_once(dirname(__FILE__) . '/../../'.$classpath.'/external.php');
/// run the Zend AMF server
$server = new Zend_Amf_Server();
$server->setClass($classpath."_external");
$response = $server->handle();
echo $response;
}
}
?>

View file

@ -1,32 +1,39 @@
<?php
/**
* Created on 01/20/2009
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* AMF Moodle server.
* LICENSE
*
* @author Jerome Mouneyrac
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/**
* Moodle AMF server
*/
require_once(dirname(__FILE__) . '/../../config.php');
include "Zend/Loader.php";
Zend_Loader::registerAutoload();
require_once('lib.php');
if (empty($CFG->enablewebservices)) {
die;
}
/*
* FULL SERVER
*
*/
//retrieve the api name
$classpath = optional_param(classpath,'user',PARAM_ALPHA);
require_once(dirname(__FILE__) . '/../../'.$classpath.'/external.php');
/// run the server
$server = new Zend_Amf_Server();
$server->setClass($classpath."_external");
$response = $server->handle();
echo $response;
$server = new amf_server();
$server->run();
?>

145
webservice/lib.php Normal file
View file

@ -0,0 +1,145 @@
<?php
/**
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* LICENSE
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/**
* web service library
*/
final class webservice_lib {
/**
* Return list of all web service protocol into the webservice folder
* @global <type> $CFG
* @return <type>
*/
public static function get_list_protocols() {
global $CFG;
$protocols = array();
$directorypath = $CFG->dirroot . "/webservice";
if( $dh = opendir($directorypath)) {
while( false !== ($file = readdir($dh)))
{
if( $file == '.' || $file == '..' || $file == 'CVS') { // Skip '.' and '..'
continue;
}
$path = $directorypath . '/' . $file;
///browse the subfolder
if( is_dir($path) ) {
$protocols[] = $file;
}
///retrieve api.php file
else {
continue;
}
}
closedir($dh);
}
return $protocols;
}
/**
* Temporary Authentication method to be modified/removed
* @global <type> $DB
* @param <type> $token
* @return <type>
*/
public static function mock_check_token($token) {
//fake test
if ($token == 465465465468468464) {
///retrieve the user
global $DB;
$user = $DB->get_record('user', array('username'=>'wsuser', 'mnethostid'=>1));
if (empty($user)) {
return false;
}
return $user;
} else {
return false;
}
}
}
/**
* Web Service server base class
*/
abstract class webservice_server {
/**
* Web Service Protocol name (eg. SOAP, REST, XML-RPC,...)
* @var String
*/
private $protocolname;
/**
* if set to false the server cannot be run
* @var String
*/
private $enable;
public function __construct() {
}
abstract public function run();
public function get_protocolname() {
return $this->protocolname;
}
public function set_protocolname($protocolname) {
$this->protocolname = $protocolname;
}
public function get_enable() {
return $this->enable;
}
public function set_enable($enable) {
$this->enable = $enable;
}
}
/**
* Temporary authentication class to be removed/modified
*/
class ws_authentication {
/**
*
* @param array|struct $params
* @return integer
*/
function tmp_get_token($params) {
if ($params['username'] == 'wsuser' && $params['password'] == 'wspassword') {
return '465465465468468464';
} else {
throw new moodle_exception('wrongusernamepassword');
}
}
}
?>

89
webservice/rest/lib.php Normal file
View file

@ -0,0 +1,89 @@
<?php
/**
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* LICENSE
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
require_once('../lib.php');
/*
* Rest server class
*/
final class rest_server extends webservice_server {
public function __construct() {
$this->set_protocolname("Rest");
}
/**
* Run REST server
*/
public function run() {
require_once('locallib.php');
//retrieve path and function name from the URL
$rest_arguments = get_file_argument('server.php');
header ("Content-type: text/xml");
echo call_moodle_function($rest_arguments);
}
/**
* Run Zend REST server
* @global object $USER .
*/
public function zend_run() {
include "Zend/Loader.php";
Zend_Loader::registerAutoload();
// retrieve the token from the url
// if the token doesn't exist, set a class containing only get_token()
$token = optional_param('token',null,PARAM_ALPHANUM);
if (empty($token)) {
$server = new Zend_Rest_Server();
$server->setClass("ws_authentication");
$server->handle();
} else { // if token exist, do the authentication here
/// TODO: following function will need to be modified
$user = webservice_lib::mock_check_token($token);
if (empty($user)) {
throw new moodle_exception('wrongidentification');
} else {
global $USER;
$USER = $user;
}
//retrieve the api name
$classpath = optional_param(classpath,null,PARAM_ALPHA);
require_once(dirname(__FILE__) . '/../../'.$classpath.'/external.php');
/// run the server
$server = new Zend_Rest_Server();
$server->setClass($classpath."_external");
$server->handle();
}
}
}
?>

View file

@ -1,8 +1,31 @@
<?php
/**
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* LICENSE
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/**
* Rest library
*
* @author Jerome Mouneyrac, Ferran Recio, David Castro Garcia
*/
@ -77,10 +100,10 @@ function call_moodle_function ($rest_arguments) {
}
/**
* TODO: remove/rewrite this funcion
* TODO: remove/rewrite this function
* Mock function waiting for token system implementation
* @param <type> $token
* @return <type>
* @param int $token
* @return object|boolean
*/
function mock_check_token($token) {
//fake test
@ -102,8 +125,8 @@ function mock_check_token($token) {
/**
*
* @author Jerome Mouneyrac
* @param <type> $description
* @return <type>
* @param array $description
* @return array
*/
function retrieve_params ($description) {
$params = array();

View file

@ -1,7 +1,29 @@
<?php
/**
* Created on 10/14/2008
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* LICENSE
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/**
* REST Moodle server.
*
* NOTE: for this first implementation, REST requires implicit url encoded params.
@ -13,16 +35,14 @@
*/
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
require_once('locallib.php');
require_once('lib.php');
if (empty($CFG->enablewebservices)) {
die;
}
//retrieve path and function name from the URL
$rest_arguments = get_file_argument('server.php');
header ("Content-type: text/xml");
//TODO implement authentication (probably in the locallib.php)
echo call_moodle_function($rest_arguments);
$server = new rest_server();
$server->run();
?>

View file

@ -54,7 +54,6 @@ if ($search) {
$out = curl_exec($ch);
$res = basicxml_xml_to_object($out);
show_object($res->user,2,'auth');
show_xml ($out);

View file

@ -1,15 +1,30 @@
<?php
/**
* Main script - try a REST connection
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* @author Jerome Mouneyrac <jerome@moodle.com>
* @version 1.0
* @package webservices
* LICENSE
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/*
* Zend Rest sclient
* Moodle Zend Rest test client
*/
require_once('../../../config.php');
include "Zend/Loader.php";
@ -18,33 +33,22 @@ Zend_Loader::registerAutoload();
//1. authentication
$client = new Zend_Rest_Client($CFG->wwwroot."/webservice/rest/zend_rest_server.php");
$token = $client->tmp_get_token(array('username' => "wsuser", 'password' => "wspassword"))->get();
echo $token->response();
$token = $token->response();
printLastRequestResponse($client);
print "<pre>\n</pre>";
//2. test functions
$client = new Zend_Rest_Client($CFG->wwwroot."/webservice/rest/zend_rest_server.php/?classpath=user&token=".$token);
var_dump($client->tmp_get_users(array('search' => "admin"))->get());
printLastRequestResponse($client);
print "<pre>\n</pre>";
var_dump($client->tmp_create_user(array('username' => "mockuser66",'firstname' => "firstname6",'lastname' => "lastname6",'email' => "mockuser6@mockuser6.com",'password' => "password6"))->get());
printLastRequestResponse($client);
print "<pre>\n</pre>";
var_dump($client->tmp_update_user(array('username' => "mockuser66",'mnethostid' => 1,'newusername' => "mockuser6b",'firstname' => "firstname6b"))->get());
printLastRequestResponse($client);
print "<pre>\n</pre>";
var_dump($client->tmp_delete_user(array('username' => "mockuser6b",'mnethostid' => 1))->get());
printLastRequestResponse($client);
function printLastRequestResponse($client) {
print "<pre>\n";
// print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
// print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
print "</pre>";
}
print "<pre>\n</pre>";
var_dump($client->tmp_do_multiple_user_searches(array(array('search' => "admin"),array('search' => 'mock')))->get());
print "<pre>\n</pre>";
?>

View file

@ -1,80 +1,41 @@
<?php
/**
* Main script - REST server
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* @author Jerome Mouneyrac <jerome@moodle.com>
* @version 1.0
* @package webservices
* LICENSE
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/*
* Zend Rest server
/**
* Zend REST Moodle server.
*/
require_once(dirname(__FILE__) . '/../../config.php');
include "Zend/Loader.php";
Zend_Loader::registerAutoload();
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
require_once('lib.php');
if (empty($CFG->enablewebservices)) {
die;
}
// retrieve the token from the url
// if the token doesn't exist, set a class containing only get_token()
$token = optional_param('token',null,PARAM_ALPHANUM);
if (empty($token)) {
$server = new Zend_Rest_Server();
$server->setClass("soap_authentication");
$server->handle();
} else { // if token exist, do the authentication here
/// TODO: following function will need to be modified
$user = mock_check_token($token);
if (empty($user)) {
throw new moodle_exception('wrongidentification');
} else {
/// TODO: probably change this
global $USER;
$USER = $user;
}
$server = new rest_server();
$server->zend_run();
//retrieve the api name
$classpath = optional_param(classpath,null,PARAM_ALPHA);
require_once(dirname(__FILE__) . '/../../'.$classpath.'/external.php');
/// run the server
$server = new Zend_Rest_Server(); //TODO: need to call the wsdl generation on the fly
$server->setClass($classpath."_external"); //TODO: pass $user as parameter
$server->handle();
}
function mock_check_token($token) {
//fake test
if ($token == 465465465468468464) {
///retrieve the user
global $DB;
$user = $DB->get_record('user', array('username'=>'wsuser', 'mnethostid'=>1));
if (empty($user)) {
return false;
}
return $user;
} else {
return false;
}
}
class soap_authentication {
/**
*
* @param array $params
* @return integer
*/
function tmp_get_token($params) {
if ($params['username'] == 'wsuser' && $params['password'] == 'wspassword') {
return '465465465468468464';
} else {
throw new moodle_exception('wrongusernamepassword');
}
}
}
?>

View file

@ -1,7 +1,32 @@
<?php
/**
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* LICENSE
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/*
* Generate the Moodle WSDL file
* This file is not use neither finish but will give you a base to start
* This file is not finish but will give you a base to start
*/
require_once('../../config.php');
@ -11,7 +36,7 @@ $wsdl = $wsdl_generator->generate_wsdl($token);
echo $wsdl;
/**
* WORK IN PROGRESS - Generator not working yet
* WORK IN PROGRESS
*/
class wsdl_generator {
@ -323,6 +348,12 @@ EOF;
case PARAM_NUMBER:
return "integer";
break;
case PARAM_INT:
return "integer";
break;
case PARAM_BOOL:
return "boolean";
break;
case PARAM_ALPHANUM:
return "string";
break;

148
webservice/soap/lib.php Normal file
View file

@ -0,0 +1,148 @@
<?php
/**
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* LICENSE
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/*
* SOAP server class
*/
require_once('../lib.php');
final class soap_server extends webservice_server {
public function __construct() {
$this->set_protocolname("Soap");
}
/**
* Run SOAP server
* @global <type> $CFG
* @global <type> $USER
*/
public function run() {
global $CFG;
// retrieve the token from the url
// if the token doesn't exist, set a class containing only get_token()
$token = optional_param('token',null,PARAM_ALPHANUM);
if (empty($token)) {
$server = new SoapServer($CFG->wwwroot."/webservice/soap/generatewsdl.php");
$server->setClass("ws_authentication");
$server->handle();
} else { // if token exist, do the authentication here
/// TODO: following function will need to be modified
$user = webservice_lib::mock_check_token($token);
if (empty($user)) {
throw new moodle_exception('wrongidentification');
} else {
/// TODO: probably change this
global $USER;
$USER = $user;
}
//retrieve the api name
$classpath = optional_param(classpath,null,PARAM_ALPHA);
require_once(dirname(__FILE__) . '/../../'.$classpath.'/external.php');
/// run the server
$server = new SoapServer($CFG->wwwroot."/webservice/soap/generatewsdl.php?token=".$token);
$server->setClass($classpath."_external"); //TODO: pass $user as parameter
$server->handle();
}
}
/**
* Run Zend SOAP server
* @global <type> $CFG
* @global <type> $USER
*/
public function zend_run() {
global $CFG;
include "Zend/Loader.php";
Zend_Loader::registerAutoload();
// retrieve the token from the url
// if the token doesn't exist, set a class containing only get_token()
$token = optional_param('token',null,PARAM_ALPHANUM);
///this is a hack, because there is a bug in Zend framework (http://framework.zend.com/issues/browse/ZF-5736)
if (empty($token)) {
$relativepath = get_file_argument();
$args = explode('/', trim($relativepath, '/'));
if (count($args) == 2) {
$token = (integer)$args[0];
$classpath = $args[1];
}
}
if (empty($token)) {
if(isset($_GET['wsdl'])) {
$autodiscover = new Zend_Soap_AutoDiscover();
$autodiscover->setClass('ws_authentication');
$autodiscover->handle();
} else {
$soap = new Zend_Soap_Server($CFG->wwwroot."/webservice/soap/zend_soap_server.php?wsdl"); // this current file here
$soap->setClass('ws_authentication');
$soap->handle();
}
} else { // if token exist, do the authentication here
/// TODO: following function will need to be modified
$user = webservice_lib::mock_check_token($token);
if (empty($user)) {
throw new moodle_exception('wrongidentification');
} else {
/// TODO: probably change this
global $USER;
$USER = $user;
}
//retrieve the api name
if (empty($classpath)) {
$classpath = optional_param('classpath',null,PARAM_ALPHANUM);
}
require_once(dirname(__FILE__) . '/../../'.$classpath.'/external.php');
/// run the server
if(isset($_GET['wsdl'])) {
$autodiscover = new Zend_Soap_AutoDiscover();
//this is a hack, because there is a bug in Zend framework (http://framework.zend.com/issues/browse/ZF-5736)
$autodiscover->setUri($CFG->wwwroot."/webservice/soap/zend_soap_server.php/".$token."/".$classpath);
$autodiscover->setClass($classpath."_external");
$autodiscover->handle();
} else {
$soap = new Zend_Soap_Server($CFG->wwwroot."/webservice/soap/zend_soap_server.php?token=".$token."&classpath=".$classpath."&wsdl"); // this current file here
$soap->setClass($classpath."_external");
$soap->handle();
}
}
}
}
?>

View file

@ -1,74 +1,40 @@
<?php
/**
* Main script - SOAP server
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* @author Jerome Mouneyrac <jerome@moodle.com>
* @version 1.0
* @package webservices
* LICENSE
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/*
* SOAP server
*/
require_once(dirname(__FILE__) . '/../../config.php');
require_once('lib.php');
if (empty($CFG->enablewebservices)) {
die;
}
// retrieve the token from the url
// if the token doesn't exist, set a class containing only get_token()
$token = optional_param('token',null,PARAM_ALPHANUM);
if (empty($token)) {
$server = new SoapServer($CFG->wwwroot."/webservice/soap/generatewsdl.php");
$server->setClass("soap_authentication");
$server->handle();
} else { // if token exist, do the authentication here
/// TODO: following function will need to be modified
$user = mock_check_token($token);
if (empty($user)) {
throw new moodle_exception('wrongidentification');
} else {
/// TODO: probably change this
global $USER;
$USER = $user;
}
$server = new soap_server();
$server->run();
//retrieve the api name
$classpath = optional_param(classpath,null,PARAM_ALPHA);
require_once(dirname(__FILE__) . '/../../'.$classpath.'/external.php');
/// run the server
$server = new SoapServer($CFG->wwwroot."/webservice/soap/generatewsdl.php?token=".$token); //TODO: need to call the wsdl generation on the fly
$server->setClass($classpath."_external"); //TODO: pass $user as parameter
$server->handle();
}
function mock_check_token($token) {
//fake test
if ($token == 465465465468468464) {
///retrieve the user
global $DB;
$user = $DB->get_record('user', array('username'=>'wsuser', 'mnethostid'=>1));
if (empty($user)) {
return false;
}
return $user;
} else {
return false;
}
}
class soap_authentication {
function tmp_get_token($params) {
if ($params['username'] == 'wsuser' && $params['password'] == 'wspassword') {
return '465465465468468464';
} else {
throw new moodle_exception('wrongusernamepassword');
}
}
}
?>

View file

@ -1,15 +1,30 @@
<?php
/**
* Main script - try a SOAP connection
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* @author Jerome Mouneyrac <jerome@moodle.com>
* @version 1.0
* @package webservices
* LICENSE
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/*
* SOAP client
* SOAP test client
*/
require_once(dirname(__FILE__) . '/../../../config.php');

View file

@ -0,0 +1,66 @@
<?php
/**
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* LICENSE
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/*
* Zend Rest sclient
*/
require_once('../../../config.php');
include "Zend/Loader.php";
Zend_Loader::registerAutoload();
//1. authentication
$client = new Zend_Soap_Client($CFG->wwwroot."/webservice/soap/zend_soap_server.php?wsdl");
try {
$token = $client->tmp_get_token(array('username' => "wsuser", 'password' => "wspassword"));
printLastRequestResponse($client);
} catch (moodle_exception $exception) {
echo $exception;
}
echo $CFG->wwwroot."/webservice/soap/zend_soap_server.php?token=".$token."&classpath=user&wsdl";
//2. test functions
$client = new Zend_Soap_Client($CFG->wwwroot."/webservice/soap/zend_soap_server.php?token=".$token."&classpath=user&wsdl");
var_dump($client->tmp_get_users(array('search' => "admin")));
printLastRequestResponse($client);
var_dump($client->tmp_create_user(array('username' => "mockuser66",'firstname' => "firstname6",'lastname' => "lastname6",'email' => "mockuser6@mockuser6.com",'password' => "password6")));
printLastRequestResponse($client);
var_dump($client->tmp_update_user(array('username' => "mockuser66",'mnethostid' => 1,'newusername' => "mockuser6b",'firstname' => "firstname6b")));
printLastRequestResponse($client);
var_dump($client->tmp_delete_user(array('username' => "mockuser6b",'mnethostid' => 1)));
printLastRequestResponse($client);
var_dump($client->tmp_do_multiple_user_searches(array(array('search' => "jerome"),array('search' => "mock"))));
printLastRequestResponse($client);
function printLastRequestResponse($client) {
print "<pre>\n";
print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
print "</pre>";
}
?>

View file

@ -0,0 +1,41 @@
<?php
/**
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* LICENSE
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/*
* Zend SOAP server
*/
require_once(dirname(__FILE__) . '/../../config.php');
require_once('lib.php');
if (empty($CFG->enablewebservices)) {
die;
}
$server = new soap_server();
$server->zend_run();
?>

77
webservice/xmlrpc/lib.php Normal file
View file

@ -0,0 +1,77 @@
<?php
/**
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* LICENSE
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/*
* XML-RPC server class
*/
require_once('../lib.php');
final class xmlrpc_server extends webservice_server {
public function __construct() {
$this->set_protocolname("XML-RPC");
}
public function run() {
include "Zend/Loader.php";
Zend_Loader::registerAutoload();
Zend_XmlRpc_Server_Fault::attachFaultException('moodle_exception');
// retrieve the token from the url
// if the token doesn't exist, set a class containing only get_token()
$token = optional_param('token',null,PARAM_ALPHANUM);
if (empty($token)) {
$server = new Zend_XmlRpc_Server();
$server->setClass("ws_authentication", "authentication");
echo $server->handle();
} else { // if token exist, do the authentication here
/// TODO: following function will need to be modified
$user = webservice_lib::mock_check_token($token);
if (empty($user)) {
throw new moodle_exception('wrongidentification');
} else {
/// TODO: probably change this
global $USER;
$USER = $user;
}
//retrieve the api name
$classpath = optional_param(classpath,null,PARAM_ALPHA);
require_once(dirname(__FILE__) . '/../../'.$classpath.'/external.php');
/// run the server
$server = new Zend_XmlRpc_Server();
$server->setClass($classpath."_external", $classpath);
echo $server->handle();
}
}
}
?>

View file

@ -0,0 +1,53 @@
<?php
/**
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* LICENSE
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/*
* Zend XMLRPC sclient
*/
require_once('../../../config.php');
include "Zend/Loader.php";
Zend_Loader::registerAutoload();
//1. authentication
$client = new Zend_XmlRpc_Client($CFG->wwwroot."/webservice/xmlrpc/zend_xmlrpc_server.php");
$token = $client->call('authentication.tmp_get_token', array(array('username' => "wsuser", 'password' => "wspassword")));
var_dump($token);
//2. test functions
$client = new Zend_XmlRpc_Client($CFG->wwwroot."/webservice/xmlrpc/zend_xmlrpc_server.php?classpath=user&token=".$token);
var_dump($users = $client->call('user.tmp_get_users', array(array('search' => "admin"))));
print "<br/><br/>\n";
var_dump($users = $client->call('user.tmp_create_user', array(array('username' => "mockuser66",'firstname' => "firstname6",'lastname' => "lastname6",'email' => "mockuser6@mockuser6.com",'password' => "password6"))));
print "<br/><br/>\n";
var_dump($users = $client->call('user.tmp_update_user', array(array('username' => "mockuser66",'mnethostid' => 1,'newusername' => "mockuser6b",'firstname' => "firstname6b"))));
print "<br/><br/>\n";
var_dump($users = $client->call('user.tmp_delete_user', array(array('username' => "mockuser6b",'mnethostid' => 1))));
print "<br/><br/>\n";
var_dump($users = $client->call('user.tmp_do_multiple_user_searches', array(array(array('search' => "jerome"),array('search' => "admin")))));
print "<br/><br/>\n";
?>

View file

@ -0,0 +1,46 @@
<?php
/**
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* LICENSE
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/**
* Main script - XML-RPC server
*
* @author Jerome Mouneyrac <jerome@moodle.com>
* @version 1.0
* @package webservices
*/
/*
* Zend XML-RPC server
*/
require_once(dirname(__FILE__) . '/../../config.php');
require_once('lib.php');
if (empty($CFG->enablewebservices)) {
die;
}
$server = new xmlrpc_server();
$server->run();
?>