webservice.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.webservices
  5. */
  6. require_once(dirname(__FILE__).'/../inc/global.inc.php');
  7. $libpath = api_get_path(LIBRARY_PATH);
  8. require_once $libpath.'usermanager.lib.php';
  9. require_once $libpath.'course.lib.php';
  10. /**
  11. * Error returned by one of the methods of the web service. Contains an error code and an error message
  12. */
  13. class WSError {
  14. /**
  15. * Error handler. This needs to be a class that implements the interface WSErrorHandler
  16. *
  17. * @var WSErrorHandler
  18. */
  19. protected static $_handler;
  20. /**
  21. * Error code
  22. *
  23. * @var int
  24. */
  25. public $code;
  26. /**
  27. * Error message
  28. *
  29. * @var string
  30. */
  31. public $message;
  32. /**
  33. * Constructor
  34. *
  35. * @param int Error code
  36. * @param string Error message
  37. */
  38. public function __construct($code, $message) {
  39. $this->code = $code;
  40. $this->message = $message;
  41. }
  42. /**
  43. * Sets the error handler
  44. *
  45. * @param WSErrorHandler Error handler
  46. */
  47. public static function setErrorHandler($handler) {
  48. if($handler instanceof WSErrorHandler) {
  49. self::$_handler = $handler;
  50. }
  51. }
  52. /**
  53. * Returns the error handler
  54. *
  55. * @return WSErrorHandler Error handler
  56. */
  57. public static function getErrorHandler() {
  58. return self::$_handler;
  59. }
  60. /**
  61. * Transforms the error into an array
  62. *
  63. * @return array Associative array with code and message
  64. */
  65. public function toArray() {
  66. return array('code' => $this->code, 'message' => $this->message);
  67. }
  68. }
  69. /**
  70. * Interface that must be implemented by any error handler
  71. */
  72. interface WSErrorHandler {
  73. /**
  74. * Handle method
  75. *
  76. * @param WSError Error
  77. */
  78. public function handle($error);
  79. }
  80. /**
  81. * Main class of the webservice. Webservice classes extend this class
  82. */
  83. class WS {
  84. /**
  85. * Chamilo configuration
  86. *
  87. * @var array
  88. */
  89. protected $_configuration;
  90. /**
  91. * Constructor
  92. */
  93. public function __construct() {
  94. $this->_configuration = $GLOBALS['_configuration'];
  95. }
  96. /**
  97. * Verifies the API key
  98. *
  99. * @param string Secret key
  100. * @return mixed WSError in case of failure, null in case of success
  101. */
  102. protected function verifyKey($secret_key) {
  103. $security_key = $_SERVER['REMOTE_ADDR'].$this->_configuration['security_key'];
  104. if(!api_is_valid_secret_key($secret_key, $security_key)) {
  105. return new WSError(1, "API key is invalid");
  106. } else {
  107. return null;
  108. }
  109. }
  110. /**
  111. * Gets the real user id based on the user id field name and value. Note that if the user id field name is "chamilo_user_id", it will use the user id
  112. * in the system database
  113. *
  114. * @param string User id field name
  115. * @param string User id value
  116. * @return mixed System user id if the user was found, WSError otherwise
  117. */
  118. protected function getUserId($user_id_field_name, $user_id_value) {
  119. if($user_id_field_name == "chamilo_user_id") {
  120. if(UserManager::is_user_id_valid(intval($user_id_value))) {
  121. return intval($user_id_value);
  122. } else {
  123. return new WSError(100, "User not found");
  124. }
  125. } else {
  126. $user_id = UserManager::get_user_id_from_original_id($user_id_value, $user_id_field_name);
  127. if($user_id == 0) {
  128. return new WSError(100, "User not found");
  129. } else {
  130. return $user_id;
  131. }
  132. }
  133. }
  134. /**
  135. * Gets the real course id based on the course id field name and value. Note that if the course id field name is "chamilo_course_id", it will use the course id
  136. * in the system database
  137. *
  138. * @param string Course id field name
  139. * @param string Course id value
  140. * @return mixed System course id if the course was found, WSError otherwise
  141. */
  142. protected function getCourseId($course_id_field_name, $course_id_value) {
  143. if($course_id_field_name == "chamilo_course_id") {
  144. if(CourseManager::get_course_code_from_course_id(intval($course_id_value)) != null) {
  145. return intval($course_id_value);
  146. } else {
  147. return new WSError(200, "Course not found");
  148. }
  149. } else {
  150. $course_code = CourseManager::get_course_code_from_original_id($course_id_value, $course_id_field_name);
  151. if (!empty($course_code)) {
  152. $course_info = CourseManager::get_course_information($course_code);
  153. return $course_info['id'];
  154. } else {
  155. return new WSError(200, "Course not found");
  156. }
  157. /*
  158. if($course_code == 0) {
  159. return new WSError(200, "Course not found");
  160. } else {
  161. return $course_info['id'];
  162. }
  163. */
  164. }
  165. }
  166. /**
  167. * Gets the real session id based on the session id field name and value. Note that if the session id field name is "chamilo_session_id", it will use the session id
  168. * in the system database
  169. *
  170. * @param string Session id field name
  171. * @param string Session id value
  172. * @return mixed System session id if the session was found, WSError otherwise
  173. */
  174. protected function getSessionId($session_id_field_name, $session_id_value) {
  175. if($session_id_field_name == "chamilo_session_id") {
  176. $session = SessionManager::fetch((int)$session_id_value);
  177. if(!empty($session)) {
  178. return intval($session_id_value);
  179. } else {
  180. return new WSError(300, "Session not found");
  181. }
  182. } else {
  183. $session_id = SessionManager::get_session_id_from_original_id($session_id_value, $session_id_field_name);
  184. if($session_id == 0) {
  185. return new WSError(300, "Session not found");
  186. } else {
  187. return $session_id;
  188. }
  189. }
  190. }
  191. /**
  192. * Handles an error by calling the WSError error handler
  193. *
  194. * @param WSError Error
  195. */
  196. protected function handleError($error) {
  197. $handler = WSError::getErrorHandler();
  198. $handler->handle($error);
  199. }
  200. /**
  201. * Gets a successful result
  202. *
  203. * @return array Array with a code of 0 and a message 'Operation was successful'
  204. */
  205. protected function getSuccessfulResult() {
  206. return array('code' => 0, 'message' => 'Operation was successful');
  207. }
  208. /**
  209. * Test function. Returns the string success
  210. *
  211. * @return string Success
  212. */
  213. public function test() {
  214. return "success";
  215. }
  216. }