]> git.wh0rd.org - tt-rss.git/blame - include/functions.php
remove experimental view_settings stuff from master
[tt-rss.git] / include / functions.php
CommitLineData
1d3a17c7 1<?php
6e658547 2 define('EXPECTED_CONFIG_VERSION', 26);
b4f83063 3 define('SCHEMA_VERSION', 114);
545ca067 4
f822a8e5 5 define('LABEL_BASE_INDEX', -1024);
a413f53e 6 define('PLUGIN_FEED_BASE_INDEX', -128);
f822a8e5 7
23d2471c 8 $fetch_last_error = false;
7a01dc77 9 $fetch_last_error_code = false;
19b3992b 10 $pluginhost = false;
23d2471c 11
a48d8533 12 function __autoload($class) {
8c0496f7 13 $class_file = str_replace("_", "/", strtolower(basename($class)));
a48d8533 14
8c0496f7 15 $file = dirname(__FILE__)."/../classes/$class_file.php";
a48d8533 16
8c0496f7
AD
17 if (file_exists($file)) {
18 require $file;
a48d8533 19 }
8c0496f7 20
a48d8533 21 }
0d421af8 22
d68629dc 23 mb_internal_encoding("UTF-8");
324944f3 24 date_default_timezone_set('UTC');
8a7f5767
CW
25 if (defined('E_DEPRECATED')) {
26 error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
27 } else {
28 error_reporting(E_ALL & ~E_NOTICE);
29 }
cce28758 30
40d13c28 31 require_once 'config.php';
cc17c205 32
fc2b26a6
AD
33 if (DB_TYPE == "pgsql") {
34 define('SUBSTRING_FOR_DATE', 'SUBSTRING_FOR_DATE');
35 } else {
36 define('SUBSTRING_FOR_DATE', 'SUBSTRING');
37 }
38
0c425dc7
AD
39 define('THEME_VERSION_REQUIRED', 1.1);
40
9632f884
AD
41 /**
42 * Return available translations names.
8d505d78 43 *
9632f884
AD
44 * @access public
45 * @return array A array of available translations.
46 */
f8c612d4 47 function get_translations() {
6a214f92 48 $tr = array(
8d505d78 49 "auto" => "Detect automatically",
a3162add 50 "ca_CA" => "Català",
a06b79c4 51 "cs_CZ" => "Česky",
6a214f92 52 "en_US" => "English",
36d0510c 53 "es_ES" => "Español",
a927fe7b 54 "de_DE" => "Deutsch",
6a214f92 55 "fr_FR" => "Français",
e78fd196 56 "hu_HU" => "Magyar (Hungarian)",
bb5d3960 57 "it_IT" => "Italiano",
1d004f12 58 "ja_JP" => "日本語 (Japanese)",
7b6c1ca7 59 "lv_LV" => "Latviešu",
592535d7 60 "nb_NO" => "Norwegian bokmål",
9e7f1f12 61 "nl_NL" => "Dutch",
ea45791a 62 "pl_PL" => "Polski",
6a214f92 63 "ru_RU" => "Русский",
9a063469 64 "pt_BR" => "Portuguese/Brazil",
6a214f92 65 "zh_CN" => "Simplified Chinese");
f8c612d4
AD
66
67 return $tr;
68 }
69
7b26a148
AD
70 require_once "lib/accept-to-gettext.php";
71 require_once "lib/gettext/gettext.inc";
aba609e0 72
87d7e850 73
7b26a148 74 function startup_gettext() {
8d505d78 75
7b26a148
AD
76 # Get locale from Accept-Language header
77 $lang = al2gt(array_keys(get_translations()), "text/html");
89cb787e 78
7b26a148
AD
79 if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) {
80 $lang = _TRANSLATION_OVERRIDE_DEFAULT;
81 }
89cb787e 82
d98e76d9 83 if ($_SESSION["language"] && $_SESSION["language"] != "auto") {
afc3cf55 84 $lang = $_SESSION["language"];
7b26a148 85 }
7c33dbd4 86
7b26a148
AD
87 if ($lang) {
88 if (defined('LC_MESSAGES')) {
89 _setlocale(LC_MESSAGES, $lang);
90 } else if (defined('LC_ALL')) {
91 _setlocale(LC_ALL, $lang);
8d039718 92 }
aba609e0 93
d98e76d9 94 _bindtextdomain("messages", "locale");
865220a4 95
7b26a148
AD
96 _textdomain("messages");
97 _bind_textdomain_codeset("messages", "UTF-8");
865220a4 98 }
7b26a148
AD
99 }
100
101 startup_gettext();
cc17c205 102
b619ff15 103 require_once 'db-prefs.php';
8911ac8b 104 require_once 'version.php';
87d7e850
AD
105 require_once 'ccache.php';
106 require_once 'labels.php';
40d13c28 107
fb850eec 108 define('SELF_USER_AGENT', 'Tiny Tiny RSS/' . VERSION . ' (http://tt-rss.org/)');
500943a4
AD
109 ini_set('user_agent', SELF_USER_AGENT);
110
b0f379df 111 require_once 'lib/pubsubhubbub/publisher.php';
010efc9b 112
7d96bfcd
AD
113 $tz_offset = -1;
114 $utc_tz = new DateTimeZone('UTC');
115 $schema_version = false;
116
45004d43
AD
117 /**
118 * Print a timestamped debug message.
8d505d78 119 *
45004d43
AD
120 * @param string $msg The debug message.
121 * @return void
122 */
6f9e33e4
AD
123 function _debug($msg) {
124 $ts = strftime("%H:%M:%S", time());
2a6a9395
AD
125 if (function_exists('posix_getpid')) {
126 $ts = "$ts/" . posix_getpid();
127 }
2191eb7a
AD
128
129 if (!(defined('QUIET') && QUIET)) {
130 print "[$ts] $msg\n";
131 }
132
133 if (defined('LOGFILE')) {
134 $fp = fopen(LOGFILE, 'a+');
135
136 if ($fp) {
137 fputs($fp, "[$ts] $msg\n");
138 fclose($fp);
139 }
140 }
141
45004d43 142 } // function _debug
6f9e33e4 143
9632f884
AD
144 /**
145 * Purge a feed old posts.
8d505d78 146 *
9632f884
AD
147 * @param mixed $link A database connection.
148 * @param mixed $feed_id The id of the purged feed.
149 * @param mixed $purge_interval Olderness of purged posts.
150 * @param boolean $debug Set to True to enable the debug. False by default.
151 * @access public
152 * @return void
153 */
ad507f85
AD
154 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
155
07d0efe9 156 if (!$purge_interval) $purge_interval = feed_purge_interval($link, $feed_id);
8d505d78 157
ad507f85 158 $rows = -1;
4c193675 159
8d505d78 160 $result = db_query($link,
07d0efe9
AD
161 "SELECT owner_uid FROM ttrss_feeds WHERE id = '$feed_id'");
162
163 $owner_uid = false;
164
165 if (db_num_rows($result) == 1) {
166 $owner_uid = db_fetch_result($result, 0, "owner_uid");
167 }
168
ab954dff
AD
169 if ($purge_interval == -1 || !$purge_interval) {
170 if ($owner_uid) {
171 ccache_update($link, $feed_id, $owner_uid);
172 }
173 return;
174 }
175
07d0efe9
AD
176 if (!$owner_uid) return;
177
3907ef71
AD
178 if (FORCE_ARTICLE_PURGE == 0) {
179 $purge_unread = get_pref($link, "PURGE_UNREAD_ARTICLES",
180 $owner_uid, false);
181 } else {
182 $purge_unread = true;
183 $purge_interval = FORCE_ARTICLE_PURGE;
184 }
07d0efe9
AD
185
186 if (!$purge_unread) $query_limit = " unread = false AND ";
187
fefa6ca3 188 if (DB_TYPE == "pgsql") {
6e7f8d26
AD
189 $pg_version = get_pgsql_version($link);
190
191 if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
1e59ae35 192
8d505d78
AD
193 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
194 ttrss_entries.id = ref_id AND
195 marked = false AND
196 feed_id = '$feed_id' AND
07d0efe9 197 $query_limit
25ea2805 198 ttrss_entries.date_updated < NOW() - INTERVAL '$purge_interval days'");
1e59ae35
AD
199
200 } else {
201
8d505d78
AD
202 $result = db_query($link, "DELETE FROM ttrss_user_entries
203 USING ttrss_entries
204 WHERE ttrss_entries.id = ref_id AND
205 marked = false AND
206 feed_id = '$feed_id' AND
07d0efe9 207 $query_limit
25ea2805 208 ttrss_entries.date_updated < NOW() - INTERVAL '$purge_interval days'");
1e59ae35 209 }
ad507f85 210
8c0496f7
AD
211 $rows = pg_affected_rows($result);
212
fefa6ca3 213 } else {
8d505d78 214
30f1746f 215/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 216 marked = false AND feed_id = '$feed_id' AND
8d505d78 217 (SELECT date_updated FROM ttrss_entries WHERE
30f1746f
AD
218 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
219
8d505d78
AD
220 $result = db_query($link, "DELETE FROM ttrss_user_entries
221 USING ttrss_user_entries, ttrss_entries
222 WHERE ttrss_entries.id = ref_id AND
223 marked = false AND
224 feed_id = '$feed_id' AND
07d0efe9 225 $query_limit
25ea2805 226 ttrss_entries.date_updated < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
8d505d78 227
8c0496f7
AD
228 $rows = mysql_affected_rows($link);
229
ad507f85
AD
230 }
231
ced46404
AD
232 ccache_update($link, $feed_id, $owner_uid);
233
ad507f85 234 if ($debug) {
6f9e33e4 235 _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
fefa6ca3 236 }
2ea09bde
AD
237
238 return $rows;
9632f884 239 } // function purge_feed
fefa6ca3 240
07d0efe9
AD
241 function feed_purge_interval($link, $feed_id) {
242
8d505d78 243 $result = db_query($link, "SELECT purge_interval, owner_uid FROM ttrss_feeds
07d0efe9
AD
244 WHERE id = '$feed_id'");
245
246 if (db_num_rows($result) == 1) {
247 $purge_interval = db_fetch_result($result, 0, "purge_interval");
248 $owner_uid = db_fetch_result($result, 0, "owner_uid");
249
8d505d78 250 if ($purge_interval == 0) $purge_interval = get_pref($link,
863be6ca 251 'PURGE_OLD_DAYS', $owner_uid);
07d0efe9
AD
252
253 return $purge_interval;
254
255 } else {
256 return -1;
257 }
258 }
259
a2d79981
AD
260 function purge_orphans($link, $do_output = false) {
261
71604ca4 262 // purge orphaned posts in main content table
8d505d78 263 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
71604ca4 264 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
a2d79981
AD
265
266 if ($do_output) {
267 $rows = db_affected_rows($link, $result);
268 _debug("Purged $rows orphaned posts.");
269 }
c3a8d71a
AD
270 }
271
c7d57b66
AD
272 function get_feed_update_interval($link, $feed_id) {
273 $result = db_query($link, "SELECT owner_uid, update_interval FROM
274 ttrss_feeds WHERE id = '$feed_id'");
275
276 if (db_num_rows($result) == 1) {
277 $update_interval = db_fetch_result($result, 0, "update_interval");
278 $owner_uid = db_fetch_result($result, 0, "owner_uid");
279
280 if ($update_interval != 0) {
281 return $update_interval;
282 } else {
283 return get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $owner_uid, false);
284 }
285
286 } else {
287 return -1;
288 }
289 }
290
7a01dc77 291 function fetch_file_contents($url, $type = false, $login = false, $pass = false, $post_query = false, $timeout = false, $timestamp = 0) {
8d505d78 292
23d2471c 293 global $fetch_last_error;
7a01dc77 294 global $fetch_last_error_code;
23d2471c 295
3610b48b 296 if (function_exists('curl_init') && !ini_get("open_basedir")) {
b799dc8b
AD
297
298 if (ini_get("safe_mode")) {
299 $ch = curl_init(geturl($url));
300 } else {
301 $ch = curl_init($url);
302 }
a1af1574 303
7a01dc77
AD
304 if ($timestamp) {
305 curl_setopt($ch, CURLOPT_HTTPHEADER,
306 array("If-Modified-Since: ".gmdate('D, d M Y H:i:s \G\M\T', $timestamp)));
307 }
308
fb850eec
AD
309 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout ? $timeout : 15);
310 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout ? $timeout : 45);
81153e6b 311 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("safe_mode"));
a1af1574
AD
312 curl_setopt($ch, CURLOPT_MAXREDIRS, 20);
313 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
314 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
8d505d78 315 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
5f6804bc 316 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
19929bbe 317 curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
268a06dc 318 curl_setopt($ch, CURLOPT_ENCODING , "gzip");
48b657fc 319 curl_setopt($ch, CURLOPT_REFERER, $url);
8d505d78 320
ae5f7bb1
AD
321 if ($post_query) {
322 curl_setopt($ch, CURLOPT_POST, true);
323 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_query);
324 }
325
8d505d78
AD
326 if ($login && $pass)
327 curl_setopt($ch, CURLOPT_USERPWD, "$login:$pass");
a1af1574 328
fb074239 329 $contents = @curl_exec($ch);
268a06dc 330
48b657fc
AD
331 if (curl_errno($ch) === 23 || curl_errno($ch) === 61) {
332 curl_setopt($ch, CURLOPT_ENCODING, 'none');
333 $contents = @curl_exec($ch);
fb850eec
AD
334 }
335
a1af1574 336 if ($contents === false) {
fb850eec 337 $fetch_last_error = curl_errno($ch) . " " . curl_error($ch);
a1af1574
AD
338 curl_close($ch);
339 return false;
4065b60b
AD
340 }
341
8d505d78 342 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
a1af1574 343 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
4065b60b 344
7a01dc77
AD
345 $fetch_last_error_code = $http_code;
346
8d505d78 347 if ($http_code != 200 || $type && strpos($content_type, "$type") === false) {
fb850eec
AD
348 if (curl_errno($ch) != 0) {
349 $fetch_last_error = curl_errno($ch) . " " . curl_error($ch);
350 } else {
351 $fetch_last_error = "HTTP Code: $http_code";
352 }
353 curl_close($ch);
a1af1574
AD
354 return false;
355 }
4065b60b 356
fb850eec
AD
357 curl_close($ch);
358
a1af1574 359 return $contents;
4065b60b 360 } else {
d3911f80 361 if ($login && $pass){
8d505d78
AD
362 $url_parts = array();
363
364 preg_match("/(^[^:]*):\/\/(.*)/", $url, $url_parts);
365
d3911f80
AD
366 $pass = urlencode($pass);
367
8d505d78
AD
368 if ($url_parts[1] && $url_parts[2]) {
369 $url = $url_parts[1] . "://$login:$pass@" . $url_parts[2];
370 }
371 }
372
23d2471c
AD
373 $data = @file_get_contents($url);
374
9e7f1f12 375 @$gzdecoded = gzdecode($data);
a0f2a3e1
AD
376 if ($gzdecoded) $data = $gzdecoded;
377
23d2471c
AD
378 if (!$data && function_exists('error_get_last')) {
379 $error = error_get_last();
380 $fetch_last_error = $error["message"];
381 }
382 return $data;
4065b60b
AD
383 }
384
385 }
78800912 386
9632f884
AD
387 /**
388 * Try to determine the favicon URL for a feed.
389 * adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
390 * http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
8d505d78 391 *
9632f884
AD
392 * @param string $url A feed or page URL
393 * @access public
394 * @return mixed The favicon URL, or false if none was found.
395 */
1bd11fdf 396 function get_favicon_url($url) {
99331724 397
1bd11fdf 398 $favicon_url = false;
ed214298 399
4065b60b 400 if ($html = @fetch_file_contents($url)) {
78800912 401
ed214298 402 libxml_use_internal_errors(true);
c798704b 403
ed214298
AD
404 $doc = new DOMDocument();
405 $doc->loadHTML($html);
406 $xpath = new DOMXPath($doc);
717f5e64 407
a712429e
AD
408 $base = $xpath->query('/html/head/base');
409 foreach ($base as $b) {
410 $url = $b->getAttribute("href");
411 break;
412 }
413
1bd11fdf 414 $entries = $xpath->query('/html/head/link[@rel="shortcut icon" or @rel="icon"]');
ed214298
AD
415 if (count($entries) > 0) {
416 foreach ($entries as $entry) {
1bd11fdf
AD
417 $favicon_url = rewrite_relative_url($url, $entry->getAttribute("href"));
418 break;
ed214298 419 }
8d505d78 420 }
4065b60b 421 }
c798704b 422
1bd11fdf
AD
423 if (!$favicon_url)
424 $favicon_url = rewrite_relative_url($url, "/favicon.ico");
425
426 return $favicon_url;
427 } // function get_favicon_url
428
429 function check_feed_favicon($site_url, $feed, $link) {
882311d9 430# print "FAVICON [$site_url]: $favicon_url\n";
4065b60b 431
1bd11fdf
AD
432 $icon_file = ICONS_DIR . "/$feed.ico";
433
434 if (!file_exists($icon_file)) {
435 $favicon_url = get_favicon_url($site_url);
436
437 if ($favicon_url) {
438 // Limiting to "image" type misses those served with text/plain
439 $contents = fetch_file_contents($favicon_url); // , "image");
440
441 if ($contents) {
442 // Crude image type matching.
443 // Patterns gleaned from the file(1) source code.
444 if (preg_match('/^\x00\x00\x01\x00/', $contents)) {
445 // 0 string \000\000\001\000 MS Windows icon resource
446 //error_log("check_feed_favicon: favicon_url=$favicon_url isa MS Windows icon resource");
447 }
448 elseif (preg_match('/^GIF8/', $contents)) {
449 // 0 string GIF8 GIF image data
450 //error_log("check_feed_favicon: favicon_url=$favicon_url isa GIF image");
451 }
452 elseif (preg_match('/^\x89PNG\x0d\x0a\x1a\x0a/', $contents)) {
453 // 0 string \x89PNG\x0d\x0a\x1a\x0a PNG image data
454 //error_log("check_feed_favicon: favicon_url=$favicon_url isa PNG image");
455 }
456 elseif (preg_match('/^\xff\xd8/', $contents)) {
457 // 0 beshort 0xffd8 JPEG image data
458 //error_log("check_feed_favicon: favicon_url=$favicon_url isa JPG image");
459 }
460 else {
461 //error_log("check_feed_favicon: favicon_url=$favicon_url isa UNKNOWN type");
462 $contents = "";
463 }
464 }
465
466 if ($contents) {
467 $fp = @fopen($icon_file, "w");
468
469 if ($fp) {
470 fwrite($fp, $contents);
471 fclose($fp);
472 chmod($icon_file, 0644);
473 }
474 }
475 }
78800912
AD
476 }
477 }
478
f175937c 479 function print_select($id, $default, $values, $attributes = "") {
79f3553b 480 print "<select name=\"$id\" id=\"$id\" $attributes>";
a0d53889
AD
481 foreach ($values as $v) {
482 if ($v == $default)
60807300 483 $sel = "selected=\"1\"";
a0d53889
AD
484 else
485 $sel = "";
8d505d78 486
e88c1943
AD
487 $v = trim($v);
488
60807300 489 print "<option value=\"$v\" $sel>$v</option>";
a0d53889
AD
490 }
491 print "</select>";
492 }
40d13c28 493
79f3553b
AD
494 function print_select_hash($id, $default, $values, $attributes = "") {
495 print "<select name=\"$id\" id='$id' $attributes>";
673d54ca
AD
496 foreach (array_keys($values) as $v) {
497 if ($v == $default)
74d5c8fa 498 $sel = 'selected="selected"';
673d54ca
AD
499 else
500 $sel = "";
8d505d78 501
e88c1943
AD
502 $v = trim($v);
503
673d54ca
AD
504 print "<option $sel value=\"$v\">".$values[$v]."</option>";
505 }
506
507 print "</select>";
508 }
509
f541eb78 510 function print_radio($id, $default, $true_is, $values, $attributes = "") {
77e96719 511 foreach ($values as $v) {
8d505d78 512
77e96719 513 if ($v == $default)
5da169d9 514 $sel = "checked";
77e96719 515 else
5da169d9
AD
516 $sel = "";
517
f541eb78 518 if ($v == $true_is) {
5da169d9
AD
519 $sel .= " value=\"1\"";
520 } else {
521 $sel .= " value=\"0\"";
522 }
8d505d78
AD
523
524 print "<input class=\"noborder\" dojoType=\"dijit.form.RadioButton\"
69654950 525 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
77e96719
AD
526
527 }
528 }
529
d9084cf2 530 function initialize_user_prefs($link, $uid, $profile = false) {
ff485f1d 531
3972bf59 532 $uid = db_escape_string($link, $uid);
ff485f1d 533
d9084cf2
AD
534 if (!$profile) {
535 $profile = "NULL";
f9aa6a89 536 $profile_qpart = "AND profile IS NULL";
d9084cf2 537 } else {
f9aa6a89 538 $profile_qpart = "AND profile = '$profile'";
d9084cf2
AD
539 }
540
f9aa6a89
AD
541 if (get_schema_version($link) < 63) $profile_qpart = "";
542
ff485f1d
AD
543 db_query($link, "BEGIN");
544
545 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
8d505d78
AD
546
547 $u_result = db_query($link, "SELECT pref_name
f9aa6a89 548 FROM ttrss_user_prefs WHERE owner_uid = '$uid' $profile_qpart");
ff485f1d
AD
549
550 $active_prefs = array();
551
552 while ($line = db_fetch_assoc($u_result)) {
8d505d78 553 array_push($active_prefs, $line["pref_name"]);
ff485f1d
AD
554 }
555
556 while ($line = db_fetch_assoc($result)) {
557 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
558// print "adding " . $line["pref_name"] . "<br>";
559
d296ba50
AD
560 $line["def_value"] = db_escape_string($link, $line["def_value"]);
561 $line["pref_name"] = db_escape_string($link, $line["pref_name"]);
562
f9aa6a89
AD
563 if (get_schema_version($link) < 63) {
564 db_query($link, "INSERT INTO ttrss_user_prefs
8d505d78 565 (owner_uid,pref_name,value) VALUES
f9aa6a89
AD
566 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
567
568 } else {
569 db_query($link, "INSERT INTO ttrss_user_prefs
8d505d78 570 (owner_uid,pref_name,value, profile) VALUES
f9aa6a89
AD
571 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."', $profile)");
572 }
ff485f1d
AD
573
574 }
575 }
576
577 db_query($link, "COMMIT");
578
579 }
956c7629 580
8de8bfb8
AD
581 function get_ssl_certificate_id() {
582 if ($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"]) {
583 return sha1($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"] .
584 $_SERVER["REDIRECT_SSL_CLIENT_V_START"] .
585 $_SERVER["REDIRECT_SSL_CLIENT_V_END"] .
586 $_SERVER["REDIRECT_SSL_CLIENT_S_DN"]);
587 }
588 return "";
589 }
590
0d421af8 591 function authenticate_user($link, $login, $password, $check_only = false) {
c8437f35 592
131b01b3 593 if (!SINGLE_USER_MODE) {
0d421af8 594 $user_id = false;
0f28f81f
AD
595
596 global $pluginhost;
597 foreach ($pluginhost->get_hooks($pluginhost::HOOK_AUTH_USER) as $plugin) {
598
599 $user_id = (int) $plugin->authenticate($login, $password);
600
601 if ($user_id) {
602 $_SESSION["auth_module"] = strtolower(get_class($plugin));
603 break;
604 }
461766f3
AD
605 }
606
0d421af8 607 if ($user_id && !$check_only) {
70400171
AD
608 @session_start();
609
0d421af8
AD
610 $_SESSION["uid"] = $user_id;
611
612 $result = db_query($link, "SELECT login,access_level,pwd_hash FROM ttrss_users
613 WHERE id = '$user_id'");
8d505d78 614
131b01b3
AD
615 $_SESSION["name"] = db_fetch_result($result, 0, "login");
616 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
8484ce22 617 $_SESSION["csrf_token"] = sha1(uniqid(rand(), true));
8d505d78
AD
618
619 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
131b01b3 620 $_SESSION["uid"]);
8d505d78 621
131b01b3 622 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1a9f4d3c 623 $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash");
91c5f229
AD
624
625 $_SESSION["last_version_check"] = time();
8d505d78 626
131b01b3 627 initialize_user_prefs($link, $_SESSION["uid"]);
8d505d78 628
131b01b3
AD
629 return true;
630 }
8d505d78 631
131b01b3 632 return false;
503eb349 633
131b01b3 634 } else {
503eb349 635
131b01b3
AD
636 $_SESSION["uid"] = 1;
637 $_SESSION["name"] = "admin";
787e5ebc 638 $_SESSION["access_level"] = 10;
21e42e5f 639
0d421af8
AD
640 $_SESSION["hide_hello"] = true;
641 $_SESSION["hide_logout"] = true;
642
d5fd183d
AD
643 $_SESSION["auth_module"] = false;
644
21e42e5f
AD
645 if (!$_SESSION["csrf_token"]) {
646 $_SESSION["csrf_token"] = sha1(uniqid(rand(), true));
647 }
f557cd78 648
0bbba72d 649 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
8d505d78 650
0bbba72d 651 initialize_user_prefs($link, $_SESSION["uid"]);
8d505d78 652
c8437f35
AD
653 return true;
654 }
c8437f35
AD
655 }
656
e6cb77a0
AD
657 function make_password($length = 8) {
658
85db6213
AD
659 $password = "";
660 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
661
662 $i = 0;
663
664 while ($i < $length) {
665 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
666
667 if (!strstr($password, $char)) {
668 $password .= $char;
669 $i++;
670 }
671 }
672 return $password;
e6cb77a0
AD
673 }
674
675 // this is called after user is created to initialize default feeds, labels
676 // or whatever else
8d505d78 677
e6cb77a0
AD
678 // user preferences are checked on every login, not here
679
680 function initialize_user($link, $uid) {
681
e6cb77a0 682 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 683 values ('$uid', 'Tiny Tiny RSS: New Releases',
b6d486a3 684 'http://tt-rss.org/releases.rss')");
3b0feb9b 685
cd2cd415
AD
686 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
687 values ('$uid', 'Tiny Tiny RSS: Forum',
f0855b88 688 'http://tt-rss.org/forum/rss.php')");
3b0feb9b 689 }
e6cb77a0 690
b8aa49bc 691 function logout_user() {
5ccc1cf5
AD
692 session_destroy();
693 if (isset($_COOKIE[session_name()])) {
694 setcookie(session_name(), '', time()-42000, '/');
695 }
b8aa49bc
AD
696 }
697
8484ce22
AD
698 function validate_csrf($csrf_token) {
699 return $csrf_token == $_SESSION['csrf_token'];
700 }
701
de612e7a
AD
702 function load_user_plugins($link, $owner_uid) {
703 if ($owner_uid) {
704 $plugins = get_pref($link, "_ENABLED_PLUGINS", $owner_uid);
705
706 global $pluginhost;
d8a1d2a2 707 $pluginhost->load($plugins, $pluginhost::KIND_USER, $owner_uid);
e9c04fd4
AD
708
709 if (get_schema_version($link) > 100) {
710 $pluginhost->load_data();
711 }
de612e7a
AD
712 }
713 }
714
d98e76d9 715 function login_sequence($link) {
75a316ab
AD
716 $_SESSION["prefs_cache"] = false;
717
97acbaf1 718 if (SINGLE_USER_MODE) {
25db6c51 719 @session_start();
de612e7a 720 authenticate_user($link, "admin", null);
0a117b86 721 cache_prefs($link);
de612e7a 722 load_user_plugins($link, $_SESSION["uid"]);
97acbaf1
AD
723 } else {
724 if (!$_SESSION["uid"] || !validate_session($link)) {
725
726 if (AUTH_AUTO_LOGIN && authenticate_user($link, null, null)) {
727 $_SESSION["ref_schema_version"] = get_schema_version($link, true);
728 } else {
729 authenticate_user($link, null, null, true);
730 }
731
d98e76d9 732 if (!$_SESSION["uid"]) render_login_form($link);
97acbaf1
AD
733
734 } else {
735 /* bump login timestamp */
736 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
737 $_SESSION["uid"]);
06b0777f 738 $_SESSION["last_login_update"] = time();
01a87dff
AD
739 }
740
afc3cf55
AD
741 if ($_SESSION["uid"] && $_SESSION["language"] && SESSION_COOKIE_LIFETIME > 0) {
742 setcookie("ttrss_lang", $_SESSION["language"],
743 time() + SESSION_COOKIE_LIFETIME);
b8aa49bc 744 }
de612e7a
AD
745
746 if ($_SESSION["uid"]) {
0a117b86 747 cache_prefs($link);
de612e7a 748 load_user_plugins($link, $_SESSION["uid"]);
b1b1d25f
AD
749
750 /* cleanup ccache */
751
752 db_query($link, "DELETE FROM ttrss_counters_cache WHERE owner_uid = ".
753 $_SESSION["uid"] . " AND
754 (SELECT COUNT(id) FROM ttrss_feeds WHERE
755 ttrss_feeds.id = feed_id) = 0");
756
757 db_query($link, "DELETE FROM ttrss_cat_counters_cache WHERE owner_uid = ".
758 $_SESSION["uid"] . " AND
759 (SELECT COUNT(id) FROM ttrss_feed_categories WHERE
760 ttrss_feed_categories.id = feed_id) = 0");
761
de612e7a 762 }
b1b1d25f 763
b8aa49bc 764 }
afc3cf55 765 }
3547842a 766
411fe209 767 function truncate_string($str, $max_len, $suffix = '&hellip;') {
12db369c 768 if (mb_strlen($str, "utf-8") > $max_len - 3) {
411fe209 769 return mb_substr($str, 0, $max_len, "utf-8") . $suffix;
3547842a
AD
770 } else {
771 return $str;
772 }
773 }
54a60e1a 774
ab4b768f
AD
775 function convert_timestamp($timestamp, $source_tz, $dest_tz) {
776
777 try {
778 $source_tz = new DateTimeZone($source_tz);
779 } catch (Exception $e) {
780 $source_tz = new DateTimeZone('UTC');
781 }
782
783 try {
784 $dest_tz = new DateTimeZone($dest_tz);
785 } catch (Exception $e) {
786 $dest_tz = new DateTimeZone('UTC');
787 }
788
789 $dt = new DateTime(date('Y-m-d H:i:s', $timestamp), $source_tz);
790 return $dt->format('U') + $dest_tz->getOffset($dt);
791 }
792
324944f3
AD
793 function make_local_datetime($link, $timestamp, $long, $owner_uid = false,
794 $no_smart_dt = false) {
795
796 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
797 if (!$timestamp) $timestamp = '1970-01-01 0:00';
798
7d96bfcd
AD
799 global $utc_tz;
800 global $tz_offset;
324944f3 801
7d96bfcd
AD
802 # We store date in UTC internally
803 $dt = new DateTime($timestamp, $utc_tz);
804
805 if ($tz_offset == -1) {
806
807 $user_tz_string = get_pref($link, 'USER_TIMEZONE', $owner_uid);
808
809 try {
810 $user_tz = new DateTimeZone($user_tz_string);
811 } catch (Exception $e) {
812 $user_tz = $utc_tz;
813 }
814
815 $tz_offset = $user_tz->getOffset($dt);
324944f3
AD
816 }
817
7d96bfcd 818 $user_timestamp = $dt->format('U') + $tz_offset;
324944f3 819
1dc52ae7 820 if (!$no_smart_dt) {
8d505d78 821 return smart_date_time($link, $user_timestamp,
7d96bfcd 822 $tz_offset, $owner_uid);
324944f3
AD
823 } else {
824 if ($long)
825 $format = get_pref($link, 'LONG_DATE_FORMAT', $owner_uid);
826 else
827 $format = get_pref($link, 'SHORT_DATE_FORMAT', $owner_uid);
828
829 return date($format, $user_timestamp);
830 }
831 }
832
2a5c136e
AD
833 function smart_date_time($link, $timestamp, $tz_offset = 0, $owner_uid = false) {
834 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
835
836 if (date("Y.m.d", $timestamp) == date("Y.m.d", time() + $tz_offset)) {
be773442 837 return date("G:i", $timestamp);
2a5c136e
AD
838 } else if (date("Y", $timestamp) == date("Y", time() + $tz_offset)) {
839 $format = get_pref($link, 'SHORT_DATE_FORMAT', $owner_uid);
840 return date($format, $timestamp);
be773442 841 } else {
2a5c136e
AD
842 $format = get_pref($link, 'LONG_DATE_FORMAT', $owner_uid);
843 return date($format, $timestamp);
be773442
AD
844 }
845 }
846
e3c99f3b 847 function sql_bool_to_bool($s) {
9955a134 848 if ($s == "t" || $s == "1" || strtolower($s) == "true") {
e3c99f3b
AD
849 return true;
850 } else {
851 return false;
852 }
853 }
8d505d78 854
badac687
AD
855 function bool_to_sql_bool($s) {
856 if ($s) {
857 return "true";
858 } else {
859 return "false";
860 }
861 }
e3c99f3b 862
fcfa9ef1
AD
863 // Session caching removed due to causing wrong redirects to upgrade
864 // script when get_schema_version() is called on an obsolete session
865 // created on a previous schema version.
199db684 866 function get_schema_version($link, $nocache = false) {
7d96bfcd
AD
867 global $schema_version;
868
869 if (!$schema_version) {
199db684
AD
870 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
871 $version = db_fetch_result($result, 0, "schema_version");
7d96bfcd 872 $schema_version = $version;
199db684 873 return $version;
7d96bfcd
AD
874 } else {
875 return $schema_version;
876 }
e4c51a6c
AD
877 }
878
6043fb7e 879 function sanity_check($link) {
31303c6b 880 require_once 'errors.php';
ebb948c2 881
6043fb7e 882 $error_code = 0;
7d96bfcd 883 $schema_version = get_schema_version($link, true);
6043fb7e
AD
884
885 if ($schema_version != SCHEMA_VERSION) {
886 $error_code = 5;
887 }
888
aec3ce39
AD
889 if (DB_TYPE == "mysql") {
890 $result = db_query($link, "SELECT true", false);
891 if (db_num_rows($result) != 1) {
892 $error_code = 10;
893 }
894 }
895
3972bf59 896 if (db_escape_string($link, "testTEST") != "testTEST") {
f29ba148
AD
897 $error_code = 12;
898 }
899
ebb948c2 900 return array("code" => $error_code, "message" => $ERRORS[$error_code]);
6043fb7e
AD
901 }
902
27981ca3 903 function file_is_locked($filename) {
31a6d42d 904 if (function_exists('flock')) {
fb074239 905 $fp = @fopen(LOCK_DIRECTORY . "/$filename", "r");
31a6d42d
AD
906 if ($fp) {
907 if (flock($fp, LOCK_EX | LOCK_NB)) {
908 flock($fp, LOCK_UN);
909 fclose($fp);
910 return false;
911 }
27981ca3 912 fclose($fp);
31a6d42d 913 return true;
e89aed7b
AD
914 } else {
915 return false;
27981ca3 916 }
27981ca3 917 }
c1fb4a5e 918 return true; // consider the file always locked and skip the test
27981ca3
AD
919 }
920
fcb4c0c9 921 function make_lockfile($filename) {
cfa43e02 922 $fp = fopen(LOCK_DIRECTORY . "/$filename", "w");
fcb4c0c9 923
a44bfcfd 924 if ($fp && flock($fp, LOCK_EX | LOCK_NB)) {
4c59adb1
AD
925 if (function_exists('posix_getpid')) {
926 fwrite($fp, posix_getpid() . "\n");
927 }
fcb4c0c9
AD
928 return $fp;
929 } else {
930 return false;
931 }
932 }
933
bf7fcde8 934 function make_stampfile($filename) {
cfa43e02 935 $fp = fopen(LOCK_DIRECTORY . "/$filename", "w");
bf7fcde8 936
8e00ae9b 937 if (flock($fp, LOCK_EX | LOCK_NB)) {
bf7fcde8 938 fwrite($fp, time() . "\n");
8e00ae9b 939 flock($fp, LOCK_UN);
bf7fcde8
AD
940 fclose($fp);
941 return true;
942 } else {
943 return false;
944 }
945 }
946
894ebcf5 947 function sql_random_function() {
8c0496f7 948 if (DB_TYPE == "mysql") {
894ebcf5
AD
949 return "RAND()";
950 } else {
951 return "RANDOM()";
952 }
953 }
954
c8b693cf 955 function catchup_feed($link, $feed, $cat_view, $owner_uid = false, $max_id = false, $mode = 'all') {
c7e51de1
AD
956
957 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
88040f57 958
37c03d3a 959 //if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
22fdebff 960
c8b693cf
AD
961 // Todo: all this interval stuff needs some generic generator function
962
963 $date_qpart = "false";
964
965 switch ($mode) {
966 case "1day":
967 if (DB_TYPE == "pgsql") {
94828a8b 968 $date_qpart = "date_entered < NOW() - INTERVAL '1 day' ";
c8b693cf 969 } else {
d5381016 970 $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY) ";
c8b693cf
AD
971 }
972 break;
973 case "1week":
974 if (DB_TYPE == "pgsql") {
94828a8b 975 $date_qpart = "date_entered < NOW() - INTERVAL '1 week' ";
c8b693cf 976 } else {
94828a8b 977 $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 WEEK) ";
c8b693cf
AD
978 }
979 break;
980 case "2weeks":
981 if (DB_TYPE == "pgsql") {
94828a8b 982 $date_qpart = "date_entered < NOW() - INTERVAL '2 week' ";
c8b693cf 983 } else {
94828a8b 984 $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 2 WEEK) ";
c8b693cf
AD
985 }
986 break;
987 default:
988 $date_qpart = "true";
989 }
990
37c03d3a 991 if (is_numeric($feed)) {
23aa0d16
AD
992 if ($cat_view) {
993
72a2f4f5 994 if ($feed >= 0) {
f9fca8cb
AD
995
996 if ($feed > 0) {
bda6afa2
AD
997 $children = getChildCategories($link, $feed, $owner_uid);
998 array_push($children, $feed);
999
1000 $children = join(",", $children);
1001
1002 $cat_qpart = "cat_id IN ($children)";
f9fca8cb
AD
1003 } else {
1004 $cat_qpart = "cat_id IS NULL";
1005 }
8d505d78 1006
bda6afa2 1007 db_query($link, "UPDATE ttrss_user_entries
c8b693cf
AD
1008 SET unread = false, last_read = NOW() WHERE ref_id IN
1009 (SELECT id FROM
1010 (SELECT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1011 AND owner_uid = $owner_uid AND unread = true AND feed_id IN
1012 (SELECT id FROM ttrss_feeds WHERE $cat_qpart) AND $date_qpart) as tmp)");
23aa0d16 1013
f9fca8cb 1014 } else if ($feed == -2) {
23aa0d16 1015
8d505d78
AD
1016 db_query($link, "UPDATE ttrss_user_entries
1017 SET unread = false,last_read = NOW() WHERE (SELECT COUNT(*)
1018 FROM ttrss_user_labels2 WHERE article_id = ref_id) > 0
c8b693cf 1019 AND unread = true AND $date_qpart AND owner_uid = $owner_uid");
23aa0d16
AD
1020 }
1021
1022 } else if ($feed > 0) {
1023
8d505d78 1024 db_query($link, "UPDATE ttrss_user_entries
c8b693cf
AD
1025 SET unread = false, last_read = NOW() WHERE ref_id IN
1026 (SELECT id FROM
1027 (SELECT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1028 AND owner_uid = $owner_uid AND unread = true AND feed_id = $feed AND $date_qpart) as tmp)");
8d505d78 1029
f822a8e5 1030 } else if ($feed < 0 && $feed > LABEL_BASE_INDEX) { // special, like starred
23aa0d16
AD
1031
1032 if ($feed == -1) {
8d505d78 1033 db_query($link, "UPDATE ttrss_user_entries
c8b693cf
AD
1034 SET unread = false, last_read = NOW() WHERE ref_id IN
1035 (SELECT id FROM
1036 (SELECT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1037 AND owner_uid = $owner_uid AND unread = true AND marked = true AND $date_qpart) as tmp)");
23aa0d16 1038 }
e4f4b46f
AD
1039
1040 if ($feed == -2) {
8d505d78 1041 db_query($link, "UPDATE ttrss_user_entries
c8b693cf
AD
1042 SET unread = false, last_read = NOW() WHERE ref_id IN
1043 (SELECT id FROM
1044 (SELECT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1045 AND owner_uid = $owner_uid AND unread = true AND published = true AND $date_qpart) as tmp)");
e4f4b46f
AD
1046 }
1047
2d24f032
AD
1048 if ($feed == -3) {
1049
c1d7e6c3
AD
1050 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
1051
2d24f032 1052 if (DB_TYPE == "pgsql") {
8d505d78 1053 $match_part = "updated > NOW() - INTERVAL '$intl hour' ";
2d24f032 1054 } else {
8d505d78 1055 $match_part = "updated > DATE_SUB(NOW(),
c1d7e6c3 1056 INTERVAL $intl HOUR) ";
2d24f032
AD
1057 }
1058
c8b693cf
AD
1059 db_query($link, "UPDATE ttrss_user_entries
1060 SET unread = false, last_read = NOW() WHERE ref_id IN
1061 (SELECT id FROM
1062 (SELECT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1063 AND owner_uid = $owner_uid AND unread = true AND feed_id = $feed AND $date_qpart AND $match_part) as tmp)");
2d24f032
AD
1064 }
1065
3584cb11 1066 if ($feed == -4) {
8d505d78 1067 db_query($link, "UPDATE ttrss_user_entries
c8b693cf
AD
1068 SET unread = false, last_read = NOW() WHERE ref_id IN
1069 (SELECT id FROM
1070 (SELECT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1071 AND owner_uid = $owner_uid AND unread = true AND $date_qpart) as tmp)");
3584cb11
AD
1072 }
1073
f822a8e5 1074 } else if ($feed < LABEL_BASE_INDEX) { // label
23aa0d16 1075
f822a8e5 1076 $label_id = feed_to_label_id($feed);
23aa0d16 1077
c8b693cf
AD
1078 db_query($link, "UPDATE ttrss_user_entries
1079 SET unread = false, last_read = NOW() WHERE ref_id IN
1080 (SELECT id FROM
1081 (SELECT ttrss_entries.id FROM ttrss_entries, ttrss_user_entries, ttrss_user_labels2 WHERE ref_id = id
1082 AND label_id = '$label_id' AND ref_id = article_id
1083 AND owner_uid = $owner_uid AND unread = true AND $date_qpart) as tmp)");
23aa0d16 1084
23aa0d16 1085 }
ad0056a8 1086
c7e51de1 1087 ccache_update($link, $feed, $owner_uid, $cat_view);
ad0056a8 1088
23aa0d16 1089 } else { // tag
c8b693cf
AD
1090 db_query($link, "UPDATE ttrss_user_entries
1091 SET unread = false, last_read = NOW() WHERE ref_id IN
1092 (SELECT id FROM
1093 (SELECT ttrss_entries.id FROM ttrss_entries, ttrss_user_entries, ttrss_tags WHERE ref_id = ttrss_entries.id
1094 AND post_int_id = int_id AND tag_name = '$feed'
1095 AND ttrss_user_entries.owner_uid = $owner_uid AND unread = true AND $date_qpart) as tmp)");
23aa0d16 1096
23aa0d16
AD
1097 }
1098 }
1099
5b55e9e2 1100 function getAllCounters($link) {
6a7817c1 1101 $data = getGlobalCounters($link);
8d505d78 1102
6a7817c1 1103 $data = array_merge($data, getVirtCounters($link));
5b55e9e2
AD
1104 $data = array_merge($data, getLabelCounters($link));
1105 $data = array_merge($data, getFeedCounters($link, $active_feed));
1106 $data = array_merge($data, getCategoryCounters($link));
6a7817c1
AD
1107
1108 return $data;
8d505d78 1109 }
a9cb1f83 1110
79178062
AD
1111 function getCategoryTitle($link, $cat_id) {
1112
1113 if ($cat_id == -1) {
1114 return __("Special");
1115 } else if ($cat_id == -2) {
1116 return __("Labels");
1117 } else {
1118
1119 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
1120 id = '$cat_id'");
1121
1122 if (db_num_rows($result) == 1) {
1123 return db_fetch_result($result, 0, "title");
1124 } else {
f99759da 1125 return __("Uncategorized");
79178062
AD
1126 }
1127 }
1128 }
1129
1130
a9cb1f83 1131 function getCategoryCounters($link) {
6a7817c1 1132 $ret_arr = array();
bba7c4bf 1133
6a7817c1 1134 /* Labels category */
bba7c4bf 1135
8acc449c 1136 $cv = array("id" => -2, "kind" => "cat",
6a7817c1 1137 "counter" => getCategoryUnread($link, -2));
bba7c4bf 1138
6a7817c1 1139 array_push($ret_arr, $cv);
bba7c4bf 1140
41759103 1141 $result = db_query($link, "SELECT id AS cat_id, value AS unread,
2c5f231e
AD
1142 (SELECT COUNT(id) FROM ttrss_feed_categories AS c2
1143 WHERE c2.parent_cat = ttrss_feed_categories.id) AS num_children
8d505d78
AD
1144 FROM ttrss_feed_categories, ttrss_cat_counters_cache
1145 WHERE ttrss_cat_counters_cache.feed_id = id AND
fc9de939 1146 ttrss_cat_counters_cache.owner_uid = ttrss_feed_categories.owner_uid AND
31375163 1147 ttrss_feed_categories.owner_uid = " . $_SESSION["uid"]);
a9cb1f83
AD
1148
1149 while ($line = db_fetch_assoc($result)) {
22fdebff 1150 $line["cat_id"] = (int) $line["cat_id"];
8a4c759e 1151
2c5f231e 1152 if ($line["num_children"] > 0) {
99c9e91a 1153 $child_counter = getCategoryChildrenUnread($link, $line["cat_id"], $_SESSION["uid"]);
2c5f231e
AD
1154 } else {
1155 $child_counter = 0;
1156 }
1157
8acc449c 1158 $cv = array("id" => $line["cat_id"], "kind" => "cat",
0ef32f48 1159 "counter" => $line["unread"] + $child_counter);
6a7817c1
AD
1160
1161 array_push($ret_arr, $cv);
a9cb1f83 1162 }
d232a40f
AD
1163
1164 /* Special case: NULL category doesn't actually exist in the DB */
1165
9798b2b4 1166 $cv = array("id" => 0, "kind" => "cat",
12e6de72 1167 "counter" => (int) ccache_find($link, 0, $_SESSION["uid"], true));
d232a40f 1168
6a7817c1
AD
1169 array_push($ret_arr, $cv);
1170
1171 return $ret_arr;
a9cb1f83
AD
1172 }
1173
2c5f231e 1174 // only accepts real cats (>= 0)
99c9e91a 1175 function getCategoryChildrenUnread($link, $cat, $owner_uid = false) {
2c5f231e
AD
1176 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
1177
1178 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE parent_cat = '$cat'
1179 AND owner_uid = $owner_uid");
1180
1181 $unread = 0;
1182
1183 while ($line = db_fetch_assoc($result)) {
1184 $unread += getCategoryUnread($link, $line["id"], $owner_uid);
99c9e91a 1185 $unread += getCategoryChildrenUnread($link, $line["id"], $owner_uid);
2c5f231e
AD
1186 }
1187
1188 return $unread;
1189 }
1190
b6d486a3
AD
1191 function getCategoryUnread($link, $cat, $owner_uid = false) {
1192
1193 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
f295c368 1194
bba7c4bf 1195 if ($cat >= 0) {
18664970 1196
bba7c4bf
AD
1197 if ($cat != 0) {
1198 $cat_query = "cat_id = '$cat'";
1199 } else {
1200 $cat_query = "cat_id IS NULL";
1201 }
14073c0a 1202
8d505d78 1203 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
b6d486a3 1204 AND owner_uid = " . $owner_uid);
8d505d78 1205
bba7c4bf
AD
1206 $cat_feeds = array();
1207 while ($line = db_fetch_assoc($result)) {
1208 array_push($cat_feeds, "feed_id = " . $line["id"]);
1209 }
8d505d78 1210
bba7c4bf 1211 if (count($cat_feeds) == 0) return 0;
8d505d78 1212
bba7c4bf 1213 $match_part = implode(" OR ", $cat_feeds);
8d505d78
AD
1214
1215 $result = db_query($link, "SELECT COUNT(int_id) AS unread
687bb90d
AD
1216 FROM ttrss_user_entries
1217 WHERE unread = true AND ($match_part)
1218 AND owner_uid = " . $owner_uid);
8d505d78 1219
bba7c4bf 1220 $unread = 0;
8d505d78 1221
bba7c4bf
AD
1222 # this needs to be rewritten
1223 while ($line = db_fetch_assoc($result)) {
1224 $unread += $line["unread"];
1225 }
8d505d78 1226
bba7c4bf
AD
1227 return $unread;
1228 } else if ($cat == -1) {
59e15af4 1229 return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3) + getFeedUnread($link, 0);
bba7c4bf 1230 } else if ($cat == -2) {
f295c368 1231
b2531a28 1232 $result = db_query($link, "
8d505d78 1233 SELECT COUNT(unread) AS unread FROM
687bb90d
AD
1234 ttrss_user_entries, ttrss_user_labels2
1235 WHERE article_id = ref_id AND unread = true
b2531a28 1236 AND ttrss_user_entries.owner_uid = '$owner_uid'");
ceb30ba4 1237
b2531a28 1238 $unread = db_fetch_result($result, 0, "unread");
f295c368 1239
b2531a28 1240 return $unread;
f295c368 1241
8d505d78 1242 }
f295c368
AD
1243 }
1244
1245 function getFeedUnread($link, $feed, $is_cat = false) {
2627f2d0 1246 return getFeedArticles($link, $feed, $is_cat, true, $_SESSION["uid"]);
bdb7369b
AD
1247 }
1248
ceb30ba4
AD
1249 function getLabelUnread($link, $label_id, $owner_uid = false) {
1250 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
1251
f360b028
AD
1252 $result = db_query($link, "SELECT COUNT(ref_id) AS unread FROM ttrss_user_entries, ttrss_user_labels2
1253 WHERE owner_uid = '$owner_uid' AND unread = true AND label_id = '$label_id' AND article_id = ref_id");
ceb30ba4
AD
1254
1255 if (db_num_rows($result) != 0) {
1256 return db_fetch_result($result, 0, "unread");
1257 } else {
1258 return 0;
1259 }
1260 }
1261
2627f2d0
AD
1262 function getFeedArticles($link, $feed, $is_cat = false, $unread_only = false,
1263 $owner_uid = false) {
1264
22fdebff 1265 $n_feed = (int) $feed;
687bb90d 1266 $need_entries = false;
f295c368 1267
2627f2d0
AD
1268 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
1269
bdb7369b
AD
1270 if ($unread_only) {
1271 $unread_qpart = "unread = true";
1272 } else {
1273 $unread_qpart = "true";
1274 }
1275
f295c368 1276 if ($is_cat) {
8d505d78 1277 return getCategoryUnread($link, $n_feed, $owner_uid);
5417fbd7
AD
1278 } else if ($n_feed == -6) {
1279 return 0;
1280 } else if ($feed != "0" && $n_feed == 0) {
326469fc 1281
3972bf59 1282 $feed = db_escape_string($link, $feed);
c5701e70 1283
326469fc 1284 $result = db_query($link, "SELECT SUM((SELECT COUNT(int_id)
8d505d78 1285 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
687bb90d 1286 AND ref_id = id AND $unread_qpart)) AS count FROM ttrss_tags
326469fc
AD
1287 WHERE owner_uid = $owner_uid AND tag_name = '$feed'");
1288 return db_fetch_result($result, 0, "count");
1289
f295c368 1290 } else if ($n_feed == -1) {
a9cb1f83 1291 $match_part = "marked = true";
e4f4b46f
AD
1292 } else if ($n_feed == -2) {
1293 $match_part = "published = true";
2d24f032 1294 } else if ($n_feed == -3) {
cd2cc43d 1295 $match_part = "unread = true AND score >= 0";
2d24f032 1296
b71e188e 1297 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
c1d7e6c3 1298
2d24f032 1299 if (DB_TYPE == "pgsql") {
8d505d78 1300 $match_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
2d24f032 1301 } else {
7608b38a 1302 $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2d24f032 1303 }
687bb90d
AD
1304
1305 $need_entries = true;
1306
b2531a28
AD
1307 } else if ($n_feed == -4) {
1308 $match_part = "true";
e04c18a2 1309 } else if ($n_feed >= 0) {
831ff047 1310
6e63a7c3
AD
1311 if ($n_feed != 0) {
1312 $match_part = "feed_id = '$n_feed'";
831ff047 1313 } else {
6e63a7c3 1314 $match_part = "feed_id IS NULL";
831ff047 1315 }
6e63a7c3 1316
f822a8e5 1317 } else if ($feed < LABEL_BASE_INDEX) {
318260cc 1318
f822a8e5 1319 $label_id = feed_to_label_id($feed);
a9cb1f83 1320
ceb30ba4 1321 return getLabelUnread($link, $label_id, $owner_uid);
a9cb1f83 1322
a9cb1f83
AD
1323 }
1324
1325 if ($match_part) {
e04c18a2 1326
687bb90d 1327 if ($need_entries) {
e04c18a2 1328 $from_qpart = "ttrss_user_entries,ttrss_entries";
687bb90d
AD
1329 $from_where = "ttrss_entries.id = ttrss_user_entries.ref_id AND";
1330 } else {
1331 $from_qpart = "ttrss_user_entries";
e04c18a2
AD
1332 }
1333
8d505d78 1334 $query = "SELECT count(int_id) AS unread
e04c18a2 1335 FROM $from_qpart WHERE
687bb90d
AD
1336 $unread_qpart AND $from_where ($match_part) AND ttrss_user_entries.owner_uid = $owner_uid";
1337
1338 //echo "[$feed/$query]\n";
dbfc4365
AD
1339
1340 $result = db_query($link, $query);
8d505d78 1341
a9cb1f83 1342 } else {
8d505d78 1343
a9cb1f83 1344 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
8d505d78
AD
1345 FROM ttrss_tags,ttrss_user_entries,ttrss_entries
1346 WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id
687bb90d 1347 AND $unread_qpart AND ttrss_tags.owner_uid = " . $owner_uid);
a9cb1f83 1348 }
8d505d78 1349
a9cb1f83 1350 $unread = db_fetch_result($result, 0, "unread");
cfb02131 1351
a9cb1f83
AD
1352 return $unread;
1353 }
1354
f3acc32e
AD
1355 function getGlobalUnread($link, $user_id = false) {
1356
1357 if (!$user_id) {
1358 $user_id = $_SESSION["uid"];
1359 }
1360
8a4c759e
AD
1361 $result = db_query($link, "SELECT SUM(value) AS c_id FROM ttrss_counters_cache
1362 WHERE owner_uid = '$user_id' AND feed_id > 0");
1363
8d505d78 1364 $c_id = db_fetch_result($result, 0, "c_id");
8a4c759e 1365
a9cb1f83
AD
1366 return $c_id;
1367 }
1368
1369 function getGlobalCounters($link, $global_unread = -1) {
6a7817c1
AD
1370 $ret_arr = array();
1371
8d505d78 1372 if ($global_unread == -1) {
a9cb1f83
AD
1373 $global_unread = getGlobalUnread($link);
1374 }
6a7817c1 1375
8d505d78 1376 $cv = array("id" => "global-unread",
12e6de72 1377 "counter" => (int) $global_unread);
6a7817c1
AD
1378
1379 array_push($ret_arr, $cv);
7bf7e4d3 1380
8d505d78 1381 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
7bf7e4d3
AD
1382 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
1383
1384 $subscribed_feeds = db_fetch_result($result, 0, "fn");
1385
8d505d78 1386 $cv = array("id" => "subscribed-feeds",
12e6de72 1387 "counter" => (int) $subscribed_feeds);
7bf7e4d3 1388
6a7817c1
AD
1389 array_push($ret_arr, $cv);
1390
1391 return $ret_arr;
a9cb1f83
AD
1392 }
1393
6a7817c1 1394 function getVirtCounters($link) {
a9cb1f83 1395
ef393de7 1396 $ret_arr = array();
bdb7369b 1397
e04c18a2 1398 for ($i = 0; $i >= -4; $i--) {
bdb7369b 1399
ceb30ba4 1400 $count = getFeedUnread($link, $i);
6a7817c1
AD
1401
1402 $cv = array("id" => $i,
12e6de72 1403 "counter" => (int) $count);
8d505d78 1404
296c8134
AD
1405// if (get_pref($link, 'EXTENDED_FEEDLIST'))
1406// $cv["xmsg"] = getFeedArticles($link, $i)." ".__("total");
bdb7369b 1407
6a7817c1 1408 array_push($ret_arr, $cv);
8d505d78 1409 }
0a6e5382 1410
a413f53e
AD
1411 global $pluginhost;
1412
1413 if ($pluginhost) {
1414 $feeds = $pluginhost->get_feeds(-1);
1415
0196ef94
AD
1416 if (is_array($feeds)) {
1417 foreach ($feeds as $feed) {
1418 $cv = array("id" => PluginHost::pfeed_to_feed_id($feed['id']),
1419 "counter" => $feed['sender']->get_unread($feed['id']));
a413f53e 1420
0196ef94
AD
1421 array_push($ret_arr, $cv);
1422 }
a413f53e 1423 }
a413f53e
AD
1424 }
1425
0a6e5382
AD
1426 return $ret_arr;
1427 }
1428
11232703 1429 function getLabelCounters($link, $descriptions = false) {
6a7817c1
AD
1430
1431 $ret_arr = array();
0a6e5382 1432
3809b278 1433 $owner_uid = $_SESSION["uid"];
bdb7369b 1434
45942238
AD
1435 $result = db_query($link, "SELECT id,caption,COUNT(unread) AS unread
1436 FROM ttrss_labels2 LEFT JOIN ttrss_user_labels2 ON
1437 (ttrss_labels2.id = label_id)
1438 LEFT JOIN ttrss_user_entries ON (ref_id = article_id AND unread = true)
123a7643
AD
1439 WHERE ttrss_labels2.owner_uid = $owner_uid GROUP BY ttrss_labels2.id,
1440 ttrss_labels2.caption");
8d505d78 1441
3809b278 1442 while ($line = db_fetch_assoc($result)) {
2d24f032 1443
f822a8e5 1444 $id = label_to_feed_id($line["id"]);
e4f4b46f 1445
3809b278 1446 $label_name = $line["caption"];
45942238 1447 $count = $line["unread"];
3809b278 1448
6a7817c1 1449 $cv = array("id" => $id,
12e6de72 1450 "counter" => (int) $count);
11232703
AD
1451
1452 if ($descriptions)
1453 $cv["description"] = $label_name;
a9cb1f83 1454
296c8134
AD
1455// if (get_pref($link, 'EXTENDED_FEEDLIST'))
1456// $cv["xmsg"] = getFeedArticles($link, $id)." ".__("total");
ef393de7 1457
6a7817c1 1458 array_push($ret_arr, $cv);
3809b278 1459 }
8d505d78 1460
ef393de7 1461 return $ret_arr;
a9cb1f83
AD
1462 }
1463
3809b278 1464 function getFeedCounters($link, $active_feed = false) {
a9cb1f83 1465
6a7817c1
AD
1466 $ret_arr = array();
1467
41759103 1468 $query = "SELECT ttrss_feeds.id,
8a4c759e 1469 ttrss_feeds.title,
8d505d78 1470 ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated,
de0a2122
AD
1471 last_error, value AS count
1472 FROM ttrss_feeds, ttrss_counters_cache
8d505d78 1473 WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
fc9de939 1474 AND ttrss_counters_cache.owner_uid = ttrss_feeds.owner_uid
55e01d7e 1475 AND ttrss_counters_cache.feed_id = id";
a9cb1f83 1476
14073c0a 1477 $result = db_query($link, $query);
a9cb1f83
AD
1478 $fctrs_modified = false;
1479
1480 while ($line = db_fetch_assoc($result)) {
8d505d78 1481
a9cb1f83 1482 $id = $line["id"];
de0a2122 1483 $count = $line["count"];
a9cb1f83 1484 $last_error = htmlspecialchars($line["last_error"]);
fb1fb4ab 1485
324944f3 1486 $last_updated = make_local_datetime($link, $line['last_updated'], false);
fb1fb4ab 1487
7defa089 1488 $has_img = feed_has_icon($id);
a9cb1f83 1489
428b704d
AD
1490 if (date('Y') - date('Y', strtotime($line['last_updated'])) > 2)
1491 $last_updated = '';
1492
6a7817c1 1493 $cv = array("id" => $id,
21884958 1494 "updated" => $last_updated,
12e6de72 1495 "counter" => (int) $count,
6a7817c1 1496 "has_img" => (int) $has_img);
a9cb1f83 1497
6a7817c1
AD
1498 if ($last_error)
1499 $cv["error"] = $last_error;
4ffa126e 1500
296c8134
AD
1501// if (get_pref($link, 'EXTENDED_FEEDLIST'))
1502// $cv["xmsg"] = getFeedArticles($link, $id)." ".__("total");
bdb7369b 1503
6a7817c1 1504 if ($active_feed && $id == $active_feed)
fbc95c5b 1505 $cv["title"] = truncate_string($line["title"], 30);
6a7817c1
AD
1506
1507 array_push($ret_arr, $cv);
a9cb1f83 1508
a9cb1f83 1509 }
6a7817c1
AD
1510
1511 return $ret_arr;
a9cb1f83
AD
1512 }
1513
6e7f8d26
AD
1514 function get_pgsql_version($link) {
1515 $result = db_query($link, "SELECT version() AS version");
9949bd15 1516 $version = explode(" ", db_fetch_result($result, 0, "version"));
6e7f8d26
AD
1517 return $version[1];
1518 }
1519
2b8290cd 1520 /**
23d2471c
AD
1521 * @return array (code => Status code, message => error message if available)
1522 *
2b8290cd
CW
1523 * 0 - OK, Feed already exists
1524 * 1 - OK, Feed added
1525 * 2 - Invalid URL
9a8ce956
CW
1526 * 3 - URL content is HTML, no feeds available
1527 * 4 - URL content is HTML which contains multiple feeds.
1528 * Here you should call extractfeedurls in rpc-backend
1529 * to get all possible feeds.
5414ad4c 1530 * 5 - Couldn't download the URL content.
2b8290cd 1531 */
8d505d78 1532 function subscribe_to_feed($link, $url, $cat_id = 0,
efc6553d 1533 $auth_login = '', $auth_pass = '') {
bb0f29a4 1534
23d2471c
AD
1535 global $fetch_last_error;
1536
2c08214a
AD
1537 require_once "include/rssfuncs.php";
1538
f0266f51 1539 $url = fix_url($url);
ec39a02c 1540
23d2471c 1541 if (!$url || !validate_feed_url($url)) return array("code" => 2);
a5819bb3 1542
759e5132
AD
1543 $contents = @fetch_file_contents($url, false, $auth_login, $auth_pass);
1544
1545 if (!$contents) {
304aadb9 1546 return array("code" => 5, "message" => $fetch_last_error);
759e5132
AD
1547 }
1548
1549 if (is_html($contents)) {
1550 $feedUrls = get_feeds_from_html($url, $contents);
304aadb9 1551
304aadb9
AD
1552 if (count($feedUrls) == 0) {
1553 return array("code" => 3);
1554 } else if (count($feedUrls) > 1) {
759e5132 1555 return array("code" => 4, "feeds" => $feedUrls);
f6d8345b 1556 }
304aadb9
AD
1557 //use feed url as new URL
1558 $url = key($feedUrls);
1559 }
f6d8345b 1560
956c7629
AD
1561 if ($cat_id == "0" || !$cat_id) {
1562 $cat_qpart = "NULL";
1563 } else {
1564 $cat_qpart = "'$cat_id'";
1565 }
8d505d78 1566
956c7629 1567 $result = db_query($link,
8d505d78 1568 "SELECT id FROM ttrss_feeds
a5819bb3 1569 WHERE feed_url = '$url' AND owner_uid = ".$_SESSION["uid"]);
8d505d78 1570
956c7629 1571 if (db_num_rows($result) == 0) {
956c7629 1572 $result = db_query($link,
8d505d78
AD
1573 "INSERT INTO ttrss_feeds
1574 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass,update_method)
1575 VALUES ('".$_SESSION["uid"]."', '$url',
19b3992b 1576 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass', 0)");
8d505d78 1577
956c7629 1578 $result = db_query($link,
8d505d78 1579 "SELECT id FROM ttrss_feeds WHERE feed_url = '$url'
f27de515 1580 AND owner_uid = " . $_SESSION["uid"]);
8d505d78 1581
956c7629 1582 $feed_id = db_fetch_result($result, 0, "id");
8d505d78 1583
956c7629 1584 if ($feed_id) {
c633e370 1585 update_rss_feed($link, $feed_id, true);
956c7629
AD
1586 }
1587
23d2471c 1588 return array("code" => 1);
956c7629 1589 } else {
23d2471c 1590 return array("code" => 0);
956c7629
AD
1591 }
1592 }
1593
8d505d78 1594 function print_feed_select($link, $id, $default_id = "",
4c9d0490
AD
1595 $attributes = "", $include_all_feeds = true,
1596 $root_id = false, $nest_level = 0) {
1597
1598 if (!$root_id) {
1599 print "<select id=\"$id\" name=\"$id\" $attributes>";
1600 if ($include_all_feeds) {
1601 $is_selected = ("0" == $default_id) ? "selected=\"1\"" : "";
1602 print "<option $is_selected value=\"0\">".__('All feeds')."</option>";
1603 }
673d54ca 1604 }
8d505d78 1605
4c9d0490 1606 if (get_pref($link, 'ENABLE_FEED_CATS')) {
673d54ca 1607
4c9d0490
AD
1608 if ($root_id)
1609 $parent_qpart = "parent_cat = '$root_id'";
1610 else
1611 $parent_qpart = "parent_cat IS NULL";
673d54ca 1612
4c9d0490
AD
1613 $result = db_query($link, "SELECT id,title,
1614 (SELECT COUNT(id) FROM ttrss_feed_categories AS c2 WHERE
1615 c2.parent_cat = ttrss_feed_categories.id) AS num_children
1616 FROM ttrss_feed_categories
1617 WHERE owner_uid = ".$_SESSION["uid"]." AND $parent_qpart ORDER BY title");
1618
1619 while ($line = db_fetch_assoc($result)) {
1620
1621 for ($i = 0; $i < $nest_level; $i++)
1622 $line["title"] = " - " . $line["title"];
1623
1624 $is_selected = ("CAT:".$line["id"] == $default_id) ? "selected=\"1\"" : "";
1625
1626 printf("<option $is_selected value='CAT:%d'>%s</option>",
1627 $line["id"], htmlspecialchars($line["title"]));
1628
1629 if ($line["num_children"] > 0)
1630 print_feed_select($link, $id, $default_id, $attributes,
1631 $include_all_feeds, $line["id"], $nest_level+1);
1632
1633 $feed_result = db_query($link, "SELECT id,title FROM ttrss_feeds
1634 WHERE cat_id = '".$line["id"]."' AND owner_uid = ".$_SESSION["uid"] . " ORDER BY title");
1635
1636 while ($fline = db_fetch_assoc($feed_result)) {
1637 $is_selected = ($fline["id"] == $default_id) ? "selected=\"1\"" : "";
1638
1639 $fline["title"] = " + " . $fline["title"];
1640
1641 for ($i = 0; $i < $nest_level; $i++)
1642 $fline["title"] = " - " . $fline["title"];
1643
1644 printf("<option $is_selected value='%d'>%s</option>",
1645 $fline["id"], htmlspecialchars($fline["title"]));
1646 }
673d54ca 1647 }
b1710666 1648
4c9d0490
AD
1649 if (!$root_id) {
1650 $is_selected = ($default_id == "CAT:0") ? "selected=\"1\"" : "";
1651
1652 printf("<option $is_selected value='CAT:0'>%s</option>",
1653 __("Uncategorized"));
1654
1655 $feed_result = db_query($link, "SELECT id,title FROM ttrss_feeds
1656 WHERE cat_id IS NULL AND owner_uid = ".$_SESSION["uid"] . " ORDER BY title");
1657
1658 while ($fline = db_fetch_assoc($feed_result)) {
1659 $is_selected = ($fline["id"] == $default_id && !$default_is_cat) ? "selected=\"1\"" : "";
1660
1661 $fline["title"] = " + " . $fline["title"];
1662
1663 for ($i = 0; $i < $nest_level; $i++)
1664 $fline["title"] = " - " . $fline["title"];
1665
1666 printf("<option $is_selected value='%d'>%s</option>",
1667 $fline["id"], htmlspecialchars($fline["title"]));
1668 }
1669 }
b1710666 1670
4c9d0490
AD
1671 } else {
1672 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
1673 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1674
1675 while ($line = db_fetch_assoc($result)) {
1676
1677 $is_selected = ($line["id"] == $default_id) ? "selected=\"1\"" : "";
1678
1679 printf("<option $is_selected value='%d'>%s</option>",
1680 $line["id"], htmlspecialchars($line["title"]));
1681 }
673d54ca 1682 }
8d505d78 1683
4c9d0490
AD
1684 if (!$root_id) {
1685 print "</select>";
1686 }
673d54ca
AD
1687 }
1688
fbf85cf6
AD
1689 function print_feed_cat_select($link, $id, $default_id,
1690 $attributes, $include_all_cats = true, $root_id = false, $nest_level = 0) {
8d505d78 1691
fbf85cf6
AD
1692 if (!$root_id) {
1693 print "<select id=\"$id\" name=\"$id\" default=\"$default_id\" onchange=\"catSelectOnChange(this)\" $attributes>";
1694 }
673d54ca 1695
fbf85cf6
AD
1696 if ($root_id)
1697 $parent_qpart = "parent_cat = '$root_id'";
1698 else
1699 $parent_qpart = "parent_cat IS NULL";
673d54ca 1700
fbf85cf6
AD
1701 $result = db_query($link, "SELECT id,title,
1702 (SELECT COUNT(id) FROM ttrss_feed_categories AS c2 WHERE
1703 c2.parent_cat = ttrss_feed_categories.id) AS num_children
1704 FROM ttrss_feed_categories
1705 WHERE owner_uid = ".$_SESSION["uid"]." AND $parent_qpart ORDER BY title");
673d54ca 1706
fbf85cf6
AD
1707 while ($line = db_fetch_assoc($result)) {
1708 if ($line["id"] == $default_id) {
1709 $is_selected = "selected=\"1\"";
1710 } else {
1711 $is_selected = "";
1712 }
673d54ca 1713
fbf85cf6
AD
1714 for ($i = 0; $i < $nest_level; $i++)
1715 $line["title"] = " - " . $line["title"];
c00907f2 1716
fbf85cf6
AD
1717 if ($line["title"])
1718 printf("<option $is_selected value='%d'>%s</option>",
1719 $line["id"], htmlspecialchars($line["title"]));
673d54ca 1720
fbf85cf6
AD
1721 if ($line["num_children"] > 0)
1722 print_feed_cat_select($link, $id, $default_id, $attributes,
1723 $include_all_cats, $line["id"], $nest_level+1);
1724 }
5c7c7da9 1725
fbf85cf6
AD
1726 if (!$root_id) {
1727 if ($include_all_cats) {
1728 if (db_num_rows($result) > 0) {
1729 print "<option disabled=\"1\">--------</option>";
1730 }
7e18f8e7
AD
1731
1732 if ($default_id == 0) {
1733 $is_selected = "selected=\"1\"";
1734 } else {
1735 $is_selected = "";
1736 }
1737
1738 print "<option $is_selected value=\"0\">".__('Uncategorized')."</option>";
fbf85cf6
AD
1739 }
1740 print "</select>";
1741 }
1742 }
8d505d78 1743
14f69488
AD
1744 function checkbox_to_sql_bool($val) {
1745 return ($val == "on") ? "true" : "false";
1746 }
86b682ce
AD
1747
1748 function getFeedCatTitle($link, $id) {
1749 if ($id == -1) {
d1db26aa 1750 return __("Special");
f822a8e5 1751 } else if ($id < LABEL_BASE_INDEX) {
d1db26aa 1752 return __("Labels");
86b682ce 1753 } else if ($id > 0) {
8d505d78 1754 $result = db_query($link, "SELECT ttrss_feed_categories.title
86b682ce
AD
1755 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
1756 cat_id = ttrss_feed_categories.id");
1757 if (db_num_rows($result) == 1) {
1758 return db_fetch_result($result, 0, "title");
1759 } else {
d1db26aa 1760 return __("Uncategorized");
86b682ce
AD
1761 }
1762 } else {
1763 return "getFeedCatTitle($id) failed";
1764 }
1765
1766 }
1767
9299102f 1768 function getFeedIcon($id) {
af88c48a 1769 switch ($id) {
4bee8b5f
AD
1770 case 0:
1771 return "images/archive.png";
1772 break;
af88c48a 1773 case -1:
c2167866 1774 return "images/mark_set.svg";
af88c48a
AD
1775 break;
1776 case -2:
c2167866 1777 return "images/pub_set.svg";
af88c48a
AD
1778 break;
1779 case -3:
1780 return "images/fresh.png";
1781 break;
1782 case -4:
1783 return "images/tag.png";
1784 break;
5417fbd7
AD
1785 case -6:
1786 return "images/recently_read.png";
1787 break;
af88c48a 1788 default:
f822a8e5 1789 if ($id < LABEL_BASE_INDEX) {
4bee8b5f
AD
1790 return "images/label.png";
1791 } else {
8d505d78 1792 if (file_exists(ICONS_DIR . "/$id.ico"))
e2eda979 1793 return ICONS_URL . "/$id.ico";
4bee8b5f 1794 }
af88c48a
AD
1795 break;
1796 }
1797 }
1798
fd994f1a
AD
1799 function getFeedTitle($link, $id, $cat = false) {
1800 if ($cat) {
8add44ec 1801 return getCategoryTitle($link, $id);
fd994f1a 1802 } else if ($id == -1) {
d1db26aa 1803 return __("Starred articles");
945c243e
AD
1804 } else if ($id == -2) {
1805 return __("Published articles");
2d24f032
AD
1806 } else if ($id == -3) {
1807 return __("Fresh articles");
b2531a28
AD
1808 } else if ($id == -4) {
1809 return __("All articles");
80db1113 1810 } else if ($id === 0 || $id === "0") {
e04c18a2 1811 return __("Archived articles");
5417fbd7
AD
1812 } else if ($id == -6) {
1813 return __("Recently read");
f822a8e5
AD
1814 } else if ($id < LABEL_BASE_INDEX) {
1815 $label_id = feed_to_label_id($id);
ceb30ba4 1816 $result = db_query($link, "SELECT caption FROM ttrss_labels2 WHERE id = '$label_id'");
86b682ce 1817 if (db_num_rows($result) == 1) {
ceb30ba4 1818 return db_fetch_result($result, 0, "caption");
86b682ce
AD
1819 } else {
1820 return "Unknown label ($label_id)";
1821 }
1822
147f5632 1823 } else if (is_numeric($id) && $id > 0) {
86b682ce
AD
1824 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
1825 if (db_num_rows($result) == 1) {
1826 return db_fetch_result($result, 0, "title");
1827 } else {
1828 return "Unknown feed ($id)";
1829 }
1830 } else {
22fdebff 1831 return $id;
86b682ce 1832 }
86b682ce 1833 }
3dd46f19 1834
d8221301 1835 function make_init_params($link) {
f1f3a642 1836 $params = array();
c9268ed5 1837
f1f3a642
AD
1838 foreach (array("ON_CATCHUP_SHOW_NEXT_FEED", "HIDE_READ_FEEDS",
1839 "ENABLE_FEED_CATS", "FEEDS_SORT_BY_UNREAD", "CONFIRM_FEED_CATCHUP",
7d12b6c8 1840 "CDM_AUTO_CATCHUP", "FRESH_ARTICLE_MAX_AGE", "DEFAULT_ARTICLE_LIMIT",
30b6ee8c 1841 "HIDE_READ_SHOWS_SPECIAL", "COMBINED_DISPLAY_MODE") as $param) {
40496720 1842
c4f7ba80 1843 $params[strtolower($param)] = (int) get_pref($link, $param);
f1f3a642 1844 }
40496720 1845
c4f7ba80
AD
1846 $params["icons_url"] = ICONS_URL;
1847 $params["cookie_lifetime"] = SESSION_COOKIE_LIFETIME;
1848 $params["default_view_mode"] = get_pref($link, "_DEFAULT_VIEW_MODE");
1849 $params["default_view_limit"] = (int) get_pref($link, "_DEFAULT_VIEW_LIMIT");
1850 $params["default_view_order_by"] = get_pref($link, "_DEFAULT_VIEW_ORDER_BY");
c4f7ba80 1851 $params["bw_limit"] = (int) $_SESSION["bw_limit"];
88a41b64 1852 $params["label_base_index"] = (int) LABEL_BASE_INDEX;
59b223d7 1853
8cd576a1 1854 $result = db_query($link, "SELECT MAX(id) AS mid, COUNT(*) AS nf FROM
9b7ecc0a
AD
1855 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
1856
8cd576a1
AD
1857 $max_feed_id = db_fetch_result($result, 0, "mid");
1858 $num_feeds = db_fetch_result($result, 0, "nf");
9b7ecc0a 1859
8cd576a1 1860 $params["max_feed_id"] = (int) $max_feed_id;
c4f7ba80 1861 $params["num_feeds"] = (int) $num_feeds;
8cd576a1 1862
c4f7ba80 1863 $params["collapsed_feedlist"] = (int) get_pref($link, "_COLLAPSED_FEEDLIST");
b8cb4d08 1864 $params["hotkeys"] = get_hotkeys_map($link);
9b7ecc0a 1865
8484ce22 1866 $params["csrf_token"] = $_SESSION["csrf_token"];
f03701fe 1867 $params["widescreen"] = (int) $_COOKIE["ttrss_widescreen"];
8484ce22 1868
6b1a4ecd 1869 $params['simple_update'] = defined('SIMPLE_UPDATE_MODE') && SIMPLE_UPDATE_MODE;
8b83bf5f 1870
d8221301 1871 return $params;
3ac2b520 1872 }
f54f515f 1873
b8cb4d08
AD
1874 function get_hotkeys_info($link) {
1875 $hotkeys = array(
1876 __("Navigation") => array(
1877 "next_feed" => __("Open next feed"),
1878 "prev_feed" => __("Open previous feed"),
1879 "next_article" => __("Open next article"),
1880 "prev_article" => __("Open previous article"),
c22580b5
AD
1881 "next_article_noscroll" => __("Open next article (don't scroll long articles)"),
1882 "prev_article_noscroll" => __("Open previous article (don't scroll long articles)"),
b8cb4d08
AD
1883 "search_dialog" => __("Show search dialog")),
1884 __("Article") => array(
1885 "toggle_mark" => __("Toggle starred"),
1886 "toggle_publ" => __("Toggle published"),
1887 "toggle_unread" => __("Toggle unread"),
1888 "edit_tags" => __("Edit tags"),
1889 "dismiss_selected" => __("Dismiss selected"),
1890 "dismiss_read" => __("Dismiss read"),
1891 "open_in_new_window" => __("Open in new window"),
1892 "catchup_below" => __("Mark below as read"),
1893 "catchup_above" => __("Mark above as read"),
1894 "article_scroll_down" => __("Scroll down"),
1895 "article_scroll_up" => __("Scroll up"),
1896 "select_article_cursor" => __("Select article under cursor"),
1bcf8f45 1897 "email_article" => __("Email article"),
414191d4 1898 "close_article" => __("Close/collapse article"),
2ccc7b8e
AD
1899 "toggle_widescreen" => __("Toggle widescreen mode"),
1900 "toggle_embed_original" => __("Toggle embed original")),
b8cb4d08
AD
1901 __("Article selection") => array(
1902 "select_all" => __("Select all articles"),
1903 "select_unread" => __("Select unread"),
1904 "select_marked" => __("Select starred"),
1905 "select_published" => __("Select published"),
1906 "select_invert" => __("Invert selection"),
1907 "select_none" => __("Deselect everything")),
1908 __("Feed") => array(
1909 "feed_refresh" => __("Refresh current feed"),
1910 "feed_unhide_read" => __("Un/hide read feeds"),
1911 "feed_subscribe" => __("Subscribe to feed"),
1912 "feed_edit" => __("Edit feed"),
1913 "feed_catchup" => __("Mark as read"),
1914 "feed_reverse" => __("Reverse headlines"),
43f775de 1915 "feed_debug_update" => __("Debug feed update"),
b8cb4d08 1916 "catchup_all" => __("Mark all feeds as read"),
4b27f0c0 1917 "cat_toggle_collapse" => __("Un/collapse current category"),
60b88b25
AD
1918 "toggle_combined_mode" => __("Toggle combined mode"),
1919 "toggle_cdm_expanded" => __("Toggle auto expand in combined mode")),
b8cb4d08
AD
1920 __("Go to") => array(
1921 "goto_all" => __("All articles"),
1922 "goto_fresh" => __("Fresh"),
1923 "goto_marked" => __("Starred"),
1924 "goto_published" => __("Published"),
1925 "goto_tagcloud" => __("Tag cloud"),
1926 "goto_prefs" => __("Preferences")),
1927 __("Other") => array(
1928 "create_label" => __("Create label"),
1929 "create_filter" => __("Create filter"),
1930 "collapse_sidebar" => __("Un/collapse sidebar"),
1931 "help_dialog" => __("Show help dialog"))
1932 );
1933
1934 return $hotkeys;
1935 }
1936
1937 function get_hotkeys_map($link) {
a83b58f1 1938 $hotkeys = array(
e218c5f5
AD
1939// "navigation" => array(
1940 "k" => "next_feed",
1941 "j" => "prev_feed",
1942 "n" => "next_article",
1943 "p" => "prev_article",
e5e2cf3b
AD
1944 "(38)|up" => "prev_article",
1945 "(40)|down" => "next_article",
da15c140
AD
1946// "^(38)|Ctrl-up" => "prev_article_noscroll",
1947// "^(40)|Ctrl-down" => "next_article_noscroll",
e5e2cf3b 1948 "(191)|/" => "search_dialog",
e218c5f5
AD
1949// "article" => array(
1950 "s" => "toggle_mark",
5b18c936 1951 "*s" => "toggle_publ",
e218c5f5 1952 "u" => "toggle_unread",
5b18c936
AD
1953 "*t" => "edit_tags",
1954 "*d" => "dismiss_selected",
1955 "*x" => "dismiss_read",
e218c5f5
AD
1956 "o" => "open_in_new_window",
1957 "c p" => "catchup_below",
1958 "c n" => "catchup_above",
5b18c936
AD
1959 "*n" => "article_scroll_down",
1960 "*p" => "article_scroll_up",
d2db81a5
AD
1961 "*(38)|Shift+up" => "article_scroll_up",
1962 "*(40)|Shift+down" => "article_scroll_down",
5b18c936 1963 "a *w" => "toggle_widescreen",
2ccc7b8e 1964 "a e" => "toggle_embed_original",
e218c5f5 1965 "e" => "email_article",
2cda4314 1966 "a q" => "close_article",
e218c5f5
AD
1967// "article_selection" => array(
1968 "a a" => "select_all",
1969 "a u" => "select_unread",
5b18c936 1970 "a *u" => "select_marked",
e218c5f5
AD
1971 "a p" => "select_published",
1972 "a i" => "select_invert",
1973 "a n" => "select_none",
1974// "feed" => array(
1975 "f r" => "feed_refresh",
1976 "f a" => "feed_unhide_read",
1977 "f s" => "feed_subscribe",
1978 "f e" => "feed_edit",
1979 "f q" => "feed_catchup",
1980 "f x" => "feed_reverse",
5b18c936
AD
1981 "f *d" => "feed_debug_update",
1982 "f *c" => "toggle_combined_mode",
60b88b25 1983 "f c" => "toggle_cdm_expanded",
5b18c936 1984 "*q" => "catchup_all",
e218c5f5
AD
1985 "x" => "cat_toggle_collapse",
1986// "goto" => array(
1987 "g a" => "goto_all",
1988 "g f" => "goto_fresh",
1989 "g s" => "goto_marked",
1990 "g p" => "goto_published",
1991 "g t" => "goto_tagcloud",
5b18c936 1992 "g *p" => "goto_prefs",
e218c5f5 1993// "other" => array(
3fb40112 1994 "(9)|Tab" => "select_article_cursor", // tab
e218c5f5
AD
1995 "c l" => "create_label",
1996 "c f" => "create_filter",
1997 "c s" => "collapse_sidebar",
3fb40112 1998 "^(191)|Ctrl+/" => "help_dialog",
a83b58f1
AD
1999 );
2000
da15c140
AD
2001 if (get_pref($link, 'COMBINED_DISPLAY_MODE')) {
2002 $hotkeys["^(38)|Ctrl-up"] = "prev_article_noscroll";
2003 $hotkeys["^(40)|Ctrl-down"] = "next_article_noscroll";
2004 }
2005
e218c5f5
AD
2006 global $pluginhost;
2007 foreach ($pluginhost->get_hooks($pluginhost::HOOK_HOTKEY_MAP) as $plugin) {
2008 $hotkeys = $plugin->hook_hotkey_map($hotkeys);
2009 }
2010
2011 $prefixes = array();
2012
2013 foreach (array_keys($hotkeys) as $hotkey) {
2014 $pair = explode(" ", $hotkey, 2);
2015
2016 if (count($pair) > 1 && !in_array($pair[0], $prefixes)) {
2017 array_push($prefixes, $pair[0]);
2018 }
2019 }
2020
2021 return array($prefixes, $hotkeys);
a83b58f1
AD
2022 }
2023
c4f7ba80 2024 function make_runtime_info($link) {
8cd576a1
AD
2025 $data = array();
2026
2027 $result = db_query($link, "SELECT MAX(id) AS mid, COUNT(*) AS nf FROM
9b7ecc0a
AD
2028 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
2029
8cd576a1
AD
2030 $max_feed_id = db_fetch_result($result, 0, "mid");
2031 $num_feeds = db_fetch_result($result, 0, "nf");
9b7ecc0a 2032
8cd576a1
AD
2033 $data["max_feed_id"] = (int) $max_feed_id;
2034 $data["num_feeds"] = (int) $num_feeds;
c4f7ba80 2035
f8fb4498 2036 $data['last_article_id'] = getLastArticleId($link);
5ae8f858 2037 $data['cdm_expanded'] = get_pref($link, 'CDM_EXPANDED');
f8fb4498 2038
16314dda 2039 $data['dep_ts'] = calculate_dep_timestamp();
4cdb8173 2040 $data['reload_on_ts_change'] = !defined('_NO_RELOAD_ON_TS_CHANGE');
16314dda 2041
dbaa4e4a 2042 if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) {
c4f7ba80
AD
2043
2044 $data['daemon_is_running'] = (int) file_is_locked("update_daemon.lock");
8e00ae9b 2045
9041f58b 2046 if (time() - $_SESSION["daemon_stamp_check"] > 30) {
8e00ae9b 2047
fb074239 2048 $stamp = (int) @file_get_contents(LOCK_DIRECTORY . "/update_daemon.stamp");
fbae93d8 2049
8e00ae9b 2050 if ($stamp) {
9041f58b
AD
2051 $stamp_delta = time() - $stamp;
2052
2053 if ($stamp_delta > 1800) {
f6854e44 2054 $stamp_check = 0;
8e00ae9b 2055 } else {
f6854e44
AD
2056 $stamp_check = 1;
2057 $_SESSION["daemon_stamp_check"] = time();
8e00ae9b
AD
2058 }
2059
c4f7ba80 2060 $data['daemon_stamp_ok'] = $stamp_check;
f6854e44 2061
8e00ae9b
AD
2062 $stamp_fmt = date("Y.m.d, G:i", $stamp);
2063
c4f7ba80 2064 $data['daemon_stamp'] = $stamp_fmt;
8e00ae9b 2065 }
8e00ae9b 2066 }
71ad883b 2067 }
8e00ae9b 2068
63855db1 2069 if ($_SESSION["last_version_check"] + 86400 + rand(-1000, 1000) < time()) {
fb074239 2070 $new_version_details = @check_for_update($link);
d9fa39f1 2071
63855db1 2072 $data['new_version_available'] = (int) ($new_version_details != false);
d9fa39f1
AD
2073
2074 $_SESSION["last_version_check"] = time();
27211afe 2075 $_SESSION["version_data"] = $new_version_details;
d9fa39f1
AD
2076 }
2077
c4f7ba80 2078 return $data;
f54f515f 2079 }
ef393de7 2080
ca5d9be4 2081 function search_to_sql($link, $search) {
ef393de7 2082
88040f57 2083 $search_query_part = "";
e20c9d88 2084
9949bd15 2085 $keywords = explode(" ", $search);
88040f57 2086 $query_keywords = array();
e20c9d88 2087
ab4b768f
AD
2088 foreach ($keywords as $k) {
2089 if (strpos($k, "-") === 0) {
2090 $k = substr($k, 1);
2091 $not = "NOT";
2092 } else {
2093 $not = "";
88040f57 2094 }
e20c9d88 2095
9949bd15 2096 $commandpair = explode(":", mb_strtolower($k), 2);
53003548
AD
2097
2098 if ($commandpair[0] == "note" && $commandpair[1]) {
2099
2100 if ($commandpair[1] == "true")
2101 array_push($query_keywords, "($not (note IS NOT NULL AND note != ''))");
2102 else
2103 array_push($query_keywords, "($not (note IS NULL OR note = ''))");
2104
2105 } else if ($commandpair[0] == "star" && $commandpair[1]) {
2106
2107 if ($commandpair[1] == "true")
2108 array_push($query_keywords, "($not (marked = true))");
2109 else
2110 array_push($query_keywords, "($not (marked = false))");
2111
2112 } else if ($commandpair[0] == "pub" && $commandpair[1]) {
2113
2114 if ($commandpair[1] == "true")
2115 array_push($query_keywords, "($not (published = true))");
2116 else
2117 array_push($query_keywords, "($not (published = false))");
2118
2119 } else if (strpos($k, "@") === 0) {
e20c9d88 2120
ab4b768f
AD
2121 $user_tz_string = get_pref($link, 'USER_TIMEZONE', $_SESSION['uid']);
2122 $orig_ts = strtotime(substr($k, 1));
ab4b768f 2123 $k = date("Y-m-d", convert_timestamp($orig_ts, $user_tz_string, 'UTC'));
8d505d78 2124
53003548
AD
2125 //$k = date("Y-m-d", strtotime(substr($k, 1)));
2126
ab4b768f 2127 array_push($query_keywords, "(".SUBSTRING_FOR_DATE."(updated,1,LENGTH('$k')) $not = '$k')");
ca5d9be4 2128 } else {
ab4b768f
AD
2129 array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
2130 OR UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
88040f57
AD
2131 }
2132 }
2133
2134 $search_query_part = implode("AND", $query_keywords);
2135
2136 return $search_query_part;
2137 }
2138
67bd0b1f
AD
2139 function getParentCategories($link, $cat, $owner_uid) {
2140 $rv = array();
2141
2142 $result = db_query($link, "SELECT parent_cat FROM ttrss_feed_categories
2143 WHERE id = '$cat' AND parent_cat IS NOT NULL AND owner_uid = $owner_uid");
2144
2145 while ($line = db_fetch_assoc($result)) {
2146 array_push($rv, $line["parent_cat"]);
2147 $rv = array_merge($rv, getParentCategories($link, $line["parent_cat"], $owner_uid));
2148 }
2149
2150 return $rv;
2151 }
2152
6d8d00e8
AD
2153 function getChildCategories($link, $cat, $owner_uid) {
2154 $rv = array();
2155
2156 $result = db_query($link, "SELECT id FROM ttrss_feed_categories
2157 WHERE parent_cat = '$cat' AND owner_uid = $owner_uid");
2158
2159 while ($line = db_fetch_assoc($result)) {
2160 array_push($rv, $line["id"]);
2161 $rv = array_merge($rv, getChildCategories($link, $line["id"], $owner_uid));
2162 }
2163
2164 return $rv;
2165 }
147f5632 2166
ca5d9be4 2167 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $override_order = false, $offset = 0, $owner_uid = 0, $filter = false, $since_id = 0, $include_children = false, $ignore_vfeed_group = false) {
c36bf4d5
AD
2168
2169 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
c1a0b534 2170
c3fddd05
AD
2171 $ext_tables_part = "";
2172
88040f57 2173 if ($search) {
e4f7f8df
AD
2174
2175 if (SPHINX_ENABLED) {
2176 $ids = join(",", @sphinx_search($search, 0, 500));
2177
8d505d78 2178 if ($ids)
e4f7f8df
AD
2179 $search_query_part = "ref_id IN ($ids) AND ";
2180 else
2181 $search_query_part = "ref_id = -1 AND ";
2182
2183 } else {
ca5d9be4 2184 $search_query_part = search_to_sql($link, $search);
e4f7f8df 2185 $search_query_part .= " AND ";
8d505d78 2186 }
e20c9d88 2187
ef393de7
AD
2188 } else {
2189 $search_query_part = "";
2190 }
2191
36184020 2192 if ($filter) {
4e02f582
AD
2193
2194 if (DB_TYPE == "pgsql") {
2195 $query_strategy_part .= " AND updated > NOW() - INTERVAL '14 days' ";
2196 } else {
2197 $query_strategy_part .= " AND updated > DATE_SUB(NOW(), INTERVAL 14 DAY) ";
2198 }
2199
2200 $override_order = "updated DESC";
2201
2680295b 2202 $filter_query_part = filter_to_sql($link, $filter, $owner_uid);
dd8c36af
AD
2203
2204 // Try to check if SQL regexp implementation chokes on a valid regexp
809c8e62 2205 $result = db_query($link, "SELECT true AS true_val FROM ttrss_entries,
2680295b 2206 ttrss_user_entries, ttrss_feeds, ttrss_feed_categories
dd8c36af
AD
2207 WHERE $filter_query_part LIMIT 1", false);
2208
7726063c
AD
2209 if ($result) {
2210 $test = db_fetch_result($result, 0, "true_val");
dd8c36af 2211
7726063c
AD
2212 if (!$test) {
2213 $filter_query_part = "false AND";
2214 } else {
2215 $filter_query_part .= " AND";
2216 }
dd8c36af 2217 } else {
7726063c 2218 $filter_query_part = "false AND";
dd8c36af
AD
2219 }
2220
36184020
AD
2221 } else {
2222 $filter_query_part = "";
2223 }
2224
97e5dbb2
AD
2225 if ($since_id) {
2226 $since_id_part = "ttrss_entries.id > $since_id AND ";
2227 } else {
2228 $since_id_part = "";
2229 }
2230
ef393de7 2231 $view_query_part = "";
8d505d78 2232
9b523c01 2233 if ($view_mode == "adaptive") {
ef393de7
AD
2234 if ($search) {
2235 $view_query_part = " ";
2236 } else if ($feed != -1) {
6d8d00e8 2237
a6adb136 2238 $unread = getFeedUnread($link, $feed, $cat_view);
f4a2f12a 2239
a6adb136
AD
2240 if ($cat_view && $feed > 0 && $include_children)
2241 $unread += getCategoryChildrenUnread($link, $feed);
f4a2f12a 2242
a6adb136
AD
2243 if ($unread > 0)
2244 $view_query_part = " unread = true AND ";
f4a2f12a 2245
ef393de7
AD
2246 }
2247 }
8d505d78 2248
ef393de7
AD
2249 if ($view_mode == "marked") {
2250 $view_query_part = " marked = true AND ";
2251 }
23d72f39 2252
127aaaa0
AD
2253 if ($view_mode == "has_note") {
2254 $view_query_part = " (note IS NOT NULL AND note != '') AND ";
2255 }
2256
23d72f39
AD
2257 if ($view_mode == "published") {
2258 $view_query_part = " published = true AND ";
2259 }
2260
0bf65987 2261 if ($view_mode == "unread" && $feed != -6) {
ef393de7
AD
2262 $view_query_part = " unread = true AND ";
2263 }
8b09eac8 2264
ef393de7
AD
2265 if ($limit > 0) {
2266 $limit_query_part = "LIMIT " . $limit;
8d505d78 2267 }
ef393de7 2268
8361e724
AD
2269 $allow_archived = false;
2270
ef393de7 2271 $vfeed_query_part = "";
8d505d78 2272
ef393de7
AD
2273 // override query strategy and enable feed display when searching globally
2274 if ($search && $search_mode == "all_feeds") {
7032f2a5 2275 $query_strategy_part = "true";
8d505d78 2276 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
22fdebff 2277 /* tags */
75c648cf 2278 } else if (!is_numeric($feed)) {
7032f2a5 2279 $query_strategy_part = "true";
ef393de7
AD
2280 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2281 id = feed_id) as feed_title,";
7032f2a5 2282 } else if ($search && $search_mode == "this_cat") {
8d505d78 2283 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
0a6c4846 2284
7032f2a5
AD
2285 if ($feed > 0) {
2286 if ($include_children) {
2287 $subcats = getChildCategories($link, $feed, $owner_uid);
2288 array_push($subcats, $feed);
2289 $cats_qpart = join(",", $subcats);
2290 } else {
2291 $cats_qpart = $feed;
ef393de7 2292 }
8d505d78 2293
7032f2a5 2294 $query_strategy_part = "ttrss_feeds.cat_id IN ($cats_qpart)";
8d505d78 2295
ef393de7 2296 } else {
7032f2a5 2297 $query_strategy_part = "ttrss_feeds.cat_id IS NULL";
ef393de7 2298 }
8d505d78 2299
e04c18a2 2300 } else if ($feed > 0) {
8d505d78 2301
ef393de7 2302 if ($cat_view) {
5c365f60 2303
ef393de7 2304 if ($feed > 0) {
09101297
AD
2305 if ($include_children) {
2306 # sub-cats
2307 $subcats = getChildCategories($link, $feed, $owner_uid);
2308
7032f2a5
AD
2309 array_push($subcats, $feed);
2310 $query_strategy_part = "cat_id IN (".
09101297 2311 implode(",", $subcats).")";
7032f2a5 2312
6d8d00e8 2313 } else {
09101297 2314 $query_strategy_part = "cat_id = '$feed'";
6d8d00e8
AD
2315 }
2316
ef393de7
AD
2317 } else {
2318 $query_strategy_part = "cat_id IS NULL";
2319 }
8d505d78 2320
ef393de7 2321 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
5c365f60 2322
8d505d78 2323 } else {
6e63a7c3 2324 $query_strategy_part = "feed_id = '$feed'";
ef393de7 2325 }
bfe5ddfc 2326 } else if ($feed == 0 && !$cat_view) { // archive virtual feed
e04c18a2 2327 $query_strategy_part = "feed_id IS NULL";
8361e724 2328 $allow_archived = true;
bfe5ddfc 2329 } else if ($feed == 0 && $cat_view) { // uncategorized
65dd90f2 2330 $query_strategy_part = "cat_id IS NULL AND feed_id IS NOT NULL";
bfe5ddfc 2331 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
ef393de7
AD
2332 } else if ($feed == -1) { // starred virtual feed
2333 $query_strategy_part = "marked = true";
2334 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
835fb294
AD
2335 $allow_archived = true;
2336
1bd7413f 2337 if (!$override_order) {
b9a06a0e 2338 $override_order = "last_marked DESC, date_entered DESC, updated DESC";
1bd7413f 2339 }
7873d588 2340
e6a38cde
AD
2341 } else if ($feed == -2) { // published virtual feed OR labels category
2342
2343 if (!$cat_view) {
2344 $query_strategy_part = "published = true";
2345 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
8361e724 2346 $allow_archived = true;
46b78149 2347
1bd7413f 2348 if (!$override_order) {
b9a06a0e 2349 $override_order = "last_published DESC, date_entered DESC, updated DESC";
1bd7413f
AD
2350 }
2351
e6a38cde
AD
2352 } else {
2353 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2354
2355 $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2";
8d505d78 2356
e6a38cde
AD
2357 $query_strategy_part = "ttrss_labels2.id = ttrss_user_labels2.label_id AND
2358 ttrss_user_labels2.article_id = ref_id";
2359
2360 }
5417fbd7 2361 } else if ($feed == -6) { // recently read
5089b30b 2362 $query_strategy_part = "unread = false AND last_read IS NOT NULL";
5417fbd7 2363 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
835fb294 2364 $allow_archived = true;
46b78149
AD
2365
2366 if (!$override_order) $override_order = "last_read DESC";
2d24f032 2367 } else if ($feed == -3) { // fresh virtual feed
cd2cc43d 2368 $query_strategy_part = "unread = true AND score >= 0";
2d24f032 2369
7a22dc2a 2370 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
c1d7e6c3 2371
2d24f032 2372 if (DB_TYPE == "pgsql") {
be574731 2373 $query_strategy_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
2d24f032 2374 } else {
be574731 2375 $query_strategy_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2d24f032
AD
2376 }
2377
b2531a28
AD
2378 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2379 } else if ($feed == -4) { // all articles virtual feed
2380 $query_strategy_part = "true";
e4f4b46f 2381 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
f822a8e5
AD
2382 } else if ($feed <= LABEL_BASE_INDEX) { // labels
2383 $label_id = feed_to_label_id($feed);
3de0261a 2384
ceb30ba4
AD
2385 $query_strategy_part = "label_id = '$label_id' AND
2386 ttrss_labels2.id = ttrss_user_labels2.label_id AND
2387 ttrss_user_labels2.article_id = ref_id";
3de0261a 2388
ef393de7 2389 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
ceb30ba4 2390 $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2";
835fb294 2391 $allow_archived = true;
8d505d78 2392
ef393de7 2393 } else {
835fb294 2394 $query_strategy_part = "true";
ef393de7 2395 }
d6e5706d 2396
b3990c92
AD
2397 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
2398 $date_sort_field = "updated";
2399 } else {
2400 $date_sort_field = "date_entered";
2401 }
2402
b9a06a0e 2403 $order_by = "$date_sort_field DESC, updated DESC";
e939722a 2404
2e4faaac 2405 if ($view_mode == "unread_first") {
434bf856 2406 $order_by = "unread DESC, $order_by";
2e4faaac
AD
2407 }
2408
e939722a
AD
2409 if ($override_order) {
2410 $order_by = $override_order;
2411 }
8d505d78 2412
ef393de7
AD
2413 $feed_title = "";
2414
22fdebff 2415 if ($search) {
7032f2a5 2416 $feed_title = T_sprintf("Search results: %s", $search);
22fdebff 2417 } else {
ef393de7 2418 if ($cat_view) {
22fdebff 2419 $feed_title = getCategoryTitle($link, $feed);
ef393de7 2420 } else {
147f5632 2421 if (is_numeric($feed) && $feed > 0) {
8d505d78 2422 $result = db_query($link, "SELECT title,site_url,last_error
22fdebff 2423 FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = $owner_uid");
8d505d78 2424
22fdebff
AD
2425 $feed_title = db_fetch_result($result, 0, "title");
2426 $feed_site_url = db_fetch_result($result, 0, "site_url");
2427 $last_error = db_fetch_result($result, 0, "last_error");
2428 } else {
2429 $feed_title = getFeedTitle($link, $feed);
8d505d78 2430 }
88040f57 2431 }
ef393de7
AD
2432 }
2433
87764a50 2434 $content_query_part = "content as content_preview, cached_content, ";
62129e67 2435
75c648cf 2436 if (is_numeric($feed)) {
8d505d78 2437
ef393de7
AD
2438 if ($feed >= 0) {
2439 $feed_kind = "Feeds";
2440 } else {
2441 $feed_kind = "Labels";
2442 }
8d505d78 2443
95a82c08
AD
2444 if ($limit_query_part) {
2445 $offset_query_part = "OFFSET $offset";
2446 }
2447
7fdf8eca 2448 // proper override_order applied above
6b3f228f 2449 if ($vfeed_query_part && !$ignore_vfeed_group && get_pref($link, 'VFEED_GROUP_BY_FEED', $owner_uid)) {
6cfea5c7 2450 if (!$override_order) {
8d505d78 2451 $order_by = "ttrss_feeds.title, $order_by";
7fdf8eca
AD
2452 } else {
2453 $order_by = "ttrss_feeds.title, $override_order";
43fc671f 2454 }
6cfea5c7
AD
2455 }
2456
8361e724 2457 if (!$allow_archived) {
e04c18a2 2458 $from_qpart = "ttrss_entries,ttrss_user_entries,ttrss_feeds$ext_tables_part";
117335bf 2459 $feed_check_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
e04c18a2
AD
2460
2461 } else {
835fb294 2462 $from_qpart = "ttrss_entries$ext_tables_part,ttrss_user_entries
e04c18a2
AD
2463 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)";
2464 }
2465
8d505d78 2466 $query = "SELECT DISTINCT
f9b2d27c 2467 date_entered,
1f64b1be 2468 guid,
ef393de7 2469 ttrss_entries.id,ttrss_entries.title,
46921916 2470 updated,
9c506873
AD
2471 label_cache,
2472 tag_cache,
c0644ee4 2473 always_display_enclosures,
d1fc2f92 2474 site_url,
c7e51de1 2475 note,
13992673
AD
2476 num_comments,
2477 comments,
db16ae50 2478 int_id,
bfd61d3f 2479 hide_images,
494a64ea 2480 unread,feed_id,marked,published,link,last_read,orig_feed_id,
7873d588 2481 last_marked, last_published,
ef393de7
AD
2482 $vfeed_query_part
2483 $content_query_part
ff6e357a 2484 author,score
ef393de7 2485 FROM
e04c18a2 2486 $from_qpart
ef393de7 2487 WHERE
e04c18a2 2488 $feed_check_qpart
ef393de7 2489 ttrss_user_entries.ref_id = ttrss_entries.id AND
c36bf4d5 2490 ttrss_user_entries.owner_uid = '$owner_uid' AND
ef393de7 2491 $search_query_part
36184020 2492 $filter_query_part
ef393de7 2493 $view_query_part
97e5dbb2 2494 $since_id_part
ef393de7 2495 $query_strategy_part ORDER BY $order_by
95a82c08 2496 $limit_query_part $offset_query_part";
4bc311fc 2497
b4e75b2a 2498 if ($_REQUEST["debug"]) print $query;
4bc311fc
AD
2499
2500 $result = db_query($link, $query);
8d505d78 2501
ef393de7
AD
2502 } else {
2503 // browsing by tag
8d505d78 2504
147f5632
CM
2505 $select_qpart = "SELECT DISTINCT " .
2506 "date_entered," .
2507 "guid," .
2508 "note," .
2509 "ttrss_entries.id as id," .
2510 "title," .
2511 "updated," .
2512 "unread," .
2513 "feed_id," .
2514 "orig_feed_id," .
2515 "marked," .
d1fc2f92
AD
2516 "num_comments, " .
2517 "comments, " .
c0644ee4
AD
2518 "tag_cache," .
2519 "label_cache," .
147f5632
CM
2520 "link," .
2521 "last_read," .
94a567df 2522 "(SELECT hide_images FROM ttrss_feeds WHERE id = feed_id) AS hide_images," .
7873d588 2523 "last_marked, last_published, " .
97e5dbb2 2524 $since_id_part .
147f5632
CM
2525 $vfeed_query_part .
2526 $content_query_part .
147f5632
CM
2527 "score ";
2528
ef393de7 2529 $feed_kind = "Tags";
147f5632
CM
2530 $all_tags = explode(",", $feed);
2531 if ($search_mode == 'any') {
2532 $tag_sql = "tag_name in (" . implode(", ", array_map("db_quote", $all_tags)) . ")";
2533 $from_qpart = " FROM ttrss_entries,ttrss_user_entries,ttrss_tags ";
2534 $where_qpart = " WHERE " .
2535 "ref_id = ttrss_entries.id AND " .
2536 "ttrss_user_entries.owner_uid = $owner_uid AND " .
2537 "post_int_id = int_id AND $tag_sql AND " .
2538 $view_query_part .
2539 $search_query_part .
2540 $query_strategy_part . " ORDER BY $order_by " .
2541 $limit_query_part;
8d505d78 2542
147f5632
CM
2543 } else {
2544 $i = 1;
2545 $sub_selects = array();
2546 $sub_ands = array();
2547 foreach ($all_tags as $term) {
2548 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");
2549 $i++;
2550 }
2551 if ($i > 2) {
2552 $x = 1;
2553 $y = 2;
2554 do {
2555 array_push($sub_ands, "A$x.post_int_id = A$y.post_int_id");
2556 $x++;
2557 $y++;
2558 } while ($y < $i);
2559 }
2560 array_push($sub_ands, "A1.post_int_id = ttrss_user_entries.int_id and ttrss_user_entries.owner_uid = $owner_uid");
2561 array_push($sub_ands, "ttrss_user_entries.ref_id = ttrss_entries.id");
2562 $from_qpart = " FROM " . implode(", ", $sub_selects) . ", ttrss_user_entries, ttrss_entries";
2563 $where_qpart = " WHERE " . implode(" AND ", $sub_ands);
2564 }
2565 // error_log("TAG SQL: " . $tag_sql);
2566 // $tag_sql = "tag_name = '$feed'"; DEFAULT way
2567
2568 // error_log("[". $select_qpart . "][" . $from_qpart . "][" .$where_qpart . "]");
2569 $result = db_query($link, $select_qpart . $from_qpart . $where_qpart);
ef393de7
AD
2570 }
2571
c7188969 2572 return array($result, $feed_title, $feed_site_url, $last_error);
8d505d78 2573
ef393de7
AD
2574 }
2575
bfd61d3f 2576 function sanitize($link, $str, $force_remove_images = false, $owner = false, $site_url = false) {
ceb0cab5
AD
2577 if (!$owner) $owner = $_SESSION["uid"];
2578
96811a55
AD
2579 $res = trim($str); if (!$res) return '';
2580
46137483
AD
2581 if (strpos($res, "href=") === false)
2582 $res = rewrite_urls($res);
533c0ea6 2583
8cc3c778
AD
2584 $charset_hack = '<head>
2585 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
2586 </head>';
2587
96811a55
AD
2588 $res = trim($res); if (!$res) return '';
2589
8cc3c778
AD
2590 libxml_use_internal_errors(true);
2591
2592 $doc = new DOMDocument();
2593 $doc->loadHTML($charset_hack . $res);
2594 $xpath = new DOMXPath($doc);
8d505d78 2595
8cc3c778
AD
2596 $entries = $xpath->query('(//a[@href]|//img[@src])');
2597
2598 foreach ($entries as $entry) {
2599
2600 if ($site_url) {
2601
2602 if ($entry->hasAttribute('href'))
2603 $entry->setAttribute('href',
2604 rewrite_relative_url($site_url, $entry->getAttribute('href')));
8d505d78 2605
f0bd8e65
AD
2606 if ($entry->hasAttribute('src')) {
2607 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
2608
2609 $cached_filename = CACHE_DIR . '/images/' . sha1($src) . '.png';
2610
2611 if (file_exists($cached_filename)) {
2612 $src = SELF_URL_PATH . '/image.php?hash=' . sha1($src);
2613 }
2614
2615 $entry->setAttribute('src', $src);
2616 }
bfd61d3f
AD
2617
2618 if ($entry->nodeName == 'img') {
f0540b59 2619 if (($owner && get_pref($link, "STRIP_IMAGES", $owner)) ||
ba79634c 2620 $force_remove_images || $_SESSION["bw_limit"]) {
bfd61d3f
AD
2621
2622 $p = $doc->createElement('p');
2623
2624 $a = $doc->createElement('a');
2625 $a->setAttribute('href', $entry->getAttribute('src'));
2626
2627 $a->appendChild(new DOMText($entry->getAttribute('src')));
2628 $a->setAttribute('target', '_blank');
2629
2630 $p->appendChild($a);
2631
2632 $entry->parentNode->replaceChild($p, $entry);
2633 }
2634 }
8cc3c778
AD
2635 }
2636
fa403733 2637 if (strtolower($entry->nodeName) == "a") {
c401d5c9 2638 $entry->setAttribute("target", "_blank");
fa403733 2639 }
8dccabed 2640 }
8d505d78 2641
254a3f56
AD
2642 $entries = $xpath->query('//iframe');
2643 foreach ($entries as $entry) {
4e404802
AD
2644 $entry->setAttribute('sandbox', 'allow-scripts');
2645
254a3f56 2646 }
8dccabed 2647
b70ccfe6
FE
2648 $allowed_elements = array('a', 'address', 'audio', 'article',
2649 'b', 'big', 'blockquote', 'body', 'br', 'cite', 'center',
2650 'code', 'dd', 'del', 'details', 'div', 'dl', 'font',
2651 'dt', 'em', 'footer', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
2652 'header', 'html', 'i', 'img', 'ins', 'kbd',
eb734e52 2653 'li', 'nav', 'noscript', 'ol', 'p', 'pre', 'q', 's','small',
b70ccfe6
FE
2654 'source', 'span', 'strike', 'strong', 'sub', 'summary',
2655 'sup', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead',
2656 'tr', 'track', 'tt', 'u', 'ul', 'var', 'wbr', 'video' );
2657
2658 if ($_SESSION['hasSandbox']) $allowed_elements[] = 'iframe';
2659
2660 $disallowed_attributes = array('id', 'style', 'class');
2661
e9b86f0a
AD
2662 global $pluginhost;
2663
2664 if (isset($pluginhost)) {
2665 foreach ($pluginhost->get_hooks($pluginhost::HOOK_SANITIZE) as $plugin) {
b70ccfe6
FE
2666 $retval = $plugin->hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes);
2667 if (is_array($retval)) {
2668 $doc = $retval[0];
2669 $allowed_elements = $retval[1];
2670 $disallowed_attributes = $retval[2];
2671 } else {
2672 $doc = $retval;
2673 }
e9b86f0a
AD
2674 }
2675 }
2676
be124dc2 2677 $doc->removeChild($doc->firstChild); //remove doctype
b70ccfe6 2678 $doc = strip_harmful_tags($doc, $allowed_elements, $disallowed_attributes);
be124dc2 2679 $res = $doc->saveHTML();
254a3f56
AD
2680 return $res;
2681 }
16ad9085 2682
b70ccfe6 2683 function strip_harmful_tags($doc, $allowed_elements, $disallowed_attributes) {
254a3f56 2684 $entries = $doc->getElementsByTagName("*");
16ad9085 2685
254a3f56
AD
2686 foreach ($entries as $entry) {
2687 if (!in_array($entry->nodeName, $allowed_elements)) {
2688 $entry->parentNode->removeChild($entry);
2689 }
2690
2691 if ($entry->hasAttributes()) {
5f0081b0
AD
2692 $attrs_to_remove = array();
2693
2694 foreach ($entry->attributes as $attr) {
254a3f56
AD
2695
2696 if (strpos($attr->nodeName, 'on') === 0) {
5f0081b0 2697 array_push($attrs_to_remove, $attr);
254a3f56
AD
2698 }
2699
2700 if (in_array($attr->nodeName, $disallowed_attributes)) {
5f0081b0 2701 array_push($attrs_to_remove, $attr);
254a3f56
AD
2702 }
2703 }
5f0081b0
AD
2704
2705 foreach ($attrs_to_remove as $attr) {
2706 $entry->removeAttributeNode($attr);
2707 }
254a3f56
AD
2708 }
2709 }
2710
2711 return $doc;
183ad07b 2712 }
b72c3ef8 2713
73495fd1 2714 function check_for_update($link) {
63855db1 2715 if (CHECK_FOR_NEW_VERSION && $_SESSION['access_level'] >= 10) {
f6064662
AD
2716 $version_url = "http://tt-rss.org/version.php?ver=" . VERSION .
2717 "&iid=" . sha1(SELF_URL_PATH);
b72c3ef8 2718
63855db1 2719 $version_data = @fetch_file_contents($version_url);
b72c3ef8 2720
63855db1
AD
2721 if ($version_data) {
2722 $version_data = json_decode($version_data, true);
8d505d78 2723 if ($version_data && $version_data['version']) {
f67d9754 2724
63855db1 2725 if (version_compare(VERSION, $version_data['version']) == -1) {
e91ad1e9 2726 return $version_data;
63855db1
AD
2727 }
2728 }
f67d9754 2729 }
b72c3ef8 2730 }
63855db1 2731 return false;
b72c3ef8 2732 }
472782e8 2733
9968d46f
AD
2734 function catchupArticlesById($link, $ids, $cmode, $owner_uid = false) {
2735
2736 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
ed41f171 2737 if (count($ids) == 0) return;
472782e8
AD
2738
2739 $tmp_ids = array();
2740
2741 foreach ($ids as $id) {
2742 array_push($tmp_ids, "ref_id = '$id'");
2743 }
2744
2745 $ids_qpart = join(" OR ", $tmp_ids);
2746
2747 if ($cmode == 0) {
8d505d78 2748 db_query($link, "UPDATE ttrss_user_entries SET
472782e8 2749 unread = false,last_read = NOW()
9968d46f 2750 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8 2751 } else if ($cmode == 1) {
8d505d78 2752 db_query($link, "UPDATE ttrss_user_entries SET
472782e8 2753 unread = true
9968d46f 2754 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8 2755 } else {
8d505d78 2756 db_query($link, "UPDATE ttrss_user_entries SET
472782e8 2757 unread = NOT unread,last_read = NOW()
9968d46f 2758 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8 2759 }
0737b95a
AD
2760
2761 /* update ccache */
2762
2763 $result = db_query($link, "SELECT DISTINCT feed_id FROM ttrss_user_entries
2764 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
2765
2766 while ($line = db_fetch_assoc($result)) {
2767 ccache_update($link, $line["feed_id"], $owner_uid);
2768 }
472782e8
AD
2769 }
2770
ca5133cb 2771 function get_article_tags($link, $id, $owner_uid = 0, $tag_cache = false) {
0b126ac2 2772
3972bf59 2773 $a_id = db_escape_string($link, $id);
0b126ac2 2774
bc976a8c
AD
2775 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
2776
8d505d78 2777 $query = "SELECT DISTINCT tag_name,
0c3d1c68 2778 owner_uid as owner FROM
0b126ac2 2779 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
bd3f2ade 2780 ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name";
0b126ac2 2781
bd3f2ade 2782 $obj_id = md5("TAGS:$owner_uid:$id");
8d505d78 2783 $tags = array();
bd3f2ade 2784
0e4a7d7a 2785 /* check cache first */
490c366d 2786
0e4a7d7a
AD
2787 if ($tag_cache === false) {
2788 $result = db_query($link, "SELECT tag_cache FROM ttrss_user_entries
2789 WHERE ref_id = '$id' AND owner_uid = $owner_uid");
490c366d 2790
0e4a7d7a
AD
2791 $tag_cache = db_fetch_result($result, 0, "tag_cache");
2792 }
bd3f2ade 2793
0e4a7d7a
AD
2794 if ($tag_cache) {
2795 $tags = explode(",", $tag_cache);
2796 } else {
490c366d 2797
0e4a7d7a 2798 /* do it the hard way */
490c366d 2799
0e4a7d7a 2800 $tmp_result = db_query($link, $query);
490c366d 2801
0e4a7d7a
AD
2802 while ($tmp_line = db_fetch_assoc($tmp_result)) {
2803 array_push($tags, $tmp_line["tag_name"]);
2804 }
490c366d 2805
0e4a7d7a 2806 /* update the cache */
490c366d 2807
3972bf59 2808 $tags_str = db_escape_string($link, join(",", $tags));
bd3f2ade 2809
0e4a7d7a
AD
2810 db_query($link, "UPDATE ttrss_user_entries
2811 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
2812 AND owner_uid = $owner_uid");
0b126ac2
AD
2813 }
2814
2815 return $tags;
2816 }
2817
d62a3b63
AD
2818 function trim_array($array) {
2819 $tmp = $array;
3415b075 2820 array_walk($tmp, 'trim');
d62a3b63
AD
2821 return $tmp;
2822 }
2823
be832a1a 2824 function tag_is_valid($tag) {
ef063748
AD
2825 if ($tag == '') return false;
2826 if (preg_match("/^[0-9]*$/", $tag)) return false;
41f7498a 2827 if (mb_strlen($tag) > 250) return false;
ef063748 2828
31365729
AD
2829 if (function_exists('iconv')) {
2830 $tag = iconv("utf-8", "utf-8", $tag);
2831 }
2832
ef063748
AD
2833 if (!$tag) return false;
2834
2835 return true;
be832a1a
AD
2836 }
2837
d98e76d9
AD
2838 function render_login_form($link) {
2839 require_once "login_form.php";
97acbaf1 2840 exit;
01a87dff
AD
2841 }
2842
dc56b3b7
AD
2843 // from http://developer.apple.com/internet/safari/faq.html
2844 function no_cache_incantation() {
2845 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
2846 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
2847 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
2848 header("Cache-Control: post-check=0, pre-check=0", false);
2849 header("Pragma: no-cache"); // HTTP/1.0
2850 }
2851
42395d28 2852 function format_warning($msg, $id = "") {
883fee8d 2853 global $link;
8d505d78 2854 return "<div class=\"warning\" id=\"$id\">
fcef9eea 2855 <img src=\"images/sign_excl.svg\"><div class='inner'>$msg</div></div>";
0d32b41e
AD
2856 }
2857
08ac193a 2858 function format_notice($msg, $id = "") {
883fee8d 2859 global $link;
8d505d78 2860 return "<div class=\"notice\" id=\"$id\">
fcef9eea 2861 <img src=\"images/sign_info.svg\"><div class='inner'>$msg</div></div>";
0d32b41e
AD
2862 }
2863
08ac193a 2864 function format_error($msg, $id = "") {
883fee8d 2865 global $link;
8d505d78 2866 return "<div class=\"error\" id=\"$id\">
fcef9eea 2867 <img src=\"images/sign_excl.svg\"><div class='inner'>$msg</div></div>";
68d2f95e
AD
2868 }
2869
4dccf1ed
AD
2870 function print_notice($msg) {
2871 return print format_notice($msg);
2872 }
2873
2874 function print_warning($msg) {
2875 return print format_warning($msg);
2876 }
2877
68d2f95e
AD
2878 function print_error($msg) {
2879 return print format_error($msg);
2880 }
2881
2882
4dccf1ed
AD
2883 function T_sprintf() {
2884 $args = func_get_args();
2885 return vsprintf(__(array_shift($args)), $args);
2886 }
2887
51682b23
AD
2888 function format_inline_player($link, $url, $ctype) {
2889
2890 $entry = "";
2891
44cd77b6
AD
2892 $url = htmlspecialchars($url);
2893
8d505d78 2894 if (strpos($ctype, "audio/") === 0) {
c3edc667
AD
2895
2896 if ($_SESSION["hasAudio"] && (strpos($ctype, "ogg") !== false ||
8d505d78 2897 strpos($_SERVER['HTTP_USER_AGENT'], "Chrome") !== false ||
c3edc667
AD
2898 strpos($_SERVER['HTTP_USER_AGENT'], "Safari") !== false )) {
2899
2900 $id = 'AUDIO-' . uniqid();
2901
cb081096 2902 $entry .= "<audio id=\"$id\"\" controls style='display : none'>
ca3bca99 2903 <source type=\"$ctype\" src=\"$url\"></source>
8d505d78 2904 </audio>";
c3edc667 2905
8d505d78 2906 $entry .= "<span onclick=\"player(this)\"
c3edc667
AD
2907 title=\"".__("Click to play")."\" status=\"0\"
2908 class=\"player\" audio-id=\"$id\">".__("Play")."</span>";
2909
2910 } else {
8d505d78
AD
2911
2912 $entry .= "<object type=\"application/x-shockwave-flash\"
ad95edc2 2913 data=\"lib/button/musicplayer.swf?song_url=$url\"
8d505d78
AD
2914 width=\"17\" height=\"17\" style='float : left; margin-right : 5px;'>
2915 <param name=\"movie\"
ad95edc2 2916 value=\"lib/button/musicplayer.swf?song_url=$url\" />
8d505d78 2917 </object>";
c3edc667 2918 }
ca3bca99 2919
44cd77b6
AD
2920 if ($entry) $entry .= "&nbsp; <a target=\"_blank\"
2921 href=\"$url\">" . basename($url) . "</a>";
ca3bca99
AD
2922
2923 return $entry;
2924
51682b23
AD
2925 }
2926
ca3bca99
AD
2927 return "";
2928
2929/* $filename = substr($url, strrpos($url, "/")+1);
c3edc667
AD
2930
2931 $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
ca3bca99 2932 $filename . " (" . $ctype . ")" . "</a>"; */
c3edc667 2933
51682b23
AD
2934 }
2935
64436e10 2936 function format_article($link, $id, $mark_as_read = true, $zoom_mode = false, $owner_uid = false) {
64436e10 2937 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
3de0261a 2938
009646d2
AD
2939 $rv = array();
2940
2941 $rv['id'] = $id;
2942
10eb9da8 2943 /* we can figure out feed_id from article id anyway, why do we
e04c18a2 2944 * pass feed_id here? let's ignore the argument :( */
10eb9da8
AD
2945
2946 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
2947 WHERE ref_id = '$id'");
2948
e04c18a2 2949 $feed_id = (int) db_fetch_result($result, 0, "feed_id");
10eb9da8 2950
009646d2
AD
2951 $rv['feed_id'] = $feed_id;
2952
2953 //if (!$zoom_mode) { print "<article id='$id'><![CDATA["; };
3de0261a 2954
3de0261a 2955 if ($mark_as_read) {
8d505d78
AD
2956 $result = db_query($link, "UPDATE ttrss_user_entries
2957 SET unread = false,last_read = NOW()
64436e10 2958 WHERE ref_id = '$id' AND owner_uid = $owner_uid");
8a4c759e 2959
64436e10 2960 ccache_update($link, $feed_id, $owner_uid);
3de0261a
AD
2961 }
2962
7252abe3 2963 $result = db_query($link, "SELECT id,title,link,content,feed_id,comments,int_id,
fc2b26a6 2964 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
8cc3c778 2965 (SELECT site_url FROM ttrss_feeds WHERE id = feed_id) as site_url,
33de3d37 2966 (SELECT hide_images FROM ttrss_feeds WHERE id = feed_id) as hide_images,
3de0261a 2967 num_comments,
9c506873 2968 tag_cache,
c7e51de1 2969 author,
ef83538d 2970 orig_feed_id,
87764a50
AD
2971 note,
2972 cached_content
3de0261a 2973 FROM ttrss_entries,ttrss_user_entries
64436e10 2974 WHERE id = '$id' AND ref_id = id AND owner_uid = $owner_uid");
3de0261a
AD
2975
2976 if ($result) {
2977
3de0261a
AD
2978 $line = db_fetch_assoc($result);
2979
84d952f1
AD
2980 $tag_cache = $line["tag_cache"];
2981
2982 $line["tags"] = get_article_tags($link, $id, $owner_uid, $line["tag_cache"]);
2983 unset($line["tag_cache"]);
2984
2985 $line["content"] = sanitize($link, $line["content"], false, $owner_uid, $line["site_url"]);
2986
2987 global $pluginhost;
2988
2989 foreach ($pluginhost->get_hooks($pluginhost::HOOK_RENDER_ARTICLE) as $p) {
2990 $line = $p->hook_render_article($line);
2991 }
8cc3c778 2992
3de0261a
AD
2993 $num_comments = $line["num_comments"];
2994 $entry_comments = "";
2995
2996 if ($num_comments > 0) {
2997 if ($line["comments"]) {
6e577ba1 2998 $comments_url = htmlspecialchars($line["comments"]);
3de0261a 2999 } else {
6e577ba1 3000 $comments_url = htmlspecialchars($line["link"]);
3de0261a 3001 }
7514749d 3002 $entry_comments = "<a target='_blank' href=\"$comments_url\">$num_comments comments</a>";
3de0261a
AD
3003 } else {
3004 if ($line["comments"] && $line["link"] != $line["comments"]) {
6e577ba1 3005 $entry_comments = "<a target='_blank' href=\"".htmlspecialchars($line["comments"])."\">comments</a>";
8d505d78 3006 }
3de0261a
AD
3007 }
3008
eedfb635
AD
3009 if ($zoom_mode) {
3010 header("Content-Type: text/html");
009646d2 3011 $rv['content'] .= "<html><head>
5bb0cc8e 3012 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
eedfb635
AD
3013 <title>Tiny Tiny RSS - ".$line["title"]."</title>
3014 <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss.css\">
b87a625c 3015 </head><body id=\"ttrssZoom\">";
eedfb635
AD
3016 }
3017
009646d2 3018 $rv['content'] .= "<div class=\"postReply\" id=\"POST-$id\">";
bc372fe3 3019
126e639a 3020 $rv['content'] .= "<div class=\"postHeader\" id=\"POSTHDR-$id\">";
3de0261a
AD
3021
3022 $entry_author = $line["author"];
3023
3024 if ($entry_author) {
60164936 3025 $entry_author = __(" - ") . $entry_author;
3de0261a
AD
3026 }
3027
8d505d78 3028 $parsed_updated = make_local_datetime($link, $line["updated"], true,
64436e10 3029 $owner_uid, true);
324944f3 3030
5321e775 3031 $rv['content'] .= "<div class=\"postDate\">$parsed_updated</div>";
3de0261a
AD
3032
3033 if ($line["link"]) {
c6c010d9 3034 $rv['content'] .= "<div class='postTitle'><a target='_blank'
bf1dc420 3035 title=\"".htmlspecialchars($line['title'])."\"
8d505d78 3036 href=\"" .
5c568973 3037 htmlspecialchars($line["link"]) . "\">" .
11bd95b4
AD
3038 $line["title"] . "</a>" .
3039 "<span class='author'>$entry_author</span></div>";
3de0261a 3040 } else {
c6c010d9 3041 $rv['content'] .= "<div class='postTitle'>" . $line["title"] . "$entry_author</div>";
3de0261a
AD
3042 }
3043
84d952f1
AD
3044 $tags_str = format_tags_string($line["tags"], $id);
3045 $tags_str_full = join(", ", $line["tags"]);
0780f4f4
AD
3046
3047 if (!$tags_str_full) $tags_str_full = __("no tags");
e7544143 3048
3de0261a
AD
3049 if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
3050
f0755b7c 3051 $rv['content'] .= "<div class='postTags' style='float : right'>
2a3b6de0 3052 <img src='images/tag.png'
e9823609 3053 class='tagsPic' alt='Tags' title='Tags'>&nbsp;";
eedfb635
AD
3054
3055 if (!$zoom_mode) {
009646d2 3056 $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span>
8d505d78 3057 <a title=\"".__('Edit tags for this article')."\"
31a53903 3058 href=\"#\" onclick=\"editArticleTags($id, $feed_id)\">(+)</a>";
4710e3dc 3059
0780f4f4
AD
3060 $rv['content'] .= "<div dojoType=\"dijit.Tooltip\"
3061 id=\"ATSTRTIP-$id\" connectId=\"ATSTR-$id\"
3062 position=\"below\">$tags_str_full</div>";
3063
19c73507 3064 global $pluginhost;
f9ac31d6 3065
19c73507
AD
3066 foreach ($pluginhost->get_hooks($pluginhost::HOOK_ARTICLE_BUTTON) as $p) {
3067 $rv['content'] .= $p->hook_article_button($line);
411fe209
AD
3068 }
3069
6f3976c9 3070
24ecbcae
AD
3071 } else {
3072 $tags_str = strip_tags($tags_str);
009646d2 3073 $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span>";
eedfb635 3074 }
009646d2
AD
3075 $rv['content'] .= "</div>";
3076 $rv['content'] .= "<div clear='both'>$entry_comments</div>";
3de0261a 3077
ef83538d
AD
3078 if ($line["orig_feed_id"]) {
3079
3080 $tmp_result = db_query($link, "SELECT * FROM ttrss_archived_feeds
3081 WHERE id = ".$line["orig_feed_id"]);
3082
3083 if (db_num_rows($tmp_result) != 0) {
3084
009646d2
AD
3085 $rv['content'] .= "<div clear='both'>";
3086 $rv['content'] .= __("Originally from:");
ef83538d 3087
009646d2 3088 $rv['content'] .= "&nbsp;";
ef83538d
AD
3089
3090 $tmp_line = db_fetch_assoc($tmp_result);
3091
009646d2 3092 $rv['content'] .= "<a target='_blank'
ef83538d
AD
3093 href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
3094 $tmp_line['title'] . "</a>";
3095
009646d2 3096 $rv['content'] .= "&nbsp;";
ef83538d 3097
009646d2 3098 $rv['content'] .= "<a target='_blank' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
c2167866 3099 $rv['content'] .= "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_set.svg'></a>";
ef83538d 3100
009646d2 3101 $rv['content'] .= "</div>";
ef83538d
AD
3102 }
3103 }
3104
009646d2 3105 $rv['content'] .= "</div>";
3de0261a 3106
009646d2 3107 $rv['content'] .= "<div id=\"POSTNOTE-$id\">";
c7e51de1 3108 if ($line['note']) {
16cbc19a 3109 $rv['content'] .= format_article_note($id, $line['note'], !$zoom_mode);
c7e51de1 3110 }
009646d2 3111 $rv['content'] .= "</div>";
c7e51de1 3112
009646d2 3113 $rv['content'] .= "<div class=\"postContent\">";
741b6090 3114
84d952f1 3115 $rv['content'] .= $line["content"];
db54143e 3116
009646d2 3117 $rv['content'] .= format_article_enclosures($link, $id,
33de3d37 3118 $always_display_enclosures, $line["content"], $line["hide_images"]);
ce53e200 3119
009646d2 3120 $rv['content'] .= "</div>";
dad14b51 3121
009646d2 3122 $rv['content'] .= "</div>";
3de0261a
AD
3123
3124 }
3125
009646d2
AD
3126 if ($zoom_mode) {
3127 $rv['content'] .= "
b87a625c 3128 <div class='footer'>
2ae69126
AD
3129 <button onclick=\"return window.close()\">".
3130 __("Close this window")."</button></div>";
009646d2 3131 $rv['content'] .= "</body></html>";
eedfb635 3132 }
3de0261a 3133
009646d2
AD
3134 return $rv;
3135
3de0261a
AD
3136 }
3137
79178062 3138 function print_checkpoint($n, $s) {
fa9e88c3 3139 $ts = microtime(true);
79178062
AD
3140 echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s);
3141 return $ts;
3142 }
3de0261a 3143
79178062
AD
3144 function sanitize_tag($tag) {
3145 $tag = trim($tag);
52d7e7da 3146
79178062 3147 $tag = mb_strtolower($tag, 'utf-8');
bd202c3f 3148
79178062 3149 $tag = preg_replace('/[\'\"\+\>\<]/', "", $tag);
46921916 3150
79178062
AD
3151// $tag = str_replace('"', "", $tag);
3152// $tag = str_replace("+", " ", $tag);
3153 $tag = str_replace("technorati tag: ", "", $tag);
961f4c73 3154
79178062
AD
3155 return $tag;
3156 }
3de0261a 3157
79178062 3158 function get_self_url_prefix() {
51cc3873
AD
3159 if (strrpos(SELF_URL_PATH, "/") === strlen(SELF_URL_PATH)-1) {
3160 return substr(SELF_URL_PATH, 0, strlen(SELF_URL_PATH)-1);
3161 } else {
3162 return SELF_URL_PATH;
3163 }
79178062 3164 }
a9bcfb8f 3165
45004d43
AD
3166 /**
3167 * Compute the Mozilla Firefox feed adding URL from server HOST and REQUEST_URI.
3168 *
3169 * @return string The Mozilla Firefox feed adding URL.
3170 */
3171 function add_feed_url() {
ed102aa0
AD
3172 //$url_path = ($_SERVER['HTTPS'] != "on" ? 'http://' : 'https://') . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
3173
3174 $url_path = get_self_url_prefix() .
97acbaf1 3175 "/public.php?op=subscribe&feed_url=%s";
755a43ee 3176 return $url_path;
45004d43
AD
3177 } // function add_feed_url
3178
e90053fe
AD
3179 function encrypt_password($pass, $salt = '', $mode2 = false) {
3180 if ($salt && $mode2) {
3181 return "MODE2:" . hash('sha256', $salt . $pass);
3182 } else if ($salt) {
3183 return "SHA1X:" . sha1("$salt:$pass");
1a9f4d3c
AD
3184 } else {
3185 return "SHA1:" . sha1($pass);
3186 }
45004d43
AD
3187 } // function encrypt_password
3188
6aff7845 3189 function load_filters($link, $feed_id, $owner_uid, $action_id = false) {
fee840fb
AD
3190 $filters = array();
3191
5574b09e 3192 $cat_id = (int)getFeedCategory($link, $feed_id);
fee840fb 3193
6aff7845 3194 $result = db_query($link, "SELECT * FROM ttrss_filters2 WHERE
8e8c8934 3195 owner_uid = $owner_uid AND enabled = true ORDER BY order_id, title");
8d505d78 3196
67bd0b1f
AD
3197 $check_cats = join(",", array_merge(
3198 getParentCategories($link, $cat_id, $owner_uid),
3199 array($cat_id)));
3200
0e4a7d7a 3201 while ($line = db_fetch_assoc($result)) {
6aff7845
AD
3202 $filter_id = $line["id"];
3203
3204 $result2 = db_query($link, "SELECT
a3a896a1 3205 r.reg_exp, r.inverse, r.feed_id, r.cat_id, r.cat_filter, t.name AS type_name
6aff7845
AD
3206 FROM ttrss_filters2_rules AS r,
3207 ttrss_filter_types AS t
3208 WHERE
67bd0b1f 3209 (cat_id IS NULL OR cat_id IN ($check_cats)) AND
6aff7845
AD
3210 (feed_id IS NULL OR feed_id = '$feed_id') AND
3211 filter_type = t.id AND filter_id = '$filter_id'");
3212
3213 $rules = array();
3214 $actions = array();
ba975b2e 3215
6aff7845
AD
3216 while ($rule_line = db_fetch_assoc($result2)) {
3217# print_r($rule_line);
8d505d78 3218
6aff7845
AD
3219 $rule = array();
3220 $rule["reg_exp"] = $rule_line["reg_exp"];
3221 $rule["type"] = $rule_line["type_name"];
a3a896a1 3222 $rule["inverse"] = sql_bool_to_bool($rule_line["inverse"]);
6aff7845
AD
3223
3224 array_push($rules, $rule);
3225 }
3226
3227 $result2 = db_query($link, "SELECT a.action_param,t.name AS type_name
3228 FROM ttrss_filters2_actions AS a,
3229 ttrss_filter_actions AS t
3230 WHERE
3231 action_id = t.id AND filter_id = '$filter_id'");
3232
3233 while ($action_line = db_fetch_assoc($result2)) {
3234# print_r($action_line);
3235
3236 $action = array();
3237 $action["type"] = $action_line["type_name"];
3238 $action["param"] = $action_line["action_param"];
3239
3240 array_push($actions, $action);
0e4a7d7a 3241 }
b8ffa322 3242
b8ffa322 3243
6aff7845
AD
3244 $filter = array();
3245 $filter["match_any_rule"] = sql_bool_to_bool($line["match_any_rule"]);
a3a896a1 3246 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
6aff7845
AD
3247 $filter["rules"] = $rules;
3248 $filter["actions"] = $actions;
3249
3250 if (count($rules) > 0 && count($actions) > 0) {
3251 array_push($filters, $filter);
3252 }
3253 }
3254
0e4a7d7a 3255 return $filters;
fee840fb 3256 }
1e36af0c
AD
3257
3258 function get_score_pic($score) {
8d505d78
AD
3259 if ($score > 100) {
3260 return "score_high.png";
3261 } else if ($score > 0) {
883fee8d 3262 return "score_half_high.png";
1cce3aca 3263 } else if ($score < -100) {
883fee8d 3264 return "score_low.png";
1cce3aca 3265 } else if ($score < 0) {
883fee8d 3266 return "score_half_low.png";
8d505d78 3267 } else {
883fee8d 3268 return "score_neutral.png";
1e36af0c
AD
3269 }
3270 }
ec92c9d1 3271
7defa089
AD
3272 function feed_has_icon($id) {
3273 return is_file(ICONS_DIR . "/$id.ico") && filesize(ICONS_DIR . "/$id.ico") > 0;
3274 }
f29ba148
AD
3275
3276 function init_connection($link) {
8c0496f7
AD
3277 if ($link) {
3278
3279 if (DB_TYPE == "pgsql") {
3280 pg_query($link, "set client_encoding = 'UTF-8'");
3281 pg_set_client_encoding("UNICODE");
3282 pg_query($link, "set datestyle = 'ISO, european'");
3283 pg_query($link, "set TIME ZONE 0");
3284 } else {
3285 db_query($link, "SET time_zone = '+0:0'");
3286
3287 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
3288 db_query($link, "SET NAMES " . MYSQL_CHARSET);
3289 }
3290 }
19c73507
AD
3291
3292 global $pluginhost;
3293
8c0496f7 3294 $pluginhost = new PluginHost($link);
d2a421e3 3295 $pluginhost->load(PLUGINS, $pluginhost::KIND_ALL);
19c73507 3296
5f0a3741
AD
3297 return true;
3298 } else {
3299 print "Unable to connect to database:" . db_last_error();
3300 return false;
f29ba148
AD
3301 }
3302 }
5e96ca9d 3303
307d187c
AD
3304 function format_tags_string($tags, $id) {
3305
3306 $tags_str = "";
3307 $tags_nolinks_str = "";
3308
3309 $num_tags = 0;
3310
d9084cf2 3311 $tag_limit = 6;
307d187c
AD
3312
3313 $formatted_tags = array();
3314
3315 foreach ($tags as $tag) {
3316 $num_tags++;
3317 $tag_escaped = str_replace("'", "\\'", $tag);
3318
275a0af2
AD
3319 if (mb_strlen($tag) > 30) {
3320 $tag = truncate_string($tag, 30);
3321 }
3322
307d187c
AD
3323 $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>";
3324
3325 array_push($formatted_tags, $tag_str);
275a0af2
AD
3326
3327 $tmp_tags_str = implode(", ", $formatted_tags);
8d505d78 3328
275a0af2 3329 if ($num_tags == $tag_limit || mb_strlen($tmp_tags_str) > 150) {
307d187c
AD
3330 break;
3331 }
3332 }
3333
3334 $tags_str = implode(", ", $formatted_tags);
3335
3336 if ($num_tags < count($tags)) {
3337 $tags_str .= ", &hellip;";
3338 }
3339
3340 if ($num_tags == 0) {
3341 $tags_str = __("no tags");
3342 }
3343
3344 return $tags_str;
3345
3346 }
2eb9c95c
AD
3347
3348 function format_article_labels($labels, $id) {
3349
3350 $labels_str = "";
3351
3352 foreach ($labels as $l) {
8d505d78 3353 $labels_str .= sprintf("<span class='hlLabelRef'
2eb9c95c
AD
3354 style='color : %s; background-color : %s'>%s</span>",
3355 $l[2], $l[3], $l[1]);
3356 }
3357
3358 return $labels_str;
3359
3360 }
c7e51de1 3361
16cbc19a 3362 function format_article_note($id, $note, $allow_edit = true) {
c7e51de1 3363
fcfa9ef1
AD
3364 $str = "<div class='articleNote' onclick=\"editArticleNote($id)\">
3365 <div class='noteEdit' onclick=\"editArticleNote($id)\">".
16cbc19a 3366 ($allow_edit ? __('(edit note)') : "")."</div>$note</div>";
c7e51de1
AD
3367
3368 return $str;
3369 }
7f969260 3370
7e329f13 3371
d2a317e3
AD
3372 function get_feed_category($link, $feed_cat, $parent_cat_id = false) {
3373 if ($parent_cat_id) {
3374 $parent_qpart = "parent_cat = '$parent_cat_id'";
3375 $parent_insert = "'$parent_cat_id'";
3376 } else {
3377 $parent_qpart = "parent_cat IS NULL";
3378 $parent_insert = "NULL";
3379 }
3380
3381 $result = db_query($link,
3382 "SELECT id FROM ttrss_feed_categories
3383 WHERE $parent_qpart AND title = '$feed_cat' AND owner_uid = ".$_SESSION["uid"]);
3384
3385 if (db_num_rows($result) == 0) {
3386 return false;
3387 } else {
3388 return db_fetch_result($result, 0, "id");
3389 }
3390 }
3391
3392 function add_feed_category($link, $feed_cat, $parent_cat_id = false) {
c00907f2
AD
3393
3394 if (!$feed_cat) return false;
3395
5c7c7da9
AD
3396 db_query($link, "BEGIN");
3397
d2a317e3
AD
3398 if ($parent_cat_id) {
3399 $parent_qpart = "parent_cat = '$parent_cat_id'";
3400 $parent_insert = "'$parent_cat_id'";
3401 } else {
3402 $parent_qpart = "parent_cat IS NULL";
3403 $parent_insert = "NULL";
3404 }
3405
5c7c7da9
AD
3406 $result = db_query($link,
3407 "SELECT id FROM ttrss_feed_categories
d2a317e3 3408 WHERE $parent_qpart AND title = '$feed_cat' AND owner_uid = ".$_SESSION["uid"]);
5c7c7da9
AD
3409
3410 if (db_num_rows($result) == 0) {
8d505d78 3411
5c7c7da9 3412 $result = db_query($link,
d2a317e3
AD
3413 "INSERT INTO ttrss_feed_categories (owner_uid,title,parent_cat)
3414 VALUES ('".$_SESSION["uid"]."', '$feed_cat', $parent_insert)");
5c7c7da9
AD
3415
3416 db_query($link, "COMMIT");
3417
3418 return true;
3419 }
3420
3421 return false;
8d505d78 3422 }
5c7c7da9 3423
ab197ae1 3424 function getArticleFeed($link, $id) {
8d505d78 3425 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
a545dc31 3426 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
ab197ae1
AD
3427
3428 if (db_num_rows($result) != 0) {
3429 return db_fetch_result($result, 0, "feed_id");
3430 } else {
3431 return 0;
3432 }
3433 }
a5819bb3 3434
f2c6c008
CW
3435 /**
3436 * Fixes incomplete URLs by prepending "http://".
f0266f51
CW
3437 * Also replaces feed:// with http://, and
3438 * prepends a trailing slash if the url is a domain name only.
f2c6c008
CW
3439 *
3440 * @param string $url Possibly incomplete URL
3441 *
3442 * @return string Fixed URL.
3443 */
3444 function fix_url($url) {
3445 if (strpos($url, '://') === false) {
3446 $url = 'http://' . $url;
f0266f51
CW
3447 } else if (substr($url, 0, 5) == 'feed:') {
3448 $url = 'http:' . substr($url, 5);
3449 }
3450
3451 //prepend slash if the URL has no slash in it
3452 // "http://www.example" -> "http://www.example/"
44453773 3453 if (strpos($url, '/', strpos($url, ':') + 3) === false) {
f0266f51 3454 $url .= '/';
f2c6c008 3455 }
ec39a02c
AD
3456
3457 if ($url != "http:///")
3458 return $url;
3459 else
3460 return '';
f2c6c008
CW
3461 }
3462
a5819bb3
AD
3463 function validate_feed_url($url) {
3464 $parts = parse_url($url);
3465
3466 return ($parts['scheme'] == 'http' || $parts['scheme'] == 'feed' || $parts['scheme'] == 'https');
3467
3468 }
d9084cf2 3469
be35798b
AD
3470 function get_article_enclosures($link, $id) {
3471
8d505d78 3472 $query = "SELECT * FROM ttrss_enclosures
be35798b
AD
3473 WHERE post_id = '$id' AND content_url != ''";
3474
be35798b
AD
3475 $rv = array();
3476
0e4a7d7a 3477 $result = db_query($link, $query);
be35798b 3478
0e4a7d7a
AD
3479 if (db_num_rows($result) > 0) {
3480 while ($line = db_fetch_assoc($result)) {
3481 array_push($rv, $line);
be35798b
AD
3482 }
3483 }
3484
3485 return $rv;
3486 }
3487
31a53903
AD
3488 function save_email_address($link, $email) {
3489 // FIXME: implement persistent storage of emails
3490
8d505d78 3491 if (!$_SESSION['stored_emails'])
31a53903
AD
3492 $_SESSION['stored_emails'] = array();
3493
3494 if (!in_array($email, $_SESSION['stored_emails']))
3495 array_push($_SESSION['stored_emails'], $email);
3496 }
8801fb01 3497
8801fb01
AD
3498
3499 function get_feed_access_key($link, $feed_id, $is_cat, $owner_uid = false) {
3500
3501 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
3502
3503 $sql_is_cat = bool_to_sql_bool($is_cat);
3504
8d505d78
AD
3505 $result = db_query($link, "SELECT access_key FROM ttrss_access_keys
3506 WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat
8801fb01
AD
3507 AND owner_uid = " . $owner_uid);
3508
3509 if (db_num_rows($result) == 1) {
3510 return db_fetch_result($result, 0, "access_key");
3511 } else {
3972bf59 3512 $key = db_escape_string($link, sha1(uniqid(rand(), true)));
8801fb01 3513
8d505d78 3514 $result = db_query($link, "INSERT INTO ttrss_access_keys
8801fb01
AD
3515 (access_key, feed_id, is_cat, owner_uid)
3516 VALUES ('$key', '$feed_id', $sql_is_cat, '$owner_uid')");
3517
3518 return $key;
3519 }
3520 return false;
3521 }
f0266f51 3522
759e5132 3523 function get_feeds_from_html($url, $content)
f0266f51
CW
3524 {
3525 $url = fix_url($url);
3526 $baseUrl = substr($url, 0, strrpos($url, '/') + 1);
3527
fb074239
AD
3528 libxml_use_internal_errors(true);
3529
f0266f51 3530 $doc = new DOMDocument();
8d505d78 3531 $doc->loadHTML($content);
f0266f51
CW
3532 $xpath = new DOMXPath($doc);
3533 $entries = $xpath->query('/html/head/link[@rel="alternate"]');
3534 $feedUrls = array();
3535 foreach ($entries as $entry) {
3536 if ($entry->hasAttribute('href')) {
3537 $title = $entry->getAttribute('title');
3538 if ($title == '') {
3539 $title = $entry->getAttribute('type');
3540 }
923818fc
CW
3541 $feedUrl = rewrite_relative_url(
3542 $baseUrl, $entry->getAttribute('href')
3543 );
f0266f51
CW
3544 $feedUrls[$feedUrl] = $title;
3545 }
3546 }
3547 return $feedUrls;
3548 }
3549
759e5132 3550 function is_html($content) {
32b86711 3551 return preg_match("/<html|DOCTYPE html/i", substr($content, 0, 20)) !== 0;
759e5132 3552 }
f33479da 3553
759e5132
AD
3554 function url_is_html($url, $login = false, $pass = false) {
3555 return is_html(fetch_file_contents($url, false, $login, $pass));
f33479da 3556 }
24e2bb3a 3557
d90868d7 3558 function print_label_select($link, $name, $value, $attributes = "") {
24e2bb3a
AD
3559
3560 $result = db_query($link, "SELECT caption FROM ttrss_labels2
3561 WHERE owner_uid = '".$_SESSION["uid"]."' ORDER BY caption");
3562
8d505d78 3563 print "<select default=\"$value\" name=\"" . htmlspecialchars($name) .
d90868d7 3564 "\" $attributes onchange=\"labelSelectOnChange(this)\" >";
24e2bb3a
AD
3565
3566 while ($line = db_fetch_assoc($result)) {
3567
3568 $issel = ($line["caption"] == $value) ? "selected=\"1\"" : "";
3569
d90868d7
AD
3570 print "<option value=\"".htmlspecialchars($line["caption"])."\"
3571 $issel>" . htmlspecialchars($line["caption"]) . "</option>";
24e2bb3a
AD
3572
3573 }
3574
d90868d7 3575# print "<option value=\"ADD_LABEL\">" .__("Add label...") . "</option>";
24e2bb3a
AD
3576
3577 print "</select>";
3578
3579
3580 }
3581
009646d2 3582 function format_article_enclosures($link, $id, $always_display_enclosures,
33de3d37 3583 $article_content, $hide_images = false) {
dad14b51
AD
3584
3585 $result = get_article_enclosures($link, $id);
009646d2 3586 $rv = '';
8d505d78 3587
dad14b51 3588 if (count($result) > 0) {
8d505d78 3589
dad14b51
AD
3590 $entries_html = array();
3591 $entries = array();
ca3bca99 3592 $entries_inline = array();
8d505d78 3593
dad14b51 3594 foreach ($result as $line) {
8d505d78 3595
dad14b51
AD
3596 $url = $line["content_url"];
3597 $ctype = $line["content_type"];
8d505d78 3598
dad14b51 3599 if (!$ctype) $ctype = __("unknown type");
8d505d78 3600
749b56bd 3601 $filename = substr($url, strrpos($url, "/")+1);
8d505d78 3602
ca3bca99
AD
3603 $player = format_inline_player($link, $url, $ctype);
3604
3605 if ($player) array_push($entries_inline, $player);
8d505d78 3606
c3edc667
AD
3607# $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
3608# $filename . " (" . $ctype . ")" . "</a>";
8d505d78 3609
749b56bd
AD
3610 $entry = "<div onclick=\"window.open('".htmlspecialchars($url)."')\"
3611 dojoType=\"dijit.MenuItem\">$filename ($ctype)</div>";
3612
dad14b51 3613 array_push($entries_html, $entry);
8d505d78 3614
dad14b51 3615 $entry = array();
8d505d78 3616
dad14b51
AD
3617 $entry["type"] = $ctype;
3618 $entry["filename"] = $filename;
3619 $entry["url"] = $url;
8d505d78 3620
dad14b51
AD
3621 array_push($entries, $entry);
3622 }
8d505d78 3623
ba79634c 3624 if ($_SESSION['uid'] && !get_pref($link, "STRIP_IMAGES") && !$_SESSION["bw_limit"]) {
dad14b51
AD
3625 if ($always_display_enclosures ||
3626 !preg_match("/<img/i", $article_content)) {
8d505d78 3627
dad14b51 3628 foreach ($entries as $entry) {
8d505d78 3629
dad14b51
AD
3630 if (preg_match("/image/", $entry["type"]) ||
3631 preg_match("/\.(jpg|png|gif|bmp)/i", $entry["filename"])) {
8d505d78 3632
33de3d37
AD
3633 if (!$hide_images) {
3634 $rv .= "<p><img
3635 alt=\"".htmlspecialchars($entry["filename"])."\"
3636 src=\"" .htmlspecialchars($entry["url"]) . "\"/></p>";
3637 } else {
3638 $rv .= "<p><a target=\"_blank\"
3639 href=\"".htmlspecialchars($entry["url"])."\"
3640 >" .htmlspecialchars($entry["url"]) . "</a></p>";
749b56bd 3641
33de3d37 3642 }
dad14b51
AD
3643 }
3644 }
3645 }
3646 }
8d505d78 3647
ca3bca99
AD
3648 if (count($entries_inline) > 0) {
3649 $rv .= "<hr clear='both'/>";
3650 foreach ($entries_inline as $entry) { $rv .= $entry; };
3651 $rv .= "<hr clear='both'/>";
3652 }
3653
bf6df236 3654 $rv .= "<select class=\"attachments\" onchange=\"openSelectedAttachment(this)\">".
f9432f26 3655 "<option value=''>" . __('Attachments')."</option>";
8d505d78 3656
f9432f26
AD
3657 foreach ($entries as $entry) {
3658 $rv .= "<option value=\"".htmlspecialchars($entry["url"])."\">" . htmlspecialchars($entry["filename"]) . "</option>";
8d505d78 3659
f9432f26
AD
3660 };
3661
3662 $rv .= "</select>";
dad14b51 3663 }
009646d2
AD
3664
3665 return $rv;
dad14b51
AD
3666 }
3667
f8fb4498
AD
3668 function getLastArticleId($link) {
3669 $result = db_query($link, "SELECT MAX(ref_id) AS id FROM ttrss_user_entries
3670 WHERE owner_uid = " . $_SESSION["uid"]);
3671
3672 if (db_num_rows($result) == 1) {
3673 return db_fetch_result($result, 0, "id");
3674 } else {
3675 return -1;
3676 }
3677 }
8cc3c778
AD
3678
3679 function build_url($parts) {
3680 return $parts['scheme'] . "://" . $parts['host'] . $parts['path'];
3681 }
3682
f679105c
CW
3683 /**
3684 * Converts a (possibly) relative URL to a absolute one.
3685 *
3686 * @param string $url Base URL (i.e. from where the document is)
3687 * @param string $rel_url Possibly relative URL in the document
3688 *
3689 * @return string Absolute URL
3690 */
8cc3c778 3691 function rewrite_relative_url($url, $rel_url) {
b4520bb8
AD
3692 if (strpos($rel_url, "magnet:") === 0) {
3693 return $rel_url;
3694 } else if (strpos($rel_url, "://") !== false) {
8cc3c778 3695 return $rel_url;
f9052d35 3696 } else if (strpos($rel_url, "//") === 0) {
3697 # protocol-relative URL (rare but they exist)
3698 return $rel_url;
8d505d78 3699 } else if (strpos($rel_url, "/") === 0)
8cc3c778
AD
3700 {
3701 $parts = parse_url($url);
3702 $parts['path'] = $rel_url;
3703
3704 return build_url($parts);
3705
3706 } else {
3707 $parts = parse_url($url);
f679105c
CW
3708 if (!isset($parts['path'])) {
3709 $parts['path'] = '/';
3710 }
3711 $dir = $parts['path'];
3712 if (substr($dir, -1) !== '/') {
3713 $dir = dirname($parts['path']);
3714 $dir !== '/' && $dir .= '/';
3715 }
3716 $parts['path'] = $dir . $rel_url;
8cc3c778
AD
3717
3718 return build_url($parts);
3719 }
3720 }
3721
e4f7f8df 3722 function sphinx_search($query, $offset = 0, $limit = 30) {
31303c6b
AD
3723 require_once 'lib/sphinxapi.php';
3724
e4f7f8df
AD
3725 $sphinxClient = new SphinxClient();
3726
3727 $sphinxClient->SetServer('localhost', 9312);
3728 $sphinxClient->SetConnectTimeout(1);
3729
8d505d78 3730 $sphinxClient->SetFieldWeights(array('title' => 70, 'content' => 30,
e4f7f8df
AD
3731 'feed_title' => 20));
3732
3733 $sphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2);
3734 $sphinxClient->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
3735 $sphinxClient->SetLimits($offset, $limit, 1000);
3736 $sphinxClient->SetArrayResult(false);
3737 $sphinxClient->SetFilter('owner_uid', array($_SESSION['uid']));
8d505d78 3738
e4f7f8df
AD
3739 $result = $sphinxClient->Query($query, SPHINX_INDEX);
3740
3741 $ids = array();
3742
3743 if (is_array($result['matches'])) {
3744 foreach (array_keys($result['matches']) as $int_id) {
3745 $ref_id = $result['matches'][$int_id]['attrs']['ref_id'];
3746 array_push($ids, $ref_id);
3747 }
3748 }
3749
3750 return $ids;
3751 }
3752
868650e4
AD
3753 function cleanup_tags($link, $days = 14, $limit = 1000) {
3754
3755 if (DB_TYPE == "pgsql") {
3756 $interval_query = "date_updated < NOW() - INTERVAL '$days days'";
3757 } else if (DB_TYPE == "mysql") {
3758 $interval_query = "date_updated < DATE_SUB(NOW(), INTERVAL $days DAY)";
3759 }
3760
b5ec13fa 3761 $tags_deleted = 0;
868650e4 3762
b5ec13fa
AD
3763 while ($limit > 0) {
3764 $limit_part = 500;
3765
8d505d78
AD
3766 $query = "SELECT ttrss_tags.id AS id
3767 FROM ttrss_tags, ttrss_user_entries, ttrss_entries
b5ec13fa
AD
3768 WHERE post_int_id = int_id AND $interval_query AND
3769 ref_id = ttrss_entries.id AND tag_cache != '' LIMIT $limit_part";
8d505d78 3770
b5ec13fa
AD
3771 $result = db_query($link, $query);
3772
3773 $ids = array();
3774
3775 while ($line = db_fetch_assoc($result)) {
3776 array_push($ids, $line['id']);
3777 }
3778
3779 if (count($ids) > 0) {
3780 $ids = join(",", $ids);
b5ec13fa
AD
3781
3782 $tmp_result = db_query($link, "DELETE FROM ttrss_tags WHERE id IN ($ids)");
3783 $tags_deleted += db_affected_rows($link, $tmp_result);
3784 } else {
3785 break;
3786 }
3787
3788 $limit -= $limit_part;
3789 }
3790
b5ec13fa 3791 return $tags_deleted;
868650e4
AD
3792 }
3793
88e4e597
AD
3794 function print_user_stylesheet($link) {
3795 $value = get_pref($link, 'USER_STYLESHEET');
3796
3797 if ($value) {
3798 print "<style type=\"text/css\">";
5823f9fb 3799 print str_replace("<br/>", "\n", $value);
88e4e597
AD
3800 print "</style>";
3801 }
3802
3803 }
3804
73c32678
AD
3805 function rewrite_urls($html) {
3806 libxml_use_internal_errors(true);
3807
3808 $charset_hack = '<head>
3809 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
3810 </head>';
3811
3812 $doc = new DOMDocument();
3813 $doc->loadHTML($charset_hack . $html);
3814 $xpath = new DOMXPath($doc);
3815
3816 $entries = $xpath->query('//*/text()');
3817
3818 foreach ($entries as $entry) {
3819 if (strstr($entry->wholeText, "://") !== false) {
3820 $text = preg_replace("/((?<!=.)((http|https|ftp)+):\/\/[^ ,!]+)/i",
3821 "<a target=\"_blank\" href=\"\\1\">\\1</a>", $entry->wholeText);
3822
3823 if ($text != $entry->wholeText) {
3824 $cdoc = new DOMDocument();
3825 $cdoc->loadHTML($charset_hack . $text);
3826
3827
3828 foreach ($cdoc->childNodes as $cnode) {
3829 $cnode = $doc->importNode($cnode, true);
3830
3831 if ($cnode) {
3832 $entry->parentNode->insertBefore($cnode);
3833 }
3834 }
3835
3836 $entry->parentNode->removeChild($entry);
3837
3838 }
3839 }
3840 }
3841
3842 $node = $doc->getElementsByTagName('body')->item(0);
3843
376897af
AD
3844 // http://tt-rss.org/forum/viewtopic.php?f=1&t=970
3845 if ($node)
cc38c8e5 3846 return $doc->saveXML($node);
376897af
AD
3847 else
3848 return $html;
533c0ea6
AD
3849 }
3850
2680295b 3851 function filter_to_sql($link, $filter, $owner_uid) {
4e02f582 3852 $query = array();
36184020 3853
4e02f582
AD
3854 if (DB_TYPE == "pgsql")
3855 $reg_qpart = "~";
3856 else
3857 $reg_qpart = "REGEXP";
36184020 3858
4e02f582
AD
3859 foreach ($filter["rules"] AS $rule) {
3860 $regexp_valid = preg_match('/' . $rule['reg_exp'] . '/',
3861 $rule['reg_exp']) !== FALSE;
36184020 3862
4e02f582 3863 if ($regexp_valid) {
36184020 3864
3972bf59 3865 $rule['reg_exp'] = db_escape_string($link, $rule['reg_exp']);
36184020 3866
a3a896a1 3867 switch ($rule["type"]) {
4e02f582
AD
3868 case "title":
3869 $qpart = "LOWER(ttrss_entries.title) $reg_qpart LOWER('".
3870 $rule['reg_exp'] . "')";
3871 break;
3872 case "content":
3873 $qpart = "LOWER(ttrss_entries.content) $reg_qpart LOWER('".
3874 $rule['reg_exp'] . "')";
3875 break;
3876 case "both":
3877 $qpart = "LOWER(ttrss_entries.title) $reg_qpart LOWER('".
3878 $rule['reg_exp'] . "') OR LOWER(" .
3879 "ttrss_entries.content) $reg_qpart LOWER('" . $rule['reg_exp'] . "')";
3880 break;
3881 case "tag":
3882 $qpart = "LOWER(ttrss_user_entries.tag_cache) $reg_qpart LOWER('".
3883 $rule['reg_exp'] . "')";
3884 break;
3885 case "link":
3886 $qpart = "LOWER(ttrss_entries.link) $reg_qpart LOWER('".
3887 $rule['reg_exp'] . "')";
3888 break;
3889 case "author":
3890 $qpart = "LOWER(ttrss_entries.author) $reg_qpart LOWER('".
3891 $rule['reg_exp'] . "')";
3892 break;
3893 }
36184020 3894
ec1f8a3d
AD
3895 if (isset($rule['inverse'])) $qpart = "NOT ($qpart)";
3896
6b218731 3897 if (isset($rule["feed_id"]) && $rule["feed_id"] > 0) {
3972bf59 3898 $qpart .= " AND feed_id = " . db_escape_string($link, $rule["feed_id"]);
4e02f582 3899 }
6b8b3af8 3900
4e02f582 3901 if (isset($rule["cat_id"])) {
2680295b
AD
3902
3903 if ($rule["cat_id"] > 0) {
3904 $children = getChildCategories($link, $rule["cat_id"], $owner_uid);
3905 array_push($children, $rule["cat_id"]);
3906
3907 $children = join(",", $children);
3908
3909 $cat_qpart = "cat_id IN ($children)";
3910 } else {
3911 $cat_qpart = "cat_id IS NULL";
3912 }
3913
3914 $qpart .= " AND $cat_qpart";
56fbb82c 3915 }
4e02f582
AD
3916
3917 array_push($query, "($qpart)");
3918
56fbb82c 3919 }
4e02f582 3920 }
56fbb82c 3921
4e02f582 3922 if (count($query) > 0) {
a3a896a1 3923 $fullquery = "(" . join($filter["match_any_rule"] ? "OR" : "AND", $query) . ")";
56fbb82c 3924 } else {
a3a896a1 3925 $fullquery = "(false)";
56fbb82c 3926 }
a3a896a1
AD
3927
3928 if ($filter['inverse']) $fullquery = "(NOT $fullquery)";
3929
3930 return $fullquery;
36184020 3931 }
ae5f7bb1 3932
3382bce1
AD
3933 if (!function_exists('gzdecode')) {
3934 function gzdecode($string) { // no support for 2nd argument
3935 return file_get_contents('compress.zlib://data:who/cares;base64,'.
3936 base64_encode($string));
3937 }
3938 }
3939
8db5d8ea
AD
3940 function get_random_bytes($length) {
3941 if (function_exists('openssl_random_pseudo_bytes')) {
3942 return openssl_random_pseudo_bytes($length);
3943 } else {
3944 $output = "";
3945
3946 for ($i = 0; $i < $length; $i++)
3947 $output .= chr(mt_rand(0, 255));
3948
3949 return $output;
3950 }
3951 }
871f0a7a
AD
3952
3953 function read_stdin() {
3954 $fp = fopen("php://stdin", "r");
3955
3956 if ($fp) {
3957 $line = trim(fgets($fp));
3958 fclose($fp);
3959 return $line;
3960 }
3961
3962 return null;
3963 }
e3449aa1
AD
3964
3965 function tmpdirname($path, $prefix) {
3966 // Use PHP's tmpfile function to create a temporary
3967 // directory name. Delete the file and keep the name.
3968 $tempname = tempnam($path,$prefix);
3969 if (!$tempname)
3970 return false;
3971
3972 if (!unlink($tempname))
3973 return false;
3974
3975 return $tempname;
3976 }
3977
6aff7845
AD
3978 function getFeedCategory($link, $feed) {
3979 $result = db_query($link, "SELECT cat_id FROM ttrss_feeds
3980 WHERE id = '$feed'");
3981
3982 if (db_num_rows($result) > 0) {
3983 return db_fetch_result($result, 0, "cat_id");
3984 } else {
3985 return false;
3986 }
3987
3988 }
3989
8dcb2b47
AD
3990 function implements_interface($class, $interface) {
3991 return in_array($interface, class_implements($class));
3992 }
e88c1943 3993
e2b0054b
AD
3994 function geturl($url){
3995
3996 (function_exists('curl_init')) ? '' : die('cURL Must be installed for geturl function to work. Ask your host to enable it or uncomment extension=php_curl.dll in php.ini');
3997
3998 $curl = curl_init();
3999 $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
4000 $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
4001 $header[] = "Cache-Control: max-age=0";
4002 $header[] = "Connection: keep-alive";
4003 $header[] = "Keep-Alive: 300";
4004 $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
4005 $header[] = "Accept-Language: en-us,en;q=0.5";
4006 $header[] = "Pragma: ";
4007
4008 curl_setopt($curl, CURLOPT_URL, $url);
4009 curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0 Firefox/5.0');
4010 curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
4011 curl_setopt($curl, CURLOPT_HEADER, true);
4012 curl_setopt($curl, CURLOPT_REFERER, $url);
4013 curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
4014 curl_setopt($curl, CURLOPT_AUTOREFERER, true);
4015 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
4016 //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); //CURLOPT_FOLLOWLOCATION Disabled...
4017 curl_setopt($curl, CURLOPT_TIMEOUT, 60);
4018
4019 $html = curl_exec($curl);
4020
4021 $status = curl_getinfo($curl);
4022 curl_close($curl);
4023
4024 if($status['http_code']!=200){
4025 if($status['http_code'] == 301 || $status['http_code'] == 302) {
4026 list($header) = explode("\r\n\r\n", $html, 2);
4027 $matches = array();
4028 preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
4029 $url = trim(str_replace($matches[1],"",$matches[0]));
4030 $url_parsed = parse_url($url);
4031 return (isset($url_parsed))? geturl($url, $referer):'';
4032 }
4033 $oline='';
4034 foreach($status as $key=>$eline){$oline.='['.$key.']'.$eline.' ';}
4035 $line =$oline." \r\n ".$url."\r\n-----------------\r\n";
911845b5
AD
4036# $handle = @fopen('./curl.error.log', 'a');
4037# fwrite($handle, $line);
e2b0054b
AD
4038 return FALSE;
4039 }
4040 return $url;
4041 }
8dcb2b47 4042
c670a80d
AD
4043 function get_minified_js($files) {
4044 require_once 'lib/jshrink/Minifier.php';
4045
4046 $rv = '';
4047
4048 foreach ($files as $js) {
4049 if (!isset($_GET['debug'])) {
4050 $cached_file = CACHE_DIR . "/js/$js.js";
4051
4052 if (file_exists($cached_file) &&
4053 is_readable($cached_file) &&
4054 filemtime($cached_file) >= filemtime("js/$js.js")) {
4055
4056 $rv .= file_get_contents($cached_file);
4057
4058 } else {
4059 $minified = JShrink\Minifier::minify(file_get_contents("js/$js.js"));
4060 file_put_contents($cached_file, $minified);
4061 $rv .= $minified;
4062 }
4063 } else {
4064 $rv .= file_get_contents("js/$js.js");
4065 }
4066 }
4067
4068 return $rv;
4069 }
4070
b5d4716a
AD
4071 function stylesheet_tag($filename) {
4072 $timestamp = filemtime($filename);
4073
4074 echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$filename?$timestamp\"/>\n";
4075 }
4076
4077 function javascript_tag($filename) {
4078 $query = "";
4079
4080 if (!(strpos($filename, "?") === FALSE)) {
4081 $query = substr($filename, strpos($filename, "?")+1);
4082 $filename = substr($filename, 0, strpos($filename, "?"));
4083 }
4084
4085 $timestamp = filemtime($filename);
4086
4087 if ($query) $timestamp .= "&$query";
4088
4089 echo "<script type=\"text/javascript\" charset=\"utf-8\" src=\"$filename?$timestamp\"></script>\n";
4090 }
4091
16314dda
AD
4092 function calculate_dep_timestamp() {
4093 $files = array_merge(glob("js/*.js"), glob("*.css"));
4094
4095 $max_ts = -1;
4096
4097 foreach ($files as $file) {
4098 if (filemtime($file) > $max_ts) $max_ts = filemtime($file);
4099 }
4100
4101 return $max_ts;
4102 }
4103
bcbb2ec7
AD
4104 function T_js_decl($s1, $s2) {
4105 if ($s1 && $s2) {
4106 $s1 = preg_replace("/\n/", "", $s1);
4107 $s2 = preg_replace("/\n/", "", $s2);
4108
4109 $s1 = preg_replace("/\"/", "\\\"", $s1);
4110 $s2 = preg_replace("/\"/", "\\\"", $s2);
4111
4112 return "T_messages[\"$s1\"] = \"$s2\";\n";
4113 }
4114 }
4115
4116 function init_js_translations() {
4117
4118 print 'var T_messages = new Object();
4119
4120 function __(msg) {
4121 if (T_messages[msg]) {
4122 return T_messages[msg];
4123 } else {
4124 return msg;
4125 }
4126 }
4127
4128 function ngettext(msg1, msg2, n) {
4129 return (parseInt(n) > 1) ? msg2 : msg1;
4130 }';
4131
4132 $l10n = _get_reader();
4133
4134 for ($i = 0; $i < $l10n->total; $i++) {
4135 $orig = $l10n->get_original_string($i);
4136 $translation = __($orig);
4137
4138 print T_js_decl($orig, $translation);
4139 }
4140 }
4141
f822a8e5
AD
4142 function label_to_feed_id($label) {
4143 return LABEL_BASE_INDEX - 1 - abs($label);
4144 }
4145
4146 function feed_to_label_id($feed) {
4147 return LABEL_BASE_INDEX - 1 + abs($feed);
4148 }
4149
8c0496f7 4150?>