]> git.mxchange.org Git - friendica.git/blob - include/message.php
Merge pull request #15 from zzottel/master
[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         $convid = 0;
31
32         // look for any existing conversation structure
33
34         if(strlen($replyto)) {
35                 $r = q("select convid from mail where uid = %d and uri = '%s' limit 1",
36                         intval(local_user()),
37                         dbesc($replyto)
38                 );
39                 if(count($r))
40                         $convid = $r[0]['convid'];
41         }               
42
43         if(! strlen($replyto))
44                 $replyto = $uri;
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         $r = q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`, 
85                 `contact-id`, `title`, `body`, `seen`, `replied`, `uri`, `parent-uri`, `created`)
86                 VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s', '%s', '%s' )",
87                 intval(local_user()),
88                 dbesc(get_guid()),
89                 intval($convid),
90                 dbesc($me[0]['name']),
91                 dbesc($me[0]['thumb']),
92                 dbesc($me[0]['url']),
93                 intval($recipient),
94                 dbesc($subject),
95                 dbesc($body),
96                 1,
97                 0,
98                 dbesc($uri),
99                 dbesc($replyto),
100                 datetime_convert()
101         );
102         $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' and `uid` = %d LIMIT 1",
103                 dbesc($uri),
104                 intval(local_user())
105         );
106         if(count($r))
107                 $post_id = $r[0]['id'];
108
109         /**
110          *
111          * When a photo was uploaded into the message using the (profile wall) ajax 
112          * uploader, The permissions are initially set to disallow anybody but the
113          * owner from seeing it. This is because the permissions may not yet have been
114          * set for the post. If it's private, the photo permissions should be set
115          * appropriately. But we didn't know the final permissions on the post until
116          * now. So now we'll look for links of uploaded messages that are in the
117          * post and set them to the same permissions as the post itself.
118          *
119          */
120
121         $match = null;
122
123         if(preg_match_all("/\[img\](.*?)\[\/img\]/",$body,$match)) {
124                 $images = $match[1];
125                 if(count($images)) {
126                         foreach($images as $image) {
127                                 if(! stristr($image,$a->get_baseurl() . '/photo/'))
128                                         continue;
129                                 $image_uri = substr($image,strrpos($image,'/') + 1);
130                                 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
131                                 $r = q("UPDATE `photo` SET `allow_cid` = '%s'
132                                         WHERE `resource-id` = '%s' AND `album` = '%s' AND `uid` = %d ",
133                                         dbesc('<' . $recipient . '>'),
134                                         dbesc($image_uri),
135                                         dbesc( t('Wall Photos')),
136                                         intval(local_user())
137                                 ); 
138                         }
139                 }
140         }
141         
142         if($post_id) {
143                 proc_run('php',"include/notifier.php","mail","$post_id");
144                 return intval($post_id);
145         } else {
146                 return -3;
147         }
148
149 }