inscription.php 43 KB

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