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',
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']);
122 Hook::callAll('jot_tool', $jotplugins);
124 $output .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot.tpl'), [
126 '$return_path' => '/display/' . $item['guid'],
128 '$share' => $this->t('Save'),
129 '$loading' => $this->t('Loading...'),
130 '$upload' => $this->t('Upload photo'),
131 '$shortupload' => $this->t('upload photo'),
132 '$attach' => $this->t('Attach file'),
133 '$shortattach' => $this->t('attach file'),
134 '$weblink' => $this->t('Insert web link'),
135 '$shortweblink' => $this->t('web link'),
136 '$video' => $this->t('Insert video link'),
137 '$shortvideo' => $this->t('video link'),
138 '$audio' => $this->t('Insert audio link'),
139 '$shortaudio' => $this->t('audio link'),
140 '$setloc' => $this->t('Set your location'),
141 '$shortsetloc' => $this->t('set location'),
142 '$noloc' => $this->t('Clear browser location'),
143 '$shortnoloc' => $this->t('clear location'),
144 '$wait' => $this->t('Please wait'),
145 '$permset' => $this->t('Permission settings'),
146 '$wall' => $item['wall'],
147 '$posttype' => $item['post-type'],
148 '$content' => $this->undoPostTagging($item['body']),
149 '$post_id' => $postId,
150 '$defloc' => $user['default-location'],
151 '$visitor' => 'none',
153 '$emailcc' => $this->t('CC: email addresses'),
154 '$public' => $this->t('Public post'),
155 '$title' => $item['title'],
156 '$placeholdertitle' => $this->t('Set title'),
157 '$category' => Post\Category::getCSVByURIId($item['uri-id'], $this->session->getLocalUserId(), Post\Category::CATEGORY),
158 '$placeholdercategory' => (Feature::isEnabled($this->session->getLocalUserId(), 'categories') ? $this->t("Categories \x28comma-separated list\x29") : ''),
159 '$emtitle' => $this->t('Example: bob@example.com, mary@example.com'),
160 '$lockstate' => $lockstate,
162 '$bang' => ($lockstate === 'lock' ? '!' : ''),
163 '$profile_uid' => $this->session->getLocalUserId(),
164 '$preview' => $this->t('Preview'),
165 '$jotplugins' => $jotplugins,
166 '$cancel' => $this->t('Cancel'),
167 '$rand_num' => Crypto::randomDigits(12),
169 // Formatting button labels
170 '$edbold' => $this->t('Bold'),
171 '$editalic' => $this->t('Italic'),
172 '$eduline' => $this->t('Underline'),
173 '$edquote' => $this->t('Quote'),
174 '$edcode' => $this->t('Code'),
175 '$edurl' => $this->t('Link'),
176 '$edattach' => $this->t('Link or Media'),
178 //jot nav tab (used in some themes)
179 '$message' => $this->t('Message'),
180 '$browser' => $this->t('Browser'),
181 '$shortpermset' => $this->t('Permissions'),
183 '$compose_link_title' => $this->t('Open Compose page'),
190 * Removes Tags from the item-body
192 * @param string $body The item body
194 * @return string the new item body without tagging
196 protected function undoPostTagging(string $body)
199 $content = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $body, $matches, PREG_SET_ORDER);
201 foreach ($matches as $match) {
202 if (in_array($match[1], ['!', '@'])) {
203 $contact = Contact::getByURL($match[2], false, ['addr']);
204 $match[3] = empty($contact['addr']) ? $match[2] : $contact['addr'];
206 $body = str_replace($match[0], $match[1] . $match[3], $body);
213 * Exists the current Module because of an error
215 * @param string $message The error message
216 * @param string $exceptionClass In case it's a modal, throw an exception instead of an redirect
220 protected function errorExit(string $message, string $exceptionClass)
222 if ($this->isModal) {
223 throw new $exceptionClass($message);
225 $this->sysMessages->addNotice($message);
226 $this->baseUrl->redirect();