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