|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Waca\Pages; |
|
4
|
|
|
|
|
5
|
|
|
use Waca\DataObjects\Request; |
|
6
|
|
|
use Waca\DataObjects\User; |
|
7
|
|
|
use Waca\Exceptions\ApplicationLogicException; |
|
8
|
|
|
use Waca\Helpers\RequestSearchHelper; |
|
9
|
|
|
use Waca\Security\SecurityConfiguration; |
|
10
|
|
|
use Waca\Tasks\InternalPageBase; |
|
11
|
|
|
use Waca\WebRequest; |
|
12
|
|
|
|
|
13
|
|
|
class PageSearch extends InternalPageBase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Main function for this page, when no specific actions are called. |
|
17
|
|
|
*/ |
|
18
|
|
|
protected function main() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->setHtmlTitle('Search'); |
|
21
|
|
|
|
|
22
|
|
|
// Dual-mode page |
|
23
|
|
|
if (WebRequest::wasPosted()) { |
|
24
|
|
|
$this->validateCSRFToken(); |
|
25
|
|
|
// TODO: logging |
|
26
|
|
|
|
|
27
|
|
|
$searchType = WebRequest::postString('type'); |
|
28
|
|
|
$searchTerm = WebRequest::postString('term'); |
|
29
|
|
|
|
|
30
|
|
|
$this->validateSearchParameters($searchType, $searchTerm); |
|
31
|
|
|
|
|
32
|
|
|
$results = array(); |
|
33
|
|
|
|
|
34
|
|
|
switch ($searchType) { |
|
35
|
|
|
case 'name': |
|
36
|
|
|
$results = $this->getNameSearchResults($searchTerm); |
|
37
|
|
|
break; |
|
38
|
|
|
case 'email': |
|
39
|
|
|
$results = $this->getEmailSearchResults($searchTerm); |
|
40
|
|
|
break; |
|
41
|
|
|
case 'ip': |
|
42
|
|
|
$results = $this->getIpSearchResults($searchTerm); |
|
43
|
|
|
break; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// deal with results |
|
47
|
|
|
$this->assign('requests', $results); |
|
48
|
|
|
$this->assign('term', $searchTerm); |
|
49
|
|
|
$this->assign('target', $searchType); |
|
50
|
|
|
|
|
51
|
|
|
$userIds = array_map( |
|
52
|
|
|
function(Request $entry) { |
|
53
|
|
|
return $entry->getReserved(); |
|
54
|
|
|
}, |
|
55
|
|
|
$results); |
|
56
|
|
|
$userList = User::getUsernames($userIds, $this->getDatabase()); |
|
57
|
|
|
$this->assign('userlist', $userList); |
|
58
|
|
|
|
|
59
|
|
|
$this->assignCSRFToken(); |
|
60
|
|
|
$this->setTemplate('search/searchResult.tpl'); |
|
61
|
|
|
} |
|
62
|
|
|
else { |
|
63
|
|
|
$this->assignCSRFToken(); |
|
64
|
|
|
$this->setTemplate('search/searchForm.tpl'); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Gets search results by name |
|
70
|
|
|
* |
|
71
|
|
|
* @param $searchTerm string |
|
72
|
|
|
* |
|
73
|
|
|
* @returns array<Request> |
|
74
|
|
|
*/ |
|
75
|
|
|
private function getNameSearchResults($searchTerm) |
|
76
|
|
|
{ |
|
77
|
|
|
$padded = '%' . $searchTerm . '%'; |
|
78
|
|
|
|
|
79
|
|
|
return RequestSearchHelper::get($this->getDatabase()) |
|
80
|
|
|
->byName($padded) |
|
81
|
|
|
->excludingPurgedData($this->getSiteConfiguration()) |
|
82
|
|
|
->fetch(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Gets search results by email |
|
87
|
|
|
* |
|
88
|
|
|
* @param $searchTerm string |
|
89
|
|
|
* |
|
90
|
|
|
* @return array <Request> |
|
91
|
|
|
* @throws ApplicationLogicException |
|
92
|
|
|
*/ |
|
93
|
|
|
private function getEmailSearchResults($searchTerm) |
|
94
|
|
|
{ |
|
95
|
|
|
if ($searchTerm === "@") { |
|
96
|
|
|
throw new ApplicationLogicException('The search term "@" is not valid for email address searches!'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$padded = '%' . $searchTerm . '%'; |
|
100
|
|
|
|
|
101
|
|
|
return RequestSearchHelper::get($this->getDatabase()) |
|
102
|
|
|
->byEmailAddress($padded) |
|
103
|
|
|
->excludingPurgedData($this->getSiteConfiguration()) |
|
104
|
|
|
->fetch(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Gets search results by IP address or XFF IP address |
|
109
|
|
|
* |
|
110
|
|
|
* @param $searchTerm string |
|
111
|
|
|
* |
|
112
|
|
|
* @returns array<Request> |
|
113
|
|
|
*/ |
|
114
|
|
|
private function getIpSearchResults($searchTerm) |
|
115
|
|
|
{ |
|
116
|
|
|
return RequestSearchHelper::get($this->getDatabase()) |
|
117
|
|
|
->byIp($searchTerm) |
|
118
|
|
|
->excludingPurgedData($this->getSiteConfiguration()) |
|
119
|
|
|
->fetch(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Sets up the security for this page. If certain actions have different permissions, this should be reflected in |
|
124
|
|
|
* the return value from this function. |
|
125
|
|
|
* |
|
126
|
|
|
* If this page even supports actions, you will need to check the route |
|
127
|
|
|
* |
|
128
|
|
|
* @return SecurityConfiguration |
|
129
|
|
|
* @category Security-Critical |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function getSecurityConfiguration() |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->getSecurityManager()->configure()->asInternalPage(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param $searchType |
|
138
|
|
|
* @param $searchTerm |
|
139
|
|
|
* |
|
140
|
|
|
* @throws ApplicationLogicException |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function validateSearchParameters($searchType, $searchTerm) |
|
143
|
|
|
{ |
|
144
|
|
|
if (!in_array($searchType, array('name', 'email', 'ip'))) { |
|
145
|
|
|
// todo: handle more gracefully. |
|
146
|
|
|
throw new ApplicationLogicException('Unknown search type'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
if ($searchTerm === '%' || $searchTerm === '') { |
|
150
|
|
|
throw new ApplicationLogicException('No search term specified entered'); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |