]> git.mxchange.org Git - friendica.git/blob - src/Module/Post/Edit.php
use round instead of floor to make imagesize <1M possible
[friendica.git] / src / Module / Post / Edit.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\DI;
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;
42
43 /**
44  * Controller to edit a post
45  */
46 class Edit extends BaseModule
47 {
48         /** @var IHandleUserSessions */
49         protected $session;
50         /** @var SystemMessages */
51         protected $sysMessages;
52         /** @var App\Page */
53         protected $page;
54         /** @var App\Mode */
55         protected $mode;
56         /** @var App */
57         protected $app;
58         /** @var bool */
59         protected $isModal = false;
60
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 = [])
62         {
63                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
64
65                 $this->session     = $session;
66                 $this->sysMessages = $sysMessages;
67                 $this->page        = $page;
68                 $this->mode        = $mode;
69                 $this->app         = $app;
70         }
71
72
73         protected function content(array $request = []): string
74         {
75                 $this->isModal = $request['mode'] ?? '' === 'none';
76
77                 if (!$this->session->getLocalUserId()) {
78                         $this->errorExit($this->t('Permission denied.'), HTTPException\UnauthorizedException::class);
79                 }
80
81                 $postId = $this->parameters['post_id'] ?? 0;
82
83                 if (empty($postId)) {
84                         $this->errorExit($this->t('Post not found.'), HTTPException\BadRequestException::class);
85                 }
86
87                 $fields = [
88                         'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
89                         'body', 'title', 'uri-id', 'wall', 'post-type', 'guid'
90                 ];
91
92                 $item = Post::selectFirstForUser($this->session->getLocalUserId(), $fields, [
93                         'id'  => $postId,
94                         'uid' => $this->session->getLocalUserId(),
95                 ]);
96
97                 if (empty($item)) {
98                         $this->errorExit($this->t('Post not found.'), HTTPException\BadRequestException::class);
99                 }
100
101                 $user = User::getById($this->session->getLocalUserId());
102
103                 $output = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
104                         '$title' => $this->t('Edit post'),
105                 ]);
106
107                 $this->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot-header.tpl'), [
108                         '$ispublic'  => '&nbsp;',
109                         '$geotag'    => '',
110                         '$nickname'  => $this->app->getLoggedInUserNickname(),
111                         '$is_mobile' => $this->mode->isMobile(),
112                 ]);
113
114                 if (strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid'])) {
115                         $lockstate = 'lock';
116                 } else {
117                         $lockstate = 'unlock';
118                 }
119
120                 $item['body'] = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
121                 $item = Post\Media::addHTMLAttachmentToItem($item);
122
123                 $jotplugins = '';
124
125                 Hook::callAll('jot_tool', $jotplugins);
126
127                 $output .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot.tpl'), [
128                         '$is_edit'             => true,
129                         '$return_path'         => '/display/' . $item['guid'],
130                         '$action'              => 'item',
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',
155                         '$pvisit'              => '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,
164                         '$acl'                 => '',
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),
171
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'),
180
181                         //jot nav tab (used in some themes)
182                         '$message'      => $this->t('Message'),
183                         '$browser'      => $this->t('Browser'),
184                         '$shortpermset' => $this->t('Permissions'),
185
186                         '$compose_link_title' => $this->t('Open Compose page'),
187
188                         // Dropzone
189                         '$max_imagesize'       => round(Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize')) / 1000000,1),
190                 ]);
191                 return $output;
192         }
193
194         /**
195          * Removes Tags from the item-body
196          *
197          * @param string $body The item body
198          *
199          * @return string the new item body without tagging
200          */
201         protected function undoPostTagging(string $body)
202         {
203                 $matches = null;
204                 $content = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $body, $matches, PREG_SET_ORDER);
205                 if ($content) {
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'];
210                                 }
211                                 $body = str_replace($match[0], $match[1] . $match[3], $body);
212                         }
213                 }
214                 return $body;
215         }
216
217         /**
218          * Exists the current Module because of an error
219          *
220          * @param string $message        The error message
221          * @param string $exceptionClass In case it's a modal, throw an exception instead of an redirect
222          *
223          * @return void
224          */
225         protected function errorExit(string $message, string $exceptionClass)
226         {
227                 if ($this->isModal) {
228                         throw new $exceptionClass($message);
229                 } else {
230                         $this->sysMessages->addNotice($message);
231                         $this->baseUrl->redirect();
232                 }
233         }
234 }