Merge pull request #2523 from annando/1605-bugfix-notificatoon

Bugfix: Under certain circumstances notifications were send
This commit is contained in:
Tobias Diekershoff 2016-05-23 16:57:23 +02:00
commit b727e0d0c0
1 changed files with 244 additions and 224 deletions

View File

@ -4,27 +4,33 @@ require_once('include/email.php');
require_once('include/bbcode.php');
require_once('include/html2bbcode.php');
/**
* @brief Creates a notification entry and possibly sends a mail
*
* @param array $params Array with the elements:
uid, item, parent, type, otype, verb, event,
link, subject, body, to_name, to_email, source_name,
source_link, activity, preamble, notify_flags,
language, show_in_notification_page
*/
function notification($params) {
#logger('notification()', LOGGER_DEBUG);
$a = get_app();
// from here on everything is in the recipients language
push_lang($params['language']);
$banner = t('Friendica Notification');
$product = FRIENDICA_PLATFORM;
$siteurl = $a->get_baseurl(true);
$thanks = t('Thank You,');
$sitename = $a->config['sitename'];
if (!x($a->config['admin_name'])) {
if (!x($a->config['admin_name']))
$site_admin = sprintf(t('%s Administrator'), $sitename);
} else {
else
$site_admin = sprintf(t('%1$s, %2$s Administrator'), $a->config['admin_name'], $sitename);
}
$nickname = "";
$sender_name = $sitename;
@ -33,9 +39,8 @@ function notification($params) {
$hostname = substr($hostname, 0, strpos($hostname, ':'));
$sender_email = $a->config['sender_email'];
if (empty($sender_email)) {
if (empty($sender_email))
$sender_email = t('noreply').'@'.$hostname;
}
$user = q("SELECT `nickname` FROM `user` WHERE `uid` = %d", intval($params['uid']));
if ($user)
@ -55,14 +60,11 @@ function notification($params) {
$additional_mail_header .= "List-ID: <notification.".$hostname.">\n";
$additional_mail_header .= "List-Archive: <".$a->get_baseurl()."/notifications/system>\n";
if (array_key_exists('item', $params)) {
$title = $params['item']['title'];
$body = $params['item']['body'];
}
else {
} else
$title = $body = '';
}
// e.g. "your post", "David's photo", etc.
$possess_desc = t('%s <!item_type!>');
@ -78,11 +80,11 @@ function notification($params) {
$parent_id = 0;
if ($params['type'] == NOTIFY_MAIL) {
$subject = sprintf(t('[Friendica:Notify] New mail received at %s'), $sitename);
$preamble = sprintf(t('%1$s sent you a new private message at %2$s.'), $params['source_name'], $sitename);
$epreamble = sprintf(t('%1$s sent you %2$s.'), '[url='.$params['source_link'].']'.$params['source_name'].'[/url]', '[url=$itemlink]'.t('a private message').'[/url]');
$sitelink = 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>');
@ -90,10 +92,6 @@ function notification($params) {
}
if ($params['type'] == NOTIFY_COMMENT) {
// logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
//$parent_id = $params['parent'];
$p = q("SELECT `ignored` FROM `thread` WHERE `iid` = %d AND `uid` = %d LIMIT 1",
intval($parent_id),
intval($params['uid'])
@ -107,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` = %d OR `type` = %d OR `type` = %d) AND `link` = '%s' AND `uid` = %d LIMIT 1",
intval(NOTIFY_TAGSELF),
intval(NOTIFY_COMMENT),
intval(NOTIFY_SHARE),
@ -119,20 +117,18 @@ function notification($params) {
return;
}
// if it's a post figure out who's post it is.
$p = null;
if ($params['otype'] === 'item' && $parent_id) {
$p = q("select * from item where id = %d and uid = %d limit 1",
$p = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
intval($parent_id),
intval($params['uid'])
);
}
$item_post_type = item_post_type($p[0]);
//$possess_desc = str_replace('<!item_type!>',$possess_desc);
// "a post"
$dest_str = sprintf(t('%1$s commented on [url=%2$s]a %3$s[/url]'),
@ -161,6 +157,7 @@ function notification($params) {
// differents subjects for messages on the same thread.
$subject = sprintf(t('[Friendica:Notify] Comment to conversation #%1$d by %2$s'), $parent_id, $params['source_name']);
$preamble = sprintf(t('%s commented on an item/conversation you have been following.'), $params['source_name']);
$epreamble = $dest_str;
@ -174,7 +171,6 @@ function notification($params) {
$subject = sprintf(t('[Friendica:Notify] %s posted to your profile wall'), $params['source_name']);
$preamble = sprintf(t('%1$s posted to your profile wall at %2$s'), $params['source_name'], $sitename);
$epreamble = sprintf(t('%1$s posted to [url=%2$s]your wall[/url]'),
'[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
$params['link']);
@ -187,6 +183,7 @@ function notification($params) {
if ($params['type'] == NOTIFY_TAGSELF) {
$subject = sprintf(t('[Friendica:Notify] %s tagged you'), $params['source_name']);
$preamble = sprintf(t('%1$s tagged you at %2$s'), $params['source_name'], $sitename);
$epreamble = sprintf(t('%1$s [url=%2$s]tagged you[/url].'),
'[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
@ -200,6 +197,7 @@ function notification($params) {
if ($params['type'] == NOTIFY_SHARE) {
$subject = sprintf(t('[Friendica:Notify] %s shared a new post'), $params['source_name']);
$preamble = sprintf(t('%1$s shared a new post at %2$s'), $params['source_name'], $sitename);
$epreamble = sprintf(t('%1$s [url=%2$s]shared a post[/url].'),
'[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
@ -212,8 +210,8 @@ function notification($params) {
}
if ($params['type'] == NOTIFY_POKE) {
$subject = sprintf(t('[Friendica:Notify] %1$s poked you'), $params['source_name']);
$preamble = sprintf(t('%1$s poked you at %2$s'), $params['source_name'], $sitename);
$epreamble = sprintf(t('%1$s [url=%2$s]poked you[/url].'),
'[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
@ -231,6 +229,7 @@ function notification($params) {
if ($params['type'] == NOTIFY_TAGSHARE) {
$subject = sprintf(t('[Friendica:Notify] %s tagged your post'), $params['source_name']);
$preamble = sprintf(t('%1$s tagged your post at %2$s'), $params['source_name'], $sitename);
$epreamble = sprintf(t('%1$s tagged [url=%2$s]your post[/url]'),
'[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
@ -244,10 +243,12 @@ function notification($params) {
if ($params['type'] == NOTIFY_INTRO) {
$subject = sprintf(t('[Friendica:Notify] Introduction received'));
$preamble = sprintf(t('You\'ve received an introduction from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
$epreamble = sprintf(t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.'),
$itemlink,
'[url='.$params['source_link'].']'.$params['source_name'].'[/url]');
$body = sprintf(t('You may visit their profile at %s'), $params['source_link']);
$sitelink = t('Please visit %s to approve or reject the introduction.');
@ -259,6 +260,7 @@ function notification($params) {
case ACTIVITY_FRIEND:
// someone started to share with user (mostly OStatus)
$subject = sprintf(t('[Friendica:Notify] A new person is sharing with you'));
$preamble = sprintf(t('%1$s is sharing with you at %2$s'), $params['source_name'], $sitename);
$epreamble = sprintf(t('%1$s is sharing with you at %2$s'),
'[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
@ -267,6 +269,7 @@ function notification($params) {
case ACTIVITY_FOLLOW:
// someone started to follow the user (mostly OStatus)
$subject = sprintf(t('[Friendica:Notify] You have a new follower'));
$preamble = sprintf(t('You have a new follower at %2$s : %1$s'), $params['source_name'], $sitename);
$epreamble = sprintf(t('You have a new follower at %2$s : %1$s'),
'[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
@ -276,12 +279,11 @@ function notification($params) {
// ACTIVITY_REQ_FRIEND is default activity for notifications
break;
}
}
if ($params['type'] == NOTIFY_SUGGEST) {
$subject = sprintf(t('[Friendica:Notify] Friend suggestion received'));
$preamble = sprintf(t('You\'ve received a friend suggestion from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
$epreamble = sprintf(t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.'),
$itemlink,
@ -301,12 +303,13 @@ function notification($params) {
if ($params['type'] == NOTIFY_CONFIRM) {
if ($params['verb'] == ACTIVITY_FRIEND) { // mutual connection
$subject = sprintf(t('[Friendica:Notify] Connection accepted'));
$preamble = sprintf(t('\'%1$s\' has accepted your connection request at %2$s'), $params['source_name'], $sitename);
$epreamble = sprintf(t('%2$s has accepted your [url=%1$s]connection request[/url].'),
$itemlink,
'[url='.$params['source_link'].']'.$params['source_name'].'[/url]');
$body = t('You are now mutual friends and may exchange status updates, photos, and email
without restriction.');
$body = t('You are now mutual friends and may exchange status updates, photos, and email without restriction.');
$sitelink = t('Please visit %s if you wish to make any changes to this relationship.');
$tsitelink = sprintf($sitelink, $siteurl);
@ -314,10 +317,12 @@ function notification($params) {
$itemlink = $params['link'];
} else { // ACTIVITY_FOLLOW
$subject = sprintf(t('[Friendica:Notify] Connection accepted'));
$preamble = sprintf(t('\'%1$s\' has accepted your connection request at %2$s'), $params['source_name'], $sitename);
$epreamble = sprintf(t('%2$s has accepted your [url=%1$s]connection request[/url].'),
$itemlink,
'[url='.$params['source_link'].']'.$params['source_name'].'[/url]');
$body = sprintf(t('\'%1$s\' has chosen to accept you a "fan", which restricts some forms of communication - such as private messaging and some profile interactions. If this is a celebrity or community page, these settings were applied automatically.'), $params['source_name']);
$body .= "\n\n";
$body .= sprintf(t('\'%1$s\' may choose to extend this into a two-way or more permissive relationship in the future.'), $params['source_name']);
@ -327,17 +332,18 @@ function notification($params) {
$hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
$itemlink = $params['link'];
}
}
if ($params['type'] == NOTIFY_SYSTEM) {
switch($params['event']) {
case "SYSTEM_REGISTER_REQUEST":
$subject = sprintf(t('[Friendica System:Notify] registration request'));
$preamble = sprintf(t('You\'ve received a registration request from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
$epreamble = sprintf(t('You\'ve received a [url=%1$s]registration request[/url] from %2$s.'),
$itemlink,
'[url='.$params['source_link'].']'.$params['source_name'].'[/url]');
$body = sprintf(t('Full Name: %1$s\nSite Location: %2$s\nLogin Name: %3$s (%4$s)'),
$params['source_name'], $siteurl, $params['source_mail'], $params['source_nick']);
@ -358,8 +364,11 @@ function notification($params) {
// password reset, invitations and so) using one look (but without
// add a notification to the user, with could be inexistent)
$subject = $params['subject'];
$preamble = $params['preamble'];
$body = $params['body'];
$sitelink = "";
$tsitelink = "";
$hsitelink = "";
@ -384,16 +393,17 @@ function notification($params) {
call_hooks('enotify', $h);
$subject = $h['subject'];
$preamble = $h['preamble'];
$epreamble = $h['epreamble'];
$body = $h['body'];
$sitelink = $h['sitelink'];
$tsitelink = $h['tsitelink'];
$hsitelink = $h['hsitelink'];
$itemlink = $h['itemlink'];
if ($show_in_notification_page) {
logger("adding notification entry", LOGGER_DEBUG);
do {
@ -405,7 +415,6 @@ function notification($params) {
$dups = true;
} while($dups == true);
$datarray = array();
$datarray['hash'] = $hash;
$datarray['name'] = $params['source_name'];
@ -430,7 +439,7 @@ 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)
$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')",
dbesc($datarray['hash']),
dbesc($datarray['name']),
@ -446,7 +455,7 @@ function notification($params) {
dbesc($datarray['otype'])
);
$r = q("select id from notify where hash = '%s' and uid = %d limit 1",
$r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' AND `uid` = %d LIMIT 1",
dbesc($hash),
intval($params['uid'])
);
@ -461,7 +470,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` = %d OR `type` = %d) AND `link` = '%s' AND `uid` = %d ORDER BY `id`",
intval(NOTIFY_TAGSELF),
intval(NOTIFY_COMMENT),
dbesc($params['link']),
@ -469,7 +478,7 @@ function notification($params) {
);
if ($p && (count($p) > 1)) {
for ($d = 1; $d < count($p); $d ++) {
q("delete from notify where id = %d",
q("DELETE FROM `notify` WHERE `id` = %d",
intval($p[$d]['id'])
);
}
@ -485,12 +494,11 @@ function notification($params) {
$itemlink = $a->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",
$r = q("UPDATE `notify` SET `msg` = '%s' WHERE `id` = %d AND `uid` = %d",
dbesc($msg),
intval($notify_id),
intval($params['uid'])
);
}
// send email notification if notification preferences permit
@ -505,7 +513,7 @@ function notification($params) {
// Is this the first email notification for this parent item and user?
$r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
$r = q("SELECT `id` FROM `notify-threads` WHERE `master-parent-item` = %d AND `receiver-uid` = %d LIMIT 1",
intval($params['parent']),
intval($params['uid']));
@ -513,7 +521,7 @@ function notification($params) {
if (!$r) {
logger("notify_id:".intval($notify_id).", parent: ".intval($params['parent'])."uid: ".intval($params['uid']), LOGGER_DEBUG);
$r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
$r = q("INSERT INTO `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
values(%d, %d, %d, %d)",
intval($notify_id),
intval($params['parent']),
@ -531,14 +539,12 @@ function notification($params) {
}
}
// textversion keeps linebreaks
$textversion = strip_tags(str_replace("<br>", "\n", html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
$body))),ENT_QUOTES, 'UTF-8')));
$htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n\\n", "\\n"),
"<br />\n", $body))), ENT_QUOTES, 'UTF-8');
$datarray = array();
$datarray['banner'] = $banner;
$datarray['product'] = $product;
@ -612,8 +618,6 @@ function notification($params) {
'$content_allowed' => $content_allowed,
));
// logger('text: ' . $email_text_body);
// use the Emailer class to send the message
return Emailer::send(array(
@ -630,7 +634,6 @@ function notification($params) {
}
return False;
}
/**
@ -647,7 +650,7 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
$profiles = $notification_data["profiles"];
$user = q("SELECT `notify-flags`, `language`, `username`, `email` FROM `user` WHERE `uid` = %d", intval($uid));
$user = q("SELECT `notify-flags`, `language`, `username`, `email`, `nickname` FROM `user` WHERE `uid` = %d", intval($uid));
if (!$user)
return false;
@ -655,13 +658,30 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
if (!$owner)
return false;
// This is our regular URL format
$profiles[] = $owner[0]["url"];
// Notifications from Diaspora are often with an URL in the Diaspora format
$profiles[] = App::get_baseurl()."/u/".$user[0]["nickname"];
$profiles2 = array();
foreach ($profiles AS $profile) {
$profiles2[] = normalise_link($profile);
$profiles2[] = str_replace("http://", "https://", normalise_link($profile));
// Check for invalid profile urls. 13 should be the shortest possible profile length:
// http://a.bc/d
// Additionally check for invalid urls that would return the normalised value "http:"
if ((strlen($profile) >= 13) AND (normalise_link($profile) != "http:")) {
if (!in_array($profile, $profiles2))
$profiles2[] = $profile;
$profile = normalise_link($profile);
if (!in_array($profile, $profiles2))
$profiles2[] = $profile;
$profile = str_replace("http://", "https://", $profile);
if (!in_array($profile, $profiles2))
$profiles2[] = $profile;
}
}
$profiles = $profiles2;