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