2 if(class_exists('Item'))
5 use Friendica\Core\Config;
6 use Friendica\Core\PConfig;
7 use Friendica\Protocol\Diaspora;
9 require_once('object/BaseObject.php');
10 require_once('include/text.php');
11 require_once('boot.php');
16 class Item extends BaseObject {
17 private $data = array();
18 private $template = null;
19 private $available_templates = array(
20 'wall' => 'wall_thread.tpl',
21 'wall2wall' => 'wallwall_thread.tpl'
23 private $comment_box_template = 'comment_item.tpl';
24 private $toplevel = false;
25 private $writable = false;
26 private $children = array();
27 private $parent = null;
28 private $conversation = null;
29 private $redirect_url = null;
30 private $owner_url = '';
31 private $owner_photo = '';
32 private $owner_name = '';
33 private $wall_to_wall = false;
34 private $threaded = false;
35 private $visiting = false;
37 public function __construct($data) {
38 $a = $this->get_app();
41 $this->set_template('wall');
42 $this->toplevel = ($this->get_id() == $this->get_data_value('parent'));
44 if (is_array($_SESSION['remote'])) {
45 foreach ($_SESSION['remote'] as $visitor) {
46 if ($visitor['cid'] == $this->get_data_value('contact-id')) {
47 $this->visiting = true;
53 $this->writable = ($this->get_data_value('writable') || $this->get_data_value('self'));
55 $ssl_state = ((local_user()) ? true : false);
56 $this->redirect_url = 'redir/' . $this->get_data_value('cid') ;
58 if (Config::get('system','thread_allow') && $a->theme_thread_allow && !$this->is_toplevel()) {
59 $this->threaded = true;
62 // Prepare the children
63 if (count($data['children'])) {
64 foreach ($data['children'] as $item) {
66 * Only add will be displayed
68 if ($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
70 } elseif (! visible_activity($item)) {
74 // You can always comment on Diaspora items
75 if (($item['network'] == NETWORK_DIASPORA) && (local_user() == $item['uid'])) {
76 $item['writable'] = true;
79 $item['pagedrop'] = $data['pagedrop'];
80 $child = new Item($item);
81 $this->add_child($child);
87 * Get data in a form usable by a conversation template
90 * _ The data requested on success
93 public function get_template_data($conv_responses, $thread_level=1) {
94 require_once("mod/proxy.php");
98 $a = $this->get_app();
100 $item = $this->get_data();
102 // If the time between "created" and "edited" differs we add
103 // a notice that the post was edited.
104 // Note: In some networks reshared items seem to have (sometimes) a difference
105 // between creation time and edit time of a second. Thats why we add the notice
106 // only if the difference is more than 1 second.
107 if (strtotime($item['edited']) - strtotime($item['created']) > 1) {
109 'label' => t('This entry was edited'),
110 'date' => datetime_convert('UTC', date_default_timezone_get(), $item['edited'], 'r'),
111 'relative' => relative_date($item['edited'])
120 $isstarred = "unstarred";
124 $total_children = $this->count_descendants();
126 $conv = $this->get_conversation();
128 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
129 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
130 ? t('Private Message')
132 $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
133 if (local_user() && link_compare($a->contact['url'],$item['author-link'])) {
134 if ($item["event-id"] != 0) {
135 $edpost = array("events/event/".$item['event-id'], t("Edit"));
137 $edpost = array("editpost/".$item['id'], t("Edit"));
143 if (($this->get_data_value('uid') == local_user()) || $this->is_visiting()) {
148 'dropping' => $dropping,
149 'pagedrop' => ((feature_enabled($conv->get_profile_owner(),'multi_delete')) ? $item['pagedrop'] : ''),
150 'select' => t('Select'),
151 'delete' => t('Delete'),
154 $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
156 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
157 $profile_name = htmlentities(((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
158 if ($item['author-link'] && (! $item['author-name'])) {
159 $profile_name = $item['author-link'];
163 $profile_link = best_link_url($item,$sp);
164 if ($profile_link === 'mailbox') {
169 $sparkle = ' sparkle';
171 $profile_link = zrl($profile_link);
174 if (!isset($item['author-thumb']) || ($item['author-thumb'] == "")) {
175 $author_contact = get_contact_details_by_url($item['author-link'], $conv->get_profile_owner());
176 if ($author_contact["thumb"]) {
177 $item['author-thumb'] = $author_contact["thumb"];
179 $item['author-thumb'] = $item['author-avatar'];
183 if (!isset($item['owner-thumb']) || ($item['owner-thumb'] == "")) {
184 $owner_contact = get_contact_details_by_url($item['owner-link'], $conv->get_profile_owner());
185 if ($owner_contact["thumb"]) {
186 $item['owner-thumb'] = $owner_contact["thumb"];
188 $item['owner-thumb'] = $item['owner-avatar'];
192 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
193 call_hooks('render_location',$locate);
194 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_dummy($locate));
201 /*foreach(explode(',',$item['tag']) as $tag){
213 // process action responses - e.g. like/dislike/attend/agree/whatever
214 $response_verbs = array('like');
215 if (feature_enabled($conv->get_profile_owner(),'dislike')) {
216 $response_verbs[] = 'dislike';
218 if ($item['object-type'] === ACTIVITY_OBJ_EVENT) {
219 $response_verbs[] = 'attendyes';
220 $response_verbs[] = 'attendno';
221 $response_verbs[] = 'attendmaybe';
222 if ($conv->is_writable()) {
224 $attend = array( t('I will attend'), t('I will not attend'), t('I might attend'));
228 $responses = get_responses($conv_responses,$response_verbs,$this,$item);
230 foreach ($response_verbs as $value=>$verbs) {
231 $responses[$verbs]['output'] = ((x($conv_responses[$verbs],$item['uri'])) ? format_like($conv_responses[$verbs][$item['uri']],$conv_responses[$verbs][$item['uri'] . '-l'],$verbs,$item['uri']) : '');
235 * We should avoid doing this all the time, but it depends on the conversation mode
236 * And the conv mode may change when we change the conv, or it changes its mode
237 * Maybe we should establish a way to be notified about conversation changes
239 $this->check_wall_to_wall();
241 if ($this->is_wall_to_wall() && ($this->get_owner_url() == $this->get_redirect_url())) {
242 $osparkle = ' sparkle';
245 if ($this->is_toplevel()) {
246 if ($conv->get_profile_owner() == local_user()) {
247 $isstarred = (($item['starred']) ? "starred" : "unstarred");
250 'do' => t("add star"),
251 'undo' => t("remove star"),
252 'toggle' => t("toggle star status"),
253 'classdo' => (($item['starred']) ? "hidden" : ""),
254 'classundo' => (($item['starred']) ? "" : "hidden"),
255 'starred' => t('starred'),
257 $r = dba::select('thread', array('ignored'), array('uid' => $item['uid'], 'iid' => $item['id']), array('limit' => 1));
258 if (dbm::is_result($r)) {
260 'do' => t("ignore thread"),
261 'undo' => t("unignore thread"),
262 'toggle' => t("toggle ignore status"),
263 'classdo' => (($r['ignored']) ? "hidden" : ""),
264 'classundo' => (($r['ignored']) ? "" : "hidden"),
265 'ignored' => t('ignored'),
270 if(feature_enabled($conv->get_profile_owner(),'commtag')) {
272 'add' => t("add tag"),
281 if ($conv->is_writable()) {
283 'like' => array( t("I like this \x28toggle\x29"), t("like")),
284 'dislike' => ((feature_enabled($conv->get_profile_owner(),'dislike')) ? array( t("I don't like this \x28toggle\x29"), t("dislike")) : ''),
287 $buttons['share'] = array( t('Share this'), t('share'));
291 $comment = $this->get_comment_box($indent);
293 if (strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0){
297 localize_item($item);
299 $body = prepare_body($item,true);
301 list($categories, $folders) = get_cats_and_terms($item);
303 if ($a->theme['template_engine'] === 'internal') {
304 $body_e = template_escape($body);
305 $text_e = strip_tags(template_escape($body));
306 $name_e = template_escape($profile_name);
307 $title_e = template_escape($item['title']);
308 $location_e = template_escape($location);
309 $owner_name_e = template_escape($this->get_owner_name());
312 $text_e = strip_tags($body);
313 $name_e = $profile_name;
314 $title_e = $item['title'];
315 $location_e = $location;
316 $owner_name_e = $this->get_owner_name();
319 // Disable features that aren't available in several networks
321 /// @todo Add NETWORK_DIASPORA when it will pass this information
322 if (!in_array($item["item_network"], array(NETWORK_DFRN)) && isset($buttons["dislike"])) {
323 unset($buttons["dislike"],$isevent);
327 if (($item["item_network"] == NETWORK_FEED) && isset($buttons["like"])) {
328 unset($buttons["like"]);
331 if (($item["item_network"] == NETWORK_MAIL) && isset($buttons["like"])) {
332 unset($buttons["like"]);
336 'template' => $this->get_template(),
337 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
338 'tags' => $item['tags'],
339 'hashtags' => $item['hashtags'],
340 'mentions' => $item['mentions'],
341 'txt_cats' => t('Categories:'),
342 'txt_folders' => t('Filed under:'),
343 'has_cats' => ((count($categories)) ? 'true' : ''),
344 'has_folders' => ((count($folders)) ? 'true' : ''),
345 'categories' => $categories,
346 'folders' => $folders,
349 'id' => $this->get_id(),
350 'guid' => urlencode($item['guid']),
351 'isevent' => $isevent,
353 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
354 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), htmlentities($this->get_owner_name()), ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
357 'wall' => t('Wall-to-Wall'),
358 'vwall' => t('via Wall-To-Wall:'),
359 'profile_url' => $profile_link,
360 'item_photo_menu' => item_photo_menu($item),
362 'thumb' => $a->remove_baseurl(proxy_url($item['author-thumb'], false, PROXY_SIZE_THUMB)),
363 'osparkle' => $osparkle,
364 'sparkle' => $sparkle,
366 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
367 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
368 'app' => $item['app'],
369 'created' => relative_date($item['created']),
371 'location' => $location_e,
374 'owner_url' => $this->get_owner_url(),
375 'owner_photo' => $a->remove_baseurl(proxy_url($item['owner-thumb'], false, PROXY_SIZE_THUMB)),
376 'owner_name' => htmlentities($owner_name_e),
377 'plink' => get_plink($item),
378 'edpost' => ((feature_enabled($conv->get_profile_owner(),'edit_posts')) ? $edpost : ''),
379 'isstarred' => $isstarred,
380 'star' => ((feature_enabled($conv->get_profile_owner(),'star_posts')) ? $star : ''),
381 'ignore' => ((feature_enabled($conv->get_profile_owner(),'ignore_posts')) ? $ignore : ''),
383 'filer' => ((feature_enabled($conv->get_profile_owner(),'filing')) ? $filer : ''),
386 'like' => $responses['like']['output'],
387 'dislike' => $responses['dislike']['output'],
388 'responses' => $responses,
389 'switchcomment' => t('Comment'),
390 'comment' => $comment,
391 'previewing' => ($conv->is_preview() ? ' preview ' : ''),
392 'wait' => t('Please wait'),
393 'thread_level' => $thread_level,
395 'network' => $item["item_network"],
396 'network_name' => network_to_name($item['item_network'], $profile_link),
397 'received' => $item['received'],
398 'commented' => $item['commented'],
399 'created_date' => $item['created'],
402 $arr = array('item' => $item, 'output' => $tmp_item);
403 call_hooks('display_item', $arr);
405 $result = $arr['output'];
407 $result['children'] = array();
408 $children = $this->get_children();
409 $nb_children = count($children);
410 if ($nb_children > 0) {
411 foreach ($children as $child) {
412 $result['children'][] = $child->get_template_data($conv_responses, $thread_level + 1);
415 if (($nb_children > 2) || ($thread_level > 1)) {
416 $result['children'][0]['comment_firstcollapsed'] = true;
417 $result['children'][0]['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
418 $result['children'][0]['hidden_comments_num'] = $total_children;
419 $result['children'][0]['hidden_comments_text'] = tt('comment', 'comments', $total_children);
420 $result['children'][0]['hide_text'] = t('show more');
421 if ($thread_level > 1) {
422 $result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
424 $result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
429 if ($this->is_toplevel()) {
430 $result['total_comments_num'] = "$total_children";
431 $result['total_comments_text'] = tt('comment', 'comments', $total_children);
434 $result['private'] = $item['private'];
435 $result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
437 if ($this->is_threaded()) {
438 $result['flatten'] = false;
439 $result['threaded'] = true;
441 $result['flatten'] = true;
442 $result['threaded'] = false;
448 public function get_id() {
449 return $this->get_data_value('id');
452 public function is_threaded() {
453 return $this->threaded;
459 public function add_child(Item $item) {
460 $item_id = $item->get_id();
462 logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
464 } elseif ($this->get_child($item->get_id())) {
465 logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
469 * Only add what will be displayed
471 if ($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid')) {
473 } elseif (activity_match($item->get_data_value('verb'),ACTIVITY_LIKE) || activity_match($item->get_data_value('verb'),ACTIVITY_DISLIKE)) {
477 $item->set_parent($this);
478 $this->children[] = $item;
479 return end($this->children);
483 * Get a child by its ID
485 public function get_child($id) {
486 foreach ($this->get_children() as $child) {
487 if ($child->get_id() == $id) {
495 * Get all ou children
497 public function get_children() {
498 return $this->children;
504 protected function set_parent($item) {
505 $parent = $this->get_parent();
507 $parent->remove_child($this);
509 $this->parent = $item;
510 $this->set_conversation($item->get_conversation());
516 protected function remove_parent() {
517 $this->parent = null;
518 $this->conversation = null;
524 public function remove_child($item) {
525 $id = $item->get_id();
526 foreach ($this->get_children() as $key => $child) {
527 if ($child->get_id() == $id) {
528 $child->remove_parent();
529 unset($this->children[$key]);
530 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
531 $this->children = array_values($this->children);
535 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
542 protected function get_parent() {
543 return $this->parent;
549 public function set_conversation($conv) {
550 $previous_mode = ($this->conversation ? $this->conversation->get_mode() : '');
552 $this->conversation = $conv;
554 // Set it on our children too
555 foreach ($this->get_children() as $child) {
556 $child->set_conversation($conv);
563 public function get_conversation() {
564 return $this->conversation;
570 * We shouldn't need this
572 public function get_data() {
583 public function get_data_value($name) {
584 if (!isset($this->data[$name])) {
585 // logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
589 return $this->data[$name];
595 private function set_template($name) {
596 if (!x($this->available_templates, $name)) {
597 logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
601 $this->template = $this->available_templates[$name];
607 private function get_template() {
608 return $this->template;
612 * Check if this is a toplevel post
614 private function is_toplevel() {
615 return $this->toplevel;
619 * Check if this is writable
621 private function is_writable() {
622 $conv = $this->get_conversation();
625 // This will allow us to comment on wall-to-wall items owned by our friends
626 // and community forums even if somebody else wrote the post.
628 // bug #517 - this fixes for conversation owner
629 if ($conv->get_mode() == 'profile' && $conv->get_profile_owner() == local_user()) {
633 // this fixes for visitors
634 return ($this->writable || ($this->is_visiting() && $conv->get_mode() == 'profile'));
636 return $this->writable;
640 * Count the total of our descendants
642 private function count_descendants() {
643 $children = $this->get_children();
644 $total = count($children);
646 foreach ($children as $child) {
647 $total += $child->count_descendants();
654 * Get the template for the comment box
656 private function get_comment_box_template() {
657 return $this->comment_box_template;
661 * Get the comment box
664 * _ The comment box string (empty if no comment box)
667 private function get_comment_box($indent) {
668 $a = $this->get_app();
669 if (!$this->is_toplevel() && !(Config::get('system','thread_allow') && $a->theme_thread_allow)) {
674 $conv = $this->get_conversation();
675 $template = get_markup_template($this->get_comment_box_template());
677 if ( ($conv->get_mode() === 'network') && $this->is_wall_to_wall() )
680 if ($conv->is_writable() && $this->is_writable()) {
681 $qc = $qcomment = null;
684 * Hmmm, code depending on the presence of a particular plugin?
685 * This should be better if done by a hook
687 if (in_array('qcomment',$a->plugins)) {
688 $qc = ((local_user()) ? PConfig::get(local_user(),'qcomment','words') : null);
689 $qcomment = (($qc) ? explode("\n",$qc) : null);
691 $comment_box = replace_macros($template,array(
692 '$return_path' => $a->query_string,
693 '$threaded' => $this->is_threaded(),
694 // '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
696 '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
697 '$id' => $this->get_id(),
698 '$parent' => $this->get_id(),
699 '$qcomment' => $qcomment,
700 '$profile_uid' => $conv->get_profile_owner(),
701 '$mylink' => $a->remove_baseurl($a->contact['url']),
702 '$mytitle' => t('This is you'),
703 '$myphoto' => $a->remove_baseurl($a->contact['thumb']),
704 '$comment' => t('Comment'),
705 '$submit' => t('Submit'),
706 '$edbold' => t('Bold'),
707 '$editalic' => t('Italic'),
708 '$eduline' => t('Underline'),
709 '$edquote' => t('Quote'),
710 '$edcode' => t('Code'),
711 '$edimg' => t('Image'),
712 '$edurl' => t('Link'),
713 '$edvideo' => t('Video'),
714 '$preview' => ((feature_enabled($conv->get_profile_owner(),'preview')) ? t('Preview') : ''),
715 '$indent' => $indent,
716 '$sourceapp' => t($a->sourcename),
717 '$ww' => (($conv->get_mode() === 'network') ? $ww : ''),
718 '$rand_num' => random_digits(12)
725 private function get_redirect_url() {
726 return $this->redirect_url;
730 * Check if we are a wall to wall item and set the relevant properties
732 protected function check_wall_to_wall() {
733 $a = $this->get_app();
734 $conv = $this->get_conversation();
735 $this->wall_to_wall = false;
737 if($this->is_toplevel()) {
738 if($conv->get_mode() !== 'profile') {
739 if($this->get_data_value('wall') && !$this->get_data_value('self')) {
740 // On the network page, I am the owner. On the display page it will be the profile owner.
741 // This will have been stored in $a->page_contact by our calling page.
742 // Put this person as the wall owner of the wall-to-wall notice.
744 $this->owner_url = zrl($a->page_contact['url']);
745 $this->owner_photo = $a->page_contact['thumb'];
746 $this->owner_name = $a->page_contact['name'];
747 $this->wall_to_wall = true;
748 } elseif($this->get_data_value('owner-link')) {
750 $owner_linkmatch = (($this->get_data_value('owner-link')) && link_compare($this->get_data_value('owner-link'),$this->get_data_value('author-link')));
751 $alias_linkmatch = (($this->get_data_value('alias')) && link_compare($this->get_data_value('alias'),$this->get_data_value('author-link')));
752 $owner_namematch = (($this->get_data_value('owner-name')) && $this->get_data_value('owner-name') == $this->get_data_value('author-name'));
754 if ((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
756 // The author url doesn't match the owner (typically the contact)
757 // and also doesn't match the contact alias.
758 // The name match is a hack to catch several weird cases where URLs are
759 // all over the park. It can be tricked, but this prevents you from
760 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
761 // well that it's the same Bob Smith.
763 // But it could be somebody else with the same name. It just isn't highly likely.
766 $this->owner_photo = $this->get_data_value('owner-avatar');
767 $this->owner_name = $this->get_data_value('owner-name');
768 $this->wall_to_wall = true;
769 // If it is our contact, use a friendly redirect link
770 if ((link_compare($this->get_data_value('owner-link'),$this->get_data_value('url')))
771 && ($this->get_data_value('network') === NETWORK_DFRN)) {
772 $this->owner_url = $this->get_redirect_url();
774 $this->owner_url = zrl($this->get_data_value('owner-link'));
781 if (!$this->wall_to_wall) {
782 $this->set_template('wall');
783 $this->owner_url = '';
784 $this->owner_photo = '';
785 $this->owner_name = '';
789 private function is_wall_to_wall() {
790 return $this->wall_to_wall;
793 private function get_owner_url() {
794 return $this->owner_url;
797 private function get_owner_photo() {
798 return $this->owner_photo;
801 private function get_owner_name() {
802 return $this->owner_name;
805 private function is_visiting() {
806 return $this->visiting;