qa-app-admin.php 18.7 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-app-admin.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: Functions used in the admin center pages


	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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31
	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
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}

Scott Vivian committed
32

Gideon Greenspan committed
33 34 35 36 37 38 39 40
	function qa_admin_check_privileges(&$qa_content)
/*
	Return true if user is logged in with admin privileges. If not, return false
	and set up $qa_content with the appropriate title and error message
*/
	{
		if (!qa_is_logged_in()) {
			require_once QA_INCLUDE_DIR.'qa-app-format.php';
Scott Vivian committed
41

Gideon Greenspan committed
42 43 44 45
			$qa_content=qa_content_prepare();

			$qa_content['title']=qa_lang_html('admin/admin_title');
			$qa_content['error']=qa_insert_login_links(qa_lang_html('admin/not_logged_in'), qa_request());
Scott Vivian committed
46

Gideon Greenspan committed
47 48 49 50
			return false;

		} elseif (qa_get_logged_in_level()<QA_USER_LEVEL_ADMIN) {
			$qa_content=qa_content_prepare();
Scott Vivian committed
51

Gideon Greenspan committed
52 53
			$qa_content['title']=qa_lang_html('admin/admin_title');
			$qa_content['error']=qa_lang_html('admin/no_privileges');
Scott Vivian committed
54

Gideon Greenspan committed
55 56
			return false;
		}
Scott Vivian committed
57

Gideon Greenspan committed
58 59
		return true;
	}
Scott Vivian committed
60 61


Gideon Greenspan committed
62 63 64 65 66
	function qa_admin_language_options()
/*
	Return a sorted array of available languages, [short code] => [long name]
*/
	{
Gideon Greenspan committed
67
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
68

Gideon Greenspan committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
		$codetolanguage=array( // 2-letter language codes as per ISO 639-1
			'ar' => 'Arabic - العربية',
			'bg' => 'Bulgarian - Български',
			'bn' => 'Bengali - বাংলা',
			'ca' => 'Catalan - Català',
			'cs' => 'Czech - Čeština',
			'cy' => 'Welsh - Cymraeg',
			'da' => 'Danish - Dansk',
			'de' => 'German - Deutsch',
			'el' => 'Greek - Ελληνικά',
			'en-GB' => 'English (UK)',
			'es' => 'Spanish - Español',
			'et' => 'Estonian - Eesti',
			'fa' => 'Persian - فارسی',
			'fi' => 'Finnish - Suomi',
			'fr' => 'French - Français',
			'he' => 'Hebrew - עברית',
			'hr' => 'Croatian - Hrvatski',
			'hu' => 'Hungarian - Magyar',
			'id' => 'Indonesian - Bahasa Indonesia',
			'is' => 'Icelandic - Íslenska',
			'it' => 'Italian - Italiano',
			'ja' => 'Japanese - 日本語',
Gideon Greenspan committed
92
			'ka' => 'Georgian - ქართული ენა',
Gideon Greenspan committed
93 94
			'kh' => 'Khmer - ភាសាខ្មែរ',
			'ko' => 'Korean - 한국어',
Gideon Greenspan committed
95
			'ku-CKB' => 'Kurdish Central - کورد',
Gideon Greenspan committed
96
			'lt' => 'Lithuanian - Lietuvių',
Gideon Greenspan committed
97
			'lv' => 'Latvian - Latviešu',
Gideon Greenspan committed
98 99 100 101 102 103 104 105
			'nl' => 'Dutch - Nederlands',
			'no' => 'Norwegian - Norsk',
			'pl' => 'Polish - Polski',
			'pt' => 'Portuguese - Português',
			'ro' => 'Romanian - Română',
			'ru' => 'Russian - Русский',
			'sk' => 'Slovak - Slovenčina',
			'sl' => 'Slovenian - Slovenščina',
Gideon Greenspan committed
106
			'sq' => 'Albanian - Shqip',
Gideon Greenspan committed
107 108 109 110
			'sr' => 'Serbian - Српски',
			'sv' => 'Swedish - Svenska',
			'th' => 'Thai - ไทย',
			'tr' => 'Turkish - Türkçe',
Gideon Greenspan committed
111
			'ug' => 'Uyghur - ئۇيغۇرچە',
Gideon Greenspan committed
112 113 114 115 116
			'uk' => 'Ukrainian - Українська',
			'vi' => 'Vietnamese - Tiếng Việt',
			'zh-TW' => 'Chinese Traditional - 繁體中文',
			'zh' => 'Chinese Simplified - 简体中文',
		);
Scott Vivian committed
117

Gideon Greenspan committed
118
		$options=array('' => 'English (US)');
Scott Vivian committed
119

Gideon Greenspan committed
120 121 122 123 124 125 126 127
		$directory=@opendir(QA_LANG_DIR);
		if (is_resource($directory)) {
			while (($code=readdir($directory))!==false)
				if (is_dir(QA_LANG_DIR.$code) && isset($codetolanguage[$code]))
					$options[$code]=$codetolanguage[$code];

			closedir($directory);
		}
Scott Vivian committed
128

Gideon Greenspan committed
129
		asort($options, SORT_STRING);
Scott Vivian committed
130

Gideon Greenspan committed
131 132
		return $options;
	}
