forked from tad0616/tad_link
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink_ajax.php
More file actions
69 lines (63 loc) · 2.45 KB
/
Copy pathlink_ajax.php
File metadata and controls
69 lines (63 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
use Xmf\Request;
use XoopsModules\Tadtools\Utility;
require_once __DIR__ . '/header.php';
// 關閉除錯訊息
header('HTTP/1.1 200 OK');
$xoopsLogger->activated = false;
$myts = \MyTextSanitizer::getInstance();
$url = $xoopsDB->escape(Request::getString('url'));
$date['metaTags']['description']['value'] = $date['title'] = '';
if (!empty($url)) {
$date = getUrlData($url);
$web['title'] = $date['title'];
$web['description'] = $date['metaTags']['description']['value'];
echo json_encode($web);
}
function getUrlData($url)
{
$result = false;
$contents = getUrlContents($url);
if (isset($contents) && is_string($contents)) {
$title = null;
$metaTags = null;
preg_match('/<title>([^>]*)<\/title>/si', $contents, $match);
if (isset($match) && is_array($match) && count($match) > 0) {
$title = strip_tags($match[1]);
}
preg_match_all('/<[\s]*meta[\s]*name="?' . '([^>"]*)"?[\s]*' . 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $contents, $match);
if (isset($match) && is_array($match) && 3 == count($match)) {
$originals = $match[0];
$names = $match[1];
$values = $match[2];
if (count($originals) == count($names) && count($names) == count($values)) {
$metaTags = [];
for ($i = 0, $limiti = count($names); $i < $limiti; $i++) {
$metaTags[$names[$i]] = [
'html' => htmlentities($originals[$i]),
'value' => $values[$i],
];
}
}
}
$result = [
'title' => $title,
'metaTags' => $metaTags,
];
}
return $result;
}
function getUrlContents($url, $maximumRedirections = null, $currentRedirection = 0)
{
$contents = Utility::vita_get_url_content($url);
// Check if we need to go somewhere else
if (isset($contents) && is_string($contents)) {
preg_match_all('/<[\s]*meta[\s]*http-equiv="?REFRESH"?' . '[\s]*content="?[0-9]*;[\s]*URL[\s]*=[\s]*([^>"]*)"?' . '[\s]*[\/]?[\s]*>/si', $contents, $match);
if (isset($match) && is_array($match) && 2 == count($match) && 1 == count($match[1])) {
if (!isset($maximumRedirections) || $currentRedirection < $maximumRedirections) {
return getUrlContents($match[1][0], $maximumRedirections, ++$currentRedirection);
}
}
}
return $contents;
}