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;
32 use Friendica\Model\Contact;
33 use Friendica\Model\Post;
34 use Friendica\Model\User;
35 use Friendica\Module\Response;
36 use Friendica\Navigation\SystemMessages;
37 use Friendica\Network\HTTPException;
38 use Friendica\Util\Crypto;
39 use Friendica\Util\Strings;
40 use Friendica\Util\Profiler;
41 use Psr\Log\LoggerInterface;
44 * Controller to edit a post
46 class Edit extends BaseModule
48 /** @var IHandleUserSessions */
50 /** @var SystemMessages */
51 protected $sysMessages;
59 protected $isModal = false;
61 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 = [])
63 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
65 $this->session = $session;
66 $this->sysMessages = $sysMessages;
73 protected function content(array $request = []): string
75 $this->isModal = $request['mode'] ?? '' === 'none';
77 if (!$this->session->getLocalUserId()) {
78 $this->errorExit($this->t('Permission denied.'), HTTPException\UnauthorizedException::class);
81 $postId = $this->parameters['post_id'] ?? 0;
84 $this->errorExit($this->t('Post not found.'), HTTPException\BadRequestException::class);
88 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
89 'body', 'title', 'uri-id', 'wall', 'post-type', 'guid'
92 $item = Post::selectFirstForUser($this->session->getLocalUserId(), $fields, [
94 'uid' => $this->session->getLocalUserId(),
98 $this->errorExit($this->t('Post not found.'), HTTPException\BadRequestException::class);
101 $user = User::getById($this->session->getLocalUserId());
103 $output = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
104 '$title' => $this->t('Edit post'),
107 $this->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot-header.tpl'), [
108 '$ispublic' => ' ',
110 '$nickname' => $this->app->getLoggedInUserNickname(),
111 '$is_mobile' => $this->mode->isMobile(),
114 if (strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid'])) {
117 $lockstate = 'unlock';
120 $item['body'] = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
121 $item = Post\Media::addHTMLAttachmentToItem($item);
125 Hook::callAll('jot_tool', $jotplugins);
127 $output .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot.tpl'), [
129 '$return_path' => '/display/' . $item['guid'],
131 '$share' => $this->t('Save'),
132 '$loading' => $this->t('Loading...'),
133 '$upload' => $this->t('Upload photo'),
134 '$shortupload' => $this->t('upload photo'),
135 '$attach' => $this->t('Attach file'),
136 '$shortattach' => $this->t('attach file'),
137 '$weblink' => $this->t('Insert web link'),
138 '$shortweblink' => $this->t('web link'),
139 '$video' => $this->t('Insert video link'),
140 '$shortvideo' => $this->t('video link'),
141 '$audio' => $this->t('Insert audio link'),
142 '$shortaudio' => $this->t('audio link'),
143 '$setloc' => $this->t('Set your location'),
144 '$shortsetloc' => $this->t('set location'),
145 '$noloc' => $this->t('Clear browser location'),
146 '$shortnoloc' => $this->t('clear location'),
147 '$wait' => $this->t('Please wait'),
148 '$permset' => $this->t('Permission settings'),
149 '$wall' => $item['wall'],
150 '$posttype' => $item['post-type'],
151 '$content' => $this->undoPostTagging($item['body']),
152 '$post_id' => $postId,
153 '$defloc' => $user['default-location'],
154 '$visitor' => 'none',
156 '$emailcc' => $this->t('CC: email addresses'),
157 '$public' => $this->t('Public post'),
158 '$title' => $item['title'],
159 '$placeholdertitle' => $this->t('Set title'),
160 '$category' => Post\Category::getCSVByURIId($item['uri-id'], $this->session->getLocalUserId(), Post\Category::CATEGORY),
161 '$placeholdercategory' => (Feature::isEnabled($this->session->getLocalUserId(), 'categories') ? $this->t("Categories \x28comma-separated list\x29") : ''),
162 '$emtitle' => $this->t('Example: bob@example.com, mary@example.com'),
163 '$lockstate' => $lockstate,
165 '$bang' => ($lockstate === 'lock' ? '!' : ''),
166 '$profile_uid' => $this->session->getLocalUserId(),
167 '$preview' => $this->t('Preview'),
168 '$jotplugins' => $jotplugins,
169 '$cancel' => $this->t('Cancel'),
170 '$rand_num' => Crypto::randomDigits(12),
172 // Formatting button labels
173 '$edbold' => $this->t('Bold'),
174 '$editalic' => $this->t('Italic'),
175 '$eduline' => $this->t('Underline'),
176 '$edquote' => $this->t('Quote'),
177 '$edcode' => $this->t('Code'),
178 '$edurl' => $this->t('Link'),
179 '$edattach' => $this->t('Link or Media'),
181 //jot nav tab (used in some themes)
182 '$message' => $this->t('Message'),
183 '$browser' => $this->t('Browser'),
184 '$shortpermset' => $this->t('Permissions'),
186 '$compose_link_title' => $this->t('Open Compose page'),
189 '$max_imagesize' => floor(Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize')) / 1000000),
195 * Removes Tags from the item-body
197 * @param string $body The item body
199 * @return string the new item body without tagging
201 protected function undoPostTagging(string $body)
204 $content = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $body, $matches, PREG_SET_ORDER);
206 foreach ($matches as $match) {
207 if (in_array($match[1], ['!', '@'])) {
208 $contact = Contact::getByURL($match[2], false, ['addr']);
209 $match[3] = empty($contact['addr']) ? $match[2] : $contact['addr'];
211 $body = str_replace($match[0], $match[1] . $match[3], $body);
218 * Exists the current Module because of an error
220 * @param string $message The error message
221 * @param string $exceptionClass In case it's a modal, throw an exception instead of an redirect
225 protected function errorExit(string $message, string $exceptionClass)
227 if ($this->isModal) {
228 throw new $exceptionClass($message);
230 $this->sysMessages->addNotice($message);
231 $this->baseUrl->redirect();