weblib: Improve PHPdoc comment and parameter names.
[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;
49 ///browse the subfolder
50 if( is_dir($path) ) {
24350e06 51 require_once($path."/lib.php");
52 $classname = $file."_server";
53 $protocols[] = new $classname;
06e7fadc 54 }
55 ///retrieve api.php file
56 else {
57 continue;
58 }
59 }
60 closedir($dh);
61 }
62 return $protocols;
63 }
64
65 /**
66 * Temporary Authentication method to be modified/removed
67 * @global <type> $DB
68 * @param <type> $token
69 * @return <type>
70 */
71 public static function mock_check_token($token) {
72 //fake test
73 if ($token == 465465465468468464) {
74 ///retrieve the user
75 global $DB;
76 $user = $DB->get_record('user', array('username'=>'wsuser', 'mnethostid'=>1));
77
78 if (empty($user)) {
79 return false;
80 }
81
82 return $user;
83 } else {
84 return false;
85 }
86 }
87
24350e06 88 /**
89 * Retrieve all external.php from Moodle (except the one of the exception list)
90 * @param <type> $
91 * @param <type> $directorypath
92 * @return boolean true if n
93 */
94 public static function setListApiFiles( &$files, $directorypath )
95 {
96 global $CFG;
97
98 if(is_dir($directorypath)){ //check that we are browsing a folder not a file
99
100 if( $dh = opendir($directorypath))
101 {
102 while( false !== ($file = readdir($dh)))
103 {
104
105 if( $file == '.' || $file == '..') { // Skip '.' and '..'
106 continue;
107 }
108 $path = $directorypath . '/' . $file;
109 ///browse the subfolder
110 if( is_dir($path) ) {
111 webservice_lib::setListApiFiles($files, $path);
112 }
113 ///retrieve api.php file
114 else if ($file == "external.php") {
115 $files[] = $path;
116 }
117 }
118 closedir($dh);
119
120 }
121 }
122
123 }
124
06e7fadc 125}
126
127/**
128 * Web Service server base class
129 */
130abstract class webservice_server {
131
132 /**
133 * Web Service Protocol name (eg. SOAP, REST, XML-RPC,...)
134 * @var String
135 */
136 private $protocolname;
137
06e7fadc 138 public function __construct() {
139 }
140
141 abstract public function run();
142
143 public function get_protocolname() {
144 return $this->protocolname;
145 }
146
147 public function set_protocolname($protocolname) {
148 $this->protocolname = $protocolname;
149 }
150
151 public function get_enable() {
24350e06 152 return get_config($this->get_protocolname(), "enable");
06e7fadc 153 }
154
155 public function set_enable($enable) {
24350e06 156 set_config("enable", $enable, $this->get_protocolname());
06e7fadc 157 }
158
159}
160
161/**
162 * Temporary authentication class to be removed/modified
163 */
164class ws_authentication {
165 /**
166 *
167 * @param array|struct $params
168 * @return integer
169 */
170 function tmp_get_token($params) {
171 if ($params['username'] == 'wsuser' && $params['password'] == 'wspassword') {
172 return '465465465468468464';
173 } else {
174 throw new moodle_exception('wrongusernamepassword');
175 }
176 }
177}
178
24350e06 179/**
180 * Form for web service user settings (administration)
181 */
182final class wsuser_form extends moodleform {
183 protected $username;
184
185 /**
186 * Definition of the moodleform
187 */
188 public function definition() {
189 global $DB;
190 $this->username = $this->_customdata['username'];
191 $mform =& $this->_form;
192
193 $strrequired = get_string('required');
194
195 $mform->addElement('hidden', 'username', $this->username);
196 $param = new stdClass();
197 $param->username = $this->username;
198 $wsuser = $DB->get_record("user", array("username" => $this->username));
199
200 $mform->addElement('text', 'ipwhitelist', get_string('ipwhitelist', 'admin'), array('value'=>get_user_preferences("ipwhitelist", "", $wsuser->id),'size' => '40'));
201 $mform->addElement('static', null, '', get_string('ipwhitelistdesc','admin', $param));
202
203 $this->add_action_buttons(true, get_string('savechanges','admin'));
204 }
205}
206
06e7fadc 207?>