]> git.mxchange.org Git - friendica.git/blob - mod/message.php
rev update
[friendica.git] / mod / message.php
1 <?php
2
3 require_once('include/acl_selectors.php');
4 require_once('include/message.php');
5
6 function message_init(&$a) {
7         $tabs = array();
8         $new = array(
9                 'label' => t('New Message'),
10                 'url' => $a->get_baseurl(true) . '/message/new',
11                 'sel'=> ($a->argv[1] == 'new'),
12         );
13         
14         $tpl = get_markup_template('message_side.tpl');
15         $a->page['aside'] = replace_macros($tpl, array(
16                 '$tabs'=>$tabs,
17                 '$new'=>$new,
18         ));
19         $base = $a->get_baseurl();
20
21         $head_tpl = get_markup_template('message-head.tpl');
22         $a->page['htmlhead'] .= replace_macros($head_tpl,array(
23                 '$baseurl' => $a->get_baseurl(true),
24                 '$base' => $base
25         ));
26
27         $end_tpl = get_markup_template('message-end.tpl');
28         $a->page['end'] .= replace_macros($end_tpl,array(
29                 '$baseurl' => $a->get_baseurl(true),
30                 '$base' => $base
31         ));
32         
33 }
34
35 function message_post(&$a) {
36
37         if(! local_user()) {
38                 notice( t('Permission denied.') . EOL);
39                 return;
40         }
41
42         $replyto   = ((x($_REQUEST,'replyto'))   ? notags(trim($_REQUEST['replyto']))   : '');
43         $subject   = ((x($_REQUEST,'subject'))   ? notags(trim($_REQUEST['subject']))   : '');
44         $body      = ((x($_REQUEST,'body'))      ? escape_tags(trim($_REQUEST['body'])) : '');
45         $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto'])       : 0 );
46
47         // Work around doubled linefeeds in Tinymce 3.5b2
48
49         $plaintext = intval(get_pconfig(local_user(),'system','plaintext'));
50         if(! $plaintext) {
51                 $body = fix_mce_lf($body);
52         }
53         
54         $ret = send_message($recipient, $body, $subject, $replyto);
55         $norecip = false;
56
57         switch($ret){
58                 case -1:
59                         notice( t('No recipient selected.') . EOL );
60                         $norecip = true;
61                         break;
62                 case -2:
63                         notice( t('Unable to locate contact information.') . EOL );
64                         break;
65                 case -3:
66                         notice( t('Message could not be sent.') . EOL );
67                         break;
68                 case -4:
69                         notice( t('Message collection failure.') . EOL );
70                         break;
71                 default:
72                         info( t('Message sent.') . EOL );
73         }
74
75         // fake it to go back to the input form if no recipient listed
76
77         if($norecip) {
78                 $a->argc = 2;
79                 $a->argv[1] = 'new';
80         }
81
82 }
83
84 // Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
85 // is identical to the code in include/conversation.php
86 if(! function_exists('item_extract_images')) {
87 function item_extract_images($body) {
88
89         $saved_image = array();
90         $orig_body = $body;
91         $new_body = '';
92
93         $cnt = 0;
94         $img_start = strpos($orig_body, '[img');
95         $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
96         $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
97         while(($img_st_close !== false) && ($img_end !== false)) {
98
99                 $img_st_close++; // make it point to AFTER the closing bracket
100                 $img_end += $img_start;
101
102                 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
103                         // This is an embedded image
104
105                         $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
106                         $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
107
108                         $cnt++;
109                 }
110                 else
111                         $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
112
113                 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
114
115                 if($orig_body === false) // in case the body ends on a closing image tag
116                         $orig_body = '';
117
118                 $img_start = strpos($orig_body, '[img');
119                 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
120                 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
121         }
122
123         $new_body = $new_body . $orig_body;
124
125         return array('body' => $new_body, 'images' => $saved_image);
126 }}
127
128 if(! function_exists('item_redir_and_replace_images')) {
129 function item_redir_and_replace_images($body, $images, $cid) {
130
131         $origbody = $body;
132         $newbody = '';
133
134         for($i = 0; $i < count($images); $i++) {
135                 $search = '/\[url\=(.*?)\]\[!#saved_image' . $i . '#!\]\[\/url\]' . '/is';
136                 $replace = '[url=' . z_path() . '/redir/' . $cid 
137                            . '?f=1&url=' . '$1' . '][!#saved_image' . $i . '#!][/url]' ;
138
139                 $img_end = strpos($origbody, '[!#saved_image' . $i . '#!][/url]') + strlen('[!#saved_image' . $i . '#!][/url]');
140                 $process_part = substr($origbody, 0, $img_end);
141                 $origbody = substr($origbody, $img_end);
142
143                 $process_part = preg_replace($search, $replace, $process_part);
144                 $newbody = $newbody . $process_part;
145         }
146         $newbody = $newbody . $origbody;
147
148         $cnt = 0;
149         foreach($images as $image) {
150                 // We're depending on the property of 'foreach' (specified on the PHP website) that
151                 // it loops over the array starting from the first element and going sequentially
152                 // to the last element
153                 $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
154                 $cnt++;
155         }
156
157         return $newbody;
158 }}
159
160
161
162 function message_content(&$a) {
163
164         $o = '';
165         nav_set_selected('messages');
166
167         if(! local_user()) {
168                 notice( t('Permission denied.') . EOL);
169                 return;
170         }
171
172         $myprofile = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
173
174         $tpl = get_markup_template('mail_head.tpl');
175         $header = replace_macros($tpl, array(
176                 '$messages' => t('Messages'),
177                 '$tab_content' => $tab_content
178         ));
179
180
181         if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
182                 if(! intval($a->argv[2]))
183                         return;
184                 $cmd = $a->argv[1];
185                 if($cmd === 'drop') {
186                         $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
187                                 intval($a->argv[2]),
188                                 intval(local_user())
189                         );
190                         if($r) {
191                                 info( t('Message deleted.') . EOL );
192                         }
193                         goaway($a->get_baseurl(true) . '/message' );
194                 }
195                 else {
196                         $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
197                                 intval($a->argv[2]),
198                                 intval(local_user())
199                         );
200                         if(count($r)) {
201                                 $parent = $r[0]['parent-uri'];
202                                 $convid = $r[0]['convid'];
203
204                                 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
205                                         dbesc($parent),
206                                         intval(local_user())
207                                 );
208
209                                 // remove diaspora conversation pointer
210                                 // Actually if we do this, we can never receive another reply to that conversation,
211                                 // as we will never again have the info we need to re-create it. 
212                                 // We'll just have to orphan it. 
213
214                                 //if($convid) {
215                                 //      q("delete from conv where id = %d limit 1",
216                                 //              intval($convid)
217                                 //      );
218                                 //}
219
220                                 if($r)
221                                         info( t('Conversation removed.') . EOL );
222                         } 
223                         goaway($a->get_baseurl(true) . '/message' );
224                 }       
225         
226         }
227
228         if(($a->argc > 1) && ($a->argv[1] === 'new')) {
229                 
230                 $o .= $header;
231                 
232                 $plaintext = false;
233                 if(intval(get_pconfig(local_user(),'system','plaintext')))
234                         $plaintext = true;
235
236
237                 $tpl = get_markup_template('msg-header.tpl');
238                 $a->page['htmlhead'] .= replace_macros($tpl, array(
239                         '$baseurl' => $a->get_baseurl(true),
240                         '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
241                         '$nickname' => $a->user['nickname'],
242                         '$linkurl' => t('Please enter a link URL:')
243                 ));
244         
245                 $tpl = get_markup_template('msg-end.tpl');
246                 $a->page['end'] .= replace_macros($tpl, array(
247                         '$baseurl' => $a->get_baseurl(true),
248                         '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
249                         '$nickname' => $a->user['nickname'],
250                         '$linkurl' => t('Please enter a link URL:')
251                 ));
252         
253                 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
254                         
255
256                 $prename = $preurl = $preid = '';
257
258                 if($preselect) {
259                         $r = q("select name, url, id from contact where uid = %d and id = %d limit 1",
260                                 intval(local_user()),
261                                 intval($a->argv[2])
262                         );
263                         if(count($r)) {
264                                 $prename = $r[0]['name'];
265                                 $preurl = $r[0]['url'];
266                                 $preid = $r[0]['id'];
267                         }
268                 }        
269
270                 $prefill = (($preselect) ? $prename  : '');
271
272                 // the ugly select box
273                 
274                 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
275
276                 $tpl = get_markup_template('prv_message.tpl');
277                 $o .= replace_macros($tpl,array(
278                         '$header' => t('Send Private Message'),
279                         '$to' => t('To:'),
280                         '$showinputs' => 'true', 
281                         '$prefill' => $prefill,
282                         '$autocomp' => $autocomp,
283                         '$preid' => $preid,
284                         '$subject' => t('Subject:'),
285                         '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
286                         '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
287                         '$readonly' => '',
288                         '$yourmessage' => t('Your message:'),
289                         '$select' => $select,
290                         '$parent' => '',
291                         '$upload' => t('Upload photo'),
292                         '$insert' => t('Insert web link'),
293                         '$wait' => t('Please wait'),
294                         '$submit' => t('Submit')
295                 ));
296
297                 return $o;
298         }
299
300         if($a->argc == 1) {
301
302                 // list messages
303
304                 $o .= $header;
305
306                 
307                 $r = q("SELECT count(*) AS `total` FROM `mail` 
308                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
309                         intval(local_user()),
310                         dbesc($myprofile)
311                 );
312                 if(count($r))
313                         $a->set_pager_total($r[0]['total']);
314
315                 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`, 
316                         `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
317                         count( * ) as count
318                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
319                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC  LIMIT %d , %d ",
320                         intval(local_user()),
321                         //
322                         intval($a->pager['start']),
323                         intval($a->pager['itemspage'])
324                 );
325
326                 if(! count($r)) {
327                         info( t('No messages.') . EOL);
328                         return $o;
329                 }
330
331                 $tpl = get_markup_template('mail_list.tpl');
332                 foreach($r as $rr) {
333                         if($rr['unknown']) {
334                                 $partecipants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
335                         }
336                         elseif (link_compare($rr['from-url'],$myprofile)){
337                                 $partecipants = sprintf( t("You and %s"), $rr['name']);
338                         }
339                         else {
340                                 $partecipants = sprintf( t("%s and You"), $rr['from-name']);
341                         }
342                         
343                         $o .= replace_macros($tpl, array(
344                                 '$id' => $rr['id'],
345                                 '$from_name' => $partecipants,
346                                 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
347                                 '$sparkle' => ' sparkle',
348                                 '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
349                                 '$subject' => template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>')),
350                                 '$delete' => t('Delete conversation'),
351                                 '$body' => template_escape($rr['body']),
352                                 '$to_name' => template_escape($rr['name']),
353                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
354                                 '$ago' => relative_date($rr['mailcreated']),
355                                 '$seen' => $rr['mailseen'],
356                                 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),
357                         ));
358                 }
359                 $o .= paginate($a);     
360                 return $o;
361         }
362
363         if(($a->argc > 1) && (intval($a->argv[1]))) {
364
365                 $o .= $header;
366
367                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
368                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
369                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
370                         intval(local_user()),
371                         intval($a->argv[1])
372                 );
373                 if(count($r)) { 
374                         $contact_id = $r[0]['contact-id'];
375                         $convid = $r[0]['convid'];
376
377                         $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
378                         if($convid)
379                                 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
380                                         dbesc($r[0]['parent-uri']),
381                                         intval($convid)
382                                 );  
383
384                         $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
385                                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
386                                 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
387                                 intval(local_user())
388                         );
389                 }
390                 if(! count($messages)) {
391                         notice( t('Message not available.') . EOL );
392                         return $o;
393                 }
394
395                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
396                         dbesc($r[0]['parent-uri']),
397                         intval(local_user())
398                 );
399
400                 require_once("include/bbcode.php");
401
402                 $tpl = get_markup_template('msg-header.tpl');
403                 $a->page['htmlhead'] .= replace_macros($tpl, array(
404                         '$nickname' => $a->user['nickname'],
405                         '$baseurl' => $a->get_baseurl(true)
406                 ));
407
408                 $tpl = get_markup_template('msg-end.tpl');
409                 $a->page['end'] .= replace_macros($tpl, array(
410                         '$nickname' => $a->user['nickname'],
411                         '$baseurl' => $a->get_baseurl(true)
412                 ));
413
414
415                 $mails = array();
416                 $seen = 0;
417                 $unknown = false;
418
419                 foreach($messages as $message) {
420                         if($message['unknown'])
421                                 $unknown = true;
422                         if($message['from-url'] == $myprofile) {
423                                 $from_url = $myprofile;
424                                 $sparkle = '';
425                         }
426                         else {
427                                 $from_url = $a->get_baseurl(true) . '/redir/' . $message['contact-id'];
428                                 $sparkle = ' sparkle';
429                         }
430
431
432                         $extracted = item_extract_images($message['body']);
433                         if($extracted['images'])
434                                 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
435
436                         $mails[] = array(
437                                 'id' => $message['id'],
438                                 'from_name' => template_escape($message['from-name']),
439                                 'from_url' => $from_url,
440                                 'sparkle' => $sparkle,
441                                 'from_photo' => $message['from-photo'],
442                                 'subject' => template_escape($message['title']),
443                                 'body' => template_escape(smilies(bbcode($message['body']))),
444                                 'delete' => t('Delete message'),
445                                 'to_name' => template_escape($message['name']),
446                                 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
447                                 'ago' => relative_date($message['created']),
448                         );
449                                 
450                         $seen = $message['seen'];
451                 }
452
453
454                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
455                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
456
457                 $tpl = get_markup_template('mail_display.tpl');
458                 $o = replace_macros($tpl, array(
459                         '$thread_id' => $a->argv[1],
460                         '$thread_subject' => $message['title'],
461                         '$thread_seen' => $seen,
462                         '$delete' =>  t('Delete conversation'),
463                         '$canreply' => (($unknown) ? false : '1'),
464                         '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),                        
465                         '$mails' => $mails,
466                         
467                         // reply
468                         '$header' => t('Send Reply'),
469                         '$to' => t('To:'),
470                         '$showinputs' => '',
471                         '$subject' => t('Subject:'),
472                         '$subjtxt' => template_escape($message['title']),
473                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
474                         '$yourmessage' => t('Your message:'),
475                         '$text' => '',
476                         '$select' => $select,
477                         '$parent' => $parent,
478                         '$upload' => t('Upload photo'),
479                         '$insert' => t('Insert web link'),
480                         '$submit' => t('Submit'),
481                         '$wait' => t('Please wait')
482
483                 ));
484
485                 return $o;
486         }
487
488 }