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