function flickr_contacts_get_contact($user_id, $contact_id)
{
    $user = users_get_by_id($user_id);
    $cluster_id = $user['cluster_id'];
    $enc_user = AddSlashes($user_id);
    $enc_contact = AddSlashes($contact_id);
    $sql = "SELECT * FROM FlickrContacts WHERE user_id='{$enc_user}' AND contact_id='{$enc_contact}'";
    $rsp = db_fetch_users($cluster_id, $sql);
    return db_single($rsp);
}
function flickr_faves_count_for_user(&$user, $more = array())
{
    $defaults = array('viewer_id' => 0);
    $more = array_merge($defaults, $more);
    $cluster_id = $user['cluster_id'];
    $enc_user = AddSlashes($user['id']);
    # TO DO: perms
    $sql = "SELECT COUNT(photo_id) AS cnt FROM FlickrFaves WHERE user_id='{$enc_user}' {$extra}";
    $row = db_single(db_fetch_users($cluster_id, $sql));
    return $row['cnt'];
}
function flickr_push_photos_for_subscription(&$sub, $older_than = null)
{
    $user = users_get_by_id($sub['user_id']);
    $cluster = $user['cluster_id'];
    $enc_sub = AddSlashes($sub['id']);
    # TO DO: indexes
    $sql = "SELECT * FROM FlickrPushPhotos WHERE subscription_id='{$enc_sub}'";
    if ($older_than) {
        $enc_older = AddSlashes($older_than);
        $sql .= " AND created > '{$enc_older}'";
    }
    $sql .= " ORDER BY created DESC";
    $rsp = db_fetch_users($cluster, $sql);
    $photos = array();
    foreach ($rsp['rows'] as $row) {
        $photo = json_decode($row['photo_data'], 'as hash');
        $photo['created'] = $row['created'];
        $photo['display_url'] = str_replace("_s.jpg", ".jpg", $photo['thumb_url']);
        $photos[] = $photo;
    }
    $rsp['rows'] = $photos;
    return $rsp;
}
function flickr_push_subscriptions_for_user(&$user)
{
    $cache_key = "flickr_push_subscriptions_user_{$user['id']}";
    $cache = cache_get($cache_key);
    if ($cache['ok']) {
        return $cache['data'];
    }
    $cluster_id = $user['cluster_id'];
    $enc_user = AddSlashes($user['id']);
    $sql = "SELECT * FROM FlickrPushSubscriptions WHERE user_id='{$enc_user}'";
    $rsp = db_fetch_users($cluster_id, $sql);
    if ($rsp['ok']) {
        cache_set($cache_key, $rsp, "cache locally");
    }
    return $rsp;
}
function sheets_counts_for_user(&$user, $viewer_id = 0)
{
    $cache_key = "sheets_counts_for_user_{$user['id']}";
    if ($user['id'] != $viewer_id) {
        $cache_key .= "_public";
    }
    $cache = cache_get($cache_key);
    if ($cache['ok']) {
        return $cache['data'];
    }
    #
    $enc_id = AddSlashes($user['id']);
    if ($viewer_id == $user['id']) {
        $sql = "SELECT COUNT(id) AS count_sheets, SUM(count_dots) AS count_dots FROM Sheets WHERE user_id='{$enc_id}'";
    } else {
        $sql = "SELECT COUNT(id) AS count_sheets, SUM(count_dots_public) AS count_dots FROM Sheets WHERE user_id='{$enc_id}' AND count_dots_public > 0";
    }
    #
    $row = db_single(db_fetch_users($user['cluster_id'], $sql));
    if ($row) {
        cache_set($cache_key, $row, 'cache locally');
    }
    return $row;
}
function dots_count_dots_for_sheet(&$sheet)
{
    $cache_key = "dots_count_for_sheet_{$sheet['id']}";
    $cache = cache_get($cache_key);
    if ($cache['ok']) {
        return $cache['data'];
    }
    #
    $user = users_get_by_id($sheet['user_id']);
    $enc_id = AddSlashes($sheet['id']);
    $sql = "SELECT COUNT(id) AS count_total FROM Dots WHERE sheet_id='{$enc_id}'";
    $rsp = db_fetch_users($user['cluster_id'], $sql);
    $row = db_single($rsp);
    $count_total = $row['count_total'];
    $sql = "SELECT COUNT(id) AS count_public FROM Dots WHERE sheet_id='{$enc_id}'";
    $sql = _dots_where_public_sql($sql);
    $rsp = db_fetch_users($user['cluster_id'], $sql);
    $row = db_single($rsp);
    $count_public = $row['count_public'];
    $rsp = array('total' => $count_total, 'public' => $count_public);
    cache_set($cache_key, $rsp, 'cache locally');
    return $rsp;
}
function privatesquare_checkins_get_by_foursquare_id(&$user, $id)
{
    $cluster_id = $user['cluster_id'];
    $enc_user = AddSlashes($user['id']);
    $enc_id = AddSlashes($id);
    $sql = "SELECT * FROM PrivatesquareCheckins WHERE user_id='{$enc_user}' AND checkin_id='{$enc_id}'";
    return db_single(db_fetch_users($cluster_id, $sql));
}
function flickr_photos_archives_days_for_user(&$user, $year, $month, $more = array())
{
    $cluster_id = $user['cluster_id'];
    $enc_user = AddSlashes($user['id']);
    list($start, $end) = dates_utils_between($year, $month);
    $enc_start = AddSlashes($start);
    $enc_end = AddSlashes($end);
    $date_col = $more['context'] == 'posted' ? 'dateupload' : 'datetaken';
    $sql = "SELECT DISTINCT(DATE_FORMAT({$date_col}, '%d')) AS day FROM FlickrPhotos WHERE user_id='{$enc_user}'";
    $sql .= " AND `{$date_col}` BETWEEN '{$enc_start}' AND '{$enc_end}'";
    $sql .= " ORDER BY `{$date_col}` ASC";
    $rsp = db_fetch_users($cluster_id, $sql);
    $days = array();
    if ($rsp['ok']) {
        foreach ($rsp['rows'] as $r) {
            $days[] = $r['day'];
        }
    }
    sort($days);
    return $days;
}
function flickr_photos_get_bookends(&$photo, $more = array())
{
    $defaults = array('viewer_id' => 0);
    $more = array_merge($defaults, $more);
    $user = users_get_by_id($photo['user_id']);
    $cluster_id = $user['cluster_id'];
    $enc_id = AddSlashes($photo['id']);
    $enc_user = AddSlashes($photo['user_id']);
    if ($perms = flickr_photos_permissions_photos_where($user['id'], $more['viewer_id'])) {
        $str_perms = implode(",", $perms);
        $extra = " AND perms IN ({$str_perms})";
    }
    # TO DO: INDEXES
    $sql = "SELECT * FROM FlickrPhotos WHERE user_id = '{$enc_user}' AND id < '{$enc_id}' {$extra} ORDER BY id DESC LIMIT 1";
    $rsp = db_fetch_users($cluster_id, $sql);
    $before = $rsp['rows'];
    # TO DO: INDEXES
    $sql = "SELECT * FROM FlickrPhotos WHERE user_id='{$enc_user}' AND id > '{$enc_id}' {$extra} ORDER BY id ASC LIMIT 1";
    $rsp = db_fetch_users($cluster_id, $sql);
    $after = $rsp['rows'];
    return array('ok' => 1, 'before' => $before, 'after' => $after);
}