]> git.mxchange.org Git - friendica.git/blob - src/Module/Item/Compose.php
old boot.php functions replaced in src/module (2)
[friendica.git] / src / Module / Item / Compose.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\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\Session;
35 use Friendica\Core\Theme;
36 use Friendica\Database\DBA;
37 use Friendica\DI;
38 use Friendica\Model\Contact;
39 use Friendica\Model\Item;
40 use Friendica\Model\User;
41 use Friendica\Module\Response;
42 use Friendica\Module\Security\Login;
43 use Friendica\Navigation\SystemMessages;
44 use Friendica\Network\HTTPException\NotImplementedException;
45 use Friendica\Util\ACLFormatter;
46 use Friendica\Util\Crypto;
47 use Friendica\Util\Profiler;
48 use Friendica\Util\Temporal;
49 use Psr\Log\LoggerInterface;
50
51 class Compose extends BaseModule
52 {
53         /** @var SystemMessages */
54         private $systemMessages;
55
56         /** @var ACLFormatter */
57         private $ACLFormatter;
58
59         /** @var App\Page */
60         private $page;
61
62         /** @var IManagePersonalConfigValues */
63         private $pConfig;
64
65         /** @var IManageConfigValues */
66         private $config;
67
68         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 = [])
69         {
70                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
71
72                 $this->systemMessages = $systemMessages;
73                 $this->ACLFormatter   = $ACLFormatter;
74                 $this->page           = $page;
75                 $this->pConfig        = $pConfig;
76                 $this->config         = $config;
77         }
78
79         protected function post(array $request = [])
80         {
81                 if (!empty($_REQUEST['body'])) {
82                         $_REQUEST['return'] = 'network';
83                         require_once 'mod/item.php';
84                         item_post(DI::app());
85                 } else {
86                         $this->systemMessages->addNotice($this->l10n->t('Please enter a post body.'));
87                 }
88         }
89
90         protected function content(array $request = []): string
91         {
92                 if (!Session::getLocalUser()) {
93                         return Login::form('compose');
94                 }
95
96                 $a = DI::app();
97
98                 if ($a->getCurrentTheme() !== 'frio') {
99                         throw new NotImplementedException($this->l10n->t('This feature is only available with the frio theme.'));
100                 }
101
102                 $posttype = $this->parameters['type'] ?? Item::PT_ARTICLE;
103                 if (!in_array($posttype, [Item::PT_ARTICLE, Item::PT_PERSONAL_NOTE])) {
104                         switch ($posttype) {
105                                 case 'note':
106                                         $posttype = Item::PT_PERSONAL_NOTE;
107                                         break;
108                                 default:
109                                         $posttype = Item::PT_ARTICLE;
110                                         break;
111                         }
112                 }
113
114                 $user = User::getById(Session::getLocalUser(), ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'default-location']);
115
116                 $contact_allow_list = $this->ACLFormatter->expand($user['allow_cid']);
117                 $group_allow_list   = $this->ACLFormatter->expand($user['allow_gid']);
118                 $contact_deny_list  = $this->ACLFormatter->expand($user['deny_cid']);
119                 $group_deny_list    = $this->ACLFormatter->expand($user['deny_gid']);
120
121                 switch ($posttype) {
122                         case Item::PT_PERSONAL_NOTE:
123                                 $compose_title = $this->l10n->t('Compose new personal note');
124                                 $type = 'note';
125                                 $doesFederate = false;
126                                 $contact_allow_list = [$a->getContactId()];
127                                 $group_allow_list = [];
128                                 $contact_deny_list = [];
129                                 $group_deny_list = [];
130                                 break;
131                         default:
132                                 $compose_title = $this->l10n->t('Compose new post');
133                                 $type = 'post';
134                                 $doesFederate = true;
135
136                                 $contact_allow = $_REQUEST['contact_allow'] ?? '';
137                                 $group_allow = $_REQUEST['group_allow'] ?? '';
138                                 $contact_deny = $_REQUEST['contact_deny'] ?? '';
139                                 $group_deny = $_REQUEST['group_deny'] ?? '';
140
141                                 if ($contact_allow
142                                         . $group_allow
143                                         . $contact_deny
144                                     . $group_deny)
145                                 {
146                                         $contact_allow_list = $contact_allow ? explode(',', $contact_allow) : [];
147                                         $group_allow_list   = $group_allow   ? explode(',', $group_allow)   : [];
148                                         $contact_deny_list  = $contact_deny  ? explode(',', $contact_deny)  : [];
149                                         $group_deny_list    = $group_deny    ? explode(',', $group_deny)    : [];
150                                 }
151
152                                 break;
153                 }
154
155                 $title         = $_REQUEST['title']         ?? '';
156                 $category      = $_REQUEST['category']      ?? '';
157                 $body          = $_REQUEST['body']          ?? '';
158                 $location      = $_REQUEST['location']      ?? $user['default-location'];
159                 $wall          = $_REQUEST['wall']          ?? $type == 'post';
160
161                 $jotplugins = '';
162                 Hook::callAll('jot_tool', $jotplugins);
163
164                 // Output
165                 $this->page->registerFooterScript(Theme::getPathForFile('js/ajaxupload.js'));
166                 $this->page->registerFooterScript(Theme::getPathForFile('js/linkPreview.js'));
167                 $this->page->registerFooterScript(Theme::getPathForFile('js/compose.js'));
168
169                 $contact = Contact::getById($a->getContactId());
170
171                 if ($this->pConfig->get(Session::getLocalUser(), 'system', 'set_creation_date')) {
172                         $created_at = Temporal::getDateTimeField(
173                                 new \DateTime(DBA::NULL_DATETIME),
174                                 new \DateTime('now'),
175                                 null,
176                                 $this->l10n->t('Created at'),
177                                 'created_at'
178                         );
179                 } else {
180                         $created_at = '';
181                 }
182
183                 $tpl = Renderer::getMarkupTemplate('item/compose.tpl');
184                 return Renderer::replaceMacros($tpl, [
185                         '$l10n' => [
186                                 'compose_title'        => $compose_title,
187                                 'default'              => '',
188                                 'visibility_title'     => $this->l10n->t('Visibility'),
189                                 'mytitle'              => $this->l10n->t('This is you'),
190                                 'submit'               => $this->l10n->t('Submit'),
191                                 'edbold'               => $this->l10n->t('Bold'),
192                                 'editalic'             => $this->l10n->t('Italic'),
193                                 'eduline'              => $this->l10n->t('Underline'),
194                                 'edquote'              => $this->l10n->t('Quote'),
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(Session::getLocalUser(),'categories') ? $this->l10n->t('Categories (comma-separated list)') : '',
208                                 'always_open_compose'  => $this->pConfig->get(Session::getLocalUser(), '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                         '$group_allow'  => implode(',', $group_allow_list),
234                         '$contact_deny' => implode(',', $contact_deny_list),
235                         '$group_deny'   => implode(',', $group_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' => $group_allow_list,
242                                 'deny_cid'  => $contact_deny_list,
243                                 'deny_gid'  => $group_deny_list,
244                         ]),
245                 ]);
246         }
247 }