/
addlink.php
executable file
·172 lines (135 loc) · 4.56 KB
/
addlink.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
require './auth.php';
require_once './helper.php';
require './vendor/autoload.php';
$tags = array();
if (PHP_SAPI == 'cli') {
switch ($argc) {
case 3:
$tags = explode(',', $argv[2]);
case 2:
$url = $argv[1];
break;
default:
die("Fehlerhafter Aufruf\n");
}
} else {
if (!(isset($_GET['url']) && $url = stripslashes($_GET['url']))) {
die("<pre>\nCLI only\n");
}
if (!empty($_GET['tags'])) {
$tags = explode(',', $_GET['tags']);
}
}
error_reporting(E_ALL);
$filename = 'feed.xml';
$dom = new DOMDocument();
$dom->formatOutput = true;
$dom->load($filename);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('foo', 'http://www.w3.org/2005/Atom');
$entries = $xpath->query('//foo:entry/foo:id[. = "'.$url.'"]/..');
if ($entries->length != 0) {
die("URL already added.");
}
$dom->getElementsByTagname('updated')->item(0)->nodeValue = date(DateTime::RFC3339);
$first_entry = $dom->getElementsByTagname('entry')->item(0);
$new_entry = new AtomEntryNode($dom);
$new_entry->setFromURL($url);
$dom->documentElement->insertBefore($new_entry->get(), $first_entry);
$dom->save($filename);
const SILENT=1;
include './create.index.php';
#header('Location: ./');
################
class AtomEntryNode
{
protected $dom;
protected $content = null;
protected $inner;
public function __construct(DOMDocument $dom) {
$this->dom = $dom;
$this->inner = $dom->createElement('entry');
$this->inner->setAttribute("xmlns", "http://www.w3.org/2005/Atom");
$this->inner->appendChild($dom->createElement('published', date(DateTime::RFC3339)));
$this->inner->appendChild($dom->createElement('updated', date(DateTime::RFC3339)));
}
public function get() {
return $this->inner;
}
public function setTags(array $tags)
{
foreach ($tags as $tag) {
$tag = strtolower(trim($tag));
$cat = $dom->createElement('category');
$cat->appendChild($dom->createTextnode($tag));
$entry->appendChild($cat);
}
}
public function setFromURL($url)
{
global $http_response_header;
$url = str_replace(" ", "+", $url);
$info = Embed\Embed::create($url);
$ct = $info->request->getMimeType();
switch ($ct) {
case 'image/png':
case 'image/gif':
case 'image/jpeg':
$this->setFromImage($url, $data);
break;
default:
$this->setFromEmbed($url, $info);
break;
}
}
public function setFromImage($url, $data)
{
$info = getimagesize('data://text/plain;base64,'.base64_encode($data));
switch ($info[2]) {
case IMAGETYPE_GIF: $type = 'GIF'; break;
case IMAGETYPE_JPEG: $type = 'JPEG'; break;
case IMAGETYPE_PNG: $type = 'PNG'; break;
default: $type = 'Unknown'; break;
}
$title = sprintf('%s - %s image (%ux%u)', basename($url), $type, $info[0], $info[1]);
$this->setData($url, $title);
$img = $this->dom->createElement('img');
$img->setAttribute('src', $url);
$this->appendContent($img);
}
public function setFromEmbed($url, $info)
{
$title = $info->title;
if ($info->providerName) {
$title .= ' ('.$info->providerName.')';
}
$this->setData($url, $title);
if ($d = $info->description) {
$div = $this->dom->createElement('div');
$t = $this->dom->createTextNode($d);
$div->appendChild($t);
$this->appendContent($div);
}
}
public function setData($url, $title)
{
$this->inner->appendChild($this->dom->createElement('id', htmlspecialchars($url)));
$this->inner->appendChild($this->dom->createElement('title', htmlspecialchars($title)));
$link = $this->dom->createElement('link');
$link->setAttribute('href', $url);
$link->setAttribute('rel', 'alternate');
$link->setAttribute('type', 'text/html');
$this->inner->appendChild($link);
}
public function appendContent(DOMNode $node)
{
if ($this->content == null) {
$this->content = $this->dom->createElement('content');
$this->content->setAttribute('type', 'xhtml');
$this->inner->appendChild($this->content);
}
$node->setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
$this->content->appendChild($node);
}
}