webservice.php 5.7 KB

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