]> git.wh0rd.org - tt-rss.git/blame - include/functions.php
fix browsing by tags
[tt-rss.git] / include / functions.php
CommitLineData
1d3a17c7 1<?php
324944f3 2 date_default_timezone_set('UTC');
8a7f5767
CW
3 if (defined('E_DEPRECATED')) {
4 error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
5 } else {
6 error_reporting(E_ALL & ~E_NOTICE);
7 }
cce28758 8
40d13c28 9 require_once 'config.php';
cc17c205 10
fc2b26a6
AD
11 if (DB_TYPE == "pgsql") {
12 define('SUBSTRING_FOR_DATE', 'SUBSTRING_FOR_DATE');
13 } else {
14 define('SUBSTRING_FOR_DATE', 'SUBSTRING');
15 }
16
0c425dc7
AD
17 define('THEME_VERSION_REQUIRED', 1.1);
18
9632f884
AD
19 /**
20 * Return available translations names.
8d505d78 21 *
9632f884
AD
22 * @access public
23 * @return array A array of available translations.
24 */
f8c612d4 25 function get_translations() {
6a214f92 26 $tr = array(
8d505d78 27 "auto" => "Detect automatically",
a3162add 28 "ca_CA" => "Català",
6a214f92 29 "en_US" => "English",
36d0510c 30 "es_ES" => "Español",
a927fe7b 31 "de_DE" => "Deutsch",
6a214f92 32 "fr_FR" => "Français",
e78fd196 33 "hu_HU" => "Magyar (Hungarian)",
bb5d3960 34 "it_IT" => "Italiano",
1d004f12 35 "ja_JP" => "日本語 (Japanese)",
592535d7 36 "nb_NO" => "Norwegian bokmål",
6a214f92 37 "ru_RU" => "Русский",
9a063469 38 "pt_BR" => "Portuguese/Brazil",
6a214f92 39 "zh_CN" => "Simplified Chinese");
f8c612d4
AD
40
41 return $tr;
42 }
43
7b26a148
AD
44 require_once "lib/accept-to-gettext.php";
45 require_once "lib/gettext/gettext.inc";
aba609e0 46
7b26a148 47 function startup_gettext() {
8d505d78 48
7b26a148
AD
49 # Get locale from Accept-Language header
50 $lang = al2gt(array_keys(get_translations()), "text/html");
89cb787e 51
7b26a148
AD
52 if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) {
53 $lang = _TRANSLATION_OVERRIDE_DEFAULT;
54 }
89cb787e 55
7b26a148
AD
56 if ($_COOKIE["ttrss_lang"] && $_COOKIE["ttrss_lang"] != "auto") {
57 $lang = $_COOKIE["ttrss_lang"];
58 }
68659d98 59
7b26a148
AD
60 /* In login action of mobile version */
61 if ($_POST["language"] && defined('MOBILE_VERSION')) {
62 $lang = $_POST["language"];
63 $_COOKIE["ttrss_lang"] = $lang;
64 }
7c33dbd4 65
7b26a148
AD
66 if ($lang) {
67 if (defined('LC_MESSAGES')) {
68 _setlocale(LC_MESSAGES, $lang);
69 } else if (defined('LC_ALL')) {
70 _setlocale(LC_ALL, $lang);
8d039718 71 }
aba609e0 72
7b26a148
AD
73 if (defined('MOBILE_VERSION')) {
74 _bindtextdomain("messages", "../locale");
75 } else {
76 _bindtextdomain("messages", "locale");
77 }
865220a4 78
7b26a148
AD
79 _textdomain("messages");
80 _bind_textdomain_codeset("messages", "UTF-8");
865220a4 81 }
7b26a148
AD
82 }
83
84 startup_gettext();
cc17c205 85
be35798b
AD
86 if (defined('MEMCACHE_SERVER')) {
87 $memcache = new Memcache;
88 $memcache->connect(MEMCACHE_SERVER, 11211);
89 }
90
b619ff15 91 require_once 'db-prefs.php';
8911ac8b 92 require_once 'version.php';
40d13c28 93
a3ee2a38
AD
94 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
95
a18a4f38
AD
96 define('SELF_USER_AGENT', 'Tiny Tiny RSS/' . VERSION . ' (http://tt-rss.org/)');
97 define('MAGPIE_USER_AGENT', SELF_USER_AGENT);
98
500943a4
AD
99 ini_set('user_agent', SELF_USER_AGENT);
100
b0f379df 101 require_once 'lib/pubsubhubbub/publisher.php';
f4f0f80d 102
31303c6b 103 $purifier = false;
35ffb5b9 104
7d96bfcd
AD
105 $tz_offset = -1;
106 $utc_tz = new DateTimeZone('UTC');
107 $schema_version = false;
108
45004d43
AD
109 /**
110 * Print a timestamped debug message.
8d505d78 111 *
45004d43
AD
112 * @param string $msg The debug message.
113 * @return void
114 */
6f9e33e4
AD
115 function _debug($msg) {
116 $ts = strftime("%H:%M:%S", time());
2a6a9395
AD
117 if (function_exists('posix_getpid')) {
118 $ts = "$ts/" . posix_getpid();
119 }
6f9e33e4 120 print "[$ts] $msg\n";
45004d43 121 } // function _debug
6f9e33e4 122
9632f884
AD
123 /**
124 * Purge a feed old posts.
8d505d78 125 *
9632f884
AD
126 * @param mixed $link A database connection.
127 * @param mixed $feed_id The id of the purged feed.
128 * @param mixed $purge_interval Olderness of purged posts.
129 * @param boolean $debug Set to True to enable the debug. False by default.
130 * @access public
131 * @return void
132 */
ad507f85
AD
133 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
134
07d0efe9 135 if (!$purge_interval) $purge_interval = feed_purge_interval($link, $feed_id);
8d505d78 136
ad507f85 137 $rows = -1;
4c193675 138
8d505d78 139 $result = db_query($link,
07d0efe9
AD
140 "SELECT owner_uid FROM ttrss_feeds WHERE id = '$feed_id'");
141
142 $owner_uid = false;
143
144 if (db_num_rows($result) == 1) {
145 $owner_uid = db_fetch_result($result, 0, "owner_uid");
146 }
147
ab954dff
AD
148 if ($purge_interval == -1 || !$purge_interval) {
149 if ($owner_uid) {
150 ccache_update($link, $feed_id, $owner_uid);
151 }
152 return;
153 }
154
07d0efe9
AD
155 if (!$owner_uid) return;
156
3907ef71
AD
157 if (FORCE_ARTICLE_PURGE == 0) {
158 $purge_unread = get_pref($link, "PURGE_UNREAD_ARTICLES",
159 $owner_uid, false);
160 } else {
161 $purge_unread = true;
162 $purge_interval = FORCE_ARTICLE_PURGE;
163 }
07d0efe9
AD
164
165 if (!$purge_unread) $query_limit = " unread = false AND ";
166
fefa6ca3 167 if (DB_TYPE == "pgsql") {
6e7f8d26
AD
168 $pg_version = get_pgsql_version($link);
169
170 if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
1e59ae35 171
8d505d78
AD
172 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
173 ttrss_entries.id = ref_id AND
174 marked = false AND
175 feed_id = '$feed_id' AND
07d0efe9 176 $query_limit
25ea2805 177 ttrss_entries.date_updated < NOW() - INTERVAL '$purge_interval days'");
1e59ae35
AD
178
179 } else {
180
8d505d78
AD
181 $result = db_query($link, "DELETE FROM ttrss_user_entries
182 USING ttrss_entries
183 WHERE ttrss_entries.id = ref_id AND
184 marked = false AND
185 feed_id = '$feed_id' AND
07d0efe9 186 $query_limit
25ea2805 187 ttrss_entries.date_updated < NOW() - INTERVAL '$purge_interval days'");
1e59ae35 188 }
ad507f85
AD
189
190 $rows = pg_affected_rows($result);
8d505d78 191
fefa6ca3 192 } else {
8d505d78 193
30f1746f 194/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 195 marked = false AND feed_id = '$feed_id' AND
8d505d78 196 (SELECT date_updated FROM ttrss_entries WHERE
30f1746f
AD
197 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
198
8d505d78
AD
199 $result = db_query($link, "DELETE FROM ttrss_user_entries
200 USING ttrss_user_entries, ttrss_entries
201 WHERE ttrss_entries.id = ref_id AND
202 marked = false AND
203 feed_id = '$feed_id' AND
07d0efe9 204 $query_limit
25ea2805 205 ttrss_entries.date_updated < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
8d505d78 206
ad507f85
AD
207 $rows = mysql_affected_rows($link);
208
209 }
210
ced46404
AD
211 ccache_update($link, $feed_id, $owner_uid);
212
ad507f85 213 if ($debug) {
6f9e33e4 214 _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
fefa6ca3 215 }
9632f884 216 } // function purge_feed
fefa6ca3 217
07d0efe9
AD
218 function feed_purge_interval($link, $feed_id) {
219
8d505d78 220 $result = db_query($link, "SELECT purge_interval, owner_uid FROM ttrss_feeds
07d0efe9
AD
221 WHERE id = '$feed_id'");
222
223 if (db_num_rows($result) == 1) {
224 $purge_interval = db_fetch_result($result, 0, "purge_interval");
225 $owner_uid = db_fetch_result($result, 0, "owner_uid");
226
8d505d78 227 if ($purge_interval == 0) $purge_interval = get_pref($link,
863be6ca 228 'PURGE_OLD_DAYS', $owner_uid);
07d0efe9
AD
229
230 return $purge_interval;
231
232 } else {
233 return -1;
234 }
235 }
236
a2d79981
AD
237 function purge_orphans($link, $do_output = false) {
238
71604ca4 239 // purge orphaned posts in main content table
8d505d78 240 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
71604ca4 241 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
a2d79981
AD
242
243 if ($do_output) {
244 $rows = db_affected_rows($link, $result);
245 _debug("Purged $rows orphaned posts.");
246 }
c3a8d71a
AD
247 }
248
c7d57b66
AD
249 function get_feed_update_interval($link, $feed_id) {
250 $result = db_query($link, "SELECT owner_uid, update_interval FROM
251 ttrss_feeds WHERE id = '$feed_id'");
252
253 if (db_num_rows($result) == 1) {
254 $update_interval = db_fetch_result($result, 0, "update_interval");
255 $owner_uid = db_fetch_result($result, 0, "owner_uid");
256
257 if ($update_interval != 0) {
258 return $update_interval;
259 } else {
260 return get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $owner_uid, false);
261 }
262
263 } else {
264 return -1;
265 }
266 }
267
ae5f7bb1 268 function fetch_file_contents($url, $type = false, $login = false, $pass = false, $post_query = false) {
8d505d78
AD
269 $login = urlencode($login);
270 $pass = urlencode($pass);
271
3610b48b 272 if (function_exists('curl_init') && !ini_get("open_basedir")) {
4065b60b 273 $ch = curl_init($url);
a1af1574
AD
274
275 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
276 curl_setopt($ch, CURLOPT_TIMEOUT, 45);
277 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
278 curl_setopt($ch, CURLOPT_MAXREDIRS, 20);
279 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
280 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
8d505d78 281 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
5f6804bc 282 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
19929bbe 283 curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
268a06dc 284 curl_setopt($ch, CURLOPT_ENCODING , "gzip");
8d505d78 285
ae5f7bb1
AD
286 if ($post_query) {
287 curl_setopt($ch, CURLOPT_POST, true);
288 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_query);
289 }
290
8d505d78
AD
291 if ($login && $pass)
292 curl_setopt($ch, CURLOPT_USERPWD, "$login:$pass");
a1af1574 293
fb074239 294 $contents = @curl_exec($ch);
268a06dc 295
a1af1574
AD
296 if ($contents === false) {
297 curl_close($ch);
298 return false;
4065b60b
AD
299 }
300
8d505d78 301 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
a1af1574
AD
302 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
303 curl_close($ch);
4065b60b 304
8d505d78 305 if ($http_code != 200 || $type && strpos($content_type, "$type") === false) {
a1af1574
AD
306 return false;
307 }
4065b60b 308
a1af1574 309 return $contents;
4065b60b 310 } else {
9949bd15 311 if ($login && $pass ){
8d505d78
AD
312 $url_parts = array();
313
314 preg_match("/(^[^:]*):\/\/(.*)/", $url, $url_parts);
315
316 if ($url_parts[1] && $url_parts[2]) {
317 $url = $url_parts[1] . "://$login:$pass@" . $url_parts[2];
318 }
319 }
320
fb074239 321 return @file_get_contents($url);
4065b60b
AD
322 }
323
324 }
78800912 325
9632f884
AD
326 /**
327 * Try to determine the favicon URL for a feed.
328 * adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
329 * http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
8d505d78 330 *
9632f884
AD
331 * @param string $url A feed or page URL
332 * @access public
333 * @return mixed The favicon URL, or false if none was found.
334 */
4065b60b 335 function get_favicon_url($url) {
99331724 336
ed214298
AD
337 $favicon_url = false;
338
4065b60b 339 if ($html = @fetch_file_contents($url)) {
78800912 340
ed214298 341 libxml_use_internal_errors(true);
c798704b 342
ed214298
AD
343 $doc = new DOMDocument();
344 $doc->loadHTML($html);
345 $xpath = new DOMXPath($doc);
717f5e64 346
a712429e
AD
347 $base = $xpath->query('/html/head/base');
348 foreach ($base as $b) {
349 $url = $b->getAttribute("href");
350 break;
351 }
352
353 $entries = $xpath->query('/html/head/link[@rel="shortcut icon" or @rel="icon"]');
ed214298
AD
354 if (count($entries) > 0) {
355 foreach ($entries as $entry) {
356 $favicon_url = rewrite_relative_url($url, $entry->getAttribute("href"));
357 break;
358 }
8d505d78 359 }
4065b60b 360 }
c798704b 361
ed214298
AD
362 if (!$favicon_url)
363 $favicon_url = rewrite_relative_url($url, "/favicon.ico");
364
014d3ad8 365 return $favicon_url;
9632f884 366 } // function get_favicon_url
4065b60b 367
4065b60b 368 function check_feed_favicon($site_url, $feed, $link) {
4065b60b
AD
369# print "FAVICON [$site_url]: $favicon_url\n";
370
4065b60b
AD
371 $icon_file = ICONS_DIR . "/$feed.ico";
372
c83a5aec
AD
373 if (!file_exists($icon_file)) {
374 $favicon_url = get_favicon_url($site_url);
375
376 if ($favicon_url) {
377 $contents = fetch_file_contents($favicon_url, "image");
378
379 if ($contents) {
e894e97f 380 $fp = @fopen($icon_file, "w");
78800912 381
c83a5aec
AD
382 if ($fp) {
383 fwrite($fp, $contents);
384 fclose($fp);
385 chmod($icon_file, 0644);
386 }
a1af1574 387 }
4065b60b 388 }
78800912
AD
389 }
390 }
391
f175937c 392 function print_select($id, $default, $values, $attributes = "") {
79f3553b 393 print "<select name=\"$id\" id=\"$id\" $attributes>";
a0d53889
AD
394 foreach ($values as $v) {
395 if ($v == $default)
60807300 396 $sel = "selected=\"1\"";
a0d53889
AD
397 else
398 $sel = "";
8d505d78 399
60807300 400 print "<option value=\"$v\" $sel>$v</option>";
a0d53889
AD
401 }
402 print "</select>";
403 }
40d13c28 404
79f3553b
AD
405 function print_select_hash($id, $default, $values, $attributes = "") {
406 print "<select name=\"$id\" id='$id' $attributes>";
673d54ca
AD
407 foreach (array_keys($values) as $v) {
408 if ($v == $default)
74d5c8fa 409 $sel = 'selected="selected"';
673d54ca
AD
410 else
411 $sel = "";
8d505d78 412
673d54ca
AD
413 print "<option $sel value=\"$v\">".$values[$v]."</option>";
414 }
415
416 print "</select>";
417 }
418
c3fc5e47 419 function get_article_filters($filters, $title, $content, $link, $timestamp, $author, $tags) {
240054f1 420 $matches = array();
c2d9322b 421
240054f1
AD
422 if ($filters["title"]) {
423 foreach ($filters["title"] as $filter) {
8d505d78
AD
424 $reg_exp = $filter["reg_exp"];
425 $inverse = $filter["inverse"];
426 if ((!$inverse && @preg_match("/$reg_exp/i", $title)) ||
a9d63d29 427 ($inverse && !@preg_match("/$reg_exp/i", $title))) {
c2d9322b 428
240054f1
AD
429 array_push($matches, array($filter["action"], $filter["action_param"]));
430 }
431 }
432 }
433
434 if ($filters["content"]) {
435 foreach ($filters["content"] as $filter) {
c2d9322b
AD
436 $reg_exp = $filter["reg_exp"];
437 $inverse = $filter["inverse"];
438
8d505d78 439 if ((!$inverse && @preg_match("/$reg_exp/i", $content)) ||
a9d63d29 440 ($inverse && !@preg_match("/$reg_exp/i", $content))) {
c2d9322b 441
240054f1 442 array_push($matches, array($filter["action"], $filter["action_param"]));
8d505d78 443 }
240054f1
AD
444 }
445 }
446
447 if ($filters["both"]) {
8d505d78
AD
448 foreach ($filters["both"] as $filter) {
449 $reg_exp = $filter["reg_exp"];
c2d9322b
AD
450 $inverse = $filter["inverse"];
451
452 if ($inverse) {
a9d63d29 453 if (!@preg_match("/$reg_exp/i", $title) && !preg_match("/$reg_exp/i", $content)) {
c2d9322b
AD
454 array_push($matches, array($filter["action"], $filter["action_param"]));
455 }
456 } else {
a9d63d29 457 if (@preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) {
c2d9322b
AD
458 array_push($matches, array($filter["action"], $filter["action_param"]));
459 }
240054f1
AD
460 }
461 }
462 }
463
464 if ($filters["link"]) {
465 $reg_exp = $filter["reg_exp"];
466 foreach ($filters["link"] as $filter) {
467 $reg_exp = $filter["reg_exp"];
c2d9322b
AD
468 $inverse = $filter["inverse"];
469
8d505d78 470 if ((!$inverse && @preg_match("/$reg_exp/i", $link)) ||
a9d63d29 471 ($inverse && !@preg_match("/$reg_exp/i", $link))) {
8d505d78 472
240054f1
AD
473 array_push($matches, array($filter["action"], $filter["action_param"]));
474 }
475 }
476 }
477
44d0e774
AD
478 if ($filters["date"]) {
479 $reg_exp = $filter["reg_exp"];
480 foreach ($filters["date"] as $filter) {
481 $date_modifier = $filter["filter_param"];
482 $inverse = $filter["inverse"];
483 $check_timestamp = strtotime($filter["reg_exp"]);
484
485 # no-op when timestamp doesn't parse to prevent misfires
486
487 if ($check_timestamp) {
488 $match_ok = false;
489
490 if ($date_modifier == "before" && $timestamp < $check_timestamp ||
491 $date_modifier == "after" && $timestamp > $check_timestamp) {
492 $match_ok = true;
493 }
494
495 if ($inverse) $match_ok = !$match_ok;
496
497 if ($match_ok) {
498 array_push($matches, array($filter["action"], $filter["action_param"]));
499 }
500 }
501 }
8d505d78 502 }
44d0e774 503
fa3317be
AD
504 if ($filters["author"]) {
505 foreach ($filters["author"] as $filter) {
8d505d78
AD
506 $reg_exp = $filter["reg_exp"];
507 $inverse = $filter["inverse"];
508 if ((!$inverse && @preg_match("/$reg_exp/i", $author)) ||
a9d63d29 509 ($inverse && !@preg_match("/$reg_exp/i", $author))) {
fa3317be
AD
510
511 array_push($matches, array($filter["action"], $filter["action_param"]));
512 }
513 }
514 }
515
c3fc5e47
AD
516 if ($filters["tag"]) {
517
518 $tag_string = join(",", $tags);
519
520 foreach ($filters["tag"] as $filter) {
521 $reg_exp = $filter["reg_exp"];
522 $inverse = $filter["inverse"];
523
8d505d78 524 if ((!$inverse && @preg_match("/$reg_exp/i", $tag_string)) ||
a9d63d29 525 ($inverse && !@preg_match("/$reg_exp/i", $tag_string))) {
c3fc5e47
AD
526
527 array_push($matches, array($filter["action"], $filter["action_param"]));
8d505d78 528 }
c3fc5e47
AD
529 }
530 }
531
532
240054f1
AD
533 return $matches;
534 }
535
f8382011
AD
536 function find_article_filter($filters, $filter_name) {
537 foreach ($filters as $f) {
538 if ($f[0] == $filter_name) {
539 return $f;
540 };
541 }
542 return false;
543 }
544
ff6e357a
AD
545 function calculate_article_score($filters) {
546 $score = 0;
547
548 foreach ($filters as $f) {
549 if ($f[0] == "score") {
550 $score += $f[1];
551 };
552 }
553 return $score;
554 }
555
ceb30ba4
AD
556 function assign_article_to_labels($link, $id, $filters, $owner_uid) {
557 foreach ($filters as $f) {
558 if ($f[0] == "label") {
559 label_add_article($link, $id, $f[1], $owner_uid);
560 };
561 }
562 }
ff6e357a 563
406d9489
AD
564 function getmicrotime() {
565 list($usec, $sec) = explode(" ",microtime());
566 return ((float)$usec + (float)$sec);
567 }
568
f541eb78 569 function print_radio($id, $default, $true_is, $values, $attributes = "") {
77e96719 570 foreach ($values as $v) {
8d505d78 571
77e96719 572 if ($v == $default)
5da169d9 573 $sel = "checked";
77e96719 574 else
5da169d9
AD
575 $sel = "";
576
f541eb78 577 if ($v == $true_is) {
5da169d9
AD
578 $sel .= " value=\"1\"";
579 } else {
580 $sel .= " value=\"0\"";
581 }
8d505d78
AD
582
583 print "<input class=\"noborder\" dojoType=\"dijit.form.RadioButton\"
69654950 584 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
77e96719
AD
585
586 }
587 }
588
d9084cf2 589 function initialize_user_prefs($link, $uid, $profile = false) {
ff485f1d
AD
590
591 $uid = db_escape_string($uid);
592
d9084cf2
AD
593 if (!$profile) {
594 $profile = "NULL";
f9aa6a89 595 $profile_qpart = "AND profile IS NULL";
d9084cf2 596 } else {
f9aa6a89 597 $profile_qpart = "AND profile = '$profile'";
d9084cf2
AD
598 }
599
f9aa6a89
AD
600 if (get_schema_version($link) < 63) $profile_qpart = "";
601
ff485f1d
AD
602 db_query($link, "BEGIN");
603
604 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
8d505d78
AD
605
606 $u_result = db_query($link, "SELECT pref_name
f9aa6a89 607 FROM ttrss_user_prefs WHERE owner_uid = '$uid' $profile_qpart");
ff485f1d
AD
608
609 $active_prefs = array();
610
611 while ($line = db_fetch_assoc($u_result)) {
8d505d78 612 array_push($active_prefs, $line["pref_name"]);
ff485f1d
AD
613 }
614
615 while ($line = db_fetch_assoc($result)) {
616 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
617// print "adding " . $line["pref_name"] . "<br>";
618
f9aa6a89
AD
619 if (get_schema_version($link) < 63) {
620 db_query($link, "INSERT INTO ttrss_user_prefs
8d505d78 621 (owner_uid,pref_name,value) VALUES
f9aa6a89
AD
622 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
623
624 } else {
625 db_query($link, "INSERT INTO ttrss_user_prefs
8d505d78 626 (owner_uid,pref_name,value, profile) VALUES
f9aa6a89
AD
627 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."', $profile)");
628 }
ff485f1d
AD
629
630 }
631 }
632
633 db_query($link, "COMMIT");
634
635 }
956c7629 636
8de8bfb8
AD
637 function get_ssl_certificate_id() {
638 if ($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"]) {
639 return sha1($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"] .
640 $_SERVER["REDIRECT_SSL_CLIENT_V_START"] .
641 $_SERVER["REDIRECT_SSL_CLIENT_V_END"] .
642 $_SERVER["REDIRECT_SSL_CLIENT_S_DN"]);
643 }
644 return "";
645 }
646
3d72afa1
AD
647 function get_login_by_ssl_certificate($link) {
648
8de8bfb8 649 $cert_serial = db_escape_string(get_ssl_certificate_id());
3d72afa1
AD
650
651 if ($cert_serial) {
652 $result = db_query($link, "SELECT login FROM ttrss_user_prefs, ttrss_users
653 WHERE pref_name = 'SSL_CERT_SERIAL' AND value = '$cert_serial' AND
654 owner_uid = ttrss_users.id");
655
656 if (db_num_rows($result) != 0) {
657 return db_escape_string(db_fetch_result($result, 0, "login"));
658 }
659 }
660
661 return "";
662 }
663
bf9fc060 664 function get_remote_user($link) {
f98252f2 665
3d72afa1 666 if (defined('ALLOW_REMOTE_USER_AUTH') && ALLOW_REMOTE_USER_AUTH) {
bf9fc060 667 return db_escape_string($_SERVER["REMOTE_USER"]);
3d72afa1 668 }
f98252f2 669
bf9fc060 670 return db_escape_string(get_login_by_ssl_certificate($link));
f98252f2
AD
671 }
672
bf9fc060
AD
673 function get_remote_fakepass($link) {
674 if (get_remote_user($link))
f98252f2
AD
675 return "******";
676 else
677 return "";
678 }
679
461766f3 680 function authenticate_user($link, $login, $password, $force_auth = false) {
c8437f35 681
131b01b3 682 if (!SINGLE_USER_MODE) {
c8437f35 683
1a9f4d3c
AD
684 $pwd_hash1 = encrypt_password($password);
685 $pwd_hash2 = encrypt_password($password, $login);
2d969845 686 $login = db_escape_string($login);
461766f3 687
bf9fc060 688 $remote_user = get_remote_user($link);
66917e70 689
c9b63492 690 if ($remote_user && $remote_user == $login && $login != "admin") {
3d72afa1
AD
691
692 $login = $remote_user;
66917e70 693
73f5f114 694 $query = "SELECT id,login,access_level,pwd_hash
461766f3 695 FROM ttrss_users WHERE
66917e70
AD
696 login = '$login'";
697
42315f8d
AD
698 if (defined('AUTO_CREATE_USER') && AUTO_CREATE_USER
699 && $_SERVER["REMOTE_USER"]) {
700 $result = db_query($link, $query);
701
702 // First login ?
703 if (db_num_rows($result) == 0) {
6ad2bbbc 704 $query2 = "INSERT INTO ttrss_users
42315f8d
AD
705 (login,access_level,last_login,created)
706 VALUES ('$login', 0, null, NOW())";
6ad2bbbc 707 db_query($link, $query2);
42315f8d
AD
708 }
709 }
710
461766f3 711 } else {
1a9f4d3c 712 $query = "SELECT id,login,access_level,pwd_hash
461766f3 713 FROM ttrss_users WHERE
1a9f4d3c
AD
714 login = '$login' AND (pwd_hash = '$pwd_hash1' OR
715 pwd_hash = '$pwd_hash2')";
461766f3
AD
716 }
717
718 $result = db_query($link, $query);
8d505d78 719
131b01b3
AD
720 if (db_num_rows($result) == 1) {
721 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
722 $_SESSION["name"] = db_fetch_result($result, 0, "login");
723 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
8d505d78
AD
724
725 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
131b01b3 726 $_SESSION["uid"]);
8d505d78 727
42315f8d
AD
728
729 // LemonLDAP can send user informations via HTTP HEADER
730 if (defined('AUTO_CREATE_USER') && AUTO_CREATE_USER){
731 // update user name
8efb5f62
AD
732 $fullname = $_SERVER['HTTP_USER_NAME'] ? $_SERVER['HTTP_USER_NAME'] : $_SERVER['AUTHENTICATE_CN'];
733 if ($fullname){
734 $fullname = db_escape_string($fullname);
42315f8d
AD
735 db_query($link, "UPDATE ttrss_users SET full_name = '$fullname' WHERE id = " .
736 $_SESSION["uid"]);
737 }
738 // update user mail
8efb5f62
AD
739 $email = $_SERVER['HTTP_USER_MAIL'] ? $_SERVER['HTTP_USER_MAIL'] : $_SERVER['AUTHENTICATE_MAIL'];
740 if ($email){
741 $email = db_escape_string($email);
42315f8d
AD
742 db_query($link, "UPDATE ttrss_users SET email = '$email' WHERE id = " .
743 $_SESSION["uid"]);
744 }
745 }
746
131b01b3 747 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1a9f4d3c 748 $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash");
91c5f229
AD
749
750 $_SESSION["last_version_check"] = time();
8d505d78 751
131b01b3 752 initialize_user_prefs($link, $_SESSION["uid"]);
8d505d78 753
131b01b3
AD
754 return true;
755 }
8d505d78 756
131b01b3 757 return false;
503eb349 758
131b01b3 759 } else {
503eb349 760
131b01b3
AD
761 $_SESSION["uid"] = 1;
762 $_SESSION["name"] = "admin";
f557cd78 763
0bbba72d 764 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
8d505d78 765
0bbba72d 766 initialize_user_prefs($link, $_SESSION["uid"]);
8d505d78 767
c8437f35
AD
768 return true;
769 }
c8437f35
AD
770 }
771
e6cb77a0
AD
772 function make_password($length = 8) {
773
774 $password = "";
8d505d78
AD
775 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
776
777 $i = 0;
778
779 while ($i < $length) {
e6cb77a0 780 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
8d505d78
AD
781
782 if (!strstr($password, $char)) {
e6cb77a0
AD
783 $password .= $char;
784 $i++;
785 }
786 }
787 return $password;
788 }
789
790 // this is called after user is created to initialize default feeds, labels
791 // or whatever else
8d505d78 792
e6cb77a0
AD
793 // user preferences are checked on every login, not here
794
795 function initialize_user($link, $uid) {
796
e6cb77a0 797 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 798 values ('$uid', 'Tiny Tiny RSS: New Releases',
b6d486a3 799 'http://tt-rss.org/releases.rss')");
3b0feb9b 800
cd2cd415
AD
801 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
802 values ('$uid', 'Tiny Tiny RSS: Forum',
f0855b88 803 'http://tt-rss.org/forum/rss.php')");
3b0feb9b 804 }
e6cb77a0 805
b8aa49bc 806 function logout_user() {
5ccc1cf5
AD
807 session_destroy();
808 if (isset($_COOKIE[session_name()])) {
809 setcookie(session_name(), '', time()-42000, '/');
810 }
b8aa49bc
AD
811 }
812
916f788a 813 function validate_session($link) {
0f41fce8
AD
814 if (SINGLE_USER_MODE) return true;
815
816 $check_ip = $_SESSION['ip_address'];
817
818 switch (SESSION_CHECK_ADDRESS) {
819 case 0:
820 $check_ip = '';
821 break;
822 case 1:
823 $check_ip = substr($check_ip, 0, strrpos($check_ip, '.')+1);
824 break;
825 case 2:
826 $check_ip = substr($check_ip, 0, strrpos($check_ip, '.'));
827 $check_ip = substr($check_ip, 0, strrpos($check_ip, '.')+1);
828 break;
829 };
830
d769a0f7 831 if ($check_ip && strpos($_SERVER['REMOTE_ADDR'], $check_ip) !== 0) {
8d505d78 832 $_SESSION["login_error_msg"] =
d769a0f7
AD
833 __("Session failed to validate (incorrect IP)");
834 return false;
835 }
0f41fce8
AD
836
837 if ($_SESSION["ref_schema_version"] != get_schema_version($link, true))
05044a59 838 return false;
05044a59 839
e6684130
AD
840 if ($_SESSION["uid"]) {
841
8d505d78 842 $result = db_query($link,
e6684130
AD
843 "SELECT pwd_hash FROM ttrss_users WHERE id = '".$_SESSION["uid"]."'");
844
845 $pwd_hash = db_fetch_result($result, 0, "pwd_hash");
846
847 if ($pwd_hash != $_SESSION["pwd_hash"]) {
848 return false;
849 }
850 }
851
a885f0ec 852/* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
d620cfe7 853
8e849206 854 //print_r($_SESSION);
d620cfe7
AD
855
856 if (time() > $_SESSION["cookie_lifetime"]) {
857 return false;
858 }
a885f0ec
AD
859 } */
860
916f788a
AD
861 return true;
862 }
863
793185a9 864 function login_sequence($link, $mobile = false) {
c648997b
AD
865 $_SESSION["prefs_cache"] = array();
866
b8aa49bc 867 if (!SINGLE_USER_MODE) {
75836f33 868
7f0acba7 869 $login_action = $_POST["login_action"];
a885f0ec 870
8d505d78 871 # try to authenticate user if called from login form
7f0acba7 872 if ($login_action == "do_login") {
92decf4f 873 $login = db_escape_string($_POST["login"]);
4044a5fa 874 $password = $_POST["password"];
d620cfe7 875 $remember_me = $_POST["remember_me"];
f557cd78 876
01a87dff
AD
877 if (authenticate_user($link, $login, $password)) {
878 $_POST["password"] = "";
d620cfe7 879
f8c612d4 880 $_SESSION["language"] = $_POST["language"];
05044a59 881 $_SESSION["ref_schema_version"] = get_schema_version($link, true);
a598370d 882 $_SESSION["bw_limit"] = !!$_POST["bw_limit"];
f8c612d4 883
d9084cf2
AD
884 if ($_POST["profile"]) {
885
886 $profile = db_escape_string($_POST["profile"]);
887
888 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
889 WHERE id = '$profile' AND owner_uid = " . $_SESSION["uid"]);
890
891 if (db_num_rows($result) != 0) {
892 $_SESSION["profile"] = $profile;
893 $_SESSION["prefs_cache"] = array();
894 }
895 }
896
6615cc36
AD
897 if ($_REQUEST['return']) {
898 header("Location: " . $_REQUEST['return']);
899 } else {
900 header("Location: " . $_SERVER["REQUEST_URI"]);
901 }
902
d620cfe7
AD
903 exit;
904
01a87dff 905 return;
7f0acba7 906 } else {
af163b85 907 $_SESSION["login_error_msg"] = __("Incorrect username or password");
01a87dff
AD
908 }
909 }
910
7f0acba7 911 if (!$_SESSION["uid"] || !validate_session($link)) {
3d72afa1 912
bf9fc060
AD
913 if (get_remote_user($link) && AUTO_LOGIN) {
914 authenticate_user($link, get_remote_user($link), null);
12df6592
AD
915 $_SESSION["ref_schema_version"] = get_schema_version($link, true);
916 } else {
917 render_login_form($link, $mobile);
918 //header("Location: login.php");
919 exit;
920 }
d3687e7a
AD
921 } else {
922 /* bump login timestamp */
8d505d78 923 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
d3687e7a 924 $_SESSION["uid"]);
019bd5a9 925
d54780bc 926 if ($_SESSION["language"] && SESSION_COOKIE_LIFETIME > 0) {
8d505d78 927 setcookie("ttrss_lang", $_SESSION["language"],
019bd5a9
AD
928 time() + SESSION_COOKIE_LIFETIME);
929 }
3261ca58
AD
930
931 // try to remove possible duplicates from feed counter cache
147f5632 932// ccache_cleanup($link, $_SESSION["uid"]);
b8aa49bc 933 }
d620cfe7 934
b8aa49bc 935 } else {
0bbba72d 936 return authenticate_user($link, "admin", null);
b8aa49bc
AD
937 }
938 }
3547842a 939
411fe209 940 function truncate_string($str, $max_len, $suffix = '&hellip;') {
12db369c 941 if (mb_strlen($str, "utf-8") > $max_len - 3) {
411fe209 942 return mb_substr($str, 0, $max_len, "utf-8") . $suffix;
3547842a
AD
943 } else {
944 return $str;
945 }
946 }
54a60e1a 947
e9823609 948 function theme_image($link, $filename) {
883fee8d
AD
949 if ($link) {
950 $theme_path = get_user_theme_path($link);
e9823609 951
883fee8d
AD
952 if ($theme_path && is_file($theme_path.$filename)) {
953 return $theme_path.$filename;
954 } else {
955 return $filename;
956 }
e9823609
AD
957 } else {
958 return $filename;
959 }
960 }
961
dce46cad 962 function get_user_theme($link) {
e4c51a6c 963
b92fbcd8 964 if (get_schema_version($link) >= 63 && $_SESSION["uid"]) {
b97e6e02
AD
965 $theme_name = get_pref($link, "_THEME_ID");
966 if (is_dir("themes/$theme_name")) {
967 return $theme_name;
968 } else {
969 return '';
970 }
e4c51a6c 971 } else {
b97e6e02 972 return '';
e4c51a6c 973 }
d9084cf2 974
dce46cad
AD
975 }
976
977 function get_user_theme_path($link) {
c3fddd05 978 $theme_path = '';
dce46cad 979
b92fbcd8 980 if (get_schema_version($link) >= 63 && $_SESSION["uid"]) {
dce46cad
AD
981 $theme_name = get_pref($link, "_THEME_ID");
982
b97e6e02 983 if ($theme_name && is_dir("themes/$theme_name")) {
dce46cad 984 $theme_path = "themes/$theme_name/";
6ba50622 985 } else {
dce46cad 986 $theme_name = '';
6ba50622 987 }
54a60e1a 988 } else {
dce46cad
AD
989 $theme_path = '';
990 }
991
0c425dc7 992 if ($theme_path) {
8d3cb8c0
AD
993 if (is_file("$theme_path/theme.ini")) {
994 $ini = parse_ini_file("$theme_path/theme.ini", true);
995 if ($ini['theme']['version'] >= THEME_VERSION_REQUIRED) {
0c425dc7
AD
996 return $theme_path;
997 }
998 }
999 }
1000 return '';
dce46cad
AD
1001 }
1002
e71f2610
AD
1003 function get_user_theme_options($link) {
1004 $t = get_user_theme_path($link);
1005
1006 if ($t) {
1007 if (is_file("$t/theme.ini")) {
1008 $ini = parse_ini_file("$t/theme.ini", true);
1009 if ($ini['theme']['version']) {
1010 return $ini['theme']['options'];
1011 }
1012 }
1013 }
f1f3a642 1014 return '';
e71f2610
AD
1015 }
1016
8d3cb8c0
AD
1017 function print_theme_includes($link) {
1018
1019 $t = get_user_theme_path($link);
1020 $time = time();
1021
1022 if ($t) {
8d505d78 1023 print "<link rel=\"stylesheet\" type=\"text/css\"
8d3cb8c0
AD
1024 href=\"$t/theme.css?$time \">";
1025 if (file_exists("$t/theme.js")) {
1026 print "<script type=\"text/javascript\" src=\"$t/theme.js?$time\">
1027 </script>";
1028 }
1029 }
1030 }
e71f2610 1031
dce46cad
AD
1032 function get_all_themes() {
1033 $themes = glob("themes/*");
1034
b97e6e02
AD
1035 asort($themes);
1036
dce46cad
AD
1037 $rv = array();
1038
1039 foreach ($themes as $t) {
1040 if (is_file("$t/theme.ini")) {
1041 $ini = parse_ini_file("$t/theme.ini", true);
8d505d78 1042 if ($ini['theme']['version'] >= THEME_VERSION_REQUIRED &&
0c425dc7 1043 !$ini['theme']['disabled']) {
dce46cad
AD
1044 $entry = array();
1045 $entry["path"] = $t;
1046 $entry["base"] = basename($t);
1047 $entry["name"] = $ini['theme']['name'];
1048 $entry["version"] = $ini['theme']['version'];
1049 $entry["author"] = $ini['theme']['author'];
e71f2610 1050 $entry["options"] = $ini['theme']['options'];
dce46cad
AD
1051 array_push($rv, $entry);
1052 }
1053 }
54a60e1a 1054 }
dce46cad
AD
1055
1056 return $rv;
54a60e1a 1057 }
be773442 1058
ab4b768f
AD
1059 function convert_timestamp($timestamp, $source_tz, $dest_tz) {
1060
1061 try {
1062 $source_tz = new DateTimeZone($source_tz);
1063 } catch (Exception $e) {
1064 $source_tz = new DateTimeZone('UTC');
1065 }
1066
1067 try {
1068 $dest_tz = new DateTimeZone($dest_tz);
1069 } catch (Exception $e) {
1070 $dest_tz = new DateTimeZone('UTC');
1071 }
1072
1073 $dt = new DateTime(date('Y-m-d H:i:s', $timestamp), $source_tz);
1074 return $dt->format('U') + $dest_tz->getOffset($dt);
1075 }
1076
324944f3
AD
1077 function make_local_datetime($link, $timestamp, $long, $owner_uid = false,
1078 $no_smart_dt = false) {
1079
1080 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
1081 if (!$timestamp) $timestamp = '1970-01-01 0:00';
1082
7d96bfcd
AD
1083 global $utc_tz;
1084 global $tz_offset;
324944f3 1085
7d96bfcd
AD
1086 # We store date in UTC internally
1087 $dt = new DateTime($timestamp, $utc_tz);
1088
1089 if ($tz_offset == -1) {
1090
1091 $user_tz_string = get_pref($link, 'USER_TIMEZONE', $owner_uid);
1092
1093 try {
1094 $user_tz = new DateTimeZone($user_tz_string);
1095 } catch (Exception $e) {
1096 $user_tz = $utc_tz;
1097 }
1098
1099 $tz_offset = $user_tz->getOffset($dt);
324944f3
AD
1100 }
1101
7d96bfcd 1102 $user_timestamp = $dt->format('U') + $tz_offset;
324944f3 1103
1dc52ae7 1104 if (!$no_smart_dt) {
8d505d78 1105 return smart_date_time($link, $user_timestamp,
7d96bfcd 1106 $tz_offset, $owner_uid);
324944f3
AD
1107 } else {
1108 if ($long)
1109 $format = get_pref($link, 'LONG_DATE_FORMAT', $owner_uid);
1110 else
1111 $format = get_pref($link, 'SHORT_DATE_FORMAT', $owner_uid);
1112
1113 return date($format, $user_timestamp);
1114 }
1115 }
1116
2a5c136e
AD
1117 function smart_date_time($link, $timestamp, $tz_offset = 0, $owner_uid = false) {
1118 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
1119
1120 if (date("Y.m.d", $timestamp) == date("Y.m.d", time() + $tz_offset)) {
be773442 1121 return date("G:i", $timestamp);
2a5c136e
AD
1122 } else if (date("Y", $timestamp) == date("Y", time() + $tz_offset)) {
1123 $format = get_pref($link, 'SHORT_DATE_FORMAT', $owner_uid);
1124 return date($format, $timestamp);
be773442 1125 } else {
2a5c136e
AD
1126 $format = get_pref($link, 'LONG_DATE_FORMAT', $owner_uid);
1127 return date($format, $timestamp);
be773442
AD
1128 }
1129 }
1130
e3c99f3b 1131 function sql_bool_to_bool($s) {
36184020 1132 if ($s == "t" || $s == "1" || $s == "true") {
e3c99f3b
AD
1133 return true;
1134 } else {
1135 return false;
1136 }
1137 }
8d505d78 1138
badac687
AD
1139 function bool_to_sql_bool($s) {
1140 if ($s) {
1141 return "true";
1142 } else {
1143 return "false";
1144 }
1145 }
e3c99f3b 1146
fcfa9ef1
AD
1147 // Session caching removed due to causing wrong redirects to upgrade
1148 // script when get_schema_version() is called on an obsolete session
1149 // created on a previous schema version.
199db684 1150 function get_schema_version($link, $nocache = false) {
7d96bfcd
AD
1151 global $schema_version;
1152
1153 if (!$schema_version) {
199db684
AD
1154 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1155 $version = db_fetch_result($result, 0, "schema_version");
7d96bfcd 1156 $schema_version = $version;
199db684 1157 return $version;
7d96bfcd
AD
1158 } else {
1159 return $schema_version;
1160 }
e4c51a6c
AD
1161 }
1162
6043fb7e 1163 function sanity_check($link) {
31303c6b 1164 require_once 'errors.php';
ebb948c2 1165
6043fb7e 1166 $error_code = 0;
7d96bfcd 1167 $schema_version = get_schema_version($link, true);
6043fb7e
AD
1168
1169 if ($schema_version != SCHEMA_VERSION) {
1170 $error_code = 5;
1171 }
1172
aec3ce39
AD
1173 if (DB_TYPE == "mysql") {
1174 $result = db_query($link, "SELECT true", false);
1175 if (db_num_rows($result) != 1) {
1176 $error_code = 10;
1177 }
1178 }
1179
f29ba148
AD
1180 if (db_escape_string("testTEST") != "testTEST") {
1181 $error_code = 12;
1182 }
1183
ebb948c2 1184 return array("code" => $error_code, "message" => $ERRORS[$error_code]);
6043fb7e
AD
1185 }
1186
27981ca3 1187 function file_is_locked($filename) {
31a6d42d 1188 if (function_exists('flock')) {
fb074239 1189 $fp = @fopen(LOCK_DIRECTORY . "/$filename", "r");
31a6d42d
AD
1190 if ($fp) {
1191 if (flock($fp, LOCK_EX | LOCK_NB)) {
1192 flock($fp, LOCK_UN);
1193 fclose($fp);
1194 return false;
1195 }
27981ca3 1196 fclose($fp);
31a6d42d 1197 return true;
e89aed7b
AD
1198 } else {
1199 return false;
27981ca3 1200 }
27981ca3 1201 }
c1fb4a5e 1202 return true; // consider the file always locked and skip the test
27981ca3
AD
1203 }
1204
fcb4c0c9 1205 function make_lockfile($filename) {
cfa43e02 1206 $fp = fopen(LOCK_DIRECTORY . "/$filename", "w");
fcb4c0c9 1207
82acc36d 1208 if (flock($fp, LOCK_EX | LOCK_NB)) {
4c59adb1
AD
1209 if (function_exists('posix_getpid')) {
1210 fwrite($fp, posix_getpid() . "\n");
1211 }
fcb4c0c9
AD
1212 return $fp;
1213 } else {
1214 return false;
1215 }
1216 }
1217
bf7fcde8 1218 function make_stampfile($filename) {
cfa43e02 1219 $fp = fopen(LOCK_DIRECTORY . "/$filename", "w");
bf7fcde8 1220
8e00ae9b 1221 if (flock($fp, LOCK_EX | LOCK_NB)) {
bf7fcde8 1222 fwrite($fp, time() . "\n");
8e00ae9b 1223 flock($fp, LOCK_UN);
bf7fcde8
AD
1224 fclose($fp);
1225 return true;
1226 } else {
1227 return false;
1228 }
1229 }
1230
894ebcf5
AD
1231 function sql_random_function() {
1232 if (DB_TYPE == "mysql") {
1233 return "RAND()";
1234 } else {
1235 return "RANDOM()";
1236 }
1237 }
1238
d36f5607 1239 function catchup_feed($link, $feed, $cat_view, $owner_uid = false) {
c7e51de1
AD
1240
1241 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
88040f57 1242
37c03d3a 1243 //if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
22fdebff 1244
37c03d3a 1245 if (is_numeric($feed)) {
23aa0d16
AD
1246 if ($cat_view) {
1247
72a2f4f5 1248 if ($feed >= 0) {
f9fca8cb
AD
1249
1250 if ($feed > 0) {
1251 $cat_qpart = "cat_id = '$feed'";
1252 } else {
1253 $cat_qpart = "cat_id IS NULL";
1254 }
8d505d78
AD
1255
1256 $tmp_result = db_query($link, "SELECT id
c7e51de1 1257 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = $owner_uid");
f9fca8cb
AD
1258
1259 while ($tmp_line = db_fetch_assoc($tmp_result)) {
23aa0d16 1260
f9fca8cb 1261 $tmp_feed = $tmp_line["id"];
23aa0d16 1262
8d505d78
AD
1263 db_query($link, "UPDATE ttrss_user_entries
1264 SET unread = false,last_read = NOW()
c7e51de1 1265 WHERE feed_id = '$tmp_feed' AND owner_uid = $owner_uid");
f9fca8cb
AD
1266 }
1267 } else if ($feed == -2) {
23aa0d16 1268
8d505d78
AD
1269 db_query($link, "UPDATE ttrss_user_entries
1270 SET unread = false,last_read = NOW() WHERE (SELECT COUNT(*)
1271 FROM ttrss_user_labels2 WHERE article_id = ref_id) > 0
c7e51de1 1272 AND unread = true AND owner_uid = $owner_uid");
23aa0d16
AD
1273 }
1274
1275 } else if ($feed > 0) {
1276
8d505d78
AD
1277 db_query($link, "UPDATE ttrss_user_entries
1278 SET unread = false,last_read = NOW()
c7e51de1 1279 WHERE feed_id = '$feed' AND owner_uid = $owner_uid");
8d505d78 1280
23aa0d16
AD
1281 } else if ($feed < 0 && $feed > -10) { // special, like starred
1282
1283 if ($feed == -1) {
8d505d78 1284 db_query($link, "UPDATE ttrss_user_entries
23aa0d16 1285 SET unread = false,last_read = NOW()
c7e51de1 1286 WHERE marked = true AND owner_uid = $owner_uid");
23aa0d16 1287 }
e4f4b46f
AD
1288
1289 if ($feed == -2) {
8d505d78 1290 db_query($link, "UPDATE ttrss_user_entries
e4f4b46f 1291 SET unread = false,last_read = NOW()
c7e51de1 1292 WHERE published = true AND owner_uid = $owner_uid");
e4f4b46f
AD
1293 }
1294
2d24f032
AD
1295 if ($feed == -3) {
1296
c1d7e6c3
AD
1297 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
1298
2d24f032 1299 if (DB_TYPE == "pgsql") {
8d505d78 1300 $match_part = "updated > NOW() - INTERVAL '$intl hour' ";
2d24f032 1301 } else {
8d505d78 1302 $match_part = "updated > DATE_SUB(NOW(),
c1d7e6c3 1303 INTERVAL $intl HOUR) ";
2d24f032
AD
1304 }
1305
8d505d78 1306 $result = db_query($link, "SELECT id FROM ttrss_entries,
1f3335dc
AD
1307 ttrss_user_entries WHERE $match_part AND
1308 unread = true AND
8d505d78 1309 ttrss_user_entries.ref_id = ttrss_entries.id AND
c7e51de1 1310 owner_uid = $owner_uid");
1f3335dc
AD
1311
1312 $affected_ids = array();
1313
1314 while ($line = db_fetch_assoc($result)) {
1315 array_push($affected_ids, $line["id"]);
1316 }
1317
1318 catchupArticlesById($link, $affected_ids, 0);
2d24f032
AD
1319 }
1320
3584cb11 1321 if ($feed == -4) {
8d505d78 1322 db_query($link, "UPDATE ttrss_user_entries
3584cb11 1323 SET unread = false,last_read = NOW()
c7e51de1 1324 WHERE owner_uid = $owner_uid");
3584cb11
AD
1325 }
1326
23aa0d16
AD
1327 } else if ($feed < -10) { // label
1328
23aa0d16
AD
1329 $label_id = -$feed - 11;
1330
8d505d78
AD
1331 db_query($link, "UPDATE ttrss_user_entries, ttrss_user_labels2
1332 SET unread = false, last_read = NOW()
338c238d 1333 WHERE label_id = '$label_id' AND unread = true
c7e51de1 1334 AND owner_uid = '$owner_uid' AND ref_id = article_id");
23aa0d16 1335
23aa0d16 1336 }
ad0056a8 1337
c7e51de1 1338 ccache_update($link, $feed, $owner_uid, $cat_view);
ad0056a8 1339
23aa0d16
AD
1340 } else { // tag
1341 db_query($link, "BEGIN");
1342
1343 $tag_name = db_escape_string($feed);
1344
1345 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
c7e51de1 1346 WHERE tag_name = '$tag_name' AND owner_uid = $owner_uid");
23aa0d16
AD
1347
1348 while ($line = db_fetch_assoc($result)) {
1349 db_query($link, "UPDATE ttrss_user_entries SET
8d505d78 1350 unread = false, last_read = NOW()
23aa0d16
AD
1351 WHERE int_id = " . $line["post_int_id"]);
1352 }
1353 db_query($link, "COMMIT");
1354 }
1355 }
1356
4ffa126e 1357 function getAllCounters($link, $omode = "flc", $active_feed = false) {
cf4d339c 1358
e33fe293 1359 if (!$omode) $omode = "flc";
0a6e5382 1360
6a7817c1 1361 $data = getGlobalCounters($link);
8d505d78 1362
6a7817c1
AD
1363 $data = array_merge($data, getVirtCounters($link));
1364
1365 if (strchr($omode, "l")) $data = array_merge($data, getLabelCounters($link));
1366 if (strchr($omode, "f")) $data = array_merge($data, getFeedCounters($link, $active_feed));
1367 if (strchr($omode, "t")) $data = array_merge($data, getTagCounters($link));
798f5a64 1368 if (strchr($omode, "c")) $data = array_merge($data, getCategoryCounters($link));
6a7817c1
AD
1369
1370 return $data;
8d505d78 1371 }
a9cb1f83 1372
79178062
AD
1373 function getCategoryTitle($link, $cat_id) {
1374
1375 if ($cat_id == -1) {
1376 return __("Special");
1377 } else if ($cat_id == -2) {
1378 return __("Labels");
1379 } else {
1380
1381 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
1382 id = '$cat_id'");
1383
1384 if (db_num_rows($result) == 1) {
1385 return db_fetch_result($result, 0, "title");
1386 } else {
1387 return "Uncategorized";
1388 }
1389 }
1390 }
1391
1392
a9cb1f83 1393 function getCategoryCounters($link) {
6a7817c1 1394 $ret_arr = array();
bba7c4bf 1395
6a7817c1 1396 /* Labels category */
bba7c4bf 1397
8acc449c 1398 $cv = array("id" => -2, "kind" => "cat",
6a7817c1 1399 "counter" => getCategoryUnread($link, -2));
bba7c4bf 1400
6a7817c1 1401 array_push($ret_arr, $cv);
bba7c4bf 1402
14073c0a
AD
1403 $age_qpart = getMaxAgeSubquery();
1404
8d505d78
AD
1405 $result = db_query($link, "SELECT id AS cat_id, value AS unread
1406 FROM ttrss_feed_categories, ttrss_cat_counters_cache
1407 WHERE ttrss_cat_counters_cache.feed_id = id AND
31375163 1408 ttrss_feed_categories.owner_uid = " . $_SESSION["uid"]);
a9cb1f83
AD
1409
1410 while ($line = db_fetch_assoc($result)) {
22fdebff 1411 $line["cat_id"] = (int) $line["cat_id"];
8a4c759e 1412
8acc449c 1413 $cv = array("id" => $line["cat_id"], "kind" => "cat",
6a7817c1
AD
1414 "counter" => $line["unread"]);
1415
1416 array_push($ret_arr, $cv);
a9cb1f83 1417 }
d232a40f
AD
1418
1419 /* Special case: NULL category doesn't actually exist in the DB */
1420
9798b2b4 1421 $cv = array("id" => 0, "kind" => "cat",
6a7817c1 1422 "counter" => ccache_find($link, 0, $_SESSION["uid"], true));
d232a40f 1423
6a7817c1
AD
1424 array_push($ret_arr, $cv);
1425
1426 return $ret_arr;
a9cb1f83
AD
1427 }
1428
b6d486a3
AD
1429 function getCategoryUnread($link, $cat, $owner_uid = false) {
1430
1431 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
f295c368 1432
bba7c4bf 1433 if ($cat >= 0) {
18664970 1434
bba7c4bf
AD
1435 if ($cat != 0) {
1436 $cat_query = "cat_id = '$cat'";
1437 } else {
1438 $cat_query = "cat_id IS NULL";
1439 }
14073c0a
AD
1440
1441 $age_qpart = getMaxAgeSubquery();
1442
8d505d78 1443 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
b6d486a3 1444 AND owner_uid = " . $owner_uid);
8d505d78 1445
bba7c4bf
AD
1446 $cat_feeds = array();
1447 while ($line = db_fetch_assoc($result)) {
1448 array_push($cat_feeds, "feed_id = " . $line["id"]);
1449 }
8d505d78 1450
bba7c4bf 1451 if (count($cat_feeds) == 0) return 0;
8d505d78 1452
bba7c4bf 1453 $match_part = implode(" OR ", $cat_feeds);
8d505d78
AD
1454
1455 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1456 FROM ttrss_user_entries,ttrss_entries
1457 WHERE unread = true AND ($match_part) AND id = ref_id
b6d486a3 1458 AND $age_qpart AND owner_uid = " . $owner_uid);
8d505d78 1459
bba7c4bf 1460 $unread = 0;
8d505d78 1461
bba7c4bf
AD
1462 # this needs to be rewritten
1463 while ($line = db_fetch_assoc($result)) {
1464 $unread += $line["unread"];
1465 }
8d505d78 1466
bba7c4bf
AD
1467 return $unread;
1468 } else if ($cat == -1) {
59e15af4 1469 return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3) + getFeedUnread($link, 0);
bba7c4bf 1470 } else if ($cat == -2) {
f295c368 1471
b2531a28 1472 $result = db_query($link, "
8d505d78
AD
1473 SELECT COUNT(unread) AS unread FROM
1474 ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds
1475 WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND
b2531a28 1476 ttrss_labels2.owner_uid = '$owner_uid'
117335bf 1477 AND unread = true AND feed_id = ttrss_feeds.id
b2531a28 1478 AND ttrss_user_entries.owner_uid = '$owner_uid'");
ceb30ba4 1479
b2531a28 1480 $unread = db_fetch_result($result, 0, "unread");
f295c368 1481
b2531a28 1482 return $unread;
f295c368 1483
8d505d78 1484 }
f295c368
AD
1485 }
1486
14073c0a
AD
1487 function getMaxAgeSubquery($days = COUNTERS_MAX_AGE) {
1488 if (DB_TYPE == "pgsql") {
8d505d78 1489 return "ttrss_entries.date_updated >
14073c0a
AD
1490 NOW() - INTERVAL '$days days'";
1491 } else {
8d505d78 1492 return "ttrss_entries.date_updated >
14073c0a
AD
1493 DATE_SUB(NOW(), INTERVAL $days DAY)";
1494 }
1495 }
1496
f295c368 1497 function getFeedUnread($link, $feed, $is_cat = false) {
2627f2d0 1498 return getFeedArticles($link, $feed, $is_cat, true, $_SESSION["uid"]);
bdb7369b
AD
1499 }
1500
ceb30ba4
AD
1501 function getLabelUnread($link, $label_id, $owner_uid = false) {
1502 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
1503
1504 $result = db_query($link, "
8d505d78
AD
1505 SELECT COUNT(unread) AS unread FROM
1506 ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds
1507 WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND
b0f24af1 1508 ttrss_labels2.owner_uid = '$owner_uid' AND ttrss_labels2.id = '$label_id'
117335bf 1509 AND unread = true AND feed_id = ttrss_feeds.id
933ba4ee 1510 AND ttrss_user_entries.owner_uid = '$owner_uid'");
ceb30ba4
AD
1511
1512 if (db_num_rows($result) != 0) {
1513 return db_fetch_result($result, 0, "unread");
1514 } else {
1515 return 0;
1516 }
1517 }
1518
2627f2d0
AD
1519 function getFeedArticles($link, $feed, $is_cat = false, $unread_only = false,
1520 $owner_uid = false) {
1521
22fdebff 1522 $n_feed = (int) $feed;
f295c368 1523
2627f2d0
AD
1524 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
1525
bdb7369b
AD
1526 if ($unread_only) {
1527 $unread_qpart = "unread = true";
1528 } else {
1529 $unread_qpart = "true";
1530 }
1531
14073c0a
AD
1532 $age_qpart = getMaxAgeSubquery();
1533
f295c368 1534 if ($is_cat) {
8d505d78 1535 return getCategoryUnread($link, $n_feed, $owner_uid);
326469fc
AD
1536 } if ($feed != "0" && $n_feed == 0) {
1537
c5701e70
AD
1538 $feed = db_escape_string($feed);
1539
326469fc 1540 $result = db_query($link, "SELECT SUM((SELECT COUNT(int_id)
8d505d78 1541 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
326469fc 1542 AND ref_id = id AND $age_qpart
8d505d78 1543 AND $unread_qpart)) AS count FROM ttrss_tags
326469fc
AD
1544 WHERE owner_uid = $owner_uid AND tag_name = '$feed'");
1545 return db_fetch_result($result, 0, "count");
1546
f295c368 1547 } else if ($n_feed == -1) {
a9cb1f83 1548 $match_part = "marked = true";
e4f4b46f
AD
1549 } else if ($n_feed == -2) {
1550 $match_part = "published = true";
2d24f032 1551 } else if ($n_feed == -3) {
cd2cc43d 1552 $match_part = "unread = true AND score >= 0";
2d24f032 1553
b71e188e 1554 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
c1d7e6c3 1555
2d24f032 1556 if (DB_TYPE == "pgsql") {
8d505d78 1557 $match_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
2d24f032 1558 } else {
7608b38a 1559 $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2d24f032 1560 }
b2531a28
AD
1561 } else if ($n_feed == -4) {
1562 $match_part = "true";
e04c18a2 1563 } else if ($n_feed >= 0) {
831ff047 1564
6e63a7c3
AD
1565 if ($n_feed != 0) {
1566 $match_part = "feed_id = '$n_feed'";
831ff047 1567 } else {
6e63a7c3 1568 $match_part = "feed_id IS NULL";
831ff047 1569 }
6e63a7c3 1570
a9cb1f83 1571 } else if ($feed < -10) {
318260cc 1572
a9cb1f83
AD
1573 $label_id = -$feed - 11;
1574
ceb30ba4 1575 return getLabelUnread($link, $label_id, $owner_uid);
a9cb1f83 1576
a9cb1f83
AD
1577 }
1578
1579 if ($match_part) {
e04c18a2
AD
1580
1581 if ($n_feed != 0) {
1582 $from_qpart = "ttrss_user_entries,ttrss_feeds,ttrss_entries";
117335bf 1583 $feeds_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
e04c18a2
AD
1584 } else {
1585 $from_qpart = "ttrss_user_entries,ttrss_entries";
c3fddd05 1586 $feeds_qpart = '';
e04c18a2
AD
1587 }
1588
8d505d78 1589 $query = "SELECT count(int_id) AS unread
e04c18a2 1590 FROM $from_qpart WHERE
8d505d78 1591 ttrss_user_entries.ref_id = ttrss_entries.id AND
14073c0a 1592 $age_qpart AND
dbfc4365
AD
1593 $feeds_qpart
1594 $unread_qpart AND ($match_part) AND ttrss_user_entries.owner_uid = $owner_uid";
1595
1596 $result = db_query($link, $query);
8d505d78 1597
a9cb1f83 1598 } else {
8d505d78 1599
a9cb1f83 1600 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
8d505d78
AD
1601 FROM ttrss_tags,ttrss_user_entries,ttrss_entries
1602 WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id
bdb7369b 1603 AND $unread_qpart AND $age_qpart AND
2627f2d0 1604 ttrss_tags.owner_uid = " . $owner_uid);
a9cb1f83 1605 }
8d505d78 1606
a9cb1f83 1607 $unread = db_fetch_result($result, 0, "unread");
cfb02131 1608
a9cb1f83
AD
1609 return $unread;
1610 }
1611
f3acc32e
AD
1612 function getGlobalUnread($link, $user_id = false) {
1613
1614 if (!$user_id) {
1615 $user_id = $_SESSION["uid"];
1616 }
1617
8a4c759e
AD
1618 $result = db_query($link, "SELECT SUM(value) AS c_id FROM ttrss_counters_cache
1619 WHERE owner_uid = '$user_id' AND feed_id > 0");
1620
8d505d78 1621 $c_id = db_fetch_result($result, 0, "c_id");
8a4c759e 1622
a9cb1f83
AD
1623 return $c_id;
1624 }
1625
1626 function getGlobalCounters($link, $global_unread = -1) {
6a7817c1
AD
1627 $ret_arr = array();
1628
8d505d78 1629 if ($global_unread == -1) {
a9cb1f83
AD
1630 $global_unread = getGlobalUnread($link);
1631 }
6a7817c1 1632
8d505d78 1633 $cv = array("id" => "global-unread",
6a7817c1
AD
1634 "counter" => $global_unread);
1635
1636 array_push($ret_arr, $cv);
7bf7e4d3 1637
8d505d78 1638 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
7bf7e4d3
AD
1639 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
1640
1641 $subscribed_feeds = db_fetch_result($result, 0, "fn");
1642
8d505d78 1643 $cv = array("id" => "subscribed-feeds",
6a7817c1 1644 "counter" => $subscribed_feeds);
7bf7e4d3 1645
6a7817c1
AD
1646 array_push($ret_arr, $cv);
1647
1648 return $ret_arr;
a9cb1f83
AD
1649 }
1650
3809b278 1651 function getTagCounters($link) {
8d505d78 1652
6a7817c1 1653 $ret_arr = array();
a9cb1f83 1654
14073c0a
AD
1655 $age_qpart = getMaxAgeSubquery();
1656
8d505d78
AD
1657 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1658 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
14073c0a 1659 AND ref_id = id AND $age_qpart
8d505d78
AD
1660 AND unread = true)) AS count FROM ttrss_tags
1661 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
ef1ac7c7 1662 ORDER BY count DESC LIMIT 55");
8d505d78 1663
a9cb1f83
AD
1664 $tags = array();
1665
1666 while ($line = db_fetch_assoc($result)) {
1667 $tags[$line["tag_name"]] += $line["count"];
1668 }
1669
1670 foreach (array_keys($tags) as $tag) {
8d505d78 1671 $unread = $tags[$tag];
a9cb1f83 1672 $tag = htmlspecialchars($tag);
6a7817c1
AD
1673
1674 $cv = array("id" => $tag,
8acc449c 1675 "kind" => "tag",
6a7817c1
AD
1676 "counter" => $unread);
1677
1678 array_push($ret_arr, $cv);
1679 }
1680
8d505d78 1681 return $ret_arr;
a9cb1f83
AD
1682 }
1683
6a7817c1 1684 function getVirtCounters($link) {
a9cb1f83 1685
ef393de7 1686 $ret_arr = array();
bdb7369b 1687
e04c18a2 1688 for ($i = 0; $i >= -4; $i--) {
bdb7369b 1689
ceb30ba4 1690 $count = getFeedUnread($link, $i);
6a7817c1
AD
1691
1692 $cv = array("id" => $i,
2abc7af0 1693 "counter" => $count);
8d505d78 1694
296c8134
AD
1695// if (get_pref($link, 'EXTENDED_FEEDLIST'))
1696// $cv["xmsg"] = getFeedArticles($link, $i)." ".__("total");
bdb7369b 1697
6a7817c1 1698 array_push($ret_arr, $cv);
8d505d78 1699 }
0a6e5382
AD
1700
1701 return $ret_arr;
1702 }
1703
11232703 1704 function getLabelCounters($link, $descriptions = false) {
6a7817c1
AD
1705
1706 $ret_arr = array();
0a6e5382
AD
1707
1708 $age_qpart = getMaxAgeSubquery();
1709
3809b278 1710 $owner_uid = $_SESSION["uid"];
bdb7369b 1711
3809b278
AD
1712 $result = db_query($link, "SELECT id, caption FROM ttrss_labels2
1713 WHERE owner_uid = '$owner_uid'");
8d505d78 1714
3809b278 1715 while ($line = db_fetch_assoc($result)) {
2d24f032 1716
3809b278 1717 $id = -$line["id"] - 11;
e4f4b46f 1718
3809b278
AD
1719 $label_name = $line["caption"];
1720 $count = getFeedUnread($link, $id);
3809b278 1721
6a7817c1 1722 $cv = array("id" => $id,
11232703
AD
1723 "counter" => $count);
1724
1725 if ($descriptions)
1726 $cv["description"] = $label_name;
a9cb1f83 1727
296c8134
AD
1728// if (get_pref($link, 'EXTENDED_FEEDLIST'))
1729// $cv["xmsg"] = getFeedArticles($link, $id)." ".__("total");
ef393de7 1730
6a7817c1 1731 array_push($ret_arr, $cv);
3809b278 1732 }
8d505d78 1733
ef393de7 1734 return $ret_arr;
a9cb1f83
AD
1735 }
1736
3809b278 1737 function getFeedCounters($link, $active_feed = false) {
a9cb1f83 1738
6a7817c1
AD
1739 $ret_arr = array();
1740
14073c0a
AD
1741 $age_qpart = getMaxAgeSubquery();
1742
8a4c759e
AD
1743 $query = "SELECT ttrss_feeds.id,
1744 ttrss_feeds.title,
8d505d78 1745 ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated,
de0a2122
AD
1746 last_error, value AS count
1747 FROM ttrss_feeds, ttrss_counters_cache
8d505d78 1748 WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
55e01d7e 1749 AND ttrss_counters_cache.feed_id = id";
a9cb1f83 1750
14073c0a 1751 $result = db_query($link, $query);
a9cb1f83
AD
1752 $fctrs_modified = false;
1753
1754 while ($line = db_fetch_assoc($result)) {
8d505d78 1755
a9cb1f83 1756 $id = $line["id"];
de0a2122 1757 $count = $line["count"];
a9cb1f83 1758 $last_error = htmlspecialchars($line["last_error"]);
fb1fb4ab 1759
324944f3 1760 $last_updated = make_local_datetime($link, $line['last_updated'], false);
fb1fb4ab 1761
7defa089 1762 $has_img = feed_has_icon($id);
a9cb1f83 1763
428b704d
AD
1764 if (date('Y') - date('Y', strtotime($line['last_updated'])) > 2)
1765 $last_updated = '';
1766
6a7817c1 1767 $cv = array("id" => $id,
21884958 1768 "updated" => $last_updated,
6a7817c1
AD
1769 "counter" => $count,
1770 "has_img" => (int) $has_img);
a9cb1f83 1771
6a7817c1
AD
1772 if ($last_error)
1773 $cv["error"] = $last_error;
4ffa126e 1774
296c8134
AD
1775// if (get_pref($link, 'EXTENDED_FEEDLIST'))
1776// $cv["xmsg"] = getFeedArticles($link, $id)." ".__("total");
bdb7369b 1777
6a7817c1 1778 if ($active_feed && $id == $active_feed)
fbc95c5b 1779 $cv["title"] = truncate_string($line["title"], 30);
6a7817c1
AD
1780
1781 array_push($ret_arr, $cv);
a9cb1f83 1782
a9cb1f83 1783 }
6a7817c1
AD
1784
1785 return $ret_arr;
a9cb1f83
AD
1786 }
1787
6e7f8d26
AD
1788 function get_pgsql_version($link) {
1789 $result = db_query($link, "SELECT version() AS version");
9949bd15 1790 $version = explode(" ", db_fetch_result($result, 0, "version"));
6e7f8d26
AD
1791 return $version[1];
1792 }
1793
2b8290cd
CW
1794 /**
1795 * Subscribes the user to the given feed
1796 *
1797 * @param resource $link Database connection
1798 * @param string $url Feed URL to subscribe to
1799 * @param integer $cat_id Category ID the feed shall be added to
1800 * @param string $auth_login (optional) Feed username
1801 * @param string $auth_pass (optional) Feed password
1802 *
1803 * @return integer Status code:
1804 * 0 - OK, Feed already exists
1805 * 1 - OK, Feed added
1806 * 2 - Invalid URL
9a8ce956
CW
1807 * 3 - URL content is HTML, no feeds available
1808 * 4 - URL content is HTML which contains multiple feeds.
1809 * Here you should call extractfeedurls in rpc-backend
1810 * to get all possible feeds.
5414ad4c 1811 * 5 - Couldn't download the URL content.
2b8290cd 1812 */
8d505d78 1813 function subscribe_to_feed($link, $url, $cat_id = 0,
f27de515 1814 $auth_login = '', $auth_pass = '') {
bb0f29a4 1815
2c08214a
AD
1816 require_once "include/rssfuncs.php";
1817
f0266f51 1818 $url = fix_url($url);
ec39a02c
AD
1819
1820 if (!$url || !validate_feed_url($url)) return 2;
a5819bb3 1821
b3009bcd
AD
1822 $update_method = 0;
1823
8d505d78 1824 $result = db_query($link, "SELECT twitter_oauth FROM ttrss_users
aeaa6991
AD
1825 WHERE id = ".$_SESSION['uid']);
1826
1827 $has_oauth = db_fetch_result($result, 0, 'twitter_oauth');
1828
54a3dd8d 1829 if (!$has_oauth || strpos($url, '://api.twitter.com') === false) {
8d505d78 1830 if (!fetch_file_contents($url, false, $auth_login, $auth_pass)) return 5;
57e24c82 1831
8d505d78
AD
1832 if (url_is_html($url, $auth_login, $auth_pass)) {
1833 $feedUrls = get_feeds_from_html($url, $auth_login, $auth_pass);
57e24c82
AD
1834 if (count($feedUrls) == 0) {
1835 return 3;
1836 } else if (count($feedUrls) > 1) {
1837 return 4;
1838 }
1839 //use feed url as new URL
1840 $url = key($feedUrls);
f6d8345b 1841 }
f6d8345b 1842
57e24c82 1843 } else {
8d505d78 1844 if (!fetch_twitter_rss($link, $url, $_SESSION['uid']))
57e24c82 1845 return 5;
b3009bcd
AD
1846
1847 $update_method = 3;
57e24c82 1848 }
956c7629
AD
1849 if ($cat_id == "0" || !$cat_id) {
1850 $cat_qpart = "NULL";
1851 } else {
1852 $cat_qpart = "'$cat_id'";
1853 }
8d505d78 1854
956c7629 1855 $result = db_query($link,
8d505d78 1856 "SELECT id FROM ttrss_feeds
a5819bb3 1857 WHERE feed_url = '$url' AND owner_uid = ".$_SESSION["uid"]);
8d505d78 1858
956c7629 1859 if (db_num_rows($result) == 0) {
956c7629 1860 $result = db_query($link,
8d505d78
AD
1861 "INSERT INTO ttrss_feeds
1862 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass,update_method)
1863 VALUES ('".$_SESSION["uid"]."', '$url',
b3009bcd 1864 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass', '$update_method')");
8d505d78 1865
956c7629 1866 $result = db_query($link,
8d505d78 1867 "SELECT id FROM ttrss_feeds WHERE feed_url = '$url'
f27de515 1868 AND owner_uid = " . $_SESSION["uid"]);
8d505d78 1869
956c7629 1870 $feed_id = db_fetch_result($result, 0, "id");
8d505d78 1871
956c7629 1872 if ($feed_id) {
c633e370 1873 update_rss_feed($link, $feed_id, true);
956c7629
AD
1874 }
1875
a5819bb3 1876 return 1;
956c7629 1877 } else {
a5819bb3 1878 return 0;
956c7629
AD
1879 }
1880 }
1881
8d505d78 1882 function print_feed_select($link, $id, $default_id = "",
673d54ca
AD
1883 $attributes = "", $include_all_feeds = true) {
1884
79f3553b 1885 print "<select id=\"$id\" name=\"$id\" $attributes>";
8d505d78 1886 if ($include_all_feeds) {
89cb787e 1887 print "<option value=\"0\">".__('All feeds')."</option>";
673d54ca 1888 }
8d505d78 1889
673d54ca
AD
1890 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
1891 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1892
1893 if (db_num_rows($result) > 0 && $include_all_feeds) {
1894 print "<option disabled>--------</option>";
1895 }
1896
1897 while ($line = db_fetch_assoc($result)) {
1898 if ($line["id"] == $default_id) {
10249c41 1899 $is_selected = "selected=\"1\"";
673d54ca
AD
1900 } else {
1901 $is_selected = "";
1902 }
b1710666
AD
1903
1904 $title = truncate_string(htmlspecialchars($line["title"]), 40);
1905
8d505d78 1906 printf("<option $is_selected value='%d'>%s</option>",
b1710666 1907 $line["id"], $title);
673d54ca 1908 }
8d505d78 1909
673d54ca
AD
1910 print "</select>";
1911 }
1912
8d505d78 1913 function print_feed_cat_select($link, $id, $default_id = "",
673d54ca 1914 $attributes = "", $include_all_cats = true) {
8d505d78 1915
5c7c7da9 1916 print "<select id=\"$id\" name=\"$id\" default=\"$default_id\" onchange=\"catSelectOnChange(this)\" $attributes>";
673d54ca
AD
1917
1918 if ($include_all_cats) {
d1db26aa 1919 print "<option value=\"0\">".__('Uncategorized')."</option>";
673d54ca
AD
1920 }
1921
1922 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
1923 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1924
1925 if (db_num_rows($result) > 0 && $include_all_cats) {
c00907f2 1926 print "<option disabled=\"1\">--------</option>";
673d54ca
AD
1927 }
1928
1929 while ($line = db_fetch_assoc($result)) {
1930 if ($line["id"] == $default_id) {
10249c41 1931 $is_selected = "selected=\"1\"";
673d54ca 1932 } else {
8d505d78 1933 $is_selected = "";
673d54ca 1934 }
c00907f2
AD
1935
1936 if ($line["title"])
8d505d78 1937 printf("<option $is_selected value='%d'>%s</option>",
c00907f2 1938 $line["id"], htmlspecialchars($line["title"]));
673d54ca
AD
1939 }
1940
f9c388f5 1941# print "<option value=\"ADD_CAT\">" .__("Add category...") . "</option>";
5c7c7da9 1942
673d54ca
AD
1943 print "</select>";
1944 }
8d505d78 1945
14f69488
AD
1946 function checkbox_to_sql_bool($val) {
1947 return ($val == "on") ? "true" : "false";
1948 }
86b682ce
AD
1949
1950 function getFeedCatTitle($link, $id) {
1951 if ($id == -1) {
d1db26aa 1952 return __("Special");
86b682ce 1953 } else if ($id < -10) {
d1db26aa 1954 return __("Labels");
86b682ce 1955 } else if ($id > 0) {
8d505d78 1956 $result = db_query($link, "SELECT ttrss_feed_categories.title
86b682ce
AD
1957 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
1958 cat_id = ttrss_feed_categories.id");
1959 if (db_num_rows($result) == 1) {
1960 return db_fetch_result($result, 0, "title");
1961 } else {
d1db26aa 1962 return __("Uncategorized");
86b682ce
AD
1963 }
1964 } else {
1965 return "getFeedCatTitle($id) failed";
1966 }
1967
1968 }
1969
af88c48a
AD
1970 function getFeedIcon($id) {
1971 switch ($id) {
4bee8b5f
AD
1972 case 0:
1973 return "images/archive.png";
1974 break;
af88c48a 1975 case -1:
f65ffc2d 1976 return "images/mark_set.png";
af88c48a
AD
1977 break;
1978 case -2:
b97e6e02 1979 return "images/pub_set.png";
af88c48a
AD
1980 break;
1981 case -3:
1982 return "images/fresh.png";
1983 break;
1984 case -4:
1985 return "images/tag.png";
1986 break;
1987 default:
4bee8b5f
AD
1988 if ($id < -10) {
1989 return "images/label.png";
1990 } else {
8d505d78 1991 if (file_exists(ICONS_DIR . "/$id.ico"))
e2eda979 1992 return ICONS_URL . "/$id.ico";
4bee8b5f 1993 }
af88c48a
AD
1994 break;
1995 }
1996 }
1997
86b682ce
AD
1998 function getFeedTitle($link, $id) {
1999 if ($id == -1) {
d1db26aa 2000 return __("Starred articles");
945c243e
AD
2001 } else if ($id == -2) {
2002 return __("Published articles");
2d24f032
AD
2003 } else if ($id == -3) {
2004 return __("Fresh articles");
b2531a28
AD
2005 } else if ($id == -4) {
2006 return __("All articles");
80db1113 2007 } else if ($id === 0 || $id === "0") {
e04c18a2 2008 return __("Archived articles");
86b682ce 2009 } else if ($id < -10) {
76626c72 2010 $label_id = -$id - 11;
ceb30ba4 2011 $result = db_query($link, "SELECT caption FROM ttrss_labels2 WHERE id = '$label_id'");
86b682ce 2012 if (db_num_rows($result) == 1) {
ceb30ba4 2013 return db_fetch_result($result, 0, "caption");
86b682ce
AD
2014 } else {
2015 return "Unknown label ($label_id)";
2016 }
2017
147f5632 2018 } else if (is_numeric($id) && $id > 0) {
86b682ce
AD
2019 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2020 if (db_num_rows($result) == 1) {
2021 return db_fetch_result($result, 0, "title");
2022 } else {
2023 return "Unknown feed ($id)";
2024 }
2025 } else {
22fdebff 2026 return $id;
86b682ce 2027 }
86b682ce 2028 }
3dd46f19 2029
d8221301 2030 function make_init_params($link) {
f1f3a642 2031 $params = array();
c9268ed5 2032
c4f7ba80
AD
2033 $params["theme"] = get_user_theme($link);
2034 $params["theme_options"] = get_user_theme_options($link);
f6d6e22f 2035
c4f7ba80
AD
2036 $params["sign_progress"] = theme_image($link, "images/indicator_white.gif");
2037 $params["sign_progress_tiny"] = theme_image($link, "images/indicator_tiny.gif");
2038 $params["sign_excl"] = theme_image($link, "images/sign_excl.png");
2039 $params["sign_info"] = theme_image($link, "images/sign_info.png");
be0801a1 2040
f1f3a642
AD
2041 foreach (array("ON_CATCHUP_SHOW_NEXT_FEED", "HIDE_READ_FEEDS",
2042 "ENABLE_FEED_CATS", "FEEDS_SORT_BY_UNREAD", "CONFIRM_FEED_CATCHUP",
7d12b6c8 2043 "CDM_AUTO_CATCHUP", "FRESH_ARTICLE_MAX_AGE", "DEFAULT_ARTICLE_LIMIT",
30b6ee8c 2044 "HIDE_READ_SHOWS_SPECIAL", "COMBINED_DISPLAY_MODE") as $param) {
40496720 2045
c4f7ba80 2046 $params[strtolower($param)] = (int) get_pref($link, $param);
f1f3a642 2047 }
40496720 2048
c4f7ba80
AD
2049 $params["icons_url"] = ICONS_URL;
2050 $params["cookie_lifetime"] = SESSION_COOKIE_LIFETIME;
2051 $params["default_view_mode"] = get_pref($link, "_DEFAULT_VIEW_MODE");
2052 $params["default_view_limit"] = (int) get_pref($link, "_DEFAULT_VIEW_LIMIT");
2053 $params["default_view_order_by"] = get_pref($link, "_DEFAULT_VIEW_ORDER_BY");
c4f7ba80 2054 $params["bw_limit"] = (int) $_SESSION["bw_limit"];
59b223d7 2055
8cd576a1 2056 $result = db_query($link, "SELECT MAX(id) AS mid, COUNT(*) AS nf FROM
9b7ecc0a
AD
2057 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
2058
8cd576a1
AD
2059 $max_feed_id = db_fetch_result($result, 0, "mid");
2060 $num_feeds = db_fetch_result($result, 0, "nf");
9b7ecc0a 2061
8cd576a1 2062 $params["max_feed_id"] = (int) $max_feed_id;
c4f7ba80 2063 $params["num_feeds"] = (int) $num_feeds;
8cd576a1 2064
c4f7ba80 2065 $params["collapsed_feedlist"] = (int) get_pref($link, "_COLLAPSED_FEEDLIST");
9b7ecc0a 2066
d8221301 2067 return $params;
3ac2b520 2068 }
f54f515f 2069
c4f7ba80 2070 function make_runtime_info($link) {
8cd576a1
AD
2071 $data = array();
2072
2073 $result = db_query($link, "SELECT MAX(id) AS mid, COUNT(*) AS nf FROM
9b7ecc0a
AD
2074 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
2075
8cd576a1
AD
2076 $max_feed_id = db_fetch_result($result, 0, "mid");
2077 $num_feeds = db_fetch_result($result, 0, "nf");
9b7ecc0a 2078
8cd576a1
AD
2079 $data["max_feed_id"] = (int) $max_feed_id;
2080 $data["num_feeds"] = (int) $num_feeds;
c4f7ba80 2081
f8fb4498 2082 $data['last_article_id'] = getLastArticleId($link);
5ae8f858 2083 $data['cdm_expanded'] = get_pref($link, 'CDM_EXPANDED');
f8fb4498 2084
dbaa4e4a 2085 if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) {
c4f7ba80
AD
2086
2087 $data['daemon_is_running'] = (int) file_is_locked("update_daemon.lock");
8e00ae9b 2088
9041f58b 2089 if (time() - $_SESSION["daemon_stamp_check"] > 30) {
8e00ae9b 2090
fb074239 2091 $stamp = (int) @file_get_contents(LOCK_DIRECTORY . "/update_daemon.stamp");
fbae93d8 2092
8e00ae9b 2093 if ($stamp) {
9041f58b
AD
2094 $stamp_delta = time() - $stamp;
2095
2096 if ($stamp_delta > 1800) {
f6854e44 2097 $stamp_check = 0;
8e00ae9b 2098 } else {
f6854e44
AD
2099 $stamp_check = 1;
2100 $_SESSION["daemon_stamp_check"] = time();
8e00ae9b
AD
2101 }
2102
c4f7ba80 2103 $data['daemon_stamp_ok'] = $stamp_check;
f6854e44 2104
8e00ae9b
AD
2105 $stamp_fmt = date("Y.m.d, G:i", $stamp);
2106
c4f7ba80 2107 $data['daemon_stamp'] = $stamp_fmt;
8e00ae9b 2108 }
8e00ae9b 2109 }
71ad883b 2110 }
8e00ae9b 2111
63855db1 2112 if ($_SESSION["last_version_check"] + 86400 + rand(-1000, 1000) < time()) {
fb074239 2113 $new_version_details = @check_for_update($link);
d9fa39f1 2114
63855db1 2115 $data['new_version_available'] = (int) ($new_version_details != false);
d9fa39f1
AD
2116
2117 $_SESSION["last_version_check"] = time();
d9fa39f1
AD
2118 }
2119
c4f7ba80 2120 return $data;
f54f515f 2121 }
ef393de7 2122
b7d1a163 2123 function search_to_sql($link, $search, $match_on) {
ef393de7 2124
88040f57 2125 $search_query_part = "";
e20c9d88 2126
9949bd15 2127 $keywords = explode(" ", $search);
88040f57 2128 $query_keywords = array();
e20c9d88 2129
ab4b768f
AD
2130 foreach ($keywords as $k) {
2131 if (strpos($k, "-") === 0) {
2132 $k = substr($k, 1);
2133 $not = "NOT";
2134 } else {
2135 $not = "";
88040f57 2136 }
e20c9d88 2137
9949bd15 2138 $commandpair = explode(":", mb_strtolower($k), 2);
53003548
AD
2139
2140 if ($commandpair[0] == "note" && $commandpair[1]) {
2141
2142 if ($commandpair[1] == "true")
2143 array_push($query_keywords, "($not (note IS NOT NULL AND note != ''))");
2144 else
2145 array_push($query_keywords, "($not (note IS NULL OR note = ''))");
2146
2147 } else if ($commandpair[0] == "star" && $commandpair[1]) {
2148
2149 if ($commandpair[1] == "true")
2150 array_push($query_keywords, "($not (marked = true))");
2151 else
2152 array_push($query_keywords, "($not (marked = false))");
2153
2154 } else if ($commandpair[0] == "pub" && $commandpair[1]) {
2155
2156 if ($commandpair[1] == "true")
2157 array_push($query_keywords, "($not (published = true))");
2158 else
2159 array_push($query_keywords, "($not (published = false))");
2160
2161 } else if (strpos($k, "@") === 0) {
e20c9d88 2162
ab4b768f
AD
2163 $user_tz_string = get_pref($link, 'USER_TIMEZONE', $_SESSION['uid']);
2164 $orig_ts = strtotime(substr($k, 1));
ab4b768f 2165 $k = date("Y-m-d", convert_timestamp($orig_ts, $user_tz_string, 'UTC'));
8d505d78 2166
53003548
AD
2167 //$k = date("Y-m-d", strtotime(substr($k, 1)));
2168
ab4b768f
AD
2169 array_push($query_keywords, "(".SUBSTRING_FOR_DATE."(updated,1,LENGTH('$k')) $not = '$k')");
2170 } else if ($match_on == "both") {
2171 array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
2172 OR UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
2173 } else if ($match_on == "title") {
eb6c7f42 2174 array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%'))");
ab4b768f 2175 } else if ($match_on == "content") {
eb6c7f42 2176 array_push($query_keywords, "(UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
88040f57
AD
2177 }
2178 }
2179
2180 $search_query_part = implode("AND", $query_keywords);
2181
2182 return $search_query_part;
2183 }
2184
147f5632 2185
97e5dbb2 2186 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0, $filter = false, $since_id = 0) {
c36bf4d5
AD
2187
2188 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
c1a0b534 2189
c3fddd05
AD
2190 $ext_tables_part = "";
2191
88040f57 2192 if ($search) {
e4f7f8df
AD
2193
2194 if (SPHINX_ENABLED) {
2195 $ids = join(",", @sphinx_search($search, 0, 500));
2196
8d505d78 2197 if ($ids)
e4f7f8df
AD
2198 $search_query_part = "ref_id IN ($ids) AND ";
2199 else
2200 $search_query_part = "ref_id = -1 AND ";
2201
2202 } else {
b7d1a163 2203 $search_query_part = search_to_sql($link, $search, $match_on);
e4f7f8df 2204 $search_query_part .= " AND ";
8d505d78 2205 }
e20c9d88 2206
ef393de7
AD
2207 } else {
2208 $search_query_part = "";
2209 }
2210
36184020
AD
2211 if ($filter) {
2212 $filter_query_part = filter_to_sql($filter);
2213 } else {
2214 $filter_query_part = "";
2215 }
2216
97e5dbb2
AD
2217 if ($since_id) {
2218 $since_id_part = "ttrss_entries.id > $since_id AND ";
2219 } else {
2220 $since_id_part = "";
2221 }
2222
ef393de7 2223 $view_query_part = "";
8d505d78 2224
7b4d02a8 2225 if ($view_mode == "adaptive" || $view_query_part == "noscores") {
ef393de7
AD
2226 if ($search) {
2227 $view_query_part = " ";
2228 } else if ($feed != -1) {
f295c368 2229 $unread = getFeedUnread($link, $feed, $cat_view);
ef393de7 2230 if ($unread > 0) {
ff863e00 2231 $view_query_part = " unread = true AND ";
ef393de7
AD
2232 }
2233 }
2234 }
8d505d78 2235
ef393de7
AD
2236 if ($view_mode == "marked") {
2237 $view_query_part = " marked = true AND ";
2238 }
23d72f39
AD
2239
2240 if ($view_mode == "published") {
2241 $view_query_part = " published = true AND ";
2242 }
2243
ef393de7
AD
2244 if ($view_mode == "unread") {
2245 $view_query_part = " unread = true AND ";
2246 }
8b09eac8
AD
2247
2248 if ($view_mode == "updated") {
2249 $view_query_part = " (last_read is null and unread = false) AND ";
2250 }
2251
ef393de7
AD
2252 if ($limit > 0) {
2253 $limit_query_part = "LIMIT " . $limit;
8d505d78 2254 }
ef393de7
AD
2255
2256 $vfeed_query_part = "";
8d505d78 2257
ef393de7
AD
2258 // override query strategy and enable feed display when searching globally
2259 if ($search && $search_mode == "all_feeds") {
2260 $query_strategy_part = "ttrss_entries.id > 0";
8d505d78 2261 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
22fdebff 2262 /* tags */
ef393de7
AD
2263 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2264 $query_strategy_part = "ttrss_entries.id > 0";
2265 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2266 id = feed_id) as feed_title,";
e04c18a2 2267 } else if ($feed > 0 && $search && $search_mode == "this_cat") {
8d505d78
AD
2268
2269 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
0a6c4846
AD
2270
2271 $tmp_result = false;
2272
2273 if ($cat_view) {
8d505d78 2274 $tmp_result = db_query($link, "SELECT id
0a6c4846
AD
2275 FROM ttrss_feeds WHERE cat_id = '$feed'");
2276 } else {
2277 $tmp_result = db_query($link, "SELECT id
8d505d78 2278 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
0a6c4846
AD
2279 WHERE id = '$feed') AND id != '$feed'");
2280 }
8d505d78 2281
ef393de7 2282 $cat_siblings = array();
8d505d78 2283
ef393de7
AD
2284 if (db_num_rows($tmp_result) > 0) {
2285 while ($p = db_fetch_assoc($tmp_result)) {
2286 array_push($cat_siblings, "feed_id = " . $p["id"]);
2287 }
8d505d78
AD
2288
2289 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
ef393de7 2290 $feed, implode(" OR ", $cat_siblings));
8d505d78 2291
ef393de7
AD
2292 } else {
2293 $query_strategy_part = "ttrss_entries.id > 0";
2294 }
8d505d78 2295
e04c18a2 2296 } else if ($feed > 0) {
8d505d78 2297
ef393de7 2298 if ($cat_view) {
5c365f60 2299
ef393de7
AD
2300 if ($feed > 0) {
2301 $query_strategy_part = "cat_id = '$feed'";
2302 } else {
2303 $query_strategy_part = "cat_id IS NULL";
2304 }
8d505d78 2305
ef393de7 2306 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
5c365f60 2307
8d505d78 2308 } else {
6e63a7c3 2309 $query_strategy_part = "feed_id = '$feed'";
ef393de7 2310 }
bfe5ddfc 2311 } else if ($feed == 0 && !$cat_view) { // archive virtual feed
e04c18a2 2312 $query_strategy_part = "feed_id IS NULL";
bfe5ddfc
AD
2313 } else if ($feed == 0 && $cat_view) { // uncategorized
2314 $query_strategy_part = "cat_id IS NULL";
2315 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
ef393de7
AD
2316 } else if ($feed == -1) { // starred virtual feed
2317 $query_strategy_part = "marked = true";
2318 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
e6a38cde
AD
2319 } else if ($feed == -2) { // published virtual feed OR labels category
2320
2321 if (!$cat_view) {
2322 $query_strategy_part = "published = true";
2323 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2324 } else {
2325 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2326
2327 $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2";
8d505d78 2328
e6a38cde
AD
2329 $query_strategy_part = "ttrss_labels2.id = ttrss_user_labels2.label_id AND
2330 ttrss_user_labels2.article_id = ref_id";
2331
2332 }
2333
2d24f032 2334 } else if ($feed == -3) { // fresh virtual feed
cd2cc43d 2335 $query_strategy_part = "unread = true AND score >= 0";
2d24f032 2336
7a22dc2a 2337 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
c1d7e6c3 2338
2d24f032 2339 if (DB_TYPE == "pgsql") {
8d505d78 2340 $query_strategy_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
2d24f032 2341 } else {
7608b38a 2342 $query_strategy_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2d24f032
AD
2343 }
2344
b2531a28
AD
2345 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2346 } else if ($feed == -4) { // all articles virtual feed
2347 $query_strategy_part = "true";
e4f4b46f 2348 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
ef393de7
AD
2349 } else if ($feed <= -10) { // labels
2350 $label_id = -$feed - 11;
3de0261a 2351
ceb30ba4
AD
2352 $query_strategy_part = "label_id = '$label_id' AND
2353 ttrss_labels2.id = ttrss_user_labels2.label_id AND
2354 ttrss_user_labels2.article_id = ref_id";
3de0261a 2355
ef393de7 2356 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
ceb30ba4 2357 $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2";
8d505d78 2358
ef393de7
AD
2359 } else {
2360 $query_strategy_part = "id > 0"; // dumb
2361 }
d6e5706d 2362
b3990c92
AD
2363 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
2364 $date_sort_field = "updated";
2365 } else {
2366 $date_sort_field = "date_entered";
2367 }
2368
7a22dc2a 2369 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
b3990c92 2370 $order_by = "$date_sort_field";
8d505d78 2371 } else {
b3990c92 2372 $order_by = "$date_sort_field DESC";
d6e5706d 2373 }
e939722a 2374
7b4d02a8
AD
2375 if ($view_mode != "noscores") {
2376 $order_by = "score DESC, $order_by";
2377 }
48b0c4ec 2378
e939722a
AD
2379 if ($override_order) {
2380 $order_by = $override_order;
2381 }
8d505d78 2382
ef393de7
AD
2383 $feed_title = "";
2384
22fdebff
AD
2385 if ($search) {
2386 $feed_title = "Search results";
2387 } else {
ef393de7 2388 if ($cat_view) {
22fdebff 2389 $feed_title = getCategoryTitle($link, $feed);
ef393de7 2390 } else {
147f5632 2391 if (is_numeric($feed) && $feed > 0) {
8d505d78 2392 $result = db_query($link, "SELECT title,site_url,last_error
22fdebff 2393 FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = $owner_uid");
8d505d78 2394
22fdebff
AD
2395 $feed_title = db_fetch_result($result, 0, "title");
2396 $feed_site_url = db_fetch_result($result, 0, "site_url");
2397 $last_error = db_fetch_result($result, 0, "last_error");
2398 } else {
2399 $feed_title = getFeedTitle($link, $feed);
8d505d78 2400 }
88040f57 2401 }
ef393de7
AD
2402 }
2403
62129e67
AD
2404 $content_query_part = "content as content_preview,";
2405
ef393de7 2406 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
8d505d78 2407
ef393de7
AD
2408 if ($feed >= 0) {
2409 $feed_kind = "Feeds";
2410 } else {
2411 $feed_kind = "Labels";
2412 }
8d505d78 2413
95a82c08
AD
2414 if ($limit_query_part) {
2415 $offset_query_part = "OFFSET $offset";
2416 }
2417
d00f22ac 2418 if ($vfeed_query_part && get_pref($link, 'VFEED_GROUP_BY_FEED', $owner_uid)) {
6cfea5c7 2419 if (!$override_order) {
8d505d78 2420 $order_by = "ttrss_feeds.title, $order_by";
43fc671f 2421 }
6cfea5c7
AD
2422 }
2423
e04c18a2
AD
2424 if ($feed != "0") {
2425 $from_qpart = "ttrss_entries,ttrss_user_entries,ttrss_feeds$ext_tables_part";
117335bf 2426 $feed_check_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
e04c18a2
AD
2427
2428 } else {
2429 $from_qpart = "ttrss_entries,ttrss_user_entries$ext_tables_part
2430 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)";
2431 }
2432
8d505d78 2433 $query = "SELECT DISTINCT
f9b2d27c 2434 date_entered,
1f64b1be 2435 guid,
ef393de7 2436 ttrss_entries.id,ttrss_entries.title,
46921916 2437 updated,
9c506873
AD
2438 label_cache,
2439 tag_cache,
c0644ee4 2440 always_display_enclosures,
d1fc2f92 2441 site_url,
c7e51de1 2442 note,
13992673
AD
2443 num_comments,
2444 comments,
db16ae50 2445 int_id,
494a64ea 2446 unread,feed_id,marked,published,link,last_read,orig_feed_id,
fc2b26a6 2447 ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms,
ef393de7
AD
2448 $vfeed_query_part
2449 $content_query_part
fc2b26a6 2450 ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms,
ff6e357a 2451 author,score
ef393de7 2452 FROM
e04c18a2 2453 $from_qpart
ef393de7 2454 WHERE
e04c18a2 2455 $feed_check_qpart
ef393de7 2456 ttrss_user_entries.ref_id = ttrss_entries.id AND
c36bf4d5 2457 ttrss_user_entries.owner_uid = '$owner_uid' AND
ef393de7 2458 $search_query_part
36184020 2459 $filter_query_part
ef393de7 2460 $view_query_part
97e5dbb2 2461 $since_id_part
ef393de7 2462 $query_strategy_part ORDER BY $order_by
95a82c08 2463 $limit_query_part $offset_query_part";
4bc311fc 2464
b4e75b2a 2465 if ($_REQUEST["debug"]) print $query;
4bc311fc
AD
2466
2467 $result = db_query($link, $query);
8d505d78 2468
ef393de7
AD
2469 } else {
2470 // browsing by tag
8d505d78 2471
147f5632
CM
2472 $select_qpart = "SELECT DISTINCT " .
2473 "date_entered," .
2474 "guid," .
2475 "note," .
2476 "ttrss_entries.id as id," .
2477 "title," .
2478 "updated," .
2479 "unread," .
2480 "feed_id," .
2481 "orig_feed_id," .
2482 "marked," .
d1fc2f92
AD
2483 "num_comments, " .
2484 "comments, " .
c0644ee4
AD
2485 "tag_cache," .
2486 "label_cache," .
147f5632
CM
2487 "link," .
2488 "last_read," .
2489 SUBSTRING_FOR_DATE . "(last_read,1,19) as last_read_noms," .
97e5dbb2 2490 $since_id_part .
147f5632
CM
2491 $vfeed_query_part .
2492 $content_query_part .
2493 SUBSTRING_FOR_DATE . "(updated,1,19) as updated_noms," .
2494 "score ";
2495
ef393de7 2496 $feed_kind = "Tags";
147f5632
CM
2497 $all_tags = explode(",", $feed);
2498 if ($search_mode == 'any') {
2499 $tag_sql = "tag_name in (" . implode(", ", array_map("db_quote", $all_tags)) . ")";
2500 $from_qpart = " FROM ttrss_entries,ttrss_user_entries,ttrss_tags ";
2501 $where_qpart = " WHERE " .
2502 "ref_id = ttrss_entries.id AND " .
2503 "ttrss_user_entries.owner_uid = $owner_uid AND " .
2504 "post_int_id = int_id AND $tag_sql AND " .
2505 $view_query_part .
2506 $search_query_part .
2507 $query_strategy_part . " ORDER BY $order_by " .
2508 $limit_query_part;
8d505d78 2509
147f5632
CM
2510 } else {
2511 $i = 1;
2512 $sub_selects = array();
2513 $sub_ands = array();
2514 foreach ($all_tags as $term) {
2515 array_push($sub_selects, "(SELECT post_int_id from ttrss_tags WHERE tag_name = " . db_quote($term) . " AND owner_uid = $owner_uid) as A$i");
2516 $i++;
2517 }
2518 if ($i > 2) {
2519 $x = 1;
2520 $y = 2;
2521 do {
2522 array_push($sub_ands, "A$x.post_int_id = A$y.post_int_id");
2523 $x++;
2524 $y++;
2525 } while ($y < $i);
2526 }
2527 array_push($sub_ands, "A1.post_int_id = ttrss_user_entries.int_id and ttrss_user_entries.owner_uid = $owner_uid");
2528 array_push($sub_ands, "ttrss_user_entries.ref_id = ttrss_entries.id");
2529 $from_qpart = " FROM " . implode(", ", $sub_selects) . ", ttrss_user_entries, ttrss_entries";
2530 $where_qpart = " WHERE " . implode(" AND ", $sub_ands);
2531 }
2532 // error_log("TAG SQL: " . $tag_sql);
2533 // $tag_sql = "tag_name = '$feed'"; DEFAULT way
2534
2535 // error_log("[". $select_qpart . "][" . $from_qpart . "][" .$where_qpart . "]");
2536 $result = db_query($link, $select_qpart . $from_qpart . $where_qpart);
ef393de7
AD
2537 }
2538
c7188969 2539 return array($result, $feed_title, $feed_site_url, $last_error);
8d505d78 2540
ef393de7
AD
2541 }
2542
183ff16c 2543 function sanitize($link, $str, $force_strip_tags = false, $owner = false, $site_url = false) {
35ffb5b9 2544 global $purifier;
8cc3c778 2545
ceb0cab5
AD
2546 if (!$owner) $owner = $_SESSION["uid"];
2547
96811a55
AD
2548 $res = trim($str); if (!$res) return '';
2549
31303c6b
AD
2550 // create global Purifier object if needed
2551 if (!$purifier) {
2552 require_once 'lib/htmlpurifier/library/HTMLPurifier.auto.php';
2553
2554 $config = HTMLPurifier_Config::createDefault();
2555
2556 $allowed = "p,a[href],i,em,b,strong,code,pre,blockquote,br,img[src|alt|title],ul,ol,li,h1,h2,h3,h4,s,object[classid|type|id|name|width|height|codebase],param[name|value],table,tr,td";
2557
2558 $config->set('HTML.SafeObject', true);
2559 @$config->set('HTML', 'Allowed', $allowed);
2560 $config->set('Output.FlashCompat', true);
2561 $config->set('Attr.EnableID', true);
2562 if (!defined('MOBILE_VERSION')) {
2563 @$config->set('Cache', 'SerializerPath', CACHE_DIR . "/htmlpurifier");
2564 } else {
2565 @$config->set('Cache', 'SerializerPath', "../" . CACHE_DIR . "/htmlpurifier");
2566 }
2567
2568 $purifier = new HTMLPurifier($config);
2569 }
2570
388c645a 2571 $res = $purifier->purify($res);
f826eee1 2572
ceb0cab5 2573 if (get_pref($link, "STRIP_IMAGES", $owner)) {
8dccabed 2574 $res = preg_replace('/<img[^>]+>/is', '', $res);
7514749d 2575 }
8dccabed 2576
46137483
AD
2577 if (strpos($res, "href=") === false)
2578 $res = rewrite_urls($res);
533c0ea6 2579
8cc3c778
AD
2580 $charset_hack = '<head>
2581 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
2582 </head>';
2583
96811a55
AD
2584 $res = trim($res); if (!$res) return '';
2585
8cc3c778
AD
2586 libxml_use_internal_errors(true);
2587
2588 $doc = new DOMDocument();
2589 $doc->loadHTML($charset_hack . $res);
2590 $xpath = new DOMXPath($doc);
8d505d78 2591
8cc3c778 2592 $entries = $xpath->query('(//a[@href]|//img[@src])');
3f770c87 2593 $br_inserted = 0;
8cc3c778
AD
2594
2595 foreach ($entries as $entry) {
2596
2597 if ($site_url) {
2598
2599 if ($entry->hasAttribute('href'))
2600 $entry->setAttribute('href',
2601 rewrite_relative_url($site_url, $entry->getAttribute('href')));
8d505d78 2602
8cc3c778 2603 if ($entry->hasAttribute('src'))
8d505d78 2604 if (preg_match('/^image.php\?i=[a-z0-9]+$/', $entry->getAttribute('src')) == 0)
b8998470
AD
2605 $entry->setAttribute('src',
2606 rewrite_relative_url($site_url, $entry->getAttribute('src')));
8cc3c778
AD
2607 }
2608
fa403733 2609 if (strtolower($entry->nodeName) == "a") {
c401d5c9 2610 $entry->setAttribute("target", "_blank");
fa403733
AD
2611 }
2612
3f770c87 2613 if (strtolower($entry->nodeName) == "img" && !$br_inserted) {
fa403733
AD
2614 $br = $doc->createElement("br");
2615
3f770c87 2616 if ($entry->parentNode->nextSibling) {
fa403733 2617 $entry->parentNode->insertBefore($br, $entry->nextSibling);
3f770c87
AD
2618 $br_inserted = 1;
2619 }
fa403733 2620
8cc3c778 2621 }
8dccabed 2622 }
8d505d78 2623
1f6131f5 2624 $node = $doc->getElementsByTagName('body')->item(0);
8dccabed 2625
8d505d78 2626 return $doc->saveXML($node);
183ad07b 2627 }
b72c3ef8 2628
45004d43
AD
2629 /**
2630 * Send by mail a digest of last articles.
8d505d78 2631 *
45004d43
AD
2632 * @param mixed $link The database connection.
2633 * @param integer $limit The maximum number of articles by digest.
2634 * @return boolean Return false if digests are not enabled.
2635 */
9cd7c995
AD
2636 function send_headlines_digests($link, $limit = 100) {
2637
31303c6b
AD
2638 require_once 'lib/phpmailer/class.phpmailer.php';
2639
09e8bdfd 2640 $user_limit = 15; // amount of users to process (e.g. emails to send out)
5430c959 2641 $days = 1;
9cd7c995
AD
2642
2643 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
2644
2645 if (DB_TYPE == "pgsql") {
2646 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
2647 } else if (DB_TYPE == "mysql") {
2648 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
2649 }
2650
8d505d78 2651 $result = db_query($link, "SELECT id,email FROM ttrss_users
9cd7c995
AD
2652 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
2653
2654 while ($line = db_fetch_assoc($result)) {
dc85be2b 2655
9cd7c995
AD
2656 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
2657 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
2658
dc85be2b
AD
2659 $do_catchup = get_pref($link, 'DIGEST_CATCHUP', $line['id'], false);
2660
9cd7c995
AD
2661 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
2662 $digest = $tuple[0];
2663 $headlines_count = $tuple[1];
dc85be2b 2664 $affected_ids = $tuple[2];
c62a2c21 2665 $digest_text = $tuple[3];
9cd7c995
AD
2666
2667 if ($headlines_count > 0) {
a8931123 2668
c62a2c21 2669 $mail = new PHPMailer();
a8931123 2670
d134e3a3
AD
2671 $mail->PluginDir = "lib/phpmailer/";
2672 $mail->SetLanguage("en", "lib/phpmailer/language/");
a8931123 2673
c62a2c21 2674 $mail->CharSet = "UTF-8";
a8931123 2675
09e8bdfd
AD
2676 $mail->From = SMTP_FROM_ADDRESS;
2677 $mail->FromName = SMTP_FROM_NAME;
c62a2c21 2678 $mail->AddAddress($line["email"], $line["login"]);
c7ddac5c 2679
09e8bdfd
AD
2680 if (SMTP_HOST) {
2681 $mail->Host = SMTP_HOST;
a8931123 2682 $mail->Mailer = "smtp";
09e8bdfd
AD
2683 $mail->SMTPAuth = SMTP_LOGIN != '';
2684 $mail->Username = SMTP_LOGIN;
2685 $mail->Password = SMTP_PASSWORD;
c62a2c21 2686 }
a8931123 2687
c62a2c21 2688 $mail->IsHTML(true);
163a295e 2689 $mail->Subject = DIGEST_SUBJECT;
c62a2c21
AD
2690 $mail->Body = $digest;
2691 $mail->AltBody = $digest_text;
a8931123 2692
c62a2c21 2693 $rc = $mail->Send();
a8931123 2694
c62a2c21 2695 if (!$rc) print "ERROR: " . $mail->ErrorInfo;
a8931123 2696
9cd7c995 2697 print "RC=$rc\n";
a8931123 2698
c62a2c21 2699 if ($rc && $do_catchup) {
dc85be2b 2700 print "Marking affected articles as read...\n";
9968d46f 2701 catchupArticlesById($link, $affected_ids, 0, $line["id"]);
dc85be2b 2702 }
9cd7c995
AD
2703 } else {
2704 print "No headlines\n";
2705 }
019dd98d 2706
8d505d78 2707 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
019dd98d 2708 WHERE id = " . $line["id"]);
9cd7c995
AD
2709 }
2710 }
2711
cedd3e89
AD
2712 print "All done.\n";
2713
9cd7c995
AD
2714 }
2715
7e3634d9 2716 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
c62a2c21 2717
fe7537b5 2718 require_once "lib/MiniTemplator.class.php";
c62a2c21
AD
2719
2720 $tpl = new MiniTemplator;
2721 $tpl_t = new MiniTemplator;
2722
2723 $tpl->readTemplateFromFile("templates/digest_template_html.txt");
2724 $tpl_t->readTemplateFromFile("templates/digest_template.txt");
2725
2726 $tpl->setVariable('CUR_DATE', date('Y/m/d'));
2727 $tpl->setVariable('CUR_TIME', date('G:i'));
2728
2729 $tpl_t->setVariable('CUR_DATE', date('Y/m/d'));
2730 $tpl_t->setVariable('CUR_TIME', date('G:i'));
7e3634d9 2731
dc85be2b
AD
2732 $affected_ids = array();
2733
7e3634d9 2734 if (DB_TYPE == "pgsql") {
25ea2805 2735 $interval_query = "ttrss_entries.date_updated > NOW() - INTERVAL '$days days'";
7e3634d9 2736 } else if (DB_TYPE == "mysql") {
25ea2805 2737 $interval_query = "ttrss_entries.date_updated > DATE_SUB(NOW(), INTERVAL $days DAY)";
7e3634d9
AD
2738 }
2739
2740 $result = db_query($link, "SELECT ttrss_entries.title,
2741 ttrss_feeds.title AS feed_title,
25ea2805 2742 date_updated,
dc85be2b 2743 ttrss_user_entries.ref_id,
7e3634d9 2744 link,
c62a2c21 2745 SUBSTRING(content, 1, 120) AS excerpt,
fc2b26a6 2746 ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
8d505d78
AD
2747 FROM
2748 ttrss_user_entries,ttrss_entries,ttrss_feeds
2749 WHERE
2750 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
3dd9183c 2751 AND include_in_digest = true
7e3634d9 2752 AND $interval_query
448b0abd 2753 AND ttrss_user_entries.owner_uid = $user_id
8d505d78 2754 AND unread = true
25ea2805 2755 ORDER BY ttrss_feeds.title, date_updated DESC
7e3634d9
AD
2756 LIMIT $limit");
2757
2758 $cur_feed_title = "";
2759
9cd7c995
AD
2760 $headlines_count = db_num_rows($result);
2761
c62a2c21
AD
2762 $headlines = array();
2763
7e3634d9 2764 while ($line = db_fetch_assoc($result)) {
c62a2c21
AD
2765 array_push($headlines, $line);
2766 }
2767
8d505d78 2768 for ($i = 0; $i < sizeof($headlines); $i++) {
c62a2c21
AD
2769
2770 $line = $headlines[$i];
dc85be2b
AD
2771
2772 array_push($affected_ids, $line["ref_id"]);
2773
324944f3
AD
2774 $updated = make_local_datetime($link, $line['last_updated'], false,
2775 $user_id);
7e3634d9 2776
c62a2c21
AD
2777 $tpl->setVariable('FEED_TITLE', $line["feed_title"]);
2778 $tpl->setVariable('ARTICLE_TITLE', $line["title"]);
2779 $tpl->setVariable('ARTICLE_LINK', $line["link"]);
2780 $tpl->setVariable('ARTICLE_UPDATED', $updated);
8d505d78 2781 $tpl->setVariable('ARTICLE_EXCERPT',
163a295e 2782 truncate_string(strip_tags($line["excerpt"]), 100));
7e3634d9 2783
c62a2c21
AD
2784 $tpl->addBlock('article');
2785
2786 $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]);
2787 $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]);
2788 $tpl_t->setVariable('ARTICLE_LINK', $line["link"]);
2789 $tpl_t->setVariable('ARTICLE_UPDATED', $updated);
8d505d78 2790// $tpl_t->setVariable('ARTICLE_EXCERPT',
c62a2c21
AD
2791// truncate_string(strip_tags($line["excerpt"]), 100));
2792
2793 $tpl_t->addBlock('article');
2794
2795 if ($headlines[$i]['feed_title'] != $headlines[$i+1]['feed_title']) {
2796 $tpl->addBlock('feed');
2797 $tpl_t->addBlock('feed');
7e3634d9
AD
2798 }
2799
7e3634d9
AD
2800 }
2801
c62a2c21
AD
2802 $tpl->addBlock('digest');
2803 $tpl->generateOutputToString($tmp);
2804
2805 $tpl_t->addBlock('digest');
2806 $tpl_t->generateOutputToString($tmp_t);
7e3634d9 2807
c62a2c21 2808 return array($tmp, $headlines_count, $affected_ids, $tmp_t);
7e3634d9
AD
2809 }
2810
73495fd1 2811 function check_for_update($link) {
63855db1
AD
2812 if (CHECK_FOR_NEW_VERSION && $_SESSION['access_level'] >= 10) {
2813 $version_url = "http://tt-rss.org/version.php?ver=" . VERSION;
b72c3ef8 2814
63855db1 2815 $version_data = @fetch_file_contents($version_url);
b72c3ef8 2816
63855db1
AD
2817 if ($version_data) {
2818 $version_data = json_decode($version_data, true);
8d505d78 2819 if ($version_data && $version_data['version']) {
f67d9754 2820
63855db1 2821 if (version_compare(VERSION, $version_data['version']) == -1) {
e91ad1e9 2822 return $version_data;
63855db1
AD
2823 }
2824 }
f67d9754 2825 }
b72c3ef8 2826 }
63855db1 2827 return false;
b72c3ef8 2828 }
472782e8 2829
18eddb2c
AD
2830 function markArticlesById($link, $ids, $cmode) {
2831
2832 $tmp_ids = array();
2833
2834 foreach ($ids as $id) {
2835 array_push($tmp_ids, "ref_id = '$id'");
2836 }
2837
2838 $ids_qpart = join(" OR ", $tmp_ids);
2839
2840 if ($cmode == 0) {
8d505d78 2841 db_query($link, "UPDATE ttrss_user_entries SET
18eddb2c
AD
2842 marked = false,last_read = NOW()
2843 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2844 } else if ($cmode == 1) {
8d505d78 2845 db_query($link, "UPDATE ttrss_user_entries SET
18eddb2c
AD
2846 marked = true
2847 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2848 } else {
8d505d78 2849 db_query($link, "UPDATE ttrss_user_entries SET
18eddb2c
AD
2850 marked = NOT marked,last_read = NOW()
2851 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2852 }
2853 }
2854
e4f4b46f
AD
2855 function publishArticlesById($link, $ids, $cmode) {
2856
2857 $tmp_ids = array();
2858
2859 foreach ($ids as $id) {
2860 array_push($tmp_ids, "ref_id = '$id'");
2861 }
2862
2863 $ids_qpart = join(" OR ", $tmp_ids);
2864
2865 if ($cmode == 0) {
8d505d78 2866 db_query($link, "UPDATE ttrss_user_entries SET
e4f4b46f
AD
2867 published = false,last_read = NOW()
2868 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2869 } else if ($cmode == 1) {
8d505d78 2870 db_query($link, "UPDATE ttrss_user_entries SET
e4f4b46f
AD
2871 published = true
2872 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2873 } else {
8d505d78 2874 db_query($link, "UPDATE ttrss_user_entries SET
e4f4b46f
AD
2875 published = NOT published,last_read = NOW()
2876 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2877 }
2fcec4c4
AD
2878
2879 if (PUBSUBHUBBUB_HUB) {
2880 $rss_link = get_self_url_prefix() .
e0d91d84 2881 "/public.php?op=rss&id=-2&key=" .
2fcec4c4
AD
2882 get_feed_access_key($link, -2, false);
2883
2884 $p = new Publisher(PUBSUBHUBBUB_HUB);
2885
2886 $pubsub_result = $p->publish_update($rss_link);
2887 }
e4f4b46f
AD
2888 }
2889
9968d46f
AD
2890 function catchupArticlesById($link, $ids, $cmode, $owner_uid = false) {
2891
2892 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
ed41f171 2893 if (count($ids) == 0) return;
472782e8
AD
2894
2895 $tmp_ids = array();
2896
2897 foreach ($ids as $id) {
2898 array_push($tmp_ids, "ref_id = '$id'");
2899 }
2900
2901 $ids_qpart = join(" OR ", $tmp_ids);
2902
2903 if ($cmode == 0) {
8d505d78 2904 db_query($link, "UPDATE ttrss_user_entries SET
472782e8 2905 unread = false,last_read = NOW()
9968d46f 2906 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8 2907 } else if ($cmode == 1) {
8d505d78 2908 db_query($link, "UPDATE ttrss_user_entries SET
472782e8 2909 unread = true
9968d46f 2910 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8 2911 } else {
8d505d78 2912 db_query($link, "UPDATE ttrss_user_entries SET
472782e8 2913 unread = NOT unread,last_read = NOW()
9968d46f 2914 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8 2915 }
0737b95a
AD
2916
2917 /* update ccache */
2918
2919 $result = db_query($link, "SELECT DISTINCT feed_id FROM ttrss_user_entries
2920 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
2921
2922 while ($line = db_fetch_assoc($result)) {
2923 ccache_update($link, $line["feed_id"], $owner_uid);
2924 }
472782e8
AD
2925 }
2926
e097e8be
AD
2927 function catchupArticleById($link, $id, $cmode) {
2928
2929 if ($cmode == 0) {
8d505d78 2930 db_query($link, "UPDATE ttrss_user_entries SET
e097e8be
AD
2931 unread = false,last_read = NOW()
2932 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
2933 } else if ($cmode == 1) {
8d505d78 2934 db_query($link, "UPDATE ttrss_user_entries SET
e097e8be
AD
2935 unread = true
2936 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
2937 } else {
8d505d78 2938 db_query($link, "UPDATE ttrss_user_entries SET
e097e8be
AD
2939 unread = NOT unread,last_read = NOW()
2940 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
2941 }
ab197ae1
AD
2942
2943 $feed_id = getArticleFeed($link, $id);
2944 ccache_update($link, $feed_id, $_SESSION["uid"]);
e097e8be
AD
2945 }
2946
1f64b1be 2947 function make_guid_from_title($title) {
8d505d78 2948 return preg_replace("/[ \"\',.:;]/", "-",
fefef828 2949 mb_strtolower(strip_tags($title), 'utf-8'));
1f64b1be
AD
2950 }
2951
ca5133cb 2952 function get_article_tags($link, $id, $owner_uid = 0, $tag_cache = false) {
0b126ac2 2953
bd3f2ade
AD
2954 global $memcache;
2955
0b126ac2
AD
2956 $a_id = db_escape_string($id);
2957
bc976a8c
AD
2958 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
2959
8d505d78 2960 $query = "SELECT DISTINCT tag_name,
0c3d1c68 2961 owner_uid as owner FROM
0b126ac2 2962 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
bd3f2ade 2963 ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name";
0b126ac2 2964
bd3f2ade 2965 $obj_id = md5("TAGS:$owner_uid:$id");
8d505d78 2966 $tags = array();
bd3f2ade
AD
2967
2968 if ($memcache && $obj = $memcache->get($obj_id)) {
2969 $tags = $obj;
2970 } else {
490c366d
AD
2971 /* check cache first */
2972
ca5133cb
AD
2973 if ($tag_cache === false) {
2974 $result = db_query($link, "SELECT tag_cache FROM ttrss_user_entries
2975 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
490c366d 2976
ca5133cb
AD
2977 $tag_cache = db_fetch_result($result, 0, "tag_cache");
2978 }
490c366d
AD
2979
2980 if ($tag_cache) {
2981 $tags = explode(",", $tag_cache);
2982 } else {
bd3f2ade 2983
490c366d
AD
2984 /* do it the hard way */
2985
2986 $tmp_result = db_query($link, $query);
2987
2988 while ($tmp_line = db_fetch_assoc($tmp_result)) {
8d505d78 2989 array_push($tags, $tmp_line["tag_name"]);
490c366d
AD
2990 }
2991
2992 /* update the cache */
2993
2994 $tags_str = db_escape_string(join(",", $tags));
2995
8d505d78 2996 db_query($link, "UPDATE ttrss_user_entries
490c366d
AD
2997 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
2998 AND owner_uid = " . $_SESSION["uid"]);
bd3f2ade
AD
2999 }
3000
3001 if ($memcache) $memcache->add($obj_id, $tags, 0, 3600);
0b126ac2
AD
3002 }
3003
3004 return $tags;
3005 }
3006
d62a3b63
AD
3007 function trim_array($array) {
3008 $tmp = $array;
3415b075 3009 array_walk($tmp, 'trim');
d62a3b63
AD
3010 return $tmp;
3011 }
3012
be832a1a 3013 function tag_is_valid($tag) {
ef063748
AD
3014 if ($tag == '') return false;
3015 if (preg_match("/^[0-9]*$/", $tag)) return false;
41f7498a 3016 if (mb_strlen($tag) > 250) return false;
ef063748 3017
31365729
AD
3018 if (function_exists('iconv')) {
3019 $tag = iconv("utf-8", "utf-8", $tag);
3020 }
3021
ef063748
AD
3022 if (!$tag) return false;
3023
3024 return true;
be832a1a
AD
3025 }
3026
afb12ed0
AD
3027 function render_login_form($link, $mobile = 0) {
3028 switch ($mobile) {
3029 case 0:
793185a9 3030 require_once "login_form.php";
afb12ed0
AD
3031 break;
3032 case 1:
793185a9 3033 require_once "mobile/login_form.php";
afb12ed0
AD
3034 break;
3035 case 2:
3036 require_once "mobile/classic/login_form.php";
793185a9 3037 }
01a87dff
AD
3038 }
3039
dc56b3b7
AD
3040 // from http://developer.apple.com/internet/safari/faq.html
3041 function no_cache_incantation() {
3042 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
3043 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
3044 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
3045 header("Cache-Control: post-check=0, pre-check=0", false);
3046 header("Pragma: no-cache"); // HTTP/1.0
3047 }
3048
42395d28 3049 function format_warning($msg, $id = "") {
883fee8d 3050 global $link;
8d505d78 3051 return "<div class=\"warning\" id=\"$id\">
883fee8d 3052 <img src=\"".theme_image($link, "images/sign_excl.png")."\">$msg</div>";
0d32b41e
AD
3053 }
3054
08ac193a 3055 function format_notice($msg, $id = "") {
883fee8d 3056 global $link;
8d505d78 3057 return "<div class=\"notice\" id=\"$id\">
883fee8d 3058 <img src=\"".theme_image($link, "images/sign_info.png")."\">$msg</div>";
0d32b41e
AD
3059 }
3060
08ac193a 3061 function format_error($msg, $id = "") {
883fee8d 3062 global $link;
8d505d78 3063 return "<div class=\"error\" id=\"$id\">
883fee8d 3064 <img src=\"".theme_image($link, "images/sign_excl.png")."\">$msg</div>";
68d2f95e
AD
3065 }
3066
4dccf1ed
AD
3067 function print_notice($msg) {
3068 return print format_notice($msg);
3069 }
3070
3071 function print_warning($msg) {
3072 return print format_warning($msg);
3073 }
3074
68d2f95e
AD
3075 function print_error($msg) {
3076 return print format_error($msg);
3077 }
3078
3079
4dccf1ed
AD
3080 function T_sprintf() {
3081 $args = func_get_args();
3082 return vsprintf(__(array_shift($args)), $args);
3083 }
3084
51682b23
AD
3085 function format_inline_player($link, $url, $ctype) {
3086
3087 $entry = "";
3088
8d505d78 3089 if (strpos($ctype, "audio/") === 0) {
c3edc667
AD
3090
3091 if ($_SESSION["hasAudio"] && (strpos($ctype, "ogg") !== false ||
8d505d78 3092 strpos($_SERVER['HTTP_USER_AGENT'], "Chrome") !== false ||
c3edc667
AD
3093 strpos($_SERVER['HTTP_USER_AGENT'], "Safari") !== false )) {
3094
3095 $id = 'AUDIO-' . uniqid();
3096
3097 $entry .= "<audio id=\"$id\"\">
3098 <source src=\"$url\"></source>
8d505d78 3099 </audio>";
c3edc667 3100
8d505d78 3101 $entry .= "<span onclick=\"player(this)\"
c3edc667
AD
3102 title=\"".__("Click to play")."\" status=\"0\"
3103 class=\"player\" audio-id=\"$id\">".__("Play")."</span>";
3104
3105 } else {
8d505d78
AD
3106
3107 $entry .= "<object type=\"application/x-shockwave-flash\"
ad95edc2 3108 data=\"lib/button/musicplayer.swf?song_url=$url\"
8d505d78
AD
3109 width=\"17\" height=\"17\" style='float : left; margin-right : 5px;'>
3110 <param name=\"movie\"
ad95edc2 3111 value=\"lib/button/musicplayer.swf?song_url=$url\" />
8d505d78 3112 </object>";
c3edc667 3113 }
51682b23
AD
3114 }
3115
c3edc667
AD
3116 $filename = substr($url, strrpos($url, "/")+1);
3117
3118 $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
3119 $filename . " (" . $ctype . ")" . "</a>";
3120
51682b23
AD
3121 return $entry;
3122 }
3123
9949bd15 3124 function format_article($link, $id, $mark_as_read = true, $zoom_mode = false) {
3de0261a 3125
009646d2
AD
3126 $rv = array();
3127
3128 $rv['id'] = $id;
3129
10eb9da8 3130 /* we can figure out feed_id from article id anyway, why do we
e04c18a2 3131 * pass feed_id here? let's ignore the argument :( */
10eb9da8
AD
3132
3133 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
3134 WHERE ref_id = '$id'");
3135
e04c18a2 3136 $feed_id = (int) db_fetch_result($result, 0, "feed_id");
10eb9da8 3137
009646d2
AD
3138 $rv['feed_id'] = $feed_id;
3139
3140 //if (!$zoom_mode) { print "<article id='$id'><![CDATA["; };
3de0261a 3141
54e61a68 3142 $result = db_query($link, "SELECT rtl_content, always_display_enclosures FROM ttrss_feeds
3de0261a
AD
3143 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
3144
3145 if (db_num_rows($result) == 1) {
3146 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
54e61a68 3147 $always_display_enclosures = sql_bool_to_bool(db_fetch_result($result, 0, "always_display_enclosures"));
3de0261a
AD
3148 } else {
3149 $rtl_content = false;
54e61a68 3150 $always_display_enclosures = false;
3de0261a
AD
3151 }
3152
3153 if ($rtl_content) {
3154 $rtl_tag = "dir=\"RTL\"";
3155 $rtl_class = "RTL";
3156 } else {
3157 $rtl_tag = "";
3158 $rtl_class = "";
3159 }
3160
3161 if ($mark_as_read) {
8d505d78
AD
3162 $result = db_query($link, "UPDATE ttrss_user_entries
3163 SET unread = false,last_read = NOW()
3de0261a 3164 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
8a4c759e
AD
3165
3166 ccache_update($link, $feed_id, $_SESSION["uid"]);
3de0261a
AD
3167 }
3168
3169 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
fc2b26a6 3170 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
3de0261a 3171 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
8cc3c778 3172 (SELECT site_url FROM ttrss_feeds WHERE id = feed_id) as site_url,
3de0261a 3173 num_comments,
9c506873 3174 tag_cache,
c7e51de1 3175 author,
ef83538d 3176 orig_feed_id,
c7e51de1 3177 note
3de0261a
AD
3178 FROM ttrss_entries,ttrss_user_entries
3179 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
3180
3181 if ($result) {
3182
3de0261a
AD
3183 $line = db_fetch_assoc($result);
3184
3185 if ($line["icon_url"]) {
8e289ca1 3186 $feed_icon = "<img src=\"" . $line["icon_url"] . "\">";
3de0261a
AD
3187 } else {
3188 $feed_icon = "&nbsp;";
3189 }
3190
8cc3c778
AD
3191 $feed_site_url = $line['site_url'];
3192
3de0261a
AD
3193 $num_comments = $line["num_comments"];
3194 $entry_comments = "";
3195
3196 if ($num_comments > 0) {
3197 if ($line["comments"]) {
3198 $comments_url = $line["comments"];
3199 } else {
3200 $comments_url = $line["link"];
3201 }
7514749d 3202 $entry_comments = "<a target='_blank' href=\"$comments_url\">$num_comments comments</a>";
3de0261a
AD
3203 } else {
3204 if ($line["comments"] && $line["link"] != $line["comments"]) {
7514749d 3205 $entry_comments = "<a target='_blank' href=\"".$line["comments"]."\">comments</a>";
8d505d78 3206 }
3de0261a
AD
3207 }
3208
eedfb635
AD
3209 if ($zoom_mode) {
3210 header("Content-Type: text/html");
009646d2 3211 $rv['content'] .= "<html><head>
5bb0cc8e 3212 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
eedfb635
AD
3213 <title>Tiny Tiny RSS - ".$line["title"]."</title>
3214 <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss.css\">
3215 </head><body>";
3216 }
3217
009646d2 3218 $rv['content'] .= "<div id=\"PTITLE-$id\" style=\"display : none\">" .
6f3976c9 3219 truncate_string(strip_tags($line['title']), 15) . "</div>";
eedfb635 3220
009646d2 3221 $rv['content'] .= "<div class=\"postReply\" id=\"POST-$id\">";
bc372fe3 3222
009646d2 3223 $rv['content'] .= "<div onclick=\"return postClicked(event, $id)\"
bc372fe3 3224 class=\"postHeader\" id=\"POSTHDR-$id\">";
3de0261a
AD
3225
3226 $entry_author = $line["author"];
3227
3228 if ($entry_author) {
60164936 3229 $entry_author = __(" - ") . $entry_author;
3de0261a
AD
3230 }
3231
8d505d78 3232 $parsed_updated = make_local_datetime($link, $line["updated"], true,
324944f3
AD
3233 false, true);
3234
009646d2 3235 $rv['content'] .= "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
3de0261a
AD
3236
3237 if ($line["link"]) {
009646d2 3238 $rv['content'] .= "<div clear='both'><a target='_blank'
a64029e5 3239 title=\"".htmlspecialchars($line['title'])."\"
8d505d78
AD
3240 href=\"" .
3241 $line["link"] . "\">" .
3242 truncate_string($line["title"], 100) .
a64029e5 3243 "<span class='author'>$entry_author</span></a></div>";
3de0261a 3244 } else {
009646d2 3245 $rv['content'] .= "<div clear='both'>" . $line["title"] . "$entry_author</div>";
3de0261a
AD
3246 }
3247
9c506873
AD
3248 $tag_cache = $line["tag_cache"];
3249
3250 if (!$tag_cache)
3251 $tags = get_article_tags($link, $id);
3252 else
3253 $tags = explode(",", $tag_cache);
3254
0780f4f4
AD
3255 $tags_str = format_tags_string($tags, $id);
3256 $tags_str_full = join(", ", $tags);
3257
3258 if (!$tags_str_full) $tags_str_full = __("no tags");
e7544143 3259
3de0261a
AD
3260 if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
3261
009646d2 3262 $rv['content'] .= "<div style='float : right'>
8d505d78 3263 <img src='".theme_image($link, 'images/tag.png')."'
e9823609 3264 class='tagsPic' alt='Tags' title='Tags'>&nbsp;";
eedfb635
AD
3265
3266 if (!$zoom_mode) {
009646d2 3267 $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span>
8d505d78 3268 <a title=\"".__('Edit tags for this article')."\"
31a53903 3269 href=\"#\" onclick=\"editArticleTags($id, $feed_id)\">(+)</a>";
4710e3dc 3270
0780f4f4
AD
3271 $rv['content'] .= "<div dojoType=\"dijit.Tooltip\"
3272 id=\"ATSTRTIP-$id\" connectId=\"ATSTR-$id\"
3273 position=\"below\">$tags_str_full</div>";
3274
009646d2 3275 $rv['content'] .= "<img src=\"".theme_image($link, 'images/art-zoom.png')."\"
9ed0b90f 3276 class='tagsPic' style=\"cursor : pointer\"
ca07f49e 3277 onclick=\"postOpenInNewTab(event, $id)\"
6f3976c9 3278 alt='Zoom' title='".__('Open article in new tab')."'>";
c7e51de1 3279
741b6090 3280 //$note_escaped = htmlspecialchars($line['note'], ENT_QUOTES);
c7e51de1 3281
009646d2 3282 $rv['content'] .= "<img src=\"".theme_image($link, 'images/art-pub-note.png')."\"
9ed0b90f 3283 class='tagsPic' style=\"cursor : pointer\"
741b6090
AD
3284 onclick=\"editArticleNote($id)\"
3285 alt='PubNote' title='".__('Edit article note')."'>";
c7e51de1 3286
09e8bdfd
AD
3287 $rv['content'] .= "<img src=\"".theme_image($link, 'images/art-email.png')."\"
3288 class='tagsPic' style=\"cursor : pointer\"
3289 onclick=\"emailArticle($id)\"
3290 alt='Zoom' title='".__('Forward by email')."'>";
31a53903 3291
411fe209 3292 if (ENABLE_TWEET_BUTTON) {
009646d2 3293 $rv['content'] .= "<img src=\"".theme_image($link, 'images/art-tweet.png')."\"
411fe209
AD
3294 class='tagsPic' style=\"cursor : pointer\"
3295 onclick=\"tweetArticle($id)\"
3296 alt='Zoom' title='".__('Share on Twitter')."'>";
3297 }
3298
83cd33fc
AD
3299 $rv['content'] .= "<img src=\"".theme_image($link, 'images/art-share.png')."\"
3300 class='tagsPic' style=\"cursor : pointer\"
3301 onclick=\"shareArticle(".$line['int_id'].")\"
3302 alt='Zoom' title='".__('Share by URL')."'>";
3303
009646d2 3304 $rv['content'] .= "<img src=\"".theme_image($link, 'images/digest_checkbox.png')."\"
6f3976c9 3305 class='tagsPic' style=\"cursor : pointer\"
e3387e2d 3306 onclick=\"closeArticlePanel($id)\"
6f3976c9
AD
3307 alt='Zoom' title='".__('Close this panel')."'>";
3308
24ecbcae
AD
3309 } else {
3310 $tags_str = strip_tags($tags_str);
009646d2 3311 $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span>";
eedfb635 3312 }
009646d2
AD
3313 $rv['content'] .= "</div>";
3314 $rv['content'] .= "<div clear='both'>$entry_comments</div>";
3de0261a 3315
ef83538d
AD
3316 if ($line["orig_feed_id"]) {
3317
3318 $tmp_result = db_query($link, "SELECT * FROM ttrss_archived_feeds
3319 WHERE id = ".$line["orig_feed_id"]);
3320
3321 if (db_num_rows($tmp_result) != 0) {
3322
009646d2
AD
3323 $rv['content'] .= "<div clear='both'>";
3324 $rv['content'] .= __("Originally from:");
ef83538d 3325
009646d2 3326 $rv['content'] .= "&nbsp;";
ef83538d
AD
3327
3328 $tmp_line = db_fetch_assoc($tmp_result);
3329
009646d2 3330 $rv['content'] .= "<a target='_blank'
ef83538d
AD
3331 href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
3332 $tmp_line['title'] . "</a>";
3333
009646d2 3334 $rv['content'] .= "&nbsp;";
ef83538d 3335
009646d2
AD
3336 $rv['content'] .= "<a target='_blank' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
3337 $rv['content'] .= "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_set.gif'></a>";
ef83538d 3338
009646d2 3339 $rv['content'] .= "</div>";
ef83538d
AD
3340 }
3341 }
3342
009646d2 3343 $rv['content'] .= "</div>";
3de0261a 3344
009646d2 3345 $rv['content'] .= "<div id=\"POSTNOTE-$id\">";
c7e51de1 3346 if ($line['note']) {
009646d2 3347 $rv['content'] .= format_article_note($id, $line['note']);
c7e51de1 3348 }
009646d2 3349 $rv['content'] .= "</div>";
c7e51de1 3350
fcfa9ef1
AD
3351 $rv['content'] .= "<div class=\"postIcon\">" .
3352 "<a target=\"_blank\" title=\"".__("Visit the website")."\"$
3353 href=\"".htmlspecialchars($feed_site_url)."\">".
3354 $feed_icon . "</a></div>";
3355
009646d2 3356 $rv['content'] .= "<div class=\"postContent\">";
741b6090 3357
183ff16c 3358 $article_content = sanitize($link, $line["content"], false, false,
741b6090
AD
3359 $feed_site_url);
3360
009646d2 3361 $rv['content'] .= $article_content;
db54143e 3362
009646d2
AD
3363 $rv['content'] .= format_article_enclosures($link, $id,
3364 $always_display_enclosures, $article_content);
ce53e200 3365
009646d2 3366 $rv['content'] .= "</div>";
dad14b51 3367
009646d2 3368 $rv['content'] .= "</div>";
3de0261a
AD
3369
3370 }
3371
009646d2
AD
3372 if ($zoom_mode) {
3373 $rv['content'] .= "
eedfb635 3374 <div style=\"text-align : center\">
2ae69126
AD
3375 <button onclick=\"return window.close()\">".
3376 __("Close this window")."</button></div>";
009646d2 3377 $rv['content'] .= "</body></html>";
eedfb635 3378 }
3de0261a 3379
009646d2
AD
3380 return $rv;
3381
3de0261a
AD
3382 }
3383
79178062
AD
3384 function print_checkpoint($n, $s) {
3385 $ts = getmicrotime();
3386 echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s);
3387 return $ts;
3388 }
3de0261a 3389
79178062
AD
3390 function sanitize_tag($tag) {
3391 $tag = trim($tag);
52d7e7da 3392
79178062 3393 $tag = mb_strtolower($tag, 'utf-8');
bd202c3f 3394
79178062 3395 $tag = preg_replace('/[\'\"\+\>\<]/', "", $tag);
46921916 3396
79178062
AD
3397// $tag = str_replace('"', "", $tag);
3398// $tag = str_replace("+", " ", $tag);
3399 $tag = str_replace("technorati tag: ", "", $tag);
961f4c73 3400
79178062
AD
3401 return $tag;
3402 }
3de0261a 3403
79178062
AD
3404 function get_self_url_prefix() {
3405 return SELF_URL_PATH;
3406 }
a9bcfb8f 3407
79178062 3408 function opml_publish_url($link){
3de0261a 3409
79178062
AD
3410 $url_path = get_self_url_prefix();
3411 $url_path .= "/opml.php?op=publish&key=" .
3412 get_feed_access_key($link, 'OPML:Publish', false, $_SESSION["uid"]);
3de0261a 3413
79178062
AD
3414 return $url_path;
3415 }
0569a712 3416
79178062
AD
3417 /**
3418 * Purge a feed contents, marked articles excepted.
3419 *
3420 * @param mixed $link The database connection.
3421 * @param integer $id The id of the feed to purge.
3422 * @return void
3423 */
3424 function clear_feed_articles($link, $id) {
3de0261a 3425
79178062
AD
3426 if ($id != 0) {
3427 $result = db_query($link, "DELETE FROM ttrss_user_entries
3428 WHERE feed_id = '$id' AND marked = false AND owner_uid = " . $_SESSION["uid"]);
3429 } else {
3430 $result = db_query($link, "DELETE FROM ttrss_user_entries
3431 WHERE feed_id IS NULL AND marked = false AND owner_uid = " . $_SESSION["uid"]);
3de0261a
AD
3432 }
3433
79178062
AD
3434 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
3435 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
a9bcfb8f 3436
79178062
AD
3437 ccache_update($link, $id, $_SESSION['uid']);
3438 } // function clear_feed_articles
45004d43
AD
3439
3440 /**
3441 * Compute the Mozilla Firefox feed adding URL from server HOST and REQUEST_URI.
3442 *
3443 * @return string The Mozilla Firefox feed adding URL.
3444 */
3445 function add_feed_url() {
ed102aa0
AD
3446 //$url_path = ($_SERVER['HTTPS'] != "on" ? 'http://' : 'https://') . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
3447
3448 $url_path = get_self_url_prefix() .
f30ef1fa 3449 "/backend.php?op=pref-feeds&quiet=1&method=add&feed_url=%s";
755a43ee 3450 return $url_path;
45004d43
AD
3451 } // function add_feed_url
3452
3453 /**
3454 * Encrypt a password in SHA1.
8d505d78 3455 *
45004d43
AD
3456 * @param string $pass The password to encrypt.
3457 * @param string $login A optionnal login.
3458 * @return string The encrypted password.
3459 */
1a9f4d3c
AD
3460 function encrypt_password($pass, $login = '') {
3461 if ($login) {
3462 return "SHA1X:" . sha1("$login:$pass");
3463 } else {
3464 return "SHA1:" . sha1($pass);
3465 }
45004d43
AD
3466 } // function encrypt_password
3467
1a9f4d3c 3468
621ffb00
AD
3469 function sanitize_article_content($text) {
3470 # we don't support CDATA sections in articles, they break our own escaping
3471 $text = preg_replace("/\[\[CDATA/", "", $text);
3472 $text = preg_replace("/\]\]\>/", "", $text);
3473 return $text;
3474 }
fee840fb
AD
3475
3476 function load_filters($link, $feed, $owner_uid, $action_id = false) {
3477 $filters = array();
3478
b8ffa322 3479 global $memcache;
fee840fb 3480
1f011328
AD
3481 $obj_id = md5("FILTER:$feed:$owner_uid:$action_id");
3482
b8ffa322
AD
3483 if ($memcache && $obj = $memcache->get($obj_id)) {
3484
b8ffa322
AD
3485 return $obj;
3486
3487 } else {
fee840fb 3488
b8ffa322 3489 if ($action_id) $ftype_query_part = "action_id = '$action_id' AND";
8d505d78 3490
b8ffa322
AD
3491 $result = db_query($link, "SELECT reg_exp,
3492 ttrss_filter_types.name AS name,
3493 ttrss_filter_actions.name AS action,
3494 inverse,
3495 action_param,
3496 filter_param
8d505d78 3497 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
b8ffa322
AD
3498 enabled = true AND
3499 $ftype_query_part
3500 owner_uid = $owner_uid AND
3501 ttrss_filter_types.id = filter_type AND
3502 ttrss_filter_actions.id = action_id AND
3503 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
8d505d78 3504
b8ffa322
AD
3505 while ($line = db_fetch_assoc($result)) {
3506 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
3507 $filter["reg_exp"] = $line["reg_exp"];
3508 $filter["action"] = $line["action"];
3509 $filter["action_param"] = $line["action_param"];
3510 $filter["filter_param"] = $line["filter_param"];
3511 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
8d505d78 3512
b8ffa322
AD
3513 array_push($filters[$line["name"]], $filter);
3514 }
3515
3516 if ($memcache) $memcache->add($obj_id, $filters, 0, 3600*8);
3517
3518 return $filters;
3519 }
fee840fb 3520 }
1e36af0c
AD
3521
3522 function get_score_pic($score) {
8d505d78
AD
3523 if ($score > 100) {
3524 return "score_high.png";
3525 } else if ($score > 0) {
883fee8d 3526 return "score_half_high.png";
1cce3aca 3527 } else if ($score < -100) {
883fee8d 3528 return "score_low.png";
1cce3aca 3529 } else if ($score < 0) {
883fee8d 3530 return "score_half_low.png";
8d505d78 3531 } else {
883fee8d 3532 return "score_neutral.png";
1e36af0c
AD
3533 }
3534 }
ec92c9d1 3535
7defa089
AD
3536 function feed_has_icon($id) {
3537 return is_file(ICONS_DIR . "/$id.ico") && filesize(ICONS_DIR . "/$id.ico") > 0;
3538 }
f29ba148
AD
3539
3540 function init_connection($link) {
5f0a3741 3541 if ($link) {
324944f3 3542
5f0a3741
AD
3543 if (DB_TYPE == "pgsql") {
3544 pg_query($link, "set client_encoding = 'UTF-8'");
3545 pg_set_client_encoding("UNICODE");
3546 pg_query($link, "set datestyle = 'ISO, european'");
3547 pg_query($link, "set TIME ZONE 0");
3548 } else {
3549 db_query($link, "SET time_zone = '+0:0'");
3550
3551 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
3552 db_query($link, "SET NAMES " . MYSQL_CHARSET);
3553 }
f29ba148 3554 }
5f0a3741
AD
3555 return true;
3556 } else {
3557 print "Unable to connect to database:" . db_last_error();
3558 return false;
f29ba148
AD
3559 }
3560 }
5e96ca9d 3561
0bfbf1c2 3562 /* function ccache_zero($link, $feed_id, $owner_uid) {
2627f2d0
AD
3563 db_query($link, "UPDATE ttrss_counters_cache SET
3564 value = 0, updated = NOW() WHERE
3565 feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
0bfbf1c2 3566 } */
2627f2d0 3567
ad0056a8
AD
3568 function ccache_zero_all($link, $owner_uid) {
3569 db_query($link, "UPDATE ttrss_counters_cache SET
3570 value = 0 WHERE owner_uid = '$owner_uid'");
3571
3572 db_query($link, "UPDATE ttrss_cat_counters_cache SET
3573 value = 0 WHERE owner_uid = '$owner_uid'");
3574 }
3575
c7e51de1
AD
3576 function ccache_remove($link, $feed_id, $owner_uid, $is_cat = false) {
3577
3578 if (!$is_cat) {
3579 $table = "ttrss_counters_cache";
3580 } else {
3581 $table = "ttrss_cat_counters_cache";
3582 }
3583
3584 db_query($link, "DELETE FROM $table WHERE
3585 feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
3586
3587 }
3588
5f4f7adf
AD
3589 function ccache_update_all($link, $owner_uid) {
3590
3591 if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid)) {
3592
3593 $result = db_query($link, "SELECT feed_id FROM ttrss_cat_counters_cache
3594 WHERE feed_id > 0 AND owner_uid = '$owner_uid'");
3595
3596 while ($line = db_fetch_assoc($result)) {
3597 ccache_update($link, $line["feed_id"], $owner_uid, true);
3598 }
3599
c5ffeb61
AD
3600 /* We have to manually include category 0 */
3601
3602 ccache_update($link, 0, $owner_uid, true);
3603
8a4c759e 3604 } else {
5f4f7adf
AD
3605 $result = db_query($link, "SELECT feed_id FROM ttrss_counters_cache
3606 WHERE feed_id > 0 AND owner_uid = '$owner_uid'");
8a4c759e 3607
5f4f7adf
AD
3608 while ($line = db_fetch_assoc($result)) {
3609 print ccache_update($link, $line["feed_id"], $owner_uid);
3610
3611 }
3612
3613 }
3614 }
2627f2d0 3615
8d505d78 3616 function ccache_find($link, $feed_id, $owner_uid, $is_cat = false,
6b49a3dd 3617 $no_update = false) {
8a4c759e 3618
5c432ba4
AD
3619 if (!is_numeric($feed_id)) return;
3620
8a4c759e
AD
3621 if (!$is_cat) {
3622 $table = "ttrss_counters_cache";
32d2181b 3623 if ($feed_id > 0) {
8d505d78 3624 $tmp_result = db_query($link, "SELECT owner_uid FROM ttrss_feeds
32d2181b
AD
3625 WHERE id = '$feed_id'");
3626 $owner_uid = db_fetch_result($tmp_result, 0, "owner_uid");
3627 }
8a4c759e
AD
3628 } else {
3629 $table = "ttrss_cat_counters_cache";
3630 }
3631
3632 if (DB_TYPE == "pgsql") {
3633 $date_qpart = "updated > NOW() - INTERVAL '15 minutes'";
3634 } else if (DB_TYPE == "mysql") {
3635 $date_qpart = "updated > DATE_SUB(NOW(), INTERVAL 15 MINUTE)";
3636 }
3637
3638 $result = db_query($link, "SELECT value FROM $table
8d505d78 3639 WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id'
37fb651d 3640 LIMIT 1");
2627f2d0
AD
3641
3642 if (db_num_rows($result) == 1) {
3643 return db_fetch_result($result, 0, "value");
3644 } else {
6b49a3dd
AD
3645 if ($no_update) {
3646 return -1;
3647 } else {
3648 return ccache_update($link, $feed_id, $owner_uid, $is_cat);
3649 }
2627f2d0
AD
3650 }
3651
3652 }
3653
8d505d78 3654 function ccache_update($link, $feed_id, $owner_uid, $is_cat = false,
6b49a3dd
AD
3655 $update_pcat = true) {
3656
5c432ba4
AD
3657 if (!is_numeric($feed_id)) return;
3658
32d2181b 3659 if (!$is_cat && $feed_id > 0) {
8d505d78 3660 $tmp_result = db_query($link, "SELECT owner_uid FROM ttrss_feeds
2e93b64c
AD
3661 WHERE id = '$feed_id'");
3662 $owner_uid = db_fetch_result($tmp_result, 0, "owner_uid");
3663 }
3664
43ead405
AD
3665 $prev_unread = ccache_find($link, $feed_id, $owner_uid, $is_cat, true);
3666
3667 /* When updating a label, all we need to do is recalculate feed counters
3668 * because labels are not cached */
c98e43db
AD
3669
3670 if ($feed_id < 0) {
43ead405
AD
3671 ccache_update_all($link, $owner_uid);
3672 return;
c98e43db
AD
3673 }
3674
8a4c759e
AD
3675 if (!$is_cat) {
3676 $table = "ttrss_counters_cache";
3677 } else {
3678 $table = "ttrss_cat_counters_cache";
3679 }
3680
b6d486a3 3681 if ($is_cat && $feed_id >= 0) {
37fb651d
AD
3682 if ($feed_id != 0) {
3683 $cat_qpart = "cat_id = '$feed_id'";
3684 } else {
3685 $cat_qpart = "cat_id IS NULL";
3686 }
3687
6b49a3dd
AD
3688 /* Recalculate counters for child feeds */
3689
3690 $result = db_query($link, "SELECT id FROM ttrss_feeds
3691 WHERE owner_uid = '$owner_uid' AND $cat_qpart");
3692
3693 while ($line = db_fetch_assoc($result)) {
3694 ccache_update($link, $line["id"], $owner_uid, false, false);
3695 }
3696
8d505d78
AD
3697 $result = db_query($link, "SELECT SUM(value) AS sv
3698 FROM ttrss_counters_cache, ttrss_feeds
3699 WHERE id = feed_id AND $cat_qpart AND
37fb651d
AD
3700 ttrss_feeds.owner_uid = '$owner_uid'");
3701
51e196de 3702 $unread = (int) db_fetch_result($result, 0, "sv");
37fb651d 3703
f55b0b12
AD
3704 } else {
3705 $unread = (int) getFeedArticles($link, $feed_id, $is_cat, true, $owner_uid);
37fb651d 3706 }
2627f2d0 3707
c7e51de1
AD
3708 db_query($link, "BEGIN");
3709
8a4c759e 3710 $result = db_query($link, "SELECT feed_id FROM $table
2627f2d0
AD
3711 WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id' LIMIT 1");
3712
3713 if (db_num_rows($result) == 1) {
8a4c759e 3714 db_query($link, "UPDATE $table SET
2627f2d0
AD
3715 value = '$unread', updated = NOW() WHERE
3716 feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
3717
3718 } else {
8a4c759e 3719 db_query($link, "INSERT INTO $table
8d505d78
AD
3720 (feed_id, value, owner_uid, updated)
3721 VALUES
2627f2d0 3722 ($feed_id, $unread, $owner_uid, NOW())");
2627f2d0
AD
3723 }
3724
c7e51de1
AD
3725 db_query($link, "COMMIT");
3726
6b49a3dd 3727 if ($feed_id > 0 && $prev_unread != $unread) {
8a4c759e 3728
37fb651d 3729 if (!$is_cat) {
8a4c759e 3730
6b49a3dd 3731 /* Update parent category */
8a4c759e 3732
6b49a3dd 3733 if ($update_pcat) {
37fb651d 3734
6b49a3dd
AD
3735 $result = db_query($link, "SELECT cat_id FROM ttrss_feeds
3736 WHERE owner_uid = '$owner_uid' AND id = '$feed_id'");
8a4c759e 3737
6b49a3dd 3738 $cat_id = (int) db_fetch_result($result, 0, "cat_id");
37fb651d 3739
6b49a3dd 3740 ccache_update($link, $cat_id, $owner_uid, true);
6c2a9b9e 3741
6c2a9b9e 3742 }
8a4c759e 3743 }
5f4f7adf
AD
3744 } else if ($feed_id < 0) {
3745 ccache_update_all($link, $owner_uid);
8a4c759e
AD
3746 }
3747
3748 return $unread;
2627f2d0 3749 }
ceb30ba4 3750
0bfbf1c2 3751 /* function ccache_cleanup($link, $owner_uid) {
3261ca58 3752
cd9da663
AD
3753 if (DB_TYPE == "pgsql") {
3754 db_query($link, "DELETE FROM ttrss_counters_cache AS c1 WHERE
3755 (SELECT count(*) FROM ttrss_counters_cache AS c2
3756 WHERE c1.feed_id = c2.feed_id AND c2.owner_uid = c1.owner_uid) > 1
3757 AND owner_uid = '$owner_uid'");
3758
3759 db_query($link, "DELETE FROM ttrss_cat_counters_cache AS c1 WHERE
3760 (SELECT count(*) FROM ttrss_cat_counters_cache AS c2
3761 WHERE c1.feed_id = c2.feed_id AND c2.owner_uid = c1.owner_uid) > 1
3261ca58 3762 AND owner_uid = '$owner_uid'");
cd9da663
AD
3763 } else {
3764 db_query($link, "DELETE c1 FROM
3765 ttrss_counters_cache AS c1,
3766 ttrss_counters_cache AS c2
3767 WHERE
3768 c1.owner_uid = '$owner_uid' AND
3769 c1.owner_uid = c2.owner_uid AND
3770 c1.feed_id = c2.feed_id");
3771
3772 db_query($link, "DELETE c1 FROM
3773 ttrss_cat_counters_cache AS c1,
3774 ttrss_cat_counters_cache AS c2
3775 WHERE
3776 c1.owner_uid = '$owner_uid' AND
3777 c1.owner_uid = c2.owner_uid AND
3778 c1.feed_id = c2.feed_id");
3779
3780 }
0bfbf1c2 3781 } */
3261ca58 3782
ceb30ba4 3783 function label_find_id($link, $label, $owner_uid) {
8d505d78
AD
3784 $result = db_query($link,
3785 "SELECT id FROM ttrss_labels2 WHERE caption = '$label'
ceb30ba4
AD
3786 AND owner_uid = '$owner_uid' LIMIT 1");
3787
3788 if (db_num_rows($result) == 1) {
3789 return db_fetch_result($result, 0, "id");
3790 } else {
3791 return 0;
3792 }
3793 }
3794
814bff66 3795 function get_article_labels($link, $id) {
d721a547
AD
3796 global $memcache;
3797
d721a547
AD
3798 $obj_id = md5("LABELS:$id:" . $_SESSION["uid"]);
3799
814bff66
AD
3800 $rv = array();
3801
d721a547
AD
3802 if ($memcache && $obj = $memcache->get($obj_id)) {
3803 return $obj;
3804 } else {
905ff52a
AD
3805
3806 $result = db_query($link, "SELECT label_cache FROM
3807 ttrss_user_entries WHERE ref_id = '$id' AND owner_uid = " .
3808 $_SESSION["uid"]);
3809
3810 $label_cache = db_fetch_result($result, 0, "label_cache");
3811
3812 if ($label_cache) {
3813
3814 $label_cache = json_decode($label_cache, true);
3815
3816 if ($label_cache["no-labels"] == 1)
3817 return $rv;
3818 else
3819 return $label_cache;
3820 }
3821
8d505d78
AD
3822 $result = db_query($link,
3823 "SELECT DISTINCT label_id,caption,fg_color,bg_color
3824 FROM ttrss_labels2, ttrss_user_labels2
3825 WHERE id = label_id
3826 AND article_id = '$id'
905ff52a
AD
3827 AND owner_uid = ".$_SESSION["uid"] . "
3828 ORDER BY caption");
3829
d721a547
AD
3830 while ($line = db_fetch_assoc($result)) {
3831 $rk = array($line["label_id"], $line["caption"], $line["fg_color"],
3832 $line["bg_color"]);
3833 array_push($rv, $rk);
3834 }
3835 if ($memcache) $memcache->add($obj_id, $rv, 0, 3600);
905ff52a 3836
8d505d78 3837 if (count($rv) > 0)
905ff52a
AD
3838 label_update_cache($link, $id, $rv);
3839 else
3840 label_update_cache($link, $id, array("no-labels" => 1));
814bff66
AD
3841 }
3842
3843 return $rv;
3844 }
3845
3846
b8a637f3 3847 function label_find_caption($link, $label, $owner_uid) {
8d505d78
AD
3848 $result = db_query($link,
3849 "SELECT caption FROM ttrss_labels2 WHERE id = '$label'
b8a637f3
AD
3850 AND owner_uid = '$owner_uid' LIMIT 1");
3851
3852 if (db_num_rows($result) == 1) {
3853 return db_fetch_result($result, 0, "caption");
3854 } else {
3855 return "";
3856 }
3857 }
3858
905ff52a
AD
3859 function label_update_cache($link, $id, $labels = false, $force = false) {
3860
3861 if ($force)
3862 label_clear_cache($link, $id);
3863
8d505d78 3864 if (!$labels)
905ff52a
AD
3865 $labels = get_article_labels($link, $id);
3866
3867 $labels = db_escape_string(json_encode($labels));
3868
3869 db_query($link, "UPDATE ttrss_user_entries SET
3870 label_cache = '$labels' WHERE ref_id = '$id'");
3871
3872 }
3873
3874 function label_clear_cache($link, $id) {
3875
3876 db_query($link, "UPDATE ttrss_user_entries SET
3877 label_cache = '' WHERE ref_id = '$id'");
3878
3879 }
3880
933ba4ee
AD
3881 function label_remove_article($link, $id, $label, $owner_uid) {
3882
3883 $label_id = label_find_id($link, $label, $owner_uid);
3884
3885 if (!$label_id) return;
3886
8d505d78 3887 $result = db_query($link,
933ba4ee 3888 "DELETE FROM ttrss_user_labels2
8d505d78 3889 WHERE
933ba4ee
AD
3890 label_id = '$label_id' AND
3891 article_id = '$id'");
905ff52a
AD
3892
3893 label_clear_cache($link, $id);
933ba4ee
AD
3894 }
3895
ceb30ba4
AD
3896 function label_add_article($link, $id, $label, $owner_uid) {
3897
d721a547
AD
3898 global $memcache;
3899
3900 if ($memcache) {
3901 $obj_id = md5("LABELS:$id:$owner_uid");
3902 $memcache->delete($obj_id);
3903 }
3904
ceb30ba4
AD
3905 $label_id = label_find_id($link, $label, $owner_uid);
3906
3907 if (!$label_id) return;
3908
8d505d78
AD
3909 $result = db_query($link,
3910 "SELECT
ceb30ba4 3911 article_id FROM ttrss_labels2, ttrss_user_labels2
8d505d78
AD
3912 WHERE
3913 label_id = id AND
ceb30ba4
AD
3914 label_id = '$label_id' AND
3915 article_id = '$id' AND owner_uid = '$owner_uid'
3916 LIMIT 1");
3917
3918 if (db_num_rows($result) == 0) {
8d505d78 3919 db_query($link, "INSERT INTO ttrss_user_labels2
ceb30ba4
AD
3920 (label_id, article_id) VALUES ('$label_id', '$id')");
3921 }
905ff52a 3922
8d505d78 3923 label_clear_cache($link, $id);
905ff52a 3924
ceb30ba4 3925 }
1380f8ee
AD
3926
3927 function label_remove($link, $id, $owner_uid) {
d721a547
AD
3928 global $memcache;
3929
905ff52a
AD
3930 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
3931
d721a547
AD
3932 if ($memcache) {
3933 $obj_id = md5("LABELS:$id:$owner_uid");
3934 $memcache->delete($obj_id);
3935 }
1380f8ee
AD
3936
3937 db_query($link, "BEGIN");
3938
3939 $result = db_query($link, "SELECT caption FROM ttrss_labels2
3940 WHERE id = '$id'");
3941
3942 $caption = db_fetch_result($result, 0, "caption");
3943
3944 $result = db_query($link, "DELETE FROM ttrss_labels2 WHERE id = '$id'
905ff52a 3945 AND owner_uid = " . $owner_uid);
1380f8ee
AD
3946
3947 if (db_affected_rows($link, $result) != 0 && $caption) {
3948
8801fb01
AD
3949 /* Remove access key for the label */
3950
3951 $ext_id = -11 - $id;
3952
3953 db_query($link, "DELETE FROM ttrss_access_keys WHERE
3954 feed_id = '$ext_id' AND owner_uid = $owner_uid");
3955
1380f8ee 3956 /* Disable filters that reference label being removed */
8d505d78 3957
1380f8ee
AD
3958 db_query($link, "UPDATE ttrss_filters SET
3959 enabled = false WHERE action_param = '$caption'
3960 AND action_id = 7
905ff52a
AD
3961 AND owner_uid = " . $owner_uid);
3962
3963 /* Remove cached data */
3964
3965 db_query($link, "UPDATE ttrss_user_entries SET label_cache = ''
3966 WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $owner_uid);
3967
3968 }
1380f8ee
AD
3969
3970 db_query($link, "COMMIT");
3971 }
79c88e11 3972
6b2ee18d
AD
3973 function label_create($link, $caption) {
3974
3975 db_query($link, "BEGIN");
3976
3977 $result = false;
3978
3979 $result = db_query($link, "SELECT id FROM ttrss_labels2
3980 WHERE caption = '$caption' AND owner_uid = ". $_SESSION["uid"]);
3981
3982 if (db_num_rows($result) == 0) {
3983 $result = db_query($link,
8d505d78 3984 "INSERT INTO ttrss_labels2 (caption,owner_uid)
6b2ee18d
AD
3985 VALUES ('$caption', '".$_SESSION["uid"]."')");
3986
3987 $result = db_affected_rows($link, $result) != 0;
3988 }
3989
3990 db_query($link, "COMMIT");
3991
3992 return $result;
3993 }
3994
307d187c
AD
3995 function format_tags_string($tags, $id) {
3996
3997 $tags_str = "";
3998 $tags_nolinks_str = "";
3999
4000 $num_tags = 0;
4001
d9084cf2 4002 $tag_limit = 6;
307d187c
AD
4003
4004 $formatted_tags = array();
4005
4006 foreach ($tags as $tag) {
4007 $num_tags++;
4008 $tag_escaped = str_replace("'", "\\'", $tag);
4009
275a0af2
AD
4010 if (mb_strlen($tag) > 30) {
4011 $tag = truncate_string($tag, 30);
4012 }
4013
307d187c
AD
4014 $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>";
4015
4016 array_push($formatted_tags, $tag_str);
275a0af2
AD
4017
4018 $tmp_tags_str = implode(", ", $formatted_tags);
8d505d78 4019
275a0af2 4020 if ($num_tags == $tag_limit || mb_strlen($tmp_tags_str) > 150) {
307d187c
AD
4021 break;
4022 }
4023 }
4024
4025 $tags_str = implode(", ", $formatted_tags);
4026
4027 if ($num_tags < count($tags)) {
4028 $tags_str .= ", &hellip;";
4029 }
4030
4031 if ($num_tags == 0) {
4032 $tags_str = __("no tags");
4033 }
4034
4035 return $tags_str;
4036
4037 }
2eb9c95c
AD
4038
4039 function format_article_labels($labels, $id) {
4040
4041 $labels_str = "";
4042
4043 foreach ($labels as $l) {
8d505d78 4044 $labels_str .= sprintf("<span class='hlLabelRef'
2eb9c95c
AD
4045 style='color : %s; background-color : %s'>%s</span>",
4046 $l[2], $l[3], $l[1]);
4047 }
4048
4049 return $labels_str;
4050
4051 }
c7e51de1
AD
4052
4053 function format_article_note($id, $note) {
4054
fcfa9ef1
AD
4055 $str = "<div class='articleNote' onclick=\"editArticleNote($id)\">
4056 <div class='noteEdit' onclick=\"editArticleNote($id)\">".
4057 __('(edit note)')."</div>$note</div>";
c7e51de1
AD
4058
4059 return $str;
4060 }
7f969260 4061
fcf70c51 4062 function toggle_collapse_cat($link, $cat_id, $mode) {
7f969260 4063 if ($cat_id > 0) {
fcf70c51
AD
4064 $mode = bool_to_sql_bool($mode);
4065
7f969260 4066 db_query($link, "UPDATE ttrss_feed_categories SET
8d505d78 4067 collapsed = $mode WHERE id = '$cat_id' AND owner_uid = " .
7f969260
AD
4068 $_SESSION["uid"]);
4069 } else {
4070 $pref_name = '';
4071
4072 switch ($cat_id) {
4073 case -1:
4074 $pref_name = '_COLLAPSED_SPECIAL';
4075 break;
4076 case -2:
4077 $pref_name = '_COLLAPSED_LABELS';
4078 break;
4079 case 0:
4080 $pref_name = '_COLLAPSED_UNCAT';
4081 break;
4082 }
4083
4084 if ($pref_name) {
fcf70c51 4085 if ($mode) {
7f969260 4086 set_pref($link, $pref_name, 'true');
fcf70c51
AD
4087 } else {
4088 set_pref($link, $pref_name, 'false');
7f969260
AD
4089 }
4090 }
4091 }
4092 }
7e329f13
AD
4093
4094 function remove_feed($link, $id, $owner_uid) {
4095
4096 if ($id > 0) {
e04c18a2
AD
4097
4098 /* save starred articles in Archived feed */
4099
4100 db_query($link, "BEGIN");
4101
8056ec50
AD
4102 /* prepare feed if necessary */
4103
4104 $result = db_query($link, "SELECT id FROM ttrss_archived_feeds
4105 WHERE id = '$id'");
4106
4107 if (db_num_rows($result) == 0) {
8d505d78 4108 db_query($link, "INSERT INTO ttrss_archived_feeds
8056ec50
AD
4109 (id, owner_uid, title, feed_url, site_url)
4110 SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
4111 WHERE id = '$id'");
4112 }
4113
74d22f0c 4114 db_query($link, "UPDATE ttrss_user_entries SET feed_id = NULL,
8d505d78 4115 orig_feed_id = '$id' WHERE feed_id = '$id' AND
e04c18a2
AD
4116 marked = true AND owner_uid = $owner_uid");
4117
8801fb01
AD
4118 /* Remove access key for the feed */
4119
4120 db_query($link, "DELETE FROM ttrss_access_keys WHERE
4121 feed_id = '$id' AND owner_uid = $owner_uid");
4122
e04c18a2
AD
4123 /* remove the feed */
4124
8d505d78 4125 db_query($link, "DELETE FROM ttrss_feeds
7e329f13
AD
4126 WHERE id = '$id' AND owner_uid = $owner_uid");
4127
e04c18a2
AD
4128 db_query($link, "COMMIT");
4129
6d634e00 4130 if (file_exists(ICONS_DIR . "/$id.ico")) {
7e329f13 4131 unlink(ICONS_DIR . "/$id.ico");
6d634e00 4132 }
7e329f13
AD
4133
4134 ccache_remove($link, $id, $owner_uid);
4135
4136 } else {
4137 label_remove($link, -11-$id, $owner_uid);
4138 ccache_remove($link, -11-$id, $owner_uid);
4139 }
4140 }
4141
5c7c7da9 4142 function add_feed_category($link, $feed_cat) {
c00907f2
AD
4143
4144 if (!$feed_cat) return false;
4145
5c7c7da9
AD
4146 db_query($link, "BEGIN");
4147
4148 $result = db_query($link,
4149 "SELECT id FROM ttrss_feed_categories
4150 WHERE title = '$feed_cat' AND owner_uid = ".$_SESSION["uid"]);
4151
4152 if (db_num_rows($result) == 0) {
8d505d78 4153
5c7c7da9 4154 $result = db_query($link,
8d505d78 4155 "INSERT INTO ttrss_feed_categories (owner_uid,title)
5c7c7da9
AD
4156 VALUES ('".$_SESSION["uid"]."', '$feed_cat')");
4157
4158 db_query($link, "COMMIT");
4159
4160 return true;
4161 }
4162
4163 return false;
8d505d78 4164 }
5c7c7da9 4165
7e329f13
AD
4166 function remove_feed_category($link, $id, $owner_uid) {
4167
4168 db_query($link, "DELETE FROM ttrss_feed_categories
4169 WHERE id = '$id' AND owner_uid = $owner_uid");
4170
4171 ccache_remove($link, $id, $owner_uid, true);
4172 }
4173
16fdac16
AD
4174 function archive_article($link, $id, $owner_uid) {
4175 db_query($link, "BEGIN");
4176
4177 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
4178 WHERE ref_id = '$id' AND owner_uid = $owner_uid");
4179
4180 if (db_num_rows($result) != 0) {
4181
4182 /* prepare the archived table */
4183
4184 $feed_id = (int) db_fetch_result($result, 0, "feed_id");
4185
4186 if ($feed_id) {
4187 $result = db_query($link, "SELECT id FROM ttrss_archived_feeds
4188 WHERE id = '$feed_id'");
4189
4190 if (db_num_rows($result) == 0) {
8d505d78 4191 db_query($link, "INSERT INTO ttrss_archived_feeds
16fdac16
AD
4192 (id, owner_uid, title, feed_url, site_url)
4193 SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
4194 WHERE id = '$feed_id'");
4195 }
4196
8d505d78 4197 db_query($link, "UPDATE ttrss_user_entries
16fdac16
AD
4198 SET orig_feed_id = feed_id, feed_id = NULL
4199 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
4200 }
4201 }
4202
4203 db_query($link, "COMMIT");
4204 }
ab197ae1
AD
4205
4206 function getArticleFeed($link, $id) {
8d505d78 4207 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
a545dc31 4208 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
ab197ae1
AD
4209
4210 if (db_num_rows($result) != 0) {
4211 return db_fetch_result($result, 0, "feed_id");
4212 } else {
4213 return 0;
4214 }
4215 }
a5819bb3 4216
f2c6c008
CW
4217 /**
4218 * Fixes incomplete URLs by prepending "http://".
f0266f51
CW
4219 * Also replaces feed:// with http://, and
4220 * prepends a trailing slash if the url is a domain name only.
f2c6c008
CW
4221 *
4222 * @param string $url Possibly incomplete URL
4223 *
4224 * @return string Fixed URL.
4225 */
4226 function fix_url($url) {
4227 if (strpos($url, '://') === false) {
4228 $url = 'http://' . $url;
f0266f51
CW
4229 } else if (substr($url, 0, 5) == 'feed:') {
4230 $url = 'http:' . substr($url, 5);
4231 }
4232
4233 //prepend slash if the URL has no slash in it
4234 // "http://www.example" -> "http://www.example/"
44453773 4235 if (strpos($url, '/', strpos($url, ':') + 3) === false) {
f0266f51 4236 $url .= '/';
f2c6c008 4237 }
ec39a02c
AD
4238
4239 if ($url != "http:///")
4240 return $url;
4241 else
4242 return '';
f2c6c008
CW
4243 }
4244
a5819bb3
AD
4245 function validate_feed_url($url) {
4246 $parts = parse_url($url);
4247
4248 return ($parts['scheme'] == 'http' || $parts['scheme'] == 'feed' || $parts['scheme'] == 'https');
4249
4250 }
d9084cf2 4251
be35798b
AD
4252 function get_article_enclosures($link, $id) {
4253
4254 global $memcache;
4255
8d505d78 4256 $query = "SELECT * FROM ttrss_enclosures
be35798b
AD
4257 WHERE post_id = '$id' AND content_url != ''";
4258
bd3f2ade 4259 $obj_id = md5("ENCLOSURES:$id");
be35798b
AD
4260
4261 $rv = array();
4262
bd3f2ade 4263 if ($memcache && $obj = $memcache->get($obj_id)) {
be35798b
AD
4264 $rv = $obj;
4265 } else {
4266 $result = db_query($link, $query);
4267
4268 if (db_num_rows($result) > 0) {
4269 while ($line = db_fetch_assoc($result)) {
4270 array_push($rv, $line);
4271 }
bd3f2ade 4272 if ($memcache) $memcache->add($obj_id, $rv, 0, 3600);
be35798b
AD
4273 }
4274 }
4275
4276 return $rv;
4277 }
4278
911d4c08 4279 function api_get_feeds($link, $cat_id, $unread_only, $limit, $offset) {
911d4c08
AD
4280
4281 $feeds = array();
4282
911d4c08
AD
4283 /* Labels */
4284
fb8d17f3 4285 if ($cat_id == -4 || $cat_id == -2) {
911d4c08
AD
4286 $counters = getLabelCounters($link, true);
4287
11232703 4288 foreach (array_values($counters) as $cv) {
911d4c08 4289
11232703 4290 $unread = $cv["counter"];
8d505d78 4291
911d4c08 4292 if ($unread || !$unread_only) {
8d505d78 4293
911d4c08 4294 $row = array(
11232703
AD
4295 "id" => $cv["id"],
4296 "title" => $cv["description"],
4297 "unread" => $cv["counter"],
911d4c08
AD
4298 "cat_id" => -2,
4299 );
8d505d78 4300
911d4c08
AD
4301 array_push($feeds, $row);
4302 }
4303 }
4304 }
4305
4306 /* Virtual feeds */
4307
fb8d17f3 4308 if ($cat_id == -4 || $cat_id == -1) {
911d4c08
AD
4309 foreach (array(-1, -2, -3, -4, 0) as $i) {
4310 $unread = getFeedUnread($link, $i);
4311
4312 if ($unread || !$unread_only) {
4313 $title = getFeedTitle($link, $i);
4314
4315 $row = array(
4316 "id" => $i,
4317 "title" => $title,
4318 "unread" => $unread,
4319 "cat_id" => -1,
4320 );
4321 array_push($feeds, $row);
4322 }
4323
8d505d78 4324 }
911d4c08 4325 }
b41c2549
AD
4326
4327 /* Real feeds */
4328
4329 if ($limit) {
4330 $limit_qpart = "LIMIT $limit OFFSET $offset";
4331 } else {
4332 $limit_qpart = "";
4333 }
4334
fb8d17f3 4335 if ($cat_id == -4 || $cat_id == -3) {
8d505d78 4336 $result = db_query($link, "SELECT
b41c2549
AD
4337 id, feed_url, cat_id, title, ".
4338 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
8d505d78 4339 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] .
b41c2549
AD
4340 " ORDER BY cat_id, title " . $limit_qpart);
4341 } else {
fb8d17f3
AD
4342
4343 if ($cat_id)
4344 $cat_qpart = "cat_id = '$cat_id'";
4345 else
4346 $cat_qpart = "cat_id IS NULL";
4347
8d505d78 4348 $result = db_query($link, "SELECT
b41c2549
AD
4349 id, feed_url, cat_id, title, ".
4350 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
8d505d78
AD
4351 FROM ttrss_feeds WHERE
4352 $cat_qpart AND owner_uid = " . $_SESSION["uid"] .
b41c2549
AD
4353 " ORDER BY cat_id, title " . $limit_qpart);
4354 }
4355
4356 while ($line = db_fetch_assoc($result)) {
4357
4358 $unread = getFeedUnread($link, $line["id"]);
4359
4360 $has_icon = feed_has_icon($line['id']);
4361
4362 if ($unread || !$unread_only) {
4363
4364 $row = array(
4365 "feed_url" => $line["feed_url"],
4366 "title" => $line["title"],
4367 "id" => (int)$line["id"],
4368 "unread" => (int)$unread,
4369 "has_icon" => $has_icon,
4370 "cat_id" => (int)$line["cat_id"],
4371 "last_updated" => strtotime($line["last_updated"])
4372 );
8d505d78 4373
b41c2549
AD
4374 array_push($feeds, $row);
4375 }
4376 }
4377
911d4c08
AD
4378 return $feeds;
4379 }
4380
4381 function api_get_headlines($link, $feed_id, $limit, $offset,
a0e580b0 4382 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order,
97e5dbb2 4383 $include_attachments, $since_id) {
911d4c08
AD
4384
4385 /* do not rely on params below */
4386
4387 $search = db_escape_string($_REQUEST["search"]);
4388 $search_mode = db_escape_string($_REQUEST["search_mode"]);
4389 $match_on = db_escape_string($_REQUEST["match_on"]);
8d505d78
AD
4390
4391 $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
911d4c08 4392 $view_mode, $is_cat, $search, $search_mode, $match_on,
97e5dbb2 4393 $order, $offset, 0, false, $since_id);
911d4c08
AD
4394
4395 $result = $qfh_ret[0];
4396 $feed_title = $qfh_ret[1];
4397
4398 $headlines = array();
4399
4400 while ($line = db_fetch_assoc($result)) {
8d505d78 4401 $is_updated = ($line["last_read"] == "" &&
911d4c08
AD
4402 ($line["unread"] != "t" && $line["unread"] != "1"));
4403
97a3b9f0
AD
4404 $tags = explode(",", $line["tag_cache"]);
4405 $labels = json_decode($line["label_cache"], true);
4406
4407 //if (!$tags) $tags = get_article_tags($link, $line["id"]);
4408 //if (!$labels) $labels = get_article_labels($link, $line["id"]);
4409
911d4c08
AD
4410 $headline_row = array(
4411 "id" => (int)$line["id"],
4412 "unread" => sql_bool_to_bool($line["unread"]),
4413 "marked" => sql_bool_to_bool($line["marked"]),
9ed133e7 4414 "published" => sql_bool_to_bool($line["published"]),
911d4c08
AD
4415 "updated" => strtotime($line["updated"]),
4416 "is_updated" => $is_updated,
4417 "title" => $line["title"],
4418 "link" => $line["link"],
4419 "feed_id" => $line["feed_id"],
97a3b9f0 4420 "tags" => $tags,
911d4c08
AD
4421 );
4422
a0e580b0
AD
4423 if ($include_attachments)
4424 $headline_row['attachments'] = get_article_enclosures($link,
4425 $line['id']);
4426
911d4c08
AD
4427 if ($show_excerpt) {
4428 $excerpt = truncate_string(strip_tags($line["content_preview"]), 100);
4429 $headline_row["excerpt"] = $excerpt;
4430 }
4431
4432 if ($show_content) {
4433 $headline_row["content"] = $line["content_preview"];
4434 }
4435
97a3b9f0
AD
4436 // unify label output to ease parsing
4437 if ($labels["no-labels"] == 1) $labels = array();
4438
4439 $headline_row["labels"] = $labels;
4440
911d4c08
AD
4441 array_push($headlines, $headline_row);
4442 }
4443
4444 return $headlines;
4445 }
4446
bd202c3f
AD
4447 function generate_error_feed($link, $error) {
4448 $reply = array();
4449
4450 $reply['headlines']['id'] = -6;
4451 $reply['headlines']['is_cat'] = false;
4452
4453 $reply['headlines']['toolbar'] = '';
4454 $reply['headlines']['content'] = "<div class='whiteBox'>". $error . "</div>";
4455
4456 $reply['headlines-info'] = array("count" => 0,
4457 "vgroup_last_feed" => '',
4458 "unread" => 0,
4459 "disable_cache" => true);
4460
4461 return $reply;
4462 }
fe1087fb 4463
13e785e0 4464
bd202c3f
AD
4465 function generate_dashboard_feed($link) {
4466 $reply = array();
4467
4468 $reply['headlines']['id'] = -5;
4469 $reply['headlines']['is_cat'] = false;
fe1087fb 4470
bd202c3f
AD
4471 $reply['headlines']['toolbar'] = '';
4472 $reply['headlines']['content'] = "<div class='whiteBox'>".__('No feed selected.');
fe1087fb 4473
bd202c3f 4474 $reply['headlines']['content'] .= "<p class=\"small\"><span class=\"insensitive\">";
5d128c95
AD
4475
4476 $result = db_query($link, "SELECT ".SUBSTRING_FOR_DATE."(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds
4477 WHERE owner_uid = " . $_SESSION['uid']);
4478
4479 $last_updated = db_fetch_result($result, 0, "last_updated");
324944f3 4480 $last_updated = make_local_datetime($link, $last_updated, false);
5d128c95 4481
bd202c3f 4482 $reply['headlines']['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);
5d128c95 4483
fe1087fb
AD
4484 $result = db_query($link, "SELECT COUNT(id) AS num_errors
4485 FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
4486
4487 $num_errors = db_fetch_result($result, 0, "num_errors");
4488
4489 if ($num_errors > 0) {
bd202c3f
AD
4490 $reply['headlines']['content'] .= "<br/>";
4491 $reply['headlines']['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".
fe1087fb
AD
4492 __('Some feeds have update errors (click for details)')."</a>";
4493 }
bd202c3f 4494 $reply['headlines']['content'] .= "</span></p>";
fe1087fb 4495
bd202c3f 4496 $reply['headlines-info'] = array("count" => 0,
ffbe082d
AD
4497 "vgroup_last_feed" => '',
4498 "unread" => 0,
4499 "disable_cache" => true);
4500
bd202c3f 4501 return $reply;
fe1087fb 4502 }
31a53903
AD
4503
4504 function save_email_address($link, $email) {
4505 // FIXME: implement persistent storage of emails
4506
8d505d78 4507 if (!$_SESSION['stored_emails'])
31a53903
AD
4508 $_SESSION['stored_emails'] = array();
4509
4510 if (!in_array($email, $_SESSION['stored_emails']))
4511 array_push($_SESSION['stored_emails'], $email);
4512 }
8801fb01
AD
4513
4514 function update_feed_access_key($link, $feed_id, $is_cat, $owner_uid = false) {
4515 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
4516
4517 $sql_is_cat = bool_to_sql_bool($is_cat);
4518
8d505d78
AD
4519 $result = db_query($link, "SELECT access_key FROM ttrss_access_keys
4520 WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat
8801fb01
AD
4521 AND owner_uid = " . $owner_uid);
4522
4523 if (db_num_rows($result) == 1) {
4524 $key = db_escape_string(sha1(uniqid(rand(), true)));
4525
4526 db_query($link, "UPDATE ttrss_access_keys SET access_key = '$key'
8d505d78 4527 WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat
8801fb01
AD
4528 AND owner_uid = " . $owner_uid);
4529
4530 return $key;
4531
4532 } else {
4533 return get_feed_access_key($link, $feed_id, $is_cat, $owner_uid);
4534 }
4535 }
4536
4537 function get_feed_access_key($link, $feed_id, $is_cat, $owner_uid = false) {
4538
4539 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
4540
4541 $sql_is_cat = bool_to_sql_bool($is_cat);
4542
8d505d78
AD
4543 $result = db_query($link, "SELECT access_key FROM ttrss_access_keys
4544 WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat
8801fb01
AD
4545 AND owner_uid = " . $owner_uid);
4546
4547 if (db_num_rows($result) == 1) {
4548 return db_fetch_result($result, 0, "access_key");
4549 } else {
4550 $key = db_escape_string(sha1(uniqid(rand(), true)));
4551
8d505d78 4552 $result = db_query($link, "INSERT INTO ttrss_access_keys
8801fb01
AD
4553 (access_key, feed_id, is_cat, owner_uid)
4554 VALUES ('$key', '$feed_id', $sql_is_cat, '$owner_uid')");
4555
4556 return $key;
4557 }
4558 return false;
4559 }
f0266f51
CW
4560
4561 /**
4562 * Extracts RSS/Atom feed URLs from the given HTML URL.
4563 *
4564 * @param string $url HTML page URL
4565 *
4566 * @return array Array of feeds. Key is the full URL, value the title
4567 */
8d505d78 4568 function get_feeds_from_html($url, $login = false, $pass = false)
f0266f51
CW
4569 {
4570 $url = fix_url($url);
4571 $baseUrl = substr($url, 0, strrpos($url, '/') + 1);
4572
fb074239
AD
4573 libxml_use_internal_errors(true);
4574
8d505d78
AD
4575 $content = @fetch_file_contents($url, false, $login, $pass);
4576
f0266f51 4577 $doc = new DOMDocument();
8d505d78 4578 $doc->loadHTML($content);
f0266f51
CW
4579 $xpath = new DOMXPath($doc);
4580 $entries = $xpath->query('/html/head/link[@rel="alternate"]');
4581 $feedUrls = array();
4582 foreach ($entries as $entry) {
4583 if ($entry->hasAttribute('href')) {
4584 $title = $entry->getAttribute('title');
4585 if ($title == '') {
4586 $title = $entry->getAttribute('type');
4587 }
923818fc
CW
4588 $feedUrl = rewrite_relative_url(
4589 $baseUrl, $entry->getAttribute('href')
4590 );
f0266f51
CW
4591 $feedUrls[$feedUrl] = $title;
4592 }
4593 }
4594 return $feedUrls;
4595 }
4596
f33479da
CW
4597 /**
4598 * Checks if the content behind the given URL is a HTML file
4599 *
4600 * @param string $url URL to check
4601 *
4602 * @return boolean True if the URL contains HTML content
4603 */
8d505d78
AD
4604 function url_is_html($url, $login = false, $pass = false) {
4605 $content = substr(fetch_file_contents($url, false, $login, $pass), 0, 1000);
4606
24eb4c78
CW
4607 if (stripos($content, '<html>') === false
4608 && stripos($content, '<html ') === false
f33479da
CW
4609 ) {
4610 return false;
4611 }
4612
4613 return true;
4614 }
24e2bb3a 4615
d90868d7 4616 function print_label_select($link, $name, $value, $attributes = "") {
24e2bb3a
AD
4617
4618 $result = db_query($link, "SELECT caption FROM ttrss_labels2
4619 WHERE owner_uid = '".$_SESSION["uid"]."' ORDER BY caption");
4620
8d505d78 4621 print "<select default=\"$value\" name=\"" . htmlspecialchars($name) .
d90868d7 4622 "\" $attributes onchange=\"labelSelectOnChange(this)\" >";
24e2bb3a
AD
4623
4624 while ($line = db_fetch_assoc($result)) {
4625
4626 $issel = ($line["caption"] == $value) ? "selected=\"1\"" : "";
4627
d90868d7
AD
4628 print "<option value=\"".htmlspecialchars($line["caption"])."\"
4629 $issel>" . htmlspecialchars($line["caption"]) . "</option>";
24e2bb3a
AD
4630
4631 }
4632
d90868d7 4633# print "<option value=\"ADD_LABEL\">" .__("Add label...") . "</option>";
24e2bb3a
AD
4634
4635 print "</select>";
4636
4637
4638 }
4639
009646d2 4640 function format_article_enclosures($link, $id, $always_display_enclosures,
dad14b51
AD
4641 $article_content) {
4642
4643 $result = get_article_enclosures($link, $id);
009646d2 4644 $rv = '';
8d505d78 4645
dad14b51 4646 if (count($result) > 0) {
8d505d78 4647
dad14b51
AD
4648 $entries_html = array();
4649 $entries = array();
8d505d78 4650
dad14b51 4651 foreach ($result as $line) {
8d505d78 4652
dad14b51
AD
4653 $url = $line["content_url"];
4654 $ctype = $line["content_type"];
8d505d78 4655
dad14b51 4656 if (!$ctype) $ctype = __("unknown type");
8d505d78 4657
c3edc667 4658# $filename = substr($url, strrpos($url, "/")+1);
8d505d78 4659
dad14b51 4660 $entry = format_inline_player($link, $url, $ctype);
8d505d78 4661
c3edc667
AD
4662# $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
4663# $filename . " (" . $ctype . ")" . "</a>";
8d505d78 4664
dad14b51 4665 array_push($entries_html, $entry);
8d505d78 4666
dad14b51 4667 $entry = array();
8d505d78 4668
dad14b51
AD
4669 $entry["type"] = $ctype;
4670 $entry["filename"] = $filename;
4671 $entry["url"] = $url;
8d505d78 4672
dad14b51
AD
4673 array_push($entries, $entry);
4674 }
8d505d78 4675
009646d2 4676 $rv .= "<div class=\"postEnclosures\">";
8d505d78 4677
dad14b51
AD
4678 if (!get_pref($link, "STRIP_IMAGES")) {
4679 if ($always_display_enclosures ||
4680 !preg_match("/<img/i", $article_content)) {
8d505d78 4681
dad14b51 4682 foreach ($entries as $entry) {
8d505d78 4683
dad14b51
AD
4684 if (preg_match("/image/", $entry["type"]) ||
4685 preg_match("/\.(jpg|png|gif|bmp)/i", $entry["filename"])) {
8d505d78 4686
009646d2 4687 $rv .= "<p><img
dad14b51
AD
4688 alt=\"".htmlspecialchars($entry["filename"])."\"
4689 src=\"" .htmlspecialchars($entry["url"]) . "\"/></p>";
4690 }
4691 }
4692 }
4693 }
8d505d78 4694
905ff52a 4695 if (count($entries) == 1) {
009646d2 4696 $rv .= __("Attachment:") . " ";
dad14b51 4697 } else {
009646d2 4698 $rv .= __("Attachments:") . " ";
dad14b51 4699 }
8d505d78 4700
009646d2 4701 $rv .= join(", ", $entries_html);
8d505d78 4702
009646d2 4703 $rv .= "</div>";
dad14b51 4704 }
009646d2
AD
4705
4706 return $rv;
dad14b51
AD
4707 }
4708
f8fb4498
AD
4709 function getLastArticleId($link) {
4710 $result = db_query($link, "SELECT MAX(ref_id) AS id FROM ttrss_user_entries
4711 WHERE owner_uid = " . $_SESSION["uid"]);
4712
4713 if (db_num_rows($result) == 1) {
4714 return db_fetch_result($result, 0, "id");
4715 } else {
4716 return -1;
4717 }
4718 }
8cc3c778
AD
4719
4720 function build_url($parts) {
4721 return $parts['scheme'] . "://" . $parts['host'] . $parts['path'];
4722 }
4723
f679105c
CW
4724 /**
4725 * Converts a (possibly) relative URL to a absolute one.
4726 *
4727 * @param string $url Base URL (i.e. from where the document is)
4728 * @param string $rel_url Possibly relative URL in the document
4729 *
4730 * @return string Absolute URL
4731 */
8cc3c778
AD
4732 function rewrite_relative_url($url, $rel_url) {
4733 if (strpos($rel_url, "://") !== false) {
4734 return $rel_url;
8d505d78 4735 } else if (strpos($rel_url, "/") === 0)
8cc3c778
AD
4736 {
4737 $parts = parse_url($url);
4738 $parts['path'] = $rel_url;
4739
4740 return build_url($parts);
4741
4742 } else {
4743 $parts = parse_url($url);
f679105c
CW
4744 if (!isset($parts['path'])) {
4745 $parts['path'] = '/';
4746 }
4747 $dir = $parts['path'];
4748 if (substr($dir, -1) !== '/') {
4749 $dir = dirname($parts['path']);
4750 $dir !== '/' && $dir .= '/';
4751 }
4752 $parts['path'] = $dir . $rel_url;
8cc3c778
AD
4753
4754 return build_url($parts);
4755 }
4756 }
4757
e4f7f8df 4758 function sphinx_search($query, $offset = 0, $limit = 30) {
31303c6b
AD
4759 require_once 'lib/sphinxapi.php';
4760
e4f7f8df
AD
4761 $sphinxClient = new SphinxClient();
4762
4763 $sphinxClient->SetServer('localhost', 9312);
4764 $sphinxClient->SetConnectTimeout(1);
4765
8d505d78 4766 $sphinxClient->SetFieldWeights(array('title' => 70, 'content' => 30,
e4f7f8df
AD
4767 'feed_title' => 20));
4768
4769 $sphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2);
4770 $sphinxClient->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
4771 $sphinxClient->SetLimits($offset, $limit, 1000);
4772 $sphinxClient->SetArrayResult(false);
4773 $sphinxClient->SetFilter('owner_uid', array($_SESSION['uid']));
8d505d78 4774
e4f7f8df
AD
4775 $result = $sphinxClient->Query($query, SPHINX_INDEX);
4776
4777 $ids = array();
4778
4779 if (is_array($result['matches'])) {
4780 foreach (array_keys($result['matches']) as $int_id) {
4781 $ref_id = $result['matches'][$int_id]['attrs']['ref_id'];
4782 array_push($ids, $ref_id);
4783 }
4784 }
4785
4786 return $ids;
4787 }
4788
868650e4
AD
4789 function cleanup_tags($link, $days = 14, $limit = 1000) {
4790
4791 if (DB_TYPE == "pgsql") {
4792 $interval_query = "date_updated < NOW() - INTERVAL '$days days'";
4793 } else if (DB_TYPE == "mysql") {
4794 $interval_query = "date_updated < DATE_SUB(NOW(), INTERVAL $days DAY)";
4795 }
4796
b5ec13fa 4797 $tags_deleted = 0;
868650e4 4798
b5ec13fa
AD
4799 while ($limit > 0) {
4800 $limit_part = 500;
4801
8d505d78
AD
4802 $query = "SELECT ttrss_tags.id AS id
4803 FROM ttrss_tags, ttrss_user_entries, ttrss_entries
b5ec13fa
AD
4804 WHERE post_int_id = int_id AND $interval_query AND
4805 ref_id = ttrss_entries.id AND tag_cache != '' LIMIT $limit_part";
8d505d78 4806
b5ec13fa
AD
4807 $result = db_query($link, $query);
4808
4809 $ids = array();
4810
4811 while ($line = db_fetch_assoc($result)) {
4812 array_push($ids, $line['id']);
4813 }
4814
4815 if (count($ids) > 0) {
4816 $ids = join(",", $ids);
4817 print ".";
4818
4819 $tmp_result = db_query($link, "DELETE FROM ttrss_tags WHERE id IN ($ids)");
4820 $tags_deleted += db_affected_rows($link, $tmp_result);
4821 } else {
4822 break;
4823 }
4824
4825 $limit -= $limit_part;
4826 }
4827
4828 print "\n";
868650e4 4829
b5ec13fa 4830 return $tags_deleted;
868650e4
AD
4831 }
4832
88e4e597
AD
4833 function print_user_stylesheet($link) {
4834 $value = get_pref($link, 'USER_STYLESHEET');
4835
4836 if ($value) {
4837 print "<style type=\"text/css\">";
5823f9fb 4838 print str_replace("<br/>", "\n", $value);
88e4e597
AD
4839 print "</style>";
4840 }
4841
4842 }
4843
533c0ea6
AD
4844 function rewrite_urls($line) {
4845 global $url_regex;
4846
4847 $urls = null;
4848
8d505d78 4849 $result = preg_replace("/((?<!=.)((http|https|ftp)+):\/\/[^ ,!]+)/i",
533c0ea6
AD
4850 "<a target=\"_blank\" href=\"\\1\">\\1</a>", $line);
4851
4852 return $result;
4853 }
4854
36184020
AD
4855 function filter_to_sql($filter) {
4856 $query = "";
4857
4858 if (DB_TYPE == "pgsql")
4859 $reg_qpart = "~";
4860 else
4861 $reg_qpart = "REGEXP";
4862
4863 switch ($filter["type"]) {
4864 case "title":
4865 $query = "LOWER(ttrss_entries.title) $reg_qpart LOWER('".
4866 $filter['reg_exp'] . "')";
4867 break;
4868 case "content":
4869 $query = "LOWER(ttrss_entries.content) $reg_qpart LOWER('".
4870 $filter['reg_exp'] . "')";
4871 break;
4872 case "both":
4873 $query = "LOWER(ttrss_entries.title) $reg_qpart LOWER('".
4874 $filter['reg_exp'] . "') OR LOWER(" .
4875 "ttrss_entries.content) $reg_qpart LOWER('" . $filter['reg_exp'] . "')";
4876 break;
4877 case "tag":
4878 $query = "LOWER(ttrss_user_entries.tag_cache) $reg_qpart LOWER('".
4879 $filter['reg_exp'] . "')";
4880 break;
4881 case "link":
4882 $query = "LOWER(ttrss_entries.link) $reg_qpart LOWER('".
4883 $filter['reg_exp'] . "')";
4884 break;
4885 case "date":
4886
4887 if ($filter["filter_param"] == "before")
4888 $cmp_qpart = "<";
4889 else
4890 $cmp_qpart = ">=";
4891
4892 $timestamp = date("Y-m-d H:N:s", strtotime($filter["reg_exp"]));
4893 $query = "ttrss_entries.date_entered $cmp_qpart '$timestamp'";
4894 break;
4895 case "author":
4896 $query = "LOWER(ttrss_entries.author) $reg_qpart LOWER('".
4897 $filter['reg_exp'] . "')";
4898 break;
4899 }
4900
4901 if ($filter["inverse"])
4902 $query = "NOT ($query)";
4903
6338b16f
AD
4904 if ($query) {
4905 if (DB_TYPE == "pgsql") {
6b8b3af8 4906 $query = " ($query) AND ttrss_entries.date_entered > NOW() - INTERVAL '14 days'";
6338b16f 4907 } else {
decf742e 4908 $query = " ($query) AND ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL 14 DAY)";
6338b16f 4909 }
36184020 4910 $query .= " AND ";
6338b16f 4911 }
36184020 4912
6b8b3af8 4913
36184020
AD
4914 return $query;
4915 }
ae5f7bb1
AD
4916
4917 // Status codes:
4918 // -1 - never connected
4919 // 0 - no data received
4920 // 1 - data received successfully
4921 // 2 - did not receive valid data
4922 // >10 - server error, code + 10 (e.g. 16 means server error 6)
4923
4924 function get_linked_feeds($link, $instance_id = false) {
4925 if ($instance_id)
4926 $instance_qpart = "id = '$instance_id' AND ";
4927 else
4928 $instance_qpart = "";
4929
4930 if (DB_TYPE == "pgsql") {
414d0d1f 4931 $date_qpart = "last_connected < NOW() - INTERVAL '6 hours'";
ae5f7bb1 4932 } else {
81731791 4933 $date_qpart = "last_connected < DATE_SUB(NOW(), INTERVAL 6 HOUR)";
ae5f7bb1
AD
4934 }
4935
4936 $result = db_query($link, "SELECT id, access_key, access_url FROM ttrss_linked_instances
4937 WHERE $instance_qpart $date_qpart ORDER BY last_connected");
4938
4939 while ($line = db_fetch_assoc($result)) {
4940 $id = $line['id'];
4941
4942 _debug("Updating: " . $line['access_url'] . " ($id)");
4943
e0d91d84 4944 $fetch_url = $line['access_url'] . '/public.php?op=fbexport';
ae5f7bb1
AD
4945 $post_query = 'key=' . $line['access_key'];
4946
4947 $feeds = fetch_file_contents($fetch_url, false, false, false, $post_query);
4948
e0d91d84
AD
4949 // try doing it the old way
4950 if (!$feeds) {
4951 $fetch_url = $line['access_url'] . '/backend.php?op=fbexport';
4952 $feeds = fetch_file_contents($fetch_url, false, false, false, $post_query);
4953 }
4954
ae5f7bb1
AD
4955 if ($feeds) {
4956 $feeds = json_decode($feeds, true);
4957
4958 if ($feeds) {
4959 if ($feeds['error']) {
4960 $status = $feeds['error']['code'] + 10;
4961 } else {
4962 $status = 1;
4963
4964 if (count($feeds['feeds']) > 0) {
4965
4966 db_query($link, "DELETE FROM ttrss_linked_feeds
4967 WHERE instance_id = '$id'");
4968
4969 foreach ($feeds['feeds'] as $feed) {
4970 $feed_url = db_escape_string($feed['feed_url']);
4971 $title = db_escape_string($feed['title']);
4972 $subscribers = db_escape_string($feed['subscribers']);
2b9c4bae 4973 $site_url = db_escape_string($feed['site_url']);
ae5f7bb1
AD
4974
4975 db_query($link, "INSERT INTO ttrss_linked_feeds
d61063c7 4976 (feed_url, site_url, title, subscribers, instance_id, created, updated)
ae5f7bb1 4977 VALUES
d61063c7 4978 ('$feed_url', '$site_url', '$title', '$subscribers', '$id', NOW(), NOW())");
ae5f7bb1
AD
4979 }
4980 } else {
4981 // received 0 feeds, this might indicate that
4982 // the instance on the other hand is rebuilding feedbrowser cache
4983 // we will try again later
4984
4985 // TODO: maybe perform expiration based on updated here?
4986 }
4987
4988 _debug("Processed " . count($feeds['feeds']) . " feeds.");
4989 }
4990 } else {
4991 $status = 2;
4992 }
4993
4994 } else {
4995 $status = 0;
4996 }
4997
4998 _debug("Status: $status");
4999
5000 db_query($link, "UPDATE ttrss_linked_instances SET
5001 last_status_out = '$status', last_connected = NOW() WHERE id = '$id'");
5002
5003 }
e0d91d84
AD
5004 }
5005
afcfe6ca 5006 function make_feed_browser($link, $search, $limit, $mode = 1) {
5f0a3741 5007
afcfe6ca
AD
5008 $owner_uid = $_SESSION["uid"];
5009 $rv = '';
5f0a3741 5010
afcfe6ca
AD
5011 if ($search) {
5012 $search_qpart = "AND (UPPER(feed_url) LIKE UPPER('%$search%') OR
5013 UPPER(title) LIKE UPPER('%$search%'))";
5014 } else {
5015 $search_qpart = "";
5016 }
5f0a3741 5017
afcfe6ca
AD
5018 if ($mode == 1) {
5019 /* $result = db_query($link, "SELECT feed_url, subscribers FROM
5020 ttrss_feedbrowser_cache WHERE (SELECT COUNT(id) = 0 FROM ttrss_feeds AS tf
5021 WHERE tf.feed_url = ttrss_feedbrowser_cache.feed_url
5022 AND owner_uid = '$owner_uid') $search_qpart
5023 ORDER BY subscribers DESC LIMIT $limit"); */
5f0a3741 5024
afcfe6ca
AD
5025 $result = db_query($link, "SELECT feed_url, site_url, title, SUM(subscribers) AS subscribers FROM
5026 (SELECT feed_url, site_url, title, subscribers FROM ttrss_feedbrowser_cache UNION ALL
5027 SELECT feed_url, site_url, title, subscribers FROM ttrss_linked_feeds) AS qqq
5028 WHERE
5029 (SELECT COUNT(id) = 0 FROM ttrss_feeds AS tf
5030 WHERE tf.feed_url = qqq.feed_url
5031 AND owner_uid = '$owner_uid') $search_qpart
5032 GROUP BY feed_url, site_url, title ORDER BY subscribers DESC LIMIT $limit");
5f0a3741 5033
afcfe6ca
AD
5034 } else if ($mode == 2) {
5035 $result = db_query($link, "SELECT *,
5036 (SELECT COUNT(*) FROM ttrss_user_entries WHERE
5037 orig_feed_id = ttrss_archived_feeds.id) AS articles_archived
5038 FROM
5039 ttrss_archived_feeds
5040 WHERE
5041 (SELECT COUNT(*) FROM ttrss_feeds
5042 WHERE ttrss_feeds.feed_url = ttrss_archived_feeds.feed_url AND
5043 owner_uid = '$owner_uid') = 0 AND
5044 owner_uid = '$owner_uid' $search_qpart
5045 ORDER BY id DESC LIMIT $limit");
5046 }
5f0a3741 5047
afcfe6ca 5048 $feedctr = 0;
5f0a3741 5049
afcfe6ca 5050 while ($line = db_fetch_assoc($result)) {
5f0a3741 5051
afcfe6ca 5052 if ($mode == 1) {
5f0a3741 5053
afcfe6ca
AD
5054 $feed_url = htmlspecialchars($line["feed_url"]);
5055 $site_url = htmlspecialchars($line["site_url"]);
5056 $subscribers = $line["subscribers"];
5f0a3741 5057
afcfe6ca
AD
5058 $check_box = "<input onclick='toggleSelectListRow2(this)'
5059 dojoType=\"dijit.form.CheckBox\"
5060 type=\"checkbox\" \">";
5f0a3741 5061
afcfe6ca 5062 $class = ($feedctr % 2) ? "even" : "odd";
5f0a3741 5063
afcfe6ca
AD
5064 $site_url = "<a target=\"_blank\"
5065 href=\"$site_url\">
5066 <span class=\"fb_feedTitle\">".
5067 htmlspecialchars($line["title"])."</span></a>";
5f0a3741 5068
afcfe6ca
AD
5069 $feed_url = "<a target=\"_blank\" class=\"fb_feedUrl\"
5070 href=\"$feed_url\"><img src='images/feed-icon-12x12.png'
5071 style='vertical-align : middle'></a>";
5f0a3741 5072
afcfe6ca
AD
5073 $rv .= "<li>$check_box $feed_url $site_url".
5074 "&nbsp;<span class='subscribers'>($subscribers)</span></li>";
5f0a3741 5075
afcfe6ca
AD
5076 } else if ($mode == 2) {
5077 $feed_url = htmlspecialchars($line["feed_url"]);
5078 $site_url = htmlspecialchars($line["site_url"]);
5079 $title = htmlspecialchars($line["title"]);
5f0a3741 5080
afcfe6ca
AD
5081 $check_box = "<input onclick='toggleSelectListRow2(this)' dojoType=\"dijit.form.CheckBox\"
5082 type=\"checkbox\">";
5f0a3741 5083
afcfe6ca 5084 $class = ($feedctr % 2) ? "even" : "odd";
5f0a3741 5085
afcfe6ca
AD
5086 if ($line['articles_archived'] > 0) {
5087 $archived = sprintf(__("%d archived articles"), $line['articles_archived']);
5088 $archived = "&nbsp;<span class='subscribers'>($archived)</span>";
5089 } else {
5090 $archived = '';
5091 }
5f0a3741 5092
afcfe6ca
AD
5093 $site_url = "<a target=\"_blank\"
5094 href=\"$site_url\">
5095 <span class=\"fb_feedTitle\">".
5096 htmlspecialchars($line["title"])."</span></a>";
5f0a3741 5097
afcfe6ca
AD
5098 $feed_url = "<a target=\"_blank\" class=\"fb_feedUrl\"
5099 href=\"$feed_url\"><img src='images/feed-icon-12x12.png'
5100 style='vertical-align : middle'></a>";
5f0a3741
AD
5101
5102
afcfe6ca
AD
5103 $rv .= "<li id=\"FBROW-".$line["id"]."\">".
5104 "$check_box $feed_url $site_url $archived</li>";
5105 }
5f0a3741 5106
afcfe6ca
AD
5107 ++$feedctr;
5108 }
5f0a3741 5109
afcfe6ca
AD
5110 if ($feedctr == 0) {
5111 $rv .= "<li style=\"text-align : center\"><p>".__('No feeds found.')."</p></li>";
5112 }
5f0a3741 5113
afcfe6ca 5114 return $rv;
afcfe6ca 5115 }
5f0a3741 5116
40d13c28 5117?>