@@ -35,8 +35,7 @@ |
||
| 35 | 35 | 'revision' => ReplicateCommon::get_revision(), |
| 36 | 36 | ); |
| 37 | 37 | $dump = Cache::get("last_fulldump"); |
| 38 | - if ($dump) |
|
| 39 | - { |
|
| 38 | + if ($dump) { |
|
| 40 | 39 | $result['latest_fulldump'] = array( |
| 41 | 40 | 'revision' => $dump['revision'], |
| 42 | 41 | 'generated_at' => $dump['meta']['generated_at'], |
@@ -37,11 +37,11 @@ discard block |
||
| 37 | 37 | public static function get_min_since() |
| 38 | 38 | { |
| 39 | 39 | static $cache = null; |
| 40 | - if ($cache == null) |
|
| 41 | - { |
|
| 40 | + if ($cache == null) { |
|
| 42 | 41 | $cache = Db::select_value("select min(id) from okapi_clog"); |
| 43 | - if ($cache === null) |
|
| 44 | - $cache = 1; |
|
| 42 | + if ($cache === null) { |
|
| 43 | + $cache = 1; |
|
| 44 | + } |
|
| 45 | 45 | $cache -= 1; |
| 46 | 46 | } |
| 47 | 47 | return $cache; |
@@ -54,19 +54,21 @@ discard block |
||
| 54 | 54 | */ |
| 55 | 55 | private static function get_diff($old, $new) |
| 56 | 56 | { |
| 57 | - if (!$old) |
|
| 58 | - return $new; |
|
| 57 | + if (!$old) { |
|
| 58 | + return $new; |
|
| 59 | + } |
|
| 59 | 60 | $changed_keys = array(); |
| 60 | - foreach ($new as $key => $value) |
|
| 61 | - { |
|
| 62 | - if (!array_key_exists($key, $old)) |
|
| 63 | - $changed_keys[] = $key; |
|
| 64 | - elseif ($old[$key] != $new[$key]) |
|
| 65 | - $changed_keys[] = $key; |
|
| 61 | + foreach ($new as $key => $value) { |
|
| 62 | + if (!array_key_exists($key, $old)) { |
|
| 63 | + $changed_keys[] = $key; |
|
| 64 | + } elseif ($old[$key] != $new[$key]) { |
|
| 65 | + $changed_keys[] = $key; |
|
| 66 | + } |
|
| 66 | 67 | } |
| 67 | 68 | $changed = array(); |
| 68 | - foreach ($changed_keys as $key) |
|
| 69 | - $changed[$key] = $new[$key]; |
|
| 69 | + foreach ($changed_keys as $key) { |
|
| 70 | + $changed[$key] = $new[$key]; |
|
| 71 | + } |
|
| 70 | 72 | return $changed; |
| 71 | 73 | } |
| 72 | 74 | |
@@ -75,8 +77,9 @@ discard block |
||
| 75 | 77 | { |
| 76 | 78 | $now = Db::select_value("select date_add(now(), interval -1 minute)"); # See issue 157. |
| 77 | 79 | $last_update = Okapi::get_var('last_clog_update'); |
| 78 | - if ($last_update === null) |
|
| 79 | - $last_update = Db::select_value("select date_add(now(), interval -1 day)"); |
|
| 80 | + if ($last_update === null) { |
|
| 81 | + $last_update = Db::select_value("select date_add(now(), interval -1 day)"); |
|
| 82 | + } |
|
| 80 | 83 | |
| 81 | 84 | # Usually this will be fast. But, for example, if admin changes ALL the |
| 82 | 85 | # caches, this will take forever. But we still want it to finish properly |
@@ -100,8 +103,7 @@ discard block |
||
| 100 | 103 | |
| 101 | 104 | # For each group, update the changelog table accordingly. |
| 102 | 105 | |
| 103 | - foreach ($cache_code_groups as $cache_codes) |
|
| 104 | - { |
|
| 106 | + foreach ($cache_code_groups as $cache_codes) { |
|
| 105 | 107 | self::generate_changelog_entries('services/caches/geocaches', 'geocache', 'cache_codes', |
| 106 | 108 | 'code', $cache_codes, self::$logged_cache_fields, false, true, null); |
| 107 | 109 | } |
@@ -109,27 +111,25 @@ discard block |
||
| 109 | 111 | # Same as above, for log entries. |
| 110 | 112 | |
| 111 | 113 | $offset = 0; |
| 112 | - while (true) |
|
| 113 | - { |
|
| 114 | + while (true) { |
|
| 114 | 115 | $log_uuids = Db::select_column(" |
| 115 | 116 | select uuid |
| 116 | 117 | from cache_logs |
| 117 | 118 | where okapi_syncbase > '".Db::escape_string($last_update)."' |
| 118 | 119 | limit $offset, 10000; |
| 119 | 120 | "); |
| 120 | - if (count($log_uuids) == 0) |
|
| 121 | - break; |
|
| 121 | + if (count($log_uuids) == 0) { |
|
| 122 | + break; |
|
| 123 | + } |
|
| 122 | 124 | $offset += 10000; |
| 123 | 125 | $log_uuid_groups = Okapi::make_groups($log_uuids, 100); |
| 124 | 126 | unset($log_uuids); |
| 125 | - foreach ($log_uuid_groups as $log_uuids) |
|
| 126 | - { |
|
| 127 | + foreach ($log_uuid_groups as $log_uuids) { |
|
| 127 | 128 | self::generate_changelog_entries('services/logs/entries', 'log', 'log_uuids', |
| 128 | 129 | 'uuid', $log_uuids, self::$logged_log_entry_fields, false, true, 3600); |
| 129 | 130 | } |
| 130 | 131 | } |
| 131 | - if (Settings::get('OC_BRANCH') == 'oc.de') |
|
| 132 | - { |
|
| 132 | + if (Settings::get('OC_BRANCH') == 'oc.de') { |
|
| 133 | 133 | # On OCDE branch, deleted log entries are MOVED to another table. |
| 134 | 134 | # So the above queries won't detect them. We need to run one more. |
| 135 | 135 | # We will assume there are not so many of them and we don't have to |
@@ -142,8 +142,7 @@ discard block |
||
| 142 | 142 | "); |
| 143 | 143 | $deleted_uuid_groups = Okapi::make_groups($DELETED_uuids, 100); |
| 144 | 144 | unset($DELETED_uuids); |
| 145 | - foreach ($deleted_uuid_groups as $deleted_uuids) |
|
| 146 | - { |
|
| 145 | + foreach ($deleted_uuid_groups as $deleted_uuids) { |
|
| 147 | 146 | self::generate_changelog_entries('services/logs/entries', 'log', 'log_uuids', |
| 148 | 147 | 'uuid', $deleted_uuids, self::$logged_log_entry_fields, false, true, 3600); |
| 149 | 148 | } |
@@ -168,8 +167,7 @@ discard block |
||
| 168 | 167 | */ |
| 169 | 168 | public static function verify_clog_consistency( |
| 170 | 169 | $force_all=false, $geocache_ignored_fields = null |
| 171 | - ) |
|
| 172 | - { |
|
| 170 | + ) { |
|
| 173 | 171 | set_time_limit(0); |
| 174 | 172 | ignore_user_abort(true); |
| 175 | 173 | |
@@ -194,16 +192,15 @@ discard block |
||
| 194 | 192 | |
| 195 | 193 | $sum = 0; |
| 196 | 194 | $two_examples = array(); |
| 197 | - foreach ($cache_code_groups as $cache_codes) |
|
| 198 | - { |
|
| 195 | + foreach ($cache_code_groups as $cache_codes) { |
|
| 199 | 196 | $entries = self::generate_changelog_entries( |
| 200 | 197 | 'services/caches/geocaches', 'geocache', 'cache_codes', |
| 201 | 198 | 'code', $cache_codes, self::$logged_cache_fields, true, true, null |
| 202 | 199 | ); |
| 203 | - foreach ($entries as $entry) |
|
| 204 | - { |
|
| 205 | - if ($entry['object_type'] != 'geocache') |
|
| 206 | - continue; |
|
| 200 | + foreach ($entries as $entry) { |
|
| 201 | + if ($entry['object_type'] != 'geocache') { |
|
| 202 | + continue; |
|
| 203 | + } |
|
| 207 | 204 | $cache_code = $entry['object_key']['code']; |
| 208 | 205 | |
| 209 | 206 | if (($entry['change_type'] == 'replace') && ($geocache_ignored_fields != null)) { |
@@ -227,8 +224,10 @@ discard block |
||
| 227 | 224 | # We will story the first and the last entry in the $two_examples |
| 228 | 225 | # vars which is to be emailed to OKAPI developers. |
| 229 | 226 | |
| 230 | - if (count($two_examples) == 0) |
|
| 231 | - $two_examples[0] = $entry; /* The first entry */ |
|
| 227 | + if (count($two_examples) == 0) { |
|
| 228 | + $two_examples[0] = $entry; |
|
| 229 | + } |
|
| 230 | + /* The first entry */ |
|
| 232 | 231 | $two_examples[1] = $entry; /* The last entry */ |
| 233 | 232 | |
| 234 | 233 | Db::execute(" |
@@ -275,18 +274,18 @@ discard block |
||
| 275 | 274 | { |
| 276 | 275 | # Retrieve the previous versions of all objects from OKAPI cache. |
| 277 | 276 | |
| 278 | - if ($use_cache) |
|
| 279 | - { |
|
| 277 | + if ($use_cache) { |
|
| 280 | 278 | $cache_keys1 = array(); |
| 281 | 279 | $cache_keys2 = array(); |
| 282 | - foreach ($key_values as $key) |
|
| 283 | - $cache_keys1[] = 'clog#'.$object_type.'#'.$key; |
|
| 284 | - foreach ($key_values as $key) |
|
| 285 | - $cache_keys2[] = 'clogmd5#'.$object_type.'#'.$key; |
|
| 280 | + foreach ($key_values as $key) { |
|
| 281 | + $cache_keys1[] = 'clog#'.$object_type.'#'.$key; |
|
| 282 | + } |
|
| 283 | + foreach ($key_values as $key) { |
|
| 284 | + $cache_keys2[] = 'clogmd5#'.$object_type.'#'.$key; |
|
| 285 | + } |
|
| 286 | 286 | $cached_values1 = Cache::get_many($cache_keys1); |
| 287 | 287 | $cached_values2 = Cache::get_many($cache_keys2); |
| 288 | - if (!$fulldump_mode) |
|
| 289 | - { |
|
| 288 | + if (!$fulldump_mode) { |
|
| 290 | 289 | Cache::delete_many($cache_keys1); |
| 291 | 290 | Cache::delete_many($cache_keys2); |
| 292 | 291 | } |
@@ -305,25 +304,20 @@ discard block |
||
| 305 | 304 | 'attribution_append' => 'static' # currently, this is for the "geocaches" method only |
| 306 | 305 | ))); |
| 307 | 306 | $entries = array(); |
| 308 | - foreach ($current_values as $key => $object) |
|
| 309 | - { |
|
| 310 | - if ($object !== null) |
|
| 311 | - { |
|
| 307 | + foreach ($current_values as $key => $object) { |
|
| 308 | + if ($object !== null) { |
|
| 312 | 309 | # Currently, the object exists. |
| 313 | - if ($use_cache) |
|
| 314 | - { |
|
| 310 | + if ($use_cache) { |
|
| 315 | 311 | # First, compare the cached hash. The hash has much longer lifetime |
| 316 | 312 | # than the actual cached object. |
| 317 | 313 | $cached_md5 = $cached_values2['clogmd5#'.$object_type.'#'.$key]; |
| 318 | 314 | $current_md5 = md5(serialize($object)); |
| 319 | - if ($cached_md5 == $current_md5) |
|
| 320 | - { |
|
| 315 | + if ($cached_md5 == $current_md5) { |
|
| 321 | 316 | # The object was not changed since it was last replaced. |
| 322 | 317 | continue; |
| 323 | 318 | } |
| 324 | 319 | $diff = self::get_diff($cached_values1['clog#'.$object_type.'#'.$key], $object); |
| 325 | - if (count($diff) == 0) |
|
| 326 | - { |
|
| 320 | + if (count($diff) == 0) { |
|
| 327 | 321 | # Md5 differs, but diff does not. Weird, but it can happen |
| 328 | 322 | # (e.g. just after the md5 extension was introduced, or if |
| 329 | 323 | # md5 somehow expired before the actual object did). |
@@ -336,18 +330,14 @@ discard block |
||
| 336 | 330 | 'change_type' => 'replace', |
| 337 | 331 | 'data' => ($use_cache ? $diff : $object), |
| 338 | 332 | ); |
| 339 | - if ($use_cache) |
|
| 340 | - { |
|
| 333 | + if ($use_cache) { |
|
| 341 | 334 | # Save the last-published state of the object, for future comparison. |
| 342 | 335 | $cached_values2['clogmd5#'.$object_type.'#'.$key] = $current_md5; |
| 343 | 336 | $cached_values1['clog#'.$object_type.'#'.$key] = $object; |
| 344 | 337 | } |
| 345 | - } |
|
| 346 | - else |
|
| 347 | - { |
|
| 338 | + } else { |
|
| 348 | 339 | # Currently, the object does not exist. |
| 349 | - if ($use_cache && ($cached_values1['clog#'.$object_type.'#'.$key] === false)) |
|
| 350 | - { |
|
| 340 | + if ($use_cache && ($cached_values1['clog#'.$object_type.'#'.$key] === false)) { |
|
| 351 | 341 | # No need to delete, we have already published its deletion. |
| 352 | 342 | continue; |
| 353 | 343 | } |
@@ -356,8 +346,7 @@ discard block |
||
| 356 | 346 | 'object_key' => array($key_name => $key), |
| 357 | 347 | 'change_type' => 'delete', |
| 358 | 348 | ); |
| 359 | - if ($use_cache) |
|
| 360 | - { |
|
| 349 | + if ($use_cache) { |
|
| 361 | 350 | # Cache the fact, that the object was deleted. |
| 362 | 351 | $cached_values2['clogmd5#'.$object_type.'#'.$key] = false; |
| 363 | 352 | $cached_values1['clog#'.$object_type.'#'.$key] = false; |
@@ -365,19 +354,16 @@ discard block |
||
| 365 | 354 | } |
| 366 | 355 | } |
| 367 | 356 | |
| 368 | - if ($fulldump_mode) |
|
| 369 | - { |
|
| 357 | + if ($fulldump_mode) { |
|
| 370 | 358 | return $entries; |
| 371 | - } |
|
| 372 | - else |
|
| 373 | - { |
|
| 359 | + } else { |
|
| 374 | 360 | # Save the entries to the clog table. |
| 375 | 361 | |
| 376 | - if (count($entries) > 0) |
|
| 377 | - { |
|
| 362 | + if (count($entries) > 0) { |
|
| 378 | 363 | $data_values = array(); |
| 379 | - foreach ($entries as $entry) |
|
| 380 | - $data_values[] = gzdeflate(serialize($entry)); |
|
| 364 | + foreach ($entries as $entry) { |
|
| 365 | + $data_values[] = gzdeflate(serialize($entry)); |
|
| 366 | + } |
|
| 381 | 367 | Db::execute(" |
| 382 | 368 | insert into okapi_clog (data) |
| 383 | 369 | values ('".implode("'),('", array_map('\okapi\Db::escape_string', $data_values))."'); |
@@ -386,8 +372,7 @@ discard block |
||
| 386 | 372 | |
| 387 | 373 | # Update the values kept in OKAPI cache. |
| 388 | 374 | |
| 389 | - if ($use_cache) |
|
| 390 | - { |
|
| 375 | + if ($use_cache) { |
|
| 391 | 376 | Cache::set_many($cached_values1, $cache_timeout); |
| 392 | 377 | Cache::set_many($cached_values2, null); # make it persistent |
| 393 | 378 | } |
@@ -403,10 +388,14 @@ discard block |
||
| 403 | 388 | $first_id = Db::select_value(" |
| 404 | 389 | select id from okapi_clog where id > '".Db::escape_string($since)."' limit 1 |
| 405 | 390 | "); |
| 406 | - if ($first_id === null) |
|
| 407 | - return true; # okay, since points to the newest revision |
|
| 408 | - if ($first_id == $since + 1) |
|
| 409 | - return true; # okay, revision $since + 1 is present |
|
| 391 | + if ($first_id === null) { |
|
| 392 | + return true; |
|
| 393 | + } |
|
| 394 | + # okay, since points to the newest revision |
|
| 395 | + if ($first_id == $since + 1) { |
|
| 396 | + return true; |
|
| 397 | + } |
|
| 398 | + # okay, revision $since + 1 is present |
|
| 410 | 399 | |
| 411 | 400 | # If we're here, then this means that $first_id > $since + 1. |
| 412 | 401 | # Revision $since + 1 is already deleted, $since must be too old! |
@@ -425,17 +414,17 @@ discard block |
||
| 425 | 414 | { |
| 426 | 415 | $current_revision = self::get_revision(); |
| 427 | 416 | $last_chunk_cut = $current_revision - ($current_revision % self::$chunk_size); |
| 428 | - if ($since >= $last_chunk_cut) |
|
| 429 | - { |
|
| 417 | + if ($since >= $last_chunk_cut) { |
|
| 430 | 418 | # If, for example, we have a choice to give user 50 items he wants, or 80 items |
| 431 | 419 | # which we probably already have in cache (and this includes the 50 which the |
| 432 | 420 | # user wants), then we'll give him 80. If user wants less than half of what we |
| 433 | 421 | # have (ex. 30), then we'll give him only his 30. |
| 434 | 422 | |
| 435 | - if ($current_revision - $since > $since - $last_chunk_cut) |
|
| 436 | - return array($last_chunk_cut + 1, $current_revision); |
|
| 437 | - else |
|
| 438 | - return array($since + 1, $current_revision); |
|
| 423 | + if ($current_revision - $since > $since - $last_chunk_cut) { |
|
| 424 | + return array($last_chunk_cut + 1, $current_revision); |
|
| 425 | + } else { |
|
| 426 | + return array($since + 1, $current_revision); |
|
| 427 | + } |
|
| 439 | 428 | } |
| 440 | 429 | $prev_chunk_cut = $since - ($since % self::$chunk_size); |
| 441 | 430 | return array($prev_chunk_cut + 1, $prev_chunk_cut + self::$chunk_size); |
@@ -446,17 +435,18 @@ discard block |
||
| 446 | 435 | */ |
| 447 | 436 | public static function get_chunk($from, $to) |
| 448 | 437 | { |
| 449 | - if ($to < $from) |
|
| 450 | - return array(); |
|
| 451 | - if ($to - $from > self::$chunk_size) |
|
| 452 | - throw new Exception("You should not get chunksize bigger than ".self::$chunk_size." entries at one time."); |
|
| 438 | + if ($to < $from) { |
|
| 439 | + return array(); |
|
| 440 | + } |
|
| 441 | + if ($to - $from > self::$chunk_size) { |
|
| 442 | + throw new Exception("You should not get chunksize bigger than ".self::$chunk_size." entries at one time."); |
|
| 443 | + } |
|
| 453 | 444 | |
| 454 | 445 | # Check if we already have this chunk in cache. |
| 455 | 446 | |
| 456 | 447 | $cache_key = 'clog_chunk#'.$from.'-'.$to; |
| 457 | 448 | $chunk = Cache::get($cache_key); |
| 458 | - if ($chunk === null) |
|
| 459 | - { |
|
| 449 | + if ($chunk === null) { |
|
| 460 | 450 | $rs = Db::query(" |
| 461 | 451 | select id, data |
| 462 | 452 | from okapi_clog |
@@ -464,8 +454,7 @@ discard block |
||
| 464 | 454 | order by id |
| 465 | 455 | "); |
| 466 | 456 | $chunk = array(); |
| 467 | - while ($row = Db::fetch_assoc($rs)) |
|
| 468 | - { |
|
| 457 | + while ($row = Db::fetch_assoc($rs)) { |
|
| 469 | 458 | $chunk[] = unserialize(gzinflate($row['data'])); |
| 470 | 459 | } |
| 471 | 460 | |
@@ -475,10 +464,11 @@ discard block |
||
| 475 | 464 | # be ever accessed after the next revision appears, so there is not point |
| 476 | 465 | # in storing them that long. |
| 477 | 466 | |
| 478 | - if (($from % self::$chunk_size === 0) && ($to % self::$chunk_size === 0)) |
|
| 479 | - $timeout = 10 * 86400; |
|
| 480 | - else |
|
| 481 | - $timeout = 86400; |
|
| 467 | + if (($from % self::$chunk_size === 0) && ($to % self::$chunk_size === 0)) { |
|
| 468 | + $timeout = 10 * 86400; |
|
| 469 | + } else { |
|
| 470 | + $timeout = 86400; |
|
| 471 | + } |
|
| 482 | 472 | Cache::set($cache_key, $chunk, $timeout); |
| 483 | 473 | } |
| 484 | 474 | |
@@ -511,16 +501,16 @@ discard block |
||
| 511 | 501 | $cache_codes = Db::select_column("select wp_oc from caches"); |
| 512 | 502 | $cache_code_groups = Okapi::make_groups($cache_codes, self::$chunk_size); |
| 513 | 503 | unset($cache_codes); |
| 514 | - foreach ($cache_code_groups as $cache_codes) |
|
| 515 | - { |
|
| 504 | + foreach ($cache_code_groups as $cache_codes) { |
|
| 516 | 505 | $basename = "part".str_pad($i, 5, "0", STR_PAD_LEFT); |
| 517 | 506 | $json_files[] = $basename.".json"; |
| 518 | 507 | $entries = self::generate_changelog_entries('services/caches/geocaches', 'geocache', 'cache_codes', |
| 519 | 508 | 'code', $cache_codes, self::$logged_cache_fields, true, false); |
| 520 | 509 | $filtered = array(); |
| 521 | - foreach ($entries as $entry) |
|
| 522 | - if ($entry['change_type'] == 'replace') |
|
| 510 | + foreach ($entries as $entry) { |
|
| 511 | + if ($entry['change_type'] == 'replace') |
|
| 523 | 512 | $filtered[] = $entry; |
| 513 | + } |
|
| 524 | 514 | unset($entries); |
| 525 | 515 | file_put_contents("$dir/$basename.json", json_encode($filtered)); |
| 526 | 516 | unset($filtered); |
@@ -532,8 +522,7 @@ discard block |
||
| 532 | 522 | # too much memory. Hence the offset/limit loop. |
| 533 | 523 | |
| 534 | 524 | $offset = 0; |
| 535 | - while (true) |
|
| 536 | - { |
|
| 525 | + while (true) { |
|
| 537 | 526 | $log_uuids = Db::select_column(" |
| 538 | 527 | select uuid |
| 539 | 528 | from cache_logs |
@@ -541,21 +530,22 @@ discard block |
||
| 541 | 530 | order by uuid |
| 542 | 531 | limit $offset, 10000 |
| 543 | 532 | "); |
| 544 | - if (count($log_uuids) == 0) |
|
| 545 | - break; |
|
| 533 | + if (count($log_uuids) == 0) { |
|
| 534 | + break; |
|
| 535 | + } |
|
| 546 | 536 | $offset += 10000; |
| 547 | 537 | $log_uuid_groups = Okapi::make_groups($log_uuids, 500); |
| 548 | 538 | unset($log_uuids); |
| 549 | - foreach ($log_uuid_groups as $log_uuids) |
|
| 550 | - { |
|
| 539 | + foreach ($log_uuid_groups as $log_uuids) { |
|
| 551 | 540 | $basename = "part".str_pad($i, 5, "0", STR_PAD_LEFT); |
| 552 | 541 | $json_files[] = $basename.".json"; |
| 553 | 542 | $entries = self::generate_changelog_entries('services/logs/entries', 'log', 'log_uuids', |
| 554 | 543 | 'uuid', $log_uuids, self::$logged_log_entry_fields, true, false); |
| 555 | 544 | $filtered = array(); |
| 556 | - foreach ($entries as $entry) |
|
| 557 | - if ($entry['change_type'] == 'replace') |
|
| 545 | + foreach ($entries as $entry) { |
|
| 546 | + if ($entry['change_type'] == 'replace') |
|
| 558 | 547 | $filtered[] = $entry; |
| 548 | + } |
|
| 559 | 549 | unset($entries); |
| 560 | 550 | file_put_contents("$dir/$basename.json", json_encode($filtered)); |
| 561 | 551 | unset($filtered); |
@@ -581,8 +571,9 @@ discard block |
||
| 581 | 571 | # Compute uncompressed size. |
| 582 | 572 | |
| 583 | 573 | $size = filesize("$dir/index.json"); |
| 584 | - foreach ($json_files as $filename) |
|
| 585 | - $size += filesize("$dir/$filename"); |
|
| 574 | + foreach ($json_files as $filename) { |
|
| 575 | + $size += filesize("$dir/$filename"); |
|
| 576 | + } |
|
| 586 | 577 | |
| 587 | 578 | # Create JSON archive. We use tar options: -j for bzip2, -z for gzip |
| 588 | 579 | # (bzip2 is MUCH slower). |
@@ -20,8 +20,9 @@ discard block |
||
| 20 | 20 | public static function get_required_locales() |
| 21 | 21 | { |
| 22 | 22 | $arr = array('POSIX'); |
| 23 | - foreach (self::$languages as $key => $value) |
|
| 24 | - $arr[] = $value['locale']; |
|
| 23 | + foreach (self::$languages as $key => $value) { |
|
| 24 | + $arr[] = $value['locale']; |
|
| 25 | + } |
|
| 25 | 26 | return $arr; |
| 26 | 27 | } |
| 27 | 28 | |
@@ -31,26 +32,28 @@ discard block |
||
| 31 | 32 | public static function get_installed_locales() |
| 32 | 33 | { |
| 33 | 34 | $arr = array(); |
| 34 | - foreach (explode("\n", shell_exec("locale -a")) as $item) |
|
| 35 | - if ($item) |
|
| 35 | + foreach (explode("\n", shell_exec("locale -a")) as $item) { |
|
| 36 | + if ($item) |
|
| 36 | 37 | $arr[] = $item; |
| 38 | + } |
|
| 37 | 39 | return $arr; |
| 38 | 40 | } |
| 39 | 41 | |
| 40 | 42 | private static function get_locale_for_language($lang) |
| 41 | 43 | { |
| 42 | - if (isset(self::$languages[$lang])) |
|
| 43 | - return self::$languages[$lang]['locale']; |
|
| 44 | + if (isset(self::$languages[$lang])) { |
|
| 45 | + return self::$languages[$lang]['locale']; |
|
| 46 | + } |
|
| 44 | 47 | return null; |
| 45 | 48 | } |
| 46 | 49 | |
| 47 | 50 | public static function get_best_locale($langprefs) |
| 48 | 51 | { |
| 49 | - foreach ($langprefs as $lang) |
|
| 50 | - { |
|
| 52 | + foreach ($langprefs as $lang) { |
|
| 51 | 53 | $locale = self::get_locale_for_language($lang); |
| 52 | - if ($locale != null) |
|
| 53 | - return $locale; |
|
| 54 | + if ($locale != null) { |
|
| 55 | + return $locale; |
|
| 56 | + } |
|
| 54 | 57 | } |
| 55 | 58 | return self::$languages['en']['locale']; |
| 56 | 59 | } |
@@ -21,8 +21,7 @@ discard block |
||
| 21 | 21 | OkapiErrorHandler::$treat_notices_as_errors = true; |
| 22 | 22 | require_once($GLOBALS['rootpath'].'okapi/urls.php'); |
| 23 | 23 | |
| 24 | -if (ob_list_handlers() == array('default output handler')) |
|
| 25 | -{ |
|
| 24 | +if (ob_list_handlers() == array('default output handler')) { |
|
| 26 | 25 | # We will assume that this one comes from "output_buffering" being turned on |
| 27 | 26 | # in PHP config. This is very common and probably is good for most other OC |
| 28 | 27 | # pages. But we don't need it in OKAPI. We will just turn this off. |
@@ -37,20 +36,23 @@ discard block |
||
| 37 | 36 | { |
| 38 | 37 | # Chop off the ?args=... part. |
| 39 | 38 | |
| 40 | - if (strpos($uri, '?') !== false) |
|
| 41 | - $uri = substr($uri, 0, strpos($uri, '?')); |
|
| 39 | + if (strpos($uri, '?') !== false) { |
|
| 40 | + $uri = substr($uri, 0, strpos($uri, '?')); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | 43 | # Chop off everything before "/okapi/". This should work for okay for most "weird" |
| 44 | 44 | # server configurations. It will also address a more subtle issue described here: |
| 45 | 45 | # http://stackoverflow.com/questions/8040461/request-uri-unexpectedly-contains-fqdn |
| 46 | 46 | |
| 47 | - if (strpos($uri, "/okapi/") !== false) |
|
| 48 | - $uri = substr($uri, strpos($uri, "/okapi/")); |
|
| 47 | + if (strpos($uri, "/okapi/") !== false) { |
|
| 48 | + $uri = substr($uri, strpos($uri, "/okapi/")); |
|
| 49 | + } |
|
| 49 | 50 | |
| 50 | 51 | # Make sure we're in the right directory (.htaccess should make sure of that). |
| 51 | 52 | |
| 52 | - if (strpos($uri, "/okapi/") !== 0) |
|
| 53 | - throw new Exception("'$uri' is outside of the /okapi/ path."); |
|
| 53 | + if (strpos($uri, "/okapi/") !== 0) { |
|
| 54 | + throw new Exception("'$uri' is outside of the /okapi/ path."); |
|
| 55 | + } |
|
| 54 | 56 | $uri = substr($uri, 7); |
| 55 | 57 | |
| 56 | 58 | # Initializing internals and running pre-request cronjobs (we don't want |
@@ -62,27 +64,23 @@ discard block |
||
| 62 | 64 | |
| 63 | 65 | # Checking for allowed patterns... |
| 64 | 66 | |
| 65 | - try |
|
| 66 | - { |
|
| 67 | - foreach (OkapiUrls::$mapping as $pattern => $namespace) |
|
| 68 | - { |
|
| 67 | + try { |
|
| 68 | + foreach (OkapiUrls::$mapping as $pattern => $namespace) { |
|
| 69 | 69 | $matches = null; |
| 70 | - if (preg_match("#$pattern#", $uri, $matches)) |
|
| 71 | - { |
|
| 70 | + if (preg_match("#$pattern#", $uri, $matches)) { |
|
| 72 | 71 | # Pattern matched! Moving on to the proper View... |
| 73 | 72 | |
| 74 | 73 | array_shift($matches); |
| 75 | 74 | require_once($GLOBALS['rootpath']."okapi/views/$namespace.php"); |
| 76 | 75 | $response = call_user_func_array(array('\\okapi\\views\\'. |
| 77 | 76 | str_replace('/', '\\', $namespace).'\\View', 'call'), $matches); |
| 78 | - if ($response) |
|
| 79 | - $response->display(); |
|
| 77 | + if ($response) { |
|
| 78 | + $response->display(); |
|
| 79 | + } |
|
| 80 | 80 | return; |
| 81 | 81 | } |
| 82 | 82 | } |
| 83 | - } |
|
| 84 | - catch (Http404 $e) |
|
| 85 | - { |
|
| 83 | + } catch (Http404 $e) { |
|
| 86 | 84 | /* pass */ |
| 87 | 85 | } |
| 88 | 86 | |
@@ -237,10 +237,8 @@ discard block |
||
| 237 | 237 | throw new Exception("Could not import <rootpath>/okapi_settings.php:\n".$e->getMessage()); |
| 238 | 238 | } |
| 239 | 239 | self::$SETTINGS = self::$DEFAULT_SETTINGS; |
| 240 | - foreach (self::$SETTINGS as $key => $_) |
|
| 241 | - { |
|
| 242 | - if (isset($ref[$key])) |
|
| 243 | - { |
|
| 240 | + foreach (self::$SETTINGS as $key => $_) { |
|
| 241 | + if (isset($ref[$key])) { |
|
| 244 | 242 | self::$SETTINGS[$key] = $ref[$key]; |
| 245 | 243 | } |
| 246 | 244 | } |
@@ -249,41 +247,53 @@ discard block |
||
| 249 | 247 | |
| 250 | 248 | private static function verify(&$dict) |
| 251 | 249 | { |
| 252 | - if (!in_array($dict['OC_BRANCH'], array('oc.pl', 'oc.de'))) |
|
| 253 | - throw new Exception("Currently, OC_BRANCH has to be either 'oc.pl' or 'oc.de'. Hint: Whom did you get your code from?"); |
|
| 250 | + if (!in_array($dict['OC_BRANCH'], array('oc.pl', 'oc.de'))) { |
|
| 251 | + throw new Exception("Currently, OC_BRANCH has to be either 'oc.pl' or 'oc.de'. Hint: Whom did you get your code from?"); |
|
| 252 | + } |
|
| 254 | 253 | $boolean_keys = array('DEBUG', 'DEBUG_PREVENT_EMAILS', 'DEBUG_PREVENT_SEMAPHORES'); |
| 255 | - foreach ($boolean_keys as $key) |
|
| 256 | - if (!in_array($dict[$key], array(true, false))) |
|
| 254 | + foreach ($boolean_keys as $key) { |
|
| 255 | + if (!in_array($dict[$key], array(true, false))) |
|
| 257 | 256 | throw new Exception("Invalid value for $key."); |
| 258 | - if (count($dict['ADMINS']) == 0) |
|
| 259 | - throw new Exception("ADMINS array has to filled (e.g. array('root@localhost'))."); |
|
| 260 | - if ($dict['DEBUG'] == false) |
|
| 261 | - foreach ($dict as $k => $v) |
|
| 257 | + } |
|
| 258 | + if (count($dict['ADMINS']) == 0) { |
|
| 259 | + throw new Exception("ADMINS array has to filled (e.g. array('root@localhost'))."); |
|
| 260 | + } |
|
| 261 | + if ($dict['DEBUG'] == false) { |
|
| 262 | + foreach ($dict as $k => $v) |
|
| 262 | 263 | if ((strpos($k, 'DEBUG_') === 0) && $v == true) |
| 263 | 264 | throw new Exception("When DEBUG is false, $k has to be false too."); |
| 264 | - if ($dict['VAR_DIR'] == null) |
|
| 265 | - throw new Exception("VAR_DIR cannot be null. Please provide a valid directory."); |
|
| 266 | - if ($dict['IMAGES_DIR'] == null) |
|
| 267 | - throw new Exception("IMAGES_DIR cannot be null. Please provide a valid directory."); |
|
| 268 | - foreach ($dict as $k => $v) |
|
| 269 | - if ((strpos($k, '_DIR') !== false) && ($k[strlen($k) - 1] == '/')) |
|
| 265 | + } |
|
| 266 | + if ($dict['VAR_DIR'] == null) { |
|
| 267 | + throw new Exception("VAR_DIR cannot be null. Please provide a valid directory."); |
|
| 268 | + } |
|
| 269 | + if ($dict['IMAGES_DIR'] == null) { |
|
| 270 | + throw new Exception("IMAGES_DIR cannot be null. Please provide a valid directory."); |
|
| 271 | + } |
|
| 272 | + foreach ($dict as $k => $v) { |
|
| 273 | + if ((strpos($k, '_DIR') !== false) && ($k[strlen($k) - 1] == '/')) |
|
| 270 | 274 | throw new Exception("None of the *_DIR settings may end with a slash. Check $k."); |
| 275 | + } |
|
| 271 | 276 | $notnull = array( |
| 272 | 277 | 'OC_COOKIE_NAME', 'DB_SERVER', 'DB_NAME', 'DB_USERNAME', 'SITE_URL', |
| 273 | 278 | 'IMAGES_URL', 'OC_NODE_ID'); |
| 274 | - foreach ($notnull as $k) |
|
| 275 | - if ($dict[$k] === null) |
|
| 279 | + foreach ($notnull as $k) { |
|
| 280 | + if ($dict[$k] === null) |
|
| 276 | 281 | throw new Exception("$k cannot be null."); |
| 282 | + } |
|
| 277 | 283 | $slash_keys = array('SITE_URL', 'IMAGES_URL'); |
| 278 | - foreach ($slash_keys as $key) |
|
| 279 | - if ($dict[$key][strlen($dict[$key]) - 1] != '/') |
|
| 284 | + foreach ($slash_keys as $key) { |
|
| 285 | + if ($dict[$key][strlen($dict[$key]) - 1] != '/') |
|
| 280 | 286 | throw new Exception("$key must end with a slash."); |
| 281 | - if ($dict['REGISTRATION_URL'] === null) |
|
| 282 | - $dict['REGISTRATION_URL'] = $dict['SITE_URL'] . 'register.php'; |
|
| 283 | - if ($dict['SITE_LOGO'] === null) |
|
| 284 | - $dict['SITE_LOGO'] = $dict['SITE_URL'] . 'okapi/static/oc_logo.png'; |
|
| 285 | - if ($dict['JPEG_QUALITY'] < 50 || $dict['JPEG_QUALITY'] > 100) |
|
| 286 | - throw new Exception('JPEG_QUALITY must be between 50 and 100.'); |
|
| 287 | + } |
|
| 288 | + if ($dict['REGISTRATION_URL'] === null) { |
|
| 289 | + $dict['REGISTRATION_URL'] = $dict['SITE_URL'] . 'register.php'; |
|
| 290 | + } |
|
| 291 | + if ($dict['SITE_LOGO'] === null) { |
|
| 292 | + $dict['SITE_LOGO'] = $dict['SITE_URL'] . 'okapi/static/oc_logo.png'; |
|
| 293 | + } |
|
| 294 | + if ($dict['JPEG_QUALITY'] < 50 || $dict['JPEG_QUALITY'] > 100) { |
|
| 295 | + throw new Exception('JPEG_QUALITY must be between 50 and 100.'); |
|
| 296 | + } |
|
| 287 | 297 | |
| 288 | 298 | # The OKAPI code is only compatible with utf8 and utf8mb4 charsets. |
| 289 | 299 | if (!in_array($dict['DB_CHARSET'], array('utf8', 'utf8mb4'))) { |
@@ -296,11 +306,13 @@ discard block |
||
| 296 | 306 | */ |
| 297 | 307 | public static function get($key) |
| 298 | 308 | { |
| 299 | - if (self::$SETTINGS == null) |
|
| 300 | - self::load_settings(); |
|
| 309 | + if (self::$SETTINGS == null) { |
|
| 310 | + self::load_settings(); |
|
| 311 | + } |
|
| 301 | 312 | |
| 302 | - if (!array_key_exists($key, self::$SETTINGS)) |
|
| 303 | - throw new Exception("Tried to access an invalid settings key: '$key'"); |
|
| 313 | + if (!array_key_exists($key, self::$SETTINGS)) { |
|
| 314 | + throw new Exception("Tried to access an invalid settings key: '$key'"); |
|
| 315 | + } |
|
| 304 | 316 | |
| 305 | 317 | return self::$SETTINGS[$key]; |
| 306 | 318 | } |
@@ -25,18 +25,18 @@ discard block |
||
| 25 | 25 | function get_admin_emails() |
| 26 | 26 | { |
| 27 | 27 | $emails = array(); |
| 28 | - if (class_exists("okapi\\Settings")) |
|
| 29 | - { |
|
| 30 | - try |
|
| 31 | - { |
|
| 32 | - foreach (Settings::get('ADMINS') as $email) |
|
| 33 | - if (!in_array($email, $emails)) |
|
| 28 | + if (class_exists("okapi\\Settings")) { |
|
| 29 | + try { |
|
| 30 | + foreach (Settings::get('ADMINS') as $email) { |
|
| 31 | + if (!in_array($email, $emails)) |
|
| 34 | 32 | $emails[] = $email; |
| 35 | - } |
|
| 36 | - catch (Exception $e) { /* pass */ } |
|
| 33 | + } |
|
| 34 | + } catch (Exception $e) { |
|
| 35 | +/* pass */ } |
|
| 36 | + } |
|
| 37 | + if (count($emails) == 0) { |
|
| 38 | + $emails[] = 'root@localhost'; |
|
| 37 | 39 | } |
| 38 | - if (count($emails) == 0) |
|
| 39 | - $emails[] = 'root@localhost'; |
|
| 40 | 40 | return $emails; |
| 41 | 41 | } |
| 42 | 42 | |
@@ -45,12 +45,15 @@ discard block |
||
| 45 | 45 | # |
| 46 | 46 | |
| 47 | 47 | /** A base class for all bad request exceptions. */ |
| 48 | -class BadRequest extends Exception { |
|
| 49 | - protected function provideExtras(&$extras) { |
|
| 48 | +class BadRequest extends Exception |
|
| 49 | +{ |
|
| 50 | + protected function provideExtras(&$extras) |
|
| 51 | + { |
|
| 50 | 52 | $extras['reason_stack'][] = 'bad_request'; |
| 51 | 53 | $extras['status'] = 400; |
| 52 | 54 | } |
| 53 | - public function getOkapiJSON() { |
|
| 55 | + public function getOkapiJSON() |
|
| 56 | + { |
|
| 54 | 57 | $extras = array( |
| 55 | 58 | 'developer_message' => $this->getMessage(), |
| 56 | 59 | 'reason_stack' => array(), |
@@ -62,7 +65,9 @@ discard block |
||
| 62 | 65 | } |
| 63 | 66 | |
| 64 | 67 | /** Thrown on PHP's FATAL errors (detected in a shutdown function). */ |
| 65 | -class FatalError extends ErrorException {} |
|
| 68 | +class FatalError extends ErrorException |
|
| 69 | +{ |
|
| 70 | +} |
|
| 66 | 71 | |
| 67 | 72 | # |
| 68 | 73 | # We'll try to make PHP into something more decent. Exception and |
@@ -75,45 +80,40 @@ discard block |
||
| 75 | 80 | /** Handle exception thrown while executing OKAPI request. */ |
| 76 | 81 | public static function handle($e) |
| 77 | 82 | { |
| 78 | - if ($e instanceof OAuthServerException) |
|
| 79 | - { |
|
| 83 | + if ($e instanceof OAuthServerException) { |
|
| 80 | 84 | # This is thrown on invalid OAuth requests. There are many subclasses |
| 81 | 85 | # of this exception. All of them result in HTTP 400 or HTTP 401 error |
| 82 | 86 | # code. See also: http://oauth.net/core/1.0a/#http_codes |
| 83 | 87 | |
| 84 | - if ($e instanceof OAuthServer400Exception) |
|
| 85 | - header("HTTP/1.0 400 Bad Request"); |
|
| 86 | - else |
|
| 87 | - header("HTTP/1.0 401 Unauthorized"); |
|
| 88 | + if ($e instanceof OAuthServer400Exception) { |
|
| 89 | + header("HTTP/1.0 400 Bad Request"); |
|
| 90 | + } else { |
|
| 91 | + header("HTTP/1.0 401 Unauthorized"); |
|
| 92 | + } |
|
| 88 | 93 | header("Access-Control-Allow-Origin: *"); |
| 89 | 94 | header("Content-Type: application/json; charset=utf-8"); |
| 90 | 95 | |
| 91 | 96 | print $e->getOkapiJSON(); |
| 92 | - } |
|
| 93 | - elseif ($e instanceof BadRequest) |
|
| 94 | - { |
|
| 97 | + } elseif ($e instanceof BadRequest) { |
|
| 95 | 98 | # Intentionally thrown from within the OKAPI method code. |
| 96 | 99 | # Consumer (aka external developer) had something wrong with his |
| 97 | 100 | # request and we want him to know that. |
| 98 | 101 | |
| 99 | 102 | # headers may have been sent e.g. by views/update |
| 100 | - if (!headers_sent()) |
|
| 101 | - { |
|
| 103 | + if (!headers_sent()) { |
|
| 102 | 104 | header("HTTP/1.0 400 Bad Request"); |
| 103 | 105 | header("Access-Control-Allow-Origin: *"); |
| 104 | 106 | header("Content-Type: application/json; charset=utf-8"); |
| 105 | 107 | } |
| 106 | 108 | |
| 107 | 109 | print $e->getOkapiJSON(); |
| 108 | - } |
|
| 109 | - else # (ErrorException, MySQL exception etc.) |
|
| 110 | + } else # (ErrorException, MySQL exception etc.) |
|
| 110 | 111 | { |
| 111 | 112 | # This one is thrown on PHP notices and warnings - usually this |
| 112 | 113 | # indicates an error in OKAPI method. If thrown, then something |
| 113 | 114 | # must be fixed on OUR part. |
| 114 | 115 | |
| 115 | - if (!headers_sent()) |
|
| 116 | - { |
|
| 116 | + if (!headers_sent()) { |
|
| 117 | 117 | header("HTTP/1.0 500 Internal Server Error"); |
| 118 | 118 | header("Access-Control-Allow-Origin: *"); |
| 119 | 119 | header("Content-Type: text/plain; charset=utf-8"); |
@@ -127,19 +127,15 @@ discard block |
||
| 127 | 127 | |
| 128 | 128 | $exception_info = self::get_exception_info($e); |
| 129 | 129 | |
| 130 | - if (class_exists("okapi\\Settings") && (Settings::get('DEBUG'))) |
|
| 131 | - { |
|
| 130 | + if (class_exists("okapi\\Settings") && (Settings::get('DEBUG'))) { |
|
| 132 | 131 | print "\n\nBUT! Since the DEBUG flag is on, then you probably ARE a developer yourself.\n"; |
| 133 | 132 | print "Let's cut to the chase then:"; |
| 134 | 133 | print "\n\n".$exception_info; |
| 135 | 134 | } |
| 136 | - if (class_exists("okapi\\Settings") && (Settings::get('DEBUG_PREVENT_EMAILS'))) |
|
| 137 | - { |
|
| 135 | + if (class_exists("okapi\\Settings") && (Settings::get('DEBUG_PREVENT_EMAILS'))) { |
|
| 138 | 136 | # Sending emails was blocked on admin's demand. |
| 139 | 137 | # This is possible only on development environment. |
| 140 | - } |
|
| 141 | - else |
|
| 142 | - { |
|
| 138 | + } else { |
|
| 143 | 139 | $subject = "OKAPI Method Error - ".substr( |
| 144 | 140 | $_SERVER['REQUEST_URI'], 0, strpos( |
| 145 | 141 | $_SERVER['REQUEST_URI'].'?', '?')); |
@@ -150,19 +146,17 @@ discard block |
||
| 150 | 146 | "developer of the module that threw this error. Thanks!\n\n". |
| 151 | 147 | $exception_info |
| 152 | 148 | ); |
| 153 | - try |
|
| 154 | - { |
|
| 149 | + try { |
|
| 155 | 150 | Okapi::mail_admins($subject, $message); |
| 156 | - } |
|
| 157 | - catch (Exception $e) |
|
| 158 | - { |
|
| 151 | + } catch (Exception $e) { |
|
| 159 | 152 | # Unable to use full-featured mail_admins version. We'll use a backup. |
| 160 | 153 | # We need to make sure we're not spamming. |
| 161 | 154 | |
| 162 | 155 | $lock_file = "/tmp/okapi-fatal-error-mode"; |
| 163 | 156 | $last_email = false; |
| 164 | - if (file_exists($lock_file)) |
|
| 165 | - $last_email = filemtime($lock_file); |
|
| 157 | + if (file_exists($lock_file)) { |
|
| 158 | + $last_email = filemtime($lock_file); |
|
| 159 | + } |
|
| 166 | 160 | if ($last_email === false) { |
| 167 | 161 | # Assume this is the first email. |
| 168 | 162 | $last_email = 0; |
@@ -212,16 +206,13 @@ discard block |
||
| 212 | 206 | $exception_info = "===== ERROR MESSAGE =====\n" |
| 213 | 207 | .trim(self::removeSensitiveData($e->getMessage())) |
| 214 | 208 | ."\n=========================\n\n"; |
| 215 | - if ($e instanceof FatalError) |
|
| 216 | - { |
|
| 209 | + if ($e instanceof FatalError) { |
|
| 217 | 210 | # This one doesn't have a stack trace. It is fed directly to OkapiExceptionHandler::handle |
| 218 | 211 | # by OkapiErrorHandler::handle_shutdown. Instead of printing trace, we will just print |
| 219 | 212 | # the file and line. |
| 220 | 213 | |
| 221 | 214 | $exception_info .= "File: ".$e->getFile()."\nLine: ".$e->getLine()."\n\n"; |
| 222 | - } |
|
| 223 | - else |
|
| 224 | - { |
|
| 215 | + } else { |
|
| 225 | 216 | $exception_info .= "--- Stack trace ---\n". |
| 226 | 217 | self::removeSensitiveData($e->getTraceAsString())."\n\n"; |
| 227 | 218 | } |
@@ -234,8 +225,7 @@ discard block |
||
| 234 | 225 | # This if-condition will solve some (but not all) problems when trying to execute |
| 235 | 226 | # OKAPI code from command line; |
| 236 | 227 | # see https://github.com/opencaching/okapi/issues/243. |
| 237 | - if (function_exists('getallheaders')) |
|
| 238 | - { |
|
| 228 | + if (function_exists('getallheaders')) { |
|
| 239 | 229 | $exception_info .= "--- Request headers ---\n".implode("\n", array_map( |
| 240 | 230 | function($k, $v) { return "$k: $v"; }, |
| 241 | 231 | array_keys(getallheaders()), array_values(getallheaders()) |
@@ -254,7 +244,9 @@ discard block |
||
| 254 | 244 | /** Handle error encountered while executing OKAPI request. */ |
| 255 | 245 | public static function handle($severity, $message, $filename, $lineno) |
| 256 | 246 | { |
| 257 | - if ($severity == E_STRICT || $severity == E_DEPRECATED) return false; |
|
| 247 | + if ($severity == E_STRICT || $severity == E_DEPRECATED) { |
|
| 248 | + return false; |
|
| 249 | + } |
|
| 258 | 250 | if (($severity == E_NOTICE) && !self::$treat_notices_as_errors) { |
| 259 | 251 | return false; |
| 260 | 252 | } |
@@ -283,8 +275,7 @@ discard block |
||
| 283 | 275 | # correctly. The only error which cannot be recovered from is E_ERROR, we have |
| 284 | 276 | # to check the type then. |
| 285 | 277 | |
| 286 | - if (($error !== null) && ($error['type'] == E_ERROR)) |
|
| 287 | - { |
|
| 278 | + if (($error !== null) && ($error['type'] == E_ERROR)) { |
|
| 288 | 279 | $e = new FatalError($error['message'], 0, $error['type'], $error['file'], $error['line']); |
| 289 | 280 | OkapiExceptionHandler::handle($e); |
| 290 | 281 | } |
@@ -304,13 +295,16 @@ discard block |
||
| 304 | 295 | # the developer). |
| 305 | 296 | # |
| 306 | 297 | |
| 307 | -class Http404 extends BadRequest {} |
|
| 298 | +class Http404 extends BadRequest |
|
| 299 | +{ |
|
| 300 | +} |
|
| 308 | 301 | |
| 309 | 302 | /** Common type of BadRequest: Required parameter is missing. */ |
| 310 | 303 | class ParamMissing extends BadRequest |
| 311 | 304 | { |
| 312 | 305 | private $paramName; |
| 313 | - protected function provideExtras(&$extras) { |
|
| 306 | + protected function provideExtras(&$extras) |
|
| 307 | + { |
|
| 314 | 308 | parent::provideExtras($extras); |
| 315 | 309 | $extras['reason_stack'][] = 'missing_parameter'; |
| 316 | 310 | $extras['parameter'] = $this->paramName; |
@@ -330,7 +324,8 @@ discard block |
||
| 330 | 324 | /** What was wrong about the param? */ |
| 331 | 325 | public $whats_wrong_about_it; |
| 332 | 326 | |
| 333 | - protected function provideExtras(&$extras) { |
|
| 327 | + protected function provideExtras(&$extras) |
|
| 328 | + { |
|
| 334 | 329 | parent::provideExtras($extras); |
| 335 | 330 | $extras['reason_stack'][] = 'invalid_parameter'; |
| 336 | 331 | $extras['parameter'] = $this->paramName; |
@@ -340,15 +335,18 @@ discard block |
||
| 340 | 335 | { |
| 341 | 336 | $this->paramName = $paramName; |
| 342 | 337 | $this->whats_wrong_about_it = $whats_wrong_about_it; |
| 343 | - if ($whats_wrong_about_it) |
|
| 344 | - parent::__construct("Parameter '$paramName' has invalid value: ".$whats_wrong_about_it, $code); |
|
| 345 | - else |
|
| 346 | - parent::__construct("Parameter '$paramName' has invalid value.", $code); |
|
| 338 | + if ($whats_wrong_about_it) { |
|
| 339 | + parent::__construct("Parameter '$paramName' has invalid value: ".$whats_wrong_about_it, $code); |
|
| 340 | + } else { |
|
| 341 | + parent::__construct("Parameter '$paramName' has invalid value.", $code); |
|
| 342 | + } |
|
| 347 | 343 | } |
| 348 | 344 | } |
| 349 | 345 | |
| 350 | 346 | /** Thrown on invalid SQL queries. */ |
| 351 | -class DbException extends Exception {} |
|
| 347 | +class DbException extends Exception |
|
| 348 | +{ |
|
| 349 | +} |
|
| 352 | 350 | |
| 353 | 351 | # |
| 354 | 352 | # Database access abstraction layer. |
@@ -364,22 +362,20 @@ discard block |
||
| 364 | 362 | |
| 365 | 363 | public static function connect() |
| 366 | 364 | { |
| 367 | - if (mysql_connect(Settings::get('DB_SERVER'), Settings::get('DB_USERNAME'), Settings::get('DB_PASSWORD'))) |
|
| 368 | - { |
|
| 365 | + if (mysql_connect(Settings::get('DB_SERVER'), Settings::get('DB_USERNAME'), Settings::get('DB_PASSWORD'))) { |
|
| 369 | 366 | mysql_select_db(Settings::get('DB_NAME')); |
| 370 | 367 | mysql_query("set names '" . Settings::get('DB_CHARSET') . "'"); |
| 371 | 368 | self::$connected = true; |
| 369 | + } else { |
|
| 370 | + throw new Exception("Could not connect to MySQL: ".mysql_error()); |
|
| 372 | 371 | } |
| 373 | - else |
|
| 374 | - throw new Exception("Could not connect to MySQL: ".mysql_error()); |
|
| 375 | 372 | } |
| 376 | 373 | |
| 377 | 374 | /** Fetch [{row}], return {row}. */ |
| 378 | 375 | public static function select_row($query) |
| 379 | 376 | { |
| 380 | 377 | $rows = self::select_all($query); |
| 381 | - switch (count($rows)) |
|
| 382 | - { |
|
| 378 | + switch (count($rows)) { |
|
| 383 | 379 | case 0: return null; |
| 384 | 380 | case 1: return $rows[0]; |
| 385 | 381 | default: |
@@ -399,15 +395,16 @@ discard block |
||
| 399 | 395 | private static function select_and_push($query, & $arr, $keyField = null) |
| 400 | 396 | { |
| 401 | 397 | $rs = self::query($query); |
| 402 | - while (true) |
|
| 403 | - { |
|
| 398 | + while (true) { |
|
| 404 | 399 | $row = Db::fetch_assoc($rs); |
| 405 | - if ($row === false) |
|
| 406 | - break; |
|
| 407 | - if ($keyField == null) |
|
| 408 | - $arr[] = $row; |
|
| 409 | - else |
|
| 410 | - $arr[$row[$keyField]] = $row; |
|
| 400 | + if ($row === false) { |
|
| 401 | + break; |
|
| 402 | + } |
|
| 403 | + if ($keyField == null) { |
|
| 404 | + $arr[] = $row; |
|
| 405 | + } else { |
|
| 406 | + $arr[$row[$keyField]] = $row; |
|
| 407 | + } |
|
| 411 | 408 | } |
| 412 | 409 | Db::free_result($rs); |
| 413 | 410 | } |
@@ -417,11 +414,11 @@ discard block |
||
| 417 | 414 | { |
| 418 | 415 | $groups = array(); |
| 419 | 416 | $rs = self::query($query); |
| 420 | - while (true) |
|
| 421 | - { |
|
| 417 | + while (true) { |
|
| 422 | 418 | $row = Db::fetch_assoc($rs); |
| 423 | - if ($row === false) |
|
| 424 | - break; |
|
| 419 | + if ($row === false) { |
|
| 420 | + break; |
|
| 421 | + } |
|
| 425 | 422 | $groups[$row[$keyField]][] = $row; |
| 426 | 423 | } |
| 427 | 424 | Db::free_result($rs); |
@@ -432,10 +429,12 @@ discard block |
||
| 432 | 429 | public static function select_value($query) |
| 433 | 430 | { |
| 434 | 431 | $column = self::select_column($query); |
| 435 | - if ($column == null) |
|
| 436 | - return null; |
|
| 437 | - if (count($column) == 1) |
|
| 438 | - return $column[0]; |
|
| 432 | + if ($column == null) { |
|
| 433 | + return null; |
|
| 434 | + } |
|
| 435 | + if (count($column) == 1) { |
|
| 436 | + return $column[0]; |
|
| 437 | + } |
|
| 439 | 438 | throw new DbException("Invalid query. Db::select_value returned more than one row for:\n\n".$query."\n"); |
| 440 | 439 | } |
| 441 | 440 | |
@@ -444,11 +443,11 @@ discard block |
||
| 444 | 443 | { |
| 445 | 444 | $column = array(); |
| 446 | 445 | $rs = self::query($query); |
| 447 | - while (true) |
|
| 448 | - { |
|
| 446 | + while (true) { |
|
| 449 | 447 | $values = Db::fetch_row($rs); |
| 450 | - if ($values === false) |
|
| 451 | - break; |
|
| 448 | + if ($values === false) { |
|
| 449 | + break; |
|
| 450 | + } |
|
| 452 | 451 | array_push($column, $values[0]); |
| 453 | 452 | } |
| 454 | 453 | Db::free_result($rs); |
@@ -488,18 +487,19 @@ discard block |
||
| 488 | 487 | public static function execute($query) |
| 489 | 488 | { |
| 490 | 489 | $rs = self::query($query); |
| 491 | - if ($rs !== true) |
|
| 492 | - throw new DbException("Db::execute returned a result set for your query. ". |
|
| 490 | + if ($rs !== true) { |
|
| 491 | + throw new DbException("Db::execute returned a result set for your query. ". |
|
| 493 | 492 | "You should use Db::select_* or Db::query for SELECT queries!"); |
| 493 | + } |
|
| 494 | 494 | } |
| 495 | 495 | |
| 496 | 496 | public static function query($query) |
| 497 | 497 | { |
| 498 | - if (!self::$connected) |
|
| 499 | - self::connect(); |
|
| 498 | + if (!self::$connected) { |
|
| 499 | + self::connect(); |
|
| 500 | + } |
|
| 500 | 501 | $rs = mysql_query($query); |
| 501 | - if (!$rs) |
|
| 502 | - { |
|
| 502 | + if (!$rs) { |
|
| 503 | 503 | $errno = mysql_errno(); |
| 504 | 504 | $msg = mysql_error(); |
| 505 | 505 | |
@@ -555,8 +555,9 @@ discard block |
||
| 555 | 555 | |
| 556 | 556 | public static function field_exists($table, $field) |
| 557 | 557 | { |
| 558 | - if (!preg_match("/[a-z0-9_]+/", $table.$field)) |
|
| 559 | - return false; |
|
| 558 | + if (!preg_match("/[a-z0-9_]+/", $table.$field)) { |
|
| 559 | + return false; |
|
| 560 | + } |
|
| 560 | 561 | try { |
| 561 | 562 | $spec = self::select_all("desc ".$table.";"); |
| 562 | 563 | } catch (Exception $e) { |
@@ -564,8 +565,9 @@ discard block |
||
| 564 | 565 | return false; |
| 565 | 566 | } |
| 566 | 567 | foreach ($spec as &$row_ref) { |
| 567 | - if (strtoupper($row_ref['Field']) == strtoupper($field)) |
|
| 568 | - return true; |
|
| 568 | + if (strtoupper($row_ref['Field']) == strtoupper($field)) { |
|
| 569 | + return true; |
|
| 570 | + } |
|
| 569 | 571 | } |
| 570 | 572 | return false; |
| 571 | 573 | } |
@@ -765,10 +767,11 @@ discard block |
||
| 765 | 767 | } catch (OAuthMissingParameterException $e) { |
| 766 | 768 | # Note, that exception will be different if token is supplied |
| 767 | 769 | # and is invalid. We catch only a completely MISSING token parameter. |
| 768 | - if (($e->getParamName() == 'oauth_token') && (!$token_required)) |
|
| 769 | - $token = null; |
|
| 770 | - else |
|
| 771 | - throw $e; |
|
| 770 | + if (($e->getParamName() == 'oauth_token') && (!$token_required)) { |
|
| 771 | + $token = null; |
|
| 772 | + } else { |
|
| 773 | + throw $e; |
|
| 774 | + } |
|
| 772 | 775 | } |
| 773 | 776 | $this->check_signature($request, $consumer, $token); |
| 774 | 777 | return array($consumer, $token); |
@@ -798,21 +801,22 @@ discard block |
||
| 798 | 801 | |
| 799 | 802 | public function get_length() |
| 800 | 803 | { |
| 801 | - if (is_resource($this->body)) |
|
| 802 | - return $this->stream_length; |
|
| 804 | + if (is_resource($this->body)) { |
|
| 805 | + return $this->stream_length; |
|
| 806 | + } |
|
| 803 | 807 | return strlen($this->body); |
| 804 | 808 | } |
| 805 | 809 | |
| 806 | 810 | /** Note: You can call this only once! */ |
| 807 | 811 | public function print_body() |
| 808 | 812 | { |
| 809 | - if (is_resource($this->body)) |
|
| 810 | - { |
|
| 811 | - while (!feof($this->body)) |
|
| 812 | - print fread($this->body, 1024*1024); |
|
| 813 | + if (is_resource($this->body)) { |
|
| 814 | + while (!feof($this->body)) { |
|
| 815 | + print fread($this->body, 1024*1024); |
|
| 816 | + } |
|
| 817 | + } else { |
|
| 818 | + print $this->body; |
|
| 813 | 819 | } |
| 814 | - else |
|
| 815 | - print $this->body; |
|
| 816 | 820 | } |
| 817 | 821 | |
| 818 | 822 | /** |
@@ -821,14 +825,13 @@ discard block |
||
| 821 | 825 | */ |
| 822 | 826 | public function get_body() |
| 823 | 827 | { |
| 824 | - if (is_resource($this->body)) |
|
| 825 | - { |
|
| 828 | + if (is_resource($this->body)) { |
|
| 826 | 829 | ob_start(); |
| 827 | 830 | fpassthru($this->body); |
| 828 | 831 | return ob_get_clean(); |
| 832 | + } else { |
|
| 833 | + return $this->body; |
|
| 829 | 834 | } |
| 830 | - else |
|
| 831 | - return $this->body; |
|
| 832 | 835 | } |
| 833 | 836 | |
| 834 | 837 | /** |
@@ -840,32 +843,33 @@ discard block |
||
| 840 | 843 | header("Access-Control-Allow-Origin: *"); |
| 841 | 844 | header("Content-Type: ".$this->content_type); |
| 842 | 845 | header("Cache-Control: ".$this->cache_control); |
| 843 | - if ($this->connection_close) |
|
| 844 | - header("Connection: close"); |
|
| 845 | - if ($this->content_disposition) |
|
| 846 | - header("Content-Disposition: ".$this->content_disposition); |
|
| 847 | - if ($this->etag) |
|
| 848 | - header("ETag: $this->etag"); |
|
| 846 | + if ($this->connection_close) { |
|
| 847 | + header("Connection: close"); |
|
| 848 | + } |
|
| 849 | + if ($this->content_disposition) { |
|
| 850 | + header("Content-Disposition: ".$this->content_disposition); |
|
| 851 | + } |
|
| 852 | + if ($this->etag) { |
|
| 853 | + header("ETag: $this->etag"); |
|
| 854 | + } |
|
| 849 | 855 | |
| 850 | 856 | # Make sure that gzip is supported by the client. |
| 851 | 857 | $use_gzip = $this->allow_gzip; |
| 852 | - if (empty($_SERVER["HTTP_ACCEPT_ENCODING"]) || (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") === false)) |
|
| 853 | - $use_gzip = false; |
|
| 858 | + if (empty($_SERVER["HTTP_ACCEPT_ENCODING"]) || (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") === false)) { |
|
| 859 | + $use_gzip = false; |
|
| 860 | + } |
|
| 854 | 861 | |
| 855 | 862 | # We will gzip the data ourselves, while disabling gziping by Apache. This way, we can |
| 856 | 863 | # set the Content-Length correctly which is handy in some scenarios. |
| 857 | 864 | |
| 858 | - if ($use_gzip && is_string($this->body)) |
|
| 859 | - { |
|
| 865 | + if ($use_gzip && is_string($this->body)) { |
|
| 860 | 866 | # Apache won't gzip a response which is already gzipped. |
| 861 | 867 | |
| 862 | 868 | header("Content-Encoding: gzip"); |
| 863 | 869 | $gzipped = gzencode($this->body, 5); |
| 864 | 870 | header("Content-Length: ".strlen($gzipped)); |
| 865 | 871 | print $gzipped; |
| 866 | - } |
|
| 867 | - else |
|
| 868 | - { |
|
| 872 | + } else { |
|
| 869 | 873 | # We don't want Apache to gzip this response. Tell it so. |
| 870 | 874 | |
| 871 | 875 | if (function_exists('apache_setenv')) { |
@@ -873,8 +877,9 @@ discard block |
||
| 873 | 877 | } |
| 874 | 878 | |
| 875 | 879 | $length = $this->get_length(); |
| 876 | - if ($length) |
|
| 877 | - header("Content-Length: ".$length); |
|
| 880 | + if ($length) { |
|
| 881 | + header("Content-Length: ".$length); |
|
| 882 | + } |
|
| 878 | 883 | $this->print_body(); |
| 879 | 884 | } |
| 880 | 885 | } |
@@ -883,7 +888,9 @@ discard block |
||
| 883 | 888 | class OkapiRedirectResponse extends OkapiHttpResponse |
| 884 | 889 | { |
| 885 | 890 | public $url; |
| 886 | - public function __construct($url) { $this->url = $url; } |
|
| 891 | + public function __construct($url) |
|
| 892 | + { |
|
| 893 | +$this->url = $url; } |
|
| 887 | 894 | public function display() |
| 888 | 895 | { |
| 889 | 896 | header("HTTP/1.1 303 See Other"); |
@@ -949,14 +956,11 @@ discard block |
||
| 949 | 956 | |
| 950 | 957 | private function __construct($name) |
| 951 | 958 | { |
| 952 | - if (Settings::get('DEBUG_PREVENT_SEMAPHORES')) |
|
| 953 | - { |
|
| 959 | + if (Settings::get('DEBUG_PREVENT_SEMAPHORES')) { |
|
| 954 | 960 | # Using semaphores is forbidden on this server by its admin. |
| 955 | 961 | # This is possible only on development environment. |
| 956 | 962 | $this->lock = null; |
| 957 | - } |
|
| 958 | - else |
|
| 959 | - { |
|
| 963 | + } else { |
|
| 960 | 964 | $this->lockfile = Okapi::get_var_dir()."/okapi-lock-".$name; |
| 961 | 965 | $this->lock = fopen($this->lockfile, "wb"); |
| 962 | 966 | } |
@@ -964,22 +968,26 @@ discard block |
||
| 964 | 968 | |
| 965 | 969 | public function acquire() |
| 966 | 970 | { |
| 967 | - if ($this->lock !== null) |
|
| 968 | - flock($this->lock, LOCK_EX); |
|
| 971 | + if ($this->lock !== null) { |
|
| 972 | + flock($this->lock, LOCK_EX); |
|
| 973 | + } |
|
| 969 | 974 | } |
| 970 | 975 | |
| 971 | 976 | public function try_acquire() |
| 972 | 977 | { |
| 973 | - if ($this->lock !== null) |
|
| 974 | - return flock($this->lock, LOCK_EX | LOCK_NB); |
|
| 975 | - else |
|
| 976 | - return true; # $lock can be null only when debugging |
|
| 978 | + if ($this->lock !== null) { |
|
| 979 | + return flock($this->lock, LOCK_EX | LOCK_NB); |
|
| 980 | + } else { |
|
| 981 | + return true; |
|
| 982 | + } |
|
| 983 | + # $lock can be null only when debugging |
|
| 977 | 984 | } |
| 978 | 985 | |
| 979 | 986 | public function release() |
| 980 | 987 | { |
| 981 | - if ($this->lock !== null) |
|
| 982 | - flock($this->lock, LOCK_UN); |
|
| 988 | + if ($this->lock !== null) { |
|
| 989 | + flock($this->lock, LOCK_UN); |
|
| 990 | + } |
|
| 983 | 991 | } |
| 984 | 992 | |
| 985 | 993 | /** |
@@ -988,8 +996,7 @@ discard block |
||
| 988 | 996 | */ |
| 989 | 997 | public function remove() |
| 990 | 998 | { |
| 991 | - if ($this->lock !== null) |
|
| 992 | - { |
|
| 999 | + if ($this->lock !== null) { |
|
| 993 | 1000 | fclose($this->lock); |
| 994 | 1001 | unlink($this->lockfile); |
| 995 | 1002 | } |
@@ -1033,18 +1040,19 @@ discard block |
||
| 1033 | 1040 | /** Get a variable stored in okapi_vars. If variable not found, return $default. */ |
| 1034 | 1041 | public static function get_var($varname, $default = null) |
| 1035 | 1042 | { |
| 1036 | - if (self::$okapi_vars === null) |
|
| 1037 | - { |
|
| 1043 | + if (self::$okapi_vars === null) { |
|
| 1038 | 1044 | $rs = Db::query(" |
| 1039 | 1045 | select var, value |
| 1040 | 1046 | from okapi_vars |
| 1041 | 1047 | "); |
| 1042 | 1048 | self::$okapi_vars = array(); |
| 1043 | - while ($row = Db::fetch_assoc($rs)) |
|
| 1044 | - self::$okapi_vars[$row['var']] = $row['value']; |
|
| 1049 | + while ($row = Db::fetch_assoc($rs)) { |
|
| 1050 | + self::$okapi_vars[$row['var']] = $row['value']; |
|
| 1051 | + } |
|
| 1052 | + } |
|
| 1053 | + if (isset(self::$okapi_vars[$varname])) { |
|
| 1054 | + return self::$okapi_vars[$varname]; |
|
| 1045 | 1055 | } |
| 1046 | - if (isset(self::$okapi_vars[$varname])) |
|
| 1047 | - return self::$okapi_vars[$varname]; |
|
| 1048 | 1056 | return $default; |
| 1049 | 1057 | } |
| 1050 | 1058 | |
@@ -1100,8 +1108,9 @@ discard block |
||
| 1100 | 1108 | |
| 1101 | 1109 | return; |
| 1102 | 1110 | } |
| 1103 | - if ($counter === null) |
|
| 1104 | - $counter = 0; |
|
| 1111 | + if ($counter === null) { |
|
| 1112 | + $counter = 0; |
|
| 1113 | + } |
|
| 1105 | 1114 | $counter++; |
| 1106 | 1115 | try { |
| 1107 | 1116 | Cache::set($cache_key, $counter, 3600); |
@@ -1109,20 +1118,16 @@ discard block |
||
| 1109 | 1118 | # If `get` suceeded and `set` did not, then probably we're having |
| 1110 | 1119 | # issue #156 scenario. We can ignore it here. |
| 1111 | 1120 | } |
| 1112 | - if ($counter <= 5) |
|
| 1113 | - { |
|
| 1121 | + if ($counter <= 5) { |
|
| 1114 | 1122 | # We're not spamming yet. |
| 1115 | 1123 | |
| 1116 | 1124 | self::mail_from_okapi(get_admin_emails(), $subject, $message); |
| 1117 | - } |
|
| 1118 | - else |
|
| 1119 | - { |
|
| 1125 | + } else { |
|
| 1120 | 1126 | # We are spamming. Prevent sending more emails. |
| 1121 | 1127 | |
| 1122 | 1128 | $content_cache_key_prefix = 'mail_admins_spam/'.(floor(time() / 3600) * 3600).'/'; |
| 1123 | 1129 | $timeout = 86400; |
| 1124 | - if ($counter == 6) |
|
| 1125 | - { |
|
| 1130 | + if ($counter == 6) { |
|
| 1126 | 1131 | self::mail_from_okapi(get_admin_emails(), "Anti-spam mode activated for '$subject'", |
| 1127 | 1132 | "OKAPI has activated an \"anti-spam\" mode for the following subject:\n\n". |
| 1128 | 1133 | "\"$subject\"\n\n". |
@@ -1142,14 +1147,14 @@ discard block |
||
| 1142 | 1147 | /** Send an email message from OKAPI to the given recipients. */ |
| 1143 | 1148 | public static function mail_from_okapi($email_addresses, $subject, $message) |
| 1144 | 1149 | { |
| 1145 | - if (class_exists("okapi\\Settings") && (Settings::get('DEBUG_PREVENT_EMAILS'))) |
|
| 1146 | - { |
|
| 1150 | + if (class_exists("okapi\\Settings") && (Settings::get('DEBUG_PREVENT_EMAILS'))) { |
|
| 1147 | 1151 | # Sending emails was blocked on admin's demand. |
| 1148 | 1152 | # This is possible only on development environment. |
| 1149 | 1153 | return; |
| 1150 | 1154 | } |
| 1151 | - if (!is_array($email_addresses)) |
|
| 1152 | - $email_addresses = array($email_addresses); |
|
| 1155 | + if (!is_array($email_addresses)) { |
|
| 1156 | + $email_addresses = array($email_addresses); |
|
| 1157 | + } |
|
| 1153 | 1158 | $sender_email = class_exists("okapi\\Settings") ? Settings::get('FROM_FIELD') : 'root@localhost'; |
| 1154 | 1159 | mail(implode(", ", $email_addresses), $subject, $message, |
| 1155 | 1160 | "Content-Type: text/plain; charset=utf-8\n". |
@@ -1162,16 +1167,18 @@ discard block |
||
| 1162 | 1167 | public static function get_var_dir() |
| 1163 | 1168 | { |
| 1164 | 1169 | $dir = Settings::get('VAR_DIR'); |
| 1165 | - if ($dir != null) |
|
| 1166 | - return rtrim($dir, "/"); |
|
| 1170 | + if ($dir != null) { |
|
| 1171 | + return rtrim($dir, "/"); |
|
| 1172 | + } |
|
| 1167 | 1173 | throw new Exception("You need to set a valid VAR_DIR."); |
| 1168 | 1174 | } |
| 1169 | 1175 | |
| 1170 | 1176 | /** Returns something like "Opencaching.PL" or "Opencaching.DE". */ |
| 1171 | 1177 | public static function get_normalized_site_name($site_url = null) |
| 1172 | 1178 | { |
| 1173 | - if ($site_url == null) |
|
| 1174 | - $site_url = Settings::get('SITE_URL'); |
|
| 1179 | + if ($site_url == null) { |
|
| 1180 | + $site_url = Settings::get('SITE_URL'); |
|
| 1181 | + } |
|
| 1175 | 1182 | $matches = null; |
| 1176 | 1183 | if (preg_match("#^https?://(www.)?opencaching.([a-z.]+)/$#", $site_url, $matches)) { |
| 1177 | 1184 | return "Opencaching.".strtoupper($matches[2]); |
@@ -1320,11 +1327,13 @@ discard block |
||
| 1320 | 1327 | */ |
| 1321 | 1328 | public static function pick_best_language($langdict, $langprefs) |
| 1322 | 1329 | { |
| 1323 | - foreach ($langprefs as $pref) |
|
| 1324 | - if (isset($langdict[$pref])) |
|
| 1330 | + foreach ($langprefs as $pref) { |
|
| 1331 | + if (isset($langdict[$pref])) |
|
| 1325 | 1332 | return $langdict[$pref]; |
| 1326 | - foreach ($langdict as &$text_ref) |
|
| 1327 | - return $text_ref; |
|
| 1333 | + } |
|
| 1334 | + foreach ($langdict as &$text_ref) { |
|
| 1335 | + return $text_ref; |
|
| 1336 | + } |
|
| 1328 | 1337 | return ""; |
| 1329 | 1338 | } |
| 1330 | 1339 | |
@@ -1335,8 +1344,7 @@ discard block |
||
| 1335 | 1344 | { |
| 1336 | 1345 | $i = 0; |
| 1337 | 1346 | $groups = array(); |
| 1338 | - while ($i < count($array)) |
|
| 1339 | - { |
|
| 1347 | + while ($i < count($array)) { |
|
| 1340 | 1348 | $groups[] = array_slice($array, $i, $size); |
| 1341 | 1349 | $i += $size; |
| 1342 | 1350 | } |
@@ -1350,8 +1358,7 @@ discard block |
||
| 1350 | 1358 | public static function execute_prerequest_cronjobs() |
| 1351 | 1359 | { |
| 1352 | 1360 | $nearest_event = Okapi::get_var("cron_nearest_event"); |
| 1353 | - if ($nearest_event + 0 <= time()) |
|
| 1354 | - { |
|
| 1361 | + if ($nearest_event + 0 <= time()) { |
|
| 1355 | 1362 | require_once($GLOBALS['rootpath']."okapi/cronjobs.php"); |
| 1356 | 1363 | $nearest_event = CronJobController::run_jobs('pre-request'); |
| 1357 | 1364 | Okapi::set_var("cron_nearest_event", $nearest_event); |
@@ -1365,8 +1372,7 @@ discard block |
||
| 1365 | 1372 | public static function execute_cron5_cronjobs() |
| 1366 | 1373 | { |
| 1367 | 1374 | $nearest_event = Okapi::get_var("cron_nearest_event"); |
| 1368 | - if ($nearest_event + 0 <= time()) |
|
| 1369 | - { |
|
| 1375 | + if ($nearest_event + 0 <= time()) { |
|
| 1370 | 1376 | set_time_limit(0); |
| 1371 | 1377 | ignore_user_abort(true); |
| 1372 | 1378 | require_once($GLOBALS['rootpath']."okapi/cronjobs.php"); |
@@ -1384,8 +1390,7 @@ discard block |
||
| 1384 | 1390 | # This way, we don't need to call the actual locale-switching code most |
| 1385 | 1391 | # of the times. |
| 1386 | 1392 | |
| 1387 | - if ($gettext_last_used_langprefs != $langprefs) |
|
| 1388 | - { |
|
| 1393 | + if ($gettext_last_used_langprefs != $langprefs) { |
|
| 1389 | 1394 | $gettext_last_set_locale = call_user_func(Settings::get("GETTEXT_INIT"), $langprefs); |
| 1390 | 1395 | $gettext_last_used_langprefs = $langprefs; |
| 1391 | 1396 | textdomain(Settings::get("GETTEXT_DOMAIN")); |
@@ -1405,12 +1410,12 @@ discard block |
||
| 1405 | 1410 | { |
| 1406 | 1411 | # Put the langprefs on the stack. |
| 1407 | 1412 | |
| 1408 | - if ($langprefs == null) |
|
| 1409 | - $langprefs = array(Settings::get('SITELANG')); |
|
| 1413 | + if ($langprefs == null) { |
|
| 1414 | + $langprefs = array(Settings::get('SITELANG')); |
|
| 1415 | + } |
|
| 1410 | 1416 | self::$gettext_langprefs_stack[] = $langprefs; |
| 1411 | 1417 | |
| 1412 | - if (count(self::$gettext_langprefs_stack) == 1) |
|
| 1413 | - { |
|
| 1418 | + if (count(self::$gettext_langprefs_stack) == 1) { |
|
| 1414 | 1419 | # This is the first time gettext_domain_init is called. In order to |
| 1415 | 1420 | # properly reinitialize the original settings after gettext_domain_restore |
| 1416 | 1421 | # is called for the last time, we need to save current textdomain (which |
@@ -1434,13 +1439,10 @@ discard block |
||
| 1434 | 1439 | array_pop(self::$gettext_langprefs_stack); |
| 1435 | 1440 | |
| 1436 | 1441 | $size = count(self::$gettext_langprefs_stack); |
| 1437 | - if ($size > 0) |
|
| 1438 | - { |
|
| 1442 | + if ($size > 0) { |
|
| 1439 | 1443 | $langprefs = self::$gettext_langprefs_stack[$size - 1]; |
| 1440 | 1444 | self::gettext_set_lang($langprefs); |
| 1441 | - } |
|
| 1442 | - else |
|
| 1443 | - { |
|
| 1445 | + } else { |
|
| 1444 | 1446 | # The stack is empty. This means we're going out of OKAPI code and |
| 1445 | 1447 | # we want the original textdomain reestablished. |
| 1446 | 1448 | |
@@ -1455,20 +1457,25 @@ discard block |
||
| 1455 | 1457 | public static function init_internals($allow_cronjobs = true) |
| 1456 | 1458 | { |
| 1457 | 1459 | static $init_made = false; |
| 1458 | - if ($init_made) |
|
| 1459 | - return; |
|
| 1460 | + if ($init_made) { |
|
| 1461 | + return; |
|
| 1462 | + } |
|
| 1460 | 1463 | ini_set('memory_limit', '256M'); |
| 1461 | 1464 | # The memory limit is - among other - crucial for the maximum size |
| 1462 | 1465 | # of processable images; see services/logs/images/add.php: max_pixels() |
| 1463 | 1466 | Db::connect(); |
| 1464 | - if (Settings::get('TIMEZONE') !== null) |
|
| 1465 | - date_default_timezone_set(Settings::get('TIMEZONE')); |
|
| 1466 | - if (!self::$data_store) |
|
| 1467 | - self::$data_store = new OkapiDataStore(); |
|
| 1468 | - if (!self::$server) |
|
| 1469 | - self::$server = new OkapiOAuthServer(self::$data_store); |
|
| 1470 | - if ($allow_cronjobs) |
|
| 1471 | - self::execute_prerequest_cronjobs(); |
|
| 1467 | + if (Settings::get('TIMEZONE') !== null) { |
|
| 1468 | + date_default_timezone_set(Settings::get('TIMEZONE')); |
|
| 1469 | + } |
|
| 1470 | + if (!self::$data_store) { |
|
| 1471 | + self::$data_store = new OkapiDataStore(); |
|
| 1472 | + } |
|
| 1473 | + if (!self::$server) { |
|
| 1474 | + self::$server = new OkapiOAuthServer(self::$data_store); |
|
| 1475 | + } |
|
| 1476 | + if ($allow_cronjobs) { |
|
| 1477 | + self::execute_prerequest_cronjobs(); |
|
| 1478 | + } |
|
| 1472 | 1479 | $init_made = true; |
| 1473 | 1480 | } |
| 1474 | 1481 | |
@@ -1479,14 +1486,14 @@ discard block |
||
| 1479 | 1486 | */ |
| 1480 | 1487 | public static function generate_key($length, $user_friendly = false) |
| 1481 | 1488 | { |
| 1482 | - if ($user_friendly) |
|
| 1483 | - $chars = "0123456789"; |
|
| 1484 | - else |
|
| 1485 | - $chars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; |
|
| 1489 | + if ($user_friendly) { |
|
| 1490 | + $chars = "0123456789"; |
|
| 1491 | + } else { |
|
| 1492 | + $chars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; |
|
| 1493 | + } |
|
| 1486 | 1494 | $max = strlen($chars); |
| 1487 | 1495 | $key = ""; |
| 1488 | - for ($i=0; $i<$length; $i++) |
|
| 1489 | - { |
|
| 1496 | + for ($i=0; $i<$length; $i++) { |
|
| 1490 | 1497 | $key .= $chars[rand(0, $max-1)]; |
| 1491 | 1498 | } |
| 1492 | 1499 | return $key; |
@@ -1504,10 +1511,11 @@ discard block |
||
| 1504 | 1511 | $appname, $appurl, $email); |
| 1505 | 1512 | $sample_cache = OkapiServiceRunner::call("services/caches/search/all", |
| 1506 | 1513 | new OkapiInternalRequest($consumer, null, array('limit', 1))); |
| 1507 | - if (count($sample_cache['results']) > 0) |
|
| 1508 | - $sample_cache_code = $sample_cache['results'][0]; |
|
| 1509 | - else |
|
| 1510 | - $sample_cache_code = "CACHECODE"; |
|
| 1514 | + if (count($sample_cache['results']) > 0) { |
|
| 1515 | + $sample_cache_code = $sample_cache['results'][0]; |
|
| 1516 | + } else { |
|
| 1517 | + $sample_cache_code = "CACHECODE"; |
|
| 1518 | + } |
|
| 1511 | 1519 | |
| 1512 | 1520 | # Message for the Consumer. |
| 1513 | 1521 | ob_start(); |
@@ -1558,7 +1566,9 @@ discard block |
||
| 1558 | 1566 | # to floating point precision limits. See issue #351 for details. |
| 1559 | 1567 | # |
| 1560 | 1568 | $d = acos(min(1, cos($x1) * cos($x2) + sin($x1) * sin($x2) * cos(($lon1-$lon2) * 3.14159 / 180))) * 6371000; |
| 1561 | - if ($d < 0) $d = 0; |
|
| 1569 | + if ($d < 0) { |
|
| 1570 | + $d = 0; |
|
| 1571 | + } |
|
| 1562 | 1572 | return $d; |
| 1563 | 1573 | } |
| 1564 | 1574 | |
@@ -1581,10 +1591,15 @@ discard block |
||
| 1581 | 1591 | /** Return bearing (float 0..360) from geopoint 1 to 2. */ |
| 1582 | 1592 | public static function get_bearing($lat1, $lon1, $lat2, $lon2) |
| 1583 | 1593 | { |
| 1584 | - if ($lat1 == $lat2 && $lon1 == $lon2) |
|
| 1585 | - return null; |
|
| 1586 | - if ($lat1 == $lat2) $lat1 += 0.0000166; |
|
| 1587 | - if ($lon1 == $lon2) $lon1 += 0.0000166; |
|
| 1594 | + if ($lat1 == $lat2 && $lon1 == $lon2) { |
|
| 1595 | + return null; |
|
| 1596 | + } |
|
| 1597 | + if ($lat1 == $lat2) { |
|
| 1598 | + $lat1 += 0.0000166; |
|
| 1599 | + } |
|
| 1600 | + if ($lon1 == $lon2) { |
|
| 1601 | + $lon1 += 0.0000166; |
|
| 1602 | + } |
|
| 1588 | 1603 | |
| 1589 | 1604 | $rad_lat1 = $lat1 / 180.0 * 3.14159; |
| 1590 | 1605 | $rad_lon1 = $lon1 / 180.0 * 3.14159; |
@@ -1595,7 +1610,9 @@ discard block |
||
| 1595 | 1610 | $bearing = atan2(sin($delta_lon) * cos($rad_lat2), |
| 1596 | 1611 | cos($rad_lat1) * sin($rad_lat2) - sin($rad_lat1) * cos($rad_lat2) * cos($delta_lon)); |
| 1597 | 1612 | $bearing = 180.0 * $bearing / 3.14159; |
| 1598 | - if ( $bearing < 0.0 ) $bearing = $bearing + 360.0; |
|
| 1613 | + if ( $bearing < 0.0 ) { |
|
| 1614 | + $bearing = $bearing + 360.0; |
|
| 1615 | + } |
|
| 1599 | 1616 | |
| 1600 | 1617 | return $bearing; |
| 1601 | 1618 | } |
@@ -1604,7 +1621,9 @@ discard block |
||
| 1604 | 1621 | public static function bearing_as_two_letters($b) |
| 1605 | 1622 | { |
| 1606 | 1623 | static $names = array('N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'); |
| 1607 | - if ($b === null) return 'n/a'; |
|
| 1624 | + if ($b === null) { |
|
| 1625 | + return 'n/a'; |
|
| 1626 | + } |
|
| 1608 | 1627 | return $names[round(($b / 360.0) * 8.0) % 8]; |
| 1609 | 1628 | } |
| 1610 | 1629 | |
@@ -1613,7 +1632,9 @@ discard block |
||
| 1613 | 1632 | { |
| 1614 | 1633 | static $names = array('N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', |
| 1615 | 1634 | 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW'); |
| 1616 | - if ($b === null) return 'n/a'; |
|
| 1635 | + if ($b === null) { |
|
| 1636 | + return 'n/a'; |
|
| 1637 | + } |
|
| 1617 | 1638 | return $names[round(($b / 360.0) * 16.0) % 16]; |
| 1618 | 1639 | } |
| 1619 | 1640 | |
@@ -1634,54 +1655,50 @@ discard block |
||
| 1634 | 1655 | */ |
| 1635 | 1656 | public static function formatted_response(OkapiRequest $request, &$object) |
| 1636 | 1657 | { |
| 1637 | - if ($request instanceof OkapiInternalRequest && (!$request->i_want_OkapiResponse)) |
|
| 1638 | - { |
|
| 1658 | + if ($request instanceof OkapiInternalRequest && (!$request->i_want_OkapiResponse)) { |
|
| 1639 | 1659 | # If you call a method internally, then you probably expect to get |
| 1640 | 1660 | # the actual object instead of it's formatted representation. |
| 1641 | 1661 | return $object; |
| 1642 | 1662 | } |
| 1643 | 1663 | $format = $request->get_parameter('format'); |
| 1644 | - if ($format == null) $format = 'json'; |
|
| 1645 | - if (!in_array($format, array('json', 'jsonp', 'xmlmap', 'xmlmap2'))) |
|
| 1646 | - throw new InvalidParam('format', "'$format'"); |
|
| 1664 | + if ($format == null) { |
|
| 1665 | + $format = 'json'; |
|
| 1666 | + } |
|
| 1667 | + if (!in_array($format, array('json', 'jsonp', 'xmlmap', 'xmlmap2'))) { |
|
| 1668 | + throw new InvalidParam('format', "'$format'"); |
|
| 1669 | + } |
|
| 1647 | 1670 | $callback = $request->get_parameter('callback'); |
| 1648 | - if ($callback && $format != 'jsonp') |
|
| 1649 | - throw new BadRequest("The 'callback' parameter is reserved to be used with the JSONP output format."); |
|
| 1650 | - if ($format == 'json') |
|
| 1651 | - { |
|
| 1671 | + if ($callback && $format != 'jsonp') { |
|
| 1672 | + throw new BadRequest("The 'callback' parameter is reserved to be used with the JSONP output format."); |
|
| 1673 | + } |
|
| 1674 | + if ($format == 'json') { |
|
| 1652 | 1675 | $response = new OkapiHttpResponse(); |
| 1653 | 1676 | $response->content_type = "application/json; charset=utf-8"; |
| 1654 | 1677 | $response->body = json_encode($object); |
| 1655 | 1678 | return $response; |
| 1656 | - } |
|
| 1657 | - elseif ($format == 'jsonp') |
|
| 1658 | - { |
|
| 1659 | - if (!$callback) |
|
| 1660 | - throw new BadRequest("'callback' parameter is required for JSONP calls"); |
|
| 1661 | - if (!preg_match("/^[a-zA-Z_][a-zA-Z0-9_]*$/", $callback)) |
|
| 1662 | - throw new InvalidParam('callback', "'$callback' doesn't seem to be a valid JavaScript function name (should match /^[a-zA-Z_][a-zA-Z0-9_]*\$/)."); |
|
| 1679 | + } elseif ($format == 'jsonp') { |
|
| 1680 | + if (!$callback) { |
|
| 1681 | + throw new BadRequest("'callback' parameter is required for JSONP calls"); |
|
| 1682 | + } |
|
| 1683 | + if (!preg_match("/^[a-zA-Z_][a-zA-Z0-9_]*$/", $callback)) { |
|
| 1684 | + throw new InvalidParam('callback', "'$callback' doesn't seem to be a valid JavaScript function name (should match /^[a-zA-Z_][a-zA-Z0-9_]*\$/)."); |
|
| 1685 | + } |
|
| 1663 | 1686 | $response = new OkapiHttpResponse(); |
| 1664 | 1687 | $response->content_type = "application/javascript; charset=utf-8"; |
| 1665 | 1688 | $response->body = $callback."(".json_encode($object).");"; |
| 1666 | 1689 | return $response; |
| 1667 | - } |
|
| 1668 | - elseif ($format == 'xmlmap') |
|
| 1669 | - { |
|
| 1690 | + } elseif ($format == 'xmlmap') { |
|
| 1670 | 1691 | # Deprecated (see issue 128). Keeping this for backward-compatibility. |
| 1671 | 1692 | $response = new OkapiHttpResponse(); |
| 1672 | 1693 | $response->content_type = "text/xml; charset=utf-8"; |
| 1673 | 1694 | $response->body = self::xmlmap_dumps($object); |
| 1674 | 1695 | return $response; |
| 1675 | - } |
|
| 1676 | - elseif ($format == 'xmlmap2') |
|
| 1677 | - { |
|
| 1696 | + } elseif ($format == 'xmlmap2') { |
|
| 1678 | 1697 | $response = new OkapiHttpResponse(); |
| 1679 | 1698 | $response->content_type = "text/xml; charset=utf-8"; |
| 1680 | 1699 | $response->body = self::xmlmap2_dumps($object); |
| 1681 | 1700 | return $response; |
| 1682 | - } |
|
| 1683 | - else |
|
| 1684 | - { |
|
| 1701 | + } else { |
|
| 1685 | 1702 | # Should not happen (as we do a proper check above). |
| 1686 | 1703 | throw new Exception(); |
| 1687 | 1704 | } |
@@ -1689,58 +1706,40 @@ discard block |
||
| 1689 | 1706 | |
| 1690 | 1707 | private static function _xmlmap_add(&$chunks, &$obj) |
| 1691 | 1708 | { |
| 1692 | - if (is_string($obj)) |
|
| 1693 | - { |
|
| 1709 | + if (is_string($obj)) { |
|
| 1694 | 1710 | $chunks[] = "<string>"; |
| 1695 | 1711 | $chunks[] = self::xmlescape($obj); |
| 1696 | 1712 | $chunks[] = "</string>"; |
| 1697 | - } |
|
| 1698 | - elseif (is_int($obj)) |
|
| 1699 | - { |
|
| 1713 | + } elseif (is_int($obj)) { |
|
| 1700 | 1714 | $chunks[] = "<int>$obj</int>"; |
| 1701 | - } |
|
| 1702 | - elseif (is_float($obj)) |
|
| 1703 | - { |
|
| 1715 | + } elseif (is_float($obj)) { |
|
| 1704 | 1716 | $chunks[] = "<float>$obj</float>"; |
| 1705 | - } |
|
| 1706 | - elseif (is_bool($obj)) |
|
| 1707 | - { |
|
| 1717 | + } elseif (is_bool($obj)) { |
|
| 1708 | 1718 | $chunks[] = $obj ? "<bool>true</bool>" : "<bool>false</bool>"; |
| 1709 | - } |
|
| 1710 | - elseif (is_null($obj)) |
|
| 1711 | - { |
|
| 1719 | + } elseif (is_null($obj)) { |
|
| 1712 | 1720 | $chunks[] = "<null/>"; |
| 1713 | - } |
|
| 1714 | - elseif (is_array($obj)) |
|
| 1715 | - { |
|
| 1721 | + } elseif (is_array($obj)) { |
|
| 1716 | 1722 | # Have to check if this is associative or not! Shit. I hate PHP. |
| 1717 | - if (array_keys($obj) === range(0, count($obj) - 1)) |
|
| 1718 | - { |
|
| 1723 | + if (array_keys($obj) === range(0, count($obj) - 1)) { |
|
| 1719 | 1724 | # Not assoc. |
| 1720 | 1725 | $chunks[] = "<list>"; |
| 1721 | - foreach ($obj as &$item_ref) |
|
| 1722 | - { |
|
| 1726 | + foreach ($obj as &$item_ref) { |
|
| 1723 | 1727 | $chunks[] = "<item>"; |
| 1724 | 1728 | self::_xmlmap_add($chunks, $item_ref); |
| 1725 | 1729 | $chunks[] = "</item>"; |
| 1726 | 1730 | } |
| 1727 | 1731 | $chunks[] = "</list>"; |
| 1728 | - } |
|
| 1729 | - else |
|
| 1730 | - { |
|
| 1732 | + } else { |
|
| 1731 | 1733 | # Assoc. |
| 1732 | 1734 | $chunks[] = "<dict>"; |
| 1733 | - foreach ($obj as $key => &$item_ref) |
|
| 1734 | - { |
|
| 1735 | + foreach ($obj as $key => &$item_ref) { |
|
| 1735 | 1736 | $chunks[] = "<item key=\"".self::xmlescape($key)."\">"; |
| 1736 | 1737 | self::_xmlmap_add($chunks, $item_ref); |
| 1737 | 1738 | $chunks[] = "</item>"; |
| 1738 | 1739 | } |
| 1739 | 1740 | $chunks[] = "</dict>"; |
| 1740 | 1741 | } |
| 1741 | - } |
|
| 1742 | - else |
|
| 1743 | - { |
|
| 1742 | + } else { |
|
| 1744 | 1743 | # That's a bug. |
| 1745 | 1744 | throw new Exception("Cannot encode as xmlmap: " + print_r($obj, true)); |
| 1746 | 1745 | } |
@@ -1749,54 +1748,36 @@ discard block |
||
| 1749 | 1748 | private static function _xmlmap2_add(&$chunks, &$obj, $key) |
| 1750 | 1749 | { |
| 1751 | 1750 | $attrs = ($key !== null) ? " key=\"".self::xmlescape($key)."\"" : ""; |
| 1752 | - if (is_string($obj)) |
|
| 1753 | - { |
|
| 1751 | + if (is_string($obj)) { |
|
| 1754 | 1752 | $chunks[] = "<string$attrs>"; |
| 1755 | 1753 | $chunks[] = self::xmlescape($obj); |
| 1756 | 1754 | $chunks[] = "</string>"; |
| 1757 | - } |
|
| 1758 | - elseif (is_int($obj)) |
|
| 1759 | - { |
|
| 1755 | + } elseif (is_int($obj)) { |
|
| 1760 | 1756 | $chunks[] = "<number$attrs>$obj</number>"; |
| 1761 | - } |
|
| 1762 | - elseif (is_float($obj)) |
|
| 1763 | - { |
|
| 1757 | + } elseif (is_float($obj)) { |
|
| 1764 | 1758 | $chunks[] = "<number$attrs>$obj</number>"; |
| 1765 | - } |
|
| 1766 | - elseif (is_bool($obj)) |
|
| 1767 | - { |
|
| 1759 | + } elseif (is_bool($obj)) { |
|
| 1768 | 1760 | $chunks[] = $obj ? "<boolean$attrs>true</boolean>" : "<boolean$attrs>false</boolean>"; |
| 1769 | - } |
|
| 1770 | - elseif (is_null($obj)) |
|
| 1771 | - { |
|
| 1761 | + } elseif (is_null($obj)) { |
|
| 1772 | 1762 | $chunks[] = "<null$attrs/>"; |
| 1773 | - } |
|
| 1774 | - elseif (is_array($obj) || ($obj instanceof ArrayObject)) |
|
| 1775 | - { |
|
| 1763 | + } elseif (is_array($obj) || ($obj instanceof ArrayObject)) { |
|
| 1776 | 1764 | # Have to check if this is associative or not! Shit. I hate PHP. |
| 1777 | - if (is_array($obj) && (array_keys($obj) === range(0, count($obj) - 1))) |
|
| 1778 | - { |
|
| 1765 | + if (is_array($obj) && (array_keys($obj) === range(0, count($obj) - 1))) { |
|
| 1779 | 1766 | # Not assoc. |
| 1780 | 1767 | $chunks[] = "<array$attrs>"; |
| 1781 | - foreach ($obj as &$item_ref) |
|
| 1782 | - { |
|
| 1768 | + foreach ($obj as &$item_ref) { |
|
| 1783 | 1769 | self::_xmlmap2_add($chunks, $item_ref, null); |
| 1784 | 1770 | } |
| 1785 | 1771 | $chunks[] = "</array>"; |
| 1786 | - } |
|
| 1787 | - else |
|
| 1788 | - { |
|
| 1772 | + } else { |
|
| 1789 | 1773 | # Assoc. |
| 1790 | 1774 | $chunks[] = "<object$attrs>"; |
| 1791 | - foreach ($obj as $key => &$item_ref) |
|
| 1792 | - { |
|
| 1775 | + foreach ($obj as $key => &$item_ref) { |
|
| 1793 | 1776 | self::_xmlmap2_add($chunks, $item_ref, $key); |
| 1794 | 1777 | } |
| 1795 | 1778 | $chunks[] = "</object>"; |
| 1796 | 1779 | } |
| 1797 | - } |
|
| 1798 | - else |
|
| 1799 | - { |
|
| 1780 | + } else { |
|
| 1800 | 1781 | # That's a bug. |
| 1801 | 1782 | throw new Exception("Cannot encode as xmlmap2: " . print_r($obj, true)); |
| 1802 | 1783 | } |
@@ -1851,8 +1832,9 @@ discard block |
||
| 1851 | 1832 | public static function cache_type_name2id($name) |
| 1852 | 1833 | { |
| 1853 | 1834 | $ref = &self::$cache_types[Settings::get('OC_BRANCH')]; |
| 1854 | - if (isset($ref[$name])) |
|
| 1855 | - return $ref[$name]; |
|
| 1835 | + if (isset($ref[$name])) { |
|
| 1836 | + return $ref[$name]; |
|
| 1837 | + } |
|
| 1856 | 1838 | throw new Exception("Method cache_type_name2id called with unsupported cache ". |
| 1857 | 1839 | "type name '$name'. You should not allow users to submit caches ". |
| 1858 | 1840 | "of non-primary type."); |
@@ -1862,14 +1844,15 @@ discard block |
||
| 1862 | 1844 | public static function cache_type_id2name($id) |
| 1863 | 1845 | { |
| 1864 | 1846 | static $reversed = null; |
| 1865 | - if ($reversed == null) |
|
| 1866 | - { |
|
| 1847 | + if ($reversed == null) { |
|
| 1867 | 1848 | $reversed = array(); |
| 1868 | - foreach (self::$cache_types[Settings::get('OC_BRANCH')] as $key => $value) |
|
| 1869 | - $reversed[$value] = $key; |
|
| 1849 | + foreach (self::$cache_types[Settings::get('OC_BRANCH')] as $key => $value) { |
|
| 1850 | + $reversed[$value] = $key; |
|
| 1851 | + } |
|
| 1852 | + } |
|
| 1853 | + if (isset($reversed[$id])) { |
|
| 1854 | + return $reversed[$id]; |
|
| 1870 | 1855 | } |
| 1871 | - if (isset($reversed[$id])) |
|
| 1872 | - return $reversed[$id]; |
|
| 1873 | 1856 | return "Other"; |
| 1874 | 1857 | } |
| 1875 | 1858 | |
@@ -1880,8 +1863,9 @@ discard block |
||
| 1880 | 1863 | /** E.g. 'Available' => 1. For unknown names throws an Exception. */ |
| 1881 | 1864 | public static function cache_status_name2id($name) |
| 1882 | 1865 | { |
| 1883 | - if (isset(self::$cache_statuses[$name])) |
|
| 1884 | - return self::$cache_statuses[$name]; |
|
| 1866 | + if (isset(self::$cache_statuses[$name])) { |
|
| 1867 | + return self::$cache_statuses[$name]; |
|
| 1868 | + } |
|
| 1885 | 1869 | throw new Exception("Method cache_status_name2id called with invalid name '$name'."); |
| 1886 | 1870 | } |
| 1887 | 1871 | |
@@ -1889,14 +1873,15 @@ discard block |
||
| 1889 | 1873 | public static function cache_status_id2name($id) |
| 1890 | 1874 | { |
| 1891 | 1875 | static $reversed = null; |
| 1892 | - if ($reversed == null) |
|
| 1893 | - { |
|
| 1876 | + if ($reversed == null) { |
|
| 1894 | 1877 | $reversed = array(); |
| 1895 | - foreach (self::$cache_statuses as $key => $value) |
|
| 1896 | - $reversed[$value] = $key; |
|
| 1878 | + foreach (self::$cache_statuses as $key => $value) { |
|
| 1879 | + $reversed[$value] = $key; |
|
| 1880 | + } |
|
| 1881 | + } |
|
| 1882 | + if (isset($reversed[$id])) { |
|
| 1883 | + return $reversed[$id]; |
|
| 1897 | 1884 | } |
| 1898 | - if (isset($reversed[$id])) |
|
| 1899 | - return $reversed[$id]; |
|
| 1900 | 1885 | return 'Archived'; |
| 1901 | 1886 | } |
| 1902 | 1887 | |
@@ -1914,8 +1899,9 @@ discard block |
||
| 1914 | 1899 | /** E.g. 'micro' => 2. For unknown names throw an Exception. */ |
| 1915 | 1900 | public static function cache_size2_to_sizeid($size2) |
| 1916 | 1901 | { |
| 1917 | - if (isset(self::$cache_sizes[$size2])) |
|
| 1918 | - return self::$cache_sizes[$size2]; |
|
| 1902 | + if (isset(self::$cache_sizes[$size2])) { |
|
| 1903 | + return self::$cache_sizes[$size2]; |
|
| 1904 | + } |
|
| 1919 | 1905 | throw new Exception("Method cache_size2_to_sizeid called with invalid size2 '$size2'."); |
| 1920 | 1906 | } |
| 1921 | 1907 | |
@@ -1923,14 +1909,15 @@ discard block |
||
| 1923 | 1909 | public static function cache_sizeid_to_size2($id) |
| 1924 | 1910 | { |
| 1925 | 1911 | static $reversed = null; |
| 1926 | - if ($reversed == null) |
|
| 1927 | - { |
|
| 1912 | + if ($reversed == null) { |
|
| 1928 | 1913 | $reversed = array(); |
| 1929 | - foreach (self::$cache_sizes as $key => $value) |
|
| 1930 | - $reversed[$value] = $key; |
|
| 1914 | + foreach (self::$cache_sizes as $key => $value) { |
|
| 1915 | + $reversed[$value] = $key; |
|
| 1916 | + } |
|
| 1917 | + } |
|
| 1918 | + if (isset($reversed[$id])) { |
|
| 1919 | + return $reversed[$id]; |
|
| 1931 | 1920 | } |
| 1932 | - if (isset($reversed[$id])) |
|
| 1933 | - return $reversed[$id]; |
|
| 1934 | 1921 | return "other"; |
| 1935 | 1922 | } |
| 1936 | 1923 | |
@@ -1953,8 +1940,9 @@ discard block |
||
| 1953 | 1940 | */ |
| 1954 | 1941 | public static function cache_size2_to_oxsize($size2) |
| 1955 | 1942 | { |
| 1956 | - if (array_key_exists($size2, self::$cache_OX_sizes)) |
|
| 1957 | - return self::$cache_OX_sizes[$size2]; |
|
| 1943 | + if (array_key_exists($size2, self::$cache_OX_sizes)) { |
|
| 1944 | + return self::$cache_OX_sizes[$size2]; |
|
| 1945 | + } |
|
| 1958 | 1946 | throw new Exception("Method cache_size2_to_oxsize called with invalid size2 '$size2'."); |
| 1959 | 1947 | } |
| 1960 | 1948 | |
@@ -1963,12 +1951,24 @@ discard block |
||
| 1963 | 1951 | */ |
| 1964 | 1952 | public static function logtypename2id($name) |
| 1965 | 1953 | { |
| 1966 | - if ($name == 'Found it') return 1; |
|
| 1967 | - if ($name == "Didn't find it") return 2; |
|
| 1968 | - if ($name == 'Comment') return 3; |
|
| 1969 | - if ($name == 'Attended') return 7; |
|
| 1970 | - if ($name == 'Will attend') return 8; |
|
| 1971 | - if (($name == 'Needs maintenance') && (Settings::get('OC_BRANCH') == 'oc.pl')) return 5; |
|
| 1954 | + if ($name == 'Found it') { |
|
| 1955 | + return 1; |
|
| 1956 | + } |
|
| 1957 | + if ($name == "Didn't find it") { |
|
| 1958 | + return 2; |
|
| 1959 | + } |
|
| 1960 | + if ($name == 'Comment') { |
|
| 1961 | + return 3; |
|
| 1962 | + } |
|
| 1963 | + if ($name == 'Attended') { |
|
| 1964 | + return 7; |
|
| 1965 | + } |
|
| 1966 | + if ($name == 'Will attend') { |
|
| 1967 | + return 8; |
|
| 1968 | + } |
|
| 1969 | + if (($name == 'Needs maintenance') && (Settings::get('OC_BRANCH') == 'oc.pl')) { |
|
| 1970 | + return 5; |
|
| 1971 | + } |
|
| 1972 | 1972 | throw new Exception("logtype2id called with invalid log type argument: $name"); |
| 1973 | 1973 | } |
| 1974 | 1974 | |
@@ -1981,21 +1981,47 @@ discard block |
||
| 1981 | 1981 | # These names are officially documented and may never change! |
| 1982 | 1982 | |
| 1983 | 1983 | # Primary. |
| 1984 | - if ($id == 1) return "Found it"; |
|
| 1985 | - if ($id == 2) return "Didn't find it"; |
|
| 1986 | - if ($id == 3) return "Comment"; |
|
| 1987 | - if ($id == 7) return "Attended"; |
|
| 1988 | - if ($id == 8) return "Will attend"; |
|
| 1984 | + if ($id == 1) { |
|
| 1985 | + return "Found it"; |
|
| 1986 | + } |
|
| 1987 | + if ($id == 2) { |
|
| 1988 | + return "Didn't find it"; |
|
| 1989 | + } |
|
| 1990 | + if ($id == 3) { |
|
| 1991 | + return "Comment"; |
|
| 1992 | + } |
|
| 1993 | + if ($id == 7) { |
|
| 1994 | + return "Attended"; |
|
| 1995 | + } |
|
| 1996 | + if ($id == 8) { |
|
| 1997 | + return "Will attend"; |
|
| 1998 | + } |
|
| 1989 | 1999 | |
| 1990 | 2000 | # Other. |
| 1991 | - if ($id == 4) return "Moved"; |
|
| 1992 | - if ($id == 5) return "Needs maintenance"; |
|
| 1993 | - if ($id == 6) return "Maintenance performed"; |
|
| 1994 | - if ($id == 9) return "Archived"; |
|
| 1995 | - if ($id == 10) return "Ready to search"; |
|
| 1996 | - if ($id == 11) return "Temporarily unavailable"; |
|
| 1997 | - if ($id == 12) return "OC Team comment"; |
|
| 1998 | - if ($id == 13 || $id == 14) return "Locked"; |
|
| 2001 | + if ($id == 4) { |
|
| 2002 | + return "Moved"; |
|
| 2003 | + } |
|
| 2004 | + if ($id == 5) { |
|
| 2005 | + return "Needs maintenance"; |
|
| 2006 | + } |
|
| 2007 | + if ($id == 6) { |
|
| 2008 | + return "Maintenance performed"; |
|
| 2009 | + } |
|
| 2010 | + if ($id == 9) { |
|
| 2011 | + return "Archived"; |
|
| 2012 | + } |
|
| 2013 | + if ($id == 10) { |
|
| 2014 | + return "Ready to search"; |
|
| 2015 | + } |
|
| 2016 | + if ($id == 11) { |
|
| 2017 | + return "Temporarily unavailable"; |
|
| 2018 | + } |
|
| 2019 | + if ($id == 12) { |
|
| 2020 | + return "OC Team comment"; |
|
| 2021 | + } |
|
| 2022 | + if ($id == 13 || $id == 14) { |
|
| 2023 | + return "Locked"; |
|
| 2024 | + } |
|
| 1999 | 2025 | |
| 2000 | 2026 | # Important: This set is not closed. Other types may be introduced |
| 2001 | 2027 | # in the future. This has to be documented in the public method |
@@ -2047,8 +2073,9 @@ discard block |
||
| 2047 | 2073 | function php_ini_get_bytes($variable) |
| 2048 | 2074 | { |
| 2049 | 2075 | $value = trim(ini_get($variable)); |
| 2050 | - if (!preg_match("/^[0-9]+[KM]?$/", $value)) |
|
| 2051 | - throw new Exception("Unexpected PHP setting: ".$variable. " = ".$value); |
|
| 2076 | + if (!preg_match("/^[0-9]+[KM]?$/", $value)) { |
|
| 2077 | + throw new Exception("Unexpected PHP setting: ".$variable. " = ".$value); |
|
| 2078 | + } |
|
| 2052 | 2079 | $value = str_replace('K', '*1024', $value); |
| 2053 | 2080 | $value = str_replace('M', '*1024*1024', $value); |
| 2054 | 2081 | $value = eval('return '.$value.';'); |
@@ -2080,8 +2107,7 @@ discard block |
||
| 2080 | 2107 | */ |
| 2081 | 2108 | public static function set($key, $value, $timeout) |
| 2082 | 2109 | { |
| 2083 | - if ($timeout == null) |
|
| 2084 | - { |
|
| 2110 | + if ($timeout == null) { |
|
| 2085 | 2111 | # The current cache implementation is ALWAYS persistent, so we will |
| 2086 | 2112 | # just replace it with a big value. |
| 2087 | 2113 | $timeout = 100*365*86400; |
@@ -2116,17 +2142,16 @@ discard block |
||
| 2116 | 2142 | /** Do 'set' on many keys at once. */ |
| 2117 | 2143 | public static function set_many($dict, $timeout) |
| 2118 | 2144 | { |
| 2119 | - if (count($dict) == 0) |
|
| 2120 | - return; |
|
| 2121 | - if ($timeout == null) |
|
| 2122 | - { |
|
| 2145 | + if (count($dict) == 0) { |
|
| 2146 | + return; |
|
| 2147 | + } |
|
| 2148 | + if ($timeout == null) { |
|
| 2123 | 2149 | # The current cache implementation is ALWAYS persistent, so we will |
| 2124 | 2150 | # just replace it with a big value. |
| 2125 | 2151 | $timeout = 100*365*86400; |
| 2126 | 2152 | } |
| 2127 | 2153 | $entries_escaped = array(); |
| 2128 | - foreach ($dict as $key => $value) |
|
| 2129 | - { |
|
| 2154 | + foreach ($dict as $key => $value) { |
|
| 2130 | 2155 | $entries_escaped[] = "( |
| 2131 | 2156 | '".Db::escape_string($key)."', |
| 2132 | 2157 | '".Db::escape_string(gzdeflate(serialize($value)))."', |
@@ -2153,15 +2178,18 @@ discard block |
||
| 2153 | 2178 | and expires > now() |
| 2154 | 2179 | "); |
| 2155 | 2180 | list($blob, $score) = Db::fetch_row($rs); |
| 2156 | - if (!$blob) |
|
| 2157 | - return null; |
|
| 2158 | - if ($score != null) # Only non-null entries are scored. |
|
| 2181 | + if (!$blob) { |
|
| 2182 | + return null; |
|
| 2183 | + } |
|
| 2184 | + if ($score != null) { |
|
| 2185 | + # Only non-null entries are scored. |
|
| 2159 | 2186 | { |
| 2160 | 2187 | Db::execute(" |
| 2161 | 2188 | insert into okapi_cache_reads (`cache_key`) |
| 2162 | 2189 | values ('".Db::escape_string($key)."') |
| 2163 | 2190 | "); |
| 2164 | 2191 | } |
| 2192 | + } |
|
| 2165 | 2193 | return unserialize(gzinflate($blob)); |
| 2166 | 2194 | } |
| 2167 | 2195 | |
@@ -2176,14 +2204,10 @@ discard block |
||
| 2176 | 2204 | `key` in ('".implode("','", array_map('\okapi\Db::escape_string', $keys))."') |
| 2177 | 2205 | and expires > now() |
| 2178 | 2206 | "); |
| 2179 | - while ($row = Db::fetch_assoc($rs)) |
|
| 2180 | - { |
|
| 2181 | - try |
|
| 2182 | - { |
|
| 2207 | + while ($row = Db::fetch_assoc($rs)) { |
|
| 2208 | + try { |
|
| 2183 | 2209 | $dict[$row['key']] = unserialize(gzinflate($row['value'])); |
| 2184 | - } |
|
| 2185 | - catch (ErrorException $e) |
|
| 2186 | - { |
|
| 2210 | + } catch (ErrorException $e) { |
|
| 2187 | 2211 | unset($dict[$row['key']]); |
| 2188 | 2212 | Okapi::mail_admins("Debug: Unserialize error", |
| 2189 | 2213 | "Could not unserialize key '".$row['key']."' from Cache.\n". |
@@ -2192,10 +2216,11 @@ discard block |
||
| 2192 | 2216 | "Length of data, compressed: ".strlen($row['value'])); |
| 2193 | 2217 | } |
| 2194 | 2218 | } |
| 2195 | - if (count($dict) < count($keys)) |
|
| 2196 | - foreach ($keys as $key) |
|
| 2219 | + if (count($dict) < count($keys)) { |
|
| 2220 | + foreach ($keys as $key) |
|
| 2197 | 2221 | if (!isset($dict[$key])) |
| 2198 | 2222 | $dict[$key] = null; |
| 2223 | + } |
|
| 2199 | 2224 | return $dict; |
| 2200 | 2225 | } |
| 2201 | 2226 | |
@@ -2210,8 +2235,9 @@ discard block |
||
| 2210 | 2235 | /** Do 'delete' on many keys at once. */ |
| 2211 | 2236 | public static function delete_many($keys) |
| 2212 | 2237 | { |
| 2213 | - if (count($keys) == 0) |
|
| 2214 | - return; |
|
| 2238 | + if (count($keys) == 0) { |
|
| 2239 | + return; |
|
| 2240 | + } |
|
| 2215 | 2241 | Db::execute(" |
| 2216 | 2242 | delete from okapi_cache |
| 2217 | 2243 | where `key` in ('".implode("','", array_map('\okapi\Db::escape_string', $keys))."') |
@@ -2229,8 +2255,9 @@ discard block |
||
| 2229 | 2255 | public static function get_file_path($key) |
| 2230 | 2256 | { |
| 2231 | 2257 | $filename = Okapi::get_var_dir()."/okapi_filecache_".md5($key); |
| 2232 | - if (!file_exists($filename)) |
|
| 2233 | - return null; |
|
| 2258 | + if (!file_exists($filename)) { |
|
| 2259 | + return null; |
|
| 2260 | + } |
|
| 2234 | 2261 | return $filename; |
| 2235 | 2262 | } |
| 2236 | 2263 | |
@@ -2316,17 +2343,19 @@ discard block |
||
| 2316 | 2343 | $this->consumer = $consumer; |
| 2317 | 2344 | $this->token = $token; |
| 2318 | 2345 | $this->parameters = array(); |
| 2319 | - foreach ($parameters as $key => $value) |
|
| 2320 | - if ($value !== null) |
|
| 2346 | + foreach ($parameters as $key => $value) { |
|
| 2347 | + if ($value !== null) |
|
| 2321 | 2348 | $this->parameters[$key] = $value; |
| 2349 | + } |
|
| 2322 | 2350 | } |
| 2323 | 2351 | |
| 2324 | 2352 | public function get_parameter($name) |
| 2325 | 2353 | { |
| 2326 | - if (isset($this->parameters[$name])) |
|
| 2327 | - return $this->parameters[$name]; |
|
| 2328 | - else |
|
| 2329 | - return null; |
|
| 2354 | + if (isset($this->parameters[$name])) { |
|
| 2355 | + return $this->parameters[$name]; |
|
| 2356 | + } else { |
|
| 2357 | + return null; |
|
| 2358 | + } |
|
| 2330 | 2359 | } |
| 2331 | 2360 | |
| 2332 | 2361 | public function get_all_parameters_including_unknown() |
@@ -2334,7 +2363,9 @@ discard block |
||
| 2334 | 2363 | return $this->parameters; |
| 2335 | 2364 | } |
| 2336 | 2365 | |
| 2337 | - public function is_http_request() { return $this->perceive_as_http_request; } |
|
| 2366 | + public function is_http_request() |
|
| 2367 | + { |
|
| 2368 | +return $this->perceive_as_http_request; } |
|
| 2338 | 2369 | } |
| 2339 | 2370 | |
| 2340 | 2371 | class OkapiHttpRequest extends OkapiRequest |
@@ -2351,20 +2382,16 @@ discard block |
||
| 2351 | 2382 | # Parsing options. |
| 2352 | 2383 | # |
| 2353 | 2384 | $DEBUG_AS_USERNAME = null; |
| 2354 | - foreach ($options as $key => $value) |
|
| 2355 | - { |
|
| 2356 | - switch ($key) |
|
| 2357 | - { |
|
| 2385 | + foreach ($options as $key => $value) { |
|
| 2386 | + switch ($key) { |
|
| 2358 | 2387 | case 'min_auth_level': |
| 2359 | - if (!in_array($value, array(0, 1, 2, 3))) |
|
| 2360 | - { |
|
| 2388 | + if (!in_array($value, array(0, 1, 2, 3))) { |
|
| 2361 | 2389 | throw new Exception("'min_auth_level' option has invalid value: $value"); |
| 2362 | 2390 | } |
| 2363 | 2391 | $this->opt_min_auth_level = $value; |
| 2364 | 2392 | break; |
| 2365 | 2393 | case 'token_type': |
| 2366 | - if (!in_array($value, array("request", "access"))) |
|
| 2367 | - { |
|
| 2394 | + if (!in_array($value, array("request", "access"))) { |
|
| 2368 | 2395 | throw new Exception("'token_type' option has invalid value: $value"); |
| 2369 | 2396 | } |
| 2370 | 2397 | $this->opt_token_type = $value; |
@@ -2377,15 +2404,15 @@ discard block |
||
| 2377 | 2404 | break; |
| 2378 | 2405 | } |
| 2379 | 2406 | } |
| 2380 | - if ($this->opt_min_auth_level === null) throw new Exception("Required 'min_auth_level' option is missing."); |
|
| 2407 | + if ($this->opt_min_auth_level === null) { |
|
| 2408 | + throw new Exception("Required 'min_auth_level' option is missing."); |
|
| 2409 | + } |
|
| 2381 | 2410 | |
| 2382 | - if ($DEBUG_AS_USERNAME != null) |
|
| 2383 | - { |
|
| 2411 | + if ($DEBUG_AS_USERNAME != null) { |
|
| 2384 | 2412 | # Enables debugging Level 2 and Level 3 methods. Should not be committed |
| 2385 | 2413 | # at any time! If run on production server, make it an error. |
| 2386 | 2414 | |
| 2387 | - if (!Settings::get('DEBUG')) |
|
| 2388 | - { |
|
| 2415 | + if (!Settings::get('DEBUG')) { |
|
| 2389 | 2416 | throw new Exception("Attempted to use DEBUG_AS_USERNAME in ". |
| 2390 | 2417 | "non-debug environment. Accidental commit?"); |
| 2391 | 2418 | } |
@@ -2400,8 +2427,7 @@ discard block |
||
| 2400 | 2427 | # It it's not, check if it isn't against the rules defined in the $options. |
| 2401 | 2428 | # |
| 2402 | 2429 | |
| 2403 | - if ($this->get_parameter('oauth_signature')) |
|
| 2404 | - { |
|
| 2430 | + if ($this->get_parameter('oauth_signature')) { |
|
| 2405 | 2431 | # User is using OAuth. |
| 2406 | 2432 | |
| 2407 | 2433 | # Check for duplicate keys in the parameters. (Datastore doesn't |
@@ -2416,36 +2442,31 @@ discard block |
||
| 2416 | 2442 | |
| 2417 | 2443 | list($this->consumer, $this->token) = Okapi::$server-> |
| 2418 | 2444 | verify_request2($this->request, $this->opt_token_type, $this->opt_min_auth_level == 3); |
| 2419 | - if ($this->get_parameter('consumer_key') && $this->get_parameter('consumer_key') != $this->get_parameter('oauth_consumer_key')) |
|
| 2420 | - throw new BadRequest("Inproper mixing of authentication types. You used both 'consumer_key' ". |
|
| 2445 | + if ($this->get_parameter('consumer_key') && $this->get_parameter('consumer_key') != $this->get_parameter('oauth_consumer_key')) { |
|
| 2446 | + throw new BadRequest("Inproper mixing of authentication types. You used both 'consumer_key' ". |
|
| 2421 | 2447 | "and 'oauth_consumer_key' parameters (Level 1 and Level 2), but they do not match with ". |
| 2422 | 2448 | "each other. Were you trying to hack me? ;)"); |
| 2423 | - if ($this->opt_min_auth_level == 3 && !$this->token) |
|
| 2424 | - { |
|
| 2449 | + } |
|
| 2450 | + if ($this->opt_min_auth_level == 3 && !$this->token) { |
|
| 2425 | 2451 | throw new BadRequest("This method requires a valid Token to be included (Level 3 ". |
| 2426 | 2452 | "Authentication). You didn't provide one."); |
| 2427 | 2453 | } |
| 2428 | - } |
|
| 2429 | - else |
|
| 2430 | - { |
|
| 2431 | - if ($this->opt_min_auth_level >= 2) |
|
| 2432 | - { |
|
| 2454 | + } else { |
|
| 2455 | + if ($this->opt_min_auth_level >= 2) { |
|
| 2433 | 2456 | throw new BadRequest("This method requires OAuth signature (Level ". |
| 2434 | 2457 | $this->opt_min_auth_level." Authentication). You didn't sign your request."); |
| 2435 | - } |
|
| 2436 | - else |
|
| 2437 | - { |
|
| 2458 | + } else { |
|
| 2438 | 2459 | $consumer_key = $this->get_parameter('consumer_key'); |
| 2439 | - if ($consumer_key) |
|
| 2440 | - { |
|
| 2460 | + if ($consumer_key) { |
|
| 2441 | 2461 | $this->consumer = Okapi::$data_store->lookup_consumer($consumer_key); |
| 2442 | 2462 | if (!$this->consumer) { |
| 2443 | 2463 | throw new InvalidParam('consumer_key', "Consumer does not exist."); |
| 2444 | 2464 | } |
| 2445 | 2465 | } |
| 2446 | - if (($this->opt_min_auth_level == 1) && (!$this->consumer)) |
|
| 2447 | - throw new BadRequest("This method requires the 'consumer_key' argument (Level 1 ". |
|
| 2466 | + if (($this->opt_min_auth_level == 1) && (!$this->consumer)) { |
|
| 2467 | + throw new BadRequest("This method requires the 'consumer_key' argument (Level 1 ". |
|
| 2448 | 2468 | "Authentication). You didn't provide one."); |
| 2469 | + } |
|
| 2449 | 2470 | } |
| 2450 | 2471 | } |
| 2451 | 2472 | |
@@ -2472,23 +2493,24 @@ discard block |
||
| 2472 | 2493 | |
| 2473 | 2494 | # When debugging, simulate as if been run using a proper Level 3 Authentication. |
| 2474 | 2495 | |
| 2475 | - if ($DEBUG_AS_USERNAME != null) |
|
| 2476 | - { |
|
| 2496 | + if ($DEBUG_AS_USERNAME != null) { |
|
| 2477 | 2497 | # Note, that this will override any other valid authentication the |
| 2478 | 2498 | # developer might have issued. |
| 2479 | 2499 | |
| 2480 | 2500 | $debug_user_id = Db::select_value("select user_id from user where username='". |
| 2481 | 2501 | Db::escape_string($options['DEBUG_AS_USERNAME'])."'"); |
| 2482 | - if ($debug_user_id == null) |
|
| 2483 | - throw new Exception("Invalid user name in DEBUG_AS_USERNAME: '".$options['DEBUG_AS_USERNAME']."'"); |
|
| 2502 | + if ($debug_user_id == null) { |
|
| 2503 | + throw new Exception("Invalid user name in DEBUG_AS_USERNAME: '".$options['DEBUG_AS_USERNAME']."'"); |
|
| 2504 | + } |
|
| 2484 | 2505 | $this->consumer = new OkapiDebugConsumer(); |
| 2485 | 2506 | $this->token = new OkapiDebugAccessToken($debug_user_id); |
| 2486 | 2507 | } |
| 2487 | 2508 | |
| 2488 | 2509 | # Read the ETag. |
| 2489 | 2510 | |
| 2490 | - if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) |
|
| 2491 | - $this->etag = $_SERVER['HTTP_IF_NONE_MATCH']; |
|
| 2511 | + if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) { |
|
| 2512 | + $this->etag = $_SERVER['HTTP_IF_NONE_MATCH']; |
|
| 2513 | + } |
|
| 2492 | 2514 | } |
| 2493 | 2515 | |
| 2494 | 2516 | private function init_request() |
@@ -2536,8 +2558,9 @@ discard block |
||
| 2536 | 2558 | # in OKAPI and should be reported back. See issue 85: |
| 2537 | 2559 | # https://github.com/opencaching/okapi/issues/85 |
| 2538 | 2560 | |
| 2539 | - if (is_array($value)) |
|
| 2540 | - throw new InvalidParam($name, "Make sure you are using '$name' no more than ONCE in your URL."); |
|
| 2561 | + if (is_array($value)) { |
|
| 2562 | + throw new InvalidParam($name, "Make sure you are using '$name' no more than ONCE in your URL."); |
|
| 2563 | + } |
|
| 2541 | 2564 | return $value; |
| 2542 | 2565 | } |
| 2543 | 2566 | |
@@ -2546,5 +2569,7 @@ discard block |
||
| 2546 | 2569 | return $this->request->get_parameters(); |
| 2547 | 2570 | } |
| 2548 | 2571 | |
| 2549 | - public function is_http_request() { return true; } |
|
| 2572 | + public function is_http_request() |
|
| 2573 | + { |
|
| 2574 | +return true; } |
|
| 2550 | 2575 | } |
@@ -22,8 +22,7 @@ |
||
| 22 | 22 | |
| 23 | 23 | # Ensure a user is logged in. |
| 24 | 24 | |
| 25 | - if ($OC_user_id == null) |
|
| 26 | - { |
|
| 25 | + if ($OC_user_id == null) { |
|
| 27 | 26 | $after_login = "okapi/apps/"; # it is correct, if you're wondering |
| 28 | 27 | $login_url = Settings::get('SITE_URL')."login.php?target=".urlencode($after_login); |
| 29 | 28 | return new OkapiRedirectResponse($login_url); |
@@ -20,8 +20,9 @@ discard block |
||
| 20 | 20 | $langpref = isset($_GET['langpref']) ? $_GET['langpref'] : Settings::get('SITELANG'); |
| 21 | 21 | $langprefs = explode("|", $langpref); |
| 22 | 22 | $locales = array(); |
| 23 | - foreach (Locales::$languages as $lang => $attrs) |
|
| 24 | - $locales[$attrs['locale']] = $attrs; |
|
| 23 | + foreach (Locales::$languages as $lang => $attrs) { |
|
| 24 | + $locales[$attrs['locale']] = $attrs; |
|
| 25 | + } |
|
| 25 | 26 | |
| 26 | 27 | # Current implementation of the "interactivity" parameter is: If developer |
| 27 | 28 | # wants to "confirm_user", then just log out the current user before we |
@@ -48,8 +49,7 @@ discard block |
||
| 48 | 49 | |
| 49 | 50 | $callback_concat_char = (strpos($token['callback'], '?') === false) ? "?" : "&"; |
| 50 | 51 | |
| 51 | - if (!$token) |
|
| 52 | - { |
|
| 52 | + if (!$token) { |
|
| 53 | 53 | # Probably Request Token has expired. This will be usually viewed |
| 54 | 54 | # by the user, who knows nothing on tokens and OAuth. Let's be nice then! |
| 55 | 55 | |
@@ -79,26 +79,21 @@ discard block |
||
| 79 | 79 | |
| 80 | 80 | # Ensure a user is logged in (or force re-login). |
| 81 | 81 | |
| 82 | - if ($force_relogin || ($OC_user_id == null)) |
|
| 83 | - { |
|
| 82 | + if ($force_relogin || ($OC_user_id == null)) { |
|
| 84 | 83 | # TODO: confirm_user should first ask the user if he's "the proper one", |
| 85 | 84 | # and then offer to sign in as a different user. |
| 86 | 85 | |
| 87 | 86 | $login_page = 'login.php?'; |
| 88 | 87 | |
| 89 | - if ($OC_user_id !== null) |
|
| 90 | - { |
|
| 91 | - if (Settings::get('OC_BRANCH') == 'oc.de') |
|
| 92 | - { |
|
| 88 | + if ($OC_user_id !== null) { |
|
| 89 | + if (Settings::get('OC_BRANCH') == 'oc.de') { |
|
| 93 | 90 | # OCDE login.php?action=logout&target=... will NOT logout and |
| 94 | 91 | # then redirect to the target, but it will log out, prompt for |
| 95 | 92 | # login and then redirect to the target after logging in - |
| 96 | 93 | # that's exactly the relogin that we want. |
| 97 | 94 | |
| 98 | 95 | $login_page .= 'action=logout&'; |
| 99 | - } |
|
| 100 | - else |
|
| 101 | - { |
|
| 96 | + } else { |
|
| 102 | 97 | # OCPL uses REAL MAGIC for session handling. I don't get ANY of it. |
| 103 | 98 | # The logout.php DOES NOT support the "target" parameter, so we |
| 104 | 99 | # can't just call it. The only thing that comes to mind is... |
@@ -141,15 +136,12 @@ discard block |
||
| 141 | 136 | and consumer_key = '".Db::escape_string($token['consumer_key'])."' |
| 142 | 137 | ", 0); |
| 143 | 138 | |
| 144 | - if (!$authorized) |
|
| 145 | - { |
|
| 146 | - if (isset($_POST['authorization_result'])) |
|
| 147 | - { |
|
| 139 | + if (!$authorized) { |
|
| 140 | + if (isset($_POST['authorization_result'])) { |
|
| 148 | 141 | # Not yet authorized, but user have just submitted the authorization form. |
| 149 | 142 | # WRTODO: CSRF protection |
| 150 | 143 | |
| 151 | - if ($_POST['authorization_result'] == 'granted') |
|
| 152 | - { |
|
| 144 | + if ($_POST['authorization_result'] == 'granted') { |
|
| 153 | 145 | Db::execute(" |
| 154 | 146 | insert ignore into okapi_authorizations (consumer_key, user_id) |
| 155 | 147 | values ( |
@@ -158,9 +150,7 @@ discard block |
||
| 158 | 150 | ); |
| 159 | 151 | "); |
| 160 | 152 | $authorized = true; |
| 161 | - } |
|
| 162 | - else |
|
| 163 | - { |
|
| 153 | + } else { |
|
| 164 | 154 | # User denied access. Nothing sensible to do now. Will try to report |
| 165 | 155 | # back to the Consumer application with an error. |
| 166 | 156 | |
@@ -175,9 +165,7 @@ discard block |
||
| 175 | 165 | return new OkapiRedirectResponse(Settings::get('SITE_URL')."index.php"); |
| 176 | 166 | } |
| 177 | 167 | } |
| 178 | - } |
|
| 179 | - else |
|
| 180 | - { |
|
| 168 | + } else { |
|
| 181 | 169 | # Not yet authorized. Display an authorization request. |
| 182 | 170 | $vars = array( |
| 183 | 171 | 'okapi_base_url' => Settings::get('SITE_URL')."okapi/", |
@@ -23,8 +23,7 @@ discard block |
||
| 23 | 23 | require_once($GLOBALS['rootpath']."okapi/lib/oc_session.php"); |
| 24 | 24 | $OC_user_id = OCSession::get_user_id(); |
| 25 | 25 | |
| 26 | - if ($OC_user_id == null) |
|
| 27 | - { |
|
| 26 | + if ($OC_user_id == null) { |
|
| 28 | 27 | $after_login = "okapi/apps/".(($langpref != Settings::get('SITELANG'))?"?langpref=".$langpref:""); |
| 29 | 28 | $login_url = Settings::get('SITE_URL')."login.php?target=".urlencode($after_login); |
| 30 | 29 | return new OkapiRedirectResponse($login_url); |
@@ -48,8 +47,9 @@ discard block |
||
| 48 | 47 | $vars['site_name'] = Okapi::get_normalized_site_name(); |
| 49 | 48 | $vars['site_logo'] = Settings::get('SITE_LOGO'); |
| 50 | 49 | $vars['apps'] = array(); |
| 51 | - while ($row = Db::fetch_assoc($rs)) |
|
| 52 | - $vars['apps'][] = $row; |
|
| 50 | + while ($row = Db::fetch_assoc($rs)) { |
|
| 51 | + $vars['apps'][] = $row; |
|
| 52 | + } |
|
| 53 | 53 | Db::free_result($rs); |
| 54 | 54 | |
| 55 | 55 | $response = new OkapiHttpResponse(); |