]> git.mxchange.org Git - friendica.git/blob - mod/message.php
1369fde2e2708af28e2d55ddd28ea1e053bda21e
[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         /*
9                 array(
10                         'label' => t('All'),
11                         'url'=> $a->get_baseurl(true) . '/message',
12                         'sel'=> ($a->argc == 1),
13                 ),
14                 array(
15                         'label' => t('Sent'),
16                         'url' => $a->get_baseurl(true) . '/message/sent',
17                         'sel'=> ($a->argv[1] == 'sent'),
18                 ),
19         */
20         );
21         $new = array(
22                 'label' => t('New Message'),
23                 'url' => $a->get_baseurl(true) . '/message/new',
24                 'sel'=> ($a->argv[1] == 'new'),
25         );
26         
27         $tpl = get_markup_template('message_side.tpl');
28         $a->page['aside'] = replace_macros($tpl, array(
29                 '$tabs'=>$tabs,
30                 '$new'=>$new,
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 = str_replace("\r\n","\n",$body);
52                 $body = str_replace("\n\n","\n",$body);
53         }
54         
55         $ret = send_message($recipient, $body, $subject, $replyto);
56         $norecip = false;
57
58         switch($ret){
59                 case -1:
60                         notice( t('No recipient selected.') . EOL );
61                         $norecip = true;
62                         break;
63                 case -2:
64                         notice( t('Unable to locate contact information.') . EOL );
65                         break;
66                 case -3:
67                         notice( t('Message could not be sent.') . EOL );
68                         break;
69                 case -4:
70                         notice( t('Message collection failure.') . EOL );
71                         break;
72                 default:
73                         info( t('Message sent.') . EOL );
74         }
75
76         // fake it to go back to the input form if no recipient listed
77
78         if($norecip) {
79                 $a->argc = 2;
80                 $a->argv[1] = 'new';
81         }
82
83 }
84
85 function message_content(&$a) {
86
87         $o = '';
88         nav_set_selected('messages');
89
90         if(! local_user()) {
91                 notice( t('Permission denied.') . EOL);
92                 return;
93         }
94
95         $myprofile = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
96
97
98
99
100
101         $tpl = get_markup_template('mail_head.tpl');
102         $header = replace_macros($tpl, array(
103                 '$messages' => t('Messages'),
104                 '$tab_content' => $tab_content
105         ));
106
107
108         if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
109                 if(! intval($a->argv[2]))
110                         return;
111                 $cmd = $a->argv[1];
112                 if($cmd === 'drop') {
113                         $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
114                                 intval($a->argv[2]),
115                                 intval(local_user())
116                         );
117                         if($r) {
118                                 info( t('Message deleted.') . EOL );
119                         }
120                         goaway($a->get_baseurl(true) . '/message' );
121                 }
122                 else {
123                         $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
124                                 intval($a->argv[2]),
125                                 intval(local_user())
126                         );
127                         if(count($r)) {
128                                 $parent = $r[0]['parent-uri'];
129                                 $convid = $r[0]['convid'];
130
131                                 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
132                                         dbesc($parent),
133                                         intval(local_user())
134                                 );
135
136                                 // remove diaspora conversation pointer
137                                 // Actually if we do this, we can never receive another reply to that conversation,
138                                 // as we will never again have the info we need to re-create it. 
139                                 // We'll just have to orphan it. 
140
141                                 //if($convid) {
142                                 //      q("delete from conv where id = %d limit 1",
143                                 //              intval($convid)
144                                 //      );
145                                 //}
146
147                                 if($r)
148                                         info( t('Conversation removed.') . EOL );
149                         } 
150                         goaway($a->get_baseurl(true) . '/message' );
151                 }       
152         
153         }
154
155         if(($a->argc > 1) && ($a->argv[1] === 'new')) {
156                 
157                 $o .= $header;
158                 
159                 $plaintext = false;
160                 if(intval(get_pconfig(local_user(),'system','plaintext')))
161                         $plaintext = true;
162
163
164                 $tpl = get_markup_template('msg-header.tpl');
165
166                 $a->page['htmlhead'] .= replace_macros($tpl, array(
167                         '$baseurl' => $a->get_baseurl(true),
168                         '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
169                         '$nickname' => $a->user['nickname'],
170                         '$linkurl' => t('Please enter a link URL:')
171                 ));
172         
173                 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
174         
175                 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
176                 $tpl = get_markup_template('prv_message.tpl');
177                 $o .= replace_macros($tpl,array(
178                         '$header' => t('Send Private Message'),
179                         '$to' => t('To:'),
180                         '$subject' => t('Subject:'),
181                         '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
182                         '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
183                         '$readonly' => '',
184                         '$yourmessage' => t('Your message:'),
185                         '$select' => $select,
186                         '$parent' => '',
187                         '$upload' => t('Upload photo'),
188                         '$insert' => t('Insert web link'),
189                         '$wait' => t('Please wait')
190                 ));
191
192                 return $o;
193         }
194
195         if($a->argc == 1) {
196
197                 // list messages
198
199                 $o .= $header;
200                 
201                 $r = q("SELECT count(*) AS `total` FROM `mail` 
202                         WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC",
203                         intval(local_user()),
204                         dbesc($myprofile)
205                 );
206                 if(count($r))
207                         $a->set_pager_total($r[0]['total']);
208         
209                 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`, 
210                         `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
211                         count( * ) as count
212                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
213                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC  LIMIT %d , %d ",
214                         intval(local_user()),
215                         //
216                         intval($a->pager['start']),
217                         intval($a->pager['itemspage'])
218                 );
219                 if(! count($r)) {
220                         info( t('No messages.') . EOL);
221                         return $o;
222                 }
223
224                 $tpl = get_markup_template('mail_list.tpl');
225                 foreach($r as $rr) {
226                         if($rr['unknown']) {
227                                 $partecipants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
228                         }
229                         elseif (link_compare($rr['from-url'],$myprofile)){
230                                 $partecipants = sprintf( t("You and %s"), $rr['name']);
231                         }
232                         else {
233                                 $partecipants = sprintf( t("%s and You"), $rr['from-name']);
234                         }
235                         
236                         $o .= replace_macros($tpl, array(
237                                 '$id' => $rr['id'],
238                                 '$from_name' => $partecipants,
239                                 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
240                                 '$sparkle' => ' sparkle',
241                                 '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
242                                 '$subject' => template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>')),
243                                 '$delete' => t('Delete conversation'),
244                                 '$body' => template_escape($rr['body']),
245                                 '$to_name' => template_escape($rr['name']),
246                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
247                                 '$seen' => $rr['mailseen'],
248                                 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),
249                         ));
250                 }
251                 $o .= paginate($a);     
252                 return $o;
253         }
254
255         if(($a->argc > 1) && (intval($a->argv[1]))) {
256
257                 $o .= $header;
258
259                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
260                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
261                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
262                         intval(local_user()),
263                         intval($a->argv[1])
264                 );
265                 if(count($r)) { 
266                         $contact_id = $r[0]['contact-id'];
267                         $convid = $r[0]['convid'];
268
269                         $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
270                         if($convid)
271                                 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
272                                         dbesc($r[0]['parent-uri']),
273                                         intval($convid)
274                                 );  
275
276                         $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
277                                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
278                                 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
279                                 intval(local_user())
280                         );
281                 }
282                 if(! count($messages)) {
283                         notice( t('Message not available.') . EOL );
284                         return $o;
285                 }
286
287                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
288                         dbesc($r[0]['parent-uri']),
289                         intval(local_user())
290                 );
291
292                 require_once("include/bbcode.php");
293
294                 $tpl = get_markup_template('msg-header.tpl');
295         
296                 $a->page['htmlhead'] .= replace_macros($tpl, array(
297                         '$nickname' => $a->user['nickname'],
298                         '$baseurl' => $a->get_baseurl(true)
299                 ));
300
301
302                 $mails = array();
303                 $seen = 0;
304                 $unknown = false;
305
306                 foreach($messages as $message) {
307                         if($message['unknown'])
308                                 $unknown = true;
309                         if($message['from-url'] == $myprofile) {
310                                 $from_url = $myprofile;
311                                 $sparkle = '';
312                         }
313                         else {
314                                 $from_url = $a->get_baseurl(true) . '/redir/' . $message['contact-id'];
315                                 $sparkle = ' sparkle';
316                         }
317                         $mails[] = array(
318                                 'id' => $message['id'],
319                                 'from_name' => template_escape($message['from-name']),
320                                 'from_url' => $from_url,
321                                 'sparkle' => $sparkle,
322                                 'from_photo' => $message['from-photo'],
323                                 'subject' => template_escape($message['title']),
324                                 'body' => template_escape(smilies(bbcode($message['body']))),
325                                 'delete' => t('Delete message'),
326                                 'to_name' => template_escape($message['name']),
327                                 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
328                         );
329                                 
330                         $seen = $message['seen'];
331                 }
332                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
333                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
334                         
335
336                 $tpl = get_markup_template('mail_display.tpl');
337                 $o = replace_macros($tpl, array(
338                         '$thread_id' => $a->argv[1],
339                         '$thread_subject' => $message['title'],
340                         '$thread_seen' => $seen,
341                         '$delete' =>  t('Delete conversation'),
342                         '$canreply' => (($unknown) ? false : '1'),
343                         '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),                        
344                         '$mails' => $mails,
345                         
346                         // reply
347                         '$header' => t('Send Reply'),
348                         '$to' => t('To:'),
349                         '$subject' => t('Subject:'),
350                         '$subjtxt' => template_escape($message['title']),
351                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
352                         '$yourmessage' => t('Your message:'),
353                         '$text' => '',
354                         '$select' => $select,
355                         '$parent' => $parent,
356                         '$upload' => t('Upload photo'),
357                         '$insert' => t('Insert web link'),
358                         '$wait' => t('Please wait')
359
360                 ));
361
362                 return $o;
363         }
364
365 }