profile.php 29 KB

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