forked from stesie/ttbag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.php
278 lines (217 loc) · 7.76 KB
/
init.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
require_once __DIR__.'/vendor/content-extractor/SiteConfig.php';
require_once __DIR__.'/vendor/content-extractor/ContentExtractor.php';
require_once __DIR__.'/vendor/readability/Readability.php';
require_once __DIR__.'/vendor/makefulltextfeedHelpers.php';
spl_autoload_register('__autoload');
require_once __DIR__.'/vendor/simplepie/autoloader.php';
class Ttbag extends Plugin implements IHandler {
function about() {
return array(1.0,
"Tiny Tiny Bag aka TTRSS Pocket clone",
"stesie",
true);
}
function init($host) {
$this->host = $host;
$this->dbh = Db::get();
$host->add_handler("public", "sharepopup", $this);
}
function sharepopup() {
$action = $_REQUEST["action"];
if ($_SESSION["uid"]) {
if ($action == 'share') {
$this->store_shared_article();
require __DIR__.'/views/share.php';
} else {
require __DIR__.'/views/index.php';
}
} else {
require __DIR__.'/views/login.php';
}
}
function api_version() {
return 2;
}
function csrf_ignore($method) {
return true;
}
function before($method) {
return true;
}
function after() {
return true;
}
protected function store_shared_article() {
$extracted_content = $this->extract_content($_REQUEST["url"]);
$url = $this->dbh->escape_string(strip_tags($_REQUEST["url"]));
$labels = $this->dbh->escape_string(strip_tags($_REQUEST["labels"]));
if($extracted_content) {
$content = $extracted_content['html'];
$title = $extracted_content['title'];
}
else {
$content = '';
$title = strip_tags($_REQUEST["title"]);
}
if(!empty($_REQUEST["xdebug"]) && $_REQUEST['xdebug'] == 2) {
$dateStr = date('Y-m-d H:i:s', $extracted_content['date']);
die("<hr>Authors: {$extracted_content['authors']}<br/>Date: {$dateStr}<h1>$title</h1>\n$content");
}
$content = $this->dbh->escape_string($content, false);
$title = $this->dbh->escape_string($title);
$this->create_archived_article($title, $url, $content, $labels, $_SESSION["uid"]);
}
protected function extract_content($url) {
$debug_enabled = defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug'];
if(!filter_var($url, FILTER_VALIDATE_URL)) {
return null;
}
$scheme = parse_url($url, PHP_URL_SCHEME);
if(!in_array($scheme, array("http", "https", "ftp"))) {
return false;
}
$html = file_get_contents($url);
if(empty($html)) {
return false;
}
if($debug_enabled) echo "<pre>";
$extractor = new ContentExtractor(__DIR__.'/ftr-site-config', __DIR__.'/site-config.local');
SiteConfig::$debug = $debug_enabled;
$extractor->debug = $debug_enabled;
$this->check_single_page($extractor, $url, $html);
$extract_result = $extractor->process($html, $url);
if($debug_enabled) echo "</pre>";
if(!$extract_result) {
return false;
}
$content_block = $extractor->getContent();
$extractor->readability->clean($content_block, 'select');
// get base URL
$base_url = get_base_url($extractor->readability->dom);
if (!$base_url) {
$base_url = $url;
}
// rewrite URLs
makeAbsolute($base_url, $content_block);
// remove nesting: <div><div><div><p>test</p></div></div></div> = <p>test</p>
while ($content_block->childNodes->length == 1 && $content_block->firstChild->nodeType === XML_ELEMENT_NODE) {
// only follow these tag names
if (!in_array(strtolower($content_block->tagName), array('div', 'article', 'section', 'header', 'footer'))) {
break;
}
$content_block = $content_block->firstChild;
}
// convert content block to HTML string
// Need to preserve things like body: //img[@id='feature']
if (in_array(strtolower($content_block->tagName), array('div', 'article', 'section', 'header', 'footer'))) {
$html = $content_block->innerHTML;
} else {
$html = $content_block->ownerDocument->saveXML($content_block); // essentially outerHTML
}
// post-processing cleanup
$html = preg_replace('!<p>[\s\h\v]*</p>!u', '', $html);
return array(
'title' => $extractor->getTitle(),
'authors' => $extractor->getAuthors(),
'date' => $extractor->getDate(),
'html' => $html,
);
}
protected function check_single_page($extractor, &$url, &$html) {
$site_config = $extractor->buildSiteConfig($url, $html);
$debug_enabled = defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug'];
if (empty($site_config->single_page_link)) {
_debug("SiteConfig doesn't declare single_page_link", $debug_enabled);
return;
}
// Build DOM tree from HTML
$readability = new Readability($html, $url);
$xpath = new DOMXPath($readability->dom);
// Loop through single_page_link xpath expressions
$single_page_url = null;
foreach ($site_config->single_page_link as $pattern) {
_debug("Trying pattern: $pattern", $debug_enabled);
$elems = @$xpath->evaluate($pattern, $readability->dom);
if (is_string($elems)) {
_debug(". matched and returned a string", $debug_enabled);
$single_page_url = trim($elems);
break;
} elseif ($elems instanceof DOMNodeList && $elems->length > 0) {
_debug(". matched and returned a node list", $debug_enabled);
foreach ($elems as $item) {
if ($item instanceof DOMElement && $item->hasAttribute('href')) {
_debug("... got an element, using href attribute", $debug_enabled);
$single_page_url = $item->getAttribute('href');
break 2;
} elseif ($item instanceof DOMAttr && $item->value) {
_debug("... got an attribute, using its value", $debug_enabled);
$single_page_url = $item->value;
break 2;
}
}
}
}
if(empty($single_page_url)) {
_debug("no single_page_url found, continuing with main page", $debug_enabled);
return;
}
_debug("extracted single_page_url: $single_page_url", $debug_enabled);
// If we've got URL, resolve against $url
$single_page_url = makeAbsoluteStr($url, $single_page_url);
_debug("... converted to absolute single_page_url: $single_page_url", $debug_enabled);
if($single_page_url == $url) {
_debug("single_page_url equals current page", $debug_enabled);
return;
}
$single_page_html = file_get_contents($single_page_url);
if(empty($single_page_html)) {
_debug("single_page_url document is empty", $debug_enabled);
return;
}
$html = $single_page_html;
$url = $single_page_url;
}
protected function create_archived_article($title, $url, $content, $labels_str, $owner_uid) {
$guid = 'SHA1:' . sha1("tinybag:" . $url . $owner_uid); // include owner_uid to prevent global GUID clash
$content_hash = sha1($content);
if ($labels_str != "") {
$labels = explode(",", $labels_str);
} else {
$labels = array();
}
$rc = false;
if (!$title) $title = $url;
if (!$title && !$url) return false;
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) return false;
db_query("BEGIN");
// only check for our user data here, others might have shared this with different content etc
$result = db_query("SELECT id FROM ttrss_entries, ttrss_user_entries WHERE
guid = '$guid' AND ref_id = id AND owner_uid = '$owner_uid' LIMIT 1");
if (db_num_rows($result) != 0) {
$rc = false;
} else {
$result = db_query("INSERT INTO ttrss_entries
(title, guid, link, updated, content, content_hash, date_entered, date_updated)
VALUES
('$title', '$guid', '$url', NOW(), '$content', '$content_hash', NOW(), NOW())");
$result = db_query("SELECT id FROM ttrss_entries WHERE guid = '$guid'");
if (db_num_rows($result) != 0) {
$ref_id = db_fetch_result($result, 0, "id");
db_query("INSERT INTO ttrss_user_entries
(ref_id, uuid, feed_id, orig_feed_id, owner_uid, published, tag_cache, label_cache, unread)
VALUES
('$ref_id', '', NULL, NULL, $owner_uid, false, '', '', true)");
if (count($labels) != 0) {
foreach ($labels as $label) {
label_add_article($ref_id, trim($label), $owner_uid);
}
}
$rc = true;
}
}
db_query("COMMIT");
return $rc;
}
}
?>