profile.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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('Pass'), get_lang('TypeCurrentPassword')], ['size' => 40]);
  286. $form->addElement('password', 'password1', [get_lang('NewPass'), get_lang('EnterYourNewPassword')], ['id' => 'password1', 'size' => 40]);
  287. $form->addElement('password', 'password2', [get_lang('Confirmation'), get_lang('RepeatYourNewPassword')], ['size' => 40]);
  288. // user must enter identical password twice so we can prevent some user errors
  289. $form->addRule(['password1', 'password2'], get_lang('PassTwo'), 'compare');
  290. $form->addPasswordRule('password1');
  291. }
  292. $extraField = new ExtraField('user');
  293. $return = $extraField->addElements(
  294. $form,
  295. api_get_user_id()
  296. );
  297. $jquery_ready_content = $return['jquery_ready_content'];
  298. // the $jquery_ready_content variable collects all functions that
  299. // will be load in the $(document).ready javascript function
  300. $htmlHeadXtra[] = '<script>
  301. $(function() {
  302. '.$jquery_ready_content.'
  303. });
  304. </script>';
  305. if (api_get_setting('profile', 'apikeys') == 'true') {
  306. $form->addElement('html', '<div id="div_api_key">');
  307. $form->addElement(
  308. 'text',
  309. 'api_key_generate',
  310. get_lang('MyApiKey'),
  311. ['size' => 40, 'id' => 'id_api_key_generate']
  312. );
  313. $form->addElement('html', '</div>');
  314. $form->addButton(
  315. 'generate_api_key',
  316. get_lang('GenerateApiKey'),
  317. 'cogs',
  318. 'default',
  319. 'default',
  320. null,
  321. ['id' => 'id_generate_api_key']
  322. );
  323. }
  324. // SUBMIT
  325. if (is_profile_editable()) {
  326. $form->addButtonUpdate(get_lang('SaveSettings'), 'apply_change');
  327. } else {
  328. $form->freeze();
  329. }
  330. // Student cannot modified their user conditions
  331. $extraConditions = api_get_configuration_value('show_conditions_to_user');
  332. if ($extraConditions && isset($extraConditions['conditions'])) {
  333. $extraConditions = $extraConditions['conditions'];
  334. foreach ($extraConditions as $condition) {
  335. $element = $form->getElement('extra_'.$condition['variable']);
  336. if ($element) {
  337. $element->freeze();
  338. }
  339. }
  340. }
  341. $form->setDefaults($user_data);
  342. $filtered_extension = false;
  343. if ($form->validate()) {
  344. $hook = HookUpdateUser::create();
  345. if ($hook) {
  346. $hook->notifyUpdateUser(HOOK_EVENT_TYPE_PRE);
  347. }
  348. $wrong_current_password = false;
  349. $user_data = $form->getSubmitValues(1);
  350. /** @var User $user */
  351. $user = UserManager::getRepository()->find(api_get_user_id());
  352. // set password if a new one was provided
  353. $validPassword = false;
  354. $passwordWasChecked = false;
  355. if ($user &&
  356. (!empty($user_data['password0']) &&
  357. !empty($user_data['password1'])) ||
  358. (!empty($user_data['password0']) &&
  359. api_get_setting('profile', 'email') == 'true')
  360. ) {
  361. $passwordWasChecked = true;
  362. $validPassword = UserManager::isPasswordValid(
  363. $user->getPassword(),
  364. $user_data['password0'],
  365. $user->getSalt()
  366. );
  367. if ($validPassword) {
  368. $password = $user_data['password1'];
  369. } else {
  370. Display::addFlash(
  371. Display:: return_message(
  372. get_lang('CurrentPasswordEmptyOrIncorrect'),
  373. 'warning',
  374. false
  375. )
  376. );
  377. }
  378. }
  379. $allow_users_to_change_email_with_no_password = true;
  380. if (is_platform_authentication() &&
  381. api_get_setting('allow_users_to_change_email_with_no_password') == 'false'
  382. ) {
  383. $allow_users_to_change_email_with_no_password = false;
  384. }
  385. // If user sending the email to be changed (input available and not frozen )
  386. if (api_get_setting('profile', 'email') == 'true') {
  387. if ($allow_users_to_change_email_with_no_password) {
  388. if (!check_user_email($user_data['email'])) {
  389. $changeemail = $user_data['email'];
  390. }
  391. } else {
  392. // Normal behaviour
  393. if (!check_user_email($user_data['email']) && $validPassword) {
  394. $changeemail = $user_data['email'];
  395. }
  396. if (!check_user_email($user_data['email']) && empty($user_data['password0'])) {
  397. Display::addFlash(
  398. Display:: return_message(
  399. get_lang('ToChangeYourEmailMustTypeYourPassword'),
  400. 'error',
  401. false
  402. )
  403. );
  404. }
  405. }
  406. }
  407. // Upload picture if a new one is provided
  408. if ($_FILES['picture']['size']) {
  409. $new_picture = UserManager::update_user_picture(
  410. api_get_user_id(),
  411. $_FILES['picture']['name'],
  412. $_FILES['picture']['tmp_name'],
  413. $user_data['picture_crop_result']
  414. );
  415. if ($new_picture) {
  416. $user_data['picture_uri'] = $new_picture;
  417. Display::addFlash(
  418. Display:: return_message(
  419. get_lang('PictureUploaded'),
  420. 'normal',
  421. false
  422. )
  423. );
  424. }
  425. } elseif (!empty($user_data['remove_picture'])) {
  426. // remove existing picture if asked
  427. UserManager::deleteUserPicture(api_get_user_id());
  428. $user_data['picture_uri'] = '';
  429. }
  430. // Remove production.
  431. if (isset($user_data['remove_production']) &&
  432. is_array($user_data['remove_production'])
  433. ) {
  434. foreach (array_keys($user_data['remove_production']) as $production) {
  435. UserManager::remove_user_production(api_get_user_id(), urldecode($production));
  436. }
  437. if ($production_list = UserManager::build_production_list(api_get_user_id(), true, true)) {
  438. $form->insertElementBefore(
  439. $form->createElement('static', null, null, $production_list),
  440. 'productions_list'
  441. );
  442. }
  443. $form->removeElement('productions_list');
  444. Display::addFlash(
  445. Display:: return_message(get_lang('FileDeleted'), 'normal', false)
  446. );
  447. }
  448. // upload production if a new one is provided
  449. if (isset($_FILES['production']) && $_FILES['production']['size']) {
  450. $res = upload_user_production(api_get_user_id());
  451. if (!$res) {
  452. //it's a bit excessive to assume the extension is the reason why
  453. // upload_user_production() returned false, but it's true in most cases
  454. $filtered_extension = true;
  455. } else {
  456. Display::addFlash(
  457. Display:: return_message(
  458. get_lang('ProductionUploaded'),
  459. 'normal',
  460. false
  461. )
  462. );
  463. }
  464. }
  465. // remove values that shouldn't go in the database
  466. unset(
  467. $user_data['password0'],
  468. $user_data['password1'],
  469. $user_data['password2'],
  470. $user_data['MAX_FILE_SIZE'],
  471. $user_data['remove_picture'],
  472. $user_data['apply_change'],
  473. $user_data['email']
  474. );
  475. // Following RFC2396 (http://www.faqs.org/rfcs/rfc2396.html), a URI uses ':' as a reserved character
  476. // we can thus ensure the URL doesn't contain any scheme name by searching for ':' in the string
  477. $my_user_openid = isset($user_data['openid']) ? $user_data['openid'] : '';
  478. if (!preg_match('/^[^:]*:\/\/.*$/', $my_user_openid)) {
  479. //ensure there is at least a http:// scheme in the URI provided
  480. $user_data['openid'] = 'http://'.$my_user_openid;
  481. }
  482. $extras = [];
  483. //Checking the user language
  484. $languages = api_get_languages();
  485. if (!in_array($user_data['language'], $languages['folder'])) {
  486. $user_data['language'] = api_get_setting('platformLanguage');
  487. }
  488. $_SESSION['_user']['language'] = $user_data['language'];
  489. //Only update values that are request by the "profile" setting
  490. $profile_list = api_get_setting('profile');
  491. //Adding missing variables
  492. $available_values_to_modify = [];
  493. foreach ($profile_list as $key => $status) {
  494. if ($status == 'true') {
  495. switch ($key) {
  496. case 'login':
  497. $available_values_to_modify[] = 'username';
  498. break;
  499. case 'name':
  500. $available_values_to_modify[] = 'firstname';
  501. $available_values_to_modify[] = 'lastname';
  502. break;
  503. case 'picture':
  504. $available_values_to_modify[] = 'picture_uri';
  505. break;
  506. default:
  507. $available_values_to_modify[] = $key;
  508. break;
  509. }
  510. }
  511. }
  512. //Fixing missing variables
  513. $available_values_to_modify = array_merge(
  514. $available_values_to_modify,
  515. ['competences', 'diplomas', 'openarea', 'teach', 'openid', 'address']
  516. );
  517. // build SQL query
  518. $sql = "UPDATE $table_user SET";
  519. unset($user_data['api_key_generate']);
  520. foreach ($user_data as $key => $value) {
  521. if (substr($key, 0, 6) === 'extra_') { //an extra field
  522. continue;
  523. } elseif (strpos($key, 'remove_extra_') !== false) {
  524. } else {
  525. if (in_array($key, $available_values_to_modify)) {
  526. $sql .= " $key = '".Database::escape_string($value)."',";
  527. }
  528. }
  529. }
  530. $changePassword = false;
  531. // Change email
  532. if ($allow_users_to_change_email_with_no_password) {
  533. if (isset($changeemail) && in_array('email', $available_values_to_modify)) {
  534. $sql .= " email = '".Database::escape_string($changeemail)."' ";
  535. }
  536. if (isset($password) && in_array('password', $available_values_to_modify)) {
  537. $changePassword = true;
  538. }
  539. } else {
  540. if (isset($changeemail) && !isset($password) && in_array('email', $available_values_to_modify)) {
  541. $sql .= " email = '".Database::escape_string($changeemail)."'";
  542. } else {
  543. if (isset($password) && in_array('password', $available_values_to_modify)) {
  544. if (isset($changeemail) && in_array('email', $available_values_to_modify)) {
  545. $sql .= " email = '".Database::escape_string($changeemail)."' ";
  546. }
  547. $changePassword = true;
  548. }
  549. }
  550. }
  551. $sql = rtrim($sql, ',');
  552. if ($changePassword && !empty($password)) {
  553. UserManager::updatePassword(api_get_user_id(), $password);
  554. }
  555. if (api_get_setting('profile', 'officialcode') === 'true' &&
  556. isset($user_data['official_code'])
  557. ) {
  558. $sql .= ", official_code = '".Database::escape_string($user_data['official_code'])."'";
  559. }
  560. $sql .= " WHERE id = '".api_get_user_id()."'";
  561. Database::query($sql);
  562. if ($passwordWasChecked == false) {
  563. Display::addFlash(
  564. Display:: return_message(get_lang('ProfileReg'), 'normal', false)
  565. );
  566. } else {
  567. if ($validPassword) {
  568. Display::addFlash(
  569. Display:: return_message(get_lang('ProfileReg'), 'normal', false)
  570. );
  571. }
  572. }
  573. $extraField = new ExtraFieldValue('user');
  574. $extraField->saveFieldValues($user_data);
  575. $userInfo = api_get_user_info(
  576. api_get_user_id(),
  577. false,
  578. false,
  579. false,
  580. false,
  581. true,
  582. true
  583. );
  584. Session::write('_user', $userInfo);
  585. if ($hook) {
  586. Database::getManager()->clear(User::class); // Avoid cache issue (user entity is used before)
  587. $user = api_get_user_entity(api_get_user_id()); // Get updated user info for hook event
  588. $hook->setEventData(['user' => $user]);
  589. $hook->notifyUpdateUser(HOOK_EVENT_TYPE_POST);
  590. }
  591. Session::erase('system_timezone');
  592. $url = api_get_self();
  593. header("Location: $url");
  594. exit;
  595. }
  596. $actions = '';
  597. if ($allowSocialTool) {
  598. if (api_get_setting('extended_profile') === 'true') {
  599. if (api_get_setting('allow_message_tool') === 'true') {
  600. $actions .= '<a href="'.api_get_path(WEB_PATH).'main/social/profile.php">'.
  601. Display::return_icon('shared_profile.png', get_lang('ViewSharedProfile')).'</a>';
  602. $actions .= '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php">'.
  603. Display::return_icon('inbox.png', get_lang('Messages')).'</a>';
  604. }
  605. $show = isset($_GET['show']) ? '&amp;show='.Security::remove_XSS($_GET['show']) : '';
  606. if (isset($_GET['type']) && $_GET['type'] === 'extended') {
  607. $actions .= '<a href="profile.php?type=reduced'.$show.'">'.
  608. Display::return_icon('edit.png', get_lang('EditNormalProfile'), '', 16).'</a>';
  609. } else {
  610. $actions .= '<a href="profile.php?type=extended'.$show.'">'.
  611. Display::return_icon('edit.png', get_lang('EditExtendProfile'), '', 16).'</a>';
  612. }
  613. }
  614. }
  615. $show_delete_account_button = api_get_setting('platform_unsubscribe_allowed') === 'true' ? true : false;
  616. $tpl = new Template(get_lang('ModifyProfile'));
  617. if ($actions) {
  618. $tpl->assign(
  619. 'actions',
  620. Display::toolbarAction('toolbar', [$actions])
  621. );
  622. }
  623. SocialManager::setSocialUserBlock($tpl, api_get_user_id(), 'messages');
  624. if ($allowSocialTool) {
  625. SocialManager::setSocialUserBlock($tpl, api_get_user_id(), 'home');
  626. $menu = SocialManager::show_social_menu(
  627. 'home',
  628. null,
  629. api_get_user_id(),
  630. false,
  631. $show_delete_account_button
  632. );
  633. $tpl->assign('social_menu_block', $menu);
  634. $tpl->assign('social_right_content', $form->returnForm());
  635. $social_layout = $tpl->get_template('social/edit_profile.tpl');
  636. $tpl->display($social_layout);
  637. } else {
  638. $bigImage = UserManager::getUserPicture(api_get_user_id(), USER_IMAGE_SIZE_BIG);
  639. $normalImage = UserManager::getUserPicture(api_get_user_id(), USER_IMAGE_SIZE_ORIGINAL);
  640. $imageToShow = '<div id="image-message-container">';
  641. $imageToShow .= '<a class="expand-image pull-right" href="'.$bigImage.'" /><img src="'.$normalImage.'"></a>';
  642. $imageToShow .= '</div>';
  643. $content = $imageToShow.$form->returnForm();
  644. $tpl->assign('content', $content);
  645. $tpl->display_one_col_template();
  646. }
  647. // Helper functions defined below this point
  648. /**
  649. * Is user auth_source is platform ?
  650. *
  651. * @return bool Whether auth_source is 'platform' or not
  652. */
  653. function is_platform_authentication()
  654. {
  655. $tabUserInfo = api_get_user_info();
  656. return $tabUserInfo['auth_source'] == PLATFORM_AUTH_SOURCE;
  657. }
  658. /**
  659. * Can a user edit his/her profile?
  660. *
  661. * @return bool Whether the profile can be edited by the user or not
  662. */
  663. function is_profile_editable()
  664. {
  665. if (isset($GLOBALS['profileIsEditable'])) {
  666. return (bool) $GLOBALS['profileIsEditable'];
  667. }
  668. return true;
  669. }
  670. /**
  671. * Upload a submitted user production.
  672. *
  673. * @param int $userId User id
  674. *
  675. * @return mixed The filename of the new production or FALSE if the upload has failed
  676. */
  677. function upload_user_production($userId)
  678. {
  679. $productionRepository = UserManager::getUserPathById($userId, 'system');
  680. if (!file_exists($productionRepository)) {
  681. @mkdir($productionRepository, api_get_permissions_for_new_directories(), true);
  682. }
  683. $filename = api_replace_dangerous_char($_FILES['production']['name']);
  684. $filename = disable_dangerous_file($filename);
  685. if (filter_extension($filename)) {
  686. if (@move_uploaded_file($_FILES['production']['tmp_name'], $productionRepository.$filename)) {
  687. return $filename;
  688. }
  689. }
  690. return false; // this should be returned if anything went wrong with the upload
  691. }
  692. /**
  693. * Check current user's current password.
  694. *
  695. * @param string $email E-mail
  696. *
  697. * @return bool Whether this e-mail is already in use or not
  698. */
  699. function check_user_email($email)
  700. {
  701. $userId = api_get_user_id();
  702. if ($userId != strval(intval($userId)) || empty($email)) {
  703. return false;
  704. }
  705. $tableUser = Database::get_main_table(TABLE_MAIN_USER);
  706. $email = Database::escape_string($email);
  707. $sql = "SELECT * FROM $tableUser WHERE user_id = $userId AND email = '$email'";
  708. $result = Database::query($sql);
  709. return Database::num_rows($result) != 0;
  710. }