inscription.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  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. $admins = UserManager::get_all_administrators();
  771. foreach ($admins as $admin_info) {
  772. MessageManager::send_message(
  773. $admin_info['user_id'],
  774. $emailsubject,
  775. $emailbody,
  776. [],
  777. [],
  778. null,
  779. null,
  780. null,
  781. null,
  782. $user_id
  783. );
  784. }
  785. // 2. set account inactive
  786. UserManager::disable($user_id);
  787. // 3. exit the page
  788. unset($user_id);
  789. Display::display_header($tool_name);
  790. echo Display::page_header($tool_name);
  791. echo $content;
  792. Display::display_footer();
  793. exit;
  794. } elseif (api_get_setting('allow_registration') === 'confirmation') {
  795. // 1. Send mail to the user
  796. $thisUser = api_get_user_entity($user_id);
  797. UserManager::sendUserConfirmationMail($thisUser);
  798. // 2. set account inactive
  799. UserManager::disable($user_id);
  800. // 3. exit the page
  801. unset($user_id);
  802. Display::addFlash(
  803. Display::return_message(
  804. get_lang('YouNeedConfirmYourAccountViaEmailToAccessThePlatform'),
  805. 'warning'
  806. )
  807. );
  808. Display::display_header($tool_name);
  809. //echo $content;
  810. Display::display_footer();
  811. exit;
  812. }
  813. }
  814. }
  815. // Terms & Conditions
  816. if (api_get_setting('allow_terms_conditions') === 'true') {
  817. // Update the terms & conditions.
  818. if (isset($values['legal_accept_type'])) {
  819. $cond_array = explode(':', $values['legal_accept_type']);
  820. if (!empty($cond_array[0]) && !empty($cond_array[1])) {
  821. $time = time();
  822. $conditionToSave = (int) $cond_array[0].':'.(int) $cond_array[1].':'.$time;
  823. UserManager::update_extra_field_value(
  824. $user_id,
  825. 'legal_accept',
  826. $conditionToSave
  827. );
  828. Event::addEvent(
  829. LOG_TERM_CONDITION_ACCEPTED,
  830. LOG_USER_OBJECT,
  831. api_get_user_info($user_id),
  832. api_get_utc_datetime()
  833. );
  834. $bossList = UserManager::getStudentBossList($user_id);
  835. if (!empty($bossList)) {
  836. $bossList = array_column($bossList, 'boss_id');
  837. $currentUserInfo = api_get_user_info($user_id);
  838. foreach ($bossList as $bossId) {
  839. $subjectEmail = sprintf(
  840. get_lang('UserXSignedTheAgreement'),
  841. $currentUserInfo['complete_name']
  842. );
  843. $contentEmail = sprintf(
  844. get_lang('UserXSignedTheAgreementTheY'),
  845. $currentUserInfo['complete_name'],
  846. api_get_local_time($time)
  847. );
  848. MessageManager::send_message_simple(
  849. $bossId,
  850. $subjectEmail,
  851. $contentEmail,
  852. $user_id
  853. );
  854. }
  855. }
  856. }
  857. }
  858. $values = api_get_user_info($user_id);
  859. }
  860. /* SESSION REGISTERING */
  861. /* @todo move this in a function */
  862. $_user['firstName'] = stripslashes($values['firstname']);
  863. $_user['lastName'] = stripslashes($values['lastname']);
  864. $_user['mail'] = $values['email'];
  865. $_user['language'] = $values['language'];
  866. $_user['user_id'] = $user_id;
  867. Session::write('_user', $_user);
  868. $is_allowedCreateCourse = isset($values['status']) && $values['status'] == 1;
  869. $usersCanCreateCourse = api_is_allowed_to_create_course();
  870. Session::write('is_allowedCreateCourse', $is_allowedCreateCourse);
  871. // Stats
  872. Event::eventLogin($user_id);
  873. // last user login date is now
  874. $user_last_login_datetime = 0; // used as a unix timestamp it will correspond to : 1 1 1970
  875. Session::write('user_last_login_datetime', $user_last_login_datetime);
  876. $recipient_name = api_get_person_name($values['firstname'], $values['lastname']);
  877. $text_after_registration =
  878. '<p>'.
  879. get_lang('Dear').' '.
  880. stripslashes(Security::remove_XSS($recipient_name)).',<br /><br />'.
  881. get_lang('PersonalSettings').".</p>";
  882. $form_data = [
  883. 'button' => Display::button(
  884. 'next',
  885. get_lang('Next'),
  886. ['class' => 'btn btn-primary btn-large']
  887. ),
  888. 'message' => '',
  889. 'action' => api_get_path(WEB_PATH).'user_portal.php',
  890. 'go_button' => '',
  891. ];
  892. if (api_get_setting('allow_terms_conditions') === 'true' && $user_already_registered_show_terms) {
  893. if (api_get_setting('load_term_conditions_section') === 'login') {
  894. $form_data['action'] = api_get_path(WEB_PATH).'user_portal.php';
  895. } else {
  896. $courseInfo = api_get_course_info();
  897. if (!empty($courseInfo)) {
  898. $form_data['action'] = $courseInfo['course_public_url'].'?id_session='.api_get_session_id();
  899. $cidReset = true;
  900. Session::erase('_course');
  901. Session::erase('_cid');
  902. } else {
  903. $form_data['action'] = api_get_path(WEB_PATH).'user_portal.php';
  904. }
  905. }
  906. } else {
  907. if (!empty($values['email'])) {
  908. $text_after_registration .= '<p>'.get_lang('MailHasBeenSent').'.</p>';
  909. }
  910. if ($is_allowedCreateCourse) {
  911. if ($usersCanCreateCourse) {
  912. $form_data['message'] = '<p>'.get_lang('NowGoCreateYourCourse').'</p>';
  913. }
  914. $form_data['action'] = api_get_path(WEB_CODE_PATH).'create_course/add_course.php';
  915. if (api_get_setting('course_validation') === 'true') {
  916. $form_data['button'] = Display::button(
  917. 'next',
  918. get_lang('CreateCourseRequest'),
  919. ['class' => 'btn btn-primary btn-large']
  920. );
  921. } else {
  922. $form_data['button'] = Display::button(
  923. 'next',
  924. get_lang('CourseCreate'),
  925. ['class' => 'btn btn-primary btn-large']
  926. );
  927. $form_data['go_button'] = '&nbsp;&nbsp;<a href="'.api_get_path(WEB_PATH).'index.php'.'">'.
  928. Display::span(
  929. get_lang('Next'),
  930. ['class' => 'btn btn-primary btn-large']
  931. ).'</a>';
  932. }
  933. } else {
  934. if (api_get_setting('allow_students_to_browse_courses') == 'true') {
  935. $form_data['action'] = 'courses.php?action=subscribe';
  936. $form_data['message'] = '<p>'.get_lang('NowGoChooseYourCourses').".</p>";
  937. } else {
  938. $form_data['action'] = api_get_path(WEB_PATH).'user_portal.php';
  939. }
  940. $form_data['button'] = Display::button(
  941. 'next',
  942. get_lang('Next'),
  943. ['class' => 'btn btn-primary btn-large']
  944. );
  945. }
  946. }
  947. if ($sessionPremiumChecker && $sessionId) {
  948. Session::erase('SessionIsPremium');
  949. Session::erase('sessionId');
  950. header('Location:'.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/process.php?i='.$sessionId.'&t=2');
  951. exit;
  952. }
  953. SessionManager::redirectToSession();
  954. $redirectBuyCourse = Session::read('buy_course_redirect');
  955. if (!empty($redirectBuyCourse)) {
  956. $form_data['action'] = api_get_path(WEB_PATH).$redirectBuyCourse;
  957. Session::erase('buy_course_redirect');
  958. }
  959. $form_data = CourseManager::redirectToCourse($form_data);
  960. $form_register = new FormValidator('form_register', 'post', $form_data['action']);
  961. if (!empty($form_data['message'])) {
  962. $form_register->addElement('html', $form_data['message'].'<br /><br />');
  963. }
  964. if ($usersCanCreateCourse) {
  965. $form_register->addElement('html', $form_data['button']);
  966. } else {
  967. if (!empty($redirectBuyCourse)) {
  968. $form_register->addButtonNext(get_lang('Next'));
  969. } else {
  970. $form_register->addElement('html', $form_data['go_button']);
  971. }
  972. }
  973. $text_after_registration .= $form_register->returnForm();
  974. // Just in case
  975. Session::erase('course_redirect');
  976. Session::erase('exercise_redirect');
  977. Session::erase('session_redirect');
  978. Session::erase('only_one_course_session_redirect');
  979. if (CustomPages::enabled() && CustomPages::exists(CustomPages::REGISTRATION_FEEDBACK)) {
  980. CustomPages::display(
  981. CustomPages::REGISTRATION_FEEDBACK,
  982. ['info' => $text_after_registration]
  983. );
  984. } else {
  985. $tpl = new Template($tool_name);
  986. $tpl->assign('inscription_content', $content);
  987. $tpl->assign('text_after_registration', $text_after_registration);
  988. $tpl->assign('hide_header', $hideHeaders);
  989. $inscription = $tpl->get_template('auth/inscription.tpl');
  990. $tpl->display($inscription);
  991. }
  992. } else {
  993. // Custom pages
  994. if (CustomPages::enabled() && CustomPages::exists(CustomPages::REGISTRATION)) {
  995. CustomPages::display(
  996. CustomPages::REGISTRATION,
  997. ['form' => $form, 'content' => $content]
  998. );
  999. } else {
  1000. if (!api_is_anonymous()) {
  1001. // Saving user to course if it was set.
  1002. if (!empty($course_code_redirect)) {
  1003. $course_info = api_get_course_info($course_code_redirect);
  1004. if (!empty($course_info)) {
  1005. if (in_array(
  1006. $course_info['visibility'],
  1007. [
  1008. COURSE_VISIBILITY_OPEN_PLATFORM,
  1009. COURSE_VISIBILITY_OPEN_WORLD,
  1010. ]
  1011. )
  1012. ) {
  1013. CourseManager::subscribeUser(
  1014. $user_id,
  1015. $course_info['code']
  1016. );
  1017. }
  1018. }
  1019. }
  1020. CourseManager::redirectToCourse([]);
  1021. }
  1022. $tpl = new Template($tool_name);
  1023. $tpl->assign('inscription_header', Display::page_header($tool_name));
  1024. $tpl->assign('inscription_content', $content);
  1025. $tpl->assign('form', $form->returnForm());
  1026. $tpl->assign('hide_header', $hideHeaders);
  1027. $inscription = $tpl->get_template('auth/inscription.tpl');
  1028. $tpl->display($inscription);
  1029. }
  1030. }