]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | /* if ($_GET["debug"]) { | |
4 | define('DEFAULT_ERROR_LEVEL', E_ALL); | |
5 | } else { | |
6 | define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE); | |
7 | } */ | |
8 | ||
9 | require_once 'config.php'; | |
10 | ||
11 | function get_translations() { | |
12 | $tr = array( | |
13 | "auto" => "Detect automatically", | |
14 | "en_US" => "English", | |
15 | "fr_FR" => "Français", | |
16 | "nb_NO" => "Norsk Bokmål", | |
17 | "ru_RU" => "Русский", | |
18 | "pt_BR" => "Portuguese/Brazil", | |
19 | "zh_CN" => "Simplified Chinese"); | |
20 | ||
21 | return $tr; | |
22 | } | |
23 | ||
24 | if (ENABLE_TRANSLATIONS == true) { | |
25 | require_once "accept-to-gettext.php"; | |
26 | require_once "gettext/gettext.inc"; | |
27 | ||
28 | function startup_gettext() { | |
29 | ||
30 | # Get locale from Accept-Language header | |
31 | $lang = al2gt(array_keys(get_translations()), "text/html"); | |
32 | ||
33 | if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) { | |
34 | $lang = _TRANSLATION_OVERRIDE_DEFAULT; | |
35 | } | |
36 | ||
37 | if ($_COOKIE["ttrss_lang"] && $_COOKIE["ttrss_lang"] != "auto") { | |
38 | $lang = $_COOKIE["ttrss_lang"]; | |
39 | } | |
40 | ||
41 | if ($lang) { | |
42 | _setlocale(LC_MESSAGES, $lang); | |
43 | _bindtextdomain("messages", "locale"); | |
44 | _textdomain("messages"); | |
45 | _bind_textdomain_codeset("messages", "UTF-8"); | |
46 | } | |
47 | } | |
48 | ||
49 | startup_gettext(); | |
50 | ||
51 | } else { | |
52 | function __($msg) { | |
53 | return $msg; | |
54 | } | |
55 | function startup_gettext() { | |
56 | // no-op | |
57 | return true; | |
58 | } | |
59 | } | |
60 | ||
61 | require_once 'db-prefs.php'; | |
62 | require_once 'compat.php'; | |
63 | require_once 'errors.php'; | |
64 | require_once 'version.php'; | |
65 | ||
66 | require_once 'phpmailer/class.phpmailer.php'; | |
67 | ||
68 | define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')'); | |
69 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); | |
70 | ||
71 | if (ENABLE_SIMPLEPIE) { | |
72 | require_once "simplepie/simplepie.inc"; | |
73 | } else { | |
74 | require_once "magpierss/rss_fetch.inc"; | |
75 | require_once 'magpierss/rss_utils.inc'; | |
76 | } | |
77 | ||
78 | function _debug($msg) { | |
79 | $ts = strftime("%H:%M:%S", time()); | |
80 | print "[$ts] $msg\n"; | |
81 | } | |
82 | ||
83 | function purge_feed($link, $feed_id, $purge_interval, $debug = false) { | |
84 | ||
85 | if (!$purge_interval) $purge_interval = feed_purge_interval($link, $feed_id); | |
86 | ||
87 | $rows = -1; | |
88 | ||
89 | $result = db_query($link, | |
90 | "SELECT owner_uid FROM ttrss_feeds WHERE id = '$feed_id'"); | |
91 | ||
92 | $owner_uid = false; | |
93 | ||
94 | if (db_num_rows($result) == 1) { | |
95 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
96 | } | |
97 | ||
98 | if (!$owner_uid) return; | |
99 | ||
100 | $purge_unread = get_pref($link, "PURGE_UNREAD_ARTICLES", | |
101 | $owner_uid, false); | |
102 | ||
103 | if (!$purge_unread) $query_limit = " unread = false AND "; | |
104 | ||
105 | if (DB_TYPE == "pgsql") { | |
106 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
107 | marked = false AND feed_id = '$feed_id' AND | |
108 | (SELECT date_entered FROM ttrss_entries WHERE | |
109 | id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */ | |
110 | ||
111 | $pg_version = get_pgsql_version($link); | |
112 | ||
113 | if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) { | |
114 | ||
115 | $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
116 | ttrss_entries.id = ref_id AND | |
117 | marked = false AND | |
118 | feed_id = '$feed_id' AND | |
119 | $query_limit | |
120 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); | |
121 | ||
122 | } else { | |
123 | ||
124 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
125 | USING ttrss_entries | |
126 | WHERE ttrss_entries.id = ref_id AND | |
127 | marked = false AND | |
128 | feed_id = '$feed_id' AND | |
129 | $query_limit | |
130 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); | |
131 | } | |
132 | ||
133 | $rows = pg_affected_rows($result); | |
134 | ||
135 | } else { | |
136 | ||
137 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
138 | marked = false AND feed_id = '$feed_id' AND | |
139 | (SELECT date_entered FROM ttrss_entries WHERE | |
140 | id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */ | |
141 | ||
142 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
143 | USING ttrss_user_entries, ttrss_entries | |
144 | WHERE ttrss_entries.id = ref_id AND | |
145 | marked = false AND | |
146 | feed_id = '$feed_id' AND | |
147 | $query_limit | |
148 | ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); | |
149 | ||
150 | $rows = mysql_affected_rows($link); | |
151 | ||
152 | } | |
153 | ||
154 | if ($debug) { | |
155 | _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles"); | |
156 | } | |
157 | } | |
158 | ||
159 | function global_purge_old_posts($link, $do_output = false, $limit = false) { | |
160 | ||
161 | $random_qpart = sql_random_function(); | |
162 | ||
163 | if ($limit) { | |
164 | $limit_qpart = "LIMIT $limit"; | |
165 | } else { | |
166 | $limit_qpart = ""; | |
167 | } | |
168 | ||
169 | $result = db_query($link, | |
170 | "SELECT id,purge_interval,owner_uid FROM ttrss_feeds | |
171 | ORDER BY $random_qpart $limit_qpart"); | |
172 | ||
173 | while ($line = db_fetch_assoc($result)) { | |
174 | ||
175 | $feed_id = $line["id"]; | |
176 | $purge_interval = $line["purge_interval"]; | |
177 | $owner_uid = $line["owner_uid"]; | |
178 | ||
179 | if ($purge_interval == 0) { | |
180 | ||
181 | $tmp_result = db_query($link, | |
182 | "SELECT value FROM ttrss_user_prefs WHERE | |
183 | pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'"); | |
184 | ||
185 | if (db_num_rows($tmp_result) != 0) { | |
186 | $purge_interval = db_fetch_result($tmp_result, 0, "value"); | |
187 | } | |
188 | } | |
189 | ||
190 | if ($do_output) { | |
191 | // print "Feed $feed_id: purge interval = $purge_interval\n"; | |
192 | } | |
193 | ||
194 | if ($purge_interval > 0) { | |
195 | purge_feed($link, $feed_id, $purge_interval, $do_output); | |
196 | } | |
197 | } | |
198 | ||
199 | // purge orphaned posts in main content table | |
200 | $result = db_query($link, "DELETE FROM ttrss_entries WHERE | |
201 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
202 | ||
203 | if ($do_output) { | |
204 | $rows = db_affected_rows($link, $result); | |
205 | _debug("Purged $rows orphaned posts."); | |
206 | } | |
207 | ||
208 | } | |
209 | ||
210 | function feed_purge_interval($link, $feed_id) { | |
211 | ||
212 | $result = db_query($link, "SELECT purge_interval, owner_uid FROM ttrss_feeds | |
213 | WHERE id = '$feed_id'"); | |
214 | ||
215 | if (db_num_rows($result) == 1) { | |
216 | $purge_interval = db_fetch_result($result, 0, "purge_interval"); | |
217 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
218 | ||
219 | if ($purge_interval == 0) $purge_interval = get_pref($link, | |
220 | 'PURGE_OLD_DAYS', $user_id); | |
221 | ||
222 | return $purge_interval; | |
223 | ||
224 | } else { | |
225 | return -1; | |
226 | } | |
227 | } | |
228 | ||
229 | function purge_old_posts($link) { | |
230 | ||
231 | $user_id = $_SESSION["uid"]; | |
232 | ||
233 | $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds | |
234 | WHERE owner_uid = '$user_id'"); | |
235 | ||
236 | while ($line = db_fetch_assoc($result)) { | |
237 | ||
238 | $feed_id = $line["id"]; | |
239 | $purge_interval = $line["purge_interval"]; | |
240 | ||
241 | if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS'); | |
242 | ||
243 | if ($purge_interval > 0) { | |
244 | purge_feed($link, $feed_id, $purge_interval); | |
245 | } | |
246 | } | |
247 | ||
248 | // purge orphaned posts in main content table | |
249 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
250 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
251 | } | |
252 | ||
253 | function get_feed_update_interval($link, $feed_id) { | |
254 | $result = db_query($link, "SELECT owner_uid, update_interval FROM | |
255 | ttrss_feeds WHERE id = '$feed_id'"); | |
256 | ||
257 | if (db_num_rows($result) == 1) { | |
258 | $update_interval = db_fetch_result($result, 0, "update_interval"); | |
259 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
260 | ||
261 | if ($update_interval != 0) { | |
262 | return $update_interval; | |
263 | } else { | |
264 | return get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $owner_uid, false); | |
265 | } | |
266 | ||
267 | } else { | |
268 | return -1; | |
269 | } | |
270 | } | |
271 | ||
272 | function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) { | |
273 | ||
274 | if (WEB_DEMO_MODE) return; | |
275 | ||
276 | if (!$user_id) { | |
277 | $user_id = $_SESSION["uid"]; | |
278 | purge_old_posts($link); | |
279 | } | |
280 | ||
281 | // db_query($link, "BEGIN"); | |
282 | ||
283 | if (MAX_UPDATE_TIME > 0) { | |
284 | if (DB_TYPE == "mysql") { | |
285 | $q_order = "RAND()"; | |
286 | } else { | |
287 | $q_order = "RANDOM()"; | |
288 | } | |
289 | } else { | |
290 | $q_order = "last_updated DESC"; | |
291 | } | |
292 | ||
293 | $result = db_query($link, "SELECT feed_url,id, | |
294 | SUBSTRING(last_updated,1,19) AS last_updated, | |
295 | update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id' | |
296 | ORDER BY $q_order"); | |
297 | ||
298 | $upd_start = time(); | |
299 | ||
300 | while ($line = db_fetch_assoc($result)) { | |
301 | $upd_intl = $line["update_interval"]; | |
302 | ||
303 | if (!$upd_intl || $upd_intl == 0) { | |
304 | $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false); | |
305 | } | |
306 | ||
307 | if ($upd_intl < 0) { | |
308 | // Updates for this feed are disabled | |
309 | continue; | |
310 | } | |
311 | ||
312 | if ($fetch || (!$line["last_updated"] || | |
313 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) { | |
314 | ||
315 | // print "<!-- feed: ".$line["feed_url"]." -->"; | |
316 | ||
317 | update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon); | |
318 | ||
319 | $upd_elapsed = time() - $upd_start; | |
320 | ||
321 | if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) { | |
322 | return; | |
323 | } | |
324 | } | |
325 | } | |
326 | ||
327 | // db_query($link, "COMMIT"); | |
328 | ||
329 | } | |
330 | ||
331 | function fetch_file_contents($url) { | |
332 | if (USE_CURL_FOR_ICONS) { | |
333 | $tmpfile = tempnam(TMP_DIRECTORY, "ttrss-tmp"); | |
334 | ||
335 | $ch = curl_init($url); | |
336 | $fp = fopen($tmpfile, "w"); | |
337 | ||
338 | if ($fp) { | |
339 | curl_setopt($ch, CURLOPT_FILE, $fp); | |
340 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); | |
341 | curl_setopt($ch, CURLOPT_TIMEOUT, 45); | |
342 | curl_exec($ch); | |
343 | curl_close($ch); | |
344 | fclose($fp); | |
345 | } | |
346 | ||
347 | $contents = file_get_contents($tmpfile); | |
348 | unlink($tmpfile); | |
349 | ||
350 | return $contents; | |
351 | ||
352 | } else { | |
353 | return file_get_contents($url); | |
354 | } | |
355 | ||
356 | } | |
357 | ||
358 | // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/) | |
359 | // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php | |
360 | ||
361 | function get_favicon_url($url) { | |
362 | ||
363 | if ($html = @fetch_file_contents($url)) { | |
364 | ||
365 | if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) { | |
366 | // Attempt to grab a favicon link from their webpage url | |
367 | $linkUrl = html_entity_decode($matches[1]); | |
368 | ||
369 | if (substr($linkUrl, 0, 1) == '/') { | |
370 | $urlParts = parse_url($url); | |
371 | $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl; | |
372 | } else if (substr($linkUrl, 0, 7) == 'http://') { | |
373 | $faviconURL = $linkUrl; | |
374 | } else if (substr($url, -1, 1) == '/') { | |
375 | $faviconURL = $url.$linkUrl; | |
376 | } else { | |
377 | $faviconURL = $url.'/'.$linkUrl; | |
378 | } | |
379 | ||
380 | } else { | |
381 | // If unsuccessful, attempt to "guess" the favicon location | |
382 | $urlParts = parse_url($url); | |
383 | $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico'; | |
384 | } | |
385 | } | |
386 | ||
387 | // Run a test to see if what we have attempted to get actually exists. | |
388 | if(USE_CURL_FOR_ICONS || url_validate($faviconURL)) { | |
389 | return $faviconURL; | |
390 | } else { | |
391 | return false; | |
392 | } | |
393 | } | |
394 | ||
395 | function url_validate($link) { | |
396 | ||
397 | $url_parts = @parse_url($link); | |
398 | ||
399 | if ( empty( $url_parts["host"] ) ) | |
400 | return false; | |
401 | ||
402 | if ( !empty( $url_parts["path"] ) ) { | |
403 | $documentpath = $url_parts["path"]; | |
404 | } else { | |
405 | $documentpath = "/"; | |
406 | } | |
407 | ||
408 | if ( !empty( $url_parts["query"] ) ) | |
409 | $documentpath .= "?" . $url_parts["query"]; | |
410 | ||
411 | $host = $url_parts["host"]; | |
412 | $port = $url_parts["port"]; | |
413 | ||
414 | if ( empty($port) ) | |
415 | $port = "80"; | |
416 | ||
417 | $socket = @fsockopen( $host, $port, $errno, $errstr, 30 ); | |
418 | ||
419 | if ( !$socket ) | |
420 | return false; | |
421 | ||
422 | fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n"); | |
423 | ||
424 | $http_response = fgets( $socket, 22 ); | |
425 | ||
426 | $responses = "/(200 OK)|(30[0-9] Moved)/"; | |
427 | if ( preg_match($responses, $http_response) ) { | |
428 | fclose($socket); | |
429 | return true; | |
430 | } else { | |
431 | return false; | |
432 | } | |
433 | ||
434 | } | |
435 | ||
436 | function check_feed_favicon($site_url, $feed, $link) { | |
437 | $favicon_url = get_favicon_url($site_url); | |
438 | ||
439 | # print "FAVICON [$site_url]: $favicon_url\n"; | |
440 | ||
441 | error_reporting(0); | |
442 | ||
443 | $icon_file = ICONS_DIR . "/$feed.ico"; | |
444 | ||
445 | if ($favicon_url && !file_exists($icon_file)) { | |
446 | $contents = fetch_file_contents($favicon_url); | |
447 | ||
448 | $fp = fopen($icon_file, "w"); | |
449 | ||
450 | if ($fp) { | |
451 | fwrite($fp, $contents); | |
452 | fclose($fp); | |
453 | chmod($icon_file, 0644); | |
454 | } | |
455 | } | |
456 | ||
457 | error_reporting(DEFAULT_ERROR_LEVEL); | |
458 | ||
459 | } | |
460 | ||
461 | function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) { | |
462 | ||
463 | if (!$_GET["daemon"] && !$ignore_daemon) { | |
464 | return; | |
465 | } | |
466 | ||
467 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
468 | _debug("update_rss_feed: start"); | |
469 | } | |
470 | ||
471 | $result = db_query($link, "SELECT id,update_interval,auth_login,auth_pass,cache_images | |
472 | FROM ttrss_feeds WHERE id = '$feed'"); | |
473 | ||
474 | if (db_num_rows($result) == 0) { | |
475 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
476 | _debug("update_rss_feed: feed $feed [$feed_url] NOT FOUND"); | |
477 | } | |
478 | return; | |
479 | } | |
480 | ||
481 | $auth_login = db_fetch_result($result, 0, "auth_login"); | |
482 | $auth_pass = db_fetch_result($result, 0, "auth_pass"); | |
483 | ||
484 | if (!ENABLE_SIMPLEPIE) { | |
485 | $auth_login = urlencode($auth_login); | |
486 | $auth_pass = urlencode($auth_pass); | |
487 | } | |
488 | ||
489 | $update_interval = db_fetch_result($result, 0, "update_interval"); | |
490 | $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images")); | |
491 | ||
492 | if ($update_interval < 0) { return; } | |
493 | ||
494 | $feed = db_escape_string($feed); | |
495 | ||
496 | $fetch_url = $feed_url; | |
497 | ||
498 | if ($auth_login && $auth_pass) { | |
499 | $url_parts = array(); | |
500 | preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts); | |
501 | ||
502 | if ($url_parts[1] && $url_parts[2]) { | |
503 | $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2]; | |
504 | } | |
505 | ||
506 | } | |
507 | ||
508 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
509 | _debug("update_rss_feed: fetching [$fetch_url]..."); | |
510 | } | |
511 | ||
512 | if (!defined('DAEMON_EXTENDED_DEBUG') && !$_GET['xdebug']) { | |
513 | error_reporting(0); | |
514 | } | |
515 | ||
516 | if (!ENABLE_SIMPLEPIE) { | |
517 | $rss = fetch_rss($fetch_url); | |
518 | } else { | |
519 | if (!is_dir(SIMPLEPIE_CACHE_DIR)) { | |
520 | mkdir(SIMPLEPIE_CACHE_DIR); | |
521 | } | |
522 | ||
523 | $rss = new SimplePie(); | |
524 | $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT); | |
525 | // $rss->set_timeout(MAGPIE_FETCH_TIME_OUT); | |
526 | $rss->set_feed_url($fetch_url); | |
527 | $rss->set_output_encoding('UTF-8'); | |
528 | ||
529 | if (SIMPLEPIE_CACHE_IMAGES && $cache_images) { | |
530 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
531 | _debug("enabling image cache"); | |
532 | } | |
533 | ||
534 | $rss->set_image_handler('./image.php', 'i'); | |
535 | } | |
536 | ||
537 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
538 | _debug("feed update interval (sec): " . | |
539 | get_feed_update_interval($link, $feed)*60); | |
540 | } | |
541 | ||
542 | if (is_dir(SIMPLEPIE_CACHE_DIR)) { | |
543 | $rss->set_cache_location(SIMPLEPIE_CACHE_DIR); | |
544 | $rss->set_cache_duration(get_feed_update_interval($link, $feed) * 60); | |
545 | } | |
546 | ||
547 | $rss->init(); | |
548 | } | |
549 | ||
550 | // print_r($rss); | |
551 | ||
552 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
553 | _debug("update_rss_feed: fetch done, parsing..."); | |
554 | } else { | |
555 | error_reporting (DEFAULT_ERROR_LEVEL); | |
556 | } | |
557 | ||
558 | $feed = db_escape_string($feed); | |
559 | ||
560 | if (ENABLE_SIMPLEPIE) { | |
561 | $fetch_ok = !$rss->error(); | |
562 | } else { | |
563 | $fetch_ok = !!$rss; | |
564 | } | |
565 | ||
566 | if ($fetch_ok) { | |
567 | ||
568 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
569 | _debug("update_rss_feed: processing feed data..."); | |
570 | } | |
571 | ||
572 | // db_query($link, "BEGIN"); | |
573 | ||
574 | $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid | |
575 | FROM ttrss_feeds WHERE id = '$feed'"); | |
576 | ||
577 | $registered_title = db_fetch_result($result, 0, "title"); | |
578 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
579 | $orig_site_url = db_fetch_result($result, 0, "site_url"); | |
580 | ||
581 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
582 | ||
583 | if (ENABLE_SIMPLEPIE) { | |
584 | $site_url = $rss->get_link(); | |
585 | } else { | |
586 | $site_url = $rss->channel["link"]; | |
587 | } | |
588 | ||
589 | if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) { | |
590 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
591 | _debug("update_rss_feed: checking favicon..."); | |
592 | } | |
593 | ||
594 | check_feed_favicon($site_url, $feed, $link); | |
595 | } | |
596 | ||
597 | if (!$registered_title || $registered_title == "[Unknown]") { | |
598 | ||
599 | if (ENABLE_SIMPLEPIE) { | |
600 | $feed_title = db_escape_string($rss->get_title()); | |
601 | } else { | |
602 | $feed_title = db_escape_string($rss->channel["title"]); | |
603 | } | |
604 | ||
605 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
606 | _debug("update_rss_feed: registering title: $feed_title"); | |
607 | } | |
608 | ||
609 | db_query($link, "UPDATE ttrss_feeds SET | |
610 | title = '$feed_title' WHERE id = '$feed'"); | |
611 | } | |
612 | ||
613 | // weird, weird Magpie | |
614 | if (!ENABLE_SIMPLEPIE) { | |
615 | if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]); | |
616 | } | |
617 | ||
618 | if ($site_url && $orig_site_url != db_escape_string($site_url)) { | |
619 | db_query($link, "UPDATE ttrss_feeds SET | |
620 | site_url = '$site_url' WHERE id = '$feed'"); | |
621 | } | |
622 | ||
623 | // print "I: " . $rss->channel["image"]["url"]; | |
624 | ||
625 | if (!ENABLE_SIMPLEPIE) { | |
626 | $icon_url = $rss->image["url"]; | |
627 | } else { | |
628 | $icon_url = $rss->get_image_url(); | |
629 | } | |
630 | ||
631 | if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) { | |
632 | $icon_url = db_escape_string($icon_url); | |
633 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
634 | } | |
635 | ||
636 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
637 | _debug("update_rss_feed: loading filters..."); | |
638 | } | |
639 | ||
640 | $filters = array(); | |
641 | ||
642 | $result = db_query($link, "SELECT reg_exp, | |
643 | ttrss_filter_types.name AS name, | |
644 | ttrss_filter_actions.name AS action, | |
645 | inverse, | |
646 | action_param | |
647 | FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE | |
648 | enabled = true AND | |
649 | owner_uid = $owner_uid AND | |
650 | ttrss_filter_types.id = filter_type AND | |
651 | ttrss_filter_actions.id = action_id AND | |
652 | (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp"); | |
653 | ||
654 | while ($line = db_fetch_assoc($result)) { | |
655 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); | |
656 | ||
657 | $filter["reg_exp"] = $line["reg_exp"]; | |
658 | $filter["action"] = $line["action"]; | |
659 | $filter["action_param"] = $line["action_param"]; | |
660 | $filter["inverse"] = sql_bool_to_bool($line["inverse"]); | |
661 | ||
662 | array_push($filters[$line["name"]], $filter); | |
663 | } | |
664 | ||
665 | if (ENABLE_SIMPLEPIE) { | |
666 | $iterator = $rss->get_items(); | |
667 | } else { | |
668 | $iterator = $rss->items; | |
669 | if (!$iterator || !is_array($iterator)) $iterator = $rss->entries; | |
670 | if (!$iterator || !is_array($iterator)) $iterator = $rss; | |
671 | } | |
672 | ||
673 | if (!is_array($iterator)) { | |
674 | /* db_query($link, "UPDATE ttrss_feeds | |
675 | SET last_error = 'Parse error: can\'t find any articles.' | |
676 | WHERE id = '$feed'"); */ | |
677 | ||
678 | // clear any errors and mark feed as updated if fetched okay | |
679 | // even if it's blank | |
680 | ||
681 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
682 | _debug("update_rss_feed: entry iterator is not an array, no articles?"); | |
683 | } | |
684 | ||
685 | db_query($link, "UPDATE ttrss_feeds | |
686 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
687 | ||
688 | return; // no articles | |
689 | } | |
690 | ||
691 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
692 | _debug("update_rss_feed: processing articles..."); | |
693 | } | |
694 | ||
695 | foreach ($iterator as $item) { | |
696 | ||
697 | if ($_GET['xdebug']) { | |
698 | print_r($item); | |
699 | ||
700 | } | |
701 | ||
702 | if (ENABLE_SIMPLEPIE) { | |
703 | $entry_guid = $item->get_id(); | |
704 | if (!$entry_guid) $entry_guid = $item->get_link(); | |
705 | if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title()); | |
706 | ||
707 | } else { | |
708 | ||
709 | $entry_guid = $item["id"]; | |
710 | ||
711 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
712 | if (!$entry_guid) $entry_guid = $item["link"]; | |
713 | if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]); | |
714 | } | |
715 | ||
716 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
717 | _debug("update_rss_feed: guid $entry_guid"); | |
718 | } | |
719 | ||
720 | if (!$entry_guid) continue; | |
721 | ||
722 | $entry_timestamp = ""; | |
723 | ||
724 | if (ENABLE_SIMPLEPIE) { | |
725 | $entry_timestamp = strtotime($item->get_date()); | |
726 | } else { | |
727 | $rss_2_date = $item['pubdate']; | |
728 | $rss_1_date = $item['dc']['date']; | |
729 | $atom_date = $item['issued']; | |
730 | if (!$atom_date) $atom_date = $item['updated']; | |
731 | ||
732 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); | |
733 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
734 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
735 | } | |
736 | ||
737 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
738 | _debug("update_rss_feed: date $entry_timestamp"); | |
739 | } | |
740 | ||
741 | if ($entry_timestamp == "" || $entry_timestamp == -1 || !$entry_timestamp) { | |
742 | $entry_timestamp = time(); | |
743 | $no_orig_date = 'true'; | |
744 | } else { | |
745 | $no_orig_date = 'false'; | |
746 | } | |
747 | ||
748 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
749 | ||
750 | if (ENABLE_SIMPLEPIE) { | |
751 | $entry_title = $item->get_title(); | |
752 | } else { | |
753 | $entry_title = trim(strip_tags($item["title"])); | |
754 | } | |
755 | ||
756 | if (ENABLE_SIMPLEPIE) { | |
757 | $entry_link = $item->get_link(); | |
758 | } else { | |
759 | // strange Magpie workaround | |
760 | $entry_link = $item["link_"]; | |
761 | if (!$entry_link) $entry_link = $item["link"]; | |
762 | } | |
763 | ||
764 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
765 | _debug("update_rss_feed: title $entry_title"); | |
766 | } | |
767 | ||
768 | if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);; | |
769 | ||
770 | $entry_link = strip_tags($entry_link); | |
771 | ||
772 | if (ENABLE_SIMPLEPIE) { | |
773 | $entry_content = $item->get_description(); | |
774 | } else { | |
775 | $entry_content = $item["content:escaped"]; | |
776 | ||
777 | if (!$entry_content) $entry_content = $item["content:encoded"]; | |
778 | if (!$entry_content) $entry_content = $item["content"]; | |
779 | ||
780 | // Magpie bugs are getting ridiculous | |
781 | if (trim($entry_content) == "Array") $entry_content = false; | |
782 | ||
783 | if (!$entry_content) $entry_content = $item["atom_content"]; | |
784 | if (!$entry_content) $entry_content = $item["summary"]; | |
785 | if (!$entry_content) $entry_content = $item["description"]; | |
786 | ||
787 | // WTF | |
788 | if (is_array($entry_content)) { | |
789 | $entry_content = $entry_content["encoded"]; | |
790 | if (!$entry_content) $entry_content = $entry_content["escaped"]; | |
791 | } | |
792 | } | |
793 | ||
794 | if ($_GET["xdebug"]) { | |
795 | print "update_rss_feed: content: "; | |
796 | print_r(htmlspecialchars($entry_content)); | |
797 | } | |
798 | ||
799 | $entry_content_unescaped = $entry_content; | |
800 | ||
801 | if (ENABLE_SIMPLEPIE) { | |
802 | $entry_comments = strip_tags($item->data["comments"]); | |
803 | if ($item->get_author()) { | |
804 | $entry_author_item = $item->get_author(); | |
805 | $entry_author = $entry_author_item->get_name(); | |
806 | } | |
807 | } else { | |
808 | $entry_comments = strip_tags($item["comments"]); | |
809 | ||
810 | $entry_author = db_escape_string(strip_tags($item['dc']['creator'])); | |
811 | ||
812 | if ($item['author']) { | |
813 | ||
814 | if (is_array($item['author'])) { | |
815 | ||
816 | if (!$entry_author) { | |
817 | $entry_author = db_escape_string(strip_tags($item['author']['name'])); | |
818 | } | |
819 | ||
820 | if (!$entry_author) { | |
821 | $entry_author = db_escape_string(strip_tags($item['author']['email'])); | |
822 | } | |
823 | } | |
824 | ||
825 | if (!$entry_author) { | |
826 | $entry_author = db_escape_string(strip_tags($item['author'])); | |
827 | } | |
828 | } | |
829 | } | |
830 | ||
831 | if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = ''; | |
832 | ||
833 | $entry_guid = db_escape_string(strip_tags($entry_guid)); | |
834 | $entry_guid = mb_substr($entry_guid, 0, 250); | |
835 | ||
836 | $result = db_query($link, "SELECT id FROM ttrss_entries | |
837 | WHERE guid = '$entry_guid'"); | |
838 | ||
839 | $entry_content = db_escape_string($entry_content); | |
840 | ||
841 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); | |
842 | ||
843 | $entry_title = db_escape_string($entry_title); | |
844 | $entry_link = db_escape_string($entry_link); | |
845 | $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250); | |
846 | $entry_author = mb_substr($entry_author, 0, 250); | |
847 | ||
848 | if (ENABLE_SIMPLEPIE) { | |
849 | $num_comments = 0; #FIXME# | |
850 | } else { | |
851 | $num_comments = db_escape_string($item["slash"]["comments"]); | |
852 | } | |
853 | ||
854 | if (!$num_comments) $num_comments = 0; | |
855 | ||
856 | // parse <category> entries into tags | |
857 | ||
858 | if (ENABLE_SIMPLEPIE) { | |
859 | ||
860 | $additional_tags = array(); | |
861 | $additional_tags_src = $item->get_categories(); | |
862 | ||
863 | if (is_array($additional_tags_src)) { | |
864 | foreach ($additional_tags_src as $tobj) { | |
865 | array_push($additional_tags, $tobj->get_term()); | |
866 | } | |
867 | } | |
868 | ||
869 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
870 | _debug("update_rss_feed: category tags:"); | |
871 | print_r($additional_tags); | |
872 | } | |
873 | ||
874 | } else { | |
875 | ||
876 | $t_ctr = $item['category#']; | |
877 | ||
878 | $additional_tags = false; | |
879 | ||
880 | if ($t_ctr == 0) { | |
881 | $additional_tags = false; | |
882 | } else if ($t_ctr > 0) { | |
883 | $additional_tags = array($item['category']); | |
884 | ||
885 | if ($item['category@term']) { | |
886 | array_push($additional_tags, $item['category@term']); | |
887 | } | |
888 | ||
889 | for ($i = 0; $i <= $t_ctr; $i++ ) { | |
890 | if ($item["category#$i"]) { | |
891 | array_push($additional_tags, $item["category#$i"]); | |
892 | } | |
893 | ||
894 | if ($item["category#$i@term"]) { | |
895 | array_push($additional_tags, $item["category#$i@term"]); | |
896 | } | |
897 | } | |
898 | } | |
899 | ||
900 | // parse <dc:subject> elements | |
901 | ||
902 | $t_ctr = $item['dc']['subject#']; | |
903 | ||
904 | if ($t_ctr > 0) { | |
905 | $additional_tags = array($item['dc']['subject']); | |
906 | ||
907 | for ($i = 0; $i <= $t_ctr; $i++ ) { | |
908 | if ($item['dc']["subject#$i"]) { | |
909 | array_push($additional_tags, $item['dc']["subject#$i"]); | |
910 | } | |
911 | } | |
912 | } | |
913 | } | |
914 | ||
915 | // enclosures | |
916 | ||
917 | $enclosures = array(); | |
918 | ||
919 | if (ENABLE_SIMPLEPIE) { | |
920 | $encs = $item->get_enclosures(); | |
921 | ||
922 | foreach ($encs as $e) { | |
923 | $e_item = array( | |
924 | $e->link, $e->type, $e->length); | |
925 | ||
926 | array_push($enclosures, $e_item); | |
927 | } | |
928 | ||
929 | } else { | |
930 | $e_ctr = $item['enclosure#']; | |
931 | ||
932 | if ($e_ctr > 0) { | |
933 | $e_item = array($item['enclosure@url'], | |
934 | $item['enclosure@type'], | |
935 | $item['enclosure@length']); | |
936 | ||
937 | array_push($enclosures, $e_item); | |
938 | ||
939 | for ($i = 0; $i <= $e_ctr; $i++ ) { | |
940 | ||
941 | if ($item["enclosure#$i@url"]) { | |
942 | $e_item = array($item["enclosure#$i@url"], | |
943 | $item["enclosure#$i@type"], | |
944 | $item["enclosure#$i@length"]); | |
945 | array_push($enclosures, $e_item); | |
946 | } | |
947 | } | |
948 | } | |
949 | ||
950 | } | |
951 | ||
952 | # sanitize content | |
953 | ||
954 | // $entry_content = sanitize_rss($entry_content); | |
955 | ||
956 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
957 | _debug("update_rss_feed: done collecting data [TITLE:$entry_title]"); | |
958 | } | |
959 | ||
960 | db_query($link, "BEGIN"); | |
961 | ||
962 | if (db_num_rows($result) == 0) { | |
963 | ||
964 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
965 | _debug("update_rss_feed: base guid not found"); | |
966 | } | |
967 | ||
968 | // base post entry does not exist, create it | |
969 | ||
970 | $result = db_query($link, | |
971 | "INSERT INTO ttrss_entries | |
972 | (title, | |
973 | guid, | |
974 | link, | |
975 | updated, | |
976 | content, | |
977 | content_hash, | |
978 | no_orig_date, | |
979 | date_entered, | |
980 | comments, | |
981 | num_comments, | |
982 | author) | |
983 | VALUES | |
984 | ('$entry_title', | |
985 | '$entry_guid', | |
986 | '$entry_link', | |
987 | '$entry_timestamp_fmt', | |
988 | '$entry_content', | |
989 | '$content_hash', | |
990 | $no_orig_date, | |
991 | NOW(), | |
992 | '$entry_comments', | |
993 | '$num_comments', | |
994 | '$entry_author')"); | |
995 | } else { | |
996 | // we keep encountering the entry in feeds, so we need to | |
997 | // update date_entered column so that we don't get horrible | |
998 | // dupes when the entry gets purged and reinserted again e.g. | |
999 | // in the case of SLOW SLOW OMG SLOW updating feeds | |
1000 | ||
1001 | $base_entry_id = db_fetch_result($result, 0, "id"); | |
1002 | ||
1003 | db_query($link, "UPDATE ttrss_entries SET date_entered = NOW() | |
1004 | WHERE id = '$base_entry_id'"); | |
1005 | } | |
1006 | ||
1007 | // now it should exist, if not - bad luck then | |
1008 | ||
1009 | $result = db_query($link, "SELECT | |
1010 | id,content_hash,no_orig_date,title, | |
1011 | substring(date_entered,1,19) as date_entered, | |
1012 | substring(updated,1,19) as updated, | |
1013 | num_comments | |
1014 | FROM | |
1015 | ttrss_entries | |
1016 | WHERE guid = '$entry_guid'"); | |
1017 | ||
1018 | $entry_ref_id = 0; | |
1019 | $entry_int_id = 0; | |
1020 | ||
1021 | if (db_num_rows($result) == 1) { | |
1022 | ||
1023 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1024 | _debug("update_rss_feed: base guid found, checking for user record"); | |
1025 | } | |
1026 | ||
1027 | // this will be used below in update handler | |
1028 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); | |
1029 | $orig_title = db_fetch_result($result, 0, "title"); | |
1030 | $orig_num_comments = db_fetch_result($result, 0, "num_comments"); | |
1031 | $orig_date_entered = strtotime(db_fetch_result($result, | |
1032 | 0, "date_entered")); | |
1033 | ||
1034 | $ref_id = db_fetch_result($result, 0, "id"); | |
1035 | $entry_ref_id = $ref_id; | |
1036 | ||
1037 | // check for user post link to main table | |
1038 | ||
1039 | // do we allow duplicate posts with same GUID in different feeds? | |
1040 | if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) { | |
1041 | $dupcheck_qpart = "AND feed_id = '$feed'"; | |
1042 | } else { | |
1043 | $dupcheck_qpart = ""; | |
1044 | } | |
1045 | ||
1046 | // error_reporting(0); | |
1047 | ||
1048 | $article_filters = get_article_filters($filters, $entry_title, | |
1049 | $entry_content, $entry_link); | |
1050 | ||
1051 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1052 | _debug("update_rss_feed: article filters: "); | |
1053 | if (count($article_filters) != 0) { | |
1054 | print_r($article_filters); | |
1055 | } | |
1056 | } | |
1057 | ||
1058 | if (find_article_filter($article_filters, "filter")) { | |
1059 | continue; | |
1060 | } | |
1061 | ||
1062 | // error_reporting (DEFAULT_ERROR_LEVEL); | |
1063 | ||
1064 | $result = db_query($link, | |
1065 | "SELECT ref_id, int_id FROM ttrss_user_entries WHERE | |
1066 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' | |
1067 | $dupcheck_qpart"); | |
1068 | ||
1069 | // okay it doesn't exist - create user entry | |
1070 | if (db_num_rows($result) == 0) { | |
1071 | ||
1072 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1073 | _debug("update_rss_feed: user record not found, creating..."); | |
1074 | } | |
1075 | ||
1076 | if (!find_article_filter($article_filters, 'catchup')) { | |
1077 | $unread = 'true'; | |
1078 | $last_read_qpart = 'NULL'; | |
1079 | } else { | |
1080 | $unread = 'false'; | |
1081 | $last_read_qpart = 'NOW()'; | |
1082 | } | |
1083 | ||
1084 | if (find_article_filter($article_filters, 'mark')) { | |
1085 | $marked = 'true'; | |
1086 | } else { | |
1087 | $marked = 'false'; | |
1088 | } | |
1089 | ||
1090 | if (find_article_filter($article_filters, 'publish')) { | |
1091 | $published = 'true'; | |
1092 | } else { | |
1093 | $published = 'false'; | |
1094 | } | |
1095 | ||
1096 | $result = db_query($link, | |
1097 | "INSERT INTO ttrss_user_entries | |
1098 | (ref_id, owner_uid, feed_id, unread, last_read, marked, published) | |
1099 | VALUES ('$ref_id', '$owner_uid', '$feed', $unread, | |
1100 | $last_read_qpart, $marked, $published)"); | |
1101 | ||
1102 | $result = db_query($link, | |
1103 | "SELECT int_id FROM ttrss_user_entries WHERE | |
1104 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND | |
1105 | feed_id = '$feed' LIMIT 1"); | |
1106 | ||
1107 | if (db_num_rows($result) == 1) { | |
1108 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
1109 | } | |
1110 | } else { | |
1111 | $entry_ref_id = db_fetch_result($result, 0, "ref_id"); | |
1112 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
1113 | } | |
1114 | ||
1115 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1116 | _debug("update_rss_feed: RID: $entry_ref_id, IID: $entry_int_id"); | |
1117 | } | |
1118 | ||
1119 | $post_needs_update = false; | |
1120 | ||
1121 | if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) && | |
1122 | ($content_hash != $orig_content_hash)) { | |
1123 | // print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->"; | |
1124 | $post_needs_update = true; | |
1125 | } | |
1126 | ||
1127 | if (db_escape_string($orig_title) != $entry_title) { | |
1128 | $post_needs_update = true; | |
1129 | } | |
1130 | ||
1131 | if ($orig_num_comments != $num_comments) { | |
1132 | $post_needs_update = true; | |
1133 | } | |
1134 | ||
1135 | // this doesn't seem to be very reliable | |
1136 | // | |
1137 | // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) { | |
1138 | // $post_needs_update = true; | |
1139 | // } | |
1140 | ||
1141 | // if post needs update, update it and mark all user entries | |
1142 | // linking to this post as updated | |
1143 | if ($post_needs_update) { | |
1144 | ||
1145 | if (defined('DAEMON_EXTENDED_DEBUG')) { | |
1146 | _debug("update_rss_feed: post $entry_guid needs update..."); | |
1147 | } | |
1148 | ||
1149 | // print "<!-- post $orig_title needs update : $post_needs_update -->"; | |
1150 | ||
1151 | db_query($link, "UPDATE ttrss_entries | |
1152 | SET title = '$entry_title', content = '$entry_content', | |
1153 | content_hash = '$content_hash', | |
1154 | num_comments = '$num_comments' | |
1155 | WHERE id = '$ref_id'"); | |
1156 | ||
1157 | if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) { | |
1158 | db_query($link, "UPDATE ttrss_user_entries | |
1159 | SET last_read = null, unread = true WHERE ref_id = '$ref_id'"); | |
1160 | } else { | |
1161 | db_query($link, "UPDATE ttrss_user_entries | |
1162 | SET last_read = null WHERE ref_id = '$ref_id' AND unread = false"); | |
1163 | } | |
1164 | ||
1165 | } | |
1166 | } | |
1167 | ||
1168 | db_query($link, "COMMIT"); | |
1169 | ||
1170 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1171 | _debug("update_rss_feed: looking for enclosures..."); | |
1172 | } | |
1173 | ||
1174 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1175 | print_r($enclosures); | |
1176 | } | |
1177 | ||
1178 | db_query($link, "BEGIN"); | |
1179 | ||
1180 | foreach ($enclosures as $enc) { | |
1181 | $enc_url = db_escape_string($enc[0]); | |
1182 | $enc_type = db_escape_string($enc[1]); | |
1183 | $enc_dur = db_escape_string($enc[2]); | |
1184 | ||
1185 | $result = db_query($link, "SELECT id FROM ttrss_enclosures | |
1186 | WHERE content_url = '$enc_url' AND post_id = '$entry_ref_id'"); | |
1187 | ||
1188 | if (db_num_rows($result) == 0) { | |
1189 | db_query($link, "INSERT INTO ttrss_enclosures | |
1190 | (content_url, content_type, title, duration, post_id) VALUES | |
1191 | ('$enc_url', '$enc_type', '', '$enc_dur', '$entry_ref_id')"); | |
1192 | } | |
1193 | } | |
1194 | ||
1195 | db_query($link, "COMMIT"); | |
1196 | ||
1197 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1198 | _debug("update_rss_feed: looking for tags..."); | |
1199 | } | |
1200 | ||
1201 | /* taaaags */ | |
1202 | // <a href="..." rel="tag">Xorg</a>, // | |
1203 | ||
1204 | $entry_tags = null; | |
1205 | ||
1206 | preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i", | |
1207 | $entry_content_unescaped, $entry_tags); | |
1208 | ||
1209 | /* print "<p><br/>$entry_title : $entry_content_unescaped<br>"; | |
1210 | print_r($entry_tags); | |
1211 | print "<br/></p>"; */ | |
1212 | ||
1213 | $entry_tags = $entry_tags[1]; | |
1214 | ||
1215 | # check for manual tags | |
1216 | ||
1217 | $tag_filter = find_article_filter($article_filters, "tag"); | |
1218 | ||
1219 | if ($tag_filter) { | |
1220 | ||
1221 | $manual_tags = trim_array(split(",", $tag_filter[1])); | |
1222 | ||
1223 | foreach ($manual_tags as $tag) { | |
1224 | if (tag_is_valid($tag)) { | |
1225 | array_push($entry_tags, $tag); | |
1226 | } | |
1227 | } | |
1228 | } | |
1229 | ||
1230 | $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link, | |
1231 | 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8'))); | |
1232 | ||
1233 | if ($additional_tags && is_array($additional_tags)) { | |
1234 | foreach ($additional_tags as $tag) { | |
1235 | if (tag_is_valid($tag) && | |
1236 | array_search($tag, $boring_tags) === FALSE) { | |
1237 | array_push($entry_tags, $tag); | |
1238 | } | |
1239 | } | |
1240 | } | |
1241 | ||
1242 | // print "<p>TAGS: "; print_r($entry_tags); print "</p>"; | |
1243 | ||
1244 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1245 | print_r($entry_tags); | |
1246 | } | |
1247 | ||
1248 | if (count($entry_tags) > 0) { | |
1249 | ||
1250 | db_query($link, "BEGIN"); | |
1251 | ||
1252 | foreach ($entry_tags as $tag) { | |
1253 | ||
1254 | $tag = sanitize_tag($tag); | |
1255 | $tag = db_escape_string($tag); | |
1256 | ||
1257 | if (!tag_is_valid($tag)) continue; | |
1258 | ||
1259 | $result = db_query($link, "SELECT id FROM ttrss_tags | |
1260 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND | |
1261 | owner_uid = '$owner_uid' LIMIT 1"); | |
1262 | ||
1263 | // print db_fetch_result($result, 0, "id"); | |
1264 | ||
1265 | if ($result && db_num_rows($result) == 0) { | |
1266 | ||
1267 | db_query($link, "INSERT INTO ttrss_tags | |
1268 | (owner_uid,tag_name,post_int_id) | |
1269 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | db_query($link, "COMMIT"); | |
1274 | } | |
1275 | ||
1276 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1277 | _debug("update_rss_feed: article processed"); | |
1278 | } | |
1279 | } | |
1280 | ||
1281 | db_query($link, "UPDATE ttrss_feeds | |
1282 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
1283 | ||
1284 | // db_query($link, "COMMIT"); | |
1285 | ||
1286 | } else { | |
1287 | ||
1288 | if (ENABLE_SIMPLEPIE) { | |
1289 | $error_msg = mb_substr($rss->error(), 0, 250); | |
1290 | } else { | |
1291 | $error_msg = mb_substr(magpie_error(), 0, 250); | |
1292 | } | |
1293 | ||
1294 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1295 | _debug("update_rss_feed: error fetching feed: $error_msg"); | |
1296 | } | |
1297 | ||
1298 | $error_msg = db_escape_string($error_msg); | |
1299 | ||
1300 | db_query($link, | |
1301 | "UPDATE ttrss_feeds SET last_error = '$error_msg', | |
1302 | last_updated = NOW() WHERE id = '$feed'"); | |
1303 | } | |
1304 | ||
1305 | if (ENABLE_SIMPLEPIE) { | |
1306 | unset($rss); | |
1307 | } | |
1308 | ||
1309 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1310 | _debug("update_rss_feed: done"); | |
1311 | } | |
1312 | ||
1313 | } | |
1314 | ||
1315 | function print_select($id, $default, $values, $attributes = "") { | |
1316 | print "<select name=\"$id\" id=\"$id\" $attributes>"; | |
1317 | foreach ($values as $v) { | |
1318 | if ($v == $default) | |
1319 | $sel = " selected"; | |
1320 | else | |
1321 | $sel = ""; | |
1322 | ||
1323 | print "<option$sel>$v</option>"; | |
1324 | } | |
1325 | print "</select>"; | |
1326 | } | |
1327 | ||
1328 | function print_select_hash($id, $default, $values, $attributes = "") { | |
1329 | print "<select name=\"$id\" id='$id' $attributes>"; | |
1330 | foreach (array_keys($values) as $v) { | |
1331 | if ($v == $default) | |
1332 | $sel = "selected"; | |
1333 | else | |
1334 | $sel = ""; | |
1335 | ||
1336 | print "<option $sel value=\"$v\">".$values[$v]."</option>"; | |
1337 | } | |
1338 | ||
1339 | print "</select>"; | |
1340 | } | |
1341 | ||
1342 | function get_article_filters($filters, $title, $content, $link) { | |
1343 | $matches = array(); | |
1344 | ||
1345 | if ($filters["title"]) { | |
1346 | foreach ($filters["title"] as $filter) { | |
1347 | $reg_exp = $filter["reg_exp"]; | |
1348 | $inverse = $filter["inverse"]; | |
1349 | if ((!$inverse && preg_match("/$reg_exp/i", $title)) || | |
1350 | ($inverse && !preg_match("/$reg_exp/i", $title))) { | |
1351 | ||
1352 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
1353 | } | |
1354 | } | |
1355 | } | |
1356 | ||
1357 | if ($filters["content"]) { | |
1358 | foreach ($filters["content"] as $filter) { | |
1359 | $reg_exp = $filter["reg_exp"]; | |
1360 | $inverse = $filter["inverse"]; | |
1361 | ||
1362 | if ((!$inverse && preg_match("/$reg_exp/i", $content)) || | |
1363 | ($inverse && !preg_match("/$reg_exp/i", $content))) { | |
1364 | ||
1365 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
1366 | } | |
1367 | } | |
1368 | } | |
1369 | ||
1370 | if ($filters["both"]) { | |
1371 | foreach ($filters["both"] as $filter) { | |
1372 | $reg_exp = $filter["reg_exp"]; | |
1373 | $inverse = $filter["inverse"]; | |
1374 | ||
1375 | if ($inverse) { | |
1376 | if (!preg_match("/$reg_exp/i", $title) || !preg_match("/$reg_exp/i", $content)) { | |
1377 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
1378 | } | |
1379 | } else { | |
1380 | if (preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) { | |
1381 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
1382 | } | |
1383 | } | |
1384 | } | |
1385 | } | |
1386 | ||
1387 | if ($filters["link"]) { | |
1388 | $reg_exp = $filter["reg_exp"]; | |
1389 | foreach ($filters["link"] as $filter) { | |
1390 | $reg_exp = $filter["reg_exp"]; | |
1391 | $inverse = $filter["inverse"]; | |
1392 | ||
1393 | if ((!$inverse && preg_match("/$reg_exp/i", $link)) || | |
1394 | ($inverse && !preg_match("/$reg_exp/i", $link))) { | |
1395 | ||
1396 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
1397 | } | |
1398 | } | |
1399 | } | |
1400 | ||
1401 | return $matches; | |
1402 | } | |
1403 | ||
1404 | function find_article_filter($filters, $filter_name) { | |
1405 | foreach ($filters as $f) { | |
1406 | if ($f[0] == $filter_name) { | |
1407 | return $f; | |
1408 | }; | |
1409 | } | |
1410 | return false; | |
1411 | } | |
1412 | ||
1413 | function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link, | |
1414 | $rtl_content = false, $last_updated = false, $last_error = false) { | |
1415 | ||
1416 | if (file_exists($icon_file) && filesize($icon_file) > 0) { | |
1417 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">"; | |
1418 | } else { | |
1419 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">"; | |
1420 | } | |
1421 | ||
1422 | if ($rtl_content) { | |
1423 | $rtl_tag = "dir=\"rtl\""; | |
1424 | } else { | |
1425 | $rtl_tag = "dir=\"ltr\""; | |
1426 | } | |
1427 | ||
1428 | $error_notify_msg = ""; | |
1429 | ||
1430 | if ($last_error) { | |
1431 | $link_title = "Error: $last_error ($last_updated)"; | |
1432 | $error_notify_msg = "(Error)"; | |
1433 | } else if ($last_updated) { | |
1434 | $link_title = "Updated: $last_updated"; | |
1435 | } | |
1436 | ||
1437 | $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" | |
1438 | href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>"; | |
1439 | ||
1440 | print "<li id=\"FEEDR-$feed_id\" class=\"$class\">"; | |
1441 | if (get_pref($link, 'ENABLE_FEED_ICONS')) { | |
1442 | print "$feed_icon"; | |
1443 | } | |
1444 | ||
1445 | print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>"; | |
1446 | ||
1447 | if ($unread != 0) { | |
1448 | $fctr_class = ""; | |
1449 | } else { | |
1450 | $fctr_class = "class=\"invisible\""; | |
1451 | } | |
1452 | ||
1453 | print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\"> | |
1454 | (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>"; | |
1455 | ||
1456 | if (get_pref($link, "EXTENDED_FEEDLIST")) { | |
1457 | print "<div class=\"feedExtInfo\"> | |
1458 | <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>"; | |
1459 | } | |
1460 | ||
1461 | print "</li>"; | |
1462 | ||
1463 | } | |
1464 | ||
1465 | function getmicrotime() { | |
1466 | list($usec, $sec) = explode(" ",microtime()); | |
1467 | return ((float)$usec + (float)$sec); | |
1468 | } | |
1469 | ||
1470 | function print_radio($id, $default, $true_is, $values, $attributes = "") { | |
1471 | foreach ($values as $v) { | |
1472 | ||
1473 | if ($v == $default) | |
1474 | $sel = "checked"; | |
1475 | else | |
1476 | $sel = ""; | |
1477 | ||
1478 | if ($v == $true_is) { | |
1479 | $sel .= " value=\"1\""; | |
1480 | } else { | |
1481 | $sel .= " value=\"0\""; | |
1482 | } | |
1483 | ||
1484 | print "<input class=\"noborder\" | |
1485 | type=\"radio\" $sel $attributes name=\"$id\"> $v "; | |
1486 | ||
1487 | } | |
1488 | } | |
1489 | ||
1490 | function initialize_user_prefs($link, $uid) { | |
1491 | ||
1492 | $uid = db_escape_string($uid); | |
1493 | ||
1494 | db_query($link, "BEGIN"); | |
1495 | ||
1496 | $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs"); | |
1497 | ||
1498 | $u_result = db_query($link, "SELECT pref_name | |
1499 | FROM ttrss_user_prefs WHERE owner_uid = '$uid'"); | |
1500 | ||
1501 | $active_prefs = array(); | |
1502 | ||
1503 | while ($line = db_fetch_assoc($u_result)) { | |
1504 | array_push($active_prefs, $line["pref_name"]); | |
1505 | } | |
1506 | ||
1507 | while ($line = db_fetch_assoc($result)) { | |
1508 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
1509 | // print "adding " . $line["pref_name"] . "<br>"; | |
1510 | ||
1511 | db_query($link, "INSERT INTO ttrss_user_prefs | |
1512 | (owner_uid,pref_name,value) VALUES | |
1513 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); | |
1514 | ||
1515 | } | |
1516 | } | |
1517 | ||
1518 | db_query($link, "COMMIT"); | |
1519 | ||
1520 | } | |
1521 | ||
1522 | function lookup_user_id($link, $user) { | |
1523 | ||
1524 | $result = db_query($link, "SELECT id FROM ttrss_users WHERE | |
1525 | login = '$login'"); | |
1526 | ||
1527 | if (db_num_rows($result) == 1) { | |
1528 | return db_fetch_result($result, 0, "id"); | |
1529 | } else { | |
1530 | return false; | |
1531 | } | |
1532 | } | |
1533 | ||
1534 | function http_authenticate_user($link) { | |
1535 | ||
1536 | error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log'); | |
1537 | ||
1538 | if (!$_SERVER["PHP_AUTH_USER"]) { | |
1539 | ||
1540 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"'); | |
1541 | header('HTTP/1.0 401 Unauthorized'); | |
1542 | exit; | |
1543 | ||
1544 | } else { | |
1545 | $auth_result = authenticate_user($link, | |
1546 | $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]); | |
1547 | ||
1548 | if (!$auth_result) { | |
1549 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"'); | |
1550 | header('HTTP/1.0 401 Unauthorized'); | |
1551 | exit; | |
1552 | } | |
1553 | } | |
1554 | ||
1555 | return true; | |
1556 | } | |
1557 | ||
1558 | function authenticate_user($link, $login, $password, $force_auth = false) { | |
1559 | ||
1560 | if (!SINGLE_USER_MODE) { | |
1561 | ||
1562 | $pwd_hash1 = encrypt_password($password); | |
1563 | $pwd_hash2 = encrypt_password($password, $login); | |
1564 | ||
1565 | if (defined('ALLOW_REMOTE_USER_AUTH') && ALLOW_REMOTE_USER_AUTH | |
1566 | && $_SERVER["REMOTE_USER"]) { | |
1567 | ||
1568 | $login = db_escape_string($_SERVER["REMOTE_USER"]); | |
1569 | ||
1570 | $query = "SELECT id,login,access_level | |
1571 | FROM ttrss_users WHERE | |
1572 | login = '$login'"; | |
1573 | ||
1574 | } else { | |
1575 | $query = "SELECT id,login,access_level,pwd_hash | |
1576 | FROM ttrss_users WHERE | |
1577 | login = '$login' AND (pwd_hash = '$pwd_hash1' OR | |
1578 | pwd_hash = '$pwd_hash2')"; | |
1579 | } | |
1580 | ||
1581 | $result = db_query($link, $query); | |
1582 | ||
1583 | if (db_num_rows($result) == 1) { | |
1584 | $_SESSION["uid"] = db_fetch_result($result, 0, "id"); | |
1585 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); | |
1586 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); | |
1587 | ||
1588 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
1589 | $_SESSION["uid"]); | |
1590 | ||
1591 | $user_theme = get_user_theme_path($link); | |
1592 | ||
1593 | $_SESSION["theme"] = $user_theme; | |
1594 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
1595 | $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash"); | |
1596 | ||
1597 | initialize_user_prefs($link, $_SESSION["uid"]); | |
1598 | ||
1599 | return true; | |
1600 | } | |
1601 | ||
1602 | return false; | |
1603 | ||
1604 | } else { | |
1605 | ||
1606 | $_SESSION["uid"] = 1; | |
1607 | $_SESSION["name"] = "admin"; | |
1608 | ||
1609 | $user_theme = get_user_theme_path($link); | |
1610 | ||
1611 | $_SESSION["theme"] = $user_theme; | |
1612 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
1613 | ||
1614 | initialize_user_prefs($link, $_SESSION["uid"]); | |
1615 | ||
1616 | return true; | |
1617 | } | |
1618 | } | |
1619 | ||
1620 | function make_password($length = 8) { | |
1621 | ||
1622 | $password = ""; | |
1623 | $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ"; | |
1624 | ||
1625 | $i = 0; | |
1626 | ||
1627 | while ($i < $length) { | |
1628 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
1629 | ||
1630 | if (!strstr($password, $char)) { | |
1631 | $password .= $char; | |
1632 | $i++; | |
1633 | } | |
1634 | } | |
1635 | return $password; | |
1636 | } | |
1637 | ||
1638 | // this is called after user is created to initialize default feeds, labels | |
1639 | // or whatever else | |
1640 | ||
1641 | // user preferences are checked on every login, not here | |
1642 | ||
1643 | function initialize_user($link, $uid) { | |
1644 | ||
1645 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
1646 | values ('$uid','unread = true', 'Unread articles')"); | |
1647 | ||
1648 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
1649 | values ('$uid','last_read is null and unread = false', 'Updated articles')"); | |
1650 | ||
1651 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
1652 | values ('$uid', 'Tiny Tiny RSS: New Releases', | |
1653 | 'http://tt-rss.spb.ru/releases.rss')"); | |
1654 | ||
1655 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
1656 | values ('$uid', 'Tiny Tiny RSS: Forum', | |
1657 | 'http://tt-rss.spb.ru/forum/rss.php')"); | |
1658 | } | |
1659 | ||
1660 | function logout_user() { | |
1661 | session_destroy(); | |
1662 | if (isset($_COOKIE[session_name()])) { | |
1663 | setcookie(session_name(), '', time()-42000, '/'); | |
1664 | } | |
1665 | } | |
1666 | ||
1667 | function get_script_urlpath() { | |
1668 | return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]); | |
1669 | } | |
1670 | ||
1671 | function validate_session($link) { | |
1672 | if (SINGLE_USER_MODE) { | |
1673 | return true; | |
1674 | } | |
1675 | ||
1676 | if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) { | |
1677 | if ($_SESSION["ip_address"]) { | |
1678 | if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) { | |
1679 | $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)"; | |
1680 | return false; | |
1681 | } | |
1682 | } | |
1683 | } | |
1684 | ||
1685 | if ($_SESSION["uid"]) { | |
1686 | ||
1687 | $result = db_query($link, | |
1688 | "SELECT pwd_hash FROM ttrss_users WHERE id = '".$_SESSION["uid"]."'"); | |
1689 | ||
1690 | $pwd_hash = db_fetch_result($result, 0, "pwd_hash"); | |
1691 | ||
1692 | if ($pwd_hash != $_SESSION["pwd_hash"]) { | |
1693 | return false; | |
1694 | } | |
1695 | } | |
1696 | ||
1697 | /* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) { | |
1698 | ||
1699 | //print_r($_SESSION); | |
1700 | ||
1701 | if (time() > $_SESSION["cookie_lifetime"]) { | |
1702 | return false; | |
1703 | } | |
1704 | } */ | |
1705 | ||
1706 | return true; | |
1707 | } | |
1708 | ||
1709 | function login_sequence($link, $mobile = false) { | |
1710 | if (!SINGLE_USER_MODE) { | |
1711 | ||
1712 | if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) { | |
1713 | $swu = db_escape_string($_REQUEST["swu"]); | |
1714 | if ($swu) { | |
1715 | $_SESSION["prefs_cache"] = false; | |
1716 | return authenticate_user($link, $swu, null, true); | |
1717 | } | |
1718 | } | |
1719 | ||
1720 | $login_action = $_POST["login_action"]; | |
1721 | ||
1722 | # try to authenticate user if called from login form | |
1723 | if ($login_action == "do_login") { | |
1724 | $login = $_POST["login"]; | |
1725 | $password = $_POST["password"]; | |
1726 | $remember_me = $_POST["remember_me"]; | |
1727 | ||
1728 | if (authenticate_user($link, $login, $password)) { | |
1729 | $_POST["password"] = ""; | |
1730 | ||
1731 | $_SESSION["language"] = $_POST["language"]; | |
1732 | ||
1733 | header("Location: " . $_SERVER["REQUEST_URI"]); | |
1734 | exit; | |
1735 | ||
1736 | return; | |
1737 | } else { | |
1738 | $_SESSION["login_error_msg"] = "Incorrect username or password"; | |
1739 | } | |
1740 | } | |
1741 | ||
1742 | // print session_id(); | |
1743 | // print_r($_SESSION); | |
1744 | ||
1745 | if (!$_SESSION["uid"] || !validate_session($link)) { | |
1746 | render_login_form($link, $mobile); | |
1747 | exit; | |
1748 | } else { | |
1749 | /* bump login timestamp */ | |
1750 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
1751 | $_SESSION["uid"]); | |
1752 | ||
1753 | if ($_SESSION["language"] && SESSION_COOKIE_LIFETIME > 0) { | |
1754 | setcookie("ttrss_lang", $_SESSION["language"], | |
1755 | time() + SESSION_COOKIE_LIFETIME); | |
1756 | } | |
1757 | } | |
1758 | ||
1759 | } else { | |
1760 | return authenticate_user($link, "admin", null); | |
1761 | } | |
1762 | } | |
1763 | ||
1764 | function truncate_string($str, $max_len) { | |
1765 | if (mb_strlen($str, "utf-8") > $max_len - 3) { | |
1766 | return mb_substr($str, 0, $max_len, "utf-8") . "…"; | |
1767 | } else { | |
1768 | return $str; | |
1769 | } | |
1770 | } | |
1771 | ||
1772 | function get_user_theme_path($link) { | |
1773 | $result = db_query($link, "SELECT theme_path | |
1774 | FROM | |
1775 | ttrss_themes,ttrss_users | |
1776 | WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]); | |
1777 | if (db_num_rows($result) != 0) { | |
1778 | return db_fetch_result($result, 0, "theme_path"); | |
1779 | } else { | |
1780 | return null; | |
1781 | } | |
1782 | } | |
1783 | ||
1784 | function smart_date_time($timestamp) { | |
1785 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1786 | return date("G:i", $timestamp); | |
1787 | } else if (date("Y", $timestamp) == date("Y")) { | |
1788 | return date("M d, G:i", $timestamp); | |
1789 | } else { | |
1790 | return date("Y/m/d, G:i", $timestamp); | |
1791 | } | |
1792 | } | |
1793 | ||
1794 | function smart_date($timestamp) { | |
1795 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1796 | return "Today"; | |
1797 | } else if (date("Y", $timestamp) == date("Y")) { | |
1798 | return date("D m", $timestamp); | |
1799 | } else { | |
1800 | return date("Y/m/d", $timestamp); | |
1801 | } | |
1802 | } | |
1803 | ||
1804 | function sql_bool_to_string($s) { | |
1805 | if ($s == "t" || $s == "1") { | |
1806 | return "true"; | |
1807 | } else { | |
1808 | return "false"; | |
1809 | } | |
1810 | } | |
1811 | ||
1812 | function sql_bool_to_bool($s) { | |
1813 | if ($s == "t" || $s == "1") { | |
1814 | return true; | |
1815 | } else { | |
1816 | return false; | |
1817 | } | |
1818 | } | |
1819 | ||
1820 | ||
1821 | function toggleEvenOdd($a) { | |
1822 | if ($a == "even") | |
1823 | return "odd"; | |
1824 | else | |
1825 | return "even"; | |
1826 | } | |
1827 | ||
1828 | function sanity_check($link) { | |
1829 | ||
1830 | error_reporting(0); | |
1831 | ||
1832 | $error_code = 0; | |
1833 | $result = db_query($link, "SELECT schema_version FROM ttrss_version"); | |
1834 | $schema_version = db_fetch_result($result, 0, "schema_version"); | |
1835 | ||
1836 | if ($schema_version != SCHEMA_VERSION) { | |
1837 | $error_code = 5; | |
1838 | } | |
1839 | ||
1840 | if (DB_TYPE == "mysql") { | |
1841 | $result = db_query($link, "SELECT true", false); | |
1842 | if (db_num_rows($result) != 1) { | |
1843 | $error_code = 10; | |
1844 | } | |
1845 | } | |
1846 | ||
1847 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1848 | ||
1849 | if ($error_code != 0) { | |
1850 | print_error_xml($error_code); | |
1851 | return false; | |
1852 | } else { | |
1853 | return true; | |
1854 | } | |
1855 | } | |
1856 | ||
1857 | function file_is_locked($filename) { | |
1858 | if (function_exists('flock')) { | |
1859 | error_reporting(0); | |
1860 | $fp = fopen($filename, "r"); | |
1861 | error_reporting(DEFAULT_ERROR_LEVEL); | |
1862 | if ($fp) { | |
1863 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1864 | flock($fp, LOCK_UN); | |
1865 | fclose($fp); | |
1866 | return false; | |
1867 | } | |
1868 | fclose($fp); | |
1869 | return true; | |
1870 | } | |
1871 | } | |
1872 | return false; | |
1873 | } | |
1874 | ||
1875 | function make_lockfile($filename) { | |
1876 | $fp = fopen($filename, "w"); | |
1877 | ||
1878 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1879 | return $fp; | |
1880 | } else { | |
1881 | return false; | |
1882 | } | |
1883 | } | |
1884 | ||
1885 | function make_stampfile($filename) { | |
1886 | $fp = fopen($filename, "w"); | |
1887 | ||
1888 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1889 | fwrite($fp, time() . "\n"); | |
1890 | flock($fp, LOCK_UN); | |
1891 | fclose($fp); | |
1892 | return true; | |
1893 | } else { | |
1894 | return false; | |
1895 | } | |
1896 | } | |
1897 | ||
1898 | function read_stampfile($filename) { | |
1899 | ||
1900 | error_reporting(0); | |
1901 | $fp = fopen($filename, "r"); | |
1902 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1903 | ||
1904 | if (flock($fp, LOCK_EX)) { | |
1905 | $stamp = fgets($fp); | |
1906 | flock($fp, LOCK_UN); | |
1907 | fclose($fp); | |
1908 | return $stamp; | |
1909 | } else { | |
1910 | return false; | |
1911 | } | |
1912 | } | |
1913 | ||
1914 | function sql_random_function() { | |
1915 | if (DB_TYPE == "mysql") { | |
1916 | return "RAND()"; | |
1917 | } else { | |
1918 | return "RANDOM()"; | |
1919 | } | |
1920 | } | |
1921 | ||
1922 | function catchup_feed($link, $feed, $cat_view) { | |
1923 | ||
1924 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
1925 | ||
1926 | if ($cat_view) { | |
1927 | ||
1928 | if ($feed > 0) { | |
1929 | $cat_qpart = "cat_id = '$feed'"; | |
1930 | } else { | |
1931 | $cat_qpart = "cat_id IS NULL"; | |
1932 | } | |
1933 | ||
1934 | $tmp_result = db_query($link, "SELECT id | |
1935 | FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " . | |
1936 | $_SESSION["uid"]); | |
1937 | ||
1938 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1939 | ||
1940 | $tmp_feed = $tmp_line["id"]; | |
1941 | ||
1942 | db_query($link, "UPDATE ttrss_user_entries | |
1943 | SET unread = false,last_read = NOW() | |
1944 | WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]); | |
1945 | } | |
1946 | ||
1947 | } else if ($feed > 0) { | |
1948 | ||
1949 | $tmp_result = db_query($link, "SELECT id | |
1950 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
1951 | ORDER BY cat_id,title"); | |
1952 | ||
1953 | $parent_ids = array(); | |
1954 | ||
1955 | if (db_num_rows($tmp_result) > 0) { | |
1956 | while ($p = db_fetch_assoc($tmp_result)) { | |
1957 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
1958 | } | |
1959 | ||
1960 | $children_qpart = implode(" OR ", $parent_ids); | |
1961 | ||
1962 | db_query($link, "UPDATE ttrss_user_entries | |
1963 | SET unread = false,last_read = NOW() | |
1964 | WHERE (feed_id = '$feed' OR $children_qpart) | |
1965 | AND owner_uid = " . $_SESSION["uid"]); | |
1966 | ||
1967 | } else { | |
1968 | db_query($link, "UPDATE ttrss_user_entries | |
1969 | SET unread = false,last_read = NOW() | |
1970 | WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
1971 | } | |
1972 | ||
1973 | } else if ($feed < 0 && $feed > -10) { // special, like starred | |
1974 | ||
1975 | if ($feed == -1) { | |
1976 | db_query($link, "UPDATE ttrss_user_entries | |
1977 | SET unread = false,last_read = NOW() | |
1978 | WHERE marked = true AND owner_uid = ".$_SESSION["uid"]); | |
1979 | } | |
1980 | ||
1981 | if ($feed == -2) { | |
1982 | db_query($link, "UPDATE ttrss_user_entries | |
1983 | SET unread = false,last_read = NOW() | |
1984 | WHERE published = true AND owner_uid = ".$_SESSION["uid"]); | |
1985 | } | |
1986 | ||
1987 | if ($feed == -3) { | |
1988 | ||
1989 | $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE"); | |
1990 | ||
1991 | if (DB_TYPE == "pgsql") { | |
1992 | $match_part = "date_entered > NOW() - INTERVAL '$intl hour' "; | |
1993 | } else { | |
1994 | $match_part = "date_entered > DATE_SUB(NOW(), | |
1995 | INTERVAL $intl HOUR) "; | |
1996 | } | |
1997 | ||
1998 | $result = db_query($link, "SELECT id FROM ttrss_entries, | |
1999 | ttrss_user_entries WHERE $match_part AND | |
2000 | unread = true AND | |
2001 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2002 | owner_uid = ".$_SESSION["uid"]); | |
2003 | ||
2004 | $affected_ids = array(); | |
2005 | ||
2006 | while ($line = db_fetch_assoc($result)) { | |
2007 | array_push($affected_ids, $line["id"]); | |
2008 | } | |
2009 | ||
2010 | catchupArticlesById($link, $affected_ids, 0); | |
2011 | } | |
2012 | ||
2013 | } else if ($feed < -10) { // label | |
2014 | ||
2015 | // TODO make this more efficient | |
2016 | ||
2017 | $label_id = -$feed - 11; | |
2018 | ||
2019 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
2020 | WHERE id = '$label_id'"); | |
2021 | ||
2022 | if ($tmp_result) { | |
2023 | $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp"); | |
2024 | ||
2025 | db_query($link, "BEGIN"); | |
2026 | ||
2027 | $tmp2_result = db_query($link, | |
2028 | "SELECT | |
2029 | int_id | |
2030 | FROM | |
2031 | ttrss_user_entries,ttrss_entries,ttrss_feeds | |
2032 | WHERE | |
2033 | ref_id = ttrss_entries.id AND | |
2034 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
2035 | $sql_exp AND | |
2036 | ttrss_user_entries.owner_uid = " . $_SESSION["uid"]); | |
2037 | ||
2038 | while ($tmp_line = db_fetch_assoc($tmp2_result)) { | |
2039 | db_query($link, "UPDATE | |
2040 | ttrss_user_entries | |
2041 | SET | |
2042 | unread = false, last_read = NOW() | |
2043 | WHERE | |
2044 | int_id = " . $tmp_line["int_id"]); | |
2045 | } | |
2046 | ||
2047 | db_query($link, "COMMIT"); | |
2048 | ||
2049 | /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries | |
2050 | SET unread = false,last_read = NOW() | |
2051 | WHERE $sql_exp | |
2052 | AND ref_id = id | |
2053 | AND owner_uid = ".$_SESSION["uid"]); */ | |
2054 | } | |
2055 | } | |
2056 | } else { // tag | |
2057 | db_query($link, "BEGIN"); | |
2058 | ||
2059 | $tag_name = db_escape_string($feed); | |
2060 | ||
2061 | $result = db_query($link, "SELECT post_int_id FROM ttrss_tags | |
2062 | WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]); | |
2063 | ||
2064 | while ($line = db_fetch_assoc($result)) { | |
2065 | db_query($link, "UPDATE ttrss_user_entries SET | |
2066 | unread = false, last_read = NOW() | |
2067 | WHERE int_id = " . $line["post_int_id"]); | |
2068 | } | |
2069 | db_query($link, "COMMIT"); | |
2070 | } | |
2071 | } | |
2072 | ||
2073 | function update_generic_feed($link, $feed, $cat_view, $force_update = false) { | |
2074 | if ($cat_view) { | |
2075 | ||
2076 | if ($feed > 0) { | |
2077 | $cat_qpart = "cat_id = '$feed'"; | |
2078 | } else { | |
2079 | $cat_qpart = "cat_id IS NULL"; | |
2080 | } | |
2081 | ||
2082 | $tmp_result = db_query($link, "SELECT id,feed_url FROM ttrss_feeds | |
2083 | WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]); | |
2084 | ||
2085 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
2086 | $feed_url = $tmp_line["feed_url"]; | |
2087 | $feed_id = $tmp_line["id"]; | |
2088 | update_rss_feed($link, $feed_url, $feed_id, $force_update); | |
2089 | } | |
2090 | ||
2091 | } else { | |
2092 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
2093 | WHERE id = '$feed'"); | |
2094 | $feed_url = db_fetch_result($tmp_result, 0, "feed_url"); | |
2095 | update_rss_feed($link, $feed_url, $feed, $force_update); | |
2096 | } | |
2097 | } | |
2098 | ||
2099 | function getAllCounters($link, $omode = "flc", $active_feed = false) { | |
2100 | /* getLabelCounters($link); | |
2101 | getFeedCounters($link); | |
2102 | getTagCounters($link); | |
2103 | getGlobalCounters($link); | |
2104 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
2105 | getCategoryCounters($link); | |
2106 | } */ | |
2107 | ||
2108 | if (!$omode) $omode = "flc"; | |
2109 | ||
2110 | getGlobalCounters($link); | |
2111 | ||
2112 | if (strchr($omode, "l")) getLabelCounters($link); | |
2113 | if (strchr($omode, "f")) getFeedCounters($link, SMART_RPC_COUNTERS, $active_feed); | |
2114 | if (strchr($omode, "t")) getTagCounters($link); | |
2115 | if (strchr($omode, "c")) { | |
2116 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
2117 | getCategoryCounters($link); | |
2118 | } | |
2119 | } | |
2120 | } | |
2121 | ||
2122 | function getCategoryCounters($link) { | |
2123 | # two special categories are -1 and -2 (all virtuals; all labels) | |
2124 | ||
2125 | $ctr = getCategoryUnread($link, -1); | |
2126 | ||
2127 | print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>"; | |
2128 | ||
2129 | $ctr = getCategoryUnread($link, -2); | |
2130 | ||
2131 | print "<counter type=\"category\" id=\"-2\" counter=\"$ctr\"/>"; | |
2132 | ||
2133 | $age_qpart = getMaxAgeSubquery(); | |
2134 | ||
2135 | $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id) | |
2136 | FROM ttrss_user_entries, ttrss_entries WHERE feed_id = ttrss_feeds.id | |
2137 | AND id = ref_id AND $age_qpart | |
2138 | AND unread = true)) AS unread FROM ttrss_feeds | |
2139 | WHERE | |
2140 | hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id"); | |
2141 | ||
2142 | while ($line = db_fetch_assoc($result)) { | |
2143 | $line["cat_id"] = sprintf("%d", $line["cat_id"]); | |
2144 | print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"". | |
2145 | $line["unread"]."\"/>"; | |
2146 | } | |
2147 | } | |
2148 | ||
2149 | function getCategoryUnread($link, $cat) { | |
2150 | ||
2151 | if ($cat >= 0) { | |
2152 | ||
2153 | if ($cat != 0) { | |
2154 | $cat_query = "cat_id = '$cat'"; | |
2155 | } else { | |
2156 | $cat_query = "cat_id IS NULL"; | |
2157 | } | |
2158 | ||
2159 | $age_qpart = getMaxAgeSubquery(); | |
2160 | ||
2161 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query | |
2162 | AND hidden = false | |
2163 | AND owner_uid = " . $_SESSION["uid"]); | |
2164 | ||
2165 | $cat_feeds = array(); | |
2166 | while ($line = db_fetch_assoc($result)) { | |
2167 | array_push($cat_feeds, "feed_id = " . $line["id"]); | |
2168 | } | |
2169 | ||
2170 | if (count($cat_feeds) == 0) return 0; | |
2171 | ||
2172 | $match_part = implode(" OR ", $cat_feeds); | |
2173 | ||
2174 | $result = db_query($link, "SELECT COUNT(int_id) AS unread | |
2175 | FROM ttrss_user_entries,ttrss_entries | |
2176 | WHERE unread = true AND ($match_part) AND id = ref_id | |
2177 | AND $age_qpart AND owner_uid = " . $_SESSION["uid"]); | |
2178 | ||
2179 | $unread = 0; | |
2180 | ||
2181 | # this needs to be rewritten | |
2182 | while ($line = db_fetch_assoc($result)) { | |
2183 | $unread += $line["unread"]; | |
2184 | } | |
2185 | ||
2186 | return $unread; | |
2187 | } else if ($cat == -1) { | |
2188 | return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3); | |
2189 | } else if ($cat == -2) { | |
2190 | ||
2191 | $rv = getLabelCounters($link, false, true); | |
2192 | $ctr = 0; | |
2193 | ||
2194 | foreach (array_keys($rv) as $k) { | |
2195 | if ($k < -10) { | |
2196 | $ctr += $rv[$k]["counter"]; | |
2197 | } | |
2198 | } | |
2199 | ||
2200 | return $ctr; | |
2201 | } | |
2202 | } | |
2203 | ||
2204 | function getMaxAgeSubquery($days = COUNTERS_MAX_AGE) { | |
2205 | if (DB_TYPE == "pgsql") { | |
2206 | return "ttrss_entries.date_entered > | |
2207 | NOW() - INTERVAL '$days days'"; | |
2208 | } else { | |
2209 | return "ttrss_entries.date_entered > | |
2210 | DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
2211 | } | |
2212 | } | |
2213 | ||
2214 | function getFeedUnread($link, $feed, $is_cat = false) { | |
2215 | $n_feed = sprintf("%d", $feed); | |
2216 | ||
2217 | $age_qpart = getMaxAgeSubquery(); | |
2218 | ||
2219 | if ($is_cat) { | |
2220 | return getCategoryUnread($link, $n_feed); | |
2221 | } else if ($n_feed == -1) { | |
2222 | $match_part = "marked = true"; | |
2223 | } else if ($n_feed == -2) { | |
2224 | $match_part = "published = true"; | |
2225 | } else if ($n_feed == -3) { | |
2226 | $match_part = "unread = true"; | |
2227 | ||
2228 | $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE"); | |
2229 | ||
2230 | if (DB_TYPE == "pgsql") { | |
2231 | $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' "; | |
2232 | } else { | |
2233 | $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) "; | |
2234 | } | |
2235 | ||
2236 | } else if ($n_feed > 0) { | |
2237 | ||
2238 | $result = db_query($link, "SELECT id FROM ttrss_feeds | |
2239 | WHERE parent_feed = '$n_feed' | |
2240 | AND hidden = false | |
2241 | AND owner_uid = " . $_SESSION["uid"]); | |
2242 | ||
2243 | if (db_num_rows($result) > 0) { | |
2244 | ||
2245 | $linked_feeds = array(); | |
2246 | while ($line = db_fetch_assoc($result)) { | |
2247 | array_push($linked_feeds, "feed_id = " . $line["id"]); | |
2248 | } | |
2249 | ||
2250 | array_push($linked_feeds, "feed_id = $n_feed"); | |
2251 | ||
2252 | $match_part = implode(" OR ", $linked_feeds); | |
2253 | ||
2254 | $result = db_query($link, "SELECT COUNT(int_id) AS unread | |
2255 | FROM ttrss_user_entries,ttrss_entries | |
2256 | WHERE unread = true AND | |
2257 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2258 | $age_qpart AND | |
2259 | ($match_part) AND | |
2260 | owner_uid = " . $_SESSION["uid"]); | |
2261 | ||
2262 | $unread = 0; | |
2263 | ||
2264 | # this needs to be rewritten | |
2265 | while ($line = db_fetch_assoc($result)) { | |
2266 | $unread += $line["unread"]; | |
2267 | } | |
2268 | ||
2269 | return $unread; | |
2270 | ||
2271 | } else { | |
2272 | $match_part = "feed_id = '$n_feed'"; | |
2273 | } | |
2274 | } else if ($feed < -10) { | |
2275 | ||
2276 | $label_id = -$feed - 11; | |
2277 | ||
2278 | $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE | |
2279 | id = '$label_id' AND owner_uid = " . $_SESSION["uid"]); | |
2280 | ||
2281 | $match_part = db_fetch_result($result, 0, "sql_exp"); | |
2282 | } | |
2283 | ||
2284 | if ($match_part) { | |
2285 | ||
2286 | $result = db_query($link, "SELECT count(int_id) AS unread | |
2287 | FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE | |
2288 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
2289 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2290 | ttrss_feeds.hidden = false AND | |
2291 | $age_qpart AND | |
2292 | unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]); | |
2293 | ||
2294 | } else { | |
2295 | ||
2296 | $result = db_query($link, "SELECT COUNT(post_int_id) AS unread | |
2297 | FROM ttrss_tags,ttrss_user_entries,ttrss_entries | |
2298 | WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id | |
2299 | AND unread = true AND $age_qpart AND | |
2300 | ttrss_tags.owner_uid = " . $_SESSION["uid"]); | |
2301 | } | |
2302 | ||
2303 | $unread = db_fetch_result($result, 0, "unread"); | |
2304 | ||
2305 | return $unread; | |
2306 | } | |
2307 | ||
2308 | /* FIXME this needs reworking */ | |
2309 | ||
2310 | function getGlobalUnread($link, $user_id = false) { | |
2311 | ||
2312 | if (!$user_id) { | |
2313 | $user_id = $_SESSION["uid"]; | |
2314 | } | |
2315 | ||
2316 | $age_qpart = getMaxAgeSubquery(); | |
2317 | ||
2318 | $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds | |
2319 | WHERE unread = true AND | |
2320 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
2321 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2322 | hidden = false AND | |
2323 | $age_qpart AND | |
2324 | ttrss_user_entries.owner_uid = '$user_id'"); | |
2325 | $c_id = db_fetch_result($result, 0, "c_id"); | |
2326 | return $c_id; | |
2327 | } | |
2328 | ||
2329 | function getGlobalCounters($link, $global_unread = -1) { | |
2330 | if ($global_unread == -1) { | |
2331 | $global_unread = getGlobalUnread($link); | |
2332 | } | |
2333 | print "<counter type=\"global\" id='global-unread' | |
2334 | counter='$global_unread'/>"; | |
2335 | ||
2336 | $result = db_query($link, "SELECT COUNT(id) AS fn FROM | |
2337 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
2338 | ||
2339 | $subscribed_feeds = db_fetch_result($result, 0, "fn"); | |
2340 | ||
2341 | print "<counter type=\"global\" id='subscribed-feeds' | |
2342 | counter='$subscribed_feeds'/>"; | |
2343 | ||
2344 | } | |
2345 | ||
2346 | function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
2347 | ||
2348 | if ($smart_mode) { | |
2349 | if (!$_SESSION["tctr_last_value"]) { | |
2350 | $_SESSION["tctr_last_value"] = array(); | |
2351 | } | |
2352 | } | |
2353 | ||
2354 | $old_counters = $_SESSION["tctr_last_value"]; | |
2355 | ||
2356 | $tctrs_modified = false; | |
2357 | ||
2358 | /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count | |
2359 | FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE | |
2360 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2361 | ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND | |
2362 | post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name | |
2363 | UNION | |
2364 | select tag_name,0 as count FROM ttrss_tags | |
2365 | WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */ | |
2366 | ||
2367 | $age_qpart = getMaxAgeSubquery(); | |
2368 | ||
2369 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) | |
2370 | FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id | |
2371 | AND ref_id = id AND $age_qpart | |
2372 | AND unread = true)) AS count FROM ttrss_tags | |
2373 | WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name | |
2374 | ORDER BY count DESC LIMIT 55"); | |
2375 | ||
2376 | $tags = array(); | |
2377 | ||
2378 | while ($line = db_fetch_assoc($result)) { | |
2379 | $tags[$line["tag_name"]] += $line["count"]; | |
2380 | } | |
2381 | ||
2382 | foreach (array_keys($tags) as $tag) { | |
2383 | $unread = $tags[$tag]; | |
2384 | ||
2385 | $tag = htmlspecialchars($tag); | |
2386 | ||
2387 | if (!$smart_mode || $old_counters[$tag] != $unread) { | |
2388 | $old_counters[$tag] = $unread; | |
2389 | $tctrs_modified = true; | |
2390 | print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>"; | |
2391 | } | |
2392 | ||
2393 | } | |
2394 | ||
2395 | if ($smart_mode && $tctrs_modified) { | |
2396 | $_SESSION["tctr_last_value"] = $old_counters; | |
2397 | } | |
2398 | ||
2399 | } | |
2400 | ||
2401 | function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) { | |
2402 | ||
2403 | $age_qpart = getMaxAgeSubquery(); | |
2404 | ||
2405 | if ($smart_mode) { | |
2406 | if (!$_SESSION["lctr_last_value"]) { | |
2407 | $_SESSION["lctr_last_value"] = array(); | |
2408 | } | |
2409 | } | |
2410 | ||
2411 | $ret_arr = array(); | |
2412 | ||
2413 | $old_counters = $_SESSION["lctr_last_value"]; | |
2414 | $lctrs_modified = false; | |
2415 | ||
2416 | $count = getFeedUnread($link, -1); | |
2417 | ||
2418 | if (!$ret_mode) { | |
2419 | print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>"; | |
2420 | } else { | |
2421 | $ret_arr["-1"]["counter"] = $count; | |
2422 | $ret_arr["-1"]["description"] = __("Starred articles"); | |
2423 | } | |
2424 | ||
2425 | $count = getFeedUnread($link, -2); | |
2426 | ||
2427 | if (!$ret_mode) { | |
2428 | print "<counter type=\"label\" id=\"-2\" counter=\"$count\"/>"; | |
2429 | } else { | |
2430 | $ret_arr["-2"]["counter"] = $count; | |
2431 | $ret_arr["-2"]["description"] = __("Published articles"); | |
2432 | } | |
2433 | ||
2434 | $count = getFeedUnread($link, -3); | |
2435 | ||
2436 | if (!$ret_mode) { | |
2437 | print "<counter type=\"label\" id=\"-3\" counter=\"$count\"/>"; | |
2438 | } else { | |
2439 | $ret_arr["-3"]["counter"] = $count; | |
2440 | $ret_arr["-3"]["description"] = __("Fresh articles"); | |
2441 | } | |
2442 | ||
2443 | ||
2444 | $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM | |
2445 | ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description"); | |
2446 | ||
2447 | while ($line = db_fetch_assoc($result)) { | |
2448 | ||
2449 | $id = -$line["id"] - 11; | |
2450 | ||
2451 | $label_name = $line["description"]; | |
2452 | ||
2453 | error_reporting (0); | |
2454 | ||
2455 | $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds | |
2456 | WHERE (" . $line["sql_exp"] . ") AND unread = true AND | |
2457 | ttrss_feeds.hidden = false AND | |
2458 | $age_qpart AND | |
2459 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
2460 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2461 | ttrss_user_entries.owner_uid = ".$_SESSION["uid"]); | |
2462 | ||
2463 | $count = db_fetch_result($tmp_result, 0, "count"); | |
2464 | ||
2465 | if (!$smart_mode || $old_counters[$id] != $count) { | |
2466 | $old_counters[$id] = $count; | |
2467 | $lctrs_modified = true; | |
2468 | if (!$ret_mode) { | |
2469 | print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>"; | |
2470 | } else { | |
2471 | $ret_arr[$id]["counter"] = $count; | |
2472 | $ret_arr[$id]["description"] = $label_name; | |
2473 | } | |
2474 | } | |
2475 | ||
2476 | error_reporting (DEFAULT_ERROR_LEVEL); | |
2477 | } | |
2478 | ||
2479 | if ($smart_mode && $lctrs_modified) { | |
2480 | $_SESSION["lctr_last_value"] = $old_counters; | |
2481 | } | |
2482 | ||
2483 | return $ret_arr; | |
2484 | } | |
2485 | ||
2486 | /* function getFeedCounter($link, $id) { | |
2487 | ||
2488 | $result = db_query($link, "SELECT | |
2489 | count(id) as count,last_error | |
2490 | FROM ttrss_entries,ttrss_user_entries,ttrss_feeds | |
2491 | WHERE feed_id = '$id' AND unread = true | |
2492 | AND ttrss_user_entries.feed_id = ttrss_feeds.id | |
2493 | AND ttrss_user_entries.ref_id = ttrss_entries.id"); | |
2494 | ||
2495 | $count = db_fetch_result($result, 0, "count"); | |
2496 | $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error")); | |
2497 | ||
2498 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>"; | |
2499 | } */ | |
2500 | ||
2501 | function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS, $active_feed = false) { | |
2502 | ||
2503 | $age_qpart = getMaxAgeSubquery(); | |
2504 | ||
2505 | if ($smart_mode) { | |
2506 | if (!$_SESSION["fctr_last_value"]) { | |
2507 | $_SESSION["fctr_last_value"] = array(); | |
2508 | } | |
2509 | } | |
2510 | ||
2511 | $old_counters = $_SESSION["fctr_last_value"]; | |
2512 | ||
2513 | /* $result = db_query($link, "SELECT id,last_error,parent_feed, | |
2514 | SUBSTRING(last_updated,1,19) AS last_updated, | |
2515 | (SELECT count(id) | |
2516 | FROM ttrss_entries,ttrss_user_entries | |
2517 | WHERE feed_id = ttrss_feeds.id AND | |
2518 | ttrss_user_entries.ref_id = ttrss_entries.id | |
2519 | AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count | |
2520 | FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . " | |
2521 | AND parent_feed IS NULL"); */ | |
2522 | ||
2523 | $query = "SELECT ttrss_feeds.id, | |
2524 | ttrss_feeds.title, | |
2525 | SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated, | |
2526 | last_error, | |
2527 | COUNT(ttrss_entries.id) AS count | |
2528 | FROM ttrss_feeds | |
2529 | LEFT JOIN ttrss_user_entries ON (ttrss_user_entries.feed_id = ttrss_feeds.id | |
2530 | AND ttrss_user_entries.owner_uid = ttrss_feeds.owner_uid | |
2531 | AND ttrss_user_entries.unread = true) | |
2532 | LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2533 | $age_qpart) | |
2534 | WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]." | |
2535 | AND parent_feed IS NULL | |
2536 | GROUP BY ttrss_feeds.id, ttrss_feeds.title, ttrss_feeds.last_updated, last_error"; | |
2537 | ||
2538 | $result = db_query($link, $query); | |
2539 | $fctrs_modified = false; | |
2540 | ||
2541 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); | |
2542 | ||
2543 | while ($line = db_fetch_assoc($result)) { | |
2544 | ||
2545 | $id = $line["id"]; | |
2546 | $count = $line["count"]; | |
2547 | $last_error = htmlspecialchars($line["last_error"]); | |
2548 | ||
2549 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
2550 | $last_updated = smart_date_time(strtotime($line["last_updated"])); | |
2551 | } else { | |
2552 | $last_updated = date($short_date, strtotime($line["last_updated"])); | |
2553 | } | |
2554 | ||
2555 | $last_updated = htmlspecialchars($last_updated); | |
2556 | ||
2557 | $has_img = is_file(ICONS_DIR . "/$id.ico"); | |
2558 | ||
2559 | $tmp_result = db_query($link, | |
2560 | "SELECT ttrss_feeds.id,COUNT(unread) AS unread | |
2561 | FROM ttrss_feeds LEFT JOIN ttrss_user_entries | |
2562 | ON (ttrss_feeds.id = ttrss_user_entries.feed_id) | |
2563 | LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id) | |
2564 | WHERE parent_feed = '$id' AND $age_qpart AND unread = true GROUP BY ttrss_feeds.id"); | |
2565 | ||
2566 | if (db_num_rows($tmp_result) > 0) { | |
2567 | while ($l = db_fetch_assoc($tmp_result)) { | |
2568 | $count += $l["unread"]; | |
2569 | } | |
2570 | } | |
2571 | ||
2572 | if (!$smart_mode || $old_counters[$id] != $count) { | |
2573 | $old_counters[$id] = $count; | |
2574 | $fctrs_modified = true; | |
2575 | ||
2576 | if ($last_error) { | |
2577 | $error_part = "error=\"$last_error\""; | |
2578 | } else { | |
2579 | $error_part = ""; | |
2580 | } | |
2581 | ||
2582 | if ($has_img) { | |
2583 | $has_img_part = "hi=\"$has_img\""; | |
2584 | } else { | |
2585 | $has_img_part = ""; | |
2586 | } | |
2587 | ||
2588 | if ($active_feed && $id == $active_feed) { | |
2589 | $has_title_part = "title=\"" . htmlspecialchars($line["title"]) . "\""; | |
2590 | } else { | |
2591 | $has_title_part = ""; | |
2592 | } | |
2593 | ||
2594 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\" $has_title_part/>"; | |
2595 | } | |
2596 | } | |
2597 | ||
2598 | if ($smart_mode && $fctrs_modified) { | |
2599 | $_SESSION["fctr_last_value"] = $old_counters; | |
2600 | } | |
2601 | } | |
2602 | ||
2603 | function get_script_dt_add() { | |
2604 | if (strpos(VERSION, ".99") === false) { | |
2605 | return VERSION; | |
2606 | } else { | |
2607 | return time(); | |
2608 | } | |
2609 | } | |
2610 | ||
2611 | function get_pgsql_version($link) { | |
2612 | $result = db_query($link, "SELECT version() AS version"); | |
2613 | $version = split(" ", db_fetch_result($result, 0, "version")); | |
2614 | return $version[1]; | |
2615 | } | |
2616 | ||
2617 | function print_error_xml($code, $add_msg = "") { | |
2618 | global $ERRORS; | |
2619 | ||
2620 | $error_msg = $ERRORS[$code]; | |
2621 | ||
2622 | if ($add_msg) { | |
2623 | $error_msg = "$error_msg; $add_msg"; | |
2624 | } | |
2625 | ||
2626 | print "<rpc-reply>"; | |
2627 | print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>"; | |
2628 | print "</rpc-reply>"; | |
2629 | } | |
2630 | ||
2631 | function subscribe_to_feed($link, $feed_link, $cat_id = 0, | |
2632 | $auth_login = '', $auth_pass = '') { | |
2633 | ||
2634 | # check for feed:http://url | |
2635 | $feed_link = trim(preg_replace("/^feed:/", "", $feed_link)); | |
2636 | ||
2637 | # check for feed://URL | |
2638 | if (strpos($feed_link, "//") === 0) { | |
2639 | $feed_link = "http:$feed_link"; | |
2640 | } | |
2641 | ||
2642 | if ($feed_link == "") return; | |
2643 | ||
2644 | if ($cat_id == "0" || !$cat_id) { | |
2645 | $cat_qpart = "NULL"; | |
2646 | } else { | |
2647 | $cat_qpart = "'$cat_id'"; | |
2648 | } | |
2649 | ||
2650 | $result = db_query($link, | |
2651 | "SELECT id FROM ttrss_feeds | |
2652 | WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]); | |
2653 | ||
2654 | if (db_num_rows($result) == 0) { | |
2655 | ||
2656 | $result = db_query($link, | |
2657 | "INSERT INTO ttrss_feeds | |
2658 | (owner_uid,feed_url,title,cat_id, auth_login,auth_pass) | |
2659 | VALUES ('".$_SESSION["uid"]."', '$feed_link', | |
2660 | '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')"); | |
2661 | ||
2662 | $result = db_query($link, | |
2663 | "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link' | |
2664 | AND owner_uid = " . $_SESSION["uid"]); | |
2665 | ||
2666 | $feed_id = db_fetch_result($result, 0, "id"); | |
2667 | ||
2668 | if ($feed_id) { | |
2669 | update_rss_feed($link, $feed_link, $feed_id, true); | |
2670 | } | |
2671 | ||
2672 | return true; | |
2673 | } else { | |
2674 | return false; | |
2675 | } | |
2676 | } | |
2677 | ||
2678 | function print_feed_select($link, $id, $default_id = "", | |
2679 | $attributes = "", $include_all_feeds = true) { | |
2680 | ||
2681 | print "<select id=\"$id\" name=\"$id\" $attributes>"; | |
2682 | if ($include_all_feeds) { | |
2683 | print "<option value=\"0\">".__('All feeds')."</option>"; | |
2684 | } | |
2685 | ||
2686 | $result = db_query($link, "SELECT id,title FROM ttrss_feeds | |
2687 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
2688 | ||
2689 | if (db_num_rows($result) > 0 && $include_all_feeds) { | |
2690 | print "<option disabled>--------</option>"; | |
2691 | } | |
2692 | ||
2693 | while ($line = db_fetch_assoc($result)) { | |
2694 | if ($line["id"] == $default_id) { | |
2695 | $is_selected = "selected"; | |
2696 | } else { | |
2697 | $is_selected = ""; | |
2698 | } | |
2699 | printf("<option $is_selected value='%d'>%s</option>", | |
2700 | $line["id"], htmlspecialchars($line["title"])); | |
2701 | } | |
2702 | ||
2703 | print "</select>"; | |
2704 | } | |
2705 | ||
2706 | function print_feed_cat_select($link, $id, $default_id = "", | |
2707 | $attributes = "", $include_all_cats = true) { | |
2708 | ||
2709 | print "<select id=\"$id\" name=\"$id\" $attributes>"; | |
2710 | ||
2711 | if ($include_all_cats) { | |
2712 | print "<option value=\"0\">".__('Uncategorized')."</option>"; | |
2713 | } | |
2714 | ||
2715 | $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories | |
2716 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
2717 | ||
2718 | if (db_num_rows($result) > 0 && $include_all_cats) { | |
2719 | print "<option disabled>--------</option>"; | |
2720 | } | |
2721 | ||
2722 | while ($line = db_fetch_assoc($result)) { | |
2723 | if ($line["id"] == $default_id) { | |
2724 | $is_selected = "selected"; | |
2725 | } else { | |
2726 | $is_selected = ""; | |
2727 | } | |
2728 | printf("<option $is_selected value='%d'>%s</option>", | |
2729 | $line["id"], htmlspecialchars($line["title"])); | |
2730 | } | |
2731 | ||
2732 | print "</select>"; | |
2733 | } | |
2734 | ||
2735 | function checkbox_to_sql_bool($val) { | |
2736 | return ($val == "on") ? "true" : "false"; | |
2737 | } | |
2738 | ||
2739 | function getFeedCatTitle($link, $id) { | |
2740 | if ($id == -1) { | |
2741 | return __("Special"); | |
2742 | } else if ($id < -10) { | |
2743 | return __("Labels"); | |
2744 | } else if ($id > 0) { | |
2745 | $result = db_query($link, "SELECT ttrss_feed_categories.title | |
2746 | FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND | |
2747 | cat_id = ttrss_feed_categories.id"); | |
2748 | if (db_num_rows($result) == 1) { | |
2749 | return db_fetch_result($result, 0, "title"); | |
2750 | } else { | |
2751 | return __("Uncategorized"); | |
2752 | } | |
2753 | } else { | |
2754 | return "getFeedCatTitle($id) failed"; | |
2755 | } | |
2756 | ||
2757 | } | |
2758 | ||
2759 | function getFeedTitle($link, $id) { | |
2760 | if ($id == -1) { | |
2761 | return __("Starred articles"); | |
2762 | } else if ($id == -2) { | |
2763 | return __("Published articles"); | |
2764 | } else if ($id == -3) { | |
2765 | return __("Fresh articles"); | |
2766 | } else if ($id < -10) { | |
2767 | $label_id = -10 - $id; | |
2768 | $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'"); | |
2769 | if (db_num_rows($result) == 1) { | |
2770 | return db_fetch_result($result, 0, "description"); | |
2771 | } else { | |
2772 | return "Unknown label ($label_id)"; | |
2773 | } | |
2774 | ||
2775 | } else if ($id > 0) { | |
2776 | $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'"); | |
2777 | if (db_num_rows($result) == 1) { | |
2778 | return db_fetch_result($result, 0, "title"); | |
2779 | } else { | |
2780 | return "Unknown feed ($id)"; | |
2781 | } | |
2782 | } else { | |
2783 | return "getFeedTitle($id) failed"; | |
2784 | } | |
2785 | ||
2786 | } | |
2787 | ||
2788 | function get_session_cookie_name() { | |
2789 | return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME); | |
2790 | } | |
2791 | ||
2792 | function print_init_params($link) { | |
2793 | print "<init-params>"; | |
2794 | if ($_SESSION["stored-params"]) { | |
2795 | foreach (array_keys($_SESSION["stored-params"]) as $key) { | |
2796 | if ($key) { | |
2797 | $value = htmlspecialchars($_SESSION["stored-params"][$key]); | |
2798 | print "<param key=\"$key\" value=\"$value\"/>"; | |
2799 | } | |
2800 | } | |
2801 | } | |
2802 | ||
2803 | print "<param key=\"theme\" value=\"".$_SESSION["theme"]."\"/>"; | |
2804 | print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>"; | |
2805 | print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>"; | |
2806 | print "<param key=\"daemon_refresh_only\" value=\"true\"/>"; | |
2807 | ||
2808 | print "<param key=\"on_catchup_show_next_feed\" value=\"" . | |
2809 | get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>"; | |
2810 | ||
2811 | print "<param key=\"hide_read_feeds\" value=\"" . | |
2812 | (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>"; | |
2813 | ||
2814 | print "<param key=\"feeds_sort_by_unread\" value=\"" . | |
2815 | (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>"; | |
2816 | ||
2817 | print "<param key=\"confirm_feed_catchup\" value=\"" . | |
2818 | (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>"; | |
2819 | ||
2820 | print "<param key=\"cdm_auto_catchup\" value=\"" . | |
2821 | (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>"; | |
2822 | ||
2823 | print "<param key=\"icons_url\" value=\"" . ICONS_URL . "\"/>"; | |
2824 | ||
2825 | print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME . "\"/>"; | |
2826 | ||
2827 | print "<param key=\"default_view_mode\" value=\"" . | |
2828 | get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>"; | |
2829 | ||
2830 | print "<param key=\"default_view_limit\" value=\"" . | |
2831 | (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>"; | |
2832 | ||
2833 | print "<param key=\"prefs_active_tab\" value=\"" . | |
2834 | get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>"; | |
2835 | ||
2836 | print "<param key=\"infobox_disable_overlay\" value=\"" . | |
2837 | get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>"; | |
2838 | ||
2839 | print "<param key=\"icons_location\" value=\"" . | |
2840 | ICONS_URL . "\"/>"; | |
2841 | ||
2842 | print "</init-params>"; | |
2843 | } | |
2844 | ||
2845 | function print_runtime_info($link) { | |
2846 | print "<runtime-info>"; | |
2847 | ||
2848 | if (ENABLE_UPDATE_DAEMON) { | |
2849 | print "<param key=\"daemon_is_running\" value=\"". | |
2850 | sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>"; | |
2851 | ||
2852 | if (time() - $_SESSION["daemon_stamp_check"] > 30) { | |
2853 | ||
2854 | $stamp = (int)read_stampfile("update_daemon.stamp"); | |
2855 | ||
2856 | // print "<param key=\"daemon_stamp_delta\" value=\"$stamp_delta\"/>"; | |
2857 | ||
2858 | if ($stamp) { | |
2859 | $stamp_delta = time() - $stamp; | |
2860 | ||
2861 | if ($stamp_delta > 1800) { | |
2862 | $stamp_check = 0; | |
2863 | } else { | |
2864 | $stamp_check = 1; | |
2865 | $_SESSION["daemon_stamp_check"] = time(); | |
2866 | } | |
2867 | ||
2868 | print "<param key=\"daemon_stamp_ok\" value=\"$stamp_check\"/>"; | |
2869 | ||
2870 | $stamp_fmt = date("Y.m.d, G:i", $stamp); | |
2871 | ||
2872 | print "<param key=\"daemon_stamp\" value=\"$stamp_fmt\"/>"; | |
2873 | } | |
2874 | } | |
2875 | } | |
2876 | ||
2877 | if (CHECK_FOR_NEW_VERSION && $_SESSION["access_level"] >= 10) { | |
2878 | ||
2879 | if ($_SESSION["last_version_check"] + 7200 < time()) { | |
2880 | $new_version_details = check_for_update($link); | |
2881 | ||
2882 | print "<param key=\"new_version_available\" value=\"". | |
2883 | sprintf("%d", $new_version_details != ""). "\"/>"; | |
2884 | ||
2885 | $_SESSION["last_version_check"] = time(); | |
2886 | } | |
2887 | } | |
2888 | ||
2889 | // print "<param key=\"new_version_available\" value=\"1\"/>"; | |
2890 | ||
2891 | print "</runtime-info>"; | |
2892 | } | |
2893 | ||
2894 | function getSearchSql($search, $match_on) { | |
2895 | ||
2896 | $search_query_part = ""; | |
2897 | ||
2898 | $keywords = split(" ", $search); | |
2899 | $query_keywords = array(); | |
2900 | ||
2901 | if ($match_on == "both") { | |
2902 | ||
2903 | foreach ($keywords as $k) { | |
2904 | array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%') | |
2905 | OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))"); | |
2906 | } | |
2907 | ||
2908 | $search_query_part = implode("AND", $query_keywords) . " AND "; | |
2909 | ||
2910 | } else if ($match_on == "title") { | |
2911 | ||
2912 | foreach ($keywords as $k) { | |
2913 | array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))"); | |
2914 | } | |
2915 | ||
2916 | $search_query_part = implode("AND", $query_keywords) . " AND "; | |
2917 | ||
2918 | } else if ($match_on == "content") { | |
2919 | ||
2920 | foreach ($keywords as $k) { | |
2921 | array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))"); | |
2922 | } | |
2923 | } | |
2924 | ||
2925 | $search_query_part = implode("AND", $query_keywords); | |
2926 | ||
2927 | return $search_query_part; | |
2928 | } | |
2929 | ||
2930 | function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) { | |
2931 | ||
2932 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
2933 | ||
2934 | if ($search) { | |
2935 | ||
2936 | $search_query_part = getSearchSql($search, $match_on); | |
2937 | $search_query_part .= " AND "; | |
2938 | ||
2939 | } else { | |
2940 | $search_query_part = ""; | |
2941 | } | |
2942 | ||
2943 | $view_query_part = ""; | |
2944 | ||
2945 | if ($view_mode == "adaptive") { | |
2946 | if ($search) { | |
2947 | $view_query_part = " "; | |
2948 | } else if ($feed != -1) { | |
2949 | $unread = getFeedUnread($link, $feed, $cat_view); | |
2950 | if ($unread > 0) { | |
2951 | $view_query_part = " unread = true AND "; | |
2952 | } | |
2953 | } | |
2954 | } | |
2955 | ||
2956 | if ($view_mode == "marked") { | |
2957 | $view_query_part = " marked = true AND "; | |
2958 | } | |
2959 | ||
2960 | if ($view_mode == "unread") { | |
2961 | $view_query_part = " unread = true AND "; | |
2962 | } | |
2963 | ||
2964 | if ($limit > 0) { | |
2965 | $limit_query_part = "LIMIT " . $limit; | |
2966 | } | |
2967 | ||
2968 | $vfeed_query_part = ""; | |
2969 | ||
2970 | // override query strategy and enable feed display when searching globally | |
2971 | if ($search && $search_mode == "all_feeds") { | |
2972 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2973 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2974 | } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) { | |
2975 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2976 | $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE | |
2977 | id = feed_id) as feed_title,"; | |
2978 | } else if ($feed >= 0 && $search && $search_mode == "this_cat") { | |
2979 | ||
2980 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2981 | ||
2982 | $tmp_result = false; | |
2983 | ||
2984 | if ($cat_view) { | |
2985 | $tmp_result = db_query($link, "SELECT id | |
2986 | FROM ttrss_feeds WHERE cat_id = '$feed'"); | |
2987 | } else { | |
2988 | $tmp_result = db_query($link, "SELECT id | |
2989 | FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds | |
2990 | WHERE id = '$feed') AND id != '$feed'"); | |
2991 | } | |
2992 | ||
2993 | $cat_siblings = array(); | |
2994 | ||
2995 | if (db_num_rows($tmp_result) > 0) { | |
2996 | while ($p = db_fetch_assoc($tmp_result)) { | |
2997 | array_push($cat_siblings, "feed_id = " . $p["id"]); | |
2998 | } | |
2999 | ||
3000 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
3001 | $feed, implode(" OR ", $cat_siblings)); | |
3002 | ||
3003 | } else { | |
3004 | $query_strategy_part = "ttrss_entries.id > 0"; | |
3005 | } | |
3006 | ||
3007 | } else if ($feed >= 0) { | |
3008 | ||
3009 | if ($cat_view) { | |
3010 | ||
3011 | if ($feed > 0) { | |
3012 | $query_strategy_part = "cat_id = '$feed'"; | |
3013 | } else { | |
3014 | $query_strategy_part = "cat_id IS NULL"; | |
3015 | } | |
3016 | ||
3017 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
3018 | ||
3019 | } else { | |
3020 | $tmp_result = db_query($link, "SELECT id | |
3021 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
3022 | ORDER BY cat_id,title"); | |
3023 | ||
3024 | $parent_ids = array(); | |
3025 | ||
3026 | if (db_num_rows($tmp_result) > 0) { | |
3027 | while ($p = db_fetch_assoc($tmp_result)) { | |
3028 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
3029 | } | |
3030 | ||
3031 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
3032 | $feed, implode(" OR ", $parent_ids)); | |
3033 | ||
3034 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
3035 | } else { | |
3036 | $query_strategy_part = "feed_id = '$feed'"; | |
3037 | } | |
3038 | } | |
3039 | } else if ($feed == -1) { // starred virtual feed | |
3040 | $query_strategy_part = "marked = true"; | |
3041 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
3042 | } else if ($feed == -2) { // published virtual feed | |
3043 | $query_strategy_part = "published = true"; | |
3044 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
3045 | } else if ($feed == -3) { // fresh virtual feed | |
3046 | $query_strategy_part = "unread = true"; | |
3047 | ||
3048 | $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE"); | |
3049 | ||
3050 | if (DB_TYPE == "pgsql") { | |
3051 | $query_strategy_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' "; | |
3052 | } else { | |
3053 | $query_strategy_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) "; | |
3054 | } | |
3055 | ||
3056 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
3057 | } else if ($feed <= -10) { // labels | |
3058 | $label_id = -$feed - 11; | |
3059 | ||
3060 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
3061 | WHERE id = '$label_id'"); | |
3062 | ||
3063 | $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp"); | |
3064 | ||
3065 | if (!$query_strategy_part) { | |
3066 | return false; | |
3067 | } | |
3068 | ||
3069 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
3070 | } else { | |
3071 | $query_strategy_part = "id > 0"; // dumb | |
3072 | } | |
3073 | ||
3074 | if (get_pref($link, 'REVERSE_HEADLINES')) { | |
3075 | $order_by = "updated"; | |
3076 | } else { | |
3077 | $order_by = "updated DESC"; | |
3078 | } | |
3079 | ||
3080 | if ($override_order) { | |
3081 | $order_by = $override_order; | |
3082 | } | |
3083 | ||
3084 | $feed_title = ""; | |
3085 | ||
3086 | if ($search && $search_mode == "all_feeds") { | |
3087 | $feed_title = __("Search results")." ($search)"; | |
3088 | } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) { | |
3089 | $feed_title = __("Search results")." ($search, $feed)"; | |
3090 | } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) { | |
3091 | $feed_title = $feed; | |
3092 | } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) { | |
3093 | ||
3094 | if ($cat_view) { | |
3095 | ||
3096 | if ($feed != 0) { | |
3097 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories | |
3098 | WHERE id = '$feed' AND owner_uid = $owner_uid"); | |
3099 | $feed_title = db_fetch_result($result, 0, "title"); | |
3100 | } else { | |
3101 | $feed_title = __("Uncategorized"); | |
3102 | } | |
3103 | ||
3104 | if ($search) { | |
3105 | $feed_title = __("Searched for")." $search ($feed_title)"; | |
3106 | } | |
3107 | ||
3108 | } else { | |
3109 | ||
3110 | $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds | |
3111 | WHERE id = '$feed' AND owner_uid = $owner_uid"); | |
3112 | ||
3113 | $feed_title = db_fetch_result($result, 0, "title"); | |
3114 | $feed_site_url = db_fetch_result($result, 0, "site_url"); | |
3115 | $last_error = db_fetch_result($result, 0, "last_error"); | |
3116 | ||
3117 | if ($search) { | |
3118 | $feed_title = __("Searched for") . " $search ($feed_title)"; | |
3119 | } | |
3120 | } | |
3121 | ||
3122 | } else if ($feed == -1) { | |
3123 | $feed_title = __("Starred articles"); | |
3124 | } else if ($feed == -2) { | |
3125 | $feed_title = __("Published articles"); | |
3126 | } else if ($feed == -3) { | |
3127 | $feed_title = __("Fresh articles"); | |
3128 | } else if ($feed < -10) { | |
3129 | $label_id = -$feed - 11; | |
3130 | $result = db_query($link, "SELECT description FROM ttrss_labels | |
3131 | WHERE id = '$label_id'"); | |
3132 | $feed_title = db_fetch_result($result, 0, "description"); | |
3133 | ||
3134 | if ($search) { | |
3135 | $feed_title = __("Searched for") . " $search ($feed_title)"; | |
3136 | } | |
3137 | } else { | |
3138 | $feed_title = "?"; | |
3139 | } | |
3140 | ||
3141 | if ($feed < -10) error_reporting (0); | |
3142 | ||
3143 | $content_query_part = "content as content_preview,"; | |
3144 | ||
3145 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
3146 | ||
3147 | if ($feed >= 0) { | |
3148 | $feed_kind = "Feeds"; | |
3149 | } else { | |
3150 | $feed_kind = "Labels"; | |
3151 | } | |
3152 | ||
3153 | if ($limit_query_part) { | |
3154 | $offset_query_part = "OFFSET $offset"; | |
3155 | } | |
3156 | ||
3157 | $query = "SELECT | |
3158 | guid, | |
3159 | ttrss_entries.id,ttrss_entries.title, | |
3160 | updated, | |
3161 | unread,feed_id,marked,published,link,last_read, | |
3162 | SUBSTRING(last_read,1,19) as last_read_noms, | |
3163 | $vfeed_query_part | |
3164 | $content_query_part | |
3165 | SUBSTRING(updated,1,19) as updated_noms, | |
3166 | author | |
3167 | FROM | |
3168 | ttrss_entries,ttrss_user_entries,ttrss_feeds | |
3169 | WHERE | |
3170 | ttrss_feeds.hidden = false AND | |
3171 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
3172 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
3173 | ttrss_user_entries.owner_uid = '$owner_uid' AND | |
3174 | $search_query_part | |
3175 | $view_query_part | |
3176 | $query_strategy_part ORDER BY $order_by | |
3177 | $limit_query_part $offset_query_part"; | |
3178 | ||
3179 | $result = db_query($link, $query); | |
3180 | ||
3181 | if ($_GET["debug"]) print $query; | |
3182 | ||
3183 | } else { | |
3184 | // browsing by tag | |
3185 | ||
3186 | $feed_kind = "Tags"; | |
3187 | ||
3188 | $result = db_query($link, "SELECT | |
3189 | guid, | |
3190 | ttrss_entries.id as id,title, | |
3191 | updated, | |
3192 | unread,feed_id, | |
3193 | marked,link,last_read, | |
3194 | SUBSTRING(last_read,1,19) as last_read_noms, | |
3195 | $vfeed_query_part | |
3196 | $content_query_part | |
3197 | SUBSTRING(updated,1,19) as updated_noms | |
3198 | FROM | |
3199 | ttrss_entries,ttrss_user_entries,ttrss_tags | |
3200 | WHERE | |
3201 | ref_id = ttrss_entries.id AND | |
3202 | ttrss_user_entries.owner_uid = '$owner_uid' AND | |
3203 | post_int_id = int_id AND tag_name = '$feed' AND | |
3204 | $view_query_part | |
3205 | $search_query_part | |
3206 | $query_strategy_part ORDER BY $order_by | |
3207 | $limit_query_part"); | |
3208 | } | |
3209 | ||
3210 | return array($result, $feed_title, $feed_site_url, $last_error); | |
3211 | ||
3212 | } | |
3213 | ||
3214 | function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat, | |
3215 | $search, $search_mode, $match_on) { | |
3216 | ||
3217 | $qfh_ret = queryFeedHeadlines($link, $feed, | |
3218 | 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC", 0, | |
3219 | $owner_uid); | |
3220 | ||
3221 | $result = $qfh_ret[0]; | |
3222 | $feed_title = htmlspecialchars($qfh_ret[1]); | |
3223 | $feed_site_url = $qfh_ret[2]; | |
3224 | $last_error = $qfh_ret[3]; | |
3225 | ||
3226 | // if (!$feed_site_url) $feed_site_url = "http://localhost/"; | |
3227 | ||
3228 | print "<?xml version=\"1.0\" encoding=\"utf-8\"?> | |
3229 | <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?> | |
3230 | <rss version=\"2.0\"> | |
3231 | <channel> | |
3232 | <title>$feed_title</title> | |
3233 | <link>$feed_site_url</link> | |
3234 | <description>Feed generated by Tiny Tiny RSS</description>"; | |
3235 | ||
3236 | while ($line = db_fetch_assoc($result)) { | |
3237 | print "<item>"; | |
3238 | print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>"; | |
3239 | print "<link>" . htmlspecialchars($line["link"]) . "</link>"; | |
3240 | ||
3241 | $tags = get_article_tags($link, $line["id"], $owner_uid); | |
3242 | ||
3243 | foreach ($tags as $tag) { | |
3244 | print "<category>" . htmlspecialchars($tag) . "</category>"; | |
3245 | } | |
3246 | ||
3247 | $rfc822_date = date('r', strtotime($line["updated"])); | |
3248 | ||
3249 | print "<pubDate>$rfc822_date</pubDate>"; | |
3250 | ||
3251 | print "<title>" . | |
3252 | htmlspecialchars($line["title"]) . "</title>"; | |
3253 | ||
3254 | print "<description><![CDATA[" . | |
3255 | $line["content_preview"] . "]]></description>"; | |
3256 | ||
3257 | print "</item>"; | |
3258 | } | |
3259 | ||
3260 | print "</channel></rss>"; | |
3261 | ||
3262 | } | |
3263 | ||
3264 | function getCategoryTitle($link, $cat_id) { | |
3265 | ||
3266 | if ($cat_id == -1) { | |
3267 | return __("Special"); | |
3268 | } else if ($cat_id == -2) { | |
3269 | return __("Labels"); | |
3270 | } else { | |
3271 | ||
3272 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE | |
3273 | id = '$cat_id'"); | |
3274 | ||
3275 | if (db_num_rows($result) == 1) { | |
3276 | return db_fetch_result($result, 0, "title"); | |
3277 | } else { | |
3278 | return "Uncategorized"; | |
3279 | } | |
3280 | } | |
3281 | } | |
3282 | ||
3283 | // http://ru2.php.net/strip-tags | |
3284 | ||
3285 | function strip_tags_long($textstring, $allowed){ | |
3286 | while($textstring != strip_tags($textstring, $allowed)) | |
3287 | { | |
3288 | while (strlen($textstring) != 0) | |
3289 | { | |
3290 | if (strlen($textstring) > 1024) { | |
3291 | $otherlen = 1024; | |
3292 | } else { | |
3293 | $otherlen = strlen($textstring); | |
3294 | } | |
3295 | $temptext = strip_tags(substr($textstring,0,$otherlen), $allowed); | |
3296 | $safetext .= $temptext; | |
3297 | $textstring = substr_replace($textstring,'',0,$otherlen); | |
3298 | } | |
3299 | $textstring = $safetext; | |
3300 | } | |
3301 | return $textstring; | |
3302 | } | |
3303 | ||
3304 | ||
3305 | function sanitize_rss($link, $str, $force_strip_tags = false) { | |
3306 | $res = $str; | |
3307 | ||
3308 | if (get_pref($link, "STRIP_UNSAFE_TAGS") || $force_strip_tags) { | |
3309 | ||
3310 | $res = strip_tags_long($res, | |
3311 | "<p><a><i><em><b><strong><blockquote><br><img><div><span><ul><ol><li>"); | |
3312 | ||
3313 | // $res = preg_replace("/\r\n|\n|\r/", "", $res); | |
3314 | // $res = strip_tags_long($res, "<p><a><i><em><b><strong><blockquote><br><img><div><span>"); | |
3315 | } | |
3316 | ||
3317 | return $res; | |
3318 | } | |
3319 | ||
3320 | function send_headlines_digests($link, $limit = 100) { | |
3321 | ||
3322 | if (!DIGEST_ENABLE) return false; | |
3323 | ||
3324 | $user_limit = DIGEST_EMAIL_LIMIT; | |
3325 | $days = 1; | |
3326 | ||
3327 | print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n"; | |
3328 | ||
3329 | if (DB_TYPE == "pgsql") { | |
3330 | $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'"; | |
3331 | } else if (DB_TYPE == "mysql") { | |
3332 | $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
3333 | } | |
3334 | ||
3335 | $result = db_query($link, "SELECT id,email FROM ttrss_users | |
3336 | WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)"); | |
3337 | ||
3338 | while ($line = db_fetch_assoc($result)) { | |
3339 | ||
3340 | if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) { | |
3341 | print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... "; | |
3342 | ||
3343 | $do_catchup = get_pref($link, 'DIGEST_CATCHUP', $line['id'], false); | |
3344 | ||
3345 | $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit); | |
3346 | $digest = $tuple[0]; | |
3347 | $headlines_count = $tuple[1]; | |
3348 | $affected_ids = $tuple[2]; | |
3349 | $digest_text = $tuple[3]; | |
3350 | ||
3351 | if ($headlines_count > 0) { | |
3352 | ||
3353 | $mail = new PHPMailer(); | |
3354 | ||
3355 | $mail->PluginDir = "phpmailer/"; | |
3356 | $mail->SetLanguage("en", "phpmailer/language/"); | |
3357 | ||
3358 | $mail->CharSet = "UTF-8"; | |
3359 | ||
3360 | $mail->From = DIGEST_FROM_ADDRESS; | |
3361 | $mail->FromName = DIGEST_FROM_NAME; | |
3362 | $mail->AddAddress($line["email"], $line["login"]); | |
3363 | ||
3364 | if (DIGEST_SMTP_HOST) { | |
3365 | $mail->Host = DIGEST_SMTP_HOST; | |
3366 | $mail->Mailer = "smtp"; | |
3367 | $mail->Username = DIGEST_SMTP_LOGIN; | |
3368 | $mail->Password = DIGEST_SMTP_PASSWORD; | |
3369 | } | |
3370 | ||
3371 | $mail->IsHTML(true); | |
3372 | $mail->Subject = DIGEST_SUBJECT; | |
3373 | $mail->Body = $digest; | |
3374 | $mail->AltBody = $digest_text; | |
3375 | ||
3376 | $rc = $mail->Send(); | |
3377 | ||
3378 | if (!$rc) print "ERROR: " . $mail->ErrorInfo; | |
3379 | ||
3380 | print "RC=$rc\n"; | |
3381 | ||
3382 | if ($rc && $do_catchup) { | |
3383 | print "Marking affected articles as read...\n"; | |
3384 | catchupArticlesById($link, $affected_ids, 0, $line["id"]); | |
3385 | } | |
3386 | ||
3387 | db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW() | |
3388 | WHERE id = " . $line["id"]); | |
3389 | } else { | |
3390 | print "No headlines\n"; | |
3391 | } | |
3392 | } | |
3393 | } | |
3394 | ||
3395 | print "All done.\n"; | |
3396 | ||
3397 | } | |
3398 | ||
3399 | function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) { | |
3400 | ||
3401 | require_once "MiniTemplator.class.php"; | |
3402 | ||
3403 | $tpl = new MiniTemplator; | |
3404 | $tpl_t = new MiniTemplator; | |
3405 | ||
3406 | $tpl->readTemplateFromFile("templates/digest_template_html.txt"); | |
3407 | $tpl_t->readTemplateFromFile("templates/digest_template.txt"); | |
3408 | ||
3409 | $tpl->setVariable('CUR_DATE', date('Y/m/d')); | |
3410 | $tpl->setVariable('CUR_TIME', date('G:i')); | |
3411 | ||
3412 | $tpl_t->setVariable('CUR_DATE', date('Y/m/d')); | |
3413 | $tpl_t->setVariable('CUR_TIME', date('G:i')); | |
3414 | ||
3415 | $affected_ids = array(); | |
3416 | ||
3417 | if (DB_TYPE == "pgsql") { | |
3418 | $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'"; | |
3419 | } else if (DB_TYPE == "mysql") { | |
3420 | $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
3421 | } | |
3422 | ||
3423 | $result = db_query($link, "SELECT ttrss_entries.title, | |
3424 | ttrss_feeds.title AS feed_title, | |
3425 | date_entered, | |
3426 | ttrss_user_entries.ref_id, | |
3427 | link, | |
3428 | SUBSTRING(content, 1, 120) AS excerpt, | |
3429 | SUBSTRING(last_updated,1,19) AS last_updated | |
3430 | FROM | |
3431 | ttrss_user_entries,ttrss_entries,ttrss_feeds | |
3432 | WHERE | |
3433 | ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id | |
3434 | AND include_in_digest = true | |
3435 | AND $interval_query | |
3436 | AND hidden = false | |
3437 | AND ttrss_user_entries.owner_uid = $user_id | |
3438 | AND unread = true | |
3439 | ORDER BY ttrss_feeds.title, date_entered DESC | |
3440 | LIMIT $limit"); | |
3441 | ||
3442 | $cur_feed_title = ""; | |
3443 | ||
3444 | $headlines_count = db_num_rows($result); | |
3445 | ||
3446 | $headlines = array(); | |
3447 | ||
3448 | while ($line = db_fetch_assoc($result)) { | |
3449 | array_push($headlines, $line); | |
3450 | } | |
3451 | ||
3452 | for ($i = 0; $i < sizeof($headlines); $i++) { | |
3453 | ||
3454 | $line = $headlines[$i]; | |
3455 | ||
3456 | array_push($affected_ids, $line["ref_id"]); | |
3457 | ||
3458 | $updated = smart_date_time(strtotime($line["last_updated"])); | |
3459 | ||
3460 | $tpl->setVariable('FEED_TITLE', $line["feed_title"]); | |
3461 | $tpl->setVariable('ARTICLE_TITLE', $line["title"]); | |
3462 | $tpl->setVariable('ARTICLE_LINK', $line["link"]); | |
3463 | $tpl->setVariable('ARTICLE_UPDATED', $updated); | |
3464 | $tpl->setVariable('ARTICLE_EXCERPT', | |
3465 | truncate_string(strip_tags($line["excerpt"]), 100)); | |
3466 | ||
3467 | $tpl->addBlock('article'); | |
3468 | ||
3469 | $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]); | |
3470 | $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]); | |
3471 | $tpl_t->setVariable('ARTICLE_LINK', $line["link"]); | |
3472 | $tpl_t->setVariable('ARTICLE_UPDATED', $updated); | |
3473 | // $tpl_t->setVariable('ARTICLE_EXCERPT', | |
3474 | // truncate_string(strip_tags($line["excerpt"]), 100)); | |
3475 | ||
3476 | $tpl_t->addBlock('article'); | |
3477 | ||
3478 | if ($headlines[$i]['feed_title'] != $headlines[$i+1]['feed_title']) { | |
3479 | $tpl->addBlock('feed'); | |
3480 | $tpl_t->addBlock('feed'); | |
3481 | } | |
3482 | ||
3483 | } | |
3484 | ||
3485 | $tpl->addBlock('digest'); | |
3486 | $tpl->generateOutputToString($tmp); | |
3487 | ||
3488 | $tpl_t->addBlock('digest'); | |
3489 | $tpl_t->generateOutputToString($tmp_t); | |
3490 | ||
3491 | return array($tmp, $headlines_count, $affected_ids, $tmp_t); | |
3492 | } | |
3493 | ||
3494 | function check_for_update($link, $brief_fmt = true) { | |
3495 | $releases_feed = "http://tt-rss.spb.ru/releases.rss"; | |
3496 | ||
3497 | if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) { | |
3498 | return; | |
3499 | } | |
3500 | ||
3501 | error_reporting(0); | |
3502 | if (ENABLE_SIMPLEPIE) { | |
3503 | $rss = new SimplePie(); | |
3504 | $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT); | |
3505 | // $rss->set_timeout(MAGPIE_FETCH_TIME_OUT); | |
3506 | $rss->set_feed_url($fetch_url); | |
3507 | $rss->set_output_encoding('UTF-8'); | |
3508 | $rss->init(); | |
3509 | } else { | |
3510 | $rss = fetch_rss($releases_feed); | |
3511 | } | |
3512 | error_reporting (DEFAULT_ERROR_LEVEL); | |
3513 | ||
3514 | if ($rss) { | |
3515 | ||
3516 | if (ENABLE_SIMPLEPIE) { | |
3517 | $items = $rss->get_items(); | |
3518 | } else { | |
3519 | $items = $rss->items; | |
3520 | ||
3521 | if (!$items || !is_array($items)) $items = $rss->entries; | |
3522 | if (!$items || !is_array($items)) $items = $rss; | |
3523 | } | |
3524 | ||
3525 | if (!is_array($items) || count($items) == 0) { | |
3526 | return; | |
3527 | } | |
3528 | ||
3529 | $latest_item = $items[0]; | |
3530 | ||
3531 | if (ENABLE_SIMPLEPIE) { | |
3532 | $last_title = $latest_item->get_title(); | |
3533 | } else { | |
3534 | $last_title = $latest_item["title"]; | |
3535 | } | |
3536 | ||
3537 | $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $last_title)); | |
3538 | ||
3539 | if (ENABLE_SIMPLEPIE) { | |
3540 | $release_url = sanitize_rss($link, $latest_item->get_link()); | |
3541 | $content = sanitize_rss($link, $latest_item->get_description()); | |
3542 | } else { | |
3543 | $release_url = sanitize_rss($link, $latest_item["link"]); | |
3544 | $content = sanitize_rss($link, $latest_item["description"]); | |
3545 | } | |
3546 | ||
3547 | if (version_compare(VERSION, $latest_version) == -1) { | |
3548 | if ($brief_fmt) { | |
3549 | return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\"> | |
3550 | New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a> | |
3551 | <div id=\"milestoneDetails\">$content</div>"); | |
3552 | } else { | |
3553 | return "New version of Tiny-Tiny RSS ($latest_version) is available: | |
3554 | <div class='milestoneDetails'>$content</div> | |
3555 | Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for | |
3556 | download and update information."; | |
3557 | } | |
3558 | ||
3559 | } | |
3560 | } | |
3561 | } | |
3562 | ||
3563 | function markArticlesById($link, $ids, $cmode) { | |
3564 | ||
3565 | $tmp_ids = array(); | |
3566 | ||
3567 | foreach ($ids as $id) { | |
3568 | array_push($tmp_ids, "ref_id = '$id'"); | |
3569 | } | |
3570 | ||
3571 | $ids_qpart = join(" OR ", $tmp_ids); | |
3572 | ||
3573 | if ($cmode == 0) { | |
3574 | db_query($link, "UPDATE ttrss_user_entries SET | |
3575 | marked = false,last_read = NOW() | |
3576 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3577 | } else if ($cmode == 1) { | |
3578 | db_query($link, "UPDATE ttrss_user_entries SET | |
3579 | marked = true | |
3580 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3581 | } else { | |
3582 | db_query($link, "UPDATE ttrss_user_entries SET | |
3583 | marked = NOT marked,last_read = NOW() | |
3584 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3585 | } | |
3586 | } | |
3587 | ||
3588 | function publishArticlesById($link, $ids, $cmode) { | |
3589 | ||
3590 | $tmp_ids = array(); | |
3591 | ||
3592 | foreach ($ids as $id) { | |
3593 | array_push($tmp_ids, "ref_id = '$id'"); | |
3594 | } | |
3595 | ||
3596 | $ids_qpart = join(" OR ", $tmp_ids); | |
3597 | ||
3598 | if ($cmode == 0) { | |
3599 | db_query($link, "UPDATE ttrss_user_entries SET | |
3600 | published = false,last_read = NOW() | |
3601 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3602 | } else if ($cmode == 1) { | |
3603 | db_query($link, "UPDATE ttrss_user_entries SET | |
3604 | published = true | |
3605 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3606 | } else { | |
3607 | db_query($link, "UPDATE ttrss_user_entries SET | |
3608 | published = NOT published,last_read = NOW() | |
3609 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3610 | } | |
3611 | } | |
3612 | ||
3613 | function catchupArticlesById($link, $ids, $cmode, $owner_uid = false) { | |
3614 | ||
3615 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
3616 | ||
3617 | $tmp_ids = array(); | |
3618 | ||
3619 | foreach ($ids as $id) { | |
3620 | array_push($tmp_ids, "ref_id = '$id'"); | |
3621 | } | |
3622 | ||
3623 | $ids_qpart = join(" OR ", $tmp_ids); | |
3624 | ||
3625 | if ($cmode == 0) { | |
3626 | db_query($link, "UPDATE ttrss_user_entries SET | |
3627 | unread = false,last_read = NOW() | |
3628 | WHERE ($ids_qpart) AND owner_uid = $owner_uid"); | |
3629 | } else if ($cmode == 1) { | |
3630 | db_query($link, "UPDATE ttrss_user_entries SET | |
3631 | unread = true | |
3632 | WHERE ($ids_qpart) AND owner_uid = $owner_uid"); | |
3633 | } else { | |
3634 | db_query($link, "UPDATE ttrss_user_entries SET | |
3635 | unread = NOT unread,last_read = NOW() | |
3636 | WHERE ($ids_qpart) AND owner_uid = $owner_uid"); | |
3637 | } | |
3638 | } | |
3639 | ||
3640 | function catchupArticleById($link, $id, $cmode) { | |
3641 | ||
3642 | if ($cmode == 0) { | |
3643 | db_query($link, "UPDATE ttrss_user_entries SET | |
3644 | unread = false,last_read = NOW() | |
3645 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
3646 | } else if ($cmode == 1) { | |
3647 | db_query($link, "UPDATE ttrss_user_entries SET | |
3648 | unread = true | |
3649 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
3650 | } else { | |
3651 | db_query($link, "UPDATE ttrss_user_entries SET | |
3652 | unread = NOT unread,last_read = NOW() | |
3653 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
3654 | } | |
3655 | } | |
3656 | ||
3657 | function make_guid_from_title($title) { | |
3658 | return preg_replace("/[ \"\',.:;]/", "-", | |
3659 | mb_strtolower(strip_tags($title), 'utf-8')); | |
3660 | } | |
3661 | ||
3662 | function print_headline_subtoolbar($link, $feed_site_url, $feed_title, | |
3663 | $bottom = false, $rtl_content = false, $feed_id = 0, | |
3664 | $is_cat = false, $search = false, $match_on = false, | |
3665 | $search_mode = false, $offset = 0, $limit = 0) { | |
3666 | ||
3667 | $user_page_offset = $offset + 1; | |
3668 | ||
3669 | if (!$bottom) { | |
3670 | $class = "headlinesSubToolbar"; | |
3671 | $tid = "headlineActionsTop"; | |
3672 | } else { | |
3673 | $class = "headlinesSubToolbar"; | |
3674 | $tid = "headlineActionsBottom"; | |
3675 | } | |
3676 | ||
3677 | print "<table class=\"$class\" id=\"$tid\" | |
3678 | width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>"; | |
3679 | ||
3680 | if ($rtl_content) { | |
3681 | $rtl_cpart = "RTL"; | |
3682 | } else { | |
3683 | $rtl_cpart = ""; | |
3684 | } | |
3685 | ||
3686 | $page_prev_link = "javascript:viewFeedGoPage(-1)"; | |
3687 | $page_next_link = "javascript:viewFeedGoPage(1)"; | |
3688 | $page_first_link = "javascript:viewFeedGoPage(0)"; | |
3689 | ||
3690 | $catchup_page_link = "javascript:catchupPage()"; | |
3691 | $catchup_feed_link = "javascript:catchupCurrentFeed()"; | |
3692 | $catchup_sel_link = "javascript:catchupSelection()"; | |
3693 | ||
3694 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) { | |
3695 | ||
3696 | $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)"; | |
3697 | $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)"; | |
3698 | $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)"; | |
3699 | ||
3700 | $tog_unread_link = "javascript:selectionToggleUnread()"; | |
3701 | $tog_marked_link = "javascript:selectionToggleMarked()"; | |
3702 | $tog_published_link = "javascript:selectionTogglePublished()"; | |
3703 | ||
3704 | } else { | |
3705 | ||
3706 | $sel_all_link = "javascript:cdmSelectArticles('all')"; | |
3707 | $sel_unread_link = "javascript:cdmSelectArticles('unread')"; | |
3708 | $sel_none_link = "javascript:cdmSelectArticles('none')"; | |
3709 | ||
3710 | $tog_unread_link = "javascript:selectionToggleUnread(true)"; | |
3711 | $tog_marked_link = "javascript:selectionToggleMarked(true)"; | |
3712 | $tog_published_link = "javascript:selectionTogglePublished(true)"; | |
3713 | ||
3714 | } | |
3715 | ||
3716 | if (strpos($_SESSION["client.userAgent"], "MSIE") === false) { | |
3717 | ||
3718 | print "<td class=\"headlineActions$rtl_cpart\"> | |
3719 | <ul class=\"headlineDropdownMenu\"> | |
3720 | <li class=\"top2\"> | |
3721 | ".__('Select:')." | |
3722 | <a href=\"$sel_all_link\">".__('All')."</a>, | |
3723 | <a href=\"$sel_unread_link\">".__('Unread')."</a>, | |
3724 | <a href=\"$sel_none_link\">".__('None')."</a></li> | |
3725 | <li class=\"vsep\"> </li> | |
3726 | <li class=\"top\">".__('Toggle')."<ul> | |
3727 | <li onclick=\"$tog_unread_link\">".__('Unread')."</li> | |
3728 | <li onclick=\"$tog_marked_link\">".__('Starred')."</li> | |
3729 | <li onclick=\"$tog_published_link\">".__('Published')."</li> | |
3730 | </ul></li> | |
3731 | <li class=\"vsep\"> </li> | |
3732 | <li class=\"top\">".__('Mark as read')."<ul> | |
3733 | <li onclick=\"$catchup_sel_link\">".__('Selection')."</li> | |
3734 | <!-- <li onclick=\"$catchup_page_link\">".__('This page')."</li> --> | |
3735 | <li><span class=\"insensitive\">--------</span></li> | |
3736 | <li onclick=\"catchupRelativeToArticle(0)\">".__("Above active article")."</li> | |
3737 | <li onclick=\"catchupRelativeToArticle(1)\">".__("Below active article")."</li> | |
3738 | <li><span class=\"insensitive\">--------</span></li> | |
3739 | <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li> | |
3740 | "; | |
3741 | ||
3742 | $enable_pagination = get_pref($link, "_PREFS_ENABLE_PAGINATION"); | |
3743 | ||
3744 | if ($limit != 0 && !$search && $enable_pagination) { | |
3745 | print " | |
3746 | <li class=\"vsep\"> </li> | |
3747 | <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul> | |
3748 | <li onclick=\"$page_prev_link\">".__('Previous page')."</li> | |
3749 | <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li> | |
3750 | </ul>"; | |
3751 | } | |
3752 | ||
3753 | if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) { | |
3754 | print " | |
3755 | <li class=\"vsep\"> </li> | |
3756 | <li class=\"top3\"> | |
3757 | <a href=\"javascript:labelFromSearch('$search', '$search_mode', | |
3758 | '$match_on', '$feed_id', '$is_cat');\"> | |
3759 | ".__('Convert to label')."</a></td>"; | |
3760 | } | |
3761 | print " | |
3762 | </td>"; | |
3763 | ||
3764 | } else { | |
3765 | // old style subtoolbar: | |
3766 | ||
3767 | print "<td class=\"headlineActions$rtl_cpart\">". | |
3768 | __('Select:')." | |
3769 | <a href=\"$sel_all_link\">".__('All')."</a>, | |
3770 | <a href=\"$sel_unread_link\">".__('Unread')."</a>, | |
3771 | <a href=\"$sel_none_link\">".__('None')."</a> | |
3772 | ". | |
3773 | __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>, | |
3774 | <a href=\"$tog_marked_link\">".__('Starred')."</a> | |
3775 | ". | |
3776 | __('Mark as read:')." | |
3777 | <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>, | |
3778 | <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>"; | |
3779 | ||
3780 | if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) { | |
3781 | ||
3782 | print " | |
3783 | <a href=\"javascript:labelFromSearch('$search', '$search_mode', | |
3784 | '$match_on', '$feed_id', '$is_cat');\"> | |
3785 | ".__('Convert to label')."</a>"; | |
3786 | } | |
3787 | ||
3788 | print "</td>"; | |
3789 | ||
3790 | } | |
3791 | ||
3792 | /* if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) { | |
3793 | print "<td class=\"headlineActions$rtl_cpart\"> | |
3794 | <a href=\"javascript:labelFromSearch('$search', '$search_mode', | |
3795 | '$match_on', '$feed_id', '$is_cat');\"> | |
3796 | ".__('Convert to Label')."</a></td>"; | |
3797 | } */ | |
3798 | ||
3799 | print "<td class=\"headlineTitle$rtl_cpart\">"; | |
3800 | ||
3801 | print "<span class=\"headlineInnerTitle\">"; | |
3802 | ||
3803 | if ($feed_site_url) { | |
3804 | if (!$bottom) { | |
3805 | $target = "target=\"_new\""; | |
3806 | } | |
3807 | print "<a $target href=\"$feed_site_url\">". | |
3808 | truncate_string($feed_title,30)."</a>"; | |
3809 | } else { | |
3810 | print $feed_title; | |
3811 | } | |
3812 | ||
3813 | if ($search) { | |
3814 | $search_q = "&q=$search&m=$match_on&smode=$search_mode"; | |
3815 | } | |
3816 | ||
3817 | if ($user_page_offset > 1) { | |
3818 | print " [$user_page_offset] "; | |
3819 | } | |
3820 | ||
3821 | print "</span>"; | |
3822 | ||
3823 | if (!$bottom) { | |
3824 | print " | |
3825 | <a target=\"_new\" | |
3826 | href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\"> | |
3827 | <img class=\"noborder\" | |
3828 | alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\"> | |
3829 | </a>"; | |
3830 | } | |
3831 | ||
3832 | print "</td>"; | |
3833 | print "</tr></table>"; | |
3834 | ||
3835 | } | |
3836 | ||
3837 | function printCategoryHeader($link, $cat_id, $hidden = false, $can_browse = true) { | |
3838 | ||
3839 | $tmp_category = getCategoryTitle($link, $cat_id); | |
3840 | $cat_unread = getCategoryUnread($link, $cat_id); | |
3841 | ||
3842 | if ($hidden) { | |
3843 | $holder_style = "display:none;"; | |
3844 | $ellipsis = "…"; | |
3845 | } else { | |
3846 | $holder_style = ""; | |
3847 | $ellipsis = ""; | |
3848 | } | |
3849 | ||
3850 | $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread"; | |
3851 | ||
3852 | print "<li class=\"feedCat\" id=\"FCAT-$cat_id\"> | |
3853 | <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>"; | |
3854 | ||
3855 | if ($can_browse) { | |
3856 | print "<a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">"; | |
3857 | } else { | |
3858 | print "<span id=\"FCAP-$cat_id\">"; | |
3859 | } | |
3860 | ||
3861 | print " <span id=\"FCATCTR-$cat_id\" | |
3862 | class=\"$catctr_class\">($cat_unread)</span> $ellipsis"; | |
3863 | ||
3864 | if ($can_browse) { | |
3865 | print "</a>"; | |
3866 | } else { | |
3867 | print "</span>"; | |
3868 | } | |
3869 | ||
3870 | print "</li>"; | |
3871 | ||
3872 | print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>"; | |
3873 | } | |
3874 | ||
3875 | function outputFeedList($link, $tags = false) { | |
3876 | ||
3877 | print "<ul class=\"feedList\" id=\"feedList\">"; | |
3878 | ||
3879 | $owner_uid = $_SESSION["uid"]; | |
3880 | ||
3881 | /* virtual feeds */ | |
3882 | ||
3883 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3884 | ||
3885 | if ($_COOKIE["ttrss_vf_vclps"] == 1) { | |
3886 | $cat_hidden = true; | |
3887 | } else { | |
3888 | $cat_hidden = false; | |
3889 | } | |
3890 | ||
3891 | # print "<li class=\"feedCat\">".__('Special')."</li>"; | |
3892 | # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">"; | |
3893 | # print "<li class=\"feedCat\">". | |
3894 | # "<a id=\"FCATN--1\" href=\"javascript:toggleCollapseCat(-1)\">". | |
3895 | # __('Special')."</a> <span id='FCAP--1'>$ellipsis</span></li>"; | |
3896 | # | |
3897 | # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"> | |
3898 | # <ul class=\"feedCatList\" id='FCATLIST--1' style='$holder_style'>"; | |
3899 | ||
3900 | # $cat_unread = getCategoryUnread($link, -1); | |
3901 | # $tmp_category = __("Special"); | |
3902 | # $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread"; | |
3903 | ||
3904 | printCategoryHeader($link, -1, $cat_hidden, false); | |
3905 | } | |
3906 | ||
3907 | $num_starred = getFeedUnread($link, -1); | |
3908 | $num_published = getFeedUnread($link, -2); | |
3909 | $num_fresh = getFeedUnread($link, -3); | |
3910 | ||
3911 | $class = "virt"; | |
3912 | ||
3913 | if ($num_fresh > 0) $class .= "Unread"; | |
3914 | ||
3915 | printFeedEntry(-3, $class, __("Fresh articles"), $num_fresh, | |
3916 | "images/fresh.png", $link); | |
3917 | ||
3918 | $class = "virt"; | |
3919 | ||
3920 | if ($num_starred > 0) $class .= "Unread"; | |
3921 | ||
3922 | $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false); | |
3923 | ||
3924 | if ($is_ie) { | |
3925 | $mark_img_ext = "gif"; | |
3926 | } else { | |
3927 | $mark_img_ext = "png"; | |
3928 | } | |
3929 | ||
3930 | printFeedEntry(-1, $class, __("Starred articles"), $num_starred, | |
3931 | "images/mark_set.$mark_img_ext", $link); | |
3932 | ||
3933 | $class = "virt"; | |
3934 | ||
3935 | if ($num_published > 0) $class .= "Unread"; | |
3936 | ||
3937 | printFeedEntry(-2, $class, __("Published articles"), $num_published, | |
3938 | "images/pub_set.gif", $link); | |
3939 | ||
3940 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3941 | print "</ul>"; | |
3942 | } | |
3943 | ||
3944 | if (!$tags) { | |
3945 | ||
3946 | if (GLOBAL_ENABLE_LABELS && get_pref($link, 'ENABLE_LABELS')) { | |
3947 | ||
3948 | $result = db_query($link, "SELECT id,sql_exp,description FROM | |
3949 | ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description"); | |
3950 | ||
3951 | if (db_num_rows($result) > 0) { | |
3952 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3953 | ||
3954 | if ($_COOKIE["ttrss_vf_lclps"] == 1) { | |
3955 | $cat_hidden = true; | |
3956 | } else { | |
3957 | $cat_hidden = false; | |
3958 | } | |
3959 | ||
3960 | printCategoryHeader($link, -2, $cat_hidden, false); | |
3961 | ||
3962 | # print "<li class=\"feedCat\">". | |
3963 | # "<a id=\"FCATN--2\" href=\"javascript:toggleCollapseCat(-2)\">". | |
3964 | # __('Labels')."</a> <span id='FCAP--2'>$ellipsis</span></li>"; | |
3965 | # | |
3966 | # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\" id='FCATLIST--2' style='$holder_style'>"; | |
3967 | } else { | |
3968 | print "<li><hr></li>"; | |
3969 | } | |
3970 | } | |
3971 | ||
3972 | while ($line = db_fetch_assoc($result)) { | |
3973 | ||
3974 | error_reporting (0); | |
3975 | ||
3976 | $label_id = -$line['id'] - 11; | |
3977 | $count = getFeedUnread($link, $label_id); | |
3978 | ||
3979 | $class = "label"; | |
3980 | ||
3981 | if ($count > 0) { | |
3982 | $class .= "Unread"; | |
3983 | } | |
3984 | ||
3985 | error_reporting (DEFAULT_ERROR_LEVEL); | |
3986 | ||
3987 | printFeedEntry($label_id, | |
3988 | $class, $line["description"], | |
3989 | $count, "images/label.png", $link); | |
3990 | ||
3991 | } | |
3992 | ||
3993 | if (db_num_rows($result) > 0) { | |
3994 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3995 | print "</ul>"; | |
3996 | } | |
3997 | } | |
3998 | ||
3999 | } | |
4000 | ||
4001 | if (!get_pref($link, 'ENABLE_FEED_CATS')) { | |
4002 | print "<li><hr></li>"; | |
4003 | } | |
4004 | ||
4005 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
4006 | if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) { | |
4007 | $order_by_qpart = "category,unread DESC,title"; | |
4008 | } else { | |
4009 | $order_by_qpart = "category,title"; | |
4010 | } | |
4011 | } else { | |
4012 | if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) { | |
4013 | $order_by_qpart = "unread DESC,title"; | |
4014 | } else { | |
4015 | $order_by_qpart = "title"; | |
4016 | } | |
4017 | } | |
4018 | ||
4019 | $age_qpart = getMaxAgeSubquery(); | |
4020 | ||
4021 | $result = db_query($link, "SELECT ttrss_feeds.*, | |
4022 | SUBSTRING(last_updated,1,19) AS last_updated_noms, | |
4023 | (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries | |
4024 | WHERE feed_id = ttrss_feeds.id AND unread = true | |
4025 | AND $age_qpart | |
4026 | AND ttrss_user_entries.ref_id = ttrss_entries.id | |
4027 | AND owner_uid = '$owner_uid') as unread, | |
4028 | cat_id,last_error, | |
4029 | ttrss_feed_categories.title AS category, | |
4030 | ttrss_feed_categories.collapsed | |
4031 | FROM ttrss_feeds LEFT JOIN ttrss_feed_categories | |
4032 | ON (ttrss_feed_categories.id = cat_id) | |
4033 | WHERE | |
4034 | ttrss_feeds.hidden = false AND | |
4035 | ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL | |
4036 | ORDER BY $order_by_qpart"); | |
4037 | ||
4038 | $actid = $_GET["actid"]; | |
4039 | ||
4040 | /* real feeds */ | |
4041 | ||
4042 | $lnum = 0; | |
4043 | ||
4044 | $total_unread = 0; | |
4045 | ||
4046 | $category = ""; | |
4047 | ||
4048 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); | |
4049 | ||
4050 | while ($line = db_fetch_assoc($result)) { | |
4051 | ||
4052 | $feed = trim($line["title"]); | |
4053 | ||
4054 | if (!$feed) $feed = "[Untitled]"; | |
4055 | ||
4056 | $feed_id = $line["id"]; | |
4057 | ||
4058 | $subop = $_GET["subop"]; | |
4059 | ||
4060 | $unread = $line["unread"]; | |
4061 | ||
4062 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
4063 | $last_updated = smart_date_time(strtotime($line["last_updated_noms"])); | |
4064 | } else { | |
4065 | $last_updated = date($short_date, strtotime($line["last_updated_noms"])); | |
4066 | } | |
4067 | ||
4068 | $rtl_content = sql_bool_to_bool($line["rtl_content"]); | |
4069 | ||
4070 | if ($rtl_content) { | |
4071 | $rtl_tag = "dir=\"RTL\""; | |
4072 | } else { | |
4073 | $rtl_tag = ""; | |
4074 | } | |
4075 | ||
4076 | $tmp_result = db_query($link, | |
4077 | "SELECT id,COUNT(unread) AS unread | |
4078 | FROM ttrss_feeds LEFT JOIN ttrss_user_entries | |
4079 | ON (ttrss_feeds.id = ttrss_user_entries.feed_id) | |
4080 | WHERE parent_feed = '$feed_id' AND unread = true | |
4081 | GROUP BY ttrss_feeds.id"); | |
4082 | ||
4083 | if (db_num_rows($tmp_result) > 0) { | |
4084 | while ($l = db_fetch_assoc($tmp_result)) { | |
4085 | $unread += $l["unread"]; | |
4086 | } | |
4087 | } | |
4088 | ||
4089 | $cat_id = $line["cat_id"]; | |
4090 | ||
4091 | $tmp_category = $line["category"]; | |
4092 | ||
4093 | if (!$tmp_category) { | |
4094 | $tmp_category = __("Uncategorized"); | |
4095 | } | |
4096 | ||
4097 | // $class = ($lnum % 2) ? "even" : "odd"; | |
4098 | ||
4099 | if ($line["last_error"]) { | |
4100 | $class = "error"; | |
4101 | } else { | |
4102 | $class = "feed"; | |
4103 | } | |
4104 | ||
4105 | if ($unread > 0) $class .= "Unread"; | |
4106 | ||
4107 | if ($actid == $feed_id) { | |
4108 | $class .= "Selected"; | |
4109 | } | |
4110 | ||
4111 | $total_unread += $unread; | |
4112 | ||
4113 | if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) { | |
4114 | ||
4115 | if ($category) { | |
4116 | print "</ul></li>"; | |
4117 | } | |
4118 | ||
4119 | $category = $tmp_category; | |
4120 | ||
4121 | $collapsed = $line["collapsed"]; | |
4122 | ||
4123 | // workaround for NULL category | |
4124 | if ($category == __("Uncategorized")) { | |
4125 | if ($_COOKIE["ttrss_vf_uclps"] == 1) { | |
4126 | $collapsed = "t"; | |
4127 | } | |
4128 | } | |
4129 | ||
4130 | if ($collapsed == "t" || $collapsed == "1") { | |
4131 | $holder_class = "feedCatHolder"; | |
4132 | $holder_style = "display:none;"; | |
4133 | $ellipsis = "…"; | |
4134 | } else { | |
4135 | $holder_class = "feedCatHolder"; | |
4136 | $holder_style = ""; | |
4137 | $ellipsis = ""; | |
4138 | } | |
4139 | ||
4140 | $cat_id = sprintf("%d", $cat_id); | |
4141 | ||
4142 | $cat_unread = getCategoryUnread($link, $cat_id); | |
4143 | ||
4144 | $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread"; | |
4145 | ||
4146 | print "<li class=\"feedCat\" id=\"FCAT-$cat_id\"> | |
4147 | <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a> | |
4148 | <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\"> | |
4149 | <span id=\"FCATCTR-$cat_id\" | |
4150 | class=\"$catctr_class\">($cat_unread)</span> $ellipsis | |
4151 | </a></li>"; | |
4152 | ||
4153 | print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>"; | |
4154 | } | |
4155 | ||
4156 | printFeedEntry($feed_id, $class, $feed, $unread, | |
4157 | ICONS_DIR."/$feed_id.ico", $link, $rtl_content, | |
4158 | $last_updated, $line["last_error"]); | |
4159 | ||
4160 | ++$lnum; | |
4161 | } | |
4162 | ||
4163 | if (db_num_rows($result) == 0) { | |
4164 | print "<li>".__('No feeds to display.')."</li>"; | |
4165 | } | |
4166 | ||
4167 | } else { | |
4168 | ||
4169 | // tags | |
4170 | ||
4171 | /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count | |
4172 | FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE | |
4173 | post_int_id = ttrss_user_entries.int_id AND | |
4174 | unread = true AND ref_id = ttrss_entries.id | |
4175 | AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name | |
4176 | UNION | |
4177 | select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid' | |
4178 | ORDER BY tag_name"); */ | |
4179 | ||
4180 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
4181 | print "<li class=\"feedCat\">".__('Tags')."</li>"; | |
4182 | print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">"; | |
4183 | } | |
4184 | ||
4185 | $age_qpart = getMaxAgeSubquery(); | |
4186 | ||
4187 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) | |
4188 | FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id | |
4189 | AND ref_id = id AND $age_qpart | |
4190 | AND unread = true)) AS count FROM ttrss_tags | |
4191 | WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name | |
4192 | ORDER BY count DESC LIMIT 50"); | |
4193 | ||
4194 | $tags = array(); | |
4195 | ||
4196 | while ($line = db_fetch_assoc($result)) { | |
4197 | $tags[$line["tag_name"]] += $line["count"]; | |
4198 | } | |
4199 | ||
4200 | foreach (array_keys($tags) as $tag) { | |
4201 | ||
4202 | $unread = $tags[$tag]; | |
4203 | ||
4204 | $class = "tag"; | |
4205 | ||
4206 | if ($unread > 0) { | |
4207 | $class .= "Unread"; | |
4208 | } | |
4209 | ||
4210 | printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link); | |
4211 | ||
4212 | } | |
4213 | ||
4214 | if (db_num_rows($result) == 0) { | |
4215 | print "<li>No tags to display.</li>"; | |
4216 | } | |
4217 | ||
4218 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
4219 | print "</ul>"; | |
4220 | } | |
4221 | ||
4222 | } | |
4223 | ||
4224 | print "</ul>"; | |
4225 | ||
4226 | } | |
4227 | ||
4228 | function get_article_tags($link, $id, $owner_uid = 0) { | |
4229 | ||
4230 | $a_id = db_escape_string($id); | |
4231 | ||
4232 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
4233 | ||
4234 | $tmp_result = db_query($link, "SELECT DISTINCT tag_name, | |
4235 | owner_uid as owner FROM | |
4236 | ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE | |
4237 | ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name"); | |
4238 | ||
4239 | $tags = array(); | |
4240 | ||
4241 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
4242 | array_push($tags, $tmp_line["tag_name"]); | |
4243 | } | |
4244 | ||
4245 | return $tags; | |
4246 | } | |
4247 | ||
4248 | function trim_value(&$value) { | |
4249 | $value = trim($value); | |
4250 | } | |
4251 | ||
4252 | function trim_array($array) { | |
4253 | $tmp = $array; | |
4254 | array_walk($tmp, 'trim_value'); | |
4255 | return $tmp; | |
4256 | } | |
4257 | ||
4258 | function tag_is_valid($tag) { | |
4259 | if ($tag == '') return false; | |
4260 | if (preg_match("/^[0-9]*$/", $tag)) return false; | |
4261 | ||
4262 | $tag = iconv("utf-8", "utf-8", $tag); | |
4263 | if (!$tag) return false; | |
4264 | ||
4265 | return true; | |
4266 | } | |
4267 | ||
4268 | function render_login_form($link, $mobile = false) { | |
4269 | if (!$mobile) { | |
4270 | require_once "login_form.php"; | |
4271 | } else { | |
4272 | require_once "mobile/login_form.php"; | |
4273 | } | |
4274 | } | |
4275 | ||
4276 | // from http://developer.apple.com/internet/safari/faq.html | |
4277 | function no_cache_incantation() { | |
4278 | header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :) | |
4279 | header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified | |
4280 | header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1 | |
4281 | header("Cache-Control: post-check=0, pre-check=0", false); | |
4282 | header("Pragma: no-cache"); // HTTP/1.0 | |
4283 | } | |
4284 | ||
4285 | function format_warning($msg, $id = "") { | |
4286 | return "<div class=\"warning\" id=\"$id\"> | |
4287 | <img src=\"images/sign_excl.gif\">$msg</div>"; | |
4288 | } | |
4289 | ||
4290 | function format_notice($msg) { | |
4291 | return "<div class=\"notice\"> | |
4292 | <img src=\"images/sign_info.gif\">$msg</div>"; | |
4293 | } | |
4294 | ||
4295 | function format_error($msg) { | |
4296 | return "<div class=\"error\"> | |
4297 | <img src=\"images/sign_excl.gif\">$msg</div>"; | |
4298 | } | |
4299 | ||
4300 | function print_notice($msg) { | |
4301 | return print format_notice($msg); | |
4302 | } | |
4303 | ||
4304 | function print_warning($msg) { | |
4305 | return print format_warning($msg); | |
4306 | } | |
4307 | ||
4308 | function print_error($msg) { | |
4309 | return print format_error($msg); | |
4310 | } | |
4311 | ||
4312 | ||
4313 | function T_sprintf() { | |
4314 | $args = func_get_args(); | |
4315 | return vsprintf(__(array_shift($args)), $args); | |
4316 | } | |
4317 | ||
4318 | function outputArticleXML($link, $id, $feed_id, $mark_as_read = true) { | |
4319 | ||
4320 | /* we can figure out feed_id from article id anyway, why do we | |
4321 | * pass feed_id here? */ | |
4322 | ||
4323 | $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries | |
4324 | WHERE ref_id = '$id'"); | |
4325 | ||
4326 | $feed_id = db_fetch_result($result, 0, "feed_id"); | |
4327 | ||
4328 | print "<article id='$id'><![CDATA["; | |
4329 | ||
4330 | $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds | |
4331 | WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]); | |
4332 | ||
4333 | if (db_num_rows($result) == 1) { | |
4334 | $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content")); | |
4335 | } else { | |
4336 | $rtl_content = false; | |
4337 | } | |
4338 | ||
4339 | if ($rtl_content) { | |
4340 | $rtl_tag = "dir=\"RTL\""; | |
4341 | $rtl_class = "RTL"; | |
4342 | } else { | |
4343 | $rtl_tag = ""; | |
4344 | $rtl_class = ""; | |
4345 | } | |
4346 | ||
4347 | if ($mark_as_read) { | |
4348 | $result = db_query($link, "UPDATE ttrss_user_entries | |
4349 | SET unread = false,last_read = NOW() | |
4350 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
4351 | } | |
4352 | ||
4353 | $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id, | |
4354 | SUBSTRING(updated,1,16) as updated, | |
4355 | (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url, | |
4356 | num_comments, | |
4357 | author | |
4358 | FROM ttrss_entries,ttrss_user_entries | |
4359 | WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]); | |
4360 | ||
4361 | if ($result) { | |
4362 | ||
4363 | $link_target = ""; | |
4364 | ||
4365 | if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) { | |
4366 | $link_target = "target=\"_new\""; | |
4367 | } | |
4368 | ||
4369 | $line = db_fetch_assoc($result); | |
4370 | ||
4371 | if ($line["icon_url"]) { | |
4372 | $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">"; | |
4373 | } else { | |
4374 | $feed_icon = " "; | |
4375 | } | |
4376 | ||
4377 | /* if ($line["comments"] && $line["link"] != $line["comments"]) { | |
4378 | $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)"; | |
4379 | } else { | |
4380 | $entry_comments = ""; | |
4381 | } */ | |
4382 | ||
4383 | $num_comments = $line["num_comments"]; | |
4384 | $entry_comments = ""; | |
4385 | ||
4386 | if ($num_comments > 0) { | |
4387 | if ($line["comments"]) { | |
4388 | $comments_url = $line["comments"]; | |
4389 | } else { | |
4390 | $comments_url = $line["link"]; | |
4391 | } | |
4392 | $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>"; | |
4393 | } else { | |
4394 | if ($line["comments"] && $line["link"] != $line["comments"]) { | |
4395 | $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>"; | |
4396 | } | |
4397 | } | |
4398 | ||
4399 | print "<div class=\"postReply\">"; | |
4400 | ||
4401 | print "<div class=\"postHeader\">"; | |
4402 | ||
4403 | $entry_author = $line["author"]; | |
4404 | ||
4405 | if ($entry_author) { | |
4406 | $entry_author = __(" - by ") . $entry_author; | |
4407 | } | |
4408 | ||
4409 | $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'), | |
4410 | strtotime($line["updated"])); | |
4411 | ||
4412 | print "<div class=\"postDate$rtl_class\">$parsed_updated</div>"; | |
4413 | ||
4414 | if ($line["link"]) { | |
4415 | print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" . | |
4416 | $line["title"] . "</a><span class='author'>$entry_author</span></div>"; | |
4417 | } else { | |
4418 | print "<div clear='both'>" . $line["title"] . "$entry_author</div>"; | |
4419 | } | |
4420 | ||
4421 | /* $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM | |
4422 | ttrss_tags WHERE post_int_id = " . $line["int_id"] . " | |
4423 | ORDER BY tag_name"); */ | |
4424 | ||
4425 | $tags = get_article_tags($link, $id); | |
4426 | ||
4427 | $tags_str = ""; | |
4428 | $f_tags_str = ""; | |
4429 | ||
4430 | $num_tags = 0; | |
4431 | ||
4432 | if ($_SESSION["theme"] == "3pane") { | |
4433 | $tag_limit = 3; | |
4434 | } else { | |
4435 | $tag_limit = 6; | |
4436 | } | |
4437 | ||
4438 | foreach ($tags as $tag) { | |
4439 | $num_tags++; | |
4440 | $tag_escaped = str_replace("'", "\\'", $tag); | |
4441 | ||
4442 | $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, "; | |
4443 | ||
4444 | if ($num_tags == $tag_limit) { | |
4445 | $tags_str .= "…"; | |
4446 | ||
4447 | } else if ($num_tags < $tag_limit) { | |
4448 | $tags_str .= $tag_str; | |
4449 | } | |
4450 | $f_tags_str .= $tag_str; | |
4451 | } | |
4452 | ||
4453 | $tags_str = preg_replace("/, $/", "", $tags_str); | |
4454 | $f_tags_str = preg_replace("/, $/", "", $f_tags_str); | |
4455 | ||
4456 | $all_tags_div = "<span class='cdmAllTagsCtr'>…<div class='cdmAllTags'>All Tags: $f_tags_str</div></span>"; | |
4457 | $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str); | |
4458 | ||
4459 | if (!$entry_comments) $entry_comments = " "; # placeholder | |
4460 | ||
4461 | if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>'; | |
4462 | ||
4463 | print "<div style='float : right'> | |
4464 | <img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'> | |
4465 | $tags_str | |
4466 | <a title=\"Edit tags for this article\" | |
4467 | href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div> | |
4468 | <div clear='both'>$entry_comments</div>"; | |
4469 | ||
4470 | print "</div>"; | |
4471 | ||
4472 | print "<div class=\"postIcon\">" . $feed_icon . "</div>"; | |
4473 | print "<div class=\"postContent\">"; | |
4474 | ||
4475 | #print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>"; | |
4476 | ||
4477 | $line["content"] = sanitize_rss($link, $line["content"]); | |
4478 | ||
4479 | if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) { | |
4480 | $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]); | |
4481 | } | |
4482 | ||
4483 | print $line["content"]; | |
4484 | ||
4485 | $result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE | |
4486 | post_id = '$id'"); | |
4487 | ||
4488 | if (db_num_rows($result) > 0) { | |
4489 | print "<div class=\"postEnclosures\">"; | |
4490 | ||
4491 | if (db_num_rows($result) == 1) { | |
4492 | print __("Attachment:") . " "; | |
4493 | } else { | |
4494 | print __("Attachments:") . " "; | |
4495 | } | |
4496 | ||
4497 | $entries = array(); | |
4498 | ||
4499 | while ($line = db_fetch_assoc($result)) { | |
4500 | ||
4501 | $url = $line["content_url"]; | |
4502 | ||
4503 | $filename = substr($url, strrpos($url, "/")+1); | |
4504 | ||
4505 | $entry = "<a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" . | |
4506 | $filename . " (" . $line["content_type"] . ")" . "</a>"; | |
4507 | ||
4508 | array_push($entries, $entry); | |
4509 | } | |
4510 | ||
4511 | print join(", ", $entries); | |
4512 | ||
4513 | print "</div>"; | |
4514 | } | |
4515 | ||
4516 | print "</div>"; | |
4517 | ||
4518 | print "</div>"; | |
4519 | ||
4520 | } | |
4521 | ||
4522 | print "]]></article>"; | |
4523 | ||
4524 | } | |
4525 | ||
4526 | function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view, | |
4527 | $next_unread_feed, $offset) { | |
4528 | ||
4529 | $disable_cache = false; | |
4530 | ||
4531 | $timing_info = getmicrotime(); | |
4532 | ||
4533 | $topmost_article_ids = array(); | |
4534 | ||
4535 | if (!$offset) { | |
4536 | $offset = 0; | |
4537 | } | |
4538 | ||
4539 | if ($subop == "undefined") $subop = ""; | |
4540 | ||
4541 | if ($subop == "CatchupSelected") { | |
4542 | $ids = split(",", db_escape_string($_GET["ids"])); | |
4543 | $cmode = sprintf("%d", $_GET["cmode"]); | |
4544 | ||
4545 | catchupArticlesById($link, $ids, $cmode); | |
4546 | } | |
4547 | ||
4548 | if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) { | |
4549 | update_generic_feed($link, $feed, $cat_view, true); | |
4550 | } | |
4551 | ||
4552 | if ($subop == "MarkAllRead") { | |
4553 | catchup_feed($link, $feed, $cat_view); | |
4554 | ||
4555 | if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) { | |
4556 | if ($next_unread_feed) { | |
4557 | $feed = $next_unread_feed; | |
4558 | } | |
4559 | } | |
4560 | } | |
4561 | ||
4562 | if ($feed_id > 0) { | |
4563 | $result = db_query($link, | |
4564 | "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1"); | |
4565 | ||
4566 | if (db_num_rows($result) == 0) { | |
4567 | print "<div align='center'>".__('Feed not found.')."</div>"; | |
4568 | return; | |
4569 | } | |
4570 | } | |
4571 | ||
4572 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
4573 | ||
4574 | $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds | |
4575 | WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
4576 | ||
4577 | if (db_num_rows($result) == 1) { | |
4578 | $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content")); | |
4579 | } else { | |
4580 | $rtl_content = false; | |
4581 | } | |
4582 | ||
4583 | if ($rtl_content) { | |
4584 | $rtl_tag = "dir=\"RTL\""; | |
4585 | } else { | |
4586 | $rtl_tag = ""; | |
4587 | } | |
4588 | } else { | |
4589 | $rtl_tag = ""; | |
4590 | $rtl_content = false; | |
4591 | } | |
4592 | ||
4593 | $script_dt_add = get_script_dt_add(); | |
4594 | ||
4595 | /// START ///////////////////////////////////////////////////////////////////////////////// | |
4596 | ||
4597 | $search = db_escape_string($_GET["query"]); | |
4598 | ||
4599 | if ($search) { | |
4600 | $disable_cache = true; | |
4601 | } | |
4602 | ||
4603 | $search_mode = db_escape_string($_GET["search_mode"]); | |
4604 | $match_on = db_escape_string($_GET["match_on"]); | |
4605 | ||
4606 | if (!$match_on) { | |
4607 | $match_on = "both"; | |
4608 | } | |
4609 | ||
4610 | $real_offset = $offset * $limit; | |
4611 | ||
4612 | if ($_GET["debug"]) $timing_info = print_checkpoint("H0", $timing_info); | |
4613 | ||
4614 | $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, | |
4615 | $search, $search_mode, $match_on, false, $real_offset); | |
4616 | ||
4617 | if ($_GET["debug"]) $timing_info = print_checkpoint("H1", $timing_info); | |
4618 | ||
4619 | $result = $qfh_ret[0]; | |
4620 | $feed_title = $qfh_ret[1]; | |
4621 | $feed_site_url = $qfh_ret[2]; | |
4622 | $last_error = $qfh_ret[3]; | |
4623 | ||
4624 | if ($feed == -2) { | |
4625 | $feed_site_url = article_publish_url($link); | |
4626 | } | |
4627 | ||
4628 | /// STOP ////////////////////////////////////////////////////////////////////////////////// | |
4629 | ||
4630 | if (!$offset) { | |
4631 | print "<div id=\"headlinesContainer\" $rtl_tag>"; | |
4632 | ||
4633 | if (!$result) { | |
4634 | print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>"; | |
4635 | return; | |
4636 | } | |
4637 | ||
4638 | print_headline_subtoolbar($link, $feed_site_url, $feed_title, false, | |
4639 | $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode, | |
4640 | $offset, $limit); | |
4641 | ||
4642 | print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">"; | |
4643 | } | |
4644 | ||
4645 | $headlines_count = db_num_rows($result); | |
4646 | ||
4647 | if (db_num_rows($result) > 0) { | |
4648 | ||
4649 | # print "\{$offset}"; | |
4650 | ||
4651 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) { | |
4652 | print "<table class=\"headlinesList\" id=\"headlinesList\" | |
4653 | cellspacing=\"0\">"; | |
4654 | } | |
4655 | ||
4656 | $lnum = $limit*$offset; | |
4657 | ||
4658 | error_reporting (DEFAULT_ERROR_LEVEL); | |
4659 | ||
4660 | $num_unread = 0; | |
4661 | ||
4662 | while ($line = db_fetch_assoc($result)) { | |
4663 | ||
4664 | $class = ($lnum % 2) ? "even" : "odd"; | |
4665 | ||
4666 | $id = $line["id"]; | |
4667 | $feed_id = $line["feed_id"]; | |
4668 | ||
4669 | if (count($topmost_article_ids) < 5) { | |
4670 | array_push($topmost_article_ids, $id); | |
4671 | } | |
4672 | ||
4673 | if ($line["last_read"] == "" && | |
4674 | ($line["unread"] != "t" && $line["unread"] != "1")) { | |
4675 | ||
4676 | $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\" | |
4677 | alt=\"Updated\">"; | |
4678 | } else { | |
4679 | $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\" | |
4680 | alt=\"Updated\">"; | |
4681 | } | |
4682 | ||
4683 | if ($line["unread"] == "t" || $line["unread"] == "1") { | |
4684 | $class .= "Unread"; | |
4685 | ++$num_unread; | |
4686 | $is_unread = true; | |
4687 | } else { | |
4688 | $is_unread = false; | |
4689 | } | |
4690 | ||
4691 | $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false); | |
4692 | ||
4693 | if ($is_ie) { | |
4694 | $mark_img_ext = "gif"; | |
4695 | } else { | |
4696 | $mark_img_ext = "png"; | |
4697 | } | |
4698 | ||
4699 | if ($line["marked"] == "t" || $line["marked"] == "1") { | |
4700 | $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.$mark_img_ext\" | |
4701 | class=\"markedPic\" | |
4702 | alt=\"Unstar article\" onclick='javascript:tMark($id)'>"; | |
4703 | } else { | |
4704 | $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.$mark_img_ext\" | |
4705 | class=\"markedPic\" | |
4706 | alt=\"Star article\" onclick='javascript:tMark($id)'>"; | |
4707 | } | |
4708 | ||
4709 | if ($line["published"] == "t" || $line["published"] == "1") { | |
4710 | $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.gif\" | |
4711 | class=\"markedPic\" | |
4712 | alt=\"Unpublish article\" onclick='javascript:tPub($id)'>"; | |
4713 | } else { | |
4714 | $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.gif\" | |
4715 | class=\"markedPic\" | |
4716 | alt=\"Publish article\" onclick='javascript:tPub($id)'>"; | |
4717 | } | |
4718 | ||
4719 | # $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" . | |
4720 | # $line["title"] . "</a>"; | |
4721 | ||
4722 | $content_link = "<a href=\"javascript:view($id,$feed_id);\">" . | |
4723 | $line["title"] . "</a>"; | |
4724 | ||
4725 | # $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" . | |
4726 | # $line["title"] . "</a>"; | |
4727 | ||
4728 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
4729 | $updated_fmt = smart_date_time(strtotime($line["updated_noms"])); | |
4730 | } else { | |
4731 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); | |
4732 | $updated_fmt = date($short_date, strtotime($line["updated_noms"])); | |
4733 | } | |
4734 | ||
4735 | if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) { | |
4736 | $content_preview = truncate_string(strip_tags($line["content_preview"]), | |
4737 | 100); | |
4738 | } | |
4739 | ||
4740 | $entry_author = $line["author"]; | |
4741 | ||
4742 | if ($entry_author) { | |
4743 | $entry_author = " - by $entry_author"; | |
4744 | } | |
4745 | ||
4746 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) { | |
4747 | ||
4748 | print "<tr class='$class' id='RROW-$id'>"; | |
4749 | ||
4750 | print "<td class='hlUpdPic'>$update_pic</td>"; | |
4751 | ||
4752 | print "<td class='hlSelectRow'> | |
4753 | <input type=\"checkbox\" onclick=\"tSR(this)\" | |
4754 | id=\"RCHK-$id\"> | |
4755 | </td>"; | |
4756 | ||
4757 | print "<td class='hlMarkedPic'>$marked_pic</td>"; | |
4758 | print "<td class='hlMarkedPic'>$published_pic</td>"; | |
4759 | ||
4760 | # if ($line["feed_title"]) { | |
4761 | # print "<td class='hlContent'>$content_link</td>"; | |
4762 | # print "<td class='hlFeed'> | |
4763 | # <a href=\"javascript:viewfeed($feed_id, '', false)\">". | |
4764 | # truncate_string($line["feed_title"],30)."</a> </td>"; | |
4765 | # } else { | |
4766 | ||
4767 | print "<td class='hlContent' valign='middle'>"; | |
4768 | ||
4769 | print "<a href=\"javascript:view($id,$feed_id);\">" . | |
4770 | $line["title"]; | |
4771 | ||
4772 | if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) { | |
4773 | if ($content_preview) { | |
4774 | print "<span class=\"contentPreview\"> - $content_preview</span>"; | |
4775 | } | |
4776 | } | |
4777 | ||
4778 | print "</a>"; | |
4779 | ||
4780 | # <a href=\"javascript:viewfeed($feed_id, '', false)\">". | |
4781 | # $line["feed_title"]."</a> | |
4782 | ||
4783 | if ($line["feed_title"]) { | |
4784 | print "<span class=\"hlFeed\"> | |
4785 | (<a href=\"javascript:viewfeed($feed_id, '', false)\">". | |
4786 | $line["feed_title"]."</a>) | |
4787 | </span>"; | |
4788 | } | |
4789 | ||
4790 | ||
4791 | print "</td>"; | |
4792 | ||
4793 | # } | |
4794 | ||
4795 | print "<td class=\"hlUpdated\"><nobr>$updated_fmt </nobr></td>"; | |
4796 | ||
4797 | print "</tr>"; | |
4798 | ||
4799 | } else { | |
4800 | ||
4801 | if ($is_unread) { | |
4802 | $add_class = "Unread"; | |
4803 | } else { | |
4804 | $add_class = ""; | |
4805 | } | |
4806 | ||
4807 | $expand_cdm = get_pref($link, 'CDM_EXPANDED'); | |
4808 | ||
4809 | if ($expand_cdm) { | |
4810 | $cdm_cstyle = ""; | |
4811 | } else { | |
4812 | $cdm_cstyle = "style=\"display : none\""; | |
4813 | } | |
4814 | ||
4815 | print "<div class=\"cdmArticle$add_class\" | |
4816 | id=\"RROW-$id\" onmouseover='cdmMouseIn(this)' | |
4817 | onmouseout='cdmMouseOut(this)'>"; | |
4818 | ||
4819 | print "<div class=\"cdmHeader\">"; | |
4820 | ||
4821 | print "<div class=\"articleUpdated\">$updated_fmt</div>"; | |
4822 | ||
4823 | print "<a class=\"title\" | |
4824 | onclick=\"javascript:toggleUnread($id, 0)\" | |
4825 | target=\"_new\" href=\"".$line["link"]."\">".$line["title"]."</a>"; | |
4826 | ||
4827 | print $entry_author; | |
4828 | ||
4829 | if (!$expand_cdm) { | |
4830 | print " <a id=\"CICH-$id\" | |
4831 | href=\"javascript:cdmExpandArticle($id)\"> | |
4832 | (".__('Show article').")</a>"; | |
4833 | } | |
4834 | ||
4835 | ||
4836 | if ($line["feed_title"]) { | |
4837 | print " (<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)"; | |
4838 | } | |
4839 | ||
4840 | print "</div>"; | |
4841 | ||
4842 | if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) { | |
4843 | $line["content_preview"] = preg_replace("/href=/i", | |
4844 | "target=\"_new\" href=", $line["content_preview"]); | |
4845 | } | |
4846 | ||
4847 | print "<div class=\"cdmContent\" id=\"CICD-$id\" $cdm_cstyle>"; | |
4848 | ||
4849 | // print "<div class=\"cdmInnerContent\" id=\"CICD-$id\" $cdm_cstyle>"; | |
4850 | print $line["content_preview"]; | |
4851 | ||
4852 | $e_result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE | |
4853 | post_id = '$id'"); | |
4854 | ||
4855 | if (db_num_rows($e_result) > 0) { | |
4856 | print "<div class=\"cdmEnclosures\">"; | |
4857 | ||
4858 | if (db_num_rows($e_result) == 1) { | |
4859 | print __("Attachment:") . " "; | |
4860 | } else { | |
4861 | print __("Attachments:") . " "; | |
4862 | } | |
4863 | ||
4864 | $entries = array(); | |
4865 | ||
4866 | while ($e_line = db_fetch_assoc($e_result)) { | |
4867 | ||
4868 | $url = $e_line["content_url"]; | |
4869 | ||
4870 | $filename = substr($url, strrpos($url, "/")+1); | |
4871 | ||
4872 | $entry = "<a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" . | |
4873 | $filename . " (" . $e_line["content_type"] . ")" . "</a>"; | |
4874 | ||
4875 | array_push($entries, $entry); | |
4876 | } | |
4877 | ||
4878 | print join(", ", $entries); | |
4879 | ||
4880 | print "</div>"; | |
4881 | } | |
4882 | ||
4883 | print "<br clear='both'>"; | |
4884 | // print "</div>"; | |
4885 | ||
4886 | /* if (!$expand_cdm) { | |
4887 | print "<a id=\"CICH-$id\" | |
4888 | href=\"javascript:cdmExpandArticle($id)\"> | |
4889 | Show article</a>"; | |
4890 | } */ | |
4891 | ||
4892 | print "</div>"; | |
4893 | ||
4894 | print "<div class=\"cdmFooter\"><span class='s0'>"; | |
4895 | ||
4896 | /* print "<div class=\"markedPic\">Star it: $marked_pic</div>"; */ | |
4897 | ||
4898 | print __("Select:"). | |
4899 | " <input type=\"checkbox\" onclick=\"toggleSelectRowById(this, | |
4900 | 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">"; | |
4901 | ||
4902 | print "</span><span class='s1'>$marked_pic</span> "; | |
4903 | print "<span class='s1'>$published_pic</span> "; | |
4904 | ||
4905 | $tags = get_article_tags($link, $id); | |
4906 | ||
4907 | $tags_str = ""; | |
4908 | $full_tags_str = ""; | |
4909 | $num_tags = 0; | |
4910 | ||
4911 | foreach ($tags as $tag) { | |
4912 | $num_tags++; | |
4913 | $full_tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, "; | |
4914 | if ($num_tags < 5) { | |
4915 | $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, "; | |
4916 | } else if ($num_tags == 5) { | |
4917 | $tags_str .= "…"; | |
4918 | } | |
4919 | } | |
4920 | ||
4921 | $tags_str = preg_replace("/, $/", "", $tags_str); | |
4922 | $full_tags_str = preg_replace("/, $/", "", $full_tags_str); | |
4923 | ||
4924 | $all_tags_div = "<span class='cdmAllTagsCtr'>…<div class='cdmAllTags'>All Tags: $full_tags_str</div></span>"; | |
4925 | ||
4926 | $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str); | |
4927 | ||
4928 | ||
4929 | if ($tags_str == "") $tags_str = "no tags"; | |
4930 | ||
4931 | // print "<img src='images/tag.png' class='markedPic'>"; | |
4932 | ||
4933 | print "<span class='s1'> | |
4934 | <img class='tagsPic' src='images/tag.png' alt='Tags' | |
4935 | title='Tags'> $tags_str <a title=\"Edit tags for this article\" | |
4936 | href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>"; | |
4937 | ||
4938 | print "</span>"; | |
4939 | ||
4940 | print "<span class='s2'>Toggle: <a class=\"cdmToggleLink\" | |
4941 | href=\"javascript:toggleUnread($id)\"> | |
4942 | Unread</a></span>"; | |
4943 | ||
4944 | print "</div>"; | |
4945 | print "</div>"; | |
4946 | ||
4947 | } | |
4948 | ||
4949 | ++$lnum; | |
4950 | } | |
4951 | ||
4952 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) { | |
4953 | print "</table>"; | |
4954 | } | |
4955 | ||
4956 | // print_headline_subtoolbar($link, | |
4957 | // "javascript:catchupPage()", "Mark page as read", true, $rtl_content); | |
4958 | ||
4959 | ||
4960 | } else { | |
4961 | if (!$offset) print "<div class='whiteBox'>".__('No articles found.')."</div>"; | |
4962 | } | |
4963 | ||
4964 | if (!$offset) { | |
4965 | print "</div>"; | |
4966 | print "</div>"; | |
4967 | } | |
4968 | ||
4969 | return array($topmost_article_ids, $headlines_count, $feed, $disable_cache); | |
4970 | } | |
4971 | ||
4972 | // from here: http://www.roscripts.com/Create_tag_cloud-71.html | |
4973 | ||
4974 | function printTagCloud($link) { | |
4975 | ||
4976 | /* get first ref_id to count from */ | |
4977 | ||
4978 | /* | |
4979 | ||
4980 | $query = ""; | |
4981 | ||
4982 | if (DB_TYPE == "pgsql") { | |
4983 | $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries | |
4984 | WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]." | |
4985 | AND date_entered > NOW() - INTERVAL '30 days'"; | |
4986 | } else { | |
4987 | $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries | |
4988 | WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]." | |
4989 | AND date_entered > DATE_SUB(NOW(), INTERVAL 30 DAY)"; | |
4990 | } | |
4991 | ||
4992 | $result = db_query($link, $query); | |
4993 | $first_id = db_fetch_result($result, 0, "id"); */ | |
4994 | ||
4995 | //AND post_int_id >= '$first_id' | |
4996 | $query = "SELECT tag_name, COUNT(post_int_id) AS count | |
4997 | FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]." | |
4998 | GROUP BY tag_name ORDER BY count DESC LIMIT 50"; | |
4999 | ||
5000 | $result = db_query($link, $query); | |
5001 | ||
5002 | $tags = array(); | |
5003 | ||
5004 | while ($line = db_fetch_assoc($result)) { | |
5005 | $tags[$line["tag_name"]] = $line["count"]; | |
5006 | } | |
5007 | ||
5008 | ksort($tags); | |
5009 | ||
5010 | $max_size = 32; // max font size in pixels | |
5011 | $min_size = 11; // min font size in pixels | |
5012 | ||
5013 | // largest and smallest array values | |
5014 | $max_qty = max(array_values($tags)); | |
5015 | $min_qty = min(array_values($tags)); | |
5016 | ||
5017 | // find the range of values | |
5018 | $spread = $max_qty - $min_qty; | |
5019 | if ($spread == 0) { // we don't want to divide by zero | |
5020 | $spread = 1; | |
5021 | } | |
5022 | ||
5023 | // set the font-size increment | |
5024 | $step = ($max_size - $min_size) / ($spread); | |
5025 | ||
5026 | // loop through the tag array | |
5027 | foreach ($tags as $key => $value) { | |
5028 | // calculate font-size | |
5029 | // find the $value in excess of $min_qty | |
5030 | // multiply by the font-size increment ($size) | |
5031 | // and add the $min_size set above | |
5032 | $size = round($min_size + (($value - $min_qty) * $step)); | |
5033 | ||
5034 | $key_escaped = str_replace("'", "\\'", $key); | |
5035 | ||
5036 | echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " . | |
5037 | $size . "px\" title=\"$value articles tagged with " . | |
5038 | $key . '">' . $key . '</a> '; | |
5039 | } | |
5040 | } | |
5041 | ||
5042 | function print_checkpoint($n, $s) { | |
5043 | $ts = getmicrotime(); | |
5044 | echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s); | |
5045 | return $ts; | |
5046 | } | |
5047 | ||
5048 | function sanitize_tag($tag) { | |
5049 | $tag = trim($tag); | |
5050 | ||
5051 | $tag = mb_strtolower($tag, 'utf-8'); | |
5052 | ||
5053 | $tag = preg_replace('/[\"\+\>\<]/', "", $tag); | |
5054 | ||
5055 | // $tag = str_replace('"', "", $tag); | |
5056 | // $tag = str_replace("+", " ", $tag); | |
5057 | $tag = str_replace("technorati tag: ", "", $tag); | |
5058 | ||
5059 | return $tag; | |
5060 | } | |
5061 | ||
5062 | function generate_publish_key() { | |
5063 | return sha1(uniqid(rand(), true)); | |
5064 | } | |
5065 | ||
5066 | function article_publish_url($link) { | |
5067 | ||
5068 | $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH); | |
5069 | ||
5070 | $url_path .= "?op=publish&key=" . get_pref($link, "_PREFS_PUBLISH_KEY"); | |
5071 | ||
5072 | return $url_path; | |
5073 | } | |
5074 | ||
5075 | function clear_feed_articles($link, $id) { | |
5076 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
5077 | WHERE feed_id = '$id' AND marked = false AND owner_uid = " . $_SESSION["uid"]); | |
5078 | ||
5079 | $result = db_query($link, "DELETE FROM ttrss_entries WHERE | |
5080 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
5081 | } | |
5082 | ||
5083 | function add_feed_url() { | |
5084 | $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH); | |
5085 | $url_path .= "?op=pref-feeds&quiet=1&subop=add&feed_url=%s"; | |
5086 | return $url_path; | |
5087 | } | |
5088 | ||
5089 | function encrypt_password($pass, $login = '') { | |
5090 | if ($login) { | |
5091 | return "SHA1X:" . sha1("$login:$pass"); | |
5092 | } else { | |
5093 | return "SHA1:" . sha1($pass); | |
5094 | } | |
5095 | } | |
5096 | ||
5097 | ?> |