inscription.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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. '<div class="form-control" disabled=disabled style="height: 100px; overflow: auto;">'.
  588. get_lang(nl2br($condition['text_area'])).
  589. '</div>',
  590. ]
  591. );
  592. }
  593. }
  594. }
  595. }
  596. if ($form->validate()) {
  597. $values = $form->getSubmitValues(1);
  598. // Make *sure* the login isn't too long
  599. if (isset($values['username'])) {
  600. $values['username'] = api_substr($values['username'], 0, USERNAME_MAX_LENGTH);
  601. }
  602. if (api_get_setting('allow_registration_as_teacher') === 'false') {
  603. $values['status'] = STUDENT;
  604. }
  605. if (empty($values['official_code']) && !empty($values['username'])) {
  606. $values['official_code'] = api_strtoupper($values['username']);
  607. }
  608. if (api_get_setting('login_is_email') === 'true') {
  609. $values['username'] = $values['email'];
  610. }
  611. if ($user_already_registered_show_terms &&
  612. api_get_setting('allow_terms_conditions') === 'true'
  613. ) {
  614. $user_id = $_SESSION['term_and_condition']['user_id'];
  615. $is_admin = UserManager::is_admin($user_id);
  616. Session::write('is_platformAdmin', $is_admin);
  617. } else {
  618. // Moved here to include extra fields when creating a user. Formerly placed after user creation
  619. // Register extra fields
  620. $extras = [];
  621. foreach ($values as $key => $value) {
  622. if (substr($key, 0, 6) == 'extra_') {
  623. //an extra field
  624. $extras[substr($key, 6)] = $value;
  625. } elseif (strpos($key, 'remove_extra_') !== false) {
  626. $extra_value = Security::filter_filename(urldecode(key($value)));
  627. // To remove from user_field_value and folder
  628. UserManager::update_extra_field_value(
  629. $user_id,
  630. substr($key, 13),
  631. $extra_value
  632. );
  633. }
  634. }
  635. $status = isset($values['status']) ? $values['status'] : STUDENT;
  636. $phone = isset($values['phone']) ? $values['phone'] : null;
  637. $values['language'] = isset($values['language']) ? $values['language'] : api_get_interface_language();
  638. $values['address'] = isset($values['address']) ? $values['address'] : '';
  639. // Creates a new user
  640. $user_id = UserManager::create_user(
  641. $values['firstname'],
  642. $values['lastname'],
  643. $status,
  644. $values['email'],
  645. $values['username'],
  646. $values['pass1'],
  647. $values['official_code'],
  648. $values['language'],
  649. $phone,
  650. null,
  651. PLATFORM_AUTH_SOURCE,
  652. null,
  653. 1,
  654. 0,
  655. $extras,
  656. null,
  657. true,
  658. false,
  659. $values['address'],
  660. false,
  661. $form
  662. );
  663. // Update the extra fields
  664. $count_extra_field = count($extras);
  665. if ($count_extra_field > 0 && is_int($user_id)) {
  666. foreach ($extras as $key => $value) {
  667. // For array $value -> if exists key 'tmp_name' then must not be empty
  668. // This avoid delete from user field value table when doesn't upload a file
  669. if (is_array($value)) {
  670. if (array_key_exists('tmp_name', $value) && empty($value['tmp_name'])) {
  671. //Nothing to do
  672. } else {
  673. if (array_key_exists('tmp_name', $value)) {
  674. $value['tmp_name'] = Security::filter_filename($value['tmp_name']);
  675. }
  676. if (array_key_exists('name', $value)) {
  677. $value['name'] = Security::filter_filename($value['name']);
  678. }
  679. UserManager::update_extra_field_value($user_id, $key, $value);
  680. }
  681. } else {
  682. UserManager::update_extra_field_value($user_id, $key, $value);
  683. }
  684. }
  685. }
  686. if ($user_id) {
  687. // Storing the extended profile
  688. $store_extended = false;
  689. $sql = "UPDATE ".Database::get_main_table(TABLE_MAIN_USER)." SET ";
  690. if (api_get_setting('extended_profile') == 'true' &&
  691. api_get_setting('extendedprofile_registration', 'mycomptetences') == 'true'
  692. ) {
  693. $sql_set[] = "competences = '".Database::escape_string($values['competences'])."'";
  694. $store_extended = true;
  695. }
  696. if (api_get_setting('extended_profile') == 'true' &&
  697. api_get_setting('extendedprofile_registration', 'mydiplomas') == 'true'
  698. ) {
  699. $sql_set[] = "diplomas = '".Database::escape_string($values['diplomas'])."'";
  700. $store_extended = true;
  701. }
  702. if (api_get_setting('extended_profile') == 'true' &&
  703. api_get_setting('extendedprofile_registration', 'myteach') == 'true'
  704. ) {
  705. $sql_set[] = "teach = '".Database::escape_string($values['teach'])."'";
  706. $store_extended = true;
  707. }
  708. if (api_get_setting('extended_profile') == 'true' &&
  709. api_get_setting('extendedprofile_registration', 'mypersonalopenarea') == 'true'
  710. ) {
  711. $sql_set[] = "openarea = '".Database::escape_string($values['openarea'])."'";
  712. $store_extended = true;
  713. }
  714. if ($store_extended) {
  715. $sql .= implode(',', $sql_set);
  716. $sql .= " WHERE user_id = ".intval($user_id)."";
  717. Database::query($sql);
  718. }
  719. // Saving user to Session if it was set
  720. if (!empty($sessionToRedirect) && !$sessionPremiumChecker) {
  721. $sessionInfo = api_get_session_info($sessionToRedirect);
  722. if (!empty($sessionInfo)) {
  723. SessionManager::subscribeUsersToSession(
  724. $sessionToRedirect,
  725. [$user_id],
  726. SESSION_VISIBLE_READ_ONLY,
  727. false
  728. );
  729. }
  730. }
  731. // Saving user to course if it was set.
  732. if (!empty($course_code_redirect)) {
  733. $course_info = api_get_course_info($course_code_redirect);
  734. if (!empty($course_info)) {
  735. if (in_array(
  736. $course_info['visibility'],
  737. [
  738. COURSE_VISIBILITY_OPEN_PLATFORM,
  739. COURSE_VISIBILITY_OPEN_WORLD,
  740. ]
  741. )
  742. ) {
  743. CourseManager::subscribeUser(
  744. $user_id,
  745. $course_info['code']
  746. );
  747. }
  748. }
  749. }
  750. /* If the account has to be approved then we set the account to inactive,
  751. sent a mail to the platform admin and exit the page.*/
  752. if (api_get_setting('allow_registration') === 'approval') {
  753. // 1. Send mail to all platform admin
  754. $emailsubject = get_lang('ApprovalForNewAccount').': '.$values['username'];
  755. $emailbody = get_lang('ApprovalForNewAccount')."\n";
  756. $emailbody .= get_lang('UserName').': '.$values['username']."\n";
  757. if (api_is_western_name_order()) {
  758. $emailbody .= get_lang('FirstName').': '.$values['firstname']."\n";
  759. $emailbody .= get_lang('LastName').': '.$values['lastname']."\n";
  760. } else {
  761. $emailbody .= get_lang('LastName').': '.$values['lastname']."\n";
  762. $emailbody .= get_lang('FirstName').': '.$values['firstname']."\n";
  763. }
  764. $emailbody .= get_lang('Email').': '.$values['email']."\n";
  765. $emailbody .= get_lang('Status').': '.$values['status']."\n\n";
  766. $url_edit = Display::url(
  767. api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user_id,
  768. api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user_id
  769. );
  770. $emailbody .= get_lang('ManageUser').": $url_edit";
  771. if (api_get_configuration_value('send_inscription_notification_to_general_admin_only')) {
  772. $email = api_get_setting('emailAdministrator');
  773. $firtname = api_get_setting('administratorSurname');
  774. $lastname = api_get_setting('administratorName');
  775. api_mail_html("$firtname $lastname", $email, $emailsubject, $emailbody);
  776. } else {
  777. $admins = UserManager::get_all_administrators();
  778. foreach ($admins as $admin_info) {
  779. MessageManager::send_message(
  780. $admin_info['user_id'],
  781. $emailsubject,
  782. $emailbody,
  783. [],
  784. [],
  785. null,
  786. null,
  787. null,
  788. null,
  789. $user_id
  790. );
  791. }
  792. }
  793. // 2. set account inactive
  794. UserManager::disable($user_id);
  795. // 3. exit the page
  796. unset($user_id);
  797. Display::display_header($tool_name);
  798. echo Display::page_header($tool_name);
  799. echo $content;
  800. Display::display_footer();
  801. exit;
  802. } elseif (api_get_setting('allow_registration') === 'confirmation') {
  803. // 1. Send mail to the user
  804. $thisUser = api_get_user_entity($user_id);
  805. UserManager::sendUserConfirmationMail($thisUser);
  806. // 2. set account inactive
  807. UserManager::disable($user_id);
  808. // 3. exit the page
  809. unset($user_id);
  810. Display::addFlash(
  811. Display::return_message(
  812. get_lang('YouNeedConfirmYourAccountViaEmailToAccessThePlatform'),
  813. 'warning'
  814. )
  815. );
  816. Display::display_header($tool_name);
  817. //echo $content;
  818. Display::display_footer();
  819. exit;
  820. }
  821. }
  822. }
  823. // Terms & Conditions
  824. if (api_get_setting('allow_terms_conditions') === 'true') {
  825. // Update the terms & conditions.
  826. if (isset($values['legal_accept_type'])) {
  827. $cond_array = explode(':', $values['legal_accept_type']);
  828. if (!empty($cond_array[0]) && !empty($cond_array[1])) {
  829. $time = time();
  830. $conditionToSave = (int) $cond_array[0].':'.(int) $cond_array[1].':'.$time;
  831. UserManager::update_extra_field_value(
  832. $user_id,
  833. 'legal_accept',
  834. $conditionToSave
  835. );
  836. Event::addEvent(
  837. LOG_TERM_CONDITION_ACCEPTED,
  838. LOG_USER_OBJECT,
  839. api_get_user_info($user_id),
  840. api_get_utc_datetime()
  841. );
  842. $bossList = UserManager::getStudentBossList($user_id);
  843. if (!empty($bossList)) {
  844. $bossList = array_column($bossList, 'boss_id');
  845. $currentUserInfo = api_get_user_info($user_id);
  846. foreach ($bossList as $bossId) {
  847. $subjectEmail = sprintf(
  848. get_lang('UserXSignedTheAgreement'),
  849. $currentUserInfo['complete_name']
  850. );
  851. $contentEmail = sprintf(
  852. get_lang('UserXSignedTheAgreementTheY'),
  853. $currentUserInfo['complete_name'],
  854. api_get_local_time($time)
  855. );
  856. MessageManager::send_message_simple(
  857. $bossId,
  858. $subjectEmail,
  859. $contentEmail,
  860. $user_id
  861. );
  862. }
  863. }
  864. }
  865. }
  866. $values = api_get_user_info($user_id);
  867. }
  868. /* SESSION REGISTERING */
  869. /* @todo move this in a function */
  870. $_user['firstName'] = stripslashes($values['firstname']);
  871. $_user['lastName'] = stripslashes($values['lastname']);
  872. $_user['mail'] = $values['email'];
  873. $_user['language'] = $values['language'];
  874. $_user['user_id'] = $user_id;
  875. Session::write('_user', $_user);
  876. $is_allowedCreateCourse = isset($values['status']) && $values['status'] == 1;
  877. $usersCanCreateCourse = api_is_allowed_to_create_course();
  878. Session::write('is_allowedCreateCourse', $is_allowedCreateCourse);
  879. // Stats
  880. Event::eventLogin($user_id);
  881. // last user login date is now
  882. $user_last_login_datetime = 0; // used as a unix timestamp it will correspond to : 1 1 1970
  883. Session::write('user_last_login_datetime', $user_last_login_datetime);
  884. $recipient_name = api_get_person_name($values['firstname'], $values['lastname']);
  885. $text_after_registration =
  886. '<p>'.
  887. get_lang('Dear').' '.
  888. stripslashes(Security::remove_XSS($recipient_name)).',<br /><br />'.
  889. get_lang('PersonalSettings').".</p>";
  890. $form_data = [
  891. 'button' => Display::button(
  892. 'next',
  893. get_lang('Next'),
  894. ['class' => 'btn btn-primary btn-large']
  895. ),
  896. 'message' => '',
  897. 'action' => api_get_path(WEB_PATH).'user_portal.php',
  898. 'go_button' => '',
  899. ];
  900. if (api_get_setting('allow_terms_conditions') === 'true' && $user_already_registered_show_terms) {
  901. if (api_get_setting('load_term_conditions_section') === 'login') {
  902. $form_data['action'] = api_get_path(WEB_PATH).'user_portal.php';
  903. } else {
  904. $courseInfo = api_get_course_info();
  905. if (!empty($courseInfo)) {
  906. $form_data['action'] = $courseInfo['course_public_url'].'?id_session='.api_get_session_id();
  907. $cidReset = true;
  908. Session::erase('_course');
  909. Session::erase('_cid');
  910. } else {
  911. $form_data['action'] = api_get_path(WEB_PATH).'user_portal.php';
  912. }
  913. }
  914. } else {
  915. if (!empty($values['email'])) {
  916. $text_after_registration .= '<p>'.get_lang('MailHasBeenSent').'.</p>';
  917. }
  918. if ($is_allowedCreateCourse) {
  919. if ($usersCanCreateCourse) {
  920. $form_data['message'] = '<p>'.get_lang('NowGoCreateYourCourse').'</p>';
  921. }
  922. $form_data['action'] = api_get_path(WEB_CODE_PATH).'create_course/add_course.php';
  923. if (api_get_setting('course_validation') === 'true') {
  924. $form_data['button'] = Display::button(
  925. 'next',
  926. get_lang('CreateCourseRequest'),
  927. ['class' => 'btn btn-primary btn-large']
  928. );
  929. } else {
  930. $form_data['button'] = Display::button(
  931. 'next',
  932. get_lang('CourseCreate'),
  933. ['class' => 'btn btn-primary btn-large']
  934. );
  935. $form_data['go_button'] = '&nbsp;&nbsp;<a href="'.api_get_path(WEB_PATH).'index.php'.'">'.
  936. Display::span(
  937. get_lang('Next'),
  938. ['class' => 'btn btn-primary btn-large']
  939. ).'</a>';
  940. }
  941. } else {
  942. if (api_get_setting('allow_students_to_browse_courses') == 'true') {
  943. $form_data['action'] = 'courses.php?action=subscribe';
  944. $form_data['message'] = '<p>'.get_lang('NowGoChooseYourCourses').".</p>";
  945. } else {
  946. $form_data['action'] = api_get_path(WEB_PATH).'user_portal.php';
  947. }
  948. $form_data['button'] = Display::button(
  949. 'next',
  950. get_lang('Next'),
  951. ['class' => 'btn btn-primary btn-large']
  952. );
  953. }
  954. }
  955. if ($sessionPremiumChecker && $sessionId) {
  956. Session::erase('SessionIsPremium');
  957. Session::erase('sessionId');
  958. header('Location:'.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/process.php?i='.$sessionId.'&t=2');
  959. exit;
  960. }
  961. SessionManager::redirectToSession();
  962. $redirectBuyCourse = Session::read('buy_course_redirect');
  963. if (!empty($redirectBuyCourse)) {
  964. $form_data['action'] = api_get_path(WEB_PATH).$redirectBuyCourse;
  965. Session::erase('buy_course_redirect');
  966. }
  967. $form_data = CourseManager::redirectToCourse($form_data);
  968. $form_register = new FormValidator('form_register', 'post', $form_data['action']);
  969. if (!empty($form_data['message'])) {
  970. $form_register->addElement('html', $form_data['message'].'<br /><br />');
  971. }
  972. if ($usersCanCreateCourse) {
  973. $form_register->addElement('html', $form_data['button']);
  974. } else {
  975. if (!empty($redirectBuyCourse)) {
  976. $form_register->addButtonNext(get_lang('Next'));
  977. } else {
  978. $form_register->addElement('html', $form_data['go_button']);
  979. }
  980. }
  981. $text_after_registration .= $form_register->returnForm();
  982. // Just in case
  983. Session::erase('course_redirect');
  984. Session::erase('exercise_redirect');
  985. Session::erase('session_redirect');
  986. Session::erase('only_one_course_session_redirect');
  987. if (CustomPages::enabled() && CustomPages::exists(CustomPages::REGISTRATION_FEEDBACK)) {
  988. CustomPages::display(
  989. CustomPages::REGISTRATION_FEEDBACK,
  990. ['info' => $text_after_registration]
  991. );
  992. } else {
  993. $tpl = new Template($tool_name);
  994. $tpl->assign('inscription_content', $content);
  995. $tpl->assign('text_after_registration', $text_after_registration);
  996. $tpl->assign('hide_header', $hideHeaders);
  997. $inscription = $tpl->get_template('auth/inscription.tpl');
  998. $tpl->display($inscription);
  999. }
  1000. } else {
  1001. // Custom pages
  1002. if (CustomPages::enabled() && CustomPages::exists(CustomPages::REGISTRATION)) {
  1003. CustomPages::display(
  1004. CustomPages::REGISTRATION,
  1005. ['form' => $form, 'content' => $content]
  1006. );
  1007. } else {
  1008. if (!api_is_anonymous()) {
  1009. // Saving user to course if it was set.
  1010. if (!empty($course_code_redirect)) {
  1011. $course_info = api_get_course_info($course_code_redirect);
  1012. if (!empty($course_info)) {
  1013. if (in_array(
  1014. $course_info['visibility'],
  1015. [
  1016. COURSE_VISIBILITY_OPEN_PLATFORM,
  1017. COURSE_VISIBILITY_OPEN_WORLD,
  1018. ]
  1019. )
  1020. ) {
  1021. CourseManager::subscribeUser(
  1022. $user_id,
  1023. $course_info['code']
  1024. );
  1025. }
  1026. }
  1027. }
  1028. CourseManager::redirectToCourse([]);
  1029. }
  1030. $tpl = new Template($tool_name);
  1031. $tpl->assign('inscription_header', Display::page_header($tool_name));
  1032. $tpl->assign('inscription_content', $content);
  1033. $tpl->assign('form', $form->returnForm());
  1034. $tpl->assign('hide_header', $hideHeaders);
  1035. $inscription = $tpl->get_template('auth/inscription.tpl');
  1036. $tpl->display($inscription);
  1037. }
  1038. }