function flickr_geobookmarks_import_for_nsid($nsid, $more = array())
{
    $flickr_user = flickr_users_get_by_nsid($nsid);
    $user = users_get_by_id($flickr_user['user_id']);
    if (!$user) {
        return not_okay("Not a valid user");
    }
    $flickr_user = flickr_users_get_by_user_id($user['id']);
    $method = 'flickr.people.geoBookmarks.getList';
    $args = array('auth_token' => $flickr_user['auth_token']);
    $rsp = flickr_api_call($method, $args);
    if (!$rsp['ok']) {
        return $rsp;
    }
    if (!$rsp['rsp']['bookmarks']['count']) {
        return okay();
    }
    $bookmarks = array();
    # mark everything as private for now since none of that stuff
    # got turned on before I left, sad face... (20120217/straup)
    $geo_perms = flickr_geo_permissions_map("string keys");
    $geo_private = $geo_perms['private'];
    foreach ($rsp['rsp']['bookmarks']['bookmark'] as $bm) {
        $bm['user_id'] = $user['id'];
        $bm['name'] = $bm['label'];
        $bm['geocontext'] = $bm['context'];
        $bm['geoperms'] = $geo_private;
        $bm['woeid'] = 0;
        unset($bm['label']);
        unset($bm['pretty_name']);
        unset($bm['context']);
        $geo_method = 'flickr.places.findByLatLon';
        $geo_args = array('lat' => $bm['latitude'], 'lon' => $bm['longitude'], 'accuracy' => $bm['accuracy']);
        $geo_rsp = flickr_api_call($geo_method, $geo_args);
        if ($geo_rsp['ok']) {
            # I still miss xpath...
            $bm['woeid'] = $geo_rsp['rsp']['places']['place'][0]['woeid'];
        }
        $bookmarks[] = $bm;
    }
    $rsp = flickr_geobookmarks_purge_for_user($user);
    if (!$rsp['ok']) {
        return $rsp;
    }
    $count = 0;
    foreach ($bookmarks as $bm) {
        $rsp = flickr_geobookmarks_add($bm);
        $count += $rsp['ok'];
    }
    return okay(array('count_imported' => $count));
}
function flickr_contacts_import_for_nsid($nsid, $more = array())
{
    $flickr_user = flickr_users_get_by_nsid($nsid);
    $user = users_get_by_id($flickr_user['user_id']);
    if (!$user) {
        return array('ok' => 0, 'error' => 'not a valid user');
    }
    $method = 'flickr.contacts.getList';
    $count_contacts = 0;
    $args = array('auth_token' => $flickr_user['auth_token'], 'per_page' => 100, 'page' => 1);
    $pages = null;
    while (!isset($pages) || $pages >= $args['page']) {
        $rsp = flickr_api_call($method, $args);
        if (!$rsp) {
            return array('ok' => 0, 'error' => 'The Flickr API is wigging out...');
        }
        if (!isset($pages)) {
            $pages = $rsp['rsp']['contacts']['pages'];
        }
        $contacts = $rsp['rsp']['contacts']['contact'];
        if (!is_array($contacts)) {
            return array('ok' => 0, 'error' => 'The Flickr API did not return any contacts');
        }
        foreach ($contacts as $contact) {
            $contact_nsid = $contact['nsid'];
            $contact_username = $contact['username'];
            $flickr_contact = flickr_users_get_by_nsid($contact_nsid);
            if (!$flickr_contact) {
                $password = random_string(32);
                $user_contact = users_create_user(array("username" => $contact_username, "email" => "{$contact_username}@donotsend-flickr.com", "password" => $password));
                #
                $method = 'flickr.people.getInfo';
                $args = array('user_id' => $contact_nsid);
                $rsp = flickr_api_call($method, $args);
                $path_alias = $rsp['ok'] ? $rsp['rsp']['person']['path_alias'] : '';
                #
                $flickr_contact = flickr_users_create_user(array('user_id' => $user_contact['id'], 'nsid' => $contact_nsid, 'path_alias' => $path_alias));
            }
            $rel = flickr_contacts_calculate_relationship($contact);
            # echo "{$contact_username} : {$rel} ({$contact['friend']} {$contact['family']})\n";
            $insert = array('user_id' => $user['id'], 'contact_id' => $flickr_contact['user_id'], 'rel' => $rel);
            $contact = flickr_contacts_add_contact($insert);
            $count_contacts++;
        }
        $args['page'] += 1;
    }
    return array('ok' => 1, 'count_imported' => $count_contacts);
}
function flickr_faves_import_for_nsid($nsid, $more = array())
{
    $flickr_user = flickr_users_get_by_nsid($nsid);
    $user = users_get_by_id($flickr_user['user_id']);
    if (!$user) {
        return array('ok' => 0, 'error' => 'not a valid user');
    }
    $method = 'flickr.favorites.getList';
    $args = array('user_id' => $flickr_user['nsid'], 'auth_token' => $flickr_user['auth_token'], 'extras' => 'original_format,tags,media,date_upload,date_taken,geo,owner_name', 'per_page' => 100, 'page' => 1);
    if (isset($more['min_fave_date'])) {
        $args['min_fave_date'] = $more['min_fave_date'];
    }
    $pages = null;
    $count = 0;
    while (!isset($pages) || $pages >= $args['page']) {
        $rsp = flickr_api_call($method, $args);
        if (!$rsp['ok']) {
            return $rsp;
        }
        if (!isset($pages)) {
            $pages = $rsp['rsp']['photos']['pages'];
        }
        $photos = $rsp['rsp']['photos']['photo'];
        if (!is_array($photos)) {
            return array('ok' => 0, 'error' => 'no photos');
        }
        foreach ($photos as $photo) {
            $ph_rsp = flickr_photos_import_photo($photo);
            if (!$ph_rsp['ok']) {
                return $ph_rsp;
            }
            $fave_rsp = flickr_faves_add_fave($user, $ph_rsp['photo'], $photo['date_faved']);
            if ($fave_rsp['ok']) {
                $count++;
            }
        }
        $args['page'] += 1;
    }
    return array('ok' => 1, 'count_imported' => $count);
}
function flickr_photos_import_get_recent($nsid, $more = array())
{
    $flickr_user = flickr_users_get_by_nsid($nsid);
    $user = users_get_by_id($flickr_user['user_id']);
    if (!$user) {
        return array('ok' => 0, 'error' => 'not a valid user');
    }
    $method = 'flickr.photos.recentlyUpdated';
    if (!isset($more['min_date'])) {
        $offset_days = 1;
        $offset = intval(60 * 60 * 24 * $offset_days);
        $min_date = time() - $offset;
    } else {
        $min_date = intval($more['min_date']);
    }
    $args = array('auth_token' => $flickr_user['auth_token'], 'min_date' => $min_date, 'extras' => 'original_format,tags,media,date_upload,date_taken,geo', 'per_page' => 100, 'page' => 1);
    $pages = null;
    $imported = 0;
    # TO DO: capture dateupdate for each photo and return that
    # if there's a fatal error so that the FlickrBackups database
    # can be set with something other than 0 (20111206/straup)
    while (!isset($pages) || $pages >= $args['page']) {
        # because the Flickr API has an annoying habit of
        # timing out and this causes an initial import of
        # photos to fail and be repeated in-toto over and
        # over again (20111206/straup)
        $tries = 1;
        $max_tries = 5;
        $ok = 0;
        while (!$ok && $tries < $max_tries) {
            $rsp = flickr_api_call($method, $args);
            $ok = $rsp['ok'];
            $tries++;
            if ($ok) {
                $photos = $rsp['rsp']['photos']['photo'];
                if (!is_array($photos)) {
                    $rsp = not_okay("no photos");
                    $ok = 0;
                }
            }
        }
        if (!$ok) {
            return $rsp;
        }
        if (!isset($pages)) {
            $pages = $rsp['rsp']['photos']['pages'];
        }
        # TO DO: date update stuff (see above)
        foreach ($photos as $photo) {
            flickr_photos_import_photo($photo, $more);
            $imported++;
        }
        $args['page'] += 1;
    }
    return okay(array('count_imported' => $imported));
}
<?php

