]> git.mxchange.org Git - friendica.git/blobdiff - include/enotify.php
MySQL ANY_VALUE with fallback to MIN
[friendica.git] / include / enotify.php
index bed00c5eaf95ac1969855d1ed8190a04dbf19c4c..ebc27309dbe61710b76241f84bfd28e6f8ce0b1d 100644 (file)
@@ -23,7 +23,7 @@ function notification($params) {
 
        $banner = t('Friendica Notification');
        $product = FRIENDICA_PLATFORM;
-       $siteurl = $a->get_baseurl(true);
+       $siteurl = App::get_baseurl(true);
        $thanks = t('Thank You,');
        $sitename = $a->config['sitename'];
        if (!x($a->config['admin_name']))
@@ -49,7 +49,7 @@ function notification($params) {
        // with $params['show_in_notification_page'] == false, the notification isn't inserted into
        // the database, and an email is sent if applicable.
        // default, if not specified: true
-       $show_in_notification_page = ((x($params, 'show_in_notification_page')) ? $params['show_in_notification_page']:True);
+       $show_in_notification_page = ((x($params, 'show_in_notification_page')) ? $params['show_in_notification_page']:true);
 
        $additional_mail_header = "";
        $additional_mail_header .= "Precedence: list\n";
@@ -58,7 +58,7 @@ function notification($params) {
        $additional_mail_header .= "X-Friendica-Platform: ".FRIENDICA_PLATFORM."\n";
        $additional_mail_header .= "X-Friendica-Version: ".FRIENDICA_VERSION."\n";
        $additional_mail_header .= "List-ID: <notification.".$hostname.">\n";
-       $additional_mail_header .= "List-Archive: <".$a->get_baseurl()."/notifications/system>\n";
+       $additional_mail_header .= "List-Archive: <".App::get_baseurl()."/notifications/system>\n";
 
        if (array_key_exists('item', $params)) {
                $title = $params['item']['title'];
@@ -105,7 +105,7 @@ function notification($params) {
                // If so don't create a second notification
 
                $p = null;
-               $p = q("SELECT `id` FROM `notify` WHERE (`type` = %d OR `type` = %d OR `type` = %d) AND `link` = '%s' AND `uid` = %d LIMIT 1",
+               $p = q("SELECT `id` FROM `notify` WHERE `type` IN (%d, %d, %d) AND `link` = '%s' AND `uid` = %d LIMIT 1",
                        intval(NOTIFY_TAGSELF),
                        intval(NOTIFY_COMMENT),
                        intval(NOTIFY_SHARE),
@@ -411,13 +411,14 @@ function notification($params) {
                        $hash = random_string();
                        $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
                                dbesc($hash));
-                       if (count($r))
+                       if (dbm::is_result($r))
                                $dups = true;
                } while($dups == true);
 
                $datarray = array();
                $datarray['hash']  = $hash;
                $datarray['name']  = $params['source_name'];
+               $datarray['name_cache'] = strip_tags(bbcode($params['source_name']));
                $datarray['url']   = $params['source_link'];
                $datarray['photo'] = $params['source_photo'];
                $datarray['date']  = datetime_convert();
@@ -439,8 +440,8 @@ function notification($params) {
 
                // create notification entry in DB
 
-               $r = q("INSERT INTO `notify` (`hash`, `name`, `url`, `photo`, `date`, `uid`, `link`, `iid`, `parent`, `type`, `verb`, `otype`)
-                       values('%s', '%s', '%s', '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s')",
+               $r = q("INSERT INTO `notify` (`hash`, `name`, `url`, `photo`, `date`, `uid`, `link`, `iid`, `parent`, `type`, `verb`, `otype`, `name_cache`)
+                       values('%s', '%s', '%s', '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s')",
                        dbesc($datarray['hash']),
                        dbesc($datarray['name']),
                        dbesc($datarray['url']),
@@ -452,7 +453,8 @@ function notification($params) {
                        intval($datarray['parent']),
                        intval($datarray['type']),
                        dbesc($datarray['verb']),
-                       dbesc($datarray['otype'])
+                       dbesc($datarray['otype']),
+                       dbesc($datarray["name_cache"])
                );
 
                $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' AND `uid` = %d LIMIT 1",
@@ -470,7 +472,7 @@ function notification($params) {
                // After we've stored everything, look again to see if there are any duplicates and if so remove them
 
                $p = null;
-               $p = q("SELECT `id` FROM `notify` WHERE (`type` = %d OR `type` = %d) AND `link` = '%s' AND `uid` = %d ORDER BY `id`",
+               $p = q("SELECT `id` FROM `notify` WHERE `type` IN (%d, %d) AND `link` = '%s' AND `uid` = %d ORDER BY `id`",
                        intval(NOTIFY_TAGSELF),
                        intval(NOTIFY_COMMENT),
                        dbesc($params['link']),
@@ -492,10 +494,12 @@ function notification($params) {
                }
 
 
-               $itemlink = $a->get_baseurl().'/notify/view/'.$notify_id;
+               $itemlink = App::get_baseurl().'/notify/view/'.$notify_id;
                $msg = replace_macros($epreamble, array('$itemlink' => $itemlink));
-               $r = q("UPDATE `notify` SET `msg` = '%s' WHERE `id` = %d AND `uid` = %d",
+               $msg_cache = format_notification_message($datarray['name_cache'], strip_tags(bbcode($msg)));
+               $r = q("UPDATE `notify` SET `msg` = '%s', `msg_cache` = '%s' WHERE `id` = %d AND `uid` = %d",
                        dbesc($msg),
+                       dbesc($msg_cache),
                        intval($notify_id),
                        intval($params['uid'])
                );
@@ -644,7 +648,6 @@ function notification($params) {
  * @param str $defaulttype (Optional) Forces a notification with this type.
  */
 function check_item_notification($itemid, $uid, $defaulttype = "") {
-
        $notification_data = array("uid" => $uid, "profiles" => array());
        call_hooks('check_item_notification', $notification_data);
 
@@ -728,17 +731,17 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
                        intval($item[0]['contact-id']),
                        intval($uid)
                );
-               $send_notification = count($r);
+               $send_notification = dbm::is_result($r);
 
                if (!$send_notification) {
                        $tags = q("SELECT `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` = %d AND `uid` = %d",
                                intval(TERM_OBJ_POST), intval($itemid), intval(TERM_MENTION), intval($uid));
 
-                       if (count($tags)) {
+                       if (dbm::is_result($tags)) {
                                foreach ($tags AS $tag) {
                                        $r = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `notify_new_posts`",
                                                normalise_link($tag["url"]), intval($uid));
-                                       if (count($r))
+                                       if (dbm::is_result($r))
                                                $send_notification = true;
                                }
                        }
@@ -778,4 +781,27 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
        if (isset($params["type"]))
                notification($params);
 }
-?>
+
+/**
+ * @brief Formats a notification message with the notification author
+ *
+ * Replace the name with {0} but ensure to make that only once. The {0} is used
+ * later and prints the name in bold.
+ *
+ * @param string $name
+ * @param string $message
+ * @return string Formatted message
+ */
+function format_notification_message($name, $message) {
+       if ($name != '') {
+               $pos = strpos($message, $name);
+       } else {
+               $pos = false;
+       }
+
+       if ($pos !== false) {
+               $message = substr_replace($message, '{0}', $pos, strlen($name));
+       }
+
+       return $message;
+}