2011-12-08 09:28:27 +00:00
< ? php
2017-11-20 20:37:30 +00:00
/**
2021-03-29 06:40:20 +00:00
* @ copyright Copyright ( C ) 2010 - 2021 , the Friendica project
2020-02-09 15:18:46 +00:00
*
* @ license GNU AGPL version 3 or any later version
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*
2017-11-20 20:37:30 +00:00
*/
2018-01-25 02:08:45 +00:00
2018-02-15 02:33:55 +00:00
use Friendica\Content\Text\BBCode ;
2021-01-30 22:03:53 +00:00
use Friendica\Content\Text\Plaintext ;
2018-12-26 06:06:24 +00:00
use Friendica\Core\Hook ;
2018-10-29 21:20:46 +00:00
use Friendica\Core\Logger ;
2018-10-31 14:35:50 +00:00
use Friendica\Core\Renderer ;
2017-08-26 06:04:21 +00:00
use Friendica\Core\System ;
2018-07-20 12:19:26 +00:00
use Friendica\Database\DBA ;
2019-12-15 23:47:24 +00:00
use Friendica\DI ;
2020-11-25 19:56:39 +00:00
use Friendica\Model\Contact ;
2018-07-20 02:15:21 +00:00
use Friendica\Model\Item ;
2021-01-23 09:53:44 +00:00
use Friendica\Model\Notification ;
2021-01-16 04:11:28 +00:00
use Friendica\Model\Post ;
2019-01-21 16:38:01 +00:00
use Friendica\Model\User ;
2021-08-21 20:35:04 +00:00
use Friendica\Model\Verb ;
2019-10-23 22:25:43 +00:00
use Friendica\Protocol\Activity ;
2017-04-30 04:07:00 +00:00
2016-05-21 11:19:30 +00:00
/**
2020-01-19 06:05:23 +00:00
* Creates a notification entry and possibly sends a mail
2016-05-21 11:19:30 +00:00
*
* @ param array $params Array with the elements :
2021-08-24 08:53:15 +00:00
* type , event , otype , activity , verb , uid , cid , item , link ,
2020-11-25 19:56:39 +00:00
* source_name , source_mail , source_nick , source_link , source_photo ,
* show_in_notification_page
2021-05-09 11:54:34 +00:00
*
2019-01-21 16:36:01 +00:00
* @ return bool
2019-01-07 15:24:06 +00:00
* @ throws \Friendica\Network\HTTPException\InternalServerErrorException
2016-05-21 11:19:30 +00:00
*/
2018-01-04 02:12:19 +00:00
function notification ( $params )
{
2020-02-04 21:30:34 +00:00
/** @var string the common prefix of a notification subject */
2020-02-05 21:22:12 +00:00
$subjectPrefix = DI :: l10n () -> t ( '[Friendica:Notify]' );
2020-02-04 21:30:34 +00:00
2018-08-02 05:21:01 +00:00
// Temporary logging for finding the origin
2019-02-23 04:00:16 +00:00
if ( ! isset ( $params [ 'uid' ])) {
Logger :: notice ( 'Missing parameters "uid".' , [ 'params' => $params , 'callstack' => System :: callstack ()]);
2018-08-02 05:21:01 +00:00
}
2018-08-23 08:00:25 +00:00
// Ensure that the important fields are set at any time
2020-11-25 19:56:39 +00:00
$fields = [ 'nickname' , 'page-flags' , 'notify-flags' , 'language' , 'username' , 'email' ];
2018-08-23 08:00:25 +00:00
$user = DBA :: selectFirst ( 'user' , $fields , [ 'uid' => $params [ 'uid' ]]);
if ( ! DBA :: isResult ( $user )) {
2019-02-23 04:00:16 +00:00
Logger :: error ( 'Unknown user' , [ 'uid' => $params [ 'uid' ]]);
2019-01-21 16:36:01 +00:00
return false ;
2018-08-23 08:00:25 +00:00
}
2020-11-25 19:56:39 +00:00
// There is no need to create notifications for forum accounts
if ( in_array ( $user [ 'page-flags' ], [ User :: PAGE_FLAGS_COMMUNITY , User :: PAGE_FLAGS_PRVGROUP ])) {
return false ;
}
$params [ 'notify_flags' ] = $user [ 'notify-flags' ];
$params [ 'language' ] = $user [ 'language' ];
$params [ 'to_name' ] = $user [ 'username' ];
$params [ 'to_email' ] = $user [ 'email' ];
2018-08-23 08:00:25 +00:00
2012-03-26 08:43:26 +00:00
// from here on everything is in the recipients language
2020-01-18 19:54:15 +00:00
$l10n = DI :: l10n () -> withLang ( $params [ 'language' ]);
2012-03-26 08:43:26 +00:00
2020-11-25 19:56:39 +00:00
if ( ! empty ( $params [ 'cid' ])) {
$contact = Contact :: getById ( $params [ 'cid' ], [ 'url' , 'name' , 'photo' ]);
if ( DBA :: isResult ( $contact )) {
$params [ 'source_link' ] = $contact [ 'url' ];
$params [ 'source_name' ] = $contact [ 'name' ];
$params [ 'source_photo' ] = $contact [ 'photo' ];
}
}
2019-12-30 22:02:20 +00:00
$siteurl = DI :: baseUrl () -> get ( true );
2020-01-19 20:21:13 +00:00
$sitename = DI :: config () -> get ( 'config' , 'sitename' );
2014-09-06 16:15:18 +00:00
// 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
2018-10-14 15:30:39 +00:00
$show_in_notification_page = isset ( $params [ 'show_in_notification_page' ]) ? $params [ 'show_in_notification_page' ] : true ;
2014-09-06 16:15:18 +00:00
2020-11-25 19:56:39 +00:00
$title = $params [ 'item' ][ 'title' ] ? ? '' ;
$body = $params [ 'item' ][ 'body' ] ? ? '' ;
2015-04-14 04:54:41 +00:00
2020-11-25 19:56:39 +00:00
$parent_id = $params [ 'item' ][ 'parent' ] ? ? 0 ;
$parent_uri_id = $params [ 'item' ][ 'parent-uri-id' ] ? ? 0 ;
2020-05-02 13:12:11 +00:00
2018-08-02 05:21:01 +00:00
$epreamble = '' ;
2019-01-07 17:51:48 +00:00
$preamble = '' ;
$subject = '' ;
$sitelink = '' ;
$tsitelink = '' ;
$hsitelink = '' ;
$itemlink = '' ;
2018-08-02 05:21:01 +00:00
2021-08-24 08:53:15 +00:00
switch ( $params [ 'type' ]) {
case Notification\Type :: MAIL :
$itemlink = $params [ 'link' ];
2018-07-10 12:27:56 +00:00
2021-08-24 08:53:15 +00:00
$subject = $l10n -> t ( '%s New mail received at %s' , $subjectPrefix , $sitename );
2011-12-24 07:07:05 +00:00
2021-08-24 08:53:15 +00:00
$preamble = $l10n -> t ( '%1$s sent you a new private message at %2$s.' , $params [ 'source_name' ], $sitename );
$epreamble = $l10n -> t ( '%1$s sent you %2$s.' , '[url=' . $params [ 'source_link' ] . ']' . $params [ 'source_name' ] . '[/url]' , '[url=' . $itemlink . ']' . $l10n -> t ( 'a private message' ) . '[/url]' );
2011-12-24 07:07:05 +00:00
2021-08-24 08:53:15 +00:00
$sitelink = $l10n -> t ( 'Please visit %s to view and/or reply to your private messages.' );
$tsitelink = sprintf ( $sitelink , $itemlink );
$hsitelink = sprintf ( $sitelink , '<a href="' . $itemlink . '">' . $sitename . '</a>' );
2020-11-25 19:56:39 +00:00
2021-08-24 08:53:15 +00:00
// Mail notifications aren't using the "notify" table entry
$show_in_notification_page = false ;
break ;
2011-12-26 22:16:25 +00:00
2021-08-24 08:53:15 +00:00
case Notification\Type :: COMMENT :
if ( Post\ThreadUser :: getIgnored ( $parent_uri_id , $params [ 'uid' ])) {
Logger :: info ( 'Thread is ignored' , [ 'parent' => $parent_id , 'parent-uri-id' => $parent_uri_id ]);
return false ;
}
2014-01-05 15:10:02 +00:00
2021-01-16 22:37:27 +00:00
$item = Post :: selectFirstForUser ( $params [ 'uid' ], Item :: ITEM_FIELDLIST , [ 'id' => $parent_id , 'deleted' => false ]);
2021-08-24 08:53:15 +00:00
if ( empty ( $item )) {
return false ;
}
2020-01-08 21:48:59 +00:00
2021-08-24 08:53:15 +00:00
$item_post_type = Item :: postType ( $item , $l10n );
2012-03-01 03:23:01 +00:00
2021-08-24 08:53:15 +00:00
$content = Plaintext :: getPost ( $item , 70 );
if ( ! empty ( $content [ 'text' ])) {
$title = '"' . trim ( str_replace ( " \n " , " " , $content [ 'text' ])) . '"' ;
} else {
$title = '' ;
}
2019-01-06 01:25:05 +00:00
2021-08-24 08:53:15 +00:00
// First go for the general message
2020-01-08 21:48:59 +00:00
2021-08-24 08:53:15 +00:00
// "George Bull's post"
2020-03-21 11:48:20 +00:00
$message = $l10n -> t ( '%1$s commented on %2$s\'s %3$s %4$s' );
2021-08-24 08:53:15 +00:00
$dest_str = sprintf ( $message , $params [ 'source_name' ], $item [ 'author-name' ], $item_post_type , $title );
2020-01-08 21:48:59 +00:00
2021-08-24 08:53:15 +00:00
// "your post"
if ( $item [ 'wall' ]) {
2020-03-21 11:48:20 +00:00
$message = $l10n -> t ( '%1$s commented on your %2$s %3$s' );
2021-08-24 08:53:15 +00:00
$dest_str = sprintf ( $message , $params [ 'source_name' ], $item_post_type , $title );
// "their post"
} elseif ( $item [ 'author-link' ] == $params [ 'source_link' ]) {
2020-03-21 11:48:20 +00:00
$message = $l10n -> t ( '%1$s commented on their %2$s %3$s' );
2021-08-24 08:53:15 +00:00
$dest_str = sprintf ( $message , $params [ 'source_name' ], $item_post_type , $title );
2019-01-06 01:25:05 +00:00
}
2020-01-08 21:48:59 +00:00
2020-02-09 21:42:51 +00:00
$subject = $l10n -> t ( '%1$s Comment to conversation #%2$d by %3$s' , $subjectPrefix , $parent_id , $params [ 'source_name' ]);
2019-01-06 01:25:05 +00:00
2019-12-28 22:12:01 +00:00
$preamble = $l10n -> t ( '%s commented on an item/conversation you have been following.' , $params [ 'source_name' ]);
2016-05-21 11:19:30 +00:00
2021-08-24 08:53:15 +00:00
$epreamble = $dest_str ;
2012-02-18 11:18:20 +00:00
2021-08-24 08:53:15 +00:00
$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' ];
break ;
2014-01-05 15:10:02 +00:00
2021-08-24 08:53:15 +00:00
case Notification\Type :: WALL :
$subject = $l10n -> t ( '%s %s posted to your profile wall' , $subjectPrefix , $params [ 'source_name' ]);
2016-05-21 11:19:30 +00:00
2021-08-24 08:53:15 +00:00
$preamble = $l10n -> t ( '%1$s posted to your profile wall at %2$s' , $params [ 'source_name' ], $sitename );
$epreamble = $l10n -> t ( '%1$s posted to [url=%2$s]your wall[/url]' ,
2020-08-31 18:51:59 +00:00
'[url=' . $params [ 'source_link' ] . ']' . $params [ 'source_name' ] . '[/url]' ,
$params [ 'link' ]
);
2021-08-24 08:53:15 +00:00
$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' ];
break ;
2016-05-21 11:19:30 +00:00
2021-08-24 08:53:15 +00:00
case Notification\Type :: POKE :
$subject = $l10n -> t ( '%1$s %2$s poked you' , $subjectPrefix , $params [ 'source_name' ]);
2014-08-20 22:53:01 +00:00
2021-08-24 08:53:15 +00:00
$preamble = $l10n -> t ( '%1$s poked you at %2$s' , $params [ 'source_name' ], $sitename );
$epreamble = $l10n -> t ( '%1$s [url=%2$s]poked you[/url].' ,
'[url=' . $params [ 'source_link' ] . ']' . $params [ 'source_name' ] . '[/url]' ,
$params [ 'link' ]
);
2012-01-04 01:29:07 +00:00
2021-08-24 08:53:15 +00:00
$subject = str_replace ( 'poked' , $l10n -> t ( $params [ 'activity' ]), $subject );
$preamble = str_replace ( 'poked' , $l10n -> t ( $params [ 'activity' ]), $preamble );
$epreamble = str_replace ( 'poked' , $l10n -> t ( $params [ 'activity' ]), $epreamble );
2012-01-04 01:29:07 +00:00
2021-08-24 08:53:15 +00:00
$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>' );
2018-07-10 12:27:56 +00:00
$itemlink = $params [ 'link' ];
2021-08-24 08:53:15 +00:00
break ;
case Notification\Type :: INTRO :
$itemlink = $params [ 'link' ];
$subject = $l10n -> t ( '%s Introduction received' , $subjectPrefix );
2014-09-06 16:15:18 +00:00
2021-08-24 08:53:15 +00:00
$preamble = $l10n -> t ( 'You\'ve received an introduction from \'%1$s\' at %2$s' , $params [ 'source_name' ], $sitename );
$epreamble = $l10n -> t ( 'You\'ve received [url=%1$s]an introduction[/url] from %2$s.' ,
2018-01-24 02:59:16 +00:00
$itemlink ,
'[url=' . $params [ 'source_link' ] . ']' . $params [ 'source_name' ] . '[/url]'
);
2016-05-21 11:19:30 +00:00
2021-08-24 08:53:15 +00:00
$body = $l10n -> t ( 'You may visit their profile at %s' , $params [ 'source_link' ]);
2016-05-21 11:19:30 +00:00
2021-08-24 08:53:15 +00:00
$sitelink = $l10n -> t ( 'Please visit %s to approve or reject the introduction.' );
2016-05-21 11:19:30 +00:00
$tsitelink = sprintf ( $sitelink , $siteurl );
$hsitelink = sprintf ( $sitelink , '<a href="' . $siteurl . '">' . $sitename . '</a>' );
2021-08-24 08:53:15 +00:00
switch ( $params [ 'verb' ]) {
case Activity :: FRIEND :
// someone started to share with user (mostly OStatus)
$subject = $l10n -> t ( '%s A new person is sharing with you' , $subjectPrefix );
$preamble = $l10n -> t ( '%1$s is sharing with you at %2$s' , $params [ 'source_name' ], $sitename );
$epreamble = $l10n -> t ( '%1$s is sharing with you at %2$s' ,
'[url=' . $params [ 'source_link' ] . ']' . $params [ 'source_name' ] . '[/url]' ,
$sitename
);
break ;
case Activity :: FOLLOW :
// someone started to follow the user (mostly OStatus)
$subject = $l10n -> t ( '%s You have a new follower' , $subjectPrefix );
$preamble = $l10n -> t ( 'You have a new follower at %2$s : %1$s' , $params [ 'source_name' ], $sitename );
$epreamble = $l10n -> t ( 'You have a new follower at %2$s : %1$s' ,
'[url=' . $params [ 'source_link' ] . ']' . $params [ 'source_name' ] . '[/url]' ,
$sitename
);
break ;
default :
// ACTIVITY_REQ_FRIEND is default activity for notifications
break ;
}
break ;
case Notification\Type :: SUGGEST :
2018-07-10 12:27:56 +00:00
$itemlink = $params [ 'link' ];
2021-08-24 08:53:15 +00:00
$subject = $l10n -> t ( '%s Friend suggestion received' , $subjectPrefix );
2016-05-21 11:19:30 +00:00
2021-08-24 08:53:15 +00:00
$preamble = $l10n -> t ( 'You\'ve received a friend suggestion from \'%1$s\' at %2$s' , $params [ 'source_name' ], $sitename );
$epreamble = $l10n -> t ( 'You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.' ,
2018-01-24 02:59:16 +00:00
$itemlink ,
2021-08-24 08:53:15 +00:00
'[url=' . $params [ 'item' ][ 'url' ] . ']' . $params [ 'item' ][ 'name' ] . '[/url]' ,
2018-01-24 02:59:16 +00:00
'[url=' . $params [ 'source_link' ] . ']' . $params [ 'source_name' ] . '[/url]'
);
2016-05-21 11:19:30 +00:00
2021-08-24 08:53:15 +00:00
$body = $l10n -> t ( 'Name:' ) . ' ' . $params [ 'item' ][ 'name' ] . " \n " ;
$body .= $l10n -> t ( 'Photo:' ) . ' ' . $params [ 'item' ][ 'photo' ] . " \n " ;
$body .= $l10n -> t ( 'You may visit their profile at %s' , $params [ 'item' ][ 'url' ]);
2014-09-06 16:15:18 +00:00
2021-08-24 08:53:15 +00:00
$sitelink = $l10n -> t ( 'Please visit %s to approve or reject the suggestion.' );
2016-05-21 11:19:30 +00:00
$tsitelink = sprintf ( $sitelink , $siteurl );
$hsitelink = sprintf ( $sitelink , '<a href="' . $siteurl . '">' . $sitename . '</a>' );
2021-08-24 08:53:15 +00:00
break ;
2012-03-25 11:37:09 +00:00
2021-08-24 08:53:15 +00:00
case Notification\Type :: CONFIRM :
if ( $params [ 'verb' ] == Activity :: FRIEND ) { // mutual connection
2018-07-10 12:27:56 +00:00
$itemlink = $params [ 'link' ];
2021-08-24 08:53:15 +00:00
$subject = $l10n -> t ( '%s Connection accepted' , $subjectPrefix );
2016-05-21 11:19:30 +00:00
2021-08-24 08:53:15 +00:00
$preamble = $l10n -> t ( '\'%1$s\' has accepted your connection request at %2$s' , $params [ 'source_name' ], $sitename );
$epreamble = $l10n -> t ( '%2$s has accepted your [url=%1$s]connection request[/url].' ,
2018-01-24 02:59:16 +00:00
$itemlink ,
'[url=' . $params [ 'source_link' ] . ']' . $params [ 'source_name' ] . '[/url]'
);
2021-08-24 08:53:15 +00:00
$body = $l10n -> t ( 'You are now mutual friends and may exchange status updates, photos, and email without restriction.' );
$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>' );
} else { // ACTIVITY_FOLLOW
$itemlink = $params [ 'link' ];
$subject = $l10n -> t ( '%s Connection accepted' , $subjectPrefix );
$preamble = $l10n -> t ( '\'%1$s\' has accepted your connection request at %2$s' , $params [ 'source_name' ], $sitename );
$epreamble = $l10n -> t ( '%2$s has accepted your [url=%1$s]connection request[/url].' ,
$itemlink ,
'[url=' . $params [ 'source_link' ] . ']' . $params [ 'source_name' ] . '[/url]'
2018-01-24 02:59:16 +00:00
);
2014-09-07 11:55:02 +00:00
2021-08-24 08:53:15 +00:00
$body = $l10n -> 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 .= $l10n -> t ( '\'%1$s\' may choose to extend this into a two-way or more permissive relationship in the future.' , $params [ 'source_name' ]);
$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>' );
}
break ;
case Notification\Type :: 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 );
$epreamble = $l10n -> t ( 'You\'ve received a [url=%1$s]registration request[/url] from %2$s.' ,
$itemlink ,
'[url=' . $params [ 'source_link' ] . ']' . $params [ 'source_name' ] . '[/url]'
);
$body = $l10n -> t ( " Full Name: %s \n Site Location: %s \n Login Name: %s (%s) " ,
$params [ 'source_name' ],
$siteurl , $params [ 'source_mail' ],
$params [ 'source_nick' ]
);
$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>' );
break ;
case " SYSTEM_DB_UPDATE_FAIL " :
break ;
}
break ;
default :
Logger :: notice ( 'Unhandled type' , [ 'type' => $params [ 'type' ]]);
return false ;
2014-09-07 08:27:39 +00:00
}
2021-08-21 20:35:04 +00:00
return notification_store_and_send ( $params , $sitelink , $tsitelink , $hsitelink , $title , $subject , $preamble , $epreamble , $body , $itemlink , $show_in_notification_page );
}
function notification_store_and_send ( $params , $sitelink , $tsitelink , $hsitelink , $title , $subject , $preamble , $epreamble , $body , $itemlink , $show_in_notification_page )
{
$item_id = $params [ 'item' ][ 'id' ] ? ? 0 ;
$uri_id = $params [ 'item' ][ 'uri-id' ] ? ? 0 ;
$parent_id = $params [ 'item' ][ 'parent' ] ? ? 0 ;
$parent_uri_id = $params [ 'item' ][ 'parent-uri-id' ] ? ? 0 ;
// Ensure that the important fields are set at any time
$fields = [ 'nickname' ];
$user = User :: getById ( $params [ 'uid' ], $fields );
$sitename = DI :: config () -> get ( 'config' , 'sitename' );
$nickname = $user [ 'nickname' ];
$hostname = DI :: baseUrl () -> getHostname ();
if ( strpos ( $hostname , ':' )) {
$hostname = substr ( $hostname , 0 , strpos ( $hostname , ':' ));
}
// Creates a new email builder for the notification email
$emailBuilder = DI :: emailer () -> newNotifyMail ();
$emailBuilder -> setHeader ( 'X-Friendica-Account' , '<' . $nickname . '@' . $hostname . '>' );
2014-12-22 23:55:36 +00:00
$subject .= " ( " . $nickname . " @ " . $hostname . " ) " ;
2014-09-06 13:52:53 +00:00
2018-01-15 13:05:12 +00:00
$h = [
2014-09-03 22:58:52 +00:00
'params' => $params ,
2012-03-25 12:06:11 +00:00
'subject' => $subject ,
2014-09-03 22:58:52 +00:00
'preamble' => $preamble ,
'epreamble' => $epreamble ,
'body' => $body ,
2012-03-25 12:06:11 +00:00
'sitelink' => $sitelink ,
'tsitelink' => $tsitelink ,
'hsitelink' => $hsitelink ,
'itemlink' => $itemlink
2018-01-15 13:05:12 +00:00
];
2014-08-20 22:53:01 +00:00
2018-12-26 06:06:24 +00:00
Hook :: callAll ( 'enotify' , $h );
2012-03-25 12:06:11 +00:00
$subject = $h [ 'subject' ];
2016-05-21 11:19:30 +00:00
2012-03-25 12:06:11 +00:00
$preamble = $h [ 'preamble' ];
$epreamble = $h [ 'epreamble' ];
2016-05-21 11:19:30 +00:00
2012-03-25 12:06:11 +00:00
$body = $h [ 'body' ];
2016-05-21 11:19:30 +00:00
2012-03-25 12:06:11 +00:00
$tsitelink = $h [ 'tsitelink' ];
$hsitelink = $h [ 'hsitelink' ];
2014-03-11 22:52:32 +00:00
$itemlink = $h [ 'itemlink' ];
2012-03-25 12:06:11 +00:00
2019-01-07 17:51:48 +00:00
$notify_id = 0 ;
2014-09-07 08:27:39 +00:00
if ( $show_in_notification_page ) {
2020-11-19 06:26:30 +00:00
$fields = [
2020-05-02 13:12:11 +00:00
'name' => $params [ 'source_name' ] ? ? '' ,
2021-07-10 12:58:48 +00:00
'name_cache' => substr ( strip_tags ( BBCode :: convertForUriId ( $uri_id , $params [ 'source_name' ])), 0 , 255 ),
2020-05-02 13:12:11 +00:00
'url' => $params [ 'source_link' ] ? ? '' ,
'photo' => $params [ 'source_photo' ] ? ? '' ,
'link' => $itemlink ? ? '' ,
'uid' => $params [ 'uid' ] ? ? 0 ,
'type' => $params [ 'type' ] ? ? '' ,
'verb' => $params [ 'verb' ] ? ? '' ,
'otype' => $params [ 'otype' ] ? ? '' ,
2020-11-19 06:26:30 +00:00
];
if ( ! empty ( $item_id )) {
$fields [ 'iid' ] = $item_id ;
}
if ( ! empty ( $uri_id )) {
$fields [ 'uri-id' ] = $uri_id ;
}
2020-11-25 19:56:39 +00:00
if ( ! empty ( $parent_id )) {
2020-11-19 06:26:30 +00:00
$fields [ 'parent' ] = $parent_id ;
}
2020-11-25 19:56:39 +00:00
if ( ! empty ( $parent_uri_id )) {
2020-11-19 06:26:30 +00:00
$fields [ 'parent-uri-id' ] = $parent_uri_id ;
}
$notification = DI :: notify () -> insert ( $fields );
2013-02-08 07:43:55 +00:00
2020-06-29 22:58:17 +00:00
// Notification insertion can be intercepted by an addon registering the 'enotify_store' hook
if ( ! $notification ) {
return false ;
}
2020-01-28 00:12:11 +00:00
$notification -> msg = Renderer :: replaceMacros ( $epreamble , [ '$itemlink' => $notification -> link ]);
2013-02-08 07:43:55 +00:00
2020-01-26 19:30:24 +00:00
DI :: notify () -> update ( $notification );
2018-08-19 12:46:11 +00:00
2020-01-28 22:21:24 +00:00
$itemlink = DI :: baseUrl () . '/notification/' . $notification -> id ;
2020-01-25 01:01:49 +00:00
$notify_id = $notification -> id ;
2014-09-06 16:15:18 +00:00
}
// send email notification if notification preferences permit
2018-08-23 08:00:25 +00:00
if (( intval ( $params [ 'notify_flags' ]) & intval ( $params [ 'type' ]))
2021-01-23 09:53:44 +00:00
|| $params [ 'type' ] == Notification\Type :: SYSTEM ) {
2011-12-24 07:07:05 +00:00
2018-10-29 21:20:46 +00:00
Logger :: log ( 'sending notification email' );
2011-12-24 07:07:05 +00:00
2017-06-08 02:00:59 +00:00
if ( isset ( $params [ 'parent' ]) && ( intval ( $params [ 'parent' ]) != 0 )) {
2021-01-16 04:11:28 +00:00
$parent = Post :: selectFirst ([ 'guid' ], [ 'id' => $params [ 'parent' ]]);
2020-09-06 17:47:25 +00:00
$message_id = " < " . $parent [ 'guid' ] . " @ " . gethostname () . " > " ;
2012-02-26 00:56:14 +00:00
2014-08-20 22:53:01 +00:00
// Is this the first email notification for this parent item and user?
2021-02-16 08:00:23 +00:00
if ( ! DBA :: exists ( 'notify-threads' , [ 'master-parent-uri-id' => $parent_uri_id , 'receiver-uid' => $params [ 'uid' ]])) {
2020-01-26 00:04:54 +00:00
Logger :: log ( " notify_id: " . intval ( $notify_id ) . " , parent: " . intval ( $params [ 'parent' ]) . " uid: " . intval ( $params [ 'uid' ]), Logger :: DEBUG );
2018-08-19 12:46:11 +00:00
2021-02-16 08:00:23 +00:00
$fields = [ 'notify-id' => $notify_id , 'master-parent-uri-id' => $parent_uri_id ,
2020-05-02 13:12:11 +00:00
'receiver-uid' => $params [ 'uid' ], 'parent-item' => 0 ];
2018-08-19 12:46:11 +00:00
DBA :: insert ( 'notify-threads' , $fields );
2014-08-20 22:53:01 +00:00
2020-09-19 18:14:55 +00:00
$emailBuilder -> setHeader ( 'Message-ID' , $message_id );
2021-08-21 20:35:04 +00:00
$log_msg = " include/enotify: No previous notification found for this parent: \n " .
" parent: ${ params['parent'] } \n " . " uid : ${ params['uid'] } \n " ;
2018-10-30 13:58:45 +00:00
Logger :: log ( $log_msg , Logger :: DEBUG );
2014-08-20 22:53:01 +00:00
} else {
// If not, just "follow" the thread.
2020-09-19 18:14:55 +00:00
$emailBuilder -> setHeader ( 'References' , $message_id );
$emailBuilder -> setHeader ( 'In-Reply-To' , $message_id );
2018-10-30 13:58:45 +00:00
Logger :: log ( " There's already a notification for this parent. " , Logger :: DEBUG );
2014-08-20 22:53:01 +00:00
}
2012-02-26 00:56:14 +00:00
}
2020-02-02 23:07:22 +00:00
$datarray = [
'preamble' => $preamble ,
'type' => $params [ 'type' ],
'parent' => $parent_id ,
'source_name' => $params [ 'source_name' ] ? ? null ,
'source_link' => $params [ 'source_link' ] ? ? null ,
'source_photo' => $params [ 'source_photo' ] ? ? null ,
'uid' => $params [ 'uid' ],
'hsitelink' => $hsitelink ,
'tsitelink' => $tsitelink ,
'itemlink' => $itemlink ,
'title' => $title ,
'body' => $body ,
'subject' => $subject ,
2020-09-21 15:07:34 +00:00
'headers' => $emailBuilder -> getHeaders (),
2020-02-02 23:07:22 +00:00
];
2012-02-27 07:54:04 +00:00
2018-12-26 06:06:24 +00:00
Hook :: callAll ( 'enotify_mail' , $datarray );
2012-02-27 07:54:04 +00:00
2020-09-21 15:07:34 +00:00
$emailBuilder
-> withHeaders ( $datarray [ 'headers' ])
2020-02-02 23:07:22 +00:00
-> withRecipient ( $params [ 'to_email' ])
2020-02-04 20:26:03 +00:00
-> forUser ([
'uid' => $datarray [ 'uid' ],
'language' => $params [ 'language' ],
])
2020-02-02 23:07:22 +00:00
-> withNotification ( $datarray [ 'subject' ], $datarray [ 'preamble' ], $datarray [ 'title' ], $datarray [ 'body' ])
-> withSiteLink ( $datarray [ 'tsitelink' ], $datarray [ 'hsitelink' ])
-> withItemLink ( $datarray [ 'itemlink' ]);
// If a photo is present, add it to the email
if ( ! empty ( $datarray [ 'source_photo' ])) {
2020-09-19 18:14:55 +00:00
$emailBuilder -> withPhoto (
2020-02-02 23:07:22 +00:00
$datarray [ 'source_photo' ],
$datarray [ 'source_link' ] ? ? $sitelink ,
$datarray [ 'source_name' ] ? ? $sitename );
}
2011-12-24 07:07:05 +00:00
2020-09-19 18:14:55 +00:00
$email = $emailBuilder -> build ();
2020-01-26 19:23:58 +00:00
// use the Emailer class to send the message
return DI :: emailer () -> send ( $email );
2011-12-24 07:07:05 +00:00
}
2012-02-18 10:57:42 +00:00
2017-11-20 20:37:30 +00:00
return false ;
2011-12-24 07:07:05 +00:00
}
2011-12-08 09:28:27 +00:00
2021-08-21 20:35:04 +00:00
function notification_from_array ( array $notification )
{
2021-08-23 15:07:14 +00:00
Logger :: info ( 'Start' , [ 'uid' => $notification [ 'uid' ], 'id' => $notification [ 'id' ], 'type' => $notification [ 'type' ]]);
2021-08-21 20:35:04 +00:00
if ( $notification [ 'type' ] == Post\UserNotification :: NOTIF_NONE ) {
2021-08-23 15:07:14 +00:00
Logger :: info ( 'Not an item based notification, quitting' , [ 'uid' => $notification [ 'uid' ], 'id' => $notification [ 'id' ], 'type' => $notification [ 'type' ]]);
2021-08-21 20:35:04 +00:00
return false ;
}
$params = [];
$params [ 'verb' ] = Verb :: getByID ( $notification [ 'vid' ]);
$params [ 'uid' ] = $notification [ 'uid' ];
$params [ 'otype' ] = Notification\ObjectType :: ITEM ;
$user = User :: getById ( $notification [ 'uid' ]);
$params [ 'notify_flags' ] = $user [ 'notify-flags' ];
$params [ 'language' ] = $user [ 'language' ];
$params [ 'to_name' ] = $user [ 'username' ];
$params [ 'to_email' ] = $user [ 'email' ];
// from here on everything is in the recipients language
$l10n = DI :: l10n () -> withLang ( $user [ 'language' ]);
$contact = Contact :: getById ( $notification [ 'actor-id' ], [ 'url' , 'name' , 'photo' ]);
if ( DBA :: isResult ( $contact )) {
2021-08-24 08:53:15 +00:00
$params [ 'source_link' ] = $contact [ 'url' ];
$params [ 'source_name' ] = $contact [ 'name' ];
$params [ 'source_photo' ] = $contact [ 'photo' ];
2021-08-21 20:35:04 +00:00
}
$item = Post :: selectFirstForUser ( $notification [ 'uid' ], Item :: ITEM_FIELDLIST ,
[ 'uid' => [ 0 , $notification [ 'uid' ]], 'uri-id' => $notification [ 'target-uri-id' ], 'deleted' => false ],
[ 'order' => [ 'uid' => true ]]);
if ( empty ( $item )) {
2021-08-23 15:07:14 +00:00
Logger :: info ( 'Item not found' , [ 'uri-id' => $notification [ 'target-uri-id' ], 'type' => $notification [ 'type' ]]);
2021-08-21 20:35:04 +00:00
return false ;
}
$params [ 'item' ] = $item ;
$params [ 'parent' ] = $item [ 'parent' ];
$params [ 'link' ] = DI :: baseUrl () . '/display/' . urlencode ( $item [ 'guid' ]);
$subjectPrefix = $l10n -> t ( '[Friendica:Notify]' );
if ( Post\ThreadUser :: getIgnored ( $notification [ 'parent-uri-id' ], $notification [ 'uid' ])) {
2021-08-23 15:07:14 +00:00
Logger :: info ( 'Thread is ignored' , [ 'parent-uri-id' => $notification [ 'parent-uri-id' ], 'type' => $notification [ 'type' ]]);
2021-08-21 20:35:04 +00:00
return false ;
}
// Check to see if there was already a tag notify or comment notify for this post.
// If so don't create a second notification
$condition = [ 'type' => [ Notification\Type :: TAG_SELF , Notification\Type :: COMMENT , Notification\Type :: SHARE ],
2021-08-23 15:07:14 +00:00
'link' => $params [ 'link' ], 'verb' => Activity :: POST , 'uid' => $notification [ 'uid' ]];
2021-08-21 20:35:04 +00:00
if ( DBA :: exists ( 'notify' , $condition )) {
2021-08-23 15:07:14 +00:00
Logger :: info ( 'Duplicate found, quitting' , $condition );
2021-08-21 20:35:04 +00:00
return false ;
}
$content = Plaintext :: getPost ( $item , 70 );
if ( ! empty ( $content [ 'text' ])) {
$title = '"' . trim ( str_replace ( " \n " , " " , $content [ 'text' ])) . '"' ;
} else {
$title = $item [ 'title' ];
}
// Some mail software relies on subject field for threading.
// So, we cannot have different subjects for notifications of the same thread.
// Before this we have the name of the replier on the subject rendering
// different subjects for messages on the same thread.
if ( $notification [ 'type' ] == Post\UserNotification :: NOTIF_EXPLICIT_TAGGED ) {
$params [ 'type' ] = Notification\Type :: TAG_SELF ;
$subject = $l10n -> t ( '%s %s tagged you' , $subjectPrefix , $contact [ 'name' ]);
} elseif ( $notification [ 'type' ] == Post\UserNotification :: NOTIF_SHARED ) {
$params [ 'type' ] = Notification\Type :: SHARE ;
$subject = $l10n -> t ( '%s %s shared a new post' , $subjectPrefix , $contact [ 'name' ]);
} else {
$params [ 'type' ] = Notification\Type :: COMMENT ;
$subject = $l10n -> t ( '%1$s Comment to conversation #%2$d by %3$s' , $subjectPrefix , $item [ 'parent' ], $contact [ 'name' ]);
}
$msg = Notification :: getMessage ( $notification );
if ( empty ( $msg )) {
2021-08-23 15:07:14 +00:00
Logger :: info ( 'No notification message, quitting' , [ 'uid' => $notification [ 'uid' ], 'id' => $notification [ 'id' ], 'type' => $notification [ 'type' ]]);
2021-08-21 20:35:04 +00:00
return false ;
}
$preamble = $msg [ 'plain' ];
$epreamble = $msg [ 'rich' ];
$sitename = DI :: config () -> get ( 'config' , 'sitename' );
$siteurl = DI :: baseUrl () -> get ( true );
$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' ];
2021-08-23 15:07:14 +00:00
Logger :: info ( 'Perform notification' , [ 'uid' => $notification [ 'uid' ], 'id' => $notification [ 'id' ], 'type' => $notification [ 'type' ]]);
2021-08-21 20:35:04 +00:00
return notification_store_and_send ( $params , $sitelink , $tsitelink , $hsitelink , $title , $subject , $preamble , $epreamble , $item [ 'body' ], $itemlink , true );
}