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