]> git.mxchange.org Git - friendica.git/blob - src/Module/ParseUrl.php
Inverted condition
[friendica.git] / src / Module / ParseUrl.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;
23
24 use Friendica\BaseModule;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Core\Hook;
27 use Friendica\Core\Session;
28 use Friendica\Core\System;
29 use Friendica\Network\HTTPException\BadRequestException;
30 use Friendica\Util;
31
32 class ParseUrl extends BaseModule
33 {
34         public static function rawContent(array $parameters = [])
35         {
36                 if (!Session::isAuthenticated()) {
37                         throw new \Friendica\Network\HTTPException\ForbiddenException();
38                 }
39
40                 $format = '';
41                 $title = '';
42                 $description = '';
43                 $ret = ['success' => false, 'contentType' => ''];
44
45                 if (!empty($_GET['binurl']) && Util\Strings::isHex($_GET['binurl'])) {
46                         $url = trim(hex2bin($_GET['binurl']));
47                 } elseif (!empty($_GET['url'])) {
48                         $url = trim($_GET['url']);
49                         // fallback in case no url is valid
50                 } else {
51                         throw new BadRequestException('No url given');
52                 }
53
54                 if (!empty($_GET['title'])) {
55                         $title = strip_tags(trim($_GET['title']));
56                 }
57
58                 if (!empty($_GET['description'])) {
59                         $description = strip_tags(trim($_GET['description']));
60                 }
61
62                 if (!empty($_GET['tags'])) {
63                         $arr_tags = Util\ParseUrl::convertTagsToArray($_GET['tags']);
64                         if (count($arr_tags)) {
65                                 $str_tags = "\n" . implode(' ', $arr_tags) . "\n";
66                         }
67                 }
68
69                 if (isset($_GET['format']) && $_GET['format'] == 'json') {
70                         $format = 'json';
71                 }
72
73                 // Add url scheme if it is missing
74                 $arrurl = parse_url($url);
75                 if (empty($arrurl['scheme'])) {
76                         if (!empty($arrurl['host'])) {
77                                 $url = 'http:' . $url;
78                         } else {
79                                 $url = 'http://' . $url;
80                         }
81                 }
82
83                 $arr = ['url' => $url, 'format' => $format, 'text' => null];
84
85                 Hook::callAll('parse_link', $arr);
86
87                 if ($arr['text']) {
88                         if ($format == 'json') {
89                                 System::jsonExit($arr['text']);
90                         } else {
91                                 echo $arr['text'];
92                                 exit();
93                         }
94                 }
95
96                 if ($format == 'json') {
97                         $siteinfo = Util\ParseUrl::getSiteinfoCached($url);
98
99                         if (in_array($siteinfo['type'], ['image', 'video', 'audio'])) {
100                                 switch ($siteinfo['type']) {
101                                         case 'video':
102                                                 $content_type = 'video';
103                                                 break;
104                                         case 'audio':
105                                                 $content_type = 'audio';
106                                                 break;
107                                         default:
108                                                 $content_type = 'image';
109                                                 break;
110                                 }
111
112                                 $ret['contentType'] = $content_type;
113                                 $ret['data'] = ['url' => $url];
114                                 $ret['success'] = true;
115                         } else {
116                                 unset($siteinfo['keywords']);
117
118                                 $ret['data'] = $siteinfo;
119                                 $ret['contentType'] = 'attachment';
120                                 $ret['success'] = true;
121                         }
122
123                         System::jsonExit($ret);
124                 } else {
125                         echo BBCode::embedURL($url, empty($_GET['noAttachment']), $title, $description, $_GET['tags'] ?? '');
126                         exit();
127                 }
128         }
129 }