profile.php 32 KB

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