]> git.mxchange.org Git - friendica.git/blob - include/enotify.php
extending notifications to other types
[friendica.git] / include / enotify.php
1 <?php
2
3 function notification($params) {
4
5         logger('notification: entry');
6
7         $a = get_app();
8         $banner = t('Friendica Notification');
9         $product = FRIENDICA_PLATFORM;
10         $siteurl = z_path();
11         $thanks = t('Thank You,');
12         $sitename = get_config('config','sitename');
13         $site_admin = sprintf( t('%s Administrator'), $sitename);
14
15         $sender_name = t('Administrator');
16         $sender_email = t('noreply') . '@' . $a->get_hostname();
17
18         $title = $params['item']['title'];
19         $body = $params['item']['body'];
20
21         if($params['type'] == NOTIFY_MAIL) {
22
23                 $subject =      sprintf( t('New mail received at %s'),$sitename);
24
25                 $preamble = sprintf( t('%s sent you a new private message at %s.'),$params['source_name'],$sitename);
26
27                 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
28                 $tsitelink = sprintf( $sitelink, $siteurl . '/message' );
29                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '/message">' . $sitename . '</a>');
30                 $itemlink = '';
31         }
32
33         if($params['type'] == NOTIFY_COMMENT) {
34
35                 $preamble = $subject = sprintf( t('%s commented on an item at %s'), $params['source_name'], $sitename);
36
37                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
38                 $tsitelink = sprintf( $sitelink, $siteurl );
39                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
40                 $itemlink =  $params['link'];
41         }
42
43         if($params['type'] == NOTIFY_WALL) {
44                 $preamble = $subject =  sprintf( t('%s posted to your profile wall at %s') , $params['source_name'], $sitename);
45
46                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
47                 $tsitelink = sprintf( $sitelink, $siteurl );
48                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
49                 $itemlink =  $params['link'];
50         }
51
52
53         // send email notification if notification preferences permit
54
55         require_once('bbcode.php');
56         if(intval($params['notify_flags']) & intval($params['type'])) {
57
58                 logger('notification: sending email');
59
60                 push_lang($params['language']);
61
62                 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
63                         $body))),ENT_QUOTES,'UTF-8'));
64                 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), 
65                         "<br />\n",$body))));
66
67                 // load the template for private message notifications
68                 $tpl = get_markup_template('email_notify_html.tpl');
69                 $email_html_body = replace_macros($tpl,array(
70                         '$banner'       => $banner,
71                         '$product'      => $product,
72                         '$preamble'     => $preamble,
73                         '$sitename'     => $sitename,
74                         '$siteurl'      => $siteurl,
75                         '$source_name'  => $parama['source_name'],
76                         '$source_link'  => $params['source_link'],
77                         '$source_photo' => $params['source_photo'],
78                         '$username'     => $params['to_name'],
79                         '$hsitelink'    => $hsitelink,
80                         '$itemlink'     => $itemlink,
81                         '$thanks'       => $thanks,
82                         '$site_admin'   => $site_admin,
83                         '$title'                => stripslashes($title),
84                         '$htmlversion'  => $htmlversion,        
85                 ));
86                 
87                 // load the template for private message notifications
88                 $tpl = get_markup_template('email_notify_text.tpl');
89                 $email_text_body = replace_macros($tpl,array(
90                         '$banner'       => $banner,
91                         '$product'      => $product,
92                         '$preamble'     => $preamble,
93                         '$sitename'     => $sitename,
94                         '$siteurl'      => $siteurl,
95                         '$source_name'  => $parama['source_name'],
96                         '$source_link'  => $params['source_link'],
97                         '$source_photo' => $params['source_photo'],
98                         '$username'     => $params['to_name'],
99                         '$tsitelink'    => $tsitelink,
100                         '$itemlink'     => $itemlink,
101                         '$thanks'       => $thanks,
102                         '$site_admin'   => $site_admin,
103                         '$title'                => stripslashes($title),
104                         '$textversion'  => $textversion,        
105                 ));
106
107                 logger('text: ' . $email_text_body);
108
109                 // use the EmailNotification library to send the message
110
111                 enotify::send(array(
112                         'fromName' => $sender_name,
113                         'fromEmail' => $sender_email,
114                         'replyTo' => $sender_email,
115                         'toEmail' => $params['to_email'],
116                         'messageSubject' => $subject,
117                         'htmlVersion' => $email_html_body,
118                         'textVersion' => $email_text_body
119                 ));
120                 pop_lang();
121         }
122 }
123
124 require_once('include/email.php');
125
126 class enotify {
127         /**
128          * Send a multipart/alternative message with Text and HTML versions
129          *
130          * @param fromName                      name of the sender
131          * @param fromEmail                     email fo the sender
132          * @param replyTo                       replyTo address to direct responses
133          * @param toEmail                       destination email address
134          * @param messageSubject        subject of the message
135          * @param htmlVersion           html version of the message
136          * @param textVersion           text only version of the message
137          */
138         static public function send($params) {
139
140                 $fromName = email_header_encode($params['fromName'],'UTF-8'); 
141                 $messageSubject = email_header_encode($params['messageSubject'],'UTF-8');
142                 
143                 // generate a mime boundary
144                 $mimeBoundary   =rand(0,9)."-"
145                                 .rand(10000000000,9999999999)."-"
146                                 .rand(10000000000,9999999999)."=:"
147                                 .rand(10000,99999);
148
149                 // generate a multipart/alternative message header
150                 $messageHeader =
151                         "From: {$params['fromName']} <{$params['fromEmail']}>\n" . 
152                         "Reply-To: {$params['replyTo']}\n" .
153                         "MIME-Version: 1.0\n" .
154                         "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
155
156                 // assemble the final multipart message body with the text and html types included
157                 $textBody       =       chunk_split(base64_encode($params['textVersion']));
158                 $htmlBody       =       chunk_split(base64_encode($params['htmlVersion']));
159                 $multipartMessageBody =
160                         "--" . $mimeBoundary . "\n" .                                   // plain text section
161                         "Content-Type: text/plain; charset=UTF-8\n" .
162                         "Content-Transfer-Encoding: base64\n\n" .
163                         $textBody . "\n" .
164                         "--" . $mimeBoundary . "\n" .                                   // text/html section
165                         "Content-Type: text/html; charset=UTF-8\n" .
166                         "Content-Transfer-Encoding: base64\n\n" .
167                         $htmlBody . "\n" .
168                         "--" . $mimeBoundary . "--\n";                                  // message ending
169
170                 // send the message
171                 $res = mail(
172                         $params['toEmail'],                                                                             // send to address
173                         $params['messageSubject'],                                                              // subject
174                         $multipartMessageBody,                                                  // message body
175                         $messageHeader                                                                  // message headers
176                 );
177                 logger("enotify::send returns " . $res, LOGGER_DEBUG);
178         }
179 }
180 ?>