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