-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Explorer.php
147 lines (123 loc) · 4.33 KB
/
Explorer.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace ludovicm67\Url\Explorer;
use ludovicm67\Request\Request;
use ludovicm67\Request\RequestBuilder;
use ludovicm67\Url\Explorer\Parser\DescriptionParser;
use ludovicm67\Url\Explorer\Parser\ImageParser;
use ludovicm67\Url\Explorer\Parser\TitleParser;
class Explorer {
private $url;
private $opts;
private $request;
private $results = [];
public function __construct($url, $opts = []) {
$this->url = $url;
$this->opts = $opts;
$this->request = Request::fetchAll(new RequestBuilder($url, $opts));
$this->buildResults();
}
private function buildTitle() {
$title = (new TitleParser($this->request->getContent()))->getResults();
if (!empty($title)) {
$this->results["title"] = $title;
}
}
private function buildDescription() {
if (substr($this->request->getInfo("content_type"), 0, strlen("text/")) === "text/") {
$this->results["description"] =
(new DescriptionParser($this->request->getContent()))->getResults();
}
}
private function buildImage() {
$img = (new ImageParser($this->request->getContent()))->getResults();
if (!$img && substr($this->request->getInfo("content_type"), 0, strlen("image/")) === "image/") {
$this->results["type"] = "image";
$img = $this->request->getInfo("url");
}
if ($img && !filter_var($img, FILTER_VALIDATE_URL)) {
$parse = parse_url($this->request->getInfo("url"));
$scheme = $parse['scheme'];
$separator = mb_substr($img, 0, 1, 'utf-8');
$separator = ($separator === "/") ? "" : "/";
$img = $scheme . '://' . $parse['host'] . rtrim($parse['path'], "/") . $separator . $img;
}
$img_size = @getimagesize($img);
if (is_array($img_size)) {
$this->results["img"] = [
"url" => $img,
"width" => $img_size[0],
"height" => $img_size[1],
"mime" => $img_size["mime"]
];
} else {
$this->results["img"] = null;
unset($this->results["type"]);
}
}
private function buildType() {
if (isset($this->results["type"])) {
return;
}
if ($this->request->isEmpty()) {
$this->results["type"] = "none";
} else if (!$this->results["img"]) {
$this->results["type"] = "basic";
} else if ($this->results["img"]["width"] >= 400
&& $this->results["img"]["height"] >= 200) {
$this->results["type"] = "large";
} else {
$this->results["type"] = "small";
}
}
private function buildUrls() {
$parsedUrl = parse_url($this->request->getInfo("url"));
$base = ($parsedUrl && isset($parsedUrl['host']))
? $parsedUrl['host']
: $this->url;
$this->results["url"] = [
"request" => $this->url,
"final" => $this->request->getInfo("url"),
"base" => $base,
];
}
private function buildDate() {
$date = new \DateTime("now");
$date->setTimezone(new \DateTimeZone("UTC"));
$this->results["updated"] = $date->format("c");
}
private function buildResults() {
$this->results["code"] = $this->request->getInfo("http_code");
$this->results["title"] = $this->request->getInfo("url");
$this->results["description"] = "";
$this->results["img"] = null;
if (!$this->request->isEmpty() && $this->request->getContent() !== "") {
$this->buildTitle();
$this->buildDescription();
$this->buildImage();
}
$this->buildType();
$this->buildUrls();
$this->buildDate();
}
public function getUrl() {
return $this->url;
}
public function getOpts() {
return $this->opts;
}
public function getRequest() {
return $this->request;
}
public function getResults() {
return (object) $this->results;
}
public function json() {
return json_encode($this->results,
JSON_PRETTY_PRINT
| JSON_UNESCAPED_SLASHES
| JSON_UNESCAPED_UNICODE);
}
public function __toString() {
return $this->json();
}
}