/** * This function connect to facebook and retrieves the user info * If user does not exist in chamilo, it creates it and logs in * If user already exists, it updates his info */ function facebookConnect() { global $facebook_config; global $helper; try { $helper = new FacebookRedirectLoginHelper($facebook_config['return_url']); $session = $helper->getSessionFromRedirect(); // see if we have a session if (isset($session)) { // graph api request for user data $request = new FacebookRequest($session, 'GET', '/me'); $response = $request->execute(); // get response $graphObject = $response->getGraphObject(); $username = changeToValidChamiloLogin($graphObject->getProperty('email')); $email = $graphObject->getProperty('email'); $locale = $graphObject->getProperty('locale'); $language = facebookPluginGetLanguage($locale); if (!$language) { $language = 'en_US'; } //Checks if user already exists in chamilo $u = array('firstname' => $graphObject->getProperty('first_name'), 'lastname' => $graphObject->getProperty('last_name'), 'status' => STUDENT, 'email' => $graphObject->getProperty('email'), 'username' => $username, 'language' => $language, 'password' => 'facebook', 'auth_source' => 'facebook', 'extra' => array()); $chamiloUinfo = api_get_user_info_from_email($email); if ($chamiloUinfo === false) { // we have to create the user $chamilo_uid = external_add_user($u); if ($chamilo_uid !== false) { $_user['user_id'] = $chamilo_uid; $_user['uidReset'] = true; $_SESSION['_user'] = $_user; header('Location:' . api_get_path(WEB_PATH)); exit; } else { return false; } } else { // User already exists, update info and login $chamilo_uid = $chamiloUinfo['user_id']; $u['user_id'] = $chamilo_uid; external_update_user($u); $_user['user_id'] = $chamilo_uid; $_user['uidReset'] = true; $_SESSION['_user'] = $_user; header('Location:' . api_get_path(WEB_PATH)); exit; } } } catch (FacebookRequestException $ex) { echo $ex; } catch (Exception $ex) { // When validation fails or other local issues } }
/** * Validates the imported data. */ function validate_data($users_courses) { $errors = array(); $coursecodes = array(); foreach ($users_courses as $index => $user_course) { $user_course['line'] = $index + 1; // 1. Check whether mandatory fields are set. $mandatory_fields = array('Email', 'CourseCode', 'Status'); foreach ($mandatory_fields as $key => $field) { if (!isset($user_course[$field]) || strlen($user_course[$field]) == 0) { $user_course['error'] = get_lang($field . 'Mandatory'); $errors[] = $user_course; } } // 2. Check whether coursecode exists. if (isset($user_course['CourseCode']) && strlen($user_course['CourseCode']) != 0) { // 2.1 Check whethher code has been allready used by this CVS-file. if (!isset($coursecodes[$user_course['CourseCode']])) { // 2.1.1 Check whether course with this code exists in the system. $course_table = Database::get_main_table(TABLE_MAIN_COURSE); $sql = "SELECT * FROM {$course_table}\n WHERE code = '" . Database::escape_string($user_course['CourseCode']) . "'"; $res = Database::query($sql); if (Database::num_rows($res) == 0) { $user_course['error'] = get_lang('CodeDoesNotExists'); $errors[] = $user_course; } else { $coursecodes[$user_course['CourseCode']] = 1; } } } // 3. Check whether Email exists. if (isset($user_course['Email']) && strlen($user_course['Email']) != 0) { $user = api_get_user_info_from_email($user_course['Email']); if (empty($user)) { $user_course['error'] = get_lang('UnknownUser'); $errors[] = $user_course; } } // 4. Check whether status is valid. if (isset($user_course['Status']) && strlen($user_course['Status']) != 0) { if ($user_course['Status'] != COURSEMANAGER && $user_course['Status'] != STUDENT) { $user_course['error'] = get_lang('UnknownStatus'); $errors[] = $user_course; } } } return $errors; }
/** * @param array $users * @param bool $checkUniqueEmail * @return array */ function validate_data($users, $checkUniqueEmail = false) { global $defined_auth_sources; $errors = array(); $usernames = array(); // 1. Check if mandatory fields are set. $mandatory_fields = array('LastName', 'FirstName'); if (api_get_setting('registration', 'email') == 'true' || $checkUniqueEmail) { $mandatory_fields[] = 'Email'; } $classExistList = array(); $usergroup = new UserGroup(); foreach ($users as $user) { foreach ($mandatory_fields as $field) { if (empty($user[$field])) { $user['error'] = get_lang($field . 'Mandatory'); $errors[] = $user; } } $username = $user['UserName']; // 2. Check username, first, check whether it is empty. if (!UserManager::is_username_empty($username)) { // 2.1. Check whether username is too long. if (UserManager::is_username_too_long($username)) { $user['error'] = get_lang('UserNameTooLong'); $errors[] = $user; } // 2.1.1 $hasDash = strpos($username, '-'); if ($hasDash !== false) { $user['error'] = get_lang('UserNameHasDash'); $errors[] = $user; } // 2.2. Check whether the username was used twice in import file. if (isset($usernames[$user['UserName']])) { $user['error'] = get_lang('UserNameUsedTwice'); $errors[] = $user; } $usernames[$user['UserName']] = 1; // 2.3. Check whether username is already occupied. if (!UserManager::is_username_available($user['UserName'])) { $user['error'] = get_lang('UserNameNotAvailable'); $errors[] = $user; } } if ($checkUniqueEmail) { if (isset($user['Email'])) { $userFromEmail = api_get_user_info_from_email($user['Email']); if (!empty($userFromEmail)) { $user['error'] = get_lang('EmailUsedTwice'); $errors[] = $user; } } } // 3. Check status. if (isset($user['Status']) && !api_status_exists($user['Status'])) { $user['error'] = get_lang('WrongStatus'); $errors[] = $user; } // 4. Check ClassId if (!empty($user['ClassId'])) { $classId = explode('|', trim($user['ClassId'])); foreach ($classId as $id) { if (in_array($id, $classExistList)) { continue; } $info = $usergroup->get($id); if (empty($info)) { $user['error'] = sprintf(get_lang('ClassIdDoesntExists'), $id); $errors[] = $user; } else { $classExistList[] = $info['id']; } } } // 5. Check authentication source if (!empty($user['AuthSource'])) { if (!in_array($user['AuthSource'], $defined_auth_sources)) { $user['error'] = get_lang('AuthSourceNotAvailable'); $errors[] = $user; } } } return $errors; }