profile.php 25 KB

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