webservice.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.
  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. $this->code = $code;
  39. $this->message = $message;
  40. }
  41. /**
  42. * Sets the error handler
  43. *
  44. * @param WSErrorHandler Error handler
  45. */
  46. public static function setErrorHandler($handler) {
  47. if($handler instanceof WSErrorHandler) {
  48. self::$_handler = $handler;
  49. }
  50. }
  51. /**
  52. * Returns the error handler
  53. *
  54. * @return WSErrorHandler Error handler
  55. */
  56. public static function getErrorHandler() {
  57. return self::$_handler;
  58. }
  59. /**
  60. * Transforms the error into an array
  61. *
  62. * @return array Associative array with code and message
  63. */
  64. public function toArray() {
  65. return array('code' => $this->code, 'message' => $this->message);
  66. }
  67. }
  68. /**
  69. * Interface that must be implemented by any error handler
  70. */
  71. interface WSErrorHandler {
  72. /**
  73. * Handle method
  74. *
  75. * @param WSError Error
  76. */
  77. public function handle($error);
  78. }
  79. /**
  80. * Main class of the webservice. Webservice classes extend this class
  81. */
  82. class WS
  83. {
  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. $ip = trim($_SERVER['REMOTE_ADDR']);
  104. // if we are behind a reverse proxy, assume it will send the
  105. // HTTP_X_FORWARDED_FOR header and use this IP instead
  106. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  107. list($ip1,$ip2) = preg_split('/,/',$_SERVER['HTTP_X_FORWARDED_FOR']);
  108. $ip = trim($ip1);
  109. }
  110. $security_key = $ip.$this->_configuration['security_key'];
  111. if (!api_is_valid_secret_key($secret_key, $security_key)) {
  112. return new WSError(1, "API key is invalid");
  113. } else {
  114. return null;
  115. }
  116. }
  117. /**
  118. * Gets the real user id based on the user id field name and value.
  119. * Note that if the user id field name is "chamilo_user_id", it will use the user id
  120. * in the system database
  121. *
  122. * @param string User id field name
  123. * @param string User id value
  124. * @return mixed System user id if the user was found, WSError otherwise
  125. */
  126. protected function getUserId($user_id_field_name, $user_id_value) {
  127. if($user_id_field_name == "chamilo_user_id") {
  128. if(UserManager::is_user_id_valid(intval($user_id_value))) {
  129. return intval($user_id_value);
  130. } else {
  131. return new WSError(100, "User not found");
  132. }
  133. } else {
  134. $user_id = UserManager::get_user_id_from_original_id($user_id_value, $user_id_field_name);
  135. if($user_id == 0) {
  136. return new WSError(100, "User not found");
  137. } else {
  138. return $user_id;
  139. }
  140. }
  141. }
  142. /**
  143. * Gets the real course id based on the course id field name and value.
  144. * Note that if the course id field name is "chamilo_course_id", it will use the course id
  145. * in the system database
  146. *
  147. * @param string Course id field name
  148. * @param string Course id value
  149. * @return mixed System course id if the course was found, WSError otherwise
  150. */
  151. protected function getCourseId($course_id_field_name, $course_id_value)
  152. {
  153. if ($course_id_field_name == "chamilo_course_id") {
  154. if (CourseManager::get_course_code_from_course_id(
  155. intval($course_id_value)
  156. ) != null
  157. ) {
  158. return intval($course_id_value);
  159. } else {
  160. return new WSError(200, "Course not found");
  161. }
  162. } else {
  163. $courseId = CourseManager::get_course_code_from_original_id(
  164. $course_id_value,
  165. $course_id_field_name
  166. );
  167. if (!empty($courseId)) {
  168. return $courseId;
  169. } else {
  170. return new WSError(200, "Course not found");
  171. }
  172. }
  173. }
  174. /**
  175. * Gets the real session id based on the session id field name and value.
  176. * Note that if the session id field name is "chamilo_session_id", it will use the session id
  177. * in the system database
  178. *
  179. * @param string Session id field name
  180. * @param string Session id value
  181. * @return mixed System session id if the session was found, WSError otherwise
  182. */
  183. protected function getSessionId($session_id_field_name, $session_id_value)
  184. {
  185. if ($session_id_field_name == "chamilo_session_id") {
  186. $session = SessionManager::fetch((int)$session_id_value);
  187. if (!empty($session)) {
  188. return intval($session_id_value);
  189. } else {
  190. return new WSError(300, "Session not found");
  191. }
  192. } else {
  193. $session_id = SessionManager::getSessionIdFromOriginalId(
  194. $session_id_value,
  195. $session_id_field_name
  196. );
  197. if ($session_id == 0) {
  198. return new WSError(300, "Session not found");
  199. } else {
  200. return $session_id;
  201. }
  202. }
  203. }
  204. /**
  205. * Handles an error by calling the WSError error handler
  206. *
  207. * @param WSError Error
  208. */
  209. protected function handleError($error) {
  210. $handler = WSError::getErrorHandler();
  211. $handler->handle($error);
  212. }
  213. /**
  214. * Gets a successful result
  215. *
  216. * @return array Array with a code of 0 and a message 'Operation was successful'
  217. */
  218. protected function getSuccessfulResult() {
  219. return array('code' => 0, 'message' => 'Operation was successful');
  220. }
  221. /**
  222. * Test function. Returns the string success
  223. *
  224. * @return string Success
  225. */
  226. public function test() {
  227. return "success";
  228. }
  229. }