inscription.php 31 KB

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