include "include/init.php";
loadlib("flickr_photos_archives");
loadlib("flickr_photos_utils");
loadlib("dates_utils");
$year = get_int32("year");
if (!$year) {
    error_404();
}
if ($path = get_str("path")) {
    $flickr_user = flickr_users_get_by_path_alias($path);
} else {
    if ($nsid = get_str("nsid")) {
        $flickr_user = flickr_users_get_by_nsid($nsid);
    }
}
if (!$flickr_user) {
    error_404();
}
$owner = users_get_by_id($flickr_user['user_id']);
$is_own = $owner['id'] == $GLOBALS['cfg']['user']['id'] ? 1 : 0;
$GLOBALS['smarty']->assign_by_ref("owner", $owner);
$GLOBALS['smarty']->assign("is_own", $is_own);
$more = array('viewer_id' => $GLOBALS['cfg']['user']['id'], 'page' => get_int32("page"));
$user_context = get_str("context");
$user_context = $user_context == 'posted' ? 'posted' : 'taken';
$GLOBALS['smarty']->assign("context", $user_context);
$more['context'] = $user_context;
$rsp = flickr_photos_archives_for_user_and_year($owner, $year, $more);
$photos = $rsp['rows'];
loadlib("flickr_dates");
#
$viewer = $GLOBALS['cfg']['user'];
$flickr_user = flickr_users_get_by_url();
$owner = users_get_by_id($flickr_user['user_id']);
$is_own = $owner['id'] == $viewer['id'] ? 1 : 0;
$GLOBALS['smarty']->assign("is_own", $is_own);
#
$more = array('viewer_id' => $viewer['id'], 'page' => get_int32("page"));
if ($by_alias = get_str("by_alias")) {
    if ($by_flickr_user = flickr_users_get_by_path_alias($by_alias)) {
        $more['by_owner'] = users_get_by_id($by_flickr_user['user_id']);
    }
} else {
    if ($by_nsid = get_str("by_nsid")) {
        if ($by_flickr_user = flickr_users_get_by_nsid($by_nsid)) {
            $more['by_owner'] = users_get_by_id($by_flickr_user['user_id']);
        }
    } else {
    }
}
$by_owner = isset($more['by_owner']) ? $more['by_owner'] : null;
$faves = flickr_faves_for_user($owner, $more);
$photos = array();
foreach ($faves['rows'] as $f) {
    $photo = flickr_photos_get_by_id($f['photo_id']);
    $photo['owner'] = users_get_by_id($photo['user_id']);
    # quick hack until perms are denormalized into the FlickrFaves table
    $photo['canview'] = flickr_photos_permissions_can_view_photo($photo, $viewer['id']);
    # going to leave this disable until I figure out what to
    # do about reciprical contacts hoohah...
function flickr_users_ensure_user_account($nsid, $username = '')
{
    $flickr_user = flickr_users_get_by_nsid($nsid);
    if ($flickr_user) {
        $user = users_get_by_id($flickr_user['user_id']);
        return $user;
    }
    loadlib("random");
    $password = random_string(32);
    if ($username == '') {
        $username = $nsid;
    }
    # TO DO: error handling
    $user = users_create_user(array("username" => $username, "email" => "{$username}@donotsend-flickr.com", "password" => $password));
    $flickr_user = flickr_users_create_user(array('user_id' => $user['id'], 'nsid' => $nsid));
    return $user;
}
function flickr_contacts_import_for_nsid($nsid, $more = array())
{
    $flickr_user = flickr_users_get_by_nsid($nsid);
    $user = users_get_by_id($flickr_user['user_id']);
    if (!$user) {
        return array('ok' => 0, 'error' => 'not a valid user');
    }
    $method = 'flickr.contacts.getList';
    $all_contacts = array();
    $count_contacts = 0;
    $args = array('auth_token' => $flickr_user['auth_token'], 'per_page' => 100, 'page' => 1);
    $pages = null;
    while (!isset($pages) || $pages >= $args['page']) {
        $api_ok = 0;
        $api_error = '';
        # Can I just say this is so profoundly annoying. Why why why
        # are API calls to a federated database table failing? Anyway.
        # (20120201/straup)
        $retries = 0;
        $max_retries = 10;
        while (!$api_ok) {
            $retries += 1;
            $rsp = flickr_api_call($method, $args);
            $api_ok = $rsp['ok'];
            if (!$api_ok) {
                $api_error = "The Flickr API is wigging out: {$rsp['error']}";
            } else {
                $contacts = $rsp['rsp']['contacts']['contact'];
                if (!is_array($contacts)) {
                    $api_error = "The Flickr API did not return any contacts";
                    $api_ok = 0;
                }
            }
            echo "page: {$args['page']}/{$pages} tries: {$retries}/{$max_retries} ok: {$api_ok}\n";
            if (!$api_ok) {
                if ($retries == $max_retries) {
                    return not_okay("Unable to fetch contacts: {$api_error}");
                }
            }
        }
        if (!isset($pages)) {
            $pages = $rsp['rsp']['contacts']['pages'];
        }
        foreach ($contacts as $contact) {
            $contact_nsid = $contact['nsid'];
            $contact_username = $contact['username'];
            $flickr_contact = flickr_users_get_by_nsid($contact_nsid);
            if (!$flickr_contact) {
                $password = random_string(32);
                $user_contact = users_create_user(array("username" => $contact_username, "email" => "{$contact_username}@donotsend-flickr.com", "password" => $password));
                #
                $method = 'flickr.people.getInfo';
                $args = array('user_id' => $contact_nsid);
                $rsp = flickr_api_call($method, $args);
                $path_alias = $rsp['ok'] ? $rsp['rsp']['person']['path_alias'] : '';
                #
                $flickr_contact = flickr_users_create_user(array('user_id' => $user_contact['id'], 'nsid' => $contact_nsid, 'path_alias' => $path_alias));
            }
            $rel = flickr_contacts_calculate_relationship($contact);
            # echo "{$contact_username} : {$rel} ({$contact['friend']} {$contact['family']})\n";
            $insert = array('user_id' => $user['id'], 'contact_id' => $flickr_contact['user_id'], 'rel' => $rel);
            $all_contacts[] = $insert;
        }
        $args['page'] += 1;
    }
    if (isset($more['purge_existing_contacts'])) {
        $rsp = flickr_contacts_purge_contacts($user);
        if (!$rsp['ok']) {
            return not_okay("failed to purge existing contacts: {$rsp['error']}");
        }
    }
    # echo "import " . count($all_contacts) . " contacts\n";
    foreach ($all_contacts as $insert) {
        if (flickr_contacts_add_contact($insert)) {
            $count_contacts++;
        }
    }
    return array('ok' => 1, 'count_imported' => $count_contacts);
}