|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Mood |
|
5
|
|
|
* |
|
6
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
7
|
|
|
* later. See the COPYING file. |
|
8
|
|
|
* |
|
9
|
|
|
* @author Maxence Lange <[email protected]> |
|
10
|
|
|
* @copyright 2017 |
|
11
|
|
|
* @license GNU AGPL version 3 or any later version |
|
12
|
|
|
* |
|
13
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
14
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
15
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
16
|
|
|
* License, or (at your option) any later version. |
|
17
|
|
|
* |
|
18
|
|
|
* This program is distributed in the hope that it will be useful, |
|
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21
|
|
|
* GNU Affero General Public License for more details. |
|
22
|
|
|
* |
|
23
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
24
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25
|
|
|
* |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
namespace OCA\Mood\Service; |
|
30
|
|
|
|
|
31
|
|
|
use OCA\Mood\Exceptions\HttpRequestException; |
|
32
|
|
|
|
|
33
|
|
|
class HttpService { |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
public function __construct() { |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $url |
|
42
|
|
|
* |
|
43
|
|
|
* @return array |
|
44
|
|
|
* @throws \Exception |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getMetaFromWebsite($url) { |
|
47
|
|
|
|
|
48
|
|
|
try { |
|
49
|
|
|
$html = self::file_get_contents_curl($url); |
|
50
|
|
|
$tags = self::getMetaFromHtml($html); |
|
51
|
|
|
|
|
52
|
|
|
$meta = self::fillWithOpenGraph($tags); |
|
53
|
|
|
|
|
54
|
|
|
$meta['url'] = $url; |
|
55
|
|
|
} catch (\Exception $e) { |
|
56
|
|
|
throw $e; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $meta; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $url |
|
65
|
|
|
* @param bool $bin |
|
66
|
|
|
* |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
* @throws HttpRequestException |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function file_get_contents_curl($url, $bin = false) { |
|
71
|
|
|
$ch = curl_init(); |
|
72
|
|
|
|
|
73
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
|
74
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
75
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
76
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
if ($bin === true) { |
|
80
|
|
|
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$data = curl_exec($ch); |
|
84
|
|
|
curl_close($ch); |
|
85
|
|
|
|
|
86
|
|
|
if ($data === false) { |
|
87
|
|
|
throw new HttpRequestException(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $data; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $str |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
public static function getMetaFromHtml($str) { |
|
100
|
|
|
$pattern = '~<\s*meta\s |
|
101
|
|
|
(?=[^>]*? |
|
102
|
|
|
\b(?:name|property|http-equiv)\s*=\s* |
|
103
|
|
|
(?|"\s*([^"]*?)\s*"|\'\s*([^\']*?)\s*\'| |
|
104
|
|
|
([^"\'>]*?)(?=\s*/?\s*>|\s\w+\s*=)) |
|
105
|
|
|
) |
|
106
|
|
|
|
|
107
|
|
|
[^>]*?\bcontent\s*=\s* |
|
108
|
|
|
(?|"\s*([^"]*?)\s*"|\'\s*([^\']*?)\s*\'| |
|
109
|
|
|
([^"\'>]*?)(?=\s*/?\s*>|\s\w+\s*=)) |
|
110
|
|
|
[^>]*> |
|
111
|
|
|
~ix'; |
|
112
|
|
|
|
|
113
|
|
|
if (preg_match_all($pattern, $str, $out)) { |
|
114
|
|
|
return array_combine($out[1], $out[2]); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return array(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param array $tags |
|
123
|
|
|
* |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
public static function fillWithOpenGraph(array $tags) { |
|
127
|
|
|
return [ |
|
128
|
|
|
'title' => self::fillWithOGTitle($tags), |
|
129
|
|
|
'thumb' => self::fillWithOGImage($tags), |
|
130
|
|
|
'description' => self::fillWithOGDescription($tags), |
|
131
|
|
|
'website' => self::fillWithOGSiteName($tags) |
|
132
|
|
|
]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param array $tags |
|
138
|
|
|
* |
|
139
|
|
|
* @return mixed|string |
|
140
|
|
|
*/ |
|
141
|
|
|
private static function fillWithOGTitle(array $tags) { |
|
142
|
|
|
return ((key_exists('og:title', $tags)) ? $tags['og:title'] : ''); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param array $tags |
|
148
|
|
|
* |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
|
|
private static function fillWithOGImage(array $tags) { |
|
152
|
|
|
return ((key_exists('og:image', $tags)) ? $tags['og:image'] : ''); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param array $tags |
|
157
|
|
|
* |
|
158
|
|
|
* @return string |
|
159
|
|
|
*/ |
|
160
|
|
|
private static function fillWithOGDescription(array $tags) { |
|
161
|
|
|
return ((key_exists('og:description', $tags)) ? $tags['og:description'] : ''); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param array $tags |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
private static function fillWithOGSiteName(array $tags) { |
|
170
|
|
|
return ((key_exists('og:site_name', $tags)) ? $tags['og:site_name'] : ''); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
} |