@@ -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; |
@@ -34,8 +34,8 @@ discard block |
||
34 | 34 | |
35 | 35 | public function scanTranslateFunctionCalls() |
36 | 36 | { |
37 | - $nNextPos = strpos($this->msContent, "t" . "('"); |
|
38 | - $nNext_ = strpos($this->msContent, "_" . "('"); |
|
37 | + $nNextPos = strpos($this->msContent, "t"."('"); |
|
38 | + $nNext_ = strpos($this->msContent, "_"."('"); |
|
39 | 39 | if ($nNext_ !== false && ($nNextPos === false || $nNext_ < $nNextPos)) { |
40 | 40 | $nNextPos = $nNext_; |
41 | 41 | } |
@@ -68,8 +68,8 @@ discard block |
||
68 | 68 | ); |
69 | 69 | } |
70 | 70 | |
71 | - $nNext_ = strpos($this->msContent, "_" . "('", $nNextPos + 1); |
|
72 | - $nNextPos = strpos($this->msContent, "t" . "('", $nNextPos + 1); |
|
71 | + $nNext_ = strpos($this->msContent, "_"."('", $nNextPos + 1); |
|
72 | + $nNextPos = strpos($this->msContent, "t"."('", $nNextPos + 1); |
|
73 | 73 | if ($nNext_ !== false && ($nNextPos === false || $nNext_ < $nNextPos)) { |
74 | 74 | $nNextPos = $nNext_; |
75 | 75 | } |
@@ -92,11 +92,11 @@ discard block |
||
92 | 92 | |
93 | 93 | public function scanTranslationPlaceholders() |
94 | 94 | { |
95 | - $nNextPos = strpos($this->msContent, '{' . 't'); |
|
95 | + $nNextPos = strpos($this->msContent, '{'.'t'); |
|
96 | 96 | while ($nNextPos !== false) { |
97 | 97 | // check for match |
98 | - if ((substr($this->msContent, $nNextPos, 3) == '{' . 't}') || |
|
99 | - (substr($this->msContent, $nNextPos, 3) == '{' . 't ') |
|
98 | + if ((substr($this->msContent, $nNextPos, 3) == '{'.'t}') || |
|
99 | + (substr($this->msContent, $nNextPos, 3) == '{'.'t ') |
|
100 | 100 | ) { |
101 | 101 | $nStart = strpos($this->msContent, '}', $nNextPos); |
102 | 102 | $nEnd = strpos($this->msContent, '{/t}', $nNextPos); |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | ); |
112 | 112 | } |
113 | 113 | |
114 | - $nNextPos = strpos($this->msContent, '{' . 't', $nNextPos + 1); |
|
114 | + $nNextPos = strpos($this->msContent, '{'.'t', $nNextPos + 1); |
|
115 | 115 | } |
116 | 116 | } |
117 | 117 | |
@@ -120,9 +120,9 @@ discard block |
||
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 |
@@ -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 @@ discard block |
||
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) { |
@@ -308,9 +308,9 @@ discard block |
||
308 | 308 | $retval = 'E '; |
309 | 309 | } |
310 | 310 | |
311 | - $retval = $retval . sprintf("%03d", floor($lon)) . '° '; |
|
311 | + $retval = $retval.sprintf("%03d", floor($lon)).'° '; |
|
312 | 312 | $lon = $lon - floor($lon); |
313 | - $retval = $retval . sprintf("%06.3f", round($lon * 60, 3)) . '\''; |
|
313 | + $retval = $retval.sprintf("%06.3f", round($lon * 60, 3)).'\''; |
|
314 | 314 | |
315 | 315 | return $retval; |
316 | 316 | } |
@@ -325,9 +325,9 @@ discard block |
||
325 | 325 | $retval = 'N '; |
326 | 326 | } |
327 | 327 | |
328 | - $retval = $retval . sprintf("%02d", floor($lat)) . '° '; |
|
328 | + $retval = $retval.sprintf("%02d", floor($lat)).'° '; |
|
329 | 329 | $lat = $lat - floor($lat); |
330 | - $retval = $retval . sprintf("%06.3f", round($lat * 60, 3)) . '\''; |
|
330 | + $retval = $retval.sprintf("%06.3f", round($lat * 60, 3)).'\''; |
|
331 | 331 | |
332 | 332 | return $retval; |
333 | 333 | } |
@@ -350,7 +350,7 @@ |
||
350 | 350 | while (mb_strlen($retval) < mb_strlen($str)) { |
351 | 351 | $nNextStart = false; |
352 | 352 | $sNextEndChar = ''; |
353 | - foreach ($delimiter AS $del) { |
|
353 | + foreach ($delimiter as $del) { |
|
354 | 354 | $nThisStart = mb_strpos($str, $del[0], mb_strlen($retval)); |
355 | 355 | |
356 | 356 | if ($nThisStart !== false) { |
@@ -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; |
@@ -5,8 +5,8 @@ discard block |
||
5 | 5 | * Unicode Reminder メモ |
6 | 6 | ***************************************************************************/ |
7 | 7 | |
8 | -require __DIR__ . '/lib2/web.inc.php'; |
|
9 | -require_once __DIR__ . '/lib2/edithelper.inc.php'; |
|
8 | +require __DIR__.'/lib2/web.inc.php'; |
|
9 | +require_once __DIR__.'/lib2/edithelper.inc.php'; |
|
10 | 10 | |
11 | 11 | $tpl->name = 'mydetails'; |
12 | 12 | $tpl->menuitem = MNU_MYPROFILE_DETAILS; |
@@ -51,8 +51,8 @@ discard block |
||
51 | 51 | |
52 | 52 | while ($record = sql_fetch_array($rs)) { |
53 | 53 | $id = $record['id']; |
54 | - $vis = isset($_REQUEST['chk' . $id]) ? $_REQUEST['chk' . $id] + 0 : 0; |
|
55 | - $value = isset($_REQUEST['inp' . $id]) ? $_REQUEST['inp' . $id] : ''; |
|
54 | + $vis = isset($_REQUEST['chk'.$id]) ? $_REQUEST['chk'.$id] + 0 : 0; |
|
55 | + $value = isset($_REQUEST['inp'.$id]) ? $_REQUEST['inp'.$id] : ''; |
|
56 | 56 | if ($vis != 1) { |
57 | 57 | $vis = 0; |
58 | 58 | } |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | $bErrorlen = true; |
64 | 64 | } else { |
65 | 65 | if (!$useroptions->setOptValue($id, $value)) { |
66 | - $error .= $useroptions->getOptName($id) . ', '; |
|
66 | + $error .= $useroptions->getOptName($id).', '; |
|
67 | 67 | $bError = true; |
68 | 68 | } |
69 | 69 | } |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | } |
173 | 173 | if ($descMode == 3) { |
174 | 174 | $tpl->add_header_javascript('resource2/tinymce/tiny_mce_gzip.js'); |
175 | - $tpl->add_header_javascript('resource2/tinymce/config/user.js.php?lang=' . strtolower($opt['template']['locale'])); |
|
175 | + $tpl->add_header_javascript('resource2/tinymce/config/user.js.php?lang='.strtolower($opt['template']['locale'])); |
|
176 | 176 | } |
177 | 177 | $tpl->add_header_javascript(editorJsPath()); |
178 | 178 | $tpl->assign('descMode', $descMode); |
@@ -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; |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | * Unicode Reminder メモ |
6 | 6 | ***************************************************************************/ |
7 | 7 | |
8 | -require __DIR__ . '/lib2/web.inc.php'; |
|
8 | +require __DIR__.'/lib2/web.inc.php'; |
|
9 | 9 | |
10 | 10 | $tpl->name = 'myprofile'; |
11 | 11 | $tpl->menuitem = MNU_MYPROFILE_DATA; |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | |
20 | 20 | if ($login->userid == 0) { |
21 | 21 | if ($action == 'change' || $action == 'changeemail') { |
22 | - $tpl->redirect('login.php?target=' . urlencode('myprofile.php?action=change')); |
|
22 | + $tpl->redirect('login.php?target='.urlencode('myprofile.php?action=change')); |
|
23 | 23 | } else { |
24 | 24 | $tpl->redirect('login.php?target=myprofile.php'); |
25 | 25 | } |
@@ -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 | { |
@@ -7,19 +7,18 @@ |
||
7 | 7 | # If you want to include_once/require_once OKAPI in your code, |
8 | 8 | # see facade.php. You should not rely on any other file, never! |
9 | 9 | |
10 | -use Exception; |
|
11 | -use ErrorException; |
|
12 | 10 | use ArrayObject; |
13 | -use okapi\oauth\OAuthServerException; |
|
14 | -use okapi\oauth\OAuthServer400Exception; |
|
15 | -use okapi\oauth\OAuthServer401Exception; |
|
16 | -use okapi\oauth\OAuthMissingParameterException; |
|
11 | +use ErrorException; |
|
12 | +use Exception; |
|
13 | +use okapi\cronjobs\CronJobController; |
|
17 | 14 | use okapi\oauth\OAuthConsumer; |
18 | -use okapi\oauth\OAuthToken; |
|
15 | +use okapi\oauth\OAuthMissingParameterException; |
|
16 | +use okapi\oauth\OAuthRequest; |
|
19 | 17 | use okapi\oauth\OAuthServer; |
18 | +use okapi\oauth\OAuthServer400Exception; |
|
19 | +use okapi\oauth\OAuthServerException; |
|
20 | 20 | use okapi\oauth\OAuthSignatureMethod_HMAC_SHA1; |
21 | -use okapi\oauth\OAuthRequest; |
|
22 | -use okapi\cronjobs\CronJobController; |
|
21 | +use okapi\oauth\OAuthToken; |
|
23 | 22 | |
24 | 23 | /** Return an array of email addresses which always get notified on OKAPI errors. */ |
25 | 24 | function get_admin_emails() |
@@ -367,7 +367,7 @@ discard block |
||
367 | 367 | if (mysql_connect(Settings::get('DB_SERVER'), Settings::get('DB_USERNAME'), Settings::get('DB_PASSWORD'))) |
368 | 368 | { |
369 | 369 | mysql_select_db(Settings::get('DB_NAME')); |
370 | - mysql_query("set names '" . Settings::get('DB_CHARSET') . "'"); |
|
370 | + mysql_query("set names '".Settings::get('DB_CHARSET')."'"); |
|
371 | 371 | self::$connected = true; |
372 | 372 | } |
373 | 373 | else |
@@ -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; |
@@ -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; |
@@ -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() |
@@ -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() |
@@ -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"); |
@@ -1261,10 +1261,10 @@ 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 | - preg_replace("/^https:/", "http:", Settings::get('SITE_URL')) . 'okapi/', |
|
1267 | - preg_replace("/^http:/", "https:", Settings::get('SITE_URL')) . 'okapi/', |
|
1266 | + preg_replace("/^https:/", "http:", Settings::get('SITE_URL')).'okapi/', |
|
1267 | + preg_replace("/^http:/", "https:", Settings::get('SITE_URL')).'okapi/', |
|
1268 | 1268 | ); |
1269 | 1269 | } else { |
1270 | 1270 | $urls = array( |
@@ -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 | } |
@@ -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 | } |
@@ -1742,7 +1742,7 @@ 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 | |
@@ -1798,7 +1798,7 @@ discard block |
||
1798 | 1798 | else |
1799 | 1799 | { |
1800 | 1800 | # That's a bug. |
1801 | - throw new Exception("Cannot encode as xmlmap2: " . print_r($obj, true)); |
|
1801 | + throw new Exception("Cannot encode as xmlmap2: ".print_r($obj, true)); |
|
1802 | 1802 | } |
1803 | 1803 | } |
1804 | 1804 | |
@@ -2048,7 +2048,7 @@ 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 | 2054 | $value = eval('return '.$value.';'); |
@@ -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,7 +2084,7 @@ 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) |
@@ -2122,7 +2122,7 @@ 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) |
@@ -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" |
@@ -2452,8 +2452,8 @@ discard block |
||
2452 | 2452 | if (is_object($this->consumer) && $this->consumer->hasFlag(OkapiConsumer::FLAG_KEY_REVOKED)) { |
2453 | 2453 | throw new InvalidParam( |
2454 | 2454 | 'consumer_key', |
2455 | - "Your application was denied access to the " . |
|
2456 | - Okapi::get_normalized_site_name() . " site " . |
|
2455 | + "Your application was denied access to the ". |
|
2456 | + Okapi::get_normalized_site_name()." site ". |
|
2457 | 2457 | "(this consumer key has been revoked)." |
2458 | 2458 | ); |
2459 | 2459 | } |
@@ -25,18 +25,18 @@ discard block |
||
25 | 25 | function get_admin_emails() |
26 | 26 | { |
27 | 27 | $emails = array(); |
28 | - if (class_exists("okapi\\Settings")) |
|
29 | - { |
|
30 | - try |
|
31 | - { |
|
32 | - foreach (Settings::get('ADMINS') as $email) |
|
33 | - if (!in_array($email, $emails)) |
|
28 | + if (class_exists("okapi\\Settings")) { |
|
29 | + try { |
|
30 | + foreach (Settings::get('ADMINS') as $email) { |
|
31 | + if (!in_array($email, $emails)) |
|
34 | 32 | $emails[] = $email; |
35 | - } |
|
36 | - catch (Exception $e) { /* pass */ } |
|
33 | + } |
|
34 | + } catch (Exception $e) { |
|
35 | +/* pass */ } |
|
36 | + } |
|
37 | + if (count($emails) == 0) { |
|
38 | + $emails[] = 'root@localhost'; |
|
37 | 39 | } |
38 | - if (count($emails) == 0) |
|
39 | - $emails[] = 'root@localhost'; |
|
40 | 40 | return $emails; |
41 | 41 | } |
42 | 42 | |
@@ -45,12 +45,15 @@ discard block |
||
45 | 45 | # |
46 | 46 | |
47 | 47 | /** A base class for all bad request exceptions. */ |
48 | -class BadRequest extends Exception { |
|
49 | - protected function provideExtras(&$extras) { |
|
48 | +class BadRequest extends Exception |
|
49 | +{ |
|
50 | + protected function provideExtras(&$extras) |
|
51 | + { |
|
50 | 52 | $extras['reason_stack'][] = 'bad_request'; |
51 | 53 | $extras['status'] = 400; |
52 | 54 | } |
53 | - public function getOkapiJSON() { |
|
55 | + public function getOkapiJSON() |
|
56 | + { |
|
54 | 57 | $extras = array( |
55 | 58 | 'developer_message' => $this->getMessage(), |
56 | 59 | 'reason_stack' => array(), |
@@ -62,7 +65,9 @@ discard block |
||
62 | 65 | } |
63 | 66 | |
64 | 67 | /** Thrown on PHP's FATAL errors (detected in a shutdown function). */ |
65 | -class FatalError extends ErrorException {} |
|
68 | +class FatalError extends ErrorException |
|
69 | +{ |
|
70 | +} |
|
66 | 71 | |
67 | 72 | # |
68 | 73 | # We'll try to make PHP into something more decent. Exception and |
@@ -75,45 +80,40 @@ discard block |
||
75 | 80 | /** Handle exception thrown while executing OKAPI request. */ |
76 | 81 | public static function handle($e) |
77 | 82 | { |
78 | - if ($e instanceof OAuthServerException) |
|
79 | - { |
|
83 | + if ($e instanceof OAuthServerException) { |
|
80 | 84 | # This is thrown on invalid OAuth requests. There are many subclasses |
81 | 85 | # of this exception. All of them result in HTTP 400 or HTTP 401 error |
82 | 86 | # code. See also: http://oauth.net/core/1.0a/#http_codes |
83 | 87 | |
84 | - if ($e instanceof OAuthServer400Exception) |
|
85 | - header("HTTP/1.0 400 Bad Request"); |
|
86 | - else |
|
87 | - header("HTTP/1.0 401 Unauthorized"); |
|
88 | + if ($e instanceof OAuthServer400Exception) { |
|
89 | + header("HTTP/1.0 400 Bad Request"); |
|
90 | + } else { |
|
91 | + header("HTTP/1.0 401 Unauthorized"); |
|
92 | + } |
|
88 | 93 | header("Access-Control-Allow-Origin: *"); |
89 | 94 | header("Content-Type: application/json; charset=utf-8"); |
90 | 95 | |
91 | 96 | print $e->getOkapiJSON(); |
92 | - } |
|
93 | - elseif ($e instanceof BadRequest) |
|
94 | - { |
|
97 | + } elseif ($e instanceof BadRequest) { |
|
95 | 98 | # Intentionally thrown from within the OKAPI method code. |
96 | 99 | # Consumer (aka external developer) had something wrong with his |
97 | 100 | # request and we want him to know that. |
98 | 101 | |
99 | 102 | # headers may have been sent e.g. by views/update |
100 | - if (!headers_sent()) |
|
101 | - { |
|
103 | + if (!headers_sent()) { |
|
102 | 104 | header("HTTP/1.0 400 Bad Request"); |
103 | 105 | header("Access-Control-Allow-Origin: *"); |
104 | 106 | header("Content-Type: application/json; charset=utf-8"); |
105 | 107 | } |
106 | 108 | |
107 | 109 | print $e->getOkapiJSON(); |
108 | - } |
|
109 | - else # (ErrorException, MySQL exception etc.) |
|
110 | + } else # (ErrorException, MySQL exception etc.) |
|
110 | 111 | { |
111 | 112 | # This one is thrown on PHP notices and warnings - usually this |
112 | 113 | # indicates an error in OKAPI method. If thrown, then something |
113 | 114 | # must be fixed on OUR part. |
114 | 115 | |
115 | - if (!headers_sent()) |
|
116 | - { |
|
116 | + if (!headers_sent()) { |
|
117 | 117 | header("HTTP/1.0 500 Internal Server Error"); |
118 | 118 | header("Access-Control-Allow-Origin: *"); |
119 | 119 | header("Content-Type: text/plain; charset=utf-8"); |
@@ -127,19 +127,15 @@ discard block |
||
127 | 127 | |
128 | 128 | $exception_info = self::get_exception_info($e); |
129 | 129 | |
130 | - if (class_exists("okapi\\Settings") && (Settings::get('DEBUG'))) |
|
131 | - { |
|
130 | + if (class_exists("okapi\\Settings") && (Settings::get('DEBUG'))) { |
|
132 | 131 | print "\n\nBUT! Since the DEBUG flag is on, then you probably ARE a developer yourself.\n"; |
133 | 132 | print "Let's cut to the chase then:"; |
134 | 133 | print "\n\n".$exception_info; |
135 | 134 | } |
136 | - if (class_exists("okapi\\Settings") && (Settings::get('DEBUG_PREVENT_EMAILS'))) |
|
137 | - { |
|
135 | + if (class_exists("okapi\\Settings") && (Settings::get('DEBUG_PREVENT_EMAILS'))) { |
|
138 | 136 | # Sending emails was blocked on admin's demand. |
139 | 137 | # This is possible only on development environment. |
140 | - } |
|
141 | - else |
|
142 | - { |
|
138 | + } else { |
|
143 | 139 | $subject = "OKAPI Method Error - ".substr( |
144 | 140 | $_SERVER['REQUEST_URI'], 0, strpos( |
145 | 141 | $_SERVER['REQUEST_URI'].'?', '?')); |
@@ -150,19 +146,17 @@ discard block |
||
150 | 146 | "developer of the module that threw this error. Thanks!\n\n". |
151 | 147 | $exception_info |
152 | 148 | ); |
153 | - try |
|
154 | - { |
|
149 | + try { |
|
155 | 150 | Okapi::mail_admins($subject, $message); |
156 | - } |
|
157 | - catch (Exception $e) |
|
158 | - { |
|
151 | + } catch (Exception $e) { |
|
159 | 152 | # Unable to use full-featured mail_admins version. We'll use a backup. |
160 | 153 | # We need to make sure we're not spamming. |
161 | 154 | |
162 | 155 | $lock_file = "/tmp/okapi-fatal-error-mode"; |
163 | 156 | $last_email = false; |
164 | - if (file_exists($lock_file)) |
|
165 | - $last_email = filemtime($lock_file); |
|
157 | + if (file_exists($lock_file)) { |
|
158 | + $last_email = filemtime($lock_file); |
|
159 | + } |
|
166 | 160 | if ($last_email === false) { |
167 | 161 | # Assume this is the first email. |
168 | 162 | $last_email = 0; |
@@ -212,16 +206,13 @@ discard block |
||
212 | 206 | $exception_info = "===== ERROR MESSAGE =====\n" |
213 | 207 | .trim(self::removeSensitiveData($e->getMessage())) |
214 | 208 | ."\n=========================\n\n"; |
215 | - if ($e instanceof FatalError) |
|
216 | - { |
|
209 | + if ($e instanceof FatalError) { |
|
217 | 210 | # This one doesn't have a stack trace. It is fed directly to OkapiExceptionHandler::handle |
218 | 211 | # by OkapiErrorHandler::handle_shutdown. Instead of printing trace, we will just print |
219 | 212 | # the file and line. |
220 | 213 | |
221 | 214 | $exception_info .= "File: ".$e->getFile()."\nLine: ".$e->getLine()."\n\n"; |
222 | - } |
|
223 | - else |
|
224 | - { |
|
215 | + } else { |
|
225 | 216 | $exception_info .= "--- Stack trace ---\n". |
226 | 217 | self::removeSensitiveData($e->getTraceAsString())."\n\n"; |
227 | 218 | } |
@@ -234,8 +225,7 @@ discard block |
||
234 | 225 | # This if-condition will solve some (but not all) problems when trying to execute |
235 | 226 | # OKAPI code from command line; |
236 | 227 | # see https://github.com/opencaching/okapi/issues/243. |
237 | - if (function_exists('getallheaders')) |
|
238 | - { |
|
228 | + if (function_exists('getallheaders')) { |
|
239 | 229 | $exception_info .= "--- Request headers ---\n".implode("\n", array_map( |
240 | 230 | function($k, $v) { return "$k: $v"; }, |
241 | 231 | array_keys(getallheaders()), array_values(getallheaders()) |
@@ -254,7 +244,9 @@ discard block |
||
254 | 244 | /** Handle error encountered while executing OKAPI request. */ |
255 | 245 | public static function handle($severity, $message, $filename, $lineno) |
256 | 246 | { |
257 | - if ($severity == E_STRICT || $severity == E_DEPRECATED) return false; |
|
247 | + if ($severity == E_STRICT || $severity == E_DEPRECATED) { |
|
248 | + return false; |
|
249 | + } |
|
258 | 250 | if (($severity == E_NOTICE) && !self::$treat_notices_as_errors) { |
259 | 251 | return false; |
260 | 252 | } |
@@ -283,8 +275,7 @@ discard block |
||
283 | 275 | # correctly. The only error which cannot be recovered from is E_ERROR, we have |
284 | 276 | # to check the type then. |
285 | 277 | |
286 | - if (($error !== null) && ($error['type'] == E_ERROR)) |
|
287 | - { |
|
278 | + if (($error !== null) && ($error['type'] == E_ERROR)) { |
|
288 | 279 | $e = new FatalError($error['message'], 0, $error['type'], $error['file'], $error['line']); |
289 | 280 | OkapiExceptionHandler::handle($e); |
290 | 281 | } |
@@ -304,13 +295,16 @@ discard block |
||
304 | 295 | # the developer). |
305 | 296 | # |
306 | 297 | |
307 | -class Http404 extends BadRequest {} |
|
298 | +class Http404 extends BadRequest |
|
299 | +{ |
|
300 | +} |
|
308 | 301 | |
309 | 302 | /** Common type of BadRequest: Required parameter is missing. */ |
310 | 303 | class ParamMissing extends BadRequest |
311 | 304 | { |
312 | 305 | private $paramName; |
313 | - protected function provideExtras(&$extras) { |
|
306 | + protected function provideExtras(&$extras) |
|
307 | + { |
|
314 | 308 | parent::provideExtras($extras); |
315 | 309 | $extras['reason_stack'][] = 'missing_parameter'; |
316 | 310 | $extras['parameter'] = $this->paramName; |
@@ -330,7 +324,8 @@ discard block |
||
330 | 324 | /** What was wrong about the param? */ |
331 | 325 | public $whats_wrong_about_it; |
332 | 326 | |
333 | - protected function provideExtras(&$extras) { |
|
327 | + protected function provideExtras(&$extras) |
|
328 | + { |
|
334 | 329 | parent::provideExtras($extras); |
335 | 330 | $extras['reason_stack'][] = 'invalid_parameter'; |
336 | 331 | $extras['parameter'] = $this->paramName; |
@@ -340,15 +335,18 @@ discard block |
||
340 | 335 | { |
341 | 336 | $this->paramName = $paramName; |
342 | 337 | $this->whats_wrong_about_it = $whats_wrong_about_it; |
343 | - if ($whats_wrong_about_it) |
|
344 | - parent::__construct("Parameter '$paramName' has invalid value: ".$whats_wrong_about_it, $code); |
|
345 | - else |
|
346 | - parent::__construct("Parameter '$paramName' has invalid value.", $code); |
|
338 | + if ($whats_wrong_about_it) { |
|
339 | + parent::__construct("Parameter '$paramName' has invalid value: ".$whats_wrong_about_it, $code); |
|
340 | + } else { |
|
341 | + parent::__construct("Parameter '$paramName' has invalid value.", $code); |
|
342 | + } |
|
347 | 343 | } |
348 | 344 | } |
349 | 345 | |
350 | 346 | /** Thrown on invalid SQL queries. */ |
351 | -class DbException extends Exception {} |
|
347 | +class DbException extends Exception |
|
348 | +{ |
|
349 | +} |
|
352 | 350 | |
353 | 351 | # |
354 | 352 | # Database access abstraction layer. |
@@ -364,22 +362,20 @@ discard block |
||
364 | 362 | |
365 | 363 | public static function connect() |
366 | 364 | { |
367 | - if (mysql_connect(Settings::get('DB_SERVER'), Settings::get('DB_USERNAME'), Settings::get('DB_PASSWORD'))) |
|
368 | - { |
|
365 | + if (mysql_connect(Settings::get('DB_SERVER'), Settings::get('DB_USERNAME'), Settings::get('DB_PASSWORD'))) { |
|
369 | 366 | mysql_select_db(Settings::get('DB_NAME')); |
370 | 367 | mysql_query("set names '" . Settings::get('DB_CHARSET') . "'"); |
371 | 368 | self::$connected = true; |
369 | + } else { |
|
370 | + throw new Exception("Could not connect to MySQL: ".mysql_error()); |
|
372 | 371 | } |
373 | - else |
|
374 | - throw new Exception("Could not connect to MySQL: ".mysql_error()); |
|
375 | 372 | } |
376 | 373 | |
377 | 374 | /** Fetch [{row}], return {row}. */ |
378 | 375 | public static function select_row($query) |
379 | 376 | { |
380 | 377 | $rows = self::select_all($query); |
381 | - switch (count($rows)) |
|
382 | - { |
|
378 | + switch (count($rows)) { |
|
383 | 379 | case 0: return null; |
384 | 380 | case 1: return $rows[0]; |
385 | 381 | default: |
@@ -399,15 +395,16 @@ discard block |
||
399 | 395 | private static function select_and_push($query, & $arr, $keyField = null) |
400 | 396 | { |
401 | 397 | $rs = self::query($query); |
402 | - while (true) |
|
403 | - { |
|
398 | + while (true) { |
|
404 | 399 | $row = Db::fetch_assoc($rs); |
405 | - if ($row === false) |
|
406 | - break; |
|
407 | - if ($keyField == null) |
|
408 | - $arr[] = $row; |
|
409 | - else |
|
410 | - $arr[$row[$keyField]] = $row; |
|
400 | + if ($row === false) { |
|
401 | + break; |
|
402 | + } |
|
403 | + if ($keyField == null) { |
|
404 | + $arr[] = $row; |
|
405 | + } else { |
|
406 | + $arr[$row[$keyField]] = $row; |
|
407 | + } |
|
411 | 408 | } |
412 | 409 | Db::free_result($rs); |
413 | 410 | } |
@@ -417,11 +414,11 @@ discard block |
||
417 | 414 | { |
418 | 415 | $groups = array(); |
419 | 416 | $rs = self::query($query); |
420 | - while (true) |
|
421 | - { |
|
417 | + while (true) { |
|
422 | 418 | $row = Db::fetch_assoc($rs); |
423 | - if ($row === false) |
|
424 | - break; |
|
419 | + if ($row === false) { |
|
420 | + break; |
|
421 | + } |
|
425 | 422 | $groups[$row[$keyField]][] = $row; |
426 | 423 | } |
427 | 424 | Db::free_result($rs); |
@@ -432,10 +429,12 @@ discard block |
||
432 | 429 | public static function select_value($query) |
433 | 430 | { |
434 | 431 | $column = self::select_column($query); |
435 | - if ($column == null) |
|
436 | - return null; |
|
437 | - if (count($column) == 1) |
|
438 | - return $column[0]; |
|
432 | + if ($column == null) { |
|
433 | + return null; |
|
434 | + } |
|
435 | + if (count($column) == 1) { |
|
436 | + return $column[0]; |
|
437 | + } |
|
439 | 438 | throw new DbException("Invalid query. Db::select_value returned more than one row for:\n\n".$query."\n"); |
440 | 439 | } |
441 | 440 | |
@@ -444,11 +443,11 @@ discard block |
||
444 | 443 | { |
445 | 444 | $column = array(); |
446 | 445 | $rs = self::query($query); |
447 | - while (true) |
|
448 | - { |
|
446 | + while (true) { |
|
449 | 447 | $values = Db::fetch_row($rs); |
450 | - if ($values === false) |
|
451 | - break; |
|
448 | + if ($values === false) { |
|
449 | + break; |
|
450 | + } |
|
452 | 451 | array_push($column, $values[0]); |
453 | 452 | } |
454 | 453 | Db::free_result($rs); |
@@ -488,18 +487,19 @@ discard block |
||
488 | 487 | public static function execute($query) |
489 | 488 | { |
490 | 489 | $rs = self::query($query); |
491 | - if ($rs !== true) |
|
492 | - throw new DbException("Db::execute returned a result set for your query. ". |
|
490 | + if ($rs !== true) { |
|
491 | + throw new DbException("Db::execute returned a result set for your query. ". |
|
493 | 492 | "You should use Db::select_* or Db::query for SELECT queries!"); |
493 | + } |
|
494 | 494 | } |
495 | 495 | |
496 | 496 | public static function query($query) |
497 | 497 | { |
498 | - if (!self::$connected) |
|
499 | - self::connect(); |
|
498 | + if (!self::$connected) { |
|
499 | + self::connect(); |
|
500 | + } |
|
500 | 501 | $rs = mysql_query($query); |
501 | - if (!$rs) |
|
502 | - { |
|
502 | + if (!$rs) { |
|
503 | 503 | $errno = mysql_errno(); |
504 | 504 | $msg = mysql_error(); |
505 | 505 | |
@@ -555,8 +555,9 @@ discard block |
||
555 | 555 | |
556 | 556 | public static function field_exists($table, $field) |
557 | 557 | { |
558 | - if (!preg_match("/[a-z0-9_]+/", $table.$field)) |
|
559 | - return false; |
|
558 | + if (!preg_match("/[a-z0-9_]+/", $table.$field)) { |
|
559 | + return false; |
|
560 | + } |
|
560 | 561 | try { |
561 | 562 | $spec = self::select_all("desc ".$table.";"); |
562 | 563 | } catch (Exception $e) { |
@@ -564,8 +565,9 @@ discard block |
||
564 | 565 | return false; |
565 | 566 | } |
566 | 567 | foreach ($spec as &$row_ref) { |
567 | - if (strtoupper($row_ref['Field']) == strtoupper($field)) |
|
568 | - return true; |
|
568 | + if (strtoupper($row_ref['Field']) == strtoupper($field)) { |
|
569 | + return true; |
|
570 | + } |
|
569 | 571 | } |
570 | 572 | return false; |
571 | 573 | } |
@@ -765,10 +767,11 @@ discard block |
||
765 | 767 | } catch (OAuthMissingParameterException $e) { |
766 | 768 | # Note, that exception will be different if token is supplied |
767 | 769 | # and is invalid. We catch only a completely MISSING token parameter. |
768 | - if (($e->getParamName() == 'oauth_token') && (!$token_required)) |
|
769 | - $token = null; |
|
770 | - else |
|
771 | - throw $e; |
|
770 | + if (($e->getParamName() == 'oauth_token') && (!$token_required)) { |
|
771 | + $token = null; |
|
772 | + } else { |
|
773 | + throw $e; |
|
774 | + } |
|
772 | 775 | } |
773 | 776 | $this->check_signature($request, $consumer, $token); |
774 | 777 | return array($consumer, $token); |
@@ -798,21 +801,22 @@ discard block |
||
798 | 801 | |
799 | 802 | public function get_length() |
800 | 803 | { |
801 | - if (is_resource($this->body)) |
|
802 | - return $this->stream_length; |
|
804 | + if (is_resource($this->body)) { |
|
805 | + return $this->stream_length; |
|
806 | + } |
|
803 | 807 | return strlen($this->body); |
804 | 808 | } |
805 | 809 | |
806 | 810 | /** Note: You can call this only once! */ |
807 | 811 | public function print_body() |
808 | 812 | { |
809 | - if (is_resource($this->body)) |
|
810 | - { |
|
811 | - while (!feof($this->body)) |
|
812 | - print fread($this->body, 1024*1024); |
|
813 | + if (is_resource($this->body)) { |
|
814 | + while (!feof($this->body)) { |
|
815 | + print fread($this->body, 1024*1024); |
|
816 | + } |
|
817 | + } else { |
|
818 | + print $this->body; |
|
813 | 819 | } |
814 | - else |
|
815 | - print $this->body; |
|
816 | 820 | } |
817 | 821 | |
818 | 822 | /** |
@@ -821,14 +825,13 @@ discard block |
||
821 | 825 | */ |
822 | 826 | public function get_body() |
823 | 827 | { |
824 | - if (is_resource($this->body)) |
|
825 | - { |
|
828 | + if (is_resource($this->body)) { |
|
826 | 829 | ob_start(); |
827 | 830 | fpassthru($this->body); |
828 | 831 | return ob_get_clean(); |
832 | + } else { |
|
833 | + return $this->body; |
|
829 | 834 | } |
830 | - else |
|
831 | - return $this->body; |
|
832 | 835 | } |
833 | 836 | |
834 | 837 | /** |
@@ -840,32 +843,33 @@ discard block |
||
840 | 843 | header("Access-Control-Allow-Origin: *"); |
841 | 844 | header("Content-Type: ".$this->content_type); |
842 | 845 | header("Cache-Control: ".$this->cache_control); |
843 | - if ($this->connection_close) |
|
844 | - header("Connection: close"); |
|
845 | - if ($this->content_disposition) |
|
846 | - header("Content-Disposition: ".$this->content_disposition); |
|
847 | - if ($this->etag) |
|
848 | - header("ETag: $this->etag"); |
|
846 | + if ($this->connection_close) { |
|
847 | + header("Connection: close"); |
|
848 | + } |
|
849 | + if ($this->content_disposition) { |
|
850 | + header("Content-Disposition: ".$this->content_disposition); |
|
851 | + } |
|
852 | + if ($this->etag) { |
|
853 | + header("ETag: $this->etag"); |
|
854 | + } |
|
849 | 855 | |
850 | 856 | # Make sure that gzip is supported by the client. |
851 | 857 | $use_gzip = $this->allow_gzip; |
852 | - if (empty($_SERVER["HTTP_ACCEPT_ENCODING"]) || (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") === false)) |
|
853 | - $use_gzip = false; |
|
858 | + if (empty($_SERVER["HTTP_ACCEPT_ENCODING"]) || (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") === false)) { |
|
859 | + $use_gzip = false; |
|
860 | + } |
|
854 | 861 | |
855 | 862 | # We will gzip the data ourselves, while disabling gziping by Apache. This way, we can |
856 | 863 | # set the Content-Length correctly which is handy in some scenarios. |
857 | 864 | |
858 | - if ($use_gzip && is_string($this->body)) |
|
859 | - { |
|
865 | + if ($use_gzip && is_string($this->body)) { |
|
860 | 866 | # Apache won't gzip a response which is already gzipped. |
861 | 867 | |
862 | 868 | header("Content-Encoding: gzip"); |
863 | 869 | $gzipped = gzencode($this->body, 5); |
864 | 870 | header("Content-Length: ".strlen($gzipped)); |
865 | 871 | print $gzipped; |
866 | - } |
|
867 | - else |
|
868 | - { |
|
872 | + } else { |
|
869 | 873 | # We don't want Apache to gzip this response. Tell it so. |
870 | 874 | |
871 | 875 | if (function_exists('apache_setenv')) { |
@@ -873,8 +877,9 @@ discard block |
||
873 | 877 | } |
874 | 878 | |
875 | 879 | $length = $this->get_length(); |
876 | - if ($length) |
|
877 | - header("Content-Length: ".$length); |
|
880 | + if ($length) { |
|
881 | + header("Content-Length: ".$length); |
|
882 | + } |
|
878 | 883 | $this->print_body(); |
879 | 884 | } |
880 | 885 | } |
@@ -883,7 +888,9 @@ discard block |
||
883 | 888 | class OkapiRedirectResponse extends OkapiHttpResponse |
884 | 889 | { |
885 | 890 | public $url; |
886 | - public function __construct($url) { $this->url = $url; } |
|
891 | + public function __construct($url) |
|
892 | + { |
|
893 | +$this->url = $url; } |
|
887 | 894 | public function display() |
888 | 895 | { |
889 | 896 | header("HTTP/1.1 303 See Other"); |
@@ -949,14 +956,11 @@ discard block |
||
949 | 956 | |
950 | 957 | private function __construct($name) |
951 | 958 | { |
952 | - if (Settings::get('DEBUG_PREVENT_SEMAPHORES')) |
|
953 | - { |
|
959 | + if (Settings::get('DEBUG_PREVENT_SEMAPHORES')) { |
|
954 | 960 | # Using semaphores is forbidden on this server by its admin. |
955 | 961 | # This is possible only on development environment. |
956 | 962 | $this->lock = null; |
957 | - } |
|
958 | - else |
|
959 | - { |
|
963 | + } else { |
|
960 | 964 | $this->lockfile = Okapi::get_var_dir()."/okapi-lock-".$name; |
961 | 965 | $this->lock = fopen($this->lockfile, "wb"); |
962 | 966 | } |
@@ -964,22 +968,26 @@ discard block |
||
964 | 968 | |
965 | 969 | public function acquire() |
966 | 970 | { |
967 | - if ($this->lock !== null) |
|
968 | - flock($this->lock, LOCK_EX); |
|
971 | + if ($this->lock !== null) { |
|
972 | + flock($this->lock, LOCK_EX); |
|
973 | + } |
|
969 | 974 | } |
970 | 975 | |
971 | 976 | public function try_acquire() |
972 | 977 | { |
973 | - if ($this->lock !== null) |
|
974 | - return flock($this->lock, LOCK_EX | LOCK_NB); |
|
975 | - else |
|
976 | - return true; # $lock can be null only when debugging |
|
978 | + if ($this->lock !== null) { |
|
979 | + return flock($this->lock, LOCK_EX | LOCK_NB); |
|
980 | + } else { |
|
981 | + return true; |
|
982 | + } |
|
983 | + # $lock can be null only when debugging |
|
977 | 984 | } |
978 | 985 | |
979 | 986 | public function release() |
980 | 987 | { |
981 | - if ($this->lock !== null) |
|
982 | - flock($this->lock, LOCK_UN); |
|
988 | + if ($this->lock !== null) { |
|
989 | + flock($this->lock, LOCK_UN); |
|
990 | + } |
|
983 | 991 | } |
984 | 992 | |
985 | 993 | /** |
@@ -988,8 +996,7 @@ discard block |
||
988 | 996 | */ |
989 | 997 | public function remove() |
990 | 998 | { |
991 | - if ($this->lock !== null) |
|
992 | - { |
|
999 | + if ($this->lock !== null) { |
|
993 | 1000 | fclose($this->lock); |
994 | 1001 | unlink($this->lockfile); |
995 | 1002 | } |
@@ -1033,18 +1040,19 @@ discard block |
||
1033 | 1040 | /** Get a variable stored in okapi_vars. If variable not found, return $default. */ |
1034 | 1041 | public static function get_var($varname, $default = null) |
1035 | 1042 | { |
1036 | - if (self::$okapi_vars === null) |
|
1037 | - { |
|
1043 | + if (self::$okapi_vars === null) { |
|
1038 | 1044 | $rs = Db::query(" |
1039 | 1045 | select var, value |
1040 | 1046 | from okapi_vars |
1041 | 1047 | "); |
1042 | 1048 | self::$okapi_vars = array(); |
1043 | - while ($row = Db::fetch_assoc($rs)) |
|
1044 | - self::$okapi_vars[$row['var']] = $row['value']; |
|
1049 | + while ($row = Db::fetch_assoc($rs)) { |
|
1050 | + self::$okapi_vars[$row['var']] = $row['value']; |
|
1051 | + } |
|
1052 | + } |
|
1053 | + if (isset(self::$okapi_vars[$varname])) { |
|
1054 | + return self::$okapi_vars[$varname]; |
|
1045 | 1055 | } |
1046 | - if (isset(self::$okapi_vars[$varname])) |
|
1047 | - return self::$okapi_vars[$varname]; |
|
1048 | 1056 | return $default; |
1049 | 1057 | } |
1050 | 1058 | |
@@ -1100,8 +1108,9 @@ discard block |
||
1100 | 1108 | |
1101 | 1109 | return; |
1102 | 1110 | } |
1103 | - if ($counter === null) |
|
1104 | - $counter = 0; |
|
1111 | + if ($counter === null) { |
|
1112 | + $counter = 0; |
|
1113 | + } |
|
1105 | 1114 | $counter++; |
1106 | 1115 | try { |
1107 | 1116 | Cache::set($cache_key, $counter, 3600); |
@@ -1109,20 +1118,16 @@ discard block |
||
1109 | 1118 | # If `get` suceeded and `set` did not, then probably we're having |
1110 | 1119 | # issue #156 scenario. We can ignore it here. |
1111 | 1120 | } |
1112 | - if ($counter <= 5) |
|
1113 | - { |
|
1121 | + if ($counter <= 5) { |
|
1114 | 1122 | # We're not spamming yet. |
1115 | 1123 | |
1116 | 1124 | self::mail_from_okapi(get_admin_emails(), $subject, $message); |
1117 | - } |
|
1118 | - else |
|
1119 | - { |
|
1125 | + } else { |
|
1120 | 1126 | # We are spamming. Prevent sending more emails. |
1121 | 1127 | |
1122 | 1128 | $content_cache_key_prefix = 'mail_admins_spam/'.(floor(time() / 3600) * 3600).'/'; |
1123 | 1129 | $timeout = 86400; |
1124 | - if ($counter == 6) |
|
1125 | - { |
|
1130 | + if ($counter == 6) { |
|
1126 | 1131 | self::mail_from_okapi(get_admin_emails(), "Anti-spam mode activated for '$subject'", |
1127 | 1132 | "OKAPI has activated an \"anti-spam\" mode for the following subject:\n\n". |
1128 | 1133 | "\"$subject\"\n\n". |
@@ -1142,14 +1147,14 @@ discard block |
||
1142 | 1147 | /** Send an email message from OKAPI to the given recipients. */ |
1143 | 1148 | public static function mail_from_okapi($email_addresses, $subject, $message) |
1144 | 1149 | { |
1145 | - if (class_exists("okapi\\Settings") && (Settings::get('DEBUG_PREVENT_EMAILS'))) |
|
1146 | - { |
|
1150 | + if (class_exists("okapi\\Settings") && (Settings::get('DEBUG_PREVENT_EMAILS'))) { |
|
1147 | 1151 | # Sending emails was blocked on admin's demand. |
1148 | 1152 | # This is possible only on development environment. |
1149 | 1153 | return; |
1150 | 1154 | } |
1151 | - if (!is_array($email_addresses)) |
|
1152 | - $email_addresses = array($email_addresses); |
|
1155 | + if (!is_array($email_addresses)) { |
|
1156 | + $email_addresses = array($email_addresses); |
|
1157 | + } |
|
1153 | 1158 | $sender_email = class_exists("okapi\\Settings") ? Settings::get('FROM_FIELD') : 'root@localhost'; |
1154 | 1159 | mail(implode(", ", $email_addresses), $subject, $message, |
1155 | 1160 | "Content-Type: text/plain; charset=utf-8\n". |
@@ -1162,16 +1167,18 @@ discard block |
||
1162 | 1167 | public static function get_var_dir() |
1163 | 1168 | { |
1164 | 1169 | $dir = Settings::get('VAR_DIR'); |
1165 | - if ($dir != null) |
|
1166 | - return rtrim($dir, "/"); |
|
1170 | + if ($dir != null) { |
|
1171 | + return rtrim($dir, "/"); |
|
1172 | + } |
|
1167 | 1173 | throw new Exception("You need to set a valid VAR_DIR."); |
1168 | 1174 | } |
1169 | 1175 | |
1170 | 1176 | /** Returns something like "Opencaching.PL" or "Opencaching.DE". */ |
1171 | 1177 | public static function get_normalized_site_name($site_url = null) |
1172 | 1178 | { |
1173 | - if ($site_url == null) |
|
1174 | - $site_url = Settings::get('SITE_URL'); |
|
1179 | + if ($site_url == null) { |
|
1180 | + $site_url = Settings::get('SITE_URL'); |
|
1181 | + } |
|
1175 | 1182 | $matches = null; |
1176 | 1183 | if (preg_match("#^https?://(www.)?opencaching.([a-z.]+)/$#", $site_url, $matches)) { |
1177 | 1184 | return "Opencaching.".strtoupper($matches[2]); |
@@ -1320,11 +1327,13 @@ discard block |
||
1320 | 1327 | */ |
1321 | 1328 | public static function pick_best_language($langdict, $langprefs) |
1322 | 1329 | { |
1323 | - foreach ($langprefs as $pref) |
|
1324 | - if (isset($langdict[$pref])) |
|
1330 | + foreach ($langprefs as $pref) { |
|
1331 | + if (isset($langdict[$pref])) |
|
1325 | 1332 | return $langdict[$pref]; |
1326 | - foreach ($langdict as &$text_ref) |
|
1327 | - return $text_ref; |
|
1333 | + } |
|
1334 | + foreach ($langdict as &$text_ref) { |
|
1335 | + return $text_ref; |
|
1336 | + } |
|
1328 | 1337 | return ""; |
1329 | 1338 | } |
1330 | 1339 | |
@@ -1335,8 +1344,7 @@ discard block |
||
1335 | 1344 | { |
1336 | 1345 | $i = 0; |
1337 | 1346 | $groups = array(); |
1338 | - while ($i < count($array)) |
|
1339 | - { |
|
1347 | + while ($i < count($array)) { |
|
1340 | 1348 | $groups[] = array_slice($array, $i, $size); |
1341 | 1349 | $i += $size; |
1342 | 1350 | } |
@@ -1350,8 +1358,7 @@ discard block |
||
1350 | 1358 | public static function execute_prerequest_cronjobs() |
1351 | 1359 | { |
1352 | 1360 | $nearest_event = Okapi::get_var("cron_nearest_event"); |
1353 | - if ($nearest_event + 0 <= time()) |
|
1354 | - { |
|
1361 | + if ($nearest_event + 0 <= time()) { |
|
1355 | 1362 | require_once($GLOBALS['rootpath']."okapi/cronjobs.php"); |
1356 | 1363 | $nearest_event = CronJobController::run_jobs('pre-request'); |
1357 | 1364 | Okapi::set_var("cron_nearest_event", $nearest_event); |
@@ -1365,8 +1372,7 @@ discard block |
||
1365 | 1372 | public static function execute_cron5_cronjobs() |
1366 | 1373 | { |
1367 | 1374 | $nearest_event = Okapi::get_var("cron_nearest_event"); |
1368 | - if ($nearest_event + 0 <= time()) |
|
1369 | - { |
|
1375 | + if ($nearest_event + 0 <= time()) { |
|
1370 | 1376 | set_time_limit(0); |
1371 | 1377 | ignore_user_abort(true); |
1372 | 1378 | require_once($GLOBALS['rootpath']."okapi/cronjobs.php"); |
@@ -1384,8 +1390,7 @@ discard block |
||
1384 | 1390 | # This way, we don't need to call the actual locale-switching code most |
1385 | 1391 | # of the times. |
1386 | 1392 | |
1387 | - if ($gettext_last_used_langprefs != $langprefs) |
|
1388 | - { |
|
1393 | + if ($gettext_last_used_langprefs != $langprefs) { |
|
1389 | 1394 | $gettext_last_set_locale = call_user_func(Settings::get("GETTEXT_INIT"), $langprefs); |
1390 | 1395 | $gettext_last_used_langprefs = $langprefs; |
1391 | 1396 | textdomain(Settings::get("GETTEXT_DOMAIN")); |
@@ -1405,12 +1410,12 @@ discard block |
||
1405 | 1410 | { |
1406 | 1411 | # Put the langprefs on the stack. |
1407 | 1412 | |
1408 | - if ($langprefs == null) |
|
1409 | - $langprefs = array(Settings::get('SITELANG')); |
|
1413 | + if ($langprefs == null) { |
|
1414 | + $langprefs = array(Settings::get('SITELANG')); |
|
1415 | + } |
|
1410 | 1416 | self::$gettext_langprefs_stack[] = $langprefs; |
1411 | 1417 | |
1412 | - if (count(self::$gettext_langprefs_stack) == 1) |
|
1413 | - { |
|
1418 | + if (count(self::$gettext_langprefs_stack) == 1) { |
|
1414 | 1419 | # This is the first time gettext_domain_init is called. In order to |
1415 | 1420 | # properly reinitialize the original settings after gettext_domain_restore |
1416 | 1421 | # is called for the last time, we need to save current textdomain (which |
@@ -1434,13 +1439,10 @@ discard block |
||
1434 | 1439 | array_pop(self::$gettext_langprefs_stack); |
1435 | 1440 | |
1436 | 1441 | $size = count(self::$gettext_langprefs_stack); |
1437 | - if ($size > 0) |
|
1438 | - { |
|
1442 | + if ($size > 0) { |
|
1439 | 1443 | $langprefs = self::$gettext_langprefs_stack[$size - 1]; |
1440 | 1444 | self::gettext_set_lang($langprefs); |
1441 | - } |
|
1442 | - else |
|
1443 | - { |
|
1445 | + } else { |
|
1444 | 1446 | # The stack is empty. This means we're going out of OKAPI code and |
1445 | 1447 | # we want the original textdomain reestablished. |
1446 | 1448 | |
@@ -1455,20 +1457,25 @@ discard block |
||
1455 | 1457 | public static function init_internals($allow_cronjobs = true) |
1456 | 1458 | { |
1457 | 1459 | static $init_made = false; |
1458 | - if ($init_made) |
|
1459 | - return; |
|
1460 | + if ($init_made) { |
|
1461 | + return; |
|
1462 | + } |
|
1460 | 1463 | ini_set('memory_limit', '256M'); |
1461 | 1464 | # The memory limit is - among other - crucial for the maximum size |
1462 | 1465 | # of processable images; see services/logs/images/add.php: max_pixels() |
1463 | 1466 | Db::connect(); |
1464 | - if (Settings::get('TIMEZONE') !== null) |
|
1465 | - date_default_timezone_set(Settings::get('TIMEZONE')); |
|
1466 | - if (!self::$data_store) |
|
1467 | - self::$data_store = new OkapiDataStore(); |
|
1468 | - if (!self::$server) |
|
1469 | - self::$server = new OkapiOAuthServer(self::$data_store); |
|
1470 | - if ($allow_cronjobs) |
|
1471 | - self::execute_prerequest_cronjobs(); |
|
1467 | + if (Settings::get('TIMEZONE') !== null) { |
|
1468 | + date_default_timezone_set(Settings::get('TIMEZONE')); |
|
1469 | + } |
|
1470 | + if (!self::$data_store) { |
|
1471 | + self::$data_store = new OkapiDataStore(); |
|
1472 | + } |
|
1473 | + if (!self::$server) { |
|
1474 | + self::$server = new OkapiOAuthServer(self::$data_store); |
|
1475 | + } |
|
1476 | + if ($allow_cronjobs) { |
|
1477 | + self::execute_prerequest_cronjobs(); |
|
1478 | + } |
|
1472 | 1479 | $init_made = true; |
1473 | 1480 | } |
1474 | 1481 | |
@@ -1479,14 +1486,14 @@ discard block |
||
1479 | 1486 | */ |
1480 | 1487 | public static function generate_key($length, $user_friendly = false) |
1481 | 1488 | { |
1482 | - if ($user_friendly) |
|
1483 | - $chars = "0123456789"; |
|
1484 | - else |
|
1485 | - $chars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; |
|
1489 | + if ($user_friendly) { |
|
1490 | + $chars = "0123456789"; |
|
1491 | + } else { |
|
1492 | + $chars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; |
|
1493 | + } |
|
1486 | 1494 | $max = strlen($chars); |
1487 | 1495 | $key = ""; |
1488 | - for ($i=0; $i<$length; $i++) |
|
1489 | - { |
|
1496 | + for ($i=0; $i<$length; $i++) { |
|
1490 | 1497 | $key .= $chars[rand(0, $max-1)]; |
1491 | 1498 | } |
1492 | 1499 | return $key; |
@@ -1504,10 +1511,11 @@ discard block |
||
1504 | 1511 | $appname, $appurl, $email); |
1505 | 1512 | $sample_cache = OkapiServiceRunner::call("services/caches/search/all", |
1506 | 1513 | new OkapiInternalRequest($consumer, null, array('limit', 1))); |
1507 | - if (count($sample_cache['results']) > 0) |
|
1508 | - $sample_cache_code = $sample_cache['results'][0]; |
|
1509 | - else |
|
1510 | - $sample_cache_code = "CACHECODE"; |
|
1514 | + if (count($sample_cache['results']) > 0) { |
|
1515 | + $sample_cache_code = $sample_cache['results'][0]; |
|
1516 | + } else { |
|
1517 | + $sample_cache_code = "CACHECODE"; |
|
1518 | + } |
|
1511 | 1519 | |
1512 | 1520 | # Message for the Consumer. |
1513 | 1521 | ob_start(); |
@@ -1558,7 +1566,9 @@ discard block |
||
1558 | 1566 | # to floating point precision limits. See issue #351 for details. |
1559 | 1567 | # |
1560 | 1568 | $d = acos(min(1, cos($x1) * cos($x2) + sin($x1) * sin($x2) * cos(($lon1-$lon2) * 3.14159 / 180))) * 6371000; |
1561 | - if ($d < 0) $d = 0; |
|
1569 | + if ($d < 0) { |
|
1570 | + $d = 0; |
|
1571 | + } |
|
1562 | 1572 | return $d; |
1563 | 1573 | } |
1564 | 1574 | |
@@ -1581,10 +1591,15 @@ discard block |
||
1581 | 1591 | /** Return bearing (float 0..360) from geopoint 1 to 2. */ |
1582 | 1592 | public static function get_bearing($lat1, $lon1, $lat2, $lon2) |
1583 | 1593 | { |
1584 | - if ($lat1 == $lat2 && $lon1 == $lon2) |
|
1585 | - return null; |
|
1586 | - if ($lat1 == $lat2) $lat1 += 0.0000166; |
|
1587 | - if ($lon1 == $lon2) $lon1 += 0.0000166; |
|
1594 | + if ($lat1 == $lat2 && $lon1 == $lon2) { |
|
1595 | + return null; |
|
1596 | + } |
|
1597 | + if ($lat1 == $lat2) { |
|
1598 | + $lat1 += 0.0000166; |
|
1599 | + } |
|
1600 | + if ($lon1 == $lon2) { |
|
1601 | + $lon1 += 0.0000166; |
|
1602 | + } |
|
1588 | 1603 | |
1589 | 1604 | $rad_lat1 = $lat1 / 180.0 * 3.14159; |
1590 | 1605 | $rad_lon1 = $lon1 / 180.0 * 3.14159; |
@@ -1595,7 +1610,9 @@ discard block |
||
1595 | 1610 | $bearing = atan2(sin($delta_lon) * cos($rad_lat2), |
1596 | 1611 | cos($rad_lat1) * sin($rad_lat2) - sin($rad_lat1) * cos($rad_lat2) * cos($delta_lon)); |
1597 | 1612 | $bearing = 180.0 * $bearing / 3.14159; |
1598 | - if ( $bearing < 0.0 ) $bearing = $bearing + 360.0; |
|
1613 | + if ( $bearing < 0.0 ) { |
|
1614 | + $bearing = $bearing + 360.0; |
|
1615 | + } |
|
1599 | 1616 | |
1600 | 1617 | return $bearing; |
1601 | 1618 | } |
@@ -1604,7 +1621,9 @@ discard block |
||
1604 | 1621 | public static function bearing_as_two_letters($b) |
1605 | 1622 | { |
1606 | 1623 | static $names = array('N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'); |
1607 | - if ($b === null) return 'n/a'; |
|
1624 | + if ($b === null) { |
|
1625 | + return 'n/a'; |
|
1626 | + } |
|
1608 | 1627 | return $names[round(($b / 360.0) * 8.0) % 8]; |
1609 | 1628 | } |
1610 | 1629 | |
@@ -1613,7 +1632,9 @@ discard block |
||
1613 | 1632 | { |
1614 | 1633 | static $names = array('N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', |
1615 | 1634 | 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW'); |
1616 | - if ($b === null) return 'n/a'; |
|
1635 | + if ($b === null) { |
|
1636 | + return 'n/a'; |
|
1637 | + } |
|
1617 | 1638 | return $names[round(($b / 360.0) * 16.0) % 16]; |
1618 | 1639 | } |
1619 | 1640 | |
@@ -1634,54 +1655,50 @@ discard block |
||
1634 | 1655 | */ |
1635 | 1656 | public static function formatted_response(OkapiRequest $request, &$object) |
1636 | 1657 | { |
1637 | - if ($request instanceof OkapiInternalRequest && (!$request->i_want_OkapiResponse)) |
|
1638 | - { |
|
1658 | + if ($request instanceof OkapiInternalRequest && (!$request->i_want_OkapiResponse)) { |
|
1639 | 1659 | # If you call a method internally, then you probably expect to get |
1640 | 1660 | # the actual object instead of it's formatted representation. |
1641 | 1661 | return $object; |
1642 | 1662 | } |
1643 | 1663 | $format = $request->get_parameter('format'); |
1644 | - if ($format == null) $format = 'json'; |
|
1645 | - if (!in_array($format, array('json', 'jsonp', 'xmlmap', 'xmlmap2'))) |
|
1646 | - throw new InvalidParam('format', "'$format'"); |
|
1664 | + if ($format == null) { |
|
1665 | + $format = 'json'; |
|
1666 | + } |
|
1667 | + if (!in_array($format, array('json', 'jsonp', 'xmlmap', 'xmlmap2'))) { |
|
1668 | + throw new InvalidParam('format', "'$format'"); |
|
1669 | + } |
|
1647 | 1670 | $callback = $request->get_parameter('callback'); |
1648 | - if ($callback && $format != 'jsonp') |
|
1649 | - throw new BadRequest("The 'callback' parameter is reserved to be used with the JSONP output format."); |
|
1650 | - if ($format == 'json') |
|
1651 | - { |
|
1671 | + if ($callback && $format != 'jsonp') { |
|
1672 | + throw new BadRequest("The 'callback' parameter is reserved to be used with the JSONP output format."); |
|
1673 | + } |
|
1674 | + if ($format == 'json') { |
|
1652 | 1675 | $response = new OkapiHttpResponse(); |
1653 | 1676 | $response->content_type = "application/json; charset=utf-8"; |
1654 | 1677 | $response->body = json_encode($object); |
1655 | 1678 | return $response; |
1656 | - } |
|
1657 | - elseif ($format == 'jsonp') |
|
1658 | - { |
|
1659 | - if (!$callback) |
|
1660 | - throw new BadRequest("'callback' parameter is required for JSONP calls"); |
|
1661 | - if (!preg_match("/^[a-zA-Z_][a-zA-Z0-9_]*$/", $callback)) |
|
1662 | - throw new InvalidParam('callback', "'$callback' doesn't seem to be a valid JavaScript function name (should match /^[a-zA-Z_][a-zA-Z0-9_]*\$/)."); |
|
1679 | + } elseif ($format == 'jsonp') { |
|
1680 | + if (!$callback) { |
|
1681 | + throw new BadRequest("'callback' parameter is required for JSONP calls"); |
|
1682 | + } |
|
1683 | + if (!preg_match("/^[a-zA-Z_][a-zA-Z0-9_]*$/", $callback)) { |
|
1684 | + throw new InvalidParam('callback', "'$callback' doesn't seem to be a valid JavaScript function name (should match /^[a-zA-Z_][a-zA-Z0-9_]*\$/)."); |
|
1685 | + } |
|
1663 | 1686 | $response = new OkapiHttpResponse(); |
1664 | 1687 | $response->content_type = "application/javascript; charset=utf-8"; |
1665 | 1688 | $response->body = $callback."(".json_encode($object).");"; |
1666 | 1689 | return $response; |
1667 | - } |
|
1668 | - elseif ($format == 'xmlmap') |
|
1669 | - { |
|
1690 | + } elseif ($format == 'xmlmap') { |
|
1670 | 1691 | # Deprecated (see issue 128). Keeping this for backward-compatibility. |
1671 | 1692 | $response = new OkapiHttpResponse(); |
1672 | 1693 | $response->content_type = "text/xml; charset=utf-8"; |
1673 | 1694 | $response->body = self::xmlmap_dumps($object); |
1674 | 1695 | return $response; |
1675 | - } |
|
1676 | - elseif ($format == 'xmlmap2') |
|
1677 | - { |
|
1696 | + } elseif ($format == 'xmlmap2') { |
|
1678 | 1697 | $response = new OkapiHttpResponse(); |
1679 | 1698 | $response->content_type = "text/xml; charset=utf-8"; |
1680 | 1699 | $response->body = self::xmlmap2_dumps($object); |
1681 | 1700 | return $response; |
1682 | - } |
|
1683 | - else |
|
1684 | - { |
|
1701 | + } else { |
|
1685 | 1702 | # Should not happen (as we do a proper check above). |
1686 | 1703 | throw new Exception(); |
1687 | 1704 | } |
@@ -1689,58 +1706,40 @@ discard block |
||
1689 | 1706 | |
1690 | 1707 | private static function _xmlmap_add(&$chunks, &$obj) |
1691 | 1708 | { |
1692 | - if (is_string($obj)) |
|
1693 | - { |
|
1709 | + if (is_string($obj)) { |
|
1694 | 1710 | $chunks[] = "<string>"; |
1695 | 1711 | $chunks[] = self::xmlescape($obj); |
1696 | 1712 | $chunks[] = "</string>"; |
1697 | - } |
|
1698 | - elseif (is_int($obj)) |
|
1699 | - { |
|
1713 | + } elseif (is_int($obj)) { |
|
1700 | 1714 | $chunks[] = "<int>$obj</int>"; |
1701 | - } |
|
1702 | - elseif (is_float($obj)) |
|
1703 | - { |
|
1715 | + } elseif (is_float($obj)) { |
|
1704 | 1716 | $chunks[] = "<float>$obj</float>"; |
1705 | - } |
|
1706 | - elseif (is_bool($obj)) |
|
1707 | - { |
|
1717 | + } elseif (is_bool($obj)) { |
|
1708 | 1718 | $chunks[] = $obj ? "<bool>true</bool>" : "<bool>false</bool>"; |
1709 | - } |
|
1710 | - elseif (is_null($obj)) |
|
1711 | - { |
|
1719 | + } elseif (is_null($obj)) { |
|
1712 | 1720 | $chunks[] = "<null/>"; |
1713 | - } |
|
1714 | - elseif (is_array($obj)) |
|
1715 | - { |
|
1721 | + } elseif (is_array($obj)) { |
|
1716 | 1722 | # Have to check if this is associative or not! Shit. I hate PHP. |
1717 | - if (array_keys($obj) === range(0, count($obj) - 1)) |
|
1718 | - { |
|
1723 | + if (array_keys($obj) === range(0, count($obj) - 1)) { |
|
1719 | 1724 | # Not assoc. |
1720 | 1725 | $chunks[] = "<list>"; |
1721 | - foreach ($obj as &$item_ref) |
|
1722 | - { |
|
1726 | + foreach ($obj as &$item_ref) { |
|
1723 | 1727 | $chunks[] = "<item>"; |
1724 | 1728 | self::_xmlmap_add($chunks, $item_ref); |
1725 | 1729 | $chunks[] = "</item>"; |
1726 | 1730 | } |
1727 | 1731 | $chunks[] = "</list>"; |
1728 | - } |
|
1729 | - else |
|
1730 | - { |
|
1732 | + } else { |
|
1731 | 1733 | # Assoc. |
1732 | 1734 | $chunks[] = "<dict>"; |
1733 | - foreach ($obj as $key => &$item_ref) |
|
1734 | - { |
|
1735 | + foreach ($obj as $key => &$item_ref) { |
|
1735 | 1736 | $chunks[] = "<item key=\"".self::xmlescape($key)."\">"; |
1736 | 1737 | self::_xmlmap_add($chunks, $item_ref); |
1737 | 1738 | $chunks[] = "</item>"; |
1738 | 1739 | } |
1739 | 1740 | $chunks[] = "</dict>"; |
1740 | 1741 | } |
1741 | - } |
|
1742 | - else |
|
1743 | - { |
|
1742 | + } else { |
|
1744 | 1743 | # That's a bug. |
1745 | 1744 | throw new Exception("Cannot encode as xmlmap: " + print_r($obj, true)); |
1746 | 1745 | } |
@@ -1749,54 +1748,36 @@ discard block |
||
1749 | 1748 | private static function _xmlmap2_add(&$chunks, &$obj, $key) |
1750 | 1749 | { |
1751 | 1750 | $attrs = ($key !== null) ? " key=\"".self::xmlescape($key)."\"" : ""; |
1752 | - if (is_string($obj)) |
|
1753 | - { |
|
1751 | + if (is_string($obj)) { |
|
1754 | 1752 | $chunks[] = "<string$attrs>"; |
1755 | 1753 | $chunks[] = self::xmlescape($obj); |
1756 | 1754 | $chunks[] = "</string>"; |
1757 | - } |
|
1758 | - elseif (is_int($obj)) |
|
1759 | - { |
|
1755 | + } elseif (is_int($obj)) { |
|
1760 | 1756 | $chunks[] = "<number$attrs>$obj</number>"; |
1761 | - } |
|
1762 | - elseif (is_float($obj)) |
|
1763 | - { |
|
1757 | + } elseif (is_float($obj)) { |
|
1764 | 1758 | $chunks[] = "<number$attrs>$obj</number>"; |
1765 | - } |
|
1766 | - elseif (is_bool($obj)) |
|
1767 | - { |
|
1759 | + } elseif (is_bool($obj)) { |
|
1768 | 1760 | $chunks[] = $obj ? "<boolean$attrs>true</boolean>" : "<boolean$attrs>false</boolean>"; |
1769 | - } |
|
1770 | - elseif (is_null($obj)) |
|
1771 | - { |
|
1761 | + } elseif (is_null($obj)) { |
|
1772 | 1762 | $chunks[] = "<null$attrs/>"; |
1773 | - } |
|
1774 | - elseif (is_array($obj) || ($obj instanceof ArrayObject)) |
|
1775 | - { |
|
1763 | + } elseif (is_array($obj) || ($obj instanceof ArrayObject)) { |
|
1776 | 1764 | # Have to check if this is associative or not! Shit. I hate PHP. |
1777 | - if (is_array($obj) && (array_keys($obj) === range(0, count($obj) - 1))) |
|
1778 | - { |
|
1765 | + if (is_array($obj) && (array_keys($obj) === range(0, count($obj) - 1))) { |
|
1779 | 1766 | # Not assoc. |
1780 | 1767 | $chunks[] = "<array$attrs>"; |
1781 | - foreach ($obj as &$item_ref) |
|
1782 | - { |
|
1768 | + foreach ($obj as &$item_ref) { |
|
1783 | 1769 | self::_xmlmap2_add($chunks, $item_ref, null); |
1784 | 1770 | } |
1785 | 1771 | $chunks[] = "</array>"; |
1786 | - } |
|
1787 | - else |
|
1788 | - { |
|
1772 | + } else { |
|
1789 | 1773 | # Assoc. |
1790 | 1774 | $chunks[] = "<object$attrs>"; |
1791 | - foreach ($obj as $key => &$item_ref) |
|
1792 | - { |
|
1775 | + foreach ($obj as $key => &$item_ref) { |
|
1793 | 1776 | self::_xmlmap2_add($chunks, $item_ref, $key); |
1794 | 1777 | } |
1795 | 1778 | $chunks[] = "</object>"; |
1796 | 1779 | } |
1797 | - } |
|
1798 | - else |
|
1799 | - { |
|
1780 | + } else { |
|
1800 | 1781 | # That's a bug. |
1801 | 1782 | throw new Exception("Cannot encode as xmlmap2: " . print_r($obj, true)); |
1802 | 1783 | } |
@@ -1851,8 +1832,9 @@ discard block |
||
1851 | 1832 | public static function cache_type_name2id($name) |
1852 | 1833 | { |
1853 | 1834 | $ref = &self::$cache_types[Settings::get('OC_BRANCH')]; |
1854 | - if (isset($ref[$name])) |
|
1855 | - return $ref[$name]; |
|
1835 | + if (isset($ref[$name])) { |
|
1836 | + return $ref[$name]; |
|
1837 | + } |
|
1856 | 1838 | throw new Exception("Method cache_type_name2id called with unsupported cache ". |
1857 | 1839 | "type name '$name'. You should not allow users to submit caches ". |
1858 | 1840 | "of non-primary type."); |
@@ -1862,14 +1844,15 @@ discard block |
||
1862 | 1844 | public static function cache_type_id2name($id) |
1863 | 1845 | { |
1864 | 1846 | static $reversed = null; |
1865 | - if ($reversed == null) |
|
1866 | - { |
|
1847 | + if ($reversed == null) { |
|
1867 | 1848 | $reversed = array(); |
1868 | - foreach (self::$cache_types[Settings::get('OC_BRANCH')] as $key => $value) |
|
1869 | - $reversed[$value] = $key; |
|
1849 | + foreach (self::$cache_types[Settings::get('OC_BRANCH')] as $key => $value) { |
|
1850 | + $reversed[$value] = $key; |
|
1851 | + } |
|
1852 | + } |
|
1853 | + if (isset($reversed[$id])) { |
|
1854 | + return $reversed[$id]; |
|
1870 | 1855 | } |
1871 | - if (isset($reversed[$id])) |
|
1872 | - return $reversed[$id]; |
|
1873 | 1856 | return "Other"; |
1874 | 1857 | } |
1875 | 1858 | |
@@ -1880,8 +1863,9 @@ discard block |
||
1880 | 1863 | /** E.g. 'Available' => 1. For unknown names throws an Exception. */ |
1881 | 1864 | public static function cache_status_name2id($name) |
1882 | 1865 | { |
1883 | - if (isset(self::$cache_statuses[$name])) |
|
1884 | - return self::$cache_statuses[$name]; |
|
1866 | + if (isset(self::$cache_statuses[$name])) { |
|
1867 | + return self::$cache_statuses[$name]; |
|
1868 | + } |
|
1885 | 1869 | throw new Exception("Method cache_status_name2id called with invalid name '$name'."); |
1886 | 1870 | } |
1887 | 1871 | |
@@ -1889,14 +1873,15 @@ discard block |
||
1889 | 1873 | public static function cache_status_id2name($id) |
1890 | 1874 | { |
1891 | 1875 | static $reversed = null; |
1892 | - if ($reversed == null) |
|
1893 | - { |
|
1876 | + if ($reversed == null) { |
|
1894 | 1877 | $reversed = array(); |
1895 | - foreach (self::$cache_statuses as $key => $value) |
|
1896 | - $reversed[$value] = $key; |
|
1878 | + foreach (self::$cache_statuses as $key => $value) { |
|
1879 | + $reversed[$value] = $key; |
|
1880 | + } |
|
1881 | + } |
|
1882 | + if (isset($reversed[$id])) { |
|
1883 | + return $reversed[$id]; |
|
1897 | 1884 | } |
1898 | - if (isset($reversed[$id])) |
|
1899 | - return $reversed[$id]; |
|
1900 | 1885 | return 'Archived'; |
1901 | 1886 | } |
1902 | 1887 | |
@@ -1914,8 +1899,9 @@ discard block |
||
1914 | 1899 | /** E.g. 'micro' => 2. For unknown names throw an Exception. */ |
1915 | 1900 | public static function cache_size2_to_sizeid($size2) |
1916 | 1901 | { |
1917 | - if (isset(self::$cache_sizes[$size2])) |
|
1918 | - return self::$cache_sizes[$size2]; |
|
1902 | + if (isset(self::$cache_sizes[$size2])) { |
|
1903 | + return self::$cache_sizes[$size2]; |
|
1904 | + } |
|
1919 | 1905 | throw new Exception("Method cache_size2_to_sizeid called with invalid size2 '$size2'."); |
1920 | 1906 | } |
1921 | 1907 | |
@@ -1923,14 +1909,15 @@ discard block |
||
1923 | 1909 | public static function cache_sizeid_to_size2($id) |
1924 | 1910 | { |
1925 | 1911 | static $reversed = null; |
1926 | - if ($reversed == null) |
|
1927 | - { |
|
1912 | + if ($reversed == null) { |
|
1928 | 1913 | $reversed = array(); |
1929 | - foreach (self::$cache_sizes as $key => $value) |
|
1930 | - $reversed[$value] = $key; |
|
1914 | + foreach (self::$cache_sizes as $key => $value) { |
|
1915 | + $reversed[$value] = $key; |
|
1916 | + } |
|
1917 | + } |
|
1918 | + if (isset($reversed[$id])) { |
|
1919 | + return $reversed[$id]; |
|
1931 | 1920 | } |
1932 | - if (isset($reversed[$id])) |
|
1933 | - return $reversed[$id]; |
|
1934 | 1921 | return "other"; |
1935 | 1922 | } |
1936 | 1923 | |
@@ -1953,8 +1940,9 @@ discard block |
||
1953 | 1940 | */ |
1954 | 1941 | public static function cache_size2_to_oxsize($size2) |
1955 | 1942 | { |
1956 | - if (array_key_exists($size2, self::$cache_OX_sizes)) |
|
1957 | - return self::$cache_OX_sizes[$size2]; |
|
1943 | + if (array_key_exists($size2, self::$cache_OX_sizes)) { |
|
1944 | + return self::$cache_OX_sizes[$size2]; |
|
1945 | + } |
|
1958 | 1946 | throw new Exception("Method cache_size2_to_oxsize called with invalid size2 '$size2'."); |
1959 | 1947 | } |
1960 | 1948 | |
@@ -1963,12 +1951,24 @@ discard block |
||
1963 | 1951 | */ |
1964 | 1952 | public static function logtypename2id($name) |
1965 | 1953 | { |
1966 | - if ($name == 'Found it') return 1; |
|
1967 | - if ($name == "Didn't find it") return 2; |
|
1968 | - if ($name == 'Comment') return 3; |
|
1969 | - if ($name == 'Attended') return 7; |
|
1970 | - if ($name == 'Will attend') return 8; |
|
1971 | - if (($name == 'Needs maintenance') && (Settings::get('OC_BRANCH') == 'oc.pl')) return 5; |
|
1954 | + if ($name == 'Found it') { |
|
1955 | + return 1; |
|
1956 | + } |
|
1957 | + if ($name == "Didn't find it") { |
|
1958 | + return 2; |
|
1959 | + } |
|
1960 | + if ($name == 'Comment') { |
|
1961 | + return 3; |
|
1962 | + } |
|
1963 | + if ($name == 'Attended') { |
|
1964 | + return 7; |
|
1965 | + } |
|
1966 | + if ($name == 'Will attend') { |
|
1967 | + return 8; |
|
1968 | + } |
|
1969 | + if (($name == 'Needs maintenance') && (Settings::get('OC_BRANCH') == 'oc.pl')) { |
|
1970 | + return 5; |
|
1971 | + } |
|
1972 | 1972 | throw new Exception("logtype2id called with invalid log type argument: $name"); |
1973 | 1973 | } |
1974 | 1974 | |
@@ -1981,21 +1981,47 @@ discard block |
||
1981 | 1981 | # These names are officially documented and may never change! |
1982 | 1982 | |
1983 | 1983 | # Primary. |
1984 | - if ($id == 1) return "Found it"; |
|
1985 | - if ($id == 2) return "Didn't find it"; |
|
1986 | - if ($id == 3) return "Comment"; |
|
1987 | - if ($id == 7) return "Attended"; |
|
1988 | - if ($id == 8) return "Will attend"; |
|
1984 | + if ($id == 1) { |
|
1985 | + return "Found it"; |
|
1986 | + } |
|
1987 | + if ($id == 2) { |
|
1988 | + return "Didn't find it"; |
|
1989 | + } |
|
1990 | + if ($id == 3) { |
|
1991 | + return "Comment"; |
|
1992 | + } |
|
1993 | + if ($id == 7) { |
|
1994 | + return "Attended"; |
|
1995 | + } |
|
1996 | + if ($id == 8) { |
|
1997 | + return "Will attend"; |
|
1998 | + } |
|
1989 | 1999 | |
1990 | 2000 | # Other. |
1991 | - if ($id == 4) return "Moved"; |
|
1992 | - if ($id == 5) return "Needs maintenance"; |
|
1993 | - if ($id == 6) return "Maintenance performed"; |
|
1994 | - if ($id == 9) return "Archived"; |
|
1995 | - if ($id == 10) return "Ready to search"; |
|
1996 | - if ($id == 11) return "Temporarily unavailable"; |
|
1997 | - if ($id == 12) return "OC Team comment"; |
|
1998 | - if ($id == 13 || $id == 14) return "Locked"; |
|
2001 | + if ($id == 4) { |
|
2002 | + return "Moved"; |
|
2003 | + } |
|
2004 | + if ($id == 5) { |
|
2005 | + return "Needs maintenance"; |
|
2006 | + } |
|
2007 | + if ($id == 6) { |
|
2008 | + return "Maintenance performed"; |
|
2009 | + } |
|
2010 | + if ($id == 9) { |
|
2011 | + return "Archived"; |
|
2012 | + } |
|
2013 | + if ($id == 10) { |
|
2014 | + return "Ready to search"; |
|
2015 | + } |
|
2016 | + if ($id == 11) { |
|
2017 | + return "Temporarily unavailable"; |
|
2018 | + } |
|
2019 | + if ($id == 12) { |
|
2020 | + return "OC Team comment"; |
|
2021 | + } |
|
2022 | + if ($id == 13 || $id == 14) { |
|
2023 | + return "Locked"; |
|
2024 | + } |
|
1999 | 2025 | |
2000 | 2026 | # Important: This set is not closed. Other types may be introduced |
2001 | 2027 | # in the future. This has to be documented in the public method |
@@ -2047,8 +2073,9 @@ discard block |
||
2047 | 2073 | function php_ini_get_bytes($variable) |
2048 | 2074 | { |
2049 | 2075 | $value = trim(ini_get($variable)); |
2050 | - if (!preg_match("/^[0-9]+[KM]?$/", $value)) |
|
2051 | - throw new Exception("Unexpected PHP setting: ".$variable. " = ".$value); |
|
2076 | + if (!preg_match("/^[0-9]+[KM]?$/", $value)) { |
|
2077 | + throw new Exception("Unexpected PHP setting: ".$variable. " = ".$value); |
|
2078 | + } |
|
2052 | 2079 | $value = str_replace('K', '*1024', $value); |
2053 | 2080 | $value = str_replace('M', '*1024*1024', $value); |
2054 | 2081 | $value = eval('return '.$value.';'); |
@@ -2080,8 +2107,7 @@ discard block |
||
2080 | 2107 | */ |
2081 | 2108 | public static function set($key, $value, $timeout) |
2082 | 2109 | { |
2083 | - if ($timeout == null) |
|
2084 | - { |
|
2110 | + if ($timeout == null) { |
|
2085 | 2111 | # The current cache implementation is ALWAYS persistent, so we will |
2086 | 2112 | # just replace it with a big value. |
2087 | 2113 | $timeout = 100*365*86400; |
@@ -2116,17 +2142,16 @@ discard block |
||
2116 | 2142 | /** Do 'set' on many keys at once. */ |
2117 | 2143 | public static function set_many($dict, $timeout) |
2118 | 2144 | { |
2119 | - if (count($dict) == 0) |
|
2120 | - return; |
|
2121 | - if ($timeout == null) |
|
2122 | - { |
|
2145 | + if (count($dict) == 0) { |
|
2146 | + return; |
|
2147 | + } |
|
2148 | + if ($timeout == null) { |
|
2123 | 2149 | # The current cache implementation is ALWAYS persistent, so we will |
2124 | 2150 | # just replace it with a big value. |
2125 | 2151 | $timeout = 100*365*86400; |
2126 | 2152 | } |
2127 | 2153 | $entries_escaped = array(); |
2128 | - foreach ($dict as $key => $value) |
|
2129 | - { |
|
2154 | + foreach ($dict as $key => $value) { |
|
2130 | 2155 | $entries_escaped[] = "( |
2131 | 2156 | '".Db::escape_string($key)."', |
2132 | 2157 | '".Db::escape_string(gzdeflate(serialize($value)))."', |
@@ -2153,15 +2178,18 @@ discard block |
||
2153 | 2178 | and expires > now() |
2154 | 2179 | "); |
2155 | 2180 | list($blob, $score) = Db::fetch_row($rs); |
2156 | - if (!$blob) |
|
2157 | - return null; |
|
2158 | - if ($score != null) # Only non-null entries are scored. |
|
2181 | + if (!$blob) { |
|
2182 | + return null; |
|
2183 | + } |
|
2184 | + if ($score != null) { |
|
2185 | + # Only non-null entries are scored. |
|
2159 | 2186 | { |
2160 | 2187 | Db::execute(" |
2161 | 2188 | insert into okapi_cache_reads (`cache_key`) |
2162 | 2189 | values ('".Db::escape_string($key)."') |
2163 | 2190 | "); |
2164 | 2191 | } |
2192 | + } |
|
2165 | 2193 | return unserialize(gzinflate($blob)); |
2166 | 2194 | } |
2167 | 2195 | |
@@ -2176,14 +2204,10 @@ discard block |
||
2176 | 2204 | `key` in ('".implode("','", array_map('\okapi\Db::escape_string', $keys))."') |
2177 | 2205 | and expires > now() |
2178 | 2206 | "); |
2179 | - while ($row = Db::fetch_assoc($rs)) |
|
2180 | - { |
|
2181 | - try |
|
2182 | - { |
|
2207 | + while ($row = Db::fetch_assoc($rs)) { |
|
2208 | + try { |
|
2183 | 2209 | $dict[$row['key']] = unserialize(gzinflate($row['value'])); |
2184 | - } |
|
2185 | - catch (ErrorException $e) |
|
2186 | - { |
|
2210 | + } catch (ErrorException $e) { |
|
2187 | 2211 | unset($dict[$row['key']]); |
2188 | 2212 | Okapi::mail_admins("Debug: Unserialize error", |
2189 | 2213 | "Could not unserialize key '".$row['key']."' from Cache.\n". |
@@ -2192,10 +2216,11 @@ discard block |
||
2192 | 2216 | "Length of data, compressed: ".strlen($row['value'])); |
2193 | 2217 | } |
2194 | 2218 | } |
2195 | - if (count($dict) < count($keys)) |
|
2196 | - foreach ($keys as $key) |
|
2219 | + if (count($dict) < count($keys)) { |
|
2220 | + foreach ($keys as $key) |
|
2197 | 2221 | if (!isset($dict[$key])) |
2198 | 2222 | $dict[$key] = null; |
2223 | + } |
|
2199 | 2224 | return $dict; |
2200 | 2225 | } |
2201 | 2226 | |
@@ -2210,8 +2235,9 @@ discard block |
||
2210 | 2235 | /** Do 'delete' on many keys at once. */ |
2211 | 2236 | public static function delete_many($keys) |
2212 | 2237 | { |
2213 | - if (count($keys) == 0) |
|
2214 | - return; |
|
2238 | + if (count($keys) == 0) { |
|
2239 | + return; |
|
2240 | + } |
|
2215 | 2241 | Db::execute(" |
2216 | 2242 | delete from okapi_cache |
2217 | 2243 | where `key` in ('".implode("','", array_map('\okapi\Db::escape_string', $keys))."') |
@@ -2229,8 +2255,9 @@ discard block |
||
2229 | 2255 | public static function get_file_path($key) |
2230 | 2256 | { |
2231 | 2257 | $filename = Okapi::get_var_dir()."/okapi_filecache_".md5($key); |
2232 | - if (!file_exists($filename)) |
|
2233 | - return null; |
|
2258 | + if (!file_exists($filename)) { |
|
2259 | + return null; |
|
2260 | + } |
|
2234 | 2261 | return $filename; |
2235 | 2262 | } |
2236 | 2263 | |
@@ -2316,17 +2343,19 @@ discard block |
||
2316 | 2343 | $this->consumer = $consumer; |
2317 | 2344 | $this->token = $token; |
2318 | 2345 | $this->parameters = array(); |
2319 | - foreach ($parameters as $key => $value) |
|
2320 | - if ($value !== null) |
|
2346 | + foreach ($parameters as $key => $value) { |
|
2347 | + if ($value !== null) |
|
2321 | 2348 | $this->parameters[$key] = $value; |
2349 | + } |
|
2322 | 2350 | } |
2323 | 2351 | |
2324 | 2352 | public function get_parameter($name) |
2325 | 2353 | { |
2326 | - if (isset($this->parameters[$name])) |
|
2327 | - return $this->parameters[$name]; |
|
2328 | - else |
|
2329 | - return null; |
|
2354 | + if (isset($this->parameters[$name])) { |
|
2355 | + return $this->parameters[$name]; |
|
2356 | + } else { |
|
2357 | + return null; |
|
2358 | + } |
|
2330 | 2359 | } |
2331 | 2360 | |
2332 | 2361 | public function get_all_parameters_including_unknown() |
@@ -2334,7 +2363,9 @@ discard block |
||
2334 | 2363 | return $this->parameters; |
2335 | 2364 | } |
2336 | 2365 | |
2337 | - public function is_http_request() { return $this->perceive_as_http_request; } |
|
2366 | + public function is_http_request() |
|
2367 | + { |
|
2368 | +return $this->perceive_as_http_request; } |
|
2338 | 2369 | } |
2339 | 2370 | |
2340 | 2371 | class OkapiHttpRequest extends OkapiRequest |
@@ -2351,20 +2382,16 @@ discard block |
||
2351 | 2382 | # Parsing options. |
2352 | 2383 | # |
2353 | 2384 | $DEBUG_AS_USERNAME = null; |
2354 | - foreach ($options as $key => $value) |
|
2355 | - { |
|
2356 | - switch ($key) |
|
2357 | - { |
|
2385 | + foreach ($options as $key => $value) { |
|
2386 | + switch ($key) { |
|
2358 | 2387 | case 'min_auth_level': |
2359 | - if (!in_array($value, array(0, 1, 2, 3))) |
|
2360 | - { |
|
2388 | + if (!in_array($value, array(0, 1, 2, 3))) { |
|
2361 | 2389 | throw new Exception("'min_auth_level' option has invalid value: $value"); |
2362 | 2390 | } |
2363 | 2391 | $this->opt_min_auth_level = $value; |
2364 | 2392 | break; |
2365 | 2393 | case 'token_type': |
2366 | - if (!in_array($value, array("request", "access"))) |
|
2367 | - { |
|
2394 | + if (!in_array($value, array("request", "access"))) { |
|
2368 | 2395 | throw new Exception("'token_type' option has invalid value: $value"); |
2369 | 2396 | } |
2370 | 2397 | $this->opt_token_type = $value; |
@@ -2377,15 +2404,15 @@ discard block |
||
2377 | 2404 | break; |
2378 | 2405 | } |
2379 | 2406 | } |
2380 | - if ($this->opt_min_auth_level === null) throw new Exception("Required 'min_auth_level' option is missing."); |
|
2407 | + if ($this->opt_min_auth_level === null) { |
|
2408 | + throw new Exception("Required 'min_auth_level' option is missing."); |
|
2409 | + } |
|
2381 | 2410 | |
2382 | - if ($DEBUG_AS_USERNAME != null) |
|
2383 | - { |
|
2411 | + if ($DEBUG_AS_USERNAME != null) { |
|
2384 | 2412 | # Enables debugging Level 2 and Level 3 methods. Should not be committed |
2385 | 2413 | # at any time! If run on production server, make it an error. |
2386 | 2414 | |
2387 | - if (!Settings::get('DEBUG')) |
|
2388 | - { |
|
2415 | + if (!Settings::get('DEBUG')) { |
|
2389 | 2416 | throw new Exception("Attempted to use DEBUG_AS_USERNAME in ". |
2390 | 2417 | "non-debug environment. Accidental commit?"); |
2391 | 2418 | } |
@@ -2400,8 +2427,7 @@ discard block |
||
2400 | 2427 | # It it's not, check if it isn't against the rules defined in the $options. |
2401 | 2428 | # |
2402 | 2429 | |
2403 | - if ($this->get_parameter('oauth_signature')) |
|
2404 | - { |
|
2430 | + if ($this->get_parameter('oauth_signature')) { |
|
2405 | 2431 | # User is using OAuth. |
2406 | 2432 | |
2407 | 2433 | # Check for duplicate keys in the parameters. (Datastore doesn't |
@@ -2416,36 +2442,31 @@ discard block |
||
2416 | 2442 | |
2417 | 2443 | list($this->consumer, $this->token) = Okapi::$server-> |
2418 | 2444 | verify_request2($this->request, $this->opt_token_type, $this->opt_min_auth_level == 3); |
2419 | - if ($this->get_parameter('consumer_key') && $this->get_parameter('consumer_key') != $this->get_parameter('oauth_consumer_key')) |
|
2420 | - throw new BadRequest("Inproper mixing of authentication types. You used both 'consumer_key' ". |
|
2445 | + if ($this->get_parameter('consumer_key') && $this->get_parameter('consumer_key') != $this->get_parameter('oauth_consumer_key')) { |
|
2446 | + throw new BadRequest("Inproper mixing of authentication types. You used both 'consumer_key' ". |
|
2421 | 2447 | "and 'oauth_consumer_key' parameters (Level 1 and Level 2), but they do not match with ". |
2422 | 2448 | "each other. Were you trying to hack me? ;)"); |
2423 | - if ($this->opt_min_auth_level == 3 && !$this->token) |
|
2424 | - { |
|
2449 | + } |
|
2450 | + if ($this->opt_min_auth_level == 3 && !$this->token) { |
|
2425 | 2451 | throw new BadRequest("This method requires a valid Token to be included (Level 3 ". |
2426 | 2452 | "Authentication). You didn't provide one."); |
2427 | 2453 | } |
2428 | - } |
|
2429 | - else |
|
2430 | - { |
|
2431 | - if ($this->opt_min_auth_level >= 2) |
|
2432 | - { |
|
2454 | + } else { |
|
2455 | + if ($this->opt_min_auth_level >= 2) { |
|
2433 | 2456 | throw new BadRequest("This method requires OAuth signature (Level ". |
2434 | 2457 | $this->opt_min_auth_level." Authentication). You didn't sign your request."); |
2435 | - } |
|
2436 | - else |
|
2437 | - { |
|
2458 | + } else { |
|
2438 | 2459 | $consumer_key = $this->get_parameter('consumer_key'); |
2439 | - if ($consumer_key) |
|
2440 | - { |
|
2460 | + if ($consumer_key) { |
|
2441 | 2461 | $this->consumer = Okapi::$data_store->lookup_consumer($consumer_key); |
2442 | 2462 | if (!$this->consumer) { |
2443 | 2463 | throw new InvalidParam('consumer_key', "Consumer does not exist."); |
2444 | 2464 | } |
2445 | 2465 | } |
2446 | - if (($this->opt_min_auth_level == 1) && (!$this->consumer)) |
|
2447 | - throw new BadRequest("This method requires the 'consumer_key' argument (Level 1 ". |
|
2466 | + if (($this->opt_min_auth_level == 1) && (!$this->consumer)) { |
|
2467 | + throw new BadRequest("This method requires the 'consumer_key' argument (Level 1 ". |
|
2448 | 2468 | "Authentication). You didn't provide one."); |
2469 | + } |
|
2449 | 2470 | } |
2450 | 2471 | } |
2451 | 2472 | |
@@ -2472,23 +2493,24 @@ discard block |
||
2472 | 2493 | |
2473 | 2494 | # When debugging, simulate as if been run using a proper Level 3 Authentication. |
2474 | 2495 | |
2475 | - if ($DEBUG_AS_USERNAME != null) |
|
2476 | - { |
|
2496 | + if ($DEBUG_AS_USERNAME != null) { |
|
2477 | 2497 | # Note, that this will override any other valid authentication the |
2478 | 2498 | # developer might have issued. |
2479 | 2499 | |
2480 | 2500 | $debug_user_id = Db::select_value("select user_id from user where username='". |
2481 | 2501 | Db::escape_string($options['DEBUG_AS_USERNAME'])."'"); |
2482 | - if ($debug_user_id == null) |
|
2483 | - throw new Exception("Invalid user name in DEBUG_AS_USERNAME: '".$options['DEBUG_AS_USERNAME']."'"); |
|
2502 | + if ($debug_user_id == null) { |
|
2503 | + throw new Exception("Invalid user name in DEBUG_AS_USERNAME: '".$options['DEBUG_AS_USERNAME']."'"); |
|
2504 | + } |
|
2484 | 2505 | $this->consumer = new OkapiDebugConsumer(); |
2485 | 2506 | $this->token = new OkapiDebugAccessToken($debug_user_id); |
2486 | 2507 | } |
2487 | 2508 | |
2488 | 2509 | # Read the ETag. |
2489 | 2510 | |
2490 | - if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) |
|
2491 | - $this->etag = $_SERVER['HTTP_IF_NONE_MATCH']; |
|
2511 | + if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) { |
|
2512 | + $this->etag = $_SERVER['HTTP_IF_NONE_MATCH']; |
|
2513 | + } |
|
2492 | 2514 | } |
2493 | 2515 | |
2494 | 2516 | private function init_request() |
@@ -2536,8 +2558,9 @@ discard block |
||
2536 | 2558 | # in OKAPI and should be reported back. See issue 85: |
2537 | 2559 | # https://github.com/opencaching/okapi/issues/85 |
2538 | 2560 | |
2539 | - if (is_array($value)) |
|
2540 | - throw new InvalidParam($name, "Make sure you are using '$name' no more than ONCE in your URL."); |
|
2561 | + if (is_array($value)) { |
|
2562 | + throw new InvalidParam($name, "Make sure you are using '$name' no more than ONCE in your URL."); |
|
2563 | + } |
|
2541 | 2564 | return $value; |
2542 | 2565 | } |
2543 | 2566 | |
@@ -2546,5 +2569,7 @@ discard block |
||
2546 | 2569 | return $this->request->get_parameters(); |
2547 | 2570 | } |
2548 | 2571 | |
2549 | - public function is_http_request() { return true; } |
|
2572 | + public function is_http_request() |
|
2573 | + { |
|
2574 | +return true; } |
|
2550 | 2575 | } |
@@ -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 | { |
@@ -15,17 +15,17 @@ |
||
15 | 15 | # Then, visit http://yoursite/okapi/cron5. |
16 | 16 | |
17 | 17 | use Exception; |
18 | -use okapi\Okapi; |
|
19 | 18 | use okapi\BadRequest; |
20 | -use okapi\Settings; |
|
21 | -use okapi\OkapiLock; |
|
22 | -use okapi\OkapiExceptionHandler; |
|
23 | -use okapi\Db; |
|
24 | 19 | use okapi\Cache; |
20 | +use okapi\Db; |
|
25 | 21 | use okapi\Locales; |
26 | -use okapi\OkapiServiceRunner; |
|
27 | -use okapi\OkapiInternalRequest; |
|
22 | +use okapi\Okapi; |
|
23 | +use okapi\OkapiExceptionHandler; |
|
28 | 24 | use okapi\OkapiInternalConsumer; |
25 | +use okapi\OkapiInternalRequest; |
|
26 | +use okapi\OkapiLock; |
|
27 | +use okapi\OkapiServiceRunner; |
|
28 | +use okapi\Settings; |
|
29 | 29 | use okapi\services\replicate\ReplicateCommon; |
30 | 30 | |
31 | 31 |
@@ -99,7 +99,7 @@ discard block |
||
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 | } |
@@ -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 |
@@ -404,7 +404,7 @@ discard block |
||
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 | } |
@@ -477,7 +477,7 @@ discard block |
||
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 | } |
@@ -583,7 +583,7 @@ 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 | 589 | require_once($GLOBALS['rootpath']."okapi/services/replicate/replicate_common.inc.php"); |
@@ -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); |
@@ -607,7 +607,7 @@ discard block |
||
607 | 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( |
@@ -654,7 +654,7 @@ 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; |
@@ -663,7 +663,7 @@ discard block |
||
663 | 663 | delete from okapi_clog |
664 | 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(); |
@@ -806,7 +806,7 @@ 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 | 812 | require_once($GLOBALS['rootpath']."okapi/locale/locales.php"); |
@@ -35,8 +35,7 @@ discard block |
||
35 | 35 | public static function get_enabled_cronjobs() |
36 | 36 | { |
37 | 37 | static $cache = null; |
38 | - if ($cache == null) |
|
39 | - { |
|
38 | + if ($cache == null) { |
|
40 | 39 | $cache = array( |
41 | 40 | new OAuthCleanupCronJob(), |
42 | 41 | new CacheCleanupCronJob(), |
@@ -54,9 +53,10 @@ discard block |
||
54 | 53 | new SearchSetsCleanerJob(), |
55 | 54 | new TableOptimizerJob(), |
56 | 55 | ); |
57 | - foreach ($cache as $cronjob) |
|
58 | - if (!in_array($cronjob->get_type(), array('pre-request', 'cron-5'))) |
|
56 | + foreach ($cache as $cronjob) { |
|
57 | + if (!in_array($cronjob->get_type(), array('pre-request', 'cron-5'))) |
|
59 | 58 | throw new Exception("Cronjob '".$cronjob->get_name()."' has an invalid (unsupported) type."); |
59 | + } |
|
60 | 60 | } |
61 | 61 | return $cache; |
62 | 62 | } |
@@ -74,25 +74,18 @@ discard block |
||
74 | 74 | $lock->acquire(); |
75 | 75 | |
76 | 76 | $schedule = Cache::get("cron_schedule"); |
77 | - if ($schedule == null) |
|
78 | - $schedule = array(); |
|
79 | - foreach (self::get_enabled_cronjobs() as $cronjob) |
|
80 | - { |
|
77 | + if ($schedule == null) { |
|
78 | + $schedule = array(); |
|
79 | + } |
|
80 | + foreach (self::get_enabled_cronjobs() as $cronjob) { |
|
81 | 81 | $name = $cronjob->get_name(); |
82 | - if ((!isset($schedule[$name])) || ($schedule[$name] <= time())) |
|
83 | - { |
|
84 | - if ($cronjob->get_type() != $type) |
|
85 | - { |
|
82 | + if ((!isset($schedule[$name])) || ($schedule[$name] <= time())) { |
|
83 | + if ($cronjob->get_type() != $type) { |
|
86 | 84 | $next_run = isset($schedule[$name]) ? $schedule[$name] : (time() - 1); |
87 | - } |
|
88 | - else |
|
89 | - { |
|
90 | - try |
|
91 | - { |
|
85 | + } else { |
|
86 | + try { |
|
92 | 87 | $cronjob->execute(); |
93 | - } |
|
94 | - catch (Exception $e) |
|
95 | - { |
|
88 | + } catch (Exception $e) { |
|
96 | 89 | Okapi::mail_admins("Cronjob error: ".$cronjob->get_name(), |
97 | 90 | OkapiExceptionHandler::get_exception_info($e)); |
98 | 91 | } |
@@ -106,8 +99,7 @@ discard block |
||
106 | 99 | # Remove "stale" schedule keys (those which are no longer declared). |
107 | 100 | |
108 | 101 | $fixed_schedule = array(); |
109 | - foreach (self::get_enabled_cronjobs() as $cronjob) |
|
110 | - { |
|
102 | + foreach (self::get_enabled_cronjobs() as $cronjob) { |
|
111 | 103 | $name = $cronjob->get_name(); |
112 | 104 | $fixed_schedule[$name] = $schedule[$name]; |
113 | 105 | } |
@@ -116,9 +108,10 @@ discard block |
||
116 | 108 | # Return the nearest scheduled event time. |
117 | 109 | |
118 | 110 | $nearest = time() + 3600; |
119 | - foreach ($fixed_schedule as $name => $time) |
|
120 | - if ($time < $nearest) |
|
111 | + foreach ($fixed_schedule as $name => $time) { |
|
112 | + if ($time < $nearest) |
|
121 | 113 | $nearest = $time; |
114 | + } |
|
122 | 115 | Cache::set("cron_schedule", $fixed_schedule, 30*86400); |
123 | 116 | $lock->release(); |
124 | 117 | return $nearest; |
@@ -132,10 +125,8 @@ discard block |
||
132 | 125 | { |
133 | 126 | require_once($GLOBALS['rootpath'].'okapi/service_runner.php'); |
134 | 127 | |
135 | - foreach (self::get_enabled_cronjobs() as $cronjob) |
|
136 | - { |
|
137 | - if (($cronjob->get_name() == $job_name) || ($cronjob->get_name() == "okapi\\cronjobs\\".$job_name)) |
|
138 | - { |
|
128 | + foreach (self::get_enabled_cronjobs() as $cronjob) { |
|
129 | + if (($cronjob->get_name() == $job_name) || ($cronjob->get_name() == "okapi\\cronjobs\\".$job_name)) { |
|
139 | 130 | $cronjob->execute(); |
140 | 131 | return; |
141 | 132 | } |
@@ -150,11 +141,13 @@ discard block |
||
150 | 141 | public static function reset_job_schedule($job_name) |
151 | 142 | { |
152 | 143 | $thejob = null; |
153 | - foreach (self::get_enabled_cronjobs() as $tmp) |
|
154 | - if (($tmp->get_name() == $job_name) || ($tmp->get_name() == "okapi\\cronjobs\\".$job_name)) |
|
144 | + foreach (self::get_enabled_cronjobs() as $tmp) { |
|
145 | + if (($tmp->get_name() == $job_name) || ($tmp->get_name() == "okapi\\cronjobs\\".$job_name)) |
|
155 | 146 | $thejob = $tmp; |
156 | - if ($thejob == null) |
|
157 | - throw new Exception("Could not reset schedule for job $job_name. $jon_name not found."); |
|
147 | + } |
|
148 | + if ($thejob == null) { |
|
149 | + throw new Exception("Could not reset schedule for job $job_name. $jon_name not found."); |
|
150 | + } |
|
158 | 151 | |
159 | 152 | # We have to acquire lock on the schedule. This might take some time if cron-5 jobs are |
160 | 153 | # currently being run. |
@@ -164,10 +157,10 @@ discard block |
||
164 | 157 | $lock->acquire(); |
165 | 158 | |
166 | 159 | $schedule = Cache::get("cron_schedule"); |
167 | - if ($schedule != null) |
|
168 | - { |
|
169 | - if (isset($schedule[$thejob->get_name()])) |
|
170 | - unset($schedule[$thejob->get_name()]); |
|
160 | + if ($schedule != null) { |
|
161 | + if (isset($schedule[$thejob->get_name()])) { |
|
162 | + unset($schedule[$thejob->get_name()]); |
|
163 | + } |
|
171 | 164 | Cache::set("cron_schedule", $schedule, 30*86400); |
172 | 165 | } |
173 | 166 | |
@@ -181,7 +174,9 @@ discard block |
||
181 | 174 | public abstract function execute(); |
182 | 175 | |
183 | 176 | /** Get unique name for this cronjob. */ |
184 | - public function get_name() { return get_class($this); } |
|
177 | + public function get_name() |
|
178 | + { |
|
179 | +return get_class($this); } |
|
185 | 180 | |
186 | 181 | /** |
187 | 182 | * Get the type of this cronjob. Currently there are two: 'pre-request' |
@@ -212,7 +207,9 @@ discard block |
||
212 | 207 | /** |
213 | 208 | * Always returns 'pre-request'. |
214 | 209 | */ |
215 | - public final function get_type() { return 'pre-request'; } |
|
210 | + public final function get_type() |
|
211 | + { |
|
212 | +return 'pre-request'; } |
|
216 | 213 | |
217 | 214 | /** |
218 | 215 | * Return number of seconds - a *minimum* time period that should pass between |
@@ -235,7 +232,9 @@ discard block |
||
235 | 232 | /** |
236 | 233 | * Always returns 'cron-5'. |
237 | 234 | */ |
238 | - public final function get_type() { return 'cron-5'; } |
|
235 | + public final function get_type() |
|
236 | + { |
|
237 | +return 'cron-5'; } |
|
239 | 238 | |
240 | 239 | /** |
241 | 240 | * Return number of seconds - period of time after which cronjob execution |
@@ -257,18 +256,23 @@ discard block |
||
257 | 256 | */ |
258 | 257 | class OAuthCleanupCronJob extends PrerequestCronJob |
259 | 258 | { |
260 | - public function get_period() { return 300; } # 5 minutes |
|
259 | + public function get_period() |
|
260 | + { |
|
261 | +return 300; } # 5 minutes |
|
261 | 262 | public function execute() |
262 | 263 | { |
263 | - if (Okapi::$data_store) |
|
264 | - Okapi::$data_store->cleanup(); |
|
264 | + if (Okapi::$data_store) { |
|
265 | + Okapi::$data_store->cleanup(); |
|
266 | + } |
|
265 | 267 | } |
266 | 268 | } |
267 | 269 | |
268 | 270 | /** Clean up the saved search tables, every 10 minutes. */ |
269 | 271 | class SearchSetsCleanerJob extends Cron5Job |
270 | 272 | { |
271 | - public function get_period() { return 600; } |
|
273 | + public function get_period() |
|
274 | + { |
|
275 | +return 600; } |
|
272 | 276 | public function execute() |
273 | 277 | { |
274 | 278 | Db::execute(" |
@@ -286,7 +290,9 @@ discard block |
||
286 | 290 | /** Clean up the cache, once per hour. */ |
287 | 291 | class CacheCleanupCronJob extends Cron5Job |
288 | 292 | { |
289 | - public function get_period() { return 3600; } |
|
293 | + public function get_period() |
|
294 | + { |
|
295 | +return 3600; } |
|
290 | 296 | public function execute() |
291 | 297 | { |
292 | 298 | # Delete all expired elements. |
@@ -358,11 +364,14 @@ discard block |
||
358 | 364 | /** Reads temporary (fast) stats-tables and reformats them into more permanent structures. */ |
359 | 365 | class StatsWriterCronJob extends PrerequestCronJob |
360 | 366 | { |
361 | - public function get_period() { return 60; } # 1 minute |
|
367 | + public function get_period() |
|
368 | + { |
|
369 | +return 60; } # 1 minute |
|
362 | 370 | public function execute() |
363 | 371 | { |
364 | - if (Okapi::get_var('db_version', 0) + 0 < 32) |
|
365 | - return; |
|
372 | + if (Okapi::get_var('db_version', 0) + 0 < 32) { |
|
373 | + return; |
|
374 | + } |
|
366 | 375 | Db::execute("lock tables okapi_stats_hourly write, okapi_stats_temp write;"); |
367 | 376 | try { |
368 | 377 | $rs = Db::query(" |
@@ -377,8 +386,7 @@ discard block |
||
377 | 386 | from okapi_stats_temp |
378 | 387 | group by substr(`datetime`, 1, 13), consumer_key, user_id, service_name, calltype |
379 | 388 | "); |
380 | - while ($row = Db::fetch_assoc($rs)) |
|
381 | - { |
|
389 | + while ($row = Db::fetch_assoc($rs)) { |
|
382 | 390 | Db::execute(" |
383 | 391 | insert into okapi_stats_hourly (consumer_key, user_id, period_start, service_name, |
384 | 392 | total_calls, http_calls, total_runtime, http_runtime) |
@@ -416,11 +424,14 @@ discard block |
||
416 | 424 | */ |
417 | 425 | class StatsCompressorCronJob extends Cron5Job |
418 | 426 | { |
419 | - public function get_period() { return 3600; } # 1 hour |
|
427 | + public function get_period() |
|
428 | + { |
|
429 | +return 3600; } # 1 hour |
|
420 | 430 | public function execute() |
421 | 431 | { |
422 | - if (Okapi::get_var('db_version', 0) + 0 < 94) |
|
423 | - return; |
|
432 | + if (Okapi::get_var('db_version', 0) + 0 < 94) { |
|
433 | + return; |
|
434 | + } |
|
424 | 435 | |
425 | 436 | # We will process a single month, every time we are being run. |
426 | 437 | |
@@ -489,7 +500,9 @@ discard block |
||
489 | 500 | */ |
490 | 501 | class CheckCronTab1 extends Cron5Job |
491 | 502 | { |
492 | - public function get_period() { return 3600; } |
|
503 | + public function get_period() |
|
504 | + { |
|
505 | +return 3600; } |
|
493 | 506 | public function execute() |
494 | 507 | { |
495 | 508 | Cache::set('crontab_last_ping', time(), 86400); |
@@ -502,14 +515,17 @@ discard block |
||
502 | 515 | */ |
503 | 516 | class CheckCronTab2 extends PrerequestCronJob |
504 | 517 | { |
505 | - public function get_period() { return 30 * 60; } |
|
518 | + public function get_period() |
|
519 | + { |
|
520 | +return 30 * 60; } |
|
506 | 521 | public function execute() |
507 | 522 | { |
508 | 523 | $last_ping = Cache::get('crontab_last_ping'); |
509 | - if ($last_ping === null) |
|
510 | - $last_ping = time() - 86400; # if not set, assume 1 day ago. |
|
511 | - if ($last_ping > time() - 3600) |
|
512 | - { |
|
524 | + if ($last_ping === null) { |
|
525 | + $last_ping = time() - 86400; |
|
526 | + } |
|
527 | + # if not set, assume 1 day ago. |
|
528 | + if ($last_ping > time() - 3600) { |
|
513 | 529 | # There was a ping during the last hour. Everything is okay. |
514 | 530 | # Reset the counter and return. |
515 | 531 | |
@@ -520,15 +536,13 @@ discard block |
||
520 | 536 | # There was no ping. Decrement the counter. When reached zero, alert. |
521 | 537 | |
522 | 538 | $counter = Cache::get('crontab_check_counter'); |
523 | - if ($counter === null) |
|
524 | - $counter = 5; |
|
539 | + if ($counter === null) { |
|
540 | + $counter = 5; |
|
541 | + } |
|
525 | 542 | $counter--; |
526 | - if ($counter > 0) |
|
527 | - { |
|
543 | + if ($counter > 0) { |
|
528 | 544 | Cache::set('crontab_check_counter', $counter, 86400); |
529 | - } |
|
530 | - elseif ($counter == 0) |
|
531 | - { |
|
545 | + } elseif ($counter == 0) { |
|
532 | 546 | Okapi::mail_admins( |
533 | 547 | "Crontab not working.", |
534 | 548 | "Hello. OKAPI detected, that it's crontab is not working properly.\n". |
@@ -554,7 +568,9 @@ discard block |
||
554 | 568 | */ |
555 | 569 | class ChangeLogWriterJob extends Cron5Job |
556 | 570 | { |
557 | - public function get_period() { return 300; } |
|
571 | + public function get_period() |
|
572 | + { |
|
573 | +return 300; } |
|
558 | 574 | public function execute() |
559 | 575 | { |
560 | 576 | require_once($GLOBALS['rootpath']."okapi/services/replicate/replicate_common.inc.php"); |
@@ -570,7 +586,9 @@ discard block |
||
570 | 586 | */ |
571 | 587 | class ChangeLogCheckerJob extends Cron5Job |
572 | 588 | { |
573 | - public function get_period() { return 86400; } |
|
589 | + public function get_period() |
|
590 | + { |
|
591 | +return 86400; } |
|
574 | 592 | public function execute() |
575 | 593 | { |
576 | 594 | require_once($GLOBALS['rootpath']."okapi/services/replicate/replicate_common.inc.php"); |
@@ -583,7 +601,9 @@ discard block |
||
583 | 601 | */ |
584 | 602 | class FulldumpGeneratorJob extends Cron5Job |
585 | 603 | { |
586 | - public function get_period() { return 7*86400; } |
|
604 | + public function get_period() |
|
605 | + { |
|
606 | +return 7*86400; } |
|
587 | 607 | public function execute() |
588 | 608 | { |
589 | 609 | require_once($GLOBALS['rootpath']."okapi/services/replicate/replicate_common.inc.php"); |
@@ -596,7 +616,9 @@ discard block |
||
596 | 616 | */ |
597 | 617 | class TileTreeUpdater extends Cron5Job |
598 | 618 | { |
599 | - public function get_period() { return 5*60; } |
|
619 | + public function get_period() |
|
620 | + { |
|
621 | +return 5*60; } |
|
600 | 622 | public function execute() |
601 | 623 | { |
602 | 624 | $current_clog_revision = Okapi::get_var('clog_revision', 0); |
@@ -605,18 +627,24 @@ discard block |
||
605 | 627 | # No update necessary. |
606 | 628 | } elseif ($tiletree_revision < $current_clog_revision) { |
607 | 629 | 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 |
|
630 | + if ($current_clog_revision - $tiletree_revision < 30000) { |
|
631 | + # In the middle of 2012, OCPL generated 30000 entries per week |
|
609 | 632 | { |
610 | - for ($timeout = time() + 240; time() < $timeout; ) # Try to stop after 4 minutes. |
|
633 | + for ($timeout = time() + 240; |
|
634 | + } |
|
635 | + time() < $timeout; ) { |
|
636 | + # Try to stop after 4 minutes. |
|
611 | 637 | { |
612 | 638 | try { |
613 | 639 | $response = OkapiServiceRunner::call('services/replicate/changelog', new OkapiInternalRequest( |
614 | 640 | new OkapiInternalConsumer(), null, array('since' => $tiletree_revision))); |
641 | + } |
|
615 | 642 | \okapi\services\caches\map\ReplicateListener::receive($response['changelog']); |
616 | 643 | $tiletree_revision = $response['revision']; |
617 | 644 | Okapi::set_var('clog_followup_revision', $tiletree_revision); |
618 | - if (!$response['more']) |
|
619 | - break; |
|
645 | + if (!$response['more']) { |
|
646 | + break; |
|
647 | + } |
|
620 | 648 | } catch (BadRequest $e) { |
621 | 649 | # Invalid 'since' parameter? May happen when crontab was |
622 | 650 | # not working for more than 10 days. Or, just after OKAPI |
@@ -640,24 +668,27 @@ discard block |
||
640 | 668 | /** Once per day, removes all revisions older than 10 days from okapi_clog table. */ |
641 | 669 | class ChangeLogCleanerJob extends Cron5Job |
642 | 670 | { |
643 | - public function get_period() { return 86400; } |
|
671 | + public function get_period() |
|
672 | + { |
|
673 | +return 86400; } |
|
644 | 674 | public function execute() |
645 | 675 | { |
646 | 676 | require_once($GLOBALS['rootpath']."okapi/services/replicate/replicate_common.inc.php"); |
647 | 677 | $max_revision = ReplicateCommon::get_revision(); |
648 | 678 | $cache_key = 'clog_revisions_daily'; |
649 | 679 | $data = Cache::get($cache_key); |
650 | - if ($data == null) |
|
651 | - $data = array(); |
|
680 | + if ($data == null) { |
|
681 | + $data = array(); |
|
682 | + } |
|
652 | 683 | $data[time()] = $max_revision; |
653 | 684 | $new_min_revision = 1; |
654 | 685 | $new_data = array(); |
655 | - foreach ($data as $time => $r) |
|
656 | - { |
|
657 | - if ($time < time() - 10*86400) |
|
658 | - $new_min_revision = max($new_min_revision, $r); |
|
659 | - else |
|
660 | - $new_data[$time] = $r; |
|
686 | + foreach ($data as $time => $r) { |
|
687 | + if ($time < time() - 10*86400) { |
|
688 | + $new_min_revision = max($new_min_revision, $r); |
|
689 | + } else { |
|
690 | + $new_data[$time] = $r; |
|
691 | + } |
|
661 | 692 | } |
662 | 693 | Db::execute(" |
663 | 694 | delete from okapi_clog |
@@ -673,7 +704,9 @@ discard block |
||
673 | 704 | */ |
674 | 705 | class AdminStatsSender extends Cron5Job |
675 | 706 | { |
676 | - public function get_period() { return 7*86400; } |
|
707 | + public function get_period() |
|
708 | + { |
|
709 | +return 7*86400; } |
|
677 | 710 | public function execute() |
678 | 711 | { |
679 | 712 | ob_start(); |
@@ -718,15 +751,16 @@ discard block |
||
718 | 751 | print "== Consumers ==\n\n"; |
719 | 752 | print "Consumer name Calls Runtime\n"; |
720 | 753 | print "----------------------------------- ------- -----------\n"; |
721 | - foreach ($consumers as $row) |
|
722 | - { |
|
754 | + foreach ($consumers as $row) { |
|
723 | 755 | $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)."..."; |
|
756 | + if ($row['consumer_key'] == 'anonymous') { |
|
757 | + $name = "Anonymous (Level 0 Authentication)"; |
|
758 | + } elseif ($row['consumer_key'] == 'facade') { |
|
759 | + $name = "Internal usage via Facade"; |
|
760 | + } |
|
761 | + if (mb_strlen($name) > 35) { |
|
762 | + $name = mb_substr($name, 0, 32)."..."; |
|
763 | + } |
|
730 | 764 | print self::mb_str_pad($name, 35, " ", STR_PAD_RIGHT); |
731 | 765 | print str_pad($row['http_calls'], 8, " ", STR_PAD_LEFT); |
732 | 766 | print str_pad(sprintf("%01.2f", $row['http_runtime']), 11, " ", STR_PAD_LEFT)."s\n"; |
@@ -746,11 +780,11 @@ discard block |
||
746 | 780 | print "== Methods ==\n\n"; |
747 | 781 | print "Service name Calls Runtime Avg\n"; |
748 | 782 | print "----------------------------------- ------- ----------- --------\n"; |
749 | - foreach ($methods as $row) |
|
750 | - { |
|
783 | + foreach ($methods as $row) { |
|
751 | 784 | $name = $row['service_name']; |
752 | - if (mb_strlen($name) > 35) |
|
753 | - $name = mb_substr($name, 0, 32)."..."; |
|
785 | + if (mb_strlen($name) > 35) { |
|
786 | + $name = mb_substr($name, 0, 32)."..."; |
|
787 | + } |
|
754 | 788 | print self::mb_str_pad($name, 35, " ", STR_PAD_RIGHT); |
755 | 789 | print str_pad($row['http_calls'], 8, " ", STR_PAD_LEFT); |
756 | 790 | print str_pad(sprintf("%01.2f", $row['http_runtime']), 11, " ", STR_PAD_LEFT)."s"; |
@@ -774,11 +808,11 @@ discard block |
||
774 | 808 | print "== Current OAuth usage by Consumers with at least 5 users ==\n\n"; |
775 | 809 | print "Consumer name Users\n"; |
776 | 810 | print "----------------------------------- -------\n"; |
777 | - foreach ($oauth_users as $row) |
|
778 | - { |
|
811 | + foreach ($oauth_users as $row) { |
|
779 | 812 | $name = $row['name']; |
780 | - if (mb_strlen($name) > 35) |
|
781 | - $name = mb_substr($name, 0, 32)."..."; |
|
813 | + if (mb_strlen($name) > 35) { |
|
814 | + $name = mb_substr($name, 0, 32)."..."; |
|
815 | + } |
|
782 | 816 | print self::mb_str_pad($name, 35, " ", STR_PAD_RIGHT); |
783 | 817 | print str_pad($row['users'], 8, " ", STR_PAD_LEFT)."\n"; |
784 | 818 | } |
@@ -806,18 +840,23 @@ discard block |
||
806 | 840 | */ |
807 | 841 | class LocaleChecker extends Cron5Job |
808 | 842 | { |
809 | - public function get_period() { return 7*86400; } |
|
843 | + public function get_period() |
|
844 | + { |
|
845 | +return 7*86400; } |
|
810 | 846 | public function execute() |
811 | 847 | { |
812 | 848 | require_once($GLOBALS['rootpath']."okapi/locale/locales.php"); |
813 | 849 | $required = Locales::get_required_locales(); |
814 | 850 | $installed = Locales::get_installed_locales(); |
815 | 851 | $missing = array(); |
816 | - foreach ($required as $locale) |
|
817 | - if (!in_array($locale, $installed)) |
|
852 | + foreach ($required as $locale) { |
|
853 | + if (!in_array($locale, $installed)) |
|
818 | 854 | $missing[] = $locale; |
819 | - if (count($missing) == 0) |
|
820 | - return; # okay! |
|
855 | + } |
|
856 | + if (count($missing) == 0) { |
|
857 | + return; |
|
858 | + } |
|
859 | + # okay! |
|
821 | 860 | ob_start(); |
822 | 861 | print "Hi!\n\n"; |
823 | 862 | print "Your system is missing some locales required by OKAPI for proper\n"; |
@@ -825,24 +864,21 @@ discard block |
||
825 | 864 | print "languages. This number (hopefully) will be growing.\n\n"; |
826 | 865 | print "Please take a moment to install the following missing locales:\n\n"; |
827 | 866 | $prefixes = array(); |
828 | - foreach ($missing as $locale) |
|
829 | - { |
|
867 | + foreach ($missing as $locale) { |
|
830 | 868 | print " - ".$locale."\n"; |
831 | 869 | $prefixes[substr($locale, 0, 2)] = true; |
832 | 870 | } |
833 | 871 | $prefixes = array_keys($prefixes); |
834 | 872 | print "\n"; |
835 | - if ((count($missing) == 1) && ($missing[0] == 'POSIX')) |
|
836 | - { |
|
873 | + if ((count($missing) == 1) && ($missing[0] == 'POSIX')) { |
|
837 | 874 | # I don't remember how to install POSIX, probably everyone has it anyway. |
838 | - } |
|
839 | - else |
|
840 | - { |
|
875 | + } else { |
|
841 | 876 | print "On Debian, try the following:\n\n"; |
842 | - foreach ($prefixes as $lang) |
|
843 | - { |
|
844 | - if ($lang != 'PO') # Two first letters cut from POSIX. |
|
877 | + foreach ($prefixes as $lang) { |
|
878 | + if ($lang != 'PO') { |
|
879 | + # Two first letters cut from POSIX. |
|
845 | 880 | print "sudo apt-get install language-pack-".$lang."-base\n"; |
881 | + } |
|
846 | 882 | } |
847 | 883 | print "sudo service apache2 restart\n"; |
848 | 884 | print "\n"; |
@@ -857,7 +893,9 @@ discard block |
||
857 | 893 | /** Once per day, optimize certain MySQL tables. */ |
858 | 894 | class TableOptimizerJob extends Cron5Job |
859 | 895 | { |
860 | - public function get_period() { return 86400; } |
|
896 | + public function get_period() |
|
897 | + { |
|
898 | +return 86400; } |
|
861 | 899 | public function execute() |
862 | 900 | { |
863 | 901 | Db::query("optimize table okapi_tile_caches"); |
@@ -140,7 +140,7 @@ |
||
140 | 140 | return; |
141 | 141 | } |
142 | 142 | } |
143 | - throw new Exception("CronJob $job_name not found."); |
|
143 | + throw new Exception("cronjob $job_name not found."); |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | /** |
@@ -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,9 +24,9 @@ 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; |
@@ -37,13 +37,13 @@ discard block |
||
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']; |
@@ -96,16 +96,16 @@ discard block |
||
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 | } |
@@ -130,13 +130,13 @@ discard block |
||
130 | 130 | function CentralDirRead_End($cd_info) { |
131 | 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 | 141 | $x['bin'] = $b.$x['v_comm']; |
142 | 142 | return $x; |
@@ -146,26 +146,26 @@ 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']); |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | |
178 | 178 | function RaiseError($Msg) { |
179 | 179 | if ($this->DisplayError) { |
180 | - if (PHP_SAPI==='cli') { |
|
180 | + if (PHP_SAPI === 'cli') { |
|
181 | 181 | echo get_class($this).' ERROR with the zip archive: '.$Msg."\r\n"; |
182 | 182 | } else { |
183 | 183 | echo '<strong>'.get_class($this).' ERROR with the zip archive:</strong> '.$Msg.'<br>'."\r\n"; |
@@ -187,7 +187,7 @@ discard block |
||
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++; |
@@ -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,7 +290,7 @@ 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); |
@@ -299,7 +299,7 @@ discard block |
||
299 | 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 | 305 | if ($Uncompress) $this->RaiseError('Unable to uncompress file "'.$NameOrIdx.'" because it is compressed with method '.$meth.'.'); |
@@ -315,20 +315,20 @@ 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 | |
@@ -337,18 +337,18 @@ discard block |
||
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,9 +472,9 @@ 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) ) { |
|
477 | + if (($File !== '') && ($this->ArchFile === $File) && ($Render == self::TBSZIP_FILE)) { |
|
478 | 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 | } |
@@ -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,13 +520,13 @@ 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 | 532 | $this->OutputFromString($b1.$ReplInfo['data'].$b2); |
@@ -534,8 +534,8 @@ discard block |
||
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,24 +627,24 @@ 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) { |
|
634 | + if ($this->OutputHandle === false) { |
|
635 | 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); |
|
646 | + header('Pragma: no-cache'); |
|
647 | + if ($ContentType != '') header('Content-Type: '.$ContentType); |
|
648 | 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'); |
@@ -652,7 +652,7 @@ discard block |
||
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,9 +717,9 @@ 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 | } |
@@ -732,11 +732,11 @@ discard block |
||
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'; |
@@ -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,25 +771,25 @@ 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) { |
@@ -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,20 +835,20 @@ 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 | |
@@ -858,23 +858,23 @@ discard block |
||
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,7 +929,7 @@ 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 { |
@@ -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']; |
@@ -11,22 +11,28 @@ discard block |
||
11 | 11 | */ |
12 | 12 | |
13 | 13 | |
14 | -class clsTbsZip { |
|
14 | +class clsTbsZip |
|
15 | +{ |
|
15 | 16 | const TBSZIP_DOWNLOAD = 1; // download (default) |
16 | 17 | const TBSZIP_NOHEADER = 4; // option to use with DOWNLOAD: no header is sent |
17 | 18 | const TBSZIP_FILE = 8; // output to file , or add from file |
18 | 19 | const TBSZIP_STRING = 32; // output to string, or add from string |
19 | 20 | |
20 | - function __construct() { |
|
21 | + function __construct() |
|
22 | + { |
|
21 | 23 | $this->Meth8Ok = extension_loaded('zlib'); // check if Zlib extension is available. This is need for compress and uncompress with method 8. |
22 | 24 | $this->DisplayError = true; |
23 | 25 | $this->ArchFile = ''; |
24 | 26 | $this->Error = false; |
25 | 27 | } |
26 | 28 | |
27 | - function CreateNew($ArchName='new.zip') { |
|
29 | + function CreateNew($ArchName='new.zip') |
|
30 | + { |
|
28 | 31 | // 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 |
|
32 | + if (!isset($this->Meth8Ok)) { |
|
33 | + $this->__construct(); |
|
34 | + } |
|
35 | + // for PHP 4 compatibility |
|
30 | 36 | $this->Close(); // note that $this->ArchHnd is set to false here |
31 | 37 | $this->Error = false; |
32 | 38 | $this->ArchFile = $ArchName; |
@@ -37,9 +43,13 @@ discard block |
||
37 | 43 | $this->CdPos = $this->CdInfo['p_cd']; |
38 | 44 | } |
39 | 45 | |
40 | - function Open($ArchFile, $UseIncludePath=false) { |
|
46 | + function Open($ArchFile, $UseIncludePath=false) |
|
47 | + { |
|
41 | 48 | // Open the zip archive |
42 | - if (!isset($this->Meth8Ok)) $this->__construct(); // for PHP 4 compatibility |
|
49 | + if (!isset($this->Meth8Ok)) { |
|
50 | + $this->__construct(); |
|
51 | + } |
|
52 | + // for PHP 4 compatibility |
|
43 | 53 | $this->Close(); // close handle and init info |
44 | 54 | $this->Error = false; |
45 | 55 | $this->ArchIsNew = false; |
@@ -53,12 +63,17 @@ discard block |
||
53 | 63 | $this->ArchHnd = fopen($ArchFile, 'rb', $UseIncludePath); |
54 | 64 | } |
55 | 65 | $ok = !($this->ArchHnd===false); |
56 | - if ($ok) $ok = $this->CentralDirRead(); |
|
66 | + if ($ok) { |
|
67 | + $ok = $this->CentralDirRead(); |
|
68 | + } |
|
57 | 69 | return $ok; |
58 | 70 | } |
59 | 71 | |
60 | - function Close() { |
|
61 | - if (isset($this->ArchHnd) and ($this->ArchHnd!==false)) fclose($this->ArchHnd); |
|
72 | + function Close() |
|
73 | + { |
|
74 | + if (isset($this->ArchHnd) and ($this->ArchHnd!==false)) { |
|
75 | + fclose($this->ArchHnd); |
|
76 | + } |
|
62 | 77 | $this->ArchFile = ''; |
63 | 78 | $this->ArchHnd = false; |
64 | 79 | $this->CdInfo = array(); |
@@ -69,7 +84,8 @@ discard block |
||
69 | 84 | $this->ArchCancelModif(); |
70 | 85 | } |
71 | 86 | |
72 | - function ArchCancelModif() { |
|
87 | + function ArchCancelModif() |
|
88 | + { |
|
73 | 89 | $this->LastReadComp = false; // compression of the last read file (1=compressed, 0=stored not compressed, -1= stored compressed but read uncompressed) |
74 | 90 | $this->LastReadIdx = false; // index of the last file read |
75 | 91 | $this->ReplInfo = array(); |
@@ -77,21 +93,28 @@ discard block |
||
77 | 93 | $this->AddInfo = array(); |
78 | 94 | } |
79 | 95 | |
80 | - function FileAdd($Name, $Data, $DataType=self::TBSZIP_STRING, $Compress=true) { |
|
96 | + function FileAdd($Name, $Data, $DataType=self::TBSZIP_STRING, $Compress=true) |
|
97 | + { |
|
81 | 98 | |
82 | - if ($Data===false) return $this->FileCancelModif($Name, false); // Cancel a previously added file |
|
99 | + if ($Data===false) { |
|
100 | + return $this->FileCancelModif($Name, false); |
|
101 | + } |
|
102 | + // Cancel a previously added file |
|
83 | 103 | |
84 | 104 | // Save information for adding a new file into the archive |
85 | 105 | $Diff = 30 + 46 + 2*strlen($Name); // size of the header + cd info |
86 | 106 | $Ref = $this->_DataCreateNewRef($Data, $DataType, $Compress, $Diff, $Name); |
87 | - if ($Ref===false) return false; |
|
107 | + if ($Ref===false) { |
|
108 | + return false; |
|
109 | + } |
|
88 | 110 | $Ref['name'] = $Name; |
89 | 111 | $this->AddInfo[] = $Ref; |
90 | 112 | return $Ref['res']; |
91 | 113 | |
92 | 114 | } |
93 | 115 | |
94 | - function CentralDirRead() { |
|
116 | + function CentralDirRead() |
|
117 | + { |
|
95 | 118 | $cd_info = 'PK'.chr(05).chr(06); // signature of the Central Directory |
96 | 119 | $cd_pos = -22; |
97 | 120 | $this->_MoveTo($cd_pos, SEEK_END); |
@@ -113,8 +136,12 @@ discard block |
||
113 | 136 | $this->CdFileNbr = $this->CdInfo['file_nbr_curr']; |
114 | 137 | $this->CdPos = $this->CdInfo['p_cd']; |
115 | 138 | |
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.'); |
|
139 | + if ($this->CdFileNbr<=0) { |
|
140 | + return $this->RaiseError('No header found in the Central Directory.'); |
|
141 | + } |
|
142 | + if ($this->CdPos<=0) { |
|
143 | + return $this->RaiseError('No position found for the Central Directory.'); |
|
144 | + } |
|
118 | 145 | |
119 | 146 | $this->_MoveTo($this->CdPos); |
120 | 147 | for ($i=0;$i<$this->CdFileNbr;$i++) { |
@@ -127,7 +154,8 @@ discard block |
||
127 | 154 | return true; |
128 | 155 | } |
129 | 156 | |
130 | - function CentralDirRead_End($cd_info) { |
|
157 | + function CentralDirRead_End($cd_info) |
|
158 | + { |
|
131 | 159 | $b = $cd_info.$this->_ReadData(18); |
132 | 160 | $x = array(); |
133 | 161 | $x['disk_num_curr'] = $this->_GetDec($b,4,2); // number of this disk |
@@ -142,12 +170,15 @@ discard block |
||
142 | 170 | return $x; |
143 | 171 | } |
144 | 172 | |
145 | - function CentralDirRead_File($idx) { |
|
173 | + function CentralDirRead_File($idx) |
|
174 | + { |
|
146 | 175 | |
147 | 176 | $b = $this->_ReadData(46); |
148 | 177 | |
149 | 178 | $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)."."); |
|
179 | + if ($x!=='h:02014b50') { |
|
180 | + return $this->RaiseError("Signature of Central Directory Header #".$idx." (file information) expected but not found at position ".$this->_TxtPos(ftell($this->ArchHnd) - 46)."."); |
|
181 | + } |
|
151 | 182 | |
152 | 183 | $x = array(); |
153 | 184 | $x['vers_used'] = $this->_GetDec($b,4,2); |
@@ -175,7 +206,8 @@ discard block |
||
175 | 206 | return $x; |
176 | 207 | } |
177 | 208 | |
178 | - function RaiseError($Msg) { |
|
209 | + function RaiseError($Msg) |
|
210 | + { |
|
179 | 211 | if ($this->DisplayError) { |
180 | 212 | if (PHP_SAPI==='cli') { |
181 | 213 | echo get_class($this).' ERROR with the zip archive: '.$Msg."\r\n"; |
@@ -187,7 +219,8 @@ discard block |
||
187 | 219 | return false; |
188 | 220 | } |
189 | 221 | |
190 | - function Debug($FileHeaders=false) { |
|
222 | + function Debug($FileHeaders=false) |
|
223 | + { |
|
191 | 224 | |
192 | 225 | $this->DisplayError = true; |
193 | 226 | |
@@ -230,7 +263,8 @@ discard block |
||
230 | 263 | |
231 | 264 | } |
232 | 265 | |
233 | - function DebugArray($arr) { |
|
266 | + function DebugArray($arr) |
|
267 | + { |
|
234 | 268 | foreach ($arr as $k=>$v) { |
235 | 269 | if (is_array($v)) { |
236 | 270 | $arr[$k] = $this->DebugArray($v); |
@@ -241,11 +275,13 @@ discard block |
||
241 | 275 | return $arr; |
242 | 276 | } |
243 | 277 | |
244 | - function FileExists($NameOrIdx) { |
|
278 | + function FileExists($NameOrIdx) |
|
279 | + { |
|
245 | 280 | return ($this->FileGetIdx($NameOrIdx)!==false); |
246 | 281 | } |
247 | 282 | |
248 | - function FileGetIdx($NameOrIdx) { |
|
283 | + function FileGetIdx($NameOrIdx) |
|
284 | + { |
|
249 | 285 | // Check if a file name, or a file index exists in the Central Directory, and return its index |
250 | 286 | if (is_string($NameOrIdx)) { |
251 | 287 | if (isset($this->CdFileByName[$NameOrIdx])) { |
@@ -262,23 +298,31 @@ discard block |
||
262 | 298 | } |
263 | 299 | } |
264 | 300 | |
265 | - function FileGetIdxAdd($Name) { |
|
301 | + function FileGetIdxAdd($Name) |
|
302 | + { |
|
266 | 303 | // Check if a file name exists in the list of file to add, and return its index |
267 | - if (!is_string($Name)) return false; |
|
304 | + if (!is_string($Name)) { |
|
305 | + return false; |
|
306 | + } |
|
268 | 307 | $idx_lst = array_keys($this->AddInfo); |
269 | 308 | foreach ($idx_lst as $idx) { |
270 | - if ($this->AddInfo[$idx]['name']===$Name) return $idx; |
|
309 | + if ($this->AddInfo[$idx]['name']===$Name) { |
|
310 | + return $idx; |
|
311 | + } |
|
271 | 312 | } |
272 | 313 | return false; |
273 | 314 | } |
274 | 315 | |
275 | - function FileRead($NameOrIdx, $Uncompress=true) { |
|
316 | + function FileRead($NameOrIdx, $Uncompress=true) |
|
317 | + { |
|
276 | 318 | |
277 | 319 | $this->LastReadComp = false; // means the file is not found |
278 | 320 | $this->LastReadIdx = false; |
279 | 321 | |
280 | 322 | $idx = $this->FileGetIdx($NameOrIdx); |
281 | - if ($idx===false) return $this->RaiseError('File "'.$NameOrIdx.'" is not found in the Central Directory.'); |
|
323 | + if ($idx===false) { |
|
324 | + return $this->RaiseError('File "'.$NameOrIdx.'" is not found in the Central Directory.'); |
|
325 | + } |
|
282 | 326 | |
283 | 327 | $pos = $this->CdFileLst[$idx]['p_loc']; |
284 | 328 | $this->_MoveTo($pos); |
@@ -302,7 +346,9 @@ discard block |
||
302 | 346 | } elseif($meth==0) { |
303 | 347 | $Comp = 0; // means stored without compression |
304 | 348 | } else { |
305 | - if ($Uncompress) $this->RaiseError('Unable to uncompress file "'.$NameOrIdx.'" because it is compressed with method '.$meth.'.'); |
|
349 | + if ($Uncompress) { |
|
350 | + $this->RaiseError('Unable to uncompress file "'.$NameOrIdx.'" because it is compressed with method '.$meth.'.'); |
|
351 | + } |
|
306 | 352 | } |
307 | 353 | $this->LastReadComp = $Comp; |
308 | 354 | |
@@ -310,13 +356,16 @@ discard block |
||
310 | 356 | |
311 | 357 | } |
312 | 358 | |
313 | - function _ReadFile($idx, $ReadData) { |
|
359 | + function _ReadFile($idx, $ReadData) |
|
360 | + { |
|
314 | 361 | // read the file header (and maybe the data ) in the archive, assuming the cursor in at a new file position |
315 | 362 | |
316 | 363 | $b = $this->_ReadData(30); |
317 | 364 | |
318 | 365 | $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)."."); |
|
366 | + if ($x!=='h:04034b50') { |
|
367 | + return $this->RaiseError("Signature of Local File Header #".$idx." (data section) expected but not found at position ".$this->_TxtPos(ftell($this->ArchHnd)-30)."."); |
|
368 | + } |
|
320 | 369 | |
321 | 370 | $x = array(); |
322 | 371 | $x['vers'] = $this->_GetDec($b,4,2); |
@@ -348,7 +397,9 @@ discard block |
||
348 | 397 | } |
349 | 398 | } else { |
350 | 399 | $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."); |
|
400 | + if ($len==0) { |
|
401 | + $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."); |
|
402 | + } |
|
352 | 403 | } |
353 | 404 | |
354 | 405 | if ($ReadData) { |
@@ -389,11 +440,14 @@ discard block |
||
389 | 440 | |
390 | 441 | } |
391 | 442 | |
392 | - function FileReplace($NameOrIdx, $Data, $DataType=self::TBSZIP_STRING, $Compress=true) { |
|
443 | + function FileReplace($NameOrIdx, $Data, $DataType=self::TBSZIP_STRING, $Compress=true) |
|
444 | + { |
|
393 | 445 | // Store replacement information. |
394 | 446 | |
395 | 447 | $idx = $this->FileGetIdx($NameOrIdx); |
396 | - if ($idx===false) return $this->RaiseError('File "'.$NameOrIdx.'" is not found in the Central Directory.'); |
|
448 | + if ($idx===false) { |
|
449 | + return $this->RaiseError('File "'.$NameOrIdx.'" is not found in the Central Directory.'); |
|
450 | + } |
|
397 | 451 | |
398 | 452 | $pos = $this->CdFileLst[$idx]['p_loc']; |
399 | 453 | |
@@ -405,7 +459,9 @@ discard block |
||
405 | 459 | // file to replace |
406 | 460 | $Diff = - $this->CdFileLst[$idx]['l_data_c']; |
407 | 461 | $Ref = $this->_DataCreateNewRef($Data, $DataType, $Compress, $Diff, $NameOrIdx); |
408 | - if ($Ref===false) return false; |
|
462 | + if ($Ref===false) { |
|
463 | + return false; |
|
464 | + } |
|
409 | 465 | $this->ReplInfo[$idx] = $Ref; |
410 | 466 | $Result = $Ref['res']; |
411 | 467 | } |
@@ -420,7 +476,8 @@ discard block |
||
420 | 476 | * Return the state of the file. |
421 | 477 | * @return {string} 'u'=unchanged, 'm'=modified, 'd'=deleted, 'a'=added, false=unknown |
422 | 478 | */ |
423 | - function FileGetState($NameOrIdx) { |
|
479 | + function FileGetState($NameOrIdx) |
|
480 | + { |
|
424 | 481 | |
425 | 482 | $idx = $this->FileGetIdx($NameOrIdx); |
426 | 483 | if ($idx===false) { |
@@ -442,7 +499,8 @@ discard block |
||
442 | 499 | |
443 | 500 | } |
444 | 501 | |
445 | - function FileCancelModif($NameOrIdx, $ReplacedAndDeleted=true) { |
|
502 | + function FileCancelModif($NameOrIdx, $ReplacedAndDeleted=true) |
|
503 | + { |
|
446 | 504 | // cancel added, modified or deleted modifications on a file in the archive |
447 | 505 | // return the number of cancels |
448 | 506 | |
@@ -472,7 +530,8 @@ discard block |
||
472 | 530 | |
473 | 531 | } |
474 | 532 | |
475 | - function Flush($Render=self::TBSZIP_DOWNLOAD, $File='', $ContentType='') { |
|
533 | + function Flush($Render=self::TBSZIP_DOWNLOAD, $File='', $ContentType='') |
|
534 | + { |
|
476 | 535 | |
477 | 536 | if ( ($File!=='') && ($this->ArchFile===$File) && ($Render==self::TBSZIP_FILE) ) { |
478 | 537 | $this->RaiseError('Method Flush() cannot overwrite the current opened archive: \''.$File.'\''); // this makes corrupted zip archives without PHP error. |
@@ -489,7 +548,9 @@ discard block |
||
489 | 548 | $date = $this->_MsDos_Date($now); |
490 | 549 | $time = $this->_MsDos_Time($now); |
491 | 550 | |
492 | - if (!$this->OutputOpen($Render, $File, $ContentType)) return false; |
|
551 | + if (!$this->OutputOpen($Render, $File, $ContentType)) { |
|
552 | + return false; |
|
553 | + } |
|
493 | 554 | |
494 | 555 | // output modified zipped files and unmodified zipped files that are beetween them |
495 | 556 | ksort($this->ReplByPos); |
@@ -497,7 +558,9 @@ discard block |
||
497 | 558 | // output data from the zip archive which is before the data to replace |
498 | 559 | $this->OutputFromArch($ArchPos, $ReplPos); |
499 | 560 | // get current file information |
500 | - if (!isset($this->VisFileLst[$ReplIdx])) $this->_ReadFile($ReplIdx, false); |
|
561 | + if (!isset($this->VisFileLst[$ReplIdx])) { |
|
562 | + $this->_ReadFile($ReplIdx, false); |
|
563 | + } |
|
501 | 564 | $FileInfo =& $this->VisFileLst[$ReplIdx]; |
502 | 565 | $b1 = $FileInfo['bin']; |
503 | 566 | if (isset($FileInfo['desc_bin'])) { |
@@ -520,7 +583,10 @@ discard block |
||
520 | 583 | $this->_PutDec($b1, $ReplInfo['crc32'], 14, 4); // crc32 |
521 | 584 | $this->_PutDec($b1, $ReplInfo['len_c'], 18, 4); // l_data_c |
522 | 585 | $this->_PutDec($b1, $ReplInfo['len_u'], 22, 4); // l_data_u |
523 | - if ($ReplInfo['meth']!==false) $this->_PutDec($b1, $ReplInfo['meth'], 8, 2); // meth |
|
586 | + if ($ReplInfo['meth']!==false) { |
|
587 | + $this->_PutDec($b1, $ReplInfo['meth'], 8, 2); |
|
588 | + } |
|
589 | + // meth |
|
524 | 590 | // prepare the bottom description if the zipped file, if any |
525 | 591 | if ($b2!=='') { |
526 | 592 | $d = (strlen($b2)==16) ? 4 : 0; // offset because of the signature if any |
@@ -544,7 +610,10 @@ discard block |
||
544 | 610 | } |
545 | 611 | |
546 | 612 | // 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 |
|
613 | + if ($this->ArchHnd!==false) { |
|
614 | + $this->OutputFromArch($ArchPos, $this->CdPos); |
|
615 | + } |
|
616 | + // ArchHnd is false if CreateNew() has been called |
|
548 | 617 | $ArchPos = $this->CdPos; |
549 | 618 | |
550 | 619 | // Output file to add |
@@ -567,7 +636,10 @@ discard block |
||
567 | 636 | $b1 = $this->CdFileLst[$i]['bin']; |
568 | 637 | $old_cd_len += strlen($b1); |
569 | 638 | if (!isset($DelLst[$i])) { |
570 | - if (isset($FicNewPos[$i])) $this->_PutDec($b1, $FicNewPos[$i], 42, 4); // p_loc |
|
639 | + if (isset($FicNewPos[$i])) { |
|
640 | + $this->_PutDec($b1, $FicNewPos[$i], 42, 4); |
|
641 | + } |
|
642 | + // p_loc |
|
571 | 643 | if (isset($this->ReplInfo[$i])) { |
572 | 644 | $ReplInfo =& $this->ReplInfo[$i]; |
573 | 645 | $this->_PutDec($b1, $time, 12, 2); // time |
@@ -575,7 +647,10 @@ discard block |
||
575 | 647 | $this->_PutDec($b1, $ReplInfo['crc32'], 16, 4); // crc32 |
576 | 648 | $this->_PutDec($b1, $ReplInfo['len_c'], 20, 4); // l_data_c |
577 | 649 | $this->_PutDec($b1, $ReplInfo['len_u'], 24, 4); // l_data_u |
578 | - if ($ReplInfo['meth']!==false) $this->_PutDec($b1, $ReplInfo['meth'], 10, 2); // meth |
|
650 | + if ($ReplInfo['meth']!==false) { |
|
651 | + $this->_PutDec($b1, $ReplInfo['meth'], 10, 2); |
|
652 | + } |
|
653 | + // meth |
|
579 | 654 | } |
580 | 655 | $b2 .= $b1; |
581 | 656 | } |
@@ -585,7 +660,10 @@ discard block |
||
585 | 660 | $DeltaCdLen = $DeltaCdLen + strlen($b2) - $old_cd_len; |
586 | 661 | |
587 | 662 | // 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 |
|
663 | + if ($this->ArchHnd!==false) { |
|
664 | + $this->OutputFromArch($ArchPos, $this->CdEndPos); |
|
665 | + } |
|
666 | + // ArchHnd is false if CreateNew() has been called |
|
589 | 667 | |
590 | 668 | // Output file information of the Central Directory for added files |
591 | 669 | if ($AddNbr>0) { |
@@ -625,11 +703,14 @@ discard block |
||
625 | 703 | // output functions |
626 | 704 | // ---------------- |
627 | 705 | |
628 | - function OutputOpen($Render, $File, $ContentType) { |
|
706 | + function OutputOpen($Render, $File, $ContentType) |
|
707 | + { |
|
629 | 708 | |
630 | 709 | if (($Render & self::TBSZIP_FILE)==self::TBSZIP_FILE) { |
631 | 710 | $this->OutputMode = self::TBSZIP_FILE; |
632 | - if (''.$File=='') $File = basename($this->ArchFile).'.zip'; |
|
711 | + if (''.$File=='') { |
|
712 | + $File = basename($this->ArchFile).'.zip'; |
|
713 | + } |
|
633 | 714 | $this->OutputHandle = @fopen($File, 'w'); |
634 | 715 | if ($this->OutputHandle===false) { |
635 | 716 | 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 +721,15 @@ discard block |
||
640 | 721 | } elseif (($Render & self::TBSZIP_DOWNLOAD)==self::TBSZIP_DOWNLOAD) { |
641 | 722 | $this->OutputMode = self::TBSZIP_DOWNLOAD; |
642 | 723 | // Output the file |
643 | - if (''.$File=='') $File = basename($this->ArchFile); |
|
724 | + if (''.$File=='') { |
|
725 | + $File = basename($this->ArchFile); |
|
726 | + } |
|
644 | 727 | if (($Render & self::TBSZIP_NOHEADER)==self::TBSZIP_NOHEADER) { |
645 | 728 | } else { |
646 | 729 | header ('Pragma: no-cache'); |
647 | - if ($ContentType!='') header ('Content-Type: '.$ContentType); |
|
730 | + if ($ContentType!='') { |
|
731 | + header ('Content-Type: '.$ContentType); |
|
732 | + } |
|
648 | 733 | header('Content-Disposition: attachment; filename="'.$File.'"'); |
649 | 734 | header('Expires: 0'); |
650 | 735 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
@@ -652,7 +737,9 @@ discard block |
||
652 | 737 | header('Content-Description: File Transfer'); |
653 | 738 | header('Content-Transfer-Encoding: binary'); |
654 | 739 | $Len = $this->_EstimateNewArchSize(); |
655 | - if ($Len!==false) header('Content-Length: '.$Len); |
|
740 | + if ($Len!==false) { |
|
741 | + header('Content-Length: '.$Len); |
|
742 | + } |
|
656 | 743 | } |
657 | 744 | } else { |
658 | 745 | return $this->RaiseError('Method Flush is called with a unsupported render option.'); |
@@ -662,9 +749,12 @@ discard block |
||
662 | 749 | |
663 | 750 | } |
664 | 751 | |
665 | - function OutputFromArch($pos, $pos_stop) { |
|
752 | + function OutputFromArch($pos, $pos_stop) |
|
753 | + { |
|
666 | 754 | $len = $pos_stop - $pos; |
667 | - if ($len<0) return; |
|
755 | + if ($len<0) { |
|
756 | + return; |
|
757 | + } |
|
668 | 758 | $this->_MoveTo($pos); |
669 | 759 | $block = 1024; |
670 | 760 | while ($len>0) { |
@@ -676,7 +766,8 @@ discard block |
||
676 | 766 | unset($x); |
677 | 767 | } |
678 | 768 | |
679 | - function OutputFromString($data) { |
|
769 | + function OutputFromString($data) |
|
770 | + { |
|
680 | 771 | if ($this->OutputMode===self::TBSZIP_DOWNLOAD) { |
681 | 772 | echo $data; // donwload |
682 | 773 | } elseif ($this->OutputMode===self::TBSZIP_STRING) { |
@@ -686,7 +777,8 @@ discard block |
||
686 | 777 | } |
687 | 778 | } |
688 | 779 | |
689 | - function OutputClose() { |
|
780 | + function OutputClose() |
|
781 | + { |
|
690 | 782 | if ( ($this->OutputMode===self::TBSZIP_FILE) && ($this->OutputHandle!==false) ) { |
691 | 783 | fclose($this->OutputHandle); |
692 | 784 | $this->OutputHandle = false; |
@@ -697,11 +789,13 @@ discard block |
||
697 | 789 | // Reading functions |
698 | 790 | // ---------------- |
699 | 791 | |
700 | - function _MoveTo($pos, $relative = SEEK_SET) { |
|
792 | + function _MoveTo($pos, $relative = SEEK_SET) |
|
793 | + { |
|
701 | 794 | fseek($this->ArchHnd, $pos, $relative); |
702 | 795 | } |
703 | 796 | |
704 | - function _ReadData($len) { |
|
797 | + function _ReadData($len) |
|
798 | + { |
|
705 | 799 | if ($len>0) { |
706 | 800 | $x = fread($this->ArchHnd, $len); |
707 | 801 | return $x; |
@@ -714,22 +808,27 @@ discard block |
||
714 | 808 | // Take info from binary data |
715 | 809 | // ---------------- |
716 | 810 | |
717 | - function _GetDec($txt, $pos, $len) { |
|
811 | + function _GetDec($txt, $pos, $len) |
|
812 | + { |
|
718 | 813 | $x = substr($txt, $pos, $len); |
719 | 814 | $z = 0; |
720 | 815 | for ($i=0;$i<$len;$i++) { |
721 | 816 | $asc = ord($x[$i]); |
722 | - if ($asc>0) $z = $z + $asc*pow(256,$i); |
|
817 | + if ($asc>0) { |
|
818 | + $z = $z + $asc*pow(256,$i); |
|
819 | + } |
|
723 | 820 | } |
724 | 821 | return $z; |
725 | 822 | } |
726 | 823 | |
727 | - function _GetHex($txt, $pos, $len) { |
|
824 | + function _GetHex($txt, $pos, $len) |
|
825 | + { |
|
728 | 826 | $x = substr($txt, $pos, $len); |
729 | 827 | return 'h:'.bin2hex(strrev($x)); |
730 | 828 | } |
731 | 829 | |
732 | - function _GetBin($txt, $pos, $len) { |
|
830 | + function _GetBin($txt, $pos, $len) |
|
831 | + { |
|
733 | 832 | $x = substr($txt, $pos, $len); |
734 | 833 | $z = ''; |
735 | 834 | for ($i=0;$i<$len;$i++) { |
@@ -749,14 +848,16 @@ discard block |
||
749 | 848 | // Put info into binary data |
750 | 849 | // ---------------- |
751 | 850 | |
752 | - function _PutDec(&$txt, $val, $pos, $len) { |
|
851 | + function _PutDec(&$txt, $val, $pos, $len) |
|
852 | + { |
|
753 | 853 | $x = ''; |
754 | 854 | for ($i=0;$i<$len;$i++) { |
755 | 855 | if ($val==0) { |
756 | 856 | $z = 0; |
757 | 857 | } else { |
758 | 858 | $z = intval($val % 256); |
759 | - if (($val<0) && ($z!=0)) { // ($z!=0) is very important, example: val=-420085702 |
|
859 | + if (($val<0) && ($z!=0)) { |
|
860 | +// ($z!=0) is very important, example: val=-420085702 |
|
760 | 861 | // 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 | 862 | $val = ($val - $z)/256 -1; |
762 | 863 | $z = 256 + $z; |
@@ -769,18 +870,21 @@ discard block |
||
769 | 870 | $txt = substr_replace($txt, $x, $pos, $len); |
770 | 871 | } |
771 | 872 | |
772 | - function _MsDos_Date($Timestamp = false) { |
|
873 | + function _MsDos_Date($Timestamp = false) |
|
874 | + { |
|
773 | 875 | // convert a date-time timstamp into the MS-Dos format |
774 | 876 | $d = ($Timestamp===false) ? getdate() : getdate($Timestamp); |
775 | 877 | return (($d['year']-1980)*512) + ($d['mon']*32) + $d['mday']; |
776 | 878 | } |
777 | - function _MsDos_Time($Timestamp = false) { |
|
879 | + function _MsDos_Time($Timestamp = false) |
|
880 | + { |
|
778 | 881 | // convert a date-time timstamp into the MS-Dos format |
779 | 882 | $d = ($Timestamp===false) ? getdate() : getdate($Timestamp); |
780 | 883 | 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 | 884 | } |
782 | 885 | |
783 | - function _MsDos_Debug($date, $time) { |
|
886 | + function _MsDos_Debug($date, $time) |
|
887 | + { |
|
784 | 888 | // Display the formated date and time. Just for debug purpose. |
785 | 889 | // date end time are encoded on 16 bits (2 bytes) : date = yyyyyyymmmmddddd , time = hhhhhnnnnnssssss |
786 | 890 | $y = ($date & 65024)/512 + 1980; |
@@ -792,7 +896,8 @@ discard block |
||
792 | 896 | 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 | 897 | } |
794 | 898 | |
795 | - function _TxtPos($pos) { |
|
899 | + function _TxtPos($pos) |
|
900 | + { |
|
796 | 901 | // Return the human readable position in both decimal and hexa |
797 | 902 | return $pos." (h:".dechex($pos).")"; |
798 | 903 | } |
@@ -803,7 +908,8 @@ discard block |
||
803 | 908 | * Return false if the record is not found. The comment cannot exceed 65335 bytes (=FFFF). |
804 | 909 | * The method is read backwards a block of 256 bytes and search the key in this block. |
805 | 910 | */ |
806 | - function _FindCDEnd($cd_info) { |
|
911 | + function _FindCDEnd($cd_info) |
|
912 | + { |
|
807 | 913 | $nbr = 1; |
808 | 914 | $p = false; |
809 | 915 | $pos = ftell($this->ArchHnd) - 4 - 256; |
@@ -825,7 +931,8 @@ discard block |
||
825 | 931 | return false; |
826 | 932 | } |
827 | 933 | |
828 | - function _DataOuputAddedFile($Idx, $PosLoc) { |
|
934 | + function _DataOuputAddedFile($Idx, $PosLoc) |
|
935 | + { |
|
829 | 936 | |
830 | 937 | $Ref =& $this->AddInfo[$Idx]; |
831 | 938 | $this->_DataPrepare($Ref); // get data from external file if necessary |
@@ -885,7 +992,8 @@ discard block |
||
885 | 992 | |
886 | 993 | } |
887 | 994 | |
888 | - function _DataCreateNewRef($Data, $DataType, $Compress, $Diff, $NameOrIdx) { |
|
995 | + function _DataCreateNewRef($Data, $DataType, $Compress, $Diff, $NameOrIdx) |
|
996 | + { |
|
889 | 997 | |
890 | 998 | $file_time = false; |
891 | 999 | if (is_array($Compress)) { |
@@ -929,7 +1037,9 @@ discard block |
||
929 | 1037 | $fi = stat($path); |
930 | 1038 | if ($fi !== false) { |
931 | 1039 | $fz = $fi['size']; |
932 | - if ($len_u===false) $len_u = $fz; |
|
1040 | + if ($len_u===false) { |
|
1041 | + $len_u = $fz; |
|
1042 | + } |
|
933 | 1043 | $len_c = ($Compress) ? false : $fz; |
934 | 1044 | $file_time = $fi['mtime']; |
935 | 1045 | } else { |
@@ -943,12 +1053,15 @@ discard block |
||
943 | 1053 | |
944 | 1054 | } |
945 | 1055 | |
946 | - function _DataPrepare(&$Ref) { |
|
1056 | + function _DataPrepare(&$Ref) |
|
1057 | + { |
|
947 | 1058 | // returns the real size of data |
948 | 1059 | // TODO: support for data returned from okapi cache |
949 | 1060 | if ($Ref['path']!==false) { |
950 | 1061 | $Ref['data'] = file_get_contents($Ref['path']); |
951 | - if ($Ref['crc32']===false) $Ref['crc32'] = crc32($Ref['data']); |
|
1062 | + if ($Ref['crc32']===false) { |
|
1063 | + $Ref['crc32'] = crc32($Ref['data']); |
|
1064 | + } |
|
952 | 1065 | if ($Ref['len_c']===false) { |
953 | 1066 | // means the data must be compressed |
954 | 1067 | $Ref['data'] = gzdeflate($Ref['data']); |
@@ -959,7 +1072,8 @@ discard block |
||
959 | 1072 | /** |
960 | 1073 | * 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 | 1074 | */ |
962 | - function _EstimateNewArchSize($Optim=true) { |
|
1075 | + function _EstimateNewArchSize($Optim=true) |
|
1076 | + { |
|
963 | 1077 | |
964 | 1078 | if ($this->ArchIsNew) { |
965 | 1079 | $Len = strlen($this->CdInfo['bin']); |
@@ -977,13 +1091,18 @@ discard block |
||
977 | 1091 | // file to delete |
978 | 1092 | $Info =& $this->CdFileLst[$i]; |
979 | 1093 | if (!isset($this->VisFileLst[$i])) { |
980 | - if ($Optim) return false; // if $Optimization is set to true, then we d'ont rewind to read information |
|
1094 | + if ($Optim) { |
|
1095 | + return false; |
|
1096 | + } |
|
1097 | + // if $Optimization is set to true, then we d'ont rewind to read information |
|
981 | 1098 | $this->_MoveTo($Info['p_loc']); |
982 | 1099 | $this->_ReadFile($i, false); |
983 | 1100 | } |
984 | 1101 | $Vis =& $this->VisFileLst[$i]; |
985 | 1102 | $Len += -strlen($Vis['bin']) -strlen($Info['bin']) - $Info['l_data_c']; |
986 | - if (isset($Vis['desc_bin'])) $Len += -strlen($Vis['desc_bin']); |
|
1103 | + if (isset($Vis['desc_bin'])) { |
|
1104 | + $Len += -strlen($Vis['desc_bin']); |
|
1105 | + } |
|
987 | 1106 | } elseif ($Ref['len_c']===false) { |
988 | 1107 | return false; // information not yet known |
989 | 1108 | } 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; |
@@ -202,9 +202,9 @@ discard block |
||
202 | 202 | * would respond to request_token and access_token calls with |
203 | 203 | */ |
204 | 204 | function to_string() { |
205 | - return "oauth_token=" . |
|
206 | - OAuthUtil::urlencode_rfc3986($this->key) . |
|
207 | - "&oauth_token_secret=" . |
|
205 | + return "oauth_token=". |
|
206 | + OAuthUtil::urlencode_rfc3986($this->key). |
|
207 | + "&oauth_token_secret=". |
|
208 | 208 | OAuthUtil::urlencode_rfc3986($this->secret); |
209 | 209 | } |
210 | 210 | |
@@ -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,14 +415,14 @@ 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'; |
422 | - $http_url = ($http_url) ? $http_url : $scheme . |
|
423 | - '://' . $_SERVER['SERVER_NAME'] . |
|
424 | - ':' . |
|
425 | - $_SERVER['SERVER_PORT'] . |
|
422 | + $http_url = ($http_url) ? $http_url : $scheme. |
|
423 | + '://'.$_SERVER['SERVER_NAME']. |
|
424 | + ':'. |
|
425 | + $_SERVER['SERVER_PORT']. |
|
426 | 426 | $_SERVER['REQUEST_URI']; |
427 | 427 | $http_method = ($http_method) ? $http_method : $_SERVER['REQUEST_METHOD']; |
428 | 428 | |
@@ -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(), |
@@ -592,10 +592,10 @@ 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) { |
|
598 | - $out = 'Authorization: OAuth realm="' . OAuthUtil::urlencode_rfc3986($realm) . '"'; |
|
597 | + if ($realm) { |
|
598 | + $out = 'Authorization: OAuth realm="'.OAuthUtil::urlencode_rfc3986($realm).'"'; |
|
599 | 599 | $first = false; |
600 | 600 | } else |
601 | 601 | $out = 'Authorization: OAuth'; |
@@ -607,9 +607,9 @@ discard block |
||
607 | 607 | throw new OAuthClientException('Arrays not supported in headers.'); |
608 | 608 | } |
609 | 609 | $out .= ($first) ? ' ' : ','; |
610 | - $out .= OAuthUtil::urlencode_rfc3986($k) . |
|
611 | - '="' . |
|
612 | - OAuthUtil::urlencode_rfc3986($v) . |
|
610 | + $out .= OAuthUtil::urlencode_rfc3986($k). |
|
611 | + '="'. |
|
612 | + OAuthUtil::urlencode_rfc3986($v). |
|
613 | 613 | '"'; |
614 | 614 | $first = false; |
615 | 615 | } |
@@ -650,13 +650,13 @@ discard block |
||
650 | 650 | $mt = microtime(); |
651 | 651 | $rand = mt_rand(); |
652 | 652 | |
653 | - return md5($mt . $rand); // md5s look nicer than numbers |
|
653 | + return md5($mt.$rand); // md5s look nicer than numbers |
|
654 | 654 | } |
655 | 655 | } |
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; |
@@ -759,9 +759,9 @@ discard block |
||
759 | 759 | if (!in_array($signature_method, |
760 | 760 | array_keys($this->signature_methods))) { |
761 | 761 | throw new OAuthUnsupportedSignatureMethodException( |
762 | - "Signature method '$signature_method' not supported " . |
|
763 | - "try one of the following: " . |
|
764 | - implode(", ", array_keys($this->signature_methods)) . "." |
|
762 | + "Signature method '$signature_method' not supported ". |
|
763 | + "try one of the following: ". |
|
764 | + implode(", ", array_keys($this->signature_methods))."." |
|
765 | 765 | ); |
766 | 766 | } |
767 | 767 | return $this->signature_methods[$signature_method]; |
@@ -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 |
@@ -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); |
@@ -1041,10 +1041,10 @@ discard block |
||
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 | 1046 | } else { |
1047 | - $pairs[] = $parameter . '=' . $value; |
|
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) |
@@ -47,19 +47,26 @@ discard block |
||
47 | 47 | */ |
48 | 48 | |
49 | 49 | /** Base exception type for all exceptions thrown by this module. */ |
50 | -abstract class OAuthException extends Exception {} |
|
50 | +abstract class OAuthException extends Exception |
|
51 | +{ |
|
52 | +} |
|
51 | 53 | |
52 | 54 | # All OAuthExceptions fall back into these two categories: |
53 | 55 | |
54 | 56 | /** OAuth client errors. */ |
55 | -class OAuthClientException extends OAuthException {} |
|
57 | +class OAuthClientException extends OAuthException |
|
58 | +{ |
|
59 | +} |
|
56 | 60 | /** OAuth server errors. */ |
57 | -abstract class OAuthServerException extends OAuthException { |
|
61 | +abstract class OAuthServerException extends OAuthException |
|
62 | +{ |
|
58 | 63 | abstract public function getHttpStatusCode(); |
59 | - protected function provideExtras(&$extras) { |
|
64 | + protected function provideExtras(&$extras) |
|
65 | + { |
|
60 | 66 | $extras['reason_stack'][] = 'invalid_oauth_request'; |
61 | 67 | } |
62 | - public function getOkapiJSON() { |
|
68 | + public function getOkapiJSON() |
|
69 | + { |
|
63 | 70 | $extras = array( |
64 | 71 | 'developer_message' => $this->getMessage(), |
65 | 72 | 'reason_stack' => array(), |
@@ -73,77 +80,102 @@ discard block |
||
73 | 80 | # More subclasses of server exceptions. |
74 | 81 | |
75 | 82 | /** OAuth server errors which should result in HTTP 400 response. */ |
76 | -abstract class OAuthServer400Exception extends OAuthServerException { |
|
77 | - protected function provideExtras(&$extras) { |
|
83 | +abstract class OAuthServer400Exception extends OAuthServerException |
|
84 | +{ |
|
85 | + protected function provideExtras(&$extras) |
|
86 | + { |
|
78 | 87 | parent::provideExtras($extras); |
79 | 88 | $extras['status'] = 400; |
80 | 89 | } |
81 | - public function getHttpStatusCode() { return 400; } |
|
90 | + public function getHttpStatusCode() |
|
91 | + { |
|
92 | +return 400; } |
|
82 | 93 | } |
83 | 94 | /** OAuth server errors which should result in HTTP 401 response. */ |
84 | -abstract class OAuthServer401Exception extends OAuthServerException { |
|
85 | - protected function provideExtras(&$extras) { |
|
95 | +abstract class OAuthServer401Exception extends OAuthServerException |
|
96 | +{ |
|
97 | + protected function provideExtras(&$extras) |
|
98 | + { |
|
86 | 99 | parent::provideExtras($extras); |
87 | 100 | $extras['status'] = 401; |
88 | 101 | } |
89 | - public function getHttpStatusCode() { return 401; } |
|
102 | + public function getHttpStatusCode() |
|
103 | + { |
|
104 | +return 401; } |
|
90 | 105 | } |
91 | 106 | |
92 | 107 | /** Client asked for an unsupported OAuth version (not 1.0). */ |
93 | -class OAuthVersionNotSupportedException extends OAuthServer400Exception { |
|
94 | - protected function provideExtras(&$extras) { |
|
108 | +class OAuthVersionNotSupportedException extends OAuthServer400Exception |
|
109 | +{ |
|
110 | + protected function provideExtras(&$extras) |
|
111 | + { |
|
95 | 112 | parent::provideExtras($extras); |
96 | 113 | $extras['reason_stack'][] = 'unsupported_oauth_version'; |
97 | 114 | } |
98 | 115 | } |
99 | 116 | /** Client didn't provide one of the key OAuth parameters. */ |
100 | -class OAuthMissingParameterException extends OAuthServer400Exception { |
|
117 | +class OAuthMissingParameterException extends OAuthServer400Exception |
|
118 | +{ |
|
101 | 119 | protected $param_name; |
102 | - protected function provideExtras(&$extras) { |
|
120 | + protected function provideExtras(&$extras) |
|
121 | + { |
|
103 | 122 | parent::provideExtras($extras); |
104 | 123 | $extras['reason_stack'][] = 'missing_parameter'; |
105 | 124 | $extras['parameter'] = $this->param_name; |
106 | 125 | } |
107 | - public function __construct($param_name) { |
|
126 | + public function __construct($param_name) |
|
127 | + { |
|
108 | 128 | parent::__construct("Missing '$param_name' parameter. This parameter is required."); |
109 | 129 | $this->param_name = $param_name; |
110 | 130 | } |
111 | - public function getParamName() { return $this->param_name; } |
|
131 | + public function getParamName() |
|
132 | + { |
|
133 | +return $this->param_name; } |
|
112 | 134 | } |
113 | 135 | /** Client used unsupported signature method. */ |
114 | -class OAuthUnsupportedSignatureMethodException extends OAuthServer400Exception { |
|
115 | - protected function provideExtras(&$extras) { |
|
136 | +class OAuthUnsupportedSignatureMethodException extends OAuthServer400Exception |
|
137 | +{ |
|
138 | + protected function provideExtras(&$extras) |
|
139 | + { |
|
116 | 140 | parent::provideExtras($extras); |
117 | 141 | $extras['reason_stack'][] = 'unsupported_signature_method'; |
118 | 142 | } |
119 | 143 | } |
120 | 144 | /** Client provided invalid Consumer Key. */ |
121 | -class OAuthInvalidConsumerException extends OAuthServer401Exception { |
|
122 | - protected function provideExtras(&$extras) { |
|
145 | +class OAuthInvalidConsumerException extends OAuthServer401Exception |
|
146 | +{ |
|
147 | + protected function provideExtras(&$extras) |
|
148 | + { |
|
123 | 149 | parent::provideExtras($extras); |
124 | 150 | $extras['reason_stack'][] = 'invalid_consumer'; |
125 | 151 | } |
126 | 152 | } |
127 | 153 | /** Client provider invalid token (either Request Token or Access Token). */ |
128 | -class OAuthInvalidTokenException extends OAuthServer401Exception { |
|
129 | - protected function provideExtras(&$extras) { |
|
154 | +class OAuthInvalidTokenException extends OAuthServer401Exception |
|
155 | +{ |
|
156 | + protected function provideExtras(&$extras) |
|
157 | + { |
|
130 | 158 | parent::provideExtras($extras); |
131 | 159 | $extras['reason_stack'][] = 'invalid_token'; |
132 | 160 | } |
133 | 161 | } |
134 | 162 | /** Client's signature was invalid. */ |
135 | -class OAuthInvalidSignatureException extends OAuthServer401Exception { |
|
136 | - protected function provideExtras(&$extras) { |
|
163 | +class OAuthInvalidSignatureException extends OAuthServer401Exception |
|
164 | +{ |
|
165 | + protected function provideExtras(&$extras) |
|
166 | + { |
|
137 | 167 | parent::provideExtras($extras); |
138 | 168 | $extras['reason_stack'][] = 'invalid_signature'; |
139 | 169 | } |
140 | 170 | } |
141 | 171 | /** Client used expired timestamp (or timestamp too far in future). */ |
142 | -class OAuthExpiredTimestampException extends OAuthServer400Exception { |
|
172 | +class OAuthExpiredTimestampException extends OAuthServer400Exception |
|
173 | +{ |
|
143 | 174 | protected $usersTimestamp; |
144 | 175 | protected $ourTimestamp; |
145 | 176 | protected $threshold; |
146 | - protected function provideExtras(&$extras) { |
|
177 | + protected function provideExtras(&$extras) |
|
178 | + { |
|
147 | 179 | parent::provideExtras($extras); |
148 | 180 | $extras['reason_stack'][] = 'invalid_timestamp'; |
149 | 181 | $extras['yours'] = $this->usersTimestamp; |
@@ -151,39 +183,50 @@ discard block |
||
151 | 183 | $extras['difference'] = $this->ourTimestamp - $this->usersTimestamp; |
152 | 184 | $extras['threshold'] = $this->threshold; |
153 | 185 | } |
154 | - public function __construct($users, $ours, $threshold) { |
|
186 | + public function __construct($users, $ours, $threshold) |
|
187 | + { |
|
155 | 188 | $this->usersTimestamp = $users; |
156 | 189 | $this->ourTimestamp = $ours; |
157 | 190 | $this->threshold = $threshold; |
158 | 191 | parent::__construct("Expired timestamp, yours $this->usersTimestamp, ours $this->ourTimestamp (threshold $this->threshold)."); |
159 | 192 | } |
160 | - public function getUsersTimestamp() { return $this->usersTimestamp; } |
|
161 | - public function getOurTimestamp() { return $this->ourTimestamp; } |
|
193 | + public function getUsersTimestamp() |
|
194 | + { |
|
195 | +return $this->usersTimestamp; } |
|
196 | + public function getOurTimestamp() |
|
197 | + { |
|
198 | +return $this->ourTimestamp; } |
|
162 | 199 | } |
163 | 200 | /** Client used the same nonce for the second time. */ |
164 | -class OAuthNonceAlreadyUsedException extends OAuthServer400Exception { |
|
165 | - protected function provideExtras(&$extras) { |
|
201 | +class OAuthNonceAlreadyUsedException extends OAuthServer400Exception |
|
202 | +{ |
|
203 | + protected function provideExtras(&$extras) |
|
204 | + { |
|
166 | 205 | parent::provideExtras($extras); |
167 | 206 | $extras['reason_stack'][] = 'nonce_already_used'; |
168 | 207 | } |
169 | 208 | } |
170 | 209 | |
171 | -class OAuthConsumer { |
|
210 | +class OAuthConsumer |
|
211 | +{ |
|
172 | 212 | public $key; |
173 | 213 | public $secret; |
174 | 214 | |
175 | - function __construct($key, $secret, $callback_url=NULL) { |
|
215 | + function __construct($key, $secret, $callback_url=NULL) |
|
216 | + { |
|
176 | 217 | $this->key = $key; |
177 | 218 | $this->secret = $secret; |
178 | 219 | $this->callback_url = $callback_url; |
179 | 220 | } |
180 | 221 | |
181 | - function __toString() { |
|
222 | + function __toString() |
|
223 | + { |
|
182 | 224 | return "OAuthConsumer[key=$this->key,secret=$this->secret]"; |
183 | 225 | } |
184 | 226 | } |
185 | 227 | |
186 | -class OAuthToken { |
|
228 | +class OAuthToken |
|
229 | +{ |
|
187 | 230 | // access tokens and request tokens |
188 | 231 | public $key; |
189 | 232 | public $secret; |
@@ -192,7 +235,8 @@ discard block |
||
192 | 235 | * key = the token |
193 | 236 | * secret = the token secret |
194 | 237 | */ |
195 | - function __construct($key, $secret) { |
|
238 | + function __construct($key, $secret) |
|
239 | + { |
|
196 | 240 | $this->key = $key; |
197 | 241 | $this->secret = $secret; |
198 | 242 | } |
@@ -201,14 +245,16 @@ discard block |
||
201 | 245 | * generates the basic string serialization of a token that a server |
202 | 246 | * would respond to request_token and access_token calls with |
203 | 247 | */ |
204 | - function to_string() { |
|
248 | + function to_string() |
|
249 | + { |
|
205 | 250 | return "oauth_token=" . |
206 | 251 | OAuthUtil::urlencode_rfc3986($this->key) . |
207 | 252 | "&oauth_token_secret=" . |
208 | 253 | OAuthUtil::urlencode_rfc3986($this->secret); |
209 | 254 | } |
210 | 255 | |
211 | - function __toString() { |
|
256 | + function __toString() |
|
257 | + { |
|
212 | 258 | return $this->to_string(); |
213 | 259 | } |
214 | 260 | } |
@@ -217,7 +263,8 @@ discard block |
||
217 | 263 | * A class for implementing a Signature Method |
218 | 264 | * See section 9 ("Signing Requests") in the spec |
219 | 265 | */ |
220 | -abstract class OAuthSignatureMethod { |
|
266 | +abstract class OAuthSignatureMethod |
|
267 | +{ |
|
221 | 268 | /** |
222 | 269 | * Needs to return the name of the Signature Method (ie HMAC-SHA1) |
223 | 270 | * @return string |
@@ -244,7 +291,8 @@ discard block |
||
244 | 291 | * @param string $signature |
245 | 292 | * @return bool |
246 | 293 | */ |
247 | - public function check_signature($request, $consumer, $token, $signature) { |
|
294 | + public function check_signature($request, $consumer, $token, $signature) |
|
295 | + { |
|
248 | 296 | $built = $this->build_signature($request, $consumer, $token); |
249 | 297 | |
250 | 298 | // Check for zero length, although unlikely here |
@@ -273,12 +321,15 @@ discard block |
||
273 | 321 | * character (ASCII code 38) even if empty. |
274 | 322 | * - Chapter 9.2 ("HMAC-SHA1") |
275 | 323 | */ |
276 | -class OAuthSignatureMethod_HMAC_SHA1 extends OAuthSignatureMethod { |
|
277 | - function get_name() { |
|
324 | +class OAuthSignatureMethod_HMAC_SHA1 extends OAuthSignatureMethod |
|
325 | +{ |
|
326 | + function get_name() |
|
327 | + { |
|
278 | 328 | return "HMAC-SHA1"; |
279 | 329 | } |
280 | 330 | |
281 | - public function build_signature($request, $consumer, $token) { |
|
331 | + public function build_signature($request, $consumer, $token) |
|
332 | + { |
|
282 | 333 | $base_string = $request->get_signature_base_string(); |
283 | 334 | $request->base_string = $base_string; |
284 | 335 | |
@@ -299,8 +350,10 @@ discard block |
||
299 | 350 | * over a secure channel such as HTTPS. It does not use the Signature Base String. |
300 | 351 | * - Chapter 9.4 ("PLAINTEXT") |
301 | 352 | */ |
302 | -class OAuthSignatureMethod_PLAINTEXT extends OAuthSignatureMethod { |
|
303 | - public function get_name() { |
|
353 | +class OAuthSignatureMethod_PLAINTEXT extends OAuthSignatureMethod |
|
354 | +{ |
|
355 | + public function get_name() |
|
356 | + { |
|
304 | 357 | return "PLAINTEXT"; |
305 | 358 | } |
306 | 359 | |
@@ -313,7 +366,8 @@ discard block |
||
313 | 366 | * Please note that the second encoding MUST NOT happen in the SignatureMethod, as |
314 | 367 | * OAuthRequest handles this! |
315 | 368 | */ |
316 | - public function build_signature($request, $consumer, $token) { |
|
369 | + public function build_signature($request, $consumer, $token) |
|
370 | + { |
|
317 | 371 | $key_parts = array( |
318 | 372 | $consumer->secret, |
319 | 373 | ($token) ? $token->secret : "" |
@@ -335,8 +389,10 @@ discard block |
||
335 | 389 | * specification. |
336 | 390 | * - Chapter 9.3 ("RSA-SHA1") |
337 | 391 | */ |
338 | -abstract class OAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod { |
|
339 | - public function get_name() { |
|
392 | +abstract class OAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod |
|
393 | +{ |
|
394 | + public function get_name() |
|
395 | + { |
|
340 | 396 | return "RSA-SHA1"; |
341 | 397 | } |
342 | 398 | |
@@ -354,7 +410,8 @@ discard block |
||
354 | 410 | // Either way should return a string representation of the certificate |
355 | 411 | protected abstract function fetch_private_cert(&$request); |
356 | 412 | |
357 | - public function build_signature($request, $consumer, $token) { |
|
413 | + public function build_signature($request, $consumer, $token) |
|
414 | + { |
|
358 | 415 | $base_string = $request->get_signature_base_string(); |
359 | 416 | $request->base_string = $base_string; |
360 | 417 | |
@@ -373,7 +430,8 @@ discard block |
||
373 | 430 | return base64_encode($signature); |
374 | 431 | } |
375 | 432 | |
376 | - public function check_signature($request, $consumer, $token, $signature) { |
|
433 | + public function check_signature($request, $consumer, $token, $signature) |
|
434 | + { |
|
377 | 435 | $decoded_sig = base64_decode($signature); |
378 | 436 | |
379 | 437 | $base_string = $request->get_signature_base_string(); |
@@ -394,7 +452,8 @@ discard block |
||
394 | 452 | } |
395 | 453 | } |
396 | 454 | |
397 | -class OAuthRequest { |
|
455 | +class OAuthRequest |
|
456 | +{ |
|
398 | 457 | protected $parameters; |
399 | 458 | protected $http_method; |
400 | 459 | protected $http_url; |
@@ -403,7 +462,8 @@ discard block |
||
403 | 462 | public static $version = '1.0'; |
404 | 463 | public static $POST_INPUT = 'php://input'; |
405 | 464 | |
406 | - function __construct($http_method, $http_url, $parameters=NULL) { |
|
465 | + function __construct($http_method, $http_url, $parameters=NULL) |
|
466 | + { |
|
407 | 467 | $parameters = ($parameters) ? $parameters : array(); |
408 | 468 | $parameters = array_merge( OAuthUtil::parse_parameters(parse_url($http_url, PHP_URL_QUERY)), $parameters); |
409 | 469 | $this->parameters = $parameters; |
@@ -415,7 +475,8 @@ discard block |
||
415 | 475 | /** |
416 | 476 | * attempt to build up a request from what was passed to the server |
417 | 477 | */ |
418 | - public static function from_request($http_method=NULL, $http_url=NULL, $parameters=NULL) { |
|
478 | + public static function from_request($http_method=NULL, $http_url=NULL, $parameters=NULL) |
|
479 | + { |
|
419 | 480 | $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") |
420 | 481 | ? 'http' |
421 | 482 | : 'https'; |
@@ -467,21 +528,24 @@ discard block |
||
467 | 528 | /** |
468 | 529 | * pretty much a helper function to set up the request |
469 | 530 | */ |
470 | - public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL) { |
|
531 | + public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL) |
|
532 | + { |
|
471 | 533 | $parameters = ($parameters) ? $parameters : array(); |
472 | 534 | $defaults = array("oauth_version" => OAuthRequest::$version, |
473 | 535 | "oauth_nonce" => OAuthRequest::generate_nonce(), |
474 | 536 | "oauth_timestamp" => OAuthRequest::generate_timestamp(), |
475 | 537 | "oauth_consumer_key" => $consumer->key); |
476 | - if ($token) |
|
477 | - $defaults['oauth_token'] = $token->key; |
|
538 | + if ($token) { |
|
539 | + $defaults['oauth_token'] = $token->key; |
|
540 | + } |
|
478 | 541 | |
479 | 542 | $parameters = array_merge($defaults, $parameters); |
480 | 543 | |
481 | 544 | return new OAuthRequest($http_method, $http_url, $parameters); |
482 | 545 | } |
483 | 546 | |
484 | - public function set_parameter($name, $value, $allow_duplicates = true) { |
|
547 | + public function set_parameter($name, $value, $allow_duplicates = true) |
|
548 | + { |
|
485 | 549 | if ($allow_duplicates && isset($this->parameters[$name])) { |
486 | 550 | // We have already added parameter(s) with this name, so add to the list |
487 | 551 | if (is_scalar($this->parameters[$name])) { |
@@ -496,15 +560,18 @@ discard block |
||
496 | 560 | } |
497 | 561 | } |
498 | 562 | |
499 | - public function get_parameter($name) { |
|
563 | + public function get_parameter($name) |
|
564 | + { |
|
500 | 565 | return isset($this->parameters[$name]) ? $this->parameters[$name] : null; |
501 | 566 | } |
502 | 567 | |
503 | - public function get_parameters() { |
|
568 | + public function get_parameters() |
|
569 | + { |
|
504 | 570 | return $this->parameters; |
505 | 571 | } |
506 | 572 | |
507 | - public function unset_parameter($name) { |
|
573 | + public function unset_parameter($name) |
|
574 | + { |
|
508 | 575 | unset($this->parameters[$name]); |
509 | 576 | } |
510 | 577 | |
@@ -512,7 +579,8 @@ discard block |
||
512 | 579 | * The request parameters, sorted and concatenated into a normalized string. |
513 | 580 | * @return string |
514 | 581 | */ |
515 | - public function get_signable_parameters() { |
|
582 | + public function get_signable_parameters() |
|
583 | + { |
|
516 | 584 | // Grab all parameters |
517 | 585 | $params = $this->parameters; |
518 | 586 | |
@@ -532,7 +600,8 @@ discard block |
||
532 | 600 | * and the parameters (normalized), each urlencoded |
533 | 601 | * and the concated with &. |
534 | 602 | */ |
535 | - public function get_signature_base_string() { |
|
603 | + public function get_signature_base_string() |
|
604 | + { |
|
536 | 605 | $parts = array( |
537 | 606 | $this->get_normalized_http_method(), |
538 | 607 | $this->get_normalized_http_url(), |
@@ -547,7 +616,8 @@ discard block |
||
547 | 616 | /** |
548 | 617 | * just uppercases the http method |
549 | 618 | */ |
550 | - public function get_normalized_http_method() { |
|
619 | + public function get_normalized_http_method() |
|
620 | + { |
|
551 | 621 | return strtoupper($this->http_method); |
552 | 622 | } |
553 | 623 | |
@@ -555,7 +625,8 @@ discard block |
||
555 | 625 | * parses the url and rebuilds it to be |
556 | 626 | * scheme://host/path |
557 | 627 | */ |
558 | - public function get_normalized_http_url() { |
|
628 | + public function get_normalized_http_url() |
|
629 | + { |
|
559 | 630 | $parts = parse_url($this->http_url); |
560 | 631 | |
561 | 632 | $scheme = (isset($parts['scheme'])) ? $parts['scheme'] : 'http'; |
@@ -573,7 +644,8 @@ discard block |
||
573 | 644 | /** |
574 | 645 | * builds a url usable for a GET request |
575 | 646 | */ |
576 | - public function to_url() { |
|
647 | + public function to_url() |
|
648 | + { |
|
577 | 649 | $post_data = $this->to_postdata(); |
578 | 650 | $out = $this->get_normalized_http_url(); |
579 | 651 | if ($post_data) { |
@@ -585,24 +657,29 @@ discard block |
||
585 | 657 | /** |
586 | 658 | * builds the data one would send in a POST request |
587 | 659 | */ |
588 | - public function to_postdata() { |
|
660 | + public function to_postdata() |
|
661 | + { |
|
589 | 662 | return OAuthUtil::build_http_query($this->parameters); |
590 | 663 | } |
591 | 664 | |
592 | 665 | /** |
593 | 666 | * builds the Authorization: header |
594 | 667 | */ |
595 | - public function to_header($realm=null) { |
|
668 | + public function to_header($realm=null) |
|
669 | + { |
|
596 | 670 | $first = true; |
597 | 671 | if($realm) { |
598 | 672 | $out = 'Authorization: OAuth realm="' . OAuthUtil::urlencode_rfc3986($realm) . '"'; |
599 | 673 | $first = false; |
600 | - } else |
|
601 | - $out = 'Authorization: OAuth'; |
|
674 | + } else { |
|
675 | + $out = 'Authorization: OAuth'; |
|
676 | + } |
|
602 | 677 | |
603 | 678 | $total = array(); |
604 | 679 | foreach ($this->parameters as $k => $v) { |
605 | - if (substr($k, 0, 5) != "oauth") continue; |
|
680 | + if (substr($k, 0, 5) != "oauth") { |
|
681 | + continue; |
|
682 | + } |
|
606 | 683 | if (is_array($v)) { |
607 | 684 | throw new OAuthClientException('Arrays not supported in headers.'); |
608 | 685 | } |
@@ -616,12 +693,14 @@ discard block |
||
616 | 693 | return $out; |
617 | 694 | } |
618 | 695 | |
619 | - public function __toString() { |
|
696 | + public function __toString() |
|
697 | + { |
|
620 | 698 | return $this->to_url(); |
621 | 699 | } |
622 | 700 | |
623 | 701 | |
624 | - public function sign_request($signature_method, $consumer, $token) { |
|
702 | + public function sign_request($signature_method, $consumer, $token) |
|
703 | + { |
|
625 | 704 | $this->set_parameter( |
626 | 705 | "oauth_signature_method", |
627 | 706 | $signature_method->get_name(), |
@@ -631,7 +710,8 @@ discard block |
||
631 | 710 | $this->set_parameter("oauth_signature", $signature, false); |
632 | 711 | } |
633 | 712 | |
634 | - public function build_signature($signature_method, $consumer, $token) { |
|
713 | + public function build_signature($signature_method, $consumer, $token) |
|
714 | + { |
|
635 | 715 | $signature = $signature_method->build_signature($this, $consumer, $token); |
636 | 716 | return $signature; |
637 | 717 | } |
@@ -639,14 +719,16 @@ discard block |
||
639 | 719 | /** |
640 | 720 | * util function: current timestamp |
641 | 721 | */ |
642 | - private static function generate_timestamp() { |
|
722 | + private static function generate_timestamp() |
|
723 | + { |
|
643 | 724 | return time(); |
644 | 725 | } |
645 | 726 | |
646 | 727 | /** |
647 | 728 | * util function: current nonce |
648 | 729 | */ |
649 | - private static function generate_nonce() { |
|
730 | + private static function generate_nonce() |
|
731 | + { |
|
650 | 732 | $mt = microtime(); |
651 | 733 | $rand = mt_rand(); |
652 | 734 | |
@@ -654,18 +736,21 @@ discard block |
||
654 | 736 | } |
655 | 737 | } |
656 | 738 | |
657 | -class OAuthServer { |
|
739 | +class OAuthServer |
|
740 | +{ |
|
658 | 741 | protected $timestamp_threshold = 300; // in seconds, five minutes |
659 | 742 | protected $version = '1.0'; // hi blaine |
660 | 743 | protected $signature_methods = array(); |
661 | 744 | |
662 | 745 | protected $data_store; |
663 | 746 | |
664 | - function __construct($data_store) { |
|
747 | + function __construct($data_store) |
|
748 | + { |
|
665 | 749 | $this->data_store = $data_store; |
666 | 750 | } |
667 | 751 | |
668 | - public function add_signature_method($signature_method) { |
|
752 | + public function add_signature_method($signature_method) |
|
753 | + { |
|
669 | 754 | $this->signature_methods[$signature_method->get_name()] = |
670 | 755 | $signature_method; |
671 | 756 | } |
@@ -676,7 +761,8 @@ discard block |
||
676 | 761 | * process a request_token request |
677 | 762 | * returns the request token on success |
678 | 763 | */ |
679 | - public function fetch_request_token(&$request) { |
|
764 | + public function fetch_request_token(&$request) |
|
765 | + { |
|
680 | 766 | $this->get_version($request); |
681 | 767 | |
682 | 768 | $consumer = $this->get_consumer($request); |
@@ -697,7 +783,8 @@ discard block |
||
697 | 783 | * process an access_token request |
698 | 784 | * returns the access token on success |
699 | 785 | */ |
700 | - public function fetch_access_token(&$request) { |
|
786 | + public function fetch_access_token(&$request) |
|
787 | + { |
|
701 | 788 | $this->get_version($request); |
702 | 789 | |
703 | 790 | $consumer = $this->get_consumer($request); |
@@ -717,7 +804,8 @@ discard block |
||
717 | 804 | /** |
718 | 805 | * verify an api call, checks all the parameters |
719 | 806 | */ |
720 | - public function verify_request(&$request) { |
|
807 | + public function verify_request(&$request) |
|
808 | + { |
|
721 | 809 | $this->get_version($request); |
722 | 810 | $consumer = $this->get_consumer($request); |
723 | 811 | $token = $this->get_token($request, $consumer, "access"); |
@@ -729,7 +817,8 @@ discard block |
||
729 | 817 | /** |
730 | 818 | * version 1 |
731 | 819 | */ |
732 | - protected function get_version(&$request) { |
|
820 | + protected function get_version(&$request) |
|
821 | + { |
|
733 | 822 | $version = $request->get_parameter("oauth_version"); |
734 | 823 | if (!$version) { |
735 | 824 | // Service Providers MUST assume the protocol version to be 1.0 if this parameter is not present. |
@@ -745,7 +834,8 @@ discard block |
||
745 | 834 | /** |
746 | 835 | * figure out the signature with some defaults |
747 | 836 | */ |
748 | - private function get_signature_method($request) { |
|
837 | + private function get_signature_method($request) |
|
838 | + { |
|
749 | 839 | $signature_method = $request instanceof OAuthRequest |
750 | 840 | ? $request->get_parameter("oauth_signature_method") |
751 | 841 | : NULL; |
@@ -770,7 +860,8 @@ discard block |
||
770 | 860 | /** |
771 | 861 | * try to find the consumer for the provided request's consumer key |
772 | 862 | */ |
773 | - protected function get_consumer($request) { |
|
863 | + protected function get_consumer($request) |
|
864 | + { |
|
774 | 865 | $consumer_key = $request instanceof OAuthRequest |
775 | 866 | ? $request->get_parameter("oauth_consumer_key") |
776 | 867 | : NULL; |
@@ -790,7 +881,8 @@ discard block |
||
790 | 881 | /** |
791 | 882 | * try to find the token for the provided request's token key |
792 | 883 | */ |
793 | - protected function get_token($request, $consumer, $token_type="access") { |
|
884 | + protected function get_token($request, $consumer, $token_type="access") |
|
885 | + { |
|
794 | 886 | $token_field = $request instanceof OAuthRequest |
795 | 887 | ? $request->get_parameter('oauth_token') |
796 | 888 | : NULL; |
@@ -810,7 +902,8 @@ discard block |
||
810 | 902 | * all-in-one function to check the signature on a request |
811 | 903 | * should guess the signature method appropriately |
812 | 904 | */ |
813 | - protected function check_signature($request, $consumer, $token) { |
|
905 | + protected function check_signature($request, $consumer, $token) |
|
906 | + { |
|
814 | 907 | // this should probably be in a different method |
815 | 908 | $timestamp = $request instanceof OAuthRequest |
816 | 909 | ? $request->get_parameter('oauth_timestamp') |
@@ -840,9 +933,11 @@ discard block |
||
840 | 933 | /** |
841 | 934 | * check that the timestamp is new enough |
842 | 935 | */ |
843 | - private function check_timestamp($timestamp) { |
|
844 | - if( ! $timestamp ) |
|
845 | - throw new OAuthMissingParameterException('oauth_timestamp'); |
|
936 | + private function check_timestamp($timestamp) |
|
937 | + { |
|
938 | + if( ! $timestamp ) { |
|
939 | + throw new OAuthMissingParameterException('oauth_timestamp'); |
|
940 | + } |
|
846 | 941 | |
847 | 942 | // Cast to integer. See issue #314. |
848 | 943 | $timestamp = $timestamp + 0; |
@@ -858,9 +953,11 @@ discard block |
||
858 | 953 | /** |
859 | 954 | * check that the nonce is not repeated |
860 | 955 | */ |
861 | - private function check_nonce($consumer, $token, $nonce, $timestamp) { |
|
862 | - if( ! $nonce ) |
|
863 | - throw new OAuthMissingParameterException('oauth_nonce'); |
|
956 | + private function check_nonce($consumer, $token, $nonce, $timestamp) |
|
957 | + { |
|
958 | + if( ! $nonce ) { |
|
959 | + throw new OAuthMissingParameterException('oauth_nonce'); |
|
960 | + } |
|
864 | 961 | |
865 | 962 | // verify that the nonce is uniqueish |
866 | 963 | $found = $this->data_store->lookup_nonce( |
@@ -876,24 +973,30 @@ discard block |
||
876 | 973 | |
877 | 974 | } |
878 | 975 | |
879 | -class OAuthDataStore { |
|
880 | - function lookup_consumer($consumer_key) { |
|
976 | +class OAuthDataStore |
|
977 | +{ |
|
978 | + function lookup_consumer($consumer_key) |
|
979 | + { |
|
881 | 980 | // implement me |
882 | 981 | } |
883 | 982 | |
884 | - function lookup_token($consumer, $token_type, $token) { |
|
983 | + function lookup_token($consumer, $token_type, $token) |
|
984 | + { |
|
885 | 985 | // implement me |
886 | 986 | } |
887 | 987 | |
888 | - function lookup_nonce($consumer, $token, $nonce, $timestamp) { |
|
988 | + function lookup_nonce($consumer, $token, $nonce, $timestamp) |
|
989 | + { |
|
889 | 990 | // implement me |
890 | 991 | } |
891 | 992 | |
892 | - function new_request_token($consumer, $callback = null) { |
|
993 | + function new_request_token($consumer, $callback = null) |
|
994 | + { |
|
893 | 995 | // return a new token attached to this consumer |
894 | 996 | } |
895 | 997 | |
896 | - function new_access_token($token, $consumer, $verifier = null) { |
|
998 | + function new_access_token($token, $consumer, $verifier = null) |
|
999 | + { |
|
897 | 1000 | // return a new access token attached to this consumer |
898 | 1001 | // for the user associated with this token if the request token |
899 | 1002 | // is authorized |
@@ -902,8 +1005,10 @@ discard block |
||
902 | 1005 | |
903 | 1006 | } |
904 | 1007 | |
905 | -class OAuthUtil { |
|
906 | - public static function urlencode_rfc3986($input) { |
|
1008 | +class OAuthUtil |
|
1009 | +{ |
|
1010 | + public static function urlencode_rfc3986($input) |
|
1011 | + { |
|
907 | 1012 | if (is_array($input)) { |
908 | 1013 | return array_map(array('\okapi\oauth\OAuthUtil', 'urlencode_rfc3986'), $input); |
909 | 1014 | } else if (is_scalar($input)) { |
@@ -921,7 +1026,8 @@ discard block |
||
921 | 1026 | // This decode function isn't taking into consideration the above |
922 | 1027 | // modifications to the encoding process. However, this method doesn't |
923 | 1028 | // seem to be used anywhere so leaving it as is. |
924 | - public static function urldecode_rfc3986($string) { |
|
1029 | + public static function urldecode_rfc3986($string) |
|
1030 | + { |
|
925 | 1031 | return urldecode($string); |
926 | 1032 | } |
927 | 1033 | |
@@ -930,7 +1036,8 @@ discard block |
||
930 | 1036 | // Can filter out any non-oauth parameters if needed (default behaviour) |
931 | 1037 | // May 28th, 2010 - method updated to tjerk.meesters for a speed improvement. |
932 | 1038 | // see http://code.google.com/p/oauth/issues/detail?id=163 |
933 | - public static function split_header($header, $only_allow_oauth_parameters = true) { |
|
1039 | + public static function split_header($header, $only_allow_oauth_parameters = true) |
|
1040 | + { |
|
934 | 1041 | $params = array(); |
935 | 1042 | if (preg_match_all('/('.($only_allow_oauth_parameters ? 'oauth_' : '').'[a-z_-]*)=(:?"([^"]*)"|([^,]*))/', $header, $matches)) { |
936 | 1043 | foreach ($matches[1] as $i => $h) { |
@@ -944,7 +1051,8 @@ discard block |
||
944 | 1051 | } |
945 | 1052 | |
946 | 1053 | // helper to try to sort out headers for people who aren't running apache |
947 | - public static function get_headers() { |
|
1054 | + public static function get_headers() |
|
1055 | + { |
|
948 | 1056 | if (function_exists('apache_request_headers')) { |
949 | 1057 | // we need this to get the actual Authorization: header |
950 | 1058 | // because apache tends to tell us it doesn't exist |
@@ -967,10 +1075,12 @@ discard block |
||
967 | 1075 | // otherwise we don't have apache and are just going to have to hope |
968 | 1076 | // that $_SERVER actually contains what we need |
969 | 1077 | $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']; |
|
1078 | + if( isset($_SERVER['CONTENT_TYPE']) ) { |
|
1079 | + $out['Content-Type'] = $_SERVER['CONTENT_TYPE']; |
|
1080 | + } |
|
1081 | + if( isset($_ENV['CONTENT_TYPE']) ) { |
|
1082 | + $out['Content-Type'] = $_ENV['CONTENT_TYPE']; |
|
1083 | + } |
|
974 | 1084 | |
975 | 1085 | foreach ($_SERVER as $key => $value) { |
976 | 1086 | if (substr($key, 0, 5) == "HTTP_") { |
@@ -992,8 +1102,11 @@ discard block |
||
992 | 1102 | // This function takes a input like a=b&a=c&d=e and returns the parsed |
993 | 1103 | // parameters like this |
994 | 1104 | // array('a' => array('b','c'), 'd' => 'e') |
995 | - public static function parse_parameters( $input ) { |
|
996 | - if (!isset($input) || !$input) return array(); |
|
1105 | + public static function parse_parameters( $input ) |
|
1106 | + { |
|
1107 | + if (!isset($input) || !$input) { |
|
1108 | + return array(); |
|
1109 | + } |
|
997 | 1110 | |
998 | 1111 | $pairs = explode('&', $input); |
999 | 1112 | |
@@ -1021,8 +1134,11 @@ discard block |
||
1021 | 1134 | return $parsed_parameters; |
1022 | 1135 | } |
1023 | 1136 | |
1024 | - public static function build_http_query($params) { |
|
1025 | - if (!$params) return ''; |
|
1137 | + public static function build_http_query($params) |
|
1138 | + { |
|
1139 | + if (!$params) { |
|
1140 | + return ''; |
|
1141 | + } |
|
1026 | 1142 | |
1027 | 1143 | // Urlencode both keys and values |
1028 | 1144 | $keys = OAuthUtil::urlencode_rfc3986(array_keys($params)); |
@@ -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,7 +403,7 @@ 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 | 408 | $parameters = array_merge( OAuthUtil::parse_parameters(parse_url($http_url, PHP_URL_QUERY)), $parameters); |
409 | 409 | $this->parameters = $parameters; |
@@ -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,7 +467,7 @@ 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) { |
|
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 | 473 | "oauth_nonce" => OAuthRequest::generate_nonce(), |
@@ -682,7 +682,7 @@ discard block |
||
682 | 682 | $consumer = $this->get_consumer($request); |
683 | 683 | |
684 | 684 | // no token required for the initial token request |
685 | - $token = NULL; |
|
685 | + $token = null; |
|
686 | 686 | |
687 | 687 | $this->check_signature($request, $consumer, $token); |
688 | 688 | |
@@ -748,7 +748,7 @@ discard block |
||
748 | 748 | private function get_signature_method($request) { |
749 | 749 | $signature_method = $request instanceof OAuthRequest |
750 | 750 | ? $request->get_parameter("oauth_signature_method") |
751 | - : NULL; |
|
751 | + : null; |
|
752 | 752 | |
753 | 753 | if (!$signature_method) { |
754 | 754 | // According to chapter 7 ("Accessing Protected Ressources") the signature-method |
@@ -773,7 +773,7 @@ discard block |
||
773 | 773 | protected function get_consumer($request) { |
774 | 774 | $consumer_key = $request instanceof OAuthRequest |
775 | 775 | ? $request->get_parameter("oauth_consumer_key") |
776 | - : NULL; |
|
776 | + : null; |
|
777 | 777 | |
778 | 778 | if (!$consumer_key) { |
779 | 779 | throw new OAuthMissingParameterException('oauth_consumer_key'); |
@@ -793,7 +793,7 @@ discard block |
||
793 | 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 | - : NULL; |
|
796 | + : null; |
|
797 | 797 | if (!$token_field) { |
798 | 798 | throw new OAuthMissingParameterException('oauth_token'); |
799 | 799 | } |
@@ -801,7 +801,7 @@ discard block |
||
801 | 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 | } |
@@ -814,10 +814,10 @@ discard block |
||
814 | 814 | // this should probably be in a different method |
815 | 815 | $timestamp = $request instanceof OAuthRequest |
816 | 816 | ? $request->get_parameter('oauth_timestamp') |
817 | - : NULL; |
|
817 | + : null; |
|
818 | 818 | $nonce = $request instanceof OAuthRequest |
819 | 819 | ? $request->get_parameter('oauth_nonce') |
820 | - : NULL; |
|
820 | + : null; |
|
821 | 821 | |
822 | 822 | $this->check_timestamp($timestamp); |
823 | 823 | $this->check_nonce($consumer, $token, $nonce, $timestamp); |
@@ -955,7 +955,7 @@ discard block |
||
955 | 955 | // returns the headers in the same case as they are in the |
956 | 956 | // request |
957 | 957 | $out = array(); |
958 | - foreach ($headers AS $key => $value) { |
|
958 | + foreach ($headers as $key => $value) { |
|
959 | 959 | $key = str_replace( |
960 | 960 | " ", |
961 | 961 | "-", |
@@ -55,6 +55,10 @@ |
||
55 | 55 | ); |
56 | 56 | } |
57 | 57 | |
58 | + /** |
|
59 | + * @param OkapiRequest $request |
|
60 | + * @param string $name |
|
61 | + */ |
|
58 | 62 | private static function require_uint($request, $name, $min_value = 0) |
59 | 63 | { |
60 | 64 | $val = $request->get_parameter($name); |
@@ -2,27 +2,19 @@ |
||
2 | 2 | |
3 | 3 | namespace okapi\services\caches\map\tile; |
4 | 4 | |
5 | -use Exception; |
|
6 | -use okapi\Okapi; |
|
7 | -use okapi\Settings; |
|
5 | +use okapi\BadRequest; |
|
8 | 6 | use okapi\Cache; |
9 | -use okapi\FileCache; |
|
10 | 7 | use okapi\Db; |
11 | -use okapi\OkapiRequest; |
|
12 | -use okapi\OkapiHttpResponse; |
|
13 | -use okapi\ParamMissing; |
|
14 | 8 | use okapi\InvalidParam; |
15 | -use okapi\BadRequest; |
|
16 | -use okapi\DoesNotExist; |
|
9 | +use okapi\OkapiConsumer; |
|
10 | +use okapi\OkapiHttpResponse; |
|
17 | 11 | use okapi\OkapiInternalRequest; |
18 | -use okapi\OkapiInternalConsumer; |
|
12 | +use okapi\OkapiRequest; |
|
19 | 13 | use okapi\OkapiServiceRunner; |
20 | -use okapi\OkapiLock; |
|
21 | - |
|
22 | -use okapi\services\caches\map\TileTree; |
|
14 | +use okapi\ParamMissing; |
|
15 | +use okapi\Settings; |
|
23 | 16 | use okapi\services\caches\map\TileRenderer; |
24 | -use okapi\services\caches\search\SearchAssistant; |
|
25 | -use okapi\OkapiConsumer; |
|
17 | +use okapi\services\caches\map\TileTree; |
|
26 | 18 | |
27 | 19 | require_once('tiletree.inc.php'); |
28 | 20 | require_once('tilerenderer.inc.php'); |
@@ -88,10 +88,10 @@ discard block |
||
88 | 88 | throw new InvalidParam('z', "Maximum value for this parameter is 21."); |
89 | 89 | $x = self::require_uint($request, 'x'); |
90 | 90 | $y = self::require_uint($request, 'y'); |
91 | - if ($x >= 1<<$zoom) |
|
92 | - throw new InvalidParam('x', "Should be in 0..".((1<<$zoom) - 1)."."); |
|
93 | - if ($y >= 1<<$zoom) |
|
94 | - throw new InvalidParam('y', "Should be in 0..".((1<<$zoom) - 1)."."); |
|
91 | + if ($x >= 1 << $zoom) |
|
92 | + throw new InvalidParam('x', "Should be in 0..".((1 << $zoom) - 1)."."); |
|
93 | + if ($y >= 1 << $zoom) |
|
94 | + throw new InvalidParam('y', "Should be in 0..".((1 << $zoom) - 1)."."); |
|
95 | 95 | |
96 | 96 | # Now, we will create a search set (or use one previously created). |
97 | 97 | # Instead of creating a new OkapiInternalRequest object, we will pass |
@@ -182,9 +182,9 @@ discard block |
||
182 | 182 | # the current user is the owner). |
183 | 183 | |
184 | 184 | if (isset($user['found'][$row_ref[0]])) |
185 | - $row_ref[6] |= TileTree::$FLAG_FOUND; # $row[6] is "flags" |
|
185 | + $row_ref[6] |= TileTree::$FLAG_FOUND; # $row[6] is "flags" |
|
186 | 186 | if (isset($user['own'][$row_ref[0]])) |
187 | - $row_ref[6] |= TileTree::$FLAG_OWN; # $row[6] is "flags" |
|
187 | + $row_ref[6] |= TileTree::$FLAG_OWN; # $row[6] is "flags" |
|
188 | 188 | } |
189 | 189 | } |
190 | 190 |
@@ -58,11 +58,13 @@ discard block |
||
58 | 58 | private static function require_uint($request, $name, $min_value = 0) |
59 | 59 | { |
60 | 60 | $val = $request->get_parameter($name); |
61 | - if ($val === null) |
|
62 | - throw new ParamMissing($name); |
|
61 | + if ($val === null) { |
|
62 | + throw new ParamMissing($name); |
|
63 | + } |
|
63 | 64 | $ret = intval($val); |
64 | - if ($ret < 0 || ("$ret" !== $val)) |
|
65 | - throw new InvalidParam($name, "Expecting non-negative integer."); |
|
65 | + if ($ret < 0 || ("$ret" !== $val)) { |
|
66 | + throw new InvalidParam($name, "Expecting non-negative integer."); |
|
67 | + } |
|
66 | 68 | return $ret; |
67 | 69 | } |
68 | 70 | |
@@ -84,14 +86,17 @@ discard block |
||
84 | 86 | # zoom, x, y - required tile-specific parameters. |
85 | 87 | |
86 | 88 | $zoom = self::require_uint($request, 'z'); |
87 | - if ($zoom > 21) |
|
88 | - throw new InvalidParam('z', "Maximum value for this parameter is 21."); |
|
89 | + if ($zoom > 21) { |
|
90 | + throw new InvalidParam('z', "Maximum value for this parameter is 21."); |
|
91 | + } |
|
89 | 92 | $x = self::require_uint($request, 'x'); |
90 | 93 | $y = self::require_uint($request, 'y'); |
91 | - if ($x >= 1<<$zoom) |
|
92 | - throw new InvalidParam('x', "Should be in 0..".((1<<$zoom) - 1)."."); |
|
93 | - if ($y >= 1<<$zoom) |
|
94 | - throw new InvalidParam('y', "Should be in 0..".((1<<$zoom) - 1)."."); |
|
94 | + if ($x >= 1<<$zoom) { |
|
95 | + throw new InvalidParam('x', "Should be in 0..".((1<<$zoom) - 1)."."); |
|
96 | + } |
|
97 | + if ($y >= 1<<$zoom) { |
|
98 | + throw new InvalidParam('y', "Should be in 0..".((1<<$zoom) - 1)."."); |
|
99 | + } |
|
95 | 100 | |
96 | 101 | # Now, we will create a search set (or use one previously created). |
97 | 102 | # Instead of creating a new OkapiInternalRequest object, we will pass |
@@ -112,10 +117,10 @@ discard block |
||
112 | 117 | |
113 | 118 | $rs = TileTree::query_fast($zoom, $x, $y, $set_id); |
114 | 119 | $rows = array(); |
115 | - if ($rs !== null) |
|
116 | - { |
|
117 | - while ($row = Db::fetch_row($rs)) |
|
118 | - $rows[] = $row; |
|
120 | + if ($rs !== null) { |
|
121 | + while ($row = Db::fetch_row($rs)) { |
|
122 | + $rows[] = $row; |
|
123 | + } |
|
119 | 124 | unset($row); |
120 | 125 | } |
121 | 126 | OkapiServiceRunner::save_stats_extra("caches/map/tile/checkpointA", null, |
@@ -124,14 +129,12 @@ discard block |
||
124 | 129 | |
125 | 130 | # Add dynamic, user-related flags. |
126 | 131 | |
127 | - if (count($rows) > 0) |
|
128 | - { |
|
132 | + if (count($rows) > 0) { |
|
129 | 133 | # Load user-related cache ids. |
130 | 134 | |
131 | 135 | $cache_key = "tileuser/".$request->token->user_id; |
132 | 136 | $user = self::$USE_OTHER_CACHE ? Cache::get($cache_key) : null; |
133 | - if ($user === null) |
|
134 | - { |
|
137 | + if ($user === null) { |
|
135 | 138 | $user = array(); |
136 | 139 | |
137 | 140 | # Ignored caches. |
@@ -142,8 +145,9 @@ discard block |
||
142 | 145 | where user_id = '".Db::escape_string($request->token->user_id)."' |
143 | 146 | "); |
144 | 147 | $user['ignored'] = array(); |
145 | - while (list($cache_id) = Db::fetch_row($rs)) |
|
146 | - $user['ignored'][$cache_id] = true; |
|
148 | + while (list($cache_id) = Db::fetch_row($rs)) { |
|
149 | + $user['ignored'][$cache_id] = true; |
|
150 | + } |
|
147 | 151 | |
148 | 152 | # Found caches. |
149 | 153 | |
@@ -156,8 +160,9 @@ discard block |
||
156 | 160 | and ".((Settings::get('OC_BRANCH') == 'oc.pl') ? "deleted = 0" : "true")." |
157 | 161 | "); |
158 | 162 | $user['found'] = array(); |
159 | - while (list($cache_id) = Db::fetch_row($rs)) |
|
160 | - $user['found'][$cache_id] = true; |
|
163 | + while (list($cache_id) = Db::fetch_row($rs)) { |
|
164 | + $user['found'][$cache_id] = true; |
|
165 | + } |
|
161 | 166 | |
162 | 167 | # Own caches. |
163 | 168 | |
@@ -167,24 +172,28 @@ discard block |
||
167 | 172 | where user_id = '".Db::escape_string($request->token->user_id)."' |
168 | 173 | "); |
169 | 174 | $user['own'] = array(); |
170 | - while (list($cache_id) = Db::fetch_row($rs)) |
|
171 | - $user['own'][$cache_id] = true; |
|
175 | + while (list($cache_id) = Db::fetch_row($rs)) { |
|
176 | + $user['own'][$cache_id] = true; |
|
177 | + } |
|
172 | 178 | |
173 | 179 | Cache::set($cache_key, $user, 30); |
174 | 180 | } |
175 | 181 | |
176 | 182 | # Add extra flags to geocaches. |
177 | 183 | |
178 | - foreach ($rows as &$row_ref) |
|
179 | - { |
|
184 | + foreach ($rows as &$row_ref) { |
|
180 | 185 | # Add the "found" flag (to indicate that this cache needs |
181 | 186 | # to be drawn as found) and the "own" flag (to indicate that |
182 | 187 | # the current user is the owner). |
183 | 188 | |
184 | - if (isset($user['found'][$row_ref[0]])) |
|
185 | - $row_ref[6] |= TileTree::$FLAG_FOUND; # $row[6] is "flags" |
|
186 | - if (isset($user['own'][$row_ref[0]])) |
|
187 | - $row_ref[6] |= TileTree::$FLAG_OWN; # $row[6] is "flags" |
|
189 | + if (isset($user['found'][$row_ref[0]])) { |
|
190 | + $row_ref[6] |= TileTree::$FLAG_FOUND; |
|
191 | + } |
|
192 | + # $row[6] is "flags" |
|
193 | + if (isset($user['own'][$row_ref[0]])) { |
|
194 | + $row_ref[6] |= TileTree::$FLAG_OWN; |
|
195 | + } |
|
196 | + # $row[6] is "flags" |
|
188 | 197 | } |
189 | 198 | } |
190 | 199 | |
@@ -207,8 +216,7 @@ discard block |
||
207 | 216 | OkapiServiceRunner::save_stats_extra("caches/map/tile/checkpointB", null, |
208 | 217 | microtime(true) - $checkpointB_started); |
209 | 218 | $checkpointC_started = microtime(true); |
210 | - if (self::$USE_ETAGS_CACHE && ($request->etag == $response->etag)) |
|
211 | - { |
|
219 | + if (self::$USE_ETAGS_CACHE && ($request->etag == $response->etag)) { |
|
212 | 220 | # Hit. Report the content was unmodified. |
213 | 221 | |
214 | 222 | $response->etag = null; |
@@ -223,8 +231,7 @@ discard block |
||
223 | 231 | OkapiServiceRunner::save_stats_extra("caches/map/tile/checkpointC", null, |
224 | 232 | microtime(true) - $checkpointC_started); |
225 | 233 | $checkpointD_started = microtime(true); |
226 | - if ($response->body !== null) |
|
227 | - { |
|
234 | + if ($response->body !== null) { |
|
228 | 235 | # Hit. We will use the cached version of the image. |
229 | 236 | |
230 | 237 | return $response; |