Scott Vivian committed
133 134


Gideon Greenspan committed
135 136 137 138 139
	function qa_admin_theme_options()
/*
	Return a sorted array of available themes, [theme name] => [theme name]
*/
	{
Gideon Greenspan committed
140
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
141

Gideon Greenspan committed
142 143 144 145 146 147 148 149 150 151
		$options=array();

		$directory=@opendir(QA_THEME_DIR);
		if (is_resource($directory)) {
			while (($theme=readdir($directory))!==false)
				if ( (substr($theme, 0, 1)!='.') && file_exists(QA_THEME_DIR.$theme.'/qa-styles.css') )
					$options[$theme]=$theme;

			closedir($directory);
		}
Scott Vivian committed
152

Gideon Greenspan committed
153
		asort($options, SORT_STRING);
Scott Vivian committed
154

Gideon Greenspan committed
155 156
		return $options;
	}
Scott Vivian committed
157 158


Gideon Greenspan committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	function qa_admin_place_options()
/*
	Return an array of widget placement options, with keys matching the database value
*/
	{
		return array(
			'FT' => qa_lang_html('options/place_full_top'),
			'FH' => qa_lang_html('options/place_full_below_nav'),
			'FL' => qa_lang_html('options/place_full_below_content'),
			'FB' => qa_lang_html('options/place_full_below_footer'),
			'MT' => qa_lang_html('options/place_main_top'),
			'MH' => qa_lang_html('options/place_main_below_title'),
			'ML' => qa_lang_html('options/place_main_below_lists'),
			'MB' => qa_lang_html('options/place_main_bottom'),
			'ST' => qa_lang_html('options/place_side_top'),
			'SH' => qa_lang_html('options/place_side_below_sidebar'),
			'SL' => qa_lang_html('options/place_side_below_categories'),
			'SB' => qa_lang_html('options/place_side_last'),
		);
	}

Scott Vivian committed
180

Gideon Greenspan committed
181 182 183 184 185 186
	function qa_admin_page_size_options($maximum)
/*
	Return an array of page size options up to $maximum, [page size] => [page size]
*/
	{
		$rawoptions=array(5, 10, 15, 20, 25, 30, 40, 50, 60, 80, 100, 120, 150, 200, 250, 300, 400, 500, 600, 800, 1000);
Scott Vivian committed
187

Gideon Greenspan committed
188 189 190 191
		$options=array();
		foreach ($rawoptions as $rawoption) {
			if ($rawoption>$maximum)
				break;
Scott Vivian committed
192

Gideon Greenspan committed
193 194
			$options[$rawoption]=$rawoption;
		}
Scott Vivian committed
195

Gideon Greenspan committed
196 197
		return $options;
	}
Scott Vivian committed
198 199


Gideon Greenspan committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213
	function qa_admin_match_options()
/*
	Return an array of options representing matching precision, [value] => [label]
*/
	{
		return array(
			5 => qa_lang_html('options/match_5'),
			4 => qa_lang_html('options/match_4'),
			3 => qa_lang_html('options/match_3'),
			2 => qa_lang_html('options/match_2'),
			1 => qa_lang_html('options/match_1'),
		);
	}

Scott Vivian committed
214

Gideon Greenspan committed
215 216 217 218 219 220 221
	function qa_admin_permit_options($widest, $narrowest, $doconfirms=true, $dopoints=true)
