inscription.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * This script displays a form for registering new users.
  6. *
  7. * @package chamilo.auth
  8. */
  9. //quick hack to adapt the registration form result to the selected registration language
  10. if (!empty($_POST['language'])) {
  11. $_GET['language'] = $_POST['language'];
  12. }
  13. require_once __DIR__.'/../inc/global.inc.php';
  14. $hideHeaders = isset($_GET['hide_headers']);
  15. $allowedFields = [
  16. 'official_code',
  17. 'phone',
  18. 'status',
  19. 'language',
  20. 'extra_fields',
  21. 'address',
  22. ];
  23. $allowedFieldsConfiguration = api_get_configuration_value('allow_fields_inscription');
  24. if ($allowedFieldsConfiguration !== false) {
  25. $allowedFields = isset($allowedFieldsConfiguration['fields']) ? $allowedFieldsConfiguration['fields'] : [];
  26. $allowedFields['extra_fields'] = isset($allowedFieldsConfiguration['extra_fields']) ? $allowedFieldsConfiguration['extra_fields'] : [];
  27. }
  28. $gMapsPlugin = GoogleMapsPlugin::create();
  29. if ($gMapsPlugin->get('enable_api') === 'true') {
  30. $key = $gMapsPlugin->get('api_key');
  31. $htmlHeadXtra[] = '<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=true&key='.$key.'" ></script>';
  32. }
  33. $htmlHeadXtra[] = api_get_password_checker_js('#username', '#pass1');
  34. // User is not allowed if Terms and Conditions are disabled and
  35. // registration is disabled too.
  36. $isNotAllowedHere = api_get_setting('allow_terms_conditions') === 'false' && api_get_setting('allow_registration') === 'false';
  37. if ($isNotAllowedHere) {
  38. api_not_allowed(true, get_lang('RegistrationDisabled'));
  39. }
  40. if (!empty($_SESSION['user_language_choice'])) {
  41. $user_selected_language = $_SESSION['user_language_choice'];
  42. } elseif (!empty($_SESSION['_user']['language'])) {
  43. $user_selected_language = $_SESSION['_user']['language'];
  44. } else {
  45. $user_selected_language = api_get_setting('platformLanguage');
  46. }
  47. $form = new FormValidator('registration');
  48. $user_already_registered_show_terms = false;
  49. if (api_get_setting('allow_terms_conditions') == 'true') {
  50. $user_already_registered_show_terms = isset($_SESSION['term_and_condition']['user_id']);
  51. }
  52. $sessionPremiumChecker = Session::read('SessionIsPremium');
  53. $sessionId = Session::read('sessionId');
  54. // Direct Link Session Subscription feature #12220
  55. $sessionRedirect = isset($_REQUEST['s']) && !empty($_REQUEST['s']) ? $_REQUEST['s'] : null;
  56. $onlyOneCourseSessionRedirect = isset($_REQUEST['cr']) && !empty($_REQUEST['cr']) ? $_REQUEST['cr'] : null;
  57. if (api_get_configuration_value('allow_redirect_to_session_after_inscription_about')) {
  58. if (!empty($sessionRedirect)) {
  59. Session::write('session_redirect', $sessionRedirect);
  60. Session::write('only_one_course_session_redirect', $onlyOneCourseSessionRedirect);
  61. }
  62. }
  63. // Direct Link Subscription feature #5299
  64. $course_code_redirect = isset($_REQUEST['c']) && !empty($_REQUEST['c']) ? $_REQUEST['c'] : null;
  65. $exercise_redirect = isset($_REQUEST['e']) && !empty($_REQUEST['e']) ? $_REQUEST['e'] : null;
  66. if (!empty($course_code_redirect)) {
  67. Session::write('course_redirect', $course_code_redirect);
  68. Session::write('exercise_redirect', $exercise_redirect);
  69. }
  70. if ($user_already_registered_show_terms === false && api_get_setting('allow_registration') === 'true') {
  71. // STUDENT/TEACHER
  72. if (api_get_setting('allow_registration_as_teacher') != 'false') {
  73. if (in_array('status', $allowedFields)) {
  74. $form->addRadio(
  75. 'status',
  76. get_lang('RegistrationRoleWhatDoYouWantToDo'),
  77. [
  78. STUDENT => '<p class="caption">'.get_lang('RegistrationRoleFollowCourses').'</p>',
  79. COURSEMANAGER => '<p class="caption">'.get_lang('RegistrationRoleTeachCourses').'</p>',
  80. ],
  81. ['class' => 'register-profile']
  82. );
  83. $form->addRule('status', get_lang('ThisFieldIsRequired'), 'required');
  84. }
  85. }
  86. if (api_is_western_name_order()) {
  87. // FIRST NAME and LAST NAME
  88. $form->addElement('text', 'firstname', get_lang('FirstName'), ['size' => 40]);
  89. $form->addElement('text', 'lastname', get_lang('LastName'), ['size' => 40]);
  90. } else {
  91. // LAST NAME and FIRST NAME
  92. $form->addElement('text', 'lastname', get_lang('LastName'), ['size' => 40]);
  93. $form->addElement('text', 'firstname', get_lang('FirstName'), ['size' => 40]);
  94. }
  95. $form->applyFilter(['lastname', 'firstname'], 'trim');
  96. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  97. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  98. // EMAIL
  99. $form->addElement('text', 'email', get_lang('Email'), ['size' => 40]);
  100. if (api_get_setting('registration', 'email') === 'true') {
  101. $form->addRule('email', get_lang('ThisFieldIsRequired'), 'required');
  102. }
  103. if (api_get_setting('login_is_email') === 'true') {
  104. $form->applyFilter('email', 'trim');
  105. if (api_get_setting('registration', 'email') != 'true') {
  106. $form->addRule('email', get_lang('ThisFieldIsRequired'), 'required');
  107. }
  108. $form->addRule(
  109. 'email',
  110. sprintf(
  111. get_lang('UsernameMaxXCharacters'),
  112. (string) USERNAME_MAX_LENGTH
  113. ),
  114. 'maxlength',
  115. USERNAME_MAX_LENGTH
  116. );
  117. $form->addRule('email', get_lang('UserTaken'), 'username_available');
  118. }
  119. $form->addRule('email', get_lang('EmailWrong'), 'email');
  120. if (api_get_setting('openid_authentication') === 'true') {
  121. $form->addElement('text', 'openid', get_lang('OpenIDURL'), ['size' => 40]);
  122. }
  123. // USERNAME
  124. if (api_get_setting('login_is_email') != 'true') {
  125. $form->addText(
  126. 'username',
  127. get_lang('UserName'),
  128. true,
  129. [
  130. 'id' => 'username',
  131. 'size' => USERNAME_MAX_LENGTH,
  132. 'autocomplete' => 'off',
  133. ]
  134. );
  135. $form->applyFilter('username', 'trim');
  136. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  137. $form->addRule(
  138. 'username',
  139. sprintf(
  140. get_lang('UsernameMaxXCharacters'),
  141. (string) USERNAME_MAX_LENGTH
  142. ),
  143. 'maxlength',
  144. USERNAME_MAX_LENGTH
  145. );
  146. $form->addRule('username', get_lang('UsernameWrong'), 'username');
  147. $form->addRule('username', get_lang('UserTaken'), 'username_available');
  148. }
  149. // PASSWORD
  150. $form->addElement(
  151. 'password',
  152. 'pass1',
  153. get_lang('Pass'),
  154. ['id' => 'pass1', 'size' => 20, 'autocomplete' => 'off']
  155. );
  156. $form->addElement(
  157. 'password',
  158. 'pass2',
  159. get_lang('Confirmation'),
  160. ['id' => 'pass2', 'size' => 20, 'autocomplete' => 'off']
  161. );
  162. $form->addRule('pass1', get_lang('ThisFieldIsRequired'), 'required');
  163. $form->addRule('pass2', get_lang('ThisFieldIsRequired'), 'required');
  164. $form->addRule(['pass1', 'pass2'], get_lang('PassTwo'), 'compare');
  165. $form->addPasswordRule('pass1');
  166. // PHONE
  167. if (in_array('phone', $allowedFields)) {
  168. $form->addElement(
  169. 'text',
  170. 'phone',
  171. get_lang('Phone'),
  172. ['size' => 20]
  173. );
  174. if (api_get_setting('registration', 'phone') == 'true') {
  175. $form->addRule(
  176. 'phone',
  177. get_lang('ThisFieldIsRequired'),
  178. 'required'
  179. );
  180. }
  181. }
  182. // Language
  183. if (in_array('language', $allowedFields)) {
  184. if (api_get_setting('registration', 'language') == 'true') {
  185. $form->addSelectLanguage(
  186. 'language',
  187. get_lang('Language')
  188. );
  189. }
  190. }
  191. // OFFICIAL CODE
  192. if (CONFVAL_ASK_FOR_OFFICIAL_CODE) {
  193. if (in_array('official_code', $allowedFields)) {
  194. $form->addElement(
  195. 'text',
  196. 'official_code',
  197. get_lang('OfficialCode'),
  198. ['size' => 40]
  199. );
  200. if (api_get_setting('registration', 'officialcode') == 'true') {
  201. $form->addRule(
  202. 'official_code',
  203. get_lang('ThisFieldIsRequired'),
  204. 'required'
  205. );
  206. }
  207. }
  208. }
  209. // EXTENDED FIELDS
  210. if (api_get_setting('extended_profile') == 'true' &&
  211. api_get_setting('extendedprofile_registration', 'mycomptetences') == 'true'
  212. ) {
  213. $form->addHtmlEditor(
  214. 'competences',
  215. get_lang('MyCompetences'),
  216. false,
  217. false,
  218. ['ToolbarSet' => 'register', 'Width' => '100%', 'Height' => '130']
  219. );
  220. }
  221. if (api_get_setting('extended_profile') == 'true' &&
  222. api_get_setting('extendedprofile_registration', 'mydiplomas') == 'true'
  223. ) {
  224. $form->addHtmlEditor(
  225. 'diplomas',
  226. get_lang('MyDiplomas'),
  227. false,
  228. false,
  229. ['ToolbarSet' => 'register', 'Width' => '100%', 'Height' => '130']
  230. );
  231. }
  232. if (api_get_setting('extended_profile') == 'true' &&
  233. api_get_setting('extendedprofile_registration', 'myteach') == 'true'
  234. ) {
  235. $form->addHtmlEditor(
  236. 'teach',
  237. get_lang('MyTeach'),
  238. false,
  239. false,
  240. ['ToolbarSet' => 'register', 'Width' => '100%', 'Height' => '130']
  241. );
  242. }
  243. if (api_get_setting('extended_profile') == 'true' &&
  244. api_get_setting('extendedprofile_registration', 'mypersonalopenarea') == 'true'
  245. ) {
  246. $form->addHtmlEditor(
  247. 'openarea',
  248. get_lang('MyPersonalOpenArea'),
  249. false,
  250. false,
  251. ['ToolbarSet' => 'register', 'Width' => '100%', 'Height' => '130']
  252. );
  253. }
  254. if (api_get_setting('extended_profile') === 'true') {
  255. if (api_get_setting('extendedprofile_registration', 'mycomptetences') === 'true' &&
  256. api_get_setting('extendedprofile_registrationrequired', 'mycomptetences') === 'true'
  257. ) {
  258. $form->addRule('competences', get_lang('ThisFieldIsRequired'), 'required');
  259. }
  260. if (api_get_setting('extendedprofile_registration', 'mydiplomas') === 'true' &&
  261. api_get_setting('extendedprofile_registrationrequired', 'mydiplomas') === 'true'
  262. ) {
  263. $form->addRule('diplomas', get_lang('ThisFieldIsRequired'), 'required');
  264. }
  265. if (api_get_setting('extendedprofile_registration', 'myteach') === 'true' &&
  266. api_get_setting('extendedprofile_registrationrequired', 'myteach') === 'true'
  267. ) {
  268. $form->addRule('teach', get_lang('ThisFieldIsRequired'), 'required');
  269. }
  270. if (api_get_setting('extendedprofile_registration', 'mypersonalopenarea') === 'true' &&
  271. api_get_setting('extendedprofile_registrationrequired', 'mypersonalopenarea') === 'true'
  272. ) {
  273. $form->addRule('openarea', get_lang('ThisFieldIsRequired'), 'required');
  274. }
  275. }
  276. // EXTRA FIELDS
  277. if (array_key_exists('extra_fields', $allowedFields) ||
  278. in_array('extra_fields', $allowedFields)
  279. ) {
  280. $extraField = new ExtraField('user');
  281. $extraFieldList = isset($allowedFields['extra_fields']) && is_array(
  282. $allowedFields['extra_fields']
  283. ) ? $allowedFields['extra_fields'] : [];
  284. $returnParams = $extraField->addElements(
  285. $form,
  286. 0,
  287. [],
  288. false,
  289. false,
  290. $extraFieldList
  291. );
  292. }
  293. // CAPTCHA
  294. $captcha = api_get_setting('allow_captcha');
  295. $allowCaptcha = $captcha === 'true';
  296. if ($allowCaptcha) {
  297. $ajax = api_get_path(WEB_AJAX_PATH).'form.ajax.php?a=get_captcha';
  298. $options = [
  299. 'width' => 220,
  300. 'height' => 90,
  301. 'callback' => $ajax.'&var='.basename(__FILE__, '.php'),
  302. 'sessionVar' => basename(__FILE__, '.php'),
  303. 'imageOptions' => [
  304. 'font_size' => 20,
  305. 'font_path' => api_get_path(SYS_FONTS_PATH).'opensans/',
  306. 'font_file' => 'OpenSans-Regular.ttf',
  307. //'output' => 'gif'
  308. ],
  309. ];
  310. $captcha_question = $form->addElement(
  311. 'CAPTCHA_Image',
  312. 'captcha_question',
  313. '',
  314. $options
  315. );
  316. $form->addElement('static', null, null, get_lang('ClickOnTheImageForANewOne'));
  317. $form->addElement(
  318. 'text',
  319. 'captcha',
  320. get_lang('EnterTheLettersYouSee'),
  321. ['size' => 40]
  322. );
  323. $form->addRule(
  324. 'captcha',
  325. get_lang('EnterTheCharactersYouReadInTheImage'),
  326. 'required',
  327. null,
  328. 'client'
  329. );
  330. $form->addRule(
  331. 'captcha',
  332. get_lang('TheTextYouEnteredDoesNotMatchThePicture'),
  333. 'CAPTCHA',
  334. $captcha_question
  335. );
  336. }
  337. }
  338. if (isset($_SESSION['user_language_choice']) && $_SESSION['user_language_choice'] != '') {
  339. $defaults['language'] = $_SESSION['user_language_choice'];
  340. } else {
  341. $defaults['language'] = api_get_setting('platformLanguage');
  342. }
  343. if (!empty($_GET['username'])) {
  344. $defaults['username'] = Security::remove_XSS($_GET['username']);
  345. }
  346. if (!empty($_GET['email'])) {
  347. $defaults['email'] = Security::remove_XSS($_GET['email']);
  348. }
  349. if (!empty($_GET['phone'])) {
  350. $defaults['phone'] = Security::remove_XSS($_GET['phone']);
  351. }
  352. if (api_get_setting('openid_authentication') === 'true' && !empty($_GET['openid'])) {
  353. $defaults['openid'] = Security::remove_XSS($_GET['openid']);
  354. }
  355. $defaults['status'] = STUDENT;
  356. $defaults['extra_mail_notify_invitation'] = 1;
  357. $defaults['extra_mail_notify_message'] = 1;
  358. $defaults['extra_mail_notify_group_message'] = 1;
  359. $form->setDefaults($defaults);
  360. $content = null;
  361. $tool_name = get_lang('Registration');
  362. if (!CustomPages::enabled()) {
  363. // Load terms & conditions from the current lang
  364. if (api_get_setting('allow_terms_conditions') === 'true') {
  365. $get = array_keys($_GET);
  366. if (isset($get)) {
  367. if (isset($get[0]) && $get[0] == 'legal') {
  368. $language = api_get_interface_language();
  369. $language = api_get_language_id($language);
  370. $term_preview = LegalManager::get_last_condition($language);
  371. if (!$term_preview) {
  372. //look for the default language
  373. $language = api_get_setting('platformLanguage');
  374. $language = api_get_language_id($language);
  375. $term_preview = LegalManager::get_last_condition($language);
  376. }
  377. $tool_name = get_lang('TermsAndConditions');
  378. Display::display_header($tool_name);
  379. if (!empty($term_preview['content'])) {
  380. echo $term_preview['content'];
  381. } else {
  382. echo get_lang('ComingSoon');
  383. }
  384. Display::display_footer();
  385. exit;
  386. }
  387. }
  388. }
  389. if (api_get_setting('allow_terms_conditions') === 'true' && $user_already_registered_show_terms) {
  390. $tool_name = get_lang('TermsAndConditions');
  391. }
  392. }
  393. $home = api_get_path(SYS_APP_PATH).'home/';
  394. if (api_is_multiple_url_enabled()) {
  395. $access_url_id = api_get_current_access_url_id();
  396. if ($access_url_id != -1) {
  397. $url_info = api_get_access_url($access_url_id);
  398. $url = api_remove_trailing_slash(preg_replace('/https?:\/\//i', '', $url_info['url']));
  399. $clean_url = api_replace_dangerous_char($url);
  400. $clean_url = str_replace('/', '-', $clean_url);
  401. $clean_url .= '/';
  402. $home_old = api_get_path(SYS_APP_PATH).'home/';
  403. $home = api_get_path(SYS_APP_PATH).'home/'.$clean_url;
  404. }
  405. }
  406. if (file_exists($home.'register_top_'.$user_selected_language.'.html')) {
  407. $home_top_temp = @(string) file_get_contents($home.'register_top_'.$user_selected_language.'.html');
  408. $open = str_replace('{rel_path}', api_get_path(REL_PATH), $home_top_temp);
  409. $open = api_to_system_encoding($open, api_detect_encoding(strip_tags($open)));
  410. if (!empty($open)) {
  411. $content = '<div class="well well-sm help-registration">'.$open.'</div>';
  412. }
  413. }
  414. // Forbidden to self-register
  415. if ($isNotAllowedHere) {
  416. api_not_allowed(true, get_lang('RegistrationDisabled'));
  417. }
  418. if (api_get_setting('allow_registration') === 'approval') {
  419. $content .= Display::return_message(get_lang('YourAccountHasToBeApproved'));
  420. }
  421. //if openid was not found
  422. if (!empty($_GET['openid_msg']) && $_GET['openid_msg'] == 'idnotfound') {
  423. $content .= Display::return_message(get_lang('OpenIDCouldNotBeFoundPleaseRegister'));
  424. }
  425. $showTerms = false;
  426. // Terms and conditions
  427. if (api_get_setting('allow_terms_conditions') == 'true' && $user_already_registered_show_terms) {
  428. if (!api_is_platform_admin()) {
  429. if (api_get_setting('show_terms_if_profile_completed') === 'true') {
  430. $userInfo = api_get_user_info();
  431. if ($userInfo && $userInfo['status'] != ANONYMOUS) {
  432. if ((int) $userInfo['profile_completed'] !== 1) {
  433. api_not_allowed(true);
  434. }
  435. }
  436. }
  437. }
  438. $language = api_get_interface_language();
  439. $language = api_get_language_id($language);
  440. $term_preview = LegalManager::get_last_condition($language);
  441. if (!$term_preview) {
  442. //we load from the platform
  443. $language = api_get_setting('platformLanguage');
  444. $language = api_get_language_id($language);
  445. $term_preview = LegalManager::get_last_condition($language);
  446. //if is false we load from english
  447. if (!$term_preview) {
  448. $language = api_get_language_id('english'); //this must work
  449. $term_preview = LegalManager::get_last_condition($language);
  450. }
  451. }
  452. // Version and language
  453. $form->addHidden('legal_accept_type', $term_preview['version'].':'.$term_preview['language_id']);
  454. $form->addHidden('legal_info', $term_preview['id'].':'.$term_preview['language_id']);
  455. if ($term_preview['type'] == 1) {
  456. $form->addElement(
  457. 'checkbox',
  458. 'legal_accept',
  459. null,
  460. get_lang('IHaveReadAndAgree').'&nbsp;<a href="inscription.php?legal" target="_blank">'.get_lang(
  461. 'TermsAndConditions'
  462. ).'</a>'
  463. );
  464. $form->addRule('legal_accept', get_lang('ThisFieldIsRequired'), 'required');
  465. } else {
  466. $preview = LegalManager::show_last_condition($term_preview);
  467. $form->addElement('label', null, $preview);
  468. }
  469. $showTerms = true;
  470. }
  471. $allowDoubleValidation = api_get_configuration_value('allow_double_validation_in_registration');
  472. $formContainsSendButton = false;
  473. if ($allowDoubleValidation && $showTerms == false) {
  474. $htmlHeadXtra[] = '<script>
  475. $(document).ready(function() {
  476. $("#pre_validation").click(function() {
  477. $(this).hide();
  478. $("#final_button").show();
  479. });
  480. });
  481. </script>';
  482. $form->addLabel(
  483. null,
  484. Display::url(
  485. get_lang('Ok'),
  486. 'javascript:void',
  487. ['class' => 'btn btn-default', 'id' => 'pre_validation']
  488. )
  489. );
  490. $form->addHtml('<div id="final_button" style="display: none">');
  491. $form->addLabel(
  492. null,
  493. Display::return_message(get_lang('DoubleValidationMessage'), 'info', false)
  494. );
  495. $form->addButton('submit', get_lang('RegisterUser'), '', 'primary');
  496. $form->addHtml('</div>');
  497. $formContainsSendButton = true;
  498. } else {
  499. if (api_get_setting('allow_registration') === 'true' || $user_already_registered_show_terms || $showTerms) {
  500. $form->addButtonNext(get_lang('RegisterUser'));
  501. $formContainsSendButton = true;
  502. }
  503. }
  504. // Blocks page because there's any action to do.
  505. if (!$formContainsSendButton) {
  506. api_not_allowed(true);
  507. }
  508. $course_code_redirect = Session::read('course_redirect');
  509. $sessionToRedirect = Session::read('session_redirect');
  510. if ($form->validate()) {
  511. $values = $form->getSubmitValues(1);
  512. // Make *sure* the login isn't too long
  513. if (isset($values['username'])) {
  514. $values['username'] = api_substr($values['username'], 0, USERNAME_MAX_LENGTH);
  515. }
  516. if (api_get_setting('allow_registration_as_teacher') === 'false') {
  517. $values['status'] = STUDENT;
  518. }
  519. if (empty($values['official_code']) && !empty($values['username'])) {
  520. $values['official_code'] = api_strtoupper($values['username']);
  521. }
  522. if (api_get_setting('login_is_email') == 'true') {
  523. $values['username'] = $values['email'];
  524. }
  525. if ($user_already_registered_show_terms &&
  526. api_get_setting('allow_terms_conditions') === 'true'
  527. ) {
  528. $user_id = $_SESSION['term_and_condition']['user_id'];
  529. $is_admin = UserManager::is_admin($user_id);
  530. Session::write('is_platformAdmin', $is_admin);
  531. } else {
  532. // Moved here to include extra fields when creating a user. Formerly placed after user creation
  533. // Register extra fields
  534. $extras = [];
  535. foreach ($values as $key => $value) {
  536. if (substr($key, 0, 6) == 'extra_') {
  537. //an extra field
  538. $extras[substr($key, 6)] = $value;
  539. } elseif (strpos($key, 'remove_extra_') !== false) {
  540. $extra_value = Security::filter_filename(urldecode(key($value)));
  541. // To remove from user_field_value and folder
  542. UserManager::update_extra_field_value(
  543. $user_id,
  544. substr($key, 13),
  545. $extra_value
  546. );
  547. }
  548. }
  549. $status = isset($values['status']) ? $values['status'] : STUDENT;
  550. $phone = isset($values['phone']) ? $values['phone'] : null;
  551. $values['language'] = isset($values['language']) ? $values['language'] : api_get_interface_language();
  552. $values['address'] = isset($values['address']) ? $values['address'] : '';
  553. // Creates a new user
  554. $user_id = UserManager::create_user(
  555. $values['firstname'],
  556. $values['lastname'],
  557. $status,
  558. $values['email'],
  559. $values['username'],
  560. $values['pass1'],
  561. $values['official_code'],
  562. $values['language'],
  563. $phone,
  564. null,
  565. PLATFORM_AUTH_SOURCE,
  566. null,
  567. 1,
  568. 0,
  569. $extras,
  570. null,
  571. true,
  572. false,
  573. $values['address'],
  574. false,
  575. $form
  576. );
  577. // Update the extra fields
  578. $count_extra_field = count($extras);
  579. if ($count_extra_field > 0 && is_integer($user_id)) {
  580. foreach ($extras as $key => $value) {
  581. // For array $value -> if exists key 'tmp_name' then must not be empty
  582. // This avoid delete from user field value table when doesn't upload a file
  583. if (is_array($value)) {
  584. if (array_key_exists('tmp_name', $value) && empty($value['tmp_name'])) {
  585. //Nothing to do
  586. } else {
  587. if (array_key_exists('tmp_name', $value)) {
  588. $value['tmp_name'] = Security::filter_filename($value['tmp_name']);
  589. }
  590. if (array_key_exists('name', $value)) {
  591. $value['name'] = Security::filter_filename($value['name']);
  592. }
  593. UserManager::update_extra_field_value($user_id, $key, $value);
  594. }
  595. } else {
  596. UserManager::update_extra_field_value($user_id, $key, $value);
  597. }
  598. }
  599. }
  600. if ($user_id) {
  601. // Storing the extended profile
  602. $store_extended = false;
  603. $sql = "UPDATE ".Database::get_main_table(TABLE_MAIN_USER)." SET ";
  604. if (api_get_setting('extended_profile') == 'true' &&
  605. api_get_setting('extendedprofile_registration', 'mycomptetences') == 'true'
  606. ) {
  607. $sql_set[] = "competences = '".Database::escape_string($values['competences'])."'";
  608. $store_extended = true;
  609. }
  610. if (api_get_setting('extended_profile') == 'true' &&
  611. api_get_setting('extendedprofile_registration', 'mydiplomas') == 'true'
  612. ) {
  613. $sql_set[] = "diplomas = '".Database::escape_string($values['diplomas'])."'";
  614. $store_extended = true;
  615. }
  616. if (api_get_setting('extended_profile') == 'true' &&
  617. api_get_setting('extendedprofile_registration', 'myteach') == 'true'
  618. ) {
  619. $sql_set[] = "teach = '".Database::escape_string($values['teach'])."'";
  620. $store_extended = true;
  621. }
  622. if (api_get_setting('extended_profile') == 'true' &&
  623. api_get_setting('extendedprofile_registration', 'mypersonalopenarea') == 'true'
  624. ) {
  625. $sql_set[] = "openarea = '".Database::escape_string($values['openarea'])."'";
  626. $store_extended = true;
  627. }
  628. if ($store_extended) {
  629. $sql .= implode(',', $sql_set);
  630. $sql .= " WHERE user_id = ".intval($user_id)."";
  631. Database::query($sql);
  632. }
  633. // Saving user to Session if it was set
  634. if (!empty($sessionToRedirect) && !$sessionPremiumChecker) {
  635. $sessionInfo = api_get_session_info($sessionToRedirect);
  636. if (!empty($sessionInfo)) {
  637. SessionManager::subscribeUsersToSession(
  638. $sessionToRedirect,
  639. [$user_id],
  640. SESSION_VISIBLE_READ_ONLY,
  641. false
  642. );
  643. }
  644. }
  645. // Saving user to course if it was set.
  646. if (!empty($course_code_redirect)) {
  647. $course_info = api_get_course_info($course_code_redirect);
  648. if (!empty($course_info)) {
  649. if (in_array(
  650. $course_info['visibility'],
  651. [
  652. COURSE_VISIBILITY_OPEN_PLATFORM,
  653. COURSE_VISIBILITY_OPEN_WORLD,
  654. ]
  655. )
  656. ) {
  657. CourseManager::subscribe_user(
  658. $user_id,
  659. $course_info['code']
  660. );
  661. }
  662. }
  663. }
  664. /* If the account has to be approved then we set the account to inactive,
  665. sent a mail to the platform admin and exit the page.*/
  666. if (api_get_setting('allow_registration') === 'approval') {
  667. // 1. Send mail to all platform admin
  668. $emailsubject = get_lang('ApprovalForNewAccount').': '.$values['username'];
  669. $emailbody = get_lang('ApprovalForNewAccount')."\n";
  670. $emailbody .= get_lang('UserName').': '.$values['username']."\n";
  671. if (api_is_western_name_order()) {
  672. $emailbody .= get_lang('FirstName').': '.$values['firstname']."\n";
  673. $emailbody .= get_lang('LastName').': '.$values['lastname']."\n";
  674. } else {
  675. $emailbody .= get_lang('LastName').': '.$values['lastname']."\n";
  676. $emailbody .= get_lang('FirstName').': '.$values['firstname']."\n";
  677. }
  678. $emailbody .= get_lang('Email').': '.$values['email']."\n";
  679. $emailbody .= get_lang('Status').': '.$values['status']."\n\n";
  680. $url_edit = Display::url(
  681. api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user_id,
  682. api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user_id
  683. );
  684. $emailbody .= get_lang('ManageUser').": $url_edit";
  685. $admins = UserManager::get_all_administrators();
  686. foreach ($admins as $admin_info) {
  687. MessageManager::send_message(
  688. $admin_info['user_id'],
  689. $emailsubject,
  690. $emailbody,
  691. [],
  692. [],
  693. null,
  694. null,
  695. null,
  696. null,
  697. $user_id
  698. );
  699. }
  700. // 2. set account inactive
  701. Usermanager::disable($user_id);
  702. // 3. exit the page
  703. unset($user_id);
  704. Display::display_header($tool_name);
  705. echo Display::page_header($tool_name);
  706. echo $content;
  707. Display::display_footer();
  708. exit;
  709. } elseif (api_get_setting('allow_registration') === 'confirmation') {
  710. // 1. Send mail to the user
  711. $thisUser = api_get_user_entity($user_id);
  712. UserManager::sendUserConfirmationMail($thisUser);
  713. // 2. set account inactive
  714. Usermanager::disable($user_id);
  715. // 3. exit the page
  716. unset($user_id);
  717. Display::addFlash(
  718. Display::return_message(
  719. get_lang('YouNeedConfirmYourAccountViaEmailToAccessThePlatform'),
  720. 'warning'
  721. )
  722. );
  723. Display::display_header($tool_name);
  724. //echo $content;
  725. Display::display_footer();
  726. exit;
  727. }
  728. }
  729. }
  730. // Terms & Conditions
  731. if (api_get_setting('allow_terms_conditions') === 'true') {
  732. // Update the terms & conditions.
  733. if (isset($values['legal_accept_type'])) {
  734. $cond_array = explode(':', $values['legal_accept_type']);
  735. if (!empty($cond_array[0]) && !empty($cond_array[1])) {
  736. $time = time();
  737. $condition_to_save = intval($cond_array[0]).':'.intval($cond_array[1]).':'.$time;
  738. UserManager::update_extra_field_value(
  739. $user_id,
  740. 'legal_accept',
  741. $condition_to_save
  742. );
  743. $bossList = UserManager::getStudentBossList($user_id);
  744. if (!empty($bossList)) {
  745. $bossList = array_column($bossList, 'boss_id');
  746. $currentUserInfo = api_get_user_info($user_id);
  747. foreach ($bossList as $bossId) {
  748. $subjectEmail = sprintf(
  749. get_lang('UserXSignedTheAgreement'),
  750. $currentUserInfo['complete_name']
  751. );
  752. $contentEmail = sprintf(
  753. get_lang('UserXSignedTheAgreementTheY'),
  754. $currentUserInfo['complete_name'],
  755. api_get_local_time($time)
  756. );
  757. MessageManager::send_message_simple(
  758. $bossId,
  759. $subjectEmail,
  760. $contentEmail,
  761. $user_id
  762. );
  763. }
  764. }
  765. }
  766. }
  767. $values = api_get_user_info($user_id);
  768. }
  769. /* SESSION REGISTERING */
  770. /* @todo move this in a function */
  771. $_user['firstName'] = stripslashes($values['firstname']);
  772. $_user['lastName'] = stripslashes($values['lastname']);
  773. $_user['mail'] = $values['email'];
  774. $_user['language'] = $values['language'];
  775. $_user['user_id'] = $user_id;
  776. Session::write('_user', $_user);
  777. $is_allowedCreateCourse = isset($values['status']) && $values['status'] == 1;
  778. $usersCanCreateCourse = api_is_allowed_to_create_course();
  779. Session::write('is_allowedCreateCourse', $is_allowedCreateCourse);
  780. // Stats
  781. Event::eventLogin($user_id);
  782. // last user login date is now
  783. $user_last_login_datetime = 0; // used as a unix timestamp it will correspond to : 1 1 1970
  784. Session::write('user_last_login_datetime', $user_last_login_datetime);
  785. $recipient_name = api_get_person_name($values['firstname'], $values['lastname']);
  786. $text_after_registration =
  787. '<p>'.
  788. get_lang('Dear').' '.
  789. stripslashes(Security::remove_XSS($recipient_name)).',<br /><br />'.
  790. get_lang('PersonalSettings').".</p>";
  791. $form_data = [
  792. 'button' => Display::button(
  793. 'next',
  794. get_lang('Next'),
  795. ['class' => 'btn btn-primary btn-large']
  796. ),
  797. 'message' => '',
  798. 'action' => api_get_path(WEB_PATH).'user_portal.php',
  799. 'go_button' => '',
  800. ];
  801. if (api_get_setting('allow_terms_conditions') === 'true' && $user_already_registered_show_terms) {
  802. if (api_get_setting('load_term_conditions_section') === 'login') {
  803. $form_data['action'] = api_get_path(WEB_PATH).'user_portal.php';
  804. } else {
  805. $courseInfo = api_get_course_info();
  806. if (!empty($courseInfo)) {
  807. $form_data['action'] = $courseInfo['course_public_url'].'?id_session='.api_get_session_id();
  808. $cidReset = true;
  809. Session::erase('_course');
  810. Session::erase('_cid');
  811. } else {
  812. $form_data['action'] = api_get_path(WEB_PATH).'user_portal.php';
  813. }
  814. }
  815. } else {
  816. if (!empty($values['email'])) {
  817. $text_after_registration .= '<p>'.get_lang('MailHasBeenSent').'.</p>';
  818. }
  819. if ($is_allowedCreateCourse) {
  820. if ($usersCanCreateCourse) {
  821. $form_data['message'] = '<p>'.get_lang('NowGoCreateYourCourse')."</p>";
  822. }
  823. $form_data['action'] = api_get_path(WEB_CODE_PATH).'create_course/add_course.php';
  824. if (api_get_setting('course_validation') === 'true') {
  825. $form_data['button'] = Display::button(
  826. 'next',
  827. get_lang('CreateCourseRequest'),
  828. ['class' => 'btn btn-primary btn-large']
  829. );
  830. } else {
  831. $form_data['button'] = Display::button(
  832. 'next',
  833. get_lang('CourseCreate'),
  834. ['class' => 'btn btn-primary btn-large']
  835. );
  836. $form_data['go_button'] = '&nbsp;&nbsp;<a href="'.api_get_path(WEB_PATH).'index.php'.'">'.
  837. Display::span(
  838. get_lang('Next'),
  839. ['class' => 'btn btn-primary btn-large']
  840. ).'</a>';
  841. }
  842. } else {
  843. if (api_get_setting('allow_students_to_browse_courses') == 'true') {
  844. $form_data['action'] = 'courses.php?action=subscribe';
  845. $form_data['message'] = '<p>'.get_lang('NowGoChooseYourCourses').".</p>";
  846. } else {
  847. $form_data['action'] = api_get_path(WEB_PATH).'user_portal.php';
  848. }
  849. $form_data['button'] = Display::button(
  850. 'next',
  851. get_lang('Next'),
  852. ['class' => 'btn btn-primary btn-large']
  853. );
  854. }
  855. }
  856. if ($sessionPremiumChecker && $sessionId) {
  857. header('Location:'.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/process.php?i='.$sessionId.'&t=2');
  858. Session::erase('SessionIsPremium');
  859. Session::erase('sessionId');
  860. exit;
  861. }
  862. SessionManager::redirectToSession();
  863. $redirectBuyCourse = Session::read('buy_course_redirect');
  864. if (!empty($redirectBuyCourse)) {
  865. $form_data['action'] = api_get_path(WEB_PATH).$redirectBuyCourse;
  866. Session::erase('buy_course_redirect');
  867. }
  868. $form_data = CourseManager::redirectToCourse($form_data);
  869. $form_register = new FormValidator('form_register', 'post', $form_data['action']);
  870. if (!empty($form_data['message'])) {
  871. $form_register->addElement('html', $form_data['message'].'<br /><br />');
  872. }
  873. if ($usersCanCreateCourse) {
  874. $form_register->addElement('html', $form_data['button']);
  875. } else {
  876. $form_register->addElement('html', $form_data['go_button']);
  877. }
  878. $text_after_registration .= $form_register->returnForm();
  879. // Just in case
  880. Session::erase('course_redirect');
  881. Session::erase('exercise_redirect');
  882. Session::erase('session_redirect');
  883. Session::erase('only_one_course_session_redirect');
  884. if (CustomPages::enabled() && CustomPages::exists(CustomPages::REGISTRATION_FEEDBACK)) {
  885. CustomPages::display(
  886. CustomPages::REGISTRATION_FEEDBACK,
  887. ['info' => $text_after_registration]
  888. );
  889. } else {
  890. $tpl = new Template($tool_name);
  891. $tpl->assign('inscription_content', $content);
  892. $tpl->assign('text_after_registration', $text_after_registration);
  893. $tpl->assign('hide_header', $hideHeaders);
  894. $inscription = $tpl->get_template('auth/inscription.tpl');
  895. $tpl->display($inscription);
  896. }
  897. } else {
  898. // Custom pages
  899. if (CustomPages::enabled() && CustomPages::exists(CustomPages::REGISTRATION)) {
  900. CustomPages::display(
  901. CustomPages::REGISTRATION,
  902. ['form' => $form, 'content' => $content]
  903. );
  904. } else {
  905. if (!api_is_anonymous()) {
  906. // Saving user to course if it was set.
  907. if (!empty($course_code_redirect)) {
  908. $course_info = api_get_course_info($course_code_redirect);
  909. if (!empty($course_info)) {
  910. if (in_array(
  911. $course_info['visibility'],
  912. [
  913. COURSE_VISIBILITY_OPEN_PLATFORM,
  914. COURSE_VISIBILITY_OPEN_WORLD,
  915. ]
  916. )
  917. ) {
  918. CourseManager::subscribe_user(
  919. $user_id,
  920. $course_info['code']
  921. );
  922. }
  923. }
  924. }
  925. CourseManager::redirectToCourse([]);
  926. }
  927. $tpl = new Template($tool_name);
  928. $tpl->assign('inscription_header', Display::page_header($tool_name));
  929. $tpl->assign('inscription_content', $content);
  930. $tpl->assign('form', $form->returnForm());
  931. $tpl->assign('hide_header', $hideHeaders);
  932. $inscription = $tpl->get_template('auth/inscription.tpl');
  933. $tpl->display($inscription);
  934. }
  935. }