qa-page-ip.php 7.58 KB
Newer Older
Gideon Greenspan committed
1
<?php
Scott Vivian committed
2

Gideon Greenspan committed
3 4 5 6 7
/*
	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-page-ip.php
	Version: See define()s at top of qa-include/qa-base.php
	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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
	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;
	}

	require_once QA_INCLUDE_DIR.'qa-db-selects.php';
	require_once QA_INCLUDE_DIR.'qa-app-format.php';

Scott Vivian committed
35

Gideon Greenspan committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	$ip=qa_request_part(1); // picked up from qa-page.php
	if (long2ip(ip2long($ip))!==$ip)
		return include QA_INCLUDE_DIR.'qa-page-not-found.php';


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

	$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 Vivian committed
58 59


Gideon Greenspan committed
60 61
//	Check we have permission to view this page, and whether we can block or unblock IPs

Gideon Greenspan committed
62
	if (qa_user_maximum_permit_error('permit_anon_view_ips')) {
Gideon Greenspan committed
63 64 65 66
		$qa_content=qa_content_prepare();
		$qa_content['error']=qa_lang_html('users/no_permission');
		return $qa_content;
	}
Scott Vivian committed
67

Gideon Greenspan committed
68
	$blockable=qa_user_level_maximum()>=QA_USER_LEVEL_MODERATOR; // allow moderator in one category to block across all categories
Scott Vivian committed
69

Gideon Greenspan committed
70 71 72

//	Perform blocking or unblocking operations as appropriate

Gideon Greenspan committed
73 74 75 76 77
	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');

		elseif ($blockable) {
Scott Vivian committed
78

Gideon Greenspan committed
79 80 81
			if (qa_clicked('doblock')) {
				$oldblocked=qa_opt('block_ips_write');
				qa_set_option('block_ips_write', (strlen($oldblocked) ? ($oldblocked.' , ') : '').$ip);
Scott Vivian committed
82

Gideon Greenspan committed
83 84 85
				qa_report_event('ip_block', $userid, qa_get_logged_in_handle(), qa_cookie_get(), array(
					'ip' => $ip,
				));
Scott Vivian committed
86

Gideon Greenspan committed
87 88
				qa_redirect(qa_request());
			}
Scott Vivian committed
89

Gideon Greenspan committed
90 91
			if (qa_clicked('dounblock')) {
				require_once QA_INCLUDE_DIR.'qa-app-limits.php';
Scott Vivian committed
92

Gideon Greenspan committed
93
				$blockipclauses=qa_block_ips_explode(qa_opt('block_ips_write'));
Scott Vivian committed
94

Gideon Greenspan committed
95 96 97
				foreach ($blockipclauses as $key => $blockipclause)
					if (qa_block_ip_match($ip, $blockipclause))
						unset($blockipclauses[$key]);
Scott Vivian committed
98

Gideon Greenspan committed
99
				qa_set_option('block_ips_write', implode(' , ', $blockipclauses));
Scott Vivian committed
100

Gideon Greenspan committed
101 102 103
				qa_report_event('ip_unblock', $userid, qa_get_logged_in_handle(), qa_cookie_get(), array(
					'ip' => $ip,
				));
Scott Vivian committed
104

Gideon Greenspan committed
105 106
				qa_redirect(qa_request());
			}
Scott Vivian committed
107

Gideon Greenspan committed
108 109
			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 Vivian committed
110

Gideon Greenspan committed
111 112
				require_once QA_INCLUDE_DIR.'qa-db-admin.php';
				require_once QA_INCLUDE_DIR.'qa-app-posts.php';
Scott Vivian committed
113

Gideon Greenspan committed
114
				$postids=qa_db_get_ip_visible_postids($ip);
Scott Vivian committed
115

Gideon Greenspan committed
116 117
				foreach ($postids as $postid)
					qa_post_set_hidden($postid, true, $userid);
Scott Vivian committed
118

Gideon Greenspan committed
119 120
				qa_redirect(qa_request());
			}
Gideon Greenspan committed
121 122
		}
	}
Scott Vivian committed
123

Gideon Greenspan committed
124 125 126 127

//	Combine sets of questions and get information for users

	$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 Vivian committed
128

Gideon Greenspan committed
129 130 131
	$usershtml=qa_userids_handles_html(qa_any_get_userids_handles($questions));

	$hostname=gethostbyaddr($ip);
Scott Vivian committed
132

Gideon Greenspan committed
133 134

//	Prepare content for theme
Scott Vivian committed
135

Gideon Greenspan committed
136 137 138
	$qa_content=qa_content_prepare();

	$qa_content['title']=qa_lang_html_sub('main/ip_address_x', qa_html($ip));
Gideon Greenspan committed
139
	$qa_content['error']=@$pageerror;
Gideon Greenspan committed
140 141

	$qa_content['form']=array(
Gideon Greenspan committed
142
		'tags' => 'method="post" action="'.qa_self_html().'"',
Scott Vivian committed
143

Gideon Greenspan committed
144
		'style' => 'wide',
Scott Vivian committed
145

Gideon Greenspan committed
146 147 148 149 150
		'fields' => array(
			'host' => array(
				'type' => 'static',
				'label' => qa_lang_html('misc/host_name'),
				'value' => qa_html($hostname),
Gideon Greenspan committed
151
			),
Gideon Greenspan committed
152
		),
Scott Vivian committed
153

Gideon Greenspan committed
154 155 156 157
		'hidden' => array(
			'code' => qa_get_form_security_code('ip-'.$ip),
		),
	);
Scott Vivian committed
158

Gideon Greenspan committed
159 160 161

	if ($blockable) {
		require_once QA_INCLUDE_DIR.'qa-app-limits.php';
Scott Vivian committed
162

Gideon Greenspan committed
163 164
		$blockipclauses=qa_block_ips_explode(qa_opt('block_ips_write'));
		$matchclauses=array();
Scott Vivian committed
165

Gideon Greenspan committed
166 167 168
		foreach ($blockipclauses as $blockipclause)
			if (qa_block_ip_match($ip, $blockipclause))
				$matchclauses[]=$blockipclause;
Scott Vivian committed
169

Gideon Greenspan committed
170 171 172 173 174 175
		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 Vivian committed
176

Gideon Greenspan committed
177
			$qa_content['form']['buttons']['unblock']=array(
Gideon Greenspan committed
178
				'tags' => 'name="dounblock"',
Gideon Greenspan committed
179 180
				'label' => qa_lang_html('misc/unblock_ip_button'),
			);
Scott Vivian committed
181

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

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

Scott Vivian committed
195

Gideon Greenspan committed
196
	$qa_content['q_list']['qs']=array();
Scott Vivian committed
197

Gideon Greenspan committed
198 199
	if (count($questions)) {
		$qa_content['q_list']['title']=qa_lang_html_sub('misc/recent_activity_from_x', qa_html($ip));
Scott Vivian committed
200

Gideon Greenspan committed
201
		foreach ($questions as $question) {
Gideon Greenspan committed
202
			$htmloptions=qa_post_html_options($question);
Gideon Greenspan committed
203 204 205 206
			$htmloptions['tagsview']=false;
			$htmloptions['voteview']=false;
			$htmloptions['ipview']=false;
			$htmloptions['answersview']=false;
Gideon Greenspan committed
207
			$htmloptions['viewsview']=false;
Gideon Greenspan committed
208
			$htmloptions['updateview']=false;
Scott Vivian committed
209

Gideon Greenspan committed
210
			$htmlfields=qa_any_to_q_html_fields($question, $userid, qa_cookie_get(), $usershtml, null, $htmloptions);
Scott Vivian committed
211

Gideon Greenspan committed
212 213
			if (isset($htmlfields['what_url'])) // link directly to relevant content
				$htmlfields['url']=$htmlfields['what_url'];
Scott Vivian committed
214

Gideon Greenspan committed
215
			$hasother=isset($question['opostid']);
Scott Vivian committed
216

Gideon Greenspan committed
217
			if ($question[$hasother ? 'ohidden' : 'hidden'] && !isset($question[$hasother ? 'oupdatetype' : 'updatetype'])) {
Gideon Greenspan committed
218 219
				$htmlfields['what_2']=qa_lang_html('main/hidden');

Gideon Greenspan committed
220 221 222 223 224
				if (@$htmloptions['whenview']) {
					$updated=@$question[$hasother ? 'oupdated' : 'updated'];
					if (isset($updated))
						$htmlfields['when_2']=qa_when_to_html($updated, @$htmloptions['fulldatedays']);
				}
Gideon Greenspan committed
225 226 227 228 229 230 231
			}

			$qa_content['q_list']['qs'][]=$htmlfields;
		}

	} else
		$qa_content['q_list']['title']=qa_lang_html_sub('misc/no_activity_from_x', qa_html($ip));
Scott Vivian committed
232 233


Gideon Greenspan committed
234
	return $qa_content;
Scott Vivian committed
235

Gideon Greenspan committed
236 237 238 239

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