3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Post;
25 use Friendica\BaseModule;
26 use Friendica\Content\Feature;
27 use Friendica\Core\Hook;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Session\Capability\IHandleUserSessions;
31 use Friendica\Model\Contact;
32 use Friendica\Model\Post;
33 use Friendica\Model\User;
34 use Friendica\Module\Response;
35 use Friendica\Navigation\SystemMessages;
36 use Friendica\Network\HTTPException;
37 use Friendica\Util\Crypto;
38 use Friendica\Util\Profiler;
39 use Psr\Log\LoggerInterface;
42 * Controller to edit a post
44 class Edit extends BaseModule
46 /** @var IHandleUserSessions */
48 /** @var SystemMessages */
49 protected $sysMessages;
57 protected $isModal = false;
59 public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $session, SystemMessages $sysMessages, App\Page $page, App\Mode $mode, App $app, array $server, array $parameters = [])
61 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
63 $this->session = $session;
64 $this->sysMessages = $sysMessages;
71 protected function content(array $request = []): string
73 $this->isModal = $request['mode'] ?? '' === 'none';
75 if (!$this->session->getLocalUserId()) {
76 $this->errorExit($this->t('Permission denied.'), HTTPException\UnauthorizedException::class);
79 $postId = $this->parameters['post_id'] ?? 0;
82 $this->errorExit($this->t('Post not found.'), HTTPException\BadRequestException::class);
86 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'gravity',
87 'body', 'title', 'uri-id', 'wall', 'post-type', 'guid'
90 $item = Post::selectFirstForUser($this->session->getLocalUserId(), $fields, [
92 'uid' => $this->session->getLocalUserId(),
96 $this->errorExit($this->t('Post not found.'), HTTPException\BadRequestException::class);
99 $user = User::getById($this->session->getLocalUserId());
101 $output = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
102 '$title' => $this->t('Edit post'),
105 $this->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot-header.tpl'), [
106 '$ispublic' => ' ',
108 '$nickname' => $this->app->getLoggedInUserNickname(),
109 '$is_mobile' => $this->mode->isMobile(),
112 if (strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid'])) {
115 $lockstate = 'unlock';
118 $item['body'] = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
119 $item = Post\Media::addHTMLAttachmentToItem($item);
123 Hook::callAll('jot_tool', $jotplugins);
125 $output .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot.tpl'), [
127 '$return_path' => '/display/' . $item['guid'],
129 '$share' => $this->t('Save'),
130 '$loading' => $this->t('Loading...'),
131 '$upload' => $this->t('Upload photo'),
132 '$shortupload' => $this->t('upload photo'),
133 '$attach' => $this->t('Attach file'),
134 '$shortattach' => $this->t('attach file'),
135 '$weblink' => $this->t('Insert web link'),
136 '$shortweblink' => $this->t('web link'),
137 '$video' => $this->t('Insert video link'),
138 '$shortvideo' => $this->t('video link'),
139 '$audio' => $this->t('Insert audio link'),
140 '$shortaudio' => $this->t('audio link'),
141 '$setloc' => $this->t('Set your location'),
142 '$shortsetloc' => $this->t('set location'),
143 '$noloc' => $this->t('Clear browser location'),
144 '$shortnoloc' => $this->t('clear location'),
145 '$wait' => $this->t('Please wait'),
146 '$permset' => $this->t('Permission settings'),
147 '$wall' => $item['wall'],
148 '$posttype' => $item['post-type'],
149 '$content' => $this->undoPostTagging($item['body']),
150 '$post_id' => $postId,
151 '$defloc' => $user['default-location'],
152 '$visitor' => 'none',
154 '$emailcc' => $this->t('CC: email addresses'),
155 '$public' => $this->t('Public post'),
156 '$title' => $item['title'],
157 '$placeholdertitle' => $this->t('Set title'),
158 '$category' => Post\Category::getCSVByURIId($item['uri-id'], $this->session->getLocalUserId(), Post\Category::CATEGORY),
159 '$placeholdercategory' => (Feature::isEnabled($this->session->getLocalUserId(), 'categories') ? $this->t("Categories \x28comma-separated list\x29") : ''),
160 '$emtitle' => $this->t('Example: bob@example.com, mary@example.com'),
161 '$lockstate' => $lockstate,
163 '$bang' => ($lockstate === 'lock' ? '!' : ''),
164 '$profile_uid' => $this->session->getLocalUserId(),
165 '$preview' => $this->t('Preview'),
166 '$jotplugins' => $jotplugins,
167 '$cancel' => $this->t('Cancel'),
168 '$rand_num' => Crypto::randomDigits(12),
170 // Formatting button labels
171 '$edbold' => $this->t('Bold'),
172 '$editalic' => $this->t('Italic'),
173 '$eduline' => $this->t('Underline'),
174 '$edquote' => $this->t('Quote'),
175 '$edemojis' => $this->t('Add emojis'),
176 '$edcode' => $this->t('Code'),
177 '$edurl' => $this->t('Link'),
178 '$edattach' => $this->t('Link or Media'),
180 //jot nav tab (used in some themes)
181 '$message' => $this->t('Message'),
182 '$browser' => $this->t('Browser'),
183 '$shortpermset' => $this->t('Permissions'),
185 '$compose_link_title' => $this->t('Open Compose page'),
192 * Removes Tags from the item-body
194 * @param string $body The item body
196 * @return string the new item body without tagging
198 protected function undoPostTagging(string $body)
201 $content = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $body, $matches, PREG_SET_ORDER);
203 foreach ($matches as $match) {
204 if (in_array($match[1], ['!', '@'])) {
205 $contact = Contact::getByURL($match[2], false, ['addr']);
206 $match[3] = empty($contact['addr']) ? $match[2] : $contact['addr'];
208 $body = str_replace($match[0], $match[1] . $match[3], $body);
215 * Exists the current Module because of an error
217 * @param string $message The error message
218 * @param string $exceptionClass In case it's a modal, throw an exception instead of an redirect
222 protected function errorExit(string $message, string $exceptionClass)
224 if ($this->isModal) {
225 throw new $exceptionClass($message);
227 $this->sysMessages->addNotice($message);
228 $this->baseUrl->redirect();