]> git.mxchange.org Git - friendica.git/blob - mod/item.php
messages need subjects to link to
[friendica.git] / mod / item.php
1 <?php
2
3 // This is the POST destination for most all locally posted
4 // text stuff. This function handles status, wall-to-wall status, 
5 // local comments, and remote coments - that are posted on this site 
6 // (as opposed to being delivered in a feed).
7 // All of these become an "item" which is our basic unit of 
8 // information. 
9
10 function item_post(&$a) {
11
12         if((! local_user()) && (! remote_user()))
13                 return;
14
15         require_once('include/security.php');
16
17         $uid = $_SESSION['uid'];
18
19
20         $parent = ((x($_POST,'parent')) ? intval($_POST['parent']) : 0);
21
22         $parent_item = null;
23
24         if($parent) {
25                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
26                         intval($parent)
27                 );
28                 if(! count($r)) {
29                         notice( t('Unable to locate original post.') . EOL);
30                         goaway($a->get_baseurl() . "/" . $_POST['return'] );
31                 }
32                 $parent_item = $r[0];
33         }
34
35         $profile_uid = ((x($_POST,'profile_uid')) ? intval($_POST['profile_uid']) : 0);
36
37
38         if(! can_write_wall($a,$profile_uid)) {
39                 notice( t('Permission denied.') . EOL) ;
40                 return;
41         }
42
43         $user = null;
44
45         $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
46                 intval($profile_uid)
47         );
48         if(count($r))
49                 $user = $r[0];
50         
51
52         $str_group_allow   = perms2str($_POST['group_allow']);
53         $str_contact_allow = perms2str($_POST['contact_allow']);
54         $str_group_deny    = perms2str($_POST['group_deny']);
55         $str_contact_deny  = perms2str($_POST['contact_deny']);
56
57         $title             = notags(trim($_POST['title']));
58         $body              = escape_tags(trim($_POST['body']));
59         $location          = notags(trim($_POST['location']));
60         $coord             = notags(trim($_POST['coord']));
61         $verb              = notags(trim($_POST['verb']));
62
63         if(! strlen($body)) {
64                 notice( t('Empty post discarded.') . EOL );
65                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
66
67         }
68
69         // get contact info for poster
70
71         $author = null;
72
73         if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
74                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
75                         intval($_SESSION['uid'])
76                 );
77         }
78         else {
79                 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
80                         $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
81                                 intval($_SESSION['visitor_id'])
82                         );
83                 }
84         }
85
86         if(count($r)) {
87                 $author = $r[0];
88                 $contact_id = $author['id'];
89         }
90
91         // get contact info for owner
92         
93         if($profile_uid == $_SESSION['uid']) {
94                 $contact_record = $author;
95         }
96         else {
97                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
98                         intval($profile_uid)
99                 );
100                 if(count($r))
101                         $contact_record = $r[0];
102         }
103
104         $post_type = notags(trim($_POST['type']));
105
106         if($post_type === 'net-comment') {
107                 if($parent_item !== null) {
108                         if($parent_item['type'] === 'remote') {
109                                 $post_type = 'remote-comment';
110                         } 
111                         else {          
112                                 $post_type = 'wall-comment';
113                         }
114                 }
115         }
116
117         $str_tags = '';
118         $tagged = array();
119
120         $tags = get_tags($body);
121
122
123         if($tags) {
124                 foreach($tags as $tag) {
125                         if(strpos($tag,'@') === 0) {
126                                 $name = substr($tag,1);
127                                 if(strpos($name,'@')) {
128
129                                         $links = @webfinger($name);
130                                         if(count($links)) {
131                                                 foreach($links as $link) {
132                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
133                                         $profile = $link['@attributes']['href'];
134                                                         if($link['@attributes']['rel'] === 'salmon')
135                                         $salmon = $link['@attributes']['href'];
136                                                 }
137                                         }
138                                 }
139                                 else {
140                                         $newname = $name;
141                                         if(strstr($name,'_')) {
142                                                 $newname = str_replace('_',' ',$name);
143                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
144                                                         dbesc($newname),
145                                                         intval($profile_uid)
146                                                 );
147                                         }
148                                         else {
149                                                 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
150                                                         dbesc($name),
151                                                         intval($profile_uid)
152                                                 );
153                                         }
154                                         if(count($r)) {
155                                                 $profile = $r[0]['url'];
156                                                 $salmon  = $r[0]['notify'];
157                                         }
158                                 }
159                                 if($profile) {
160                                         $body = str_replace($name,'[url=' . $profile . ']' . $newname   . '[/url]', $body);
161                                         if(strlen($str_tags))
162                                                 $str_tags .= ',';
163                                         $profile = str_replace(',','%2c',$profile);
164                                         $str_tags .= '[url=' . $profile . ']' . $newname        . '[/url]';
165                                 }
166                         }
167                 }
168         }
169
170         $wall = 0;
171         if($post_type === 'wall' || $post_type === 'wall-comment')
172                 $wall = 1;
173
174         if(! strlen($verb))
175                 $verb = ACTIVITY_POST ;
176
177         $gravity = (($parent) ? 6 : 0 );
178  
179         $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
180
181         $uri = item_new_uri($a->get_hostname(),$profile_uid);
182
183         $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`, 
184                 `author-name`, `author-link`, `author-avatar`, `created`,
185                 `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
186                 VALUES( %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
187                 intval($profile_uid),
188                 dbesc($post_type),
189                 intval($wall),
190                 intval($gravity),
191                 intval($contact_id),
192                 dbesc($contact_record['name']),
193                 dbesc($contact_record['url']),
194                 dbesc($contact_record['thumb']),
195                 dbesc($author['name']),
196                 dbesc($author['url']),
197                 dbesc($author['thumb']),
198                 dbesc(datetime_convert()),
199                 dbesc(datetime_convert()),
200                 dbesc(datetime_convert()),
201                 dbesc($uri),
202                 dbesc($title),
203                 dbesc($body),
204                 dbesc($location),
205                 dbesc($coord),
206                 dbesc($verb),
207                 dbesc($str_contact_allow),
208                 dbesc($str_group_allow),
209                 dbesc($str_contact_deny),
210                 dbesc($str_group_deny)
211         );
212         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
213                 dbesc($uri));
214         if(count($r)) {
215                 $post_id = $r[0]['id'];
216
217                 if($parent) {
218
219                         // This item is the last leaf and gets the comment box, clear any ancestors
220                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
221                                 dbesc(datetime_convert()),
222                                 intval($parent)
223                         );
224
225                         // Inherit ACL's from the parent item.
226                         // TODO merge with subsequent UPDATE operation and save a db write 
227
228                         $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
229                                 WHERE `id` = %d LIMIT 1",
230                                 dbesc($parent_item['allow_cid']),
231                                 dbesc($parent_item['allow_gid']),
232                                 dbesc($parent_item['deny_cid']),
233                                 dbesc($parent_item['deny_gid']),
234                                 intval($post_id)
235                         );
236
237                         // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
238                         if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
239                                 require_once('bbcode.php');
240                                 $from = $author['name'];
241                                 $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
242                                 $email_tpl = replace_macros($tpl, array(
243                                         '$sitename' => $a->config['sitename'],
244                                         '$siteurl' =>  $a->get_baseurl(),
245                                         '$username' => $user['username'],
246                                         '$email' => $user['email'],
247                                         '$from' => $from,
248                                         '$body' => strip_tags(bbcode($body))
249                                 ));
250
251                                 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
252                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
253                         }
254                 }
255                 else {
256                         $parent = $post_id;
257
258                         // let me know if somebody did a wall-to-wall post on my profile
259
260                         if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
261                                 require_once('bbcode.php');
262                                 $from = $author['name'];
263                                 $tpl = load_view_file('view/wall_received_eml.tpl');                    
264                                 $email_tpl = replace_macros($tpl, array(
265                                         '$sitename' => $a->config['sitename'],
266                                         '$siteurl' =>  $a->get_baseurl(),
267                                         '$username' => $user['username'],
268                                         '$email' => $user['email'],
269                                         '$from' => $from,
270                                         '$body' => strip_tags(bbcode($body))
271                                 ));
272
273                                 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
274                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
275                         }
276                 }
277
278                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
279                         WHERE `id` = %d LIMIT 1",
280                         intval($parent),
281                         dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
282                         dbesc(datetime_convert()),
283                         intval($post_id)
284                 );
285
286                 // photo comments turn the corresponding item visible to the profile wall
287                 // This way we don't see every picture in your new photo album posted to your wall at once.
288                 // They will show up as people comment on them.
289
290                 if(! $parent_item['visible']) {
291                         $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
292                                 intval($parent_item['id'])
293                         );
294                 }
295         }
296
297         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
298         $proc_debug = get_config('system','proc_debug');
299
300         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" $proc_debug &",
301                 array(),$foo));
302
303         goaway($a->get_baseurl() . "/" . $_POST['return'] );
304         return; // NOTREACHED
305 }
306
307 function item_content(&$a) {
308
309         if((! local_user()) && (! remote_user()))
310                 return;
311
312         require_once('include/security.php');
313
314         $uid = $_SESSION['uid'];
315
316         if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
317
318                 // locate item to be deleted
319
320                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
321                         intval($a->argv[2])
322                 );
323
324                 if(! count($r)) {
325                         notice( t('Item not found.') . EOL);
326                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
327                 }
328                 $item = $r[0];
329
330                 // check if logged in user is either the author or owner of this item
331
332                 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
333
334                         // delete the item
335
336                         $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
337                                 dbesc(datetime_convert()),
338                                 dbesc(datetime_convert()),
339                                 intval($item['id'])
340                         );
341
342                         // If item is a link to a photo resource, nuke all the associated photos 
343                         // (visitors will not have photo resources)
344                         // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
345                         // generate a resource-id and therefore aren't intimately linked to the item. 
346
347                         if(strlen($item['resource-id'])) {
348                                 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
349                                         dbesc($item['resource-id']),
350                                         intval($item['uid'])
351                                 );
352                                 // ignore the result
353                         }
354
355                         // If it's the parent of a comment thread, kill all the kids
356
357                         if($item['uri'] == $item['parent-uri']) {
358                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' 
359                                         WHERE `parent-uri` = '%s' AND `uid` = %d ",
360                                         dbesc(datetime_convert()),
361                                         dbesc(datetime_convert()),
362                                         dbesc($item['parent-uri']),
363                                         intval($item['uid'])
364                                 );
365                                 // ignore the result
366                         }
367                         else {
368                                 // ensure that last-child is set in case the comment that had it just got wiped.
369                                 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
370                                         dbesc(datetime_convert()),
371                                         dbesc($item['parent-uri']),
372                                         intval($item['uid'])
373                                 );
374                                 // who is the last child now? 
375                                 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d ORDER BY `edited` DESC LIMIT 1",
376                                         dbesc($item['parent-uri']),
377                                         intval($item['uid'])
378                                 );
379                                 if(count($r)) {
380                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
381                                                 intval($r[0]['id'])
382                                         );
383                                 }       
384                         }
385                         $drop_id = intval($item['id']);
386                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
387                         $proc_debug = get_config('system','proc_debug');
388
389                         
390                         // send the notification upstream/downstream as the case may be
391
392                         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" $proc_debug &",
393                                 array(), $foo));
394
395                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
396                         return; //NOTREACHED
397                 }
398                 else {
399                         notice( t('Permission denied.') . EOL);
400                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
401                         //NOTREACHED
402                 }
403         }
404 }