$smarty->assign('status', unserialize($token->getValueBinary("s")));
        $smarty->assign('filenames', unserialize($token->getValueBinary("f")));
    }
} elseif (isset($_POST['selected'])) {
    //we dont get the button :(
    $status = array();
    $filenames = array();
    foreach ($_POST['field'] as $key => $value) {
        $uploadmanager = new UploadManager();
        $square = new GridSquare();
        $files_key = str_replace('.', '_', $value);
        $filenames[$key] = $_FILES[$files_key]['name'];
        $ok = $square->setByFullGridRef($_POST['grid_reference'][$key]);
        if ($ok) {
            // set up attributes from uploaded data
            $uploadmanager->setSquare($square);
            $uploadmanager->setViewpoint($_POST['photographer_gridref'][$key]);
            $uploadmanager->setDirection($_POST['view_direction'][$key]);
            $uploadmanager->setUse6fig(stripslashes($_POST['use6fig'][$key]));
            $uploadmanager->setTaken($_POST['imagetaken'][$key]);
            $uploadmanager->setTitle(utf8_decode($_POST['title'][$key]));
            $uploadmanager->setTitle2(utf8_decode($_POST['title2'][$key]));
            if ($_POST['comment'][$key] != "comment[{$key}]") {
                //bug? in Picasa sends the name in the value if blank, useful! (but only seems to apply to textareas)
                $uploadmanager->setComment(utf8_decode($_POST['comment'][$key]));
            }
            if ($_POST['comment2'][$key] != "comment2[{$key}]") {
                //bug? in Picasa sends the name in the value if blank, useful! (but only seems to apply to textareas)
                $uploadmanager->setComment2(utf8_decode($_POST['comment2'][$key]));
            }
            if (($_POST['imageclass'][$key] == 'Other' || empty($_POST['imageclass'][$key])) && !empty($_POST['imageclassother'][$key])) {
function UploadPicture()
{
    global $CONF;
    global $xml;
    global $USER;
    if (empty($_POST['userid']) || !intval($_POST['userid'])) {
        $xml['status'] = 'Not Logged In';
        returnXML();
    } else {
        $USER = new GeographUser(intval($_POST['userid']));
        //TODO: check validation hash?
        if ($_POST['validation'] != md5($_POST['userid'] . '#' . $CONF['register_confirmation_secret'])) {
            $xml['status'] = 'User not verified';
            returnXML();
        }
        if (!$USER->user_id || !$USER->hasPerm('basic')) {
            $xml['status'] = 'Not authorised to post';
            returnXML();
        }
    }
    $um = new UploadManager();
    $gs = new GridSquare();
    // this is the check that we like the client and any image has
    // come in with the appropriate cc licence
    $ccl = $_POST['cclicence'];
    if ($ccl != "I grant you the permission to use this submission " . "under the terms of the Creative Commons by-sa-2.0 licence") {
        $xml['status'] = 'Bad client submission';
        returnXML();
    }
    // validate the grid square - we may be going back to the user
    // quickly here :-)
    $gs->setByFullGridRef($_POST['subject']);
    if ($gs->errormsg != "") {
        $xml['status'] = $gs->errormsg;
        returnXML();
    }
    $takendate = strtotime_uk($_POST['date']);
    if ($takendate > time()) {
        $xml['status'] = "Date taken in future";
        returnXML();
    }
    // set up attributes from uploaded data
    $um->setSquare($gs);
    $um->setViewpoint($_POST['photographer']);
    $um->setDirection($_POST['direction']);
    $um->setTaken(date('Y-m-d', $takendate));
    $um->setTitle($_POST['title']);
    $um->setComment($_POST['comments']);
    $um->setClass($_POST['feature']);
    $um->setUserStatus($_POST['supplemental']);
    $um->processUpload($_FILES['uploadfile']['tmp_name']);
    // where there any errors back from the image processing?
    // if so, JUppy needs to know...
    if ($um->error != "") {
        $xml['status'] = $um->error;
    } else {
        // so far so good... can we commit the submission?
        $rc = $um->commit();
        if ($rc == "") {
            //clear user profile
            $ab = floor($USER->user_id / 10000);
            $smarty = new GeographPage();
            $smarty->clear_cache(null, "user{$ab}|{$USER->user_id}");
            $xml['status'] = "OK";
        } else {
            $xml['status'] = $rc;
        }
    }
    returnXML();
}