diff --git a/includes/services/CommentService.php b/includes/services/CommentService.php index 49b09cbc3..1834ad22d 100644 --- a/includes/services/CommentService.php +++ b/includes/services/CommentService.php @@ -90,7 +90,7 @@ public function addCommentIfAuthorized($content, $idComment = '') } else { $num = "1"; } - $idComment = "Comment".$num; + $idComment = "Comment" . $num; } else { $newComment = false; } @@ -118,12 +118,19 @@ public function addCommentIfAuthorized($content, $idComment = '') $com['tag'] = $comment['tag']; $com['commentOn'] = $comment['comment_on']; $com['rawbody'] = $comment['body']; - $com['body'] = $this->wiki->Format($comment['body']); + // Do the page change in any case (useful for attach or grid) + $oldPage = $GLOBALS['wiki']->GetPageTag(); + $oldPageArray = $GLOBALS['wiki']->page; + $GLOBALS['wiki']->tag = $comment['tag']; + $GLOBALS['wiki']->page = $comment; + $com['body'] = $GLOBALS['wiki']->Format($comment['body']); + $GLOBALS['wiki']->tag = $oldPage; + $GLOBALS['wiki']->page = $oldPageArray; $this->setUserData($comment, 'user', $com); $this->setUserData($comment, 'owner', $com); - $com['date'] = 'le '.date("d.m.Y à H:i:s", strtotime($comment['time'])); + $com['date'] = 'le ' . date("d.m.Y à H:i:s", strtotime($comment['time'])); if ($this->wiki->HasAccess('comment', $comment['tag'])) { - $com['linkcomment'] = $this->wiki->href('pages/'.$comment['tag'].'/comments', 'api'); + $com['linkcomment'] = $this->wiki->href('pages/' . $comment['tag'] . '/comments', 'api'); } if ($this->wiki->UserIsOwner($comment['tag']) || $this->wiki->UserIsAdmin()) { $com['linkeditcomment'] = $this->wiki->href('edit', $comment['tag']); @@ -133,9 +140,9 @@ public function addCommentIfAuthorized($content, $idComment = '') $com['reponses'] = $this->getCommentList($comment['tag'], false); $com['parentPage'] = $this->getParentPage($comment['tag']); $errors = $this->eventDispatcher->yesWikiDispatch($newComment ? 'comment.created' : 'comment.updated', [ - 'id' => $com['tag'], - 'data' => $com, - ]); + 'id' => $com['tag'], + 'data' => $com, + ]); return [ 'code' => 200, 'success' => _t('COMMENT_PUBLISHED'), @@ -167,22 +174,22 @@ public function delete(string $commentTag): array $parentPage = $this->getParentPage($commentTag); $this->pageManager->deleteOrphaned($commentTag); $errors = $this->eventDispatcher->yesWikiDispatch('comment.deleted', [ - 'id' => $comment['tag'], - 'data' => array_merge($comment, [ - 'associatedComments' => $comments, - 'parentPage' => $parentPage - ]) - ]); + 'id' => $comment['tag'], + 'data' => array_merge($comment, [ + 'associatedComments' => $comments, + 'parentPage' => $parentPage + ]) + ]); return $errors; } /** - * Load comments for given page. - * - * @param string $tag Page name (Ex : "PagePrincipale") if empty, all comments - * @param bool $bypassAcls - * @return array All comments and their corresponding properties. - */ + * Load comments for given page. + * + * @param string $tag Page name (Ex : "PagePrincipale") if empty, all comments + * @param bool $bypassAcls + * @return array All comments and their corresponding properties. + */ public function loadComments($tag, bool $bypassAcls = false) { $query = 'SELECT * FROM ' . $this->wiki->config['table_prefix'] . 'pages ' . 'WHERE '; @@ -193,7 +200,7 @@ public function loadComments($tag, bool $bypassAcls = false) } if (!empty($username)) { $query .= - <<wiki->Format($comment['body']); $this->setUserData($comment, 'user', $com['comments'][$i]); $this->setUserData($comment, 'owner', $com['comments'][$i]); - $com['comments'][$i]['date'] = 'le '.date("d.m.Y à H:i:s", strtotime($comment['time'])); + $com['comments'][$i]['date'] = 'le ' . date("d.m.Y à H:i:s", strtotime($comment['time'])); if ($this->wiki->HasAccess('comment', $comment['tag'])) { - $com['comments'][$i]['linkcomment'] = $this->wiki->href('pages/'.$comment['tag'].'/comments', 'api'); + $com['comments'][$i]['linkcomment'] = $this->wiki->href('pages/' . $comment['tag'] . '/comments', 'api'); } if ($this->wiki->UserIsOwner($comment['tag']) || $this->wiki->UserIsAdmin()) { $com['comments'][$i]['linkeditcomment'] = $this->wiki->href('edit', $comment['tag']); - $com['comments'][$i]['linkdeletecomment'] = $this->wiki->href('comments/'.$comment['tag'].'/delete', 'api'); + $com['comments'][$i]['linkdeletecomment'] = $this->wiki->href('comments/' . $comment['tag'] . '/delete', 'api'); } $com['comments'][$i]['reponses'] = $this->getCommentList($comment['tag'], false); } @@ -251,14 +258,14 @@ public function getCommentList($tag, $first = true, $comments = null) private function setUserData(array $comment, string $key, array &$data) { - if (in_array($key, ['user','owner'], true) && !empty($comment[$key])) { + if (in_array($key, ['user', 'owner'], true) && !empty($comment[$key])) { $data[$key] = $comment[$key]; $data["link$key"] = $this->wiki->href('', $comment[$key]); $data["{$key}color"] = $this->genColorCodeFromText($comment[$key]); $data["{$key}picture"] = !empty($this->wiki->config['default_comment_avatar']) ? $this->wiki->config['default_comment_avatar'] - : "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='".str_replace('#', '%23', $data["{$key}color"])."' class='bi bi-person-circle' viewBox='0 0 16 16'%3E%3Cpath d='M11 6a3 3 0 1 1-6 0 3 3 0 0 1 6 0z'/%3E%3Cpath fill-rule='evenodd' d='M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8zm8-7a7 7 0 0 0-5.468 11.37C3.242 11.226 4.805 10 8 10s4.757 1.225 5.468 2.37A7 7 0 0 0 8 1z'/%3E%3C/svg%3E"; + : "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='" . str_replace('#', '%23', $data["{$key}color"]) . "' class='bi bi-person-circle' viewBox='0 0 16 16'%3E%3Cpath d='M11 6a3 3 0 1 1-6 0 3 3 0 0 1 6 0z'/%3E%3Cpath fill-rule='evenodd' d='M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8zm8-7a7 7 0 0 0-5.468 11.37C3.242 11.226 4.805 10 8 10s4.757 1.225 5.468 2.37A7 7 0 0 0 8 1z'/%3E%3C/svg%3E"; } } @@ -279,10 +286,12 @@ public function getCommentForm($tag) } $page = $this->pageManager->getOne($tag); $commentOn = !empty($page['comment_on']) ? $page['comment_on'] : $page['tag']; + $tempTag = ($this->wiki->config['temp_tag_for_entry_creation'] ?? null) . '_' . bin2hex(random_bytes(10)); $options = [ 'pagetag' => $commentOn, 'formlink' => $this->wiki->href('comments', 'api'), - 'hashcash' => $hashCashCode + 'hashcash' => $hashCashCode, + 'tempTag' => $tempTag ]; } else { $options['alerts'][] = [ @@ -361,25 +370,25 @@ public function genColorCodeFromText($text, $min_brightness = 100, $spec = 10) $hash = md5($text); //Gen hash of text $colors = array(); - for ($i = 0;$i < 3;$i++) { - $colors[$i] = max(array(round(((hexdec(substr($hash, $spec * $i, $spec))) / hexdec(str_pad('', $spec, 'F'))) * 255),$min_brightness)); + for ($i = 0; $i < 3; $i++) { + $colors[$i] = max(array(round(((hexdec(substr($hash, $spec * $i, $spec))) / hexdec(str_pad('', $spec, 'F'))) * 255), $min_brightness)); } //convert hash into 3 decimal values between 0 and 255 if ($min_brightness > 0) { //only check brightness requirements if min_brightness is about 100 while (array_sum($colors) / 3 < $min_brightness) { //loop until brightness is above or equal to min_brightness - for ($i = 0;$i < 3;$i++) { + for ($i = 0; $i < 3; $i++) { $colors[$i] += 10; } } - } //increase each color by 10 + } //increase each color by 10 $output = ''; - for ($i = 0;$i < 3;$i++) { + for ($i = 0; $i < 3; $i++) { $output .= str_pad(dechex($colors[$i]), 2, 0, STR_PAD_LEFT); } //convert each color to hex and append to output - return '#'.$output; + return '#' . $output; } public function sendEmailAfterCreate(Event $event) @@ -462,9 +471,11 @@ protected function extractTaggedUsernamesFromContent(array $comment, $loggedUser // filter $filteredUsers = []; foreach ($users as $user) { - if ($user['email'] != $loggedUser['email'] && + if ( + $user['email'] != $loggedUser['email'] && (empty($owner) || ($user['email'] != $owner['email'])) && - !in_array($user['name'], array_keys($filteredUsers))) { + !in_array($user['name'], array_keys($filteredUsers)) + ) { $filteredUsers[$user['name']] = $user; } } diff --git a/templates/comment-form.twig b/templates/comment-form.twig index b38bddf92..746214b5d 100644 --- a/templates/comment-form.twig +++ b/templates/comment-form.twig @@ -2,7 +2,7 @@
- {{ renderAction('aceditor', { name: "body", placeholder: _t('YW_WRITE_COMMENT_HERE'), rows: 3})|raw }} + {{ renderAction('aceditor', { name: "body", placeholder: _t('YW_WRITE_COMMENT_HERE'), rows: 3, tempTag: tempTag })|raw }}
{{ hashcash|raw }}