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