inscription.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script displays a form for registering new users.
  5. * @package chamilo.auth
  6. */
  7. use \ChamiloSession as Session;
  8. //quick hack to adapt the registration form result to the selected registration language
  9. if (!empty($_POST['language'])) {
  10. $_GET['language'] = $_POST['language'];
  11. }
  12. require_once '../inc/global.inc.php';
  13. $htmlHeadXtra[] = api_get_password_checker_js('#username', '#pass1');
  14. // User is not allowed if Terms and Conditions are disabled and
  15. // registration is disabled too.
  16. $isNotAllowedHere = api_get_setting('allow_terms_conditions') === 'false' &&
  17. api_get_setting('allow_registration') === 'false';
  18. if ($isNotAllowedHere) {
  19. api_not_allowed(true, get_lang('RegistrationDisabled'));
  20. }
  21. if (!empty($_SESSION['user_language_choice'])) {
  22. $user_selected_language = $_SESSION['user_language_choice'];
  23. } elseif (!empty($_SESSION['_user']['language'])) {
  24. $user_selected_language = $_SESSION['_user']['language'];
  25. } else {
  26. $user_selected_language = get_setting('platformLanguage');
  27. }
  28. $form = new FormValidator('registration');
  29. if (api_get_setting('allow_terms_conditions') == 'true') {
  30. $user_already_registered_show_terms = isset($_SESSION['term_and_condition']['user_id']);
  31. } else {
  32. $user_already_registered_show_terms = false;
  33. }
  34. // Direct Link Subscription feature #5299
  35. $course_code_redirect = isset($_REQUEST['c']) && !empty($_REQUEST['c']) ? $_REQUEST['c'] : null;
  36. $exercise_redirect = isset($_REQUEST['e']) && !empty($_REQUEST['e']) ? $_REQUEST['e'] : null;
  37. if (!empty($course_code_redirect)) {
  38. Session::write('course_redirect', $course_code_redirect);
  39. Session::write('exercise_redirect', $exercise_redirect);
  40. }
  41. if ($user_already_registered_show_terms == false) {
  42. if (api_is_western_name_order()) {
  43. // FIRST NAME and LAST NAME
  44. $form->addElement('text', 'firstname', get_lang('FirstName'), array('size' => 40));
  45. $form->addElement('text', 'lastname', get_lang('LastName'), array('size' => 40));
  46. } else {
  47. // LAST NAME and FIRST NAME
  48. $form->addElement('text', 'lastname', get_lang('LastName'), array('size' => 40));
  49. $form->addElement('text', 'firstname', get_lang('FirstName'), array('size' => 40));
  50. }
  51. $form->applyFilter(array('lastname', 'firstname'), 'trim');
  52. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  53. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  54. // EMAIL
  55. $form->addElement('text', 'email', get_lang('Email'), array('size' => 40));
  56. if (api_get_setting('registration', 'email') == 'true') {
  57. $form->addRule('email', get_lang('ThisFieldIsRequired'), 'required');
  58. }
  59. if (api_get_setting('login_is_email') == 'true') {
  60. $form->applyFilter('email', 'trim');
  61. if (api_get_setting('registration', 'email') != 'true') {
  62. $form->addRule('email', get_lang('ThisFieldIsRequired'), 'required');
  63. }
  64. $form->addRule('email', sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  65. $form->addRule('email', get_lang('UserTaken'), 'username_available');
  66. }
  67. $form->addRule('email', get_lang('EmailWrong'), 'email');
  68. if (api_get_setting('openid_authentication') == 'true') {
  69. $form->addElement('text', 'openid', get_lang('OpenIDURL'), array('size' => 40));
  70. }
  71. // Enabled by Ivan Tcholakov, 06-APR-2009. CONFVAL_ASK_FOR_OFFICIAL_CODE = false by default.
  72. // OFFICIAL CODE
  73. if (CONFVAL_ASK_FOR_OFFICIAL_CODE) {
  74. $form->addElement('text', 'official_code', get_lang('OfficialCode'), array('size' => 40));
  75. if (api_get_setting('registration', 'officialcode') == 'true') {
  76. $form->addRule(
  77. 'official_code',
  78. get_lang('ThisFieldIsRequired'),
  79. 'required'
  80. );
  81. }
  82. }
  83. // USERNAME
  84. if (api_get_setting('login_is_email') != 'true') {
  85. $form->addElement('text', 'username', get_lang('UserName'), array('id' => 'username', 'size' => USERNAME_MAX_LENGTH));
  86. $form->applyFilter('username', 'trim');
  87. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  88. $form->addRule('username', sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  89. $form->addRule('username', get_lang('UsernameWrong'), 'username');
  90. $form->addRule('username', get_lang('UserTaken'), 'username_available');
  91. }
  92. // PASSWORD
  93. $form->addElement('password', 'pass1', get_lang('Pass'), array('id' => 'pass1', 'size' => 20, 'autocomplete' => 'off'));
  94. if (isset($_configuration['allow_strength_pass_checker']) && $_configuration['allow_strength_pass_checker']) {
  95. $form->addElement('label', null, '<div id="password_progress"></div>');
  96. }
  97. $form->addElement('password', 'pass2', get_lang('Confirmation'), array('id' => 'pass2', 'size' => 20, 'autocomplete' => 'off'));
  98. $form->addRule('pass1', get_lang('ThisFieldIsRequired'), 'required');
  99. $form->addRule('pass2', get_lang('ThisFieldIsRequired'), 'required');
  100. $form->addRule(array('pass1', 'pass2'), get_lang('PassTwo'), 'compare');
  101. if (CHECK_PASS_EASY_TO_FIND) {
  102. $form->addRule(
  103. 'password1',
  104. get_lang('PassTooEasy') . ': ' . api_generate_password(),
  105. 'callback',
  106. 'api_check_password'
  107. );
  108. }
  109. // PHONE
  110. $form->addElement('text', 'phone', get_lang('Phone'), array('size' => 20));
  111. if (api_get_setting('registration', 'phone') == 'true') {
  112. $form->addRule('phone', get_lang('ThisFieldIsRequired'), 'required');
  113. }
  114. // PICTURE
  115. /*if (api_get_setting('profile', 'picture') == 'true') {
  116. $form->addElement('file', 'picture', get_lang('AddPicture'));
  117. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  118. $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  119. }*/
  120. // LANGUAGE
  121. if (api_get_setting('registration', 'language') == 'true') {
  122. $form->addElement('select_language', 'language', get_lang('Language'));
  123. }
  124. // STUDENT/TEACHER
  125. if (api_get_setting('allow_registration_as_teacher') != 'false') {
  126. $form->addElement('radio', 'status', get_lang('Profile'), get_lang('RegStudent'), STUDENT);
  127. $form->addElement('radio', 'status', null, get_lang('RegAdmin'), COURSEMANAGER);
  128. }
  129. $allowCaptcha = isset($_configuration['allow_captcha']) ? $_configuration['allow_captcha'] : false;
  130. if ($allowCaptcha) {
  131. $ajax = api_get_path(WEB_AJAX_PATH).'form.ajax.php?a=get_captcha';
  132. $options = array(
  133. 'width' => 220,
  134. 'height' => 90,
  135. 'callback' => $ajax.'&var='.basename(__FILE__, '.php'),
  136. 'sessionVar' => basename(__FILE__, '.php'),
  137. 'imageOptions' => array(
  138. 'font_size' => 20,
  139. 'font_path' => api_get_path(SYS_FONTS_PATH) . 'opensans/',
  140. 'font_file' => 'OpenSans-Regular.ttf',
  141. //'output' => 'gif'
  142. )
  143. );
  144. $captcha_question = $form->addElement('CAPTCHA_Image', 'captcha_question', '', $options);
  145. $form->addElement('static', null, null, get_lang('ClickOnTheImageForANewOne'));
  146. $form->addElement('text', 'captcha', get_lang('EnterTheLettersYouSee'), array('size' => 40));
  147. $form->addRule('captcha', get_lang('EnterTheCharactersYouReadInTheImage'), 'required', null, 'client');
  148. $form->addRule('captcha', get_lang('TheTextYouEnteredDoesNotMatchThePicture'), 'CAPTCHA', $captcha_question);
  149. }
  150. // EXTENDED FIELDS
  151. if (api_get_setting('extended_profile') == 'true' &&
  152. api_get_setting('extendedprofile_registration', 'mycomptetences') == 'true'
  153. ) {
  154. $form->addHtmlEditor('competences', get_lang('MyCompetences'), false, false, array('ToolbarSet' => 'register', 'Width' => '100%', 'Height' => '130'));
  155. }
  156. if (api_get_setting('extended_profile') == 'true' &&
  157. api_get_setting('extendedprofile_registration', 'mydiplomas') == 'true'
  158. ) {
  159. $form->addHtmlEditor('diplomas', get_lang('MyDiplomas'), false, false, array('ToolbarSet' => 'register', 'Width' => '100%', 'Height' => '130'));
  160. }
  161. if (api_get_setting('extended_profile') == 'true' &&
  162. api_get_setting('extendedprofile_registration', 'myteach') == 'true'
  163. ) {
  164. $form->addHtmlEditor('teach', get_lang('MyTeach'), false, false, array('ToolbarSet' => 'register', 'Width' => '100%', 'Height' => '130'));
  165. }
  166. if (api_get_setting('extended_profile') == 'true' &&
  167. api_get_setting('extendedprofile_registration', 'mypersonalopenarea') == 'true'
  168. ) {
  169. $form->addHtmlEditor('openarea', get_lang('MyPersonalOpenArea'), false, false, array('ToolbarSet' => 'register', 'Width' => '100%', 'Height' => '130'));
  170. }
  171. if (api_get_setting('extended_profile') == 'true') {
  172. if (api_get_setting('extendedprofile_registration', 'mycomptetences') == 'true' &&
  173. api_get_setting('extendedprofile_registrationrequired', 'mycomptetences') == 'true'
  174. ) {
  175. $form->addRule('competences', get_lang('ThisFieldIsRequired'), 'required');
  176. }
  177. if (api_get_setting('extendedprofile_registration', 'mydiplomas') == 'true' &&
  178. api_get_setting('extendedprofile_registrationrequired', 'mydiplomas') == 'true'
  179. ) {
  180. $form->addRule('diplomas', get_lang('ThisFieldIsRequired'), 'required');
  181. }
  182. if (api_get_setting('extendedprofile_registration', 'myteach') == 'true' &&
  183. api_get_setting('extendedprofile_registrationrequired', 'myteach') == 'true'
  184. ) {
  185. $form->addRule('teach', get_lang('ThisFieldIsRequired'), 'required');
  186. }
  187. if (api_get_setting('extendedprofile_registration', 'mypersonalopenarea') == 'true' &&
  188. api_get_setting('extendedprofile_registrationrequired', 'mypersonalopenarea') == 'true'
  189. ) {
  190. $form->addRule('openarea', get_lang('ThisFieldIsRequired'), 'required');
  191. }
  192. }
  193. // EXTRA FIELDS
  194. $extra_data = UserManager::get_extra_user_data(api_get_user_id(), true);
  195. UserManager::set_extra_fields_in_form($form, $extra_data, 'registration');
  196. }
  197. if (isset($_SESSION['user_language_choice']) && $_SESSION['user_language_choice'] != '') {
  198. $defaults['language'] = $_SESSION['user_language_choice'];
  199. } else {
  200. $defaults['language'] = api_get_setting('platformLanguage');
  201. }
  202. if (!empty($_GET['username'])) {
  203. $defaults['username'] = Security::remove_XSS($_GET['username']);
  204. }
  205. if (!empty($_GET['email'])) {
  206. $defaults['email'] = Security::remove_XSS($_GET['email']);
  207. }
  208. if (!empty($_GET['phone'])) {
  209. $defaults['phone'] = Security::remove_XSS($_GET['phone']);
  210. }
  211. if (api_get_setting('openid_authentication') == 'true' && !empty($_GET['openid'])) {
  212. $defaults['openid'] = Security::remove_XSS($_GET['openid']);
  213. }
  214. $defaults['status'] = STUDENT;
  215. if (is_array($extra_data)) {
  216. $defaults = array_merge($defaults, $extra_data);
  217. }
  218. $form->setDefaults($defaults);
  219. $content = null;
  220. if (!CustomPages::enabled()) {
  221. // Load terms & conditions from the current lang
  222. if (api_get_setting('allow_terms_conditions') == 'true') {
  223. $get = array_keys($_GET);
  224. if (isset($get)) {
  225. if ($get[0] == 'legal') {
  226. $language = api_get_interface_language();
  227. $language = api_get_language_id($language);
  228. $term_preview = LegalManager::get_last_condition($language);
  229. if (!$term_preview) {
  230. //look for the default language
  231. $language = api_get_setting('platformLanguage');
  232. $language = api_get_language_id($language);
  233. $term_preview = LegalManager::get_last_condition($language);
  234. }
  235. $tool_name = get_lang('TermsAndConditions');
  236. Display :: display_header($tool_name);
  237. if (!empty($term_preview['content'])) {
  238. echo $term_preview['content'];
  239. } else {
  240. echo get_lang('ComingSoon');
  241. }
  242. Display :: display_footer();
  243. exit;
  244. }
  245. }
  246. }
  247. $tool_name = get_lang('Registration', null, (!empty($_POST['language'])?$_POST['language']:$_user['language']));
  248. if (api_get_setting('allow_terms_conditions') == 'true' && $user_already_registered_show_terms) {
  249. $tool_name = get_lang('TermsAndConditions');
  250. }
  251. $home = api_get_path(SYS_PATH).'home/';
  252. if (api_is_multiple_url_enabled()) {
  253. $access_url_id = api_get_current_access_url_id();
  254. if ($access_url_id != -1) {
  255. $url_info = api_get_access_url($access_url_id);
  256. $url = api_remove_trailing_slash(preg_replace('/https?:\/\//i', '', $url_info['url']));
  257. $clean_url = replace_dangerous_char($url);
  258. $clean_url = str_replace('/', '-', $clean_url);
  259. $clean_url .= '/';
  260. $home_old = api_get_path(SYS_PATH).'home/';
  261. $home = api_get_path(SYS_PATH).'home/'.$clean_url;
  262. }
  263. }
  264. if (file_exists($home.'register_top_'.$user_selected_language.'.html')) {
  265. $home_top_temp = @(string)file_get_contents($home.'register_top_'.$user_selected_language.'.html');
  266. $open = str_replace('{rel_path}', api_get_path(REL_PATH), $home_top_temp);
  267. $open = api_to_system_encoding($open, api_detect_encoding(strip_tags($open)));
  268. if (!empty($open)) {
  269. $content = '<div class="well_border">'.$open.'</div>';
  270. }
  271. }
  272. // Forbidden to self-register
  273. if ($isNotAllowedHere) {
  274. api_not_allowed(true, get_lang('RegistrationDisabled'));
  275. }
  276. if (api_get_setting('allow_registration') == 'approval') {
  277. $content .= Display::return_message(get_lang('YourAccountHasToBeApproved'));
  278. }
  279. //if openid was not found
  280. if (!empty($_GET['openid_msg']) && $_GET['openid_msg'] == 'idnotfound') {
  281. $content .= Display::return_message(get_lang('OpenIDCouldNotBeFoundPleaseRegister'));
  282. }
  283. }
  284. // Terms and conditions
  285. if (api_get_setting('allow_terms_conditions') == 'true') {
  286. $language = api_get_interface_language();
  287. $language = api_get_language_id($language);
  288. $term_preview = LegalManager::get_last_condition($language);
  289. if (!$term_preview) {
  290. //we load from the platform
  291. $language = api_get_setting('platformLanguage');
  292. $language = api_get_language_id($language);
  293. $term_preview = LegalManager::get_last_condition($language);
  294. //if is false we load from english
  295. if (!$term_preview) {
  296. $language = api_get_language_id('english'); //this must work
  297. $term_preview = LegalManager::get_last_condition($language);
  298. }
  299. }
  300. // Version and language
  301. $form->addElement('hidden', 'legal_accept_type', $term_preview['version'].':'.$term_preview['language_id']);
  302. $form->addElement('hidden', 'legal_info', $term_preview['legal_id'].':'.$term_preview['language_id']);
  303. if ($term_preview['type'] == 1) {
  304. $form->addElement(
  305. 'checkbox',
  306. 'legal_accept',
  307. null,
  308. get_lang('IHaveReadAndAgree').'&nbsp;<a href="inscription.php?legal" target="_blank">'.get_lang('TermsAndConditions').'</a>'
  309. );
  310. $form->addRule('legal_accept', get_lang('ThisFieldIsRequired'), 'required');
  311. } else {
  312. $preview = LegalManager::show_last_condition($term_preview);
  313. $form->addElement('label', null, $preview);
  314. }
  315. }
  316. $form->addButtonCreate(get_lang('RegisterUser'));
  317. if ($form->validate()) {
  318. $values = $form->getSubmitValues(1);
  319. //make *sure* the login isn't too long
  320. $values['username'] = api_substr($values['username'], 0, USERNAME_MAX_LENGTH);
  321. if (api_get_setting('allow_registration_as_teacher') == 'false') {
  322. $values['status'] = STUDENT;
  323. }
  324. // Added by Ivan Tcholakov, 06-MAR-2008.
  325. if (empty($values['official_code'])) {
  326. $values['official_code'] = api_strtoupper($values['username']);
  327. }
  328. if (api_get_setting('login_is_email') == 'true') {
  329. $values['username'] = $values['email'];
  330. }
  331. if ($user_already_registered_show_terms &&
  332. api_get_setting('allow_terms_conditions') == 'true'
  333. ) {
  334. $user_id = $_SESSION['term_and_condition']['user_id'];
  335. $is_admin = UserManager::is_admin($user_id);
  336. Session::write('is_platformAdmin', $is_admin);
  337. } else {
  338. // Moved here to include extra fields when creating a user. Formerly placed after user creation
  339. // Register extra fields
  340. $extras = array();
  341. foreach ($values as $key => $value) {
  342. if (substr($key, 0, 6) == 'extra_') {
  343. //an extra field
  344. $extras[substr($key, 6)] = $value;
  345. } elseif (strpos($key, 'remove_extra_') !== false) {
  346. $extra_value = Security::filter_filename(urldecode(key($value)));
  347. // To remove from user_field_value and folder
  348. UserManager::update_extra_field_value(
  349. $user_id,
  350. substr($key, 13),
  351. $extra_value
  352. );
  353. }
  354. }
  355. $status = isset($values['status']) ? $values['status'] : null;
  356. $phone = isset($values['phone']) ? $values['phone'] : null;
  357. // Creates a new user
  358. $user_id = UserManager::create_user(
  359. $values['firstname'],
  360. $values['lastname'],
  361. $status,
  362. $values['email'],
  363. $values['username'],
  364. $values['pass1'],
  365. $values['official_code'],
  366. $values['language'],
  367. $phone,
  368. null,
  369. PLATFORM_AUTH_SOURCE,
  370. null,
  371. 1,
  372. 0,
  373. $extras,
  374. null,
  375. true
  376. );
  377. //update the extra fields
  378. $count_extra_field = count($extras);
  379. if ($count_extra_field > 0) {
  380. foreach ($extras as $key => $value) {
  381. // For array $value -> if exists key 'tmp_name' then must not be empty
  382. // This avoid delete from user field value table when doesn't upload a file
  383. if (is_array($value)) {
  384. if (array_key_exists('tmp_name', $value) && empty($value['tmp_name'])) {
  385. //Nothing to do
  386. } else {
  387. if (array_key_exists('tmp_name', $value)) {
  388. $value['tmp_name'] = Security::filter_filename($value['tmp_name']);
  389. }
  390. if (array_key_exists('name', $value)) {
  391. $value['name'] = Security::filter_filename($value['name']);
  392. }
  393. UserManager::update_extra_field_value($user_id, $key, $value);
  394. }
  395. } else {
  396. UserManager::update_extra_field_value($user_id, $key, $value);
  397. }
  398. }
  399. }
  400. if ($user_id) {
  401. // Storing the extended profile
  402. $store_extended = false;
  403. $sql = "UPDATE ".Database::get_main_table(TABLE_MAIN_USER)." SET ";
  404. if (api_get_setting('extended_profile') == 'true' && api_get_setting('extendedprofile_registration', 'mycomptetences') == 'true') {
  405. $sql_set[] = "competences = '".Database::escape_string($values['competences'])."'";
  406. $store_extended = true;
  407. }
  408. if (api_get_setting('extended_profile') == 'true' && api_get_setting('extendedprofile_registration', 'mydiplomas') == 'true') {
  409. $sql_set[] = "diplomas = '".Database::escape_string($values['diplomas'])."'";
  410. $store_extended = true;
  411. }
  412. if (api_get_setting('extended_profile') == 'true' && api_get_setting('extendedprofile_registration', 'myteach') == 'true') {
  413. $sql_set[] = "teach = '".Database::escape_string($values['teach'])."'";
  414. $store_extended = true;
  415. }
  416. if (api_get_setting('extended_profile') == 'true' && api_get_setting('extendedprofile_registration', 'mypersonalopenarea') == 'true') {
  417. $sql_set[] = "openarea = '".Database::escape_string($values['openarea'])."'";
  418. $store_extended = true;
  419. }
  420. if ($store_extended) {
  421. $sql .= implode(',', $sql_set);
  422. $sql .= " WHERE user_id = ".intval($user_id)."";
  423. Database::query($sql);
  424. }
  425. // if there is a default duration of a valid account then we have to change the expiration_date accordingly
  426. if (api_get_setting('account_valid_duration') != '') {
  427. $sql = "UPDATE ".Database::get_main_table(TABLE_MAIN_USER)."
  428. SET expiration_date='registration_date+1'
  429. WHERE user_id='".$user_id."'";
  430. Database::query($sql);
  431. }
  432. $course_code_redirect = Session::read('course_redirect');
  433. // Saving user to course if it was set.
  434. if (!empty($course_code_redirect)) {
  435. $course_info = api_get_course_info($course_code_redirect);
  436. if (!empty($course_info)) {
  437. if (in_array(
  438. $course_info['visibility'],
  439. array(
  440. COURSE_VISIBILITY_OPEN_PLATFORM,
  441. COURSE_VISIBILITY_OPEN_WORLD
  442. )
  443. )
  444. ) {
  445. CourseManager::subscribe_user(
  446. $user_id,
  447. $course_info['code']
  448. );
  449. }
  450. }
  451. }
  452. /* If the account has to be approved then we set the account to inactive,
  453. sent a mail to the platform admin and exit the page.*/
  454. if (api_get_setting('allow_registration') == 'approval') {
  455. $TABLE_USER = Database::get_main_table(TABLE_MAIN_USER);
  456. // 1. set account inactive
  457. $sql = "UPDATE $TABLE_USER SET active='0' WHERE user_id = ".$user_id;
  458. Database::query($sql);
  459. // 2. Send mail to all platform admin
  460. $emailsubject = get_lang('ApprovalForNewAccount', null, $values['language']).': '.$values['username'];
  461. $emailbody = get_lang('ApprovalForNewAccount', null, $values['language'])."\n";
  462. $emailbody .= get_lang('UserName', null, $values['language']).': '.$values['username']."\n";
  463. if (api_is_western_name_order()) {
  464. $emailbody .= get_lang('FirstName', null, $values['language']).': '.$values['firstname']."\n";
  465. $emailbody .= get_lang('LastName', null, $values['language']).': '.$values['lastname']."\n";
  466. } else {
  467. $emailbody .= get_lang('LastName', null, $values['language']).': '.$values['lastname']."\n";
  468. $emailbody .= get_lang('FirstName', null, $values['language']).': '.$values['firstname']."\n";
  469. }
  470. $emailbody .= get_lang('Email', null, $values['language']).': '.$values['email']."\n";
  471. $emailbody .= get_lang('Status', null, $values['language']).': '.$values['status']."\n\n";
  472. $url_edit = Display::url(
  473. api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user_id,
  474. api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user_id
  475. );
  476. $emailbody .= get_lang('ManageUser', null, $values['language']).": $url_edit";
  477. $admins = UserManager::get_all_administrators();
  478. foreach ($admins as $admin_info) {
  479. MessageManager::send_message(
  480. $admin_info['user_id'],
  481. $emailsubject,
  482. $emailbody,
  483. null,
  484. null,
  485. null,
  486. null,
  487. null,
  488. null,
  489. $user_id
  490. );
  491. }
  492. // 3. exit the page
  493. unset($user_id);
  494. Display :: display_header($tool_name);
  495. echo Display::page_header($tool_name);
  496. echo $content;
  497. Display::display_footer();
  498. exit;
  499. }
  500. }
  501. }
  502. // Terms & Conditions
  503. if (api_get_setting('allow_terms_conditions') == 'true') {
  504. // Update the terms & conditions.
  505. if (isset($values['legal_accept_type'])) {
  506. $cond_array = explode(':', $values['legal_accept_type']);
  507. if (!empty($cond_array[0]) && !empty($cond_array[1])) {
  508. $time = time();
  509. $condition_to_save = intval($cond_array[0]).':'.intval($cond_array[1]).':'.$time;
  510. UserManager::update_extra_field_value($user_id, 'legal_accept', $condition_to_save);
  511. }
  512. }
  513. $values = api_get_user_info($user_id);
  514. }
  515. /* SESSION REGISTERING */
  516. /* @todo move this in a function */
  517. $_user['firstName'] = stripslashes($values['firstname']);
  518. $_user['lastName'] = stripslashes($values['lastname']);
  519. $_user['mail'] = $values['email'];
  520. $_user['language'] = $values['language'];
  521. $_user['user_id'] = $user_id;
  522. $is_allowedCreateCourse = isset($values['status']) && $values['status'] == 1;
  523. $usersCanCreateCourse = (api_get_setting('allow_users_to_create_courses') == 'true');
  524. Session::write('_user', $_user);
  525. Session::write('is_allowedCreateCourse', $is_allowedCreateCourse);
  526. // Stats
  527. Event::event_login();
  528. // last user login date is now
  529. $user_last_login_datetime = 0; // used as a unix timestamp it will correspond to : 1 1 1970
  530. Session::write('user_last_login_datetime', $user_last_login_datetime);
  531. $recipient_name = api_get_person_name($values['firstname'], $values['lastname']);
  532. $text_after_registration = '<p>'.get_lang('Dear', null, $_user['language']).' '.stripslashes(Security::remove_XSS($recipient_name)).',<br /><br />'.get_lang('PersonalSettings',null,$_user['language']).".</p>";
  533. $form_data = array(
  534. 'button' => Display::button('next', get_lang('Next', null, $_user['language']), array('class' => 'btn btn-primary btn-large')),
  535. 'message' => null,
  536. 'action' => api_get_path(WEB_PATH).'user_portal.php'
  537. );
  538. if (api_get_setting('allow_terms_conditions') == 'true' && $user_already_registered_show_terms) {
  539. $form_data['action'] = api_get_path(WEB_PATH).'user_portal.php';
  540. } else {
  541. if (!empty ($values['email'])) {
  542. $text_after_registration.= '<p>'.get_lang('MailHasBeenSent',null,$_user['language']).'.</p>';
  543. }
  544. if ($is_allowedCreateCourse) {
  545. if ($usersCanCreateCourse) {
  546. $form_data['message'] = '<p>'. get_lang('NowGoCreateYourCourse', null, $_user['language']). "</p>";
  547. }
  548. $form_data['action'] = '../create_course/add_course.php';
  549. if (api_get_setting('course_validation') == 'true') {
  550. $form_data['button'] = Display::button('next', get_lang('CreateCourseRequest', null, $_user['language']), array('class' => 'btn btn-primary btn-large'));
  551. } else {
  552. $form_data['button'] = Display::button('next', get_lang('CourseCreate', null, $_user['language']), array('class' => 'btn btn-primary btn-large'));
  553. $form_data['go_button'] = '&nbsp;&nbsp;<a href="'.api_get_path(WEB_PATH).'index.php'.'">'.Display::span(get_lang('Next', null, $_user['language']), array('class' => 'btn btn-primary btn-large')).'</a>';
  554. }
  555. } else {
  556. if (api_get_setting('allow_students_to_browse_courses') == 'true') {
  557. $form_data['action'] = 'courses.php?action=subscribe';
  558. $form_data['message'] = '<p>'. get_lang('NowGoChooseYourCourses', null, $_user['language']). ".</p>";
  559. } else {
  560. $form_data['action'] = api_get_path(WEB_PATH).'user_portal.php';
  561. }
  562. $form_data['button'] = Display::button('next', get_lang('Next', null, $_user['language']), array('class' => 'btn btn-primary btn-large'));
  563. }
  564. }
  565. /*
  566. * Direct course link see #5299
  567. *
  568. * You can send to your students an URL like this
  569. * http://chamilodev.beeznest.com/main/auth/inscription.php?c=ABC&e=3
  570. * Where "c" is the course code and "e" is the exercise Id, after a successful
  571. * registration the user will be sent to the course or exercise
  572. *
  573. */
  574. $course_code_redirect = Session::read('course_redirect');
  575. if (!empty($course_code_redirect)) {
  576. $course_info = api_get_course_info($course_code_redirect);
  577. if (!empty($course_info)) {
  578. if (in_array($course_info['visibility'], array(COURSE_VISIBILITY_OPEN_PLATFORM, COURSE_VISIBILITY_OPEN_WORLD))) {
  579. $user_id = api_get_user_id();
  580. if (CourseManager::is_user_subscribed_in_course($user_id, $course_info['code'])) {
  581. $form_data['action'] = $course_info['course_public_url'];
  582. $form_data['message'] = sprintf(get_lang('YouHaveBeenRegisteredToCourseX'), $course_info['title']);
  583. $form_data['button'] = Display::button(
  584. 'next',
  585. get_lang('GoToCourse', null, $_user['language']),
  586. array('class' => 'btn btn-primary btn-large')
  587. );
  588. $exercise_redirect = intval(Session::read('exercise_redirect'));
  589. // Specifiy course ID as the current context does not
  590. // hold a global $_course array
  591. $objExercise = new Exercise($course_info['real_id']);
  592. $result = $objExercise->read($exercise_redirect);
  593. if (!empty($exercise_redirect) && !empty($result)) {
  594. $form_data['action'] = api_get_path(WEB_CODE_PATH).'exercice/overview.php?exerciseId='.$exercise_redirect.'&cidReq='.$course_info['code'];
  595. $form_data['message'] .= '<br />'.get_lang('YouCanAccessTheExercise');
  596. $form_data['button'] = Display::button(
  597. 'next',
  598. get_lang('Go', null, $_user['language']),
  599. array('class' => 'btn btn-primary btn-large')
  600. );
  601. }
  602. if (!empty($form_data['action'])) {
  603. header('Location: '.$form_data['action']);
  604. exit;
  605. }
  606. }
  607. }
  608. }
  609. }
  610. $form_register = new FormValidator('form_register', 'post', $form_data['action']);
  611. if (!empty($form_data['message'])) {
  612. $form_register->addElement('html', $form_data['message'].'<br /><br />');
  613. }
  614. if ($usersCanCreateCourse) {
  615. $form_register->addElement('html', $form_data['button']);
  616. } else {
  617. $form_register->addElement('html', $form_data['go_button']);
  618. }
  619. $text_after_registration .= $form_register->return_form();
  620. // Just in case
  621. Session::erase('course_redirect');
  622. Session::erase('exercise_redirect');
  623. if (CustomPages::enabled()) {
  624. CustomPages::display(
  625. CustomPages::REGISTRATION_FEEDBACK,
  626. array('info' => $text_after_registration)
  627. );
  628. } else {
  629. Display :: display_header($tool_name);
  630. echo Display::page_header($tool_name);
  631. echo $content;
  632. echo $text_after_registration;
  633. }
  634. } else {
  635. // Custom pages
  636. if (CustomPages::enabled()) {
  637. CustomPages::display(CustomPages::REGISTRATION, array('form' => $form));
  638. } else {
  639. Display :: display_header($tool_name);
  640. echo Display::page_header($tool_name);
  641. echo $content;
  642. $form->display();
  643. }
  644. }
  645. Display :: display_footer();