]> git.mxchange.org Git - friendica.git/blob - src/Module/ParseUrl.php
Merge pull request #13128 from annando/owa
[friendica.git] / src / Module / ParseUrl.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Content\Text\BBCode;
27 use Friendica\Core\Hook;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Session\Capability\IHandleUserSessions;
30 use Friendica\Core\System;
31 use Friendica\Network\HTTPException\BadRequestException;
32 use Friendica\Util;
33 use Friendica\Util\Profiler;
34 use Psr\Log\LoggerInterface;
35
36 class ParseUrl extends BaseModule
37 {
38         /** @var IHandleUserSessions */
39         protected $userSession;
40
41         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $userSession, $server, array $parameters = [])
42         {
43                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
44
45                 $this->userSession = $userSession;
46         }
47
48         protected function rawContent(array $request = [])
49         {
50                 if (!$this->userSession->isAuthenticated()) {
51                         throw new \Friendica\Network\HTTPException\ForbiddenException();
52                 }
53
54                 $format = '';
55                 $title = '';
56                 $description = '';
57                 $ret = ['success' => false, 'contentType' => ''];
58
59                 if (!empty($_GET['binurl']) && Util\Strings::isHex($_GET['binurl'])) {
60                         $url = trim(hex2bin($_GET['binurl']));
61                 } elseif (!empty($_GET['url'])) {
62                         $url = trim($_GET['url']);
63                         // fallback in case no url is valid
64                 } else {
65                         throw new BadRequestException('No url given');
66                 }
67
68                 if (!empty($_GET['title'])) {
69                         $title = strip_tags(trim($_GET['title']));
70                 }
71
72                 if (!empty($_GET['description'])) {
73                         $description = strip_tags(trim($_GET['description']));
74                 }
75
76                 if (!empty($_GET['tags'])) {
77                         $arr_tags = Util\ParseUrl::convertTagsToArray($_GET['tags']);
78                         if (count($arr_tags)) {
79                                 $str_tags = "\n" . implode(' ', $arr_tags) . "\n";
80                         }
81                 }
82
83                 if (isset($_GET['format']) && $_GET['format'] == 'json') {
84                         $format = 'json';
85                 }
86
87                 // Add url scheme if it is missing
88                 $arrurl = parse_url($url);
89                 if (empty($arrurl['scheme'])) {
90                         if (!empty($arrurl['host'])) {
91                                 $url = 'http:' . $url;
92                         } else {
93                                 $url = 'http://' . $url;
94                         }
95                 }
96
97                 $arr = ['url' => $url, 'format' => $format, 'text' => null];
98
99                 Hook::callAll('parse_link', $arr);
100
101                 if ($arr['text']) {
102                         if ($format == 'json') {
103                                 System::jsonExit($arr['text']);
104                         } else {
105                                 System::httpExit($arr['text']);
106                         }
107                 }
108
109                 if ($format == 'json') {
110                         $siteinfo = Util\ParseUrl::getSiteinfoCached($url);
111
112                         if (in_array($siteinfo['type'], ['image', 'video', 'audio'])) {
113                                 switch ($siteinfo['type']) {
114                                         case 'video':
115                                                 $content_type = 'video';
116                                                 break;
117                                         case 'audio':
118                                                 $content_type = 'audio';
119                                                 break;
120                                         default:
121                                                 $content_type = 'image';
122                                                 break;
123                                 }
124
125                                 $ret['contentType'] = $content_type;
126                                 $ret['data'] = ['url' => $url];
127                                 $ret['success'] = true;
128                         } else {
129                                 unset($siteinfo['keywords']);
130
131                                 $ret['data'] = $siteinfo;
132                                 $ret['contentType'] = 'attachment';
133                                 $ret['success'] = true;
134                         }
135
136                         System::jsonExit($ret);
137                 } else {
138                         System::httpExit(BBCode::embedURL($url, empty($_GET['noAttachment']), $title, $description, $_GET['tags'] ?? ''));
139                 }
140         }
141 }