cm_webservice.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. require_once(dirname(__FILE__).'/../inc/global.inc.php');
  3. $libpath = api_get_path(LIBRARY_PATH);
  4. require_once $libpath.'usermanager.lib.php';
  5. require_once $libpath.'course.lib.php';
  6. /**
  7. * Error returned by one of the methods of the web service. Contains an error code and an error message
  8. */
  9. class WSCMError {
  10. /**
  11. * Error handler. This needs to be a class that implements the interface WSErrorHandler
  12. *
  13. * @var WSErrorHandler
  14. */
  15. protected static $_handler;
  16. /**
  17. * Error code
  18. *
  19. * @var int
  20. */
  21. public $code;
  22. /**
  23. * Error message
  24. *
  25. * @var string
  26. */
  27. public $message;
  28. /**
  29. * Constructor
  30. *
  31. * @param int Error code
  32. * @param string Error message
  33. */
  34. public function __construct($code, $message) {
  35. $this->code = $code;
  36. $this->message = $message;
  37. }
  38. /**
  39. * Sets the error handler
  40. *
  41. * @param WSErrorHandler Error handler
  42. */
  43. public static function setErrorHandler($handler) {
  44. if($handler instanceof WSErrorHandler) {
  45. self::$_handler = $handler;
  46. }
  47. }
  48. /**
  49. * Returns the error handler
  50. *
  51. * @return WSErrorHandler Error handler
  52. */
  53. public static function getErrorHandler() {
  54. return self::$_handler;
  55. }
  56. /**
  57. * Transforms the error into an array
  58. *
  59. * @return array Associative array with code and message
  60. */
  61. public function toArray() {
  62. return array('code' => $this->code, 'message' => $this->message);
  63. }
  64. }
  65. /**
  66. * Interface that must be implemented by any error handler
  67. */
  68. interface WSCMErrorHandler {
  69. /**
  70. * Handle method
  71. *
  72. * @param WSError Error
  73. */
  74. public function handle($error);
  75. }
  76. /**
  77. * Main class of the webservice. Webservice classes extend this class
  78. */
  79. class WSCM {
  80. /**
  81. * Chamilo configuration
  82. *
  83. * @var array
  84. */
  85. protected $_configuration;
  86. /**
  87. * Constructor
  88. */
  89. public function __construct() {
  90. $this->_configuration = $GLOBALS['_configuration'];
  91. }
  92. /**
  93. * Verifies the API key
  94. *
  95. * @param string Secret key
  96. * @return mixed WSError in case of failure, null in case of success
  97. */
  98. protected function verifyKey($secret_key) {
  99. $ip = trim($_SERVER['REMOTE_ADDR']);
  100. // if we are behind a reverse proxy, assume it will send the
  101. // HTTP_X_FORWARDED_FOR header and use this IP instead
  102. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  103. list($ip1,$ip2) = split(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
  104. $ip = trim($ip1);
  105. }
  106. $security_key = $ip.$this->_configuration['security_key'];
  107. if(!api_is_valid_secret_key($secret_key, $security_key)) {
  108. return new WSCMError(1, "API key is invalid");
  109. } else {
  110. return null;
  111. }
  112. }
  113. /**
  114. * Verifies if the user is valid
  115. *
  116. * @param <String> $username of the user in chamilo
  117. * @param <String> $pass of the same user (in MD5 of SHA)
  118. *
  119. * return "valid" if username e password are correct! Else, return a message error
  120. */
  121. public function verifyUserPass($username, $pass) {
  122. $login = $username;
  123. $password = $pass;
  124. //lookup the user in the main database
  125. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  126. $sql = "SELECT user_id, username, password, auth_source, active, expiration_date
  127. FROM $user_table
  128. WHERE username = '".trim(addslashes($login))."'";
  129. $result = Database::query($sql);
  130. if (Database::num_rows($result) > 0) {
  131. $uData = Database::fetch_array($result);
  132. if ($uData['auth_source'] == PLATFORM_AUTH_SOURCE) {
  133. $password = trim(stripslashes($password));
  134. // Check the user's password
  135. if ($password == $uData['password'] AND (trim($login) == $uData['username'])) {
  136. // Check if the account is active (not locked)
  137. if ($uData['active']=='1') {
  138. // Check if the expiration date has not been reached
  139. if ($uData['expiration_date']>date('Y-m-d H:i:s') OR $uData['expiration_date']=='0000-00-00 00:00:00') {
  140. return "valid";
  141. }
  142. else
  143. return get_lang('AccountExpired');
  144. }
  145. else
  146. return get_lang('AccountInactive');
  147. }
  148. else
  149. return get_lang('InvalidId');
  150. }
  151. else
  152. return get_lang('AccountURLInactive');
  153. }
  154. return get_lang('InvalidId');
  155. }
  156. /**
  157. * Return the encrypted pass
  158. * @param <String> $pass
  159. * @return <String> $pass encrypted
  160. */
  161. public function encryptPass($pass){
  162. return api_get_encrypted_password($pass);
  163. }
  164. /**
  165. * 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
  166. * in the system database
  167. *
  168. * @param string User id field name
  169. * @param string User id value
  170. * @return mixed System user id if the user was found, WSError otherwise
  171. */
  172. protected function getUserId($user_id_field_name, $user_id_value) {
  173. if($user_id_field_name == "chamilo_user_id") {
  174. if(UserManager::is_user_id_valid(intval($user_id_value))) {
  175. return intval($user_id_value);
  176. } else {
  177. return new WSCMError(100, "User not found");
  178. }
  179. } else {
  180. $user_id = UserManager::get_user_id_from_original_id($user_id_value, $user_id_field_name);
  181. if($user_id == 0) {
  182. return new WSCMError(100, "User not found");
  183. } else {
  184. return $user_id;
  185. }
  186. }
  187. }
  188. /**
  189. * 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
  190. * in the system database
  191. *
  192. * @param string Course id field name
  193. * @param string Course id value
  194. * @return mixed System course id if the course was found, WSError otherwise
  195. */
  196. protected function getCourseId($course_id_field_name, $course_id_value) {
  197. if($course_id_field_name == "chamilo_course_id") {
  198. if(CourseManager::get_course_code_from_course_id(intval($course_id_value)) != null) {
  199. return intval($course_id_value);
  200. } else {
  201. return new WSCMError(200, "Course not found");
  202. }
  203. } else {
  204. $course_code = CourseManager::get_course_code_from_original_id($course_id_value, $course_id_field_name);
  205. if($course_code == 0) {
  206. return new WSCMError(200, "Course not found");
  207. } else {
  208. $course_info = CourseManager::get_course_information($course_code);
  209. return $course_info['id'];
  210. }
  211. }
  212. }
  213. /**
  214. * 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
  215. * in the system database
  216. *
  217. * @param string Session id field name
  218. * @param string Session id value
  219. * @return mixed System session id if the session was found, WSError otherwise
  220. */
  221. protected function getSessionId($session_id_field_name, $session_id_value) {
  222. if($session_id_field_name == "chamilo_session_id") {
  223. $session = SessionManager::fetch((int)$session_id_value);
  224. if(!empty($session)) {
  225. return intval($session_id_value);
  226. } else {
  227. return new WSCMError(300, "Session not found");
  228. }
  229. } else {
  230. $session_id = SessionManager::get_session_id_from_original_id($session_id_value, $session_id_field_name);
  231. if($session_id == 0) {
  232. return new WSCMError(300, "Session not found");
  233. } else {
  234. return $session_id;
  235. }
  236. }
  237. }
  238. /**
  239. * Handles an error by calling the WSError error handler
  240. *
  241. * @param WSError Error
  242. */
  243. protected function handleError($error) {
  244. $handler = WSCMError::getErrorHandler();
  245. $handler->handle($error);
  246. }
  247. /**
  248. * Gets a successful result
  249. *
  250. * @return array Array with a code of 0 and a message 'Operation was successful'
  251. */
  252. protected function getSuccessfulResult() {
  253. return array('code' => 0, 'message' => 'Operation was successful');
  254. }
  255. /**
  256. * Test function. Returns the string success
  257. *
  258. * @return string Success
  259. */
  260. public function test() {
  261. return "success";
  262. }
  263. /**
  264. * *Strictly* reverts PHP's nl2br() effects (whether it was used in XHTML mode or not)
  265. * @param <type> $string
  266. * @return <type> $string
  267. */
  268. public function nl2br_revert($string) {
  269. return preg_replace('`<br(?: /)?>([\\n\\r])`', '$1', $string);
  270. }
  271. }