webservice.php 5.9 KB

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