示例#1
0
文件: osmapi.php 项目: tyrasd/Level0
function oauth_upload($comment, $data)
{
    global $messages, $error;
    if (!isset($_SESSION['osm_token']) || !isset($_SESSION['osm_secret'])) {
        $error = _('OAuth token was lost, please relogin.');
        oauth_logout();
        return false;
    }
    try {
        $stage = 'login';
        $oauth = new OAuth(CLIENT_ID, CLIENT_SECRET, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
        $oauth->setToken($_SESSION['osm_token'], $_SESSION['osm_secret']);
        $change_data = create_changeset($data, $comment);
        $xml_content = array('Content-Type' => 'application/xml');
        $stage = 'create';
        // note: this call works instead of returning 401 because of $xml_content
        $oauth->fetch(OSM_API_URL . 'changeset/create', $change_data, OAUTH_HTTP_METHOD_PUT, $xml_content);
        if (!preg_match('/\\d+/', $oauth->getLastResponse(), $m)) {
            $error = _('Could not aquire changeset id for a new changeset.');
            return false;
        }
        $changeset = $m[0];
        $osc = create_osc($data, $changeset);
        $stage = 'upload';
        $oauth->fetch(OSM_API_URL . 'changeset/' . $changeset . '/upload', $osc, OAUTH_HTTP_METHOD_POST, $xml_content);
        // todo: parse response and renumber created objects?
        $stage = 'close';
        $oauth->fetch(OSM_API_URL . 'changeset/' . $changeset . '/close', array(), OAUTH_HTTP_METHOD_PUT);
        $chlink = '<a href="https://www.openstreetmap.org/changeset/' . $changeset . '" target="_blank">' . $changeset . '</a>';
        // todo: replace %d with %s and $chlink, removing str_replace
        $messages[] = '!' . str_replace($changeset, $chlink, sprintf(_('Changeset %d was uploaded successfully.'), $changeset));
        return true;
    } catch (OAuthException $E) {
        if ($stage == 'upload' && $E->getCode() == 409) {
            $error = sprintf(_('Conflict while uploading changeset %d: %s.'), $changeset, $oauth->getLastResponse());
            // todo: process conflict
            // http://wiki.openstreetmap.org/wiki/API_0.6#Error_codes_9
        } else {
            print_r($E);
            $msg = $oauth->getLastResponse();
            $error = sprintf(_('OAuth error %d at stage "%s": %s.'), $E->getCode(), $stage, $msg ? $msg : $E->getMessage());
        }
    }
    return false;
}
示例#2
0
文件: index.php 项目: tyrasd/Level0
        if (oauth_upload(trim(isset($_REQUEST['comment']) ? $_REQUEST['comment'] : ''), $e)) {
            clear_data();
            $text = '';
            $validation = array();
            $_REQUEST['comment'] = '';
        }
        $loggedin = isset($_SESSION['osm_token']);
    }
} elseif (isset($_REQUEST['check'])) {
    update_modified();
}
$osccontent = '';
if (isset($_REQUEST['showosc']) && strlen($_REQUEST['showosc']) > 0) {
    $e = prepare_export();
    if (is_array($e)) {
        $osccontent = create_osc($e, 1234);
    } else {
        $osccontent = sprintf(_('Error preparing data: %s.'), $e);
    }
}
// Restore map parameters
$center = calculate_center();
$center_r = false;
if (!$center && isset($_REQUEST['center']) && preg_match('/^-?\\d{1,2}(?:\\.\\d+)?,-?\\d{1,3}(?:\\.\\d+)?$/', $_REQUEST['center'])) {
    $center = explode(',', $_REQUEST['center']);
    $center_r = true;
}
$zoom = $center ? 17 : ($center_r ? 15 : 2);
if (!$center) {
    $center = array(30, 0);
}