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

	File: qa-include/qa-page-admin-recalc.php
	Description: Handles admin-triggered recalculations if JavaScript disabled


	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
*/

Scott committed
23 24 25 26
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
	header('Location: ../');
	exit;
}
Scott committed
27

Scott committed
28 29
require_once QA_INCLUDE_DIR . 'app/admin.php';
require_once QA_INCLUDE_DIR . 'app/recalc.php';
Scott committed
30 31 32 33


//	Check we have administrative privileges

Scott committed
34 35
if (!qa_admin_check_privileges($qa_content))
	return $qa_content;
Scott committed
36 37 38 39


//	Find out the operation

Scott committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
$allowstates = array(
	'dorecountposts',
	'doreindexcontent',
	'dorecalcpoints',
	'dorefillevents',
	'dorecalccategories',
	'dodeletehidden',
	'doblobstodisk',
	'doblobstodb',
);

$recalcnow = false;

foreach ($allowstates as $allowstate) {
	if (qa_post_text($allowstate) || qa_get($allowstate)) {
		$state = $allowstate;
		$code = qa_post_text('code');

		if (isset($code) && qa_check_form_security_code('admin/recalc', $code))
			$recalcnow = true;
	}
}
Scott committed
62

Scott committed
63 64
if ($recalcnow) {
	?>
Scott committed
65

Scott committed
66 67 68 69 70
	<html>
		<head>
			<meta http-equiv="content-type" content="text/html; charset=utf-8">
		</head>
		<body>
Scott committed
71
			<code>
Scott committed
72

Scott committed
73
	<?php
Scott committed
74

Scott committed
75 76
	while ($state) {
		set_time_limit(60);
Scott committed
77

Scott committed
78
		$stoptime = time() + 2; // run in lumps of two seconds...
Scott committed
79

Scott committed
80 81
		while (qa_recalc_perform_step($state) && time() < $stoptime)
			;
Scott committed
82

Scott committed
83
		echo qa_html(qa_recalc_get_message($state)) . str_repeat('    ', 1024) . "<br>\n";
Scott committed
84

Scott committed
85 86 87
		flush();
		sleep(1); // ... then rest for one
	}
Scott committed
88

Scott committed
89
	?>
Scott committed
90
			</code>
Scott committed
91

Scott committed
92 93 94
			<a href="<?php echo qa_path_html('admin/stats')?>"><?php echo qa_lang_html('admin/admin_title').' - '.qa_lang_html('admin/stats_title')?></a>
		</body>
	</html>
Scott committed
95

Scott committed
96 97
	<?php
	qa_exit();
Scott committed
98

Scott committed
99 100
} elseif (isset($state)) {
	$qa_content = qa_content_prepare();
Scott committed
101

Scott committed
102 103
	$qa_content['title'] = qa_lang_html('admin/admin_title');
	$qa_content['error'] = qa_lang_html('misc/form_security_again');
Scott committed
104

Scott committed
105 106
	$qa_content['form'] = array(
		'tags' => 'method="post" action="' . qa_self_html() . '"',
Scott committed
107

Scott committed
108
		'style' => 'wide',
Scott committed
109

Scott committed
110 111 112 113
		'buttons' => array(
			'recalc' => array(
				'tags' => 'name="' . qa_html($state) . '"',
				'label' => qa_lang_html('misc/form_security_again'),
Scott committed
114
			),
Scott committed
115
		),
Scott committed
116

Scott committed
117 118 119 120
		'hidden' => array(
			'code' => qa_get_form_security_code('admin/recalc'),
		),
	);
Scott committed
121

Scott committed
122
	return $qa_content;
Scott committed
123

Scott committed
124 125
} else {
	require_once QA_INCLUDE_DIR . 'app/format.php';
Scott committed
126

Scott committed
127
	$qa_content = qa_content_prepare();
Scott committed
128

Scott committed
129 130
	$qa_content['title'] = qa_lang_html('admin/admin_title');
	$qa_content['error'] = qa_lang_html('main/page_not_found');
Scott committed
131

Scott committed
132 133
	return $qa_content;
}