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