inscription.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. use Chamilo\CoreBundle\Framework\Container;
  5. /**
  6. * This script displays a form for registering new users.
  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 '../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 = $allowedFieldsConfiguration;
  26. }
  27. $userGeolocalization = api_get_setting('enable_profile_user_address_geolocalization') == 'true';
  28. $htmlHeadXtra[] = api_get_password_checker_js('#username', '#pass1');
  29. // User is not allowed if Terms and Conditions are disabled and
  30. // registration is disabled too.
  31. $isNotAllowedHere = api_get_setting('registration.allow_terms_conditions') === 'false' &&
  32. api_get_setting('registration.allow_registration') === 'false';
  33. if ($isNotAllowedHere) {
  34. api_not_allowed(true, get_lang('RegistrationDisabled'));
  35. }
  36. if (!empty($_SESSION['user_language_choice'])) {
  37. $user_selected_language = $_SESSION['user_language_choice'];
  38. } elseif (!empty($_SESSION['_user']['language'])) {
  39. $user_selected_language = $_SESSION['_user']['language'];
  40. } else {
  41. $user_selected_language = api_get_setting('language.platform_language');
  42. }
  43. $htmlHeadXtra[] = '<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=true" ></script>';
  44. if ($userGeolocalization) {
  45. $htmlHeadXtra[] = '<script>
  46. $(document).ready(function() {
  47. initializeGeo(false, false);
  48. $("#geolocalization").on("click", function() {
  49. var address = $("#address").val();
  50. initializeGeo(address, false);
  51. return false;
  52. });
  53. $("#myLocation").on("click", function() {
  54. myLocation();
  55. return false;
  56. });
  57. $("#address").keypress(function (event) {
  58. if (event.which == 13) {
  59. $("#geolocalization").click();
  60. return false;
  61. }
  62. });
  63. });
  64. function myLocation() {
  65. if (navigator.geolocation) {
  66. var geoPosition = function(position) {
  67. var lat = position.coords.latitude;
  68. var lng = position.coords.longitude;
  69. var latLng = new google.maps.LatLng(lat, lng);
  70. initializeGeo(false, latLng)
  71. };
  72. var geoError = function(error) {
  73. alert("Geocode ' . get_lang('Error') . ': " + error);
  74. };
  75. var geoOptions = {
  76. enableHighAccuracy: true
  77. };
  78. navigator.geolocation.getCurrentPosition(geoPosition, geoError, geoOptions);
  79. }
  80. }
  81. function initializeGeo(address, latLng) {
  82. var geocoder = new google.maps.Geocoder();
  83. var latlng = new google.maps.LatLng(-75.503, 22.921);
  84. var myOptions = {
  85. zoom: 15,
  86. center: latlng,
  87. mapTypeControl: true,
  88. mapTypeControlOptions: {
  89. style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
  90. },
  91. navigationControl: true,
  92. mapTypeId: google.maps.MapTypeId.ROADMAP
  93. };
  94. map = new google.maps.Map(document.getElementById("map"), myOptions);
  95. var parameter = address ? { "address": address } : latLng ? { "latLng": latLng } : { "address": "Google" };
  96. if (geocoder && parameter) {
  97. geocoder.geocode(parameter, function(results, status) {
  98. if (status == google.maps.GeocoderStatus.OK) {
  99. if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
  100. map.setCenter(results[0].geometry.location);
  101. if (!address) {
  102. $("#address").val(results[0].formatted_address);
  103. }
  104. var infowindow = new google.maps.InfoWindow({
  105. content: "<b>" + $("#address").val() + "</b>",
  106. size: new google.maps.Size(150, 50)
  107. });
  108. var marker = new google.maps.Marker({
  109. position: results[0].geometry.location,
  110. map: map,
  111. title: $("#address").val()
  112. });
  113. google.maps.event.addListener(marker, "click", function() {
  114. infowindow.open(map, marker);
  115. });
  116. } else {
  117. alert("' . get_lang("NotFound") . '");
  118. }
  119. } else {
  120. alert("Geocode ' . get_lang('Error') . ': " + status);
  121. }
  122. });
  123. }
  124. }
  125. </script>';
  126. }
  127. $form = new FormValidator('registration');
  128. $user_already_registered_show_terms = false;
  129. if (api_get_setting('registration.allow_terms_conditions') == 'true') {
  130. $user_already_registered_show_terms = isset($_SESSION['term_and_condition']['user_id']);
  131. }
  132. // Direct Link Subscription feature #5299
  133. $course_code_redirect = isset($_REQUEST['c']) && !empty($_REQUEST['c']) ? $_REQUEST['c'] : null;
  134. $exercise_redirect = isset($_REQUEST['e']) && !empty($_REQUEST['e']) ? $_REQUEST['e'] : null;
  135. if (!empty($course_code_redirect)) {
  136. Session::write('course_redirect', $course_code_redirect);
  137. Session::write('exercise_redirect', $exercise_redirect);
  138. }
  139. if ($user_already_registered_show_terms === false) {
  140. if (api_is_western_name_order()) {
  141. // FIRST NAME and LAST NAME
  142. $form->addElement('text', 'firstname', get_lang('FirstName'), array('size' => 40));
  143. $form->addElement('text', 'lastname', get_lang('LastName'), array('size' => 40));
  144. } else {
  145. // LAST NAME and FIRST NAME
  146. $form->addElement('text', 'lastname', get_lang('LastName'), array('size' => 40));
  147. $form->addElement('text', 'firstname', get_lang('FirstName'), array('size' => 40));
  148. }
  149. $form->applyFilter(array('lastname', 'firstname'), 'trim');
  150. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  151. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  152. // EMAIL
  153. $form->addElement('text', 'email', get_lang('Email'), array('size' => 40));
  154. if (api_get_setting('registration', 'email') == 'true') {
  155. $form->addRule('email', get_lang('ThisFieldIsRequired'), 'required');
  156. }
  157. if (api_get_setting('profile.login_is_email') == 'true') {
  158. $form->applyFilter('email', 'trim');
  159. if (api_get_setting('registration', 'email') != 'true') {
  160. $form->addRule('email', get_lang('ThisFieldIsRequired'), 'required');
  161. }
  162. $form->addRule('email', sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  163. $form->addRule('email', get_lang('UserTaken'), 'username_available');
  164. }
  165. $form->addRule('email', get_lang('EmailWrong'), 'email');
  166. if (api_get_setting('openid_authentication') == 'true') {
  167. $form->addElement('text', 'openid', get_lang('OpenIDURL'), array('size' => 40));
  168. }
  169. // OFFICIAL CODE
  170. if (CONFVAL_ASK_FOR_OFFICIAL_CODE) {
  171. if (in_array('official_code', $allowedFields)) {
  172. $form->addElement(
  173. 'text',
  174. 'official_code',
  175. get_lang('OfficialCode'),
  176. array('size' => 40)
  177. );
  178. if (api_get_setting('registration', 'officialcode') == 'true') {
  179. $form->addRule(
  180. 'official_code',
  181. get_lang('ThisFieldIsRequired'),
  182. 'required'
  183. );
  184. }
  185. }
  186. }
  187. // USERNAME
  188. if (api_get_setting('profile.login_is_email') != 'true') {
  189. $form->addText(
  190. 'username',
  191. get_lang('UserName'),
  192. array(
  193. 'id' => 'username',
  194. 'size' => USERNAME_MAX_LENGTH,
  195. 'autocomplete' => 'off'
  196. )
  197. );
  198. $form->applyFilter('username', 'trim');
  199. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  200. $form->addRule('username', sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  201. $form->addRule('username', get_lang('UsernameWrong'), 'username');
  202. $form->addRule('username', get_lang('UserTaken'), 'username_available');
  203. }
  204. // PASSWORD
  205. $form->addElement(
  206. 'password',
  207. 'pass1',
  208. get_lang('Pass'),
  209. array('id' => 'pass1', 'size' => 20, 'autocomplete' => 'off')
  210. );
  211. $checkPass = api_get_setting('security.allow_strength_pass_checker');
  212. if ($checkPass == 'true') {
  213. $form->addElement('label', null, '<div id="password_progress"></div>');
  214. }
  215. $form->addElement(
  216. 'password',
  217. 'pass2',
  218. get_lang('Confirmation'),
  219. array('id' => 'pass2', 'size' => 20, 'autocomplete' => 'off')
  220. );
  221. $form->addRule('pass1', get_lang('ThisFieldIsRequired'), 'required');
  222. $form->addRule('pass2', get_lang('ThisFieldIsRequired'), 'required');
  223. $form->addRule(array('pass1', 'pass2'), get_lang('PassTwo'), 'compare');
  224. if (CHECK_PASS_EASY_TO_FIND) {
  225. $form->addRule(
  226. 'pass1',
  227. get_lang('PassTooEasy') . ': ' . api_generate_password(),
  228. 'callback',
  229. 'api_check_password'
  230. );
  231. }
  232. // PHONE
  233. if (in_array('phone', $allowedFields)) {
  234. $form->addElement(
  235. 'text',
  236. 'phone',
  237. get_lang('Phone'),
  238. array('size' => 20)
  239. );
  240. if (api_get_setting('registration', 'phone') == 'true') {
  241. $form->addRule(
  242. 'phone',
  243. get_lang('ThisFieldIsRequired'),
  244. 'required'
  245. );
  246. }
  247. }
  248. if ($userGeolocalization) {
  249. // Geolocation
  250. if (in_array('address', $allowedFields)) {
  251. $form->addElement('text', 'address', get_lang('AddressField'), ['id' => 'address']);
  252. $form->addHtml('
  253. <div class="form-group">
  254. <label for="geolocalization" class="col-sm-2 control-label"></label>
  255. <div class="col-sm-8">
  256. <button class="null btn btn-default " id="geolocalization" name="geolocalization" type="submit"><em class="fa fa-map-marker"></em> ' . get_lang('Geolocalization') . '</button>
  257. <button class="null btn btn-default " id="myLocation" name="myLocation" type="submit"><em class="fa fa-crosshairs"></em> ' . get_lang('MyLocation') . '</button>
  258. </div>
  259. </div>
  260. ');
  261. $form->addHtml('
  262. <div class="form-group">
  263. <label for="map" class="col-sm-2 control-label">
  264. ' . get_lang('Map') . '
  265. </label>
  266. <div class="col-sm-8">
  267. <div name="map" id="map" style="width:100%; height:300px;">
  268. </div>
  269. </div>
  270. </div>
  271. ');
  272. }
  273. }
  274. // LANGUAGE
  275. if (in_array('language', $allowedFields)) {
  276. if (api_get_setting('registration', 'language') == 'true') {
  277. $form->addElement(
  278. 'select_language',
  279. 'language',
  280. get_lang('Language')
  281. );
  282. }
  283. }
  284. // STUDENT/TEACHER
  285. if (api_get_setting(
  286. 'registration.allow_registration_as_teacher'
  287. ) != 'false'
  288. ) {
  289. if (in_array('status', $allowedFields)) {
  290. $form->addElement(
  291. 'radio',
  292. 'status',
  293. get_lang('Profile'),
  294. get_lang('RegStudent'),
  295. STUDENT
  296. );
  297. $form->addElement(
  298. 'radio',
  299. 'status',
  300. null,
  301. get_lang('RegAdmin'),
  302. COURSEMANAGER
  303. );
  304. }
  305. }
  306. $captcha = api_get_setting('allow_captcha');
  307. $allowCaptcha = $captcha === 'true';
  308. if ($allowCaptcha) {
  309. $ajax = api_get_path(WEB_AJAX_PATH).'form.ajax.php?a=get_captcha';
  310. $options = array(
  311. 'width' => 220,
  312. 'height' => 90,
  313. 'callback' => $ajax.'&var='.basename(__FILE__, '.php'),
  314. 'sessionVar' => basename(__FILE__, '.php'),
  315. 'imageOptions' => array(
  316. 'font_size' => 20,
  317. 'font_path' => api_get_path(SYS_FONTS_PATH) . 'opensans/',
  318. 'font_file' => 'OpenSans-Regular.ttf',
  319. //'output' => 'gif'
  320. )
  321. );
  322. $captcha_question = $form->addElement('CAPTCHA_Image', 'captcha_question', '', $options);
  323. $form->addElement('static', null, null, get_lang('ClickOnTheImageForANewOne'));
  324. $form->addElement('text', 'captcha', get_lang('EnterTheLettersYouSee'), array('size' => 40));
  325. $form->addRule('captcha', get_lang('EnterTheCharactersYouReadInTheImage'), 'required', null, 'client');
  326. $form->addRule('captcha', get_lang('TheTextYouEnteredDoesNotMatchThePicture'), 'CAPTCHA', $captcha_question);
  327. }
  328. // EXTENDED FIELDS
  329. if (api_get_setting('profile.extended_profile') == 'true' &&
  330. api_get_setting_in_list('registration.extendedprofile_registration', 'mycomptetences') == 'true'
  331. ) {
  332. $form->addHtmlEditor(
  333. 'competences',
  334. get_lang('MyCompetences'),
  335. false,
  336. false,
  337. array(
  338. 'ToolbarSet' => 'Register',
  339. 'Width' => '100%',
  340. 'Height' => '130',
  341. )
  342. );
  343. }
  344. if (api_get_setting('profile.extended_profile') == 'true' &&
  345. api_get_setting_in_list('registration.extendedprofile_registration', 'mydiplomas') == 'true'
  346. ) {
  347. $form->addHtmlEditor(
  348. 'diplomas',
  349. get_lang('MyDiplomas'),
  350. false,
  351. false,
  352. array(
  353. 'ToolbarSet' => 'Register',
  354. 'Width' => '100%',
  355. 'Height' => '130',
  356. )
  357. );
  358. }
  359. if (api_get_setting('profile.extended_profile') == 'true' &&
  360. api_get_setting_in_list('registration.extendedprofile_registration', 'myteach') == 'true'
  361. ) {
  362. $form->addHtmlEditor(
  363. 'teach',
  364. get_lang('MyTeach'),
  365. false,
  366. false,
  367. array(
  368. 'ToolbarSet' => 'Register',
  369. 'Width' => '100%',
  370. 'Height' => '130',
  371. )
  372. );
  373. }
  374. if (api_get_setting('profile.extended_profile') == 'true' &&
  375. api_get_setting_in_list('registration.extendedprofile_registration', 'mypersonalopenarea') == 'true'
  376. ) {
  377. $form->addHtmlEditor(
  378. 'openarea',
  379. get_lang('MyPersonalOpenArea'),
  380. false,
  381. false,
  382. array(
  383. 'ToolbarSet' => 'Register',
  384. 'Width' => '100%',
  385. 'Height' => '130',
  386. )
  387. );
  388. }
  389. if (api_get_setting('profile.extended_profile') == 'true') {
  390. if (api_get_setting_in_list('registration.extendedprofile_registration', 'mycomptetences') == 'true' &&
  391. api_get_setting_in_list('registration.extendedprofile_registrationrequired', 'mycomptetences') == 'true'
  392. ) {
  393. $form->addRule('competences', get_lang('ThisFieldIsRequired'), 'required');
  394. }
  395. if (api_get_setting_in_list('registration.extendedprofile_registration', 'mydiplomas') == 'true' &&
  396. api_get_setting_in_list('registration.extendedprofile_registrationrequired', 'mydiplomas') == 'true'
  397. ) {
  398. $form->addRule('diplomas', get_lang('ThisFieldIsRequired'), 'required');
  399. }
  400. if (api_get_setting_in_list('registration.extendedprofile_registration', 'myteach') == 'true' &&
  401. api_get_setting_in_list('registration.extendedprofile_registrationrequired', 'myteach') == 'true'
  402. ) {
  403. $form->addRule('teach', get_lang('ThisFieldIsRequired'), 'required');
  404. }
  405. if (api_get_setting_in_list('registration.extendedprofile_registration', 'mypersonalopenarea') == 'true' &&
  406. api_get_setting_in_list('registration.extendedprofile_registrationrequired', 'mypersonalopenarea') == 'true'
  407. ) {
  408. $form->addRule('openarea', get_lang('ThisFieldIsRequired'), 'required');
  409. }
  410. }
  411. // EXTRA FIELDS
  412. if (array_key_exists('extra_fields', $allowedFields) || in_array('extra_fields', $allowedFields)) {
  413. $extraField = new ExtraField('user');
  414. $extraFieldList = isset($allowedFields['extra_fields']) && is_array($allowedFields['extra_fields']) ? $allowedFields['extra_fields'] : [];
  415. $returnParams = $extraField->addElements($form, 0, [], false, false, $extraFieldList);
  416. }
  417. }
  418. if (isset($_SESSION['user_language_choice']) && $_SESSION['user_language_choice'] != '') {
  419. $defaults['language'] = $_SESSION['user_language_choice'];
  420. } else {
  421. $defaults['language'] = api_get_setting('language.platform_language');
  422. }
  423. if (!empty($_GET['username'])) {
  424. $defaults['username'] = Security::remove_XSS($_GET['username']);
  425. }
  426. if (!empty($_GET['email'])) {
  427. $defaults['email'] = Security::remove_XSS($_GET['email']);
  428. }
  429. if (!empty($_GET['phone'])) {
  430. $defaults['phone'] = Security::remove_XSS($_GET['phone']);
  431. }
  432. if (api_get_setting('openid_authentication') == 'true' && !empty($_GET['openid'])) {
  433. $defaults['openid'] = Security::remove_XSS($_GET['openid']);
  434. }
  435. $defaults['status'] = STUDENT;
  436. $defaults['extra_mail_notify_invitation'] = 1;
  437. $defaults['extra_mail_notify_message'] = 1;
  438. $defaults['extra_mail_notify_group_message'] = 1;
  439. $form->setDefaults($defaults);
  440. $content = null;
  441. if (!CustomPages::enabled()) {
  442. // Load terms & conditions from the current lang
  443. if (api_get_setting('registration.allow_terms_conditions') == 'true') {
  444. $get = array_keys($_GET);
  445. if (isset($get)) {
  446. if (isset($get[0]) && $get[0] == 'legal') {
  447. $language = api_get_language_isocode();
  448. $language = api_get_language_id($language);
  449. $term_preview = LegalManager::get_last_condition($language);
  450. if (!$term_preview) {
  451. //look for the default language
  452. $language = api_get_setting('language.platform_language');
  453. $language = api_get_language_id($language);
  454. $term_preview = LegalManager::get_last_condition($language);
  455. }
  456. $tool_name = get_lang('TermsAndConditions');
  457. Display :: display_header($tool_name);
  458. if (!empty($term_preview['content'])) {
  459. echo $term_preview['content'];
  460. } else {
  461. echo get_lang('ComingSoon');
  462. }
  463. Display :: display_footer();
  464. exit;
  465. }
  466. }
  467. }
  468. $tool_name = get_lang('Registration', null, (!empty($_POST['language'])?$_POST['language']: $_user['language']));
  469. if (api_get_setting(
  470. 'registration.allow_terms_conditions'
  471. ) == 'true' && $user_already_registered_show_terms
  472. ) {
  473. $tool_name = get_lang('TermsAndConditions');
  474. }
  475. $home = api_get_path(SYS_APP_PATH).'home/';
  476. if (api_is_multiple_url_enabled()) {
  477. $access_url_id = api_get_current_access_url_id();
  478. if ($access_url_id != -1) {
  479. $url_info = api_get_access_url($access_url_id);
  480. $url = api_remove_trailing_slash(preg_replace('/https?:\/\//i', '', $url_info['url']));
  481. $clean_url = api_replace_dangerous_char($url);
  482. $clean_url = str_replace('/', '-', $clean_url);
  483. $clean_url .= '/';
  484. $home_old = api_get_path(SYS_APP_PATH).'home/';
  485. $home = api_get_path(SYS_APP_PATH).'home/'.$clean_url;
  486. }
  487. }
  488. if (file_exists($home.'register_top_'.$user_selected_language.'.html')) {
  489. $home_top_temp = @(string)file_get_contents($home.'register_top_'.$user_selected_language.'.html');
  490. $open = str_replace('{rel_path}', api_get_path(REL_PATH), $home_top_temp);
  491. $open = api_to_system_encoding($open, api_detect_encoding(strip_tags($open)));
  492. if (!empty($open)) {
  493. $content = '<div class="well_border">'.$open.'</div>';
  494. }
  495. }
  496. // Forbidden to self-register
  497. if ($isNotAllowedHere) {
  498. api_not_allowed(true, get_lang('RegistrationDisabled'));
  499. }
  500. if (api_get_setting('registration.allow_registration') == 'approval') {
  501. $content .= Display::return_message(get_lang('YourAccountHasToBeApproved'));
  502. }
  503. //if openid was not found
  504. if (!empty($_GET['openid_msg']) && $_GET['openid_msg'] == 'idnotfound') {
  505. $content .= Display::return_message(get_lang('OpenIDCouldNotBeFoundPleaseRegister'));
  506. }
  507. }
  508. // Terms and conditions
  509. if (api_get_setting('registration.allow_terms_conditions') == 'true') {
  510. if (!api_is_platform_admin()) {
  511. if (api_get_setting('show_terms_if_profile_completed') === 'true') {
  512. $userInfo = api_get_user_info();
  513. if ($userInfo && $userInfo['status'] != ANONYMOUS) {
  514. if ((int)$userInfo['profile_completed'] !== 1) {
  515. api_not_allowed(true);
  516. }
  517. }
  518. }
  519. }
  520. $language = api_get_language_isocode();
  521. $language = api_get_language_id($language);
  522. $term_preview = LegalManager::get_last_condition($language);
  523. if (!$term_preview) {
  524. //we load from the platform
  525. $language = api_get_setting('language.platform_language');
  526. $language = api_get_language_id($language);
  527. $term_preview = LegalManager::get_last_condition($language);
  528. //if is false we load from english
  529. if (!$term_preview) {
  530. $language = api_get_language_id('english'); //this must work
  531. $term_preview = LegalManager::get_last_condition($language);
  532. }
  533. }
  534. // Version and language
  535. $form->addElement('hidden', 'legal_accept_type', $term_preview['version'].':'.$term_preview['language_id']);
  536. $form->addElement('hidden', 'legal_info', $term_preview['id'].':'.$term_preview['language_id']);
  537. if ($term_preview['type'] == 1) {
  538. $form->addElement(
  539. 'checkbox',
  540. 'legal_accept',
  541. null,
  542. get_lang('IHaveReadAndAgree').'&nbsp;<a href="inscription.php?legal" target="_blank">'.get_lang('TermsAndConditions').'</a>'
  543. );
  544. $form->addRule('legal_accept', get_lang('ThisFieldIsRequired'), 'required');
  545. } else {
  546. $preview = LegalManager::show_last_condition($term_preview);
  547. $form->addElement('label', null, $preview);
  548. }
  549. }
  550. $form->addButtonCreate(get_lang('RegisterUser'));
  551. $course_code_redirect = Session::read('course_redirect');
  552. if ($form->validate()) {
  553. $values = $form->getSubmitValues(1);
  554. // Make *sure* the login isn't too long
  555. if (isset($values['username'])) {
  556. $values['username'] = api_substr($values['username'], 0, USERNAME_MAX_LENGTH);
  557. }
  558. if (api_get_setting(
  559. 'registration.allow_registration_as_teacher'
  560. ) == 'false'
  561. ) {
  562. $values['status'] = STUDENT;
  563. }
  564. if (empty($values['official_code']) && !empty($values['username'])) {
  565. $values['official_code'] = api_strtoupper($values['username']);
  566. }
  567. if (api_get_setting('profile.login_is_email') == 'true') {
  568. $values['username'] = $values['email'];
  569. }
  570. if ($user_already_registered_show_terms &&
  571. api_get_setting('registration.allow_terms_conditions') == 'true'
  572. ) {
  573. $user_id = $_SESSION['term_and_condition']['user_id'];
  574. $is_admin = UserManager::is_admin($user_id);
  575. Session::write('is_platformAdmin', $is_admin);
  576. } else {
  577. // Moved here to include extra fields when creating a user. Formerly placed after user creation
  578. // Register extra fields
  579. $extras = array();
  580. foreach ($values as $key => $value) {
  581. if (substr($key, 0, 6) == 'extra_') {
  582. //an extra field
  583. $extras[substr($key, 6)] = $value;
  584. } elseif (strpos($key, 'remove_extra_') !== false) {
  585. $extra_value = Security::filter_filename(urldecode(key($value)));
  586. // To remove from user_field_value and folder
  587. UserManager::update_extra_field_value(
  588. $user_id,
  589. substr($key, 13),
  590. $extra_value
  591. );
  592. }
  593. }
  594. $status = isset($values['status']) ? $values['status'] : STUDENT;
  595. $phone = isset($values['phone']) ? $values['phone'] : null;
  596. $values['language'] = isset($values['language']) ? $values['language'] : api_get_interface_language();
  597. $values['address'] = isset($values['address']) ? $values['address'] : '';
  598. // Creates a new user
  599. $user_id = UserManager::create_user(
  600. $values['firstname'],
  601. $values['lastname'],
  602. $status,
  603. $values['email'],
  604. $values['username'],
  605. $values['pass1'],
  606. $values['official_code'],
  607. $values['language'],
  608. $phone,
  609. null,
  610. PLATFORM_AUTH_SOURCE,
  611. null,
  612. 1,
  613. 0,
  614. $extras,
  615. null,
  616. true,
  617. false,
  618. $values['address'],
  619. false,
  620. $form
  621. );
  622. //update the extra fields
  623. $count_extra_field = count($extras);
  624. if ($count_extra_field > 0 && is_integer($user_id)) {
  625. foreach ($extras as $key => $value) {
  626. // For array $value -> if exists key 'tmp_name' then must not be empty
  627. // This avoid delete from user field value table when doesn't upload a file
  628. if (is_array($value)) {
  629. if (array_key_exists('tmp_name', $value) && empty($value['tmp_name'])) {
  630. //Nothing to do
  631. } else {
  632. if (array_key_exists('tmp_name', $value)) {
  633. $value['tmp_name'] = Security::filter_filename($value['tmp_name']);
  634. }
  635. if (array_key_exists('name', $value)) {
  636. $value['name'] = Security::filter_filename($value['name']);
  637. }
  638. UserManager::update_extra_field_value($user_id, $key, $value);
  639. }
  640. } else {
  641. UserManager::update_extra_field_value($user_id, $key, $value);
  642. }
  643. }
  644. }
  645. if ($user_id) {
  646. // Storing the extended profile
  647. $store_extended = false;
  648. $sql = "UPDATE ".Database::get_main_table(TABLE_MAIN_USER)." SET ";
  649. if (api_get_setting('profile.extended_profile') == 'true' &&
  650. api_get_setting_in_list('extendedprofile_registration', 'mycomptetences') == 'true'
  651. ) {
  652. $sql_set[] = "competences = '".Database::escape_string($values['competences'])."'";
  653. $store_extended = true;
  654. }
  655. if (api_get_setting('profile.extended_profile') == 'true' &&
  656. api_get_setting_in_list('extendedprofile_registration', 'mydiplomas') == 'true'
  657. ) {
  658. $sql_set[] = "diplomas = '".Database::escape_string($values['diplomas'])."'";
  659. $store_extended = true;
  660. }
  661. if (api_get_setting('profile.extended_profile') == 'true' &&
  662. api_get_setting_in_list('extendedprofile_registration', 'myteach') == 'true'
  663. ) {
  664. $sql_set[] = "teach = '".Database::escape_string($values['teach'])."'";
  665. $store_extended = true;
  666. }
  667. if (api_get_setting('profile.extended_profile') == 'true' &&
  668. api_get_setting_in_list('extendedprofile_registration', 'mypersonalopenarea') == 'true'
  669. ) {
  670. $sql_set[] = "openarea = '".Database::escape_string($values['openarea'])."'";
  671. $store_extended = true;
  672. }
  673. if ($store_extended) {
  674. $sql .= implode(',', $sql_set);
  675. $sql .= " WHERE user_id = ".intval($user_id)."";
  676. Database::query($sql);
  677. }
  678. // Saving user to course if it was set.
  679. if (!empty($course_code_redirect)) {
  680. $course_info = api_get_course_info($course_code_redirect);
  681. if (!empty($course_info)) {
  682. if (in_array(
  683. $course_info['visibility'],
  684. array(
  685. COURSE_VISIBILITY_OPEN_PLATFORM,
  686. COURSE_VISIBILITY_OPEN_WORLD
  687. )
  688. )
  689. ) {
  690. CourseManager::subscribe_user(
  691. $user_id,
  692. $course_info['code']
  693. );
  694. }
  695. }
  696. }
  697. /* If the account has to be approved then we set the account to inactive,
  698. sent a mail to the platform admin and exit the page.*/
  699. if (api_get_setting(
  700. 'registration.allow_registration'
  701. ) == 'approval'
  702. ) {
  703. $TABLE_USER = Database::get_main_table(TABLE_MAIN_USER);
  704. // 1. set account inactive
  705. $sql = "UPDATE $TABLE_USER SET active='0' WHERE user_id = ".$user_id;
  706. Database::query($sql);
  707. // 2. Send mail to all platform admin
  708. $emailsubject = get_lang('ApprovalForNewAccount', null, $values['language']).': '.$values['username'];
  709. $emailbody = get_lang('ApprovalForNewAccount', null, $values['language'])."\n";
  710. $emailbody .= get_lang('UserName', null, $values['language']).': '.$values['username']."\n";
  711. if (api_is_western_name_order()) {
  712. $emailbody .= get_lang('FirstName', null, $values['language']).': '.$values['firstname']."\n";
  713. $emailbody .= get_lang('LastName', null, $values['language']).': '.$values['lastname']."\n";
  714. } else {
  715. $emailbody .= get_lang('LastName', null, $values['language']).': '.$values['lastname']."\n";
  716. $emailbody .= get_lang('FirstName', null, $values['language']).': '.$values['firstname']."\n";
  717. }
  718. $emailbody .= get_lang('Email', null, $values['language']).': '.$values['email']."\n";
  719. $emailbody .= get_lang('Status', null, $values['language']).': '.$values['status']."\n\n";
  720. $url_edit = Display::url(
  721. api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user_id,
  722. api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user_id
  723. );
  724. $emailbody .= get_lang('ManageUser', null, $values['language']).": $url_edit";
  725. $admins = UserManager::get_all_administrators();
  726. foreach ($admins as $admin_info) {
  727. MessageManager::send_message(
  728. $admin_info['user_id'],
  729. $emailsubject,
  730. $emailbody,
  731. [],
  732. [],
  733. null,
  734. null,
  735. null,
  736. null,
  737. $user_id
  738. );
  739. }
  740. // 3. exit the page
  741. unset($user_id);
  742. Display::display_header($tool_name);
  743. echo Display::page_header($tool_name);
  744. echo $content;
  745. Display::display_footer();
  746. exit;
  747. }
  748. }
  749. }
  750. // Terms & Conditions
  751. if (api_get_setting('registration.allow_terms_conditions') == 'true') {
  752. // Update the terms & conditions.
  753. if (isset($values['legal_accept_type'])) {
  754. $cond_array = explode(':', $values['legal_accept_type']);
  755. if (!empty($cond_array[0]) && !empty($cond_array[1])) {
  756. $time = time();
  757. $condition_to_save = intval($cond_array[0]).':'.intval($cond_array[1]).':'.$time;
  758. UserManager::update_extra_field_value($user_id, 'legal_accept', $condition_to_save);
  759. $bossList = UserManager::getStudentBossList($user_id);
  760. if ($bossList) {
  761. $bossList = array_column($bossList, 'boss_id');
  762. $currentUserInfo = api_get_user_info($user_id);
  763. foreach ($bossList as $bossId) {
  764. $subjectEmail = sprintf(
  765. get_lang('UserXSignedTheAgreement'),
  766. $currentUserInfo['complete_name']
  767. );
  768. $contentEmail = sprintf(
  769. get_lang('UserXSignedTheAgreementTheY'),
  770. $currentUserInfo['complete_name'],
  771. api_get_local_time($time)
  772. );
  773. MessageManager::send_message_simple(
  774. $bossId,
  775. $subjectEmail,
  776. $contentEmail
  777. );
  778. }
  779. }
  780. }
  781. }
  782. $values = api_get_user_info($user_id);
  783. }
  784. /* SESSION REGISTERING */
  785. /* @todo move this in a function */
  786. $_user['firstName'] = stripslashes($values['firstname']);
  787. $_user['lastName'] = stripslashes($values['lastname']);
  788. $_user['mail'] = $values['email'];
  789. $_user['language'] = $values['language'];
  790. $_user['user_id'] = $user_id;
  791. $is_allowedCreateCourse = isset($values['status']) && $values['status'] == 1;
  792. $usersCanCreateCourse = api_get_setting(
  793. 'course.allow_users_to_create_courses'
  794. ) == 'true';
  795. Session::write('_user', $_user);
  796. Session::write('is_allowedCreateCourse', $is_allowedCreateCourse);
  797. // Stats
  798. Event::event_login($user_id);
  799. // last user login date is now
  800. $user_last_login_datetime = 0; // used as a unix timestamp it will correspond to : 1 1 1970
  801. Session::write('user_last_login_datetime', $user_last_login_datetime);
  802. $recipient_name = api_get_person_name($values['firstname'], $values['lastname']);
  803. $text_after_registration =
  804. '<p>'.
  805. get_lang('Dear', null, $_user['language']).' '.
  806. stripslashes(Security::remove_XSS($recipient_name)).',<br /><br />'.
  807. get_lang('PersonalSettings',null,$_user['language']).".</p>";
  808. $form_data = array(
  809. 'button' => Display::button(
  810. 'next',
  811. get_lang('Next', null, $_user['language']),
  812. array('class' => 'btn btn-primary btn-large')
  813. ),
  814. 'message' => '',
  815. 'action' => api_get_path(WEB_PATH).'user_portal.php',
  816. 'go_button' => ''
  817. );
  818. if (api_get_setting('registration.allow_terms_conditions') === 'true' && $user_already_registered_show_terms) {
  819. if (api_get_setting('load_term_conditions_section') === 'login') {
  820. $form_data['action'] = api_get_path(WEB_PATH).'user_portal.php';
  821. } else {
  822. $courseInfo = api_get_course_info();
  823. if (!empty($courseInfo)) {
  824. $form_data['action'] = $courseInfo['course_public_url'].'?id_session='.api_get_session_id();
  825. $cidReset = true;
  826. Session::erase('_course');
  827. Session::erase('_cid');
  828. } else {
  829. $form_data['action'] = api_get_path(WEB_PATH).'user_portal.php';
  830. }
  831. }
  832. } else {
  833. if (!empty($values['email'])) {
  834. $text_after_registration.= '<p>'.get_lang('MailHasBeenSent', null, $_user['language']).'.</p>';
  835. }
  836. if ($is_allowedCreateCourse) {
  837. if ($usersCanCreateCourse) {
  838. $form_data['message'] = '<p>'. get_lang('NowGoCreateYourCourse', null, $_user['language']). "</p>";
  839. }
  840. $form_data['action'] = api_get_path(WEB_CODE_PATH).'create_course/add_course.php';
  841. if (api_get_setting('course.course_validation') === 'true') {
  842. $form_data['button'] = Display::button(
  843. 'next',
  844. get_lang('CreateCourseRequest', null, $_user['language']),
  845. array('class' => 'btn btn-primary btn-large')
  846. );
  847. } else {
  848. $form_data['button'] = Display::button(
  849. 'next',
  850. get_lang('CourseCreate', null, $_user['language']),
  851. array('class' => 'btn btn-primary btn-large')
  852. );
  853. $form_data['go_button'] = '&nbsp;&nbsp;<a href="'.api_get_path(WEB_PATH).'index.php'.'">'.
  854. Display::span(
  855. get_lang('Next', null, $_user['language']),
  856. array('class' => 'btn btn-primary btn-large')
  857. ).'</a>';
  858. }
  859. } else {
  860. if (api_get_setting('display.allow_students_to_browse_courses') == 'true') {
  861. $form_data['action'] = 'courses.php?action=subscribe';
  862. $form_data['message'] = '<p>'. get_lang('NowGoChooseYourCourses', null, $_user['language']). ".</p>";
  863. } else {
  864. $form_data['action'] = api_get_path(WEB_PATH).'user_portal.php';
  865. }
  866. $form_data['button'] = Display::button(
  867. 'next',
  868. get_lang('Next', null, $_user['language']),
  869. array('class' => 'btn btn-primary btn-large')
  870. );
  871. }
  872. }
  873. $form_data = CourseManager::redirectToCourse($form_data);
  874. $form_register = new FormValidator('form_register', 'post', $form_data['action']);
  875. if (!empty($form_data['message'])) {
  876. $form_register->addElement('html', $form_data['message'].'<br /><br />');
  877. }
  878. if ($usersCanCreateCourse) {
  879. $form_register->addElement('html', $form_data['button']);
  880. } else {
  881. $form_register->addElement('html', $form_data['go_button']);
  882. }
  883. $text_after_registration .= $form_register->returnForm();
  884. // Just in case
  885. Session::erase('course_redirect');
  886. Session::erase('exercise_redirect');
  887. if (CustomPages::enabled()) {
  888. CustomPages::display(
  889. CustomPages::REGISTRATION_FEEDBACK,
  890. array('info' => $text_after_registration)
  891. );
  892. } else {
  893. //$tpl = new Template($tool_name);
  894. echo Container::getTemplating()->render(
  895. '@template_style/auth/inscription.html.twig',
  896. [
  897. 'inscription_content' => $content,
  898. 'text_after_registration' => $text_after_registration,
  899. 'hide_header' => $hideHeaders,
  900. ]
  901. );
  902. }
  903. } else {
  904. // Custom pages
  905. if (CustomPages::enabled()) {
  906. CustomPages::display(
  907. CustomPages::REGISTRATION, array('form' => $form)
  908. );
  909. } else {
  910. if (!api_is_anonymous()) {
  911. // Saving user to course if it was set.
  912. if (!empty($course_code_redirect)) {
  913. $course_info = api_get_course_info($course_code_redirect);
  914. if (!empty($course_info)) {
  915. if (in_array(
  916. $course_info['visibility'],
  917. array(
  918. COURSE_VISIBILITY_OPEN_PLATFORM,
  919. COURSE_VISIBILITY_OPEN_WORLD
  920. )
  921. )
  922. ) {
  923. CourseManager::subscribe_user(
  924. $user_id,
  925. $course_info['code']
  926. );
  927. }
  928. }
  929. }
  930. CourseManager::redirectToCourse([]);
  931. }
  932. //$tpl = new Template($tool_name);
  933. echo Container::getTemplating()->render(
  934. '@template_style/auth/inscription.html.twig',
  935. [
  936. 'inscription_header' =>Display::page_header($tool_name),
  937. 'inscription_content' => $content,
  938. 'hide_header' => $hideHeaders,
  939. 'form', $form->returnForm()
  940. ]
  941. );
  942. }
  943. }