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