@@ -30,26 +30,32 @@ discard block |
||
| 30 | 30 | # It's much easier to grasp their meaning this way. |
| 31 | 31 | |
| 32 | 32 | $tmp = $request->get_parameter('bbox'); |
| 33 | - if (!$tmp) |
|
| 34 | - throw new ParamMissing('bbox'); |
|
| 33 | + if (!$tmp) { |
|
| 34 | + throw new ParamMissing('bbox'); |
|
| 35 | + } |
|
| 35 | 36 | $parts = explode('|', $tmp); |
| 36 | - if (count($parts) != 4) |
|
| 37 | - throw new InvalidParam('bbox', "Expecting 4 pipe-separated parts, got ".count($parts)."."); |
|
| 38 | - foreach ($parts as &$part_ref) |
|
| 39 | - { |
|
| 40 | - if (!preg_match("/^-?[0-9]+(\.?[0-9]*)$/", $part_ref)) |
|
| 41 | - throw new InvalidParam('bbox', "'$part_ref' is not a valid float number."); |
|
| 37 | + if (count($parts) != 4) { |
|
| 38 | + throw new InvalidParam('bbox', "Expecting 4 pipe-separated parts, got ".count($parts)."."); |
|
| 39 | + } |
|
| 40 | + foreach ($parts as &$part_ref) { |
|
| 41 | + if (!preg_match("/^-?[0-9]+(\.?[0-9]*)$/", $part_ref)) { |
|
| 42 | + throw new InvalidParam('bbox', "'$part_ref' is not a valid float number."); |
|
| 43 | + } |
|
| 42 | 44 | $part_ref = floatval($part_ref); |
| 43 | 45 | } |
| 44 | 46 | list($bbsouth, $bbwest, $bbnorth, $bbeast) = $parts; |
| 45 | - if ($bbnorth <= $bbsouth) |
|
| 46 | - throw new InvalidParam('bbox', "Northern edge must be situated to the north of the southern edge."); |
|
| 47 | - if ($bbeast == $bbwest) |
|
| 48 | - throw new InvalidParam('bbox', "Eastern edge longitude is the same as the western one."); |
|
| 49 | - if ($bbnorth > 90 || $bbnorth < -90 || $bbsouth > 90 || $bbsouth < -90) |
|
| 50 | - throw new InvalidParam('bbox', "Latitudes have to be within -90..90 range."); |
|
| 51 | - if ($bbeast > 180 || $bbeast < -180 || $bbwest > 180 || $bbwest < -180) |
|
| 52 | - throw new InvalidParam('bbox', "Longitudes have to be within -180..180 range."); |
|
| 47 | + if ($bbnorth <= $bbsouth) { |
|
| 48 | + throw new InvalidParam('bbox', "Northern edge must be situated to the north of the southern edge."); |
|
| 49 | + } |
|
| 50 | + if ($bbeast == $bbwest) { |
|
| 51 | + throw new InvalidParam('bbox', "Eastern edge longitude is the same as the western one."); |
|
| 52 | + } |
|
| 53 | + if ($bbnorth > 90 || $bbnorth < -90 || $bbsouth > 90 || $bbsouth < -90) { |
|
| 54 | + throw new InvalidParam('bbox', "Latitudes have to be within -90..90 range."); |
|
| 55 | + } |
|
| 56 | + if ($bbeast > 180 || $bbeast < -180 || $bbwest > 180 || $bbwest < -180) { |
|
| 57 | + throw new InvalidParam('bbox', "Longitudes have to be within -180..180 range."); |
|
| 58 | + } |
|
| 53 | 59 | |
| 54 | 60 | # Construct SQL conditions for the specified bounding box. |
| 55 | 61 | |
@@ -64,16 +70,13 @@ discard block |
||
| 64 | 70 | $lat >= '".Db::escape_string($bbsouth)."' |
| 65 | 71 | and $lat < '".Db::escape_string($bbnorth)."' |
| 66 | 72 | )"; |
| 67 | - if ($bbeast > $bbwest) |
|
| 68 | - { |
|
| 73 | + if ($bbeast > $bbwest) { |
|
| 69 | 74 | # Easy one. |
| 70 | 75 | $where_conds[] = "( |
| 71 | 76 | $lon >= '".Db::escape_string($bbwest)."' |
| 72 | 77 | and $lon < '".Db::escape_string($bbeast)."' |
| 73 | 78 | )"; |
| 74 | - } |
|
| 75 | - else |
|
| 76 | - { |
|
| 79 | + } else { |
|
| 77 | 80 | # We'll have to assume that this bbox goes through the 180-degree meridian. |
| 78 | 81 | # For example, $bbwest = 179 and $bbeast = -179. |
| 79 | 82 | $where_conds[] = "( |
@@ -95,8 +95,7 @@ discard block |
||
| 95 | 95 | # At the beginning we have to set up some "magic e$Xpressions". |
| 96 | 96 | # We will use them to make our query run on both OCPL and OCDE databases. |
| 97 | 97 | |
| 98 | - if (Settings::get('OC_BRANCH') == 'oc.pl') |
|
| 99 | - { |
|
| 98 | + if (Settings::get('OC_BRANCH') == 'oc.pl') { |
|
| 100 | 99 | # OCPL's 'caches' table contains some fields which OCDE's does not |
| 101 | 100 | # (topratings, founds, notfounds, last_found, votes, score). If |
| 102 | 101 | # we're being run on OCPL installation, we will simply use them. |
@@ -107,9 +106,7 @@ discard block |
||
| 107 | 106 | $X_LAST_FOUND = 'caches.last_found'; |
| 108 | 107 | $X_VOTES = 'caches.votes'; |
| 109 | 108 | $X_SCORE = 'caches.score'; |
| 110 | - } |
|
| 111 | - else |
|
| 112 | - { |
|
| 109 | + } else { |
|
| 113 | 110 | # OCDE holds this data in a separate table. Additionally, OCDE |
| 114 | 111 | # does not provide a rating system (votes and score fields). |
| 115 | 112 | # If we're being run on OCDE database, we will include this |
@@ -131,55 +128,44 @@ discard block |
||
| 131 | 128 | # type |
| 132 | 129 | # |
| 133 | 130 | |
| 134 | - if ($tmp = $this->request->get_parameter('type')) |
|
| 135 | - { |
|
| 131 | + if ($tmp = $this->request->get_parameter('type')) { |
|
| 136 | 132 | $operator = "in"; |
| 137 | - if ($tmp[0] == '-') |
|
| 138 | - { |
|
| 133 | + if ($tmp[0] == '-') { |
|
| 139 | 134 | $tmp = substr($tmp, 1); |
| 140 | 135 | $operator = "not in"; |
| 141 | 136 | } |
| 142 | 137 | $types = array(); |
| 143 | - foreach (explode("|", $tmp) as $name) |
|
| 144 | - { |
|
| 145 | - try |
|
| 146 | - { |
|
| 138 | + foreach (explode("|", $tmp) as $name) { |
|
| 139 | + try { |
|
| 147 | 140 | $id = Okapi::cache_type_name2id($name); |
| 148 | 141 | $types[] = $id; |
| 149 | - } |
|
| 150 | - catch (Exception $e) |
|
| 151 | - { |
|
| 142 | + } catch (Exception $e) { |
|
| 152 | 143 | throw new InvalidParam('type', "'$name' is not a valid cache type."); |
| 153 | 144 | } |
| 154 | 145 | } |
| 155 | - if (count($types) > 0) |
|
| 156 | - $where_conds[] = "caches.type $operator ('".implode("','", array_map('\okapi\Db::escape_string', $types))."')"; |
|
| 157 | - else if ($operator == "in") |
|
| 158 | - $where_conds[] = "false"; |
|
| 146 | + if (count($types) > 0) { |
|
| 147 | + $where_conds[] = "caches.type $operator ('".implode("','", array_map('\okapi\Db::escape_string', $types))."')"; |
|
| 148 | + } else if ($operator == "in") { |
|
| 149 | + $where_conds[] = "false"; |
|
| 150 | + } |
|
| 159 | 151 | } |
| 160 | 152 | |
| 161 | 153 | # |
| 162 | 154 | # size2 |
| 163 | 155 | # |
| 164 | 156 | |
| 165 | - if ($tmp = $this->request->get_parameter('size2')) |
|
| 166 | - { |
|
| 157 | + if ($tmp = $this->request->get_parameter('size2')) { |
|
| 167 | 158 | $operator = "in"; |
| 168 | - if ($tmp[0] == '-') |
|
| 169 | - { |
|
| 159 | + if ($tmp[0] == '-') { |
|
| 170 | 160 | $tmp = substr($tmp, 1); |
| 171 | 161 | $operator = "not in"; |
| 172 | 162 | } |
| 173 | 163 | $types = array(); |
| 174 | - foreach (explode("|", $tmp) as $name) |
|
| 175 | - { |
|
| 176 | - try |
|
| 177 | - { |
|
| 164 | + foreach (explode("|", $tmp) as $name) { |
|
| 165 | + try { |
|
| 178 | 166 | $id = Okapi::cache_size2_to_sizeid($name); |
| 179 | 167 | $types[] = $id; |
| 180 | - } |
|
| 181 | - catch (Exception $e) |
|
| 182 | - { |
|
| 168 | + } catch (Exception $e) { |
|
| 183 | 169 | throw new InvalidParam('size2', "'$name' is not a valid cache size."); |
| 184 | 170 | } |
| 185 | 171 | } |
@@ -191,16 +177,14 @@ discard block |
||
| 191 | 177 | # |
| 192 | 178 | |
| 193 | 179 | $tmp = $this->request->get_parameter('status'); |
| 194 | - if ($tmp == null) $tmp = "Available"; |
|
| 180 | + if ($tmp == null) { |
|
| 181 | + $tmp = "Available"; |
|
| 182 | + } |
|
| 195 | 183 | $codes = array(); |
| 196 | - foreach (explode("|", $tmp) as $name) |
|
| 197 | - { |
|
| 198 | - try |
|
| 199 | - { |
|
| 184 | + foreach (explode("|", $tmp) as $name) { |
|
| 185 | + try { |
|
| 200 | 186 | $codes[] = Okapi::cache_status_name2id($name); |
| 201 | - } |
|
| 202 | - catch (Exception $e) |
|
| 203 | - { |
|
| 187 | + } catch (Exception $e) { |
|
| 204 | 188 | throw new InvalidParam('status', "'$name' is not a valid cache status."); |
| 205 | 189 | } |
| 206 | 190 | } |
@@ -210,26 +194,23 @@ discard block |
||
| 210 | 194 | # owner_uuid |
| 211 | 195 | # |
| 212 | 196 | |
| 213 | - if ($tmp = $this->request->get_parameter('owner_uuid')) |
|
| 214 | - { |
|
| 197 | + if ($tmp = $this->request->get_parameter('owner_uuid')) { |
|
| 215 | 198 | $operator = "in"; |
| 216 | - if ($tmp[0] == '-') |
|
| 217 | - { |
|
| 199 | + if ($tmp[0] == '-') { |
|
| 218 | 200 | $tmp = substr($tmp, 1); |
| 219 | 201 | $operator = "not in"; |
| 220 | 202 | } |
| 221 | - try |
|
| 222 | - { |
|
| 203 | + try { |
|
| 223 | 204 | $users = OkapiServiceRunner::call("services/users/users", new OkapiInternalRequest( |
| 224 | 205 | $this->request->consumer, null, array('user_uuids' => $tmp, 'fields' => 'internal_id'))); |
| 225 | - } |
|
| 226 | - catch (InvalidParam $e) # invalid uuid |
|
| 206 | + } catch (InvalidParam $e) # invalid uuid |
|
| 227 | 207 | { |
| 228 | 208 | throw new InvalidParam('owner_uuid', $e->whats_wrong_about_it); |
| 229 | 209 | } |
| 230 | 210 | $user_ids = array(); |
| 231 | - foreach ($users as $user) |
|
| 232 | - $user_ids[] = $user['internal_id']; |
|
| 211 | + foreach ($users as $user) { |
|
| 212 | + $user_ids[] = $user['internal_id']; |
|
| 213 | + } |
|
| 233 | 214 | $where_conds[] = "caches.user_id $operator ('".implode("','", array_map('\okapi\Db::escape_string', $user_ids))."')"; |
| 234 | 215 | } |
| 235 | 216 | |
@@ -237,27 +218,26 @@ discard block |
||
| 237 | 218 | # terrain, difficulty, size, rating - these are similar, we'll do them in a loop |
| 238 | 219 | # |
| 239 | 220 | |
| 240 | - foreach (array('terrain', 'difficulty', 'size', 'rating') as $param_name) |
|
| 241 | - { |
|
| 242 | - if ($tmp = $this->request->get_parameter($param_name)) |
|
| 243 | - { |
|
| 244 | - if (!preg_match("/^[1-5]-[1-5](\|X)?$/", $tmp)) |
|
| 245 | - throw new InvalidParam($param_name, "'$tmp'"); |
|
| 221 | + foreach (array('terrain', 'difficulty', 'size', 'rating') as $param_name) { |
|
| 222 | + if ($tmp = $this->request->get_parameter($param_name)) { |
|
| 223 | + if (!preg_match("/^[1-5]-[1-5](\|X)?$/", $tmp)) { |
|
| 224 | + throw new InvalidParam($param_name, "'$tmp'"); |
|
| 225 | + } |
|
| 246 | 226 | list($min, $max) = explode("-", $tmp); |
| 247 | - if (strpos($max, "|X") !== false) |
|
| 248 | - { |
|
| 227 | + if (strpos($max, "|X") !== false) { |
|
| 249 | 228 | $max = $max[0]; |
| 250 | 229 | $allow_null = true; |
| 251 | 230 | } else { |
| 252 | 231 | $allow_null = false; |
| 253 | 232 | } |
| 254 | - if ($min > $max) |
|
| 255 | - throw new InvalidParam($param_name, "'$tmp'"); |
|
| 256 | - switch ($param_name) |
|
| 257 | - { |
|
| 233 | + if ($min > $max) { |
|
| 234 | + throw new InvalidParam($param_name, "'$tmp'"); |
|
| 235 | + } |
|
| 236 | + switch ($param_name) { |
|
| 258 | 237 | case 'terrain': |
| 259 | - if ($allow_null) |
|
| 260 | - throw new InvalidParam($param_name, "The '|X' suffix is not allowed here."); |
|
| 238 | + if ($allow_null) { |
|
| 239 | + throw new InvalidParam($param_name, "The '|X' suffix is not allowed here."); |
|
| 240 | + } |
|
| 261 | 241 | if (($min == 1) && ($max == 5)) { |
| 262 | 242 | /* no extra condition necessary */ |
| 263 | 243 | } else { |
@@ -265,8 +245,9 @@ discard block |
||
| 265 | 245 | } |
| 266 | 246 | break; |
| 267 | 247 | case 'difficulty': |
| 268 | - if ($allow_null) |
|
| 269 | - throw new InvalidParam($param_name, "The '|X' suffix is not allowed here."); |
|
| 248 | + if ($allow_null) { |
|
| 249 | + throw new InvalidParam($param_name, "The '|X' suffix is not allowed here."); |
|
| 250 | + } |
|
| 270 | 251 | if (($min == 1) && ($max == 5)) { |
| 271 | 252 | /* no extra condition necessary */ |
| 272 | 253 | } else { |
@@ -288,8 +269,7 @@ discard block |
||
| 288 | 269 | } |
| 289 | 270 | break; |
| 290 | 271 | case 'rating': |
| 291 | - if (Settings::get('OC_BRANCH') == 'oc.pl') |
|
| 292 | - { |
|
| 272 | + if (Settings::get('OC_BRANCH') == 'oc.pl') { |
|
| 293 | 273 | if (($min == 1) && ($max == 5) && $allow_null) { |
| 294 | 274 | /* no extra condition necessary */ |
| 295 | 275 | } else { |
@@ -299,9 +279,7 @@ discard block |
||
| 299 | 279 | $where_conds[] = "($X_SCORE >= $min and $X_SCORE < $max and $X_VOTES >= 3)". |
| 300 | 280 | ($allow_null ? " or ($X_VOTES < 3)" : ""); |
| 301 | 281 | } |
| 302 | - } |
|
| 303 | - else |
|
| 304 | - { |
|
| 282 | + } else { |
|
| 305 | 283 | # OCDE does not support rating. We will ignore this parameter. |
| 306 | 284 | } |
| 307 | 285 | break; |
@@ -313,22 +291,23 @@ discard block |
||
| 313 | 291 | # min_rcmds |
| 314 | 292 | # |
| 315 | 293 | |
| 316 | - if ($tmp = $this->request->get_parameter('min_rcmds')) |
|
| 317 | - { |
|
| 318 | - if ($tmp[strlen($tmp) - 1] == '%') |
|
| 319 | - { |
|
| 294 | + if ($tmp = $this->request->get_parameter('min_rcmds')) { |
|
| 295 | + if ($tmp[strlen($tmp) - 1] == '%') { |
|
| 320 | 296 | $tmp = substr($tmp, 0, strlen($tmp) - 1); |
| 321 | - if (!is_numeric($tmp)) |
|
| 322 | - throw new InvalidParam('min_rcmds', "'$tmp'"); |
|
| 297 | + if (!is_numeric($tmp)) { |
|
| 298 | + throw new InvalidParam('min_rcmds', "'$tmp'"); |
|
| 299 | + } |
|
| 323 | 300 | $tmp = intval($tmp); |
| 324 | - if ($tmp > 100 || $tmp < 0) |
|
| 325 | - throw new InvalidParam('min_rcmds', "'$tmp'"); |
|
| 301 | + if ($tmp > 100 || $tmp < 0) { |
|
| 302 | + throw new InvalidParam('min_rcmds', "'$tmp'"); |
|
| 303 | + } |
|
| 326 | 304 | $tmp = floatval($tmp) / 100.0; |
| 327 | 305 | $where_conds[] = "$X_TOPRATINGS >= $X_FOUNDS * '".Db::escape_string($tmp)."'"; |
| 328 | 306 | $where_conds[] = "$X_FOUNDS > 0"; |
| 329 | 307 | } |
| 330 | - if (!is_numeric($tmp)) |
|
| 331 | - throw new InvalidParam('min_rcmds', "'$tmp'"); |
|
| 308 | + if (!is_numeric($tmp)) { |
|
| 309 | + throw new InvalidParam('min_rcmds', "'$tmp'"); |
|
| 310 | + } |
|
| 332 | 311 | $where_conds[] = "$X_TOPRATINGS >= '".Db::escape_string($tmp)."'"; |
| 333 | 312 | } |
| 334 | 313 | |
@@ -336,10 +315,10 @@ discard block |
||
| 336 | 315 | # min_founds |
| 337 | 316 | # |
| 338 | 317 | |
| 339 | - if ($tmp = $this->request->get_parameter('min_founds')) |
|
| 340 | - { |
|
| 341 | - if (!is_numeric($tmp)) |
|
| 342 | - throw new InvalidParam('min_founds', "'$tmp'"); |
|
| 318 | + if ($tmp = $this->request->get_parameter('min_founds')) { |
|
| 319 | + if (!is_numeric($tmp)) { |
|
| 320 | + throw new InvalidParam('min_founds', "'$tmp'"); |
|
| 321 | + } |
|
| 343 | 322 | $where_conds[] = "$X_FOUNDS >= '".Db::escape_string($tmp)."'"; |
| 344 | 323 | } |
| 345 | 324 | |
@@ -348,10 +327,10 @@ discard block |
||
| 348 | 327 | # may be '0' for FTF hunts |
| 349 | 328 | # |
| 350 | 329 | |
| 351 | - if (!is_null($tmp = $this->request->get_parameter('max_founds'))) |
|
| 352 | - { |
|
| 353 | - if (!is_numeric($tmp)) |
|
| 354 | - throw new InvalidParam('max_founds', "'$tmp'"); |
|
| 330 | + if (!is_null($tmp = $this->request->get_parameter('max_founds'))) { |
|
| 331 | + if (!is_numeric($tmp)) { |
|
| 332 | + throw new InvalidParam('max_founds', "'$tmp'"); |
|
| 333 | + } |
|
| 355 | 334 | $where_conds[] = "$X_FOUNDS <= '".Db::escape_string($tmp)."'"; |
| 356 | 335 | } |
| 357 | 336 | |
@@ -359,27 +338,27 @@ discard block |
||
| 359 | 338 | # modified_since |
| 360 | 339 | # |
| 361 | 340 | |
| 362 | - if ($tmp = $this->request->get_parameter('modified_since')) |
|
| 363 | - { |
|
| 341 | + if ($tmp = $this->request->get_parameter('modified_since')) { |
|
| 364 | 342 | $timestamp = strtotime($tmp); |
| 365 | - if ($timestamp) |
|
| 366 | - $where_conds[] = "unix_timestamp(caches.last_modified) > '".Db::escape_string($timestamp)."'"; |
|
| 367 | - else |
|
| 368 | - throw new InvalidParam('modified_since', "'$tmp' is not in a valid format or is not a valid date."); |
|
| 343 | + if ($timestamp) { |
|
| 344 | + $where_conds[] = "unix_timestamp(caches.last_modified) > '".Db::escape_string($timestamp)."'"; |
|
| 345 | + } else { |
|
| 346 | + throw new InvalidParam('modified_since', "'$tmp' is not in a valid format or is not a valid date."); |
|
| 347 | + } |
|
| 369 | 348 | } |
| 370 | 349 | |
| 371 | 350 | # |
| 372 | 351 | # found_status |
| 373 | 352 | # |
| 374 | 353 | |
| 375 | - if ($tmp = $this->request->get_parameter('found_status')) |
|
| 376 | - { |
|
| 377 | - if ($this->request->token == null) |
|
| 378 | - throw new InvalidParam('found_status', "Might be used only for requests signed with an Access Token."); |
|
| 379 | - if (!in_array($tmp, array('found_only', 'notfound_only', 'either'))) |
|
| 380 | - throw new InvalidParam('found_status', "'$tmp'"); |
|
| 381 | - if ($tmp != 'either') |
|
| 382 | - { |
|
| 354 | + if ($tmp = $this->request->get_parameter('found_status')) { |
|
| 355 | + if ($this->request->token == null) { |
|
| 356 | + throw new InvalidParam('found_status', "Might be used only for requests signed with an Access Token."); |
|
| 357 | + } |
|
| 358 | + if (!in_array($tmp, array('found_only', 'notfound_only', 'either'))) { |
|
| 359 | + throw new InvalidParam('found_status', "'$tmp'"); |
|
| 360 | + } |
|
| 361 | + if ($tmp != 'either') { |
|
| 383 | 362 | $found_cache_ids = self::get_found_cache_ids(array($this->request->token->user_id)); |
| 384 | 363 | $operator = ($tmp == 'found_only') ? "in" : "not in"; |
| 385 | 364 | $where_conds[] = "caches.cache_id $operator ('".implode("','", array_map('\okapi\Db::escape_string', $found_cache_ids))."')"; |
@@ -390,12 +369,12 @@ discard block |
||
| 390 | 369 | # found_by |
| 391 | 370 | # |
| 392 | 371 | |
| 393 | - if ($tmp = $this->request->get_parameter('found_by')) |
|
| 394 | - { |
|
| 372 | + if ($tmp = $this->request->get_parameter('found_by')) { |
|
| 395 | 373 | try { |
| 396 | 374 | $users = OkapiServiceRunner::call("services/users/users", new OkapiInternalRequest( |
| 397 | 375 | $this->request->consumer, null, array('user_uuids' => $tmp, 'fields' => 'internal_id'))); |
| 398 | - } catch (InvalidParam $e) { # invalid uuid |
|
| 376 | + } catch (InvalidParam $e) { |
|
| 377 | +# invalid uuid |
|
| 399 | 378 | throw new InvalidParam('found_by', $e->whats_wrong_about_it); |
| 400 | 379 | } |
| 401 | 380 | $internal_user_ids = array_map(create_function('$user', 'return $user["internal_id"];'), $users); |
@@ -407,12 +386,12 @@ discard block |
||
| 407 | 386 | # not_found_by |
| 408 | 387 | # |
| 409 | 388 | |
| 410 | - if ($tmp = $this->request->get_parameter('not_found_by')) |
|
| 411 | - { |
|
| 389 | + if ($tmp = $this->request->get_parameter('not_found_by')) { |
|
| 412 | 390 | try { |
| 413 | 391 | $users = OkapiServiceRunner::call("services/users/users", new OkapiInternalRequest( |
| 414 | 392 | $this->request->consumer, null, array('user_uuids' => $tmp, 'fields' => 'internal_id'))); |
| 415 | - } catch (InvalidParam $e) { # invalid uuid |
|
| 393 | + } catch (InvalidParam $e) { |
|
| 394 | +# invalid uuid |
|
| 416 | 395 | throw new InvalidParam('not_found_by', $e->whats_wrong_about_it); |
| 417 | 396 | } |
| 418 | 397 | $internal_user_ids = array_map(create_function('$user', 'return $user["internal_id"];'), $users); |
@@ -424,21 +403,20 @@ discard block |
||
| 424 | 403 | # watched_only |
| 425 | 404 | # |
| 426 | 405 | |
| 427 | - if ($tmp = $this->request->get_parameter('watched_only')) |
|
| 428 | - { |
|
| 429 | - if ($this->request->token == null) |
|
| 430 | - throw new InvalidParam('watched_only', "Might be used only for requests signed with an Access Token."); |
|
| 431 | - if (!in_array($tmp, array('true', 'false'))) |
|
| 432 | - throw new InvalidParam('watched_only', "'$tmp'"); |
|
| 433 | - if ($tmp == 'true') |
|
| 434 | - { |
|
| 406 | + if ($tmp = $this->request->get_parameter('watched_only')) { |
|
| 407 | + if ($this->request->token == null) { |
|
| 408 | + throw new InvalidParam('watched_only', "Might be used only for requests signed with an Access Token."); |
|
| 409 | + } |
|
| 410 | + if (!in_array($tmp, array('true', 'false'))) { |
|
| 411 | + throw new InvalidParam('watched_only', "'$tmp'"); |
|
| 412 | + } |
|
| 413 | + if ($tmp == 'true') { |
|
| 435 | 414 | $watched_cache_ids = Db::select_column(" |
| 436 | 415 | select cache_id |
| 437 | 416 | from cache_watches |
| 438 | 417 | where user_id = '".Db::escape_string($this->request->token->user_id)."' |
| 439 | 418 | "); |
| 440 | - if (Settings::get('OC_BRANCH') == 'oc.de') |
|
| 441 | - { |
|
| 419 | + if (Settings::get('OC_BRANCH') == 'oc.de') { |
|
| 442 | 420 | $watched_cache_ids = array_merge($watched_cache_ids, Db::select_column(" |
| 443 | 421 | select cache_id |
| 444 | 422 | from cache_list_items cli, cache_list_watches clw |
@@ -455,33 +433,35 @@ discard block |
||
| 455 | 433 | # |
| 456 | 434 | |
| 457 | 435 | $ignored_status = 'either'; |
| 458 | - if ($tmp = $this->request->get_parameter('exclude_ignored')) |
|
| 459 | - { |
|
| 460 | - if ($this->request->token == null) |
|
| 461 | - throw new InvalidParam('exclude_ignored', "Might be used only for requests signed with an Access Token."); |
|
| 462 | - if ($tmp == 'true') |
|
| 463 | - $ignored_status = 'notignored_only'; |
|
| 464 | - elseif ($tmp != 'false') |
|
| 465 | - throw new InvalidParam('exclude_ignored', "'$tmp'"); |
|
| 466 | - } |
|
| 467 | - if ($tmp = $this->request->get_parameter('ignored_status')) |
|
| 468 | - { |
|
| 469 | - if ($this->request->token == null) |
|
| 470 | - throw new InvalidParam('ignored_status', "Might be used only for requests signed with an Access Token."); |
|
| 471 | - if (!in_array($tmp, array('ignored_only', 'notignored_only', 'either'))) |
|
| 472 | - throw new InvalidParam('ignored_status', "'$tmp'"); |
|
| 436 | + if ($tmp = $this->request->get_parameter('exclude_ignored')) { |
|
| 437 | + if ($this->request->token == null) { |
|
| 438 | + throw new InvalidParam('exclude_ignored', "Might be used only for requests signed with an Access Token."); |
|
| 439 | + } |
|
| 440 | + if ($tmp == 'true') { |
|
| 441 | + $ignored_status = 'notignored_only'; |
|
| 442 | + } elseif ($tmp != 'false') { |
|
| 443 | + throw new InvalidParam('exclude_ignored', "'$tmp'"); |
|
| 444 | + } |
|
| 445 | + } |
|
| 446 | + if ($tmp = $this->request->get_parameter('ignored_status')) { |
|
| 447 | + if ($this->request->token == null) { |
|
| 448 | + throw new InvalidParam('ignored_status', "Might be used only for requests signed with an Access Token."); |
|
| 449 | + } |
|
| 450 | + if (!in_array($tmp, array('ignored_only', 'notignored_only', 'either'))) { |
|
| 451 | + throw new InvalidParam('ignored_status', "'$tmp'"); |
|
| 452 | + } |
|
| 473 | 453 | if ($tmp != 'either') { |
| 474 | - if ($tmp == 'ignored_only' && $ignored_status == 'notignored_only') |
|
| 475 | - $ignored_status = 'none'; |
|
| 476 | - else |
|
| 477 | - $ignored_status = $tmp; |
|
| 454 | + if ($tmp == 'ignored_only' && $ignored_status == 'notignored_only') { |
|
| 455 | + $ignored_status = 'none'; |
|
| 456 | + } else { |
|
| 457 | + $ignored_status = $tmp; |
|
| 458 | + } |
|
| 478 | 459 | } |
| 479 | 460 | } |
| 480 | 461 | |
| 481 | - if ($ignored_status == 'none') |
|
| 482 | - $where_conds[] = 'false'; |
|
| 483 | - elseif ($ignored_status != 'either') |
|
| 484 | - { |
|
| 462 | + if ($ignored_status == 'none') { |
|
| 463 | + $where_conds[] = 'false'; |
|
| 464 | + } elseif ($ignored_status != 'either') { |
|
| 485 | 465 | $ignored_cache_ids = Db::select_column(" |
| 486 | 466 | select cache_id |
| 487 | 467 | from cache_ignore |
@@ -495,27 +475,29 @@ discard block |
||
| 495 | 475 | # exclude_my_own |
| 496 | 476 | # |
| 497 | 477 | |
| 498 | - if ($tmp = $this->request->get_parameter('exclude_my_own')) |
|
| 499 | - { |
|
| 500 | - if ($this->request->token == null) |
|
| 501 | - throw new InvalidParam('exclude_my_own', "Might be used only for requests signed with an Access Token."); |
|
| 502 | - if (!in_array($tmp, array('true', 'false'))) |
|
| 503 | - throw new InvalidParam('exclude_my_own', "'$tmp'"); |
|
| 504 | - if ($tmp == 'true') |
|
| 505 | - $where_conds[] = "caches.user_id != '".Db::escape_string($this->request->token->user_id)."'"; |
|
| 478 | + if ($tmp = $this->request->get_parameter('exclude_my_own')) { |
|
| 479 | + if ($this->request->token == null) { |
|
| 480 | + throw new InvalidParam('exclude_my_own', "Might be used only for requests signed with an Access Token."); |
|
| 481 | + } |
|
| 482 | + if (!in_array($tmp, array('true', 'false'))) { |
|
| 483 | + throw new InvalidParam('exclude_my_own', "'$tmp'"); |
|
| 484 | + } |
|
| 485 | + if ($tmp == 'true') { |
|
| 486 | + $where_conds[] = "caches.user_id != '".Db::escape_string($this->request->token->user_id)."'"; |
|
| 487 | + } |
|
| 506 | 488 | } |
| 507 | 489 | |
| 508 | 490 | # |
| 509 | 491 | # name |
| 510 | 492 | # |
| 511 | 493 | |
| 512 | - if ($tmp = $this->request->get_parameter('name')) |
|
| 513 | - { |
|
| 494 | + if ($tmp = $this->request->get_parameter('name')) { |
|
| 514 | 495 | # WRTODO: Make this more user-friendly. See: |
| 515 | 496 | # https://github.com/opencaching/okapi/issues/121 |
| 516 | 497 | |
| 517 | - if (strlen($tmp) > 100) |
|
| 518 | - throw new InvalidParam('name', "Maximum length of 'name' parameter is 100 characters"); |
|
| 498 | + if (strlen($tmp) > 100) { |
|
| 499 | + throw new InvalidParam('name', "Maximum length of 'name' parameter is 100 characters"); |
|
| 500 | + } |
|
| 519 | 501 | $tmp = str_replace("*", "%", str_replace("%", "%%", $tmp)); |
| 520 | 502 | $where_conds[] = "caches.name LIKE '".Db::escape_string($tmp)."'"; |
| 521 | 503 | } |
@@ -524,12 +506,11 @@ discard block |
||
| 524 | 506 | # with_trackables_only |
| 525 | 507 | # |
| 526 | 508 | |
| 527 | - if ($tmp = $this->request->get_parameter('with_trackables_only')) |
|
| 528 | - { |
|
| 529 | - if (!in_array($tmp, array('true', 'false'), 1)) |
|
| 530 | - throw new InvalidParam('with_trackables_only', "'$tmp'"); |
|
| 531 | - if ($tmp == 'true') |
|
| 532 | - { |
|
| 509 | + if ($tmp = $this->request->get_parameter('with_trackables_only')) { |
|
| 510 | + if (!in_array($tmp, array('true', 'false'), 1)) { |
|
| 511 | + throw new InvalidParam('with_trackables_only', "'$tmp'"); |
|
| 512 | + } |
|
| 513 | + if ($tmp == 'true') { |
|
| 533 | 514 | $where_conds[] = " |
| 534 | 515 | caches.wp_oc in ( |
| 535 | 516 | select distinct wp |
@@ -543,12 +524,11 @@ discard block |
||
| 543 | 524 | # ftf_hunter |
| 544 | 525 | # |
| 545 | 526 | |
| 546 | - if ($tmp = $this->request->get_parameter('ftf_hunter')) |
|
| 547 | - { |
|
| 548 | - if (!in_array($tmp, array('true', 'false'), 1)) |
|
| 549 | - throw new InvalidParam('ftf_hunter', "'$tmp'"); |
|
| 550 | - if ($tmp == 'true') |
|
| 551 | - { |
|
| 527 | + if ($tmp = $this->request->get_parameter('ftf_hunter')) { |
|
| 528 | + if (!in_array($tmp, array('true', 'false'), 1)) { |
|
| 529 | + throw new InvalidParam('ftf_hunter', "'$tmp'"); |
|
| 530 | + } |
|
| 531 | + if ($tmp == 'true') { |
|
| 552 | 532 | $where_conds[] = "$X_FOUNDS = 0"; |
| 553 | 533 | } |
| 554 | 534 | } |
@@ -558,8 +538,7 @@ discard block |
||
| 558 | 538 | # |
| 559 | 539 | |
| 560 | 540 | $join_powertrails = false; |
| 561 | - if ($tmp = $this->request->get_parameter('powertrail_only')) |
|
| 562 | - { |
|
| 541 | + if ($tmp = $this->request->get_parameter('powertrail_only')) { |
|
| 563 | 542 | if ($tmp === 'true') { |
| 564 | 543 | $join_powertrails = true; |
| 565 | 544 | } elseif ($tmp === 'false') { |
@@ -594,8 +573,7 @@ discard block |
||
| 594 | 573 | # set_and |
| 595 | 574 | # |
| 596 | 575 | |
| 597 | - if ($tmp = $this->request->get_parameter('set_and')) |
|
| 598 | - { |
|
| 576 | + if ($tmp = $this->request->get_parameter('set_and')) { |
|
| 599 | 577 | # Check if the set exists. |
| 600 | 578 | |
| 601 | 579 | $exists = Db::select_value(" |
@@ -603,8 +581,9 @@ discard block |
||
| 603 | 581 | from okapi_search_sets |
| 604 | 582 | where id = '".Db::escape_string($tmp)."' |
| 605 | 583 | "); |
| 606 | - if (!$exists) |
|
| 607 | - throw new InvalidParam('set_and', "Couldn't find a set by given ID."); |
|
| 584 | + if (!$exists) { |
|
| 585 | + throw new InvalidParam('set_and', "Couldn't find a set by given ID."); |
|
| 586 | + } |
|
| 608 | 587 | $extra_tables[] = "okapi_search_results osr_and"; |
| 609 | 588 | $where_conds[] = "osr_and.cache_id = caches.cache_id"; |
| 610 | 589 | $where_conds[] = "osr_and.set_id = '".Db::escape_string($tmp)."'"; |
@@ -615,34 +594,43 @@ discard block |
||
| 615 | 594 | # |
| 616 | 595 | |
| 617 | 596 | $limit = $this->request->get_parameter('limit'); |
| 618 | - if ($limit == null) $limit = "100"; |
|
| 619 | - if (!is_numeric($limit)) |
|
| 620 | - throw new InvalidParam('limit', "'$limit'"); |
|
| 621 | - if ($limit < 1 || (($limit > 500) && (!$this->request->skip_limits))) |
|
| 622 | - throw new InvalidParam( |
|
| 597 | + if ($limit == null) { |
|
| 598 | + $limit = "100"; |
|
| 599 | + } |
|
| 600 | + if (!is_numeric($limit)) { |
|
| 601 | + throw new InvalidParam('limit', "'$limit'"); |
|
| 602 | + } |
|
| 603 | + if ($limit < 1 || (($limit > 500) && (!$this->request->skip_limits))) { |
|
| 604 | + throw new InvalidParam( |
|
| 623 | 605 | 'limit', |
| 624 | 606 | $this->request->skip_limits |
| 625 | 607 | ? "Cannot be lower than 1." |
| 626 | 608 | : "Has to be between 1 and 500." |
| 627 | 609 | ); |
| 610 | + } |
|
| 628 | 611 | |
| 629 | 612 | # |
| 630 | 613 | # offset |
| 631 | 614 | # |
| 632 | 615 | |
| 633 | 616 | $offset = $this->request->get_parameter('offset'); |
| 634 | - if ($offset == null) $offset = "0"; |
|
| 635 | - if (!is_numeric($offset)) |
|
| 636 | - throw new InvalidParam('offset', "'$offset'"); |
|
| 637 | - if (($offset + $limit > 500) && (!$this->request->skip_limits)) |
|
| 638 | - throw new BadRequest("The sum of offset and limit may not exceed 500."); |
|
| 639 | - if ($offset < 0 || (($offset > 499) && (!$this->request->skip_limits))) |
|
| 640 | - throw new InvalidParam( |
|
| 617 | + if ($offset == null) { |
|
| 618 | + $offset = "0"; |
|
| 619 | + } |
|
| 620 | + if (!is_numeric($offset)) { |
|
| 621 | + throw new InvalidParam('offset', "'$offset'"); |
|
| 622 | + } |
|
| 623 | + if (($offset + $limit > 500) && (!$this->request->skip_limits)) { |
|
| 624 | + throw new BadRequest("The sum of offset and limit may not exceed 500."); |
|
| 625 | + } |
|
| 626 | + if ($offset < 0 || (($offset > 499) && (!$this->request->skip_limits))) { |
|
| 627 | + throw new InvalidParam( |
|
| 641 | 628 | 'offset', |
| 642 | 629 | $this->request->skip_limits |
| 643 | 630 | ? "Cannot be lower than 0." |
| 644 | 631 | : "Has to be between 0 and 499." |
| 645 | 632 | ); |
| 633 | + } |
|
| 646 | 634 | |
| 647 | 635 | # |
| 648 | 636 | # order_by |
@@ -650,21 +638,18 @@ discard block |
||
| 650 | 638 | |
| 651 | 639 | $order_clauses = array(); |
| 652 | 640 | $order_by = $this->request->get_parameter('order_by'); |
| 653 | - if ($order_by != null) |
|
| 654 | - { |
|
| 641 | + if ($order_by != null) { |
|
| 655 | 642 | $order_by = explode('|', $order_by); |
| 656 | - foreach ($order_by as $field) |
|
| 657 | - { |
|
| 643 | + foreach ($order_by as $field) { |
|
| 658 | 644 | $dir = 'asc'; |
| 659 | - if ($field[0] == '-') |
|
| 660 | - { |
|
| 645 | + if ($field[0] == '-') { |
|
| 661 | 646 | $dir = 'desc'; |
| 662 | 647 | $field = substr($field, 1); |
| 648 | + } elseif ($field[0] == '+') { |
|
| 649 | + $field = substr($field, 1); |
|
| 663 | 650 | } |
| 664 | - elseif ($field[0] == '+') |
|
| 665 | - $field = substr($field, 1); # ignore leading "+" |
|
| 666 | - switch ($field) |
|
| 667 | - { |
|
| 651 | + # ignore leading "+" |
|
| 652 | + switch ($field) { |
|
| 668 | 653 | case 'code': $cl = "caches.wp_oc"; break; |
| 669 | 654 | case 'name': $cl = "caches.name"; break; |
| 670 | 655 | case 'founds': $cl = "$X_FOUNDS"; break; |
@@ -682,8 +667,9 @@ discard block |
||
| 682 | 667 | # To avoid join errors, put each of the $where_conds in extra paranthesis. |
| 683 | 668 | |
| 684 | 669 | $tmp = array(); |
| 685 | - foreach($where_conds as $cond) |
|
| 686 | - $tmp[] = "(".$cond.")"; |
|
| 670 | + foreach($where_conds as $cond) { |
|
| 671 | + $tmp[] = "(".$cond.")"; |
|
| 672 | + } |
|
| 687 | 673 | $where_conds = $tmp; |
| 688 | 674 | unset($tmp); |
| 689 | 675 | |
@@ -697,8 +683,7 @@ discard block |
||
| 697 | 683 | 'extra_joins' => $extra_joins, |
| 698 | 684 | ); |
| 699 | 685 | |
| 700 | - if ($this->search_params === NULL) |
|
| 701 | - { |
|
| 686 | + if ($this->search_params === NULL) { |
|
| 702 | 687 | $this->search_params = $ret_array; |
| 703 | 688 | } else { |
| 704 | 689 | $this->search_params = array_merge_recursive($this->search_params, $ret_array); |
@@ -738,8 +723,7 @@ discard block |
||
| 738 | 723 | limit ".($this->search_params['offset']).", ".($this->search_params['limit'] + 1)."; |
| 739 | 724 | "); |
| 740 | 725 | |
| 741 | - if (count($cache_codes) > $this->search_params['limit']) |
|
| 742 | - { |
|
| 726 | + if (count($cache_codes) > $this->search_params['limit']) { |
|
| 743 | 727 | $more = true; |
| 744 | 728 | array_pop($cache_codes); # get rid of the one above the limit |
| 745 | 729 | } else { |
@@ -767,18 +751,17 @@ discard block |
||
| 767 | 751 | public function prepare_location_search_params() |
| 768 | 752 | { |
| 769 | 753 | $location_source = $this->request->get_parameter('location_source'); |
| 770 | - if (!$location_source) |
|
| 771 | - $location_source = 'default-coords'; |
|
| 754 | + if (!$location_source) { |
|
| 755 | + $location_source = 'default-coords'; |
|
| 756 | + } |
|
| 772 | 757 | |
| 773 | 758 | # Make sure location_source has prefix alt_wpt: |
| 774 | - if ($location_source != 'default-coords' && strncmp($location_source, 'alt_wpt:', 8) != 0) |
|
| 775 | - { |
|
| 759 | + if ($location_source != 'default-coords' && strncmp($location_source, 'alt_wpt:', 8) != 0) { |
|
| 776 | 760 | throw new InvalidParam('location_source', '\''.$location_source.'\''); |
| 777 | 761 | } |
| 778 | 762 | |
| 779 | 763 | # Make sure we have sufficient authorization |
| 780 | - if ($location_source == 'alt_wpt:user-coords' && $this->request->token == null) |
|
| 781 | - { |
|
| 764 | + if ($location_source == 'alt_wpt:user-coords' && $this->request->token == null) { |
|
| 782 | 765 | throw new BadRequest("Level 3 Authentication is required to access 'alt_wpt:user-coords'."); |
| 783 | 766 | } |
| 784 | 767 | |
@@ -787,14 +770,12 @@ discard block |
||
| 787 | 770 | $location_source = 'default-coords'; |
| 788 | 771 | } |
| 789 | 772 | |
| 790 | - if ($location_source == 'default-coords') |
|
| 791 | - { |
|
| 773 | + if ($location_source == 'default-coords') { |
|
| 792 | 774 | $this->longitude_expr = 'caches.longitude'; |
| 793 | 775 | $this->latitude_expr = 'caches.latitude'; |
| 794 | 776 | } else { |
| 795 | 777 | $extra_joins = null; |
| 796 | - if (Settings::get('OC_BRANCH') == 'oc.pl') |
|
| 797 | - { |
|
| 778 | + if (Settings::get('OC_BRANCH') == 'oc.pl') { |
|
| 798 | 779 | $this->longitude_expr = 'ifnull(cache_mod_cords.longitude, caches.longitude)'; |
| 799 | 780 | $this->latitude_expr = 'ifnull(cache_mod_cords.latitude, caches.latitude)'; |
| 800 | 781 | $extra_joins = array(" |
@@ -818,8 +799,7 @@ discard block |
||
| 818 | 799 | $location_extra_sql = array( |
| 819 | 800 | 'extra_joins' => $extra_joins |
| 820 | 801 | ); |
| 821 | - if ($this->search_params === NULL) |
|
| 822 | - { |
|
| 802 | + if ($this->search_params === NULL) { |
|
| 823 | 803 | $this->search_params = $location_extra_sql; |
| 824 | 804 | } else { |
| 825 | 805 | $this->search_params = array_merge_recursive($this->search_params, $location_extra_sql); |
@@ -39,8 +39,9 @@ discard block |
||
| 39 | 39 | order by id desc |
| 40 | 40 | limit 1 |
| 41 | 41 | "); |
| 42 | - if ($tmp === null) |
|
| 43 | - return array(null, null, null); |
|
| 42 | + if ($tmp === null) { |
|
| 43 | + return array(null, null, null); |
|
| 44 | + } |
|
| 44 | 45 | return array($tmp['id'], $tmp['date_created'], $tmp['expires']); |
| 45 | 46 | } |
| 46 | 47 | |
@@ -49,17 +50,25 @@ discard block |
||
| 49 | 50 | # "Cache control" parameters. |
| 50 | 51 | |
| 51 | 52 | $tmp = $request->get_parameter('min_store'); |
| 52 | - if ($tmp === null) $tmp = "300"; |
|
| 53 | + if ($tmp === null) { |
|
| 54 | + $tmp = "300"; |
|
| 55 | + } |
|
| 53 | 56 | $min_store = intval($tmp); |
| 54 | - if (("$min_store" !== $tmp) ||($min_store < 0) || ($min_store > 64800)) |
|
| 55 | - throw new InvalidParam('min_store', "Has to be in the 0..64800 range."); |
|
| 57 | + if (("$min_store" !== $tmp) ||($min_store < 0) || ($min_store > 64800)) { |
|
| 58 | + throw new InvalidParam('min_store', "Has to be in the 0..64800 range."); |
|
| 59 | + } |
|
| 56 | 60 | |
| 57 | 61 | $tmp = $request->get_parameter('ref_max_age'); |
| 58 | - if ($tmp === null) $tmp = "300"; |
|
| 59 | - if ($tmp == "nolimit") $tmp = "9999999"; |
|
| 62 | + if ($tmp === null) { |
|
| 63 | + $tmp = "300"; |
|
| 64 | + } |
|
| 65 | + if ($tmp == "nolimit") { |
|
| 66 | + $tmp = "9999999"; |
|
| 67 | + } |
|
| 60 | 68 | $ref_max_age = intval($tmp); |
| 61 | - if (("$ref_max_age" !== $tmp) || ($ref_max_age < 300)) |
|
| 62 | - throw new InvalidParam('ref_max_age', "Has to be >=300."); |
|
| 69 | + if (("$ref_max_age" !== $tmp) || ($ref_max_age < 300)) { |
|
| 70 | + throw new InvalidParam('ref_max_age', "Has to be >=300."); |
|
| 71 | + } |
|
| 63 | 72 | |
| 64 | 73 | # Search params. |
| 65 | 74 | |
@@ -75,10 +84,11 @@ discard block |
||
| 75 | 84 | $search_params['where_conds'] |
| 76 | 85 | ); |
| 77 | 86 | |
| 78 | - if (isset($search_params['extra_joins']) && is_array($search_params['extra_joins'])) |
|
| 79 | - $joins = $search_params['extra_joins']; |
|
| 80 | - else |
|
| 81 | - $joins = array(); |
|
| 87 | + if (isset($search_params['extra_joins']) && is_array($search_params['extra_joins'])) { |
|
| 88 | + $joins = $search_params['extra_joins']; |
|
| 89 | + } else { |
|
| 90 | + $joins = array(); |
|
| 91 | + } |
|
| 82 | 92 | |
| 83 | 93 | unset($search_params); |
| 84 | 94 | |
@@ -103,8 +113,7 @@ discard block |
||
| 103 | 113 | # given freshness criteria. |
| 104 | 114 | |
| 105 | 115 | list($set_id, $date_created, $expires) = self::find_param_set($params_hash, $ref_max_age); |
| 106 | - if ($set_id === null) |
|
| 107 | - { |
|
| 116 | + if ($set_id === null) { |
|
| 108 | 117 | # To avoid generating the same results by multiple threads at once |
| 109 | 118 | # (the "tile" method uses the "save" method, so the problem is |
| 110 | 119 | # quite real!), we will acquire a write-lock here. |
@@ -112,13 +121,11 @@ discard block |
||
| 112 | 121 | $lock = OkapiLock::get("search-results-writer"); |
| 113 | 122 | $lock->acquire(); |
| 114 | 123 | |
| 115 | - try |
|
| 116 | - { |
|
| 124 | + try { |
|
| 117 | 125 | # Make sure we were the first to acquire the lock. |
| 118 | 126 | |
| 119 | 127 | list($set_id, $date_created, $expires) = self::find_param_set($params_hash, $ref_max_age); |
| 120 | - if ($set_id === null) |
|
| 121 | - { |
|
| 128 | + if ($set_id === null) { |
|
| 122 | 129 | # We are in the first thread which have acquired the lock. |
| 123 | 130 | # We will proceed with result-set creation. Other threads |
| 124 | 131 | # will be waiting until we finish. |
@@ -161,9 +168,7 @@ discard block |
||
| 161 | 168 | # generated the result set. We don't need to do anything. |
| 162 | 169 | } |
| 163 | 170 | $lock->release(); |
| 164 | - } |
|
| 165 | - catch (Exception $e) |
|
| 166 | - { |
|
| 171 | + } catch (Exception $e) { |
|
| 167 | 172 | # SQL error? Make sure the lock is released and rethrow. |
| 168 | 173 | |
| 169 | 174 | $lock->release(); |
@@ -174,8 +179,7 @@ discard block |
||
| 174 | 179 | # If we got an old set, we may need to expand its lifetime in order to |
| 175 | 180 | # meet user's "min_store" criterium. |
| 176 | 181 | |
| 177 | - if (time() + $min_store > $expires) |
|
| 178 | - { |
|
| 182 | + if (time() + $min_store > $expires) { |
|
| 179 | 183 | Db::execute(" |
| 180 | 184 | update okapi_search_sets |
| 181 | 185 | set expires = date_add(now(), interval '".Db::escape_string($min_store + 60)."' second) |
@@ -30,22 +30,26 @@ discard block |
||
| 30 | 30 | # It's much easier to grasp their meaning this way. |
| 31 | 31 | |
| 32 | 32 | $tmp = $request->get_parameter('center'); |
| 33 | - if (!$tmp) |
|
| 34 | - throw new ParamMissing('center'); |
|
| 33 | + if (!$tmp) { |
|
| 34 | + throw new ParamMissing('center'); |
|
| 35 | + } |
|
| 35 | 36 | $parts = explode('|', $tmp); |
| 36 | - if (count($parts) != 2) |
|
| 37 | - throw new InvalidParam('center', "Expecting 2 pipe-separated parts, got ".count($parts)."."); |
|
| 38 | - foreach ($parts as &$part_ref) |
|
| 39 | - { |
|
| 40 | - if (!preg_match("/^-?[0-9]+(\.?[0-9]*)$/", $part_ref)) |
|
| 41 | - throw new InvalidParam('center', "'$part_ref' is not a valid float number."); |
|
| 37 | + if (count($parts) != 2) { |
|
| 38 | + throw new InvalidParam('center', "Expecting 2 pipe-separated parts, got ".count($parts)."."); |
|
| 39 | + } |
|
| 40 | + foreach ($parts as &$part_ref) { |
|
| 41 | + if (!preg_match("/^-?[0-9]+(\.?[0-9]*)$/", $part_ref)) { |
|
| 42 | + throw new InvalidParam('center', "'$part_ref' is not a valid float number."); |
|
| 43 | + } |
|
| 42 | 44 | $part_ref = floatval($part_ref); |
| 43 | 45 | } |
| 44 | 46 | list($center_lat, $center_lon) = $parts; |
| 45 | - if ($center_lat > 90 || $center_lat < -90) |
|
| 46 | - throw new InvalidParam('center', "Latitudes have to be within -90..90 range."); |
|
| 47 | - if ($center_lon > 180 || $center_lon < -180) |
|
| 48 | - throw new InvalidParam('center', "Longitudes have to be within -180..180 range."); |
|
| 47 | + if ($center_lat > 90 || $center_lat < -90) { |
|
| 48 | + throw new InvalidParam('center', "Latitudes have to be within -90..90 range."); |
|
| 49 | + } |
|
| 50 | + if ($center_lon > 180 || $center_lon < -180) { |
|
| 51 | + throw new InvalidParam('center', "Longitudes have to be within -180..180 range."); |
|
| 52 | + } |
|
| 49 | 53 | |
| 50 | 54 | # |
| 51 | 55 | # In the method description, we promised to return caches ordered by the *rough* |
@@ -66,13 +70,14 @@ discard block |
||
| 66 | 70 | |
| 67 | 71 | $where_conds = array(); |
| 68 | 72 | $radius = null; |
| 69 | - if ($tmp = $request->get_parameter('radius')) |
|
| 70 | - { |
|
| 71 | - if (!preg_match("/^-?[0-9]+(\.?[0-9]*)$/", $tmp)) |
|
| 72 | - throw new InvalidParam('radius', "'$tmp' is not a valid float number."); |
|
| 73 | + if ($tmp = $request->get_parameter('radius')) { |
|
| 74 | + if (!preg_match("/^-?[0-9]+(\.?[0-9]*)$/", $tmp)) { |
|
| 75 | + throw new InvalidParam('radius', "'$tmp' is not a valid float number."); |
|
| 76 | + } |
|
| 73 | 77 | $radius = floatval($tmp); # is given in kilometers |
| 74 | - if ($radius <= 0) |
|
| 75 | - throw new InvalidParam('radius', "Has to be a positive number."); |
|
| 78 | + if ($radius <= 0) { |
|
| 79 | + throw new InvalidParam('radius', "Has to be a positive number."); |
|
| 80 | + } |
|
| 76 | 81 | |
| 77 | 82 | # Apply a latitude-range prefilter if it looks promising. |
| 78 | 83 | # See https://github.com/opencaching/okapi/issues/363 for more info. |
@@ -80,8 +85,7 @@ discard block |
||
| 80 | 85 | $optimization_radius = 100; # in kilometers, optimized for Opencaching.de |
| 81 | 86 | $km2degrees_upper_estimate_factor = 0.01; |
| 82 | 87 | |
| 83 | - if ($radius <= $optimization_radius) |
|
| 84 | - { |
|
| 88 | + if ($radius <= $optimization_radius) { |
|
| 85 | 89 | $radius_degrees = $radius * $km2degrees_upper_estimate_factor; |
| 86 | 90 | $where_conds[] = " |
| 87 | 91 | caches.latitude >= '".Db::escape_string($center_lat - $radius_degrees)."' |
@@ -100,8 +104,7 @@ discard block |
||
| 100 | 104 | $search_assistant->set_search_params($search_params); |
| 101 | 105 | |
| 102 | 106 | $result = $search_assistant->get_common_search_result(); |
| 103 | - if ($radius == null) |
|
| 104 | - { |
|
| 107 | + if ($radius == null) { |
|
| 105 | 108 | # 'more' is meaningless in this case, we'll remove it. |
| 106 | 109 | unset($result['more']); |
| 107 | 110 | } |
@@ -30,48 +30,56 @@ discard block |
||
| 30 | 30 | |
| 31 | 31 | static $host = null; |
| 32 | 32 | static $length = null; |
| 33 | - if ($host == null) |
|
| 34 | - { |
|
| 33 | + if ($host == null) { |
|
| 35 | 34 | $host = parse_url(Settings::get('SITE_URL'), PHP_URL_HOST); |
| 36 | - if (strpos($host, "www.") === 0) |
|
| 37 | - $host = substr($host, 4); |
|
| 35 | + if (strpos($host, "www.") === 0) { |
|
| 36 | + $host = substr($host, 4); |
|
| 37 | + } |
|
| 38 | 38 | $length = strlen($host); |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | # Parse the URL |
| 42 | 42 | |
| 43 | 43 | $uri = parse_url($url); |
| 44 | - if ($uri == false) |
|
| 45 | - return null; |
|
| 46 | - if ((!isset($uri['scheme'])) || (!in_array($uri['scheme'], array('http', 'https')))) |
|
| 47 | - return null; |
|
| 48 | - if ((!isset($uri['host'])) || (substr($uri['host'], -$length) != $host)) |
|
| 49 | - return null; |
|
| 50 | - if (!isset($uri['path'])) |
|
| 51 | - return null; |
|
| 52 | - if (preg_match("#^/(O[A-Z][A-Z0-9]{4,5})$#", $uri['path'], $matches)) |
|
| 53 | - { |
|
| 44 | + if ($uri == false) { |
|
| 45 | + return null; |
|
| 46 | + } |
|
| 47 | + if ((!isset($uri['scheme'])) || (!in_array($uri['scheme'], array('http', 'https')))) { |
|
| 48 | + return null; |
|
| 49 | + } |
|
| 50 | + if ((!isset($uri['host'])) || (substr($uri['host'], -$length) != $host)) { |
|
| 51 | + return null; |
|
| 52 | + } |
|
| 53 | + if (!isset($uri['path'])) { |
|
| 54 | + return null; |
|
| 55 | + } |
|
| 56 | + if (preg_match("#^/(O[A-Z][A-Z0-9]{4,5})$#", $uri['path'], $matches)) { |
|
| 54 | 57 | # Some servers allow "http://oc.xx/<cache_code>" shortcut. |
| 55 | 58 | return array('cache_code', $matches[1]); |
| 56 | 59 | } |
| 57 | 60 | $parts = array(); |
| 58 | - if (isset($uri['query'])) |
|
| 59 | - $parts = array_merge($parts, explode('&', $uri['query'])); |
|
| 60 | - if (isset($uri['fragment'])) |
|
| 61 | - $parts = array_merge($parts, explode('&', $uri['fragment'])); |
|
| 62 | - foreach ($parts as $param) |
|
| 63 | - { |
|
| 61 | + if (isset($uri['query'])) { |
|
| 62 | + $parts = array_merge($parts, explode('&', $uri['query'])); |
|
| 63 | + } |
|
| 64 | + if (isset($uri['fragment'])) { |
|
| 65 | + $parts = array_merge($parts, explode('&', $uri['fragment'])); |
|
| 66 | + } |
|
| 67 | + foreach ($parts as $param) { |
|
| 64 | 68 | $item = explode('=', $param, 2); |
| 65 | - if (count($item) != 2) |
|
| 66 | - continue; |
|
| 69 | + if (count($item) != 2) { |
|
| 70 | + continue; |
|
| 71 | + } |
|
| 67 | 72 | $key = $item[0]; |
| 68 | 73 | $value = $item[1]; |
| 69 | - if ($key == 'wp') |
|
| 70 | - return array('cache_code', $value); |
|
| 71 | - if ($key == 'cacheid') |
|
| 72 | - return array('internal_id', $value); |
|
| 73 | - if ($key == 'uuid') |
|
| 74 | - return array('uuid', $value); |
|
| 74 | + if ($key == 'wp') { |
|
| 75 | + return array('cache_code', $value); |
|
| 76 | + } |
|
| 77 | + if ($key == 'cacheid') { |
|
| 78 | + return array('internal_id', $value); |
|
| 79 | + } |
|
| 80 | + if ($key == 'uuid') { |
|
| 81 | + return array('uuid', $value); |
|
| 82 | + } |
|
| 75 | 83 | } |
| 76 | 84 | return null; |
| 77 | 85 | } |
@@ -81,13 +89,17 @@ discard block |
||
| 81 | 89 | # Retrieve the list of URLs to check. |
| 82 | 90 | |
| 83 | 91 | $tmp = $request->get_parameter('urls'); |
| 84 | - if (!$tmp) |
|
| 85 | - throw new ParamMissing('urls'); |
|
| 92 | + if (!$tmp) { |
|
| 93 | + throw new ParamMissing('urls'); |
|
| 94 | + } |
|
| 86 | 95 | $urls = explode('|', $tmp); |
| 87 | 96 | $as_dict = $request->get_parameter('as_dict'); |
| 88 | - if (!$as_dict) $as_dict = 'false'; |
|
| 89 | - if (!in_array($as_dict, array('true', 'false'))) |
|
| 90 | - throw new InvalidParam('as_dict'); |
|
| 97 | + if (!$as_dict) { |
|
| 98 | + $as_dict = 'false'; |
|
| 99 | + } |
|
| 100 | + if (!in_array($as_dict, array('true', 'false'))) { |
|
| 101 | + throw new InvalidParam('as_dict'); |
|
| 102 | + } |
|
| 91 | 103 | $as_dict = ($as_dict == 'true'); |
| 92 | 104 | |
| 93 | 105 | # Generate the lists of keys. |
@@ -98,25 +110,25 @@ discard block |
||
| 98 | 110 | 'internal_id' => array(), |
| 99 | 111 | 'uuid' => array() |
| 100 | 112 | ); |
| 101 | - foreach ($urls as &$url_ref) |
|
| 102 | - { |
|
| 113 | + foreach ($urls as &$url_ref) { |
|
| 103 | 114 | $key = self::get_cache_key($url_ref); |
| 104 | - if ($key != null) |
|
| 105 | - $urls_with[$key[0]][$url_ref] = $key[1]; |
|
| 106 | - else |
|
| 107 | - $results[$url_ref] = null; |
|
| 115 | + if ($key != null) { |
|
| 116 | + $urls_with[$key[0]][$url_ref] = $key[1]; |
|
| 117 | + } else { |
|
| 118 | + $results[$url_ref] = null; |
|
| 119 | + } |
|
| 108 | 120 | } |
| 109 | 121 | |
| 110 | 122 | # Include 'cache_code' references. |
| 111 | 123 | |
| 112 | - foreach ($urls_with['cache_code'] as $url => $cache_code) |
|
| 113 | - $results[$url] = $cache_code; |
|
| 124 | + foreach ($urls_with['cache_code'] as $url => $cache_code) { |
|
| 125 | + $results[$url] = $cache_code; |
|
| 126 | + } |
|
| 114 | 127 | |
| 115 | 128 | # Include 'internal_id' references. |
| 116 | 129 | |
| 117 | 130 | $internal_ids = array_values($urls_with['internal_id']); |
| 118 | - if (count($internal_ids) > 0) |
|
| 119 | - { |
|
| 131 | + if (count($internal_ids) > 0) { |
|
| 120 | 132 | $rs = Db::query(" |
| 121 | 133 | select cache_id, wp_oc |
| 122 | 134 | from caches |
@@ -125,22 +137,22 @@ discard block |
||
| 125 | 137 | and status in (1,2,3) |
| 126 | 138 | "); |
| 127 | 139 | $dict = array(); |
| 128 | - while ($row = Db::fetch_assoc($rs)) |
|
| 129 | - $dict[$row['cache_id']] = $row['wp_oc']; |
|
| 130 | - foreach ($urls_with['internal_id'] as $url => $internal_id) |
|
| 131 | - { |
|
| 132 | - if (isset($dict[$internal_id])) |
|
| 133 | - $results[$url] = $dict[$internal_id]; |
|
| 134 | - else |
|
| 135 | - $results[$url] = null; |
|
| 140 | + while ($row = Db::fetch_assoc($rs)) { |
|
| 141 | + $dict[$row['cache_id']] = $row['wp_oc']; |
|
| 142 | + } |
|
| 143 | + foreach ($urls_with['internal_id'] as $url => $internal_id) { |
|
| 144 | + if (isset($dict[$internal_id])) { |
|
| 145 | + $results[$url] = $dict[$internal_id]; |
|
| 146 | + } else { |
|
| 147 | + $results[$url] = null; |
|
| 148 | + } |
|
| 136 | 149 | } |
| 137 | 150 | } |
| 138 | 151 | |
| 139 | 152 | # Include 'uuid' references. |
| 140 | 153 | |
| 141 | 154 | $uuids = array_values($urls_with['uuid']); |
| 142 | - if (count($uuids) > 0) |
|
| 143 | - { |
|
| 155 | + if (count($uuids) > 0) { |
|
| 144 | 156 | $rs = Db::query(" |
| 145 | 157 | select uuid, wp_oc |
| 146 | 158 | from caches |
@@ -149,27 +161,28 @@ discard block |
||
| 149 | 161 | and status in (1,2,3) |
| 150 | 162 | "); |
| 151 | 163 | $dict = array(); |
| 152 | - while ($row = Db::fetch_assoc($rs)) |
|
| 153 | - $dict[$row['uuid']] = $row['wp_oc']; |
|
| 154 | - foreach ($urls_with['uuid'] as $url => $uuid) |
|
| 155 | - { |
|
| 156 | - if (isset($dict[$uuid])) |
|
| 157 | - $results[$url] = $dict[$uuid]; |
|
| 158 | - else |
|
| 159 | - $results[$url] = null; |
|
| 164 | + while ($row = Db::fetch_assoc($rs)) { |
|
| 165 | + $dict[$row['uuid']] = $row['wp_oc']; |
|
| 166 | + } |
|
| 167 | + foreach ($urls_with['uuid'] as $url => $uuid) { |
|
| 168 | + if (isset($dict[$uuid])) { |
|
| 169 | + $results[$url] = $dict[$uuid]; |
|
| 170 | + } else { |
|
| 171 | + $results[$url] = null; |
|
| 172 | + } |
|
| 160 | 173 | } |
| 161 | 174 | } |
| 162 | 175 | |
| 163 | 176 | # Format the results according to the 'as_dict' parameter. |
| 164 | 177 | |
| 165 | - if ($as_dict) |
|
| 166 | - return Okapi::formatted_response($request, $results); |
|
| 167 | - else |
|
| 168 | - { |
|
| 178 | + if ($as_dict) { |
|
| 179 | + return Okapi::formatted_response($request, $results); |
|
| 180 | + } else { |
|
| 169 | 181 | $cache_codes = array(); |
| 170 | - foreach ($results as $url => $cache_code) |
|
| 171 | - if ($cache_code != null) |
|
| 182 | + foreach ($results as $url => $cache_code) { |
|
| 183 | + if ($cache_code != null) |
|
| 172 | 184 | $cache_codes[$cache_code] = true; |
| 185 | + } |
|
| 173 | 186 | $flattened = array('results' => array_keys($cache_codes)); |
| 174 | 187 | return Okapi::formatted_response($request, $flattened); |
| 175 | 188 | } |
@@ -74,23 +74,28 @@ discard block |
||
| 74 | 74 | { |
| 75 | 75 | # Preprocess the rows. |
| 76 | 76 | |
| 77 | - if ($this->zoom >= 5) |
|
| 78 | - $this->decide_which_get_captions(); |
|
| 77 | + if ($this->zoom >= 5) { |
|
| 78 | + $this->decide_which_get_captions(); |
|
| 79 | + } |
|
| 79 | 80 | |
| 80 | 81 | # Make a background. |
| 81 | 82 | |
| 82 | 83 | $this->im = imagecreatetruecolor(256, 256); |
| 83 | 84 | imagealphablending($this->im, false); |
| 84 | - if ($this->zoom >= 13) $opacity = 15; |
|
| 85 | - elseif ($this->zoom <= 12) $opacity = max(0, $this->zoom * 2 - 14); |
|
| 85 | + if ($this->zoom >= 13) { |
|
| 86 | + $opacity = 15; |
|
| 87 | + } elseif ($this->zoom <= 12) { |
|
| 88 | + $opacity = max(0, $this->zoom * 2 - 14); |
|
| 89 | + } |
|
| 86 | 90 | $transparent = imagecolorallocatealpha($this->im, 0, 0, 0, 127 - $opacity); |
| 87 | 91 | imagefilledrectangle($this->im, 0, 0, 256, 256, $transparent); |
| 88 | 92 | imagealphablending($this->im, true); |
| 89 | 93 | |
| 90 | 94 | # Draw the caches. |
| 91 | 95 | |
| 92 | - foreach ($this->rows_ref as &$row_ref) |
|
| 93 | - $this->draw_cache($row_ref); |
|
| 96 | + foreach ($this->rows_ref as &$row_ref) { |
|
| 97 | + $this->draw_cache($row_ref); |
|
| 98 | + } |
|
| 94 | 99 | |
| 95 | 100 | # Return the result. |
| 96 | 101 | |
@@ -109,37 +114,36 @@ discard block |
||
| 109 | 114 | # Check locmem cache. |
| 110 | 115 | |
| 111 | 116 | $key = "$name/$opacity/$brightness/$contrast/$r/$g/$b"; |
| 112 | - if (!isset($locmem_cache[$key])) |
|
| 113 | - { |
|
| 117 | + if (!isset($locmem_cache[$key])) { |
|
| 114 | 118 | # Miss. Check default cache. WRTODO: upgrade to normal Cache? |
| 115 | 119 | |
| 116 | - try |
|
| 117 | - { |
|
| 120 | + try { |
|
| 118 | 121 | $cache_key = "tilesrc/".Okapi::$git_revision."/".self::$VERSION."/".$key; |
| 119 | 122 | $gd2_path = self::$USE_STATIC_IMAGE_CACHE |
| 120 | 123 | ? FileCache::get_file_path($cache_key) : null; |
| 121 | - if ($gd2_path === null) |
|
| 122 | - throw new Exception("Not in cache"); |
|
| 124 | + if ($gd2_path === null) { |
|
| 125 | + throw new Exception("Not in cache"); |
|
| 126 | + } |
|
| 123 | 127 | # File cache hit. GD2 files are much faster to read than PNGs. |
| 124 | 128 | # This can throw an Exception (see bug#160). |
| 125 | 129 | $locmem_cache[$key] = imagecreatefromgd2($gd2_path); |
| 126 | - } |
|
| 127 | - catch (Exception $e) |
|
| 128 | - { |
|
| 130 | + } catch (Exception $e) { |
|
| 129 | 131 | # Miss again (or error decoding). Read the image from PNG. |
| 130 | 132 | |
| 131 | 133 | $locmem_cache[$key] = imagecreatefrompng($GLOBALS['rootpath']."okapi/static/tilemap/$name.png"); |
| 132 | 134 | |
| 133 | 135 | # Apply all wanted effects. |
| 134 | 136 | |
| 135 | - if ($opacity != 1) |
|
| 136 | - self::change_opacity($locmem_cache[$key], $opacity); |
|
| 137 | - if ($contrast != 0) |
|
| 138 | - imagefilter($locmem_cache[$key], IMG_FILTER_CONTRAST, $contrast); |
|
| 139 | - if ($brightness != 0) |
|
| 140 | - imagefilter($locmem_cache[$key], IMG_FILTER_BRIGHTNESS, $brightness); |
|
| 141 | - if (($r != 0) || ($g != 0) || ($b != 0)) |
|
| 142 | - { |
|
| 137 | + if ($opacity != 1) { |
|
| 138 | + self::change_opacity($locmem_cache[$key], $opacity); |
|
| 139 | + } |
|
| 140 | + if ($contrast != 0) { |
|
| 141 | + imagefilter($locmem_cache[$key], IMG_FILTER_CONTRAST, $contrast); |
|
| 142 | + } |
|
| 143 | + if ($brightness != 0) { |
|
| 144 | + imagefilter($locmem_cache[$key], IMG_FILTER_BRIGHTNESS, $brightness); |
|
| 145 | + } |
|
| 146 | + if (($r != 0) || ($g != 0) || ($b != 0)) { |
|
| 143 | 147 | imagefilter($locmem_cache[$key], IMG_FILTER_GRAYSCALE); |
| 144 | 148 | imagefilter($locmem_cache[$key], IMG_FILTER_COLORIZE, $r, $g, $b); |
| 145 | 149 | } |
@@ -165,10 +169,8 @@ discard block |
||
| 165 | 169 | $w = imagesx($im); |
| 166 | 170 | $h = imagesy($im); |
| 167 | 171 | |
| 168 | - for($x = 0; $x < $w; $x++) |
|
| 169 | - { |
|
| 170 | - for($y = 0; $y < $h; $y++) |
|
| 171 | - { |
|
| 172 | + for($x = 0; $x < $w; $x++) { |
|
| 173 | + for($y = 0; $y < $h; $y++) { |
|
| 172 | 174 | $color = imagecolorat($im, $x, $y); |
| 173 | 175 | $new_color = ((max(0, floor(127 - ((127 - (($color >> 24) & 0x7f)) * $ratio))) & 0x7f) << 24) | ($color & 0x80ffffff); |
| 174 | 176 | imagesetpixel($im, $x, $y, $new_color); |
@@ -182,17 +184,17 @@ discard block |
||
| 182 | 184 | { |
| 183 | 185 | $capt = ($cache_struct[6] & TileTree::$FLAG_DRAW_CAPTION); |
| 184 | 186 | |
| 185 | - if (($this->zoom <= 8) && (!$capt)) |
|
| 186 | - $this->draw_cache_tiny($cache_struct); |
|
| 187 | - elseif (($this->zoom <= 13) && (!$capt)) |
|
| 188 | - $this->draw_cache_medium($cache_struct); |
|
| 189 | - else |
|
| 190 | - $this->draw_cache_large($cache_struct); |
|
| 187 | + if (($this->zoom <= 8) && (!$capt)) { |
|
| 188 | + $this->draw_cache_tiny($cache_struct); |
|
| 189 | + } elseif (($this->zoom <= 13) && (!$capt)) { |
|
| 190 | + $this->draw_cache_medium($cache_struct); |
|
| 191 | + } else { |
|
| 192 | + $this->draw_cache_large($cache_struct); |
|
| 193 | + } |
|
| 191 | 194 | |
| 192 | 195 | # Put caption (this flag is set only when there is plenty of space around). |
| 193 | 196 | |
| 194 | - if ($capt) |
|
| 195 | - { |
|
| 197 | + if ($capt) { |
|
| 196 | 198 | $caption = $this->get_caption($cache_struct[0], $cache_struct[7]); |
| 197 | 199 | imagecopy($this->im, $caption, $cache_struct[1] - 32, $cache_struct[2] + 6, 0, 0, 64, 26); |
| 198 | 200 | } |
@@ -241,8 +243,9 @@ discard block |
||
| 241 | 243 | $markercenter_x = 12; |
| 242 | 244 | $markercenter_y = 12; |
| 243 | 245 | |
| 244 | - if ($count > 1) |
|
| 245 | - imagecopy($this->im, $outer_marker, $px - $center_x + 3, $py - $center_y - 2, 0, 0, $width, $height); |
|
| 246 | + if ($count > 1) { |
|
| 247 | + imagecopy($this->im, $outer_marker, $px - $center_x + 3, $py - $center_y - 2, 0, 0, $width, $height); |
|
| 248 | + } |
|
| 246 | 249 | imagecopy($this->im, $outer_marker, $px - $center_x, $py - $center_y, 0, 0, $width, $height); |
| 247 | 250 | |
| 248 | 251 | # Put the inner marker (indicates the type). |
@@ -253,8 +256,7 @@ discard block |
||
| 253 | 256 | |
| 254 | 257 | # If the cache is unavailable, mark it with X. |
| 255 | 258 | |
| 256 | - if (($status != 1) && ($count == 1)) |
|
| 257 | - { |
|
| 259 | + if (($status != 1) && ($count == 1)) { |
|
| 258 | 260 | $icon = self::get_image(($status == 2) ? "status_unavailable" |
| 259 | 261 | : "status_archived", $a); |
| 260 | 262 | imagecopy($this->im, $icon, $px - 1, $py - $center_y - 4, 0, 0, 16, 16); |
@@ -262,10 +264,8 @@ discard block |
||
| 262 | 264 | |
| 263 | 265 | # Put the rating smile. :) |
| 264 | 266 | |
| 265 | - if ($status == 1) |
|
| 266 | - { |
|
| 267 | - if ($rating >= 4.2) |
|
| 268 | - { |
|
| 267 | + if ($status == 1) { |
|
| 268 | + if ($rating >= 4.2) { |
|
| 269 | 269 | if ($flags & TileTree::$FLAG_STAR) { |
| 270 | 270 | $icon = self::get_image("rating_grin", $a, $br, $c, $r, $g, $b); |
| 271 | 271 | imagecopy($this->im, $icon, $px - 7 - 6, $py - $center_y - 8, 0, 0, 16, 16); |
@@ -285,8 +285,7 @@ discard block |
||
| 285 | 285 | |
| 286 | 286 | # Mark found caches with V. |
| 287 | 287 | |
| 288 | - if ($found) |
|
| 289 | - { |
|
| 288 | + if ($found) { |
|
| 290 | 289 | $icon = self::get_image("found", 0.7*$a, $br, $c, $r, $g, $b); |
| 291 | 290 | imagecopy($this->im, $icon, $px - 2, $py - $center_y - 3, 0, 0, 16, 16); |
| 292 | 291 | } |
@@ -304,8 +303,9 @@ discard block |
||
| 304 | 303 | $nextBonus = ""; |
| 305 | 304 | for ($i=0; ($i<count($words)) || (mb_strlen($nextBonus)>0); $i++) { |
| 306 | 305 | $word = isset($words[$i])?$words[$i]:""; |
| 307 | - if (mb_strlen($nextBonus) > 0) |
|
| 308 | - $word = $nextBonus." ".$word; |
|
| 306 | + if (mb_strlen($nextBonus) > 0) { |
|
| 307 | + $word = $nextBonus." ".$word; |
|
| 308 | + } |
|
| 309 | 309 | $nextBonus = ""; |
| 310 | 310 | while (true) { |
| 311 | 311 | $bbox = imagettfbbox($size, 0, $font, $line.$word); |
@@ -324,8 +324,9 @@ discard block |
||
| 324 | 324 | continue; |
| 325 | 325 | } |
| 326 | 326 | } |
| 327 | - if (mb_strlen($line) > 0) |
|
| 328 | - $lines[] = trim($line); |
|
| 327 | + if (mb_strlen($line) > 0) { |
|
| 328 | + $lines[] = trim($line); |
|
| 329 | + } |
|
| 329 | 330 | return implode("\n", $lines); |
| 330 | 331 | } |
| 331 | 332 | |
@@ -338,8 +339,7 @@ discard block |
||
| 338 | 339 | |
| 339 | 340 | $cache_key = "tilecaption/".self::$VERSION."/".$cache_id."/".$name_crc; |
| 340 | 341 | $gd2 = self::$USE_CAPTIONS_CACHE ? Cache::get($cache_key) : null; |
| 341 | - if ($gd2 === null) |
|
| 342 | - { |
|
| 342 | + if ($gd2 === null) { |
|
| 343 | 343 | # We'll work with 16x bigger image to get smoother interpolation. |
| 344 | 344 | |
| 345 | 345 | $im = imagecreatetruecolor(64*4, 26*4); |
@@ -367,19 +367,16 @@ discard block |
||
| 367 | 367 | |
| 368 | 368 | $y = 0; |
| 369 | 369 | $positions = array(); |
| 370 | - foreach ($lines as $line) |
|
| 371 | - { |
|
| 370 | + foreach ($lines as $line) { |
|
| 372 | 371 | $bbox = imagettfbbox($size, 0, $font, $line); |
| 373 | 372 | $width = $bbox[2]-$bbox[0]; |
| 374 | 373 | $x = 128 - ($width >> 1); |
| 375 | 374 | $positions[] = array($x, $y); |
| 376 | 375 | $y += 36; |
| 377 | 376 | } |
| 378 | - $drawer = function($x, $y, $color) use (&$lines, &$positions, &$im, &$size, &$font) |
|
| 379 | - { |
|
| 377 | + $drawer = function($x, $y, $color) use (&$lines, &$positions, &$im, &$size, &$font) { |
|
| 380 | 378 | $len = count($lines); |
| 381 | - for ($i=0; $i<$len; $i++) |
|
| 382 | - { |
|
| 379 | + for ($i=0; $i<$len; $i++) { |
|
| 383 | 380 | $line = $lines[$i]; |
| 384 | 381 | list($offset_x, $offset_y) = $positions[$i]; |
| 385 | 382 | imagettftext($im, $size, 0, $offset_x + $x, $offset_y + $y, $color, $font, $line); |
@@ -389,9 +386,12 @@ discard block |
||
| 389 | 386 | # Draw an outline. |
| 390 | 387 | |
| 391 | 388 | $outline_color = imagecolorallocatealpha($im, 255, 255, 255, 80); |
| 392 | - for ($x=0; $x<=12; $x+=3) |
|
| 393 | - for ($y=$size-3; $y<=$size+9; $y+=3) |
|
| 394 | - $drawer($x, $y, $outline_color); |
|
| 389 | + for ($x=0; $x<=12; $x+=3) { |
|
| 390 | + for ($y=$size-3; |
|
| 391 | + } |
|
| 392 | + $y<=$size+9; $y+=3) { |
|
| 393 | + $drawer($x, $y, $outline_color); |
|
| 394 | + } |
|
| 395 | 395 | |
| 396 | 396 | # Add a slight shadow effect (on top of the outline). |
| 397 | 397 | |
@@ -426,10 +426,11 @@ discard block |
||
| 426 | 426 | $found = $flags & TileTree::$FLAG_FOUND; |
| 427 | 427 | $own = $flags & TileTree::$FLAG_OWN; |
| 428 | 428 | $new = $flags & TileTree::$FLAG_NEW; |
| 429 | - if ($found && (!($flags & TileTree::$FLAG_DRAW_CAPTION))) |
|
| 430 | - $a = .35; |
|
| 431 | - else |
|
| 432 | - $a = 1; |
|
| 429 | + if ($found && (!($flags & TileTree::$FLAG_DRAW_CAPTION))) { |
|
| 430 | + $a = .35; |
|
| 431 | + } else { |
|
| 432 | + $a = 1; |
|
| 433 | + } |
|
| 433 | 434 | |
| 434 | 435 | # Put the marker (indicates the type). |
| 435 | 436 | |
@@ -441,20 +442,19 @@ discard block |
||
| 441 | 442 | $markercenter_x = 7; |
| 442 | 443 | $markercenter_y = 8; |
| 443 | 444 | |
| 444 | - if ($count > 1) |
|
| 445 | - { |
|
| 445 | + if ($count > 1) { |
|
| 446 | 446 | imagecopy($this->im, $marker, $px - $center_x + 3, $py - $center_y - 2, 0, 0, $width, $height); |
| 447 | 447 | imagecopy($this->im, $marker, $px - $center_x, $py - $center_y, 0, 0, $width, $height); |
| 448 | - } |
|
| 449 | - elseif ($status == 1) # don't put the marker for unavailable caches (X only) |
|
| 448 | + } elseif ($status == 1) { |
|
| 449 | + # don't put the marker for unavailable caches (X only) |
|
| 450 | 450 | { |
| 451 | 451 | imagecopy($this->im, $marker, $px - $center_x, $py - $center_y, 0, 0, $width, $height); |
| 452 | 452 | } |
| 453 | + } |
|
| 453 | 454 | |
| 454 | 455 | # If the cache is unavailable, mark it with X. |
| 455 | 456 | |
| 456 | - if (($status != 1) && ($count == 1)) |
|
| 457 | - { |
|
| 457 | + if (($status != 1) && ($count == 1)) { |
|
| 458 | 458 | $icon = self::get_image(($status == 2) ? "status_unavailable" |
| 459 | 459 | : "status_archived"); |
| 460 | 460 | imagecopy($this->im, $icon, $px - ($center_x - $markercenter_x) - 6, |
@@ -463,10 +463,8 @@ discard block |
||
| 463 | 463 | |
| 464 | 464 | # Put small versions of rating icons. |
| 465 | 465 | |
| 466 | - if ($status == 1) |
|
| 467 | - { |
|
| 468 | - if ($rating >= 4.2) |
|
| 469 | - { |
|
| 466 | + if ($status == 1) { |
|
| 467 | + if ($rating >= 4.2) { |
|
| 470 | 468 | if ($flags & TileTree::$FLAG_STAR) { |
| 471 | 469 | $icon = self::get_image("rating_grin_small", max(0.6, $a)); |
| 472 | 470 | imagecopy($this->im, $icon, $px - 5, $py - $center_y - 1, 0, 0, 6, 6); |
@@ -479,23 +477,18 @@ discard block |
||
| 479 | 477 | } |
| 480 | 478 | } |
| 481 | 479 | |
| 482 | - if ($own) |
|
| 483 | - { |
|
| 480 | + if ($own) { |
|
| 484 | 481 | # Mark own caches with additional overlay. |
| 485 | 482 | |
| 486 | 483 | $overlay = self::get_image("medium_overlay_own"); |
| 487 | 484 | imagecopy($this->im, $overlay, $px - $center_x, $py - $center_y, 0, 0, $width, $height); |
| 488 | - } |
|
| 489 | - elseif ($found) |
|
| 490 | - { |
|
| 485 | + } elseif ($found) { |
|
| 491 | 486 | # Mark found caches with V. |
| 492 | 487 | |
| 493 | 488 | $icon = self::get_image("found", 0.7*$a); |
| 494 | 489 | imagecopy($this->im, $icon, $px - ($center_x - $markercenter_x) - 7, |
| 495 | 490 | $py - ($center_y - $markercenter_y) - 9, 0, 0, 16, 16); |
| 496 | - } |
|
| 497 | - elseif ($new) |
|
| 498 | - { |
|
| 491 | + } elseif ($new) { |
|
| 499 | 492 | # Mark new caches with additional overlay. |
| 500 | 493 | |
| 501 | 494 | $overlay = self::get_image("medium_overlay_new"); |
@@ -513,8 +506,7 @@ discard block |
||
| 513 | 506 | case 4: return 'virtual'; |
| 514 | 507 | case 1: return 'unknown'; |
| 515 | 508 | } |
| 516 | - if ($extended_set) |
|
| 517 | - { |
|
| 509 | + if ($extended_set) { |
|
| 518 | 510 | switch ($type) { |
| 519 | 511 | case 10: return 'own'; |
| 520 | 512 | case 8: return 'moving'; |
@@ -542,20 +534,16 @@ discard block |
||
| 542 | 534 | |
| 543 | 535 | # Put the marker. If cache covers more caches, then put two markers instead of one. |
| 544 | 536 | |
| 545 | - if ($count > 1) |
|
| 546 | - { |
|
| 537 | + if ($count > 1) { |
|
| 547 | 538 | imagecopy($this->im, $marker, $px - $center_x + 3, $py - $center_y - 2, 0, 0, $width, $height); |
| 548 | 539 | imagecopy($this->im, $marker, $px - $center_x, $py - $center_y, 0, 0, $width, $height); |
| 549 | - } |
|
| 550 | - elseif ($status == 1) |
|
| 551 | - { |
|
| 540 | + } elseif ($status == 1) { |
|
| 552 | 541 | imagecopy($this->im, $marker, $px - $center_x, $py - $center_y, 0, 0, $width, $height); |
| 553 | 542 | } |
| 554 | 543 | |
| 555 | 544 | # If the cache is unavailable, mark it with X. |
| 556 | 545 | |
| 557 | - if (($status != 1) && ($count == 1)) |
|
| 558 | - { |
|
| 546 | + if (($status != 1) && ($count == 1)) { |
|
| 559 | 547 | $icon = self::get_image(($status == 2) ? "status_unavailable" |
| 560 | 548 | : "status_archived"); |
| 561 | 549 | imagecopy($this->im, $icon, $px - ($center_x - $markercenter_x) - 6, |
@@ -577,25 +565,30 @@ discard block |
||
| 577 | 565 | # This is efficient and yields acceptable results. |
| 578 | 566 | |
| 579 | 567 | $matrix = array(); |
| 580 | - for ($i=0; $i<12; $i++) |
|
| 581 | - $matrix[] = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
|
| 568 | + for ($i=0; $i<12; $i++) { |
|
| 569 | + $matrix[] = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
|
| 570 | + } |
|
| 582 | 571 | |
| 583 | - foreach ($this->rows_ref as &$row_ref) |
|
| 584 | - { |
|
| 572 | + foreach ($this->rows_ref as &$row_ref) { |
|
| 585 | 573 | $mx = ($row_ref[1] + 64) >> 5; |
| 586 | 574 | $my = ($row_ref[2] + 64) >> 5; |
| 587 | - if (($mx >= 12) || ($my >= 12)) continue; |
|
| 588 | - if (($matrix[$mx][$my] === 0) && ($row_ref[8] == 1)) # 8 is count |
|
| 589 | - $matrix[$mx][$my] = $row_ref[0]; # 0 is cache_id |
|
| 590 | - else |
|
| 591 | - $matrix[$mx][$my] = -1; |
|
| 575 | + if (($mx >= 12) || ($my >= 12)) { |
|
| 576 | + continue; |
|
| 577 | + } |
|
| 578 | + if (($matrix[$mx][$my] === 0) && ($row_ref[8] == 1)) { |
|
| 579 | + # 8 is count |
|
| 580 | + $matrix[$mx][$my] = $row_ref[0]; |
|
| 581 | + } |
|
| 582 | + # 0 is cache_id |
|
| 583 | + else { |
|
| 584 | + $matrix[$mx][$my] = -1; |
|
| 585 | + } |
|
| 592 | 586 | } |
| 593 | 587 | $selected_cache_ids = array(); |
| 594 | - for ($mx=1; $mx<11; $mx++) |
|
| 595 | - { |
|
| 596 | - for ($my=1; $my<11; $my++) |
|
| 597 | - { |
|
| 598 | - if ($matrix[$mx][$my] > 0) # cache_id |
|
| 588 | + for ($mx=1; $mx<11; $mx++) { |
|
| 589 | + for ($my=1; $my<11; $my++) { |
|
| 590 | + if ($matrix[$mx][$my] > 0) { |
|
| 591 | + # cache_id |
|
| 599 | 592 | { |
| 600 | 593 | # Check all adjacent squares. |
| 601 | 594 | |
@@ -610,12 +603,14 @@ discard block |
||
| 610 | 603 | ) |
| 611 | 604 | $selected_cache_ids[] = $matrix[$mx][$my]; |
| 612 | 605 | } |
| 606 | + } |
|
| 613 | 607 | } |
| 614 | 608 | } |
| 615 | 609 | |
| 616 | - foreach ($this->rows_ref as &$row_ref) |
|
| 617 | - if (in_array($row_ref[0], $selected_cache_ids)) |
|
| 610 | + foreach ($this->rows_ref as &$row_ref) { |
|
| 611 | + if (in_array($row_ref[0], $selected_cache_ids)) |
|
| 618 | 612 | $row_ref[6] |= TileTree::$FLAG_DRAW_CAPTION; |
| 613 | + } |
|
| 619 | 614 | } |
| 620 | 615 | |
| 621 | 616 | } |
| 622 | 617 | \ No newline at end of file |
@@ -64,19 +64,23 @@ discard block |
||
| 64 | 64 | # (and if it was, was it empty). |
| 65 | 65 | |
| 66 | 66 | $status = self::get_tile_status($zoom, $x, $y); |
| 67 | - if ($status === null) # Not yet computed. |
|
| 67 | + if ($status === null) { |
|
| 68 | + # Not yet computed. |
|
| 68 | 69 | { |
| 69 | 70 | # Note, that computing the tile does not involve taking any |
| 70 | 71 | # search parameters. |
| 71 | 72 | |
| 72 | 73 | $status = self::compute_tile($zoom, $x, $y); |
| 73 | 74 | } |
| 75 | + } |
|
| 74 | 76 | |
| 75 | - if ($status === 1) # Computed and empty. |
|
| 77 | + if ($status === 1) { |
|
| 78 | + # Computed and empty. |
|
| 76 | 79 | { |
| 77 | 80 | # This tile was already computed and it is empty. |
| 78 | 81 | return null; |
| 79 | 82 | } |
| 83 | + } |
|
| 80 | 84 | |
| 81 | 85 | # If we got here, then the tile is computed and not empty (status 2). |
| 82 | 86 | |
@@ -121,11 +125,11 @@ discard block |
||
| 121 | 125 | # For low-level tiles, this can be expensive. |
| 122 | 126 | |
| 123 | 127 | $status = self::get_tile_status($zoom, $x, $y); |
| 124 | - if ($status !== null) |
|
| 125 | - return $status; |
|
| 128 | + if ($status !== null) { |
|
| 129 | + return $status; |
|
| 130 | + } |
|
| 126 | 131 | |
| 127 | - if ($zoom === 0) |
|
| 128 | - { |
|
| 132 | + if ($zoom === 0) { |
|
| 129 | 133 | # When computing zoom zero, we don't have a parent to speed up |
| 130 | 134 | # the computation. We need to use the caches table. Note, that |
| 131 | 135 | # zoom level 0 contains *entire world*, so we don't have to use |
@@ -150,8 +154,7 @@ discard block |
||
| 150 | 154 | $internal_request->skip_limits = true; |
| 151 | 155 | $caches = OkapiServiceRunner::call("services/caches/geocaches", $internal_request); |
| 152 | 156 | |
| 153 | - foreach ($caches as $cache) |
|
| 154 | - { |
|
| 157 | + foreach ($caches as $cache) { |
|
| 155 | 158 | $row = self::generate_short_row($cache); |
| 156 | 159 | if (!$row) { |
| 157 | 160 | /* Some caches cannot be included, e.g. the ones near the poles. */ |
@@ -174,9 +177,7 @@ discard block |
||
| 174 | 177 | "); |
| 175 | 178 | } |
| 176 | 179 | $status = 2; |
| 177 | - } |
|
| 178 | - else |
|
| 179 | - { |
|
| 180 | + } else { |
|
| 180 | 181 | # We will use the parent tile to compute the contents of this tile. |
| 181 | 182 | |
| 182 | 183 | $parent_zoom = $zoom - 1; |
@@ -184,19 +185,23 @@ discard block |
||
| 184 | 185 | $parent_y = $y >> 1; |
| 185 | 186 | |
| 186 | 187 | $status = self::get_tile_status($parent_zoom, $parent_x, $parent_y); |
| 187 | - if ($status === null) # Not computed. |
|
| 188 | + if ($status === null) { |
|
| 189 | + # Not computed. |
|
| 188 | 190 | { |
| 189 | 191 | $time_started = microtime(true); |
| 192 | + } |
|
| 190 | 193 | $status = self::compute_tile($parent_zoom, $parent_x, $parent_y); |
| 191 | 194 | } |
| 192 | 195 | |
| 193 | - if ($status === 1) # Computed and empty. |
|
| 196 | + if ($status === 1) { |
|
| 197 | + # Computed and empty. |
|
| 194 | 198 | { |
| 195 | 199 | # No need to check. |
| 196 | 200 | } |
| 197 | 201 | else # Computed, not empty. |
| 198 | 202 | { |
| 199 | 203 | $scale = 8 + 21 - $zoom; |
| 204 | + } |
|
| 200 | 205 | $parentcenter_z21x = (($parent_x << 1) | 1) << $scale; |
| 201 | 206 | $parentcenter_z21y = (($parent_y << 1) | 1) << $scale; |
| 202 | 207 | $margin = 1 << ($scale - 2); |
@@ -209,14 +214,20 @@ discard block |
||
| 209 | 214 | # |1 2| |
| 210 | 215 | # |3 4| |
| 211 | 216 | |
| 212 | - if ($x & 1) # 2 or 4 |
|
| 217 | + if ($x & 1) { |
|
| 218 | + # 2 or 4 |
|
| 213 | 219 | $left_z21x = $parentcenter_z21x - $margin; |
| 214 | - else # 1 or 3 |
|
| 220 | + } else { |
|
| 221 | + # 1 or 3 |
|
| 215 | 222 | $right_z21x = $parentcenter_z21x + $margin; |
| 216 | - if ($y & 1) # 3 or 4 |
|
| 223 | + } |
|
| 224 | + if ($y & 1) { |
|
| 225 | + # 3 or 4 |
|
| 217 | 226 | $top_z21y = $parentcenter_z21y - $margin; |
| 218 | - else # 1 or 2 |
|
| 227 | + } else { |
|
| 228 | + # 1 or 2 |
|
| 219 | 229 | $bottom_z21y = $parentcenter_z21y + $margin; |
| 230 | + } |
|
| 220 | 231 | |
| 221 | 232 | # Cache the result. |
| 222 | 233 | |
@@ -256,10 +267,11 @@ discard block |
||
| 256 | 267 | and y = '".Db::escape_string($y)."' |
| 257 | 268 | limit 1; |
| 258 | 269 | "); |
| 259 | - if ($test) |
|
| 260 | - $status = 2; |
|
| 261 | - else |
|
| 262 | - $status = 1; |
|
| 270 | + if ($test) { |
|
| 271 | + $status = 2; |
|
| 272 | + } else { |
|
| 273 | + $status = 1; |
|
| 274 | + } |
|
| 263 | 275 | } |
| 264 | 276 | } |
| 265 | 277 | |
@@ -294,12 +306,15 @@ discard block |
||
| 294 | 306 | return false; |
| 295 | 307 | } |
| 296 | 308 | $flags = 0; |
| 297 | - if (($cache['founds'] > 6) && (($cache['recommendations'] / $cache['founds']) > 0.3)) |
|
| 298 | - $flags |= self::$FLAG_STAR; |
|
| 299 | - if ($cache['trackables_count'] > 0) |
|
| 300 | - $flags |= self::$FLAG_HAS_TRACKABLES; |
|
| 301 | - if ($cache['founds'] == 0) |
|
| 302 | - $flags |= self::$FLAG_NOT_YET_FOUND; |
|
| 309 | + if (($cache['founds'] > 6) && (($cache['recommendations'] / $cache['founds']) > 0.3)) { |
|
| 310 | + $flags |= self::$FLAG_STAR; |
|
| 311 | + } |
|
| 312 | + if ($cache['trackables_count'] > 0) { |
|
| 313 | + $flags |= self::$FLAG_HAS_TRACKABLES; |
|
| 314 | + } |
|
| 315 | + if ($cache['founds'] == 0) { |
|
| 316 | + $flags |= self::$FLAG_NOT_YET_FOUND; |
|
| 317 | + } |
|
| 303 | 318 | return array($cache['internal_id'], $z21x, $z21y, Okapi::cache_status_name2id($cache['status']), |
| 304 | 319 | Okapi::cache_type_name2id($cache['type']), $cache['rating'], $flags, |
| 305 | 320 | self::compute_name_crc($cache['name'])); |
@@ -29,14 +29,13 @@ discard block |
||
| 29 | 29 | # changelog. The format of $changelog is described in the replicate module |
| 30 | 30 | # (NOT the entire response, just the "changelog" key). |
| 31 | 31 | |
| 32 | - foreach ($changelog as $c) |
|
| 33 | - { |
|
| 34 | - if ($c['object_type'] == 'geocache') |
|
| 35 | - { |
|
| 36 | - if ($c['change_type'] == 'replace') |
|
| 37 | - self::handle_geocache_replace($c); |
|
| 38 | - else |
|
| 39 | - self::handle_geocache_delete($c); |
|
| 32 | + foreach ($changelog as $c) { |
|
| 33 | + if ($c['object_type'] == 'geocache') { |
|
| 34 | + if ($c['change_type'] == 'replace') { |
|
| 35 | + self::handle_geocache_replace($c); |
|
| 36 | + } else { |
|
| 37 | + self::handle_geocache_delete($c); |
|
| 38 | + } |
|
| 40 | 39 | } |
| 41 | 40 | } |
| 42 | 41 | } |
@@ -90,25 +89,21 @@ discard block |
||
| 90 | 89 | # Compute the new row for okapi_tile_caches. Compare with the old one. |
| 91 | 90 | |
| 92 | 91 | $theirs = TileTree::generate_short_row($cache); |
| 93 | - if (!$ours) |
|
| 94 | - { |
|
| 92 | + if (!$ours) { |
|
| 95 | 93 | # Aaah, a new geocache! How nice... ;) |
| 96 | 94 | |
| 97 | 95 | self::add_geocache_to_cached_tiles($theirs); |
| 98 | - } |
|
| 99 | - elseif (($ours[1] != $theirs[1]) || ($ours[2] != $theirs[2])) # z21x & z21y fields |
|
| 96 | + } elseif (($ours[1] != $theirs[1]) || ($ours[2] != $theirs[2])) { |
|
| 97 | + # z21x & z21y fields |
|
| 100 | 98 | { |
| 101 | 99 | # Location changed. |
| 102 | 100 | |
| 103 | 101 | self::remove_geocache_from_cached_tiles($ours[0]); |
| 104 | - self::add_geocache_to_cached_tiles($theirs); |
|
| 105 | 102 | } |
| 106 | - elseif ($ours != $theirs) |
|
| 107 | - { |
|
| 103 | + self::add_geocache_to_cached_tiles($theirs); |
|
| 104 | + } elseif ($ours != $theirs) { |
|
| 108 | 105 | self::update_geocache_attributes_in_cached_tiles($theirs); |
| 109 | - } |
|
| 110 | - else |
|
| 111 | - { |
|
| 106 | + } else { |
|
| 112 | 107 | # No need to update anything. This is very common (i.e. when the |
| 113 | 108 | # cache was simply found, not actually changed). Replicate module generates |
| 114 | 109 | # many updates which do not influence our cache. |
@@ -147,20 +142,21 @@ discard block |
||
| 147 | 142 | $z21y = $row[2]; |
| 148 | 143 | $ex = $z21x >> 8; # initially, z21x / <tile width> |
| 149 | 144 | $ey = $z21y >> 8; # initially, z21y / <tile height> |
| 150 | - for ($zoom = 21; $zoom >= 0; $zoom--, $ex >>= 1, $ey >>= 1) |
|
| 151 | - { |
|
| 145 | + for ($zoom = 21; $zoom >= 0; $zoom--, $ex >>= 1, $ey >>= 1) { |
|
| 152 | 146 | # ($ex, $ey) points to the "exact match" tile. We need to determine |
| 153 | 147 | # tile-range to check for "just outside the border" tiles. We will |
| 154 | 148 | # go with the simple approach and check all 1+8 bordering tiles. |
| 155 | 149 | |
| 156 | 150 | $tiles_in_this_region = array(); |
| 157 | - for ($x=$ex-1; $x<=$ex+1; $x++) |
|
| 158 | - for ($y=$ey-1; $y<=$ey+1; $y++) |
|
| 159 | - if (($x >= 0) && ($x < 1<<$zoom) && ($y >= 0) && ($y < 1<<$zoom)) |
|
| 151 | + for ($x=$ex-1; $x<=$ex+1; $x++) { |
|
| 152 | + for ($y=$ey-1; |
|
| 153 | + } |
|
| 154 | + $y<=$ey+1; $y++) { |
|
| 155 | + if (($x >= 0) && ($x < 1<<$zoom) && ($y >= 0) && ($y < 1<<$zoom)) |
|
| 160 | 156 | $tiles_in_this_region[] = array($x, $y); |
| 157 | + } |
|
| 161 | 158 | |
| 162 | - foreach ($tiles_in_this_region as $coords) |
|
| 163 | - { |
|
| 159 | + foreach ($tiles_in_this_region as $coords) { |
|
| 164 | 160 | list($x, $y) = $coords; |
| 165 | 161 | |
| 166 | 162 | $scale = 8 + 21 - $zoom; |
@@ -171,14 +167,18 @@ discard block |
||
| 171 | 167 | $top_z21y = ($y << $scale) - $margin; |
| 172 | 168 | $bottom_z21y = (($y + 1) << $scale) + $margin; |
| 173 | 169 | |
| 174 | - if ($z21x < $left_z21x) |
|
| 175 | - continue; |
|
| 176 | - if ($z21x > $right_z21x) |
|
| 177 | - continue; |
|
| 178 | - if ($z21y < $top_z21y) |
|
| 179 | - continue; |
|
| 180 | - if ($z21y > $bottom_z21y) |
|
| 181 | - continue; |
|
| 170 | + if ($z21x < $left_z21x) { |
|
| 171 | + continue; |
|
| 172 | + } |
|
| 173 | + if ($z21x > $right_z21x) { |
|
| 174 | + continue; |
|
| 175 | + } |
|
| 176 | + if ($z21y < $top_z21y) { |
|
| 177 | + continue; |
|
| 178 | + } |
|
| 179 | + if ($z21y > $bottom_z21y) { |
|
| 180 | + continue; |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | 183 | # We found a match. Store it for later. |
| 184 | 184 | |
@@ -191,8 +191,7 @@ discard block |
||
| 191 | 191 | # only the cached ones. |
| 192 | 192 | |
| 193 | 193 | $alternatives_escaped = array(); |
| 194 | - foreach ($tiles_to_update as $coords) |
|
| 195 | - { |
|
| 194 | + foreach ($tiles_to_update as $coords) { |
|
| 196 | 195 | list($z, $x, $y) = $coords; |
| 197 | 196 | $alternatives_escaped[] = "( |
| 198 | 197 | z = '".Db::escape_string($z)."' |
@@ -200,8 +199,7 @@ discard block |
||
| 200 | 199 | and y = '".Db::escape_string($y)."' |
| 201 | 200 | )"; |
| 202 | 201 | } |
| 203 | - if (count($alternatives_escaped) > 0) |
|
| 204 | - { |
|
| 202 | + if (count($alternatives_escaped) > 0) { |
|
| 205 | 203 | Db::execute(" |
| 206 | 204 | replace into okapi_tile_caches ( |
| 207 | 205 | z, x, y, cache_id, z21x, z21y, status, type, rating, flags, name_crc |
@@ -58,11 +58,13 @@ discard block |
||
| 58 | 58 | private static function require_uint($request, $name, $min_value = 0) |
| 59 | 59 | { |
| 60 | 60 | $val = $request->get_parameter($name); |
| 61 | - if ($val === null) |
|
| 62 | - throw new ParamMissing($name); |
|
| 61 | + if ($val === null) { |
|
| 62 | + throw new ParamMissing($name); |
|
| 63 | + } |
|
| 63 | 64 | $ret = intval($val); |
| 64 | - if ($ret < 0 || ("$ret" !== $val)) |
|
| 65 | - throw new InvalidParam($name, "Expecting non-negative integer."); |
|
| 65 | + if ($ret < 0 || ("$ret" !== $val)) { |
|
| 66 | + throw new InvalidParam($name, "Expecting non-negative integer."); |
|
| 67 | + } |
|
| 66 | 68 | return $ret; |
| 67 | 69 | } |
| 68 | 70 | |
@@ -84,14 +86,17 @@ discard block |
||
| 84 | 86 | # zoom, x, y - required tile-specific parameters. |
| 85 | 87 | |
| 86 | 88 | $zoom = self::require_uint($request, 'z'); |
| 87 | - if ($zoom > 21) |
|
| 88 | - throw new InvalidParam('z', "Maximum value for this parameter is 21."); |
|
| 89 | + if ($zoom > 21) { |
|
| 90 | + throw new InvalidParam('z', "Maximum value for this parameter is 21."); |
|
| 91 | + } |
|
| 89 | 92 | $x = self::require_uint($request, 'x'); |
| 90 | 93 | $y = self::require_uint($request, 'y'); |
| 91 | - if ($x >= 1<<$zoom) |
|
| 92 | - throw new InvalidParam('x', "Should be in 0..".((1<<$zoom) - 1)."."); |
|
| 93 | - if ($y >= 1<<$zoom) |
|
| 94 | - throw new InvalidParam('y', "Should be in 0..".((1<<$zoom) - 1)."."); |
|
| 94 | + if ($x >= 1<<$zoom) { |
|
| 95 | + throw new InvalidParam('x', "Should be in 0..".((1<<$zoom) - 1)."."); |
|
| 96 | + } |
|
| 97 | + if ($y >= 1<<$zoom) { |
|
| 98 | + throw new InvalidParam('y', "Should be in 0..".((1<<$zoom) - 1)."."); |
|
| 99 | + } |
|
| 95 | 100 | |
| 96 | 101 | # Now, we will create a search set (or use one previously created). |
| 97 | 102 | # Instead of creating a new OkapiInternalRequest object, we will pass |
@@ -112,10 +117,10 @@ discard block |
||
| 112 | 117 | |
| 113 | 118 | $rs = TileTree::query_fast($zoom, $x, $y, $set_id); |
| 114 | 119 | $rows = array(); |
| 115 | - if ($rs !== null) |
|
| 116 | - { |
|
| 117 | - while ($row = Db::fetch_row($rs)) |
|
| 118 | - $rows[] = $row; |
|
| 120 | + if ($rs !== null) { |
|
| 121 | + while ($row = Db::fetch_row($rs)) { |
|
| 122 | + $rows[] = $row; |
|
| 123 | + } |
|
| 119 | 124 | unset($row); |
| 120 | 125 | } |
| 121 | 126 | OkapiServiceRunner::save_stats_extra("caches/map/tile/checkpointA", null, |
@@ -124,14 +129,12 @@ discard block |
||
| 124 | 129 | |
| 125 | 130 | # Add dynamic, user-related flags. |
| 126 | 131 | |
| 127 | - if (count($rows) > 0) |
|
| 128 | - { |
|
| 132 | + if (count($rows) > 0) { |
|
| 129 | 133 | # Load user-related cache ids. |
| 130 | 134 | |
| 131 | 135 | $cache_key = "tileuser/".$request->token->user_id; |
| 132 | 136 | $user = self::$USE_OTHER_CACHE ? Cache::get($cache_key) : null; |
| 133 | - if ($user === null) |
|
| 134 | - { |
|
| 137 | + if ($user === null) { |
|
| 135 | 138 | $user = array(); |
| 136 | 139 | |
| 137 | 140 | # Ignored caches. |
@@ -142,8 +145,9 @@ discard block |
||
| 142 | 145 | where user_id = '".Db::escape_string($request->token->user_id)."' |
| 143 | 146 | "); |
| 144 | 147 | $user['ignored'] = array(); |
| 145 | - while (list($cache_id) = Db::fetch_row($rs)) |
|
| 146 | - $user['ignored'][$cache_id] = true; |
|
| 148 | + while (list($cache_id) = Db::fetch_row($rs)) { |
|
| 149 | + $user['ignored'][$cache_id] = true; |
|
| 150 | + } |
|
| 147 | 151 | |
| 148 | 152 | # Found caches. |
| 149 | 153 | |
@@ -156,8 +160,9 @@ discard block |
||
| 156 | 160 | and ".((Settings::get('OC_BRANCH') == 'oc.pl') ? "deleted = 0" : "true")." |
| 157 | 161 | "); |
| 158 | 162 | $user['found'] = array(); |
| 159 | - while (list($cache_id) = Db::fetch_row($rs)) |
|
| 160 | - $user['found'][$cache_id] = true; |
|
| 163 | + while (list($cache_id) = Db::fetch_row($rs)) { |
|
| 164 | + $user['found'][$cache_id] = true; |
|
| 165 | + } |
|
| 161 | 166 | |
| 162 | 167 | # Own caches. |
| 163 | 168 | |
@@ -167,24 +172,28 @@ discard block |
||
| 167 | 172 | where user_id = '".Db::escape_string($request->token->user_id)."' |
| 168 | 173 | "); |
| 169 | 174 | $user['own'] = array(); |
| 170 | - while (list($cache_id) = Db::fetch_row($rs)) |
|
| 171 | - $user['own'][$cache_id] = true; |
|
| 175 | + while (list($cache_id) = Db::fetch_row($rs)) { |
|
| 176 | + $user['own'][$cache_id] = true; |
|
| 177 | + } |
|
| 172 | 178 | |
| 173 | 179 | Cache::set($cache_key, $user, 30); |
| 174 | 180 | } |
| 175 | 181 | |
| 176 | 182 | # Add extra flags to geocaches. |
| 177 | 183 | |
| 178 | - foreach ($rows as &$row_ref) |
|
| 179 | - { |
|
| 184 | + foreach ($rows as &$row_ref) { |
|
| 180 | 185 | # Add the "found" flag (to indicate that this cache needs |
| 181 | 186 | # to be drawn as found) and the "own" flag (to indicate that |
| 182 | 187 | # the current user is the owner). |
| 183 | 188 | |
| 184 | - if (isset($user['found'][$row_ref[0]])) |
|
| 185 | - $row_ref[6] |= TileTree::$FLAG_FOUND; # $row[6] is "flags" |
|
| 186 | - if (isset($user['own'][$row_ref[0]])) |
|
| 187 | - $row_ref[6] |= TileTree::$FLAG_OWN; # $row[6] is "flags" |
|
| 189 | + if (isset($user['found'][$row_ref[0]])) { |
|
| 190 | + $row_ref[6] |= TileTree::$FLAG_FOUND; |
|
| 191 | + } |
|
| 192 | + # $row[6] is "flags" |
|
| 193 | + if (isset($user['own'][$row_ref[0]])) { |
|
| 194 | + $row_ref[6] |= TileTree::$FLAG_OWN; |
|
| 195 | + } |
|
| 196 | + # $row[6] is "flags" |
|
| 188 | 197 | } |
| 189 | 198 | } |
| 190 | 199 | |
@@ -207,8 +216,7 @@ discard block |
||
| 207 | 216 | OkapiServiceRunner::save_stats_extra("caches/map/tile/checkpointB", null, |
| 208 | 217 | microtime(true) - $checkpointB_started); |
| 209 | 218 | $checkpointC_started = microtime(true); |
| 210 | - if (self::$USE_ETAGS_CACHE && ($request->etag == $response->etag)) |
|
| 211 | - { |
|
| 219 | + if (self::$USE_ETAGS_CACHE && ($request->etag == $response->etag)) { |
|
| 212 | 220 | # Hit. Report the content was unmodified. |
| 213 | 221 | |
| 214 | 222 | $response->etag = null; |
@@ -223,8 +231,7 @@ discard block |
||
| 223 | 231 | OkapiServiceRunner::save_stats_extra("caches/map/tile/checkpointC", null, |
| 224 | 232 | microtime(true) - $checkpointC_started); |
| 225 | 233 | $checkpointD_started = microtime(true); |
| 226 | - if ($response->body !== null) |
|
| 227 | - { |
|
| 234 | + if ($response->body !== null) { |
|
| 228 | 235 | # Hit. We will use the cached version of the image. |
| 229 | 236 | |
| 230 | 237 | return $response; |