]> git.mxchange.org Git - friendica.git/blob - mod/item.php
missing pagination in photo collections
[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 = local_user();
18
19         $parent = ((x($_POST,'parent')) ? intval($_POST['parent']) : 0);
20
21         $parent_item = null;
22
23         if($parent) {
24                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
25                         intval($parent)
26                 );
27                 if(! count($r)) {
28                         notice( t('Unable to locate original post.') . EOL);
29                         goaway($a->get_baseurl() . "/" . $_POST['return'] );
30                 }
31                 $parent_item = $r[0];
32         }
33
34         $profile_uid = ((x($_POST,'profile_uid')) ? intval($_POST['profile_uid']) : 0);
35
36
37         if(! can_write_wall($a,$profile_uid)) {
38                 notice( t('Permission denied.') . EOL) ;
39                 return;
40         }
41
42         $user = null;
43
44         $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
45                 intval($profile_uid)
46         );
47         if(count($r))
48                 $user = $r[0];
49         
50
51         $str_group_allow   = perms2str($_POST['group_allow']);
52         $str_contact_allow = perms2str($_POST['contact_allow']);
53         $str_group_deny    = perms2str($_POST['group_deny']);
54         $str_contact_deny  = perms2str($_POST['contact_deny']);
55
56         $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);
57
58         if(($parent_item) && 
59                 (($parent_item['private']) 
60                         || strlen($parent_item['allow_cid']) 
61                         || strlen($parent_item['allow_gid']) 
62                         || strlen($parent_item['deny_cid']) 
63                         || strlen($parent_item['deny_gid'])
64                 )
65         ) {
66                 $private = 1;
67         }
68
69         $title             = notags(trim($_POST['title']));
70         $body              = escape_tags(trim($_POST['body']));
71         $location          = notags(trim($_POST['location']));
72         $coord             = notags(trim($_POST['coord']));
73         $verb              = notags(trim($_POST['verb']));
74
75         if(! strlen($body)) {
76                 notice( t('Empty post discarded.') . EOL );
77                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
78
79         }
80
81         // get contact info for poster
82
83         $author = null;
84         $self   = false;
85
86         if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
87                 $self = true;
88                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
89                         intval($_SESSION['uid'])
90                 );
91         }
92         else {
93                 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
94                         $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
95                                 intval($_SESSION['visitor_id'])
96                         );
97                 }
98         }
99
100         if(count($r)) {
101                 $author = $r[0];
102                 $contact_id = $author['id'];
103         }
104
105         // get contact info for owner
106         
107         if($profile_uid == $_SESSION['uid']) {
108                 $contact_record = $author;
109         }
110         else {
111                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
112                         intval($profile_uid)
113                 );
114                 if(count($r))
115                         $contact_record = $r[0];
116         }
117
118         $post_type = notags(trim($_POST['type']));
119
120         if($post_type === 'net-comment') {
121                 if($parent_item !== null) {
122                         if($parent_item['type'] === 'remote') {
123                                 $post_type = 'remote-comment';
124                         } 
125                         else {          
126                                 $post_type = 'wall-comment';
127                         }
128                 }
129         }
130
131
132         /**
133          *
134          * If a photo was uploaded into the message using the ajax uploader,
135          * it can be seen by anybody. Set the permissions to match the message.
136          * Ideally this should be done when the photo was uploaded, but the permissions 
137          * may not have been set at that time, and passing the permission arrays via 
138          * javascript to the ajax upload is going to be a challenge.
139          * This is a compromise. Granted there is a window of time when the photo
140          * is public. You are welcome to suggest other ways to fix this.
141          *
142          */
143
144         $match = null;
145
146         if($private) {
147                 if(preg_match_all("/\[img\](.+?)\[\/img\]/",$body,$match)) {
148                         $images = $match[1];
149                         if(count($images)) {
150                                 foreach($images as $image) {
151                                         if(! stristr($image,$a->get_baseurl() . '/photo/'))
152                                                 continue;
153                                         $image_uri = substr($image,strrpos($image,'/') + 1);
154                                         $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
155                                         $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
156                                                 WHERE `resource-id` = '%s' AND `album` = '%s' ",
157                                                 dbesc($str_contact_allow),
158                                                 dbesc($str_group_allow),
159                                                 dbesc($str_contact_deny),
160                                                 dbesc($str_group_deny),
161                                                 dbesc($image_uri),
162                                                 dbesc( t('Wall Photos'))
163                                         );
164   
165                                 }
166                         }
167                 }
168         }
169
170
171         /**
172          * Look for any tags and linkify them
173          */
174
175         $str_tags = '';
176         $inform   = '';
177
178         $tags = get_tags($body);
179
180
181         if(count($tags)) {
182                 foreach($tags as $tag) {
183                         if(strpos($tag,'#') === 0) {
184                                 $basetag = str_replace('_',' ',substr($tag,1));
185                                 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
186                                 if(strlen($str_tags))
187                                         $str_tags .= ',';
188                                 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
189                                 continue;
190                         }
191                         if(strpos($tag,'@') === 0) {
192                                 $name = substr($tag,1);
193                                 if((strpos($name,'@')) || (strpos($name,'http://'))) {
194                                         $newname = $name;
195                                         $links = @lrdd($name);
196                                         if(count($links)) {
197                                                 foreach($links as $link) {
198                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
199                                         $profile = $link['@attributes']['href'];
200                                                         if($link['@attributes']['rel'] === 'salmon') {
201                                                                 if(strlen($inform))
202                                                                         $inform .= ',';
203                                         $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
204                                                         }
205                                                 }
206                                         }
207                                 }
208                                 else {
209                                         $newname = $name;
210                                         if(strstr($name,'_')) {
211                                                 $newname = str_replace('_',' ',$name);
212                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
213                                                         dbesc($newname),
214                                                         intval($profile_uid)
215                                                 );
216                                         }
217                                         else {
218                                                 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
219                                                         dbesc($name),
220                                                         intval($profile_uid)
221                                                 );
222                                         }
223                                         if(count($r)) {
224                                                 $profile = $r[0]['url'];
225                                                 $newname = $r[0]['name'];
226                                                 if(strlen($inform))
227                                                         $inform .= ',';
228                                                 $inform .= 'cid:' . $r[0]['id'];
229                                         }
230                                 }
231                                 if($profile) {
232                                         $body = str_replace($name,'[url=' . $profile . ']' . $newname   . '[/url]', $body);
233                                         $profile = str_replace(',','%2c',$profile);
234                                         if(strlen($str_tags))
235                                                 $str_tags .= ',';
236                                         $str_tags .= '@[url=' . $profile . ']' . $newname       . '[/url]';
237                                 }
238                         }
239                 }
240         }
241
242         $wall = 0;
243         if($post_type === 'wall' || $post_type === 'wall-comment')
244                 $wall = 1;
245
246         if(! strlen($verb))
247                 $verb = ACTIVITY_POST ;
248
249         $gravity = (($parent) ? 6 : 0 );
250  
251         $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
252
253         $uri = item_new_uri($a->get_hostname(),$profile_uid);
254
255         $datarray = array();
256         $datarray['uid']           = $profile_uid;
257         $datarray['type']          = $post_type;
258         $datarray['wall']          = $wall;
259         $datarray['gravity']       = $gravity;
260         $datarray['contact-id']    = $contact_id;
261         $datarray['owner-name']    = $contact_record['name'];
262         $datarray['owner-link']    = $contact_record['url'];
263         $datarray['owner-avatar']  = $contact_record['thumb'];
264         $datarray['author-name']   = $author['name'];
265         $datarray['author-link']   = $author['url'];
266         $datarray['author-avatar'] = $author['thumb'];
267         $datarray['created']       = datetime_convert();
268         $datarray['edited']        = datetime_convert();
269         $datarray['changed']       = datetime_convert();
270         $datarray['uri']           = $uri;
271         $datarray['title']         = $title;
272         $datarray['body']          = $body;
273         $datarray['location']      = $location;
274         $datarray['coord']         = $coord;
275         $datarray['tag']           = $str_tags;
276         $datarray['inform']        = $inform;
277         $datarray['verb']          = $verb;
278         $datarray['allow_cid']     = $str_contact_allow;
279         $datarray['allow_gid']     = $str_group_allow;
280         $datarray['deny_cid']      = $str_contact_deny;
281         $datarray['deny_gid']      = $str_group_deny;
282         $datarray['private']       = $private;
283
284         /**
285          * These fields are for the convenience of plugins...
286          * 'self' if true indicates the owner is posting on their own wall
287          * If parent is 0 it is a top-level post.
288          */
289
290         $datarray['parent']        = $parent;
291         $datarray['self']          = $self;
292
293
294         call_hooks('post_local',$datarray);
295
296         $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`, 
297                 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`, 
298                 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private` )
299                 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', '%s', '%s', %d )",
300                 intval($datarray['uid']),
301                 dbesc($datarray['type']),
302                 intval($datarray['wall']),
303                 intval($datarray['gravity']),
304                 intval($datarray['contact-id']),
305                 dbesc($datarray['owner-name']),
306                 dbesc($datarray['owner-link']),
307                 dbesc($datarray['owner-avatar']),
308                 dbesc($datarray['author-name']),
309                 dbesc($datarray['author-link']),
310                 dbesc($datarray['author-avatar']),
311                 dbesc($datarray['created']),
312                 dbesc($datarray['edited']),
313                 dbesc($datarray['changed']),
314                 dbesc($datarray['uri']),
315                 dbesc($datarray['title']),
316                 dbesc($datarray['body']),
317                 dbesc($datarray['location']),
318                 dbesc($datarray['coord']),
319                 dbesc($datarray['tag']),
320                 dbesc($datarray['inform']),
321                 dbesc($datarray['verb']),
322                 dbesc($datarray['allow_cid']),
323                 dbesc($datarray['allow_gid']),
324                 dbesc($datarray['deny_cid']),
325                 dbesc($datarray['deny_gid']),
326                 intval($datarray['private'])
327         );
328
329         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
330                 dbesc($datarray['uri']));
331         if(count($r)) {
332                 $post_id = $r[0]['id'];
333                 logger('mod_item: saved item ' . $post_id);
334
335                 if($parent) {
336
337                         // This item is the last leaf and gets the comment box, clear any ancestors
338                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
339                                 dbesc(datetime_convert()),
340                                 intval($parent)
341                         );
342
343                         // Inherit ACL's from the parent item.
344
345                         $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
346                                 WHERE `id` = %d LIMIT 1",
347                                 dbesc($parent_item['allow_cid']),
348                                 dbesc($parent_item['allow_gid']),
349                                 dbesc($parent_item['deny_cid']),
350                                 dbesc($parent_item['deny_gid']),
351                                 intval($parent_item['private']),
352                                 intval($post_id)
353                         );
354
355                         // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
356                         if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
357                                 require_once('bbcode.php');
358                                 $from = $author['name'];
359                                 $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
360                                 $email_tpl = replace_macros($tpl, array(
361                                         '$sitename' => $a->config['sitename'],
362                                         '$siteurl' =>  $a->get_baseurl(),
363                                         '$username' => $user['username'],
364                                         '$email' => $user['email'],
365                                         '$from' => $from,
366                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
367                                         '$body' => strip_tags(bbcode($datarray['body']))
368                                 ));
369
370                                 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
371                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
372                         }
373                 }
374                 else {
375                         $parent = $post_id;
376
377                         // let me know if somebody did a wall-to-wall post on my profile
378
379                         if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
380                                 require_once('bbcode.php');
381                                 $from = $author['name'];
382                                 $tpl = load_view_file('view/wall_received_eml.tpl');                    
383                                 $email_tpl = replace_macros($tpl, array(
384                                         '$sitename' => $a->config['sitename'],
385                                         '$siteurl' =>  $a->get_baseurl(),
386                                         '$username' => $user['username'],
387                                         '$email' => $user['email'],
388                                         '$from' => $from,
389                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
390                                         '$body' => strip_tags(bbcode($datarray['body']))
391                                 ));
392
393                                 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
394                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
395                         }
396                 }
397
398                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
399                         WHERE `id` = %d LIMIT 1",
400                         intval($parent),
401                         dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
402                         dbesc(datetime_convert()),
403                         intval($post_id)
404                 );
405
406                 // photo comments turn the corresponding item visible to the profile wall
407                 // This way we don't see every picture in your new photo album posted to your wall at once.
408                 // They will show up as people comment on them.
409
410                 if(! $parent_item['visible']) {
411                         $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
412                                 intval($parent_item['id'])
413                         );
414                 }
415         }
416
417         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
418
419         logger('mod_item: notifier invoked: ' . "\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &");
420
421         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &",
422                 array(),$foo));
423
424         $datarray['id'] = $post_id;
425
426         call_hooks('post_local_end', $datarray);
427  
428
429         goaway($a->get_baseurl() . "/" . $_POST['return'] );
430         return; // NOTREACHED
431 }
432
433
434
435
436
437 function item_content(&$a) {
438
439         if((! local_user()) && (! remote_user()))
440                 return;
441
442         require_once('include/security.php');
443
444         $uid = $_SESSION['uid'];
445
446         if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
447
448                 // locate item to be deleted
449
450                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
451                         intval($a->argv[2])
452                 );
453
454                 if(! count($r)) {
455                         notice( t('Item not found.') . EOL);
456                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
457                 }
458                 $item = $r[0];
459
460                 // check if logged in user is either the author or owner of this item
461
462                 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
463
464                         // delete the item
465
466                         $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
467                                 dbesc(datetime_convert()),
468                                 dbesc(datetime_convert()),
469                                 intval($item['id'])
470                         );
471
472                         // If item is a link to a photo resource, nuke all the associated photos 
473                         // (visitors will not have photo resources)
474                         // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
475                         // generate a resource-id and therefore aren't intimately linked to the item. 
476
477                         if(strlen($item['resource-id'])) {
478                                 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
479                                         dbesc($item['resource-id']),
480                                         intval($item['uid'])
481                                 );
482                                 // ignore the result
483                         }
484
485                         // If it's the parent of a comment thread, kill all the kids
486
487                         if($item['uri'] == $item['parent-uri']) {
488                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' 
489                                         WHERE `parent-uri` = '%s' AND `uid` = %d ",
490                                         dbesc(datetime_convert()),
491                                         dbesc(datetime_convert()),
492                                         dbesc($item['parent-uri']),
493                                         intval($item['uid'])
494                                 );
495                                 // ignore the result
496                         }
497                         else {
498                                 // ensure that last-child is set in case the comment that had it just got wiped.
499                                 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
500                                         dbesc(datetime_convert()),
501                                         dbesc($item['parent-uri']),
502                                         intval($item['uid'])
503                                 );
504                                 // who is the last child now? 
505                                 $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",
506                                         dbesc($item['parent-uri']),
507                                         intval($item['uid'])
508                                 );
509                                 if(count($r)) {
510                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
511                                                 intval($r[0]['id'])
512                                         );
513                                 }       
514                         }
515                         $drop_id = intval($item['id']);
516                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
517                         
518                         // send the notification upstream/downstream as the case may be
519
520                         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &",
521                                 array(), $foo));
522
523                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
524                         return; //NOTREACHED
525                 }
526                 else {
527                         notice( t('Permission denied.') . EOL);
528                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
529                         //NOTREACHED
530                 }
531         }
532 }