]> git.mxchange.org Git - friendica.git/blob - src/Module/Contact/Advanced.php
Merge pull request #9577 from annando/updateprofile
[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                 $nurl        = Strings::normaliseLink($url);
67
68                 $r = DI::dba()->update(
69                         'contact',
70                         [
71                                 'name'        => $name,
72                                 'nick'        => $nick,
73                                 'url'         => $url,
74                                 'nurl'        => $nurl,
75                                 'alias'       => $alias,
76                                 'request'     => $request,
77                                 'confirm'     => $confirm,
78                                 'notify'      => $notify,
79                                 'poll'        => $poll,
80                                 'attag'       => $attag,
81                         ],
82                         ['id' => $contact['id'], 'uid' => local_user()]
83                 );
84
85                 if ($photo) {
86                         DI::logger()->notice('Updating photo.', ['photo' => $photo]);
87
88                         Model\Contact::updateAvatar($contact['id'], $photo, true);
89                 }
90
91                 if (!$r) {
92                         notice(DI::l10n()->t('Contact update failed.'));
93                 }
94
95                 return;
96         }
97
98         public static function content(array $parameters = [])
99         {
100                 $cid = $parameters['id'];
101
102                 $contact = Model\Contact::selectFirst([], ['id' => $cid, 'uid' => local_user()]);
103                 if (empty($contact)) {
104                         throw new BadRequestException(DI::l10n()->t('Contact not found.'));
105                 }
106
107                 Model\Profile::load(DI::app(), "", Model\Contact::getByURL($contact["url"], false));
108
109                 $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.');
110                 $info    = DI::l10n()->t('Please use your browser \'Back\' button <strong>now</strong> if you are uncertain what to do on this page.');
111
112                 $returnaddr = "contact/$cid";
113
114                 // This data is fetched automatically for most networks.
115                 // Editing does only makes sense for mail and feed contacts.
116                 if (!in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
117                         $readonly = 'readonly';
118                 } else {
119                         $readonly = '';
120                 }
121
122                 $tab_str = Contact::getTabsHTML($contact, Contact::TAB_ADVANCED);
123
124                 $tpl = Renderer::getMarkupTemplate('contact/advanced.tpl');
125                 return Renderer::replaceMacros($tpl, [
126                         '$tab_str'           => $tab_str,
127                         '$warning'           => $warning,
128                         '$info'              => $info,
129                         '$returnaddr'        => $returnaddr,
130                         '$return'            => DI::l10n()->t('Return to contact editor'),
131                         '$contact_id'        => $contact['id'],
132                         '$lbl_submit'        => DI::l10n()->t('Submit'),
133
134                         '$name'    => ['name', DI::l10n()->t('Name'), $contact['name'], '', '', $readonly],
135                         '$nick'    => ['nick', DI::l10n()->t('Account Nickname'), $contact['nick'], '', '', $readonly],
136                         '$attag'   => ['attag', DI::l10n()->t('@Tagname - overrides Name/Nickname'), $contact['attag']],
137                         '$url'     => ['url', DI::l10n()->t('Account URL'), $contact['url'], '', '', $readonly],
138                         '$alias'   => ['alias', DI::l10n()->t('Account URL Alias'), $contact['alias'], '', '', $readonly],
139                         '$request' => ['request', DI::l10n()->t('Friend Request URL'), $contact['request'], '', '', $readonly],
140                         'confirm'  => ['confirm', DI::l10n()->t('Friend Confirm URL'), $contact['confirm'], '', '', $readonly],
141                         'notify'   => ['notify', DI::l10n()->t('Notification Endpoint URL'), $contact['notify'], '', '', $readonly],
142                         'poll'     => ['poll', DI::l10n()->t('Poll/Feed URL'), $contact['poll'], '', '', $readonly],
143                         'photo'    => ['photo', DI::l10n()->t('New photo from this URL'), '', '', '', $readonly],
144                 ]);
145         }
146 }