]> git.mxchange.org Git - friendica.git/blobdiff - include/enotify.php
Merge pull request #2172 from tobiasd/20151212-frosticons
[friendica.git] / include / enotify.php
index 2dca5a609cdf647e37c2e28be44be094da272194..e02c61383136f5350b01af64928883c5715e924c 100644 (file)
@@ -15,6 +15,46 @@ function notification($params) {
        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'])) {
+           $site_admin = sprintf( t('%s Administrator'), $sitename);
+       } else {
+           $site_admin = sprintf( t('%1$s, %2$s Administrator'), $a->config['admin_name'], $sitename);
+       }
+       $nickname = "";
+
+       $sender_name = $sitename;
+       $hostname = $a->get_hostname();
+       if(strpos($hostname,':'))
+               $hostname = substr($hostname,0,strpos($hostname,':'));
+
+       $sender_email = $a->config['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)
+               $nickname = $user[0]["nickname"];
+
+       // 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);
+
+       $additional_mail_header = "";
+       $additional_mail_header .= "Precedence: list\n";
+       $additional_mail_header .= "X-Friendica-Host: ".$hostname."\n";
+       $additional_mail_header .= "X-Friendica-Account: <".$nickname."@".$hostname.">\n";
+       $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";
+
 
        if(array_key_exists('item',$params)) {
                $title = $params['item']['title'];
@@ -27,6 +67,16 @@ function notification($params) {
        // e.g. "your post", "David's photo", etc.
        $possess_desc = t('%s <!item_type!>');
 
+       if (isset($params['item']['id']))
+               $item_id = $params['item']['id'];
+       else
+               $item_id = 0;
+
+       if (isset($params['parent']))
+               $parent_id = $params['parent'];
+       else
+               $parent_id = 0;
+
        if($params['type'] == NOTIFY_MAIL) {
 
                $subject =      sprintf( t('[Friendica:Notify] New mail received at %s'),$sitename);
@@ -42,7 +92,7 @@ function notification($params) {
        if($params['type'] == NOTIFY_COMMENT) {
 //             logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
 
-               $parent_id = $params['parent'];
+               //$parent_id = $params['parent'];
 
                $p = q("SELECT `ignored` FROM `thread` WHERE `iid` = %d AND `uid` = %d LIMIT 1",
                        intval($parent_id),
@@ -204,6 +254,30 @@ function notification($params) {
                $tsitelink = sprintf( $sitelink, $siteurl );
                $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
                $itemlink =  $params['link'];
+
+               switch ($params['verb']) {
+                       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]',
+                                                                               $sitename);
+                               break;
+                       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]',
+                                                                               $sitename);
+                               break;
+                       default:
+                               // ACTIVITY_REQ_FRIEND is default activity for notifications
+                               break;
+               }
+
+
        }
 
        if($params['type'] == NOTIFY_SUGGEST) {
@@ -225,40 +299,75 @@ 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.');
+
+                       $sitelink = 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
+                       $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']);
+
+                       $sitelink = 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":
+                               $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']);
+
+                               $sitelink = 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;
+               }
        }
 
+       if ($params['type'] == "SYSTEM_EMAIL"){
+               // not part of the notifications.
+               // it just send a mail to the user.
+               // It will be used by the system to send emails to users (like
+               // 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 = "";
+                       $itemlink =  "";
+                       $show_in_notification_page = false;
+       }
 
-       /*$email = prepare_notificaion_mail($params, $subject, $preamble, $body, $sitelink, $tsitelink, $hsitelink, $itemlink);
-       if ($email) Emailer::send($email);
-       pop_lang();*/
-
-
-       $banner = t('Friendica Notification');
-       $product = FRIENDICA_PLATFORM;
-       $siteurl = $a->get_baseurl(true);
-       $thanks = t('Thank You,');
-       $sitename = $a->config['sitename'];
-       $site_admin = sprintf( t('%s Administrator'), $sitename);
-
-       $sender_name = $product;
-       $hostname = $a->get_hostname();
-       if(strpos($hostname,':'))
-               $hostname = substr($hostname,0,strpos($hostname,':'));
-
-       $sender_email = t('noreply') . '@' . $hostname;
-
-
-       $additional_mail_header = "";
-       $additional_mail_header .= "Precedence: list\n";
-       $additional_mail_header .= "X-Friendica-Host: ".$hostname."\n";
-       $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";
+       $subject .= " (".$nickname."@".$hostname.")";
 
        $h = array(
                'params'    => $params,
@@ -285,105 +394,111 @@ function notification($params) {
 
 
 
-       do {
-               $dups = false;
-               $hash = random_string();
-        $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
-                       dbesc($hash));
-               if(count($r))
-                       $dups = true;
-       } while($dups == true);
-
-
-       $datarray = array();
-       $datarray['hash']  = $hash;
-       $datarray['name']  = $params['source_name'];
-       $datarray['url']   = $params['source_link'];
-       $datarray['photo'] = $params['source_photo'];
-       $datarray['date']  = datetime_convert();
-       $datarray['uid']   = $params['uid'];
-       $datarray['link']  = $itemlink;
-       $datarray['parent'] = $parent_id;
-       $datarray['type']  = $params['type'];
-       $datarray['verb']  = $params['verb'];
-       $datarray['otype'] = $params['otype'];
-       $datarray['abort'] = false;
-
-       call_hooks('enotify_store', $datarray);
-
-       if($datarray['abort']) {
-               pop_lang();
-               return False;
-       }
-
-       // create notification entry in DB
-
-       $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
-               values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
-               dbesc($datarray['hash']),
-               dbesc($datarray['name']),
-               dbesc($datarray['url']),
-               dbesc($datarray['photo']),
-               dbesc($datarray['date']),
-               intval($datarray['uid']),
-               dbesc($datarray['link']),
-               intval($datarray['parent']),
-               intval($datarray['type']),
-               dbesc($datarray['verb']),
-               dbesc($datarray['otype'])
-       );
-
-       $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
-               dbesc($hash),
-               intval($params['uid'])
-       );
-       if($r)
-               $notify_id = $r[0]['id'];
-       else {
-               pop_lang();
-               return False;
-       }
+       if ($show_in_notification_page) {
+               logger("adding notification entry", LOGGER_DEBUG);
+               do {
+                       $dups = false;
+                       $hash = random_string();
+                       $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
+                               dbesc($hash));
+                       if(count($r))
+                               $dups = true;
+               } while($dups == true);
 
-       // we seem to have a lot of duplicate comment notifications due to race conditions, mostly from forums
-       // 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",
-               intval(NOTIFY_TAGSELF),
-               intval(NOTIFY_COMMENT),
-               dbesc($params['link']),
-               intval($params['uid'])
-       );
-       if($p && (count($p) > 1)) {
-               for ($d = 1; $d < count($p); $d ++) {
-                       q("delete from notify where id = %d",
-                               intval($p[$d]['id'])
-                       );
+               $datarray = array();
+               $datarray['hash']  = $hash;
+               $datarray['name']  = $params['source_name'];
+               $datarray['url']   = $params['source_link'];
+               $datarray['photo'] = $params['source_photo'];
+               $datarray['date']  = datetime_convert();
+               $datarray['uid']   = $params['uid'];
+               $datarray['link']  = $itemlink;
+               $datarray['iid']   = $item_id;
+               $datarray['parent'] = $parent_id;
+               $datarray['type']  = $params['type'];
+               $datarray['verb']  = $params['verb'];
+               $datarray['otype'] = $params['otype'];
+               $datarray['abort'] = false;
+
+               call_hooks('enotify_store', $datarray);
+
+               if($datarray['abort']) {
+                       pop_lang();
+                       return False;
                }
 
-               // only continue on if we stored the first one
+               // 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')",
+                       dbesc($datarray['hash']),
+                       dbesc($datarray['name']),
+                       dbesc($datarray['url']),
+                       dbesc($datarray['photo']),
+                       dbesc($datarray['date']),
+                       intval($datarray['uid']),
+                       dbesc($datarray['link']),
+                       intval($datarray['iid']),
+                       intval($datarray['parent']),
+                       intval($datarray['type']),
+                       dbesc($datarray['verb']),
+                       dbesc($datarray['otype'])
+               );
 
-               if($notify_id != $p[0]['id']) {
+               $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
+                       dbesc($hash),
+                       intval($params['uid'])
+               );
+               if($r)
+                       $notify_id = $r[0]['id'];
+               else {
                        pop_lang();
                        return False;
                }
-       }
 
