]> git.mxchange.org Git - friendica.git/blob - include/message.php
add remove_user hook (it looks like dreamhost changed all my file permissions, this...
[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
9         $a = get_app();
10
11         if(! $recipient) return -1;
12         
13         if(! strlen($subject))
14                 $subject = t('[no subject]');
15
16         $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
17                 intval(local_user())
18         );
19         $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
20                         intval($recipient),
21                         intval(local_user())
22         );
23
24         if(! (count($me) && (count($contact)))) {
25                 return -2;
26         }
27
28         $hash = random_string();
29         $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . local_user() . ':' . $hash ;
30
31         $convid = 0;
32         $reply = false;
33
34         // look for any existing conversation structure
35
36         if(strlen($replyto)) {
37                 $reply = true;
38                 $r = q("select convid from mail where uid = %d and ( uri = '%s' or `parent-uri` = '%s' ) limit 1",
39                         intval(local_user()),
40                         dbesc($replyto),
41                         dbesc($replyto)
42                 );
43                 if(count($r))
44                         $convid = $r[0]['convid'];
45         }               
46
47         if(! $convid) {
48
49                 // create a new conversation
50
51                 $conv_guid = get_guid();
52
53                 $recip_host = substr($contact[0]['url'],strpos($contact[0]['url'],'://')+3);
54                 $recip_host = substr($recip_host,0,strpos($recip_host,'/'));
55
56                 $recip_handle = (($contact[0]['addr']) ? $contact[0]['addr'] : $contact[0]['nick'] . '@' . $recip_host);
57                 $sender_handle = $a->user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
58
59                 $handles = $recip_handle . ';' . $sender_handle;
60
61                 $r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
62                         intval(local_user()),
63                         dbesc($conv_guid),
64                         dbesc($sender_handle),
65                         dbesc(datetime_convert()),
66                         dbesc(datetime_convert()),
67                         dbesc($subject),
68                         dbesc($handles)
69                 );
70
71                 $r = q("select * from conv where guid = '%s' and uid = %d limit 1",
72                         dbesc($conv_guid),
73                         intval(local_user())
74                 );
75                 if(count($r))
76                         $convid = $r[0]['id'];
77         }
78
79         if(! $convid) {
80                 logger('send message: conversation not found.');
81                 return -4;
82         }
83
84         if(! strlen($replyto)) {
85                 $replyto = $uri;
86         }
87
88
89         $r = q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`, 
90                 `contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`)
91                 VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s' )",
92                 intval(local_user()),
93                 dbesc(get_guid()),
94                 intval($convid),
95                 dbesc($me[0]['name']),
96                 dbesc($me[0]['thumb']),
97                 dbesc($me[0]['url']),
98                 intval($recipient),
99                 dbesc($subject),
100                 dbesc($body),
101                 1,
102                 intval($reply),
103                 0,
104                 dbesc($uri),
105                 dbesc($replyto),
106                 datetime_convert()
107         );
108
109
110         $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' and `uid` = %d LIMIT 1",
111                 dbesc($uri),
112                 intval(local_user())
113         );
114         if(count($r))
115                 $post_id = $r[0]['id'];
116
117         /**
118          *
119          * When a photo was uploaded into the message using the (profile wall) ajax 
120          * uploader, The permissions are initially set to disallow anybody but the
121          * owner from seeing it. This is because the permissions may not yet have been
122          * set for the post. If it's private, the photo permissions should be set
123          * appropriately. But we didn't know the final permissions on the post until
124          * now. So now we'll look for links of uploaded messages that are in the
125          * post and set them to the same permissions as the post itself.
126          *
127          */
128
129         $match = null;
130
131         if(preg_match_all("/\[img\](.*?)\[\/img\]/",$body,$match)) {
132                 $images = $match[1];
133                 if(count($images)) {
134                         foreach($images as $image) {
135                                 if(! stristr($image,$a->get_baseurl() . '/photo/'))
136                                         continue;
137                                 $image_uri = substr($image,strrpos($image,'/') + 1);
138                                 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
139                                 $r = q("UPDATE `photo` SET `allow_cid` = '%s'
140                                         WHERE `resource-id` = '%s' AND `album` = '%s' AND `uid` = %d ",
141                                         dbesc('<' . $recipient . '>'),
142                                         dbesc($image_uri),
143                                         dbesc( t('Wall Photos')),
144                                         intval(local_user())
145                                 ); 
146                         }
147                 }
148         }
149         
150         if($post_id) {
151                 proc_run('php',"include/notifier.php","mail","$post_id");
152                 return intval($post_id);
153         } else {
154                 return -3;
155         }
156
157 }