profile.php 30 KB

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