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