Changed recent commit to new DB API 2.0
[moodle.git] / webservice / lib.php
CommitLineData
06e7fadc 1<?php
2/**
3 * Moodle - Modular Object-Oriented Dynamic Learning Environment
4 * http://moodle.com
5 *
6 * LICENSE
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details:
17 *
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @category Moodle
21 * @package webservice
551f4420 22 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
06e7fadc 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
24 */
25
24350e06 26require_once(dirname(dirname(__FILE__)) . '/lib/formslib.php');
06e7fadc 27
893d7f0f 28/**
29 * Returns detailed information about external function
30 * @param string $functionname name of external function
31 * @return aray
32 */
33function ws_get_function_info($functionname) {
34 global $CFG, $DB;
35
36 $function = $DB->get_record('external_functions', array('name'=>$functionname), '*', MUST_EXIST);
37
38 $defpath = get_component_directory($function->component);
39 if (!file_exists("$defpath/db/services.php")) {
40 //TODO: maybe better throw invalid parameter exception
41 return null;
42 }
43
44 $functions = array();
45 include("$defpath/db/services.php");
46
47 if (empty($functions[$functionname])) {
48 return null;
49 }
50
51 $desc = $functions[$functionname];
52 if (empty($desc['classpath'])) {
53 $desc['classpath'] = "$defpath/externallib.php";
54 } else {
55 $desc['classpath'] = "$CFG->dirroot/".$desc['classpath'];
56 }
57 $desc['component'] = $function->component;
58
59 return $desc;
60}
61
06e7fadc 62/**
63 * web service library
64 */
65final class webservice_lib {
66
886d7556 67/**
68 * Return list of all web service protocol into the webservice folder
69 * @global <type> $CFG
70 * @return <type>
71 */
06e7fadc 72 public static function get_list_protocols() {
73 global $CFG;
74 $protocols = array();
75 $directorypath = $CFG->dirroot . "/webservice";
76 if( $dh = opendir($directorypath)) {
886d7556 77 while( false !== ($file = readdir($dh))) {
06e7fadc 78 if( $file == '.' || $file == '..' || $file == 'CVS') { // Skip '.' and '..'
79 continue;
80 }
81 $path = $directorypath . '/' . $file;
40f024c9 82 ///browse the subfolder
06e7fadc 83 if( is_dir($path) ) {
fbe52a39 84 if ($file != 'db') { //we don't want to browse the 'db' subfolder of webservice folder
886d7556 85 require_once($path."/lib.php");
86 $classname = $file."_server";
87 $protocols[] = new $classname;
fbe52a39 88 }
06e7fadc 89 }
40f024c9 90 ///retrieve api.php file
886d7556 91 else {
06e7fadc 92 continue;
93 }
94 }
95 closedir($dh);
96 }
97 return $protocols;
98 }
99
100 /**
101 * Temporary Authentication method to be modified/removed
102 * @global <type> $DB
103 * @param <type> $token
104 * @return <type>
105 */
106 public static function mock_check_token($token) {
886d7556 107 //fake test
fa0797ec 108 if ($token == 456) {
886d7556 109 ///retrieve the user
06e7fadc 110 global $DB;
111 $user = $DB->get_record('user', array('username'=>'wsuser', 'mnethostid'=>1));
112
113 if (empty($user)) {
114 return false;
115 }
116
117 return $user;
118 } else {
119 return false;
120 }
121 }
122
24350e06 123 /**
124 * Retrieve all external.php from Moodle (except the one of the exception list)
125 * @param <type> $
126 * @param <type> $directorypath
127 * @return boolean true if n
128 */
886d7556 129 public static function setListApiFiles( &$files, $directorypath ) {
24350e06 130 global $CFG;
131
886d7556 132 if(is_dir($directorypath)) { //check that we are browsing a folder not a file
24350e06 133
886d7556 134 if( $dh = opendir($directorypath)) {
135 while( false !== ($file = readdir($dh))) {
24350e06 136
137 if( $file == '.' || $file == '..') { // Skip '.' and '..'
138 continue;
139 }
140 $path = $directorypath . '/' . $file;
40f024c9 141 ///browse the subfolder
24350e06 142 if( is_dir($path) ) {
40f024c9 143 webservice_lib::setListApiFiles($files, $path);
24350e06 144 }
40f024c9 145 ///retrieve api.php file
24350e06 146 else if ($file == "external.php") {
886d7556 147 $files[] = $path;
148 }
24350e06 149 }
150 closedir($dh);
151
152 }
153 }
154
155 }
156
fa0797ec 157 /**
158 * Check if the Moodle site has the web service protocol enable
159 * @global object $CFG
160 * @param string $protocol
161 */
886d7556 162 function display_webservices_availability($protocol) {
fa0797ec 163 global $CFG;
164
165 $available = true;
166
167 echo get_string('webservicesenable','webservice').": ";
168 if (empty($CFG->enablewebservices)) {
169 echo "<strong style=\"color:red\">".get_string('fail','webservice')."</strong>";
170 $available = false;
171 } else {
172 echo "<strong style=\"color:green\">".get_string('ok','webservice')."</strong>";
173 }
174 echo "<br/>";
175
176 foreach(webservice_lib::get_list_protocols() as $wsprotocol) {
f7631e73 177 if (strtolower($wsprotocol->get_protocolid()) == strtolower($protocol)) {
178 echo get_string('protocolenable','webservice',array($wsprotocol->get_protocolid())).": ";
179 if ( get_config($wsprotocol-> get_protocolid(), "enable")) {
fa0797ec 180 echo "<strong style=\"color:green\">".get_string('ok','webservice')."</strong>";
181 } else {
182 echo "<strong style=\"color:red\">".get_string('fail','webservice')."</strong>";
183 $available = false;
184 }
185 echo "<br/>";
186 continue;
187 }
188 }
189
190 //check debugging
191 if ($CFG->debugdisplay) {
192 echo "<strong style=\"color:red\">".get_string('debugdisplayon','webservice')."</strong>";
193 $available = false;
194 }
195
196 return $available;
197 }
198
06e7fadc 199}
200
201/**
202 * Web Service server base class
203 */
204abstract class webservice_server {
205
886d7556 206/**
207 * Web Service Protocol name (eg. SOAP, REST, XML-RPC,...)
208 * @var String
209 */
06e7fadc 210 private $protocolname;
211
f7631e73 212 /**
213 * Web Service Protocol id (eg. soap, rest, xmlrpc...)
214 * @var String
215 */
216 private $protocolid;
217
06e7fadc 218 public function __construct() {
219 }
220
221 abstract public function run();
222
223 public function get_protocolname() {
224 return $this->protocolname;
225 }
226
f7631e73 227 public function get_protocolid() {
228 return $this->protocolid;
229 }
230
06e7fadc 231 public function set_protocolname($protocolname) {
232 $this->protocolname = $protocolname;
233 }
234
f7631e73 235 public function set_protocolid($protocolid) {
236 $this->protocolid = $protocolid;
237 }
238
06e7fadc 239 public function get_enable() {
f7631e73 240 return get_config($this->get_protocolid(), "enable");
06e7fadc 241 }
242
243 public function set_enable($enable) {
f7631e73 244 set_config("enable", $enable, $this->get_protocolid());
245 }
246
247 /**
248 * Names of the server settings
249 * @return array
250 */
251 public static function get_setting_names() {
252 return array();
253 }
254
255 public function settings_form(&$mform) {
06e7fadc 256 }
257
258}
259
260/**
261 * Temporary authentication class to be removed/modified
262 */
263class ws_authentication {
886d7556 264/**
265 *
266 * @param object|struct $params
267 * @return integer
268 */
ebcc6bd3 269 function get_token($params) {
270 $params->username = clean_param($params->username, PARAM_ALPHANUM);
271 $params->password = clean_param($params->password, PARAM_ALPHANUM);
272 if ($params->username == 'wsuser' && $params->password == 'wspassword') {
fa0797ec 273 return '456';
06e7fadc 274 } else {
275 throw new moodle_exception('wrongusernamepassword');
886d7556 276 }
06e7fadc 277 }
278}
279
24350e06 280/**
281 * Form for web service user settings (administration)
282 */
283final class wsuser_form extends moodleform {
284 protected $username;
285
286 /**
287 * Definition of the moodleform
288 */
289 public function definition() {
290 global $DB;
291 $this->username = $this->_customdata['username'];
292 $mform =& $this->_form;
40f024c9 293
24350e06 294 $mform->addElement('hidden', 'username', $this->username);
d18e0fe6 295 $mform->setType('username', PARAM_RAW);
24350e06 296 $param = new stdClass();
297 $param->username = $this->username;
298 $wsuser = $DB->get_record("user", array("username" => $this->username));
299
300 $mform->addElement('text', 'ipwhitelist', get_string('ipwhitelist', 'admin'), array('value'=>get_user_preferences("ipwhitelist", "", $wsuser->id),'size' => '40'));
301 $mform->addElement('static', null, '', get_string('ipwhitelistdesc','admin', $param));
302
303 $this->add_action_buttons(true, get_string('savechanges','admin'));
304 }
305}
306
f7631e73 307/**
308 * Form for web service server settings (administration)
309 */
310final class wssettings_form extends moodleform {
311 protected $settings;
312
313 /**
314 * Definition of the moodleform
315 */
316 public function definition() {
317 global $DB,$CFG;
318 $settings = $this->_customdata['settings'];
319 $mform =& $this->_form;
320
321 $mform->addElement('hidden', 'settings', $settings);
d18e0fe6 322 $mform->setType('settings', PARAM_RAW);
f7631e73 323 $param = new stdClass();
324
325 require_once($CFG->dirroot . '/webservice/'. $settings . '/lib.php');
326 $servername = $settings.'_server';
327 $server = new $servername();
328 $server->settings_form($mform);
329
330 // set the data if we have some.
331 $data = array();
332 $option_names = $server->get_setting_names();
333 foreach ($option_names as $config) {
334 $data[$config] = get_config($settings, $config);
335 }
336 $this->set_data($data);
337
338
339 $this->add_action_buttons(true, get_string('savechanges','admin'));
340 }
341}
342
9baf6825 343/**
344 * Form for web service server settings (administration)
345 */
346final class wsservicesettings_form extends moodleform {
347 protected $settings;
348
349 /**
350 * Definition of the moodleform
351 */
352 public function definition() {
353 global $DB,$CFG;
354 $serviceid = $this->_customdata['serviceid'];
355 $mform =& $this->_form;
356
357 $mform->addElement('hidden', 'serviceid', $serviceid);
d18e0fe6 358 $mform->setType('serviceid', PARAM_INT);
9baf6825 359 $param = new stdClass();
360
361 // require_once($CFG->dirroot . '/webservice/'. $settings . '/lib.php');
362 // $servername = $settings.'_server';
363 // $server = new $servername();
364 // $server->settings_form($mform);
365
366 // set the data if we have some.
367 // $data = array();
368 // $option_names = $server->get_setting_names();
369 // foreach ($option_names as $config) {
370 // $data[$config] = get_config($settings, $config);
371 // }
372 // $this->set_data($data);
373 $service = $DB->get_record('external_services',array('id' => $serviceid));
374
375 $mform->addElement('text', 'servicename', get_string('servicename', 'webservice'));
376 $mform->setDefault('servicename',get_string($service->name, 'webservice'));
377 if (!empty($serviceid)) {
378 $mform->disabledIf('servicename', 'serviceid', 'eq', $serviceid);
379 }
380
381 if (empty($serviceid)) {
382 //display list of functions to select
383 }
384
385 //display list of functions associated to the service
386
387
388
389 $this->add_action_buttons(true, get_string('savechanges','admin'));
390 }
391}
392
393
06e7fadc 394?>