]> git.mxchange.org Git - friendica.git/blob - src/Module/Item/Compose.php
6766cb5e51cdc9bcd281d8d49d0e63078cd9773c
[friendica.git] / src / Module / Item / Compose.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\Item;
23
24 use DateTime;
25 use Friendica\App;
26 use Friendica\BaseModule;
27 use Friendica\Content\Feature;
28 use Friendica\Core\ACL;
29 use Friendica\Core\Config\Capability\IManageConfigValues;
30 use Friendica\Core\Hook;
31 use Friendica\Core\L10n;
32 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
33 use Friendica\Core\Renderer;
34 use Friendica\Core\Theme;
35 use Friendica\Database\DBA;
36 use Friendica\DI;
37 use Friendica\Model\Contact;
38 use Friendica\Model\Item;
39 use Friendica\Model\User;
40 use Friendica\Module\Response;
41 use Friendica\Module\Security\Login;
42 use Friendica\Navigation\SystemMessages;
43 use Friendica\Network\HTTPException\NotImplementedException;
44 use Friendica\Util\ACLFormatter;
45 use Friendica\Util\Crypto;
46 use Friendica\Util\Profiler;
47 use Friendica\Util\Temporal;
48 use Psr\Log\LoggerInterface;
49
50 class Compose extends BaseModule
51 {
52         /** @var SystemMessages */
53         private $systemMessages;
54
55         /** @var ACLFormatter */
56         private $ACLFormatter;
57
58         /** @var App\Page */
59         private $page;
60
61         /** @var IManagePersonalConfigValues */
62         private $pConfig;
63
64         /** @var IManageConfigValues */
65         private $config;
66
67         public function __construct(IManageConfigValues $config, IManagePersonalConfigValues $pConfig, App\Page $page, ACLFormatter $ACLFormatter, SystemMessages $systemMessages, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
68         {
69                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
70
71                 $this->systemMessages = $systemMessages;
72                 $this->ACLFormatter   = $ACLFormatter;
73                 $this->page           = $page;
74                 $this->pConfig        = $pConfig;
75                 $this->config         = $config;
76         }
77
78         protected function post(array $request = [])
79         {
80                 if (!empty($_REQUEST['body'])) {
81                         $_REQUEST['return'] = 'network';
82                         require_once 'mod/item.php';
83                         item_post(DI::app());
84                 } else {
85                         $this->systemMessages->addNotice($this->l10n->t('Please enter a post body.'));
86                 }
87         }
88
89         protected function content(array $request = []): string
90         {
91                 if (!DI::userSession()->getLocalUserId()) {
92                         return Login::form('compose');
93                 }
94
95                 $a = DI::app();
96
97                 if ($a->getCurrentTheme() !== 'frio') {
98                         throw new NotImplementedException($this->l10n->t('This feature is only available with the frio theme.'));
99                 }
100
101                 $posttype = $this->parameters['type'] ?? Item::PT_ARTICLE;
102                 if (!in_array($posttype, [Item::PT_ARTICLE, Item::PT_PERSONAL_NOTE])) {
103                         switch ($posttype) {
104                                 case 'note':
105                                         $posttype = Item::PT_PERSONAL_NOTE;
106                                         break;
107                                 default:
108                                         $posttype = Item::PT_ARTICLE;
109                                         break;
110                         }
111                 }
112
113                 $user = User::getById(DI::userSession()->getLocalUserId(), ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'default-location']);
114
115                 $contact_allow_list = $this->ACLFormatter->expand($user['allow_cid']);
116                 $circle_allow_list  = $this->ACLFormatter->expand($user['allow_gid']);
117                 $contact_deny_list  = $this->ACLFormatter->expand($user['deny_cid']);
118                 $circle_deny_list   = $this->ACLFormatter->expand($user['deny_gid']);
119
120                 switch ($posttype) {
121                         case Item::PT_PERSONAL_NOTE:
122                                 $compose_title = $this->l10n->t('Compose new personal note');
123                                 $type = 'note';
124                                 $doesFederate = false;
125                                 $contact_allow_list = [$a->getContactId()];
126                                 $circle_allow_list = [];
127                                 $contact_deny_list = [];
128                                 $circle_deny_list = [];
129                                 break;
130                         default:
131                                 $compose_title = $this->l10n->t('Compose new post');
132                                 $type = 'post';
133                                 $doesFederate = true;
134
135                                 $contact_allow = $_REQUEST['contact_allow'] ?? '';
136                                 $circle_allow = $_REQUEST['circle_allow'] ?? '';
137                                 $contact_deny = $_REQUEST['contact_deny'] ?? '';
138                                 $circle_deny = $_REQUEST['circle_deny'] ?? '';
139
140                                 if ($contact_allow
141                                         . $circle_allow
142                                         . $contact_deny
143                                     . $circle_deny)
144                                 {
145                                         $contact_allow_list = $contact_allow ? explode(',', $contact_allow) : [];
146                                         $circle_allow_list  = $circle_allow  ? explode(',', $circle_allow)  : [];
147                                         $contact_deny_list  = $contact_deny  ? explode(',', $contact_deny)  : [];
148                                         $circle_deny_list   = $circle_deny   ? explode(',', $circle_deny)   : [];
149                                 }
150
151                                 break;
152                 }
153
154                 $title         = $_REQUEST['title']         ?? '';
155                 $category      = $_REQUEST['category']      ?? '';
156                 $body          = $_REQUEST['body']          ?? '';
157                 $location      = $_REQUEST['location']      ?? $user['default-location'];
158                 $wall          = $_REQUEST['wall']          ?? $type == 'post';
159
160                 $jotplugins = '';
161                 Hook::callAll('jot_tool', $jotplugins);
162
163                 // Output
164                 $this->page->registerFooterScript(Theme::getPathForFile('js/ajaxupload.js'));
165                 $this->page->registerFooterScript(Theme::getPathForFile('js/linkPreview.js'));
166                 $this->page->registerFooterScript(Theme::getPathForFile('js/compose.js'));
167
168                 $contact = Contact::getById($a->getContactId());
169
170                 if ($this->pConfig->get(DI::userSession()->getLocalUserId(), 'system', 'set_creation_date')) {
171                         $created_at = Temporal::getDateTimeField(
172                                 new \DateTime(DBA::NULL_DATETIME),
173                                 new \DateTime('now'),
174                                 null,
175                                 $this->l10n->t('Created at'),
176                                 'created_at'
177                         );
178                 } else {
179                         $created_at = '';
180                 }
181
182                 $tpl = Renderer::getMarkupTemplate('item/compose.tpl');
183                 return Renderer::replaceMacros($tpl, [
184                         '$l10n' => [
185                                 'compose_title'        => $compose_title,
186                                 'default'              => '',
187                                 'visibility_title'     => $this->l10n->t('Visibility'),
188                                 'mytitle'              => $this->l10n->t('This is you'),
189                                 'submit'               => $this->l10n->t('Submit'),
190                                 'edbold'               => $this->l10n->t('Bold'),
191                                 'editalic'             => $this->l10n->t('Italic'),
192                                 'eduline'              => $this->l10n->t('Underline'),
193                                 'edquote'              => $this->l10n->t('Quote'),
194                                 '$edemojis'            => $this->l10n->t('Add emojis'),
195                                 'edcode'               => $this->l10n->t('Code'),
196                                 'edimg'                => $this->l10n->t('Image'),
197                                 'edurl'                => $this->l10n->t('Link'),
198                                 'edattach'             => $this->l10n->t('Link or Media'),
199                                 'prompttext'           => $this->l10n->t('Please enter a image/video/audio/webpage URL:'),
200                                 'preview'              => $this->l10n->t('Preview'),
201                                 'location_set'         => $this->l10n->t('Set your location'),
202                                 'location_clear'       => $this->l10n->t('Clear the location'),
203                                 'location_unavailable' => $this->l10n->t('Location services are unavailable on your device'),
204                                 'location_disabled'    => $this->l10n->t('Location services are disabled. Please check the website\'s permissions on your device'),
205                                 'wait'                 => $this->l10n->t('Please wait'),
206                                 'placeholdertitle'     => $this->l10n->t('Set title'),
207                                 'placeholdercategory'  => Feature::isEnabled(DI::userSession()->getLocalUserId(),'categories') ? $this->l10n->t('Categories (comma-separated list)') : '',
208                                 'always_open_compose'  => $this->pConfig->get(DI::userSession()->getLocalUserId(), 'frio', 'always_open_compose',
209                                         $this->config->get('frio', 'always_open_compose', false)) ? '' :
210                                                 $this->l10n->t('You can make this page always open when you use the New Post button in the <a href="/settings/display">Theme Customization settings</a>.'),
211                         ],
212
213                         '$id'           => 0,
214                         '$posttype'     => $posttype,
215                         '$type'         => $type,
216                         '$wall'         => $wall,
217                         '$mylink'       => $this->baseUrl->remove($contact['url']),
218                         '$myphoto'      => $this->baseUrl->remove($contact['thumb']),
219                         '$scheduled_at' => Temporal::getDateTimeField(
220                                 new DateTime(),
221                                 new DateTime('now + 6 months'),
222                                 null,
223                                 $this->l10n->t('Scheduled at'),
224                                 'scheduled_at'
225                         ),
226                         '$created_at'   => $created_at,
227                         '$title'        => $title,
228                         '$category'     => $category,
229                         '$body'         => $body,
230                         '$location'     => $location,
231
232                         '$contact_allow' => implode(',', $contact_allow_list),
233                         '$circle_allow'  => implode(',', $circle_allow_list),
234                         '$contact_deny'  => implode(',', $contact_deny_list),
235                         '$circle_deny'   => implode(',', $circle_deny_list),
236
237                         '$jotplugins'   => $jotplugins,
238                         '$rand_num'     => Crypto::randomDigits(12),
239                         '$acl_selector'  => ACL::getFullSelectorHTML($this->page, $a->getLoggedInUserId(), $doesFederate, [
240                                 'allow_cid' => $contact_allow_list,
241                                 'allow_gid' => $circle_allow_list,
242                                 'deny_cid'  => $contact_deny_list,
243                                 'deny_gid'  => $circle_deny_list,
244                         ]),
245                 ]);
246         }
247 }