profile.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. /**
  12. * Code
  13. */
  14. /* Init section */
  15. // Language files that should be included.
  16. $language_file = array('registration', 'messages', 'userInfo');
  17. $cidReset = true;
  18. require_once '../inc/global.inc.php';
  19. if (api_get_setting('allow_social_tool') == 'true') {
  20. $this_section = SECTION_SOCIAL;
  21. } else {
  22. $this_section = SECTION_MYPROFILE;
  23. }
  24. $_SESSION['this_section'] = $this_section;
  25. if (!(isset($_user['user_id']) && $_user['user_id']) || api_is_anonymous($_user['user_id'], true)) {
  26. api_not_allowed(true);
  27. }
  28. //jquery thickbox already called from main/inc/header.inc.php
  29. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/tag/jquery.fcbkcomplete.js" type="text/javascript" language="javascript"></script>';
  30. $htmlHeadXtra[] = '<link href="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/style.css" rel="stylesheet" type="text/css" />';
  31. $htmlHeadXtra[] = '<script type="text/javascript">
  32. function confirmation(name) {
  33. if (confirm("'.get_lang('AreYouSureToDelete', '').' " + name + " ?")) {
  34. document.forms["profile"].submit();
  35. } else {
  36. return false;
  37. }
  38. }
  39. function show_image(image,width,height) {
  40. width = parseInt(width) + 20;
  41. height = parseInt(height) + 20;
  42. window_x = window.open(image,\'windowX\',\'width=\'+ width + \', height=\'+ height + \'\');
  43. }
  44. function generate_open_id_form() {
  45. $.ajax({
  46. contentType: "application/x-www-form-urlencoded",
  47. beforeSend: function(objeto) {
  48. /*$("#div_api_key").html("Loading...");*/ },
  49. type: "POST",
  50. url: "'.api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=generate_api_key",
  51. data: "num_key_id="+"",
  52. success: function(datos) {
  53. $("#div_api_key").html(datos);
  54. }
  55. });
  56. }
  57. function hide_icon_edit(element_html) {
  58. ident="#edit_image";
  59. $(ident).hide();
  60. }
  61. function show_icon_edit(element_html) {
  62. ident="#edit_image";
  63. $(ident).show();
  64. }
  65. </script>';
  66. //$interbreadcrumb[] = array('url' => '../auth/profile.php', 'name' => get_lang('ModifyProfile'));
  67. if (!empty ($_GET['coursePath'])) {
  68. $course_url = api_get_path(WEB_COURSE_PATH).htmlentities(strip_tags($_GET['coursePath'])).'/index.php';
  69. $interbreadcrumb[] = array('url' => $course_url, 'name' => Security::remove_XSS($_GET['courseCode']));
  70. }
  71. $warning_msg = '';
  72. if (!empty($_GET['fe'])) {
  73. $warning_msg .= get_lang('UplUnableToSaveFileFilteredExtension');
  74. $_GET['fe'] = null;
  75. }
  76. $jquery_ready_content = '';
  77. if (api_get_setting('allow_message_tool') == 'true') {
  78. $jquery_ready_content = <<<EOF
  79. $(".message-content .message-delete").click(function(){
  80. $(this).parents(".message-content").animate({ opacity: "hide" }, "slow");
  81. $(".message-view").animate({ opacity: "show" }, "slow");
  82. });
  83. EOF;
  84. }
  85. // Configuration file
  86. require_once api_get_path(CONFIGURATION_PATH).'profile.conf.php';
  87. // Libraries
  88. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  89. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  90. $tool_name = is_profile_editable() ? get_lang('ModifProfile') : get_lang('ViewProfile');
  91. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  92. /* Form */
  93. /*
  94. * Get initial values for all fields.
  95. */
  96. $user_data = UserManager::get_user_info_by_id(api_get_user_id());
  97. $array_list_key = UserManager::get_api_keys(api_get_user_id());
  98. $id_temp_key = UserManager::get_api_key_id(api_get_user_id(), 'dokeos');
  99. $value_array = $array_list_key[$id_temp_key];
  100. $user_data['api_key_generate'] = $value_array;
  101. if ($user_data !== false) {
  102. if (is_null($user_data['language'])) {
  103. $user_data['language'] = api_get_setting('platformLanguage');
  104. }
  105. }
  106. /*
  107. * Initialize the form.
  108. */
  109. $form = new FormValidator('profile', 'post', api_get_self()."?".str_replace('&fe=1', '', $_SERVER['QUERY_STRING']), null, array('style' => 'width: 70%; float: '.($text_dir == 'rtl' ? 'right;' : 'left;')));
  110. /* Make sure this is the first submit on the form, even though it is hidden!
  111. * Otherwise, if a user has productions and presses ENTER to submit, he will
  112. * attempt to delete the first production in the list. */
  113. //if (is_profile_editable()) {
  114. // $form->addElement('style_submit_button', null, get_lang('SaveSettings'), 'class="save"', array('style' => 'visibility:hidden;'));
  115. //}
  116. // SUBMIT (visible)
  117. /*if (is_profile_editable()) {
  118. $form->addElement('style_submit_button', 'apply_change', get_lang('SaveSettings'), 'class="save"');
  119. } else {
  120. $form->freeze();
  121. }*/
  122. //THEME
  123. if (is_profile_editable() && api_get_setting('user_selected_theme') == 'true') {
  124. $form->addElement('select_theme', 'theme', get_lang('Theme'));
  125. if (api_get_setting('profile', 'theme') !== 'true')
  126. $form->freeze('theme');
  127. $form->applyFilter('theme', 'trim');
  128. }
  129. if (api_is_western_name_order()) {
  130. // FIRST NAME and LAST NAME
  131. $form->addElement('text', 'firstname', get_lang('FirstName'), array('size' => 40));
  132. $form->addElement('text', 'lastname', get_lang('LastName'), array('size' => 40));
  133. } else {
  134. // LAST NAME and FIRST NAME
  135. $form->addElement('text', 'lastname', get_lang('LastName'), array('size' => 40));
  136. $form->addElement('text', 'firstname', get_lang('FirstName'), array('size' => 40));
  137. }
  138. if (api_get_setting('profile', 'name') !== 'true') {
  139. $form->freeze(array('lastname', 'firstname'));
  140. }
  141. $form->applyFilter(array('lastname', 'firstname'), 'stripslashes');
  142. $form->applyFilter(array('lastname', 'firstname'), 'trim');
  143. $form->addRule('lastname' , get_lang('ThisFieldIsRequired'), 'required');
  144. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  145. // USERNAME
  146. $form->addElement('text', 'username', get_lang('UserName'), array('maxlength' => USERNAME_MAX_LENGTH, 'size' => 40));
  147. if (api_get_setting('profile', 'login') !== 'true') {
  148. $form->freeze('username');
  149. }
  150. $form->applyFilter('username', 'stripslashes');
  151. $form->applyFilter('username', 'trim');
  152. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  153. $form->addRule('username', get_lang('UsernameWrong'), 'username');
  154. $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
  155. // OFFICIAL CODE
  156. if (CONFVAL_ASK_FOR_OFFICIAL_CODE) {
  157. $form->addElement('text', 'official_code', get_lang('OfficialCode'), array('size' => 40));
  158. if (api_get_setting('profile', 'officialcode') !== 'true') {
  159. $form->freeze('official_code');
  160. }
  161. $form->applyFilter('official_code', 'stripslashes');
  162. $form->applyFilter('official_code', 'trim');
  163. if (api_get_setting('registration', 'officialcode') == 'true' && api_get_setting('profile', 'officialcode') == 'true') {
  164. $form->addRule('official_code', get_lang('ThisFieldIsRequired'), 'required');
  165. }
  166. }
  167. // EMAIL
  168. $form->addElement('text', 'email', get_lang('Email'), array('size' => 40));
  169. if (api_get_setting('profile', 'email') !== 'true') {
  170. $form->freeze('email');
  171. }
  172. $form->applyFilter('email', 'stripslashes');
  173. $form->applyFilter('email', 'trim');
  174. if (api_get_setting('registration', 'email') == 'true') {
  175. $form->addRule('email', get_lang('ThisFieldIsRequired'), 'required');
  176. }
  177. $form->addRule('email', get_lang('EmailWrong'), 'email');
  178. // OPENID URL
  179. if (is_profile_editable() && api_get_setting('openid_authentication') == 'true') {
  180. $form->addElement('text', 'openid', get_lang('OpenIDURL'), array('size' => 40));
  181. if (api_get_setting('profile', 'openid') !== 'true') {
  182. $form->freeze('openid');
  183. }
  184. $form->applyFilter('openid', 'trim');
  185. //if (api_get_setting('registration', 'openid') == 'true') {
  186. // $form->addRule('openid', get_lang('ThisFieldIsRequired'), 'required');
  187. //}
  188. }
  189. // PHONE
  190. $form->addElement('text', 'phone', get_lang('phone'), array('size' => 20));
  191. if (api_get_setting('profile', 'phone') !== 'true') {
  192. $form->freeze('phone');
  193. }
  194. $form->applyFilter('phone', 'stripslashes');
  195. $form->applyFilter('phone', 'trim');
  196. /*if (api_get_setting('registration', 'phone') == 'true') {
  197. $form->addRule('phone', get_lang('ThisFieldIsRequired'), 'required');
  198. }
  199. $form->addRule('phone', get_lang('EmailWrong'), 'email');*/
  200. // PICTURE
  201. if (is_profile_editable() && api_get_setting('profile', 'picture') == 'true') {
  202. $form->addElement('file', 'picture', ($user_data['picture_uri'] != '' ? get_lang('UpdateImage') : get_lang('AddImage')));
  203. $form->add_progress_bar();
  204. if (!empty($user_data['picture_uri'])) {
  205. $form->addElement('checkbox', 'remove_picture', null, get_lang('DelImage'));
  206. }
  207. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  208. $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  209. }
  210. // LANGUAGE
  211. $form->addElement('select_language', 'language', get_lang('Language'));
  212. if (api_get_setting('profile', 'language') !== 'true') {
  213. $form->freeze('language');
  214. }
  215. // EXTENDED PROFILE this make the page very slow!
  216. if (api_get_setting('extended_profile') == 'true') {
  217. if (!isset($_GET['type']) || (isset($_GET['type']) && $_GET['type'] == 'extended')) {
  218. $width_extended_profile = 500;
  219. //$form->addElement('html', '<a href="javascript: void(0);" onclick="javascript: show_extend();"> show_extend_profile</a>');
  220. //$form->addElement('static', null, '<em>'.get_lang('OptionalTextFields').'</em>');
  221. // MY COMPETENCES
  222. $form->add_html_editor('competences', get_lang('MyCompetences'), false, false, array('ToolbarSet' => 'Profile', 'Width' => $width_extended_profile, 'Height' => '130'));
  223. // MY DIPLOMAS
  224. $form->add_html_editor('diplomas', get_lang('MyDiplomas'), false, false, array('ToolbarSet' => 'Profile', 'Width' => $width_extended_profile, 'Height' => '130'));
  225. // WHAT I AM ABLE TO TEACH
  226. $form->add_html_editor('teach', get_lang('MyTeach'), false, false, array('ToolbarSet' => 'Profile', 'Width' => $width_extended_profile, 'Height' => '130'));
  227. // MY PRODUCTIONS
  228. $form->addElement('file', 'production', get_lang('MyProductions'));
  229. if ($production_list = UserManager::build_production_list(api_get_user_id(), '', true)) {
  230. $form->addElement('static', 'productions_list', null, $production_list);
  231. }
  232. // MY PERSONAL OPEN AREA
  233. $form->add_html_editor('openarea', get_lang('MyPersonalOpenArea'), false, false, array('ToolbarSet' => 'Profile', 'Width' => $width_extended_profile, 'Height' => '350'));
  234. $form->applyFilter(array('competences', 'diplomas', 'teach', 'openarea'), 'stripslashes');
  235. $form->applyFilter(array('competences', 'diplomas', 'teach'), 'trim'); // openarea is untrimmed for maximum openness
  236. }
  237. }
  238. // PASSWORD
  239. if (is_profile_editable() && api_get_setting('profile', 'password') == 'true') {
  240. $form->addElement('password', 'password0', array(get_lang('Pass'), get_lang('Enter2passToChange')), array('size' => 40));
  241. $form->addElement('password', 'password1', get_lang('NewPass'), array('size' => 40));
  242. $form->addElement('password', 'password2', get_lang('Confirmation'), array('size' => 40));
  243. // user must enter identical password twice so we can prevent some user errors
  244. $form->addRule(array('password1', 'password2'), get_lang('PassTwo'), 'compare');
  245. if (CHECK_PASS_EASY_TO_FIND) {
  246. $form->addRule('password1', get_lang('CurrentPasswordEmptyOrIncorrect'), 'callback', 'api_check_password');
  247. }
  248. }
  249. // EXTRA FIELDS
  250. $extra = UserManager::get_extra_fields(0, 50, 5, 'ASC');
  251. $extra_data = UserManager::get_extra_user_data(api_get_user_id(), true);
  252. foreach ($extra as $id => $field_details) {
  253. if ($field_details[6] == 0) {
  254. continue;
  255. }
  256. switch ($field_details[2]) {
  257. case USER_FIELD_TYPE_TEXT:
  258. $form->addElement('text', 'extra_'.$field_details[1], $field_details[3], array('size' => 40));
  259. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  260. $form->applyFilter('extra_'.$field_details[1], 'trim');
  261. if ($field_details[7] == 0) $form->freeze('extra_'.$field_details[1]);
  262. break;
  263. case USER_FIELD_TYPE_TEXTAREA:
  264. $form->add_html_editor('extra_'.$field_details[1], $field_details[3], false);
  265. //$form->addElement('textarea', 'extra_'.$field_details[1], $field_details[3], array('size' => 80));
  266. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  267. $form->applyFilter('extra_'.$field_details[1], 'trim');
  268. if ($field_details[7] == 0) $form->freeze('extra_'.$field_details[1]);
  269. break;
  270. case USER_FIELD_TYPE_RADIO:
  271. $group = array();
  272. foreach ($field_details[9] as $option_id => $option_details) {
  273. $options[$option_details[1]] = $option_details[2];
  274. $group[] =& HTML_QuickForm::createElement('radio', 'extra_'.$field_details[1], $option_details[1], $option_details[2].'<br />', $option_details[1]);
  275. }
  276. $form->addGroup($group, 'extra_'.$field_details[1], $field_details[3], '');
  277. if ($field_details[7] == 0) $form->freeze('extra_'.$field_details[1]);
  278. break;
  279. case USER_FIELD_TYPE_SELECT:
  280. if (in_array($field_details[1], array('mail_notify_invitation', 'mail_notify_message', 'mail_notify_group_message'))) {
  281. $field_details[3] = get_lang($field_details[3]);
  282. }
  283. $options = array();
  284. foreach ($field_details[9] as $option_id => $option_details) {
  285. if (in_array($field_details[1], array('mail_notify_invitation', 'mail_notify_message', 'mail_notify_group_message'))) {
  286. $option_details[2] = get_lang($option_details[2]);
  287. }
  288. $options[$option_details[1]] = $option_details[2];
  289. }
  290. $form->addElement('select', 'extra_'.$field_details[1], $field_details[3], $options, array('class'=>'chzn-select', 'id'=>'extra_'.$field_details[1]));
  291. if ($field_details[7] == 0) $form->freeze('extra_'.$field_details[1]);
  292. break;
  293. case USER_FIELD_TYPE_SELECT_MULTIPLE:
  294. $options = array();
  295. foreach ($field_details[9] as $option_id => $option_details) {
  296. $options[$option_details[1]] = $option_details[2];
  297. }
  298. $form->addElement('select', 'extra_'.$field_details[1], $field_details[3], $options, array('multiple' => 'multiple'));
  299. if ($field_details[7] == 0) $form->freeze('extra_'.$field_details[1]);
  300. break;
  301. case USER_FIELD_TYPE_DATE:
  302. $form->addElement('datepickerdate', 'extra_'.$field_details[1], $field_details[3], array('form_name' => 'profile'));
  303. $form->_elements[$form->_elementIndex['extra_'.$field_details[1]]]->setLocalOption('minYear', 1900);
  304. if ($field_details[7] == 0) $form->freeze('extra_'.$field_details[1]);
  305. $form->applyFilter('theme', 'trim');
  306. $defaults['extra_'.$field_details[1]] = date('Y-m-d 12:00:00');
  307. $form -> setDefaults($defaults);
  308. break;
  309. case USER_FIELD_TYPE_DATETIME:
  310. $form->addElement('datepicker', 'extra_'.$field_details[1],$field_details[3], array('form_name' => 'profile'));
  311. $form->_elements[$form->_elementIndex['extra_'.$field_details[1]]]->setLocalOption('minYear', 1900);
  312. if ($field_details[7] == 0) $form->freeze('extra_'.$field_details[1]);
  313. $form->applyFilter('theme', 'trim');
  314. $defaults['extra_'.$field_details[1]] = date('Y-m-d 12:00:00');
  315. $form -> setDefaults($defaults);
  316. break;
  317. case USER_FIELD_TYPE_DOUBLE_SELECT:
  318. foreach ($field_details[9] as $key => $element) {
  319. if ($element[2][0] == '*') {
  320. $values['*'][$element[0]] = str_replace('*', '', $element[2]);
  321. } else {
  322. $values[0][$element[0]] = $element[2];
  323. }
  324. }
  325. $group = '';
  326. $group[] =& HTML_QuickForm::createElement('select', 'extra_'.$field_details[1], '', $values[0], '');
  327. $group[] =& HTML_QuickForm::createElement('select', 'extra_'.$field_details[1].'*', '', $values['*'], '');
  328. $form->addGroup($group, 'extra_'.$field_details[1], $field_details[3], '&nbsp;');
  329. if ($field_details[7] == 0) $form->freeze('extra_'.$field_details[1]);
  330. // recoding the selected values for double : if the user has selected certain values, we have to assign them to the correct select form
  331. if (key_exists('extra_'.$field_details[1], $extra_data)) {
  332. // exploding all the selected values (of both select forms)
  333. $selected_values = explode(';', $extra_data['extra_'.$field_details[1]]);
  334. $extra_data['extra_'.$field_details[1]] = array();
  335. // looping through the selected values and assigning the selected values to either the first or second select form
  336. foreach ($selected_values as $key=>$selected_value) {
  337. if (key_exists($selected_value,$values[0])) {
  338. $extra_data['extra_'.$field_details[1]]['extra_'.$field_details[1]] = $selected_value;
  339. } else {
  340. $extra_data['extra_'.$field_details[1]]['extra_'.$field_details[1].'*'] = $selected_value;
  341. }
  342. }
  343. }
  344. break;
  345. case USER_FIELD_TYPE_DIVIDER:
  346. $form->addElement('static', $field_details[1], '<br /><strong>'.$field_details[3].'</strong>');
  347. break;
  348. case USER_FIELD_TYPE_TAG:
  349. //the magic should be here
  350. $user_tags = UserManager::get_user_tags(api_get_user_id(),$field_details[0]);
  351. $pre_html = '<div class="row">
  352. <div class="label">'.$field_details[3].'</div>
  353. <div class="formw">';
  354. $post = '</div></div>';
  355. $tag_list = '';
  356. if (is_array($user_tags) && count($user_tags)> 0) {
  357. foreach ($user_tags as $tag) {
  358. $tag_list .= '<option value="'.$tag['tag'].'" class="selected">'.$tag['tag'].'</option>';
  359. }
  360. }
  361. $multi_select = '<select id="extra_'.$field_details[1].'" name="extra_'.$field_details[1].'">
  362. '.$tag_list.'
  363. </select>';
  364. $form->addElement('html',$pre_html.$multi_select.$post );
  365. $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php';
  366. $complete_text = get_lang('StartToType');
  367. //if cache is set to true the jquery will be called 1 time
  368. $jquery_ready_content.= <<<EOF
  369. $("#extra_$field_details[1]").fcbkcomplete({
  370. json_url: "$url?a=search_tags&field_id=$field_details[0]",
  371. cache: false,
  372. filter_case: true,
  373. filter_hide: true,
  374. complete_text:"$complete_text",
  375. firstselected: true,
  376. //onremove: "testme",
  377. //onselect: "testme",
  378. filter_selected: true,
  379. newel: true
  380. });
  381. EOF;
  382. break;
  383. case USER_FIELD_TYPE_TIMEZONE:
  384. $form->addElement('select', 'extra_'.$field_details[1], $field_details[3], api_get_timezones(), '');
  385. if ($field_details[7] == 0) $form->freeze('extra_'.$field_details[1]);
  386. break;
  387. case USER_FIELD_TYPE_SOCIAL_PROFILE:
  388. // get the social network's favicon
  389. $icon_path = UserManager::get_favicon_from_url($extra_data['extra_'.$field_details[1]], $field_details[4]);
  390. // special hack for hi5
  391. $leftpad = '1.7'; $top = '0.4'; $domain = parse_url($icon_path, PHP_URL_HOST); if ($domain == 'www.hi5.com' or $domain == 'hi5.com') { $leftpad = '3'; $top = '0';}
  392. // print the input field
  393. $form->addElement('text', 'extra_'.$field_details[1], $field_details[3], array('size' => 60, 'style' => 'background-image: url(\''.$icon_path.'\'); background-repeat: no-repeat; background-position: 0.4em '.$top.'em; padding-left: '.$leftpad.'em; '));
  394. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  395. $form->applyFilter('extra_'.$field_details[1], 'trim');
  396. if ($field_details[7] == 0) $form->freeze('extra_'.$field_details[1]);
  397. break;
  398. }
  399. }
  400. // the $jquery_ready_content variable collects all functions that will be load in the $(document).ready javascript function
  401. $htmlHeadXtra[] ='<script type="text/javascript">
  402. $(document).ready(function(){
  403. '.$jquery_ready_content.'
  404. });
  405. </script>';
  406. if (api_get_setting('profile', 'apikeys') == 'true') {
  407. $form->addElement('html', '<div id="div_api_key">');
  408. $form->addElement('text', 'api_key_generate', get_lang('MyApiKey'), array('size' => 40, 'id' => 'id_api_key_generate'));
  409. $form->addElement('html', '</div>');
  410. $form->addElement('button', 'generate_api_key', get_lang('GenerateApiKey'), array('id' => 'id_generate_api_key', 'onclick' => 'generate_open_id_form()')); //generate_open_id_form()
  411. }
  412. // SUBMIT
  413. if (is_profile_editable()) {
  414. $form->addElement('style_submit_button', 'apply_change', get_lang('SaveSettings'), 'class="save"');
  415. } else {
  416. $form->freeze();
  417. }
  418. $user_data = array_merge($user_data, $extra_data);
  419. $form->setDefaults($user_data);
  420. /* FUNCTIONS */
  421. /**
  422. * Can a user edit his/her profile?
  423. *
  424. * @return boolean Editability of the profile
  425. */
  426. function is_profile_editable() {
  427. return $GLOBALS['profileIsEditable'];
  428. }
  429. /*
  430. PRODUCTIONS FUNCTIONS
  431. */
  432. /**
  433. * Upload a submitted user production.
  434. *
  435. * @param $user_id User id
  436. * @return The filename of the new production or FALSE if the upload has failed
  437. */
  438. function upload_user_production($user_id) {
  439. $image_path = UserManager::get_user_picture_path_by_id($user_id, 'system', true);
  440. $production_repository = $image_path['dir'].$user_id.'/';
  441. if (!file_exists($production_repository)) {
  442. @mkdir($production_repository, api_get_permissions_for_new_directories(), true);
  443. }
  444. $filename = replace_dangerous_char($_FILES['production']['name']);
  445. $filename = disable_dangerous_file($filename);
  446. if (filter_extension($filename)) {
  447. if (@move_uploaded_file($_FILES['production']['tmp_name'], $production_repository.$filename)) {
  448. return $filename;
  449. }
  450. }
  451. return false; // this should be returned if anything went wrong with the upload
  452. }
  453. /**
  454. * Check current user's current password
  455. * @param char password
  456. * @return bool true o false
  457. * @uses Gets user ID from global variable
  458. */
  459. function check_user_password($password){
  460. global $_user;
  461. $user_id = api_get_user_id();
  462. if ($user_id != strval(intval($user_id)) || empty($password)) { return false; }
  463. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  464. $password = api_get_encrypted_password($password);
  465. $sql_password = "SELECT * FROM $table_user WHERE user_id='".$user_id."' AND password='".$password."'";
  466. $result = Database::query($sql_password);
  467. return Database::num_rows($result) != 0;
  468. }
  469. /**
  470. * Check current user's current password
  471. * @param char email
  472. * @return bool true o false
  473. * @uses Gets user ID from global variable
  474. */
  475. function check_user_email($email) {
  476. $user_id = api_get_user_id();
  477. if ($user_id != strval(intval($user_id)) || empty($email)) { return false; }
  478. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  479. $email = Database::escape_string($email);
  480. $sql_password = "SELECT * FROM $table_user WHERE user_id='".$user_id."' AND email='".$email."'";
  481. $result = Database::query($sql_password);
  482. return Database::num_rows($result) != 0;
  483. }
  484. /* MAIN CODE */
  485. $filtered_extension = false;
  486. $update_success = false;
  487. $upload_picture_success = false;
  488. $upload_production_success = false;
  489. $msg_fail_changue_email = false;
  490. $msg_is_not_password = false;
  491. if (!empty($_SESSION['change_email'])) {
  492. $msg_fail_changue_email= ($_SESSION['change_email'] == 'success');
  493. unset($_SESSION['change_email']);
  494. } elseif (!empty($_SESSION['is_not_password'])) {
  495. $msg_is_not_password = ($_SESSION['is_not_password'] == 'success');
  496. unset($_SESSION['is_not_password']);
  497. } elseif (!empty($_SESSION['profile_update'])) {
  498. $update_success = ($_SESSION['profile_update'] == 'success');
  499. unset($_SESSION['profile_update']);
  500. } elseif (!empty($_SESSION['image_uploaded'])) {
  501. $upload_picture_success = ($_SESSION['image_uploaded'] == 'success');
  502. unset($_SESSION['image_uploaded']);
  503. } elseif (!empty($_SESSION['production_uploaded'])) {
  504. $upload_production_success = ($_SESSION['production_uploaded'] == 'success');
  505. unset($_SESSION['production_uploaded']);
  506. }
  507. if ($form->validate()) {
  508. $wrong_current_password = false;
  509. // $user_data = $form->exportValues();
  510. $user_data = $form->getSubmitValues();
  511. // set password if a new one was provided
  512. if (!empty($user_data['password0'])) {
  513. if (check_user_password($user_data['password0'])) {
  514. if (!empty($user_data['password1'])) {
  515. $password = $user_data['password1'];
  516. }
  517. } else {
  518. $wrong_current_password = true;
  519. $_SESSION['is_not_password'] = 'success';
  520. }
  521. }
  522. if (empty($user_data['password0']) && !empty($user_data['password1'])) {
  523. $wrong_current_password = true;
  524. $_SESSION['is_not_password'] = 'success';
  525. }
  526. $allow_users_to_change_email_with_no_password = api_get_setting('allow_users_to_change_email_with_no_password') == 'true' ? true :false;
  527. //$allow_users_to_change_email_with_no_password = true;
  528. $changeemail = '';
  529. if ($allow_users_to_change_email_with_no_password) {
  530. $changeemail = '';
  531. if (!check_user_email($user_data['email'])) {
  532. $changeemail = $user_data['email'];
  533. //$_SESSION['change_email'] = 'success';
  534. }
  535. } else {
  536. //Normal behaviour
  537. if (!check_user_email($user_data['email']) && !empty($user_data['password0']) && !$wrong_current_password) {
  538. $changeemail = $user_data['email'];
  539. }
  540. if (!check_user_email($user_data['email']) && empty($user_data['password0'])){
  541. $_SESSION['change_email'] = 'success';
  542. }
  543. }
  544. // Upload picture if a new one is provided
  545. if ($_FILES['picture']['size']) {
  546. if ($new_picture = UserManager::update_user_picture(api_get_user_id(), $_FILES['picture']['name'], $_FILES['picture']['tmp_name'])) {
  547. $user_data['picture_uri'] = $new_picture;
  548. $_SESSION['image_uploaded'] = 'success';
  549. }
  550. } elseif (!empty($user_data['remove_picture'])) {
  551. // remove existing picture if asked
  552. UserManager::delete_user_picture(api_get_user_id());
  553. $user_data['picture_uri'] = '';
  554. }
  555. //Remove production
  556. if (is_array($user_data['remove_production'])) {
  557. foreach (array_keys($user_data['remove_production']) as $production) {
  558. UserManager::remove_user_production(api_get_user_id(), urldecode($production));
  559. }
  560. if ($production_list = UserManager::build_production_list(api_get_user_id(), true, true)) {
  561. $form->insertElementBefore($form->createElement('static', null, null, $production_list), 'productions_list');
  562. }
  563. $form->removeElement('productions_list');
  564. $file_deleted = true;
  565. }
  566. // upload production if a new one is provided
  567. if ($_FILES['production']['size']) {
  568. $res = upload_user_production(api_get_user_id());
  569. if (!$res) {
  570. //it's a bit excessive to assume the extension is the reason why upload_user_production() returned false, but it's true in most cases
  571. $filtered_extension = true;
  572. } else {
  573. $_SESSION['production_uploaded'] = 'success';
  574. }
  575. }
  576. // remove values that shouldn't go in the database
  577. unset($user_data['password0'],$user_data['password1'], $user_data['password2'], $user_data['MAX_FILE_SIZE'],
  578. $user_data['remove_picture'], $user_data['apply_change'],$user_data['email'] );
  579. // Following RFC2396 (http://www.faqs.org/rfcs/rfc2396.html), a URI uses ':' as a reserved character
  580. // we can thus ensure the URL doesn't contain any scheme name by searching for ':' in the string
  581. $my_user_openid = isset($user_data['openid']) ? $user_data['openid'] : '';
  582. if (!preg_match('/^[^:]*:\/\/.*$/', $my_user_openid)) {
  583. //ensure there is at least a http:// scheme in the URI provided
  584. $user_data['openid'] = 'http://'.$my_user_openid;
  585. }
  586. $extras = array();
  587. //Checking the user language
  588. $languages = api_get_languages();
  589. if (!in_array($user_data['language'], $languages['folder'])) {
  590. $user_data['language'] = api_get_setting('platformLanguage');
  591. }
  592. //Only update values that are request by the "profile" setting
  593. $profile_list = api_get_setting('profile');
  594. //Adding missing variables
  595. $available_values_to_modify = array();
  596. foreach($profile_list as $key => $status) {
  597. if ($status == 'true') {
  598. switch($key) {
  599. case 'login':
  600. $available_values_to_modify[] = 'username';
  601. break;
  602. case 'name':
  603. $available_values_to_modify[] = 'firstname';
  604. $available_values_to_modify[] = 'lastname';
  605. break;
  606. case 'picture':
  607. $available_values_to_modify[] = 'picture_uri';
  608. break;
  609. default:
  610. $available_values_to_modify[] = $key;
  611. break;
  612. }
  613. }
  614. }
  615. //Fixing missing variables
  616. $available_values_to_modify = array_merge($available_values_to_modify, array('competences', 'diplomas', 'openarea', 'teach', 'openid'));
  617. // build SQL query
  618. $sql = "UPDATE $table_user SET";
  619. unset($user_data['api_key_generate']);
  620. foreach ($user_data as $key => $value) {
  621. if (substr($key, 0, 6) == 'extra_') { //an extra field
  622. $new_key = substr($key, 6);
  623. // format array date to 'Y-m-d' or date time to 'Y-m-d H:i:s'
  624. if (is_array($value) && isset($value['Y']) && isset($value['F']) && isset($value['d'])) {
  625. if (isset($value['H']) && isset($value['i'])) {
  626. // extra field date time
  627. $time = mktime($value['H'],$value['i'],0,$value['F'],$value['d'],$value['Y']);
  628. $extras[$new_key] = date('Y-m-d H:i:s',$time);
  629. } else {
  630. // extra field date
  631. $time = mktime(0,0,0,$value['F'],$value['d'],$value['Y']);
  632. $extras[$new_key] = date('Y-m-d',$time);
  633. }
  634. } else {
  635. $extras[$new_key] = $value;
  636. }
  637. } else {
  638. if (in_array($key, $available_values_to_modify)) {
  639. $sql .= " $key = '".Database::escape_string($value)."',";
  640. }
  641. }
  642. }
  643. //change email
  644. if ($allow_users_to_change_email_with_no_password) {
  645. if (!empty($changeemail) && in_array('email', $available_values_to_modify)) {
  646. $sql .= " email = '".Database::escape_string($changeemail)."',";
  647. }
  648. if (isset($password) && in_array('password', $available_values_to_modify)) {
  649. $password = api_get_encrypted_password($password);
  650. $sql .= " password = '".Database::escape_string($password)."'";
  651. } else {
  652. // remove trailing , from the query we have so far
  653. $sql = rtrim($sql, ',');
  654. }
  655. } else {
  656. //normal behaviour
  657. if (!empty($changeemail) && !isset($password) && in_array('email', $available_values_to_modify)) {
  658. $sql .= " email = '".Database::escape_string($changeemail)."'";
  659. } elseif (isset($password) && isset($changeemail) && in_array('email', $available_values_to_modify) && in_array('password', $available_values_to_modify)) {
  660. $sql .= " email = '".Database::escape_string($changeemail)."',";
  661. $password = api_get_encrypted_password($password);
  662. $sql .= " password = '".Database::escape_string($password)."'";
  663. } elseif (isset($password) && in_array('password', $available_values_to_modify)) {
  664. $password = api_get_encrypted_password($password);
  665. $sql .= " password = '".Database::escape_string($password)."'";
  666. } else {
  667. // remove trailing , from the query we have so far
  668. $sql = rtrim($sql, ',');
  669. }
  670. }
  671. $sql .= " WHERE user_id = '".api_get_user_id()."'";
  672. Database::query($sql);
  673. // User tag process
  674. //1. Deleting all user tags
  675. $list_extra_field_type_tag = UserManager::get_all_extra_field_by_type(USER_FIELD_TYPE_TAG);
  676. if (is_array($list_extra_field_type_tag) && count($list_extra_field_type_tag)>0) {
  677. foreach ($list_extra_field_type_tag as $id) {
  678. UserManager::delete_user_tags(api_get_user_id(), $id);
  679. }
  680. }
  681. //2. Update the extra fields and user tags if available
  682. if (is_array($extras) && count($extras)> 0) {
  683. foreach ($extras as $key => $value) {
  684. //3. Tags are process in the UserManager::update_extra_field_value by the UserManager::process_tags function
  685. $myres = UserManager::update_extra_field_value(api_get_user_id(), $key, $value);
  686. }
  687. }
  688. // re-init the system to take new settings into account
  689. $_SESSION['_user']['uidReset'] = true;
  690. $_SESSION['noredirection'] = true;
  691. $_SESSION['profile_update'] = 'success';
  692. $url = api_get_self()."?{$_SERVER['QUERY_STRING']}".($filtered_extension && strpos($_SERVER['QUERY_STRING'], '&fe=1') === false ? '&fe=1' : '');
  693. header("Location: ".$url);
  694. exit;
  695. }
  696. /* MAIN DISPLAY SECTION */
  697. // the header
  698. Display::display_header(get_lang('ModifyProfile'));
  699. if (api_get_setting('allow_social_tool') != 'true') {
  700. if (api_get_setting('extended_profile') == 'true') {
  701. echo '<div class="actions">';
  702. if (api_get_setting('allow_social_tool') == 'true' && api_get_setting('allow_message_tool') == 'true') {
  703. echo '<a href="'.api_get_path(WEB_PATH).'main/social/profile.php">'.Display::return_icon('shared_profile.png', get_lang('ViewSharedProfile')).'&nbsp;'.get_lang('ViewSharedProfile').'</a>';
  704. }
  705. if (api_get_setting('allow_message_tool') == 'true') {
  706. echo '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php">'.Display::return_icon('inbox.png').' '.get_lang('Messages').'</a>';
  707. }
  708. $show = isset($_GET['show']) ? '&amp;show='.Security::remove_XSS($_GET['show']) : '';
  709. if (isset($_GET['type']) && $_GET['type'] == 'extended') {
  710. echo '<a href="profile.php?type=reduced'.$show.'">'.Display::return_icon('edit.png', get_lang('EditNormalProfile'),'',16).'&nbsp;'.get_lang('EditNormalProfile').'</a>';
  711. } else {
  712. echo '<a href="profile.php?type=extended'.$show.'">'.Display::return_icon('edit.png', get_lang('EditExtendProfile'),'',16).'&nbsp;'.get_lang('EditExtendProfile').'</a>';
  713. }
  714. echo '</div>';
  715. }
  716. }
  717. if (!empty($file_deleted)) {
  718. Display :: display_confirmation_message(get_lang('FileDeleted'), false);
  719. } elseif (!empty($update_success)) {
  720. $message = get_lang('ProfileReg');
  721. if ($upload_picture_success) {
  722. $message .= '<br /> '.get_lang('PictureUploaded');
  723. }
  724. if ($upload_production_success) {
  725. $message.='<br />'.get_lang('ProductionUploaded');
  726. }
  727. Display :: display_confirmation_message($message, false);
  728. }
  729. if (!empty($msg_fail_changue_email)){
  730. $errormail=get_lang('ToChangeYourEmailMustTypeYourPassword');
  731. Display :: display_error_message($errormail, false);
  732. }
  733. if (!empty($msg_is_not_password)){
  734. $warning_msg = get_lang('CurrentPasswordEmptyOrIncorrect');
  735. Display :: display_warning_message($warning_msg, false);
  736. }
  737. //User picture size is calculated from SYSTEM path
  738. $image_syspath = UserManager::get_user_picture_path_by_id(api_get_user_id(), 'system', false, true);
  739. $image_syspath['dir'].$image_syspath['file'];
  740. //$image_size = api_getimagesize($image_syspath['dir'].$image_syspath['file']);
  741. //Web path
  742. $image_path = UserManager::get_user_picture_path_by_id(api_get_user_id(), 'web', false, true);
  743. $image_dir = $image_path['dir'];
  744. $image = $image_path['file'];
  745. $image_file = $image_dir.$image;
  746. $img_attributes = 'src="'.$image_file.'?rand='.time().'" '
  747. .'alt="'.api_get_person_name($user_data['firstname'], $user_data['lastname']).'" '
  748. .'style="float:'.($text_dir == 'rtl' ? 'left' : 'right').'; margin-top:0px;padding:5px;" ';
  749. if ($image_size['width'] > 300) {
  750. //limit display width to 300px
  751. $img_attributes .= 'width="300" ';
  752. }
  753. // get the path,width and height from original picture
  754. $big_image = $image_dir.'big_'.$image;
  755. $big_image_size = api_getimagesize($big_image);
  756. $big_image_width = $big_image_size['width'];
  757. $big_image_height = $big_image_size['height'];
  758. $url_big_image = $big_image.'?rnd='.time();
  759. if (api_get_setting('allow_social_tool') == 'true') {
  760. echo '<div id="social-content">';
  761. echo '<div id="social-content-left">';
  762. echo SocialManager::show_social_menu('home', null, api_get_user_id(), false);
  763. echo '</div>';
  764. echo '<div id="social-content-right">';
  765. $form->display();
  766. echo '</div>';
  767. } else {
  768. // Style position:absolute has been removed for Opera-compatibility.
  769. //echo '<div id="image-message-container" style="float:right;display:inline;position:absolute;padding:3px;width:250px;" >';
  770. echo '<div id="image-message-container" style="float:right;display:inline;padding:3px;width:230px;" >';
  771. if ($image == 'unknown.jpg') {
  772. echo '<img '.$img_attributes.' />';
  773. } else {
  774. echo '<input type="image" '.$img_attributes.' onclick="javascript: return show_image(\''.$url_big_image.'\',\''.$big_image_width.'\',\''.$big_image_height.'\');"/>';
  775. }
  776. echo '</div>';
  777. $form->display();
  778. }
  779. Display :: display_footer();