123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- require_once __DIR__.'/../inc/global.inc.php';
- class WSError
- {
-
- 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 WSErrorHandler
- {
-
- public function handle($error);
- }
- class WS
- {
-
- protected $_configuration;
-
- public function __construct()
- {
- $this->_configuration = $GLOBALS['_configuration'];
- }
-
- public function test()
- {
- return "success";
- }
-
- protected function verifyKey($secret_key)
- {
- $ip = trim($_SERVER['REMOTE_ADDR']);
-
-
- if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
- list($ip1, $ip2) = preg_split(
- '/,/',
- $_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 WSError(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 WSError(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 WSError(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(
- intval($course_id_value)
- ) != null
- ) {
- return intval($course_id_value);
- } else {
- return new WSError(200, "Course not found");
- }
- } else {
- $courseId = CourseManager::get_course_code_from_original_id(
- $course_id_value,
- $course_id_field_name
- );
- if (!empty($courseId)) {
- return $courseId;
- } else {
- return new WSError(200, "Course not found");
- }
- }
- }
-
- 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 WSError(300, "Session not found");
- }
- } else {
- $session_id = SessionManager::getSessionIdFromOriginalId(
- $session_id_value,
- $session_id_field_name
- );
- if ($session_id == 0) {
- return new WSError(300, "Session not found");
- } else {
- return $session_id;
- }
- }
- }
-
- protected function handleError($error)
- {
- $handler = WSError::getErrorHandler();
- $handler->handle($error);
- }
-
- protected function getSuccessfulResult()
- {
- return ['code' => 0, 'message' => 'Operation was successful'];
- }
- }
|