/*
	Return an array of options representing permission restrictions, [value] => [label]
	ranging from $widest to $narrowest. Set $doconfirms to whether email confirmations are on
*/
	{
		require_once QA_INCLUDE_DIR.'qa-app-options.php';
Scott Vivian committed
222

Gideon Greenspan committed
223 224 225 226 227 228
		$options=array(
			QA_PERMIT_ALL => qa_lang_html('options/permit_all'),
			QA_PERMIT_USERS => qa_lang_html('options/permit_users'),
			QA_PERMIT_CONFIRMED => qa_lang_html('options/permit_confirmed'),
			QA_PERMIT_POINTS => qa_lang_html('options/permit_points'),
			QA_PERMIT_POINTS_CONFIRMED => qa_lang_html('options/permit_points_confirmed'),
Gideon Greenspan committed
229 230
			QA_PERMIT_APPROVED => qa_lang_html('options/permit_approved'),
			QA_PERMIT_APPROVED_POINTS => qa_lang_html('options/permit_approved_points'),
Gideon Greenspan committed
231 232 233 234 235 236
			QA_PERMIT_EXPERTS => qa_lang_html('options/permit_experts'),
			QA_PERMIT_EDITORS => qa_lang_html('options/permit_editors'),
			QA_PERMIT_MODERATORS => qa_lang_html('options/permit_moderators'),
			QA_PERMIT_ADMINS => qa_lang_html('options/permit_admins'),
			QA_PERMIT_SUPERS => qa_lang_html('options/permit_supers'),
		);
Scott Vivian committed
237

Gideon Greenspan committed
238 239 240
		foreach ($options as $key => $label)
			if (($key<$narrowest) || ($key>$widest))
				unset($options[$key]);
Scott Vivian committed
241

Gideon Greenspan committed
242 243 244 245
		if (!$doconfirms) {
			unset($options[QA_PERMIT_CONFIRMED]);
			unset($options[QA_PERMIT_POINTS_CONFIRMED]);
		}
Scott Vivian committed
246

Gideon Greenspan committed
247 248 249
		if (!$dopoints) {
			unset($options[QA_PERMIT_POINTS]);
			unset($options[QA_PERMIT_POINTS_CONFIRMED]);
Gideon Greenspan committed
250 251
			unset($options[QA_PERMIT_APPROVED_POINTS]);
		}
Scott Vivian committed
252

Gideon Greenspan committed
253 254 255
		if (QA_FINAL_EXTERNAL_USERS || !qa_opt('moderate_users')) {
			unset($options[QA_PERMIT_APPROVED]);
			unset($options[QA_PERMIT_APPROVED_POINTS]);
Gideon Greenspan committed
256
		}
Scott Vivian committed
257

Gideon Greenspan committed
258 259 260
		return $options;
	}

Scott Vivian committed
261

Gideon Greenspan committed
262 263 264 265 266
	function qa_admin_sub_navigation()
