]> git.mxchange.org Git - friendica.git/blob - include/message.php
Class file relocations
[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) return -1;
15
16         if (! strlen($subject))
17                 $subject = t('[no subject]');
18
19         $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
20                 intval(local_user())
21         );
22         $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
23                         intval($recipient),
24                         intval(local_user())
25         );
26
27         if (! (count($me) && (count($contact)))) {
28                 return -2;
29         }
30
31         $guid = get_guid(32);
32         $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
33
34         $convid = 0;
35         $reply = false;
36
37         // look for any existing conversation structure
38
39         if (strlen($replyto)) {
40                 $reply = true;
41                 $r = q("select convid from mail where uid = %d and ( uri = '%s' or `parent-uri` = '%s' ) limit 1",
42                         intval(local_user()),
43                         dbesc($replyto),
44                         dbesc($replyto)
45                 );
46                 if (DBM::is_result($r))
47                         $convid = $r[0]['convid'];
48         }
49
50         if (! $convid) {
51
52                 // create a new conversation
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(System::baseUrl(), strpos(System::baseUrl(),'://') + 3);
59
60                 $conv_guid = get_guid(32);
61                 $convuri = $recip_handle.':'.$conv_guid;
62
63                 $handles = $recip_handle . ';' . $sender_handle;
64
65                 $fields = array('uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
66                                 'created' => datetime_convert(), 'updated' => datetime_convert(),
67                                 'subject' => $subject, 'recips' => $handles);
68                 $r = dba::insert('conv', $fields);
69
70                 $r = dba::select('conv', array('id'), array('guid' => $conv_guid, 'uid' => local_user()), array('limit' => 1));
71                 if (DBM::is_result($r))
72                         $convid = $r['id'];
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
85         $r = 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
105
106         $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
107                 dbesc($uri),
108                 intval(local_user())
109         );
110         if (DBM::is_result($r))
111                 $post_id = $r[0]['id'];
112
113         /**
114          *
115          * When a photo was uploaded into the message using the (profile wall) ajax
116          * uploader, The permissions are initially set to disallow anybody but the
117          * owner from seeing it. This is because the permissions may not yet have been
118          * set for the post. If it's private, the photo permissions should be set
119          * appropriately. But we didn't know the final permissions on the post until
120          * now. So now we'll look for links of uploaded messages that are in the
121          * post and set them to the same permissions as the post itself.
122          *
123          */
124
125         $match = null;
126
127         if (preg_match_all("/\[img\](.*?)\[\/img\]/",$body,$match)) {
128                 $images = $match[1];
129                 if (count($images)) {
130                         foreach ($images as $image) {
131                                 if (! stristr($image,System::baseUrl() . '/photo/')) {
132                                         continue;
133                                 }
134                                 $image_uri = substr($image,strrpos($image,'/') + 1);
135                                 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
136                                 $r = q("UPDATE `photo` SET `allow_cid` = '%s'
137                                         WHERE `resource-id` = '%s' AND `album` = '%s' AND `uid` = %d ",
138                                         dbesc('<' . $recipient . '>'),
139                                         dbesc($image_uri),
140                                         dbesc( t('Wall Photos')),
141                                         intval(local_user())
142                                 );
143                         }
144                 }
145         }
146
147         if ($post_id) {
148                 Worker::add(PRIORITY_HIGH, "notifier", "mail", $post_id);
149                 return intval($post_id);
150         } else {
151                 return -3;
152         }
153
154 }
155
156 function send_wallmessage($recipient='', $body='', $subject='', $replyto=''){
157
158         if (! $recipient) {
159                 return -1;
160         }
161
162         if (! strlen($subject)) {
163                 $subject = t('[no subject]');
164         }
165
166         $guid = get_guid(32);
167         $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
168
169         $convid = 0;
170         $reply = false;
171
172         $me = Probe::uri($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 }