profile.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\UserBundle\Entity\User;
  4. use ChamiloSession as Session;
  5. /**
  6. * This file displays the user's profile,
  7. * optionally it allows users to modify their profile as well.
  8. *
  9. * See inc/conf/profile.conf.php to modify settings
  10. *
  11. * @package chamilo.auth
  12. */
  13. $cidReset = true;
  14. require_once __DIR__.'/../inc/global.inc.php';
  15. $this_section = SECTION_MYPROFILE;
  16. $allowSocialTool = api_get_setting('allow_social_tool') == 'true';
  17. if ($allowSocialTool) {
  18. $this_section = SECTION_SOCIAL;
  19. }
  20. $logInfo = [
  21. 'tool' => 'profile',
  22. 'tool_id' => 0,
  23. 'tool_id_detail' => 0,
  24. 'action' => $this_section,
  25. ];
  26. Event::registerLog($logInfo);
  27. $_SESSION['this_section'] = $this_section;
  28. if (!(isset($_user['user_id']) && $_user['user_id']) || api_is_anonymous($_user['user_id'], true)) {
  29. api_not_allowed(true);
  30. }
  31. $htmlHeadXtra[] = api_get_password_checker_js('#username', '#password1');
  32. $htmlHeadXtra[] = api_get_css_asset('cropper/dist/cropper.min.css');
  33. $htmlHeadXtra[] = api_get_asset('cropper/dist/cropper.min.js');
  34. $htmlHeadXtra[] = '<script>
  35. $(function() {
  36. $("#id_generate_api_key").on("click", function (e) {
  37. e.preventDefault();
  38. $.ajax({
  39. contentType: "application/x-www-form-urlencoded",
  40. type: "POST",
  41. url: "'.api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=generate_api_key",
  42. data: "num_key_id="+"",
  43. success: function(datos) {
  44. $("#div_api_key").html(datos);
  45. }
  46. });
  47. });
  48. });
  49. function confirmation(name) {
  50. if (confirm("'.get_lang('AreYouSureToDeleteJS', '').' " + name + " ?")) {
  51. document.forms["profile"].submit();
  52. } else {
  53. return false;
  54. }
  55. }
  56. function show_image(image,width,height) {
  57. width = parseInt(width) + 20;
  58. height = parseInt(height) + 20;
  59. window_x = window.open(image,\'windowX\',\'width=\'+ width + \', height=\'+ height + \'\');
  60. }
  61. </script>';
  62. $jquery_ready_content = '';
  63. if (api_get_setting('allow_message_tool') === 'true') {
  64. $jquery_ready_content = <<<EOF
  65. $(".message-content .message-delete").click(function(){
  66. $(this).parents(".message-content").animate({ opacity: "hide" }, "slow");
  67. $(".message-view").animate({ opacity: "show" }, "slow");
  68. });
  69. EOF;
  70. }
  71. $tool_name = is_profile_editable() ? get_lang('ModifProfile') : get_lang('ViewProfile');
  72. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  73. /*
  74. * Get initial values for all fields.
  75. */
  76. $user_data = api_get_user_info(
  77. api_get_user_id(),
  78. false,
  79. false,
  80. false,
  81. false,
  82. true,
  83. true
  84. );
  85. $array_list_key = UserManager::get_api_keys(api_get_user_id());
  86. $id_temp_key = UserManager::get_api_key_id(api_get_user_id(), 'dokeos');
  87. $value_array = $array_list_key[$id_temp_key];
  88. $user_data['api_key_generate'] = $value_array;
  89. if ($user_data !== false) {
  90. if (api_get_setting('login_is_email') == 'true') {
  91. $user_data['username'] = $user_data['email'];
  92. }
  93. if (is_null($user_data['language'])) {
  94. $user_data['language'] = api_get_setting('platformLanguage');
  95. }
  96. }
  97. /*
  98. * Initialize the form.
  99. */
  100. $form = new FormValidator('profile');
  101. if (api_is_western_name_order()) {
  102. // FIRST NAME and LAST NAME
  103. $form->addElement('text', 'firstname', get_lang('FirstName'), ['size' => 40]);
  104. $form->addElement('text', 'lastname', get_lang('LastName'), ['size' => 40]);
  105. } else {
  106. // LAST NAME and FIRST NAME
  107. $form->addElement('text', 'lastname', get_lang('LastName'), ['size' => 40]);
  108. $form->addElement('text', 'firstname', get_lang('FirstName'), ['size' => 40]);
  109. }
  110. if (api_get_setting('profile', 'name') !== 'true') {
  111. $form->freeze(['lastname', 'firstname']);
  112. }
  113. $form->applyFilter(['lastname', 'firstname'], 'stripslashes');
  114. $form->applyFilter(['lastname', 'firstname'], 'trim');
  115. $form->applyFilter(['lastname', 'firstname'], 'html_filter');
  116. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  117. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  118. // USERNAME
  119. $form->addElement(
  120. 'text',
  121. 'username',
  122. get_lang('UserName'),
  123. [
  124. 'id' => 'username',
  125. 'maxlength' => USERNAME_MAX_LENGTH,
  126. 'size' => USERNAME_MAX_LENGTH,
  127. ]
  128. );
  129. if (api_get_setting('profile', 'login') !== 'true' || api_get_setting('login_is_email') == 'true') {
  130. $form->freeze('username');
  131. }
  132. $form->applyFilter('username', 'stripslashes');
  133. $form->applyFilter('username', 'trim');
  134. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  135. $form->addRule('username', get_lang('UsernameWrong'), 'username');
  136. $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
  137. // OFFICIAL CODE
  138. if (defined('CONFVAL_ASK_FOR_OFFICIAL_CODE') && CONFVAL_ASK_FOR_OFFICIAL_CODE === true) {
  139. $form->addElement('text', 'official_code', get_lang('OfficialCode'), ['size' => 40]);
  140. if (api_get_setting('profile', 'officialcode') !== 'true') {
  141. $form->freeze('official_code');
  142. }
  143. $form->applyFilter('official_code', 'stripslashes');
  144. $form->applyFilter('official_code', 'trim');
  145. $form->applyFilter('official_code', 'html_filter');
  146. if (api_get_setting('registration', 'officialcode') === 'true' &&
  147. api_get_setting('profile', 'officialcode') === 'true'
  148. ) {
  149. $form->addRule('official_code', get_lang('ThisFieldIsRequired'), 'required');
  150. }
  151. }
  152. // EMAIL
  153. $form->addElement('email', 'email', get_lang('Email'), ['size' => 40]);
  154. if (api_get_setting('profile', 'email') !== 'true') {
  155. $form->freeze('email');
  156. }
  157. if (api_get_setting('registration', 'email') == 'true' && api_get_setting('profile', 'email') == 'true') {
  158. $form->applyFilter('email', 'stripslashes');
  159. $form->applyFilter('email', 'trim');
  160. $form->addRule('email', get_lang('ThisFieldIsRequired'), 'required');
  161. $form->addRule('email', get_lang('EmailWrong'), 'email');
  162. }
  163. // OPENID URL
  164. if (is_profile_editable() && api_get_setting('openid_authentication') == 'true') {
  165. $form->addElement('text', 'openid', get_lang('OpenIDURL'), ['size' => 40]);
  166. if (api_get_setting('profile', 'openid') !== 'true') {
  167. $form->freeze('openid');
  168. }
  169. $form->applyFilter('openid', 'trim');
  170. }
  171. // PHONE
  172. $form->addElement('text', 'phone', get_lang('Phone'), ['size' => 20]);
  173. if (api_get_setting('profile', 'phone') !== 'true') {
  174. $form->freeze('phone');
  175. }
  176. $form->applyFilter('phone', 'stripslashes');
  177. $form->applyFilter('phone', 'trim');
  178. $form->applyFilter('phone', 'html_filter');
  179. // PICTURE
  180. if (is_profile_editable() && api_get_setting('profile', 'picture') == 'true') {
  181. $form->addFile(
  182. 'picture',
  183. [
  184. $user_data['picture_uri'] != '' ? get_lang('UpdateImage') : get_lang('AddImage'),
  185. get_lang('OnlyImagesAllowed'),
  186. ],
  187. [
  188. 'id' => 'picture',
  189. 'class' => 'picture-form',
  190. 'crop_image' => true,
  191. 'crop_ratio' => '1 / 1',
  192. 'accept' => 'image/*',
  193. ]
  194. );
  195. $form->addProgress();
  196. if (!empty($user_data['picture_uri'])) {
  197. $form->addElement('checkbox', 'remove_picture', null, get_lang('DelImage'));
  198. }
  199. $allowed_picture_types = api_get_supported_image_extensions(false);
  200. $form->addRule(
  201. 'picture',
  202. get_lang('OnlyImagesAllowed').' ('.implode(', ', $allowed_picture_types).')',
  203. 'filetype',
  204. $allowed_picture_types
  205. );
  206. }
  207. // LANGUAGE
  208. $form->addSelectLanguage('language', get_lang('Language'));
  209. if (api_get_setting('profile', 'language') !== 'true') {
  210. $form->freeze('language');
  211. }
  212. // THEME
  213. if (is_profile_editable() && api_get_setting('user_selected_theme') == 'true') {
  214. $form->addElement('SelectTheme', 'theme', get_lang('Theme'));
  215. if (api_get_setting('profile', 'theme') !== 'true') {
  216. $form->freeze('theme');
  217. }
  218. $form->applyFilter('theme', 'trim');
  219. }
  220. // EXTENDED PROFILE this make the page very slow!
  221. if (api_get_setting('extended_profile') === 'true') {
  222. $width_extended_profile = 500;
  223. // MY COMPETENCES
  224. $form->addHtmlEditor(
  225. 'competences',
  226. get_lang('MyCompetences'),
  227. false,
  228. false,
  229. [
  230. 'ToolbarSet' => 'Profile',
  231. 'Width' => $width_extended_profile,
  232. 'Height' => '130',
  233. ]
  234. );
  235. // MY DIPLOMAS
  236. $form->addHtmlEditor(
  237. 'diplomas',
  238. get_lang('MyDiplomas'),
  239. false,
  240. false,
  241. [
  242. 'ToolbarSet' => 'Profile',
  243. 'Width' => $width_extended_profile,
  244. 'Height' => '130',
  245. ]
  246. );
  247. // WHAT I AM ABLE TO TEACH
  248. $form->addHtmlEditor(
  249. 'teach',
  250. get_lang('MyTeach'),
  251. false,
  252. false,
  253. [
  254. 'ToolbarSet' => 'Profile',
  255. 'Width' => $width_extended_profile,
  256. 'Height' => '130',
  257. ]
  258. );
  259. // MY PRODUCTIONS
  260. $form->addElement('file', 'production', get_lang('MyProductions'));
  261. if ($production_list = UserManager::build_production_list(api_get_user_id(), '', true)) {
  262. $form->addElement('static', 'productions_list', null, $production_list);
  263. }
  264. // MY PERSONAL OPEN AREA
  265. $form->addHtmlEditor(
  266. 'openarea',
  267. get_lang('MyPersonalOpenArea'),
  268. false,
  269. false,
  270. [
  271. 'ToolbarSet' => 'Profile',
  272. 'Width' => $width_extended_profile,
  273. 'Height' => '350',
  274. ]
  275. );
  276. // openarea is untrimmed for maximum openness
  277. $form->applyFilter(['competences', 'diplomas', 'teach', 'openarea'], 'stripslashes');
  278. $form->applyFilter(['competences', 'diplomas', 'teach'], 'trim');
  279. }
  280. // PASSWORD, if auth_source is platform
  281. if (is_platform_authentication() &&
  282. is_profile_editable() &&
  283. api_get_setting('profile', 'password') == 'true'
  284. ) {
  285. $form->addElement('password', 'password0', [get_lang('CurrentPassword'), get_lang('Enter2passToChange')], ['size' => 40]);
  286. $form->addElement('password', 'password1', get_lang('NewPass'), ['id' => 'password1', 'size' => 40]);
  287. $checkPass = api_get_setting('allow_strength_pass_checker');
  288. if ($checkPass == 'true') {
  289. $form->addElement('label', null, '<div id="password_progress"></div>');
  290. }
  291. $form->addElement('password', 'password2', get_lang('Confirmation'), ['size' => 40]);
  292. // user must enter identical password twice so we can prevent some user errors
  293. $form->addRule(['password1', 'password2'], get_lang('PassTwo'), 'compare');
  294. $form->addPasswordRule('password1');
  295. }
  296. $extraField = new ExtraField('user');
  297. $return = $extraField->addElements(
  298. $form,
  299. api_get_user_id(),
  300. [],
  301. false,
  302. false,
  303. [],
  304. [],
  305. false,
  306. false,
  307. [],
  308. [],
  309. [],
  310. false,
  311. [],
  312. [],
  313. true
  314. );
  315. $jquery_ready_content = $return['jquery_ready_content'];
  316. // the $jquery_ready_content variable collects all functions that
  317. // will be load in the $(document).ready javascript function
  318. $htmlHeadXtra[] = '<script>
  319. $(function() {
  320. '.$jquery_ready_content.'
  321. });
  322. </script>';
  323. if (api_get_setting('profile', 'apikeys') == 'true') {
  324. $form->addElement('html', '<div id="div_api_key">');
  325. $form->addElement(
  326. 'text',
  327. 'api_key_generate',
  328. get_lang('MyApiKey'),
  329. ['size' => 40, 'id' => 'id_api_key_generate']
  330. );
  331. $form->addElement('html', '</div>');
  332. $form->addButton(
  333. 'generate_api_key',
  334. get_lang('GenerateApiKey'),
  335. 'cogs',
  336. 'default',
  337. 'default',
  338. null,
  339. ['id' => 'id_generate_api_key']
  340. );
  341. }
  342. // SUBMIT
  343. if (is_profile_editable()) {
  344. $form->addButtonUpdate(get_lang('SaveSettings'), 'apply_change');
  345. } else {
  346. $form->freeze();
  347. }
  348. // Student cannot modified their user conditions
  349. $extraConditions = api_get_configuration_value('show_conditions_to_user');
  350. if ($extraConditions && isset($extraConditions['conditions'])) {
  351. $extraConditions = $extraConditions['conditions'];
  352. foreach ($extraConditions as $condition) {
  353. $element = $form->getElement('extra_'.$condition['variable']);
  354. if ($element) {
  355. $element->freeze();
  356. }
  357. }
  358. }
  359. $form->setDefaults($user_data);
  360. $filtered_extension = false;
  361. if ($form->validate()) {
  362. $hook = HookUpdateUser::create();
  363. if ($hook) {
  364. $hook->notifyUpdateUser(HOOK_EVENT_TYPE_PRE);
  365. }
  366. $wrong_current_password = false;
  367. $user_data = $form->getSubmitValues(1);
  368. /** @var User $user */
  369. $user = UserManager::getRepository()->find(api_get_user_id());
  370. // set password if a new one was provided
  371. $validPassword = false;
  372. $passwordWasChecked = false;
  373. if ($user &&
  374. (!empty($user_data['password0']) &&
  375. !empty($user_data['password1'])) ||
  376. (!empty($user_data['password0']) &&
  377. api_get_setting('profile', 'email') == 'true')
  378. ) {
  379. $passwordWasChecked = true;
  380. $validPassword = UserManager::isPasswordValid(
  381. $user->getPassword(),
  382. $user_data['password0'],
  383. $user->getSalt()
  384. );
  385. if ($validPassword) {
  386. $password = $user_data['password1'];
  387. } else {
  388. Display::addFlash(
  389. Display:: return_message(
  390. get_lang('CurrentPasswordEmptyOrIncorrect'),
  391. 'warning',
  392. false
  393. )
  394. );
  395. }
  396. }
  397. $allow_users_to_change_email_with_no_password = true;
  398. if (is_platform_authentication() &&
  399. api_get_setting('allow_users_to_change_email_with_no_password') == 'false'
  400. ) {
  401. $allow_users_to_change_email_with_no_password = false;
  402. }
  403. // If user sending the email to be changed (input available and not frozen )
  404. if (api_get_setting('profile', 'email') == 'true') {
  405. if ($allow_users_to_change_email_with_no_password) {
  406. if (!check_user_email($user_data['email'])) {
  407. $changeemail = $user_data['email'];
  408. }
  409. } else {
  410. // Normal behaviour
  411. if (!check_user_email($user_data['email']) && $validPassword) {
  412. $changeemail = $user_data['email'];
  413. }
  414. if (!check_user_email($user_data['email']) && empty($user_data['password0'])) {
  415. Display::addFlash(
  416. Display:: return_message(
  417. get_lang('ToChangeYourEmailMustTypeYourPassword'),
  418. 'error',
  419. false
  420. )
  421. );
  422. }
  423. }
  424. }
  425. // Upload picture if a new one is provided
  426. if ($_FILES['picture']['size']) {
  427. $new_picture = UserManager::update_user_picture(
  428. api_get_user_id(),
  429. $_FILES['picture']['name'],
  430. $_FILES['picture']['tmp_name'],
  431. $user_data['picture_crop_result']
  432. );
  433. if ($new_picture) {
  434. $user_data['picture_uri'] = $new_picture;
  435. Display::addFlash(
  436. Display:: return_message(
  437. get_lang('PictureUploaded'),
  438. 'normal',
  439. false
  440. )
  441. );
  442. }
  443. } elseif (!empty($user_data['remove_picture'])) {
  444. // remove existing picture if asked
  445. UserManager::deleteUserPicture(api_get_user_id());
  446. $user_data['picture_uri'] = '';
  447. }
  448. // Remove production.
  449. if (isset($user_data['remove_production']) &&
  450. is_array($user_data['remove_production'])
  451. ) {
  452. foreach (array_keys($user_data['remove_production']) as $production) {
  453. UserManager::remove_user_production(api_get_user_id(), urldecode($production));
  454. }
  455. if ($production_list = UserManager::build_production_list(api_get_user_id(), true, true)) {
  456. $form->insertElementBefore(
  457. $form->createElement('static', null, null, $production_list),
  458. 'productions_list'
  459. );
  460. }
  461. $form->removeElement('productions_list');
  462. Display::addFlash(
  463. Display:: return_message(get_lang('FileDeleted'), 'normal', false)
  464. );
  465. }
  466. // upload production if a new one is provided
  467. if (isset($_FILES['production']) && $_FILES['production']['size']) {
  468. $res = upload_user_production(api_get_user_id());
  469. if (!$res) {
  470. //it's a bit excessive to assume the extension is the reason why
  471. // upload_user_production() returned false, but it's true in most cases
  472. $filtered_extension = true;
  473. } else {
  474. Display::addFlash(
  475. Display:: return_message(
  476. get_lang('ProductionUploaded'),
  477. 'normal',
  478. false
  479. )
  480. );
  481. }
  482. }
  483. // remove values that shouldn't go in the database
  484. unset(
  485. $user_data['password0'],
  486. $user_data['password1'],
  487. $user_data['password2'],
  488. $user_data['MAX_FILE_SIZE'],
  489. $user_data['remove_picture'],
  490. $user_data['apply_change'],
  491. $user_data['email']
  492. );
  493. // Following RFC2396 (http://www.faqs.org/rfcs/rfc2396.html), a URI uses ':' as a reserved character
  494. // we can thus ensure the URL doesn't contain any scheme name by searching for ':' in the string
  495. $my_user_openid = isset($user_data['openid']) ? $user_data['openid'] : '';
  496. if (!preg_match('/^[^:]*:\/\/.*$/', $my_user_openid)) {
  497. //ensure there is at least a http:// scheme in the URI provided
  498. $user_data['openid'] = 'http://'.$my_user_openid;
  499. }
  500. $extras = [];
  501. //Checking the user language
  502. $languages = api_get_languages();
  503. if (!in_array($user_data['language'], $languages['folder'])) {
  504. $user_data['language'] = api_get_setting('platformLanguage');
  505. }
  506. $_SESSION['_user']['language'] = $user_data['language'];
  507. //Only update values that are request by the "profile" setting
  508. $profile_list = api_get_setting('profile');
  509. //Adding missing variables
  510. $available_values_to_modify = [];
  511. foreach ($profile_list as $key => $status) {
  512. if ($status == 'true') {
  513. switch ($key) {
  514. case 'login':
  515. $available_values_to_modify[] = 'username';
  516. break;
  517. case 'name':
  518. $available_values_to_modify[] = 'firstname';
  519. $available_values_to_modify[] = 'lastname';
  520. break;
  521. case 'picture':
  522. $available_values_to_modify[] = 'picture_uri';
  523. break;
  524. default:
  525. $available_values_to_modify[] = $key;
  526. break;
  527. }
  528. }
  529. }
  530. //Fixing missing variables
  531. $available_values_to_modify = array_merge(
  532. $available_values_to_modify,
  533. ['competences', 'diplomas', 'openarea', 'teach', 'openid', 'address']
  534. );
  535. // build SQL query
  536. $sql = "UPDATE $table_user SET";
  537. unset($user_data['api_key_generate']);
  538. foreach ($user_data as $key => $value) {
  539. if (substr($key, 0, 6) === 'extra_') { //an extra field
  540. continue;
  541. } elseif (strpos($key, 'remove_extra_') !== false) {
  542. } else {
  543. if (in_array($key, $available_values_to_modify)) {
  544. $sql .= " $key = '".Database::escape_string($value)."',";
  545. }
  546. }
  547. }
  548. $changePassword = false;
  549. // Change email
  550. if ($allow_users_to_change_email_with_no_password) {
  551. if (isset($changeemail) && in_array('email', $available_values_to_modify)) {
  552. $sql .= " email = '".Database::escape_string($changeemail)."' ";
  553. }
  554. if (isset($password) && in_array('password', $available_values_to_modify)) {
  555. $changePassword = true;
  556. }
  557. } else {
  558. if (isset($changeemail) && !isset($password) && in_array('email', $available_values_to_modify)) {
  559. $sql .= " email = '".Database::escape_string($changeemail)."'";
  560. } else {
  561. if (isset($password) && in_array('password', $available_values_to_modify)) {
  562. if (isset($changeemail) && in_array('email', $available_values_to_modify)) {
  563. $sql .= " email = '".Database::escape_string($changeemail)."' ";
  564. }
  565. $changePassword = true;
  566. }
  567. }
  568. }
  569. $sql = rtrim($sql, ',');
  570. if ($changePassword && !empty($password)) {
  571. UserManager::updatePassword(api_get_user_id(), $password);
  572. }
  573. if (api_get_setting('profile', 'officialcode') === 'true' &&
  574. isset($user_data['official_code'])
  575. ) {
  576. $sql .= ", official_code = '".Database::escape_string($user_data['official_code'])."'";
  577. }
  578. $sql .= " WHERE id = '".api_get_user_id()."'";
  579. Database::query($sql);
  580. $webserviceUrl = api_get_plugin_setting('logintcc', 'webservice_url');
  581. $hash = api_get_plugin_setting('logintcc', 'hash');
  582. $extraField = new ExtraFieldValue('user');
  583. $extraField->saveFieldValues($user_data);
  584. if (!empty($webserviceUrl) && !empty($hash)) {
  585. $tccUserIdData = $extraField->get_values_by_handler_and_field_variable(api_get_user_id(), 'tcc_user_id');
  586. $tccUserId = $tccUserIdData['value'];
  587. $tccHashData = $extraField->get_values_by_handler_and_field_variable(api_get_user_id(), 'tcc_hash_key');
  588. $tccHash = $tccHashData['value'];
  589. $genreData = $extraField->get_values_by_handler_and_field_variable(api_get_user_id(), 'terms_genre');
  590. $genre = $genreData['value'] == 'homme' ? 'Masculin' : 'Féminin';
  591. $codeData = $extraField->get_values_by_handler_and_field_variable(api_get_user_id(), 'terms_codepostal');
  592. $cp = $codeData['value'];
  593. $citizenshipData = $extraField->get_values_by_handler_and_field_variable(
  594. api_get_user_id(),
  595. 'terms_nationalite'
  596. );
  597. $citizenship = $citizenshipData['value'];
  598. $birthData = $extraField->get_values_by_handler_and_field_variable(api_get_user_id(), 'terms_datedenaissance');
  599. $birthDate = $birthData['value'];
  600. $countryData = $extraField->get_values_by_handler_and_field_variable(api_get_user_id(), 'terms_paysresidence');
  601. $country = $countryData['value'];
  602. $cityData = $extraField->get_values_by_handler_and_field_variable(api_get_user_id(), 'terms_ville');
  603. $city = $cityData['value'];
  604. $AddressData = $extraField->get_values_by_handler_and_field_variable(api_get_user_id(), 'terms_adresse');
  605. $Adresse = $AddressData['value'];
  606. switch ($user_data['language']) {
  607. case 'french':
  608. case 'french2':
  609. $language = 'fr-FR';
  610. break;
  611. case 'german':
  612. case 'german2':
  613. $language = 'de-DE';
  614. break;
  615. default:
  616. $language = 'fr-FR';
  617. break;
  618. }
  619. $params = [
  620. 'UserID' => $tccUserId,
  621. 'Genre' => $genre,
  622. 'Nom' => $user_data['lastname'],
  623. 'Prenom' => $user_data['firstname'],
  624. 'DateNaissance' => $birthDate,
  625. 'Langue' => $language,
  626. 'Nationalite' => $citizenship,
  627. 'Adresse' => $Adresse,
  628. 'CP' => $cp,
  629. 'PaysResidence' => $country,
  630. 'Ville' => $city,
  631. 'Email' => $user->getEmail(),
  632. 'HashKey' => $hash,
  633. ];
  634. try {
  635. $client = new GuzzleHttp\Client();
  636. $response = $client->request('POST', $webserviceUrl.'/UpdateUser', ['json' => $params]);
  637. } catch (Exception $e) {
  638. echo $e->getMessage();
  639. }
  640. }
  641. if ($passwordWasChecked == false) {
  642. Display::addFlash(
  643. Display:: return_message(get_lang('ProfileReg'), 'normal', false)
  644. );
  645. } else {
  646. if ($validPassword) {
  647. Display::addFlash(
  648. Display:: return_message(get_lang('ProfileReg'), 'normal', false)
  649. );
  650. }
  651. }
  652. $userInfo = api_get_user_info(
  653. api_get_user_id(),
  654. false,
  655. false,
  656. false,
  657. false,
  658. true,
  659. true
  660. );
  661. Session::write('_user', $userInfo);
  662. if ($hook) {
  663. Database::getManager()->clear(User::class); // Avoid cache issue (user entity is used before)
  664. $user = api_get_user_entity(api_get_user_id()); // Get updated user info for hook event
  665. $hook->setEventData(['user' => $user]);
  666. $hook->notifyUpdateUser(HOOK_EVENT_TYPE_POST);
  667. }
  668. Session::erase('system_timezone');
  669. $url = api_get_self();
  670. header("Location: $url");
  671. exit;
  672. }
  673. $actions = '';
  674. if ($allowSocialTool) {
  675. if (api_get_setting('extended_profile') === 'true') {
  676. if (api_get_setting('allow_message_tool') === 'true') {
  677. $actions .= '<a href="'.api_get_path(WEB_PATH).'main/social/profile.php">'.
  678. Display::return_icon('shared_profile.png', get_lang('ViewSharedProfile')).'</a>';
  679. $actions .= '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php">'.
  680. Display::return_icon('inbox.png', get_lang('Messages')).'</a>';
  681. }
  682. $show = isset($_GET['show']) ? '&amp;show='.Security::remove_XSS($_GET['show']) : '';
  683. if (isset($_GET['type']) && $_GET['type'] === 'extended') {
  684. $actions .= '<a href="profile.php?type=reduced'.$show.'">'.
  685. Display::return_icon('edit.png', get_lang('EditNormalProfile'), '', 16).'</a>';
  686. } else {
  687. $actions .= '<a href="profile.php?type=extended'.$show.'">'.
  688. Display::return_icon('edit.png', get_lang('EditExtendProfile'), '', 16).'</a>';
  689. }
  690. }
  691. }
  692. $show_delete_account_button = api_get_setting('platform_unsubscribe_allowed') === 'true' ? true : false;
  693. if (api_get_setting('show_terms_if_profile_completed') === 'true') {
  694. if (empty($user_data['profile_completed'])) {
  695. Display::addFlash(Display::return_message(get_lang('ProfileIsNotCompleted'), 'warning'));
  696. }
  697. $profileCompleteResults = Session::read('profile_completed_result');
  698. if (!empty($profileCompleteResults)) {
  699. foreach ($profileCompleteResults as $profileVariable => $value) {
  700. if ($value === false) {
  701. $data = $extraField->get_handler_field_info_by_field_variable($profileVariable);
  702. Display::addFlash(
  703. Display::return_message('"'.$data['display_text'].'" '.get_lang('ThisFieldIsRequired'),
  704. 'warning',
  705. false
  706. )
  707. );
  708. }
  709. }
  710. }
  711. Session::erase('profile_completed_result');
  712. }
  713. $tpl = new Template(get_lang('ModifyProfile'));
  714. if ($actions) {
  715. $tpl->assign(
  716. 'actions',
  717. Display::toolbarAction('toolbar', [$actions])
  718. );
  719. }
  720. SocialManager::setSocialUserBlock($tpl, api_get_user_id(), 'messages');
  721. if ($allowSocialTool) {
  722. SocialManager::setSocialUserBlock($tpl, api_get_user_id(), 'home');
  723. $menu = SocialManager::show_social_menu(
  724. 'home',
  725. null,
  726. api_get_user_id(),
  727. false,
  728. $show_delete_account_button
  729. );
  730. $tpl->assign('social_menu_block', $menu);
  731. $tpl->assign('social_right_content', $form->returnForm());
  732. $social_layout = $tpl->get_template('social/edit_profile.tpl');
  733. $tpl->display($social_layout);
  734. } else {
  735. $bigImage = UserManager::getUserPicture(api_get_user_id(), USER_IMAGE_SIZE_BIG);
  736. $normalImage = UserManager::getUserPicture(api_get_user_id(), USER_IMAGE_SIZE_ORIGINAL);
  737. $imageToShow = '<div id="image-message-container">';
  738. $imageToShow .= '<a class="expand-image pull-right" href="'.$bigImage.'" /><img src="'.$normalImage.'"></a>';
  739. $imageToShow .= '</div>';
  740. $content = $imageToShow.$form->returnForm();
  741. $tpl->assign('content', $content);
  742. $tpl->display_one_col_template();
  743. }
  744. // Helper functions defined below this point
  745. /**
  746. * Is user auth_source is platform ?
  747. *
  748. * @return bool Whether auth_source is 'platform' or not
  749. */
  750. function is_platform_authentication()
  751. {
  752. $tabUserInfo = api_get_user_info();
  753. return $tabUserInfo['auth_source'] == PLATFORM_AUTH_SOURCE;
  754. }
  755. /**
  756. * Can a user edit his/her profile?
  757. *
  758. * @return bool Whether the profile can be edited by the user or not
  759. */
  760. function is_profile_editable()
  761. {
  762. if (isset($GLOBALS['profileIsEditable'])) {
  763. return (bool) $GLOBALS['profileIsEditable'];
  764. }
  765. return true;
  766. }
  767. /**
  768. * Upload a submitted user production.
  769. *
  770. * @param int $userId User id
  771. *
  772. * @return mixed The filename of the new production or FALSE if the upload has failed
  773. */
  774. function upload_user_production($userId)
  775. {
  776. $productionRepository = UserManager::getUserPathById($userId, 'system');
  777. if (!file_exists($productionRepository)) {
  778. @mkdir($productionRepository, api_get_permissions_for_new_directories(), true);
  779. }
  780. $filename = api_replace_dangerous_char($_FILES['production']['name']);
  781. $filename = disable_dangerous_file($filename);
  782. if (filter_extension($filename)) {
  783. if (@move_uploaded_file($_FILES['production']['tmp_name'], $productionRepository.$filename)) {
  784. return $filename;
  785. }
  786. }
  787. return false; // this should be returned if anything went wrong with the upload
  788. }
  789. /**
  790. * Check current user's current password.
  791. *
  792. * @param string $email E-mail
  793. *
  794. * @return bool Whether this e-mail is already in use or not
  795. */
  796. function check_user_email($email)
  797. {
  798. $userId = api_get_user_id();
  799. if ($userId != strval(intval($userId)) || empty($email)) {
  800. return false;
  801. }
  802. $tableUser = Database::get_main_table(TABLE_MAIN_USER);
  803. $email = Database::escape_string($email);
  804. $sql = "SELECT * FROM $tableUser WHERE user_id = $userId AND email = '$email'";
  805. $result = Database::query($sql);
  806. return Database::num_rows($result) != 0;
  807. }