/*
	Return the sub navigation structure common to admin pages
*/
	{
Gideon Greenspan committed
267
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
268

Gideon Greenspan committed
269
		$navigation=array();
Gideon Greenspan committed
270
		$level=qa_get_logged_in_level();
Scott Vivian committed
271

Gideon Greenspan committed
272
		if ($level>=QA_USER_LEVEL_ADMIN) {
Gideon Greenspan committed
273
			$navigation['admin/general']=array(
Gideon Greenspan committed
274
				'label' => qa_lang_html('admin/general_title'),
Gideon Greenspan committed
275 276
				'url' => qa_path_html('admin/general'),
			);
Scott Vivian committed
277

Gideon Greenspan committed
278
			$navigation['admin/emails']=array(
Gideon Greenspan committed
279
				'label' => qa_lang_html('admin/emails_title'),
Gideon Greenspan committed
280 281
				'url' => qa_path_html('admin/emails'),
			);
Scott Vivian committed
282

Gideon Greenspan committed
283
			$navigation['admin/user']=array(
Gideon Greenspan committed
284
				'label' => qa_lang_html('admin/users_title'),
Gideon Greenspan committed
285 286
				'url' => qa_path_html('admin/users'),
			);
Scott Vivian committed
287

Gideon Greenspan committed
288
			$navigation['admin/layout']=array(
Gideon Greenspan committed
289
				'label' => qa_lang_html('admin/layout_title'),
Gideon Greenspan committed
290 291
				'url' => qa_path_html('admin/layout'),
			);
Scott Vivian committed
292

Gideon Greenspan committed
293
			$navigation['admin/posting']=array(
Gideon Greenspan committed
294
				'label' => qa_lang_html('admin/posting_title'),
Gideon Greenspan committed
295 296
				'url' => qa_path_html('admin/posting'),
			);
Scott Vivian committed
297

Gideon Greenspan committed
298
			$navigation['admin/viewing']=array(
Gideon Greenspan committed
299
				'label' => qa_lang_html('admin/viewing_title'),
Gideon Greenspan committed
300 301
				'url' => qa_path_html('admin/viewing'),
			);
Scott Vivian committed
302

Gideon Greenspan committed
303
			$navigation['admin/lists']=array(
Gideon Greenspan committed
304
				'label' => qa_lang_html('admin/lists_title'),
Gideon Greenspan committed
305 306
				'url' => qa_path_html('admin/lists'),
			);
Scott Vivian committed
307

Gideon Greenspan committed
308 309
			if (qa_using_categories())
				$navigation['admin/categories']=array(
Gideon Greenspan committed
310
					'label' => qa_lang_html('admin/categories_title'),
Gideon Greenspan committed
311 312
					'url' => qa_path_html('admin/categories'),
				);
Scott Vivian committed
313

Gideon Greenspan committed
314
			$navigation['admin/permissions']=array(
Gideon Greenspan committed
315
				'label' => qa_lang_html('admin/permissions_title'),
Gideon Greenspan committed
316 317
				'url' => qa_path_html('admin/permissions'),
			);
Scott Vivian committed
318

Gideon Greenspan committed
319
			$navigation['admin/pages']=array(
Gideon Greenspan committed
320
				'label' => qa_lang_html('admin/pages_title'),
Gideon Greenspan committed
321 322
				'url' => qa_path_html('admin/pages'),
			);
Scott Vivian committed
323

Gideon Greenspan committed
324
			$navigation['admin/feeds']=array(
Gideon Greenspan committed
325
				'label' => qa_lang_html('admin/feeds_title'),
Gideon Greenspan committed
326 327
				'url' => qa_path_html('admin/feeds'),
			);
Scott Vivian committed
328

Gideon Greenspan committed
329
			$navigation['admin/points']=array(
Gideon Greenspan committed
330
				'label' => qa_lang_html('admin/points_title'),
Gideon Greenspan committed
331 332
				'url' => qa_path_html('admin/points'),
			);
Scott Vivian committed
333

Gideon Greenspan committed
334
			$navigation['admin/spam']=array(
Gideon Greenspan committed
335
				'label' => qa_lang_html('admin/spam_title'),
Gideon Greenspan committed
336 337 338 339
				'url' => qa_path_html('admin/spam'),
			);

			$navigation['admin/stats']=array(
Gideon Greenspan committed
340
				'label' => qa_lang_html('admin/stats_title'),
Gideon Greenspan committed
341 342 343 344 345
				'url' => qa_path_html('admin/stats'),
			);

			if (!QA_FINAL_EXTERNAL_USERS)
				$navigation['admin/mailing']=array(
Gideon Greenspan committed
346
					'label' => qa_lang_html('admin/mailing_title'),
Gideon Greenspan committed
347 348
					'url' => qa_path_html('admin/mailing'),
				);
Scott Vivian committed
349

Gideon Greenspan committed
350
			$navigation['admin/plugins']=array(
Gideon Greenspan committed
351
				'label' => qa_lang_html('admin/plugins_title'),
Gideon Greenspan committed
352 353 354
				'url' => qa_path_html('admin/plugins'),
			);
		}
Scott Vivian committed
355

Gideon Greenspan committed
356 357
		if (!qa_user_maximum_permit_error('permit_moderate')) {
			$count=qa_user_permit_error('permit_moderate') ? null : qa_opt('cache_queuedcount'); // if only in some categories don't show cached count
Scott Vivian committed
358

Gideon Greenspan committed
359
			$navigation['admin/moderate']=array(
Gideon Greenspan committed
360
				'label' => qa_lang_html('admin/moderate_title').($count ? (' ('.$count.')') : ''),
Gideon Greenspan committed
361 362
				'url' => qa_path_html('admin/moderate'),
			);
Gideon Greenspan committed
363
		}
Scott Vivian committed
364

Gideon Greenspan committed
365 366
		if (qa_opt('flagging_of_posts') && !qa_user_maximum_permit_error('permit_hide_show')) {
			$count=qa_user_permit_error('permit_hide_show') ? null : qa_opt('cache_flaggedcount'); // if only in some categories don't show cached count
Scott Vivian committed
367

Gideon Greenspan committed
368
			$navigation['admin/flagged']=array(
Gideon Greenspan committed
369
				'label' => qa_lang_html('admin/flagged_title').($count ? (' ('.$count.')') : ''),
Gideon Greenspan committed
370 371
				'url' => qa_path_html('admin/flagged'),
			);
Gideon Greenspan committed
372
		}
Scott Vivian committed
373

Gideon Greenspan committed
374
		if ( (!qa_user_maximum_permit_error('permit_hide_show')) || (!qa_user_maximum_permit_error('permit_delete_hidden')) )
Gideon Greenspan committed
375
			$navigation['admin/hidden']=array(
Gideon Greenspan committed
376
				'label' => qa_lang_html('admin/hidden_title'),
Gideon Greenspan committed
377 378
				'url' => qa_path_html('admin/hidden'),
			);
Scott Vivian committed
379

Gideon Greenspan committed
380 381
		if ( (!QA_FINAL_EXTERNAL_USERS) && qa_opt('moderate_users') && ($level>=QA_USER_LEVEL_MODERATOR)) {
			$count=qa_opt('cache_uapprovecount');
Scott Vivian committed
382

Gideon Greenspan committed
383 384 385 386 387 388
			$navigation['admin/moderate-users']=array(
				'label' => qa_lang_html('admin/approve_users_title').($count ? (' ('.$count.')') : ''),
				'url' => qa_path_html('admin/approve'),
			);
		}

Gideon Greenspan committed
389 390
		return $navigation;
	}
