]> git.mxchange.org Git - friendica.git/blob - src/Module/Contact/Advanced.php
e34d6321b64694cf1f0467275f57dda1cc12e231
[friendica.git] / src / Module / Contact / Advanced.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\Contact;
23
24 use Friendica\App;
25 use Friendica\App\Page;
26 use Friendica\BaseModule;
27 use Friendica\Content\Widget;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Protocol;
30 use Friendica\Core\Renderer;
31 use Friendica\Core\Session;
32 use Friendica\Database\Database;
33 use Friendica\Model;
34 use Friendica\Module\Contact;
35 use Friendica\Network\HTTPException\BadRequestException;
36 use Friendica\Network\HTTPException\ForbiddenException;
37 use Friendica\Util\Profiler;
38 use Friendica\Util\Strings;
39 use Psr\Log\LoggerInterface;
40
41 /**
42  * GUI for advanced contact details manipulation
43  */
44 class Advanced extends BaseModule
45 {
46         /** @var Database */
47         protected $dba;
48         /** @var Page */
49         protected $page;
50
51         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, App\Page $page, LoggerInterface $logger, Profiler $profiler, Database $dba, array $server, array $parameters = [])
52         {
53                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $server, $parameters);
54
55                 $this->dba  = $dba;
56                 $this->page = $page;
57
58                 if (!Session::isAuthenticated()) {
59                         throw new ForbiddenException($this->t('Permission denied.'));
60                 }
61         }
62
63         protected function post(array $request = [], array $post = [])
64         {
65                 $cid = $this->parameters['id'];
66
67                 $contact = Model\Contact::selectFirst([], ['id' => $cid, 'uid' => local_user()]);
68                 if (empty($contact)) {
69                         throw new BadRequestException($this->t('Contact not found.'));
70                 }
71
72                 $name        = ($_POST['name'] ?? '') ?: $contact['name'];
73                 $nick        = $_POST['nick'] ?? '';
74                 $url         = $_POST['url'] ?? '';
75                 $alias       = $_POST['alias'] ?? '';
76                 $request     = $_POST['request'] ?? '';
77                 $confirm     = $_POST['confirm'] ?? '';
78                 $notify      = $_POST['notify'] ?? '';
79                 $poll        = $_POST['poll'] ?? '';
80                 $attag       = $_POST['attag'] ?? '';
81                 $photo       = $_POST['photo'] ?? '';
82                 $nurl        = Strings::normaliseLink($url);
83
84                 $r = $this->dba->update(
85                         'contact',
86                         [
87                                 'name'        => $name,
88                                 'nick'        => $nick,
89                                 'url'         => $url,
90                                 'nurl'        => $nurl,
91                                 'alias'       => $alias,
92                                 'request'     => $request,
93                                 'confirm'     => $confirm,
94                                 'notify'      => $notify,
95                                 'poll'        => $poll,
96                                 'attag'       => $attag,
97                         ],
98                         ['id' => $contact['id'], 'uid' => local_user()]
99                 );
100
101                 if ($photo) {
102                         $this->logger->notice('Updating photo.', ['photo' => $photo]);
103
104                         Model\Contact::updateAvatar($contact['id'], $photo, true);
105                 }
106
107                 if (!$r) {
108                         notice($this->t('Contact update failed.'));
109                 }
110         }
111
112         protected function content(array $request = []): string
113         {
114                 $cid = $this->parameters['id'];
115
116                 $contact = Model\Contact::selectFirst([], ['id' => $cid, 'uid' => local_user()]);
117                 if (empty($contact)) {
118                         throw new BadRequestException($this->t('Contact not found.'));
119                 }
120
121                 $this->page['aside'] = Widget\VCard::getHTML($contact);
122
123                 $warning = $this->t('<strong>WARNING: This is highly advanced</strong> and if you enter incorrect information your communications with this contact may stop working.');
124                 $info    = $this->t('Please use your browser \'Back\' button <strong>now</strong> if you are uncertain what to do on this page.');
125
126                 $returnaddr = "contact/$cid";
127
128                 // This data is fetched automatically for most networks.
129                 // Editing does only makes sense for mail and feed contacts.
130                 if (!in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
131                         $readonly = 'readonly';
132                 } else {
133                         $readonly = '';
134                 }
135
136                 $tab_str = Contact::getTabsHTML($contact, Contact::TAB_ADVANCED);
137
138                 $tpl = Renderer::getMarkupTemplate('contact/advanced.tpl');
139                 return Renderer::replaceMacros($tpl, [
140                         '$tab_str'           => $tab_str,
141                         '$warning'           => $warning,
142                         '$info'              => $info,
143                         '$returnaddr'        => $returnaddr,
144                         '$return'            => $this->t('Return to contact editor'),
145                         '$contact_id'        => $contact['id'],
146                         '$lbl_submit'        => $this->t('Submit'),
147
148                         '$name'    => ['name', $this->t('Name'), $contact['name'], '', '', $readonly],
149                         '$nick'    => ['nick', $this->t('Account Nickname'), $contact['nick'], '', '', $readonly],
150                         '$attag'   => ['attag', $this->t('@Tagname - overrides Name/Nickname'), $contact['attag']],
151                         '$url'     => ['url', $this->t('Account URL'), $contact['url'], '', '', $readonly],
152                         '$alias'   => ['alias', $this->t('Account URL Alias'), $contact['alias'], '', '', $readonly],
153                         '$request' => ['request', $this->t('Friend Request URL'), $contact['request'], '', '', $readonly],
154                         'confirm'  => ['confirm', $this->t('Friend Confirm URL'), $contact['confirm'], '', '', $readonly],
155                         'notify'   => ['notify', $this->t('Notification Endpoint URL'), $contact['notify'], '', '', $readonly],
156                         'poll'     => ['poll', $this->t('Poll/Feed URL'), $contact['poll'], '', '', $readonly],
157                         'photo'    => ['photo', $this->t('New photo from this URL'), '', '', '', $readonly],
158                 ]);
159         }
160 }