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