]> git.mxchange.org Git - friendica.git/blob - src/Module/Contact/Advanced.php
Merge pull request #8265 from nupplaphil/task/add_license
[friendica.git] / src / Module / Contact / Advanced.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Contact;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Session;
28 use Friendica\DI;
29 use Friendica\Model;
30 use Friendica\Module\Contact;
31 use Friendica\Network\HTTPException\BadRequestException;
32 use Friendica\Network\HTTPException\ForbiddenException;
33 use Friendica\Util\Strings;
34
35 /**
36  * GUI for advanced contact details manipulation
37  */
38 class Advanced extends BaseModule
39 {
40         public static function init(array $parameters = [])
41         {
42                 if (!Session::isAuthenticated()) {
43                         throw new ForbiddenException(DI::l10n()->t('Permission denied.'));
44                 }
45         }
46
47         public static function post(array $parameters = [])
48         {
49                 $cid = $parameters['id'];
50
51                 $contact = Model\Contact::selectFirst([], ['id' => $cid, 'uid' => local_user()]);
52                 if (empty($contact)) {
53                         throw new BadRequestException(DI::l10n()->t('Contact not found.'));
54                 }
55
56                 $name        = ($_POST['name'] ?? '') ?: $contact['name'];
57                 $nick        = $_POST['nick'] ?? '';
58                 $url         = $_POST['url'] ?? '';
59                 $alias       = $_POST['alias'] ?? '';
60                 $request     = $_POST['request'] ?? '';
61                 $confirm     = $_POST['confirm'] ?? '';
62                 $notify      = $_POST['notify'] ?? '';
63                 $poll        = $_POST['poll'] ?? '';
64                 $attag       = $_POST['attag'] ?? '';
65                 $photo       = $_POST['photo'] ?? '';
66                 $remote_self = $_POST['remote_self'] ?? false;
67                 $nurl        = Strings::normaliseLink($url);
68
69                 $r = DI::dba()->update(
70                         'contact',
71                         [
72                                 'name'        => $name,
73                                 'nick'        => $nick,
74                                 'url'         => $url,
75                                 'nurl'        => $nurl,
76                                 'alias'       => $alias,
77                                 'request'     => $request,
78                                 'confirm'     => $confirm,
79                                 'notify'      => $notify,
80                                 'poll'        => $poll,
81                                 'attag'       => $attag,
82                                 'remote_self' => $remote_self,
83                         ],
84                         ['id' => $contact['id'], 'uid' => local_user()]
85                 );
86
87                 if ($photo) {
88                         DI::logger()->notice('Updating photo.', ['photo' => $photo]);
89
90                         Model\Contact::updateAvatar($photo, local_user(), $contact['id'], true);
91                 }
92
93                 if ($r) {
94                         info(DI::l10n()->t('Contact settings applied.') . EOL);
95                 } else {
96                         notice(DI::l10n()->t('Contact update failed.') . EOL);
97                 }
98
99                 return;
100         }
101
102         public static function content(array $parameters = [])
103         {
104                 $cid = $parameters['id'];
105
106                 $contact = Model\Contact::selectFirst([], ['id' => $cid, 'uid' => local_user()]);
107                 if (empty($contact)) {
108                         throw new BadRequestException(DI::l10n()->t('Contact not found.'));
109                 }
110
111                 Model\Profile::load(DI::app(), "", Model\Contact::getDetailsByURL($contact["url"]));
112
113                 $warning = DI::l10n()->t('<strong>WARNING: This is highly advanced</strong> and if you enter incorrect information your communications with this contact may stop working.');
114                 $info    = DI::l10n()->t('Please use your browser \'Back\' button <strong>now</strong> if you are uncertain what to do on this page.');
115
116                 $returnaddr = "contact/$cid";
117
118                 // Disable remote self for everything except feeds.
119                 // There is an issue when you repeat an item from maybe twitter and you got comments from friendica and twitter
120                 // Problem is, you couldn't reply to both networks.
121                 $allow_remote_self = in_array($contact['network'], [Protocol::FEED, Protocol::DFRN, Protocol::DIASPORA, Protocol::TWITTER])
122                                      && DI::config()->get('system', 'allow_users_remote_self');
123
124                 if ($contact['network'] == Protocol::FEED) {
125                         $remote_self_options = ['0' => DI::l10n()->t('No mirroring'), '1' => DI::l10n()->t('Mirror as forwarded posting'), '2' => DI::l10n()->t('Mirror as my own posting')];
126                 } else {
127                         $remote_self_options = ['0' => DI::l10n()->t('No mirroring'), '2' => DI::l10n()->t('Mirror as my own posting')];
128                 }
129
130                 $tab_str = Contact::getTabsHTML(DI::app(), $contact, 6);
131
132                 $tpl = Renderer::getMarkupTemplate('contact/advanced.tpl');
133                 return Renderer::replaceMacros($tpl, [
134                         '$tab_str'           => $tab_str,
135                         '$warning'           => $warning,
136                         '$info'              => $info,
137                         '$returnaddr'        => $returnaddr,
138                         '$return'            => DI::l10n()->t('Return to contact editor'),
139                         '$update_profile'    => in_array($contact['network'], Protocol::FEDERATED),
140                         '$udprofilenow'      => DI::l10n()->t('Refetch contact data'),
141                         '$contact_id'        => $contact['id'],
142                         '$lbl_submit'        => DI::l10n()->t('Submit'),
143                         '$label_remote_self' => DI::l10n()->t('Remote Self'),
144                         '$allow_remote_self' => $allow_remote_self,
145                         '$remote_self'       => ['remote_self',
146                                 DI::l10n()->t('Mirror postings from this contact'),
147                                 $contact['remote_self'],
148                                 DI::l10n()->t('Mark this contact as remote_self, this will cause friendica to repost new entries from this contact.'),
149                                 $remote_self_options
150                         ],
151
152                         '$name'    => ['name', DI::l10n()->t('Name'), $contact['name']],
153                         '$nick'    => ['nick', DI::l10n()->t('Account Nickname'), $contact['nick']],
154                         '$attag'   => ['attag', DI::l10n()->t('@Tagname - overrides Name/Nickname'), $contact['attag']],
155                         '$url'     => ['url', DI::l10n()->t('Account URL'), $contact['url']],
156                         '$alias'   => ['alias', DI::l10n()->t('Account URL Alias'), $contact['alias']],
157                         '$request' => ['request', DI::l10n()->t('Friend Request URL'), $contact['request']],
158                         'confirm'  => ['confirm', DI::l10n()->t('Friend Confirm URL'), $contact['confirm']],
159                         'notify'   => ['notify', DI::l10n()->t('Notification Endpoint URL'), $contact['notify']],
160                         'poll'     => ['poll', DI::l10n()->t('Poll/Feed URL'), $contact['poll']],
161                         'photo'    => ['photo', DI::l10n()->t('New photo from this URL'), ''],
162                 ]);
163         }
164 }