1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* zKillboard |
4
|
|
|
* Copyright (C) 2012-2015 EVE-KILL Team and EVSCO. |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU Affero General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU Affero General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
class Filters |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param array $tables |
24
|
|
|
* @param array $combined |
25
|
|
|
* @param array $whereClauses |
26
|
|
|
* @param array $parameters |
27
|
|
|
* @param bool $allTime |
28
|
|
|
* @throws Exception |
29
|
|
|
*/ |
30
|
|
|
public static function buildFilters(&$tables, &$combined, &$whereClauses, &$parameters, $allTime = true) |
31
|
|
|
{ |
32
|
|
|
$year = null; |
33
|
|
|
$month = null; |
34
|
|
|
$week = null; |
35
|
|
|
// zz_participants filters |
36
|
|
|
$participants = "zz_participants p"; |
37
|
|
|
$filterColumns = array("allianceID", "characterID", "corporationID", "factionID", "shipTypeID", "groupID", "solarSystemID", "regionID"); |
38
|
|
|
foreach ($filterColumns as $filterColumn) { |
39
|
|
|
self::buildWhere($tables, $combined, $participants, $filterColumn, $parameters); |
40
|
|
|
self::buildWhere($tables, $combined, $participants, "!$filterColumn", $parameters); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (array_key_exists("year", $parameters)) $year = (int)$parameters["year"]; // Optional |
44
|
|
|
if (array_key_exists("week", $parameters)) $week = (int)$parameters["week"]; // Optional |
45
|
|
|
if (array_key_exists("month", $parameters)) $month = (int)$parameters["month"]; // Optional |
46
|
|
|
if (!array_key_exists("pastSeconds", $parameters) && $allTime == false && (!isset($year) || !isset($week))) { |
|
|
|
|
47
|
|
|
$year = array_key_exists("year", $parameters) ? (int)$parameters["year"] : date("Y"); |
48
|
|
|
$week = array_key_exists("week", $parameters) ? (int)$parameters["week"] : date("W"); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (array_key_exists("killID", $parameters)) { |
52
|
|
|
$tables[] = "zz_participants p"; |
53
|
|
|
$whereClauses[] = "p.killID = " . ((int) $parameters["killID"]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (array_key_exists("api-only", $parameters)) { |
57
|
|
|
$tables[] = "zz_participants p"; |
58
|
|
|
$whereClauses[] = "p.killID > 0"; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (array_key_exists("solo", $parameters) && $parameters["solo"] === true) { |
62
|
|
|
$tables[] = "zz_participants p"; |
63
|
|
|
$whereClauses[] = "p.number_involved = 1"; |
64
|
|
|
$whereClauses[] = "p.vGroupID not in (237, 29, 31)"; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (array_key_exists("relatedTime", $parameters)) { |
68
|
|
|
$relatedTime = $parameters["relatedTime"]; |
69
|
|
|
$unixTime = strtotime($relatedTime); |
70
|
|
|
if ($unixTime % 3600 != 0) throw new Exception("User attempted an unsupported value. Fail."); |
71
|
|
|
$tables[] = "zz_participants p"; |
72
|
|
|
$hourModifier = 1; |
73
|
|
|
if (array_key_exists("exHours", $parameters)) { |
74
|
|
|
$exHours = (int)$parameters["exHours"]; |
75
|
|
|
if ($exHours > 1 && $exHours <= 12) $hourModifier = $exHours; |
76
|
|
|
} |
77
|
|
|
$whereClauses[] = "p.dttm >= '" . date("Y-m-d H:i:00", $unixTime - ($hourModifier * 3600)) . "'"; |
78
|
|
|
$whereClauses[] = "p.dttm <= '" . date("Y-m-d H:i:00", $unixTime + ($hourModifier * 3600)) . "'"; |
79
|
|
|
$parameters["limit"] = 10000; |
80
|
|
|
} |
81
|
|
|
if (array_key_exists("startTime", $parameters)) { |
82
|
|
|
$time = $parameters["startTime"]; |
83
|
|
|
$unixTime = strtotime($time); |
84
|
|
|
$tables[] = "zz_participants p"; |
85
|
|
|
$whereClauses[] = "p.dttm >= '" . date("Y-m-d H:i:s", (int)$unixTime) . "'"; |
86
|
|
|
} |
87
|
|
|
if (array_key_exists("endTime", $parameters)) { |
88
|
|
|
$time = $parameters["endTime"]; |
89
|
|
|
$unixTime = strtotime($time); |
90
|
|
|
$tables[] = "zz_participants p"; |
91
|
|
|
$whereClauses[] = "p.dttm <= '" . date("Y-m-d H:i:s", (int)$unixTime) . "'"; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (array_key_exists("pastSeconds", $parameters)) { |
95
|
|
|
$tables[] = "zz_participants p"; |
96
|
|
|
$whereClauses[] = "p.dttm >= date_sub(now(), interval " . ((int)$parameters["pastSeconds"]) . " second)"; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (array_key_exists("iskValue", $parameters)) { |
100
|
|
|
$tables[] = "zz_participants p"; |
101
|
|
|
$whereClauses[] = "p.total_price >= '" . ((int)$parameters["iskValue"]) . "'"; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (array_key_exists("w-space", $parameters)) { |
105
|
|
|
$tables[] = "zz_participants p"; |
106
|
|
|
$whereClauses[] = "(regionID >= '11000001' and regionID <= '11000033')"; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (array_key_exists("lowsec", $parameters)) { |
110
|
|
|
$regions = array(); |
111
|
|
|
$rows = Db::query("select distinct(regionID) as regionID from ccp_systems where security >= 0 and security <= 0.45", array(), 3600); |
112
|
|
|
foreach($rows as $row) $regions[] = $row["regionID"]; |
113
|
|
|
$tables[] = "zz_participants p"; |
114
|
|
|
$whereClauses[] = " regionID in (" . implode(",", $regions) . ")"; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (array_key_exists("highsec", $parameters)) { |
118
|
|
|
$regions = array(); |
119
|
|
|
$rows = Db::query("select distinct(regionID) as regionID from ccp_systems where security > 0.45", array(), 3600); |
120
|
|
|
foreach($rows as $row) $regions[] = $row["regionID"]; |
121
|
|
|
$tables[] = "zz_participants p"; |
122
|
|
|
$whereClauses[] = " regionID in (" . implode(",", $regions) . ")"; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (array_key_exists("nullsec", $parameters)) { |
126
|
|
|
$regions = array(); |
127
|
|
|
$rows = Db::query("select distinct(regionID) as regionID from ccp_systems where security < 0 and (regionID < 11000001 or regionID > 11000030)", array(), 3600); |
128
|
|
|
foreach($rows as $row) $regions[] = $row["regionID"]; |
129
|
|
|
$tables[] = "zz_participants p"; |
130
|
|
|
$whereClauses[] = " regionID in (" . implode(",", $regions) . ")"; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (array_key_exists("beforeKillID", $parameters)) { |
134
|
|
|
$killID = (int)$parameters["beforeKillID"]; |
135
|
|
|
$tables[] = "zz_participants p"; |
136
|
|
|
$whereClauses[] = "p.killID < $killID"; |
137
|
|
|
//$killdttm = Db::queryField("select dttm from zz_participants where killID = :killID limit 1", "dttm", array(":killID" => $killID)); |
138
|
|
|
//$whereClauses[] = "p.dttm <= '$killdttm'"; |
139
|
|
|
} |
140
|
|
|
if (array_key_exists("afterKillID", $parameters)) { |
141
|
|
|
$killID = (int)$parameters["afterKillID"]; |
142
|
|
|
$tables[] = "zz_participants p"; |
143
|
|
|
$whereClauses[] = "p.killID > $killID"; |
144
|
|
|
//$killdttm = Db::queryField("select dttm from zz_participants where killID = :killID limit 1", "dttm", array(":killID" => $killID)); |
145
|
|
|
//$whereClauses[] = "p.dttm >= '$killdttm'"; |
146
|
|
|
} |
147
|
|
|
if (array_key_exists("war", $parameters) || array_key_exists("warID", $parameters)) { |
148
|
|
|
$warID = isset($parameters["war"]) ? (int)$parameters["war"] : (int)$parameters["warID"]; |
149
|
|
|
$tables[] = "zz_participants p"; |
150
|
|
|
$tables[] = "zz_warmails w"; |
151
|
|
|
$whereClauses[] = "w.warID = $warID"; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$kills = array_key_exists("kills", $parameters); |
155
|
|
|
$losses = array_key_exists("losses", $parameters); //|| (array_key_exists("solo", $parameters)); |
156
|
|
|
if ((array_key_exists("mixed", $parameters) && $parameters["mixed"] == true) || array_key_exists("iskValue", $parameters)) { |
|
|
|
|
157
|
|
|
} else if ($losses) { |
158
|
|
|
$tables[] = $participants; |
159
|
|
|
$whereClauses[] = "p.isVictim = '1'"; |
160
|
|
|
} else if ($kills) { |
161
|
|
|
$tables[] = $participants; |
162
|
|
|
$whereClauses[] = "p.isVictim = '0'"; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$tables = array_unique($tables); |
166
|
|
|
if (sizeof($tables) == 0) $tables[] = "zz_participants p"; |
167
|
|
|
foreach ($tables as $table) { |
168
|
|
|
$tablePrefix = substr($table, strlen($table) - 1, 1); |
169
|
|
|
if (isset($year)) { |
170
|
|
|
$whereClauses[] = "{$tablePrefix}.dttm >= '$year-01-01 00:00:00'"; |
171
|
|
|
$whereClauses[] = "{$tablePrefix}.dttm <= '$year-12-31 23:59:59'"; |
172
|
|
|
} |
173
|
|
|
if (isset($week)) { |
174
|
|
|
if (!isset($year)) throw new Exception("Must include a year when setting week!"); |
175
|
|
|
$weekStart = date("Y-m-d H:i:00", strtotime("{$year}W{$week}")); |
176
|
|
|
$whereClauses[] = "{$tablePrefix}.dttm >= '$weekStart'"; |
177
|
|
|
$whereClauses[] = "{$tablePrefix}.dttm <= date_add('$weekStart', interval 7 day)"; |
178
|
|
|
} |
179
|
|
|
if (isset($month)) { |
180
|
|
|
if (!isset($year)) throw new Exception("Must include a year when setting month!"); |
181
|
|
|
$whereClauses[] = "{$tablePrefix}.dttm >= '$year-$month-01 00:00:00'"; |
182
|
|
|
$whereClauses[] = "{$tablePrefix}.dttm <= '$year-$month-31 23:59:59'"; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param array $tables |
189
|
|
|
* @param array $whereClauses |
190
|
|
|
* @param string $table |
191
|
|
|
* @param string $column |
192
|
|
|
* @param array $parameters |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
private static function buildWhere(&$tables, &$whereClauses, $table, $column, $parameters) |
196
|
|
|
{ |
197
|
|
|
$array = self::grabParameters($parameters, $column); |
198
|
|
|
if ($array === null || !is_array($array) || sizeof($array) == 0) return ""; |
199
|
|
|
// Ensure SQL safe parameters |
200
|
|
|
$cleanArray = array(); |
201
|
|
|
foreach ($array as $value) $cleanArray[] = "'" . (int)$value . "'"; |
202
|
|
|
$tables[] = $table; |
203
|
|
|
$not = ""; |
204
|
|
|
if (Util::startsWith($column, "!")) { |
205
|
|
|
$not = " not "; |
206
|
|
|
$column = substr($column, 1); |
207
|
|
|
} |
208
|
|
|
if ($column == "groupID") { |
209
|
|
|
//$whereClauses[] = "(p.$column $not in (" . implode(",", $cleanArray) . ") or p.vGroupID $not in (" . implode(",", $cleanArray) . "))"; |
210
|
|
|
$whereClauses[] = "(p.vGroupID $not in (" . implode(",", $cleanArray) . "))"; |
211
|
|
|
} else $whereClauses[] = "p.$column $not in (" . implode(",", $cleanArray) . ")"; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param null|array $parameters |
216
|
|
|
* @param string $name |
217
|
|
|
* @return null|array; |
|
|
|
|
218
|
|
|
**/ |
219
|
|
|
public static function grabParameters($parameters, $name) |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
$retValue = isset($parameters[$name]) ? $parameters[$name] : null; |
222
|
|
|
if ($retValue === null) return $retValue; |
223
|
|
|
if (!is_array($retValue)) $retValue = array($retValue); |
224
|
|
|
return $retValue; |
225
|
|
|
} |
226
|
|
|
} |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.