inscription.php 43 KB

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