1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Xhgui_Db_Mapper |
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Convert request data keys into mongo values. |
8
|
|
|
* |
9
|
|
|
* @param array $options |
10
|
|
|
* @return array |
11
|
|
|
*/ |
12
|
|
|
public function convert($options) |
13
|
|
|
{ |
14
|
|
|
$result = array( |
15
|
|
|
'conditions' => array(), |
16
|
|
|
'sort' => null, |
17
|
|
|
'direction' => null, |
18
|
|
|
'perPage' => 25 |
19
|
|
|
); |
20
|
|
|
if (isset($options['conditions'])) { |
21
|
|
|
$result['conditions'] = $this->_conditions($options['conditions']); |
22
|
|
|
} |
23
|
|
|
$result['direction'] = $this->_direction($options); |
24
|
|
|
$result['sort'] = $this->_sort($options); |
25
|
|
|
|
26
|
|
|
if (isset($options['perPage'])) { |
27
|
|
|
$result['perPage'] = $options['perPage']; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $result; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Convert the search parameters into the matching fields. |
35
|
|
|
* |
36
|
|
|
* Keeps the schema details out of the GET parameters. |
37
|
|
|
* String casts are uses to prevent mongo operator injection. |
38
|
|
|
* |
39
|
|
|
* @param array $search |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
protected function _conditions($search) |
43
|
|
|
{ |
44
|
|
View Code Duplication |
if (!empty($search['limit_custom']) && $search['limit_custom'][0] == "P") { |
|
|
|
|
45
|
|
|
$search['limit'] = $search['limit_custom']; |
46
|
|
|
} |
47
|
|
|
$hasLimit = (!empty($search['limit']) && $search['limit'] != -1); |
48
|
|
|
|
49
|
|
|
$conditions = array(); |
50
|
|
View Code Duplication |
if (!empty($search['date_start']) && !$hasLimit) { |
|
|
|
|
51
|
|
|
$conditions['meta.request_date']['$gte'] = (string)$search['date_start']; |
52
|
|
|
} |
53
|
|
View Code Duplication |
if (!empty($search['date_end']) && !$hasLimit) { |
|
|
|
|
54
|
|
|
$conditions['meta.request_date']['$lte'] = (string)$search['date_end']; |
55
|
|
|
} |
56
|
|
|
if (isset($search['simple_url'])) { |
57
|
|
|
$conditions['meta.simple_url'] = (string)$search['simple_url']; |
58
|
|
|
} |
59
|
|
View Code Duplication |
if (!empty($search['request_start'])) { |
|
|
|
|
60
|
|
|
$conditions['meta.SERVER.REQUEST_TIME']['$gte'] = $this->_convertDate($search['request_start']); |
61
|
|
|
} |
62
|
|
View Code Duplication |
if (!empty($search['request_end'])) { |
|
|
|
|
63
|
|
|
$conditions['meta.SERVER.REQUEST_TIME']['$lte'] = $this->_convertDate($search['request_end']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!empty($search['remote_addr'])) { |
67
|
|
|
$conditions['meta.SERVER.REMOTE_ADDR'] = (string)$search['remote_addr']; |
68
|
|
|
} |
69
|
|
|
if (isset($search['cookie'])) { |
70
|
|
|
$conditions['meta.SERVER.HTTP_COOKIE'] = (string)$search['cookie']; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($hasLimit && $search['limit'][0] == "P") { |
74
|
|
|
$date = new DateTime(); |
75
|
|
|
try { |
76
|
|
|
$date->sub(new DateInterval($search['limit'])); |
77
|
|
|
$conditions['meta.request_ts']['$gte'] = new MongoDate($date->getTimestamp()); |
78
|
|
|
} catch (\Exception $e) { |
79
|
|
|
// Match a day in the future so we match nothing, as it's likely an invalid format |
80
|
|
|
$conditions['meta.request_ts']['$gte'] = new MongoDate(time() + 86400); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (isset($search['url'])) { |
85
|
|
|
// Not sure if letting people use regex here |
86
|
|
|
// is a good idea. Only one way to find out. |
87
|
|
|
$conditions['meta.url'] = array( |
88
|
|
|
'$regex' => (string)$search['url'], |
89
|
|
|
'$options' => 'i', |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $conditions; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function _convertDate($dateString) |
97
|
|
|
{ |
98
|
|
|
if (is_numeric($dateString)) { |
99
|
|
|
return (float) $dateString; |
100
|
|
|
} |
101
|
|
|
$date = DateTime::createFromFormat('Y-m-d H:i:s', $dateString); |
102
|
|
|
if (!$date) { |
103
|
|
|
return $date; |
104
|
|
|
} |
105
|
|
|
return $date->getTimestamp(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function _direction($options) |
109
|
|
|
{ |
110
|
|
|
if (empty($options['direction'])) { |
111
|
|
|
return 'desc'; |
112
|
|
|
} |
113
|
|
|
$valid = array('desc', 'asc'); |
114
|
|
|
if (in_array($options['direction'], $valid, true)) { |
115
|
|
|
return $options['direction']; |
116
|
|
|
} |
117
|
|
|
return 'desc'; |
118
|
|
|
} |
119
|
|
|
/** |
120
|
|
|
* Get sort options for a paginated set. |
121
|
|
|
* |
122
|
|
|
* Whitelists to valid known keys. |
123
|
|
|
* |
124
|
|
|
* @param array $options Pagination options including the sort key. |
125
|
|
|
* @return array Sort field & direction. |
126
|
|
|
*/ |
127
|
|
|
protected function _sort($options) |
128
|
|
|
{ |
129
|
|
|
$direction = -1; |
130
|
|
|
if (isset($options['direction']) && $options['direction'] === 'asc') { |
131
|
|
|
$direction = 1; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$valid = array('time', 'wt', 'mu', 'cpu'); |
135
|
|
|
if ( |
136
|
|
|
empty($options['sort']) || |
137
|
|
|
(isset($options['sort']) && !in_array($options['sort'], $valid)) |
138
|
|
|
) { |
139
|
|
|
return array('meta.SERVER.REQUEST_TIME' => $direction); |
140
|
|
|
} |
141
|
|
|
if ($options['sort'] == 'time') { |
142
|
|
|
return array('meta.SERVER.REQUEST_TIME' => $direction); |
143
|
|
|
} elseif ($options['sort'] == 'wt') { |
144
|
|
|
return array('profile.main().wt' => $direction); |
145
|
|
|
} elseif ($options['sort'] == 'mu') { |
146
|
|
|
return array('profile.main().mu' => $direction); |
147
|
|
|
} elseif ($options['sort'] == 'cpu') { |
148
|
|
|
return array('profile.main().cpu' => $direction); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
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.