]> git.mxchange.org Git - friendica.git/blob - include/message.php
cca913b4ef67bc879171a1ede6eea1b6034fa557
[friendica.git] / include / message.php
1 <?php
2         // send a private message
3         
4
5
6
7 function send_message($recipient=0, $body='', $subject='', $replyto=''){ 
8         $a = get_app();
9
10         if(! $recipient) return -1;
11         
12         if(! strlen($subject))
13                 $subject = t('[no subject]');
14
15         $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
16                 intval(local_user())
17         );
18         $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
19                         intval($recipient),
20                         intval(local_user())
21         );
22
23         if(! (count($me) && (count($contact)))) {
24                 return -2;
25         }
26
27         $hash = random_string();
28         $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . local_user() . ':' . $hash ;
29
30         if(! strlen($replyto))
31                 $replyto = $uri;
32
33         $r = q("INSERT INTO `mail` ( `uid`, `from-name`, `from-photo`, `from-url`, 
34                 `contact-id`, `title`, `body`, `seen`, `replied`, `uri`, `parent-uri`, `created`)
35                 VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s', '%s', '%s' )",
36                 intval(local_user()),
37                 dbesc($me[0]['name']),
38                 dbesc($me[0]['thumb']),
39                 dbesc($me[0]['url']),
40                 intval($recipient),
41                 dbesc($subject),
42                 dbesc($body),
43                 1,
44                 0,
45                 dbesc($uri),
46                 dbesc($replyto),
47                 datetime_convert()
48         );
49         $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' and `uid` = %d LIMIT 1",
50                 dbesc($uri),
51                 intval(local_user())
52         );
53         if(count($r))
54                 $post_id = $r[0]['id'];
55
56         /**
57          *
58          * When a photo was uploaded into the message using the (profile wall) ajax 
59          * uploader, The permissions are initially set to disallow anybody but the
60          * owner from seeing it. This is because the permissions may not yet have been
61          * set for the post. If it's private, the photo permissions should be set
62          * appropriately. But we didn't know the final permissions on the post until
63          * now. So now we'll look for links of uploaded messages that are in the
64          * post and set them to the same permissions as the post itself.
65          *
66          */
67
68         $match = null;
69
70         if(preg_match_all("/\[img\](.*?)\[\/img\]/",$body,$match)) {
71                 $images = $match[1];
72                 if(count($images)) {
73                         foreach($images as $image) {
74                                 if(! stristr($image,$a->get_baseurl() . '/photo/'))
75                                         continue;
76                                 $image_uri = substr($image,strrpos($image,'/') + 1);
77                                 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
78                                 $r = q("UPDATE `photo` SET `allow_cid` = '%s'
79                                         WHERE `resource-id` = '%s' AND `album` = '%s' AND `uid` = %d ",
80                                         dbesc('<' . $recipient . '>'),
81                                         dbesc($image_uri),
82                                         dbesc( t('Wall Photos')),
83                                         intval(local_user())
84                                 ); 
85                         }
86                 }
87         }
88         
89         if($post_id) {
90                 proc_run('php',"include/notifier.php","mail","$post_id");
91                 return intval($post_id);
92         } else {
93                 return -3;
94         }
95
96 }