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