webservice.php 6.9 KB

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