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
require_once QA_INCLUDE_DIR . 'app/admin.php';
Scott committed
28

Scott committed
29
// Check we have administrative privileges
Scott committed
30

Scott committed
31 32
if (!qa_admin_check_privileges($qa_content))
	return $qa_content;
Scott committed
33 34


Scott committed
35
// Find out the operation
Scott committed
36

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

Scott committed
60 61
if ($recalcnow) {
	?>
Scott committed
62

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

Scott committed
70
	<?php
Scott committed
71

72
	$recalc = new \Q2A\Recalc\RecalcMain($state);
73
	while ($recalc->getState()) {
Scott committed
74
		set_time_limit(60);
Scott committed
75

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

78
		while ($recalc->performStep() && time() < $stoptime)
Scott committed
79
			;
Scott committed
80

81
		echo qa_html($recalc->getMessage()) . str_repeat('    ', 1024) . "<br>\n";
Scott committed
82

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

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

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

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

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

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

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

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

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

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

Scott committed
120
	return $qa_content;
Scott committed
121

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

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

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

Scott committed
130 131
	return $qa_content;
}