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

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');
}
}
}
?>