3 * @copyright Copyright (C) 2010-2022, 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);
85 $fields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
86 'body', 'title', 'uri-id', 'wall', 'post-type', 'guid'];
88 $item = Post::selectFirstForUser($this->session->getLocalUserId(), $fields, [
90 'uid' => $this->session->getLocalUserId(),
94 $this->errorExit($this->t('Post not found.'), HTTPException\BadRequestException::class);
97 $user = User::getById($this->session->getLocalUserId());
100 $output = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
101 '$title' => $this->t('Edit post'),
104 $this->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot-header.tpl'), [
105 '$ispublic' => ' ', // $this->t('Visible to <strong>everybody</strong>'),
106 '$geotag' => $geoTag,
107 '$nickname' => $this->app->getLoggedInUserNickname(),
108 '$is_mobile' => $this->mode->isMobile(),
111 if (strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid'])) {
114 $lockstate = 'unlock';
119 Hook::callAll('jot_tool', $jotplugins);
121 $output .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot.tpl'), [
123 '$return_path' => '/display/' . $item['guid'],
125 '$share' => $this->t('Save'),
126 '$loading' => $this->t('Loading...'),
127 '$upload' => $this->t('Upload photo'),
128 '$shortupload' => $this->t('upload photo'),
129 '$attach' => $this->t('Attach file'),
130 '$shortattach' => $this->t('attach file'),
131 '$weblink' => $this->t('Insert web link'),
132 '$shortweblink' => $this->t('web link'),
133 '$video' => $this->t('Insert video link'),
134 '$shortvideo' => $this->t('video link'),
135 '$audio' => $this->t('Insert audio link'),
136 '$shortaudio' => $this->t('audio link'),
137 '$setloc' => $this->t('Set your location'),
138 '$shortsetloc' => $this->t('set location'),
139 '$noloc' => $this->t('Clear browser location'),
140 '$shortnoloc' => $this->t('clear location'),
141 '$wait' => $this->t('Please wait'),
142 '$permset' => $this->t('Permission settings'),
143 '$wall' => $item['wall'],
144 '$posttype' => $item['post-type'],
145 '$content' => $this->undoPostTagging($item['body']),
146 '$post_id' => $postId,
147 '$defloc' => $user['default-location'],
148 '$visitor' => 'none',
150 '$emailcc' => $this->t('CC: email addresses'),
151 '$public' => $this->t('Public post'),
152 '$title' => $item['title'],
153 '$placeholdertitle' => $this->t('Set title'),
154 '$category' => Post\Category::getCSVByURIId($item['uri-id'], $this->session->getLocalUserId(), Post\Category::CATEGORY),
155 '$placeholdercategory' => (Feature::isEnabled($this->session->getLocalUserId(), 'categories') ? $this->t("Categories \x28comma-separated list\x29") : ''),
156 '$emtitle' => $this->t('Example: bob@example.com, mary@example.com'),
157 '$lockstate' => $lockstate,
158 '$acl' => '', // populate_acl((($group) ? $group_acl : $a->user)),
159 '$bang' => ($lockstate === 'lock' ? '!' : ''),
160 '$profile_uid' => $this->session->getLocalUserId(),
161 '$preview' => $this->t('Preview'),
162 '$jotplugins' => $jotplugins,
163 '$cancel' => $this->t('Cancel'),
164 '$rand_num' => Crypto::randomDigits(12),
166 // Formatting button labels
167 '$edbold' => $this->t('Bold'),
168 '$editalic' => $this->t('Italic'),
169 '$eduline' => $this->t('Underline'),
170 '$edquote' => $this->t('Quote'),
171 '$edcode' => $this->t('Code'),
172 '$edurl' => $this->t('Link'),
173 '$edattach' => $this->t('Link or Media'),
175 //jot nav tab (used in some themes)
176 '$message' => $this->t('Message'),
177 '$browser' => $this->t('Browser'),
178 '$shortpermset' => $this->t('Permissions'),
180 '$compose_link_title' => $this->t('Open Compose page'),
187 * Removes Tags from the item-body
189 * @param string $body The item body
191 * @return string the new item body without tagging
193 protected function undoPostTagging(string $body)
196 $content = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $body, $matches, PREG_SET_ORDER);
198 foreach ($matches as $match) {
199 if (in_array($match[1], ['!', '@'])) {
200 $contact = Contact::getByURL($match[2], false, ['addr']);
201 $match[3] = empty($contact['addr']) ? $match[2] : $contact['addr'];
203 $body = str_replace($match[0], $match[1] . $match[3], $body);
210 * Exists the current Module because of an error
212 * @param string $message The error message
213 * @param string $exceptionClass In case it's a modal, throw an exception instead of an redirect
217 protected function errorExit(string $message, string $exceptionClass)
219 if ($this->isModal) {
220 throw new $exceptionClass($message);
222 $this->sysMessages->addNotice($message);
223 $this->baseUrl->redirect();