+               // we seem to have a lot of duplicate comment notifications due to race conditions, mostly from forums
+               // After we've stored everything, look again to see if there are any duplicates and if so remove them
 
-       $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",
-               dbesc($msg),
-               intval($notify_id),
-               intval($params['uid'])
-       );
+               $p = null;
+               $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']),
+                       intval($params['uid'])
+               );
+               if($p && (count($p) > 1)) {
+                       for ($d = 1; $d < count($p); $d ++) {
+                               q("delete from notify where id = %d",
+                                       intval($p[$d]['id'])
+                               );
+                       }
 
+                       // only continue on if we stored the first one
 
-       // send email notification if notification preferences permit
+                       if($notify_id != $p[0]['id']) {
+                               pop_lang();
+                               return False;
+                       }
+               }
+
+
+               $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",
+                       dbesc($msg),
+                       intval($notify_id),
+                       intval($params['uid'])
+               );
+
+       }
 
-       if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
+       // send email notification if notification preferences permit
+       if((intval($params['notify_flags']) & intval($params['type']))
+               || $params['type'] == NOTIFY_SYSTEM
+               || $params['type'] == "SYSTEM_EMAIL") {
 
-               logger('notification: sending notification email');
+               logger('sending notification email');
 
                if (isset($params['parent']) AND (intval($params['parent']) != 0)) {
                        $id_for_parent = $params['parent']."@".$hostname;
@@ -417,11 +532,13 @@ function notification($params) {
                }
 
 
-               $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
-                       $body))),ENT_QUOTES,'UTF-8'));
+               // 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;
@@ -450,14 +567,15 @@ function notification($params) {
                call_hooks('enotify_mail', $datarray);
 
                // check whether sending post content in email notifications is allowed
-               $content_allowed = !get_config('system','enotify_no_content');
+               // always true for "SYSTEM_EMAIL"
+               $content_allowed = ((!get_config('system','enotify_no_content')) || ($params['type'] == "SYSTEM_EMAIL"));
 
                // load the template for private message notifications
                $tpl = get_markup_template('email_notify_html.tpl');
                $email_html_body = replace_macros($tpl,array(
                        '$banner'       => $datarray['banner'],
                        '$product'      => $datarray['product'],
-                       '$preamble'     => $datarray['preamble'],
+                       '$preamble'     => str_replace("\n","<br>\n",$datarray['preamble']),
                        '$sitename'     => $datarray['sitename'],
                        '$siteurl'      => $datarray['siteurl'],
                        '$source_name'  => $datarray['source_name'],
@@ -498,7 +616,8 @@ function notification($params) {
 
                // use the Emailer class to send the message
 
-               Emailer::send(array(
+               return Emailer::send(array(
+                       'uid' => $params['uid'],
                        'fromName' => $sender_name,
                        'fromEmail' => $sender_email,
                        'replyTo' => $sender_email,
@@ -508,7 +627,6 @@ function notification($params) {
                        'textVersion' => $email_text_body,
                        'additionalMailHeader' => $datarray['headers'],
                ));
-        return True;
        }
 
     return False;