Issues (186)

includes/Pages/PageSearch.php (5 issues)

Labels
Severity
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 * ACC Development Team. Please see team.json for a list of contributors.     *
5
 *                                                                            *
6
 * This is free and unencumbered software released into the public domain.    *
7
 * Please see LICENSE.md for the full licencing statement.                    *
8
 ******************************************************************************/
9
10
namespace Waca\Pages;
11
12
use Waca\DataObjects\Request;
13
use Waca\DataObjects\User;
14
use Waca\Exceptions\AccessDeniedException;
15
use Waca\Exceptions\ApplicationLogicException;
16
use Waca\Fragments\RequestListData;
17
use Waca\Helpers\SearchHelpers\RequestSearchHelper;
18
use Waca\SessionAlert;
19
use Waca\Tasks\PagedInternalPageBase;
20
use Waca\WebRequest;
21
22
class PageSearch extends PagedInternalPageBase
23
{
24
    use RequestListData;
25
26
    /**
27
     * Main function for this page, when no specific actions are called.
28
     */
29
    protected function main()
30
    {
31
        $this->setHtmlTitle('Search');
32
33
        $database = $this->getDatabase();
34
        $currentUser = User::getCurrent($database);
35
36
        $this->assign('canSearchByComment', $this->barrierTest('byComment', $currentUser));
37
        $this->assign('canSearchByEmail', $this->barrierTest('byEmail', $currentUser));
38
        $this->assign('canSearchByIp', $this->barrierTest('byIp', $currentUser));
39
        $this->assign('canSearchByName', $this->barrierTest('byName', $currentUser));
40
        $this->assign('canSeeNonConfirmed', $this->barrierTest('allowNonConfirmed', $currentUser));
41
42
        $this->setTemplate('search/main.tpl');
43
44
        // Dual-mode page
45
        if (WebRequest::getString('type') !== null) {
46
            $searchType = WebRequest::getString('type');
47
            $searchTerm = WebRequest::getString('term');
48
49
            $excludeNonConfirmed = true;
50
            if ($this->barrierTest('allowNonConfirmed', $currentUser)) {
51
                $excludeNonConfirmed = WebRequest::getBoolean('excludeNonConfirmed');
52
            }
53
54
            $formParameters = [
55
                'term' => $searchTerm,
56
                'type' => $searchType,
57
            ];
58
59
            if ($excludeNonConfirmed) {
60
                $formParameters['excludeNonConfirmed'] = true;
61
            }
62
63
            // FIXME: domains
64
            $requestSearch = RequestSearchHelper::get($database, 1);
65
            $this->setSearchHelper($requestSearch);
66
            $this->setupLimits();
67
68
            $validationError = "";
69
            if (!$this->validateSearchParameters($searchType, $searchTerm, $validationError)) {
70
                SessionAlert::error($validationError, "Search error");
71
72
                $this->setupPageData(0, $formParameters);
73
                $this->assign('hasResultset', false);
74
75
                return;
76
            }
77
78
            // searchType known to be sane from the validate step above
79
            if (!$this->barrierTest('by' . ucfirst($searchType), User::getCurrent($this->getDatabase()))) {
0 ignored issues
show
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

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

90
                    $this->getNameSearchResults($requestSearch, /** @scrutinizer ignore-type */ $searchTerm);
Loading history...
91
                    break;
92
                case 'email':
93
                    $this->getEmailSearchResults($requestSearch, $searchTerm);
0 ignored issues
show
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

93
                    $this->getEmailSearchResults($requestSearch, /** @scrutinizer ignore-type */ $searchTerm);
Loading history...
94
                    break;
95
                case 'ip':
96
                    $this->getIpSearchResults($requestSearch, $searchTerm);
0 ignored issues
show
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

96
                    $this->getIpSearchResults($requestSearch, /** @scrutinizer ignore-type */ $searchTerm);
Loading history...
97
                    break;
98
                case 'comment':
99
                    $this->getCommentSearchResults($requestSearch, $searchTerm);
0 ignored issues
show
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

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