function _get_nsid($flickr_user, $more = array())
{
    # TO DO: put this all in a function somewhere and
    # call/queue it when a user signs up...
    # As db_main:Users
    $user = users_get_by_id($flickr_user['user_id']);
    if (!$user) {
        return;
    }
    $method = 'flickr.people.getInfo';
    $args = array('user_id' => $flickr_user['nsid']);
    $ret = flickr_api_call($method, $args);
    if (!$ret['ok']) {
        dumper($args);
        dumper($ret);
        return;
    }
    $rsp = $ret['rsp']['person'];
    $path_alias = $rsp['path_alias'];
    $username = $rsp['username']['_content'];
    echo "[{$user['id']}] path alias: {$path_alias} screen name: {$username}\n";
    if ($path_alias != $flickr_user['path_alias']) {
        $update = array('path_alias' => $path_alias);
        $rsp = flickr_users_update_user($flickr_user, $update);
        echo "[{$user['id']}] update path alias: {$rsp['ok']}\n";
        # just let this fail silently if there's a duplicate
        flickr_users_path_aliases_create($user, $path_alias);
    }
    if ($username != $user['username']) {
        $update = array('username' => $username);
        $rsp = users_update_user($user, $update);
        echo "[{$user['id']}] update username: {$rsp['ok']}\n";
    }
}
function flickr_users_create_user($flickr_user)
{
    $hash = array();
    foreach ($flickr_user as $k => $v) {
        $hash[$k] = AddSlashes($v);
    }
    $rsp = db_insert('FlickrUsers', $hash);
    if (!$rsp['ok']) {
        return null;
    }
    # hey look! this is parallel-flickr specific and not part
    # of flamework-flickrapp (20111202/straup)
    # just add the path_alias to the FlickrUsersPathAliases
    # table even if the feature flag is disabled so that it
    # will do the right thing if/when it is enabled.
    # (20111202/straup)
    if ($flickr_user['path_alias']) {
        loadlib("flickr_users_path_aliases");
        $user = users_get_by_id($flickr_user['user_id']);
        $rsp = flickr_users_path_aliases_create($user, $flickr_user['path_alias']);
        # Okay, so this (mysql's duplicate key (1062) error) means that a
        # parallel-flickr user has managed to change their path alias to be
        # the same as something that a person on flickr chose *after* the
        # local user changed their path alias. So, because parallel-flickr
        # is not meant to be a global mirror of the entirety of flickr we're
        # going to flag the record for $flickr_user and update lib_flickr_urls
        # to only ever use that user's NSID when building URLs. This is not
        # a perfect solution in that if you did a GET on the path_alias in
        # question it would redirect to the local user who "stole" the path
        # alias from the (newer) flickr user but in a perverse kind of way it
        # is the right thing to do. But only if path_alias redirects are enabled
        # which is why we always store what flickr tells us against the FlickrUsers
        # table. (20111202/straup)
        if (!$rsp['ok'] && $rsp['error_code'] == 1062) {
            $alias = flickr_users_path_aliases_get_by_alias($flickr_user['path_alias']);
            # unless of course the two users are the same, which shouldn't
            # ever happen but that's what makes life interesting, right?
            if ($alias['user_id'] != $flickr_user['user_id']) {
                $update = array('path_alias_taken_by' => $alias['user_id']);
                $update_rsp = flickr_users_update_user($flickr_user, $update);
                # note the caching below
                if ($update_rsp['ok']) {
                    $flickr_user = array_merge($flickr_user, $update);
                }
            }
        }
    }
    $cache_key = "flickr_user_{$flickr_user['nsid']}";
    cache_set($cache_key, $flickr_user, "cache locally");
    $cache_key = "flickr_user_{$flickr_user['user_id']}";
    cache_set($cache_key, $flickr_user, "cache locally");
    return $flickr_user;
}
$flickr_user = flickr_users_get_by_nsid($nsid);
if ($flickr_user) {
    $user = users_get_by_id($flickr_user['user_id']);
    $change = 0;
    if (!$flickr_user['auth_token']) {
        $change = 1;
    }
    if ($flickr_user['auth_token'] != $token) {
        $change = 1;
    }
    if ($flickr_user['token_perms'] != $perms) {
        $change = 1;
    }
    if ($change) {
        $update = array('auth_token' => $token, 'token_perms' => $perms);
        $rsp = flickr_users_update_user($flickr_user, $update);
        if (!$rsp['ok']) {
            $GLOBALS['error']['dberr_flickruser_update'] = 1;
            $GLOBALS['smarty']->display("page_auth_callback_flickr_flickrauth.txt");
            exit;
        }
    }
} else {
    if (!$GLOBALS['cfg']['enable_feature_signup']) {
        $GLOBALS['smarty']->display("page_signup_disabled.txt");
        exit;
    } else {
        $password = random_string(32);
        $user = users_create_user(array("username" => $username, "email" => "{$username}@donotsend-flickr.com", "password" => $password));
        if (!$user) {
            $GLOBALS['error']['dberr_user'] = 1;