Scott Vivian committed
391 392


Gideon Greenspan committed
393 394 395 396 397
	function qa_admin_page_error()
/*
	Return the error that needs to displayed on all admin pages, or null if none
*/
	{
Gideon Greenspan committed
398 399
		if (file_exists(QA_INCLUDE_DIR.'qa-db-install.php')) // file can be removed for extra security
			include_once QA_INCLUDE_DIR.'qa-db-install.php';
Scott Vivian committed
400

Gideon Greenspan committed
401 402 403
		if (defined('QA_DB_VERSION_CURRENT') && (qa_opt('db_version')<QA_DB_VERSION_CURRENT) && (qa_get_logged_in_level()>=QA_USER_LEVEL_ADMIN))
			return strtr(
				qa_lang_html('admin/upgrade_db'),
Scott Vivian committed
404

Gideon Greenspan committed
405
				array(
Gideon Greenspan committed
406 407
					'^1' => '<a href="'.qa_path_html('install').'">',
					'^2' => '</a>',
Gideon Greenspan committed
408 409
				)
			);
Gideon Greenspan committed
410 411 412

		elseif (defined('QA_BLOBS_DIRECTORY') && !is_writable(QA_BLOBS_DIRECTORY))
			return qa_lang_html_sub('admin/blobs_directory_error', qa_html(QA_BLOBS_DIRECTORY));
Scott Vivian committed
413

Gideon Greenspan committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
		else
			return null;
	}


	function qa_admin_url_test_html()
/*
	Return an HTML fragment to display for a URL test which has passed
*/
	{
		return '; font-size:9px; color:#060; font-weight:bold; font-family:arial,sans-serif; border-color:#060;">OK<';
	}


	function qa_admin_is_slug_reserved($requestpart)
