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