admin dbtransfer: MDL-18225 also, let the caller control how transactions are used.
[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
26
27/**
28 * web service library
29 */
30final class webservice_lib {
31
32 /**
33 * Return list of all web service protocol into the webservice folder
34 * @global <type> $CFG
35 * @return <type>
36 */
37 public static function get_list_protocols() {
38 global $CFG;
39 $protocols = array();
40 $directorypath = $CFG->dirroot . "/webservice";
41 if( $dh = opendir($directorypath)) {
42 while( false !== ($file = readdir($dh)))
43 {
44 if( $file == '.' || $file == '..' || $file == 'CVS') { // Skip '.' and '..'
45 continue;
46 }
47 $path = $directorypath . '/' . $file;
48 ///browse the subfolder
49 if( is_dir($path) ) {
50 $protocols[] = $file;
51 }
52 ///retrieve api.php file
53 else {
54 continue;
55 }
56 }
57 closedir($dh);
58 }
59 return $protocols;
60 }
61
62 /**
63 * Temporary Authentication method to be modified/removed
64 * @global <type> $DB
65 * @param <type> $token
66 * @return <type>
67 */
68 public static function mock_check_token($token) {
69 //fake test
70 if ($token == 465465465468468464) {
71 ///retrieve the user
72 global $DB;
73 $user = $DB->get_record('user', array('username'=>'wsuser', 'mnethostid'=>1));
74
75 if (empty($user)) {
76 return false;
77 }
78
79 return $user;
80 } else {
81 return false;
82 }
83 }
84
85}
86
87/**
88 * Web Service server base class
89 */
90abstract class webservice_server {
91
92 /**
93 * Web Service Protocol name (eg. SOAP, REST, XML-RPC,...)
94 * @var String
95 */
96 private $protocolname;
97
98 /**
99 * if set to false the server cannot be run
100 * @var String
101 */
102 private $enable;
103
104 public function __construct() {
105 }
106
107 abstract public function run();
108
109 public function get_protocolname() {
110 return $this->protocolname;
111 }
112
113 public function set_protocolname($protocolname) {
114 $this->protocolname = $protocolname;
115 }
116
117 public function get_enable() {
118 return $this->enable;
119 }
120
121 public function set_enable($enable) {
122 $this->enable = $enable;
123 }
124
125}
126
127/**
128 * Temporary authentication class to be removed/modified
129 */
130class ws_authentication {
131 /**
132 *
133 * @param array|struct $params
134 * @return integer
135 */
136 function tmp_get_token($params) {
137 if ($params['username'] == 'wsuser' && $params['password'] == 'wspassword') {
138 return '465465465468468464';
139 } else {
140 throw new moodle_exception('wrongusernamepassword');
141 }
142 }
143}
144
145?>