cm_webservice.php 8.4 KB

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