profile.php 28 KB

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