]> git.mxchange.org Git - friendica.git/blob - src/Object/Post.php
Improve goaway legibility
[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\Feature;
9 use Friendica\Core\PConfig;
10 use Friendica\Database\DBM;
11 use Friendica\Model\Contact;
12 use dba;
13
14 require_once 'include/dba.php';
15 require_once 'include/text.php';
16 require_once 'boot.php';
17 require_once 'include/conversation.php';
18
19 /**
20  * An item
21  */
22 class Post extends BaseObject
23 {
24         private $data = array();
25         private $template = null;
26         private $available_templates = array(
27                 'wall' => 'wall_thread.tpl',
28                 'wall2wall' => 'wallwall_thread.tpl'
29         );
30         private $comment_box_template = 'comment_item.tpl';
31         private $toplevel = false;
32         private $writable = false;
33         private $children = array();
34         private $parent = null;
35         private $thread = null;
36         private $redirect_url = null;
37         private $owner_url = '';
38         private $owner_photo = '';
39         private $owner_name = '';
40         private $wall_to_wall = false;
41         private $threaded = false;
42         private $visiting = false;
43
44         /**
45          * Constructor
46          *
47          * @param array $data data array
48          */
49         public function __construct(array $data)
50         {
51                 $a = self::getApp();
52
53                 $this->data = $data;
54                 $this->setTemplate('wall');
55                 $this->toplevel = ($this->getId() == $this->getDataValue('parent'));
56
57                 if (is_array($_SESSION['remote'])) {
58                         foreach ($_SESSION['remote'] as $visitor) {
59                                 if ($visitor['cid'] == $this->getDataValue('contact-id')) {
60                                         $this->visiting = true;
61                                         break;
62                                 }
63                         }
64                 }
65
66                 $this->writable = ($this->getDataValue('writable') || $this->getDataValue('self'));
67
68                 $ssl_state = ((local_user()) ? true : false);
69                 $this->redirect_url = 'redir/' . $this->getDataValue('cid');
70
71                 if (!$this->isToplevel()) {
72                         $this->threaded = true;
73                 }
74
75                 // Prepare the children
76                 if (count($data['children'])) {
77                         foreach ($data['children'] as $item) {
78                                 /*
79                                  * Only add will be displayed
80                                  */
81                                 if ($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
82                                         continue;
83                                 } elseif (! visible_activity($item)) {
84                                         continue;
85                                 }
86
87                                 // You can always comment on Diaspora items
88                                 if (($item['network'] == NETWORK_DIASPORA) && (local_user() == $item['uid'])) {
89                                         $item['writable'] = true;
90                                 }
91
92                                 $item['pagedrop'] = $data['pagedrop'];
93                                 $child = new Post($item);
94                                 $this->addChild($child);
95                         }
96                 }
97         }
98
99         /**
100          * Get data in a form usable by a conversation template
101          *
102          * @param object  $conv_responses conversation responses
103          * @param integer $thread_level   default = 1
104          *
105          * @return mixed The data requested on success
106          *               false on failure
107          */
108         public function getTemplateData($conv_responses, $thread_level = 1)
109         {
110                 require_once "mod/proxy.php";
111
112                 $result = array();
113
114                 $a = self::getApp();
115
116                 $item = $this->getData();
117                 $edited = false;
118                 // If the time between "created" and "edited" differs we add
119                 // a notice that the post was edited.
120                 // Note: In some networks reshared items seem to have (sometimes) a difference
121                 // between creation time and edit time of a second. Thats why we add the notice
122                 // only if the difference is more than 1 second.
123                 if (strtotime($item['edited']) - strtotime($item['created']) > 1) {
124                         $edited = array(
125                                 'label'    => t('This entry was edited'),
126                                 'date'     => datetime_convert('UTC', date_default_timezone_get(), $item['edited'], 'r'),
127                                 'relative' => relative_date($item['edited'])
128                         );
129                 }
130                 $commentww = '';
131                 $sparkle = '';
132                 $buttons = '';
133                 $dropping = false;
134                 $star = false;
135                 $ignore = false;
136                 $isstarred = "unstarred";
137                 $indent = '';
138                 $shiny = '';
139                 $osparkle = '';
140                 $total_children = $this->countDescendants();
141
142                 $conv = $this->getThread();
143
144                 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
145                         || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
146                         ? t('Private Message')
147                         : false);
148                 $shareable = ((in_array($conv->getProfileOwner(), [0, local_user()]) && ($item['private'] != 1)) ? true : false);
149
150                 if (local_user() && link_compare($a->contact['url'], $item['author-link'])) {
151                         if ($item["event-id"] != 0) {
152                                 $edpost = array("events/event/".$item['event-id'], t("Edit"));
153                         } else {
154                                 $edpost = array("editpost/".$item['id'], t("Edit"));
155                         }
156                 } else {
157                         $edpost = false;
158                 }
159
160                 if (($this->getDataValue('uid') == local_user()) || $this->isVisiting()) {
161                         $dropping = true;
162                 }
163
164                 $drop = array(
165                         'dropping' => $dropping,
166                         'pagedrop' => ((Feature::isEnabled($conv->getProfileOwner(), 'multi_delete')) ? $item['pagedrop'] : ''),
167                         'select'   => t('Select'),
168                         'delete'   => t('Delete'),
169                 );
170
171                 $filer = (($conv->getProfileOwner() == local_user()) ? t("save to folder") : false);
172
173                 $diff_author    = ((link_compare($item['url'], $item['author-link'])) ? false : true);
174                 $profile_name   = htmlentities(((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
175                 if ($item['author-link'] && (! $item['author-name'])) {
176                         $profile_name = $item['author-link'];
177                 }
178
179                 $sp = false;
180                 $profile_link = best_link_url($item, $sp);
181                 if ($profile_link === 'mailbox') {
182                         $profile_link = '';
183                 }
184
185                 if ($sp) {
186                         $sparkle = ' sparkle';
187                 } else {
188                         $profile_link = zrl($profile_link);
189                 }
190
191                 if (!isset($item['author-thumb']) || ($item['author-thumb'] == "")) {
192                         $author_contact = Contact::getDetailsByURL($item['author-link'], $conv->getProfileOwner());
193                         if ($author_contact["thumb"]) {
194                                 $item['author-thumb'] = $author_contact["thumb"];
195                         } else {
196                                 $item['author-thumb'] = $item['author-avatar'];
197                         }
198                 }
199
200                 if (!isset($item['owner-thumb']) || ($item['owner-thumb'] == "")) {
201                         $owner_contact = Contact::getDetailsByURL($item['owner-link'], $conv->getProfileOwner());
202                         if ($owner_contact["thumb"]) {
203                                 $item['owner-thumb'] = $owner_contact["thumb"];
204                         } else {
205                                 $item['owner-thumb'] = $item['owner-avatar'];
206                         }
207                 }
208
209                 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
210                 call_hooks('render_location', $locate);
211                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_dummy($locate));
212
213                 $tags=array();
214                 $hashtags = array();
215                 $mentions = array();
216
217                 /*foreach(explode(',',$item['tag']) as $tag){
218                         $tag = trim($tag);
219                         if ($tag!="") {
220                                 $t = bbcode($tag);
221                                 $tags[] = $t;
222                                 if($t[0] == '#')
223                                         $hashtags[] = $t;
224                                 elseif($t[0] == '@')
225                                         $mentions[] = $t;
226                         }
227                 }*/
228
229                 // process action responses - e.g. like/dislike/attend/agree/whatever
230                 $response_verbs = array('like', 'dislike');
231
232                 if ($item['object-type'] === ACTIVITY_OBJ_EVENT) {
233                         $response_verbs[] = 'attendyes';
234                         $response_verbs[] = 'attendno';
235                         $response_verbs[] = 'attendmaybe';
236                         if ($conv->isWritable()) {
237                                 $isevent = true;
238                                 $attend = array( t('I will attend'), t('I will not attend'), t('I might attend'));
239                         }
240                 }
241
242                 $responses = get_responses($conv_responses, $response_verbs, $this, $item);
243
244                 foreach ($response_verbs as $value => $verbs) {
245                         $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']) : '');
246                 }
247
248                 /*
249                  * We should avoid doing this all the time, but it depends on the conversation mode
250                  * And the conv mode may change when we change the conv, or it changes its mode
251                  * Maybe we should establish a way to be notified about conversation changes
252                  */
253                 $this->checkWallToWall();
254
255                 if ($this->isWallToWall() && ($this->getOwnerUrl() == $this->getRedirectUrl())) {
256                         $osparkle = ' sparkle';
257                 }
258
259                 if ($this->isToplevel()) {
260                         if ($conv->getProfileOwner() == local_user()) {
261                                 $isstarred = (($item['starred']) ? "starred" : "unstarred");
262
263                                 $star = array(
264                                         'do'        => t("add star"),
265                                         'undo'      => t("remove star"),
266                                         'toggle'    => t("toggle star status"),
267                                         'classdo'   => (($item['starred']) ? "hidden" : ""),
268                                         'classundo' => (($item['starred']) ? "" : "hidden"),
269                                         'starred'   =>  t('starred'),
270                                 );
271                                 $r = dba::select('thread', array('ignored'), array('uid' => $item['uid'], 'iid' => $item['id']), array('limit' => 1));
272                                 if (DBM::is_result($r)) {
273                                         $ignore = array(
274                                                 'do'        => t("ignore thread"),
275                                                 'undo'      => t("unignore thread"),
276                                                 'toggle'    => t("toggle ignore status"),
277                                                 'classdo'   => (($r['ignored']) ? "hidden" : ""),
278                                                 'classundo' => (($r['ignored']) ? "" : "hidden"),
279                                                 'ignored'   =>  t('ignored'),
280                                         );
281                                 }
282
283                                 $tagger = '';
284                                 if (Feature::isEnabled($conv->getProfileOwner(), 'commtag')) {
285                                         $tagger = array(
286                                                 'add'   => t("add tag"),
287                                                 'class' => "",
288                                         );
289                                 }
290                         }
291                 } else {
292                         $indent = 'comment';
293                 }
294
295                 if ($conv->isWritable()) {
296                         $buttons = array(
297                                 'like' => array( t("I like this \x28toggle\x29"), t("like")),
298                                 'dislike' => ((Feature::isEnabled($conv->getProfileOwner(), 'dislike')) ? array( t("I don't like this \x28toggle\x29"), t("dislike")) : ''),
299                         );
300                         if ($shareable) {
301                                 $buttons['share'] = array( t('Share this'), t('share'));
302                         }
303                 }
304
305                 $comment = $this->getCommentBox($indent);
306
307                 if (strcmp(datetime_convert('UTC', 'UTC', $item['created']), datetime_convert('UTC', 'UTC', 'now - 12 hours')) > 0) {
308                         $shiny = 'shiny';
309                 }
310
311                 localize_item($item);
312
313                 $body = prepare_body($item, true);
314
315                 list($categories, $folders) = get_cats_and_terms($item);
316
317                 $body_e       = $body;
318                 $text_e       = strip_tags($body);
319                 $name_e       = $profile_name;
320                 $title_e      = $item['title'];
321                 $location_e   = $location;
322                 $owner_name_e = $this->getOwnerName();
323
324                 // Disable features that aren't available in several networks
325
326                 /// @todo Add NETWORK_DIASPORA when it will pass this information
327                 if (!in_array($item["item_network"], array(NETWORK_DFRN)) && isset($buttons["dislike"])) {
328                         unset($buttons["dislike"], $isevent);
329                         $tagger = '';
330                 }
331
332                 if (($item["item_network"] == NETWORK_FEED) && isset($buttons["like"])) {
333                         unset($buttons["like"]);
334                 }
335
336                 if (($item["item_network"] == NETWORK_MAIL) && isset($buttons["like"])) {
337                         unset($buttons["like"]);
338                 }
339
340                 $tmp_item = array(
341                         'template'        => $this->getTemplate(),
342                         'type'            => implode("", array_slice(explode("/", $item['verb']), -1)),
343                         'tags'            => $item['tags'],
344                         'hashtags'        => $item['hashtags'],
345                         'mentions'        => $item['mentions'],
346                         'txt_cats'        => t('Categories:'),
347                         'txt_folders'     => 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'       => sprintf(t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
359                         'olinktitle'      => sprintf(t('View %s\'s profile @ %s'), htmlentities($this->getOwnerName()), ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
360                         'to'              => t('to'),
361                         'via'             => t('via'),
362                         'wall'            => t('Wall-to-Wall'),
363                         'vwall'           => 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-thumb'], false, PROXY_SIZE_THUMB)),
368                         'osparkle'        => $osparkle,
369                         'sparkle'         => $sparkle,
370                         'title'           => $title_e,
371                         'localtime'       => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
372                         'ago'             => (($item['app']) ? sprintf(t('%s from %s'), relative_date($item['created']), $item['app']) : relative_date($item['created'])),
373                         'app'             => $item['app'],
374                         'created'         => relative_date($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-thumb'], 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'   => t('Comment'),
395                         'comment'         => $comment,
396                         'previewing'      => ($conv->isPreview() ? ' preview ' : ''),
397                         'wait'            => t('Please wait'),
398                         'thread_level'    => $thread_level,
399                         'edited'          => $edited,
400                         'network'         => $item["item_network"],
401                         'network_name'    => network_to_name($item['item_network'], $profile_link),
402                         'received'        => $item['received'],
403                         'commented'       => $item['commented'],
404                         'created_date'    => $item['created'],
405                 );
406
407                 $arr = array('item' => $item, 'output' => $tmp_item);
408                 call_hooks('display_item', $arr);
409
410                 $result = $arr['output'];
411
412                 $result['children'] = array();
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'] = sprintf(tt('%d comment', '%d comments', $total_children), $total_children);
423                                 $result['children'][0]['hidden_comments_num'] = $total_children;
424                                 $result['children'][0]['hidden_comments_text'] = tt('comment', 'comments', $total_children);
425                                 $result['children'][0]['hide_text'] = 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'] = 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
700                         // bug #517 - this fixes for conversation owner
701                         if ($conv->getMode() == 'profile' && $conv->getProfileOwner() == local_user()) {
702                                 return true;
703                         }
704
705                         // this fixes for visitors
706                         return ($this->writable || ($this->isVisiting() && $conv->getMode() == 'profile'));
707                 }
708                 return $this->writable;
709         }
710
711         /**
712          * Count the total of our descendants
713          *
714          * @return integer
715          */
716         private function countDescendants()
717         {
718                 $children = $this->getChildren();
719                 $total = count($children);
720                 if ($total > 0) {
721                         foreach ($children as $child) {
722                                 $total += $child->countDescendants();
723                         }
724                 }
725
726                 return $total;
727         }
728
729         /**
730          * Get the template for the comment box
731          *
732          * @return string
733          */
734         private function getCommentBoxTemplate()
735         {
736                 return $this->comment_box_template;
737         }
738
739         /**
740          * Get the comment box
741          *
742          * @param string $indent Indent value
743          *
744          * @return mixed The comment box string (empty if no comment box)
745          *               false on failure
746          */
747         private function getCommentBox($indent)
748         {
749                 $a = self::getApp();
750
751                 $comment_box = '';
752                 $conv = $this->getThread();
753                 $template = get_markup_template($this->getCommentBoxTemplate());
754                 $ww = '';
755                 if (($conv->getMode() === 'network') && $this->isWallToWall()) {
756                         $ww = 'ww';
757                 }
758
759                 if ($conv->isWritable() && $this->isWritable()) {
760                         $qc = $qcomment =  null;
761
762                         /*
763                          * Hmmm, code depending on the presence of a particular plugin?
764                          * This should be better if done by a hook
765                          */
766                         if (in_array('qcomment', $a->plugins)) {
767                                 $qc = ((local_user()) ? PConfig::get(local_user(), 'qcomment', 'words') : null);
768                                 $qcomment = (($qc) ? explode("\n", $qc) : null);
769                         }
770
771                         $comment_box = replace_macros(
772                                 $template,
773                                 array(
774                                 '$return_path' => $a->query_string,
775                                 '$threaded'    => $this->isThreaded(),
776                                 // '$jsreload'    => (($conv->getMode() === 'display') ? $_SESSION['return_url'] : ''),
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' =>  $conv->getProfileOwner(),
783                                 '$mylink'      => $a->remove_baseurl($a->contact['url']),
784                                 '$mytitle'     => t('This is you'),
785                                 '$myphoto'     => $a->remove_baseurl($a->contact['thumb']),
786                                 '$comment'     => t('Comment'),
787                                 '$submit'      => t('Submit'),
788                                 '$edbold'      => t('Bold'),
789                                 '$editalic'    => t('Italic'),
790                                 '$eduline'     => t('Underline'),
791                                 '$edquote'     => t('Quote'),
792                                 '$edcode'      => t('Code'),
793                                 '$edimg'       => t('Image'),
794                                 '$edurl'       => t('Link'),
795                                 '$edvideo'     => t('Video'),
796                                 '$preview'     => ((Feature::isEnabled($conv->getProfileOwner(), 'preview')) ? t('Preview') : ''),
797                                 '$indent'      => $indent,
798                                 '$sourceapp'   => 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 = 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
850                                                 // But it could be somebody else with the same name. It just isn't highly likely.
851
852
853                                                 $this->owner_photo = $this->getDataValue('owner-avatar');
854                                                 $this->owner_name = $this->getDataValue('owner-name');
855                                                 $this->wall_to_wall = true;
856                                                 // If it is our contact, use a friendly redirect link
857                                                 if ((link_compare($this->getDataValue('owner-link'), $this->getDataValue('url')))
858                                                         && ($this->getDataValue('network') === NETWORK_DFRN)
859                                                 ) {
860                                                         $this->owner_url = $this->getRedirectUrl();
861                                                 } else {
862                                                         $this->owner_url = zrl($this->getDataValue('owner-link'));
863                                                 }
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 }