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