]> git.mxchange.org Git - friendica.git/blob - src/Module/Post/Edit.php
Move mod/editpost.php to src\Module\Post\Edit
[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'];
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                 $geoTag = '';
99
100                 $output = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
101                         '$title' => $this->t('Edit post'),
102                 ]);
103
104                 $this->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot-header.tpl'), [
105                         '$ispublic'  => '&nbsp;', // $this->t('Visible to <strong>everybody</strong>'),
106                         '$geotag'    => $geoTag,
107                         '$nickname'  => $this->app->getLoggedInUserNickname(),
108                         '$is_mobile' => $this->mode->isMobile(),
109                 ]);
110
111                 if (strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid'])) {
112                         $lockstate = 'lock';
113                 } else {
114                         $lockstate = 'unlock';
115                 }
116
117                 $jotplugins = '';
118                 $jotnets    = '';
119
120                 Hook::callAll('jot_tool', $jotplugins);
121
122                 $output .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot.tpl'), [
123                         '$is_edit'             => true,
124                         '$return_path'         => '/display/' . $item['guid'],
125                         '$action'              => 'item',
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',
150                         '$pvisit'              => 'none',
151                         '$emailcc'             => $this->t('CC: email addresses'),
152                         '$public'              => $this->t('Public post'),
153                         '$jotnets'             => $jotnets,
154                         '$title'               => $item['title'],
155                         '$placeholdertitle'    => $this->t('Set title'),
156                         '$category'            => Post\Category::getCSVByURIId($item['uri-id'], $this->session->getLocalUserId(), Post\Category::CATEGORY),
157                         '$placeholdercategory' => (Feature::isEnabled($this->session->getLocalUserId(), 'categories') ? $this->t("Categories \x28comma-separated list\x29") : ''),
158                         '$emtitle'             => $this->t('Example: bob@example.com, mary@example.com'),
159                         '$lockstate'           => $lockstate,
160                         '$acl'                 => '', // populate_acl((($group) ? $group_acl : $a->user)),
161                         '$bang'                => ($lockstate === 'lock' ? '!' : ''),
162                         '$profile_uid'         => $_SESSION['uid'],
163                         '$preview'             => $this->t('Preview'),
164                         '$jotplugins'          => $jotplugins,
165                         '$cancel'              => $this->t('Cancel'),
166                         '$rand_num'            => Crypto::randomDigits(12),
167
168                         // Formatting button labels
169                         '$edbold'              => $this->t('Bold'),
170                         '$editalic'            => $this->t('Italic'),
171                         '$eduline'             => $this->t('Underline'),
172                         '$edquote'             => $this->t('Quote'),
173                         '$edcode'              => $this->t('Code'),
174                         '$edurl'               => $this->t('Link'),
175                         '$edattach'            => $this->t('Link or Media'),
176
177                         //jot nav tab (used in some themes)
178                         '$message'             => $this->t('Message'),
179                         '$browser'             => $this->t('Browser'),
180                         '$shortpermset'        => $this->t('Permissions'),
181
182                         '$compose_link_title' => $this->t('Open Compose page'),
183                 ]);
184
185                 return $output;
186         }
187
188         /**
189          * Removes Tags from the item-body
190          *
191          * @param string $body The item body
192          *
193          * @return string the new item body without tagging
194          */
195         protected function undoPostTagging(string $body)
196         {
197                 $matches = null;
198                 $content = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $body, $matches, PREG_SET_ORDER);
199                 if ($content) {
200                         foreach ($matches as $match) {
201                                 if (in_array($match[1], ['!', '@'])) {
202                                         $contact  = Contact::getByURL($match[2], false, ['addr']);
203                                         $match[3] = empty($contact['addr']) ? $match[2] : $contact['addr'];
204                                 }
205                                 $body = str_replace($match[0], $match[1] . $match[3], $body);
206                         }
207                 }
208                 return $body;
209         }
210
211         /**
212          * Exists the current Module because of an error
213          *
214          * @param string $message        The error message
215          * @param string $exceptionClass In case it's a modal, throw an exception instead of an redirect
216          *
217          * @return void
218          */
219         protected function errorExit(string $message, string $exceptionClass)
220         {
221                 if ($this->isModal) {
222                         throw new $exceptionClass($message);
223                 } else {
224                         $this->sysMessages->addNotice($message);
225                         $this->baseUrl->redirect();
226                 }
227         }
228 }