]> git.mxchange.org Git - friendica.git/blob - include/enotify.php
add tag notifications to enotify
[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_TAGSELF) {
58                 $preamble = $subject =  sprintf( t('%s tagged you at %s') , $params['source_name'], $sitename);
59
60                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
61                 $tsitelink = sprintf( $sitelink, $siteurl );
62                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
63                 $itemlink =  $params['link'];
64         }
65
66         if($params['type'] == NOTIFY_TAGSHARE) {
67                 $preamble = $subject =  sprintf( t('%s tagged your post at %s') , $params['source_name'], $sitename);
68
69                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
70                 $tsitelink = sprintf( $sitelink, $siteurl );
71                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
72                 $itemlink =  $params['link'];
73         }
74
75         if($params['type'] == NOTIFY_INTRO) {
76                 $subject = sprintf( t('Introduction received at %s'), $sitename);
77                 $preamble = sprintf( t('You\'ve received an introduction from \'%s\' at %s'), $params['source_name'], $sitename); 
78                 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
79
80                 $sitelink = t('Please visit %s to approve or reject the introduction.');
81                 $tsitelink = sprintf( $sitelink, $siteurl );
82                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
83                 $itemlink =  $params['link'];
84         }
85
86         if($params['type'] == NOTIFY_SUGGEST) {
87                 $subject = sprintf( t('Friend suggestion received at %s'), $sitename);
88                 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%s\' at %s'), $params['source_name'], $sitename); 
89                 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
90                 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
91                 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
92
93                 $sitelink = t('Please visit %s to approve or reject the suggestion.');
94                 $tsitelink = sprintf( $sitelink, $siteurl );
95                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
96                 $itemlink =  $params['link'];
97         }
98
99         if($params['type'] == NOTIFY_CONFIRM) {
100
101         }
102
103         // TODO - create notification entry in DB
104
105
106
107         // send email notification if notification preferences permit
108
109         require_once('bbcode.php');
110         if(intval($params['notify_flags']) & intval($params['type'])) {
111
112                 logger('notification: sending notification email');
113
114                 push_lang($params['language']);
115
116                 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
117                         $body))),ENT_QUOTES,'UTF-8'));
118                 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), 
119                         "<br />\n",$body))));
120
121                 // load the template for private message notifications
122                 $tpl = get_markup_template('email_notify_html.tpl');
123                 $email_html_body = replace_macros($tpl,array(
124                         '$banner'       => $banner,
125                         '$product'      => $product,
126                         '$preamble'     => $preamble,
127                         '$sitename'     => $sitename,
128                         '$siteurl'      => $siteurl,
129                         '$source_name'  => $parama['source_name'],
130                         '$source_link'  => $params['source_link'],
131                         '$source_photo' => $params['source_photo'],
132                         '$username'     => $params['to_name'],
133                         '$hsitelink'    => $hsitelink,
134                         '$itemlink'     => '<a href="' . $itemlink . '">' . $itemlink . '</a>',
135                         '$thanks'       => $thanks,
136                         '$site_admin'   => $site_admin,
137                         '$title'                => stripslashes($title),
138                         '$htmlversion'  => $htmlversion,        
139                 ));
140                 
141                 // load the template for private message notifications
142                 $tpl = get_markup_template('email_notify_text.tpl');
143                 $email_text_body = replace_macros($tpl,array(
144                         '$banner'       => $banner,
145                         '$product'      => $product,
146                         '$preamble'     => $preamble,
147                         '$sitename'     => $sitename,
148                         '$siteurl'      => $siteurl,
149                         '$source_name'  => $parama['source_name'],
150                         '$source_link'  => $params['source_link'],
151                         '$source_photo' => $params['source_photo'],
152                         '$username'     => $params['to_name'],
153                         '$tsitelink'    => $tsitelink,
154                         '$itemlink'     => $itemlink,
155                         '$thanks'       => $thanks,
156                         '$site_admin'   => $site_admin,
157                         '$title'                => stripslashes($title),
158                         '$textversion'  => $textversion,        
159                 ));
160
161 //              logger('text: ' . $email_text_body);
162
163                 // use the EmailNotification library to send the message
164
165                 enotify::send(array(
166                         'fromName' => $sender_name,
167                         'fromEmail' => $sender_email,
168                         'replyTo' => $sender_email,
169                         'toEmail' => $params['to_email'],
170                         'messageSubject' => $subject,
171                         'htmlVersion' => $email_html_body,
172                         'textVersion' => $email_text_body
173                 ));
174                 pop_lang();
175         }
176 }
177
178 require_once('include/email.php');
179
180 class enotify {
181         /**
182          * Send a multipart/alternative message with Text and HTML versions
183          *
184          * @param fromName                      name of the sender
185          * @param fromEmail                     email fo the sender
186          * @param replyTo                       replyTo address to direct responses
187          * @param toEmail                       destination email address
188          * @param messageSubject        subject of the message
189          * @param htmlVersion           html version of the message
190          * @param textVersion           text only version of the message
191          */
192         static public function send($params) {
193
194                 $fromName = email_header_encode($params['fromName'],'UTF-8'); 
195                 $messageSubject = email_header_encode($params['messageSubject'],'UTF-8');
196                 
197                 // generate a mime boundary
198                 $mimeBoundary   =rand(0,9)."-"
199                                 .rand(10000000000,9999999999)."-"
200                                 .rand(10000000000,9999999999)."=:"
201                                 .rand(10000,99999);
202
203                 // generate a multipart/alternative message header
204                 $messageHeader =
205                         "From: {$params['fromName']} <{$params['fromEmail']}>\n" . 
206                         "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
207                         "MIME-Version: 1.0\n" .
208                         "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
209
210                 // assemble the final multipart message body with the text and html types included
211                 $textBody       =       chunk_split(base64_encode($params['textVersion']));
212                 $htmlBody       =       chunk_split(base64_encode($params['htmlVersion']));
213                 $multipartMessageBody =
214                         "--" . $mimeBoundary . "\n" .                                   // plain text section
215                         "Content-Type: text/plain; charset=UTF-8\n" .
216                         "Content-Transfer-Encoding: base64\n\n" .
217                         $textBody . "\n" .
218                         "--" . $mimeBoundary . "\n" .                                   // text/html section
219                         "Content-Type: text/html; charset=UTF-8\n" .
220                         "Content-Transfer-Encoding: base64\n\n" .
221                         $htmlBody . "\n" .
222                         "--" . $mimeBoundary . "--\n";                                  // message ending
223
224                 // send the message
225                 $res = mail(
226                         $params['toEmail'],                                                                             // send to address
227                         $params['messageSubject'],                                                              // subject
228                         $multipartMessageBody,                                                  // message body
229                         $messageHeader                                                                  // message headers
230                 );
231                 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);
232         }
233 }
234 ?>