admin-recalc.php 2.96 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
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	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
22
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
23
	header('Location: ../../../');
Scott committed
24 25
	exit;
}
Scott committed
26

Scott committed
27 28
require_once QA_INCLUDE_DIR . 'app/admin.php';
require_once QA_INCLUDE_DIR . 'app/recalc.php';
Scott committed
29 30


Scott committed
31
// Check we have administrative privileges
Scott committed
32

Scott committed
33 34
if (!qa_admin_check_privileges($qa_content))
	return $qa_content;
Scott committed
35 36


Scott committed
37
// Find out the operation
Scott committed
38

Scott committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
$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
61

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

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

Scott committed
72
	<?php
Scott committed
73

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

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

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

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

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

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

Scott committed
91 92 93
			<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
94

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

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

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

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

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

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

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

Scott committed
121
	return $qa_content;
Scott committed
122

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

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

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

Scott committed
131 132
	return $qa_content;
}