inscription.php 37 KB

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