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