-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretitle.php
More file actions
156 lines (113 loc) · 2.7 KB
/
retitle.php
File metadata and controls
156 lines (113 loc) · 2.7 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
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
148
149
150
151
152
153
154
155
156
<?php ob_start();?><!DOCTYPE html>
<html><head><meta charset="UTF-8"/></head>
<body>
Attempting retitle, please wait…
<?php
ob_flush();
if (!isset($_GET['m'])){die();}
$title='';
$magic = basename($_GET['m']);
if (!file_exists('./t')){
mkdir ('./t');
}
$target = './t/'.$magic.'.dat';
$array = null;
if (file_exists($target)){
$content=file_get_contents($target);
if($content!==false){
$aar=unserialize($content);
if ($aar!==false){
$array=$aar;
}
}
}
$old_title=$array;
$url='http://';
$url.='[';
$url.=str_replace('_', ']:', $magic);
$content=false;
if (is_array(get_headers($url))){
$content = fopen($url, 'r');
stream_set_timeout($content, 60);
if (stream_get_meta_data($content)['timed_out']===true){
fclose ($content);
$content=false;
die ('Connection to site timed out. <a href="./">Home</a>.');
}
else {
$wrap = stream_get_meta_data($content)['wrapper_data'];
$run = false;
foreach ($wrap as $meal){
if (strpos($meal, 'Content-Type: text/')==0||strpos($meal, 'Content-type: text/')==0){
$run=true;
}
}
if (!$run){
$title="Not an text/* content-type served";
}
fclose($content);
$content=$run;
}
}
if ($content){
$newhtml = file_get_contents($url);
if (strpos($newhtml, '<!DOCTYPE html>')!=0){
$newhtml= '<!DOCTYPE html>'."\n".$newhtml;
}
libxml_use_internal_errors(true);
$dom = new DOMDocument();
if($dom->loadHTML($newhtml)) {
$elems = $dom->getElementsByTagName("title");
if ($elems->length > 0) {
$title = $elems->item(0)->textContent;
}
else
{
$newhtml=str_replace('<!DOCTYPE html>', '', $newhtml);
$title = explode("\n", strip_tags($newhtml))[0];
}
}
else {
$newhtml=strip_tags($newhtml);
$toks=explode("\n", $newhtml);
$i=0;
while (str_replace ("\t", '', $toks[$i])==''){
while (!strstr($toks[$i],"\t")&&isset($toks[$i])&&$toks[$i]===''&&trim($toks[$i])===''){
$i++;
}
$i++;
}
$title=$toks[$i];
}
}
//we don't want title of more than 140 chars long
if (strlen($title)>140){
$title = substr($title, 0, 138).'…';
}
if (strlen(trim($title))==0&&isset($newhtml)){
$title = preg_split('#\r?\n#', ltrim(strip_tags($newhtml)), 2)[0];
}
else if(strlen(trim($title))==0){
$title='Site';
}
if ($old_title!==$title){
echo '<br/>Trying to save title. If you cannot see a saved ok mention line below, it was not saved<br/>';
$datz = serialize ($title);
if ($datz!==false){
if (file_put_contents($target, $datz)!==false){
echo 'title saved ok, <a href="./">go home?</a><hr/>';
}
}
}
else
{
echo 'title hasn\'t changed. <a href="./">go home?</a><hr/>';
die('</html>');
}
?>
Retitle résult: <br/>
<?php
echo htmlentities($title);
?>
</body>
</html>