@@ -54,8 +54,7 @@ discard block |
||
| 54 | 54 | $path = $GLOBALS['rootpath']."okapi/services/attrs/attribute-definitions.xml"; |
| 55 | 55 | $xml = file_get_contents($path); |
| 56 | 56 | self::refresh_from_string($xml); |
| 57 | - } |
|
| 58 | - catch (Exception $e) |
|
| 57 | + } catch (Exception $e) |
|
| 59 | 58 | { |
| 60 | 59 | # Failed to read or parse the file (i.e. after a syntax error was |
| 61 | 60 | # commited). Let's check when the last successful parse occured. |
@@ -127,13 +126,15 @@ discard block |
||
| 127 | 126 | /* It is used by THIS OC site. */ |
| 128 | 127 | |
| 129 | 128 | $internal_id = (int)$ocnode['id']; |
| 130 | - if (isset($all_internal_ids[$internal_id])) |
|
| 131 | - throw new Exception("The internal attribute ".$internal_id. |
|
| 129 | + if (isset($all_internal_ids[$internal_id])) { |
|
| 130 | + throw new Exception("The internal attribute ".$internal_id. |
|
| 132 | 131 | " has multiple assigments to OKAPI attributes."); |
| 132 | + } |
|
| 133 | 133 | $all_internal_ids[$internal_id] = true; |
| 134 | - if (!is_null($attr['internal_id'])) |
|
| 135 | - throw new Exception("There are multiple internal IDs for the ". |
|
| 134 | + if (!is_null($attr['internal_id'])) { |
|
| 135 | + throw new Exception("There are multiple internal IDs for the ". |
|
| 136 | 136 | $attr['acode']." attribute."); |
| 137 | + } |
|
| 137 | 138 | $attr['internal_id'] = $internal_id; |
| 138 | 139 | } |
| 139 | 140 | } |
@@ -142,14 +143,16 @@ discard block |
||
| 142 | 143 | $lang = (string)$langnode['id']; |
| 143 | 144 | foreach ($langnode->name as $namenode) |
| 144 | 145 | { |
| 145 | - if (isset($attr['names'][$lang])) |
|
| 146 | - throw new Exception("Duplicate ".$lang." name of attribute ".$attr['acode']); |
|
| 146 | + if (isset($attr['names'][$lang])) { |
|
| 147 | + throw new Exception("Duplicate ".$lang." name of attribute ".$attr['acode']); |
|
| 148 | + } |
|
| 147 | 149 | $attr['names'][$lang] = (string)$namenode; |
| 148 | 150 | } |
| 149 | 151 | foreach ($langnode->desc as $descnode) |
| 150 | 152 | { |
| 151 | - if (isset($attr['descriptions'][$lang])) |
|
| 152 | - throw new Exception("Duplicate ".$lang." description of attribute ".$attr['acode']); |
|
| 153 | + if (isset($attr['descriptions'][$lang])) { |
|
| 154 | + throw new Exception("Duplicate ".$lang." description of attribute ".$attr['acode']); |
|
| 155 | + } |
|
| 153 | 156 | $xml = $descnode->asxml(); /* contains "<desc>" and "</desc>" */ |
| 154 | 157 | $innerxml = preg_replace("/(^[^>]+>)|(<[^<]+$)/us", "", $xml); |
| 155 | 158 | $attr['descriptions'][$lang] = self::cleanup_string($innerxml); |
@@ -210,8 +213,7 @@ discard block |
||
| 210 | 213 | self::refresh_now(); |
| 211 | 214 | self::init_from_cache(false); |
| 212 | 215 | return; |
| 213 | - } |
|
| 214 | - else |
|
| 216 | + } else |
|
| 215 | 217 | { |
| 216 | 218 | $cachedvalue = array( |
| 217 | 219 | 'attr_dict' => array(), |
@@ -244,8 +246,9 @@ discard block |
||
| 244 | 246 | public static function get_internal_id_to_acode_mapping() |
| 245 | 247 | { |
| 246 | 248 | static $mapping = null; |
| 247 | - if ($mapping !== null) |
|
| 248 | - return $mapping; |
|
| 249 | + if ($mapping !== null) { |
|
| 250 | + return $mapping; |
|
| 251 | + } |
|
| 249 | 252 | |
| 250 | 253 | $cache_key = "attrhelper/id2acode/".Okapi::$git_revision.self::cache_key_suffix(); |
| 251 | 254 | $mapping = Cache::get($cache_key); |
@@ -253,8 +256,9 @@ discard block |
||
| 253 | 256 | { |
| 254 | 257 | self::init_from_cache(); |
| 255 | 258 | $mapping = array(); |
| 256 | - foreach (self::$attr_dict as $acode => &$attr_ref) |
|
| 257 | - $mapping[$attr_ref['internal_id']] = $acode; |
|
| 259 | + foreach (self::$attr_dict as $acode => &$attr_ref) { |
|
| 260 | + $mapping[$attr_ref['internal_id']] = $acode; |
|
| 261 | + } |
|
| 258 | 262 | Cache::set($cache_key, $mapping, self::ttl()); |
| 259 | 263 | } |
| 260 | 264 | return $mapping; |
@@ -267,8 +271,9 @@ discard block |
||
| 267 | 271 | public static function get_acode_to_name_mapping($langpref) |
| 268 | 272 | { |
| 269 | 273 | static $mapping = null; |
| 270 | - if ($mapping !== null) |
|
| 271 | - return $mapping; |
|
| 274 | + if ($mapping !== null) { |
|
| 275 | + return $mapping; |
|
| 276 | + } |
|
| 272 | 277 | |
| 273 | 278 | $cache_key = md5(serialize(array("attrhelper/acode2name", $langpref, |
| 274 | 279 | Okapi::$git_revision, self::cache_key_suffix()))); |
@@ -49,8 +49,7 @@ discard block |
||
| 49 | 49 | if (!$doc) { |
| 50 | 50 | throw new ErrorException(); # just to get to the catch block |
| 51 | 51 | } |
| 52 | - } |
|
| 53 | - catch (ErrorException $e) |
|
| 52 | + } catch (ErrorException $e) |
|
| 54 | 53 | { |
| 55 | 54 | # GitHub failed on us. Try to respond with a backup list. |
| 56 | 55 | |
@@ -80,21 +79,24 @@ discard block |
||
| 80 | 79 | foreach ($doc->installation as $inst) |
| 81 | 80 | { |
| 82 | 81 | $site_url = (string)$inst[0]['site_url']; |
| 83 | - if ($inst[0]['okapi_base_url']) |
|
| 84 | - $okapi_base_url = (string)$inst[0]['okapi_base_url']; |
|
| 85 | - else |
|
| 86 | - $okapi_base_url = $site_url."okapi/"; |
|
| 87 | - if ($inst[0]['site_name']) |
|
| 88 | - $site_name = (string)$inst[0]['site_name']; |
|
| 89 | - else |
|
| 90 | - $site_name = Okapi::get_normalized_site_name($site_url); |
|
| 82 | + if ($inst[0]['okapi_base_url']) { |
|
| 83 | + $okapi_base_url = (string)$inst[0]['okapi_base_url']; |
|
| 84 | + } else { |
|
| 85 | + $okapi_base_url = $site_url."okapi/"; |
|
| 86 | + } |
|
| 87 | + if ($inst[0]['site_name']) { |
|
| 88 | + $site_name = (string)$inst[0]['site_name']; |
|
| 89 | + } else { |
|
| 90 | + $site_name = Okapi::get_normalized_site_name($site_url); |
|
| 91 | + } |
|
| 91 | 92 | $results[] = array( |
| 92 | 93 | 'site_url' => $site_url, |
| 93 | 94 | 'site_name' => $site_name, |
| 94 | 95 | 'okapi_base_url' => $okapi_base_url, |
| 95 | 96 | ); |
| 96 | - if ($site_url == Settings::get('SITE_URL')) |
|
| 97 | - $i_was_included = true; |
|
| 97 | + if ($site_url == Settings::get('SITE_URL')) { |
|
| 98 | + $i_was_included = true; |
|
| 99 | + } |
|
| 98 | 100 | } |
| 99 | 101 | |
| 100 | 102 | # If running on a local development installation, then include the local |
@@ -249,41 +249,53 @@ discard block |
||
| 249 | 249 | |
| 250 | 250 | private static function verify(&$dict) |
| 251 | 251 | { |
| 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?"); |
|
| 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?"); |
|
| 254 | + } |
|
| 254 | 255 | $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))) |
|
| 256 | + foreach ($boolean_keys as $key) { |
|
| 257 | + if (!in_array($dict[$key], array(true, false))) |
|
| 257 | 258 | 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) |
|
| 259 | + } |
|
| 260 | + if (count($dict['ADMINS']) == 0) { |
|
| 261 | + throw new Exception("ADMINS array has to filled (e.g. array('root@localhost'))."); |
|
| 262 | + } |
|
| 263 | + if ($dict['DEBUG'] == false) { |
|
| 264 | + foreach ($dict as $k => $v) |
|
| 262 | 265 | if ((strpos($k, 'DEBUG_') === 0) && $v == true) |
| 263 | 266 | 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] == '/')) |
|
| 267 | + } |
|
| 268 | + if ($dict['VAR_DIR'] == null) { |
|
| 269 | + throw new Exception("VAR_DIR cannot be null. Please provide a valid directory."); |
|
| 270 | + } |
|
| 271 | + if ($dict['IMAGES_DIR'] == null) { |
|
| 272 | + throw new Exception("IMAGES_DIR cannot be null. Please provide a valid directory."); |
|
| 273 | + } |
|
| 274 | + foreach ($dict as $k => $v) { |
|
| 275 | + if ((strpos($k, '_DIR') !== false) && ($k[strlen($k) - 1] == '/')) |
|
| 270 | 276 | throw new Exception("None of the *_DIR settings may end with a slash. Check $k."); |
| 277 | + } |
|
| 271 | 278 | $notnull = array( |
| 272 | 279 | 'OC_COOKIE_NAME', 'DB_SERVER', 'DB_NAME', 'DB_USERNAME', 'SITE_URL', |
| 273 | 280 | 'IMAGES_URL', 'OC_NODE_ID'); |
| 274 | - foreach ($notnull as $k) |
|
| 275 | - if ($dict[$k] === null) |
|
| 281 | + foreach ($notnull as $k) { |
|
| 282 | + if ($dict[$k] === null) |
|
| 276 | 283 | throw new Exception("$k cannot be null."); |
| 284 | + } |
|
| 277 | 285 | $slash_keys = array('SITE_URL', 'IMAGES_URL'); |
| 278 | - foreach ($slash_keys as $key) |
|
| 279 | - if ($dict[$key][strlen($dict[$key]) - 1] != '/') |
|
| 286 | + foreach ($slash_keys as $key) { |
|
| 287 | + if ($dict[$key][strlen($dict[$key]) - 1] != '/') |
|
| 280 | 288 | 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.'); |
|
| 289 | + } |
|
| 290 | + if ($dict['REGISTRATION_URL'] === null) { |
|
| 291 | + $dict['REGISTRATION_URL'] = $dict['SITE_URL'] . 'register.php'; |
|
| 292 | + } |
|
| 293 | + if ($dict['SITE_LOGO'] === null) { |
|
| 294 | + $dict['SITE_LOGO'] = $dict['SITE_URL'] . 'okapi/static/oc_logo.png'; |
|
| 295 | + } |
|
| 296 | + if ($dict['JPEG_QUALITY'] < 50 || $dict['JPEG_QUALITY'] > 100) { |
|
| 297 | + throw new Exception('JPEG_QUALITY must be between 50 and 100.'); |
|
| 298 | + } |
|
| 287 | 299 | |
| 288 | 300 | # The OKAPI code is only compatible with utf8 and utf8mb4 charsets. |
| 289 | 301 | if (!in_array($dict['DB_CHARSET'], array('utf8', 'utf8mb4'))) { |
@@ -296,11 +308,13 @@ discard block |
||
| 296 | 308 | */ |
| 297 | 309 | public static function get($key) |
| 298 | 310 | { |
| 299 | - if (self::$SETTINGS == null) |
|
| 300 | - self::load_settings(); |
|
| 311 | + if (self::$SETTINGS == null) { |
|
| 312 | + self::load_settings(); |
|
| 313 | + } |
|
| 301 | 314 | |
| 302 | - if (!array_key_exists($key, self::$SETTINGS)) |
|
| 303 | - throw new Exception("Tried to access an invalid settings key: '$key'"); |
|
| 315 | + if (!array_key_exists($key, self::$SETTINGS)) { |
|
| 316 | + throw new Exception("Tried to access an invalid settings key: '$key'"); |
|
| 317 | + } |
|
| 304 | 318 | |
| 305 | 319 | return self::$SETTINGS[$key]; |
| 306 | 320 | } |
@@ -473,8 +473,9 @@ discard block |
||
| 473 | 473 | "oauth_nonce" => OAuthRequest::generate_nonce(), |
| 474 | 474 | "oauth_timestamp" => OAuthRequest::generate_timestamp(), |
| 475 | 475 | "oauth_consumer_key" => $consumer->key); |
| 476 | - if ($token) |
|
| 477 | - $defaults['oauth_token'] = $token->key; |
|
| 476 | + if ($token) { |
|
| 477 | + $defaults['oauth_token'] = $token->key; |
|
| 478 | + } |
|
| 478 | 479 | |
| 479 | 480 | $parameters = array_merge($defaults, $parameters); |
| 480 | 481 | |
@@ -597,12 +598,15 @@ discard block |
||
| 597 | 598 | if($realm) { |
| 598 | 599 | $out = 'Authorization: OAuth realm="' . OAuthUtil::urlencode_rfc3986($realm) . '"'; |
| 599 | 600 | $first = false; |
| 600 | - } else |
|
| 601 | - $out = 'Authorization: OAuth'; |
|
| 601 | + } else { |
|
| 602 | + $out = 'Authorization: OAuth'; |
|
| 603 | + } |
|
| 602 | 604 | |
| 603 | 605 | $total = array(); |
| 604 | 606 | foreach ($this->parameters as $k => $v) { |
| 605 | - if (substr($k, 0, 5) != "oauth") continue; |
|
| 607 | + if (substr($k, 0, 5) != "oauth") { |
|
| 608 | + continue; |
|
| 609 | + } |
|
| 606 | 610 | if (is_array($v)) { |
| 607 | 611 | throw new OAuthClientException('Arrays not supported in headers.'); |
| 608 | 612 | } |
@@ -841,8 +845,9 @@ discard block |
||
| 841 | 845 | * check that the timestamp is new enough |
| 842 | 846 | */ |
| 843 | 847 | private function check_timestamp($timestamp) { |
| 844 | - if( ! $timestamp ) |
|
| 845 | - throw new OAuthMissingParameterException('oauth_timestamp'); |
|
| 848 | + if( ! $timestamp ) { |
|
| 849 | + throw new OAuthMissingParameterException('oauth_timestamp'); |
|
| 850 | + } |
|
| 846 | 851 | |
| 847 | 852 | // Cast to integer. See issue #314. |
| 848 | 853 | $timestamp = $timestamp + 0; |
@@ -859,8 +864,9 @@ discard block |
||
| 859 | 864 | * check that the nonce is not repeated |
| 860 | 865 | */ |
| 861 | 866 | private function check_nonce($consumer, $token, $nonce, $timestamp) { |
| 862 | - if( ! $nonce ) |
|
| 863 | - throw new OAuthMissingParameterException('oauth_nonce'); |
|
| 867 | + if( ! $nonce ) { |
|
| 868 | + throw new OAuthMissingParameterException('oauth_nonce'); |
|
| 869 | + } |
|
| 864 | 870 | |
| 865 | 871 | // verify that the nonce is uniqueish |
| 866 | 872 | $found = $this->data_store->lookup_nonce( |
@@ -967,10 +973,12 @@ discard block |
||
| 967 | 973 | // otherwise we don't have apache and are just going to have to hope |
| 968 | 974 | // that $_SERVER actually contains what we need |
| 969 | 975 | $out = array(); |
| 970 | - if( isset($_SERVER['CONTENT_TYPE']) ) |
|
| 971 | - $out['Content-Type'] = $_SERVER['CONTENT_TYPE']; |
|
| 972 | - if( isset($_ENV['CONTENT_TYPE']) ) |
|
| 973 | - $out['Content-Type'] = $_ENV['CONTENT_TYPE']; |
|
| 976 | + if( isset($_SERVER['CONTENT_TYPE']) ) { |
|
| 977 | + $out['Content-Type'] = $_SERVER['CONTENT_TYPE']; |
|
| 978 | + } |
|
| 979 | + if( isset($_ENV['CONTENT_TYPE']) ) { |
|
| 980 | + $out['Content-Type'] = $_ENV['CONTENT_TYPE']; |
|
| 981 | + } |
|
| 974 | 982 | |
| 975 | 983 | foreach ($_SERVER as $key => $value) { |
| 976 | 984 | if (substr($key, 0, 5) == "HTTP_") { |
@@ -993,7 +1001,9 @@ discard block |
||
| 993 | 1001 | // parameters like this |
| 994 | 1002 | // array('a' => array('b','c'), 'd' => 'e') |
| 995 | 1003 | public static function parse_parameters( $input ) { |
| 996 | - if (!isset($input) || !$input) return array(); |
|
| 1004 | + if (!isset($input) || !$input) { |
|
| 1005 | + return array(); |
|
| 1006 | + } |
|
| 997 | 1007 | |
| 998 | 1008 | $pairs = explode('&', $input); |
| 999 | 1009 | |
@@ -1022,7 +1032,9 @@ discard block |
||
| 1022 | 1032 | } |
| 1023 | 1033 | |
| 1024 | 1034 | public static function build_http_query($params) { |
| 1025 | - if (!$params) return ''; |
|
| 1035 | + if (!$params) { |
|
| 1036 | + return ''; |
|
| 1037 | + } |
|
| 1026 | 1038 | |
| 1027 | 1039 | // Urlencode both keys and values |
| 1028 | 1040 | $keys = OAuthUtil::urlencode_rfc3986(array_keys($params)); |
@@ -13,8 +13,9 @@ discard block |
||
| 13 | 13 | from okapi_consumers |
| 14 | 14 | where `key` = '".Db::escape_string($consumer_key)."' |
| 15 | 15 | "); |
| 16 | - if (!$row) |
|
| 17 | - return null; |
|
| 16 | + if (!$row) { |
|
| 17 | + return null; |
|
| 18 | + } |
|
| 18 | 19 | return new OkapiConsumer($row['key'], $row['secret'], $row['name'], |
| 19 | 20 | $row['url'], $row['email'], $row['bflags']); |
| 20 | 21 | } |
@@ -29,8 +30,9 @@ discard block |
||
| 29 | 30 | and token_type = '".Db::escape_string($token_type)."' |
| 30 | 31 | and `key` = '".Db::escape_string($token)."' |
| 31 | 32 | "); |
| 32 | - if (!$row) |
|
| 33 | - return null; |
|
| 33 | + if (!$row) { |
|
| 34 | + return null; |
|
| 35 | + } |
|
| 34 | 36 | switch ($row['token_type']) |
| 35 | 37 | { |
| 36 | 38 | case 'request': |
@@ -71,8 +73,7 @@ discard block |
||
| 71 | 73 | ); |
| 72 | 74 | "); |
| 73 | 75 | return null; |
| 74 | - } |
|
| 75 | - catch (\Exception $e) |
|
| 76 | + } catch (\Exception $e) |
|
| 76 | 77 | { |
| 77 | 78 | # INSERT failed. This nonce was already used. |
| 78 | 79 | |
@@ -84,8 +85,7 @@ discard block |
||
| 84 | 85 | { |
| 85 | 86 | if ((preg_match("#^[a-z][a-z0-9_.-]*://#", $callback) > 0) || |
| 86 | 87 | $callback == "oob") |
| 87 | - { /* ok */ } |
|
| 88 | - else { throw new BadRequest("oauth_callback should begin with lower case <scheme>://, or should equal 'oob'."); } |
|
| 88 | + { /* ok */ } else { throw new BadRequest("oauth_callback should begin with lower case <scheme>://, or should equal 'oob'."); } |
|
| 89 | 89 | $token = new OkapiRequestToken(Okapi::generate_key(20), Okapi::generate_key(40), |
| 90 | 90 | $consumer->key, $callback, null, Okapi::generate_key(8, true)); |
| 91 | 91 | Db::execute(" |
@@ -111,12 +111,15 @@ discard block |
||
| 111 | 111 | |
| 112 | 112 | public function new_access_token($token, $consumer, $verifier = null) |
| 113 | 113 | { |
| 114 | - if ($token->consumer_key != $consumer->key) |
|
| 115 | - throw new BadRequest("Request Token given is not associated with the Consumer who signed the request."); |
|
| 116 | - if (!$token->authorized_by_user_id) |
|
| 117 | - throw new BadRequest("Request Token given has not been authorized."); |
|
| 118 | - if ($token->verifier != $verifier) |
|
| 119 | - throw new BadRequest("Invalid verifier."); |
|
| 114 | + if ($token->consumer_key != $consumer->key) { |
|
| 115 | + throw new BadRequest("Request Token given is not associated with the Consumer who signed the request."); |
|
| 116 | + } |
|
| 117 | + if (!$token->authorized_by_user_id) { |
|
| 118 | + throw new BadRequest("Request Token given has not been authorized."); |
|
| 119 | + } |
|
| 120 | + if ($token->verifier != $verifier) { |
|
| 121 | + throw new BadRequest("Invalid verifier."); |
|
| 122 | + } |
|
| 120 | 123 | |
| 121 | 124 | # Invalidate the Request Token. |
| 122 | 125 | |
@@ -144,8 +147,7 @@ discard block |
||
| 144 | 147 | |
| 145 | 148 | $access_token = new OkapiAccessToken($row['key'], $row['secret'], |
| 146 | 149 | $consumer->key, $token->authorized_by_user_id); |
| 147 | - } |
|
| 148 | - else |
|
| 150 | + } else |
|
| 149 | 151 | { |
| 150 | 152 | # Generate a new Access Token. |
| 151 | 153 | |
@@ -78,8 +78,9 @@ discard block |
||
| 78 | 78 | ); |
| 79 | 79 | $request->i_want_OkapiResponse = true; |
| 80 | 80 | $request->perceive_as_http_request = true; |
| 81 | - if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) |
|
| 82 | - $request->etag = $_SERVER['HTTP_IF_NONE_MATCH']; |
|
| 81 | + if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) { |
|
| 82 | + $request->etag = $_SERVER['HTTP_IF_NONE_MATCH']; |
|
| 83 | + } |
|
| 83 | 84 | $response = OkapiServiceRunner::call($service_name, $request); |
| 84 | 85 | $response->display(); |
| 85 | 86 | } |
@@ -118,8 +119,9 @@ discard block |
||
| 118 | 119 | */ |
| 119 | 120 | public static function schedule_geocache_check($cache_codes) |
| 120 | 121 | { |
| 121 | - if (!is_array($cache_codes)) |
|
| 122 | - $cache_codes = array($cache_codes); |
|
| 122 | + if (!is_array($cache_codes)) { |
|
| 123 | + $cache_codes = array($cache_codes); |
|
| 124 | + } |
|
| 123 | 125 | Db::execute(" |
| 124 | 126 | update caches |
| 125 | 127 | set okapi_syncbase = now() |
@@ -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,16 +32,18 @@ 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 | |
@@ -49,8 +52,9 @@ discard block |
||
| 49 | 52 | foreach ($langprefs as $lang) |
| 50 | 53 | { |
| 51 | 54 | $locale = self::get_locale_for_language($lang); |
| 52 | - if ($locale != null) |
|
| 53 | - return $locale; |
|
| 55 | + if ($locale != null) { |
|
| 56 | + return $locale; |
|
| 57 | + } |
|
| 54 | 58 | } |
| 55 | 59 | return self::$languages['en']['locale']; |
| 56 | 60 | } |
@@ -29,14 +29,15 @@ discard block |
||
| 29 | 29 | { |
| 30 | 30 | try |
| 31 | 31 | { |
| 32 | - foreach (Settings::get('ADMINS') as $email) |
|
| 33 | - if (!in_array($email, $emails)) |
|
| 32 | + foreach (Settings::get('ADMINS') as $email) { |
|
| 33 | + if (!in_array($email, $emails)) |
|
| 34 | 34 | $emails[] = $email; |
| 35 | - } |
|
| 36 | - catch (Exception $e) { /* pass */ } |
|
| 35 | + } |
|
| 36 | + } catch (Exception $e) { /* pass */ } |
|
| 37 | + } |
|
| 38 | + if (count($emails) == 0) { |
|
| 39 | + $emails[] = 'root@localhost'; |
|
| 37 | 40 | } |
| 38 | - if (count($emails) == 0) |
|
| 39 | - $emails[] = 'root@localhost'; |
|
| 40 | 41 | return $emails; |
| 41 | 42 | } |
| 42 | 43 | |
@@ -81,16 +82,16 @@ discard block |
||
| 81 | 82 | # of this exception. All of them result in HTTP 400 or HTTP 401 error |
| 82 | 83 | # code. See also: http://oauth.net/core/1.0a/#http_codes |
| 83 | 84 | |
| 84 | - if ($e instanceof OAuthServer400Exception) |
|
| 85 | - header("HTTP/1.0 400 Bad Request"); |
|
| 86 | - else |
|
| 87 | - header("HTTP/1.0 401 Unauthorized"); |
|
| 85 | + if ($e instanceof OAuthServer400Exception) { |
|
| 86 | + header("HTTP/1.0 400 Bad Request"); |
|
| 87 | + } else { |
|
| 88 | + header("HTTP/1.0 401 Unauthorized"); |
|
| 89 | + } |
|
| 88 | 90 | header("Access-Control-Allow-Origin: *"); |
| 89 | 91 | header("Content-Type: application/json; charset=utf-8"); |
| 90 | 92 | |
| 91 | 93 | print $e->getOkapiJSON(); |
| 92 | - } |
|
| 93 | - elseif ($e instanceof BadRequest) |
|
| 94 | + } elseif ($e instanceof BadRequest) |
|
| 94 | 95 | { |
| 95 | 96 | # Intentionally thrown from within the OKAPI method code. |
| 96 | 97 | # Consumer (aka external developer) had something wrong with his |
@@ -105,8 +106,7 @@ discard block |
||
| 105 | 106 | } |
| 106 | 107 | |
| 107 | 108 | print $e->getOkapiJSON(); |
| 108 | - } |
|
| 109 | - else # (ErrorException, MySQL exception etc.) |
|
| 109 | + } else # (ErrorException, MySQL exception etc.) |
|
| 110 | 110 | { |
| 111 | 111 | # This one is thrown on PHP notices and warnings - usually this |
| 112 | 112 | # indicates an error in OKAPI method. If thrown, then something |
@@ -137,8 +137,7 @@ discard block |
||
| 137 | 137 | { |
| 138 | 138 | # Sending emails was blocked on admin's demand. |
| 139 | 139 | # This is possible only on development environment. |
| 140 | - } |
|
| 141 | - else |
|
| 140 | + } else |
|
| 142 | 141 | { |
| 143 | 142 | $subject = "OKAPI Method Error - ".substr( |
| 144 | 143 | $_SERVER['REQUEST_URI'], 0, strpos( |
@@ -153,16 +152,16 @@ discard block |
||
| 153 | 152 | try |
| 154 | 153 | { |
| 155 | 154 | Okapi::mail_admins($subject, $message); |
| 156 | - } |
|
| 157 | - catch (Exception $e) |
|
| 155 | + } catch (Exception $e) |
|
| 158 | 156 | { |
| 159 | 157 | # Unable to use full-featured mail_admins version. We'll use a backup. |
| 160 | 158 | # We need to make sure we're not spamming. |
| 161 | 159 | |
| 162 | 160 | $lock_file = "/tmp/okapi-fatal-error-mode"; |
| 163 | 161 | $last_email = false; |
| 164 | - if (file_exists($lock_file)) |
|
| 165 | - $last_email = filemtime($lock_file); |
|
| 162 | + if (file_exists($lock_file)) { |
|
| 163 | + $last_email = filemtime($lock_file); |
|
| 164 | + } |
|
| 166 | 165 | if ($last_email === false) { |
| 167 | 166 | # Assume this is the first email. |
| 168 | 167 | $last_email = 0; |
@@ -219,8 +218,7 @@ discard block |
||
| 219 | 218 | # the file and line. |
| 220 | 219 | |
| 221 | 220 | $exception_info .= "File: ".$e->getFile()."\nLine: ".$e->getLine()."\n\n"; |
| 222 | - } |
|
| 223 | - else |
|
| 221 | + } else |
|
| 224 | 222 | { |
| 225 | 223 | $exception_info .= "--- Stack trace ---\n". |
| 226 | 224 | self::removeSensitiveData($e->getTraceAsString())."\n\n"; |
@@ -254,7 +252,9 @@ discard block |
||
| 254 | 252 | /** Handle error encountered while executing OKAPI request. */ |
| 255 | 253 | public static function handle($severity, $message, $filename, $lineno) |
| 256 | 254 | { |
| 257 | - if ($severity == E_STRICT || $severity == E_DEPRECATED) return false; |
|
| 255 | + if ($severity == E_STRICT || $severity == E_DEPRECATED) { |
|
| 256 | + return false; |
|
| 257 | + } |
|
| 258 | 258 | if (($severity == E_NOTICE) && !self::$treat_notices_as_errors) { |
| 259 | 259 | return false; |
| 260 | 260 | } |
@@ -340,10 +340,11 @@ discard block |
||
| 340 | 340 | { |
| 341 | 341 | $this->paramName = $paramName; |
| 342 | 342 | $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); |
|
| 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); |
|
| 347 | + } |
|
| 347 | 348 | } |
| 348 | 349 | } |
| 349 | 350 | |
@@ -369,9 +370,9 @@ discard block |
||
| 369 | 370 | mysql_select_db(Settings::get('DB_NAME')); |
| 370 | 371 | mysql_query("set names '" . Settings::get('DB_CHARSET') . "'"); |
| 371 | 372 | self::$connected = true; |
| 373 | + } else { |
|
| 374 | + throw new Exception("Could not connect to MySQL: ".mysql_error()); |
|
| 372 | 375 | } |
| 373 | - else |
|
| 374 | - throw new Exception("Could not connect to MySQL: ".mysql_error()); |
|
| 375 | 376 | } |
| 376 | 377 | |
| 377 | 378 | /** Fetch [{row}], return {row}. */ |
@@ -402,12 +403,14 @@ discard block |
||
| 402 | 403 | while (true) |
| 403 | 404 | { |
| 404 | 405 | $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; |
|
| 406 | + if ($row === false) { |
|
| 407 | + break; |
|
| 408 | + } |
|
| 409 | + if ($keyField == null) { |
|
| 410 | + $arr[] = $row; |
|
| 411 | + } else { |
|
| 412 | + $arr[$row[$keyField]] = $row; |
|
| 413 | + } |
|
| 411 | 414 | } |
| 412 | 415 | Db::free_result($rs); |
| 413 | 416 | } |
@@ -420,8 +423,9 @@ discard block |
||
| 420 | 423 | while (true) |
| 421 | 424 | { |
| 422 | 425 | $row = Db::fetch_assoc($rs); |
| 423 | - if ($row === false) |
|
| 424 | - break; |
|
| 426 | + if ($row === false) { |
|
| 427 | + break; |
|
| 428 | + } |
|
| 425 | 429 | $groups[$row[$keyField]][] = $row; |
| 426 | 430 | } |
| 427 | 431 | Db::free_result($rs); |
@@ -432,10 +436,12 @@ discard block |
||
| 432 | 436 | public static function select_value($query) |
| 433 | 437 | { |
| 434 | 438 | $column = self::select_column($query); |
| 435 | - if ($column == null) |
|
| 436 | - return null; |
|
| 437 | - if (count($column) == 1) |
|
| 438 | - return $column[0]; |
|
| 439 | + if ($column == null) { |
|
| 440 | + return null; |
|
| 441 | + } |
|
| 442 | + if (count($column) == 1) { |
|
| 443 | + return $column[0]; |
|
| 444 | + } |
|
| 439 | 445 | throw new DbException("Invalid query. Db::select_value returned more than one row for:\n\n".$query."\n"); |
| 440 | 446 | } |
| 441 | 447 | |
@@ -447,8 +453,9 @@ discard block |
||
| 447 | 453 | while (true) |
| 448 | 454 | { |
| 449 | 455 | $values = Db::fetch_row($rs); |
| 450 | - if ($values === false) |
|
| 451 | - break; |
|
| 456 | + if ($values === false) { |
|
| 457 | + break; |
|
| 458 | + } |
|
| 452 | 459 | array_push($column, $values[0]); |
| 453 | 460 | } |
| 454 | 461 | Db::free_result($rs); |
@@ -488,15 +495,17 @@ discard block |
||
| 488 | 495 | public static function execute($query) |
| 489 | 496 | { |
| 490 | 497 | $rs = self::query($query); |
| 491 | - if ($rs !== true) |
|
| 492 | - throw new DbException("Db::execute returned a result set for your query. ". |
|
| 498 | + if ($rs !== true) { |
|
| 499 | + throw new DbException("Db::execute returned a result set for your query. ". |
|
| 493 | 500 | "You should use Db::select_* or Db::query for SELECT queries!"); |
| 501 | + } |
|
| 494 | 502 | } |
| 495 | 503 | |
| 496 | 504 | public static function query($query) |
| 497 | 505 | { |
| 498 | - if (!self::$connected) |
|
| 499 | - self::connect(); |
|
| 506 | + if (!self::$connected) { |
|
| 507 | + self::connect(); |
|
| 508 | + } |
|
| 500 | 509 | $rs = mysql_query($query); |
| 501 | 510 | if (!$rs) |
| 502 | 511 | { |
@@ -555,8 +564,9 @@ discard block |
||
| 555 | 564 | |
| 556 | 565 | public static function field_exists($table, $field) |
| 557 | 566 | { |
| 558 | - if (!preg_match("/[a-z0-9_]+/", $table.$field)) |
|
| 559 | - return false; |
|
| 567 | + if (!preg_match("/[a-z0-9_]+/", $table.$field)) { |
|
| 568 | + return false; |
|
| 569 | + } |
|
| 560 | 570 | try { |
| 561 | 571 | $spec = self::select_all("desc ".$table.";"); |
| 562 | 572 | } catch (Exception $e) { |
@@ -564,8 +574,9 @@ discard block |
||
| 564 | 574 | return false; |
| 565 | 575 | } |
| 566 | 576 | foreach ($spec as &$row_ref) { |
| 567 | - if (strtoupper($row_ref['Field']) == strtoupper($field)) |
|
| 568 | - return true; |
|
| 577 | + if (strtoupper($row_ref['Field']) == strtoupper($field)) { |
|
| 578 | + return true; |
|
| 579 | + } |
|
| 569 | 580 | } |
| 570 | 581 | return false; |
| 571 | 582 | } |
@@ -765,10 +776,11 @@ discard block |
||
| 765 | 776 | } catch (OAuthMissingParameterException $e) { |
| 766 | 777 | # Note, that exception will be different if token is supplied |
| 767 | 778 | # 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; |
|
| 779 | + if (($e->getParamName() == 'oauth_token') && (!$token_required)) { |
|
| 780 | + $token = null; |
|
| 781 | + } else { |
|
| 782 | + throw $e; |
|
| 783 | + } |
|
| 772 | 784 | } |
| 773 | 785 | $this->check_signature($request, $consumer, $token); |
| 774 | 786 | return array($consumer, $token); |
@@ -798,8 +810,9 @@ discard block |
||
| 798 | 810 | |
| 799 | 811 | public function get_length() |
| 800 | 812 | { |
| 801 | - if (is_resource($this->body)) |
|
| 802 | - return $this->stream_length; |
|
| 813 | + if (is_resource($this->body)) { |
|
| 814 | + return $this->stream_length; |
|
| 815 | + } |
|
| 803 | 816 | return strlen($this->body); |
| 804 | 817 | } |
| 805 | 818 | |
@@ -808,11 +821,12 @@ discard block |
||
| 808 | 821 | { |
| 809 | 822 | if (is_resource($this->body)) |
| 810 | 823 | { |
| 811 | - while (!feof($this->body)) |
|
| 812 | - print fread($this->body, 1024*1024); |
|
| 824 | + while (!feof($this->body)) { |
|
| 825 | + print fread($this->body, 1024*1024); |
|
| 826 | + } |
|
| 827 | + } else { |
|
| 828 | + print $this->body; |
|
| 813 | 829 | } |
| 814 | - else |
|
| 815 | - print $this->body; |
|
| 816 | 830 | } |
| 817 | 831 | |
| 818 | 832 | /** |
@@ -826,9 +840,9 @@ discard block |
||
| 826 | 840 | ob_start(); |
| 827 | 841 | fpassthru($this->body); |
| 828 | 842 | return ob_get_clean(); |
| 843 | + } else { |
|
| 844 | + return $this->body; |
|
| 829 | 845 | } |
| 830 | - else |
|
| 831 | - return $this->body; |
|
| 832 | 846 | } |
| 833 | 847 | |
| 834 | 848 | /** |
@@ -840,17 +854,21 @@ discard block |
||
| 840 | 854 | header("Access-Control-Allow-Origin: *"); |
| 841 | 855 | header("Content-Type: ".$this->content_type); |
| 842 | 856 | 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"); |
|
| 857 | + if ($this->connection_close) { |
|
| 858 | + header("Connection: close"); |
|
| 859 | + } |
|
| 860 | + if ($this->content_disposition) { |
|
| 861 | + header("Content-Disposition: ".$this->content_disposition); |
|
| 862 | + } |
|
| 863 | + if ($this->etag) { |
|
| 864 | + header("ETag: $this->etag"); |
|
| 865 | + } |
|
| 849 | 866 | |
| 850 | 867 | # Make sure that gzip is supported by the client. |
| 851 | 868 | $use_gzip = $this->allow_gzip; |
| 852 | - if (empty($_SERVER["HTTP_ACCEPT_ENCODING"]) || (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") === false)) |
|
| 853 | - $use_gzip = false; |
|
| 869 | + if (empty($_SERVER["HTTP_ACCEPT_ENCODING"]) || (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") === false)) { |
|
| 870 | + $use_gzip = false; |
|
| 871 | + } |
|
| 854 | 872 | |
| 855 | 873 | # We will gzip the data ourselves, while disabling gziping by Apache. This way, we can |
| 856 | 874 | # set the Content-Length correctly which is handy in some scenarios. |
@@ -863,8 +881,7 @@ discard block |
||
| 863 | 881 | $gzipped = gzencode($this->body, 5); |
| 864 | 882 | header("Content-Length: ".strlen($gzipped)); |
| 865 | 883 | print $gzipped; |
| 866 | - } |
|
| 867 | - else |
|
| 884 | + } else |
|
| 868 | 885 | { |
| 869 | 886 | # We don't want Apache to gzip this response. Tell it so. |
| 870 | 887 | |
@@ -873,8 +890,9 @@ discard block |
||
| 873 | 890 | } |
| 874 | 891 | |
| 875 | 892 | $length = $this->get_length(); |
| 876 | - if ($length) |
|
| 877 | - header("Content-Length: ".$length); |
|
| 893 | + if ($length) { |
|
| 894 | + header("Content-Length: ".$length); |
|
| 895 | + } |
|
| 878 | 896 | $this->print_body(); |
| 879 | 897 | } |
| 880 | 898 | } |
@@ -954,8 +972,7 @@ discard block |
||
| 954 | 972 | # Using semaphores is forbidden on this server by its admin. |
| 955 | 973 | # This is possible only on development environment. |
| 956 | 974 | $this->lock = null; |
| 957 | - } |
|
| 958 | - else |
|
| 975 | + } else |
|
| 959 | 976 | { |
| 960 | 977 | $this->lockfile = Okapi::get_var_dir()."/okapi-lock-".$name; |
| 961 | 978 | $this->lock = fopen($this->lockfile, "wb"); |
@@ -964,22 +981,26 @@ discard block |
||
| 964 | 981 | |
| 965 | 982 | public function acquire() |
| 966 | 983 | { |
| 967 | - if ($this->lock !== null) |
|
| 968 | - flock($this->lock, LOCK_EX); |
|
| 984 | + if ($this->lock !== null) { |
|
| 985 | + flock($this->lock, LOCK_EX); |
|
| 986 | + } |
|
| 969 | 987 | } |
| 970 | 988 | |
| 971 | 989 | public function try_acquire() |
| 972 | 990 | { |
| 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 |
|
| 991 | + if ($this->lock !== null) { |
|
| 992 | + return flock($this->lock, LOCK_EX | LOCK_NB); |
|
| 993 | + } else { |
|
| 994 | + return true; |
|
| 995 | + } |
|
| 996 | + # $lock can be null only when debugging |
|
| 977 | 997 | } |
| 978 | 998 | |
| 979 | 999 | public function release() |
| 980 | 1000 | { |
| 981 | - if ($this->lock !== null) |
|
| 982 | - flock($this->lock, LOCK_UN); |
|
| 1001 | + if ($this->lock !== null) { |
|
| 1002 | + flock($this->lock, LOCK_UN); |
|
| 1003 | + } |
|
| 983 | 1004 | } |
| 984 | 1005 | |
| 985 | 1006 | /** |
@@ -1040,11 +1061,13 @@ discard block |
||
| 1040 | 1061 | from okapi_vars |
| 1041 | 1062 | "); |
| 1042 | 1063 | self::$okapi_vars = array(); |
| 1043 | - while ($row = Db::fetch_assoc($rs)) |
|
| 1044 | - self::$okapi_vars[$row['var']] = $row['value']; |
|
| 1064 | + while ($row = Db::fetch_assoc($rs)) { |
|
| 1065 | + self::$okapi_vars[$row['var']] = $row['value']; |
|
| 1066 | + } |
|
| 1067 | + } |
|
| 1068 | + if (isset(self::$okapi_vars[$varname])) { |
|
| 1069 | + return self::$okapi_vars[$varname]; |
|
| 1045 | 1070 | } |
| 1046 | - if (isset(self::$okapi_vars[$varname])) |
|
| 1047 | - return self::$okapi_vars[$varname]; |
|
| 1048 | 1071 | return $default; |
| 1049 | 1072 | } |
| 1050 | 1073 | |
@@ -1100,8 +1123,9 @@ discard block |
||
| 1100 | 1123 | |
| 1101 | 1124 | return; |
| 1102 | 1125 | } |
| 1103 | - if ($counter === null) |
|
| 1104 | - $counter = 0; |
|
| 1126 | + if ($counter === null) { |
|
| 1127 | + $counter = 0; |
|
| 1128 | + } |
|
| 1105 | 1129 | $counter++; |
| 1106 | 1130 | try { |
| 1107 | 1131 | Cache::set($cache_key, $counter, 3600); |
@@ -1114,8 +1138,7 @@ discard block |
||
| 1114 | 1138 | # We're not spamming yet. |
| 1115 | 1139 | |
| 1116 | 1140 | self::mail_from_okapi(get_admin_emails(), $subject, $message); |
| 1117 | - } |
|
| 1118 | - else |
|
| 1141 | + } else |
|
| 1119 | 1142 | { |
| 1120 | 1143 | # We are spamming. Prevent sending more emails. |
| 1121 | 1144 | |
@@ -1148,8 +1171,9 @@ discard block |
||
| 1148 | 1171 | # This is possible only on development environment. |
| 1149 | 1172 | return; |
| 1150 | 1173 | } |
| 1151 | - if (!is_array($email_addresses)) |
|
| 1152 | - $email_addresses = array($email_addresses); |
|
| 1174 | + if (!is_array($email_addresses)) { |
|
| 1175 | + $email_addresses = array($email_addresses); |
|
| 1176 | + } |
|
| 1153 | 1177 | $sender_email = class_exists("okapi\\Settings") ? Settings::get('FROM_FIELD') : 'root@localhost'; |
| 1154 | 1178 | mail(implode(", ", $email_addresses), $subject, $message, |
| 1155 | 1179 | "Content-Type: text/plain; charset=utf-8\n". |
@@ -1162,16 +1186,18 @@ discard block |
||
| 1162 | 1186 | public static function get_var_dir() |
| 1163 | 1187 | { |
| 1164 | 1188 | $dir = Settings::get('VAR_DIR'); |
| 1165 | - if ($dir != null) |
|
| 1166 | - return rtrim($dir, "/"); |
|
| 1189 | + if ($dir != null) { |
|
| 1190 | + return rtrim($dir, "/"); |
|
| 1191 | + } |
|
| 1167 | 1192 | throw new Exception("You need to set a valid VAR_DIR."); |
| 1168 | 1193 | } |
| 1169 | 1194 | |
| 1170 | 1195 | /** Returns something like "Opencaching.PL" or "Opencaching.DE". */ |
| 1171 | 1196 | public static function get_normalized_site_name($site_url = null) |
| 1172 | 1197 | { |
| 1173 | - if ($site_url == null) |
|
| 1174 | - $site_url = Settings::get('SITE_URL'); |
|
| 1198 | + if ($site_url == null) { |
|
| 1199 | + $site_url = Settings::get('SITE_URL'); |
|
| 1200 | + } |
|
| 1175 | 1201 | $matches = null; |
| 1176 | 1202 | if (preg_match("#^https?://(www.)?opencaching.([a-z.]+)/$#", $site_url, $matches)) { |
| 1177 | 1203 | return "Opencaching.".strtoupper($matches[2]); |
@@ -1320,11 +1346,13 @@ discard block |
||
| 1320 | 1346 | */ |
| 1321 | 1347 | public static function pick_best_language($langdict, $langprefs) |
| 1322 | 1348 | { |
| 1323 | - foreach ($langprefs as $pref) |
|
| 1324 | - if (isset($langdict[$pref])) |
|
| 1349 | + foreach ($langprefs as $pref) { |
|
| 1350 | + if (isset($langdict[$pref])) |
|
| 1325 | 1351 | return $langdict[$pref]; |
| 1326 | - foreach ($langdict as &$text_ref) |
|
| 1327 | - return $text_ref; |
|
| 1352 | + } |
|
| 1353 | + foreach ($langdict as &$text_ref) { |
|
| 1354 | + return $text_ref; |
|
| 1355 | + } |
|
| 1328 | 1356 | return ""; |
| 1329 | 1357 | } |
| 1330 | 1358 | |
@@ -1405,8 +1433,9 @@ discard block |
||
| 1405 | 1433 | { |
| 1406 | 1434 | # Put the langprefs on the stack. |
| 1407 | 1435 | |
| 1408 | - if ($langprefs == null) |
|
| 1409 | - $langprefs = array(Settings::get('SITELANG')); |
|
| 1436 | + if ($langprefs == null) { |
|
| 1437 | + $langprefs = array(Settings::get('SITELANG')); |
|
| 1438 | + } |
|
| 1410 | 1439 | self::$gettext_langprefs_stack[] = $langprefs; |
| 1411 | 1440 | |
| 1412 | 1441 | if (count(self::$gettext_langprefs_stack) == 1) |
@@ -1438,8 +1467,7 @@ discard block |
||
| 1438 | 1467 | { |
| 1439 | 1468 | $langprefs = self::$gettext_langprefs_stack[$size - 1]; |
| 1440 | 1469 | self::gettext_set_lang($langprefs); |
| 1441 | - } |
|
| 1442 | - else |
|
| 1470 | + } else |
|
| 1443 | 1471 | { |
| 1444 | 1472 | # The stack is empty. This means we're going out of OKAPI code and |
| 1445 | 1473 | # we want the original textdomain reestablished. |
@@ -1455,20 +1483,25 @@ discard block |
||
| 1455 | 1483 | public static function init_internals($allow_cronjobs = true) |
| 1456 | 1484 | { |
| 1457 | 1485 | static $init_made = false; |
| 1458 | - if ($init_made) |
|
| 1459 | - return; |
|
| 1486 | + if ($init_made) { |
|
| 1487 | + return; |
|
| 1488 | + } |
|
| 1460 | 1489 | ini_set('memory_limit', '256M'); |
| 1461 | 1490 | # The memory limit is - among other - crucial for the maximum size |
| 1462 | 1491 | # of processable images; see services/logs/images/add.php: max_pixels() |
| 1463 | 1492 | 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(); |
|
| 1493 | + if (Settings::get('TIMEZONE') !== null) { |
|
| 1494 | + date_default_timezone_set(Settings::get('TIMEZONE')); |
|
| 1495 | + } |
|
| 1496 | + if (!self::$data_store) { |
|
| 1497 | + self::$data_store = new OkapiDataStore(); |
|
| 1498 | + } |
|
| 1499 | + if (!self::$server) { |
|
| 1500 | + self::$server = new OkapiOAuthServer(self::$data_store); |
|
| 1501 | + } |
|
| 1502 | + if ($allow_cronjobs) { |
|
| 1503 | + self::execute_prerequest_cronjobs(); |
|
| 1504 | + } |
|
| 1472 | 1505 | $init_made = true; |
| 1473 | 1506 | } |
| 1474 | 1507 | |
@@ -1479,10 +1512,11 @@ discard block |
||
| 1479 | 1512 | */ |
| 1480 | 1513 | public static function generate_key($length, $user_friendly = false) |
| 1481 | 1514 | { |
| 1482 | - if ($user_friendly) |
|
| 1483 | - $chars = "0123456789"; |
|
| 1484 | - else |
|
| 1485 | - $chars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; |
|
| 1515 | + if ($user_friendly) { |
|
| 1516 | + $chars = "0123456789"; |
|
| 1517 | + } else { |
|
| 1518 | + $chars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; |
|
| 1519 | + } |
|
| 1486 | 1520 | $max = strlen($chars); |
| 1487 | 1521 | $key = ""; |
| 1488 | 1522 | for ($i=0; $i<$length; $i++) |
@@ -1504,10 +1538,11 @@ discard block |
||
| 1504 | 1538 | $appname, $appurl, $email); |
| 1505 | 1539 | $sample_cache = OkapiServiceRunner::call("services/caches/search/all", |
| 1506 | 1540 | 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"; |
|
| 1541 | + if (count($sample_cache['results']) > 0) { |
|
| 1542 | + $sample_cache_code = $sample_cache['results'][0]; |
|
| 1543 | + } else { |
|
| 1544 | + $sample_cache_code = "CACHECODE"; |
|
| 1545 | + } |
|
| 1511 | 1546 | |
| 1512 | 1547 | # Message for the Consumer. |
| 1513 | 1548 | ob_start(); |
@@ -1558,7 +1593,9 @@ discard block |
||
| 1558 | 1593 | # to floating point precision limits. See issue #351 for details. |
| 1559 | 1594 | # |
| 1560 | 1595 | $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; |
|
| 1596 | + if ($d < 0) { |
|
| 1597 | + $d = 0; |
|
| 1598 | + } |
|
| 1562 | 1599 | return $d; |
| 1563 | 1600 | } |
| 1564 | 1601 | |
@@ -1581,10 +1618,15 @@ discard block |
||
| 1581 | 1618 | /** Return bearing (float 0..360) from geopoint 1 to 2. */ |
| 1582 | 1619 | public static function get_bearing($lat1, $lon1, $lat2, $lon2) |
| 1583 | 1620 | { |
| 1584 | - if ($lat1 == $lat2 && $lon1 == $lon2) |
|
| 1585 | - return null; |
|
| 1586 | - if ($lat1 == $lat2) $lat1 += 0.0000166; |
|
| 1587 | - if ($lon1 == $lon2) $lon1 += 0.0000166; |
|
| 1621 | + if ($lat1 == $lat2 && $lon1 == $lon2) { |
|
| 1622 | + return null; |
|
| 1623 | + } |
|
| 1624 | + if ($lat1 == $lat2) { |
|
| 1625 | + $lat1 += 0.0000166; |
|
| 1626 | + } |
|
| 1627 | + if ($lon1 == $lon2) { |
|
| 1628 | + $lon1 += 0.0000166; |
|
| 1629 | + } |
|
| 1588 | 1630 | |
| 1589 | 1631 | $rad_lat1 = $lat1 / 180.0 * 3.14159; |
| 1590 | 1632 | $rad_lon1 = $lon1 / 180.0 * 3.14159; |
@@ -1595,7 +1637,9 @@ discard block |
||
| 1595 | 1637 | $bearing = atan2(sin($delta_lon) * cos($rad_lat2), |
| 1596 | 1638 | cos($rad_lat1) * sin($rad_lat2) - sin($rad_lat1) * cos($rad_lat2) * cos($delta_lon)); |
| 1597 | 1639 | $bearing = 180.0 * $bearing / 3.14159; |
| 1598 | - if ( $bearing < 0.0 ) $bearing = $bearing + 360.0; |
|
| 1640 | + if ( $bearing < 0.0 ) { |
|
| 1641 | + $bearing = $bearing + 360.0; |
|
| 1642 | + } |
|
| 1599 | 1643 | |
| 1600 | 1644 | return $bearing; |
| 1601 | 1645 | } |
@@ -1604,7 +1648,9 @@ discard block |
||
| 1604 | 1648 | public static function bearing_as_two_letters($b) |
| 1605 | 1649 | { |
| 1606 | 1650 | static $names = array('N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'); |
| 1607 | - if ($b === null) return 'n/a'; |
|
| 1651 | + if ($b === null) { |
|
| 1652 | + return 'n/a'; |
|
| 1653 | + } |
|
| 1608 | 1654 | return $names[round(($b / 360.0) * 8.0) % 8]; |
| 1609 | 1655 | } |
| 1610 | 1656 | |
@@ -1613,7 +1659,9 @@ discard block |
||
| 1613 | 1659 | { |
| 1614 | 1660 | static $names = array('N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', |
| 1615 | 1661 | 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW'); |
| 1616 | - if ($b === null) return 'n/a'; |
|
| 1662 | + if ($b === null) { |
|
| 1663 | + return 'n/a'; |
|
| 1664 | + } |
|
| 1617 | 1665 | return $names[round(($b / 360.0) * 16.0) % 16]; |
| 1618 | 1666 | } |
| 1619 | 1667 | |
@@ -1641,46 +1689,48 @@ discard block |
||
| 1641 | 1689 | return $object; |
| 1642 | 1690 | } |
| 1643 | 1691 | $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'"); |
|
| 1692 | + if ($format == null) { |
|
| 1693 | + $format = 'json'; |
|
| 1694 | + } |
|
| 1695 | + if (!in_array($format, array('json', 'jsonp', 'xmlmap', 'xmlmap2'))) { |
|
| 1696 | + throw new InvalidParam('format', "'$format'"); |
|
| 1697 | + } |
|
| 1647 | 1698 | $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."); |
|
| 1699 | + if ($callback && $format != 'jsonp') { |
|
| 1700 | + throw new BadRequest("The 'callback' parameter is reserved to be used with the JSONP output format."); |
|
| 1701 | + } |
|
| 1650 | 1702 | if ($format == 'json') |
| 1651 | 1703 | { |
| 1652 | 1704 | $response = new OkapiHttpResponse(); |
| 1653 | 1705 | $response->content_type = "application/json; charset=utf-8"; |
| 1654 | 1706 | $response->body = json_encode($object); |
| 1655 | 1707 | return $response; |
| 1656 | - } |
|
| 1657 | - elseif ($format == 'jsonp') |
|
| 1708 | + } elseif ($format == 'jsonp') |
|
| 1658 | 1709 | { |
| 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_]*\$/)."); |
|
| 1710 | + if (!$callback) { |
|
| 1711 | + throw new BadRequest("'callback' parameter is required for JSONP calls"); |
|
| 1712 | + } |
|
| 1713 | + if (!preg_match("/^[a-zA-Z_][a-zA-Z0-9_]*$/", $callback)) { |
|
| 1714 | + throw new InvalidParam('callback', "'$callback' doesn't seem to be a valid JavaScript function name (should match /^[a-zA-Z_][a-zA-Z0-9_]*\$/)."); |
|
| 1715 | + } |
|
| 1663 | 1716 | $response = new OkapiHttpResponse(); |
| 1664 | 1717 | $response->content_type = "application/javascript; charset=utf-8"; |
| 1665 | 1718 | $response->body = $callback."(".json_encode($object).");"; |
| 1666 | 1719 | return $response; |
| 1667 | - } |
|
| 1668 | - elseif ($format == 'xmlmap') |
|
| 1720 | + } elseif ($format == 'xmlmap') |
|
| 1669 | 1721 | { |
| 1670 | 1722 | # Deprecated (see issue 128). Keeping this for backward-compatibility. |
| 1671 | 1723 | $response = new OkapiHttpResponse(); |
| 1672 | 1724 | $response->content_type = "text/xml; charset=utf-8"; |
| 1673 | 1725 | $response->body = self::xmlmap_dumps($object); |
| 1674 | 1726 | return $response; |
| 1675 | - } |
|
| 1676 | - elseif ($format == 'xmlmap2') |
|
| 1727 | + } elseif ($format == 'xmlmap2') |
|
| 1677 | 1728 | { |
| 1678 | 1729 | $response = new OkapiHttpResponse(); |
| 1679 | 1730 | $response->content_type = "text/xml; charset=utf-8"; |
| 1680 | 1731 | $response->body = self::xmlmap2_dumps($object); |
| 1681 | 1732 | return $response; |
| 1682 | - } |
|
| 1683 | - else |
|
| 1733 | + } else |
|
| 1684 | 1734 | { |
| 1685 | 1735 | # Should not happen (as we do a proper check above). |
| 1686 | 1736 | throw new Exception(); |
@@ -1694,24 +1744,19 @@ discard block |
||
| 1694 | 1744 | $chunks[] = "<string>"; |
| 1695 | 1745 | $chunks[] = self::xmlescape($obj); |
| 1696 | 1746 | $chunks[] = "</string>"; |
| 1697 | - } |
|
| 1698 | - elseif (is_int($obj)) |
|
| 1747 | + } elseif (is_int($obj)) |
|
| 1699 | 1748 | { |
| 1700 | 1749 | $chunks[] = "<int>$obj</int>"; |
| 1701 | - } |
|
| 1702 | - elseif (is_float($obj)) |
|
| 1750 | + } elseif (is_float($obj)) |
|
| 1703 | 1751 | { |
| 1704 | 1752 | $chunks[] = "<float>$obj</float>"; |
| 1705 | - } |
|
| 1706 | - elseif (is_bool($obj)) |
|
| 1753 | + } elseif (is_bool($obj)) |
|
| 1707 | 1754 | { |
| 1708 | 1755 | $chunks[] = $obj ? "<bool>true</bool>" : "<bool>false</bool>"; |
| 1709 | - } |
|
| 1710 | - elseif (is_null($obj)) |
|
| 1756 | + } elseif (is_null($obj)) |
|
| 1711 | 1757 | { |
| 1712 | 1758 | $chunks[] = "<null/>"; |
| 1713 | - } |
|
| 1714 | - elseif (is_array($obj)) |
|
| 1759 | + } elseif (is_array($obj)) |
|
| 1715 | 1760 | { |
| 1716 | 1761 | # Have to check if this is associative or not! Shit. I hate PHP. |
| 1717 | 1762 | if (array_keys($obj) === range(0, count($obj) - 1)) |
@@ -1725,8 +1770,7 @@ discard block |
||
| 1725 | 1770 | $chunks[] = "</item>"; |
| 1726 | 1771 | } |
| 1727 | 1772 | $chunks[] = "</list>"; |
| 1728 | - } |
|
| 1729 | - else |
|
| 1773 | + } else |
|
| 1730 | 1774 | { |
| 1731 | 1775 | # Assoc. |
| 1732 | 1776 | $chunks[] = "<dict>"; |
@@ -1738,8 +1782,7 @@ discard block |
||
| 1738 | 1782 | } |
| 1739 | 1783 | $chunks[] = "</dict>"; |
| 1740 | 1784 | } |
| 1741 | - } |
|
| 1742 | - else |
|
| 1785 | + } else |
|
| 1743 | 1786 | { |
| 1744 | 1787 | # That's a bug. |
| 1745 | 1788 | throw new Exception("Cannot encode as xmlmap: " + print_r($obj, true)); |
@@ -1754,24 +1797,19 @@ discard block |
||
| 1754 | 1797 | $chunks[] = "<string$attrs>"; |
| 1755 | 1798 | $chunks[] = self::xmlescape($obj); |
| 1756 | 1799 | $chunks[] = "</string>"; |
| 1757 | - } |
|
| 1758 | - elseif (is_int($obj)) |
|
| 1800 | + } elseif (is_int($obj)) |
|
| 1759 | 1801 | { |
| 1760 | 1802 | $chunks[] = "<number$attrs>$obj</number>"; |
| 1761 | - } |
|
| 1762 | - elseif (is_float($obj)) |
|
| 1803 | + } elseif (is_float($obj)) |
|
| 1763 | 1804 | { |
| 1764 | 1805 | $chunks[] = "<number$attrs>$obj</number>"; |
| 1765 | - } |
|
| 1766 | - elseif (is_bool($obj)) |
|
| 1806 | + } elseif (is_bool($obj)) |
|
| 1767 | 1807 | { |
| 1768 | 1808 | $chunks[] = $obj ? "<boolean$attrs>true</boolean>" : "<boolean$attrs>false</boolean>"; |
| 1769 | - } |
|
| 1770 | - elseif (is_null($obj)) |
|
| 1809 | + } elseif (is_null($obj)) |
|
| 1771 | 1810 | { |
| 1772 | 1811 | $chunks[] = "<null$attrs/>"; |
| 1773 | - } |
|
| 1774 | - elseif (is_array($obj) || ($obj instanceof ArrayObject)) |
|
| 1812 | + } elseif (is_array($obj) || ($obj instanceof ArrayObject)) |
|
| 1775 | 1813 | { |
| 1776 | 1814 | # Have to check if this is associative or not! Shit. I hate PHP. |
| 1777 | 1815 | if (is_array($obj) && (array_keys($obj) === range(0, count($obj) - 1))) |
@@ -1783,8 +1821,7 @@ discard block |
||
| 1783 | 1821 | self::_xmlmap2_add($chunks, $item_ref, null); |
| 1784 | 1822 | } |
| 1785 | 1823 | $chunks[] = "</array>"; |
| 1786 | - } |
|
| 1787 | - else |
|
| 1824 | + } else |
|
| 1788 | 1825 | { |
| 1789 | 1826 | # Assoc. |
| 1790 | 1827 | $chunks[] = "<object$attrs>"; |
@@ -1794,8 +1831,7 @@ discard block |
||
| 1794 | 1831 | } |
| 1795 | 1832 | $chunks[] = "</object>"; |
| 1796 | 1833 | } |
| 1797 | - } |
|
| 1798 | - else |
|
| 1834 | + } else |
|
| 1799 | 1835 | { |
| 1800 | 1836 | # That's a bug. |
| 1801 | 1837 | throw new Exception("Cannot encode as xmlmap2: " . print_r($obj, true)); |
@@ -1851,8 +1887,9 @@ discard block |
||
| 1851 | 1887 | public static function cache_type_name2id($name) |
| 1852 | 1888 | { |
| 1853 | 1889 | $ref = &self::$cache_types[Settings::get('OC_BRANCH')]; |
| 1854 | - if (isset($ref[$name])) |
|
| 1855 | - return $ref[$name]; |
|
| 1890 | + if (isset($ref[$name])) { |
|
| 1891 | + return $ref[$name]; |
|
| 1892 | + } |
|
| 1856 | 1893 | throw new Exception("Method cache_type_name2id called with unsupported cache ". |
| 1857 | 1894 | "type name '$name'. You should not allow users to submit caches ". |
| 1858 | 1895 | "of non-primary type."); |
@@ -1865,11 +1902,13 @@ discard block |
||
| 1865 | 1902 | if ($reversed == null) |
| 1866 | 1903 | { |
| 1867 | 1904 | $reversed = array(); |
| 1868 | - foreach (self::$cache_types[Settings::get('OC_BRANCH')] as $key => $value) |
|
| 1869 | - $reversed[$value] = $key; |
|
| 1905 | + foreach (self::$cache_types[Settings::get('OC_BRANCH')] as $key => $value) { |
|
| 1906 | + $reversed[$value] = $key; |
|
| 1907 | + } |
|
| 1908 | + } |
|
| 1909 | + if (isset($reversed[$id])) { |
|
| 1910 | + return $reversed[$id]; |
|
| 1870 | 1911 | } |
| 1871 | - if (isset($reversed[$id])) |
|
| 1872 | - return $reversed[$id]; |
|
| 1873 | 1912 | return "Other"; |
| 1874 | 1913 | } |
| 1875 | 1914 | |
@@ -1880,8 +1919,9 @@ discard block |
||
| 1880 | 1919 | /** E.g. 'Available' => 1. For unknown names throws an Exception. */ |
| 1881 | 1920 | public static function cache_status_name2id($name) |
| 1882 | 1921 | { |
| 1883 | - if (isset(self::$cache_statuses[$name])) |
|
| 1884 | - return self::$cache_statuses[$name]; |
|
| 1922 | + if (isset(self::$cache_statuses[$name])) { |
|
| 1923 | + return self::$cache_statuses[$name]; |
|
| 1924 | + } |
|
| 1885 | 1925 | throw new Exception("Method cache_status_name2id called with invalid name '$name'."); |
| 1886 | 1926 | } |
| 1887 | 1927 | |
@@ -1892,11 +1932,13 @@ discard block |
||
| 1892 | 1932 | if ($reversed == null) |
| 1893 | 1933 | { |
| 1894 | 1934 | $reversed = array(); |
| 1895 | - foreach (self::$cache_statuses as $key => $value) |
|
| 1896 | - $reversed[$value] = $key; |
|
| 1935 | + foreach (self::$cache_statuses as $key => $value) { |
|
| 1936 | + $reversed[$value] = $key; |
|
| 1937 | + } |
|
| 1938 | + } |
|
| 1939 | + if (isset($reversed[$id])) { |
|
| 1940 | + return $reversed[$id]; |
|
| 1897 | 1941 | } |
| 1898 | - if (isset($reversed[$id])) |
|
| 1899 | - return $reversed[$id]; |
|
| 1900 | 1942 | return 'Archived'; |
| 1901 | 1943 | } |
| 1902 | 1944 | |
@@ -1914,8 +1956,9 @@ discard block |
||
| 1914 | 1956 | /** E.g. 'micro' => 2. For unknown names throw an Exception. */ |
| 1915 | 1957 | public static function cache_size2_to_sizeid($size2) |
| 1916 | 1958 | { |
| 1917 | - if (isset(self::$cache_sizes[$size2])) |
|
| 1918 | - return self::$cache_sizes[$size2]; |
|
| 1959 | + if (isset(self::$cache_sizes[$size2])) { |
|
| 1960 | + return self::$cache_sizes[$size2]; |
|
| 1961 | + } |
|
| 1919 | 1962 | throw new Exception("Method cache_size2_to_sizeid called with invalid size2 '$size2'."); |
| 1920 | 1963 | } |
| 1921 | 1964 | |
@@ -1926,11 +1969,13 @@ discard block |
||
| 1926 | 1969 | if ($reversed == null) |
| 1927 | 1970 | { |
| 1928 | 1971 | $reversed = array(); |
| 1929 | - foreach (self::$cache_sizes as $key => $value) |
|
| 1930 | - $reversed[$value] = $key; |
|
| 1972 | + foreach (self::$cache_sizes as $key => $value) { |
|
| 1973 | + $reversed[$value] = $key; |
|
| 1974 | + } |
|
| 1975 | + } |
|
| 1976 | + if (isset($reversed[$id])) { |
|
| 1977 | + return $reversed[$id]; |
|
| 1931 | 1978 | } |
| 1932 | - if (isset($reversed[$id])) |
|
| 1933 | - return $reversed[$id]; |
|
| 1934 | 1979 | return "other"; |
| 1935 | 1980 | } |
| 1936 | 1981 | |
@@ -1953,8 +1998,9 @@ discard block |
||
| 1953 | 1998 | */ |
| 1954 | 1999 | public static function cache_size2_to_oxsize($size2) |
| 1955 | 2000 | { |
| 1956 | - if (array_key_exists($size2, self::$cache_OX_sizes)) |
|
| 1957 | - return self::$cache_OX_sizes[$size2]; |
|
| 2001 | + if (array_key_exists($size2, self::$cache_OX_sizes)) { |
|
| 2002 | + return self::$cache_OX_sizes[$size2]; |
|
| 2003 | + } |
|
| 1958 | 2004 | throw new Exception("Method cache_size2_to_oxsize called with invalid size2 '$size2'."); |
| 1959 | 2005 | } |
| 1960 | 2006 | |
@@ -1963,12 +2009,24 @@ discard block |
||
| 1963 | 2009 | */ |
| 1964 | 2010 | public static function logtypename2id($name) |
| 1965 | 2011 | { |
| 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; |
|
| 2012 | + if ($name == 'Found it') { |
|
| 2013 | + return 1; |
|
| 2014 | + } |
|
| 2015 | + if ($name == "Didn't find it") { |
|
| 2016 | + return 2; |
|
| 2017 | + } |
|
| 2018 | + if ($name == 'Comment') { |
|
| 2019 | + return 3; |
|
| 2020 | + } |
|
| 2021 | + if ($name == 'Attended') { |
|
| 2022 | + return 7; |
|
| 2023 | + } |
|
| 2024 | + if ($name == 'Will attend') { |
|
| 2025 | + return 8; |
|
| 2026 | + } |
|
| 2027 | + if (($name == 'Needs maintenance') && (Settings::get('OC_BRANCH') == 'oc.pl')) { |
|
| 2028 | + return 5; |
|
| 2029 | + } |
|
| 1972 | 2030 | throw new Exception("logtype2id called with invalid log type argument: $name"); |
| 1973 | 2031 | } |
| 1974 | 2032 | |
@@ -1981,21 +2039,47 @@ discard block |
||
| 1981 | 2039 | # These names are officially documented and may never change! |
| 1982 | 2040 | |
| 1983 | 2041 | # 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"; |
|
| 2042 | + if ($id == 1) { |
|
| 2043 | + return "Found it"; |
|
| 2044 | + } |
|
| 2045 | + if ($id == 2) { |
|
| 2046 | + return "Didn't find it"; |
|
| 2047 | + } |
|
| 2048 | + if ($id == 3) { |
|
| 2049 | + return "Comment"; |
|
| 2050 | + } |
|
| 2051 | + if ($id == 7) { |
|
| 2052 | + return "Attended"; |
|
| 2053 | + } |
|
| 2054 | + if ($id == 8) { |
|
| 2055 | + return "Will attend"; |
|
| 2056 | + } |
|
| 1989 | 2057 | |
| 1990 | 2058 | # 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"; |
|
| 2059 | + if ($id == 4) { |
|
| 2060 | + return "Moved"; |
|
| 2061 | + } |
|
| 2062 | + if ($id == 5) { |
|
| 2063 | + return "Needs maintenance"; |
|
| 2064 | + } |
|
| 2065 | + if ($id == 6) { |
|
| 2066 | + return "Maintenance performed"; |
|
| 2067 | + } |
|
| 2068 | + if ($id == 9) { |
|
| 2069 | + return "Archived"; |
|
| 2070 | + } |
|
| 2071 | + if ($id == 10) { |
|
| 2072 | + return "Ready to search"; |
|
| 2073 | + } |
|
| 2074 | + if ($id == 11) { |
|
| 2075 | + return "Temporarily unavailable"; |
|
| 2076 | + } |
|
| 2077 | + if ($id == 12) { |
|
| 2078 | + return "OC Team comment"; |
|
| 2079 | + } |
|
| 2080 | + if ($id == 13 || $id == 14) { |
|
| 2081 | + return "Locked"; |
|
| 2082 | + } |
|
| 1999 | 2083 | |
| 2000 | 2084 | # Important: This set is not closed. Other types may be introduced |
| 2001 | 2085 | # in the future. This has to be documented in the public method |
@@ -2047,8 +2131,9 @@ discard block |
||
| 2047 | 2131 | function php_ini_get_bytes($variable) |
| 2048 | 2132 | { |
| 2049 | 2133 | $value = trim(ini_get($variable)); |
| 2050 | - if (!preg_match("/^[0-9]+[KM]?$/", $value)) |
|
| 2051 | - throw new Exception("Unexpected PHP setting: ".$variable. " = ".$value); |
|
| 2134 | + if (!preg_match("/^[0-9]+[KM]?$/", $value)) { |
|
| 2135 | + throw new Exception("Unexpected PHP setting: ".$variable. " = ".$value); |
|
| 2136 | + } |
|
| 2052 | 2137 | $value = str_replace('K', '*1024', $value); |
| 2053 | 2138 | $value = str_replace('M', '*1024*1024', $value); |
| 2054 | 2139 | $value = eval('return '.$value.';'); |
@@ -2116,8 +2201,9 @@ discard block |
||
| 2116 | 2201 | /** Do 'set' on many keys at once. */ |
| 2117 | 2202 | public static function set_many($dict, $timeout) |
| 2118 | 2203 | { |
| 2119 | - if (count($dict) == 0) |
|
| 2120 | - return; |
|
| 2204 | + if (count($dict) == 0) { |
|
| 2205 | + return; |
|
| 2206 | + } |
|
| 2121 | 2207 | if ($timeout == null) |
| 2122 | 2208 | { |
| 2123 | 2209 | # The current cache implementation is ALWAYS persistent, so we will |
@@ -2153,15 +2239,18 @@ discard block |
||
| 2153 | 2239 | and expires > now() |
| 2154 | 2240 | "); |
| 2155 | 2241 | list($blob, $score) = Db::fetch_row($rs); |
| 2156 | - if (!$blob) |
|
| 2157 | - return null; |
|
| 2158 | - if ($score != null) # Only non-null entries are scored. |
|
| 2242 | + if (!$blob) { |
|
| 2243 | + return null; |
|
| 2244 | + } |
|
| 2245 | + if ($score != null) { |
|
| 2246 | + # Only non-null entries are scored. |
|
| 2159 | 2247 | { |
| 2160 | 2248 | Db::execute(" |
| 2161 | 2249 | insert into okapi_cache_reads (`cache_key`) |
| 2162 | 2250 | values ('".Db::escape_string($key)."') |
| 2163 | 2251 | "); |
| 2164 | 2252 | } |
| 2253 | + } |
|
| 2165 | 2254 | return unserialize(gzinflate($blob)); |
| 2166 | 2255 | } |
| 2167 | 2256 | |
@@ -2181,8 +2270,7 @@ discard block |
||
| 2181 | 2270 | try |
| 2182 | 2271 | { |
| 2183 | 2272 | $dict[$row['key']] = unserialize(gzinflate($row['value'])); |
| 2184 | - } |
|
| 2185 | - catch (ErrorException $e) |
|
| 2273 | + } catch (ErrorException $e) |
|
| 2186 | 2274 | { |
| 2187 | 2275 | unset($dict[$row['key']]); |
| 2188 | 2276 | Okapi::mail_admins("Debug: Unserialize error", |
@@ -2192,10 +2280,11 @@ discard block |
||
| 2192 | 2280 | "Length of data, compressed: ".strlen($row['value'])); |
| 2193 | 2281 | } |
| 2194 | 2282 | } |
| 2195 | - if (count($dict) < count($keys)) |
|
| 2196 | - foreach ($keys as $key) |
|
| 2283 | + if (count($dict) < count($keys)) { |
|
| 2284 | + foreach ($keys as $key) |
|
| 2197 | 2285 | if (!isset($dict[$key])) |
| 2198 | 2286 | $dict[$key] = null; |
| 2287 | + } |
|
| 2199 | 2288 | return $dict; |
| 2200 | 2289 | } |
| 2201 | 2290 | |
@@ -2210,8 +2299,9 @@ discard block |
||
| 2210 | 2299 | /** Do 'delete' on many keys at once. */ |
| 2211 | 2300 | public static function delete_many($keys) |
| 2212 | 2301 | { |
| 2213 | - if (count($keys) == 0) |
|
| 2214 | - return; |
|
| 2302 | + if (count($keys) == 0) { |
|
| 2303 | + return; |
|
| 2304 | + } |
|
| 2215 | 2305 | Db::execute(" |
| 2216 | 2306 | delete from okapi_cache |
| 2217 | 2307 | where `key` in ('".implode("','", array_map('\okapi\Db::escape_string', $keys))."') |
@@ -2229,8 +2319,9 @@ discard block |
||
| 2229 | 2319 | public static function get_file_path($key) |
| 2230 | 2320 | { |
| 2231 | 2321 | $filename = Okapi::get_var_dir()."/okapi_filecache_".md5($key); |
| 2232 | - if (!file_exists($filename)) |
|
| 2233 | - return null; |
|
| 2322 | + if (!file_exists($filename)) { |
|
| 2323 | + return null; |
|
| 2324 | + } |
|
| 2234 | 2325 | return $filename; |
| 2235 | 2326 | } |
| 2236 | 2327 | |
@@ -2316,17 +2407,19 @@ discard block |
||
| 2316 | 2407 | $this->consumer = $consumer; |
| 2317 | 2408 | $this->token = $token; |
| 2318 | 2409 | $this->parameters = array(); |
| 2319 | - foreach ($parameters as $key => $value) |
|
| 2320 | - if ($value !== null) |
|
| 2410 | + foreach ($parameters as $key => $value) { |
|
| 2411 | + if ($value !== null) |
|
| 2321 | 2412 | $this->parameters[$key] = $value; |
| 2413 | + } |
|
| 2322 | 2414 | } |
| 2323 | 2415 | |
| 2324 | 2416 | public function get_parameter($name) |
| 2325 | 2417 | { |
| 2326 | - if (isset($this->parameters[$name])) |
|
| 2327 | - return $this->parameters[$name]; |
|
| 2328 | - else |
|
| 2329 | - return null; |
|
| 2418 | + if (isset($this->parameters[$name])) { |
|
| 2419 | + return $this->parameters[$name]; |
|
| 2420 | + } else { |
|
| 2421 | + return null; |
|
| 2422 | + } |
|
| 2330 | 2423 | } |
| 2331 | 2424 | |
| 2332 | 2425 | public function get_all_parameters_including_unknown() |
@@ -2377,7 +2470,9 @@ discard block |
||
| 2377 | 2470 | break; |
| 2378 | 2471 | } |
| 2379 | 2472 | } |
| 2380 | - if ($this->opt_min_auth_level === null) throw new Exception("Required 'min_auth_level' option is missing."); |
|
| 2473 | + if ($this->opt_min_auth_level === null) { |
|
| 2474 | + throw new Exception("Required 'min_auth_level' option is missing."); |
|
| 2475 | + } |
|
| 2381 | 2476 | |
| 2382 | 2477 | if ($DEBUG_AS_USERNAME != null) |
| 2383 | 2478 | { |
@@ -2416,24 +2511,23 @@ discard block |
||
| 2416 | 2511 | |
| 2417 | 2512 | list($this->consumer, $this->token) = Okapi::$server-> |
| 2418 | 2513 | 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' ". |
|
| 2514 | + if ($this->get_parameter('consumer_key') && $this->get_parameter('consumer_key') != $this->get_parameter('oauth_consumer_key')) { |
|
| 2515 | + throw new BadRequest("Inproper mixing of authentication types. You used both 'consumer_key' ". |
|
| 2421 | 2516 | "and 'oauth_consumer_key' parameters (Level 1 and Level 2), but they do not match with ". |
| 2422 | 2517 | "each other. Were you trying to hack me? ;)"); |
| 2518 | + } |
|
| 2423 | 2519 | if ($this->opt_min_auth_level == 3 && !$this->token) |
| 2424 | 2520 | { |
| 2425 | 2521 | throw new BadRequest("This method requires a valid Token to be included (Level 3 ". |
| 2426 | 2522 | "Authentication). You didn't provide one."); |
| 2427 | 2523 | } |
| 2428 | - } |
|
| 2429 | - else |
|
| 2524 | + } else |
|
| 2430 | 2525 | { |
| 2431 | 2526 | if ($this->opt_min_auth_level >= 2) |
| 2432 | 2527 | { |
| 2433 | 2528 | throw new BadRequest("This method requires OAuth signature (Level ". |
| 2434 | 2529 | $this->opt_min_auth_level." Authentication). You didn't sign your request."); |
| 2435 | - } |
|
| 2436 | - else |
|
| 2530 | + } else |
|
| 2437 | 2531 | { |
| 2438 | 2532 | $consumer_key = $this->get_parameter('consumer_key'); |
| 2439 | 2533 | if ($consumer_key) |
@@ -2443,9 +2537,10 @@ discard block |
||
| 2443 | 2537 | throw new InvalidParam('consumer_key', "Consumer does not exist."); |
| 2444 | 2538 | } |
| 2445 | 2539 | } |
| 2446 | - if (($this->opt_min_auth_level == 1) && (!$this->consumer)) |
|
| 2447 | - throw new BadRequest("This method requires the 'consumer_key' argument (Level 1 ". |
|
| 2540 | + if (($this->opt_min_auth_level == 1) && (!$this->consumer)) { |
|
| 2541 | + throw new BadRequest("This method requires the 'consumer_key' argument (Level 1 ". |
|
| 2448 | 2542 | "Authentication). You didn't provide one."); |
| 2543 | + } |
|
| 2449 | 2544 | } |
| 2450 | 2545 | } |
| 2451 | 2546 | |
@@ -2479,16 +2574,18 @@ discard block |
||
| 2479 | 2574 | |
| 2480 | 2575 | $debug_user_id = Db::select_value("select user_id from user where username='". |
| 2481 | 2576 | 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']."'"); |
|
| 2577 | + if ($debug_user_id == null) { |
|
| 2578 | + throw new Exception("Invalid user name in DEBUG_AS_USERNAME: '".$options['DEBUG_AS_USERNAME']."'"); |
|
| 2579 | + } |
|
| 2484 | 2580 | $this->consumer = new OkapiDebugConsumer(); |
| 2485 | 2581 | $this->token = new OkapiDebugAccessToken($debug_user_id); |
| 2486 | 2582 | } |
| 2487 | 2583 | |
| 2488 | 2584 | # Read the ETag. |
| 2489 | 2585 | |
| 2490 | - if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) |
|
| 2491 | - $this->etag = $_SERVER['HTTP_IF_NONE_MATCH']; |
|
| 2586 | + if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) { |
|
| 2587 | + $this->etag = $_SERVER['HTTP_IF_NONE_MATCH']; |
|
| 2588 | + } |
|
| 2492 | 2589 | } |
| 2493 | 2590 | |
| 2494 | 2591 | private function init_request() |
@@ -2536,8 +2633,9 @@ discard block |
||
| 2536 | 2633 | # in OKAPI and should be reported back. See issue 85: |
| 2537 | 2634 | # https://github.com/opencaching/okapi/issues/85 |
| 2538 | 2635 | |
| 2539 | - if (is_array($value)) |
|
| 2540 | - throw new InvalidParam($name, "Make sure you are using '$name' no more than ONCE in your URL."); |
|
| 2636 | + if (is_array($value)) { |
|
| 2637 | + throw new InvalidParam($name, "Make sure you are using '$name' no more than ONCE in your URL."); |
|
| 2638 | + } |
|
| 2541 | 2639 | return $value; |
| 2542 | 2640 | } |
| 2543 | 2641 | |
@@ -12,18 +12,22 @@ |
||
| 12 | 12 | public static function get_user_id() |
| 13 | 13 | { |
| 14 | 14 | static $cached_result = false; |
| 15 | - if ($cached_result !== false) |
|
| 16 | - return $cached_result; |
|
| 15 | + if ($cached_result !== false) { |
|
| 16 | + return $cached_result; |
|
| 17 | + } |
|
| 17 | 18 | |
| 18 | 19 | $cookie_name = Settings::get('OC_COOKIE_NAME'); |
| 19 | - if (!isset($_COOKIE[$cookie_name])) |
|
| 20 | - return null; |
|
| 20 | + if (!isset($_COOKIE[$cookie_name])) { |
|
| 21 | + return null; |
|
| 22 | + } |
|
| 21 | 23 | $OC_data = unserialize(base64_decode($_COOKIE[$cookie_name])); |
| 22 | - if (!isset($OC_data['sessionid'])) |
|
| 23 | - return null; |
|
| 24 | + if (!isset($OC_data['sessionid'])) { |
|
| 25 | + return null; |
|
| 26 | + } |
|
| 24 | 27 | $OC_sessionid = $OC_data['sessionid']; |
| 25 | - if (!$OC_sessionid) |
|
| 26 | - return null; |
|
| 28 | + if (!$OC_sessionid) { |
|
| 29 | + return null; |
|
| 30 | + } |
|
| 27 | 31 | |
| 28 | 32 | return Db::select_value("select user_id from sys_sessions where uuid='".Db::escape_string($OC_sessionid)."'"); |
| 29 | 33 | } |