id . ") AND (threadid = " . $thread->id . ")"; $result = sql_query($sql); // If they're already subscribed, don't add them again; if (sql_num_rows($result > 0)) { // TODO: Specific "error" page for this case. show_result_page("subscribe", true, $thread); } $sql = "INSERT INTO subscriptions SET userid = " . $user->id . ", threadid = " . $thread->id; show_result_page("subscribe", (sql_query($sql) != null), $thread); } function unsubscribe($thread, $user=null) { if (!$thread) { show_result_page("unsubscribe", false, NULL); exit(); } if (!$user) { $user = get_logged_in_user(true, '../'); } $sql = "DELETE FROM subscriptions WHERE (userid = ". $user->id . ") AND (threadid = " . $thread->id . ")"; show_result_page("unsubscribe", (sql_query($sql) != null), $thread); } function show_result_page($action, $success, $thread) { if ($action == "subscribe" && $success) { doHeader("Subscription Successful"); echo "Subscription successful"; echo "
You are now subscribed to ", $thread->title, ". You will receive an email whenever another user posts to this thread.
"; } else if ($action == "unsubscribe" && $success) { doHeader("Unsubscription Successful"); echo "Unsubscription successful"; echo "You have successfully unsubscribed from ", $thread->title, ". You will no longer receive emails for posts to this thread."; } else if ($action == "subscribe" && !$success) { doHeader("Subscription Failed"); echo "Subscription failed"; if ($thread) { echo "
There was a problem subscribing you to ", $thread->title, ". Please try again later.
"; } else { echo "The thread you tried to subscribe to does not exist.
"; } } else if ($action == "unsubscribe" && !$success) { doHeader("Unsubscription Failed"); echo "Unsubscription failed"; if ($thread) { echo "There was a problem unsubscribing you from ", $thread->title, ". Please try again later.
"; } else { echo "The thread you tried to unsubscribe from does not exist.
"; } } else { doHeader("Unknown action"); echo "Unknown action"; } doFooter(); } function notify_subscribers($thread) { $sql = "SELECT DISTINCT * FROM subscriptions WHERE threadid = " . $thread->id; $result = sql_query($sql); while ($row = mysql_fetch_assoc($result)) { send_notice_email($row['userid'], $thread); } } function send_notice_email($userId, $thread) { $result = sql_query("SELECT * FROM user WHERE id = $userId"); $row = mysql_fetch_assoc($result); $title = PROJECT . ": A user has posted to your subscribed thread."; $link = URL_BASE . "forum/thread.php?id=" . $thread->id; $body = "Another " . PROJECT . " user has posted to the thread \"" . $thread->title . "\".\n" ."To view the updated thread, direct your web browser to the following URL:\n\n$link"; mail($row['email_addr'], $title, $body); } ?>