/*
	Returns whether a URL path beginning with $requestpart is reserved by the engine or a plugin page module
*/
	{
		$requestpart=trim(strtolower($requestpart));
		$routing=qa_page_routing();
Scott Vivian committed
435

Gideon Greenspan committed
436 437
		if (isset($routing[$requestpart]) || isset($routing[$requestpart.'/']) || is_numeric($requestpart))
			return true;
Scott Vivian committed
438

Gideon Greenspan committed
439 440 441 442 443
		$pathmap=qa_get_request_map();

		foreach ($pathmap as $mappedrequest)
			if (trim(strtolower($mappedrequest)) == $requestpart)
				return true;
Scott Vivian committed
444

Gideon Greenspan committed
445 446 447 448 449 450 451 452 453 454
		switch ($requestpart) {
			case '':
			case 'qa':
			case 'feed':
			case 'install':
			case 'url':
			case 'image':
			case 'ajax':
				return true;
		}
Scott Vivian committed
455

Gideon Greenspan committed
456 457 458 459
		$pagemodules=qa_load_modules_with('page', 'match_request');
		foreach ($pagemodules as $pagemodule)
			if ($pagemodule->match_request($requestpart))
				return true;
Scott Vivian committed
460

Gideon Greenspan committed
461 462
		return false;
	}
Scott Vivian committed
463 464


Gideon Greenspan committed
465
	function qa_admin_single_click($entityid, $action)
Gideon Greenspan committed
466
/*
Gideon Greenspan committed
467 468
	Returns true if admin (hidden/flagged/approve/moderate) page $action performed on $entityid is permitted by the
	logged in user and was processed successfully
Gideon Greenspan committed
469
*/
Scott Vivian committed
470
	{
Gideon Greenspan committed
471 472 473 474 475 476
		$userid=qa_get_logged_in_userid();

		if ( (!QA_FINAL_EXTERNAL_USERS) && (($action=='userapprove') || ($action=='userblock')) ) { // approve/block moderated users
			require_once QA_INCLUDE_DIR.'qa-db-selects.php';

			$useraccount=qa_db_select_with_pending(qa_db_user_account_selectspec($entityid, true));
Scott Vivian committed
477

Gideon Greenspan committed
478 479 480 481 482 483 484 485 486
			if ( isset($useraccount) && (qa_get_logged_in_level()>=QA_USER_LEVEL_MODERATOR) )
				switch ($action) {
					case 'userapprove':
						if ($useraccount['level']<=QA_USER_LEVEL_APPROVED) { // don't demote higher level users
							require_once QA_INCLUDE_DIR.'qa-app-users-edit.php';
							qa_set_user_level($useraccount['userid'], $useraccount['handle'], QA_USER_LEVEL_APPROVED, $useraccount['level']);
							return true;
						}
						break;
Scott Vivian committed
487

Gideon Greenspan committed
488 489 490 491 492 493
					case 'userblock':
						require_once QA_INCLUDE_DIR.'qa-app-users-edit.php';
						qa_set_user_blocked($useraccount['userid'], $useraccount['handle'], true);
						return true;
						break;
				}
Scott Vivian committed
494

Gideon Greenspan committed
495 496
		} else { // something to do with a post
			require_once QA_INCLUDE_DIR.'qa-app-posts.php';
Scott Vivian committed
497

Gideon Greenspan committed
498
			$post=qa_post_get_full($entityid);
Scott Vivian committed
499

Gideon Greenspan committed
500 501
			if (isset($post)) {
				$queued=(substr($post['type'], 1)=='_QUEUED');
Scott Vivian committed
502

Gideon Greenspan committed
503 504 505 506 507 508 509
				switch ($action) {
					case 'approve':
						if ($queued && !qa_user_post_permit_error('permit_moderate', $post)) {
							qa_post_set_hidden($entityid, false, $userid);
							return true;
						}
						break;
Scott Vivian committed
510

Gideon Greenspan committed
511 512 513 514 515 516
					case 'reject':
						if ($queued && !qa_user_post_permit_error('permit_moderate', $post)) {
							qa_post_set_hidden($entityid, true, $userid);
							return true;
						}
						break;
Scott Vivian committed
517

Gideon Greenspan committed
518 519 520 521
					case 'hide':
						if ((!$queued) && !qa_user_post_permit_error('permit_hide_show', $post)) {
							qa_post_set_hidden($entityid, true, $userid);
							return true;
Scott Vivian committed
522
						}
Gideon Greenspan committed
523
						break;
Scott Vivian committed
524

Gideon Greenspan committed
525 526 527 528 529 530
					case 'reshow':
						if ($post['hidden'] && !qa_user_post_permit_error('permit_hide_show', $post)) {
							qa_post_set_hidden($entityid, false, $userid);
							return true;
						}
						break;
Scott Vivian committed
531

Gideon Greenspan committed
532 533 534 535 536 537
					case 'delete':
						if ($post['hidden'] && !qa_user_post_permit_error('permit_delete_hidden', $post)) {
							qa_post_delete($entityid);
							return true;
						}
						break;
Scott Vivian committed
538

Gideon Greenspan committed
539 540
					case 'clearflags':
						require_once QA_INCLUDE_DIR.'qa-app-votes.php';
Scott Vivian committed
541

Gideon Greenspan committed
542 543 544 545 546 547
						if (!qa_user_post_permit_error('permit_hide_show', $post)) {
							qa_flags_clear_all($post, $userid, qa_get_logged_in_handle(), null);
							return true;
						}
						break;
				}
Gideon Greenspan committed
548 549
			}
		}
Scott Vivian committed
550

Gideon Greenspan committed
551 552
		return false;
	}
