ip.php 7.21 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: Controller for page showing recent activity for an IP address


	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 . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/format.php';
Scott committed
29 30


Scott committed
31 32 33
$ip = qa_request_part(1); // picked up from qa-page.php
if (filter_var($ip, FILTER_VALIDATE_IP) === false)
	return include QA_INCLUDE_DIR . 'qa-page-not-found.php';
Scott committed
34 35


Scott committed
36
// Find recently (hidden, queued or not) questions, answers, comments and edits for this IP
Scott committed
37

Scott committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
$userid = qa_get_logged_in_userid();

list($qs, $qs_queued, $qs_hidden, $a_qs, $a_queued_qs, $a_hidden_qs, $c_qs, $c_queued_qs, $c_hidden_qs, $edit_qs) =
	qa_db_select_with_pending(
		qa_db_qs_selectspec($userid, 'created', 0, null, $ip, false),
		qa_db_qs_selectspec($userid, 'created', 0, null, $ip, 'Q_QUEUED'),
		qa_db_qs_selectspec($userid, 'created', 0, null, $ip, 'Q_HIDDEN', true),
		qa_db_recent_a_qs_selectspec($userid, 0, null, $ip, false),
		qa_db_recent_a_qs_selectspec($userid, 0, null, $ip, 'A_QUEUED'),
		qa_db_recent_a_qs_selectspec($userid, 0, null, $ip, 'A_HIDDEN', true),
		qa_db_recent_c_qs_selectspec($userid, 0, null, $ip, false),
		qa_db_recent_c_qs_selectspec($userid, 0, null, $ip, 'C_QUEUED'),
		qa_db_recent_c_qs_selectspec($userid, 0, null, $ip, 'C_HIDDEN', true),
		qa_db_recent_edit_qs_selectspec($userid, 0, null, $ip, false)
	);
Scott committed
53 54


Scott committed
55
// Check we have permission to view this page, and whether we can block or unblock IPs
Scott committed
56

Scott committed
57 58 59 60 61
if (qa_user_maximum_permit_error('permit_anon_view_ips')) {
	$qa_content = qa_content_prepare();
	$qa_content['error'] = qa_lang_html('users/no_permission');
	return $qa_content;
}
Scott committed
62

Scott committed
63
$blockable = qa_user_level_maximum() >= QA_USER_LEVEL_MODERATOR; // allow moderator in one category to block across all categories
Scott committed
64 65


Scott committed
66
// Perform blocking or unblocking operations as appropriate
Scott committed
67

Scott committed
68 69 70
if (qa_clicked('doblock') || qa_clicked('dounblock') || qa_clicked('dohideall')) {
	if (!qa_check_form_security_code('ip-' . $ip, qa_post_text('code')))
		$pageerror = qa_lang_html('misc/form_security_again');
Scott committed
71

Scott committed
72 73 74 75
	elseif ($blockable) {
		if (qa_clicked('doblock')) {
			$oldblocked = qa_opt('block_ips_write');
			qa_set_option('block_ips_write', (strlen($oldblocked) ? ($oldblocked . ' , ') : '') . $ip);
Scott committed
76

Scott committed
77 78 79
			qa_report_event('ip_block', $userid, qa_get_logged_in_handle(), qa_cookie_get(), array(
				'ip' => $ip,
			));
Scott committed
80

Scott committed
81 82
			qa_redirect(qa_request());
		}
Scott committed
83

Scott committed
84 85
		if (qa_clicked('dounblock')) {
			require_once QA_INCLUDE_DIR . 'app/limits.php';
Scott committed
86

Scott committed
87
			$blockipclauses = qa_block_ips_explode(qa_opt('block_ips_write'));
Scott committed
88

Scott committed
89 90 91 92
			foreach ($blockipclauses as $key => $blockipclause) {
				if (qa_block_ip_match($ip, $blockipclause))
					unset($blockipclauses[$key]);
			}
Scott committed
93

Scott committed
94
			qa_set_option('block_ips_write', implode(' , ', $blockipclauses));
Scott committed
95

Scott committed
96 97 98
			qa_report_event('ip_unblock', $userid, qa_get_logged_in_handle(), qa_cookie_get(), array(
				'ip' => $ip,
			));
Scott committed
99

Scott committed
100 101
			qa_redirect(qa_request());
		}
Scott committed
102

Scott committed
103 104
		if (qa_clicked('dohideall') && !qa_user_maximum_permit_error('permit_hide_show')) {
			// allow moderator in one category to hide posts across all categories if they are identified via IP page
Scott committed
105

Scott committed
106 107
			require_once QA_INCLUDE_DIR . 'db/admin.php';
			require_once QA_INCLUDE_DIR . 'app/posts.php';
Scott committed
108

Scott committed
109
			$postids = qa_db_get_ip_visible_postids($ip);
Scott committed
110

Scott committed
111
			foreach ($postids as $postid)
112
				qa_post_set_status($postid, QA_POST_STATUS_HIDDEN, $userid);
Scott committed
113

Scott committed
114
			qa_redirect(qa_request());
Scott committed
115 116
		}
	}
Scott committed
117
}
Scott committed
118 119


Scott committed
120
// Combine sets of questions and get information for users
Scott committed
121

