Passed
Push — master ( c4fb07...e50892 )
by Ismayil
06:01 queued 03:26
created
src/hypeJunction/Parser.php 1 patch
Indentation   +490 added lines, -490 removed lines patch added patch discarded remove patch
@@ -12,500 +12,500 @@
 block discarded – undo
12 12
  */
13 13
 class Parser {
14 14
 
15
-	/**
16
-	 * @var ClientInterface
17
-	 */
18
-	private $client;
19
-
20
-	/**
21
-	 * @var array
22
-	 */
23
-	private static $cache;
24
-
25
-	/**
26
-	 * Constructor
27
-	 * @param ClientInterface $client HTTP Client
28
-	 */
29
-	public function __construct(ClientInterface $client) {
30
-		$this->client = $client;
31
-	}
32
-
33
-	/**
34
-	 * Parses a URL into a an array of metatags
35
-	 *
36
-	 * @param string $url URL to parse
37
-	 * @return array
38
-	 */
39
-	public function parse($url = '') {
40
-
41
-		$data = $this->getImageData($url);
42
-		if (!$data) {
43
-			$data = $this->getOEmbedData($url);
44
-		}
45
-		if (!$data) {
46
-			$data = $this->getDOMData($url);
47
-			if (is_array($data) && !empty($data['oembed_url'])) {
48
-				foreach ($data['oembed_url'] as $oembed_url) {
49
-					$oembed_data = $this->parse($oembed_url);
50
-					if (!empty($oembed_data) && is_array($oembed_data)) {
51
-						$oembed_data['oembed_url'] = $oembed_data['url'];
52
-						unset($oembed_data['url']);
53
-						$data = array_merge($data, $oembed_data);
54
-					}
55
-				}
56
-			}
57
-		}
15
+    /**
16
+     * @var ClientInterface
17
+     */
18
+    private $client;
19
+
20
+    /**
21
+     * @var array
22
+     */
23
+    private static $cache;
24
+
25
+    /**
26
+     * Constructor
27
+     * @param ClientInterface $client HTTP Client
28
+     */
29
+    public function __construct(ClientInterface $client) {
30
+        $this->client = $client;
31
+    }
32
+
33
+    /**
34
+     * Parses a URL into a an array of metatags
35
+     *
36
+     * @param string $url URL to parse
37
+     * @return array
38
+     */
39
+    public function parse($url = '') {
40
+
41
+        $data = $this->getImageData($url);
42
+        if (!$data) {
43
+            $data = $this->getOEmbedData($url);
44
+        }
45
+        if (!$data) {
46
+            $data = $this->getDOMData($url);
47
+            if (is_array($data) && !empty($data['oembed_url'])) {
48
+                foreach ($data['oembed_url'] as $oembed_url) {
49
+                    $oembed_data = $this->parse($oembed_url);
50
+                    if (!empty($oembed_data) && is_array($oembed_data)) {
51
+                        $oembed_data['oembed_url'] = $oembed_data['url'];
52
+                        unset($oembed_data['url']);
53
+                        $data = array_merge($data, $oembed_data);
54
+                    }
55
+                }
56
+            }
57
+        }
58 58
 	
59
-		if (!is_array($data)) {
60
-			$data = array();
61
-		}
62
-
63
-		if (empty($data['thumbnail_url']) && !empty($data['thumbnails'])) {
64
-			$data['thumbnail_url'] = $data['thumbnails'][0];
65
-		}
66
-
67
-		return $data;
68
-	}
69
-
70
-	/**
71
-	 * Parses image metatags
72
-	 *
73
-	 * @param string $url URL of the image
74
-	 * @return array|false
75
-	 */
76
-	public function getImageData($url = '') {
77
-		if (!$this->isImage($url)) {
78
-			return false;
79
-		}
80
-
81
-		return array(
82
-			'type' => 'photo',
83
-			'url' => $url,
84
-			'thumbnails' => array($url),
85
-		);
86
-	}
87
-
88
-	/**
89
-	 * Parses OEmbed data
90
-	 *
91
-	 * @param  string $url URL of the image
92
-	 * @return array|false
93
-	 */
94
-	public function getOEmbedData($url = '') {
95
-
96
-		if (!$this->isJSON($url) && !$this->isXML($url)) {
97
-			return false;
98
-		}
99
-
100
-		$meta = array(
101
-			'url' => $url,
102
-		);
103
-
104
-		$content = $this->read($url);
105
-		if (!$content) {
106
-			return $meta;
107
-		}
108
-
109
-		$data = new \stdClass();
110
-		if ($this->isJSON($url)) {
111
-			$data = json_decode($content);
112
-		} else if ($this->isXML($url)) {
113
-			$data = simplexml_load_string($content);
114
-		}
115
-
116
-		$props = array(
117
-			'type',
118
-			'version',
119
-			'title',
120
-			'author_name',
121
-			'author_url',
122
-			'provider_name',
123
-			'provider_url',
124
-			'cache_age',
125
-			'thumbnail_url',
126
-			'thumbnail_width',
127
-			'thumbnail_height',
128
-			'width',
129
-			'height',
130
-			'html',
131
-		);
132
-		foreach ($props as $key) {
133
-			if (!empty($data->$key)) {
134
-				$meta[$key] = (string) $data->$key;
135
-			}
136
-		}
137
-		return $meta;
138
-	}
139
-
140
-	/**
141
-	 * Parses metatags from DOM
142
-	 *
143
-	 * @param  string $url URL
144
-	 * @return array|false
145
-	 */
146
-	public function getDOMData($url = '') {
147
-
148
-		if (!$this->isHTML($url)) {
149
-			return false;
150
-		}
151
-
152
-		$doc = $this->getDOM($url);
153
-		if (!$doc) {
154
-			return false;
155
-		}
59
+        if (!is_array($data)) {
60
+            $data = array();
61
+        }
62
+
63
+        if (empty($data['thumbnail_url']) && !empty($data['thumbnails'])) {
64
+            $data['thumbnail_url'] = $data['thumbnails'][0];
65
+        }
66
+
67
+        return $data;
68
+    }
69
+
70
+    /**
71
+     * Parses image metatags
72
+     *
73
+     * @param string $url URL of the image
74
+     * @return array|false
75
+     */
76
+    public function getImageData($url = '') {
77
+        if (!$this->isImage($url)) {
78
+            return false;
79
+        }
80
+
81
+        return array(
82
+            'type' => 'photo',
83
+            'url' => $url,
84
+            'thumbnails' => array($url),
85
+        );
86
+    }
87
+
88
+    /**
89
+     * Parses OEmbed data
90
+     *
91
+     * @param  string $url URL of the image
92
+     * @return array|false
93
+     */
94
+    public function getOEmbedData($url = '') {
95
+
96
+        if (!$this->isJSON($url) && !$this->isXML($url)) {
97
+            return false;
98
+        }
99
+
100
+        $meta = array(
101
+            'url' => $url,
102
+        );
103
+
104
+        $content = $this->read($url);
105
+        if (!$content) {
106
+            return $meta;
107
+        }
108
+
109
+        $data = new \stdClass();
110
+        if ($this->isJSON($url)) {
111
+            $data = json_decode($content);
112
+        } else if ($this->isXML($url)) {
113
+            $data = simplexml_load_string($content);
114
+        }
115
+
116
+        $props = array(
117
+            'type',
118
+            'version',
119
+            'title',
120
+            'author_name',
121
+            'author_url',
122
+            'provider_name',
123
+            'provider_url',
124
+            'cache_age',
125
+            'thumbnail_url',
126
+            'thumbnail_width',
127
+            'thumbnail_height',
128
+            'width',
129
+            'height',
130
+            'html',
131
+        );
132
+        foreach ($props as $key) {
133
+            if (!empty($data->$key)) {
134
+                $meta[$key] = (string) $data->$key;
135
+            }
136
+        }
137
+        return $meta;
138
+    }
139
+
140
+    /**
141
+     * Parses metatags from DOM
142
+     *
143
+     * @param  string $url URL
144
+     * @return array|false
145
+     */
146
+    public function getDOMData($url = '') {
147
+
148
+        if (!$this->isHTML($url)) {
149
+            return false;
150
+        }
151
+
152
+        $doc = $this->getDOM($url);
153
+        if (!$doc) {
154
+            return false;
155
+        }
156 156
 		
157
-		$defaults = array(
158
-			'url' => $url,
159
-		);
157
+        $defaults = array(
158
+            'url' => $url,
159
+        );
160 160
 
161
-		$link_tags = $this->parseLinkTags($doc);
162
-		$meta_tags = $this->parseMetaTags($doc);
163
-		$img_tags = $this->parseImgTags($doc);
161
+        $link_tags = $this->parseLinkTags($doc);
162
+        $meta_tags = $this->parseMetaTags($doc);
163
+        $img_tags = $this->parseImgTags($doc);
164 164
 
165
-		$meta = array_merge_recursive($defaults, $link_tags, $meta_tags, $img_tags);
165
+        $meta = array_merge_recursive($defaults, $link_tags, $meta_tags, $img_tags);
166 166
 		
167
-		if (empty($meta['title'])) {
168
-			$meta['title'] = $this->parseTitle($doc);
169
-		}
170
-
171
-
172
-		return $meta;
173
-	}
174
-
175
-	/**
176
-	 * Check if URL exists and is reachable by making an HTTP request to retrieve header information
177
-	 *
178
-	 * @param string $url URL of the resource
179
-	 * @return boolean
180
-	 */
181
-	public function exists($url = '') {
182
-		$response = $this->request($url);
183
-		if ($response instanceof Response) {
184
-			return $response->getStatusCode() == 200;
185
-		}
186
-		return false;
187
-	}
188
-
189
-	/**
190
-	 * Returns head of the resource
191
-	 *
192
-	 * @param string $url URL of the resource
193
-	 * @return Response|false
194
-	 */
195
-	public function request($url = '') {
196
-		if (!filter_var($url, FILTER_VALIDATE_URL)) {
197
-			return false;
198
-		}
199
-		if (!isset(self::$cache[$url])) {
200
-			try {
201
-				$response = $this->client->request('GET', $url);
202
-			} catch (Exception $e) {
203
-				$response = false;
204
-				error_log("Parser Error for HEAD request ($url): {$e->getMessage()}");
205
-			}
206
-			self::$cache[$url] = $response;
207
-		}
208
-
209
-		return self::$cache[$url];
210
-	}
211
-
212
-	/**
213
-	 * Get contents of the page
214
-	 *
215
-	 * @param string $url URL of the resource
216
-	 * @return string
217
-	 */
218
-	public function read($url = '') {
219
-		$body = '';
220
-		if (!$this->exists($url)) {
221
-			return $body;
222
-		}
223
-
224
-		$response = $this->request($url);
225
-		$body = (string) $response->getBody();
226
-		return $body;
227
-	}
228
-
229
-	/**
230
-	 * Checks if resource is an html page
231
-	 *
232
-	 * @param string $url URL of the resource
233
-	 * @return boolean
234
-	 */
235
-	public function isHTML($url = '') {
236
-		$mime = $this->getContentType($url);
237
-		return strpos($mime, 'text/html') !== false;
238
-	}
239
-
240
-	/**
241
-	 * Checks if resource is JSON
242
-	 *
243
-	 * @param string $url URL of the resource
244
-	 * @return boolean
245
-	 */
246
-	public function isJSON($url = '') {
247
-		$mime = $this->getContentType($url);
248
-		return strpos($mime, 'json') !== false;
249
-	}
250
-
251
-	/**
252
-	 * Checks if resource is XML
253
-	 *
254
-	 * @param string $url URL of the resource
255
-	 * @return boolean
256
-	 */
257
-	public function isXML($url = '') {
258
-		$mime = $this->getContentType($url);
259
-		return strpos($mime, 'xml') !== false;
260
-	}
261
-
262
-	/**
263
-	 * Checks if resource is an image
264
-	 *
265
-	 * @param string $url URL of the resource
266
-	 * @return boolean
267
-	 */
268
-	public function isImage($url = '') {
269
-		$mime = $this->getContentType($url);
270
-		if ($mime) {
271
-			list($simple,) = explode('/', $mime);
272
-			return ($simple == 'image');
273
-		}
274
-
275
-		return false;
276
-	}
277
-
278
-	/**
279
-	 * Get mime type of the URL content
280
-	 *
281
-	 * @param string $url URL of the resource
282
-	 * @return string
283
-	 */
284
-	public function getContentType($url = '') {
285
-		$response = $this->request($url);
286
-		if ($response instanceof Response) {
287
-			$header = $response->getHeader('Content-Type');
288
-			if (is_array($header) && !empty($header)) {
289
-				$parts = explode(';', $header[0]);
290
-				return trim($parts[0]);
291
-			}
292
-		}
293
-		return '';
294
-	}
295
-
296
-	/**
297
-	 * Returns HTML contents of the page
298
-	 *
299
-	 * @param string $url URL of the resource
300
-	 * @return string
301
-	 */
302
-	public function getHTML($url = '') {
303
-		if (!$this->isHTML($url)) {
304
-			return '';
305
-		}
306
-		return $this->read($url);
307
-	}
308
-
309
-	/**
310
-	 * Returns HTML contents of the page as a DOMDocument
311
-	 *
312
-	 * @param string $url URL of the resource
313
-	 * @return DOMDocument|false
314
-	 */
315
-	public function getDOM($url = '') {
316
-		$html = $this->getHTML($url);
317
-		if (empty($html)) {
318
-			return false;
319
-		}
320
-		$doc = new DOMDocument();
321
-		$doc->loadHTML($html);
322
-		if (!$doc->documentURI) {
323
-			$doc->documentURI = $url;
324
-		}
325
-		return $doc;
326
-	}
327
-
328
-	/**
329
-	 * Parses document title
330
-	 *
331
-	 * @param DOMDocument $doc Document
332
-	 * @return string
333
-	 */
334
-	public function parseTitle(DOMDocument $doc) {
335
-		$node = $doc->getElementsByTagName('title');
336
-		$title = $node->item(0)->nodeValue;
337
-		return ($title) ?: '';
338
-	}
339
-
340
-	/**
341
-	 * Parses <link> tags
342
-	 *
343
-	 * @param DOMDocument $doc Document
344
-	 * @return array
345
-	 */
346
-	public function parseLinkTags(DOMDocument $doc) {
347
-
348
-		$meta = array();
349
-
350
-		$nodes = $doc->getElementsByTagName('link');
351
-		foreach ($nodes as $node) {
352
-			$rel = $node->getAttribute('rel');
353
-			$href = $node->getAttribute('href');
354
-
355
-			switch ($rel) {
356
-
357
-				case 'icon' :
358
-					$meta['icons'][] = $this->getAbsoluteURL($doc, $href);
359
-					break;
360
-
361
-				case 'canonical' :
362
-					$meta['canonical'] = $this->getAbsoluteURL($doc, $href);
363
-					break;
364
-
365
-				case 'alternate' :
366
-					$type = $node->getAttribute('type');
367
-					if (in_array($type, array(
368
-								'application/json+oembed',
369
-								'text/json+oembed',
370
-								'application/xml+oembed',
371
-								'text/xml+oembed'
372
-							))) {
373
-						$meta['oembed_url'][] = $this->getAbsoluteURL($doc, $href);
374
-					}
375
-					break;
376
-			}
377
-		}
378
-
379
-		return $meta;
380
-	}
381
-
382
-	/**
383
-	 * Parses <meta> tags
384
-	 *
385
-	 * @param DOMDocument $doc Document
386
-	 * @return array
387
-	 */
388
-	public function parseMetaTags(DOMDocument $doc) {
389
-
390
-		$meta = array();
391
-
392
-		$nodes = $doc->getElementsByTagName('meta');
393
-		if (!empty($nodes)) {
394
-			foreach ($nodes as $node) {
395
-				$name = $node->getAttribute('name');
396
-				if (!$name) {
397
-					$name = $node->getAttribute('property');
398
-				}
399
-				if (!$name) {
400
-					continue;
401
-				}
402
-
403
-				$name = strtolower($name);
404
-
405
-				$content = $node->getAttribute('content');
406
-				if (isset($meta['metatags'][$name])) {
407
-					if (!is_array($meta['metatags'][$name])) {
408
-						$meta['metatags'][$name] = array($meta['metatags'][$name]);
409
-					}
410
-					$meta['metatags'][$name][] = $content;
411
-				} else {
412
-					$meta['metatags'][$name] = $content;
413
-				}
414
-
415
-				switch ($name) {
416
-
417
-					case 'title' :
418
-					case 'og:title' :
419
-					case 'twitter:title' :
420
-						if (empty($meta['title'])) {
421
-							$meta['title'] = $content;
422
-						}
423
-						break;
424
-
425
-					case 'og:type' :
426
-						if (empty($meta['type'])) {
427
-							$meta['type'] = $content;
428
-						}
429
-						break;
430
-
431
-					case 'description' :
432
-					case 'og:description' :
433
-					case 'twitter:description' :
434
-						if (empty($meta['description'])) {
435
-							$meta['description'] = $content;
436
-						}
437
-						break;
438
-
439
-					case 'keywords' :
440
-						if (is_string($content)) {
441
-							$content = explode(',', $content);
442
-							$content = array_map('trim', $content);
443
-						}
444
-						$meta['tags'] = $content;
445
-						break;
446
-
447
-					case 'og:site_name' :
448
-					case 'twitter:site' :
449
-						if (empty($meta['provider_name'])) {
450
-							$meta['provider_name'] = $content;
451
-						}
452
-						break;
453
-
454
-					case 'og:image' :
455
-					case 'twitter:image' :
456
-						$meta['thumbnails'][] = $this->getAbsoluteURL($doc, $content);
457
-						break;
458
-				}
459
-			}
460
-		}
461
-
462
-		return $meta;
463
-	}
464
-
465
-	/**
466
-	 * Parses <img> tags
467
-	 *
468
-	 * @param DOMDocument $doc Document
469
-	 * @return array
470
-	 */
471
-	public function parseImgTags(DOMDocument $doc) {
472
-
473
-		$meta = array();
474
-
475
-		$nodes = $doc->getElementsByTagName('img');
476
-		foreach ($nodes as $node) {
477
-			$src = $node->getAttribute('src');
478
-			$meta['thumbnails'][] = $this->getAbsoluteURL($doc, $src);
479
-		}
480
-
481
-		return $meta;
482
-	}
483
-
484
-	/**
485
-	 * Normalizes relative URLs
486
-	 *
487
-	 * @param DOMDocument $doc  Document
488
-	 * @param string      $href URL to normalize
489
-	 * @return string
490
-	 */
491
-	public function getAbsoluteURL(DOMDocument $doc, $href = '') {
492
-
493
-		// Check if $url is absolute
494
-		if (parse_url($href, PHP_URL_HOST)) {
495
-			return $href;
496
-		}
497
-
498
-		$uri = trim($doc->documentURI ?: '', '/');
499
-
500
-		// Check if $url is relative to root
501
-		if (substr($href, 0, 1) === "/") {
502
-			$scheme = parse_url($uri, PHP_URL_SCHEME);
503
-			$host = parse_url($uri, PHP_URL_HOST);
504
-			return "$scheme://$host$href";
505
-		}
506
-
507
-		// $url is relative to page
508
-		return "$uri/$href";
509
-	}
167
+        if (empty($meta['title'])) {
168
+            $meta['title'] = $this->parseTitle($doc);
169
+        }
170
+
171
+
172
+        return $meta;
173
+    }
174
+
175
+    /**
176
+     * Check if URL exists and is reachable by making an HTTP request to retrieve header information
177
+     *
178
+     * @param string $url URL of the resource
179
+     * @return boolean
180
+     */
181
+    public function exists($url = '') {
182
+        $response = $this->request($url);
183
+        if ($response instanceof Response) {
184
+            return $response->getStatusCode() == 200;
185
+        }
186
+        return false;
187
+    }
188
+
189
+    /**
190
+     * Returns head of the resource
191
+     *
192
+     * @param string $url URL of the resource
193
+     * @return Response|false
194
+     */
195
+    public function request($url = '') {
196
+        if (!filter_var($url, FILTER_VALIDATE_URL)) {
197
+            return false;
198
+        }
199
+        if (!isset(self::$cache[$url])) {
200
+            try {
201
+                $response = $this->client->request('GET', $url);
202
+            } catch (Exception $e) {
203
+                $response = false;
204
+                error_log("Parser Error for HEAD request ($url): {$e->getMessage()}");
205
+            }
206
+            self::$cache[$url] = $response;
207
+        }
208
+
209
+        return self::$cache[$url];
210
+    }
211
+
212
+    /**
213
+     * Get contents of the page
214
+     *
215
+     * @param string $url URL of the resource
216
+     * @return string
217
+     */
218
+    public function read($url = '') {
219
+        $body = '';
220
+        if (!$this->exists($url)) {
221
+            return $body;
222
+        }
223
+
224
+        $response = $this->request($url);
225
+        $body = (string) $response->getBody();
226
+        return $body;
227
+    }
228
+
229
+    /**
230
+     * Checks if resource is an html page
231
+     *
232
+     * @param string $url URL of the resource
233
+     * @return boolean
234
+     */
235
+    public function isHTML($url = '') {
236
+        $mime = $this->getContentType($url);
237
+        return strpos($mime, 'text/html') !== false;
238
+    }
239
+
240
+    /**
241
+     * Checks if resource is JSON
242
+     *
243
+     * @param string $url URL of the resource
244
+     * @return boolean
245
+     */
246
+    public function isJSON($url = '') {
247
+        $mime = $this->getContentType($url);
248
+        return strpos($mime, 'json') !== false;
249
+    }
250
+
251
+    /**
252
+     * Checks if resource is XML
253
+     *
254
+     * @param string $url URL of the resource
255
+     * @return boolean
256
+     */
257
+    public function isXML($url = '') {
258
+        $mime = $this->getContentType($url);
259
+        return strpos($mime, 'xml') !== false;
260
+    }
261
+
262
+    /**
263
+     * Checks if resource is an image
264
+     *
265
+     * @param string $url URL of the resource
266
+     * @return boolean
267
+     */
268
+    public function isImage($url = '') {
269
+        $mime = $this->getContentType($url);
270
+        if ($mime) {
271
+            list($simple,) = explode('/', $mime);
272
+            return ($simple == 'image');
273
+        }
274
+
275
+        return false;
276
+    }
277
+
278
+    /**
279
+     * Get mime type of the URL content
280
+     *
281
+     * @param string $url URL of the resource
282
+     * @return string
283
+     */
284
+    public function getContentType($url = '') {
285
+        $response = $this->request($url);
286
+        if ($response instanceof Response) {
287
+            $header = $response->getHeader('Content-Type');
288
+            if (is_array($header) && !empty($header)) {
289
+                $parts = explode(';', $header[0]);
290
+                return trim($parts[0]);
291
+            }
292
+        }
293
+        return '';
294
+    }
295
+
296
+    /**
297
+     * Returns HTML contents of the page
298
+     *
299
+     * @param string $url URL of the resource
300
+     * @return string
301
+     */
302
+    public function getHTML($url = '') {
303
+        if (!$this->isHTML($url)) {
304
+            return '';
305
+        }
306
+        return $this->read($url);
307
+    }
308
+
309
+    /**
310
+     * Returns HTML contents of the page as a DOMDocument
311
+     *
312
+     * @param string $url URL of the resource
313
+     * @return DOMDocument|false
314
+     */
315
+    public function getDOM($url = '') {
316
+        $html = $this->getHTML($url);
317
+        if (empty($html)) {
318
+            return false;
319
+        }
320
+        $doc = new DOMDocument();
321
+        $doc->loadHTML($html);
322
+        if (!$doc->documentURI) {
323
+            $doc->documentURI = $url;
324
+        }
325
+        return $doc;
326
+    }
327
+
328
+    /**
329
+     * Parses document title
330
+     *
331
+     * @param DOMDocument $doc Document
332
+     * @return string
333
+     */
334
+    public function parseTitle(DOMDocument $doc) {
335
+        $node = $doc->getElementsByTagName('title');
336
+        $title = $node->item(0)->nodeValue;
337
+        return ($title) ?: '';
338
+    }
339
+
340
+    /**
341
+     * Parses <link> tags
342
+     *
343
+     * @param DOMDocument $doc Document
344
+     * @return array
345
+     */
346
+    public function parseLinkTags(DOMDocument $doc) {
347
+
348
+        $meta = array();
349
+
350
+        $nodes = $doc->getElementsByTagName('link');
351
+        foreach ($nodes as $node) {
352
+            $rel = $node->getAttribute('rel');
353
+            $href = $node->getAttribute('href');
354
+
355
+            switch ($rel) {
356
+
357
+                case 'icon' :
358
+                    $meta['icons'][] = $this->getAbsoluteURL($doc, $href);
359
+                    break;
360
+
361
+                case 'canonical' :
362
+                    $meta['canonical'] = $this->getAbsoluteURL($doc, $href);
363
+                    break;
364
+
365
+                case 'alternate' :
366
+                    $type = $node->getAttribute('type');
367
+                    if (in_array($type, array(
368
+                                'application/json+oembed',
369
+                                'text/json+oembed',
370
+                                'application/xml+oembed',
371
+                                'text/xml+oembed'
372
+                            ))) {
373
+                        $meta['oembed_url'][] = $this->getAbsoluteURL($doc, $href);
374
+                    }
375
+                    break;
376
+            }
377
+        }
378
+
379
+        return $meta;
380
+    }
381
+
382
+    /**
383
+     * Parses <meta> tags
384
+     *
385
+     * @param DOMDocument $doc Document
386
+     * @return array
387
+     */
388
+    public function parseMetaTags(DOMDocument $doc) {
389
+
390
+        $meta = array();
391
+
392
+        $nodes = $doc->getElementsByTagName('meta');
393
+        if (!empty($nodes)) {
394
+            foreach ($nodes as $node) {
395
+                $name = $node->getAttribute('name');
396
+                if (!$name) {
397
+                    $name = $node->getAttribute('property');
398
+                }
399
+                if (!$name) {
400
+                    continue;
401
+                }
402
+
403
+                $name = strtolower($name);
404
+
405
+                $content = $node->getAttribute('content');
406
+                if (isset($meta['metatags'][$name])) {
407
+                    if (!is_array($meta['metatags'][$name])) {
408
+                        $meta['metatags'][$name] = array($meta['metatags'][$name]);
409
+                    }
410
+                    $meta['metatags'][$name][] = $content;
411
+                } else {
412
+                    $meta['metatags'][$name] = $content;
413
+                }
414
+
415
+                switch ($name) {
416
+
417
+                    case 'title' :
418
+                    case 'og:title' :
419
+                    case 'twitter:title' :
420
+                        if (empty($meta['title'])) {
421
+                            $meta['title'] = $content;
422
+                        }
423
+                        break;
424
+
425
+                    case 'og:type' :
426
+                        if (empty($meta['type'])) {
427
+                            $meta['type'] = $content;
428
+                        }
429
+                        break;
430
+
431
+                    case 'description' :
432
+                    case 'og:description' :
433
+                    case 'twitter:description' :
434
+                        if (empty($meta['description'])) {
435
+                            $meta['description'] = $content;
436
+                        }
437
+                        break;
438
+
439
+                    case 'keywords' :
440
+                        if (is_string($content)) {
441
+                            $content = explode(',', $content);
442
+                            $content = array_map('trim', $content);
443
+                        }
444
+                        $meta['tags'] = $content;
445
+                        break;
446
+
447
+                    case 'og:site_name' :
448
+                    case 'twitter:site' :
449
+                        if (empty($meta['provider_name'])) {
450
+                            $meta['provider_name'] = $content;
451
+                        }
452
+                        break;
453
+
454
+                    case 'og:image' :
455
+                    case 'twitter:image' :
456
+                        $meta['thumbnails'][] = $this->getAbsoluteURL($doc, $content);
457
+                        break;
458
+                }
459
+            }
460
+        }
461
+
462
+        return $meta;
463
+    }
464
+
465
+    /**
466
+     * Parses <img> tags
467
+     *
468
+     * @param DOMDocument $doc Document
469
+     * @return array
470
+     */
471
+    public function parseImgTags(DOMDocument $doc) {
472
+
473
+        $meta = array();
474
+
475
+        $nodes = $doc->getElementsByTagName('img');
476
+        foreach ($nodes as $node) {
477
+            $src = $node->getAttribute('src');
478
+            $meta['thumbnails'][] = $this->getAbsoluteURL($doc, $src);
479
+        }
480
+
481
+        return $meta;
482
+    }
483
+
484
+    /**
485
+     * Normalizes relative URLs
486
+     *
487
+     * @param DOMDocument $doc  Document
488
+     * @param string      $href URL to normalize
489
+     * @return string
490
+     */
491
+    public function getAbsoluteURL(DOMDocument $doc, $href = '') {
492
+
493
+        // Check if $url is absolute
494
+        if (parse_url($href, PHP_URL_HOST)) {
495
+            return $href;
496
+        }
497
+
498
+        $uri = trim($doc->documentURI ?: '', '/');
499
+
500
+        // Check if $url is relative to root
501
+        if (substr($href, 0, 1) === "/") {
502
+            $scheme = parse_url($uri, PHP_URL_SCHEME);
503
+            $host = parse_url($uri, PHP_URL_HOST);
504
+            return "$scheme://$host$href";
505
+        }
506
+
507
+        // $url is relative to page
508
+        return "$uri/$href";
509
+    }
510 510
 
511 511
 }
Please login to merge, or discard this patch.