]> git.mxchange.org Git - friendica.git/blob - src/Module/Post/Edit.php
0f382aebf1634d535eee7de18e083add57405f2c
[friendica.git] / src / Module / Post / Edit.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Post;
23
24 use Friendica\App;
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;
40
41 /**
42  * Controller to edit a post
43  */
44 class Edit extends BaseModule
45 {
46         /** @var IHandleUserSessions */
47         protected $session;
48         /** @var SystemMessages */
49         protected $sysMessages;
50         /** @var App\Page */
51         protected $page;
52         /** @var App\Mode */
53         protected $mode;
54         /** @var App */
55         protected $app;
56         /** @var bool */
57         protected $isModal = false;
58
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 = [])
60         {
61                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
62
63                 $this->session     = $session;
64                 $this->sysMessages = $sysMessages;
65                 $this->page        = $page;
66                 $this->mode        = $mode;
67                 $this->app         = $app;
68         }
69
70
71         protected function content(array $request = []): string
72         {
73                 $this->isModal = $request['mode'] ?? '' === 'none';
74
75                 if (!$this->session->getLocalUserId()) {
76                         $this->errorExit($this->t('Permission denied.'), HTTPException\UnauthorizedException::class);
77                 }
78
79                 $postId = $this->parameters['post_id'] ?? 0;
80
81                 if (empty($postId)) {
82                         $this->errorExit($this->t('Post not found.'), HTTPException\BadRequestException::class);
83                 }
84
85                 $fields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
86                                    'body', 'title', 'uri-id', 'wall', 'post-type', 'guid'];
87
88                 $item = Post::selectFirstForUser($this->session->getLocalUserId(), $fields, [
89                         'id'  => $postId,
90                         'uid' => $this->session->getLocalUserId(),
91                 ]);
92
93                 if (empty($item)) {
94                         $this->errorExit($this->t('Post not found.'), HTTPException\BadRequestException::class);
95                 }
96
97                 $user   = User::getById($this->session->getLocalUserId());
98
99                 $output = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
100                         '$title' => $this->t('Edit post'),
101                 ]);
102
103                 $this->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot-header.tpl'), [
104                         '$ispublic'  => '&nbsp;', // $this->t('Visible to <strong>everybody</strong>'),
105                         '$nickname'  => $this->app->getLoggedInUserNickname(),
106                         '$is_mobile' => $this->mode->isMobile(),
107                 ]);
108
109                 if (strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid'])) {
110                         $lockstate = 'lock';
111                 } else {
112                         $lockstate = 'unlock';
113                 }
114
115                 $jotplugins = '';
116
117                 Hook::callAll('jot_tool', $jotplugins);
118
119                 $output .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot.tpl'), [
120                         '$is_edit'             => true,
121                         '$return_path'         => '/display/' . $item['guid'],
122                         '$action'              => 'item',
123                         '$share'               => $this->t('Save'),
124                         '$loading'             => $this->t('Loading...'),
125                         '$upload'              => $this->t('Upload photo'),
126                         '$shortupload'         => $this->t('upload photo'),
127                         '$attach'              => $this->t('Attach file'),
128                         '$shortattach'         => $this->t('attach file'),
129                         '$weblink'             => $this->t('Insert web link'),
130                         '$shortweblink'        => $this->t('web link'),
131                         '$video'               => $this->t('Insert video link'),
132                         '$shortvideo'          => $this->t('video link'),
133                         '$audio'               => $this->t('Insert audio link'),
134                         '$shortaudio'          => $this->t('audio link'),
135                         '$setloc'              => $this->t('Set your location'),
136                         '$shortsetloc'         => $this->t('set location'),
137                         '$noloc'               => $this->t('Clear browser location'),
138                         '$shortnoloc'          => $this->t('clear location'),
139                         '$wait'                => $this->t('Please wait'),
140                         '$permset'             => $this->t('Permission settings'),
141                         '$wall'                => $item['wall'],
142                         '$posttype'            => $item['post-type'],
143                         '$content'             => $this->undoPostTagging($item['body']),
144                         '$post_id'             => $postId,
145                         '$defloc'              => $user['default-location'],
146                         '$visitor'             => 'none',
147                         '$pvisit'              => 'none',
148                         '$emailcc'             => $this->t('CC: email addresses'),
149                         '$public'              => $this->t('Public post'),
150                         '$title'               => $item['title'],
151                         '$placeholdertitle'    => $this->t('Set title'),
152                         '$category'            => Post\Category::getCSVByURIId($item['uri-id'], $this->session->getLocalUserId(), Post\Category::CATEGORY),
153                         '$placeholdercategory' => (Feature::isEnabled($this->session->getLocalUserId(), 'categories') ? $this->t("Categories \x28comma-separated list\x29") : ''),
154                         '$emtitle'             => $this->t('Example: bob@example.com, mary@example.com'),
155                         '$lockstate'           => $lockstate,
156                         '$acl'                 => '', // populate_acl((($group) ? $group_acl : $a->user)),
157                         '$bang'                => ($lockstate === 'lock' ? '!' : ''),
158                         '$profile_uid'         => $this->session->getLocalUserId(),
159                         '$preview'             => $this->t('Preview'),
160                         '$jotplugins'          => $jotplugins,
161                         '$cancel'              => $this->t('Cancel'),
162                         '$rand_num'            => Crypto::randomDigits(12),
163
164                         // Formatting button labels
165                         '$edbold'              => $this->t('Bold'),
166                         '$editalic'            => $this->t('Italic'),
167                         '$eduline'             => $this->t('Underline'),
168                         '$edquote'             => $this->t('Quote'),
169                         '$edcode'              => $this->t('Code'),
170                         '$edurl'               => $this->t('Link'),
171                         '$edattach'            => $this->t('Link or Media'),
172
173                         //jot nav tab (used in some themes)
174                         '$message'             => $this->t('Message'),
175                         '$browser'             => $this->t('Browser'),
176                         '$shortpermset'        => $this->t('Permissions'),
177
178                         '$compose_link_title' => $this->t('Open Compose page'),
179                 ]);
180
181                 return $output;
182         }
183
184         /**
185          * Removes Tags from the item-body
186          *
187          * @param string $body The item body
188          *
189          * @return string the new item body without tagging
190          */
191         protected function undoPostTagging(string $body)
192         {
193                 $matches = null;
194                 $content = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $body, $matches, PREG_SET_ORDER);
195                 if ($content) {
196                         foreach ($matches as $match) {
197                                 if (in_array($match[1], ['!', '@'])) {
198                                         $contact  = Contact::getByURL($match[2], false, ['addr']);
199                                         $match[3] = empty($contact['addr']) ? $match[2] : $contact['addr'];
200                                 }
201                                 $body = str_replace($match[0], $match[1] . $match[3], $body);
202                         }
203                 }
204                 return $body;
205         }
206
207         /**
208          * Exists the current Module because of an error
209          *
210          * @param string $message        The error message
211          * @param string $exceptionClass In case it's a modal, throw an exception instead of an redirect
212          *
213          * @return void
214          */
215         protected function errorExit(string $message, string $exceptionClass)
216         {
217                 if ($this->isModal) {
218                         throw new $exceptionClass($message);
219                 } else {
220                         $this->sysMessages->addNotice($message);
221                         $this->baseUrl->redirect();
222                 }
223         }
224 }