]> git.mxchange.org Git - friendica.git/blob - src/Module/Item/Compose.php
0564e2f9885f22fd305085e3d2a73775ca7c3e50
[friendica.git] / src / Module / Item / Compose.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\BaseModule;
26 use Friendica\Content\Feature;
27 use Friendica\Core\ACL;
28 use Friendica\Core\Hook;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Theme;
31 use Friendica\DI;
32 use Friendica\Model\Contact;
33 use Friendica\Model\Item;
34 use Friendica\Model\User;
35 use Friendica\Module\Security\Login;
36 use Friendica\Network\HTTPException\NotImplementedException;
37 use Friendica\Util\Crypto;
38 use Friendica\Util\DateTimeFormat;
39 use Friendica\Util\Temporal;
40
41 class Compose extends BaseModule
42 {
43         protected function post(array $request = [], array $post = [])
44         {
45                 if (!empty($_REQUEST['body'])) {
46                         $_REQUEST['return'] = 'network';
47                         require_once 'mod/item.php';
48                         item_post(DI::app());
49                 } else {
50                         notice(DI::l10n()->t('Please enter a post body.'));
51                 }
52         }
53
54         protected function content(array $request = []): string
55         {
56                 if (!local_user()) {
57                         return Login::form('compose', false);
58                 }
59
60                 $a = DI::app();
61
62                 if ($a->getCurrentTheme() !== 'frio') {
63                         throw new NotImplementedException(DI::l10n()->t('This feature is only available with the frio theme.'));
64                 }
65
66                 /// @TODO Retrieve parameter from router
67                 $posttype = $this->parameters['type'] ?? Item::PT_ARTICLE;
68                 if (!in_array($posttype, [Item::PT_ARTICLE, Item::PT_PERSONAL_NOTE])) {
69                         switch ($posttype) {
70                                 case 'note':
71                                         $posttype = Item::PT_PERSONAL_NOTE;
72                                         break;
73                                 default:
74                                         $posttype = Item::PT_ARTICLE;
75                                         break;
76                         }
77                 }
78
79                 $user = User::getById(local_user(), ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'default-location']);
80
81                 $aclFormatter = DI::aclFormatter();
82
83                 $contact_allow_list = $aclFormatter->expand($user['allow_cid']);
84                 $group_allow_list   = $aclFormatter->expand($user['allow_gid']);
85                 $contact_deny_list  = $aclFormatter->expand($user['deny_cid']);
86                 $group_deny_list    = $aclFormatter->expand($user['deny_gid']);
87
88                 switch ($posttype) {
89                         case Item::PT_PERSONAL_NOTE:
90                                 $compose_title = DI::l10n()->t('Compose new personal note');
91                                 $type = 'note';
92                                 $doesFederate = false;
93                                 $contact_allow_list = [$a->getContactId()];
94                                 $group_allow_list = [];
95                                 $contact_deny_list = [];
96                                 $group_deny_list = [];
97                                 break;
98                         default:
99                                 $compose_title = DI::l10n()->t('Compose new post');
100                                 $type = 'post';
101                                 $doesFederate = true;
102
103                                 $contact_allow = $_REQUEST['contact_allow'] ?? '';
104                                 $group_allow = $_REQUEST['group_allow'] ?? '';
105                                 $contact_deny = $_REQUEST['contact_deny'] ?? '';
106                                 $group_deny = $_REQUEST['group_deny'] ?? '';
107
108                                 if ($contact_allow
109                                         . $group_allow
110                                         . $contact_deny
111                                     . $group_deny)
112                                 {
113                                         $contact_allow_list = $contact_allow ? explode(',', $contact_allow) : [];
114                                         $group_allow_list   = $group_allow   ? explode(',', $group_allow)   : [];
115                                         $contact_deny_list  = $contact_deny  ? explode(',', $contact_deny)  : [];
116                                         $group_deny_list    = $group_deny    ? explode(',', $group_deny)    : [];
117                                 }
118
119                                 break;
120                 }
121
122                 $title         = $_REQUEST['title']         ?? '';
123                 $category      = $_REQUEST['category']      ?? '';
124                 $body          = $_REQUEST['body']          ?? '';
125                 $location      = $_REQUEST['location']      ?? $user['default-location'];
126                 $wall          = $_REQUEST['wall']          ?? $type == 'post';
127
128                 $jotplugins = '';
129                 Hook::callAll('jot_tool', $jotplugins);
130
131                 // Output
132                 DI::page()->registerFooterScript(Theme::getPathForFile('js/ajaxupload.js'));
133                 DI::page()->registerFooterScript(Theme::getPathForFile('js/linkPreview.js'));
134                 DI::page()->registerFooterScript(Theme::getPathForFile('js/compose.js'));
135
136                 $contact = Contact::getById($a->getContactId());
137
138                 $tpl = Renderer::getMarkupTemplate('item/compose.tpl');
139                 return Renderer::replaceMacros($tpl, [
140                         '$compose_title'=> $compose_title,
141                         '$visibility_title'=> DI::l10n()->t('Visibility'),
142                         '$id'           => 0,
143                         '$posttype'     => $posttype,
144                         '$type'         => $type,
145                         '$wall'         => $wall,
146                         '$default'      => '',
147                         '$mylink'       => DI::baseUrl()->remove($contact['url']),
148                         '$mytitle'      => DI::l10n()->t('This is you'),
149                         '$myphoto'      => DI::baseUrl()->remove($contact['thumb']),
150                         '$submit'       => DI::l10n()->t('Submit'),
151                         '$edbold'       => DI::l10n()->t('Bold'),
152                         '$editalic'     => DI::l10n()->t('Italic'),
153                         '$eduline'      => DI::l10n()->t('Underline'),
154                         '$edquote'      => DI::l10n()->t('Quote'),
155                         '$edcode'       => DI::l10n()->t('Code'),
156                         '$edimg'        => DI::l10n()->t('Image'),
157                         '$edurl'        => DI::l10n()->t('Link'),
158                         '$edattach'     => DI::l10n()->t('Link or Media'),
159                         '$prompttext'   => DI::l10n()->t('Please enter a image/video/audio/webpage URL:'),
160                         '$preview'      => DI::l10n()->t('Preview'),
161                         '$location_set' => DI::l10n()->t('Set your location'),
162                         '$location_clear' => DI::l10n()->t('Clear the location'),
163                         '$location_unavailable' => DI::l10n()->t('Location services are unavailable on your device'),
164                         '$location_disabled' => DI::l10n()->t('Location services are disabled. Please check the website\'s permissions on your device'),
165                         '$wait'         => DI::l10n()->t('Please wait'),
166                         '$placeholdertitle' => DI::l10n()->t('Set title'),
167                         '$placeholdercategory' => (Feature::isEnabled(local_user(),'categories') ? DI::l10n()->t('Categories (comma-separated list)') : ''),
168                         '$scheduled_at' => Temporal::getDateTimeField(
169                                 new DateTime(),
170                                 new DateTime('now + 6 months'),
171                                 null,
172                                 DI::l10n()->t('Scheduled at'),
173                                 'scheduled_at'
174                         ),
175
176                         '$title'        => $title,
177                         '$category'     => $category,
178                         '$body'         => $body,
179                         '$location'     => $location,
180
181                         '$contact_allow'=> implode(',', $contact_allow_list),
182                         '$group_allow'  => implode(',', $group_allow_list),
183                         '$contact_deny' => implode(',', $contact_deny_list),
184                         '$group_deny'   => implode(',', $group_deny_list),
185
186                         '$jotplugins'   => $jotplugins,
187                         '$rand_num'     => Crypto::randomDigits(12),
188                         '$acl_selector'  => ACL::getFullSelectorHTML(DI::page(), $a->getLoggedInUserId(), $doesFederate, [
189                                 'allow_cid' => $contact_allow_list,
190                                 'allow_gid' => $group_allow_list,
191                                 'deny_cid'  => $contact_deny_list,
192                                 'deny_gid'  => $group_deny_list,
193                         ]),
194                 ]);
195         }
196 }