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