Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions okapi/core/OkapiServiceRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class OkapiServiceRunner
'services/caches/geocaches',
'services/caches/mark',
'services/caches/save_personal_notes',
'services/caches/save_user_coords',
'services/caches/formatters/gpx',
'services/caches/formatters/garmin',
'services/caches/formatters/ggz',
Expand Down
144 changes: 144 additions & 0 deletions okapi/services/caches/save_user_coords/WebService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace okapi\services\caches\save_user_coords;

use okapi\core\Db;
use okapi\core\Exception\ParamMissing;
use okapi\core\Exception\InvalidParam;
use okapi\core\Okapi;
use okapi\core\OkapiServiceRunner;
use okapi\core\Request\OkapiInternalRequest;
use okapi\core\Request\OkapiRequest;
use okapi\Settings;

class WebService
{
public static function options()
{
return array(
'min_auth_level' => 3
);
}

public static function call(OkapiRequest $request)
{

$user_coords = $request->get_parameter('user_coords');
if ($user_coords == null)
throw new ParamMissing('user_coords');
$parts = explode('|', $user_coords);
if (count($parts) != 2)
throw new InvalidParam('user_coords', "Expecting 2 pipe-separated parts, got ".count($parts).".");
foreach ($parts as &$part_ref)
{
if (!preg_match("/^-?[0-9]+(\.?[0-9]*)$/", $part_ref))
throw new InvalidParam('user_coords', "'$part_ref' is not a valid float number.");
$part_ref = floatval($part_ref);
}
list($latitude, $longitude) = $parts;

# Verify cache_code

$cache_code = $request->get_parameter('cache_code');
if ($cache_code == null)
throw new ParamMissing('cache_code');
$geocache = OkapiServiceRunner::call(
'services/caches/geocache',
new OkapiInternalRequest($request->consumer, $request->token, array(
'cache_code' => $cache_code,
'fields' => 'internal_id'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'fields' => 'internal_id'
'fields' => 'internal_id|type'

))
);
$cache_id = $geocache['internal_id'];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self::validate_cache_type($geocache['type']);

self::update_coordinates($cache_id, $request->token->user_id, $latitude, $longitude);

$result = array(
'success' => true
);
return Okapi::formatted_response($request, $result);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static function validate_cache_type($cache_type)
{
if (Settings::get('OC_BRANCH') != 'oc.pl') {
return;
}
$allowed_types = array(
'Other',
'Quiz',
'Multi',
);
if (!in_array($cache_type, $allowed_types, true)) {
throw new InvalidParam(
'cache_code',
"User coordinates are not supported for cache type '$cache_type'."
);
}
}

private static function update_coordinates($cache_id, $user_id, $latitude, $longitude)
{
if (Settings::get('OC_BRANCH') == 'oc.de')
{

/* See:
*
* - https://github.com/OpencachingDeutschland/oc-server3/tree/development/htdocs/src/Oc/Libse/CacheNote
* - https://www.opencaching.de/okapi/devel/dbstruct
*/

$rs = Db::query("
select max(id) as id
from coordinates
where
type = 2 -- personal note
and cache_id = '".Db::escape_string($cache_id)."'
and user_id = '".Db::escape_string($user_id)."'
");
Comment thread
hxdimpf marked this conversation as resolved.
$id = null;
if($row = Db::fetch_assoc($rs)) {
$id = $row['id'];
}
if ($id == null) {
Db::query("
insert into coordinates (
type, latitude, longitude, cache_id, user_id, description
) values (
2,
'".Db::escape_string($latitude)."',
'".Db::escape_string($longitude)."',
'".Db::escape_string($cache_id)."',
'".Db::escape_string($user_id)."',
''
)
");
} else {
Db::query("
update coordinates
set latitude = '".Db::escape_string($latitude)."',
longitude = '".Db::escape_string($longitude)."'
where
id = '".Db::escape_string($id)."'
and type = 2
");
}
}
else # oc.pl branch
{
$rs = Db::query("
select max(id) as id
from cache_mod_cords
where
cache_id = '".Db::escape_string($cache_id)."'
and user_id = '".Db::escape_string($user_id)."'
");
$id = null;
if($row = Db::fetch_assoc($rs)) {
$id = $row['id'];
}
if ($id == null) {
Db::query("
insert into cache_mod_cords (
cache_id, user_id, latitude, longitude
) values (
'".Db::escape_string($cache_id)."',
'".Db::escape_string($user_id)."',
'".Db::escape_string($latitude)."',
'".Db::escape_string($longitude)."'
)
");
} else {
Db::query("
update cache_mod_cords
set latitude = '".Db::escape_string($latitude)."',
longitude = '".Db::escape_string($longitude)."'
where
id = '".Db::escape_string($id)."'
");
}
}
}
Comment on lines +127 to +150

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OCPL cache_mod_cords table does not contain an id column.

create table cache_mod_cords
(
	cache_id int not null,
	user_id int not null,
	date timestamp default current_timestamp() not null,
	longitude double not null,
	latitude double not null,
	primary key (cache_id, user_id)
)

it should look something like this:

else # oc.pl branch
{
    Db::query("
        INSERT INTO cache_mod_cords (
            cache_id,
            user_id,
            latitude,
            longitude,
            date
        ) VALUES (
            '".Db::escape_string($cache_id)."',
            '".Db::escape_string($user_id)."',
            '".Db::escape_string($latitude)."',
            '".Db::escape_string($longitude)."',
            NOW()
        )
        ON DUPLICATE KEY UPDATE
            latitude = VALUES(latitude),
            longitude = VALUES(longitude),
            date = NOW()
    ");
}

Additionally, in OCPL only some cache types support user coordinates:
https://github.com/opencaching/opencaching-pl/blob/30ae3a4681c6840fe5aad454ccdb50fdb1340603/src/Controllers/ViewCacheController.php#L646-L648

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have commited your suggested else path.

With respect go the cache types that support user coordinates: in OCDE all do. I don't know how to access ocpl source code so pls send me the list of cache types that do support it then I will add the filtering.

}
29 changes: 29 additions & 0 deletions okapi/services/caches/save_user_coords/docs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<xml>
<brief>Update personal coordinates of a geocache</brief>
Comment thread
hxdimpf marked this conversation as resolved.
<issue-id>629</issue-id>
<desc>
<p>This method allows your users to update the coordinates of their
personal geocache coordinates.</p>

<p>Current personal coordinates for the geocache can be retrieved
using the <b>alt_wpts</b> field in the
<a href="%OKAPI:methodargref:services/caches/geocache#fields%">services/caches/geocache</a>
method.</p>
Comment thread
hxdimpf marked this conversation as resolved.
</desc>
<req name='cache_code'>
<p>Code of the geocache</p>
</req>
<req name='user_coords'>
<p>The coordinates are defined by a string in the format "lat|lon"</p>
<p>Use positive numbers for latitudes in the northern hemisphere and longitudes
in the eastern hemisphere (and negative for southern and western hemispheres
accordingly). These are full degrees with a dot as a decimal point (ex. "48.7|15.89").</p>
</req>
<common-format-params/>
<returns>
<p>A dictionary of the following structure:</p>
<ul>
<li>success - true</li>
</ul>
</returns>
</xml>