UserProfile.php 36.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

namespace Q2A\Controllers\User;

21 22
use Q2A\Controllers\BaseController;
use Q2A\Database\DbConnection;
23
use Q2A\Http\Exceptions\PageNotFoundException;
24

25
class UserProfile extends BaseController
26 27 28 29
{
	private $userid;
	private $userhtml;

30
	public function __construct(DbConnection $db)
31 32 33 34 35
	{
		require_once QA_INCLUDE_DIR . 'db/users.php';
		require_once QA_INCLUDE_DIR . 'db/selects.php';
		require_once QA_INCLUDE_DIR . 'app/users.php';
		require_once QA_INCLUDE_DIR . 'app/format.php';
36 37

		parent::__construct($db);
38 39
	}

40 41 42 43 44 45 46 47 48 49 50 51
	public function index()
	{
		$handle = qa_get_logged_in_handle();
		qa_redirect(!empty($handle) ? 'user/' . $handle : 'users');
	}

	public function profile($handle)
	{
		// Get the HTML to display for the handle, and if we're using external users, determine the userid

		if (QA_FINAL_EXTERNAL_USERS) {
			$userid = qa_handle_to_userid($handle);
52 53 54
			if (!isset($userid)) { // check the user exists
				throw new PageNotFoundException();
			}
55 56 57

			$usershtml = qa_get_users_html(array($userid), false, qa_path_to_root(), true);
			$userhtml = @$usershtml[$userid];
58
		} else {
59
			$userhtml = qa_html($handle);
60
		}
61 62 63 64


		// Redirect to 'My Account' page if button clicked

65
		if (qa_clicked('doaccount')) {
66
			qa_redirect('account');
67
		}
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88


		// Find the user profile and questions and answers for this handle

		$loginuserid = qa_get_logged_in_userid();
		$identifier = QA_FINAL_EXTERNAL_USERS ? $userid : $handle;

		list($useraccount, $userprofile, $userfields, $usermessages, $userpoints, $userlevels, $navcategories, $userrank) =
			qa_db_select_with_pending(
				QA_FINAL_EXTERNAL_USERS ? null : qa_db_user_account_selectspec($handle, false),
				QA_FINAL_EXTERNAL_USERS ? null : qa_db_user_profile_selectspec($handle, false),
				QA_FINAL_EXTERNAL_USERS ? null : qa_db_userfields_selectspec(),
				QA_FINAL_EXTERNAL_USERS ? null : qa_db_recent_messages_selectspec(null, null, $handle, false, qa_opt_if_loaded('page_size_wall')),
				qa_db_user_points_selectspec($identifier),
				qa_db_user_levels_selectspec($identifier, QA_FINAL_EXTERNAL_USERS, true),
				qa_db_category_nav_selectspec(null, true),
				qa_db_user_rank_selectspec($identifier)
			);

		if (!QA_FINAL_EXTERNAL_USERS && $handle !== qa_get_logged_in_handle()) {
			foreach ($userfields as $index => $userfield) {
89
				if (isset($userfield['permit']) && qa_permit_value_error($userfield['permit'], $loginuserid, qa_get_logged_in_level(), qa_get_logged_in_flags())) {
90
					unset($userfields[$index]); // don't pay attention to user fields we're not allowed to view
91
				}
92 93 94 95 96 97 98 99 100 101
			}
		}


		// Check the user exists and work out what can and can't be set (if not using single sign-on)

		$errors = array();

		$loginlevel = qa_get_logged_in_level();

102 103
		// if we're using integrated user management, we can know and show more
		if (!QA_FINAL_EXTERNAL_USERS) {
104 105
			require_once QA_INCLUDE_DIR . 'app/messages.php';

106 107 108
			if (!is_array($userpoints) && !is_array($useraccount)) { // check the user exists
				throw new PageNotFoundException();
			}
109 110 111 112 113 114

			$userid = $useraccount['userid'];
			$fieldseditable = false;
			$maxlevelassign = null;

			$maxuserlevel = $useraccount['level'];
115
			foreach ($userlevels as $userlevel) {
116
				$maxuserlevel = max($maxuserlevel, $userlevel['level']);
117
			}
118 119 120 121

			if (isset($loginuserid) && $loginuserid != $userid &&
				($loginlevel >= QA_USER_LEVEL_SUPER || $loginlevel > $maxuserlevel) &&
				!qa_user_permit_error()
122 123
			) {
				// can't change self - or someone on your level (or higher, obviously) unless you're a super admin
124
				if ($loginlevel >= QA_USER_LEVEL_SUPER) {
125
					$maxlevelassign = QA_USER_LEVEL_SUPER;
126
				} elseif ($loginlevel >= QA_USER_LEVEL_ADMIN) {
127
					$maxlevelassign = QA_USER_LEVEL_MODERATOR;
128
				} elseif ($loginlevel >= QA_USER_LEVEL_MODERATOR) {
129
					$maxlevelassign = QA_USER_LEVEL_EXPERT;
130
				}
131

132
				if ($loginlevel >= QA_USER_LEVEL_ADMIN) {
133
					$fieldseditable = true;
134
				}
135

136
				if (isset($maxlevelassign) && ($useraccount['flags'] & QA_USER_FLAGS_USER_BLOCKED)) {
137
					$maxlevelassign = min($maxlevelassign, QA_USER_LEVEL_EDITOR); // if blocked, can't promote too high
138
				}
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
			}

			$approvebutton = isset($maxlevelassign)
				&& $useraccount['level'] < QA_USER_LEVEL_APPROVED
				&& $maxlevelassign >= QA_USER_LEVEL_APPROVED
				&& !($useraccount['flags'] & QA_USER_FLAGS_USER_BLOCKED)
				&& qa_opt('moderate_users');
			$usereditbutton = $fieldseditable || isset($maxlevelassign);
			$userediting = $usereditbutton && (qa_get_state() == 'edit');

			$wallposterrorhtml = qa_wall_error_html($loginuserid, $useraccount['userid'], $useraccount['flags']);

			// This code is similar but not identical to that in to qq-page-user-wall.php

			$usermessages = array_slice($usermessages, 0, qa_opt('page_size_wall'));
			$usermessages = qa_wall_posts_add_rules($usermessages, 0);

			foreach ($usermessages as $message) {
				if ($message['deleteable'] && qa_clicked('m' . $message['messageid'] . '_dodelete')) {
158
					if (!qa_check_form_security_code('wall-' . $useraccount['handle'], qa_post_text('code'))) {
159
						$errors['page'] = qa_lang_html('misc/form_security_again');
160
					} else {
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
						qa_wall_delete_post($loginuserid, qa_get_logged_in_handle(), qa_cookie_get(), $message);
						qa_redirect(qa_request(), null, null, null, 'wall');
					}
				}
			}
		}


		// Process edit or save button for user, and other actions

		if (!QA_FINAL_EXTERNAL_USERS) {
			$reloaduser = false;

			if ($usereditbutton) {
				if (qa_clicked('docancel')) {
					qa_redirect(qa_request());
				} elseif (qa_clicked('doedit')) {
					qa_redirect(qa_request(), array('state' => 'edit'));
				} elseif (qa_clicked('dosave')) {
					require_once QA_INCLUDE_DIR . 'app/users-edit.php';
					require_once QA_INCLUDE_DIR . 'db/users.php';

					$inemail = qa_post_text('email');

					$inprofile = array();
186
					foreach ($userfields as $userfield) {
187
						$inprofile[$userfield['fieldid']] = qa_post_text('field_' . $userfield['fieldid']);
188
					}
189 190 191 192 193 194 195 196 197 198 199 200

					if (!qa_check_form_security_code('user-edit-' . $handle, qa_post_text('code'))) {
						$errors['page'] = qa_lang_html('misc/form_security_again');
						$userediting = true;
					} else {
						if (qa_post_text('removeavatar')) {
							qa_db_user_set_flag($userid, QA_USER_FLAGS_SHOW_AVATAR, false);
							qa_db_user_set_flag($userid, QA_USER_FLAGS_SHOW_GRAVATAR, false);

							if (isset($useraccount['avatarblobid'])) {
								require_once QA_INCLUDE_DIR . 'app/blobs.php';

201 202 203 204 205
								qa_db_user_set($userid, array(
									'avatarblobid' => null,
									'avatarwidth' => null,
									'avatarheight' => null,
								));
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
								qa_delete_blob($useraccount['avatarblobid']);
							}
						}

						if ($fieldseditable) {
							$filterhandle = $handle; // we're not filtering the handle...
							$errors = qa_handle_email_filter($filterhandle, $inemail, $useraccount);
							unset($errors['handle']); // ...and we don't care about any errors in it

							if (!isset($errors['email'])) {
								if ($inemail != $useraccount['email']) {
									qa_db_user_set($userid, 'email', $inemail);
									qa_db_user_set_flag($userid, QA_USER_FLAGS_EMAIL_CONFIRMED, false);
								}
							}

							if (count($inprofile)) {
								$filtermodules = qa_load_modules_with('filter', 'filter_profile');
224
								foreach ($filtermodules as $filtermodule) {
225
									$filtermodule->filter_profile($inprofile, $errors, $useraccount, $userprofile);
226
								}
227 228 229
							}

							foreach ($userfields as $userfield) {
230
								if (!isset($errors[$userfield['fieldid']])) {
231
									qa_db_user_profile_set($userid, $userfield['title'], $inprofile[$userfield['fieldid']]);
232
								}
233 234
							}

235
							if (count($errors)) {
236
								$userediting = true;
237
							}
238 239 240 241 242 243 244 245 246

							qa_report_event('u_edit', $loginuserid, qa_get_logged_in_handle(), qa_cookie_get(), array(
								'userid' => $userid,
								'handle' => $useraccount['handle'],
							));
						}

						if (isset($maxlevelassign)) {
							$inlevel = min($maxlevelassign, (int)qa_post_text('level')); // constrain based on maximum permitted to prevent simple browser-based attack
247
							if ($inlevel != $useraccount['level']) {
248
								qa_set_user_level($userid, $useraccount['handle'], $inlevel, $useraccount['level']);
249
							}
250 251 252 253 254 255

							if (qa_using_categories()) {
								$inuserlevels = array();

								for ($index = 1; $index <= 999; $index++) {
									$inlevel = qa_post_text('uc_' . $index . '_level');
256
									if (!isset($inlevel)) {
257
										break;
258
									}
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

									$categoryid = qa_get_category_field_value('uc_' . $index . '_cat');

									if (strlen($categoryid) && strlen($inlevel)) {
										$inuserlevels[] = array(
											'entitytype' => QA_ENTITY_CATEGORY,
											'entityid' => $categoryid,
											'level' => min($maxlevelassign, (int)$inlevel),
										);
									}
								}

								qa_db_user_levels_set($userid, $inuserlevels);
							}
						}

275
						if (empty($errors)) {
276
							qa_redirect(qa_request());
277
						}
278 279 280 281 282 283 284 285 286 287 288

						list($useraccount, $userprofile, $userlevels) = qa_db_select_with_pending(
							qa_db_user_account_selectspec($userid, true),
							qa_db_user_profile_selectspec($userid, true),
							qa_db_user_levels_selectspec($userid, true, true)
						);
					}
				}
			}

			if (qa_clicked('doapprove') || qa_clicked('doblock') || qa_clicked('dounblock') || qa_clicked('dohideall') || qa_clicked('dodelete')) {
Scott committed
289
				if (!qa_check_form_security_code('user-' . $handle, qa_post_text('code'))) {
290
					$errors['page'] = qa_lang_html('misc/form_security_again');
Scott committed
291
				} else {
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
					if ($approvebutton && qa_clicked('doapprove')) {
						require_once QA_INCLUDE_DIR . 'app/users-edit.php';
						qa_set_user_level($userid, $useraccount['handle'], QA_USER_LEVEL_APPROVED, $useraccount['level']);
						qa_redirect(qa_request());
					}

					if (isset($maxlevelassign) && ($maxuserlevel < QA_USER_LEVEL_MODERATOR)) {
						if (qa_clicked('doblock')) {
							require_once QA_INCLUDE_DIR . 'app/users-edit.php';

							qa_set_user_blocked($userid, $useraccount['handle'], true);
							qa_redirect(qa_request());
						}

						if (qa_clicked('dounblock')) {
							require_once QA_INCLUDE_DIR . 'app/users-edit.php';

							qa_set_user_blocked($userid, $useraccount['handle'], false);
							qa_redirect(qa_request());
						}

						if (qa_clicked('dohideall') && !qa_user_permit_error('permit_hide_show')) {
							require_once QA_INCLUDE_DIR . 'db/admin.php';
							require_once QA_INCLUDE_DIR . 'app/posts.php';

							$postids = qa_db_get_user_visible_postids($userid);

319
							foreach ($postids as $postid) {
320
								qa_post_set_status($postid, QA_POST_STATUS_HIDDEN, $loginuserid);
321
							}
322 323 324 325 326 327 328

							qa_redirect(qa_request());
						}

						if (qa_clicked('dodelete') && ($loginlevel >= QA_USER_LEVEL_ADMIN)) {
							require_once QA_INCLUDE_DIR . 'app/users-edit.php';

Scott committed
329 330 331 332 333
							qa_report_event('u_delete_before', $loginuserid, qa_get_logged_in_handle(), qa_cookie_get(), array(
								'userid' => $userid,
								'handle' => $useraccount['handle'],
							));

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
							qa_delete_user($userid);

							qa_report_event('u_delete', $loginuserid, qa_get_logged_in_handle(), qa_cookie_get(), array(
								'userid' => $userid,
								'handle' => $useraccount['handle'],
							));

							qa_redirect('users');
						}
					}
				}
			}


			if (qa_clicked('dowallpost')) {
				$inmessage = qa_post_text('message');

				if (!strlen($inmessage)) {
					$errors['message'] = qa_lang('profile/post_wall_empty');
				} elseif (!qa_check_form_security_code('wall-' . $useraccount['handle'], qa_post_text('code'))) {
					$errors['message'] = qa_lang_html('misc/form_security_again');
				} elseif (!$wallposterrorhtml) {
					qa_wall_add_post($loginuserid, qa_get_logged_in_handle(), qa_cookie_get(), $userid, $useraccount['handle'], $inmessage, '');
					qa_redirect(qa_request(), null, null, null, 'wall');
				}
			}
		}


		// Process bonus setting button

		if ($loginlevel >= QA_USER_LEVEL_ADMIN && qa_clicked('dosetbonus')) {
			require_once QA_INCLUDE_DIR . 'db/points.php';

			$inbonus = (int)qa_post_text('bonus');

			if (!qa_check_form_security_code('user-activity-' . $handle, qa_post_text('code'))) {
				$errors['page'] = qa_lang_html('misc/form_security_again');
			} else {
				qa_db_points_set_bonus($userid, $inbonus);
				qa_db_points_update_ifuser($userid, null);
				qa_redirect(qa_request(), null, null, null, 'activity');
			}
		}


		// Prepare content for theme

		$qa_content = qa_content_prepare();

		$qa_content['title'] = qa_lang_html_sub('profile/user_x', $userhtml);
		$qa_content['error'] = @$errors['page'];

		if (isset($loginuserid) && $loginuserid != $useraccount['userid'] && !QA_FINAL_EXTERNAL_USERS) {
			$favoritemap = qa_get_favorite_non_qs_map();
			$favorite = @$favoritemap['user'][$useraccount['userid']];

391 392
			$title = qa_lang_sub($favorite ? 'main/remove_x_favorites' : 'users/add_user_x_favorites', $handle);
			$qa_content['favorite'] = qa_favorite_form(QA_ENTITY_USER, $useraccount['userid'], $favorite, $title);
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
		}


		// General information about the user, only available if we're using internal user management

		if (!QA_FINAL_EXTERNAL_USERS) {
			$membertime = qa_time_to_string(qa_opt('db_time') - $useraccount['created']);
			$joindate = qa_when_to_html($useraccount['created'], 0);

			$qa_content['form_profile'] = array(
				'tags' => 'method="post" action="' . qa_self_html() . '"',

				'style' => 'wide',

				'fields' => array(
					'avatar' => array(
						'type' => 'image',
						'style' => 'tall',
						'label' => '',
412 413 414 415 416 417 418 419 420
						'html' => qa_get_user_avatar_html(
							$useraccount['flags'],
							$useraccount['email'],
							$useraccount['handle'],
							$useraccount['avatarblobid'],
							$useraccount['avatarwidth'],
							$useraccount['avatarheight'],
							qa_opt('avatar_profile_size')
						),
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
						'id' => 'avatar',
					),

					'removeavatar' => null,

					'duration' => array(
						'type' => 'static',
						'label' => qa_lang_html('users/member_for'),
						'value' => qa_html($membertime . ' (' . qa_lang_sub('main/since_x', $joindate['data']) . ')'),
						'id' => 'duration',
					),

					'level' => array(
						'type' => 'static',
						'label' => qa_lang_html('users/member_type'),
						'tags' => 'name="level"',
						'value' => qa_html(qa_user_level_string($useraccount['level'])),
						'note' => (($useraccount['flags'] & QA_USER_FLAGS_USER_BLOCKED) && isset($maxlevelassign)) ? qa_lang_html('users/user_blocked') : '',
						'id' => 'level',
					),
				),
			);

444
			if (empty($qa_content['form_profile']['fields']['avatar']['html'])) {
445
				unset($qa_content['form_profile']['fields']['avatar']);
446
			}
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465


			// Private message link

			if (qa_opt('allow_private_messages') && isset($loginuserid) && $loginuserid != $userid && !($useraccount['flags'] & QA_USER_FLAGS_NO_MESSAGES) && !$userediting) {
				$qa_content['form_profile']['fields']['level']['value'] .= strtr(qa_lang_html('profile/send_private_message'), array(
					'^1' => '<a href="' . qa_path_html('message/' . $handle) . '">',
					'^2' => '</a>',
				));
			}


			// Levels editing or viewing (add category-specific levels)

			if ($userediting) {
				if (isset($maxlevelassign)) {
					$qa_content['form_profile']['fields']['level']['type'] = 'select';

					$showlevels = array(QA_USER_LEVEL_BASIC);
466
					if (qa_opt('moderate_users')) {
467
						$showlevels[] = QA_USER_LEVEL_APPROVED;
468
					}
469 470 471 472 473 474 475 476 477

					array_push($showlevels, QA_USER_LEVEL_EXPERT, QA_USER_LEVEL_EDITOR, QA_USER_LEVEL_MODERATOR, QA_USER_LEVEL_ADMIN, QA_USER_LEVEL_SUPER);

					$leveloptions = array();
					$catleveloptions = array('' => qa_lang_html('users/category_level_none'));

					foreach ($showlevels as $showlevel) {
						if ($showlevel <= $maxlevelassign) {
							$leveloptions[$showlevel] = qa_html(qa_user_level_string($showlevel));
478
							if ($showlevel > QA_USER_LEVEL_BASIC) {
479
								$catleveloptions[$showlevel] = $leveloptions[$showlevel];
480
							}
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
						}
					}

					$qa_content['form_profile']['fields']['level']['options'] = $leveloptions;


					// Category-specific levels

					if (qa_using_categories()) {
						$catleveladd = strlen(qa_get('catleveladd')) > 0;

						if (!$catleveladd && !count($userlevels)) {
							$qa_content['form_profile']['fields']['level']['suffix'] = strtr(qa_lang_html('users/category_level_add'), array(
								'^1' => '<a href="' . qa_path_html(qa_request(), array('state' => 'edit', 'catleveladd' => 1)) . '">',
								'^2' => '</a>',
							));
						} else {
							$qa_content['form_profile']['fields']['level']['suffix'] = qa_lang_html('users/level_in_general');
						}

501
						if ($catleveladd || count($userlevels)) {
502
							$userlevels[] = array('entitytype' => QA_ENTITY_CATEGORY);
503
						}
504 505 506 507 508

						$index = 0;
						foreach ($userlevels as $userlevel) {
							if ($userlevel['entitytype'] == QA_ENTITY_CATEGORY) {
								$index++;
Scott committed
509
								$id = 'ls_' . (+$index);
510 511 512 513 514 515 516 517 518 519 520 521

								$qa_content['form_profile']['fields']['uc_' . $index . '_level'] = array(
									'label' => qa_lang_html('users/category_level_label'),
									'type' => 'select',
									'tags' => 'name="uc_' . $index . '_level" id="' . qa_html($id) . '" onchange="this.qa_prev=this.options[this.selectedIndex].value;"',
									'options' => $catleveloptions,
									'value' => isset($userlevel['level']) ? qa_html(qa_user_level_string($userlevel['level'])) : '',
									'suffix' => qa_lang_html('users/category_level_in'),
								);

								$qa_content['form_profile']['fields']['uc_' . $index . '_cat'] = array();

522
								if (isset($userlevel['entityid'])) {
523
									$fieldnavcategories = qa_db_select_with_pending(qa_db_category_nav_selectspec($userlevel['entityid'], true));
524
								} else {
525
									$fieldnavcategories = $navcategories;
526
								}
527

528 529 530 531 532 533 534 535 536
								qa_set_up_category_field(
									$qa_content,
									$qa_content['form_profile']['fields']['uc_' . $index . '_cat'],
									'uc_' . $index . '_cat',
									$fieldnavcategories,
									@$userlevel['entityid'],
									true,
									true
								);
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592

								unset($qa_content['form_profile']['fields']['uc_' . $index . '_cat']['note']);
							}
						}

						$qa_content['script_lines'][] = array(
							"function qa_update_category_levels()",
							"{",
							"\tglob=document.getElementById('level_select');",
							"\tif (!glob)",
							"\t\treturn;",
							"\tvar opts=glob.options;",
							"\tvar lev=parseInt(opts[glob.selectedIndex].value);",
							"\tfor (var i=1; i<9999; i++) {",
							"\t\tvar sel=document.getElementById('ls_'+i);",
							"\t\tif (!sel)",
							"\t\t\tbreak;",
							"\t\tsel.qa_prev=sel.qa_prev || sel.options[sel.selectedIndex].value;",
							"\t\tsel.options.length=1;", // just leaves "no upgrade" element
							"\t\tfor (var j=0; j<opts.length; j++)",
							"\t\t\tif (parseInt(opts[j].value)>lev)",
							"\t\t\t\tsel.options[sel.options.length]=new Option(opts[j].text, opts[j].value, false, (opts[j].value==sel.qa_prev));",
							"\t}",
							"}",
						);

						$qa_content['script_onloads'][] = array(
							"qa_update_category_levels();",
						);

						$qa_content['form_profile']['fields']['level']['tags'] .= ' id="level_select" onchange="qa_update_category_levels();"';
					}
				}
			} else {
				foreach ($userlevels as $userlevel) {
					if ($userlevel['entitytype'] == QA_ENTITY_CATEGORY && $userlevel['level'] > $useraccount['level']) {
						$qa_content['form_profile']['fields']['level']['value'] .= '<br/>' .
							strtr(qa_lang_html('users/level_for_category'), array(
								'^1' => qa_html(qa_user_level_string($userlevel['level'])),
								'^2' => '<a href="' . qa_path_html(implode('/', array_reverse(explode('/', $userlevel['backpath'])))) . '">' . qa_html($userlevel['title']) . '</a>',
							));
					}
				}
			}


			// Show any extra privileges due to user's level or their points

			$showpermits = array();
			$permitoptions = qa_get_permit_options();

			foreach ($permitoptions as $permitoption) {
				// if not available to approved and email confirmed users with no points, but yes available to the user, it's something special
				if (qa_permit_error($permitoption, $userid, QA_USER_LEVEL_APPROVED, QA_USER_FLAGS_EMAIL_CONFIRMED, 0) &&
					!qa_permit_error($permitoption, $userid, $useraccount['level'], $useraccount['flags'], $userpoints['points'])
				) {
593
					if ($permitoption == 'permit_retag_cat') {
594
						$showpermits[] = qa_lang(qa_using_categories() ? 'profile/permit_recat' : 'profile/permit_retag');
595
					} else {
596
						$showpermits[] = qa_lang('profile/' . $permitoption); // then show it as an extra priviliege
597
					}
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
				}
			}

			if (count($showpermits)) {
				$qa_content['form_profile']['fields']['permits'] = array(
					'type' => 'static',
					'label' => qa_lang_html('profile/extra_privileges'),
					'value' => qa_html(implode("\n", $showpermits), true),
					'rows' => count($showpermits),
					'id' => 'permits',
				);
			}


			// Show email address only if we're an administrator

			if ($loginlevel >= QA_USER_LEVEL_ADMIN && !qa_user_permit_error()) {
				$doconfirms = qa_opt('confirm_user_emails') && $useraccount['level'] < QA_USER_LEVEL_EXPERT;
				$isconfirmed = ($useraccount['flags'] & QA_USER_FLAGS_EMAIL_CONFIRMED) > 0;
				$htmlemail = qa_html(isset($inemail) ? $inemail : $useraccount['email']);

				$qa_content['form_profile']['fields']['email'] = array(
620
					'type' => $userediting ? 'email' : 'static',
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
					'label' => qa_lang_html('users/email_label'),
					'tags' => 'name="email"',
					'value' => $userediting ? $htmlemail : ('<a href="mailto:' . $htmlemail . '">' . $htmlemail . '</a>'),
					'error' => qa_html(@$errors['email']),
					'note' => ($doconfirms ? (qa_lang_html($isconfirmed ? 'users/email_confirmed' : 'users/email_not_confirmed') . ' ') : '') .
						($userediting ? '' : qa_lang_html('users/only_shown_admins')),
					'id' => 'email',
				);
			}


			// Show IP addresses and times for last login or write - only if we're a moderator or higher

			if ($loginlevel >= QA_USER_LEVEL_MODERATOR && !qa_user_permit_error()) {
				$qa_content['form_profile']['fields']['lastlogin'] = array(
					'type' => 'static',
					'label' => qa_lang_html('users/last_login_label'),
					'value' =>
						strtr(qa_lang_html('users/x_ago_from_y'), array(
							'^1' => qa_time_to_string(qa_opt('db_time') - $useraccount['loggedin']),
							'^2' => qa_ip_anchor_html(@inet_ntop($useraccount['loginip'])),
						)),
					'note' => $userediting ? null : qa_lang_html('users/only_shown_moderators'),
					'id' => 'lastlogin',
				);

				if (isset($useraccount['written'])) {
					$qa_content['form_profile']['fields']['lastwrite'] = array(
						'type' => 'static',
						'label' => qa_lang_html('users/last_write_label'),
						'value' =>
							strtr(qa_lang_html('users/x_ago_from_y'), array(
								'^1' => qa_time_to_string(qa_opt('db_time') - $useraccount['written']),
								'^2' => qa_ip_anchor_html(@inet_ntop($useraccount['writeip'])),
							)),
						'note' => $userediting ? null : qa_lang_html('users/only_shown_moderators'),
						'id' => 'lastwrite',
					);
				} else {
					unset($qa_content['form_profile']['fields']['lastwrite']);
				}
			}


			// Show other profile fields

			$fieldsediting = $fieldseditable && $userediting;

			foreach ($userfields as $userfield) {
				if (($userfield['flags'] & QA_FIELD_FLAGS_LINK_URL) && !$fieldsediting) {
					$valuehtml = qa_url_to_html_link(@$userprofile[$userfield['title']], qa_opt('links_in_new_window'));
				} else {
					$value = @$inprofile[$userfield['fieldid']];
674
					if (!isset($value)) {
675
						$value = @$userprofile[$userfield['title']];
676
					}
677 678 679 680 681

					$valuehtml = qa_html($value, (($userfield['flags'] & QA_FIELD_FLAGS_MULTI_LINE) && !$fieldsediting));
				}

				$label = trim(qa_user_userfield_label($userfield), ':');
682
				if (strlen($label)) {
683
					$label .= ':';
684
				}
685 686 687

				$notehtml = null;
				if (isset($userfield['permit']) && !$userediting) {
688
					if ($userfield['permit'] <= QA_PERMIT_ADMINS) {
689
						$notehtml = qa_lang_html('users/only_shown_admins');
690
					} elseif ($userfield['permit'] <= QA_PERMIT_MODERATORS) {
691
						$notehtml = qa_lang_html('users/only_shown_moderators');
692
					} elseif ($userfield['permit'] <= QA_PERMIT_EDITORS) {
693
						$notehtml = qa_lang_html('users/only_shown_editors');
694
					} elseif ($userfield['permit'] <= QA_PERMIT_EXPERTS) {
695
						$notehtml = qa_lang_html('users/only_shown_experts');
696
					}
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
				}

				$qa_content['form_profile']['fields'][$userfield['title']] = array(
					'type' => $fieldsediting ? 'text' : 'static',
					'label' => qa_html($label),
					'tags' => 'name="field_' . $userfield['fieldid'] . '"',
					'value' => $valuehtml,
					'error' => qa_html(@$errors[$userfield['fieldid']]),
					'note' => $notehtml,
					'rows' => ($userfield['flags'] & QA_FIELD_FLAGS_MULTI_LINE) ? 8 : null,
					'id' => 'userfield-' . $userfield['fieldid'],
				);
			}


			// Edit form or button, if appropriate

			if ($userediting) {
				if ((qa_opt('avatar_allow_gravatar') && ($useraccount['flags'] & QA_USER_FLAGS_SHOW_GRAVATAR)) ||
					(qa_opt('avatar_allow_upload') && ($useraccount['flags'] & QA_USER_FLAGS_SHOW_AVATAR) && isset($useraccount['avatarblobid']))
				) {
					$qa_content['form_profile']['fields']['removeavatar'] = array(
						'type' => 'checkbox',
						'label' => qa_lang_html('users/remove_avatar'),
						'tags' => 'name="removeavatar"',
					);
				}

				$qa_content['form_profile']['buttons'] = array(
					'save' => array(
						'tags' => 'onclick="qa_show_waiting_after(this, false);"',
						'label' => qa_lang_html('users/save_user'),
					),

					'cancel' => array(
						'tags' => 'name="docancel"',
						'label' => qa_lang_html('main/cancel_button'),
					),
				);

				$qa_content['form_profile']['hidden'] = array(
					'dosave' => '1',
					'code' => qa_get_form_security_code('user-edit-' . $handle),
				);
			} elseif ($usereditbutton) {
				$qa_content['form_profile']['buttons'] = array();

				if ($approvebutton) {
					$qa_content['form_profile']['buttons']['approve'] = array(
						'tags' => 'name="doapprove"',
						'label' => qa_lang_html('users/approve_user_button'),
					);
				}

				$qa_content['form_profile']['buttons']['edit'] = array(
					'tags' => 'name="doedit"',
					'label' => qa_lang_html('users/edit_user_button'),
				);

				if (isset($maxlevelassign) && $useraccount['level'] < QA_USER_LEVEL_MODERATOR) {
					if ($useraccount['flags'] & QA_USER_FLAGS_USER_BLOCKED) {
						$qa_content['form_profile']['buttons']['unblock'] = array(
							'tags' => 'name="dounblock"',
							'label' => qa_lang_html('users/unblock_user_button'),
						);

						if (!qa_user_permit_error('permit_hide_show')) {
							$qa_content['form_profile']['buttons']['hideall'] = array(
								'tags' => 'name="dohideall" onclick="qa_show_waiting_after(this, false);"',
								'label' => qa_lang_html('users/hide_all_user_button'),
							);
						}

						if ($loginlevel >= QA_USER_LEVEL_ADMIN) {
							$qa_content['form_profile']['buttons']['delete'] = array(
								'tags' => 'name="dodelete" onclick="qa_show_waiting_after(this, false);"',
								'label' => qa_lang_html('users/delete_user_button'),
							);
						}
					} else {
						$qa_content['form_profile']['buttons']['block'] = array(
							'tags' => 'name="doblock"',
							'label' => qa_lang_html('users/block_user_button'),
						);
					}

					$qa_content['form_profile']['hidden'] = array(
						'code' => qa_get_form_security_code('user-' . $handle),
					);
				}
			} elseif (isset($loginuserid) && ($loginuserid == $userid)) {
				$qa_content['form_profile']['buttons'] = array(
					'account' => array(
						'tags' => 'name="doaccount"',
						'label' => qa_lang_html('users/edit_profile'),
					),
				);
			}


797
			if (!is_array($qa_content['form_profile']['fields']['removeavatar'])) {
798
				unset($qa_content['form_profile']['fields']['removeavatar']);
799
			}
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871

			$qa_content['raw']['account'] = $useraccount; // for plugin layers to access
			$qa_content['raw']['profile'] = $userprofile;
		}


		// Information about user activity, available also with single sign-on integration

		$qa_content['form_activity'] = array(
			'title' => '<span id="activity">' . qa_lang_html_sub('profile/activity_by_x', $userhtml) . '</span>',

			'style' => 'wide',

			'fields' => array(
				'bonus' => array(
					'label' => qa_lang_html('profile/bonus_points'),
					'tags' => 'name="bonus"',
					'value' => qa_html(isset($inbonus) ? $inbonus : $userpoints['bonus']),
					'type' => 'number',
					'note' => qa_lang_html('users/only_shown_admins'),
					'id' => 'bonus',
				),

				'points' => array(
					'type' => 'static',
					'label' => qa_lang_html('profile/score'),
					'value' => (@$userpoints['points'] == 1)
						? qa_lang_html_sub('main/1_point', '<span class="qa-uf-user-points">1</span>', '1')
						: qa_lang_html_sub('main/x_points', '<span class="qa-uf-user-points">' . qa_html(qa_format_number(@$userpoints['points'])) . '</span>'),
					'id' => 'points',
				),

				'title' => array(
					'type' => 'static',
					'label' => qa_lang_html('profile/title'),
					'value' => qa_get_points_title_html(@$userpoints['points'], qa_get_points_to_titles()),
					'id' => 'title',
				),

				'questions' => array(
					'type' => 'static',
					'label' => qa_lang_html('profile/questions'),
					'value' => '<span class="qa-uf-user-q-posts">' . qa_html(qa_format_number(@$userpoints['qposts'])) . '</span>',
					'id' => 'questions',
				),

				'answers' => array(
					'type' => 'static',
					'label' => qa_lang_html('profile/answers'),
					'value' => '<span class="qa-uf-user-a-posts">' . qa_html(qa_format_number(@$userpoints['aposts'])) . '</span>',
					'id' => 'answers',
				),
			),
		);

		if ($loginlevel >= QA_USER_LEVEL_ADMIN) {
			$qa_content['form_activity']['tags'] = 'method="post" action="' . qa_self_html() . '"';

			$qa_content['form_activity']['buttons'] = array(
				'setbonus' => array(
					'tags' => 'name="dosetbonus"',
					'label' => qa_lang_html('profile/set_bonus_button'),
				),
			);

			$qa_content['form_activity']['hidden'] = array(
				'code' => qa_get_form_security_code('user-activity-' . $handle),
			);
		} else {
			unset($qa_content['form_activity']['fields']['bonus']);
		}

872
		if (!isset($qa_content['form_activity']['fields']['title']['value'])) {
873
			unset($qa_content['form_activity']['fields']['title']);
874
		}
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894

		if (qa_opt('comment_on_qs') || qa_opt('comment_on_as')) { // only show comment count if comments are enabled
			$qa_content['form_activity']['fields']['comments'] = array(
				'type' => 'static',
				'label' => qa_lang_html('profile/comments'),
				'value' => '<span class="qa-uf-user-c-posts">' . qa_html(qa_format_number(@$userpoints['cposts'])) . '</span>',
				'id' => 'comments',
			);
		}

		if (qa_opt('voting_on_qs') || qa_opt('voting_on_as')) { // only show vote record if voting is enabled
			$votedonvalue = '';

			if (qa_opt('voting_on_qs')) {
				$qvotes = @$userpoints['qupvotes'] + @$userpoints['qdownvotes'];

				$innervalue = '<span class="qa-uf-user-q-votes">' . qa_format_number($qvotes) . '</span>';
				$votedonvalue .= ($qvotes == 1) ? qa_lang_html_sub('main/1_question', $innervalue, '1')
					: qa_lang_html_sub('main/x_questions', $innervalue);

895
				if (qa_opt('voting_on_as')) {
896
					$votedonvalue .= ', ';
897
				}
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
			}

			if (qa_opt('voting_on_as')) {
				$avotes = @$userpoints['aupvotes'] + @$userpoints['adownvotes'];

				$innervalue = '<span class="qa-uf-user-a-votes">' . qa_format_number($avotes) . '</span>';
				$votedonvalue .= ($avotes == 1) ? qa_lang_html_sub('main/1_answer', $innervalue, '1')
					: qa_lang_html_sub('main/x_answers', $innervalue);
			}

			$qa_content['form_activity']['fields']['votedon'] = array(
				'type' => 'static',
				'label' => qa_lang_html('profile/voted_on'),
				'value' => $votedonvalue,
				'id' => 'votedon',
			);

			$upvotes = @$userpoints['qupvotes'] + @$userpoints['aupvotes'];
			$innervalue = '<span class="qa-uf-user-upvotes">' . qa_format_number($upvotes) . '</span>';
			$votegavevalue = (($upvotes == 1) ? qa_lang_html_sub('profile/1_up_vote', $innervalue, '1') : qa_lang_html_sub('profile/x_up_votes', $innervalue)) . ', ';

			$downvotes = @$userpoints['qdownvotes'] + @$userpoints['adownvotes'];
			$innervalue = '<span class="qa-uf-user-downvotes">' . qa_format_number($downvotes) . '</span>';
			$votegavevalue .= ($downvotes == 1) ? qa_lang_html_sub('profile/1_down_vote', $innervalue, '1') : qa_lang_html_sub('profile/x_down_votes', $innervalue);

			$qa_content['form_activity']['fields']['votegave'] = array(
				'type' => 'static',
				'label' => qa_lang_html('profile/gave_out'),
				'value' => $votegavevalue,
				'id' => 'votegave',
			);

			$innervalue = '<span class="qa-uf-user-upvoteds">' . qa_format_number(@$userpoints['upvoteds']) . '</span>';
			$votegotvalue = ((@$userpoints['upvoteds'] == 1) ? qa_lang_html_sub('profile/1_up_vote', $innervalue, '1')
					: qa_lang_html_sub('profile/x_up_votes', $innervalue)) . ', ';

			$innervalue = '<span class="qa-uf-user-downvoteds">' . qa_format_number(@$userpoints['downvoteds']) . '</span>';
			$votegotvalue .= (@$userpoints['downvoteds'] == 1) ? qa_lang_html_sub('profile/1_down_vote', $innervalue, '1')
				: qa_lang_html_sub('profile/x_down_votes', $innervalue);

			$qa_content['form_activity']['fields']['votegot'] = array(
				'type' => 'static',
				'label' => qa_lang_html('profile/received'),
				'value' => $votegotvalue,
				'id' => 'votegot',
			);
		}

		if (@$userpoints['points']) {
			$qa_content['form_activity']['fields']['points']['value'] .=
				qa_lang_html_sub('profile/ranked_x', '<span class="qa-uf-user-rank">' . qa_format_number($userrank) . '</span>');
		}

		if (@$userpoints['aselects']) {
			$qa_content['form_activity']['fields']['questions']['value'] .= ($userpoints['aselects'] == 1)
				? qa_lang_html_sub('profile/1_with_best_chosen', '<span class="qa-uf-user-q-selects">1</span>', '1')
				: qa_lang_html_sub('profile/x_with_best_chosen', '<span class="qa-uf-user-q-selects">' . qa_format_number($userpoints['aselects']) . '</span>');
		}

		if (@$userpoints['aselecteds']) {
			$qa_content['form_activity']['fields']['answers']['value'] .= ($userpoints['aselecteds'] == 1)
				? qa_lang_html_sub('profile/1_chosen_as_best', '<span class="qa-uf-user-a-selecteds">1</span>', '1')
				: qa_lang_html_sub('profile/x_chosen_as_best', '<span class="qa-uf-user-a-selecteds">' . qa_format_number($userpoints['aselecteds']) . '</span>');
		}


		// For plugin layers to access

		$qa_content['raw']['userid'] = $userid;
		$qa_content['raw']['points'] = $userpoints;
		$qa_content['raw']['rank'] = $userrank;


		// Wall posts

		if (!QA_FINAL_EXTERNAL_USERS && qa_opt('allow_user_walls')) {
			$qa_content['message_list'] = array(
				'title' => '<span id="wall">' . qa_lang_html_sub('profile/wall_for_x', $userhtml) . '</span>',

				'tags' => 'id="wallmessages"',

				'form' => array(
					'tags' => 'name="wallpost" method="post" action="' . qa_self_html() . '#wall"',
					'style' => 'tall',
					'hidden' => array(
						'qa_click' => '', // for simulating clicks in Javascript
						'handle' => qa_html($useraccount['handle']),
						'start' => 0,
						'code' => qa_get_form_security_code('wall-' . $useraccount['handle']),
					),
				),

				'messages' => array(),
			);

			if ($wallposterrorhtml) {
				$qa_content['message_list']['error'] = $wallposterrorhtml; // an error that means we are not allowed to post
			} else {
				$qa_content['message_list']['form']['fields'] = array(
					'message' => array(
						'tags' => 'name="message" id="message"',
						'value' => qa_html(@$inmessage, false),
						'rows' => 2,
						'error' => qa_html(@$errors['message']),
					),
				);

				$qa_content['message_list']['form']['buttons'] = array(
					'post' => array(
						'tags' => 'name="dowallpost" onclick="return qa_submit_wall_post(this, true);"',
						'label' => qa_lang_html('profile/post_wall_button'),
					),
				);
			}

1013
			foreach ($usermessages as $message) {
1014
				$qa_content['message_list']['messages'][] = qa_wall_post_view($message);
1015
			}
1016

1017
			if ($useraccount['wallposts'] > count($usermessages)) {
1018
				$qa_content['message_list']['messages'][] = qa_wall_view_more_link($handle, count($usermessages));
1019
			}
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
		}


		// Sub menu for navigation in user pages

		$ismyuser = isset($loginuserid) && $loginuserid == (QA_FINAL_EXTERNAL_USERS ? $userid : $useraccount['userid']);
		$qa_content['navigation']['sub'] = qa_user_sub_navigation($handle, 'profile', $ismyuser);


		return $qa_content;
	}
}