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