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