]> git.wh0rd.org - tt-rss.git/blame - functions.php
TAG 1.2.29
[tt-rss.git] / functions.php
CommitLineData
1d3a17c7 1<?php
f1a80dae 2
894ebcf5 3/* if ($_GET["debug"]) {
cce28758
AD
4 define('DEFAULT_ERROR_LEVEL', E_ALL);
5 } else {
6 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
894ebcf5 7 } */
cce28758 8
40d13c28 9 require_once 'config.php';
cc17c205 10
fc2b26a6
AD
11 if (DB_TYPE == "pgsql") {
12 define('SUBSTRING_FOR_DATE', 'SUBSTRING_FOR_DATE');
13 } else {
14 define('SUBSTRING_FOR_DATE', 'SUBSTRING');
15 }
16
9632f884
AD
17 /**
18 * Return available translations names.
19 *
20 * @access public
21 * @return array A array of available translations.
22 */
f8c612d4 23 function get_translations() {
6a214f92 24 $tr = array(
019bd5a9 25 "auto" => "Detect automatically",
6a214f92
AD
26 "en_US" => "English",
27 "fr_FR" => "Français",
e78fd196 28 "hu_HU" => "Magyar (Hungarian)",
bb5d3960 29 "it_IT" => "Italiano",
1d004f12 30 "ja_JP" => "日本語 (Japanese)",
592535d7 31 "nb_NO" => "Norwegian bokmål",
6a214f92 32 "ru_RU" => "Русский",
9a063469 33 "pt_BR" => "Portuguese/Brazil",
6a214f92 34 "zh_CN" => "Simplified Chinese");
f8c612d4
AD
35
36 return $tr;
37 }
38
9632f884 39 if (ENABLE_TRANSLATIONS == true) { // If translations are enabled.
865220a4
AD
40 require_once "accept-to-gettext.php";
41 require_once "gettext/gettext.inc";
aba609e0 42
8d039718
AD
43 function startup_gettext() {
44
45 # Get locale from Accept-Language header
6a214f92 46 $lang = al2gt(array_keys(get_translations()), "text/html");
89cb787e
AD
47
48 if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) {
49 $lang = _TRANSLATION_OVERRIDE_DEFAULT;
50 }
51
672f3f3c 52 if ($_COOKIE["ttrss_lang"] && $_COOKIE["ttrss_lang"] != "auto") {
68659d98
AD
53 $lang = $_COOKIE["ttrss_lang"];
54 }
55
8d039718 56 if ($lang) {
86e2e1b9
AD
57 if (defined('LC_MESSAGES')) {
58 _setlocale(LC_MESSAGES, $lang);
59 } else if (defined('LC_ALL')) {
60 _setlocale(LC_ALL, $lang);
61 } else {
62 die("can't setlocale(): please set ENABLE_TRANSLATIONS to false in config.php");
63 }
8d039718
AD
64 _bindtextdomain("messages", "locale");
65 _textdomain("messages");
66 _bind_textdomain_codeset("messages", "UTF-8");
67 }
aba609e0 68 }
aba609e0 69
cc17c205 70 startup_gettext();
865220a4 71
9632f884 72 } else { // If translations are enabled.
865220a4
AD
73 function __($msg) {
74 return $msg;
75 }
76 function startup_gettext() {
77 // no-op
78 return true;
79 }
9632f884 80 } // If translations are enabled.
cc17c205 81
b619ff15 82 require_once 'db-prefs.php';
5bc0bd27 83 require_once 'compat.php';
af106b0e 84 require_once 'errors.php';
8911ac8b 85 require_once 'version.php';
40d13c28 86
a8931123
AD
87 require_once 'phpmailer/class.phpmailer.php';
88
49f9c923 89 define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')');
a3ee2a38 90 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
ba0f7628 91 define('MAGPIE_CACHE_AGE', 60*15); // 15 minutes
a3ee2a38 92
16211ddb
AD
93 require_once "simplepie/simplepie.inc";
94 require_once "magpierss/rss_fetch.inc";
95 require_once 'magpierss/rss_utils.inc';
49f9c923 96
45004d43
AD
97 /**
98 * Print a timestamped debug message.
99 *
100 * @param string $msg The debug message.
101 * @return void
102 */
6f9e33e4
AD
103 function _debug($msg) {
104 $ts = strftime("%H:%M:%S", time());
2a6a9395
AD
105 if (function_exists('posix_getpid')) {
106 $ts = "$ts/" . posix_getpid();
107 }
6f9e33e4 108 print "[$ts] $msg\n";
45004d43 109 } // function _debug
6f9e33e4 110
9632f884
AD
111 /**
112 * Purge a feed old posts.
113 *
114 * @param mixed $link A database connection.
115 * @param mixed $feed_id The id of the purged feed.
116 * @param mixed $purge_interval Olderness of purged posts.
117 * @param boolean $debug Set to True to enable the debug. False by default.
118 * @access public
119 * @return void
120 */
ad507f85
AD
121 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
122
07d0efe9
AD
123 if (!$purge_interval) $purge_interval = feed_purge_interval($link, $feed_id);
124
ad507f85 125 $rows = -1;
4c193675 126
07d0efe9
AD
127 $result = db_query($link,
128 "SELECT owner_uid FROM ttrss_feeds WHERE id = '$feed_id'");
129
130 $owner_uid = false;
131
132 if (db_num_rows($result) == 1) {
133 $owner_uid = db_fetch_result($result, 0, "owner_uid");
134 }
135
136 if (!$owner_uid) return;
137
138 $purge_unread = get_pref($link, "PURGE_UNREAD_ARTICLES",
139 $owner_uid, false);
140
141 if (!$purge_unread) $query_limit = " unread = false AND ";
142
fefa6ca3 143 if (DB_TYPE == "pgsql") {
44e241cb 144/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 145 marked = false AND feed_id = '$feed_id' AND
35d8cf43 146 (SELECT date_entered FROM ttrss_entries WHERE
44e241cb
AD
147 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
148
6e7f8d26
AD
149 $pg_version = get_pgsql_version($link);
150
151 if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
1e59ae35
AD
152
153 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
154 ttrss_entries.id = ref_id AND
155 marked = false AND
156 feed_id = '$feed_id' AND
07d0efe9 157 $query_limit
1e59ae35
AD
158 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
159
160 } else {
161
162 $result = db_query($link, "DELETE FROM ttrss_user_entries
163 USING ttrss_entries
164 WHERE ttrss_entries.id = ref_id AND
165 marked = false AND
166 feed_id = '$feed_id' AND
07d0efe9 167 $query_limit
fc774155 168 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
1e59ae35 169 }
ad507f85
AD
170
171 $rows = pg_affected_rows($result);
172
fefa6ca3 173 } else {
1e59ae35 174
30f1746f 175/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 176 marked = false AND feed_id = '$feed_id' AND
35d8cf43 177 (SELECT date_entered FROM ttrss_entries WHERE
30f1746f
AD
178 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
179
180 $result = db_query($link, "DELETE FROM ttrss_user_entries
181 USING ttrss_user_entries, ttrss_entries
182 WHERE ttrss_entries.id = ref_id AND
183 marked = false AND
184 feed_id = '$feed_id' AND
07d0efe9 185 $query_limit
30f1746f
AD
186 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
187
ad507f85
AD
188 $rows = mysql_affected_rows($link);
189
190 }
191
192 if ($debug) {
6f9e33e4 193 _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
fefa6ca3 194 }
9632f884 195 } // function purge_feed
fefa6ca3 196
9632f884
AD
197 /**
198 * Purge old posts from old feeds.
199 *
200 * @param mixed $link A database connection
201 * @param boolean $do_output Set to true to enable printed output, false by default.
202 * @param integer $limit The maximal number of removed posts.
203 * @access public
204 * @return void
205 */
44e241cb
AD
206 function global_purge_old_posts($link, $do_output = false, $limit = false) {
207
894ebcf5 208 $random_qpart = sql_random_function();
fefa6ca3 209
44e241cb
AD
210 if ($limit) {
211 $limit_qpart = "LIMIT $limit";
212 } else {
213 $limit_qpart = "";
214 }
215
fefa6ca3 216 $result = db_query($link,
44e241cb
AD
217 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
218 ORDER BY $random_qpart $limit_qpart");
fefa6ca3
AD
219
220 while ($line = db_fetch_assoc($result)) {
221
222 $feed_id = $line["id"];
223 $purge_interval = $line["purge_interval"];
224 $owner_uid = $line["owner_uid"];
225
226 if ($purge_interval == 0) {
227
228 $tmp_result = db_query($link,
229 "SELECT value FROM ttrss_user_prefs WHERE
230 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
231
232 if (db_num_rows($tmp_result) != 0) {
233 $purge_interval = db_fetch_result($tmp_result, 0, "value");
234 }
235 }
236
237 if ($do_output) {
ad507f85 238// print "Feed $feed_id: purge interval = $purge_interval\n";
fefa6ca3
AD
239 }
240
241 if ($purge_interval > 0) {
ad507f85 242 purge_feed($link, $feed_id, $purge_interval, $do_output);
fefa6ca3
AD
243 }
244 }
245
71604ca4 246 // purge orphaned posts in main content table
dab52d7b 247 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
71604ca4
AD
248 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
249
dab52d7b
AD
250 if ($do_output) {
251 $rows = db_affected_rows($link, $result);
252 _debug("Purged $rows orphaned posts.");
253 }
254
9632f884 255 } // function global_purge_old_posts
fefa6ca3 256
07d0efe9
AD
257 function feed_purge_interval($link, $feed_id) {
258
259 $result = db_query($link, "SELECT purge_interval, owner_uid FROM ttrss_feeds
260 WHERE id = '$feed_id'");
261
262 if (db_num_rows($result) == 1) {
263 $purge_interval = db_fetch_result($result, 0, "purge_interval");
264 $owner_uid = db_fetch_result($result, 0, "owner_uid");
265
266 if ($purge_interval == 0) $purge_interval = get_pref($link,
267 'PURGE_OLD_DAYS', $user_id);
268
269 return $purge_interval;
270
271 } else {
272 return -1;
273 }
274 }
275
b6eefba5 276 function purge_old_posts($link) {
5d73494a 277
f1a80dae
AD
278 $user_id = $_SESSION["uid"];
279
280 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
281 WHERE owner_uid = '$user_id'");
5d73494a
AD
282
283 while ($line = db_fetch_assoc($result)) {
284
285 $feed_id = $line["id"];
286 $purge_interval = $line["purge_interval"];
287
b619ff15 288 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
5d73494a 289
140aae81 290 if ($purge_interval > 0) {
fefa6ca3 291 purge_feed($link, $feed_id, $purge_interval);
5d73494a
AD
292 }
293 }
71604ca4
AD
294
295 // purge orphaned posts in main content table
296 db_query($link, "DELETE FROM ttrss_entries WHERE
297 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
c3a8d71a
AD
298 }
299
c7d57b66
AD
300 function get_feed_update_interval($link, $feed_id) {
301 $result = db_query($link, "SELECT owner_uid, update_interval FROM
302 ttrss_feeds WHERE id = '$feed_id'");
303
304 if (db_num_rows($result) == 1) {
305 $update_interval = db_fetch_result($result, 0, "update_interval");
306 $owner_uid = db_fetch_result($result, 0, "owner_uid");
307
308 if ($update_interval != 0) {
309 return $update_interval;
310 } else {
311 return get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $owner_uid, false);
312 }
313
314 } else {
315 return -1;
316 }
317 }
318
1f2b01ed 319 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
40d13c28 320
4769ddaf 321 if (WEB_DEMO_MODE) return;
b0b4abcf 322
a2770077
AD
323 if (!$user_id) {
324 $user_id = $_SESSION["uid"];
325 purge_old_posts($link);
326 }
327
25af8dad 328// db_query($link, "BEGIN");
b82af8c3 329
cbd8650d
AD
330 if (MAX_UPDATE_TIME > 0) {
331 if (DB_TYPE == "mysql") {
332 $q_order = "RAND()";
333 } else {
334 $q_order = "RANDOM()";
335 }
336 } else {
337 $q_order = "last_updated DESC";
338 }
339
d148926e 340 $result = db_query($link, "SELECT feed_url,id,
fc2b26a6 341 ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated,
5c563acd 342 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
cbd8650d
AD
343 ORDER BY $q_order");
344
345 $upd_start = time();
40d13c28 346
b6eefba5 347 while ($line = db_fetch_assoc($result)) {
d148926e
AD
348 $upd_intl = $line["update_interval"];
349
b619ff15 350 if (!$upd_intl || $upd_intl == 0) {
e289ca71 351 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
b619ff15 352 }
d148926e 353
c1e202b7
AD
354 if ($upd_intl < 0) {
355 // Updates for this feed are disabled
356 continue;
357 }
358
93d40f50
AD
359 if ($fetch || (!$line["last_updated"] ||
360 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
c5142cca 361
cbd8650d
AD
362// print "<!-- feed: ".$line["feed_url"]." -->";
363
1f2b01ed 364 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
cbd8650d
AD
365
366 $upd_elapsed = time() - $upd_start;
367
368 if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) {
369 return;
370 }
d148926e 371 }
40d13c28
AD
372 }
373
25af8dad 374// db_query($link, "COMMIT");
b82af8c3 375
40d13c28
AD
376 }
377
4065b60b
AD
378 function fetch_file_contents($url) {
379 if (USE_CURL_FOR_ICONS) {
380 $tmpfile = tempnam(TMP_DIRECTORY, "ttrss-tmp");
381
382 $ch = curl_init($url);
383 $fp = fopen($tmpfile, "w");
384
385 if ($fp) {
386 curl_setopt($ch, CURLOPT_FILE, $fp);
dd966fed
AD
387 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
388 curl_setopt($ch, CURLOPT_TIMEOUT, 45);
4065b60b
AD
389 curl_exec($ch);
390 curl_close($ch);
391 fclose($fp);
392 }
393
394 $contents = file_get_contents($tmpfile);
395 unlink($tmpfile);
396
397 return $contents;
398
399 } else {
400 return file_get_contents($url);
401 }
402
403 }
78800912 404
9632f884
AD
405 /**
406 * Try to determine the favicon URL for a feed.
407 * adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
408 * http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
409 *
410 * @param string $url A feed or page URL
411 * @access public
412 * @return mixed The favicon URL, or false if none was found.
413 */
4065b60b 414 function get_favicon_url($url) {
99331724 415
4065b60b 416 if ($html = @fetch_file_contents($url)) {
78800912 417
4065b60b
AD
418 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
419 // Attempt to grab a favicon link from their webpage url
420 $linkUrl = html_entity_decode($matches[1]);
c798704b 421
4065b60b
AD
422 if (substr($linkUrl, 0, 1) == '/') {
423 $urlParts = parse_url($url);
424 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
425 } else if (substr($linkUrl, 0, 7) == 'http://') {
426 $faviconURL = $linkUrl;
427 } else if (substr($url, -1, 1) == '/') {
428 $faviconURL = $url.$linkUrl;
429 } else {
430 $faviconURL = $url.'/'.$linkUrl;
e695fdc8 431 }
717f5e64 432
c798704b 433 } else {
4065b60b
AD
434 // If unsuccessful, attempt to "guess" the favicon location
435 $urlParts = parse_url($url);
436 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
437 }
438 }
c798704b 439
4065b60b
AD
440 // Run a test to see if what we have attempted to get actually exists.
441 if(USE_CURL_FOR_ICONS || url_validate($faviconURL)) {
442 return $faviconURL;
443 } else {
444 return false;
445 }
9632f884 446 } // function get_favicon_url
4065b60b 447
9632f884
AD
448 /**
449 * Check if a link is a valid and working URL.
450 *
451 * @param mixed $link A URL to check
452 * @access public
453 * @return boolean True if the URL is valid, false otherwise.
454 */
4065b60b
AD
455 function url_validate($link) {
456
457 $url_parts = @parse_url($link);
458
459 if ( empty( $url_parts["host"] ) )
460 return false;
461
462 if ( !empty( $url_parts["path"] ) ) {
463 $documentpath = $url_parts["path"];
464 } else {
465 $documentpath = "/";
466 }
467
468 if ( !empty( $url_parts["query"] ) )
469 $documentpath .= "?" . $url_parts["query"];
470
471 $host = $url_parts["host"];
472 $port = $url_parts["port"];
473
474 if ( empty($port) )
475 $port = "80";
476
477 $socket = @fsockopen( $host, $port, $errno, $errstr, 30 );
478
479 if ( !$socket )
480 return false;
c798704b 481
4065b60b
AD
482 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
483
484 $http_response = fgets( $socket, 22 );
485
486 $responses = "/(200 OK)|(30[0-9] Moved)/";
487 if ( preg_match($responses, $http_response) ) {
488 fclose($socket);
489 return true;
490 } else {
491 return false;
492 }
493
9632f884 494 } // function url_validate
4065b60b
AD
495
496 function check_feed_favicon($site_url, $feed, $link) {
497 $favicon_url = get_favicon_url($site_url);
498
499# print "FAVICON [$site_url]: $favicon_url\n";
500
501 error_reporting(0);
502
503 $icon_file = ICONS_DIR . "/$feed.ico";
504
505 if ($favicon_url && !file_exists($icon_file)) {
506 $contents = fetch_file_contents($favicon_url);
507
508 $fp = fopen($icon_file, "w");
78800912 509
4065b60b
AD
510 if ($fp) {
511 fwrite($fp, $contents);
512 fclose($fp);
513 chmod($icon_file, 0644);
514 }
78800912 515 }
4065b60b
AD
516
517 error_reporting(DEFAULT_ERROR_LEVEL);
518
78800912
AD
519 }
520
ddb68b81 521 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
40d13c28 522
17c0eeba 523 if (!$_GET["daemon"] && !$ignore_daemon) {
45004d43 524 return false;
21cfcdf2
AD
525 }
526
4bc64807 527 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb 528 _debug("update_rss_feed: start");
219bd8fc
AD
529 }
530
39a52499
AD
531 if (!$ignore_daemon) {
532
533 if (DB_TYPE == "pgsql") {
534 $updstart_thresh_qpart = "(ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')";
535 } else {
536 $updstart_thresh_qpart = "(ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))";
537 }
538
539 $result = db_query($link, "SELECT id,update_interval,auth_login,
16211ddb 540 auth_pass,cache_images,update_method
39a52499 541 FROM ttrss_feeds WHERE id = '$feed' AND $updstart_thresh_qpart");
02008cb1 542
39a52499
AD
543 } else {
544
545 $result = db_query($link, "SELECT id,update_interval,auth_login,
16211ddb 546 auth_pass,cache_images,update_method
39a52499
AD
547 FROM ttrss_feeds WHERE id = '$feed'");
548
549 }
a88c1f36 550
5370d37f
AD
551 if (db_num_rows($result) == 0) {
552 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
02008cb1 553 _debug("update_rss_feed: feed $feed [$feed_url] NOT FOUND/SKIPPED");
5370d37f 554 }
45004d43 555 return false;
5370d37f
AD
556 }
557
16211ddb
AD
558 $update_method = db_fetch_result($result, 0, "update_method");
559
3c50da83
AD
560 db_query($link, "UPDATE ttrss_feeds SET last_update_started = NOW()
561 WHERE id = '$feed'");
562
464bd61e
AD
563 $auth_login = db_fetch_result($result, 0, "auth_login");
564 $auth_pass = db_fetch_result($result, 0, "auth_pass");
565
34459667
AD
566 if (ALLOW_SELECT_UPDATE_METHOD) {
567 if (ENABLE_SIMPLEPIE) {
568 $use_simplepie = $update_method != 1;
569 } else {
570 $use_simplepie = $update_method == 2;
571 }
16211ddb 572 } else {
34459667 573 $use_simplepie = ENABLE_SIMPLEPIE;
16211ddb
AD
574 }
575
576 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
577 _debug("use simplepie: $use_simplepie (feed setting: $update_method)\n");
578 }
579
580 if (!$use_simplepie) {
464bd61e
AD
581 $auth_login = urlencode($auth_login);
582 $auth_pass = urlencode($auth_pass);
583 }
47c6c988 584
a88c1f36 585 $update_interval = db_fetch_result($result, 0, "update_interval");
bc0f0785 586 $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images"));
a88c1f36
AD
587
588 if ($update_interval < 0) { return; }
589
ab3d0b99
AD
590 $feed = db_escape_string($feed);
591
47c6c988
AD
592 $fetch_url = $feed_url;
593
594 if ($auth_login && $auth_pass) {
595 $url_parts = array();
596 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
597
598 if ($url_parts[1] && $url_parts[2]) {
599 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
600 }
601
602 }
ab3d0b99 603
4bc64807 604 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
ca872b9d 605 _debug("update_rss_feed: fetching [$fetch_url]...");
219bd8fc
AD
606 }
607
9fdf7824 608 if (!defined('DAEMON_EXTENDED_DEBUG') && !$_GET['xdebug']) {
219bd8fc
AD
609 error_reporting(0);
610 }
611
16211ddb 612 if (!$use_simplepie) {
9fdf7824
AD
613 $rss = fetch_rss($fetch_url);
614 } else {
c7d57b66
AD
615 if (!is_dir(SIMPLEPIE_CACHE_DIR)) {
616 mkdir(SIMPLEPIE_CACHE_DIR);
617 }
618
ca872b9d
AD
619 $rss = new SimplePie();
620 $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT);
16211ddb 621# $rss->set_timeout(10);
ca872b9d
AD
622 $rss->set_feed_url($fetch_url);
623 $rss->set_output_encoding('UTF-8');
c7d57b66 624
bc0f0785
AD
625 if (SIMPLEPIE_CACHE_IMAGES && $cache_images) {
626 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
627 _debug("enabling image cache");
628 }
629
dab52d7b 630 $rss->set_image_handler('./image.php', 'i');
bc0f0785 631 }
dab52d7b 632
c7d57b66
AD
633 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
634 _debug("feed update interval (sec): " .
635 get_feed_update_interval($link, $feed)*60);
636 }
637
638 if (is_dir(SIMPLEPIE_CACHE_DIR)) {
639 $rss->set_cache_location(SIMPLEPIE_CACHE_DIR);
640 $rss->set_cache_duration(get_feed_update_interval($link, $feed) * 60);
641 }
642
9fdf7824 643 $rss->init();
9fdf7824
AD
644 }
645
646// print_r($rss);
219bd8fc 647
4bc64807 648 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb 649 _debug("update_rss_feed: fetch done, parsing...");
219bd8fc
AD
650 } else {
651 error_reporting (DEFAULT_ERROR_LEVEL);
652 }
653
b6eefba5 654 $feed = db_escape_string($feed);
dcee8f61 655
16211ddb 656 if ($use_simplepie) {
ca872b9d
AD
657 $fetch_ok = !$rss->error();
658 } else {
659 $fetch_ok = !!$rss;
660 }
661
662 if ($fetch_ok) {
50b62214 663
4bc64807 664 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
50b62214
AD
665 _debug("update_rss_feed: processing feed data...");
666 }
667
44e241cb 668// db_query($link, "BEGIN");
dd8c76a9 669
a88c1f36 670 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
f324892e 671 FROM ttrss_feeds WHERE id = '$feed'");
331900c6 672
b6eefba5
AD
673 $registered_title = db_fetch_result($result, 0, "title");
674 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
f324892e 675 $orig_site_url = db_fetch_result($result, 0, "site_url");
331900c6 676
7fed1940
AD
677 $owner_uid = db_fetch_result($result, 0, "owner_uid");
678
16211ddb 679 if ($use_simplepie) {
9fdf7824
AD
680 $site_url = $rss->get_link();
681 } else {
682 $site_url = $rss->channel["link"];
683 }
684
8d0ec6fd 685 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
4bc64807 686 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
50b62214
AD
687 _debug("update_rss_feed: checking favicon...");
688 }
9fdf7824
AD
689
690 check_feed_favicon($site_url, $feed, $link);
a2770077
AD
691 }
692
746b249f 693 if (!$registered_title || $registered_title == "[Unknown]") {
4bc64807 694
16211ddb 695 if ($use_simplepie) {
c2f8aac4 696 $feed_title = db_escape_string($rss->get_title());
fb486a33
AD
697 } else {
698 $feed_title = db_escape_string($rss->channel["title"]);
699 }
4bc64807
AD
700
701 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
702 _debug("update_rss_feed: registering title: $feed_title");
703 }
7c5a308d 704
f324892e
AD
705 db_query($link, "UPDATE ttrss_feeds SET
706 title = '$feed_title' WHERE id = '$feed'");
707 }
708
49f9c923 709 // weird, weird Magpie
16211ddb 710 if (!$use_simplepie) {
9fdf7824
AD
711 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
712 }
147f7691
AD
713
714 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
f324892e
AD
715 db_query($link, "UPDATE ttrss_feeds SET
716 site_url = '$site_url' WHERE id = '$feed'");
331900c6 717 }
40d13c28 718
b7f4bda2
AD
719// print "I: " . $rss->channel["image"]["url"];
720
16211ddb 721 if (!$use_simplepie) {
9fdf7824
AD
722 $icon_url = $rss->image["url"];
723 } else {
724 $icon_url = $rss->get_image_url();
725 }
b7f4bda2 726
147f7691 727 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
b6eefba5
AD
728 $icon_url = db_escape_string($icon_url);
729 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
b7f4bda2
AD
730 }
731
51e456d6 732 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
50b62214
AD
733 _debug("update_rss_feed: loading filters...");
734 }
e6155a06 735
5daa24f2 736 $filters = load_filters($link, $feed, $owner_uid);
e6155a06 737
16211ddb 738 if ($use_simplepie) {
9fdf7824
AD
739 $iterator = $rss->get_items();
740 } else {
741 $iterator = $rss->items;
742 if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
743 if (!$iterator || !is_array($iterator)) $iterator = $rss;
744 }
c22789da
AD
745
746 if (!is_array($iterator)) {
39541e74 747 /* db_query($link, "UPDATE ttrss_feeds
75bd0669 748 SET last_error = 'Parse error: can\'t find any articles.'
77f0a2a7
AD
749 WHERE id = '$feed'"); */
750
751 // clear any errors and mark feed as updated if fetched okay
752 // even if it's blank
753
51e456d6 754 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
844012bc
AD
755 _debug("update_rss_feed: entry iterator is not an array, no articles?");
756 }
757
77f0a2a7
AD
758 db_query($link, "UPDATE ttrss_feeds
759 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
760
761 return; // no articles
c22789da 762 }
ddb68b81 763
51e456d6 764 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
50b62214
AD
765 _debug("update_rss_feed: processing articles...");
766 }
767
ddb68b81 768 foreach ($iterator as $item) {
7c5a308d 769
40ce98f4
AD
770 if ($_GET['xdebug']) {
771 print_r($item);
ea322415 772
40ce98f4 773 }
71bd29f6 774
16211ddb 775 if ($use_simplepie) {
9fdf7824
AD
776 $entry_guid = $item->get_id();
777 if (!$entry_guid) $entry_guid = $item->get_link();
778 if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title());
779
780 } else {
781
782 $entry_guid = $item["id"];
34e420fb 783
9fdf7824
AD
784 if (!$entry_guid) $entry_guid = $item["guid"];
785 if (!$entry_guid) $entry_guid = $item["link"];
786 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
787 }
be832a1a 788
9fdf7824 789 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb
AD
790 _debug("update_rss_feed: guid $entry_guid");
791 }
792
be832a1a
AD
793 if (!$entry_guid) continue;
794
795 $entry_timestamp = "";
796
16211ddb 797 if ($use_simplepie) {
9fdf7824
AD
798 $entry_timestamp = strtotime($item->get_date());
799 } else {
800 $rss_2_date = $item['pubdate'];
801 $rss_1_date = $item['dc']['date'];
802 $atom_date = $item['issued'];
803 if (!$atom_date) $atom_date = $item['updated'];
be832a1a 804
9fdf7824
AD
805 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
806 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
807 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
808 }
9bb36aa0
AD
809
810 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
811 _debug("update_rss_feed: date $entry_timestamp");
812 }
813
2e930846 814 if ($entry_timestamp == "" || $entry_timestamp == -1 || !$entry_timestamp) {
be832a1a
AD
815 $entry_timestamp = time();
816 $no_orig_date = 'true';
817 } else {
818 $no_orig_date = 'false';
819 }
820
821 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
822
16211ddb 823 if ($use_simplepie) {
9fdf7824
AD
824 $entry_title = $item->get_title();
825 } else {
826 $entry_title = trim(strip_tags($item["title"]));
827 }
be832a1a 828
16211ddb 829 if ($use_simplepie) {
9fdf7824
AD
830 $entry_link = $item->get_link();
831 } else {
832 // strange Magpie workaround
833 $entry_link = $item["link_"];
834 if (!$entry_link) $entry_link = $item["link"];
835 }
be832a1a 836
9bb36aa0
AD
837 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
838 _debug("update_rss_feed: title $entry_title");
839 }
840
841 if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);;
7d3ab0dd 842
be832a1a 843 $entry_link = strip_tags($entry_link);
7d3ab0dd 844
16211ddb 845 if ($use_simplepie) {
4af7a36a
AD
846 $entry_content = $item->get_content();
847 if (!$entry_content) $entry_content = $item->get_description();
9fdf7824
AD
848 } else {
849 $entry_content = $item["content:escaped"];
850
851 if (!$entry_content) $entry_content = $item["content:encoded"];
e91ab107 852 if (!$entry_content) $entry_content = $item["content"]["encoded"];
9fdf7824 853 if (!$entry_content) $entry_content = $item["content"];
ea322415
AD
854
855 // Magpie bugs are getting ridiculous
856 if (trim($entry_content) == "Array") $entry_content = false;
857
9fdf7824
AD
858 if (!$entry_content) $entry_content = $item["atom_content"];
859 if (!$entry_content) $entry_content = $item["summary"];
e91ab107
AD
860
861 if (!$entry_content ||
862 strlen($entry_content) < strlen($item["description"])) {
863 $entry_content = $item["description"];
864 };
9fdf7824
AD
865
866 // WTF
867 if (is_array($entry_content)) {
868 $entry_content = $entry_content["encoded"];
869 if (!$entry_content) $entry_content = $entry_content["escaped"];
ea322415 870 }
be832a1a
AD
871 }
872
ea322415
AD
873 if ($_GET["xdebug"]) {
874 print "update_rss_feed: content: ";
875 print_r(htmlspecialchars($entry_content));
876 }
be832a1a
AD
877
878 $entry_content_unescaped = $entry_content;
be832a1a 879
16211ddb 880 if ($use_simplepie) {
9fdf7824 881 $entry_comments = strip_tags($item->data["comments"]);
30cf38dd 882 if ($item->get_author()) {
e1d600f0 883 $entry_author_item = $item->get_author();
a0c6eafb
AD
884 $entry_author = $entry_author_item->get_name();
885 if (!$entry_author) $entry_author = $entry_author_item->get_email();
d1ee9106
AD
886
887 $entry_author = db_escape_string($entry_author);
30cf38dd 888 }
9fdf7824
AD
889 } else {
890 $entry_comments = strip_tags($item["comments"]);
891
892 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
4d6e9157 893
9fdf7824
AD
894 if ($item['author']) {
895
896 if (is_array($item['author'])) {
897
898 if (!$entry_author) {
899 $entry_author = db_escape_string(strip_tags($item['author']['name']));
900 }
901
902 if (!$entry_author) {
903 $entry_author = db_escape_string(strip_tags($item['author']['email']));
904 }
4d6e9157 905 }
9fdf7824 906
4d6e9157 907 if (!$entry_author) {
9fdf7824 908 $entry_author = db_escape_string(strip_tags($item['author']));
4d6e9157 909 }
83f114c8 910 }
be832a1a
AD
911 }
912
83f114c8
AD
913 if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
914
be832a1a 915 $entry_guid = db_escape_string(strip_tags($entry_guid));
2ad9ee56 916 $entry_guid = mb_substr($entry_guid, 0, 250);
be832a1a
AD
917
918 $result = db_query($link, "SELECT id FROM ttrss_entries
919 WHERE guid = '$entry_guid'");
920
921 $entry_content = db_escape_string($entry_content);
7e43ad58
AD
922
923 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
924
be832a1a
AD
925 $entry_title = db_escape_string($entry_title);
926 $entry_link = db_escape_string($entry_link);
2544f36b
AD
927 $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250);
928 $entry_author = mb_substr($entry_author, 0, 250);
be832a1a 929
16211ddb 930 if ($use_simplepie) {
9fdf7824
AD
931 $num_comments = 0; #FIXME#
932 } else {
933 $num_comments = db_escape_string($item["slash"]["comments"]);
934 }
be832a1a
AD
935
936 if (!$num_comments) $num_comments = 0;
937
fefef828 938 // parse <category> entries into tags
be832a1a 939
16211ddb 940 if ($use_simplepie) {
be832a1a 941
fefef828 942 $additional_tags = array();
9fdf7824 943 $additional_tags_src = $item->get_categories();
3b9e5af4 944
a702e931
AD
945 if (is_array($additional_tags_src)) {
946 foreach ($additional_tags_src as $tobj) {
947 array_push($additional_tags, $tobj->get_term());
948 }
fefef828 949 }
fefef828 950
6af621c7
AD
951 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
952 _debug("update_rss_feed: category tags:");
953 print_r($additional_tags);
954 }
955
9fdf7824 956 } else {
fefef828 957
9fdf7824 958 $t_ctr = $item['category#'];
fefef828 959
9fdf7824
AD
960 $additional_tags = false;
961
962 if ($t_ctr == 0) {
963 $additional_tags = false;
71bd29f6 964 } else if ($t_ctr > 0) {
9fdf7824 965 $additional_tags = array($item['category']);
71bd29f6
AD
966
967 if ($item['category@term']) {
968 array_push($additional_tags, $item['category@term']);
969 }
970
9fdf7824
AD
971 for ($i = 0; $i <= $t_ctr; $i++ ) {
972 if ($item["category#$i"]) {
973 array_push($additional_tags, $item["category#$i"]);
974 }
71bd29f6
AD
975
976 if ($item["category#$i@term"]) {
977 array_push($additional_tags, $item["category#$i@term"]);
978 }
9fdf7824
AD
979 }
980 }
981
982 // parse <dc:subject> elements
983
984 $t_ctr = $item['dc']['subject#'];
985
71bd29f6 986 if ($t_ctr > 0) {
9fdf7824 987 $additional_tags = array($item['dc']['subject']);
71bd29f6 988
9fdf7824
AD
989 for ($i = 0; $i <= $t_ctr; $i++ ) {
990 if ($item['dc']["subject#$i"]) {
991 array_push($additional_tags, $item['dc']["subject#$i"]);
992 }
fefef828
AD
993 }
994 }
995 }
8add756a 996
ce53e200
AD
997 // enclosures
998
999 $enclosures = array();
1000
16211ddb 1001 if ($use_simplepie) {
ce53e200
AD
1002 $encs = $item->get_enclosures();
1003
3b9e5af4
AD
1004 if (is_array($encs)) {
1005 foreach ($encs as $e) {
1006 $e_item = array(
1007 $e->link, $e->type, $e->length);
1008
1009 array_push($enclosures, $e_item);
1010 }
ce53e200
AD
1011 }
1012
1013 } else {
e91ab107
AD
1014 // <enclosure>
1015
ce53e200
AD
1016 $e_ctr = $item['enclosure#'];
1017
1018 if ($e_ctr > 0) {
1019 $e_item = array($item['enclosure@url'],
1020 $item['enclosure@type'],
1021 $item['enclosure@length']);
1022
1023 array_push($enclosures, $e_item);
1024
71bd29f6
AD
1025 for ($i = 0; $i <= $e_ctr; $i++ ) {
1026
1027 if ($item["enclosure#$i@url"]) {
1028 $e_item = array($item["enclosure#$i@url"],
1029 $item["enclosure#$i@type"],
1030 $item["enclosure#$i@length"]);
1031 array_push($enclosures, $e_item);
1032 }
ce53e200
AD
1033 }
1034 }
1035
e91ab107 1036 // <media:content>
b652fdae 1037 // can there be many of those? yes -fox
e91ab107
AD
1038
1039 $m_ctr = $item['media']['content#'];
1040
1041 if ($m_ctr > 0) {
1042 $e_item = array($item['media']['content@url'],
1043 $item['media']['content@medium'],
1044 $item['media']['content@length']);
1045
1046 array_push($enclosures, $e_item);
e91ab107 1047
b652fdae
AD
1048 for ($i = 0; $i <= $m_ctr; $i++ ) {
1049
1050 if ($item["media"]["content#$i@url"]) {
1051 $e_item = array($item["media"]["content#$i@url"],
1052 $item["media"]["content#$i@medium"],
1053 $item["media"]["content#$i@length"]);
1054 array_push($enclosures, $e_item);
1055 }
1056 }
1057
1058 }
ce53e200
AD
1059 }
1060
d48d160c 1061 # sanitize content
183ad07b 1062
621ffb00
AD
1063 $entry_content = sanitize_article_content($entry_content);
1064 $entry_title = sanitize_article_content($entry_title);
d48d160c 1065
9fdf7824 1066 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb
AD
1067 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
1068 }
1069
44e241cb
AD
1070 db_query($link, "BEGIN");
1071
4c193675
AD
1072 if (db_num_rows($result) == 0) {
1073
9fdf7824 1074 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb
AD
1075 _debug("update_rss_feed: base guid not found");
1076 }
1077
4c193675
AD
1078 // base post entry does not exist, create it
1079
4c193675
AD
1080 $result = db_query($link,
1081 "INSERT INTO ttrss_entries
1082 (title,
1083 guid,
1084 link,
1085 updated,
1086 content,
1087 content_hash,
1088 no_orig_date,
1089 date_entered,
11b0dce2 1090 comments,
b6104dee
AD
1091 num_comments,
1092 author)
4c193675
AD
1093 VALUES
1094 ('$entry_title',
1095 '$entry_guid',
1096 '$entry_link',
1097 '$entry_timestamp_fmt',
1098 '$entry_content',
1099 '$content_hash',
1100 $no_orig_date,
1101 NOW(),
11b0dce2 1102 '$entry_comments',
b6104dee
AD
1103 '$num_comments',
1104 '$entry_author')");
8926aab8
AD
1105 } else {
1106 // we keep encountering the entry in feeds, so we need to
1107 // update date_entered column so that we don't get horrible
1108 // dupes when the entry gets purged and reinserted again e.g.
1109 // in the case of SLOW SLOW OMG SLOW updating feeds
1110
1111 $base_entry_id = db_fetch_result($result, 0, "id");
1112
1113 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
1114 WHERE id = '$base_entry_id'");
4c193675
AD
1115 }
1116
1117 // now it should exist, if not - bad luck then
1118
6385315d
AD
1119 $result = db_query($link, "SELECT
1120 id,content_hash,no_orig_date,title,
2ac6b765
AD
1121 ".SUBSTRING_FOR_DATE."(date_entered,1,19) as date_entered,
1122 ".SUBSTRING_FOR_DATE."(updated,1,19) as updated,
11b0dce2 1123 num_comments
6385315d
AD
1124 FROM
1125 ttrss_entries
1126 WHERE guid = '$entry_guid'");
4c193675 1127
ce53e200
AD
1128 $entry_ref_id = 0;
1129 $entry_int_id = 0;
1130
4c193675
AD
1131 if (db_num_rows($result) == 1) {
1132
9fdf7824 1133 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
7ca91eb3 1134 _debug("update_rss_feed: base guid found, checking for user record");
34e420fb
AD
1135 }
1136
11b0dce2
AD
1137 // this will be used below in update handler
1138 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
1139 $orig_title = db_fetch_result($result, 0, "title");
1140 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
8926aab8
AD
1141 $orig_date_entered = strtotime(db_fetch_result($result,
1142 0, "date_entered"));
6385315d 1143
11b0dce2 1144 $ref_id = db_fetch_result($result, 0, "id");
ce53e200 1145 $entry_ref_id = $ref_id;
4c193675 1146
11b0dce2 1147 // check for user post link to main table
4c193675 1148
11b0dce2 1149 // do we allow duplicate posts with same GUID in different feeds?
8d0ec6fd 1150 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
11b0dce2
AD
1151 $dupcheck_qpart = "AND feed_id = '$feed'";
1152 } else {
1153 $dupcheck_qpart = "";
1154 }
71604ca4 1155
11b0dce2 1156// error_reporting(0);
19c9cb11 1157
f8382011
AD
1158 $article_filters = get_article_filters($filters, $entry_title,
1159 $entry_content, $entry_link);
19c9cb11 1160
9fdf7824 1161 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
7ca91eb3
AD
1162 _debug("update_rss_feed: article filters: ");
1163 if (count($article_filters) != 0) {
1164 print_r($article_filters);
1165 }
1166 }
1167
f8382011 1168 if (find_article_filter($article_filters, "filter")) {
ee4a9812 1169 db_query($link, "COMMIT"); // close transaction in progress
11b0dce2
AD
1170 continue;
1171 }
19c9cb11 1172
11b0dce2 1173// error_reporting (DEFAULT_ERROR_LEVEL);
3a933f22 1174
ff6e357a
AD
1175 $score = calculate_article_score($article_filters);
1176
1177 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1178 _debug("update_rss_feed: initial score: $score");
1179 }
1180
11b0dce2 1181 $result = db_query($link,
ce53e200 1182 "SELECT ref_id, int_id FROM ttrss_user_entries WHERE
11b0dce2
AD
1183 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
1184 $dupcheck_qpart");
7ca91eb3 1185
11b0dce2
AD
1186 // okay it doesn't exist - create user entry
1187 if (db_num_rows($result) == 0) {
1188
9fdf7824 1189 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
7ca91eb3
AD
1190 _debug("update_rss_feed: user record not found, creating...");
1191 }
1192
24605713 1193 if ($score >= -500 && !find_article_filter($article_filters, 'catchup')) {
11b0dce2
AD
1194 $unread = 'true';
1195 $last_read_qpart = 'NULL';
1196 } else {
1197 $unread = 'false';
1198 $last_read_qpart = 'NOW()';
1199 }
dd7d3187 1200
32d59314 1201 if (find_article_filter($article_filters, 'mark') || $score > 1000) {
dd7d3187
AD
1202 $marked = 'true';
1203 } else {
1204 $marked = 'false';
1205 }
a36c0dfe
AD
1206
1207 if (find_article_filter($article_filters, 'publish')) {
1208 $published = 'true';
1209 } else {
1210 $published = 'false';
1211 }
1212
11b0dce2
AD
1213 $result = db_query($link,
1214 "INSERT INTO ttrss_user_entries
ff6e357a
AD
1215 (ref_id, owner_uid, feed_id, unread, last_read, marked,
1216 published, score)
11b0dce2 1217 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
ff6e357a 1218 $last_read_qpart, $marked, $published, '$score')");
ce53e200
AD
1219
1220 $result = db_query($link,
1221 "SELECT int_id FROM ttrss_user_entries WHERE
1222 ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND
1223 feed_id = '$feed' LIMIT 1");
1224
1225 if (db_num_rows($result) == 1) {
1226 $entry_int_id = db_fetch_result($result, 0, "int_id");
1227 }
1228 } else {
1229 $entry_ref_id = db_fetch_result($result, 0, "ref_id");
1230 $entry_int_id = db_fetch_result($result, 0, "int_id");
11b0dce2 1231 }
ce53e200
AD
1232
1233 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1234 _debug("update_rss_feed: RID: $entry_ref_id, IID: $entry_int_id");
1235 }
1236
6385315d
AD
1237 $post_needs_update = false;
1238
8d0ec6fd 1239 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
6385315d 1240 ($content_hash != $orig_content_hash)) {
7e43ad58 1241// print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->";
6385315d
AD
1242 $post_needs_update = true;
1243 }
1244
7e43ad58 1245 if (db_escape_string($orig_title) != $entry_title) {
6385315d
AD
1246 $post_needs_update = true;
1247 }
1248
11b0dce2
AD
1249 if ($orig_num_comments != $num_comments) {
1250 $post_needs_update = true;
1251 }
1252
6385315d
AD
1253// this doesn't seem to be very reliable
1254//
1255// if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
1256// $post_needs_update = true;
1257// }
1258
1259 // if post needs update, update it and mark all user entries
1c73bc0c 1260 // linking to this post as updated
6385315d
AD
1261 if ($post_needs_update) {
1262
7ca91eb3
AD
1263 if (defined('DAEMON_EXTENDED_DEBUG')) {
1264 _debug("update_rss_feed: post $entry_guid needs update...");
1265 }
1266
6385315d
AD
1267// print "<!-- post $orig_title needs update : $post_needs_update -->";
1268
6385315d 1269 db_query($link, "UPDATE ttrss_entries
11b0dce2 1270 SET title = '$entry_title', content = '$entry_content',
7e43ad58 1271 content_hash = '$content_hash',
11b0dce2 1272 num_comments = '$num_comments'
6385315d
AD
1273 WHERE id = '$ref_id'");
1274
8d0ec6fd 1275 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
4919fb42
AD
1276 db_query($link, "UPDATE ttrss_user_entries
1277 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
1278 } else {
1279 db_query($link, "UPDATE ttrss_user_entries
1280 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
1281 }
6385315d
AD
1282
1283 }
4c193675
AD
1284 }
1285
44e241cb
AD
1286 db_query($link, "COMMIT");
1287
ce53e200
AD
1288 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1289 _debug("update_rss_feed: looking for enclosures...");
1290 }
1291
1292 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1293 print_r($enclosures);
1294 }
1295
1296 db_query($link, "BEGIN");
1297
1298 foreach ($enclosures as $enc) {
1299 $enc_url = db_escape_string($enc[0]);
1300 $enc_type = db_escape_string($enc[1]);
1301 $enc_dur = db_escape_string($enc[2]);
1302
1303 $result = db_query($link, "SELECT id FROM ttrss_enclosures
1304 WHERE content_url = '$enc_url' AND post_id = '$entry_ref_id'");
1305
1306 if (db_num_rows($result) == 0) {
1307 db_query($link, "INSERT INTO ttrss_enclosures
1308 (content_url, content_type, title, duration, post_id) VALUES
1309 ('$enc_url', '$enc_type', '', '$enc_dur', '$entry_ref_id')");
1310 }
1311 }
1312
1313 db_query($link, "COMMIT");
1314
9fdf7824 1315 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb
AD
1316 _debug("update_rss_feed: looking for tags...");
1317 }
1318
eb36b4eb 1319 /* taaaags */
40e1a95b 1320 // <a href="..." rel="tag">Xorg</a>, //
eb36b4eb 1321
05732aa0 1322 $entry_tags = null;
eb36b4eb 1323
40e1a95b 1324 preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i",
ee2c3050
AD
1325 $entry_content_unescaped, $entry_tags);
1326
fefef828
AD
1327/* print "<p><br/>$entry_title : $entry_content_unescaped<br>";
1328 print_r($entry_tags);
1329 print "<br/></p>"; */
eb36b4eb
AD
1330
1331 $entry_tags = $entry_tags[1];
1332
073ca0e6
AD
1333 # check for manual tags
1334
f8382011
AD
1335 $tag_filter = find_article_filter($article_filters, "tag");
1336
1337 if ($tag_filter) {
073ca0e6 1338
f8382011 1339 $manual_tags = trim_array(split(",", $tag_filter[1]));
073ca0e6 1340
f8382011 1341 foreach ($manual_tags as $tag) {
be832a1a
AD
1342 if (tag_is_valid($tag)) {
1343 array_push($entry_tags, $tag);
1344 }
1345 }
1346 }
1347
11c9ea1f
AD
1348 $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link,
1349 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8')));
8fc70781 1350
fefef828
AD
1351 if ($additional_tags && is_array($additional_tags)) {
1352 foreach ($additional_tags as $tag) {
156a785d
AD
1353 if (tag_is_valid($tag) &&
1354 array_search($tag, $boring_tags) === FALSE) {
073ca0e6
AD
1355 array_push($entry_tags, $tag);
1356 }
1357 }
fefef828
AD
1358 }
1359
fcc95e24 1360// print "<p>TAGS: "; print_r($entry_tags); print "</p>";
073ca0e6 1361
9fdf7824
AD
1362 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1363 print_r($entry_tags);
1364 }
1365
eb36b4eb
AD
1366 if (count($entry_tags) > 0) {
1367
44e241cb
AD
1368 db_query($link, "BEGIN");
1369
fe99ab12 1370 foreach ($entry_tags as $tag) {
fefef828 1371
14b6c54b 1372 $tag = sanitize_tag($tag);
fefef828 1373 $tag = db_escape_string($tag);
31483fc1 1374
ef063748
AD
1375 if (!tag_is_valid($tag)) continue;
1376
fe99ab12
AD
1377 $result = db_query($link, "SELECT id FROM ttrss_tags
1378 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
1379 owner_uid = '$owner_uid' LIMIT 1");
1380
1381 // print db_fetch_result($result, 0, "id");
1382
1383 if ($result && db_num_rows($result) == 0) {
1384
fe99ab12
AD
1385 db_query($link, "INSERT INTO ttrss_tags
1386 (owner_uid,tag_name,post_int_id)
1387 VALUES ('$owner_uid','$tag', '$entry_int_id')");
1388 }
1389 }
ce53e200 1390
44e241cb 1391 db_query($link, "COMMIT");
05732aa0 1392 }
9fdf7824
AD
1393
1394 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1395 _debug("update_rss_feed: article processed");
1396 }
4c193675 1397 }
40d13c28 1398
ab3d0b99
AD
1399 db_query($link, "UPDATE ttrss_feeds
1400 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
eb36b4eb 1401
44e241cb 1402// db_query($link, "COMMIT");
dd8c76a9 1403
ab3d0b99 1404 } else {
ca872b9d 1405
16211ddb 1406 if ($use_simplepie) {
ca872b9d 1407 $error_msg = mb_substr($rss->error(), 0, 250);
9fdf7824 1408 } else {
ca872b9d 1409 $error_msg = mb_substr(magpie_error(), 0, 250);
9fdf7824 1410 }
ca872b9d
AD
1411
1412 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1413 _debug("update_rss_feed: error fetching feed: $error_msg");
1414 }
1415
1416 $error_msg = db_escape_string($error_msg);
1417
ab3d0b99 1418 db_query($link,
aa5f9f5f
AD
1419 "UPDATE ttrss_feeds SET last_error = '$error_msg',
1420 last_updated = NOW() WHERE id = '$feed'");
40d13c28
AD
1421 }
1422
16211ddb 1423 if ($use_simplepie) {
ac6ebdb3
AD
1424 unset($rss);
1425 }
1426
9fdf7824 1427 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb 1428 _debug("update_rss_feed: done");
219bd8fc
AD
1429 }
1430
40d13c28
AD
1431 }
1432
f175937c 1433 function print_select($id, $default, $values, $attributes = "") {
79f3553b 1434 print "<select name=\"$id\" id=\"$id\" $attributes>";
a0d53889
AD
1435 foreach ($values as $v) {
1436 if ($v == $default)
1437 $sel = " selected";
1438 else
1439 $sel = "";
1440
1441 print "<option$sel>$v</option>";
1442 }
1443 print "</select>";
1444 }
40d13c28 1445
79f3553b
AD
1446 function print_select_hash($id, $default, $values, $attributes = "") {
1447 print "<select name=\"$id\" id='$id' $attributes>";
673d54ca
AD
1448 foreach (array_keys($values) as $v) {
1449 if ($v == $default)
74d5c8fa 1450 $sel = 'selected="selected"';
673d54ca
AD
1451 else
1452 $sel = "";
1453
1454 print "<option $sel value=\"$v\">".$values[$v]."</option>";
1455 }
1456
1457 print "</select>";
1458 }
1459
f8382011 1460 function get_article_filters($filters, $title, $content, $link) {
240054f1 1461 $matches = array();
c2d9322b 1462
240054f1
AD
1463 if ($filters["title"]) {
1464 foreach ($filters["title"] as $filter) {
c2d9322b
AD
1465 $reg_exp = $filter["reg_exp"];
1466 $inverse = $filter["inverse"];
1467 if ((!$inverse && preg_match("/$reg_exp/i", $title)) ||
1468 ($inverse && !preg_match("/$reg_exp/i", $title))) {
1469
240054f1
AD
1470 array_push($matches, array($filter["action"], $filter["action_param"]));
1471 }
1472 }
1473 }
1474
1475 if ($filters["content"]) {
1476 foreach ($filters["content"] as $filter) {
c2d9322b
AD
1477 $reg_exp = $filter["reg_exp"];
1478 $inverse = $filter["inverse"];
1479
1480 if ((!$inverse && preg_match("/$reg_exp/i", $content)) ||
1481 ($inverse && !preg_match("/$reg_exp/i", $content))) {
1482
240054f1
AD
1483 array_push($matches, array($filter["action"], $filter["action_param"]));
1484 }
1485 }
1486 }
1487
1488 if ($filters["both"]) {
1489 foreach ($filters["both"] as $filter) {
1490 $reg_exp = $filter["reg_exp"];
c2d9322b
AD
1491 $inverse = $filter["inverse"];
1492
1493 if ($inverse) {
1494 if (!preg_match("/$reg_exp/i", $title) || !preg_match("/$reg_exp/i", $content)) {
1495 array_push($matches, array($filter["action"], $filter["action_param"]));
1496 }
1497 } else {
1498 if (preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) {
1499 array_push($matches, array($filter["action"], $filter["action_param"]));
1500 }
240054f1
AD
1501 }
1502 }
1503 }
1504
1505 if ($filters["link"]) {
1506 $reg_exp = $filter["reg_exp"];
1507 foreach ($filters["link"] as $filter) {
1508 $reg_exp = $filter["reg_exp"];
c2d9322b
AD
1509 $inverse = $filter["inverse"];
1510
1511 if ((!$inverse && preg_match("/$reg_exp/i", $link)) ||
1512 ($inverse && !preg_match("/$reg_exp/i", $link))) {
1513
240054f1
AD
1514 array_push($matches, array($filter["action"], $filter["action_param"]));
1515 }
1516 }
1517 }
1518
1519 return $matches;
1520 }
1521
f8382011
AD
1522 function find_article_filter($filters, $filter_name) {
1523 foreach ($filters as $f) {
1524 if ($f[0] == $filter_name) {
1525 return $f;
1526 };
1527 }
1528 return false;
1529 }
1530
ff6e357a
AD
1531 function calculate_article_score($filters) {
1532 $score = 0;
1533
1534 foreach ($filters as $f) {
1535 if ($f[0] == "score") {
1536 $score += $f[1];
1537 };
1538 }
1539 return $score;
1540 }
1541
1542
9323147e 1543 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
fb1fb4ab 1544 $rtl_content = false, $last_updated = false, $last_error = false) {
254e0e4b
AD
1545
1546 if (file_exists($icon_file) && filesize($icon_file) > 0) {
023fe037 1547 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
254e0e4b 1548 } else {
023fe037 1549 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
254e0e4b
AD
1550 }
1551
9323147e
AD
1552 if ($rtl_content) {
1553 $rtl_tag = "dir=\"rtl\"";
1554 } else {
1555 $rtl_tag = "dir=\"ltr\"";
1556 }
1557
78d5212c
AD
1558 $error_notify_msg = "";
1559
fb1fb4ab
AD
1560 if ($last_error) {
1561 $link_title = "Error: $last_error ($last_updated)";
78d5212c 1562 $error_notify_msg = "(Error)";
ad780e9c 1563 } else if ($last_updated) {
fb1fb4ab
AD
1564 $link_title = "Updated: $last_updated";
1565 }
1566
7210613a 1567 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
c50e2b30 1568 href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
254e0e4b 1569
8836613c 1570 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
b619ff15 1571 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
254e0e4b
AD
1572 print "$feed_icon";
1573 }
1574
9323147e 1575 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
254e0e4b
AD
1576
1577 if ($unread != 0) {
d7e83df7 1578 $fctr_class = "class=\"feedCtrHasUnread\"";
254e0e4b 1579 } else {
d7e83df7 1580 $fctr_class = "class=\"feedCtrNoUnread\"";
254e0e4b
AD
1581 }
1582
9323147e 1583 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
254e0e4b 1584 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
78d5212c
AD
1585
1586 if (get_pref($link, "EXTENDED_FEEDLIST")) {
bdb7369b 1587 $total = getFeedArticles($link, $feed_id);
78d5212c 1588 print "<div class=\"feedExtInfo\">
bdb7369b 1589 <span id=\"FLUPD-$feed_id\">$last_updated ($total total) $error_notify_msg</span></div>";
78d5212c
AD
1590 }
1591
254e0e4b
AD
1592 print "</li>";
1593
1594 }
1595
406d9489
AD
1596 function getmicrotime() {
1597 list($usec, $sec) = explode(" ",microtime());
1598 return ((float)$usec + (float)$sec);
1599 }
1600
f541eb78 1601 function print_radio($id, $default, $true_is, $values, $attributes = "") {
77e96719
AD
1602 foreach ($values as $v) {
1603
1604 if ($v == $default)
5da169d9 1605 $sel = "checked";
77e96719 1606 else
5da169d9
AD
1607 $sel = "";
1608
f541eb78 1609 if ($v == $true_is) {
5da169d9
AD
1610 $sel .= " value=\"1\"";
1611 } else {
1612 $sel .= " value=\"0\"";
1613 }
77e96719 1614
69654950
AD
1615 print "<input class=\"noborder\"
1616 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
77e96719
AD
1617
1618 }
1619 }
1620
ff485f1d
AD
1621 function initialize_user_prefs($link, $uid) {
1622
1623 $uid = db_escape_string($uid);
1624
1625 db_query($link, "BEGIN");
1626
1627 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
1628
1629 $u_result = db_query($link, "SELECT pref_name
1630 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
1631
1632 $active_prefs = array();
1633
1634 while ($line = db_fetch_assoc($u_result)) {
1635 array_push($active_prefs, $line["pref_name"]);
1636 }
1637
1638 while ($line = db_fetch_assoc($result)) {
1639 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
1640// print "adding " . $line["pref_name"] . "<br>";
1641
1642 db_query($link, "INSERT INTO ttrss_user_prefs
1643 (owner_uid,pref_name,value) VALUES
1644 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
1645
1646 }
1647 }
1648
1649 db_query($link, "COMMIT");
1650
1651 }
956c7629
AD
1652
1653 function lookup_user_id($link, $user) {
1654
1655 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1656 login = '$login'");
1657
1658 if (db_num_rows($result) == 1) {
1659 return db_fetch_result($result, 0, "id");
1660 } else {
1661 return false;
1662 }
1663 }
1664
18664970
AD
1665 function http_authenticate_user($link) {
1666
4bc64807
AD
1667 error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log');
1668
18664970
AD
1669 if (!$_SERVER["PHP_AUTH_USER"]) {
1670
1671 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1672 header('HTTP/1.0 401 Unauthorized');
1673 exit;
1674
1675 } else {
1676 $auth_result = authenticate_user($link,
1677 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1678
1679 if (!$auth_result) {
1680 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1681 header('HTTP/1.0 401 Unauthorized');
1682 exit;
1683 }
1684 }
1685
1686 return true;
1687 }
1688
461766f3 1689 function authenticate_user($link, $login, $password, $force_auth = false) {
c8437f35 1690
131b01b3 1691 if (!SINGLE_USER_MODE) {
c8437f35 1692
1a9f4d3c
AD
1693 $pwd_hash1 = encrypt_password($password);
1694 $pwd_hash2 = encrypt_password($password, $login);
461766f3 1695
66917e70 1696 if (defined('ALLOW_REMOTE_USER_AUTH') && ALLOW_REMOTE_USER_AUTH
73f5f114 1697 && $_SERVER["REMOTE_USER"] && $login != "admin") {
66917e70
AD
1698
1699 $login = db_escape_string($_SERVER["REMOTE_USER"]);
1700
73f5f114 1701 $query = "SELECT id,login,access_level,pwd_hash
461766f3 1702 FROM ttrss_users WHERE
66917e70
AD
1703 login = '$login'";
1704
461766f3 1705 } else {
1a9f4d3c 1706 $query = "SELECT id,login,access_level,pwd_hash
461766f3 1707 FROM ttrss_users WHERE
1a9f4d3c
AD
1708 login = '$login' AND (pwd_hash = '$pwd_hash1' OR
1709 pwd_hash = '$pwd_hash2')";
461766f3
AD
1710 }
1711
1712 $result = db_query($link, $query);
131b01b3
AD
1713
1714 if (db_num_rows($result) == 1) {
1715 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1716 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1717 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1718
1719 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1720 $_SESSION["uid"]);
1721
1722 $user_theme = get_user_theme_path($link);
1723
1724 $_SESSION["theme"] = $user_theme;
1725 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1a9f4d3c 1726 $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash");
131b01b3
AD
1727
1728 initialize_user_prefs($link, $_SESSION["uid"]);
1729
1730 return true;
1731 }
1732
1733 return false;
503eb349 1734
131b01b3 1735 } else {
503eb349 1736
131b01b3
AD
1737 $_SESSION["uid"] = 1;
1738 $_SESSION["name"] = "admin";
f557cd78 1739
0bbba72d
AD
1740 $user_theme = get_user_theme_path($link);
1741
1742 $_SESSION["theme"] = $user_theme;
1743 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1744
1745 initialize_user_prefs($link, $_SESSION["uid"]);
1746
c8437f35
AD
1747 return true;
1748 }
c8437f35
AD
1749 }
1750
e6cb77a0
AD
1751 function make_password($length = 8) {
1752
1753 $password = "";
798f722b
AD
1754 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1755
1756 $i = 0;
e6cb77a0
AD
1757
1758 while ($i < $length) {
1759 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1760
1761 if (!strstr($password, $char)) {
1762 $password .= $char;
1763 $i++;
1764 }
1765 }
1766 return $password;
1767 }
1768
1769 // this is called after user is created to initialize default feeds, labels
1770 // or whatever else
1771
1772 // user preferences are checked on every login, not here
1773
1774 function initialize_user($link, $uid) {
1775
1776 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1777 values ('$uid','unread = true', 'Unread articles')");
1778
1779 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1780 values ('$uid','last_read is null and unread = false', 'Updated articles')");
e603a0fa 1781
e6cb77a0 1782 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 1783 values ('$uid', 'Tiny Tiny RSS: New Releases',
628fcd2c 1784 'http://tt-rss.spb.ru/releases.rss')");
3b0feb9b 1785
cd2cd415
AD
1786 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1787 values ('$uid', 'Tiny Tiny RSS: Forum',
1788 'http://tt-rss.spb.ru/forum/rss.php')");
3b0feb9b 1789 }
e6cb77a0 1790
b8aa49bc 1791 function logout_user() {
5ccc1cf5
AD
1792 session_destroy();
1793 if (isset($_COOKIE[session_name()])) {
1794 setcookie(session_name(), '', time()-42000, '/');
1795 }
b8aa49bc
AD
1796 }
1797
75836f33 1798 function get_script_urlpath() {
87a79fa4 1799 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
75836f33
AD
1800 }
1801
916f788a 1802 function validate_session($link) {
741edab2
AD
1803 if (SINGLE_USER_MODE) {
1804 return true;
1805 }
1806
a2e9b457 1807 if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) {
916f788a
AD
1808 if ($_SESSION["ip_address"]) {
1809 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
7f0acba7 1810 $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)";
916f788a
AD
1811 return false;
1812 }
1813 }
1814 }
d620cfe7 1815
e6684130
AD
1816 if ($_SESSION["uid"]) {
1817
1818 $result = db_query($link,
1819 "SELECT pwd_hash FROM ttrss_users WHERE id = '".$_SESSION["uid"]."'");
1820
1821 $pwd_hash = db_fetch_result($result, 0, "pwd_hash");
1822
1823 if ($pwd_hash != $_SESSION["pwd_hash"]) {
1824 return false;
1825 }
1826 }
1827
a885f0ec 1828/* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
d620cfe7 1829
8e849206 1830 //print_r($_SESSION);
d620cfe7
AD
1831
1832 if (time() > $_SESSION["cookie_lifetime"]) {
1833 return false;
1834 }
a885f0ec
AD
1835 } */
1836
916f788a
AD
1837 return true;
1838 }
1839
793185a9 1840 function login_sequence($link, $mobile = false) {
b8aa49bc 1841 if (!SINGLE_USER_MODE) {
75836f33 1842
461766f3
AD
1843 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1844 $swu = db_escape_string($_REQUEST["swu"]);
1845 if ($swu) {
1846 $_SESSION["prefs_cache"] = false;
1847 return authenticate_user($link, $swu, null, true);
1848 }
1849 }
1850
7f0acba7 1851 $login_action = $_POST["login_action"];
a885f0ec 1852
01a87dff 1853 # try to authenticate user if called from login form
7f0acba7 1854 if ($login_action == "do_login") {
01a87dff
AD
1855 $login = $_POST["login"];
1856 $password = $_POST["password"];
d620cfe7 1857 $remember_me = $_POST["remember_me"];
f557cd78 1858
01a87dff
AD
1859 if (authenticate_user($link, $login, $password)) {
1860 $_POST["password"] = "";
d620cfe7 1861
f8c612d4 1862 $_SESSION["language"] = $_POST["language"];
a598370d 1863 $_SESSION["bw_limit"] = !!$_POST["bw_limit"];
f8c612d4 1864
d620cfe7
AD
1865 header("Location: " . $_SERVER["REQUEST_URI"]);
1866 exit;
1867
01a87dff 1868 return;
7f0acba7
AD
1869 } else {
1870 $_SESSION["login_error_msg"] = "Incorrect username or password";
01a87dff
AD
1871 }
1872 }
1873
1df0f48b
AD
1874// print session_id();
1875// print_r($_SESSION);
7f0acba7
AD
1876
1877 if (!$_SESSION["uid"] || !validate_session($link)) {
793185a9 1878 render_login_form($link, $mobile);
01a87dff 1879 exit;
d3687e7a
AD
1880 } else {
1881 /* bump login timestamp */
1882 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1883 $_SESSION["uid"]);
019bd5a9 1884
d54780bc 1885 if ($_SESSION["language"] && SESSION_COOKIE_LIFETIME > 0) {
019bd5a9
AD
1886 setcookie("ttrss_lang", $_SESSION["language"],
1887 time() + SESSION_COOKIE_LIFETIME);
1888 }
4fdb0476
AD
1889
1890 /* bump counters stamp since we're getting reloaded anyway */
1891
1892 $_SESSION["get_all_counters_stamp"] = time();
b8aa49bc 1893 }
d620cfe7 1894
b8aa49bc 1895 } else {
0bbba72d 1896 return authenticate_user($link, "admin", null);
b8aa49bc
AD
1897 }
1898 }
3547842a
AD
1899
1900 function truncate_string($str, $max_len) {
12db369c 1901 if (mb_strlen($str, "utf-8") > $max_len - 3) {
66a251f9 1902 return mb_substr($str, 0, $max_len, "utf-8") . "&hellip;";
3547842a
AD
1903 } else {
1904 return $str;
1905 }
1906 }
54a60e1a
AD
1907
1908 function get_user_theme_path($link) {
798f722b
AD
1909 $result = db_query($link, "SELECT theme_path
1910 FROM
1911 ttrss_themes,ttrss_users
1912 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
54a60e1a
AD
1913 if (db_num_rows($result) != 0) {
1914 return db_fetch_result($result, 0, "theme_path");
1915 } else {
1916 return null;
1917 }
1918 }
be773442
AD
1919
1920 function smart_date_time($timestamp) {
1921 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1922 return date("G:i", $timestamp);
f26450f1 1923 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
1924 return date("M d, G:i", $timestamp);
1925 } else {
7d7e0509 1926 return date("Y/m/d, G:i", $timestamp);
be773442
AD
1927 }
1928 }
1929
1930 function smart_date($timestamp) {
1931 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1932 return "Today";
f26450f1 1933 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
1934 return date("D m", $timestamp);
1935 } else {
b02111c2 1936 return date("Y/m/d", $timestamp);
be773442
AD
1937 }
1938 }
a654a595
AD
1939
1940 function sql_bool_to_string($s) {
1941 if ($s == "t" || $s == "1") {
1942 return "true";
1943 } else {
1944 return "false";
1945 }
1946 }
e3c99f3b
AD
1947
1948 function sql_bool_to_bool($s) {
1949 if ($s == "t" || $s == "1") {
1950 return true;
1951 } else {
1952 return false;
1953 }
1954 }
0ea4fb50 1955
e3c99f3b 1956
0ea4fb50
AD
1957 function toggleEvenOdd($a) {
1958 if ($a == "even")
1959 return "odd";
1960 else
1961 return "even";
1962 }
6043fb7e
AD
1963
1964 function sanity_check($link) {
9cbca41f 1965
aec3ce39
AD
1966 error_reporting(0);
1967
6043fb7e
AD
1968 $error_code = 0;
1969 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1970 $schema_version = db_fetch_result($result, 0, "schema_version");
1971
1972 if ($schema_version != SCHEMA_VERSION) {
1973 $error_code = 5;
1974 }
1975
aec3ce39
AD
1976 if (DB_TYPE == "mysql") {
1977 $result = db_query($link, "SELECT true", false);
1978 if (db_num_rows($result) != 1) {
1979 $error_code = 10;
1980 }
1981 }
1982
f29ba148
AD
1983 if (db_escape_string("testTEST") != "testTEST") {
1984 $error_code = 12;
1985 }
1986
aec3ce39
AD
1987 error_reporting (DEFAULT_ERROR_LEVEL);
1988
6043fb7e 1989 if ($error_code != 0) {
aec3ce39 1990 print_error_xml($error_code);
6043fb7e
AD
1991 return false;
1992 } else {
1993 return true;
4220d6b0 1994 }
6043fb7e
AD
1995 }
1996
27981ca3 1997 function file_is_locked($filename) {
31a6d42d
AD
1998 if (function_exists('flock')) {
1999 error_reporting(0);
cfa43e02 2000 $fp = fopen(LOCK_DIRECTORY . "/$filename", "r");
31a6d42d
AD
2001 error_reporting(DEFAULT_ERROR_LEVEL);
2002 if ($fp) {
2003 if (flock($fp, LOCK_EX | LOCK_NB)) {
2004 flock($fp, LOCK_UN);
2005 fclose($fp);
2006 return false;
2007 }
27981ca3 2008 fclose($fp);
31a6d42d 2009 return true;
e89aed7b
AD
2010 } else {
2011 return false;
27981ca3 2012 }
27981ca3 2013 }
c1fb4a5e 2014 return true; // consider the file always locked and skip the test
27981ca3
AD
2015 }
2016
fcb4c0c9 2017 function make_lockfile($filename) {
cfa43e02 2018 $fp = fopen(LOCK_DIRECTORY . "/$filename", "w");
fcb4c0c9
AD
2019
2020 if (flock($fp, LOCK_EX | LOCK_NB)) {
2021 return $fp;
2022 } else {
2023 return false;
2024 }
2025 }
2026
bf7fcde8 2027 function make_stampfile($filename) {
cfa43e02 2028 $fp = fopen(LOCK_DIRECTORY . "/$filename", "w");
bf7fcde8 2029
8e00ae9b 2030 if (flock($fp, LOCK_EX | LOCK_NB)) {
bf7fcde8 2031 fwrite($fp, time() . "\n");
8e00ae9b 2032 flock($fp, LOCK_UN);
bf7fcde8
AD
2033 fclose($fp);
2034 return true;
2035 } else {
2036 return false;
2037 }
2038 }
2039
8e00ae9b
AD
2040 function read_stampfile($filename) {
2041
2042 error_reporting(0);
cfa43e02 2043 $fp = fopen(LOCK_DIRECTORY . "/$filename", "r");
8e00ae9b
AD
2044 error_reporting (DEFAULT_ERROR_LEVEL);
2045
e89aed7b
AD
2046 if ($fp) {
2047 if (flock($fp, LOCK_EX)) {
2048 $stamp = fgets($fp);
2049 flock($fp, LOCK_UN);
2050 fclose($fp);
2051 return $stamp;
2052 } else {
2053 return false;
2054 }
8e00ae9b
AD
2055 } else {
2056 return false;
2057 }
2058 }
bf7fcde8 2059
894ebcf5
AD
2060 function sql_random_function() {
2061 if (DB_TYPE == "mysql") {
2062 return "RAND()";
2063 } else {
2064 return "RANDOM()";
2065 }
2066 }
2067
23aa0d16 2068 function catchup_feed($link, $feed, $cat_view) {
88040f57
AD
2069
2070 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
23aa0d16
AD
2071
2072 if ($cat_view) {
2073
2074 if ($feed > 0) {
2075 $cat_qpart = "cat_id = '$feed'";
2076 } else {
2077 $cat_qpart = "cat_id IS NULL";
2078 }
2079
2080 $tmp_result = db_query($link, "SELECT id
2081 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
2082 $_SESSION["uid"]);
2083
2084 while ($tmp_line = db_fetch_assoc($tmp_result)) {
2085
2086 $tmp_feed = $tmp_line["id"];
2087
2088 db_query($link, "UPDATE ttrss_user_entries
2089 SET unread = false,last_read = NOW()
2090 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
2091 }
2092
2093 } else if ($feed > 0) {
2094
2095 $tmp_result = db_query($link, "SELECT id
2096 FROM ttrss_feeds WHERE parent_feed = '$feed'
2097 ORDER BY cat_id,title");
2098
2099 $parent_ids = array();
2100
2101 if (db_num_rows($tmp_result) > 0) {
2102 while ($p = db_fetch_assoc($tmp_result)) {
2103 array_push($parent_ids, "feed_id = " . $p["id"]);
2104 }
2105
2106 $children_qpart = implode(" OR ", $parent_ids);
2107
2108 db_query($link, "UPDATE ttrss_user_entries
2109 SET unread = false,last_read = NOW()
2110 WHERE (feed_id = '$feed' OR $children_qpart)
2111 AND owner_uid = " . $_SESSION["uid"]);
2112
2113 } else {
2114 db_query($link, "UPDATE ttrss_user_entries
2115 SET unread = false,last_read = NOW()
2116 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2117 }
2118
2119 } else if ($feed < 0 && $feed > -10) { // special, like starred
2120
2121 if ($feed == -1) {
2122 db_query($link, "UPDATE ttrss_user_entries
2123 SET unread = false,last_read = NOW()
2124 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
2125 }
e4f4b46f
AD
2126
2127 if ($feed == -2) {
2128 db_query($link, "UPDATE ttrss_user_entries
2129 SET unread = false,last_read = NOW()
2130 WHERE published = true AND owner_uid = ".$_SESSION["uid"]);
2131 }
2132
2d24f032
AD
2133 if ($feed == -3) {
2134
c1d7e6c3
AD
2135 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
2136
2d24f032 2137 if (DB_TYPE == "pgsql") {
7608b38a 2138 $match_part = "updated > NOW() - INTERVAL '$intl hour' ";
2d24f032 2139 } else {
7608b38a 2140 $match_part = "updated > DATE_SUB(NOW(),
c1d7e6c3 2141 INTERVAL $intl HOUR) ";
2d24f032
AD
2142 }
2143
1f3335dc
AD
2144 $result = db_query($link, "SELECT id FROM ttrss_entries,
2145 ttrss_user_entries WHERE $match_part AND
2146 unread = true AND
2147 ttrss_user_entries.ref_id = ttrss_entries.id AND
2148 owner_uid = ".$_SESSION["uid"]);
2149
2150 $affected_ids = array();
2151
2152 while ($line = db_fetch_assoc($result)) {
2153 array_push($affected_ids, $line["id"]);
2154 }
2155
2156 catchupArticlesById($link, $affected_ids, 0);
2d24f032
AD
2157 }
2158
23aa0d16
AD
2159 } else if ($feed < -10) { // label
2160
2161 // TODO make this more efficient
2162
2163 $label_id = -$feed - 11;
2164
2165 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2166 WHERE id = '$label_id'");
2167
2168 if ($tmp_result) {
2169 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
2170
2171 db_query($link, "BEGIN");
2172
2173 $tmp2_result = db_query($link,
2174 "SELECT
2175 int_id
2176 FROM
88040f57 2177 ttrss_user_entries,ttrss_entries,ttrss_feeds
23aa0d16 2178 WHERE
88040f57
AD
2179 ref_id = ttrss_entries.id AND
2180 ttrss_user_entries.feed_id = ttrss_feeds.id AND
23aa0d16 2181 $sql_exp AND
88040f57 2182 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
23aa0d16
AD
2183
2184 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
2185 db_query($link, "UPDATE
2186 ttrss_user_entries
2187 SET
2188 unread = false, last_read = NOW()
2189 WHERE
2190 int_id = " . $tmp_line["int_id"]);
2191 }
2192
2193 db_query($link, "COMMIT");
2194
2195/* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
2196 SET unread = false,last_read = NOW()
2197 WHERE $sql_exp
2198 AND ref_id = id
2199 AND owner_uid = ".$_SESSION["uid"]); */
2200 }
2201 }
2202 } else { // tag
2203 db_query($link, "BEGIN");
2204
2205 $tag_name = db_escape_string($feed);
2206
2207 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
2208 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
2209
2210 while ($line = db_fetch_assoc($result)) {
2211 db_query($link, "UPDATE ttrss_user_entries SET
2212 unread = false, last_read = NOW()
2213 WHERE int_id = " . $line["post_int_id"]);
2214 }
2215 db_query($link, "COMMIT");
2216 }
2217 }
2218
35bf080c 2219 function update_generic_feed($link, $feed, $cat_view, $force_update = false) {
23aa0d16
AD
2220 if ($cat_view) {
2221
2222 if ($feed > 0) {
2223 $cat_qpart = "cat_id = '$feed'";
2224 } else {
2225 $cat_qpart = "cat_id IS NULL";
2226 }
2227
571dad82 2228 $tmp_result = db_query($link, "SELECT id,feed_url FROM ttrss_feeds
23aa0d16
AD
2229 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
2230
2231 while ($tmp_line = db_fetch_assoc($tmp_result)) {
2232 $feed_url = $tmp_line["feed_url"];
571dad82 2233 $feed_id = $tmp_line["id"];
35bf080c 2234 update_rss_feed($link, $feed_url, $feed_id, $force_update);
23aa0d16
AD
2235 }
2236
2237 } else {
2238 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
2239 WHERE id = '$feed'");
2240 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
35bf080c 2241 update_rss_feed($link, $feed_url, $feed, $force_update);
23aa0d16
AD
2242 }
2243 }
a9cb1f83 2244
4ffa126e 2245 function getAllCounters($link, $omode = "flc", $active_feed = false) {
cf4d339c 2246
d9e4bba0
AD
2247 /* getting all counters is a resource intensive operation, so we
2248 * rate limit it a little bit */
cf4d339c 2249
d234a2e3
AD
2250
2251
2252 if (get_pref($link, "SYNC_COUNTERS") ||
2253 time() - $_SESSION["get_all_counters_stamp"] > 5) {
cf4d339c 2254
d9e4bba0
AD
2255 if (!$omode) $omode = "flc";
2256
2257 getGlobalCounters($link);
2258
2259 if (strchr($omode, "l")) getLabelCounters($link);
2260 if (strchr($omode, "f")) getFeedCounters($link, SMART_RPC_COUNTERS, $active_feed);
2261 if (strchr($omode, "t")) getTagCounters($link);
2262 if (strchr($omode, "c")) {
2263 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2264 getCategoryCounters($link);
2265 }
cf4d339c 2266 }
d9e4bba0
AD
2267
2268 $_SESSION["get_all_counters_stamp"] = time();
a9cb1f83 2269 }
d9e4bba0 2270
a9cb1f83
AD
2271 }
2272
2273 function getCategoryCounters($link) {
bba7c4bf
AD
2274 # two special categories are -1 and -2 (all virtuals; all labels)
2275
2276 $ctr = getCategoryUnread($link, -1);
2277
2278 print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>";
2279
2280 $ctr = getCategoryUnread($link, -2);
2281
2282 print "<counter type=\"category\" id=\"-2\" counter=\"$ctr\"/>";
2283
14073c0a
AD
2284 $age_qpart = getMaxAgeSubquery();
2285
a9cb1f83 2286 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
14073c0a
AD
2287 FROM ttrss_user_entries, ttrss_entries WHERE feed_id = ttrss_feeds.id
2288 AND id = ref_id AND $age_qpart
a9cb1f83
AD
2289 AND unread = true)) AS unread FROM ttrss_feeds
2290 WHERE
cfb02131 2291 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
a9cb1f83
AD
2292
2293 while ($line = db_fetch_assoc($result)) {
2294 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
2295 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
2296 $line["unread"]."\"/>";
2297 }
2298 }
2299
f295c368
AD
2300 function getCategoryUnread($link, $cat) {
2301
bba7c4bf 2302 if ($cat >= 0) {
18664970 2303
bba7c4bf
AD
2304 if ($cat != 0) {
2305 $cat_query = "cat_id = '$cat'";
2306 } else {
2307 $cat_query = "cat_id IS NULL";
2308 }
14073c0a
AD
2309
2310 $age_qpart = getMaxAgeSubquery();
2311
bba7c4bf
AD
2312 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
2313 AND hidden = false
2314 AND owner_uid = " . $_SESSION["uid"]);
2315
2316 $cat_feeds = array();
2317 while ($line = db_fetch_assoc($result)) {
2318 array_push($cat_feeds, "feed_id = " . $line["id"]);
2319 }
2320
2321 if (count($cat_feeds) == 0) return 0;
2322
2323 $match_part = implode(" OR ", $cat_feeds);
2324
2325 $result = db_query($link, "SELECT COUNT(int_id) AS unread
14073c0a
AD
2326 FROM ttrss_user_entries,ttrss_entries
2327 WHERE unread = true AND ($match_part) AND id = ref_id
2328 AND $age_qpart AND owner_uid = " . $_SESSION["uid"]);
bba7c4bf
AD
2329
2330 $unread = 0;
2331
2332 # this needs to be rewritten
2333 while ($line = db_fetch_assoc($result)) {
2334 $unread += $line["unread"];
2335 }
2336
2337 return $unread;
2338 } else if ($cat == -1) {
2d24f032 2339 return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3);
bba7c4bf 2340 } else if ($cat == -2) {
f295c368 2341
bba7c4bf
AD
2342 $rv = getLabelCounters($link, false, true);
2343 $ctr = 0;
f295c368 2344
bba7c4bf
AD
2345 foreach (array_keys($rv) as $k) {
2346 if ($k < -10) {
2347 $ctr += $rv[$k]["counter"];
2348 }
2349 }
f295c368 2350
bba7c4bf 2351 return $ctr;
f295c368 2352 }
f295c368
AD
2353 }
2354
14073c0a
AD
2355 function getMaxAgeSubquery($days = COUNTERS_MAX_AGE) {
2356 if (DB_TYPE == "pgsql") {
2357 return "ttrss_entries.date_entered >
2358 NOW() - INTERVAL '$days days'";
2359 } else {
2360 return "ttrss_entries.date_entered >
2361 DATE_SUB(NOW(), INTERVAL $days DAY)";
2362 }
2363 }
2364
f295c368 2365 function getFeedUnread($link, $feed, $is_cat = false) {
bdb7369b
AD
2366 return getFeedArticles($link, $feed, $is_cat, true);
2367 }
2368
2369 function getFeedArticles($link, $feed, $is_cat = false, $unread_only = false) {
a9cb1f83 2370 $n_feed = sprintf("%d", $feed);
f295c368 2371
bdb7369b
AD
2372 if ($unread_only) {
2373 $unread_qpart = "unread = true";
2374 } else {
2375 $unread_qpart = "true";
2376 }
2377
14073c0a
AD
2378 $age_qpart = getMaxAgeSubquery();
2379
f295c368 2380 if ($is_cat) {
831ff047 2381 return getCategoryUnread($link, $n_feed);
f295c368 2382 } else if ($n_feed == -1) {
a9cb1f83 2383 $match_part = "marked = true";
e4f4b46f
AD
2384 } else if ($n_feed == -2) {
2385 $match_part = "published = true";
2d24f032
AD
2386 } else if ($n_feed == -3) {
2387 $match_part = "unread = true";
2388
c1d7e6c3
AD
2389 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
2390
2d24f032 2391 if (DB_TYPE == "pgsql") {
7608b38a 2392 $match_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
2d24f032 2393 } else {
7608b38a 2394 $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2d24f032
AD
2395 }
2396
4919fb42 2397 } else if ($n_feed > 0) {
831ff047 2398
e8b8485f
AD
2399 $result = db_query($link, "SELECT id FROM ttrss_feeds
2400 WHERE parent_feed = '$n_feed'
318260cc 2401 AND hidden = false
4919fb42 2402 AND owner_uid = " . $_SESSION["uid"]);
831ff047
AD
2403
2404 if (db_num_rows($result) > 0) {
4919fb42 2405
831ff047
AD
2406 $linked_feeds = array();
2407 while ($line = db_fetch_assoc($result)) {
2408 array_push($linked_feeds, "feed_id = " . $line["id"]);
2409 }
e8b8485f
AD
2410
2411 array_push($linked_feeds, "feed_id = $n_feed");
831ff047
AD
2412
2413 $match_part = implode(" OR ", $linked_feeds);
2414
4919fb42 2415 $result = db_query($link, "SELECT COUNT(int_id) AS unread
14073c0a 2416 FROM ttrss_user_entries,ttrss_entries
bdb7369b 2417 WHERE $unread_qpart AND
14073c0a
AD
2418 ttrss_user_entries.ref_id = ttrss_entries.id AND
2419 $age_qpart AND
2420 ($match_part) AND
2421 owner_uid = " . $_SESSION["uid"]);
4919fb42
AD
2422
2423 $unread = 0;
2424
2425 # this needs to be rewritten
2426 while ($line = db_fetch_assoc($result)) {
2427 $unread += $line["unread"];
2428 }
2429
2430 return $unread;
2431
831ff047
AD
2432 } else {
2433 $match_part = "feed_id = '$n_feed'";
2434 }
a9cb1f83 2435 } else if ($feed < -10) {
318260cc 2436
a9cb1f83
AD
2437 $label_id = -$feed - 11;
2438
2439 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
2440 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
2441
2442 $match_part = db_fetch_result($result, 0, "sql_exp");
2443 }
2444
2445 if ($match_part) {
2446
2447 $result = db_query($link, "SELECT count(int_id) AS unread
88040f57
AD
2448 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
2449 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2450 ttrss_user_entries.ref_id = ttrss_entries.id AND
cfb02131 2451 ttrss_feeds.hidden = false AND
14073c0a 2452 $age_qpart AND
bdb7369b 2453 $unread_qpart AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
a9cb1f83
AD
2454
2455 } else {
2456
2457 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
14073c0a 2458 FROM ttrss_tags,ttrss_user_entries,ttrss_entries
828f22b7 2459 WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id
bdb7369b 2460 AND $unread_qpart AND $age_qpart AND
a9cb1f83
AD
2461 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
2462 }
2463
2464 $unread = db_fetch_result($result, 0, "unread");
cfb02131 2465
a9cb1f83
AD
2466 return $unread;
2467 }
2468
2469 /* FIXME this needs reworking */
2470
f3acc32e
AD
2471 function getGlobalUnread($link, $user_id = false) {
2472
2473 if (!$user_id) {
2474 $user_id = $_SESSION["uid"];
2475 }
2476
14073c0a
AD
2477 $age_qpart = getMaxAgeSubquery();
2478
3831db41 2479 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
a9cb1f83 2480 WHERE unread = true AND
3831db41 2481 ttrss_user_entries.feed_id = ttrss_feeds.id AND
a9cb1f83 2482 ttrss_user_entries.ref_id = ttrss_entries.id AND
3831db41 2483 hidden = false AND
14073c0a 2484 $age_qpart AND
f3acc32e 2485 ttrss_user_entries.owner_uid = '$user_id'");
a9cb1f83
AD
2486 $c_id = db_fetch_result($result, 0, "c_id");
2487 return $c_id;
2488 }
2489
2490 function getGlobalCounters($link, $global_unread = -1) {
2491 if ($global_unread == -1) {
2492 $global_unread = getGlobalUnread($link);
2493 }
7bf7e4d3
AD
2494 print "<counter type=\"global\" id='global-unread'
2495 counter='$global_unread'/>";
2496
2497 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
2498 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
2499
2500 $subscribed_feeds = db_fetch_result($result, 0, "fn");
2501
2502 print "<counter type=\"global\" id='subscribed-feeds'
2503 counter='$subscribed_feeds'/>";
2504
a9cb1f83
AD
2505 }
2506
2507 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
2508
2509 if ($smart_mode) {
2510 if (!$_SESSION["tctr_last_value"]) {
2511 $_SESSION["tctr_last_value"] = array();
2512 }
2513 }
2514
2515 $old_counters = $_SESSION["tctr_last_value"];
2516
2517 $tctrs_modified = false;
2518
2519/* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
2520 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
2521 ttrss_user_entries.ref_id = ttrss_entries.id AND
2522 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
2523 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
2524 UNION
2525 select tag_name,0 as count FROM ttrss_tags
2526 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
2527
14073c0a
AD
2528 $age_qpart = getMaxAgeSubquery();
2529
a9cb1f83 2530 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
14073c0a
AD
2531 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
2532 AND ref_id = id AND $age_qpart
a9cb1f83 2533 AND unread = true)) AS count FROM ttrss_tags
ef1ac7c7
AD
2534 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
2535 ORDER BY count DESC LIMIT 55");
a9cb1f83
AD
2536
2537 $tags = array();
2538
2539 while ($line = db_fetch_assoc($result)) {
2540 $tags[$line["tag_name"]] += $line["count"];
2541 }
2542
2543 foreach (array_keys($tags) as $tag) {
2544 $unread = $tags[$tag];
2545
2546 $tag = htmlspecialchars($tag);
2547
2548 if (!$smart_mode || $old_counters[$tag] != $unread) {
2549 $old_counters[$tag] = $unread;
2550 $tctrs_modified = true;
2551 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
2552 }
2553
2554 }
2555
2556 if ($smart_mode && $tctrs_modified) {
2557 $_SESSION["tctr_last_value"] = $old_counters;
2558 }
2559
2560 }
2561
ef393de7 2562 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) {
a9cb1f83 2563
14073c0a
AD
2564 $age_qpart = getMaxAgeSubquery();
2565
a9cb1f83
AD
2566 if ($smart_mode) {
2567 if (!$_SESSION["lctr_last_value"]) {
2568 $_SESSION["lctr_last_value"] = array();
2569 }
2570 }
2571
ef393de7
AD
2572 $ret_arr = array();
2573
a9cb1f83
AD
2574 $old_counters = $_SESSION["lctr_last_value"];
2575 $lctrs_modified = false;
2576
2d24f032 2577 $count = getFeedUnread($link, -1);
a9cb1f83 2578
ef393de7 2579 if (!$ret_mode) {
bdb7369b
AD
2580
2581 if (get_pref($link, 'EXTENDED_FEEDLIST')) {
2582 $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $id) . " total)\"";
2583 } else {
2584 $xmsg_part = "";
2585 }
2586
2587 print "<counter type=\"label\" id=\"-1\" counter=\"$count\" $xmsg_part/>";
ef393de7
AD
2588 } else {
2589 $ret_arr["-1"]["counter"] = $count;
bba7c4bf 2590 $ret_arr["-1"]["description"] = __("Starred articles");
ef393de7 2591 }
a9cb1f83 2592
2d24f032 2593 $count = getFeedUnread($link, -2);
e4f4b46f
AD
2594
2595 if (!$ret_mode) {
bdb7369b
AD
2596
2597 if (get_pref($link, 'EXTENDED_FEEDLIST')) {
2598 $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $id) . " total)\"";
2599 } else {
2600 $xmsg_part = "";
2601 }
2602
2603 print "<counter type=\"label\" id=\"-2\" counter=\"$count\" $xmsg_part/>";
e4f4b46f
AD
2604 } else {
2605 $ret_arr["-2"]["counter"] = $count;
bba7c4bf 2606 $ret_arr["-2"]["description"] = __("Published articles");
e4f4b46f
AD
2607 }
2608
2d24f032
AD
2609 $count = getFeedUnread($link, -3);
2610
2611 if (!$ret_mode) {
bdb7369b
AD
2612
2613 if (get_pref($link, 'EXTENDED_FEEDLIST')) {
2614 $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $id) . " total)\"";
2615 } else {
2616 $xmsg_part = "";
2617 }
2618
2619 print "<counter type=\"label\" id=\"-3\" counter=\"$count\" $xmsg_part/>";
2d24f032
AD
2620 } else {
2621 $ret_arr["-3"]["counter"] = $count;
2622 $ret_arr["-3"]["description"] = __("Fresh articles");
2623 }
2624
e4f4b46f 2625
a9cb1f83
AD
2626 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
2627 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
2628
2629 while ($line = db_fetch_assoc($result)) {
2630
2631 $id = -$line["id"] - 11;
2632
ef393de7
AD
2633 $label_name = $line["description"];
2634
a9cb1f83
AD
2635 error_reporting (0);
2636
88040f57 2637 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
a9cb1f83 2638 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
cfb02131 2639 ttrss_feeds.hidden = false AND
14073c0a 2640 $age_qpart AND
88040f57 2641 ttrss_user_entries.feed_id = ttrss_feeds.id AND
a9cb1f83 2642 ttrss_user_entries.ref_id = ttrss_entries.id AND
88040f57 2643 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
a9cb1f83
AD
2644
2645 $count = db_fetch_result($tmp_result, 0, "count");
2646
2647 if (!$smart_mode || $old_counters[$id] != $count) {
2648 $old_counters[$id] = $count;
2649 $lctrs_modified = true;
ef393de7 2650 if (!$ret_mode) {
bdb7369b
AD
2651
2652 if (get_pref($link, 'EXTENDED_FEEDLIST')) {
2653 $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $id) . " total)\"";
2654 } else {
2655 $xmsg_part = "";
2656 }
2657
2658 print "<counter type=\"label\" id=\"$id\" counter=\"$count\" $xmsg_part/>";
ef393de7
AD
2659 } else {
2660 $ret_arr[$id]["counter"] = $count;
2661 $ret_arr[$id]["description"] = $label_name;
2662 }
a9cb1f83
AD
2663 }
2664
2665 error_reporting (DEFAULT_ERROR_LEVEL);
2666 }
2667
2668 if ($smart_mode && $lctrs_modified) {
2669 $_SESSION["lctr_last_value"] = $old_counters;
2670 }
ef393de7
AD
2671
2672 return $ret_arr;
a9cb1f83
AD
2673 }
2674
2675/* function getFeedCounter($link, $id) {
2676
2677 $result = db_query($link, "SELECT
2678 count(id) as count,last_error
2679 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
2680 WHERE feed_id = '$id' AND unread = true
2681 AND ttrss_user_entries.feed_id = ttrss_feeds.id
2682 AND ttrss_user_entries.ref_id = ttrss_entries.id");
2683
2684 $count = db_fetch_result($result, 0, "count");
2685 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
2686
2687 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
2688 } */
2689
4ffa126e 2690 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS, $active_feed = false) {
a9cb1f83 2691
14073c0a
AD
2692 $age_qpart = getMaxAgeSubquery();
2693
a9cb1f83
AD
2694 if ($smart_mode) {
2695 if (!$_SESSION["fctr_last_value"]) {
2696 $_SESSION["fctr_last_value"] = array();
2697 }
2698 }
2699
2700 $old_counters = $_SESSION["fctr_last_value"];
2701
1b1b8a7b 2702/* $result = db_query($link, "SELECT id,last_error,parent_feed,
fc2b26a6 2703 ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated,
a9cb1f83
AD
2704 (SELECT count(id)
2705 FROM ttrss_entries,ttrss_user_entries
2706 WHERE feed_id = ttrss_feeds.id AND
2707 ttrss_user_entries.ref_id = ttrss_entries.id
2708 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
2709 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1b1b8a7b
AD
2710 AND parent_feed IS NULL"); */
2711
14073c0a
AD
2712 $query = "SELECT ttrss_feeds.id,
2713 ttrss_feeds.title,
fc2b26a6 2714 ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated,
1b1b8a7b 2715 last_error,
bc60fda3 2716 COUNT(ttrss_entries.id) AS count
1b1b8a7b
AD
2717 FROM ttrss_feeds
2718 LEFT JOIN ttrss_user_entries ON (ttrss_user_entries.feed_id = ttrss_feeds.id
2719 AND ttrss_user_entries.owner_uid = ttrss_feeds.owner_uid
2720 AND ttrss_user_entries.unread = true)
14073c0a
AD
2721 LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id AND
2722 $age_qpart)
2723 WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
1b1b8a7b 2724 AND parent_feed IS NULL
14073c0a 2725 GROUP BY ttrss_feeds.id, ttrss_feeds.title, ttrss_feeds.last_updated, last_error";
a9cb1f83 2726
14073c0a 2727 $result = db_query($link, $query);
a9cb1f83
AD
2728 $fctrs_modified = false;
2729
fb1fb4ab
AD
2730 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2731
a9cb1f83
AD
2732 while ($line = db_fetch_assoc($result)) {
2733
2734 $id = $line["id"];
2735 $count = $line["count"];
2736 $last_error = htmlspecialchars($line["last_error"]);
fb1fb4ab
AD
2737
2738 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2739 $last_updated = smart_date_time(strtotime($line["last_updated"]));
2740 } else {
2741 $last_updated = date($short_date, strtotime($line["last_updated"]));
2742 }
2743
588fb13b
AD
2744 $last_updated = htmlspecialchars($last_updated);
2745
7defa089 2746 $has_img = feed_has_icon($id);
a9cb1f83
AD
2747
2748 $tmp_result = db_query($link,
14073c0a 2749 "SELECT ttrss_feeds.id,COUNT(unread) AS unread
a9cb1f83
AD
2750 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
2751 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
14073c0a
AD
2752 LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id)
2753 WHERE parent_feed = '$id' AND $age_qpart AND unread = true GROUP BY ttrss_feeds.id");
a9cb1f83
AD
2754
2755 if (db_num_rows($tmp_result) > 0) {
2756 while ($l = db_fetch_assoc($tmp_result)) {
2757 $count += $l["unread"];
2758 }
2759 }
2760
2761 if (!$smart_mode || $old_counters[$id] != $count) {
2762 $old_counters[$id] = $count;
2763 $fctrs_modified = true;
2764
2765 if ($last_error) {
2766 $error_part = "error=\"$last_error\"";
2767 } else {
2768 $error_part = "";
2769 }
2770
2771 if ($has_img) {
2772 $has_img_part = "hi=\"$has_img\"";
2773 } else {
2774 $has_img_part = "";
2775 }
2776
4ffa126e
AD
2777 if ($active_feed && $id == $active_feed) {
2778 $has_title_part = "title=\"" . htmlspecialchars($line["title"]) . "\"";
2779 } else {
2780 $has_title_part = "";
2781 }
2782
bdb7369b
AD
2783 if (get_pref($link, 'EXTENDED_FEEDLIST')) {
2784 $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $id) . " total)\"";
2785 }
2786
2787 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\" $xmsg_part $has_title_part/>";
a9cb1f83
AD
2788 }
2789 }
2790
2791 if ($smart_mode && $fctrs_modified) {
2792 $_SESSION["fctr_last_value"] = $old_counters;
2793 }
2794 }
2795
1b758780 2796 function get_script_dt_add() {
34e420fb 2797 if (strpos(VERSION, ".99") === false) {
1b758780
AD
2798 return VERSION;
2799 } else {
2800 return time();
2801 }
2802 }
2803
6e7f8d26
AD
2804 function get_pgsql_version($link) {
2805 $result = db_query($link, "SELECT version() AS version");
2806 $version = split(" ", db_fetch_result($result, 0, "version"));
2807 return $version[1];
2808 }
2809
af106b0e
AD
2810 function print_error_xml($code, $add_msg = "") {
2811 global $ERRORS;
2812
2813 $error_msg = $ERRORS[$code];
2814
2815 if ($add_msg) {
2816 $error_msg = "$error_msg; $add_msg";
2817 }
2818
4c2abbc1 2819 print "<rpc-reply>";
af106b0e 2820 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
4c2abbc1 2821 print "</rpc-reply>";
af106b0e 2822 }
956c7629 2823
f27de515
AD
2824 function subscribe_to_feed($link, $feed_link, $cat_id = 0,
2825 $auth_login = '', $auth_pass = '') {
bb0f29a4 2826
b3dfe8ba 2827 # check for feed:http://url
c91c2249
AD
2828 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
2829
b3dfe8ba 2830 # check for feed://URL
e2d84cdb 2831 if (strpos($feed_link, "//") === 0) {
b3dfe8ba
AD
2832 $feed_link = "http:$feed_link";
2833 }
2834
c91c2249 2835 if ($feed_link == "") return;
bb0f29a4 2836
956c7629
AD
2837 if ($cat_id == "0" || !$cat_id) {
2838 $cat_qpart = "NULL";
2839 } else {
2840 $cat_qpart = "'$cat_id'";
2841 }
2842
2843 $result = db_query($link,
2844 "SELECT id FROM ttrss_feeds
2845 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
2846
2847 if (db_num_rows($result) == 0) {
2848
2849 $result = db_query($link,
f27de515
AD
2850 "INSERT INTO ttrss_feeds
2851 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass)
956c7629 2852 VALUES ('".$_SESSION["uid"]."', '$feed_link',
f27de515 2853 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')");
956c7629
AD
2854
2855 $result = db_query($link,
2856 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
f27de515 2857 AND owner_uid = " . $_SESSION["uid"]);
956c7629
AD
2858
2859 $feed_id = db_fetch_result($result, 0, "id");
2860
2861 if ($feed_id) {
2862 update_rss_feed($link, $feed_link, $feed_id, true);
2863 }
2864
2865 return true;
2866 } else {
2867 return false;
2868 }
2869 }
2870
673d54ca
AD
2871 function print_feed_select($link, $id, $default_id = "",
2872 $attributes = "", $include_all_feeds = true) {
2873
79f3553b 2874 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca 2875 if ($include_all_feeds) {
89cb787e 2876 print "<option value=\"0\">".__('All feeds')."</option>";
673d54ca
AD
2877 }
2878
2879 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2880 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2881
2882 if (db_num_rows($result) > 0 && $include_all_feeds) {
2883 print "<option disabled>--------</option>";
2884 }
2885
2886 while ($line = db_fetch_assoc($result)) {
2887 if ($line["id"] == $default_id) {
2888 $is_selected = "selected";
2889 } else {
2890 $is_selected = "";
2891 }
79f3553b 2892 printf("<option $is_selected value='%d'>%s</option>",
47439031 2893 $line["id"], htmlspecialchars($line["title"]));
673d54ca
AD
2894 }
2895
2896 print "</select>";
2897 }
2898
2899 function print_feed_cat_select($link, $id, $default_id = "",
2900 $attributes = "", $include_all_cats = true) {
2901
79f3553b 2902 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca
AD
2903
2904 if ($include_all_cats) {
d1db26aa 2905 print "<option value=\"0\">".__('Uncategorized')."</option>";
673d54ca
AD
2906 }
2907
2908 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
2909 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2910
2911 if (db_num_rows($result) > 0 && $include_all_cats) {
2912 print "<option disabled>--------</option>";
2913 }
2914
2915 while ($line = db_fetch_assoc($result)) {
2916 if ($line["id"] == $default_id) {
2917 $is_selected = "selected";
2918 } else {
2919 $is_selected = "";
2920 }
14f69488 2921 printf("<option $is_selected value='%d'>%s</option>",
47439031 2922 $line["id"], htmlspecialchars($line["title"]));
673d54ca
AD
2923 }
2924
2925 print "</select>";
2926 }
2927
14f69488
AD
2928 function checkbox_to_sql_bool($val) {
2929 return ($val == "on") ? "true" : "false";
2930 }
86b682ce
AD
2931
2932 function getFeedCatTitle($link, $id) {
2933 if ($id == -1) {
d1db26aa 2934 return __("Special");
86b682ce 2935 } else if ($id < -10) {
d1db26aa 2936 return __("Labels");
86b682ce
AD
2937 } else if ($id > 0) {
2938 $result = db_query($link, "SELECT ttrss_feed_categories.title
2939 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
2940 cat_id = ttrss_feed_categories.id");
2941 if (db_num_rows($result) == 1) {
2942 return db_fetch_result($result, 0, "title");
2943 } else {
d1db26aa 2944 return __("Uncategorized");
86b682ce
AD
2945 }
2946 } else {
2947 return "getFeedCatTitle($id) failed";
2948 }
2949
2950 }
2951
2952 function getFeedTitle($link, $id) {
2953 if ($id == -1) {
d1db26aa 2954 return __("Starred articles");
945c243e
AD
2955 } else if ($id == -2) {
2956 return __("Published articles");
2d24f032
AD
2957 } else if ($id == -3) {
2958 return __("Fresh articles");
86b682ce 2959 } else if ($id < -10) {
76626c72 2960 $label_id = -$id - 11;
86b682ce
AD
2961 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2962 if (db_num_rows($result) == 1) {
2963 return db_fetch_result($result, 0, "description");
2964 } else {
2965 return "Unknown label ($label_id)";
2966 }
2967
2968 } else if ($id > 0) {
2969 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2970 if (db_num_rows($result) == 1) {
2971 return db_fetch_result($result, 0, "title");
2972 } else {
2973 return "Unknown feed ($id)";
2974 }
2975 } else {
2976 return "getFeedTitle($id) failed";
2977 }
2978
2979 }
3dd46f19
AD
2980
2981 function get_session_cookie_name() {
2982 return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME);
2983 }
3ac2b520
AD
2984
2985 function print_init_params($link) {
2986 print "<init-params>";
2987 if ($_SESSION["stored-params"]) {
2988 foreach (array_keys($_SESSION["stored-params"]) as $key) {
5f57b06d
AD
2989 if ($key) {
2990 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2991 print "<param key=\"$key\" value=\"$value\"/>";
2992 }
3ac2b520
AD
2993 }
2994 }
2995
20361063 2996 print "<param key=\"theme\" value=\"".$_SESSION["theme"]."\"/>";
3ac2b520
AD
2997 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>";
2998 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>";
17c0eeba 2999 print "<param key=\"daemon_refresh_only\" value=\"true\"/>";
3ac2b520
AD
3000
3001 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
3002 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
3003
e8bd0da9 3004 print "<param key=\"hide_read_feeds\" value=\"" .
465ff90b 3005 (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>";
e8bd0da9 3006
c9268ed5 3007 print "<param key=\"feeds_sort_by_unread\" value=\"" .
465ff90b 3008 (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>";
c9268ed5 3009
f6d6e22f 3010 print "<param key=\"confirm_feed_catchup\" value=\"" .
465ff90b 3011 (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>";
f6d6e22f 3012
ac7bcd71 3013 print "<param key=\"cdm_auto_catchup\" value=\"" .
465ff90b 3014 (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>";
ac7bcd71 3015
8e9c121b
AD
3016 print "<param key=\"icons_url\" value=\"" . ICONS_URL . "\"/>";
3017
be0801a1
AD
3018 print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME . "\"/>";
3019
40496720
AD
3020 print "<param key=\"default_view_mode\" value=\"" .
3021 get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>";
3022
3023 print "<param key=\"default_view_limit\" value=\"" .
465ff90b 3024 (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>";
40496720 3025
7b4d02a8
AD
3026 print "<param key=\"default_view_order_by\" value=\"" .
3027 get_pref($link, "_DEFAULT_VIEW_ORDER_BY") . "\"/>";
3028
fe8d2059
AD
3029 print "<param key=\"prefs_active_tab\" value=\"" .
3030 get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>";
3031
465ff90b
AD
3032 print "<param key=\"infobox_disable_overlay\" value=\"" .
3033 get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>";
3034
527c3bf0
AD
3035 print "<param key=\"icons_location\" value=\"" .
3036 ICONS_URL . "\"/>";
3037
22f3e356
AD
3038 print "<param key=\"hide_read_shows_special\" value=\"" .
3039 (int) get_pref($link, "HIDE_READ_SHOWS_SPECIAL") . "\"/>";
3040
fca93350
AD
3041 print "<param key=\"hide_feedlist\" value=\"" .
3042 (int) get_pref($link, "HIDE_FEEDLIST") . "\"/>";
24c1e1c1 3043
a598370d
AD
3044 print "<param key=\"bw_limit\" value=\"".
3045 (int) $_SESSION["bw_limit"]."\"/>";
3046
d234a2e3
AD
3047 print "<param key=\"sync_counters\" value=\"" .
3048 (int) get_pref($link, "SYNC_COUNTERS") . "\"/>";
3049
3ac2b520
AD
3050 print "</init-params>";
3051 }
f54f515f
AD
3052
3053 function print_runtime_info($link) {
3054 print "<runtime-info>";
20361063 3055
71ad883b
AD
3056 if (ENABLE_UPDATE_DAEMON) {
3057 print "<param key=\"daemon_is_running\" value=\"".
3058 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
8e00ae9b 3059
9041f58b 3060 if (time() - $_SESSION["daemon_stamp_check"] > 30) {
8e00ae9b
AD
3061
3062 $stamp = (int)read_stampfile("update_daemon.stamp");
3063
fbae93d8
AD
3064// print "<param key=\"daemon_stamp_delta\" value=\"$stamp_delta\"/>";
3065
8e00ae9b 3066 if ($stamp) {
9041f58b
AD
3067 $stamp_delta = time() - $stamp;
3068
3069 if ($stamp_delta > 1800) {
f6854e44 3070 $stamp_check = 0;
8e00ae9b 3071 } else {
f6854e44
AD
3072 $stamp_check = 1;
3073 $_SESSION["daemon_stamp_check"] = time();
8e00ae9b
AD
3074 }
3075
f6854e44
AD
3076 print "<param key=\"daemon_stamp_ok\" value=\"$stamp_check\"/>";
3077
8e00ae9b
AD
3078 $stamp_fmt = date("Y.m.d, G:i", $stamp);
3079
3080 print "<param key=\"daemon_stamp\" value=\"$stamp_fmt\"/>";
3081 }
8e00ae9b 3082 }
71ad883b 3083 }
8e00ae9b 3084
d9fa39f1
AD
3085 if (CHECK_FOR_NEW_VERSION && $_SESSION["access_level"] >= 10) {
3086
2d1d59c7 3087 if ($_SESSION["last_version_check"] + 86400 < time()) {
d9fa39f1
AD
3088 $new_version_details = check_for_update($link);
3089
3090 print "<param key=\"new_version_available\" value=\"".
3091 sprintf("%d", $new_version_details != ""). "\"/>";
3092
3093 $_SESSION["last_version_check"] = time();
3094 }
3095 }
3096
1c9df66e 3097// print "<param key=\"new_version_available\" value=\"1\"/>";
b4507bc2 3098
f54f515f
AD
3099 print "</runtime-info>";
3100 }
ef393de7 3101
88040f57 3102 function getSearchSql($search, $match_on) {
ef393de7 3103
88040f57 3104 $search_query_part = "";
e20c9d88 3105
88040f57
AD
3106 $keywords = split(" ", $search);
3107 $query_keywords = array();
e20c9d88 3108
88040f57 3109 if ($match_on == "both") {
e20c9d88 3110
88040f57
AD
3111 foreach ($keywords as $k) {
3112 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
3113 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
3114 }
e20c9d88 3115
88040f57 3116 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 3117
88040f57 3118 } else if ($match_on == "title") {
e20c9d88 3119
88040f57
AD
3120 foreach ($keywords as $k) {
3121 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
3122 }
e20c9d88 3123
88040f57 3124 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 3125
88040f57
AD
3126 } else if ($match_on == "content") {
3127
3128 foreach ($keywords as $k) {
3129 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
3130 }
3131 }
3132
3133 $search_query_part = implode("AND", $query_keywords);
3134
3135 return $search_query_part;
3136 }
3137
c36bf4d5
AD
3138 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) {
3139
3140 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
c1a0b534 3141
88040f57
AD
3142 if ($search) {
3143
3144 $search_query_part = getSearchSql($search, $match_on);
3145 $search_query_part .= " AND ";
e20c9d88 3146
ef393de7
AD
3147 } else {
3148 $search_query_part = "";
3149 }
3150
3151 $view_query_part = "";
3152
7b4d02a8 3153 if ($view_mode == "adaptive" || $view_query_part == "noscores") {
ef393de7
AD
3154 if ($search) {
3155 $view_query_part = " ";
3156 } else if ($feed != -1) {
f295c368 3157 $unread = getFeedUnread($link, $feed, $cat_view);
ef393de7
AD
3158 if ($unread > 0) {
3159 $view_query_part = " unread = true AND ";
3160 }
3161 }
3162 }
3163
3164 if ($view_mode == "marked") {
3165 $view_query_part = " marked = true AND ";
3166 }
3167
3168 if ($view_mode == "unread") {
3169 $view_query_part = " unread = true AND ";
3170 }
3171
3172 if ($limit > 0) {
3173 $limit_query_part = "LIMIT " . $limit;
3174 }
3175
3176 $vfeed_query_part = "";
3177
3178 // override query strategy and enable feed display when searching globally
3179 if ($search && $search_mode == "all_feeds") {
3180 $query_strategy_part = "ttrss_entries.id > 0";
3181 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3182 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
3183 $query_strategy_part = "ttrss_entries.id > 0";
3184 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
3185 id = feed_id) as feed_title,";
3186 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
3187
3188 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
0a6c4846
AD
3189
3190 $tmp_result = false;
3191
3192 if ($cat_view) {
3193 $tmp_result = db_query($link, "SELECT id
3194 FROM ttrss_feeds WHERE cat_id = '$feed'");
3195 } else {
3196 $tmp_result = db_query($link, "SELECT id
3197 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
3198 WHERE id = '$feed') AND id != '$feed'");
3199 }
ef393de7
AD
3200
3201 $cat_siblings = array();
3202
3203 if (db_num_rows($tmp_result) > 0) {
3204 while ($p = db_fetch_assoc($tmp_result)) {
3205 array_push($cat_siblings, "feed_id = " . $p["id"]);
3206 }
3207
3208 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
3209 $feed, implode(" OR ", $cat_siblings));
3210
3211 } else {
3212 $query_strategy_part = "ttrss_entries.id > 0";
3213 }
3214
3215 } else if ($feed >= 0) {
3216
3217 if ($cat_view) {
5c365f60 3218
ef393de7
AD
3219 if ($feed > 0) {
3220 $query_strategy_part = "cat_id = '$feed'";
3221 } else {
3222 $query_strategy_part = "cat_id IS NULL";
3223 }
3224
3225 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
5c365f60 3226
ef393de7
AD
3227 } else {
3228 $tmp_result = db_query($link, "SELECT id
3229 FROM ttrss_feeds WHERE parent_feed = '$feed'
3230 ORDER BY cat_id,title");
3231
3232 $parent_ids = array();
3233
3234 if (db_num_rows($tmp_result) > 0) {
3235 while ($p = db_fetch_assoc($tmp_result)) {
3236 array_push($parent_ids, "feed_id = " . $p["id"]);
3237 }
3238
3239 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
3240 $feed, implode(" OR ", $parent_ids));
3241
3242 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3243 } else {
3244 $query_strategy_part = "feed_id = '$feed'";
3245 }
3246 }
3247 } else if ($feed == -1) { // starred virtual feed
3248 $query_strategy_part = "marked = true";
3249 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
e4f4b46f
AD
3250 } else if ($feed == -2) { // published virtual feed
3251 $query_strategy_part = "published = true";
2d24f032
AD
3252 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3253 } else if ($feed == -3) { // fresh virtual feed
3254 $query_strategy_part = "unread = true";
3255
7a22dc2a 3256 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
c1d7e6c3 3257
2d24f032 3258 if (DB_TYPE == "pgsql") {
7608b38a 3259 $query_strategy_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
2d24f032 3260 } else {
7608b38a 3261 $query_strategy_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2d24f032
AD
3262 }
3263
e4f4b46f 3264 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
ef393de7
AD
3265 } else if ($feed <= -10) { // labels
3266 $label_id = -$feed - 11;
3267
3268 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
3269 WHERE id = '$label_id'");
3270
4bc311fc 3271 $query_strategy_part = "(" . db_fetch_result($tmp_result, 0, "sql_exp") . ")";
3de0261a
AD
3272
3273 if (!$query_strategy_part) {
3274 return false;
3275 }
3276
ef393de7
AD
3277 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3278 } else {
3279 $query_strategy_part = "id > 0"; // dumb
3280 }
d6e5706d 3281
7a22dc2a 3282 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
d6e5706d
AD
3283 $order_by = "updated";
3284 } else {
3285 $order_by = "updated DESC";
3286 }
e939722a 3287
7b4d02a8
AD
3288 if ($view_mode != "noscores") {
3289 $order_by = "score DESC, $order_by";
3290 }
48b0c4ec 3291
e939722a
AD
3292 if ($override_order) {
3293 $order_by = $override_order;
3294 }
ef393de7
AD
3295
3296 $feed_title = "";
3297
3298 if ($search && $search_mode == "all_feeds") {
b36e002f 3299 $feed_title = __("Search results")." ($search)";
ef393de7 3300 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
b36e002f 3301 $feed_title = __("Search results")." ($search, $feed)";
ef393de7
AD
3302 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
3303 $feed_title = $feed;
3304 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
3305
3306 if ($cat_view) {
5c365f60 3307
ef393de7
AD
3308 if ($feed != 0) {
3309 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
c36bf4d5 3310 WHERE id = '$feed' AND owner_uid = $owner_uid");
ef393de7
AD
3311 $feed_title = db_fetch_result($result, 0, "title");
3312 } else {
d1db26aa 3313 $feed_title = __("Uncategorized");
ef393de7 3314 }
e1eb2147
AD
3315
3316 if ($search) {
b36e002f 3317 $feed_title = __("Searched for")." $search ($feed_title)";
e1eb2147
AD
3318 }
3319
ef393de7
AD
3320 } else {
3321
3322 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
c36bf4d5 3323 WHERE id = '$feed' AND owner_uid = $owner_uid");
ef393de7
AD
3324
3325 $feed_title = db_fetch_result($result, 0, "title");
3326 $feed_site_url = db_fetch_result($result, 0, "site_url");
3327 $last_error = db_fetch_result($result, 0, "last_error");
e1eb2147
AD
3328
3329 if ($search) {
b36e002f 3330 $feed_title = __("Searched for") . " $search ($feed_title)";
e1eb2147 3331 }
ef393de7
AD
3332 }
3333
3334 } else if ($feed == -1) {
d1db26aa 3335 $feed_title = __("Starred articles");
6cfa22dc 3336 if ($search) { $feed_title = __("Searched for") . " $search ($feed_title)"; }
e4f4b46f
AD
3337 } else if ($feed == -2) {
3338 $feed_title = __("Published articles");
6cfa22dc 3339 if ($search) { $feed_title = __("Searched for") . " $search ($feed_title)"; }
2d24f032
AD
3340 } else if ($feed == -3) {
3341 $feed_title = __("Fresh articles");
6cfa22dc 3342 if ($search) { $feed_title = __("Searched for") . " $search ($feed_title)"; }
ef393de7
AD
3343 } else if ($feed < -10) {
3344 $label_id = -$feed - 11;
3345 $result = db_query($link, "SELECT description FROM ttrss_labels
3346 WHERE id = '$label_id'");
3347 $feed_title = db_fetch_result($result, 0, "description");
88040f57
AD
3348
3349 if ($search) {
b36e002f 3350 $feed_title = __("Searched for") . " $search ($feed_title)";
88040f57 3351 }
ef393de7
AD
3352 } else {
3353 $feed_title = "?";
3354 }
3355
ef393de7
AD
3356 if ($feed < -10) error_reporting (0);
3357
62129e67
AD
3358 $content_query_part = "content as content_preview,";
3359
ef393de7
AD
3360 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
3361
3362 if ($feed >= 0) {
3363 $feed_kind = "Feeds";
3364 } else {
3365 $feed_kind = "Labels";
3366 }
3367
95a82c08
AD
3368 if ($limit_query_part) {
3369 $offset_query_part = "OFFSET $offset";
3370 }
3371
d00f22ac 3372 if ($vfeed_query_part && get_pref($link, 'VFEED_GROUP_BY_FEED', $owner_uid)) {
6cfea5c7 3373 if (!$override_order) {
43fc671f
AD
3374 $order_by = "ttrss_feeds.title, $order_by";
3375 }
3376
a91c7082
AD
3377 // Special output for Fresh feed
3378
3379/* if ($feed == -3) {
43fc671f
AD
3380 $group_limit_part = "(select count(*) from
3381 ttrss_user_entries AS t1, ttrss_entries AS t2 where
3382 t1.ref_id = t2.id and t1.owner_uid = 2 and
3383 t1.feed_id = ttrss_user_entries.feed_id and
3384 t2.updated > ttrss_entries.updated) <= 5 AND";
a91c7082 3385} */
6cfea5c7
AD
3386 }
3387
ef393de7 3388 $query = "SELECT
1f64b1be 3389 guid,
ef393de7 3390 ttrss_entries.id,ttrss_entries.title,
46921916 3391 updated,
e4f4b46f 3392 unread,feed_id,marked,published,link,last_read,
fc2b26a6 3393 ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms,
ef393de7
AD
3394 $vfeed_query_part
3395 $content_query_part
fc2b26a6 3396 ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms,
ff6e357a 3397 author,score
ef393de7
AD
3398 FROM
3399 ttrss_entries,ttrss_user_entries,ttrss_feeds
3400 WHERE
43fc671f 3401 $group_limit_part
546499a9 3402 ttrss_feeds.hidden = false AND
ef393de7
AD
3403 ttrss_user_entries.feed_id = ttrss_feeds.id AND
3404 ttrss_user_entries.ref_id = ttrss_entries.id AND
c36bf4d5 3405 ttrss_user_entries.owner_uid = '$owner_uid' AND
ef393de7
AD
3406 $search_query_part
3407 $view_query_part
3408 $query_strategy_part ORDER BY $order_by
95a82c08 3409 $limit_query_part $offset_query_part";
4bc311fc 3410
ef393de7 3411 if ($_GET["debug"]) print $query;
4bc311fc
AD
3412
3413 $result = db_query($link, $query);
ef393de7
AD
3414
3415 } else {
3416 // browsing by tag
3417
3418 $feed_kind = "Tags";
3419
3420 $result = db_query($link, "SELECT
1f64b1be 3421 guid,
ef393de7 3422 ttrss_entries.id as id,title,
46921916 3423 updated,
ef393de7
AD
3424 unread,feed_id,
3425 marked,link,last_read,
fc2b26a6 3426 ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms,
ef393de7
AD
3427 $vfeed_query_part
3428 $content_query_part
4d0b3607
AD
3429 ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms,
3430 score
ef393de7
AD
3431 FROM
3432 ttrss_entries,ttrss_user_entries,ttrss_tags
3433 WHERE
546499a9 3434 ref_id = ttrss_entries.id AND
c36bf4d5 3435 ttrss_user_entries.owner_uid = '$owner_uid' AND
ef393de7
AD
3436 post_int_id = int_id AND tag_name = '$feed' AND
3437 $view_query_part
3438 $search_query_part
3439 $query_strategy_part ORDER BY $order_by
3440 $limit_query_part");
3441 }
3442
c7188969 3443 return array($result, $feed_title, $feed_site_url, $last_error);
ef393de7
AD
3444
3445 }
3446
c36bf4d5 3447 function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat,
3baeeeca 3448 $search, $search_mode, $match_on) {
18664970
AD
3449
3450 $qfh_ret = queryFeedHeadlines($link, $feed,
c36bf4d5
AD
3451 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC", 0,
3452 $owner_uid);
18664970
AD
3453
3454 $result = $qfh_ret[0];
59e2aab4 3455 $feed_title = htmlspecialchars($qfh_ret[1]);
18664970
AD
3456 $feed_site_url = $qfh_ret[2];
3457 $last_error = $qfh_ret[3];
3458
a5472764 3459// if (!$feed_site_url) $feed_site_url = "http://localhost/";
4bc64807 3460
a5472764
AD
3461 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>
3462 <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?>
3463 <rss version=\"2.0\">
3baeeeca
AD
3464 <channel>
3465 <title>$feed_title</title>
4bc64807
AD
3466 <link>$feed_site_url</link>
3467 <description>Feed generated by Tiny Tiny RSS</description>";
3baeeeca
AD
3468
3469 while ($line = db_fetch_assoc($result)) {
3470 print "<item>";
4bc64807 3471 print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>";
3baeeeca 3472 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
0c3d1c68 3473
bc976a8c 3474 $tags = get_article_tags($link, $line["id"], $owner_uid);
0c3d1c68
AD
3475
3476 foreach ($tags as $tag) {
3477 print "<category>" . htmlspecialchars($tag) . "</category>";
3478 }
3479
3baeeeca
AD
3480 $rfc822_date = date('r', strtotime($line["updated"]));
3481
3482 print "<pubDate>$rfc822_date</pubDate>";
3483
3484 print "<title>" .
3485 htmlspecialchars($line["title"]) . "</title>";
3486
0c3d1c68
AD
3487 print "<description><![CDATA[" .
3488 $line["content_preview"] . "]]></description>";
3baeeeca
AD
3489
3490 print "</item>";
3491 }
3492
3493 print "</channel></rss>";
18664970
AD
3494
3495 }
3496
0a6c4846
AD
3497 function getCategoryTitle($link, $cat_id) {
3498
bba7c4bf
AD
3499 if ($cat_id == -1) {
3500 return __("Special");
3501 } else if ($cat_id == -2) {
3502 return __("Labels");
0a6c4846 3503 } else {
bba7c4bf
AD
3504
3505 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
3506 id = '$cat_id'");
3507
3508 if (db_num_rows($result) == 1) {
3509 return db_fetch_result($result, 0, "title");
3510 } else {
3511 return "Uncategorized";
3512 }
0a6c4846
AD
3513 }
3514 }
3515
f738aef1
AD
3516 // http://ru2.php.net/strip-tags
3517
3518 function strip_tags_long($textstring, $allowed){
3519 while($textstring != strip_tags($textstring, $allowed))
3520 {
3521 while (strlen($textstring) != 0)
3522 {
3523 if (strlen($textstring) > 1024) {
3524 $otherlen = 1024;
3525 } else {
3526 $otherlen = strlen($textstring);
3527 }
3528 $temptext = strip_tags(substr($textstring,0,$otherlen), $allowed);
3529 $safetext .= $temptext;
3530 $textstring = substr_replace($textstring,'',0,$otherlen);
3531 }
3532 $textstring = $safetext;
3533 }
3534 return $textstring;
3535 }
3536
3537
1ac0baf4 3538 function sanitize_rss($link, $str, $force_strip_tags = false) {
60452879 3539 $res = $str;
183ad07b 3540
1ac0baf4 3541 if (get_pref($link, "STRIP_UNSAFE_TAGS") || $force_strip_tags) {
f738aef1 3542
7e1265ed 3543 $res = strip_tags_long($res,
4f376043 3544 "<p><a><i><em><b><strong><code><pre><blockquote><br><img><ul><ol><li>");
f738aef1
AD
3545
3546// $res = preg_replace("/\r\n|\n|\r/", "", $res);
3547// $res = strip_tags_long($res, "<p><a><i><em><b><strong><blockquote><br><img><div><span>");
f826eee1
AD
3548 }
3549
8dccabed
AD
3550 if (get_pref($link, "STRIP_IMAGES")) {
3551
3552 $res = preg_replace('/<img[^>]+>/is', '', $res);
3553
3554 }
3555
183ad07b
AD
3556 return $res;
3557 }
b72c3ef8 3558
45004d43
AD
3559 /**
3560 * Send by mail a digest of last articles.
3561 *
3562 * @param mixed $link The database connection.
3563 * @param integer $limit The maximum number of articles by digest.
3564 * @return boolean Return false if digests are not enabled.
3565 */
9cd7c995
AD
3566 function send_headlines_digests($link, $limit = 100) {
3567
1ddba275
AD
3568 if (!DIGEST_ENABLE) return false;
3569
9cd7c995 3570 $user_limit = DIGEST_EMAIL_LIMIT;
5430c959 3571 $days = 1;
9cd7c995
AD
3572
3573 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
3574
3575 if (DB_TYPE == "pgsql") {
3576 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
3577 } else if (DB_TYPE == "mysql") {
3578 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
3579 }
3580
3581 $result = db_query($link, "SELECT id,email FROM ttrss_users
3582 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
3583
3584 while ($line = db_fetch_assoc($result)) {
dc85be2b 3585
9cd7c995
AD
3586 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
3587 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
3588
dc85be2b
AD
3589 $do_catchup = get_pref($link, 'DIGEST_CATCHUP', $line['id'], false);
3590
9cd7c995
AD
3591 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
3592 $digest = $tuple[0];
3593 $headlines_count = $tuple[1];
dc85be2b 3594 $affected_ids = $tuple[2];
c62a2c21 3595 $digest_text = $tuple[3];
9cd7c995
AD
3596
3597 if ($headlines_count > 0) {
a8931123 3598
c62a2c21 3599 $mail = new PHPMailer();
a8931123 3600
c62a2c21
AD
3601 $mail->PluginDir = "phpmailer/";
3602 $mail->SetLanguage("en", "phpmailer/language/");
a8931123 3603
c62a2c21 3604 $mail->CharSet = "UTF-8";
a8931123 3605
c62a2c21
AD
3606 $mail->From = DIGEST_FROM_ADDRESS;
3607 $mail->FromName = DIGEST_FROM_NAME;
3608 $mail->AddAddress($line["email"], $line["login"]);
c7ddac5c 3609
c62a2c21 3610 if (DIGEST_SMTP_HOST) {
a8931123
AD
3611 $mail->Host = DIGEST_SMTP_HOST;
3612 $mail->Mailer = "smtp";
a8931123
AD
3613 $mail->Username = DIGEST_SMTP_LOGIN;
3614 $mail->Password = DIGEST_SMTP_PASSWORD;
c62a2c21 3615 }
a8931123 3616
c62a2c21 3617 $mail->IsHTML(true);
163a295e 3618 $mail->Subject = DIGEST_SUBJECT;
c62a2c21
AD
3619 $mail->Body = $digest;
3620 $mail->AltBody = $digest_text;
a8931123 3621
c62a2c21 3622 $rc = $mail->Send();
a8931123 3623
c62a2c21 3624 if (!$rc) print "ERROR: " . $mail->ErrorInfo;
a8931123 3625
9cd7c995 3626 print "RC=$rc\n";
a8931123 3627
c62a2c21 3628 if ($rc && $do_catchup) {
dc85be2b 3629 print "Marking affected articles as read...\n";
9968d46f 3630 catchupArticlesById($link, $affected_ids, 0, $line["id"]);
dc85be2b
AD
3631 }
3632
9cd7c995
AD
3633 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
3634 WHERE id = " . $line["id"]);
3635 } else {
3636 print "No headlines\n";
3637 }
3638 }
3639 }
3640
cedd3e89
AD
3641 print "All done.\n";
3642
9cd7c995
AD
3643 }
3644
7e3634d9 3645 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
c62a2c21
AD
3646
3647 require_once "MiniTemplator.class.php";
3648
3649 $tpl = new MiniTemplator;
3650 $tpl_t = new MiniTemplator;
3651
3652 $tpl->readTemplateFromFile("templates/digest_template_html.txt");
3653 $tpl_t->readTemplateFromFile("templates/digest_template.txt");
3654
3655 $tpl->setVariable('CUR_DATE', date('Y/m/d'));
3656 $tpl->setVariable('CUR_TIME', date('G:i'));
3657
3658 $tpl_t->setVariable('CUR_DATE', date('Y/m/d'));
3659 $tpl_t->setVariable('CUR_TIME', date('G:i'));
7e3634d9 3660
dc85be2b
AD
3661 $affected_ids = array();
3662
7e3634d9 3663 if (DB_TYPE == "pgsql") {
9cd7c995 3664 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
7e3634d9
AD
3665 } else if (DB_TYPE == "mysql") {
3666 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
3667 }
3668
3669 $result = db_query($link, "SELECT ttrss_entries.title,
3670 ttrss_feeds.title AS feed_title,
3671 date_entered,
dc85be2b 3672 ttrss_user_entries.ref_id,
7e3634d9 3673 link,
c62a2c21 3674 SUBSTRING(content, 1, 120) AS excerpt,
fc2b26a6 3675 ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
7e3634d9
AD
3676 FROM
3677 ttrss_user_entries,ttrss_entries,ttrss_feeds
3678 WHERE
3679 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
3dd9183c 3680 AND include_in_digest = true
7e3634d9 3681 AND $interval_query
a8931123 3682 AND hidden = false
448b0abd 3683 AND ttrss_user_entries.owner_uid = $user_id
c62a2c21
AD
3684 AND unread = true
3685 ORDER BY ttrss_feeds.title, date_entered DESC
7e3634d9
AD
3686 LIMIT $limit");
3687
3688 $cur_feed_title = "";
3689
9cd7c995
AD
3690 $headlines_count = db_num_rows($result);
3691
c62a2c21
AD
3692 $headlines = array();
3693
7e3634d9 3694 while ($line = db_fetch_assoc($result)) {
c62a2c21
AD
3695 array_push($headlines, $line);
3696 }
3697
3698 for ($i = 0; $i < sizeof($headlines); $i++) {
3699
3700 $line = $headlines[$i];
dc85be2b
AD
3701
3702 array_push($affected_ids, $line["ref_id"]);
3703
7e3634d9 3704 $updated = smart_date_time(strtotime($line["last_updated"]));
7e3634d9 3705
c62a2c21
AD
3706 $tpl->setVariable('FEED_TITLE', $line["feed_title"]);
3707 $tpl->setVariable('ARTICLE_TITLE', $line["title"]);
3708 $tpl->setVariable('ARTICLE_LINK', $line["link"]);
3709 $tpl->setVariable('ARTICLE_UPDATED', $updated);
163a295e
AD
3710 $tpl->setVariable('ARTICLE_EXCERPT',
3711 truncate_string(strip_tags($line["excerpt"]), 100));
7e3634d9 3712
c62a2c21
AD
3713 $tpl->addBlock('article');
3714
3715 $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]);
3716 $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]);
3717 $tpl_t->setVariable('ARTICLE_LINK', $line["link"]);
3718 $tpl_t->setVariable('ARTICLE_UPDATED', $updated);
3719// $tpl_t->setVariable('ARTICLE_EXCERPT',
3720// truncate_string(strip_tags($line["excerpt"]), 100));
3721
3722 $tpl_t->addBlock('article');
3723
3724 if ($headlines[$i]['feed_title'] != $headlines[$i+1]['feed_title']) {
3725 $tpl->addBlock('feed');
3726 $tpl_t->addBlock('feed');
7e3634d9
AD
3727 }
3728
7e3634d9
AD
3729 }
3730
c62a2c21
AD
3731 $tpl->addBlock('digest');
3732 $tpl->generateOutputToString($tmp);
3733
3734 $tpl_t->addBlock('digest');
3735 $tpl_t->generateOutputToString($tmp_t);
7e3634d9 3736
c62a2c21 3737 return array($tmp, $headlines_count, $affected_ids, $tmp_t);
7e3634d9
AD
3738 }
3739
d9fa39f1 3740 function check_for_update($link, $brief_fmt = true) {
b72c3ef8
AD
3741 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
3742
3743 if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) {
3744 return;
3745 }
3746
3747 error_reporting(0);
f67d9754
AD
3748 if (ENABLE_SIMPLEPIE) {
3749 $rss = new SimplePie();
3750 $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT);
4148e809 3751// $rss->set_timeout(MAGPIE_FETCH_TIME_OUT);
f67d9754
AD
3752 $rss->set_feed_url($fetch_url);
3753 $rss->set_output_encoding('UTF-8');
3754 $rss->init();
3755 } else {
3756 $rss = fetch_rss($releases_feed);
3757 }
b72c3ef8
AD
3758 error_reporting (DEFAULT_ERROR_LEVEL);
3759
3760 if ($rss) {
3761
f67d9754
AD
3762 if (ENABLE_SIMPLEPIE) {
3763 $items = $rss->get_items();
3764 } else {
3765 $items = $rss->items;
b72c3ef8 3766
f67d9754
AD
3767 if (!$items || !is_array($items)) $items = $rss->entries;
3768 if (!$items || !is_array($items)) $items = $rss;
3769 }
b72c3ef8 3770
da412ad3 3771 if (!is_array($items) || count($items) == 0) {
b72c3ef8 3772 return;
da412ad3 3773 }
b72c3ef8 3774
a41d2c65 3775 $latest_item = $items[0];
b72c3ef8 3776
f67d9754
AD
3777 if (ENABLE_SIMPLEPIE) {
3778 $last_title = $latest_item->get_title();
3779 } else {
3780 $last_title = $latest_item["title"];
3781 }
b72c3ef8 3782
f67d9754
AD
3783 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $last_title));
3784
3785 if (ENABLE_SIMPLEPIE) {
dcf7fd08
AD
3786 $release_url = sanitize_rss($link, $latest_item->get_link());
3787 $content = sanitize_rss($link, $latest_item->get_description());
f67d9754
AD
3788 } else {
3789 $release_url = sanitize_rss($link, $latest_item["link"]);
3790 $content = sanitize_rss($link, $latest_item["description"]);
3791 }
48e1a342 3792
a41d2c65 3793 if (version_compare(VERSION, $latest_version) == -1) {
d9fa39f1 3794 if ($brief_fmt) {
0d32b41e 3795 return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\">
d9fa39f1 3796 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
0d32b41e 3797 <div id=\"milestoneDetails\">$content</div>");
d9fa39f1 3798 } else {
92625568
AD
3799 return "New version of Tiny-Tiny RSS ($latest_version) is available:
3800 <div class='milestoneDetails'>$content</div>
e944346c 3801 Visit <a target=\"_blank\" href=\"http://tt-rss.spb.ru/\">official site</a> for
92625568 3802 download and update information.";
d9fa39f1
AD
3803 }
3804
da412ad3 3805 }
b72c3ef8
AD
3806 }
3807 }
472782e8 3808
18eddb2c
AD
3809 function markArticlesById($link, $ids, $cmode) {
3810
3811 $tmp_ids = array();
3812
3813 foreach ($ids as $id) {
3814 array_push($tmp_ids, "ref_id = '$id'");
3815 }
3816
3817 $ids_qpart = join(" OR ", $tmp_ids);
3818
3819 if ($cmode == 0) {
3820 db_query($link, "UPDATE ttrss_user_entries SET
3821 marked = false,last_read = NOW()
3822 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3823 } else if ($cmode == 1) {
3824 db_query($link, "UPDATE ttrss_user_entries SET
3825 marked = true
3826 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3827 } else {
3828 db_query($link, "UPDATE ttrss_user_entries SET
3829 marked = NOT marked,last_read = NOW()
3830 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3831 }
3832 }
3833
e4f4b46f
AD
3834 function publishArticlesById($link, $ids, $cmode) {
3835
3836 $tmp_ids = array();
3837
3838 foreach ($ids as $id) {
3839 array_push($tmp_ids, "ref_id = '$id'");
3840 }
3841
3842 $ids_qpart = join(" OR ", $tmp_ids);
3843
3844 if ($cmode == 0) {
3845 db_query($link, "UPDATE ttrss_user_entries SET
3846 published = false,last_read = NOW()
3847 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3848 } else if ($cmode == 1) {
3849 db_query($link, "UPDATE ttrss_user_entries SET
3850 published = true
3851 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3852 } else {
3853 db_query($link, "UPDATE ttrss_user_entries SET
3854 published = NOT published,last_read = NOW()
3855 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3856 }
3857 }
3858
9968d46f
AD
3859 function catchupArticlesById($link, $ids, $cmode, $owner_uid = false) {
3860
3861 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
472782e8
AD
3862
3863 $tmp_ids = array();
3864
3865 foreach ($ids as $id) {
3866 array_push($tmp_ids, "ref_id = '$id'");
3867 }
3868
3869 $ids_qpart = join(" OR ", $tmp_ids);
3870
3871 if ($cmode == 0) {
3872 db_query($link, "UPDATE ttrss_user_entries SET
3873 unread = false,last_read = NOW()
9968d46f 3874 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8
AD
3875 } else if ($cmode == 1) {
3876 db_query($link, "UPDATE ttrss_user_entries SET
3877 unread = true
9968d46f 3878 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8
AD
3879 } else {
3880 db_query($link, "UPDATE ttrss_user_entries SET
3881 unread = NOT unread,last_read = NOW()
9968d46f 3882 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8
AD
3883 }
3884 }
3885
e097e8be
AD
3886 function catchupArticleById($link, $id, $cmode) {
3887
3888 if ($cmode == 0) {
3889 db_query($link, "UPDATE ttrss_user_entries SET
3890 unread = false,last_read = NOW()
3891 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3892 } else if ($cmode == 1) {
3893 db_query($link, "UPDATE ttrss_user_entries SET
3894 unread = true
3895 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3896 } else {
3897 db_query($link, "UPDATE ttrss_user_entries SET
3898 unread = NOT unread,last_read = NOW()
3899 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3900 }
3901 }
3902
1f64b1be
AD
3903 function make_guid_from_title($title) {
3904 return preg_replace("/[ \"\',.:;]/", "-",
fefef828 3905 mb_strtolower(strip_tags($title), 'utf-8'));
1f64b1be
AD
3906 }
3907
11befbb2
AD
3908 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
3909 $bottom = false, $rtl_content = false, $feed_id = 0,
3910 $is_cat = false, $search = false, $match_on = false,
1681df97
AD
3911 $search_mode = false, $offset = 0, $limit = 0,
3912 $dashboard_menu = 0, $disable_feed = 0, $feed_small_icon = 0) {
11befbb2 3913
e6c115b2
AD
3914 $user_page_offset = $offset + 1;
3915
11befbb2
AD
3916 if (!$bottom) {
3917 $class = "headlinesSubToolbar";
3918 $tid = "headlineActionsTop";
3919 } else {
3920 $class = "headlinesSubToolbar";
3921 $tid = "headlineActionsBottom";
3922 }
3923
ecf2a265 3924 print "<nobr><table class=\"$class\" id=\"$tid\"
11befbb2
AD
3925 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
3926
3927 if ($rtl_content) {
3928 $rtl_cpart = "RTL";
3929 } else {
3930 $rtl_cpart = "";
3931 }
3932
e6c115b2
AD
3933 $page_prev_link = "javascript:viewFeedGoPage(-1)";
3934 $page_next_link = "javascript:viewFeedGoPage(1)";
3935 $page_first_link = "javascript:viewFeedGoPage(0)";
203de776 3936
eb28b131
AD
3937 $catchup_page_link = "javascript:catchupPage()";
3938 $catchup_feed_link = "javascript:catchupCurrentFeed()";
a5ae125a 3939 $catchup_sel_link = "javascript:catchupSelection()";
c6008b62 3940
11befbb2
AD
3941 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3942
c6008b62
AD
3943 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
3944 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
3945 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
e75d70b5 3946 $sel_inv_link = "javascript:invertHeadlineSelection()";
11befbb2 3947
c6008b62
AD
3948 $tog_unread_link = "javascript:selectionToggleUnread()";
3949 $tog_marked_link = "javascript:selectionToggleMarked()";
e4f4b46f 3950 $tog_published_link = "javascript:selectionTogglePublished()";
11befbb2 3951
c6008b62 3952 } else {
11befbb2 3953
c6008b62
AD
3954 $sel_all_link = "javascript:cdmSelectArticles('all')";
3955 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
3956 $sel_none_link = "javascript:cdmSelectArticles('none')";
3957
e75d70b5
AD
3958 $sel_inv_link = "javascript:invertHeadlineSelection()";
3959
c6008b62
AD
3960 $tog_unread_link = "javascript:selectionToggleUnread(true)";
3961 $tog_marked_link = "javascript:selectionToggleMarked(true)";
e4f4b46f 3962 $tog_published_link = "javascript:selectionTogglePublished(true)";
c6008b62
AD
3963
3964 }
3965
1681df97 3966 if (!$dashboard_menu) {
c6008b62 3967
1681df97 3968 if (strpos($_SESSION["client.userAgent"], "MSIE") === false) {
6cfa22dc 3969
1681df97 3970 print "<td class=\"headlineActions$rtl_cpart\">
6cfa22dc
AD
3971 <ul class=\"headlineDropdownMenu\">
3972 <li class=\"top2\">
3973 ".__('Select:')."
3974 <a href=\"$sel_all_link\">".__('All')."</a>,
3975 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
e75d70b5 3976 <a href=\"$sel_inv_link\">".__('Invert')."</a>,
6cfa22dc
AD
3977 <a href=\"$sel_none_link\">".__('None')."</a></li>
3978 <li class=\"vsep\">&nbsp;</li>
3979 <li class=\"top\">".__('Actions...')."<ul>
3980 <li><span class=\"insensitive\">".__('Selection toggle:')."</span></li>
3981 <li onclick=\"$tog_unread_link\">&nbsp;&nbsp;".__('Unread')."</li>
3982 <li onclick=\"$tog_marked_link\">&nbsp;&nbsp;".__('Starred')."</li>
3983 <li onclick=\"$tog_published_link\">&nbsp;&nbsp;".__('Published')."</li>
eee80a87 3984 <li><span class=\"insensitive\">--------</span></li>
6cfa22dc
AD
3985 <li><span class=\"insensitive\">".__('Mark as read:')."</span></li>
3986 <li onclick=\"$catchup_sel_link\">&nbsp;&nbsp;".__('Selection')."</li>";
3987
6cfa22dc
AD
3988/* if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3989
3990 print "
3991 <li onclick=\"catchupRelativeToArticle(0)\">&nbsp;&nbsp;".__("Above active article")."</li>
3992 <li onclick=\"catchupRelativeToArticle(1)\">&nbsp;&nbsp;".__("Below active article")."</li>";
3993 } else {
3994 print "
3995 <li><span class=\"insensitive\">&nbsp;&nbsp;".__("Above active article")."</span></li>
3996 <li><span class=\"insensitive\">&nbsp;&nbsp;".__("Below active article")."</span></li>";
3997
3998 } */
3999
4000 print "<li onclick=\"$catchup_feed_link\">&nbsp;&nbsp;".__('Entire feed')."</li>";
4001
eee80a87 4002 print "<li><span class=\"insensitive\">--------</span></li>";
6cfa22dc
AD
4003 print "<li><span class=\"insensitive\">".__('Other actions:')."</span></li>";
4004
4005
4006 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
4007 print "
4008 <li onclick=\"javascript:labelFromSearch('$search', '$search_mode',
4009 '$match_on', '$feed_id', '$is_cat');\">&nbsp;&nbsp;
4010 ".__('Search to label')."</li>";
4011 } else {
4012 print "<li><span class=\"insensitive\">&nbsp;&nbsp;".__('Search to label')."</li>";
4013
4014 }
4015
4016 print "</ul></li></ul>";
ff284aa0 4017 print "</td>";
1681df97
AD
4018
4019 } else {
ff284aa0 4020 // old style subtoolbar:
1681df97
AD
4021
4022 print "<td class=\"headlineActions$rtl_cpart\">".
4023 __('Select:')."
4024 <a href=\"$sel_all_link\">".__('All')."</a>,
4025 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
4026 <a href=\"$sel_none_link\">".__('None')."</a>
4027 &nbsp;&nbsp;".
4028 __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
4029 <a href=\"$tog_marked_link\">".__('Starred')."</a>
4030 &nbsp;&nbsp;".
4031 __('Mark as read:')."
4032 <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
4033 <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
4034
d420f2ee 4035 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
1681df97
AD
4036
4037 print "&nbsp;&nbsp;
4038 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
4039 '$match_on', '$feed_id', '$is_cat');\">
4040 ".__('Convert to label')."</a>";
d420f2ee 4041 }
1681df97
AD
4042
4043 print "</td>";
4044
d420f2ee 4045 }
1681df97
AD
4046 } else { // dashboard menu actions
4047
ff284aa0
AD
4048 // not implemented
4049 print "</td>";
2dd2c13b 4050 }
c6008b62 4051
11befbb2 4052 print "<td class=\"headlineTitle$rtl_cpart\">";
11befbb2 4053
7a822893 4054 print "<span id=\"subtoolbar_search\"
abe6d934 4055 style=\"display : none\"><input
7a822893
AD
4056 id=\"subtoolbar_search_box\"
4057 onblur=\"javascript:enableHotkeys();\"
4058 onfocus=\"javascript:disableHotkeys();\"
4059 onchange=\"subtoolbarSearch()\"
4060 onkeyup=\"subtoolbarSearch()\" type=\"search\"></span>";
4061
4062 print "<span id=\"subtoolbar_ftitle\">";
11befbb2 4063
b27967ac
AD
4064 if ($feed_site_url) {
4065 if (!$bottom) {
e944346c 4066 $target = "target=\"_blank\"";
20361063 4067 }
b27967ac
AD
4068 print "<a $target href=\"$feed_site_url\">".
4069 truncate_string($feed_title,30)."</a>";
4070 } else {
4071 print $feed_title;
4072 }
4073
4074 if ($search) {
4075 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
e6c115b2
AD
4076 }
4077
b27967ac
AD
4078 if ($user_page_offset > 1) {
4079 print " [$user_page_offset] ";
4080 }
4081
1681df97 4082 if (!$bottom && !$disable_feed) {
e6c115b2 4083 print "
e944346c 4084 <a target=\"_blank\"
11befbb2
AD
4085 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
4086 <img class=\"noborder\"
1025ad87 4087 alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
11befbb2 4088 </a>";
1681df97
AD
4089 } else if ($feed_small_icon) {
4090 print "<img class=\"noborder\" alt=\"\" src=\"images/$feed_small_icon\">";
11befbb2 4091 }
7a822893
AD
4092
4093 print "</span>";
4094
11befbb2 4095 print "</td>";
ecf2a265 4096 print "</tr></table></nobr>";
11befbb2
AD
4097
4098 }
4099
bba7c4bf
AD
4100 function printCategoryHeader($link, $cat_id, $hidden = false, $can_browse = true) {
4101
4102 $tmp_category = getCategoryTitle($link, $cat_id);
4103 $cat_unread = getCategoryUnread($link, $cat_id);
4104
4105 if ($hidden) {
4106 $holder_style = "display:none;";
66a251f9 4107 $ellipsis = "…";
bba7c4bf
AD
4108 } else {
4109 $holder_style = "";
4110 $ellipsis = "";
4111 }
4112
4113 $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
4114
bba7c4bf 4115 if ($can_browse) {
4c41e58f
AD
4116 $browse_cat_link = "onclick=\"javascript:viewCategory($cat_id)\"";
4117 $inner_title_class = "catTitle";
bba7c4bf 4118 } else {
4c41e58f
AD
4119 $browse_cat_link = "";
4120 $inner_title_class = "catTitleNL";
bba7c4bf
AD
4121 }
4122
364e391e
AD
4123 if ($cat_id > 0) {
4124 $cat_class = "feedCat";
4125 } else {
4126 $cat_class = "virtCat";
4127 }
4128
4129 print "<li class=\"$cat_class\" id=\"FCAT-$cat_id\">
98fb6193 4130 <img onclick=\"toggleCollapseCat($cat_id)\" class=\"catCollapse\"
4c41e58f
AD
4131 title=\"".__('Click to collapse category')."\"
4132 src=\"images/cat-collapse.png\"><span class=\"$inner_title_class\"
4133 id=\"FCATN-$cat_id\" $browse_cat_link
4134 \">$tmp_category</span>";
4135
4136 print "<span id=\"FCAP-$cat_id\">";
4137
dda1396f 4138 print " <span id=\"FCATCTR-$cat_id\"
bba7c4bf
AD
4139 class=\"$catctr_class\">($cat_unread)</span> $ellipsis";
4140
4c41e58f 4141 print "</span>";
bba7c4bf 4142
782ddd70 4143 //print "</li>";
bba7c4bf 4144
60ea2377 4145 print "<ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
7abee14f 4146
bba7c4bf
AD
4147 }
4148
f407c086
AD
4149 function outputFeedList($link, $tags = false) {
4150
3bd9a780 4151 print "<ul class=\"feedList\" id=\"feedList\">";
f407c086
AD
4152
4153 $owner_uid = $_SESSION["uid"];
4154
cf4d339c 4155 /* virtual feeds */
f407c086 4156
cf4d339c 4157 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3b086176
AD
4158
4159 if ($_COOKIE["ttrss_vf_vclps"] == 1) {
bba7c4bf 4160 $cat_hidden = true;
3b086176 4161 } else {
bba7c4bf 4162 $cat_hidden = false;
3b086176
AD
4163 }
4164
bba7c4bf 4165 printCategoryHeader($link, -1, $cat_hidden, false);
cf4d339c 4166 }
f407c086 4167
d96b7774
AD
4168 if (defined('_ENABLE_DASHBOARD')) {
4169 printFeedEntry(-4, "virt", __("Dashboard"), 0,
4170 "images/tag.png", $link);
4171 }
4172
cf4d339c 4173 $num_starred = getFeedUnread($link, -1);
e4f4b46f 4174 $num_published = getFeedUnread($link, -2);
2d24f032
AD
4175 $num_fresh = getFeedUnread($link, -3);
4176
4177 $class = "virt";
4178
4179 if ($num_fresh > 0) $class .= "Unread";
4180
4181 printFeedEntry(-3, $class, __("Fresh articles"), $num_fresh,
4182 "images/fresh.png", $link);
f407c086 4183
cf4d339c 4184 $class = "virt";
f407c086 4185
cf4d339c 4186 if ($num_starred > 0) $class .= "Unread";
f407c086 4187
abd8a516
AD
4188 $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false);
4189
4190 if ($is_ie) {
4191 $mark_img_ext = "gif";
4192 } else {
4193 $mark_img_ext = "png";
4194 }
4195
d1db26aa 4196 printFeedEntry(-1, $class, __("Starred articles"), $num_starred,
abd8a516 4197 "images/mark_set.$mark_img_ext", $link);
f407c086 4198
e4f4b46f
AD
4199 $class = "virt";
4200
4201 if ($num_published > 0) $class .= "Unread";
4202
4203 printFeedEntry(-2, $class, __("Published articles"), $num_published,
f5e0338d 4204 "images/pub_set.gif", $link);
e4f4b46f 4205
cf4d339c 4206 if (get_pref($link, 'ENABLE_FEED_CATS')) {
8b803aa2 4207 print "</ul></li>";
cf4d339c 4208 }
f407c086 4209
cf4d339c 4210 if (!$tags) {
f407c086
AD
4211
4212 if (GLOBAL_ENABLE_LABELS && get_pref($link, 'ENABLE_LABELS')) {
4213
4214 $result = db_query($link, "SELECT id,sql_exp,description FROM
4215 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
4216
4217 if (db_num_rows($result) > 0) {
4218 if (get_pref($link, 'ENABLE_FEED_CATS')) {
bd64489f
AD
4219
4220 if ($_COOKIE["ttrss_vf_lclps"] == 1) {
bba7c4bf 4221 $cat_hidden = true;
bd64489f 4222 } else {
bba7c4bf 4223 $cat_hidden = false;
bd64489f
AD
4224 }
4225
bba7c4bf 4226 printCategoryHeader($link, -2, $cat_hidden, false);
bd64489f 4227
f407c086 4228 } else {
3bd9a780 4229 print "<li><hr></li>";
f407c086
AD
4230 }
4231 }
4232
4233 while ($line = db_fetch_assoc($result)) {
4234
4235 error_reporting (0);
4236
4237 $label_id = -$line['id'] - 11;
4238 $count = getFeedUnread($link, $label_id);
4239
4240 $class = "label";
4241
4242 if ($count > 0) {
4243 $class .= "Unread";
4244 }
4245
4246 error_reporting (DEFAULT_ERROR_LEVEL);
4247
4248 printFeedEntry($label_id,
47439031 4249 $class, $line["description"],
f407c086
AD
4250 $count, "images/label.png", $link);
4251
4252 }
4253
4254 if (db_num_rows($result) > 0) {
4255 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4256 print "</ul>";
4257 }
4258 }
4259
4260 }
4261
4262 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
3bd9a780 4263 print "<li><hr></li>";
f407c086
AD
4264 }
4265
4266 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4267 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
9d393c84 4268 $order_by_qpart = "order_id,category,unread DESC,title";
f407c086 4269 } else {
9d393c84 4270 $order_by_qpart = "order_id,category,title";
f407c086
AD
4271 }
4272 } else {
4273 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
4274 $order_by_qpart = "unread DESC,title";
4275 } else {
4276 $order_by_qpart = "title";
4277 }
4278 }
4279
14073c0a
AD
4280 $age_qpart = getMaxAgeSubquery();
4281
99509451 4282 $query = "SELECT ttrss_feeds.*,
fc2b26a6 4283 ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated_noms,
f407c086
AD
4284 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
4285 WHERE feed_id = ttrss_feeds.id AND unread = true
14073c0a 4286 AND $age_qpart
f407c086
AD
4287 AND ttrss_user_entries.ref_id = ttrss_entries.id
4288 AND owner_uid = '$owner_uid') as unread,
4289 cat_id,last_error,
4290 ttrss_feed_categories.title AS category,
4291 ttrss_feed_categories.collapsed
4292 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
4293 ON (ttrss_feed_categories.id = cat_id)
4294 WHERE
4295 ttrss_feeds.hidden = false AND
4296 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
99509451
AD
4297 ORDER BY $order_by_qpart";
4298
4299 $result = db_query($link, $query);
f407c086
AD
4300
4301 $actid = $_GET["actid"];
4302
4303 /* real feeds */
4304
4305 $lnum = 0;
4306
4307 $total_unread = 0;
4308
4309 $category = "";
4310
4311 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
4312
4313 while ($line = db_fetch_assoc($result)) {
4314
47439031 4315 $feed = trim($line["title"]);
0f39ae20
AD
4316
4317 if (!$feed) $feed = "[Untitled]";
4318
f407c086
AD
4319 $feed_id = $line["id"];
4320
4321 $subop = $_GET["subop"];
4322
4323 $unread = $line["unread"];
4324
4325 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
4326 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
4327 } else {
4328 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
4329 }
4330
4331 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
4332
4333 if ($rtl_content) {
4334 $rtl_tag = "dir=\"RTL\"";
4335 } else {
4336 $rtl_tag = "";
4337 }
4338
4339 $tmp_result = db_query($link,
4340 "SELECT id,COUNT(unread) AS unread
4341 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
4342 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
4343 WHERE parent_feed = '$feed_id' AND unread = true
4344 GROUP BY ttrss_feeds.id");
4345
4346 if (db_num_rows($tmp_result) > 0) {
4347 while ($l = db_fetch_assoc($tmp_result)) {
4348 $unread += $l["unread"];
4349 }
4350 }
4351
4352 $cat_id = $line["cat_id"];
4353
4354 $tmp_category = $line["category"];
4355
4356 if (!$tmp_category) {
d1db26aa 4357 $tmp_category = __("Uncategorized");
f407c086
AD
4358 }
4359
4360 // $class = ($lnum % 2) ? "even" : "odd";
4361
4362 if ($line["last_error"]) {
4363 $class = "error";
4364 } else {
4365 $class = "feed";
4366 }
4367
4368 if ($unread > 0) $class .= "Unread";
4369
4370 if ($actid == $feed_id) {
4371 $class .= "Selected";
4372 }
4373
4374 $total_unread += $unread;
4375
4376 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
4377
4378 if ($category) {
8b803aa2 4379 print "</ul></li>";
f407c086
AD
4380 }
4381
4382 $category = $tmp_category;
4383
7abee14f 4384 $collapsed = sql_bool_to_bool($line["collapsed"]);
f407c086
AD
4385
4386 // workaround for NULL category
d1db26aa 4387 if ($category == __("Uncategorized")) {
f407c086
AD
4388 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
4389 $collapsed = "t";
4390 }
4391 }
4392
f407c086
AD
4393 $cat_id = sprintf("%d", $cat_id);
4394
7abee14f
AD
4395 printCategoryHeader($link, $cat_id, $collapsed, true);
4396
f407c086
AD
4397 }
4398
4399 printFeedEntry($feed_id, $class, $feed, $unread,
68dcbd31 4400 ICONS_URL."/$feed_id.ico", $link, $rtl_content,
f407c086
AD
4401 $last_updated, $line["last_error"]);
4402
4403 ++$lnum;
4404 }
4405
4406 if (db_num_rows($result) == 0) {
3bd9a780 4407 print "<li>".__('No feeds to display.')."</li>";
f407c086
AD
4408 }
4409
4410 } else {
4411
4412 // tags
4413
4414/* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
4415 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
4416 post_int_id = ttrss_user_entries.int_id AND
4417 unread = true AND ref_id = ttrss_entries.id
4418 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
4419 UNION
4420 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
4421 ORDER BY tag_name"); */
4422
4423 if (get_pref($link, 'ENABLE_FEED_CATS')) {
d1db26aa 4424 print "<li class=\"feedCat\">".__('Tags')."</li>";
60ea2377 4425 print "<ul class=\"feedCatList\">";
f407c086
AD
4426 }
4427
14073c0a
AD
4428 $age_qpart = getMaxAgeSubquery();
4429
f407c086 4430 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
14073c0a
AD
4431 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
4432 AND ref_id = id AND $age_qpart
f407c086 4433 AND unread = true)) AS count FROM ttrss_tags
ef1ac7c7
AD
4434 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
4435 ORDER BY count DESC LIMIT 50");
f407c086
AD
4436
4437 $tags = array();
4438
4439 while ($line = db_fetch_assoc($result)) {
4440 $tags[$line["tag_name"]] += $line["count"];
4441 }
4442
4443 foreach (array_keys($tags) as $tag) {
4444
4445 $unread = $tags[$tag];
4446
4447 $class = "tag";
4448
4449 if ($unread > 0) {
4450 $class .= "Unread";
4451 }
4452
4453 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
4454
4455 }
4456
4457 if (db_num_rows($result) == 0) {
4458 print "<li>No tags to display.</li>";
4459 }
4460
4461 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3bd9a780 4462 print "</ul>";
f407c086
AD
4463 }
4464
4465 }
4466
4467 print "</ul>";
4468
4469 }
4470
bc976a8c 4471 function get_article_tags($link, $id, $owner_uid = 0) {
0b126ac2
AD
4472
4473 $a_id = db_escape_string($id);
4474
bc976a8c
AD
4475 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
4476
0c3d1c68
AD
4477 $tmp_result = db_query($link, "SELECT DISTINCT tag_name,
4478 owner_uid as owner FROM
0b126ac2 4479 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
bc976a8c 4480 ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name");
0b126ac2
AD
4481
4482 $tags = array();
4483
4484 while ($tmp_line = db_fetch_assoc($tmp_result)) {
4485 array_push($tags, $tmp_line["tag_name"]);
4486 }
4487
4488 return $tags;
4489 }
4490
d62a3b63
AD
4491 function trim_value(&$value) {
4492 $value = trim($value);
4493 }
4494
4495 function trim_array($array) {
4496 $tmp = $array;
4497 array_walk($tmp, 'trim_value');
4498 return $tmp;
4499 }
4500
be832a1a 4501 function tag_is_valid($tag) {
ef063748
AD
4502 if ($tag == '') return false;
4503 if (preg_match("/^[0-9]*$/", $tag)) return false;
4504
4505 $tag = iconv("utf-8", "utf-8", $tag);
4506 if (!$tag) return false;
4507
4508 return true;
be832a1a
AD
4509 }
4510
793185a9
AD
4511 function render_login_form($link, $mobile = false) {
4512 if (!$mobile) {
4513 require_once "login_form.php";
4514 } else {
4515 require_once "mobile/login_form.php";
4516 }
01a87dff
AD
4517 }
4518
dc56b3b7
AD
4519 // from http://developer.apple.com/internet/safari/faq.html
4520 function no_cache_incantation() {
4521 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
4522 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
4523 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
4524 header("Cache-Control: post-check=0, pre-check=0", false);
4525 header("Pragma: no-cache"); // HTTP/1.0
4526 }
4527
42395d28
AD
4528 function format_warning($msg, $id = "") {
4529 return "<div class=\"warning\" id=\"$id\">
e780d1d2 4530 <img src=\"images/sign_excl.gif\">$msg</div>";
0d32b41e
AD
4531 }
4532
4533 function format_notice($msg) {
4534 return "<div class=\"notice\">
e780d1d2 4535 <img src=\"images/sign_info.gif\">$msg</div>";
0d32b41e
AD
4536 }
4537
68d2f95e
AD
4538 function format_error($msg) {
4539 return "<div class=\"error\">
e780d1d2 4540 <img src=\"images/sign_excl.gif\">$msg</div>";
68d2f95e
AD
4541 }
4542
4dccf1ed
AD
4543 function print_notice($msg) {
4544 return print format_notice($msg);
4545 }
4546
4547 function print_warning($msg) {
4548 return print format_warning($msg);
4549 }
4550
68d2f95e
AD
4551 function print_error($msg) {
4552 return print format_error($msg);
4553 }
4554
4555
4dccf1ed
AD
4556 function T_sprintf() {
4557 $args = func_get_args();
4558 return vsprintf(__(array_shift($args)), $args);
4559 }
4560
eedfb635
AD
4561 function outputArticleXML($link, $id, $feed_id, $mark_as_read = true,
4562 $zoom_mode = false) {
3de0261a 4563
10eb9da8
AD
4564 /* we can figure out feed_id from article id anyway, why do we
4565 * pass feed_id here? */
4566
4567 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
4568 WHERE ref_id = '$id'");
4569
4570 $feed_id = db_fetch_result($result, 0, "feed_id");
4571
eedfb635 4572 if (!$zoom_mode) { print "<article id='$id'><![CDATA["; };
3de0261a
AD
4573
4574 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
4575 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
4576
4577 if (db_num_rows($result) == 1) {
4578 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
4579 } else {
4580 $rtl_content = false;
4581 }
4582
4583 if ($rtl_content) {
4584 $rtl_tag = "dir=\"RTL\"";
4585 $rtl_class = "RTL";
4586 } else {
4587 $rtl_tag = "";
4588 $rtl_class = "";
4589 }
4590
4591 if ($mark_as_read) {
4592 $result = db_query($link, "UPDATE ttrss_user_entries
4593 SET unread = false,last_read = NOW()
4594 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
4595 }
4596
4597 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
fc2b26a6 4598 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
3de0261a
AD
4599 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
4600 num_comments,
4601 author
4602 FROM ttrss_entries,ttrss_user_entries
4603 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
4604
4605 if ($result) {
4606
4607 $link_target = "";
4608
4609 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
e944346c 4610 $link_target = "target=\"_blank\"";
3de0261a
AD
4611 }
4612
4613 $line = db_fetch_assoc($result);
4614
4615 if ($line["icon_url"]) {
4616 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
4617 } else {
4618 $feed_icon = "&nbsp;";
4619 }
4620
4621/* if ($line["comments"] && $line["link"] != $line["comments"]) {
4622 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
4623 } else {
4624 $entry_comments = "";
4625 } */
4626
4627 $num_comments = $line["num_comments"];
4628 $entry_comments = "";
4629
4630 if ($num_comments > 0) {
4631 if ($line["comments"]) {
4632 $comments_url = $line["comments"];
4633 } else {
4634 $comments_url = $line["link"];
4635 }
4636 $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
4637 } else {
4638 if ($line["comments"] && $line["link"] != $line["comments"]) {
4639 $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
4640 }
4641 }
4642
eedfb635
AD
4643 if ($zoom_mode) {
4644 header("Content-Type: text/html");
4645 print "<html><head>
5bb0cc8e 4646 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
eedfb635
AD
4647 <title>Tiny Tiny RSS - ".$line["title"]."</title>
4648 <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss.css\">
4649 </head><body>";
4650 }
4651
4652
3de0261a
AD
4653 print "<div class=\"postReply\">";
4654
94047498
AD
4655 print "<div class=\"postHeader\" onmouseover=\"enable_resize(true)\"
4656 onmouseout=\"enable_resize(false)\">";
3de0261a
AD
4657
4658 $entry_author = $line["author"];
4659
4660 if ($entry_author) {
60164936 4661 $entry_author = __(" - ") . $entry_author;
3de0261a
AD
4662 }
4663
4664 $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
4665 strtotime($line["updated"]));
4666
4667 print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
4668
4669 if ($line["link"]) {
4670 print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" .
06202d88 4671 $line["title"] . "</a><span class='author'>$entry_author</span></div>";
3de0261a
AD
4672 } else {
4673 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
4674 }
4675
9cfd409d 4676/* $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
3de0261a 4677 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
9cfd409d
AD
4678 ORDER BY tag_name"); */
4679
4680 $tags = get_article_tags($link, $id);
3de0261a
AD
4681
4682 $tags_str = "";
eedfb635 4683 $tags_nolinks_str = "";
3de0261a
AD
4684 $f_tags_str = "";
4685
4686 $num_tags = 0;
4687
20361063
AD
4688 if ($_SESSION["theme"] == "3pane") {
4689 $tag_limit = 3;
4690 } else {
4691 $tag_limit = 6;
4692 }
4693
9cfd409d 4694 foreach ($tags as $tag) {
3de0261a 4695 $num_tags++;
14b6c54b
AD
4696 $tag_escaped = str_replace("'", "\\'", $tag);
4697
4698 $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, ";
3de0261a 4699
20361063 4700 if ($num_tags == $tag_limit) {
66a251f9 4701 $tags_str .= "&hellip;";
eedfb635 4702 $tags_nolinks_str .= "&hellip;";
e7544143 4703
20361063 4704 } else if ($num_tags < $tag_limit) {
3de0261a 4705 $tags_str .= $tag_str;
eedfb635 4706 $tags_nolinks_str .= "$tag, ";
3de0261a
AD
4707 }
4708 $f_tags_str .= $tag_str;
4709 }
4710
4711 $tags_str = preg_replace("/, $/", "", $tags_str);
eedfb635 4712 $tags_nolinks_str = preg_replace("/, $/", "", $tags_nolinks_str);
3de0261a
AD
4713 $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
4714
66a251f9 4715 $all_tags_div = "<span class='cdmAllTagsCtr'>&hellip;<div class='cdmAllTags'>All Tags: $f_tags_str</div></span>";
e7544143
AD
4716 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4717
3de0261a
AD
4718 if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
4719
4720 if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>';
eedfb635 4721 if (!$tags_nolinks_str) $tags_nolinks_str = '<span class="tagList">'.__('no tags').'</span>';
3de0261a 4722
5f014cf1 4723 print "<div style='float : right'>
eedfb635
AD
4724 <img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>";
4725
4726 if (!$zoom_mode) {
4727 print "$tags_str
4728 <a title=\"".__('Edit tags for this article')."\"
4710e3dc
AD
4729 href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a>";
4730
4731 if (defined('_ENABLE_INLINE_VIEW')) {
4732
4733 print "<img src=\"images/art-inline.png\" class='tagsPic'
98fe7044 4734 style=\"cursor : pointer\" style=\"cursor : pointer\"
4710e3dc
AD
4735 onclick=\"showOriginalArticleInline($id)\"
4736 alt='Inline' title='".__('Display original article content')."'>";
4737
4738 }
4739
4740 print "<img src=\"images/art-zoom.png\" class='tagsPic'
98fe7044 4741 style=\"cursor : pointer\" style=\"cursor : pointer\"
eedfb635
AD
4742 onclick=\"zoomToArticle($id)\"
4743 alt='Zoom' title='".__('Show article summary in new window')."'>";
4744 } else {
4745 print "$tags_nolinks_str";
4746 }
4747 print "</div>";
4748 print "<div clear='both'>$entry_comments</div>";
3de0261a
AD
4749
4750 print "</div>";
4751
4752 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
4753 print "<div class=\"postContent\">";
4754
e7544143 4755 #print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>";
3de0261a 4756
c54526fe 4757 $article_content = sanitize_rss($link, $line["content"]);
c41890b0 4758
3de0261a 4759 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
c54526fe
AD
4760 $article_content = preg_replace("/href=/i", "target=\"_blank\" href=",
4761 $article_content);
3de0261a
AD
4762 }
4763
c54526fe 4764 print $article_content;
ce53e200
AD
4765
4766 $result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE
4752b041 4767 post_id = '$id' AND content_url != ''");
ce53e200
AD
4768
4769 if (db_num_rows($result) > 0) {
ce53e200 4770
9c5ee7e1 4771 $entries_html = array();
ce53e200
AD
4772 $entries = array();
4773
4774 while ($line = db_fetch_assoc($result)) {
4775
4776 $url = $line["content_url"];
4752b041
AD
4777 $ctype = $line["content_type"];
4778
4779 if (!$ctype) $ctype = __("unknown type");
ce53e200
AD
4780
4781 $filename = substr($url, strrpos($url, "/")+1);
4782
8dccabed
AD
4783 $entry = "";
4784
b84b68d9 4785 if (($ctype == __("audio/mpeg")) &&
8dccabed
AD
4786 (get_pref($link, "ENABLE_FLASH_PLAYER")) ) {
4787
39e865fa 4788 $entry .= "<object type=\"application/x-shockwave-flash\" data=\"extras/button/musicplayer.swf?song_url=$url\" width=\"17\" height=\"17\"> <param name=\"movie\" value=\"extras/button/musicplayer.swf?song_url=$url\" /> </object> ";
8dccabed
AD
4789
4790 }
4791
4792 $entry .= "<a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
4752b041 4793 $filename . " (" . $ctype . ")" . "</a>";
ce53e200 4794
9c5ee7e1
AD
4795 array_push($entries_html, $entry);
4796
4797 $entry = array();
4798
4799 $entry["type"] = $ctype;
4800 $entry["filename"] = $filename;
4801 $entry["url"] = $url;
4802
ce53e200
AD
4803 array_push($entries, $entry);
4804 }
4805
9c5ee7e1
AD
4806 print "<div class=\"postEnclosures\">";
4807
c54526fe 4808 if (!preg_match("/img/i", $article_content)) {
9c5ee7e1
AD
4809 foreach ($entries as $entry) {
4810 if (preg_match("/image/", $entry["type"])) {
4811 print "<p><img
4812 alt=\"".htmlspecialchars($entry["filename"])."\"
4813 src=\"" .htmlspecialchars($entry["url"]) . "\"></p>";
4814 }
4815 }
4816 }
4817
4818 print "<div class=\"postEnclosures\">";
4819
4820 if (db_num_rows($result) == 1) {
4821 print __("Attachment:") . " ";
4822 } else {
4823 print __("Attachments:") . " ";
4824 }
4825
4826 print join(", ", $entries_html);
ce53e200
AD
4827
4828 print "</div>";
4829 }
4830
4831 print "</div>";
3de0261a
AD
4832
4833 print "</div>";
4834
4835 }
4836
eedfb635
AD
4837 if (!$zoom_mode) {
4838 print "]]></article>";
4839 } else {
4840 print "
4841 <div style=\"text-align : center\">
4842 <input type=\"submit\" onclick=\"return window.close()\"
4843 value=\"".__("Close this window")."\"></div>";
4844 print "</body></html>";
4845
4846 }
3de0261a
AD
4847
4848 }
4849
4850 function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view,
7b4d02a8
AD
4851 $next_unread_feed, $offset, $vgr_last_feed = false,
4852 $override_order = false) {
3de0261a 4853
52d7e7da
AD
4854 $disable_cache = false;
4855
46921916
AD
4856 $timing_info = getmicrotime();
4857
961f4c73
AD
4858 $topmost_article_ids = array();
4859
ac541432
AD
4860 if (!$offset) {
4861 $offset = 0;
4862 }
3de0261a
AD
4863
4864 if ($subop == "undefined") $subop = "";
4865
a9bcfb8f
AD
4866 $subop_split = split(":", $subop);
4867
3de0261a
AD
4868 if ($subop == "CatchupSelected") {
4869 $ids = split(",", db_escape_string($_GET["ids"]));
4870 $cmode = sprintf("%d", $_GET["cmode"]);
4871
4872 catchupArticlesById($link, $ids, $cmode);
4873 }
4874
4875 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
35bf080c 4876 update_generic_feed($link, $feed, $cat_view, true);
3de0261a
AD
4877 }
4878
4879 if ($subop == "MarkAllRead") {
4880 catchup_feed($link, $feed, $cat_view);
4881
4882 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
4883 if ($next_unread_feed) {
4884 $feed = $next_unread_feed;
4885 }
4886 }
4887 }
4888
a9bcfb8f
AD
4889 if ($subop_split[0] == "MarkAllReadGR") {
4890 catchup_feed($link, $subop_split[1], false);
4891 }
4892
4893
3de0261a
AD
4894 if ($feed_id > 0) {
4895 $result = db_query($link,
4896 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
4897
4898 if (db_num_rows($result) == 0) {
4899 print "<div align='center'>".__('Feed not found.')."</div>";
4900 return;
4901 }
4902 }
4903
4904 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
10eb9da8 4905
3de0261a
AD
4906 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
4907 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
4908
4909 if (db_num_rows($result) == 1) {
4910 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
4911 } else {
4912 $rtl_content = false;
4913 }
4914
4915 if ($rtl_content) {
4916 $rtl_tag = "dir=\"RTL\"";
4917 } else {
4918 $rtl_tag = "";
4919 }
4920 } else {
4921 $rtl_tag = "";
4922 $rtl_content = false;
4923 }
4924
4925 $script_dt_add = get_script_dt_add();
4926
4927 /// START /////////////////////////////////////////////////////////////////////////////////
4928
4929 $search = db_escape_string($_GET["query"]);
52d7e7da
AD
4930
4931 if ($search) {
4932 $disable_cache = true;
4933 }
4934
3de0261a
AD
4935 $search_mode = db_escape_string($_GET["search_mode"]);
4936 $match_on = db_escape_string($_GET["match_on"]);
4937
4938 if (!$match_on) {
4939 $match_on = "both";
4940 }
4941
4942 $real_offset = $offset * $limit;
4943
46921916
AD
4944 if ($_GET["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
4945
3de0261a 4946 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
7b4d02a8 4947 $search, $search_mode, $match_on, $override_order, $real_offset);
3de0261a 4948
46921916
AD
4949 if ($_GET["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
4950
3de0261a
AD
4951 $result = $qfh_ret[0];
4952 $feed_title = $qfh_ret[1];
4953 $feed_site_url = $qfh_ret[2];
4954 $last_error = $qfh_ret[3];
f56e3080 4955
081e527d
AD
4956 $vgroup_last_feed = $vgr_last_feed;
4957
f56e3080
AD
4958 if ($feed == -2) {
4959 $feed_site_url = article_publish_url($link);
4960 }
4961
3de0261a
AD
4962 /// STOP //////////////////////////////////////////////////////////////////////////////////
4963
ac541432
AD
4964 if (!$offset) {
4965 print "<div id=\"headlinesContainer\" $rtl_tag>";
3de0261a 4966
ac541432
AD
4967 if (!$result) {
4968 print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
4969 return;
4970 }
3de0261a 4971
ac541432
AD
4972 print_headline_subtoolbar($link, $feed_site_url, $feed_title, false,
4973 $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode,
4974 $offset, $limit);
3de0261a 4975
ac541432
AD
4976 print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
4977 }
3de0261a 4978
29dfb258
AD
4979 $headlines_count = db_num_rows($result);
4980
3de0261a
AD
4981 if (db_num_rows($result) > 0) {
4982
4983# print "\{$offset}";
4984
ac541432 4985 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
3de0261a
AD
4986 print "<table class=\"headlinesList\" id=\"headlinesList\"
4987 cellspacing=\"0\">";
4988 }
4989
4ab4d364
AD
4990 $lnum = $limit*$offset;
4991
3de0261a
AD
4992 error_reporting (DEFAULT_ERROR_LEVEL);
4993
4994 $num_unread = 0;
6cfea5c7
AD
4995 $cur_feed_title = '';
4996
3de0261a
AD
4997 while ($line = db_fetch_assoc($result)) {
4998
4999 $class = ($lnum % 2) ? "even" : "odd";
5000
5001 $id = $line["id"];
5002 $feed_id = $line["feed_id"];
961f4c73
AD
5003
5004 if (count($topmost_article_ids) < 5) {
5005 array_push($topmost_article_ids, $id);
5006 }
5007
3de0261a
AD
5008 if ($line["last_read"] == "" &&
5009 ($line["unread"] != "t" && $line["unread"] != "1")) {
5010
5011 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
5012 alt=\"Updated\">";
5013 } else {
5014 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
5015 alt=\"Updated\">";
5016 }
5017
5018 if ($line["unread"] == "t" || $line["unread"] == "1") {
5019 $class .= "Unread";
5020 ++$num_unread;
5021 $is_unread = true;
5022 } else {
5023 $is_unread = false;
5024 }
abd8a516
AD
5025
5026 $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false);
5027
5028 if ($is_ie) {
5029 $mark_img_ext = "gif";
5030 } else {
5031 $mark_img_ext = "png";
5032 }
5033
3de0261a 5034 if ($line["marked"] == "t" || $line["marked"] == "1") {
abd8a516 5035 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.$mark_img_ext\"
3de0261a 5036 class=\"markedPic\"
f5e0338d 5037 alt=\"Unstar article\" onclick='javascript:tMark($id)'>";
3de0261a 5038 } else {
abd8a516 5039 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.$mark_img_ext\"
3de0261a 5040 class=\"markedPic\"
f5e0338d 5041 alt=\"Star article\" onclick='javascript:tMark($id)'>";
3de0261a
AD
5042 }
5043
e4f4b46f 5044 if ($line["published"] == "t" || $line["published"] == "1") {
f5e0338d 5045 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.gif\"
e4f4b46f 5046 class=\"markedPic\"
f5e0338d 5047 alt=\"Unpublish article\" onclick='javascript:tPub($id)'>";
e4f4b46f 5048 } else {
f5e0338d 5049 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.gif\"
e4f4b46f 5050 class=\"markedPic\"
f5e0338d 5051 alt=\"Publish article\" onclick='javascript:tPub($id)'>";
e4f4b46f
AD
5052 }
5053
e944346c 5054# $content_link = "<a target=\"_blank\" href=\"".$line["link"]."\">" .
3de0261a
AD
5055# $line["title"] . "</a>";
5056
f0971fc1
AD
5057# $content_link = "<a
5058# href=\"" . htmlspecialchars($line["link"]) . "\"
5059# onclick=\"view($id,$feed_id);\">" .
5060# $line["title"] . "</a>";
3de0261a
AD
5061
5062# $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
5063# $line["title"] . "</a>";
5064
5065 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
46921916 5066 $updated_fmt = smart_date_time(strtotime($line["updated_noms"]));
3de0261a
AD
5067 } else {
5068 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
46921916 5069 $updated_fmt = date($short_date, strtotime($line["updated_noms"]));
3de0261a
AD
5070 }
5071
5072 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
5073 $content_preview = truncate_string(strip_tags($line["content_preview"]),
5074 100);
5075 }
5076
ff6e357a
AD
5077 $score = $line["score"];
5078
1e36af0c 5079 $score_pic = get_score_pic($score);
546499a9
AD
5080
5081 $score_title = __("(Click to change)");
5082
5daa24f2 5083 $score_pic = "<img class='hlScorePic' src=\"images/$score_pic\"
546499a9 5084 onclick=\"adjustArticleScore($id, $score)\" title=\"$score $score_title\">";
ff6e357a 5085
5daa24f2
AD
5086 if ($score > 500) {
5087 $hlc_suffix = "H";
5088 } else if ($score < -100) {
5089 $hlc_suffix = "L";
5090 } else {
5091 $hlc_suffix = "";
5092 }
5093
3de0261a
AD
5094 $entry_author = $line["author"];
5095
5096 if ($entry_author) {
60164936 5097 $entry_author = " - $entry_author";
3de0261a
AD
5098 }
5099
7defa089 5100 $has_feed_icon = feed_has_icon($feed_id);
bd51294a
AD
5101
5102 if ($has_feed_icon) {
5103 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"".ICONS_URL."/$feed_id.ico\" alt=\"\">";
5104 } else {
5105 //$feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\" alt=\"\">";
20be0cf8 5106 $feed_icon_img = "";
bd51294a
AD
5107 }
5108
3de0261a 5109 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
6cfea5c7 5110
d00f22ac 5111 if (get_pref($link, 'VFEED_GROUP_BY_FEED')) {
bb031f91 5112 if ($feed_id != $vgroup_last_feed && $line["feed_title"]) {
a9bcfb8f
AD
5113
5114 $cur_feed_title = $line["feed_title"];
081e527d 5115 $vgroup_last_feed = $feed_id;
a9bcfb8f 5116
962d8ba4 5117 $cur_feed_title = htmlspecialchars($cur_feed_title);
43fc671f 5118
338ce36c 5119 $vf_catchup_link = "(<a onclick='javascript:catchupFeedInGroup($feed_id);' href='#'>mark as read</a>)";
a9bcfb8f 5120
6cfea5c7 5121 print "<tr class='feedTitle'><td colspan='7'>".
dc803347 5122 "<div style=\"float : right\">$feed_icon_img</div>".
6cfea5c7 5123 "<a href=\"javascript:viewfeed($feed_id, '', false)\">".
965fb2af 5124 $line["feed_title"]."</a> $vf_catchup_link</td></tr>";
6cfea5c7
AD
5125 }
5126 }
314fcd2b
AD
5127
5128 $mouseover_attrs = "onmouseover='postMouseIn($id)'
5129 onmouseout='postMouseOut($id)'";
5130
5131 print "<tr class='$class' id='RROW-$id' $mouseover_attrs>";
3de0261a 5132
67343d9f 5133 print "<td class='hlUpdPic'>$update_pic</td>";
3de0261a
AD
5134
5135 print "<td class='hlSelectRow'>
67343d9f
AD
5136 <input type=\"checkbox\" onclick=\"tSR(this)\"
5137 id=\"RCHK-$id\">
3de0261a
AD
5138 </td>";
5139
5140 print "<td class='hlMarkedPic'>$marked_pic</td>";
e4f4b46f 5141 print "<td class='hlMarkedPic'>$published_pic</td>";
3de0261a 5142
df456bb0
AD
5143# if ($line["feed_title"]) {
5144# print "<td class='hlContent'>$content_link</td>";
5145# print "<td class='hlFeed'>
5146# <a href=\"javascript:viewfeed($feed_id, '', false)\">".
5147# truncate_string($line["feed_title"],30)."</a>&nbsp;</td>";
5148# } else {
5149
f0971fc1 5150 print "<td onclick='view($id,$feed_id)' class='hlContent$hlc_suffix' valign='middle'>";
df456bb0 5151
f0971fc1
AD
5152 print "<a id=\"RTITLE-$id\"
5153 href=\"" . htmlspecialchars($line["link"]) . "\"
a7764e51 5154 onclick=\"return view($id,$feed_id);\">" .
df456bb0
AD
5155 $line["title"];
5156
5157 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
5158 if ($content_preview) {
5159 print "<span class=\"contentPreview\"> - $content_preview</span>";
3de0261a 5160 }
3de0261a 5161 }
df456bb0
AD
5162
5163 print "</a>";
5164
5165# <a href=\"javascript:viewfeed($feed_id, '', false)\">".
5166# $line["feed_title"]."</a>
5167
d00f22ac 5168 if (!get_pref($link, 'VFEED_GROUP_BY_FEED')) {
6cfea5c7
AD
5169 if ($line["feed_title"]) {
5170 print "<span class=\"hlFeed\">
5171 (<a href=\"javascript:viewfeed($feed_id, '', false)\">".
5172 $line["feed_title"]."</a>)
5173 </span>";
5174 }
df456bb0 5175 }
df456bb0 5176 print "</td>";
bd51294a 5177
df456bb0 5178# }
3de0261a 5179
d7e83df7 5180 print "<td class=\"hlUpdated\" onclick='view($id,$feed_id)'><nobr>$updated_fmt&nbsp;</nobr></td>";
546499a9
AD
5181
5182 print "<td class='hlMarkedPic'>$score_pic</td>";
bd51294a
AD
5183
5184 if ($line["feed_title"] && !get_pref($link, 'VFEED_GROUP_BY_FEED')) {
d7e83df7 5185 print "<td onclick=\"viewfeed($feed_id)\" class=\"hlFeedIcon\">$feed_icon_img</td>";
bd51294a
AD
5186 }
5187
3de0261a
AD
5188 print "</tr>";
5189
5190 } else {
6cfea5c7 5191
bb031f91 5192 if (get_pref($link, 'VFEED_GROUP_BY_FEED') && $line["feed_title"]) {
081e527d
AD
5193 if ($feed_id != $vgroup_last_feed) {
5194
5195 $cur_feed_title = $line["feed_title"];
5196 $vgroup_last_feed = $feed_id;
5197
962d8ba4
AD
5198 $cur_feed_title = htmlspecialchars($cur_feed_title);
5199
338ce36c 5200 $vf_catchup_link = "(<a onclick='javascript:catchupFeedInGroup($feed_id);' href='#'>mark as read</a>)";
081e527d 5201
7defa089 5202 $has_feed_icon = feed_has_icon($feed_id);
dc803347
AD
5203
5204 if ($has_feed_icon) {
5205 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"".ICONS_URL."/$feed_id.ico\" alt=\"\">";
5206 } else {
5207 //$feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\" alt=\"\">";
5208 }
5209
6cfea5c7 5210 print "<div class='cdmFeedTitle'>".
dc803347 5211 "<div style=\"float : right\">$feed_icon_img</div>".
6cfea5c7 5212 "<a href=\"javascript:viewfeed($feed_id, '', false)\">".
081e527d 5213 $line["feed_title"]."</a> $vf_catchup_link</div>";
6cfea5c7
AD
5214 }
5215 }
5216
3de0261a
AD
5217 if ($is_unread) {
5218 $add_class = "Unread";
5219 } else {
5220 $add_class = "";
5221 }
0cacc891
AD
5222
5223 $expand_cdm = get_pref($link, 'CDM_EXPANDED');
3cd4239a 5224 $show_excerpt = false;
0cacc891 5225
5daa24f2 5226 if ($expand_cdm && $score >= -100) {
0cacc891 5227 $cdm_cstyle = "";
3cd4239a 5228 $show_excerpt = false;
0cacc891
AD
5229 } else {
5230 $cdm_cstyle = "style=\"display : none\"";
3cd4239a 5231 $show_excerpt = true;
0cacc891
AD
5232 }
5233
314fcd2b
AD
5234 $mouseover_attrs = "onmouseover='postMouseIn($id)'
5235 onmouseout='postMouseOut($id)'";
5236
e4914b62 5237 print "<div class=\"cdmArticle$add_class\"
3cd4239a 5238 id=\"RROW-$id\"
314fcd2b 5239 $mouseover_attrs'>";
3de0261a
AD
5240
5241 print "<div class=\"cdmHeader\">";
5242
965fb2af
AD
5243 if (!get_pref($link, "VFEED_GROUP_BY_FEED") || !$line["feed_title"]) {
5244 $cdm_feed_icon = "<span style=\"cursor : pointer\" onclick=\"viewfeed($feed_id)\">$feed_icon_img</span>";
5245 }
5246
5247 print "<div class=\"articleUpdated\">$updated_fmt $score_pic $cdm_feed_icon
b3296d36 5248 </div>";
5daa24f2 5249
9617eb94 5250 print "<span id=\"RTITLE-$id\" class=\"titleWrap$hlc_suffix\"><a class=\"title\"
3de0261a 5251 onclick=\"javascript:toggleUnread($id, 0)\"
5daa24f2
AD
5252 target=\"_blank\" href=\"".$line["link"]."\">".$line["title"]."</a>
5253 ";
3de0261a
AD
5254
5255 print $entry_author;
5256
3cd4239a 5257/* if (!$expand_cdm || $score < -100) {
0cacc891
AD
5258 print "&nbsp;<a id=\"CICH-$id\"
5259 href=\"javascript:cdmExpandArticle($id)\">
5260 (".__('Show article').")</a>";
3cd4239a 5261 } */
0cacc891
AD
5262
5263
d00f22ac 5264 if (!get_pref($link, 'VFEED_GROUP_BY_FEED')) {
6cfea5c7
AD
5265 if ($line["feed_title"]) {
5266 print "&nbsp;(<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
5267 }
3de0261a
AD
5268 }
5269
5daa24f2 5270 print "</span></div>";
3de0261a 5271
3fc2eed5
AD
5272 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
5273 $line["content_preview"] = preg_replace("/href=/i",
e944346c 5274 "target=\"_blank\" href=", $line["content_preview"]);
3fc2eed5
AD
5275 }
5276
3cd4239a
AD
5277 if ($show_excerpt) {
5278 print "<div class=\"cdmExcerpt\" id=\"CEXC-$id\"
5279 onclick=\"cdmExpandArticle($id)\"
5280 title=\"".__('Click to expand article')."\">";
5281 print truncate_string(strip_tags($line["content_preview"]), 100);
5282 print "</div>";
5283 }
5284
5285 print "<div class=\"cdmContent\"
5286 onclick=\"cdmClicked($id)\"
5287 id=\"CICD-$id\" $cdm_cstyle>";
a04c8e8d 5288
0cacc891 5289// print "<div class=\"cdmInnerContent\" id=\"CICD-$id\" $cdm_cstyle>";
621ffb00 5290
b2d5d145 5291 print sanitize_rss($link, $line["content_preview"]);
c54526fe 5292 $article_content = $line["content_preview"];
3c66b582
AD
5293
5294 $e_result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE
4752b041 5295 post_id = '$id' AND content_url != ''");
3c66b582
AD
5296
5297 if (db_num_rows($e_result) > 0) {
3c66b582 5298
a3eeb471 5299 $entries_html = array();
3c66b582
AD
5300 $entries = array();
5301
5302 while ($e_line = db_fetch_assoc($e_result)) {
5303
5304 $url = $e_line["content_url"];
4752b041
AD
5305 $ctype = $e_line["content_type"];
5306 if (!$ctype) $ctype = __("unknown type");
3c66b582
AD
5307
5308 $filename = substr($url, strrpos($url, "/")+1);
5309
8dccabed
AD
5310 $entry = "";
5311
b84b68d9 5312 if (($ctype == __("audio/mpeg")) &&
8dccabed
AD
5313 (get_pref($link, "ENABLE_FLASH_PLAYER")) ) {
5314
39e865fa 5315 $entry .= "<object type=\"application/x-shockwave-flash\" data=\"extras/button/musicplayer.swf?song_url=$url\" width=\"17\" height=\"17\"> <param name=\"movie\" value=\"extras/button/musicplayer.swf?song_url=$url\" /> </object> ";
8dccabed
AD
5316
5317 }
5318
5319 $entry .= "<a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
4752b041 5320 $filename . " (" . $ctype . ")" . "</a>";
3c66b582 5321
a3eeb471
AD
5322 array_push($entries_html, $entry);
5323
5324 $entry = array();
5325
5326 $entry["type"] = $ctype;
5327 $entry["filename"] = $filename;
5328 $entry["url"] = $url;
5329
3c66b582
AD
5330 array_push($entries, $entry);
5331 }
5332
c54526fe 5333 if (!preg_match("/img/i", $article_content)) {
a3eeb471
AD
5334 foreach ($entries as $entry) {
5335 if (preg_match("/image/", $entry["type"])) {
5336 print "<p><img
5337 alt=\"".htmlspecialchars($entry["filename"])."\"
5338 src=\"" .htmlspecialchars($entry["url"]) . "\"></p>";
5339 }
5340 }
5341 }
5342
5343 print "<div class=\"cdmEnclosures\">";
5344
5345 if (db_num_rows($e_result) == 1) {
5346 print __("Attachment:") . " ";
5347 } else {
5348 print __("Attachments:") . " ";
5349 }
5350
5351 print join(", ", $entries_html);
3c66b582
AD
5352
5353 print "</div>";
5354 }
5355
a3eeb471 5356
0cacc891
AD
5357 print "<br clear='both'>";
5358// print "</div>";
a04c8e8d 5359
0cacc891 5360/* if (!$expand_cdm) {
12f5d8fe
AD
5361 print "<a id=\"CICH-$id\"
5362 href=\"javascript:cdmExpandArticle($id)\">
5363 Show article</a>";
0cacc891 5364 } */
a04c8e8d 5365
0cacc891 5366 print "</div>";
3de0261a 5367
e2ccbfab 5368 print "<div class=\"cdmFooter\"><span class='s0'>";
3de0261a 5369
e2ccbfab 5370 /* print "<div class=\"markedPic\">Star it: $marked_pic</div>"; */
3de0261a 5371
e2ccbfab
AD
5372 print __("Select:").
5373 " <input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
3de0261a
AD
5374 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">";
5375
e2ccbfab 5376 print "</span><span class='s1'>$marked_pic</span> ";
e4f4b46f 5377 print "<span class='s1'>$published_pic</span> ";
eedfb635
AD
5378 print "<span class='s1'><img src=\"images/art-zoom.png\" class='tagsPic'
5379 onclick=\"zoomToArticle($id)\"
5380 style=\"cursor : pointer\"
5381 alt='Zoom'
5382 title='".__('Show article summary in new window')."'></span>";
e2ccbfab 5383
3de0261a
AD
5384 $tags = get_article_tags($link, $id);
5385
5386 $tags_str = "";
22d1f3db 5387 $full_tags_str = "";
d735ebd2 5388 $num_tags = 0;
3de0261a
AD
5389
5390 foreach ($tags as $tag) {
5391 $num_tags++;
22d1f3db
AD
5392 $full_tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
5393 if ($num_tags < 5) {
5394 $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
5395 } else if ($num_tags == 5) {
66a251f9 5396 $tags_str .= "&hellip;";
22d1f3db 5397 }
3de0261a
AD
5398 }
5399
5400 $tags_str = preg_replace("/, $/", "", $tags_str);
22d1f3db
AD
5401 $full_tags_str = preg_replace("/, $/", "", $full_tags_str);
5402
66a251f9 5403 $all_tags_div = "<span class='cdmAllTagsCtr'>&hellip;<div class='cdmAllTags'>All Tags: $full_tags_str</div></span>";
22d1f3db
AD
5404
5405 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
5406
3de0261a
AD
5407
5408 if ($tags_str == "") $tags_str = "no tags";
e2ccbfab
AD
5409
5410// print "<img src='images/tag.png' class='markedPic'>";
5411
5f014cf1
AD
5412 print "<span class='s1'>
5413 <img class='tagsPic' src='images/tag.png' alt='Tags'
5414 title='Tags'> $tags_str <a title=\"Edit tags for this article\"
3de0261a
AD
5415 href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
5416
e2ccbfab 5417 print "</span>";
3de0261a 5418
e2ccbfab
AD
5419 print "<span class='s2'>Toggle: <a class=\"cdmToggleLink\"
5420 href=\"javascript:toggleUnread($id)\">
5421 Unread</a></span>";
3de0261a 5422
e2ccbfab 5423 print "</div>";
3de0261a
AD
5424 print "</div>";
5425
5426 }
5427
5428 ++$lnum;
5429 }
5430
ac541432 5431 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
3de0261a
AD
5432 print "</table>";
5433 }
5434
5435// print_headline_subtoolbar($link,
5436// "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
5437
5438
5439 } else {
93c841c4
AD
5440 $message = "";
5441
5442 switch ($view_mode) {
5443 case "unread":
5444 $message = __("No unread articles found to display.");
5445 break;
5446 case "marked":
5447 $message = __("No starred articles found to display.");
5448 break;
5449 default:
5450 $message = __("No articles found to display.");
5451 }
5452
5453 if (!$offset) print "<div class='whiteBox'>$message</div>";
3de0261a
AD
5454 }
5455
ac541432
AD
5456 if (!$offset) {
5457 print "</div>";
5458 print "</div>";
5459 }
3de0261a 5460
081e527d 5461 return array($topmost_article_ids, $headlines_count, $feed, $disable_cache, $vgroup_last_feed);
3de0261a 5462 }
0979b696
AD
5463
5464// from here: http://www.roscripts.com/Create_tag_cloud-71.html
5465
5466 function printTagCloud($link) {
35a03bdd
AD
5467
5468 /* get first ref_id to count from */
5469
dcac082b
AD
5470 /*
5471
35a03bdd
AD
5472 $query = "";
5473
5474 if (DB_TYPE == "pgsql") {
5475 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
5476 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
5477 AND date_entered > NOW() - INTERVAL '30 days'";
5478 } else {
5479 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
5480 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
5481 AND date_entered > DATE_SUB(NOW(), INTERVAL 30 DAY)";
5482 }
5483
5484 $result = db_query($link, $query);
dcac082b 5485 $first_id = db_fetch_result($result, 0, "id"); */
35a03bdd 5486
dcac082b 5487 //AND post_int_id >= '$first_id'
0979b696
AD
5488 $query = "SELECT tag_name, COUNT(post_int_id) AS count
5489 FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]."
b31af972 5490 GROUP BY tag_name ORDER BY count DESC LIMIT 50";
0979b696
AD
5491
5492 $result = db_query($link, $query);
5493
5494 $tags = array();
5495
5496 while ($line = db_fetch_assoc($result)) {
5497 $tags[$line["tag_name"]] = $line["count"];
5498 }
5499
5500 ksort($tags);
5501
5502 $max_size = 32; // max font size in pixels
4548a580 5503 $min_size = 11; // min font size in pixels
0979b696
AD
5504
5505 // largest and smallest array values
5506 $max_qty = max(array_values($tags));
5507 $min_qty = min(array_values($tags));
5508
5509 // find the range of values
5510 $spread = $max_qty - $min_qty;
5511 if ($spread == 0) { // we don't want to divide by zero
5512 $spread = 1;
5513 }
5514
5515 // set the font-size increment
5516 $step = ($max_size - $min_size) / ($spread);
5517
5518 // loop through the tag array
5519 foreach ($tags as $key => $value) {
5520 // calculate font-size
5521 // find the $value in excess of $min_qty
5522 // multiply by the font-size increment ($size)
5523 // and add the $min_size set above
5524 $size = round($min_size + (($value - $min_qty) * $step));
546ffab4
AD
5525
5526 $key_escaped = str_replace("'", "\\'", $key);
5527
5528 echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " .
0979b696
AD
5529 $size . "px\" title=\"$value articles tagged with " .
5530 $key . '">' . $key . '</a> ';
5531 }
5532 }
46921916
AD
5533
5534 function print_checkpoint($n, $s) {
5535 $ts = getmicrotime();
5536 echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s);
5537 return $ts;
5538 }
14b6c54b
AD
5539
5540 function sanitize_tag($tag) {
5541 $tag = trim($tag);
5542
5543 $tag = mb_strtolower($tag, 'utf-8');
5544
0c3d1c68
AD
5545 $tag = preg_replace('/[\"\+\>\<]/', "", $tag);
5546
5547// $tag = str_replace('"', "", $tag);
5548// $tag = str_replace("+", " ", $tag);
14b6c54b
AD
5549 $tag = str_replace("technorati tag: ", "", $tag);
5550
5551 return $tag;
5552 }
e4f4b46f
AD
5553
5554 function generate_publish_key() {
5555 return sha1(uniqid(rand(), true));
5556 }
5557
f56e3080
AD
5558 function article_publish_url($link) {
5559
e2749781
AD
5560 $url_path = "";
5561
5562
5563 if ($_SERVER['HTTPS'] != "on") {
5564 $url_path = "http://";
5565 } else {
5566 $url_path = "https://";
5567 }
f56e3080 5568
e2749781
AD
5569 $url_path .= $_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
5570 $url_path .= "/backend.php?op=publish&key=" . get_pref($link, "_PREFS_PUBLISH_KEY");
f56e3080
AD
5571
5572 return $url_path;
5573 }
5574
45004d43
AD
5575 /**
5576 * Purge a feed contents, marked articles excepted.
5577 *
5578 * @param mixed $link The database connection.
5579 * @param integer $id The id of the feed to purge.
5580 * @return void
5581 */
d1f0c584
AD
5582 function clear_feed_articles($link, $id) {
5583 $result = db_query($link, "DELETE FROM ttrss_user_entries
a8ae1b9a 5584 WHERE feed_id = '$id' AND marked = false AND owner_uid = " . $_SESSION["uid"]);
d1f0c584
AD
5585
5586 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
5587 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
45004d43
AD
5588 } // function clear_feed_articles
5589
5590 /**
5591 * Compute the Mozilla Firefox feed adding URL from server HOST and REQUEST_URI.
5592 *
5593 * @return string The Mozilla Firefox feed adding URL.
5594 */
5595 function add_feed_url() {
d70c5ae4 5596 $url_path = ($_SERVER['HTTPS'] != "on" ? 'http://' : 'https://') . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
755a43ee
AD
5597 $url_path .= "?op=pref-feeds&quiet=1&subop=add&feed_url=%s";
5598 return $url_path;
45004d43
AD
5599 } // function add_feed_url
5600
5601 /**
5602 * Encrypt a password in SHA1.
5603 *
5604 * @param string $pass The password to encrypt.
5605 * @param string $login A optionnal login.
5606 * @return string The encrypted password.
5607 */
1a9f4d3c
AD
5608 function encrypt_password($pass, $login = '') {
5609 if ($login) {
5610 return "SHA1X:" . sha1("$login:$pass");
5611 } else {
5612 return "SHA1:" . sha1($pass);
5613 }
45004d43
AD
5614 } // function encrypt_password
5615
5616 /**
5617 * Update a feed batch.
5618 * Used by daemons to update n feeds by run.
5619 * Only update feed needing a update, and not being processed
5620 * by another process.
5621 *
5622 * @param mixed $link Database link
5623 * @param integer $limit Maximum number of feeds in update batch. Default to DAEMON_FEED_LIMIT.
5624 * @param boolean $from_http Set to true if you call this function from http to disable cli specific code.
5625 * @param boolean $debug Set to false to disable debug output. Default to true.
5626 * @return void
5627 */
5628 function update_daemon_common($link, $limit = DAEMON_FEED_LIMIT, $from_http = false, $debug = true) {
5629 // Process all other feeds using last_updated and interval parameters
5630
5631 // Test if the user has loggued in recently. If not, it does not update its feeds.
5632 if (DAEMON_UPDATE_LOGIN_LIMIT > 0) {
5633 if (DB_TYPE == "pgsql") {
5634 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'";
5635 } else {
5636 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)";
5637 }
5638 } else {
5639 $login_thresh_qpart = "";
5640 }
5641
5642 // Test if the feed need a update (update interval exceded).
5643 if (DB_TYPE == "pgsql") {
5644 $update_limit_qpart = "AND ((
5645 ttrss_feeds.update_interval = 0
5646 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
5647 ) OR (
5648 ttrss_feeds.update_interval > 0
5649 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
da4caf5d 5650 ) OR ttrss_feeds.last_updated IS NULL)";
45004d43
AD
5651 } else {
5652 $update_limit_qpart = "AND ((
5653 ttrss_feeds.update_interval = 0
5654 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
5655 ) OR (
5656 ttrss_feeds.update_interval > 0
5657 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
da4caf5d 5658 ) OR ttrss_feeds.last_updated IS NULL)";
45004d43
AD
5659 }
5660
5661 // Test if feed is currently being updated by another process.
5662 if (DB_TYPE == "pgsql") {
5663 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')";
5664 } else {
5665 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))";
5666 }
5667
5668 // Test if there is a limit to number of updated feeds
5669 $query_limit = "";
5670 if($limit) $query_limit = sprintf("LIMIT %d", $limit);
5671
51b8c957
AD
5672 $random_qpart = sql_random_function();
5673
45004d43
AD
5674 // We search for feed needing update.
5675 $result = db_query($link, "SELECT ttrss_feeds.feed_url,ttrss_feeds.id, ttrss_feeds.owner_uid,
fc2b26a6 5676 ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated,
45004d43
AD
5677 ttrss_feeds.update_interval
5678 FROM
5679 ttrss_feeds, ttrss_users, ttrss_user_prefs
5680 WHERE
5681 ttrss_feeds.owner_uid = ttrss_users.id
5682 AND ttrss_users.id = ttrss_user_prefs.owner_uid
5683 AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
5684 $login_thresh_qpart $update_limit_qpart
51b8c957
AD
5685 $updstart_thresh_qpart
5686 ORDER BY $random_qpart $query_limit");
45004d43
AD
5687
5688 $user_prefs_cache = array();
5689
5690 if($debug) _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
5691
5692 // Here is a little cache magic in order to minimize risk of double feed updates.
5693 $feeds_to_update = array();
5694 while ($line = db_fetch_assoc($result)) {
5695 $feeds_to_update[$line['id']] = $line;
5696 }
5697
5698 // We update the feed last update started date before anything else.
5699 // There is no lag due to feed contents downloads
5700 // It prevent an other process to update the same feed.
5701 $feed_ids = array_keys($feeds_to_update);
5702 if($feed_ids) {
5703 db_query($link, sprintf("UPDATE ttrss_feeds SET last_update_started = NOW()
5704 WHERE id IN (%s)", implode(',', $feed_ids)));
5705 }
5706
5707 // For each feed, we call the feed update function.
5708 while ($line = array_pop($feeds_to_update)) {
5709
5710 if($debug) _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]);
5711
5712 // We setup a alarm to alert if the feed take more than 300s to update.
5713 // => HANG alarm.
9a91a51e 5714 if(!$from_http && function_exists('pcntl_alarm')) pcntl_alarm(300);
45004d43
AD
5715 update_rss_feed($link, $line["feed_url"], $line["id"], true);
5716 // Cancel the alarm (the update went well)
9a91a51e 5717 if(!$from_http && function_exists('pcntl_alarm')) pcntl_alarm(0);
45004d43
AD
5718
5719 sleep(1); // prevent flood (FIXME make this an option?)
5720 }
5721
5722 // Send feed digests by email if needed.
5723 if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link);
5724
5725 } // function update_daemon_common
1a9f4d3c 5726
1681df97
AD
5727 function generate_dashboard_feed($link) {
5728
5729 print "<div id=\"headlinesContainer\">";
5730
5731 print_headline_subtoolbar($link, "", "Dashboard",
5732 false, false, -4, false, false, false,
5733 false, 0, 0, true, true, "tag.png");
5734
5735 print "<div id=\"headlinesInnerContainer\" class=\"dashboard\">";
5736 print "<div>There is <b>666</b> unread articles in <b>666</b> feeds.</div>";
5737 print "</div>";
5738
5739 print "</div>";
5740
5741 print "]]></headlines>";
5742 print "<headlines-count value=\"0\"/>";
5743 print "<headlines-unread value=\"0\"/>";
5744 print "<disable-cache value=\"1\"/>";
5745
5746 print "<articles>";
5747 print "</articles>";
5748 }
5749
621ffb00
AD
5750 function sanitize_article_content($text) {
5751 # we don't support CDATA sections in articles, they break our own escaping
5752 $text = preg_replace("/\[\[CDATA/", "", $text);
5753 $text = preg_replace("/\]\]\>/", "", $text);
5754 return $text;
5755 }
fee840fb
AD
5756
5757 function load_filters($link, $feed, $owner_uid, $action_id = false) {
5758 $filters = array();
5759
5760 if ($action_id) $ftype_query_part = "action_id = '$action_id' AND";
5761
5762 $result = db_query($link, "SELECT reg_exp,
5763 ttrss_filter_types.name AS name,
5764 ttrss_filter_actions.name AS action,
5765 inverse,
5766 action_param
5767 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
5768 enabled = true AND
5769 $ftype_query_part
5770 owner_uid = $owner_uid AND
5771 ttrss_filter_types.id = filter_type AND
5772 ttrss_filter_actions.id = action_id AND
5773 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
5774
5775 while ($line = db_fetch_assoc($result)) {
5776 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
5777 $filter["reg_exp"] = $line["reg_exp"];
5778 $filter["action"] = $line["action"];
5779 $filter["action_param"] = $line["action_param"];
5780 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
5781
5782 array_push($filters[$line["name"]], $filter);
5783 }
5784
5785 return $filters;
5786 }
1e36af0c
AD
5787
5788 function get_score_pic($score) {
1cce3aca 5789 if ($score > 100) {
1e36af0c 5790 return "score_high.png";
1cce3aca
AD
5791 } else if ($score > 0) {
5792 return "score_half_high.png";
5793 } else if ($score < -100) {
1e36af0c 5794 return "score_low.png";
1cce3aca
AD
5795 } else if ($score < 0) {
5796 return "score_half_low.png";
1e36af0c
AD
5797 } else {
5798 return "score_neutral.png";
5799 }
5800 }
ec92c9d1 5801
0745839a 5802 function rounded_table_start($classname, $header = "&nbsp;") {
ec92c9d1 5803 print "<table width='100%' class='$classname' cellspacing='0' cellpadding='0'>";
74d5c8fa 5804 print "<tr><td class='c1'>&nbsp;</td><td class='top'>$header</td><td class='c2'>&nbsp;</td></tr>";
ec92c9d1
AD
5805 print "<tr><td class='left'>&nbsp;</td><td class='content'>";
5806 }
5807
0745839a 5808 function rounded_table_end($footer = "&nbsp;") {
ec92c9d1 5809 print "</td><td class='right'>&nbsp;</td></tr>";
74d5c8fa 5810 print "<tr><td class='c4'>&nbsp;</td><td class='bottom'>$footer</td><td class='c3'>&nbsp;</td></tr>";
ec92c9d1
AD
5811 print "</table>";
5812 }
5813
071ec48f
AD
5814 function print_label_dlg_common_examples() {
5815
5816 print __("Match ") . " ";
5817
5be2805b
AD
5818/* print "<select name=\"label_andor\">";
5819 print "<option value=\"and\">AND</option>";
5820 print "<option value=\"or\">OR</option>";
5821 print "</select>"; */
5822
071ec48f
AD
5823 print "<select name=\"label_fields\" onchange=\"labelFieldsCheck(this)\">";
5824 print "<option value=\"unread\">".__("Unread articles")."</option>";
5825 print "<option value=\"updated\">".__("Updated articles")."</option>";
5826 print "<option value=\"kw_title\">".__("Title contains")."</option>";
5827 print "<option value=\"kw_content\">".__("Content contains")."</option>";
8c96d4b1
AD
5828 print "<option value=\"scoreE\">".__("Score equals")."</option>";
5829 print "<option value=\"scoreG\">".__("Score is greater than")."</option>";
5830 print "<option value=\"scoreL\">".__("Score is less than")."</option>";
071ec48f
AD
5831 print "<option value=\"newerH\">".__("Articles newer than X hours")."</option>";
5832 print "<option value=\"newerD\">".__("Articles newer than X days")."</option>";
5833
5834 print "</select>";
5835
5836 print "<input style=\"display : none\" name=\"label_fields_param\"
5837 size=\"10\">";
5838
5839 print " <input type=\"submit\"
5840 onclick=\"return addLabelExample()\"
5841 value=\"".__("Add")."\">";
5842 }
7defa089
AD
5843
5844 function feed_has_icon($id) {
5845 return is_file(ICONS_DIR . "/$id.ico") && filesize(ICONS_DIR . "/$id.ico") > 0;
5846 }
f29ba148
AD
5847
5848 function init_connection($link) {
5849 if (DB_TYPE == "pgsql") {
5850 pg_query("set client_encoding = 'UTF-8'");
5851 pg_set_client_encoding("UNICODE");
5852 } else {
5853 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
5854 db_query($link, "SET NAMES " . MYSQL_CHARSET);
5855 // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET);
5856 }
5857 }
5858 }
40d13c28 5859?>