]> git.mxchange.org Git - friendica.git/blob - src/Module/Item/Compose.php
c44e4c61ab8f66eef3afc77276f43569fa2a9d19
[friendica.git] / src / Module / Item / Compose.php
1 <?php
2
3 namespace Friendica\Module\Item;
4
5 use Friendica\BaseModule;
6 use Friendica\Content\Feature;
7 use Friendica\Core\Config;
8 use Friendica\Core\Hook;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Renderer;
11 use Friendica\Database\DBA;
12 use Friendica\Model\Contact;
13 use Friendica\Model\FileTag;
14 use Friendica\Model\Group;
15 use Friendica\Model\Item;
16 use Friendica\Model\User;
17 use Friendica\Module\Login;
18 use Friendica\Network\HTTPException\NotImplementedException;
19 use Friendica\Util\ACLFormatter;
20 use Friendica\Util\Crypto;
21
22 class Compose extends BaseModule
23 {
24         public static function post()
25         {
26                 if (!empty($_REQUEST['body'])) {
27                         $_REQUEST['return'] = 'network';
28                         require_once 'mod/item.php';
29                         item_post(self::getApp());
30                 } else {
31                         notice(L10n::t('Please enter a post body.'));
32                 }
33         }
34
35         public static function content()
36         {
37                 if (!local_user()) {
38                         return Login::form('compose', false);
39                 }
40
41                 $a = self::getApp();
42
43                 if ($a->getCurrentTheme() !== 'frio') {
44                         throw new NotImplementedException(L10n::t('This feature is only available with the frio theme.'));
45                 }
46
47                 /// @TODO Retrieve parameter from router
48                 $posttype = $a->argv[1] ?? Item::PT_ARTICLE;
49                 if (!in_array($posttype, [Item::PT_ARTICLE, Item::PT_PERSONAL_NOTE])) {
50                         switch ($posttype) {
51                                 case 'note':
52                                         $posttype = Item::PT_PERSONAL_NOTE;
53                                         break;
54                                 default:
55                                         $posttype = Item::PT_ARTICLE;
56                                         break;
57                         }
58                 }
59
60                 $user = User::getById(local_user(), ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'hidewall', 'default-location']);
61
62                 /** @var ACLFormatter $aclFormatter */
63                 $aclFormatter = self::getClass(ACLFormatter::class);
64
65                 switch ($posttype) {
66                         case Item::PT_PERSONAL_NOTE:
67                                 $compose_title = L10n::t('Compose new personal note');
68                                 $type = 'note';
69                                 $doesFederate = false;
70                                 $contact_allow = $a->contact['id'];
71                                 $group_allow = '';
72                                 break;
73                         default:
74                                 $compose_title = L10n::t('Compose new post');
75                                 $type = 'post';
76                                 $doesFederate = true;
77                                 $contact_allow = implode(',', $aclFormatter->expand($user['allow_cid']));
78                                 $group_allow = implode(',', $aclFormatter->expand($user['allow_gid'])) ?: Group::FOLLOWERS;
79                                 break;
80                 }
81
82                 $title         = $_REQUEST['title']         ?? '';
83                 $category      = $_REQUEST['category']      ?? '';
84                 $body          = $_REQUEST['body']          ?? '';
85                 $location      = $_REQUEST['location']      ?? $user['default-location'];
86                 $wall          = $_REQUEST['wall']          ?? $type == 'post';
87                 $contact_allow = $_REQUEST['contact_allow'] ?? $contact_allow;
88                 $group_allow   = $_REQUEST['group_allow']   ?? $group_allow;
89                 $contact_deny  = $_REQUEST['contact_deny']  ?? implode(',', $aclFormatter->expand($user['deny_cid']));
90                 $group_deny    = $_REQUEST['group_deny']    ?? implode(',', $aclFormatter->expand($user['deny_gid']));
91                 $visibility    = ($contact_allow . $user['allow_gid'] . $user['deny_cid'] . $user['deny_gid']) ? 'custom' : 'public';
92
93                 $acl_contacts = Contact::selectToArray(['id', 'name', 'addr', 'micro'], ['uid' => local_user(), 'pending' => false, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]]);
94                 array_walk($acl_contacts, function (&$value) {
95                         $value['type'] = 'contact';
96                 });
97
98                 $acl_groups = [
99                         [
100                                 'id' => Group::FOLLOWERS,
101                                 'name' => L10n::t('Followers'),
102                                 'addr' => '',
103                                 'micro' => 'images/twopeople.png',
104                                 'type' => 'group',
105                         ],
106                         [
107                                 'id' => Group::MUTUALS,
108                                 'name' => L10n::t('Mutuals'),
109                                 'addr' => '',
110                                 'micro' => 'images/twopeople.png',
111                                 'type' => 'group',
112                         ]
113                 ];
114                 foreach (Group::getByUserId(local_user()) as $group) {
115                         $acl_groups[] = [
116                                 'id' => $group['id'],
117                                 'name' => $group['name'],
118                                 'addr' => '',
119                                 'micro' => 'images/twopeople.png',
120                                 'type' => 'group',
121                         ];
122                 }
123
124                 $acl = array_merge($acl_groups, $acl_contacts);
125
126                 $jotnets_fields = [];
127                 $mail_enabled = false;
128                 $pubmail_enabled = false;
129                 if (function_exists('imap_open') && !Config::get('system', 'imap_disabled')) {
130                         $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', local_user()]);
131                         if (DBA::isResult($mailacct)) {
132                                 $mail_enabled = true;
133                                 $pubmail_enabled = !empty($mailacct['pubmail']);
134                         }
135                 }
136
137                 if (empty($user['hidewall'])) {
138                         if ($mail_enabled) {
139                                 $jotnets_fields[] = [
140                                         'type' => 'checkbox',
141                                         'field' => [
142                                                 'pubmail_enable',
143                                                 L10n::t('Post to Email'),
144                                                 $pubmail_enabled
145                                         ]
146                                 ];
147                         }
148
149                         Hook::callAll('jot_networks', $jotnets_fields);
150                 }
151
152                 $jotplugins = '';
153                 Hook::callAll('jot_tool', $jotplugins);
154
155                 // Output
156
157                 $a->registerFooterScript('view/js/ajaxupload.js');
158                 $a->registerFooterScript('view/js/linkPreview.js');
159                 $a->registerFooterScript('view/asset/typeahead.js/dist/typeahead.bundle.js');
160                 $a->registerFooterScript('view/theme/frio/frameworks/friendica-tagsinput/friendica-tagsinput.js');
161                 $a->registerStylesheet('view/theme/frio/frameworks/friendica-tagsinput/friendica-tagsinput.css');
162                 $a->registerStylesheet('view/theme/frio/frameworks/friendica-tagsinput/friendica-tagsinput-typeahead.css');
163
164                 $tpl = Renderer::getMarkupTemplate('item/compose-footer.tpl');
165                 $a->page['footer'] .= Renderer::replaceMacros($tpl, [
166                         '$acl_contacts' => $acl_contacts,
167                         '$acl_groups' => $acl_groups,
168                         '$acl' => $acl,
169                 ]);
170
171                 $tpl = Renderer::getMarkupTemplate('item/compose.tpl');
172                 return Renderer::replaceMacros($tpl, [
173                         '$compose_title'=> $compose_title,
174                         '$id'           => 0,
175                         '$posttype'     => $posttype,
176                         '$type'         => $type,
177                         '$wall'         => $wall,
178                         '$default'      => '',
179                         '$mylink'       => $a->removeBaseURL($a->contact['url']),
180                         '$mytitle'      => L10n::t('This is you'),
181                         '$myphoto'      => $a->removeBaseURL($a->contact['thumb']),
182                         '$submit'       => L10n::t('Submit'),
183                         '$edbold'       => L10n::t('Bold'),
184                         '$editalic'     => L10n::t('Italic'),
185                         '$eduline'      => L10n::t('Underline'),
186                         '$edquote'      => L10n::t('Quote'),
187                         '$edcode'       => L10n::t('Code'),
188                         '$edimg'        => L10n::t('Image'),
189                         '$edurl'        => L10n::t('Link'),
190                         '$edattach'     => L10n::t('Link or Media'),
191                         '$prompttext'   => L10n::t('Please enter a image/video/audio/webpage URL:'),
192                         '$preview'      => L10n::t('Preview'),
193                         '$location_set' => L10n::t('Set your location'),
194                         '$location_clear' => L10n::t('Clear the location'),
195                         '$location_unavailable' => L10n::t('Location services are unavailable on your device'),
196                         '$location_disabled' => L10n::t('Location services are disabled. Please check the website\'s permissions on your device'),
197                         '$wait'         => L10n::t('Please wait'),
198                         '$placeholdertitle' => L10n::t('Set title'),
199                         '$placeholdercategory' => (Feature::isEnabled(local_user(),'categories') ? L10n::t('Categories (comma-separated list)') : ''),
200                         '$public_title'  => L10n::t('Public'),
201                         '$public_desc'  => L10n::t('This post will be sent to all your followers and can be seen in the community pages and by anyone with its link.'),
202                         '$custom_title' => L10n::t('Limited/Private'),
203                         '$custom_desc'  => L10n::t('This post will be sent only to the people in the first box, to the exception of the people mentioned in the second box. It won\'t appear anywhere public.'),
204                         '$emailcc'      => L10n::t('CC: email addresses'),
205                         '$title'        => $title,
206                         '$category'     => $category,
207                         '$body'         => $body,
208                         '$location'     => $location,
209                         '$visibility'   => $visibility,
210                         '$contact_allow'=> $contact_allow,
211                         '$group_allow'  => $group_allow,
212                         '$contact_deny' => $contact_deny,
213                         '$group_deny'   => $group_deny,
214                         '$jotplugins'   => $jotplugins,
215                         '$doesFederate' => $doesFederate,
216                         '$jotnets_fields'=> $jotnets_fields,
217                         '$sourceapp'    => L10n::t($a->sourcename),
218                         '$rand_num'     => Crypto::randomDigits(12)
219                 ]);
220         }
221 }