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