Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
oembed.php
1<?php
2
4
10
11class Oembed extends Parser
12{
13 const OEMBED_TYPE_XML = "text/xml+oembed";
14 const OEMBED_TYPE_JSON ="application/json+oembed";
15
17 protected $metadataType;
18
20 protected $metadataUrl;
21
24
31 public function handle(HtmlDocument $document, HttpClient $httpClient = null)
32 {
33 if(!$this->detectOembedLink($document) || $this->metadataUrl == '')
34 {
35 return;
36 }
37
38 $isHttpClientPassed = true;
39 if(!$httpClient)
40 {
41 $httpClient = $this->initHttpClient();
42 $isHttpClientPassed = false;
43 }
44 $rawMetadata = $this->getRawMetaData($httpClient);
45 // if request was served through http - try to switch to https
46 if(
47 (
48 !$rawMetadata
49 || $httpClient->getStatus() === 403
50 )
51 && mb_strpos($this->metadataUrl, 'http://') === 0)
52 {
53 if(!$isHttpClientPassed)
54 {
55 $httpClient = $this->initHttpClient();
56 }
57 $metadataUrl = str_replace('http://', 'https://', $this->metadataUrl);
58 $rawMetadata = $httpClient->get($metadataUrl);
59 }
60
61 if($rawMetadata === false)
62 {
63 return;
64 }
65
66 $parsedMetadata = $this->parseMetadata($rawMetadata);
67 if($parsedMetadata !== false)
68 {
69 if($this->metadataEncoding <> '' && $document->getEncoding() !== $this->metadataEncoding)
70 {
71 $parsedMetadata = Encoding::convertEncoding($parsedMetadata, $this->metadataEncoding, $document->getEncoding());
72 }
73
74 if($document->getTitle() == '' && !empty($parsedMetadata['title']))
75 {
76 $document->setTitle($parsedMetadata['title']);
77 }
78
79 if($document->getImage() == '' && !empty($parsedMetadata['thumbnail_url']))
80 {
81 $document->setImage($parsedMetadata['thumbnail_url']);
82 }
83
84 if($document->getEmdbed() == '' && !empty($parsedMetadata['html']))
85 {
86 $document->setEmbed($parsedMetadata['html']);
87 }
88
89 if($document->getExtraField('PROVIDER_NAME') == '' && !empty($parsedMetadata['provider_name']))
90 {
91 $document->setExtraField('PROVIDER_NAME', $parsedMetadata['provider_name']);
92 }
93
94 if($document->getExtraField('VIDEO_WIDTH') == '' && !empty($parsedMetadata['width']))
95 {
96 $document->setExtraField('VIDEO_WIDTH', $parsedMetadata['width']);
97 }
98
99 if($document->getExtraField('VIDEO_HEIGHT') == '' && !empty($parsedMetadata['height']))
100 {
101 $document->setExtraField('VIDEO_HEIGHT', $parsedMetadata['height']);
102 }
103 }
104 }
105
110 protected function detectOembedLink(HtmlDocument $document)
111 {
112 preg_match_all('/<link[^>]*rel\s*=\s*["\']?alternate["\']?[^>]*?>/', $document->getHtml(), $linkElements);
113
114 foreach($linkElements[0] as $linkElement)
115 {
116 $typeJson = (strpos($linkElement, $this::OEMBED_TYPE_JSON) !== false);
117 $typeXml = (strpos($linkElement, $this::OEMBED_TYPE_XML) !== false);
118 if($typeJson || $typeXml)
119 {
120 if(preg_match('/href=[\'"](.+?)[\'"]/', $linkElement, $attributes))
121 {
122 $this->metadataType = ($typeJson ? 'json' : 'xml');
123 $this->metadataUrl = htmlspecialcharsback($attributes[1]);
124 return true;
125 }
126 }
127 }
128
129 return false;
130 }
131
136 protected function parseMetadata($rawMetadata)
137 {
138 switch($this->metadataType)
139 {
140 case 'json':
141 return $this->parseJsonMetadata($rawMetadata);
142 break;
143 case 'xml':
144 return $this->parseXmlMetadata($rawMetadata);
145 break;
146 }
147
148 return false;
149 }
150
151 protected function parseJsonMetadata($rawMetadata)
152 {
153 $parsedMetadata = json_decode($rawMetadata, true);
154 $this->metadataEncoding = 'UTF-8';
155
156 return $parsedMetadata;
157 }
158
163 protected function parseXmlMetadata($rawMetadata)
164 {
165 $xml = new \CDataXML();
166 if($xml->LoadString($rawMetadata))
167 {
168 //detect xml encoding
169 if(preg_match('/<\?xml[^>]+?encoding=[\'"](.+?)[\'"]\?>/', $rawMetadata, $matches))
170 $this->metadataEncoding = $matches[1];
171 else
172 $this->metadataEncoding = 'UTF-8';
173
174 $result = array();
175 $dom = $xml->GetTree();
176 $mainNode = $dom->elementsByName('oembed');
177 foreach($mainNode[0]->children as $node)
178 {
179 $result[$node->name] = $node->content;
180 }
181 return $result;
182 }
183
184 return false;
185 }
186
187 protected function getRawMetaData(HttpClient $httpClient)
188 {
189 $rawMetadata = $httpClient->get($this->metadataUrl);
190
191 return $rawMetadata;
192 }
193
194 protected function initHttpClient(): HttpClient
195 {
196 $httpClient = new HttpClient();
197 $httpClient->setTimeout(5);
198 $httpClient->setStreamTimeout(5);
199 $httpClient->setHeader('User-Agent', UrlPreview::USER_AGENT, true);
200 $httpClient->setPrivateIp(false);
201
202 return $httpClient;
203 }
204}
setExtraField($fieldName, $fieldValue)
detectOembedLink(HtmlDocument $document)
Definition oembed.php:110
getRawMetaData(HttpClient $httpClient)
Definition oembed.php:187
handle(HtmlDocument $document, HttpClient $httpClient=null)
Definition oembed.php:31