|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace okapi\services\caches\edit; |
| 4 | + |
| 5 | +use okapi\core\Okapi; |
| 6 | +use okapi\core\Db; |
| 7 | +use okapi\core\Exception\BadRequest; |
| 8 | +use okapi\core\Exception\InvalidParam; |
| 9 | +use okapi\core\Exception\ParamMissing; |
| 10 | +use okapi\core\Request\OkapiRequest; |
| 11 | +use okapi\core\Request\OkapiInternalRequest; |
| 12 | +use okapi\core\OkapiServiceRunner; |
| 13 | +use okapi\Settings; |
| 14 | + |
| 15 | + |
| 16 | +class WebService |
| 17 | +{ |
| 18 | + public static function options() |
| 19 | + { |
| 20 | + return array( |
| 21 | + 'min_auth_level' => 3, |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + public static function call(OkapiRequest $request) |
| 26 | + { |
| 27 | + $cache_code = $request->get_parameter('cache_code'); |
| 28 | + if ($cache_code == null) |
| 29 | + throw new ParamMissing('cache_code'); |
| 30 | + $geocache = OkapiServiceRunner::call( |
| 31 | + 'services/caches/geocache', |
| 32 | + new OkapiInternalRequest( |
| 33 | + $request->consumer, |
| 34 | + $request->token, |
| 35 | + array('cache_code' => $cache_code, 'fields' => 'internal_id|type|date_created') |
| 36 | + ) |
| 37 | + ); |
| 38 | + $internal_id_escaped = Db::escape_string($geocache['internal_id']); |
| 39 | + $owner_id = Db::select_value( |
| 40 | + "select user_id from caches where cache_id = '".$internal_id_escaped."'" |
| 41 | + ); |
| 42 | + if ($owner_id != $request->token->user_id) |
| 43 | + throw new BadRequest("Only own caches may be edited."); |
| 44 | + |
| 45 | + $problems = []; |
| 46 | + $change_sqls_escaped = []; |
| 47 | + |
| 48 | + $langpref = $request->get_parameter('langpref'); |
| 49 | + if (!$langpref) $langpref = "en"; |
| 50 | + $langprefs = explode("|", $langpref); |
| 51 | + |
| 52 | + Okapi::gettext_domain_init($langprefs); |
| 53 | + try |
| 54 | + { |
| 55 | + # passwd |
| 56 | + $newpw = $request->get_parameter('passwd'); |
| 57 | + if ($newpw !== null) |
| 58 | + { |
| 59 | + $installation = OkapiServiceRunner::call( |
| 60 | + 'services/apisrv/installation', |
| 61 | + new OkapiInternalRequest($request->consumer, $request->token, []) |
| 62 | + ); |
| 63 | + if (strlen($newpw) > $installation['geocache_passwd_max_length']) { |
| 64 | + $problems['passwd'] = sprintf( |
| 65 | + _('The password must not be longer than %d characters.'), |
| 66 | + $installation['geocache_passwd_max_length'] |
| 67 | + ); |
| 68 | + } elseif ( |
| 69 | + Settings::get('OC_BRANCH') == 'oc.pl' && |
| 70 | + $geocache['type'] == 'Traditional' && |
| 71 | + $geocache['date_created'] > '2010-06-18 20:03:18' |
| 72 | + ) { |
| 73 | + # We won't bother the user with the creation date thing here. |
| 74 | + # The *current* rule is that OCPL sites do not allow tradi passwords. |
| 75 | + # For older caches, the user won't see this message. |
| 76 | + |
| 77 | + $problems['passwd'] = sprintf( |
| 78 | + _('%s does not allow log passwords for traditional caches.'), |
| 79 | + Okapi::get_normalized_site_name() |
| 80 | + ); |
| 81 | + } else { |
| 82 | + $oldpw = Db::select_value("select logpw from caches where cache_id='".$internal_id_escaped."'"); |
| 83 | + if ($newpw != $oldpw) |
| 84 | + $change_sqls_escaped[] = "logpw = '".Db::escape_string($newpw)."'"; |
| 85 | + unset($oldpw); |
| 86 | + } |
| 87 | + } |
| 88 | + unset($newpw); |
| 89 | + |
| 90 | + Okapi::gettext_domain_restore(); |
| 91 | + } |
| 92 | + catch (Exception $e) |
| 93 | + { |
| 94 | + Okapi::gettext_domain_restore(); |
| 95 | + throw $e; |
| 96 | + } |
| 97 | + |
| 98 | + # save changes |
| 99 | + if (count($problems) == 0 && count($change_sqls_escaped) > 0) { |
| 100 | + Db::execute(" |
| 101 | + update caches |
| 102 | + set " . implode(', ', $change_sqls_escaped) . ", last_modified=NOW() |
| 103 | + where cache_id = '".$internal_id_escaped."' |
| 104 | + "); |
| 105 | + } |
| 106 | + |
| 107 | + $result = ['success' => count($problems) == 0, 'messages' => $problems]; |
| 108 | + |
| 109 | + Okapi::update_user_activity($request); |
| 110 | + return Okapi::formatted_response($request, $result); |
| 111 | + } |
| 112 | +} |
0 commit comments