]> git.mxchange.org Git - friendica.git/blob - mod/message.php
eb18bac8989c1a6b615f97341e9ee8473565b360
[friendica.git] / mod / message.php
1 <?php
2
3 require_once('view/acl_selectors.php');
4
5 function message_init(&$a) {
6
7
8 }
9
10 function message_post(&$a) {
11
12         if(! local_user()) {
13                 notice( t('Permission denied.') . EOL);
14                 return;
15         }
16
17         $replyto = notags(trim($_POST['replyto']));
18         $recipient = intval($_POST['messageto']);
19         $subject = notags(trim($_POST['subject']));
20         $body = escape_tags(trim($_POST['body']));
21
22         if(! $recipient) {
23                 notice( t('No recipient selected.') . EOL );
24                 return;
25         }
26
27         $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
28                 intval($_SESSION['uid'])
29         );
30         $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
31                         intval($recipient),
32                         intval($_SESSION['uid'])
33         );
34
35         if(! (count($me) && (count($contact)))) {
36                 notice( t('Unable to locate contact information.') . EOL );
37                 return;
38         }
39
40         $hash = random_string();
41         $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . $_SESSION['uid'] . ':' . $hash ;
42
43         if(! strlen($replyto))
44                 $replyto = $uri;
45
46         $r = q("INSERT INTO `mail` ( `uid`, `from-name`, `from-photo`, `from-url`, 
47                 `contact-id`, `title`, `body`, `delivered`, `seen`, `replied`, `uri`, `parent-uri`, `created`)
48                 VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s' )",
49                 intval($_SESSION['uid']),
50                 dbesc($me[0]['name']),
51                 dbesc($me[0]['thumb']),
52                 dbesc($me[0]['url']),
53                 intval($recipient),
54                 dbesc($subject),
55                 dbesc($body),
56                 0,
57                 0,
58                 0,
59                 dbesc($uri),
60                 dbesc($replyto),
61                 datetime_convert()
62         );
63         $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' and `uid` = %d LIMIT 1",
64                 dbesc($uri),
65                 intval($_SESSION['uid'])
66         );
67         if(count($r))
68                 $post_id = $r[0]['id'];
69
70         $url = $a->get_baseurl();
71
72         if($post_id) {
73                 proc_close(proc_open("php include/notifier.php \"$url\" \"mail\" \"$post_id\" > mail.log &",
74                         array(),$foo));
75                 notice( t('Message sent.') . EOL );
76         }
77         else {
78                 notice( t('Message could not be sent.') . EOL );
79         }
80         return;
81
82 }
83
84 function message_content(&$a) {
85
86         if(! local_user()) {
87                 notice( t('Permission denied.') . EOL);
88                 return;
89         }
90
91         $myprofile = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
92
93         if(($a->argc > 1) && ($a->argv[1] == 'new')) {
94                 
95                 $tpl = file_get_contents('view/jot-header.tpl');
96         
97                 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
98
99                 $select .= contact_select('messageto','message-to-select', false, 4, true);
100                 $tpl = file_get_contents('view/prv_message.tpl');
101                 $o = replace_macros($tpl,array(
102                         '$header' => t('Send Private Message'),
103                         '$to' => t('To:'),
104                         '$subject' => t('Subject:'),
105                         '$yourmessage' => t('Your message:'),
106                         '$select' => $select,
107                         '$upload' => t('Upload photo'),
108                         '$insert' => t('Insert web link'),
109                         '$wait' => t('Please wait')
110
111                 ));
112
113                 return $o;
114         }
115
116         if($a->argc == 1) {
117
118                 $r = q("SELECT count(*) AS `total` FROM `mail` 
119                         WHERE `mail`.`uid` = %d AND `from-url` != '%s' ",
120                         intval($_SESSION['uid']),
121                         dbesc($myprofile)
122                 );
123                 if(count($r))
124                         $a->set_pager_total($r[0]['total']);
125         
126                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
127                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
128                         WHERE `mail`.`uid` = %d AND `from-url` != '%s' LIMIT %d , %d ",
129                         intval($_SESSION['uid']),
130                         dbesc($myprofile),
131                         intval($a->pager['start']),
132                         intval($a->pager['itemspage'])
133                 );
134                 if(! count($r)) {
135                         notice( t('No messages.') . EOL);
136                         return;
137                 }
138
139                 $tpl = file_get_contents('view/mail_list.tpl');
140                 foreach($r as $rr) {
141                         $o .= replace_macros($tpl, array(
142                                 '$id' => $rr['id'],
143                                 '$from_name' =>$rr['from-name'],
144                                 '$from_url' => $a->get_baseurl() . '/redir/' . $rr['contact-id'],
145                                 '$from_photo' => $rr['from-photo'],
146                                 '$subject' => (($rr['seen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'),
147                                 '$to_name' => $rr['name'],
148                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['created'],'D, d M Y - g:i A')
149                         ));
150                 }
151                 $o .= paginate($a);     
152                 return $o;
153         }
154
155 }