Scott committed
122
$questions = qa_any_sort_by_date(array_merge($qs, $qs_queued, $qs_hidden, $a_qs, $a_queued_qs, $a_hidden_qs, $c_qs, $c_queued_qs, $c_hidden_qs, $edit_qs));
Scott committed
123

Scott committed
124
$usershtml = qa_userids_handles_html(qa_any_get_userids_handles($questions));
Scott committed
125

Scott committed
126
$hostname = gethostbyaddr($ip);
Scott committed
127 128


Scott committed
129
// Prepare content for theme
Scott committed
130

Scott committed
131
$qa_content = qa_content_prepare();
Scott committed
132

Scott committed
133 134
$qa_content['title'] = qa_lang_html_sub('main/ip_address_x', qa_html($ip));
$qa_content['error'] = @$pageerror;
Scott committed
135

Scott committed
136 137
$qa_content['form'] = array(
	'tags' => 'method="post" action="' . qa_self_html() . '"',
Scott committed
138

Scott committed
139
	'style' => 'wide',
Scott committed
140

Scott committed
141 142 143 144 145
	'fields' => array(
		'host' => array(
			'type' => 'static',
			'label' => qa_lang_html('misc/host_name'),
			'value' => qa_html($hostname),
Scott committed
146
		),
Scott committed
147
	),
Scott committed
148

Scott committed
149 150 151 152
	'hidden' => array(
		'code' => qa_get_form_security_code('ip-' . $ip),
	),
);
Scott committed
153 154


Scott committed
155 156
if ($blockable) {
	require_once QA_INCLUDE_DIR . 'app/limits.php';
Scott committed
157

Scott committed
158 159
	$blockipclauses = qa_block_ips_explode(qa_opt('block_ips_write'));
	$matchclauses = array();
Scott committed
160

Scott committed
161 162 163 164
	foreach ($blockipclauses as $blockipclause) {
		if (qa_block_ip_match($ip, $blockipclause))
			$matchclauses[] = $blockipclause;
	}
Scott committed
165

Scott committed
166 167 168 169 170 171
	if (count($matchclauses)) {
		$qa_content['form']['fields']['status'] = array(
			'type' => 'static',
			'label' => qa_lang_html('misc/matches_blocked_ips'),
			'value' => qa_html(implode("\n", $matchclauses), true),
		);
Scott committed
172

Scott committed
173 174 175 176
		$qa_content['form']['buttons']['unblock'] = array(
			'tags' => 'name="dounblock"',
			'label' => qa_lang_html('misc/unblock_ip_button'),
		);
Scott committed
177

Scott committed
178 179 180 181
		if (count($questions) && !qa_user_maximum_permit_error('permit_hide_show'))
			$qa_content['form']['buttons']['hideall'] = array(
				'tags' => 'name="dohideall" onclick="qa_show_waiting_after(this, false);"',
				'label' => qa_lang_html('misc/hide_all_ip_button'),
Scott committed
182
			);
Scott committed
183 184 185 186 187 188

	} else {
		$qa_content['form']['buttons']['block'] = array(
			'tags' => 'name="doblock"',
			'label' => qa_lang_html('misc/block_ip_button'),
		);
Scott committed
189
	}
Scott committed
190
}
Scott committed
191 192


Scott committed
193
$qa_content['q_list']['qs'] = array();
Scott committed
194

Scott committed
195 196
if (count($questions)) {
	$qa_content['q_list']['title'] = qa_lang_html_sub('misc/recent_activity_from_x', qa_html($ip));
Scott committed
197

Scott committed
198 199 200 201 202 203 204 205
	foreach ($questions as $question) {
		$htmloptions = qa_post_html_options($question);
		$htmloptions['tagsview'] = false;
		$htmloptions['voteview'] = false;
		$htmloptions['ipview'] = false;
		$htmloptions['answersview'] = false;
		$htmloptions['viewsview'] = false;
		$htmloptions['updateview'] = false;
Scott committed
206

Scott committed
207
		$htmlfields = qa_any_to_q_html_fields($question, $userid, qa_cookie_get(), $usershtml, null, $htmloptions);
Scott committed
208

Scott committed
209 210
		if (isset($htmlfields['what_url'])) // link directly to relevant content
			$htmlfields['url'] = $htmlfields['what_url'];
Scott committed
211

Scott committed
212
		$hasother = isset($question['opostid']);
Scott committed
213

Scott committed
214 215
		if ($question[$hasother ? 'ohidden' : 'hidden'] && !isset($question[$hasother ? 'oupdatetype' : 'updatetype'])) {
			$htmlfields['what_2'] = qa_lang_html('main/hidden');
Scott committed
216

Scott committed
217 218 219 220
			if (@$htmloptions['whenview']) {
				$updated = @$question[$hasother ? 'oupdated' : 'updated'];
				if (isset($updated))
					$htmlfields['when_2'] = qa_when_to_html($updated, @$htmloptions['fulldatedays']);
Scott committed
221 222 223
			}
		}

Scott committed
224 225
		$qa_content['q_list']['qs'][] = $htmlfields;
	}
Scott committed
226

Scott committed
227 228
} else
	$qa_content['q_list']['title'] = qa_lang_html_sub('misc/no_activity_from_x', qa_html($ip));
Scott committed
229 230


Scott committed
231
return $qa_content;