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