]> git.mxchange.org Git - friendica.git/blob - include/message.php
add conv structure
[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                 $conv_guid = get_guid();
34
35                 $recip_host = substr($contact[0]['url'],strpos($contact[0]['url'],'://')+3);
36                 $recip_host = substr($recip_host,0,strpos($recip_host,'/'));
37
38                 $recip_handle = (($contact[0]['addr']) ? $contact[0]['addr'] : $contact[0]['nick'] . '@' . $recip_host);
39                 $sender_handle = $a->user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
40
41                 $handles = $recip_handle . ';' . $sender_handle;
42
43                 $r = q("insert into conv (uid,guid,recips) values (%d, '%s', '%s') ",
44                         intval(local_user()),
45                         dbesc($conv_guid),
46                         dbesc($handles)
47                 );
48                 $r = q("select * from conv where guid = '%s' and uid = %d limit 1",
49                         dbesc($conv_guid),
50                         intval(local_user())
51                 );
52                 if(count($r))
53                         $convid = $r[0]['id'];
54         }
55         else {
56                 $r = q("select convid from mail where uid = %d and uri = '%s' limit 1",
57                         intval(local_user()),
58                         dbesc($replyto)
59                 );
60                 if(count($r))
61                         $convid = $r[0]['convid'];
62         }               
63
64         if(! $convid) {
65                 logger('send message: conversation not found.');
66                 return -4;
67         }
68
69         $r = q("INSERT INTO `mail` ( `uid`, `convid`, `from-name`, `from-photo`, `from-url`, 
70                 `contact-id`, `title`, `body`, `seen`, `replied`, `uri`, `parent-uri`, `created`)
71                 VALUES ( %d, %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s', '%s', '%s' )",
72                 intval(local_user()),
73                 intval($convid),
74                 dbesc($me[0]['name']),
75                 dbesc($me[0]['thumb']),
76                 dbesc($me[0]['url']),
77                 intval($recipient),
78                 dbesc($subject),
79                 dbesc($body),
80                 1,
81                 0,
82                 dbesc($uri),
83                 dbesc($replyto),
84                 datetime_convert()
85         );
86         $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' and `uid` = %d LIMIT 1",
87                 dbesc($uri),
88                 intval(local_user())
89         );
90         if(count($r))
91                 $post_id = $r[0]['id'];
92
93         /**
94          *
95          * When a photo was uploaded into the message using the (profile wall) ajax 
96          * uploader, The permissions are initially set to disallow anybody but the
97          * owner from seeing it. This is because the permissions may not yet have been
98          * set for the post. If it's private, the photo permissions should be set
99          * appropriately. But we didn't know the final permissions on the post until
100          * now. So now we'll look for links of uploaded messages that are in the
101          * post and set them to the same permissions as the post itself.
102          *
103          */
104
105         $match = null;
106
107         if(preg_match_all("/\[img\](.*?)\[\/img\]/",$body,$match)) {
108                 $images = $match[1];
109                 if(count($images)) {
110                         foreach($images as $image) {
111                                 if(! stristr($image,$a->get_baseurl() . '/photo/'))
112                                         continue;
113                                 $image_uri = substr($image,strrpos($image,'/') + 1);
114                                 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
115                                 $r = q("UPDATE `photo` SET `allow_cid` = '%s'
116                                         WHERE `resource-id` = '%s' AND `album` = '%s' AND `uid` = %d ",
117                                         dbesc('<' . $recipient . '>'),
118                                         dbesc($image_uri),
119                                         dbesc( t('Wall Photos')),
120                                         intval(local_user())
121                                 ); 
122                         }
123                 }
124         }
125         
126         if($post_id) {
127                 proc_run('php',"include/notifier.php","mail","$post_id");
128                 return intval($post_id);
129         } else {
130                 return -3;
131         }
132
133 }