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