function GetRss($loginState) { $cacheKey = 'subrss2_' . $loginState['id']; $rss = MCGet($cacheKey); if ($rss === false) { $db = DBConnect(); $stmt = $db->prepare('select rss from tblUser WHERE id = ?'); $stmt->bind_param('i', $loginState['id']); $stmt->execute(); $stmt->bind_result($rss); if (!$stmt->fetch()) { $rss = ''; // user not found, or some other error? } $stmt->close(); if (is_null($rss)) { // usually only when account is created $rss = UpdateUserRss($loginState['id'], $db); } if (!$rss) { $rss = ''; // can't store boolean false in memcache } MCSet($cacheKey, $rss); } if (!$rss) { // i.e. empty string $rss = false; } return $rss; }
function SendUserMessage($userId, $messageType, $subject, $message) { $loops = 0; while (!MCAdd(SUBSCRIPTION_MESSAGES_CACHEKEY . "lock_{$userId}", 1, 15)) { usleep(250000); if ($loops++ >= 120) { // 30 seconds return false; } } $db = DBConnect(true); $seq = 0; $cnt = 0; $stmt = $db->prepare('select ifnull(max(seq),0)+1, count(*) from tblUserMessages where user = ?'); $stmt->bind_param('i', $userId); $stmt->execute(); $stmt->bind_result($seq, $cnt); $stmt->fetch(); $stmt->close(); $stmt = $db->prepare('INSERT INTO tblUserMessages (user, seq, created, type, subject, message) VALUES (?, ?, NOW(), ?, ?, ?)'); $stmt->bind_param('iisss', $userId, $seq, $messageType, $subject, $message); $success = $stmt->execute(); $stmt->close(); if ($success) { $cnt++; } if ($cnt > SUBSCRIPTION_MESSAGES_MAX) { $stmt = $db->prepare('delete from tblUserMessages where user = ? order by seq asc limit ?'); $cnt = $cnt - SUBSCRIPTION_MESSAGES_MAX; $stmt->bind_param('ii', $userId, $cnt); $stmt->execute(); $stmt->close(); } MCDelete(SUBSCRIPTION_MESSAGES_CACHEKEY . "lock_{$userId}"); if (!$success) { DebugMessage("Error adding user message: " . $db->error); $db->close(); return false; } MCDelete(SUBSCRIPTION_MESSAGES_CACHEKEY . $userId); MCDelete(SUBSCRIPTION_MESSAGES_CACHEKEY . $userId . '_' . $seq); $stmt = $db->prepare('select name, email, locale from tblUser where id = ? and email is not null and emailverification is null'); $stmt->bind_param('i', $userId); $stmt->execute(); $name = $address = $locale = ''; $stmt->bind_result($name, $address, $locale); if (!$stmt->fetch()) { $address = false; } $stmt->close(); if ($address) { NewsstandMail($address, $name, $subject, $message, $locale); } UpdateUserRss($userId, $db); $db->close(); return $seq; }