inscription.php 42 KB

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