Scott Vivian committed
553 554


Gideon Greenspan committed
555 556
	function qa_admin_check_clicks()
/*
Gideon Greenspan committed
557
	Checks for a POSTed click on an admin (hidden/flagged/approve/moderate) page, and refresh the page if processed successfully (non Ajax)
Gideon Greenspan committed
558 559 560 561 562
*/
	{
		if (qa_is_http_post())
			foreach ($_POST as $field => $value)
				if (strpos($field, 'admin_')===0) {
Gideon Greenspan committed
563
					@list($dummy, $entityid, $action)=explode('_', $field);
Scott Vivian committed
564

Gideon Greenspan committed
565 566 567 568 569 570
					if (strlen($entityid) && strlen($action)) {
						if (!qa_check_form_security_code('admin/click', qa_post_text('code')))
							return qa_lang_html('misc/form_security_again');
						elseif (qa_admin_single_click($entityid, $action))
							qa_redirect(qa_request());
					}
Gideon Greenspan committed
571
				}
Scott Vivian committed
572

Gideon Greenspan committed
573
		return null;
Gideon Greenspan committed
574
	}
Scott Vivian committed
575 576


577 578 579 580 581
	/**
	 * Retrieve metadata information from the $contents of a qa-theme.php or qa-plugin.php file, mapping via $fields.
	 *
	 * @deprecated Deprecated from 1.7; use `qa_addon_metadata($contents, $type)` instead.
	 */
Gideon Greenspan committed
582 583 584 585 586 587 588
	function qa_admin_addon_metadata($contents, $fields)
	{
		$metadata=array();

		foreach ($fields as $key => $field)
			if (preg_match('/'.str_replace(' ', '[ \t]*', preg_quote($field, '/')).':[ \t]*([^\n\f]*)[\n\f]/i', $contents, $matches))
				$metadata[$key]=trim($matches[1]);
Scott Vivian committed
589

Gideon Greenspan committed
590 591
		return $metadata;
	}
Scott Vivian committed
592 593


Gideon Greenspan committed
594
	function qa_admin_plugin_directory_hash($directory)
Gideon Greenspan committed
595 596 597
/*
	Return the hash code for the plugin in $directory, used for in-page navigation on admin/plugins page
*/
Gideon Greenspan committed
598 599 600
	{
		return md5($directory);
	}
Scott Vivian committed
601 602


Gideon Greenspan committed
603
	function qa_admin_plugin_options_path($directory)
Gideon Greenspan committed
604 605 606
/*
	Return the URL (relative to the current page) to navigate to the options panel for the plugin in $directory
*/
Gideon Greenspan committed
607
	{
Scott Vivian committed
608
		$hash=qa_admin_plugin_directory_hash($directory);
Gideon Greenspan committed
609 610
		return qa_path_html('admin/plugins', array('show' => $hash), null, null, $hash);
	}
Scott Vivian committed
611

Gideon Greenspan committed
612 613

	function qa_admin_module_options_path($type, $name)
Gideon Greenspan committed
614 615 616
/*
	Return the URL (relative to the current page) to navigate to the options panel for plugin module $name of $type
*/
Gideon Greenspan committed
617 618 619 620
	{
		$info=qa_get_module_info($type, $name);
		return qa_admin_plugin_options_path($info['directory']);
	}
Gideon Greenspan committed
621 622 623 624 625


/*
	Omit PHP closing tag to help avoid accidental output
*/