]>
Commit | Line | Data |
---|---|---|
1d3a17c7 | 1 | <?php |
f1a80dae | 2 | |
894ebcf5 | 3 | /* if ($_GET["debug"]) { |
cce28758 AD |
4 | define('DEFAULT_ERROR_LEVEL', E_ALL); |
5 | } else { | |
6 | define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE); | |
894ebcf5 | 7 | } */ |
cce28758 | 8 | |
40d13c28 | 9 | require_once 'config.php'; |
cc17c205 | 10 | |
f8c612d4 | 11 | function get_translations() { |
6a214f92 | 12 | $tr = array( |
019bd5a9 | 13 | "auto" => "Detect automatically", |
6a214f92 AD |
14 | "en_US" => "English", |
15 | "fr_FR" => "Français", | |
16 | "ru_RU" => "Русский", | |
17 | "zh_CN" => "Simplified Chinese"); | |
f8c612d4 AD |
18 | |
19 | return $tr; | |
20 | } | |
21 | ||
8d039718 | 22 | if (ENABLE_TRANSLATIONS == true) { |
865220a4 AD |
23 | require_once "accept-to-gettext.php"; |
24 | require_once "gettext/gettext.inc"; | |
aba609e0 | 25 | |
8d039718 AD |
26 | function startup_gettext() { |
27 | ||
28 | # Get locale from Accept-Language header | |
6a214f92 | 29 | $lang = al2gt(array_keys(get_translations()), "text/html"); |
89cb787e AD |
30 | |
31 | if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) { | |
32 | $lang = _TRANSLATION_OVERRIDE_DEFAULT; | |
33 | } | |
34 | ||
672f3f3c | 35 | if ($_COOKIE["ttrss_lang"] && $_COOKIE["ttrss_lang"] != "auto") { |
68659d98 AD |
36 | $lang = $_COOKIE["ttrss_lang"]; |
37 | } | |
38 | ||
8d039718 AD |
39 | if ($lang) { |
40 | _setlocale(LC_MESSAGES, $lang); | |
41 | _bindtextdomain("messages", "locale"); | |
42 | _textdomain("messages"); | |
43 | _bind_textdomain_codeset("messages", "UTF-8"); | |
44 | } | |
aba609e0 | 45 | } |
aba609e0 | 46 | |
cc17c205 | 47 | startup_gettext(); |
865220a4 AD |
48 | |
49 | } else { | |
50 | function __($msg) { | |
51 | return $msg; | |
52 | } | |
53 | function startup_gettext() { | |
54 | // no-op | |
55 | return true; | |
56 | } | |
cc17c205 AD |
57 | } |
58 | ||
b619ff15 | 59 | require_once 'db-prefs.php'; |
5bc0bd27 | 60 | require_once 'compat.php'; |
af106b0e | 61 | require_once 'errors.php'; |
8911ac8b | 62 | require_once 'version.php'; |
40d13c28 | 63 | |
49f9c923 | 64 | define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')'); |
a3ee2a38 AD |
65 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); |
66 | ||
9fdf7824 AD |
67 | if (ENABLE_SIMPLEPIE) { |
68 | require_once "simplepie/simplepie.inc"; | |
69 | } else { | |
70 | require_once "magpierss/rss_fetch.inc"; | |
71 | require_once 'magpierss/rss_utils.inc'; | |
72 | } | |
49f9c923 | 73 | |
f738aef1 AD |
74 | include_once "tw/tw-config.php"; |
75 | include_once "tw/tw.php"; | |
76 | include_once TW_SETUP . "paranoya.php"; | |
77 | ||
78 | $tw_parser = new twParser(); | |
79 | ||
6f9e33e4 AD |
80 | function _debug($msg) { |
81 | $ts = strftime("%H:%M:%S", time()); | |
82 | print "[$ts] $msg\n"; | |
83 | } | |
84 | ||
ad507f85 AD |
85 | function purge_feed($link, $feed_id, $purge_interval, $debug = false) { |
86 | ||
87 | $rows = -1; | |
4c193675 | 88 | |
fefa6ca3 | 89 | if (DB_TYPE == "pgsql") { |
44e241cb | 90 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 91 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 92 | (SELECT date_entered FROM ttrss_entries WHERE |
44e241cb AD |
93 | id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */ |
94 | ||
6e7f8d26 AD |
95 | $pg_version = get_pgsql_version($link); |
96 | ||
97 | if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) { | |
1e59ae35 AD |
98 | |
99 | $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
100 | ttrss_entries.id = ref_id AND | |
101 | marked = false AND | |
102 | feed_id = '$feed_id' AND | |
103 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); | |
104 | ||
105 | } else { | |
106 | ||
107 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
108 | USING ttrss_entries | |
109 | WHERE ttrss_entries.id = ref_id AND | |
110 | marked = false AND | |
111 | feed_id = '$feed_id' AND | |
fc774155 | 112 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); |
1e59ae35 | 113 | } |
ad507f85 AD |
114 | |
115 | $rows = pg_affected_rows($result); | |
116 | ||
fefa6ca3 | 117 | } else { |
1e59ae35 | 118 | |
30f1746f | 119 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 120 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 121 | (SELECT date_entered FROM ttrss_entries WHERE |
30f1746f AD |
122 | id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */ |
123 | ||
124 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
125 | USING ttrss_user_entries, ttrss_entries | |
126 | WHERE ttrss_entries.id = ref_id AND | |
127 | marked = false AND | |
128 | feed_id = '$feed_id' AND | |
129 | ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); | |
130 | ||
ad507f85 AD |
131 | $rows = mysql_affected_rows($link); |
132 | ||
133 | } | |
134 | ||
135 | if ($debug) { | |
6f9e33e4 | 136 | _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles"); |
fefa6ca3 AD |
137 | } |
138 | } | |
139 | ||
44e241cb AD |
140 | function global_purge_old_posts($link, $do_output = false, $limit = false) { |
141 | ||
894ebcf5 | 142 | $random_qpart = sql_random_function(); |
fefa6ca3 | 143 | |
44e241cb AD |
144 | if ($limit) { |
145 | $limit_qpart = "LIMIT $limit"; | |
146 | } else { | |
147 | $limit_qpart = ""; | |
148 | } | |
149 | ||
fefa6ca3 | 150 | $result = db_query($link, |
44e241cb AD |
151 | "SELECT id,purge_interval,owner_uid FROM ttrss_feeds |
152 | ORDER BY $random_qpart $limit_qpart"); | |
fefa6ca3 AD |
153 | |
154 | while ($line = db_fetch_assoc($result)) { | |
155 | ||
156 | $feed_id = $line["id"]; | |
157 | $purge_interval = $line["purge_interval"]; | |
158 | $owner_uid = $line["owner_uid"]; | |
159 | ||
160 | if ($purge_interval == 0) { | |
161 | ||
162 | $tmp_result = db_query($link, | |
163 | "SELECT value FROM ttrss_user_prefs WHERE | |
164 | pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'"); | |
165 | ||
166 | if (db_num_rows($tmp_result) != 0) { | |
167 | $purge_interval = db_fetch_result($tmp_result, 0, "value"); | |
168 | } | |
169 | } | |
170 | ||
171 | if ($do_output) { | |
ad507f85 | 172 | // print "Feed $feed_id: purge interval = $purge_interval\n"; |
fefa6ca3 AD |
173 | } |
174 | ||
175 | if ($purge_interval > 0) { | |
ad507f85 | 176 | purge_feed($link, $feed_id, $purge_interval, $do_output); |
fefa6ca3 AD |
177 | } |
178 | } | |
179 | ||
71604ca4 | 180 | // purge orphaned posts in main content table |
dab52d7b | 181 | $result = db_query($link, "DELETE FROM ttrss_entries WHERE |
71604ca4 AD |
182 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); |
183 | ||
dab52d7b AD |
184 | if ($do_output) { |
185 | $rows = db_affected_rows($link, $result); | |
186 | _debug("Purged $rows orphaned posts."); | |
187 | } | |
188 | ||
fefa6ca3 AD |
189 | } |
190 | ||
b6eefba5 | 191 | function purge_old_posts($link) { |
5d73494a | 192 | |
f1a80dae AD |
193 | $user_id = $_SESSION["uid"]; |
194 | ||
195 | $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds | |
196 | WHERE owner_uid = '$user_id'"); | |
5d73494a AD |
197 | |
198 | while ($line = db_fetch_assoc($result)) { | |
199 | ||
200 | $feed_id = $line["id"]; | |
201 | $purge_interval = $line["purge_interval"]; | |
202 | ||
b619ff15 | 203 | if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS'); |
5d73494a | 204 | |
140aae81 | 205 | if ($purge_interval > 0) { |
fefa6ca3 | 206 | purge_feed($link, $feed_id, $purge_interval); |
5d73494a AD |
207 | } |
208 | } | |
71604ca4 AD |
209 | |
210 | // purge orphaned posts in main content table | |
211 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
212 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
c3a8d71a AD |
213 | } |
214 | ||
c7d57b66 AD |
215 | function get_feed_update_interval($link, $feed_id) { |
216 | $result = db_query($link, "SELECT owner_uid, update_interval FROM | |
217 | ttrss_feeds WHERE id = '$feed_id'"); | |
218 | ||
219 | if (db_num_rows($result) == 1) { | |
220 | $update_interval = db_fetch_result($result, 0, "update_interval"); | |
221 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
222 | ||
223 | if ($update_interval != 0) { | |
224 | return $update_interval; | |
225 | } else { | |
226 | return get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $owner_uid, false); | |
227 | } | |
228 | ||
229 | } else { | |
230 | return -1; | |
231 | } | |
232 | } | |
233 | ||
1f2b01ed | 234 | function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) { |
40d13c28 | 235 | |
4769ddaf | 236 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 237 | |
a2770077 AD |
238 | if (!$user_id) { |
239 | $user_id = $_SESSION["uid"]; | |
240 | purge_old_posts($link); | |
241 | } | |
242 | ||
25af8dad | 243 | // db_query($link, "BEGIN"); |
b82af8c3 | 244 | |
cbd8650d AD |
245 | if (MAX_UPDATE_TIME > 0) { |
246 | if (DB_TYPE == "mysql") { | |
247 | $q_order = "RAND()"; | |
248 | } else { | |
249 | $q_order = "RANDOM()"; | |
250 | } | |
251 | } else { | |
252 | $q_order = "last_updated DESC"; | |
253 | } | |
254 | ||
d148926e | 255 | $result = db_query($link, "SELECT feed_url,id, |
798f722b | 256 | SUBSTRING(last_updated,1,19) AS last_updated, |
5c563acd | 257 | update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id' |
cbd8650d AD |
258 | ORDER BY $q_order"); |
259 | ||
260 | $upd_start = time(); | |
40d13c28 | 261 | |
b6eefba5 | 262 | while ($line = db_fetch_assoc($result)) { |
d148926e AD |
263 | $upd_intl = $line["update_interval"]; |
264 | ||
b619ff15 | 265 | if (!$upd_intl || $upd_intl == 0) { |
e289ca71 | 266 | $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false); |
b619ff15 | 267 | } |
d148926e | 268 | |
c1e202b7 AD |
269 | if ($upd_intl < 0) { |
270 | // Updates for this feed are disabled | |
271 | continue; | |
272 | } | |
273 | ||
93d40f50 AD |
274 | if ($fetch || (!$line["last_updated"] || |
275 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) { | |
c5142cca | 276 | |
cbd8650d AD |
277 | // print "<!-- feed: ".$line["feed_url"]." -->"; |
278 | ||
1f2b01ed | 279 | update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon); |
cbd8650d AD |
280 | |
281 | $upd_elapsed = time() - $upd_start; | |
282 | ||
283 | if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) { | |
284 | return; | |
285 | } | |
d148926e | 286 | } |
40d13c28 AD |
287 | } |
288 | ||
25af8dad | 289 | // db_query($link, "COMMIT"); |
b82af8c3 | 290 | |
40d13c28 AD |
291 | } |
292 | ||
4065b60b AD |
293 | function fetch_file_contents($url) { |
294 | if (USE_CURL_FOR_ICONS) { | |
295 | $tmpfile = tempnam(TMP_DIRECTORY, "ttrss-tmp"); | |
296 | ||
297 | $ch = curl_init($url); | |
298 | $fp = fopen($tmpfile, "w"); | |
299 | ||
300 | if ($fp) { | |
301 | curl_setopt($ch, CURLOPT_FILE, $fp); | |
dd966fed AD |
302 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); |
303 | curl_setopt($ch, CURLOPT_TIMEOUT, 45); | |
4065b60b AD |
304 | curl_exec($ch); |
305 | curl_close($ch); | |
306 | fclose($fp); | |
307 | } | |
308 | ||
309 | $contents = file_get_contents($tmpfile); | |
310 | unlink($tmpfile); | |
311 | ||
312 | return $contents; | |
313 | ||
314 | } else { | |
315 | return file_get_contents($url); | |
316 | } | |
317 | ||
318 | } | |
78800912 | 319 | |
4065b60b AD |
320 | // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/) |
321 | // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php | |
78800912 | 322 | |
4065b60b | 323 | function get_favicon_url($url) { |
99331724 | 324 | |
4065b60b | 325 | if ($html = @fetch_file_contents($url)) { |
78800912 | 326 | |
4065b60b AD |
327 | if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) { |
328 | // Attempt to grab a favicon link from their webpage url | |
329 | $linkUrl = html_entity_decode($matches[1]); | |
c798704b | 330 | |
4065b60b AD |
331 | if (substr($linkUrl, 0, 1) == '/') { |
332 | $urlParts = parse_url($url); | |
333 | $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl; | |
334 | } else if (substr($linkUrl, 0, 7) == 'http://') { | |
335 | $faviconURL = $linkUrl; | |
336 | } else if (substr($url, -1, 1) == '/') { | |
337 | $faviconURL = $url.$linkUrl; | |
338 | } else { | |
339 | $faviconURL = $url.'/'.$linkUrl; | |
e695fdc8 | 340 | } |
717f5e64 | 341 | |
c798704b | 342 | } else { |
4065b60b AD |
343 | // If unsuccessful, attempt to "guess" the favicon location |
344 | $urlParts = parse_url($url); | |
345 | $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico'; | |
346 | } | |
347 | } | |
c798704b | 348 | |
4065b60b AD |
349 | // Run a test to see if what we have attempted to get actually exists. |
350 | if(USE_CURL_FOR_ICONS || url_validate($faviconURL)) { | |
351 | return $faviconURL; | |
352 | } else { | |
353 | return false; | |
354 | } | |
355 | } | |
356 | ||
357 | function url_validate($link) { | |
358 | ||
359 | $url_parts = @parse_url($link); | |
360 | ||
361 | if ( empty( $url_parts["host"] ) ) | |
362 | return false; | |
363 | ||
364 | if ( !empty( $url_parts["path"] ) ) { | |
365 | $documentpath = $url_parts["path"]; | |
366 | } else { | |
367 | $documentpath = "/"; | |
368 | } | |
369 | ||
370 | if ( !empty( $url_parts["query"] ) ) | |
371 | $documentpath .= "?" . $url_parts["query"]; | |
372 | ||
373 | $host = $url_parts["host"]; | |
374 | $port = $url_parts["port"]; | |
375 | ||
376 | if ( empty($port) ) | |
377 | $port = "80"; | |
378 | ||
379 | $socket = @fsockopen( $host, $port, $errno, $errstr, 30 ); | |
380 | ||
381 | if ( !$socket ) | |
382 | return false; | |
c798704b | 383 | |
4065b60b AD |
384 | fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n"); |
385 | ||
386 | $http_response = fgets( $socket, 22 ); | |
387 | ||
388 | $responses = "/(200 OK)|(30[0-9] Moved)/"; | |
389 | if ( preg_match($responses, $http_response) ) { | |
390 | fclose($socket); | |
391 | return true; | |
392 | } else { | |
393 | return false; | |
394 | } | |
395 | ||
396 | } | |
397 | ||
398 | function check_feed_favicon($site_url, $feed, $link) { | |
399 | $favicon_url = get_favicon_url($site_url); | |
400 | ||
401 | # print "FAVICON [$site_url]: $favicon_url\n"; | |
402 | ||
403 | error_reporting(0); | |
404 | ||
405 | $icon_file = ICONS_DIR . "/$feed.ico"; | |
406 | ||
407 | if ($favicon_url && !file_exists($icon_file)) { | |
408 | $contents = fetch_file_contents($favicon_url); | |
409 | ||
410 | $fp = fopen($icon_file, "w"); | |
78800912 | 411 | |
4065b60b AD |
412 | if ($fp) { |
413 | fwrite($fp, $contents); | |
414 | fclose($fp); | |
415 | chmod($icon_file, 0644); | |
416 | } | |
78800912 | 417 | } |
4065b60b AD |
418 | |
419 | error_reporting(DEFAULT_ERROR_LEVEL); | |
420 | ||
78800912 AD |
421 | } |
422 | ||
ddb68b81 | 423 | function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) { |
40d13c28 | 424 | |
ddb68b81 | 425 | if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) { |
21cfcdf2 AD |
426 | return; |
427 | } | |
428 | ||
4bc64807 | 429 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
34e420fb | 430 | _debug("update_rss_feed: start"); |
219bd8fc AD |
431 | } |
432 | ||
bc0f0785 | 433 | $result = db_query($link, "SELECT update_interval,auth_login,auth_pass,cache_images |
a88c1f36 AD |
434 | FROM ttrss_feeds WHERE id = '$feed'"); |
435 | ||
47439031 AD |
436 | $auth_login = db_fetch_result($result, 0, "auth_login"); |
437 | $auth_pass = db_fetch_result($result, 0, "auth_pass"); | |
47c6c988 | 438 | |
a88c1f36 | 439 | $update_interval = db_fetch_result($result, 0, "update_interval"); |
bc0f0785 | 440 | $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images")); |
a88c1f36 AD |
441 | |
442 | if ($update_interval < 0) { return; } | |
443 | ||
ab3d0b99 AD |
444 | $feed = db_escape_string($feed); |
445 | ||
47c6c988 AD |
446 | $fetch_url = $feed_url; |
447 | ||
448 | if ($auth_login && $auth_pass) { | |
449 | $url_parts = array(); | |
450 | preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts); | |
451 | ||
452 | if ($url_parts[1] && $url_parts[2]) { | |
453 | $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2]; | |
454 | } | |
455 | ||
456 | } | |
ab3d0b99 | 457 | |
4bc64807 | 458 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
ca872b9d | 459 | _debug("update_rss_feed: fetching [$fetch_url]..."); |
219bd8fc AD |
460 | } |
461 | ||
9fdf7824 | 462 | if (!defined('DAEMON_EXTENDED_DEBUG') && !$_GET['xdebug']) { |
219bd8fc AD |
463 | error_reporting(0); |
464 | } | |
465 | ||
9fdf7824 AD |
466 | if (!ENABLE_SIMPLEPIE) { |
467 | $rss = fetch_rss($fetch_url); | |
468 | } else { | |
c7d57b66 AD |
469 | if (!is_dir(SIMPLEPIE_CACHE_DIR)) { |
470 | mkdir(SIMPLEPIE_CACHE_DIR); | |
471 | } | |
472 | ||
ca872b9d AD |
473 | $rss = new SimplePie(); |
474 | $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT); | |
03a577bf | 475 | $rss->set_timeout(MAGPIE_FETCH_TIME_OUT); |
ca872b9d AD |
476 | $rss->set_feed_url($fetch_url); |
477 | $rss->set_output_encoding('UTF-8'); | |
c7d57b66 | 478 | |
bc0f0785 AD |
479 | if (SIMPLEPIE_CACHE_IMAGES && $cache_images) { |
480 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
481 | _debug("enabling image cache"); | |
482 | } | |
483 | ||
dab52d7b | 484 | $rss->set_image_handler('./image.php', 'i'); |
bc0f0785 | 485 | } |
dab52d7b | 486 | |
c7d57b66 AD |
487 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
488 | _debug("feed update interval (sec): " . | |
489 | get_feed_update_interval($link, $feed)*60); | |
490 | } | |
491 | ||
492 | if (is_dir(SIMPLEPIE_CACHE_DIR)) { | |
493 | $rss->set_cache_location(SIMPLEPIE_CACHE_DIR); | |
494 | $rss->set_cache_duration(get_feed_update_interval($link, $feed) * 60); | |
495 | } | |
496 | ||
9fdf7824 | 497 | $rss->init(); |
9fdf7824 AD |
498 | } |
499 | ||
500 | // print_r($rss); | |
219bd8fc | 501 | |
4bc64807 | 502 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
34e420fb | 503 | _debug("update_rss_feed: fetch done, parsing..."); |
219bd8fc AD |
504 | } else { |
505 | error_reporting (DEFAULT_ERROR_LEVEL); | |
506 | } | |
507 | ||
b6eefba5 | 508 | $feed = db_escape_string($feed); |
dcee8f61 | 509 | |
ca872b9d AD |
510 | if (ENABLE_SIMPLEPIE) { |
511 | $fetch_ok = !$rss->error(); | |
512 | } else { | |
513 | $fetch_ok = !!$rss; | |
514 | } | |
515 | ||
516 | if ($fetch_ok) { | |
50b62214 | 517 | |
4bc64807 | 518 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
50b62214 AD |
519 | _debug("update_rss_feed: processing feed data..."); |
520 | } | |
521 | ||
44e241cb | 522 | // db_query($link, "BEGIN"); |
dd8c76a9 | 523 | |
a88c1f36 | 524 | $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid |
f324892e | 525 | FROM ttrss_feeds WHERE id = '$feed'"); |
331900c6 | 526 | |
b6eefba5 AD |
527 | $registered_title = db_fetch_result($result, 0, "title"); |
528 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
f324892e | 529 | $orig_site_url = db_fetch_result($result, 0, "site_url"); |
331900c6 | 530 | |
7fed1940 AD |
531 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); |
532 | ||
9fdf7824 AD |
533 | if (ENABLE_SIMPLEPIE) { |
534 | $site_url = $rss->get_link(); | |
535 | } else { | |
536 | $site_url = $rss->channel["link"]; | |
537 | } | |
538 | ||
8d0ec6fd | 539 | if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) { |
4bc64807 | 540 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
50b62214 AD |
541 | _debug("update_rss_feed: checking favicon..."); |
542 | } | |
9fdf7824 AD |
543 | |
544 | check_feed_favicon($site_url, $feed, $link); | |
a2770077 AD |
545 | } |
546 | ||
746b249f | 547 | if (!$registered_title || $registered_title == "[Unknown]") { |
4bc64807 | 548 | |
fb486a33 | 549 | if (ENABLE_SIMPLEPIE) { |
c2f8aac4 | 550 | $feed_title = db_escape_string($rss->get_title()); |
fb486a33 AD |
551 | } else { |
552 | $feed_title = db_escape_string($rss->channel["title"]); | |
553 | } | |
4bc64807 AD |
554 | |
555 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
556 | _debug("update_rss_feed: registering title: $feed_title"); | |
557 | } | |
7c5a308d | 558 | |
f324892e AD |
559 | db_query($link, "UPDATE ttrss_feeds SET |
560 | title = '$feed_title' WHERE id = '$feed'"); | |
561 | } | |
562 | ||
49f9c923 | 563 | // weird, weird Magpie |
9fdf7824 AD |
564 | if (!ENABLE_SIMPLEPIE) { |
565 | if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]); | |
566 | } | |
147f7691 AD |
567 | |
568 | if ($site_url && $orig_site_url != db_escape_string($site_url)) { | |
f324892e AD |
569 | db_query($link, "UPDATE ttrss_feeds SET |
570 | site_url = '$site_url' WHERE id = '$feed'"); | |
331900c6 | 571 | } |
40d13c28 | 572 | |
b7f4bda2 AD |
573 | // print "I: " . $rss->channel["image"]["url"]; |
574 | ||
9fdf7824 AD |
575 | if (!ENABLE_SIMPLEPIE) { |
576 | $icon_url = $rss->image["url"]; | |
577 | } else { | |
578 | $icon_url = $rss->get_image_url(); | |
579 | } | |
b7f4bda2 | 580 | |
147f7691 | 581 | if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) { |
b6eefba5 AD |
582 | $icon_url = db_escape_string($icon_url); |
583 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
b7f4bda2 AD |
584 | } |
585 | ||
51e456d6 | 586 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
50b62214 AD |
587 | _debug("update_rss_feed: loading filters..."); |
588 | } | |
e6155a06 AD |
589 | |
590 | $filters = array(); | |
591 | ||
4b3dff6e | 592 | $result = db_query($link, "SELECT reg_exp, |
db42b934 | 593 | ttrss_filter_types.name AS name, |
073ca0e6 | 594 | ttrss_filter_actions.name AS action, |
c2d9322b | 595 | inverse, |
073ca0e6 | 596 | action_param |
db42b934 | 597 | FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE |
e8b79d16 | 598 | enabled = true AND |
db42b934 AD |
599 | owner_uid = $owner_uid AND |
600 | ttrss_filter_types.id = filter_type AND | |
601 | ttrss_filter_actions.id = action_id AND | |
f8382011 | 602 | (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp"); |
e6155a06 | 603 | |
b6eefba5 | 604 | while ($line = db_fetch_assoc($result)) { |
e6155a06 | 605 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); |
19c9cb11 AD |
606 | |
607 | $filter["reg_exp"] = $line["reg_exp"]; | |
608 | $filter["action"] = $line["action"]; | |
073ca0e6 | 609 | $filter["action_param"] = $line["action_param"]; |
c2d9322b | 610 | $filter["inverse"] = sql_bool_to_bool($line["inverse"]); |
073ca0e6 | 611 | |
19c9cb11 | 612 | array_push($filters[$line["name"]], $filter); |
e6155a06 AD |
613 | } |
614 | ||
9fdf7824 AD |
615 | if (ENABLE_SIMPLEPIE) { |
616 | $iterator = $rss->get_items(); | |
617 | } else { | |
618 | $iterator = $rss->items; | |
619 | if (!$iterator || !is_array($iterator)) $iterator = $rss->entries; | |
620 | if (!$iterator || !is_array($iterator)) $iterator = $rss; | |
621 | } | |
c22789da AD |
622 | |
623 | if (!is_array($iterator)) { | |
39541e74 | 624 | /* db_query($link, "UPDATE ttrss_feeds |
75bd0669 | 625 | SET last_error = 'Parse error: can\'t find any articles.' |
77f0a2a7 AD |
626 | WHERE id = '$feed'"); */ |
627 | ||
628 | // clear any errors and mark feed as updated if fetched okay | |
629 | // even if it's blank | |
630 | ||
51e456d6 | 631 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
844012bc AD |
632 | _debug("update_rss_feed: entry iterator is not an array, no articles?"); |
633 | } | |
634 | ||
77f0a2a7 AD |
635 | db_query($link, "UPDATE ttrss_feeds |
636 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
637 | ||
638 | return; // no articles | |
c22789da | 639 | } |
ddb68b81 | 640 | |
51e456d6 | 641 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
50b62214 AD |
642 | _debug("update_rss_feed: processing articles..."); |
643 | } | |
644 | ||
ddb68b81 | 645 | foreach ($iterator as $item) { |
7c5a308d | 646 | |
9fdf7824 AD |
647 | if (ENABLE_SIMPLEPIE) { |
648 | $entry_guid = $item->get_id(); | |
649 | if (!$entry_guid) $entry_guid = $item->get_link(); | |
650 | if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title()); | |
651 | ||
652 | } else { | |
653 | ||
654 | $entry_guid = $item["id"]; | |
34e420fb | 655 | |
9fdf7824 AD |
656 | if (!$entry_guid) $entry_guid = $item["guid"]; |
657 | if (!$entry_guid) $entry_guid = $item["link"]; | |
658 | if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]); | |
659 | } | |
be832a1a | 660 | |
9fdf7824 | 661 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
34e420fb AD |
662 | _debug("update_rss_feed: guid $entry_guid"); |
663 | } | |
664 | ||
be832a1a AD |
665 | if (!$entry_guid) continue; |
666 | ||
667 | $entry_timestamp = ""; | |
668 | ||
9fdf7824 AD |
669 | if (ENABLE_SIMPLEPIE) { |
670 | $entry_timestamp = strtotime($item->get_date()); | |
671 | } else { | |
672 | $rss_2_date = $item['pubdate']; | |
673 | $rss_1_date = $item['dc']['date']; | |
674 | $atom_date = $item['issued']; | |
675 | if (!$atom_date) $atom_date = $item['updated']; | |
be832a1a | 676 | |
9fdf7824 AD |
677 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); |
678 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
679 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
680 | } | |
9bb36aa0 AD |
681 | |
682 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
683 | _debug("update_rss_feed: date $entry_timestamp"); | |
684 | } | |
685 | ||
2e930846 | 686 | if ($entry_timestamp == "" || $entry_timestamp == -1 || !$entry_timestamp) { |
be832a1a AD |
687 | $entry_timestamp = time(); |
688 | $no_orig_date = 'true'; | |
689 | } else { | |
690 | $no_orig_date = 'false'; | |
691 | } | |
692 | ||
693 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
694 | ||
9fdf7824 AD |
695 | if (ENABLE_SIMPLEPIE) { |
696 | $entry_title = $item->get_title(); | |
697 | } else { | |
698 | $entry_title = trim(strip_tags($item["title"])); | |
699 | } | |
be832a1a | 700 | |
9fdf7824 AD |
701 | if (ENABLE_SIMPLEPIE) { |
702 | $entry_link = $item->get_link(); | |
703 | } else { | |
704 | // strange Magpie workaround | |
705 | $entry_link = $item["link_"]; | |
706 | if (!$entry_link) $entry_link = $item["link"]; | |
707 | } | |
be832a1a | 708 | |
9bb36aa0 AD |
709 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
710 | _debug("update_rss_feed: title $entry_title"); | |
711 | } | |
712 | ||
713 | if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);; | |
7d3ab0dd | 714 | |
be832a1a | 715 | $entry_link = strip_tags($entry_link); |
7d3ab0dd | 716 | |
9fdf7824 AD |
717 | if (ENABLE_SIMPLEPIE) { |
718 | $entry_content = $item->get_description(); | |
719 | } else { | |
720 | $entry_content = $item["content:escaped"]; | |
721 | ||
722 | if (!$entry_content) $entry_content = $item["content:encoded"]; | |
723 | if (!$entry_content) $entry_content = $item["content"]; | |
724 | if (!$entry_content) $entry_content = $item["atom_content"]; | |
725 | if (!$entry_content) $entry_content = $item["summary"]; | |
726 | if (!$entry_content) $entry_content = $item["description"]; | |
727 | ||
728 | // WTF | |
729 | if (is_array($entry_content)) { | |
730 | $entry_content = $entry_content["encoded"]; | |
731 | if (!$entry_content) $entry_content = $entry_content["escaped"]; | |
732 | } | |
be832a1a AD |
733 | } |
734 | ||
735 | // print_r($item); | |
736 | // print_r(htmlspecialchars($entry_content)); | |
737 | // print "<br>"; | |
738 | ||
739 | $entry_content_unescaped = $entry_content; | |
be832a1a | 740 | |
9fdf7824 AD |
741 | if (ENABLE_SIMPLEPIE) { |
742 | $entry_comments = strip_tags($item->data["comments"]); | |
30cf38dd AD |
743 | if ($item->get_author()) { |
744 | $entry_author = $item->get_author()->get_name(); | |
745 | } | |
9fdf7824 AD |
746 | } else { |
747 | $entry_comments = strip_tags($item["comments"]); | |
748 | ||
749 | $entry_author = db_escape_string(strip_tags($item['dc']['creator'])); | |
4d6e9157 | 750 | |
9fdf7824 AD |
751 | if ($item['author']) { |
752 | ||
753 | if (is_array($item['author'])) { | |
754 | ||
755 | if (!$entry_author) { | |
756 | $entry_author = db_escape_string(strip_tags($item['author']['name'])); | |
757 | } | |
758 | ||
759 | if (!$entry_author) { | |
760 | $entry_author = db_escape_string(strip_tags($item['author']['email'])); | |
761 | } | |
4d6e9157 | 762 | } |
9fdf7824 | 763 | |
4d6e9157 | 764 | if (!$entry_author) { |
9fdf7824 | 765 | $entry_author = db_escape_string(strip_tags($item['author'])); |
4d6e9157 | 766 | } |
83f114c8 | 767 | } |
be832a1a AD |
768 | } |
769 | ||
83f114c8 AD |
770 | if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = ''; |
771 | ||
be832a1a AD |
772 | $entry_guid = db_escape_string(strip_tags($entry_guid)); |
773 | ||
774 | $result = db_query($link, "SELECT id FROM ttrss_entries | |
775 | WHERE guid = '$entry_guid'"); | |
776 | ||
777 | $entry_content = db_escape_string($entry_content); | |
7e43ad58 AD |
778 | |
779 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); | |
780 | ||
be832a1a AD |
781 | $entry_title = db_escape_string($entry_title); |
782 | $entry_link = db_escape_string($entry_link); | |
2544f36b AD |
783 | $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250); |
784 | $entry_author = mb_substr($entry_author, 0, 250); | |
be832a1a | 785 | |
9fdf7824 AD |
786 | if (ENABLE_SIMPLEPIE) { |
787 | $num_comments = 0; #FIXME# | |
788 | } else { | |
789 | $num_comments = db_escape_string($item["slash"]["comments"]); | |
790 | } | |
be832a1a AD |
791 | |
792 | if (!$num_comments) $num_comments = 0; | |
793 | ||
fefef828 | 794 | // parse <category> entries into tags |
be832a1a | 795 | |
9fdf7824 | 796 | if (ENABLE_SIMPLEPIE) { |
be832a1a | 797 | |
fefef828 | 798 | $additional_tags = array(); |
9fdf7824 AD |
799 | $additional_tags_src = $item->get_categories(); |
800 | ||
a702e931 AD |
801 | if (is_array($additional_tags_src)) { |
802 | foreach ($additional_tags_src as $tobj) { | |
803 | array_push($additional_tags, $tobj->get_term()); | |
804 | } | |
fefef828 | 805 | } |
fefef828 | 806 | |
6af621c7 AD |
807 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
808 | _debug("update_rss_feed: category tags:"); | |
809 | print_r($additional_tags); | |
810 | } | |
811 | ||
9fdf7824 | 812 | } else { |
fefef828 | 813 | |
9fdf7824 | 814 | $t_ctr = $item['category#']; |
fefef828 | 815 | |
9fdf7824 AD |
816 | $additional_tags = false; |
817 | ||
818 | if ($t_ctr == 0) { | |
819 | $additional_tags = false; | |
820 | } else if ($t_ctr == 1) { | |
821 | $additional_tags = array($item['category']); | |
822 | } else { | |
823 | $additional_tags = array(); | |
824 | for ($i = 0; $i <= $t_ctr; $i++ ) { | |
825 | if ($item["category#$i"]) { | |
826 | array_push($additional_tags, $item["category#$i"]); | |
827 | } | |
828 | } | |
829 | } | |
830 | ||
831 | // parse <dc:subject> elements | |
832 | ||
833 | $t_ctr = $item['dc']['subject#']; | |
834 | ||
835 | if ($t_ctr == 1) { | |
836 | $additional_tags = array($item['dc']['subject']); | |
837 | } else if ($t_ctr > 1) { | |
838 | $additional_tags = array(); | |
839 | for ($i = 0; $i <= $t_ctr; $i++ ) { | |
840 | if ($item['dc']["subject#$i"]) { | |
841 | array_push($additional_tags, $item['dc']["subject#$i"]); | |
842 | } | |
fefef828 AD |
843 | } |
844 | } | |
845 | } | |
8add756a | 846 | |
d48d160c | 847 | # sanitize content |
183ad07b | 848 | |
007a38d4 | 849 | // $entry_content = sanitize_rss($entry_content); |
d48d160c | 850 | |
9fdf7824 | 851 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
34e420fb AD |
852 | _debug("update_rss_feed: done collecting data [TITLE:$entry_title]"); |
853 | } | |
854 | ||
44e241cb AD |
855 | db_query($link, "BEGIN"); |
856 | ||
4c193675 AD |
857 | if (db_num_rows($result) == 0) { |
858 | ||
9fdf7824 | 859 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
34e420fb AD |
860 | _debug("update_rss_feed: base guid not found"); |
861 | } | |
862 | ||
4c193675 AD |
863 | // base post entry does not exist, create it |
864 | ||
4c193675 AD |
865 | $result = db_query($link, |
866 | "INSERT INTO ttrss_entries | |
867 | (title, | |
868 | guid, | |
869 | link, | |
870 | updated, | |
871 | content, | |
872 | content_hash, | |
873 | no_orig_date, | |
874 | date_entered, | |
11b0dce2 | 875 | comments, |
b6104dee AD |
876 | num_comments, |
877 | author) | |
4c193675 AD |
878 | VALUES |
879 | ('$entry_title', | |
880 | '$entry_guid', | |
881 | '$entry_link', | |
882 | '$entry_timestamp_fmt', | |
883 | '$entry_content', | |
884 | '$content_hash', | |
885 | $no_orig_date, | |
886 | NOW(), | |
11b0dce2 | 887 | '$entry_comments', |
b6104dee AD |
888 | '$num_comments', |
889 | '$entry_author')"); | |
8926aab8 AD |
890 | } else { |
891 | // we keep encountering the entry in feeds, so we need to | |
892 | // update date_entered column so that we don't get horrible | |
893 | // dupes when the entry gets purged and reinserted again e.g. | |
894 | // in the case of SLOW SLOW OMG SLOW updating feeds | |
895 | ||
896 | $base_entry_id = db_fetch_result($result, 0, "id"); | |
897 | ||
898 | db_query($link, "UPDATE ttrss_entries SET date_entered = NOW() | |
899 | WHERE id = '$base_entry_id'"); | |
4c193675 AD |
900 | } |
901 | ||
902 | // now it should exist, if not - bad luck then | |
903 | ||
6385315d AD |
904 | $result = db_query($link, "SELECT |
905 | id,content_hash,no_orig_date,title, | |
8926aab8 | 906 | substring(date_entered,1,19) as date_entered, |
11b0dce2 AD |
907 | substring(updated,1,19) as updated, |
908 | num_comments | |
6385315d AD |
909 | FROM |
910 | ttrss_entries | |
911 | WHERE guid = '$entry_guid'"); | |
4c193675 AD |
912 | |
913 | if (db_num_rows($result) == 1) { | |
914 | ||
9fdf7824 | 915 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
7ca91eb3 | 916 | _debug("update_rss_feed: base guid found, checking for user record"); |
34e420fb AD |
917 | } |
918 | ||
11b0dce2 AD |
919 | // this will be used below in update handler |
920 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); | |
921 | $orig_title = db_fetch_result($result, 0, "title"); | |
922 | $orig_num_comments = db_fetch_result($result, 0, "num_comments"); | |
8926aab8 AD |
923 | $orig_date_entered = strtotime(db_fetch_result($result, |
924 | 0, "date_entered")); | |
6385315d | 925 | |
11b0dce2 | 926 | $ref_id = db_fetch_result($result, 0, "id"); |
4c193675 | 927 | |
11b0dce2 | 928 | // check for user post link to main table |
4c193675 | 929 | |
11b0dce2 | 930 | // do we allow duplicate posts with same GUID in different feeds? |
8d0ec6fd | 931 | if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) { |
11b0dce2 AD |
932 | $dupcheck_qpart = "AND feed_id = '$feed'"; |
933 | } else { | |
934 | $dupcheck_qpart = ""; | |
935 | } | |
71604ca4 | 936 | |
11b0dce2 | 937 | // error_reporting(0); |
19c9cb11 | 938 | |
f8382011 AD |
939 | $article_filters = get_article_filters($filters, $entry_title, |
940 | $entry_content, $entry_link); | |
19c9cb11 | 941 | |
9fdf7824 | 942 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
7ca91eb3 AD |
943 | _debug("update_rss_feed: article filters: "); |
944 | if (count($article_filters) != 0) { | |
945 | print_r($article_filters); | |
946 | } | |
947 | } | |
948 | ||
f8382011 | 949 | if (find_article_filter($article_filters, "filter")) { |
11b0dce2 AD |
950 | continue; |
951 | } | |
19c9cb11 | 952 | |
11b0dce2 | 953 | // error_reporting (DEFAULT_ERROR_LEVEL); |
3a933f22 | 954 | |
11b0dce2 AD |
955 | $result = db_query($link, |
956 | "SELECT ref_id FROM ttrss_user_entries WHERE | |
957 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' | |
958 | $dupcheck_qpart"); | |
7ca91eb3 | 959 | |
11b0dce2 AD |
960 | // okay it doesn't exist - create user entry |
961 | if (db_num_rows($result) == 0) { | |
962 | ||
9fdf7824 | 963 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
7ca91eb3 AD |
964 | _debug("update_rss_feed: user record not found, creating..."); |
965 | } | |
966 | ||
f8382011 | 967 | if (!find_article_filter($article_filters, 'catchup')) { |
11b0dce2 AD |
968 | $unread = 'true'; |
969 | $last_read_qpart = 'NULL'; | |
970 | } else { | |
971 | $unread = 'false'; | |
972 | $last_read_qpart = 'NOW()'; | |
973 | } | |
dd7d3187 | 974 | |
f8382011 | 975 | if (find_article_filter($article_filters, 'mark')) { |
dd7d3187 AD |
976 | $marked = 'true'; |
977 | } else { | |
978 | $marked = 'false'; | |
979 | } | |
a36c0dfe AD |
980 | |
981 | if (find_article_filter($article_filters, 'publish')) { | |
982 | $published = 'true'; | |
983 | } else { | |
984 | $published = 'false'; | |
985 | } | |
986 | ||
11b0dce2 AD |
987 | $result = db_query($link, |
988 | "INSERT INTO ttrss_user_entries | |
a36c0dfe | 989 | (ref_id, owner_uid, feed_id, unread, last_read, marked, published) |
11b0dce2 | 990 | VALUES ('$ref_id', '$owner_uid', '$feed', $unread, |
a36c0dfe | 991 | $last_read_qpart, $marked, $published)"); |
11b0dce2 AD |
992 | } |
993 | ||
6385315d AD |
994 | $post_needs_update = false; |
995 | ||
8d0ec6fd | 996 | if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) && |
6385315d | 997 | ($content_hash != $orig_content_hash)) { |
7e43ad58 | 998 | // print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->"; |
6385315d AD |
999 | $post_needs_update = true; |
1000 | } | |
1001 | ||
7e43ad58 | 1002 | if (db_escape_string($orig_title) != $entry_title) { |
6385315d AD |
1003 | $post_needs_update = true; |
1004 | } | |
1005 | ||
11b0dce2 AD |
1006 | if ($orig_num_comments != $num_comments) { |
1007 | $post_needs_update = true; | |
1008 | } | |
1009 | ||
6385315d AD |
1010 | // this doesn't seem to be very reliable |
1011 | // | |
1012 | // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) { | |
1013 | // $post_needs_update = true; | |
1014 | // } | |
1015 | ||
1016 | // if post needs update, update it and mark all user entries | |
1c73bc0c | 1017 | // linking to this post as updated |
6385315d AD |
1018 | if ($post_needs_update) { |
1019 | ||
7ca91eb3 AD |
1020 | if (defined('DAEMON_EXTENDED_DEBUG')) { |
1021 | _debug("update_rss_feed: post $entry_guid needs update..."); | |
1022 | } | |
1023 | ||
6385315d AD |
1024 | // print "<!-- post $orig_title needs update : $post_needs_update -->"; |
1025 | ||
6385315d | 1026 | db_query($link, "UPDATE ttrss_entries |
11b0dce2 | 1027 | SET title = '$entry_title', content = '$entry_content', |
7e43ad58 | 1028 | content_hash = '$content_hash', |
11b0dce2 | 1029 | num_comments = '$num_comments' |
6385315d AD |
1030 | WHERE id = '$ref_id'"); |
1031 | ||
8d0ec6fd | 1032 | if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) { |
4919fb42 AD |
1033 | db_query($link, "UPDATE ttrss_user_entries |
1034 | SET last_read = null, unread = true WHERE ref_id = '$ref_id'"); | |
1035 | } else { | |
1036 | db_query($link, "UPDATE ttrss_user_entries | |
1037 | SET last_read = null WHERE ref_id = '$ref_id' AND unread = false"); | |
1038 | } | |
6385315d AD |
1039 | |
1040 | } | |
4c193675 AD |
1041 | } |
1042 | ||
44e241cb AD |
1043 | db_query($link, "COMMIT"); |
1044 | ||
9fdf7824 | 1045 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
34e420fb AD |
1046 | _debug("update_rss_feed: looking for tags..."); |
1047 | } | |
1048 | ||
eb36b4eb | 1049 | /* taaaags */ |
40e1a95b | 1050 | // <a href="..." rel="tag">Xorg</a>, // |
eb36b4eb | 1051 | |
05732aa0 | 1052 | $entry_tags = null; |
eb36b4eb | 1053 | |
40e1a95b | 1054 | preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i", |
ee2c3050 AD |
1055 | $entry_content_unescaped, $entry_tags); |
1056 | ||
fefef828 AD |
1057 | /* print "<p><br/>$entry_title : $entry_content_unescaped<br>"; |
1058 | print_r($entry_tags); | |
1059 | print "<br/></p>"; */ | |
eb36b4eb AD |
1060 | |
1061 | $entry_tags = $entry_tags[1]; | |
1062 | ||
073ca0e6 AD |
1063 | # check for manual tags |
1064 | ||
f8382011 AD |
1065 | $tag_filter = find_article_filter($article_filters, "tag"); |
1066 | ||
1067 | if ($tag_filter) { | |
073ca0e6 | 1068 | |
f8382011 | 1069 | $manual_tags = trim_array(split(",", $tag_filter[1])); |
073ca0e6 | 1070 | |
f8382011 | 1071 | foreach ($manual_tags as $tag) { |
be832a1a AD |
1072 | if (tag_is_valid($tag)) { |
1073 | array_push($entry_tags, $tag); | |
1074 | } | |
1075 | } | |
1076 | } | |
1077 | ||
11c9ea1f AD |
1078 | $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link, |
1079 | 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8'))); | |
8fc70781 | 1080 | |
fefef828 AD |
1081 | if ($additional_tags && is_array($additional_tags)) { |
1082 | foreach ($additional_tags as $tag) { | |
156a785d AD |
1083 | if (tag_is_valid($tag) && |
1084 | array_search($tag, $boring_tags) === FALSE) { | |
073ca0e6 AD |
1085 | array_push($entry_tags, $tag); |
1086 | } | |
1087 | } | |
fefef828 AD |
1088 | } |
1089 | ||
fcc95e24 | 1090 | // print "<p>TAGS: "; print_r($entry_tags); print "</p>"; |
073ca0e6 | 1091 | |
9fdf7824 AD |
1092 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
1093 | print_r($entry_tags); | |
1094 | } | |
1095 | ||
eb36b4eb AD |
1096 | if (count($entry_tags) > 0) { |
1097 | ||
44e241cb AD |
1098 | db_query($link, "BEGIN"); |
1099 | ||
05732aa0 AD |
1100 | $result = db_query($link, "SELECT id,int_id |
1101 | FROM ttrss_entries,ttrss_user_entries | |
25da6909 | 1102 | WHERE guid = '$entry_guid' |
05732aa0 | 1103 | AND feed_id = '$feed' AND ref_id = id |
7fed1940 | 1104 | AND owner_uid = '$owner_uid'"); |
eb36b4eb | 1105 | |
fe99ab12 | 1106 | if (db_num_rows($result) == 1) { |
eb36b4eb | 1107 | |
fe99ab12 AD |
1108 | $entry_id = db_fetch_result($result, 0, "id"); |
1109 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
1110 | ||
1111 | foreach ($entry_tags as $tag) { | |
fefef828 | 1112 | |
14b6c54b | 1113 | $tag = sanitize_tag($tag); |
fefef828 | 1114 | $tag = db_escape_string($tag); |
31483fc1 | 1115 | |
ef063748 AD |
1116 | if (!tag_is_valid($tag)) continue; |
1117 | ||
fe99ab12 AD |
1118 | $result = db_query($link, "SELECT id FROM ttrss_tags |
1119 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND | |
1120 | owner_uid = '$owner_uid' LIMIT 1"); | |
1121 | ||
1122 | // print db_fetch_result($result, 0, "id"); | |
1123 | ||
1124 | if ($result && db_num_rows($result) == 0) { | |
1125 | ||
fe99ab12 AD |
1126 | db_query($link, "INSERT INTO ttrss_tags |
1127 | (owner_uid,tag_name,post_int_id) | |
1128 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
1129 | } | |
1130 | } | |
eb36b4eb | 1131 | } |
44e241cb | 1132 | db_query($link, "COMMIT"); |
05732aa0 | 1133 | } |
9fdf7824 AD |
1134 | |
1135 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1136 | _debug("update_rss_feed: article processed"); | |
1137 | } | |
4c193675 | 1138 | } |
40d13c28 | 1139 | |
ab3d0b99 AD |
1140 | db_query($link, "UPDATE ttrss_feeds |
1141 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
eb36b4eb | 1142 | |
44e241cb | 1143 | // db_query($link, "COMMIT"); |
dd8c76a9 | 1144 | |
ab3d0b99 | 1145 | } else { |
ca872b9d | 1146 | |
9fdf7824 | 1147 | if (ENABLE_SIMPLEPIE) { |
ca872b9d | 1148 | $error_msg = mb_substr($rss->error(), 0, 250); |
9fdf7824 | 1149 | } else { |
ca872b9d | 1150 | $error_msg = mb_substr(magpie_error(), 0, 250); |
9fdf7824 | 1151 | } |
ca872b9d AD |
1152 | |
1153 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1154 | _debug("update_rss_feed: error fetching feed: $error_msg"); | |
1155 | } | |
1156 | ||
1157 | $error_msg = db_escape_string($error_msg); | |
1158 | ||
ab3d0b99 | 1159 | db_query($link, |
aa5f9f5f AD |
1160 | "UPDATE ttrss_feeds SET last_error = '$error_msg', |
1161 | last_updated = NOW() WHERE id = '$feed'"); | |
40d13c28 AD |
1162 | } |
1163 | ||
9fdf7824 | 1164 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
34e420fb | 1165 | _debug("update_rss_feed: done"); |
219bd8fc AD |
1166 | } |
1167 | ||
40d13c28 AD |
1168 | } |
1169 | ||
f175937c | 1170 | function print_select($id, $default, $values, $attributes = "") { |
79f3553b | 1171 | print "<select name=\"$id\" id=\"$id\" $attributes>"; |
a0d53889 AD |
1172 | foreach ($values as $v) { |
1173 | if ($v == $default) | |
1174 | $sel = " selected"; | |
1175 | else | |
1176 | $sel = ""; | |
1177 | ||
1178 | print "<option$sel>$v</option>"; | |
1179 | } | |
1180 | print "</select>"; | |
1181 | } | |
40d13c28 | 1182 | |
79f3553b AD |
1183 | function print_select_hash($id, $default, $values, $attributes = "") { |
1184 | print "<select name=\"$id\" id='$id' $attributes>"; | |
673d54ca AD |
1185 | foreach (array_keys($values) as $v) { |
1186 | if ($v == $default) | |
1187 | $sel = "selected"; | |
1188 | else | |
1189 | $sel = ""; | |
1190 | ||
1191 | print "<option $sel value=\"$v\">".$values[$v]."</option>"; | |
1192 | } | |
1193 | ||
1194 | print "</select>"; | |
1195 | } | |
1196 | ||
f8382011 | 1197 | function get_article_filters($filters, $title, $content, $link) { |
240054f1 | 1198 | $matches = array(); |
c2d9322b | 1199 | |
240054f1 AD |
1200 | if ($filters["title"]) { |
1201 | foreach ($filters["title"] as $filter) { | |
c2d9322b AD |
1202 | $reg_exp = $filter["reg_exp"]; |
1203 | $inverse = $filter["inverse"]; | |
1204 | if ((!$inverse && preg_match("/$reg_exp/i", $title)) || | |
1205 | ($inverse && !preg_match("/$reg_exp/i", $title))) { | |
1206 | ||
240054f1 AD |
1207 | array_push($matches, array($filter["action"], $filter["action_param"])); |
1208 | } | |
1209 | } | |
1210 | } | |
1211 | ||
1212 | if ($filters["content"]) { | |
1213 | foreach ($filters["content"] as $filter) { | |
c2d9322b AD |
1214 | $reg_exp = $filter["reg_exp"]; |
1215 | $inverse = $filter["inverse"]; | |
1216 | ||
1217 | if ((!$inverse && preg_match("/$reg_exp/i", $content)) || | |
1218 | ($inverse && !preg_match("/$reg_exp/i", $content))) { | |
1219 | ||
240054f1 AD |
1220 | array_push($matches, array($filter["action"], $filter["action_param"])); |
1221 | } | |
1222 | } | |
1223 | } | |
1224 | ||
1225 | if ($filters["both"]) { | |
1226 | foreach ($filters["both"] as $filter) { | |
1227 | $reg_exp = $filter["reg_exp"]; | |
c2d9322b AD |
1228 | $inverse = $filter["inverse"]; |
1229 | ||
1230 | if ($inverse) { | |
1231 | if (!preg_match("/$reg_exp/i", $title) || !preg_match("/$reg_exp/i", $content)) { | |
1232 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
1233 | } | |
1234 | } else { | |
1235 | if (preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) { | |
1236 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
1237 | } | |
240054f1 AD |
1238 | } |
1239 | } | |
1240 | } | |
1241 | ||
1242 | if ($filters["link"]) { | |
1243 | $reg_exp = $filter["reg_exp"]; | |
1244 | foreach ($filters["link"] as $filter) { | |
1245 | $reg_exp = $filter["reg_exp"]; | |
c2d9322b AD |
1246 | $inverse = $filter["inverse"]; |
1247 | ||
1248 | if ((!$inverse && preg_match("/$reg_exp/i", $link)) || | |
1249 | ($inverse && !preg_match("/$reg_exp/i", $link))) { | |
1250 | ||
240054f1 AD |
1251 | array_push($matches, array($filter["action"], $filter["action_param"])); |
1252 | } | |
1253 | } | |
1254 | } | |
1255 | ||
1256 | return $matches; | |
1257 | } | |
1258 | ||
f8382011 AD |
1259 | function find_article_filter($filters, $filter_name) { |
1260 | foreach ($filters as $f) { | |
1261 | if ($f[0] == $filter_name) { | |
1262 | return $f; | |
1263 | }; | |
1264 | } | |
1265 | return false; | |
1266 | } | |
1267 | ||
9323147e | 1268 | function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link, |
fb1fb4ab | 1269 | $rtl_content = false, $last_updated = false, $last_error = false) { |
254e0e4b AD |
1270 | |
1271 | if (file_exists($icon_file) && filesize($icon_file) > 0) { | |
023fe037 | 1272 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">"; |
254e0e4b | 1273 | } else { |
023fe037 | 1274 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">"; |
254e0e4b AD |
1275 | } |
1276 | ||
9323147e AD |
1277 | if ($rtl_content) { |
1278 | $rtl_tag = "dir=\"rtl\""; | |
1279 | } else { | |
1280 | $rtl_tag = "dir=\"ltr\""; | |
1281 | } | |
1282 | ||
78d5212c AD |
1283 | $error_notify_msg = ""; |
1284 | ||
fb1fb4ab AD |
1285 | if ($last_error) { |
1286 | $link_title = "Error: $last_error ($last_updated)"; | |
78d5212c | 1287 | $error_notify_msg = "(Error)"; |
ad780e9c | 1288 | } else if ($last_updated) { |
fb1fb4ab AD |
1289 | $link_title = "Updated: $last_updated"; |
1290 | } | |
1291 | ||
7210613a | 1292 | $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" |
c50e2b30 | 1293 | href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>"; |
254e0e4b AD |
1294 | |
1295 | print "<li id=\"FEEDR-$feed_id\" class=\"$class\">"; | |
b619ff15 | 1296 | if (get_pref($link, 'ENABLE_FEED_ICONS')) { |
254e0e4b AD |
1297 | print "$feed_icon"; |
1298 | } | |
1299 | ||
9323147e | 1300 | print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>"; |
254e0e4b AD |
1301 | |
1302 | if ($unread != 0) { | |
1303 | $fctr_class = ""; | |
1304 | } else { | |
1305 | $fctr_class = "class=\"invisible\""; | |
1306 | } | |
1307 | ||
9323147e | 1308 | print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\"> |
254e0e4b | 1309 | (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>"; |
78d5212c AD |
1310 | |
1311 | if (get_pref($link, "EXTENDED_FEEDLIST")) { | |
1312 | print "<div class=\"feedExtInfo\"> | |
1313 | <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>"; | |
1314 | } | |
1315 | ||
254e0e4b AD |
1316 | print "</li>"; |
1317 | ||
1318 | } | |
1319 | ||
406d9489 AD |
1320 | function getmicrotime() { |
1321 | list($usec, $sec) = explode(" ",microtime()); | |
1322 | return ((float)$usec + (float)$sec); | |
1323 | } | |
1324 | ||
f541eb78 | 1325 | function print_radio($id, $default, $true_is, $values, $attributes = "") { |
77e96719 AD |
1326 | foreach ($values as $v) { |
1327 | ||
1328 | if ($v == $default) | |
5da169d9 | 1329 | $sel = "checked"; |
77e96719 | 1330 | else |
5da169d9 AD |
1331 | $sel = ""; |
1332 | ||
f541eb78 | 1333 | if ($v == $true_is) { |
5da169d9 AD |
1334 | $sel .= " value=\"1\""; |
1335 | } else { | |
1336 | $sel .= " value=\"0\""; | |
1337 | } | |
77e96719 | 1338 | |
69654950 AD |
1339 | print "<input class=\"noborder\" |
1340 | type=\"radio\" $sel $attributes name=\"$id\"> $v "; | |
77e96719 AD |
1341 | |
1342 | } | |
1343 | } | |
1344 | ||
ff485f1d AD |
1345 | function initialize_user_prefs($link, $uid) { |
1346 | ||
1347 | $uid = db_escape_string($uid); | |
1348 | ||
1349 | db_query($link, "BEGIN"); | |
1350 | ||
1351 | $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs"); | |
1352 | ||
1353 | $u_result = db_query($link, "SELECT pref_name | |
1354 | FROM ttrss_user_prefs WHERE owner_uid = '$uid'"); | |
1355 | ||
1356 | $active_prefs = array(); | |
1357 | ||
1358 | while ($line = db_fetch_assoc($u_result)) { | |
1359 | array_push($active_prefs, $line["pref_name"]); | |
1360 | } | |
1361 | ||
1362 | while ($line = db_fetch_assoc($result)) { | |
1363 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
1364 | // print "adding " . $line["pref_name"] . "<br>"; | |
1365 | ||
1366 | db_query($link, "INSERT INTO ttrss_user_prefs | |
1367 | (owner_uid,pref_name,value) VALUES | |
1368 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); | |
1369 | ||
1370 | } | |
1371 | } | |
1372 | ||
1373 | db_query($link, "COMMIT"); | |
1374 | ||
1375 | } | |
956c7629 AD |
1376 | |
1377 | function lookup_user_id($link, $user) { | |
1378 | ||
1379 | $result = db_query($link, "SELECT id FROM ttrss_users WHERE | |
1380 | login = '$login'"); | |
1381 | ||
1382 | if (db_num_rows($result) == 1) { | |
1383 | return db_fetch_result($result, 0, "id"); | |
1384 | } else { | |
1385 | return false; | |
1386 | } | |
1387 | } | |
1388 | ||
18664970 AD |
1389 | function http_authenticate_user($link) { |
1390 | ||
4bc64807 AD |
1391 | error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log'); |
1392 | ||
18664970 AD |
1393 | if (!$_SERVER["PHP_AUTH_USER"]) { |
1394 | ||
1395 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"'); | |
1396 | header('HTTP/1.0 401 Unauthorized'); | |
1397 | exit; | |
1398 | ||
1399 | } else { | |
1400 | $auth_result = authenticate_user($link, | |
1401 | $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]); | |
1402 | ||
1403 | if (!$auth_result) { | |
1404 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"'); | |
1405 | header('HTTP/1.0 401 Unauthorized'); | |
1406 | exit; | |
1407 | } | |
1408 | } | |
1409 | ||
1410 | return true; | |
1411 | } | |
1412 | ||
461766f3 | 1413 | function authenticate_user($link, $login, $password, $force_auth = false) { |
c8437f35 | 1414 | |
131b01b3 | 1415 | if (!SINGLE_USER_MODE) { |
c8437f35 | 1416 | |
131b01b3 | 1417 | $pwd_hash = 'SHA1:' . sha1($password); |
461766f3 AD |
1418 | |
1419 | if ($force_auth && defined('_DEBUG_USER_SWITCH')) { | |
1420 | $query = "SELECT id,login,access_level | |
1421 | FROM ttrss_users WHERE | |
1422 | login = '$login'"; | |
1423 | } else { | |
1424 | $query = "SELECT id,login,access_level | |
1425 | FROM ttrss_users WHERE | |
1426 | login = '$login' AND pwd_hash = '$pwd_hash'"; | |
1427 | } | |
1428 | ||
1429 | $result = db_query($link, $query); | |
131b01b3 AD |
1430 | |
1431 | if (db_num_rows($result) == 1) { | |
1432 | $_SESSION["uid"] = db_fetch_result($result, 0, "id"); | |
1433 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); | |
1434 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); | |
1435 | ||
1436 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
1437 | $_SESSION["uid"]); | |
1438 | ||
1439 | $user_theme = get_user_theme_path($link); | |
1440 | ||
1441 | $_SESSION["theme"] = $user_theme; | |
1442 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
1443 | ||
1444 | initialize_user_prefs($link, $_SESSION["uid"]); | |
1445 | ||
1446 | return true; | |
1447 | } | |
1448 | ||
1449 | return false; | |
503eb349 | 1450 | |
131b01b3 | 1451 | } else { |
503eb349 | 1452 | |
131b01b3 AD |
1453 | $_SESSION["uid"] = 1; |
1454 | $_SESSION["name"] = "admin"; | |
f557cd78 | 1455 | |
0bbba72d AD |
1456 | $user_theme = get_user_theme_path($link); |
1457 | ||
1458 | $_SESSION["theme"] = $user_theme; | |
1459 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
1460 | ||
1461 | initialize_user_prefs($link, $_SESSION["uid"]); | |
1462 | ||
c8437f35 AD |
1463 | return true; |
1464 | } | |
c8437f35 AD |
1465 | } |
1466 | ||
e6cb77a0 AD |
1467 | function make_password($length = 8) { |
1468 | ||
1469 | $password = ""; | |
798f722b AD |
1470 | $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ"; |
1471 | ||
1472 | $i = 0; | |
e6cb77a0 AD |
1473 | |
1474 | while ($i < $length) { | |
1475 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
1476 | ||
1477 | if (!strstr($password, $char)) { | |
1478 | $password .= $char; | |
1479 | $i++; | |
1480 | } | |
1481 | } | |
1482 | return $password; | |
1483 | } | |
1484 | ||
1485 | // this is called after user is created to initialize default feeds, labels | |
1486 | // or whatever else | |
1487 | ||
1488 | // user preferences are checked on every login, not here | |
1489 | ||
1490 | function initialize_user($link, $uid) { | |
1491 | ||
1492 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
1493 | values ('$uid','unread = true', 'Unread articles')"); | |
1494 | ||
1495 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
1496 | values ('$uid','last_read is null and unread = false', 'Updated articles')"); | |
1497 | ||
1498 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
74bff337 | 1499 | values ('$uid', 'Tiny Tiny RSS: New Releases', |
628fcd2c | 1500 | 'http://tt-rss.spb.ru/releases.rss')"); |
3b0feb9b | 1501 | |
cd2cd415 AD |
1502 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) |
1503 | values ('$uid', 'Tiny Tiny RSS: Forum', | |
1504 | 'http://tt-rss.spb.ru/forum/rss.php')"); | |
3b0feb9b | 1505 | } |
e6cb77a0 | 1506 | |
b8aa49bc | 1507 | function logout_user() { |
5ccc1cf5 AD |
1508 | session_destroy(); |
1509 | if (isset($_COOKIE[session_name()])) { | |
1510 | setcookie(session_name(), '', time()-42000, '/'); | |
1511 | } | |
b8aa49bc AD |
1512 | } |
1513 | ||
75836f33 | 1514 | function get_script_urlpath() { |
87a79fa4 | 1515 | return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]); |
75836f33 AD |
1516 | } |
1517 | ||
916f788a | 1518 | function validate_session($link) { |
a2e9b457 | 1519 | if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) { |
916f788a AD |
1520 | if ($_SESSION["ip_address"]) { |
1521 | if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) { | |
7f0acba7 | 1522 | $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)"; |
916f788a AD |
1523 | return false; |
1524 | } | |
1525 | } | |
1526 | } | |
d620cfe7 | 1527 | |
a885f0ec | 1528 | /* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) { |
d620cfe7 | 1529 | |
8e849206 | 1530 | //print_r($_SESSION); |
d620cfe7 AD |
1531 | |
1532 | if (time() > $_SESSION["cookie_lifetime"]) { | |
1533 | return false; | |
1534 | } | |
a885f0ec AD |
1535 | } */ |
1536 | ||
916f788a AD |
1537 | return true; |
1538 | } | |
1539 | ||
793185a9 | 1540 | function login_sequence($link, $mobile = false) { |
b8aa49bc | 1541 | if (!SINGLE_USER_MODE) { |
75836f33 | 1542 | |
461766f3 AD |
1543 | if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) { |
1544 | $swu = db_escape_string($_REQUEST["swu"]); | |
1545 | if ($swu) { | |
1546 | $_SESSION["prefs_cache"] = false; | |
1547 | return authenticate_user($link, $swu, null, true); | |
1548 | } | |
1549 | } | |
1550 | ||
7f0acba7 | 1551 | $login_action = $_POST["login_action"]; |
a885f0ec | 1552 | |
01a87dff | 1553 | # try to authenticate user if called from login form |
7f0acba7 | 1554 | if ($login_action == "do_login") { |
01a87dff AD |
1555 | $login = $_POST["login"]; |
1556 | $password = $_POST["password"]; | |
d620cfe7 | 1557 | $remember_me = $_POST["remember_me"]; |
f557cd78 | 1558 | |
01a87dff AD |
1559 | if (authenticate_user($link, $login, $password)) { |
1560 | $_POST["password"] = ""; | |
d620cfe7 | 1561 | |
f8c612d4 AD |
1562 | $_SESSION["language"] = $_POST["language"]; |
1563 | ||
d620cfe7 AD |
1564 | header("Location: " . $_SERVER["REQUEST_URI"]); |
1565 | exit; | |
1566 | ||
01a87dff | 1567 | return; |
7f0acba7 AD |
1568 | } else { |
1569 | $_SESSION["login_error_msg"] = "Incorrect username or password"; | |
01a87dff AD |
1570 | } |
1571 | } | |
1572 | ||
1df0f48b AD |
1573 | // print session_id(); |
1574 | // print_r($_SESSION); | |
7f0acba7 AD |
1575 | |
1576 | if (!$_SESSION["uid"] || !validate_session($link)) { | |
793185a9 | 1577 | render_login_form($link, $mobile); |
01a87dff | 1578 | exit; |
d3687e7a AD |
1579 | } else { |
1580 | /* bump login timestamp */ | |
1581 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
1582 | $_SESSION["uid"]); | |
019bd5a9 | 1583 | |
d54780bc | 1584 | if ($_SESSION["language"] && SESSION_COOKIE_LIFETIME > 0) { |
019bd5a9 AD |
1585 | setcookie("ttrss_lang", $_SESSION["language"], |
1586 | time() + SESSION_COOKIE_LIFETIME); | |
1587 | } | |
b8aa49bc | 1588 | } |
d620cfe7 | 1589 | |
b8aa49bc | 1590 | } else { |
0bbba72d | 1591 | return authenticate_user($link, "admin", null); |
b8aa49bc AD |
1592 | } |
1593 | } | |
3547842a AD |
1594 | |
1595 | function truncate_string($str, $max_len) { | |
12db369c AD |
1596 | if (mb_strlen($str, "utf-8") > $max_len - 3) { |
1597 | return mb_substr($str, 0, $max_len, "utf-8") . "..."; | |
3547842a AD |
1598 | } else { |
1599 | return $str; | |
1600 | } | |
1601 | } | |
54a60e1a AD |
1602 | |
1603 | function get_user_theme_path($link) { | |
798f722b AD |
1604 | $result = db_query($link, "SELECT theme_path |
1605 | FROM | |
1606 | ttrss_themes,ttrss_users | |
1607 | WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]); | |
54a60e1a AD |
1608 | if (db_num_rows($result) != 0) { |
1609 | return db_fetch_result($result, 0, "theme_path"); | |
1610 | } else { | |
1611 | return null; | |
1612 | } | |
1613 | } | |
be773442 AD |
1614 | |
1615 | function smart_date_time($timestamp) { | |
1616 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1617 | return date("G:i", $timestamp); | |
f26450f1 | 1618 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
1619 | return date("M d, G:i", $timestamp); |
1620 | } else { | |
7d7e0509 | 1621 | return date("Y/m/d, G:i", $timestamp); |
be773442 AD |
1622 | } |
1623 | } | |
1624 | ||
1625 | function smart_date($timestamp) { | |
1626 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1627 | return "Today"; | |
f26450f1 | 1628 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
1629 | return date("D m", $timestamp); |
1630 | } else { | |
b02111c2 | 1631 | return date("Y/m/d", $timestamp); |
be773442 AD |
1632 | } |
1633 | } | |
a654a595 AD |
1634 | |
1635 | function sql_bool_to_string($s) { | |
1636 | if ($s == "t" || $s == "1") { | |
1637 | return "true"; | |
1638 | } else { | |
1639 | return "false"; | |
1640 | } | |
1641 | } | |
e3c99f3b AD |
1642 | |
1643 | function sql_bool_to_bool($s) { | |
1644 | if ($s == "t" || $s == "1") { | |
1645 | return true; | |
1646 | } else { | |
1647 | return false; | |
1648 | } | |
1649 | } | |
0ea4fb50 | 1650 | |
e3c99f3b | 1651 | |
0ea4fb50 AD |
1652 | function toggleEvenOdd($a) { |
1653 | if ($a == "even") | |
1654 | return "odd"; | |
1655 | else | |
1656 | return "even"; | |
1657 | } | |
6043fb7e AD |
1658 | |
1659 | function sanity_check($link) { | |
9cbca41f | 1660 | |
aec3ce39 AD |
1661 | error_reporting(0); |
1662 | ||
6043fb7e AD |
1663 | $error_code = 0; |
1664 | $result = db_query($link, "SELECT schema_version FROM ttrss_version"); | |
1665 | $schema_version = db_fetch_result($result, 0, "schema_version"); | |
1666 | ||
1667 | if ($schema_version != SCHEMA_VERSION) { | |
1668 | $error_code = 5; | |
1669 | } | |
1670 | ||
aec3ce39 AD |
1671 | if (DB_TYPE == "mysql") { |
1672 | $result = db_query($link, "SELECT true", false); | |
1673 | if (db_num_rows($result) != 1) { | |
1674 | $error_code = 10; | |
1675 | } | |
1676 | } | |
1677 | ||
1678 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1679 | ||
6043fb7e | 1680 | if ($error_code != 0) { |
aec3ce39 | 1681 | print_error_xml($error_code); |
6043fb7e AD |
1682 | return false; |
1683 | } else { | |
1684 | return true; | |
4220d6b0 | 1685 | } |
6043fb7e AD |
1686 | } |
1687 | ||
27981ca3 AD |
1688 | function file_is_locked($filename) { |
1689 | error_reporting(0); | |
1690 | $fp = fopen($filename, "r"); | |
1691 | error_reporting(DEFAULT_ERROR_LEVEL); | |
1692 | if ($fp) { | |
1693 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1694 | flock($fp, LOCK_UN); | |
1695 | fclose($fp); | |
1696 | return false; | |
1697 | } | |
1698 | fclose($fp); | |
1699 | return true; | |
1700 | } | |
1701 | return false; | |
1702 | } | |
1703 | ||
fcb4c0c9 AD |
1704 | function make_lockfile($filename) { |
1705 | $fp = fopen($filename, "w"); | |
1706 | ||
1707 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1708 | return $fp; | |
1709 | } else { | |
1710 | return false; | |
1711 | } | |
1712 | } | |
1713 | ||
bf7fcde8 AD |
1714 | function make_stampfile($filename) { |
1715 | $fp = fopen($filename, "w"); | |
1716 | ||
8e00ae9b | 1717 | if (flock($fp, LOCK_EX | LOCK_NB)) { |
bf7fcde8 | 1718 | fwrite($fp, time() . "\n"); |
8e00ae9b | 1719 | flock($fp, LOCK_UN); |
bf7fcde8 AD |
1720 | fclose($fp); |
1721 | return true; | |
1722 | } else { | |
1723 | return false; | |
1724 | } | |
1725 | } | |
1726 | ||
8e00ae9b AD |
1727 | function read_stampfile($filename) { |
1728 | ||
1729 | error_reporting(0); | |
1730 | $fp = fopen($filename, "r"); | |
1731 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1732 | ||
1733 | if (flock($fp, LOCK_EX)) { | |
1734 | $stamp = fgets($fp); | |
1735 | flock($fp, LOCK_UN); | |
1736 | fclose($fp); | |
1737 | return $stamp; | |
1738 | } else { | |
1739 | return false; | |
1740 | } | |
1741 | } | |
bf7fcde8 | 1742 | |
894ebcf5 AD |
1743 | function sql_random_function() { |
1744 | if (DB_TYPE == "mysql") { | |
1745 | return "RAND()"; | |
1746 | } else { | |
1747 | return "RANDOM()"; | |
1748 | } | |
1749 | } | |
1750 | ||
23aa0d16 | 1751 | function catchup_feed($link, $feed, $cat_view) { |
88040f57 AD |
1752 | |
1753 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
23aa0d16 AD |
1754 | |
1755 | if ($cat_view) { | |
1756 | ||
1757 | if ($feed > 0) { | |
1758 | $cat_qpart = "cat_id = '$feed'"; | |
1759 | } else { | |
1760 | $cat_qpart = "cat_id IS NULL"; | |
1761 | } | |
1762 | ||
1763 | $tmp_result = db_query($link, "SELECT id | |
1764 | FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " . | |
1765 | $_SESSION["uid"]); | |
1766 | ||
1767 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1768 | ||
1769 | $tmp_feed = $tmp_line["id"]; | |
1770 | ||
1771 | db_query($link, "UPDATE ttrss_user_entries | |
1772 | SET unread = false,last_read = NOW() | |
1773 | WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]); | |
1774 | } | |
1775 | ||
1776 | } else if ($feed > 0) { | |
1777 | ||
1778 | $tmp_result = db_query($link, "SELECT id | |
1779 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
1780 | ORDER BY cat_id,title"); | |
1781 | ||
1782 | $parent_ids = array(); | |
1783 | ||
1784 | if (db_num_rows($tmp_result) > 0) { | |
1785 | while ($p = db_fetch_assoc($tmp_result)) { | |
1786 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
1787 | } | |
1788 | ||
1789 | $children_qpart = implode(" OR ", $parent_ids); | |
1790 | ||
1791 | db_query($link, "UPDATE ttrss_user_entries | |
1792 | SET unread = false,last_read = NOW() | |
1793 | WHERE (feed_id = '$feed' OR $children_qpart) | |
1794 | AND owner_uid = " . $_SESSION["uid"]); | |
1795 | ||
1796 | } else { | |
1797 | db_query($link, "UPDATE ttrss_user_entries | |
1798 | SET unread = false,last_read = NOW() | |
1799 | WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
1800 | } | |
1801 | ||
1802 | } else if ($feed < 0 && $feed > -10) { // special, like starred | |
1803 | ||
1804 | if ($feed == -1) { | |
1805 | db_query($link, "UPDATE ttrss_user_entries | |
1806 | SET unread = false,last_read = NOW() | |
1807 | WHERE marked = true AND owner_uid = ".$_SESSION["uid"]); | |
1808 | } | |
e4f4b46f AD |
1809 | |
1810 | if ($feed == -2) { | |
1811 | db_query($link, "UPDATE ttrss_user_entries | |
1812 | SET unread = false,last_read = NOW() | |
1813 | WHERE published = true AND owner_uid = ".$_SESSION["uid"]); | |
1814 | } | |
1815 | ||
2d24f032 AD |
1816 | if ($feed == -3) { |
1817 | ||
c1d7e6c3 AD |
1818 | $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE"); |
1819 | ||
2d24f032 | 1820 | if (DB_TYPE == "pgsql") { |
c1d7e6c3 | 1821 | $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' "; |
2d24f032 AD |
1822 | } else { |
1823 | $match_part .= " AND date_entered > DATE_SUB(NOW(), | |
c1d7e6c3 | 1824 | INTERVAL $intl HOUR) "; |
2d24f032 AD |
1825 | } |
1826 | ||
1827 | db_query($link, "UPDATE ttrss_user_entries | |
1828 | SET unread = false,last_read = NOW() | |
1829 | WHERE $match_part AND owner_uid = ".$_SESSION["uid"]); | |
1830 | } | |
1831 | ||
23aa0d16 AD |
1832 | } else if ($feed < -10) { // label |
1833 | ||
1834 | // TODO make this more efficient | |
1835 | ||
1836 | $label_id = -$feed - 11; | |
1837 | ||
1838 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
1839 | WHERE id = '$label_id'"); | |
1840 | ||
1841 | if ($tmp_result) { | |
1842 | $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp"); | |
1843 | ||
1844 | db_query($link, "BEGIN"); | |
1845 | ||
1846 | $tmp2_result = db_query($link, | |
1847 | "SELECT | |
1848 | int_id | |
1849 | FROM | |
88040f57 | 1850 | ttrss_user_entries,ttrss_entries,ttrss_feeds |
23aa0d16 | 1851 | WHERE |
88040f57 AD |
1852 | ref_id = ttrss_entries.id AND |
1853 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
23aa0d16 | 1854 | $sql_exp AND |
88040f57 | 1855 | ttrss_user_entries.owner_uid = " . $_SESSION["uid"]); |
23aa0d16 AD |
1856 | |
1857 | while ($tmp_line = db_fetch_assoc($tmp2_result)) { | |
1858 | db_query($link, "UPDATE | |
1859 | ttrss_user_entries | |
1860 | SET | |
1861 | unread = false, last_read = NOW() | |
1862 | WHERE | |
1863 | int_id = " . $tmp_line["int_id"]); | |
1864 | } | |
1865 | ||
1866 | db_query($link, "COMMIT"); | |
1867 | ||
1868 | /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries | |
1869 | SET unread = false,last_read = NOW() | |
1870 | WHERE $sql_exp | |
1871 | AND ref_id = id | |
1872 | AND owner_uid = ".$_SESSION["uid"]); */ | |
1873 | } | |
1874 | } | |
1875 | } else { // tag | |
1876 | db_query($link, "BEGIN"); | |
1877 | ||
1878 | $tag_name = db_escape_string($feed); | |
1879 | ||
1880 | $result = db_query($link, "SELECT post_int_id FROM ttrss_tags | |
1881 | WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]); | |
1882 | ||
1883 | while ($line = db_fetch_assoc($result)) { | |
1884 | db_query($link, "UPDATE ttrss_user_entries SET | |
1885 | unread = false, last_read = NOW() | |
1886 | WHERE int_id = " . $line["post_int_id"]); | |
1887 | } | |
1888 | db_query($link, "COMMIT"); | |
1889 | } | |
1890 | } | |
1891 | ||
1892 | function update_generic_feed($link, $feed, $cat_view) { | |
1893 | if ($cat_view) { | |
1894 | ||
1895 | if ($feed > 0) { | |
1896 | $cat_qpart = "cat_id = '$feed'"; | |
1897 | } else { | |
1898 | $cat_qpart = "cat_id IS NULL"; | |
1899 | } | |
1900 | ||
1901 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1902 | WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]); | |
1903 | ||
1904 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1905 | $feed_url = $tmp_line["feed_url"]; | |
1906 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1907 | } | |
1908 | ||
1909 | } else { | |
1910 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1911 | WHERE id = '$feed'"); | |
1912 | $feed_url = db_fetch_result($tmp_result, 0, "feed_url"); | |
1913 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1914 | } | |
1915 | } | |
a9cb1f83 | 1916 | |
4ffa126e | 1917 | function getAllCounters($link, $omode = "flc", $active_feed = false) { |
cf4d339c | 1918 | /* getLabelCounters($link); |
a9cb1f83 AD |
1919 | getFeedCounters($link); |
1920 | getTagCounters($link); | |
1921 | getGlobalCounters($link); | |
1922 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
1923 | getCategoryCounters($link); | |
cf4d339c AD |
1924 | } */ |
1925 | ||
2bc2147f | 1926 | if (!$omode) $omode = "flc"; |
cf4d339c AD |
1927 | |
1928 | getGlobalCounters($link); | |
1929 | ||
1930 | if (strchr($omode, "l")) getLabelCounters($link); | |
4ffa126e | 1931 | if (strchr($omode, "f")) getFeedCounters($link, SMART_RPC_COUNTERS, $active_feed); |
cf4d339c AD |
1932 | if (strchr($omode, "t")) getTagCounters($link); |
1933 | if (strchr($omode, "c")) { | |
1934 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
1935 | getCategoryCounters($link); | |
1936 | } | |
a9cb1f83 AD |
1937 | } |
1938 | } | |
1939 | ||
1940 | function getCategoryCounters($link) { | |
bba7c4bf AD |
1941 | # two special categories are -1 and -2 (all virtuals; all labels) |
1942 | ||
1943 | $ctr = getCategoryUnread($link, -1); | |
1944 | ||
1945 | print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>"; | |
1946 | ||
1947 | $ctr = getCategoryUnread($link, -2); | |
1948 | ||
1949 | print "<counter type=\"category\" id=\"-2\" counter=\"$ctr\"/>"; | |
1950 | ||
14073c0a AD |
1951 | $age_qpart = getMaxAgeSubquery(); |
1952 | ||
a9cb1f83 | 1953 | $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id) |
14073c0a AD |
1954 | FROM ttrss_user_entries, ttrss_entries WHERE feed_id = ttrss_feeds.id |
1955 | AND id = ref_id AND $age_qpart | |
a9cb1f83 AD |
1956 | AND unread = true)) AS unread FROM ttrss_feeds |
1957 | WHERE | |
cfb02131 | 1958 | hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id"); |
a9cb1f83 AD |
1959 | |
1960 | while ($line = db_fetch_assoc($result)) { | |
1961 | $line["cat_id"] = sprintf("%d", $line["cat_id"]); | |
1962 | print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"". | |
1963 | $line["unread"]."\"/>"; | |
1964 | } | |
1965 | } | |
1966 | ||
f295c368 AD |
1967 | function getCategoryUnread($link, $cat) { |
1968 | ||
bba7c4bf | 1969 | if ($cat >= 0) { |
18664970 | 1970 | |
bba7c4bf AD |
1971 | if ($cat != 0) { |
1972 | $cat_query = "cat_id = '$cat'"; | |
1973 | } else { | |
1974 | $cat_query = "cat_id IS NULL"; | |
1975 | } | |
14073c0a AD |
1976 | |
1977 | $age_qpart = getMaxAgeSubquery(); | |
1978 | ||
bba7c4bf AD |
1979 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query |
1980 | AND hidden = false | |
1981 | AND owner_uid = " . $_SESSION["uid"]); | |
1982 | ||
1983 | $cat_feeds = array(); | |
1984 | while ($line = db_fetch_assoc($result)) { | |
1985 | array_push($cat_feeds, "feed_id = " . $line["id"]); | |
1986 | } | |
1987 | ||
1988 | if (count($cat_feeds) == 0) return 0; | |
1989 | ||
1990 | $match_part = implode(" OR ", $cat_feeds); | |
1991 | ||
1992 | $result = db_query($link, "SELECT COUNT(int_id) AS unread | |
14073c0a AD |
1993 | FROM ttrss_user_entries,ttrss_entries |
1994 | WHERE unread = true AND ($match_part) AND id = ref_id | |
1995 | AND $age_qpart AND owner_uid = " . $_SESSION["uid"]); | |
bba7c4bf AD |
1996 | |
1997 | $unread = 0; | |
1998 | ||
1999 | # this needs to be rewritten | |
2000 | while ($line = db_fetch_assoc($result)) { | |
2001 | $unread += $line["unread"]; | |
2002 | } | |
2003 | ||
2004 | return $unread; | |
2005 | } else if ($cat == -1) { | |
2d24f032 | 2006 | return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3); |
bba7c4bf | 2007 | } else if ($cat == -2) { |
f295c368 | 2008 | |
bba7c4bf AD |
2009 | $rv = getLabelCounters($link, false, true); |
2010 | $ctr = 0; | |
f295c368 | 2011 | |
bba7c4bf AD |
2012 | foreach (array_keys($rv) as $k) { |
2013 | if ($k < -10) { | |
2014 | $ctr += $rv[$k]["counter"]; | |
2015 | } | |
2016 | } | |
f295c368 | 2017 | |
bba7c4bf | 2018 | return $ctr; |
f295c368 | 2019 | } |
f295c368 AD |
2020 | } |
2021 | ||
14073c0a AD |
2022 | function getMaxAgeSubquery($days = COUNTERS_MAX_AGE) { |
2023 | if (DB_TYPE == "pgsql") { | |
2024 | return "ttrss_entries.date_entered > | |
2025 | NOW() - INTERVAL '$days days'"; | |
2026 | } else { | |
2027 | return "ttrss_entries.date_entered > | |
2028 | DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
2029 | } | |
2030 | } | |
2031 | ||
f295c368 | 2032 | function getFeedUnread($link, $feed, $is_cat = false) { |
a9cb1f83 | 2033 | $n_feed = sprintf("%d", $feed); |
f295c368 | 2034 | |
14073c0a AD |
2035 | $age_qpart = getMaxAgeSubquery(); |
2036 | ||
f295c368 | 2037 | if ($is_cat) { |
831ff047 | 2038 | return getCategoryUnread($link, $n_feed); |
f295c368 | 2039 | } else if ($n_feed == -1) { |
a9cb1f83 | 2040 | $match_part = "marked = true"; |
e4f4b46f AD |
2041 | } else if ($n_feed == -2) { |
2042 | $match_part = "published = true"; | |
2d24f032 AD |
2043 | } else if ($n_feed == -3) { |
2044 | $match_part = "unread = true"; | |
2045 | ||
c1d7e6c3 AD |
2046 | $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE"); |
2047 | ||
2d24f032 | 2048 | if (DB_TYPE == "pgsql") { |
c1d7e6c3 | 2049 | $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' "; |
2d24f032 | 2050 | } else { |
c1d7e6c3 | 2051 | $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) "; |
2d24f032 AD |
2052 | } |
2053 | ||
4919fb42 | 2054 | } else if ($n_feed > 0) { |
831ff047 | 2055 | |
e8b8485f AD |
2056 | $result = db_query($link, "SELECT id FROM ttrss_feeds |
2057 | WHERE parent_feed = '$n_feed' | |
318260cc | 2058 | AND hidden = false |
4919fb42 | 2059 | AND owner_uid = " . $_SESSION["uid"]); |
831ff047 AD |
2060 | |
2061 | if (db_num_rows($result) > 0) { | |
4919fb42 | 2062 | |
831ff047 AD |
2063 | $linked_feeds = array(); |
2064 | while ($line = db_fetch_assoc($result)) { | |
2065 | array_push($linked_feeds, "feed_id = " . $line["id"]); | |
2066 | } | |
e8b8485f AD |
2067 | |
2068 | array_push($linked_feeds, "feed_id = $n_feed"); | |
831ff047 AD |
2069 | |
2070 | $match_part = implode(" OR ", $linked_feeds); | |
2071 | ||
4919fb42 | 2072 | $result = db_query($link, "SELECT COUNT(int_id) AS unread |
14073c0a AD |
2073 | FROM ttrss_user_entries,ttrss_entries |
2074 | WHERE unread = true AND | |
2075 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2076 | $age_qpart AND | |
2077 | ($match_part) AND | |
2078 | owner_uid = " . $_SESSION["uid"]); | |
4919fb42 AD |
2079 | |
2080 | $unread = 0; | |
2081 | ||
2082 | # this needs to be rewritten | |
2083 | while ($line = db_fetch_assoc($result)) { | |
2084 | $unread += $line["unread"]; | |
2085 | } | |
2086 | ||
2087 | return $unread; | |
2088 | ||
831ff047 AD |
2089 | } else { |
2090 | $match_part = "feed_id = '$n_feed'"; | |
2091 | } | |
a9cb1f83 | 2092 | } else if ($feed < -10) { |
318260cc | 2093 | |
a9cb1f83 AD |
2094 | $label_id = -$feed - 11; |
2095 | ||
2096 | $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE | |
2097 | id = '$label_id' AND owner_uid = " . $_SESSION["uid"]); | |
2098 | ||
2099 | $match_part = db_fetch_result($result, 0, "sql_exp"); | |
2100 | } | |
2101 | ||
2102 | if ($match_part) { | |
2103 | ||
2104 | $result = db_query($link, "SELECT count(int_id) AS unread | |
88040f57 AD |
2105 | FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE |
2106 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
2107 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
cfb02131 | 2108 | ttrss_feeds.hidden = false AND |
14073c0a | 2109 | $age_qpart AND |
88040f57 | 2110 | unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]); |
a9cb1f83 AD |
2111 | |
2112 | } else { | |
2113 | ||
2114 | $result = db_query($link, "SELECT COUNT(post_int_id) AS unread | |
14073c0a AD |
2115 | FROM ttrss_tags,ttrss_user_entries,ttrss_entries |
2116 | WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = id | |
2117 | AND unread = true AND $age_qpart AND | |
a9cb1f83 AD |
2118 | ttrss_tags.owner_uid = " . $_SESSION["uid"]); |
2119 | } | |
2120 | ||
2121 | $unread = db_fetch_result($result, 0, "unread"); | |
cfb02131 | 2122 | |
a9cb1f83 AD |
2123 | return $unread; |
2124 | } | |
2125 | ||
2126 | /* FIXME this needs reworking */ | |
2127 | ||
f3acc32e AD |
2128 | function getGlobalUnread($link, $user_id = false) { |
2129 | ||
2130 | if (!$user_id) { | |
2131 | $user_id = $_SESSION["uid"]; | |
2132 | } | |
2133 | ||
14073c0a AD |
2134 | $age_qpart = getMaxAgeSubquery(); |
2135 | ||
3831db41 | 2136 | $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds |
a9cb1f83 | 2137 | WHERE unread = true AND |
3831db41 | 2138 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
a9cb1f83 | 2139 | ttrss_user_entries.ref_id = ttrss_entries.id AND |
3831db41 | 2140 | hidden = false AND |
14073c0a | 2141 | $age_qpart AND |
f3acc32e | 2142 | ttrss_user_entries.owner_uid = '$user_id'"); |
a9cb1f83 AD |
2143 | $c_id = db_fetch_result($result, 0, "c_id"); |
2144 | return $c_id; | |
2145 | } | |
2146 | ||
2147 | function getGlobalCounters($link, $global_unread = -1) { | |
2148 | if ($global_unread == -1) { | |
2149 | $global_unread = getGlobalUnread($link); | |
2150 | } | |
7bf7e4d3 AD |
2151 | print "<counter type=\"global\" id='global-unread' |
2152 | counter='$global_unread'/>"; | |
2153 | ||
2154 | $result = db_query($link, "SELECT COUNT(id) AS fn FROM | |
2155 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
2156 | ||
2157 | $subscribed_feeds = db_fetch_result($result, 0, "fn"); | |
2158 | ||
2159 | print "<counter type=\"global\" id='subscribed-feeds' | |
2160 | counter='$subscribed_feeds'/>"; | |
2161 | ||
a9cb1f83 AD |
2162 | } |
2163 | ||
2164 | function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
2165 | ||
2166 | if ($smart_mode) { | |
2167 | if (!$_SESSION["tctr_last_value"]) { | |
2168 | $_SESSION["tctr_last_value"] = array(); | |
2169 | } | |
2170 | } | |
2171 | ||
2172 | $old_counters = $_SESSION["tctr_last_value"]; | |
2173 | ||
2174 | $tctrs_modified = false; | |
2175 | ||
2176 | /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count | |
2177 | FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE | |
2178 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2179 | ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND | |
2180 | post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name | |
2181 | UNION | |
2182 | select tag_name,0 as count FROM ttrss_tags | |
2183 | WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */ | |
2184 | ||
14073c0a AD |
2185 | $age_qpart = getMaxAgeSubquery(); |
2186 | ||
a9cb1f83 | 2187 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) |
14073c0a AD |
2188 | FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id |
2189 | AND ref_id = id AND $age_qpart | |
a9cb1f83 | 2190 | AND unread = true)) AS count FROM ttrss_tags |
ef1ac7c7 AD |
2191 | WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name |
2192 | ORDER BY count DESC LIMIT 55"); | |
a9cb1f83 AD |
2193 | |
2194 | $tags = array(); | |
2195 | ||
2196 | while ($line = db_fetch_assoc($result)) { | |
2197 | $tags[$line["tag_name"]] += $line["count"]; | |
2198 | } | |
2199 | ||
2200 | foreach (array_keys($tags) as $tag) { | |
2201 | $unread = $tags[$tag]; | |
2202 | ||
2203 | $tag = htmlspecialchars($tag); | |
2204 | ||
2205 | if (!$smart_mode || $old_counters[$tag] != $unread) { | |
2206 | $old_counters[$tag] = $unread; | |
2207 | $tctrs_modified = true; | |
2208 | print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>"; | |
2209 | } | |
2210 | ||
2211 | } | |
2212 | ||
2213 | if ($smart_mode && $tctrs_modified) { | |
2214 | $_SESSION["tctr_last_value"] = $old_counters; | |
2215 | } | |
2216 | ||
2217 | } | |
2218 | ||
ef393de7 | 2219 | function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) { |
a9cb1f83 | 2220 | |
14073c0a AD |
2221 | $age_qpart = getMaxAgeSubquery(); |
2222 | ||
a9cb1f83 AD |
2223 | if ($smart_mode) { |
2224 | if (!$_SESSION["lctr_last_value"]) { | |
2225 | $_SESSION["lctr_last_value"] = array(); | |
2226 | } | |
2227 | } | |
2228 | ||
ef393de7 AD |
2229 | $ret_arr = array(); |
2230 | ||
a9cb1f83 AD |
2231 | $old_counters = $_SESSION["lctr_last_value"]; |
2232 | $lctrs_modified = false; | |
2233 | ||
2d24f032 | 2234 | $count = getFeedUnread($link, -1); |
a9cb1f83 | 2235 | |
ef393de7 AD |
2236 | if (!$ret_mode) { |
2237 | print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>"; | |
2238 | } else { | |
2239 | $ret_arr["-1"]["counter"] = $count; | |
bba7c4bf | 2240 | $ret_arr["-1"]["description"] = __("Starred articles"); |
ef393de7 | 2241 | } |
a9cb1f83 | 2242 | |
2d24f032 | 2243 | $count = getFeedUnread($link, -2); |
e4f4b46f AD |
2244 | |
2245 | if (!$ret_mode) { | |
2246 | print "<counter type=\"label\" id=\"-2\" counter=\"$count\"/>"; | |
2247 | } else { | |
2248 | $ret_arr["-2"]["counter"] = $count; | |
bba7c4bf | 2249 | $ret_arr["-2"]["description"] = __("Published articles"); |
e4f4b46f AD |
2250 | } |
2251 | ||
2d24f032 AD |
2252 | $count = getFeedUnread($link, -3); |
2253 | ||
2254 | if (!$ret_mode) { | |
2255 | print "<counter type=\"label\" id=\"-3\" counter=\"$count\"/>"; | |
2256 | } else { | |
2257 | $ret_arr["-3"]["counter"] = $count; | |
2258 | $ret_arr["-3"]["description"] = __("Fresh articles"); | |
2259 | } | |
2260 | ||
e4f4b46f | 2261 | |
a9cb1f83 AD |
2262 | $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM |
2263 | ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description"); | |
2264 | ||
2265 | while ($line = db_fetch_assoc($result)) { | |
2266 | ||
2267 | $id = -$line["id"] - 11; | |
2268 | ||
ef393de7 AD |
2269 | $label_name = $line["description"]; |
2270 | ||
a9cb1f83 AD |
2271 | error_reporting (0); |
2272 | ||
88040f57 | 2273 | $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds |
a9cb1f83 | 2274 | WHERE (" . $line["sql_exp"] . ") AND unread = true AND |
cfb02131 | 2275 | ttrss_feeds.hidden = false AND |
14073c0a | 2276 | $age_qpart AND |
88040f57 | 2277 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
a9cb1f83 | 2278 | ttrss_user_entries.ref_id = ttrss_entries.id AND |
88040f57 | 2279 | ttrss_user_entries.owner_uid = ".$_SESSION["uid"]); |
a9cb1f83 AD |
2280 | |
2281 | $count = db_fetch_result($tmp_result, 0, "count"); | |
2282 | ||
2283 | if (!$smart_mode || $old_counters[$id] != $count) { | |
2284 | $old_counters[$id] = $count; | |
2285 | $lctrs_modified = true; | |
ef393de7 AD |
2286 | if (!$ret_mode) { |
2287 | print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>"; | |
2288 | } else { | |
2289 | $ret_arr[$id]["counter"] = $count; | |
2290 | $ret_arr[$id]["description"] = $label_name; | |
2291 | } | |
a9cb1f83 AD |
2292 | } |
2293 | ||
2294 | error_reporting (DEFAULT_ERROR_LEVEL); | |
2295 | } | |
2296 | ||
2297 | if ($smart_mode && $lctrs_modified) { | |
2298 | $_SESSION["lctr_last_value"] = $old_counters; | |
2299 | } | |
ef393de7 AD |
2300 | |
2301 | return $ret_arr; | |
a9cb1f83 AD |
2302 | } |
2303 | ||
2304 | /* function getFeedCounter($link, $id) { | |
2305 | ||
2306 | $result = db_query($link, "SELECT | |
2307 | count(id) as count,last_error | |
2308 | FROM ttrss_entries,ttrss_user_entries,ttrss_feeds | |
2309 | WHERE feed_id = '$id' AND unread = true | |
2310 | AND ttrss_user_entries.feed_id = ttrss_feeds.id | |
2311 | AND ttrss_user_entries.ref_id = ttrss_entries.id"); | |
2312 | ||
2313 | $count = db_fetch_result($result, 0, "count"); | |
2314 | $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error")); | |
2315 | ||
2316 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>"; | |
2317 | } */ | |
2318 | ||
4ffa126e | 2319 | function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS, $active_feed = false) { |
a9cb1f83 | 2320 | |
14073c0a AD |
2321 | $age_qpart = getMaxAgeSubquery(); |
2322 | ||
a9cb1f83 AD |
2323 | if ($smart_mode) { |
2324 | if (!$_SESSION["fctr_last_value"]) { | |
2325 | $_SESSION["fctr_last_value"] = array(); | |
2326 | } | |
2327 | } | |
2328 | ||
2329 | $old_counters = $_SESSION["fctr_last_value"]; | |
2330 | ||
1b1b8a7b | 2331 | /* $result = db_query($link, "SELECT id,last_error,parent_feed, |
fb1fb4ab | 2332 | SUBSTRING(last_updated,1,19) AS last_updated, |
a9cb1f83 AD |
2333 | (SELECT count(id) |
2334 | FROM ttrss_entries,ttrss_user_entries | |
2335 | WHERE feed_id = ttrss_feeds.id AND | |
2336 | ttrss_user_entries.ref_id = ttrss_entries.id | |
2337 | AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count | |
2338 | FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . " | |
1b1b8a7b AD |
2339 | AND parent_feed IS NULL"); */ |
2340 | ||
14073c0a AD |
2341 | $query = "SELECT ttrss_feeds.id, |
2342 | ttrss_feeds.title, | |
1b1b8a7b AD |
2343 | SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated, |
2344 | last_error, | |
bc60fda3 | 2345 | COUNT(ttrss_entries.id) AS count |
1b1b8a7b AD |
2346 | FROM ttrss_feeds |
2347 | LEFT JOIN ttrss_user_entries ON (ttrss_user_entries.feed_id = ttrss_feeds.id | |
2348 | AND ttrss_user_entries.owner_uid = ttrss_feeds.owner_uid | |
2349 | AND ttrss_user_entries.unread = true) | |
14073c0a AD |
2350 | LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id AND |
2351 | $age_qpart) | |
2352 | WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]." | |
1b1b8a7b | 2353 | AND parent_feed IS NULL |
14073c0a | 2354 | GROUP BY ttrss_feeds.id, ttrss_feeds.title, ttrss_feeds.last_updated, last_error"; |
a9cb1f83 | 2355 | |
14073c0a | 2356 | $result = db_query($link, $query); |
a9cb1f83 AD |
2357 | $fctrs_modified = false; |
2358 | ||
fb1fb4ab AD |
2359 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); |
2360 | ||
a9cb1f83 AD |
2361 | while ($line = db_fetch_assoc($result)) { |
2362 | ||
2363 | $id = $line["id"]; | |
2364 | $count = $line["count"]; | |
2365 | $last_error = htmlspecialchars($line["last_error"]); | |
fb1fb4ab AD |
2366 | |
2367 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
2368 | $last_updated = smart_date_time(strtotime($line["last_updated"])); | |
2369 | } else { | |
2370 | $last_updated = date($short_date, strtotime($line["last_updated"])); | |
2371 | } | |
2372 | ||
588fb13b AD |
2373 | $last_updated = htmlspecialchars($last_updated); |
2374 | ||
a9cb1f83 AD |
2375 | $has_img = is_file(ICONS_DIR . "/$id.ico"); |
2376 | ||
2377 | $tmp_result = db_query($link, | |
14073c0a | 2378 | "SELECT ttrss_feeds.id,COUNT(unread) AS unread |
a9cb1f83 AD |
2379 | FROM ttrss_feeds LEFT JOIN ttrss_user_entries |
2380 | ON (ttrss_feeds.id = ttrss_user_entries.feed_id) | |
14073c0a AD |
2381 | LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id) |
2382 | WHERE parent_feed = '$id' AND $age_qpart AND unread = true GROUP BY ttrss_feeds.id"); | |
a9cb1f83 AD |
2383 | |
2384 | if (db_num_rows($tmp_result) > 0) { | |
2385 | while ($l = db_fetch_assoc($tmp_result)) { | |
2386 | $count += $l["unread"]; | |
2387 | } | |
2388 | } | |
2389 | ||
2390 | if (!$smart_mode || $old_counters[$id] != $count) { | |
2391 | $old_counters[$id] = $count; | |
2392 | $fctrs_modified = true; | |
2393 | ||
2394 | if ($last_error) { | |
2395 | $error_part = "error=\"$last_error\""; | |
2396 | } else { | |
2397 | $error_part = ""; | |
2398 | } | |
2399 | ||
2400 | if ($has_img) { | |
2401 | $has_img_part = "hi=\"$has_img\""; | |
2402 | } else { | |
2403 | $has_img_part = ""; | |
2404 | } | |
2405 | ||
4ffa126e AD |
2406 | if ($active_feed && $id == $active_feed) { |
2407 | $has_title_part = "title=\"" . htmlspecialchars($line["title"]) . "\""; | |
2408 | } else { | |
2409 | $has_title_part = ""; | |
2410 | } | |
2411 | ||
2412 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\" $has_title_part/>"; | |
a9cb1f83 AD |
2413 | } |
2414 | } | |
2415 | ||
2416 | if ($smart_mode && $fctrs_modified) { | |
2417 | $_SESSION["fctr_last_value"] = $old_counters; | |
2418 | } | |
2419 | } | |
2420 | ||
1b758780 | 2421 | function get_script_dt_add() { |
34e420fb | 2422 | if (strpos(VERSION, ".99") === false) { |
1b758780 AD |
2423 | return VERSION; |
2424 | } else { | |
2425 | return time(); | |
2426 | } | |
2427 | } | |
2428 | ||
6e7f8d26 AD |
2429 | function get_pgsql_version($link) { |
2430 | $result = db_query($link, "SELECT version() AS version"); | |
2431 | $version = split(" ", db_fetch_result($result, 0, "version")); | |
2432 | return $version[1]; | |
2433 | } | |
2434 | ||
af106b0e AD |
2435 | function print_error_xml($code, $add_msg = "") { |
2436 | global $ERRORS; | |
2437 | ||
2438 | $error_msg = $ERRORS[$code]; | |
2439 | ||
2440 | if ($add_msg) { | |
2441 | $error_msg = "$error_msg; $add_msg"; | |
2442 | } | |
2443 | ||
4c2abbc1 | 2444 | print "<rpc-reply>"; |
af106b0e | 2445 | print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>"; |
4c2abbc1 | 2446 | print "</rpc-reply>"; |
af106b0e | 2447 | } |
956c7629 | 2448 | |
f27de515 AD |
2449 | function subscribe_to_feed($link, $feed_link, $cat_id = 0, |
2450 | $auth_login = '', $auth_pass = '') { | |
bb0f29a4 | 2451 | |
b3dfe8ba | 2452 | # check for feed:http://url |
c91c2249 AD |
2453 | $feed_link = trim(preg_replace("/^feed:/", "", $feed_link)); |
2454 | ||
b3dfe8ba | 2455 | # check for feed://URL |
e2d84cdb | 2456 | if (strpos($feed_link, "//") === 0) { |
b3dfe8ba AD |
2457 | $feed_link = "http:$feed_link"; |
2458 | } | |
2459 | ||
c91c2249 | 2460 | if ($feed_link == "") return; |
bb0f29a4 | 2461 | |
956c7629 AD |
2462 | if ($cat_id == "0" || !$cat_id) { |
2463 | $cat_qpart = "NULL"; | |
2464 | } else { | |
2465 | $cat_qpart = "'$cat_id'"; | |
2466 | } | |
2467 | ||
2468 | $result = db_query($link, | |
2469 | "SELECT id FROM ttrss_feeds | |
2470 | WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]); | |
2471 | ||
2472 | if (db_num_rows($result) == 0) { | |
2473 | ||
2474 | $result = db_query($link, | |
f27de515 AD |
2475 | "INSERT INTO ttrss_feeds |
2476 | (owner_uid,feed_url,title,cat_id, auth_login,auth_pass) | |
956c7629 | 2477 | VALUES ('".$_SESSION["uid"]."', '$feed_link', |
f27de515 | 2478 | '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')"); |
956c7629 AD |
2479 | |
2480 | $result = db_query($link, | |
2481 | "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link' | |
f27de515 | 2482 | AND owner_uid = " . $_SESSION["uid"]); |
956c7629 AD |
2483 | |
2484 | $feed_id = db_fetch_result($result, 0, "id"); | |
2485 | ||
2486 | if ($feed_id) { | |
2487 | update_rss_feed($link, $feed_link, $feed_id, true); | |
2488 | } | |
2489 | ||
2490 | return true; | |
2491 | } else { | |
2492 | return false; | |
2493 | } | |
2494 | } | |
2495 | ||
673d54ca AD |
2496 | function print_feed_select($link, $id, $default_id = "", |
2497 | $attributes = "", $include_all_feeds = true) { | |
2498 | ||
79f3553b | 2499 | print "<select id=\"$id\" name=\"$id\" $attributes>"; |
673d54ca | 2500 | if ($include_all_feeds) { |
89cb787e | 2501 | print "<option value=\"0\">".__('All feeds')."</option>"; |
673d54ca AD |
2502 | } |
2503 | ||
2504 | $result = db_query($link, "SELECT id,title FROM ttrss_feeds | |
2505 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
2506 | ||
2507 | if (db_num_rows($result) > 0 && $include_all_feeds) { | |
2508 | print "<option disabled>--------</option>"; | |
2509 | } | |
2510 | ||
2511 | while ($line = db_fetch_assoc($result)) { | |
2512 | if ($line["id"] == $default_id) { | |
2513 | $is_selected = "selected"; | |
2514 | } else { | |
2515 | $is_selected = ""; | |
2516 | } | |
79f3553b | 2517 | printf("<option $is_selected value='%d'>%s</option>", |
47439031 | 2518 | $line["id"], htmlspecialchars($line["title"])); |
673d54ca AD |
2519 | } |
2520 | ||
2521 | print "</select>"; | |
2522 | } | |
2523 | ||
2524 | function print_feed_cat_select($link, $id, $default_id = "", | |
2525 | $attributes = "", $include_all_cats = true) { | |
2526 | ||
79f3553b | 2527 | print "<select id=\"$id\" name=\"$id\" $attributes>"; |
673d54ca AD |
2528 | |
2529 | if ($include_all_cats) { | |
d1db26aa | 2530 | print "<option value=\"0\">".__('Uncategorized')."</option>"; |
673d54ca AD |
2531 | } |
2532 | ||
2533 | $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories | |
2534 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
2535 | ||
2536 | if (db_num_rows($result) > 0 && $include_all_cats) { | |
2537 | print "<option disabled>--------</option>"; | |
2538 | } | |
2539 | ||
2540 | while ($line = db_fetch_assoc($result)) { | |
2541 | if ($line["id"] == $default_id) { | |
2542 | $is_selected = "selected"; | |
2543 | } else { | |
2544 | $is_selected = ""; | |
2545 | } | |
14f69488 | 2546 | printf("<option $is_selected value='%d'>%s</option>", |
47439031 | 2547 | $line["id"], htmlspecialchars($line["title"])); |
673d54ca AD |
2548 | } |
2549 | ||
2550 | print "</select>"; | |
2551 | } | |
2552 | ||
14f69488 AD |
2553 | function checkbox_to_sql_bool($val) { |
2554 | return ($val == "on") ? "true" : "false"; | |
2555 | } | |
86b682ce AD |
2556 | |
2557 | function getFeedCatTitle($link, $id) { | |
2558 | if ($id == -1) { | |
d1db26aa | 2559 | return __("Special"); |
86b682ce | 2560 | } else if ($id < -10) { |
d1db26aa | 2561 | return __("Labels"); |
86b682ce AD |
2562 | } else if ($id > 0) { |
2563 | $result = db_query($link, "SELECT ttrss_feed_categories.title | |
2564 | FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND | |
2565 | cat_id = ttrss_feed_categories.id"); | |
2566 | if (db_num_rows($result) == 1) { | |
2567 | return db_fetch_result($result, 0, "title"); | |
2568 | } else { | |
d1db26aa | 2569 | return __("Uncategorized"); |
86b682ce AD |
2570 | } |
2571 | } else { | |
2572 | return "getFeedCatTitle($id) failed"; | |
2573 | } | |
2574 | ||
2575 | } | |
2576 | ||
2577 | function getFeedTitle($link, $id) { | |
2578 | if ($id == -1) { | |
d1db26aa | 2579 | return __("Starred articles"); |
945c243e AD |
2580 | } else if ($id == -2) { |
2581 | return __("Published articles"); | |
2d24f032 AD |
2582 | } else if ($id == -3) { |
2583 | return __("Fresh articles"); | |
86b682ce AD |
2584 | } else if ($id < -10) { |
2585 | $label_id = -10 - $id; | |
2586 | $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'"); | |
2587 | if (db_num_rows($result) == 1) { | |
2588 | return db_fetch_result($result, 0, "description"); | |
2589 | } else { | |
2590 | return "Unknown label ($label_id)"; | |
2591 | } | |
2592 | ||
2593 | } else if ($id > 0) { | |
2594 | $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'"); | |
2595 | if (db_num_rows($result) == 1) { | |
2596 | return db_fetch_result($result, 0, "title"); | |
2597 | } else { | |
2598 | return "Unknown feed ($id)"; | |
2599 | } | |
2600 | } else { | |
2601 | return "getFeedTitle($id) failed"; | |
2602 | } | |
2603 | ||
2604 | } | |
3dd46f19 AD |
2605 | |
2606 | function get_session_cookie_name() { | |
2607 | return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME); | |
2608 | } | |
3ac2b520 AD |
2609 | |
2610 | function print_init_params($link) { | |
2611 | print "<init-params>"; | |
2612 | if ($_SESSION["stored-params"]) { | |
2613 | foreach (array_keys($_SESSION["stored-params"]) as $key) { | |
5f57b06d AD |
2614 | if ($key) { |
2615 | $value = htmlspecialchars($_SESSION["stored-params"][$key]); | |
2616 | print "<param key=\"$key\" value=\"$value\"/>"; | |
2617 | } | |
3ac2b520 AD |
2618 | } |
2619 | } | |
2620 | ||
2621 | print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>"; | |
2622 | print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>"; | |
0d51e25d | 2623 | print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>"; |
3ac2b520 AD |
2624 | |
2625 | print "<param key=\"on_catchup_show_next_feed\" value=\"" . | |
2626 | get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>"; | |
2627 | ||
e8bd0da9 | 2628 | print "<param key=\"hide_read_feeds\" value=\"" . |
465ff90b | 2629 | (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>"; |
e8bd0da9 | 2630 | |
c9268ed5 | 2631 | print "<param key=\"feeds_sort_by_unread\" value=\"" . |
465ff90b | 2632 | (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>"; |
c9268ed5 | 2633 | |
f6d6e22f | 2634 | print "<param key=\"confirm_feed_catchup\" value=\"" . |
465ff90b | 2635 | (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>"; |
f6d6e22f | 2636 | |
ac7bcd71 | 2637 | print "<param key=\"cdm_auto_catchup\" value=\"" . |
465ff90b | 2638 | (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>"; |
ac7bcd71 | 2639 | |
8e9c121b AD |
2640 | print "<param key=\"icons_url\" value=\"" . ICONS_URL . "\"/>"; |
2641 | ||
be0801a1 AD |
2642 | print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME . "\"/>"; |
2643 | ||
40496720 AD |
2644 | print "<param key=\"default_view_mode\" value=\"" . |
2645 | get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>"; | |
2646 | ||
2647 | print "<param key=\"default_view_limit\" value=\"" . | |
465ff90b | 2648 | (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>"; |
40496720 | 2649 | |
fe8d2059 AD |
2650 | print "<param key=\"prefs_active_tab\" value=\"" . |
2651 | get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>"; | |
2652 | ||
465ff90b AD |
2653 | print "<param key=\"infobox_disable_overlay\" value=\"" . |
2654 | get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>"; | |
2655 | ||
527c3bf0 AD |
2656 | print "<param key=\"icons_location\" value=\"" . |
2657 | ICONS_URL . "\"/>"; | |
2658 | ||
3ac2b520 AD |
2659 | print "</init-params>"; |
2660 | } | |
f54f515f AD |
2661 | |
2662 | function print_runtime_info($link) { | |
2663 | print "<runtime-info>"; | |
71ad883b AD |
2664 | if (ENABLE_UPDATE_DAEMON) { |
2665 | print "<param key=\"daemon_is_running\" value=\"". | |
2666 | sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>"; | |
8e00ae9b | 2667 | |
784b47a3 | 2668 | if ($_SESSION["daemon_stamp_check"] + 600 < time()) { |
8e00ae9b AD |
2669 | |
2670 | $stamp = (int)read_stampfile("update_daemon.stamp"); | |
2671 | ||
2672 | if ($stamp) { | |
2673 | if ($stamp + 86400*3 < time()) { | |
2674 | print "<param key=\"daemon_stamp_ok\" value=\"0\"/>"; | |
2675 | } else { | |
2676 | print "<param key=\"daemon_stamp_ok\" value=\"1\"/>"; | |
2677 | } | |
2678 | ||
2679 | $stamp_fmt = date("Y.m.d, G:i", $stamp); | |
2680 | ||
2681 | print "<param key=\"daemon_stamp\" value=\"$stamp_fmt\"/>"; | |
2682 | } | |
2683 | ||
2684 | $_SESSION["daemon_stamp_check"] = time(); | |
2685 | } | |
71ad883b | 2686 | } |
8e00ae9b | 2687 | |
d9fa39f1 AD |
2688 | if (CHECK_FOR_NEW_VERSION && $_SESSION["access_level"] >= 10) { |
2689 | ||
c04206e5 | 2690 | if ($_SESSION["last_version_check"] + 7200 < time()) { |
d9fa39f1 AD |
2691 | $new_version_details = check_for_update($link); |
2692 | ||
2693 | print "<param key=\"new_version_available\" value=\"". | |
2694 | sprintf("%d", $new_version_details != ""). "\"/>"; | |
2695 | ||
2696 | $_SESSION["last_version_check"] = time(); | |
2697 | } | |
2698 | } | |
2699 | ||
1c9df66e | 2700 | // print "<param key=\"new_version_available\" value=\"1\"/>"; |
b4507bc2 | 2701 | |
f54f515f AD |
2702 | print "</runtime-info>"; |
2703 | } | |
ef393de7 | 2704 | |
88040f57 | 2705 | function getSearchSql($search, $match_on) { |
ef393de7 | 2706 | |
88040f57 | 2707 | $search_query_part = ""; |
e20c9d88 | 2708 | |
88040f57 AD |
2709 | $keywords = split(" ", $search); |
2710 | $query_keywords = array(); | |
e20c9d88 | 2711 | |
88040f57 | 2712 | if ($match_on == "both") { |
e20c9d88 | 2713 | |
88040f57 AD |
2714 | foreach ($keywords as $k) { |
2715 | array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%') | |
2716 | OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))"); | |
2717 | } | |
e20c9d88 | 2718 | |
88040f57 | 2719 | $search_query_part = implode("AND", $query_keywords) . " AND "; |
e20c9d88 | 2720 | |
88040f57 | 2721 | } else if ($match_on == "title") { |
e20c9d88 | 2722 | |
88040f57 AD |
2723 | foreach ($keywords as $k) { |
2724 | array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))"); | |
2725 | } | |
e20c9d88 | 2726 | |
88040f57 | 2727 | $search_query_part = implode("AND", $query_keywords) . " AND "; |
e20c9d88 | 2728 | |
88040f57 AD |
2729 | } else if ($match_on == "content") { |
2730 | ||
2731 | foreach ($keywords as $k) { | |
2732 | array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))"); | |
2733 | } | |
2734 | } | |
2735 | ||
2736 | $search_query_part = implode("AND", $query_keywords); | |
2737 | ||
2738 | return $search_query_part; | |
2739 | } | |
2740 | ||
c36bf4d5 AD |
2741 | function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) { |
2742 | ||
2743 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
c1a0b534 | 2744 | |
88040f57 AD |
2745 | if ($search) { |
2746 | ||
2747 | $search_query_part = getSearchSql($search, $match_on); | |
2748 | $search_query_part .= " AND "; | |
e20c9d88 | 2749 | |
ef393de7 AD |
2750 | } else { |
2751 | $search_query_part = ""; | |
2752 | } | |
2753 | ||
2754 | $view_query_part = ""; | |
2755 | ||
2756 | if ($view_mode == "adaptive") { | |
2757 | if ($search) { | |
2758 | $view_query_part = " "; | |
2759 | } else if ($feed != -1) { | |
f295c368 | 2760 | $unread = getFeedUnread($link, $feed, $cat_view); |
ef393de7 AD |
2761 | if ($unread > 0) { |
2762 | $view_query_part = " unread = true AND "; | |
2763 | } | |
2764 | } | |
2765 | } | |
2766 | ||
2767 | if ($view_mode == "marked") { | |
2768 | $view_query_part = " marked = true AND "; | |
2769 | } | |
2770 | ||
2771 | if ($view_mode == "unread") { | |
2772 | $view_query_part = " unread = true AND "; | |
2773 | } | |
2774 | ||
2775 | if ($limit > 0) { | |
2776 | $limit_query_part = "LIMIT " . $limit; | |
2777 | } | |
2778 | ||
2779 | $vfeed_query_part = ""; | |
2780 | ||
2781 | // override query strategy and enable feed display when searching globally | |
2782 | if ($search && $search_mode == "all_feeds") { | |
2783 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2784 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2785 | } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) { | |
2786 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2787 | $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE | |
2788 | id = feed_id) as feed_title,"; | |
2789 | } else if ($feed >= 0 && $search && $search_mode == "this_cat") { | |
2790 | ||
2791 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
0a6c4846 AD |
2792 | |
2793 | $tmp_result = false; | |
2794 | ||
2795 | if ($cat_view) { | |
2796 | $tmp_result = db_query($link, "SELECT id | |
2797 | FROM ttrss_feeds WHERE cat_id = '$feed'"); | |
2798 | } else { | |
2799 | $tmp_result = db_query($link, "SELECT id | |
2800 | FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds | |
2801 | WHERE id = '$feed') AND id != '$feed'"); | |
2802 | } | |
ef393de7 AD |
2803 | |
2804 | $cat_siblings = array(); | |
2805 | ||
2806 | if (db_num_rows($tmp_result) > 0) { | |
2807 | while ($p = db_fetch_assoc($tmp_result)) { | |
2808 | array_push($cat_siblings, "feed_id = " . $p["id"]); | |
2809 | } | |
2810 | ||
2811 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
2812 | $feed, implode(" OR ", $cat_siblings)); | |
2813 | ||
2814 | } else { | |
2815 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2816 | } | |
2817 | ||
2818 | } else if ($feed >= 0) { | |
2819 | ||
2820 | if ($cat_view) { | |
5c365f60 | 2821 | |
ef393de7 AD |
2822 | if ($feed > 0) { |
2823 | $query_strategy_part = "cat_id = '$feed'"; | |
2824 | } else { | |
2825 | $query_strategy_part = "cat_id IS NULL"; | |
2826 | } | |
2827 | ||
2828 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
5c365f60 | 2829 | |
ef393de7 AD |
2830 | } else { |
2831 | $tmp_result = db_query($link, "SELECT id | |
2832 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
2833 | ORDER BY cat_id,title"); | |
2834 | ||
2835 | $parent_ids = array(); | |
2836 | ||
2837 | if (db_num_rows($tmp_result) > 0) { | |
2838 | while ($p = db_fetch_assoc($tmp_result)) { | |
2839 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
2840 | } | |
2841 | ||
2842 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
2843 | $feed, implode(" OR ", $parent_ids)); | |
2844 | ||
2845 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2846 | } else { | |
2847 | $query_strategy_part = "feed_id = '$feed'"; | |
2848 | } | |
2849 | } | |
2850 | } else if ($feed == -1) { // starred virtual feed | |
2851 | $query_strategy_part = "marked = true"; | |
2852 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
e4f4b46f AD |
2853 | } else if ($feed == -2) { // published virtual feed |
2854 | $query_strategy_part = "published = true"; | |
2d24f032 AD |
2855 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; |
2856 | } else if ($feed == -3) { // fresh virtual feed | |
2857 | $query_strategy_part = "unread = true"; | |
2858 | ||
c1d7e6c3 AD |
2859 | $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE"); |
2860 | ||
2d24f032 | 2861 | if (DB_TYPE == "pgsql") { |
c1d7e6c3 | 2862 | $query_strategy_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' "; |
2d24f032 | 2863 | } else { |
c1d7e6c3 | 2864 | $query_strategy_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) "; |
2d24f032 AD |
2865 | } |
2866 | ||
e4f4b46f | 2867 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; |
ef393de7 AD |
2868 | } else if ($feed <= -10) { // labels |
2869 | $label_id = -$feed - 11; | |
2870 | ||
2871 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
2872 | WHERE id = '$label_id'"); | |
2873 | ||
2874 | $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp"); | |
3de0261a AD |
2875 | |
2876 | if (!$query_strategy_part) { | |
2877 | return false; | |
2878 | } | |
2879 | ||
ef393de7 AD |
2880 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; |
2881 | } else { | |
2882 | $query_strategy_part = "id > 0"; // dumb | |
2883 | } | |
d6e5706d AD |
2884 | |
2885 | if (get_pref($link, 'REVERSE_HEADLINES')) { | |
2886 | $order_by = "updated"; | |
2887 | } else { | |
2888 | $order_by = "updated DESC"; | |
2889 | } | |
e939722a AD |
2890 | |
2891 | if ($override_order) { | |
2892 | $order_by = $override_order; | |
2893 | } | |
ef393de7 AD |
2894 | |
2895 | $feed_title = ""; | |
2896 | ||
2897 | if ($search && $search_mode == "all_feeds") { | |
b36e002f | 2898 | $feed_title = __("Search results")." ($search)"; |
ef393de7 | 2899 | } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) { |
b36e002f | 2900 | $feed_title = __("Search results")." ($search, $feed)"; |
ef393de7 AD |
2901 | } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) { |
2902 | $feed_title = $feed; | |
2903 | } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) { | |
2904 | ||
2905 | if ($cat_view) { | |
5c365f60 | 2906 | |
ef393de7 AD |
2907 | if ($feed != 0) { |
2908 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories | |
c36bf4d5 | 2909 | WHERE id = '$feed' AND owner_uid = $owner_uid"); |
ef393de7 AD |
2910 | $feed_title = db_fetch_result($result, 0, "title"); |
2911 | } else { | |
d1db26aa | 2912 | $feed_title = __("Uncategorized"); |
ef393de7 | 2913 | } |
e1eb2147 AD |
2914 | |
2915 | if ($search) { | |
b36e002f | 2916 | $feed_title = __("Searched for")." $search ($feed_title)"; |
e1eb2147 AD |
2917 | } |
2918 | ||
ef393de7 AD |
2919 | } else { |
2920 | ||
2921 | $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds | |
c36bf4d5 | 2922 | WHERE id = '$feed' AND owner_uid = $owner_uid"); |
ef393de7 AD |
2923 | |
2924 | $feed_title = db_fetch_result($result, 0, "title"); | |
2925 | $feed_site_url = db_fetch_result($result, 0, "site_url"); | |
2926 | $last_error = db_fetch_result($result, 0, "last_error"); | |
e1eb2147 AD |
2927 | |
2928 | if ($search) { | |
b36e002f | 2929 | $feed_title = __("Searched for") . " $search ($feed_title)"; |
e1eb2147 | 2930 | } |
ef393de7 AD |
2931 | } |
2932 | ||
2933 | } else if ($feed == -1) { | |
d1db26aa | 2934 | $feed_title = __("Starred articles"); |
e4f4b46f AD |
2935 | } else if ($feed == -2) { |
2936 | $feed_title = __("Published articles"); | |
2d24f032 AD |
2937 | } else if ($feed == -3) { |
2938 | $feed_title = __("Fresh articles"); | |
ef393de7 AD |
2939 | } else if ($feed < -10) { |
2940 | $label_id = -$feed - 11; | |
2941 | $result = db_query($link, "SELECT description FROM ttrss_labels | |
2942 | WHERE id = '$label_id'"); | |
2943 | $feed_title = db_fetch_result($result, 0, "description"); | |
88040f57 AD |
2944 | |
2945 | if ($search) { | |
b36e002f | 2946 | $feed_title = __("Searched for") . " $search ($feed_title)"; |
88040f57 | 2947 | } |
ef393de7 AD |
2948 | } else { |
2949 | $feed_title = "?"; | |
2950 | } | |
2951 | ||
ef393de7 AD |
2952 | if ($feed < -10) error_reporting (0); |
2953 | ||
2954 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
2955 | ||
2956 | if ($feed >= 0) { | |
2957 | $feed_kind = "Feeds"; | |
2958 | } else { | |
2959 | $feed_kind = "Labels"; | |
2960 | } | |
2961 | ||
2962 | $content_query_part = "content as content_preview,"; | |
203de776 | 2963 | |
95a82c08 AD |
2964 | if ($limit_query_part) { |
2965 | $offset_query_part = "OFFSET $offset"; | |
2966 | } | |
2967 | ||
ef393de7 | 2968 | $query = "SELECT |
1f64b1be | 2969 | guid, |
ef393de7 | 2970 | ttrss_entries.id,ttrss_entries.title, |
46921916 | 2971 | updated, |
e4f4b46f | 2972 | unread,feed_id,marked,published,link,last_read, |
ef393de7 AD |
2973 | SUBSTRING(last_read,1,19) as last_read_noms, |
2974 | $vfeed_query_part | |
2975 | $content_query_part | |
d4b4b9de AD |
2976 | SUBSTRING(updated,1,19) as updated_noms, |
2977 | author | |
ef393de7 AD |
2978 | FROM |
2979 | ttrss_entries,ttrss_user_entries,ttrss_feeds | |
2980 | WHERE | |
cfb02131 | 2981 | ttrss_feeds.hidden = false AND |
ef393de7 AD |
2982 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
2983 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
c36bf4d5 | 2984 | ttrss_user_entries.owner_uid = '$owner_uid' AND |
ef393de7 AD |
2985 | $search_query_part |
2986 | $view_query_part | |
2987 | $query_strategy_part ORDER BY $order_by | |
95a82c08 | 2988 | $limit_query_part $offset_query_part"; |
ef393de7 AD |
2989 | |
2990 | $result = db_query($link, $query); | |
2991 | ||
2992 | if ($_GET["debug"]) print $query; | |
2993 | ||
2994 | } else { | |
2995 | // browsing by tag | |
2996 | ||
2997 | $feed_kind = "Tags"; | |
2998 | ||
2999 | $result = db_query($link, "SELECT | |
1f64b1be | 3000 | guid, |
ef393de7 | 3001 | ttrss_entries.id as id,title, |
46921916 | 3002 | updated, |
ef393de7 AD |
3003 | unread,feed_id, |
3004 | marked,link,last_read, | |
3005 | SUBSTRING(last_read,1,19) as last_read_noms, | |
3006 | $vfeed_query_part | |
3007 | $content_query_part | |
3008 | SUBSTRING(updated,1,19) as updated_noms | |
3009 | FROM | |
3010 | ttrss_entries,ttrss_user_entries,ttrss_tags | |
3011 | WHERE | |
3012 | ref_id = ttrss_entries.id AND | |
c36bf4d5 | 3013 | ttrss_user_entries.owner_uid = '$owner_uid' AND |
ef393de7 AD |
3014 | post_int_id = int_id AND tag_name = '$feed' AND |
3015 | $view_query_part | |
3016 | $search_query_part | |
3017 | $query_strategy_part ORDER BY $order_by | |
3018 | $limit_query_part"); | |
3019 | } | |
3020 | ||
c7188969 | 3021 | return array($result, $feed_title, $feed_site_url, $last_error); |
ef393de7 AD |
3022 | |
3023 | } | |
3024 | ||
c36bf4d5 | 3025 | function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat, |
3baeeeca | 3026 | $search, $search_mode, $match_on) { |
18664970 AD |
3027 | |
3028 | $qfh_ret = queryFeedHeadlines($link, $feed, | |
c36bf4d5 AD |
3029 | 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC", 0, |
3030 | $owner_uid); | |
18664970 AD |
3031 | |
3032 | $result = $qfh_ret[0]; | |
59e2aab4 | 3033 | $feed_title = htmlspecialchars($qfh_ret[1]); |
18664970 AD |
3034 | $feed_site_url = $qfh_ret[2]; |
3035 | $last_error = $qfh_ret[3]; | |
3036 | ||
a5472764 | 3037 | // if (!$feed_site_url) $feed_site_url = "http://localhost/"; |
4bc64807 | 3038 | |
a5472764 AD |
3039 | print "<?xml version=\"1.0\" encoding=\"utf-8\"?> |
3040 | <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?> | |
3041 | <rss version=\"2.0\"> | |
3baeeeca AD |
3042 | <channel> |
3043 | <title>$feed_title</title> | |
4bc64807 AD |
3044 | <link>$feed_site_url</link> |
3045 | <description>Feed generated by Tiny Tiny RSS</description>"; | |
3baeeeca AD |
3046 | |
3047 | while ($line = db_fetch_assoc($result)) { | |
3048 | print "<item>"; | |
4bc64807 | 3049 | print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>"; |
3baeeeca | 3050 | print "<link>" . htmlspecialchars($line["link"]) . "</link>"; |
0c3d1c68 | 3051 | |
bc976a8c | 3052 | $tags = get_article_tags($link, $line["id"], $owner_uid); |
0c3d1c68 AD |
3053 | |
3054 | foreach ($tags as $tag) { | |
3055 | print "<category>" . htmlspecialchars($tag) . "</category>"; | |
3056 | } | |
3057 | ||
3baeeeca AD |
3058 | $rfc822_date = date('r', strtotime($line["updated"])); |
3059 | ||
3060 | print "<pubDate>$rfc822_date</pubDate>"; | |
3061 | ||
3062 | print "<title>" . | |
3063 | htmlspecialchars($line["title"]) . "</title>"; | |
3064 | ||
0c3d1c68 AD |
3065 | print "<description><![CDATA[" . |
3066 | $line["content_preview"] . "]]></description>"; | |
3baeeeca AD |
3067 | |
3068 | print "</item>"; | |
3069 | } | |
3070 | ||
3071 | print "</channel></rss>"; | |
18664970 AD |
3072 | |
3073 | } | |
3074 | ||
0a6c4846 AD |
3075 | function getCategoryTitle($link, $cat_id) { |
3076 | ||
bba7c4bf AD |
3077 | if ($cat_id == -1) { |
3078 | return __("Special"); | |
3079 | } else if ($cat_id == -2) { | |
3080 | return __("Labels"); | |
0a6c4846 | 3081 | } else { |
bba7c4bf AD |
3082 | |
3083 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE | |
3084 | id = '$cat_id'"); | |
3085 | ||
3086 | if (db_num_rows($result) == 1) { | |
3087 | return db_fetch_result($result, 0, "title"); | |
3088 | } else { | |
3089 | return "Uncategorized"; | |
3090 | } | |
0a6c4846 AD |
3091 | } |
3092 | } | |
3093 | ||
f738aef1 AD |
3094 | // http://ru2.php.net/strip-tags |
3095 | ||
3096 | function strip_tags_long($textstring, $allowed){ | |
3097 | while($textstring != strip_tags($textstring, $allowed)) | |
3098 | { | |
3099 | while (strlen($textstring) != 0) | |
3100 | { | |
3101 | if (strlen($textstring) > 1024) { | |
3102 | $otherlen = 1024; | |
3103 | } else { | |
3104 | $otherlen = strlen($textstring); | |
3105 | } | |
3106 | $temptext = strip_tags(substr($textstring,0,$otherlen), $allowed); | |
3107 | $safetext .= $temptext; | |
3108 | $textstring = substr_replace($textstring,'',0,$otherlen); | |
3109 | } | |
3110 | $textstring = $safetext; | |
3111 | } | |
3112 | return $textstring; | |
3113 | } | |
3114 | ||
3115 | ||
1ac0baf4 | 3116 | function sanitize_rss($link, $str, $force_strip_tags = false) { |
60452879 | 3117 | $res = $str; |
183ad07b | 3118 | |
1ac0baf4 | 3119 | if (get_pref($link, "STRIP_UNSAFE_TAGS") || $force_strip_tags) { |
f738aef1 AD |
3120 | global $tw_parser; |
3121 | global $tw_paranoya_setup; | |
3122 | ||
3123 | $res = $tw_parser->strip_tags($res, $tw_paranoya_setup); | |
3124 | ||
3125 | // $res = preg_replace("/\r\n|\n|\r/", "", $res); | |
3126 | // $res = strip_tags_long($res, "<p><a><i><em><b><strong><blockquote><br><img><div><span>"); | |
f826eee1 AD |
3127 | } |
3128 | ||
183ad07b AD |
3129 | return $res; |
3130 | } | |
b72c3ef8 | 3131 | |
9cd7c995 AD |
3132 | function send_headlines_digests($link, $limit = 100) { |
3133 | ||
1ddba275 AD |
3134 | if (!DIGEST_ENABLE) return false; |
3135 | ||
9cd7c995 | 3136 | $user_limit = DIGEST_EMAIL_LIMIT; |
5430c959 | 3137 | $days = 1; |
9cd7c995 AD |
3138 | |
3139 | print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n"; | |
3140 | ||
3141 | if (DB_TYPE == "pgsql") { | |
3142 | $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'"; | |
3143 | } else if (DB_TYPE == "mysql") { | |
3144 | $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
3145 | } | |
3146 | ||
3147 | $result = db_query($link, "SELECT id,email FROM ttrss_users | |
3148 | WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)"); | |
3149 | ||
3150 | while ($line = db_fetch_assoc($result)) { | |
3151 | if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) { | |
3152 | print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... "; | |
3153 | ||
3154 | $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit); | |
3155 | $digest = $tuple[0]; | |
3156 | $headlines_count = $tuple[1]; | |
3157 | ||
3158 | if ($headlines_count > 0) { | |
3159 | $rc = mail($line["login"] . " <" . $line["email"] . ">", | |
3160 | "[tt-rss] New headlines for last 24 hours", $digest, | |
3ab3c1f0 AD |
3161 | "From: " . MAIL_FROM . "\n". |
3162 | "Content-Type: text/plain; charset=\"utf-8\"\n". | |
3163 | "Content-Transfer-Encoding: 8bit\n"); | |
9cd7c995 AD |
3164 | print "RC=$rc\n"; |
3165 | db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW() | |
3166 | WHERE id = " . $line["id"]); | |
3167 | } else { | |
3168 | print "No headlines\n"; | |
3169 | } | |
3170 | } | |
3171 | } | |
3172 | ||
3173 | // $digest = prepare_headlines_digest($link, $user_id, $days, $limit); | |
3174 | ||
3175 | } | |
3176 | ||
7e3634d9 | 3177 | function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) { |
d1db26aa | 3178 | $tmp = __("New headlines for last 24 hours, as of ") . date("Y/m/d H:m") . "\n"; |
7e3634d9 AD |
3179 | $tmp .= "=======================================================\n\n"; |
3180 | ||
3181 | if (DB_TYPE == "pgsql") { | |
9cd7c995 | 3182 | $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'"; |
7e3634d9 AD |
3183 | } else if (DB_TYPE == "mysql") { |
3184 | $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
3185 | } | |
3186 | ||
3187 | $result = db_query($link, "SELECT ttrss_entries.title, | |
3188 | ttrss_feeds.title AS feed_title, | |
3189 | date_entered, | |
3190 | link, | |
3191 | SUBSTRING(last_updated,1,19) AS last_updated | |
3192 | FROM | |
3193 | ttrss_user_entries,ttrss_entries,ttrss_feeds | |
3194 | WHERE | |
3195 | ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id | |
3dd9183c | 3196 | AND include_in_digest = true |
7e3634d9 | 3197 | AND $interval_query |
448b0abd | 3198 | AND ttrss_user_entries.owner_uid = $user_id |
7e3634d9 AD |
3199 | AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC |
3200 | LIMIT $limit"); | |
3201 | ||
3202 | $cur_feed_title = ""; | |
3203 | ||
9cd7c995 AD |
3204 | $headlines_count = db_num_rows($result); |
3205 | ||
7e3634d9 AD |
3206 | while ($line = db_fetch_assoc($result)) { |
3207 | $updated = smart_date_time(strtotime($line["last_updated"])); | |
3208 | $feed_title = $line["feed_title"]; | |
3209 | ||
3210 | if ($cur_feed_title != $feed_title) { | |
3211 | $cur_feed_title = $feed_title; | |
3212 | ||
3213 | $tmp .= "$feed_title\n\n"; | |
3214 | } | |
3215 | ||
3216 | $tmp .= " * " . trim($line["title"]) . " - $updated\n"; | |
3217 | $tmp .= " " . trim($line["link"]) . "\n"; | |
3218 | $tmp .= "\n"; | |
3219 | } | |
3220 | ||
3221 | $tmp .= "--- \n"; | |
d1db26aa | 3222 | $tmp .= __("You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at ") . |
dfe6f833 | 3223 | DIGEST_HOSTNAME . "\n". |
d1db26aa | 3224 | __("To unsubscribe, visit your configuration options or contact instance owner.\n"); |
7e3634d9 AD |
3225 | |
3226 | ||
9cd7c995 | 3227 | return array($tmp, $headlines_count); |
7e3634d9 AD |
3228 | } |
3229 | ||
d9fa39f1 | 3230 | function check_for_update($link, $brief_fmt = true) { |
b72c3ef8 AD |
3231 | $releases_feed = "http://tt-rss.spb.ru/releases.rss"; |
3232 | ||
3233 | if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) { | |
3234 | return; | |
3235 | } | |
3236 | ||
3237 | error_reporting(0); | |
f67d9754 AD |
3238 | if (ENABLE_SIMPLEPIE) { |
3239 | $rss = new SimplePie(); | |
3240 | $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT); | |
3241 | $rss->set_timeout(MAGPIE_FETCH_TIME_OUT); | |
3242 | $rss->set_feed_url($fetch_url); | |
3243 | $rss->set_output_encoding('UTF-8'); | |
3244 | $rss->init(); | |
3245 | } else { | |
3246 | $rss = fetch_rss($releases_feed); | |
3247 | } | |
b72c3ef8 AD |
3248 | error_reporting (DEFAULT_ERROR_LEVEL); |
3249 | ||
3250 | if ($rss) { | |
3251 | ||
f67d9754 AD |
3252 | if (ENABLE_SIMPLEPIE) { |
3253 | $items = $rss->get_items(); | |
3254 | } else { | |
3255 | $items = $rss->items; | |
b72c3ef8 | 3256 | |
f67d9754 AD |
3257 | if (!$items || !is_array($items)) $items = $rss->entries; |
3258 | if (!$items || !is_array($items)) $items = $rss; | |
3259 | } | |
b72c3ef8 | 3260 | |
da412ad3 | 3261 | if (!is_array($items) || count($items) == 0) { |
b72c3ef8 | 3262 | return; |
da412ad3 | 3263 | } |
b72c3ef8 | 3264 | |
a41d2c65 | 3265 | $latest_item = $items[0]; |
b72c3ef8 | 3266 | |
f67d9754 AD |
3267 | if (ENABLE_SIMPLEPIE) { |
3268 | $last_title = $latest_item->get_title(); | |
3269 | } else { | |
3270 | $last_title = $latest_item["title"]; | |
3271 | } | |
b72c3ef8 | 3272 | |
f67d9754 AD |
3273 | $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $last_title)); |
3274 | ||
3275 | if (ENABLE_SIMPLEPIE) { | |
dcf7fd08 AD |
3276 | $release_url = sanitize_rss($link, $latest_item->get_link()); |
3277 | $content = sanitize_rss($link, $latest_item->get_description()); | |
f67d9754 AD |
3278 | } else { |
3279 | $release_url = sanitize_rss($link, $latest_item["link"]); | |
3280 | $content = sanitize_rss($link, $latest_item["description"]); | |
3281 | } | |
48e1a342 | 3282 | |
a41d2c65 | 3283 | if (version_compare(VERSION, $latest_version) == -1) { |
d9fa39f1 | 3284 | if ($brief_fmt) { |
0d32b41e | 3285 | return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\"> |
d9fa39f1 | 3286 | New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a> |
0d32b41e | 3287 | <div id=\"milestoneDetails\">$content</div>"); |
d9fa39f1 | 3288 | } else { |
92625568 AD |
3289 | return "New version of Tiny-Tiny RSS ($latest_version) is available: |
3290 | <div class='milestoneDetails'>$content</div> | |
3291 | Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for | |
3292 | download and update information."; | |
d9fa39f1 AD |
3293 | } |
3294 | ||
da412ad3 | 3295 | } |
b72c3ef8 AD |
3296 | } |
3297 | } | |
472782e8 | 3298 | |
18eddb2c AD |
3299 | function markArticlesById($link, $ids, $cmode) { |
3300 | ||
3301 | $tmp_ids = array(); | |
3302 | ||
3303 | foreach ($ids as $id) { | |
3304 | array_push($tmp_ids, "ref_id = '$id'"); | |
3305 | } | |
3306 | ||
3307 | $ids_qpart = join(" OR ", $tmp_ids); | |
3308 | ||
3309 | if ($cmode == 0) { | |
3310 | db_query($link, "UPDATE ttrss_user_entries SET | |
3311 | marked = false,last_read = NOW() | |
3312 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3313 | } else if ($cmode == 1) { | |
3314 | db_query($link, "UPDATE ttrss_user_entries SET | |
3315 | marked = true | |
3316 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3317 | } else { | |
3318 | db_query($link, "UPDATE ttrss_user_entries SET | |
3319 | marked = NOT marked,last_read = NOW() | |
3320 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3321 | } | |
3322 | } | |
3323 | ||
e4f4b46f AD |
3324 | function publishArticlesById($link, $ids, $cmode) { |
3325 | ||
3326 | $tmp_ids = array(); | |
3327 | ||
3328 | foreach ($ids as $id) { | |
3329 | array_push($tmp_ids, "ref_id = '$id'"); | |
3330 | } | |
3331 | ||
3332 | $ids_qpart = join(" OR ", $tmp_ids); | |
3333 | ||
3334 | if ($cmode == 0) { | |
3335 | db_query($link, "UPDATE ttrss_user_entries SET | |
3336 | published = false,last_read = NOW() | |
3337 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3338 | } else if ($cmode == 1) { | |
3339 | db_query($link, "UPDATE ttrss_user_entries SET | |
3340 | published = true | |
3341 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3342 | } else { | |
3343 | db_query($link, "UPDATE ttrss_user_entries SET | |
3344 | published = NOT published,last_read = NOW() | |
3345 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3346 | } | |
3347 | } | |
3348 | ||
472782e8 AD |
3349 | function catchupArticlesById($link, $ids, $cmode) { |
3350 | ||
3351 | $tmp_ids = array(); | |
3352 | ||
3353 | foreach ($ids as $id) { | |
3354 | array_push($tmp_ids, "ref_id = '$id'"); | |
3355 | } | |
3356 | ||
3357 | $ids_qpart = join(" OR ", $tmp_ids); | |
3358 | ||
3359 | if ($cmode == 0) { | |
3360 | db_query($link, "UPDATE ttrss_user_entries SET | |
3361 | unread = false,last_read = NOW() | |
3362 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3363 | } else if ($cmode == 1) { | |
3364 | db_query($link, "UPDATE ttrss_user_entries SET | |
3365 | unread = true | |
3366 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3367 | } else { | |
3368 | db_query($link, "UPDATE ttrss_user_entries SET | |
3369 | unread = NOT unread,last_read = NOW() | |
3370 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3371 | } | |
3372 | } | |
3373 | ||
e097e8be AD |
3374 | function catchupArticleById($link, $id, $cmode) { |
3375 | ||
3376 | if ($cmode == 0) { | |
3377 | db_query($link, "UPDATE ttrss_user_entries SET | |
3378 | unread = false,last_read = NOW() | |
3379 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
3380 | } else if ($cmode == 1) { | |
3381 | db_query($link, "UPDATE ttrss_user_entries SET | |
3382 | unread = true | |
3383 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
3384 | } else { | |
3385 | db_query($link, "UPDATE ttrss_user_entries SET | |
3386 | unread = NOT unread,last_read = NOW() | |
3387 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
3388 | } | |
3389 | } | |
3390 | ||
1f64b1be AD |
3391 | function make_guid_from_title($title) { |
3392 | return preg_replace("/[ \"\',.:;]/", "-", | |
fefef828 | 3393 | mb_strtolower(strip_tags($title), 'utf-8')); |
1f64b1be AD |
3394 | } |
3395 | ||
11befbb2 AD |
3396 | function print_headline_subtoolbar($link, $feed_site_url, $feed_title, |
3397 | $bottom = false, $rtl_content = false, $feed_id = 0, | |
3398 | $is_cat = false, $search = false, $match_on = false, | |
95a82c08 | 3399 | $search_mode = false, $offset = 0, $limit = 0) { |
11befbb2 | 3400 | |
e6c115b2 AD |
3401 | $user_page_offset = $offset + 1; |
3402 | ||
11befbb2 AD |
3403 | if (!$bottom) { |
3404 | $class = "headlinesSubToolbar"; | |
3405 | $tid = "headlineActionsTop"; | |
3406 | } else { | |
3407 | $class = "headlinesSubToolbar"; | |
3408 | $tid = "headlineActionsBottom"; | |
3409 | } | |
3410 | ||
3411 | print "<table class=\"$class\" id=\"$tid\" | |
3412 | width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>"; | |
3413 | ||
3414 | if ($rtl_content) { | |
3415 | $rtl_cpart = "RTL"; | |
3416 | } else { | |
3417 | $rtl_cpart = ""; | |
3418 | } | |
3419 | ||
e6c115b2 AD |
3420 | $page_prev_link = "javascript:viewFeedGoPage(-1)"; |
3421 | $page_next_link = "javascript:viewFeedGoPage(1)"; | |
3422 | $page_first_link = "javascript:viewFeedGoPage(0)"; | |
203de776 | 3423 | |
eb28b131 AD |
3424 | $catchup_page_link = "javascript:catchupPage()"; |
3425 | $catchup_feed_link = "javascript:catchupCurrentFeed()"; | |
a5ae125a | 3426 | $catchup_sel_link = "javascript:catchupSelection()"; |
c6008b62 | 3427 | |
11befbb2 AD |
3428 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) { |
3429 | ||
c6008b62 AD |
3430 | $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)"; |
3431 | $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)"; | |
3432 | $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)"; | |
11befbb2 | 3433 | |
c6008b62 AD |
3434 | $tog_unread_link = "javascript:selectionToggleUnread()"; |
3435 | $tog_marked_link = "javascript:selectionToggleMarked()"; | |
e4f4b46f | 3436 | $tog_published_link = "javascript:selectionTogglePublished()"; |
11befbb2 | 3437 | |
c6008b62 | 3438 | } else { |
11befbb2 | 3439 | |
c6008b62 AD |
3440 | $sel_all_link = "javascript:cdmSelectArticles('all')"; |
3441 | $sel_unread_link = "javascript:cdmSelectArticles('unread')"; | |
3442 | $sel_none_link = "javascript:cdmSelectArticles('none')"; | |
3443 | ||
3444 | $tog_unread_link = "javascript:selectionToggleUnread(true)"; | |
3445 | $tog_marked_link = "javascript:selectionToggleMarked(true)"; | |
e4f4b46f | 3446 | $tog_published_link = "javascript:selectionTogglePublished(true)"; |
c6008b62 AD |
3447 | |
3448 | } | |
3449 | ||
d420f2ee | 3450 | if (strpos($_SESSION["client.userAgent"], "MSIE") === false) { |
c6008b62 | 3451 | |
2dd2c13b AD |
3452 | print "<td class=\"headlineActions$rtl_cpart\"> |
3453 | <ul class=\"headlineDropdownMenu\"> | |
3454 | <li class=\"top2\"> | |
3692e98f | 3455 | ".__('Select:')." |
1025ad87 AD |
3456 | <a href=\"$sel_all_link\">".__('All')."</a>, |
3457 | <a href=\"$sel_unread_link\">".__('Unread')."</a>, | |
3458 | <a href=\"$sel_none_link\">".__('None')."</a></li> | |
2dd2c13b | 3459 | <li class=\"vsep\"> </li> |
89cb787e | 3460 | <li class=\"top\">".__('Toggle')."<ul> |
9cc600d1 | 3461 | <li onclick=\"$tog_unread_link\">".__('Unread')."</li> |
e4f4b46f AD |
3462 | <li onclick=\"$tog_marked_link\">".__('Starred')."</li> |
3463 | <li onclick=\"$tog_published_link\">".__('Published')."</li> | |
3464 | </ul></li> | |
2dd2c13b | 3465 | <li class=\"vsep\"> </li> |
1025ad87 | 3466 | <li class=\"top\"><a href=\"$catchup_page_link\">".__('Mark as read')."</a><ul> |
a5ae125a | 3467 | <li onclick=\"$catchup_sel_link\">".__('Selection')."</li> |
8be83f42 AD |
3468 | <!-- <li onclick=\"$catchup_page_link\">".__('This page')."</li> --> |
3469 | <li><span class=\"insensitive\">--------</span></li> | |
3470 | <li onclick=\"catchupRelativeToArticle(0)\">".__("Above active article")."</li> | |
3471 | <li onclick=\"catchupRelativeToArticle(1)\">".__("Below active article")."</li> | |
3472 | <li><span class=\"insensitive\">--------</span></li> | |
1025ad87 | 3473 | <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li> |
237ec2ad | 3474 | "; |
95a82c08 | 3475 | |
237ec2ad AD |
3476 | $enable_pagination = get_pref($link, "_PREFS_ENABLE_PAGINATION"); |
3477 | ||
3478 | if ($limit != 0 && !$search && $enable_pagination) { | |
95a82c08 | 3479 | print " |
237ec2ad | 3480 | <li class=\"vsep\"> </li> |
1025ad87 AD |
3481 | <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul> |
3482 | <li onclick=\"$page_prev_link\">".__('Previous page')."</li> | |
3483 | <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li> | |
95a82c08 | 3484 | </ul>"; |
d420f2ee | 3485 | } |
95a82c08 | 3486 | |
d420f2ee | 3487 | if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) { |
237ec2ad AD |
3488 | print " |
3489 | <li class=\"vsep\"> </li> | |
3490 | <li class=\"top3\"> | |
d420f2ee AD |
3491 | <a href=\"javascript:labelFromSearch('$search', '$search_mode', |
3492 | '$match_on', '$feed_id', '$is_cat');\"> | |
b36e002f | 3493 | ".__('Convert to label')."</a></td>"; |
d420f2ee | 3494 | } |
95a82c08 | 3495 | print " |
2dd2c13b AD |
3496 | </td>"; |
3497 | ||
3498 | } else { | |
e6c115b2 AD |
3499 | // old style subtoolbar: |
3500 | ||
2dd2c13b | 3501 | print "<td class=\"headlineActions$rtl_cpart\">". |
d1db26aa | 3502 | __('Select:')." |
1025ad87 AD |
3503 | <a href=\"$sel_all_link\">".__('All')."</a>, |
3504 | <a href=\"$sel_unread_link\">".__('Unread')."</a>, | |
3505 | <a href=\"$sel_none_link\">".__('None')."</a> | |
2dd2c13b | 3506 | ". |
1025ad87 AD |
3507 | __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>, |
3508 | <a href=\"$tog_marked_link\">".__('Starred')."</a> | |
2dd2c13b | 3509 | ". |
d1db26aa | 3510 | __('Mark as read:')." |
1025ad87 AD |
3511 | <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>, |
3512 | <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>"; | |
d420f2ee AD |
3513 | |
3514 | if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) { | |
3515 | ||
3516 | print " | |
3517 | <a href=\"javascript:labelFromSearch('$search', '$search_mode', | |
3518 | '$match_on', '$feed_id', '$is_cat');\"> | |
b36e002f | 3519 | ".__('Convert to label')."</a>"; |
d420f2ee AD |
3520 | } |
3521 | ||
2dd2c13b AD |
3522 | print "</td>"; |
3523 | ||
3524 | } | |
c6008b62 | 3525 | |
d420f2ee | 3526 | /* if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) { |
c6008b62 AD |
3527 | print "<td class=\"headlineActions$rtl_cpart\"> |
3528 | <a href=\"javascript:labelFromSearch('$search', '$search_mode', | |
3529 | '$match_on', '$feed_id', '$is_cat');\"> | |
d1db26aa | 3530 | ".__('Convert to Label')."</a></td>"; |
d420f2ee | 3531 | } */ |
11befbb2 AD |
3532 | |
3533 | print "<td class=\"headlineTitle$rtl_cpart\">"; | |
3534 | ||
3535 | if ($feed_site_url) { | |
3536 | if (!$bottom) { | |
3fc2eed5 | 3537 | $target = "target=\"_new\""; |
11befbb2 | 3538 | } |
02815555 AD |
3539 | print "<a $target href=\"$feed_site_url\">". |
3540 | truncate_string($feed_title,30)."</a>"; | |
11befbb2 AD |
3541 | } else { |
3542 | print $feed_title; | |
3543 | } | |
3544 | ||
3545 | if ($search) { | |
3546 | $search_q = "&q=$search&m=$match_on&smode=$search_mode"; | |
3547 | } | |
3548 | ||
e6c115b2 AD |
3549 | if ($user_page_offset > 1) { |
3550 | print " [$user_page_offset] "; | |
3551 | } | |
3552 | ||
11befbb2 | 3553 | if (!$bottom) { |
e6c115b2 | 3554 | print " |
11befbb2 AD |
3555 | <a target=\"_new\" |
3556 | href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\"> | |
3557 | <img class=\"noborder\" | |
1025ad87 | 3558 | alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\"> |
11befbb2 AD |
3559 | </a>"; |
3560 | } | |
3561 | ||
3562 | print "</td>"; | |
3563 | print "</tr></table>"; | |
3564 | ||
3565 | } | |
3566 | ||
bba7c4bf AD |
3567 | function printCategoryHeader($link, $cat_id, $hidden = false, $can_browse = true) { |
3568 | ||
3569 | $tmp_category = getCategoryTitle($link, $cat_id); | |
3570 | $cat_unread = getCategoryUnread($link, $cat_id); | |
3571 | ||
3572 | if ($hidden) { | |
3573 | $holder_style = "display:none;"; | |
3574 | $ellipsis = "..."; | |
3575 | } else { | |
3576 | $holder_style = ""; | |
3577 | $ellipsis = ""; | |
3578 | } | |
3579 | ||
3580 | $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread"; | |
3581 | ||
3582 | print "<li class=\"feedCat\" id=\"FCAT-$cat_id\"> | |
3583 | <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>"; | |
3584 | ||
3585 | if ($can_browse) { | |
3586 | print "<a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">"; | |
3587 | } else { | |
3588 | print "<span id=\"FCAP-$cat_id\">"; | |
3589 | } | |
3590 | ||
3591 | print " <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\" | |
3592 | class=\"$catctr_class\">($cat_unread)</span> $ellipsis"; | |
3593 | ||
3594 | if ($can_browse) { | |
3595 | print "</a>"; | |
3596 | } else { | |
3597 | print "</span>"; | |
3598 | } | |
3599 | ||
3600 | print "</li>"; | |
3601 | ||
3602 | print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>"; | |
3603 | } | |
3604 | ||
f407c086 AD |
3605 | function outputFeedList($link, $tags = false) { |
3606 | ||
3bd9a780 | 3607 | print "<ul class=\"feedList\" id=\"feedList\">"; |
f407c086 AD |
3608 | |
3609 | $owner_uid = $_SESSION["uid"]; | |
3610 | ||
cf4d339c | 3611 | /* virtual feeds */ |
f407c086 | 3612 | |
cf4d339c | 3613 | if (get_pref($link, 'ENABLE_FEED_CATS')) { |
3b086176 AD |
3614 | |
3615 | if ($_COOKIE["ttrss_vf_vclps"] == 1) { | |
bba7c4bf | 3616 | $cat_hidden = true; |
3b086176 | 3617 | } else { |
bba7c4bf | 3618 | $cat_hidden = false; |
3b086176 AD |
3619 | } |
3620 | ||
3621 | # print "<li class=\"feedCat\">".__('Special')."</li>"; | |
3622 | # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">"; | |
bba7c4bf AD |
3623 | # print "<li class=\"feedCat\">". |
3624 | # "<a id=\"FCATN--1\" href=\"javascript:toggleCollapseCat(-1)\">". | |
3625 | # __('Special')."</a> <span id='FCAP--1'>$ellipsis</span></li>"; | |
3626 | # | |
3627 | # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"> | |
3628 | # <ul class=\"feedCatList\" id='FCATLIST--1' style='$holder_style'>"; | |
3b086176 | 3629 | |
bba7c4bf AD |
3630 | # $cat_unread = getCategoryUnread($link, -1); |
3631 | # $tmp_category = __("Special"); | |
3632 | # $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread"; | |
3b086176 | 3633 | |
bba7c4bf | 3634 | printCategoryHeader($link, -1, $cat_hidden, false); |
cf4d339c | 3635 | } |
f407c086 | 3636 | |
cf4d339c | 3637 | $num_starred = getFeedUnread($link, -1); |
e4f4b46f | 3638 | $num_published = getFeedUnread($link, -2); |
2d24f032 AD |
3639 | $num_fresh = getFeedUnread($link, -3); |
3640 | ||
3641 | $class = "virt"; | |
3642 | ||
3643 | if ($num_fresh > 0) $class .= "Unread"; | |
3644 | ||
3645 | printFeedEntry(-3, $class, __("Fresh articles"), $num_fresh, | |
3646 | "images/fresh.png", $link); | |
f407c086 | 3647 | |
cf4d339c | 3648 | $class = "virt"; |
f407c086 | 3649 | |
cf4d339c | 3650 | if ($num_starred > 0) $class .= "Unread"; |
f407c086 | 3651 | |
abd8a516 AD |
3652 | $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false); |
3653 | ||
3654 | if ($is_ie) { | |
3655 | $mark_img_ext = "gif"; | |
3656 | } else { | |
3657 | $mark_img_ext = "png"; | |
3658 | } | |
3659 | ||
d1db26aa | 3660 | printFeedEntry(-1, $class, __("Starred articles"), $num_starred, |
abd8a516 | 3661 | "images/mark_set.$mark_img_ext", $link); |
f407c086 | 3662 | |
e4f4b46f AD |
3663 | $class = "virt"; |
3664 | ||
3665 | if ($num_published > 0) $class .= "Unread"; | |
3666 | ||
3667 | printFeedEntry(-2, $class, __("Published articles"), $num_published, | |
f5e0338d | 3668 | "images/pub_set.gif", $link); |
e4f4b46f | 3669 | |
cf4d339c | 3670 | if (get_pref($link, 'ENABLE_FEED_CATS')) { |
3bd9a780 | 3671 | print "</ul>"; |
cf4d339c | 3672 | } |
f407c086 | 3673 | |
cf4d339c | 3674 | if (!$tags) { |
f407c086 AD |
3675 | |
3676 | if (GLOBAL_ENABLE_LABELS && get_pref($link, 'ENABLE_LABELS')) { | |
3677 | ||
3678 | $result = db_query($link, "SELECT id,sql_exp,description FROM | |
3679 | ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description"); | |
3680 | ||
3681 | if (db_num_rows($result) > 0) { | |
3682 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
bd64489f AD |
3683 | |
3684 | if ($_COOKIE["ttrss_vf_lclps"] == 1) { | |
bba7c4bf | 3685 | $cat_hidden = true; |
bd64489f | 3686 | } else { |
bba7c4bf | 3687 | $cat_hidden = false; |
bd64489f AD |
3688 | } |
3689 | ||
bba7c4bf | 3690 | printCategoryHeader($link, -2, $cat_hidden, false); |
bd64489f | 3691 | |
bba7c4bf AD |
3692 | # print "<li class=\"feedCat\">". |
3693 | # "<a id=\"FCATN--2\" href=\"javascript:toggleCollapseCat(-2)\">". | |
3694 | # __('Labels')."</a> <span id='FCAP--2'>$ellipsis</span></li>"; | |
3695 | # | |
3696 | # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\" id='FCATLIST--2' style='$holder_style'>"; | |
f407c086 | 3697 | } else { |
3bd9a780 | 3698 | print "<li><hr></li>"; |
f407c086 AD |
3699 | } |
3700 | } | |
3701 | ||
3702 | while ($line = db_fetch_assoc($result)) { | |
3703 | ||
3704 | error_reporting (0); | |
3705 | ||
3706 | $label_id = -$line['id'] - 11; | |
3707 | $count = getFeedUnread($link, $label_id); | |
3708 | ||
3709 | $class = "label"; | |
3710 | ||
3711 | if ($count > 0) { | |
3712 | $class .= "Unread"; | |
3713 | } | |
3714 | ||
3715 | error_reporting (DEFAULT_ERROR_LEVEL); | |
3716 | ||
3717 | printFeedEntry($label_id, | |
47439031 | 3718 | $class, $line["description"], |
f407c086 AD |
3719 | $count, "images/label.png", $link); |
3720 | ||
3721 | } | |
3722 | ||
3723 | if (db_num_rows($result) > 0) { | |
3724 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3725 | print "</ul>"; | |
3726 | } | |
3727 | } | |
3728 | ||
3729 | } | |
3730 | ||
3731 | if (!get_pref($link, 'ENABLE_FEED_CATS')) { | |
3bd9a780 | 3732 | print "<li><hr></li>"; |
f407c086 AD |
3733 | } |
3734 | ||
3735 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3736 | if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) { | |
3737 | $order_by_qpart = "category,unread DESC,title"; | |
3738 | } else { | |
3739 | $order_by_qpart = "category,title"; | |
3740 | } | |
3741 | } else { | |
3742 | if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) { | |
3743 | $order_by_qpart = "unread DESC,title"; | |
3744 | } else { | |
3745 | $order_by_qpart = "title"; | |
3746 | } | |
3747 | } | |
3748 | ||
14073c0a AD |
3749 | $age_qpart = getMaxAgeSubquery(); |
3750 | ||
f407c086 AD |
3751 | $result = db_query($link, "SELECT ttrss_feeds.*, |
3752 | SUBSTRING(last_updated,1,19) AS last_updated_noms, | |
3753 | (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries | |
3754 | WHERE feed_id = ttrss_feeds.id AND unread = true | |
14073c0a | 3755 | AND $age_qpart |
f407c086 AD |
3756 | AND ttrss_user_entries.ref_id = ttrss_entries.id |
3757 | AND owner_uid = '$owner_uid') as unread, | |
3758 | cat_id,last_error, | |
3759 | ttrss_feed_categories.title AS category, | |
3760 | ttrss_feed_categories.collapsed | |
3761 | FROM ttrss_feeds LEFT JOIN ttrss_feed_categories | |
3762 | ON (ttrss_feed_categories.id = cat_id) | |
3763 | WHERE | |
3764 | ttrss_feeds.hidden = false AND | |
3765 | ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL | |
3766 | ORDER BY $order_by_qpart"); | |
3767 | ||
3768 | $actid = $_GET["actid"]; | |
3769 | ||
3770 | /* real feeds */ | |
3771 | ||
3772 | $lnum = 0; | |
3773 | ||
3774 | $total_unread = 0; | |
3775 | ||
3776 | $category = ""; | |
3777 | ||
3778 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); | |
3779 | ||
3780 | while ($line = db_fetch_assoc($result)) { | |
3781 | ||
47439031 | 3782 | $feed = trim($line["title"]); |
0f39ae20 AD |
3783 | |
3784 | if (!$feed) $feed = "[Untitled]"; | |
3785 | ||
f407c086 AD |
3786 | $feed_id = $line["id"]; |
3787 | ||
3788 | $subop = $_GET["subop"]; | |
3789 | ||
3790 | $unread = $line["unread"]; | |
3791 | ||
3792 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
3793 | $last_updated = smart_date_time(strtotime($line["last_updated_noms"])); | |
3794 | } else { | |
3795 | $last_updated = date($short_date, strtotime($line["last_updated_noms"])); | |
3796 | } | |
3797 | ||
3798 | $rtl_content = sql_bool_to_bool($line["rtl_content"]); | |
3799 | ||
3800 | if ($rtl_content) { | |
3801 | $rtl_tag = "dir=\"RTL\""; | |
3802 | } else { | |
3803 | $rtl_tag = ""; | |
3804 | } | |
3805 | ||
3806 | $tmp_result = db_query($link, | |
3807 | "SELECT id,COUNT(unread) AS unread | |
3808 | FROM ttrss_feeds LEFT JOIN ttrss_user_entries | |
3809 | ON (ttrss_feeds.id = ttrss_user_entries.feed_id) | |
3810 | WHERE parent_feed = '$feed_id' AND unread = true | |
3811 | GROUP BY ttrss_feeds.id"); | |
3812 | ||
3813 | if (db_num_rows($tmp_result) > 0) { | |
3814 | while ($l = db_fetch_assoc($tmp_result)) { | |
3815 | $unread += $l["unread"]; | |
3816 | } | |
3817 | } | |
3818 | ||
3819 | $cat_id = $line["cat_id"]; | |
3820 | ||
3821 | $tmp_category = $line["category"]; | |
3822 | ||
3823 | if (!$tmp_category) { | |
d1db26aa | 3824 | $tmp_category = __("Uncategorized"); |
f407c086 AD |
3825 | } |
3826 | ||
3827 | // $class = ($lnum % 2) ? "even" : "odd"; | |
3828 | ||
3829 | if ($line["last_error"]) { | |
3830 | $class = "error"; | |
3831 | } else { | |
3832 | $class = "feed"; | |
3833 | } | |
3834 | ||
3835 | if ($unread > 0) $class .= "Unread"; | |
3836 | ||
3837 | if ($actid == $feed_id) { | |
3838 | $class .= "Selected"; | |
3839 | } | |
3840 | ||
3841 | $total_unread += $unread; | |
3842 | ||
3843 | if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) { | |
3844 | ||
3845 | if ($category) { | |
3846 | print "</ul></li>"; | |
3847 | } | |
3848 | ||
3849 | $category = $tmp_category; | |
3850 | ||
3851 | $collapsed = $line["collapsed"]; | |
3852 | ||
3853 | // workaround for NULL category | |
d1db26aa | 3854 | if ($category == __("Uncategorized")) { |
f407c086 AD |
3855 | if ($_COOKIE["ttrss_vf_uclps"] == 1) { |
3856 | $collapsed = "t"; | |
3857 | } | |
3858 | } | |
3859 | ||
3860 | if ($collapsed == "t" || $collapsed == "1") { | |
225ec0d4 AD |
3861 | $holder_class = "feedCatHolder"; |
3862 | $holder_style = "display:none;"; | |
f407c086 AD |
3863 | $ellipsis = "..."; |
3864 | } else { | |
be5b75da | 3865 | $holder_class = "feedCatHolder"; |
225ec0d4 | 3866 | $holder_style = ""; |
f407c086 AD |
3867 | $ellipsis = ""; |
3868 | } | |
3869 | ||
3870 | $cat_id = sprintf("%d", $cat_id); | |
3871 | ||
3872 | $cat_unread = getCategoryUnread($link, $cat_id); | |
67dabe1a AD |
3873 | |
3874 | $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread"; | |
3875 | ||
3bd9a780 | 3876 | print "<li class=\"feedCat\" id=\"FCAT-$cat_id\"> |
439bbe63 | 3877 | <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a> |
7210613a | 3878 | <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\"> |
67dabe1a AD |
3879 | <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\" |
3880 | class=\"$catctr_class\">($cat_unread)</span> $ellipsis | |
3bd9a780 | 3881 | </a></li>"; |
f407c086 | 3882 | |
225ec0d4 | 3883 | print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>"; |
f407c086 AD |
3884 | } |
3885 | ||
3886 | printFeedEntry($feed_id, $class, $feed, $unread, | |
99e37e09 | 3887 | ICONS_DIR."/$feed_id.ico", $link, $rtl_content, |
f407c086 AD |
3888 | $last_updated, $line["last_error"]); |
3889 | ||
3890 | ++$lnum; | |
3891 | } | |
3892 | ||
3893 | if (db_num_rows($result) == 0) { | |
3bd9a780 | 3894 | print "<li>".__('No feeds to display.')."</li>"; |
f407c086 AD |
3895 | } |
3896 | ||
3897 | } else { | |
3898 | ||
3899 | // tags | |
3900 | ||
3901 | /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count | |
3902 | FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE | |
3903 | post_int_id = ttrss_user_entries.int_id AND | |
3904 | unread = true AND ref_id = ttrss_entries.id | |
3905 | AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name | |
3906 | UNION | |
3907 | select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid' | |
3908 | ORDER BY tag_name"); */ | |
3909 | ||
3910 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
d1db26aa | 3911 | print "<li class=\"feedCat\">".__('Tags')."</li>"; |
f407c086 AD |
3912 | print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">"; |
3913 | } | |
3914 | ||
14073c0a AD |
3915 | $age_qpart = getMaxAgeSubquery(); |
3916 | ||
f407c086 | 3917 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) |
14073c0a AD |
3918 | FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id |
3919 | AND ref_id = id AND $age_qpart | |
f407c086 | 3920 | AND unread = true)) AS count FROM ttrss_tags |
ef1ac7c7 AD |
3921 | WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name |
3922 | ORDER BY count DESC LIMIT 50"); | |
f407c086 AD |
3923 | |
3924 | $tags = array(); | |
3925 | ||
3926 | while ($line = db_fetch_assoc($result)) { | |
3927 | $tags[$line["tag_name"]] += $line["count"]; | |
3928 | } | |
3929 | ||
3930 | foreach (array_keys($tags) as $tag) { | |
3931 | ||
3932 | $unread = $tags[$tag]; | |
3933 | ||
3934 | $class = "tag"; | |
3935 | ||
3936 | if ($unread > 0) { | |
3937 | $class .= "Unread"; | |
3938 | } | |
3939 | ||
3940 | printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link); | |
3941 | ||
3942 | } | |
3943 | ||
3944 | if (db_num_rows($result) == 0) { | |
3945 | print "<li>No tags to display.</li>"; | |
3946 | } | |
3947 | ||
3948 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3bd9a780 | 3949 | print "</ul>"; |
f407c086 AD |
3950 | } |
3951 | ||
3952 | } | |
3953 | ||
3954 | print "</ul>"; | |
3955 | ||
3956 | } | |
3957 | ||
bc976a8c | 3958 | function get_article_tags($link, $id, $owner_uid = 0) { |
0b126ac2 AD |
3959 | |
3960 | $a_id = db_escape_string($id); | |
3961 | ||
bc976a8c AD |
3962 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; |
3963 | ||
0c3d1c68 AD |
3964 | $tmp_result = db_query($link, "SELECT DISTINCT tag_name, |
3965 | owner_uid as owner FROM | |
0b126ac2 | 3966 | ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE |
bc976a8c | 3967 | ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name"); |
0b126ac2 AD |
3968 | |
3969 | $tags = array(); | |
3970 | ||
3971 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
3972 | array_push($tags, $tmp_line["tag_name"]); | |
3973 | } | |
3974 | ||
3975 | return $tags; | |
3976 | } | |
3977 | ||
d62a3b63 AD |
3978 | function trim_value(&$value) { |
3979 | $value = trim($value); | |
3980 | } | |
3981 | ||
3982 | function trim_array($array) { | |
3983 | $tmp = $array; | |
3984 | array_walk($tmp, 'trim_value'); | |
3985 | return $tmp; | |
3986 | } | |
3987 | ||
be832a1a | 3988 | function tag_is_valid($tag) { |
ef063748 AD |
3989 | if ($tag == '') return false; |
3990 | if (preg_match("/^[0-9]*$/", $tag)) return false; | |
3991 | ||
3992 | $tag = iconv("utf-8", "utf-8", $tag); | |
3993 | if (!$tag) return false; | |
3994 | ||
3995 | return true; | |
be832a1a AD |
3996 | } |
3997 | ||
793185a9 AD |
3998 | function render_login_form($link, $mobile = false) { |
3999 | if (!$mobile) { | |
4000 | require_once "login_form.php"; | |
4001 | } else { | |
4002 | require_once "mobile/login_form.php"; | |
4003 | } | |
01a87dff AD |
4004 | } |
4005 | ||
dc56b3b7 AD |
4006 | // from http://developer.apple.com/internet/safari/faq.html |
4007 | function no_cache_incantation() { | |
4008 | header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :) | |
4009 | header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified | |
4010 | header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1 | |
4011 | header("Cache-Control: post-check=0, pre-check=0", false); | |
4012 | header("Pragma: no-cache"); // HTTP/1.0 | |
4013 | } | |
4014 | ||
42395d28 AD |
4015 | function format_warning($msg, $id = "") { |
4016 | return "<div class=\"warning\" id=\"$id\"> | |
e780d1d2 | 4017 | <img src=\"images/sign_excl.gif\">$msg</div>"; |
0d32b41e AD |
4018 | } |
4019 | ||
4020 | function format_notice($msg) { | |
4021 | return "<div class=\"notice\"> | |
e780d1d2 | 4022 | <img src=\"images/sign_info.gif\">$msg</div>"; |
0d32b41e AD |
4023 | } |
4024 | ||
68d2f95e AD |
4025 | function format_error($msg) { |
4026 | return "<div class=\"error\"> | |
e780d1d2 | 4027 | <img src=\"images/sign_excl.gif\">$msg</div>"; |
68d2f95e AD |
4028 | } |
4029 | ||
4dccf1ed AD |
4030 | function print_notice($msg) { |
4031 | return print format_notice($msg); | |
4032 | } | |
4033 | ||
4034 | function print_warning($msg) { | |
4035 | return print format_warning($msg); | |
4036 | } | |
4037 | ||
68d2f95e AD |
4038 | function print_error($msg) { |
4039 | return print format_error($msg); | |
4040 | } | |
4041 | ||
4042 | ||
4dccf1ed AD |
4043 | function T_sprintf() { |
4044 | $args = func_get_args(); | |
4045 | return vsprintf(__(array_shift($args)), $args); | |
4046 | } | |
4047 | ||
3de0261a AD |
4048 | function outputArticleXML($link, $id, $feed_id, $mark_as_read = true) { |
4049 | ||
10eb9da8 AD |
4050 | /* we can figure out feed_id from article id anyway, why do we |
4051 | * pass feed_id here? */ | |
4052 | ||
4053 | $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries | |
4054 | WHERE ref_id = '$id'"); | |
4055 | ||
4056 | $feed_id = db_fetch_result($result, 0, "feed_id"); | |
4057 | ||
3de0261a AD |
4058 | print "<article id='$id'><![CDATA["; |
4059 | ||
4060 | $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds | |
4061 | WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]); | |
4062 | ||
4063 | if (db_num_rows($result) == 1) { | |
4064 | $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content")); | |
4065 | } else { | |
4066 | $rtl_content = false; | |
4067 | } | |
4068 | ||
4069 | if ($rtl_content) { | |
4070 | $rtl_tag = "dir=\"RTL\""; | |
4071 | $rtl_class = "RTL"; | |
4072 | } else { | |
4073 | $rtl_tag = ""; | |
4074 | $rtl_class = ""; | |
4075 | } | |
4076 | ||
4077 | if ($mark_as_read) { | |
4078 | $result = db_query($link, "UPDATE ttrss_user_entries | |
4079 | SET unread = false,last_read = NOW() | |
4080 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
4081 | } | |
4082 | ||
4083 | $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id, | |
4084 | SUBSTRING(updated,1,16) as updated, | |
4085 | (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url, | |
4086 | num_comments, | |
4087 | author | |
4088 | FROM ttrss_entries,ttrss_user_entries | |
4089 | WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]); | |
4090 | ||
4091 | if ($result) { | |
4092 | ||
4093 | $link_target = ""; | |
4094 | ||
4095 | if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) { | |
4096 | $link_target = "target=\"_new\""; | |
4097 | } | |
4098 | ||
4099 | $line = db_fetch_assoc($result); | |
4100 | ||
4101 | if ($line["icon_url"]) { | |
4102 | $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">"; | |
4103 | } else { | |
4104 | $feed_icon = " "; | |
4105 | } | |
4106 | ||
4107 | /* if ($line["comments"] && $line["link"] != $line["comments"]) { | |
4108 | $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)"; | |
4109 | } else { | |
4110 | $entry_comments = ""; | |
4111 | } */ | |
4112 | ||
4113 | $num_comments = $line["num_comments"]; | |
4114 | $entry_comments = ""; | |
4115 | ||
4116 | if ($num_comments > 0) { | |
4117 | if ($line["comments"]) { | |
4118 | $comments_url = $line["comments"]; | |
4119 | } else { | |
4120 | $comments_url = $line["link"]; | |
4121 | } | |
4122 | $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>"; | |
4123 | } else { | |
4124 | if ($line["comments"] && $line["link"] != $line["comments"]) { | |
4125 | $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>"; | |
4126 | } | |
4127 | } | |
4128 | ||
4129 | print "<div class=\"postReply\">"; | |
4130 | ||
4131 | print "<div class=\"postHeader\">"; | |
4132 | ||
4133 | $entry_author = $line["author"]; | |
4134 | ||
4135 | if ($entry_author) { | |
4136 | $entry_author = __(" - by ") . $entry_author; | |
4137 | } | |
4138 | ||
4139 | $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'), | |
4140 | strtotime($line["updated"])); | |
4141 | ||
4142 | print "<div class=\"postDate$rtl_class\">$parsed_updated</div>"; | |
4143 | ||
4144 | if ($line["link"]) { | |
4145 | print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" . | |
06202d88 | 4146 | $line["title"] . "</a><span class='author'>$entry_author</span></div>"; |
3de0261a AD |
4147 | } else { |
4148 | print "<div clear='both'>" . $line["title"] . "$entry_author</div>"; | |
4149 | } | |
4150 | ||
9cfd409d | 4151 | /* $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM |
3de0261a | 4152 | ttrss_tags WHERE post_int_id = " . $line["int_id"] . " |
9cfd409d AD |
4153 | ORDER BY tag_name"); */ |
4154 | ||
4155 | $tags = get_article_tags($link, $id); | |
3de0261a AD |
4156 | |
4157 | $tags_str = ""; | |
4158 | $f_tags_str = ""; | |
4159 | ||
4160 | $num_tags = 0; | |
4161 | ||
9cfd409d | 4162 | foreach ($tags as $tag) { |
3de0261a | 4163 | $num_tags++; |
14b6c54b AD |
4164 | $tag_escaped = str_replace("'", "\\'", $tag); |
4165 | ||
4166 | $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, "; | |
3de0261a AD |
4167 | |
4168 | if ($num_tags == 6) { | |
e7544143 AD |
4169 | $tags_str .= "..."; |
4170 | ||
3de0261a AD |
4171 | } else if ($num_tags < 6) { |
4172 | $tags_str .= $tag_str; | |
4173 | } | |
4174 | $f_tags_str .= $tag_str; | |
4175 | } | |
4176 | ||
4177 | $tags_str = preg_replace("/, $/", "", $tags_str); | |
4178 | $f_tags_str = preg_replace("/, $/", "", $f_tags_str); | |
4179 | ||
e7544143 AD |
4180 | $all_tags_div = "<span class='cdmAllTagsCtr'>...<div class='cdmAllTags'>All Tags: $f_tags_str</div></span>"; |
4181 | $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str); | |
4182 | ||
3de0261a AD |
4183 | if (!$entry_comments) $entry_comments = " "; # placeholder |
4184 | ||
4185 | if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>'; | |
4186 | ||
5f014cf1 AD |
4187 | print "<div style='float : right'> |
4188 | <img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'> | |
4189 | $tags_str | |
3de0261a AD |
4190 | <a title=\"Edit tags for this article\" |
4191 | href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div> | |
4192 | <div clear='both'>$entry_comments</div>"; | |
4193 | ||
4194 | print "</div>"; | |
4195 | ||
4196 | print "<div class=\"postIcon\">" . $feed_icon . "</div>"; | |
4197 | print "<div class=\"postContent\">"; | |
4198 | ||
e7544143 | 4199 | #print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>"; |
3de0261a | 4200 | |
c41890b0 AD |
4201 | $line["content"] = sanitize_rss($link, $line["content"]); |
4202 | ||
3de0261a AD |
4203 | if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) { |
4204 | $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]); | |
4205 | } | |
4206 | ||
3de0261a AD |
4207 | print $line["content"] . "</div>"; |
4208 | ||
4209 | print "</div>"; | |
4210 | ||
4211 | } | |
4212 | ||
4213 | print "]]></article>"; | |
4214 | ||
4215 | } | |
4216 | ||
4217 | function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view, | |
4218 | $next_unread_feed, $offset) { | |
4219 | ||
46921916 AD |
4220 | $timing_info = getmicrotime(); |
4221 | ||
961f4c73 AD |
4222 | $topmost_article_ids = array(); |
4223 | ||
ac541432 AD |
4224 | if (!$offset) { |
4225 | $offset = 0; | |
4226 | } | |
3de0261a AD |
4227 | |
4228 | if ($subop == "undefined") $subop = ""; | |
4229 | ||
4230 | if ($subop == "CatchupSelected") { | |
4231 | $ids = split(",", db_escape_string($_GET["ids"])); | |
4232 | $cmode = sprintf("%d", $_GET["cmode"]); | |
4233 | ||
4234 | catchupArticlesById($link, $ids, $cmode); | |
4235 | } | |
4236 | ||
4237 | if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) { | |
4238 | update_generic_feed($link, $feed, $cat_view); | |
4239 | } | |
4240 | ||
4241 | if ($subop == "MarkAllRead") { | |
4242 | catchup_feed($link, $feed, $cat_view); | |
4243 | ||
4244 | if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) { | |
4245 | if ($next_unread_feed) { | |
4246 | $feed = $next_unread_feed; | |
4247 | } | |
4248 | } | |
4249 | } | |
4250 | ||
4251 | if ($feed_id > 0) { | |
4252 | $result = db_query($link, | |
4253 | "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1"); | |
4254 | ||
4255 | if (db_num_rows($result) == 0) { | |
4256 | print "<div align='center'>".__('Feed not found.')."</div>"; | |
4257 | return; | |
4258 | } | |
4259 | } | |
4260 | ||
4261 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
10eb9da8 | 4262 | |
3de0261a AD |
4263 | $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds |
4264 | WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
4265 | ||
4266 | if (db_num_rows($result) == 1) { | |
4267 | $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content")); | |
4268 | } else { | |
4269 | $rtl_content = false; | |
4270 | } | |
4271 | ||
4272 | if ($rtl_content) { | |
4273 | $rtl_tag = "dir=\"RTL\""; | |
4274 | } else { | |
4275 | $rtl_tag = ""; | |
4276 | } | |
4277 | } else { | |
4278 | $rtl_tag = ""; | |
4279 | $rtl_content = false; | |
4280 | } | |
4281 | ||
4282 | $script_dt_add = get_script_dt_add(); | |
4283 | ||
4284 | /// START ///////////////////////////////////////////////////////////////////////////////// | |
4285 | ||
4286 | $search = db_escape_string($_GET["query"]); | |
4287 | $search_mode = db_escape_string($_GET["search_mode"]); | |
4288 | $match_on = db_escape_string($_GET["match_on"]); | |
4289 | ||
4290 | if (!$match_on) { | |
4291 | $match_on = "both"; | |
4292 | } | |
4293 | ||
4294 | $real_offset = $offset * $limit; | |
4295 | ||
46921916 AD |
4296 | if ($_GET["debug"]) $timing_info = print_checkpoint("H0", $timing_info); |
4297 | ||
3de0261a AD |
4298 | $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, |
4299 | $search, $search_mode, $match_on, false, $real_offset); | |
4300 | ||
46921916 AD |
4301 | if ($_GET["debug"]) $timing_info = print_checkpoint("H1", $timing_info); |
4302 | ||
3de0261a AD |
4303 | $result = $qfh_ret[0]; |
4304 | $feed_title = $qfh_ret[1]; | |
4305 | $feed_site_url = $qfh_ret[2]; | |
4306 | $last_error = $qfh_ret[3]; | |
f56e3080 AD |
4307 | |
4308 | if ($feed == -2) { | |
4309 | $feed_site_url = article_publish_url($link); | |
4310 | } | |
4311 | ||
3de0261a AD |
4312 | /// STOP ////////////////////////////////////////////////////////////////////////////////// |
4313 | ||
ac541432 AD |
4314 | if (!$offset) { |
4315 | print "<div id=\"headlinesContainer\" $rtl_tag>"; | |
3de0261a | 4316 | |
ac541432 AD |
4317 | if (!$result) { |
4318 | print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>"; | |
4319 | return; | |
4320 | } | |
3de0261a | 4321 | |
ac541432 AD |
4322 | print_headline_subtoolbar($link, $feed_site_url, $feed_title, false, |
4323 | $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode, | |
4324 | $offset, $limit); | |
3de0261a | 4325 | |
ac541432 AD |
4326 | print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">"; |
4327 | } | |
3de0261a | 4328 | |
29dfb258 AD |
4329 | $headlines_count = db_num_rows($result); |
4330 | ||
3de0261a AD |
4331 | if (db_num_rows($result) > 0) { |
4332 | ||
4333 | # print "\{$offset}"; | |
4334 | ||
ac541432 | 4335 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) { |
3de0261a AD |
4336 | print "<table class=\"headlinesList\" id=\"headlinesList\" |
4337 | cellspacing=\"0\">"; | |
4338 | } | |
4339 | ||
4ab4d364 AD |
4340 | $lnum = $limit*$offset; |
4341 | ||
3de0261a AD |
4342 | error_reporting (DEFAULT_ERROR_LEVEL); |
4343 | ||
4344 | $num_unread = 0; | |
4345 | ||
4346 | while ($line = db_fetch_assoc($result)) { | |
4347 | ||
4348 | $class = ($lnum % 2) ? "even" : "odd"; | |
4349 | ||
4350 | $id = $line["id"]; | |
4351 | $feed_id = $line["feed_id"]; | |
961f4c73 AD |
4352 | |
4353 | if (count($topmost_article_ids) < 5) { | |
4354 | array_push($topmost_article_ids, $id); | |
4355 | } | |
4356 | ||
3de0261a AD |
4357 | if ($line["last_read"] == "" && |
4358 | ($line["unread"] != "t" && $line["unread"] != "1")) { | |
4359 | ||
4360 | $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\" | |
4361 | alt=\"Updated\">"; | |
4362 | } else { | |
4363 | $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\" | |
4364 | alt=\"Updated\">"; | |
4365 | } | |
4366 | ||
4367 | if ($line["unread"] == "t" || $line["unread"] == "1") { | |
4368 | $class .= "Unread"; | |
4369 | ++$num_unread; | |
4370 | $is_unread = true; | |
4371 | } else { | |
4372 | $is_unread = false; | |
4373 | } | |
abd8a516 AD |
4374 | |
4375 | $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false); | |
4376 | ||
4377 | if ($is_ie) { | |
4378 | $mark_img_ext = "gif"; | |
4379 | } else { | |
4380 | $mark_img_ext = "png"; | |
4381 | } | |
4382 | ||
3de0261a | 4383 | if ($line["marked"] == "t" || $line["marked"] == "1") { |
abd8a516 | 4384 | $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.$mark_img_ext\" |
3de0261a | 4385 | class=\"markedPic\" |
f5e0338d | 4386 | alt=\"Unstar article\" onclick='javascript:tMark($id)'>"; |
3de0261a | 4387 | } else { |
abd8a516 | 4388 | $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.$mark_img_ext\" |
3de0261a | 4389 | class=\"markedPic\" |
f5e0338d | 4390 | alt=\"Star article\" onclick='javascript:tMark($id)'>"; |
3de0261a AD |
4391 | } |
4392 | ||
e4f4b46f | 4393 | if ($line["published"] == "t" || $line["published"] == "1") { |
f5e0338d | 4394 | $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.gif\" |
e4f4b46f | 4395 | class=\"markedPic\" |
f5e0338d | 4396 | alt=\"Unpublish article\" onclick='javascript:tPub($id)'>"; |
e4f4b46f | 4397 | } else { |
f5e0338d | 4398 | $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.gif\" |
e4f4b46f | 4399 | class=\"markedPic\" |
f5e0338d | 4400 | alt=\"Publish article\" onclick='javascript:tPub($id)'>"; |
e4f4b46f AD |
4401 | } |
4402 | ||
3de0261a AD |
4403 | # $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" . |
4404 | # $line["title"] . "</a>"; | |
4405 | ||
4406 | $content_link = "<a href=\"javascript:view($id,$feed_id);\">" . | |
4407 | $line["title"] . "</a>"; | |
4408 | ||
4409 | # $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" . | |
4410 | # $line["title"] . "</a>"; | |
4411 | ||
4412 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
46921916 | 4413 | $updated_fmt = smart_date_time(strtotime($line["updated_noms"])); |
3de0261a AD |
4414 | } else { |
4415 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); | |
46921916 | 4416 | $updated_fmt = date($short_date, strtotime($line["updated_noms"])); |
3de0261a AD |
4417 | } |
4418 | ||
4419 | if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) { | |
4420 | $content_preview = truncate_string(strip_tags($line["content_preview"]), | |
4421 | 100); | |
4422 | } | |
4423 | ||
4424 | $entry_author = $line["author"]; | |
4425 | ||
4426 | if ($entry_author) { | |
4427 | $entry_author = " - by $entry_author"; | |
4428 | } | |
4429 | ||
4430 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) { | |
4431 | ||
4432 | print "<tr class='$class' id='RROW-$id'>"; | |
4433 | ||
67343d9f | 4434 | print "<td class='hlUpdPic'>$update_pic</td>"; |
3de0261a AD |
4435 | |
4436 | print "<td class='hlSelectRow'> | |
67343d9f AD |
4437 | <input type=\"checkbox\" onclick=\"tSR(this)\" |
4438 | id=\"RCHK-$id\"> | |
3de0261a AD |
4439 | </td>"; |
4440 | ||
4441 | print "<td class='hlMarkedPic'>$marked_pic</td>"; | |
e4f4b46f | 4442 | print "<td class='hlMarkedPic'>$published_pic</td>"; |
3de0261a | 4443 | |
df456bb0 AD |
4444 | # if ($line["feed_title"]) { |
4445 | # print "<td class='hlContent'>$content_link</td>"; | |
4446 | # print "<td class='hlFeed'> | |
4447 | # <a href=\"javascript:viewfeed($feed_id, '', false)\">". | |
4448 | # truncate_string($line["feed_title"],30)."</a> </td>"; | |
4449 | # } else { | |
4450 | ||
4451 | print "<td class='hlContent' valign='middle'>"; | |
4452 | ||
4453 | print "<a href=\"javascript:view($id,$feed_id);\">" . | |
4454 | $line["title"]; | |
4455 | ||
4456 | if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) { | |
4457 | if ($content_preview) { | |
4458 | print "<span class=\"contentPreview\"> - $content_preview</span>"; | |
3de0261a | 4459 | } |
3de0261a | 4460 | } |
df456bb0 AD |
4461 | |
4462 | print "</a>"; | |
4463 | ||
4464 | # <a href=\"javascript:viewfeed($feed_id, '', false)\">". | |
4465 | # $line["feed_title"]."</a> | |
4466 | ||
4467 | if ($line["feed_title"]) { | |
4468 | print "<span class=\"hlFeed\"> | |
4469 | (<a href=\"javascript:viewfeed($feed_id, '', false)\">". | |
4470 | $line["feed_title"]."</a>) | |
4471 | </span>"; | |
4472 | } | |
4473 | ||
4474 | ||
4475 | print "</td>"; | |
4476 | ||
4477 | # } | |
3de0261a AD |
4478 | |
4479 | print "<td class=\"hlUpdated\"><nobr>$updated_fmt </nobr></td>"; | |
4480 | ||
4481 | print "</tr>"; | |
4482 | ||
4483 | } else { | |
4484 | ||
4485 | if ($is_unread) { | |
4486 | $add_class = "Unread"; | |
4487 | } else { | |
4488 | $add_class = ""; | |
4489 | } | |
4490 | ||
e4914b62 AD |
4491 | print "<div class=\"cdmArticle$add_class\" |
4492 | id=\"RROW-$id\" onmouseover='cdmMouseIn(this)' | |
4493 | onmouseout='cdmMouseOut(this)'>"; | |
3de0261a AD |
4494 | |
4495 | print "<div class=\"cdmHeader\">"; | |
4496 | ||
4497 | print "<div class=\"articleUpdated\">$updated_fmt</div>"; | |
4498 | ||
4499 | print "<a class=\"title\" | |
4500 | onclick=\"javascript:toggleUnread($id, 0)\" | |
3fc2eed5 | 4501 | target=\"_new\" href=\"".$line["link"]."\">".$line["title"]."</a>"; |
3de0261a AD |
4502 | |
4503 | print $entry_author; | |
4504 | ||
4505 | if ($line["feed_title"]) { | |
4506 | print " (<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)"; | |
4507 | } | |
4508 | ||
4509 | print "</div>"; | |
4510 | ||
3fc2eed5 AD |
4511 | if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) { |
4512 | $line["content_preview"] = preg_replace("/href=/i", | |
4513 | "target=\"_new\" href=", $line["content_preview"]); | |
4514 | } | |
4515 | ||
3de0261a AD |
4516 | print "<div class=\"cdmContent\">" . $line["content_preview"] . "</div><br clear=\"all\">"; |
4517 | ||
e2ccbfab | 4518 | print "<div class=\"cdmFooter\"><span class='s0'>"; |
3de0261a | 4519 | |
e2ccbfab | 4520 | /* print "<div class=\"markedPic\">Star it: $marked_pic</div>"; */ |
3de0261a | 4521 | |
e2ccbfab AD |
4522 | print __("Select:"). |
4523 | " <input type=\"checkbox\" onclick=\"toggleSelectRowById(this, | |
3de0261a AD |
4524 | 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">"; |
4525 | ||
e2ccbfab | 4526 | print "</span><span class='s1'>$marked_pic</span> "; |
e4f4b46f | 4527 | print "<span class='s1'>$published_pic</span> "; |
e2ccbfab | 4528 | |
3de0261a AD |
4529 | $tags = get_article_tags($link, $id); |
4530 | ||
4531 | $tags_str = ""; | |
22d1f3db | 4532 | $full_tags_str = ""; |
d735ebd2 | 4533 | $num_tags = 0; |
3de0261a AD |
4534 | |
4535 | foreach ($tags as $tag) { | |
4536 | $num_tags++; | |
22d1f3db AD |
4537 | $full_tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, "; |
4538 | if ($num_tags < 5) { | |
4539 | $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, "; | |
4540 | } else if ($num_tags == 5) { | |
4541 | $tags_str .= "..."; | |
4542 | } | |
3de0261a AD |
4543 | } |
4544 | ||
4545 | $tags_str = preg_replace("/, $/", "", $tags_str); | |
22d1f3db AD |
4546 | $full_tags_str = preg_replace("/, $/", "", $full_tags_str); |
4547 | ||
4548 | $all_tags_div = "<span class='cdmAllTagsCtr'>...<div class='cdmAllTags'>All Tags: $full_tags_str</div></span>"; | |
4549 | ||
4550 | $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str); | |
4551 | ||
3de0261a AD |
4552 | |
4553 | if ($tags_str == "") $tags_str = "no tags"; | |
e2ccbfab AD |
4554 | |
4555 | // print "<img src='images/tag.png' class='markedPic'>"; | |
4556 | ||
5f014cf1 AD |
4557 | print "<span class='s1'> |
4558 | <img class='tagsPic' src='images/tag.png' alt='Tags' | |
4559 | title='Tags'> $tags_str <a title=\"Edit tags for this article\" | |
3de0261a AD |
4560 | href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>"; |
4561 | ||
e2ccbfab | 4562 | print "</span>"; |
3de0261a | 4563 | |
e2ccbfab AD |
4564 | print "<span class='s2'>Toggle: <a class=\"cdmToggleLink\" |
4565 | href=\"javascript:toggleUnread($id)\"> | |
4566 | Unread</a></span>"; | |
3de0261a | 4567 | |
e2ccbfab | 4568 | print "</div>"; |
3de0261a AD |
4569 | print "</div>"; |
4570 | ||
4571 | } | |
4572 | ||
4573 | ++$lnum; | |
4574 | } | |
4575 | ||
ac541432 | 4576 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) { |
3de0261a AD |
4577 | print "</table>"; |
4578 | } | |
4579 | ||
4580 | // print_headline_subtoolbar($link, | |
4581 | // "javascript:catchupPage()", "Mark page as read", true, $rtl_content); | |
4582 | ||
4583 | ||
4584 | } else { | |
ac541432 | 4585 | if (!$offset) print "<div class='whiteBox'>".__('No articles found.')."</div>"; |
3de0261a AD |
4586 | } |
4587 | ||
ac541432 AD |
4588 | if (!$offset) { |
4589 | print "</div>"; | |
4590 | print "</div>"; | |
4591 | } | |
3de0261a | 4592 | |
29dfb258 | 4593 | return array($topmost_article_ids, $headlines_count); |
3de0261a | 4594 | } |
0979b696 AD |
4595 | |
4596 | // from here: http://www.roscripts.com/Create_tag_cloud-71.html | |
4597 | ||
4598 | function printTagCloud($link) { | |
35a03bdd AD |
4599 | |
4600 | /* get first ref_id to count from */ | |
4601 | ||
dcac082b AD |
4602 | /* |
4603 | ||
35a03bdd AD |
4604 | $query = ""; |
4605 | ||
4606 | if (DB_TYPE == "pgsql") { | |
4607 | $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries | |
4608 | WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]." | |
4609 | AND date_entered > NOW() - INTERVAL '30 days'"; | |
4610 | } else { | |
4611 | $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries | |
4612 | WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]." | |
4613 | AND date_entered > DATE_SUB(NOW(), INTERVAL 30 DAY)"; | |
4614 | } | |
4615 | ||
4616 | $result = db_query($link, $query); | |
dcac082b | 4617 | $first_id = db_fetch_result($result, 0, "id"); */ |
35a03bdd | 4618 | |
dcac082b | 4619 | //AND post_int_id >= '$first_id' |
0979b696 AD |
4620 | $query = "SELECT tag_name, COUNT(post_int_id) AS count |
4621 | FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]." | |
b31af972 | 4622 | GROUP BY tag_name ORDER BY count DESC LIMIT 50"; |
0979b696 AD |
4623 | |
4624 | $result = db_query($link, $query); | |
4625 | ||
4626 | $tags = array(); | |
4627 | ||
4628 | while ($line = db_fetch_assoc($result)) { | |
4629 | $tags[$line["tag_name"]] = $line["count"]; | |
4630 | } | |
4631 | ||
4632 | ksort($tags); | |
4633 | ||
4634 | $max_size = 32; // max font size in pixels | |
4548a580 | 4635 | $min_size = 11; // min font size in pixels |
0979b696 AD |
4636 | |
4637 | // largest and smallest array values | |
4638 | $max_qty = max(array_values($tags)); | |
4639 | $min_qty = min(array_values($tags)); | |
4640 | ||
4641 | // find the range of values | |
4642 | $spread = $max_qty - $min_qty; | |
4643 | if ($spread == 0) { // we don't want to divide by zero | |
4644 | $spread = 1; | |
4645 | } | |
4646 | ||
4647 | // set the font-size increment | |
4648 | $step = ($max_size - $min_size) / ($spread); | |
4649 | ||
4650 | // loop through the tag array | |
4651 | foreach ($tags as $key => $value) { | |
4652 | // calculate font-size | |
4653 | // find the $value in excess of $min_qty | |
4654 | // multiply by the font-size increment ($size) | |
4655 | // and add the $min_size set above | |
4656 | $size = round($min_size + (($value - $min_qty) * $step)); | |
546ffab4 AD |
4657 | |
4658 | $key_escaped = str_replace("'", "\\'", $key); | |
4659 | ||
4660 | echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " . | |
0979b696 AD |
4661 | $size . "px\" title=\"$value articles tagged with " . |
4662 | $key . '">' . $key . '</a> '; | |
4663 | } | |
4664 | } | |
46921916 AD |
4665 | |
4666 | function print_checkpoint($n, $s) { | |
4667 | $ts = getmicrotime(); | |
4668 | echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s); | |
4669 | return $ts; | |
4670 | } | |
14b6c54b AD |
4671 | |
4672 | function sanitize_tag($tag) { | |
4673 | $tag = trim($tag); | |
4674 | ||
4675 | $tag = mb_strtolower($tag, 'utf-8'); | |
4676 | ||
0c3d1c68 AD |
4677 | $tag = preg_replace('/[\"\+\>\<]/', "", $tag); |
4678 | ||
4679 | // $tag = str_replace('"', "", $tag); | |
4680 | // $tag = str_replace("+", " ", $tag); | |
14b6c54b AD |
4681 | $tag = str_replace("technorati tag: ", "", $tag); |
4682 | ||
4683 | return $tag; | |
4684 | } | |
e4f4b46f AD |
4685 | |
4686 | function generate_publish_key() { | |
4687 | return sha1(uniqid(rand(), true)); | |
4688 | } | |
4689 | ||
f56e3080 AD |
4690 | function article_publish_url($link) { |
4691 | ||
17a756d1 | 4692 | $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH); |
f56e3080 AD |
4693 | |
4694 | $url_path .= "?op=publish&key=" . get_pref($link, "_PREFS_PUBLISH_KEY"); | |
4695 | ||
4696 | return $url_path; | |
4697 | } | |
4698 | ||
d1f0c584 AD |
4699 | function clear_feed_articles($link, $id) { |
4700 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
a8ae1b9a | 4701 | WHERE feed_id = '$id' AND marked = false AND owner_uid = " . $_SESSION["uid"]); |
d1f0c584 AD |
4702 | |
4703 | $result = db_query($link, "DELETE FROM ttrss_entries WHERE | |
4704 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
4705 | } | |
4706 | ||
40d13c28 | 4707 | ?> |