Passed
Push — search ( 5eeca1...47d2b9 )
by Simon
03:02
created

PageSearch::getNameSearchResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
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\AccessDeniedException;
14
use Waca\Exceptions\ApplicationLogicException;
15
use Waca\Fragments\RequestListData;
16
use Waca\Helpers\SearchHelpers\RequestSearchHelper;
17
use Waca\SessionAlert;
18
use Waca\Tasks\PagedInternalPageBase;
19
use Waca\WebRequest;
20
21
class PageSearch extends PagedInternalPageBase
22
{
23
    use RequestListData;
24
25
    /**
26
     * Main function for this page, when no specific actions are called.
27
     */
28
    protected function main()
29
    {
30
        $this->setHtmlTitle('Search');
31
32
        $database = $this->getDatabase();
33
        $currentUser = User::getCurrent($database);
34
35
        $this->assign('canSearchByComment', $this->barrierTest('byComment', $currentUser));
36
        $this->assign('canSearchByEmail', $this->barrierTest('byEmail', $currentUser));
37
        $this->assign('canSearchByIp', $this->barrierTest('byIp', $currentUser));
38
        $this->assign('canSearchByName', $this->barrierTest('byName', $currentUser));
39
        $this->assign('canSeeNonConfirmed', $this->barrierTest('allowNonConfirmed', $currentUser));
40
41
        $this->setTemplate('search/main.tpl');
42
43
        // Dual-mode page
44
        if (WebRequest::getString('type') !== null) {
45
            $searchType = WebRequest::getString('type');
46
            $searchTerm = WebRequest::getString('term');
47
48
            $excludeNonConfirmed = true;
49
            if ($this->barrierTest('allowNonConfirmed', $currentUser)) {
50
                $excludeNonConfirmed = WebRequest::getBoolean('excludeNonConfirmed');
51
            }
52
53
            $validationError = "";
54
            if (!$this->validateSearchParameters($searchType, $searchTerm, $validationError)) {
55
                SessionAlert::error($validationError, "Search error");
56
57
                $this->assign('term', $searchTerm);
58
                $this->assign('target', $searchType);
59
                $this->assign('excludeNonConfirmed', $excludeNonConfirmed);
60
                $this->assign('hasResultset', false);
61
62
                return;
63
            }
64
65
            // searchType known to be sane from the validate step above
66
            if (!$this->barrierTest('by' . ucfirst($searchType), User::getCurrent($this->getDatabase()))) {
0 ignored issues
show
Bug introduced by
It seems like $searchType can also be of type null; however, parameter $string of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
            if (!$this->barrierTest('by' . ucfirst(/** @scrutinizer ignore-type */ $searchType), User::getCurrent($this->getDatabase()))) {
Loading history...
67
                // only accessible by url munging, don't care about the UX
68
                throw new AccessDeniedException($this->getSecurityManager());
69
            }
70
71
            $requestSearch = RequestSearchHelper::get($database);
72
73
            $this->setSearchHelper($requestSearch);
74
            $this->setupLimits();
75
76
            if ($excludeNonConfirmed) {
77
                $requestSearch->withConfirmedEmail();
78
            }
79
80
            switch ($searchType) {
81
                case 'name':
82
                    $this->getNameSearchResults($requestSearch, $searchTerm);
0 ignored issues
show
Bug introduced by
It seems like $searchTerm can also be of type null; however, parameter $searchTerm of Waca\Pages\PageSearch::getNameSearchResults() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
                    $this->getNameSearchResults($requestSearch, /** @scrutinizer ignore-type */ $searchTerm);
Loading history...
83
                    break;
84
                case 'email':
85
                    $this->getEmailSearchResults($requestSearch, $searchTerm);
0 ignored issues
show
Bug introduced by
It seems like $searchTerm can also be of type null; however, parameter $searchTerm of Waca\Pages\PageSearch::getEmailSearchResults() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
                    $this->getEmailSearchResults($requestSearch, /** @scrutinizer ignore-type */ $searchTerm);
Loading history...
86
                    break;
87
                case 'ip':
88
                    $this->getIpSearchResults($requestSearch, $searchTerm);
0 ignored issues
show
Bug introduced by
It seems like $searchTerm can also be of type null; however, parameter $searchTerm of Waca\Pages\PageSearch::getIpSearchResults() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
                    $this->getIpSearchResults($requestSearch, /** @scrutinizer ignore-type */ $searchTerm);
Loading history...
89
                    break;
90
                case 'comment':
91
                    $this->getCommentSearchResults($requestSearch, $searchTerm);
0 ignored issues
show
Bug introduced by
It seems like $searchTerm can also be of type null; however, parameter $searchTerm of Waca\Pages\PageSearch::getCommentSearchResults() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
                    $this->getCommentSearchResults($requestSearch, /** @scrutinizer ignore-type */ $searchTerm);
Loading history...
92
                    break;
93
            }
94
95
            /** @var Request[] $results */
96
            $results = $requestSearch->getRecordCount($count)->fetch();
97
98
            $formParameters = [
99
                'term' => $searchTerm,
100
                'type' => $searchType,
101
            ];
102
103
            if ($excludeNonConfirmed) {
104
                $formParameters['excludeNonConfirmed'] = true;
105
            }
106
107
            $this->setupPageData($count, $formParameters);
108
109
            // deal with results
110
            $this->assign('requests', $this->prepareRequestData($results));
111
            $this->assign('resultCount', count($results));
112
            $this->assign('hasResultset', true);
113
        }
114
        else {
115
            $this->assign('target', 'name');
116
            $this->assign('hasResultset', false);
117
            $this->assign('limit', 50);
118
            $this->assign('excludeNonConfirmed', true);
119
        }
120
    }
121
122
    /**
123
     * Gets search results by name
124
     *
125
     * @param RequestSearchHelper $searchHelper
126
     * @param string              $searchTerm
127
     */
128
    private function getNameSearchResults(RequestSearchHelper $searchHelper, string $searchTerm)
129
    {
130
        $padded = '%' . $searchTerm . '%';
131
        $searchHelper->byName($padded);
132
    }
133
134
    /**
135
     * Gets search results by comment
136
     *
137
     * @param RequestSearchHelper $searchHelper
138
     * @param string              $searchTerm
139
     */
140
    private function getCommentSearchResults(RequestSearchHelper $searchHelper, string $searchTerm)
141
    {
142
        $padded = '%' . $searchTerm . '%';
143
        $searchHelper->byComment($padded);
144
145
        $currentUser = User::getCurrent($this->getDatabase());
146
        $commentSecurity = ['requester', 'user'];
147
148
        if ($this->barrierTest('seeRestrictedComments', $currentUser, 'RequestData')) {
149
            $commentSecurity[] = 'admin';
150
        }
151
152
        if ($this->barrierTest('seeCheckuserComments', $currentUser, 'RequestData')) {
153
            $commentSecurity[] = 'checkuser';
154
        }
155
156
        $searchHelper->byCommentSecurity($commentSecurity);
157
    }
158
159
    /**
160
     * Gets search results by email
161
     *
162
     * @param RequestSearchHelper $searchHelper
163
     * @param string              $searchTerm
164
     *
165
     * @throws ApplicationLogicException
166
     */
167
    private function getEmailSearchResults(RequestSearchHelper $searchHelper, string $searchTerm)
168
    {
169
        if ($searchTerm === "@") {
170
            throw new ApplicationLogicException('The search term "@" is not valid for email address searches!');
171
        }
172
173
        $padded = '%' . $searchTerm . '%';
174
175
        $searchHelper->byEmailAddress($padded)->excludingPurgedData($this->getSiteConfiguration());
176
    }
177
178
    /**
179
     * Gets search results by IP address or XFF IP address
180
     *
181
     * @param RequestSearchHelper $searchHelper
182
     * @param string              $searchTerm
183
     */
184
    private function getIpSearchResults(RequestSearchHelper $searchHelper, string $searchTerm)
185
    {
186
        $searchHelper
187
            ->byIp($searchTerm)
188
            ->excludingPurgedData($this->getSiteConfiguration());
189
    }
190
191
    /**
192
     * @param string $searchType
193
     * @param string $searchTerm
194
     *
195
     * @param string $errorMessage
196
     *
197
     * @return bool true if parameters are valid
198
     */
199
    protected function validateSearchParameters($searchType, $searchTerm, &$errorMessage)
200
    {
201
        if (!in_array($searchType, array('name', 'email', 'ip', 'comment'))) {
202
            $errorMessage = 'Unknown search type';
203
204
            return false;
205
        }
206
207
        if ($searchTerm === '%' || $searchTerm === '' || $searchTerm === null) {
208
            $errorMessage = 'No search term specified entered';
209
210
            return false;
211
        }
212
213
        $errorMessage = "";
214
215
        return true;
216
    }
217
}
218