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