profile.php 30 KB

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