123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- <?php
- use Chamilo\UserBundle\Entity\User;
- require_once __DIR__.'/../inc/global.inc.php';
- class WSCMError
- {
-
- public $code;
-
- public $message;
-
- protected static $_handler;
-
- public function __construct($code, $message)
- {
- $this->code = $code;
- $this->message = $message;
- }
-
- public static function setErrorHandler($handler)
- {
- if ($handler instanceof WSErrorHandler) {
- self::$_handler = $handler;
- }
- }
-
- public static function getErrorHandler()
- {
- return self::$_handler;
- }
-
- public function toArray()
- {
- return ['code' => $this->code, 'message' => $this->message];
- }
- }
- interface WSCMErrorHandler
- {
-
- public function handle($error);
- }
- class WSCM
- {
-
- protected $_configuration;
-
- public function __construct()
- {
- $this->_configuration = $GLOBALS['_configuration'];
- }
-
- public function verifyUserPass($username, $pass)
- {
- $login = $username;
- $password = $pass;
- $userRepo = UserManager::getRepository();
-
- $uData = $userRepo->findOneBy([
- 'username' => trim(addslashes($login)),
- ]);
- if ($uData) {
- if ($uData->getAuthSource() == PLATFORM_AUTH_SOURCE) {
- $passwordEncoded = UserManager::encryptPassword($password, $uData);
-
- if ($passwordEncoded == $uData->getPassword() && (trim($login) == $uData->getUsername())) {
-
- if ($uData->getActive()) {
-
- $now = new DateTime();
- if ($uData->getExpirationDate() > $now || !$uData->getExpirationDate()) {
- return "valid";
- } else {
- return get_lang('AccountExpired');
- }
- } else {
- return get_lang('AccountInactive');
- }
- } else {
- return get_lang('InvalidId');
- }
- } else {
- return get_lang('AccountURLInactive');
- }
- }
- return get_lang('InvalidId');
- }
-
- public function test()
- {
- return "success";
- }
-
- public function nl2br_revert($string)
- {
- return preg_replace('`<br(?: /)?>([\\n\\r])`', '$1', $string);
- }
-
- protected function verifyKey($secret_key)
- {
- $ip = trim($_SERVER['REMOTE_ADDR']);
-
-
- if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
- list($ip1, $ip2) = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
- $ip = trim($ip1);
- }
- $security_key = $ip.$this->_configuration['security_key'];
- if (!api_is_valid_secret_key($secret_key, $security_key)) {
- return new WSCMError(1, "API key is invalid");
- } else {
- return null;
- }
- }
-
- protected function getUserId($user_id_field_name, $user_id_value)
- {
- if ($user_id_field_name == "chamilo_user_id") {
- if (UserManager::is_user_id_valid(intval($user_id_value))) {
- return intval($user_id_value);
- } else {
- return new WSCMError(100, "User not found");
- }
- } else {
- $user_id = UserManager::get_user_id_from_original_id(
- $user_id_value,
- $user_id_field_name
- );
- if ($user_id == 0) {
- return new WSCMError(100, "User not found");
- } else {
- return $user_id;
- }
- }
- }
-
- protected function getCourseId($course_id_field_name, $course_id_value)
- {
- if ($course_id_field_name == "chamilo_course_id") {
- if (CourseManager::get_course_code_from_course_id($course_id_value) != null) {
- return intval($course_id_value);
- } else {
- return new WSCMError(200, "Course not found");
- }
- } else {
- $courseId = CourseManager::get_course_code_from_original_id(
- $course_id_value,
- $course_id_field_name
- );
- if (empty($courseId)) {
- return new WSCMError(200, "Course not found");
- } else {
- return $courseId;
- }
- }
- }
-
- protected function getSessionId($session_id_field_name, $session_id_value)
- {
- if ($session_id_field_name == "chamilo_session_id") {
- $session = SessionManager::fetch((int) $session_id_value);
- if (!empty($session)) {
- return intval($session_id_value);
- } else {
- return new WSCMError(300, "Session not found");
- }
- } else {
- $session_id = SessionManager::getSessionIdFromOriginalId(
- $session_id_value,
- $session_id_field_name
- );
- if ($session_id == 0) {
- return new WSCMError(300, "Session not found");
- } else {
- return $session_id;
- }
- }
- }
-
- protected function handleError($error)
- {
- $handler = WSCMError::getErrorHandler();
- $handler->handle($error);
- }
-
- protected function getSuccessfulResult()
- {
- return ['code' => 0, 'message' => 'Operation was successful'];
- }
- }
|