cm_webservice.php 8.2 KB

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