]> git.mxchange.org Git - friendica.git/blob - include/message.php
Improve dba::selectFirst calls
[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                 dba::insert('conv', $fields);
71                 $convid = dba::lastInsertId();
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         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         $post_id = dba::lastInsertId();
103
104         /**
105          *
106          * When a photo was uploaded into the message using the (profile wall) ajax
107          * uploader, The permissions are initially set to disallow anybody but the
108          * owner from seeing it. This is because the permissions may not yet have been
109          * set for the post. If it's private, the photo permissions should be set
110          * appropriately. But we didn't know the final permissions on the post until
111          * now. So now we'll look for links of uploaded messages that are in the
112          * post and set them to the same permissions as the post itself.
113          *
114          */
115         $match = null;
116         if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
117                 $images = $match[1];
118                 if (count($images)) {
119                         foreach ($images as $image) {
120                                 if (!stristr($image, System::baseUrl() . '/photo/')) {
121                                         continue;
122                                 }
123                                 $image_uri = substr($image, strrpos($image, '/') + 1);
124                                 $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
125                                 q("UPDATE `photo` SET `allow_cid` = '%s'
126                                         WHERE `resource-id` = '%s' AND `album` = '%s' AND `uid` = %d ",
127                                         dbesc('<' . $recipient . '>'),
128                                         dbesc($image_uri),
129                                         dbesc( t('Wall Photos')),
130                                         intval(local_user())
131                                 );
132                         }
133                 }
134         }
135
136         if ($post_id) {
137                 Worker::add(PRIORITY_HIGH, "Notifier", "mail", $post_id);
138                 return intval($post_id);
139         } else {
140                 return -3;
141         }
142 }
143
144 function send_wallmessage($recipient = '', $body = '', $subject = '', $replyto = '')
145 {
146         if (!$recipient) {
147                 return -1;
148         }
149
150         if (!strlen($subject)) {
151                 $subject = t('[no subject]');
152         }
153
154         $guid = get_guid(32);
155         $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
156
157         $me = Probe::uri($replyto);
158
159         if (!$me['name']) {
160                 return -2;
161         }
162
163         $conv_guid = get_guid(32);
164
165         $recip_handle = $recipient['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
166
167         $sender_nick = basename($replyto);
168         $sender_host = substr($replyto, strpos($replyto, '://') + 3);
169         $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
170         $sender_handle = $sender_nick . '@' . $sender_host;
171
172         $handles = $recip_handle . ';' . $sender_handle;
173
174         $fields = array('uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
175                 'created' => datetime_convert(), 'updated' => datetime_convert(),
176                 'subject' => $subject, 'recips' => $handles);
177         dba::insert('conv', $fields);
178         $convid = dba::lastInsertId();
179         
180         if (!$convid) {
181                 logger('send message: conversation not found.');
182                 return -4;
183         }
184
185         q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`,
186                 `contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`, `unknown`)
187                 VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s', %d )",
188                 intval($recipient['uid']),
189                 dbesc($guid),
190                 intval($convid),
191                 dbesc($me['name']),
192                 dbesc($me['photo']),
193                 dbesc($me['url']),
194                 0,
195                 dbesc($subject),
196                 dbesc($body),
197                 0,
198                 0,
199                 0,
200                 dbesc($uri),
201                 dbesc($replyto),
202                 datetime_convert(),
203                 1
204         );
205
206         return 0;
207 }