navigationMDL-20216 Moved type argument to third argument
[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
22 * @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
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
28/**
29 * web service library
30 */
31final class webservice_lib {
32
33 /**
34 * Return list of all web service protocol into the webservice folder
35 * @global <type> $CFG
36 * @return <type>
37 */
38 public static function get_list_protocols() {
39 global $CFG;
40 $protocols = array();
41 $directorypath = $CFG->dirroot . "/webservice";
42 if( $dh = opendir($directorypath)) {
43 while( false !== ($file = readdir($dh)))
44 {
45 if( $file == '.' || $file == '..' || $file == 'CVS') { // Skip '.' and '..'
46 continue;
47 }
48 $path = $directorypath . '/' . $file;
40f024c9 49 ///browse the subfolder
06e7fadc 50 if( is_dir($path) ) {
fbe52a39 51 if ($file != 'db') { //we don't want to browse the 'db' subfolder of webservice folder
40f024c9 52 require_once($path."/lib.php");
53 $classname = $file."_server";
54 $protocols[] = new $classname;
fbe52a39 55 }
06e7fadc 56 }
40f024c9 57 ///retrieve api.php file
06e7fadc 58 else {
59 continue;
60 }
61 }
62 closedir($dh);
63 }
64 return $protocols;
65 }
66
67 /**
68 * Temporary Authentication method to be modified/removed
69 * @global <type> $DB
70 * @param <type> $token
71 * @return <type>
72 */
73 public static function mock_check_token($token) {
74 //fake test
fa0797ec 75 if ($token == 456) {
06e7fadc 76 ///retrieve the user
77 global $DB;
78 $user = $DB->get_record('user', array('username'=>'wsuser', 'mnethostid'=>1));
79
80 if (empty($user)) {
81 return false;
82 }
83
84 return $user;
85 } else {
86 return false;
87 }
88 }
89
24350e06 90 /**
91 * Retrieve all external.php from Moodle (except the one of the exception list)
92 * @param <type> $
93 * @param <type> $directorypath
94 * @return boolean true if n
95 */
96 public static function setListApiFiles( &$files, $directorypath )
97 {
98 global $CFG;
99
100 if(is_dir($directorypath)){ //check that we are browsing a folder not a file
101
102 if( $dh = opendir($directorypath))
103 {
104 while( false !== ($file = readdir($dh)))
105 {
106
107 if( $file == '.' || $file == '..') { // Skip '.' and '..'
108 continue;
109 }
110 $path = $directorypath . '/' . $file;
40f024c9 111 ///browse the subfolder
24350e06 112 if( is_dir($path) ) {
40f024c9 113 webservice_lib::setListApiFiles($files, $path);
24350e06 114 }
40f024c9 115 ///retrieve api.php file
24350e06 116 else if ($file == "external.php") {
117 $files[] = $path;
118 }
119 }
120 closedir($dh);
121
122 }
123 }
124
125 }
126
fa0797ec 127 /**
128 * Check if the Moodle site has the web service protocol enable
129 * @global object $CFG
130 * @param string $protocol
131 */
132 function display_webservices_availability($protocol){
133 global $CFG;
134
135 $available = true;
136
137 echo get_string('webservicesenable','webservice').": ";
138 if (empty($CFG->enablewebservices)) {
139 echo "<strong style=\"color:red\">".get_string('fail','webservice')."</strong>";
140 $available = false;
141 } else {
142 echo "<strong style=\"color:green\">".get_string('ok','webservice')."</strong>";
143 }
144 echo "<br/>";
145
146 foreach(webservice_lib::get_list_protocols() as $wsprotocol) {
f7631e73 147 if (strtolower($wsprotocol->get_protocolid()) == strtolower($protocol)) {
148 echo get_string('protocolenable','webservice',array($wsprotocol->get_protocolid())).": ";
149 if ( get_config($wsprotocol-> get_protocolid(), "enable")) {
fa0797ec 150 echo "<strong style=\"color:green\">".get_string('ok','webservice')."</strong>";
151 } else {
152 echo "<strong style=\"color:red\">".get_string('fail','webservice')."</strong>";
153 $available = false;
154 }
155 echo "<br/>";
156 continue;
157 }
158 }
159
160 //check debugging
161 if ($CFG->debugdisplay) {
162 echo "<strong style=\"color:red\">".get_string('debugdisplayon','webservice')."</strong>";
163 $available = false;
164 }
165
166 return $available;
167 }
168
06e7fadc 169}
170
171/**
172 * Web Service server base class
173 */
174abstract class webservice_server {
175
176 /**
177 * Web Service Protocol name (eg. SOAP, REST, XML-RPC,...)
178 * @var String
179 */
180 private $protocolname;
181
f7631e73 182 /**
183 * Web Service Protocol id (eg. soap, rest, xmlrpc...)
184 * @var String
185 */
186 private $protocolid;
187
06e7fadc 188 public function __construct() {
189 }
190
191 abstract public function run();
192
193 public function get_protocolname() {
194 return $this->protocolname;
195 }
196
f7631e73 197 public function get_protocolid() {
198 return $this->protocolid;
199 }
200
06e7fadc 201 public function set_protocolname($protocolname) {
202 $this->protocolname = $protocolname;
203 }
204
f7631e73 205 public function set_protocolid($protocolid) {
206 $this->protocolid = $protocolid;
207 }
208
06e7fadc 209 public function get_enable() {
f7631e73 210 return get_config($this->get_protocolid(), "enable");
06e7fadc 211 }
212
213 public function set_enable($enable) {
f7631e73 214 set_config("enable", $enable, $this->get_protocolid());
215 }
216
217 /**
218 * Names of the server settings
219 * @return array
220 */
221 public static function get_setting_names() {
222 return array();
223 }
224
225 public function settings_form(&$mform) {
06e7fadc 226 }
227
228}
229
230/**
231 * Temporary authentication class to be removed/modified
232 */
233class ws_authentication {
234 /**
235 *
ebcc6bd3 236 * @param object|struct $params
06e7fadc 237 * @return integer
238 */
ebcc6bd3 239 function get_token($params) {
240 $params->username = clean_param($params->username, PARAM_ALPHANUM);
241 $params->password = clean_param($params->password, PARAM_ALPHANUM);
242 if ($params->username == 'wsuser' && $params->password == 'wspassword') {
fa0797ec 243 return '456';
06e7fadc 244 } else {
245 throw new moodle_exception('wrongusernamepassword');
fbe52a39 246 }
06e7fadc 247 }
248}
249
24350e06 250/**
251 * Form for web service user settings (administration)
252 */
253final class wsuser_form extends moodleform {
254 protected $username;
255
256 /**
257 * Definition of the moodleform
258 */
259 public function definition() {
260 global $DB;
261 $this->username = $this->_customdata['username'];
262 $mform =& $this->_form;
40f024c9 263
24350e06 264 $mform->addElement('hidden', 'username', $this->username);
265 $param = new stdClass();
266 $param->username = $this->username;
267 $wsuser = $DB->get_record("user", array("username" => $this->username));
268
269 $mform->addElement('text', 'ipwhitelist', get_string('ipwhitelist', 'admin'), array('value'=>get_user_preferences("ipwhitelist", "", $wsuser->id),'size' => '40'));
270 $mform->addElement('static', null, '', get_string('ipwhitelistdesc','admin', $param));
271
272 $this->add_action_buttons(true, get_string('savechanges','admin'));
273 }
274}
275
f7631e73 276/**
277 * Form for web service server settings (administration)
278 */
279final class wssettings_form extends moodleform {
280 protected $settings;
281
282 /**
283 * Definition of the moodleform
284 */
285 public function definition() {
286 global $DB,$CFG;
287 $settings = $this->_customdata['settings'];
288 $mform =& $this->_form;
289
290 $mform->addElement('hidden', 'settings', $settings);
291 $param = new stdClass();
292
293 require_once($CFG->dirroot . '/webservice/'. $settings . '/lib.php');
294 $servername = $settings.'_server';
295 $server = new $servername();
296 $server->settings_form($mform);
297
298 // set the data if we have some.
299 $data = array();
300 $option_names = $server->get_setting_names();
301 foreach ($option_names as $config) {
302 $data[$config] = get_config($settings, $config);
303 }
304 $this->set_data($data);
305
306
307 $this->add_action_buttons(true, get_string('savechanges','admin'));
308 }
309}
310
06e7fadc 311?>