]> git.mxchange.org Git - friendica.git/blob - include/message.php
Merge https://github.com/friendica/friendica into pull
[friendica.git] / include / message.php
1 <?php
2
3         // send a private message
4         
5
6
7
8 function send_message($recipient=0, $body='', $subject='', $replyto=''){ 
9
10         $a = get_app();
11
12         if(! $recipient) return -1;
13         
14         if(! strlen($subject))
15                 $subject = t('[no subject]');
16
17         $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
18                 intval(local_user())
19         );
20         $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
21                         intval($recipient),
22                         intval(local_user())
23         );
24
25         if(! (count($me) && (count($contact)))) {
26                 return -2;
27         }
28
29         $hash = random_string();
30         $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . local_user() . ':' . $hash ;
31
32         $convid = 0;
33         $reply = false;
34
35         // look for any existing conversation structure
36
37         if(strlen($replyto)) {
38                 $reply = true;
39                 $r = q("select convid from mail where uid = %d and ( uri = '%s' or `parent-uri` = '%s' ) limit 1",
40                         intval(local_user()),
41                         dbesc($replyto),
42                         dbesc($replyto)
43                 );
44                 if(count($r))
45                         $convid = $r[0]['convid'];
46         }               
47
48         if(! $convid) {
49
50                 // create a new conversation
51
52                 $conv_guid = get_guid();
53
54                 $recip_host = substr($contact[0]['url'],strpos($contact[0]['url'],'://')+3);
55                 $recip_host = substr($recip_host,0,strpos($recip_host,'/'));
56
57                 $recip_handle = (($contact[0]['addr']) ? $contact[0]['addr'] : $contact[0]['nick'] . '@' . $recip_host);
58                 $sender_handle = $a->user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
59
60                 $handles = $recip_handle . ';' . $sender_handle;
61
62                 $r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
63                         intval(local_user()),
64                         dbesc($conv_guid),
65                         dbesc($sender_handle),
66                         dbesc(datetime_convert()),
67                         dbesc(datetime_convert()),
68                         dbesc($subject),
69                         dbesc($handles)
70                 );
71
72                 $r = q("select * from conv where guid = '%s' and uid = %d limit 1",
73                         dbesc($conv_guid),
74                         intval(local_user())
75                 );
76                 if(count($r))
77                         $convid = $r[0]['id'];
78         }
79
80         if(! $convid) {
81                 logger('send message: conversation not found.');
82                 return -4;
83         }
84
85         if(! strlen($replyto)) {
86                 $replyto = $uri;
87         }
88
89
90         $r = q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`, 
91                 `contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`)
92                 VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s' )",
93                 intval(local_user()),
94                 dbesc(get_guid()),
95                 intval($convid),
96                 dbesc($me[0]['name']),
97                 dbesc($me[0]['thumb']),
98                 dbesc($me[0]['url']),
99                 intval($recipient),
100                 dbesc($subject),
101                 dbesc($body),
102                 1,
103                 intval($reply),
104                 0,
105                 dbesc($uri),
106                 dbesc($replyto),
107                 datetime_convert()
108         );
109
110
111         $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' and `uid` = %d LIMIT 1",
112                 dbesc($uri),
113                 intval(local_user())
114         );
115         if(count($r))
116                 $post_id = $r[0]['id'];
117
118         /**
119          *
120          * When a photo was uploaded into the message using the (profile wall) ajax 
121          * uploader, The permissions are initially set to disallow anybody but the
122          * owner from seeing it. This is because the permissions may not yet have been
123          * set for the post. If it's private, the photo permissions should be set
124          * appropriately. But we didn't know the final permissions on the post until
125          * now. So now we'll look for links of uploaded messages that are in the
126          * post and set them to the same permissions as the post itself.
127          *
128          */
129
130         $match = null;
131
132         if(preg_match_all("/\[img\](.*?)\[\/img\]/",$body,$match)) {
133                 $images = $match[1];
134                 if(count($images)) {
135                         foreach($images as $image) {
136                                 if(! stristr($image,$a->get_baseurl() . '/photo/'))
137                                         continue;
138                                 $image_uri = substr($image,strrpos($image,'/') + 1);
139                                 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
140                                 $r = q("UPDATE `photo` SET `allow_cid` = '%s'
141                                         WHERE `resource-id` = '%s' AND `album` = '%s' AND `uid` = %d ",
142                                         dbesc('<' . $recipient . '>'),
143                                         dbesc($image_uri),
144                                         dbesc( t('Wall Photos')),
145                                         intval(local_user())
146                                 ); 
147                         }
148                 }
149         }
150         
151         if($post_id) {
152                 proc_run('php',"include/notifier.php","mail","$post_id");
153                 return intval($post_id);
154         } else {
155                 return -3;
156         }
157
158 }
159
160
161
162
163
164 function send_wallmessage($recipient='', $body='', $subject='', $replyto=''){ 
165
166         $a = get_app();
167
168
169         if(! $recipient) return -1;
170         
171         if(! strlen($subject))
172                 $subject = t('[no subject]');
173
174         $hash = random_string();
175         $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . local_user() . ':' . $hash ;
176
177         $convid = 0;
178         $reply = false;
179
180         require_once('include/Scrape.php');
181
182         $me = probe_url($replyto);
183
184         if(! $me['name'])
185                 return -2;
186
187         $conv_guid = get_guid();
188
189         $recip_handle = $recipient['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
190
191         $sender_nick = basename($replyto);
192         $sender_host = substr($replyto,strpos($replyto,'://')+3);
193         $sender_host = substr($sender_host,0,strpos($sender_host,'/'));
194         $sender_handle = $sender_nick . '@' . $sender_host;
195
196         $handles = $recip_handle . ';' . $sender_handle;
197
198         $r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
199                 intval($recipient['uid']),
200                 dbesc($conv_guid),
201                 dbesc($sender_handle),
202                 dbesc(datetime_convert()),
203                 dbesc(datetime_convert()),
204                 dbesc($subject),
205                 dbesc($handles)
206         );
207
208         $r = q("select * from conv where guid = '%s' and uid = %d limit 1",
209                 dbesc($conv_guid),
210                 intval($recipient['uid'])
211         );
212         if(count($r))
213                 $convid = $r[0]['id'];
214
215         if(! $convid) {
216                 logger('send message: conversation not found.');
217                 return -4;
218         }
219
220         $r = q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`, 
221                 `contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`, `unknown`)
222                 VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s', %d )",
223                 intval($recipient['uid']),
224                 dbesc(get_guid()),
225                 intval($convid),
226                 dbesc($me['name']),
227                 dbesc($me['photo']),
228                 dbesc($me['url']),
229                 0,
230                 dbesc($subject),
231                 dbesc($body),
232                 0,
233                 0,
234                 0,
235                 dbesc($uri),
236                 dbesc($replyto),
237                 datetime_convert(),
238                 1
239         );
240
241         return 0;
242
243 }