]> git.mxchange.org Git - friendica.git/blob - object/Item.php
Merge https://github.com/friendica/friendica into pull
[friendica.git] / object / Item.php
1 <?php
2 if(class_exists('Item'))
3         return;
4
5 require_once('object/BaseObject.php');
6 require_once('include/text.php');
7 require_once('boot.php');
8
9 /**
10  * An item
11  */
12 class Item extends BaseObject {
13         private $data = array();
14         private $template = null;
15         private $available_templates = array(
16                 'wall' => 'wall_thread.tpl',
17                 'wall2wall' => 'wallwall_thread.tpl'
18         );
19         private $comment_box_template = 'comment_item.tpl';
20         private $toplevel = false;
21         private $writable = false;
22         private $children = array();
23         private $parent = null;
24         private $conversation = null;
25         private $redirect_url = null;
26         private $owner_url = '';
27         private $owner_photo = '';
28         private $owner_name = '';
29         private $wall_to_wall = false;
30         private $threaded = false;
31         private $visiting = false;
32
33         public function __construct($data) {
34                 $a = $this->get_app();
35                 
36                 $this->data = $data;
37                 $this->set_template('wall');
38                 $this->toplevel = ($this->get_id() == $this->get_data_value('parent'));
39
40                 if(is_array($_SESSION['remote'])) {
41                         foreach($_SESSION['remote'] as $visitor) {
42                                 if($visitor['cid'] == $this->get_data_value('contact-id')) {
43                                         $this->visiting = true;
44                                         break;
45                                 }
46                         }
47                 }
48                 
49                 $this->writable = ($this->get_data_value('writable') || $this->get_data_value('self'));
50
51                 $ssl_state = ((local_user()) ? true : false);
52                 $this->redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $this->get_data_value('cid') ;
53
54                 if(get_config('system','thread_allow') && $a->theme_thread_allow && !$this->is_toplevel())
55                         $this->threaded = true;
56
57                 // Prepare the children
58                 if(count($data['children'])) {
59                         foreach($data['children'] as $item) {
60                                 /*
61                                  * Only add will be displayed
62                                  */
63                                 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
64                                         continue;
65                                 }
66                                 if(! visible_activity($item)) {
67                                         continue;
68                                 }
69                                 $child = new Item($item);
70                                 $this->add_child($child);
71                         }
72                 }
73         }
74
75         /**
76          * Get data in a form usable by a conversation template
77          *
78          * Returns:
79          *      _ The data requested on success
80          *      _ false on failure
81          */
82         public function get_template_data($alike, $dlike, $thread_level=1) {
83                 $result = array();
84
85                 $a = $this->get_app();
86
87                 $item = $this->get_data();
88
89                 $commentww = '';
90                 $sparkle = '';
91                 $buttons = '';
92                 $dropping = false;
93                 $star = false;
94                 $isstarred = "unstarred";
95                 $indent = '';
96                 $shiny = '';
97                 $osparkle = '';
98                 $total_children = $this->count_descendants();
99
100                 $conv = $this->get_conversation();
101
102                 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid']) 
103                         || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
104                         ? t('Private Message')
105                         : false);
106                 $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
107                 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
108                         $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
109                 else
110                         $edpost = false;
111                 if(($this->get_data_value('uid') == local_user()) || $this->is_visiting())
112                         $dropping = true;
113
114                 $drop = array(
115                         'dropping' => $dropping,
116                         'pagedrop' => ((feature_enabled($conv->get_profile_owner(),'multi_delete')) ? $item['pagedrop'] : ''),
117                         'select' => t('Select'), 
118                         'delete' => t('Delete'),
119                 );
120                 
121                 $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
122
123                 $diff_author    = ((link_compare($item['url'],$item['author-link'])) ? false : true);
124                 $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
125                 if($item['author-link'] && (! $item['author-name']))
126                         $profile_name = $item['author-link'];
127
128                 $sp = false;
129                 $profile_link = best_link_url($item,$sp);
130                 if($profile_link === 'mailbox')
131                         $profile_link = '';
132                 if($sp)
133                         $sparkle = ' sparkle';
134                 else
135                         $profile_link = zrl($profile_link);                 
136
137                 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
138                 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
139                         $profile_avatar = $a->contacts[$normalised]['thumb'];
140                 else
141                         $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($this->get_data_value('thumb')));
142
143                 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
144                 call_hooks('render_location',$locate);
145                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
146
147                 $tags=array();
148                 $hashtags = array();
149                 $mentions = array();
150                 foreach(explode(',',$item['tag']) as $tag){
151                         $tag = trim($tag);
152                         if ($tag!="") {
153                                 $t = bbcode($tag);
154                                 $tags[] = $t;
155                                 if($t[0] == '#')
156                                         $hashtags[] = $t;
157                                 elseif($t[0] == '@')
158                                         $mentions[] = $t;
159                         }
160
161                 }        
162
163                 $like    = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
164                 $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
165
166                 /*
167                  * We should avoid doing this all the time, but it depends on the conversation mode
168                  * And the conv mode may change when we change the conv, or it changes its mode
169                  * Maybe we should establish a way to be notified about conversation changes
170                  */
171                 $this->check_wall_to_wall();
172                 
173                 if($this->is_wall_to_wall() && ($this->get_owner_url() == $this->get_redirect_url()))
174                         $osparkle = ' sparkle';
175                 
176                 if($this->is_toplevel()) {
177                         if($conv->get_profile_owner() == local_user()) {
178                                 $isstarred = (($item['starred']) ? "starred" : "unstarred");
179
180                                 $star = array(
181                                         'do' => t("add star"),
182                                         'undo' => t("remove star"),
183                                         'toggle' => t("toggle star status"),
184                                         'classdo' => (($item['starred']) ? "hidden" : ""),
185                                         'classundo' => (($item['starred']) ? "" : "hidden"),
186                                         'starred' =>  t('starred'),
187                                 );
188                                 $tagger = '';
189                                 if(feature_enabled($conv->get_profile_owner(),'commtag')) {
190                                         $tagger = array(
191                                                 'add' => t("add tag"),
192                                                 'class' => "",
193                                         );
194                                 }
195                         }
196                 } else {
197                         $indent = 'comment';
198                 }
199
200                 if($conv->is_writable()) {
201                         $buttons = array(
202                                 'like' => array( t("I like this \x28toggle\x29"), t("like")),
203                                 'dislike' => ((feature_enabled($conv->get_profile_owner(),'dislike')) ? array( t("I don't like this \x28toggle\x29"), t("dislike")) : ''),
204                         );
205                         if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
206                 }
207
208                 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0){
209                         $shiny = 'shiny';
210                 }
211
212                 localize_item($item);
213
214                 $body = prepare_body($item,true);
215
216         list($categories, $folders) = get_cats_and_terms($item);
217
218                 $tmp_item = array(
219                         'template' => $this->get_template(),
220                         
221                         'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
222                         'tags' => $tags,
223             'hashtags' => $hashtags,
224             'mentions' => $mentions,
225                         'txt_cats' => t('Categories:'),
226                         'txt_folders' => t('Filed under:'),
227                         'has_cats' => ((count($categories)) ? 'true' : ''),
228                         'has_folders' => ((count($folders)) ? 'true' : ''),
229             'categories' => $categories,
230             'folders' => $folders,            
231                         'body' => template_escape($body),
232                         'text' => strip_tags(template_escape($body)),
233                         'id' => $this->get_id(),
234                         'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
235                         'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $this->get_owner_name(), ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
236                         'to' => t('to'),
237                         'via' => t('via'),
238                         'wall' => t('Wall-to-Wall'),
239                         'vwall' => t('via Wall-To-Wall:'),
240                         'profile_url' => $profile_link,
241                         'item_photo_menu' => item_photo_menu($item),
242                         'name' => template_escape($profile_name),
243                         'thumb' => $profile_avatar,
244                         'osparkle' => $osparkle,
245                         'sparkle' => $sparkle,
246                         'title' => template_escape($item['title']),
247                         'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
248                         'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
249                         'lock' => $lock,
250                         'location' => template_escape($location),
251                         'indent' => $indent,
252                         'shiny' => $shiny,
253                         'owner_url' => $this->get_owner_url(),
254                         'owner_photo' => $this->get_owner_photo(),
255                         'owner_name' => template_escape($this->get_owner_name()),
256                         'plink' => get_plink($item),
257                         'edpost'    => ((feature_enabled($conv->get_profile_owner(),'edit_posts')) ? $edpost : ''),
258                         'isstarred' => $isstarred,
259                         'star'      => ((feature_enabled($conv->get_profile_owner(),'star_posts')) ? $star : ''),
260                         'tagger'        => $tagger,
261                         'filer'     => ((feature_enabled($conv->get_profile_owner(),'filing')) ? $filer : ''),
262                         'drop' => $drop,
263                         'vote' => $buttons,
264                         'like' => $like,
265                         'dislike'   => $dislike,
266                         'switchcomment' => t('Comment'),
267                         'comment' => $this->get_comment_box($indent),
268                         'previewing' => ($conv->is_preview() ? ' preview ' : ''),
269                         'wait' => t('Please wait'),
270                         'thread_level' => $thread_level
271                 );
272
273                 $arr = array('item' => $item, 'output' => $tmp_item);
274                 call_hooks('display_item', $arr);
275
276                 $result = $arr['output'];
277
278                 $result['children'] = array();
279                 $children = $this->get_children();
280                 $nb_children = count($children);
281                 if($nb_children > 0) {
282                         foreach($children as $child) {
283                                 $result['children'][] = $child->get_template_data($alike, $dlike, $thread_level + 1);
284                         }
285                         // Collapse
286                         if(($nb_children > 2) || ($thread_level > 1)) {
287                                 $result['children'][0]['comment_firstcollapsed'] = true;
288                                 $result['children'][0]['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
289                                 $result['children'][0]['hidden_comments_num'] = $total_children;
290                                 $result['children'][0]['hidden_comments_text'] = tt('comment', 'comments', $total_children);
291                                 $result['children'][0]['hide_text'] = t('show more');
292                                 if($thread_level > 1) {
293                                         $result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
294                                 }
295                                 else {
296                                         $result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
297                                 }
298                         }
299                 }
300                 
301         if ($this->is_toplevel()) {
302             $result['total_comments_num'] = "$total_children";
303             $result['total_comments_text'] = tt('comment', 'comments', $total_children);
304         }
305         
306                 $result['private'] = $item['private'];
307                 $result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
308
309                 if($this->is_threaded()) {
310                         $result['flatten'] = false;
311                         $result['threaded'] = true;
312                 }
313                 else {
314                         $result['flatten'] = true;
315                         $result['threaded'] = false;
316                 }
317
318                 return $result;
319         }
320         
321         public function get_id() {
322                 return $this->get_data_value('id');
323         }
324
325         public function is_threaded() {
326                 return $this->threaded;
327         }
328
329         /**
330          * Add a child item
331          */
332         public function add_child($item) {
333                 $item_id = $item->get_id();
334                 if(!$item_id) {
335                         logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
336                         return false;
337                 }
338                 if($this->get_child($item->get_id())) {
339                         logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
340                         return false;
341                 }
342                 /*
343                  * Only add what will be displayed
344                  */
345                 if($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid')) {
346                         return false;
347                 }
348                 if(activity_match($item->get_data_value('verb'),ACTIVITY_LIKE) || activity_match($item->get_data_value('verb'),ACTIVITY_DISLIKE)) {
349                         return false;
350                 }
351                 
352                 $item->set_parent($this);
353                 $this->children[] = $item;
354                 return end($this->children);
355         }
356
357         /**
358          * Get a child by its ID
359          */
360         public function get_child($id) {
361                 foreach($this->get_children() as $child) {
362                         if($child->get_id() == $id)
363                                 return $child;
364                 }
365                 return null;
366         }
367
368         /**
369          * Get all ou children
370          */
371         public function get_children() {
372                 return $this->children;
373         }
374
375         /**
376          * Set our parent
377          */
378         protected function set_parent($item) {
379                 $parent = $this->get_parent();
380                 if($parent) {
381                         $parent->remove_child($this);
382                 }
383                 $this->parent = $item;
384                 $this->set_conversation($item->get_conversation());
385         }
386
387         /**
388          * Remove our parent
389          */
390         protected function remove_parent() {
391                 $this->parent = null;
392                 $this->conversation = null;
393         }
394
395         /**
396          * Remove a child
397          */
398         public function remove_child($item) {
399                 $id = $item->get_id();
400                 foreach($this->get_children() as $key => $child) {
401                         if($child->get_id() == $id) {
402                                 $child->remove_parent();
403                                 unset($this->children[$key]);
404                                 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
405                                 $this->children = array_values($this->children);
406                                 return true;
407                         }
408                 }
409                 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
410                 return false;
411         }
412
413         /**
414          * Get parent item
415          */
416         protected function get_parent() {
417                 return $this->parent;
418         }
419
420         /**
421          * set conversation
422          */
423         public function set_conversation($conv) {
424                 $previous_mode = ($this->conversation ? $this->conversation->get_mode() : '');
425                 
426                 $this->conversation = $conv;
427
428                 // Set it on our children too
429                 foreach($this->get_children() as $child)
430                         $child->set_conversation($conv);
431         }
432
433         /**
434          * get conversation
435          */
436         public function get_conversation() {
437                 return $this->conversation;
438         }
439
440         /**
441          * Get raw data
442          *
443          * We shouldn't need this
444          */
445         public function get_data() {
446                 return $this->data;
447         }
448
449         /**
450          * Get a data value
451          *
452          * Returns:
453          *      _ value on success
454          *      _ false on failure
455          */
456         public function get_data_value($name) {
457                 if(!isset($this->data[$name])) {
458                         logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
459                         return false;
460                 }
461
462                 return $this->data[$name];
463         }
464
465         /**
466          * Set template
467          */
468         private function set_template($name) {
469                 if(!x($this->available_templates, $name)) {
470                         logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
471                         return false;
472                 }
473                 $this->template = $this->available_templates[$name];
474         }
475
476         /**
477          * Get template
478          */
479         private function get_template() {
480                 return $this->template;
481         }
482
483         /**
484          * Check if this is a toplevel post
485          */
486         private function is_toplevel() {
487                 return $this->toplevel;
488         }
489
490         /**
491          * Check if this is writable
492          */
493         private function is_writable() {
494                 $conv = $this->get_conversation();
495
496                 if($conv) {
497                         // This will allow us to comment on wall-to-wall items owned by our friends
498                         // and community forums even if somebody else wrote the post.
499                         return ($this->writable || ($this->is_visiting() && $conv->get_mode() == 'profile'));
500                 }
501                 return $this->writable;
502         }
503
504         /**
505          * Count the total of our descendants
506          */
507         private function count_descendants() {
508                 $children = $this->get_children();
509                 $total = count($children);
510                 if($total > 0) {
511                         foreach($children as $child) {
512                                 $total += $child->count_descendants();
513                         }
514                 }
515                 return $total;
516         }
517
518         /**
519          * Get the template for the comment box
520          */
521         private function get_comment_box_template() {
522                 return $this->comment_box_template;
523         }
524
525         /**
526          * Get the comment box
527          *
528          * Returns:
529          *      _ The comment box string (empty if no comment box)
530          *      _ false on failure
531          */
532         private function get_comment_box($indent) {
533                 $a = $this->get_app();
534                 if(!$this->is_toplevel() && !(get_config('system','thread_allow') && $a->theme_thread_allow)) {
535                         return '';
536                 }
537                 
538                 $comment_box = '';
539                 $conv = $this->get_conversation();
540                 $template = get_markup_template($this->get_comment_box_template());
541                 $ww = '';
542                 if( ($conv->get_mode() === 'network') && $this->is_wall_to_wall() )
543                         $ww = 'ww';
544
545                 if($conv->is_writable() && $this->is_writable()) {
546                         $qc = $qcomment =  null;
547
548                         /*
549                          * Hmmm, code depending on the presence of a particular plugin?
550                          * This should be better if done by a hook
551                          */
552                         if(in_array('qcomment',$a->plugins)) {
553                                 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
554                                 $qcomment = (($qc) ? explode("\n",$qc) : null);
555                         }
556                         $comment_box = replace_macros($template,array(
557                                 '$return_path' => '',
558                                 '$threaded' => $this->is_threaded(),
559 //                              '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
560                                 '$jsreload' => '',
561                                 '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
562                                 '$id' => $this->get_id(),
563                                 '$parent' => $this->get_id(),
564                                 '$qcomment' => $qcomment,
565                                 '$profile_uid' =>  $conv->get_profile_owner(),
566                                 '$mylink' => $a->contact['url'],
567                                 '$mytitle' => t('This is you'),
568                                 '$myphoto' => $a->contact['thumb'],
569                                 '$comment' => t('Comment'),
570                                 '$submit' => t('Submit'),
571                                 '$edbold' => t('Bold'),
572                                 '$editalic' => t('Italic'),
573                                 '$eduline' => t('Underline'),
574                                 '$edquote' => t('Quote'),
575                                 '$edcode' => t('Code'),
576                                 '$edimg' => t('Image'),
577                                 '$edurl' => t('Link'),
578                                 '$edvideo' => t('Video'),
579                                 '$preview' => ((feature_enabled($conv->get_profile_owner(),'preview')) ? t('Preview') : ''),
580                                 '$indent' => $indent,
581                                 '$sourceapp' => t($a->sourcename),
582                                 '$ww' => (($conv->get_mode() === 'network') ? $ww : ''),
583                                 '$rand_num' => random_digits(12)
584                         ));
585                 }
586
587                 return $comment_box;
588         }
589
590         private function get_redirect_url() {
591                 return $this->redirect_url;
592         }
593
594         /**
595          * Check if we are a wall to wall item and set the relevant properties
596          */
597         protected function check_wall_to_wall() {
598                 $a = $this->get_app();
599                 $conv = $this->get_conversation();
600                 $this->wall_to_wall = false;
601                 
602                 if($this->is_toplevel()) {
603                         if( (! $this->get_data_value('self')) && ($conv->get_mode() !== 'profile')) {
604                                 if($this->get_data_value('wall')) {
605
606                                         // On the network page, I am the owner. On the display page it will be the profile owner.
607                                         // This will have been stored in $a->page_contact by our calling page.
608                                         // Put this person as the wall owner of the wall-to-wall notice.
609
610                                         $this->owner_url = zrl($a->page_contact['url']);
611                                         $this->owner_photo = $a->page_contact['thumb'];
612                                         $this->owner_name = $a->page_contact['name'];
613                                         $this->wall_to_wall = true;
614                                 }
615                                 else if($this->get_data_value('owner-link')) {
616
617                                         $owner_linkmatch = (($this->get_data_value('owner-link')) && link_compare($this->get_data_value('owner-link'),$this->get_data_value('author-link')));
618                                         $alias_linkmatch = (($this->get_data_value('alias')) && link_compare($this->get_data_value('alias'),$this->get_data_value('author-link')));
619                                         $owner_namematch = (($this->get_data_value('owner-name')) && $this->get_data_value('owner-name') == $this->get_data_value('author-name'));
620                                         if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
621
622                                                 // The author url doesn't match the owner (typically the contact)
623                                                 // and also doesn't match the contact alias. 
624                                                 // The name match is a hack to catch several weird cases where URLs are 
625                                                 // all over the park. It can be tricked, but this prevents you from
626                                                 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
627                                                 // well that it's the same Bob Smith. 
628
629                                                 // But it could be somebody else with the same name. It just isn't highly likely. 
630                                                 
631
632                                                 $this->owner_photo = $this->get_data_value('owner-avatar');
633                                                 $this->owner_name = $this->get_data_value('owner-name');
634                                                 $this->wall_to_wall = true;
635                                                 // If it is our contact, use a friendly redirect link
636                                                 if((link_compare($this->get_data_value('owner-link'),$this->get_data_value('url'))) 
637                                                         && ($this->get_data_value('network') === NETWORK_DFRN)) {
638                                                         $this->owner_url = $this->get_redirect_url();
639                                                 }
640                                                 else
641                                                         $this->owner_url = zrl($this->get_data_value('owner-link'));
642                                         }
643                                 }
644                         }
645                 }
646
647                 if(!$this->wall_to_wall) {
648                         $this->set_template('wall');
649                         $this->owner_url = '';
650                         $this->owner_photo = '';
651                         $this->owner_name = '';
652                 }
653         }
654
655         private function is_wall_to_wall() {
656                 return $this->wall_to_wall;
657         }
658
659         private function get_owner_url() {
660                 return $this->owner_url;
661         }
662
663         private function get_owner_photo() {
664                 return $this->owner_photo;
665         }
666
667         private function get_owner_name() {
668                 return $this->owner_name;
669         }
670
671         private function is_visiting() {
672                 return $this->visiting;
673         }
674
675
676
677
678 }
679 ?>