webservice.php 5.7 KB

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