Completed
Branch newinternal (cdd491)
by Simon
04:39
created

PageSearch::validateSearchParameters()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 3
b 0
f 0
nc 3
nop 3
dl 0
loc 15
rs 8.8571
ccs 0
cts 13
cp 0
crap 30
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Pages;
10
11
use Waca\DataObjects\Request;
12
use Waca\DataObjects\User;
13
use Waca\Exceptions\ApplicationLogicException;
14
use Waca\Helpers\SearchHelpers\RequestSearchHelper;
15
use Waca\Security\SecurityConfiguration;
16
use Waca\SessionAlert;
17
use Waca\Tasks\InternalPageBase;
18
use Waca\WebRequest;
19
20
class PageSearch extends InternalPageBase
21
{
22
	/**
23
	 * Main function for this page, when no specific actions are called.
24
	 */
25
	protected function main()
26
	{
27
		$this->setHtmlTitle('Search');
28
29
		// Dual-mode page
30
		if (WebRequest::wasPosted()) {
31
			$this->validateCSRFToken();
32
33
			$searchType = WebRequest::postString('type');
34
			$searchTerm = WebRequest::postString('term');
35
36
			$validationError = "";
37
			if(!$this->validateSearchParameters($searchType, $searchTerm, $validationError)) {
38
				SessionAlert::error($validationError, "Search error");
39
				$this->redirect("search");
40
				return;
41
			}
42
43
			$results = array();
44
45
			switch ($searchType) {
46
				case 'name':
47
					$results = $this->getNameSearchResults($searchTerm);
48
					break;
49
				case 'email':
50
					$results = $this->getEmailSearchResults($searchTerm);
51
					break;
52
				case 'ip':
53
					$results = $this->getIpSearchResults($searchTerm);
54
					break;
55
			}
56
57
			// deal with results
58
			$this->assign('requests', $results);
59
			$this->assign('term', $searchTerm);
60
			$this->assign('target', $searchType);
61
62
			$userIds = array_map(
63
				function(Request $entry) {
64
					return $entry->getReserved();
65
				},
66
				$results);
67
			$userList = User::getUsernames($userIds, $this->getDatabase());
68
			$this->assign('userlist', $userList);
69
70
			$this->assignCSRFToken();
71
			$this->setTemplate('search/searchResult.tpl');
72
		}
73
		else {
74
			$this->assignCSRFToken();
75
			$this->setTemplate('search/searchForm.tpl');
76
		}
77
	}
78
79
	/**
80
	 * Gets search results by name
81
	 *
82
	 * @param string $searchTerm
83
	 *
84
	 * @returns Request[]
85
	 */
86
	private function getNameSearchResults($searchTerm)
87
	{
88
		$padded = '%' . $searchTerm . '%';
89
90
		return RequestSearchHelper::get($this->getDatabase())
91
			->byName($padded)
92
			->excludingPurgedData($this->getSiteConfiguration())
93
			->fetch();
94
	}
95
96
	/**
97
	 * Gets search results by email
98
	 *
99
	 * @param string $searchTerm
100
	 *
101
	 * @return Request[]
102
	 * @throws ApplicationLogicException
103
	 */
104
	private function getEmailSearchResults($searchTerm)
105
	{
106
		if ($searchTerm === "@") {
107
			throw new ApplicationLogicException('The search term "@" is not valid for email address searches!');
108
		}
109
110
		$padded = '%' . $searchTerm . '%';
111
112
		return RequestSearchHelper::get($this->getDatabase())
113
			->byEmailAddress($padded)
114
			->excludingPurgedData($this->getSiteConfiguration())
115
			->fetch();
116
	}
117
118
	/**
119
	 * Gets search results by IP address or XFF IP address
120
	 *
121
	 * @param string $searchTerm
122
	 *
123
	 * @returns Request[]
124
	 */
125
	private function getIpSearchResults($searchTerm)
126
	{
127
		return RequestSearchHelper::get($this->getDatabase())
128
			->byIp($searchTerm)
129
			->excludingPurgedData($this->getSiteConfiguration())
130
			->fetch();
131
	}
132
133
	/**
134
	 * Sets up the security for this page. If certain actions have different permissions, this should be reflected in
135
	 * the return value from this function.
136
	 *
137
	 * If this page even supports actions, you will need to check the route
138
	 *
139
	 * @return SecurityConfiguration
140
	 * @category Security-Critical
141
	 */
142
	protected function getSecurityConfiguration()
143
	{
144
		return $this->getSecurityManager()->configure()->asInternalPage();
145
	}
146
147
	/**
148
	 * @param string $searchType
149
	 * @param string $searchTerm
150
	 *
151
	 * @param string $errorMessage
152
	 *
153
	 * @return bool true if parameters are valid
154
	 * @throws ApplicationLogicException
155
	 */
156
	protected function validateSearchParameters($searchType, $searchTerm, &$errorMessage)
157
	{
158
		if (!in_array($searchType, array('name', 'email', 'ip'))) {
159
			$errorMessage = 'Unknown search type';
160
			return false;
161
		}
162
163
		if ($searchTerm === '%' || $searchTerm === '' || $searchTerm === null) {
164
			$errorMessage = 'No search term specified entered';
165
			return false;
166
		}
167
168
		$errorMessage = "";
169
		return true;
170
	}
171
}