]> git.mxchange.org Git - friendica.git/blobdiff - include/enotify.php
Fixes:
[friendica.git] / include / enotify.php
index 57e7b2df0831b962e9fb8cc10303f8dcfa0c6014..b0a8c4b660aa635713e5e905f60d106a603e046b 100644 (file)
@@ -8,10 +8,11 @@ use Friendica\Core\Addon;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
 use Friendica\Core\System;
+use Friendica\Database\DBA;
 use Friendica\Database\DBM;
+use Friendica\Model\Item;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Emailer;
-use Friendica\Model\Item;
 
 /**
  * @brief Creates a notification entry and possibly sends a mail
@@ -33,11 +34,11 @@ function notification($params)
        $product = FRIENDICA_PLATFORM;
        $siteurl = System::baseUrl(true);
        $thanks = L10n::t('Thank You,');
-       $sitename = $a->config['sitename'];
-       if (!x($a->config['admin_name'])) {
-               $site_admin = L10n::t('%s Administrator', $sitename);
+       $sitename = Config::get('config', 'sitename');
+       if (Config::get('config', 'admin_name')) {
+               $site_admin = L10n::t('%1$s, %2$s Administrator', Config::get('config', 'admin_name'), $sitename);
        } else {
-               $site_admin = L10n::t('%1$s, %2$s Administrator', $a->config['admin_name'], $sitename);
+               $site_admin = L10n::t('%s Administrator', $sitename);
        }
 
        $sender_name = $sitename;
@@ -49,7 +50,7 @@ function notification($params)
        $sender_email = $a->getSenderEmailAddress();
 
        if ($params['type'] != SYSTEM_EMAIL) {
-               $user = dba::selectFirst('user', ['nickname', 'page-flags'],
+               $user = DBA::selectFirst('user', ['nickname', 'page-flags'],
                        ['uid' => $params['uid']]);
 
                // There is no need to create notifications for forum accounts
@@ -93,6 +94,8 @@ function notification($params)
        }
 
        if ($params['type'] == NOTIFY_MAIL) {
+               $itemlink = $siteurl.'/message/'.$params['item']['id'];
+
                $subject = L10n::t('[Friendica:Notify] New mail received at %s', $sitename);
 
                $preamble = L10n::t('%1$s sent you a new private message at %2$s.', $params['source_name'], $sitename);
@@ -101,11 +104,10 @@ function notification($params)
                $sitelink = L10n::t('Please visit %s to view and/or reply to your private messages.');
                $tsitelink = sprintf($sitelink, $siteurl.'/message/'.$params['item']['id']);
                $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'/message/'.$params['item']['id'].'">'.$sitename.'</a>');
-               $itemlink = $siteurl.'/message/'.$params['item']['id'];
        }
 
        if ($params['type'] == NOTIFY_COMMENT) {
-               $thread = dba::selectFirst('thread', ['ignored'], ['iid' => $parent_id]);
+               $thread = DBA::selectFirst('thread', ['ignored'], ['iid' => $parent_id]);
                if (DBM::is_result($thread) && $thread["ignored"]) {
                        logger("Thread ".$parent_id." will be ignored", LOGGER_DEBUG);
                        return;
@@ -130,10 +132,11 @@ function notification($params)
                $item = null;
 
                if ($params['otype'] === 'item' && $parent_id) {
-                       $item = Item::selectFirst($params['uid'], [], ['id' => $parent_id]);
+                       $item = Item::selectFirstForUser($params['uid'], Item::ITEM_FIELDLIST, ['id' => $parent_id]);
                }
 
                $item_post_type = item_post_type($item);
+               $itemlink = $item['plink'];
 
                // "a post"
                $dest_str = L10n::t('%1$s commented on [url=%2$s]a %3$s[/url]',
@@ -242,6 +245,7 @@ function notification($params)
        }
 
        if ($params['type'] == NOTIFY_TAGSHARE) {
+               $itemlink =  $params['link'];
                $subject = L10n::t('[Friendica:Notify] %s tagged your post', $params['source_name']);
 
                $preamble = L10n::t('%1$s tagged your post at %2$s', $params['source_name'], $sitename);
@@ -253,10 +257,10 @@ function notification($params)
                $sitelink = L10n::t('Please visit %s to view and/or reply to the conversation.');
                $tsitelink = sprintf($sitelink, $siteurl);
                $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
-               $itemlink =  $params['link'];
        }
 
        if ($params['type'] == NOTIFY_INTRO) {
+               $itemlink = $params['link'];
                $subject = L10n::t('[Friendica:Notify] Introduction received');
 
                $preamble = L10n::t('You\'ve received an introduction from \'%1$s\' at %2$s', $params['source_name'], $sitename);
@@ -270,7 +274,6 @@ function notification($params)
                $sitelink = L10n::t('Please visit %s to approve or reject the introduction.');
                $tsitelink = sprintf($sitelink, $siteurl);
                $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
-               $itemlink =  $params['link'];
 
                switch ($params['verb']) {
                        case ACTIVITY_FRIEND:
@@ -300,6 +303,7 @@ function notification($params)
        }
 
        if ($params['type'] == NOTIFY_SUGGEST) {
+               $itemlink =  $params['link'];
                $subject = L10n::t('[Friendica:Notify] Friend suggestion received');
 
                $preamble = L10n::t('You\'ve received a friend suggestion from \'%1$s\' at %2$s', $params['source_name'], $sitename);
@@ -316,11 +320,11 @@ function notification($params)
                $sitelink = L10n::t('Please visit %s to approve or reject the suggestion.');
                $tsitelink = sprintf($sitelink, $siteurl);
                $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
-               $itemlink =  $params['link'];
        }
 
        if ($params['type'] == NOTIFY_CONFIRM) {
                if ($params['verb'] == ACTIVITY_FRIEND) { // mutual connection
+                       $itemlink =  $params['link'];
                        $subject = L10n::t('[Friendica:Notify] Connection accepted');
 
                        $preamble = L10n::t('\'%1$s\' has accepted your connection request at %2$s', $params['source_name'], $sitename);
@@ -334,8 +338,8 @@ function notification($params)
                        $sitelink = L10n::t('Please visit %s if you wish to make any changes to this relationship.');
                        $tsitelink = sprintf($sitelink, $siteurl);
                        $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
-                       $itemlink =  $params['link'];
                } else { // ACTIVITY_FOLLOW
+                       $itemlink =  $params['link'];
                        $subject = L10n::t('[Friendica:Notify] Connection accepted');
 
                        $preamble = L10n::t('\'%1$s\' has accepted your connection request at %2$s', $params['source_name'], $sitename);
@@ -351,13 +355,13 @@ function notification($params)
                        $sitelink = L10n::t('Please visit %s  if you wish to make any changes to this relationship.');
                        $tsitelink = sprintf($sitelink, $siteurl);
                        $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
-                       $itemlink =  $params['link'];
                }
        }
 
        if ($params['type'] == NOTIFY_SYSTEM) {
                switch($params['event']) {
                        case "SYSTEM_REGISTER_REQUEST":
+                               $itemlink =  $params['link'];
                                $subject = L10n::t('[Friendica System Notify]') . ' ' . L10n::t('registration request');
 
                                $preamble = L10n::t('You\'ve received a registration request from \'%1$s\' at %2$s', $params['source_name'], $sitename);
@@ -375,7 +379,6 @@ function notification($params)
                                $sitelink = L10n::t('Please visit %s to approve or reject the request.');
                                $tsitelink = sprintf($sitelink, $params['link']);
                                $hsitelink = sprintf($sitelink, '<a href="'.$params['link'].'">'.$sitename.'</a><br><br>');
-                               $itemlink =  $params['link'];
                                break;
                        case "SYSTEM_DB_UPDATE_FAIL":
                                break;
@@ -503,7 +506,7 @@ function notification($params)
                );
                if ($p && (count($p) > 1)) {
                        for ($d = 1; $d < count($p); $d ++) {
-                               dba::delete('notify', ['id' => $p[$d]['id']]);
+                               DBA::delete('notify', ['id' => $p[$d]['id']]);
                        }
 
                        // only continue on if we stored the first one
@@ -572,7 +575,7 @@ function notification($params)
                $datarray['sitename'] = $sitename;
                $datarray['siteurl'] = $siteurl;
                $datarray['type'] = $params['type'];
-               $datarray['parent'] = $params['parent'];
+               $datarray['parent'] = $parent_id;
                $datarray['source_name'] = $params['source_name'];
                $datarray['source_link'] = $params['source_link'];
                $datarray['source_photo'] = $params['source_photo'];
@@ -607,7 +610,7 @@ function notification($params)
                        '$source_name'  => $datarray['source_name'],
                        '$source_link'  => $datarray['source_link'],
                        '$source_photo' => $datarray['source_photo'],
-                       '$username'     => $datarray['to_name'],
+                       '$username'     => $datarray['username'],
                        '$hsitelink'    => $datarray['hsitelink'],
                        '$hitemlink'    => $datarray['hitemlink'],
                        '$thanks'       => $datarray['thanks'],
@@ -628,7 +631,7 @@ function notification($params)
                        '$source_name'  => $datarray['source_name'],
                        '$source_link'  => $datarray['source_link'],
                        '$source_photo' => $datarray['source_photo'],
-                       '$username'     => $datarray['to_name'],
+                       '$username'     => $datarray['username'],
                        '$tsitelink'    => $datarray['tsitelink'],
                        '$titemlink'    => $datarray['titemlink'],
                        '$thanks'       => $datarray['thanks'],
@@ -663,13 +666,13 @@ function notification($params)
  */
 function check_user_notification($itemid) {
        // fetch all users in the thread
-       $users = dba::p("SELECT DISTINCT(`contact`.`uid`) FROM `item`
+       $users = DBA::p("SELECT DISTINCT(`contact`.`uid`) FROM `item`
                        INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` != 0
                        WHERE `parent` IN (SELECT `parent` FROM `item` WHERE `id`=?)", $itemid);
-       while ($user = dba::fetch($users)) {
+       while ($user = DBA::fetch($users)) {
                check_item_notification($itemid, $user['uid']);
        }
-       dba::close($users);
+       DBA::close($users);
 }
 
 /**
@@ -677,7 +680,7 @@ function check_user_notification($itemid) {
  *
  * @param int $itemid ID of the item for which the check should be done
  * @param int $uid User ID
- * @param str $defaulttype (Optional) Forces a notification with this type.
+ * @param string $defaulttype (Optional) Forces a notification with this type.
  */
 function check_item_notification($itemid, $uid, $defaulttype = "") {
        $notification_data = ["uid" => $uid, "profiles" => []];
@@ -686,12 +689,12 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
        $profiles = $notification_data["profiles"];
 
        $fields = ['notify-flags', 'language', 'username', 'email', 'nickname'];
-       $user = dba::selectFirst('user', $fields, ['uid' => $uid]);
+       $user = DBA::selectFirst('user', $fields, ['uid' => $uid]);
        if (!DBM::is_result($user)) {
                return false;
        }
 
-       $owner = dba::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]);
+       $owner = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]);
        if (!DBM::is_result($owner)) {
                return false;
        }
@@ -724,28 +727,28 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
 
        $profiles = $profiles2;
 
-       $profile_list = "";
+       $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
 
-       foreach ($profiles AS $profile) {
-               if ($profile_list != "")
-                       $profile_list .= "', '";
+       $contacts = [];
 
-               $profile_list .= dbesc($profile);
+       while ($contact = DBA::fetch($ret)) {
+               $contacts[] = $contact['id'];
        }
 
-       $profile_list = "'".$profile_list."'";
+       $contact_list = implode(',', $contacts);
+
+       DBA::close($ret);
 
        // Only act if it is a "real" post
        // We need the additional check for the "local_profile" because of mixed situations on connector networks
-       $item = q("SELECT `id`, `mention`, `tag`,`parent`, `title`, `body`, `author-id`, `guid`,
-                       `parent-uri`, `uri`, `contact-id`
-                       FROM `item` WHERE `id` = %d AND `verb` IN ('%s', '') AND `type` != 'activity' AND
-                               NOT (`author-link` IN ($profile_list))  LIMIT 1",
-               intval($itemid), dbesc(ACTIVITY_POST));
-       if (!$item)
-               return false;
-
-       $author = dba::selectFirst('contact', ['name', 'thumb', 'url'], ['id' => $item[0]['author-id']]);
+       $fields = ['id', 'mention', 'tag', 'parent', 'title', 'body',
+               'author-link', 'author-name', 'author-avatar', 'author-id',
+               'guid', 'parent-uri', 'uri', 'contact-id', 'network'];
+       $condition = ['id' => $itemid, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]];
+       $item = Item::selectFirst($fields, $condition);
+       if (!DBM::is_result($item) || in_array($item['author-id'], $contacts)) {
+               return;
+       }
 
        // Generate the notification array
        $params = [];
@@ -754,17 +757,17 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
        $params["language"] = $user["language"];
        $params["to_name"] = $user["username"];
        $params["to_email"] = $user["email"];
-       $params["item"] = $item[0];
-       $params["parent"] = $item[0]["parent"];
-       $params["link"] = System::baseUrl().'/display/'.urlencode($item[0]["guid"]);
+       $params["item"] = $item;
+       $params["parent"] = $item["parent"];
+       $params["link"] = System::baseUrl().'/display/'.urlencode($item["guid"]);
        $params["otype"] = 'item';
-       $params["source_name"] = $author["name"];
-       $params["source_link"] = $author["url"];
-       $params["source_photo"] = $author["thumb"];
+       $params["source_name"] = $item["author-name"];
+       $params["source_link"] = $item["author-link"];
+       $params["source_photo"] = $item["author-avatar"];
 
-       if ($item[0]["parent-uri"] === $item[0]["uri"]) {
+       if ($item["parent-uri"] === $item["uri"]) {
                // Send a notification for every new post?
-               $send_notification = dba::exists('contact', ['id' => $item[0]['contact-id'], 'notify_new_posts' => true]);
+               $send_notification = DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true]);
 
                if (!$send_notification) {
                        $tags = q("SELECT `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` = %d AND `uid` = %d",
@@ -773,7 +776,7 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
                        if (DBM::is_result($tags)) {
                                foreach ($tags AS $tag) {
                                        $condition = ['nurl' => normalise_link($tag["url"]), 'uid' => $uid, 'notify_new_posts' => true];
-                                       $r = dba::exists('contact', $condition);
+                                       $r = DBA::exists('contact', $condition);
                                        if ($r) {
                                                $send_notification = true;
                                        }
@@ -791,11 +794,11 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
        $tagged = false;
 
        foreach ($profiles AS $profile) {
-               if (strpos($item[0]["tag"], "=".$profile."]") || strpos($item[0]["body"], "=".$profile."]"))
+               if (strpos($item["tag"], "=".$profile."]") || strpos($item["body"], "=".$profile."]"))
                        $tagged = true;
        }
 
-       if ($item[0]["mention"] || $tagged || ($defaulttype == NOTIFY_TAGSELF)) {
+       if ($item["mention"] || $tagged || ($defaulttype == NOTIFY_TAGSELF)) {
                $params["type"] = NOTIFY_TAGSELF;
                $params["verb"] = ACTIVITY_TAG;
        }
@@ -803,9 +806,9 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
        // Is it a post that the user had started or where he interacted?
        $parent = q("SELECT `thread`.`iid` FROM `thread` INNER JOIN `item` ON `item`.`parent` = `thread`.`iid`
                        WHERE `thread`.`iid` = %d AND NOT `thread`.`ignored` AND
-                               (`thread`.`mention` OR `item`.`author-link` IN ($profile_list))
+                               (`thread`.`mention` OR `item`.`author-id` IN ($contact_list))
                        LIMIT 1",
-                       intval($item[0]["parent"]));
+                       intval($item["parent"]));
 
        if ($parent && !isset($params["type"])) {
                $params["type"] = NOTIFY_COMMENT;