PageSearch::getNameSearchResults()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 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
            $formParameters = [
54
                'term' => $searchTerm,
55
                'type' => $searchType,
56
            ];
57
58
            if ($excludeNonConfirmed) {
59
                $formParameters['excludeNonConfirmed'] = true;
60
            }
61
62
            $requestSearch = RequestSearchHelper::get($database);
63
            $this->setSearchHelper($requestSearch);
64
            $this->setupLimits();
65
66
            $validationError = "";
67
            if (!$this->validateSearchParameters($searchType, $searchTerm, $validationError)) {
68
                SessionAlert::error($validationError, "Search error");
69
70
                $this->setupPageData(0, $formParameters);
71
                $this->assign('hasResultset', false);
72
73
                return;
74
            }
75
76
            // searchType known to be sane from the validate step above
77
            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

77
            if (!$this->barrierTest('by' . ucfirst(/** @scrutinizer ignore-type */ $searchType), User::getCurrent($this->getDatabase()))) {
Loading history...
78
                // only accessible by url munging, don't care about the UX
79
                throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager());
80
            }
81
82
            if ($excludeNonConfirmed) {
83
                $requestSearch->withConfirmedEmail();
84
            }
85
86
            switch ($searchType) {
87
                case 'name':
88
                    $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

88
                    $this->getNameSearchResults($requestSearch, /** @scrutinizer ignore-type */ $searchTerm);
Loading history...
89
                    break;
90
                case 'email':
91
                    $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

91
                    $this->getEmailSearchResults($requestSearch, /** @scrutinizer ignore-type */ $searchTerm);
Loading history...
92
                    break;
93
                case 'ip':
94
                    $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

94
                    $this->getIpSearchResults($requestSearch, /** @scrutinizer ignore-type */ $searchTerm);
Loading history...
95
                    break;
96
                case 'comment':
97
                    $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

97
                    $this->getCommentSearchResults($requestSearch, /** @scrutinizer ignore-type */ $searchTerm);
Loading history...
98
                    break;
99
            }
100
101
            /** @var Request[] $results */
102
            $results = $requestSearch->getRecordCount($count)->fetch();
103
            $this->setupPageData($count, $formParameters);
104
105
            // deal with results
106
            $this->assign('requests', $this->prepareRequestData($results));
107
            $this->assign('resultCount', count($results));
108
            $this->assign('hasResultset', true);
109
110
            list($defaultSort, $defaultSortDirection) = WebRequest::requestListDefaultSort();
111
            $this->assign('defaultSort', $defaultSort);
112
            $this->assign('defaultSortDirection', $defaultSortDirection);
113
        }
114
        else {
115
            $this->assign('type', '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