webservice.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 handler. This needs to be a class that implements the interface WSErrorHandler
  15. *
  16. * @var WSErrorHandler
  17. */
  18. protected static $_handler;
  19. /**
  20. * Error code
  21. *
  22. * @var int
  23. */
  24. public $code;
  25. /**
  26. * Error message
  27. *
  28. * @var string
  29. */
  30. public $message;
  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 array('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. * Verifies the API key
  104. *
  105. * @param string Secret key
  106. * @return mixed WSError in case of failure, null in case of success
  107. */
  108. protected function verifyKey($secret_key)
  109. {
  110. $ip = trim($_SERVER['REMOTE_ADDR']);
  111. // if we are behind a reverse proxy, assume it will send the
  112. // HTTP_X_FORWARDED_FOR header and use this IP instead
  113. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  114. list($ip1, $ip2) = preg_split(
  115. '/,/',
  116. $_SERVER['HTTP_X_FORWARDED_FOR']
  117. );
  118. $ip = trim($ip1);
  119. }
  120. $security_key = $ip.$this->_configuration['security_key'];
  121. if (!api_is_valid_secret_key($secret_key, $security_key)) {
  122. return new WSError(1, "API key is invalid");
  123. } else {
  124. return null;
  125. }
  126. }
  127. /**
  128. * Gets the real user id based on the user id field name and value.
  129. * Note that if the user id field name is "chamilo_user_id", it will use the user id
  130. * in the system database
  131. *
  132. * @param string User id field name
  133. * @param string User id value
  134. * @return mixed System user id if the user was found, WSError otherwise
  135. */
  136. protected function getUserId($user_id_field_name, $user_id_value)
  137. {
  138. if ($user_id_field_name == "chamilo_user_id") {
  139. if (UserManager::is_user_id_valid(intval($user_id_value))) {
  140. return intval($user_id_value);
  141. } else {
  142. return new WSError(100, "User not found");
  143. }
  144. } else {
  145. $user_id = UserManager::get_user_id_from_original_id(
  146. $user_id_value,
  147. $user_id_field_name
  148. );
  149. if ($user_id == 0) {
  150. return new WSError(100, "User not found");
  151. } else {
  152. return $user_id;
  153. }
  154. }
  155. }
  156. /**
  157. * Gets the real course id based on the course id field name and value.
  158. * Note that if the course id field name is "chamilo_course_id", it will use the course id
  159. * in the system database
  160. *
  161. * @param string Course id field name
  162. * @param string Course id value
  163. * @return mixed System course id if the course was found, WSError otherwise
  164. */
  165. protected function getCourseId($course_id_field_name, $course_id_value)
  166. {
  167. if ($course_id_field_name == "chamilo_course_id") {
  168. if (CourseManager::get_course_code_from_course_id(
  169. intval($course_id_value)
  170. ) != null
  171. ) {
  172. return intval($course_id_value);
  173. } else {
  174. return new WSError(200, "Course not found");
  175. }
  176. } else {
  177. $courseId = CourseManager::get_course_code_from_original_id(
  178. $course_id_value,
  179. $course_id_field_name
  180. );
  181. if (!empty($courseId)) {
  182. return $courseId;
  183. } else {
  184. return new WSError(200, "Course not found");
  185. }
  186. }
  187. }
  188. /**
  189. * Gets the real session id based on the session id field name and value.
  190. * Note that if the session id field name is "chamilo_session_id", it will use the session id
  191. * in the system database
  192. *
  193. * @param string Session id field name
  194. * @param string Session id value
  195. * @return mixed System session id if the session was found, WSError otherwise
  196. */
  197. protected function getSessionId($session_id_field_name, $session_id_value)
  198. {
  199. if ($session_id_field_name == "chamilo_session_id") {
  200. $session = SessionManager::fetch((int) $session_id_value);
  201. if (!empty($session)) {
  202. return intval($session_id_value);
  203. } else {
  204. return new WSError(300, "Session not found");
  205. }
  206. } else {
  207. $session_id = SessionManager::getSessionIdFromOriginalId(
  208. $session_id_value,
  209. $session_id_field_name
  210. );
  211. if ($session_id == 0) {
  212. return new WSError(300, "Session not found");
  213. } else {
  214. return $session_id;
  215. }
  216. }
  217. }
  218. /**
  219. * Handles an error by calling the WSError error handler
  220. *
  221. * @param WSError Error
  222. */
  223. protected function handleError($error)
  224. {
  225. $handler = WSError::getErrorHandler();
  226. $handler->handle($error);
  227. }
  228. /**
  229. * Gets a successful result
  230. *
  231. * @return array Array with a code of 0 and a message 'Operation was successful'
  232. */
  233. protected function getSuccessfulResult()
  234. {
  235. return array('code' => 0, 'message' => 'Operation was successful');
  236. }
  237. /**
  238. * Test function. Returns the string success
  239. *
  240. * @return string Success
  241. */
  242. public function test()
  243. {
  244. return "success";
  245. }
  246. }