@@ -76,6 +76,10 @@ discard block |
||
| 76 | 76 | } |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | + /** |
|
| 80 | + * @param string $sCode |
|
| 81 | + * @param integer $nStartSearch |
|
| 82 | + */ |
|
| 79 | 83 | public function findEndOfPHPString($sCode, $nStartSearch) |
| 80 | 84 | { |
| 81 | 85 | $nEnd = 0; |
@@ -116,6 +120,10 @@ discard block |
||
| 116 | 120 | } |
| 117 | 121 | |
| 118 | 122 | // TODO: performance ... scan once at __construct and store line positions |
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @param integer $nPos |
|
| 126 | + */ |
|
| 119 | 127 | public function findLineOfPos($nPos) |
| 120 | 128 | { |
| 121 | 129 | $nLine = 1; |
@@ -120,9 +120,9 @@ |
||
| 120 | 120 | { |
| 121 | 121 | $nLine = 1; |
| 122 | 122 | |
| 123 | - for ($n = 0; $n < $nPos; $n ++) { |
|
| 123 | + for ($n = 0; $n < $nPos; $n++) { |
|
| 124 | 124 | if (substr($this->msContent, $n, 1) == "\n") { |
| 125 | - $nLine ++; |
|
| 125 | + $nLine++; |
|
| 126 | 126 | } |
| 127 | 127 | } |
| 128 | 128 | |
@@ -196,6 +196,11 @@ discard block |
||
| 196 | 196 | |
| 197 | 197 | /* import variables for substition from config2/nodetext/ |
| 198 | 198 | */ |
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * @param string $file |
|
| 202 | + * @param string $language |
|
| 203 | + */ |
|
| 199 | 204 | public function loadNodeTextFile(&$variables, $file, $language) |
| 200 | 205 | { |
| 201 | 206 | // generic load |
@@ -222,6 +227,9 @@ discard block |
||
| 222 | 227 | return false; |
| 223 | 228 | } |
| 224 | 229 | |
| 230 | + /** |
|
| 231 | + * @param string $lang |
|
| 232 | + */ |
|
| 225 | 233 | public function substitueVariables(&$variables, $lang, $str) |
| 226 | 234 | { |
| 227 | 235 | $langstr = $str; |
@@ -246,11 +246,21 @@ discard block |
||
| 246 | 246 | return $ret; |
| 247 | 247 | } |
| 248 | 248 | |
| 249 | +/** |
|
| 250 | + * @param string $name |
|
| 251 | + * @param string $default |
|
| 252 | + * |
|
| 253 | + * @return string |
|
| 254 | + */ |
|
| 249 | 255 | function getSysConfig($name, $default) |
| 250 | 256 | { |
| 251 | 257 | return sql_value("SELECT `value` FROM `sysconfig` WHERE `name`='&1'", $default, $name); |
| 252 | 258 | } |
| 253 | 259 | |
| 260 | +/** |
|
| 261 | + * @param string $name |
|
| 262 | + * @param string $value |
|
| 263 | + */ |
|
| 254 | 264 | function setSysConfig($name, $value) |
| 255 | 265 | { |
| 256 | 266 | sql( |
@@ -333,6 +343,9 @@ discard block |
||
| 333 | 343 | } |
| 334 | 344 | |
| 335 | 345 | |
| 346 | +/** |
|
| 347 | + * @return string |
|
| 348 | + */ |
|
| 336 | 349 | function escape_javascript($text) |
| 337 | 350 | { |
| 338 | 351 | return str_replace('\'', '\\\'', str_replace('"', '"', $text)); |
@@ -229,7 +229,7 @@ |
||
| 229 | 229 | |
| 230 | 230 | while ($nCurPos < mb_strlen($str)) { |
| 231 | 231 | $nNextSep = mb_strlen($str); |
| 232 | - for ($nSepPos = 0; $nSepPos < mb_strlen($sep); $nSepPos ++) { |
|
| 232 | + for ($nSepPos = 0; $nSepPos < mb_strlen($sep); $nSepPos++) { |
|
| 233 | 233 | $nThisPos = mb_strpos($str, mb_substr($sep, $nSepPos, 1), $nCurPos); |
| 234 | 234 | if ($nThisPos !== false) { |
| 235 | 235 | if ($nNextSep > $nThisPos) { |
@@ -128,6 +128,9 @@ |
||
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | |
| 131 | +/** |
|
| 132 | + * @param boolean $include_editor |
|
| 133 | + */ |
|
| 131 | 134 | function assignFromDB($userid, $include_editor) |
| 132 | 135 | { |
| 133 | 136 | global $tpl, $opt, $smilies, $_REQUEST; |
@@ -182,6 +182,9 @@ |
||
| 182 | 182 | $tpl->display(); |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | +/** |
|
| 186 | + * @param user $user |
|
| 187 | + */ |
|
| 185 | 188 | function assignFromUser($user) |
| 186 | 189 | { |
| 187 | 190 | global $tpl; |
@@ -188,6 +188,9 @@ discard block |
||
| 188 | 188 | } |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | + /** |
|
| 192 | + * @return string |
|
| 193 | + */ |
|
| 191 | 194 | public static function removeSensitiveData($message) |
| 192 | 195 | { |
| 193 | 196 | return str_replace( |
@@ -553,6 +556,10 @@ discard block |
||
| 553 | 556 | return mysql_affected_rows(); |
| 554 | 557 | } |
| 555 | 558 | |
| 559 | + /** |
|
| 560 | + * @param string $table |
|
| 561 | + * @param string $field |
|
| 562 | + */ |
|
| 556 | 563 | public static function field_exists($table, $field) |
| 557 | 564 | { |
| 558 | 565 | if (!preg_match("/[a-z0-9_]+/", $table.$field)) |
@@ -679,6 +686,9 @@ discard block |
||
| 679 | 686 | public $consumer_key; |
| 680 | 687 | public $token_type; |
| 681 | 688 | |
| 689 | + /** |
|
| 690 | + * @param string $token_type |
|
| 691 | + */ |
|
| 682 | 692 | public function __construct($key, $secret, $consumer_key, $token_type) |
| 683 | 693 | { |
| 684 | 694 | parent::__construct($key, $secret); |
@@ -755,6 +765,7 @@ discard block |
||
| 755 | 765 | * options. If $token_required == false, it doesn't throw an exception when |
| 756 | 766 | * there is no token specified. You may also change the token_type required |
| 757 | 767 | * for this request. |
| 768 | + * @param OAuthRequest $request |
|
| 758 | 769 | */ |
| 759 | 770 | public function verify_request2(&$request, $token_type = 'access', $token_required = true) |
| 760 | 771 | { |
@@ -1052,6 +1063,7 @@ discard block |
||
| 1052 | 1063 | * Save a variable to okapi_vars. WARNING: The entire content of okapi_vars table |
| 1053 | 1064 | * is loaded on EVERY execution. Do not store data in this table, unless it's |
| 1054 | 1065 | * frequently needed. |
| 1066 | + * @param string $varname |
|
| 1055 | 1067 | */ |
| 1056 | 1068 | public static function set_var($varname, $value) |
| 1057 | 1069 | { |
@@ -1330,6 +1342,7 @@ discard block |
||
| 1330 | 1342 | |
| 1331 | 1343 | /** |
| 1332 | 1344 | * Split the array into groups of max. $size items. |
| 1345 | + * @param integer $size |
|
| 1333 | 1346 | */ |
| 1334 | 1347 | public static function make_groups($array, $size) |
| 1335 | 1348 | { |
@@ -1476,6 +1489,7 @@ discard block |
||
| 1476 | 1489 | * Generate a string of random characters, suitable for keys as passwords. |
| 1477 | 1490 | * Troublesome characters like '0', 'O', '1', 'l' will not be used. |
| 1478 | 1491 | * If $user_friendly=true, then it will consist from numbers only. |
| 1492 | + * @param integer $length |
|
| 1479 | 1493 | */ |
| 1480 | 1494 | public static function generate_key($length, $user_friendly = false) |
| 1481 | 1495 | { |
@@ -1496,6 +1510,7 @@ discard block |
||
| 1496 | 1510 | * Register new OKAPI Consumer, send him an email with his key-pair, etc. |
| 1497 | 1511 | * This method does not verify parameter values, check if they are in |
| 1498 | 1512 | * a correct format prior the execution. |
| 1513 | + * @param string $appname |
|
| 1499 | 1514 | */ |
| 1500 | 1515 | public static function register_new_consumer($appname, $appurl, $email) |
| 1501 | 1516 | { |
@@ -1565,6 +1580,8 @@ discard block |
||
| 1565 | 1580 | /** |
| 1566 | 1581 | * Return an SQL formula for calculating distance between two geopoints. |
| 1567 | 1582 | * Parameters should be either numberals or strings (SQL field references). |
| 1583 | + * @param null|string $lat2 |
|
| 1584 | + * @param null|string $lon2 |
|
| 1568 | 1585 | */ |
| 1569 | 1586 | public static function get_distance_sql($lat1, $lon1, $lat2, $lon2) |
| 1570 | 1587 | { |
@@ -2044,6 +2061,9 @@ discard block |
||
| 2044 | 2061 | return $html; |
| 2045 | 2062 | } |
| 2046 | 2063 | |
| 2064 | + /** |
|
| 2065 | + * @param string $variable |
|
| 2066 | + */ |
|
| 2047 | 2067 | function php_ini_get_bytes($variable) |
| 2048 | 2068 | { |
| 2049 | 2069 | $value = trim(ini_get($variable)); |
@@ -2077,6 +2097,7 @@ discard block |
||
| 2077 | 2097 | * |
| 2078 | 2098 | * If $timeout is null, then the object will be treated as persistent |
| 2079 | 2099 | * (the Cache will do its best to NEVER remove it). |
| 2100 | + * @param string $key |
|
| 2080 | 2101 | */ |
| 2081 | 2102 | public static function set($key, $value, $timeout) |
| 2082 | 2103 | { |
@@ -2099,6 +2120,8 @@ discard block |
||
| 2099 | 2120 | /** |
| 2100 | 2121 | * Scored version of set. Elements set up this way will expire when they're |
| 2101 | 2122 | * not used. |
| 2123 | + * @param string $key |
|
| 2124 | + * @param string $value |
|
| 2102 | 2125 | */ |
| 2103 | 2126 | public static function set_scored($key, $value) |
| 2104 | 2127 | { |
@@ -2201,6 +2224,7 @@ discard block |
||
| 2201 | 2224 | |
| 2202 | 2225 | /** |
| 2203 | 2226 | * Delete key $key from the cache. |
| 2227 | + * @param string $key |
|
| 2204 | 2228 | */ |
| 2205 | 2229 | public static function delete($key) |
| 2206 | 2230 | { |
@@ -2226,6 +2250,9 @@ discard block |
||
| 2226 | 2250 | */ |
| 2227 | 2251 | class FileCache |
| 2228 | 2252 | { |
| 2253 | + /** |
|
| 2254 | + * @param string $key |
|
| 2255 | + */ |
|
| 2229 | 2256 | public static function get_file_path($key) |
| 2230 | 2257 | { |
| 2231 | 2258 | $filename = Okapi::get_var_dir()."/okapi_filecache_".md5($key); |
@@ -2238,6 +2265,8 @@ discard block |
||
| 2238 | 2265 | * Note, there is no $timeout (time to live) parameter. Currently, |
| 2239 | 2266 | * OKAPI will delete every old file after certain amount of time. |
| 2240 | 2267 | * See CacheCleanupCronJob for details. |
| 2268 | + * @param string $key |
|
| 2269 | + * @param string $value |
|
| 2241 | 2270 | */ |
| 2242 | 2271 | public static function set($key, $value) |
| 2243 | 2272 | { |
@@ -2274,6 +2303,7 @@ discard block |
||
| 2274 | 2303 | /** |
| 2275 | 2304 | * Return request parameter, or NULL when not found. Use this instead of |
| 2276 | 2305 | * $_GET or $_POST or $_REQUEST. |
| 2306 | + * @param string $name |
|
| 2277 | 2307 | */ |
| 2278 | 2308 | public abstract function get_parameter($name); |
| 2279 | 2309 | |
@@ -2310,6 +2340,7 @@ discard block |
||
| 2310 | 2340 | /** |
| 2311 | 2341 | * You may use "null" values in parameters if you want them skipped |
| 2312 | 2342 | * (null-ized keys will be removed from parameters). |
| 2343 | + * @param OkapiConsumer|null $consumer |
|
| 2313 | 2344 | */ |
| 2314 | 2345 | public function __construct($consumer, $token, $parameters) |
| 2315 | 2346 | { |
@@ -2526,6 +2557,7 @@ discard block |
||
| 2526 | 2557 | /** |
| 2527 | 2558 | * Return request parameter, or NULL when not found. Use this instead of |
| 2528 | 2559 | * $_GET or $_POST or $_REQUEST. |
| 2560 | + * @param string $name |
|
| 2529 | 2561 | */ |
| 2530 | 2562 | public function get_parameter($name) |
| 2531 | 2563 | { |
@@ -12,7 +12,6 @@ |
||
| 12 | 12 | use ArrayObject; |
| 13 | 13 | use okapi\oauth\OAuthServerException; |
| 14 | 14 | use okapi\oauth\OAuthServer400Exception; |
| 15 | -use okapi\oauth\OAuthServer401Exception; |
|
| 16 | 15 | use okapi\oauth\OAuthMissingParameterException; |
| 17 | 16 | use okapi\oauth\OAuthConsumer; |
| 18 | 17 | use okapi\oauth\OAuthToken; |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | 'reason_stack' => array(), |
| 57 | 57 | ); |
| 58 | 58 | $this->provideExtras($extras); |
| 59 | - $extras['more_info'] = Settings::get('SITE_URL')."okapi/introduction.html#errors"; |
|
| 59 | + $extras['more_info'] = Settings::get('SITE_URL') . "okapi/introduction.html#errors"; |
|
| 60 | 60 | return json_encode(array("error" => $extras)); |
| 61 | 61 | } |
| 62 | 62 | } |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | { |
| 132 | 132 | print "\n\nBUT! Since the DEBUG flag is on, then you probably ARE a developer yourself.\n"; |
| 133 | 133 | print "Let's cut to the chase then:"; |
| 134 | - print "\n\n".$exception_info; |
|
| 134 | + print "\n\n" . $exception_info; |
|
| 135 | 135 | } |
| 136 | 136 | if (class_exists("okapi\\Settings") && (Settings::get('DEBUG_PREVENT_EMAILS'))) |
| 137 | 137 | { |
@@ -140,14 +140,14 @@ discard block |
||
| 140 | 140 | } |
| 141 | 141 | else |
| 142 | 142 | { |
| 143 | - $subject = "OKAPI Method Error - ".substr( |
|
| 143 | + $subject = "OKAPI Method Error - " . substr( |
|
| 144 | 144 | $_SERVER['REQUEST_URI'], 0, strpos( |
| 145 | - $_SERVER['REQUEST_URI'].'?', '?')); |
|
| 145 | + $_SERVER['REQUEST_URI'] . '?', '?')); |
|
| 146 | 146 | |
| 147 | 147 | $message = ( |
| 148 | - "OKAPI caught the following exception while executing API method request.\n". |
|
| 149 | - "This is an error in OUR code and should be fixed. Please contact the\n". |
|
| 150 | - "developer of the module that threw this error. Thanks!\n\n". |
|
| 148 | + "OKAPI caught the following exception while executing API method request.\n" . |
|
| 149 | + "This is an error in OUR code and should be fixed. Please contact the\n" . |
|
| 150 | + "developer of the module that threw this error. Thanks!\n\n" . |
|
| 151 | 151 | $exception_info |
| 152 | 152 | ); |
| 153 | 153 | try |
@@ -175,11 +175,11 @@ discard block |
||
| 175 | 175 | |
| 176 | 176 | $admin_email = implode(", ", get_admin_emails()); |
| 177 | 177 | $sender_email = class_exists("okapi\\Settings") ? Settings::get('FROM_FIELD') : 'root@localhost'; |
| 178 | - $subject = "Fatal error mode: ".$subject; |
|
| 179 | - $message = "Fatal error mode: OKAPI will send at most ONE message per minute.\n\n".$message; |
|
| 178 | + $subject = "Fatal error mode: " . $subject; |
|
| 179 | + $message = "Fatal error mode: OKAPI will send at most ONE message per minute.\n\n" . $message; |
|
| 180 | 180 | $headers = ( |
| 181 | - "Content-Type: text/plain; charset=utf-8\n". |
|
| 182 | - "From: OKAPI <$sender_email>\n". |
|
| 181 | + "Content-Type: text/plain; charset=utf-8\n" . |
|
| 182 | + "From: OKAPI <$sender_email>\n" . |
|
| 183 | 183 | "Reply-To: $sender_email\n" |
| 184 | 184 | ); |
| 185 | 185 | mail($admin_email, $subject, $message, $headers); |
@@ -193,9 +193,9 @@ discard block |
||
| 193 | 193 | return str_replace( |
| 194 | 194 | array( |
| 195 | 195 | Settings::get('DB_PASSWORD'), |
| 196 | - "'".Settings::get('DB_USERNAME')."'", |
|
| 196 | + "'" . Settings::get('DB_USERNAME') . "'", |
|
| 197 | 197 | Settings::get('DB_SERVER'), |
| 198 | - "'".Settings::get('DB_NAME')."'" |
|
| 198 | + "'" . Settings::get('DB_NAME') . "'" |
|
| 199 | 199 | ), |
| 200 | 200 | array( |
| 201 | 201 | "******", |
@@ -218,25 +218,25 @@ discard block |
||
| 218 | 218 | # by OkapiErrorHandler::handle_shutdown. Instead of printing trace, we will just print |
| 219 | 219 | # the file and line. |
| 220 | 220 | |
| 221 | - $exception_info .= "File: ".$e->getFile()."\nLine: ".$e->getLine()."\n\n"; |
|
| 221 | + $exception_info .= "File: " . $e->getFile() . "\nLine: " . $e->getLine() . "\n\n"; |
|
| 222 | 222 | } |
| 223 | 223 | else |
| 224 | 224 | { |
| 225 | - $exception_info .= "--- Stack trace ---\n". |
|
| 226 | - self::removeSensitiveData($e->getTraceAsString())."\n\n"; |
|
| 225 | + $exception_info .= "--- Stack trace ---\n" . |
|
| 226 | + self::removeSensitiveData($e->getTraceAsString()) . "\n\n"; |
|
| 227 | 227 | } |
| 228 | 228 | |
| 229 | - $exception_info .= (isset($_SERVER['REQUEST_URI']) ? "--- OKAPI method called ---\n". |
|
| 230 | - preg_replace("/([?&])/", "\n$1", $_SERVER['REQUEST_URI'])."\n\n" : ""); |
|
| 231 | - $exception_info .= "--- OKAPI version ---\n".Okapi::$version_number. |
|
| 232 | - " (".Okapi::$git_revision.")\n\n"; |
|
| 229 | + $exception_info .= (isset($_SERVER['REQUEST_URI']) ? "--- OKAPI method called ---\n" . |
|
| 230 | + preg_replace("/([?&])/", "\n$1", $_SERVER['REQUEST_URI']) . "\n\n" : ""); |
|
| 231 | + $exception_info .= "--- OKAPI version ---\n" . Okapi::$version_number . |
|
| 232 | + " (" . Okapi::$git_revision . ")\n\n"; |
|
| 233 | 233 | |
| 234 | 234 | # This if-condition will solve some (but not all) problems when trying to execute |
| 235 | 235 | # OKAPI code from command line; |
| 236 | 236 | # see https://github.com/opencaching/okapi/issues/243. |
| 237 | 237 | if (function_exists('getallheaders')) |
| 238 | 238 | { |
| 239 | - $exception_info .= "--- Request headers ---\n".implode("\n", array_map( |
|
| 239 | + $exception_info .= "--- Request headers ---\n" . implode("\n", array_map( |
|
| 240 | 240 | function($k, $v) { return "$k: $v"; }, |
| 241 | 241 | array_keys(getallheaders()), array_values(getallheaders()) |
| 242 | 242 | )); |
@@ -341,7 +341,7 @@ discard block |
||
| 341 | 341 | $this->paramName = $paramName; |
| 342 | 342 | $this->whats_wrong_about_it = $whats_wrong_about_it; |
| 343 | 343 | if ($whats_wrong_about_it) |
| 344 | - parent::__construct("Parameter '$paramName' has invalid value: ".$whats_wrong_about_it, $code); |
|
| 344 | + parent::__construct("Parameter '$paramName' has invalid value: " . $whats_wrong_about_it, $code); |
|
| 345 | 345 | else |
| 346 | 346 | parent::__construct("Parameter '$paramName' has invalid value.", $code); |
| 347 | 347 | } |
@@ -371,7 +371,7 @@ discard block |
||
| 371 | 371 | self::$connected = true; |
| 372 | 372 | } |
| 373 | 373 | else |
| 374 | - throw new Exception("Could not connect to MySQL: ".mysql_error()); |
|
| 374 | + throw new Exception("Could not connect to MySQL: " . mysql_error()); |
|
| 375 | 375 | } |
| 376 | 376 | |
| 377 | 377 | /** Fetch [{row}], return {row}. */ |
@@ -383,7 +383,7 @@ discard block |
||
| 383 | 383 | case 0: return null; |
| 384 | 384 | case 1: return $rows[0]; |
| 385 | 385 | default: |
| 386 | - throw new DbException("Invalid query. Db::select_row returned more than one row for:\n\n".$query."\n"); |
|
| 386 | + throw new DbException("Invalid query. Db::select_row returned more than one row for:\n\n" . $query . "\n"); |
|
| 387 | 387 | } |
| 388 | 388 | } |
| 389 | 389 | |
@@ -436,7 +436,7 @@ discard block |
||
| 436 | 436 | return null; |
| 437 | 437 | if (count($column) == 1) |
| 438 | 438 | return $column[0]; |
| 439 | - throw new DbException("Invalid query. Db::select_value returned more than one row for:\n\n".$query."\n"); |
|
| 439 | + throw new DbException("Invalid query. Db::select_value returned more than one row for:\n\n" . $query . "\n"); |
|
| 440 | 440 | } |
| 441 | 441 | |
| 442 | 442 | /** Fetch all [(A), (B), (C)], return [A, B, C]. */ |
@@ -489,7 +489,7 @@ discard block |
||
| 489 | 489 | { |
| 490 | 490 | $rs = self::query($query); |
| 491 | 491 | if ($rs !== true) |
| 492 | - throw new DbException("Db::execute returned a result set for your query. ". |
|
| 492 | + throw new DbException("Db::execute returned a result set for your query. " . |
|
| 493 | 493 | "You should use Db::select_* or Db::query for SELECT queries!"); |
| 494 | 494 | } |
| 495 | 495 | |
@@ -514,8 +514,8 @@ discard block |
||
| 514 | 514 | self::execute("repair table okapi_cache"); |
| 515 | 515 | Okapi::mail_admins( |
| 516 | 516 | "okapi_cache - Automatic repair", |
| 517 | - "Hi.\n\nOKAPI detected that okapi_cache table needed ". |
|
| 518 | - "repairs and it has performed such\nrepairs automatically. ". |
|
| 517 | + "Hi.\n\nOKAPI detected that okapi_cache table needed " . |
|
| 518 | + "repairs and it has performed such\nrepairs automatically. " . |
|
| 519 | 519 | "However, this should not happen regularly!" |
| 520 | 520 | ); |
| 521 | 521 | } catch (Exception $e) { |
@@ -526,10 +526,10 @@ discard block |
||
| 526 | 526 | self::execute("truncate okapi_cache"); |
| 527 | 527 | Okapi::mail_admins( |
| 528 | 528 | "okapi_cache was truncated", |
| 529 | - "Hi.\n\nOKAPI detected that okapi_cache table needed ". |
|
| 530 | - "repairs, but it failed to repair\nthe table automatically. ". |
|
| 531 | - "In order to counteract more severe errors, \nwe have ". |
|
| 532 | - "truncated the okapi_cache table to make it alive.\n". |
|
| 529 | + "Hi.\n\nOKAPI detected that okapi_cache table needed " . |
|
| 530 | + "repairs, but it failed to repair\nthe table automatically. " . |
|
| 531 | + "In order to counteract more severe errors, \nwe have " . |
|
| 532 | + "truncated the okapi_cache table to make it alive.\n" . |
|
| 533 | 533 | "However, this should not happen regularly!" |
| 534 | 534 | ); |
| 535 | 535 | } catch (Exception $e) { |
@@ -538,7 +538,7 @@ discard block |
||
| 538 | 538 | } |
| 539 | 539 | } |
| 540 | 540 | |
| 541 | - throw new DbException("SQL Error $errno: $msg\n\nThe query was:\n".$query."\n"); |
|
| 541 | + throw new DbException("SQL Error $errno: $msg\n\nThe query was:\n" . $query . "\n"); |
|
| 542 | 542 | } |
| 543 | 543 | return $rs; |
| 544 | 544 | } |
@@ -555,10 +555,10 @@ discard block |
||
| 555 | 555 | |
| 556 | 556 | public static function field_exists($table, $field) |
| 557 | 557 | { |
| 558 | - if (!preg_match("/[a-z0-9_]+/", $table.$field)) |
|
| 558 | + if (!preg_match("/[a-z0-9_]+/", $table . $field)) |
|
| 559 | 559 | return false; |
| 560 | 560 | try { |
| 561 | - $spec = self::select_all("desc ".$table.";"); |
|
| 561 | + $spec = self::select_all("desc " . $table . ";"); |
|
| 562 | 562 | } catch (Exception $e) { |
| 563 | 563 | /* Table doesn't exist, probably. */ |
| 564 | 564 | return false; |
@@ -575,7 +575,7 @@ discard block |
||
| 575 | 575 | # Including OAuth internals. Preparing OKAPI Consumer and Token classes. |
| 576 | 576 | # |
| 577 | 577 | |
| 578 | -require_once($GLOBALS['rootpath']."okapi/oauth.php"); |
|
| 578 | +require_once($GLOBALS['rootpath'] . "okapi/oauth.php"); |
|
| 579 | 579 | |
| 580 | 580 | class OkapiConsumer extends OAuthConsumer |
| 581 | 581 | { |
@@ -611,7 +611,7 @@ discard block |
||
| 611 | 611 | */ |
| 612 | 612 | const FLAG_KEY_REVOKED = 4; |
| 613 | 613 | |
| 614 | - public function __construct($key, $secret, $name, $url, $email, $bflags=0) |
|
| 614 | + public function __construct($key, $secret, $name, $url, $email, $bflags = 0) |
|
| 615 | 615 | { |
| 616 | 616 | $this->key = $key; |
| 617 | 617 | $this->secret = $secret; |
@@ -718,7 +718,7 @@ discard block |
||
| 718 | 718 | { |
| 719 | 719 | public function __construct($user_id) |
| 720 | 720 | { |
| 721 | - parent::__construct('internal-'.$user_id, null, 'internal', $user_id); |
|
| 721 | + parent::__construct('internal-' . $user_id, null, 'internal', $user_id); |
|
| 722 | 722 | } |
| 723 | 723 | } |
| 724 | 724 | |
@@ -727,7 +727,7 @@ discard block |
||
| 727 | 727 | { |
| 728 | 728 | public function __construct($user_id) |
| 729 | 729 | { |
| 730 | - parent::__construct('facade-'.$user_id, null, 'facade', $user_id); |
|
| 730 | + parent::__construct('facade-' . $user_id, null, 'facade', $user_id); |
|
| 731 | 731 | } |
| 732 | 732 | } |
| 733 | 733 | |
@@ -736,7 +736,7 @@ discard block |
||
| 736 | 736 | { |
| 737 | 737 | public function __construct($user_id) |
| 738 | 738 | { |
| 739 | - parent::__construct('debug-'.$user_id, null, 'debug', $user_id); |
|
| 739 | + parent::__construct('debug-' . $user_id, null, 'debug', $user_id); |
|
| 740 | 740 | } |
| 741 | 741 | } |
| 742 | 742 | |
@@ -777,8 +777,8 @@ discard block |
||
| 777 | 777 | |
| 778 | 778 | # Including local datastore and settings (connecting SQL database etc.). |
| 779 | 779 | |
| 780 | -require_once($GLOBALS['rootpath']."okapi/settings.php"); |
|
| 781 | -require_once($GLOBALS['rootpath']."okapi/datastore.php"); |
|
| 780 | +require_once($GLOBALS['rootpath'] . "okapi/settings.php"); |
|
| 781 | +require_once($GLOBALS['rootpath'] . "okapi/datastore.php"); |
|
| 782 | 782 | |
| 783 | 783 | class OkapiHttpResponse |
| 784 | 784 | { |
@@ -809,7 +809,7 @@ discard block |
||
| 809 | 809 | if (is_resource($this->body)) |
| 810 | 810 | { |
| 811 | 811 | while (!feof($this->body)) |
| 812 | - print fread($this->body, 1024*1024); |
|
| 812 | + print fread($this->body, 1024 * 1024); |
|
| 813 | 813 | } |
| 814 | 814 | else |
| 815 | 815 | print $this->body; |
@@ -836,14 +836,14 @@ discard block |
||
| 836 | 836 | */ |
| 837 | 837 | public function display() |
| 838 | 838 | { |
| 839 | - header("HTTP/1.1 ".$this->status); |
|
| 839 | + header("HTTP/1.1 " . $this->status); |
|
| 840 | 840 | header("Access-Control-Allow-Origin: *"); |
| 841 | - header("Content-Type: ".$this->content_type); |
|
| 842 | - header("Cache-Control: ".$this->cache_control); |
|
| 841 | + header("Content-Type: " . $this->content_type); |
|
| 842 | + header("Cache-Control: " . $this->cache_control); |
|
| 843 | 843 | if ($this->connection_close) |
| 844 | 844 | header("Connection: close"); |
| 845 | 845 | if ($this->content_disposition) |
| 846 | - header("Content-Disposition: ".$this->content_disposition); |
|
| 846 | + header("Content-Disposition: " . $this->content_disposition); |
|
| 847 | 847 | if ($this->etag) |
| 848 | 848 | header("ETag: $this->etag"); |
| 849 | 849 | |
@@ -861,7 +861,7 @@ discard block |
||
| 861 | 861 | |
| 862 | 862 | header("Content-Encoding: gzip"); |
| 863 | 863 | $gzipped = gzencode($this->body, 5); |
| 864 | - header("Content-Length: ".strlen($gzipped)); |
|
| 864 | + header("Content-Length: " . strlen($gzipped)); |
|
| 865 | 865 | print $gzipped; |
| 866 | 866 | } |
| 867 | 867 | else |
@@ -874,7 +874,7 @@ discard block |
||
| 874 | 874 | |
| 875 | 875 | $length = $this->get_length(); |
| 876 | 876 | if ($length) |
| 877 | - header("Content-Length: ".$length); |
|
| 877 | + header("Content-Length: " . $length); |
|
| 878 | 878 | $this->print_body(); |
| 879 | 879 | } |
| 880 | 880 | } |
@@ -887,11 +887,11 @@ discard block |
||
| 887 | 887 | public function display() |
| 888 | 888 | { |
| 889 | 889 | header("HTTP/1.1 303 See Other"); |
| 890 | - header("Location: ".$this->url); |
|
| 890 | + header("Location: " . $this->url); |
|
| 891 | 891 | } |
| 892 | 892 | } |
| 893 | 893 | |
| 894 | -require_once ($GLOBALS['rootpath'].'okapi/lib/tbszip.php'); |
|
| 894 | +require_once ($GLOBALS['rootpath'] . 'okapi/lib/tbszip.php'); |
|
| 895 | 895 | use \clsTbsZip; |
| 896 | 896 | |
| 897 | 897 | class OkapiZIPHttpResponse extends OkapiHttpResponse |
@@ -906,7 +906,7 @@ discard block |
||
| 906 | 906 | |
| 907 | 907 | public function print_body() |
| 908 | 908 | { |
| 909 | - $this->zip->Flush(clsTbsZip::TBSZIP_DOWNLOAD|clsTbsZip::TBSZIP_NOHEADER); |
|
| 909 | + $this->zip->Flush(clsTbsZip::TBSZIP_DOWNLOAD | clsTbsZip::TBSZIP_NOHEADER); |
|
| 910 | 910 | } |
| 911 | 911 | |
| 912 | 912 | public function get_body() |
@@ -938,7 +938,7 @@ discard block |
||
| 938 | 938 | /** Note: This does NOT tell you if someone currently locked it! */ |
| 939 | 939 | public static function exists($name) |
| 940 | 940 | { |
| 941 | - $lockfile = Okapi::get_var_dir()."/okapi-lock-".$name; |
|
| 941 | + $lockfile = Okapi::get_var_dir() . "/okapi-lock-" . $name; |
|
| 942 | 942 | return file_exists($lockfile); |
| 943 | 943 | } |
| 944 | 944 | |
@@ -957,7 +957,7 @@ discard block |
||
| 957 | 957 | } |
| 958 | 958 | else |
| 959 | 959 | { |
| 960 | - $this->lockfile = Okapi::get_var_dir()."/okapi-lock-".$name; |
|
| 960 | + $this->lockfile = Okapi::get_var_dir() . "/okapi-lock-" . $name; |
|
| 961 | 961 | $this->lock = fopen($this->lockfile, "wb"); |
| 962 | 962 | } |
| 963 | 963 | } |
@@ -973,7 +973,7 @@ discard block |
||
| 973 | 973 | if ($this->lock !== null) |
| 974 | 974 | return flock($this->lock, LOCK_EX | LOCK_NB); |
| 975 | 975 | else |
| 976 | - return true; # $lock can be null only when debugging |
|
| 976 | + return true; # $lock can be null only when debugging |
|
| 977 | 977 | } |
| 978 | 978 | |
| 979 | 979 | public function release() |
@@ -1059,8 +1059,8 @@ discard block |
||
| 1059 | 1059 | Db::execute(" |
| 1060 | 1060 | replace into okapi_vars (var, value) |
| 1061 | 1061 | values ( |
| 1062 | - '".Db::escape_string($varname)."', |
|
| 1063 | - '".Db::escape_string($value)."'); |
|
| 1062 | + '".Db::escape_string($varname) . "', |
|
| 1063 | + '".Db::escape_string($value) . "'); |
|
| 1064 | 1064 | "); |
| 1065 | 1065 | self::$okapi_vars[$varname] = $value; |
| 1066 | 1066 | } |
@@ -1083,12 +1083,12 @@ discard block |
||
| 1083 | 1083 | # Make sure we're not sending HUGE emails. |
| 1084 | 1084 | |
| 1085 | 1085 | if (strlen($message) > 10000) { |
| 1086 | - $message = substr($message, 0, 10000)."\n\n...(message clipped at 10k chars)\n"; |
|
| 1086 | + $message = substr($message, 0, 10000) . "\n\n...(message clipped at 10k chars)\n"; |
|
| 1087 | 1087 | } |
| 1088 | 1088 | |
| 1089 | 1089 | # Make sure we're not spamming. |
| 1090 | 1090 | |
| 1091 | - $cache_key = 'mail_admins_counter/'.(floor(time() / 3600) * 3600).'/'.md5($subject); |
|
| 1091 | + $cache_key = 'mail_admins_counter/' . (floor(time() / 3600) * 3600) . '/' . md5($subject); |
|
| 1092 | 1092 | try { |
| 1093 | 1093 | $counter = Cache::get($cache_key); |
| 1094 | 1094 | } catch (DbException $e) { |
@@ -1119,22 +1119,22 @@ discard block |
||
| 1119 | 1119 | { |
| 1120 | 1120 | # We are spamming. Prevent sending more emails. |
| 1121 | 1121 | |
| 1122 | - $content_cache_key_prefix = 'mail_admins_spam/'.(floor(time() / 3600) * 3600).'/'; |
|
| 1122 | + $content_cache_key_prefix = 'mail_admins_spam/' . (floor(time() / 3600) * 3600) . '/'; |
|
| 1123 | 1123 | $timeout = 86400; |
| 1124 | 1124 | if ($counter == 6) |
| 1125 | 1125 | { |
| 1126 | 1126 | self::mail_from_okapi(get_admin_emails(), "Anti-spam mode activated for '$subject'", |
| 1127 | - "OKAPI has activated an \"anti-spam\" mode for the following subject:\n\n". |
|
| 1128 | - "\"$subject\"\n\n". |
|
| 1129 | - "Anti-spam mode is activiated when more than 5 messages with\n". |
|
| 1130 | - "the same subject are sent within one hour.\n\n". |
|
| 1131 | - "Additional debug information:\n". |
|
| 1132 | - "- counter cache key: $cache_key\n". |
|
| 1133 | - "- content prefix: $content_cache_key_prefix<n>\n". |
|
| 1127 | + "OKAPI has activated an \"anti-spam\" mode for the following subject:\n\n" . |
|
| 1128 | + "\"$subject\"\n\n" . |
|
| 1129 | + "Anti-spam mode is activiated when more than 5 messages with\n" . |
|
| 1130 | + "the same subject are sent within one hour.\n\n" . |
|
| 1131 | + "Additional debug information:\n" . |
|
| 1132 | + "- counter cache key: $cache_key\n" . |
|
| 1133 | + "- content prefix: $content_cache_key_prefix<n>\n" . |
|
| 1134 | 1134 | "- content timeout: $timeout\n" |
| 1135 | 1135 | ); |
| 1136 | 1136 | } |
| 1137 | - $content_cache_key = $content_cache_key_prefix.$counter; |
|
| 1137 | + $content_cache_key = $content_cache_key_prefix . $counter; |
|
| 1138 | 1138 | Cache::set($content_cache_key, $message, $timeout); |
| 1139 | 1139 | } |
| 1140 | 1140 | } |
@@ -1152,8 +1152,8 @@ discard block |
||
| 1152 | 1152 | $email_addresses = array($email_addresses); |
| 1153 | 1153 | $sender_email = class_exists("okapi\\Settings") ? Settings::get('FROM_FIELD') : 'root@localhost'; |
| 1154 | 1154 | mail(implode(", ", $email_addresses), $subject, $message, |
| 1155 | - "Content-Type: text/plain; charset=utf-8\n". |
|
| 1156 | - "From: OKAPI <$sender_email>\n". |
|
| 1155 | + "Content-Type: text/plain; charset=utf-8\n" . |
|
| 1156 | + "From: OKAPI <$sender_email>\n" . |
|
| 1157 | 1157 | "Reply-To: $sender_email\n" |
| 1158 | 1158 | ); |
| 1159 | 1159 | } |
@@ -1174,7 +1174,7 @@ discard block |
||
| 1174 | 1174 | $site_url = Settings::get('SITE_URL'); |
| 1175 | 1175 | $matches = null; |
| 1176 | 1176 | if (preg_match("#^https?://(www.)?opencaching.([a-z.]+)/$#", $site_url, $matches)) { |
| 1177 | - return "Opencaching.".strtoupper($matches[2]); |
|
| 1177 | + return "Opencaching." . strtoupper($matches[2]); |
|
| 1178 | 1178 | } else { |
| 1179 | 1179 | return "DEVELSITE"; |
| 1180 | 1180 | } |
@@ -1198,18 +1198,18 @@ discard block |
||
| 1198 | 1198 | /* All OCDE-based sites use exactly the same schema. */ |
| 1199 | 1199 | |
| 1200 | 1200 | if (Settings::get('OC_BRANCH') == 'oc.de') { |
| 1201 | - return "OCDE"; // OC |
|
| 1201 | + return "OCDE"; // OC |
|
| 1202 | 1202 | } |
| 1203 | 1203 | |
| 1204 | 1204 | /* All OCPL-based sites use separate schemas. (Hopefully, this will |
| 1205 | 1205 | * change in time.) */ |
| 1206 | 1206 | |
| 1207 | 1207 | $mapping = array( |
| 1208 | - 2 => "OCPL", // OP |
|
| 1209 | - 6 => "OCORGUK", // OK |
|
| 1210 | - 10 => "OCUS", // OU |
|
| 1211 | - 14 => "OCNL", // OB |
|
| 1212 | - 16 => "OCRO", // OR |
|
| 1208 | + 2 => "OCPL", // OP |
|
| 1209 | + 6 => "OCORGUK", // OK |
|
| 1210 | + 10 => "OCUS", // OU |
|
| 1211 | + 14 => "OCNL", // OB |
|
| 1212 | + 16 => "OCRO", // OR |
|
| 1213 | 1213 | // should be expanded when new OCPL-based sites are added |
| 1214 | 1214 | ); |
| 1215 | 1215 | $oc_node_id = Settings::get("OC_NODE_ID"); |
@@ -1231,7 +1231,7 @@ discard block |
||
| 1231 | 1231 | */ |
| 1232 | 1232 | public static function get_recommended_base_url() |
| 1233 | 1233 | { |
| 1234 | - return Settings::get('SITE_URL')."okapi/"; |
|
| 1234 | + return Settings::get('SITE_URL') . "okapi/"; |
|
| 1235 | 1235 | } |
| 1236 | 1236 | |
| 1237 | 1237 | /** |
@@ -1261,7 +1261,7 @@ discard block |
||
| 1261 | 1261 | ); |
| 1262 | 1262 | break; |
| 1263 | 1263 | case 'OCDE': |
| 1264 | - if (in_array(Settings::get('OC_NODE_ID'), array(4,5))) { |
|
| 1264 | + if (in_array(Settings::get('OC_NODE_ID'), array(4, 5))) { |
|
| 1265 | 1265 | $urls = array( |
| 1266 | 1266 | preg_replace("/^https:/", "http:", Settings::get('SITE_URL')) . 'okapi/', |
| 1267 | 1267 | preg_replace("/^http:/", "https:", Settings::get('SITE_URL')) . 'okapi/', |
@@ -1352,7 +1352,7 @@ discard block |
||
| 1352 | 1352 | $nearest_event = Okapi::get_var("cron_nearest_event"); |
| 1353 | 1353 | if ($nearest_event + 0 <= time()) |
| 1354 | 1354 | { |
| 1355 | - require_once($GLOBALS['rootpath']."okapi/cronjobs.php"); |
|
| 1355 | + require_once($GLOBALS['rootpath'] . "okapi/cronjobs.php"); |
|
| 1356 | 1356 | $nearest_event = CronJobController::run_jobs('pre-request'); |
| 1357 | 1357 | Okapi::set_var("cron_nearest_event", $nearest_event); |
| 1358 | 1358 | } |
@@ -1369,7 +1369,7 @@ discard block |
||
| 1369 | 1369 | { |
| 1370 | 1370 | set_time_limit(0); |
| 1371 | 1371 | ignore_user_abort(true); |
| 1372 | - require_once($GLOBALS['rootpath']."okapi/cronjobs.php"); |
|
| 1372 | + require_once($GLOBALS['rootpath'] . "okapi/cronjobs.php"); |
|
| 1373 | 1373 | $nearest_event = CronJobController::run_jobs('cron-5'); |
| 1374 | 1374 | Okapi::set_var("cron_nearest_event", $nearest_event); |
| 1375 | 1375 | } |
@@ -1485,9 +1485,9 @@ discard block |
||
| 1485 | 1485 | $chars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; |
| 1486 | 1486 | $max = strlen($chars); |
| 1487 | 1487 | $key = ""; |
| 1488 | - for ($i=0; $i<$length; $i++) |
|
| 1488 | + for ($i = 0; $i < $length; $i++) |
|
| 1489 | 1489 | { |
| 1490 | - $key .= $chars[rand(0, $max-1)]; |
|
| 1490 | + $key .= $chars[rand(0, $max - 1)]; |
|
| 1491 | 1491 | } |
| 1492 | 1492 | return $key; |
| 1493 | 1493 | } |
@@ -1499,7 +1499,7 @@ discard block |
||
| 1499 | 1499 | */ |
| 1500 | 1500 | public static function register_new_consumer($appname, $appurl, $email) |
| 1501 | 1501 | { |
| 1502 | - require_once($GLOBALS['rootpath']."okapi/service_runner.php"); |
|
| 1502 | + require_once($GLOBALS['rootpath'] . "okapi/service_runner.php"); |
|
| 1503 | 1503 | $consumer = new OkapiConsumer(Okapi::generate_key(20), Okapi::generate_key(40), |
| 1504 | 1504 | $appname, $appurl, $email); |
| 1505 | 1505 | $sample_cache = OkapiServiceRunner::call("services/caches/search/all", |
@@ -1517,7 +1517,7 @@ discard block |
||
| 1517 | 1517 | print "Note: Consumer Secret is needed only when you intend to use OAuth.\n"; |
| 1518 | 1518 | print "You don't need Consumer Secret for Level 1 Authentication.\n\n"; |
| 1519 | 1519 | print "Now you can easily access Level 1 OKAPI methods. E.g.:\n"; |
| 1520 | - print Settings::get('SITE_URL')."okapi/services/caches/geocache?cache_code=$sample_cache_code&consumer_key=$consumer->key\n\n"; |
|
| 1520 | + print Settings::get('SITE_URL') . "okapi/services/caches/geocache?cache_code=$sample_cache_code&consumer_key=$consumer->key\n\n"; |
|
| 1521 | 1521 | print "If you plan on using OKAPI for a longer time, then you may want to\n"; |
| 1522 | 1522 | print "subscribe to the OKAPI News blog to stay up-to-date:\n"; |
| 1523 | 1523 | print "http://opencaching-api.blogspot.com/\n\n"; |
@@ -1538,11 +1538,11 @@ discard block |
||
| 1538 | 1538 | Db::execute(" |
| 1539 | 1539 | insert into okapi_consumers (`key`, name, secret, url, email, date_created) |
| 1540 | 1540 | values ( |
| 1541 | - '".Db::escape_string($consumer->key)."', |
|
| 1542 | - '".Db::escape_string($consumer->name)."', |
|
| 1543 | - '".Db::escape_string($consumer->secret)."', |
|
| 1544 | - '".Db::escape_string($consumer->url)."', |
|
| 1545 | - '".Db::escape_string($consumer->email)."', |
|
| 1541 | + '".Db::escape_string($consumer->key) . "', |
|
| 1542 | + '".Db::escape_string($consumer->name) . "', |
|
| 1543 | + '".Db::escape_string($consumer->secret) . "', |
|
| 1544 | + '".Db::escape_string($consumer->url) . "', |
|
| 1545 | + '".Db::escape_string($consumer->email) . "', |
|
| 1546 | 1546 | now() |
| 1547 | 1547 | ); |
| 1548 | 1548 | "); |
@@ -1551,13 +1551,13 @@ discard block |
||
| 1551 | 1551 | /** Return the distance between two geopoints, in meters. */ |
| 1552 | 1552 | public static function get_distance($lat1, $lon1, $lat2, $lon2) |
| 1553 | 1553 | { |
| 1554 | - $x1 = (90-$lat1) * 3.14159 / 180; |
|
| 1555 | - $x2 = (90-$lat2) * 3.14159 / 180; |
|
| 1554 | + $x1 = (90 - $lat1) * 3.14159 / 180; |
|
| 1555 | + $x2 = (90 - $lat2) * 3.14159 / 180; |
|
| 1556 | 1556 | # |
| 1557 | 1557 | # min(1, ...) was added below to prevent getting values greater than 1 due |
| 1558 | 1558 | # to floating point precision limits. See issue #351 for details. |
| 1559 | 1559 | # |
| 1560 | - $d = acos(min(1, cos($x1) * cos($x2) + sin($x1) * sin($x2) * cos(($lon1-$lon2) * 3.14159 / 180))) * 6371000; |
|
| 1560 | + $d = acos(min(1, cos($x1) * cos($x2) + sin($x1) * sin($x2) * cos(($lon1 - $lon2) * 3.14159 / 180))) * 6371000; |
|
| 1561 | 1561 | if ($d < 0) $d = 0; |
| 1562 | 1562 | return $d; |
| 1563 | 1563 | } |
@@ -1595,7 +1595,7 @@ discard block |
||
| 1595 | 1595 | $bearing = atan2(sin($delta_lon) * cos($rad_lat2), |
| 1596 | 1596 | cos($rad_lat1) * sin($rad_lat2) - sin($rad_lat1) * cos($rad_lat2) * cos($delta_lon)); |
| 1597 | 1597 | $bearing = 180.0 * $bearing / 3.14159; |
| 1598 | - if ( $bearing < 0.0 ) $bearing = $bearing + 360.0; |
|
| 1598 | + if ($bearing < 0.0) $bearing = $bearing + 360.0; |
|
| 1599 | 1599 | |
| 1600 | 1600 | return $bearing; |
| 1601 | 1601 | } |
@@ -1662,7 +1662,7 @@ discard block |
||
| 1662 | 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_]*\$/)."); |
| 1663 | 1663 | $response = new OkapiHttpResponse(); |
| 1664 | 1664 | $response->content_type = "application/javascript; charset=utf-8"; |
| 1665 | - $response->body = $callback."(".json_encode($object).");"; |
|
| 1665 | + $response->body = $callback . "(" . json_encode($object) . ");"; |
|
| 1666 | 1666 | return $response; |
| 1667 | 1667 | } |
| 1668 | 1668 | elseif ($format == 'xmlmap') |
@@ -1732,7 +1732,7 @@ discard block |
||
| 1732 | 1732 | $chunks[] = "<dict>"; |
| 1733 | 1733 | foreach ($obj as $key => &$item_ref) |
| 1734 | 1734 | { |
| 1735 | - $chunks[] = "<item key=\"".self::xmlescape($key)."\">"; |
|
| 1735 | + $chunks[] = "<item key=\"" . self::xmlescape($key) . "\">"; |
|
| 1736 | 1736 | self::_xmlmap_add($chunks, $item_ref); |
| 1737 | 1737 | $chunks[] = "</item>"; |
| 1738 | 1738 | } |
@@ -1742,13 +1742,13 @@ discard block |
||
| 1742 | 1742 | else |
| 1743 | 1743 | { |
| 1744 | 1744 | # That's a bug. |
| 1745 | - throw new Exception("Cannot encode as xmlmap: " + print_r($obj, true)); |
|
| 1745 | + throw new Exception("Cannot encode as xmlmap: " +print_r($obj, true)); |
|
| 1746 | 1746 | } |
| 1747 | 1747 | } |
| 1748 | 1748 | |
| 1749 | 1749 | private static function _xmlmap2_add(&$chunks, &$obj, $key) |
| 1750 | 1750 | { |
| 1751 | - $attrs = ($key !== null) ? " key=\"".self::xmlescape($key)."\"" : ""; |
|
| 1751 | + $attrs = ($key !== null) ? " key=\"" . self::xmlescape($key) . "\"" : ""; |
|
| 1752 | 1752 | if (is_string($obj)) |
| 1753 | 1753 | { |
| 1754 | 1754 | $chunks[] = "<string$attrs>"; |
@@ -1853,8 +1853,8 @@ discard block |
||
| 1853 | 1853 | $ref = &self::$cache_types[Settings::get('OC_BRANCH')]; |
| 1854 | 1854 | if (isset($ref[$name])) |
| 1855 | 1855 | return $ref[$name]; |
| 1856 | - throw new Exception("Method cache_type_name2id called with unsupported cache ". |
|
| 1857 | - "type name '$name'. You should not allow users to submit caches ". |
|
| 1856 | + throw new Exception("Method cache_type_name2id called with unsupported cache " . |
|
| 1857 | + "type name '$name'. You should not allow users to submit caches " . |
|
| 1858 | 1858 | "of non-primary type."); |
| 1859 | 1859 | } |
| 1860 | 1860 | |
@@ -2018,7 +2018,7 @@ discard block |
||
| 2018 | 2018 | |
| 2019 | 2019 | $html = preg_replace( |
| 2020 | 2020 | "~\b(src|href)=([\"'])(?![a-z0-9_-]+:)~", |
| 2021 | - "$1=$2".Settings::get("SITE_URL"), |
|
| 2021 | + "$1=$2" . Settings::get("SITE_URL"), |
|
| 2022 | 2022 | $html |
| 2023 | 2023 | ); |
| 2024 | 2024 | |
@@ -2048,10 +2048,10 @@ discard block |
||
| 2048 | 2048 | { |
| 2049 | 2049 | $value = trim(ini_get($variable)); |
| 2050 | 2050 | if (!preg_match("/^[0-9]+[KM]?$/", $value)) |
| 2051 | - throw new Exception("Unexpected PHP setting: ".$variable. " = ".$value); |
|
| 2051 | + throw new Exception("Unexpected PHP setting: " . $variable . " = " . $value); |
|
| 2052 | 2052 | $value = str_replace('K', '*1024', $value); |
| 2053 | 2053 | $value = str_replace('M', '*1024*1024', $value); |
| 2054 | - $value = eval('return '.$value.';'); |
|
| 2054 | + $value = eval('return ' . $value . ';'); |
|
| 2055 | 2055 | return $value; |
| 2056 | 2056 | } |
| 2057 | 2057 | |
@@ -2060,8 +2060,8 @@ discard block |
||
| 2060 | 2060 | const OBJECT_TYPE_CACHE_DESCRIPTION = 2; |
| 2061 | 2061 | const OBJECT_TYPE_CACHE_IMAGE = 3; |
| 2062 | 2062 | const OBJECT_TYPE_CACHE_MP3 = 4; |
| 2063 | - const OBJECT_TYPE_CACHE_LOG = 5; # implemented |
|
| 2064 | - const OBJECT_TYPE_CACHE_LOG_IMAGE = 6; # implemented |
|
| 2063 | + const OBJECT_TYPE_CACHE_LOG = 5; # implemented |
|
| 2064 | + const OBJECT_TYPE_CACHE_LOG_IMAGE = 6; # implemented |
|
| 2065 | 2065 | const OBJECT_TYPE_CACHELIST = 7; |
| 2066 | 2066 | const OBJECT_TYPE_EMAIL = 8; |
| 2067 | 2067 | const OBJECT_TYPE_CACHE_REPORT = 9; |
@@ -2084,14 +2084,14 @@ discard block |
||
| 2084 | 2084 | { |
| 2085 | 2085 | # The current cache implementation is ALWAYS persistent, so we will |
| 2086 | 2086 | # just replace it with a big value. |
| 2087 | - $timeout = 100*365*86400; |
|
| 2087 | + $timeout = 100 * 365 * 86400; |
|
| 2088 | 2088 | } |
| 2089 | 2089 | Db::execute(" |
| 2090 | 2090 | replace into okapi_cache (`key`, value, expires) |
| 2091 | 2091 | values ( |
| 2092 | - '".Db::escape_string($key)."', |
|
| 2093 | - '".Db::escape_string(gzdeflate(serialize($value)))."', |
|
| 2094 | - date_add(now(), interval '".Db::escape_string($timeout)."' second) |
|
| 2092 | + '".Db::escape_string($key) . "', |
|
| 2093 | + '".Db::escape_string(gzdeflate(serialize($value))) . "', |
|
| 2094 | + date_add(now(), interval '".Db::escape_string($timeout) . "' second) |
|
| 2095 | 2095 | ); |
| 2096 | 2096 | "); |
| 2097 | 2097 | } |
@@ -2105,8 +2105,8 @@ discard block |
||
| 2105 | 2105 | Db::execute(" |
| 2106 | 2106 | replace into okapi_cache (`key`, value, expires, score) |
| 2107 | 2107 | values ( |
| 2108 | - '".Db::escape_string($key)."', |
|
| 2109 | - '".Db::escape_string(gzdeflate(serialize($value)))."', |
|
| 2108 | + '".Db::escape_string($key) . "', |
|
| 2109 | + '".Db::escape_string(gzdeflate(serialize($value))) . "', |
|
| 2110 | 2110 | date_add(now(), interval 120 day), |
| 2111 | 2111 | 1.0 |
| 2112 | 2112 | ); |
@@ -2122,20 +2122,20 @@ discard block |
||
| 2122 | 2122 | { |
| 2123 | 2123 | # The current cache implementation is ALWAYS persistent, so we will |
| 2124 | 2124 | # just replace it with a big value. |
| 2125 | - $timeout = 100*365*86400; |
|
| 2125 | + $timeout = 100 * 365 * 86400; |
|
| 2126 | 2126 | } |
| 2127 | 2127 | $entries_escaped = array(); |
| 2128 | 2128 | foreach ($dict as $key => $value) |
| 2129 | 2129 | { |
| 2130 | 2130 | $entries_escaped[] = "( |
| 2131 | - '".Db::escape_string($key)."', |
|
| 2132 | - '".Db::escape_string(gzdeflate(serialize($value)))."', |
|
| 2133 | - date_add(now(), interval '".Db::escape_string($timeout)."' second) |
|
| 2131 | + '".Db::escape_string($key) . "', |
|
| 2132 | + '".Db::escape_string(gzdeflate(serialize($value))) . "', |
|
| 2133 | + date_add(now(), interval '".Db::escape_string($timeout) . "' second) |
|
| 2134 | 2134 | )"; |
| 2135 | 2135 | } |
| 2136 | 2136 | Db::execute(" |
| 2137 | 2137 | replace into okapi_cache (`key`, value, expires) |
| 2138 | - values ".implode(", ", $entries_escaped)." |
|
| 2138 | + values ".implode(", ", $entries_escaped) . " |
|
| 2139 | 2139 | "); |
| 2140 | 2140 | } |
| 2141 | 2141 | |
@@ -2149,7 +2149,7 @@ discard block |
||
| 2149 | 2149 | select value, score |
| 2150 | 2150 | from okapi_cache |
| 2151 | 2151 | where |
| 2152 | - `key` = '".Db::escape_string($key)."' |
|
| 2152 | + `key` = '".Db::escape_string($key) . "' |
|
| 2153 | 2153 | and expires > now() |
| 2154 | 2154 | "); |
| 2155 | 2155 | list($blob, $score) = Db::fetch_row($rs); |
@@ -2159,7 +2159,7 @@ discard block |
||
| 2159 | 2159 | { |
| 2160 | 2160 | Db::execute(" |
| 2161 | 2161 | insert into okapi_cache_reads (`cache_key`) |
| 2162 | - values ('".Db::escape_string($key)."') |
|
| 2162 | + values ('".Db::escape_string($key) . "') |
|
| 2163 | 2163 | "); |
| 2164 | 2164 | } |
| 2165 | 2165 | return unserialize(gzinflate($blob)); |
@@ -2173,7 +2173,7 @@ discard block |
||
| 2173 | 2173 | select `key`, value |
| 2174 | 2174 | from okapi_cache |
| 2175 | 2175 | where |
| 2176 | - `key` in ('".implode("','", array_map('\okapi\Db::escape_string', $keys))."') |
|
| 2176 | + `key` in ('".implode("','", array_map('\okapi\Db::escape_string', $keys)) . "') |
|
| 2177 | 2177 | and expires > now() |
| 2178 | 2178 | "); |
| 2179 | 2179 | while ($row = Db::fetch_assoc($rs)) |
@@ -2186,10 +2186,10 @@ discard block |
||
| 2186 | 2186 | { |
| 2187 | 2187 | unset($dict[$row['key']]); |
| 2188 | 2188 | Okapi::mail_admins("Debug: Unserialize error", |
| 2189 | - "Could not unserialize key '".$row['key']."' from Cache.\n". |
|
| 2190 | - "Probably something REALLY big was put there and data has been truncated.\n". |
|
| 2191 | - "Consider upgrading cache table to LONGBLOB.\n\n". |
|
| 2192 | - "Length of data, compressed: ".strlen($row['value'])); |
|
| 2189 | + "Could not unserialize key '" . $row['key'] . "' from Cache.\n" . |
|
| 2190 | + "Probably something REALLY big was put there and data has been truncated.\n" . |
|
| 2191 | + "Consider upgrading cache table to LONGBLOB.\n\n" . |
|
| 2192 | + "Length of data, compressed: " . strlen($row['value'])); |
|
| 2193 | 2193 | } |
| 2194 | 2194 | } |
| 2195 | 2195 | if (count($dict) < count($keys)) |
@@ -2214,7 +2214,7 @@ discard block |
||
| 2214 | 2214 | return; |
| 2215 | 2215 | Db::execute(" |
| 2216 | 2216 | delete from okapi_cache |
| 2217 | - where `key` in ('".implode("','", array_map('\okapi\Db::escape_string', $keys))."') |
|
| 2217 | + where `key` in ('".implode("','", array_map('\okapi\Db::escape_string', $keys)) . "') |
|
| 2218 | 2218 | "); |
| 2219 | 2219 | } |
| 2220 | 2220 | } |
@@ -2228,7 +2228,7 @@ discard block |
||
| 2228 | 2228 | { |
| 2229 | 2229 | public static function get_file_path($key) |
| 2230 | 2230 | { |
| 2231 | - $filename = Okapi::get_var_dir()."/okapi_filecache_".md5($key); |
|
| 2231 | + $filename = Okapi::get_var_dir() . "/okapi_filecache_" . md5($key); |
|
| 2232 | 2232 | if (!file_exists($filename)) |
| 2233 | 2233 | return null; |
| 2234 | 2234 | return $filename; |
@@ -2241,7 +2241,7 @@ discard block |
||
| 2241 | 2241 | */ |
| 2242 | 2242 | public static function set($key, $value) |
| 2243 | 2243 | { |
| 2244 | - $filename = Okapi::get_var_dir()."/okapi_filecache_".md5($key); |
|
| 2244 | + $filename = Okapi::get_var_dir() . "/okapi_filecache_" . md5($key); |
|
| 2245 | 2245 | file_put_contents($filename, $value); |
| 2246 | 2246 | return $filename; |
| 2247 | 2247 | } |
@@ -2262,7 +2262,7 @@ discard block |
||
| 2262 | 2262 | { |
| 2263 | 2263 | public $consumer; |
| 2264 | 2264 | public $token; |
| 2265 | - public $etag; # see: http://en.wikipedia.org/wiki/HTTP_ETag |
|
| 2265 | + public $etag; # see: http://en.wikipedia.org/wiki/HTTP_ETag |
|
| 2266 | 2266 | |
| 2267 | 2267 | /** |
| 2268 | 2268 | * Set this to true, for some method to allow you to set higher "limit" |
@@ -2386,7 +2386,7 @@ discard block |
||
| 2386 | 2386 | |
| 2387 | 2387 | if (!Settings::get('DEBUG')) |
| 2388 | 2388 | { |
| 2389 | - throw new Exception("Attempted to use DEBUG_AS_USERNAME in ". |
|
| 2389 | + throw new Exception("Attempted to use DEBUG_AS_USERNAME in " . |
|
| 2390 | 2390 | "non-debug environment. Accidental commit?"); |
| 2391 | 2391 | } |
| 2392 | 2392 | |
@@ -2417,12 +2417,12 @@ discard block |
||
| 2417 | 2417 | list($this->consumer, $this->token) = Okapi::$server-> |
| 2418 | 2418 | verify_request2($this->request, $this->opt_token_type, $this->opt_min_auth_level == 3); |
| 2419 | 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' ". |
|
| 2421 | - "and 'oauth_consumer_key' parameters (Level 1 and Level 2), but they do not match with ". |
|
| 2420 | + throw new BadRequest("Inproper mixing of authentication types. You used both 'consumer_key' " . |
|
| 2421 | + "and 'oauth_consumer_key' parameters (Level 1 and Level 2), but they do not match with " . |
|
| 2422 | 2422 | "each other. Were you trying to hack me? ;)"); |
| 2423 | 2423 | if ($this->opt_min_auth_level == 3 && !$this->token) |
| 2424 | 2424 | { |
| 2425 | - throw new BadRequest("This method requires a valid Token to be included (Level 3 ". |
|
| 2425 | + throw new BadRequest("This method requires a valid Token to be included (Level 3 " . |
|
| 2426 | 2426 | "Authentication). You didn't provide one."); |
| 2427 | 2427 | } |
| 2428 | 2428 | } |
@@ -2430,8 +2430,8 @@ discard block |
||
| 2430 | 2430 | { |
| 2431 | 2431 | if ($this->opt_min_auth_level >= 2) |
| 2432 | 2432 | { |
| 2433 | - throw new BadRequest("This method requires OAuth signature (Level ". |
|
| 2434 | - $this->opt_min_auth_level." Authentication). You didn't sign your request."); |
|
| 2433 | + throw new BadRequest("This method requires OAuth signature (Level " . |
|
| 2434 | + $this->opt_min_auth_level . " Authentication). You didn't sign your request."); |
|
| 2435 | 2435 | } |
| 2436 | 2436 | else |
| 2437 | 2437 | { |
@@ -2444,7 +2444,7 @@ discard block |
||
| 2444 | 2444 | } |
| 2445 | 2445 | } |
| 2446 | 2446 | if (($this->opt_min_auth_level == 1) && (!$this->consumer)) |
| 2447 | - throw new BadRequest("This method requires the 'consumer_key' argument (Level 1 ". |
|
| 2447 | + throw new BadRequest("This method requires the 'consumer_key' argument (Level 1 " . |
|
| 2448 | 2448 | "Authentication). You didn't provide one."); |
| 2449 | 2449 | } |
| 2450 | 2450 | } |
@@ -2477,10 +2477,10 @@ discard block |
||
| 2477 | 2477 | # Note, that this will override any other valid authentication the |
| 2478 | 2478 | # developer might have issued. |
| 2479 | 2479 | |
| 2480 | - $debug_user_id = Db::select_value("select user_id from user where username='". |
|
| 2481 | - Db::escape_string($options['DEBUG_AS_USERNAME'])."'"); |
|
| 2480 | + $debug_user_id = Db::select_value("select user_id from user where username='" . |
|
| 2481 | + Db::escape_string($options['DEBUG_AS_USERNAME']) . "'"); |
|
| 2482 | 2482 | if ($debug_user_id == null) |
| 2483 | - throw new Exception("Invalid user name in DEBUG_AS_USERNAME: '".$options['DEBUG_AS_USERNAME']."'"); |
|
| 2483 | + throw new Exception("Invalid user name in DEBUG_AS_USERNAME: '" . $options['DEBUG_AS_USERNAME'] . "'"); |
|
| 2484 | 2484 | $this->consumer = new OkapiDebugConsumer(); |
| 2485 | 2485 | $this->token = new OkapiDebugAccessToken($debug_user_id); |
| 2486 | 2486 | } |
@@ -2516,9 +2516,9 @@ discard block |
||
| 2516 | 2516 | } |
| 2517 | 2517 | if (!$allowed) { |
| 2518 | 2518 | throw new BadRequest( |
| 2519 | - "Unrecognized base URL prefix! See `okapi_base_urls` field ". |
|
| 2520 | - "in the `services/apisrv/installation` method. (Recommended ". |
|
| 2521 | - "base URL to use is '".Okapi::get_recommended_base_url()."'.)" |
|
| 2519 | + "Unrecognized base URL prefix! See `okapi_base_urls` field " . |
|
| 2520 | + "in the `services/apisrv/installation` method. (Recommended " . |
|
| 2521 | + "base URL to use is '" . Okapi::get_recommended_base_url() . "'.)" |
|
| 2522 | 2522 | ); |
| 2523 | 2523 | } |
| 2524 | 2524 | } |
@@ -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 | |
@@ -64,6 +64,7 @@ discard block |
||
| 64 | 64 | /** |
| 65 | 65 | * Execute all scheduled cronjobs of given type, reschedule, and return |
| 66 | 66 | * UNIX timestamp of the nearest scheduled event. |
| 67 | + * @param string $type |
|
| 67 | 68 | */ |
| 68 | 69 | public static function run_jobs($type) |
| 69 | 70 | { |
@@ -127,6 +128,7 @@ discard block |
||
| 127 | 128 | /** |
| 128 | 129 | * Force a specified cronjob to run. Throw an exception if cronjob not found. |
| 129 | 130 | * $job_name mast equal one of the names returned by ->get_name() method. |
| 131 | + * @param string $job_name |
|
| 130 | 132 | */ |
| 131 | 133 | public static function force_run($job_name) |
| 132 | 134 | { |
@@ -146,6 +148,7 @@ discard block |
||
| 146 | 148 | /** |
| 147 | 149 | * Reset the schedule of a specified cronjob. This will force the job to |
| 148 | 150 | * run on nearest occasion (but not NOW). |
| 151 | + * @param string $job_name |
|
| 149 | 152 | */ |
| 150 | 153 | public static function reset_job_schedule($job_name) |
| 151 | 154 | { |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | ); |
| 57 | 57 | foreach ($cache as $cronjob) |
| 58 | 58 | if (!in_array($cronjob->get_type(), array('pre-request', 'cron-5'))) |
| 59 | - throw new Exception("Cronjob '".$cronjob->get_name()."' has an invalid (unsupported) type."); |
|
| 59 | + throw new Exception("Cronjob '" . $cronjob->get_name() . "' has an invalid (unsupported) type."); |
|
| 60 | 60 | } |
| 61 | 61 | return $cache; |
| 62 | 62 | } |
@@ -67,10 +67,10 @@ discard block |
||
| 67 | 67 | */ |
| 68 | 68 | public static function run_jobs($type) |
| 69 | 69 | { |
| 70 | - require_once($GLOBALS['rootpath'].'okapi/service_runner.php'); |
|
| 70 | + require_once($GLOBALS['rootpath'] . 'okapi/service_runner.php'); |
|
| 71 | 71 | |
| 72 | 72 | # We don't want other cronjobs of the same time to run simultanously. |
| 73 | - $lock = OkapiLock::get('cronjobs-'.$type); |
|
| 73 | + $lock = OkapiLock::get('cronjobs-' . $type); |
|
| 74 | 74 | $lock->acquire(); |
| 75 | 75 | |
| 76 | 76 | $schedule = Cache::get("cron_schedule"); |
@@ -93,13 +93,13 @@ discard block |
||
| 93 | 93 | } |
| 94 | 94 | catch (Exception $e) |
| 95 | 95 | { |
| 96 | - Okapi::mail_admins("Cronjob error: ".$cronjob->get_name(), |
|
| 96 | + Okapi::mail_admins("Cronjob error: " . $cronjob->get_name(), |
|
| 97 | 97 | OkapiExceptionHandler::get_exception_info($e)); |
| 98 | 98 | } |
| 99 | 99 | $next_run = $cronjob->get_next_scheduled_run(isset($schedule[$name]) ? $schedule[$name] : time()); |
| 100 | 100 | } |
| 101 | 101 | $schedule[$name] = $next_run; |
| 102 | - Cache::set("cron_schedule", $schedule, 30*86400); |
|
| 102 | + Cache::set("cron_schedule", $schedule, 30 * 86400); |
|
| 103 | 103 | } |
| 104 | 104 | } |
| 105 | 105 | |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | foreach ($fixed_schedule as $name => $time) |
| 120 | 120 | if ($time < $nearest) |
| 121 | 121 | $nearest = $time; |
| 122 | - Cache::set("cron_schedule", $fixed_schedule, 30*86400); |
|
| 122 | + Cache::set("cron_schedule", $fixed_schedule, 30 * 86400); |
|
| 123 | 123 | $lock->release(); |
| 124 | 124 | return $nearest; |
| 125 | 125 | } |
@@ -130,11 +130,11 @@ discard block |
||
| 130 | 130 | */ |
| 131 | 131 | public static function force_run($job_name) |
| 132 | 132 | { |
| 133 | - require_once($GLOBALS['rootpath'].'okapi/service_runner.php'); |
|
| 133 | + require_once($GLOBALS['rootpath'] . 'okapi/service_runner.php'); |
|
| 134 | 134 | |
| 135 | 135 | foreach (self::get_enabled_cronjobs() as $cronjob) |
| 136 | 136 | { |
| 137 | - if (($cronjob->get_name() == $job_name) || ($cronjob->get_name() == "okapi\\cronjobs\\".$job_name)) |
|
| 137 | + if (($cronjob->get_name() == $job_name) || ($cronjob->get_name() == "okapi\\cronjobs\\" . $job_name)) |
|
| 138 | 138 | { |
| 139 | 139 | $cronjob->execute(); |
| 140 | 140 | return; |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | { |
| 152 | 152 | $thejob = null; |
| 153 | 153 | foreach (self::get_enabled_cronjobs() as $tmp) |
| 154 | - if (($tmp->get_name() == $job_name) || ($tmp->get_name() == "okapi\\cronjobs\\".$job_name)) |
|
| 154 | + if (($tmp->get_name() == $job_name) || ($tmp->get_name() == "okapi\\cronjobs\\" . $job_name)) |
|
| 155 | 155 | $thejob = $tmp; |
| 156 | 156 | if ($thejob == null) |
| 157 | 157 | throw new Exception("Could not reset schedule for job $job_name. $jon_name not found."); |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | # currently being run. |
| 161 | 161 | |
| 162 | 162 | $type = $thejob->get_type(); |
| 163 | - $lock = OkapiLock::get('cronjobs-'.$type); |
|
| 163 | + $lock = OkapiLock::get('cronjobs-' . $type); |
|
| 164 | 164 | $lock->acquire(); |
| 165 | 165 | |
| 166 | 166 | $schedule = Cache::get("cron_schedule"); |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | { |
| 169 | 169 | if (isset($schedule[$thejob->get_name()])) |
| 170 | 170 | unset($schedule[$thejob->get_name()]); |
| 171 | - Cache::set("cron_schedule", $schedule, 30*86400); |
|
| 171 | + Cache::set("cron_schedule", $schedule, 30 * 86400); |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | $lock->release(); |
@@ -298,8 +298,8 @@ discard block |
||
| 298 | 298 | |
| 299 | 299 | # Update the "score" stats. |
| 300 | 300 | |
| 301 | - $multiplier = 0.9; # Every hour, all scores are multiplied by this. |
|
| 302 | - $limit = 0.01; # When a score reaches this limit, the entry is deleted. |
|
| 301 | + $multiplier = 0.9; # Every hour, all scores are multiplied by this. |
|
| 302 | + $limit = 0.01; # When a score reaches this limit, the entry is deleted. |
|
| 303 | 303 | |
| 304 | 304 | # Every time the entry is read, its score is incread by 1. If an entry |
| 305 | 305 | # is saved, but never read, it will be deleted after log(L,M) hours |
@@ -309,7 +309,7 @@ discard block |
||
| 309 | 309 | |
| 310 | 310 | Db::execute(" |
| 311 | 311 | update okapi_cache |
| 312 | - set score = score * '".Db::escape_string($multiplier)."' |
|
| 312 | + set score = score * '".Db::escape_string($multiplier) . "' |
|
| 313 | 313 | where score is not null |
| 314 | 314 | "); |
| 315 | 315 | Db::execute(" |
@@ -334,7 +334,7 @@ discard block |
||
| 334 | 334 | delete from okapi_cache |
| 335 | 335 | where |
| 336 | 336 | score is not null |
| 337 | - and score < '".Db::escape_string($limit)."' |
|
| 337 | + and score < '".Db::escape_string($limit) . "' |
|
| 338 | 338 | "); |
| 339 | 339 | Db::query("optimize table okapi_cache"); |
| 340 | 340 | |
@@ -383,28 +383,28 @@ discard block |
||
| 383 | 383 | insert into okapi_stats_hourly (consumer_key, user_id, period_start, service_name, |
| 384 | 384 | total_calls, http_calls, total_runtime, http_runtime) |
| 385 | 385 | values ( |
| 386 | - '".Db::escape_string($row['consumer_key'])."', |
|
| 387 | - '".Db::escape_string($row['user_id'])."', |
|
| 388 | - '".Db::escape_string($row['period_start'])."', |
|
| 389 | - '".Db::escape_string($row['service_name'])."', |
|
| 390 | - '".Db::escape_string($row['calls'])."', |
|
| 391 | - '".Db::escape_string(($row['calltype'] == 'http') ? $row['calls'] : 0)."', |
|
| 392 | - '".Db::escape_string($row['runtime'])."', |
|
| 393 | - '".Db::escape_string(($row['calltype'] == 'http') ? $row['runtime'] : 0)."' |
|
| 386 | + '".Db::escape_string($row['consumer_key']) . "', |
|
| 387 | + '".Db::escape_string($row['user_id']) . "', |
|
| 388 | + '".Db::escape_string($row['period_start']) . "', |
|
| 389 | + '".Db::escape_string($row['service_name']) . "', |
|
| 390 | + '".Db::escape_string($row['calls']) . "', |
|
| 391 | + '".Db::escape_string(($row['calltype'] == 'http') ? $row['calls'] : 0) . "', |
|
| 392 | + '".Db::escape_string($row['runtime']) . "', |
|
| 393 | + '".Db::escape_string(($row['calltype'] == 'http') ? $row['runtime'] : 0) . "' |
|
| 394 | 394 | ) |
| 395 | 395 | on duplicate key update |
| 396 | 396 | ".(($row['calltype'] == 'http') ? " |
| 397 | - http_calls = http_calls + '".Db::escape_string($row['calls'])."', |
|
| 398 | - http_runtime = http_runtime + '".Db::escape_string($row['runtime'])."', |
|
| 399 | - " : "")." |
|
| 400 | - total_calls = total_calls + '".Db::escape_string($row['calls'])."', |
|
| 401 | - total_runtime = total_runtime + '".Db::escape_string($row['runtime'])."' |
|
| 397 | + http_calls = http_calls + '".Db::escape_string($row['calls']) . "', |
|
| 398 | + http_runtime = http_runtime + '".Db::escape_string($row['runtime']) . "', |
|
| 399 | + " : "") . " |
|
| 400 | + total_calls = total_calls + '".Db::escape_string($row['calls']) . "', |
|
| 401 | + total_runtime = total_runtime + '".Db::escape_string($row['runtime']) . "' |
|
| 402 | 402 | "); |
| 403 | 403 | } |
| 404 | 404 | Db::execute("delete from okapi_stats_temp;"); |
| 405 | 405 | Db::execute("unlock tables;"); |
| 406 | 406 | } catch (Exception $e) { |
| 407 | - Db::execute("unlock tables;"); // No "finally" in PHP 5.3 |
|
| 407 | + Db::execute("unlock tables;"); // No "finally" in PHP 5.3 |
|
| 408 | 408 | throw $e; |
| 409 | 409 | } |
| 410 | 410 | } |
@@ -463,7 +463,7 @@ discard block |
||
| 463 | 463 | and m.user_id = h.user_id |
| 464 | 464 | and substr(m.period_start, 1, 7) = substr(h.period_start, 1, 7) |
| 465 | 465 | and m.service_name = h.service_name |
| 466 | - where substr(h.period_start, 1, 7) = '".Db::escape_string($month)."' |
|
| 466 | + where substr(h.period_start, 1, 7) = '".Db::escape_string($month) . "' |
|
| 467 | 467 | group by substr(h.period_start, 1, 7), h.consumer_key, h.user_id, h.service_name; |
| 468 | 468 | "); |
| 469 | 469 | |
@@ -471,13 +471,13 @@ discard block |
||
| 471 | 471 | |
| 472 | 472 | Db::execute(" |
| 473 | 473 | delete from okapi_stats_hourly |
| 474 | - where substr(period_start, 1, 7) = '".Db::escape_string($month)."' |
|
| 474 | + where substr(period_start, 1, 7) = '".Db::escape_string($month) . "' |
|
| 475 | 475 | "); |
| 476 | 476 | } |
| 477 | 477 | |
| 478 | 478 | Db::execute("unlock tables;"); |
| 479 | 479 | } catch (Exception $e) { |
| 480 | - Db::execute("unlock tables;"); // No "finally" in PHP 5.3 |
|
| 480 | + Db::execute("unlock tables;"); // No "finally" in PHP 5.3 |
|
| 481 | 481 | throw $e; |
| 482 | 482 | } |
| 483 | 483 | } |
@@ -531,12 +531,12 @@ discard block |
||
| 531 | 531 | { |
| 532 | 532 | Okapi::mail_admins( |
| 533 | 533 | "Crontab not working.", |
| 534 | - "Hello. OKAPI detected, that it's crontab is not working properly.\n". |
|
| 535 | - "Please check your configuration or contact OKAPI developers.\n\n". |
|
| 536 | - "This line should be present among your crontab entries:\n\n". |
|
| 537 | - "*/5 * * * * wget -O - -q -t 1 ".Settings::get('SITE_URL')."okapi/cron5\n\n". |
|
| 538 | - "If you're receiving this in Virtual Machine development environment, then\n". |
|
| 539 | - "ignore it. Probably you just paused (or switched off) your VM for some time\n". |
|
| 534 | + "Hello. OKAPI detected, that it's crontab is not working properly.\n" . |
|
| 535 | + "Please check your configuration or contact OKAPI developers.\n\n" . |
|
| 536 | + "This line should be present among your crontab entries:\n\n" . |
|
| 537 | + "*/5 * * * * wget -O - -q -t 1 " . Settings::get('SITE_URL') . "okapi/cron5\n\n" . |
|
| 538 | + "If you're receiving this in Virtual Machine development environment, then\n" . |
|
| 539 | + "ignore it. Probably you just paused (or switched off) your VM for some time\n" . |
|
| 540 | 540 | "(which would be considered an error in production environment)." |
| 541 | 541 | ); |
| 542 | 542 | |
@@ -557,7 +557,7 @@ discard block |
||
| 557 | 557 | public function get_period() { return 300; } |
| 558 | 558 | public function execute() |
| 559 | 559 | { |
| 560 | - require_once($GLOBALS['rootpath']."okapi/services/replicate/replicate_common.inc.php"); |
|
| 560 | + require_once($GLOBALS['rootpath'] . "okapi/services/replicate/replicate_common.inc.php"); |
|
| 561 | 561 | ReplicateCommon::update_clog_table(); |
| 562 | 562 | } |
| 563 | 563 | } |
@@ -573,7 +573,7 @@ discard block |
||
| 573 | 573 | public function get_period() { return 86400; } |
| 574 | 574 | public function execute() |
| 575 | 575 | { |
| 576 | - require_once($GLOBALS['rootpath']."okapi/services/replicate/replicate_common.inc.php"); |
|
| 576 | + require_once($GLOBALS['rootpath'] . "okapi/services/replicate/replicate_common.inc.php"); |
|
| 577 | 577 | ReplicateCommon::verify_clog_consistency(); |
| 578 | 578 | } |
| 579 | 579 | } |
@@ -583,10 +583,10 @@ discard block |
||
| 583 | 583 | */ |
| 584 | 584 | class FulldumpGeneratorJob extends Cron5Job |
| 585 | 585 | { |
| 586 | - public function get_period() { return 7*86400; } |
|
| 586 | + public function get_period() { return 7 * 86400; } |
|
| 587 | 587 | public function execute() |
| 588 | 588 | { |
| 589 | - require_once($GLOBALS['rootpath']."okapi/services/replicate/replicate_common.inc.php"); |
|
| 589 | + require_once($GLOBALS['rootpath'] . "okapi/services/replicate/replicate_common.inc.php"); |
|
| 590 | 590 | ReplicateCommon::generate_fulldump(); |
| 591 | 591 | } |
| 592 | 592 | } |
@@ -596,7 +596,7 @@ discard block |
||
| 596 | 596 | */ |
| 597 | 597 | class TileTreeUpdater extends Cron5Job |
| 598 | 598 | { |
| 599 | - public function get_period() { return 5*60; } |
|
| 599 | + public function get_period() { return 5 * 60; } |
|
| 600 | 600 | public function execute() |
| 601 | 601 | { |
| 602 | 602 | $current_clog_revision = Okapi::get_var('clog_revision', 0); |
@@ -604,10 +604,10 @@ discard block |
||
| 604 | 604 | if ($tiletree_revision === $current_clog_revision) { |
| 605 | 605 | # No update necessary. |
| 606 | 606 | } elseif ($tiletree_revision < $current_clog_revision) { |
| 607 | - require_once($GLOBALS['rootpath']."okapi/services/caches/map/replicate_listener.inc.php"); |
|
| 607 | + require_once($GLOBALS['rootpath'] . "okapi/services/caches/map/replicate_listener.inc.php"); |
|
| 608 | 608 | if ($current_clog_revision - $tiletree_revision < 30000) # In the middle of 2012, OCPL generated 30000 entries per week |
| 609 | 609 | { |
| 610 | - for ($timeout = time() + 240; time() < $timeout; ) # Try to stop after 4 minutes. |
|
| 610 | + for ($timeout = time() + 240; time() < $timeout;) # Try to stop after 4 minutes. |
|
| 611 | 611 | { |
| 612 | 612 | try { |
| 613 | 613 | $response = OkapiServiceRunner::call('services/replicate/changelog', new OkapiInternalRequest( |
@@ -643,7 +643,7 @@ discard block |
||
| 643 | 643 | public function get_period() { return 86400; } |
| 644 | 644 | public function execute() |
| 645 | 645 | { |
| 646 | - require_once($GLOBALS['rootpath']."okapi/services/replicate/replicate_common.inc.php"); |
|
| 646 | + require_once($GLOBALS['rootpath'] . "okapi/services/replicate/replicate_common.inc.php"); |
|
| 647 | 647 | $max_revision = ReplicateCommon::get_revision(); |
| 648 | 648 | $cache_key = 'clog_revisions_daily'; |
| 649 | 649 | $data = Cache::get($cache_key); |
@@ -654,16 +654,16 @@ discard block |
||
| 654 | 654 | $new_data = array(); |
| 655 | 655 | foreach ($data as $time => $r) |
| 656 | 656 | { |
| 657 | - if ($time < time() - 10*86400) |
|
| 657 | + if ($time < time() - 10 * 86400) |
|
| 658 | 658 | $new_min_revision = max($new_min_revision, $r); |
| 659 | 659 | else |
| 660 | 660 | $new_data[$time] = $r; |
| 661 | 661 | } |
| 662 | 662 | Db::execute(" |
| 663 | 663 | delete from okapi_clog |
| 664 | - where id < '".Db::escape_string($new_min_revision)."' |
|
| 664 | + where id < '".Db::escape_string($new_min_revision) . "' |
|
| 665 | 665 | "); |
| 666 | - Cache::set($cache_key, $new_data, 10*86400); |
|
| 666 | + Cache::set($cache_key, $new_data, 10 * 86400); |
|
| 667 | 667 | Db::query("optimize table okapi_clog"); |
| 668 | 668 | } |
| 669 | 669 | } |
@@ -673,7 +673,7 @@ discard block |
||
| 673 | 673 | */ |
| 674 | 674 | class AdminStatsSender extends Cron5Job |
| 675 | 675 | { |
| 676 | - public function get_period() { return 7*86400; } |
|
| 676 | + public function get_period() { return 7 * 86400; } |
|
| 677 | 677 | public function execute() |
| 678 | 678 | { |
| 679 | 679 | ob_start(); |
@@ -698,8 +698,8 @@ discard block |
||
| 698 | 698 | and s.period_start > date_add(now(), interval -7 day) |
| 699 | 699 | "); |
| 700 | 700 | print "Hello! This is your weekly summary of OKAPI usage.\n\n"; |
| 701 | - print "Apps active this week: ".$active_apps_count." out of ".$apisrv_stats['apps_count'].".\n"; |
|
| 702 | - print "Total of ".$weekly_stats['total_http_calls']." requests were made (".sprintf("%01.1f", $weekly_stats['total_http_runtime'])." seconds).\n\n"; |
|
| 701 | + print "Apps active this week: " . $active_apps_count . " out of " . $apisrv_stats['apps_count'] . ".\n"; |
|
| 702 | + print "Total of " . $weekly_stats['total_http_calls'] . " requests were made (" . sprintf("%01.1f", $weekly_stats['total_http_runtime']) . " seconds).\n\n"; |
|
| 703 | 703 | $consumers = Db::select_all(" |
| 704 | 704 | select |
| 705 | 705 | s.consumer_key, |
@@ -726,10 +726,10 @@ discard block |
||
| 726 | 726 | elseif ($row['consumer_key'] == 'facade') |
| 727 | 727 | $name = "Internal usage via Facade"; |
| 728 | 728 | if (mb_strlen($name) > 35) |
| 729 | - $name = mb_substr($name, 0, 32)."..."; |
|
| 729 | + $name = mb_substr($name, 0, 32) . "..."; |
|
| 730 | 730 | print self::mb_str_pad($name, 35, " ", STR_PAD_RIGHT); |
| 731 | 731 | print str_pad($row['http_calls'], 8, " ", STR_PAD_LEFT); |
| 732 | - print str_pad(sprintf("%01.2f", $row['http_runtime']), 11, " ", STR_PAD_LEFT)."s\n"; |
|
| 732 | + print str_pad(sprintf("%01.2f", $row['http_runtime']), 11, " ", STR_PAD_LEFT) . "s\n"; |
|
| 733 | 733 | } |
| 734 | 734 | print "\n"; |
| 735 | 735 | $methods = Db::select_all(" |
@@ -750,13 +750,13 @@ discard block |
||
| 750 | 750 | { |
| 751 | 751 | $name = $row['service_name']; |
| 752 | 752 | if (mb_strlen($name) > 35) |
| 753 | - $name = mb_substr($name, 0, 32)."..."; |
|
| 753 | + $name = mb_substr($name, 0, 32) . "..."; |
|
| 754 | 754 | print self::mb_str_pad($name, 35, " ", STR_PAD_RIGHT); |
| 755 | 755 | print str_pad($row['http_calls'], 8, " ", STR_PAD_LEFT); |
| 756 | - print str_pad(sprintf("%01.2f", $row['http_runtime']), 11, " ", STR_PAD_LEFT)."s"; |
|
| 756 | + print str_pad(sprintf("%01.2f", $row['http_runtime']), 11, " ", STR_PAD_LEFT) . "s"; |
|
| 757 | 757 | print str_pad(sprintf("%01.4f", ( |
| 758 | 758 | ($row['http_calls'] > 0) ? ($row['http_runtime'] / $row['http_calls']) : 0 |
| 759 | - )), 8, " ", STR_PAD_LEFT)."s\n"; |
|
| 759 | + )), 8, " ", STR_PAD_LEFT) . "s\n"; |
|
| 760 | 760 | } |
| 761 | 761 | print "\n"; |
| 762 | 762 | $oauth_users = Db::select_all(" |
@@ -778,9 +778,9 @@ discard block |
||
| 778 | 778 | { |
| 779 | 779 | $name = $row['name']; |
| 780 | 780 | if (mb_strlen($name) > 35) |
| 781 | - $name = mb_substr($name, 0, 32)."..."; |
|
| 781 | + $name = mb_substr($name, 0, 32) . "..."; |
|
| 782 | 782 | print self::mb_str_pad($name, 35, " ", STR_PAD_RIGHT); |
| 783 | - print str_pad($row['users'], 8, " ", STR_PAD_LEFT)."\n"; |
|
| 783 | + print str_pad($row['users'], 8, " ", STR_PAD_LEFT) . "\n"; |
|
| 784 | 784 | } |
| 785 | 785 | print "\n"; |
| 786 | 786 | |
@@ -806,10 +806,10 @@ discard block |
||
| 806 | 806 | */ |
| 807 | 807 | class LocaleChecker extends Cron5Job |
| 808 | 808 | { |
| 809 | - public function get_period() { return 7*86400; } |
|
| 809 | + public function get_period() { return 7 * 86400; } |
|
| 810 | 810 | public function execute() |
| 811 | 811 | { |
| 812 | - require_once($GLOBALS['rootpath']."okapi/locale/locales.php"); |
|
| 812 | + require_once($GLOBALS['rootpath'] . "okapi/locale/locales.php"); |
|
| 813 | 813 | $required = Locales::get_required_locales(); |
| 814 | 814 | $installed = Locales::get_installed_locales(); |
| 815 | 815 | $missing = array(); |
@@ -827,7 +827,7 @@ discard block |
||
| 827 | 827 | $prefixes = array(); |
| 828 | 828 | foreach ($missing as $locale) |
| 829 | 829 | { |
| 830 | - print " - ".$locale."\n"; |
|
| 830 | + print " - " . $locale . "\n"; |
|
| 831 | 831 | $prefixes[substr($locale, 0, 2)] = true; |
| 832 | 832 | } |
| 833 | 833 | $prefixes = array_keys($prefixes); |
@@ -842,7 +842,7 @@ discard block |
||
| 842 | 842 | foreach ($prefixes as $lang) |
| 843 | 843 | { |
| 844 | 844 | if ($lang != 'PO') # Two first letters cut from POSIX. |
| 845 | - print "sudo apt-get install language-pack-".$lang."-base\n"; |
|
| 845 | + print "sudo apt-get install language-pack-" . $lang . "-base\n"; |
|
| 846 | 846 | } |
| 847 | 847 | print "sudo service apache2 restart\n"; |
| 848 | 848 | print "\n"; |
@@ -54,9 +54,10 @@ discard block |
||
| 54 | 54 | new SearchSetsCleanerJob(), |
| 55 | 55 | new TableOptimizerJob(), |
| 56 | 56 | ); |
| 57 | - foreach ($cache as $cronjob) |
|
| 58 | - if (!in_array($cronjob->get_type(), array('pre-request', 'cron-5'))) |
|
| 57 | + foreach ($cache as $cronjob) { |
|
| 58 | + if (!in_array($cronjob->get_type(), array('pre-request', 'cron-5'))) |
|
| 59 | 59 | throw new Exception("Cronjob '".$cronjob->get_name()."' has an invalid (unsupported) type."); |
| 60 | + } |
|
| 60 | 61 | } |
| 61 | 62 | return $cache; |
| 62 | 63 | } |
@@ -74,8 +75,9 @@ discard block |
||
| 74 | 75 | $lock->acquire(); |
| 75 | 76 | |
| 76 | 77 | $schedule = Cache::get("cron_schedule"); |
| 77 | - if ($schedule == null) |
|
| 78 | - $schedule = array(); |
|
| 78 | + if ($schedule == null) { |
|
| 79 | + $schedule = array(); |
|
| 80 | + } |
|
| 79 | 81 | foreach (self::get_enabled_cronjobs() as $cronjob) |
| 80 | 82 | { |
| 81 | 83 | $name = $cronjob->get_name(); |
@@ -84,14 +86,12 @@ discard block |
||
| 84 | 86 | if ($cronjob->get_type() != $type) |
| 85 | 87 | { |
| 86 | 88 | $next_run = isset($schedule[$name]) ? $schedule[$name] : (time() - 1); |
| 87 | - } |
|
| 88 | - else |
|
| 89 | + } else |
|
| 89 | 90 | { |
| 90 | 91 | try |
| 91 | 92 | { |
| 92 | 93 | $cronjob->execute(); |
| 93 | - } |
|
| 94 | - catch (Exception $e) |
|
| 94 | + } catch (Exception $e) |
|
| 95 | 95 | { |
| 96 | 96 | Okapi::mail_admins("Cronjob error: ".$cronjob->get_name(), |
| 97 | 97 | OkapiExceptionHandler::get_exception_info($e)); |
@@ -116,9 +116,10 @@ discard block |
||
| 116 | 116 | # Return the nearest scheduled event time. |
| 117 | 117 | |
| 118 | 118 | $nearest = time() + 3600; |
| 119 | - foreach ($fixed_schedule as $name => $time) |
|
| 120 | - if ($time < $nearest) |
|
| 119 | + foreach ($fixed_schedule as $name => $time) { |
|
| 120 | + if ($time < $nearest) |
|
| 121 | 121 | $nearest = $time; |
| 122 | + } |
|
| 122 | 123 | Cache::set("cron_schedule", $fixed_schedule, 30*86400); |
| 123 | 124 | $lock->release(); |
| 124 | 125 | return $nearest; |
@@ -150,11 +151,13 @@ discard block |
||
| 150 | 151 | public static function reset_job_schedule($job_name) |
| 151 | 152 | { |
| 152 | 153 | $thejob = null; |
| 153 | - foreach (self::get_enabled_cronjobs() as $tmp) |
|
| 154 | - if (($tmp->get_name() == $job_name) || ($tmp->get_name() == "okapi\\cronjobs\\".$job_name)) |
|
| 154 | + foreach (self::get_enabled_cronjobs() as $tmp) { |
|
| 155 | + if (($tmp->get_name() == $job_name) || ($tmp->get_name() == "okapi\\cronjobs\\".$job_name)) |
|
| 155 | 156 | $thejob = $tmp; |
| 156 | - if ($thejob == null) |
|
| 157 | - throw new Exception("Could not reset schedule for job $job_name. $jon_name not found."); |
|
| 157 | + } |
|
| 158 | + if ($thejob == null) { |
|
| 159 | + throw new Exception("Could not reset schedule for job $job_name. $jon_name not found."); |
|
| 160 | + } |
|
| 158 | 161 | |
| 159 | 162 | # We have to acquire lock on the schedule. This might take some time if cron-5 jobs are |
| 160 | 163 | # currently being run. |
@@ -166,8 +169,9 @@ discard block |
||
| 166 | 169 | $schedule = Cache::get("cron_schedule"); |
| 167 | 170 | if ($schedule != null) |
| 168 | 171 | { |
| 169 | - if (isset($schedule[$thejob->get_name()])) |
|
| 170 | - unset($schedule[$thejob->get_name()]); |
|
| 172 | + if (isset($schedule[$thejob->get_name()])) { |
|
| 173 | + unset($schedule[$thejob->get_name()]); |
|
| 174 | + } |
|
| 171 | 175 | Cache::set("cron_schedule", $schedule, 30*86400); |
| 172 | 176 | } |
| 173 | 177 | |
@@ -260,8 +264,9 @@ discard block |
||
| 260 | 264 | public function get_period() { return 300; } # 5 minutes |
| 261 | 265 | public function execute() |
| 262 | 266 | { |
| 263 | - if (Okapi::$data_store) |
|
| 264 | - Okapi::$data_store->cleanup(); |
|
| 267 | + if (Okapi::$data_store) { |
|
| 268 | + Okapi::$data_store->cleanup(); |
|
| 269 | + } |
|
| 265 | 270 | } |
| 266 | 271 | } |
| 267 | 272 | |
@@ -361,8 +366,9 @@ discard block |
||
| 361 | 366 | public function get_period() { return 60; } # 1 minute |
| 362 | 367 | public function execute() |
| 363 | 368 | { |
| 364 | - if (Okapi::get_var('db_version', 0) + 0 < 32) |
|
| 365 | - return; |
|
| 369 | + if (Okapi::get_var('db_version', 0) + 0 < 32) { |
|
| 370 | + return; |
|
| 371 | + } |
|
| 366 | 372 | Db::execute("lock tables okapi_stats_hourly write, okapi_stats_temp write;"); |
| 367 | 373 | try { |
| 368 | 374 | $rs = Db::query(" |
@@ -419,8 +425,9 @@ discard block |
||
| 419 | 425 | public function get_period() { return 3600; } # 1 hour |
| 420 | 426 | public function execute() |
| 421 | 427 | { |
| 422 | - if (Okapi::get_var('db_version', 0) + 0 < 94) |
|
| 423 | - return; |
|
| 428 | + if (Okapi::get_var('db_version', 0) + 0 < 94) { |
|
| 429 | + return; |
|
| 430 | + } |
|
| 424 | 431 | |
| 425 | 432 | # We will process a single month, every time we are being run. |
| 426 | 433 | |
@@ -506,8 +513,10 @@ discard block |
||
| 506 | 513 | public function execute() |
| 507 | 514 | { |
| 508 | 515 | $last_ping = Cache::get('crontab_last_ping'); |
| 509 | - if ($last_ping === null) |
|
| 510 | - $last_ping = time() - 86400; # if not set, assume 1 day ago. |
|
| 516 | + if ($last_ping === null) { |
|
| 517 | + $last_ping = time() - 86400; |
|
| 518 | + } |
|
| 519 | + # if not set, assume 1 day ago. |
|
| 511 | 520 | if ($last_ping > time() - 3600) |
| 512 | 521 | { |
| 513 | 522 | # There was a ping during the last hour. Everything is okay. |
@@ -520,14 +529,14 @@ discard block |
||
| 520 | 529 | # There was no ping. Decrement the counter. When reached zero, alert. |
| 521 | 530 | |
| 522 | 531 | $counter = Cache::get('crontab_check_counter'); |
| 523 | - if ($counter === null) |
|
| 524 | - $counter = 5; |
|
| 532 | + if ($counter === null) { |
|
| 533 | + $counter = 5; |
|
| 534 | + } |
|
| 525 | 535 | $counter--; |
| 526 | 536 | if ($counter > 0) |
| 527 | 537 | { |
| 528 | 538 | Cache::set('crontab_check_counter', $counter, 86400); |
| 529 | - } |
|
| 530 | - elseif ($counter == 0) |
|
| 539 | + } elseif ($counter == 0) |
|
| 531 | 540 | { |
| 532 | 541 | Okapi::mail_admins( |
| 533 | 542 | "Crontab not working.", |
@@ -605,18 +614,24 @@ discard block |
||
| 605 | 614 | # No update necessary. |
| 606 | 615 | } elseif ($tiletree_revision < $current_clog_revision) { |
| 607 | 616 | require_once($GLOBALS['rootpath']."okapi/services/caches/map/replicate_listener.inc.php"); |
| 608 | - if ($current_clog_revision - $tiletree_revision < 30000) # In the middle of 2012, OCPL generated 30000 entries per week |
|
| 617 | + if ($current_clog_revision - $tiletree_revision < 30000) { |
|
| 618 | + # In the middle of 2012, OCPL generated 30000 entries per week |
|
| 609 | 619 | { |
| 610 | - for ($timeout = time() + 240; time() < $timeout; ) # Try to stop after 4 minutes. |
|
| 620 | + for ($timeout = time() + 240; |
|
| 621 | + } |
|
| 622 | + time() < $timeout; ) { |
|
| 623 | + # Try to stop after 4 minutes. |
|
| 611 | 624 | { |
| 612 | 625 | try { |
| 613 | 626 | $response = OkapiServiceRunner::call('services/replicate/changelog', new OkapiInternalRequest( |
| 614 | 627 | new OkapiInternalConsumer(), null, array('since' => $tiletree_revision))); |
| 628 | + } |
|
| 615 | 629 | \okapi\services\caches\map\ReplicateListener::receive($response['changelog']); |
| 616 | 630 | $tiletree_revision = $response['revision']; |
| 617 | 631 | Okapi::set_var('clog_followup_revision', $tiletree_revision); |
| 618 | - if (!$response['more']) |
|
| 619 | - break; |
|
| 632 | + if (!$response['more']) { |
|
| 633 | + break; |
|
| 634 | + } |
|
| 620 | 635 | } catch (BadRequest $e) { |
| 621 | 636 | # Invalid 'since' parameter? May happen when crontab was |
| 622 | 637 | # not working for more than 10 days. Or, just after OKAPI |
@@ -647,17 +662,19 @@ discard block |
||
| 647 | 662 | $max_revision = ReplicateCommon::get_revision(); |
| 648 | 663 | $cache_key = 'clog_revisions_daily'; |
| 649 | 664 | $data = Cache::get($cache_key); |
| 650 | - if ($data == null) |
|
| 651 | - $data = array(); |
|
| 665 | + if ($data == null) { |
|
| 666 | + $data = array(); |
|
| 667 | + } |
|
| 652 | 668 | $data[time()] = $max_revision; |
| 653 | 669 | $new_min_revision = 1; |
| 654 | 670 | $new_data = array(); |
| 655 | 671 | foreach ($data as $time => $r) |
| 656 | 672 | { |
| 657 | - if ($time < time() - 10*86400) |
|
| 658 | - $new_min_revision = max($new_min_revision, $r); |
|
| 659 | - else |
|
| 660 | - $new_data[$time] = $r; |
|
| 673 | + if ($time < time() - 10*86400) { |
|
| 674 | + $new_min_revision = max($new_min_revision, $r); |
|
| 675 | + } else { |
|
| 676 | + $new_data[$time] = $r; |
|
| 677 | + } |
|
| 661 | 678 | } |
| 662 | 679 | Db::execute(" |
| 663 | 680 | delete from okapi_clog |
@@ -721,12 +738,14 @@ discard block |
||
| 721 | 738 | foreach ($consumers as $row) |
| 722 | 739 | { |
| 723 | 740 | $name = $row['name']; |
| 724 | - if ($row['consumer_key'] == 'anonymous') |
|
| 725 | - $name = "Anonymous (Level 0 Authentication)"; |
|
| 726 | - elseif ($row['consumer_key'] == 'facade') |
|
| 727 | - $name = "Internal usage via Facade"; |
|
| 728 | - if (mb_strlen($name) > 35) |
|
| 729 | - $name = mb_substr($name, 0, 32)."..."; |
|
| 741 | + if ($row['consumer_key'] == 'anonymous') { |
|
| 742 | + $name = "Anonymous (Level 0 Authentication)"; |
|
| 743 | + } elseif ($row['consumer_key'] == 'facade') { |
|
| 744 | + $name = "Internal usage via Facade"; |
|
| 745 | + } |
|
| 746 | + if (mb_strlen($name) > 35) { |
|
| 747 | + $name = mb_substr($name, 0, 32)."..."; |
|
| 748 | + } |
|
| 730 | 749 | print self::mb_str_pad($name, 35, " ", STR_PAD_RIGHT); |
| 731 | 750 | print str_pad($row['http_calls'], 8, " ", STR_PAD_LEFT); |
| 732 | 751 | print str_pad(sprintf("%01.2f", $row['http_runtime']), 11, " ", STR_PAD_LEFT)."s\n"; |
@@ -749,8 +768,9 @@ discard block |
||
| 749 | 768 | foreach ($methods as $row) |
| 750 | 769 | { |
| 751 | 770 | $name = $row['service_name']; |
| 752 | - if (mb_strlen($name) > 35) |
|
| 753 | - $name = mb_substr($name, 0, 32)."..."; |
|
| 771 | + if (mb_strlen($name) > 35) { |
|
| 772 | + $name = mb_substr($name, 0, 32)."..."; |
|
| 773 | + } |
|
| 754 | 774 | print self::mb_str_pad($name, 35, " ", STR_PAD_RIGHT); |
| 755 | 775 | print str_pad($row['http_calls'], 8, " ", STR_PAD_LEFT); |
| 756 | 776 | print str_pad(sprintf("%01.2f", $row['http_runtime']), 11, " ", STR_PAD_LEFT)."s"; |
@@ -777,8 +797,9 @@ discard block |
||
| 777 | 797 | foreach ($oauth_users as $row) |
| 778 | 798 | { |
| 779 | 799 | $name = $row['name']; |
| 780 | - if (mb_strlen($name) > 35) |
|
| 781 | - $name = mb_substr($name, 0, 32)."..."; |
|
| 800 | + if (mb_strlen($name) > 35) { |
|
| 801 | + $name = mb_substr($name, 0, 32)."..."; |
|
| 802 | + } |
|
| 782 | 803 | print self::mb_str_pad($name, 35, " ", STR_PAD_RIGHT); |
| 783 | 804 | print str_pad($row['users'], 8, " ", STR_PAD_LEFT)."\n"; |
| 784 | 805 | } |
@@ -813,11 +834,14 @@ discard block |
||
| 813 | 834 | $required = Locales::get_required_locales(); |
| 814 | 835 | $installed = Locales::get_installed_locales(); |
| 815 | 836 | $missing = array(); |
| 816 | - foreach ($required as $locale) |
|
| 817 | - if (!in_array($locale, $installed)) |
|
| 837 | + foreach ($required as $locale) { |
|
| 838 | + if (!in_array($locale, $installed)) |
|
| 818 | 839 | $missing[] = $locale; |
| 819 | - if (count($missing) == 0) |
|
| 820 | - return; # okay! |
|
| 840 | + } |
|
| 841 | + if (count($missing) == 0) { |
|
| 842 | + return; |
|
| 843 | + } |
|
| 844 | + # okay! |
|
| 821 | 845 | ob_start(); |
| 822 | 846 | print "Hi!\n\n"; |
| 823 | 847 | print "Your system is missing some locales required by OKAPI for proper\n"; |
@@ -835,14 +859,15 @@ discard block |
||
| 835 | 859 | if ((count($missing) == 1) && ($missing[0] == 'POSIX')) |
| 836 | 860 | { |
| 837 | 861 | # I don't remember how to install POSIX, probably everyone has it anyway. |
| 838 | - } |
|
| 839 | - else |
|
| 862 | + } else |
|
| 840 | 863 | { |
| 841 | 864 | print "On Debian, try the following:\n\n"; |
| 842 | 865 | foreach ($prefixes as $lang) |
| 843 | 866 | { |
| 844 | - if ($lang != 'PO') # Two first letters cut from POSIX. |
|
| 867 | + if ($lang != 'PO') { |
|
| 868 | + # Two first letters cut from POSIX. |
|
| 845 | 869 | print "sudo apt-get install language-pack-".$lang."-base\n"; |
| 870 | + } |
|
| 846 | 871 | } |
| 847 | 872 | print "sudo service apache2 restart\n"; |
| 848 | 873 | print "\n"; |
@@ -77,6 +77,9 @@ discard block |
||
| 77 | 77 | $this->AddInfo = array(); |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | + /** |
|
| 81 | + * @param string $Name |
|
| 82 | + */ |
|
| 80 | 83 | function FileAdd($Name, $Data, $DataType=self::TBSZIP_STRING, $Compress=true) { |
| 81 | 84 | |
| 82 | 85 | if ($Data===false) return $this->FileCancelModif($Name, false); // Cancel a previously added file |
@@ -127,6 +130,9 @@ discard block |
||
| 127 | 130 | return true; |
| 128 | 131 | } |
| 129 | 132 | |
| 133 | + /** |
|
| 134 | + * @param string $cd_info |
|
| 135 | + */ |
|
| 130 | 136 | function CentralDirRead_End($cd_info) { |
| 131 | 137 | $b = $cd_info.$this->_ReadData(18); |
| 132 | 138 | $x = array(); |
@@ -142,6 +148,9 @@ discard block |
||
| 142 | 148 | return $x; |
| 143 | 149 | } |
| 144 | 150 | |
| 151 | + /** |
|
| 152 | + * @param integer $idx |
|
| 153 | + */ |
|
| 145 | 154 | function CentralDirRead_File($idx) { |
| 146 | 155 | |
| 147 | 156 | $b = $this->_ReadData(46); |
@@ -175,6 +184,9 @@ discard block |
||
| 175 | 184 | return $x; |
| 176 | 185 | } |
| 177 | 186 | |
| 187 | + /** |
|
| 188 | + * @param string $Msg |
|
| 189 | + */ |
|
| 178 | 190 | function RaiseError($Msg) { |
| 179 | 191 | if ($this->DisplayError) { |
| 180 | 192 | if (PHP_SAPI==='cli') { |
@@ -310,6 +322,9 @@ discard block |
||
| 310 | 322 | |
| 311 | 323 | } |
| 312 | 324 | |
| 325 | + /** |
|
| 326 | + * @param boolean $ReadData |
|
| 327 | + */ |
|
| 313 | 328 | function _ReadFile($idx, $ReadData) { |
| 314 | 329 | // read the file header (and maybe the data ) in the archive, assuming the cursor in at a new file position |
| 315 | 330 | |
@@ -625,6 +640,10 @@ discard block |
||
| 625 | 640 | // output functions |
| 626 | 641 | // ---------------- |
| 627 | 642 | |
| 643 | + /** |
|
| 644 | + * @param integer $Render |
|
| 645 | + * @param string $ContentType |
|
| 646 | + */ |
|
| 628 | 647 | function OutputOpen($Render, $File, $ContentType) { |
| 629 | 648 | |
| 630 | 649 | if (($Render & self::TBSZIP_FILE)==self::TBSZIP_FILE) { |
@@ -697,6 +716,9 @@ discard block |
||
| 697 | 716 | // Reading functions |
| 698 | 717 | // ---------------- |
| 699 | 718 | |
| 719 | + /** |
|
| 720 | + * @param integer $relative |
|
| 721 | + */ |
|
| 700 | 722 | function _MoveTo($pos, $relative = SEEK_SET) { |
| 701 | 723 | fseek($this->ArchHnd, $pos, $relative); |
| 702 | 724 | } |
@@ -714,6 +736,10 @@ discard block |
||
| 714 | 736 | // Take info from binary data |
| 715 | 737 | // ---------------- |
| 716 | 738 | |
| 739 | + /** |
|
| 740 | + * @param integer $pos |
|
| 741 | + * @param integer $len |
|
| 742 | + */ |
|
| 717 | 743 | function _GetDec($txt, $pos, $len) { |
| 718 | 744 | $x = substr($txt, $pos, $len); |
| 719 | 745 | $z = 0; |
@@ -724,11 +750,21 @@ discard block |
||
| 724 | 750 | return $z; |
| 725 | 751 | } |
| 726 | 752 | |
| 753 | + /** |
|
| 754 | + * @param string $txt |
|
| 755 | + * @param integer $pos |
|
| 756 | + * @param integer $len |
|
| 757 | + */ |
|
| 727 | 758 | function _GetHex($txt, $pos, $len) { |
| 728 | 759 | $x = substr($txt, $pos, $len); |
| 729 | 760 | return 'h:'.bin2hex(strrev($x)); |
| 730 | 761 | } |
| 731 | 762 | |
| 763 | + /** |
|
| 764 | + * @param string $txt |
|
| 765 | + * @param integer $pos |
|
| 766 | + * @param integer $len |
|
| 767 | + */ |
|
| 732 | 768 | function _GetBin($txt, $pos, $len) { |
| 733 | 769 | $x = substr($txt, $pos, $len); |
| 734 | 770 | $z = ''; |
@@ -749,6 +785,10 @@ discard block |
||
| 749 | 785 | // Put info into binary data |
| 750 | 786 | // ---------------- |
| 751 | 787 | |
| 788 | + /** |
|
| 789 | + * @param integer $pos |
|
| 790 | + * @param integer $len |
|
| 791 | + */ |
|
| 752 | 792 | function _PutDec(&$txt, $val, $pos, $len) { |
| 753 | 793 | $x = ''; |
| 754 | 794 | for ($i=0;$i<$len;$i++) { |
@@ -802,6 +842,7 @@ discard block |
||
| 802 | 842 | * Return the position of the record in the file. |
| 803 | 843 | * Return false if the record is not found. The comment cannot exceed 65335 bytes (=FFFF). |
| 804 | 844 | * The method is read backwards a block of 256 bytes and search the key in this block. |
| 845 | + * @param string $cd_info |
|
| 805 | 846 | */ |
| 806 | 847 | function _FindCDEnd($cd_info) { |
| 807 | 848 | $nbr = 1; |
@@ -825,6 +866,9 @@ discard block |
||
| 825 | 866 | return false; |
| 826 | 867 | } |
| 827 | 868 | |
| 869 | + /** |
|
| 870 | + * @param integer $PosLoc |
|
| 871 | + */ |
|
| 828 | 872 | function _DataOuputAddedFile($Idx, $PosLoc) { |
| 829 | 873 | |
| 830 | 874 | $Ref =& $this->AddInfo[$Idx]; |
@@ -885,6 +929,10 @@ discard block |
||
| 885 | 929 | |
| 886 | 930 | } |
| 887 | 931 | |
| 932 | + /** |
|
| 933 | + * @param integer $DataType |
|
| 934 | + * @param boolean $Compress |
|
| 935 | + */ |
|
| 888 | 936 | function _DataCreateNewRef($Data, $DataType, $Compress, $Diff, $NameOrIdx) { |
| 889 | 937 | |
| 890 | 938 | $file_time = false; |
@@ -12,10 +12,10 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | |
| 14 | 14 | class clsTbsZip { |
| 15 | - const TBSZIP_DOWNLOAD = 1; // download (default) |
|
| 16 | - const TBSZIP_NOHEADER = 4; // option to use with DOWNLOAD: no header is sent |
|
| 17 | - const TBSZIP_FILE = 8; // output to file , or add from file |
|
| 18 | - const TBSZIP_STRING = 32; // output to string, or add from string |
|
| 15 | + const TBSZIP_DOWNLOAD = 1; // download (default) |
|
| 16 | + const TBSZIP_NOHEADER = 4; // option to use with DOWNLOAD: no header is sent |
|
| 17 | + const TBSZIP_FILE = 8; // output to file , or add from file |
|
| 18 | + const TBSZIP_STRING = 32; // output to string, or add from string |
|
| 19 | 19 | |
| 20 | 20 | function __construct() { |
| 21 | 21 | $this->Meth8Ok = extension_loaded('zlib'); // check if Zlib extension is available. This is need for compress and uncompress with method 8. |
@@ -24,26 +24,26 @@ discard block |
||
| 24 | 24 | $this->Error = false; |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | - function CreateNew($ArchName='new.zip') { |
|
| 27 | + function CreateNew($ArchName = 'new.zip') { |
|
| 28 | 28 | // Create a new virtual empty archive, the name will be the default name when the archive is flushed. |
| 29 | - if (!isset($this->Meth8Ok)) $this->__construct(); // for PHP 4 compatibility |
|
| 29 | + if (!isset($this->Meth8Ok)) $this->__construct(); // for PHP 4 compatibility |
|
| 30 | 30 | $this->Close(); // note that $this->ArchHnd is set to false here |
| 31 | 31 | $this->Error = false; |
| 32 | 32 | $this->ArchFile = $ArchName; |
| 33 | 33 | $this->ArchIsNew = true; |
| 34 | - $bin = 'PK'.chr(05).chr(06).str_repeat(chr(0), 18); |
|
| 34 | + $bin = 'PK' . chr(05) . chr(06) . str_repeat(chr(0), 18); |
|
| 35 | 35 | $this->CdEndPos = strlen($bin) - 4; |
| 36 | 36 | $this->CdInfo = array('disk_num_curr'=>0, 'disk_num_cd'=>0, 'file_nbr_curr'=>0, 'file_nbr_tot'=>0, 'l_cd'=>0, 'p_cd'=>0, 'l_comm'=>0, 'v_comm'=>'', 'bin'=>$bin); |
| 37 | 37 | $this->CdPos = $this->CdInfo['p_cd']; |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - function Open($ArchFile, $UseIncludePath=false) { |
|
| 40 | + function Open($ArchFile, $UseIncludePath = false) { |
|
| 41 | 41 | // Open the zip archive |
| 42 | - if (!isset($this->Meth8Ok)) $this->__construct(); // for PHP 4 compatibility |
|
| 42 | + if (!isset($this->Meth8Ok)) $this->__construct(); // for PHP 4 compatibility |
|
| 43 | 43 | $this->Close(); // close handle and init info |
| 44 | 44 | $this->Error = false; |
| 45 | 45 | $this->ArchIsNew = false; |
| 46 | - $this->ArchIsStream = (is_resource($ArchFile) && (get_resource_type($ArchFile)=='stream')); |
|
| 46 | + $this->ArchIsStream = (is_resource($ArchFile) && (get_resource_type($ArchFile) == 'stream')); |
|
| 47 | 47 | if ($this->ArchIsStream) { |
| 48 | 48 | $this->ArchFile = 'from_stream.zip'; |
| 49 | 49 | $this->ArchHnd = $ArchFile; |
@@ -52,13 +52,13 @@ discard block |
||
| 52 | 52 | $this->ArchFile = $ArchFile; |
| 53 | 53 | $this->ArchHnd = fopen($ArchFile, 'rb', $UseIncludePath); |
| 54 | 54 | } |
| 55 | - $ok = !($this->ArchHnd===false); |
|
| 55 | + $ok = !($this->ArchHnd === false); |
|
| 56 | 56 | if ($ok) $ok = $this->CentralDirRead(); |
| 57 | 57 | return $ok; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | function Close() { |
| 61 | - if (isset($this->ArchHnd) and ($this->ArchHnd!==false)) fclose($this->ArchHnd); |
|
| 61 | + if (isset($this->ArchHnd) and ($this->ArchHnd !== false)) fclose($this->ArchHnd); |
|
| 62 | 62 | $this->ArchFile = ''; |
| 63 | 63 | $this->ArchHnd = false; |
| 64 | 64 | $this->CdInfo = array(); |
@@ -71,20 +71,20 @@ discard block |
||
| 71 | 71 | |
| 72 | 72 | function ArchCancelModif() { |
| 73 | 73 | $this->LastReadComp = false; // compression of the last read file (1=compressed, 0=stored not compressed, -1= stored compressed but read uncompressed) |
| 74 | - $this->LastReadIdx = false; // index of the last file read |
|
| 74 | + $this->LastReadIdx = false; // index of the last file read |
|
| 75 | 75 | $this->ReplInfo = array(); |
| 76 | 76 | $this->ReplByPos = array(); |
| 77 | 77 | $this->AddInfo = array(); |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - function FileAdd($Name, $Data, $DataType=self::TBSZIP_STRING, $Compress=true) { |
|
| 80 | + function FileAdd($Name, $Data, $DataType = self::TBSZIP_STRING, $Compress = true) { |
|
| 81 | 81 | |
| 82 | - if ($Data===false) return $this->FileCancelModif($Name, false); // Cancel a previously added file |
|
| 82 | + if ($Data === false) return $this->FileCancelModif($Name, false); // Cancel a previously added file |
|
| 83 | 83 | |
| 84 | 84 | // Save information for adding a new file into the archive |
| 85 | - $Diff = 30 + 46 + 2*strlen($Name); // size of the header + cd info |
|
| 85 | + $Diff = 30 + 46 + 2 * strlen($Name); // size of the header + cd info |
|
| 86 | 86 | $Ref = $this->_DataCreateNewRef($Data, $DataType, $Compress, $Diff, $Name); |
| 87 | - if ($Ref===false) return false; |
|
| 87 | + if ($Ref === false) return false; |
|
| 88 | 88 | $Ref['name'] = $Name; |
| 89 | 89 | $this->AddInfo[] = $Ref; |
| 90 | 90 | return $Ref['res']; |
@@ -92,20 +92,20 @@ discard block |
||
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | function CentralDirRead() { |
| 95 | - $cd_info = 'PK'.chr(05).chr(06); // signature of the Central Directory |
|
| 95 | + $cd_info = 'PK' . chr(05) . chr(06); // signature of the Central Directory |
|
| 96 | 96 | $cd_pos = -22; |
| 97 | 97 | $this->_MoveTo($cd_pos, SEEK_END); |
| 98 | 98 | $b = $this->_ReadData(4); |
| 99 | - if ($b===$cd_info) { |
|
| 99 | + if ($b === $cd_info) { |
|
| 100 | 100 | $this->CdEndPos = ftell($this->ArchHnd) - 4; |
| 101 | 101 | } else { |
| 102 | 102 | $p = $this->_FindCDEnd($cd_info); |
| 103 | 103 | //echo 'p='.var_export($p,true); exit; |
| 104 | - if ($p===false) { |
|
| 104 | + if ($p === false) { |
|
| 105 | 105 | return $this->RaiseError('The End of Central Directory Record is not found.'); |
| 106 | 106 | } else { |
| 107 | 107 | $this->CdEndPos = $p; |
| 108 | - $this->_MoveTo($p+4); |
|
| 108 | + $this->_MoveTo($p + 4); |
|
| 109 | 109 | } |
| 110 | 110 | } |
| 111 | 111 | $this->CdInfo = $this->CentralDirRead_End($cd_info); |
@@ -113,13 +113,13 @@ discard block |
||
| 113 | 113 | $this->CdFileNbr = $this->CdInfo['file_nbr_curr']; |
| 114 | 114 | $this->CdPos = $this->CdInfo['p_cd']; |
| 115 | 115 | |
| 116 | - if ($this->CdFileNbr<=0) return $this->RaiseError('No header found in the Central Directory.'); |
|
| 117 | - if ($this->CdPos<=0) return $this->RaiseError('No position found for the Central Directory.'); |
|
| 116 | + if ($this->CdFileNbr <= 0) return $this->RaiseError('No header found in the Central Directory.'); |
|
| 117 | + if ($this->CdPos <= 0) return $this->RaiseError('No position found for the Central Directory.'); |
|
| 118 | 118 | |
| 119 | 119 | $this->_MoveTo($this->CdPos); |
| 120 | - for ($i=0;$i<$this->CdFileNbr;$i++) { |
|
| 120 | + for ($i = 0; $i < $this->CdFileNbr; $i++) { |
|
| 121 | 121 | $x = $this->CentralDirRead_File($i); |
| 122 | - if ($x!==false) { |
|
| 122 | + if ($x !== false) { |
|
| 123 | 123 | $this->CdFileLst[$i] = $x; |
| 124 | 124 | $this->CdFileByName[$x['v_name']] = $i; |
| 125 | 125 | } |
@@ -128,17 +128,17 @@ discard block |
||
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | function CentralDirRead_End($cd_info) { |
| 131 | - $b = $cd_info.$this->_ReadData(18); |
|
| 131 | + $b = $cd_info . $this->_ReadData(18); |
|
| 132 | 132 | $x = array(); |
| 133 | - $x['disk_num_curr'] = $this->_GetDec($b,4,2); // number of this disk |
|
| 134 | - $x['disk_num_cd'] = $this->_GetDec($b,6,2); // number of the disk with the start of the central directory |
|
| 135 | - $x['file_nbr_curr'] = $this->_GetDec($b,8,2); // total number of entries in the central directory on this disk |
|
| 136 | - $x['file_nbr_tot'] = $this->_GetDec($b,10,2); // total number of entries in the central directory |
|
| 137 | - $x['l_cd'] = $this->_GetDec($b,12,4); // size of the central directory |
|
| 138 | - $x['p_cd'] = $this->_GetDec($b,16,4); // position of start of central directory with respect to the starting disk number |
|
| 139 | - $x['l_comm'] = $this->_GetDec($b,20,2); // .ZIP file comment length |
|
| 133 | + $x['disk_num_curr'] = $this->_GetDec($b, 4, 2); // number of this disk |
|
| 134 | + $x['disk_num_cd'] = $this->_GetDec($b, 6, 2); // number of the disk with the start of the central directory |
|
| 135 | + $x['file_nbr_curr'] = $this->_GetDec($b, 8, 2); // total number of entries in the central directory on this disk |
|
| 136 | + $x['file_nbr_tot'] = $this->_GetDec($b, 10, 2); // total number of entries in the central directory |
|
| 137 | + $x['l_cd'] = $this->_GetDec($b, 12, 4); // size of the central directory |
|
| 138 | + $x['p_cd'] = $this->_GetDec($b, 16, 4); // position of start of central directory with respect to the starting disk number |
|
| 139 | + $x['l_comm'] = $this->_GetDec($b, 20, 2); // .ZIP file comment length |
|
| 140 | 140 | $x['v_comm'] = $this->_ReadData($x['l_comm']); // .ZIP file comment |
| 141 | - $x['bin'] = $b.$x['v_comm']; |
|
| 141 | + $x['bin'] = $b . $x['v_comm']; |
|
| 142 | 142 | return $x; |
| 143 | 143 | } |
| 144 | 144 | |
@@ -146,48 +146,48 @@ discard block |
||
| 146 | 146 | |
| 147 | 147 | $b = $this->_ReadData(46); |
| 148 | 148 | |
| 149 | - $x = $this->_GetHex($b,0,4); |
|
| 150 | - if ($x!=='h:02014b50') return $this->RaiseError("Signature of Central Directory Header #".$idx." (file information) expected but not found at position ".$this->_TxtPos(ftell($this->ArchHnd) - 46)."."); |
|
| 149 | + $x = $this->_GetHex($b, 0, 4); |
|
| 150 | + if ($x !== 'h:02014b50') return $this->RaiseError("Signature of Central Directory Header #" . $idx . " (file information) expected but not found at position " . $this->_TxtPos(ftell($this->ArchHnd) - 46) . "."); |
|
| 151 | 151 | |
| 152 | 152 | $x = array(); |
| 153 | - $x['vers_used'] = $this->_GetDec($b,4,2); |
|
| 154 | - $x['vers_necess'] = $this->_GetDec($b,6,2); |
|
| 155 | - $x['purp'] = $this->_GetBin($b,8,2); |
|
| 156 | - $x['meth'] = $this->_GetDec($b,10,2); |
|
| 157 | - $x['time'] = $this->_GetDec($b,12,2); |
|
| 158 | - $x['date'] = $this->_GetDec($b,14,2); |
|
| 159 | - $x['crc32'] = $this->_GetDec($b,16,4); |
|
| 160 | - $x['l_data_c'] = $this->_GetDec($b,20,4); |
|
| 161 | - $x['l_data_u'] = $this->_GetDec($b,24,4); |
|
| 162 | - $x['l_name'] = $this->_GetDec($b,28,2); |
|
| 163 | - $x['l_fields'] = $this->_GetDec($b,30,2); |
|
| 164 | - $x['l_comm'] = $this->_GetDec($b,32,2); |
|
| 165 | - $x['disk_num'] = $this->_GetDec($b,34,2); |
|
| 166 | - $x['int_file_att'] = $this->_GetDec($b,36,2); |
|
| 167 | - $x['ext_file_att'] = $this->_GetDec($b,38,4); |
|
| 168 | - $x['p_loc'] = $this->_GetDec($b,42,4); |
|
| 153 | + $x['vers_used'] = $this->_GetDec($b, 4, 2); |
|
| 154 | + $x['vers_necess'] = $this->_GetDec($b, 6, 2); |
|
| 155 | + $x['purp'] = $this->_GetBin($b, 8, 2); |
|
| 156 | + $x['meth'] = $this->_GetDec($b, 10, 2); |
|
| 157 | + $x['time'] = $this->_GetDec($b, 12, 2); |
|
| 158 | + $x['date'] = $this->_GetDec($b, 14, 2); |
|
| 159 | + $x['crc32'] = $this->_GetDec($b, 16, 4); |
|
| 160 | + $x['l_data_c'] = $this->_GetDec($b, 20, 4); |
|
| 161 | + $x['l_data_u'] = $this->_GetDec($b, 24, 4); |
|
| 162 | + $x['l_name'] = $this->_GetDec($b, 28, 2); |
|
| 163 | + $x['l_fields'] = $this->_GetDec($b, 30, 2); |
|
| 164 | + $x['l_comm'] = $this->_GetDec($b, 32, 2); |
|
| 165 | + $x['disk_num'] = $this->_GetDec($b, 34, 2); |
|
| 166 | + $x['int_file_att'] = $this->_GetDec($b, 36, 2); |
|
| 167 | + $x['ext_file_att'] = $this->_GetDec($b, 38, 4); |
|
| 168 | + $x['p_loc'] = $this->_GetDec($b, 42, 4); |
|
| 169 | 169 | $x['v_name'] = $this->_ReadData($x['l_name']); |
| 170 | 170 | $x['v_fields'] = $this->_ReadData($x['l_fields']); |
| 171 | 171 | $x['v_comm'] = $this->_ReadData($x['l_comm']); |
| 172 | 172 | |
| 173 | - $x['bin'] = $b.$x['v_name'].$x['v_fields'].$x['v_comm']; |
|
| 173 | + $x['bin'] = $b . $x['v_name'] . $x['v_fields'] . $x['v_comm']; |
|
| 174 | 174 | |
| 175 | 175 | return $x; |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | function RaiseError($Msg) { |
| 179 | 179 | if ($this->DisplayError) { |
| 180 | - if (PHP_SAPI==='cli') { |
|
| 181 | - echo get_class($this).' ERROR with the zip archive: '.$Msg."\r\n"; |
|
| 180 | + if (PHP_SAPI === 'cli') { |
|
| 181 | + echo get_class($this) . ' ERROR with the zip archive: ' . $Msg . "\r\n"; |
|
| 182 | 182 | } else { |
| 183 | - echo '<strong>'.get_class($this).' ERROR with the zip archive:</strong> '.$Msg.'<br>'."\r\n"; |
|
| 183 | + echo '<strong>' . get_class($this) . ' ERROR with the zip archive:</strong> ' . $Msg . '<br>' . "\r\n"; |
|
| 184 | 184 | } |
| 185 | 185 | } |
| 186 | 186 | $this->Error = $Msg; |
| 187 | 187 | return false; |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - function Debug($FileHeaders=false) { |
|
| 190 | + function Debug($FileHeaders = false) { |
|
| 191 | 191 | |
| 192 | 192 | $this->DisplayError = true; |
| 193 | 193 | |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | $pos = 0; |
| 198 | 198 | $pos_stop = $this->CdInfo['p_cd']; |
| 199 | 199 | $this->_MoveTo($pos); |
| 200 | - while ( ($pos<$pos_stop) && ($ok = $this->_ReadFile($idx,false)) ) { |
|
| 200 | + while (($pos < $pos_stop) && ($ok = $this->_ReadFile($idx, false))) { |
|
| 201 | 201 | $this->VisFileLst[$idx]['p_this_header (debug_mode only)'] = $pos; |
| 202 | 202 | $pos = ftell($this->ArchHnd); |
| 203 | 203 | $idx++; |
@@ -207,22 +207,22 @@ discard block |
||
| 207 | 207 | $nl = "\r\n"; |
| 208 | 208 | echo "<pre>"; |
| 209 | 209 | |
| 210 | - echo "-------------------------------".$nl; |
|
| 211 | - echo "End of Central Directory record".$nl; |
|
| 212 | - echo "-------------------------------".$nl; |
|
| 210 | + echo "-------------------------------" . $nl; |
|
| 211 | + echo "End of Central Directory record" . $nl; |
|
| 212 | + echo "-------------------------------" . $nl; |
|
| 213 | 213 | print_r($this->DebugArray($this->CdInfo)); |
| 214 | 214 | |
| 215 | 215 | echo $nl; |
| 216 | - echo "-------------------------".$nl; |
|
| 217 | - echo "Central Directory headers".$nl; |
|
| 218 | - echo "-------------------------".$nl; |
|
| 216 | + echo "-------------------------" . $nl; |
|
| 217 | + echo "Central Directory headers" . $nl; |
|
| 218 | + echo "-------------------------" . $nl; |
|
| 219 | 219 | print_r($this->DebugArray($this->CdFileLst)); |
| 220 | 220 | |
| 221 | 221 | if ($FileHeaders) { |
| 222 | 222 | echo $nl; |
| 223 | - echo "------------------".$nl; |
|
| 224 | - echo "Local File headers".$nl; |
|
| 225 | - echo "------------------".$nl; |
|
| 223 | + echo "------------------" . $nl; |
|
| 224 | + echo "Local File headers" . $nl; |
|
| 225 | + echo "------------------" . $nl; |
|
| 226 | 226 | print_r($this->DebugArray($this->VisFileLst)); |
| 227 | 227 | } |
| 228 | 228 | |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | foreach ($arr as $k=>$v) { |
| 235 | 235 | if (is_array($v)) { |
| 236 | 236 | $arr[$k] = $this->DebugArray($v); |
| 237 | - } elseif (substr($k,0,2)=='p_') { |
|
| 237 | + } elseif (substr($k, 0, 2) == 'p_') { |
|
| 238 | 238 | $arr[$k] = $this->_TxtPos($v); |
| 239 | 239 | } |
| 240 | 240 | } |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | function FileExists($NameOrIdx) { |
| 245 | - return ($this->FileGetIdx($NameOrIdx)!==false); |
|
| 245 | + return ($this->FileGetIdx($NameOrIdx) !== false); |
|
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | function FileGetIdx($NameOrIdx) { |
@@ -267,18 +267,18 @@ discard block |
||
| 267 | 267 | if (!is_string($Name)) return false; |
| 268 | 268 | $idx_lst = array_keys($this->AddInfo); |
| 269 | 269 | foreach ($idx_lst as $idx) { |
| 270 | - if ($this->AddInfo[$idx]['name']===$Name) return $idx; |
|
| 270 | + if ($this->AddInfo[$idx]['name'] === $Name) return $idx; |
|
| 271 | 271 | } |
| 272 | 272 | return false; |
| 273 | 273 | } |
| 274 | 274 | |
| 275 | - function FileRead($NameOrIdx, $Uncompress=true) { |
|
| 275 | + function FileRead($NameOrIdx, $Uncompress = true) { |
|
| 276 | 276 | |
| 277 | 277 | $this->LastReadComp = false; // means the file is not found |
| 278 | 278 | $this->LastReadIdx = false; |
| 279 | 279 | |
| 280 | 280 | $idx = $this->FileGetIdx($NameOrIdx); |
| 281 | - if ($idx===false) return $this->RaiseError('File "'.$NameOrIdx.'" is not found in the Central Directory.'); |
|
| 281 | + if ($idx === false) return $this->RaiseError('File "' . $NameOrIdx . '" is not found in the Central Directory.'); |
|
| 282 | 282 | |
| 283 | 283 | $pos = $this->CdFileLst[$idx]['p_loc']; |
| 284 | 284 | $this->_MoveTo($pos); |
@@ -290,19 +290,19 @@ discard block |
||
| 290 | 290 | // Manage uncompression |
| 291 | 291 | $Comp = 1; // means the contents stays compressed |
| 292 | 292 | $meth = $this->CdFileLst[$idx]['meth']; |
| 293 | - if ($meth==8) { |
|
| 293 | + if ($meth == 8) { |
|
| 294 | 294 | if ($Uncompress) { |
| 295 | 295 | if ($this->Meth8Ok) { |
| 296 | 296 | $Data = gzinflate($Data); |
| 297 | 297 | $Comp = -1; // means uncompressed |
| 298 | 298 | } else { |
| 299 | - $this->RaiseError('Unable to uncompress file "'.$NameOrIdx.'" because extension Zlib is not installed.'); |
|
| 299 | + $this->RaiseError('Unable to uncompress file "' . $NameOrIdx . '" because extension Zlib is not installed.'); |
|
| 300 | 300 | } |
| 301 | 301 | } |
| 302 | - } elseif($meth==0) { |
|
| 302 | + } elseif ($meth == 0) { |
|
| 303 | 303 | $Comp = 0; // means stored without compression |
| 304 | 304 | } else { |
| 305 | - if ($Uncompress) $this->RaiseError('Unable to uncompress file "'.$NameOrIdx.'" because it is compressed with method '.$meth.'.'); |
|
| 305 | + if ($Uncompress) $this->RaiseError('Unable to uncompress file "' . $NameOrIdx . '" because it is compressed with method ' . $meth . '.'); |
|
| 306 | 306 | } |
| 307 | 307 | $this->LastReadComp = $Comp; |
| 308 | 308 | |
@@ -315,40 +315,40 @@ discard block |
||
| 315 | 315 | |
| 316 | 316 | $b = $this->_ReadData(30); |
| 317 | 317 | |
| 318 | - $x = $this->_GetHex($b,0,4); |
|
| 319 | - if ($x!=='h:04034b50') return $this->RaiseError("Signature of Local File Header #".$idx." (data section) expected but not found at position ".$this->_TxtPos(ftell($this->ArchHnd)-30)."."); |
|
| 318 | + $x = $this->_GetHex($b, 0, 4); |
|
| 319 | + if ($x !== 'h:04034b50') return $this->RaiseError("Signature of Local File Header #" . $idx . " (data section) expected but not found at position " . $this->_TxtPos(ftell($this->ArchHnd) - 30) . "."); |
|
| 320 | 320 | |
| 321 | 321 | $x = array(); |
| 322 | - $x['vers'] = $this->_GetDec($b,4,2); |
|
| 323 | - $x['purp'] = $this->_GetBin($b,6,2); |
|
| 324 | - $x['meth'] = $this->_GetDec($b,8,2); |
|
| 325 | - $x['time'] = $this->_GetDec($b,10,2); |
|
| 326 | - $x['date'] = $this->_GetDec($b,12,2); |
|
| 327 | - $x['crc32'] = $this->_GetDec($b,14,4); |
|
| 328 | - $x['l_data_c'] = $this->_GetDec($b,18,4); |
|
| 329 | - $x['l_data_u'] = $this->_GetDec($b,22,4); |
|
| 330 | - $x['l_name'] = $this->_GetDec($b,26,2); |
|
| 331 | - $x['l_fields'] = $this->_GetDec($b,28,2); |
|
| 322 | + $x['vers'] = $this->_GetDec($b, 4, 2); |
|
| 323 | + $x['purp'] = $this->_GetBin($b, 6, 2); |
|
| 324 | + $x['meth'] = $this->_GetDec($b, 8, 2); |
|
| 325 | + $x['time'] = $this->_GetDec($b, 10, 2); |
|
| 326 | + $x['date'] = $this->_GetDec($b, 12, 2); |
|
| 327 | + $x['crc32'] = $this->_GetDec($b, 14, 4); |
|
| 328 | + $x['l_data_c'] = $this->_GetDec($b, 18, 4); |
|
| 329 | + $x['l_data_u'] = $this->_GetDec($b, 22, 4); |
|
| 330 | + $x['l_name'] = $this->_GetDec($b, 26, 2); |
|
| 331 | + $x['l_fields'] = $this->_GetDec($b, 28, 2); |
|
| 332 | 332 | $x['v_name'] = $this->_ReadData($x['l_name']); |
| 333 | 333 | $x['v_fields'] = $this->_ReadData($x['l_fields']); |
| 334 | 334 | |
| 335 | - $x['bin'] = $b.$x['v_name'].$x['v_fields']; |
|
| 335 | + $x['bin'] = $b . $x['v_name'] . $x['v_fields']; |
|
| 336 | 336 | |
| 337 | 337 | // Read Data |
| 338 | 338 | if (isset($this->CdFileLst[$idx])) { |
| 339 | 339 | $len_cd = $this->CdFileLst[$idx]['l_data_c']; |
| 340 | - if ($x['l_data_c']==0) { |
|
| 340 | + if ($x['l_data_c'] == 0) { |
|
| 341 | 341 | // Sometimes, the size is not specified in the local information. |
| 342 | 342 | $len = $len_cd; |
| 343 | 343 | } else { |
| 344 | 344 | $len = $x['l_data_c']; |
| 345 | - if ($len!=$len_cd) { |
|
| 345 | + if ($len != $len_cd) { |
|
| 346 | 346 | //echo "TbsZip Warning: Local information for file #".$idx." says len=".$len.", while Central Directory says len=".$len_cd."."; |
| 347 | 347 | } |
| 348 | 348 | } |
| 349 | 349 | } else { |
| 350 | 350 | $len = $x['l_data_c']; |
| 351 | - if ($len==0) $this->RaiseError("File Data #".$idx." cannt be read because no length is specified in the Local File Header and its Central Directory information has not been found."); |
|
| 351 | + if ($len == 0) $this->RaiseError("File Data #" . $idx . " cannt be read because no length is specified in the Local File Header and its Central Directory information has not been found."); |
|
| 352 | 352 | } |
| 353 | 353 | |
| 354 | 354 | if ($ReadData) { |
@@ -358,13 +358,13 @@ discard block |
||
| 358 | 358 | } |
| 359 | 359 | |
| 360 | 360 | // Description information |
| 361 | - $desc_ok = ($x['purp'][2+3]=='1'); |
|
| 361 | + $desc_ok = ($x['purp'][2 + 3] == '1'); |
|
| 362 | 362 | if ($desc_ok) { |
| 363 | 363 | $b = $this->_ReadData(12); |
| 364 | - $s = $this->_GetHex($b,0,4); |
|
| 364 | + $s = $this->_GetHex($b, 0, 4); |
|
| 365 | 365 | $d = 0; |
| 366 | 366 | // the specification says the signature may or may not be present |
| 367 | - if ($s=='h:08074b50') { |
|
| 367 | + if ($s == 'h:08074b50') { |
|
| 368 | 368 | $b .= $this->_ReadData(4); |
| 369 | 369 | $d = 4; |
| 370 | 370 | $x['desc_bin'] = $b; |
@@ -372,9 +372,9 @@ discard block |
||
| 372 | 372 | } else { |
| 373 | 373 | $x['desc_bin'] = $b; |
| 374 | 374 | } |
| 375 | - $x['desc_crc32'] = $this->_GetDec($b,0+$d,4); |
|
| 376 | - $x['desc_l_data_c'] = $this->_GetDec($b,4+$d,4); |
|
| 377 | - $x['desc_l_data_u'] = $this->_GetDec($b,8+$d,4); |
|
| 375 | + $x['desc_crc32'] = $this->_GetDec($b, 0 + $d, 4); |
|
| 376 | + $x['desc_l_data_c'] = $this->_GetDec($b, 4 + $d, 4); |
|
| 377 | + $x['desc_l_data_u'] = $this->_GetDec($b, 8 + $d, 4); |
|
| 378 | 378 | } |
| 379 | 379 | |
| 380 | 380 | // Save file info without the data |
@@ -389,15 +389,15 @@ discard block |
||
| 389 | 389 | |
| 390 | 390 | } |
| 391 | 391 | |
| 392 | - function FileReplace($NameOrIdx, $Data, $DataType=self::TBSZIP_STRING, $Compress=true) { |
|
| 392 | + function FileReplace($NameOrIdx, $Data, $DataType = self::TBSZIP_STRING, $Compress = true) { |
|
| 393 | 393 | // Store replacement information. |
| 394 | 394 | |
| 395 | 395 | $idx = $this->FileGetIdx($NameOrIdx); |
| 396 | - if ($idx===false) return $this->RaiseError('File "'.$NameOrIdx.'" is not found in the Central Directory.'); |
|
| 396 | + if ($idx === false) return $this->RaiseError('File "' . $NameOrIdx . '" is not found in the Central Directory.'); |
|
| 397 | 397 | |
| 398 | 398 | $pos = $this->CdFileLst[$idx]['p_loc']; |
| 399 | 399 | |
| 400 | - if ($Data===false) { |
|
| 400 | + if ($Data === false) { |
|
| 401 | 401 | // file to delete |
| 402 | 402 | $this->ReplInfo[$idx] = false; |
| 403 | 403 | $Result = true; |
@@ -405,7 +405,7 @@ discard block |
||
| 405 | 405 | // file to replace |
| 406 | 406 | $Diff = - $this->CdFileLst[$idx]['l_data_c']; |
| 407 | 407 | $Ref = $this->_DataCreateNewRef($Data, $DataType, $Compress, $Diff, $NameOrIdx); |
| 408 | - if ($Ref===false) return false; |
|
| 408 | + if ($Ref === false) return false; |
|
| 409 | 409 | $this->ReplInfo[$idx] = $Ref; |
| 410 | 410 | $Result = $Ref['res']; |
| 411 | 411 | } |
@@ -423,15 +423,15 @@ discard block |
||
| 423 | 423 | function FileGetState($NameOrIdx) { |
| 424 | 424 | |
| 425 | 425 | $idx = $this->FileGetIdx($NameOrIdx); |
| 426 | - if ($idx===false) { |
|
| 426 | + if ($idx === false) { |
|
| 427 | 427 | $idx = $this->FileGetIdxAdd($NameOrIdx); |
| 428 | - if ($idx===false) { |
|
| 428 | + if ($idx === false) { |
|
| 429 | 429 | return false; |
| 430 | 430 | } else { |
| 431 | 431 | return 'a'; |
| 432 | 432 | } |
| 433 | 433 | } elseif (isset($this->ReplInfo[$idx])) { |
| 434 | - if ($this->ReplInfo[$idx]===false) { |
|
| 434 | + if ($this->ReplInfo[$idx] === false) { |
|
| 435 | 435 | return 'd'; |
| 436 | 436 | } else { |
| 437 | 437 | return 'm'; |
@@ -442,7 +442,7 @@ discard block |
||
| 442 | 442 | |
| 443 | 443 | } |
| 444 | 444 | |
| 445 | - function FileCancelModif($NameOrIdx, $ReplacedAndDeleted=true) { |
|
| 445 | + function FileCancelModif($NameOrIdx, $ReplacedAndDeleted = true) { |
|
| 446 | 446 | // cancel added, modified or deleted modifications on a file in the archive |
| 447 | 447 | // return the number of cancels |
| 448 | 448 | |
@@ -451,7 +451,7 @@ discard block |
||
| 451 | 451 | if ($ReplacedAndDeleted) { |
| 452 | 452 | // replaced or deleted files |
| 453 | 453 | $idx = $this->FileGetIdx($NameOrIdx); |
| 454 | - if ($idx!==false) { |
|
| 454 | + if ($idx !== false) { |
|
| 455 | 455 | if (isset($this->ReplInfo[$idx])) { |
| 456 | 456 | $pos = $this->CdFileLst[$idx]['p_loc']; |
| 457 | 457 | unset($this->ReplByPos[$pos]); |
@@ -463,7 +463,7 @@ discard block |
||
| 463 | 463 | |
| 464 | 464 | // added files |
| 465 | 465 | $idx = $this->FileGetIdxAdd($NameOrIdx); |
| 466 | - if ($idx!==false) { |
|
| 466 | + if ($idx !== false) { |
|
| 467 | 467 | unset($this->AddInfo[$idx]); |
| 468 | 468 | $nbr++; |
| 469 | 469 | } |
@@ -472,10 +472,10 @@ discard block |
||
| 472 | 472 | |
| 473 | 473 | } |
| 474 | 474 | |
| 475 | - function Flush($Render=self::TBSZIP_DOWNLOAD, $File='', $ContentType='') { |
|
| 475 | + function Flush($Render = self::TBSZIP_DOWNLOAD, $File = '', $ContentType = '') { |
|
| 476 | 476 | |
| 477 | - if ( ($File!=='') && ($this->ArchFile===$File) && ($Render==self::TBSZIP_FILE) ) { |
|
| 478 | - $this->RaiseError('Method Flush() cannot overwrite the current opened archive: \''.$File.'\''); // this makes corrupted zip archives without PHP error. |
|
| 477 | + if (($File !== '') && ($this->ArchFile === $File) && ($Render == self::TBSZIP_FILE)) { |
|
| 478 | + $this->RaiseError('Method Flush() cannot overwrite the current opened archive: \'' . $File . '\''); // this makes corrupted zip archives without PHP error. |
|
| 479 | 479 | return false; |
| 480 | 480 | } |
| 481 | 481 | |
@@ -498,7 +498,7 @@ discard block |
||
| 498 | 498 | $this->OutputFromArch($ArchPos, $ReplPos); |
| 499 | 499 | // get current file information |
| 500 | 500 | if (!isset($this->VisFileLst[$ReplIdx])) $this->_ReadFile($ReplIdx, false); |
| 501 | - $FileInfo =& $this->VisFileLst[$ReplIdx]; |
|
| 501 | + $FileInfo = & $this->VisFileLst[$ReplIdx]; |
|
| 502 | 502 | $b1 = $FileInfo['bin']; |
| 503 | 503 | if (isset($FileInfo['desc_bin'])) { |
| 504 | 504 | $b2 = $FileInfo['desc_bin']; |
@@ -507,8 +507,8 @@ discard block |
||
| 507 | 507 | } |
| 508 | 508 | $info_old_len = strlen($b1) + $this->CdFileLst[$ReplIdx]['l_data_c'] + strlen($b2); // $FileInfo['l_data_c'] may have a 0 value in some archives |
| 509 | 509 | // get replacement information |
| 510 | - $ReplInfo =& $this->ReplInfo[$ReplIdx]; |
|
| 511 | - if ($ReplInfo===false) { |
|
| 510 | + $ReplInfo = & $this->ReplInfo[$ReplIdx]; |
|
| 511 | + if ($ReplInfo === false) { |
|
| 512 | 512 | // The file is to be deleted |
| 513 | 513 | $Delta = $Delta - $info_old_len; // headers and footers are also deleted |
| 514 | 514 | $DelLst[$ReplIdx] = true; |
@@ -520,22 +520,22 @@ discard block |
||
| 520 | 520 | $this->_PutDec($b1, $ReplInfo['crc32'], 14, 4); // crc32 |
| 521 | 521 | $this->_PutDec($b1, $ReplInfo['len_c'], 18, 4); // l_data_c |
| 522 | 522 | $this->_PutDec($b1, $ReplInfo['len_u'], 22, 4); // l_data_u |
| 523 | - if ($ReplInfo['meth']!==false) $this->_PutDec($b1, $ReplInfo['meth'], 8, 2); // meth |
|
| 523 | + if ($ReplInfo['meth'] !== false) $this->_PutDec($b1, $ReplInfo['meth'], 8, 2); // meth |
|
| 524 | 524 | // prepare the bottom description if the zipped file, if any |
| 525 | - if ($b2!=='') { |
|
| 526 | - $d = (strlen($b2)==16) ? 4 : 0; // offset because of the signature if any |
|
| 527 | - $this->_PutDec($b2, $ReplInfo['crc32'], $d+0, 4); // crc32 |
|
| 528 | - $this->_PutDec($b2, $ReplInfo['len_c'], $d+4, 4); // l_data_c |
|
| 529 | - $this->_PutDec($b2, $ReplInfo['len_u'], $d+8, 4); // l_data_u |
|
| 525 | + if ($b2 !== '') { |
|
| 526 | + $d = (strlen($b2) == 16) ? 4 : 0; // offset because of the signature if any |
|
| 527 | + $this->_PutDec($b2, $ReplInfo['crc32'], $d + 0, 4); // crc32 |
|
| 528 | + $this->_PutDec($b2, $ReplInfo['len_c'], $d + 4, 4); // l_data_c |
|
| 529 | + $this->_PutDec($b2, $ReplInfo['len_u'], $d + 8, 4); // l_data_u |
|
| 530 | 530 | } |
| 531 | 531 | // output data |
| 532 | - $this->OutputFromString($b1.$ReplInfo['data'].$b2); |
|
| 532 | + $this->OutputFromString($b1 . $ReplInfo['data'] . $b2); |
|
| 533 | 533 | unset($ReplInfo['data']); // save PHP memory |
| 534 | 534 | $Delta = $Delta + $ReplInfo['diff'] + $ReplInfo['len_c']; |
| 535 | 535 | } |
| 536 | 536 | // Update the delta of positions for zipped files which are physically after the currently replaced one |
| 537 | - for ($i=0;$i<$this->CdFileNbr;$i++) { |
|
| 538 | - if ($this->CdFileLst[$i]['p_loc']>$ReplPos) { |
|
| 537 | + for ($i = 0; $i < $this->CdFileNbr; $i++) { |
|
| 538 | + if ($this->CdFileLst[$i]['p_loc'] > $ReplPos) { |
|
| 539 | 539 | $FicNewPos[$i] = $this->CdFileLst[$i]['p_loc'] + $Delta; |
| 540 | 540 | } |
| 541 | 541 | } |
@@ -544,13 +544,13 @@ discard block |
||
| 544 | 544 | } |
| 545 | 545 | |
| 546 | 546 | // Ouput all the zipped files that remain before the Central Directory listing |
| 547 | - if ($this->ArchHnd!==false) $this->OutputFromArch($ArchPos, $this->CdPos); // ArchHnd is false if CreateNew() has been called |
|
| 547 | + if ($this->ArchHnd !== false) $this->OutputFromArch($ArchPos, $this->CdPos); // ArchHnd is false if CreateNew() has been called |
|
| 548 | 548 | $ArchPos = $this->CdPos; |
| 549 | 549 | |
| 550 | 550 | // Output file to add |
| 551 | 551 | $AddNbr = count($this->AddInfo); |
| 552 | 552 | $AddDataLen = 0; // total len of added data (inlcuding file headers) |
| 553 | - if ($AddNbr>0) { |
|
| 553 | + if ($AddNbr > 0) { |
|
| 554 | 554 | $AddPos = $ArchPos + $Delta; // position of the start |
| 555 | 555 | $AddLst = array_keys($this->AddInfo); |
| 556 | 556 | foreach ($AddLst as $idx) { |
@@ -563,32 +563,32 @@ discard block |
||
| 563 | 563 | // Modifiy file information in the Central Directory for replaced files |
| 564 | 564 | $b2 = ''; |
| 565 | 565 | $old_cd_len = 0; |
| 566 | - for ($i=0;$i<$this->CdFileNbr;$i++) { |
|
| 566 | + for ($i = 0; $i < $this->CdFileNbr; $i++) { |
|
| 567 | 567 | $b1 = $this->CdFileLst[$i]['bin']; |
| 568 | 568 | $old_cd_len += strlen($b1); |
| 569 | 569 | if (!isset($DelLst[$i])) { |
| 570 | - if (isset($FicNewPos[$i])) $this->_PutDec($b1, $FicNewPos[$i], 42, 4); // p_loc |
|
| 570 | + if (isset($FicNewPos[$i])) $this->_PutDec($b1, $FicNewPos[$i], 42, 4); // p_loc |
|
| 571 | 571 | if (isset($this->ReplInfo[$i])) { |
| 572 | - $ReplInfo =& $this->ReplInfo[$i]; |
|
| 572 | + $ReplInfo = & $this->ReplInfo[$i]; |
|
| 573 | 573 | $this->_PutDec($b1, $time, 12, 2); // time |
| 574 | 574 | $this->_PutDec($b1, $date, 14, 2); // date |
| 575 | 575 | $this->_PutDec($b1, $ReplInfo['crc32'], 16, 4); // crc32 |
| 576 | 576 | $this->_PutDec($b1, $ReplInfo['len_c'], 20, 4); // l_data_c |
| 577 | 577 | $this->_PutDec($b1, $ReplInfo['len_u'], 24, 4); // l_data_u |
| 578 | - if ($ReplInfo['meth']!==false) $this->_PutDec($b1, $ReplInfo['meth'], 10, 2); // meth |
|
| 578 | + if ($ReplInfo['meth'] !== false) $this->_PutDec($b1, $ReplInfo['meth'], 10, 2); // meth |
|
| 579 | 579 | } |
| 580 | 580 | $b2 .= $b1; |
| 581 | 581 | } |
| 582 | 582 | } |
| 583 | 583 | $this->OutputFromString($b2); |
| 584 | 584 | $ArchPos += $old_cd_len; |
| 585 | - $DeltaCdLen = $DeltaCdLen + strlen($b2) - $old_cd_len; |
|
| 585 | + $DeltaCdLen = $DeltaCdLen + strlen($b2) - $old_cd_len; |
|
| 586 | 586 | |
| 587 | 587 | // Output until "end of central directory record" |
| 588 | - if ($this->ArchHnd!==false) $this->OutputFromArch($ArchPos, $this->CdEndPos); // ArchHnd is false if CreateNew() has been called |
|
| 588 | + if ($this->ArchHnd !== false) $this->OutputFromArch($ArchPos, $this->CdEndPos); // ArchHnd is false if CreateNew() has been called |
|
| 589 | 589 | |
| 590 | 590 | // Output file information of the Central Directory for added files |
| 591 | - if ($AddNbr>0) { |
|
| 591 | + if ($AddNbr > 0) { |
|
| 592 | 592 | $b2 = ''; |
| 593 | 593 | foreach ($AddLst as $idx) { |
| 594 | 594 | $b2 .= $this->AddInfo[$idx]['bin']; |
@@ -600,10 +600,10 @@ discard block |
||
| 600 | 600 | // Output "end of central directory record" |
| 601 | 601 | $b2 = $this->CdInfo['bin']; |
| 602 | 602 | $DelNbr = count($DelLst); |
| 603 | - if ( ($AddNbr>0) or ($DelNbr>0) ) { |
|
| 603 | + if (($AddNbr > 0) or ($DelNbr > 0)) { |
|
| 604 | 604 | // total number of entries in the central directory on this disk |
| 605 | 605 | $n = $this->_GetDec($b2, 8, 2); |
| 606 | - $this->_PutDec($b2, $n + $AddNbr - $DelNbr, 8, 2); |
|
| 606 | + $this->_PutDec($b2, $n + $AddNbr - $DelNbr, 8, 2); |
|
| 607 | 607 | // total number of entries in the central directory |
| 608 | 608 | $n = $this->_GetDec($b2, 10, 2); |
| 609 | 609 | $this->_PutDec($b2, $n + $AddNbr - $DelNbr, 10, 2); |
@@ -612,7 +612,7 @@ discard block |
||
| 612 | 612 | $this->_PutDec($b2, $n + $DeltaCdLen, 12, 4); |
| 613 | 613 | $Delta = $Delta + $AddDataLen; |
| 614 | 614 | } |
| 615 | - $this->_PutDec($b2, $this->CdPos+$Delta , 16, 4); // p_cd (offset of start of central directory with respect to the starting disk number) |
|
| 615 | + $this->_PutDec($b2, $this->CdPos + $Delta, 16, 4); // p_cd (offset of start of central directory with respect to the starting disk number) |
|
| 616 | 616 | $this->OutputFromString($b2); |
| 617 | 617 | |
| 618 | 618 | $this->OutputClose(); |
@@ -627,32 +627,32 @@ discard block |
||
| 627 | 627 | |
| 628 | 628 | function OutputOpen($Render, $File, $ContentType) { |
| 629 | 629 | |
| 630 | - if (($Render & self::TBSZIP_FILE)==self::TBSZIP_FILE) { |
|
| 630 | + if (($Render & self::TBSZIP_FILE) == self::TBSZIP_FILE) { |
|
| 631 | 631 | $this->OutputMode = self::TBSZIP_FILE; |
| 632 | - if (''.$File=='') $File = basename($this->ArchFile).'.zip'; |
|
| 632 | + if ('' . $File == '') $File = basename($this->ArchFile) . '.zip'; |
|
| 633 | 633 | $this->OutputHandle = @fopen($File, 'w'); |
| 634 | - if ($this->OutputHandle===false) { |
|
| 635 | - return $this->RaiseError('Method Flush() cannot overwrite the target file \''.$File.'\'. This may not be a valid file path or the file may be locked by another process or because of a denied permission.'); |
|
| 634 | + if ($this->OutputHandle === false) { |
|
| 635 | + return $this->RaiseError('Method Flush() cannot overwrite the target file \'' . $File . '\'. This may not be a valid file path or the file may be locked by another process or because of a denied permission.'); |
|
| 636 | 636 | } |
| 637 | - } elseif (($Render & self::TBSZIP_STRING)==self::TBSZIP_STRING) { |
|
| 637 | + } elseif (($Render & self::TBSZIP_STRING) == self::TBSZIP_STRING) { |
|
| 638 | 638 | $this->OutputMode = self::TBSZIP_STRING; |
| 639 | 639 | $this->OutputSrc = ''; |
| 640 | - } elseif (($Render & self::TBSZIP_DOWNLOAD)==self::TBSZIP_DOWNLOAD) { |
|
| 640 | + } elseif (($Render & self::TBSZIP_DOWNLOAD) == self::TBSZIP_DOWNLOAD) { |
|
| 641 | 641 | $this->OutputMode = self::TBSZIP_DOWNLOAD; |
| 642 | 642 | // Output the file |
| 643 | - if (''.$File=='') $File = basename($this->ArchFile); |
|
| 644 | - if (($Render & self::TBSZIP_NOHEADER)==self::TBSZIP_NOHEADER) { |
|
| 643 | + if ('' . $File == '') $File = basename($this->ArchFile); |
|
| 644 | + if (($Render & self::TBSZIP_NOHEADER) == self::TBSZIP_NOHEADER) { |
|
| 645 | 645 | } else { |
| 646 | - header ('Pragma: no-cache'); |
|
| 647 | - if ($ContentType!='') header ('Content-Type: '.$ContentType); |
|
| 648 | - header('Content-Disposition: attachment; filename="'.$File.'"'); |
|
| 646 | + header('Pragma: no-cache'); |
|
| 647 | + if ($ContentType != '') header('Content-Type: ' . $ContentType); |
|
| 648 | + header('Content-Disposition: attachment; filename="' . $File . '"'); |
|
| 649 | 649 | header('Expires: 0'); |
| 650 | 650 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
| 651 | 651 | header('Cache-Control: public'); |
| 652 | 652 | header('Content-Description: File Transfer'); |
| 653 | 653 | header('Content-Transfer-Encoding: binary'); |
| 654 | 654 | $Len = $this->_EstimateNewArchSize(); |
| 655 | - if ($Len!==false) header('Content-Length: '.$Len); |
|
| 655 | + if ($Len !== false) header('Content-Length: ' . $Len); |
|
| 656 | 656 | } |
| 657 | 657 | } else { |
| 658 | 658 | return $this->RaiseError('Method Flush is called with a unsupported render option.'); |
@@ -664,10 +664,10 @@ discard block |
||
| 664 | 664 | |
| 665 | 665 | function OutputFromArch($pos, $pos_stop) { |
| 666 | 666 | $len = $pos_stop - $pos; |
| 667 | - if ($len<0) return; |
|
| 667 | + if ($len < 0) return; |
|
| 668 | 668 | $this->_MoveTo($pos); |
| 669 | 669 | $block = 1024; |
| 670 | - while ($len>0) { |
|
| 670 | + while ($len > 0) { |
|
| 671 | 671 | $l = min($len, $block); |
| 672 | 672 | $x = $this->_ReadData($l); |
| 673 | 673 | $this->OutputFromString($x); |
@@ -677,9 +677,9 @@ discard block |
||
| 677 | 677 | } |
| 678 | 678 | |
| 679 | 679 | function OutputFromString($data) { |
| 680 | - if ($this->OutputMode===self::TBSZIP_DOWNLOAD) { |
|
| 680 | + if ($this->OutputMode === self::TBSZIP_DOWNLOAD) { |
|
| 681 | 681 | echo $data; // donwload |
| 682 | - } elseif ($this->OutputMode===self::TBSZIP_STRING) { |
|
| 682 | + } elseif ($this->OutputMode === self::TBSZIP_STRING) { |
|
| 683 | 683 | $this->OutputSrc .= $data; // to string |
| 684 | 684 | } elseif (self::TBSZIP_FILE) { |
| 685 | 685 | fwrite($this->OutputHandle, $data); // to file |
@@ -687,7 +687,7 @@ discard block |
||
| 687 | 687 | } |
| 688 | 688 | |
| 689 | 689 | function OutputClose() { |
| 690 | - if ( ($this->OutputMode===self::TBSZIP_FILE) && ($this->OutputHandle!==false) ) { |
|
| 690 | + if (($this->OutputMode === self::TBSZIP_FILE) && ($this->OutputHandle !== false)) { |
|
| 691 | 691 | fclose($this->OutputHandle); |
| 692 | 692 | $this->OutputHandle = false; |
| 693 | 693 | } |
@@ -702,7 +702,7 @@ discard block |
||
| 702 | 702 | } |
| 703 | 703 | |
| 704 | 704 | function _ReadData($len) { |
| 705 | - if ($len>0) { |
|
| 705 | + if ($len > 0) { |
|
| 706 | 706 | $x = fread($this->ArchHnd, $len); |
| 707 | 707 | return $x; |
| 708 | 708 | } else { |
@@ -717,32 +717,32 @@ discard block |
||
| 717 | 717 | function _GetDec($txt, $pos, $len) { |
| 718 | 718 | $x = substr($txt, $pos, $len); |
| 719 | 719 | $z = 0; |
| 720 | - for ($i=0;$i<$len;$i++) { |
|
| 720 | + for ($i = 0; $i < $len; $i++) { |
|
| 721 | 721 | $asc = ord($x[$i]); |
| 722 | - if ($asc>0) $z = $z + $asc*pow(256,$i); |
|
| 722 | + if ($asc > 0) $z = $z + $asc * pow(256, $i); |
|
| 723 | 723 | } |
| 724 | 724 | return $z; |
| 725 | 725 | } |
| 726 | 726 | |
| 727 | 727 | function _GetHex($txt, $pos, $len) { |
| 728 | 728 | $x = substr($txt, $pos, $len); |
| 729 | - return 'h:'.bin2hex(strrev($x)); |
|
| 729 | + return 'h:' . bin2hex(strrev($x)); |
|
| 730 | 730 | } |
| 731 | 731 | |
| 732 | 732 | function _GetBin($txt, $pos, $len) { |
| 733 | 733 | $x = substr($txt, $pos, $len); |
| 734 | 734 | $z = ''; |
| 735 | - for ($i=0;$i<$len;$i++) { |
|
| 735 | + for ($i = 0; $i < $len; $i++) { |
|
| 736 | 736 | $asc = ord($x[$i]); |
| 737 | 737 | if (isset($x[$i])) { |
| 738 | - for ($j=0;$j<8;$j++) { |
|
| 739 | - $z .= ($asc & pow(2,$j)) ? '1' : '0'; |
|
| 738 | + for ($j = 0; $j < 8; $j++) { |
|
| 739 | + $z .= ($asc & pow(2, $j)) ? '1' : '0'; |
|
| 740 | 740 | } |
| 741 | 741 | } else { |
| 742 | 742 | $z .= '00000000'; |
| 743 | 743 | } |
| 744 | 744 | } |
| 745 | - return 'b:'.$z; |
|
| 745 | + return 'b:' . $z; |
|
| 746 | 746 | } |
| 747 | 747 | |
| 748 | 748 | // ---------------- |
@@ -751,17 +751,17 @@ discard block |
||
| 751 | 751 | |
| 752 | 752 | function _PutDec(&$txt, $val, $pos, $len) { |
| 753 | 753 | $x = ''; |
| 754 | - for ($i=0;$i<$len;$i++) { |
|
| 755 | - if ($val==0) { |
|
| 754 | + for ($i = 0; $i < $len; $i++) { |
|
| 755 | + if ($val == 0) { |
|
| 756 | 756 | $z = 0; |
| 757 | 757 | } else { |
| 758 | 758 | $z = intval($val % 256); |
| 759 | - if (($val<0) && ($z!=0)) { // ($z!=0) is very important, example: val=-420085702 |
|
| 759 | + if (($val < 0) && ($z != 0)) { // ($z!=0) is very important, example: val=-420085702 |
|
| 760 | 760 | // special opration for negative value. If the number id too big, PHP stores it into a signed integer. For example: crc32('coucou') => -256185401 instead of 4038781895. NegVal = BigVal - (MaxVal+1) = BigVal - 256^4 |
| 761 | - $val = ($val - $z)/256 -1; |
|
| 761 | + $val = ($val - $z) / 256 - 1; |
|
| 762 | 762 | $z = 256 + $z; |
| 763 | 763 | } else { |
| 764 | - $val = ($val - $z)/256; |
|
| 764 | + $val = ($val - $z) / 256; |
|
| 765 | 765 | } |
| 766 | 766 | } |
| 767 | 767 | $x .= chr($z); |
@@ -771,30 +771,30 @@ discard block |
||
| 771 | 771 | |
| 772 | 772 | function _MsDos_Date($Timestamp = false) { |
| 773 | 773 | // convert a date-time timstamp into the MS-Dos format |
| 774 | - $d = ($Timestamp===false) ? getdate() : getdate($Timestamp); |
|
| 775 | - return (($d['year']-1980)*512) + ($d['mon']*32) + $d['mday']; |
|
| 774 | + $d = ($Timestamp === false) ? getdate() : getdate($Timestamp); |
|
| 775 | + return (($d['year'] - 1980) * 512) + ($d['mon'] * 32) + $d['mday']; |
|
| 776 | 776 | } |
| 777 | 777 | function _MsDos_Time($Timestamp = false) { |
| 778 | 778 | // convert a date-time timstamp into the MS-Dos format |
| 779 | - $d = ($Timestamp===false) ? getdate() : getdate($Timestamp); |
|
| 780 | - return ($d['hours']*2048) + ($d['minutes']*32) + intval($d['seconds']/2); // seconds are rounded to an even number in order to save 1 bit |
|
| 779 | + $d = ($Timestamp === false) ? getdate() : getdate($Timestamp); |
|
| 780 | + return ($d['hours'] * 2048) + ($d['minutes'] * 32) + intval($d['seconds'] / 2); // seconds are rounded to an even number in order to save 1 bit |
|
| 781 | 781 | } |
| 782 | 782 | |
| 783 | 783 | function _MsDos_Debug($date, $time) { |
| 784 | 784 | // Display the formated date and time. Just for debug purpose. |
| 785 | 785 | // date end time are encoded on 16 bits (2 bytes) : date = yyyyyyymmmmddddd , time = hhhhhnnnnnssssss |
| 786 | - $y = ($date & 65024)/512 + 1980; |
|
| 787 | - $m = ($date & 480)/32; |
|
| 786 | + $y = ($date & 65024) / 512 + 1980; |
|
| 787 | + $m = ($date & 480) / 32; |
|
| 788 | 788 | $d = ($date & 31); |
| 789 | - $h = ($time & 63488)/2048; |
|
| 790 | - $i = ($time & 1984)/32; |
|
| 789 | + $h = ($time & 63488) / 2048; |
|
| 790 | + $i = ($time & 1984) / 32; |
|
| 791 | 791 | $s = ($time & 31) * 2; // seconds have been rounded to an even number in order to save 1 bit |
| 792 | - return $y.'-'.str_pad($m,2,'0',STR_PAD_LEFT).'-'.str_pad($d,2,'0',STR_PAD_LEFT).' '.str_pad($h,2,'0',STR_PAD_LEFT).':'.str_pad($i,2,'0',STR_PAD_LEFT).':'.str_pad($s,2,'0',STR_PAD_LEFT); |
|
| 792 | + return $y . '-' . str_pad($m, 2, '0', STR_PAD_LEFT) . '-' . str_pad($d, 2, '0', STR_PAD_LEFT) . ' ' . str_pad($h, 2, '0', STR_PAD_LEFT) . ':' . str_pad($i, 2, '0', STR_PAD_LEFT) . ':' . str_pad($s, 2, '0', STR_PAD_LEFT); |
|
| 793 | 793 | } |
| 794 | 794 | |
| 795 | 795 | function _TxtPos($pos) { |
| 796 | 796 | // Return the human readable position in both decimal and hexa |
| 797 | - return $pos." (h:".dechex($pos).")"; |
|
| 797 | + return $pos . " (h:" . dechex($pos) . ")"; |
|
| 798 | 798 | } |
| 799 | 799 | |
| 800 | 800 | /** |
@@ -807,15 +807,15 @@ discard block |
||
| 807 | 807 | $nbr = 1; |
| 808 | 808 | $p = false; |
| 809 | 809 | $pos = ftell($this->ArchHnd) - 4 - 256; |
| 810 | - while ( ($p===false) && ($nbr<256) ) { |
|
| 811 | - if ($pos<=0) { |
|
| 810 | + while (($p === false) && ($nbr < 256)) { |
|
| 811 | + if ($pos <= 0) { |
|
| 812 | 812 | $pos = 0; |
| 813 | 813 | $nbr = 256; // in order to make this a last check |
| 814 | 814 | } |
| 815 | 815 | $this->_MoveTo($pos); |
| 816 | 816 | $x = $this->_ReadData(256); |
| 817 | 817 | $p = strpos($x, $cd_info); |
| 818 | - if ($p===false) { |
|
| 818 | + if ($p === false) { |
|
| 819 | 819 | $nbr++; |
| 820 | 820 | $pos = $pos - 256 - 256; |
| 821 | 821 | } else { |
@@ -827,7 +827,7 @@ discard block |
||
| 827 | 827 | |
| 828 | 828 | function _DataOuputAddedFile($Idx, $PosLoc) { |
| 829 | 829 | |
| 830 | - $Ref =& $this->AddInfo[$Idx]; |
|
| 830 | + $Ref = & $this->AddInfo[$Idx]; |
|
| 831 | 831 | $this->_DataPrepare($Ref); // get data from external file if necessary |
| 832 | 832 | |
| 833 | 833 | // Other info |
@@ -835,46 +835,46 @@ discard block |
||
| 835 | 835 | $date = $this->_MsDos_Date($file_time); |
| 836 | 836 | $time = $this->_MsDos_Time($file_time); |
| 837 | 837 | $len_n = strlen($Ref['name']); |
| 838 | - $purp = 2048 ; // purpose // +8 to indicates that there is an extended local header |
|
| 838 | + $purp = 2048; // purpose // +8 to indicates that there is an extended local header |
|
| 839 | 839 | |
| 840 | 840 | // Header for file in the data section |
| 841 | - $b = 'PK'.chr(03).chr(04).str_repeat(' ',26); // signature |
|
| 842 | - $this->_PutDec($b,20,4,2); //vers = 20 |
|
| 843 | - $this->_PutDec($b,$purp,6,2); // purp |
|
| 844 | - $this->_PutDec($b,$Ref['meth'],8,2); // meth |
|
| 845 | - $this->_PutDec($b,$time,10,2); // time |
|
| 846 | - $this->_PutDec($b,$date,12,2); // date |
|
| 847 | - $this->_PutDec($b,$Ref['crc32'],14,4); // crc32 |
|
| 848 | - $this->_PutDec($b,$Ref['len_c'],18,4); // l_data_c |
|
| 849 | - $this->_PutDec($b,$Ref['len_u'],22,4); // l_data_u |
|
| 850 | - $this->_PutDec($b,$len_n,26,2); // l_name |
|
| 851 | - $this->_PutDec($b,0,28,2); // l_fields |
|
| 841 | + $b = 'PK' . chr(03) . chr(04) . str_repeat(' ', 26); // signature |
|
| 842 | + $this->_PutDec($b, 20, 4, 2); //vers = 20 |
|
| 843 | + $this->_PutDec($b, $purp, 6, 2); // purp |
|
| 844 | + $this->_PutDec($b, $Ref['meth'], 8, 2); // meth |
|
| 845 | + $this->_PutDec($b, $time, 10, 2); // time |
|
| 846 | + $this->_PutDec($b, $date, 12, 2); // date |
|
| 847 | + $this->_PutDec($b, $Ref['crc32'], 14, 4); // crc32 |
|
| 848 | + $this->_PutDec($b, $Ref['len_c'], 18, 4); // l_data_c |
|
| 849 | + $this->_PutDec($b, $Ref['len_u'], 22, 4); // l_data_u |
|
| 850 | + $this->_PutDec($b, $len_n, 26, 2); // l_name |
|
| 851 | + $this->_PutDec($b, 0, 28, 2); // l_fields |
|
| 852 | 852 | $b .= $Ref['name']; // name |
| 853 | 853 | $b .= ''; // fields |
| 854 | 854 | |
| 855 | 855 | // Output the data |
| 856 | - $this->OutputFromString($b.$Ref['data']); |
|
| 856 | + $this->OutputFromString($b . $Ref['data']); |
|
| 857 | 857 | $OutputLen = strlen($b) + $Ref['len_c']; // new position of the cursor |
| 858 | 858 | unset($Ref['data']); // save PHP memory |
| 859 | 859 | |
| 860 | 860 | // Information for file in the Central Directory |
| 861 | - $b = 'PK'.chr(01).chr(02).str_repeat(' ',42); // signature |
|
| 862 | - $this->_PutDec($b,20,4,2); // vers_used = 20 |
|
| 863 | - $this->_PutDec($b,20,6,2); // vers_necess = 20 |
|
| 864 | - $this->_PutDec($b,$purp,8,2); // purp |
|
| 865 | - $this->_PutDec($b,$Ref['meth'],10,2); // meth |
|
| 866 | - $this->_PutDec($b,$time,12,2); // time |
|
| 867 | - $this->_PutDec($b,$date,14,2); // date |
|
| 868 | - $this->_PutDec($b,$Ref['crc32'],16,4); // crc32 |
|
| 869 | - $this->_PutDec($b,$Ref['len_c'],20,4); // l_data_c |
|
| 870 | - $this->_PutDec($b,$Ref['len_u'],24,4); // l_data_u |
|
| 871 | - $this->_PutDec($b,$len_n,28,2); // l_name |
|
| 872 | - $this->_PutDec($b,0,30,2); // l_fields |
|
| 873 | - $this->_PutDec($b,0,32,2); // l_comm |
|
| 874 | - $this->_PutDec($b,0,34,2); // disk_num |
|
| 875 | - $this->_PutDec($b,0,36,2); // int_file_att |
|
| 876 | - $this->_PutDec($b,0,38,4); // ext_file_att |
|
| 877 | - $this->_PutDec($b,$PosLoc,42,4); // p_loc |
|
| 861 | + $b = 'PK' . chr(01) . chr(02) . str_repeat(' ', 42); // signature |
|
| 862 | + $this->_PutDec($b, 20, 4, 2); // vers_used = 20 |
|
| 863 | + $this->_PutDec($b, 20, 6, 2); // vers_necess = 20 |
|
| 864 | + $this->_PutDec($b, $purp, 8, 2); // purp |
|
| 865 | + $this->_PutDec($b, $Ref['meth'], 10, 2); // meth |
|
| 866 | + $this->_PutDec($b, $time, 12, 2); // time |
|
| 867 | + $this->_PutDec($b, $date, 14, 2); // date |
|
| 868 | + $this->_PutDec($b, $Ref['crc32'], 16, 4); // crc32 |
|
| 869 | + $this->_PutDec($b, $Ref['len_c'], 20, 4); // l_data_c |
|
| 870 | + $this->_PutDec($b, $Ref['len_u'], 24, 4); // l_data_u |
|
| 871 | + $this->_PutDec($b, $len_n, 28, 2); // l_name |
|
| 872 | + $this->_PutDec($b, 0, 30, 2); // l_fields |
|
| 873 | + $this->_PutDec($b, 0, 32, 2); // l_comm |
|
| 874 | + $this->_PutDec($b, 0, 34, 2); // disk_num |
|
| 875 | + $this->_PutDec($b, 0, 36, 2); // int_file_att |
|
| 876 | + $this->_PutDec($b, 0, 38, 4); // ext_file_att |
|
| 877 | + $this->_PutDec($b, $PosLoc, 42, 4); // p_loc |
|
| 878 | 878 | $b .= $Ref['name']; // v_name |
| 879 | 879 | $b .= ''; // v_fields |
| 880 | 880 | $b .= ''; // v_comm |
@@ -908,7 +908,7 @@ discard block |
||
| 908 | 908 | } |
| 909 | 909 | |
| 910 | 910 | // TODO support for data taken from okapi cache |
| 911 | - if ($DataType==self::TBSZIP_STRING) { |
|
| 911 | + if ($DataType == self::TBSZIP_STRING) { |
|
| 912 | 912 | $path = false; |
| 913 | 913 | if ($Compress) { |
| 914 | 914 | // we compress now in order to save PHP memory |
@@ -918,7 +918,7 @@ discard block |
||
| 918 | 918 | $len_c = strlen($Data); |
| 919 | 919 | } else { |
| 920 | 920 | $len_c = strlen($Data); |
| 921 | - if ($len_u===false) { |
|
| 921 | + if ($len_u === false) { |
|
| 922 | 922 | $len_u = $len_c; |
| 923 | 923 | $crc32 = crc32($Data); |
| 924 | 924 | } |
@@ -929,11 +929,11 @@ discard block |
||
| 929 | 929 | $fi = stat($path); |
| 930 | 930 | if ($fi !== false) { |
| 931 | 931 | $fz = $fi['size']; |
| 932 | - if ($len_u===false) $len_u = $fz; |
|
| 932 | + if ($len_u === false) $len_u = $fz; |
|
| 933 | 933 | $len_c = ($Compress) ? false : $fz; |
| 934 | 934 | $file_time = $fi['mtime']; |
| 935 | 935 | } else { |
| 936 | - return $this->RaiseError("Cannot add the file '".$path."' because it is not found."); |
|
| 936 | + return $this->RaiseError("Cannot add the file '" . $path . "' because it is not found."); |
|
| 937 | 937 | } |
| 938 | 938 | } |
| 939 | 939 | |
@@ -946,10 +946,10 @@ discard block |
||
| 946 | 946 | function _DataPrepare(&$Ref) { |
| 947 | 947 | // returns the real size of data |
| 948 | 948 | // TODO: support for data returned from okapi cache |
| 949 | - if ($Ref['path']!==false) { |
|
| 949 | + if ($Ref['path'] !== false) { |
|
| 950 | 950 | $Ref['data'] = file_get_contents($Ref['path']); |
| 951 | - if ($Ref['crc32']===false) $Ref['crc32'] = crc32($Ref['data']); |
|
| 952 | - if ($Ref['len_c']===false) { |
|
| 951 | + if ($Ref['crc32'] === false) $Ref['crc32'] = crc32($Ref['data']); |
|
| 952 | + if ($Ref['len_c'] === false) { |
|
| 953 | 953 | // means the data must be compressed |
| 954 | 954 | $Ref['data'] = gzdeflate($Ref['data']); |
| 955 | 955 | $Ref['len_c'] = strlen($Ref['data']); |
@@ -959,7 +959,7 @@ discard block |
||
| 959 | 959 | /** |
| 960 | 960 | * Return the size of the new archive, or false if it cannot be calculated (because of external file that must be compressed before to be insered) |
| 961 | 961 | */ |
| 962 | - function _EstimateNewArchSize($Optim=true) { |
|
| 962 | + function _EstimateNewArchSize($Optim = true) { |
|
| 963 | 963 | |
| 964 | 964 | if ($this->ArchIsNew) { |
| 965 | 965 | $Len = strlen($this->CdInfo['bin']); |
@@ -972,19 +972,19 @@ discard block |
||
| 972 | 972 | |
| 973 | 973 | // files to replace or delete |
| 974 | 974 | foreach ($this->ReplByPos as $i) { |
| 975 | - $Ref =& $this->ReplInfo[$i]; |
|
| 976 | - if ($Ref===false) { |
|
| 975 | + $Ref = & $this->ReplInfo[$i]; |
|
| 976 | + if ($Ref === false) { |
|
| 977 | 977 | // file to delete |
| 978 | - $Info =& $this->CdFileLst[$i]; |
|
| 978 | + $Info = & $this->CdFileLst[$i]; |
|
| 979 | 979 | if (!isset($this->VisFileLst[$i])) { |
| 980 | 980 | if ($Optim) return false; // if $Optimization is set to true, then we d'ont rewind to read information |
| 981 | 981 | $this->_MoveTo($Info['p_loc']); |
| 982 | 982 | $this->_ReadFile($i, false); |
| 983 | 983 | } |
| 984 | - $Vis =& $this->VisFileLst[$i]; |
|
| 985 | - $Len += -strlen($Vis['bin']) -strlen($Info['bin']) - $Info['l_data_c']; |
|
| 984 | + $Vis = & $this->VisFileLst[$i]; |
|
| 985 | + $Len += -strlen($Vis['bin']) - strlen($Info['bin']) - $Info['l_data_c']; |
|
| 986 | 986 | if (isset($Vis['desc_bin'])) $Len += -strlen($Vis['desc_bin']); |
| 987 | - } elseif ($Ref['len_c']===false) { |
|
| 987 | + } elseif ($Ref['len_c'] === false) { |
|
| 988 | 988 | return false; // information not yet known |
| 989 | 989 | } else { |
| 990 | 990 | // file to replace |
@@ -995,8 +995,8 @@ discard block |
||
| 995 | 995 | // files to add |
| 996 | 996 | $i_lst = array_keys($this->AddInfo); |
| 997 | 997 | foreach ($i_lst as $i) { |
| 998 | - $Ref =& $this->AddInfo[$i]; |
|
| 999 | - if ($Ref['len_c']===false) { |
|
| 998 | + $Ref = & $this->AddInfo[$i]; |
|
| 999 | + if ($Ref['len_c'] === false) { |
|
| 1000 | 1000 | return false; // information not yet known |
| 1001 | 1001 | } else { |
| 1002 | 1002 | $Len += $Ref['len_c'] + $Ref['diff']; |
@@ -26,7 +26,10 @@ discard block |
||
| 26 | 26 | |
| 27 | 27 | function CreateNew($ArchName='new.zip') { |
| 28 | 28 | // Create a new virtual empty archive, the name will be the default name when the archive is flushed. |
| 29 | - if (!isset($this->Meth8Ok)) $this->__construct(); // for PHP 4 compatibility |
|
| 29 | + if (!isset($this->Meth8Ok)) { |
|
| 30 | + $this->__construct(); |
|
| 31 | + } |
|
| 32 | + // for PHP 4 compatibility |
|
| 30 | 33 | $this->Close(); // note that $this->ArchHnd is set to false here |
| 31 | 34 | $this->Error = false; |
| 32 | 35 | $this->ArchFile = $ArchName; |
@@ -39,7 +42,10 @@ discard block |
||
| 39 | 42 | |
| 40 | 43 | function Open($ArchFile, $UseIncludePath=false) { |
| 41 | 44 | // Open the zip archive |
| 42 | - if (!isset($this->Meth8Ok)) $this->__construct(); // for PHP 4 compatibility |
|
| 45 | + if (!isset($this->Meth8Ok)) { |
|
| 46 | + $this->__construct(); |
|
| 47 | + } |
|
| 48 | + // for PHP 4 compatibility |
|
| 43 | 49 | $this->Close(); // close handle and init info |
| 44 | 50 | $this->Error = false; |
| 45 | 51 | $this->ArchIsNew = false; |
@@ -53,12 +59,16 @@ discard block |
||
| 53 | 59 | $this->ArchHnd = fopen($ArchFile, 'rb', $UseIncludePath); |
| 54 | 60 | } |
| 55 | 61 | $ok = !($this->ArchHnd===false); |
| 56 | - if ($ok) $ok = $this->CentralDirRead(); |
|
| 62 | + if ($ok) { |
|
| 63 | + $ok = $this->CentralDirRead(); |
|
| 64 | + } |
|
| 57 | 65 | return $ok; |
| 58 | 66 | } |
| 59 | 67 | |
| 60 | 68 | function Close() { |
| 61 | - if (isset($this->ArchHnd) and ($this->ArchHnd!==false)) fclose($this->ArchHnd); |
|
| 69 | + if (isset($this->ArchHnd) and ($this->ArchHnd!==false)) { |
|
| 70 | + fclose($this->ArchHnd); |
|
| 71 | + } |
|
| 62 | 72 | $this->ArchFile = ''; |
| 63 | 73 | $this->ArchHnd = false; |
| 64 | 74 | $this->CdInfo = array(); |
@@ -79,12 +89,17 @@ discard block |
||
| 79 | 89 | |
| 80 | 90 | function FileAdd($Name, $Data, $DataType=self::TBSZIP_STRING, $Compress=true) { |
| 81 | 91 | |
| 82 | - if ($Data===false) return $this->FileCancelModif($Name, false); // Cancel a previously added file |
|
| 92 | + if ($Data===false) { |
|
| 93 | + return $this->FileCancelModif($Name, false); |
|
| 94 | + } |
|
| 95 | + // Cancel a previously added file |
|
| 83 | 96 | |
| 84 | 97 | // Save information for adding a new file into the archive |
| 85 | 98 | $Diff = 30 + 46 + 2*strlen($Name); // size of the header + cd info |
| 86 | 99 | $Ref = $this->_DataCreateNewRef($Data, $DataType, $Compress, $Diff, $Name); |
| 87 | - if ($Ref===false) return false; |
|
| 100 | + if ($Ref===false) { |
|
| 101 | + return false; |
|
| 102 | + } |
|
| 88 | 103 | $Ref['name'] = $Name; |
| 89 | 104 | $this->AddInfo[] = $Ref; |
| 90 | 105 | return $Ref['res']; |
@@ -113,8 +128,12 @@ discard block |
||
| 113 | 128 | $this->CdFileNbr = $this->CdInfo['file_nbr_curr']; |
| 114 | 129 | $this->CdPos = $this->CdInfo['p_cd']; |
| 115 | 130 | |
| 116 | - if ($this->CdFileNbr<=0) return $this->RaiseError('No header found in the Central Directory.'); |
|
| 117 | - if ($this->CdPos<=0) return $this->RaiseError('No position found for the Central Directory.'); |
|
| 131 | + if ($this->CdFileNbr<=0) { |
|
| 132 | + return $this->RaiseError('No header found in the Central Directory.'); |
|
| 133 | + } |
|
| 134 | + if ($this->CdPos<=0) { |
|
| 135 | + return $this->RaiseError('No position found for the Central Directory.'); |
|
| 136 | + } |
|
| 118 | 137 | |
| 119 | 138 | $this->_MoveTo($this->CdPos); |
| 120 | 139 | for ($i=0;$i<$this->CdFileNbr;$i++) { |
@@ -147,7 +166,9 @@ discard block |
||
| 147 | 166 | $b = $this->_ReadData(46); |
| 148 | 167 | |
| 149 | 168 | $x = $this->_GetHex($b,0,4); |
| 150 | - if ($x!=='h:02014b50') return $this->RaiseError("Signature of Central Directory Header #".$idx." (file information) expected but not found at position ".$this->_TxtPos(ftell($this->ArchHnd) - 46)."."); |
|
| 169 | + if ($x!=='h:02014b50') { |
|
| 170 | + return $this->RaiseError("Signature of Central Directory Header #".$idx." (file information) expected but not found at position ".$this->_TxtPos(ftell($this->ArchHnd) - 46)."."); |
|
| 171 | + } |
|
| 151 | 172 | |
| 152 | 173 | $x = array(); |
| 153 | 174 | $x['vers_used'] = $this->_GetDec($b,4,2); |
@@ -264,10 +285,14 @@ discard block |
||
| 264 | 285 | |
| 265 | 286 | function FileGetIdxAdd($Name) { |
| 266 | 287 | // Check if a file name exists in the list of file to add, and return its index |
| 267 | - if (!is_string($Name)) return false; |
|
| 288 | + if (!is_string($Name)) { |
|
| 289 | + return false; |
|
| 290 | + } |
|
| 268 | 291 | $idx_lst = array_keys($this->AddInfo); |
| 269 | 292 | foreach ($idx_lst as $idx) { |
| 270 | - if ($this->AddInfo[$idx]['name']===$Name) return $idx; |
|
| 293 | + if ($this->AddInfo[$idx]['name']===$Name) { |
|
| 294 | + return $idx; |
|
| 295 | + } |
|
| 271 | 296 | } |
| 272 | 297 | return false; |
| 273 | 298 | } |
@@ -278,7 +303,9 @@ discard block |
||
| 278 | 303 | $this->LastReadIdx = false; |
| 279 | 304 | |
| 280 | 305 | $idx = $this->FileGetIdx($NameOrIdx); |
| 281 | - if ($idx===false) return $this->RaiseError('File "'.$NameOrIdx.'" is not found in the Central Directory.'); |
|
| 306 | + if ($idx===false) { |
|
| 307 | + return $this->RaiseError('File "'.$NameOrIdx.'" is not found in the Central Directory.'); |
|
| 308 | + } |
|
| 282 | 309 | |
| 283 | 310 | $pos = $this->CdFileLst[$idx]['p_loc']; |
| 284 | 311 | $this->_MoveTo($pos); |
@@ -302,7 +329,9 @@ discard block |
||
| 302 | 329 | } elseif($meth==0) { |
| 303 | 330 | $Comp = 0; // means stored without compression |
| 304 | 331 | } else { |
| 305 | - if ($Uncompress) $this->RaiseError('Unable to uncompress file "'.$NameOrIdx.'" because it is compressed with method '.$meth.'.'); |
|
| 332 | + if ($Uncompress) { |
|
| 333 | + $this->RaiseError('Unable to uncompress file "'.$NameOrIdx.'" because it is compressed with method '.$meth.'.'); |
|
| 334 | + } |
|
| 306 | 335 | } |
| 307 | 336 | $this->LastReadComp = $Comp; |
| 308 | 337 | |
@@ -316,7 +345,9 @@ discard block |
||
| 316 | 345 | $b = $this->_ReadData(30); |
| 317 | 346 | |
| 318 | 347 | $x = $this->_GetHex($b,0,4); |
| 319 | - if ($x!=='h:04034b50') return $this->RaiseError("Signature of Local File Header #".$idx." (data section) expected but not found at position ".$this->_TxtPos(ftell($this->ArchHnd)-30)."."); |
|
| 348 | + if ($x!=='h:04034b50') { |
|
| 349 | + return $this->RaiseError("Signature of Local File Header #".$idx." (data section) expected but not found at position ".$this->_TxtPos(ftell($this->ArchHnd)-30)."."); |
|
| 350 | + } |
|
| 320 | 351 | |
| 321 | 352 | $x = array(); |
| 322 | 353 | $x['vers'] = $this->_GetDec($b,4,2); |
@@ -348,7 +379,9 @@ discard block |
||
| 348 | 379 | } |
| 349 | 380 | } else { |
| 350 | 381 | $len = $x['l_data_c']; |
| 351 | - if ($len==0) $this->RaiseError("File Data #".$idx." cannt be read because no length is specified in the Local File Header and its Central Directory information has not been found."); |
|
| 382 | + if ($len==0) { |
|
| 383 | + $this->RaiseError("File Data #".$idx." cannt be read because no length is specified in the Local File Header and its Central Directory information has not been found."); |
|
| 384 | + } |
|
| 352 | 385 | } |
| 353 | 386 | |
| 354 | 387 | if ($ReadData) { |
@@ -393,7 +426,9 @@ discard block |
||
| 393 | 426 | // Store replacement information. |
| 394 | 427 | |
| 395 | 428 | $idx = $this->FileGetIdx($NameOrIdx); |
| 396 | - if ($idx===false) return $this->RaiseError('File "'.$NameOrIdx.'" is not found in the Central Directory.'); |
|
| 429 | + if ($idx===false) { |
|
| 430 | + return $this->RaiseError('File "'.$NameOrIdx.'" is not found in the Central Directory.'); |
|
| 431 | + } |
|
| 397 | 432 | |
| 398 | 433 | $pos = $this->CdFileLst[$idx]['p_loc']; |
| 399 | 434 | |
@@ -405,7 +440,9 @@ discard block |
||
| 405 | 440 | // file to replace |
| 406 | 441 | $Diff = - $this->CdFileLst[$idx]['l_data_c']; |
| 407 | 442 | $Ref = $this->_DataCreateNewRef($Data, $DataType, $Compress, $Diff, $NameOrIdx); |
| 408 | - if ($Ref===false) return false; |
|
| 443 | + if ($Ref===false) { |
|
| 444 | + return false; |
|
| 445 | + } |
|
| 409 | 446 | $this->ReplInfo[$idx] = $Ref; |
| 410 | 447 | $Result = $Ref['res']; |
| 411 | 448 | } |
@@ -489,7 +526,9 @@ discard block |
||
| 489 | 526 | $date = $this->_MsDos_Date($now); |
| 490 | 527 | $time = $this->_MsDos_Time($now); |
| 491 | 528 | |
| 492 | - if (!$this->OutputOpen($Render, $File, $ContentType)) return false; |
|
| 529 | + if (!$this->OutputOpen($Render, $File, $ContentType)) { |
|
| 530 | + return false; |
|
| 531 | + } |
|
| 493 | 532 | |
| 494 | 533 | // output modified zipped files and unmodified zipped files that are beetween them |
| 495 | 534 | ksort($this->ReplByPos); |
@@ -497,7 +536,9 @@ discard block |
||
| 497 | 536 | // output data from the zip archive which is before the data to replace |
| 498 | 537 | $this->OutputFromArch($ArchPos, $ReplPos); |
| 499 | 538 | // get current file information |
| 500 | - if (!isset($this->VisFileLst[$ReplIdx])) $this->_ReadFile($ReplIdx, false); |
|
| 539 | + if (!isset($this->VisFileLst[$ReplIdx])) { |
|
| 540 | + $this->_ReadFile($ReplIdx, false); |
|
| 541 | + } |
|
| 501 | 542 | $FileInfo =& $this->VisFileLst[$ReplIdx]; |
| 502 | 543 | $b1 = $FileInfo['bin']; |
| 503 | 544 | if (isset($FileInfo['desc_bin'])) { |
@@ -520,7 +561,10 @@ discard block |
||
| 520 | 561 | $this->_PutDec($b1, $ReplInfo['crc32'], 14, 4); // crc32 |
| 521 | 562 | $this->_PutDec($b1, $ReplInfo['len_c'], 18, 4); // l_data_c |
| 522 | 563 | $this->_PutDec($b1, $ReplInfo['len_u'], 22, 4); // l_data_u |
| 523 | - if ($ReplInfo['meth']!==false) $this->_PutDec($b1, $ReplInfo['meth'], 8, 2); // meth |
|
| 564 | + if ($ReplInfo['meth']!==false) { |
|
| 565 | + $this->_PutDec($b1, $ReplInfo['meth'], 8, 2); |
|
| 566 | + } |
|
| 567 | + // meth |
|
| 524 | 568 | // prepare the bottom description if the zipped file, if any |
| 525 | 569 | if ($b2!=='') { |
| 526 | 570 | $d = (strlen($b2)==16) ? 4 : 0; // offset because of the signature if any |
@@ -544,7 +588,10 @@ discard block |
||
| 544 | 588 | } |
| 545 | 589 | |
| 546 | 590 | // Ouput all the zipped files that remain before the Central Directory listing |
| 547 | - if ($this->ArchHnd!==false) $this->OutputFromArch($ArchPos, $this->CdPos); // ArchHnd is false if CreateNew() has been called |
|
| 591 | + if ($this->ArchHnd!==false) { |
|
| 592 | + $this->OutputFromArch($ArchPos, $this->CdPos); |
|
| 593 | + } |
|
| 594 | + // ArchHnd is false if CreateNew() has been called |
|
| 548 | 595 | $ArchPos = $this->CdPos; |
| 549 | 596 | |
| 550 | 597 | // Output file to add |
@@ -567,7 +614,10 @@ discard block |
||
| 567 | 614 | $b1 = $this->CdFileLst[$i]['bin']; |
| 568 | 615 | $old_cd_len += strlen($b1); |
| 569 | 616 | if (!isset($DelLst[$i])) { |
| 570 | - if (isset($FicNewPos[$i])) $this->_PutDec($b1, $FicNewPos[$i], 42, 4); // p_loc |
|
| 617 | + if (isset($FicNewPos[$i])) { |
|
| 618 | + $this->_PutDec($b1, $FicNewPos[$i], 42, 4); |
|
| 619 | + } |
|
| 620 | + // p_loc |
|
| 571 | 621 | if (isset($this->ReplInfo[$i])) { |
| 572 | 622 | $ReplInfo =& $this->ReplInfo[$i]; |
| 573 | 623 | $this->_PutDec($b1, $time, 12, 2); // time |
@@ -575,7 +625,10 @@ discard block |
||
| 575 | 625 | $this->_PutDec($b1, $ReplInfo['crc32'], 16, 4); // crc32 |
| 576 | 626 | $this->_PutDec($b1, $ReplInfo['len_c'], 20, 4); // l_data_c |
| 577 | 627 | $this->_PutDec($b1, $ReplInfo['len_u'], 24, 4); // l_data_u |
| 578 | - if ($ReplInfo['meth']!==false) $this->_PutDec($b1, $ReplInfo['meth'], 10, 2); // meth |
|
| 628 | + if ($ReplInfo['meth']!==false) { |
|
| 629 | + $this->_PutDec($b1, $ReplInfo['meth'], 10, 2); |
|
| 630 | + } |
|
| 631 | + // meth |
|
| 579 | 632 | } |
| 580 | 633 | $b2 .= $b1; |
| 581 | 634 | } |
@@ -585,7 +638,10 @@ discard block |
||
| 585 | 638 | $DeltaCdLen = $DeltaCdLen + strlen($b2) - $old_cd_len; |
| 586 | 639 | |
| 587 | 640 | // Output until "end of central directory record" |
| 588 | - if ($this->ArchHnd!==false) $this->OutputFromArch($ArchPos, $this->CdEndPos); // ArchHnd is false if CreateNew() has been called |
|
| 641 | + if ($this->ArchHnd!==false) { |
|
| 642 | + $this->OutputFromArch($ArchPos, $this->CdEndPos); |
|
| 643 | + } |
|
| 644 | + // ArchHnd is false if CreateNew() has been called |
|
| 589 | 645 | |
| 590 | 646 | // Output file information of the Central Directory for added files |
| 591 | 647 | if ($AddNbr>0) { |
@@ -629,7 +685,9 @@ discard block |
||
| 629 | 685 | |
| 630 | 686 | if (($Render & self::TBSZIP_FILE)==self::TBSZIP_FILE) { |
| 631 | 687 | $this->OutputMode = self::TBSZIP_FILE; |
| 632 | - if (''.$File=='') $File = basename($this->ArchFile).'.zip'; |
|
| 688 | + if (''.$File=='') { |
|
| 689 | + $File = basename($this->ArchFile).'.zip'; |
|
| 690 | + } |
|
| 633 | 691 | $this->OutputHandle = @fopen($File, 'w'); |
| 634 | 692 | if ($this->OutputHandle===false) { |
| 635 | 693 | return $this->RaiseError('Method Flush() cannot overwrite the target file \''.$File.'\'. This may not be a valid file path or the file may be locked by another process or because of a denied permission.'); |
@@ -640,11 +698,15 @@ discard block |
||
| 640 | 698 | } elseif (($Render & self::TBSZIP_DOWNLOAD)==self::TBSZIP_DOWNLOAD) { |
| 641 | 699 | $this->OutputMode = self::TBSZIP_DOWNLOAD; |
| 642 | 700 | // Output the file |
| 643 | - if (''.$File=='') $File = basename($this->ArchFile); |
|
| 701 | + if (''.$File=='') { |
|
| 702 | + $File = basename($this->ArchFile); |
|
| 703 | + } |
|
| 644 | 704 | if (($Render & self::TBSZIP_NOHEADER)==self::TBSZIP_NOHEADER) { |
| 645 | 705 | } else { |
| 646 | 706 | header ('Pragma: no-cache'); |
| 647 | - if ($ContentType!='') header ('Content-Type: '.$ContentType); |
|
| 707 | + if ($ContentType!='') { |
|
| 708 | + header ('Content-Type: '.$ContentType); |
|
| 709 | + } |
|
| 648 | 710 | header('Content-Disposition: attachment; filename="'.$File.'"'); |
| 649 | 711 | header('Expires: 0'); |
| 650 | 712 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
@@ -652,7 +714,9 @@ discard block |
||
| 652 | 714 | header('Content-Description: File Transfer'); |
| 653 | 715 | header('Content-Transfer-Encoding: binary'); |
| 654 | 716 | $Len = $this->_EstimateNewArchSize(); |
| 655 | - if ($Len!==false) header('Content-Length: '.$Len); |
|
| 717 | + if ($Len!==false) { |
|
| 718 | + header('Content-Length: '.$Len); |
|
| 719 | + } |
|
| 656 | 720 | } |
| 657 | 721 | } else { |
| 658 | 722 | return $this->RaiseError('Method Flush is called with a unsupported render option.'); |
@@ -664,7 +728,9 @@ discard block |
||
| 664 | 728 | |
| 665 | 729 | function OutputFromArch($pos, $pos_stop) { |
| 666 | 730 | $len = $pos_stop - $pos; |
| 667 | - if ($len<0) return; |
|
| 731 | + if ($len<0) { |
|
| 732 | + return; |
|
| 733 | + } |
|
| 668 | 734 | $this->_MoveTo($pos); |
| 669 | 735 | $block = 1024; |
| 670 | 736 | while ($len>0) { |
@@ -719,7 +785,9 @@ discard block |
||
| 719 | 785 | $z = 0; |
| 720 | 786 | for ($i=0;$i<$len;$i++) { |
| 721 | 787 | $asc = ord($x[$i]); |
| 722 | - if ($asc>0) $z = $z + $asc*pow(256,$i); |
|
| 788 | + if ($asc>0) { |
|
| 789 | + $z = $z + $asc*pow(256,$i); |
|
| 790 | + } |
|
| 723 | 791 | } |
| 724 | 792 | return $z; |
| 725 | 793 | } |
@@ -929,7 +997,9 @@ discard block |
||
| 929 | 997 | $fi = stat($path); |
| 930 | 998 | if ($fi !== false) { |
| 931 | 999 | $fz = $fi['size']; |
| 932 | - if ($len_u===false) $len_u = $fz; |
|
| 1000 | + if ($len_u===false) { |
|
| 1001 | + $len_u = $fz; |
|
| 1002 | + } |
|
| 933 | 1003 | $len_c = ($Compress) ? false : $fz; |
| 934 | 1004 | $file_time = $fi['mtime']; |
| 935 | 1005 | } else { |
@@ -948,7 +1018,9 @@ discard block |
||
| 948 | 1018 | // TODO: support for data returned from okapi cache |
| 949 | 1019 | if ($Ref['path']!==false) { |
| 950 | 1020 | $Ref['data'] = file_get_contents($Ref['path']); |
| 951 | - if ($Ref['crc32']===false) $Ref['crc32'] = crc32($Ref['data']); |
|
| 1021 | + if ($Ref['crc32']===false) { |
|
| 1022 | + $Ref['crc32'] = crc32($Ref['data']); |
|
| 1023 | + } |
|
| 952 | 1024 | if ($Ref['len_c']===false) { |
| 953 | 1025 | // means the data must be compressed |
| 954 | 1026 | $Ref['data'] = gzdeflate($Ref['data']); |
@@ -977,13 +1049,18 @@ discard block |
||
| 977 | 1049 | // file to delete |
| 978 | 1050 | $Info =& $this->CdFileLst[$i]; |
| 979 | 1051 | if (!isset($this->VisFileLst[$i])) { |
| 980 | - if ($Optim) return false; // if $Optimization is set to true, then we d'ont rewind to read information |
|
| 1052 | + if ($Optim) { |
|
| 1053 | + return false; |
|
| 1054 | + } |
|
| 1055 | + // if $Optimization is set to true, then we d'ont rewind to read information |
|
| 981 | 1056 | $this->_MoveTo($Info['p_loc']); |
| 982 | 1057 | $this->_ReadFile($i, false); |
| 983 | 1058 | } |
| 984 | 1059 | $Vis =& $this->VisFileLst[$i]; |
| 985 | 1060 | $Len += -strlen($Vis['bin']) -strlen($Info['bin']) - $Info['l_data_c']; |
| 986 | - if (isset($Vis['desc_bin'])) $Len += -strlen($Vis['desc_bin']); |
|
| 1061 | + if (isset($Vis['desc_bin'])) { |
|
| 1062 | + $Len += -strlen($Vis['desc_bin']); |
|
| 1063 | + } |
|
| 987 | 1064 | } elseif ($Ref['len_c']===false) { |
| 988 | 1065 | return false; // information not yet known |
| 989 | 1066 | } else { |
@@ -104,6 +104,10 @@ discard block |
||
| 104 | 104 | $extras['reason_stack'][] = 'missing_parameter'; |
| 105 | 105 | $extras['parameter'] = $this->param_name; |
| 106 | 106 | } |
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @param string $param_name |
|
| 110 | + */ |
|
| 107 | 111 | public function __construct($param_name) { |
| 108 | 112 | parent::__construct("Missing '$param_name' parameter. This parameter is required."); |
| 109 | 113 | $this->param_name = $param_name; |
@@ -151,6 +155,12 @@ discard block |
||
| 151 | 155 | $extras['difference'] = $this->ourTimestamp - $this->usersTimestamp; |
| 152 | 156 | $extras['threshold'] = $this->threshold; |
| 153 | 157 | } |
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @param integer $users |
|
| 161 | + * @param integer $ours |
|
| 162 | + * @param integer $threshold |
|
| 163 | + */ |
|
| 154 | 164 | public function __construct($users, $ours, $threshold) { |
| 155 | 165 | $this->usersTimestamp = $users; |
| 156 | 166 | $this->ourTimestamp = $ours; |
@@ -352,6 +362,12 @@ discard block |
||
| 352 | 362 | // (1) do a lookup in a table of trusted certs keyed off of consumer |
| 353 | 363 | // |
| 354 | 364 | // Either way should return a string representation of the certificate |
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * @param OAuthRequest $request |
|
| 368 | + * |
|
| 369 | + * @return string |
|
| 370 | + */ |
|
| 355 | 371 | protected abstract function fetch_private_cert(&$request); |
| 356 | 372 | |
| 357 | 373 | public function build_signature($request, $consumer, $token) { |
@@ -481,6 +497,9 @@ discard block |
||
| 481 | 497 | return new OAuthRequest($http_method, $http_url, $parameters); |
| 482 | 498 | } |
| 483 | 499 | |
| 500 | + /** |
|
| 501 | + * @param string $name |
|
| 502 | + */ |
|
| 484 | 503 | public function set_parameter($name, $value, $allow_duplicates = true) { |
| 485 | 504 | if ($allow_duplicates && isset($this->parameters[$name])) { |
| 486 | 505 | // We have already added parameter(s) with this name, so add to the list |
@@ -665,6 +684,9 @@ discard block |
||
| 665 | 684 | $this->data_store = $data_store; |
| 666 | 685 | } |
| 667 | 686 | |
| 687 | + /** |
|
| 688 | + * @param OAuthSignatureMethod_HMAC_SHA1 $signature_method |
|
| 689 | + */ |
|
| 668 | 690 | public function add_signature_method($signature_method) { |
| 669 | 691 | $this->signature_methods[$signature_method->get_name()] = |
| 670 | 692 | $signature_method; |
@@ -55,162 +55,162 @@ discard block |
||
| 55 | 55 | class OAuthClientException extends OAuthException {} |
| 56 | 56 | /** OAuth server errors. */ |
| 57 | 57 | abstract class OAuthServerException extends OAuthException { |
| 58 | - abstract public function getHttpStatusCode(); |
|
| 59 | - protected function provideExtras(&$extras) { |
|
| 58 | + abstract public function getHttpStatusCode(); |
|
| 59 | + protected function provideExtras(&$extras) { |
|
| 60 | 60 | $extras['reason_stack'][] = 'invalid_oauth_request'; |
| 61 | - } |
|
| 62 | - public function getOkapiJSON() { |
|
| 61 | + } |
|
| 62 | + public function getOkapiJSON() { |
|
| 63 | 63 | $extras = array( |
| 64 | - 'developer_message' => $this->getMessage(), |
|
| 65 | - 'reason_stack' => array(), |
|
| 64 | + 'developer_message' => $this->getMessage(), |
|
| 65 | + 'reason_stack' => array(), |
|
| 66 | 66 | ); |
| 67 | 67 | $this->provideExtras($extras); |
| 68 | 68 | $extras['more_info'] = "http://opencaching.pl/okapi/introduction.html#errors"; |
| 69 | 69 | return json_encode(array("error" => $extras)); |
| 70 | - } |
|
| 70 | + } |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | # More subclasses of server exceptions. |
| 74 | 74 | |
| 75 | 75 | /** OAuth server errors which should result in HTTP 400 response. */ |
| 76 | 76 | abstract class OAuthServer400Exception extends OAuthServerException { |
| 77 | - protected function provideExtras(&$extras) { |
|
| 77 | + protected function provideExtras(&$extras) { |
|
| 78 | 78 | parent::provideExtras($extras); |
| 79 | 79 | $extras['status'] = 400; |
| 80 | - } |
|
| 81 | - public function getHttpStatusCode() { return 400; } |
|
| 80 | + } |
|
| 81 | + public function getHttpStatusCode() { return 400; } |
|
| 82 | 82 | } |
| 83 | 83 | /** OAuth server errors which should result in HTTP 401 response. */ |
| 84 | 84 | abstract class OAuthServer401Exception extends OAuthServerException { |
| 85 | - protected function provideExtras(&$extras) { |
|
| 85 | + protected function provideExtras(&$extras) { |
|
| 86 | 86 | parent::provideExtras($extras); |
| 87 | 87 | $extras['status'] = 401; |
| 88 | - } |
|
| 89 | - public function getHttpStatusCode() { return 401; } |
|
| 88 | + } |
|
| 89 | + public function getHttpStatusCode() { return 401; } |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | /** Client asked for an unsupported OAuth version (not 1.0). */ |
| 93 | 93 | class OAuthVersionNotSupportedException extends OAuthServer400Exception { |
| 94 | - protected function provideExtras(&$extras) { |
|
| 94 | + protected function provideExtras(&$extras) { |
|
| 95 | 95 | parent::provideExtras($extras); |
| 96 | 96 | $extras['reason_stack'][] = 'unsupported_oauth_version'; |
| 97 | - } |
|
| 97 | + } |
|
| 98 | 98 | } |
| 99 | 99 | /** Client didn't provide one of the key OAuth parameters. */ |
| 100 | 100 | class OAuthMissingParameterException extends OAuthServer400Exception { |
| 101 | - protected $param_name; |
|
| 102 | - protected function provideExtras(&$extras) { |
|
| 101 | + protected $param_name; |
|
| 102 | + protected function provideExtras(&$extras) { |
|
| 103 | 103 | parent::provideExtras($extras); |
| 104 | 104 | $extras['reason_stack'][] = 'missing_parameter'; |
| 105 | 105 | $extras['parameter'] = $this->param_name; |
| 106 | - } |
|
| 107 | - public function __construct($param_name) { |
|
| 106 | + } |
|
| 107 | + public function __construct($param_name) { |
|
| 108 | 108 | parent::__construct("Missing '$param_name' parameter. This parameter is required."); |
| 109 | 109 | $this->param_name = $param_name; |
| 110 | - } |
|
| 111 | - public function getParamName() { return $this->param_name; } |
|
| 110 | + } |
|
| 111 | + public function getParamName() { return $this->param_name; } |
|
| 112 | 112 | } |
| 113 | 113 | /** Client used unsupported signature method. */ |
| 114 | 114 | class OAuthUnsupportedSignatureMethodException extends OAuthServer400Exception { |
| 115 | - protected function provideExtras(&$extras) { |
|
| 115 | + protected function provideExtras(&$extras) { |
|
| 116 | 116 | parent::provideExtras($extras); |
| 117 | 117 | $extras['reason_stack'][] = 'unsupported_signature_method'; |
| 118 | - } |
|
| 118 | + } |
|
| 119 | 119 | } |
| 120 | 120 | /** Client provided invalid Consumer Key. */ |
| 121 | 121 | class OAuthInvalidConsumerException extends OAuthServer401Exception { |
| 122 | - protected function provideExtras(&$extras) { |
|
| 122 | + protected function provideExtras(&$extras) { |
|
| 123 | 123 | parent::provideExtras($extras); |
| 124 | 124 | $extras['reason_stack'][] = 'invalid_consumer'; |
| 125 | - } |
|
| 125 | + } |
|
| 126 | 126 | } |
| 127 | 127 | /** Client provider invalid token (either Request Token or Access Token). */ |
| 128 | 128 | class OAuthInvalidTokenException extends OAuthServer401Exception { |
| 129 | - protected function provideExtras(&$extras) { |
|
| 129 | + protected function provideExtras(&$extras) { |
|
| 130 | 130 | parent::provideExtras($extras); |
| 131 | 131 | $extras['reason_stack'][] = 'invalid_token'; |
| 132 | - } |
|
| 132 | + } |
|
| 133 | 133 | } |
| 134 | 134 | /** Client's signature was invalid. */ |
| 135 | 135 | class OAuthInvalidSignatureException extends OAuthServer401Exception { |
| 136 | - protected function provideExtras(&$extras) { |
|
| 136 | + protected function provideExtras(&$extras) { |
|
| 137 | 137 | parent::provideExtras($extras); |
| 138 | 138 | $extras['reason_stack'][] = 'invalid_signature'; |
| 139 | - } |
|
| 139 | + } |
|
| 140 | 140 | } |
| 141 | 141 | /** Client used expired timestamp (or timestamp too far in future). */ |
| 142 | 142 | class OAuthExpiredTimestampException extends OAuthServer400Exception { |
| 143 | - protected $usersTimestamp; |
|
| 144 | - protected $ourTimestamp; |
|
| 145 | - protected $threshold; |
|
| 146 | - protected function provideExtras(&$extras) { |
|
| 143 | + protected $usersTimestamp; |
|
| 144 | + protected $ourTimestamp; |
|
| 145 | + protected $threshold; |
|
| 146 | + protected function provideExtras(&$extras) { |
|
| 147 | 147 | parent::provideExtras($extras); |
| 148 | 148 | $extras['reason_stack'][] = 'invalid_timestamp'; |
| 149 | 149 | $extras['yours'] = $this->usersTimestamp; |
| 150 | 150 | $extras['ours'] = $this->ourTimestamp; |
| 151 | 151 | $extras['difference'] = $this->ourTimestamp - $this->usersTimestamp; |
| 152 | 152 | $extras['threshold'] = $this->threshold; |
| 153 | - } |
|
| 154 | - public function __construct($users, $ours, $threshold) { |
|
| 153 | + } |
|
| 154 | + public function __construct($users, $ours, $threshold) { |
|
| 155 | 155 | $this->usersTimestamp = $users; |
| 156 | 156 | $this->ourTimestamp = $ours; |
| 157 | 157 | $this->threshold = $threshold; |
| 158 | 158 | parent::__construct("Expired timestamp, yours $this->usersTimestamp, ours $this->ourTimestamp (threshold $this->threshold)."); |
| 159 | - } |
|
| 160 | - public function getUsersTimestamp() { return $this->usersTimestamp; } |
|
| 161 | - public function getOurTimestamp() { return $this->ourTimestamp; } |
|
| 159 | + } |
|
| 160 | + public function getUsersTimestamp() { return $this->usersTimestamp; } |
|
| 161 | + public function getOurTimestamp() { return $this->ourTimestamp; } |
|
| 162 | 162 | } |
| 163 | 163 | /** Client used the same nonce for the second time. */ |
| 164 | 164 | class OAuthNonceAlreadyUsedException extends OAuthServer400Exception { |
| 165 | - protected function provideExtras(&$extras) { |
|
| 165 | + protected function provideExtras(&$extras) { |
|
| 166 | 166 | parent::provideExtras($extras); |
| 167 | 167 | $extras['reason_stack'][] = 'nonce_already_used'; |
| 168 | - } |
|
| 168 | + } |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | class OAuthConsumer { |
| 172 | - public $key; |
|
| 173 | - public $secret; |
|
| 172 | + public $key; |
|
| 173 | + public $secret; |
|
| 174 | 174 | |
| 175 | - function __construct($key, $secret, $callback_url=NULL) { |
|
| 175 | + function __construct($key, $secret, $callback_url=NULL) { |
|
| 176 | 176 | $this->key = $key; |
| 177 | 177 | $this->secret = $secret; |
| 178 | 178 | $this->callback_url = $callback_url; |
| 179 | - } |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - function __toString() { |
|
| 181 | + function __toString() { |
|
| 182 | 182 | return "OAuthConsumer[key=$this->key,secret=$this->secret]"; |
| 183 | - } |
|
| 183 | + } |
|
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | class OAuthToken { |
| 187 | - // access tokens and request tokens |
|
| 188 | - public $key; |
|
| 189 | - public $secret; |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * key = the token |
|
| 193 | - * secret = the token secret |
|
| 194 | - */ |
|
| 195 | - function __construct($key, $secret) { |
|
| 187 | + // access tokens and request tokens |
|
| 188 | + public $key; |
|
| 189 | + public $secret; |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * key = the token |
|
| 193 | + * secret = the token secret |
|
| 194 | + */ |
|
| 195 | + function __construct($key, $secret) { |
|
| 196 | 196 | $this->key = $key; |
| 197 | 197 | $this->secret = $secret; |
| 198 | - } |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - /** |
|
| 201 | - * generates the basic string serialization of a token that a server |
|
| 202 | - * would respond to request_token and access_token calls with |
|
| 203 | - */ |
|
| 204 | - function to_string() { |
|
| 200 | + /** |
|
| 201 | + * generates the basic string serialization of a token that a server |
|
| 202 | + * would respond to request_token and access_token calls with |
|
| 203 | + */ |
|
| 204 | + function to_string() { |
|
| 205 | 205 | return "oauth_token=" . |
| 206 | - OAuthUtil::urlencode_rfc3986($this->key) . |
|
| 207 | - "&oauth_token_secret=" . |
|
| 208 | - OAuthUtil::urlencode_rfc3986($this->secret); |
|
| 209 | - } |
|
| 206 | + OAuthUtil::urlencode_rfc3986($this->key) . |
|
| 207 | + "&oauth_token_secret=" . |
|
| 208 | + OAuthUtil::urlencode_rfc3986($this->secret); |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | - function __toString() { |
|
| 211 | + function __toString() { |
|
| 212 | 212 | return $this->to_string(); |
| 213 | - } |
|
| 213 | + } |
|
| 214 | 214 | } |
| 215 | 215 | |
| 216 | 216 | /** |
@@ -218,52 +218,52 @@ discard block |
||
| 218 | 218 | * See section 9 ("Signing Requests") in the spec |
| 219 | 219 | */ |
| 220 | 220 | abstract class OAuthSignatureMethod { |
| 221 | - /** |
|
| 222 | - * Needs to return the name of the Signature Method (ie HMAC-SHA1) |
|
| 223 | - * @return string |
|
| 224 | - */ |
|
| 225 | - abstract public function get_name(); |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Build up the signature |
|
| 229 | - * NOTE: The output of this function MUST NOT be urlencoded. |
|
| 230 | - * the encoding is handled in OAuthRequest when the final |
|
| 231 | - * request is serialized |
|
| 232 | - * @param OAuthRequest $request |
|
| 233 | - * @param OAuthConsumer $consumer |
|
| 234 | - * @param OAuthToken $token |
|
| 235 | - * @return string |
|
| 236 | - */ |
|
| 237 | - abstract public function build_signature($request, $consumer, $token); |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Verifies that a given signature is correct |
|
| 241 | - * @param OAuthRequest $request |
|
| 242 | - * @param OAuthConsumer $consumer |
|
| 243 | - * @param OAuthToken $token |
|
| 244 | - * @param string $signature |
|
| 245 | - * @return bool |
|
| 246 | - */ |
|
| 247 | - public function check_signature($request, $consumer, $token, $signature) { |
|
| 221 | + /** |
|
| 222 | + * Needs to return the name of the Signature Method (ie HMAC-SHA1) |
|
| 223 | + * @return string |
|
| 224 | + */ |
|
| 225 | + abstract public function get_name(); |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Build up the signature |
|
| 229 | + * NOTE: The output of this function MUST NOT be urlencoded. |
|
| 230 | + * the encoding is handled in OAuthRequest when the final |
|
| 231 | + * request is serialized |
|
| 232 | + * @param OAuthRequest $request |
|
| 233 | + * @param OAuthConsumer $consumer |
|
| 234 | + * @param OAuthToken $token |
|
| 235 | + * @return string |
|
| 236 | + */ |
|
| 237 | + abstract public function build_signature($request, $consumer, $token); |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Verifies that a given signature is correct |
|
| 241 | + * @param OAuthRequest $request |
|
| 242 | + * @param OAuthConsumer $consumer |
|
| 243 | + * @param OAuthToken $token |
|
| 244 | + * @param string $signature |
|
| 245 | + * @return bool |
|
| 246 | + */ |
|
| 247 | + public function check_signature($request, $consumer, $token, $signature) { |
|
| 248 | 248 | $built = $this->build_signature($request, $consumer, $token); |
| 249 | 249 | |
| 250 | 250 | // Check for zero length, although unlikely here |
| 251 | 251 | if (strlen($built) == 0 || strlen($signature) == 0) { |
| 252 | - return false; |
|
| 252 | + return false; |
|
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | if (strlen($built) != strlen($signature)) { |
| 256 | - return false; |
|
| 256 | + return false; |
|
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | // Avoid a timing leak with a (hopefully) time insensitive compare |
| 260 | 260 | $result = 0; |
| 261 | 261 | for ($i = 0; $i < strlen($signature); $i++) { |
| 262 | - $result |= ord($built{$i}) ^ ord($signature{$i}); |
|
| 262 | + $result |= ord($built{$i}) ^ ord($signature{$i}); |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | return $result == 0; |
| 266 | - } |
|
| 266 | + } |
|
| 267 | 267 | } |
| 268 | 268 | |
| 269 | 269 | /** |
@@ -274,24 +274,24 @@ discard block |
||
| 274 | 274 | * - Chapter 9.2 ("HMAC-SHA1") |
| 275 | 275 | */ |
| 276 | 276 | class OAuthSignatureMethod_HMAC_SHA1 extends OAuthSignatureMethod { |
| 277 | - function get_name() { |
|
| 277 | + function get_name() { |
|
| 278 | 278 | return "HMAC-SHA1"; |
| 279 | - } |
|
| 279 | + } |
|
| 280 | 280 | |
| 281 | - public function build_signature($request, $consumer, $token) { |
|
| 281 | + public function build_signature($request, $consumer, $token) { |
|
| 282 | 282 | $base_string = $request->get_signature_base_string(); |
| 283 | 283 | $request->base_string = $base_string; |
| 284 | 284 | |
| 285 | 285 | $key_parts = array( |
| 286 | - $consumer->secret, |
|
| 287 | - ($token) ? $token->secret : "" |
|
| 286 | + $consumer->secret, |
|
| 287 | + ($token) ? $token->secret : "" |
|
| 288 | 288 | ); |
| 289 | 289 | |
| 290 | 290 | $key_parts = OAuthUtil::urlencode_rfc3986($key_parts); |
| 291 | 291 | $key = implode('&', $key_parts); |
| 292 | 292 | |
| 293 | 293 | return base64_encode(hash_hmac('sha1', $base_string, $key, true)); |
| 294 | - } |
|
| 294 | + } |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | /** |
@@ -300,23 +300,23 @@ discard block |
||
| 300 | 300 | * - Chapter 9.4 ("PLAINTEXT") |
| 301 | 301 | */ |
| 302 | 302 | class OAuthSignatureMethod_PLAINTEXT extends OAuthSignatureMethod { |
| 303 | - public function get_name() { |
|
| 303 | + public function get_name() { |
|
| 304 | 304 | return "PLAINTEXT"; |
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * oauth_signature is set to the concatenated encoded values of the Consumer Secret and |
|
| 309 | - * Token Secret, separated by a '&' character (ASCII code 38), even if either secret is |
|
| 310 | - * empty. The result MUST be encoded again. |
|
| 311 | - * - Chapter 9.4.1 ("Generating Signatures") |
|
| 312 | - * |
|
| 313 | - * Please note that the second encoding MUST NOT happen in the SignatureMethod, as |
|
| 314 | - * OAuthRequest handles this! |
|
| 315 | - */ |
|
| 316 | - public function build_signature($request, $consumer, $token) { |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * oauth_signature is set to the concatenated encoded values of the Consumer Secret and |
|
| 309 | + * Token Secret, separated by a '&' character (ASCII code 38), even if either secret is |
|
| 310 | + * empty. The result MUST be encoded again. |
|
| 311 | + * - Chapter 9.4.1 ("Generating Signatures") |
|
| 312 | + * |
|
| 313 | + * Please note that the second encoding MUST NOT happen in the SignatureMethod, as |
|
| 314 | + * OAuthRequest handles this! |
|
| 315 | + */ |
|
| 316 | + public function build_signature($request, $consumer, $token) { |
|
| 317 | 317 | $key_parts = array( |
| 318 | - $consumer->secret, |
|
| 319 | - ($token) ? $token->secret : "" |
|
| 318 | + $consumer->secret, |
|
| 319 | + ($token) ? $token->secret : "" |
|
| 320 | 320 | ); |
| 321 | 321 | |
| 322 | 322 | $key_parts = OAuthUtil::urlencode_rfc3986($key_parts); |
@@ -324,7 +324,7 @@ discard block |
||
| 324 | 324 | $request->base_string = $key; |
| 325 | 325 | |
| 326 | 326 | return $key; |
| 327 | - } |
|
| 327 | + } |
|
| 328 | 328 | } |
| 329 | 329 | |
| 330 | 330 | /** |
@@ -336,25 +336,25 @@ discard block |
||
| 336 | 336 | * - Chapter 9.3 ("RSA-SHA1") |
| 337 | 337 | */ |
| 338 | 338 | abstract class OAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod { |
| 339 | - public function get_name() { |
|
| 339 | + public function get_name() { |
|
| 340 | 340 | return "RSA-SHA1"; |
| 341 | - } |
|
| 342 | - |
|
| 343 | - // Up to the SP to implement this lookup of keys. Possible ideas are: |
|
| 344 | - // (1) do a lookup in a table of trusted certs keyed off of consumer |
|
| 345 | - // (2) fetch via http using a url provided by the requester |
|
| 346 | - // (3) some sort of specific discovery code based on request |
|
| 347 | - // |
|
| 348 | - // Either way should return a string representation of the certificate |
|
| 349 | - protected abstract function fetch_public_cert(&$request); |
|
| 350 | - |
|
| 351 | - // Up to the SP to implement this lookup of keys. Possible ideas are: |
|
| 352 | - // (1) do a lookup in a table of trusted certs keyed off of consumer |
|
| 353 | - // |
|
| 354 | - // Either way should return a string representation of the certificate |
|
| 355 | - protected abstract function fetch_private_cert(&$request); |
|
| 356 | - |
|
| 357 | - public function build_signature($request, $consumer, $token) { |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + // Up to the SP to implement this lookup of keys. Possible ideas are: |
|
| 344 | + // (1) do a lookup in a table of trusted certs keyed off of consumer |
|
| 345 | + // (2) fetch via http using a url provided by the requester |
|
| 346 | + // (3) some sort of specific discovery code based on request |
|
| 347 | + // |
|
| 348 | + // Either way should return a string representation of the certificate |
|
| 349 | + protected abstract function fetch_public_cert(&$request); |
|
| 350 | + |
|
| 351 | + // Up to the SP to implement this lookup of keys. Possible ideas are: |
|
| 352 | + // (1) do a lookup in a table of trusted certs keyed off of consumer |
|
| 353 | + // |
|
| 354 | + // Either way should return a string representation of the certificate |
|
| 355 | + protected abstract function fetch_private_cert(&$request); |
|
| 356 | + |
|
| 357 | + public function build_signature($request, $consumer, $token) { |
|
| 358 | 358 | $base_string = $request->get_signature_base_string(); |
| 359 | 359 | $request->base_string = $base_string; |
| 360 | 360 | |
@@ -371,9 +371,9 @@ discard block |
||
| 371 | 371 | openssl_free_key($privatekeyid); |
| 372 | 372 | |
| 373 | 373 | return base64_encode($signature); |
| 374 | - } |
|
| 374 | + } |
|
| 375 | 375 | |
| 376 | - public function check_signature($request, $consumer, $token, $signature) { |
|
| 376 | + public function check_signature($request, $consumer, $token, $signature) { |
|
| 377 | 377 | $decoded_sig = base64_decode($signature); |
| 378 | 378 | |
| 379 | 379 | $base_string = $request->get_signature_base_string(); |
@@ -391,39 +391,39 @@ discard block |
||
| 391 | 391 | openssl_free_key($publickeyid); |
| 392 | 392 | |
| 393 | 393 | return $ok == 1; |
| 394 | - } |
|
| 394 | + } |
|
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | class OAuthRequest { |
| 398 | - protected $parameters; |
|
| 399 | - protected $http_method; |
|
| 400 | - protected $http_url; |
|
| 401 | - // for debug purposes |
|
| 402 | - public $base_string; |
|
| 403 | - public static $version = '1.0'; |
|
| 404 | - public static $POST_INPUT = 'php://input'; |
|
| 405 | - |
|
| 406 | - function __construct($http_method, $http_url, $parameters=NULL) { |
|
| 398 | + protected $parameters; |
|
| 399 | + protected $http_method; |
|
| 400 | + protected $http_url; |
|
| 401 | + // for debug purposes |
|
| 402 | + public $base_string; |
|
| 403 | + public static $version = '1.0'; |
|
| 404 | + public static $POST_INPUT = 'php://input'; |
|
| 405 | + |
|
| 406 | + function __construct($http_method, $http_url, $parameters=NULL) { |
|
| 407 | 407 | $parameters = ($parameters) ? $parameters : array(); |
| 408 | 408 | $parameters = array_merge( OAuthUtil::parse_parameters(parse_url($http_url, PHP_URL_QUERY)), $parameters); |
| 409 | 409 | $this->parameters = $parameters; |
| 410 | 410 | $this->http_method = $http_method; |
| 411 | 411 | $this->http_url = $http_url; |
| 412 | - } |
|
| 412 | + } |
|
| 413 | 413 | |
| 414 | 414 | |
| 415 | - /** |
|
| 416 | - * attempt to build up a request from what was passed to the server |
|
| 417 | - */ |
|
| 418 | - public static function from_request($http_method=NULL, $http_url=NULL, $parameters=NULL) { |
|
| 415 | + /** |
|
| 416 | + * attempt to build up a request from what was passed to the server |
|
| 417 | + */ |
|
| 418 | + public static function from_request($http_method=NULL, $http_url=NULL, $parameters=NULL) { |
|
| 419 | 419 | $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") |
| 420 | 420 | ? 'http' |
| 421 | 421 | : 'https'; |
| 422 | 422 | $http_url = ($http_url) ? $http_url : $scheme . |
| 423 | - '://' . $_SERVER['SERVER_NAME'] . |
|
| 424 | - ':' . |
|
| 425 | - $_SERVER['SERVER_PORT'] . |
|
| 426 | - $_SERVER['REQUEST_URI']; |
|
| 423 | + '://' . $_SERVER['SERVER_NAME'] . |
|
| 424 | + ':' . |
|
| 425 | + $_SERVER['SERVER_PORT'] . |
|
| 426 | + $_SERVER['REQUEST_URI']; |
|
| 427 | 427 | $http_method = ($http_method) ? $http_method : $_SERVER['REQUEST_METHOD']; |
| 428 | 428 | |
| 429 | 429 | // We weren't handed any parameters, so let's find the ones relevant to |
@@ -431,131 +431,131 @@ discard block |
||
| 431 | 431 | // If you run XML-RPC or similar you should use this to provide your own |
| 432 | 432 | // parsed parameter-list |
| 433 | 433 | if (!$parameters) { |
| 434 | - // Find request headers |
|
| 435 | - $request_headers = OAuthUtil::get_headers(); |
|
| 434 | + // Find request headers |
|
| 435 | + $request_headers = OAuthUtil::get_headers(); |
|
| 436 | 436 | |
| 437 | - // Parse the query-string to find GET parameters |
|
| 438 | - $parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']); |
|
| 437 | + // Parse the query-string to find GET parameters |
|
| 438 | + $parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']); |
|
| 439 | 439 | |
| 440 | - // It's a POST request of the proper content-type, so parse POST |
|
| 441 | - // parameters and add those overriding any duplicates from GET |
|
| 442 | - if ($http_method == "POST" |
|
| 440 | + // It's a POST request of the proper content-type, so parse POST |
|
| 441 | + // parameters and add those overriding any duplicates from GET |
|
| 442 | + if ($http_method == "POST" |
|
| 443 | 443 | && isset($request_headers['Content-Type']) |
| 444 | 444 | && strstr($request_headers['Content-Type'], |
| 445 | - 'application/x-www-form-urlencoded') |
|
| 446 | - ) { |
|
| 445 | + 'application/x-www-form-urlencoded') |
|
| 446 | + ) { |
|
| 447 | 447 | $post_data = OAuthUtil::parse_parameters( |
| 448 | - file_get_contents(self::$POST_INPUT) |
|
| 448 | + file_get_contents(self::$POST_INPUT) |
|
| 449 | 449 | ); |
| 450 | 450 | $parameters = array_merge($parameters, $post_data); |
| 451 | - } |
|
| 451 | + } |
|
| 452 | 452 | |
| 453 | - // We have a Authorization-header with OAuth data. Parse the header |
|
| 454 | - // and add those overriding any duplicates from GET or POST |
|
| 455 | - if (isset($request_headers['Authorization']) && substr($request_headers['Authorization'], 0, 6) == 'OAuth ') { |
|
| 453 | + // We have a Authorization-header with OAuth data. Parse the header |
|
| 454 | + // and add those overriding any duplicates from GET or POST |
|
| 455 | + if (isset($request_headers['Authorization']) && substr($request_headers['Authorization'], 0, 6) == 'OAuth ') { |
|
| 456 | 456 | $header_parameters = OAuthUtil::split_header( |
| 457 | - $request_headers['Authorization'] |
|
| 457 | + $request_headers['Authorization'] |
|
| 458 | 458 | ); |
| 459 | 459 | $parameters = array_merge($parameters, $header_parameters); |
| 460 | - } |
|
| 460 | + } |
|
| 461 | 461 | |
| 462 | 462 | } |
| 463 | 463 | |
| 464 | 464 | return new OAuthRequest($http_method, $http_url, $parameters); |
| 465 | - } |
|
| 465 | + } |
|
| 466 | 466 | |
| 467 | - /** |
|
| 468 | - * pretty much a helper function to set up the request |
|
| 469 | - */ |
|
| 470 | - public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL) { |
|
| 467 | + /** |
|
| 468 | + * pretty much a helper function to set up the request |
|
| 469 | + */ |
|
| 470 | + public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL) { |
|
| 471 | 471 | $parameters = ($parameters) ? $parameters : array(); |
| 472 | 472 | $defaults = array("oauth_version" => OAuthRequest::$version, |
| 473 | - "oauth_nonce" => OAuthRequest::generate_nonce(), |
|
| 474 | - "oauth_timestamp" => OAuthRequest::generate_timestamp(), |
|
| 475 | - "oauth_consumer_key" => $consumer->key); |
|
| 473 | + "oauth_nonce" => OAuthRequest::generate_nonce(), |
|
| 474 | + "oauth_timestamp" => OAuthRequest::generate_timestamp(), |
|
| 475 | + "oauth_consumer_key" => $consumer->key); |
|
| 476 | 476 | if ($token) |
| 477 | - $defaults['oauth_token'] = $token->key; |
|
| 477 | + $defaults['oauth_token'] = $token->key; |
|
| 478 | 478 | |
| 479 | 479 | $parameters = array_merge($defaults, $parameters); |
| 480 | 480 | |
| 481 | 481 | return new OAuthRequest($http_method, $http_url, $parameters); |
| 482 | - } |
|
| 482 | + } |
|
| 483 | 483 | |
| 484 | - public function set_parameter($name, $value, $allow_duplicates = true) { |
|
| 484 | + public function set_parameter($name, $value, $allow_duplicates = true) { |
|
| 485 | 485 | if ($allow_duplicates && isset($this->parameters[$name])) { |
| 486 | - // We have already added parameter(s) with this name, so add to the list |
|
| 487 | - if (is_scalar($this->parameters[$name])) { |
|
| 486 | + // We have already added parameter(s) with this name, so add to the list |
|
| 487 | + if (is_scalar($this->parameters[$name])) { |
|
| 488 | 488 | // This is the first duplicate, so transform scalar (string) |
| 489 | 489 | // into an array so we can add the duplicates |
| 490 | 490 | $this->parameters[$name] = array($this->parameters[$name]); |
| 491 | - } |
|
| 491 | + } |
|
| 492 | 492 | |
| 493 | - $this->parameters[$name][] = $value; |
|
| 493 | + $this->parameters[$name][] = $value; |
|
| 494 | 494 | } else { |
| 495 | - $this->parameters[$name] = $value; |
|
| 495 | + $this->parameters[$name] = $value; |
|
| 496 | + } |
|
| 496 | 497 | } |
| 497 | - } |
|
| 498 | 498 | |
| 499 | - public function get_parameter($name) { |
|
| 499 | + public function get_parameter($name) { |
|
| 500 | 500 | return isset($this->parameters[$name]) ? $this->parameters[$name] : null; |
| 501 | - } |
|
| 501 | + } |
|
| 502 | 502 | |
| 503 | - public function get_parameters() { |
|
| 503 | + public function get_parameters() { |
|
| 504 | 504 | return $this->parameters; |
| 505 | - } |
|
| 505 | + } |
|
| 506 | 506 | |
| 507 | - public function unset_parameter($name) { |
|
| 507 | + public function unset_parameter($name) { |
|
| 508 | 508 | unset($this->parameters[$name]); |
| 509 | - } |
|
| 509 | + } |
|
| 510 | 510 | |
| 511 | - /** |
|
| 512 | - * The request parameters, sorted and concatenated into a normalized string. |
|
| 513 | - * @return string |
|
| 514 | - */ |
|
| 515 | - public function get_signable_parameters() { |
|
| 511 | + /** |
|
| 512 | + * The request parameters, sorted and concatenated into a normalized string. |
|
| 513 | + * @return string |
|
| 514 | + */ |
|
| 515 | + public function get_signable_parameters() { |
|
| 516 | 516 | // Grab all parameters |
| 517 | 517 | $params = $this->parameters; |
| 518 | 518 | |
| 519 | 519 | // Remove oauth_signature if present |
| 520 | 520 | // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.") |
| 521 | 521 | if (isset($params['oauth_signature'])) { |
| 522 | - unset($params['oauth_signature']); |
|
| 522 | + unset($params['oauth_signature']); |
|
| 523 | 523 | } |
| 524 | 524 | |
| 525 | 525 | return OAuthUtil::build_http_query($params); |
| 526 | - } |
|
| 527 | - |
|
| 528 | - /** |
|
| 529 | - * Returns the base string of this request |
|
| 530 | - * |
|
| 531 | - * The base string defined as the method, the url |
|
| 532 | - * and the parameters (normalized), each urlencoded |
|
| 533 | - * and the concated with &. |
|
| 534 | - */ |
|
| 535 | - public function get_signature_base_string() { |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + /** |
|
| 529 | + * Returns the base string of this request |
|
| 530 | + * |
|
| 531 | + * The base string defined as the method, the url |
|
| 532 | + * and the parameters (normalized), each urlencoded |
|
| 533 | + * and the concated with &. |
|
| 534 | + */ |
|
| 535 | + public function get_signature_base_string() { |
|
| 536 | 536 | $parts = array( |
| 537 | - $this->get_normalized_http_method(), |
|
| 538 | - $this->get_normalized_http_url(), |
|
| 539 | - $this->get_signable_parameters() |
|
| 537 | + $this->get_normalized_http_method(), |
|
| 538 | + $this->get_normalized_http_url(), |
|
| 539 | + $this->get_signable_parameters() |
|
| 540 | 540 | ); |
| 541 | 541 | |
| 542 | 542 | $parts = OAuthUtil::urlencode_rfc3986($parts); |
| 543 | 543 | |
| 544 | 544 | return implode('&', $parts); |
| 545 | - } |
|
| 545 | + } |
|
| 546 | 546 | |
| 547 | - /** |
|
| 548 | - * just uppercases the http method |
|
| 549 | - */ |
|
| 550 | - public function get_normalized_http_method() { |
|
| 547 | + /** |
|
| 548 | + * just uppercases the http method |
|
| 549 | + */ |
|
| 550 | + public function get_normalized_http_method() { |
|
| 551 | 551 | return strtoupper($this->http_method); |
| 552 | - } |
|
| 552 | + } |
|
| 553 | 553 | |
| 554 | - /** |
|
| 555 | - * parses the url and rebuilds it to be |
|
| 556 | - * scheme://host/path |
|
| 557 | - */ |
|
| 558 | - public function get_normalized_http_url() { |
|
| 554 | + /** |
|
| 555 | + * parses the url and rebuilds it to be |
|
| 556 | + * scheme://host/path |
|
| 557 | + */ |
|
| 558 | + public function get_normalized_http_url() { |
|
| 559 | 559 | $parts = parse_url($this->http_url); |
| 560 | 560 | |
| 561 | 561 | $scheme = (isset($parts['scheme'])) ? $parts['scheme'] : 'http'; |
@@ -565,118 +565,118 @@ discard block |
||
| 565 | 565 | |
| 566 | 566 | if (($scheme == 'https' && $port != '443') |
| 567 | 567 | || ($scheme == 'http' && $port != '80')) { |
| 568 | - $host = "$host:$port"; |
|
| 568 | + $host = "$host:$port"; |
|
| 569 | 569 | } |
| 570 | 570 | return "$scheme://$host$path"; |
| 571 | - } |
|
| 571 | + } |
|
| 572 | 572 | |
| 573 | - /** |
|
| 574 | - * builds a url usable for a GET request |
|
| 575 | - */ |
|
| 576 | - public function to_url() { |
|
| 573 | + /** |
|
| 574 | + * builds a url usable for a GET request |
|
| 575 | + */ |
|
| 576 | + public function to_url() { |
|
| 577 | 577 | $post_data = $this->to_postdata(); |
| 578 | 578 | $out = $this->get_normalized_http_url(); |
| 579 | 579 | if ($post_data) { |
| 580 | - $out .= '?'.$post_data; |
|
| 580 | + $out .= '?'.$post_data; |
|
| 581 | 581 | } |
| 582 | 582 | return $out; |
| 583 | - } |
|
| 583 | + } |
|
| 584 | 584 | |
| 585 | - /** |
|
| 586 | - * builds the data one would send in a POST request |
|
| 587 | - */ |
|
| 588 | - public function to_postdata() { |
|
| 585 | + /** |
|
| 586 | + * builds the data one would send in a POST request |
|
| 587 | + */ |
|
| 588 | + public function to_postdata() { |
|
| 589 | 589 | return OAuthUtil::build_http_query($this->parameters); |
| 590 | - } |
|
| 590 | + } |
|
| 591 | 591 | |
| 592 | - /** |
|
| 593 | - * builds the Authorization: header |
|
| 594 | - */ |
|
| 595 | - public function to_header($realm=null) { |
|
| 592 | + /** |
|
| 593 | + * builds the Authorization: header |
|
| 594 | + */ |
|
| 595 | + public function to_header($realm=null) { |
|
| 596 | 596 | $first = true; |
| 597 | 597 | if($realm) { |
| 598 | - $out = 'Authorization: OAuth realm="' . OAuthUtil::urlencode_rfc3986($realm) . '"'; |
|
| 599 | - $first = false; |
|
| 598 | + $out = 'Authorization: OAuth realm="' . OAuthUtil::urlencode_rfc3986($realm) . '"'; |
|
| 599 | + $first = false; |
|
| 600 | 600 | } else |
| 601 | - $out = 'Authorization: OAuth'; |
|
| 601 | + $out = 'Authorization: OAuth'; |
|
| 602 | 602 | |
| 603 | 603 | $total = array(); |
| 604 | 604 | foreach ($this->parameters as $k => $v) { |
| 605 | - if (substr($k, 0, 5) != "oauth") continue; |
|
| 606 | - if (is_array($v)) { |
|
| 605 | + if (substr($k, 0, 5) != "oauth") continue; |
|
| 606 | + if (is_array($v)) { |
|
| 607 | 607 | throw new OAuthClientException('Arrays not supported in headers.'); |
| 608 | - } |
|
| 609 | - $out .= ($first) ? ' ' : ','; |
|
| 610 | - $out .= OAuthUtil::urlencode_rfc3986($k) . |
|
| 611 | - '="' . |
|
| 612 | - OAuthUtil::urlencode_rfc3986($v) . |
|
| 613 | - '"'; |
|
| 614 | - $first = false; |
|
| 608 | + } |
|
| 609 | + $out .= ($first) ? ' ' : ','; |
|
| 610 | + $out .= OAuthUtil::urlencode_rfc3986($k) . |
|
| 611 | + '="' . |
|
| 612 | + OAuthUtil::urlencode_rfc3986($v) . |
|
| 613 | + '"'; |
|
| 614 | + $first = false; |
|
| 615 | 615 | } |
| 616 | 616 | return $out; |
| 617 | - } |
|
| 617 | + } |
|
| 618 | 618 | |
| 619 | - public function __toString() { |
|
| 619 | + public function __toString() { |
|
| 620 | 620 | return $this->to_url(); |
| 621 | - } |
|
| 621 | + } |
|
| 622 | 622 | |
| 623 | 623 | |
| 624 | - public function sign_request($signature_method, $consumer, $token) { |
|
| 624 | + public function sign_request($signature_method, $consumer, $token) { |
|
| 625 | 625 | $this->set_parameter( |
| 626 | - "oauth_signature_method", |
|
| 627 | - $signature_method->get_name(), |
|
| 628 | - false |
|
| 626 | + "oauth_signature_method", |
|
| 627 | + $signature_method->get_name(), |
|
| 628 | + false |
|
| 629 | 629 | ); |
| 630 | 630 | $signature = $this->build_signature($signature_method, $consumer, $token); |
| 631 | 631 | $this->set_parameter("oauth_signature", $signature, false); |
| 632 | - } |
|
| 632 | + } |
|
| 633 | 633 | |
| 634 | - public function build_signature($signature_method, $consumer, $token) { |
|
| 634 | + public function build_signature($signature_method, $consumer, $token) { |
|
| 635 | 635 | $signature = $signature_method->build_signature($this, $consumer, $token); |
| 636 | 636 | return $signature; |
| 637 | - } |
|
| 637 | + } |
|
| 638 | 638 | |
| 639 | - /** |
|
| 640 | - * util function: current timestamp |
|
| 641 | - */ |
|
| 642 | - private static function generate_timestamp() { |
|
| 639 | + /** |
|
| 640 | + * util function: current timestamp |
|
| 641 | + */ |
|
| 642 | + private static function generate_timestamp() { |
|
| 643 | 643 | return time(); |
| 644 | - } |
|
| 644 | + } |
|
| 645 | 645 | |
| 646 | - /** |
|
| 647 | - * util function: current nonce |
|
| 648 | - */ |
|
| 649 | - private static function generate_nonce() { |
|
| 646 | + /** |
|
| 647 | + * util function: current nonce |
|
| 648 | + */ |
|
| 649 | + private static function generate_nonce() { |
|
| 650 | 650 | $mt = microtime(); |
| 651 | 651 | $rand = mt_rand(); |
| 652 | 652 | |
| 653 | 653 | return md5($mt . $rand); // md5s look nicer than numbers |
| 654 | - } |
|
| 654 | + } |
|
| 655 | 655 | } |
| 656 | 656 | |
| 657 | 657 | class OAuthServer { |
| 658 | - protected $timestamp_threshold = 300; // in seconds, five minutes |
|
| 659 | - protected $version = '1.0'; // hi blaine |
|
| 660 | - protected $signature_methods = array(); |
|
| 658 | + protected $timestamp_threshold = 300; // in seconds, five minutes |
|
| 659 | + protected $version = '1.0'; // hi blaine |
|
| 660 | + protected $signature_methods = array(); |
|
| 661 | 661 | |
| 662 | - protected $data_store; |
|
| 662 | + protected $data_store; |
|
| 663 | 663 | |
| 664 | - function __construct($data_store) { |
|
| 664 | + function __construct($data_store) { |
|
| 665 | 665 | $this->data_store = $data_store; |
| 666 | - } |
|
| 666 | + } |
|
| 667 | 667 | |
| 668 | - public function add_signature_method($signature_method) { |
|
| 668 | + public function add_signature_method($signature_method) { |
|
| 669 | 669 | $this->signature_methods[$signature_method->get_name()] = |
| 670 | - $signature_method; |
|
| 671 | - } |
|
| 670 | + $signature_method; |
|
| 671 | + } |
|
| 672 | 672 | |
| 673 | - // high level functions |
|
| 673 | + // high level functions |
|
| 674 | 674 | |
| 675 | - /** |
|
| 676 | - * process a request_token request |
|
| 677 | - * returns the request token on success |
|
| 678 | - */ |
|
| 679 | - public function fetch_request_token(&$request) { |
|
| 675 | + /** |
|
| 676 | + * process a request_token request |
|
| 677 | + * returns the request token on success |
|
| 678 | + */ |
|
| 679 | + public function fetch_request_token(&$request) { |
|
| 680 | 680 | $this->get_version($request); |
| 681 | 681 | |
| 682 | 682 | $consumer = $this->get_consumer($request); |
@@ -691,13 +691,13 @@ discard block |
||
| 691 | 691 | $new_token = $this->data_store->new_request_token($consumer, $callback); |
| 692 | 692 | |
| 693 | 693 | return $new_token; |
| 694 | - } |
|
| 694 | + } |
|
| 695 | 695 | |
| 696 | - /** |
|
| 697 | - * process an access_token request |
|
| 698 | - * returns the access token on success |
|
| 699 | - */ |
|
| 700 | - public function fetch_access_token(&$request) { |
|
| 696 | + /** |
|
| 697 | + * process an access_token request |
|
| 698 | + * returns the access token on success |
|
| 699 | + */ |
|
| 700 | + public function fetch_access_token(&$request) { |
|
| 701 | 701 | $this->get_version($request); |
| 702 | 702 | |
| 703 | 703 | $consumer = $this->get_consumer($request); |
@@ -712,105 +712,105 @@ discard block |
||
| 712 | 712 | $new_token = $this->data_store->new_access_token($token, $consumer, $verifier); |
| 713 | 713 | |
| 714 | 714 | return $new_token; |
| 715 | - } |
|
| 715 | + } |
|
| 716 | 716 | |
| 717 | - /** |
|
| 718 | - * verify an api call, checks all the parameters |
|
| 719 | - */ |
|
| 720 | - public function verify_request(&$request) { |
|
| 717 | + /** |
|
| 718 | + * verify an api call, checks all the parameters |
|
| 719 | + */ |
|
| 720 | + public function verify_request(&$request) { |
|
| 721 | 721 | $this->get_version($request); |
| 722 | 722 | $consumer = $this->get_consumer($request); |
| 723 | 723 | $token = $this->get_token($request, $consumer, "access"); |
| 724 | 724 | $this->check_signature($request, $consumer, $token); |
| 725 | 725 | return array($consumer, $token); |
| 726 | - } |
|
| 726 | + } |
|
| 727 | 727 | |
| 728 | - // Internals from here |
|
| 729 | - /** |
|
| 730 | - * version 1 |
|
| 731 | - */ |
|
| 732 | - protected function get_version(&$request) { |
|
| 728 | + // Internals from here |
|
| 729 | + /** |
|
| 730 | + * version 1 |
|
| 731 | + */ |
|
| 732 | + protected function get_version(&$request) { |
|
| 733 | 733 | $version = $request->get_parameter("oauth_version"); |
| 734 | 734 | if (!$version) { |
| 735 | - // Service Providers MUST assume the protocol version to be 1.0 if this parameter is not present. |
|
| 736 | - // Chapter 7.0 ("Accessing Protected Ressources") |
|
| 737 | - $version = '1.0'; |
|
| 735 | + // Service Providers MUST assume the protocol version to be 1.0 if this parameter is not present. |
|
| 736 | + // Chapter 7.0 ("Accessing Protected Ressources") |
|
| 737 | + $version = '1.0'; |
|
| 738 | 738 | } |
| 739 | 739 | if ($version !== $this->version) { |
| 740 | - throw new OAuthVersionNotSupportedException("OAuth version '$version' not supported."); |
|
| 740 | + throw new OAuthVersionNotSupportedException("OAuth version '$version' not supported."); |
|
| 741 | 741 | } |
| 742 | 742 | return $version; |
| 743 | - } |
|
| 743 | + } |
|
| 744 | 744 | |
| 745 | - /** |
|
| 746 | - * figure out the signature with some defaults |
|
| 747 | - */ |
|
| 748 | - private function get_signature_method($request) { |
|
| 745 | + /** |
|
| 746 | + * figure out the signature with some defaults |
|
| 747 | + */ |
|
| 748 | + private function get_signature_method($request) { |
|
| 749 | 749 | $signature_method = $request instanceof OAuthRequest |
| 750 | 750 | ? $request->get_parameter("oauth_signature_method") |
| 751 | 751 | : NULL; |
| 752 | 752 | |
| 753 | 753 | if (!$signature_method) { |
| 754 | - // According to chapter 7 ("Accessing Protected Ressources") the signature-method |
|
| 755 | - // parameter is required, and we can't just fallback to PLAINTEXT |
|
| 756 | - throw new OAuthMissingParameterException('oauth_signature_method'); |
|
| 754 | + // According to chapter 7 ("Accessing Protected Ressources") the signature-method |
|
| 755 | + // parameter is required, and we can't just fallback to PLAINTEXT |
|
| 756 | + throw new OAuthMissingParameterException('oauth_signature_method'); |
|
| 757 | 757 | } |
| 758 | 758 | |
| 759 | 759 | if (!in_array($signature_method, |
| 760 | - array_keys($this->signature_methods))) { |
|
| 761 | - throw new OAuthUnsupportedSignatureMethodException( |
|
| 760 | + array_keys($this->signature_methods))) { |
|
| 761 | + throw new OAuthUnsupportedSignatureMethodException( |
|
| 762 | 762 | "Signature method '$signature_method' not supported " . |
| 763 | 763 | "try one of the following: " . |
| 764 | 764 | implode(", ", array_keys($this->signature_methods)) . "." |
| 765 | - ); |
|
| 765 | + ); |
|
| 766 | 766 | } |
| 767 | 767 | return $this->signature_methods[$signature_method]; |
| 768 | - } |
|
| 768 | + } |
|
| 769 | 769 | |
| 770 | - /** |
|
| 771 | - * try to find the consumer for the provided request's consumer key |
|
| 772 | - */ |
|
| 773 | - protected function get_consumer($request) { |
|
| 770 | + /** |
|
| 771 | + * try to find the consumer for the provided request's consumer key |
|
| 772 | + */ |
|
| 773 | + protected function get_consumer($request) { |
|
| 774 | 774 | $consumer_key = $request instanceof OAuthRequest |
| 775 | 775 | ? $request->get_parameter("oauth_consumer_key") |
| 776 | 776 | : NULL; |
| 777 | 777 | |
| 778 | 778 | if (!$consumer_key) { |
| 779 | - throw new OAuthMissingParameterException('oauth_consumer_key'); |
|
| 779 | + throw new OAuthMissingParameterException('oauth_consumer_key'); |
|
| 780 | 780 | } |
| 781 | 781 | |
| 782 | 782 | $consumer = $this->data_store->lookup_consumer($consumer_key); |
| 783 | 783 | if (!$consumer) { |
| 784 | - throw new OAuthInvalidConsumerException("Invalid consumer"); |
|
| 784 | + throw new OAuthInvalidConsumerException("Invalid consumer"); |
|
| 785 | 785 | } |
| 786 | 786 | |
| 787 | 787 | return $consumer; |
| 788 | - } |
|
| 788 | + } |
|
| 789 | 789 | |
| 790 | - /** |
|
| 791 | - * try to find the token for the provided request's token key |
|
| 792 | - */ |
|
| 793 | - protected function get_token($request, $consumer, $token_type="access") { |
|
| 790 | + /** |
|
| 791 | + * try to find the token for the provided request's token key |
|
| 792 | + */ |
|
| 793 | + protected function get_token($request, $consumer, $token_type="access") { |
|
| 794 | 794 | $token_field = $request instanceof OAuthRequest |
| 795 | 795 | ? $request->get_parameter('oauth_token') |
| 796 | 796 | : NULL; |
| 797 | 797 | if (!$token_field) { |
| 798 | - throw new OAuthMissingParameterException('oauth_token'); |
|
| 798 | + throw new OAuthMissingParameterException('oauth_token'); |
|
| 799 | 799 | } |
| 800 | 800 | $token = $this->data_store->lookup_token( |
| 801 | - $consumer, $token_type, $token_field |
|
| 801 | + $consumer, $token_type, $token_field |
|
| 802 | 802 | ); |
| 803 | 803 | if (!$token) { |
| 804 | - throw new OAuthInvalidTokenException("Invalid $token_type token: $token_field."); |
|
| 804 | + throw new OAuthInvalidTokenException("Invalid $token_type token: $token_field."); |
|
| 805 | 805 | } |
| 806 | 806 | return $token; |
| 807 | - } |
|
| 807 | + } |
|
| 808 | 808 | |
| 809 | - /** |
|
| 810 | - * all-in-one function to check the signature on a request |
|
| 811 | - * should guess the signature method appropriately |
|
| 812 | - */ |
|
| 813 | - protected function check_signature($request, $consumer, $token) { |
|
| 809 | + /** |
|
| 810 | + * all-in-one function to check the signature on a request |
|
| 811 | + * should guess the signature method appropriately |
|
| 812 | + */ |
|
| 813 | + protected function check_signature($request, $consumer, $token) { |
|
| 814 | 814 | // this should probably be in a different method |
| 815 | 815 | $timestamp = $request instanceof OAuthRequest |
| 816 | 816 | ? $request->get_parameter('oauth_timestamp') |
@@ -826,23 +826,23 @@ discard block |
||
| 826 | 826 | |
| 827 | 827 | $signature = $request->get_parameter('oauth_signature'); |
| 828 | 828 | $valid_sig = $signature_method->check_signature( |
| 829 | - $request, |
|
| 830 | - $consumer, |
|
| 831 | - $token, |
|
| 832 | - $signature |
|
| 829 | + $request, |
|
| 830 | + $consumer, |
|
| 831 | + $token, |
|
| 832 | + $signature |
|
| 833 | 833 | ); |
| 834 | 834 | |
| 835 | 835 | if (!$valid_sig) { |
| 836 | - throw new OAuthInvalidSignatureException("Invalid signature."); |
|
| 836 | + throw new OAuthInvalidSignatureException("Invalid signature."); |
|
| 837 | + } |
|
| 837 | 838 | } |
| 838 | - } |
|
| 839 | 839 | |
| 840 | - /** |
|
| 841 | - * check that the timestamp is new enough |
|
| 842 | - */ |
|
| 843 | - private function check_timestamp($timestamp) { |
|
| 840 | + /** |
|
| 841 | + * check that the timestamp is new enough |
|
| 842 | + */ |
|
| 843 | + private function check_timestamp($timestamp) { |
|
| 844 | 844 | if( ! $timestamp ) |
| 845 | - throw new OAuthMissingParameterException('oauth_timestamp'); |
|
| 845 | + throw new OAuthMissingParameterException('oauth_timestamp'); |
|
| 846 | 846 | |
| 847 | 847 | // Cast to integer. See issue #314. |
| 848 | 848 | $timestamp = $timestamp + 0; |
@@ -850,178 +850,178 @@ discard block |
||
| 850 | 850 | // verify that timestamp is recentish |
| 851 | 851 | $now = time(); |
| 852 | 852 | if (abs($now - $timestamp) > $this->timestamp_threshold) { |
| 853 | - throw new OAuthExpiredTimestampException($timestamp, $now, |
|
| 853 | + throw new OAuthExpiredTimestampException($timestamp, $now, |
|
| 854 | 854 | $this->timestamp_threshold); |
| 855 | 855 | } |
| 856 | - } |
|
| 856 | + } |
|
| 857 | 857 | |
| 858 | - /** |
|
| 859 | - * check that the nonce is not repeated |
|
| 860 | - */ |
|
| 861 | - private function check_nonce($consumer, $token, $nonce, $timestamp) { |
|
| 858 | + /** |
|
| 859 | + * check that the nonce is not repeated |
|
| 860 | + */ |
|
| 861 | + private function check_nonce($consumer, $token, $nonce, $timestamp) { |
|
| 862 | 862 | if( ! $nonce ) |
| 863 | - throw new OAuthMissingParameterException('oauth_nonce'); |
|
| 863 | + throw new OAuthMissingParameterException('oauth_nonce'); |
|
| 864 | 864 | |
| 865 | 865 | // verify that the nonce is uniqueish |
| 866 | 866 | $found = $this->data_store->lookup_nonce( |
| 867 | - $consumer, |
|
| 868 | - $token, |
|
| 869 | - $nonce, |
|
| 870 | - $timestamp |
|
| 867 | + $consumer, |
|
| 868 | + $token, |
|
| 869 | + $nonce, |
|
| 870 | + $timestamp |
|
| 871 | 871 | ); |
| 872 | 872 | if ($found) { |
| 873 | - throw new OAuthNonceAlreadyUsedException("Nonce already used: $nonce."); |
|
| 873 | + throw new OAuthNonceAlreadyUsedException("Nonce already used: $nonce."); |
|
| 874 | + } |
|
| 874 | 875 | } |
| 875 | - } |
|
| 876 | 876 | |
| 877 | 877 | } |
| 878 | 878 | |
| 879 | 879 | class OAuthDataStore { |
| 880 | - function lookup_consumer($consumer_key) { |
|
| 880 | + function lookup_consumer($consumer_key) { |
|
| 881 | 881 | // implement me |
| 882 | - } |
|
| 882 | + } |
|
| 883 | 883 | |
| 884 | - function lookup_token($consumer, $token_type, $token) { |
|
| 884 | + function lookup_token($consumer, $token_type, $token) { |
|
| 885 | 885 | // implement me |
| 886 | - } |
|
| 886 | + } |
|
| 887 | 887 | |
| 888 | - function lookup_nonce($consumer, $token, $nonce, $timestamp) { |
|
| 888 | + function lookup_nonce($consumer, $token, $nonce, $timestamp) { |
|
| 889 | 889 | // implement me |
| 890 | - } |
|
| 890 | + } |
|
| 891 | 891 | |
| 892 | - function new_request_token($consumer, $callback = null) { |
|
| 892 | + function new_request_token($consumer, $callback = null) { |
|
| 893 | 893 | // return a new token attached to this consumer |
| 894 | - } |
|
| 894 | + } |
|
| 895 | 895 | |
| 896 | - function new_access_token($token, $consumer, $verifier = null) { |
|
| 896 | + function new_access_token($token, $consumer, $verifier = null) { |
|
| 897 | 897 | // return a new access token attached to this consumer |
| 898 | 898 | // for the user associated with this token if the request token |
| 899 | 899 | // is authorized |
| 900 | 900 | // should also invalidate the request token |
| 901 | - } |
|
| 901 | + } |
|
| 902 | 902 | |
| 903 | 903 | } |
| 904 | 904 | |
| 905 | 905 | class OAuthUtil { |
| 906 | - public static function urlencode_rfc3986($input) { |
|
| 907 | - if (is_array($input)) { |
|
| 906 | + public static function urlencode_rfc3986($input) { |
|
| 907 | + if (is_array($input)) { |
|
| 908 | 908 | return array_map(array('\okapi\oauth\OAuthUtil', 'urlencode_rfc3986'), $input); |
| 909 | - } else if (is_scalar($input)) { |
|
| 909 | + } else if (is_scalar($input)) { |
|
| 910 | 910 | return str_replace( |
| 911 | - '+', |
|
| 912 | - ' ', |
|
| 913 | - str_replace('%7E', '~', rawurlencode($input)) |
|
| 911 | + '+', |
|
| 912 | + ' ', |
|
| 913 | + str_replace('%7E', '~', rawurlencode($input)) |
|
| 914 | 914 | ); |
| 915 | - } else { |
|
| 915 | + } else { |
|
| 916 | 916 | return ''; |
| 917 | - } |
|
| 917 | + } |
|
| 918 | 918 | } |
| 919 | 919 | |
| 920 | 920 | |
| 921 | - // This decode function isn't taking into consideration the above |
|
| 922 | - // modifications to the encoding process. However, this method doesn't |
|
| 923 | - // seem to be used anywhere so leaving it as is. |
|
| 924 | - public static function urldecode_rfc3986($string) { |
|
| 921 | + // This decode function isn't taking into consideration the above |
|
| 922 | + // modifications to the encoding process. However, this method doesn't |
|
| 923 | + // seem to be used anywhere so leaving it as is. |
|
| 924 | + public static function urldecode_rfc3986($string) { |
|
| 925 | 925 | return urldecode($string); |
| 926 | - } |
|
| 927 | - |
|
| 928 | - // Utility function for turning the Authorization: header into |
|
| 929 | - // parameters, has to do some unescaping |
|
| 930 | - // Can filter out any non-oauth parameters if needed (default behaviour) |
|
| 931 | - // May 28th, 2010 - method updated to tjerk.meesters for a speed improvement. |
|
| 932 | - // see http://code.google.com/p/oauth/issues/detail?id=163 |
|
| 933 | - public static function split_header($header, $only_allow_oauth_parameters = true) { |
|
| 926 | + } |
|
| 927 | + |
|
| 928 | + // Utility function for turning the Authorization: header into |
|
| 929 | + // parameters, has to do some unescaping |
|
| 930 | + // Can filter out any non-oauth parameters if needed (default behaviour) |
|
| 931 | + // May 28th, 2010 - method updated to tjerk.meesters for a speed improvement. |
|
| 932 | + // see http://code.google.com/p/oauth/issues/detail?id=163 |
|
| 933 | + public static function split_header($header, $only_allow_oauth_parameters = true) { |
|
| 934 | 934 | $params = array(); |
| 935 | 935 | if (preg_match_all('/('.($only_allow_oauth_parameters ? 'oauth_' : '').'[a-z_-]*)=(:?"([^"]*)"|([^,]*))/', $header, $matches)) { |
| 936 | - foreach ($matches[1] as $i => $h) { |
|
| 936 | + foreach ($matches[1] as $i => $h) { |
|
| 937 | 937 | $params[$h] = OAuthUtil::urldecode_rfc3986(empty($matches[3][$i]) ? $matches[4][$i] : $matches[3][$i]); |
| 938 | - } |
|
| 939 | - if (isset($params['realm'])) { |
|
| 938 | + } |
|
| 939 | + if (isset($params['realm'])) { |
|
| 940 | 940 | unset($params['realm']); |
| 941 | - } |
|
| 941 | + } |
|
| 942 | 942 | } |
| 943 | 943 | return $params; |
| 944 | - } |
|
| 944 | + } |
|
| 945 | 945 | |
| 946 | - // helper to try to sort out headers for people who aren't running apache |
|
| 947 | - public static function get_headers() { |
|
| 946 | + // helper to try to sort out headers for people who aren't running apache |
|
| 947 | + public static function get_headers() { |
|
| 948 | 948 | if (function_exists('apache_request_headers')) { |
| 949 | - // we need this to get the actual Authorization: header |
|
| 950 | - // because apache tends to tell us it doesn't exist |
|
| 951 | - $headers = apache_request_headers(); |
|
| 952 | - |
|
| 953 | - // sanitize the output of apache_request_headers because |
|
| 954 | - // we always want the keys to be Cased-Like-This and arh() |
|
| 955 | - // returns the headers in the same case as they are in the |
|
| 956 | - // request |
|
| 957 | - $out = array(); |
|
| 958 | - foreach ($headers AS $key => $value) { |
|
| 949 | + // we need this to get the actual Authorization: header |
|
| 950 | + // because apache tends to tell us it doesn't exist |
|
| 951 | + $headers = apache_request_headers(); |
|
| 952 | + |
|
| 953 | + // sanitize the output of apache_request_headers because |
|
| 954 | + // we always want the keys to be Cased-Like-This and arh() |
|
| 955 | + // returns the headers in the same case as they are in the |
|
| 956 | + // request |
|
| 957 | + $out = array(); |
|
| 958 | + foreach ($headers AS $key => $value) { |
|
| 959 | 959 | $key = str_replace( |
| 960 | 960 | " ", |
| 961 | 961 | "-", |
| 962 | 962 | ucwords(strtolower(str_replace("-", " ", $key))) |
| 963 | - ); |
|
| 963 | + ); |
|
| 964 | 964 | $out[$key] = $value; |
| 965 | - } |
|
| 965 | + } |
|
| 966 | 966 | } else { |
| 967 | - // otherwise we don't have apache and are just going to have to hope |
|
| 968 | - // that $_SERVER actually contains what we need |
|
| 969 | - $out = array(); |
|
| 970 | - if( isset($_SERVER['CONTENT_TYPE']) ) |
|
| 967 | + // otherwise we don't have apache and are just going to have to hope |
|
| 968 | + // that $_SERVER actually contains what we need |
|
| 969 | + $out = array(); |
|
| 970 | + if( isset($_SERVER['CONTENT_TYPE']) ) |
|
| 971 | 971 | $out['Content-Type'] = $_SERVER['CONTENT_TYPE']; |
| 972 | - if( isset($_ENV['CONTENT_TYPE']) ) |
|
| 972 | + if( isset($_ENV['CONTENT_TYPE']) ) |
|
| 973 | 973 | $out['Content-Type'] = $_ENV['CONTENT_TYPE']; |
| 974 | 974 | |
| 975 | - foreach ($_SERVER as $key => $value) { |
|
| 975 | + foreach ($_SERVER as $key => $value) { |
|
| 976 | 976 | if (substr($key, 0, 5) == "HTTP_") { |
| 977 | - // this is chaos, basically it is just there to capitalize the first |
|
| 978 | - // letter of every word that is not an initial HTTP and strip HTTP |
|
| 979 | - // code from przemek |
|
| 980 | - $key = str_replace( |
|
| 977 | + // this is chaos, basically it is just there to capitalize the first |
|
| 978 | + // letter of every word that is not an initial HTTP and strip HTTP |
|
| 979 | + // code from przemek |
|
| 980 | + $key = str_replace( |
|
| 981 | 981 | " ", |
| 982 | 982 | "-", |
| 983 | 983 | ucwords(strtolower(str_replace("_", " ", substr($key, 5)))) |
| 984 | - ); |
|
| 985 | - $out[$key] = $value; |
|
| 984 | + ); |
|
| 985 | + $out[$key] = $value; |
|
| 986 | + } |
|
| 986 | 987 | } |
| 987 | - } |
|
| 988 | 988 | } |
| 989 | 989 | return $out; |
| 990 | - } |
|
| 990 | + } |
|
| 991 | 991 | |
| 992 | - // This function takes a input like a=b&a=c&d=e and returns the parsed |
|
| 993 | - // parameters like this |
|
| 994 | - // array('a' => array('b','c'), 'd' => 'e') |
|
| 995 | - public static function parse_parameters( $input ) { |
|
| 992 | + // This function takes a input like a=b&a=c&d=e and returns the parsed |
|
| 993 | + // parameters like this |
|
| 994 | + // array('a' => array('b','c'), 'd' => 'e') |
|
| 995 | + public static function parse_parameters( $input ) { |
|
| 996 | 996 | if (!isset($input) || !$input) return array(); |
| 997 | 997 | |
| 998 | 998 | $pairs = explode('&', $input); |
| 999 | 999 | |
| 1000 | 1000 | $parsed_parameters = array(); |
| 1001 | 1001 | foreach ($pairs as $pair) { |
| 1002 | - $split = explode('=', $pair, 2); |
|
| 1003 | - $parameter = OAuthUtil::urldecode_rfc3986($split[0]); |
|
| 1004 | - $value = isset($split[1]) ? OAuthUtil::urldecode_rfc3986($split[1]) : ''; |
|
| 1002 | + $split = explode('=', $pair, 2); |
|
| 1003 | + $parameter = OAuthUtil::urldecode_rfc3986($split[0]); |
|
| 1004 | + $value = isset($split[1]) ? OAuthUtil::urldecode_rfc3986($split[1]) : ''; |
|
| 1005 | 1005 | |
| 1006 | - if (isset($parsed_parameters[$parameter])) { |
|
| 1006 | + if (isset($parsed_parameters[$parameter])) { |
|
| 1007 | 1007 | // We have already recieved parameter(s) with this name, so add to the list |
| 1008 | 1008 | // of parameters with this name |
| 1009 | 1009 | |
| 1010 | 1010 | if (is_scalar($parsed_parameters[$parameter])) { |
| 1011 | - // This is the first duplicate, so transform scalar (string) into an array |
|
| 1012 | - // so we can add the duplicates |
|
| 1013 | - $parsed_parameters[$parameter] = array($parsed_parameters[$parameter]); |
|
| 1011 | + // This is the first duplicate, so transform scalar (string) into an array |
|
| 1012 | + // so we can add the duplicates |
|
| 1013 | + $parsed_parameters[$parameter] = array($parsed_parameters[$parameter]); |
|
| 1014 | 1014 | } |
| 1015 | 1015 | |
| 1016 | 1016 | $parsed_parameters[$parameter][] = $value; |
| 1017 | - } else { |
|
| 1017 | + } else { |
|
| 1018 | 1018 | $parsed_parameters[$parameter] = $value; |
| 1019 | - } |
|
| 1019 | + } |
|
| 1020 | 1020 | } |
| 1021 | 1021 | return $parsed_parameters; |
| 1022 | - } |
|
| 1022 | + } |
|
| 1023 | 1023 | |
| 1024 | - public static function build_http_query($params) { |
|
| 1024 | + public static function build_http_query($params) { |
|
| 1025 | 1025 | if (!$params) return ''; |
| 1026 | 1026 | |
| 1027 | 1027 | // Urlencode both keys and values |
@@ -1035,22 +1035,22 @@ discard block |
||
| 1035 | 1035 | |
| 1036 | 1036 | $pairs = array(); |
| 1037 | 1037 | foreach ($params as $parameter => $value) { |
| 1038 | - if (is_array($value)) { |
|
| 1038 | + if (is_array($value)) { |
|
| 1039 | 1039 | // If two or more parameters share the same name, they are sorted by their value |
| 1040 | 1040 | // Ref: Spec: 9.1.1 (1) |
| 1041 | 1041 | // June 12th, 2010 - changed to sort because of issue 164 by hidetaka |
| 1042 | 1042 | sort($value, SORT_STRING); |
| 1043 | 1043 | foreach ($value as $duplicate_value) { |
| 1044 | - $pairs[] = $parameter . '=' . $duplicate_value; |
|
| 1044 | + $pairs[] = $parameter . '=' . $duplicate_value; |
|
| 1045 | 1045 | } |
| 1046 | - } else { |
|
| 1046 | + } else { |
|
| 1047 | 1047 | $pairs[] = $parameter . '=' . $value; |
| 1048 | - } |
|
| 1048 | + } |
|
| 1049 | 1049 | } |
| 1050 | 1050 | // For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61) |
| 1051 | 1051 | // Each name-value pair is separated by an '&' character (ASCII code 38) |
| 1052 | 1052 | return implode('&', $pairs); |
| 1053 | - } |
|
| 1053 | + } |
|
| 1054 | 1054 | } |
| 1055 | 1055 | |
| 1056 | 1056 | ?> |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | public $key; |
| 173 | 173 | public $secret; |
| 174 | 174 | |
| 175 | - function __construct($key, $secret, $callback_url=NULL) { |
|
| 175 | + function __construct($key, $secret, $callback_url = NULL) { |
|
| 176 | 176 | $this->key = $key; |
| 177 | 177 | $this->secret = $secret; |
| 178 | 178 | $this->callback_url = $callback_url; |
@@ -403,9 +403,9 @@ discard block |
||
| 403 | 403 | public static $version = '1.0'; |
| 404 | 404 | public static $POST_INPUT = 'php://input'; |
| 405 | 405 | |
| 406 | - function __construct($http_method, $http_url, $parameters=NULL) { |
|
| 406 | + function __construct($http_method, $http_url, $parameters = NULL) { |
|
| 407 | 407 | $parameters = ($parameters) ? $parameters : array(); |
| 408 | - $parameters = array_merge( OAuthUtil::parse_parameters(parse_url($http_url, PHP_URL_QUERY)), $parameters); |
|
| 408 | + $parameters = array_merge(OAuthUtil::parse_parameters(parse_url($http_url, PHP_URL_QUERY)), $parameters); |
|
| 409 | 409 | $this->parameters = $parameters; |
| 410 | 410 | $this->http_method = $http_method; |
| 411 | 411 | $this->http_url = $http_url; |
@@ -415,7 +415,7 @@ discard block |
||
| 415 | 415 | /** |
| 416 | 416 | * attempt to build up a request from what was passed to the server |
| 417 | 417 | */ |
| 418 | - public static function from_request($http_method=NULL, $http_url=NULL, $parameters=NULL) { |
|
| 418 | + public static function from_request($http_method = NULL, $http_url = NULL, $parameters = NULL) { |
|
| 419 | 419 | $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") |
| 420 | 420 | ? 'http' |
| 421 | 421 | : 'https'; |
@@ -467,8 +467,8 @@ discard block |
||
| 467 | 467 | /** |
| 468 | 468 | * pretty much a helper function to set up the request |
| 469 | 469 | */ |
| 470 | - public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL) { |
|
| 471 | - $parameters = ($parameters) ? $parameters : array(); |
|
| 470 | + public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters = NULL) { |
|
| 471 | + $parameters = ($parameters) ? $parameters : array(); |
|
| 472 | 472 | $defaults = array("oauth_version" => OAuthRequest::$version, |
| 473 | 473 | "oauth_nonce" => OAuthRequest::generate_nonce(), |
| 474 | 474 | "oauth_timestamp" => OAuthRequest::generate_timestamp(), |
@@ -577,7 +577,7 @@ discard block |
||
| 577 | 577 | $post_data = $this->to_postdata(); |
| 578 | 578 | $out = $this->get_normalized_http_url(); |
| 579 | 579 | if ($post_data) { |
| 580 | - $out .= '?'.$post_data; |
|
| 580 | + $out .= '?' . $post_data; |
|
| 581 | 581 | } |
| 582 | 582 | return $out; |
| 583 | 583 | } |
@@ -592,9 +592,9 @@ discard block |
||
| 592 | 592 | /** |
| 593 | 593 | * builds the Authorization: header |
| 594 | 594 | */ |
| 595 | - public function to_header($realm=null) { |
|
| 595 | + public function to_header($realm = null) { |
|
| 596 | 596 | $first = true; |
| 597 | - if($realm) { |
|
| 597 | + if ($realm) { |
|
| 598 | 598 | $out = 'Authorization: OAuth realm="' . OAuthUtil::urlencode_rfc3986($realm) . '"'; |
| 599 | 599 | $first = false; |
| 600 | 600 | } else |
@@ -656,7 +656,7 @@ discard block |
||
| 656 | 656 | |
| 657 | 657 | class OAuthServer { |
| 658 | 658 | protected $timestamp_threshold = 300; // in seconds, five minutes |
| 659 | - protected $version = '1.0'; // hi blaine |
|
| 659 | + protected $version = '1.0'; // hi blaine |
|
| 660 | 660 | protected $signature_methods = array(); |
| 661 | 661 | |
| 662 | 662 | protected $data_store; |
@@ -790,7 +790,7 @@ discard block |
||
| 790 | 790 | /** |
| 791 | 791 | * try to find the token for the provided request's token key |
| 792 | 792 | */ |
| 793 | - protected function get_token($request, $consumer, $token_type="access") { |
|
| 793 | + protected function get_token($request, $consumer, $token_type = "access") { |
|
| 794 | 794 | $token_field = $request instanceof OAuthRequest |
| 795 | 795 | ? $request->get_parameter('oauth_token') |
| 796 | 796 | : NULL; |
@@ -841,7 +841,7 @@ discard block |
||
| 841 | 841 | * check that the timestamp is new enough |
| 842 | 842 | */ |
| 843 | 843 | private function check_timestamp($timestamp) { |
| 844 | - if( ! $timestamp ) |
|
| 844 | + if (!$timestamp) |
|
| 845 | 845 | throw new OAuthMissingParameterException('oauth_timestamp'); |
| 846 | 846 | |
| 847 | 847 | // Cast to integer. See issue #314. |
@@ -859,7 +859,7 @@ discard block |
||
| 859 | 859 | * check that the nonce is not repeated |
| 860 | 860 | */ |
| 861 | 861 | private function check_nonce($consumer, $token, $nonce, $timestamp) { |
| 862 | - if( ! $nonce ) |
|
| 862 | + if (!$nonce) |
|
| 863 | 863 | throw new OAuthMissingParameterException('oauth_nonce'); |
| 864 | 864 | |
| 865 | 865 | // verify that the nonce is uniqueish |
@@ -932,7 +932,7 @@ discard block |
||
| 932 | 932 | // see http://code.google.com/p/oauth/issues/detail?id=163 |
| 933 | 933 | public static function split_header($header, $only_allow_oauth_parameters = true) { |
| 934 | 934 | $params = array(); |
| 935 | - if (preg_match_all('/('.($only_allow_oauth_parameters ? 'oauth_' : '').'[a-z_-]*)=(:?"([^"]*)"|([^,]*))/', $header, $matches)) { |
|
| 935 | + if (preg_match_all('/(' . ($only_allow_oauth_parameters ? 'oauth_' : '') . '[a-z_-]*)=(:?"([^"]*)"|([^,]*))/', $header, $matches)) { |
|
| 936 | 936 | foreach ($matches[1] as $i => $h) { |
| 937 | 937 | $params[$h] = OAuthUtil::urldecode_rfc3986(empty($matches[3][$i]) ? $matches[4][$i] : $matches[3][$i]); |
| 938 | 938 | } |
@@ -967,9 +967,9 @@ discard block |
||
| 967 | 967 | // otherwise we don't have apache and are just going to have to hope |
| 968 | 968 | // that $_SERVER actually contains what we need |
| 969 | 969 | $out = array(); |
| 970 | - if( isset($_SERVER['CONTENT_TYPE']) ) |
|
| 970 | + if (isset($_SERVER['CONTENT_TYPE'])) |
|
| 971 | 971 | $out['Content-Type'] = $_SERVER['CONTENT_TYPE']; |
| 972 | - if( isset($_ENV['CONTENT_TYPE']) ) |
|
| 972 | + if (isset($_ENV['CONTENT_TYPE'])) |
|
| 973 | 973 | $out['Content-Type'] = $_ENV['CONTENT_TYPE']; |
| 974 | 974 | |
| 975 | 975 | foreach ($_SERVER as $key => $value) { |
@@ -992,7 +992,7 @@ discard block |
||
| 992 | 992 | // This function takes a input like a=b&a=c&d=e and returns the parsed |
| 993 | 993 | // parameters like this |
| 994 | 994 | // array('a' => array('b','c'), 'd' => 'e') |
| 995 | - public static function parse_parameters( $input ) { |
|
| 995 | + public static function parse_parameters($input) { |
|
| 996 | 996 | if (!isset($input) || !$input) return array(); |
| 997 | 997 | |
| 998 | 998 | $pairs = explode('&', $input); |
@@ -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)); |