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