]> git.wh0rd.org - tt-rss.git/blame - functions.php
backend/viewfeed: fix feed checking for validity
[tt-rss.git] / functions.php
CommitLineData
1d3a17c7 1<?php
f1a80dae 2
324944f3 3 date_default_timezone_set('UTC');
8a7f5767
CW
4 if (defined('E_DEPRECATED')) {
5 error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
6 } else {
7 error_reporting(E_ALL & ~E_NOTICE);
8 }
cce28758 9
40d13c28 10 require_once 'config.php';
cc17c205 11
fc2b26a6
AD
12 if (DB_TYPE == "pgsql") {
13 define('SUBSTRING_FOR_DATE', 'SUBSTRING_FOR_DATE');
14 } else {
15 define('SUBSTRING_FOR_DATE', 'SUBSTRING');
16 }
17
0c425dc7
AD
18 define('THEME_VERSION_REQUIRED', 1.1);
19
9632f884
AD
20 /**
21 * Return available translations names.
8d505d78 22 *
9632f884
AD
23 * @access public
24 * @return array A array of available translations.
25 */
f8c612d4 26 function get_translations() {
6a214f92 27 $tr = array(
8d505d78 28 "auto" => "Detect automatically",
a3162add 29 "ca_CA" => "Català",
6a214f92 30 "en_US" => "English",
36d0510c 31 "es_ES" => "Español",
a927fe7b 32 "de_DE" => "Deutsch",
6a214f92 33 "fr_FR" => "Français",
e78fd196 34 "hu_HU" => "Magyar (Hungarian)",
bb5d3960 35 "it_IT" => "Italiano",
1d004f12 36 "ja_JP" => "日本語 (Japanese)",
592535d7 37 "nb_NO" => "Norwegian bokmål",
6a214f92 38 "ru_RU" => "Русский",
9a063469 39 "pt_BR" => "Portuguese/Brazil",
6a214f92 40 "zh_CN" => "Simplified Chinese");
f8c612d4
AD
41
42 return $tr;
43 }
44
9632f884 45 if (ENABLE_TRANSLATIONS == true) { // If translations are enabled.
5de668ed 46 require_once "lib/accept-to-gettext.php";
0ba3a127 47 require_once "lib/gettext/gettext.inc";
aba609e0 48
8d039718 49 function startup_gettext() {
8d505d78 50
8d039718 51 # Get locale from Accept-Language header
6a214f92 52 $lang = al2gt(array_keys(get_translations()), "text/html");
89cb787e
AD
53
54 if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) {
55 $lang = _TRANSLATION_OVERRIDE_DEFAULT;
56 }
57
8d505d78 58 if ($_COOKIE["ttrss_lang"] && $_COOKIE["ttrss_lang"] != "auto") {
68659d98
AD
59 $lang = $_COOKIE["ttrss_lang"];
60 }
61
7c33dbd4
AD
62 /* In login action of mobile version */
63 if ($_POST["language"] && defined('MOBILE_VERSION')) {
64 $lang = $_POST["language"];
65 $_COOKIE["ttrss_lang"] = $lang;
66 }
67
8d039718 68 if ($lang) {
86e2e1b9
AD
69 if (defined('LC_MESSAGES')) {
70 _setlocale(LC_MESSAGES, $lang);
71 } else if (defined('LC_ALL')) {
72 _setlocale(LC_ALL, $lang);
73 } else {
74 die("can't setlocale(): please set ENABLE_TRANSLATIONS to false in config.php");
75 }
7c33dbd4
AD
76
77 if (defined('MOBILE_VERSION')) {
78 _bindtextdomain("messages", "../locale");
79 } else {
80 _bindtextdomain("messages", "locale");
81 }
82
8d039718
AD
83 _textdomain("messages");
84 _bind_textdomain_codeset("messages", "UTF-8");
85 }
aba609e0 86 }
aba609e0 87
cc17c205 88 startup_gettext();
865220a4 89
9632f884 90 } else { // If translations are enabled.
865220a4
AD
91 function __($msg) {
92 return $msg;
93 }
94 function startup_gettext() {
95 // no-op
96 return true;
97 }
9632f884 98 } // If translations are enabled.
cc17c205 99
be35798b
AD
100 if (defined('MEMCACHE_SERVER')) {
101 $memcache = new Memcache;
102 $memcache->connect(MEMCACHE_SERVER, 11211);
103 }
104
b619ff15 105 require_once 'db-prefs.php';
af106b0e 106 require_once 'errors.php';
8911ac8b 107 require_once 'version.php';
40d13c28 108
d134e3a3 109 require_once 'lib/phpmailer/class.phpmailer.php';
e4f7f8df 110 require_once 'lib/sphinxapi.php';
54a3dd8d 111 require_once 'lib/tmhoauth/tmhOAuth.php';
a8931123 112
a18a4f38 113 //define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')');
a3ee2a38 114 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
ba0f7628 115 define('MAGPIE_CACHE_AGE', 60*15); // 15 minutes
a3ee2a38 116
a18a4f38
AD
117 define('SELF_USER_AGENT', 'Tiny Tiny RSS/' . VERSION . ' (http://tt-rss.org/)');
118 define('MAGPIE_USER_AGENT', SELF_USER_AGENT);
119
500943a4
AD
120 ini_set('user_agent', SELF_USER_AGENT);
121
5ef03f15 122 require_once "lib/simplepie/simplepie.inc";
816cdfb7
AD
123 require_once "lib/magpierss/rss_fetch.inc";
124 require_once 'lib/magpierss/rss_utils.inc';
f45a286b 125 require_once 'lib/htmlpurifier/library/HTMLPurifier.auto.php';
49f9c923 126
35ffb5b9
AD
127 $config = HTMLPurifier_Config::createDefault();
128
ed29b175 129 $allowed = "p,a[href],i,em,b,strong,code,pre,blockquote,br,img[src|alt|title],ul,ol,li,h1,h2,h3,h4,s";
35ffb5b9
AD
130
131 $config->set('HTML', 'Allowed', $allowed);
132 $purifier = new HTMLPurifier($config);
133
45004d43
AD
134 /**
135 * Print a timestamped debug message.
8d505d78 136 *
45004d43
AD
137 * @param string $msg The debug message.
138 * @return void
139 */
6f9e33e4
AD
140 function _debug($msg) {
141 $ts = strftime("%H:%M:%S", time());
2a6a9395
AD
142 if (function_exists('posix_getpid')) {
143 $ts = "$ts/" . posix_getpid();
144 }
6f9e33e4 145 print "[$ts] $msg\n";
45004d43 146 } // function _debug
6f9e33e4 147
9632f884
AD
148 /**
149 * Purge a feed old posts.
8d505d78 150 *
9632f884
AD
151 * @param mixed $link A database connection.
152 * @param mixed $feed_id The id of the purged feed.
153 * @param mixed $purge_interval Olderness of purged posts.
154 * @param boolean $debug Set to True to enable the debug. False by default.
155 * @access public
156 * @return void
157 */
ad507f85
AD
158 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
159
07d0efe9 160 if (!$purge_interval) $purge_interval = feed_purge_interval($link, $feed_id);
8d505d78 161
ad507f85 162 $rows = -1;
4c193675 163
8d505d78 164 $result = db_query($link,
07d0efe9
AD
165 "SELECT owner_uid FROM ttrss_feeds WHERE id = '$feed_id'");
166
167 $owner_uid = false;
168
169 if (db_num_rows($result) == 1) {
170 $owner_uid = db_fetch_result($result, 0, "owner_uid");
171 }
172
ab954dff
AD
173 if ($purge_interval == -1 || !$purge_interval) {
174 if ($owner_uid) {
175 ccache_update($link, $feed_id, $owner_uid);
176 }
177 return;
178 }
179
07d0efe9
AD
180 if (!$owner_uid) return;
181
3907ef71
AD
182 if (FORCE_ARTICLE_PURGE == 0) {
183 $purge_unread = get_pref($link, "PURGE_UNREAD_ARTICLES",
184 $owner_uid, false);
185 } else {
186 $purge_unread = true;
187 $purge_interval = FORCE_ARTICLE_PURGE;
188 }
07d0efe9
AD
189
190 if (!$purge_unread) $query_limit = " unread = false AND ";
191
fefa6ca3 192 if (DB_TYPE == "pgsql") {
44e241cb 193/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 194 marked = false AND feed_id = '$feed_id' AND
25ea2805 195 (SELECT date_updated FROM ttrss_entries WHERE
44e241cb
AD
196 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
197
6e7f8d26
AD
198 $pg_version = get_pgsql_version($link);
199
200 if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
1e59ae35 201
8d505d78
AD
202 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
203 ttrss_entries.id = ref_id AND
204 marked = false AND
205 feed_id = '$feed_id' AND
07d0efe9 206 $query_limit
25ea2805 207 ttrss_entries.date_updated < NOW() - INTERVAL '$purge_interval days'");
1e59ae35
AD
208
209 } else {
210
8d505d78
AD
211 $result = db_query($link, "DELETE FROM ttrss_user_entries
212 USING ttrss_entries
213 WHERE ttrss_entries.id = ref_id AND
214 marked = false AND
215 feed_id = '$feed_id' AND
07d0efe9 216 $query_limit
25ea2805 217 ttrss_entries.date_updated < NOW() - INTERVAL '$purge_interval days'");
1e59ae35 218 }
ad507f85
AD
219
220 $rows = pg_affected_rows($result);
8d505d78 221
fefa6ca3 222 } else {
8d505d78 223
30f1746f 224/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 225 marked = false AND feed_id = '$feed_id' AND
8d505d78 226 (SELECT date_updated FROM ttrss_entries WHERE
30f1746f
AD
227 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
228
8d505d78
AD
229 $result = db_query($link, "DELETE FROM ttrss_user_entries
230 USING ttrss_user_entries, ttrss_entries
231 WHERE ttrss_entries.id = ref_id AND
232 marked = false AND
233 feed_id = '$feed_id' AND
07d0efe9 234 $query_limit
25ea2805 235 ttrss_entries.date_updated < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
8d505d78 236
ad507f85
AD
237 $rows = mysql_affected_rows($link);
238
239 }
240
ced46404
AD
241 ccache_update($link, $feed_id, $owner_uid);
242
ad507f85 243 if ($debug) {
6f9e33e4 244 _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
fefa6ca3 245 }
9632f884 246 } // function purge_feed
fefa6ca3 247
9632f884 248 /**
a2d79981 249 * Purge old posts from old feeds. Not used anymore, purging is done after feed update.
8d505d78 250 *
9632f884
AD
251 * @param mixed $link A database connection
252 * @param boolean $do_output Set to true to enable printed output, false by default.
253 * @param integer $limit The maximal number of removed posts.
254 * @access public
255 * @return void
256 */
a2d79981 257 /* function global_purge_old_posts($link, $do_output = false, $limit = false) {
44e241cb 258
894ebcf5 259 $random_qpart = sql_random_function();
fefa6ca3 260
44e241cb
AD
261 if ($limit) {
262 $limit_qpart = "LIMIT $limit";
263 } else {
264 $limit_qpart = "";
265 }
8d505d78
AD
266
267 $result = db_query($link,
268 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
44e241cb 269 ORDER BY $random_qpart $limit_qpart");
fefa6ca3
AD
270
271 while ($line = db_fetch_assoc($result)) {
272
273 $feed_id = $line["id"];
274 $purge_interval = $line["purge_interval"];
275 $owner_uid = $line["owner_uid"];
276
277 if ($purge_interval == 0) {
8d505d78
AD
278
279 $tmp_result = db_query($link,
fefa6ca3
AD
280 "SELECT value FROM ttrss_user_prefs WHERE
281 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
282
8d505d78 283 if (db_num_rows($tmp_result) != 0) {
fefa6ca3
AD
284 $purge_interval = db_fetch_result($tmp_result, 0, "value");
285 }
286 }
287
288 if ($do_output) {
ad507f85 289// print "Feed $feed_id: purge interval = $purge_interval\n";
fefa6ca3
AD
290 }
291
3907ef71 292 if ($purge_interval > 0 || FORCE_ARTICLE_PURGE) {
ad507f85 293 purge_feed($link, $feed_id, $purge_interval, $do_output);
fefa6ca3 294 }
8d505d78 295 }
fefa6ca3 296
a2d79981 297 purge_orphans($link, $do_output);
dab52d7b 298
a2d79981 299 } // function global_purge_old_posts */
fefa6ca3 300
07d0efe9
AD
301 function feed_purge_interval($link, $feed_id) {
302
8d505d78 303 $result = db_query($link, "SELECT purge_interval, owner_uid FROM ttrss_feeds
07d0efe9
AD
304 WHERE id = '$feed_id'");
305
306 if (db_num_rows($result) == 1) {
307 $purge_interval = db_fetch_result($result, 0, "purge_interval");
308 $owner_uid = db_fetch_result($result, 0, "owner_uid");
309
8d505d78 310 if ($purge_interval == 0) $purge_interval = get_pref($link,
863be6ca 311 'PURGE_OLD_DAYS', $owner_uid);
07d0efe9
AD
312
313 return $purge_interval;
314
315 } else {
316 return -1;
317 }
318 }
319
b6eefba5 320 function purge_old_posts($link) {
5d73494a 321
f1a80dae 322 $user_id = $_SESSION["uid"];
8d505d78
AD
323
324 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
f1a80dae 325 WHERE owner_uid = '$user_id'");
5d73494a
AD
326
327 while ($line = db_fetch_assoc($result)) {
328
329 $feed_id = $line["id"];
330 $purge_interval = $line["purge_interval"];
331
b619ff15 332 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
5d73494a 333
140aae81 334 if ($purge_interval > 0) {
fefa6ca3 335 purge_feed($link, $feed_id, $purge_interval);
5d73494a 336 }
8d505d78 337 }
71604ca4 338
0e0dd486
AD
339 purge_orphans($link);
340 }
341
a2d79981
AD
342 function purge_orphans($link, $do_output = false) {
343
71604ca4 344 // purge orphaned posts in main content table
8d505d78 345 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
71604ca4 346 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
a2d79981
AD
347
348 if ($do_output) {
349 $rows = db_affected_rows($link, $result);
350 _debug("Purged $rows orphaned posts.");
351 }
c3a8d71a
AD
352 }
353
c7d57b66
AD
354 function get_feed_update_interval($link, $feed_id) {
355 $result = db_query($link, "SELECT owner_uid, update_interval FROM
356 ttrss_feeds WHERE id = '$feed_id'");
357
358 if (db_num_rows($result) == 1) {
359 $update_interval = db_fetch_result($result, 0, "update_interval");
360 $owner_uid = db_fetch_result($result, 0, "owner_uid");
361
362 if ($update_interval != 0) {
363 return $update_interval;
364 } else {
365 return get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $owner_uid, false);
366 }
367
368 } else {
369 return -1;
370 }
371 }
372
8d505d78
AD
373 function fetch_file_contents($url, $type = false, $login = false, $pass = false) {
374 $login = urlencode($login);
375 $pass = urlencode($pass);
376
014d3ad8 377 if (function_exists('curl_init')) {
4065b60b 378 $ch = curl_init($url);
a1af1574
AD
379
380 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
381 curl_setopt($ch, CURLOPT_TIMEOUT, 45);
382 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
383 curl_setopt($ch, CURLOPT_MAXREDIRS, 20);
384 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
385 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
8d505d78
AD
386 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
387 curl_setopt($fp, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
388
389 if ($login && $pass)
390 curl_setopt($ch, CURLOPT_USERPWD, "$login:$pass");
a1af1574 391
fb074239 392 $contents = @curl_exec($ch);
a1af1574
AD
393 if ($contents === false) {
394 curl_close($ch);
395 return false;
4065b60b
AD
396 }
397
8d505d78 398 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
a1af1574
AD
399 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
400 curl_close($ch);
4065b60b 401
8d505d78 402 if ($http_code != 200 || $type && strpos($content_type, "$type") === false) {
a1af1574
AD
403 return false;
404 }
4065b60b 405
a1af1574 406 return $contents;
4065b60b 407 } else {
8d505d78
AD
408 if ($login && $pass && $updated != 3) {
409 $url_parts = array();
410
411 preg_match("/(^[^:]*):\/\/(.*)/", $url, $url_parts);
412
413 if ($url_parts[1] && $url_parts[2]) {
414 $url = $url_parts[1] . "://$login:$pass@" . $url_parts[2];
415 }
416 }
417
fb074239 418 return @file_get_contents($url);
4065b60b
AD
419 }
420
421 }
78800912 422
9632f884
AD
423 /**
424 * Try to determine the favicon URL for a feed.
425 * adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
426 * http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
8d505d78 427 *
9632f884
AD
428 * @param string $url A feed or page URL
429 * @access public
430 * @return mixed The favicon URL, or false if none was found.
431 */
4065b60b 432 function get_favicon_url($url) {
99331724 433
ed214298
AD
434 $favicon_url = false;
435
4065b60b 436 if ($html = @fetch_file_contents($url)) {
78800912 437
ed214298 438 libxml_use_internal_errors(true);
c798704b 439
ed214298
AD
440 $doc = new DOMDocument();
441 $doc->loadHTML($html);
442 $xpath = new DOMXPath($doc);
443 $entries = $xpath->query('/html/head/link[@rel="shortcut icon"]');
717f5e64 444
ed214298
AD
445 if (count($entries) > 0) {
446 foreach ($entries as $entry) {
447 $favicon_url = rewrite_relative_url($url, $entry->getAttribute("href"));
448 break;
449 }
8d505d78 450 }
4065b60b 451 }
c798704b 452
ed214298
AD
453 if (!$favicon_url)
454 $favicon_url = rewrite_relative_url($url, "/favicon.ico");
455
014d3ad8 456 return $favicon_url;
9632f884 457 } // function get_favicon_url
4065b60b 458
4065b60b
AD
459 function check_feed_favicon($site_url, $feed, $link) {
460 $favicon_url = get_favicon_url($site_url);
461
462# print "FAVICON [$site_url]: $favicon_url\n";
463
4065b60b
AD
464 $icon_file = ICONS_DIR . "/$feed.ico";
465
466 if ($favicon_url && !file_exists($icon_file)) {
a1af1574 467 $contents = fetch_file_contents($favicon_url, "image");
4065b60b 468
a1af1574
AD
469 if ($contents) {
470 $fp = fopen($icon_file, "w");
78800912 471
a1af1574
AD
472 if ($fp) {
473 fwrite($fp, $contents);
474 fclose($fp);
475 chmod($icon_file, 0644);
476 }
4065b60b 477 }
78800912
AD
478 }
479 }
480
c633e370
AD
481 function update_rss_feed($link, $feed, $ignore_daemon = false) {
482
483 global $memcache;
484
485 /* Update all feeds with the same URL to utilize memcache */
486
487 if ($memcache) {
8d505d78
AD
488 $result = db_query($link, "SELECT f1.id
489 FROM ttrss_feeds AS f1, ttrss_feeds AS f2
c633e370
AD
490 WHERE f2.feed_url = f1.feed_url AND f2.id = '$feed'");
491
492 while ($line = db_fetch_assoc($result)) {
493 update_rss_feed_real($link, $line["id"], $ignore_daemon);
494 }
495 } else {
496 update_rss_feed_real($link, $feed, $ignore_daemon);
497 }
498 }
499
500 function update_rss_feed_real($link, $feed, $ignore_daemon = false) {
40d13c28 501
602690e5
AD
502 global $memcache;
503
b4e75b2a 504 if (!$_REQUEST["daemon"] && !$ignore_daemon) {
45004d43 505 return false;
21cfcdf2
AD
506 }
507
b4e75b2a 508 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
34e420fb 509 _debug("update_rss_feed: start");
219bd8fc
AD
510 }
511
39a52499
AD
512 if (!$ignore_daemon) {
513
514 if (DB_TYPE == "pgsql") {
515 $updstart_thresh_qpart = "(ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')";
516 } else {
517 $updstart_thresh_qpart = "(ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))";
8d505d78
AD
518 }
519
39a52499 520 $result = db_query($link, "SELECT id,update_interval,auth_login,
16211ddb 521 auth_pass,cache_images,update_method
39a52499 522 FROM ttrss_feeds WHERE id = '$feed' AND $updstart_thresh_qpart");
02008cb1 523
39a52499
AD
524 } else {
525
526 $result = db_query($link, "SELECT id,update_interval,auth_login,
57e24c82
AD
527 feed_url,auth_pass,cache_images,update_method,last_updated,
528 owner_uid
39a52499
AD
529 FROM ttrss_feeds WHERE id = '$feed'");
530
531 }
a88c1f36 532
5370d37f 533 if (db_num_rows($result) == 0) {
b4e75b2a 534 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
c633e370 535 _debug("update_rss_feed: feed $feed NOT FOUND/SKIPPED");
8d505d78 536 }
45004d43 537 return false;
5370d37f
AD
538 }
539
16211ddb 540 $update_method = db_fetch_result($result, 0, "update_method");
50b2db96 541 $last_updated = db_fetch_result($result, 0, "last_updated");
57e24c82 542 $owner_uid = db_fetch_result($result, 0, "owner_uid");
16211ddb 543
3c50da83
AD
544 db_query($link, "UPDATE ttrss_feeds SET last_update_started = NOW()
545 WHERE id = '$feed'");
546
464bd61e
AD
547 $auth_login = db_fetch_result($result, 0, "auth_login");
548 $auth_pass = db_fetch_result($result, 0, "auth_pass");
549
b3009bcd
AD
550 if ($update_method == 0)
551 $update_method = DEFAULT_UPDATE_METHOD + 1;
552
553 // 1 - Magpie
554 // 2 - SimplePie
555 // 3 - Twitter OAuth
556
557 if ($update_method == 2)
558 $use_simplepie = true;
559 else
560 $use_simplepie = false;
16211ddb 561
b4e75b2a 562 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
b3009bcd 563 _debug("update method: $update_method (feed setting: $update_method) (use simplepie: $use_simplepie)\n");
16211ddb
AD
564 }
565
b3009bcd 566 if ($update_method == 1) {
464bd61e
AD
567 $auth_login = urlencode($auth_login);
568 $auth_pass = urlencode($auth_pass);
569 }
47c6c988 570
a88c1f36 571 $update_interval = db_fetch_result($result, 0, "update_interval");
bc0f0785 572 $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images"));
c633e370 573 $fetch_url = db_fetch_result($result, 0, "feed_url");
a88c1f36
AD
574
575 if ($update_interval < 0) { return; }
576
ab3d0b99
AD
577 $feed = db_escape_string($feed);
578
b3009bcd 579 if ($auth_login && $auth_pass && $updated != 3) {
47c6c988
AD
580 $url_parts = array();
581 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
582
583 if ($url_parts[1] && $url_parts[2]) {
584 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
585 }
586
587 }
ab3d0b99 588
b4e75b2a 589 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
ca872b9d 590 _debug("update_rss_feed: fetching [$fetch_url]...");
219bd8fc
AD
591 }
592
e656b9f7 593 $obj_id = md5("FDATA:$use_simplepie:$fetch_url");
6e4f0519 594
c633e370 595 if ($memcache && $obj = $memcache->get($obj_id)) {
6e4f0519
AD
596
597 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
c633e370 598 _debug("update_rss_feed: data found in memcache.");
6e4f0519
AD
599 }
600
c633e370
AD
601 $rss = $obj;
602
603 } else {
604
b3009bcd 605 if ($update_method == 3) {
57e24c82 606 $rss = fetch_twitter_rss($link, $fetch_url, $owner_uid);
b3009bcd 607 } else if ($update_method == 1) {
fb074239 608 $rss = @fetch_rss($fetch_url);
c633e370
AD
609 } else {
610 if (!is_dir(SIMPLEPIE_CACHE_DIR)) {
611 mkdir(SIMPLEPIE_CACHE_DIR);
612 }
8d505d78 613
c633e370 614 $rss = new SimplePie();
a18a4f38 615 $rss->set_useragent(SELF_USER_AGENT);
c633e370
AD
616 # $rss->set_timeout(10);
617 $rss->set_feed_url($fetch_url);
618 $rss->set_output_encoding('UTF-8');
8d505d78 619
c633e370 620 if (SIMPLEPIE_CACHE_IMAGES && $cache_images) {
9fcbc76f 621
c633e370 622 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
b8998470 623 _debug("enabling image cache");
c633e370 624 }
9fcbc76f 625
b8998470 626 $rss->set_image_handler("image.php", 'i');
c633e370 627 }
8d505d78 628
c633e370
AD
629 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
630 _debug("feed update interval (sec): " .
631 get_feed_update_interval($link, $feed)*60);
632 }
8d505d78 633
c633e370
AD
634 if (is_dir(SIMPLEPIE_CACHE_DIR)) {
635 $rss->set_cache_location(SIMPLEPIE_CACHE_DIR);
636 $rss->set_cache_duration(get_feed_update_interval($link, $feed) * 60);
637 }
8d505d78 638
c633e370 639 $rss->init();
6e4f0519
AD
640 }
641
c633e370 642 if ($memcache && $rss) $memcache->add($obj_id, $rss, 0, 300);
9fdf7824
AD
643 }
644
645// print_r($rss);
219bd8fc 646
b4e75b2a 647 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
34e420fb 648 _debug("update_rss_feed: fetch done, parsing...");
219bd8fc
AD
649 }
650
b6eefba5 651 $feed = db_escape_string($feed);
dcee8f61 652
b3009bcd 653 if ($update_method == 2) {
ca872b9d
AD
654 $fetch_ok = !$rss->error();
655 } else {
656 $fetch_ok = !!$rss;
657 }
658
659 if ($fetch_ok) {
50b62214 660
b4e75b2a 661 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
50b62214
AD
662 _debug("update_rss_feed: processing feed data...");
663 }
664
44e241cb 665// db_query($link, "BEGIN");
dd8c76a9 666
a88c1f36 667 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
f324892e 668 FROM ttrss_feeds WHERE id = '$feed'");
331900c6 669
b6eefba5
AD
670 $registered_title = db_fetch_result($result, 0, "title");
671 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
f324892e 672 $orig_site_url = db_fetch_result($result, 0, "site_url");
331900c6 673
7fed1940
AD
674 $owner_uid = db_fetch_result($result, 0, "owner_uid");
675
16211ddb 676 if ($use_simplepie) {
9fdf7824
AD
677 $site_url = $rss->get_link();
678 } else {
679 $site_url = $rss->channel["link"];
680 }
681
85a92289
AD
682 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
683 _debug("update_rss_feed: checking favicon...");
a2770077
AD
684 }
685
85a92289
AD
686 check_feed_favicon($site_url, $feed, $link);
687
746b249f 688 if (!$registered_title || $registered_title == "[Unknown]") {
4bc64807 689
16211ddb 690 if ($use_simplepie) {
c2f8aac4 691 $feed_title = db_escape_string($rss->get_title());
fb486a33
AD
692 } else {
693 $feed_title = db_escape_string($rss->channel["title"]);
694 }
4bc64807 695
b4e75b2a 696 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
4bc64807
AD
697 _debug("update_rss_feed: registering title: $feed_title");
698 }
8d505d78
AD
699
700 db_query($link, "UPDATE ttrss_feeds SET
f324892e
AD
701 title = '$feed_title' WHERE id = '$feed'");
702 }
703
49f9c923 704 // weird, weird Magpie
16211ddb 705 if (!$use_simplepie) {
9fdf7824
AD
706 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
707 }
147f7691
AD
708
709 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
8d505d78 710 db_query($link, "UPDATE ttrss_feeds SET
f324892e 711 site_url = '$site_url' WHERE id = '$feed'");
331900c6 712 }
40d13c28 713
b7f4bda2
AD
714// print "I: " . $rss->channel["image"]["url"];
715
16211ddb 716 if (!$use_simplepie) {
98833033 717 $icon_url = db_escape_string($rss->image["url"]);
9fdf7824 718 } else {
98833033 719 $icon_url = db_escape_string($rss->get_image_url());
9fdf7824 720 }
b7f4bda2 721
0526ffdd
AD
722 $icon_url = substr($icon_url, 0, 250);
723
8d505d78 724 if ($icon_url && $orig_icon_url != $icon_url) {
014d3ad8 725 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
b7f4bda2
AD
726 }
727
b4e75b2a 728 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
50b62214
AD
729 _debug("update_rss_feed: loading filters...");
730 }
e6155a06 731
5daa24f2 732 $filters = load_filters($link, $feed, $owner_uid);
e6155a06 733
0569a712
AD
734 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
735 print_r($filters);
736 }
737
16211ddb 738 if ($use_simplepie) {
9fdf7824
AD
739 $iterator = $rss->get_items();
740 } else {
741 $iterator = $rss->items;
742 if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
743 if (!$iterator || !is_array($iterator)) $iterator = $rss;
744 }
c22789da
AD
745
746 if (!is_array($iterator)) {
8d505d78 747 /* db_query($link, "UPDATE ttrss_feeds
75bd0669 748 SET last_error = 'Parse error: can\'t find any articles.'
77f0a2a7
AD
749 WHERE id = '$feed'"); */
750
751 // clear any errors and mark feed as updated if fetched okay
752 // even if it's blank
753
b4e75b2a 754 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
844012bc
AD
755 _debug("update_rss_feed: entry iterator is not an array, no articles?");
756 }
757
8d505d78 758 db_query($link, "UPDATE ttrss_feeds
77f0a2a7
AD
759 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
760
761 return; // no articles
c22789da 762 }
ddb68b81 763
b4e75b2a 764 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
50b62214
AD
765 _debug("update_rss_feed: processing articles...");
766 }
767
ddb68b81 768 foreach ($iterator as $item) {
7c5a308d 769
b4e75b2a 770 if ($_REQUEST['xdebug'] == 2) {
40ce98f4
AD
771 print_r($item);
772 }
71bd29f6 773
16211ddb 774 if ($use_simplepie) {
9fdf7824
AD
775 $entry_guid = $item->get_id();
776 if (!$entry_guid) $entry_guid = $item->get_link();
777 if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title());
778
779 } else {
780
781 $entry_guid = $item["id"];
34e420fb 782
9fdf7824 783 if (!$entry_guid) $entry_guid = $item["guid"];
530f5cda 784 if (!$entry_guid) $entry_guid = $item["about"];
9fdf7824
AD
785 if (!$entry_guid) $entry_guid = $item["link"];
786 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
787 }
be832a1a 788
b4e75b2a 789 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
34e420fb
AD
790 _debug("update_rss_feed: guid $entry_guid");
791 }
792
be832a1a
AD
793 if (!$entry_guid) continue;
794
795 $entry_timestamp = "";
796
16211ddb 797 if ($use_simplepie) {
9fdf7824
AD
798 $entry_timestamp = strtotime($item->get_date());
799 } else {
800 $rss_2_date = $item['pubdate'];
801 $rss_1_date = $item['dc']['date'];
802 $atom_date = $item['issued'];
803 if (!$atom_date) $atom_date = $item['updated'];
e9558345 804
9fdf7824
AD
805 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
806 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
807 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
e9558345 808
9fdf7824 809 }
9bb36aa0 810
2e930846 811 if ($entry_timestamp == "" || $entry_timestamp == -1 || !$entry_timestamp) {
be832a1a
AD
812 $entry_timestamp = time();
813 $no_orig_date = 'true';
814 } else {
815 $no_orig_date = 'false';
816 }
817
818 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
819
b4e75b2a 820 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
44d0e774
AD
821 _debug("update_rss_feed: date $entry_timestamp [$entry_timestamp_fmt]");
822 }
823
16211ddb 824 if ($use_simplepie) {
9fdf7824
AD
825 $entry_title = $item->get_title();
826 } else {
827 $entry_title = trim(strip_tags($item["title"]));
828 }
be832a1a 829
16211ddb 830 if ($use_simplepie) {
9fdf7824
AD
831 $entry_link = $item->get_link();
832 } else {
833 // strange Magpie workaround
834 $entry_link = $item["link_"];
835 if (!$entry_link) $entry_link = $item["link"];
836 }
be832a1a 837
b4e75b2a 838 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
9bb36aa0
AD
839 _debug("update_rss_feed: title $entry_title");
840 }
841
842 if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);;
7d3ab0dd 843
be832a1a 844 $entry_link = strip_tags($entry_link);
7d3ab0dd 845
16211ddb 846 if ($use_simplepie) {
4af7a36a
AD
847 $entry_content = $item->get_content();
848 if (!$entry_content) $entry_content = $item->get_description();
9fdf7824
AD
849 } else {
850 $entry_content = $item["content:escaped"];
851
852 if (!$entry_content) $entry_content = $item["content:encoded"];
e91ab107 853 if (!$entry_content) $entry_content = $item["content"]["encoded"];
9fdf7824 854 if (!$entry_content) $entry_content = $item["content"];
ea322415
AD
855
856 // Magpie bugs are getting ridiculous
857 if (trim($entry_content) == "Array") $entry_content = false;
858
9fdf7824
AD
859 if (!$entry_content) $entry_content = $item["atom_content"];
860 if (!$entry_content) $entry_content = $item["summary"];
e91ab107 861
8d505d78 862 if (!$entry_content ||
e91ab107
AD
863 strlen($entry_content) < strlen($item["description"])) {
864 $entry_content = $item["description"];
865 };
9fdf7824
AD
866
867 // WTF
868 if (is_array($entry_content)) {
869 $entry_content = $entry_content["encoded"];
870 if (!$entry_content) $entry_content = $entry_content["escaped"];
8d505d78 871 }
be832a1a
AD
872 }
873
b4e75b2a 874 if ($_REQUEST["xdebug"] == 2) {
ea322415
AD
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"]);
8d505d78 892
9fdf7824 893 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
4d6e9157 894
9fdf7824 895 if ($item['author']) {
8d505d78 896
9fdf7824 897 if (is_array($item['author'])) {
8d505d78 898
9fdf7824
AD
899 if (!$entry_author) {
900 $entry_author = db_escape_string(strip_tags($item['author']['name']));
901 }
8d505d78 902
9fdf7824
AD
903 if (!$entry_author) {
904 $entry_author = db_escape_string(strip_tags($item['author']['email']));
905 }
4d6e9157 906 }
8d505d78 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 918
8d505d78 919 $result = db_query($link, "SELECT id FROM ttrss_entries
be832a1a
AD
920 WHERE guid = '$entry_guid'");
921
d564f283 922 $entry_content = db_escape_string($entry_content, false);
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
c3fc5e47
AD
939 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
940 _debug("update_rss_feed: looking for tags [1]...");
941 }
942
fefef828 943 // parse <category> entries into tags
be832a1a 944
e75df19d
AD
945 $additional_tags = array();
946
16211ddb 947 if ($use_simplepie) {
be832a1a 948
9fdf7824 949 $additional_tags_src = $item->get_categories();
8d505d78 950
a702e931
AD
951 if (is_array($additional_tags_src)) {
952 foreach ($additional_tags_src as $tobj) {
953 array_push($additional_tags, $tobj->get_term());
954 }
fefef828 955 }
fefef828 956
b4e75b2a 957 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
6af621c7
AD
958 _debug("update_rss_feed: category tags:");
959 print_r($additional_tags);
960 }
961
9fdf7824 962 } else {
fefef828 963
9fdf7824 964 $t_ctr = $item['category#'];
fefef828 965
9fdf7824 966 if ($t_ctr == 0) {
e75df19d 967 $additional_tags = array();
71bd29f6 968 } else if ($t_ctr > 0) {
9fdf7824 969 $additional_tags = array($item['category']);
71bd29f6
AD
970
971 if ($item['category@term']) {
972 array_push($additional_tags, $item['category@term']);
973 }
974
9fdf7824
AD
975 for ($i = 0; $i <= $t_ctr; $i++ ) {
976 if ($item["category#$i"]) {
977 array_push($additional_tags, $item["category#$i"]);
978 }
71bd29f6
AD
979
980 if ($item["category#$i@term"]) {
981 array_push($additional_tags, $item["category#$i@term"]);
982 }
9fdf7824
AD
983 }
984 }
8d505d78 985
9fdf7824 986 // parse <dc:subject> elements
8d505d78 987
9fdf7824 988 $t_ctr = $item['dc']['subject#'];
8d505d78 989
71bd29f6 990 if ($t_ctr > 0) {
e75df19d 991 array_push($additional_tags, $item['dc']['subject']);
71bd29f6 992
9fdf7824
AD
993 for ($i = 0; $i <= $t_ctr; $i++ ) {
994 if ($item['dc']["subject#$i"]) {
995 array_push($additional_tags, $item['dc']["subject#$i"]);
996 }
fefef828
AD
997 }
998 }
999 }
8add756a 1000
c3fc5e47
AD
1001 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
1002 _debug("update_rss_feed: looking for tags [2]...");
1003 }
e91ab107 1004
c3fc5e47
AD
1005 /* taaaags */
1006 // <a href="..." rel="tag">Xorg</a>, //
e91ab107 1007
c3fc5e47 1008 $entry_tags = null;
e91ab107 1009
c3fc5e47
AD
1010 preg_match_all("/<a.*?rel=['\"]tag['\"].*?\>([^<]+)<\/a>/i",
1011 $entry_content_unescaped, $entry_tags);
e91ab107 1012
c3fc5e47 1013 $entry_tags = $entry_tags[1];
b652fdae 1014
c3fc5e47 1015 $entry_tags = array_merge($entry_tags, $additional_tags);
1e87951d
AD
1016 $entry_tags = array_unique($entry_tags);
1017
1018 for ($i = 0; $i < count($entry_tags); $i++)
1019 $entry_tags[$i] = mb_strtolower($entry_tags[$i], 'utf-8');
b652fdae 1020
c3fc5e47
AD
1021 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
1022 _debug("update_rss_feed: unfiltered tags found:");
1023 print_r($entry_tags);
ce53e200
AD
1024 }
1025
d48d160c 1026 # sanitize content
8d505d78 1027
621ffb00
AD
1028 $entry_content = sanitize_article_content($entry_content);
1029 $entry_title = sanitize_article_content($entry_title);
d48d160c 1030
b4e75b2a 1031 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
34e420fb
AD
1032 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
1033 }
1034
44e241cb
AD
1035 db_query($link, "BEGIN");
1036
4c193675
AD
1037 if (db_num_rows($result) == 0) {
1038
b4e75b2a 1039 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
34e420fb
AD
1040 _debug("update_rss_feed: base guid not found");
1041 }
1042
4c193675
AD
1043 // base post entry does not exist, create it
1044
4c193675 1045 $result = db_query($link,
8d505d78 1046 "INSERT INTO ttrss_entries
4c193675
AD
1047 (title,
1048 guid,
1049 link,
1050 updated,
1051 content,
1052 content_hash,
1053 no_orig_date,
25ea2805 1054 date_updated,
4c193675 1055 date_entered,
11b0dce2 1056 comments,
b6104dee
AD
1057 num_comments,
1058 author)
4c193675 1059 VALUES
8d505d78
AD
1060 ('$entry_title',
1061 '$entry_guid',
4c193675 1062 '$entry_link',
8d505d78
AD
1063 '$entry_timestamp_fmt',
1064 '$entry_content',
4c193675 1065 '$content_hash',
8d505d78 1066 $no_orig_date,
25ea2805
AD
1067 NOW(),
1068 NOW(),
11b0dce2 1069 '$entry_comments',
b6104dee
AD
1070 '$num_comments',
1071 '$entry_author')");
8926aab8
AD
1072 } else {
1073 // we keep encountering the entry in feeds, so we need to
25ea2805 1074 // update date_updated column so that we don't get horrible
8926aab8
AD
1075 // dupes when the entry gets purged and reinserted again e.g.
1076 // in the case of SLOW SLOW OMG SLOW updating feeds
1077
1078 $base_entry_id = db_fetch_result($result, 0, "id");
1079
25ea2805 1080 db_query($link, "UPDATE ttrss_entries SET date_updated = NOW()
8926aab8 1081 WHERE id = '$base_entry_id'");
4c193675
AD
1082 }
1083
1084 // now it should exist, if not - bad luck then
1085
8d505d78 1086 $result = db_query($link, "SELECT
6385315d 1087 id,content_hash,no_orig_date,title,
25ea2805 1088 ".SUBSTRING_FOR_DATE."(date_updated,1,19) as date_updated,
2ac6b765 1089 ".SUBSTRING_FOR_DATE."(updated,1,19) as updated,
11b0dce2 1090 num_comments
8d505d78
AD
1091 FROM
1092 ttrss_entries
6385315d 1093 WHERE guid = '$entry_guid'");
4c193675 1094
ce53e200
AD
1095 $entry_ref_id = 0;
1096 $entry_int_id = 0;
1097
4c193675
AD
1098 if (db_num_rows($result) == 1) {
1099
b4e75b2a 1100 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
7ca91eb3 1101 _debug("update_rss_feed: base guid found, checking for user record");
34e420fb
AD
1102 }
1103
11b0dce2
AD
1104 // this will be used below in update handler
1105 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
1106 $orig_title = db_fetch_result($result, 0, "title");
1107 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
8d505d78 1108 $orig_date_updated = strtotime(db_fetch_result($result,
25ea2805 1109 0, "date_updated"));
6385315d 1110
11b0dce2 1111 $ref_id = db_fetch_result($result, 0, "id");
ce53e200 1112 $entry_ref_id = $ref_id;
4c193675 1113
11b0dce2 1114 // check for user post link to main table
4c193675 1115
11b0dce2 1116 // do we allow duplicate posts with same GUID in different feeds?
8d0ec6fd 1117 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
e04c18a2 1118 $dupcheck_qpart = "AND (feed_id = '$feed' OR feed_id IS NULL)";
8d505d78 1119 } else {
11b0dce2
AD
1120 $dupcheck_qpart = "";
1121 }
71604ca4 1122
c3fc5e47
AD
1123 /* Collect article tags here so we could filter by them: */
1124
8d505d78
AD
1125 $article_filters = get_article_filters($filters, $entry_title,
1126 $entry_content, $entry_link, $entry_timestamp, $entry_author,
1e87951d 1127 $entry_tags);
19c9cb11 1128
b4e75b2a 1129 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
7ca91eb3
AD
1130 _debug("update_rss_feed: article filters: ");
1131 if (count($article_filters) != 0) {
1132 print_r($article_filters);
1133 }
1134 }
1135
f8382011 1136 if (find_article_filter($article_filters, "filter")) {
ee4a9812 1137 db_query($link, "COMMIT"); // close transaction in progress
11b0dce2
AD
1138 continue;
1139 }
19c9cb11 1140
ff6e357a
AD
1141 $score = calculate_article_score($article_filters);
1142
b4e75b2a 1143 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
ff6e357a
AD
1144 _debug("update_rss_feed: initial score: $score");
1145 }
1146
ef83538d 1147 $query = "SELECT ref_id, int_id FROM ttrss_user_entries WHERE
11b0dce2 1148 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
ef83538d
AD
1149 $dupcheck_qpart";
1150
b4e75b2a 1151// if ($_REQUEST["xdebug"]) print "$query\n";
ef83538d
AD
1152
1153 $result = db_query($link, $query);
7ca91eb3 1154
11b0dce2
AD
1155 // okay it doesn't exist - create user entry
1156 if (db_num_rows($result) == 0) {
1157
b4e75b2a 1158 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
7ca91eb3
AD
1159 _debug("update_rss_feed: user record not found, creating...");
1160 }
1161
24605713 1162 if ($score >= -500 && !find_article_filter($article_filters, 'catchup')) {
11b0dce2
AD
1163 $unread = 'true';
1164 $last_read_qpart = 'NULL';
1165 } else {
1166 $unread = 'false';
1167 $last_read_qpart = 'NOW()';
8d505d78 1168 }
dd7d3187 1169
32d59314 1170 if (find_article_filter($article_filters, 'mark') || $score > 1000) {
dd7d3187
AD
1171 $marked = 'true';
1172 } else {
1173 $marked = 'false';
1174 }
a36c0dfe
AD
1175
1176 if (find_article_filter($article_filters, 'publish')) {
1177 $published = 'true';
1178 } else {
1179 $published = 'false';
1180 }
1181
11b0dce2 1182 $result = db_query($link,
8d505d78
AD
1183 "INSERT INTO ttrss_user_entries
1184 (ref_id, owner_uid, feed_id, unread, last_read, marked,
1185 published, score, tag_cache, label_cache)
11b0dce2 1186 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
905ff52a 1187 $last_read_qpart, $marked, $published, '$score', '', '')");
ce53e200 1188
8d505d78 1189 $result = db_query($link,
ce53e200
AD
1190 "SELECT int_id FROM ttrss_user_entries WHERE
1191 ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND
1192 feed_id = '$feed' LIMIT 1");
1193
1194 if (db_num_rows($result) == 1) {
1195 $entry_int_id = db_fetch_result($result, 0, "int_id");
1196 }
1197 } else {
b4e75b2a 1198 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
e04c18a2
AD
1199 _debug("update_rss_feed: user record FOUND");
1200 }
1201
ce53e200
AD
1202 $entry_ref_id = db_fetch_result($result, 0, "ref_id");
1203 $entry_int_id = db_fetch_result($result, 0, "int_id");
11b0dce2 1204 }
ce53e200 1205
b4e75b2a 1206 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
ce53e200
AD
1207 _debug("update_rss_feed: RID: $entry_ref_id, IID: $entry_int_id");
1208 }
1209
6385315d
AD
1210 $post_needs_update = false;
1211
8d0ec6fd 1212 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
6385315d 1213 ($content_hash != $orig_content_hash)) {
7e43ad58 1214// print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->";
6385315d
AD
1215 $post_needs_update = true;
1216 }
1217
7e43ad58 1218 if (db_escape_string($orig_title) != $entry_title) {
6385315d
AD
1219 $post_needs_update = true;
1220 }
1221
11b0dce2
AD
1222 if ($orig_num_comments != $num_comments) {
1223 $post_needs_update = true;
1224 }
1225
6385315d
AD
1226// this doesn't seem to be very reliable
1227//
1228// if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
1229// $post_needs_update = true;
1230// }
1231
8d505d78
AD
1232 // if post needs update, update it and mark all user entries
1233 // linking to this post as updated
6385315d
AD
1234 if ($post_needs_update) {
1235
7ca91eb3
AD
1236 if (defined('DAEMON_EXTENDED_DEBUG')) {
1237 _debug("update_rss_feed: post $entry_guid needs update...");
1238 }
1239
6385315d
AD
1240// print "<!-- post $orig_title needs update : $post_needs_update -->";
1241
8d505d78 1242 db_query($link, "UPDATE ttrss_entries
11b0dce2 1243 SET title = '$entry_title', content = '$entry_content',
7e43ad58 1244 content_hash = '$content_hash',
11b0dce2 1245 num_comments = '$num_comments'
6385315d
AD
1246 WHERE id = '$ref_id'");
1247
8d0ec6fd 1248 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
8d505d78 1249 db_query($link, "UPDATE ttrss_user_entries
4919fb42
AD
1250 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
1251 } else {
8d505d78 1252 db_query($link, "UPDATE ttrss_user_entries
4919fb42
AD
1253 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
1254 }
6385315d
AD
1255
1256 }
4c193675
AD
1257 }
1258
44e241cb
AD
1259 db_query($link, "COMMIT");
1260
b4e75b2a 1261 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
ceb30ba4
AD
1262 _debug("update_rss_feed: assigning labels...");
1263 }
1264
1265 assign_article_to_labels($link, $entry_ref_id, $article_filters,
1266 $owner_uid);
1267
b4e75b2a 1268 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
ce53e200
AD
1269 _debug("update_rss_feed: looking for enclosures...");
1270 }
1271
c3fc5e47
AD
1272 // enclosures
1273
1274 $enclosures = array();
1275
1276 if ($use_simplepie) {
1277 $encs = $item->get_enclosures();
1278
1279 if (is_array($encs)) {
1280 foreach ($encs as $e) {
1281 $e_item = array(
1282 $e->link, $e->type, $e->length);
8d505d78 1283
c3fc5e47
AD
1284 array_push($enclosures, $e_item);
1285 }
1286 }
1287
1288 } else {
1289 // <enclosure>
1290
1291 $e_ctr = $item['enclosure#'];
1292
1293 if ($e_ctr > 0) {
1294 $e_item = array($item['enclosure@url'],
1295 $item['enclosure@type'],
1296 $item['enclosure@length']);
1297
1298 array_push($enclosures, $e_item);
1299
1300 for ($i = 0; $i <= $e_ctr; $i++ ) {
1301
1302 if ($item["enclosure#$i@url"]) {
1303 $e_item = array($item["enclosure#$i@url"],
1304 $item["enclosure#$i@type"],
1305 $item["enclosure#$i@length"]);
1306 array_push($enclosures, $e_item);
1307 }
1308 }
1309 }
1310
1311 // <media:content>
1312 // can there be many of those? yes -fox
1313
1314 $m_ctr = $item['media']['content#'];
1315
1316 if ($m_ctr > 0) {
1317 $e_item = array($item['media']['content@url'],
1318 $item['media']['content@medium'],
1319 $item['media']['content@length']);
1320
1321 array_push($enclosures, $e_item);
1322
1323 for ($i = 0; $i <= $m_ctr; $i++ ) {
1324
1325 if ($item["media"]["content#$i@url"]) {
1326 $e_item = array($item["media"]["content#$i@url"],
1327 $item["media"]["content#$i@medium"],
1328 $item["media"]["content#$i@length"]);
1329 array_push($enclosures, $e_item);
1330 }
1331 }
1332
1333 }
1334 }
1335
1336
b4e75b2a 1337 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
c3fc5e47 1338 _debug("update_rss_feed: article enclosures:");
ce53e200
AD
1339 print_r($enclosures);
1340 }
1341
1342 db_query($link, "BEGIN");
1343
1344 foreach ($enclosures as $enc) {
1345 $enc_url = db_escape_string($enc[0]);
1346 $enc_type = db_escape_string($enc[1]);
1347 $enc_dur = db_escape_string($enc[2]);
1348
1349 $result = db_query($link, "SELECT id FROM ttrss_enclosures
1350 WHERE content_url = '$enc_url' AND post_id = '$entry_ref_id'");
1351
1352 if (db_num_rows($result) == 0) {
1353 db_query($link, "INSERT INTO ttrss_enclosures
1354 (content_url, content_type, title, duration, post_id) VALUES
1355 ('$enc_url', '$enc_type', '', '$enc_dur', '$entry_ref_id')");
1356 }
1357 }
1358
1359 db_query($link, "COMMIT");
1360
c3fc5e47 1361 // check for manual tags (we have to do it here since they're loaded from filters)
073ca0e6 1362
4d50f419
AD
1363 foreach ($article_filters as $f) {
1364 if ($f[0] == "tag") {
f8382011 1365
4d50f419 1366 $manual_tags = trim_array(split(",", $f[1]));
073ca0e6 1367
4d50f419
AD
1368 foreach ($manual_tags as $tag) {
1369 if (tag_is_valid($tag)) {
1370 array_push($entry_tags, $tag);
1371 }
be832a1a
AD
1372 }
1373 }
1374 }
1375
c3fc5e47
AD
1376 // Skip boring tags
1377
8d505d78 1378 $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link,
11c9ea1f 1379 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8')));
8fc70781 1380
c3fc5e47 1381 $filtered_tags = array();
779560b7 1382 $tags_to_cache = array();
c3fc5e47
AD
1383
1384 if ($entry_tags && is_array($entry_tags)) {
1385 foreach ($entry_tags as $tag) {
1386 if (array_search($tag, $boring_tags) === false) {
1387 array_push($filtered_tags, $tag);
073ca0e6
AD
1388 }
1389 }
8d505d78 1390 }
fefef828 1391
779560b7
AD
1392 $filtered_tags = array_unique($filtered_tags);
1393
b4e75b2a 1394 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
c3fc5e47
AD
1395 _debug("update_rss_feed: filtered article tags:");
1396 print_r($filtered_tags);
9fdf7824
AD
1397 }
1398
c3fc5e47
AD
1399 // Save article tags in the database
1400
1401 if (count($filtered_tags) > 0) {
8d505d78 1402
44e241cb 1403 db_query($link, "BEGIN");
8d505d78 1404
779560b7 1405 foreach ($filtered_tags as $tag) {
fefef828 1406
779560b7
AD
1407 $tag = sanitize_tag($tag);
1408 $tag = db_escape_string($tag);
1409
1410 if (!tag_is_valid($tag)) continue;
8d505d78
AD
1411
1412 $result = db_query($link, "SELECT id FROM ttrss_tags
1413 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
779560b7 1414 owner_uid = '$owner_uid' LIMIT 1");
31483fc1 1415
fe99ab12 1416 if ($result && db_num_rows($result) == 0) {
8d505d78
AD
1417
1418 db_query($link, "INSERT INTO ttrss_tags
fe99ab12
AD
1419 (owner_uid,tag_name,post_int_id)
1420 VALUES ('$owner_uid','$tag', '$entry_int_id')");
779560b7 1421 }
490c366d 1422
779560b7
AD
1423 array_push($tags_to_cache, $tag);
1424 }
490c366d 1425
779560b7 1426 /* update the cache */
8d505d78 1427
779560b7 1428 $tags_to_cache = array_unique($tags_to_cache);
8d505d78 1429
779560b7 1430 $tags_str = db_escape_string(join(",", $tags_to_cache));
490c366d 1431
8d505d78 1432 db_query($link, "UPDATE ttrss_user_entries
779560b7
AD
1433 SET tag_cache = '$tags_str' WHERE ref_id = '$entry_ref_id'
1434 AND owner_uid = $owner_uid");
ce53e200 1435
44e241cb 1436 db_query($link, "COMMIT");
8d505d78 1437 }
9fdf7824 1438
b4e75b2a 1439 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
9fdf7824
AD
1440 _debug("update_rss_feed: article processed");
1441 }
8d505d78 1442 }
40d13c28 1443
50b2db96 1444 if (!$last_updated) {
b4e75b2a 1445 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
50b2db96
AD
1446 _debug("update_rss_feed: new feed, catching it up...");
1447 }
c7e51de1 1448 catchup_feed($link, $feed, false, $owner_uid);
50b2db96
AD
1449 }
1450
0e70ed51 1451 purge_feed($link, $feed, 0);
3907ef71 1452
8d505d78 1453 db_query($link, "UPDATE ttrss_feeds
ab3d0b99 1454 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
eb36b4eb 1455
44e241cb 1456// db_query($link, "COMMIT");
dd8c76a9 1457
ab3d0b99 1458 } else {
ca872b9d 1459
16211ddb 1460 if ($use_simplepie) {
ca872b9d 1461 $error_msg = mb_substr($rss->error(), 0, 250);
9fdf7824 1462 } else {
ca872b9d 1463 $error_msg = mb_substr(magpie_error(), 0, 250);
9fdf7824 1464 }
ca872b9d 1465
b4e75b2a 1466 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
ca872b9d
AD
1467 _debug("update_rss_feed: error fetching feed: $error_msg");
1468 }
1469
1470 $error_msg = db_escape_string($error_msg);
1471
8d505d78
AD
1472 db_query($link,
1473 "UPDATE ttrss_feeds SET last_error = '$error_msg',
aa5f9f5f 1474 last_updated = NOW() WHERE id = '$feed'");
40d13c28
AD
1475 }
1476
16211ddb 1477 if ($use_simplepie) {
ac6ebdb3
AD
1478 unset($rss);
1479 }
1480
b4e75b2a 1481 if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
34e420fb 1482 _debug("update_rss_feed: done");
219bd8fc
AD
1483 }
1484
40d13c28
AD
1485 }
1486
f175937c 1487 function print_select($id, $default, $values, $attributes = "") {
79f3553b 1488 print "<select name=\"$id\" id=\"$id\" $attributes>";
a0d53889
AD
1489 foreach ($values as $v) {
1490 if ($v == $default)
60807300 1491 $sel = "selected=\"1\"";
a0d53889
AD
1492 else
1493 $sel = "";
8d505d78 1494
60807300 1495 print "<option value=\"$v\" $sel>$v</option>";
a0d53889
AD
1496 }
1497 print "</select>";
1498 }
40d13c28 1499
79f3553b
AD
1500 function print_select_hash($id, $default, $values, $attributes = "") {
1501 print "<select name=\"$id\" id='$id' $attributes>";
673d54ca
AD
1502 foreach (array_keys($values) as $v) {
1503 if ($v == $default)
74d5c8fa 1504 $sel = 'selected="selected"';
673d54ca
AD
1505 else
1506 $sel = "";
8d505d78 1507
673d54ca
AD
1508 print "<option $sel value=\"$v\">".$values[$v]."</option>";
1509 }
1510
1511 print "</select>";
1512 }
1513
c3fc5e47 1514 function get_article_filters($filters, $title, $content, $link, $timestamp, $author, $tags) {
240054f1 1515 $matches = array();
c2d9322b 1516
240054f1
AD
1517 if ($filters["title"]) {
1518 foreach ($filters["title"] as $filter) {
8d505d78
AD
1519 $reg_exp = $filter["reg_exp"];
1520 $inverse = $filter["inverse"];
1521 if ((!$inverse && @preg_match("/$reg_exp/i", $title)) ||
a9d63d29 1522 ($inverse && !@preg_match("/$reg_exp/i", $title))) {
c2d9322b 1523
240054f1
AD
1524 array_push($matches, array($filter["action"], $filter["action_param"]));
1525 }
1526 }
1527 }
1528
1529 if ($filters["content"]) {
1530 foreach ($filters["content"] as $filter) {
c2d9322b
AD
1531 $reg_exp = $filter["reg_exp"];
1532 $inverse = $filter["inverse"];
1533
8d505d78 1534 if ((!$inverse && @preg_match("/$reg_exp/i", $content)) ||
a9d63d29 1535 ($inverse && !@preg_match("/$reg_exp/i", $content))) {
c2d9322b 1536
240054f1 1537 array_push($matches, array($filter["action"], $filter["action_param"]));
8d505d78 1538 }
240054f1
AD
1539 }
1540 }
1541
1542 if ($filters["both"]) {
8d505d78
AD
1543 foreach ($filters["both"] as $filter) {
1544 $reg_exp = $filter["reg_exp"];
c2d9322b
AD
1545 $inverse = $filter["inverse"];
1546
1547 if ($inverse) {
a9d63d29 1548 if (!@preg_match("/$reg_exp/i", $title) && !preg_match("/$reg_exp/i", $content)) {
c2d9322b
AD
1549 array_push($matches, array($filter["action"], $filter["action_param"]));
1550 }
1551 } else {
a9d63d29 1552 if (@preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) {
c2d9322b
AD
1553 array_push($matches, array($filter["action"], $filter["action_param"]));
1554 }
240054f1
AD
1555 }
1556 }
1557 }
1558
1559 if ($filters["link"]) {
1560 $reg_exp = $filter["reg_exp"];
1561 foreach ($filters["link"] as $filter) {
1562 $reg_exp = $filter["reg_exp"];
c2d9322b
AD
1563 $inverse = $filter["inverse"];
1564
8d505d78 1565 if ((!$inverse && @preg_match("/$reg_exp/i", $link)) ||
a9d63d29 1566 ($inverse && !@preg_match("/$reg_exp/i", $link))) {
8d505d78 1567
240054f1
AD
1568 array_push($matches, array($filter["action"], $filter["action_param"]));
1569 }
1570 }
1571 }
1572
44d0e774
AD
1573 if ($filters["date"]) {
1574 $reg_exp = $filter["reg_exp"];
1575 foreach ($filters["date"] as $filter) {
1576 $date_modifier = $filter["filter_param"];
1577 $inverse = $filter["inverse"];
1578 $check_timestamp = strtotime($filter["reg_exp"]);
1579
1580 # no-op when timestamp doesn't parse to prevent misfires
1581
1582 if ($check_timestamp) {
1583 $match_ok = false;
1584
1585 if ($date_modifier == "before" && $timestamp < $check_timestamp ||
1586 $date_modifier == "after" && $timestamp > $check_timestamp) {
1587 $match_ok = true;
1588 }
1589
1590 if ($inverse) $match_ok = !$match_ok;
1591
1592 if ($match_ok) {
1593 array_push($matches, array($filter["action"], $filter["action_param"]));
1594 }
1595 }
1596 }
8d505d78 1597 }
44d0e774 1598
fa3317be
AD
1599 if ($filters["author"]) {
1600 foreach ($filters["author"] as $filter) {
8d505d78
AD
1601 $reg_exp = $filter["reg_exp"];
1602 $inverse = $filter["inverse"];
1603 if ((!$inverse && @preg_match("/$reg_exp/i", $author)) ||
a9d63d29 1604 ($inverse && !@preg_match("/$reg_exp/i", $author))) {
fa3317be
AD
1605
1606 array_push($matches, array($filter["action"], $filter["action_param"]));
1607 }
1608 }
1609 }
1610
c3fc5e47
AD
1611 if ($filters["tag"]) {
1612
1613 $tag_string = join(",", $tags);
1614
1615 foreach ($filters["tag"] as $filter) {
1616 $reg_exp = $filter["reg_exp"];
1617 $inverse = $filter["inverse"];
1618
8d505d78 1619 if ((!$inverse && @preg_match("/$reg_exp/i", $tag_string)) ||
a9d63d29 1620 ($inverse && !@preg_match("/$reg_exp/i", $tag_string))) {
c3fc5e47
AD
1621
1622 array_push($matches, array($filter["action"], $filter["action_param"]));
8d505d78 1623 }
c3fc5e47
AD
1624 }
1625 }
1626
1627
240054f1
AD
1628 return $matches;
1629 }
1630
f8382011
AD
1631 function find_article_filter($filters, $filter_name) {
1632 foreach ($filters as $f) {
1633 if ($f[0] == $filter_name) {
1634 return $f;
1635 };
1636 }
1637 return false;
1638 }
1639
ff6e357a
AD
1640 function calculate_article_score($filters) {
1641 $score = 0;
1642
1643 foreach ($filters as $f) {
1644 if ($f[0] == "score") {
1645 $score += $f[1];
1646 };
1647 }
1648 return $score;
1649 }
1650
ceb30ba4
AD
1651 function assign_article_to_labels($link, $id, $filters, $owner_uid) {
1652 foreach ($filters as $f) {
1653 if ($f[0] == "label") {
1654 label_add_article($link, $id, $f[1], $owner_uid);
1655 };
1656 }
1657 }
ff6e357a 1658
406d9489
AD
1659 function getmicrotime() {
1660 list($usec, $sec) = explode(" ",microtime());
1661 return ((float)$usec + (float)$sec);
1662 }
1663
f541eb78 1664 function print_radio($id, $default, $true_is, $values, $attributes = "") {
77e96719 1665 foreach ($values as $v) {
8d505d78 1666
77e96719 1667 if ($v == $default)
5da169d9 1668 $sel = "checked";
77e96719 1669 else
5da169d9
AD
1670 $sel = "";
1671
f541eb78 1672 if ($v == $true_is) {
5da169d9
AD
1673 $sel .= " value=\"1\"";
1674 } else {
1675 $sel .= " value=\"0\"";
1676 }
8d505d78
AD
1677
1678 print "<input class=\"noborder\" dojoType=\"dijit.form.RadioButton\"
69654950 1679 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
77e96719
AD
1680
1681 }
1682 }
1683
d9084cf2 1684 function initialize_user_prefs($link, $uid, $profile = false) {
ff485f1d
AD
1685
1686 $uid = db_escape_string($uid);
1687
d9084cf2
AD
1688 if (!$profile) {
1689 $profile = "NULL";
f9aa6a89 1690 $profile_qpart = "AND profile IS NULL";
d9084cf2 1691 } else {
f9aa6a89 1692 $profile_qpart = "AND profile = '$profile'";
d9084cf2
AD
1693 }
1694
f9aa6a89
AD
1695 if (get_schema_version($link) < 63) $profile_qpart = "";
1696
ff485f1d
AD
1697 db_query($link, "BEGIN");
1698
1699 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
8d505d78
AD
1700
1701 $u_result = db_query($link, "SELECT pref_name
f9aa6a89 1702 FROM ttrss_user_prefs WHERE owner_uid = '$uid' $profile_qpart");
ff485f1d
AD
1703
1704 $active_prefs = array();
1705
1706 while ($line = db_fetch_assoc($u_result)) {
8d505d78 1707 array_push($active_prefs, $line["pref_name"]);
ff485f1d
AD
1708 }
1709
1710 while ($line = db_fetch_assoc($result)) {
1711 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
1712// print "adding " . $line["pref_name"] . "<br>";
1713
f9aa6a89
AD
1714 if (get_schema_version($link) < 63) {
1715 db_query($link, "INSERT INTO ttrss_user_prefs
8d505d78 1716 (owner_uid,pref_name,value) VALUES
f9aa6a89
AD
1717 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
1718
1719 } else {
1720 db_query($link, "INSERT INTO ttrss_user_prefs
8d505d78 1721 (owner_uid,pref_name,value, profile) VALUES
f9aa6a89
AD
1722 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."', $profile)");
1723 }
ff485f1d
AD
1724
1725 }
1726 }
1727
1728 db_query($link, "COMMIT");
1729
1730 }
956c7629
AD
1731
1732 function lookup_user_id($link, $user) {
1733
8d505d78 1734 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
956c7629
AD
1735 login = '$login'");
1736
1737 if (db_num_rows($result) == 1) {
1738 return db_fetch_result($result, 0, "id");
1739 } else {
1740 return false;
1741 }
1742 }
1743
18664970
AD
1744 function http_authenticate_user($link) {
1745
99ea1043 1746// error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log');
4bc64807 1747
18664970
AD
1748 if (!$_SERVER["PHP_AUTH_USER"]) {
1749
1750 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1751 header('HTTP/1.0 401 Unauthorized');
1752 exit;
8d505d78 1753
18664970 1754 } else {
8d505d78 1755 $auth_result = authenticate_user($link,
18664970
AD
1756 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1757
1758 if (!$auth_result) {
1759 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1760 header('HTTP/1.0 401 Unauthorized');
1761 exit;
1762 }
1763 }
1764
1765 return true;
1766 }
1767
461766f3 1768 function authenticate_user($link, $login, $password, $force_auth = false) {
c8437f35 1769
131b01b3 1770 if (!SINGLE_USER_MODE) {
c8437f35 1771
1a9f4d3c
AD
1772 $pwd_hash1 = encrypt_password($password);
1773 $pwd_hash2 = encrypt_password($password, $login);
2d969845 1774 $login = db_escape_string($login);
461766f3 1775
8d505d78 1776 if (defined('ALLOW_REMOTE_USER_AUTH') && ALLOW_REMOTE_USER_AUTH
73f5f114 1777 && $_SERVER["REMOTE_USER"] && $login != "admin") {
66917e70
AD
1778
1779 $login = db_escape_string($_SERVER["REMOTE_USER"]);
1780
73f5f114 1781 $query = "SELECT id,login,access_level,pwd_hash
461766f3 1782 FROM ttrss_users WHERE
66917e70
AD
1783 login = '$login'";
1784
461766f3 1785 } else {
1a9f4d3c 1786 $query = "SELECT id,login,access_level,pwd_hash
461766f3 1787 FROM ttrss_users WHERE
1a9f4d3c
AD
1788 login = '$login' AND (pwd_hash = '$pwd_hash1' OR
1789 pwd_hash = '$pwd_hash2')";
461766f3
AD
1790 }
1791
1792 $result = db_query($link, $query);
8d505d78 1793
131b01b3
AD
1794 if (db_num_rows($result) == 1) {
1795 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1796 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1797 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
8d505d78
AD
1798
1799 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
131b01b3 1800 $_SESSION["uid"]);
8d505d78 1801
131b01b3 1802 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1a9f4d3c 1803 $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash");
91c5f229
AD
1804
1805 $_SESSION["last_version_check"] = time();
8d505d78 1806
131b01b3 1807 initialize_user_prefs($link, $_SESSION["uid"]);
8d505d78 1808
131b01b3
AD
1809 return true;
1810 }
8d505d78 1811
131b01b3 1812 return false;
503eb349 1813
131b01b3 1814 } else {
503eb349 1815
131b01b3
AD
1816 $_SESSION["uid"] = 1;
1817 $_SESSION["name"] = "admin";
f557cd78 1818
0bbba72d 1819 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
8d505d78 1820
0bbba72d 1821 initialize_user_prefs($link, $_SESSION["uid"]);
8d505d78 1822
c8437f35
AD
1823 return true;
1824 }
c8437f35
AD
1825 }
1826
e6cb77a0
AD
1827 function make_password($length = 8) {
1828
1829 $password = "";
8d505d78
AD
1830 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1831
1832 $i = 0;
1833
1834 while ($i < $length) {
e6cb77a0 1835 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
8d505d78
AD
1836
1837 if (!strstr($password, $char)) {
e6cb77a0
AD
1838 $password .= $char;
1839 $i++;
1840 }
1841 }
1842 return $password;
1843 }
1844
1845 // this is called after user is created to initialize default feeds, labels
1846 // or whatever else
8d505d78 1847
e6cb77a0
AD
1848 // user preferences are checked on every login, not here
1849
1850 function initialize_user($link, $uid) {
1851
e6cb77a0 1852 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 1853 values ('$uid', 'Tiny Tiny RSS: New Releases',
b6d486a3 1854 'http://tt-rss.org/releases.rss')");
3b0feb9b 1855
cd2cd415
AD
1856 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1857 values ('$uid', 'Tiny Tiny RSS: Forum',
f0855b88 1858 'http://tt-rss.org/forum/rss.php')");
3b0feb9b 1859 }
e6cb77a0 1860
b8aa49bc 1861 function logout_user() {
5ccc1cf5
AD
1862 session_destroy();
1863 if (isset($_COOKIE[session_name()])) {
1864 setcookie(session_name(), '', time()-42000, '/');
1865 }
b8aa49bc
AD
1866 }
1867
75836f33 1868 function get_script_urlpath() {
87a79fa4 1869 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
75836f33
AD
1870 }
1871
916f788a 1872 function validate_session($link) {
0f41fce8
AD
1873 if (SINGLE_USER_MODE) return true;
1874
1875 $check_ip = $_SESSION['ip_address'];
1876
1877 switch (SESSION_CHECK_ADDRESS) {
1878 case 0:
1879 $check_ip = '';
1880 break;
1881 case 1:
1882 $check_ip = substr($check_ip, 0, strrpos($check_ip, '.')+1);
1883 break;
1884 case 2:
1885 $check_ip = substr($check_ip, 0, strrpos($check_ip, '.'));
1886 $check_ip = substr($check_ip, 0, strrpos($check_ip, '.')+1);
1887 break;
1888 };
1889
d769a0f7 1890 if ($check_ip && strpos($_SERVER['REMOTE_ADDR'], $check_ip) !== 0) {
8d505d78 1891 $_SESSION["login_error_msg"] =
d769a0f7
AD
1892 __("Session failed to validate (incorrect IP)");
1893 return false;
1894 }
0f41fce8
AD
1895
1896 if ($_SESSION["ref_schema_version"] != get_schema_version($link, true))
05044a59 1897 return false;
05044a59 1898
e6684130
AD
1899 if ($_SESSION["uid"]) {
1900
8d505d78 1901 $result = db_query($link,
e6684130
AD
1902 "SELECT pwd_hash FROM ttrss_users WHERE id = '".$_SESSION["uid"]."'");
1903
1904 $pwd_hash = db_fetch_result($result, 0, "pwd_hash");
1905
1906 if ($pwd_hash != $_SESSION["pwd_hash"]) {
1907 return false;
1908 }
1909 }
1910
a885f0ec 1911/* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
d620cfe7 1912
8e849206 1913 //print_r($_SESSION);
d620cfe7
AD
1914
1915 if (time() > $_SESSION["cookie_lifetime"]) {
1916 return false;
1917 }
a885f0ec
AD
1918 } */
1919
916f788a
AD
1920 return true;
1921 }
1922
793185a9 1923 function login_sequence($link, $mobile = false) {
b8aa49bc 1924 if (!SINGLE_USER_MODE) {
75836f33 1925
7f0acba7 1926 $login_action = $_POST["login_action"];
a885f0ec 1927
8d505d78 1928 # try to authenticate user if called from login form
7f0acba7 1929 if ($login_action == "do_login") {
01a87dff
AD
1930 $login = $_POST["login"];
1931 $password = $_POST["password"];
d620cfe7 1932 $remember_me = $_POST["remember_me"];
f557cd78 1933
01a87dff
AD
1934 if (authenticate_user($link, $login, $password)) {
1935 $_POST["password"] = "";
d620cfe7 1936
f8c612d4 1937 $_SESSION["language"] = $_POST["language"];
05044a59 1938 $_SESSION["ref_schema_version"] = get_schema_version($link, true);
a598370d 1939 $_SESSION["bw_limit"] = !!$_POST["bw_limit"];
f8c612d4 1940
d9084cf2
AD
1941 if ($_POST["profile"]) {
1942
1943 $profile = db_escape_string($_POST["profile"]);
1944
1945 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
1946 WHERE id = '$profile' AND owner_uid = " . $_SESSION["uid"]);
1947
1948 if (db_num_rows($result) != 0) {
1949 $_SESSION["profile"] = $profile;
1950 $_SESSION["prefs_cache"] = array();
1951 }
1952 }
1953
6615cc36
AD
1954 if ($_REQUEST['return']) {
1955 header("Location: " . $_REQUEST['return']);
1956 } else {
1957 header("Location: " . $_SERVER["REQUEST_URI"]);
1958 }
1959
d620cfe7
AD
1960 exit;
1961
01a87dff 1962 return;
7f0acba7 1963 } else {
af163b85 1964 $_SESSION["login_error_msg"] = __("Incorrect username or password");
01a87dff
AD
1965 }
1966 }
1967
7f0acba7 1968 if (!$_SESSION["uid"] || !validate_session($link)) {
12df6592
AD
1969 if (defined('ALLOW_REMOTE_USER_AUTH') && ALLOW_REMOTE_USER_AUTH
1970 && $_SERVER["REMOTE_USER"] && defined('AUTO_LOGIN') && AUTO_LOGIN) {
1971 authenticate_user($link,$_SERVER['REMOTE_USER'],null);
1972 $_SESSION["ref_schema_version"] = get_schema_version($link, true);
1973 } else {
1974 render_login_form($link, $mobile);
1975 //header("Location: login.php");
1976 exit;
1977 }
d3687e7a
AD
1978 } else {
1979 /* bump login timestamp */
8d505d78 1980 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
d3687e7a 1981 $_SESSION["uid"]);
019bd5a9 1982
d54780bc 1983 if ($_SESSION["language"] && SESSION_COOKIE_LIFETIME > 0) {
8d505d78 1984 setcookie("ttrss_lang", $_SESSION["language"],
019bd5a9
AD
1985 time() + SESSION_COOKIE_LIFETIME);
1986 }
b8aa49bc 1987 }
d620cfe7 1988
b8aa49bc 1989 } else {
0bbba72d 1990 return authenticate_user($link, "admin", null);
b8aa49bc
AD
1991 }
1992 }
3547842a 1993
411fe209 1994 function truncate_string($str, $max_len, $suffix = '&hellip;') {
12db369c 1995 if (mb_strlen($str, "utf-8") > $max_len - 3) {
411fe209 1996 return mb_substr($str, 0, $max_len, "utf-8") . $suffix;
3547842a
AD
1997 } else {
1998 return $str;
1999 }
2000 }
54a60e1a 2001
e9823609 2002 function theme_image($link, $filename) {
883fee8d
AD
2003 if ($link) {
2004 $theme_path = get_user_theme_path($link);
e9823609 2005
883fee8d
AD
2006 if ($theme_path && is_file($theme_path.$filename)) {
2007 return $theme_path.$filename;
2008 } else {
2009 return $filename;
2010 }
e9823609
AD
2011 } else {
2012 return $filename;
2013 }
2014 }
2015
dce46cad 2016 function get_user_theme($link) {
e4c51a6c 2017
b92fbcd8 2018 if (get_schema_version($link) >= 63 && $_SESSION["uid"]) {
b97e6e02
AD
2019 $theme_name = get_pref($link, "_THEME_ID");
2020 if (is_dir("themes/$theme_name")) {
2021 return $theme_name;
2022 } else {
2023 return '';
2024 }
e4c51a6c 2025 } else {
b97e6e02 2026 return '';
e4c51a6c 2027 }
d9084cf2 2028
dce46cad
AD
2029 }
2030
2031 function get_user_theme_path($link) {
c3fddd05 2032 $theme_path = '';
dce46cad 2033
b92fbcd8 2034 if (get_schema_version($link) >= 63 && $_SESSION["uid"]) {
dce46cad
AD
2035 $theme_name = get_pref($link, "_THEME_ID");
2036
b97e6e02 2037 if ($theme_name && is_dir("themes/$theme_name")) {
dce46cad 2038 $theme_path = "themes/$theme_name/";
6ba50622 2039 } else {
dce46cad 2040 $theme_name = '';
6ba50622 2041 }
54a60e1a 2042 } else {
dce46cad
AD
2043 $theme_path = '';
2044 }
2045
0c425dc7 2046 if ($theme_path) {
8d3cb8c0
AD
2047 if (is_file("$theme_path/theme.ini")) {
2048 $ini = parse_ini_file("$theme_path/theme.ini", true);
2049 if ($ini['theme']['version'] >= THEME_VERSION_REQUIRED) {
0c425dc7
AD
2050 return $theme_path;
2051 }
2052 }
2053 }
2054 return '';
dce46cad
AD
2055 }
2056
e71f2610
AD
2057 function get_user_theme_options($link) {
2058 $t = get_user_theme_path($link);
2059
2060 if ($t) {
2061 if (is_file("$t/theme.ini")) {
2062 $ini = parse_ini_file("$t/theme.ini", true);
2063 if ($ini['theme']['version']) {
2064 return $ini['theme']['options'];
2065 }
2066 }
2067 }
f1f3a642 2068 return '';
e71f2610
AD
2069 }
2070
8d3cb8c0
AD
2071 function print_theme_includes($link) {
2072
2073 $t = get_user_theme_path($link);
2074 $time = time();
2075
2076 if ($t) {
8d505d78 2077 print "<link rel=\"stylesheet\" type=\"text/css\"
8d3cb8c0
AD
2078 href=\"$t/theme.css?$time \">";
2079 if (file_exists("$t/theme.js")) {
2080 print "<script type=\"text/javascript\" src=\"$t/theme.js?$time\">
2081 </script>";
2082 }
2083 }
2084 }
e71f2610 2085
dce46cad
AD
2086 function get_all_themes() {
2087 $themes = glob("themes/*");
2088
b97e6e02
AD
2089 asort($themes);
2090
dce46cad
AD
2091 $rv = array();
2092
2093 foreach ($themes as $t) {
2094 if (is_file("$t/theme.ini")) {
2095 $ini = parse_ini_file("$t/theme.ini", true);
8d505d78 2096 if ($ini['theme']['version'] >= THEME_VERSION_REQUIRED &&
0c425dc7 2097 !$ini['theme']['disabled']) {
dce46cad
AD
2098 $entry = array();
2099 $entry["path"] = $t;
2100 $entry["base"] = basename($t);
2101 $entry["name"] = $ini['theme']['name'];
2102 $entry["version"] = $ini['theme']['version'];
2103 $entry["author"] = $ini['theme']['author'];
e71f2610 2104 $entry["options"] = $ini['theme']['options'];
dce46cad
AD
2105 array_push($rv, $entry);
2106 }
2107 }
54a60e1a 2108 }
dce46cad
AD
2109
2110 return $rv;
54a60e1a 2111 }
be773442 2112
ab4b768f
AD
2113 function convert_timestamp($timestamp, $source_tz, $dest_tz) {
2114
2115 try {
2116 $source_tz = new DateTimeZone($source_tz);
2117 } catch (Exception $e) {
2118 $source_tz = new DateTimeZone('UTC');
2119 }
2120
2121 try {
2122 $dest_tz = new DateTimeZone($dest_tz);
2123 } catch (Exception $e) {
2124 $dest_tz = new DateTimeZone('UTC');
2125 }
2126
2127 $dt = new DateTime(date('Y-m-d H:i:s', $timestamp), $source_tz);
2128 return $dt->format('U') + $dest_tz->getOffset($dt);
2129 }
2130
324944f3
AD
2131 function make_local_datetime($link, $timestamp, $long, $owner_uid = false,
2132 $no_smart_dt = false) {
2133
2134 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
2135 if (!$timestamp) $timestamp = '1970-01-01 0:00';
2136
2137 $user_tz_string = get_pref($link, 'USER_TIMEZONE', $owner_uid);
2138
2139 try {
2140 $user_tz = new DateTimeZone($user_tz_string);
2141 } catch (Exception $e) {
2142 $user_tz = new DateTimeZone('UTC');
2143 }
2144
2145 # We store date in UTC internally
2146 $dt = new DateTime($timestamp, new DateTimeZone('UTC'));
2147 $user_timestamp = $dt->format('U') + $user_tz->getOffset($dt);
2148
2149 if (!$no_smart_dt && get_pref($link, 'HEADLINES_SMART_DATE', $owner_uid)) {
8d505d78 2150 return smart_date_time($link, $user_timestamp,
2a5c136e 2151 $user_tz->getOffset($dt), $owner_uid);
324944f3
AD
2152 } else {
2153 if ($long)
2154 $format = get_pref($link, 'LONG_DATE_FORMAT', $owner_uid);
2155 else
2156 $format = get_pref($link, 'SHORT_DATE_FORMAT', $owner_uid);
2157
2158 return date($format, $user_timestamp);
2159 }
2160 }
2161
2a5c136e
AD
2162 function smart_date_time($link, $timestamp, $tz_offset = 0, $owner_uid = false) {
2163 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
2164
2165 if (date("Y.m.d", $timestamp) == date("Y.m.d", time() + $tz_offset)) {
be773442 2166 return date("G:i", $timestamp);
2a5c136e
AD
2167 } else if (date("Y", $timestamp) == date("Y", time() + $tz_offset)) {
2168 $format = get_pref($link, 'SHORT_DATE_FORMAT', $owner_uid);
2169 return date($format, $timestamp);
be773442 2170 } else {
2a5c136e
AD
2171 $format = get_pref($link, 'LONG_DATE_FORMAT', $owner_uid);
2172 return date($format, $timestamp);
be773442
AD
2173 }
2174 }
2175
2176 function smart_date($timestamp) {
2177 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
2178 return "Today";
f26450f1 2179 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
2180 return date("D m", $timestamp);
2181 } else {
b02111c2 2182 return date("Y/m/d", $timestamp);
be773442
AD
2183 }
2184 }
a654a595
AD
2185
2186 function sql_bool_to_string($s) {
2187 if ($s == "t" || $s == "1") {
2188 return "true";
2189 } else {
2190 return "false";
2191 }
2192 }
e3c99f3b
AD
2193
2194 function sql_bool_to_bool($s) {
2195 if ($s == "t" || $s == "1") {
2196 return true;
2197 } else {
2198 return false;
2199 }
2200 }
8d505d78 2201
badac687
AD
2202 function bool_to_sql_bool($s) {
2203 if ($s) {
2204 return "true";
2205 } else {
2206 return "false";
2207 }
2208 }
e3c99f3b 2209
0ea4fb50 2210 function toggleEvenOdd($a) {
8d505d78 2211 if ($a == "even")
0ea4fb50
AD
2212 return "odd";
2213 else
2214 return "even";
2215 }
6043fb7e 2216
199db684
AD
2217 function get_schema_version($link, $nocache = false) {
2218 if (!$_SESSION["schema_version"] || $nocache) {
2219 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
2220 $version = db_fetch_result($result, 0, "schema_version");
2221 $_SESSION["schema_version"] = $version;
2222 return $version;
2223 } else {
2224 return $_SESSION["schema_version"];
2225 }
e4c51a6c
AD
2226 }
2227
6043fb7e 2228 function sanity_check($link) {
9cbca41f 2229
6043fb7e 2230 $error_code = 0;
05044a59 2231 $schema_version = get_schema_version($link);
6043fb7e
AD
2232
2233 if ($schema_version != SCHEMA_VERSION) {
2234 $error_code = 5;
2235 }
2236
aec3ce39
AD
2237 if (DB_TYPE == "mysql") {
2238 $result = db_query($link, "SELECT true", false);
2239 if (db_num_rows($result) != 1) {
2240 $error_code = 10;
2241 }
2242 }
2243
f29ba148
AD
2244 if (db_escape_string("testTEST") != "testTEST") {
2245 $error_code = 12;
2246 }
2247
6043fb7e 2248 if ($error_code != 0) {
aec3ce39 2249 print_error_xml($error_code);
6043fb7e
AD
2250 return false;
2251 } else {
2252 return true;
4220d6b0 2253 }
6043fb7e
AD
2254 }
2255
27981ca3 2256 function file_is_locked($filename) {
31a6d42d 2257 if (function_exists('flock')) {
fb074239 2258 $fp = @fopen(LOCK_DIRECTORY . "/$filename", "r");
31a6d42d
AD
2259 if ($fp) {
2260 if (flock($fp, LOCK_EX | LOCK_NB)) {
2261 flock($fp, LOCK_UN);
2262 fclose($fp);
2263 return false;
2264 }
27981ca3 2265 fclose($fp);
31a6d42d 2266 return true;
e89aed7b
AD
2267 } else {
2268 return false;
27981ca3 2269 }
27981ca3 2270 }
c1fb4a5e 2271 return true; // consider the file always locked and skip the test
27981ca3
AD
2272 }
2273
fcb4c0c9 2274 function make_lockfile($filename) {
cfa43e02 2275 $fp = fopen(LOCK_DIRECTORY . "/$filename", "w");
fcb4c0c9 2276
82acc36d 2277 if (flock($fp, LOCK_EX | LOCK_NB)) {
4c59adb1
AD
2278 if (function_exists('posix_getpid')) {
2279 fwrite($fp, posix_getpid() . "\n");
2280 }
fcb4c0c9
AD
2281 return $fp;
2282 } else {
2283 return false;
2284 }
2285 }
2286
bf7fcde8 2287 function make_stampfile($filename) {
cfa43e02 2288 $fp = fopen(LOCK_DIRECTORY . "/$filename", "w");
bf7fcde8 2289
8e00ae9b 2290 if (flock($fp, LOCK_EX | LOCK_NB)) {
bf7fcde8 2291 fwrite($fp, time() . "\n");
8e00ae9b 2292 flock($fp, LOCK_UN);
bf7fcde8
AD
2293 fclose($fp);
2294 return true;
2295 } else {
2296 return false;
2297 }
2298 }
2299
894ebcf5
AD
2300 function sql_random_function() {
2301 if (DB_TYPE == "mysql") {
2302 return "RAND()";
2303 } else {
2304 return "RANDOM()";
2305 }
2306 }
2307
d36f5607 2308 function catchup_feed($link, $feed, $cat_view, $owner_uid = false) {
c7e51de1
AD
2309
2310 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
88040f57
AD
2311
2312 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
22fdebff 2313
23aa0d16
AD
2314 if ($cat_view) {
2315
72a2f4f5 2316 if ($feed >= 0) {
f9fca8cb
AD
2317
2318 if ($feed > 0) {
2319 $cat_qpart = "cat_id = '$feed'";
2320 } else {
2321 $cat_qpart = "cat_id IS NULL";
2322 }
8d505d78
AD
2323
2324 $tmp_result = db_query($link, "SELECT id
c7e51de1 2325 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = $owner_uid");
f9fca8cb
AD
2326
2327 while ($tmp_line = db_fetch_assoc($tmp_result)) {
23aa0d16 2328
f9fca8cb 2329 $tmp_feed = $tmp_line["id"];
23aa0d16 2330
8d505d78
AD
2331 db_query($link, "UPDATE ttrss_user_entries
2332 SET unread = false,last_read = NOW()
c7e51de1 2333 WHERE feed_id = '$tmp_feed' AND owner_uid = $owner_uid");
f9fca8cb
AD
2334 }
2335 } else if ($feed == -2) {
23aa0d16 2336
6f69764c 2337
8d505d78
AD
2338 db_query($link, "UPDATE ttrss_user_entries
2339 SET unread = false,last_read = NOW() WHERE (SELECT COUNT(*)
2340 FROM ttrss_user_labels2 WHERE article_id = ref_id) > 0
c7e51de1 2341 AND unread = true AND owner_uid = $owner_uid");
23aa0d16
AD
2342 }
2343
2344 } else if ($feed > 0) {
2345
8d505d78
AD
2346 db_query($link, "UPDATE ttrss_user_entries
2347 SET unread = false,last_read = NOW()
c7e51de1 2348 WHERE feed_id = '$feed' AND owner_uid = $owner_uid");
8d505d78 2349
23aa0d16
AD
2350 } else if ($feed < 0 && $feed > -10) { // special, like starred
2351
2352 if ($feed == -1) {
8d505d78 2353 db_query($link, "UPDATE ttrss_user_entries
23aa0d16 2354 SET unread = false,last_read = NOW()
c7e51de1 2355 WHERE marked = true AND owner_uid = $owner_uid");
23aa0d16 2356 }
e4f4b46f
AD
2357
2358 if ($feed == -2) {
8d505d78 2359 db_query($link, "UPDATE ttrss_user_entries
e4f4b46f 2360 SET unread = false,last_read = NOW()
c7e51de1 2361 WHERE published = true AND owner_uid = $owner_uid");
e4f4b46f
AD
2362 }
2363
2d24f032
AD
2364 if ($feed == -3) {
2365
c1d7e6c3
AD
2366 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
2367
2d24f032 2368 if (DB_TYPE == "pgsql") {
8d505d78 2369 $match_part = "updated > NOW() - INTERVAL '$intl hour' ";
2d24f032 2370 } else {
8d505d78 2371 $match_part = "updated > DATE_SUB(NOW(),
c1d7e6c3 2372 INTERVAL $intl HOUR) ";
2d24f032
AD
2373 }
2374
8d505d78 2375 $result = db_query($link, "SELECT id FROM ttrss_entries,
1f3335dc
AD
2376 ttrss_user_entries WHERE $match_part AND
2377 unread = true AND
8d505d78 2378 ttrss_user_entries.ref_id = ttrss_entries.id AND
c7e51de1 2379 owner_uid = $owner_uid");
1f3335dc
AD
2380
2381 $affected_ids = array();
2382
2383 while ($line = db_fetch_assoc($result)) {
2384 array_push($affected_ids, $line["id"]);
2385 }
2386
2387 catchupArticlesById($link, $affected_ids, 0);
2d24f032
AD
2388 }
2389
3584cb11 2390 if ($feed == -4) {
8d505d78 2391 db_query($link, "UPDATE ttrss_user_entries
3584cb11 2392 SET unread = false,last_read = NOW()
c7e51de1 2393 WHERE owner_uid = $owner_uid");
3584cb11
AD
2394 }
2395
23aa0d16
AD
2396 } else if ($feed < -10) { // label
2397
23aa0d16
AD
2398 $label_id = -$feed - 11;
2399
8d505d78
AD
2400 db_query($link, "UPDATE ttrss_user_entries, ttrss_user_labels2
2401 SET unread = false, last_read = NOW()
338c238d 2402 WHERE label_id = '$label_id' AND unread = true
c7e51de1 2403 AND owner_uid = '$owner_uid' AND ref_id = article_id");
23aa0d16 2404
23aa0d16 2405 }
ad0056a8 2406
c7e51de1 2407 ccache_update($link, $feed, $owner_uid, $cat_view);
ad0056a8 2408
23aa0d16
AD
2409 } else { // tag
2410 db_query($link, "BEGIN");
2411
2412 $tag_name = db_escape_string($feed);
2413
2414 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
c7e51de1 2415 WHERE tag_name = '$tag_name' AND owner_uid = $owner_uid");
23aa0d16
AD
2416
2417 while ($line = db_fetch_assoc($result)) {
2418 db_query($link, "UPDATE ttrss_user_entries SET
8d505d78 2419 unread = false, last_read = NOW()
23aa0d16
AD
2420 WHERE int_id = " . $line["post_int_id"]);
2421 }
2422 db_query($link, "COMMIT");
2423 }
2424 }
2425
4ffa126e 2426 function getAllCounters($link, $omode = "flc", $active_feed = false) {
cf4d339c 2427
e33fe293 2428 if (!$omode) $omode = "flc";
0a6e5382 2429
6a7817c1 2430 $data = getGlobalCounters($link);
8d505d78 2431
6a7817c1
AD
2432 $data = array_merge($data, getVirtCounters($link));
2433
2434 if (strchr($omode, "l")) $data = array_merge($data, getLabelCounters($link));
2435 if (strchr($omode, "f")) $data = array_merge($data, getFeedCounters($link, $active_feed));
2436 if (strchr($omode, "t")) $data = array_merge($data, getTagCounters($link));
8d505d78 2437 if (strchr($omode, "c")) {
e33fe293 2438 if (get_pref($link, 'ENABLE_FEED_CATS')) {
6a7817c1 2439 $data = array_merge($data, getCategoryCounters($link));
cf4d339c 2440 }
e33fe293 2441 }
6a7817c1
AD
2442
2443 return $data;
8d505d78 2444 }
a9cb1f83
AD
2445
2446 function getCategoryCounters($link) {
6a7817c1 2447 $ret_arr = array();
bba7c4bf 2448
6a7817c1 2449 /* Labels category */
bba7c4bf 2450
8acc449c 2451 $cv = array("id" => -2, "kind" => "cat",
6a7817c1 2452 "counter" => getCategoryUnread($link, -2));
bba7c4bf 2453
6a7817c1 2454 array_push($ret_arr, $cv);
bba7c4bf 2455
14073c0a
AD
2456 $age_qpart = getMaxAgeSubquery();
2457
8d505d78
AD
2458 $result = db_query($link, "SELECT id AS cat_id, value AS unread
2459 FROM ttrss_feed_categories, ttrss_cat_counters_cache
2460 WHERE ttrss_cat_counters_cache.feed_id = id AND
31375163 2461 ttrss_feed_categories.owner_uid = " . $_SESSION["uid"]);
a9cb1f83
AD
2462
2463 while ($line = db_fetch_assoc($result)) {
22fdebff 2464 $line["cat_id"] = (int) $line["cat_id"];
8a4c759e 2465
8acc449c 2466 $cv = array("id" => $line["cat_id"], "kind" => "cat",
6a7817c1
AD
2467 "counter" => $line["unread"]);
2468
2469 array_push($ret_arr, $cv);
a9cb1f83 2470 }
d232a40f
AD
2471
2472 /* Special case: NULL category doesn't actually exist in the DB */
2473
9798b2b4 2474 $cv = array("id" => 0, "kind" => "cat",
6a7817c1 2475 "counter" => ccache_find($link, 0, $_SESSION["uid"], true));
d232a40f 2476
6a7817c1
AD
2477 array_push($ret_arr, $cv);
2478
2479 return $ret_arr;
a9cb1f83
AD
2480 }
2481
b6d486a3
AD
2482 function getCategoryUnread($link, $cat, $owner_uid = false) {
2483
2484 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
f295c368 2485
bba7c4bf 2486 if ($cat >= 0) {
18664970 2487
bba7c4bf
AD
2488 if ($cat != 0) {
2489 $cat_query = "cat_id = '$cat'";
2490 } else {
2491 $cat_query = "cat_id IS NULL";
2492 }
14073c0a
AD
2493
2494 $age_qpart = getMaxAgeSubquery();
2495
8d505d78 2496 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
b6d486a3 2497 AND owner_uid = " . $owner_uid);
8d505d78 2498
bba7c4bf
AD
2499 $cat_feeds = array();
2500 while ($line = db_fetch_assoc($result)) {
2501 array_push($cat_feeds, "feed_id = " . $line["id"]);
2502 }
8d505d78 2503
bba7c4bf 2504 if (count($cat_feeds) == 0) return 0;
8d505d78 2505
bba7c4bf 2506 $match_part = implode(" OR ", $cat_feeds);
8d505d78
AD
2507
2508 $result = db_query($link, "SELECT COUNT(int_id) AS unread
2509 FROM ttrss_user_entries,ttrss_entries
2510 WHERE unread = true AND ($match_part) AND id = ref_id
b6d486a3 2511 AND $age_qpart AND owner_uid = " . $owner_uid);
8d505d78 2512
bba7c4bf 2513 $unread = 0;
8d505d78 2514
bba7c4bf
AD
2515 # this needs to be rewritten
2516 while ($line = db_fetch_assoc($result)) {
2517 $unread += $line["unread"];
2518 }
8d505d78 2519
bba7c4bf
AD
2520 return $unread;
2521 } else if ($cat == -1) {
59e15af4 2522 return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3) + getFeedUnread($link, 0);
bba7c4bf 2523 } else if ($cat == -2) {
f295c368 2524
b2531a28 2525 $result = db_query($link, "
8d505d78
AD
2526 SELECT COUNT(unread) AS unread FROM
2527 ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds
2528 WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND
b2531a28 2529 ttrss_labels2.owner_uid = '$owner_uid'
117335bf 2530 AND unread = true AND feed_id = ttrss_feeds.id
b2531a28 2531 AND ttrss_user_entries.owner_uid = '$owner_uid'");
ceb30ba4 2532
b2531a28 2533 $unread = db_fetch_result($result, 0, "unread");
f295c368 2534
b2531a28 2535 return $unread;
f295c368 2536
8d505d78 2537 }
f295c368
AD
2538 }
2539
14073c0a
AD
2540 function getMaxAgeSubquery($days = COUNTERS_MAX_AGE) {
2541 if (DB_TYPE == "pgsql") {
8d505d78 2542 return "ttrss_entries.date_updated >
14073c0a
AD
2543 NOW() - INTERVAL '$days days'";
2544 } else {
8d505d78 2545 return "ttrss_entries.date_updated >
14073c0a
AD
2546 DATE_SUB(NOW(), INTERVAL $days DAY)";
2547 }
2548 }
2549
f295c368 2550 function getFeedUnread($link, $feed, $is_cat = false) {
2627f2d0 2551 return getFeedArticles($link, $feed, $is_cat, true, $_SESSION["uid"]);
bdb7369b
AD
2552 }
2553
ceb30ba4
AD
2554 function getLabelUnread($link, $label_id, $owner_uid = false) {
2555 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
2556
2557 $result = db_query($link, "
8d505d78
AD
2558 SELECT COUNT(unread) AS unread FROM
2559 ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds
2560 WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND
b0f24af1 2561 ttrss_labels2.owner_uid = '$owner_uid' AND ttrss_labels2.id = '$label_id'
117335bf 2562 AND unread = true AND feed_id = ttrss_feeds.id
933ba4ee 2563 AND ttrss_user_entries.owner_uid = '$owner_uid'");
ceb30ba4
AD
2564
2565 if (db_num_rows($result) != 0) {
2566 return db_fetch_result($result, 0, "unread");
2567 } else {
2568 return 0;
2569 }
2570 }
2571
2627f2d0
AD
2572 function getFeedArticles($link, $feed, $is_cat = false, $unread_only = false,
2573 $owner_uid = false) {
2574
22fdebff 2575 $n_feed = (int) $feed;
f295c368 2576
2627f2d0
AD
2577 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
2578
bdb7369b
AD
2579 if ($unread_only) {
2580 $unread_qpart = "unread = true";
2581 } else {
2582 $unread_qpart = "true";
2583 }
2584
14073c0a
AD
2585 $age_qpart = getMaxAgeSubquery();
2586
f295c368 2587 if ($is_cat) {
8d505d78 2588 return getCategoryUnread($link, $n_feed, $owner_uid);
326469fc
AD
2589 } if ($feed != "0" && $n_feed == 0) {
2590
c5701e70
AD
2591 $feed = db_escape_string($feed);
2592
326469fc 2593 $result = db_query($link, "SELECT SUM((SELECT COUNT(int_id)
8d505d78 2594 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
326469fc 2595 AND ref_id = id AND $age_qpart
8d505d78 2596 AND $unread_qpart)) AS count FROM ttrss_tags
326469fc
AD
2597 WHERE owner_uid = $owner_uid AND tag_name = '$feed'");
2598 return db_fetch_result($result, 0, "count");
2599
f295c368 2600 } else if ($n_feed == -1) {
a9cb1f83 2601 $match_part = "marked = true";
e4f4b46f
AD
2602 } else if ($n_feed == -2) {
2603 $match_part = "published = true";
2d24f032 2604 } else if ($n_feed == -3) {
cd2cc43d 2605 $match_part = "unread = true AND score >= 0";
2d24f032 2606
b71e188e 2607 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
c1d7e6c3 2608
2d24f032 2609 if (DB_TYPE == "pgsql") {
8d505d78 2610 $match_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
2d24f032 2611 } else {
7608b38a 2612 $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2d24f032 2613 }
b2531a28
AD
2614 } else if ($n_feed == -4) {
2615 $match_part = "true";
e04c18a2 2616 } else if ($n_feed >= 0) {
831ff047 2617
6e63a7c3
AD
2618 if ($n_feed != 0) {
2619 $match_part = "feed_id = '$n_feed'";
831ff047 2620 } else {
6e63a7c3 2621 $match_part = "feed_id IS NULL";
831ff047 2622 }
6e63a7c3 2623
a9cb1f83 2624 } else if ($feed < -10) {
318260cc 2625
a9cb1f83
AD
2626 $label_id = -$feed - 11;
2627
ceb30ba4 2628 return getLabelUnread($link, $label_id, $owner_uid);
a9cb1f83 2629
a9cb1f83
AD
2630 }
2631
2632 if ($match_part) {
e04c18a2
AD
2633
2634 if ($n_feed != 0) {
2635 $from_qpart = "ttrss_user_entries,ttrss_feeds,ttrss_entries";
117335bf 2636 $feeds_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
e04c18a2
AD
2637 } else {
2638 $from_qpart = "ttrss_user_entries,ttrss_entries";
c3fddd05 2639 $feeds_qpart = '';
e04c18a2
AD
2640 }
2641
8d505d78 2642 $query = "SELECT count(int_id) AS unread
e04c18a2 2643 FROM $from_qpart WHERE
8d505d78 2644 ttrss_user_entries.ref_id = ttrss_entries.id AND
14073c0a 2645 $age_qpart AND
dbfc4365
AD
2646 $feeds_qpart
2647 $unread_qpart AND ($match_part) AND ttrss_user_entries.owner_uid = $owner_uid";
2648
2649 $result = db_query($link, $query);
8d505d78 2650
a9cb1f83 2651 } else {
8d505d78 2652
a9cb1f83 2653 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
8d505d78
AD
2654 FROM ttrss_tags,ttrss_user_entries,ttrss_entries
2655 WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id
bdb7369b 2656 AND $unread_qpart AND $age_qpart AND
2627f2d0 2657 ttrss_tags.owner_uid = " . $owner_uid);
a9cb1f83 2658 }
8d505d78 2659
a9cb1f83 2660 $unread = db_fetch_result($result, 0, "unread");
cfb02131 2661
a9cb1f83
AD
2662 return $unread;
2663 }
2664
f3acc32e
AD
2665 function getGlobalUnread($link, $user_id = false) {
2666
2667 if (!$user_id) {
2668 $user_id = $_SESSION["uid"];
2669 }
2670
8a4c759e
AD
2671 $result = db_query($link, "SELECT SUM(value) AS c_id FROM ttrss_counters_cache
2672 WHERE owner_uid = '$user_id' AND feed_id > 0");
2673
8d505d78 2674 $c_id = db_fetch_result($result, 0, "c_id");
8a4c759e 2675
a9cb1f83
AD
2676 return $c_id;
2677 }
2678
2679 function getGlobalCounters($link, $global_unread = -1) {
6a7817c1
AD
2680 $ret_arr = array();
2681
8d505d78 2682 if ($global_unread == -1) {
a9cb1f83
AD
2683 $global_unread = getGlobalUnread($link);
2684 }
6a7817c1 2685
8d505d78 2686 $cv = array("id" => "global-unread",
6a7817c1
AD
2687 "counter" => $global_unread);
2688
2689 array_push($ret_arr, $cv);
7bf7e4d3 2690
8d505d78 2691 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
7bf7e4d3
AD
2692 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
2693
2694 $subscribed_feeds = db_fetch_result($result, 0, "fn");
2695
8d505d78 2696 $cv = array("id" => "subscribed-feeds",
6a7817c1 2697 "counter" => $subscribed_feeds);
7bf7e4d3 2698
6a7817c1
AD
2699 array_push($ret_arr, $cv);
2700
2701 return $ret_arr;
a9cb1f83
AD
2702 }
2703
95004daf 2704 function getSubscribedFeeds($link) {
8d505d78 2705 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
95004daf
AD
2706 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
2707
2708 return db_fetch_result($result, 0, "fn");
2709 }
2710
3809b278 2711 function getTagCounters($link) {
8d505d78 2712
6a7817c1 2713 $ret_arr = array();
a9cb1f83 2714
14073c0a
AD
2715 $age_qpart = getMaxAgeSubquery();
2716
8d505d78
AD
2717 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
2718 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
14073c0a 2719 AND ref_id = id AND $age_qpart
8d505d78
AD
2720 AND unread = true)) AS count FROM ttrss_tags
2721 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
ef1ac7c7 2722 ORDER BY count DESC LIMIT 55");
8d505d78 2723
a9cb1f83
AD
2724 $tags = array();
2725
2726 while ($line = db_fetch_assoc($result)) {
2727 $tags[$line["tag_name"]] += $line["count"];
2728 }
2729
2730 foreach (array_keys($tags) as $tag) {
8d505d78 2731 $unread = $tags[$tag];
a9cb1f83 2732 $tag = htmlspecialchars($tag);
6a7817c1
AD
2733
2734 $cv = array("id" => $tag,
8acc449c 2735 "kind" => "tag",
6a7817c1
AD
2736 "counter" => $unread);
2737
2738 array_push($ret_arr, $cv);
2739 }
2740
8d505d78 2741 return $ret_arr;
a9cb1f83
AD
2742 }
2743
6a7817c1 2744 function getVirtCounters($link) {
a9cb1f83 2745
ef393de7 2746 $ret_arr = array();
bdb7369b 2747
e04c18a2 2748 for ($i = 0; $i >= -4; $i--) {
bdb7369b 2749
ceb30ba4 2750 $count = getFeedUnread($link, $i);
6a7817c1
AD
2751
2752 $cv = array("id" => $i,
2abc7af0 2753 "counter" => $count);
8d505d78 2754
296c8134
AD
2755// if (get_pref($link, 'EXTENDED_FEEDLIST'))
2756// $cv["xmsg"] = getFeedArticles($link, $i)." ".__("total");
bdb7369b 2757
6a7817c1 2758 array_push($ret_arr, $cv);
8d505d78 2759 }
0a6e5382
AD
2760
2761 return $ret_arr;
2762 }
2763
11232703 2764 function getLabelCounters($link, $descriptions = false) {
6a7817c1
AD
2765
2766 $ret_arr = array();
0a6e5382
AD
2767
2768 $age_qpart = getMaxAgeSubquery();
2769
3809b278 2770 $owner_uid = $_SESSION["uid"];
bdb7369b 2771
3809b278
AD
2772 $result = db_query($link, "SELECT id, caption FROM ttrss_labels2
2773 WHERE owner_uid = '$owner_uid'");
8d505d78 2774
3809b278 2775 while ($line = db_fetch_assoc($result)) {
2d24f032 2776
3809b278 2777 $id = -$line["id"] - 11;
e4f4b46f 2778
3809b278
AD
2779 $label_name = $line["caption"];
2780 $count = getFeedUnread($link, $id);
3809b278 2781
6a7817c1 2782 $cv = array("id" => $id,
11232703
AD
2783 "counter" => $count);
2784
2785 if ($descriptions)
2786 $cv["description"] = $label_name;
a9cb1f83 2787
296c8134
AD
2788// if (get_pref($link, 'EXTENDED_FEEDLIST'))
2789// $cv["xmsg"] = getFeedArticles($link, $id)." ".__("total");
ef393de7 2790
6a7817c1 2791 array_push($ret_arr, $cv);
3809b278 2792 }
8d505d78 2793
ef393de7 2794 return $ret_arr;
a9cb1f83
AD
2795 }
2796
3809b278 2797 function getFeedCounters($link, $active_feed = false) {
a9cb1f83 2798
6a7817c1
AD
2799 $ret_arr = array();
2800
14073c0a
AD
2801 $age_qpart = getMaxAgeSubquery();
2802
8a4c759e
AD
2803 $query = "SELECT ttrss_feeds.id,
2804 ttrss_feeds.title,
8d505d78 2805 ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated,
de0a2122
AD
2806 last_error, value AS count
2807 FROM ttrss_feeds, ttrss_counters_cache
8d505d78 2808 WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
55e01d7e 2809 AND ttrss_counters_cache.feed_id = id";
a9cb1f83 2810
14073c0a 2811 $result = db_query($link, $query);
a9cb1f83
AD
2812 $fctrs_modified = false;
2813
2814 while ($line = db_fetch_assoc($result)) {
8d505d78 2815
a9cb1f83 2816 $id = $line["id"];
de0a2122 2817 $count = $line["count"];
a9cb1f83 2818 $last_error = htmlspecialchars($line["last_error"]);
fb1fb4ab 2819
324944f3 2820 $last_updated = make_local_datetime($link, $line['last_updated'], false);
fb1fb4ab 2821
7defa089 2822 $has_img = feed_has_icon($id);
a9cb1f83 2823
428b704d
AD
2824 if (date('Y') - date('Y', strtotime($line['last_updated'])) > 2)
2825 $last_updated = '';
2826
6a7817c1 2827 $cv = array("id" => $id,
21884958 2828 "updated" => $last_updated,
6a7817c1
AD
2829 "counter" => $count,
2830 "has_img" => (int) $has_img);
a9cb1f83 2831
6a7817c1
AD
2832 if ($last_error)
2833 $cv["error"] = $last_error;
4ffa126e 2834
296c8134
AD
2835// if (get_pref($link, 'EXTENDED_FEEDLIST'))
2836// $cv["xmsg"] = getFeedArticles($link, $id)." ".__("total");
bdb7369b 2837
6a7817c1 2838 if ($active_feed && $id == $active_feed)
fbc95c5b 2839 $cv["title"] = truncate_string($line["title"], 30);
6a7817c1
AD
2840
2841 array_push($ret_arr, $cv);
a9cb1f83 2842
a9cb1f83 2843 }
6a7817c1
AD
2844
2845 return $ret_arr;
a9cb1f83
AD
2846 }
2847
6e7f8d26
AD
2848 function get_pgsql_version($link) {
2849 $result = db_query($link, "SELECT version() AS version");
2850 $version = split(" ", db_fetch_result($result, 0, "version"));
2851 return $version[1];
2852 }
2853
af106b0e
AD
2854 function print_error_xml($code, $add_msg = "") {
2855 global $ERRORS;
2856
2857 $error_msg = $ERRORS[$code];
8d505d78 2858
af106b0e
AD
2859 if ($add_msg) {
2860 $error_msg = "$error_msg; $add_msg";
2861 }
8d505d78 2862
4c2abbc1 2863 print "<rpc-reply>";
af106b0e 2864 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
4c2abbc1 2865 print "</rpc-reply>";
af106b0e 2866 }
956c7629 2867
2b8290cd
CW
2868 /**
2869 * Subscribes the user to the given feed
2870 *
2871 * @param resource $link Database connection
2872 * @param string $url Feed URL to subscribe to
2873 * @param integer $cat_id Category ID the feed shall be added to
2874 * @param string $auth_login (optional) Feed username
2875 * @param string $auth_pass (optional) Feed password
2876 *
2877 * @return integer Status code:
2878 * 0 - OK, Feed already exists
2879 * 1 - OK, Feed added
2880 * 2 - Invalid URL
9a8ce956
CW
2881 * 3 - URL content is HTML, no feeds available
2882 * 4 - URL content is HTML which contains multiple feeds.
2883 * Here you should call extractfeedurls in rpc-backend
2884 * to get all possible feeds.
5414ad4c 2885 * 5 - Couldn't download the URL content.
2b8290cd 2886 */
8d505d78 2887 function subscribe_to_feed($link, $url, $cat_id = 0,
f27de515 2888 $auth_login = '', $auth_pass = '') {
bb0f29a4 2889
f0266f51 2890 $url = fix_url($url);
ec39a02c
AD
2891
2892 if (!$url || !validate_feed_url($url)) return 2;
a5819bb3 2893
b3009bcd
AD
2894 $update_method = 0;
2895
8d505d78 2896 $result = db_query($link, "SELECT twitter_oauth FROM ttrss_users
aeaa6991
AD
2897 WHERE id = ".$_SESSION['uid']);
2898
2899 $has_oauth = db_fetch_result($result, 0, 'twitter_oauth');
2900
54a3dd8d 2901 if (!$has_oauth || strpos($url, '://api.twitter.com') === false) {
8d505d78 2902 if (!fetch_file_contents($url, false, $auth_login, $auth_pass)) return 5;
57e24c82 2903
8d505d78
AD
2904 if (url_is_html($url, $auth_login, $auth_pass)) {
2905 $feedUrls = get_feeds_from_html($url, $auth_login, $auth_pass);
57e24c82
AD
2906 if (count($feedUrls) == 0) {
2907 return 3;
2908 } else if (count($feedUrls) > 1) {
2909 return 4;
2910 }
2911 //use feed url as new URL
2912 $url = key($feedUrls);
f6d8345b 2913 }
f6d8345b 2914
57e24c82 2915 } else {
8d505d78 2916 if (!fetch_twitter_rss($link, $url, $_SESSION['uid']))
57e24c82 2917 return 5;
b3009bcd
AD
2918
2919 $update_method = 3;
57e24c82 2920 }
956c7629
AD
2921 if ($cat_id == "0" || !$cat_id) {
2922 $cat_qpart = "NULL";
2923 } else {
2924 $cat_qpart = "'$cat_id'";
2925 }
8d505d78 2926
956c7629 2927 $result = db_query($link,
8d505d78 2928 "SELECT id FROM ttrss_feeds
a5819bb3 2929 WHERE feed_url = '$url' AND owner_uid = ".$_SESSION["uid"]);
8d505d78 2930
956c7629 2931 if (db_num_rows($result) == 0) {
956c7629 2932 $result = db_query($link,
8d505d78
AD
2933 "INSERT INTO ttrss_feeds
2934 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass,update_method)
2935 VALUES ('".$_SESSION["uid"]."', '$url',
b3009bcd 2936 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass', '$update_method')");
8d505d78 2937
956c7629 2938 $result = db_query($link,
8d505d78 2939 "SELECT id FROM ttrss_feeds WHERE feed_url = '$url'
f27de515 2940 AND owner_uid = " . $_SESSION["uid"]);
8d505d78 2941
956c7629 2942 $feed_id = db_fetch_result($result, 0, "id");
8d505d78 2943
956c7629 2944 if ($feed_id) {
c633e370 2945 update_rss_feed($link, $feed_id, true);
956c7629
AD
2946 }
2947
a5819bb3 2948 return 1;
956c7629 2949 } else {
a5819bb3 2950 return 0;
956c7629
AD
2951 }
2952 }
2953
8d505d78 2954 function print_feed_select($link, $id, $default_id = "",
673d54ca
AD
2955 $attributes = "", $include_all_feeds = true) {
2956
79f3553b 2957 print "<select id=\"$id\" name=\"$id\" $attributes>";
8d505d78 2958 if ($include_all_feeds) {
89cb787e 2959 print "<option value=\"0\">".__('All feeds')."</option>";
673d54ca 2960 }
8d505d78 2961
673d54ca
AD
2962 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2963 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2964
2965 if (db_num_rows($result) > 0 && $include_all_feeds) {
2966 print "<option disabled>--------</option>";
2967 }
2968
2969 while ($line = db_fetch_assoc($result)) {
2970 if ($line["id"] == $default_id) {
10249c41 2971 $is_selected = "selected=\"1\"";
673d54ca
AD
2972 } else {
2973 $is_selected = "";
2974 }
b1710666
AD
2975
2976 $title = truncate_string(htmlspecialchars($line["title"]), 40);
2977
8d505d78 2978 printf("<option $is_selected value='%d'>%s</option>",
b1710666 2979 $line["id"], $title);
673d54ca 2980 }
8d505d78 2981
673d54ca
AD
2982 print "</select>";
2983 }
2984
8d505d78 2985 function print_feed_cat_select($link, $id, $default_id = "",
673d54ca 2986 $attributes = "", $include_all_cats = true) {
8d505d78 2987
5c7c7da9 2988 print "<select id=\"$id\" name=\"$id\" default=\"$default_id\" onchange=\"catSelectOnChange(this)\" $attributes>";
673d54ca
AD
2989
2990 if ($include_all_cats) {
d1db26aa 2991 print "<option value=\"0\">".__('Uncategorized')."</option>";
673d54ca
AD
2992 }
2993
2994 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
2995 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2996
2997 if (db_num_rows($result) > 0 && $include_all_cats) {
c00907f2 2998 print "<option disabled=\"1\">--------</option>";
673d54ca
AD
2999 }
3000
3001 while ($line = db_fetch_assoc($result)) {
3002 if ($line["id"] == $default_id) {
10249c41 3003 $is_selected = "selected=\"1\"";
673d54ca 3004 } else {
8d505d78 3005 $is_selected = "";
673d54ca 3006 }
c00907f2
AD
3007
3008 if ($line["title"])
8d505d78 3009 printf("<option $is_selected value='%d'>%s</option>",
c00907f2 3010 $line["id"], htmlspecialchars($line["title"]));
673d54ca
AD
3011 }
3012
f9c388f5 3013# print "<option value=\"ADD_CAT\">" .__("Add category...") . "</option>";
5c7c7da9 3014
673d54ca
AD
3015 print "</select>";
3016 }
8d505d78 3017
14f69488
AD
3018 function checkbox_to_sql_bool($val) {
3019 return ($val == "on") ? "true" : "false";
3020 }
86b682ce
AD
3021
3022 function getFeedCatTitle($link, $id) {
3023 if ($id == -1) {
d1db26aa 3024 return __("Special");
86b682ce 3025 } else if ($id < -10) {
d1db26aa 3026 return __("Labels");
86b682ce 3027 } else if ($id > 0) {
8d505d78 3028 $result = db_query($link, "SELECT ttrss_feed_categories.title
86b682ce
AD
3029 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
3030 cat_id = ttrss_feed_categories.id");
3031 if (db_num_rows($result) == 1) {
3032 return db_fetch_result($result, 0, "title");
3033 } else {
d1db26aa 3034 return __("Uncategorized");
86b682ce
AD
3035 }
3036 } else {
3037 return "getFeedCatTitle($id) failed";
3038 }
3039
3040 }
3041
af88c48a
AD
3042 function getFeedIcon($id) {
3043 switch ($id) {
4bee8b5f
AD
3044 case 0:
3045 return "images/archive.png";
3046 break;
af88c48a 3047 case -1:
f65ffc2d 3048 return "images/mark_set.png";
af88c48a
AD
3049 break;
3050 case -2:
b97e6e02 3051 return "images/pub_set.png";
af88c48a
AD
3052 break;
3053 case -3:
3054 return "images/fresh.png";
3055 break;
3056 case -4:
3057 return "images/tag.png";
3058 break;
3059 default:
4bee8b5f
AD
3060 if ($id < -10) {
3061 return "images/label.png";
3062 } else {
8d505d78 3063 if (file_exists(ICONS_DIR . "/$id.ico"))
e2eda979 3064 return ICONS_URL . "/$id.ico";
4bee8b5f 3065 }
af88c48a
AD
3066 break;
3067 }
3068 }
3069
86b682ce
AD
3070 function getFeedTitle($link, $id) {
3071 if ($id == -1) {
d1db26aa 3072 return __("Starred articles");
945c243e
AD
3073 } else if ($id == -2) {
3074 return __("Published articles");
2d24f032
AD
3075 } else if ($id == -3) {
3076 return __("Fresh articles");
b2531a28
AD
3077 } else if ($id == -4) {
3078 return __("All articles");
80db1113 3079 } else if ($id === 0 || $id === "0") {
e04c18a2 3080 return __("Archived articles");
86b682ce 3081 } else if ($id < -10) {
76626c72 3082 $label_id = -$id - 11;
ceb30ba4 3083 $result = db_query($link, "SELECT caption FROM ttrss_labels2 WHERE id = '$label_id'");
86b682ce 3084 if (db_num_rows($result) == 1) {
ceb30ba4 3085 return db_fetch_result($result, 0, "caption");
86b682ce
AD
3086 } else {
3087 return "Unknown label ($label_id)";
3088 }
3089
3090 } else if ($id > 0) {
3091 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
3092 if (db_num_rows($result) == 1) {
3093 return db_fetch_result($result, 0, "title");
3094 } else {
3095 return "Unknown feed ($id)";
3096 }
3097 } else {
22fdebff 3098 return $id;
86b682ce 3099 }
86b682ce 3100 }
3dd46f19
AD
3101
3102 function get_session_cookie_name() {
3103 return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME);
3104 }
3ac2b520 3105
d8221301 3106 function make_init_params($link) {
f1f3a642 3107 $params = array();
c9268ed5 3108
c4f7ba80
AD
3109 $params["theme"] = get_user_theme($link);
3110 $params["theme_options"] = get_user_theme_options($link);
3111 $params["daemon_enabled"] = ENABLE_UPDATE_DAEMON;
f6d6e22f 3112
c4f7ba80
AD
3113 $params["sign_progress"] = theme_image($link, "images/indicator_white.gif");
3114 $params["sign_progress_tiny"] = theme_image($link, "images/indicator_tiny.gif");
3115 $params["sign_excl"] = theme_image($link, "images/sign_excl.png");
3116 $params["sign_info"] = theme_image($link, "images/sign_info.png");
be0801a1 3117
f1f3a642
AD
3118 foreach (array("ON_CATCHUP_SHOW_NEXT_FEED", "HIDE_READ_FEEDS",
3119 "ENABLE_FEED_CATS", "FEEDS_SORT_BY_UNREAD", "CONFIRM_FEED_CATCHUP",
7d12b6c8 3120 "CDM_AUTO_CATCHUP", "FRESH_ARTICLE_MAX_AGE", "DEFAULT_ARTICLE_LIMIT",
30b6ee8c 3121 "HIDE_READ_SHOWS_SPECIAL", "COMBINED_DISPLAY_MODE") as $param) {
40496720 3122
c4f7ba80 3123 $params[strtolower($param)] = (int) get_pref($link, $param);
f1f3a642 3124 }
40496720 3125
c4f7ba80
AD
3126 $params["icons_url"] = ICONS_URL;
3127 $params["cookie_lifetime"] = SESSION_COOKIE_LIFETIME;
3128 $params["default_view_mode"] = get_pref($link, "_DEFAULT_VIEW_MODE");
3129 $params["default_view_limit"] = (int) get_pref($link, "_DEFAULT_VIEW_LIMIT");
3130 $params["default_view_order_by"] = get_pref($link, "_DEFAULT_VIEW_ORDER_BY");
c4f7ba80 3131 $params["bw_limit"] = (int) $_SESSION["bw_limit"];
59b223d7 3132
8cd576a1 3133 $result = db_query($link, "SELECT MAX(id) AS mid, COUNT(*) AS nf FROM
9b7ecc0a
AD
3134 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
3135
8cd576a1
AD
3136 $max_feed_id = db_fetch_result($result, 0, "mid");
3137 $num_feeds = db_fetch_result($result, 0, "nf");
9b7ecc0a 3138
8cd576a1 3139 $params["max_feed_id"] = (int) $max_feed_id;
c4f7ba80 3140 $params["num_feeds"] = (int) $num_feeds;
8cd576a1 3141
c4f7ba80 3142 $params["collapsed_feedlist"] = (int) get_pref($link, "_COLLAPSED_FEEDLIST");
9b7ecc0a 3143
d8221301 3144 return $params;
3ac2b520 3145 }
f54f515f
AD
3146
3147 function print_runtime_info($link) {
c4f7ba80
AD
3148 print "<runtime-info><![CDATA[";
3149 print json_encode(make_runtime_info($link));
3150 print "]]></runtime-info>";
3151 }
20361063 3152
c4f7ba80 3153 function make_runtime_info($link) {
8cd576a1
AD
3154 $data = array();
3155
3156 $result = db_query($link, "SELECT MAX(id) AS mid, COUNT(*) AS nf FROM
9b7ecc0a
AD
3157 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
3158
8cd576a1
AD
3159 $max_feed_id = db_fetch_result($result, 0, "mid");
3160 $num_feeds = db_fetch_result($result, 0, "nf");
9b7ecc0a 3161
8cd576a1
AD
3162 $data["max_feed_id"] = (int) $max_feed_id;
3163 $data["num_feeds"] = (int) $num_feeds;
c4f7ba80 3164
f8fb4498 3165 $data['last_article_id'] = getLastArticleId($link);
5ae8f858 3166 $data['cdm_expanded'] = get_pref($link, 'CDM_EXPANDED');
f8fb4498 3167
71ad883b 3168 if (ENABLE_UPDATE_DAEMON) {
c4f7ba80
AD
3169
3170 $data['daemon_is_running'] = (int) file_is_locked("update_daemon.lock");
8e00ae9b 3171
9041f58b 3172 if (time() - $_SESSION["daemon_stamp_check"] > 30) {
8e00ae9b 3173
fb074239 3174 $stamp = (int) @file_get_contents(LOCK_DIRECTORY . "/update_daemon.stamp");
fbae93d8 3175
8e00ae9b 3176 if ($stamp) {
9041f58b
AD
3177 $stamp_delta = time() - $stamp;
3178
3179 if ($stamp_delta > 1800) {
f6854e44 3180 $stamp_check = 0;
8e00ae9b 3181 } else {
f6854e44
AD
3182 $stamp_check = 1;
3183 $_SESSION["daemon_stamp_check"] = time();
8e00ae9b
AD
3184 }
3185
c4f7ba80 3186 $data['daemon_stamp_ok'] = $stamp_check;
f6854e44 3187
8e00ae9b
AD
3188 $stamp_fmt = date("Y.m.d, G:i", $stamp);
3189
c4f7ba80 3190 $data['daemon_stamp'] = $stamp_fmt;
8e00ae9b 3191 }
8e00ae9b 3192 }
71ad883b 3193 }
8e00ae9b 3194
63855db1 3195 if ($_SESSION["last_version_check"] + 86400 + rand(-1000, 1000) < time()) {
fb074239 3196 $new_version_details = @check_for_update($link);
d9fa39f1 3197
63855db1 3198 $data['new_version_available'] = (int) ($new_version_details != false);
d9fa39f1
AD
3199
3200 $_SESSION["last_version_check"] = time();
d9fa39f1
AD
3201 }
3202
c4f7ba80 3203 return $data;
f54f515f 3204 }
ef393de7 3205
ab4b768f 3206 function getSearchSql($link, $search, $match_on) {
ef393de7 3207
88040f57 3208 $search_query_part = "";
e20c9d88 3209
88040f57
AD
3210 $keywords = split(" ", $search);
3211 $query_keywords = array();
e20c9d88 3212
ab4b768f
AD
3213 foreach ($keywords as $k) {
3214 if (strpos($k, "-") === 0) {
3215 $k = substr($k, 1);
3216 $not = "NOT";
3217 } else {
3218 $not = "";
88040f57 3219 }
e20c9d88 3220
ab4b768f 3221 if (strpos($k, "@") === 0) {
e20c9d88 3222
ab4b768f
AD
3223 $user_tz_string = get_pref($link, 'USER_TIMEZONE', $_SESSION['uid']);
3224 $orig_ts = strtotime(substr($k, 1));
eb6c7f42 3225
ab4b768f 3226 $k = date("Y-m-d", convert_timestamp($orig_ts, $user_tz_string, 'UTC'));
8d505d78 3227
ab4b768f
AD
3228 array_push($query_keywords, "(".SUBSTRING_FOR_DATE."(updated,1,LENGTH('$k')) $not = '$k')");
3229 } else if ($match_on == "both") {
3230 array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
3231 OR UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
3232 } else if ($match_on == "title") {
eb6c7f42 3233 array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%'))");
ab4b768f 3234 } else if ($match_on == "content") {
eb6c7f42 3235 array_push($query_keywords, "(UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
88040f57
AD
3236 }
3237 }
3238
3239 $search_query_part = implode("AND", $query_keywords);
3240
3241 return $search_query_part;
3242 }
3243
c36bf4d5
AD
3244 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) {
3245
3246 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
c1a0b534 3247
c3fddd05
AD
3248 $ext_tables_part = "";
3249
88040f57 3250 if ($search) {
e4f7f8df
AD
3251
3252 if (SPHINX_ENABLED) {
3253 $ids = join(",", @sphinx_search($search, 0, 500));
3254
8d505d78 3255 if ($ids)
e4f7f8df
AD
3256 $search_query_part = "ref_id IN ($ids) AND ";
3257 else
3258 $search_query_part = "ref_id = -1 AND ";
3259
3260 } else {
ab4b768f 3261 $search_query_part = getSearchSql($link, $search, $match_on);
e4f7f8df 3262 $search_query_part .= " AND ";
8d505d78 3263 }
e20c9d88 3264
ef393de7
AD
3265 } else {
3266 $search_query_part = "";
3267 }
3268
3269 $view_query_part = "";
8d505d78 3270
7b4d02a8 3271 if ($view_mode == "adaptive" || $view_query_part == "noscores") {
ef393de7
AD
3272 if ($search) {
3273 $view_query_part = " ";
3274 } else if ($feed != -1) {
f295c368 3275 $unread = getFeedUnread($link, $feed, $cat_view);
ef393de7 3276 if ($unread > 0) {
ff863e00 3277 $view_query_part = " unread = true AND ";
ef393de7
AD
3278 }
3279 }
3280 }
8d505d78 3281
ef393de7
AD
3282 if ($view_mode == "marked") {
3283 $view_query_part = " marked = true AND ";
3284 }
23d72f39
AD
3285
3286 if ($view_mode == "published") {
3287 $view_query_part = " published = true AND ";
3288 }
3289
ef393de7
AD
3290 if ($view_mode == "unread") {
3291 $view_query_part = " unread = true AND ";
3292 }
8b09eac8
AD
3293
3294 if ($view_mode == "updated") {
3295 $view_query_part = " (last_read is null and unread = false) AND ";
3296 }
3297
ef393de7
AD
3298 if ($limit > 0) {
3299 $limit_query_part = "LIMIT " . $limit;
8d505d78 3300 }
ef393de7
AD
3301
3302 $vfeed_query_part = "";
8d505d78 3303
ef393de7
AD
3304 // override query strategy and enable feed display when searching globally
3305 if ($search && $search_mode == "all_feeds") {
3306 $query_strategy_part = "ttrss_entries.id > 0";
8d505d78 3307 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
22fdebff 3308 /* tags */
ef393de7
AD
3309 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
3310 $query_strategy_part = "ttrss_entries.id > 0";
3311 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
3312 id = feed_id) as feed_title,";
e04c18a2 3313 } else if ($feed > 0 && $search && $search_mode == "this_cat") {
8d505d78
AD
3314
3315 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
0a6c4846
AD
3316
3317 $tmp_result = false;
3318
3319 if ($cat_view) {
8d505d78 3320 $tmp_result = db_query($link, "SELECT id
0a6c4846
AD
3321 FROM ttrss_feeds WHERE cat_id = '$feed'");
3322 } else {
3323 $tmp_result = db_query($link, "SELECT id
8d505d78 3324 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
0a6c4846
AD
3325 WHERE id = '$feed') AND id != '$feed'");
3326 }
8d505d78 3327
ef393de7 3328 $cat_siblings = array();
8d505d78 3329
ef393de7
AD
3330 if (db_num_rows($tmp_result) > 0) {
3331 while ($p = db_fetch_assoc($tmp_result)) {
3332 array_push($cat_siblings, "feed_id = " . $p["id"]);
3333 }
8d505d78
AD
3334
3335 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
ef393de7 3336 $feed, implode(" OR ", $cat_siblings));
8d505d78 3337
ef393de7
AD
3338 } else {
3339 $query_strategy_part = "ttrss_entries.id > 0";
3340 }
8d505d78 3341
e04c18a2 3342 } else if ($feed > 0) {
8d505d78 3343
ef393de7 3344 if ($cat_view) {
5c365f60 3345
ef393de7
AD
3346 if ($feed > 0) {
3347 $query_strategy_part = "cat_id = '$feed'";
3348 } else {
3349 $query_strategy_part = "cat_id IS NULL";
3350 }
8d505d78 3351
ef393de7 3352 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
5c365f60 3353
8d505d78 3354 } else {
6e63a7c3 3355 $query_strategy_part = "feed_id = '$feed'";
ef393de7 3356 }
bfe5ddfc 3357 } else if ($feed == 0 && !$cat_view) { // archive virtual feed
e04c18a2 3358 $query_strategy_part = "feed_id IS NULL";
bfe5ddfc
AD
3359 } else if ($feed == 0 && $cat_view) { // uncategorized
3360 $query_strategy_part = "cat_id IS NULL";
3361 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
ef393de7
AD
3362 } else if ($feed == -1) { // starred virtual feed
3363 $query_strategy_part = "marked = true";
3364 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
e6a38cde
AD
3365 } else if ($feed == -2) { // published virtual feed OR labels category
3366
3367 if (!$cat_view) {
3368 $query_strategy_part = "published = true";
3369 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3370 } else {
3371 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3372
3373 $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2";
8d505d78 3374
e6a38cde
AD
3375 $query_strategy_part = "ttrss_labels2.id = ttrss_user_labels2.label_id AND
3376 ttrss_user_labels2.article_id = ref_id";
3377
3378 }
3379
2d24f032 3380 } else if ($feed == -3) { // fresh virtual feed
cd2cc43d 3381 $query_strategy_part = "unread = true AND score >= 0";
2d24f032 3382
7a22dc2a 3383 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
c1d7e6c3 3384
2d24f032 3385 if (DB_TYPE == "pgsql") {
8d505d78 3386 $query_strategy_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
2d24f032 3387 } else {
7608b38a 3388 $query_strategy_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2d24f032
AD
3389 }
3390
b2531a28
AD
3391 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3392 } else if ($feed == -4) { // all articles virtual feed
3393 $query_strategy_part = "true";
e4f4b46f 3394 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
ef393de7
AD
3395 } else if ($feed <= -10) { // labels
3396 $label_id = -$feed - 11;
3de0261a 3397
ceb30ba4
AD
3398 $query_strategy_part = "label_id = '$label_id' AND
3399 ttrss_labels2.id = ttrss_user_labels2.label_id AND
3400 ttrss_user_labels2.article_id = ref_id";
3de0261a 3401
ef393de7 3402 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
ceb30ba4 3403 $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2";
8d505d78 3404
ef393de7
AD
3405 } else {
3406 $query_strategy_part = "id > 0"; // dumb
3407 }
d6e5706d 3408
b3990c92
AD
3409 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
3410 $date_sort_field = "updated";
3411 } else {
3412 $date_sort_field = "date_entered";
3413 }
3414
7a22dc2a 3415 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
b3990c92 3416 $order_by = "$date_sort_field";
8d505d78 3417 } else {
b3990c92 3418 $order_by = "$date_sort_field DESC";
d6e5706d 3419 }
e939722a 3420
7b4d02a8
AD
3421 if ($view_mode != "noscores") {
3422 $order_by = "score DESC, $order_by";
3423 }
48b0c4ec 3424
e939722a
AD
3425 if ($override_order) {
3426 $order_by = $override_order;
3427 }
8d505d78 3428
ef393de7
AD
3429 $feed_title = "";
3430
22fdebff
AD
3431 if ($search) {
3432 $feed_title = "Search results";
3433 } else {
ef393de7 3434 if ($cat_view) {
22fdebff 3435 $feed_title = getCategoryTitle($link, $feed);
ef393de7 3436 } else {
22fdebff 3437 if ((int)$feed == $feed && $feed > 0) {
8d505d78 3438 $result = db_query($link, "SELECT title,site_url,last_error
22fdebff 3439 FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = $owner_uid");
8d505d78 3440
22fdebff
AD
3441 $feed_title = db_fetch_result($result, 0, "title");
3442 $feed_site_url = db_fetch_result($result, 0, "site_url");
3443 $last_error = db_fetch_result($result, 0, "last_error");
3444 } else {
3445 $feed_title = getFeedTitle($link, $feed);
8d505d78 3446 }
88040f57 3447 }
ef393de7
AD
3448 }
3449
62129e67
AD
3450 $content_query_part = "content as content_preview,";
3451
ef393de7 3452 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
8d505d78 3453
ef393de7
AD
3454 if ($feed >= 0) {
3455 $feed_kind = "Feeds";
3456 } else {
3457 $feed_kind = "Labels";
3458 }
8d505d78 3459
95a82c08
AD
3460 if ($limit_query_part) {
3461 $offset_query_part = "OFFSET $offset";
3462 }
3463
d00f22ac 3464 if ($vfeed_query_part && get_pref($link, 'VFEED_GROUP_BY_FEED', $owner_uid)) {
6cfea5c7 3465 if (!$override_order) {
8d505d78 3466 $order_by = "ttrss_feeds.title, $order_by";
43fc671f 3467 }
6cfea5c7
AD
3468 }
3469
e04c18a2
AD
3470 if ($feed != "0") {
3471 $from_qpart = "ttrss_entries,ttrss_user_entries,ttrss_feeds$ext_tables_part";
117335bf 3472 $feed_check_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
e04c18a2
AD
3473
3474 } else {
3475 $from_qpart = "ttrss_entries,ttrss_user_entries$ext_tables_part
3476 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)";
3477 }
3478
8d505d78 3479 $query = "SELECT DISTINCT
f9b2d27c 3480 date_entered,
1f64b1be 3481 guid,
ef393de7 3482 ttrss_entries.id,ttrss_entries.title,
46921916 3483 updated,
c7e51de1 3484 note,
494a64ea 3485 unread,feed_id,marked,published,link,last_read,orig_feed_id,
fc2b26a6 3486 ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms,
ef393de7
AD
3487 $vfeed_query_part
3488 $content_query_part
fc2b26a6 3489 ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms,
ff6e357a 3490 author,score
ef393de7 3491 FROM
e04c18a2 3492 $from_qpart
ef393de7 3493 WHERE
e04c18a2 3494 $feed_check_qpart
ef393de7 3495 ttrss_user_entries.ref_id = ttrss_entries.id AND
c36bf4d5 3496 ttrss_user_entries.owner_uid = '$owner_uid' AND
ef393de7
AD
3497 $search_query_part
3498 $view_query_part
3499 $query_strategy_part ORDER BY $order_by
95a82c08 3500 $limit_query_part $offset_query_part";
4bc311fc 3501
b4e75b2a 3502 if ($_REQUEST["debug"]) print $query;
4bc311fc
AD
3503
3504 $result = db_query($link, $query);
8d505d78 3505
ef393de7
AD
3506 } else {
3507 // browsing by tag
8d505d78 3508
ef393de7 3509 $feed_kind = "Tags";
8d505d78 3510
dcb38ced
AD
3511 $result = db_query($link, "SELECT DISTINCT
3512 date_entered,
1f64b1be 3513 guid,
c7e51de1 3514 note,
ef393de7 3515 ttrss_entries.id as id,title,
46921916 3516 updated,
494a64ea 3517 unread,feed_id,orig_feed_id,
8d505d78 3518 marked,link,last_read,
fc2b26a6 3519 ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms,
ef393de7
AD
3520 $vfeed_query_part
3521 $content_query_part
4d0b3607
AD
3522 ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms,
3523 score
ef393de7
AD
3524 FROM
3525 ttrss_entries,ttrss_user_entries,ttrss_tags
3526 WHERE
8d505d78 3527 ref_id = ttrss_entries.id AND
c36bf4d5 3528 ttrss_user_entries.owner_uid = '$owner_uid' AND
ef393de7
AD
3529 post_int_id = int_id AND tag_name = '$feed' AND
3530 $view_query_part
3531 $search_query_part
3532 $query_strategy_part ORDER BY $order_by
8d505d78 3533 $limit_query_part");
ef393de7
AD
3534 }
3535
c7188969 3536 return array($result, $feed_title, $feed_site_url, $last_error);
8d505d78 3537
ef393de7
AD
3538 }
3539
c36bf4d5 3540 function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat,
23d72f39 3541 $limit, $search, $search_mode, $match_on, $view_mode = false) {
ead2715d 3542
db54143e 3543 $note_style = "float : right; background-color : #fff7d5; border-width : 1px; ".
c7e51de1 3544 "padding : 5px; border-style : dashed; border-color : #e7d796;".
db54143e 3545 "margin-bottom : 1em; color : #9a8c59;";
c7e51de1 3546
ead2715d 3547 if (!$limit) $limit = 30;
18664970 3548
b3990c92
AD
3549 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
3550 $date_sort_field = "updated";
3551 } else {
3552 $date_sort_field = "date_entered";
3553 }
3554
8d505d78
AD
3555 $qfh_ret = queryFeedHeadlines($link, $feed,
3556 $limit, $view_mode, $is_cat, $search, $search_mode,
b3990c92 3557 $match_on, "$date_sort_field DESC", 0, $owner_uid);
18664970
AD
3558
3559 $result = $qfh_ret[0];
59e2aab4 3560 $feed_title = htmlspecialchars($qfh_ret[1]);
18664970
AD
3561 $feed_site_url = $qfh_ret[2];
3562 $last_error = $qfh_ret[3];
3563
a5472764 3564// if (!$feed_site_url) $feed_site_url = "http://localhost/";
4bc64807 3565
a5472764
AD
3566 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>
3567 <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?>
3568 <rss version=\"2.0\">
3baeeeca
AD
3569 <channel>
3570 <title>$feed_title</title>
4bc64807
AD
3571 <link>$feed_site_url</link>
3572 <description>Feed generated by Tiny Tiny RSS</description>";
8d505d78 3573
3baeeeca
AD
3574 while ($line = db_fetch_assoc($result)) {
3575 print "<item>";
4bc64807 3576 print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>";
3baeeeca 3577 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
0c3d1c68 3578
bc976a8c 3579 $tags = get_article_tags($link, $line["id"], $owner_uid);
0c3d1c68
AD
3580
3581 foreach ($tags as $tag) {
3582 print "<category>" . htmlspecialchars($tag) . "</category>";
3583 }
3584
3baeeeca 3585 $rfc822_date = date('r', strtotime($line["updated"]));
8d505d78 3586
2f0903a6
AD
3587 print "<pubDate>$rfc822_date</pubDate>";
3588
3589 if ($line["author"]) {
3590 print "<author>" . htmlspecialchars($line["author"]) . "</author>";
3591 }
8d505d78
AD
3592
3593 print "<title><![CDATA[" .
e5208bac 3594 htmlspecialchars($line["title"]) . "]]></title>";
8d505d78 3595
db54143e
AD
3596 print "<description><![CDATA[";
3597
c7e51de1
AD
3598 if ($line["note"]) {
3599 print "<div style='$note_style'>";
3600 print $line["note"];
3601 print "</div>";
3602 }
db54143e 3603
ceb0cab5 3604 print sanitize_rss($link, $line["content_preview"], false, $owner_uid);
c7e51de1 3605 print "]]></description>";
8d505d78 3606
c7845467
AD
3607 $enclosures = get_article_enclosures($link, $line["id"]);
3608
3609 foreach ($enclosures as $e) {
3610 $type = htmlspecialchars($e['content_type']);
3611 $url = htmlspecialchars($e['content_url']);
3612 $length = $e['duration'];
3613 print "<enclosure url=\"$url\" type=\"$type\" length=\"$length\"/>";
3614 }
3615
3baeeeca
AD
3616 print "</item>";
3617 }
8d505d78 3618
3baeeeca 3619 print "</channel></rss>";
18664970
AD
3620
3621 }
3622
0a6c4846
AD
3623 function getCategoryTitle($link, $cat_id) {
3624
bba7c4bf
AD
3625 if ($cat_id == -1) {
3626 return __("Special");
3627 } else if ($cat_id == -2) {
3628 return __("Labels");
0a6c4846 3629 } else {
bba7c4bf
AD
3630
3631 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
3632 id = '$cat_id'");
3633
3634 if (db_num_rows($result) == 1) {
3635 return db_fetch_result($result, 0, "title");
3636 } else {
3637 return "Uncategorized";
3638 }
0a6c4846
AD
3639 }
3640 }
3641
8cc3c778 3642 function sanitize_rss($link, $str, $force_strip_tags = false, $owner = false, $site_url = false) {
35ffb5b9 3643 global $purifier;
8cc3c778 3644
ceb0cab5
AD
3645 if (!$owner) $owner = $_SESSION["uid"];
3646
96811a55
AD
3647 $res = trim($str); if (!$res) return '';
3648
ceb0cab5 3649 if (get_pref($link, "STRIP_UNSAFE_TAGS", $owner) || $force_strip_tags) {
35ffb5b9 3650 $res = $purifier->purify($res);
f826eee1
AD
3651 }
3652
ceb0cab5 3653 if (get_pref($link, "STRIP_IMAGES", $owner)) {
8dccabed 3654 $res = preg_replace('/<img[^>]+>/is', '', $res);
7514749d 3655 }
8dccabed 3656
46137483
AD
3657 if (strpos($res, "href=") === false)
3658 $res = rewrite_urls($res);
533c0ea6 3659
8cc3c778
AD
3660 $charset_hack = '<head>
3661 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
3662 </head>';
3663
96811a55
AD
3664 $res = trim($res); if (!$res) return '';
3665
8cc3c778
AD
3666 libxml_use_internal_errors(true);
3667
3668 $doc = new DOMDocument();
3669 $doc->loadHTML($charset_hack . $res);
3670 $xpath = new DOMXPath($doc);
8d505d78 3671
8cc3c778 3672 $entries = $xpath->query('(//a[@href]|//img[@src])');
3f770c87 3673 $br_inserted = 0;
8cc3c778
AD
3674
3675 foreach ($entries as $entry) {
3676
3677 if ($site_url) {
3678
3679 if ($entry->hasAttribute('href'))
3680 $entry->setAttribute('href',
3681 rewrite_relative_url($site_url, $entry->getAttribute('href')));
8d505d78 3682
8cc3c778 3683 if ($entry->hasAttribute('src'))
8d505d78 3684 if (preg_match('/^image.php\?i=[a-z0-9]+$/', $entry->getAttribute('src')) == 0)
b8998470
AD
3685 $entry->setAttribute('src',
3686 rewrite_relative_url($site_url, $entry->getAttribute('src')));
8cc3c778
AD
3687 }
3688
fa403733 3689 if (strtolower($entry->nodeName) == "a") {
c401d5c9 3690 $entry->setAttribute("target", "_blank");
fa403733
AD
3691 }
3692
3f770c87 3693 if (strtolower($entry->nodeName) == "img" && !$br_inserted) {
fa403733
AD
3694 $br = $doc->createElement("br");
3695
3f770c87 3696 if ($entry->parentNode->nextSibling) {
fa403733 3697 $entry->parentNode->insertBefore($br, $entry->nextSibling);
3f770c87
AD
3698 $br_inserted = 1;
3699 }
fa403733 3700
8cc3c778 3701 }
8dccabed 3702 }
8d505d78 3703
1f6131f5 3704 $node = $doc->getElementsByTagName('body')->item(0);
8dccabed 3705
8d505d78 3706 return $doc->saveXML($node);
183ad07b 3707 }
b72c3ef8 3708
45004d43
AD
3709 /**
3710 * Send by mail a digest of last articles.
8d505d78 3711 *
45004d43
AD
3712 * @param mixed $link The database connection.
3713 * @param integer $limit The maximum number of articles by digest.
3714 * @return boolean Return false if digests are not enabled.
3715 */
9cd7c995
AD
3716 function send_headlines_digests($link, $limit = 100) {
3717
1ddba275
AD
3718 if (!DIGEST_ENABLE) return false;
3719
9cd7c995 3720 $user_limit = DIGEST_EMAIL_LIMIT;
5430c959 3721 $days = 1;
9cd7c995
AD
3722
3723 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
3724
3725 if (DB_TYPE == "pgsql") {
3726 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
3727 } else if (DB_TYPE == "mysql") {
3728 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
3729 }
3730
8d505d78 3731 $result = db_query($link, "SELECT id,email FROM ttrss_users
9cd7c995
AD
3732 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
3733
3734 while ($line = db_fetch_assoc($result)) {
dc85be2b 3735
9cd7c995
AD
3736 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
3737 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
3738
dc85be2b
AD
3739 $do_catchup = get_pref($link, 'DIGEST_CATCHUP', $line['id'], false);
3740
9cd7c995
AD
3741 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
3742 $digest = $tuple[0];
3743 $headlines_count = $tuple[1];
dc85be2b 3744 $affected_ids = $tuple[2];
c62a2c21 3745 $digest_text = $tuple[3];
9cd7c995
AD
3746
3747 if ($headlines_count > 0) {
a8931123 3748
c62a2c21 3749 $mail = new PHPMailer();
a8931123 3750
d134e3a3
AD
3751 $mail->PluginDir = "lib/phpmailer/";
3752 $mail->SetLanguage("en", "lib/phpmailer/language/");
a8931123 3753
c62a2c21 3754 $mail->CharSet = "UTF-8";
a8931123 3755
c62a2c21
AD
3756 $mail->From = DIGEST_FROM_ADDRESS;
3757 $mail->FromName = DIGEST_FROM_NAME;
3758 $mail->AddAddress($line["email"], $line["login"]);
c7ddac5c 3759
c62a2c21 3760 if (DIGEST_SMTP_HOST) {
a8931123
AD
3761 $mail->Host = DIGEST_SMTP_HOST;
3762 $mail->Mailer = "smtp";
19a1da0d 3763 $mail->SMTPAuth = DIGEST_SMTP_LOGIN != '';
a8931123
AD
3764 $mail->Username = DIGEST_SMTP_LOGIN;
3765 $mail->Password = DIGEST_SMTP_PASSWORD;
c62a2c21 3766 }
a8931123 3767
c62a2c21 3768 $mail->IsHTML(true);
163a295e 3769 $mail->Subject = DIGEST_SUBJECT;
c62a2c21
AD
3770 $mail->Body = $digest;
3771 $mail->AltBody = $digest_text;
a8931123 3772
c62a2c21 3773 $rc = $mail->Send();
a8931123 3774
c62a2c21 3775 if (!$rc) print "ERROR: " . $mail->ErrorInfo;
a8931123 3776
9cd7c995 3777 print "RC=$rc\n";
a8931123 3778
c62a2c21 3779 if ($rc && $do_catchup) {
dc85be2b 3780 print "Marking affected articles as read...\n";
9968d46f 3781 catchupArticlesById($link, $affected_ids, 0, $line["id"]);
dc85be2b 3782 }
9cd7c995
AD
3783 } else {
3784 print "No headlines\n";
3785 }
019dd98d 3786
8d505d78 3787 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
019dd98d 3788 WHERE id = " . $line["id"]);
9cd7c995
AD
3789 }
3790 }
3791
cedd3e89
AD
3792 print "All done.\n";
3793
9cd7c995
AD
3794 }
3795
7e3634d9 3796 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
c62a2c21 3797
fe7537b5 3798 require_once "lib/MiniTemplator.class.php";
c62a2c21
AD
3799
3800 $tpl = new MiniTemplator;
3801 $tpl_t = new MiniTemplator;
3802
3803 $tpl->readTemplateFromFile("templates/digest_template_html.txt");
3804 $tpl_t->readTemplateFromFile("templates/digest_template.txt");
3805
3806 $tpl->setVariable('CUR_DATE', date('Y/m/d'));
3807 $tpl->setVariable('CUR_TIME', date('G:i'));
3808
3809 $tpl_t->setVariable('CUR_DATE', date('Y/m/d'));
3810 $tpl_t->setVariable('CUR_TIME', date('G:i'));
7e3634d9 3811
dc85be2b
AD
3812 $affected_ids = array();
3813
7e3634d9 3814 if (DB_TYPE == "pgsql") {
25ea2805 3815 $interval_query = "ttrss_entries.date_updated > NOW() - INTERVAL '$days days'";
7e3634d9 3816 } else if (DB_TYPE == "mysql") {
25ea2805 3817 $interval_query = "ttrss_entries.date_updated > DATE_SUB(NOW(), INTERVAL $days DAY)";
7e3634d9
AD
3818 }
3819
3820 $result = db_query($link, "SELECT ttrss_entries.title,
3821 ttrss_feeds.title AS feed_title,
25ea2805 3822 date_updated,
dc85be2b 3823 ttrss_user_entries.ref_id,
7e3634d9 3824 link,
c62a2c21 3825 SUBSTRING(content, 1, 120) AS excerpt,
fc2b26a6 3826 ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
8d505d78
AD
3827 FROM
3828 ttrss_user_entries,ttrss_entries,ttrss_feeds
3829 WHERE
3830 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
3dd9183c 3831 AND include_in_digest = true
7e3634d9 3832 AND $interval_query
448b0abd 3833 AND ttrss_user_entries.owner_uid = $user_id
8d505d78 3834 AND unread = true
25ea2805 3835 ORDER BY ttrss_feeds.title, date_updated DESC
7e3634d9
AD
3836 LIMIT $limit");
3837
3838 $cur_feed_title = "";
3839
9cd7c995
AD
3840 $headlines_count = db_num_rows($result);
3841
c62a2c21
AD
3842 $headlines = array();
3843
7e3634d9 3844 while ($line = db_fetch_assoc($result)) {
c62a2c21
AD
3845 array_push($headlines, $line);
3846 }
3847
8d505d78 3848 for ($i = 0; $i < sizeof($headlines); $i++) {
c62a2c21
AD
3849
3850 $line = $headlines[$i];
dc85be2b
AD
3851
3852 array_push($affected_ids, $line["ref_id"]);
3853
324944f3
AD
3854 $updated = make_local_datetime($link, $line['last_updated'], false,
3855 $user_id);
7e3634d9 3856
c62a2c21
AD
3857 $tpl->setVariable('FEED_TITLE', $line["feed_title"]);
3858 $tpl->setVariable('ARTICLE_TITLE', $line["title"]);
3859 $tpl->setVariable('ARTICLE_LINK', $line["link"]);
3860 $tpl->setVariable('ARTICLE_UPDATED', $updated);
8d505d78 3861 $tpl->setVariable('ARTICLE_EXCERPT',
163a295e 3862 truncate_string(strip_tags($line["excerpt"]), 100));
7e3634d9 3863
c62a2c21
AD
3864 $tpl->addBlock('article');
3865
3866 $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]);
3867 $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]);
3868 $tpl_t->setVariable('ARTICLE_LINK', $line["link"]);
3869 $tpl_t->setVariable('ARTICLE_UPDATED', $updated);
8d505d78 3870// $tpl_t->setVariable('ARTICLE_EXCERPT',
c62a2c21
AD
3871// truncate_string(strip_tags($line["excerpt"]), 100));
3872
3873 $tpl_t->addBlock('article');
3874
3875 if ($headlines[$i]['feed_title'] != $headlines[$i+1]['feed_title']) {
3876 $tpl->addBlock('feed');
3877 $tpl_t->addBlock('feed');
7e3634d9
AD
3878 }
3879
7e3634d9
AD
3880 }
3881
c62a2c21
AD
3882 $tpl->addBlock('digest');
3883 $tpl->generateOutputToString($tmp);
3884
3885 $tpl_t->addBlock('digest');
3886 $tpl_t->generateOutputToString($tmp_t);
7e3634d9 3887
c62a2c21 3888 return array($tmp, $headlines_count, $affected_ids, $tmp_t);
7e3634d9
AD
3889 }
3890
73495fd1 3891 function check_for_update($link) {
63855db1
AD
3892 if (CHECK_FOR_NEW_VERSION && $_SESSION['access_level'] >= 10) {
3893 $version_url = "http://tt-rss.org/version.php?ver=" . VERSION;
b72c3ef8 3894
63855db1 3895 $version_data = @fetch_file_contents($version_url);
b72c3ef8 3896
63855db1
AD
3897 if ($version_data) {
3898 $version_data = json_decode($version_data, true);
8d505d78 3899 if ($version_data && $version_data['version']) {
f67d9754 3900
63855db1 3901 if (version_compare(VERSION, $version_data['version']) == -1) {
e91ad1e9 3902 return $version_data;
63855db1
AD
3903 }
3904 }
f67d9754 3905 }
b72c3ef8 3906 }
63855db1 3907 return false;
b72c3ef8 3908 }
472782e8 3909
18eddb2c
AD
3910 function markArticlesById($link, $ids, $cmode) {
3911
3912 $tmp_ids = array();
3913
3914 foreach ($ids as $id) {
3915 array_push($tmp_ids, "ref_id = '$id'");
3916 }
3917
3918 $ids_qpart = join(" OR ", $tmp_ids);
3919
3920 if ($cmode == 0) {
8d505d78 3921 db_query($link, "UPDATE ttrss_user_entries SET
18eddb2c
AD
3922 marked = false,last_read = NOW()
3923 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3924 } else if ($cmode == 1) {
8d505d78 3925 db_query($link, "UPDATE ttrss_user_entries SET
18eddb2c
AD
3926 marked = true
3927 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3928 } else {
8d505d78 3929 db_query($link, "UPDATE ttrss_user_entries SET
18eddb2c
AD
3930 marked = NOT marked,last_read = NOW()
3931 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3932 }
3933 }
3934
e4f4b46f
AD
3935 function publishArticlesById($link, $ids, $cmode) {
3936
3937 $tmp_ids = array();
3938
3939 foreach ($ids as $id) {
3940 array_push($tmp_ids, "ref_id = '$id'");
3941 }
3942
3943 $ids_qpart = join(" OR ", $tmp_ids);
3944
3945 if ($cmode == 0) {
8d505d78 3946 db_query($link, "UPDATE ttrss_user_entries SET
e4f4b46f
AD
3947 published = false,last_read = NOW()
3948 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3949 } else if ($cmode == 1) {
8d505d78 3950 db_query($link, "UPDATE ttrss_user_entries SET
e4f4b46f
AD
3951 published = true
3952 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3953 } else {
8d505d78 3954 db_query($link, "UPDATE ttrss_user_entries SET
e4f4b46f
AD
3955 published = NOT published,last_read = NOW()
3956 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3957 }
3958 }
3959
9968d46f
AD
3960 function catchupArticlesById($link, $ids, $cmode, $owner_uid = false) {
3961
3962 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
ed41f171 3963 if (count($ids) == 0) return;
472782e8
AD
3964
3965 $tmp_ids = array();
3966
3967 foreach ($ids as $id) {
3968 array_push($tmp_ids, "ref_id = '$id'");
3969 }
3970
3971 $ids_qpart = join(" OR ", $tmp_ids);
3972
3973 if ($cmode == 0) {
8d505d78 3974 db_query($link, "UPDATE ttrss_user_entries SET
472782e8 3975 unread = false,last_read = NOW()
9968d46f 3976 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8 3977 } else if ($cmode == 1) {
8d505d78 3978 db_query($link, "UPDATE ttrss_user_entries SET
472782e8 3979 unread = true
9968d46f 3980 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8 3981 } else {
8d505d78 3982 db_query($link, "UPDATE ttrss_user_entries SET
472782e8 3983 unread = NOT unread,last_read = NOW()
9968d46f 3984 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8 3985 }
0737b95a
AD
3986
3987 /* update ccache */
3988
3989 $result = db_query($link, "SELECT DISTINCT feed_id FROM ttrss_user_entries
3990 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
3991
3992 while ($line = db_fetch_assoc($result)) {
3993 ccache_update($link, $line["feed_id"], $owner_uid);
3994 }
472782e8
AD
3995 }
3996
e097e8be
AD
3997 function catchupArticleById($link, $id, $cmode) {
3998
3999 if ($cmode == 0) {
8d505d78 4000 db_query($link, "UPDATE ttrss_user_entries SET
e097e8be
AD
4001 unread = false,last_read = NOW()
4002 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
4003 } else if ($cmode == 1) {
8d505d78 4004 db_query($link, "UPDATE ttrss_user_entries SET
e097e8be
AD
4005 unread = true
4006 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
4007 } else {
8d505d78 4008 db_query($link, "UPDATE ttrss_user_entries SET
e097e8be
AD
4009 unread = NOT unread,last_read = NOW()
4010 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
4011 }
ab197ae1
AD
4012
4013 $feed_id = getArticleFeed($link, $id);
4014 ccache_update($link, $feed_id, $_SESSION["uid"]);
e097e8be
AD
4015 }
4016
1f64b1be 4017 function make_guid_from_title($title) {
8d505d78 4018 return preg_replace("/[ \"\',.:;]/", "-",
fefef828 4019 mb_strtolower(strip_tags($title), 'utf-8'));
1f64b1be
AD
4020 }
4021
bd202c3f 4022 function format_headline_subtoolbar($link, $feed_site_url, $feed_title,
0ef68e6f 4023 $feed_id, $is_cat, $search, $match_on,
26a1e185 4024 $search_mode, $view_mode, $error) {
e6c115b2 4025
bd202c3f
AD
4026 $page_prev_link = "viewFeedGoPage(-1)";
4027 $page_next_link = "viewFeedGoPage(1)";
4028 $page_first_link = "viewFeedGoPage(0)";
203de776 4029
bd202c3f
AD
4030 $catchup_page_link = "catchupPage()";
4031 $catchup_feed_link = "catchupCurrentFeed()";
4032 $catchup_sel_link = "catchupSelection()";
c6008b62 4033
bd202c3f
AD
4034 $archive_sel_link = "archiveSelection()";
4035 $delete_sel_link = "deleteSelection()";
e04c18a2 4036
bd202c3f
AD
4037 $sel_all_link = "selectArticles('all')";
4038 $sel_unread_link = "selectArticles('unread')";
4039 $sel_none_link = "selectArticles('none')";
4040 $sel_inv_link = "selectArticles('invert')";
11befbb2 4041
bd202c3f
AD
4042 $tog_unread_link = "selectionToggleUnread()";
4043 $tog_marked_link = "selectionToggleMarked()";
4044 $tog_published_link = "selectionTogglePublished()";
fcf70c51 4045
bd202c3f 4046 $reply = "<div id=\"subtoolbar_main\">";
fcf70c51 4047
bd202c3f
AD
4048 $reply .= __('Select:')."
4049 <a href=\"#\" onclick=\"$sel_all_link\">".__('All')."</a>,
4050 <a href=\"#\" onclick=\"$sel_unread_link\">".__('Unread')."</a>,
4051 <a href=\"#\" onclick=\"$sel_inv_link\">".__('Invert')."</a>,
4052 <a href=\"#\" onclick=\"$sel_none_link\">".__('None')."</a></li>";
fcf70c51 4053
bd202c3f 4054 $reply .= " ";
fcf70c51 4055
bd202c3f
AD
4056 $reply .= "<select dojoType=\"dijit.form.Select\"
4057 onchange=\"headlineActionsChange(this)\">";
4058 $reply .= "<option value=\"false\">".__('Actions...')."</option>";
fcf70c51 4059
bd202c3f 4060 $reply .= "<option value=\"0\" disabled=\"1\">".__('Selection toggle:')."</option>";
fcf70c51 4061
bd202c3f
AD
4062 $reply .= "<option value=\"$tog_unread_link\">".__('Unread')."</option>
4063 <option value=\"$tog_marked_link\">".__('Starred')."</option>
4064 <option value=\"$tog_published_link\">".__('Published')."</option>";
fcf70c51 4065
bd202c3f 4066 $reply .= "<option value=\"0\" disabled=\"1\">".__('Selection:')."</option>";
fcf70c51 4067
bd202c3f 4068 $reply .= "<option value=\"$catchup_sel_link\">".__('Mark as read')."</option>";
fcf70c51 4069
bd202c3f
AD
4070 if ($feed_id != "0") {
4071 $reply .= "<option value=\"$archive_sel_link\">".__('Archive')."</option>";
4072 } else {
4073 $reply .= "<option value=\"$archive_sel_link\">".__('Move back')."</option>";
4074 $reply .= "<option value=\"$delete_sel_link\">".__('Delete')."</option>";
fcf70c51 4075
bd202c3f 4076 }
fcf70c51 4077
bd202c3f
AD
4078 $reply .= "<option value=\"emailArticle(false)\">".__('Forward by email').
4079 "</option>";
fcf70c51 4080
bd202c3f
AD
4081 $rss_link = htmlspecialchars(get_self_url_prefix() .
4082 "/backend.php?op=rss&id=$feed_id&is_cat=$is_cat&view_mode=$view_mode$search_q");
0f9b4a60 4083
bd202c3f 4084 $reply .= "<option value=\"0\" disabled=\"1\">".__('Feed:')."</option>";
176b8ba6 4085
bd202c3f 4086 $reply .= "<option value=\"catchupPage()\">".__('Mark as read')."</option>";
fcf70c51 4087
bd202c3f 4088 $reply .= "<option value=\"displayDlg('generatedFeed', '$feed_id:$is_cat:$rss_link')\">".__('View as RSS')."</option>";
fcf70c51 4089
bd202c3f 4090 $reply .= "</select>";
c6008b62 4091
bd202c3f 4092 $reply .= "</div>";
0ef68e6f 4093
bd202c3f 4094 $reply .= "<div id=\"subtoolbar_ftitle\">";
26a1e185 4095
bd202c3f
AD
4096 if ($feed_site_url) {
4097 $target = "target=\"_blank\"";
4098 $reply .= "<a title=\"".__("Visit the website")."\" $target href=\"$feed_site_url\">".
4099 truncate_string($feed_title,30)."</a>";
26a1e185 4100
bd202c3f
AD
4101 if ($error) {
4102 $reply .= " (<span class=\"error\" title=\"$error\">Error</span>)";
4103 }
5163fc70 4104
bd202c3f
AD
4105 } else {
4106 if ($feed_id < -10) {
4107 $label_id = -11-$feed_id;
5163fc70 4108
bd202c3f
AD
4109 $result = db_query($link, "SELECT fg_color, bg_color
4110 FROM ttrss_labels2 WHERE id = '$label_id' AND owner_uid = " .
4111 $_SESSION["uid"]);
5163fc70 4112
bd202c3f
AD
4113 if (db_num_rows($result) != 0) {
4114 $fg_color = db_fetch_result($result, 0, "fg_color");
4115 $bg_color = db_fetch_result($result, 0, "bg_color");
5163fc70 4116
bd202c3f
AD
4117 $reply .= "<span style='background : $bg_color; color : $fg_color'>";
4118 $reply .= $feed_title;
4119 $reply .= "</span>";
5163fc70 4120 } else {
bd202c3f 4121 $reply .= $feed_title;
5163fc70 4122 }
0ef68e6f 4123
c3fddd05 4124 } else {
bd202c3f 4125 $reply .= $feed_title;
0ef68e6f 4126 }
bd202c3f 4127 }
0ef68e6f 4128
bd202c3f
AD
4129 if ($search) {
4130 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
4131 } else {
4132 $search_q = "";
4133 }
aa1c2aa4 4134
bd202c3f
AD
4135 // Adaptive doesn't really make any sense for generated feeds
4136 // All Articles is the default, so no need to insert it either
4137 if ($view_mode == "adaptive" || $view_mode == "all_articles")
4138 $view_mode = "";
4139 else
4140 $view_mode = "&view-mode=$view_mode";
b8a637f3 4141
bd202c3f
AD
4142 $reply .= "
4143 <a href=\"#\"
4144 title=\"".__("View as RSS feed")."\"
4145 onclick=\"displayDlg('generatedFeed', '$feed_id:$is_cat:$rss_link')\">
4146 <img class=\"noborder\" style=\"vertical-align : middle\" src=\"images/feed-icon-12x12.png\"></a>";
ceb30ba4 4147
bd202c3f
AD
4148 $reply .= "</div>";
4149
4150 return $reply;
4151 }
8d505d78 4152
1985a5e0 4153 function outputFeedList($link, $special = true) {
f407c086 4154
13e785e0
AD
4155 $feedlist = array();
4156
4157 $enable_cats = get_pref($link, 'ENABLE_FEED_CATS');
4158
4159 $feedlist['identifier'] = 'id';
4160 $feedlist['label'] = 'name';
4161 $feedlist['items'] = array();
f407c086
AD
4162
4163 $owner_uid = $_SESSION["uid"];
4164
cf4d339c 4165 /* virtual feeds */
f407c086 4166
1985a5e0 4167 if ($special) {
e4f4b46f 4168
1985a5e0
AD
4169 if ($enable_cats) {
4170 $cat_hidden = get_pref($link, "_COLLAPSED_SPECIAL");
4171 $cat = feedlist_init_cat($link, -1, $cat_hidden);
4172 } else {
4173 $cat['items'] = array();
4174 }
8d505d78 4175
1985a5e0
AD
4176 foreach (array(-4, -3, -1, -2, 0) as $i) {
4177 array_push($cat['items'], feedlist_init_feed($link, $i));
4178 }
8d505d78 4179
1985a5e0
AD
4180 if ($enable_cats) {
4181 array_push($feedlist['items'], $cat);
4182 } else {
4183 $feedlist['items'] = array_merge($feedlist['items'], $cat['items']);
4184 }
8d505d78 4185
1985a5e0
AD
4186 $result = db_query($link, "SELECT * FROM
4187 ttrss_labels2 WHERE owner_uid = '$owner_uid' ORDER by caption");
9fe80bcd 4188
d4f46bc1 4189 if (db_num_rows($result) > 0) {
9fe80bcd 4190
d4f46bc1
AD
4191 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4192 $cat_hidden = get_pref($link, "_COLLAPSED_LABELS");
4193 $cat = feedlist_init_cat($link, -2, $cat_hidden);
4194 } else {
4195 $cat['items'] = array();
4196 }
9fe80bcd 4197
d4f46bc1 4198 while ($line = db_fetch_assoc($result)) {
8d505d78 4199
d4f46bc1
AD
4200 $label_id = -$line['id'] - 11;
4201 $count = getFeedUnread($link, $label_id);
8d505d78 4202
d4f46bc1 4203 $feed = feedlist_init_feed($link, $label_id, false, $count);
8d505d78 4204
d4f46bc1
AD
4205 $feed['fg_color'] = $line['fg_color'];
4206 $feed['bg_color'] = $line['bg_color'];
8d505d78 4207
d4f46bc1
AD
4208 array_push($cat['items'], $feed);
4209 }
8d505d78 4210
d4f46bc1
AD
4211 if ($enable_cats) {
4212 array_push($feedlist['items'], $cat);
4213 } else {
4214 $feedlist['items'] = array_merge($feedlist['items'], $cat['items']);
4215 }
1985a5e0 4216 }
9c99281f 4217 }
8d505d78 4218
76657c46 4219/* if (get_pref($link, 'ENABLE_FEED_CATS')) {
9c99281f
AD
4220 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
4221 $order_by_qpart = "order_id,category,unread DESC,title";
13e785e0 4222 } else {
9c99281f 4223 $order_by_qpart = "order_id,category,title";
f407c086 4224 }
9c99281f
AD
4225 } else {
4226 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
4227 $order_by_qpart = "unread DESC,title";
8d505d78 4228 } else {
9c99281f 4229 $order_by_qpart = "title";
f407c086 4230 }
76657c46
AD
4231 } */
4232
4d65b7df
AD
4233 /* real feeds */
4234
76657c46 4235 if ($enable_cats)
7b8a143f
AD
4236 $order_by_qpart = "ttrss_feed_categories.order_id,category,
4237 ttrss_feeds.order_id,title";
76657c46
AD
4238 else
4239 $order_by_qpart = "title";
f407c086 4240
9c99281f 4241 $age_qpart = getMaxAgeSubquery();
f407c086 4242
9c99281f
AD
4243 $query = "SELECT ttrss_feeds.id, ttrss_feeds.title,
4244 ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated_noms,
4245 cat_id,last_error,
4246 ttrss_feed_categories.title AS category,
4247 ttrss_feed_categories.collapsed,
8d505d78 4248 value AS unread
9c99281f 4249 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
8d505d78
AD
4250 ON (ttrss_feed_categories.id = cat_id)
4251 LEFT JOIN ttrss_counters_cache
9c99281f
AD
4252 ON
4253 (ttrss_feeds.id = feed_id)
8d505d78 4254 WHERE
9c99281f 4255 ttrss_feeds.owner_uid = '$owner_uid'
8d505d78 4256 ORDER BY $order_by_qpart";
13e785e0 4257
9c99281f 4258 $result = db_query($link, $query);
13e785e0 4259
9c99281f 4260 $actid = $_REQUEST["actid"];
0f39ae20 4261
4d65b7df 4262 if (db_num_rows($result) > 0) {
f407c086 4263
4d65b7df 4264 $category = "";
8d505d78
AD
4265
4266 if (!$enable_cats)
4d65b7df
AD
4267 $cat['items'] = array();
4268 else
4269 $cat = false;
8d505d78 4270
4d65b7df 4271 while ($line = db_fetch_assoc($result)) {
8d505d78 4272
4d65b7df 4273 $feed = htmlspecialchars(trim($line["title"]));
8d505d78 4274
4d65b7df 4275 if (!$feed) $feed = "[Untitled]";
8d505d78
AD
4276
4277 $feed_id = $line["id"];
4d65b7df 4278 $unread = $line["unread"];
8d505d78 4279
4d65b7df
AD
4280 $cat_id = $line["cat_id"];
4281 $tmp_category = $line["category"];
4282 if (!$tmp_category) $tmp_category = __("Uncategorized");
8d505d78 4283
4d65b7df 4284 if ($category != $tmp_category && $enable_cats) {
8d505d78 4285
4d65b7df 4286 $category = $tmp_category;
8d505d78 4287
4d65b7df 4288 $collapsed = sql_bool_to_bool($line["collapsed"]);
8d505d78 4289
4d65b7df
AD
4290 // workaround for NULL category
4291 if ($category == __("Uncategorized")) {
4292 $collapsed = get_pref($link, "_COLLAPSED_UNCAT");
4293 }
8d505d78 4294
4d65b7df 4295 if ($cat) array_push($feedlist['items'], $cat);
8d505d78 4296
4d65b7df 4297 $cat = feedlist_init_cat($link, $cat_id, $collapsed);
f0855b88 4298 }
8d505d78
AD
4299
4300 $updated = make_local_datetime($link, $line["updated_noms"], false);
4301
4302 array_push($cat['items'], feedlist_init_feed($link, $feed_id,
4d65b7df
AD
4303 $feed, $unread, $line['last_error'], $updated));
4304 }
8d505d78 4305
4d65b7df
AD
4306 if ($enable_cats) {
4307 array_push($feedlist['items'], $cat);
8d505d78 4308 } else {
4d65b7df 4309 $feedlist['items'] = array_merge($feedlist['items'], $cat['items']);
f407c086
AD
4310 }
4311
9c99281f 4312 }
13e785e0
AD
4313
4314 return $feedlist;
f407c086
AD
4315 }
4316
bc976a8c 4317 function get_article_tags($link, $id, $owner_uid = 0) {
0b126ac2 4318
bd3f2ade
AD
4319 global $memcache;
4320
0b126ac2
AD
4321 $a_id = db_escape_string($id);
4322
bc976a8c
AD
4323 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
4324
8d505d78 4325 $query = "SELECT DISTINCT tag_name,
0c3d1c68 4326 owner_uid as owner FROM
0b126ac2 4327 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
bd3f2ade 4328 ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name";
0b126ac2 4329
bd3f2ade 4330 $obj_id = md5("TAGS:$owner_uid:$id");
8d505d78 4331 $tags = array();
bd3f2ade
AD
4332
4333 if ($memcache && $obj = $memcache->get($obj_id)) {
4334 $tags = $obj;
4335 } else {
490c366d
AD
4336 /* check cache first */
4337
4338 $result = db_query($link, "SELECT tag_cache FROM ttrss_user_entries
4339 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
4340
4341 $tag_cache = db_fetch_result($result, 0, "tag_cache");
4342
4343 if ($tag_cache) {
4344 $tags = explode(",", $tag_cache);
4345 } else {
bd3f2ade 4346
490c366d
AD
4347 /* do it the hard way */
4348
4349 $tmp_result = db_query($link, $query);
4350
4351 while ($tmp_line = db_fetch_assoc($tmp_result)) {
8d505d78 4352 array_push($tags, $tmp_line["tag_name"]);
490c366d
AD
4353 }
4354
4355 /* update the cache */
4356
4357 $tags_str = db_escape_string(join(",", $tags));
4358
8d505d78 4359 db_query($link, "UPDATE ttrss_user_entries
490c366d
AD
4360 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
4361 AND owner_uid = " . $_SESSION["uid"]);
bd3f2ade
AD
4362 }
4363
4364 if ($memcache) $memcache->add($obj_id, $tags, 0, 3600);
0b126ac2
AD
4365 }
4366
4367 return $tags;
4368 }
4369
d62a3b63
AD
4370 function trim_value(&$value) {
4371 $value = trim($value);
8d505d78 4372 }
d62a3b63
AD
4373
4374 function trim_array($array) {
4375 $tmp = $array;
4376 array_walk($tmp, 'trim_value');
4377 return $tmp;
4378 }
4379
be832a1a 4380 function tag_is_valid($tag) {
ef063748
AD
4381 if ($tag == '') return false;
4382 if (preg_match("/^[0-9]*$/", $tag)) return false;
41f7498a 4383 if (mb_strlen($tag) > 250) return false;
ef063748 4384
31365729
AD
4385 if (function_exists('iconv')) {
4386 $tag = iconv("utf-8", "utf-8", $tag);
4387 }
4388
ef063748
AD
4389 if (!$tag) return false;
4390
4391 return true;
be832a1a
AD
4392 }
4393
afb12ed0
AD
4394 function render_login_form($link, $mobile = 0) {
4395 switch ($mobile) {
4396 case 0:
793185a9 4397 require_once "login_form.php";
afb12ed0
AD
4398 break;
4399 case 1:
793185a9 4400 require_once "mobile/login_form.php";
afb12ed0
AD
4401 break;
4402 case 2:
4403 require_once "mobile/classic/login_form.php";
793185a9 4404 }
01a87dff
AD
4405 }
4406
dc56b3b7
AD
4407 // from http://developer.apple.com/internet/safari/faq.html
4408 function no_cache_incantation() {
4409 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
4410 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
4411 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
4412 header("Cache-Control: post-check=0, pre-check=0", false);
4413 header("Pragma: no-cache"); // HTTP/1.0
4414 }
4415
42395d28 4416 function format_warning($msg, $id = "") {
883fee8d 4417 global $link;
8d505d78 4418 return "<div class=\"warning\" id=\"$id\">
883fee8d 4419 <img src=\"".theme_image($link, "images/sign_excl.png")."\">$msg</div>";
0d32b41e
AD
4420 }
4421
08ac193a 4422 function format_notice($msg, $id = "") {
883fee8d 4423 global $link;
8d505d78 4424 return "<div class=\"notice\" id=\"$id\">
883fee8d 4425 <img src=\"".theme_image($link, "images/sign_info.png")."\">$msg</div>";
0d32b41e
AD
4426 }
4427
08ac193a 4428 function format_error($msg, $id = "") {
883fee8d 4429 global $link;
8d505d78 4430 return "<div class=\"error\" id=\"$id\">
883fee8d 4431 <img src=\"".theme_image($link, "images/sign_excl.png")."\">$msg</div>";
68d2f95e
AD
4432 }
4433
4dccf1ed
AD
4434 function print_notice($msg) {
4435 return print format_notice($msg);
4436 }
4437
4438 function print_warning($msg) {
4439 return print format_warning($msg);
4440 }
4441
68d2f95e
AD
4442 function print_error($msg) {
4443 return print format_error($msg);
4444 }
4445
4446
4dccf1ed
AD
4447 function T_sprintf() {
4448 $args = func_get_args();
4449 return vsprintf(__(array_shift($args)), $args);
4450 }
4451
51682b23
AD
4452 function format_inline_player($link, $url, $ctype) {
4453
4454 $entry = "";
4455
8d505d78 4456 if (strpos($ctype, "audio/") === 0) {
c3edc667
AD
4457
4458 if ($_SESSION["hasAudio"] && (strpos($ctype, "ogg") !== false ||
8d505d78 4459 strpos($_SERVER['HTTP_USER_AGENT'], "Chrome") !== false ||
c3edc667
AD
4460 strpos($_SERVER['HTTP_USER_AGENT'], "Safari") !== false )) {
4461
4462 $id = 'AUDIO-' . uniqid();
4463
4464 $entry .= "<audio id=\"$id\"\">
4465 <source src=\"$url\"></source>
8d505d78 4466 </audio>";
c3edc667 4467
8d505d78 4468 $entry .= "<span onclick=\"player(this)\"
c3edc667
AD
4469 title=\"".__("Click to play")."\" status=\"0\"
4470 class=\"player\" audio-id=\"$id\">".__("Play")."</span>";
4471
4472 } else {
8d505d78
AD
4473
4474 $entry .= "<object type=\"application/x-shockwave-flash\"
4475 data=\"extras/button/musicplayer.swf?song_url=$url\"
4476 width=\"17\" height=\"17\" style='float : left; margin-right : 5px;'>
4477 <param name=\"movie\"
c3edc667 4478 value=\"extras/button/musicplayer.swf?song_url=$url\" />
8d505d78 4479 </object>";
c3edc667 4480 }
51682b23
AD
4481 }
4482
c3edc667
AD
4483 $filename = substr($url, strrpos($url, "/")+1);
4484
4485 $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
4486 $filename . " (" . $ctype . ")" . "</a>";
4487
51682b23
AD
4488 return $entry;
4489 }
4490
009646d2 4491 function format_article($link, $id, $feed_id, $mark_as_read = true,
eedfb635 4492 $zoom_mode = false) {
3de0261a 4493
009646d2
AD
4494 $rv = array();
4495
4496 $rv['id'] = $id;
4497
10eb9da8 4498 /* we can figure out feed_id from article id anyway, why do we
e04c18a2 4499 * pass feed_id here? let's ignore the argument :( */
10eb9da8
AD
4500
4501 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
4502 WHERE ref_id = '$id'");
4503
e04c18a2 4504 $feed_id = (int) db_fetch_result($result, 0, "feed_id");
10eb9da8 4505
009646d2
AD
4506 $rv['feed_id'] = $feed_id;
4507
4508 //if (!$zoom_mode) { print "<article id='$id'><![CDATA["; };
3de0261a 4509
54e61a68 4510 $result = db_query($link, "SELECT rtl_content, always_display_enclosures FROM ttrss_feeds
3de0261a
AD
4511 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
4512
4513 if (db_num_rows($result) == 1) {
4514 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
54e61a68 4515 $always_display_enclosures = sql_bool_to_bool(db_fetch_result($result, 0, "always_display_enclosures"));
3de0261a
AD
4516 } else {
4517 $rtl_content = false;
54e61a68 4518 $always_display_enclosures = false;
3de0261a
AD
4519 }
4520
4521 if ($rtl_content) {
4522 $rtl_tag = "dir=\"RTL\"";
4523 $rtl_class = "RTL";
4524 } else {
4525 $rtl_tag = "";
4526 $rtl_class = "";
4527 }
4528
4529 if ($mark_as_read) {
8d505d78
AD
4530 $result = db_query($link, "UPDATE ttrss_user_entries
4531 SET unread = false,last_read = NOW()
3de0261a 4532 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
8a4c759e
AD
4533
4534 ccache_update($link, $feed_id, $_SESSION["uid"]);
3de0261a
AD
4535 }
4536
4537 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
fc2b26a6 4538 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
3de0261a 4539 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
8cc3c778 4540 (SELECT site_url FROM ttrss_feeds WHERE id = feed_id) as site_url,
3de0261a 4541 num_comments,
c7e51de1 4542 author,
ef83538d 4543 orig_feed_id,
c7e51de1 4544 note
3de0261a
AD
4545 FROM ttrss_entries,ttrss_user_entries
4546 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
4547
4548 if ($result) {
4549
3de0261a
AD
4550 $line = db_fetch_assoc($result);
4551
4552 if ($line["icon_url"]) {
8e289ca1 4553 $feed_icon = "<img src=\"" . $line["icon_url"] . "\">";
3de0261a
AD
4554 } else {
4555 $feed_icon = "&nbsp;";
4556 }
4557
8cc3c778
AD
4558 $feed_site_url = $line['site_url'];
4559
3de0261a
AD
4560 $num_comments = $line["num_comments"];
4561 $entry_comments = "";
4562
4563 if ($num_comments > 0) {
4564 if ($line["comments"]) {
4565 $comments_url = $line["comments"];
4566 } else {
4567 $comments_url = $line["link"];
4568 }
7514749d 4569 $entry_comments = "<a target='_blank' href=\"$comments_url\">$num_comments comments</a>";
3de0261a
AD
4570 } else {
4571 if ($line["comments"] && $line["link"] != $line["comments"]) {
7514749d 4572 $entry_comments = "<a target='_blank' href=\"".$line["comments"]."\">comments</a>";
8d505d78 4573 }
3de0261a
AD
4574 }
4575
eedfb635
AD
4576 if ($zoom_mode) {
4577 header("Content-Type: text/html");
009646d2 4578 $rv['content'] .= "<html><head>
5bb0cc8e 4579 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
eedfb635
AD
4580 <title>Tiny Tiny RSS - ".$line["title"]."</title>
4581 <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss.css\">
4582 </head><body>";
4583 }
4584
009646d2 4585 $rv['content'] .= "<div id=\"PTITLE-$id\" style=\"display : none\">" .
6f3976c9 4586 truncate_string(strip_tags($line['title']), 15) . "</div>";
eedfb635 4587
009646d2 4588 $rv['content'] .= "<div class=\"postReply\" id=\"POST-$id\">";
bc372fe3 4589
009646d2 4590 $rv['content'] .= "<div onclick=\"return postClicked(event, $id)\"
bc372fe3 4591 class=\"postHeader\" id=\"POSTHDR-$id\">";
3de0261a
AD
4592
4593 $entry_author = $line["author"];
4594
4595 if ($entry_author) {
60164936 4596 $entry_author = __(" - ") . $entry_author;
3de0261a
AD
4597 }
4598
8d505d78 4599 $parsed_updated = make_local_datetime($link, $line["updated"], true,
324944f3
AD
4600 false, true);
4601
009646d2 4602 $rv['content'] .= "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
3de0261a
AD
4603
4604 if ($line["link"]) {
009646d2 4605 $rv['content'] .= "<div clear='both'><a target='_blank'
a64029e5 4606 title=\"".htmlspecialchars($line['title'])."\"
8d505d78
AD
4607 href=\"" .
4608 $line["link"] . "\">" .
4609 truncate_string($line["title"], 100) .
a64029e5 4610 "<span class='author'>$entry_author</span></a></div>";
3de0261a 4611 } else {
009646d2 4612 $rv['content'] .= "<div clear='both'>" . $line["title"] . "$entry_author</div>";
3de0261a
AD
4613 }
4614
307d187c 4615 $tags_str = format_tags_string(get_article_tags($link, $id), $id);
e7544143 4616
3de0261a
AD
4617 if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
4618
009646d2 4619 $rv['content'] .= "<div style='float : right'>
8d505d78 4620 <img src='".theme_image($link, 'images/tag.png')."'
e9823609 4621 class='tagsPic' alt='Tags' title='Tags'>&nbsp;";
eedfb635
AD
4622
4623 if (!$zoom_mode) {
009646d2 4624 $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span>
8d505d78 4625 <a title=\"".__('Edit tags for this article')."\"
31a53903 4626 href=\"#\" onclick=\"editArticleTags($id, $feed_id)\">(+)</a>";
4710e3dc 4627
009646d2 4628 $rv['content'] .= "<img src=\"".theme_image($link, 'images/art-zoom.png')."\"
9ed0b90f 4629 class='tagsPic' style=\"cursor : pointer\"
ca07f49e 4630 onclick=\"postOpenInNewTab(event, $id)\"
6f3976c9 4631 alt='Zoom' title='".__('Open article in new tab')."'>";
c7e51de1 4632
741b6090 4633 //$note_escaped = htmlspecialchars($line['note'], ENT_QUOTES);
c7e51de1 4634
009646d2 4635 $rv['content'] .= "<img src=\"".theme_image($link, 'images/art-pub-note.png')."\"
9ed0b90f 4636 class='tagsPic' style=\"cursor : pointer\"
741b6090
AD
4637 onclick=\"editArticleNote($id)\"
4638 alt='PubNote' title='".__('Edit article note')."'>";
c7e51de1 4639
31a53903 4640 if (DIGEST_ENABLE) {
009646d2 4641 $rv['content'] .= "<img src=\"".theme_image($link, 'images/art-email.png')."\"
9ed0b90f 4642 class='tagsPic' style=\"cursor : pointer\"
31a53903
AD
4643 onclick=\"emailArticle($id)\"
4644 alt='Zoom' title='".__('Forward by email')."'>";
4645 }
4646
411fe209 4647 if (ENABLE_TWEET_BUTTON) {
009646d2 4648 $rv['content'] .= "<img src=\"".theme_image($link, 'images/art-tweet.png')."\"
411fe209
AD
4649 class='tagsPic' style=\"cursor : pointer\"
4650 onclick=\"tweetArticle($id)\"
4651 alt='Zoom' title='".__('Share on Twitter')."'>";
4652 }
4653
009646d2 4654 $rv['content'] .= "<img src=\"".theme_image($link, 'images/digest_checkbox.png')."\"
6f3976c9 4655 class='tagsPic' style=\"cursor : pointer\"
e3387e2d 4656 onclick=\"closeArticlePanel($id)\"
6f3976c9
AD
4657 alt='Zoom' title='".__('Close this panel')."'>";
4658
24ecbcae
AD
4659 } else {
4660 $tags_str = strip_tags($tags_str);
009646d2 4661 $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span>";
eedfb635 4662 }
009646d2
AD
4663 $rv['content'] .= "</div>";
4664 $rv['content'] .= "<div clear='both'>$entry_comments</div>";
3de0261a 4665
ef83538d
AD
4666 if ($line["orig_feed_id"]) {
4667
4668 $tmp_result = db_query($link, "SELECT * FROM ttrss_archived_feeds
4669 WHERE id = ".$line["orig_feed_id"]);
4670
4671 if (db_num_rows($tmp_result) != 0) {
4672
009646d2
AD
4673 $rv['content'] .= "<div clear='both'>";
4674 $rv['content'] .= __("Originally from:");
ef83538d 4675
009646d2 4676 $rv['content'] .= "&nbsp;";
ef83538d
AD
4677
4678 $tmp_line = db_fetch_assoc($tmp_result);
4679
009646d2 4680 $rv['content'] .= "<a target='_blank'
ef83538d
AD
4681 href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
4682 $tmp_line['title'] . "</a>";
4683
009646d2 4684 $rv['content'] .= "&nbsp;";
ef83538d 4685
009646d2
AD
4686 $rv['content'] .= "<a target='_blank' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
4687 $rv['content'] .= "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_set.gif'></a>";
ef83538d 4688
009646d2 4689 $rv['content'] .= "</div>";
ef83538d
AD
4690 }
4691 }
4692
009646d2 4693 $rv['content'] .= "</div>";
3de0261a 4694
009646d2 4695 $rv['content'] .= "<div class=\"postIcon\">" .
8d505d78 4696 "<a target=\"_blank\" title=\"".__("Visit the website")."\"$
f412e5ad
AD
4697 href=\"".htmlspecialchars($feed_site_url)."\">".
4698 $feed_icon . "</a></div>";
64ab16ac 4699
009646d2 4700 $rv['content'] .= "<div id=\"POSTNOTE-$id\">";
c7e51de1 4701 if ($line['note']) {
009646d2 4702 $rv['content'] .= format_article_note($id, $line['note']);
c7e51de1 4703 }
009646d2 4704 $rv['content'] .= "</div>";
c7e51de1 4705
009646d2 4706 $rv['content'] .= "<div class=\"postContent\">";
741b6090
AD
4707
4708 $article_content = sanitize_rss($link, $line["content"], false, false,
4709 $feed_site_url);
4710
009646d2 4711 $rv['content'] .= $article_content;
db54143e 4712
009646d2
AD
4713 $rv['content'] .= format_article_enclosures($link, $id,
4714 $always_display_enclosures, $article_content);
ce53e200 4715
009646d2 4716 $rv['content'] .= "</div>";
dad14b51 4717
009646d2 4718 $rv['content'] .= "</div>";
3de0261a
AD
4719
4720 }
4721
009646d2
AD
4722 if ($zoom_mode) {
4723 $rv['content'] .= "
eedfb635 4724 <div style=\"text-align : center\">
2ae69126
AD
4725 <button onclick=\"return window.close()\">".
4726 __("Close this window")."</button></div>";
009646d2 4727 $rv['content'] .= "</body></html>";
eedfb635 4728 }
3de0261a 4729
009646d2
AD
4730 return $rv;
4731
3de0261a
AD
4732 }
4733
bd202c3f 4734 function format_headlines_list($link, $feed, $subop, $view_mode, $limit, $cat_view,
8d505d78 4735 $next_unread_feed, $offset, $vgr_last_feed = false,
7b4d02a8 4736 $override_order = false) {
3de0261a 4737
52d7e7da
AD
4738 $disable_cache = false;
4739
bd202c3f
AD
4740 $reply = array();
4741
46921916
AD
4742 $timing_info = getmicrotime();
4743
961f4c73
AD
4744 $topmost_article_ids = array();
4745
ac541432
AD
4746 if (!$offset) {
4747 $offset = 0;
4748 }
3de0261a
AD
4749
4750 if ($subop == "undefined") $subop = "";
4751
a9bcfb8f
AD
4752 $subop_split = split(":", $subop);
4753
3de0261a 4754 if ($subop == "CatchupSelected") {
b4e75b2a
AD
4755 $ids = split(",", db_escape_string($_REQUEST["ids"]));
4756 $cmode = sprintf("%d", $_REQUEST["cmode"]);
3de0261a
AD
4757
4758 catchupArticlesById($link, $ids, $cmode);
4759 }
4760
0569a712
AD
4761 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
4762 update_rss_feed($link, $feed, true);
4763 }
4764
3de0261a
AD
4765 if ($subop == "MarkAllRead") {
4766 catchup_feed($link, $feed, $cat_view);
4767
4768 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
4769 if ($next_unread_feed) {
4770 $feed = $next_unread_feed;
4771 }
4772 }
4773 }
4774
a9bcfb8f
AD
4775 if ($subop_split[0] == "MarkAllReadGR") {
4776 catchup_feed($link, $subop_split[1], false);
4777 }
4778
c3fddd05 4779 // FIXME: might break tag display?
a9bcfb8f 4780
8d505d78 4781 if ($feed > 0 && !$cat_view) {
3de0261a
AD
4782 $result = db_query($link,
4783 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
8d505d78 4784
3de0261a 4785 if (db_num_rows($result) == 0) {
bd202c3f 4786 $reply['content'] = "<div align='center'>".__('Feed not found.')."</div>";
3de0261a
AD
4787 }
4788 }
4789
4790 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
10eb9da8 4791
3de0261a
AD
4792 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
4793 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
4794
4795 if (db_num_rows($result) == 1) {
4796 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
4797 } else {
4798 $rtl_content = false;
4799 }
8d505d78 4800
3de0261a
AD
4801 if ($rtl_content) {
4802 $rtl_tag = "dir=\"RTL\"";
4803 } else {
4804 $rtl_tag = "";
4805 }
4806 } else {
4807 $rtl_tag = "";
4808 $rtl_content = false;
4809 }
4810
3de0261a
AD
4811 /// START /////////////////////////////////////////////////////////////////////////////////
4812
c3fddd05 4813 @$search = db_escape_string($_REQUEST["query"]);
52d7e7da 4814
8d505d78 4815 if ($search) {
52d7e7da
AD
4816 $disable_cache = true;
4817 }
4818
c3fddd05
AD
4819 @$search_mode = db_escape_string($_REQUEST["search_mode"]);
4820 @$match_on = db_escape_string($_REQUEST["match_on"]);
3de0261a
AD
4821
4822 if (!$match_on) {
4823 $match_on = "both";
4824 }
4825
4826 $real_offset = $offset * $limit;
4827
b4e75b2a 4828 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
46921916 4829
8d505d78 4830 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
7b4d02a8 4831 $search, $search_mode, $match_on, $override_order, $real_offset);
3de0261a 4832
b4e75b2a 4833 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
46921916 4834
3de0261a
AD
4835 $result = $qfh_ret[0];
4836 $feed_title = $qfh_ret[1];
4837 $feed_site_url = $qfh_ret[2];
4838 $last_error = $qfh_ret[3];
f56e3080 4839
081e527d
AD
4840 $vgroup_last_feed = $vgr_last_feed;
4841
3de0261a
AD
4842 /// STOP //////////////////////////////////////////////////////////////////////////////////
4843
ac541432 4844 if (!$offset) {
3de0261a 4845
6b32516b 4846 if (db_num_rows($result) > 0) {
bd202c3f 4847 $reply['toolbar'] = format_headline_subtoolbar($link, $feed_site_url, $feed_title,
8d505d78 4848 $feed, $cat_view, $search, $match_on, $search_mode, $view_mode,
26a1e185 4849 $last_error);
6b32516b 4850 }
ac541432 4851 }
3de0261a 4852
29dfb258
AD
4853 $headlines_count = db_num_rows($result);
4854
3de0261a
AD
4855 if (db_num_rows($result) > 0) {
4856
4ab4d364
AD
4857 $lnum = $limit*$offset;
4858
3de0261a 4859 $num_unread = 0;
6cfea5c7
AD
4860 $cur_feed_title = '';
4861
784ac51f
AD
4862 $fresh_intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE") * 60 * 60;
4863
3de0261a
AD
4864 while ($line = db_fetch_assoc($result)) {
4865
4866 $class = ($lnum % 2) ? "even" : "odd";
8d505d78 4867
3de0261a
AD
4868 $id = $line["id"];
4869 $feed_id = $line["feed_id"];
961f4c73 4870
e2549229 4871 $labels = get_article_labels($link, $id);
e2549229 4872
2eb9c95c
AD
4873 $labels_str = "<span id=\"HLLCTR-$id\">";
4874 $labels_str .= format_article_labels($labels, $id);
f9247195 4875 $labels_str .= "</span>";
8d505d78 4876
905ff52a 4877 if (count($topmost_article_ids) < 3) {
961f4c73
AD
4878 array_push($topmost_article_ids, $id);
4879 }
4880
784ac51f 4881 if ($line["last_read"] == "" && !sql_bool_to_bool($line["unread"])) {
8d505d78 4882
883fee8d 4883 $update_pic = "<img id='FUPDPIC-$id' src=\"".
8d505d78 4884 theme_image($link, 'images/updated.png')."\"
3de0261a
AD
4885 alt=\"Updated\">";
4886 } else {
8d505d78 4887 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
3de0261a
AD
4888 alt=\"Updated\">";
4889 }
784ac51f 4890
8d505d78 4891 if (sql_bool_to_bool($line["unread"]) &&
784ac51f
AD
4892 time() - strtotime($line["updated_noms"]) < $fresh_intl) {
4893
e9823609
AD
4894 $update_pic = "<img id='FUPDPIC-$id' src=\"".
4895 theme_image($link, 'images/fresh_sign.png')."\" alt=\"Fresh\">";
784ac51f 4896 }
8d505d78 4897
3de0261a 4898 if ($line["unread"] == "t" || $line["unread"] == "1") {
ca8e3d75 4899 $class .= " Unread";
3de0261a
AD
4900 ++$num_unread;
4901 $is_unread = true;
4902 } else {
4903 $is_unread = false;
4904 }
abd8a516 4905
3de0261a 4906 if ($line["marked"] == "t" || $line["marked"] == "1") {
8d505d78
AD
4907 $marked_pic = "<img id=\"FMPIC-$id\"
4908 src=\"".theme_image($link, 'images/mark_set.png')."\"
4909 class=\"markedPic\" alt=\"Unstar article\"
e9823609 4910 onclick='javascript:tMark($id)'>";
3de0261a 4911 } else {
8d505d78
AD
4912 $marked_pic = "<img id=\"FMPIC-$id\"
4913 src=\"".theme_image($link, 'images/mark_unset.png')."\"
4914 class=\"markedPic\" alt=\"Star article\"
e9823609 4915 onclick='javascript:tMark($id)'>";
3de0261a
AD
4916 }
4917
e4f4b46f 4918 if ($line["published"] == "t" || $line["published"] == "1") {
8d505d78
AD
4919 $published_pic = "<img id=\"FPPIC-$id\" src=\"".theme_image($link,
4920 'images/pub_set.png')."\"
e4f4b46f 4921 class=\"markedPic\"
f5e0338d 4922 alt=\"Unpublish article\" onclick='javascript:tPub($id)'>";
e4f4b46f 4923 } else {
e9823609 4924 $published_pic = "<img id=\"FPPIC-$id\" src=\"".theme_image($link,
8d505d78 4925 'images/pub_unset.png')."\"
e4f4b46f 4926 class=\"markedPic\"
f5e0338d 4927 alt=\"Publish article\" onclick='javascript:tPub($id)'>";
e4f4b46f
AD
4928 }
4929
e944346c 4930# $content_link = "<a target=\"_blank\" href=\"".$line["link"]."\">" .
3de0261a
AD
4931# $line["title"] . "</a>";
4932
8d505d78 4933# $content_link = "<a
f0971fc1
AD
4934# href=\"" . htmlspecialchars($line["link"]) . "\"
4935# onclick=\"view($id,$feed_id);\">" .
4936# $line["title"] . "</a>";
3de0261a
AD
4937
4938# $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
4939# $line["title"] . "</a>";
4940
8d505d78 4941 $updated_fmt = make_local_datetime($link, $line["updated_noms"], false);
3de0261a
AD
4942
4943 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
8d505d78 4944 $content_preview = truncate_string(strip_tags($line["content_preview"]),
3de0261a
AD
4945 100);
4946 }
4947
ff6e357a
AD
4948 $score = $line["score"];
4949
8d505d78 4950 $score_pic = theme_image($link,
883fee8d 4951 "images/" . get_score_pic($score));
546499a9 4952
9bf3f101 4953/* $score_title = __("(Click to change)");
8d505d78 4954 $score_pic = "<img class='hlScorePic' src=\"images/$score_pic\"
9bf3f101 4955 onclick=\"adjustArticleScore($id, $score)\" title=\"$score $score_title\">"; */
546499a9 4956
8d505d78 4957 $score_pic = "<img class='hlScorePic' src=\"$score_pic\"
9bf3f101 4958 title=\"$score\">";
ff6e357a 4959
5daa24f2
AD
4960 if ($score > 500) {
4961 $hlc_suffix = "H";
4962 } else if ($score < -100) {
4963 $hlc_suffix = "L";
4964 } else {
4965 $hlc_suffix = "";
4966 }
4967
3de0261a
AD
4968 $entry_author = $line["author"];
4969
4970 if ($entry_author) {
60164936 4971 $entry_author = " - $entry_author";
3de0261a
AD
4972 }
4973
7defa089 4974 $has_feed_icon = feed_has_icon($feed_id);
bd51294a
AD
4975
4976 if ($has_feed_icon) {
4977 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"".ICONS_URL."/$feed_id.ico\" alt=\"\">";
4978 } else {
da4fb53f 4979 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/feed-icon-12x12.png\" alt=\"\">";
bd51294a
AD
4980 }
4981
3de0261a 4982 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
6cfea5c7 4983
d00f22ac 4984 if (get_pref($link, 'VFEED_GROUP_BY_FEED')) {
bb031f91 4985 if ($feed_id != $vgroup_last_feed && $line["feed_title"]) {
a9bcfb8f
AD
4986
4987 $cur_feed_title = $line["feed_title"];
081e527d 4988 $vgroup_last_feed = $feed_id;
a9bcfb8f 4989
962d8ba4 4990 $cur_feed_title = htmlspecialchars($cur_feed_title);
43fc671f 4991
af163b85 4992 $vf_catchup_link = "(<a onclick='javascript:catchupFeedInGroup($feed_id);' href='#'>".__('mark as read')."</a>)";
a9bcfb8f 4993
bd202c3f 4994 $reply['content'] .= "<div class='cdmFeedTitle'>".
dc803347 4995 "<div style=\"float : right\">$feed_icon_img</div>".
4169bb67 4996 "<a href=\"#\" onclick=\"viewfeed($feed_id)\">".
8cc5e965
AD
4997 $line["feed_title"]."</a> $vf_catchup_link</div>";
4998
6cfea5c7
AD
4999 }
5000 }
314fcd2b 5001
8d505d78 5002 $mouseover_attrs = "onmouseover='postMouseIn($id)'
314fcd2b
AD
5003 onmouseout='postMouseOut($id)'";
5004
bd202c3f 5005 $reply['content'] .= "<div class='$class' id='RROW-$id' $mouseover_attrs>";
8cc5e965 5006
bd202c3f 5007 $reply['content'] .= "<div class='hlUpdPic'>$update_pic</div>";
8cc5e965 5008
bd202c3f 5009 $reply['content'] .= "<div class='hlLeft'>";
8cc5e965 5010
bd202c3f 5011 $reply['content'] .= "<input type=\"checkbox\" onclick=\"tSR(this)\"
8cc5e965 5012 id=\"RCHK-$id\">";
8d505d78 5013
bd202c3f
AD
5014 $reply['content'] .= "$marked_pic";
5015 $reply['content'] .= "$published_pic";
3de0261a 5016
bd202c3f 5017 $reply['content'] .= "</div>";
df456bb0 5018
bd202c3f 5019 $reply['content'] .= "<div onclick='return hlClicked(event, $id)'
8cc5e965 5020 class=\"hlTitle\"><span class='hlContent$hlc_suffix'>";
bd202c3f 5021 $reply['content'] .= "<a id=\"RTITLE-$id\"
f0971fc1 5022 href=\"" . htmlspecialchars($line["link"]) . "\"
8cc5e965 5023 onclick=\"return false;\">" .
df456bb0
AD
5024 $line["title"];
5025
5026 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
5027 if ($content_preview) {
bd202c3f 5028 $reply['content'] .= "<span class=\"contentPreview\"> - $content_preview</span>";
3de0261a 5029 }
3de0261a 5030 }
df456bb0 5031
bd202c3f 5032 $reply['content'] .= "</a></span>";
df456bb0 5033
bd202c3f 5034 $reply['content'] .= $labels_str;
e2549229 5035
af32a59a 5036 /* if (!get_pref($link, 'VFEED_GROUP_BY_FEED')) {
8d505d78 5037 if (@$line["feed_title"]) {
6cfea5c7 5038 print "<span class=\"hlFeed\">
4169bb67 5039 (<a href=\"#\" onclick=\"viewfeed($feed_id)\">".
6cfea5c7
AD
5040 $line["feed_title"]."</a>)
5041 </span>";
5042 }
af32a59a 5043 } */
6e35a862 5044
bd202c3f 5045 $reply['content'] .= "</div>";
7b5e74c7 5046
bd202c3f
AD
5047 $reply['content'] .= "<div class=\"hlRight\">";
5048 $reply['content'] .= "<span class=\"hlUpdated\">$updated_fmt</span>";
5049 $reply['content'] .= $score_pic;
546499a9 5050
8cc5e965 5051 if ($line["feed_title"] && !get_pref($link, 'VFEED_GROUP_BY_FEED')) {
bd51294a 5052
bd202c3f 5053 $reply['content'] .= "<span onclick=\"viewfeed($feed_id)\"
8cc5e965
AD
5054 title=\"".htmlspecialchars($line['feed_title'])."\">
5055 $feed_icon_img<span>";
bd51294a
AD
5056 }
5057
bd202c3f
AD
5058 $reply['content'] .= "</div>";
5059 $reply['content'] .= "</div>";
3de0261a
AD
5060
5061 } else {
6cfea5c7 5062
bb031f91 5063 if (get_pref($link, 'VFEED_GROUP_BY_FEED') && $line["feed_title"]) {
081e527d
AD
5064 if ($feed_id != $vgroup_last_feed) {
5065
5066 $cur_feed_title = $line["feed_title"];
5067 $vgroup_last_feed = $feed_id;
5068
962d8ba4
AD
5069 $cur_feed_title = htmlspecialchars($cur_feed_title);
5070
af163b85 5071 $vf_catchup_link = "(<a onclick='javascript:catchupFeedInGroup($feed_id);' href='#'>".__('mark as read')."</a>)";
081e527d 5072
7defa089 5073 $has_feed_icon = feed_has_icon($feed_id);
dc803347
AD
5074
5075 if ($has_feed_icon) {
5076 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"".ICONS_URL."/$feed_id.ico\" alt=\"\">";
5077 } else {
5078 //$feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\" alt=\"\">";
5079 }
5080
bd202c3f 5081 $reply['content'] .= "<div class='cdmFeedTitle'>".
dc803347 5082 "<div style=\"float : right\">$feed_icon_img</div>".
4169bb67 5083 "<a href=\"#\" onclick=\"viewfeed($feed_id)\">".
081e527d 5084 $line["feed_title"]."</a> $vf_catchup_link</div>";
6cfea5c7
AD
5085 }
5086 }
5087
0cacc891 5088 $expand_cdm = get_pref($link, 'CDM_EXPANDED');
0cacc891 5089
8d505d78 5090 $mouseover_attrs = "onmouseover='postMouseIn($id)'
314fcd2b
AD
5091 onmouseout='postMouseOut($id)'";
5092
bd202c3f 5093 $reply['content'] .= "<div class=\"$class\"
dd4c8697 5094 id=\"RROW-$id\" $mouseover_attrs'>";
3de0261a 5095
bd202c3f 5096 $reply['content'] .= "<div class=\"cdmHeader\">";
3de0261a 5097
bd202c3f
AD
5098 $reply['content'] .= "<div style='float : right'>";
5099 $reply['content'] .= "<span class='updated'>$updated_fmt</span>";
5100 $reply['content'] .= "$score_pic";
965fb2af 5101
dad14b51 5102 if (!get_pref($link, "VFEED_GROUP_BY_FEED") && $line["feed_title"]) {
bd202c3f 5103 $reply['content'] .= "<span style=\"cursor : pointer\"
dad14b51
AD
5104 title=\"".htmlspecialchars($line["feed_title"])."\"
5105 onclick=\"viewfeed($feed_id)\">$feed_icon_img</span>";
5106 }
bd202c3f 5107 $reply['content'] .= "<div class=\"updPic\">$update_pic</div>";
dd1c0680 5108
bd202c3f 5109 $reply['content'] .= "</div>";
8d505d78 5110
bd202c3f 5111 $reply['content'] .= "<input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
ca8e3d75 5112 'RROW-$id')\" id=\"RCHK-$id\"/>";
5daa24f2 5113
bd202c3f
AD
5114 $reply['content'] .= "$marked_pic";
5115 $reply['content'] .= "$published_pic";
a7f003e0 5116
bd202c3f 5117 $reply['content'] .= "<span id=\"RTITLE-$id\"
dd4c8697 5118 onclick=\"return cdmClicked(event, $id);\"
dad14b51
AD
5119 class=\"titleWrap$hlc_suffix\">
5120 <a class=\"title\"
a64029e5 5121 title=\"".htmlspecialchars($line['title'])."\"
96d44601 5122 target=\"_blank\" href=\"".
a64029e5 5123 htmlspecialchars($line["link"])."\">".
8d505d78 5124 truncate_string($line["title"], 100) .
2547c9ec 5125 " $entry_author</a>";
0cacc891 5126
bd202c3f 5127 $reply['content'] .= $labels_str;
0cacc891 5128
02ef7e02 5129 if (!$expand_cdm)
dad14b51
AD
5130 $content_hidden = "style=\"display : none\"";
5131 else
5132 $excerpt_hidden = "style=\"display : none\"";
3de0261a 5133
bd202c3f 5134 $reply['content'] .= "<span $excerpt_hidden
dad14b51 5135 id=\"CEXC-$id\" class=\"cdmExcerpt\"> - $content_preview</span>";
3de0261a 5136
bd202c3f 5137 $reply['content'] .= "</span>";
dd4c8697 5138
bd202c3f 5139 $reply['content'] .= "</div>";
c9efd838 5140
bd202c3f 5141 $reply['content'] .= "<div class=\"cdmContent\" $content_hidden
dd4c8697 5142 onclick=\"return cdmClicked(event, $id);\"
dad14b51 5143 id=\"CICD-$id\">";
c9efd838 5144
bd202c3f 5145 $reply['content'] .= "<div class=\"cdmContentInner\">";
a04c8e8d 5146
dad14b51 5147 if ($line["orig_feed_id"]) {
494a64ea 5148
dad14b51
AD
5149 $tmp_result = db_query($link, "SELECT * FROM ttrss_archived_feeds
5150 WHERE id = ".$line["orig_feed_id"]);
494a64ea
AD
5151
5152 if (db_num_rows($tmp_result) != 0) {
8d505d78 5153
bd202c3f
AD
5154 $reply['content'] .= "<div clear='both'>";
5155 $reply['content'] .= __("Originally from:");
8d505d78 5156
bd202c3f 5157 $reply['content'] .= "&nbsp;";
8d505d78 5158
dad14b51 5159 $tmp_line = db_fetch_assoc($tmp_result);
8d505d78 5160
bd202c3f 5161 $reply['content'] .= "<a target='_blank'
dad14b51
AD
5162 href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
5163 $tmp_line['title'] . "</a>";
8d505d78 5164
bd202c3f 5165 $reply['content'] .= "&nbsp;";
8d505d78 5166
bd202c3f
AD
5167 $reply['content'] .= "<a target='_blank' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
5168 $reply['content'] .= "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_set.gif'></a>";
8d505d78 5169
bd202c3f 5170 $reply['content'] .= "</div>";
dad14b51 5171 }
494a64ea 5172 }
494a64ea 5173
9d3c031d
AD
5174 // FIXME: make this less of a hack
5175
5176 $feed_site_url = false;
5177
5178 if ($line["feed_id"]) {
5179 $tmp_result = db_query($link, "SELECT site_url FROM ttrss_feeds
5180 WHERE id = " . $line["feed_id"]);
5181
5182 if (db_num_rows($tmp_result) == 1) {
5183 $feed_site_url = db_fetch_result($tmp_result, 0, "site_url");
5184 }
5185 }
5186
dd1c0680 5187 if ($expand_cdm) {
8d505d78 5188 $article_content = sanitize_rss($link, $line["content_preview"],
62b800b4 5189 false, false, $feed_site_url);
621ffb00 5190
62b800b4 5191 if (!$article_content) $article_content = "&nbsp;";
dd1c0680
AD
5192 } else {
5193 $article_content = '';
5194 }
1ede5814 5195
bd202c3f 5196 $reply['content'] .= "<div id=\"POSTNOTE-$id\">";
c7e51de1 5197 if ($line['note']) {
bd202c3f 5198 $reply['content'] .= format_article_note($id, $line['note']);
c7e51de1 5199 }
bd202c3f 5200 $reply['content'] .= "</div>";
c7e51de1 5201
bd202c3f 5202 $reply['content'] .= "<span id=\"CWRAP-$id\">$article_content</span>";
a3eeb471 5203
dad14b51
AD
5204 $tmp_result = db_query($link, "SELECT always_display_enclosures FROM
5205 ttrss_feeds WHERE id = ".
8d505d78 5206 (($line['feed_id'] == null) ? $line['orig_feed_id'] :
a16a62c0 5207 $line['feed_id'])." AND owner_uid = ".$_SESSION["uid"]);
54e61a68 5208
8d505d78 5209 $always_display_enclosures = sql_bool_to_bool(db_fetch_result($tmp_result,
dad14b51 5210 0, "always_display_enclosures"));
a04c8e8d 5211
bd202c3f 5212 $reply['content'] .= format_article_enclosures($link, $id, $always_display_enclosures,
dad14b51 5213 $article_content);
a04c8e8d 5214
bd202c3f 5215 $reply['content'] .= "</div>";
3de0261a 5216
bd202c3f 5217 $reply['content'] .= "<div class=\"cdmFooter\">";
3de0261a 5218
dad14b51
AD
5219 $tags_str = format_tags_string(get_article_tags($link, $id), $id);
5220
bd202c3f 5221 $reply['content'] .= "<img src='".theme_image($link,
dad14b51
AD
5222 'images/tag.png')."' alt='Tags' title='Tags'>
5223 <span id=\"ATSTR-$id\">$tags_str</span>
8d505d78 5224 <a title=\"".__('Edit tags for this article')."\"
4169bb67 5225 href=\"#\" onclick=\"editArticleTags($id, $feed_id, true)\">(+)</a>";
3de0261a 5226
bd202c3f 5227 $reply['content'] .= "<div style=\"float : right\">";
3de0261a 5228
bd202c3f 5229 $reply['content'] .= "<img src=\"images/art-zoom.png\"
8a6702ad 5230 onclick=\"zoomToArticle(event, $id)\"
eedfb635 5231 style=\"cursor : pointer\"
8d505d78 5232 alt='Zoom'
6f3976c9 5233 title='".__('Open article in new tab')."'>";
dad14b51 5234
741b6090 5235 //$note_escaped = htmlspecialchars($line['note'], ENT_QUOTES);
411fe209 5236
bd202c3f 5237 $reply['content'] .= "<img src=\"images/art-pub-note.png\"
411fe209 5238 style=\"cursor : pointer\" style=\"cursor : pointer\"
741b6090
AD
5239 onclick=\"editArticleNote($id)\"
5240 alt='PubNote' title='".__('Edit article note')."'>";
411fe209 5241
dad14b51 5242 if (DIGEST_ENABLE) {
bd202c3f 5243 $reply['content'] .= "<img src=\"".theme_image($link, 'images/art-email.png')."\"
dad14b51
AD
5244 style=\"cursor : pointer\"
5245 onclick=\"emailArticle($id)\"
5246 alt='Zoom' title='".__('Forward by email')."'>";
5247 }
c7e51de1 5248
411fe209 5249 if (ENABLE_TWEET_BUTTON) {
bd202c3f 5250 $reply['content'] .= "<img src=\"".theme_image($link, 'images/art-tweet.png')."\"
411fe209
AD
5251 class='tagsPic' style=\"cursor : pointer\"
5252 onclick=\"tweetArticle($id)\"
5253 alt='Zoom' title='".__('Share on Twitter')."'>";
5254 }
c7e51de1 5255
bd202c3f 5256 $reply['content'] .= "<img src=\"images/digest_checkbox.png\"
dad14b51 5257 style=\"cursor : pointer\" style=\"cursor : pointer\"
1ede5814 5258 onclick=\"dismissArticle($id)\"
dad14b51 5259 alt='Dismiss' title='".__('Dismiss article')."'>";
3de0261a 5260
bd202c3f
AD
5261 $reply['content'] .= "</div>";
5262 $reply['content'] .= "</div>";
3de0261a 5263
bd202c3f 5264 $reply['content'] .= "</div>";
3de0261a 5265
bd202c3f 5266 $reply['content'] .= "</div>";
3de0261a 5267
8d505d78
AD
5268 }
5269
3de0261a 5270 ++$lnum;
8d505d78 5271 }
3de0261a 5272
3de0261a 5273 } else {
93c841c4
AD
5274 $message = "";
5275
5276 switch ($view_mode) {
5277 case "unread":
5278 $message = __("No unread articles found to display.");
5279 break;
8b09eac8
AD
5280 case "updated":
5281 $message = __("No updated articles found to display.");
5282 break;
93c841c4
AD
5283 case "marked":
5284 $message = __("No starred articles found to display.");
5285 break;
5286 default:
215af892
AD
5287 if ($feed < -10) {
5288 $message = __("No articles found to display. You can assign articles to labels manually (see the Actions menu above) or use a filter.");
5289 } else {
5290 $message = __("No articles found to display.");
5291 }
93c841c4
AD
5292 }
5293
8f7c631e 5294 if (!$offset && $message) {
bd202c3f 5295 $reply['content'] .= "<div class='whiteBox'>$message";
8f7c631e 5296
bd202c3f 5297 $reply['content'] .= "<p class=\"small\"><span class=\"insensitive\">";
8f7c631e
AD
5298
5299 $result = db_query($link, "SELECT ".SUBSTRING_FOR_DATE."(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds
5300 WHERE owner_uid = " . $_SESSION['uid']);
8d505d78 5301
8f7c631e
AD
5302 $last_updated = db_fetch_result($result, 0, "last_updated");
5303 $last_updated = make_local_datetime($link, $last_updated, false);
8d505d78 5304
bd202c3f 5305 $reply['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);
8d505d78 5306
8f7c631e
AD
5307 $result = db_query($link, "SELECT COUNT(id) AS num_errors
5308 FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
8d505d78 5309
8f7c631e 5310 $num_errors = db_fetch_result($result, 0, "num_errors");
8d505d78 5311
8f7c631e 5312 if ($num_errors > 0) {
bd202c3f
AD
5313 $reply['content'] .= "<br/>";
5314 $reply['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".
8f7c631e
AD
5315 __('Some feeds have update errors (click for details)')."</a>";
5316 }
bd202c3f 5317 $reply['content'] .= "</span></p></div>";
8f7c631e 5318 }
3de0261a
AD
5319 }
5320
6e4f4ce1
AD
5321# if (!$offset) {
5322# if ($headlines_count > 0) print "</div>";
5323# print "</div>";
5324# }
5325
bd202c3f 5326 #print "]]></content>";
3de0261a 5327
bd202c3f
AD
5328 return array($topmost_article_ids, $headlines_count, $feed, $disable_cache,
5329 $vgroup_last_feed, $reply['content'], $reply['toolbar']);
3de0261a 5330 }
0979b696
AD
5331
5332// from here: http://www.roscripts.com/Create_tag_cloud-71.html
5333
5334 function printTagCloud($link) {
35a03bdd 5335
8d505d78
AD
5336 $query = "SELECT tag_name, COUNT(post_int_id) AS count
5337 FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]."
b31af972 5338 GROUP BY tag_name ORDER BY count DESC LIMIT 50";
0979b696
AD
5339
5340 $result = db_query($link, $query);
5341
5342 $tags = array();
5343
5344 while ($line = db_fetch_assoc($result)) {
5345 $tags[$line["tag_name"]] = $line["count"];
5346 }
5347
5348 ksort($tags);
5349
5350 $max_size = 32; // max font size in pixels
4548a580 5351 $min_size = 11; // min font size in pixels
8d505d78 5352
0979b696
AD
5353 // largest and smallest array values
5354 $max_qty = max(array_values($tags));
5355 $min_qty = min(array_values($tags));
8d505d78 5356
0979b696
AD
5357 // find the range of values
5358 $spread = $max_qty - $min_qty;
5359 if ($spread == 0) { // we don't want to divide by zero
5360 $spread = 1;
5361 }
8d505d78 5362
0979b696
AD
5363 // set the font-size increment
5364 $step = ($max_size - $min_size) / ($spread);
8d505d78 5365
0979b696
AD
5366 // loop through the tag array
5367 foreach ($tags as $key => $value) {
5368 // calculate font-size
5369 // find the $value in excess of $min_qty
5370 // multiply by the font-size increment ($size)
5371 // and add the $min_size set above
5372 $size = round($min_size + (($value - $min_qty) * $step));
546ffab4
AD
5373
5374 $key_escaped = str_replace("'", "\\'", $key);
5375
8d505d78
AD
5376 echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " .
5377 $size . "px\" title=\"$value articles tagged with " .
0979b696
AD
5378 $key . '">' . $key . '</a> ';
5379 }
5380 }
46921916
AD
5381
5382 function print_checkpoint($n, $s) {
8d505d78 5383 $ts = getmicrotime();
46921916
AD
5384 echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s);
5385 return $ts;
5386 }
14b6c54b
AD
5387
5388 function sanitize_tag($tag) {
5389 $tag = trim($tag);
5390
5391 $tag = mb_strtolower($tag, 'utf-8');
5392
8d505d78 5393 $tag = preg_replace('/[\"\+\>\<]/', "", $tag);
0c3d1c68 5394
8d505d78
AD
5395// $tag = str_replace('"', "", $tag);
5396// $tag = str_replace("+", " ", $tag);
14b6c54b
AD
5397 $tag = str_replace("technorati tag: ", "", $tag);
5398
5399 return $tag;
5400 }
e4f4b46f 5401
8801fb01 5402 function get_self_url_prefix() {
f56e3080 5403
e2749781 5404 $url_path = "";
8d505d78 5405
e2749781
AD
5406 if ($_SERVER['HTTPS'] != "on") {
5407 $url_path = "http://";
5408 } else {
5409 $url_path = "https://";
5410 }
f56e3080 5411
e2749781 5412 $url_path .= $_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
f56e3080
AD
5413
5414 return $url_path;
e0dc56d4 5415
8801fb01
AD
5416 }
5417 function opml_publish_url($link){
e0dc56d4 5418
8801fb01 5419 $url_path = get_self_url_prefix();
8d505d78 5420 $url_path .= "/opml.php?op=publish&key=" .
2e7f046f 5421 get_feed_access_key($link, 'OPML:Publish', false, $_SESSION["uid"]);
e0dc56d4
MK
5422
5423 return $url_path;
5424 }
f56e3080 5425
45004d43
AD
5426 /**
5427 * Purge a feed contents, marked articles excepted.
8d505d78 5428 *
45004d43
AD
5429 * @param mixed $link The database connection.
5430 * @param integer $id The id of the feed to purge.
5431 * @return void
5432 */
d1f0c584 5433 function clear_feed_articles($link, $id) {
e04c18a2
AD
5434
5435 if ($id != 0) {
5436 $result = db_query($link, "DELETE FROM ttrss_user_entries
a8ae1b9a 5437 WHERE feed_id = '$id' AND marked = false AND owner_uid = " . $_SESSION["uid"]);
e04c18a2
AD
5438 } else {
5439 $result = db_query($link, "DELETE FROM ttrss_user_entries
5440 WHERE feed_id IS NULL AND marked = false AND owner_uid = " . $_SESSION["uid"]);
5441 }
d1f0c584 5442
8d505d78 5443 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
d1f0c584 5444 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
ced46404
AD
5445
5446 ccache_update($link, $id, $_SESSION['uid']);
45004d43
AD
5447 } // function clear_feed_articles
5448
5449 /**
5450 * Compute the Mozilla Firefox feed adding URL from server HOST and REQUEST_URI.
5451 *
5452 * @return string The Mozilla Firefox feed adding URL.
5453 */
5454 function add_feed_url() {
d70c5ae4 5455 $url_path = ($_SERVER['HTTPS'] != "on" ? 'http://' : 'https://') . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
755a43ee
AD
5456 $url_path .= "?op=pref-feeds&quiet=1&subop=add&feed_url=%s";
5457 return $url_path;
45004d43
AD
5458 } // function add_feed_url
5459
5460 /**
5461 * Encrypt a password in SHA1.
8d505d78 5462 *
45004d43
AD
5463 * @param string $pass The password to encrypt.
5464 * @param string $login A optionnal login.
5465 * @return string The encrypted password.
5466 */
1a9f4d3c
AD
5467 function encrypt_password($pass, $login = '') {
5468 if ($login) {
5469 return "SHA1X:" . sha1("$login:$pass");
5470 } else {
5471 return "SHA1:" . sha1($pass);
5472 }
45004d43
AD
5473 } // function encrypt_password
5474
5475 /**
5476 * Update a feed batch.
5477 * Used by daemons to update n feeds by run.
5478 * Only update feed needing a update, and not being processed
5479 * by another process.
8d505d78 5480 *
45004d43
AD
5481 * @param mixed $link Database link
5482 * @param integer $limit Maximum number of feeds in update batch. Default to DAEMON_FEED_LIMIT.
5483 * @param boolean $from_http Set to true if you call this function from http to disable cli specific code.
5484 * @param boolean $debug Set to false to disable debug output. Default to true.
5485 * @return void
5486 */
5487 function update_daemon_common($link, $limit = DAEMON_FEED_LIMIT, $from_http = false, $debug = true) {
5488 // Process all other feeds using last_updated and interval parameters
5489
5490 // Test if the user has loggued in recently. If not, it does not update its feeds.
5491 if (DAEMON_UPDATE_LOGIN_LIMIT > 0) {
5492 if (DB_TYPE == "pgsql") {
5493 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'";
5494 } else {
5495 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)";
8d505d78 5496 }
45004d43
AD
5497 } else {
5498 $login_thresh_qpart = "";
5499 }
5500
5501 // Test if the feed need a update (update interval exceded).
5502 if (DB_TYPE == "pgsql") {
5503 $update_limit_qpart = "AND ((
5504 ttrss_feeds.update_interval = 0
5505 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
5506 ) OR (
5507 ttrss_feeds.update_interval > 0
5508 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
da4caf5d 5509 ) OR ttrss_feeds.last_updated IS NULL)";
45004d43
AD
5510 } else {
5511 $update_limit_qpart = "AND ((
5512 ttrss_feeds.update_interval = 0
5513 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
5514 ) OR (
5515 ttrss_feeds.update_interval > 0
5516 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
da4caf5d 5517 ) OR ttrss_feeds.last_updated IS NULL)";
45004d43
AD
5518 }
5519
5520 // Test if feed is currently being updated by another process.
5521 if (DB_TYPE == "pgsql") {
5522 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')";
5523 } else {
5524 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))";
5525 }
5526
5527 // Test if there is a limit to number of updated feeds
5528 $query_limit = "";
5529 if($limit) $query_limit = sprintf("LIMIT %d", $limit);
5530
51b8c957
AD
5531 $random_qpart = sql_random_function();
5532
45004d43
AD
5533 // We search for feed needing update.
5534 $result = db_query($link, "SELECT ttrss_feeds.feed_url,ttrss_feeds.id, ttrss_feeds.owner_uid,
fc2b26a6 5535 ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated,
8d505d78
AD
5536 ttrss_feeds.update_interval
5537 FROM
45004d43
AD
5538 ttrss_feeds, ttrss_users, ttrss_user_prefs
5539 WHERE
5540 ttrss_feeds.owner_uid = ttrss_users.id
5541 AND ttrss_users.id = ttrss_user_prefs.owner_uid
5542 AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
5543 $login_thresh_qpart $update_limit_qpart
51b8c957
AD
5544 $updstart_thresh_qpart
5545 ORDER BY $random_qpart $query_limit");
45004d43
AD
5546
5547 $user_prefs_cache = array();
5548
5549 if($debug) _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
5550
5551 // Here is a little cache magic in order to minimize risk of double feed updates.
5552 $feeds_to_update = array();
5553 while ($line = db_fetch_assoc($result)) {
5554 $feeds_to_update[$line['id']] = $line;
5555 }
5556
5557 // We update the feed last update started date before anything else.
5558 // There is no lag due to feed contents downloads
5559 // It prevent an other process to update the same feed.
5560 $feed_ids = array_keys($feeds_to_update);
5561 if($feed_ids) {
5562 db_query($link, sprintf("UPDATE ttrss_feeds SET last_update_started = NOW()
5563 WHERE id IN (%s)", implode(',', $feed_ids)));
5564 }
5565
5566 // For each feed, we call the feed update function.
5567 while ($line = array_pop($feeds_to_update)) {
5568
5569 if($debug) _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]);
5570
cce9822a
AD
5571 update_rss_feed($link, $line["id"], true);
5572
45004d43
AD
5573 sleep(1); // prevent flood (FIXME make this an option?)
5574 }
5575
0e0dd486
AD
5576 // Send feed digests by email if needed.
5577 if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link);
5578
45004d43 5579 } // function update_daemon_common
1a9f4d3c 5580
621ffb00
AD
5581 function sanitize_article_content($text) {
5582 # we don't support CDATA sections in articles, they break our own escaping
5583 $text = preg_replace("/\[\[CDATA/", "", $text);
5584 $text = preg_replace("/\]\]\>/", "", $text);
5585 return $text;
5586 }
fee840fb
AD
5587
5588 function load_filters($link, $feed, $owner_uid, $action_id = false) {
5589 $filters = array();
5590
b8ffa322 5591 global $memcache;
fee840fb 5592
1f011328
AD
5593 $obj_id = md5("FILTER:$feed:$owner_uid:$action_id");
5594
b8ffa322
AD
5595 if ($memcache && $obj = $memcache->get($obj_id)) {
5596
b8ffa322
AD
5597 return $obj;
5598
5599 } else {
fee840fb 5600
b8ffa322 5601 if ($action_id) $ftype_query_part = "action_id = '$action_id' AND";
8d505d78 5602
b8ffa322
AD
5603 $result = db_query($link, "SELECT reg_exp,
5604 ttrss_filter_types.name AS name,
5605 ttrss_filter_actions.name AS action,
5606 inverse,
5607 action_param,
5608 filter_param
8d505d78 5609 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
b8ffa322
AD
5610 enabled = true AND
5611 $ftype_query_part
5612 owner_uid = $owner_uid AND
5613 ttrss_filter_types.id = filter_type AND
5614 ttrss_filter_actions.id = action_id AND
5615 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
8d505d78 5616
b8ffa322
AD
5617 while ($line = db_fetch_assoc($result)) {
5618 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
5619 $filter["reg_exp"] = $line["reg_exp"];
5620 $filter["action"] = $line["action"];
5621 $filter["action_param"] = $line["action_param"];
5622 $filter["filter_param"] = $line["filter_param"];
5623 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
8d505d78 5624
b8ffa322
AD
5625 array_push($filters[$line["name"]], $filter);
5626 }
5627
5628 if ($memcache) $memcache->add($obj_id, $filters, 0, 3600*8);
5629
5630 return $filters;
5631 }
fee840fb 5632 }
1e36af0c
AD
5633
5634 function get_score_pic($score) {
8d505d78
AD
5635 if ($score > 100) {
5636 return "score_high.png";
5637 } else if ($score > 0) {
883fee8d 5638 return "score_half_high.png";
1cce3aca 5639 } else if ($score < -100) {
883fee8d 5640 return "score_low.png";
1cce3aca 5641 } else if ($score < 0) {
883fee8d 5642 return "score_half_low.png";
8d505d78 5643 } else {
883fee8d 5644 return "score_neutral.png";
1e36af0c
AD
5645 }
5646 }
ec92c9d1 5647
0745839a 5648 function rounded_table_start($classname, $header = "&nbsp;") {
ec92c9d1 5649 print "<table width='100%' class='$classname' cellspacing='0' cellpadding='0'>";
74d5c8fa 5650 print "<tr><td class='c1'>&nbsp;</td><td class='top'>$header</td><td class='c2'>&nbsp;</td></tr>";
ec92c9d1
AD
5651 print "<tr><td class='left'>&nbsp;</td><td class='content'>";
5652 }
5653
0745839a 5654 function rounded_table_end($footer = "&nbsp;") {
ec92c9d1 5655 print "</td><td class='right'>&nbsp;</td></tr>";
74d5c8fa 5656 print "<tr><td class='c4'>&nbsp;</td><td class='bottom'>$footer</td><td class='c3'>&nbsp;</td></tr>";
ec92c9d1
AD
5657 print "</table>";
5658 }
5659
7defa089
AD
5660 function feed_has_icon($id) {
5661 return is_file(ICONS_DIR . "/$id.ico") && filesize(ICONS_DIR . "/$id.ico") > 0;
5662 }
f29ba148
AD
5663
5664 function init_connection($link) {
5665 if (DB_TYPE == "pgsql") {
6fc720fd 5666 pg_query($link, "set client_encoding = 'UTF-8'");
f29ba148 5667 pg_set_client_encoding("UNICODE");
045d0ab8 5668 pg_query($link, "set datestyle = 'ISO, european'");
324944f3 5669 pg_query($link, "set TIME ZONE 0");
f29ba148 5670 } else {
324944f3
AD
5671 db_query($link, "SET time_zone = '+0:0'");
5672
f29ba148
AD
5673 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
5674 db_query($link, "SET NAMES " . MYSQL_CHARSET);
5675 // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET);
5676 }
5677 }
5678 }
5e96ca9d
AD
5679
5680 function update_feedbrowser_cache($link) {
5681
931dcbc1 5682 $result = db_query($link, "SELECT feed_url,title, COUNT(id) AS subscribers
8d505d78
AD
5683 FROM ttrss_feeds WHERE (SELECT COUNT(id) = 0 FROM ttrss_feeds AS tf
5684 WHERE tf.feed_url = ttrss_feeds.feed_url
5685 AND (private IS true OR feed_url LIKE '%:%@%/%'))
d460f7aa 5686 GROUP BY feed_url, title ORDER BY subscribers DESC LIMIT 1000");
8d505d78 5687
5e96ca9d 5688 db_query($link, "BEGIN");
8d505d78 5689
5e96ca9d 5690 db_query($link, "DELETE FROM ttrss_feedbrowser_cache");
8d505d78 5691
5e96ca9d 5692 $count = 0;
8d505d78 5693
5e96ca9d
AD
5694 while ($line = db_fetch_assoc($result)) {
5695 $subscribers = db_escape_string($line["subscribers"]);
5696 $feed_url = db_escape_string($line["feed_url"]);
931dcbc1
AD
5697 $title = db_escape_string($line["title"]);
5698
5699 $tmp_result = db_query($link, "SELECT subscribers FROM
5700 ttrss_feedbrowser_cache WHERE feed_url = '$feed_url'");
5701
5702 if (db_num_rows($tmp_result) == 0) {
5703
8d505d78
AD
5704 db_query($link, "INSERT INTO ttrss_feedbrowser_cache
5705 (feed_url, title, subscribers) VALUES ('$feed_url',
931dcbc1
AD
5706 '$title', '$subscribers')");
5707
5708 ++$count;
5709
5710 }
8d505d78 5711
5e96ca9d 5712 }
8d505d78 5713
5e96ca9d
AD
5714 db_query($link, "COMMIT");
5715
5716 return $count;
5717
5718 }
2627f2d0 5719
8a4c759e 5720 function ccache_zero($link, $feed_id, $owner_uid) {
2627f2d0
AD
5721 db_query($link, "UPDATE ttrss_counters_cache SET
5722 value = 0, updated = NOW() WHERE
5723 feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
8a4c759e 5724 }
2627f2d0 5725
ad0056a8
AD
5726 function ccache_zero_all($link, $owner_uid) {
5727 db_query($link, "UPDATE ttrss_counters_cache SET
5728 value = 0 WHERE owner_uid = '$owner_uid'");
5729
5730 db_query($link, "UPDATE ttrss_cat_counters_cache SET
5731 value = 0 WHERE owner_uid = '$owner_uid'");
5732 }
5733
c7e51de1
AD
5734 function ccache_remove($link, $feed_id, $owner_uid, $is_cat = false) {
5735
5736 if (!$is_cat) {
5737 $table = "ttrss_counters_cache";
5738 } else {
5739 $table = "ttrss_cat_counters_cache";
5740 }
5741
5742 db_query($link, "DELETE FROM $table WHERE
5743 feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
5744
5745 }
5746
5f4f7adf
AD
5747 function ccache_update_all($link, $owner_uid) {
5748
5749 if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid)) {
5750
5751 $result = db_query($link, "SELECT feed_id FROM ttrss_cat_counters_cache
5752 WHERE feed_id > 0 AND owner_uid = '$owner_uid'");
5753
5754 while ($line = db_fetch_assoc($result)) {
5755 ccache_update($link, $line["feed_id"], $owner_uid, true);
5756 }
5757
c5ffeb61
AD
5758 /* We have to manually include category 0 */
5759
5760 ccache_update($link, 0, $owner_uid, true);
5761
8a4c759e 5762 } else {
5f4f7adf
AD
5763 $result = db_query($link, "SELECT feed_id FROM ttrss_counters_cache
5764 WHERE feed_id > 0 AND owner_uid = '$owner_uid'");
8a4c759e 5765
5f4f7adf
AD
5766 while ($line = db_fetch_assoc($result)) {
5767 print ccache_update($link, $line["feed_id"], $owner_uid);
5768
5769 }
5770
5771 }
5772 }
2627f2d0 5773
8d505d78 5774 function ccache_find($link, $feed_id, $owner_uid, $is_cat = false,
6b49a3dd 5775 $no_update = false) {
8a4c759e 5776
5c432ba4
AD
5777 if (!is_numeric($feed_id)) return;
5778
8a4c759e
AD
5779 if (!$is_cat) {
5780 $table = "ttrss_counters_cache";
32d2181b 5781 if ($feed_id > 0) {
8d505d78 5782 $tmp_result = db_query($link, "SELECT owner_uid FROM ttrss_feeds
32d2181b
AD
5783 WHERE id = '$feed_id'");
5784 $owner_uid = db_fetch_result($tmp_result, 0, "owner_uid");
5785 }
8a4c759e
AD
5786 } else {
5787 $table = "ttrss_cat_counters_cache";
5788 }
5789
5790 if (DB_TYPE == "pgsql") {
5791 $date_qpart = "updated > NOW() - INTERVAL '15 minutes'";
5792 } else if (DB_TYPE == "mysql") {
5793 $date_qpart = "updated > DATE_SUB(NOW(), INTERVAL 15 MINUTE)";
5794 }
5795
5796 $result = db_query($link, "SELECT value FROM $table
8d505d78 5797 WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id'
37fb651d 5798 LIMIT 1");
2627f2d0
AD
5799
5800 if (db_num_rows($result) == 1) {
5801 return db_fetch_result($result, 0, "value");
5802 } else {
6b49a3dd
AD
5803 if ($no_update) {
5804 return -1;
5805 } else {
5806 return ccache_update($link, $feed_id, $owner_uid, $is_cat);
5807 }
2627f2d0
AD
5808 }
5809
5810 }
5811
8d505d78 5812 function ccache_update($link, $feed_id, $owner_uid, $is_cat = false,
6b49a3dd
AD
5813 $update_pcat = true) {
5814
5c432ba4
AD
5815 if (!is_numeric($feed_id)) return;
5816
32d2181b 5817 if (!$is_cat && $feed_id > 0) {
8d505d78 5818 $tmp_result = db_query($link, "SELECT owner_uid FROM ttrss_feeds
2e93b64c
AD
5819 WHERE id = '$feed_id'");
5820 $owner_uid = db_fetch_result($tmp_result, 0, "owner_uid");
5821 }
5822
43ead405
AD
5823 $prev_unread = ccache_find($link, $feed_id, $owner_uid, $is_cat, true);
5824
5825 /* When updating a label, all we need to do is recalculate feed counters
5826 * because labels are not cached */
c98e43db
AD
5827
5828 if ($feed_id < 0) {
43ead405
AD
5829 ccache_update_all($link, $owner_uid);
5830 return;
c98e43db
AD
5831 }
5832
8a4c759e
AD
5833 if (!$is_cat) {
5834 $table = "ttrss_counters_cache";
5835 } else {
5836 $table = "ttrss_cat_counters_cache";
5837 }
5838
b6d486a3 5839 if ($is_cat && $feed_id >= 0) {
37fb651d
AD
5840 if ($feed_id != 0) {
5841 $cat_qpart = "cat_id = '$feed_id'";
5842 } else {
5843 $cat_qpart = "cat_id IS NULL";
5844 }
5845
6b49a3dd
AD
5846 /* Recalculate counters for child feeds */
5847
5848 $result = db_query($link, "SELECT id FROM ttrss_feeds
5849 WHERE owner_uid = '$owner_uid' AND $cat_qpart");
5850
5851 while ($line = db_fetch_assoc($result)) {
5852 ccache_update($link, $line["id"], $owner_uid, false, false);
5853 }
5854
8d505d78
AD
5855 $result = db_query($link, "SELECT SUM(value) AS sv
5856 FROM ttrss_counters_cache, ttrss_feeds
5857 WHERE id = feed_id AND $cat_qpart AND
37fb651d
AD
5858 ttrss_feeds.owner_uid = '$owner_uid'");
5859
51e196de 5860 $unread = (int) db_fetch_result($result, 0, "sv");
37fb651d 5861
f55b0b12
AD
5862 } else {
5863 $unread = (int) getFeedArticles($link, $feed_id, $is_cat, true, $owner_uid);
37fb651d 5864 }
2627f2d0 5865
c7e51de1
AD
5866 db_query($link, "BEGIN");
5867
8a4c759e 5868 $result = db_query($link, "SELECT feed_id FROM $table
2627f2d0
AD
5869 WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id' LIMIT 1");
5870
5871 if (db_num_rows($result) == 1) {
8a4c759e 5872 db_query($link, "UPDATE $table SET
2627f2d0
AD
5873 value = '$unread', updated = NOW() WHERE
5874 feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
5875
5876 } else {
8a4c759e 5877 db_query($link, "INSERT INTO $table
8d505d78
AD
5878 (feed_id, value, owner_uid, updated)
5879 VALUES
2627f2d0 5880 ($feed_id, $unread, $owner_uid, NOW())");
2627f2d0
AD
5881 }
5882
c7e51de1
AD
5883 db_query($link, "COMMIT");
5884
6b49a3dd 5885 if ($feed_id > 0 && $prev_unread != $unread) {
8a4c759e 5886
37fb651d 5887 if (!$is_cat) {
8a4c759e 5888
6b49a3dd 5889 /* Update parent category */
8a4c759e 5890
6b49a3dd 5891 if ($update_pcat) {
37fb651d 5892
6b49a3dd
AD
5893 $result = db_query($link, "SELECT cat_id FROM ttrss_feeds
5894 WHERE owner_uid = '$owner_uid' AND id = '$feed_id'");
8a4c759e 5895
6b49a3dd 5896 $cat_id = (int) db_fetch_result($result, 0, "cat_id");
37fb651d 5897
6b49a3dd 5898 ccache_update($link, $cat_id, $owner_uid, true);
6c2a9b9e 5899
6c2a9b9e 5900 }
8a4c759e 5901 }
5f4f7adf
AD
5902 } else if ($feed_id < 0) {
5903 ccache_update_all($link, $owner_uid);
8a4c759e
AD
5904 }
5905
5906 return $unread;
2627f2d0 5907 }
ceb30ba4
AD
5908
5909 function label_find_id($link, $label, $owner_uid) {
8d505d78
AD
5910 $result = db_query($link,
5911 "SELECT id FROM ttrss_labels2 WHERE caption = '$label'
ceb30ba4
AD
5912 AND owner_uid = '$owner_uid' LIMIT 1");
5913
5914 if (db_num_rows($result) == 1) {
5915 return db_fetch_result($result, 0, "id");
5916 } else {
5917 return 0;
5918 }
5919 }
5920
814bff66 5921 function get_article_labels($link, $id) {
d721a547
AD
5922 global $memcache;
5923
d721a547
AD
5924 $obj_id = md5("LABELS:$id:" . $_SESSION["uid"]);
5925
814bff66
AD
5926 $rv = array();
5927
d721a547
AD
5928 if ($memcache && $obj = $memcache->get($obj_id)) {
5929 return $obj;
5930 } else {
905ff52a
AD
5931
5932 $result = db_query($link, "SELECT label_cache FROM
5933 ttrss_user_entries WHERE ref_id = '$id' AND owner_uid = " .
5934 $_SESSION["uid"]);
5935
5936 $label_cache = db_fetch_result($result, 0, "label_cache");
5937
5938 if ($label_cache) {
5939
5940 $label_cache = json_decode($label_cache, true);
5941
5942 if ($label_cache["no-labels"] == 1)
5943 return $rv;
5944 else
5945 return $label_cache;
5946 }
5947
8d505d78
AD
5948 $result = db_query($link,
5949 "SELECT DISTINCT label_id,caption,fg_color,bg_color
5950 FROM ttrss_labels2, ttrss_user_labels2
5951 WHERE id = label_id
5952 AND article_id = '$id'
905ff52a
AD
5953 AND owner_uid = ".$_SESSION["uid"] . "
5954 ORDER BY caption");
5955
d721a547
AD
5956 while ($line = db_fetch_assoc($result)) {
5957 $rk = array($line["label_id"], $line["caption"], $line["fg_color"],
5958 $line["bg_color"]);
5959 array_push($rv, $rk);
5960 }
5961 if ($memcache) $memcache->add($obj_id, $rv, 0, 3600);
905ff52a 5962
8d505d78 5963 if (count($rv) > 0)
905ff52a
AD
5964 label_update_cache($link, $id, $rv);
5965 else
5966 label_update_cache($link, $id, array("no-labels" => 1));
814bff66
AD
5967 }
5968
5969 return $rv;
5970 }
5971
5972
b8a637f3 5973 function label_find_caption($link, $label, $owner_uid) {
8d505d78
AD
5974 $result = db_query($link,
5975 "SELECT caption FROM ttrss_labels2 WHERE id = '$label'
b8a637f3
AD
5976 AND owner_uid = '$owner_uid' LIMIT 1");
5977
5978 if (db_num_rows($result) == 1) {
5979 return db_fetch_result($result, 0, "caption");
5980 } else {
5981 return "";
5982 }
5983 }
5984
905ff52a
AD
5985 function label_update_cache($link, $id, $labels = false, $force = false) {
5986
5987 if ($force)
5988 label_clear_cache($link, $id);
5989
8d505d78 5990 if (!$labels)
905ff52a
AD
5991 $labels = get_article_labels($link, $id);
5992
5993 $labels = db_escape_string(json_encode($labels));
5994
5995 db_query($link, "UPDATE ttrss_user_entries SET
5996 label_cache = '$labels' WHERE ref_id = '$id'");
5997
5998 }
5999
6000 function label_clear_cache($link, $id) {
6001
6002 db_query($link, "UPDATE ttrss_user_entries SET
6003 label_cache = '' WHERE ref_id = '$id'");
6004
6005 }
6006
933ba4ee
AD
6007 function label_remove_article($link, $id, $label, $owner_uid) {
6008
6009 $label_id = label_find_id($link, $label, $owner_uid);
6010
6011 if (!$label_id) return;
6012
8d505d78 6013 $result = db_query($link,
933ba4ee 6014 "DELETE FROM ttrss_user_labels2
8d505d78 6015 WHERE
933ba4ee
AD
6016 label_id = '$label_id' AND
6017 article_id = '$id'");
905ff52a
AD
6018
6019 label_clear_cache($link, $id);
933ba4ee
AD
6020 }
6021
ceb30ba4
AD
6022 function label_add_article($link, $id, $label, $owner_uid) {
6023
d721a547
AD
6024 global $memcache;
6025
6026 if ($memcache) {
6027 $obj_id = md5("LABELS:$id:$owner_uid");
6028 $memcache->delete($obj_id);
6029 }
6030
ceb30ba4
AD
6031 $label_id = label_find_id($link, $label, $owner_uid);
6032
6033 if (!$label_id) return;
6034
8d505d78
AD
6035 $result = db_query($link,
6036 "SELECT
ceb30ba4 6037 article_id FROM ttrss_labels2, ttrss_user_labels2
8d505d78
AD
6038 WHERE
6039 label_id = id AND
ceb30ba4
AD
6040 label_id = '$label_id' AND
6041 article_id = '$id' AND owner_uid = '$owner_uid'
6042 LIMIT 1");
6043
6044 if (db_num_rows($result) == 0) {
8d505d78 6045 db_query($link, "INSERT INTO ttrss_user_labels2
ceb30ba4
AD
6046 (label_id, article_id) VALUES ('$label_id', '$id')");
6047 }
905ff52a 6048
8d505d78 6049 label_clear_cache($link, $id);
905ff52a 6050
ceb30ba4 6051 }
1380f8ee
AD
6052
6053 function label_remove($link, $id, $owner_uid) {
d721a547
AD
6054 global $memcache;
6055
905ff52a
AD
6056 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
6057
d721a547
AD
6058 if ($memcache) {
6059 $obj_id = md5("LABELS:$id:$owner_uid");
6060 $memcache->delete($obj_id);
6061 }
1380f8ee
AD
6062
6063 db_query($link, "BEGIN");
6064
6065 $result = db_query($link, "SELECT caption FROM ttrss_labels2
6066 WHERE id = '$id'");
6067
6068 $caption = db_fetch_result($result, 0, "caption");
6069
6070 $result = db_query($link, "DELETE FROM ttrss_labels2 WHERE id = '$id'
905ff52a 6071 AND owner_uid = " . $owner_uid);
1380f8ee
AD
6072
6073 if (db_affected_rows($link, $result) != 0 && $caption) {
6074
8801fb01
AD
6075 /* Remove access key for the label */
6076
6077 $ext_id = -11 - $id;
6078
6079 db_query($link, "DELETE FROM ttrss_access_keys WHERE
6080 feed_id = '$ext_id' AND owner_uid = $owner_uid");
6081
1380f8ee 6082 /* Disable filters that reference label being removed */
8d505d78 6083
1380f8ee
AD
6084 db_query($link, "UPDATE ttrss_filters SET
6085 enabled = false WHERE action_param = '$caption'
6086 AND action_id = 7
905ff52a
AD
6087 AND owner_uid = " . $owner_uid);
6088
6089 /* Remove cached data */
6090
6091 db_query($link, "UPDATE ttrss_user_entries SET label_cache = ''
6092 WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $owner_uid);
6093
6094 }
1380f8ee
AD
6095
6096 db_query($link, "COMMIT");
6097 }
79c88e11 6098
6b2ee18d
AD
6099 function label_create($link, $caption) {
6100
6101 db_query($link, "BEGIN");
6102
6103 $result = false;
6104
6105 $result = db_query($link, "SELECT id FROM ttrss_labels2
6106 WHERE caption = '$caption' AND owner_uid = ". $_SESSION["uid"]);
6107
6108 if (db_num_rows($result) == 0) {
6109 $result = db_query($link,
8d505d78 6110 "INSERT INTO ttrss_labels2 (caption,owner_uid)
6b2ee18d
AD
6111 VALUES ('$caption', '".$_SESSION["uid"]."')");
6112
6113 $result = db_affected_rows($link, $result) != 0;
6114 }
6115
6116 db_query($link, "COMMIT");
6117
6118 return $result;
6119 }
6120
79c88e11 6121 function print_labels_headlines_dropdown($link, $feed_id) {
fcf70c51 6122 print "<option value=\"addLabel()\">".__("Create label...")."</option>";
79c88e11
AD
6123
6124 $result = db_query($link, "SELECT id, caption FROM ttrss_labels2 WHERE
6125 owner_uid = '".$_SESSION["uid"]."' ORDER BY caption");
6126
6127 while ($line = db_fetch_assoc($result)) {
6128
6129 $label_id = $line["id"];
6130 $label_caption = $line["caption"];
c3fddd05 6131 $id = $line["id"];
79c88e11
AD
6132
6133 if ($feed_id < -10 && $feed_id == -11-$label_id) {
8d505d78 6134 print "<option id=\"LHDL-$id\"
fcf70c51
AD
6135 value=\"selectionRemoveLabel($label_id)\">".
6136 __('Remove:') . " $label_caption</option>";
8d505d78
AD
6137 } else {
6138 print "<option id=\"LHDL-$id\"
fcf70c51
AD
6139 value=\"selectionAssignLabel($label_id)\">".
6140 __('Assign:') . " $label_caption</option>";
79c88e11
AD
6141 }
6142 }
6143 }
307d187c
AD
6144
6145 function format_tags_string($tags, $id) {
6146
6147 $tags_str = "";
6148 $tags_nolinks_str = "";
6149
6150 $num_tags = 0;
6151
dce46cad 6152/* if (get_user_theme($link) == "3pane") {
307d187c
AD
6153 $tag_limit = 3;
6154 } else {
6155 $tag_limit = 6;
d9084cf2
AD
6156 } */
6157
6158 $tag_limit = 6;
307d187c
AD
6159
6160 $formatted_tags = array();
6161
6162 foreach ($tags as $tag) {
6163 $num_tags++;
6164 $tag_escaped = str_replace("'", "\\'", $tag);
6165
275a0af2
AD
6166 if (mb_strlen($tag) > 30) {
6167 $tag = truncate_string($tag, 30);
6168 }
6169
307d187c
AD
6170 $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>";
6171
6172 array_push($formatted_tags, $tag_str);
275a0af2
AD
6173
6174 $tmp_tags_str = implode(", ", $formatted_tags);
8d505d78 6175
275a0af2 6176 if ($num_tags == $tag_limit || mb_strlen($tmp_tags_str) > 150) {
307d187c
AD
6177 break;
6178 }
6179 }
6180
6181 $tags_str = implode(", ", $formatted_tags);
6182
6183 if ($num_tags < count($tags)) {
6184 $tags_str .= ", &hellip;";
6185 }
6186
6187 if ($num_tags == 0) {
6188 $tags_str = __("no tags");
6189 }
6190
6191 return $tags_str;
6192
6193 }
2eb9c95c
AD
6194
6195 function format_article_labels($labels, $id) {
6196
6197 $labels_str = "";
6198
6199 foreach ($labels as $l) {
8d505d78 6200 $labels_str .= sprintf("<span class='hlLabelRef'
2eb9c95c
AD
6201 style='color : %s; background-color : %s'>%s</span>",
6202 $l[2], $l[3], $l[1]);
6203 }
6204
6205 return $labels_str;
6206
6207 }
c7e51de1
AD
6208
6209 function format_article_note($id, $note) {
6210
8d505d78 6211 $str = "<div class='articleNote' title=\"".__('edit note')."\"
741b6090 6212 onclick=\"editArticleNote($id)\">$note</div>";
c7e51de1
AD
6213
6214 return $str;
6215 }
7f969260 6216
fcf70c51 6217 function toggle_collapse_cat($link, $cat_id, $mode) {
7f969260 6218 if ($cat_id > 0) {
fcf70c51
AD
6219 $mode = bool_to_sql_bool($mode);
6220
7f969260 6221 db_query($link, "UPDATE ttrss_feed_categories SET
8d505d78 6222 collapsed = $mode WHERE id = '$cat_id' AND owner_uid = " .
7f969260
AD
6223 $_SESSION["uid"]);
6224 } else {
6225 $pref_name = '';
6226
6227 switch ($cat_id) {
6228 case -1:
6229 $pref_name = '_COLLAPSED_SPECIAL';
6230 break;
6231 case -2:
6232 $pref_name = '_COLLAPSED_LABELS';
6233 break;
6234 case 0:
6235 $pref_name = '_COLLAPSED_UNCAT';
6236 break;
6237 }
6238
6239 if ($pref_name) {
fcf70c51 6240 if ($mode) {
7f969260 6241 set_pref($link, $pref_name, 'true');
fcf70c51
AD
6242 } else {
6243 set_pref($link, $pref_name, 'false');
7f969260
AD
6244 }
6245 }
6246 }
6247 }
7e329f13
AD
6248
6249 function remove_feed($link, $id, $owner_uid) {
6250
6251 if ($id > 0) {
e04c18a2
AD
6252
6253 /* save starred articles in Archived feed */
6254
6255 db_query($link, "BEGIN");
6256
8056ec50
AD
6257 /* prepare feed if necessary */
6258
6259 $result = db_query($link, "SELECT id FROM ttrss_archived_feeds
6260 WHERE id = '$id'");
6261
6262 if (db_num_rows($result) == 0) {
8d505d78 6263 db_query($link, "INSERT INTO ttrss_archived_feeds
8056ec50
AD
6264 (id, owner_uid, title, feed_url, site_url)
6265 SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
6266 WHERE id = '$id'");
6267 }
6268
74d22f0c 6269 db_query($link, "UPDATE ttrss_user_entries SET feed_id = NULL,
8d505d78 6270 orig_feed_id = '$id' WHERE feed_id = '$id' AND
e04c18a2
AD
6271 marked = true AND owner_uid = $owner_uid");
6272
8801fb01
AD
6273 /* Remove access key for the feed */
6274
6275 db_query($link, "DELETE FROM ttrss_access_keys WHERE
6276 feed_id = '$id' AND owner_uid = $owner_uid");
6277
e04c18a2
AD
6278 /* remove the feed */
6279
8d505d78 6280 db_query($link, "DELETE FROM ttrss_feeds
7e329f13
AD
6281 WHERE id = '$id' AND owner_uid = $owner_uid");
6282
e04c18a2
AD
6283 db_query($link, "COMMIT");
6284
69877646 6285/* if (file_exists(ICONS_DIR . "/$id.ico")) {
7e329f13 6286 unlink(ICONS_DIR . "/$id.ico");
69877646 6287 } */
7e329f13
AD
6288
6289 ccache_remove($link, $id, $owner_uid);
6290
6291 } else {
6292 label_remove($link, -11-$id, $owner_uid);
6293 ccache_remove($link, -11-$id, $owner_uid);
6294 }
6295 }
6296
5c7c7da9 6297 function add_feed_category($link, $feed_cat) {
c00907f2
AD
6298
6299 if (!$feed_cat) return false;
6300
5c7c7da9
AD
6301 db_query($link, "BEGIN");
6302
6303 $result = db_query($link,
6304 "SELECT id FROM ttrss_feed_categories
6305 WHERE title = '$feed_cat' AND owner_uid = ".$_SESSION["uid"]);
6306
6307 if (db_num_rows($result) == 0) {
8d505d78 6308
5c7c7da9 6309 $result = db_query($link,
8d505d78 6310 "INSERT INTO ttrss_feed_categories (owner_uid,title)
5c7c7da9
AD
6311 VALUES ('".$_SESSION["uid"]."', '$feed_cat')");
6312
6313 db_query($link, "COMMIT");
6314
6315 return true;
6316 }
6317
6318 return false;
8d505d78 6319 }
5c7c7da9 6320
7e329f13
AD
6321 function remove_feed_category($link, $id, $owner_uid) {
6322
6323 db_query($link, "DELETE FROM ttrss_feed_categories
6324 WHERE id = '$id' AND owner_uid = $owner_uid");
6325
6326 ccache_remove($link, $id, $owner_uid, true);
6327 }
6328
16fdac16
AD
6329 function archive_article($link, $id, $owner_uid) {
6330 db_query($link, "BEGIN");
6331
6332 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
6333 WHERE ref_id = '$id' AND owner_uid = $owner_uid");
6334
6335 if (db_num_rows($result) != 0) {
6336
6337 /* prepare the archived table */
6338
6339 $feed_id = (int) db_fetch_result($result, 0, "feed_id");
6340
6341 if ($feed_id) {
6342 $result = db_query($link, "SELECT id FROM ttrss_archived_feeds
6343 WHERE id = '$feed_id'");
6344
6345 if (db_num_rows($result) == 0) {
8d505d78 6346 db_query($link, "INSERT INTO ttrss_archived_feeds
16fdac16
AD
6347 (id, owner_uid, title, feed_url, site_url)
6348 SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
6349 WHERE id = '$feed_id'");
6350 }
6351
8d505d78 6352 db_query($link, "UPDATE ttrss_user_entries
16fdac16
AD
6353 SET orig_feed_id = feed_id, feed_id = NULL
6354 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
6355 }
6356 }
6357
6358 db_query($link, "COMMIT");
6359 }
ab197ae1
AD
6360
6361 function getArticleFeed($link, $id) {
8d505d78 6362 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
a545dc31 6363 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
ab197ae1
AD
6364
6365 if (db_num_rows($result) != 0) {
6366 return db_fetch_result($result, 0, "feed_id");
6367 } else {
6368 return 0;
6369 }
6370 }
a5819bb3
AD
6371
6372 function make_url_from_parts($parts) {
6373 $url = $parts['scheme'] . '://' . $parts['host'];
6374
6375 if ($parts['path']) $url .= $parts['path'];
6376 if ($parts['query']) $url .= '?' . $parts['query'];
6377
6378 return $url;
6379 }
6380
f2c6c008
CW
6381 /**
6382 * Fixes incomplete URLs by prepending "http://".
f0266f51
CW
6383 * Also replaces feed:// with http://, and
6384 * prepends a trailing slash if the url is a domain name only.
f2c6c008
CW
6385 *
6386 * @param string $url Possibly incomplete URL
6387 *
6388 * @return string Fixed URL.
6389 */
6390 function fix_url($url) {
6391 if (strpos($url, '://') === false) {
6392 $url = 'http://' . $url;
f0266f51
CW
6393 } else if (substr($url, 0, 5) == 'feed:') {
6394 $url = 'http:' . substr($url, 5);
6395 }
6396
6397 //prepend slash if the URL has no slash in it
6398 // "http://www.example" -> "http://www.example/"
44453773 6399 if (strpos($url, '/', strpos($url, ':') + 3) === false) {
f0266f51 6400 $url .= '/';
f2c6c008 6401 }
ec39a02c
AD
6402
6403 if ($url != "http:///")
6404 return $url;
6405 else
6406 return '';
f2c6c008
CW
6407 }
6408
a5819bb3
AD
6409 function validate_feed_url($url) {
6410 $parts = parse_url($url);
6411
6412 return ($parts['scheme'] == 'http' || $parts['scheme'] == 'feed' || $parts['scheme'] == 'https');
6413
6414 }
d9084cf2 6415
be35798b
AD
6416 function get_article_enclosures($link, $id) {
6417
6418 global $memcache;
6419
8d505d78 6420 $query = "SELECT * FROM ttrss_enclosures
be35798b
AD
6421 WHERE post_id = '$id' AND content_url != ''";
6422
bd3f2ade 6423 $obj_id = md5("ENCLOSURES:$id");
be35798b
AD
6424
6425 $rv = array();
6426
bd3f2ade 6427 if ($memcache && $obj = $memcache->get($obj_id)) {
be35798b
AD
6428 $rv = $obj;
6429 } else {
6430 $result = db_query($link, $query);
6431
6432 if (db_num_rows($result) > 0) {
6433 while ($line = db_fetch_assoc($result)) {
6434 array_push($rv, $line);
6435 }
bd3f2ade 6436 if ($memcache) $memcache->add($obj_id, $rv, 0, 3600);
be35798b
AD
6437 }
6438 }
6439
6440 return $rv;
6441 }
6442
911d4c08 6443 function api_get_feeds($link, $cat_id, $unread_only, $limit, $offset) {
911d4c08
AD
6444
6445 $feeds = array();
6446
911d4c08
AD
6447 /* Labels */
6448
fb8d17f3 6449 if ($cat_id == -4 || $cat_id == -2) {
911d4c08
AD
6450 $counters = getLabelCounters($link, true);
6451
11232703 6452 foreach (array_values($counters) as $cv) {
911d4c08 6453
11232703 6454 $unread = $cv["counter"];
8d505d78 6455
911d4c08 6456 if ($unread || !$unread_only) {
8d505d78 6457
911d4c08 6458 $row = array(
11232703
AD
6459 "id" => $cv["id"],
6460 "title" => $cv["description"],
6461 "unread" => $cv["counter"],
911d4c08
AD
6462 "cat_id" => -2,
6463 );
8d505d78 6464
911d4c08
AD
6465 array_push($feeds, $row);
6466 }
6467 }
6468 }
6469
6470 /* Virtual feeds */
6471
fb8d17f3 6472 if ($cat_id == -4 || $cat_id == -1) {
911d4c08
AD
6473 foreach (array(-1, -2, -3, -4, 0) as $i) {
6474 $unread = getFeedUnread($link, $i);
6475
6476 if ($unread || !$unread_only) {
6477 $title = getFeedTitle($link, $i);
6478
6479 $row = array(
6480 "id" => $i,
6481 "title" => $title,
6482 "unread" => $unread,
6483 "cat_id" => -1,
6484 );
6485 array_push($feeds, $row);
6486 }
6487
8d505d78 6488 }
911d4c08 6489 }
b41c2549
AD
6490
6491 /* Real feeds */
6492
6493 if ($limit) {
6494 $limit_qpart = "LIMIT $limit OFFSET $offset";
6495 } else {
6496 $limit_qpart = "";
6497 }
6498
fb8d17f3 6499 if ($cat_id == -4 || $cat_id == -3) {
8d505d78 6500 $result = db_query($link, "SELECT
b41c2549
AD
6501 id, feed_url, cat_id, title, ".
6502 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
8d505d78 6503 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] .
b41c2549
AD
6504 " ORDER BY cat_id, title " . $limit_qpart);
6505 } else {
fb8d17f3
AD
6506
6507 if ($cat_id)
6508 $cat_qpart = "cat_id = '$cat_id'";
6509 else
6510 $cat_qpart = "cat_id IS NULL";
6511
8d505d78 6512 $result = db_query($link, "SELECT
b41c2549
AD
6513 id, feed_url, cat_id, title, ".
6514 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
8d505d78
AD
6515 FROM ttrss_feeds WHERE
6516 $cat_qpart AND owner_uid = " . $_SESSION["uid"] .
b41c2549
AD
6517 " ORDER BY cat_id, title " . $limit_qpart);
6518 }
6519
6520 while ($line = db_fetch_assoc($result)) {
6521
6522 $unread = getFeedUnread($link, $line["id"]);
6523
6524 $has_icon = feed_has_icon($line['id']);
6525
6526 if ($unread || !$unread_only) {
6527
6528 $row = array(
6529 "feed_url" => $line["feed_url"],
6530 "title" => $line["title"],
6531 "id" => (int)$line["id"],
6532 "unread" => (int)$unread,
6533 "has_icon" => $has_icon,
6534 "cat_id" => (int)$line["cat_id"],
6535 "last_updated" => strtotime($line["last_updated"])
6536 );
8d505d78 6537
b41c2549
AD
6538 array_push($feeds, $row);
6539 }
6540 }
6541
911d4c08
AD
6542 return $feeds;
6543 }
6544
6545 function api_get_headlines($link, $feed_id, $limit, $offset,
6546 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order) {
6547
6548 /* do not rely on params below */
6549
6550 $search = db_escape_string($_REQUEST["search"]);
6551 $search_mode = db_escape_string($_REQUEST["search_mode"]);
6552 $match_on = db_escape_string($_REQUEST["match_on"]);
8d505d78
AD
6553
6554 $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
911d4c08
AD
6555 $view_mode, $is_cat, $search, $search_mode, $match_on,
6556 $order, $offset);
6557
6558 $result = $qfh_ret[0];
6559 $feed_title = $qfh_ret[1];
6560
6561 $headlines = array();
6562
6563 while ($line = db_fetch_assoc($result)) {
8d505d78 6564 $is_updated = ($line["last_read"] == "" &&
911d4c08
AD
6565 ($line["unread"] != "t" && $line["unread"] != "1"));
6566
6567 $headline_row = array(
6568 "id" => (int)$line["id"],
6569 "unread" => sql_bool_to_bool($line["unread"]),
6570 "marked" => sql_bool_to_bool($line["marked"]),
9ed133e7 6571 "published" => sql_bool_to_bool($line["published"]),
911d4c08
AD
6572 "updated" => strtotime($line["updated"]),
6573 "is_updated" => $is_updated,
6574 "title" => $line["title"],
6575 "link" => $line["link"],
6576 "feed_id" => $line["feed_id"],
78ac6caf 6577 "tags" => get_article_tags($link, $line["id"]),
911d4c08
AD
6578 );
6579
6580 if ($show_excerpt) {
6581 $excerpt = truncate_string(strip_tags($line["content_preview"]), 100);
6582 $headline_row["excerpt"] = $excerpt;
6583 }
6584
6585 if ($show_content) {
6586 $headline_row["content"] = $line["content_preview"];
6587 }
6588
6589 array_push($headlines, $headline_row);
6590 }
6591
6592 return $headlines;
6593 }
6594
bd202c3f
AD
6595 function generate_error_feed($link, $error) {
6596 $reply = array();
6597
6598 $reply['headlines']['id'] = -6;
6599 $reply['headlines']['is_cat'] = false;
6600
6601 $reply['headlines']['toolbar'] = '';
6602 $reply['headlines']['content'] = "<div class='whiteBox'>". $error . "</div>";
6603
6604 $reply['headlines-info'] = array("count" => 0,
6605 "vgroup_last_feed" => '',
6606 "unread" => 0,
6607 "disable_cache" => true);
6608
6609 return $reply;
6610 }
fe1087fb 6611
13e785e0 6612
bd202c3f
AD
6613 function generate_dashboard_feed($link) {
6614 $reply = array();
6615
6616 $reply['headlines']['id'] = -5;
6617 $reply['headlines']['is_cat'] = false;
fe1087fb 6618
bd202c3f
AD
6619 $reply['headlines']['toolbar'] = '';
6620 $reply['headlines']['content'] = "<div class='whiteBox'>".__('No feed selected.');
fe1087fb 6621
bd202c3f 6622 $reply['headlines']['content'] .= "<p class=\"small\"><span class=\"insensitive\">";
5d128c95
AD
6623
6624 $result = db_query($link, "SELECT ".SUBSTRING_FOR_DATE."(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds
6625 WHERE owner_uid = " . $_SESSION['uid']);
6626
6627 $last_updated = db_fetch_result($result, 0, "last_updated");
324944f3 6628 $last_updated = make_local_datetime($link, $last_updated, false);
5d128c95 6629
bd202c3f 6630 $reply['headlines']['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);
5d128c95 6631
fe1087fb
AD
6632 $result = db_query($link, "SELECT COUNT(id) AS num_errors
6633 FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
6634
6635 $num_errors = db_fetch_result($result, 0, "num_errors");
6636
6637 if ($num_errors > 0) {
bd202c3f
AD
6638 $reply['headlines']['content'] .= "<br/>";
6639 $reply['headlines']['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".
fe1087fb
AD
6640 __('Some feeds have update errors (click for details)')."</a>";
6641 }
bd202c3f 6642 $reply['headlines']['content'] .= "</span></p>";
fe1087fb 6643
bd202c3f 6644 $reply['headlines-info'] = array("count" => 0,
ffbe082d
AD
6645 "vgroup_last_feed" => '',
6646 "unread" => 0,
6647 "disable_cache" => true);
6648
bd202c3f 6649 return $reply;
fe1087fb 6650 }
31a53903
AD
6651
6652 function save_email_address($link, $email) {
6653 // FIXME: implement persistent storage of emails
6654
8d505d78 6655 if (!$_SESSION['stored_emails'])
31a53903
AD
6656 $_SESSION['stored_emails'] = array();
6657
6658 if (!in_array($email, $_SESSION['stored_emails']))
6659 array_push($_SESSION['stored_emails'], $email);
6660 }
8801fb01
AD
6661
6662 function update_feed_access_key($link, $feed_id, $is_cat, $owner_uid = false) {
6663 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
6664
6665 $sql_is_cat = bool_to_sql_bool($is_cat);
6666
8d505d78
AD
6667 $result = db_query($link, "SELECT access_key FROM ttrss_access_keys
6668 WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat
8801fb01
AD
6669 AND owner_uid = " . $owner_uid);
6670
6671 if (db_num_rows($result) == 1) {
6672 $key = db_escape_string(sha1(uniqid(rand(), true)));
6673
6674 db_query($link, "UPDATE ttrss_access_keys SET access_key = '$key'
8d505d78 6675 WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat
8801fb01
AD
6676 AND owner_uid = " . $owner_uid);
6677
6678 return $key;
6679
6680 } else {
6681 return get_feed_access_key($link, $feed_id, $is_cat, $owner_uid);
6682 }
6683 }
6684
6685 function get_feed_access_key($link, $feed_id, $is_cat, $owner_uid = false) {
6686
6687 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
6688
6689 $sql_is_cat = bool_to_sql_bool($is_cat);
6690
8d505d78
AD
6691 $result = db_query($link, "SELECT access_key FROM ttrss_access_keys
6692 WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat
8801fb01
AD
6693 AND owner_uid = " . $owner_uid);
6694
6695 if (db_num_rows($result) == 1) {
6696 return db_fetch_result($result, 0, "access_key");
6697 } else {
6698 $key = db_escape_string(sha1(uniqid(rand(), true)));
6699
8d505d78 6700 $result = db_query($link, "INSERT INTO ttrss_access_keys
8801fb01
AD
6701 (access_key, feed_id, is_cat, owner_uid)
6702 VALUES ('$key', '$feed_id', $sql_is_cat, '$owner_uid')");
6703
6704 return $key;
6705 }
6706 return false;
6707 }
f0266f51
CW
6708
6709 /**
6710 * Extracts RSS/Atom feed URLs from the given HTML URL.
6711 *
6712 * @param string $url HTML page URL
6713 *
6714 * @return array Array of feeds. Key is the full URL, value the title
6715 */
8d505d78 6716 function get_feeds_from_html($url, $login = false, $pass = false)
f0266f51
CW
6717 {
6718 $url = fix_url($url);
6719 $baseUrl = substr($url, 0, strrpos($url, '/') + 1);
6720
fb074239
AD
6721 libxml_use_internal_errors(true);
6722
8d505d78
AD
6723 $content = @fetch_file_contents($url, false, $login, $pass);
6724
f0266f51 6725 $doc = new DOMDocument();
8d505d78 6726 $doc->loadHTML($content);
f0266f51
CW
6727 $xpath = new DOMXPath($doc);
6728 $entries = $xpath->query('/html/head/link[@rel="alternate"]');
6729 $feedUrls = array();
6730 foreach ($entries as $entry) {
6731 if ($entry->hasAttribute('href')) {
6732 $title = $entry->getAttribute('title');
6733 if ($title == '') {
6734 $title = $entry->getAttribute('type');
6735 }
923818fc
CW
6736 $feedUrl = rewrite_relative_url(
6737 $baseUrl, $entry->getAttribute('href')
6738 );
f0266f51
CW
6739 $feedUrls[$feedUrl] = $title;
6740 }
6741 }
6742 return $feedUrls;
6743 }
6744
f33479da
CW
6745 /**
6746 * Checks if the content behind the given URL is a HTML file
6747 *
6748 * @param string $url URL to check
6749 *
6750 * @return boolean True if the URL contains HTML content
6751 */
8d505d78
AD
6752 function url_is_html($url, $login = false, $pass = false) {
6753 $content = substr(fetch_file_contents($url, false, $login, $pass), 0, 1000);
6754
24eb4c78
CW
6755 if (stripos($content, '<html>') === false
6756 && stripos($content, '<html ') === false
f33479da
CW
6757 ) {
6758 return false;
6759 }
6760
6761 return true;
6762 }
24e2bb3a 6763
d90868d7 6764 function print_label_select($link, $name, $value, $attributes = "") {
24e2bb3a
AD
6765
6766 $result = db_query($link, "SELECT caption FROM ttrss_labels2
6767 WHERE owner_uid = '".$_SESSION["uid"]."' ORDER BY caption");
6768
8d505d78 6769 print "<select default=\"$value\" name=\"" . htmlspecialchars($name) .
d90868d7 6770 "\" $attributes onchange=\"labelSelectOnChange(this)\" >";
24e2bb3a
AD
6771
6772 while ($line = db_fetch_assoc($result)) {
6773
6774 $issel = ($line["caption"] == $value) ? "selected=\"1\"" : "";
6775
d90868d7
AD
6776 print "<option value=\"".htmlspecialchars($line["caption"])."\"
6777 $issel>" . htmlspecialchars($line["caption"]) . "</option>";
24e2bb3a
AD
6778
6779 }
6780
d90868d7 6781# print "<option value=\"ADD_LABEL\">" .__("Add label...") . "</option>";
24e2bb3a
AD
6782
6783 print "</select>";
6784
6785
6786 }
6787
009646d2 6788 function format_article_enclosures($link, $id, $always_display_enclosures,
dad14b51
AD
6789 $article_content) {
6790
6791 $result = get_article_enclosures($link, $id);
009646d2 6792 $rv = '';
8d505d78 6793
dad14b51 6794 if (count($result) > 0) {
8d505d78 6795
dad14b51
AD
6796 $entries_html = array();
6797 $entries = array();
8d505d78 6798
dad14b51 6799 foreach ($result as $line) {
8d505d78 6800
dad14b51
AD
6801 $url = $line["content_url"];
6802 $ctype = $line["content_type"];
8d505d78 6803
dad14b51 6804 if (!$ctype) $ctype = __("unknown type");
8d505d78 6805
c3edc667 6806# $filename = substr($url, strrpos($url, "/")+1);
8d505d78 6807
dad14b51 6808 $entry = format_inline_player($link, $url, $ctype);
8d505d78 6809
c3edc667
AD
6810# $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
6811# $filename . " (" . $ctype . ")" . "</a>";
8d505d78 6812
dad14b51 6813 array_push($entries_html, $entry);
8d505d78 6814
dad14b51 6815 $entry = array();
8d505d78 6816
dad14b51
AD
6817 $entry["type"] = $ctype;
6818 $entry["filename"] = $filename;
6819 $entry["url"] = $url;
8d505d78 6820
dad14b51
AD
6821 array_push($entries, $entry);
6822 }
8d505d78 6823
009646d2 6824 $rv .= "<div class=\"postEnclosures\">";
8d505d78 6825
dad14b51
AD
6826 if (!get_pref($link, "STRIP_IMAGES")) {
6827 if ($always_display_enclosures ||
6828 !preg_match("/<img/i", $article_content)) {
8d505d78 6829
dad14b51 6830 foreach ($entries as $entry) {
8d505d78 6831
dad14b51
AD
6832 if (preg_match("/image/", $entry["type"]) ||
6833 preg_match("/\.(jpg|png|gif|bmp)/i", $entry["filename"])) {
8d505d78 6834
009646d2 6835 $rv .= "<p><img
dad14b51
AD
6836 alt=\"".htmlspecialchars($entry["filename"])."\"
6837 src=\"" .htmlspecialchars($entry["url"]) . "\"/></p>";
6838 }
6839 }
6840 }
6841 }
8d505d78 6842
905ff52a 6843 if (count($entries) == 1) {
009646d2 6844 $rv .= __("Attachment:") . " ";
dad14b51 6845 } else {
009646d2 6846 $rv .= __("Attachments:") . " ";
dad14b51 6847 }
8d505d78 6848
009646d2 6849 $rv .= join(", ", $entries_html);
8d505d78 6850
009646d2 6851 $rv .= "</div>";
dad14b51 6852 }
009646d2
AD
6853
6854 return $rv;
dad14b51
AD
6855 }
6856
f8fb4498
AD
6857 function getLastArticleId($link) {
6858 $result = db_query($link, "SELECT MAX(ref_id) AS id FROM ttrss_user_entries
6859 WHERE owner_uid = " . $_SESSION["uid"]);
6860
6861 if (db_num_rows($result) == 1) {
6862 return db_fetch_result($result, 0, "id");
6863 } else {
6864 return -1;
6865 }
6866 }
8cc3c778
AD
6867
6868 function build_url($parts) {
6869 return $parts['scheme'] . "://" . $parts['host'] . $parts['path'];
6870 }
6871
f679105c
CW
6872 /**
6873 * Converts a (possibly) relative URL to a absolute one.
6874 *
6875 * @param string $url Base URL (i.e. from where the document is)
6876 * @param string $rel_url Possibly relative URL in the document
6877 *
6878 * @return string Absolute URL
6879 */
8cc3c778
AD
6880 function rewrite_relative_url($url, $rel_url) {
6881 if (strpos($rel_url, "://") !== false) {
6882 return $rel_url;
8d505d78 6883 } else if (strpos($rel_url, "/") === 0)
8cc3c778
AD
6884 {
6885 $parts = parse_url($url);
6886 $parts['path'] = $rel_url;
6887
6888 return build_url($parts);
6889
6890 } else {
6891 $parts = parse_url($url);
f679105c
CW
6892 if (!isset($parts['path'])) {
6893 $parts['path'] = '/';
6894 }
6895 $dir = $parts['path'];
6896 if (substr($dir, -1) !== '/') {
6897 $dir = dirname($parts['path']);
6898 $dir !== '/' && $dir .= '/';
6899 }
6900 $parts['path'] = $dir . $rel_url;
8cc3c778
AD
6901
6902 return build_url($parts);
6903 }
6904 }
6905
e4f7f8df
AD
6906 function sphinx_search($query, $offset = 0, $limit = 30) {
6907 $sphinxClient = new SphinxClient();
6908
6909 $sphinxClient->SetServer('localhost', 9312);
6910 $sphinxClient->SetConnectTimeout(1);
6911
8d505d78 6912 $sphinxClient->SetFieldWeights(array('title' => 70, 'content' => 30,
e4f7f8df
AD
6913 'feed_title' => 20));
6914
6915 $sphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2);
6916 $sphinxClient->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
6917 $sphinxClient->SetLimits($offset, $limit, 1000);
6918 $sphinxClient->SetArrayResult(false);
6919 $sphinxClient->SetFilter('owner_uid', array($_SESSION['uid']));
8d505d78 6920
e4f7f8df
AD
6921 $result = $sphinxClient->Query($query, SPHINX_INDEX);
6922
6923 $ids = array();
6924
6925 if (is_array($result['matches'])) {
6926 foreach (array_keys($result['matches']) as $int_id) {
6927 $ref_id = $result['matches'][$int_id]['attrs']['ref_id'];
6928 array_push($ids, $ref_id);
6929 }
6930 }
6931
6932 return $ids;
6933 }
6934
868650e4
AD
6935 function cleanup_tags($link, $days = 14, $limit = 1000) {
6936
6937 if (DB_TYPE == "pgsql") {
6938 $interval_query = "date_updated < NOW() - INTERVAL '$days days'";
6939 } else if (DB_TYPE == "mysql") {
6940 $interval_query = "date_updated < DATE_SUB(NOW(), INTERVAL $days DAY)";
6941 }
6942
b5ec13fa 6943 $tags_deleted = 0;
868650e4 6944
b5ec13fa
AD
6945 while ($limit > 0) {
6946 $limit_part = 500;
6947
8d505d78
AD
6948 $query = "SELECT ttrss_tags.id AS id
6949 FROM ttrss_tags, ttrss_user_entries, ttrss_entries
b5ec13fa
AD
6950 WHERE post_int_id = int_id AND $interval_query AND
6951 ref_id = ttrss_entries.id AND tag_cache != '' LIMIT $limit_part";
8d505d78 6952
b5ec13fa
AD
6953 $result = db_query($link, $query);
6954
6955 $ids = array();
6956
6957 while ($line = db_fetch_assoc($result)) {
6958 array_push($ids, $line['id']);
6959 }
6960
6961 if (count($ids) > 0) {
6962 $ids = join(",", $ids);
6963 print ".";
6964
6965 $tmp_result = db_query($link, "DELETE FROM ttrss_tags WHERE id IN ($ids)");
6966 $tags_deleted += db_affected_rows($link, $tmp_result);
6967 } else {
6968 break;
6969 }
6970
6971 $limit -= $limit_part;
6972 }
6973
6974 print "\n";
868650e4 6975
b5ec13fa 6976 return $tags_deleted;
868650e4
AD
6977 }
6978
13e785e0
AD
6979 function feedlist_init_cat($link, $cat_id, $hidden = false) {
6980 $obj = array();
db34e084 6981 $cat_id = (int) $cat_id;
13e785e0
AD
6982
6983 if ($cat_id > 0) {
6984 $cat_unread = ccache_find($link, $cat_id, $_SESSION["uid"], true);
6985 } else if ($cat_id == 0 || $cat_id == -2) {
6986 $cat_unread = getCategoryUnread($link, $cat_id);
6987 }
6988
1985a5e0 6989 $obj['id'] = 'CAT:' . $cat_id;
13e785e0
AD
6990 $obj['items'] = array();
6991 $obj['name'] = getCategoryTitle($link, $cat_id);
6992 $obj['type'] = 'feed';
6993 $obj['unread'] = (int) $cat_unread;
6994 $obj['hidden'] = $hidden;
1985a5e0 6995 $obj['bare_id'] = $cat_id;
13e785e0
AD
6996
6997 return $obj;
6998 }
6999
fcf70c51 7000 function feedlist_init_feed($link, $feed_id, $title = false, $unread = false, $error = '', $updated = '') {
13e785e0 7001 $obj = array();
1985a5e0 7002 $feed_id = (int) $feed_id;
13e785e0 7003
8d505d78 7004 if (!$title)
13e785e0
AD
7005 $title = getFeedTitle($link, $feed_id, false);
7006
cd1bb36d 7007 if ($unread === false)
13e785e0
AD
7008 $unread = getFeedUnread($link, $feed_id, false);
7009
7010 $obj['id'] = 'FEED:' . $feed_id;
7011 $obj['name'] = $title;
7012 $obj['unread'] = (int) $unread;
7013 $obj['type'] = 'feed';
fcf70c51
AD
7014 $obj['error'] = $error;
7015 $obj['updated'] = $updated;
2ef5c21f 7016 $obj['icon'] = getFeedIcon($feed_id);
1985a5e0 7017 $obj['bare_id'] = $feed_id;
13e785e0
AD
7018
7019 return $obj;
7020 }
7021
54a3dd8d 7022
57e24c82 7023 function fetch_twitter_rss($link, $url, $owner_uid) {
8d505d78 7024 $result = db_query($link, "SELECT twitter_oauth FROM ttrss_users
57e24c82
AD
7025 WHERE id = $owner_uid");
7026
7027 $access_token = json_decode(db_fetch_result($result, 0, 'twitter_oauth'), true);
54a3dd8d 7028 $url_escaped = db_escape_string($url);
57e24c82
AD
7029
7030 if ($access_token) {
57e24c82 7031
54a3dd8d
AD
7032 $tmhOAuth = new tmhOAuth(array(
7033 'consumer_key' => CONSUMER_KEY,
7034 'consumer_secret' => CONSUMER_SECRET,
7035 'user_token' => $access_token['oauth_token'],
7036 'user_secret' => $access_token['oauth_token_secret'],
7037 ));
7038
7039 $code = $tmhOAuth->request('GET', $url);
7040
7041 if ($code == 200) {
7042
7043 $content = $tmhOAuth->response['response'];
7044
8d505d78 7045 $rss = new MagpieRSS($content, MAGPIE_OUTPUT_ENCODING,
54a3dd8d
AD
7046 MAGPIE_INPUT_ENCODING, MAGPIE_DETECT_ENCODING );
7047
7048 return $rss;
7049
7050 } else {
7051
8d505d78 7052 db_query($link, "UPDATE ttrss_feeds
54a3dd8d
AD
7053 SET last_error = 'OAuth authorization failed ($code).'
7054 WHERE feed_url = '$url_escaped' AND owner_uid = $owner_uid");
7055 }
57e24c82 7056
57e24c82 7057 } else {
54a3dd8d 7058
8d505d78 7059 db_query($link, "UPDATE ttrss_feeds
54a3dd8d 7060 SET last_error = 'OAuth information not found.'
dc891b12 7061 WHERE feed_url = '$url_escaped' AND owner_uid = $owner_uid");
54a3dd8d 7062
57e24c82
AD
7063 return false;
7064 }
7065 }
7066
88e4e597
AD
7067 function print_user_stylesheet($link) {
7068 $value = get_pref($link, 'USER_STYLESHEET');
7069
7070 if ($value) {
7071 print "<style type=\"text/css\">";
5823f9fb 7072 print str_replace("<br/>", "\n", $value);
88e4e597
AD
7073 print "</style>";
7074 }
7075
7076 }
7077
533c0ea6
AD
7078 function rewrite_urls($line) {
7079 global $url_regex;
7080
7081 $urls = null;
7082
8d505d78 7083 $result = preg_replace("/((?<!=.)((http|https|ftp)+):\/\/[^ ,!]+)/i",
533c0ea6
AD
7084 "<a target=\"_blank\" href=\"\\1\">\\1</a>", $line);
7085
7086 return $result;
7087 }
7088
40d13c28 7089?>