]> git.mxchange.org Git - friendica.git/blob - mod/message.php
allow more than one embedded private photo
[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         $a->page['htmlhead'] .= '<script src="' . $a->get_baseurl(true) . '/library/jquery_ac/friendica.complete.js" ></script>';
22         $a->page['htmlhead'] .= <<< EOT
23
24 <script>$(document).ready(function() { 
25         var a; 
26         a = $("#recip").autocomplete({ 
27                 serviceUrl: '$base/acl',
28                 minChars: 2,
29                 width: 350,
30                 onSelect: function(value,data) {
31                         $("#recip-complete").val(data);
32                 }                       
33         });
34
35 }); 
36
37 </script>
38 EOT;
39         
40 }
41
42 function message_post(&$a) {
43
44         if(! local_user()) {
45                 notice( t('Permission denied.') . EOL);
46                 return;
47         }
48
49         $replyto   = ((x($_REQUEST,'replyto'))   ? notags(trim($_REQUEST['replyto']))   : '');
50         $subject   = ((x($_REQUEST,'subject'))   ? notags(trim($_REQUEST['subject']))   : '');
51         $body      = ((x($_REQUEST,'body'))      ? escape_tags(trim($_REQUEST['body'])) : '');
52         $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto'])       : 0 );
53
54         // Work around doubled linefeeds in Tinymce 3.5b2
55
56         $plaintext = intval(get_pconfig(local_user(),'system','plaintext'));
57         if(! $plaintext) {
58                 $body = fix_mce_lf($body);
59         }
60         
61         $ret = send_message($recipient, $body, $subject, $replyto);
62         $norecip = false;
63
64         switch($ret){
65                 case -1:
66                         notice( t('No recipient selected.') . EOL );
67                         $norecip = true;
68                         break;
69                 case -2:
70                         notice( t('Unable to locate contact information.') . EOL );
71                         break;
72                 case -3:
73                         notice( t('Message could not be sent.') . EOL );
74                         break;
75                 case -4:
76                         notice( t('Message collection failure.') . EOL );
77                         break;
78                 default:
79                         info( t('Message sent.') . EOL );
80         }
81
82         // fake it to go back to the input form if no recipient listed
83
84         if($norecip) {
85                 $a->argc = 2;
86                 $a->argv[1] = 'new';
87         }
88
89 }
90
91 // Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
92 // is identical to the code in include/conversation.php
93 if(! function_exists('item_extract_images')) {
94 function item_extract_images($body) {
95
96         $saved_image = array();
97         $orig_body = $body;
98         $new_body = '';
99
100         $cnt = 0;
101         $img_start = strpos($orig_body, '[img');
102         $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
103         $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
104         while(($img_st_close !== false) && ($img_end !== false)) {
105
106                 $img_st_close++; // make it point to AFTER the closing bracket
107                 $img_end += $img_start;
108
109                 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
110                         // This is an embedded image
111
112                         $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - $img_start);
113                         $cnt++;
114
115                         $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
116                 }
117                 else
118                         $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
119
120                 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
121
122                 if($orig_body === false) // in case the body ends on a closing image tag
123                         $orig_body = '';
124
125                 $img_start = strpos($orig_body, '[img');
126                 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
127                 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
128         }
129
130         $new_body = $new_body . $orig_body;
131
132         return array('body' => $new_body, 'images' => $saved_image);
133 }}
134
135 if(! function_exists('item_redir_and_replace_images')) {
136 function item_redir_and_replace_images($body, $images, $cid) {
137
138         $newbody = $body;
139
140         for($i = 0; $i < count($images); $i++) {
141                 $search = '/\[url\=(.*?)\]\[!#saved_image' . $i . '#!\]\[\/url\]' . '/is';
142                 $replace = '[url=' . z_path() . '/redir/' . $cid 
143                            . '?f=1&url=' . '$1' . '][!#saved_image' . $i . '#!][/url]' ;
144
145                 $newbody = preg_replace($search, $replace, $newbody);
146         }
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
239                 $a->page['htmlhead'] .= replace_macros($tpl, array(
240                         '$baseurl' => $a->get_baseurl(true),
241                         '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
242                         '$nickname' => $a->user['nickname'],
243                         '$linkurl' => t('Please enter a link URL:')
244                 ));
245         
246                 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
247                         
248
249                 $prename = $preurl = $preid = '';
250
251                 if($preselect) {
252                         $r = q("select name, url, id from contact where uid = %d and id = %d limit 1",
253                                 intval(local_user()),
254                                 intval($a->argv[2])
255                         );
256                         if(count($r)) {
257                                 $prename = $r[0]['name'];
258                                 $preurl = $r[0]['url'];
259                                 $preid = $r[0]['id'];
260                         }
261                 }        
262
263                 $prefill = (($preselect) ? $prename  : '');
264
265                 // the ugly select box
266                 
267                 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
268
269                 $tpl = get_markup_template('prv_message.tpl');
270                 $o .= replace_macros($tpl,array(
271                         '$header' => t('Send Private Message'),
272                         '$to' => t('To:'),
273                         '$showinputs' => 'true', 
274                         '$prefill' => $prefill,
275                         '$autocomp' => $autocomp,
276                         '$preid' => $preid,
277                         '$subject' => t('Subject:'),
278                         '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
279                         '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
280                         '$readonly' => '',
281                         '$yourmessage' => t('Your message:'),
282                         '$select' => $select,
283                         '$parent' => '',
284                         '$upload' => t('Upload photo'),
285                         '$insert' => t('Insert web link'),
286                         '$wait' => t('Please wait'),
287                         '$submit' => t('Submit')
288                 ));
289
290                 return $o;
291         }
292
293         if($a->argc == 1) {
294
295                 // list messages
296
297                 $o .= $header;
298
299                 
300                 $r = q("SELECT count(*) AS `total` FROM `mail` 
301                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
302                         intval(local_user()),
303                         dbesc($myprofile)
304                 );
305                 if(count($r))
306                         $a->set_pager_total($r[0]['total']);
307
308                 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`, 
309                         `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
310                         count( * ) as count
311                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
312                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC  LIMIT %d , %d ",
313                         intval(local_user()),
314                         //
315                         intval($a->pager['start']),
316                         intval($a->pager['itemspage'])
317                 );
318
319                 if(! count($r)) {
320                         info( t('No messages.') . EOL);
321                         return $o;
322                 }
323
324                 $tpl = get_markup_template('mail_list.tpl');
325                 foreach($r as $rr) {
326                         if($rr['unknown']) {
327                                 $partecipants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
328                         }
329                         elseif (link_compare($rr['from-url'],$myprofile)){
330                                 $partecipants = sprintf( t("You and %s"), $rr['name']);
331                         }
332                         else {
333                                 $partecipants = sprintf( t("%s and You"), $rr['from-name']);
334                         }
335                         
336                         $o .= replace_macros($tpl, array(
337                                 '$id' => $rr['id'],
338                                 '$from_name' => $partecipants,
339                                 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
340                                 '$sparkle' => ' sparkle',
341                                 '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
342                                 '$subject' => template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>')),
343                                 '$delete' => t('Delete conversation'),
344                                 '$body' => template_escape($rr['body']),
345                                 '$to_name' => template_escape($rr['name']),
346                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
347                                 '$seen' => $rr['mailseen'],
348                                 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),
349                         ));
350                 }
351                 $o .= paginate($a);     
352                 return $o;
353         }
354
355         if(($a->argc > 1) && (intval($a->argv[1]))) {
356
357                 $o .= $header;
358
359                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
360                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
361                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
362                         intval(local_user()),
363                         intval($a->argv[1])
364                 );
365                 if(count($r)) { 
366                         $contact_id = $r[0]['contact-id'];
367                         $convid = $r[0]['convid'];
368
369                         $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
370                         if($convid)
371                                 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
372                                         dbesc($r[0]['parent-uri']),
373                                         intval($convid)
374                                 );  
375
376                         $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
377                                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
378                                 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
379                                 intval(local_user())
380                         );
381                 }
382                 if(! count($messages)) {
383                         notice( t('Message not available.') . EOL );
384                         return $o;
385                 }
386
387                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
388                         dbesc($r[0]['parent-uri']),
389                         intval(local_user())
390                 );
391
392                 require_once("include/bbcode.php");
393
394                 $tpl = get_markup_template('msg-header.tpl');
395         
396                 $a->page['htmlhead'] .= replace_macros($tpl, array(
397                         '$nickname' => $a->user['nickname'],
398                         '$baseurl' => $a->get_baseurl(true)
399                 ));
400
401
402                 $mails = array();
403                 $seen = 0;
404                 $unknown = false;
405
406                 foreach($messages as $message) {
407                         if($message['unknown'])
408                                 $unknown = true;
409                         if($message['from-url'] == $myprofile) {
410                                 $from_url = $myprofile;
411                                 $sparkle = '';
412                         }
413                         else {
414                                 $from_url = $a->get_baseurl(true) . '/redir/' . $message['contact-id'];
415                                 $sparkle = ' sparkle';
416                         }
417
418
419                         $extracted = item_extract_images($message['body']);
420                         if($extracted['images'])
421                                 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
422
423                         $mails[] = array(
424                                 'id' => $message['id'],
425                                 'from_name' => template_escape($message['from-name']),
426                                 'from_url' => $from_url,
427                                 'sparkle' => $sparkle,
428                                 'from_photo' => $message['from-photo'],
429                                 'subject' => template_escape($message['title']),
430                                 'body' => template_escape(smilies(bbcode($message['body']))),
431                                 'delete' => t('Delete message'),
432                                 'to_name' => template_escape($message['name']),
433                                 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
434                         );
435                                 
436                         $seen = $message['seen'];
437                 }
438
439
440                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
441                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
442
443                 $tpl = get_markup_template('mail_display.tpl');
444                 $o = replace_macros($tpl, array(
445                         '$thread_id' => $a->argv[1],
446                         '$thread_subject' => $message['title'],
447                         '$thread_seen' => $seen,
448                         '$delete' =>  t('Delete conversation'),
449                         '$canreply' => (($unknown) ? false : '1'),
450                         '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),                        
451                         '$mails' => $mails,
452                         
453                         // reply
454                         '$header' => t('Send Reply'),
455                         '$to' => t('To:'),
456                         '$showinputs' => '',
457                         '$subject' => t('Subject:'),
458                         '$subjtxt' => template_escape($message['title']),
459                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
460                         '$yourmessage' => t('Your message:'),
461                         '$text' => '',
462                         '$select' => $select,
463                         '$parent' => $parent,
464                         '$upload' => t('Upload photo'),
465                         '$insert' => t('Insert web link'),
466                         '$submit' => t('Submit'),
467                         '$wait' => t('Please wait')
468
469                 ));
470
471                 return $o;
472         }
473
474 }