]> git.mxchange.org Git - friendica.git/blob - src/Object/Post.php
Merge pull request #4682 from MrPetovan/task/4681-nsfw-add-hashtag-only-hiding
[friendica.git] / src / Object / Post.php
1 <?php
2 /**
3  * @file src/Object/Post.php
4  */
5 namespace Friendica\Object;
6
7 use Friendica\BaseObject;
8 use Friendica\Content\ContactSelector;
9 use Friendica\Content\Feature;
10 use Friendica\Core\Addon;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
14 use Friendica\Database\DBM;
15 use Friendica\Model\Contact;
16 use Friendica\Model\Profile;
17 use Friendica\Util\DateTimeFormat;
18 use Friendica\Util\Temporal;
19 use dba;
20
21 require_once 'include/dba.php';
22 require_once 'include/text.php';
23 require_once 'boot.php';
24 require_once 'include/conversation.php';
25
26 /**
27  * An item
28  */
29 class Post extends BaseObject
30 {
31         private $data = [];
32         private $template = null;
33         private $available_templates = [
34                 'wall' => 'wall_thread.tpl',
35                 'wall2wall' => 'wallwall_thread.tpl'
36         ];
37         private $comment_box_template = 'comment_item.tpl';
38         private $toplevel = false;
39         private $writable = false;
40         private $children = [];
41         private $parent = null;
42         private $thread = null;
43         private $redirect_url = null;
44         private $owner_url = '';
45         private $owner_photo = '';
46         private $owner_name = '';
47         private $wall_to_wall = false;
48         private $threaded = false;
49         private $visiting = false;
50
51         /**
52          * Constructor
53          *
54          * @param array $data data array
55          */
56         public function __construct(array $data)
57         {
58                 $a = self::getApp();
59
60                 $this->data = $data;
61                 $this->setTemplate('wall');
62                 $this->toplevel = $this->getId() == $this->getDataValue('parent');
63
64                 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
65                         foreach ($_SESSION['remote'] as $visitor) {
66                                 if ($visitor['cid'] == $this->getDataValue('contact-id')) {
67                                         $this->visiting = true;
68                                         break;
69                                 }
70                         }
71                 }
72
73                 $this->writable = $this->getDataValue('writable') || $this->getDataValue('self');
74                 $this->redirect_url = 'redir/' . $this->getDataValue('cid');
75
76                 if (!$this->isToplevel()) {
77                         $this->threaded = true;
78                 }
79
80                 // Prepare the children
81                 if (count($data['children'])) {
82                         foreach ($data['children'] as $item) {
83                                 // Only add will be displayed
84                                 if ($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
85                                         continue;
86                                 } elseif (!visible_activity($item)) {
87                                         continue;
88                                 }
89
90                                 // You can always comment on Diaspora and OStatus items
91                                 if (in_array($item['network'], [NETWORK_OSTATUS, NETWORK_DIASPORA]) && (local_user() == $item['uid'])) {
92                                         $item['writable'] = true;
93                                 }
94
95                                 $item['pagedrop'] = $data['pagedrop'];
96                                 $child = new Post($item);
97                                 $this->addChild($child);
98                         }
99                 }
100         }
101
102         /**
103          * Get data in a form usable by a conversation template
104          *
105          * @param object  $conv_responses conversation responses
106          * @param integer $thread_level   default = 1
107          *
108          * @return mixed The data requested on success
109          *               false on failure
110          */
111         public function getTemplateData($conv_responses, $thread_level = 1)
112         {
113                 require_once "mod/proxy.php";
114
115                 $result = [];
116
117                 $a = self::getApp();
118
119                 $item = $this->getData();
120                 $edited = false;
121                 // If the time between "created" and "edited" differs we add
122                 // a notice that the post was edited.
123                 // Note: In some networks reshared items seem to have (sometimes) a difference
124                 // between creation time and edit time of a second. Thats why we add the notice
125                 // only if the difference is more than 1 second.
126                 if (strtotime($item['edited']) - strtotime($item['created']) > 1) {
127                         $edited = [
128                                 'label'    => L10n::t('This entry was edited'),
129                                 'date'     => DateTimeFormat::local($item['edited'], 'r'),
130                                 'relative' => Temporal::getRelativeDate($item['edited'])
131                         ];
132                 }
133                 $commentww = '';
134                 $sparkle = '';
135                 $buttons = '';
136                 $dropping = false;
137                 $star = false;
138                 $ignore = false;
139                 $isstarred = "unstarred";
140                 $indent = '';
141                 $shiny = '';
142                 $osparkle = '';
143                 $total_children = $this->countDescendants();
144
145                 $conv = $this->getThread();
146
147                 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
148                         || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
149                         ? L10n::t('Private Message')
150                         : false);
151                 $shareable = in_array($conv->getProfileOwner(), [0, local_user()]) && $item['private'] != 1;
152
153                 if (local_user() && link_compare($a->contact['url'], $item['author-link'])) {
154                         if ($item["event-id"] != 0) {
155                                 $edpost = ["events/event/" . $item['event-id'], L10n::t("Edit")];
156                         } else {
157                                 $edpost = ["editpost/" . $item['id'], L10n::t("Edit")];
158                         }
159                         $dropping = in_array($item['uid'], [0, local_user()]);
160                 } else {
161                         $edpost = false;
162                 }
163
164                 // Editing on items of not subscribed users isn't currently possible
165                 // There are some issues on editing that prevent this.
166                 // But also it is an issue of the supported protocols that doesn't allow editing at all.
167                 if ($item['uid'] == 0) {
168                         $edpost = false;
169                 }
170
171                 if (($this->getDataValue('uid') == local_user()) || $this->isVisiting()) {
172                         $dropping = true;
173                 }
174
175                 $drop = [
176                         'dropping' => $dropping,
177                         'pagedrop' => ((Feature::isEnabled($conv->getProfileOwner(), 'multi_delete')) ? $item['pagedrop'] : ''),
178                         'select'   => L10n::t('Select'),
179                         'delete'   => L10n::t('Delete'),
180                 ];
181
182                 $filer = (($conv->getProfileOwner() == local_user() && ($item['uid'] != 0)) ? L10n::t("save to folder") : false);
183
184                 $diff_author = !link_compare($item['url'], $item['author-link']);
185                 $profile_name = htmlentities(((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
186                 if ($item['author-link'] && (!$item['author-name'])) {
187                         $profile_name = $item['author-link'];
188                 }
189
190                 $sp = false;
191                 $profile_link = best_link_url($item, $sp);
192                 if ($profile_link === 'mailbox') {
193                         $profile_link = '';
194                 }
195
196                 if ($sp) {
197                         $sparkle = ' sparkle';
198                 } else {
199                         $profile_link = Profile::zrl($profile_link);
200                 }
201
202                 if (!isset($item['author-thumb']) || ($item['author-thumb'] == "")) {
203                         $author_contact = Contact::getDetailsByURL($item['author-link'], $conv->getProfileOwner());
204                         if ($author_contact["thumb"]) {
205                                 $item['author-thumb'] = $author_contact["thumb"];
206                         } else {
207                                 $item['author-thumb'] = $item['author-avatar'];
208                         }
209                 }
210
211                 if (!isset($item['owner-thumb']) || ($item['owner-thumb'] == "")) {
212                         $owner_contact = Contact::getDetailsByURL($item['owner-link'], $conv->getProfileOwner());
213                         if ($owner_contact["thumb"]) {
214                                 $item['owner-thumb'] = $owner_contact["thumb"];
215                         } else {
216                                 $item['owner-thumb'] = $item['owner-avatar'];
217                         }
218                 }
219
220                 $locate = ['location' => $item['location'], 'coord' => $item['coord'], 'html' => ''];
221                 Addon::callHooks('render_location', $locate);
222                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_dummy($locate));
223
224                 // process action responses - e.g. like/dislike/attend/agree/whatever
225                 $response_verbs = ['like', 'dislike'];
226
227                 $isevent = false;
228                 $attend = [];
229                 if ($item['object-type'] === ACTIVITY_OBJ_EVENT) {
230                         $response_verbs[] = 'attendyes';
231                         $response_verbs[] = 'attendno';
232                         $response_verbs[] = 'attendmaybe';
233                         if ($conv->isWritable()) {
234                                 $isevent = true;
235                                 $attend = [L10n::t('I will attend'), L10n::t('I will not attend'), L10n::t('I might attend')];
236                         }
237                 }
238
239                 $responses = get_responses($conv_responses, $response_verbs, $this, $item);
240
241                 foreach ($response_verbs as $value => $verbs) {
242                         $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']) : '';
243                 }
244
245                 /*
246                  * We should avoid doing this all the time, but it depends on the conversation mode
247                  * And the conv mode may change when we change the conv, or it changes its mode
248                  * Maybe we should establish a way to be notified about conversation changes
249                  */
250                 $this->checkWallToWall();
251
252                 if ($this->isWallToWall() && ($this->getOwnerUrl() == $this->getRedirectUrl())) {
253                         $osparkle = ' sparkle';
254                 }
255
256                 $tagger = '';
257
258                 if ($this->isToplevel()) {
259                         if ($conv->getProfileOwner() == local_user() && ($item['uid'] != 0)) {
260                                 $isstarred = (($item['starred']) ? "starred" : "unstarred");
261
262                                 $star = [
263                                         'do'        => L10n::t("add star"),
264                                         'undo'      => L10n::t("remove star"),
265                                         'toggle'    => L10n::t("toggle star status"),
266                                         'classdo'   => $item['starred'] ? "hidden" : "",
267                                         'classundo' => $item['starred'] ? "" : "hidden",
268                                         'starred'   => L10n::t('starred'),
269                                 ];
270
271                                 $thread = dba::selectFirst('thread', ['ignored'], ['uid' => $item['uid'], 'iid' => $item['id']]);
272                                 if (DBM::is_result($thread)) {
273                                         $ignore = [
274                                                 'do'        => L10n::t("ignore thread"),
275                                                 'undo'      => L10n::t("unignore thread"),
276                                                 'toggle'    => L10n::t("toggle ignore status"),
277                                                 'classdo'   => $thread['ignored'] ? "hidden" : "",
278                                                 'classundo' => $thread['ignored'] ? "" : "hidden",
279                                                 'ignored'   => L10n::t('ignored'),
280                                         ];
281                                 }
282
283                                 if (Feature::isEnabled($conv->getProfileOwner(), 'commtag')) {
284                                         $tagger = [
285                                                 'add'   => L10n::t("add tag"),
286                                                 'class' => "",
287                                         ];
288                                 }
289                         }
290                 } else {
291                         $indent = 'comment';
292                 }
293
294                 if ($conv->isWritable()) {
295                         $buttons = [
296                                 'like'    => [L10n::t("I like this \x28toggle\x29"), L10n::t("like")],
297                                 'dislike' => Feature::isEnabled($conv->getProfileOwner(), 'dislike') ? [L10n::t("I don't like this \x28toggle\x29"), L10n::t("dislike")] : '',
298                         ];
299                         if ($shareable) {
300                                 $buttons['share'] = [L10n::t('Share this'), L10n::t('share')];
301                         }
302                 }
303
304                 $comment = $this->getCommentBox($indent);
305
306                 if (strcmp(DateTimeFormat::utc($item['created']), DateTimeFormat::utc('now - 12 hours')) > 0) {
307                         $shiny = 'shiny';
308                 }
309
310                 localize_item($item);
311
312                 $body = prepare_body($item, true);
313
314                 list($categories, $folders) = get_cats_and_terms($item);
315
316                 $body_e       = $body;
317                 $text_e       = strip_tags($body);
318                 $name_e       = $profile_name;
319                 $title_e      = $item['title'];
320                 $location_e   = $location;
321                 $owner_name_e = $this->getOwnerName();
322
323                 // Disable features that aren't available in several networks
324                 if (!in_array($item["item_network"], [NETWORK_DFRN, NETWORK_DIASPORA]) && isset($buttons["dislike"])) {
325                         unset($buttons["dislike"]);
326                         $isevent = false;
327                         $tagger = '';
328                 }
329
330                 if (($item["item_network"] == NETWORK_FEED) && isset($buttons["like"])) {
331                         unset($buttons["like"]);
332                 }
333
334                 if (($item["item_network"] == NETWORK_MAIL) && isset($buttons["like"])) {
335                         unset($buttons["like"]);
336                 }
337
338                 $tmp_item = [
339                         'template'        => $this->getTemplate(),
340                         'type'            => implode("", array_slice(explode("/", $item['verb']), -1)),
341                         'suppress_tags'   => Config::get('system', 'suppress_tags'),
342                         'tags'            => $item['tags'],
343                         'hashtags'        => $item['hashtags'],
344                         'mentions'        => $item['mentions'],
345                         'txt_cats'        => L10n::t('Categories:'),
346                         'txt_folders'     => L10n::t('Filed under:'),
347                         'has_cats'        => ((count($categories)) ? 'true' : ''),
348                         'has_folders'     => ((count($folders)) ? 'true' : ''),
349                         'categories'      => $categories,
350                         'folders'         => $folders,
351                         'body'            => $body_e,
352                         'text'            => $text_e,
353                         'id'              => $this->getId(),
354                         'guid'            => urlencode($item['guid']),
355                         'isevent'         => $isevent,
356                         'attend'          => $attend,
357                         'linktitle'       => L10n::t('View %s\'s profile @ %s', $profile_name, defaults($item, 'author-link', $item['url'])),
358                         'olinktitle'      => L10n::t('View %s\'s profile @ %s', htmlentities($this->getOwnerName()), defaults($item, 'owner-link', $item['url'])),
359                         'to'              => L10n::t('to'),
360                         'via'             => L10n::t('via'),
361                         'wall'            => L10n::t('Wall-to-Wall'),
362                         'vwall'           => L10n::t('via Wall-To-Wall:'),
363                         'profile_url'     => $profile_link,
364                         'item_photo_menu' => item_photo_menu($item),
365                         'name'            => $name_e,
366                         'thumb'           => $a->remove_baseurl(proxy_url($item['author-thumb'], false, PROXY_SIZE_THUMB)),
367                         'osparkle'        => $osparkle,
368                         'sparkle'         => $sparkle,
369                         'title'           => $title_e,
370                         'localtime'       => DateTimeFormat::local($item['created'], 'r'),
371                         'ago'             => $item['app'] ? L10n::t('%s from %s', Temporal::getRelativeDate($item['created']), $item['app']) : Temporal::getRelativeDate($item['created']),
372                         'app'             => $item['app'],
373                         'created'         => Temporal::getRelativeDate($item['created']),
374                         'lock'            => $lock,
375                         'location'        => $location_e,
376                         'indent'          => $indent,
377                         'shiny'           => $shiny,
378                         'owner_url'       => $this->getOwnerUrl(),
379                         'owner_photo'     => $a->remove_baseurl(proxy_url($item['owner-thumb'], false, PROXY_SIZE_THUMB)),
380                         'owner_name'      => htmlentities($owner_name_e),
381                         'plink'           => get_plink($item),
382                         'edpost'          => Feature::isEnabled($conv->getProfileOwner(), 'edit_posts') ? $edpost : '',
383                         'isstarred'       => $isstarred,
384                         'star'            => Feature::isEnabled($conv->getProfileOwner(), 'star_posts') ? $star : '',
385                         'ignore'          => Feature::isEnabled($conv->getProfileOwner(), 'ignore_posts') ? $ignore : '',
386                         'tagger'          => $tagger,
387                         'filer'           => Feature::isEnabled($conv->getProfileOwner(), 'filing') ? $filer : '',
388                         'drop'            => $drop,
389                         'vote'            => $buttons,
390                         'like'            => $responses['like']['output'],
391                         'dislike'         => $responses['dislike']['output'],
392                         'responses'       => $responses,
393                         'switchcomment'   => L10n::t('Comment'),
394                         'comment'         => $comment,
395                         'previewing'      => $conv->isPreview() ? ' preview ' : '',
396                         'wait'            => L10n::t('Please wait'),
397                         'thread_level'    => $thread_level,
398                         'edited'          => $edited,
399                         'network'         => $item["item_network"],
400                         'network_name'    => ContactSelector::networkToName($item['item_network'], $profile_link),
401                         'received'        => $item['received'],
402                         'commented'       => $item['commented'],
403                         'created_date'    => $item['created'],
404                 ];
405
406                 $arr = ['item' => $item, 'output' => $tmp_item];
407                 Addon::callHooks('display_item', $arr);
408
409                 $result = $arr['output'];
410
411                 $result['children'] = [];
412                 $children = $this->getChildren();
413                 $nb_children = count($children);
414                 if ($nb_children > 0) {
415                         foreach ($children as $child) {
416                                 $result['children'][] = $child->getTemplateData($conv_responses, $thread_level + 1);
417                         }
418                         // Collapse
419                         if (($nb_children > 2) || ($thread_level > 1)) {
420                                 $result['children'][0]['comment_firstcollapsed'] = true;
421                                 $result['children'][0]['num_comments'] = L10n::tt('%d comment', '%d comments', $total_children);
422                                 $result['children'][0]['hidden_comments_num'] = $total_children;
423                                 $result['children'][0]['hidden_comments_text'] = L10n::tt('comment', 'comments', $total_children);
424                                 $result['children'][0]['hide_text'] = L10n::t('show more');
425                                 if ($thread_level > 1) {
426                                         $result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
427                                 } else {
428                                         $result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
429                                 }
430                         }
431                 }
432
433                 if ($this->isToplevel()) {
434                         $result['total_comments_num'] = "$total_children";
435                         $result['total_comments_text'] = L10n::tt('comment', 'comments', $total_children);
436                 }
437
438                 $result['private'] = $item['private'];
439                 $result['toplevel'] = ($this->isToplevel() ? 'toplevel_item' : '');
440
441                 if ($this->isThreaded()) {
442                         $result['flatten'] = false;
443                         $result['threaded'] = true;
444                 } else {
445                         $result['flatten'] = true;
446                         $result['threaded'] = false;
447                 }
448
449                 return $result;
450         }
451
452         /**
453          * @return integer
454          */
455         public function getId()
456         {
457                 return $this->getDataValue('id');
458         }
459
460         /**
461          * @return boolean
462          */
463         public function isThreaded()
464         {
465                 return $this->threaded;
466         }
467
468         /**
469          * Add a child item
470          *
471          * @param object $item The child item to add
472          *
473          * @return mixed
474          */
475         public function addChild(Post $item)
476         {
477                 $item_id = $item->getId();
478                 if (!$item_id) {
479                         logger('[ERROR] Post::addChild : Item has no ID!!', LOGGER_DEBUG);
480                         return false;
481                 } elseif ($this->getChild($item->getId())) {
482                         logger('[WARN] Post::addChild : Item already exists (' . $item->getId() . ').', LOGGER_DEBUG);
483                         return false;
484                 }
485                 /*
486                  * Only add what will be displayed
487                  */
488                 if ($item->getDataValue('network') === NETWORK_MAIL && local_user() != $item->getDataValue('uid')) {
489                         return false;
490                 } elseif (activity_match($item->getDataValue('verb'), ACTIVITY_LIKE) || activity_match($item->getDataValue('verb'), ACTIVITY_DISLIKE)) {
491                         return false;
492                 }
493
494                 $item->setParent($this);
495                 $this->children[] = $item;
496
497                 return end($this->children);
498         }
499
500         /**
501          * Get a child by its ID
502          *
503          * @param integer $id The child id
504          *
505          * @return mixed
506          */
507         public function getChild($id)
508         {
509                 foreach ($this->getChildren() as $child) {
510                         if ($child->getId() == $id) {
511                                 return $child;
512                         }
513                 }
514
515                 return null;
516         }
517
518         /**
519          * Get all our children
520          *
521          * @return object
522          */
523         public function getChildren()
524         {
525                 return $this->children;
526         }
527
528         /**
529          * Set our parent
530          *
531          * @param object $item The item to set as parent
532          *
533          * @return void
534          */
535         protected function setParent($item)
536         {
537                 $parent = $this->getParent();
538                 if ($parent) {
539                         $parent->removeChild($this);
540                 }
541
542                 $this->parent = $item;
543                 $this->setThread($item->getThread());
544         }
545
546         /**
547          * Remove our parent
548          *
549          * @return void
550          */
551         protected function removeParent()
552         {
553                 $this->parent = null;
554                 $this->thread = null;
555         }
556
557         /**
558          * Remove a child
559          *
560          * @param object $item The child to be removed
561          *
562          * @return boolean Success or failure
563          */
564         public function removeChild($item)
565         {
566                 $id = $item->getId();
567                 foreach ($this->getChildren() as $key => $child) {
568                         if ($child->getId() == $id) {
569                                 $child->removeParent();
570                                 unset($this->children[$key]);
571                                 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
572                                 $this->children = array_values($this->children);
573                                 return true;
574                         }
575                 }
576                 logger('[WARN] Item::removeChild : Item is not a child (' . $id . ').', LOGGER_DEBUG);
577                 return false;
578         }
579
580         /**
581          * Get parent item
582          *
583          * @return object
584          */
585         protected function getParent()
586         {
587                 return $this->parent;
588         }
589
590         /**
591          * Set conversation
592          *
593          * @param object $conv The conversation
594          *
595          * @return void
596          */
597         public function setThread($conv)
598         {
599                 $previous_mode = ($this->thread ? $this->thread->getMode() : '');
600
601                 $this->thread = $conv;
602
603                 // Set it on our children too
604                 foreach ($this->getChildren() as $child) {
605                         $child->setThread($conv);
606                 }
607         }
608
609         /**
610          * Get conversation
611          *
612          * @return object
613          */
614         public function getThread()
615         {
616                 return $this->thread;
617         }
618
619         /**
620          * Get raw data
621          *
622          * We shouldn't need this
623          *
624          * @return array
625          */
626         public function getData()
627         {
628                 return $this->data;
629         }
630
631         /**
632          * Get a data value
633          *
634          * @param object $name key
635          *
636          * @return mixed value on success
637          *               false on failure
638          */
639         public function getDataValue($name)
640         {
641                 if (!isset($this->data[$name])) {
642                         // logger('[ERROR] Item::getDataValue : Item has no value name "'. $name .'".', LOGGER_DEBUG);
643                         return false;
644                 }
645
646                 return $this->data[$name];
647         }
648
649         /**
650          * Set template
651          *
652          * @param object $name template name
653          *
654          * @return void
655          */
656         private function setTemplate($name)
657         {
658                 if (!x($this->available_templates, $name)) {
659                         logger('[ERROR] Item::setTemplate : Template not available ("' . $name . '").', LOGGER_DEBUG);
660                         return false;
661                 }
662
663                 $this->template = $this->available_templates[$name];
664         }
665
666         /**
667          * Get template
668          *
669          * @return object
670          */
671         private function getTemplate()
672         {
673                 return $this->template;
674         }
675
676         /**
677          * Check if this is a toplevel post
678          *
679          * @return boolean
680          */
681         private function isToplevel()
682         {
683                 return $this->toplevel;
684         }
685
686         /**
687          * Check if this is writable
688          *
689          * @return boolean
690          */
691         private function isWritable()
692         {
693                 $conv = $this->getThread();
694
695                 if ($conv) {
696                         // This will allow us to comment on wall-to-wall items owned by our friends
697                         // and community forums even if somebody else wrote the post.
698                         // bug #517 - this fixes for conversation owner
699                         if ($conv->getMode() == 'profile' && $conv->getProfileOwner() == local_user()) {
700                                 return true;
701                         }
702
703                         // this fixes for visitors
704                         return ($this->writable || ($this->isVisiting() && $conv->getMode() == 'profile'));
705                 }
706                 return $this->writable;
707         }
708
709         /**
710          * Count the total of our descendants
711          *
712          * @return integer
713          */
714         private function countDescendants()
715         {
716                 $children = $this->getChildren();
717                 $total = count($children);
718                 if ($total > 0) {
719                         foreach ($children as $child) {
720                                 $total += $child->countDescendants();
721                         }
722                 }
723
724                 return $total;
725         }
726
727         /**
728          * Get the template for the comment box
729          *
730          * @return string
731          */
732         private function getCommentBoxTemplate()
733         {
734                 return $this->comment_box_template;
735         }
736
737         /**
738          * Get the comment box
739          *
740          * @param string $indent Indent value
741          *
742          * @return mixed The comment box string (empty if no comment box)
743          *               false on failure
744          */
745         private function getCommentBox($indent)
746         {
747                 $a = self::getApp();
748
749                 $comment_box = '';
750                 $conv = $this->getThread();
751                 $ww = '';
752                 if (($conv->getMode() === 'network') && $this->isWallToWall()) {
753                         $ww = 'ww';
754                 }
755
756                 if ($conv->isWritable() && $this->isWritable()) {
757                         $qc = $qcomment = null;
758
759                         /*
760                          * Hmmm, code depending on the presence of a particular addon?
761                          * This should be better if done by a hook
762                          */
763                         if (in_array('qcomment', $a->addons)) {
764                                 $qc = ((local_user()) ? PConfig::get(local_user(), 'qcomment', 'words') : null);
765                                 $qcomment = (($qc) ? explode("\n", $qc) : null);
766                         }
767
768                         // Fetch the user id from the parent when the owner user is empty
769                         $uid = $conv->getProfileOwner();
770                         $parent_uid = $this->getDataValue('uid');
771
772                         if (!is_null($parent_uid) && ($uid != $parent_uid)) {
773                                 $uid = $parent_uid;
774                         }
775
776                         $template = get_markup_template($this->getCommentBoxTemplate());
777                         $comment_box = replace_macros($template, [
778                                 '$return_path' => $a->query_string,
779                                 '$threaded'    => $this->isThreaded(),
780                                 '$jsreload'    => '',
781                                 '$type'        => $conv->getMode() === 'profile' ? 'wall-comment' : 'net-comment',
782                                 '$id'          => $this->getId(),
783                                 '$parent'      => $this->getId(),
784                                 '$qcomment'    => $qcomment,
785                                 '$profile_uid' => $uid,
786                                 '$mylink'      => $a->remove_baseurl($a->contact['url']),
787                                 '$mytitle'     => L10n::t('This is you'),
788                                 '$myphoto'     => $a->remove_baseurl($a->contact['thumb']),
789                                 '$comment'     => L10n::t('Comment'),
790                                 '$submit'      => L10n::t('Submit'),
791                                 '$edbold'      => L10n::t('Bold'),
792                                 '$editalic'    => L10n::t('Italic'),
793                                 '$eduline'     => L10n::t('Underline'),
794                                 '$edquote'     => L10n::t('Quote'),
795                                 '$edcode'      => L10n::t('Code'),
796                                 '$edimg'       => L10n::t('Image'),
797                                 '$edurl'       => L10n::t('Link'),
798                                 '$edvideo'     => L10n::t('Video'),
799                                 '$preview'     => ((Feature::isEnabled($conv->getProfileOwner(), 'preview')) ? L10n::t('Preview') : ''),
800                                 '$indent'      => $indent,
801                                 '$sourceapp'   => L10n::t($a->sourcename),
802                                 '$ww'          => $conv->getMode() === 'network' ? $ww : '',
803                                 '$rand_num'    => random_digits(12)
804                         ]);
805                 }
806
807                 return $comment_box;
808         }
809
810         /**
811          * @return string
812          */
813         private function getRedirectUrl()
814         {
815                 return $this->redirect_url;
816         }
817
818         /**
819          * Check if we are a wall to wall item and set the relevant properties
820          *
821          * @return void
822          */
823         protected function checkWallToWall()
824         {
825                 $a = self::getApp();
826                 $conv = $this->getThread();
827                 $this->wall_to_wall = false;
828
829                 if ($this->isToplevel()) {
830                         if ($conv->getMode() !== 'profile') {
831                                 if ($this->getDataValue('wall') && !$this->getDataValue('self')) {
832                                         // On the network page, I am the owner. On the display page it will be the profile owner.
833                                         // This will have been stored in $a->page_contact by our calling page.
834                                         // Put this person as the wall owner of the wall-to-wall notice.
835
836                                         $this->owner_url = Profile::zrl($a->page_contact['url']);
837                                         $this->owner_photo = $a->page_contact['thumb'];
838                                         $this->owner_name = $a->page_contact['name'];
839                                         $this->wall_to_wall = true;
840                                 } elseif ($this->getDataValue('owner-link')) {
841                                         $owner_linkmatch = (($this->getDataValue('owner-link')) && link_compare($this->getDataValue('owner-link'), $this->getDataValue('author-link')));
842                                         $alias_linkmatch = (($this->getDataValue('alias')) && link_compare($this->getDataValue('alias'), $this->getDataValue('author-link')));
843                                         $owner_namematch = (($this->getDataValue('owner-name')) && $this->getDataValue('owner-name') == $this->getDataValue('author-name'));
844
845                                         if ((!$owner_linkmatch) && (!$alias_linkmatch) && (!$owner_namematch)) {
846                                                 // The author url doesn't match the owner (typically the contact)
847                                                 // and also doesn't match the contact alias.
848                                                 // The name match is a hack to catch several weird cases where URLs are
849                                                 // all over the park. It can be tricked, but this prevents you from
850                                                 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
851                                                 // well that it's the same Bob Smith.
852                                                 // But it could be somebody else with the same name. It just isn't highly likely.
853
854
855                                                 $this->owner_photo = $this->getDataValue('owner-avatar');
856                                                 $this->owner_name = $this->getDataValue('owner-name');
857                                                 $this->wall_to_wall = true;
858                                                 // If it is our contact, use a friendly redirect link
859                                                 if ($this->getDataValue('network') === NETWORK_DFRN
860                                                         && link_compare($this->getDataValue('owner-link'), $this->getDataValue('url'))
861                                                 ) {
862                                                         $this->owner_url = $this->getRedirectUrl();
863                                                 } else {
864                                                         $this->owner_url = Profile::zrl($this->getDataValue('owner-link'));
865                                                 }
866                                         }
867                                 }
868                         }
869                 }
870
871                 if (!$this->wall_to_wall) {
872                         $this->setTemplate('wall');
873                         $this->owner_url = '';
874                         $this->owner_photo = '';
875                         $this->owner_name = '';
876                 }
877         }
878
879         /**
880          * @return boolean
881          */
882         private function isWallToWall()
883         {
884                 return $this->wall_to_wall;
885         }
886
887         /**
888          * @return string
889          */
890         private function getOwnerUrl()
891         {
892                 return $this->owner_url;
893         }
894
895         /**
896          * @return string
897          */
898         private function getOwnerName()
899         {
900                 return $this->owner_name;
901         }
902
903         /**
904          * @return boolean
905          */
906         private function isVisiting()
907         {
908                 return $this->visiting;
909         }
910 }