@@ -32,22 +32,32 @@ discard block |
||
32 | 32 | public static function call(OkapiRequest $request) |
33 | 33 | { |
34 | 34 | $cache_codes = $request->get_parameter('cache_codes'); |
35 | - if ($cache_codes === null) throw new ParamMissing('cache_codes'); |
|
35 | + if ($cache_codes === null) { |
|
36 | + throw new ParamMissing('cache_codes'); |
|
37 | + } |
|
36 | 38 | |
37 | 39 | # Issue 106 requires us to allow empty list of cache codes to be passed into this method. |
38 | 40 | # All of the queries below have to be ready for $cache_codes to be empty! |
39 | 41 | |
40 | 42 | $langpref = $request->get_parameter('langpref'); |
41 | - if (!$langpref) $langpref = "en"; |
|
43 | + if (!$langpref) { |
|
44 | + $langpref = "en"; |
|
45 | + } |
|
42 | 46 | $langpref .= "|".Settings::get('SITELANG'); |
43 | 47 | $images = $request->get_parameter('images'); |
44 | - if (!$images) $images = "all"; |
|
45 | - if (!in_array($images, array("none", "all", "spoilers", "nonspoilers"))) |
|
46 | - throw new InvalidParam('images'); |
|
48 | + if (!$images) { |
|
49 | + $images = "all"; |
|
50 | + } |
|
51 | + if (!in_array($images, array("none", "all", "spoilers", "nonspoilers"))) { |
|
52 | + throw new InvalidParam('images'); |
|
53 | + } |
|
47 | 54 | $format = $request->get_parameter('caches_format'); |
48 | - if (!$format) $format = "gpx"; |
|
49 | - if (!in_array($format, array("gpx", "ggz"))) |
|
50 | - throw new InvalidParam('caches_format'); |
|
55 | + if (!$format) { |
|
56 | + $format = "gpx"; |
|
57 | + } |
|
58 | + if (!in_array($format, array("gpx", "ggz"))) { |
|
59 | + throw new InvalidParam('caches_format'); |
|
60 | + } |
|
51 | 61 | |
52 | 62 | $location_source = $request->get_parameter('location_source'); |
53 | 63 | $location_change_prefix = $request->get_parameter('location_change_prefix'); |
@@ -97,25 +107,29 @@ discard block |
||
97 | 107 | $caches = OkapiServiceRunner::call('services/caches/geocaches', new OkapiInternalRequest( |
98 | 108 | $request->consumer, $request->token, array('cache_codes' => $cache_codes, |
99 | 109 | 'langpref' => $langpref, 'fields' => "images"))); |
100 | - if (count($caches) > 50) |
|
101 | - throw new InvalidParam('cache_codes', "The maximum number of caches allowed to be downloaded with this method is 50."); |
|
110 | + if (count($caches) > 50) { |
|
111 | + throw new InvalidParam('cache_codes', "The maximum number of caches allowed to be downloaded with this method is 50."); |
|
112 | + } |
|
102 | 113 | if ($images != 'none') |
103 | 114 | { |
104 | 115 | $supported_extensions = array('jpg', 'jpeg', 'gif', 'png', 'bmp'); |
105 | 116 | foreach ($caches as $cache_code => $dict) |
106 | 117 | { |
107 | 118 | $imgs = $dict['images']; |
108 | - if (count($imgs) == 0) |
|
109 | - continue; |
|
119 | + if (count($imgs) == 0) { |
|
120 | + continue; |
|
121 | + } |
|
110 | 122 | $dir = "Garmin/GeocachePhotos/".$cache_code[strlen($cache_code) - 1]; |
111 | 123 | $dir .= "/".$cache_code[strlen($cache_code) - 2]; |
112 | 124 | $dir .= "/".$cache_code; |
113 | 125 | foreach ($imgs as $no => $img) |
114 | 126 | { |
115 | - if ($images == 'spoilers' && (!$img['is_spoiler'])) |
|
116 | - continue; |
|
117 | - if ($images == 'nonspoilers' && $img['is_spoiler']) |
|
118 | - continue; |
|
127 | + if ($images == 'spoilers' && (!$img['is_spoiler'])) { |
|
128 | + continue; |
|
129 | + } |
|
130 | + if ($images == 'nonspoilers' && $img['is_spoiler']) { |
|
131 | + continue; |
|
132 | + } |
|
119 | 133 | $tmp = false; |
120 | 134 | foreach ($supported_extensions as $ext) |
121 | 135 | { |
@@ -125,8 +139,10 @@ discard block |
||
125 | 139 | continue; |
126 | 140 | } |
127 | 141 | } |
128 | - if (!$tmp) |
|
129 | - continue; # unsupported file extension |
|
142 | + if (!$tmp) { |
|
143 | + continue; |
|
144 | + } |
|
145 | + # unsupported file extension |
|
130 | 146 | |
131 | 147 | if ($img['is_spoiler']) { |
132 | 148 | $zippath = $dir."/Spoilers/".$img['unique_caption'].".jpg"; |
@@ -145,8 +161,7 @@ discard block |
||
145 | 161 | if (file_exists($syspath)) |
146 | 162 | { |
147 | 163 | $response->zip->FileAdd($zippath, $syspath, clsTbsZip::TBSZIP_FILE, false); |
148 | - } |
|
149 | - else |
|
164 | + } else |
|
150 | 165 | { |
151 | 166 | # If file exists, but does not end with ".jpg", we will create |
152 | 167 | # JPEG version of it and store it in the cache. |
@@ -167,8 +182,7 @@ discard block |
||
167 | 182 | imagejpeg($image); |
168 | 183 | $jpeg_contents = ob_get_clean(); |
169 | 184 | imagedestroy($image); |
170 | - } |
|
171 | - catch (Exception $e) |
|
185 | + } catch (Exception $e) |
|
172 | 186 | { |
173 | 187 | # GD couldn't parse the file. We will skip it, and cache |
174 | 188 | # the "false" value as the contents. This way, we won't |
@@ -181,8 +195,10 @@ discard block |
||
181 | 195 | } |
182 | 196 | } |
183 | 197 | } |
184 | - if ($jpeg_contents) # This can be "null" *or* "false"! |
|
198 | + if ($jpeg_contents) { |
|
199 | + # This can be "null" *or* "false"! |
|
185 | 200 | $response->zip->FileAdd($zippath, $jpeg_contents, clsTbsZip::TBSZIP_STRING, false); |
201 | + } |
|
186 | 202 | } |
187 | 203 | } |
188 | 204 | } |
@@ -24,19 +24,33 @@ discard block |
||
24 | 24 | public static function call(OkapiRequest $request) |
25 | 25 | { |
26 | 26 | $cache_code = $request->get_parameter('cache_code'); |
27 | - if (!$cache_code) throw new ParamMissing('cache_code'); |
|
28 | - if (strpos($cache_code, "|") !== false) throw new InvalidParam('cache_code'); |
|
27 | + if (!$cache_code) { |
|
28 | + throw new ParamMissing('cache_code'); |
|
29 | + } |
|
30 | + if (strpos($cache_code, "|") !== false) { |
|
31 | + throw new InvalidParam('cache_code'); |
|
32 | + } |
|
29 | 33 | $langpref = $request->get_parameter('langpref'); |
30 | - if (!$langpref) $langpref = "en"; |
|
34 | + if (!$langpref) { |
|
35 | + $langpref = "en"; |
|
36 | + } |
|
31 | 37 | $langpref .= "|".Settings::get('SITELANG'); |
32 | 38 | $fields = $request->get_parameter('fields'); |
33 | - if (!$fields) $fields = "code|name|location|type|status"; |
|
39 | + if (!$fields) { |
|
40 | + $fields = "code|name|location|type|status"; |
|
41 | + } |
|
34 | 42 | $log_fields = $request->get_parameter('log_fields'); |
35 | - if (!$log_fields) $log_fields = "uuid|date|user|type|comment"; |
|
43 | + if (!$log_fields) { |
|
44 | + $log_fields = "uuid|date|user|type|comment"; |
|
45 | + } |
|
36 | 46 | $lpc = $request->get_parameter('lpc'); |
37 | - if (!$lpc) $lpc = 10; |
|
47 | + if (!$lpc) { |
|
48 | + $lpc = 10; |
|
49 | + } |
|
38 | 50 | $attribution_append = $request->get_parameter('attribution_append'); |
39 | - if (!$attribution_append) $attribution_append = 'full'; |
|
51 | + if (!$attribution_append) { |
|
52 | + $attribution_append = 'full'; |
|
53 | + } |
|
40 | 54 | $params = array( |
41 | 55 | 'cache_codes' => $cache_code, |
42 | 56 | 'langpref' => $langpref, |
@@ -46,11 +60,13 @@ discard block |
||
46 | 60 | 'log_fields' => $log_fields |
47 | 61 | ); |
48 | 62 | $my_location = $request->get_parameter('my_location'); |
49 | - if ($my_location) |
|
50 | - $params['my_location'] = $my_location; |
|
63 | + if ($my_location) { |
|
64 | + $params['my_location'] = $my_location; |
|
65 | + } |
|
51 | 66 | $user_uuid = $request->get_parameter('user_uuid'); |
52 | - if ($user_uuid) |
|
53 | - $params['user_uuid'] = $user_uuid; |
|
67 | + if ($user_uuid) { |
|
68 | + $params['user_uuid'] = $user_uuid; |
|
69 | + } |
|
54 | 70 | |
55 | 71 | # There's no need to validate the fields/lpc parameters as the 'geocaches' |
56 | 72 | # method does this (it will raise a proper exception on invalid values). |
@@ -29,8 +29,9 @@ discard block |
||
29 | 29 | # Get current notes, and verify cache_code |
30 | 30 | |
31 | 31 | $cache_code = $request->get_parameter('cache_code'); |
32 | - if ($cache_code == null) |
|
33 | - throw new ParamMissing('cache_code'); |
|
32 | + if ($cache_code == null) { |
|
33 | + throw new ParamMissing('cache_code'); |
|
34 | + } |
|
34 | 35 | $geocache = OkapiServiceRunner::call( |
35 | 36 | 'services/caches/geocache', |
36 | 37 | new OkapiInternalRequest($request->consumer, $request->token, array( |
@@ -47,14 +48,16 @@ discard block |
||
47 | 48 | # old_value |
48 | 49 | |
49 | 50 | $old_value = $request->get_parameter('old_value'); |
50 | - if ($old_value === null) |
|
51 | - $old_value = ''; |
|
51 | + if ($old_value === null) { |
|
52 | + $old_value = ''; |
|
53 | + } |
|
52 | 54 | |
53 | 55 | # new_value (force "no HTML" policy). |
54 | 56 | |
55 | 57 | $new_value = $request->get_parameter('new_value'); |
56 | - if ($new_value === null) |
|
57 | - throw new ParamMissing('new_value'); |
|
58 | + if ($new_value === null) { |
|
59 | + throw new ParamMissing('new_value'); |
|
60 | + } |
|
58 | 61 | |
59 | 62 | # Force "no HTML" policy. |
60 | 63 | |
@@ -98,10 +101,12 @@ discard block |
||
98 | 101 | |
99 | 102 | private static function str_equals($str1, $str2) |
100 | 103 | { |
101 | - if ($str1 == null) |
|
102 | - $str1 = ''; |
|
103 | - if ($str2 == null) |
|
104 | - $str2 = ''; |
|
104 | + if ($str1 == null) { |
|
105 | + $str1 = ''; |
|
106 | + } |
|
107 | + if ($str2 == null) { |
|
108 | + $str2 = ''; |
|
109 | + } |
|
105 | 110 | $str1 = mb_ereg_replace("[ \t\n\r\x0B]+", '', $str1); |
106 | 111 | $str2 = mb_ereg_replace("[ \t\n\r\x0B]+", '', $str2); |
107 | 112 | |
@@ -151,8 +156,7 @@ discard block |
||
151 | 156 | and type = 2 |
152 | 157 | "); |
153 | 158 | } |
154 | - } |
|
155 | - else # oc.pl branch |
|
159 | + } else # oc.pl branch |
|
156 | 160 | { |
157 | 161 | $rs = Db::query(" |
158 | 162 | select max(note_id) as id |
@@ -30,22 +30,27 @@ discard block |
||
30 | 30 | # It's much easier to grasp their meaning this way. |
31 | 31 | |
32 | 32 | $tmp = $request->get_parameter('center'); |
33 | - if (!$tmp) |
|
34 | - throw new ParamMissing('center'); |
|
33 | + if (!$tmp) { |
|
34 | + throw new ParamMissing('center'); |
|
35 | + } |
|
35 | 36 | $parts = explode('|', $tmp); |
36 | - if (count($parts) != 2) |
|
37 | - throw new InvalidParam('center', "Expecting 2 pipe-separated parts, got ".count($parts)."."); |
|
37 | + if (count($parts) != 2) { |
|
38 | + throw new InvalidParam('center', "Expecting 2 pipe-separated parts, got ".count($parts)."."); |
|
39 | + } |
|
38 | 40 | foreach ($parts as &$part_ref) |
39 | 41 | { |
40 | - if (!preg_match("/^-?[0-9]+(\.?[0-9]*)$/", $part_ref)) |
|
41 | - throw new InvalidParam('center', "'$part_ref' is not a valid float number."); |
|
42 | + if (!preg_match("/^-?[0-9]+(\.?[0-9]*)$/", $part_ref)) { |
|
43 | + throw new InvalidParam('center', "'$part_ref' is not a valid float number."); |
|
44 | + } |
|
42 | 45 | $part_ref = floatval($part_ref); |
43 | 46 | } |
44 | 47 | list($center_lat, $center_lon) = $parts; |
45 | - if ($center_lat > 90 || $center_lat < -90) |
|
46 | - throw new InvalidParam('center', "Latitudes have to be within -90..90 range."); |
|
47 | - if ($center_lon > 180 || $center_lon < -180) |
|
48 | - throw new InvalidParam('center', "Longitudes have to be within -180..180 range."); |
|
48 | + if ($center_lat > 90 || $center_lat < -90) { |
|
49 | + throw new InvalidParam('center', "Latitudes have to be within -90..90 range."); |
|
50 | + } |
|
51 | + if ($center_lon > 180 || $center_lon < -180) { |
|
52 | + throw new InvalidParam('center', "Longitudes have to be within -180..180 range."); |
|
53 | + } |
|
49 | 54 | |
50 | 55 | # |
51 | 56 | # In the method description, we promised to return caches ordered by the *rough* |
@@ -68,11 +73,13 @@ discard block |
||
68 | 73 | $radius = null; |
69 | 74 | if ($tmp = $request->get_parameter('radius')) |
70 | 75 | { |
71 | - if (!preg_match("/^-?[0-9]+(\.?[0-9]*)$/", $tmp)) |
|
72 | - throw new InvalidParam('radius', "'$tmp' is not a valid float number."); |
|
76 | + if (!preg_match("/^-?[0-9]+(\.?[0-9]*)$/", $tmp)) { |
|
77 | + throw new InvalidParam('radius', "'$tmp' is not a valid float number."); |
|
78 | + } |
|
73 | 79 | $radius = floatval($tmp); # is given in kilometers |
74 | - if ($radius <= 0) |
|
75 | - throw new InvalidParam('radius', "Has to be a positive number."); |
|
80 | + if ($radius <= 0) { |
|
81 | + throw new InvalidParam('radius', "Has to be a positive number."); |
|
82 | + } |
|
76 | 83 | |
77 | 84 | # Apply a latitude-range prefilter if it looks promising. |
78 | 85 | # See https://github.com/opencaching/okapi/issues/363 for more info. |
@@ -39,8 +39,9 @@ discard block |
||
39 | 39 | order by id desc |
40 | 40 | limit 1 |
41 | 41 | "); |
42 | - if ($tmp === null) |
|
43 | - return array(null, null, null); |
|
42 | + if ($tmp === null) { |
|
43 | + return array(null, null, null); |
|
44 | + } |
|
44 | 45 | return array($tmp['id'], $tmp['date_created'], $tmp['expires']); |
45 | 46 | } |
46 | 47 | |
@@ -49,17 +50,25 @@ discard block |
||
49 | 50 | # "Cache control" parameters. |
50 | 51 | |
51 | 52 | $tmp = $request->get_parameter('min_store'); |
52 | - if ($tmp === null) $tmp = "300"; |
|
53 | + if ($tmp === null) { |
|
54 | + $tmp = "300"; |
|
55 | + } |
|
53 | 56 | $min_store = intval($tmp); |
54 | - if (("$min_store" !== $tmp) ||($min_store < 0) || ($min_store > 64800)) |
|
55 | - throw new InvalidParam('min_store', "Has to be in the 0..64800 range."); |
|
57 | + if (("$min_store" !== $tmp) ||($min_store < 0) || ($min_store > 64800)) { |
|
58 | + throw new InvalidParam('min_store', "Has to be in the 0..64800 range."); |
|
59 | + } |
|
56 | 60 | |
57 | 61 | $tmp = $request->get_parameter('ref_max_age'); |
58 | - if ($tmp === null) $tmp = "300"; |
|
59 | - if ($tmp == "nolimit") $tmp = "9999999"; |
|
62 | + if ($tmp === null) { |
|
63 | + $tmp = "300"; |
|
64 | + } |
|
65 | + if ($tmp == "nolimit") { |
|
66 | + $tmp = "9999999"; |
|
67 | + } |
|
60 | 68 | $ref_max_age = intval($tmp); |
61 | - if (("$ref_max_age" !== $tmp) || ($ref_max_age < 300)) |
|
62 | - throw new InvalidParam('ref_max_age', "Has to be >=300."); |
|
69 | + if (("$ref_max_age" !== $tmp) || ($ref_max_age < 300)) { |
|
70 | + throw new InvalidParam('ref_max_age', "Has to be >=300."); |
|
71 | + } |
|
63 | 72 | |
64 | 73 | # Search params. |
65 | 74 | |
@@ -75,10 +84,11 @@ discard block |
||
75 | 84 | $search_params['where_conds'] |
76 | 85 | ); |
77 | 86 | |
78 | - if (isset($search_params['extra_joins']) && is_array($search_params['extra_joins'])) |
|
79 | - $joins = $search_params['extra_joins']; |
|
80 | - else |
|
81 | - $joins = array(); |
|
87 | + if (isset($search_params['extra_joins']) && is_array($search_params['extra_joins'])) { |
|
88 | + $joins = $search_params['extra_joins']; |
|
89 | + } else { |
|
90 | + $joins = array(); |
|
91 | + } |
|
82 | 92 | |
83 | 93 | unset($search_params); |
84 | 94 | |
@@ -161,8 +171,7 @@ discard block |
||
161 | 171 | # generated the result set. We don't need to do anything. |
162 | 172 | } |
163 | 173 | $lock->release(); |
164 | - } |
|
165 | - catch (Exception $e) |
|
174 | + } catch (Exception $e) |
|
166 | 175 | { |
167 | 176 | # SQL error? Make sure the lock is released and rethrow. |
168 | 177 |
@@ -33,45 +33,56 @@ discard block |
||
33 | 33 | if ($host == null) |
34 | 34 | { |
35 | 35 | $host = parse_url(Settings::get('SITE_URL'), PHP_URL_HOST); |
36 | - if (strpos($host, "www.") === 0) |
|
37 | - $host = substr($host, 4); |
|
36 | + if (strpos($host, "www.") === 0) { |
|
37 | + $host = substr($host, 4); |
|
38 | + } |
|
38 | 39 | $length = strlen($host); |
39 | 40 | } |
40 | 41 | |
41 | 42 | # Parse the URL |
42 | 43 | |
43 | 44 | $uri = parse_url($url); |
44 | - if ($uri == false) |
|
45 | - return null; |
|
46 | - if ((!isset($uri['scheme'])) || (!in_array($uri['scheme'], array('http', 'https')))) |
|
47 | - return null; |
|
48 | - if ((!isset($uri['host'])) || (substr($uri['host'], -$length) != $host)) |
|
49 | - return null; |
|
50 | - if (!isset($uri['path'])) |
|
51 | - return null; |
|
45 | + if ($uri == false) { |
|
46 | + return null; |
|
47 | + } |
|
48 | + if ((!isset($uri['scheme'])) || (!in_array($uri['scheme'], array('http', 'https')))) { |
|
49 | + return null; |
|
50 | + } |
|
51 | + if ((!isset($uri['host'])) || (substr($uri['host'], -$length) != $host)) { |
|
52 | + return null; |
|
53 | + } |
|
54 | + if (!isset($uri['path'])) { |
|
55 | + return null; |
|
56 | + } |
|
52 | 57 | if (preg_match("#^/(O[A-Z][A-Z0-9]{4,5})$#", $uri['path'], $matches)) |
53 | 58 | { |
54 | 59 | # Some servers allow "http://oc.xx/<cache_code>" shortcut. |
55 | 60 | return array('cache_code', $matches[1]); |
56 | 61 | } |
57 | 62 | $parts = array(); |
58 | - if (isset($uri['query'])) |
|
59 | - $parts = array_merge($parts, explode('&', $uri['query'])); |
|
60 | - if (isset($uri['fragment'])) |
|
61 | - $parts = array_merge($parts, explode('&', $uri['fragment'])); |
|
63 | + if (isset($uri['query'])) { |
|
64 | + $parts = array_merge($parts, explode('&', $uri['query'])); |
|
65 | + } |
|
66 | + if (isset($uri['fragment'])) { |
|
67 | + $parts = array_merge($parts, explode('&', $uri['fragment'])); |
|
68 | + } |
|
62 | 69 | foreach ($parts as $param) |
63 | 70 | { |
64 | 71 | $item = explode('=', $param, 2); |
65 | - if (count($item) != 2) |
|
66 | - continue; |
|
72 | + if (count($item) != 2) { |
|
73 | + continue; |
|
74 | + } |
|
67 | 75 | $key = $item[0]; |
68 | 76 | $value = $item[1]; |
69 | - if ($key == 'wp') |
|
70 | - return array('cache_code', $value); |
|
71 | - if ($key == 'cacheid') |
|
72 | - return array('internal_id', $value); |
|
73 | - if ($key == 'uuid') |
|
74 | - return array('uuid', $value); |
|
77 | + if ($key == 'wp') { |
|
78 | + return array('cache_code', $value); |
|
79 | + } |
|
80 | + if ($key == 'cacheid') { |
|
81 | + return array('internal_id', $value); |
|
82 | + } |
|
83 | + if ($key == 'uuid') { |
|
84 | + return array('uuid', $value); |
|
85 | + } |
|
75 | 86 | } |
76 | 87 | return null; |
77 | 88 | } |
@@ -81,13 +92,17 @@ discard block |
||
81 | 92 | # Retrieve the list of URLs to check. |
82 | 93 | |
83 | 94 | $tmp = $request->get_parameter('urls'); |
84 | - if (!$tmp) |
|
85 | - throw new ParamMissing('urls'); |
|
95 | + if (!$tmp) { |
|
96 | + throw new ParamMissing('urls'); |
|
97 | + } |
|
86 | 98 | $urls = explode('|', $tmp); |
87 | 99 | $as_dict = $request->get_parameter('as_dict'); |
88 | - if (!$as_dict) $as_dict = 'false'; |
|
89 | - if (!in_array($as_dict, array('true', 'false'))) |
|
90 | - throw new InvalidParam('as_dict'); |
|
100 | + if (!$as_dict) { |
|
101 | + $as_dict = 'false'; |
|
102 | + } |
|
103 | + if (!in_array($as_dict, array('true', 'false'))) { |
|
104 | + throw new InvalidParam('as_dict'); |
|
105 | + } |
|
91 | 106 | $as_dict = ($as_dict == 'true'); |
92 | 107 | |
93 | 108 | # Generate the lists of keys. |
@@ -101,16 +116,18 @@ discard block |
||
101 | 116 | foreach ($urls as &$url_ref) |
102 | 117 | { |
103 | 118 | $key = self::get_cache_key($url_ref); |
104 | - if ($key != null) |
|
105 | - $urls_with[$key[0]][$url_ref] = $key[1]; |
|
106 | - else |
|
107 | - $results[$url_ref] = null; |
|
119 | + if ($key != null) { |
|
120 | + $urls_with[$key[0]][$url_ref] = $key[1]; |
|
121 | + } else { |
|
122 | + $results[$url_ref] = null; |
|
123 | + } |
|
108 | 124 | } |
109 | 125 | |
110 | 126 | # Include 'cache_code' references. |
111 | 127 | |
112 | - foreach ($urls_with['cache_code'] as $url => $cache_code) |
|
113 | - $results[$url] = $cache_code; |
|
128 | + foreach ($urls_with['cache_code'] as $url => $cache_code) { |
|
129 | + $results[$url] = $cache_code; |
|
130 | + } |
|
114 | 131 | |
115 | 132 | # Include 'internal_id' references. |
116 | 133 | |
@@ -125,14 +142,16 @@ discard block |
||
125 | 142 | and status in (1,2,3) |
126 | 143 | "); |
127 | 144 | $dict = array(); |
128 | - while ($row = Db::fetch_assoc($rs)) |
|
129 | - $dict[$row['cache_id']] = $row['wp_oc']; |
|
145 | + while ($row = Db::fetch_assoc($rs)) { |
|
146 | + $dict[$row['cache_id']] = $row['wp_oc']; |
|
147 | + } |
|
130 | 148 | foreach ($urls_with['internal_id'] as $url => $internal_id) |
131 | 149 | { |
132 | - if (isset($dict[$internal_id])) |
|
133 | - $results[$url] = $dict[$internal_id]; |
|
134 | - else |
|
135 | - $results[$url] = null; |
|
150 | + if (isset($dict[$internal_id])) { |
|
151 | + $results[$url] = $dict[$internal_id]; |
|
152 | + } else { |
|
153 | + $results[$url] = null; |
|
154 | + } |
|
136 | 155 | } |
137 | 156 | } |
138 | 157 | |
@@ -149,27 +168,30 @@ discard block |
||
149 | 168 | and status in (1,2,3) |
150 | 169 | "); |
151 | 170 | $dict = array(); |
152 | - while ($row = Db::fetch_assoc($rs)) |
|
153 | - $dict[$row['uuid']] = $row['wp_oc']; |
|
171 | + while ($row = Db::fetch_assoc($rs)) { |
|
172 | + $dict[$row['uuid']] = $row['wp_oc']; |
|
173 | + } |
|
154 | 174 | foreach ($urls_with['uuid'] as $url => $uuid) |
155 | 175 | { |
156 | - if (isset($dict[$uuid])) |
|
157 | - $results[$url] = $dict[$uuid]; |
|
158 | - else |
|
159 | - $results[$url] = null; |
|
176 | + if (isset($dict[$uuid])) { |
|
177 | + $results[$url] = $dict[$uuid]; |
|
178 | + } else { |
|
179 | + $results[$url] = null; |
|
180 | + } |
|
160 | 181 | } |
161 | 182 | } |
162 | 183 | |
163 | 184 | # Format the results according to the 'as_dict' parameter. |
164 | 185 | |
165 | - if ($as_dict) |
|
166 | - return Okapi::formatted_response($request, $results); |
|
167 | - else |
|
186 | + if ($as_dict) { |
|
187 | + return Okapi::formatted_response($request, $results); |
|
188 | + } else |
|
168 | 189 | { |
169 | 190 | $cache_codes = array(); |
170 | - foreach ($results as $url => $cache_code) |
|
171 | - if ($cache_code != null) |
|
191 | + foreach ($results as $url => $cache_code) { |
|
192 | + if ($cache_code != null) |
|
172 | 193 | $cache_codes[$cache_code] = true; |
194 | + } |
|
173 | 195 | $flattened = array('results' => array_keys($cache_codes)); |
174 | 196 | return Okapi::formatted_response($request, $flattened); |
175 | 197 | } |
@@ -30,26 +30,33 @@ discard block |
||
30 | 30 | # It's much easier to grasp their meaning this way. |
31 | 31 | |
32 | 32 | $tmp = $request->get_parameter('bbox'); |
33 | - if (!$tmp) |
|
34 | - throw new ParamMissing('bbox'); |
|
33 | + if (!$tmp) { |
|
34 | + throw new ParamMissing('bbox'); |
|
35 | + } |
|
35 | 36 | $parts = explode('|', $tmp); |
36 | - if (count($parts) != 4) |
|
37 | - throw new InvalidParam('bbox', "Expecting 4 pipe-separated parts, got ".count($parts)."."); |
|
37 | + if (count($parts) != 4) { |
|
38 | + throw new InvalidParam('bbox', "Expecting 4 pipe-separated parts, got ".count($parts)."."); |
|
39 | + } |
|
38 | 40 | foreach ($parts as &$part_ref) |
39 | 41 | { |
40 | - if (!preg_match("/^-?[0-9]+(\.?[0-9]*)$/", $part_ref)) |
|
41 | - throw new InvalidParam('bbox', "'$part_ref' is not a valid float number."); |
|
42 | + if (!preg_match("/^-?[0-9]+(\.?[0-9]*)$/", $part_ref)) { |
|
43 | + throw new InvalidParam('bbox', "'$part_ref' is not a valid float number."); |
|
44 | + } |
|
42 | 45 | $part_ref = floatval($part_ref); |
43 | 46 | } |
44 | 47 | list($bbsouth, $bbwest, $bbnorth, $bbeast) = $parts; |
45 | - if ($bbnorth <= $bbsouth) |
|
46 | - throw new InvalidParam('bbox', "Northern edge must be situated to the north of the southern edge."); |
|
47 | - if ($bbeast == $bbwest) |
|
48 | - throw new InvalidParam('bbox', "Eastern edge longitude is the same as the western one."); |
|
49 | - if ($bbnorth > 90 || $bbnorth < -90 || $bbsouth > 90 || $bbsouth < -90) |
|
50 | - throw new InvalidParam('bbox', "Latitudes have to be within -90..90 range."); |
|
51 | - if ($bbeast > 180 || $bbeast < -180 || $bbwest > 180 || $bbwest < -180) |
|
52 | - throw new InvalidParam('bbox', "Longitudes have to be within -180..180 range."); |
|
48 | + if ($bbnorth <= $bbsouth) { |
|
49 | + throw new InvalidParam('bbox', "Northern edge must be situated to the north of the southern edge."); |
|
50 | + } |
|
51 | + if ($bbeast == $bbwest) { |
|
52 | + throw new InvalidParam('bbox', "Eastern edge longitude is the same as the western one."); |
|
53 | + } |
|
54 | + if ($bbnorth > 90 || $bbnorth < -90 || $bbsouth > 90 || $bbsouth < -90) { |
|
55 | + throw new InvalidParam('bbox', "Latitudes have to be within -90..90 range."); |
|
56 | + } |
|
57 | + if ($bbeast > 180 || $bbeast < -180 || $bbwest > 180 || $bbwest < -180) { |
|
58 | + throw new InvalidParam('bbox', "Longitudes have to be within -180..180 range."); |
|
59 | + } |
|
53 | 60 | |
54 | 61 | # Construct SQL conditions for the specified bounding box. |
55 | 62 | |
@@ -71,8 +78,7 @@ discard block |
||
71 | 78 | $lon >= '".Db::escape_string($bbwest)."' |
72 | 79 | and $lon < '".Db::escape_string($bbeast)."' |
73 | 80 | )"; |
74 | - } |
|
75 | - else |
|
81 | + } else |
|
76 | 82 | { |
77 | 83 | # We'll have to assume that this bbox goes through the 180-degree meridian. |
78 | 84 | # For example, $bbwest = 179 and $bbeast = -179. |
@@ -107,8 +107,7 @@ discard block |
||
107 | 107 | $X_LAST_FOUND = 'caches.last_found'; |
108 | 108 | $X_VOTES = 'caches.votes'; |
109 | 109 | $X_SCORE = 'caches.score'; |
110 | - } |
|
111 | - else |
|
110 | + } else |
|
112 | 111 | { |
113 | 112 | # OCDE holds this data in a separate table. Additionally, OCDE |
114 | 113 | # does not provide a rating system (votes and score fields). |
@@ -146,16 +145,16 @@ discard block |
||
146 | 145 | { |
147 | 146 | $id = Okapi::cache_type_name2id($name); |
148 | 147 | $types[] = $id; |
149 | - } |
|
150 | - catch (Exception $e) |
|
148 | + } catch (Exception $e) |
|
151 | 149 | { |
152 | 150 | throw new InvalidParam('type', "'$name' is not a valid cache type."); |
153 | 151 | } |
154 | 152 | } |
155 | - if (count($types) > 0) |
|
156 | - $where_conds[] = "caches.type $operator ('".implode("','", array_map('\okapi\Db::escape_string', $types))."')"; |
|
157 | - else if ($operator == "in") |
|
158 | - $where_conds[] = "false"; |
|
153 | + if (count($types) > 0) { |
|
154 | + $where_conds[] = "caches.type $operator ('".implode("','", array_map('\okapi\Db::escape_string', $types))."')"; |
|
155 | + } else if ($operator == "in") { |
|
156 | + $where_conds[] = "false"; |
|
157 | + } |
|
159 | 158 | } |
160 | 159 | |
161 | 160 | # |
@@ -177,8 +176,7 @@ discard block |
||
177 | 176 | { |
178 | 177 | $id = Okapi::cache_size2_to_sizeid($name); |
179 | 178 | $types[] = $id; |
180 | - } |
|
181 | - catch (Exception $e) |
|
179 | + } catch (Exception $e) |
|
182 | 180 | { |
183 | 181 | throw new InvalidParam('size2', "'$name' is not a valid cache size."); |
184 | 182 | } |
@@ -191,15 +189,16 @@ discard block |
||
191 | 189 | # |
192 | 190 | |
193 | 191 | $tmp = $this->request->get_parameter('status'); |
194 | - if ($tmp == null) $tmp = "Available"; |
|
192 | + if ($tmp == null) { |
|
193 | + $tmp = "Available"; |
|
194 | + } |
|
195 | 195 | $codes = array(); |
196 | 196 | foreach (explode("|", $tmp) as $name) |
197 | 197 | { |
198 | 198 | try |
199 | 199 | { |
200 | 200 | $codes[] = Okapi::cache_status_name2id($name); |
201 | - } |
|
202 | - catch (Exception $e) |
|
201 | + } catch (Exception $e) |
|
203 | 202 | { |
204 | 203 | throw new InvalidParam('status', "'$name' is not a valid cache status."); |
205 | 204 | } |
@@ -222,14 +221,14 @@ discard block |
||
222 | 221 | { |
223 | 222 | $users = OkapiServiceRunner::call("services/users/users", new OkapiInternalRequest( |
224 | 223 | $this->request->consumer, null, array('user_uuids' => $tmp, 'fields' => 'internal_id'))); |
225 | - } |
|
226 | - catch (InvalidParam $e) # invalid uuid |
|
224 | + } catch (InvalidParam $e) # invalid uuid |
|
227 | 225 | { |
228 | 226 | throw new InvalidParam('owner_uuid', $e->whats_wrong_about_it); |
229 | 227 | } |
230 | 228 | $user_ids = array(); |
231 | - foreach ($users as $user) |
|
232 | - $user_ids[] = $user['internal_id']; |
|
229 | + foreach ($users as $user) { |
|
230 | + $user_ids[] = $user['internal_id']; |
|
231 | + } |
|
233 | 232 | $where_conds[] = "caches.user_id $operator ('".implode("','", array_map('\okapi\Db::escape_string', $user_ids))."')"; |
234 | 233 | } |
235 | 234 | |
@@ -241,8 +240,9 @@ discard block |
||
241 | 240 | { |
242 | 241 | if ($tmp = $this->request->get_parameter($param_name)) |
243 | 242 | { |
244 | - if (!preg_match("/^[1-5]-[1-5](\|X)?$/", $tmp)) |
|
245 | - throw new InvalidParam($param_name, "'$tmp'"); |
|
243 | + if (!preg_match("/^[1-5]-[1-5](\|X)?$/", $tmp)) { |
|
244 | + throw new InvalidParam($param_name, "'$tmp'"); |
|
245 | + } |
|
246 | 246 | list($min, $max) = explode("-", $tmp); |
247 | 247 | if (strpos($max, "|X") !== false) |
248 | 248 | { |
@@ -251,13 +251,15 @@ discard block |
||
251 | 251 | } else { |
252 | 252 | $allow_null = false; |
253 | 253 | } |
254 | - if ($min > $max) |
|
255 | - throw new InvalidParam($param_name, "'$tmp'"); |
|
254 | + if ($min > $max) { |
|
255 | + throw new InvalidParam($param_name, "'$tmp'"); |
|
256 | + } |
|
256 | 257 | switch ($param_name) |
257 | 258 | { |
258 | 259 | case 'terrain': |
259 | - if ($allow_null) |
|
260 | - throw new InvalidParam($param_name, "The '|X' suffix is not allowed here."); |
|
260 | + if ($allow_null) { |
|
261 | + throw new InvalidParam($param_name, "The '|X' suffix is not allowed here."); |
|
262 | + } |
|
261 | 263 | if (($min == 1) && ($max == 5)) { |
262 | 264 | /* no extra condition necessary */ |
263 | 265 | } else { |
@@ -265,8 +267,9 @@ discard block |
||
265 | 267 | } |
266 | 268 | break; |
267 | 269 | case 'difficulty': |
268 | - if ($allow_null) |
|
269 | - throw new InvalidParam($param_name, "The '|X' suffix is not allowed here."); |
|
270 | + if ($allow_null) { |
|
271 | + throw new InvalidParam($param_name, "The '|X' suffix is not allowed here."); |
|
272 | + } |
|
270 | 273 | if (($min == 1) && ($max == 5)) { |
271 | 274 | /* no extra condition necessary */ |
272 | 275 | } else { |
@@ -299,8 +302,7 @@ discard block |
||
299 | 302 | $where_conds[] = "($X_SCORE >= $min and $X_SCORE < $max and $X_VOTES >= 3)". |
300 | 303 | ($allow_null ? " or ($X_VOTES < 3)" : ""); |
301 | 304 | } |
302 | - } |
|
303 | - else |
|
305 | + } else |
|
304 | 306 | { |
305 | 307 | # OCDE does not support rating. We will ignore this parameter. |
306 | 308 | } |
@@ -318,17 +320,20 @@ discard block |
||
318 | 320 | if ($tmp[strlen($tmp) - 1] == '%') |
319 | 321 | { |
320 | 322 | $tmp = substr($tmp, 0, strlen($tmp) - 1); |
321 | - if (!is_numeric($tmp)) |
|
322 | - throw new InvalidParam('min_rcmds', "'$tmp'"); |
|
323 | + if (!is_numeric($tmp)) { |
|
324 | + throw new InvalidParam('min_rcmds', "'$tmp'"); |
|
325 | + } |
|
323 | 326 | $tmp = intval($tmp); |
324 | - if ($tmp > 100 || $tmp < 0) |
|
325 | - throw new InvalidParam('min_rcmds', "'$tmp'"); |
|
327 | + if ($tmp > 100 || $tmp < 0) { |
|
328 | + throw new InvalidParam('min_rcmds', "'$tmp'"); |
|
329 | + } |
|
326 | 330 | $tmp = floatval($tmp) / 100.0; |
327 | 331 | $where_conds[] = "$X_TOPRATINGS >= $X_FOUNDS * '".Db::escape_string($tmp)."'"; |
328 | 332 | $where_conds[] = "$X_FOUNDS > 0"; |
329 | 333 | } |
330 | - if (!is_numeric($tmp)) |
|
331 | - throw new InvalidParam('min_rcmds', "'$tmp'"); |
|
334 | + if (!is_numeric($tmp)) { |
|
335 | + throw new InvalidParam('min_rcmds', "'$tmp'"); |
|
336 | + } |
|
332 | 337 | $where_conds[] = "$X_TOPRATINGS >= '".Db::escape_string($tmp)."'"; |
333 | 338 | } |
334 | 339 | |
@@ -338,8 +343,9 @@ discard block |
||
338 | 343 | |
339 | 344 | if ($tmp = $this->request->get_parameter('min_founds')) |
340 | 345 | { |
341 | - if (!is_numeric($tmp)) |
|
342 | - throw new InvalidParam('min_founds', "'$tmp'"); |
|
346 | + if (!is_numeric($tmp)) { |
|
347 | + throw new InvalidParam('min_founds', "'$tmp'"); |
|
348 | + } |
|
343 | 349 | $where_conds[] = "$X_FOUNDS >= '".Db::escape_string($tmp)."'"; |
344 | 350 | } |
345 | 351 | |
@@ -350,8 +356,9 @@ discard block |
||
350 | 356 | |
351 | 357 | if (!is_null($tmp = $this->request->get_parameter('max_founds'))) |
352 | 358 | { |
353 | - if (!is_numeric($tmp)) |
|
354 | - throw new InvalidParam('max_founds', "'$tmp'"); |
|
359 | + if (!is_numeric($tmp)) { |
|
360 | + throw new InvalidParam('max_founds', "'$tmp'"); |
|
361 | + } |
|
355 | 362 | $where_conds[] = "$X_FOUNDS <= '".Db::escape_string($tmp)."'"; |
356 | 363 | } |
357 | 364 | |
@@ -362,10 +369,11 @@ discard block |
||
362 | 369 | if ($tmp = $this->request->get_parameter('modified_since')) |
363 | 370 | { |
364 | 371 | $timestamp = strtotime($tmp); |
365 | - if ($timestamp) |
|
366 | - $where_conds[] = "unix_timestamp(caches.last_modified) > '".Db::escape_string($timestamp)."'"; |
|
367 | - else |
|
368 | - throw new InvalidParam('modified_since', "'$tmp' is not in a valid format or is not a valid date."); |
|
372 | + if ($timestamp) { |
|
373 | + $where_conds[] = "unix_timestamp(caches.last_modified) > '".Db::escape_string($timestamp)."'"; |
|
374 | + } else { |
|
375 | + throw new InvalidParam('modified_since', "'$tmp' is not in a valid format or is not a valid date."); |
|
376 | + } |
|
369 | 377 | } |
370 | 378 | |
371 | 379 | # |
@@ -374,10 +382,12 @@ discard block |
||
374 | 382 | |
375 | 383 | if ($tmp = $this->request->get_parameter('found_status')) |
376 | 384 | { |
377 | - if ($this->request->token == null) |
|
378 | - throw new InvalidParam('found_status', "Might be used only for requests signed with an Access Token."); |
|
379 | - if (!in_array($tmp, array('found_only', 'notfound_only', 'either'))) |
|
380 | - throw new InvalidParam('found_status', "'$tmp'"); |
|
385 | + if ($this->request->token == null) { |
|
386 | + throw new InvalidParam('found_status', "Might be used only for requests signed with an Access Token."); |
|
387 | + } |
|
388 | + if (!in_array($tmp, array('found_only', 'notfound_only', 'either'))) { |
|
389 | + throw new InvalidParam('found_status', "'$tmp'"); |
|
390 | + } |
|
381 | 391 | if ($tmp != 'either') |
382 | 392 | { |
383 | 393 | $found_cache_ids = self::get_found_cache_ids(array($this->request->token->user_id)); |
@@ -426,10 +436,12 @@ discard block |
||
426 | 436 | |
427 | 437 | if ($tmp = $this->request->get_parameter('watched_only')) |
428 | 438 | { |
429 | - if ($this->request->token == null) |
|
430 | - throw new InvalidParam('watched_only', "Might be used only for requests signed with an Access Token."); |
|
431 | - if (!in_array($tmp, array('true', 'false'))) |
|
432 | - throw new InvalidParam('watched_only', "'$tmp'"); |
|
439 | + if ($this->request->token == null) { |
|
440 | + throw new InvalidParam('watched_only', "Might be used only for requests signed with an Access Token."); |
|
441 | + } |
|
442 | + if (!in_array($tmp, array('true', 'false'))) { |
|
443 | + throw new InvalidParam('watched_only', "'$tmp'"); |
|
444 | + } |
|
433 | 445 | if ($tmp == 'true') |
434 | 446 | { |
435 | 447 | $watched_cache_ids = Db::select_column(" |
@@ -457,30 +469,35 @@ discard block |
||
457 | 469 | $ignored_status = 'either'; |
458 | 470 | if ($tmp = $this->request->get_parameter('exclude_ignored')) |
459 | 471 | { |
460 | - if ($this->request->token == null) |
|
461 | - throw new InvalidParam('exclude_ignored', "Might be used only for requests signed with an Access Token."); |
|
462 | - if ($tmp == 'true') |
|
463 | - $ignored_status = 'notignored_only'; |
|
464 | - elseif ($tmp != 'false') |
|
465 | - throw new InvalidParam('exclude_ignored', "'$tmp'"); |
|
472 | + if ($this->request->token == null) { |
|
473 | + throw new InvalidParam('exclude_ignored', "Might be used only for requests signed with an Access Token."); |
|
474 | + } |
|
475 | + if ($tmp == 'true') { |
|
476 | + $ignored_status = 'notignored_only'; |
|
477 | + } elseif ($tmp != 'false') { |
|
478 | + throw new InvalidParam('exclude_ignored', "'$tmp'"); |
|
479 | + } |
|
466 | 480 | } |
467 | 481 | if ($tmp = $this->request->get_parameter('ignored_status')) |
468 | 482 | { |
469 | - if ($this->request->token == null) |
|
470 | - throw new InvalidParam('ignored_status', "Might be used only for requests signed with an Access Token."); |
|
471 | - if (!in_array($tmp, array('ignored_only', 'notignored_only', 'either'))) |
|
472 | - throw new InvalidParam('ignored_status', "'$tmp'"); |
|
483 | + if ($this->request->token == null) { |
|
484 | + throw new InvalidParam('ignored_status', "Might be used only for requests signed with an Access Token."); |
|
485 | + } |
|
486 | + if (!in_array($tmp, array('ignored_only', 'notignored_only', 'either'))) { |
|
487 | + throw new InvalidParam('ignored_status', "'$tmp'"); |
|
488 | + } |
|
473 | 489 | if ($tmp != 'either') { |
474 | - if ($tmp == 'ignored_only' && $ignored_status == 'notignored_only') |
|
475 | - $ignored_status = 'none'; |
|
476 | - else |
|
477 | - $ignored_status = $tmp; |
|
490 | + if ($tmp == 'ignored_only' && $ignored_status == 'notignored_only') { |
|
491 | + $ignored_status = 'none'; |
|
492 | + } else { |
|
493 | + $ignored_status = $tmp; |
|
494 | + } |
|
478 | 495 | } |
479 | 496 | } |
480 | 497 | |
481 | - if ($ignored_status == 'none') |
|
482 | - $where_conds[] = 'false'; |
|
483 | - elseif ($ignored_status != 'either') |
|
498 | + if ($ignored_status == 'none') { |
|
499 | + $where_conds[] = 'false'; |
|
500 | + } elseif ($ignored_status != 'either') |
|
484 | 501 | { |
485 | 502 | $ignored_cache_ids = Db::select_column(" |
486 | 503 | select cache_id |
@@ -497,12 +514,15 @@ discard block |
||
497 | 514 | |
498 | 515 | if ($tmp = $this->request->get_parameter('exclude_my_own')) |
499 | 516 | { |
500 | - if ($this->request->token == null) |
|
501 | - throw new InvalidParam('exclude_my_own', "Might be used only for requests signed with an Access Token."); |
|
502 | - if (!in_array($tmp, array('true', 'false'))) |
|
503 | - throw new InvalidParam('exclude_my_own', "'$tmp'"); |
|
504 | - if ($tmp == 'true') |
|
505 | - $where_conds[] = "caches.user_id != '".Db::escape_string($this->request->token->user_id)."'"; |
|
517 | + if ($this->request->token == null) { |
|
518 | + throw new InvalidParam('exclude_my_own', "Might be used only for requests signed with an Access Token."); |
|
519 | + } |
|
520 | + if (!in_array($tmp, array('true', 'false'))) { |
|
521 | + throw new InvalidParam('exclude_my_own', "'$tmp'"); |
|
522 | + } |
|
523 | + if ($tmp == 'true') { |
|
524 | + $where_conds[] = "caches.user_id != '".Db::escape_string($this->request->token->user_id)."'"; |
|
525 | + } |
|
506 | 526 | } |
507 | 527 | |
508 | 528 | # |
@@ -514,8 +534,9 @@ discard block |
||
514 | 534 | # WRTODO: Make this more user-friendly. See: |
515 | 535 | # https://github.com/opencaching/okapi/issues/121 |
516 | 536 | |
517 | - if (strlen($tmp) > 100) |
|
518 | - throw new InvalidParam('name', "Maximum length of 'name' parameter is 100 characters"); |
|
537 | + if (strlen($tmp) > 100) { |
|
538 | + throw new InvalidParam('name', "Maximum length of 'name' parameter is 100 characters"); |
|
539 | + } |
|
519 | 540 | $tmp = str_replace("*", "%", str_replace("%", "%%", $tmp)); |
520 | 541 | $where_conds[] = "caches.name LIKE '".Db::escape_string($tmp)."'"; |
521 | 542 | } |
@@ -526,8 +547,9 @@ discard block |
||
526 | 547 | |
527 | 548 | if ($tmp = $this->request->get_parameter('with_trackables_only')) |
528 | 549 | { |
529 | - if (!in_array($tmp, array('true', 'false'), 1)) |
|
530 | - throw new InvalidParam('with_trackables_only', "'$tmp'"); |
|
550 | + if (!in_array($tmp, array('true', 'false'), 1)) { |
|
551 | + throw new InvalidParam('with_trackables_only', "'$tmp'"); |
|
552 | + } |
|
531 | 553 | if ($tmp == 'true') |
532 | 554 | { |
533 | 555 | $where_conds[] = " |
@@ -545,8 +567,9 @@ discard block |
||
545 | 567 | |
546 | 568 | if ($tmp = $this->request->get_parameter('ftf_hunter')) |
547 | 569 | { |
548 | - if (!in_array($tmp, array('true', 'false'), 1)) |
|
549 | - throw new InvalidParam('ftf_hunter', "'$tmp'"); |
|
570 | + if (!in_array($tmp, array('true', 'false'), 1)) { |
|
571 | + throw new InvalidParam('ftf_hunter', "'$tmp'"); |
|
572 | + } |
|
550 | 573 | if ($tmp == 'true') |
551 | 574 | { |
552 | 575 | $where_conds[] = "$X_FOUNDS = 0"; |
@@ -603,8 +626,9 @@ discard block |
||
603 | 626 | from okapi_search_sets |
604 | 627 | where id = '".Db::escape_string($tmp)."' |
605 | 628 | "); |
606 | - if (!$exists) |
|
607 | - throw new InvalidParam('set_and', "Couldn't find a set by given ID."); |
|
629 | + if (!$exists) { |
|
630 | + throw new InvalidParam('set_and', "Couldn't find a set by given ID."); |
|
631 | + } |
|
608 | 632 | $extra_tables[] = "okapi_search_results osr_and"; |
609 | 633 | $where_conds[] = "osr_and.cache_id = caches.cache_id"; |
610 | 634 | $where_conds[] = "osr_and.set_id = '".Db::escape_string($tmp)."'"; |
@@ -615,34 +639,43 @@ discard block |
||
615 | 639 | # |
616 | 640 | |
617 | 641 | $limit = $this->request->get_parameter('limit'); |
618 | - if ($limit == null) $limit = "100"; |
|
619 | - if (!is_numeric($limit)) |
|
620 | - throw new InvalidParam('limit', "'$limit'"); |
|
621 | - if ($limit < 1 || (($limit > 500) && (!$this->request->skip_limits))) |
|
622 | - throw new InvalidParam( |
|
642 | + if ($limit == null) { |
|
643 | + $limit = "100"; |
|
644 | + } |
|
645 | + if (!is_numeric($limit)) { |
|
646 | + throw new InvalidParam('limit', "'$limit'"); |
|
647 | + } |
|
648 | + if ($limit < 1 || (($limit > 500) && (!$this->request->skip_limits))) { |
|
649 | + throw new InvalidParam( |
|
623 | 650 | 'limit', |
624 | 651 | $this->request->skip_limits |
625 | 652 | ? "Cannot be lower than 1." |
626 | 653 | : "Has to be between 1 and 500." |
627 | 654 | ); |
655 | + } |
|
628 | 656 | |
629 | 657 | # |
630 | 658 | # offset |
631 | 659 | # |
632 | 660 | |
633 | 661 | $offset = $this->request->get_parameter('offset'); |
634 | - if ($offset == null) $offset = "0"; |
|
635 | - if (!is_numeric($offset)) |
|
636 | - throw new InvalidParam('offset', "'$offset'"); |
|
637 | - if (($offset + $limit > 500) && (!$this->request->skip_limits)) |
|
638 | - throw new BadRequest("The sum of offset and limit may not exceed 500."); |
|
639 | - if ($offset < 0 || (($offset > 499) && (!$this->request->skip_limits))) |
|
640 | - throw new InvalidParam( |
|
662 | + if ($offset == null) { |
|
663 | + $offset = "0"; |
|
664 | + } |
|
665 | + if (!is_numeric($offset)) { |
|
666 | + throw new InvalidParam('offset', "'$offset'"); |
|
667 | + } |
|
668 | + if (($offset + $limit > 500) && (!$this->request->skip_limits)) { |
|
669 | + throw new BadRequest("The sum of offset and limit may not exceed 500."); |
|
670 | + } |
|
671 | + if ($offset < 0 || (($offset > 499) && (!$this->request->skip_limits))) { |
|
672 | + throw new InvalidParam( |
|
641 | 673 | 'offset', |
642 | 674 | $this->request->skip_limits |
643 | 675 | ? "Cannot be lower than 0." |
644 | 676 | : "Has to be between 0 and 499." |
645 | 677 | ); |
678 | + } |
|
646 | 679 | |
647 | 680 | # |
648 | 681 | # order_by |
@@ -660,9 +693,10 @@ discard block |
||
660 | 693 | { |
661 | 694 | $dir = 'desc'; |
662 | 695 | $field = substr($field, 1); |
696 | + } elseif ($field[0] == '+') { |
|
697 | + $field = substr($field, 1); |
|
663 | 698 | } |
664 | - elseif ($field[0] == '+') |
|
665 | - $field = substr($field, 1); # ignore leading "+" |
|
699 | + # ignore leading "+" |
|
666 | 700 | switch ($field) |
667 | 701 | { |
668 | 702 | case 'code': $cl = "caches.wp_oc"; break; |
@@ -682,8 +716,9 @@ discard block |
||
682 | 716 | # To avoid join errors, put each of the $where_conds in extra paranthesis. |
683 | 717 | |
684 | 718 | $tmp = array(); |
685 | - foreach($where_conds as $cond) |
|
686 | - $tmp[] = "(".$cond.")"; |
|
719 | + foreach($where_conds as $cond) { |
|
720 | + $tmp[] = "(".$cond.")"; |
|
721 | + } |
|
687 | 722 | $where_conds = $tmp; |
688 | 723 | unset($tmp); |
689 | 724 | |
@@ -767,8 +802,9 @@ discard block |
||
767 | 802 | public function prepare_location_search_params() |
768 | 803 | { |
769 | 804 | $location_source = $this->request->get_parameter('location_source'); |
770 | - if (!$location_source) |
|
771 | - $location_source = 'default-coords'; |
|
805 | + if (!$location_source) { |
|
806 | + $location_source = 'default-coords'; |
|
807 | + } |
|
772 | 808 | |
773 | 809 | # Make sure location_source has prefix alt_wpt: |
774 | 810 | if ($location_source != 'default-coords' && strncmp($location_source, 'alt_wpt:', 8) != 0) |
@@ -24,40 +24,52 @@ discard block |
||
24 | 24 | { |
25 | 25 | # Check search method |
26 | 26 | $search_method = $request->get_parameter('search_method'); |
27 | - if (!$search_method) |
|
28 | - throw new ParamMissing('search_method'); |
|
29 | - if (strpos($search_method, "services/caches/search/") !== 0) |
|
30 | - throw new InvalidParam('search_method', "Should begin with 'services/caches/search/'."); |
|
31 | - if (!OkapiServiceRunner::exists($search_method)) |
|
32 | - throw new InvalidParam('search_method', "Method does not exist: '$search_method'"); |
|
27 | + if (!$search_method) { |
|
28 | + throw new ParamMissing('search_method'); |
|
29 | + } |
|
30 | + if (strpos($search_method, "services/caches/search/") !== 0) { |
|
31 | + throw new InvalidParam('search_method', "Should begin with 'services/caches/search/'."); |
|
32 | + } |
|
33 | + if (!OkapiServiceRunner::exists($search_method)) { |
|
34 | + throw new InvalidParam('search_method', "Method does not exist: '$search_method'"); |
|
35 | + } |
|
33 | 36 | $search_params = $request->get_parameter('search_params'); |
34 | - if (!$search_params) |
|
35 | - throw new ParamMissing('search_params'); |
|
37 | + if (!$search_params) { |
|
38 | + throw new ParamMissing('search_params'); |
|
39 | + } |
|
36 | 40 | $search_params = json_decode($search_params, true); |
37 | - if (!is_array($search_params)) |
|
38 | - throw new InvalidParam('search_params', "Should be a JSON-encoded dictionary"); |
|
41 | + if (!is_array($search_params)) { |
|
42 | + throw new InvalidParam('search_params', "Should be a JSON-encoded dictionary"); |
|
43 | + } |
|
39 | 44 | |
40 | 45 | # Check retrieval method |
41 | 46 | $retr_method = $request->get_parameter('retr_method'); |
42 | - if (!$retr_method) |
|
43 | - throw new ParamMissing('retr_method'); |
|
44 | - if (!OkapiServiceRunner::exists($retr_method)) |
|
45 | - throw new InvalidParam('retr_method', "Method does not exist: '$retr_method'"); |
|
47 | + if (!$retr_method) { |
|
48 | + throw new ParamMissing('retr_method'); |
|
49 | + } |
|
50 | + if (!OkapiServiceRunner::exists($retr_method)) { |
|
51 | + throw new InvalidParam('retr_method', "Method does not exist: '$retr_method'"); |
|
52 | + } |
|
46 | 53 | $retr_params = $request->get_parameter('retr_params'); |
47 | - if (!$retr_params) |
|
48 | - throw new ParamMissing('retr_params'); |
|
54 | + if (!$retr_params) { |
|
55 | + throw new ParamMissing('retr_params'); |
|
56 | + } |
|
49 | 57 | $retr_params = json_decode($retr_params, true); |
50 | - if (!is_array($retr_params)) |
|
51 | - throw new InvalidParam('retr_params', "Should be a JSON-encoded dictionary"); |
|
58 | + if (!is_array($retr_params)) { |
|
59 | + throw new InvalidParam('retr_params', "Should be a JSON-encoded dictionary"); |
|
60 | + } |
|
52 | 61 | |
53 | 62 | self::map_values_to_strings($search_params); |
54 | 63 | self::map_values_to_strings($retr_params); |
55 | 64 | |
56 | 65 | # Wrapped? |
57 | 66 | $wrap = $request->get_parameter('wrap'); |
58 | - if ($wrap == null) throw new ParamMissing('wrap'); |
|
59 | - if (!in_array($wrap, array('true', 'false'))) |
|
60 | - throw new InvalidParam('wrap'); |
|
67 | + if ($wrap == null) { |
|
68 | + throw new ParamMissing('wrap'); |
|
69 | + } |
|
70 | + if (!in_array($wrap, array('true', 'false'))) { |
|
71 | + throw new InvalidParam('wrap'); |
|
72 | + } |
|
61 | 73 | $wrap = ($wrap == 'true'); |
62 | 74 | |
63 | 75 | # Run search method |
@@ -65,8 +77,7 @@ discard block |
||
65 | 77 | { |
66 | 78 | $search_result = OkapiServiceRunner::call($search_method, new OkapiInternalRequest( |
67 | 79 | $request->consumer, $request->token, $search_params)); |
68 | - } |
|
69 | - catch (BadRequest $e) |
|
80 | + } catch (BadRequest $e) |
|
70 | 81 | { |
71 | 82 | throw new InvalidParam('search_params', "Search method responded with the ". |
72 | 83 | "following error message: ".$e->getMessage()); |
@@ -78,8 +89,7 @@ discard block |
||
78 | 89 | $retr_result = OkapiServiceRunner::call($retr_method, new OkapiInternalRequest( |
79 | 90 | $request->consumer, $request->token, array_merge($retr_params, |
80 | 91 | array('cache_codes' => implode("|", $search_result['results']))))); |
81 | - } |
|
82 | - catch (BadRequest $e) |
|
92 | + } catch (BadRequest $e) |
|
83 | 93 | { |
84 | 94 | throw new InvalidParam('retr_params', "Retrieval method responded with the ". |
85 | 95 | "following error message: ".$e->getMessage()); |
@@ -89,21 +99,23 @@ discard block |
||
89 | 99 | { |
90 | 100 | # $retr_result might be a PHP object, but also might be a binary response |
91 | 101 | # (e.g. a GPX file). |
92 | - if ($retr_result instanceof OkapiHttpResponse) |
|
93 | - $result = array('results' => $retr_result->get_body()); |
|
94 | - else |
|
95 | - $result = array('results' => $retr_result); |
|
96 | - foreach ($search_result as $key => &$value_ref) |
|
97 | - if ($key != 'results') |
|
102 | + if ($retr_result instanceof OkapiHttpResponse) { |
|
103 | + $result = array('results' => $retr_result->get_body()); |
|
104 | + } else { |
|
105 | + $result = array('results' => $retr_result); |
|
106 | + } |
|
107 | + foreach ($search_result as $key => &$value_ref) { |
|
108 | + if ($key != 'results') |
|
98 | 109 | $result[$key] = $value_ref; |
110 | + } |
|
99 | 111 | return Okapi::formatted_response($request, $result); |
100 | - } |
|
101 | - else |
|
112 | + } else |
|
102 | 113 | { |
103 | - if ($retr_result instanceof OkapiHttpResponse) |
|
104 | - return $retr_result; |
|
105 | - else |
|
106 | - return Okapi::formatted_response($request, $retr_result); |
|
114 | + if ($retr_result instanceof OkapiHttpResponse) { |
|
115 | + return $retr_result; |
|
116 | + } else { |
|
117 | + return Okapi::formatted_response($request, $retr_result); |
|
118 | + } |
|
107 | 119 | } |
108 | 120 | } |
109 | 121 | |
@@ -112,10 +124,11 @@ discard block |
||
112 | 124 | foreach (array_keys($dict) as $key) |
113 | 125 | { |
114 | 126 | $val = $dict[$key]; |
115 | - if (is_numeric($val) || is_string($val)) |
|
116 | - $dict[$key] = (string)$val; |
|
117 | - else |
|
118 | - throw new BadRequest("Invalid value format for key: ".$key); |
|
127 | + if (is_numeric($val) || is_string($val)) { |
|
128 | + $dict[$key] = (string)$val; |
|
129 | + } else { |
|
130 | + throw new BadRequest("Invalid value format for key: ".$key); |
|
131 | + } |
|
119 | 132 | } |
120 | 133 | } |
121 | 134 | } |