1414use function __ ;
1515use function array_intersect ;
1616use function array_key_exists ;
17- use function count ;
1817use function explode ;
19- use function htmlspecialchars ;
2018use function implode ;
21- use function intval ;
2219use function is_array ;
2320use function is_string ;
24- use function strlen ;
2521
2622/**
2723 * Class to handle database search
@@ -73,7 +69,7 @@ class Search
7369 /**
7470 * Criteria Tables to search in
7571 *
76- * @var array
72+ * @var string[]
7773 */
7874 private $ criteriaTables ;
7975
@@ -145,9 +141,9 @@ private function setSearchParams(): void
145141 }
146142
147143 if (empty ($ _POST ['criteriaColumnName ' ]) || ! is_string ($ _POST ['criteriaColumnName ' ])) {
148- unset( $ this ->criteriaColumnName ) ;
144+ $ this ->criteriaColumnName = '' ;
149145 } else {
150- $ this ->criteriaColumnName = $ this -> dbi -> escapeString ( $ _POST ['criteriaColumnName ' ]) ;
146+ $ this ->criteriaColumnName = $ _POST ['criteriaColumnName ' ];
151147 }
152148 }
153149
@@ -156,35 +152,25 @@ private function setSearchParams(): void
156152 *
157153 * @param string $table The table name
158154 *
159- * @return array 3 SQL queries (for count, display and delete results)
155+ * @return string[] 3 SQL queries (for count, display and delete results)
160156 *
161157 * @todo can we make use of fulltextsearch IN BOOLEAN MODE for this?
162- * PMA_backquote
163- * DatabaseInterface::fetchAssoc
164- * $GLOBALS['db']
165- * explode
166- * count
167- * strlen
168158 */
169- private function getSearchSqls ($ table )
159+ private function getSearchSqls (string $ table ): array
170160 {
171161 // Statement types
172162 $ sqlstr_select = 'SELECT ' ;
173163 $ sqlstr_delete = 'DELETE ' ;
174164 // Table to use
175- $ sqlstr_from = ' FROM '
176- . Util::backquote ($ GLOBALS ['db ' ]) . '. '
177- . Util::backquote ($ table );
165+ $ sqlstr_from = ' FROM ' . Util::backquote ($ GLOBALS ['db ' ]) . '. ' . Util::backquote ($ table );
178166 // Gets where clause for the query
179167 $ where_clause = $ this ->getWhereClause ($ table );
180168 // Builds complete queries
181169 $ sql = [];
182- $ sql ['select_columns ' ] = $ sqlstr_select . ' * ' . $ sqlstr_from
183- . $ where_clause ;
170+ $ sql ['select_columns ' ] = $ sqlstr_select . ' * ' . $ sqlstr_from . $ where_clause ;
184171 // here, I think we need to still use the COUNT clause, even for
185172 // VIEWs, anyway we have a WHERE clause that should limit results
186- $ sql ['select_count ' ] = $ sqlstr_select . ' COUNT(*) AS `count` '
187- . $ sqlstr_from . $ where_clause ;
173+ $ sql ['select_count ' ] = $ sqlstr_select . ' COUNT(*) AS `count` ' . $ sqlstr_from . $ where_clause ;
188174 $ sql ['delete ' ] = $ sqlstr_delete . $ sqlstr_from . $ where_clause ;
189175
190176 return $ sql ;
@@ -197,7 +183,7 @@ private function getSearchSqls($table)
197183 *
198184 * @return string The generated where clause
199185 */
200- private function getWhereClause ($ table )
186+ private function getWhereClause (string $ table ): string
201187 {
202188 // Columns to select
203189 $ allColumns = $ this ->dbi ->getColumns ($ GLOBALS ['db ' ], $ table );
@@ -208,65 +194,57 @@ private function getWhereClause($table)
208194 // For "as regular expression" (search option 5), LIKE won't be used
209195 // Usage example: If user is searching for a literal $ in a regexp search,
210196 // they should enter \$ as the value.
211- $ criteriaSearchStringEscaped = $ this ->dbi ->escapeString ($ this ->criteriaSearchString );
212197 // Extract search words or pattern
213198 $ search_words = $ this ->criteriaSearchType > 2
214- ? [$ criteriaSearchStringEscaped ]
215- : explode (' ' , $ criteriaSearchStringEscaped );
199+ ? [$ this -> criteriaSearchString ]
200+ : explode (' ' , $ this -> criteriaSearchString );
216201
217202 foreach ($ search_words as $ search_word ) {
218203 // Eliminates empty values
219- if (strlen ( $ search_word) === 0 ) {
204+ if ($ search_word === '' ) {
220205 continue ;
221206 }
222207
223208 $ likeClausesPerColumn = [];
224209 // for each column in the table
225210 foreach ($ allColumns as $ column ) {
226211 if (
227- isset ($ this ->criteriaColumnName )
228- && strlen ($ this ->criteriaColumnName ) !== 0
212+ $ this ->criteriaColumnName !== ''
229213 && $ column ['Field ' ] != $ this ->criteriaColumnName
230214 ) {
231215 continue ;
232216 }
233217
234- $ column = 'CONVERT( ' . Util::backquote ($ column ['Field ' ])
235- . ' USING utf8) ' ;
218+ $ column = 'CONVERT( ' . Util::backquote ($ column ['Field ' ]) . ' USING utf8) ' ;
236219 $ likeClausesPerColumn [] = $ column . ' ' . $ like_or_regex . ' '
237- . "' "
238- . $ automatic_wildcard . $ search_word . $ automatic_wildcard
239- . "' " ;
220+ . $ this ->dbi ->quoteString ($ automatic_wildcard . $ search_word . $ automatic_wildcard );
240221 }
241222
242- if (count ( $ likeClausesPerColumn) <= 0 ) {
223+ if ($ likeClausesPerColumn === [] ) {
243224 continue ;
244225 }
245226
246227 $ likeClauses [] = implode (' OR ' , $ likeClausesPerColumn );
247228 }
248229
249- // Use 'OR' if 'at least one word' is to be searched, else use 'AND'
250- $ implode_str = ($ this ->criteriaSearchType == 1 ? ' OR ' : ' AND ' );
251- if (empty ($ likeClauses )) {
230+ if ($ likeClauses === []) {
252231 // this could happen when the "inside column" does not exist
253232 // in any selected tables
254- $ where_clause = ' WHERE FALSE ' ;
255- } else {
256- $ where_clause = ' WHERE ( '
257- . implode (') ' . $ implode_str . ' ( ' , $ likeClauses )
258- . ') ' ;
233+ return ' WHERE FALSE ' ;
259234 }
260235
261- return $ where_clause ;
236+ // Use 'OR' if 'at least one word' is to be searched, else use 'AND'
237+ $ implode_str = ($ this ->criteriaSearchType == 1 ? ' OR ' : ' AND ' );
238+
239+ return ' WHERE ( ' . implode (') ' . $ implode_str . ' ( ' , $ likeClauses ) . ') ' ;
262240 }
263241
264242 /**
265243 * Displays database search results
266244 *
267245 * @return string HTML for search results
268246 */
269- public function getSearchResults ()
247+ public function getSearchResults (): string
270248 {
271249 $ resultTotal = 0 ;
272250 $ rows = [];
@@ -275,13 +253,11 @@ public function getSearchResults()
275253 // Gets the SQL statements
276254 $ newSearchSqls = $ this ->getSearchSqls ($ eachTable );
277255 // Executes the "COUNT" statement
278- $ resultCount = intval ($ this ->dbi ->fetchValue (
279- $ newSearchSqls ['select_count ' ]
280- ));
256+ $ resultCount = (int ) $ this ->dbi ->fetchValue ($ newSearchSqls ['select_count ' ]);
281257 $ resultTotal += $ resultCount ;
282258 // Gets the result row's HTML for a table
283259 $ rows [] = [
284- 'table ' => htmlspecialchars ( $ eachTable) ,
260+ 'table ' => $ eachTable ,
285261 'new_search_sqls ' => $ newSearchSqls ,
286262 'result_count ' => $ resultCount ,
287263 ];
@@ -292,7 +268,7 @@ public function getSearchResults()
292268 'rows ' => $ rows ,
293269 'result_total ' => $ resultTotal ,
294270 'criteria_tables ' => $ this ->criteriaTables ,
295- 'criteria_search_string ' => htmlspecialchars ( $ this ->criteriaSearchString ) ,
271+ 'criteria_search_string ' => $ this ->criteriaSearchString ,
296272 'search_type_description ' => $ this ->searchTypeDescription ,
297273 ]);
298274 }
@@ -310,7 +286,7 @@ public function getMainHtml()
310286 'criteria_search_type ' => $ this ->criteriaSearchType ,
311287 'criteria_tables ' => $ this ->criteriaTables ,
312288 'tables_names_only ' => $ this ->tablesNamesOnly ,
313- 'criteria_column_name ' => $ this ->criteriaColumnName ?? null ,
289+ 'criteria_column_name ' => $ this ->criteriaColumnName ,
314290 ]);
315291 }
316292}
0 commit comments