+function ping_get_notifications($uid) {
+
+ $result = array();
+ $offset = 0;
+ $seen = false;
+ $seensql = "NOT";
+ $order = "DESC";
+ $quit = false;
+
+ $a = get_app();
+
+ do {
+ $r = q("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
+ FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
+ WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
+ AND NOT (`notify`.`type` IN (%d, %d))
+ AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
+ intval($uid),
+ intval(NOTIFY_INTRO),
+ intval(NOTIFY_MAIL),
+ intval($offset)
+ );
+
+ if (!$r AND !$seen) {
+ $seen = true;
+ $seensql = "";
+ $order = "DESC";
+ $offset = 0;
+ } elseif (!$r)
+ $quit = true;
+ else
+ $offset += 50;
+
+
+ foreach ($r AS $notification) {
+ if (is_null($notification["visible"]))
+ $notification["visible"] = true;
+
+ if (is_null($notification["spam"]))
+ $notification["spam"] = 0;
+
+ if (is_null($notification["deleted"]))
+ $notification["deleted"] = 0;
+
+ $notification["message"] = strip_tags(bbcode($notification["msg"]));
+ $notification["name"] = strip_tags(bbcode($notification["name"]));
+
+ // Replace the name with {0} but ensure to make that only once
+ // The {0} is used later and prints the name in bold.
+
+ $pos = strpos($notification["message"],$notification['name']);
+ if ($pos !== false)
+ $notification["message"] = substr_replace($notification["message"],"{0}",$pos,strlen($notification["name"]));
+
+ $notification['href'] = $a->get_baseurl() . '/notify/view/' . $notification['id'];
+
+ if ($notification["visible"] AND !$notification["spam"] AND
+ !$notification["deleted"] AND !is_array($result[$notification["parent"]])) {
+ $result[$notification["parent"]] = $notification;
+ }
+ }
+
+ } while ((count($result) < 50) AND !$quit);
+
+
+ return($result);
+}