This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace XHGui\Db; |
||
4 | |||
5 | use DateInterval; |
||
6 | use DateTime; |
||
7 | use MongoDate; |
||
8 | use XHGui\Searcher\SearcherInterface; |
||
9 | |||
10 | class Mapper |
||
11 | { |
||
12 | /** |
||
13 | * Convert request data keys into mongo values. |
||
14 | */ |
||
15 | public function convert(array $options): array |
||
16 | { |
||
17 | return [ |
||
18 | 'conditions' => $this->buildConditions($options['conditions'] ?? []), |
||
19 | 'sort' => $this->buildSort($options), |
||
20 | 'direction' => $this->buildDirection($options), |
||
21 | 'perPage' => $options['perPage'] ?? SearcherInterface::DEFAULT_PER_PAGE, |
||
22 | ]; |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * Convert the search parameters into the matching fields. |
||
27 | * |
||
28 | * Keeps the schema details out of the GET parameters. |
||
29 | * String casts are uses to prevent mongo operator injection. |
||
30 | */ |
||
31 | private function buildConditions(array $search): array |
||
32 | { |
||
33 | if (!$search) { |
||
0 ignored issues
–
show
|
|||
34 | return []; |
||
35 | } |
||
36 | |||
37 | View Code Duplication | if (!empty($search['limit_custom']) && $search['limit_custom'][0] === 'P') { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
38 | $search['limit'] = $search['limit_custom']; |
||
39 | } |
||
40 | $hasLimit = (!empty($search['limit']) && $search['limit'] != -1); |
||
41 | |||
42 | $conditions = []; |
||
43 | View Code Duplication | if (!empty($search['date_start']) && !$hasLimit) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
44 | $conditions['meta.request_date']['$gte'] = (string)$search['date_start']; |
||
45 | } |
||
46 | View Code Duplication | if (!empty($search['date_end']) && !$hasLimit) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
47 | $conditions['meta.request_date']['$lte'] = (string)$search['date_end']; |
||
48 | } |
||
49 | if (isset($search['simple_url'])) { |
||
50 | $conditions['meta.simple_url'] = (string)$search['simple_url']; |
||
51 | } |
||
52 | View Code Duplication | if (!empty($search['request_start'])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
53 | $conditions['meta.SERVER.REQUEST_TIME']['$gte'] = $this->convertDate($search['request_start']); |
||
54 | } |
||
55 | View Code Duplication | if (!empty($search['request_end'])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
56 | $conditions['meta.SERVER.REQUEST_TIME']['$lte'] = $this->convertDate($search['request_end']); |
||
57 | } |
||
58 | |||
59 | if (!empty($search['remote_addr'])) { |
||
60 | $conditions['meta.SERVER.REMOTE_ADDR'] = (string)$search['remote_addr']; |
||
61 | } |
||
62 | if (isset($search['cookie'])) { |
||
63 | $conditions['meta.SERVER.HTTP_COOKIE'] = (string)$search['cookie']; |
||
64 | } |
||
65 | |||
66 | if ($hasLimit && $search['limit'][0] === 'P') { |
||
67 | $date = new DateTime(); |
||
68 | try { |
||
69 | $date->sub(new DateInterval($search['limit'])); |
||
70 | $conditions['meta.request_ts']['$gte'] = new MongoDate($date->getTimestamp()); |
||
71 | } catch (\Exception $e) { |
||
72 | // Match a day in the future so we match nothing, as it's likely an invalid format |
||
73 | $conditions['meta.request_ts']['$gte'] = new MongoDate(time() + 86400); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | if (isset($search['url'])) { |
||
78 | // Not sure if letting people use regex here |
||
79 | // is a good idea. Only one way to find out. |
||
80 | $conditions['meta.url'] = [ |
||
81 | '$regex' => (string)$search['url'], |
||
82 | '$options' => 'i', |
||
83 | ]; |
||
84 | } |
||
85 | |||
86 | return $conditions; |
||
87 | } |
||
88 | |||
89 | private function convertDate($dateString) |
||
90 | { |
||
91 | if (is_numeric($dateString)) { |
||
92 | return (float) $dateString; |
||
93 | } |
||
94 | $date = DateTime::createFromFormat('Y-m-d H:i:s', $dateString); |
||
95 | if (!$date) { |
||
96 | return $date; |
||
97 | } |
||
98 | |||
99 | return $date->getTimestamp(); |
||
100 | } |
||
101 | |||
102 | private function buildDirection(array $options): string |
||
103 | { |
||
104 | if (empty($options['direction'])) { |
||
105 | return SearcherInterface::DEFAULT_DIRECTION; |
||
106 | } |
||
107 | $valid = ['desc', 'asc']; |
||
108 | if (in_array($options['direction'], $valid, true)) { |
||
109 | return $options['direction']; |
||
110 | } |
||
111 | |||
112 | return 'desc'; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Get sort options for a paginated set. |
||
117 | * |
||
118 | * Whitelists to valid known keys. |
||
119 | * |
||
120 | * @param array $options pagination options including the sort key |
||
121 | * @return array sort field & direction |
||
122 | */ |
||
123 | private function buildSort(array $options): array |
||
124 | { |
||
125 | $direction = -1; |
||
126 | if (isset($options['direction']) && $options['direction'] === 'asc') { |
||
127 | $direction = 1; |
||
128 | } |
||
129 | |||
130 | $valid = ['time', 'wt', 'mu', 'cpu']; |
||
131 | if ( |
||
132 | empty($options['sort']) || |
||
133 | (isset($options['sort']) && !in_array($options['sort'], $valid)) |
||
134 | ) { |
||
135 | return ['meta.SERVER.REQUEST_TIME' => $direction]; |
||
136 | } |
||
137 | if ($options['sort'] === 'time') { |
||
138 | return ['meta.SERVER.REQUEST_TIME' => $direction]; |
||
139 | } elseif ($options['sort'] === 'wt') { |
||
140 | return ['profile.main().wt' => $direction]; |
||
141 | } elseif ($options['sort'] === 'mu') { |
||
142 | return ['profile.main().mu' => $direction]; |
||
143 | } elseif ($options['sort'] === 'cpu') { |
||
144 | return ['profile.main().cpu' => $direction]; |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.