This repository was archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathinit.php
More file actions
171 lines (144 loc) · 5.04 KB
/
init.php
File metadata and controls
171 lines (144 loc) · 5.04 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
class Af_FullPost extends Plugin {
private $host;
function about() {
return array(0.09,
"Full post (requires CURL)",
"atallo");
}
function init($host) {
$this->host = $host;
$host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
$host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this);
$host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this);
}
function hook_prefs_edit_feed($feed_id) {
print "<div class=\"dlgSec\">".__("Plugin (full_post)")."</div>";
print "<div class=\"dlgSecCont\">";
$enabled_feeds = $this->host->get($this, "enabled_feeds");
if (!array($enabled_feeds)) $enabled_feeds = array();
$key = array_search($feed_id, $enabled_feeds);
$checked = $key !== FALSE ? "checked" : "";
print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"affull_enabled\"
name=\"affull_enabled\"
$checked> <label for=\"affull_enabled\">".__('Show full post')."</label>";
print "</div>";
}
function hook_prefs_save_feed($feed_id) {
$enabled_feeds = $this->host->get($this, "enabled_feeds");
if (!is_array($enabled_feeds)) $enabled_feeds = array();
$enable = checkbox_to_sql_bool($_POST["affull_enabled"]) == 'true';
$key = array_search($feed_id, $enabled_feeds);
if ($enable) {
if ($key === FALSE) {
array_push($enabled_feeds, $feed_id);
}
} else {
if ($key !== FALSE) {
unset($enabled_feeds[$key]);
}
}
$this->host->set($this, "enabled_feeds", $enabled_feeds);
}
function hook_article_filter($article) {
if (!function_exists("curl_init"))
return $article;
$enable_globally = $this->host->get($this, "enable_globally");
if (!$enable_globally) {
$enabled_feeds = $this->host->get($this, "enabled_feeds");
$key = array_search($article["feed"]["id"], $enabled_feeds);
if ($key === FALSE) return $article;
}
//$article["content"] .= "<br/><hr/>Full post:<br><hr/>" .
// $this->get_full_post($article["link"]);
$fulldata = '';
try {
$fulldata = $this->get_full_post($article["link"]);
} catch (Exception $e) {
//$fulldata = 'Readability: Error sacando datos';
$fulldata = $e->getMessage() . '<br/><pre>' . $e->getTraceAsString() . '</pre>';
}
$article["content"] .= "<br/><hr/>Full post:<br><hr/>" . $fulldata;
//
return $article;
}
function api_version() {
return 2;
}
private function get_full_post($request_url) {
// now an amalgamation of code from:
// 1) https://github.com/feelinglucky/php-readability
// 2) http://code.fivefilters.org/php-readability/src
if (!class_exists("Readability")) include_once 'Readability.php';
if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
$handle = curl_init();
curl_setopt_array($handle, array(
CURLOPT_USERAGENT => USER_AGENT,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => false,
CURLOPT_HTTPGET => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_URL => $request_url
));
$html = curl_exec($handle);
curl_close($handle);
//if (!$charset = mb_detect_encoding($source)) {
//}
preg_match("/charset=([\w|\-]+);?/", $html, $match);
$charset = isset($match[1]) ? $match[1] : 'utf-8';
$html = mb_convert_encoding($html, 'UTF-8', $charset);
// If we've got Tidy, let's clean up input.
// This step is highly recommended - PHP's default HTML parser often doesn't do a great job and results in strange output.
if (function_exists('tidy_parse_string')) {
$tidy = tidy_parse_string($html, array(), 'UTF8');
$tidy->cleanRepair();
$html = $tidy->value;
}
$readability = new Readability($html);
// print debug output?
$readability->debug = false;
// convert links to footnotes?
$readability->convertLinksToFootnotes = false;
$result = $readability->init();
if ($result) {
// $title = $readability->getTitle()->textContent;
$content = $readability->getContent()->innerHTML;
// if we've got Tidy, let's clean it up for output
if (function_exists('tidy_parse_string')) {
$tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
$tidy->cleanRepair();
$content = $tidy->value;
}
} else {
# Raise an error so that we know not to replace the RSS stub article with something even less helpful
throw new Exception('Full-text extraction failed');
}
return $content;
}
private function get_full_post_old($request_url) {
include_once 'Readability.inc.php';
$handle = curl_init();
curl_setopt_array($handle, array(
CURLOPT_USERAGENT => USER_AGENT,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => false,
CURLOPT_HTTPGET => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_URL => $request_url
));
$source = curl_exec($handle);
curl_close($handle);
preg_match("/charset=([\w|\-]+);?/", $source, $match);
$charset = isset($match[1]) ? $match[1] : 'utf-8';
try {
$Readability = new Readability($source, $charset);
$Data = $Readability->getContent();
return $Data['content'];
} catch (Exception $e) {
return 'Readability: Error sacando datos';
}
}
}
?>