commit | author | age
|
8bd4d9
|
1 |
<?php |
JK |
2 |
class rss implements module { |
|
3 |
static function register_cmd() { |
|
4 |
return array( |
|
5 |
'r' => 'cmd_rss', |
|
6 |
'rss' => 'cmd_rss', |
|
7 |
'kanal' => 'cmd_rss', |
|
8 |
'kanaly' => 'cmd_rss', |
|
9 |
'news' => 'cmd_rss', |
|
10 |
'newsy' => 'cmd_rss', |
|
11 |
'wiad' => 'cmd_rss', |
|
12 |
'wiadomosc' => 'cmd_rss', |
|
13 |
'wiadomosci' => 'cmd_rss', |
|
14 |
|
|
15 |
'kanal' => 'cmd_set', |
|
16 |
'kanaly' => 'cmd_set', |
|
17 |
|
|
18 |
'rss2' => 'cmd_rssex', |
|
19 |
'exrss' => 'cmd_rssex', |
|
20 |
'rssex' => 'cmd_rssex', |
|
21 |
); |
|
22 |
} |
|
23 |
|
|
24 |
static function help($cmd=NULL) { |
|
25 |
if($cmd === NULL) { |
|
26 |
GGapi::putRichText('rss ', TRUE); |
|
27 |
GGapi::putRichText('[kanał]', FALSE, TRUE); |
|
28 |
GGapi::putRichText("\n".' Wiadomości z podanego kanału'."\n"); |
|
29 |
GGapi::putRichText('kanal ', TRUE); |
|
30 |
GGapi::putRichText('kanał', FALSE, TRUE); |
|
31 |
GGapi::putRichText("\n".' Ustawia domyślny kanał dla komendy rss'."\n\n"); |
|
32 |
} |
|
33 |
elseif($cmd == 'kanal' || $cmd == 'kanaly') { |
|
34 |
GGapi::putRichText('kanal ', TRUE); |
|
35 |
GGapi::putRichText('kanał', FALSE, TRUE); |
|
36 |
GGapi::putRichText("\n".' Zapisuje domyślny '); |
|
37 |
GGapi::putRichText('kanał', FALSE, TRUE); |
|
38 |
GGapi::putRichText(' RSS dla użytkownika. Dostępne kanały: '."\n".'- '.implode("\n".'- ', self::channels())); |
|
39 |
} |
|
40 |
else |
|
41 |
{ |
|
42 |
GGapi::putRichText('rss ', TRUE); |
|
43 |
GGapi::putRichText('[kanał]', FALSE, TRUE); |
|
44 |
GGapi::putRichText(' (alias: '); |
|
45 |
GGapi::putRichText('r, news, wiadomosci', TRUE); |
|
46 |
GGapi::putRichText(')'."\n".' Podaje ostatnie wiadomości z kanału RSS '); |
|
47 |
GGapi::putRichText('kanał', FALSE, TRUE); |
|
48 |
GGapi::putRichText('. Dostępne kanały: '."\n".'- '.implode("\n".'- ', self::channels())); |
|
49 |
} |
|
50 |
} |
|
51 |
|
|
52 |
private static function channels() { |
|
53 |
$file = file('./data/rss/channels.list'); |
|
54 |
$return = array(); |
|
55 |
foreach($file as $chan) { |
|
56 |
$chan = trim($chan); |
|
57 |
if(empty($chan) || substr($chan, 0, 1)=='#') { |
|
58 |
continue; |
|
59 |
} |
|
60 |
|
|
61 |
$parts = preg_split('/\t[\40\t]*/', $chan, 4); |
|
62 |
for($i=0; $i<4; $i++) { |
|
63 |
$parts[$i] = trim($parts[$i]); |
|
64 |
} |
|
65 |
|
|
66 |
if($aliases) { |
|
67 |
$return[$parts[1]] = $parts[1]; |
|
68 |
|
|
69 |
if($parts[2] == 'NULL') continue; |
|
70 |
|
|
71 |
$alias = explode(',', $parts[2]); |
|
72 |
foreach($alias as $val) { |
|
73 |
$return[trim($val)] = $parts[1]; |
|
74 |
} |
|
75 |
} |
|
76 |
else |
|
77 |
{ |
|
78 |
$return[] = $parts[1]; |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
return $return; |
|
83 |
} |
|
84 |
|
|
85 |
private static function channel($name, $verbose=FALSE) { |
|
86 |
$file = file('./data/rss/channels.list'); |
|
87 |
|
|
88 |
foreach($file as $chan) { |
|
89 |
$chan = trim($chan); |
|
90 |
if(empty($chan) || substr($chan, 0, 1)=='#') { |
|
91 |
continue; |
|
92 |
} |
|
93 |
|
|
94 |
$parts = preg_split('/\t[\40\t]*/', $chan, 4); |
|
95 |
for($i=1; $i<3; $i++) { |
|
96 |
$parts[$i] = trim($parts[$i]); |
|
97 |
} |
|
98 |
|
|
99 |
if($parts[1] == $name) { |
|
100 |
if($verbose) { |
|
101 |
return $parts; |
|
102 |
} |
|
103 |
else |
|
104 |
{ |
|
105 |
return $parts[1]; |
|
106 |
} |
|
107 |
} |
|
108 |
elseif($parts[2] == 'NULL') { |
|
109 |
continue; |
|
110 |
} |
|
111 |
else |
|
112 |
{ |
|
113 |
$alias = explode(',', $parts[2]); |
|
114 |
foreach($alias as $val) { |
|
115 |
if($val == $name) { |
|
116 |
if($verbose) { |
|
117 |
return $parts; |
|
118 |
} |
|
119 |
else |
|
120 |
{ |
|
121 |
return $parts[1]; |
|
122 |
} |
|
123 |
} |
|
124 |
} |
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 |
return FALSE; |
|
129 |
} |
|
130 |
|
|
131 |
static function p($text, $bash=FALSE) { |
|
132 |
$text = trim($text); |
|
133 |
$replace = array( |
|
134 |
"\n" => ' ', |
|
135 |
"\r" => ' ', |
|
136 |
'–' => '-', |
|
137 |
'–' => '-', |
|
138 |
''' => '"', |
|
139 |
'„' => '"', |
|
140 |
'”' => '"', |
|
141 |
); |
|
142 |
$text = strtr($text, $replace); |
|
143 |
$text = html_entity_decode($text); |
|
144 |
$replace = array( |
|
145 |
'<br />' => "\n", |
|
146 |
'<br/>' => "\n", |
|
147 |
'<br>' => "\n", |
|
148 |
' ' => ' ', |
|
149 |
); |
|
150 |
$text = strtr($text, $replace); |
|
151 |
if(!$bash) { |
|
152 |
$text = strip_tags($text); |
|
153 |
} |
|
154 |
$text = str_replace(array('©', '©'), '(C)', $text); |
|
155 |
return $text; |
|
156 |
} |
|
157 |
|
|
158 |
static function cmd_rss($name, $arg) { |
|
159 |
$arg = self::channel(funcs::utfToAscii($arg)); |
|
160 |
if(!$arg) { |
|
161 |
$arg = database::get($_GET['from'], 'rss', 'kanal'); |
|
162 |
if(!$arg) { |
|
163 |
$arg = self::channel('DEFAULT'); |
|
164 |
} |
|
165 |
} |
|
166 |
|
664fe7
|
167 |
$rss = @simplexml_load_string(file_get_contents('./data/rss/'.$arg.'.rss')); |
8bd4d9
|
168 |
if(!$rss) { |
JK |
169 |
GGapi::putText('Błąd przy przetwarzaniu kanału, przepraszamy.'); |
|
170 |
return FALSE; |
|
171 |
} |
|
172 |
|
ff648f
|
173 |
if($rss->entry) { |
JK |
174 |
GGapi::putRichText(self::p($rss->title), TRUE); |
|
175 |
|
|
176 |
foreach($rss->entry as $item) { |
|
177 |
GGapi::putRichText("\n\n".self::p($item->title), TRUE); |
|
178 |
GGapi::putRichText("\n".self::p($item->summary, ($arg=='bash'))."\n".self::p($item->link['href'])); |
|
179 |
|
|
180 |
if(GGapi::getLength() > 1700) { |
|
181 |
return; |
|
182 |
} |
|
183 |
} |
8bd4d9
|
184 |
} |
ff648f
|
185 |
else |
JK |
186 |
{ |
|
187 |
GGapi::putRichText(self::p($rss->channel->title), TRUE); |
|
188 |
if($rss->channel->copyright) { |
|
189 |
GGapi::putRichText("\n".self::p($rss->channel->copyright)); |
|
190 |
} |
|
191 |
|
|
192 |
foreach($rss->channel->item as $item) { |
|
193 |
GGapi::putRichText("\n\n".self::p($item->title), TRUE); |
|
194 |
GGapi::putRichText("\n".self::p($item->description, ($arg=='bash'))."\n".self::p($item->link)); |
|
195 |
|
|
196 |
if(GGapi::getLength() > 1700) { |
|
197 |
return; |
|
198 |
} |
8bd4d9
|
199 |
} |
JK |
200 |
} |
|
201 |
} |
|
202 |
|
|
203 |
static function cmd_set($name, $arg) { |
|
204 |
$arg = self::channel(funcs::utfToAscii($arg), TRUE); |
|
205 |
if(!$arg) { |
|
206 |
GGapi::putText('Wybrany kanał nie istnieje! Dostępne kanały: '."\n".'- '.implode("\n".'- ', self::channels())); |
|
207 |
} |
|
208 |
else |
|
209 |
{ |
|
210 |
database::add($_GET['from'], 'rss', 'kanal', $arg[1]); |
|
211 |
GGapi::putText('Kanał '.$arg[3].' został ustawiony jako domyślny. Teraz zamiast:'."\n".'rss '.$arg[1]."\n".'możesz wpisać samo'."\n".'rss'); |
|
212 |
} |
|
213 |
} |
|
214 |
|
|
215 |
static function testurl($url) { |
|
216 |
$url = parse_url($url); |
|
217 |
$schemas = array('http', 'https', 'ftp'); |
|
218 |
if(empty($url['scheme'])) { |
|
219 |
return array(-1, 'Podaj pełny adres do kanału RSS (z http://)!'); |
|
220 |
} |
|
221 |
if(!in_array(strtolower($url['scheme']), $schemas)) { |
|
222 |
return array(-1, 'Niedozowolona metoda dostępu (dostępne: http, https, ftp)'); |
|
223 |
} |
|
224 |
|
|
225 |
$hosts = gethostbynamel($url['host']); |
|
226 |
if(!is_array($hosts)) return array(-2, 'Podany host nie istnieje'); |
|
227 |
foreach($hosts as $ip) { |
|
228 |
if(substr($ip, 0, 4)=='127.' || substr($ip, 0, 3)=='10.' || substr($ip, 0, 2)=='0.' || substr($ip, 0, 3) > 223) { |
|
229 |
return array(-2, 'Niedozwolony numer IP hosta'); |
|
230 |
} |
|
231 |
} |
|
232 |
|
|
233 |
$res = @simplexml_load_file($url['scheme'].'://'.$url['user'].':'.$url['pass'].'@'.$url['host'].'/'.ltrim($url['path'], '/')); |
|
234 |
if(!$res) { |
|
235 |
return array(-3, 'Nie udało się załadować podanego kanału RSS'); |
|
236 |
} |
|
237 |
|
|
238 |
return $res; |
|
239 |
} |
|
240 |
|
|
241 |
static function cmd_rssex($name, $arg) { |
|
242 |
if(!$arg) { |
|
243 |
$arg = database::get($_GET['from'], 'rssex', 'kanal'); |
|
244 |
if(!$arg) { |
|
245 |
GGapi::putText('Podaj pełny adres kanału (z http://) lub ustaw domyślny funkcją '); |
|
246 |
GGapi::putRichText('kanal2', TRUE); |
|
247 |
GGapi::putRichText('!'."\n\n"); |
|
248 |
GGapi::putRichText('Przykład:', FALSE, FALSE, TRUE); |
|
249 |
GGapi::putRichText("\n".'rss2 http://wiadomosci.onet.pl/2,kategoria.rss'); |
|
250 |
return FALSE; |
|
251 |
} |
|
252 |
} |
|
253 |
|
|
254 |
$rss = self::testurl($arg); |
|
255 |
if(is_array($rss)) { |
|
256 |
GGapi::putText('Nie udało się pobrać wybranego kanału RSS. Błąd: '.$rss[1]); |
|
257 |
return FALSE; |
|
258 |
} |
|
259 |
elseif(!is_object($rss)) { |
|
260 |
GGapi::putText('Wystąpił nieznany błąd przy pobieraniu danych. Przepraszamy.'); |
|
261 |
} |
|
262 |
|
|
263 |
GGapi::putRichText(self::p($rss->channel->title), TRUE); |
|
264 |
if($rss->channel->copyright) { |
|
265 |
GGapi::putRichText("\n".self::p($rss->channel->copyright)); |
|
266 |
} |
|
267 |
|
|
268 |
foreach($rss->channel->item as $item) { |
|
269 |
GGapi::putRichText("\n\n".self::p($item->title), TRUE); |
|
270 |
GGapi::putRichText("\n".self::p($item->description, ($arg=='bash'))."\n".self::p($item->link)); |
|
271 |
|
|
272 |
if(GGapi::getLength() > 1700) { |
|
273 |
return; |
|
274 |
} |
|
275 |
} |
|
276 |
} |
|
277 |
|
|
278 |
static function cmd_setex($name, $arg) { |
|
279 |
if(!$arg) { |
|
280 |
GGapi::putText('Podaj pełny adres kanału (z http://)!'."\n\n"); |
|
281 |
GGapi::putRichText('Przykład:', FALSE, FALSE, TRUE); |
|
282 |
GGapi::putRichText("\n".'kanal2 http://wiadomosci.onet.pl/2,kategoria.rss'); |
|
283 |
} |
|
284 |
else |
|
285 |
{ |
|
286 |
$ret = self::testurl($arg); |
|
287 |
if(is_object($ret)) { |
|
288 |
database::add($_GET['from'], 'rssex', 'kanal', $arg[1]); |
|
289 |
GGapi::putText('Kanał '.$arg.' został ustawiony jako domyślny. Teraz zamiast:'."\n".'rss '.$arg."\n".'możesz wpisać samo'."\n".'rss'); |
|
290 |
} |
|
291 |
elseif(is_array($ret)) { |
|
292 |
GGapi::putText('Nie udało się pobrać wybranego kanału RSS. Błąd: '.$ret[1]); |
|
293 |
} |
|
294 |
else |
|
295 |
{ |
|
296 |
GGapi::putText('Wystąpił nieznany błąd przy pobieraniu danych. Przepraszamy.'); |
|
297 |
} |
|
298 |
} |
|
299 |
} |
|
300 |
} |
|
301 |
?> |