PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Sources/Profile.php

https://github.com/smf-portal/SMF2.1
PHP | 810 lines | 615 code | 75 blank | 120 comment | 145 complexity | 81c903fa51fe5965b61f330cce1372d4 MD5 | raw file
  1. <?php
  2. /**
  3. * This file has the primary job of showing and editing people's profiles.
  4. * It also allows the user to change some of their or another's preferences,
  5. * and such things.
  6. *
  7. * Simple Machines Forum (SMF)
  8. *
  9. * @package SMF
  10. * @author Simple Machines http://www.simplemachines.org
  11. * @copyright 2012 Simple Machines
  12. * @license http://www.simplemachines.org/about/smf/license.php BSD
  13. *
  14. * @version 2.1 Alpha 1
  15. */
  16. if (!defined('SMF'))
  17. die('Hacking attempt...');
  18. /**
  19. * Allow the change or view of profiles...
  20. *
  21. * @param array $post_errors = array()
  22. */
  23. function ModifyProfile($post_errors = array())
  24. {
  25. global $txt, $scripturl, $user_info, $context, $sourcedir, $user_profile, $cur_profile;
  26. global $modSettings, $memberContext, $profile_vars, $smcFunc, $post_errors, $options, $user_settings;
  27. // Don't reload this as we may have processed error strings.
  28. if (empty($post_errors))
  29. loadLanguage('Profile+Drafts');
  30. loadTemplate('Profile');
  31. require_once($sourcedir . '/Subs-Menu.php');
  32. // Did we get the user by name...
  33. if (isset($_REQUEST['user']))
  34. $memberResult = loadMemberData($_REQUEST['user'], true, 'profile');
  35. // ... or by id_member?
  36. elseif (!empty($_REQUEST['u']))
  37. $memberResult = loadMemberData((int) $_REQUEST['u'], false, 'profile');
  38. // If it was just ?action=profile, edit your own profile.
  39. else
  40. $memberResult = loadMemberData($user_info['id'], false, 'profile');
  41. // Check if loadMemberData() has returned a valid result.
  42. if (!is_array($memberResult))
  43. fatal_lang_error('not_a_user', false);
  44. // If all went well, we have a valid member ID!
  45. list ($memID) = $memberResult;
  46. $context['id_member'] = $memID;
  47. $cur_profile = $user_profile[$memID];
  48. // Let's have some information about this member ready, too.
  49. loadMemberContext($memID);
  50. $context['member'] = $memberContext[$memID];
  51. // Is this the profile of the user himself or herself?
  52. $context['user']['is_owner'] = $memID == $user_info['id'];
  53. /* Define all the sections within the profile area!
  54. We start by defining the permission required - then SMF takes this and turns it into the relevant context ;)
  55. Possible fields:
  56. For Section:
  57. string $title: Section title.
  58. array $areas: Array of areas within this section.
  59. For Areas:
  60. string $label: Text string that will be used to show the area in the menu.
  61. string $file: Optional text string that may contain a file name that's needed for inclusion in order to display the area properly.
  62. string $custom_url: Optional href for area.
  63. string $function: Function to execute for this section.
  64. bool $enabled: Should area be shown?
  65. string $sc: Session check validation to do on save - note without this save will get unset - if set.
  66. bool $hidden: Does this not actually appear on the menu?
  67. bool $password: Whether to require the user's password in order to save the data in the area.
  68. array $subsections: Array of subsections, in order of appearance.
  69. array $permission: Array of permissions to determine who can access this area. Should contain arrays $own and $any.
  70. */
  71. $profile_areas = array(
  72. 'info' => array(
  73. 'title' => $txt['profileInfo'],
  74. 'areas' => array(
  75. 'summary' => array(
  76. 'label' => $txt['summary'],
  77. 'file' => 'Profile-View.php',
  78. 'function' => 'summary',
  79. 'permission' => array(
  80. 'own' => 'profile_view_own',
  81. 'any' => 'profile_view_any',
  82. ),
  83. ),
  84. 'statistics' => array(
  85. 'label' => $txt['statPanel'],
  86. 'file' => 'Profile-View.php',
  87. 'function' => 'statPanel',
  88. 'permission' => array(
  89. 'own' => 'profile_view_own',
  90. 'any' => 'profile_view_any',
  91. ),
  92. ),
  93. 'showposts' => array(
  94. 'label' => $txt['showPosts'],
  95. 'file' => 'Profile-View.php',
  96. 'function' => 'showPosts',
  97. 'subsections' => array(
  98. 'messages' => array($txt['showMessages'], array('profile_view_own', 'profile_view_any')),
  99. 'topics' => array($txt['showTopics'], array('profile_view_own', 'profile_view_any')),
  100. 'attach' => array($txt['showAttachments'], array('profile_view_own', 'profile_view_any')),
  101. ),
  102. 'permission' => array(
  103. 'own' => 'profile_view_own',
  104. 'any' => 'profile_view_any',
  105. ),
  106. ),
  107. 'showdrafts' => array(
  108. 'label' => $txt['drafts_show'],
  109. 'file' => 'Drafts.php',
  110. 'function' => 'showProfileDrafts',
  111. 'enabled' => !empty($modSettings['drafts_enabled']) && $context['user']['is_owner'],
  112. 'permission' => array(
  113. 'own' => 'profile_view_own',
  114. 'any' => array(),
  115. ),
  116. ),
  117. 'permissions' => array(
  118. 'label' => $txt['showPermissions'],
  119. 'file' => 'Profile-View.php',
  120. 'function' => 'showPermissions',
  121. 'permission' => array(
  122. 'own' => 'manage_permissions',
  123. 'any' => 'manage_permissions',
  124. ),
  125. ),
  126. 'tracking' => array(
  127. 'label' => $txt['trackUser'],
  128. 'file' => 'Profile-View.php',
  129. 'function' => 'tracking',
  130. 'subsections' => array(
  131. 'activity' => array($txt['trackActivity'], 'moderate_forum'),
  132. 'ip' => array($txt['trackIP'], 'moderate_forum'),
  133. 'edits' => array($txt['trackEdits'], 'moderate_forum'),
  134. 'logins' => array($txt['trackLogins'], array('profile_view_own', 'moderate_forum')),
  135. ),
  136. 'permission' => array(
  137. 'own' => 'moderate_forum',
  138. 'any' => 'moderate_forum',
  139. ),
  140. ),
  141. 'viewwarning' => array(
  142. 'label' => $txt['profile_view_warnings'],
  143. 'enabled' => in_array('w', $context['admin_features']) && $modSettings['warning_settings'][0] == 1 && $cur_profile['warning'] && (!empty($modSettings['warning_show']) && ($context['user']['is_owner'] || $modSettings['warning_show'] == 2)),
  144. 'file' => 'Profile-View.php',
  145. 'function' => 'viewWarning',
  146. 'permission' => array(
  147. 'own' => 'profile_view_own',
  148. 'any' => 'issue_warning',
  149. ),
  150. ),
  151. ),
  152. ),
  153. 'edit_profile' => array(
  154. 'title' => $txt['profileEdit'],
  155. 'areas' => array(
  156. 'account' => array(
  157. 'label' => $txt['account'],
  158. 'file' => 'Profile-Modify.php',
  159. 'function' => 'account',
  160. 'enabled' => $context['user']['is_admin'] || ($cur_profile['id_group'] != 1 && !in_array(1, explode(',', $cur_profile['additional_groups']))),
  161. 'sc' => 'post',
  162. 'token' => 'profile-ac%u',
  163. 'password' => true,
  164. 'permission' => array(
  165. 'own' => array('profile_identity_any', 'profile_identity_own', 'manage_membergroups'),
  166. 'any' => array('profile_identity_any', 'manage_membergroups'),
  167. ),
  168. ),
  169. 'forumprofile' => array(
  170. 'label' => $txt['forumprofile'],
  171. 'file' => 'Profile-Modify.php',
  172. 'function' => 'forumProfile',
  173. 'sc' => 'post',
  174. 'token' => 'profile-fp%u',
  175. 'permission' => array(
  176. 'own' => array('profile_extra_any', 'profile_extra_own', 'profile_title_own', 'profile_title_any'),
  177. 'any' => array('profile_extra_any', 'profile_title_any'),
  178. ),
  179. ),
  180. 'theme' => array(
  181. 'label' => $txt['theme'],
  182. 'file' => 'Profile-Modify.php',
  183. 'function' => 'theme',
  184. 'sc' => 'post',
  185. 'token' => 'profile-th%u',
  186. 'permission' => array(
  187. 'own' => array('profile_extra_any', 'profile_extra_own'),
  188. 'any' => array('profile_extra_any'),
  189. ),
  190. ),
  191. 'authentication' => array(
  192. 'label' => $txt['authentication'],
  193. 'file' => 'Profile-Modify.php',
  194. 'function' => 'authentication',
  195. 'enabled' => !empty($modSettings['enableOpenID']) || !empty($cur_profile['openid_uri']),
  196. 'sc' => 'post',
  197. 'token' => 'profile-au%u',
  198. 'hidden' => empty($modSettings['enableOpenID']) && empty($cur_profile['openid_uri']),
  199. 'password' => true,
  200. 'permission' => array(
  201. 'own' => array('profile_identity_any', 'profile_identity_own'),
  202. 'any' => array('profile_identity_any'),
  203. ),
  204. ),
  205. 'notification' => array(
  206. 'label' => $txt['notification'],
  207. 'file' => 'Profile-Modify.php',
  208. 'function' => 'notification',
  209. 'sc' => 'post',
  210. 'token' => 'profile-nt%u',
  211. 'permission' => array(
  212. 'own' => array('profile_extra_any', 'profile_extra_own'),
  213. 'any' => array('profile_extra_any'),
  214. ),
  215. ),
  216. // Without profile_extra_own, settings are accessible from the PM section.
  217. 'pmprefs' => array(
  218. 'label' => $txt['pmprefs'],
  219. 'file' => 'Profile-Modify.php',
  220. 'function' => 'pmprefs',
  221. 'enabled' => allowedTo(array('profile_extra_own', 'profile_extra_any')),
  222. 'sc' => 'post',
  223. 'token' => 'profile-pm%u',
  224. 'permission' => array(
  225. 'own' => array('pm_read'),
  226. 'any' => array('profile_extra_any'),
  227. ),
  228. ),
  229. 'ignoreboards' => array(
  230. 'label' => $txt['ignoreboards'],
  231. 'file' => 'Profile-Modify.php',
  232. 'function' => 'ignoreboards',
  233. 'enabled' => !empty($modSettings['allow_ignore_boards']),
  234. 'sc' => 'post',
  235. 'token' => 'profile-ib%u',
  236. 'permission' => array(
  237. 'own' => array('profile_extra_any', 'profile_extra_own'),
  238. 'any' => array('profile_extra_any'),
  239. ),
  240. ),
  241. 'lists' => array(
  242. 'label' => $txt['editBuddyIgnoreLists'],
  243. 'file' => 'Profile-Modify.php',
  244. 'function' => 'editBuddyIgnoreLists',
  245. 'enabled' => !empty($modSettings['enable_buddylist']) && $context['user']['is_owner'],
  246. 'sc' => 'post',
  247. 'token' => 'profile-bl%u',
  248. 'subsections' => array(
  249. 'buddies' => array($txt['editBuddies']),
  250. 'ignore' => array($txt['editIgnoreList']),
  251. ),
  252. 'permission' => array(
  253. 'own' => array('profile_extra_any', 'profile_extra_own'),
  254. 'any' => array(),
  255. ),
  256. ),
  257. 'groupmembership' => array(
  258. 'label' => $txt['groupmembership'],
  259. 'file' => 'Profile-Modify.php',
  260. 'function' => 'groupMembership',
  261. 'enabled' => !empty($modSettings['show_group_membership']) && $context['user']['is_owner'],
  262. 'sc' => 'request',
  263. 'token' => 'profile-gm%u',
  264. 'token_type' => 'request',
  265. 'permission' => array(
  266. 'own' => array('profile_view_own'),
  267. 'any' => array('manage_membergroups'),
  268. ),
  269. ),
  270. ),
  271. ),
  272. 'profile_action' => array(
  273. 'title' => $txt['profileAction'],
  274. 'areas' => array(
  275. 'sendpm' => array(
  276. 'label' => $txt['profileSendIm'],
  277. 'custom_url' => $scripturl . '?action=pm;sa=send',
  278. 'permission' => array(
  279. 'own' => array(),
  280. 'any' => array('pm_send'),
  281. ),
  282. ),
  283. 'issuewarning' => array(
  284. 'label' => $txt['profile_issue_warning'],
  285. 'enabled' => in_array('w', $context['admin_features']) && $modSettings['warning_settings'][0] == 1 && (!$context['user']['is_owner'] || $context['user']['is_admin']),
  286. 'file' => 'Profile-Actions.php',
  287. 'function' => 'issueWarning',
  288. 'token' => 'profile-iw%u',
  289. 'enabled' => !$context['user']['is_owner'],
  290. 'permission' => array(
  291. 'own' => array('issue_warning'),
  292. 'any' => array('issue_warning'),
  293. ),
  294. ),
  295. 'banuser' => array(
  296. 'label' => $txt['profileBanUser'],
  297. 'custom_url' => $scripturl . '?action=admin;area=ban;sa=add',
  298. 'enabled' => $cur_profile['id_group'] != 1 && !in_array(1, explode(',', $cur_profile['additional_groups'])),
  299. 'permission' => array(
  300. 'own' => array(),
  301. 'any' => array('manage_bans'),
  302. ),
  303. ),
  304. 'subscriptions' => array(
  305. 'label' => $txt['subscriptions'],
  306. 'file' => 'Profile-Actions.php',
  307. 'function' => 'subscriptions',
  308. 'enabled' => !empty($modSettings['paid_enabled']),
  309. 'permission' => array(
  310. 'own' => array('profile_view_own'),
  311. 'any' => array('moderate_forum'),
  312. ),
  313. ),
  314. 'deleteaccount' => array(
  315. 'label' => $txt['deleteAccount'],
  316. 'file' => 'Profile-Actions.php',
  317. 'function' => 'deleteAccount',
  318. 'sc' => 'post',
  319. 'token' => 'profile-da%u',
  320. 'password' => true,
  321. 'permission' => array(
  322. 'own' => array('profile_remove_any', 'profile_remove_own'),
  323. 'any' => array('profile_remove_any'),
  324. ),
  325. ),
  326. 'activateaccount' => array(
  327. 'file' => 'Profile-Actions.php',
  328. 'function' => 'activateAccount',
  329. 'sc' => 'get',
  330. 'token' => 'profile-aa%u',
  331. 'select' => 'summary',
  332. 'permission' => array(
  333. 'own' => array(),
  334. 'any' => array('moderate_forum'),
  335. ),
  336. ),
  337. ),
  338. ),
  339. );
  340. // Do some cleaning ready for the menu function.
  341. $context['password_areas'] = array();
  342. $current_area = isset($_REQUEST['area']) ? $_REQUEST['area'] : '';
  343. foreach ($profile_areas as $section_id => $section)
  344. {
  345. // Do a bit of spring cleaning so to speak.
  346. foreach ($section['areas'] as $area_id => $area)
  347. {
  348. // If it said no permissions that meant it wasn't valid!
  349. if (empty($area['permission'][$context['user']['is_owner'] ? 'own' : 'any']))
  350. $profile_areas[$section_id]['areas'][$area_id]['enabled'] = false;
  351. // Otherwise pick the right set.
  352. else
  353. $profile_areas[$section_id]['areas'][$area_id]['permission'] = $area['permission'][$context['user']['is_owner'] ? 'own' : 'any'];
  354. // Password required - only if not on OpenID.
  355. if (!empty($area['password']))
  356. $context['password_areas'][] = $area_id;
  357. }
  358. }
  359. // Is there an updated message to show?
  360. if (isset($_GET['updated']))
  361. $context['profile_updated'] = $txt['profile_updated_own'];
  362. // Set a few options for the menu.
  363. $menuOptions = array(
  364. 'disable_url_session_check' => true,
  365. 'current_area' => $current_area,
  366. 'extra_url_parameters' => array(
  367. 'u' => $context['id_member'],
  368. ),
  369. );
  370. // Actually create the menu!
  371. $profile_include_data = createMenu($profile_areas, $menuOptions);
  372. // No menu means no access.
  373. if (!$profile_include_data && (!$user_info['is_guest'] || validateSession()))
  374. fatal_lang_error('no_access', false);
  375. // Make a note of the Unique ID for this menu.
  376. $context['profile_menu_id'] = $context['max_menu_id'];
  377. $context['profile_menu_name'] = 'menu_data_' . $context['profile_menu_id'];
  378. // Set the selected item - now it's been validated.
  379. $current_area = $profile_include_data['current_area'];
  380. $context['menu_item_selected'] = $current_area;
  381. // Before we go any further, let's work on the area we've said is valid. Note this is done here just in case we ever compromise the menu function in error!
  382. $context['completed_save'] = false;
  383. $context['do_preview'] = isset($_REQUEST['preview_signature']);
  384. $security_checks = array();
  385. $found_area = false;
  386. foreach ($profile_areas as $section_id => $section)
  387. {
  388. // Do a bit of spring cleaning so to speak.
  389. foreach ($section['areas'] as $area_id => $area)
  390. {
  391. // Is this our area?
  392. if ($current_area == $area_id)
  393. {
  394. // This can't happen - but is a security check.
  395. if ((isset($section['enabled']) && $section['enabled'] == false) || (isset($area['enabled']) && $area['enabled'] == false))
  396. fatal_lang_error('no_access', false);
  397. // Are we saving data in a valid area?
  398. if (isset($area['sc']) && (isset($_REQUEST['save']) || $context['do_preview']))
  399. {
  400. $security_checks['session'] = $area['sc'];
  401. $context['completed_save'] = true;
  402. }
  403. // Do we need to perform a token check?
  404. if (!empty($area['token']))
  405. {
  406. $security_checks[isset($_REQUEST['save']) ? 'validateToken' : 'needsToken'] = $area['token'];
  407. $token_name = $area['token'] !== true ? str_replace('%u', $context['id_member'], $area['token']) : 'profile-u' . $context['id_member'];
  408. $token_type = isset($area['token_type']) && in_array($area['token_type'], array('request', 'post', 'get')) ? $area['token_type'] : 'post';
  409. }
  410. // Does this require session validating?
  411. if (!empty($area['validate']) || (isset($_REQUEST['save']) && !$context['user']['is_owner']))
  412. $security_checks['validate'] = true;
  413. // Permissions for good measure.
  414. if (!empty($profile_include_data['permission']))
  415. $security_checks['permission'] = $profile_include_data['permission'];
  416. // Either way got something.
  417. $found_area = true;
  418. }
  419. }
  420. }
  421. // Oh dear, some serious security lapse is going on here... we'll put a stop to that!
  422. if (!$found_area)
  423. fatal_lang_error('no_access', false);
  424. // Release this now.
  425. unset($profile_areas);
  426. // Now the context is setup have we got any security checks to carry out additional to that above?
  427. if (isset($security_checks['validateToken']))
  428. validateToken($token_name, $token_type);
  429. if (isset($security_checks['session']))
  430. checkSession($security_checks['session']);
  431. if (isset($security_checks['validate']))
  432. validateSession();
  433. if (isset($security_checks['permission']))
  434. isAllowedTo($security_checks['permission']);
  435. // Create a token if needed.
  436. if (isset($security_checks['needsToken']) || isset($security_checks['validateToken']))
  437. {
  438. createToken($token_name, $token_type);
  439. $context['token_check'] = $token_name;
  440. }
  441. // File to include?
  442. if (isset($profile_include_data['file']))
  443. require_once($sourcedir . '/' . $profile_include_data['file']);
  444. // Make sure that the area function does exist!
  445. if (!isset($profile_include_data['function']) || !function_exists($profile_include_data['function']))
  446. {
  447. destroyMenu();
  448. fatal_lang_error('no_access', false);
  449. }
  450. // Build the link tree.
  451. $context['linktree'][] = array(
  452. 'url' => $scripturl . '?action=profile' . ($memID != $user_info['id'] ? ';u=' . $memID : ''),
  453. 'name' => sprintf($txt['profile_of_username'], $context['member']['name']),
  454. );
  455. if (!empty($profile_include_data['label']))
  456. $context['linktree'][] = array(
  457. 'url' => $scripturl . '?action=profile' . ($memID != $user_info['id'] ? ';u=' . $memID : '') . ';area=' . $profile_include_data['current_area'],
  458. 'name' => $profile_include_data['label'],
  459. );
  460. if (!empty($profile_include_data['current_subsection']) && $profile_include_data['subsections'][$profile_include_data['current_subsection']][0] != $profile_include_data['label'])
  461. $context['linktree'][] = array(
  462. 'url' => $scripturl . '?action=profile' . ($memID != $user_info['id'] ? ';u=' . $memID : '') . ';area=' . $profile_include_data['current_area'] . ';sa=' . $profile_include_data['current_subsection'],
  463. 'name' => $profile_include_data['subsections'][$profile_include_data['current_subsection']][0],
  464. );
  465. // Set the template for this area and add the profile layer.
  466. $context['sub_template'] = $profile_include_data['function'];
  467. $context['template_layers'][] = 'profile';
  468. // All the subactions that require a user password in order to validate.
  469. $check_password = $context['user']['is_owner'] && in_array($profile_include_data['current_area'], $context['password_areas']);
  470. $context['require_password'] = $check_password && empty($user_settings['openid_uri']);
  471. // If we're in wireless then we have a cut down template...
  472. if (WIRELESS && $context['sub_template'] == 'summary' && WIRELESS_PROTOCOL != 'wap')
  473. $context['sub_template'] = WIRELESS_PROTOCOL . '_profile';
  474. // These will get populated soon!
  475. $post_errors = array();
  476. $profile_vars = array();
  477. // Right - are we saving - if so let's save the old data first.
  478. if ($context['completed_save'])
  479. {
  480. // Clean up the POST variables.
  481. $_POST = htmltrim__recursive($_POST);
  482. $_POST = htmlspecialchars__recursive($_POST);
  483. if ($check_password)
  484. {
  485. // If we're using OpenID try to revalidate.
  486. if (!empty($user_settings['openid_uri']))
  487. {
  488. require_once($sourcedir . '/Subs-OpenID.php');
  489. smf_openID_revalidate();
  490. }
  491. else
  492. {
  493. // You didn't even enter a password!
  494. if (trim($_POST['oldpasswrd']) == '')
  495. $post_errors[] = 'no_password';
  496. // Since the password got modified due to all the $_POST cleaning, lets undo it so we can get the correct password
  497. $_POST['oldpasswrd'] = un_htmlspecialchars($_POST['oldpasswrd']);
  498. // Does the integration want to check passwords?
  499. $good_password = in_array(true, call_integration_hook('integrate_verify_password', array($cur_profile['member_name'], $_POST['oldpasswrd'], false)), true);
  500. // Bad password!!!
  501. if (!$good_password && $user_info['passwd'] != sha1(strtolower($user_profile[$memID]['member_name']) . un_htmlspecialchars(stripslashes($_POST['oldpasswrd']))))
  502. $post_errors[] = 'bad_password';
  503. // Warn other elements not to jump the gun and do custom changes!
  504. if (in_array('bad_password', $post_errors))
  505. $context['password_auth_failed'] = true;
  506. }
  507. }
  508. // Change the IP address in the database.
  509. if ($context['user']['is_owner'])
  510. $profile_vars['member_ip'] = $user_info['ip'];
  511. // Now call the sub-action function...
  512. if ($current_area == 'activateaccount')
  513. {
  514. if (empty($post_errors))
  515. activateAccount($memID);
  516. }
  517. elseif ($current_area == 'deleteaccount')
  518. {
  519. if (empty($post_errors))
  520. {
  521. deleteAccount2($memID);
  522. redirectexit();
  523. }
  524. }
  525. elseif ($current_area == 'groupmembership' && empty($post_errors))
  526. {
  527. $msg = groupMembership2($profile_vars, $post_errors, $memID);
  528. // Whatever we've done, we have nothing else to do here...
  529. redirectexit('action=profile' . ($context['user']['is_owner'] ? '' : ';u=' . $memID) . ';area=groupmembership' . (!empty($msg) ? ';msg=' . $msg : ''));
  530. }
  531. // Authentication changes?
  532. elseif ($current_area == 'authentication')
  533. {
  534. authentication($memID, true);
  535. }
  536. elseif (in_array($current_area, array('account', 'forumprofile', 'theme', 'pmprefs')))
  537. saveProfileFields();
  538. else
  539. {
  540. $force_redirect = true;
  541. // Ensure we include this.
  542. require_once($sourcedir . '/Profile-Modify.php');
  543. saveProfileChanges($profile_vars, $post_errors, $memID);
  544. }
  545. call_integration_hook('integrate_profile_save', array($profile_vars, $post_errors, $memID));
  546. // There was a problem, let them try to re-enter.
  547. if (!empty($post_errors))
  548. {
  549. // Load the language file so we can give a nice explanation of the errors.
  550. loadLanguage('Errors');
  551. $context['post_errors'] = $post_errors;
  552. }
  553. elseif (!empty($profile_vars))
  554. {
  555. // If we've changed the password, notify any integration that may be listening in.
  556. if (isset($profile_vars['passwd']))
  557. call_integration_hook('integrate_reset_pass', array($cur_profile['member_name'], $cur_profile['member_name'], $_POST['passwrd2']));
  558. updateMemberData($memID, $profile_vars);
  559. // What if this is the newest member?
  560. if ($modSettings['latestMember'] == $memID)
  561. updateStats('member');
  562. elseif (isset($profile_vars['real_name']))
  563. updateSettings(array('memberlist_updated' => time()));
  564. // If the member changed his/her birthdate, update calendar statistics.
  565. if (isset($profile_vars['birthdate']) || isset($profile_vars['real_name']))
  566. updateSettings(array(
  567. 'calendar_updated' => time(),
  568. ));
  569. // Anything worth logging?
  570. if (!empty($context['log_changes']) && !empty($modSettings['modlog_enabled']))
  571. {
  572. $log_changes = array();
  573. require_once($sourcedir . '/Logging.php');
  574. foreach ($context['log_changes'] as $k => $v)
  575. $log_changes[] = array(
  576. 'action' => $k,
  577. 'log_type' => 'user',
  578. 'extra' => array_merge($v, array(
  579. 'applicator' => $user_info['id'],
  580. 'member_affected' => $memID,
  581. )),
  582. );
  583. logActions($log_changes);
  584. }
  585. // Have we got any post save functions to execute?
  586. if (!empty($context['profile_execute_on_save']))
  587. foreach ($context['profile_execute_on_save'] as $saveFunc)
  588. $saveFunc();
  589. // Let them know it worked!
  590. $context['profile_updated'] = $context['user']['is_owner'] ? $txt['profile_updated_own'] : sprintf($txt['profile_updated_else'], $cur_profile['member_name']);
  591. // Invalidate any cached data.
  592. cache_put_data('member_data-profile-' . $memID, null, 0);
  593. }
  594. }
  595. // Have some errors for some reason?
  596. if (!empty($post_errors))
  597. {
  598. // Set all the errors so the template knows what went wrong.
  599. foreach ($post_errors as $error_type)
  600. $context['modify_error'][$error_type] = true;
  601. }
  602. // If it's you then we should redirect upon save.
  603. elseif (!empty($profile_vars) && $context['user']['is_owner'] && !$context['do_preview'])
  604. redirectexit('action=profile;area=' . $current_area . ';updated');
  605. elseif (!empty($force_redirect))
  606. redirectexit('action=profile' . ($context['user']['is_owner'] ? '' : ';u=' . $memID) . ';area=' . $current_area);
  607. // Call the appropriate subaction function.
  608. $profile_include_data['function']($memID);
  609. // Set the page title if it's not already set...
  610. if (!isset($context['page_title']))
  611. $context['page_title'] = $txt['profile'] . (isset($txt[$current_area]) ? ' - ' . $txt[$current_area] : '');
  612. }
  613. /**
  614. * Load any custom fields for this area... no area means load all, 'summary' loads all public ones.
  615. *
  616. * @param int $memID
  617. * @param string $area = 'summary'
  618. */
  619. function loadCustomFields($memID, $area = 'summary')
  620. {
  621. global $context, $txt, $user_profile, $smcFunc, $user_info, $settings, $scripturl;
  622. // Get the right restrictions in place...
  623. $where = 'active = 1';
  624. if (!allowedTo('admin_forum') && $area != 'register')
  625. {
  626. // If it's the owner they can see two types of private fields, regardless.
  627. if ($memID == $user_info['id'])
  628. $where .= $area == 'summary' ? ' AND private < 3' : ' AND (private = 0 OR private = 2)';
  629. else
  630. $where .= $area == 'summary' ? ' AND private < 2' : ' AND private = 0';
  631. }
  632. if ($area == 'register')
  633. $where .= ' AND show_reg != 0';
  634. elseif ($area != 'summary')
  635. $where .= ' AND show_profile = {string:area}';
  636. // Load all the relevant fields - and data.
  637. $request = $smcFunc['db_query']('', '
  638. SELECT
  639. col_name, field_name, field_desc, field_type, show_reg, field_length, field_options,
  640. default_value, bbc, enclose, placement
  641. FROM {db_prefix}custom_fields
  642. WHERE ' . $where,
  643. array(
  644. 'area' => $area,
  645. )
  646. );
  647. $context['custom_fields'] = array();
  648. $context['custom_fields_required'] = false;
  649. while ($row = $smcFunc['db_fetch_assoc']($request))
  650. {
  651. // Shortcut.
  652. $exists = $memID && isset($user_profile[$memID], $user_profile[$memID]['options'][$row['col_name']]);
  653. $value = $exists ? $user_profile[$memID]['options'][$row['col_name']] : '';
  654. // If this was submitted already then make the value the posted version.
  655. if (isset($_POST['customfield']) && isset($_POST['customfield'][$row['col_name']]))
  656. {
  657. $value = $smcFunc['htmlspecialchars']($_POST['customfield'][$row['col_name']]);
  658. if (in_array($row['field_type'], array('select', 'radio')))
  659. $value = ($options = explode(',', $row['field_options'])) && isset($options[$value]) ? $options[$value] : '';
  660. }
  661. // HTML for the input form.
  662. $output_html = $value;
  663. if ($row['field_type'] == 'check')
  664. {
  665. $true = (!$exists && $row['default_value']) || $value;
  666. $input_html = '<input type="checkbox" name="customfield[' . $row['col_name'] . ']" ' . ($true ? 'checked="checked"' : '') . ' class="input_check" />';
  667. $output_html = $true ? $txt['yes'] : $txt['no'];
  668. }
  669. elseif ($row['field_type'] == 'select')
  670. {
  671. $input_html = '<select name="customfield[' . $row['col_name'] . ']"><option value="-1"></option>';
  672. $options = explode(',', $row['field_options']);
  673. foreach ($options as $k => $v)
  674. {
  675. $true = (!$exists && $row['default_value'] == $v) || $value == $v;
  676. $input_html .= '<option value="' . $k . '"' . ($true ? ' selected="selected"' : '') . '>' . $v . '</option>';
  677. if ($true)
  678. $output_html = $v;
  679. }
  680. $input_html .= '</select>';
  681. }
  682. elseif ($row['field_type'] == 'radio')
  683. {
  684. $input_html = '<fieldset>';
  685. $options = explode(',', $row['field_options']);
  686. foreach ($options as $k => $v)
  687. {
  688. $true = (!$exists && $row['default_value'] == $v) || $value == $v;
  689. $input_html .= '<label for="customfield_' . $row['col_name'] . '_' . $k . '"><input type="radio" name="customfield[' . $row['col_name'] . ']" class="input_radio" id="customfield_' . $row['col_name'] . '_' . $k . '" value="' . $k . '" ' . ($true ? 'checked="checked"' : '') . ' />' . $v . '</label><br />';
  690. if ($true)
  691. $output_html = $v;
  692. }
  693. $input_html .= '</fieldset>';
  694. }
  695. elseif ($row['field_type'] == 'text')
  696. {
  697. $input_html = '<input type="text" name="customfield[' . $row['col_name'] . ']" ' . ($row['field_length'] != 0 ? 'maxlength="' . $row['field_length'] . '"' : '') . ' size="' . ($row['field_length'] == 0 || $row['field_length'] >= 50 ? 50 : ($row['field_length'] > 30 ? 30 : ($row['field_length'] > 10 ? 20 : 10))) . '" value="' . $value . '" class="input_text" />';
  698. }
  699. else
  700. {
  701. @list ($rows, $cols) = @explode(',', $row['default_value']);
  702. $input_html = '<textarea name="customfield[' . $row['col_name'] . ']" ' . (!empty($rows) ? 'rows="' . $rows . '"' : '') . ' ' . (!empty($cols) ? 'cols="' . $cols . '"' : '') . '>' . $value . '</textarea>';
  703. }
  704. // Parse BBCode
  705. if ($row['bbc'])
  706. $output_html = parse_bbc($output_html);
  707. elseif($row['field_type'] == 'textarea')
  708. // Allow for newlines at least
  709. $output_html = strtr($output_html, array("\n" => '<br />'));
  710. // Enclosing the user input within some other text?
  711. if (!empty($row['enclose']) && !empty($output_html))
  712. $output_html = strtr($row['enclose'], array(
  713. '{SCRIPTURL}' => $scripturl,
  714. '{IMAGES_URL}' => $settings['images_url'],
  715. '{DEFAULT_IMAGES_URL}' => $settings['default_images_url'],
  716. '{INPUT}' => $output_html,
  717. ));
  718. $context['custom_fields'][] = array(
  719. 'name' => $row['field_name'],
  720. 'desc' => $row['field_desc'],
  721. 'type' => $row['field_type'],
  722. 'input_html' => $input_html,
  723. 'output_html' => $output_html,
  724. 'placement' => $row['placement'],
  725. 'colname' => $row['col_name'],
  726. 'value' => $value,
  727. 'show_reg' => $row['show_reg'],
  728. );
  729. $context['custom_fields_required'] = $context['custom_fields_required'] || $row['show_reg'];
  730. }
  731. $smcFunc['db_free_result']($request);
  732. call_integration_hook('integrate_load_custom_profile_fields', array($memID, $area));
  733. }
  734. ?>