aboutsummaryrefslogtreecommitdiffstats
path: root/htdocs/application/controllers/Spamadmin.php
blob: 8bd96825fe19ce04d9f4c26338105dd37e4e67e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
 * Class and Function List:
 * Function list:
 * - __construct()
 * - index()
 * - spam_detail()
 * - blacklist()
 * - unblock_ip()
 * Classes list:
 * - Spamadmin extends CI_Controller
 */

class Spamadmin extends CI_Controller
{
	
	function __construct() 
	{
		parent::__construct();

		//protection
		$user = $this->config->item('spamadmin_user');
		$pass = $this->config->item('spamadmin_pass');

		// FastCGI doesn't provide PHP_AUTH_USER and PHP_AUTH_PW, apparently?
		if (empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['PHP_AUTH_PW'])) {
			if (!empty($_SERVER['HTTP_AUTHORIZATION'])) {
				list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
			}
		}

		// If they're not set, set them to blank. The null coalesce operator would be handy here, but
		// that's PHP 7.0 and higher...
		if (empty($_SERVER['PHP_AUTH_USER']))
		{
			$_SERVER['PHP_AUTH_USER'] = "";
		}
		if (empty($_SERVER['PHP_AUTH_PW']))
		{
			$_SERVER['PHP_AUTH_PW'] = "";
		}
		
		if ($user === '' || $pass === '' || $_SERVER['PHP_AUTH_USER'] !== $user || $_SERVER['PHP_AUTH_PW'] !== $pass) 
		{
			header('WWW-Authenticate: Basic realm="Spamadmin"');
			header('HTTP/1.0 401 Unauthorized');
			exit;
		}
	}
	
	function index() 
	{
		$this->load->model('pastes');
		$pastes_to_delete = $this->input->post('pastes_to_delete');
		
		if ($pastes_to_delete) 
		{
			foreach (explode(' ', $pastes_to_delete) as $pid) 
			{
				$this->db->where('pid', $pid);
				$this->db->delete('pastes');
			}
			redirect(site_url('spamadmin/' . $this->uri->segment(2)));
		}

		//render view
		$data = $this->pastes->getSpamLists();
		$this->load->view('list_ips', $data);
	}
	
	function spam_detail() 
	{
		$this->load->model('pastes');
		$ip_address = $this->uri->segment(2);
		
		if ($this->input->post('confirm_remove') && $ip_address != '') 
		{
			$this->db->where('ip_address', $ip_address);
			$this->db->delete('pastes');
			$paste_count = $this->db->affected_rows();
			
			if ($this->input->post('block_ip')) 
			{
				$query = $this->db->get_where('blocked_ips', array(
					'ip_address' => $ip_address
				));
				
				if ($query->num_rows() == 0) 
				{
					$this->db->insert('blocked_ips', array(
						'ip_address' => $ip_address,
						'blocked_at' => time() ,
						'spam_attempts' => $paste_count,
					));
				}
			}
		}

		//fill data
		$data = $this->pastes->getSpamLists('spamadmin/' . $ip_address, $seg = 3, $ip_address);
		$data['ip_address'] = $ip_address;
		$ip = explode('.', $ip_address);
		
		if (count($ip) > 1) 
		{
			$ip_firstpart = $ip[0] . '.' . $ip[1] . '.';
			$data['ip_range'] = $ip_firstpart . '*.*';
		}
		else
		{

			// ipv6
			$ip = explode(':', $ip_address);
			$ip_firstpart = $ip[0] . ':' . $ip[1] . ':' . $ip[2] . ':' . $ip[3] . ':' . $ip[4] . ':' . $ip[5] . ':' . $ip[6];
			$data['ip_range'] = $ip_firstpart . ':*';
		}

		//view
		$this->load->view('spam_detail', $data);
	}
	
	function blacklist() 
	{

		//pagination
		$amount = $this->config->item('per_page');
		$page = ($this->uri->segment(3) ? $this->uri->segment(3) : 0);

		//get
		$this->db->select('ip_address, blocked_at, spam_attempts');
		$this->db->order_by('blocked_at desc, ip_address asc');
		$query = $this->db->get('blocked_ips', $amount, $page);
		$data['blocked_ips'] = $query->result_array();

		//pagination
		$config['base_url'] = site_url('spamadmin/blacklist');
		$query = $this->db->get('blocked_ips');
		$config['total_rows'] = $query->num_rows();
		$config['per_page'] = $amount;
		$config['num_links'] = 9;
		$config['full_tag_open'] = '<div class="pages">';
		$config['full_tag_close'] = '</div>';
		$config['uri_segment'] = 3;
		$this->load->library('pagination');
		$this->pagination->initialize($config);
		$data['pages'] = $this->pagination->create_links();

		//view
		$this->load->view('list_blocked_ips', $data);
	}
	
	function unblock_ip() 
	{
		$ip_address = $this->uri->segment(4);
		$this->db->where('ip_address', $ip_address);
		$this->db->delete('blocked_ips');
		redirect('spamadmin/blacklist');
	}
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy