]>
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 | |
fc2b26a6 AD |
11 | if (DB_TYPE == "pgsql") { |
12 | define('SUBSTRING_FOR_DATE', 'SUBSTRING_FOR_DATE'); | |
13 | } else { | |
14 | define('SUBSTRING_FOR_DATE', 'SUBSTRING'); | |
15 | } | |
16 | ||
9632f884 AD |
17 | /** |
18 | * Return available translations names. | |
19 | * | |
20 | * @access public | |
21 | * @return array A array of available translations. | |
22 | */ | |
f8c612d4 | 23 | function get_translations() { |
6a214f92 | 24 | $tr = array( |
019bd5a9 | 25 | "auto" => "Detect automatically", |
6a214f92 AD |
26 | "en_US" => "English", |
27 | "fr_FR" => "Français", | |
e78fd196 | 28 | "hu_HU" => "Magyar (Hungarian)", |
bb5d3960 | 29 | "it_IT" => "Italiano", |
1d004f12 | 30 | "ja_JP" => "日本語 (Japanese)", |
592535d7 | 31 | "nb_NO" => "Norwegian bokmål", |
6a214f92 | 32 | "ru_RU" => "Русский", |
9a063469 | 33 | "pt_BR" => "Portuguese/Brazil", |
6a214f92 | 34 | "zh_CN" => "Simplified Chinese"); |
f8c612d4 AD |
35 | |
36 | return $tr; | |
37 | } | |
38 | ||
9632f884 | 39 | if (ENABLE_TRANSLATIONS == true) { // If translations are enabled. |
865220a4 | 40 | require_once "accept-to-gettext.php"; |
0ba3a127 | 41 | require_once "lib/gettext/gettext.inc"; |
aba609e0 | 42 | |
8d039718 AD |
43 | function startup_gettext() { |
44 | ||
45 | # Get locale from Accept-Language header | |
6a214f92 | 46 | $lang = al2gt(array_keys(get_translations()), "text/html"); |
89cb787e AD |
47 | |
48 | if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) { | |
49 | $lang = _TRANSLATION_OVERRIDE_DEFAULT; | |
50 | } | |
51 | ||
672f3f3c | 52 | if ($_COOKIE["ttrss_lang"] && $_COOKIE["ttrss_lang"] != "auto") { |
68659d98 AD |
53 | $lang = $_COOKIE["ttrss_lang"]; |
54 | } | |
55 | ||
7c33dbd4 AD |
56 | /* In login action of mobile version */ |
57 | if ($_POST["language"] && defined('MOBILE_VERSION')) { | |
58 | $lang = $_POST["language"]; | |
59 | $_COOKIE["ttrss_lang"] = $lang; | |
60 | } | |
61 | ||
8d039718 | 62 | if ($lang) { |
86e2e1b9 AD |
63 | if (defined('LC_MESSAGES')) { |
64 | _setlocale(LC_MESSAGES, $lang); | |
65 | } else if (defined('LC_ALL')) { | |
66 | _setlocale(LC_ALL, $lang); | |
67 | } else { | |
68 | die("can't setlocale(): please set ENABLE_TRANSLATIONS to false in config.php"); | |
69 | } | |
7c33dbd4 AD |
70 | |
71 | if (defined('MOBILE_VERSION')) { | |
72 | _bindtextdomain("messages", "../locale"); | |
73 | } else { | |
74 | _bindtextdomain("messages", "locale"); | |
75 | } | |
76 | ||
8d039718 AD |
77 | _textdomain("messages"); |
78 | _bind_textdomain_codeset("messages", "UTF-8"); | |
79 | } | |
aba609e0 | 80 | } |
aba609e0 | 81 | |
cc17c205 | 82 | startup_gettext(); |
865220a4 | 83 | |
9632f884 | 84 | } else { // If translations are enabled. |
865220a4 AD |
85 | function __($msg) { |
86 | return $msg; | |
87 | } | |
88 | function startup_gettext() { | |
89 | // no-op | |
90 | return true; | |
91 | } | |
9632f884 | 92 | } // If translations are enabled. |
cc17c205 | 93 | |
b619ff15 | 94 | require_once 'db-prefs.php'; |
5bc0bd27 | 95 | require_once 'compat.php'; |
af106b0e | 96 | require_once 'errors.php'; |
8911ac8b | 97 | require_once 'version.php'; |
40d13c28 | 98 | |
d134e3a3 | 99 | require_once 'lib/phpmailer/class.phpmailer.php'; |
a8931123 | 100 | |
49f9c923 | 101 | define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')'); |
a3ee2a38 | 102 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); |
ba0f7628 | 103 | define('MAGPIE_CACHE_AGE', 60*15); // 15 minutes |
a3ee2a38 | 104 | |
5ef03f15 | 105 | require_once "lib/simplepie/simplepie.inc"; |
816cdfb7 AD |
106 | require_once "lib/magpierss/rss_fetch.inc"; |
107 | require_once 'lib/magpierss/rss_utils.inc'; | |
49f9c923 | 108 | |
45004d43 AD |
109 | /** |
110 | * Print a timestamped debug message. | |
111 | * | |
112 | * @param string $msg The debug message. | |
113 | * @return void | |
114 | */ | |
6f9e33e4 AD |
115 | function _debug($msg) { |
116 | $ts = strftime("%H:%M:%S", time()); | |
2a6a9395 AD |
117 | if (function_exists('posix_getpid')) { |
118 | $ts = "$ts/" . posix_getpid(); | |
119 | } | |
6f9e33e4 | 120 | print "[$ts] $msg\n"; |
45004d43 | 121 | } // function _debug |
6f9e33e4 | 122 | |
9632f884 AD |
123 | /** |
124 | * Purge a feed old posts. | |
125 | * | |
126 | * @param mixed $link A database connection. | |
127 | * @param mixed $feed_id The id of the purged feed. | |
128 | * @param mixed $purge_interval Olderness of purged posts. | |
129 | * @param boolean $debug Set to True to enable the debug. False by default. | |
130 | * @access public | |
131 | * @return void | |
132 | */ | |
ad507f85 AD |
133 | function purge_feed($link, $feed_id, $purge_interval, $debug = false) { |
134 | ||
07d0efe9 | 135 | if (!$purge_interval) $purge_interval = feed_purge_interval($link, $feed_id); |
01299231 | 136 | |
ad507f85 | 137 | $rows = -1; |
4c193675 | 138 | |
07d0efe9 AD |
139 | $result = db_query($link, |
140 | "SELECT owner_uid FROM ttrss_feeds WHERE id = '$feed_id'"); | |
141 | ||
142 | $owner_uid = false; | |
143 | ||
144 | if (db_num_rows($result) == 1) { | |
145 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
146 | } | |
147 | ||
ab954dff AD |
148 | if ($purge_interval == -1 || !$purge_interval) { |
149 | if ($owner_uid) { | |
150 | ccache_update($link, $feed_id, $owner_uid); | |
151 | } | |
152 | return; | |
153 | } | |
154 | ||
07d0efe9 AD |
155 | if (!$owner_uid) return; |
156 | ||
3907ef71 AD |
157 | if (FORCE_ARTICLE_PURGE == 0) { |
158 | $purge_unread = get_pref($link, "PURGE_UNREAD_ARTICLES", | |
159 | $owner_uid, false); | |
160 | } else { | |
161 | $purge_unread = true; | |
162 | $purge_interval = FORCE_ARTICLE_PURGE; | |
163 | } | |
07d0efe9 AD |
164 | |
165 | if (!$purge_unread) $query_limit = " unread = false AND "; | |
166 | ||
fefa6ca3 | 167 | if (DB_TYPE == "pgsql") { |
44e241cb | 168 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 169 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 170 | (SELECT date_entered FROM ttrss_entries WHERE |
44e241cb AD |
171 | id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */ |
172 | ||
6e7f8d26 AD |
173 | $pg_version = get_pgsql_version($link); |
174 | ||
175 | if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) { | |
1e59ae35 AD |
176 | |
177 | $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
178 | ttrss_entries.id = ref_id AND | |
179 | marked = false AND | |
180 | feed_id = '$feed_id' AND | |
07d0efe9 | 181 | $query_limit |
1e59ae35 AD |
182 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); |
183 | ||
184 | } else { | |
185 | ||
186 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
187 | USING ttrss_entries | |
188 | WHERE ttrss_entries.id = ref_id AND | |
189 | marked = false AND | |
190 | feed_id = '$feed_id' AND | |
07d0efe9 | 191 | $query_limit |
fc774155 | 192 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); |
1e59ae35 | 193 | } |
ad507f85 AD |
194 | |
195 | $rows = pg_affected_rows($result); | |
196 | ||
fefa6ca3 | 197 | } else { |
1e59ae35 | 198 | |
30f1746f | 199 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 200 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 201 | (SELECT date_entered FROM ttrss_entries WHERE |
30f1746f AD |
202 | id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */ |
203 | ||
204 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
205 | USING ttrss_user_entries, ttrss_entries | |
206 | WHERE ttrss_entries.id = ref_id AND | |
207 | marked = false AND | |
208 | feed_id = '$feed_id' AND | |
07d0efe9 | 209 | $query_limit |
30f1746f AD |
210 | ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); |
211 | ||
ad507f85 AD |
212 | $rows = mysql_affected_rows($link); |
213 | ||
214 | } | |
215 | ||
ced46404 AD |
216 | ccache_update($link, $feed_id, $owner_uid); |
217 | ||
ad507f85 | 218 | if ($debug) { |
6f9e33e4 | 219 | _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles"); |
fefa6ca3 | 220 | } |
9632f884 | 221 | } // function purge_feed |
fefa6ca3 | 222 | |
9632f884 AD |
223 | /** |
224 | * Purge old posts from old feeds. | |
225 | * | |
226 | * @param mixed $link A database connection | |
227 | * @param boolean $do_output Set to true to enable printed output, false by default. | |
228 | * @param integer $limit The maximal number of removed posts. | |
229 | * @access public | |
230 | * @return void | |
231 | */ | |
44e241cb AD |
232 | function global_purge_old_posts($link, $do_output = false, $limit = false) { |
233 | ||
894ebcf5 | 234 | $random_qpart = sql_random_function(); |
fefa6ca3 | 235 | |
44e241cb AD |
236 | if ($limit) { |
237 | $limit_qpart = "LIMIT $limit"; | |
238 | } else { | |
239 | $limit_qpart = ""; | |
240 | } | |
241 | ||
fefa6ca3 | 242 | $result = db_query($link, |
44e241cb AD |
243 | "SELECT id,purge_interval,owner_uid FROM ttrss_feeds |
244 | ORDER BY $random_qpart $limit_qpart"); | |
fefa6ca3 AD |
245 | |
246 | while ($line = db_fetch_assoc($result)) { | |
247 | ||
248 | $feed_id = $line["id"]; | |
249 | $purge_interval = $line["purge_interval"]; | |
250 | $owner_uid = $line["owner_uid"]; | |
251 | ||
252 | if ($purge_interval == 0) { | |
253 | ||
254 | $tmp_result = db_query($link, | |
255 | "SELECT value FROM ttrss_user_prefs WHERE | |
256 | pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'"); | |
257 | ||
258 | if (db_num_rows($tmp_result) != 0) { | |
259 | $purge_interval = db_fetch_result($tmp_result, 0, "value"); | |
260 | } | |
261 | } | |
262 | ||
263 | if ($do_output) { | |
ad507f85 | 264 | // print "Feed $feed_id: purge interval = $purge_interval\n"; |
fefa6ca3 AD |
265 | } |
266 | ||
3907ef71 | 267 | if ($purge_interval > 0 || FORCE_ARTICLE_PURGE) { |
ad507f85 | 268 | purge_feed($link, $feed_id, $purge_interval, $do_output); |
fefa6ca3 AD |
269 | } |
270 | } | |
271 | ||
71604ca4 | 272 | // purge orphaned posts in main content table |
dab52d7b | 273 | $result = db_query($link, "DELETE FROM ttrss_entries WHERE |
71604ca4 AD |
274 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); |
275 | ||
dab52d7b AD |
276 | if ($do_output) { |
277 | $rows = db_affected_rows($link, $result); | |
278 | _debug("Purged $rows orphaned posts."); | |
279 | } | |
280 | ||
9632f884 | 281 | } // function global_purge_old_posts |
fefa6ca3 | 282 | |
07d0efe9 AD |
283 | function feed_purge_interval($link, $feed_id) { |
284 | ||
285 | $result = db_query($link, "SELECT purge_interval, owner_uid FROM ttrss_feeds | |
286 | WHERE id = '$feed_id'"); | |
287 | ||
288 | if (db_num_rows($result) == 1) { | |
289 | $purge_interval = db_fetch_result($result, 0, "purge_interval"); | |
290 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
291 | ||
292 | if ($purge_interval == 0) $purge_interval = get_pref($link, | |
863be6ca | 293 | 'PURGE_OLD_DAYS', $owner_uid); |
07d0efe9 AD |
294 | |
295 | return $purge_interval; | |
296 | ||
297 | } else { | |
298 | return -1; | |
299 | } | |
300 | } | |
301 | ||
b6eefba5 | 302 | function purge_old_posts($link) { |
5d73494a | 303 | |
f1a80dae AD |
304 | $user_id = $_SESSION["uid"]; |
305 | ||
306 | $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds | |
307 | WHERE owner_uid = '$user_id'"); | |
5d73494a AD |
308 | |
309 | while ($line = db_fetch_assoc($result)) { | |
310 | ||
311 | $feed_id = $line["id"]; | |
312 | $purge_interval = $line["purge_interval"]; | |
313 | ||
b619ff15 | 314 | if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS'); |
5d73494a | 315 | |
140aae81 | 316 | if ($purge_interval > 0) { |
fefa6ca3 | 317 | purge_feed($link, $feed_id, $purge_interval); |
5d73494a AD |
318 | } |
319 | } | |
71604ca4 | 320 | |
0e0dd486 AD |
321 | purge_orphans($link); |
322 | } | |
323 | ||
324 | function purge_orphans($link) { | |
71604ca4 AD |
325 | // purge orphaned posts in main content table |
326 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
327 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
c3a8d71a AD |
328 | } |
329 | ||
c7d57b66 AD |
330 | function get_feed_update_interval($link, $feed_id) { |
331 | $result = db_query($link, "SELECT owner_uid, update_interval FROM | |
332 | ttrss_feeds WHERE id = '$feed_id'"); | |
333 | ||
334 | if (db_num_rows($result) == 1) { | |
335 | $update_interval = db_fetch_result($result, 0, "update_interval"); | |
336 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
337 | ||
338 | if ($update_interval != 0) { | |
339 | return $update_interval; | |
340 | } else { | |
341 | return get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $owner_uid, false); | |
342 | } | |
343 | ||
344 | } else { | |
345 | return -1; | |
346 | } | |
347 | } | |
348 | ||
4065b60b AD |
349 | function fetch_file_contents($url) { |
350 | if (USE_CURL_FOR_ICONS) { | |
351 | $tmpfile = tempnam(TMP_DIRECTORY, "ttrss-tmp"); | |
352 | ||
353 | $ch = curl_init($url); | |
354 | $fp = fopen($tmpfile, "w"); | |
355 | ||
356 | if ($fp) { | |
357 | curl_setopt($ch, CURLOPT_FILE, $fp); | |
dd966fed AD |
358 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); |
359 | curl_setopt($ch, CURLOPT_TIMEOUT, 45); | |
4065b60b AD |
360 | curl_exec($ch); |
361 | curl_close($ch); | |
362 | fclose($fp); | |
363 | } | |
364 | ||
365 | $contents = file_get_contents($tmpfile); | |
366 | unlink($tmpfile); | |
367 | ||
368 | return $contents; | |
369 | ||
370 | } else { | |
371 | return file_get_contents($url); | |
372 | } | |
373 | ||
374 | } | |
78800912 | 375 | |
9632f884 AD |
376 | /** |
377 | * Try to determine the favicon URL for a feed. | |
378 | * adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/) | |
379 | * http://dev.wp-plugins.org/file/favatars/trunk/favatars.php | |
380 | * | |
381 | * @param string $url A feed or page URL | |
382 | * @access public | |
383 | * @return mixed The favicon URL, or false if none was found. | |
384 | */ | |
4065b60b | 385 | function get_favicon_url($url) { |
99331724 | 386 | |
4065b60b | 387 | if ($html = @fetch_file_contents($url)) { |
78800912 | 388 | |
4065b60b AD |
389 | if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) { |
390 | // Attempt to grab a favicon link from their webpage url | |
391 | $linkUrl = html_entity_decode($matches[1]); | |
c798704b | 392 | |
4065b60b AD |
393 | if (substr($linkUrl, 0, 1) == '/') { |
394 | $urlParts = parse_url($url); | |
395 | $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl; | |
396 | } else if (substr($linkUrl, 0, 7) == 'http://') { | |
397 | $faviconURL = $linkUrl; | |
398 | } else if (substr($url, -1, 1) == '/') { | |
399 | $faviconURL = $url.$linkUrl; | |
400 | } else { | |
401 | $faviconURL = $url.'/'.$linkUrl; | |
e695fdc8 | 402 | } |
717f5e64 | 403 | |
c798704b | 404 | } else { |
4065b60b AD |
405 | // If unsuccessful, attempt to "guess" the favicon location |
406 | $urlParts = parse_url($url); | |
407 | $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico'; | |
408 | } | |
409 | } | |
c798704b | 410 | |
4065b60b AD |
411 | // Run a test to see if what we have attempted to get actually exists. |
412 | if(USE_CURL_FOR_ICONS || url_validate($faviconURL)) { | |
413 | return $faviconURL; | |
414 | } else { | |
415 | return false; | |
416 | } | |
9632f884 | 417 | } // function get_favicon_url |
4065b60b | 418 | |
9632f884 AD |
419 | /** |
420 | * Check if a link is a valid and working URL. | |
421 | * | |
422 | * @param mixed $link A URL to check | |
423 | * @access public | |
424 | * @return boolean True if the URL is valid, false otherwise. | |
425 | */ | |
4065b60b AD |
426 | function url_validate($link) { |
427 | ||
428 | $url_parts = @parse_url($link); | |
429 | ||
430 | if ( empty( $url_parts["host"] ) ) | |
431 | return false; | |
432 | ||
433 | if ( !empty( $url_parts["path"] ) ) { | |
434 | $documentpath = $url_parts["path"]; | |
435 | } else { | |
436 | $documentpath = "/"; | |
437 | } | |
438 | ||
439 | if ( !empty( $url_parts["query"] ) ) | |
440 | $documentpath .= "?" . $url_parts["query"]; | |
441 | ||
442 | $host = $url_parts["host"]; | |
443 | $port = $url_parts["port"]; | |
444 | ||
445 | if ( empty($port) ) | |
446 | $port = "80"; | |
447 | ||
448 | $socket = @fsockopen( $host, $port, $errno, $errstr, 30 ); | |
449 | ||
450 | if ( !$socket ) | |
451 | return false; | |
c798704b | 452 | |
4065b60b AD |
453 | fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n"); |
454 | ||
455 | $http_response = fgets( $socket, 22 ); | |
456 | ||
457 | $responses = "/(200 OK)|(30[0-9] Moved)/"; | |
458 | if ( preg_match($responses, $http_response) ) { | |
459 | fclose($socket); | |
460 | return true; | |
461 | } else { | |
462 | return false; | |
463 | } | |
464 | ||
9632f884 | 465 | } // function url_validate |
4065b60b AD |
466 | |
467 | function check_feed_favicon($site_url, $feed, $link) { | |
468 | $favicon_url = get_favicon_url($site_url); | |
469 | ||
470 | # print "FAVICON [$site_url]: $favicon_url\n"; | |
471 | ||
472 | error_reporting(0); | |
473 | ||
474 | $icon_file = ICONS_DIR . "/$feed.ico"; | |
475 | ||
476 | if ($favicon_url && !file_exists($icon_file)) { | |
477 | $contents = fetch_file_contents($favicon_url); | |
478 | ||
479 | $fp = fopen($icon_file, "w"); | |
78800912 | 480 | |
4065b60b AD |
481 | if ($fp) { |
482 | fwrite($fp, $contents); | |
483 | fclose($fp); | |
484 | chmod($icon_file, 0644); | |
485 | } | |
78800912 | 486 | } |
4065b60b AD |
487 | |
488 | error_reporting(DEFAULT_ERROR_LEVEL); | |
489 | ||
78800912 AD |
490 | } |
491 | ||
ddb68b81 | 492 | function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) { |
40d13c28 | 493 | |
17c0eeba | 494 | if (!$_GET["daemon"] && !$ignore_daemon) { |
45004d43 | 495 | return false; |
21cfcdf2 AD |
496 | } |
497 | ||
4bc64807 | 498 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
34e420fb | 499 | _debug("update_rss_feed: start"); |
219bd8fc AD |
500 | } |
501 | ||
39a52499 AD |
502 | if (!$ignore_daemon) { |
503 | ||
504 | if (DB_TYPE == "pgsql") { | |
505 | $updstart_thresh_qpart = "(ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')"; | |
506 | } else { | |
507 | $updstart_thresh_qpart = "(ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))"; | |
508 | } | |
509 | ||
510 | $result = db_query($link, "SELECT id,update_interval,auth_login, | |
16211ddb | 511 | auth_pass,cache_images,update_method |
39a52499 | 512 | FROM ttrss_feeds WHERE id = '$feed' AND $updstart_thresh_qpart"); |
02008cb1 | 513 | |
39a52499 AD |
514 | } else { |
515 | ||
516 | $result = db_query($link, "SELECT id,update_interval,auth_login, | |
50b2db96 | 517 | auth_pass,cache_images,update_method,hidden,last_updated |
39a52499 AD |
518 | FROM ttrss_feeds WHERE id = '$feed'"); |
519 | ||
520 | } | |
a88c1f36 | 521 | |
5370d37f AD |
522 | if (db_num_rows($result) == 0) { |
523 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
02008cb1 | 524 | _debug("update_rss_feed: feed $feed [$feed_url] NOT FOUND/SKIPPED"); |
5370d37f | 525 | } |
45004d43 | 526 | return false; |
5370d37f AD |
527 | } |
528 | ||
ddc34b9b | 529 | $hidden = sql_bool_to_bool(db_fetch_result($result, 0, "hidden")); |
16211ddb | 530 | $update_method = db_fetch_result($result, 0, "update_method"); |
50b2db96 | 531 | $last_updated = db_fetch_result($result, 0, "last_updated"); |
16211ddb | 532 | |
3c50da83 AD |
533 | db_query($link, "UPDATE ttrss_feeds SET last_update_started = NOW() |
534 | WHERE id = '$feed'"); | |
535 | ||
464bd61e AD |
536 | $auth_login = db_fetch_result($result, 0, "auth_login"); |
537 | $auth_pass = db_fetch_result($result, 0, "auth_pass"); | |
538 | ||
34459667 AD |
539 | if (ALLOW_SELECT_UPDATE_METHOD) { |
540 | if (ENABLE_SIMPLEPIE) { | |
541 | $use_simplepie = $update_method != 1; | |
542 | } else { | |
543 | $use_simplepie = $update_method == 2; | |
544 | } | |
16211ddb | 545 | } else { |
34459667 | 546 | $use_simplepie = ENABLE_SIMPLEPIE; |
16211ddb AD |
547 | } |
548 | ||
549 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
550 | _debug("use simplepie: $use_simplepie (feed setting: $update_method)\n"); | |
551 | } | |
552 | ||
553 | if (!$use_simplepie) { | |
464bd61e AD |
554 | $auth_login = urlencode($auth_login); |
555 | $auth_pass = urlencode($auth_pass); | |
556 | } | |
47c6c988 | 557 | |
a88c1f36 | 558 | $update_interval = db_fetch_result($result, 0, "update_interval"); |
bc0f0785 | 559 | $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images")); |
a88c1f36 AD |
560 | |
561 | if ($update_interval < 0) { return; } | |
562 | ||
ab3d0b99 AD |
563 | $feed = db_escape_string($feed); |
564 | ||
47c6c988 AD |
565 | $fetch_url = $feed_url; |
566 | ||
567 | if ($auth_login && $auth_pass) { | |
568 | $url_parts = array(); | |
569 | preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts); | |
570 | ||
571 | if ($url_parts[1] && $url_parts[2]) { | |
572 | $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2]; | |
573 | } | |
574 | ||
575 | } | |
ab3d0b99 | 576 | |
4bc64807 | 577 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
ca872b9d | 578 | _debug("update_rss_feed: fetching [$fetch_url]..."); |
219bd8fc AD |
579 | } |
580 | ||
9fdf7824 | 581 | if (!defined('DAEMON_EXTENDED_DEBUG') && !$_GET['xdebug']) { |
219bd8fc AD |
582 | error_reporting(0); |
583 | } | |
584 | ||
16211ddb | 585 | if (!$use_simplepie) { |
9fdf7824 AD |
586 | $rss = fetch_rss($fetch_url); |
587 | } else { | |
c7d57b66 AD |
588 | if (!is_dir(SIMPLEPIE_CACHE_DIR)) { |
589 | mkdir(SIMPLEPIE_CACHE_DIR); | |
590 | } | |
591 | ||
ca872b9d AD |
592 | $rss = new SimplePie(); |
593 | $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT); | |
16211ddb | 594 | # $rss->set_timeout(10); |
ca872b9d AD |
595 | $rss->set_feed_url($fetch_url); |
596 | $rss->set_output_encoding('UTF-8'); | |
c7d57b66 | 597 | |
bc0f0785 AD |
598 | if (SIMPLEPIE_CACHE_IMAGES && $cache_images) { |
599 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
600 | _debug("enabling image cache"); | |
601 | } | |
602 | ||
dab52d7b | 603 | $rss->set_image_handler('./image.php', 'i'); |
bc0f0785 | 604 | } |
dab52d7b | 605 | |
c7d57b66 AD |
606 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
607 | _debug("feed update interval (sec): " . | |
608 | get_feed_update_interval($link, $feed)*60); | |
609 | } | |
610 | ||
611 | if (is_dir(SIMPLEPIE_CACHE_DIR)) { | |
612 | $rss->set_cache_location(SIMPLEPIE_CACHE_DIR); | |
613 | $rss->set_cache_duration(get_feed_update_interval($link, $feed) * 60); | |
614 | } | |
615 | ||
9fdf7824 | 616 | $rss->init(); |
9fdf7824 AD |
617 | } |
618 | ||
619 | // print_r($rss); | |
219bd8fc | 620 | |
4bc64807 | 621 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
34e420fb | 622 | _debug("update_rss_feed: fetch done, parsing..."); |
219bd8fc AD |
623 | } else { |
624 | error_reporting (DEFAULT_ERROR_LEVEL); | |
625 | } | |
626 | ||
b6eefba5 | 627 | $feed = db_escape_string($feed); |
dcee8f61 | 628 | |
16211ddb | 629 | if ($use_simplepie) { |
ca872b9d AD |
630 | $fetch_ok = !$rss->error(); |
631 | } else { | |
632 | $fetch_ok = !!$rss; | |
633 | } | |
634 | ||
635 | if ($fetch_ok) { | |
50b62214 | 636 | |
4bc64807 | 637 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
50b62214 AD |
638 | _debug("update_rss_feed: processing feed data..."); |
639 | } | |
640 | ||
44e241cb | 641 | // db_query($link, "BEGIN"); |
dd8c76a9 | 642 | |
a88c1f36 | 643 | $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid |
f324892e | 644 | FROM ttrss_feeds WHERE id = '$feed'"); |
331900c6 | 645 | |
b6eefba5 AD |
646 | $registered_title = db_fetch_result($result, 0, "title"); |
647 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
f324892e | 648 | $orig_site_url = db_fetch_result($result, 0, "site_url"); |
331900c6 | 649 | |
7fed1940 AD |
650 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); |
651 | ||
16211ddb | 652 | if ($use_simplepie) { |
9fdf7824 AD |
653 | $site_url = $rss->get_link(); |
654 | } else { | |
655 | $site_url = $rss->channel["link"]; | |
656 | } | |
657 | ||
8d0ec6fd | 658 | if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) { |
4bc64807 | 659 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
50b62214 AD |
660 | _debug("update_rss_feed: checking favicon..."); |
661 | } | |
9fdf7824 AD |
662 | |
663 | check_feed_favicon($site_url, $feed, $link); | |
a2770077 AD |
664 | } |
665 | ||
746b249f | 666 | if (!$registered_title || $registered_title == "[Unknown]") { |
4bc64807 | 667 | |
16211ddb | 668 | if ($use_simplepie) { |
c2f8aac4 | 669 | $feed_title = db_escape_string($rss->get_title()); |
fb486a33 AD |
670 | } else { |
671 | $feed_title = db_escape_string($rss->channel["title"]); | |
672 | } | |
4bc64807 AD |
673 | |
674 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
675 | _debug("update_rss_feed: registering title: $feed_title"); | |
676 | } | |
7c5a308d | 677 | |
f324892e AD |
678 | db_query($link, "UPDATE ttrss_feeds SET |
679 | title = '$feed_title' WHERE id = '$feed'"); | |
680 | } | |
681 | ||
49f9c923 | 682 | // weird, weird Magpie |
16211ddb | 683 | if (!$use_simplepie) { |
9fdf7824 AD |
684 | if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]); |
685 | } | |
147f7691 AD |
686 | |
687 | if ($site_url && $orig_site_url != db_escape_string($site_url)) { | |
f324892e AD |
688 | db_query($link, "UPDATE ttrss_feeds SET |
689 | site_url = '$site_url' WHERE id = '$feed'"); | |
331900c6 | 690 | } |
40d13c28 | 691 | |
b7f4bda2 AD |
692 | // print "I: " . $rss->channel["image"]["url"]; |
693 | ||
16211ddb | 694 | if (!$use_simplepie) { |
9fdf7824 AD |
695 | $icon_url = $rss->image["url"]; |
696 | } else { | |
697 | $icon_url = $rss->get_image_url(); | |
698 | } | |
b7f4bda2 | 699 | |
147f7691 | 700 | if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) { |
b6eefba5 AD |
701 | $icon_url = db_escape_string($icon_url); |
702 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
b7f4bda2 AD |
703 | } |
704 | ||
51e456d6 | 705 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
50b62214 AD |
706 | _debug("update_rss_feed: loading filters..."); |
707 | } | |
e6155a06 | 708 | |
5daa24f2 | 709 | $filters = load_filters($link, $feed, $owner_uid); |
e6155a06 | 710 | |
16211ddb | 711 | if ($use_simplepie) { |
9fdf7824 AD |
712 | $iterator = $rss->get_items(); |
713 | } else { | |
714 | $iterator = $rss->items; | |
715 | if (!$iterator || !is_array($iterator)) $iterator = $rss->entries; | |
716 | if (!$iterator || !is_array($iterator)) $iterator = $rss; | |
717 | } | |
c22789da AD |
718 | |
719 | if (!is_array($iterator)) { | |
39541e74 | 720 | /* db_query($link, "UPDATE ttrss_feeds |
75bd0669 | 721 | SET last_error = 'Parse error: can\'t find any articles.' |
77f0a2a7 AD |
722 | WHERE id = '$feed'"); */ |
723 | ||
724 | // clear any errors and mark feed as updated if fetched okay | |
725 | // even if it's blank | |
726 | ||
51e456d6 | 727 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
844012bc AD |
728 | _debug("update_rss_feed: entry iterator is not an array, no articles?"); |
729 | } | |
730 | ||
77f0a2a7 AD |
731 | db_query($link, "UPDATE ttrss_feeds |
732 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
733 | ||
734 | return; // no articles | |
c22789da | 735 | } |
ddb68b81 | 736 | |
51e456d6 | 737 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
50b62214 AD |
738 | _debug("update_rss_feed: processing articles..."); |
739 | } | |
740 | ||
ddb68b81 | 741 | foreach ($iterator as $item) { |
7c5a308d | 742 | |
40ce98f4 AD |
743 | if ($_GET['xdebug']) { |
744 | print_r($item); | |
ea322415 | 745 | |
40ce98f4 | 746 | } |
71bd29f6 | 747 | |
16211ddb | 748 | if ($use_simplepie) { |
9fdf7824 AD |
749 | $entry_guid = $item->get_id(); |
750 | if (!$entry_guid) $entry_guid = $item->get_link(); | |
751 | if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title()); | |
752 | ||
753 | } else { | |
754 | ||
755 | $entry_guid = $item["id"]; | |
34e420fb | 756 | |
9fdf7824 AD |
757 | if (!$entry_guid) $entry_guid = $item["guid"]; |
758 | if (!$entry_guid) $entry_guid = $item["link"]; | |
759 | if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]); | |
760 | } | |
be832a1a | 761 | |
9fdf7824 | 762 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
34e420fb AD |
763 | _debug("update_rss_feed: guid $entry_guid"); |
764 | } | |
765 | ||
be832a1a AD |
766 | if (!$entry_guid) continue; |
767 | ||
768 | $entry_timestamp = ""; | |
769 | ||
16211ddb | 770 | if ($use_simplepie) { |
9fdf7824 AD |
771 | $entry_timestamp = strtotime($item->get_date()); |
772 | } else { | |
773 | $rss_2_date = $item['pubdate']; | |
774 | $rss_1_date = $item['dc']['date']; | |
775 | $atom_date = $item['issued']; | |
776 | if (!$atom_date) $atom_date = $item['updated']; | |
be832a1a | 777 | |
9fdf7824 AD |
778 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); |
779 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
780 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
781 | } | |
9bb36aa0 | 782 | |
2e930846 | 783 | if ($entry_timestamp == "" || $entry_timestamp == -1 || !$entry_timestamp) { |
be832a1a AD |
784 | $entry_timestamp = time(); |
785 | $no_orig_date = 'true'; | |
786 | } else { | |
787 | $no_orig_date = 'false'; | |
788 | } | |
789 | ||
790 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
791 | ||
44d0e774 AD |
792 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
793 | _debug("update_rss_feed: date $entry_timestamp [$entry_timestamp_fmt]"); | |
794 | } | |
795 | ||
16211ddb | 796 | if ($use_simplepie) { |
9fdf7824 AD |
797 | $entry_title = $item->get_title(); |
798 | } else { | |
799 | $entry_title = trim(strip_tags($item["title"])); | |
800 | } | |
be832a1a | 801 | |
16211ddb | 802 | if ($use_simplepie) { |
9fdf7824 AD |
803 | $entry_link = $item->get_link(); |
804 | } else { | |
805 | // strange Magpie workaround | |
806 | $entry_link = $item["link_"]; | |
807 | if (!$entry_link) $entry_link = $item["link"]; | |
808 | } | |
be832a1a | 809 | |
9bb36aa0 AD |
810 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
811 | _debug("update_rss_feed: title $entry_title"); | |
812 | } | |
813 | ||
814 | if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);; | |
7d3ab0dd | 815 | |
be832a1a | 816 | $entry_link = strip_tags($entry_link); |
7d3ab0dd | 817 | |
16211ddb | 818 | if ($use_simplepie) { |
4af7a36a AD |
819 | $entry_content = $item->get_content(); |
820 | if (!$entry_content) $entry_content = $item->get_description(); | |
9fdf7824 AD |
821 | } else { |
822 | $entry_content = $item["content:escaped"]; | |
823 | ||
824 | if (!$entry_content) $entry_content = $item["content:encoded"]; | |
e91ab107 | 825 | if (!$entry_content) $entry_content = $item["content"]["encoded"]; |
9fdf7824 | 826 | if (!$entry_content) $entry_content = $item["content"]; |
ea322415 AD |
827 | |
828 | // Magpie bugs are getting ridiculous | |
829 | if (trim($entry_content) == "Array") $entry_content = false; | |
830 | ||
9fdf7824 AD |
831 | if (!$entry_content) $entry_content = $item["atom_content"]; |
832 | if (!$entry_content) $entry_content = $item["summary"]; | |
e91ab107 AD |
833 | |
834 | if (!$entry_content || | |
835 | strlen($entry_content) < strlen($item["description"])) { | |
836 | $entry_content = $item["description"]; | |
837 | }; | |
9fdf7824 AD |
838 | |
839 | // WTF | |
840 | if (is_array($entry_content)) { | |
841 | $entry_content = $entry_content["encoded"]; | |
842 | if (!$entry_content) $entry_content = $entry_content["escaped"]; | |
ea322415 | 843 | } |
be832a1a AD |
844 | } |
845 | ||
ea322415 AD |
846 | if ($_GET["xdebug"]) { |
847 | print "update_rss_feed: content: "; | |
848 | print_r(htmlspecialchars($entry_content)); | |
849 | } | |
be832a1a AD |
850 | |
851 | $entry_content_unescaped = $entry_content; | |
be832a1a | 852 | |
16211ddb | 853 | if ($use_simplepie) { |
9fdf7824 | 854 | $entry_comments = strip_tags($item->data["comments"]); |
30cf38dd | 855 | if ($item->get_author()) { |
e1d600f0 | 856 | $entry_author_item = $item->get_author(); |
a0c6eafb AD |
857 | $entry_author = $entry_author_item->get_name(); |
858 | if (!$entry_author) $entry_author = $entry_author_item->get_email(); | |
d1ee9106 AD |
859 | |
860 | $entry_author = db_escape_string($entry_author); | |
30cf38dd | 861 | } |
9fdf7824 AD |
862 | } else { |
863 | $entry_comments = strip_tags($item["comments"]); | |
864 | ||
865 | $entry_author = db_escape_string(strip_tags($item['dc']['creator'])); | |
4d6e9157 | 866 | |
9fdf7824 AD |
867 | if ($item['author']) { |
868 | ||
869 | if (is_array($item['author'])) { | |
870 | ||
871 | if (!$entry_author) { | |
872 | $entry_author = db_escape_string(strip_tags($item['author']['name'])); | |
873 | } | |
874 | ||
875 | if (!$entry_author) { | |
876 | $entry_author = db_escape_string(strip_tags($item['author']['email'])); | |
877 | } | |
4d6e9157 | 878 | } |
9fdf7824 | 879 | |
4d6e9157 | 880 | if (!$entry_author) { |
9fdf7824 | 881 | $entry_author = db_escape_string(strip_tags($item['author'])); |
4d6e9157 | 882 | } |
83f114c8 | 883 | } |
be832a1a AD |
884 | } |
885 | ||
83f114c8 AD |
886 | if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = ''; |
887 | ||
be832a1a | 888 | $entry_guid = db_escape_string(strip_tags($entry_guid)); |
2ad9ee56 | 889 | $entry_guid = mb_substr($entry_guid, 0, 250); |
be832a1a AD |
890 | |
891 | $result = db_query($link, "SELECT id FROM ttrss_entries | |
892 | WHERE guid = '$entry_guid'"); | |
893 | ||
894 | $entry_content = db_escape_string($entry_content); | |
7e43ad58 AD |
895 | |
896 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); | |
897 | ||
be832a1a AD |
898 | $entry_title = db_escape_string($entry_title); |
899 | $entry_link = db_escape_string($entry_link); | |
2544f36b AD |
900 | $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250); |
901 | $entry_author = mb_substr($entry_author, 0, 250); | |
be832a1a | 902 | |
16211ddb | 903 | if ($use_simplepie) { |
9fdf7824 AD |
904 | $num_comments = 0; #FIXME# |
905 | } else { | |
906 | $num_comments = db_escape_string($item["slash"]["comments"]); | |
907 | } | |
be832a1a AD |
908 | |
909 | if (!$num_comments) $num_comments = 0; | |
910 | ||
fefef828 | 911 | // parse <category> entries into tags |
be832a1a | 912 | |
16211ddb | 913 | if ($use_simplepie) { |
be832a1a | 914 | |
fefef828 | 915 | $additional_tags = array(); |
9fdf7824 | 916 | $additional_tags_src = $item->get_categories(); |
3b9e5af4 | 917 | |
a702e931 AD |
918 | if (is_array($additional_tags_src)) { |
919 | foreach ($additional_tags_src as $tobj) { | |
920 | array_push($additional_tags, $tobj->get_term()); | |
921 | } | |
fefef828 | 922 | } |
fefef828 | 923 | |
6af621c7 AD |
924 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
925 | _debug("update_rss_feed: category tags:"); | |
926 | print_r($additional_tags); | |
927 | } | |
928 | ||
9fdf7824 | 929 | } else { |
fefef828 | 930 | |
9fdf7824 | 931 | $t_ctr = $item['category#']; |
fefef828 | 932 | |
9fdf7824 AD |
933 | $additional_tags = false; |
934 | ||
935 | if ($t_ctr == 0) { | |
936 | $additional_tags = false; | |
71bd29f6 | 937 | } else if ($t_ctr > 0) { |
9fdf7824 | 938 | $additional_tags = array($item['category']); |
71bd29f6 AD |
939 | |
940 | if ($item['category@term']) { | |
941 | array_push($additional_tags, $item['category@term']); | |
942 | } | |
943 | ||
9fdf7824 AD |
944 | for ($i = 0; $i <= $t_ctr; $i++ ) { |
945 | if ($item["category#$i"]) { | |
946 | array_push($additional_tags, $item["category#$i"]); | |
947 | } | |
71bd29f6 AD |
948 | |
949 | if ($item["category#$i@term"]) { | |
950 | array_push($additional_tags, $item["category#$i@term"]); | |
951 | } | |
9fdf7824 AD |
952 | } |
953 | } | |
954 | ||
955 | // parse <dc:subject> elements | |
956 | ||
957 | $t_ctr = $item['dc']['subject#']; | |
958 | ||
71bd29f6 | 959 | if ($t_ctr > 0) { |
9fdf7824 | 960 | $additional_tags = array($item['dc']['subject']); |
71bd29f6 | 961 | |
9fdf7824 AD |
962 | for ($i = 0; $i <= $t_ctr; $i++ ) { |
963 | if ($item['dc']["subject#$i"]) { | |
964 | array_push($additional_tags, $item['dc']["subject#$i"]); | |
965 | } | |
fefef828 AD |
966 | } |
967 | } | |
968 | } | |
8add756a | 969 | |
ce53e200 AD |
970 | // enclosures |
971 | ||
972 | $enclosures = array(); | |
973 | ||
16211ddb | 974 | if ($use_simplepie) { |
ce53e200 AD |
975 | $encs = $item->get_enclosures(); |
976 | ||
3b9e5af4 AD |
977 | if (is_array($encs)) { |
978 | foreach ($encs as $e) { | |
979 | $e_item = array( | |
980 | $e->link, $e->type, $e->length); | |
981 | ||
982 | array_push($enclosures, $e_item); | |
983 | } | |
ce53e200 AD |
984 | } |
985 | ||
986 | } else { | |
e91ab107 AD |
987 | // <enclosure> |
988 | ||
ce53e200 AD |
989 | $e_ctr = $item['enclosure#']; |
990 | ||
991 | if ($e_ctr > 0) { | |
992 | $e_item = array($item['enclosure@url'], | |
993 | $item['enclosure@type'], | |
994 | $item['enclosure@length']); | |
995 | ||
996 | array_push($enclosures, $e_item); | |
997 | ||
71bd29f6 AD |
998 | for ($i = 0; $i <= $e_ctr; $i++ ) { |
999 | ||
1000 | if ($item["enclosure#$i@url"]) { | |
1001 | $e_item = array($item["enclosure#$i@url"], | |
1002 | $item["enclosure#$i@type"], | |
1003 | $item["enclosure#$i@length"]); | |
1004 | array_push($enclosures, $e_item); | |
1005 | } | |
ce53e200 AD |
1006 | } |
1007 | } | |
1008 | ||
e91ab107 | 1009 | // <media:content> |
b652fdae | 1010 | // can there be many of those? yes -fox |
e91ab107 AD |
1011 | |
1012 | $m_ctr = $item['media']['content#']; | |
1013 | ||
1014 | if ($m_ctr > 0) { | |
1015 | $e_item = array($item['media']['content@url'], | |
1016 | $item['media']['content@medium'], | |
1017 | $item['media']['content@length']); | |
1018 | ||
1019 | array_push($enclosures, $e_item); | |
e91ab107 | 1020 | |
b652fdae AD |
1021 | for ($i = 0; $i <= $m_ctr; $i++ ) { |
1022 | ||
1023 | if ($item["media"]["content#$i@url"]) { | |
1024 | $e_item = array($item["media"]["content#$i@url"], | |
1025 | $item["media"]["content#$i@medium"], | |
1026 | $item["media"]["content#$i@length"]); | |
1027 | array_push($enclosures, $e_item); | |
1028 | } | |
1029 | } | |
1030 | ||
1031 | } | |
ce53e200 AD |
1032 | } |
1033 | ||
d48d160c | 1034 | # sanitize content |
183ad07b | 1035 | |
621ffb00 AD |
1036 | $entry_content = sanitize_article_content($entry_content); |
1037 | $entry_title = sanitize_article_content($entry_title); | |
d48d160c | 1038 | |
9fdf7824 | 1039 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
34e420fb AD |
1040 | _debug("update_rss_feed: done collecting data [TITLE:$entry_title]"); |
1041 | } | |
1042 | ||
44e241cb AD |
1043 | db_query($link, "BEGIN"); |
1044 | ||
4c193675 AD |
1045 | if (db_num_rows($result) == 0) { |
1046 | ||
9fdf7824 | 1047 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
34e420fb AD |
1048 | _debug("update_rss_feed: base guid not found"); |
1049 | } | |
1050 | ||
4c193675 AD |
1051 | // base post entry does not exist, create it |
1052 | ||
4c193675 AD |
1053 | $result = db_query($link, |
1054 | "INSERT INTO ttrss_entries | |
1055 | (title, | |
1056 | guid, | |
1057 | link, | |
1058 | updated, | |
1059 | content, | |
1060 | content_hash, | |
1061 | no_orig_date, | |
1062 | date_entered, | |
11b0dce2 | 1063 | comments, |
b6104dee AD |
1064 | num_comments, |
1065 | author) | |
4c193675 AD |
1066 | VALUES |
1067 | ('$entry_title', | |
1068 | '$entry_guid', | |
1069 | '$entry_link', | |
1070 | '$entry_timestamp_fmt', | |
1071 | '$entry_content', | |
1072 | '$content_hash', | |
1073 | $no_orig_date, | |
1074 | NOW(), | |
11b0dce2 | 1075 | '$entry_comments', |
b6104dee AD |
1076 | '$num_comments', |
1077 | '$entry_author')"); | |
8926aab8 AD |
1078 | } else { |
1079 | // we keep encountering the entry in feeds, so we need to | |
1080 | // update date_entered column so that we don't get horrible | |
1081 | // dupes when the entry gets purged and reinserted again e.g. | |
1082 | // in the case of SLOW SLOW OMG SLOW updating feeds | |
1083 | ||
1084 | $base_entry_id = db_fetch_result($result, 0, "id"); | |
1085 | ||
1086 | db_query($link, "UPDATE ttrss_entries SET date_entered = NOW() | |
1087 | WHERE id = '$base_entry_id'"); | |
4c193675 AD |
1088 | } |
1089 | ||
1090 | // now it should exist, if not - bad luck then | |
1091 | ||
6385315d AD |
1092 | $result = db_query($link, "SELECT |
1093 | id,content_hash,no_orig_date,title, | |
2ac6b765 AD |
1094 | ".SUBSTRING_FOR_DATE."(date_entered,1,19) as date_entered, |
1095 | ".SUBSTRING_FOR_DATE."(updated,1,19) as updated, | |
11b0dce2 | 1096 | num_comments |
6385315d AD |
1097 | FROM |
1098 | ttrss_entries | |
1099 | WHERE guid = '$entry_guid'"); | |
4c193675 | 1100 | |
ce53e200 AD |
1101 | $entry_ref_id = 0; |
1102 | $entry_int_id = 0; | |
1103 | ||
4c193675 AD |
1104 | if (db_num_rows($result) == 1) { |
1105 | ||
9fdf7824 | 1106 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
7ca91eb3 | 1107 | _debug("update_rss_feed: base guid found, checking for user record"); |
34e420fb AD |
1108 | } |
1109 | ||
11b0dce2 AD |
1110 | // this will be used below in update handler |
1111 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); | |
1112 | $orig_title = db_fetch_result($result, 0, "title"); | |
1113 | $orig_num_comments = db_fetch_result($result, 0, "num_comments"); | |
8926aab8 AD |
1114 | $orig_date_entered = strtotime(db_fetch_result($result, |
1115 | 0, "date_entered")); | |
6385315d | 1116 | |
11b0dce2 | 1117 | $ref_id = db_fetch_result($result, 0, "id"); |
ce53e200 | 1118 | $entry_ref_id = $ref_id; |
4c193675 | 1119 | |
11b0dce2 | 1120 | // check for user post link to main table |
4c193675 | 1121 | |
11b0dce2 | 1122 | // do we allow duplicate posts with same GUID in different feeds? |
8d0ec6fd | 1123 | if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) { |
11b0dce2 AD |
1124 | $dupcheck_qpart = "AND feed_id = '$feed'"; |
1125 | } else { | |
1126 | $dupcheck_qpart = ""; | |
1127 | } | |
71604ca4 | 1128 | |
11b0dce2 | 1129 | // error_reporting(0); |
19c9cb11 | 1130 | |
f8382011 | 1131 | $article_filters = get_article_filters($filters, $entry_title, |
44d0e774 | 1132 | $entry_content, $entry_link, $entry_timestamp); |
19c9cb11 | 1133 | |
9fdf7824 | 1134 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
7ca91eb3 AD |
1135 | _debug("update_rss_feed: article filters: "); |
1136 | if (count($article_filters) != 0) { | |
1137 | print_r($article_filters); | |
1138 | } | |
1139 | } | |
1140 | ||
f8382011 | 1141 | if (find_article_filter($article_filters, "filter")) { |
ee4a9812 | 1142 | db_query($link, "COMMIT"); // close transaction in progress |
11b0dce2 AD |
1143 | continue; |
1144 | } | |
19c9cb11 | 1145 | |
11b0dce2 | 1146 | // error_reporting (DEFAULT_ERROR_LEVEL); |
3a933f22 | 1147 | |
ff6e357a AD |
1148 | $score = calculate_article_score($article_filters); |
1149 | ||
1150 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1151 | _debug("update_rss_feed: initial score: $score"); | |
1152 | } | |
1153 | ||
11b0dce2 | 1154 | $result = db_query($link, |
ce53e200 | 1155 | "SELECT ref_id, int_id FROM ttrss_user_entries WHERE |
11b0dce2 AD |
1156 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' |
1157 | $dupcheck_qpart"); | |
7ca91eb3 | 1158 | |
11b0dce2 AD |
1159 | // okay it doesn't exist - create user entry |
1160 | if (db_num_rows($result) == 0) { | |
1161 | ||
9fdf7824 | 1162 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
7ca91eb3 AD |
1163 | _debug("update_rss_feed: user record not found, creating..."); |
1164 | } | |
1165 | ||
24605713 | 1166 | if ($score >= -500 && !find_article_filter($article_filters, 'catchup')) { |
11b0dce2 AD |
1167 | $unread = 'true'; |
1168 | $last_read_qpart = 'NULL'; | |
1169 | } else { | |
1170 | $unread = 'false'; | |
1171 | $last_read_qpart = 'NOW()'; | |
1172 | } | |
dd7d3187 | 1173 | |
32d59314 | 1174 | if (find_article_filter($article_filters, 'mark') || $score > 1000) { |
dd7d3187 AD |
1175 | $marked = 'true'; |
1176 | } else { | |
1177 | $marked = 'false'; | |
1178 | } | |
a36c0dfe AD |
1179 | |
1180 | if (find_article_filter($article_filters, 'publish')) { | |
1181 | $published = 'true'; | |
1182 | } else { | |
1183 | $published = 'false'; | |
1184 | } | |
1185 | ||
11b0dce2 AD |
1186 | $result = db_query($link, |
1187 | "INSERT INTO ttrss_user_entries | |
ff6e357a AD |
1188 | (ref_id, owner_uid, feed_id, unread, last_read, marked, |
1189 | published, score) | |
11b0dce2 | 1190 | VALUES ('$ref_id', '$owner_uid', '$feed', $unread, |
ff6e357a | 1191 | $last_read_qpart, $marked, $published, '$score')"); |
ce53e200 AD |
1192 | |
1193 | $result = db_query($link, | |
1194 | "SELECT int_id FROM ttrss_user_entries WHERE | |
1195 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND | |
1196 | feed_id = '$feed' LIMIT 1"); | |
1197 | ||
1198 | if (db_num_rows($result) == 1) { | |
1199 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
1200 | } | |
1201 | } else { | |
1202 | $entry_ref_id = db_fetch_result($result, 0, "ref_id"); | |
1203 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
11b0dce2 | 1204 | } |
ce53e200 AD |
1205 | |
1206 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1207 | _debug("update_rss_feed: RID: $entry_ref_id, IID: $entry_int_id"); | |
1208 | } | |
1209 | ||
6385315d AD |
1210 | $post_needs_update = false; |
1211 | ||
8d0ec6fd | 1212 | if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) && |
6385315d | 1213 | ($content_hash != $orig_content_hash)) { |
7e43ad58 | 1214 | // print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->"; |
6385315d AD |
1215 | $post_needs_update = true; |
1216 | } | |
1217 | ||
7e43ad58 | 1218 | if (db_escape_string($orig_title) != $entry_title) { |
6385315d AD |
1219 | $post_needs_update = true; |
1220 | } | |
1221 | ||
11b0dce2 AD |
1222 | if ($orig_num_comments != $num_comments) { |
1223 | $post_needs_update = true; | |
1224 | } | |
1225 | ||
6385315d AD |
1226 | // this doesn't seem to be very reliable |
1227 | // | |
1228 | // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) { | |
1229 | // $post_needs_update = true; | |
1230 | // } | |
1231 | ||
1232 | // if post needs update, update it and mark all user entries | |
1c73bc0c | 1233 | // linking to this post as updated |
6385315d AD |
1234 | if ($post_needs_update) { |
1235 | ||
7ca91eb3 AD |
1236 | if (defined('DAEMON_EXTENDED_DEBUG')) { |
1237 | _debug("update_rss_feed: post $entry_guid needs update..."); | |
1238 | } | |
1239 | ||
6385315d AD |
1240 | // print "<!-- post $orig_title needs update : $post_needs_update -->"; |
1241 | ||
6385315d | 1242 | db_query($link, "UPDATE ttrss_entries |
11b0dce2 | 1243 | SET title = '$entry_title', content = '$entry_content', |
7e43ad58 | 1244 | content_hash = '$content_hash', |
11b0dce2 | 1245 | num_comments = '$num_comments' |
6385315d AD |
1246 | WHERE id = '$ref_id'"); |
1247 | ||
8d0ec6fd | 1248 | if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) { |
4919fb42 AD |
1249 | db_query($link, "UPDATE ttrss_user_entries |
1250 | SET last_read = null, unread = true WHERE ref_id = '$ref_id'"); | |
1251 | } else { | |
1252 | db_query($link, "UPDATE ttrss_user_entries | |
1253 | SET last_read = null WHERE ref_id = '$ref_id' AND unread = false"); | |
1254 | } | |
6385315d AD |
1255 | |
1256 | } | |
4c193675 AD |
1257 | } |
1258 | ||
44e241cb AD |
1259 | db_query($link, "COMMIT"); |
1260 | ||
ceb30ba4 AD |
1261 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
1262 | _debug("update_rss_feed: assigning labels..."); | |
1263 | } | |
1264 | ||
1265 | assign_article_to_labels($link, $entry_ref_id, $article_filters, | |
1266 | $owner_uid); | |
1267 | ||
ce53e200 AD |
1268 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
1269 | _debug("update_rss_feed: looking for enclosures..."); | |
1270 | } | |
1271 | ||
1272 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1273 | print_r($enclosures); | |
1274 | } | |
1275 | ||
1276 | db_query($link, "BEGIN"); | |
1277 | ||
1278 | foreach ($enclosures as $enc) { | |
1279 | $enc_url = db_escape_string($enc[0]); | |
1280 | $enc_type = db_escape_string($enc[1]); | |
1281 | $enc_dur = db_escape_string($enc[2]); | |
1282 | ||
1283 | $result = db_query($link, "SELECT id FROM ttrss_enclosures | |
1284 | WHERE content_url = '$enc_url' AND post_id = '$entry_ref_id'"); | |
1285 | ||
1286 | if (db_num_rows($result) == 0) { | |
1287 | db_query($link, "INSERT INTO ttrss_enclosures | |
1288 | (content_url, content_type, title, duration, post_id) VALUES | |
1289 | ('$enc_url', '$enc_type', '', '$enc_dur', '$entry_ref_id')"); | |
1290 | } | |
1291 | } | |
1292 | ||
1293 | db_query($link, "COMMIT"); | |
1294 | ||
9fdf7824 | 1295 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
34e420fb AD |
1296 | _debug("update_rss_feed: looking for tags..."); |
1297 | } | |
1298 | ||
eb36b4eb | 1299 | /* taaaags */ |
40e1a95b | 1300 | // <a href="..." rel="tag">Xorg</a>, // |
eb36b4eb | 1301 | |
05732aa0 | 1302 | $entry_tags = null; |
eb36b4eb | 1303 | |
40e1a95b | 1304 | preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i", |
ee2c3050 AD |
1305 | $entry_content_unescaped, $entry_tags); |
1306 | ||
fefef828 AD |
1307 | /* print "<p><br/>$entry_title : $entry_content_unescaped<br>"; |
1308 | print_r($entry_tags); | |
1309 | print "<br/></p>"; */ | |
eb36b4eb AD |
1310 | |
1311 | $entry_tags = $entry_tags[1]; | |
1312 | ||
073ca0e6 AD |
1313 | # check for manual tags |
1314 | ||
f8382011 AD |
1315 | $tag_filter = find_article_filter($article_filters, "tag"); |
1316 | ||
1317 | if ($tag_filter) { | |
073ca0e6 | 1318 | |
f8382011 | 1319 | $manual_tags = trim_array(split(",", $tag_filter[1])); |
073ca0e6 | 1320 | |
f8382011 | 1321 | foreach ($manual_tags as $tag) { |
be832a1a AD |
1322 | if (tag_is_valid($tag)) { |
1323 | array_push($entry_tags, $tag); | |
1324 | } | |
1325 | } | |
1326 | } | |
1327 | ||
11c9ea1f AD |
1328 | $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link, |
1329 | 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8'))); | |
8fc70781 | 1330 | |
fefef828 AD |
1331 | if ($additional_tags && is_array($additional_tags)) { |
1332 | foreach ($additional_tags as $tag) { | |
156a785d AD |
1333 | if (tag_is_valid($tag) && |
1334 | array_search($tag, $boring_tags) === FALSE) { | |
073ca0e6 AD |
1335 | array_push($entry_tags, $tag); |
1336 | } | |
1337 | } | |
fefef828 AD |
1338 | } |
1339 | ||
fcc95e24 | 1340 | // print "<p>TAGS: "; print_r($entry_tags); print "</p>"; |
073ca0e6 | 1341 | |
9fdf7824 AD |
1342 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
1343 | print_r($entry_tags); | |
1344 | } | |
1345 | ||
eb36b4eb AD |
1346 | if (count($entry_tags) > 0) { |
1347 | ||
44e241cb AD |
1348 | db_query($link, "BEGIN"); |
1349 | ||
fe99ab12 | 1350 | foreach ($entry_tags as $tag) { |
fefef828 | 1351 | |
14b6c54b | 1352 | $tag = sanitize_tag($tag); |
fefef828 | 1353 | $tag = db_escape_string($tag); |
31483fc1 | 1354 | |
ef063748 AD |
1355 | if (!tag_is_valid($tag)) continue; |
1356 | ||
fe99ab12 AD |
1357 | $result = db_query($link, "SELECT id FROM ttrss_tags |
1358 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND | |
1359 | owner_uid = '$owner_uid' LIMIT 1"); | |
1360 | ||
1361 | // print db_fetch_result($result, 0, "id"); | |
1362 | ||
1363 | if ($result && db_num_rows($result) == 0) { | |
1364 | ||
fe99ab12 AD |
1365 | db_query($link, "INSERT INTO ttrss_tags |
1366 | (owner_uid,tag_name,post_int_id) | |
1367 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
1368 | } | |
1369 | } | |
ce53e200 | 1370 | |
44e241cb | 1371 | db_query($link, "COMMIT"); |
05732aa0 | 1372 | } |
9fdf7824 AD |
1373 | |
1374 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1375 | _debug("update_rss_feed: article processed"); | |
1376 | } | |
4c193675 | 1377 | } |
40d13c28 | 1378 | |
50b2db96 AD |
1379 | if (!$last_updated) { |
1380 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1381 | _debug("update_rss_feed: new feed, catching it up..."); | |
1382 | } | |
1383 | catchup_feed($link, $feed, false); | |
1384 | } | |
1385 | ||
ddc34b9b AD |
1386 | if (!$hidden) { |
1387 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1388 | _debug("update_rss_feed: updating counters cache..."); | |
1389 | } | |
2627f2d0 | 1390 | |
3907ef71 AD |
1391 | // disabled, purge_feed() does that... |
1392 | //ccache_update($link, $feed, $owner_uid); | |
ddc34b9b | 1393 | } |
2627f2d0 | 1394 | |
0e70ed51 | 1395 | purge_feed($link, $feed, 0); |
3907ef71 | 1396 | |
ab3d0b99 AD |
1397 | db_query($link, "UPDATE ttrss_feeds |
1398 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
eb36b4eb | 1399 | |
44e241cb | 1400 | // db_query($link, "COMMIT"); |
dd8c76a9 | 1401 | |
ab3d0b99 | 1402 | } else { |
ca872b9d | 1403 | |
16211ddb | 1404 | if ($use_simplepie) { |
ca872b9d | 1405 | $error_msg = mb_substr($rss->error(), 0, 250); |
9fdf7824 | 1406 | } else { |
ca872b9d | 1407 | $error_msg = mb_substr(magpie_error(), 0, 250); |
9fdf7824 | 1408 | } |
ca872b9d AD |
1409 | |
1410 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
1411 | _debug("update_rss_feed: error fetching feed: $error_msg"); | |
1412 | } | |
1413 | ||
1414 | $error_msg = db_escape_string($error_msg); | |
1415 | ||
ab3d0b99 | 1416 | db_query($link, |
aa5f9f5f AD |
1417 | "UPDATE ttrss_feeds SET last_error = '$error_msg', |
1418 | last_updated = NOW() WHERE id = '$feed'"); | |
40d13c28 AD |
1419 | } |
1420 | ||
16211ddb | 1421 | if ($use_simplepie) { |
ac6ebdb3 AD |
1422 | unset($rss); |
1423 | } | |
1424 | ||
9fdf7824 | 1425 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { |
34e420fb | 1426 | _debug("update_rss_feed: done"); |
219bd8fc AD |
1427 | } |
1428 | ||
40d13c28 AD |
1429 | } |
1430 | ||
f175937c | 1431 | function print_select($id, $default, $values, $attributes = "") { |
79f3553b | 1432 | print "<select name=\"$id\" id=\"$id\" $attributes>"; |
a0d53889 AD |
1433 | foreach ($values as $v) { |
1434 | if ($v == $default) | |
1435 | $sel = " selected"; | |
1436 | else | |
1437 | $sel = ""; | |
1438 | ||
1439 | print "<option$sel>$v</option>"; | |
1440 | } | |
1441 | print "</select>"; | |
1442 | } | |
40d13c28 | 1443 | |
79f3553b AD |
1444 | function print_select_hash($id, $default, $values, $attributes = "") { |
1445 | print "<select name=\"$id\" id='$id' $attributes>"; | |
673d54ca AD |
1446 | foreach (array_keys($values) as $v) { |
1447 | if ($v == $default) | |
74d5c8fa | 1448 | $sel = 'selected="selected"'; |
673d54ca AD |
1449 | else |
1450 | $sel = ""; | |
1451 | ||
1452 | print "<option $sel value=\"$v\">".$values[$v]."</option>"; | |
1453 | } | |
1454 | ||
1455 | print "</select>"; | |
1456 | } | |
1457 | ||
44d0e774 | 1458 | function get_article_filters($filters, $title, $content, $link, $timestamp) { |
240054f1 | 1459 | $matches = array(); |
c2d9322b | 1460 | |
240054f1 AD |
1461 | if ($filters["title"]) { |
1462 | foreach ($filters["title"] as $filter) { | |
c2d9322b AD |
1463 | $reg_exp = $filter["reg_exp"]; |
1464 | $inverse = $filter["inverse"]; | |
1465 | if ((!$inverse && preg_match("/$reg_exp/i", $title)) || | |
1466 | ($inverse && !preg_match("/$reg_exp/i", $title))) { | |
1467 | ||
240054f1 AD |
1468 | array_push($matches, array($filter["action"], $filter["action_param"])); |
1469 | } | |
1470 | } | |
1471 | } | |
1472 | ||
1473 | if ($filters["content"]) { | |
1474 | foreach ($filters["content"] as $filter) { | |
c2d9322b AD |
1475 | $reg_exp = $filter["reg_exp"]; |
1476 | $inverse = $filter["inverse"]; | |
1477 | ||
1478 | if ((!$inverse && preg_match("/$reg_exp/i", $content)) || | |
1479 | ($inverse && !preg_match("/$reg_exp/i", $content))) { | |
1480 | ||
240054f1 AD |
1481 | array_push($matches, array($filter["action"], $filter["action_param"])); |
1482 | } | |
1483 | } | |
1484 | } | |
1485 | ||
1486 | if ($filters["both"]) { | |
1487 | foreach ($filters["both"] as $filter) { | |
1488 | $reg_exp = $filter["reg_exp"]; | |
c2d9322b AD |
1489 | $inverse = $filter["inverse"]; |
1490 | ||
1491 | if ($inverse) { | |
d404ae81 | 1492 | if (!preg_match("/$reg_exp/i", $title) && !preg_match("/$reg_exp/i", $content)) { |
c2d9322b AD |
1493 | array_push($matches, array($filter["action"], $filter["action_param"])); |
1494 | } | |
1495 | } else { | |
1496 | if (preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) { | |
1497 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
1498 | } | |
240054f1 AD |
1499 | } |
1500 | } | |
1501 | } | |
1502 | ||
1503 | if ($filters["link"]) { | |
1504 | $reg_exp = $filter["reg_exp"]; | |
1505 | foreach ($filters["link"] as $filter) { | |
1506 | $reg_exp = $filter["reg_exp"]; | |
c2d9322b AD |
1507 | $inverse = $filter["inverse"]; |
1508 | ||
1509 | if ((!$inverse && preg_match("/$reg_exp/i", $link)) || | |
1510 | ($inverse && !preg_match("/$reg_exp/i", $link))) { | |
1511 | ||
240054f1 AD |
1512 | array_push($matches, array($filter["action"], $filter["action_param"])); |
1513 | } | |
1514 | } | |
1515 | } | |
1516 | ||
44d0e774 AD |
1517 | if ($filters["date"]) { |
1518 | $reg_exp = $filter["reg_exp"]; | |
1519 | foreach ($filters["date"] as $filter) { | |
1520 | $date_modifier = $filter["filter_param"]; | |
1521 | $inverse = $filter["inverse"]; | |
1522 | $check_timestamp = strtotime($filter["reg_exp"]); | |
1523 | ||
1524 | # no-op when timestamp doesn't parse to prevent misfires | |
1525 | ||
1526 | if ($check_timestamp) { | |
1527 | $match_ok = false; | |
1528 | ||
1529 | if ($date_modifier == "before" && $timestamp < $check_timestamp || | |
1530 | $date_modifier == "after" && $timestamp > $check_timestamp) { | |
1531 | $match_ok = true; | |
1532 | } | |
1533 | ||
1534 | if ($inverse) $match_ok = !$match_ok; | |
1535 | ||
1536 | if ($match_ok) { | |
1537 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
1538 | } | |
1539 | } | |
1540 | } | |
1541 | } | |
1542 | ||
240054f1 AD |
1543 | return $matches; |
1544 | } | |
1545 | ||
f8382011 AD |
1546 | function find_article_filter($filters, $filter_name) { |
1547 | foreach ($filters as $f) { | |
1548 | if ($f[0] == $filter_name) { | |
1549 | return $f; | |
1550 | }; | |
1551 | } | |
1552 | return false; | |
1553 | } | |
1554 | ||
ff6e357a AD |
1555 | function calculate_article_score($filters) { |
1556 | $score = 0; | |
1557 | ||
1558 | foreach ($filters as $f) { | |
1559 | if ($f[0] == "score") { | |
1560 | $score += $f[1]; | |
1561 | }; | |
1562 | } | |
1563 | return $score; | |
1564 | } | |
1565 | ||
ceb30ba4 AD |
1566 | function assign_article_to_labels($link, $id, $filters, $owner_uid) { |
1567 | foreach ($filters as $f) { | |
1568 | if ($f[0] == "label") { | |
1569 | label_add_article($link, $id, $f[1], $owner_uid); | |
1570 | }; | |
1571 | } | |
1572 | } | |
ff6e357a | 1573 | |
9323147e | 1574 | function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link, |
fb1fb4ab | 1575 | $rtl_content = false, $last_updated = false, $last_error = false) { |
254e0e4b AD |
1576 | |
1577 | if (file_exists($icon_file) && filesize($icon_file) > 0) { | |
023fe037 | 1578 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">"; |
254e0e4b | 1579 | } else { |
023fe037 | 1580 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">"; |
254e0e4b AD |
1581 | } |
1582 | ||
9323147e AD |
1583 | if ($rtl_content) { |
1584 | $rtl_tag = "dir=\"rtl\""; | |
1585 | } else { | |
1586 | $rtl_tag = "dir=\"ltr\""; | |
1587 | } | |
1588 | ||
78d5212c AD |
1589 | $error_notify_msg = ""; |
1590 | ||
fb1fb4ab AD |
1591 | if ($last_error) { |
1592 | $link_title = "Error: $last_error ($last_updated)"; | |
78d5212c | 1593 | $error_notify_msg = "(Error)"; |
ad780e9c | 1594 | } else if ($last_updated) { |
fb1fb4ab AD |
1595 | $link_title = "Updated: $last_updated"; |
1596 | } | |
1597 | ||
7210613a | 1598 | $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" |
c50e2b30 | 1599 | href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>"; |
254e0e4b | 1600 | |
8836613c | 1601 | print "<li id=\"FEEDR-$feed_id\" class=\"$class\">"; |
b619ff15 | 1602 | if (get_pref($link, 'ENABLE_FEED_ICONS')) { |
254e0e4b AD |
1603 | print "$feed_icon"; |
1604 | } | |
1605 | ||
9323147e | 1606 | print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>"; |
254e0e4b AD |
1607 | |
1608 | if ($unread != 0) { | |
d7e83df7 | 1609 | $fctr_class = "class=\"feedCtrHasUnread\""; |
254e0e4b | 1610 | } else { |
d7e83df7 | 1611 | $fctr_class = "class=\"feedCtrNoUnread\""; |
254e0e4b AD |
1612 | } |
1613 | ||
9323147e | 1614 | print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\"> |
254e0e4b | 1615 | (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>"; |
78d5212c AD |
1616 | |
1617 | if (get_pref($link, "EXTENDED_FEEDLIST")) { | |
bdb7369b | 1618 | $total = getFeedArticles($link, $feed_id); |
78d5212c | 1619 | print "<div class=\"feedExtInfo\"> |
bdb7369b | 1620 | <span id=\"FLUPD-$feed_id\">$last_updated ($total total) $error_notify_msg</span></div>"; |
78d5212c AD |
1621 | } |
1622 | ||
254e0e4b AD |
1623 | print "</li>"; |
1624 | ||
1625 | } | |
1626 | ||
406d9489 AD |
1627 | function getmicrotime() { |
1628 | list($usec, $sec) = explode(" ",microtime()); | |
1629 | return ((float)$usec + (float)$sec); | |
1630 | } | |
1631 | ||
f541eb78 | 1632 | function print_radio($id, $default, $true_is, $values, $attributes = "") { |
77e96719 AD |
1633 | foreach ($values as $v) { |
1634 | ||
1635 | if ($v == $default) | |
5da169d9 | 1636 | $sel = "checked"; |
77e96719 | 1637 | else |
5da169d9 AD |
1638 | $sel = ""; |
1639 | ||
f541eb78 | 1640 | if ($v == $true_is) { |
5da169d9 AD |
1641 | $sel .= " value=\"1\""; |
1642 | } else { | |
1643 | $sel .= " value=\"0\""; | |
1644 | } | |
77e96719 | 1645 | |
69654950 AD |
1646 | print "<input class=\"noborder\" |
1647 | type=\"radio\" $sel $attributes name=\"$id\"> $v "; | |
77e96719 AD |
1648 | |
1649 | } | |
1650 | } | |
1651 | ||
ff485f1d AD |
1652 | function initialize_user_prefs($link, $uid) { |
1653 | ||
1654 | $uid = db_escape_string($uid); | |
1655 | ||
1656 | db_query($link, "BEGIN"); | |
1657 | ||
1658 | $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs"); | |
1659 | ||
1660 | $u_result = db_query($link, "SELECT pref_name | |
1661 | FROM ttrss_user_prefs WHERE owner_uid = '$uid'"); | |
1662 | ||
1663 | $active_prefs = array(); | |
1664 | ||
1665 | while ($line = db_fetch_assoc($u_result)) { | |
1666 | array_push($active_prefs, $line["pref_name"]); | |
1667 | } | |
1668 | ||
1669 | while ($line = db_fetch_assoc($result)) { | |
1670 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
1671 | // print "adding " . $line["pref_name"] . "<br>"; | |
1672 | ||
1673 | db_query($link, "INSERT INTO ttrss_user_prefs | |
1674 | (owner_uid,pref_name,value) VALUES | |
1675 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); | |
1676 | ||
1677 | } | |
1678 | } | |
1679 | ||
1680 | db_query($link, "COMMIT"); | |
1681 | ||
1682 | } | |
956c7629 AD |
1683 | |
1684 | function lookup_user_id($link, $user) { | |
1685 | ||
1686 | $result = db_query($link, "SELECT id FROM ttrss_users WHERE | |
1687 | login = '$login'"); | |
1688 | ||
1689 | if (db_num_rows($result) == 1) { | |
1690 | return db_fetch_result($result, 0, "id"); | |
1691 | } else { | |
1692 | return false; | |
1693 | } | |
1694 | } | |
1695 | ||
18664970 AD |
1696 | function http_authenticate_user($link) { |
1697 | ||
4bc64807 AD |
1698 | error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log'); |
1699 | ||
18664970 AD |
1700 | if (!$_SERVER["PHP_AUTH_USER"]) { |
1701 | ||
1702 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"'); | |
1703 | header('HTTP/1.0 401 Unauthorized'); | |
1704 | exit; | |
1705 | ||
1706 | } else { | |
1707 | $auth_result = authenticate_user($link, | |
1708 | $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]); | |
1709 | ||
1710 | if (!$auth_result) { | |
1711 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"'); | |
1712 | header('HTTP/1.0 401 Unauthorized'); | |
1713 | exit; | |
1714 | } | |
1715 | } | |
1716 | ||
1717 | return true; | |
1718 | } | |
1719 | ||
461766f3 | 1720 | function authenticate_user($link, $login, $password, $force_auth = false) { |
c8437f35 | 1721 | |
131b01b3 | 1722 | if (!SINGLE_USER_MODE) { |
c8437f35 | 1723 | |
1a9f4d3c AD |
1724 | $pwd_hash1 = encrypt_password($password); |
1725 | $pwd_hash2 = encrypt_password($password, $login); | |
461766f3 | 1726 | |
66917e70 | 1727 | if (defined('ALLOW_REMOTE_USER_AUTH') && ALLOW_REMOTE_USER_AUTH |
73f5f114 | 1728 | && $_SERVER["REMOTE_USER"] && $login != "admin") { |
66917e70 AD |
1729 | |
1730 | $login = db_escape_string($_SERVER["REMOTE_USER"]); | |
1731 | ||
73f5f114 | 1732 | $query = "SELECT id,login,access_level,pwd_hash |
461766f3 | 1733 | FROM ttrss_users WHERE |
66917e70 AD |
1734 | login = '$login'"; |
1735 | ||
461766f3 | 1736 | } else { |
1a9f4d3c | 1737 | $query = "SELECT id,login,access_level,pwd_hash |
461766f3 | 1738 | FROM ttrss_users WHERE |
1a9f4d3c AD |
1739 | login = '$login' AND (pwd_hash = '$pwd_hash1' OR |
1740 | pwd_hash = '$pwd_hash2')"; | |
461766f3 AD |
1741 | } |
1742 | ||
1743 | $result = db_query($link, $query); | |
131b01b3 AD |
1744 | |
1745 | if (db_num_rows($result) == 1) { | |
1746 | $_SESSION["uid"] = db_fetch_result($result, 0, "id"); | |
1747 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); | |
1748 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); | |
1749 | ||
1750 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
1751 | $_SESSION["uid"]); | |
1752 | ||
1753 | $user_theme = get_user_theme_path($link); | |
1754 | ||
1755 | $_SESSION["theme"] = $user_theme; | |
1756 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
1a9f4d3c | 1757 | $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash"); |
131b01b3 AD |
1758 | |
1759 | initialize_user_prefs($link, $_SESSION["uid"]); | |
1760 | ||
1761 | return true; | |
1762 | } | |
1763 | ||
1764 | return false; | |
503eb349 | 1765 | |
131b01b3 | 1766 | } else { |
503eb349 | 1767 | |
131b01b3 AD |
1768 | $_SESSION["uid"] = 1; |
1769 | $_SESSION["name"] = "admin"; | |
f557cd78 | 1770 | |
0bbba72d AD |
1771 | $user_theme = get_user_theme_path($link); |
1772 | ||
1773 | $_SESSION["theme"] = $user_theme; | |
1774 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
1775 | ||
1776 | initialize_user_prefs($link, $_SESSION["uid"]); | |
1777 | ||
c8437f35 AD |
1778 | return true; |
1779 | } | |
c8437f35 AD |
1780 | } |
1781 | ||
e6cb77a0 AD |
1782 | function make_password($length = 8) { |
1783 | ||
1784 | $password = ""; | |
798f722b AD |
1785 | $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ"; |
1786 | ||
1787 | $i = 0; | |
e6cb77a0 AD |
1788 | |
1789 | while ($i < $length) { | |
1790 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
1791 | ||
1792 | if (!strstr($password, $char)) { | |
1793 | $password .= $char; | |
1794 | $i++; | |
1795 | } | |
1796 | } | |
1797 | return $password; | |
1798 | } | |
1799 | ||
1800 | // this is called after user is created to initialize default feeds, labels | |
1801 | // or whatever else | |
1802 | ||
1803 | // user preferences are checked on every login, not here | |
1804 | ||
1805 | function initialize_user($link, $uid) { | |
1806 | ||
e2549229 | 1807 | /* db_query($link, "INSERT INTO ttrss_labels2 (owner_uid, caption) |
69d47936 AD |
1808 | VALUES ('$uid', 'All Articles')"); |
1809 | ||
1810 | db_query($link, "INSERT INTO ttrss_filters | |
1811 | (owner_uid, feed_id, filter_type, reg_exp, enabled, | |
1812 | action_id, action_param, filter_param) | |
e2549229 | 1813 | VALUES ('$uid', NULL, 1, '.', true, 7, 'All Articles', 'before')"); */ |
69d47936 | 1814 | |
e6cb77a0 | 1815 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) |
74bff337 | 1816 | values ('$uid', 'Tiny Tiny RSS: New Releases', |
b6d486a3 | 1817 | 'http://tt-rss.org/releases.rss')"); |
3b0feb9b | 1818 | |
cd2cd415 AD |
1819 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) |
1820 | values ('$uid', 'Tiny Tiny RSS: Forum', | |
b6d486a3 | 1821 | 'http://tt-rss.org/forum/rss.php')"); |
3b0feb9b | 1822 | } |
e6cb77a0 | 1823 | |
b8aa49bc | 1824 | function logout_user() { |
5ccc1cf5 AD |
1825 | session_destroy(); |
1826 | if (isset($_COOKIE[session_name()])) { | |
1827 | setcookie(session_name(), '', time()-42000, '/'); | |
1828 | } | |
b8aa49bc AD |
1829 | } |
1830 | ||
75836f33 | 1831 | function get_script_urlpath() { |
87a79fa4 | 1832 | return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]); |
75836f33 AD |
1833 | } |
1834 | ||
916f788a | 1835 | function validate_session($link) { |
741edab2 AD |
1836 | if (SINGLE_USER_MODE) { |
1837 | return true; | |
1838 | } | |
1839 | ||
a2e9b457 | 1840 | if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) { |
916f788a AD |
1841 | if ($_SESSION["ip_address"]) { |
1842 | if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) { | |
7f0acba7 | 1843 | $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)"; |
916f788a AD |
1844 | return false; |
1845 | } | |
1846 | } | |
1847 | } | |
d620cfe7 | 1848 | |
e6684130 AD |
1849 | if ($_SESSION["uid"]) { |
1850 | ||
1851 | $result = db_query($link, | |
1852 | "SELECT pwd_hash FROM ttrss_users WHERE id = '".$_SESSION["uid"]."'"); | |
1853 | ||
1854 | $pwd_hash = db_fetch_result($result, 0, "pwd_hash"); | |
1855 | ||
1856 | if ($pwd_hash != $_SESSION["pwd_hash"]) { | |
1857 | return false; | |
1858 | } | |
1859 | } | |
1860 | ||
a885f0ec | 1861 | /* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) { |
d620cfe7 | 1862 | |
8e849206 | 1863 | //print_r($_SESSION); |
d620cfe7 AD |
1864 | |
1865 | if (time() > $_SESSION["cookie_lifetime"]) { | |
1866 | return false; | |
1867 | } | |
a885f0ec AD |
1868 | } */ |
1869 | ||
916f788a AD |
1870 | return true; |
1871 | } | |
1872 | ||
793185a9 | 1873 | function login_sequence($link, $mobile = false) { |
b8aa49bc | 1874 | if (!SINGLE_USER_MODE) { |
75836f33 | 1875 | |
461766f3 AD |
1876 | if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) { |
1877 | $swu = db_escape_string($_REQUEST["swu"]); | |
1878 | if ($swu) { | |
1879 | $_SESSION["prefs_cache"] = false; | |
1880 | return authenticate_user($link, $swu, null, true); | |
1881 | } | |
1882 | } | |
1883 | ||
7f0acba7 | 1884 | $login_action = $_POST["login_action"]; |
a885f0ec | 1885 | |
01a87dff | 1886 | # try to authenticate user if called from login form |
7f0acba7 | 1887 | if ($login_action == "do_login") { |
01a87dff AD |
1888 | $login = $_POST["login"]; |
1889 | $password = $_POST["password"]; | |
d620cfe7 | 1890 | $remember_me = $_POST["remember_me"]; |
f557cd78 | 1891 | |
01a87dff AD |
1892 | if (authenticate_user($link, $login, $password)) { |
1893 | $_POST["password"] = ""; | |
d620cfe7 | 1894 | |
f8c612d4 | 1895 | $_SESSION["language"] = $_POST["language"]; |
a598370d | 1896 | $_SESSION["bw_limit"] = !!$_POST["bw_limit"]; |
f8c612d4 | 1897 | |
d620cfe7 AD |
1898 | header("Location: " . $_SERVER["REQUEST_URI"]); |
1899 | exit; | |
1900 | ||
01a87dff | 1901 | return; |
7f0acba7 AD |
1902 | } else { |
1903 | $_SESSION["login_error_msg"] = "Incorrect username or password"; | |
01a87dff AD |
1904 | } |
1905 | } | |
1906 | ||
7f0acba7 | 1907 | if (!$_SESSION["uid"] || !validate_session($link)) { |
793185a9 | 1908 | render_login_form($link, $mobile); |
01a87dff | 1909 | exit; |
d3687e7a AD |
1910 | } else { |
1911 | /* bump login timestamp */ | |
1912 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
1913 | $_SESSION["uid"]); | |
019bd5a9 | 1914 | |
d54780bc | 1915 | if ($_SESSION["language"] && SESSION_COOKIE_LIFETIME > 0) { |
019bd5a9 AD |
1916 | setcookie("ttrss_lang", $_SESSION["language"], |
1917 | time() + SESSION_COOKIE_LIFETIME); | |
1918 | } | |
4fdb0476 AD |
1919 | |
1920 | /* bump counters stamp since we're getting reloaded anyway */ | |
1921 | ||
1922 | $_SESSION["get_all_counters_stamp"] = time(); | |
b8aa49bc | 1923 | } |
d620cfe7 | 1924 | |
b8aa49bc | 1925 | } else { |
0bbba72d | 1926 | return authenticate_user($link, "admin", null); |
b8aa49bc AD |
1927 | } |
1928 | } | |
3547842a AD |
1929 | |
1930 | function truncate_string($str, $max_len) { | |
12db369c | 1931 | if (mb_strlen($str, "utf-8") > $max_len - 3) { |
66a251f9 | 1932 | return mb_substr($str, 0, $max_len, "utf-8") . "…"; |
3547842a AD |
1933 | } else { |
1934 | return $str; | |
1935 | } | |
1936 | } | |
54a60e1a AD |
1937 | |
1938 | function get_user_theme_path($link) { | |
798f722b AD |
1939 | $result = db_query($link, "SELECT theme_path |
1940 | FROM | |
1941 | ttrss_themes,ttrss_users | |
1942 | WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]); | |
54a60e1a AD |
1943 | if (db_num_rows($result) != 0) { |
1944 | return db_fetch_result($result, 0, "theme_path"); | |
1945 | } else { | |
1946 | return null; | |
1947 | } | |
1948 | } | |
be773442 AD |
1949 | |
1950 | function smart_date_time($timestamp) { | |
1951 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1952 | return date("G:i", $timestamp); | |
f26450f1 | 1953 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
1954 | return date("M d, G:i", $timestamp); |
1955 | } else { | |
7d7e0509 | 1956 | return date("Y/m/d, G:i", $timestamp); |
be773442 AD |
1957 | } |
1958 | } | |
1959 | ||
1960 | function smart_date($timestamp) { | |
1961 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1962 | return "Today"; | |
f26450f1 | 1963 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
1964 | return date("D m", $timestamp); |
1965 | } else { | |
b02111c2 | 1966 | return date("Y/m/d", $timestamp); |
be773442 AD |
1967 | } |
1968 | } | |
a654a595 AD |
1969 | |
1970 | function sql_bool_to_string($s) { | |
1971 | if ($s == "t" || $s == "1") { | |
1972 | return "true"; | |
1973 | } else { | |
1974 | return "false"; | |
1975 | } | |
1976 | } | |
e3c99f3b AD |
1977 | |
1978 | function sql_bool_to_bool($s) { | |
1979 | if ($s == "t" || $s == "1") { | |
1980 | return true; | |
1981 | } else { | |
1982 | return false; | |
1983 | } | |
1984 | } | |
0ea4fb50 | 1985 | |
e3c99f3b | 1986 | |
0ea4fb50 AD |
1987 | function toggleEvenOdd($a) { |
1988 | if ($a == "even") | |
1989 | return "odd"; | |
1990 | else | |
1991 | return "even"; | |
1992 | } | |
6043fb7e AD |
1993 | |
1994 | function sanity_check($link) { | |
9cbca41f | 1995 | |
aec3ce39 AD |
1996 | error_reporting(0); |
1997 | ||
6043fb7e AD |
1998 | $error_code = 0; |
1999 | $result = db_query($link, "SELECT schema_version FROM ttrss_version"); | |
2000 | $schema_version = db_fetch_result($result, 0, "schema_version"); | |
2001 | ||
2002 | if ($schema_version != SCHEMA_VERSION) { | |
2003 | $error_code = 5; | |
2004 | } | |
2005 | ||
aec3ce39 AD |
2006 | if (DB_TYPE == "mysql") { |
2007 | $result = db_query($link, "SELECT true", false); | |
2008 | if (db_num_rows($result) != 1) { | |
2009 | $error_code = 10; | |
2010 | } | |
2011 | } | |
2012 | ||
f29ba148 AD |
2013 | if (db_escape_string("testTEST") != "testTEST") { |
2014 | $error_code = 12; | |
2015 | } | |
2016 | ||
aec3ce39 AD |
2017 | error_reporting (DEFAULT_ERROR_LEVEL); |
2018 | ||
6043fb7e | 2019 | if ($error_code != 0) { |
aec3ce39 | 2020 | print_error_xml($error_code); |
6043fb7e AD |
2021 | return false; |
2022 | } else { | |
2023 | return true; | |
4220d6b0 | 2024 | } |
6043fb7e AD |
2025 | } |
2026 | ||
27981ca3 | 2027 | function file_is_locked($filename) { |
31a6d42d AD |
2028 | if (function_exists('flock')) { |
2029 | error_reporting(0); | |
cfa43e02 | 2030 | $fp = fopen(LOCK_DIRECTORY . "/$filename", "r"); |
31a6d42d AD |
2031 | error_reporting(DEFAULT_ERROR_LEVEL); |
2032 | if ($fp) { | |
2033 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
2034 | flock($fp, LOCK_UN); | |
2035 | fclose($fp); | |
2036 | return false; | |
2037 | } | |
27981ca3 | 2038 | fclose($fp); |
31a6d42d | 2039 | return true; |
e89aed7b AD |
2040 | } else { |
2041 | return false; | |
27981ca3 | 2042 | } |
27981ca3 | 2043 | } |
c1fb4a5e | 2044 | return true; // consider the file always locked and skip the test |
27981ca3 AD |
2045 | } |
2046 | ||
fcb4c0c9 | 2047 | function make_lockfile($filename) { |
cfa43e02 | 2048 | $fp = fopen(LOCK_DIRECTORY . "/$filename", "w"); |
fcb4c0c9 AD |
2049 | |
2050 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
2051 | return $fp; | |
2052 | } else { | |
2053 | return false; | |
2054 | } | |
2055 | } | |
2056 | ||
bf7fcde8 | 2057 | function make_stampfile($filename) { |
cfa43e02 | 2058 | $fp = fopen(LOCK_DIRECTORY . "/$filename", "w"); |
bf7fcde8 | 2059 | |
8e00ae9b | 2060 | if (flock($fp, LOCK_EX | LOCK_NB)) { |
bf7fcde8 | 2061 | fwrite($fp, time() . "\n"); |
8e00ae9b | 2062 | flock($fp, LOCK_UN); |
bf7fcde8 AD |
2063 | fclose($fp); |
2064 | return true; | |
2065 | } else { | |
2066 | return false; | |
2067 | } | |
2068 | } | |
2069 | ||
8e00ae9b AD |
2070 | function read_stampfile($filename) { |
2071 | ||
2072 | error_reporting(0); | |
cfa43e02 | 2073 | $fp = fopen(LOCK_DIRECTORY . "/$filename", "r"); |
8e00ae9b AD |
2074 | error_reporting (DEFAULT_ERROR_LEVEL); |
2075 | ||
e89aed7b AD |
2076 | if ($fp) { |
2077 | if (flock($fp, LOCK_EX)) { | |
2078 | $stamp = fgets($fp); | |
2079 | flock($fp, LOCK_UN); | |
2080 | fclose($fp); | |
2081 | return $stamp; | |
2082 | } else { | |
2083 | return false; | |
2084 | } | |
8e00ae9b AD |
2085 | } else { |
2086 | return false; | |
2087 | } | |
2088 | } | |
bf7fcde8 | 2089 | |
894ebcf5 AD |
2090 | function sql_random_function() { |
2091 | if (DB_TYPE == "mysql") { | |
2092 | return "RAND()"; | |
2093 | } else { | |
2094 | return "RANDOM()"; | |
2095 | } | |
2096 | } | |
2097 | ||
23aa0d16 | 2098 | function catchup_feed($link, $feed, $cat_view) { |
88040f57 AD |
2099 | |
2100 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
23aa0d16 AD |
2101 | |
2102 | if ($cat_view) { | |
2103 | ||
2104 | if ($feed > 0) { | |
2105 | $cat_qpart = "cat_id = '$feed'"; | |
2106 | } else { | |
2107 | $cat_qpart = "cat_id IS NULL"; | |
2108 | } | |
2109 | ||
2110 | $tmp_result = db_query($link, "SELECT id | |
2111 | FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " . | |
2112 | $_SESSION["uid"]); | |
2113 | ||
2114 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
2115 | ||
2116 | $tmp_feed = $tmp_line["id"]; | |
2117 | ||
2118 | db_query($link, "UPDATE ttrss_user_entries | |
2119 | SET unread = false,last_read = NOW() | |
2120 | WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]); | |
2121 | } | |
2122 | ||
2123 | } else if ($feed > 0) { | |
2124 | ||
2125 | $tmp_result = db_query($link, "SELECT id | |
2126 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
2127 | ORDER BY cat_id,title"); | |
2128 | ||
2129 | $parent_ids = array(); | |
2130 | ||
2131 | if (db_num_rows($tmp_result) > 0) { | |
2132 | while ($p = db_fetch_assoc($tmp_result)) { | |
2133 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
2134 | } | |
2135 | ||
2136 | $children_qpart = implode(" OR ", $parent_ids); | |
2137 | ||
2138 | db_query($link, "UPDATE ttrss_user_entries | |
2139 | SET unread = false,last_read = NOW() | |
2140 | WHERE (feed_id = '$feed' OR $children_qpart) | |
2141 | AND owner_uid = " . $_SESSION["uid"]); | |
2142 | ||
2143 | } else { | |
2144 | db_query($link, "UPDATE ttrss_user_entries | |
2145 | SET unread = false,last_read = NOW() | |
2146 | WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
2147 | } | |
2148 | ||
2149 | } else if ($feed < 0 && $feed > -10) { // special, like starred | |
2150 | ||
2151 | if ($feed == -1) { | |
2152 | db_query($link, "UPDATE ttrss_user_entries | |
2153 | SET unread = false,last_read = NOW() | |
2154 | WHERE marked = true AND owner_uid = ".$_SESSION["uid"]); | |
2155 | } | |
e4f4b46f AD |
2156 | |
2157 | if ($feed == -2) { | |
2158 | db_query($link, "UPDATE ttrss_user_entries | |
2159 | SET unread = false,last_read = NOW() | |
2160 | WHERE published = true AND owner_uid = ".$_SESSION["uid"]); | |
2161 | } | |
2162 | ||
2d24f032 AD |
2163 | if ($feed == -3) { |
2164 | ||
c1d7e6c3 AD |
2165 | $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE"); |
2166 | ||
2d24f032 | 2167 | if (DB_TYPE == "pgsql") { |
7608b38a | 2168 | $match_part = "updated > NOW() - INTERVAL '$intl hour' "; |
2d24f032 | 2169 | } else { |
7608b38a | 2170 | $match_part = "updated > DATE_SUB(NOW(), |
c1d7e6c3 | 2171 | INTERVAL $intl HOUR) "; |
2d24f032 AD |
2172 | } |
2173 | ||
1f3335dc AD |
2174 | $result = db_query($link, "SELECT id FROM ttrss_entries, |
2175 | ttrss_user_entries WHERE $match_part AND | |
2176 | unread = true AND | |
2177 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2178 | owner_uid = ".$_SESSION["uid"]); | |
2179 | ||
2180 | $affected_ids = array(); | |
2181 | ||
2182 | while ($line = db_fetch_assoc($result)) { | |
2183 | array_push($affected_ids, $line["id"]); | |
2184 | } | |
2185 | ||
2186 | catchupArticlesById($link, $affected_ids, 0); | |
2d24f032 AD |
2187 | } |
2188 | ||
3584cb11 AD |
2189 | if ($feed == -4) { |
2190 | db_query($link, "UPDATE ttrss_user_entries | |
2191 | SET unread = false,last_read = NOW() | |
2192 | WHERE owner_uid = ".$_SESSION["uid"]); | |
2193 | } | |
2194 | ||
23aa0d16 AD |
2195 | } else if ($feed < -10) { // label |
2196 | ||
23aa0d16 AD |
2197 | $label_id = -$feed - 11; |
2198 | ||
933ba4ee | 2199 | db_query($link, "UPDATE ttrss_user_entries, ttrss_user_labels2 |
0112162d | 2200 | SET unread = false WHERE label_id = '$label_id' AND unread = true |
933ba4ee | 2201 | AND owner_uid = '".$_SESSION["uid"]."' AND ref_id = article_id"); |
23aa0d16 | 2202 | |
23aa0d16 | 2203 | } |
ad0056a8 AD |
2204 | |
2205 | ccache_update($link, $feed, $_SESSION["uid"], $cat_view); | |
2206 | ||
23aa0d16 AD |
2207 | } else { // tag |
2208 | db_query($link, "BEGIN"); | |
2209 | ||
2210 | $tag_name = db_escape_string($feed); | |
2211 | ||
2212 | $result = db_query($link, "SELECT post_int_id FROM ttrss_tags | |
2213 | WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]); | |
2214 | ||
2215 | while ($line = db_fetch_assoc($result)) { | |
2216 | db_query($link, "UPDATE ttrss_user_entries SET | |
2217 | unread = false, last_read = NOW() | |
2218 | WHERE int_id = " . $line["post_int_id"]); | |
2219 | } | |
2220 | db_query($link, "COMMIT"); | |
2221 | } | |
2222 | } | |
2223 | ||
35bf080c | 2224 | function update_generic_feed($link, $feed, $cat_view, $force_update = false) { |
23aa0d16 AD |
2225 | if ($cat_view) { |
2226 | ||
2227 | if ($feed > 0) { | |
2228 | $cat_qpart = "cat_id = '$feed'"; | |
2229 | } else { | |
2230 | $cat_qpart = "cat_id IS NULL"; | |
2231 | } | |
2232 | ||
571dad82 | 2233 | $tmp_result = db_query($link, "SELECT id,feed_url FROM ttrss_feeds |
23aa0d16 AD |
2234 | WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]); |
2235 | ||
2236 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
2237 | $feed_url = $tmp_line["feed_url"]; | |
571dad82 | 2238 | $feed_id = $tmp_line["id"]; |
35bf080c | 2239 | update_rss_feed($link, $feed_url, $feed_id, $force_update); |
23aa0d16 AD |
2240 | } |
2241 | ||
2242 | } else { | |
2243 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
2244 | WHERE id = '$feed'"); | |
2245 | $feed_url = db_fetch_result($tmp_result, 0, "feed_url"); | |
35bf080c | 2246 | update_rss_feed($link, $feed_url, $feed, $force_update); |
23aa0d16 AD |
2247 | } |
2248 | } | |
a9cb1f83 | 2249 | |
4ffa126e | 2250 | function getAllCounters($link, $omode = "flc", $active_feed = false) { |
cf4d339c | 2251 | |
e33fe293 | 2252 | if (!$omode) $omode = "flc"; |
d9e4bba0 | 2253 | |
e33fe293 | 2254 | getGlobalCounters($link); |
d9e4bba0 | 2255 | |
e33fe293 AD |
2256 | if (strchr($omode, "l")) getLabelCounters($link); |
2257 | if (strchr($omode, "f")) getFeedCounters($link, SMART_RPC_COUNTERS, $active_feed); | |
2258 | if (strchr($omode, "t")) getTagCounters($link); | |
2259 | if (strchr($omode, "c")) { | |
2260 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
2261 | getCategoryCounters($link); | |
cf4d339c | 2262 | } |
e33fe293 | 2263 | } |
a9cb1f83 AD |
2264 | } |
2265 | ||
2266 | function getCategoryCounters($link) { | |
bba7c4bf AD |
2267 | # two special categories are -1 and -2 (all virtuals; all labels) |
2268 | ||
b2531a28 | 2269 | /* $ctr = getCategoryUnread($link, -1); |
bba7c4bf | 2270 | |
b2531a28 | 2271 | print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>"; */ |
bba7c4bf | 2272 | |
b2531a28 | 2273 | $ctr = getCategoryUnread($link, -2); |
bba7c4bf AD |
2274 | |
2275 | print "<counter type=\"category\" id=\"-2\" counter=\"$ctr\"/>"; | |
2276 | ||
14073c0a AD |
2277 | $age_qpart = getMaxAgeSubquery(); |
2278 | ||
31375163 AD |
2279 | $result = db_query($link, "SELECT id AS cat_id, value AS unread |
2280 | FROM ttrss_feed_categories, ttrss_cat_counters_cache | |
2281 | WHERE ttrss_cat_counters_cache.feed_id = id AND | |
2282 | ttrss_feed_categories.owner_uid = " . $_SESSION["uid"]); | |
a9cb1f83 AD |
2283 | |
2284 | while ($line = db_fetch_assoc($result)) { | |
2285 | $line["cat_id"] = sprintf("%d", $line["cat_id"]); | |
8a4c759e | 2286 | |
a9cb1f83 AD |
2287 | print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"". |
2288 | $line["unread"]."\"/>"; | |
2289 | } | |
d232a40f AD |
2290 | |
2291 | /* Special case: NULL category doesn't actually exist in the DB */ | |
2292 | ||
2293 | print "<counter type=\"category\" id=\"0\" counter=\"". | |
2294 | ccache_find($link, 0, $_SESSION["uid"], true)."\"/>"; | |
2295 | ||
a9cb1f83 AD |
2296 | } |
2297 | ||
b6d486a3 AD |
2298 | function getCategoryUnread($link, $cat, $owner_uid = false) { |
2299 | ||
2300 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
f295c368 | 2301 | |
bba7c4bf | 2302 | if ($cat >= 0) { |
18664970 | 2303 | |
bba7c4bf AD |
2304 | if ($cat != 0) { |
2305 | $cat_query = "cat_id = '$cat'"; | |
2306 | } else { | |
2307 | $cat_query = "cat_id IS NULL"; | |
2308 | } | |
14073c0a AD |
2309 | |
2310 | $age_qpart = getMaxAgeSubquery(); | |
2311 | ||
bba7c4bf AD |
2312 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query |
2313 | AND hidden = false | |
b6d486a3 | 2314 | AND owner_uid = " . $owner_uid); |
bba7c4bf AD |
2315 | |
2316 | $cat_feeds = array(); | |
2317 | while ($line = db_fetch_assoc($result)) { | |
2318 | array_push($cat_feeds, "feed_id = " . $line["id"]); | |
2319 | } | |
2320 | ||
2321 | if (count($cat_feeds) == 0) return 0; | |
2322 | ||
2323 | $match_part = implode(" OR ", $cat_feeds); | |
2324 | ||
2325 | $result = db_query($link, "SELECT COUNT(int_id) AS unread | |
14073c0a AD |
2326 | FROM ttrss_user_entries,ttrss_entries |
2327 | WHERE unread = true AND ($match_part) AND id = ref_id | |
b6d486a3 | 2328 | AND $age_qpart AND owner_uid = " . $owner_uid); |
bba7c4bf AD |
2329 | |
2330 | $unread = 0; | |
2331 | ||
2332 | # this needs to be rewritten | |
2333 | while ($line = db_fetch_assoc($result)) { | |
2334 | $unread += $line["unread"]; | |
2335 | } | |
2336 | ||
2337 | return $unread; | |
2338 | } else if ($cat == -1) { | |
2d24f032 | 2339 | return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3); |
bba7c4bf | 2340 | } else if ($cat == -2) { |
f295c368 | 2341 | |
b2531a28 AD |
2342 | $result = db_query($link, " |
2343 | SELECT COUNT(unread) AS unread FROM | |
2344 | ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds | |
2345 | WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND | |
2346 | ttrss_labels2.owner_uid = '$owner_uid' | |
2347 | AND unread = true AND hidden = false AND feed_id = ttrss_feeds.id | |
2348 | AND ttrss_user_entries.owner_uid = '$owner_uid'"); | |
ceb30ba4 | 2349 | |
b2531a28 | 2350 | $unread = db_fetch_result($result, 0, "unread"); |
f295c368 | 2351 | |
b2531a28 | 2352 | return $unread; |
f295c368 | 2353 | |
ceb30ba4 | 2354 | } |
f295c368 AD |
2355 | } |
2356 | ||
14073c0a AD |
2357 | function getMaxAgeSubquery($days = COUNTERS_MAX_AGE) { |
2358 | if (DB_TYPE == "pgsql") { | |
2359 | return "ttrss_entries.date_entered > | |
2360 | NOW() - INTERVAL '$days days'"; | |
2361 | } else { | |
2362 | return "ttrss_entries.date_entered > | |
2363 | DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
2364 | } | |
2365 | } | |
2366 | ||
f295c368 | 2367 | function getFeedUnread($link, $feed, $is_cat = false) { |
2627f2d0 | 2368 | return getFeedArticles($link, $feed, $is_cat, true, $_SESSION["uid"]); |
bdb7369b AD |
2369 | } |
2370 | ||
ceb30ba4 AD |
2371 | function getLabelUnread($link, $label_id, $owner_uid = false) { |
2372 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
2373 | ||
2374 | $result = db_query($link, " | |
0112162d | 2375 | SELECT COUNT(unread) AS unread FROM |
b0f24af1 AD |
2376 | ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds |
2377 | WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND | |
2378 | ttrss_labels2.owner_uid = '$owner_uid' AND ttrss_labels2.id = '$label_id' | |
2379 | AND unread = true AND hidden = false AND feed_id = ttrss_feeds.id | |
933ba4ee | 2380 | AND ttrss_user_entries.owner_uid = '$owner_uid'"); |
ceb30ba4 AD |
2381 | |
2382 | if (db_num_rows($result) != 0) { | |
2383 | return db_fetch_result($result, 0, "unread"); | |
2384 | } else { | |
2385 | return 0; | |
2386 | } | |
2387 | } | |
2388 | ||
2627f2d0 AD |
2389 | function getFeedArticles($link, $feed, $is_cat = false, $unread_only = false, |
2390 | $owner_uid = false) { | |
2391 | ||
a9cb1f83 | 2392 | $n_feed = sprintf("%d", $feed); |
f295c368 | 2393 | |
2627f2d0 AD |
2394 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; |
2395 | ||
bdb7369b AD |
2396 | if ($unread_only) { |
2397 | $unread_qpart = "unread = true"; | |
2398 | } else { | |
2399 | $unread_qpart = "true"; | |
2400 | } | |
2401 | ||
14073c0a AD |
2402 | $age_qpart = getMaxAgeSubquery(); |
2403 | ||
f295c368 | 2404 | if ($is_cat) { |
b6d486a3 | 2405 | return getCategoryUnread($link, $n_feed, $owner_uid); |
f295c368 | 2406 | } else if ($n_feed == -1) { |
a9cb1f83 | 2407 | $match_part = "marked = true"; |
e4f4b46f AD |
2408 | } else if ($n_feed == -2) { |
2409 | $match_part = "published = true"; | |
2d24f032 AD |
2410 | } else if ($n_feed == -3) { |
2411 | $match_part = "unread = true"; | |
2412 | ||
b71e188e | 2413 | $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid); |
c1d7e6c3 | 2414 | |
2d24f032 | 2415 | if (DB_TYPE == "pgsql") { |
7608b38a | 2416 | $match_part .= " AND updated > NOW() - INTERVAL '$intl hour' "; |
2d24f032 | 2417 | } else { |
7608b38a | 2418 | $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) "; |
2d24f032 | 2419 | } |
b2531a28 AD |
2420 | } else if ($n_feed == -4) { |
2421 | $match_part = "true"; | |
4919fb42 | 2422 | } else if ($n_feed > 0) { |
831ff047 | 2423 | |
e8b8485f AD |
2424 | $result = db_query($link, "SELECT id FROM ttrss_feeds |
2425 | WHERE parent_feed = '$n_feed' | |
318260cc | 2426 | AND hidden = false |
2627f2d0 | 2427 | AND owner_uid = " . $owner_uid); |
831ff047 AD |
2428 | |
2429 | if (db_num_rows($result) > 0) { | |
4919fb42 | 2430 | |
831ff047 AD |
2431 | $linked_feeds = array(); |
2432 | while ($line = db_fetch_assoc($result)) { | |
2433 | array_push($linked_feeds, "feed_id = " . $line["id"]); | |
2434 | } | |
e8b8485f AD |
2435 | |
2436 | array_push($linked_feeds, "feed_id = $n_feed"); | |
831ff047 AD |
2437 | |
2438 | $match_part = implode(" OR ", $linked_feeds); | |
2439 | ||
4919fb42 | 2440 | $result = db_query($link, "SELECT COUNT(int_id) AS unread |
14073c0a | 2441 | FROM ttrss_user_entries,ttrss_entries |
bdb7369b | 2442 | WHERE $unread_qpart AND |
14073c0a AD |
2443 | ttrss_user_entries.ref_id = ttrss_entries.id AND |
2444 | $age_qpart AND | |
2445 | ($match_part) AND | |
2627f2d0 | 2446 | owner_uid = " . $owner_uid); |
4919fb42 AD |
2447 | |
2448 | $unread = 0; | |
2449 | ||
2450 | # this needs to be rewritten | |
2451 | while ($line = db_fetch_assoc($result)) { | |
2452 | $unread += $line["unread"]; | |
2453 | } | |
2454 | ||
2455 | return $unread; | |
2456 | ||
831ff047 AD |
2457 | } else { |
2458 | $match_part = "feed_id = '$n_feed'"; | |
2459 | } | |
a9cb1f83 | 2460 | } else if ($feed < -10) { |
318260cc | 2461 | |
a9cb1f83 AD |
2462 | $label_id = -$feed - 11; |
2463 | ||
ceb30ba4 | 2464 | return getLabelUnread($link, $label_id, $owner_uid); |
a9cb1f83 | 2465 | |
a9cb1f83 AD |
2466 | } |
2467 | ||
2468 | if ($match_part) { | |
2469 | ||
2470 | $result = db_query($link, "SELECT count(int_id) AS unread | |
88040f57 AD |
2471 | FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE |
2472 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
2473 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
cfb02131 | 2474 | ttrss_feeds.hidden = false AND |
14073c0a | 2475 | $age_qpart AND |
2627f2d0 | 2476 | $unread_qpart AND ($match_part) AND ttrss_user_entries.owner_uid = " . $owner_uid); |
a9cb1f83 AD |
2477 | |
2478 | } else { | |
2479 | ||
2480 | $result = db_query($link, "SELECT COUNT(post_int_id) AS unread | |
14073c0a | 2481 | FROM ttrss_tags,ttrss_user_entries,ttrss_entries |
828f22b7 | 2482 | WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id |
bdb7369b | 2483 | AND $unread_qpart AND $age_qpart AND |
2627f2d0 | 2484 | ttrss_tags.owner_uid = " . $owner_uid); |
a9cb1f83 AD |
2485 | } |
2486 | ||
2487 | $unread = db_fetch_result($result, 0, "unread"); | |
cfb02131 | 2488 | |
a9cb1f83 AD |
2489 | return $unread; |
2490 | } | |
2491 | ||
f3acc32e AD |
2492 | function getGlobalUnread($link, $user_id = false) { |
2493 | ||
2494 | if (!$user_id) { | |
2495 | $user_id = $_SESSION["uid"]; | |
2496 | } | |
2497 | ||
8a4c759e AD |
2498 | $result = db_query($link, "SELECT SUM(value) AS c_id FROM ttrss_counters_cache |
2499 | WHERE owner_uid = '$user_id' AND feed_id > 0"); | |
2500 | ||
2501 | $c_id = db_fetch_result($result, 0, "c_id"); | |
2502 | ||
a9cb1f83 AD |
2503 | return $c_id; |
2504 | } | |
2505 | ||
2506 | function getGlobalCounters($link, $global_unread = -1) { | |
2507 | if ($global_unread == -1) { | |
2508 | $global_unread = getGlobalUnread($link); | |
2509 | } | |
7bf7e4d3 AD |
2510 | print "<counter type=\"global\" id='global-unread' |
2511 | counter='$global_unread'/>"; | |
2512 | ||
2513 | $result = db_query($link, "SELECT COUNT(id) AS fn FROM | |
2514 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
2515 | ||
2516 | $subscribed_feeds = db_fetch_result($result, 0, "fn"); | |
2517 | ||
2518 | print "<counter type=\"global\" id='subscribed-feeds' | |
2519 | counter='$subscribed_feeds'/>"; | |
2520 | ||
a9cb1f83 AD |
2521 | } |
2522 | ||
2523 | function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
2524 | ||
2525 | if ($smart_mode) { | |
2526 | if (!$_SESSION["tctr_last_value"]) { | |
2527 | $_SESSION["tctr_last_value"] = array(); | |
2528 | } | |
2529 | } | |
2530 | ||
2531 | $old_counters = $_SESSION["tctr_last_value"]; | |
2532 | ||
2533 | $tctrs_modified = false; | |
2534 | ||
14073c0a AD |
2535 | $age_qpart = getMaxAgeSubquery(); |
2536 | ||
a9cb1f83 | 2537 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) |
14073c0a AD |
2538 | FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id |
2539 | AND ref_id = id AND $age_qpart | |
a9cb1f83 | 2540 | AND unread = true)) AS count FROM ttrss_tags |
ef1ac7c7 AD |
2541 | WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name |
2542 | ORDER BY count DESC LIMIT 55"); | |
a9cb1f83 AD |
2543 | |
2544 | $tags = array(); | |
2545 | ||
2546 | while ($line = db_fetch_assoc($result)) { | |
2547 | $tags[$line["tag_name"]] += $line["count"]; | |
2548 | } | |
2549 | ||
2550 | foreach (array_keys($tags) as $tag) { | |
2551 | $unread = $tags[$tag]; | |
2552 | ||
2553 | $tag = htmlspecialchars($tag); | |
2554 | ||
2555 | if (!$smart_mode || $old_counters[$tag] != $unread) { | |
2556 | $old_counters[$tag] = $unread; | |
2557 | $tctrs_modified = true; | |
2558 | print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>"; | |
2559 | } | |
2560 | ||
2561 | } | |
2562 | ||
2563 | if ($smart_mode && $tctrs_modified) { | |
2564 | $_SESSION["tctr_last_value"] = $old_counters; | |
2565 | } | |
2566 | ||
2567 | } | |
2568 | ||
ef393de7 | 2569 | function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) { |
a9cb1f83 | 2570 | |
14073c0a AD |
2571 | $age_qpart = getMaxAgeSubquery(); |
2572 | ||
a9cb1f83 AD |
2573 | if ($smart_mode) { |
2574 | if (!$_SESSION["lctr_last_value"]) { | |
2575 | $_SESSION["lctr_last_value"] = array(); | |
2576 | } | |
2577 | } | |
2578 | ||
ef393de7 | 2579 | $ret_arr = array(); |
bdb7369b | 2580 | |
b2531a28 | 2581 | for ($i = -1; $i >= -4; $i--) { |
bdb7369b | 2582 | |
ceb30ba4 AD |
2583 | $count = getFeedUnread($link, $i); |
2584 | ||
2585 | if (!$ret_mode) { | |
2586 | ||
2587 | if (get_pref($link, 'EXTENDED_FEEDLIST')) { | |
2588 | $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $i) . " total)\""; | |
2589 | } else { | |
2590 | $xmsg_part = ""; | |
2591 | } | |
bdb7369b | 2592 | |
ceb30ba4 | 2593 | print "<counter type=\"label\" id=\"$i\" counter=\"$count\" $xmsg_part/>"; |
bdb7369b | 2594 | } else { |
ceb30ba4 AD |
2595 | $ret_arr[$i]["counter"] = $count; |
2596 | $ret_arr[$i]["description"] = getFeedTitle($link, $i); | |
bdb7369b | 2597 | } |
ceb30ba4 | 2598 | |
e4f4b46f AD |
2599 | } |
2600 | ||
ceb30ba4 AD |
2601 | $old_counters = $_SESSION["lctr_last_value"]; |
2602 | $lctrs_modified = false; | |
bdb7369b | 2603 | |
2d24f032 | 2604 | |
ceb30ba4 | 2605 | $owner_uid = $_SESSION["uid"]; |
e4f4b46f | 2606 | |
467ff402 AD |
2607 | $result = db_query($link, "SELECT id, caption FROM ttrss_labels2 |
2608 | WHERE owner_uid = '$owner_uid'"); | |
abd9b165 AD |
2609 | |
2610 | while ($line = db_fetch_assoc($result)) { | |
a9cb1f83 | 2611 | |
abd9b165 AD |
2612 | $id = -$line["id"] - 11; |
2613 | ||
ceb30ba4 | 2614 | $label_name = $line["caption"]; |
467ff402 | 2615 | $count = getFeedUnread($link, $id); |
ceb30ba4 | 2616 | |
abd9b165 AD |
2617 | if (!$smart_mode || $old_counters[$id] != $count) { |
2618 | $old_counters[$id] = $count; | |
2619 | $lctrs_modified = true; | |
2620 | if (!$ret_mode) { | |
2621 | ||
2622 | if (get_pref($link, 'EXTENDED_FEEDLIST')) { | |
2623 | $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $id) . " total)\""; | |
2624 | } else { | |
2625 | $xmsg_part = ""; | |
2626 | } | |
2627 | ||
2628 | print "<counter type=\"label\" id=\"$id\" counter=\"$count\" $xmsg_part/>"; | |
bdb7369b | 2629 | } else { |
abd9b165 AD |
2630 | $ret_arr[$id]["counter"] = $count; |
2631 | $ret_arr[$id]["description"] = $label_name; | |
bdb7369b | 2632 | } |
ef393de7 | 2633 | } |
abd9b165 AD |
2634 | |
2635 | error_reporting (DEFAULT_ERROR_LEVEL); | |
5f4f7adf | 2636 | } |
a9cb1f83 AD |
2637 | |
2638 | if ($smart_mode && $lctrs_modified) { | |
2639 | $_SESSION["lctr_last_value"] = $old_counters; | |
2640 | } | |
ef393de7 AD |
2641 | |
2642 | return $ret_arr; | |
a9cb1f83 AD |
2643 | } |
2644 | ||
4ffa126e | 2645 | function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS, $active_feed = false) { |
a9cb1f83 | 2646 | |
14073c0a AD |
2647 | $age_qpart = getMaxAgeSubquery(); |
2648 | ||
a9cb1f83 AD |
2649 | if ($smart_mode) { |
2650 | if (!$_SESSION["fctr_last_value"]) { | |
2651 | $_SESSION["fctr_last_value"] = array(); | |
2652 | } | |
2653 | } | |
2654 | ||
2655 | $old_counters = $_SESSION["fctr_last_value"]; | |
2656 | ||
8a4c759e AD |
2657 | $query = "SELECT ttrss_feeds.id, |
2658 | ttrss_feeds.title, | |
2659 | ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated, | |
de0a2122 AD |
2660 | last_error, value AS count |
2661 | FROM ttrss_feeds, ttrss_counters_cache | |
8a4c759e AD |
2662 | WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]." |
2663 | AND parent_feed IS NULL | |
55e01d7e | 2664 | AND ttrss_counters_cache.feed_id = id"; |
a9cb1f83 | 2665 | |
14073c0a | 2666 | $result = db_query($link, $query); |
a9cb1f83 AD |
2667 | $fctrs_modified = false; |
2668 | ||
fb1fb4ab AD |
2669 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); |
2670 | ||
a9cb1f83 AD |
2671 | while ($line = db_fetch_assoc($result)) { |
2672 | ||
2673 | $id = $line["id"]; | |
de0a2122 | 2674 | $count = $line["count"]; |
a9cb1f83 | 2675 | $last_error = htmlspecialchars($line["last_error"]); |
fb1fb4ab AD |
2676 | |
2677 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
2678 | $last_updated = smart_date_time(strtotime($line["last_updated"])); | |
2679 | } else { | |
2680 | $last_updated = date($short_date, strtotime($line["last_updated"])); | |
2681 | } | |
2682 | ||
588fb13b AD |
2683 | $last_updated = htmlspecialchars($last_updated); |
2684 | ||
7defa089 | 2685 | $has_img = feed_has_icon($id); |
a9cb1f83 | 2686 | |
de0a2122 AD |
2687 | $tmp_result = db_query($link, |
2688 | "SELECT SUM(value) AS unread FROM ttrss_feeds, ttrss_counters_cache | |
2689 | WHERE parent_feed = '$id' AND feed_id = id"); | |
2690 | ||
2691 | $count += db_fetch_result($tmp_result, 0, "unread"); | |
a9cb1f83 AD |
2692 | |
2693 | if (!$smart_mode || $old_counters[$id] != $count) { | |
2694 | $old_counters[$id] = $count; | |
2695 | $fctrs_modified = true; | |
2696 | ||
2697 | if ($last_error) { | |
2698 | $error_part = "error=\"$last_error\""; | |
2699 | } else { | |
2700 | $error_part = ""; | |
2701 | } | |
2702 | ||
2703 | if ($has_img) { | |
2704 | $has_img_part = "hi=\"$has_img\""; | |
2705 | } else { | |
2706 | $has_img_part = ""; | |
2707 | } | |
2708 | ||
4ffa126e AD |
2709 | if ($active_feed && $id == $active_feed) { |
2710 | $has_title_part = "title=\"" . htmlspecialchars($line["title"]) . "\""; | |
2711 | } else { | |
2712 | $has_title_part = ""; | |
2713 | } | |
2714 | ||
bdb7369b AD |
2715 | if (get_pref($link, 'EXTENDED_FEEDLIST')) { |
2716 | $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $id) . " total)\""; | |
2717 | } | |
2718 | ||
2719 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\" $xmsg_part $has_title_part/>"; | |
a9cb1f83 AD |
2720 | } |
2721 | } | |
2722 | ||
2723 | if ($smart_mode && $fctrs_modified) { | |
2724 | $_SESSION["fctr_last_value"] = $old_counters; | |
2725 | } | |
2726 | } | |
2727 | ||
1b758780 | 2728 | function get_script_dt_add() { |
34e420fb | 2729 | if (strpos(VERSION, ".99") === false) { |
1b758780 AD |
2730 | return VERSION; |
2731 | } else { | |
2732 | return time(); | |
2733 | } | |
2734 | } | |
2735 | ||
6e7f8d26 AD |
2736 | function get_pgsql_version($link) { |
2737 | $result = db_query($link, "SELECT version() AS version"); | |
2738 | $version = split(" ", db_fetch_result($result, 0, "version")); | |
2739 | return $version[1]; | |
2740 | } | |
2741 | ||
af106b0e AD |
2742 | function print_error_xml($code, $add_msg = "") { |
2743 | global $ERRORS; | |
2744 | ||
2745 | $error_msg = $ERRORS[$code]; | |
2746 | ||
2747 | if ($add_msg) { | |
2748 | $error_msg = "$error_msg; $add_msg"; | |
2749 | } | |
2750 | ||
4c2abbc1 | 2751 | print "<rpc-reply>"; |
af106b0e | 2752 | print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>"; |
4c2abbc1 | 2753 | print "</rpc-reply>"; |
af106b0e | 2754 | } |
956c7629 | 2755 | |
f27de515 AD |
2756 | function subscribe_to_feed($link, $feed_link, $cat_id = 0, |
2757 | $auth_login = '', $auth_pass = '') { | |
bb0f29a4 | 2758 | |
b3dfe8ba | 2759 | # check for feed:http://url |
c91c2249 AD |
2760 | $feed_link = trim(preg_replace("/^feed:/", "", $feed_link)); |
2761 | ||
b3dfe8ba | 2762 | # check for feed://URL |
e2d84cdb | 2763 | if (strpos($feed_link, "//") === 0) { |
b3dfe8ba AD |
2764 | $feed_link = "http:$feed_link"; |
2765 | } | |
2766 | ||
c91c2249 | 2767 | if ($feed_link == "") return; |
bb0f29a4 | 2768 | |
956c7629 AD |
2769 | if ($cat_id == "0" || !$cat_id) { |
2770 | $cat_qpart = "NULL"; | |
2771 | } else { | |
2772 | $cat_qpart = "'$cat_id'"; | |
2773 | } | |
2774 | ||
2775 | $result = db_query($link, | |
2776 | "SELECT id FROM ttrss_feeds | |
2777 | WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]); | |
2778 | ||
2779 | if (db_num_rows($result) == 0) { | |
2780 | ||
2781 | $result = db_query($link, | |
f27de515 AD |
2782 | "INSERT INTO ttrss_feeds |
2783 | (owner_uid,feed_url,title,cat_id, auth_login,auth_pass) | |
956c7629 | 2784 | VALUES ('".$_SESSION["uid"]."', '$feed_link', |
f27de515 | 2785 | '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')"); |
956c7629 AD |
2786 | |
2787 | $result = db_query($link, | |
2788 | "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link' | |
f27de515 | 2789 | AND owner_uid = " . $_SESSION["uid"]); |
956c7629 AD |
2790 | |
2791 | $feed_id = db_fetch_result($result, 0, "id"); | |
2792 | ||
2793 | if ($feed_id) { | |
2794 | update_rss_feed($link, $feed_link, $feed_id, true); | |
2795 | } | |
2796 | ||
2797 | return true; | |
2798 | } else { | |
2799 | return false; | |
2800 | } | |
2801 | } | |
2802 | ||
673d54ca AD |
2803 | function print_feed_select($link, $id, $default_id = "", |
2804 | $attributes = "", $include_all_feeds = true) { | |
2805 | ||
79f3553b | 2806 | print "<select id=\"$id\" name=\"$id\" $attributes>"; |
673d54ca | 2807 | if ($include_all_feeds) { |
89cb787e | 2808 | print "<option value=\"0\">".__('All feeds')."</option>"; |
673d54ca AD |
2809 | } |
2810 | ||
2811 | $result = db_query($link, "SELECT id,title FROM ttrss_feeds | |
2812 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
2813 | ||
2814 | if (db_num_rows($result) > 0 && $include_all_feeds) { | |
2815 | print "<option disabled>--------</option>"; | |
2816 | } | |
2817 | ||
2818 | while ($line = db_fetch_assoc($result)) { | |
2819 | if ($line["id"] == $default_id) { | |
2820 | $is_selected = "selected"; | |
2821 | } else { | |
2822 | $is_selected = ""; | |
2823 | } | |
79f3553b | 2824 | printf("<option $is_selected value='%d'>%s</option>", |
47439031 | 2825 | $line["id"], htmlspecialchars($line["title"])); |
673d54ca AD |
2826 | } |
2827 | ||
2828 | print "</select>"; | |
2829 | } | |
2830 | ||
2831 | function print_feed_cat_select($link, $id, $default_id = "", | |
2832 | $attributes = "", $include_all_cats = true) { | |
2833 | ||
79f3553b | 2834 | print "<select id=\"$id\" name=\"$id\" $attributes>"; |
673d54ca AD |
2835 | |
2836 | if ($include_all_cats) { | |
d1db26aa | 2837 | print "<option value=\"0\">".__('Uncategorized')."</option>"; |
673d54ca AD |
2838 | } |
2839 | ||
2840 | $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories | |
2841 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
2842 | ||
2843 | if (db_num_rows($result) > 0 && $include_all_cats) { | |
2844 | print "<option disabled>--------</option>"; | |
2845 | } | |
2846 | ||
2847 | while ($line = db_fetch_assoc($result)) { | |
2848 | if ($line["id"] == $default_id) { | |
2849 | $is_selected = "selected"; | |
2850 | } else { | |
2851 | $is_selected = ""; | |
2852 | } | |
14f69488 | 2853 | printf("<option $is_selected value='%d'>%s</option>", |
47439031 | 2854 | $line["id"], htmlspecialchars($line["title"])); |
673d54ca AD |
2855 | } |
2856 | ||
2857 | print "</select>"; | |
2858 | } | |
2859 | ||
14f69488 AD |
2860 | function checkbox_to_sql_bool($val) { |
2861 | return ($val == "on") ? "true" : "false"; | |
2862 | } | |
86b682ce AD |
2863 | |
2864 | function getFeedCatTitle($link, $id) { | |
2865 | if ($id == -1) { | |
d1db26aa | 2866 | return __("Special"); |
86b682ce | 2867 | } else if ($id < -10) { |
d1db26aa | 2868 | return __("Labels"); |
86b682ce AD |
2869 | } else if ($id > 0) { |
2870 | $result = db_query($link, "SELECT ttrss_feed_categories.title | |
2871 | FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND | |
2872 | cat_id = ttrss_feed_categories.id"); | |
2873 | if (db_num_rows($result) == 1) { | |
2874 | return db_fetch_result($result, 0, "title"); | |
2875 | } else { | |
d1db26aa | 2876 | return __("Uncategorized"); |
86b682ce AD |
2877 | } |
2878 | } else { | |
2879 | return "getFeedCatTitle($id) failed"; | |
2880 | } | |
2881 | ||
2882 | } | |
2883 | ||
2884 | function getFeedTitle($link, $id) { | |
2885 | if ($id == -1) { | |
d1db26aa | 2886 | return __("Starred articles"); |
945c243e AD |
2887 | } else if ($id == -2) { |
2888 | return __("Published articles"); | |
2d24f032 AD |
2889 | } else if ($id == -3) { |
2890 | return __("Fresh articles"); | |
b2531a28 AD |
2891 | } else if ($id == -4) { |
2892 | return __("All articles"); | |
86b682ce | 2893 | } else if ($id < -10) { |
76626c72 | 2894 | $label_id = -$id - 11; |
ceb30ba4 | 2895 | $result = db_query($link, "SELECT caption FROM ttrss_labels2 WHERE id = '$label_id'"); |
86b682ce | 2896 | if (db_num_rows($result) == 1) { |
ceb30ba4 | 2897 | return db_fetch_result($result, 0, "caption"); |
86b682ce AD |
2898 | } else { |
2899 | return "Unknown label ($label_id)"; | |
2900 | } | |
2901 | ||
2902 | } else if ($id > 0) { | |
2903 | $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'"); | |
2904 | if (db_num_rows($result) == 1) { | |
2905 | return db_fetch_result($result, 0, "title"); | |
2906 | } else { | |
2907 | return "Unknown feed ($id)"; | |
2908 | } | |
2909 | } else { | |
2910 | return "getFeedTitle($id) failed"; | |
2911 | } | |
2912 | ||
2913 | } | |
3dd46f19 AD |
2914 | |
2915 | function get_session_cookie_name() { | |
2916 | return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME); | |
2917 | } | |
3ac2b520 AD |
2918 | |
2919 | function print_init_params($link) { | |
2920 | print "<init-params>"; | |
2921 | if ($_SESSION["stored-params"]) { | |
2922 | foreach (array_keys($_SESSION["stored-params"]) as $key) { | |
5f57b06d AD |
2923 | if ($key) { |
2924 | $value = htmlspecialchars($_SESSION["stored-params"][$key]); | |
2925 | print "<param key=\"$key\" value=\"$value\"/>"; | |
2926 | } | |
3ac2b520 AD |
2927 | } |
2928 | } | |
2929 | ||
20361063 | 2930 | print "<param key=\"theme\" value=\"".$_SESSION["theme"]."\"/>"; |
3ac2b520 AD |
2931 | print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>"; |
2932 | print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>"; | |
17c0eeba | 2933 | print "<param key=\"daemon_refresh_only\" value=\"true\"/>"; |
3ac2b520 AD |
2934 | |
2935 | print "<param key=\"on_catchup_show_next_feed\" value=\"" . | |
2936 | get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>"; | |
2937 | ||
e8bd0da9 | 2938 | print "<param key=\"hide_read_feeds\" value=\"" . |
465ff90b | 2939 | (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>"; |
e8bd0da9 | 2940 | |
c9268ed5 | 2941 | print "<param key=\"feeds_sort_by_unread\" value=\"" . |
465ff90b | 2942 | (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>"; |
c9268ed5 | 2943 | |
f6d6e22f | 2944 | print "<param key=\"confirm_feed_catchup\" value=\"" . |
465ff90b | 2945 | (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>"; |
f6d6e22f | 2946 | |
ac7bcd71 | 2947 | print "<param key=\"cdm_auto_catchup\" value=\"" . |
465ff90b | 2948 | (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>"; |
ac7bcd71 | 2949 | |
8e9c121b AD |
2950 | print "<param key=\"icons_url\" value=\"" . ICONS_URL . "\"/>"; |
2951 | ||
be0801a1 AD |
2952 | print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME . "\"/>"; |
2953 | ||
40496720 AD |
2954 | print "<param key=\"default_view_mode\" value=\"" . |
2955 | get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>"; | |
2956 | ||
2957 | print "<param key=\"default_view_limit\" value=\"" . | |
465ff90b | 2958 | (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>"; |
40496720 | 2959 | |
7b4d02a8 AD |
2960 | print "<param key=\"default_view_order_by\" value=\"" . |
2961 | get_pref($link, "_DEFAULT_VIEW_ORDER_BY") . "\"/>"; | |
2962 | ||
fe8d2059 AD |
2963 | print "<param key=\"prefs_active_tab\" value=\"" . |
2964 | get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>"; | |
2965 | ||
465ff90b AD |
2966 | print "<param key=\"infobox_disable_overlay\" value=\"" . |
2967 | get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>"; | |
2968 | ||
527c3bf0 AD |
2969 | print "<param key=\"icons_location\" value=\"" . |
2970 | ICONS_URL . "\"/>"; | |
2971 | ||
22f3e356 AD |
2972 | print "<param key=\"hide_read_shows_special\" value=\"" . |
2973 | (int) get_pref($link, "HIDE_READ_SHOWS_SPECIAL") . "\"/>"; | |
2974 | ||
fca93350 AD |
2975 | print "<param key=\"hide_feedlist\" value=\"" . |
2976 | (int) get_pref($link, "HIDE_FEEDLIST") . "\"/>"; | |
24c1e1c1 | 2977 | |
a598370d AD |
2978 | print "<param key=\"bw_limit\" value=\"". |
2979 | (int) $_SESSION["bw_limit"]."\"/>"; | |
2980 | ||
bd090ab4 AD |
2981 | // print "<param key=\"sync_counters\" value=\"" . |
2982 | // (int) get_pref($link, "SYNC_COUNTERS") . "\"/>"; | |
2983 | ||
2984 | print "<param key=\"sync_counters\" value=\"1\"/>"; | |
d234a2e3 | 2985 | |
3ac2b520 AD |
2986 | print "</init-params>"; |
2987 | } | |
f54f515f AD |
2988 | |
2989 | function print_runtime_info($link) { | |
2990 | print "<runtime-info>"; | |
20361063 | 2991 | |
71ad883b AD |
2992 | if (ENABLE_UPDATE_DAEMON) { |
2993 | print "<param key=\"daemon_is_running\" value=\"". | |
2994 | sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>"; | |
8e00ae9b | 2995 | |
9041f58b | 2996 | if (time() - $_SESSION["daemon_stamp_check"] > 30) { |
8e00ae9b AD |
2997 | |
2998 | $stamp = (int)read_stampfile("update_daemon.stamp"); | |
2999 | ||
fbae93d8 AD |
3000 | // print "<param key=\"daemon_stamp_delta\" value=\"$stamp_delta\"/>"; |
3001 | ||
8e00ae9b | 3002 | if ($stamp) { |
9041f58b AD |
3003 | $stamp_delta = time() - $stamp; |
3004 | ||
3005 | if ($stamp_delta > 1800) { | |
f6854e44 | 3006 | $stamp_check = 0; |
8e00ae9b | 3007 | } else { |
f6854e44 AD |
3008 | $stamp_check = 1; |
3009 | $_SESSION["daemon_stamp_check"] = time(); | |
8e00ae9b AD |
3010 | } |
3011 | ||
f6854e44 AD |
3012 | print "<param key=\"daemon_stamp_ok\" value=\"$stamp_check\"/>"; |
3013 | ||
8e00ae9b AD |
3014 | $stamp_fmt = date("Y.m.d, G:i", $stamp); |
3015 | ||
3016 | print "<param key=\"daemon_stamp\" value=\"$stamp_fmt\"/>"; | |
3017 | } | |
8e00ae9b | 3018 | } |
71ad883b | 3019 | } |
8e00ae9b | 3020 | |
d9fa39f1 AD |
3021 | if (CHECK_FOR_NEW_VERSION && $_SESSION["access_level"] >= 10) { |
3022 | ||
8742de78 | 3023 | if ($_SESSION["last_version_check"] + 86400 + rand(-1000, 1000) < time()) { |
d9fa39f1 AD |
3024 | $new_version_details = check_for_update($link); |
3025 | ||
3026 | print "<param key=\"new_version_available\" value=\"". | |
3027 | sprintf("%d", $new_version_details != ""). "\"/>"; | |
3028 | ||
3029 | $_SESSION["last_version_check"] = time(); | |
3030 | } | |
3031 | } | |
3032 | ||
1c9df66e | 3033 | // print "<param key=\"new_version_available\" value=\"1\"/>"; |
b4507bc2 | 3034 | |
f54f515f AD |
3035 | print "</runtime-info>"; |
3036 | } | |
ef393de7 | 3037 | |
88040f57 | 3038 | function getSearchSql($search, $match_on) { |
ef393de7 | 3039 | |
88040f57 | 3040 | $search_query_part = ""; |
e20c9d88 | 3041 | |
88040f57 AD |
3042 | $keywords = split(" ", $search); |
3043 | $query_keywords = array(); | |
e20c9d88 | 3044 | |
88040f57 | 3045 | if ($match_on == "both") { |
e20c9d88 | 3046 | |
88040f57 AD |
3047 | foreach ($keywords as $k) { |
3048 | array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%') | |
3049 | OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))"); | |
3050 | } | |
e20c9d88 | 3051 | |
88040f57 | 3052 | $search_query_part = implode("AND", $query_keywords) . " AND "; |
e20c9d88 | 3053 | |
88040f57 | 3054 | } else if ($match_on == "title") { |
e20c9d88 | 3055 | |
88040f57 AD |
3056 | foreach ($keywords as $k) { |
3057 | array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))"); | |
3058 | } | |
e20c9d88 | 3059 | |
88040f57 | 3060 | $search_query_part = implode("AND", $query_keywords) . " AND "; |
e20c9d88 | 3061 | |
88040f57 AD |
3062 | } else if ($match_on == "content") { |
3063 | ||
3064 | foreach ($keywords as $k) { | |
3065 | array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))"); | |
3066 | } | |
3067 | } | |
3068 | ||
3069 | $search_query_part = implode("AND", $query_keywords); | |
3070 | ||
3071 | return $search_query_part; | |
3072 | } | |
3073 | ||
c36bf4d5 AD |
3074 | function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) { |
3075 | ||
3076 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
c1a0b534 | 3077 | |
88040f57 AD |
3078 | if ($search) { |
3079 | ||
3080 | $search_query_part = getSearchSql($search, $match_on); | |
3081 | $search_query_part .= " AND "; | |
e20c9d88 | 3082 | |
ef393de7 AD |
3083 | } else { |
3084 | $search_query_part = ""; | |
3085 | } | |
3086 | ||
3087 | $view_query_part = ""; | |
3088 | ||
7b4d02a8 | 3089 | if ($view_mode == "adaptive" || $view_query_part == "noscores") { |
ef393de7 AD |
3090 | if ($search) { |
3091 | $view_query_part = " "; | |
3092 | } else if ($feed != -1) { | |
f295c368 | 3093 | $unread = getFeedUnread($link, $feed, $cat_view); |
ef393de7 | 3094 | if ($unread > 0) { |
ff863e00 | 3095 | $view_query_part = " unread = true AND "; |
ef393de7 AD |
3096 | } |
3097 | } | |
3098 | } | |
3099 | ||
3100 | if ($view_mode == "marked") { | |
3101 | $view_query_part = " marked = true AND "; | |
3102 | } | |
3103 | ||
3104 | if ($view_mode == "unread") { | |
3105 | $view_query_part = " unread = true AND "; | |
3106 | } | |
8b09eac8 AD |
3107 | |
3108 | if ($view_mode == "updated") { | |
3109 | $view_query_part = " (last_read is null and unread = false) AND "; | |
3110 | } | |
3111 | ||
ef393de7 AD |
3112 | if ($limit > 0) { |
3113 | $limit_query_part = "LIMIT " . $limit; | |
3114 | } | |
3115 | ||
3116 | $vfeed_query_part = ""; | |
3117 | ||
3118 | // override query strategy and enable feed display when searching globally | |
3119 | if ($search && $search_mode == "all_feeds") { | |
3120 | $query_strategy_part = "ttrss_entries.id > 0"; | |
3121 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
3122 | } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) { | |
3123 | $query_strategy_part = "ttrss_entries.id > 0"; | |
3124 | $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE | |
3125 | id = feed_id) as feed_title,"; | |
3126 | } else if ($feed >= 0 && $search && $search_mode == "this_cat") { | |
3127 | ||
3128 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
0a6c4846 AD |
3129 | |
3130 | $tmp_result = false; | |
3131 | ||
3132 | if ($cat_view) { | |
3133 | $tmp_result = db_query($link, "SELECT id | |
3134 | FROM ttrss_feeds WHERE cat_id = '$feed'"); | |
3135 | } else { | |
3136 | $tmp_result = db_query($link, "SELECT id | |
3137 | FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds | |
3138 | WHERE id = '$feed') AND id != '$feed'"); | |
3139 | } | |
ef393de7 AD |
3140 | |
3141 | $cat_siblings = array(); | |
3142 | ||
3143 | if (db_num_rows($tmp_result) > 0) { | |
3144 | while ($p = db_fetch_assoc($tmp_result)) { | |
3145 | array_push($cat_siblings, "feed_id = " . $p["id"]); | |
3146 | } | |
3147 | ||
3148 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
3149 | $feed, implode(" OR ", $cat_siblings)); | |
3150 | ||
3151 | } else { | |
3152 | $query_strategy_part = "ttrss_entries.id > 0"; | |
3153 | } | |
3154 | ||
3155 | } else if ($feed >= 0) { | |
3156 | ||
3157 | if ($cat_view) { | |
5c365f60 | 3158 | |
ef393de7 AD |
3159 | if ($feed > 0) { |
3160 | $query_strategy_part = "cat_id = '$feed'"; | |
3161 | } else { | |
3162 | $query_strategy_part = "cat_id IS NULL"; | |
3163 | } | |
3164 | ||
3165 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
5c365f60 | 3166 | |
ef393de7 AD |
3167 | } else { |
3168 | $tmp_result = db_query($link, "SELECT id | |
3169 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
3170 | ORDER BY cat_id,title"); | |
3171 | ||
3172 | $parent_ids = array(); | |
3173 | ||
3174 | if (db_num_rows($tmp_result) > 0) { | |
3175 | while ($p = db_fetch_assoc($tmp_result)) { | |
3176 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
3177 | } | |
3178 | ||
3179 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
3180 | $feed, implode(" OR ", $parent_ids)); | |
3181 | ||
3182 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
3183 | } else { | |
3184 | $query_strategy_part = "feed_id = '$feed'"; | |
3185 | } | |
3186 | } | |
3187 | } else if ($feed == -1) { // starred virtual feed | |
3188 | $query_strategy_part = "marked = true"; | |
3189 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
e6a38cde AD |
3190 | } else if ($feed == -2) { // published virtual feed OR labels category |
3191 | ||
3192 | if (!$cat_view) { | |
3193 | $query_strategy_part = "published = true"; | |
3194 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
3195 | } else { | |
3196 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
3197 | ||
3198 | $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2"; | |
3199 | ||
3200 | $query_strategy_part = "ttrss_labels2.id = ttrss_user_labels2.label_id AND | |
3201 | ttrss_user_labels2.article_id = ref_id"; | |
3202 | ||
3203 | } | |
3204 | ||
2d24f032 AD |
3205 | } else if ($feed == -3) { // fresh virtual feed |
3206 | $query_strategy_part = "unread = true"; | |
3207 | ||
7a22dc2a | 3208 | $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid); |
c1d7e6c3 | 3209 | |
2d24f032 | 3210 | if (DB_TYPE == "pgsql") { |
7608b38a | 3211 | $query_strategy_part .= " AND updated > NOW() - INTERVAL '$intl hour' "; |
2d24f032 | 3212 | } else { |
7608b38a | 3213 | $query_strategy_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) "; |
2d24f032 AD |
3214 | } |
3215 | ||
b2531a28 AD |
3216 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; |
3217 | } else if ($feed == -4) { // all articles virtual feed | |
3218 | $query_strategy_part = "true"; | |
e4f4b46f | 3219 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; |
ef393de7 AD |
3220 | } else if ($feed <= -10) { // labels |
3221 | $label_id = -$feed - 11; | |
3de0261a | 3222 | |
ceb30ba4 AD |
3223 | $query_strategy_part = "label_id = '$label_id' AND |
3224 | ttrss_labels2.id = ttrss_user_labels2.label_id AND | |
3225 | ttrss_user_labels2.article_id = ref_id"; | |
3de0261a | 3226 | |
ef393de7 | 3227 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; |
ceb30ba4 AD |
3228 | $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2"; |
3229 | ||
ef393de7 AD |
3230 | } else { |
3231 | $query_strategy_part = "id > 0"; // dumb | |
3232 | } | |
d6e5706d | 3233 | |
7a22dc2a | 3234 | if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) { |
d6e5706d AD |
3235 | $order_by = "updated"; |
3236 | } else { | |
3237 | $order_by = "updated DESC"; | |
3238 | } | |
e939722a | 3239 | |
7b4d02a8 AD |
3240 | if ($view_mode != "noscores") { |
3241 | $order_by = "score DESC, $order_by"; | |
3242 | } | |
48b0c4ec | 3243 | |
e939722a AD |
3244 | if ($override_order) { |
3245 | $order_by = $override_order; | |
3246 | } | |
ef393de7 AD |
3247 | |
3248 | $feed_title = ""; | |
3249 | ||
3250 | if ($search && $search_mode == "all_feeds") { | |
b36e002f | 3251 | $feed_title = __("Search results")." ($search)"; |
ef393de7 | 3252 | } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) { |
b36e002f | 3253 | $feed_title = __("Search results")." ($search, $feed)"; |
ef393de7 AD |
3254 | } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) { |
3255 | $feed_title = $feed; | |
3256 | } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) { | |
3257 | ||
3258 | if ($cat_view) { | |
5c365f60 | 3259 | |
ef393de7 AD |
3260 | if ($feed != 0) { |
3261 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories | |
c36bf4d5 | 3262 | WHERE id = '$feed' AND owner_uid = $owner_uid"); |
ef393de7 AD |
3263 | $feed_title = db_fetch_result($result, 0, "title"); |
3264 | } else { | |
d1db26aa | 3265 | $feed_title = __("Uncategorized"); |
ef393de7 | 3266 | } |
e1eb2147 AD |
3267 | |
3268 | if ($search) { | |
b36e002f | 3269 | $feed_title = __("Searched for")." $search ($feed_title)"; |
e1eb2147 AD |
3270 | } |
3271 | ||
ef393de7 AD |
3272 | } else { |
3273 | ||
3274 | $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds | |
c36bf4d5 | 3275 | WHERE id = '$feed' AND owner_uid = $owner_uid"); |
ef393de7 AD |
3276 | |
3277 | $feed_title = db_fetch_result($result, 0, "title"); | |
3278 | $feed_site_url = db_fetch_result($result, 0, "site_url"); | |
3279 | $last_error = db_fetch_result($result, 0, "last_error"); | |
e1eb2147 AD |
3280 | |
3281 | if ($search) { | |
b36e002f | 3282 | $feed_title = __("Searched for") . " $search ($feed_title)"; |
e1eb2147 | 3283 | } |
ef393de7 AD |
3284 | } |
3285 | ||
3286 | } else if ($feed == -1) { | |
d1db26aa | 3287 | $feed_title = __("Starred articles"); |
6cfa22dc | 3288 | if ($search) { $feed_title = __("Searched for") . " $search ($feed_title)"; } |
e4f4b46f | 3289 | } else if ($feed == -2) { |
3c88195b AD |
3290 | if (!$cat_view) { |
3291 | $feed_title = __("Published articles"); | |
3292 | if ($search) { $feed_title = __("Searched for") . " $search ($feed_title)"; } | |
3293 | } else { | |
3294 | $feed_title = __("Labels"); | |
3295 | } | |
2d24f032 AD |
3296 | } else if ($feed == -3) { |
3297 | $feed_title = __("Fresh articles"); | |
6cfa22dc | 3298 | if ($search) { $feed_title = __("Searched for") . " $search ($feed_title)"; } |
b2531a28 AD |
3299 | } else if ($feed == -4) { |
3300 | $feed_title = __("All articles"); | |
3301 | if ($search) { $feed_title = __("Searched for") . " $search ($feed_title)"; } | |
ef393de7 AD |
3302 | } else if ($feed < -10) { |
3303 | $label_id = -$feed - 11; | |
ceb30ba4 | 3304 | $result = db_query($link, "SELECT caption FROM ttrss_labels2 |
ef393de7 | 3305 | WHERE id = '$label_id'"); |
ceb30ba4 | 3306 | $feed_title = db_fetch_result($result, 0, "caption"); |
88040f57 AD |
3307 | |
3308 | if ($search) { | |
b36e002f | 3309 | $feed_title = __("Searched for") . " $search ($feed_title)"; |
88040f57 | 3310 | } |
ef393de7 AD |
3311 | } else { |
3312 | $feed_title = "?"; | |
3313 | } | |
3314 | ||
ef393de7 AD |
3315 | if ($feed < -10) error_reporting (0); |
3316 | ||
62129e67 AD |
3317 | $content_query_part = "content as content_preview,"; |
3318 | ||
ef393de7 AD |
3319 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { |
3320 | ||
3321 | if ($feed >= 0) { | |
3322 | $feed_kind = "Feeds"; | |
3323 | } else { | |
3324 | $feed_kind = "Labels"; | |
3325 | } | |
3326 | ||
95a82c08 AD |
3327 | if ($limit_query_part) { |
3328 | $offset_query_part = "OFFSET $offset"; | |
3329 | } | |
3330 | ||
d00f22ac | 3331 | if ($vfeed_query_part && get_pref($link, 'VFEED_GROUP_BY_FEED', $owner_uid)) { |
6cfea5c7 | 3332 | if (!$override_order) { |
43fc671f AD |
3333 | $order_by = "ttrss_feeds.title, $order_by"; |
3334 | } | |
3335 | ||
a91c7082 AD |
3336 | // Special output for Fresh feed |
3337 | ||
3338 | /* if ($feed == -3) { | |
43fc671f AD |
3339 | $group_limit_part = "(select count(*) from |
3340 | ttrss_user_entries AS t1, ttrss_entries AS t2 where | |
3341 | t1.ref_id = t2.id and t1.owner_uid = 2 and | |
3342 | t1.feed_id = ttrss_user_entries.feed_id and | |
3343 | t2.updated > ttrss_entries.updated) <= 5 AND"; | |
a91c7082 | 3344 | } */ |
6cfea5c7 AD |
3345 | } |
3346 | ||
3ebd7ca5 | 3347 | $query = "SELECT DISTINCT |
1f64b1be | 3348 | guid, |
ef393de7 | 3349 | ttrss_entries.id,ttrss_entries.title, |
46921916 | 3350 | updated, |
e4f4b46f | 3351 | unread,feed_id,marked,published,link,last_read, |
fc2b26a6 | 3352 | ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms, |
ef393de7 AD |
3353 | $vfeed_query_part |
3354 | $content_query_part | |
fc2b26a6 | 3355 | ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms, |
ff6e357a | 3356 | author,score |
ef393de7 | 3357 | FROM |
ceb30ba4 | 3358 | ttrss_entries,ttrss_user_entries,ttrss_feeds$ext_tables_part |
ef393de7 | 3359 | WHERE |
43fc671f | 3360 | $group_limit_part |
546499a9 | 3361 | ttrss_feeds.hidden = false AND |
ef393de7 AD |
3362 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
3363 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
c36bf4d5 | 3364 | ttrss_user_entries.owner_uid = '$owner_uid' AND |
ef393de7 AD |
3365 | $search_query_part |
3366 | $view_query_part | |
3367 | $query_strategy_part ORDER BY $order_by | |
95a82c08 | 3368 | $limit_query_part $offset_query_part"; |
4bc311fc | 3369 | |
ef393de7 | 3370 | if ($_GET["debug"]) print $query; |
4bc311fc AD |
3371 | |
3372 | $result = db_query($link, $query); | |
ef393de7 AD |
3373 | |
3374 | } else { | |
3375 | // browsing by tag | |
3376 | ||
3377 | $feed_kind = "Tags"; | |
3378 | ||
3379 | $result = db_query($link, "SELECT | |
1f64b1be | 3380 | guid, |
ef393de7 | 3381 | ttrss_entries.id as id,title, |
46921916 | 3382 | updated, |
ef393de7 AD |
3383 | unread,feed_id, |
3384 | marked,link,last_read, | |
fc2b26a6 | 3385 | ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms, |
ef393de7 AD |
3386 | $vfeed_query_part |
3387 | $content_query_part | |
4d0b3607 AD |
3388 | ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms, |
3389 | score | |
ef393de7 AD |
3390 | FROM |
3391 | ttrss_entries,ttrss_user_entries,ttrss_tags | |
3392 | WHERE | |
546499a9 | 3393 | ref_id = ttrss_entries.id AND |
c36bf4d5 | 3394 | ttrss_user_entries.owner_uid = '$owner_uid' AND |
ef393de7 AD |
3395 | post_int_id = int_id AND tag_name = '$feed' AND |
3396 | $view_query_part | |
3397 | $search_query_part | |
3398 | $query_strategy_part ORDER BY $order_by | |
3399 | $limit_query_part"); | |
3400 | } | |
3401 | ||
c7188969 | 3402 | return array($result, $feed_title, $feed_site_url, $last_error); |
ef393de7 AD |
3403 | |
3404 | } | |
3405 | ||
c36bf4d5 | 3406 | function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat, |
ead2715d AD |
3407 | $limit, $search, $search_mode, $match_on) { |
3408 | ||
3409 | if (!$limit) $limit = 30; | |
18664970 AD |
3410 | |
3411 | $qfh_ret = queryFeedHeadlines($link, $feed, | |
ead2715d | 3412 | $limit, false, $is_cat, $search, $search_mode, $match_on, "updated DESC", 0, |
c36bf4d5 | 3413 | $owner_uid); |
18664970 AD |
3414 | |
3415 | $result = $qfh_ret[0]; | |
59e2aab4 | 3416 | $feed_title = htmlspecialchars($qfh_ret[1]); |
18664970 AD |
3417 | $feed_site_url = $qfh_ret[2]; |
3418 | $last_error = $qfh_ret[3]; | |
3419 | ||
a5472764 | 3420 | // if (!$feed_site_url) $feed_site_url = "http://localhost/"; |
4bc64807 | 3421 | |
a5472764 AD |
3422 | print "<?xml version=\"1.0\" encoding=\"utf-8\"?> |
3423 | <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?> | |
3424 | <rss version=\"2.0\"> | |
3baeeeca AD |
3425 | <channel> |
3426 | <title>$feed_title</title> | |
4bc64807 AD |
3427 | <link>$feed_site_url</link> |
3428 | <description>Feed generated by Tiny Tiny RSS</description>"; | |
3baeeeca AD |
3429 | |
3430 | while ($line = db_fetch_assoc($result)) { | |
3431 | print "<item>"; | |
4bc64807 | 3432 | print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>"; |
3baeeeca | 3433 | print "<link>" . htmlspecialchars($line["link"]) . "</link>"; |
0c3d1c68 | 3434 | |
bc976a8c | 3435 | $tags = get_article_tags($link, $line["id"], $owner_uid); |
0c3d1c68 AD |
3436 | |
3437 | foreach ($tags as $tag) { | |
3438 | print "<category>" . htmlspecialchars($tag) . "</category>"; | |
3439 | } | |
3440 | ||
3baeeeca AD |
3441 | $rfc822_date = date('r', strtotime($line["updated"])); |
3442 | ||
3443 | print "<pubDate>$rfc822_date</pubDate>"; | |
3444 | ||
3445 | print "<title>" . | |
3446 | htmlspecialchars($line["title"]) . "</title>"; | |
3447 | ||
0c3d1c68 AD |
3448 | print "<description><![CDATA[" . |
3449 | $line["content_preview"] . "]]></description>"; | |
3baeeeca AD |
3450 | |
3451 | print "</item>"; | |
3452 | } | |
3453 | ||
3454 | print "</channel></rss>"; | |
18664970 AD |
3455 | |
3456 | } | |
3457 | ||
0a6c4846 AD |
3458 | function getCategoryTitle($link, $cat_id) { |
3459 | ||
bba7c4bf AD |
3460 | if ($cat_id == -1) { |
3461 | return __("Special"); | |
3462 | } else if ($cat_id == -2) { | |
3463 | return __("Labels"); | |
0a6c4846 | 3464 | } else { |
bba7c4bf AD |
3465 | |
3466 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE | |
3467 | id = '$cat_id'"); | |
3468 | ||
3469 | if (db_num_rows($result) == 1) { | |
3470 | return db_fetch_result($result, 0, "title"); | |
3471 | } else { | |
3472 | return "Uncategorized"; | |
3473 | } | |
0a6c4846 AD |
3474 | } |
3475 | } | |
3476 | ||
f738aef1 AD |
3477 | // http://ru2.php.net/strip-tags |
3478 | ||
3479 | function strip_tags_long($textstring, $allowed){ | |
3480 | while($textstring != strip_tags($textstring, $allowed)) | |
3481 | { | |
3482 | while (strlen($textstring) != 0) | |
3483 | { | |
3484 | if (strlen($textstring) > 1024) { | |
3485 | $otherlen = 1024; | |
3486 | } else { | |
3487 | $otherlen = strlen($textstring); | |
3488 | } | |
3489 | $temptext = strip_tags(substr($textstring,0,$otherlen), $allowed); | |
3490 | $safetext .= $temptext; | |
3491 | $textstring = substr_replace($textstring,'',0,$otherlen); | |
3492 | } | |
3493 | $textstring = $safetext; | |
3494 | } | |
3495 | return $textstring; | |
3496 | } | |
3497 | ||
3498 | ||
1ac0baf4 | 3499 | function sanitize_rss($link, $str, $force_strip_tags = false) { |
60452879 | 3500 | $res = $str; |
183ad07b | 3501 | |
1ac0baf4 | 3502 | if (get_pref($link, "STRIP_UNSAFE_TAGS") || $force_strip_tags) { |
f738aef1 | 3503 | |
7e1265ed | 3504 | $res = strip_tags_long($res, |
4f376043 | 3505 | "<p><a><i><em><b><strong><code><pre><blockquote><br><img><ul><ol><li>"); |
f738aef1 AD |
3506 | |
3507 | // $res = preg_replace("/\r\n|\n|\r/", "", $res); | |
3508 | // $res = strip_tags_long($res, "<p><a><i><em><b><strong><blockquote><br><img><div><span>"); | |
f826eee1 AD |
3509 | } |
3510 | ||
8dccabed AD |
3511 | if (get_pref($link, "STRIP_IMAGES")) { |
3512 | ||
3513 | $res = preg_replace('/<img[^>]+>/is', '', $res); | |
3514 | ||
3515 | } | |
3516 | ||
183ad07b AD |
3517 | return $res; |
3518 | } | |
b72c3ef8 | 3519 | |
45004d43 AD |
3520 | /** |
3521 | * Send by mail a digest of last articles. | |
3522 | * | |
3523 | * @param mixed $link The database connection. | |
3524 | * @param integer $limit The maximum number of articles by digest. | |
3525 | * @return boolean Return false if digests are not enabled. | |
3526 | */ | |
9cd7c995 AD |
3527 | function send_headlines_digests($link, $limit = 100) { |
3528 | ||
1ddba275 AD |
3529 | if (!DIGEST_ENABLE) return false; |
3530 | ||
9cd7c995 | 3531 | $user_limit = DIGEST_EMAIL_LIMIT; |
5430c959 | 3532 | $days = 1; |
9cd7c995 AD |
3533 | |
3534 | print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n"; | |
3535 | ||
3536 | if (DB_TYPE == "pgsql") { | |
3537 | $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'"; | |
3538 | } else if (DB_TYPE == "mysql") { | |
3539 | $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
3540 | } | |
3541 | ||
3542 | $result = db_query($link, "SELECT id,email FROM ttrss_users | |
3543 | WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)"); | |
3544 | ||
3545 | while ($line = db_fetch_assoc($result)) { | |
dc85be2b | 3546 | |
9cd7c995 AD |
3547 | if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) { |
3548 | print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... "; | |
3549 | ||
dc85be2b AD |
3550 | $do_catchup = get_pref($link, 'DIGEST_CATCHUP', $line['id'], false); |
3551 | ||
9cd7c995 AD |
3552 | $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit); |
3553 | $digest = $tuple[0]; | |
3554 | $headlines_count = $tuple[1]; | |
dc85be2b | 3555 | $affected_ids = $tuple[2]; |
c62a2c21 | 3556 | $digest_text = $tuple[3]; |
9cd7c995 AD |
3557 | |
3558 | if ($headlines_count > 0) { | |
a8931123 | 3559 | |
c62a2c21 | 3560 | $mail = new PHPMailer(); |
a8931123 | 3561 | |
d134e3a3 AD |
3562 | $mail->PluginDir = "lib/phpmailer/"; |
3563 | $mail->SetLanguage("en", "lib/phpmailer/language/"); | |
a8931123 | 3564 | |
c62a2c21 | 3565 | $mail->CharSet = "UTF-8"; |
a8931123 | 3566 | |
c62a2c21 AD |
3567 | $mail->From = DIGEST_FROM_ADDRESS; |
3568 | $mail->FromName = DIGEST_FROM_NAME; | |
3569 | $mail->AddAddress($line["email"], $line["login"]); | |
c7ddac5c | 3570 | |
c62a2c21 | 3571 | if (DIGEST_SMTP_HOST) { |
a8931123 AD |
3572 | $mail->Host = DIGEST_SMTP_HOST; |
3573 | $mail->Mailer = "smtp"; | |
19a1da0d | 3574 | $mail->SMTPAuth = DIGEST_SMTP_LOGIN != ''; |
a8931123 AD |
3575 | $mail->Username = DIGEST_SMTP_LOGIN; |
3576 | $mail->Password = DIGEST_SMTP_PASSWORD; | |
c62a2c21 | 3577 | } |
a8931123 | 3578 | |
c62a2c21 | 3579 | $mail->IsHTML(true); |
163a295e | 3580 | $mail->Subject = DIGEST_SUBJECT; |
c62a2c21 AD |
3581 | $mail->Body = $digest; |
3582 | $mail->AltBody = $digest_text; | |
a8931123 | 3583 | |
c62a2c21 | 3584 | $rc = $mail->Send(); |
a8931123 | 3585 | |
c62a2c21 | 3586 | if (!$rc) print "ERROR: " . $mail->ErrorInfo; |
a8931123 | 3587 | |
9cd7c995 | 3588 | print "RC=$rc\n"; |
a8931123 | 3589 | |
c62a2c21 | 3590 | if ($rc && $do_catchup) { |
dc85be2b | 3591 | print "Marking affected articles as read...\n"; |
9968d46f | 3592 | catchupArticlesById($link, $affected_ids, 0, $line["id"]); |
dc85be2b AD |
3593 | } |
3594 | ||
9cd7c995 AD |
3595 | db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW() |
3596 | WHERE id = " . $line["id"]); | |
3597 | } else { | |
3598 | print "No headlines\n"; | |
3599 | } | |
3600 | } | |
3601 | } | |
3602 | ||
cedd3e89 AD |
3603 | print "All done.\n"; |
3604 | ||
9cd7c995 AD |
3605 | } |
3606 | ||
7e3634d9 | 3607 | function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) { |
c62a2c21 | 3608 | |
fe7537b5 | 3609 | require_once "lib/MiniTemplator.class.php"; |
c62a2c21 AD |
3610 | |
3611 | $tpl = new MiniTemplator; | |
3612 | $tpl_t = new MiniTemplator; | |
3613 | ||
3614 | $tpl->readTemplateFromFile("templates/digest_template_html.txt"); | |
3615 | $tpl_t->readTemplateFromFile("templates/digest_template.txt"); | |
3616 | ||
3617 | $tpl->setVariable('CUR_DATE', date('Y/m/d')); | |
3618 | $tpl->setVariable('CUR_TIME', date('G:i')); | |
3619 | ||
3620 | $tpl_t->setVariable('CUR_DATE', date('Y/m/d')); | |
3621 | $tpl_t->setVariable('CUR_TIME', date('G:i')); | |
7e3634d9 | 3622 | |
dc85be2b AD |
3623 | $affected_ids = array(); |
3624 | ||
7e3634d9 | 3625 | if (DB_TYPE == "pgsql") { |
9cd7c995 | 3626 | $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'"; |
7e3634d9 AD |
3627 | } else if (DB_TYPE == "mysql") { |
3628 | $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
3629 | } | |
3630 | ||
3631 | $result = db_query($link, "SELECT ttrss_entries.title, | |
3632 | ttrss_feeds.title AS feed_title, | |
3633 | date_entered, | |
dc85be2b | 3634 | ttrss_user_entries.ref_id, |
7e3634d9 | 3635 | link, |
c62a2c21 | 3636 | SUBSTRING(content, 1, 120) AS excerpt, |
fc2b26a6 | 3637 | ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated |
7e3634d9 AD |
3638 | FROM |
3639 | ttrss_user_entries,ttrss_entries,ttrss_feeds | |
3640 | WHERE | |
3641 | ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id | |
3dd9183c | 3642 | AND include_in_digest = true |
7e3634d9 | 3643 | AND $interval_query |
a8931123 | 3644 | AND hidden = false |
448b0abd | 3645 | AND ttrss_user_entries.owner_uid = $user_id |
c62a2c21 AD |
3646 | AND unread = true |
3647 | ORDER BY ttrss_feeds.title, date_entered DESC | |
7e3634d9 AD |
3648 | LIMIT $limit"); |
3649 | ||
3650 | $cur_feed_title = ""; | |
3651 | ||
9cd7c995 AD |
3652 | $headlines_count = db_num_rows($result); |
3653 | ||
c62a2c21 AD |
3654 | $headlines = array(); |
3655 | ||
7e3634d9 | 3656 | while ($line = db_fetch_assoc($result)) { |
c62a2c21 AD |
3657 | array_push($headlines, $line); |
3658 | } | |
3659 | ||
3660 | for ($i = 0; $i < sizeof($headlines); $i++) { | |
3661 | ||
3662 | $line = $headlines[$i]; | |
dc85be2b AD |
3663 | |
3664 | array_push($affected_ids, $line["ref_id"]); | |
3665 | ||
7e3634d9 | 3666 | $updated = smart_date_time(strtotime($line["last_updated"])); |
7e3634d9 | 3667 | |
c62a2c21 AD |
3668 | $tpl->setVariable('FEED_TITLE', $line["feed_title"]); |
3669 | $tpl->setVariable('ARTICLE_TITLE', $line["title"]); | |
3670 | $tpl->setVariable('ARTICLE_LINK', $line["link"]); | |
3671 | $tpl->setVariable('ARTICLE_UPDATED', $updated); | |
163a295e AD |
3672 | $tpl->setVariable('ARTICLE_EXCERPT', |
3673 | truncate_string(strip_tags($line["excerpt"]), 100)); | |
7e3634d9 | 3674 | |
c62a2c21 AD |
3675 | $tpl->addBlock('article'); |
3676 | ||
3677 | $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]); | |
3678 | $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]); | |
3679 | $tpl_t->setVariable('ARTICLE_LINK', $line["link"]); | |
3680 | $tpl_t->setVariable('ARTICLE_UPDATED', $updated); | |
3681 | // $tpl_t->setVariable('ARTICLE_EXCERPT', | |
3682 | // truncate_string(strip_tags($line["excerpt"]), 100)); | |
3683 | ||
3684 | $tpl_t->addBlock('article'); | |
3685 | ||
3686 | if ($headlines[$i]['feed_title'] != $headlines[$i+1]['feed_title']) { | |
3687 | $tpl->addBlock('feed'); | |
3688 | $tpl_t->addBlock('feed'); | |
7e3634d9 AD |
3689 | } |
3690 | ||
7e3634d9 AD |
3691 | } |
3692 | ||
c62a2c21 AD |
3693 | $tpl->addBlock('digest'); |
3694 | $tpl->generateOutputToString($tmp); | |
3695 | ||
3696 | $tpl_t->addBlock('digest'); | |
3697 | $tpl_t->generateOutputToString($tmp_t); | |
7e3634d9 | 3698 | |
c62a2c21 | 3699 | return array($tmp, $headlines_count, $affected_ids, $tmp_t); |
7e3634d9 AD |
3700 | } |
3701 | ||
73495fd1 | 3702 | function check_for_update($link) { |
b6d486a3 | 3703 | $releases_feed = "http://tt-rss.org/releases.rss"; |
b72c3ef8 AD |
3704 | |
3705 | if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) { | |
3706 | return; | |
3707 | } | |
3708 | ||
3709 | error_reporting(0); | |
f67d9754 AD |
3710 | if (ENABLE_SIMPLEPIE) { |
3711 | $rss = new SimplePie(); | |
3712 | $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT); | |
4148e809 | 3713 | // $rss->set_timeout(MAGPIE_FETCH_TIME_OUT); |
f67d9754 AD |
3714 | $rss->set_feed_url($fetch_url); |
3715 | $rss->set_output_encoding('UTF-8'); | |
3716 | $rss->init(); | |
3717 | } else { | |
3718 | $rss = fetch_rss($releases_feed); | |
3719 | } | |
b72c3ef8 AD |
3720 | error_reporting (DEFAULT_ERROR_LEVEL); |
3721 | ||
3722 | if ($rss) { | |
3723 | ||
f67d9754 AD |
3724 | if (ENABLE_SIMPLEPIE) { |
3725 | $items = $rss->get_items(); | |
3726 | } else { | |
3727 | $items = $rss->items; | |
b72c3ef8 | 3728 | |
f67d9754 AD |
3729 | if (!$items || !is_array($items)) $items = $rss->entries; |
3730 | if (!$items || !is_array($items)) $items = $rss; | |
3731 | } | |
b72c3ef8 | 3732 | |
da412ad3 | 3733 | if (!is_array($items) || count($items) == 0) { |
b72c3ef8 | 3734 | return; |
da412ad3 | 3735 | } |
b72c3ef8 | 3736 | |
a41d2c65 | 3737 | $latest_item = $items[0]; |
b72c3ef8 | 3738 | |
f67d9754 AD |
3739 | if (ENABLE_SIMPLEPIE) { |
3740 | $last_title = $latest_item->get_title(); | |
3741 | } else { | |
3742 | $last_title = $latest_item["title"]; | |
3743 | } | |
b72c3ef8 | 3744 | |
f67d9754 AD |
3745 | $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $last_title)); |
3746 | ||
3747 | if (ENABLE_SIMPLEPIE) { | |
dcf7fd08 AD |
3748 | $release_url = sanitize_rss($link, $latest_item->get_link()); |
3749 | $content = sanitize_rss($link, $latest_item->get_description()); | |
f67d9754 AD |
3750 | } else { |
3751 | $release_url = sanitize_rss($link, $latest_item["link"]); | |
3752 | $content = sanitize_rss($link, $latest_item["description"]); | |
3753 | } | |
48e1a342 | 3754 | |
a41d2c65 | 3755 | if (version_compare(VERSION, $latest_version) == -1) { |
73495fd1 AD |
3756 | return sprintf("New version of Tiny-Tiny RSS (%s) is available:", |
3757 | $latest_version)."<div class='milestoneDetails'>$content</div>"; | |
3758 | } else { | |
3759 | return false; | |
3760 | } | |
b72c3ef8 AD |
3761 | } |
3762 | } | |
472782e8 | 3763 | |
18eddb2c AD |
3764 | function markArticlesById($link, $ids, $cmode) { |
3765 | ||
3766 | $tmp_ids = array(); | |
3767 | ||
3768 | foreach ($ids as $id) { | |
3769 | array_push($tmp_ids, "ref_id = '$id'"); | |
3770 | } | |
3771 | ||
3772 | $ids_qpart = join(" OR ", $tmp_ids); | |
3773 | ||
3774 | if ($cmode == 0) { | |
3775 | db_query($link, "UPDATE ttrss_user_entries SET | |
3776 | marked = false,last_read = NOW() | |
3777 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3778 | } else if ($cmode == 1) { | |
3779 | db_query($link, "UPDATE ttrss_user_entries SET | |
3780 | marked = true | |
3781 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3782 | } else { | |
3783 | db_query($link, "UPDATE ttrss_user_entries SET | |
3784 | marked = NOT marked,last_read = NOW() | |
3785 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3786 | } | |
3787 | } | |
3788 | ||
e4f4b46f AD |
3789 | function publishArticlesById($link, $ids, $cmode) { |
3790 | ||
3791 | $tmp_ids = array(); | |
3792 | ||
3793 | foreach ($ids as $id) { | |
3794 | array_push($tmp_ids, "ref_id = '$id'"); | |
3795 | } | |
3796 | ||
3797 | $ids_qpart = join(" OR ", $tmp_ids); | |
3798 | ||
3799 | if ($cmode == 0) { | |
3800 | db_query($link, "UPDATE ttrss_user_entries SET | |
3801 | published = false,last_read = NOW() | |
3802 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3803 | } else if ($cmode == 1) { | |
3804 | db_query($link, "UPDATE ttrss_user_entries SET | |
3805 | published = true | |
3806 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3807 | } else { | |
3808 | db_query($link, "UPDATE ttrss_user_entries SET | |
3809 | published = NOT published,last_read = NOW() | |
3810 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3811 | } | |
3812 | } | |
3813 | ||
9968d46f AD |
3814 | function catchupArticlesById($link, $ids, $cmode, $owner_uid = false) { |
3815 | ||
3816 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
472782e8 AD |
3817 | |
3818 | $tmp_ids = array(); | |
3819 | ||
3820 | foreach ($ids as $id) { | |
3821 | array_push($tmp_ids, "ref_id = '$id'"); | |
3822 | } | |
3823 | ||
3824 | $ids_qpart = join(" OR ", $tmp_ids); | |
3825 | ||
3826 | if ($cmode == 0) { | |
3827 | db_query($link, "UPDATE ttrss_user_entries SET | |
3828 | unread = false,last_read = NOW() | |
9968d46f | 3829 | WHERE ($ids_qpart) AND owner_uid = $owner_uid"); |
472782e8 AD |
3830 | } else if ($cmode == 1) { |
3831 | db_query($link, "UPDATE ttrss_user_entries SET | |
3832 | unread = true | |
9968d46f | 3833 | WHERE ($ids_qpart) AND owner_uid = $owner_uid"); |
472782e8 AD |
3834 | } else { |
3835 | db_query($link, "UPDATE ttrss_user_entries SET | |
3836 | unread = NOT unread,last_read = NOW() | |
9968d46f | 3837 | WHERE ($ids_qpart) AND owner_uid = $owner_uid"); |
472782e8 | 3838 | } |
0737b95a AD |
3839 | |
3840 | /* update ccache */ | |
3841 | ||
3842 | $result = db_query($link, "SELECT DISTINCT feed_id FROM ttrss_user_entries | |
3843 | WHERE ($ids_qpart) AND owner_uid = $owner_uid"); | |
3844 | ||
3845 | while ($line = db_fetch_assoc($result)) { | |
3846 | ccache_update($link, $line["feed_id"], $owner_uid); | |
3847 | } | |
472782e8 AD |
3848 | } |
3849 | ||
e097e8be AD |
3850 | function catchupArticleById($link, $id, $cmode) { |
3851 | ||
3852 | if ($cmode == 0) { | |
3853 | db_query($link, "UPDATE ttrss_user_entries SET | |
3854 | unread = false,last_read = NOW() | |
3855 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
3856 | } else if ($cmode == 1) { | |
3857 | db_query($link, "UPDATE ttrss_user_entries SET | |
3858 | unread = true | |
3859 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
3860 | } else { | |
3861 | db_query($link, "UPDATE ttrss_user_entries SET | |
3862 | unread = NOT unread,last_read = NOW() | |
3863 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
3864 | } | |
3865 | } | |
3866 | ||
1f64b1be AD |
3867 | function make_guid_from_title($title) { |
3868 | return preg_replace("/[ \"\',.:;]/", "-", | |
fefef828 | 3869 | mb_strtolower(strip_tags($title), 'utf-8')); |
1f64b1be AD |
3870 | } |
3871 | ||
11befbb2 AD |
3872 | function print_headline_subtoolbar($link, $feed_site_url, $feed_title, |
3873 | $bottom = false, $rtl_content = false, $feed_id = 0, | |
3874 | $is_cat = false, $search = false, $match_on = false, | |
1681df97 AD |
3875 | $search_mode = false, $offset = 0, $limit = 0, |
3876 | $dashboard_menu = 0, $disable_feed = 0, $feed_small_icon = 0) { | |
11befbb2 | 3877 | |
e6c115b2 AD |
3878 | $user_page_offset = $offset + 1; |
3879 | ||
11befbb2 AD |
3880 | if (!$bottom) { |
3881 | $class = "headlinesSubToolbar"; | |
3882 | $tid = "headlineActionsTop"; | |
3883 | } else { | |
3884 | $class = "headlinesSubToolbar"; | |
3885 | $tid = "headlineActionsBottom"; | |
3886 | } | |
3887 | ||
ecf2a265 | 3888 | print "<nobr><table class=\"$class\" id=\"$tid\" |
11befbb2 AD |
3889 | width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>"; |
3890 | ||
3891 | if ($rtl_content) { | |
3892 | $rtl_cpart = "RTL"; | |
3893 | } else { | |
3894 | $rtl_cpart = ""; | |
3895 | } | |
3896 | ||
e6c115b2 AD |
3897 | $page_prev_link = "javascript:viewFeedGoPage(-1)"; |
3898 | $page_next_link = "javascript:viewFeedGoPage(1)"; | |
3899 | $page_first_link = "javascript:viewFeedGoPage(0)"; | |
203de776 | 3900 | |
eb28b131 AD |
3901 | $catchup_page_link = "javascript:catchupPage()"; |
3902 | $catchup_feed_link = "javascript:catchupCurrentFeed()"; | |
a5ae125a | 3903 | $catchup_sel_link = "javascript:catchupSelection()"; |
c6008b62 | 3904 | |
11befbb2 AD |
3905 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) { |
3906 | ||
c6008b62 AD |
3907 | $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)"; |
3908 | $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)"; | |
3909 | $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)"; | |
e75d70b5 | 3910 | $sel_inv_link = "javascript:invertHeadlineSelection()"; |
11befbb2 | 3911 | |
c6008b62 AD |
3912 | $tog_unread_link = "javascript:selectionToggleUnread()"; |
3913 | $tog_marked_link = "javascript:selectionToggleMarked()"; | |
e4f4b46f | 3914 | $tog_published_link = "javascript:selectionTogglePublished()"; |
11befbb2 | 3915 | |
c6008b62 | 3916 | } else { |
11befbb2 | 3917 | |
c6008b62 AD |
3918 | $sel_all_link = "javascript:cdmSelectArticles('all')"; |
3919 | $sel_unread_link = "javascript:cdmSelectArticles('unread')"; | |
3920 | $sel_none_link = "javascript:cdmSelectArticles('none')"; | |
3921 | ||
e75d70b5 AD |
3922 | $sel_inv_link = "javascript:invertHeadlineSelection()"; |
3923 | ||
c6008b62 AD |
3924 | $tog_unread_link = "javascript:selectionToggleUnread(true)"; |
3925 | $tog_marked_link = "javascript:selectionToggleMarked(true)"; | |
e4f4b46f | 3926 | $tog_published_link = "javascript:selectionTogglePublished(true)"; |
c6008b62 AD |
3927 | |
3928 | } | |
3929 | ||
d75ed3eb | 3930 | print "<td class=\"headlineActions$rtl_cpart\">"; |
b8a637f3 | 3931 | |
ceb30ba4 | 3932 | |
d75ed3eb AD |
3933 | print __('Select:')." |
3934 | <a href=\"$sel_all_link\">".__('All')."</a>, | |
3935 | <a href=\"$sel_unread_link\">".__('Unread')."</a>, | |
3936 | <a href=\"$sel_inv_link\">".__('Invert')."</a>, | |
3937 | <a href=\"$sel_none_link\">".__('None')."</a></li>"; | |
bf3c9838 | 3938 | |
d75ed3eb | 3939 | print " "; |
bf3c9838 | 3940 | |
d75ed3eb AD |
3941 | print "<span |
3942 | onmouseover=\"enable_selection(false)\" | |
3943 | onmouseout=\"enable_selection(true)\" | |
3944 | onclick=\"toggleHeadlineActions()\" id=\"headlineActionsDrop\">". | |
3945 | __("Actions...") . " <img src=\"images/down_arrow.png\"> | |
3946 | </span>"; | |
bf3c9838 | 3947 | |
d75ed3eb | 3948 | print "<ul id=\"headlineActionsBody\" style=\"display : none\">"; |
bf3c9838 | 3949 | |
d75ed3eb AD |
3950 | print "<li class=\"insensitive\">".__('Selection toggle:')."</li> |
3951 | <li onclick=\"$tog_unread_link\"> ".__('Unread')."</li> | |
3952 | <li onclick=\"$tog_marked_link\"> ".__('Starred')."</li> | |
3953 | <li onclick=\"$tog_published_link\"> ".__('Published')."</li> | |
3954 | <!-- <li><span class=\"insensitive\">--------</span></li> --> | |
3955 | <li class=\"insensitive\">".__('Mark as read:')."</li> | |
3956 | <li onclick=\"$catchup_sel_link\"> ".__('Selection')."</li>"; | |
bf3c9838 | 3957 | |
d75ed3eb AD |
3958 | print "<li onclick=\"$catchup_feed_link\"> ".__('Entire feed'). |
3959 | "</li>"; | |
bf3c9838 | 3960 | |
d75ed3eb AD |
3961 | //print "<li><span class=\"insensitive\">--------</span></li>"; |
3962 | print "<li class=\"insensitive\">".__('Assign label:')."</li>"; | |
bf3c9838 | 3963 | |
d75ed3eb AD |
3964 | print "<li onclick=\"javascript:addLabel()\"> |
3965 | ".__("Create label...")."</li>"; | |
4f570f56 | 3966 | |
d75ed3eb AD |
3967 | $result = db_query($link, "SELECT id, caption FROM ttrss_labels2 WHERE |
3968 | owner_uid = '".$_SESSION["uid"]."' ORDER BY caption"); | |
bf3c9838 | 3969 | |
d75ed3eb | 3970 | while ($line = db_fetch_assoc($result)) { |
bf3c9838 | 3971 | |
d75ed3eb AD |
3972 | $label_id = $line["id"]; |
3973 | $label_caption = $line["caption"]; | |
bf3c9838 | 3974 | |
d75ed3eb AD |
3975 | if ($feed_id < -10 && $feed_id == -11-$label_id) { |
3976 | print "<li onclick=\"javascript:selectionRemoveLabel($label_id)\"> | |
3977 | $label_caption ".__('(remove)')."</li>"; | |
3978 | } else { | |
3979 | print "<li onclick=\"javascript:selectionAssignLabel($label_id)\"> | |
3980 | $label_caption</li>"; | |
d420f2ee | 3981 | } |
d75ed3eb | 3982 | } |
c6008b62 | 3983 | |
d75ed3eb AD |
3984 | print "</ul>"; |
3985 | print "</td>"; | |
bf3c9838 | 3986 | |
11befbb2 | 3987 | print "<td class=\"headlineTitle$rtl_cpart\">"; |
11befbb2 | 3988 | |
7a822893 | 3989 | print "<span id=\"subtoolbar_ftitle\">"; |
11befbb2 | 3990 | |
b27967ac AD |
3991 | if ($feed_site_url) { |
3992 | if (!$bottom) { | |
e944346c | 3993 | $target = "target=\"_blank\""; |
20361063 | 3994 | } |
b27967ac AD |
3995 | print "<a $target href=\"$feed_site_url\">". |
3996 | truncate_string($feed_title,30)."</a>"; | |
3997 | } else { | |
3998 | print $feed_title; | |
3999 | } | |
4000 | ||
4001 | if ($search) { | |
4002 | $search_q = "&q=$search&m=$match_on&smode=$search_mode"; | |
e6c115b2 AD |
4003 | } |
4004 | ||
b27967ac AD |
4005 | if ($user_page_offset > 1) { |
4006 | print " [$user_page_offset] "; | |
4007 | } | |
4008 | ||
1681df97 | 4009 | if (!$bottom && !$disable_feed) { |
e6c115b2 | 4010 | print " |
e944346c | 4011 | <a target=\"_blank\" |
11befbb2 AD |
4012 | href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\"> |
4013 | <img class=\"noborder\" | |
1025ad87 | 4014 | alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\"> |
11befbb2 | 4015 | </a>"; |
1681df97 AD |
4016 | } else if ($feed_small_icon) { |
4017 | print "<img class=\"noborder\" alt=\"\" src=\"images/$feed_small_icon\">"; | |
11befbb2 | 4018 | } |
7a822893 AD |
4019 | |
4020 | print "</span>"; | |
4021 | ||
11befbb2 | 4022 | print "</td>"; |
ecf2a265 | 4023 | print "</tr></table></nobr>"; |
11befbb2 AD |
4024 | |
4025 | } | |
4026 | ||
bba7c4bf AD |
4027 | function printCategoryHeader($link, $cat_id, $hidden = false, $can_browse = true) { |
4028 | ||
4029 | $tmp_category = getCategoryTitle($link, $cat_id); | |
cc914918 AD |
4030 | |
4031 | if ($cat_id > 0) { | |
4032 | $cat_unread = ccache_find($link, $cat_id, $_SESSION["uid"], true); | |
b2531a28 | 4033 | } else if ($cat_id == 0 || $cat_id == -2) { |
cc914918 AD |
4034 | $cat_unread = getCategoryUnread($link, $cat_id); |
4035 | } | |
bba7c4bf AD |
4036 | |
4037 | if ($hidden) { | |
4038 | $holder_style = "display:none;"; | |
66a251f9 | 4039 | $ellipsis = "…"; |
bba7c4bf AD |
4040 | } else { |
4041 | $holder_style = ""; | |
4042 | $ellipsis = ""; | |
4043 | } | |
4044 | ||
4045 | $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread"; | |
4046 | ||
bba7c4bf | 4047 | if ($can_browse) { |
4c41e58f AD |
4048 | $browse_cat_link = "onclick=\"javascript:viewCategory($cat_id)\""; |
4049 | $inner_title_class = "catTitle"; | |
bba7c4bf | 4050 | } else { |
4c41e58f AD |
4051 | $browse_cat_link = ""; |
4052 | $inner_title_class = "catTitleNL"; | |
bba7c4bf AD |
4053 | } |
4054 | ||
2baedabe | 4055 | $cat_class = "feedCat"; |
364e391e AD |
4056 | |
4057 | print "<li class=\"$cat_class\" id=\"FCAT-$cat_id\"> | |
98fb6193 | 4058 | <img onclick=\"toggleCollapseCat($cat_id)\" class=\"catCollapse\" |
4c41e58f AD |
4059 | title=\"".__('Click to collapse category')."\" |
4060 | src=\"images/cat-collapse.png\"><span class=\"$inner_title_class\" | |
4061 | id=\"FCATN-$cat_id\" $browse_cat_link | |
4062 | \">$tmp_category</span>"; | |
4063 | ||
4064 | print "<span id=\"FCAP-$cat_id\">"; | |
4065 | ||
dda1396f | 4066 | print " <span id=\"FCATCTR-$cat_id\" |
bba7c4bf AD |
4067 | class=\"$catctr_class\">($cat_unread)</span> $ellipsis"; |
4068 | ||
4c41e58f | 4069 | print "</span>"; |
bba7c4bf | 4070 | |
782ddd70 | 4071 | //print "</li>"; |
bba7c4bf | 4072 | |
60ea2377 | 4073 | print "<ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>"; |
7abee14f | 4074 | |
bba7c4bf AD |
4075 | } |
4076 | ||
f407c086 AD |
4077 | function outputFeedList($link, $tags = false) { |
4078 | ||
3bd9a780 | 4079 | print "<ul class=\"feedList\" id=\"feedList\">"; |
f407c086 AD |
4080 | |
4081 | $owner_uid = $_SESSION["uid"]; | |
4082 | ||
cf4d339c | 4083 | /* virtual feeds */ |
f407c086 | 4084 | |
cf4d339c | 4085 | if (get_pref($link, 'ENABLE_FEED_CATS')) { |
3b086176 AD |
4086 | |
4087 | if ($_COOKIE["ttrss_vf_vclps"] == 1) { | |
bba7c4bf | 4088 | $cat_hidden = true; |
3b086176 | 4089 | } else { |
bba7c4bf | 4090 | $cat_hidden = false; |
3b086176 AD |
4091 | } |
4092 | ||
bba7c4bf | 4093 | printCategoryHeader($link, -1, $cat_hidden, false); |
cf4d339c | 4094 | } |
f407c086 | 4095 | |
cf4d339c | 4096 | $num_starred = getFeedUnread($link, -1); |
e4f4b46f | 4097 | $num_published = getFeedUnread($link, -2); |
2d24f032 | 4098 | $num_fresh = getFeedUnread($link, -3); |
b2531a28 AD |
4099 | $num_total = getFeedUnread($link, -4); |
4100 | ||
4101 | $class = "virt"; | |
4102 | ||
4103 | if ($num_total > 0) $class .= "Unread"; | |
4104 | ||
4105 | printFeedEntry(-4, $class, __("All articles"), $num_total, | |
4106 | "images/tag.png", $link); | |
2d24f032 AD |
4107 | |
4108 | $class = "virt"; | |
4109 | ||
4110 | if ($num_fresh > 0) $class .= "Unread"; | |
4111 | ||
4112 | printFeedEntry(-3, $class, __("Fresh articles"), $num_fresh, | |
4113 | "images/fresh.png", $link); | |
f407c086 | 4114 | |
cf4d339c | 4115 | $class = "virt"; |
f407c086 | 4116 | |
cf4d339c | 4117 | if ($num_starred > 0) $class .= "Unread"; |
f407c086 | 4118 | |
abd8a516 AD |
4119 | $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false); |
4120 | ||
4121 | if ($is_ie) { | |
4122 | $mark_img_ext = "gif"; | |
4123 | } else { | |
4124 | $mark_img_ext = "png"; | |
4125 | } | |
4126 | ||
d1db26aa | 4127 | printFeedEntry(-1, $class, __("Starred articles"), $num_starred, |
abd8a516 | 4128 | "images/mark_set.$mark_img_ext", $link); |
f407c086 | 4129 | |
e4f4b46f AD |
4130 | $class = "virt"; |
4131 | ||
4132 | if ($num_published > 0) $class .= "Unread"; | |
4133 | ||
4134 | printFeedEntry(-2, $class, __("Published articles"), $num_published, | |
f5e0338d | 4135 | "images/pub_set.gif", $link); |
e4f4b46f | 4136 | |
cf4d339c | 4137 | if (get_pref($link, 'ENABLE_FEED_CATS')) { |
8b803aa2 | 4138 | print "</ul></li>"; |
cf4d339c | 4139 | } |
f407c086 | 4140 | |
cf4d339c | 4141 | if (!$tags) { |
f407c086 | 4142 | |
ceb30ba4 AD |
4143 | |
4144 | $result = db_query($link, "SELECT id,caption FROM | |
4145 | ttrss_labels2 WHERE owner_uid = '$owner_uid' ORDER by caption"); | |
f407c086 AD |
4146 | |
4147 | if (db_num_rows($result) > 0) { | |
4148 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
bd64489f AD |
4149 | |
4150 | if ($_COOKIE["ttrss_vf_lclps"] == 1) { | |
bba7c4bf | 4151 | $cat_hidden = true; |
bd64489f | 4152 | } else { |
bba7c4bf | 4153 | $cat_hidden = false; |
bd64489f AD |
4154 | } |
4155 | ||
e6a38cde | 4156 | printCategoryHeader($link, -2, $cat_hidden, true); |
bd64489f | 4157 | |
f407c086 | 4158 | } else { |
3bd9a780 | 4159 | print "<li><hr></li>"; |
f407c086 AD |
4160 | } |
4161 | } | |
4162 | ||
4163 | while ($line = db_fetch_assoc($result)) { | |
4164 | ||
f407c086 AD |
4165 | $label_id = -$line['id'] - 11; |
4166 | $count = getFeedUnread($link, $label_id); | |
4167 | ||
4168 | $class = "label"; | |
4169 | ||
4170 | if ($count > 0) { | |
4171 | $class .= "Unread"; | |
4172 | } | |
f407c086 AD |
4173 | |
4174 | printFeedEntry($label_id, | |
ceb30ba4 | 4175 | $class, $line["caption"], |
f407c086 AD |
4176 | $count, "images/label.png", $link); |
4177 | ||
4178 | } | |
4179 | ||
4180 | if (db_num_rows($result) > 0) { | |
4181 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
4182 | print "</ul>"; | |
4183 | } | |
ceb30ba4 | 4184 | } |
f407c086 | 4185 | |
f407c086 AD |
4186 | |
4187 | if (!get_pref($link, 'ENABLE_FEED_CATS')) { | |
3bd9a780 | 4188 | print "<li><hr></li>"; |
f407c086 AD |
4189 | } |
4190 | ||
4191 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
4192 | if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) { | |
9d393c84 | 4193 | $order_by_qpart = "order_id,category,unread DESC,title"; |
f407c086 | 4194 | } else { |
9d393c84 | 4195 | $order_by_qpart = "order_id,category,title"; |
f407c086 AD |
4196 | } |
4197 | } else { | |
4198 | if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) { | |
4199 | $order_by_qpart = "unread DESC,title"; | |
4200 | } else { | |
4201 | $order_by_qpart = "title"; | |
4202 | } | |
4203 | } | |
4204 | ||
14073c0a AD |
4205 | $age_qpart = getMaxAgeSubquery(); |
4206 | ||
99509451 | 4207 | $query = "SELECT ttrss_feeds.*, |
fc2b26a6 | 4208 | ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated_noms, |
f407c086 AD |
4209 | cat_id,last_error, |
4210 | ttrss_feed_categories.title AS category, | |
d7135e2a AD |
4211 | ttrss_feed_categories.collapsed, |
4212 | value AS unread | |
4213 | FROM ttrss_feeds LEFT JOIN ttrss_feed_categories | |
4214 | ON (ttrss_feed_categories.id = cat_id) | |
4215 | LEFT JOIN ttrss_counters_cache | |
4216 | ON | |
4217 | (ttrss_feeds.id = feed_id) | |
f407c086 AD |
4218 | WHERE |
4219 | ttrss_feeds.hidden = false AND | |
4220 | ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL | |
99509451 AD |
4221 | ORDER BY $order_by_qpart"; |
4222 | ||
4223 | $result = db_query($link, $query); | |
f407c086 AD |
4224 | |
4225 | $actid = $_GET["actid"]; | |
4226 | ||
4227 | /* real feeds */ | |
4228 | ||
4229 | $lnum = 0; | |
4230 | ||
4231 | $total_unread = 0; | |
4232 | ||
4233 | $category = ""; | |
4234 | ||
4235 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); | |
4236 | ||
4237 | while ($line = db_fetch_assoc($result)) { | |
4238 | ||
47439031 | 4239 | $feed = trim($line["title"]); |
0f39ae20 AD |
4240 | |
4241 | if (!$feed) $feed = "[Untitled]"; | |
4242 | ||
f407c086 | 4243 | $feed_id = $line["id"]; |
d7135e2a | 4244 | $unread = $line["unread"]; |
f407c086 AD |
4245 | |
4246 | $subop = $_GET["subop"]; | |
f407c086 AD |
4247 | |
4248 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
4249 | $last_updated = smart_date_time(strtotime($line["last_updated_noms"])); | |
4250 | } else { | |
4251 | $last_updated = date($short_date, strtotime($line["last_updated_noms"])); | |
4252 | } | |
4253 | ||
4254 | $rtl_content = sql_bool_to_bool($line["rtl_content"]); | |
4255 | ||
4256 | if ($rtl_content) { | |
4257 | $rtl_tag = "dir=\"RTL\""; | |
4258 | } else { | |
4259 | $rtl_tag = ""; | |
4260 | } | |
4261 | ||
4262 | $tmp_result = db_query($link, | |
de0a2122 AD |
4263 | "SELECT SUM(value) AS unread FROM ttrss_feeds, ttrss_counters_cache |
4264 | WHERE parent_feed = '$feed_id' AND feed_id = id"); | |
f407c086 | 4265 | |
de0a2122 AD |
4266 | $unread += db_fetch_result($tmp_result, 0, "unread"); |
4267 | ||
f407c086 AD |
4268 | $cat_id = $line["cat_id"]; |
4269 | ||
4270 | $tmp_category = $line["category"]; | |
4271 | ||
4272 | if (!$tmp_category) { | |
d1db26aa | 4273 | $tmp_category = __("Uncategorized"); |
f407c086 AD |
4274 | } |
4275 | ||
4276 | // $class = ($lnum % 2) ? "even" : "odd"; | |
4277 | ||
4278 | if ($line["last_error"]) { | |
4279 | $class = "error"; | |
4280 | } else { | |
4281 | $class = "feed"; | |
4282 | } | |
4283 | ||
4284 | if ($unread > 0) $class .= "Unread"; | |
4285 | ||
4286 | if ($actid == $feed_id) { | |
4287 | $class .= "Selected"; | |
4288 | } | |
4289 | ||
4290 | $total_unread += $unread; | |
4291 | ||
4292 | if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) { | |
4293 | ||
4294 | if ($category) { | |
8b803aa2 | 4295 | print "</ul></li>"; |
f407c086 AD |
4296 | } |
4297 | ||
4298 | $category = $tmp_category; | |
4299 | ||
7abee14f | 4300 | $collapsed = sql_bool_to_bool($line["collapsed"]); |
f407c086 AD |
4301 | |
4302 | // workaround for NULL category | |
d1db26aa | 4303 | if ($category == __("Uncategorized")) { |
f407c086 AD |
4304 | if ($_COOKIE["ttrss_vf_uclps"] == 1) { |
4305 | $collapsed = "t"; | |
4306 | } | |
4307 | } | |
4308 | ||
f407c086 AD |
4309 | $cat_id = sprintf("%d", $cat_id); |
4310 | ||
7abee14f AD |
4311 | printCategoryHeader($link, $cat_id, $collapsed, true); |
4312 | ||
f407c086 AD |
4313 | } |
4314 | ||
4315 | printFeedEntry($feed_id, $class, $feed, $unread, | |
68dcbd31 | 4316 | ICONS_URL."/$feed_id.ico", $link, $rtl_content, |
f407c086 AD |
4317 | $last_updated, $line["last_error"]); |
4318 | ||
4319 | ++$lnum; | |
4320 | } | |
4321 | ||
4322 | if (db_num_rows($result) == 0) { | |
3bd9a780 | 4323 | print "<li>".__('No feeds to display.')."</li>"; |
f407c086 AD |
4324 | } |
4325 | ||
4326 | } else { | |
4327 | ||
4328 | // tags | |
4329 | ||
4330 | /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count | |
4331 | FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE | |
4332 | post_int_id = ttrss_user_entries.int_id AND | |
4333 | unread = true AND ref_id = ttrss_entries.id | |
4334 | AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name | |
4335 | UNION | |
4336 | select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid' | |
4337 | ORDER BY tag_name"); */ | |
4338 | ||
4339 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
d1db26aa | 4340 | print "<li class=\"feedCat\">".__('Tags')."</li>"; |
60ea2377 | 4341 | print "<ul class=\"feedCatList\">"; |
f407c086 AD |
4342 | } |
4343 | ||
14073c0a AD |
4344 | $age_qpart = getMaxAgeSubquery(); |
4345 | ||
f407c086 | 4346 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) |
14073c0a AD |
4347 | FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id |
4348 | AND ref_id = id AND $age_qpart | |
f407c086 | 4349 | AND unread = true)) AS count FROM ttrss_tags |
ef1ac7c7 AD |
4350 | WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name |
4351 | ORDER BY count DESC LIMIT 50"); | |
f407c086 AD |
4352 | |
4353 | $tags = array(); | |
4354 | ||
4355 | while ($line = db_fetch_assoc($result)) { | |
4356 | $tags[$line["tag_name"]] += $line["count"]; | |
4357 | } | |
4358 | ||
4359 | foreach (array_keys($tags) as $tag) { | |
4360 | ||
4361 | $unread = $tags[$tag]; | |
4362 | ||
4363 | $class = "tag"; | |
4364 | ||
4365 | if ($unread > 0) { | |
4366 | $class .= "Unread"; | |
4367 | } | |
4368 | ||
4369 | printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link); | |
4370 | ||
4371 | } | |
4372 | ||
4373 | if (db_num_rows($result) == 0) { | |
4374 | print "<li>No tags to display.</li>"; | |
4375 | } | |
4376 | ||
4377 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3bd9a780 | 4378 | print "</ul>"; |
f407c086 AD |
4379 | } |
4380 | ||
4381 | } | |
4382 | ||
4383 | print "</ul>"; | |
4384 | ||
4385 | } | |
4386 | ||
bc976a8c | 4387 | function get_article_tags($link, $id, $owner_uid = 0) { |
0b126ac2 AD |
4388 | |
4389 | $a_id = db_escape_string($id); | |
4390 | ||
bc976a8c AD |
4391 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; |
4392 | ||
0c3d1c68 AD |
4393 | $tmp_result = db_query($link, "SELECT DISTINCT tag_name, |
4394 | owner_uid as owner FROM | |
0b126ac2 | 4395 | ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE |
bc976a8c | 4396 | ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name"); |
0b126ac2 AD |
4397 | |
4398 | $tags = array(); | |
4399 | ||
4400 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
4401 | array_push($tags, $tmp_line["tag_name"]); | |
4402 | } | |
4403 | ||
4404 | return $tags; | |
4405 | } | |
4406 | ||
d62a3b63 AD |
4407 | function trim_value(&$value) { |
4408 | $value = trim($value); | |
4409 | } | |
4410 | ||
4411 | function trim_array($array) { | |
4412 | $tmp = $array; | |
4413 | array_walk($tmp, 'trim_value'); | |
4414 | return $tmp; | |
4415 | } | |
4416 | ||
be832a1a | 4417 | function tag_is_valid($tag) { |
ef063748 AD |
4418 | if ($tag == '') return false; |
4419 | if (preg_match("/^[0-9]*$/", $tag)) return false; | |
4420 | ||
31365729 AD |
4421 | if (function_exists('iconv')) { |
4422 | $tag = iconv("utf-8", "utf-8", $tag); | |
4423 | } | |
4424 | ||
ef063748 AD |
4425 | if (!$tag) return false; |
4426 | ||
4427 | return true; | |
be832a1a AD |
4428 | } |
4429 | ||
793185a9 AD |
4430 | function render_login_form($link, $mobile = false) { |
4431 | if (!$mobile) { | |
4432 | require_once "login_form.php"; | |
4433 | } else { | |
4434 | require_once "mobile/login_form.php"; | |
4435 | } | |
01a87dff AD |
4436 | } |
4437 | ||
dc56b3b7 AD |
4438 | // from http://developer.apple.com/internet/safari/faq.html |
4439 | function no_cache_incantation() { | |
4440 | header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :) | |
4441 | header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified | |
4442 | header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1 | |
4443 | header("Cache-Control: post-check=0, pre-check=0", false); | |
4444 | header("Pragma: no-cache"); // HTTP/1.0 | |
4445 | } | |
4446 | ||
42395d28 AD |
4447 | function format_warning($msg, $id = "") { |
4448 | return "<div class=\"warning\" id=\"$id\"> | |
e780d1d2 | 4449 | <img src=\"images/sign_excl.gif\">$msg</div>"; |
0d32b41e AD |
4450 | } |
4451 | ||
4452 | function format_notice($msg) { | |
4453 | return "<div class=\"notice\"> | |
e780d1d2 | 4454 | <img src=\"images/sign_info.gif\">$msg</div>"; |
0d32b41e AD |
4455 | } |
4456 | ||
68d2f95e AD |
4457 | function format_error($msg) { |
4458 | return "<div class=\"error\"> | |
e780d1d2 | 4459 | <img src=\"images/sign_excl.gif\">$msg</div>"; |
68d2f95e AD |
4460 | } |
4461 | ||
4dccf1ed AD |
4462 | function print_notice($msg) { |
4463 | return print format_notice($msg); | |
4464 | } | |
4465 | ||
4466 | function print_warning($msg) { | |
4467 | return print format_warning($msg); | |
4468 | } | |
4469 | ||
68d2f95e AD |
4470 | function print_error($msg) { |
4471 | return print format_error($msg); | |
4472 | } | |
4473 | ||
4474 | ||
4dccf1ed AD |
4475 | function T_sprintf() { |
4476 | $args = func_get_args(); | |
4477 | return vsprintf(__(array_shift($args)), $args); | |
4478 | } | |
4479 | ||
eedfb635 AD |
4480 | function outputArticleXML($link, $id, $feed_id, $mark_as_read = true, |
4481 | $zoom_mode = false) { | |
3de0261a | 4482 | |
10eb9da8 AD |
4483 | /* we can figure out feed_id from article id anyway, why do we |
4484 | * pass feed_id here? */ | |
4485 | ||
4486 | $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries | |
4487 | WHERE ref_id = '$id'"); | |
4488 | ||
4489 | $feed_id = db_fetch_result($result, 0, "feed_id"); | |
4490 | ||
eedfb635 | 4491 | if (!$zoom_mode) { print "<article id='$id'><![CDATA["; }; |
3de0261a AD |
4492 | |
4493 | $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds | |
4494 | WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]); | |
4495 | ||
4496 | if (db_num_rows($result) == 1) { | |
4497 | $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content")); | |
4498 | } else { | |
4499 | $rtl_content = false; | |
4500 | } | |
4501 | ||
4502 | if ($rtl_content) { | |
4503 | $rtl_tag = "dir=\"RTL\""; | |
4504 | $rtl_class = "RTL"; | |
4505 | } else { | |
4506 | $rtl_tag = ""; | |
4507 | $rtl_class = ""; | |
4508 | } | |
4509 | ||
4510 | if ($mark_as_read) { | |
4511 | $result = db_query($link, "UPDATE ttrss_user_entries | |
4512 | SET unread = false,last_read = NOW() | |
4513 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
8a4c759e AD |
4514 | |
4515 | ccache_update($link, $feed_id, $_SESSION["uid"]); | |
3de0261a AD |
4516 | } |
4517 | ||
4518 | $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id, | |
fc2b26a6 | 4519 | ".SUBSTRING_FOR_DATE."(updated,1,16) as updated, |
3de0261a AD |
4520 | (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url, |
4521 | num_comments, | |
4522 | author | |
4523 | FROM ttrss_entries,ttrss_user_entries | |
4524 | WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]); | |
4525 | ||
4526 | if ($result) { | |
4527 | ||
4528 | $link_target = ""; | |
4529 | ||
4530 | if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) { | |
e944346c | 4531 | $link_target = "target=\"_blank\""; |
3de0261a AD |
4532 | } |
4533 | ||
4534 | $line = db_fetch_assoc($result); | |
4535 | ||
4536 | if ($line["icon_url"]) { | |
4537 | $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">"; | |
4538 | } else { | |
4539 | $feed_icon = " "; | |
4540 | } | |
4541 | ||
3de0261a AD |
4542 | $num_comments = $line["num_comments"]; |
4543 | $entry_comments = ""; | |
4544 | ||
4545 | if ($num_comments > 0) { | |
4546 | if ($line["comments"]) { | |
4547 | $comments_url = $line["comments"]; | |
4548 | } else { | |
4549 | $comments_url = $line["link"]; | |
4550 | } | |
4551 | $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>"; | |
4552 | } else { | |
4553 | if ($line["comments"] && $line["link"] != $line["comments"]) { | |
4554 | $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>"; | |
4555 | } | |
4556 | } | |
4557 | ||
eedfb635 AD |
4558 | if ($zoom_mode) { |
4559 | header("Content-Type: text/html"); | |
4560 | print "<html><head> | |
5bb0cc8e | 4561 | <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/> |
eedfb635 AD |
4562 | <title>Tiny Tiny RSS - ".$line["title"]."</title> |
4563 | <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss.css\"> | |
4564 | </head><body>"; | |
4565 | } | |
4566 | ||
4567 | ||
3de0261a AD |
4568 | print "<div class=\"postReply\">"; |
4569 | ||
94047498 AD |
4570 | print "<div class=\"postHeader\" onmouseover=\"enable_resize(true)\" |
4571 | onmouseout=\"enable_resize(false)\">"; | |
3de0261a AD |
4572 | |
4573 | $entry_author = $line["author"]; | |
4574 | ||
4575 | if ($entry_author) { | |
60164936 | 4576 | $entry_author = __(" - ") . $entry_author; |
3de0261a AD |
4577 | } |
4578 | ||
4579 | $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'), | |
4580 | strtotime($line["updated"])); | |
4581 | ||
4582 | print "<div class=\"postDate$rtl_class\">$parsed_updated</div>"; | |
4583 | ||
4584 | if ($line["link"]) { | |
4585 | print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" . | |
06202d88 | 4586 | $line["title"] . "</a><span class='author'>$entry_author</span></div>"; |
3de0261a AD |
4587 | } else { |
4588 | print "<div clear='both'>" . $line["title"] . "$entry_author</div>"; | |
4589 | } | |
4590 | ||
9cfd409d | 4591 | $tags = get_article_tags($link, $id); |
3de0261a AD |
4592 | |
4593 | $tags_str = ""; | |
eedfb635 | 4594 | $tags_nolinks_str = ""; |
3de0261a AD |
4595 | $f_tags_str = ""; |
4596 | ||
4597 | $num_tags = 0; | |
4598 | ||
20361063 AD |
4599 | if ($_SESSION["theme"] == "3pane") { |
4600 | $tag_limit = 3; | |
4601 | } else { | |
4602 | $tag_limit = 6; | |
4603 | } | |
4604 | ||
9cfd409d | 4605 | foreach ($tags as $tag) { |
3de0261a | 4606 | $num_tags++; |
14b6c54b AD |
4607 | $tag_escaped = str_replace("'", "\\'", $tag); |
4608 | ||
4609 | $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, "; | |
3de0261a | 4610 | |
20361063 | 4611 | if ($num_tags == $tag_limit) { |
66a251f9 | 4612 | $tags_str .= "…"; |
eedfb635 | 4613 | $tags_nolinks_str .= "…"; |
e7544143 | 4614 | |
20361063 | 4615 | } else if ($num_tags < $tag_limit) { |
3de0261a | 4616 | $tags_str .= $tag_str; |
eedfb635 | 4617 | $tags_nolinks_str .= "$tag, "; |
3de0261a AD |
4618 | } |
4619 | $f_tags_str .= $tag_str; | |
4620 | } | |
4621 | ||
4622 | $tags_str = preg_replace("/, $/", "", $tags_str); | |
eedfb635 | 4623 | $tags_nolinks_str = preg_replace("/, $/", "", $tags_nolinks_str); |
3de0261a AD |
4624 | $f_tags_str = preg_replace("/, $/", "", $f_tags_str); |
4625 | ||
66a251f9 | 4626 | $all_tags_div = "<span class='cdmAllTagsCtr'>…<div class='cdmAllTags'>All Tags: $f_tags_str</div></span>"; |
e7544143 AD |
4627 | $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str); |
4628 | ||
3de0261a AD |
4629 | if (!$entry_comments) $entry_comments = " "; # placeholder |
4630 | ||
4631 | if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>'; | |
eedfb635 | 4632 | if (!$tags_nolinks_str) $tags_nolinks_str = '<span class="tagList">'.__('no tags').'</span>'; |
3de0261a | 4633 | |
5f014cf1 | 4634 | print "<div style='float : right'> |
eedfb635 AD |
4635 | <img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>"; |
4636 | ||
4637 | if (!$zoom_mode) { | |
4638 | print "$tags_str | |
4639 | <a title=\"".__('Edit tags for this article')."\" | |
4710e3dc AD |
4640 | href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a>"; |
4641 | ||
4642 | if (defined('_ENABLE_INLINE_VIEW')) { | |
4643 | ||
4644 | print "<img src=\"images/art-inline.png\" class='tagsPic' | |
98fe7044 | 4645 | style=\"cursor : pointer\" style=\"cursor : pointer\" |
4710e3dc AD |
4646 | onclick=\"showOriginalArticleInline($id)\" |
4647 | alt='Inline' title='".__('Display original article content')."'>"; | |
4648 | ||
4649 | } | |
4650 | ||
4651 | print "<img src=\"images/art-zoom.png\" class='tagsPic' | |
98fe7044 | 4652 | style=\"cursor : pointer\" style=\"cursor : pointer\" |
eedfb635 AD |
4653 | onclick=\"zoomToArticle($id)\" |
4654 | alt='Zoom' title='".__('Show article summary in new window')."'>"; | |
4655 | } else { | |
4656 | print "$tags_nolinks_str"; | |
4657 | } | |
4658 | print "</div>"; | |
4659 | print "<div clear='both'>$entry_comments</div>"; | |
3de0261a AD |
4660 | |
4661 | print "</div>"; | |
4662 | ||
4663 | print "<div class=\"postIcon\">" . $feed_icon . "</div>"; | |
64ab16ac | 4664 | |
3de0261a | 4665 | print "<div class=\"postContent\">"; |
64ab16ac | 4666 | |
e7544143 | 4667 | #print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>"; |
3de0261a | 4668 | |
c54526fe | 4669 | $article_content = sanitize_rss($link, $line["content"]); |
c41890b0 | 4670 | |
3de0261a | 4671 | if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) { |
c54526fe AD |
4672 | $article_content = preg_replace("/href=/i", "target=\"_blank\" href=", |
4673 | $article_content); | |
3de0261a AD |
4674 | } |
4675 | ||
c54526fe | 4676 | print $article_content; |
ce53e200 AD |
4677 | |
4678 | $result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE | |
4752b041 | 4679 | post_id = '$id' AND content_url != ''"); |
ce53e200 AD |
4680 | |
4681 | if (db_num_rows($result) > 0) { | |
ce53e200 | 4682 | |
9c5ee7e1 | 4683 | $entries_html = array(); |
ce53e200 AD |
4684 | $entries = array(); |
4685 | ||
4686 | while ($line = db_fetch_assoc($result)) { | |
4687 | ||
4688 | $url = $line["content_url"]; | |
4752b041 AD |
4689 | $ctype = $line["content_type"]; |
4690 | ||
4691 | if (!$ctype) $ctype = __("unknown type"); | |
ce53e200 AD |
4692 | |
4693 | $filename = substr($url, strrpos($url, "/")+1); | |
4694 | ||
8dccabed AD |
4695 | $entry = ""; |
4696 | ||
b84b68d9 | 4697 | if (($ctype == __("audio/mpeg")) && |
8dccabed AD |
4698 | (get_pref($link, "ENABLE_FLASH_PLAYER")) ) { |
4699 | ||
39e865fa | 4700 | $entry .= "<object type=\"application/x-shockwave-flash\" data=\"extras/button/musicplayer.swf?song_url=$url\" width=\"17\" height=\"17\"> <param name=\"movie\" value=\"extras/button/musicplayer.swf?song_url=$url\" /> </object> "; |
8dccabed AD |
4701 | |
4702 | } | |
4703 | ||
4704 | $entry .= "<a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" . | |
4752b041 | 4705 | $filename . " (" . $ctype . ")" . "</a>"; |
ce53e200 | 4706 | |
9c5ee7e1 AD |
4707 | array_push($entries_html, $entry); |
4708 | ||
4709 | $entry = array(); | |
4710 | ||
4711 | $entry["type"] = $ctype; | |
4712 | $entry["filename"] = $filename; | |
4713 | $entry["url"] = $url; | |
4714 | ||
ce53e200 AD |
4715 | array_push($entries, $entry); |
4716 | } | |
4717 | ||
9c5ee7e1 AD |
4718 | print "<div class=\"postEnclosures\">"; |
4719 | ||
c54526fe | 4720 | if (!preg_match("/img/i", $article_content)) { |
9c5ee7e1 AD |
4721 | foreach ($entries as $entry) { |
4722 | if (preg_match("/image/", $entry["type"])) { | |
4723 | print "<p><img | |
4724 | alt=\"".htmlspecialchars($entry["filename"])."\" | |
4725 | src=\"" .htmlspecialchars($entry["url"]) . "\"></p>"; | |
4726 | } | |
4727 | } | |
4728 | } | |
4729 | ||
4730 | print "<div class=\"postEnclosures\">"; | |
4731 | ||
4732 | if (db_num_rows($result) == 1) { | |
4733 | print __("Attachment:") . " "; | |
4734 | } else { | |
4735 | print __("Attachments:") . " "; | |
4736 | } | |
4737 | ||
4738 | print join(", ", $entries_html); | |
ce53e200 AD |
4739 | |
4740 | print "</div>"; | |
4741 | } | |
4742 | ||
4743 | print "</div>"; | |
3de0261a AD |
4744 | |
4745 | print "</div>"; | |
4746 | ||
4747 | } | |
4748 | ||
eedfb635 AD |
4749 | if (!$zoom_mode) { |
4750 | print "]]></article>"; | |
4751 | } else { | |
4752 | print " | |
4753 | <div style=\"text-align : center\"> | |
4754 | <input type=\"submit\" onclick=\"return window.close()\" | |
4755 | value=\"".__("Close this window")."\"></div>"; | |
4756 | print "</body></html>"; | |
4757 | ||
4758 | } | |
3de0261a AD |
4759 | |
4760 | } | |
4761 | ||
4762 | function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view, | |
7b4d02a8 AD |
4763 | $next_unread_feed, $offset, $vgr_last_feed = false, |
4764 | $override_order = false) { | |
3de0261a | 4765 | |
52d7e7da AD |
4766 | $disable_cache = false; |
4767 | ||
46921916 AD |
4768 | $timing_info = getmicrotime(); |
4769 | ||
961f4c73 AD |
4770 | $topmost_article_ids = array(); |
4771 | ||
ac541432 AD |
4772 | if (!$offset) { |
4773 | $offset = 0; | |
4774 | } | |
3de0261a AD |
4775 | |
4776 | if ($subop == "undefined") $subop = ""; | |
4777 | ||
a9bcfb8f AD |
4778 | $subop_split = split(":", $subop); |
4779 | ||
3de0261a AD |
4780 | if ($subop == "CatchupSelected") { |
4781 | $ids = split(",", db_escape_string($_GET["ids"])); | |
4782 | $cmode = sprintf("%d", $_GET["cmode"]); | |
4783 | ||
4784 | catchupArticlesById($link, $ids, $cmode); | |
4785 | } | |
4786 | ||
4787 | if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) { | |
35bf080c | 4788 | update_generic_feed($link, $feed, $cat_view, true); |
3de0261a AD |
4789 | } |
4790 | ||
4791 | if ($subop == "MarkAllRead") { | |
4792 | catchup_feed($link, $feed, $cat_view); | |
4793 | ||
4794 | if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) { | |
4795 | if ($next_unread_feed) { | |
4796 | $feed = $next_unread_feed; | |
4797 | } | |
4798 | } | |
4799 | } | |
4800 | ||
a9bcfb8f AD |
4801 | if ($subop_split[0] == "MarkAllReadGR") { |
4802 | catchup_feed($link, $subop_split[1], false); | |
4803 | } | |
4804 | ||
4805 | ||
3de0261a AD |
4806 | if ($feed_id > 0) { |
4807 | $result = db_query($link, | |
4808 | "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1"); | |
4809 | ||
4810 | if (db_num_rows($result) == 0) { | |
4811 | print "<div align='center'>".__('Feed not found.')."</div>"; | |
4812 | return; | |
4813 | } | |
4814 | } | |
4815 | ||
4816 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
10eb9da8 | 4817 | |
3de0261a AD |
4818 | $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds |
4819 | WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
4820 | ||
4821 | if (db_num_rows($result) == 1) { | |
4822 | $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content")); | |
4823 | } else { | |
4824 | $rtl_content = false; | |
4825 | } | |
4826 | ||
4827 | if ($rtl_content) { | |
4828 | $rtl_tag = "dir=\"RTL\""; | |
4829 | } else { | |
4830 | $rtl_tag = ""; | |
4831 | } | |
4832 | } else { | |
4833 | $rtl_tag = ""; | |
4834 | $rtl_content = false; | |
4835 | } | |
4836 | ||
4837 | $script_dt_add = get_script_dt_add(); | |
4838 | ||
4839 | /// START ///////////////////////////////////////////////////////////////////////////////// | |
4840 | ||
4841 | $search = db_escape_string($_GET["query"]); | |
52d7e7da AD |
4842 | |
4843 | if ($search) { | |
4844 | $disable_cache = true; | |
4845 | } | |
4846 | ||
3de0261a AD |
4847 | $search_mode = db_escape_string($_GET["search_mode"]); |
4848 | $match_on = db_escape_string($_GET["match_on"]); | |
4849 | ||
4850 | if (!$match_on) { | |
4851 | $match_on = "both"; | |
4852 | } | |
4853 | ||
4854 | $real_offset = $offset * $limit; | |
4855 | ||
46921916 AD |
4856 | if ($_GET["debug"]) $timing_info = print_checkpoint("H0", $timing_info); |
4857 | ||
3de0261a | 4858 | $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, |
7b4d02a8 | 4859 | $search, $search_mode, $match_on, $override_order, $real_offset); |
3de0261a | 4860 | |
46921916 AD |
4861 | if ($_GET["debug"]) $timing_info = print_checkpoint("H1", $timing_info); |
4862 | ||
3de0261a AD |
4863 | $result = $qfh_ret[0]; |
4864 | $feed_title = $qfh_ret[1]; | |
4865 | $feed_site_url = $qfh_ret[2]; | |
4866 | $last_error = $qfh_ret[3]; | |
f56e3080 | 4867 | |
081e527d AD |
4868 | $vgroup_last_feed = $vgr_last_feed; |
4869 | ||
f56e3080 AD |
4870 | if ($feed == -2) { |
4871 | $feed_site_url = article_publish_url($link); | |
4872 | } | |
4873 | ||
3de0261a AD |
4874 | /// STOP ////////////////////////////////////////////////////////////////////////////////// |
4875 | ||
ac541432 AD |
4876 | if (!$offset) { |
4877 | print "<div id=\"headlinesContainer\" $rtl_tag>"; | |
3de0261a | 4878 | |
ac541432 AD |
4879 | if (!$result) { |
4880 | print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>"; | |
4881 | return; | |
4882 | } | |
3de0261a | 4883 | |
ac541432 AD |
4884 | print_headline_subtoolbar($link, $feed_site_url, $feed_title, false, |
4885 | $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode, | |
4886 | $offset, $limit); | |
3de0261a | 4887 | |
ac541432 AD |
4888 | print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">"; |
4889 | } | |
3de0261a | 4890 | |
29dfb258 AD |
4891 | $headlines_count = db_num_rows($result); |
4892 | ||
3de0261a AD |
4893 | if (db_num_rows($result) > 0) { |
4894 | ||
4895 | # print "\{$offset}"; | |
4896 | ||
ac541432 | 4897 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) { |
3de0261a AD |
4898 | print "<table class=\"headlinesList\" id=\"headlinesList\" |
4899 | cellspacing=\"0\">"; | |
4900 | } | |
4901 | ||
4ab4d364 AD |
4902 | $lnum = $limit*$offset; |
4903 | ||
3de0261a AD |
4904 | error_reporting (DEFAULT_ERROR_LEVEL); |
4905 | ||
4906 | $num_unread = 0; | |
6cfea5c7 AD |
4907 | $cur_feed_title = ''; |
4908 | ||
3de0261a AD |
4909 | while ($line = db_fetch_assoc($result)) { |
4910 | ||
4911 | $class = ($lnum % 2) ? "even" : "odd"; | |
4912 | ||
4913 | $id = $line["id"]; | |
4914 | $feed_id = $line["feed_id"]; | |
961f4c73 | 4915 | |
e2549229 | 4916 | $labels = get_article_labels($link, $id); |
f9247195 | 4917 | $labels_str = "<span id=\"HLLCTR-$id\">"; |
e2549229 AD |
4918 | |
4919 | foreach ($labels as $l) { | |
4920 | $labels_str .= "<span | |
4921 | class='hlLabelRef'>". | |
4922 | $l[1]."</span>"; | |
4923 | } | |
4924 | ||
f9247195 AD |
4925 | $labels_str .= "</span>"; |
4926 | ||
961f4c73 AD |
4927 | if (count($topmost_article_ids) < 5) { |
4928 | array_push($topmost_article_ids, $id); | |
4929 | } | |
4930 | ||
3de0261a AD |
4931 | if ($line["last_read"] == "" && |
4932 | ($line["unread"] != "t" && $line["unread"] != "1")) { | |
4933 | ||
4934 | $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\" | |
4935 | alt=\"Updated\">"; | |
4936 | } else { | |
4937 | $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\" | |
4938 | alt=\"Updated\">"; | |
4939 | } | |
4940 | ||
4941 | if ($line["unread"] == "t" || $line["unread"] == "1") { | |
4942 | $class .= "Unread"; | |
4943 | ++$num_unread; | |
4944 | $is_unread = true; | |
4945 | } else { | |
4946 | $is_unread = false; | |
4947 | } | |
abd8a516 AD |
4948 | |
4949 | $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false); | |
4950 | ||
4951 | if ($is_ie) { | |
4952 | $mark_img_ext = "gif"; | |
4953 | } else { | |
4954 | $mark_img_ext = "png"; | |
4955 | } | |
4956 | ||
3de0261a | 4957 | if ($line["marked"] == "t" || $line["marked"] == "1") { |
abd8a516 | 4958 | $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.$mark_img_ext\" |
3de0261a | 4959 | class=\"markedPic\" |
f5e0338d | 4960 | alt=\"Unstar article\" onclick='javascript:tMark($id)'>"; |
3de0261a | 4961 | } else { |
abd8a516 | 4962 | $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.$mark_img_ext\" |
3de0261a | 4963 | class=\"markedPic\" |
f5e0338d | 4964 | alt=\"Star article\" onclick='javascript:tMark($id)'>"; |
3de0261a AD |
4965 | } |
4966 | ||
e4f4b46f | 4967 | if ($line["published"] == "t" || $line["published"] == "1") { |
f5e0338d | 4968 | $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.gif\" |
e4f4b46f | 4969 | class=\"markedPic\" |
f5e0338d | 4970 | alt=\"Unpublish article\" onclick='javascript:tPub($id)'>"; |
e4f4b46f | 4971 | } else { |
f5e0338d | 4972 | $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.gif\" |
e4f4b46f | 4973 | class=\"markedPic\" |
f5e0338d | 4974 | alt=\"Publish article\" onclick='javascript:tPub($id)'>"; |
e4f4b46f AD |
4975 | } |
4976 | ||
e944346c | 4977 | # $content_link = "<a target=\"_blank\" href=\"".$line["link"]."\">" . |
3de0261a AD |
4978 | # $line["title"] . "</a>"; |
4979 | ||
f0971fc1 AD |
4980 | # $content_link = "<a |
4981 | # href=\"" . htmlspecialchars($line["link"]) . "\" | |
4982 | # onclick=\"view($id,$feed_id);\">" . | |
4983 | # $line["title"] . "</a>"; | |
3de0261a AD |
4984 | |
4985 | # $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" . | |
4986 | # $line["title"] . "</a>"; | |
4987 | ||
4988 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
46921916 | 4989 | $updated_fmt = smart_date_time(strtotime($line["updated_noms"])); |
3de0261a AD |
4990 | } else { |
4991 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); | |
46921916 | 4992 | $updated_fmt = date($short_date, strtotime($line["updated_noms"])); |
3de0261a AD |
4993 | } |
4994 | ||
4995 | if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) { | |
4996 | $content_preview = truncate_string(strip_tags($line["content_preview"]), | |
4997 | 100); | |
4998 | } | |
4999 | ||
ff6e357a AD |
5000 | $score = $line["score"]; |
5001 | ||
1e36af0c | 5002 | $score_pic = get_score_pic($score); |
546499a9 | 5003 | |
9bf3f101 AD |
5004 | /* $score_title = __("(Click to change)"); |
5005 | $score_pic = "<img class='hlScorePic' src=\"images/$score_pic\" | |
5006 | onclick=\"adjustArticleScore($id, $score)\" title=\"$score $score_title\">"; */ | |
546499a9 | 5007 | |
5daa24f2 | 5008 | $score_pic = "<img class='hlScorePic' src=\"images/$score_pic\" |
9bf3f101 | 5009 | title=\"$score\">"; |
ff6e357a | 5010 | |
5daa24f2 AD |
5011 | if ($score > 500) { |
5012 | $hlc_suffix = "H"; | |
5013 | } else if ($score < -100) { | |
5014 | $hlc_suffix = "L"; | |
5015 | } else { | |
5016 | $hlc_suffix = ""; | |
5017 | } | |
5018 | ||
3de0261a AD |
5019 | $entry_author = $line["author"]; |
5020 | ||
5021 | if ($entry_author) { | |
60164936 | 5022 | $entry_author = " - $entry_author"; |
3de0261a AD |
5023 | } |
5024 | ||
7defa089 | 5025 | $has_feed_icon = feed_has_icon($feed_id); |
bd51294a AD |
5026 | |
5027 | if ($has_feed_icon) { | |
5028 | $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"".ICONS_URL."/$feed_id.ico\" alt=\"\">"; | |
5029 | } else { | |
5030 | //$feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\" alt=\"\">"; | |
20be0cf8 | 5031 | $feed_icon_img = ""; |
bd51294a AD |
5032 | } |
5033 | ||
3de0261a | 5034 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) { |
6cfea5c7 | 5035 | |
d00f22ac | 5036 | if (get_pref($link, 'VFEED_GROUP_BY_FEED')) { |
bb031f91 | 5037 | if ($feed_id != $vgroup_last_feed && $line["feed_title"]) { |
a9bcfb8f AD |
5038 | |
5039 | $cur_feed_title = $line["feed_title"]; | |
081e527d | 5040 | $vgroup_last_feed = $feed_id; |
a9bcfb8f | 5041 | |
962d8ba4 | 5042 | $cur_feed_title = htmlspecialchars($cur_feed_title); |
43fc671f | 5043 | |
338ce36c | 5044 | $vf_catchup_link = "(<a onclick='javascript:catchupFeedInGroup($feed_id);' href='#'>mark as read</a>)"; |
a9bcfb8f | 5045 | |
6cfea5c7 | 5046 | print "<tr class='feedTitle'><td colspan='7'>". |
dc803347 | 5047 | "<div style=\"float : right\">$feed_icon_img</div>". |
6cfea5c7 | 5048 | "<a href=\"javascript:viewfeed($feed_id, '', false)\">". |
965fb2af | 5049 | $line["feed_title"]."</a> $vf_catchup_link</td></tr>"; |
6cfea5c7 AD |
5050 | } |
5051 | } | |
314fcd2b AD |
5052 | |
5053 | $mouseover_attrs = "onmouseover='postMouseIn($id)' | |
5054 | onmouseout='postMouseOut($id)'"; | |
5055 | ||
5056 | print "<tr class='$class' id='RROW-$id' $mouseover_attrs>"; | |
3de0261a | 5057 | |
67343d9f | 5058 | print "<td class='hlUpdPic'>$update_pic</td>"; |
3de0261a AD |
5059 | |
5060 | print "<td class='hlSelectRow'> | |
67343d9f AD |
5061 | <input type=\"checkbox\" onclick=\"tSR(this)\" |
5062 | id=\"RCHK-$id\"> | |
3de0261a AD |
5063 | </td>"; |
5064 | ||
5065 | print "<td class='hlMarkedPic'>$marked_pic</td>"; | |
e4f4b46f | 5066 | print "<td class='hlMarkedPic'>$published_pic</td>"; |
3de0261a | 5067 | |
df456bb0 AD |
5068 | # if ($line["feed_title"]) { |
5069 | # print "<td class='hlContent'>$content_link</td>"; | |
5070 | # print "<td class='hlFeed'> | |
5071 | # <a href=\"javascript:viewfeed($feed_id, '', false)\">". | |
5072 | # truncate_string($line["feed_title"],30)."</a> </td>"; | |
5073 | # } else { | |
5074 | ||
f0971fc1 | 5075 | print "<td onclick='view($id,$feed_id)' class='hlContent$hlc_suffix' valign='middle'>"; |
df456bb0 | 5076 | |
f0971fc1 AD |
5077 | print "<a id=\"RTITLE-$id\" |
5078 | href=\"" . htmlspecialchars($line["link"]) . "\" | |
a7764e51 | 5079 | onclick=\"return view($id,$feed_id);\">" . |
df456bb0 AD |
5080 | $line["title"]; |
5081 | ||
5082 | if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) { | |
5083 | if ($content_preview) { | |
5084 | print "<span class=\"contentPreview\"> - $content_preview</span>"; | |
3de0261a | 5085 | } |
3de0261a | 5086 | } |
df456bb0 AD |
5087 | |
5088 | print "</a>"; | |
5089 | ||
e2549229 AD |
5090 | print $labels_str; |
5091 | ||
df456bb0 AD |
5092 | # <a href=\"javascript:viewfeed($feed_id, '', false)\">". |
5093 | # $line["feed_title"]."</a> | |
5094 | ||
d00f22ac | 5095 | if (!get_pref($link, 'VFEED_GROUP_BY_FEED')) { |
6cfea5c7 AD |
5096 | if ($line["feed_title"]) { |
5097 | print "<span class=\"hlFeed\"> | |
5098 | (<a href=\"javascript:viewfeed($feed_id, '', false)\">". | |
5099 | $line["feed_title"]."</a>) | |
5100 | </span>"; | |
5101 | } | |
df456bb0 | 5102 | } |
df456bb0 | 5103 | print "</td>"; |
bd51294a | 5104 | |
df456bb0 | 5105 | # } |
3de0261a | 5106 | |
d7e83df7 | 5107 | print "<td class=\"hlUpdated\" onclick='view($id,$feed_id)'><nobr>$updated_fmt </nobr></td>"; |
546499a9 AD |
5108 | |
5109 | print "<td class='hlMarkedPic'>$score_pic</td>"; | |
bd51294a AD |
5110 | |
5111 | if ($line["feed_title"] && !get_pref($link, 'VFEED_GROUP_BY_FEED')) { | |
d7e83df7 | 5112 | print "<td onclick=\"viewfeed($feed_id)\" class=\"hlFeedIcon\">$feed_icon_img</td>"; |
bd51294a AD |
5113 | } |
5114 | ||
3de0261a AD |
5115 | print "</tr>"; |
5116 | ||
5117 | } else { | |
6cfea5c7 | 5118 | |
bb031f91 | 5119 | if (get_pref($link, 'VFEED_GROUP_BY_FEED') && $line["feed_title"]) { |
081e527d AD |
5120 | if ($feed_id != $vgroup_last_feed) { |
5121 | ||
5122 | $cur_feed_title = $line["feed_title"]; | |
5123 | $vgroup_last_feed = $feed_id; | |
5124 | ||
962d8ba4 AD |
5125 | $cur_feed_title = htmlspecialchars($cur_feed_title); |
5126 | ||
338ce36c | 5127 | $vf_catchup_link = "(<a onclick='javascript:catchupFeedInGroup($feed_id);' href='#'>mark as read</a>)"; |
081e527d | 5128 | |
7defa089 | 5129 | $has_feed_icon = feed_has_icon($feed_id); |
dc803347 AD |
5130 | |
5131 | if ($has_feed_icon) { | |
5132 | $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"".ICONS_URL."/$feed_id.ico\" alt=\"\">"; | |
5133 | } else { | |
5134 | //$feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\" alt=\"\">"; | |
5135 | } | |
5136 | ||
6cfea5c7 | 5137 | print "<div class='cdmFeedTitle'>". |
dc803347 | 5138 | "<div style=\"float : right\">$feed_icon_img</div>". |
6cfea5c7 | 5139 | "<a href=\"javascript:viewfeed($feed_id, '', false)\">". |
081e527d | 5140 | $line["feed_title"]."</a> $vf_catchup_link</div>"; |
6cfea5c7 AD |
5141 | } |
5142 | } | |
5143 | ||
3de0261a AD |
5144 | if ($is_unread) { |
5145 | $add_class = "Unread"; | |
5146 | } else { | |
5147 | $add_class = ""; | |
5148 | } | |
0cacc891 AD |
5149 | |
5150 | $expand_cdm = get_pref($link, 'CDM_EXPANDED'); | |
3cd4239a | 5151 | $show_excerpt = false; |
0cacc891 | 5152 | |
5daa24f2 | 5153 | if ($expand_cdm && $score >= -100) { |
0cacc891 | 5154 | $cdm_cstyle = ""; |
3cd4239a | 5155 | $show_excerpt = false; |
0cacc891 AD |
5156 | } else { |
5157 | $cdm_cstyle = "style=\"display : none\""; | |
3cd4239a | 5158 | $show_excerpt = true; |
0cacc891 AD |
5159 | } |
5160 | ||
314fcd2b AD |
5161 | $mouseover_attrs = "onmouseover='postMouseIn($id)' |
5162 | onmouseout='postMouseOut($id)'"; | |
5163 | ||
e4914b62 | 5164 | print "<div class=\"cdmArticle$add_class\" |
3cd4239a | 5165 | id=\"RROW-$id\" |
314fcd2b | 5166 | $mouseover_attrs'>"; |
3de0261a AD |
5167 | |
5168 | print "<div class=\"cdmHeader\">"; | |
5169 | ||
965fb2af AD |
5170 | if (!get_pref($link, "VFEED_GROUP_BY_FEED") || !$line["feed_title"]) { |
5171 | $cdm_feed_icon = "<span style=\"cursor : pointer\" onclick=\"viewfeed($feed_id)\">$feed_icon_img</span>"; | |
5172 | } | |
5173 | ||
5174 | print "<div class=\"articleUpdated\">$updated_fmt $score_pic $cdm_feed_icon | |
b3296d36 | 5175 | </div>"; |
5daa24f2 | 5176 | |
9617eb94 | 5177 | print "<span id=\"RTITLE-$id\" class=\"titleWrap$hlc_suffix\"><a class=\"title\" |
3de0261a | 5178 | onclick=\"javascript:toggleUnread($id, 0)\" |
5daa24f2 AD |
5179 | target=\"_blank\" href=\"".$line["link"]."\">".$line["title"]."</a> |
5180 | "; | |
3de0261a AD |
5181 | |
5182 | print $entry_author; | |
5183 | ||
3cd4239a | 5184 | /* if (!$expand_cdm || $score < -100) { |
0cacc891 AD |
5185 | print " <a id=\"CICH-$id\" |
5186 | href=\"javascript:cdmExpandArticle($id)\"> | |
5187 | (".__('Show article').")</a>"; | |
3cd4239a | 5188 | } */ |
0cacc891 | 5189 | |
39f3c580 | 5190 | print $labels_str; |
0cacc891 | 5191 | |
d00f22ac | 5192 | if (!get_pref($link, 'VFEED_GROUP_BY_FEED')) { |
6cfea5c7 AD |
5193 | if ($line["feed_title"]) { |
5194 | print " (<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)"; | |
5195 | } | |
3de0261a AD |
5196 | } |
5197 | ||
5daa24f2 | 5198 | print "</span></div>"; |
3de0261a | 5199 | |
3fc2eed5 AD |
5200 | if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) { |
5201 | $line["content_preview"] = preg_replace("/href=/i", | |
e944346c | 5202 | "target=\"_blank\" href=", $line["content_preview"]); |
3fc2eed5 AD |
5203 | } |
5204 | ||
3cd4239a AD |
5205 | if ($show_excerpt) { |
5206 | print "<div class=\"cdmExcerpt\" id=\"CEXC-$id\" | |
5207 | onclick=\"cdmExpandArticle($id)\" | |
5208 | title=\"".__('Click to expand article')."\">"; | |
5209 | print truncate_string(strip_tags($line["content_preview"]), 100); | |
5210 | print "</div>"; | |
5211 | } | |
5212 | ||
5213 | print "<div class=\"cdmContent\" | |
5214 | onclick=\"cdmClicked($id)\" | |
5215 | id=\"CICD-$id\" $cdm_cstyle>"; | |
a04c8e8d | 5216 | |
0cacc891 | 5217 | // print "<div class=\"cdmInnerContent\" id=\"CICD-$id\" $cdm_cstyle>"; |
621ffb00 | 5218 | |
b2d5d145 | 5219 | print sanitize_rss($link, $line["content_preview"]); |
c54526fe | 5220 | $article_content = $line["content_preview"]; |
3c66b582 AD |
5221 | |
5222 | $e_result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE | |
4752b041 | 5223 | post_id = '$id' AND content_url != ''"); |
3c66b582 AD |
5224 | |
5225 | if (db_num_rows($e_result) > 0) { | |
3c66b582 | 5226 | |
a3eeb471 | 5227 | $entries_html = array(); |
3c66b582 AD |
5228 | $entries = array(); |
5229 | ||
5230 | while ($e_line = db_fetch_assoc($e_result)) { | |
5231 | ||
5232 | $url = $e_line["content_url"]; | |
4752b041 AD |
5233 | $ctype = $e_line["content_type"]; |
5234 | if (!$ctype) $ctype = __("unknown type"); | |
3c66b582 AD |
5235 | |
5236 | $filename = substr($url, strrpos($url, "/")+1); | |
5237 | ||
8dccabed AD |
5238 | $entry = ""; |
5239 | ||
b84b68d9 | 5240 | if (($ctype == __("audio/mpeg")) && |
8dccabed AD |
5241 | (get_pref($link, "ENABLE_FLASH_PLAYER")) ) { |
5242 | ||
39e865fa | 5243 | $entry .= "<object type=\"application/x-shockwave-flash\" data=\"extras/button/musicplayer.swf?song_url=$url\" width=\"17\" height=\"17\"> <param name=\"movie\" value=\"extras/button/musicplayer.swf?song_url=$url\" /> </object> "; |
8dccabed AD |
5244 | |
5245 | } | |
5246 | ||
5247 | $entry .= "<a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" . | |
4752b041 | 5248 | $filename . " (" . $ctype . ")" . "</a>"; |
3c66b582 | 5249 | |
a3eeb471 AD |
5250 | array_push($entries_html, $entry); |
5251 | ||
5252 | $entry = array(); | |
5253 | ||
5254 | $entry["type"] = $ctype; | |
5255 | $entry["filename"] = $filename; | |
5256 | $entry["url"] = $url; | |
5257 | ||
3c66b582 AD |
5258 | array_push($entries, $entry); |
5259 | } | |
5260 | ||
c54526fe | 5261 | if (!preg_match("/img/i", $article_content)) { |
a3eeb471 AD |
5262 | foreach ($entries as $entry) { |
5263 | if (preg_match("/image/", $entry["type"])) { | |
5264 | print "<p><img | |
5265 | alt=\"".htmlspecialchars($entry["filename"])."\" | |
5266 | src=\"" .htmlspecialchars($entry["url"]) . "\"></p>"; | |
5267 | } | |
5268 | } | |
5269 | } | |
5270 | ||
5271 | print "<div class=\"cdmEnclosures\">"; | |
5272 | ||
5273 | if (db_num_rows($e_result) == 1) { | |
5274 | print __("Attachment:") . " "; | |
5275 | } else { | |
5276 | print __("Attachments:") . " "; | |
5277 | } | |
5278 | ||
5279 | print join(", ", $entries_html); | |
3c66b582 AD |
5280 | |
5281 | print "</div>"; | |
5282 | } | |
5283 | ||
a3eeb471 | 5284 | |
0cacc891 AD |
5285 | print "<br clear='both'>"; |
5286 | // print "</div>"; | |
a04c8e8d | 5287 | |
0cacc891 | 5288 | /* if (!$expand_cdm) { |
12f5d8fe AD |
5289 | print "<a id=\"CICH-$id\" |
5290 | href=\"javascript:cdmExpandArticle($id)\"> | |
5291 | Show article</a>"; | |
0cacc891 | 5292 | } */ |
a04c8e8d | 5293 | |
0cacc891 | 5294 | print "</div>"; |
3de0261a | 5295 | |
e2ccbfab | 5296 | print "<div class=\"cdmFooter\"><span class='s0'>"; |
3de0261a | 5297 | |
e2ccbfab | 5298 | /* print "<div class=\"markedPic\">Star it: $marked_pic</div>"; */ |
3de0261a | 5299 | |
e2ccbfab AD |
5300 | print __("Select:"). |
5301 | " <input type=\"checkbox\" onclick=\"toggleSelectRowById(this, | |
3de0261a AD |
5302 | 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">"; |
5303 | ||
e2ccbfab | 5304 | print "</span><span class='s1'>$marked_pic</span> "; |
e4f4b46f | 5305 | print "<span class='s1'>$published_pic</span> "; |
eedfb635 AD |
5306 | print "<span class='s1'><img src=\"images/art-zoom.png\" class='tagsPic' |
5307 | onclick=\"zoomToArticle($id)\" | |
5308 | style=\"cursor : pointer\" | |
5309 | alt='Zoom' | |
5310 | title='".__('Show article summary in new window')."'></span>"; | |
e2ccbfab | 5311 | |
3de0261a AD |
5312 | $tags = get_article_tags($link, $id); |
5313 | ||
5314 | $tags_str = ""; | |
22d1f3db | 5315 | $full_tags_str = ""; |
d735ebd2 | 5316 | $num_tags = 0; |
3de0261a AD |
5317 | |
5318 | foreach ($tags as $tag) { | |
5319 | $num_tags++; | |
22d1f3db AD |
5320 | $full_tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, "; |
5321 | if ($num_tags < 5) { | |
5322 | $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, "; | |
5323 | } else if ($num_tags == 5) { | |
66a251f9 | 5324 | $tags_str .= "…"; |
22d1f3db | 5325 | } |
3de0261a AD |
5326 | } |
5327 | ||
5328 | $tags_str = preg_replace("/, $/", "", $tags_str); | |
22d1f3db AD |
5329 | $full_tags_str = preg_replace("/, $/", "", $full_tags_str); |
5330 | ||
66a251f9 | 5331 | $all_tags_div = "<span class='cdmAllTagsCtr'>…<div class='cdmAllTags'>All Tags: $full_tags_str</div></span>"; |
22d1f3db AD |
5332 | |
5333 | $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str); | |
5334 | ||
3de0261a AD |
5335 | |
5336 | if ($tags_str == "") $tags_str = "no tags"; | |
e2ccbfab AD |
5337 | |
5338 | // print "<img src='images/tag.png' class='markedPic'>"; | |
5339 | ||
5f014cf1 AD |
5340 | print "<span class='s1'> |
5341 | <img class='tagsPic' src='images/tag.png' alt='Tags' | |
5342 | title='Tags'> $tags_str <a title=\"Edit tags for this article\" | |
3de0261a AD |
5343 | href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>"; |
5344 | ||
e2ccbfab | 5345 | print "</span>"; |
3de0261a | 5346 | |
e2ccbfab AD |
5347 | print "<span class='s2'>Toggle: <a class=\"cdmToggleLink\" |
5348 | href=\"javascript:toggleUnread($id)\"> | |
5349 | Unread</a></span>"; | |
3de0261a | 5350 | |
e2ccbfab | 5351 | print "</div>"; |
3de0261a AD |
5352 | print "</div>"; |
5353 | ||
5354 | } | |
5355 | ||
5356 | ++$lnum; | |
5357 | } | |
5358 | ||
ac541432 | 5359 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) { |
3de0261a AD |
5360 | print "</table>"; |
5361 | } | |
5362 | ||
3de0261a | 5363 | } else { |
93c841c4 AD |
5364 | $message = ""; |
5365 | ||
5366 | switch ($view_mode) { | |
5367 | case "unread": | |
5368 | $message = __("No unread articles found to display."); | |
5369 | break; | |
8b09eac8 AD |
5370 | case "updated": |
5371 | $message = __("No updated articles found to display."); | |
5372 | break; | |
93c841c4 AD |
5373 | case "marked": |
5374 | $message = __("No starred articles found to display."); | |
5375 | break; | |
5376 | default: | |
215af892 AD |
5377 | if ($feed < -10) { |
5378 | $message = __("No articles found to display. You can assign articles to labels manually (see the Actions menu above) or use a filter."); | |
5379 | } else { | |
5380 | $message = __("No articles found to display."); | |
5381 | } | |
93c841c4 AD |
5382 | } |
5383 | ||
5384 | if (!$offset) print "<div class='whiteBox'>$message</div>"; | |
3de0261a AD |
5385 | } |
5386 | ||
ac541432 AD |
5387 | if (!$offset) { |
5388 | print "</div>"; | |
5389 | print "</div>"; | |
5390 | } | |
3de0261a | 5391 | |
081e527d | 5392 | return array($topmost_article_ids, $headlines_count, $feed, $disable_cache, $vgroup_last_feed); |
3de0261a | 5393 | } |
0979b696 AD |
5394 | |
5395 | // from here: http://www.roscripts.com/Create_tag_cloud-71.html | |
5396 | ||
5397 | function printTagCloud($link) { | |
35a03bdd | 5398 | |
0979b696 AD |
5399 | $query = "SELECT tag_name, COUNT(post_int_id) AS count |
5400 | FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]." | |
b31af972 | 5401 | GROUP BY tag_name ORDER BY count DESC LIMIT 50"; |
0979b696 AD |
5402 | |
5403 | $result = db_query($link, $query); | |
5404 | ||
5405 | $tags = array(); | |
5406 | ||
5407 | while ($line = db_fetch_assoc($result)) { | |
5408 | $tags[$line["tag_name"]] = $line["count"]; | |
5409 | } | |
5410 | ||
5411 | ksort($tags); | |
5412 | ||
5413 | $max_size = 32; // max font size in pixels | |
4548a580 | 5414 | $min_size = 11; // min font size in pixels |
0979b696 AD |
5415 | |
5416 | // largest and smallest array values | |
5417 | $max_qty = max(array_values($tags)); | |
5418 | $min_qty = min(array_values($tags)); | |
5419 | ||
5420 | // find the range of values | |
5421 | $spread = $max_qty - $min_qty; | |
5422 | if ($spread == 0) { // we don't want to divide by zero | |
5423 | $spread = 1; | |
5424 | } | |
5425 | ||
5426 | // set the font-size increment | |
5427 | $step = ($max_size - $min_size) / ($spread); | |
5428 | ||
5429 | // loop through the tag array | |
5430 | foreach ($tags as $key => $value) { | |
5431 | // calculate font-size | |
5432 | // find the $value in excess of $min_qty | |
5433 | // multiply by the font-size increment ($size) | |
5434 | // and add the $min_size set above | |
5435 | $size = round($min_size + (($value - $min_qty) * $step)); | |
546ffab4 AD |
5436 | |
5437 | $key_escaped = str_replace("'", "\\'", $key); | |
5438 | ||
5439 | echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " . | |
0979b696 AD |
5440 | $size . "px\" title=\"$value articles tagged with " . |
5441 | $key . '">' . $key . '</a> '; | |
5442 | } | |
5443 | } | |
46921916 AD |
5444 | |
5445 | function print_checkpoint($n, $s) { | |
5446 | $ts = getmicrotime(); | |
5447 | echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s); | |
5448 | return $ts; | |
5449 | } | |
14b6c54b AD |
5450 | |
5451 | function sanitize_tag($tag) { | |
5452 | $tag = trim($tag); | |
5453 | ||
5454 | $tag = mb_strtolower($tag, 'utf-8'); | |
5455 | ||
0c3d1c68 AD |
5456 | $tag = preg_replace('/[\"\+\>\<]/', "", $tag); |
5457 | ||
5458 | // $tag = str_replace('"', "", $tag); | |
5459 | // $tag = str_replace("+", " ", $tag); | |
14b6c54b AD |
5460 | $tag = str_replace("technorati tag: ", "", $tag); |
5461 | ||
5462 | return $tag; | |
5463 | } | |
e4f4b46f AD |
5464 | |
5465 | function generate_publish_key() { | |
5466 | return sha1(uniqid(rand(), true)); | |
5467 | } | |
5468 | ||
f56e3080 AD |
5469 | function article_publish_url($link) { |
5470 | ||
e2749781 AD |
5471 | $url_path = ""; |
5472 | ||
5473 | ||
5474 | if ($_SERVER['HTTPS'] != "on") { | |
5475 | $url_path = "http://"; | |
5476 | } else { | |
5477 | $url_path = "https://"; | |
5478 | } | |
f56e3080 | 5479 | |
e2749781 AD |
5480 | $url_path .= $_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']); |
5481 | $url_path .= "/backend.php?op=publish&key=" . get_pref($link, "_PREFS_PUBLISH_KEY"); | |
f56e3080 AD |
5482 | |
5483 | return $url_path; | |
5484 | } | |
5485 | ||
45004d43 AD |
5486 | /** |
5487 | * Purge a feed contents, marked articles excepted. | |
5488 | * | |
5489 | * @param mixed $link The database connection. | |
5490 | * @param integer $id The id of the feed to purge. | |
5491 | * @return void | |
5492 | */ | |
d1f0c584 AD |
5493 | function clear_feed_articles($link, $id) { |
5494 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
a8ae1b9a | 5495 | WHERE feed_id = '$id' AND marked = false AND owner_uid = " . $_SESSION["uid"]); |
d1f0c584 AD |
5496 | |
5497 | $result = db_query($link, "DELETE FROM ttrss_entries WHERE | |
5498 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
ced46404 AD |
5499 | |
5500 | ccache_update($link, $id, $_SESSION['uid']); | |
45004d43 AD |
5501 | } // function clear_feed_articles |
5502 | ||
5503 | /** | |
5504 | * Compute the Mozilla Firefox feed adding URL from server HOST and REQUEST_URI. | |
5505 | * | |
5506 | * @return string The Mozilla Firefox feed adding URL. | |
5507 | */ | |
5508 | function add_feed_url() { | |
d70c5ae4 | 5509 | $url_path = ($_SERVER['HTTPS'] != "on" ? 'http://' : 'https://') . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH); |
755a43ee AD |
5510 | $url_path .= "?op=pref-feeds&quiet=1&subop=add&feed_url=%s"; |
5511 | return $url_path; | |
45004d43 AD |
5512 | } // function add_feed_url |
5513 | ||
5514 | /** | |
5515 | * Encrypt a password in SHA1. | |
5516 | * | |
5517 | * @param string $pass The password to encrypt. | |
5518 | * @param string $login A optionnal login. | |
5519 | * @return string The encrypted password. | |
5520 | */ | |
1a9f4d3c AD |
5521 | function encrypt_password($pass, $login = '') { |
5522 | if ($login) { | |
5523 | return "SHA1X:" . sha1("$login:$pass"); | |
5524 | } else { | |
5525 | return "SHA1:" . sha1($pass); | |
5526 | } | |
45004d43 AD |
5527 | } // function encrypt_password |
5528 | ||
5529 | /** | |
5530 | * Update a feed batch. | |
5531 | * Used by daemons to update n feeds by run. | |
5532 | * Only update feed needing a update, and not being processed | |
5533 | * by another process. | |
5534 | * | |
5535 | * @param mixed $link Database link | |
5536 | * @param integer $limit Maximum number of feeds in update batch. Default to DAEMON_FEED_LIMIT. | |
5537 | * @param boolean $from_http Set to true if you call this function from http to disable cli specific code. | |
5538 | * @param boolean $debug Set to false to disable debug output. Default to true. | |
5539 | * @return void | |
5540 | */ | |
5541 | function update_daemon_common($link, $limit = DAEMON_FEED_LIMIT, $from_http = false, $debug = true) { | |
5542 | // Process all other feeds using last_updated and interval parameters | |
5543 | ||
5544 | // Test if the user has loggued in recently. If not, it does not update its feeds. | |
5545 | if (DAEMON_UPDATE_LOGIN_LIMIT > 0) { | |
5546 | if (DB_TYPE == "pgsql") { | |
5547 | $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'"; | |
5548 | } else { | |
5549 | $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)"; | |
5550 | } | |
5551 | } else { | |
5552 | $login_thresh_qpart = ""; | |
5553 | } | |
5554 | ||
5555 | // Test if the feed need a update (update interval exceded). | |
5556 | if (DB_TYPE == "pgsql") { | |
5557 | $update_limit_qpart = "AND (( | |
5558 | ttrss_feeds.update_interval = 0 | |
5559 | AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL) | |
5560 | ) OR ( | |
5561 | ttrss_feeds.update_interval > 0 | |
5562 | AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL) | |
da4caf5d | 5563 | ) OR ttrss_feeds.last_updated IS NULL)"; |
45004d43 AD |
5564 | } else { |
5565 | $update_limit_qpart = "AND (( | |
5566 | ttrss_feeds.update_interval = 0 | |
5567 | AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE) | |
5568 | ) OR ( | |
5569 | ttrss_feeds.update_interval > 0 | |
5570 | AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE) | |
da4caf5d | 5571 | ) OR ttrss_feeds.last_updated IS NULL)"; |
45004d43 AD |
5572 | } |
5573 | ||
5574 | // Test if feed is currently being updated by another process. | |
5575 | if (DB_TYPE == "pgsql") { | |
5576 | $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')"; | |
5577 | } else { | |
5578 | $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))"; | |
5579 | } | |
5580 | ||
5581 | // Test if there is a limit to number of updated feeds | |
5582 | $query_limit = ""; | |
5583 | if($limit) $query_limit = sprintf("LIMIT %d", $limit); | |
5584 | ||
51b8c957 AD |
5585 | $random_qpart = sql_random_function(); |
5586 | ||
45004d43 AD |
5587 | // We search for feed needing update. |
5588 | $result = db_query($link, "SELECT ttrss_feeds.feed_url,ttrss_feeds.id, ttrss_feeds.owner_uid, | |
fc2b26a6 | 5589 | ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated, |
45004d43 AD |
5590 | ttrss_feeds.update_interval |
5591 | FROM | |
5592 | ttrss_feeds, ttrss_users, ttrss_user_prefs | |
5593 | WHERE | |
5594 | ttrss_feeds.owner_uid = ttrss_users.id | |
5595 | AND ttrss_users.id = ttrss_user_prefs.owner_uid | |
5596 | AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL' | |
5597 | $login_thresh_qpart $update_limit_qpart | |
51b8c957 AD |
5598 | $updstart_thresh_qpart |
5599 | ORDER BY $random_qpart $query_limit"); | |
45004d43 AD |
5600 | |
5601 | $user_prefs_cache = array(); | |
5602 | ||
5603 | if($debug) _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result))); | |
5604 | ||
5605 | // Here is a little cache magic in order to minimize risk of double feed updates. | |
5606 | $feeds_to_update = array(); | |
5607 | while ($line = db_fetch_assoc($result)) { | |
5608 | $feeds_to_update[$line['id']] = $line; | |
5609 | } | |
5610 | ||
5611 | // We update the feed last update started date before anything else. | |
5612 | // There is no lag due to feed contents downloads | |
5613 | // It prevent an other process to update the same feed. | |
5614 | $feed_ids = array_keys($feeds_to_update); | |
5615 | if($feed_ids) { | |
5616 | db_query($link, sprintf("UPDATE ttrss_feeds SET last_update_started = NOW() | |
5617 | WHERE id IN (%s)", implode(',', $feed_ids))); | |
5618 | } | |
5619 | ||
5620 | // For each feed, we call the feed update function. | |
5621 | while ($line = array_pop($feeds_to_update)) { | |
5622 | ||
5623 | if($debug) _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]); | |
5624 | ||
5625 | // We setup a alarm to alert if the feed take more than 300s to update. | |
5626 | // => HANG alarm. | |
9a91a51e | 5627 | if(!$from_http && function_exists('pcntl_alarm')) pcntl_alarm(300); |
45004d43 AD |
5628 | update_rss_feed($link, $line["feed_url"], $line["id"], true); |
5629 | // Cancel the alarm (the update went well) | |
9a91a51e | 5630 | if(!$from_http && function_exists('pcntl_alarm')) pcntl_alarm(0); |
45004d43 AD |
5631 | |
5632 | sleep(1); // prevent flood (FIXME make this an option?) | |
5633 | } | |
5634 | ||
0e0dd486 AD |
5635 | // Send feed digests by email if needed. |
5636 | if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link); | |
5637 | ||
5638 | purge_orphans($link); | |
45004d43 AD |
5639 | |
5640 | } // function update_daemon_common | |
1a9f4d3c | 5641 | |
621ffb00 AD |
5642 | function sanitize_article_content($text) { |
5643 | # we don't support CDATA sections in articles, they break our own escaping | |
5644 | $text = preg_replace("/\[\[CDATA/", "", $text); | |
5645 | $text = preg_replace("/\]\]\>/", "", $text); | |
5646 | return $text; | |
5647 | } | |
fee840fb AD |
5648 | |
5649 | function load_filters($link, $feed, $owner_uid, $action_id = false) { | |
5650 | $filters = array(); | |
5651 | ||
5652 | if ($action_id) $ftype_query_part = "action_id = '$action_id' AND"; | |
5653 | ||
5654 | $result = db_query($link, "SELECT reg_exp, | |
5655 | ttrss_filter_types.name AS name, | |
5656 | ttrss_filter_actions.name AS action, | |
5657 | inverse, | |
44d0e774 AD |
5658 | action_param, |
5659 | filter_param | |
fee840fb AD |
5660 | FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE |
5661 | enabled = true AND | |
5662 | $ftype_query_part | |
5663 | owner_uid = $owner_uid AND | |
5664 | ttrss_filter_types.id = filter_type AND | |
5665 | ttrss_filter_actions.id = action_id AND | |
5666 | (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp"); | |
5667 | ||
5668 | while ($line = db_fetch_assoc($result)) { | |
5669 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); | |
5670 | $filter["reg_exp"] = $line["reg_exp"]; | |
5671 | $filter["action"] = $line["action"]; | |
5672 | $filter["action_param"] = $line["action_param"]; | |
44d0e774 | 5673 | $filter["filter_param"] = $line["filter_param"]; |
fee840fb AD |
5674 | $filter["inverse"] = sql_bool_to_bool($line["inverse"]); |
5675 | ||
5676 | array_push($filters[$line["name"]], $filter); | |
5677 | } | |
5678 | ||
5679 | return $filters; | |
5680 | } | |
1e36af0c AD |
5681 | |
5682 | function get_score_pic($score) { | |
1cce3aca | 5683 | if ($score > 100) { |
1e36af0c | 5684 | return "score_high.png"; |
1cce3aca AD |
5685 | } else if ($score > 0) { |
5686 | return "score_half_high.png"; | |
5687 | } else if ($score < -100) { | |
1e36af0c | 5688 | return "score_low.png"; |
1cce3aca AD |
5689 | } else if ($score < 0) { |
5690 | return "score_half_low.png"; | |
1e36af0c AD |
5691 | } else { |
5692 | return "score_neutral.png"; | |
5693 | } | |
5694 | } | |
ec92c9d1 | 5695 | |
0745839a | 5696 | function rounded_table_start($classname, $header = " ") { |
ec92c9d1 | 5697 | print "<table width='100%' class='$classname' cellspacing='0' cellpadding='0'>"; |
74d5c8fa | 5698 | print "<tr><td class='c1'> </td><td class='top'>$header</td><td class='c2'> </td></tr>"; |
ec92c9d1 AD |
5699 | print "<tr><td class='left'> </td><td class='content'>"; |
5700 | } | |
5701 | ||
0745839a | 5702 | function rounded_table_end($footer = " ") { |
ec92c9d1 | 5703 | print "</td><td class='right'> </td></tr>"; |
74d5c8fa | 5704 | print "<tr><td class='c4'> </td><td class='bottom'>$footer</td><td class='c3'> </td></tr>"; |
ec92c9d1 AD |
5705 | print "</table>"; |
5706 | } | |
5707 | ||
7defa089 AD |
5708 | function feed_has_icon($id) { |
5709 | return is_file(ICONS_DIR . "/$id.ico") && filesize(ICONS_DIR . "/$id.ico") > 0; | |
5710 | } | |
f29ba148 AD |
5711 | |
5712 | function init_connection($link) { | |
5713 | if (DB_TYPE == "pgsql") { | |
6fc720fd | 5714 | pg_query($link, "set client_encoding = 'UTF-8'"); |
f29ba148 | 5715 | pg_set_client_encoding("UNICODE"); |
045d0ab8 | 5716 | pg_query($link, "set datestyle = 'ISO, european'"); |
f29ba148 AD |
5717 | } else { |
5718 | if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { | |
5719 | db_query($link, "SET NAMES " . MYSQL_CHARSET); | |
5720 | // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET); | |
5721 | } | |
5722 | } | |
5723 | } | |
5e96ca9d AD |
5724 | |
5725 | function update_feedbrowser_cache($link) { | |
5726 | ||
931dcbc1 | 5727 | $result = db_query($link, "SELECT feed_url,title, COUNT(id) AS subscribers |
5e96ca9d AD |
5728 | FROM ttrss_feeds WHERE (SELECT COUNT(id) = 0 FROM ttrss_feeds AS tf |
5729 | WHERE tf.feed_url = ttrss_feeds.feed_url | |
5730 | AND (private IS true OR feed_url LIKE '%:%@%/%')) | |
d460f7aa | 5731 | GROUP BY feed_url, title ORDER BY subscribers DESC LIMIT 1000"); |
5e96ca9d AD |
5732 | |
5733 | db_query($link, "BEGIN"); | |
5734 | ||
5735 | db_query($link, "DELETE FROM ttrss_feedbrowser_cache"); | |
5736 | ||
5737 | $count = 0; | |
5738 | ||
5739 | while ($line = db_fetch_assoc($result)) { | |
5740 | $subscribers = db_escape_string($line["subscribers"]); | |
5741 | $feed_url = db_escape_string($line["feed_url"]); | |
931dcbc1 AD |
5742 | $title = db_escape_string($line["title"]); |
5743 | ||
5744 | $tmp_result = db_query($link, "SELECT subscribers FROM | |
5745 | ttrss_feedbrowser_cache WHERE feed_url = '$feed_url'"); | |
5746 | ||
5747 | if (db_num_rows($tmp_result) == 0) { | |
5748 | ||
5749 | db_query($link, "INSERT INTO ttrss_feedbrowser_cache | |
5750 | (feed_url, title, subscribers) VALUES ('$feed_url', | |
5751 | '$title', '$subscribers')"); | |
5752 | ||
5753 | ++$count; | |
5754 | ||
5755 | } | |
5e96ca9d | 5756 | |
5e96ca9d AD |
5757 | } |
5758 | ||
5759 | db_query($link, "COMMIT"); | |
5760 | ||
5761 | return $count; | |
5762 | ||
5763 | } | |
2627f2d0 | 5764 | |
8a4c759e | 5765 | function ccache_zero($link, $feed_id, $owner_uid) { |
2627f2d0 AD |
5766 | db_query($link, "UPDATE ttrss_counters_cache SET |
5767 | value = 0, updated = NOW() WHERE | |
5768 | feed_id = '$feed_id' AND owner_uid = '$owner_uid'"); | |
8a4c759e | 5769 | } |
2627f2d0 | 5770 | |
ad0056a8 AD |
5771 | function ccache_zero_all($link, $owner_uid) { |
5772 | db_query($link, "UPDATE ttrss_counters_cache SET | |
5773 | value = 0 WHERE owner_uid = '$owner_uid'"); | |
5774 | ||
5775 | db_query($link, "UPDATE ttrss_cat_counters_cache SET | |
5776 | value = 0 WHERE owner_uid = '$owner_uid'"); | |
5777 | } | |
5778 | ||
5f4f7adf AD |
5779 | function ccache_update_all($link, $owner_uid) { |
5780 | ||
5781 | if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid)) { | |
5782 | ||
5783 | $result = db_query($link, "SELECT feed_id FROM ttrss_cat_counters_cache | |
5784 | WHERE feed_id > 0 AND owner_uid = '$owner_uid'"); | |
5785 | ||
5786 | while ($line = db_fetch_assoc($result)) { | |
5787 | ccache_update($link, $line["feed_id"], $owner_uid, true); | |
5788 | } | |
5789 | ||
c5ffeb61 AD |
5790 | /* We have to manually include category 0 */ |
5791 | ||
5792 | ccache_update($link, 0, $owner_uid, true); | |
5793 | ||
8a4c759e | 5794 | } else { |
5f4f7adf AD |
5795 | $result = db_query($link, "SELECT feed_id FROM ttrss_counters_cache |
5796 | WHERE feed_id > 0 AND owner_uid = '$owner_uid'"); | |
8a4c759e | 5797 | |
5f4f7adf AD |
5798 | while ($line = db_fetch_assoc($result)) { |
5799 | print ccache_update($link, $line["feed_id"], $owner_uid); | |
5800 | ||
5801 | } | |
5802 | ||
5803 | } | |
5804 | } | |
2627f2d0 | 5805 | |
6b49a3dd AD |
5806 | function ccache_find($link, $feed_id, $owner_uid, $is_cat = false, |
5807 | $no_update = false) { | |
8a4c759e AD |
5808 | |
5809 | if (!$is_cat) { | |
5810 | $table = "ttrss_counters_cache"; | |
5811 | } else { | |
5812 | $table = "ttrss_cat_counters_cache"; | |
5813 | } | |
5814 | ||
5815 | if (DB_TYPE == "pgsql") { | |
5816 | $date_qpart = "updated > NOW() - INTERVAL '15 minutes'"; | |
5817 | } else if (DB_TYPE == "mysql") { | |
5818 | $date_qpart = "updated > DATE_SUB(NOW(), INTERVAL 15 MINUTE)"; | |
5819 | } | |
5820 | ||
5821 | $result = db_query($link, "SELECT value FROM $table | |
5822 | WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id' | |
37fb651d | 5823 | LIMIT 1"); |
2627f2d0 AD |
5824 | |
5825 | if (db_num_rows($result) == 1) { | |
5826 | return db_fetch_result($result, 0, "value"); | |
5827 | } else { | |
6b49a3dd AD |
5828 | if ($no_update) { |
5829 | return -1; | |
5830 | } else { | |
5831 | return ccache_update($link, $feed_id, $owner_uid, $is_cat); | |
5832 | } | |
2627f2d0 AD |
5833 | } |
5834 | ||
5835 | } | |
5836 | ||
6c2a9b9e | 5837 | function ccache_update($link, $feed_id, $owner_uid, $is_cat = false, |
6b49a3dd AD |
5838 | $update_pcat = true) { |
5839 | ||
43ead405 AD |
5840 | $prev_unread = ccache_find($link, $feed_id, $owner_uid, $is_cat, true); |
5841 | ||
5842 | /* When updating a label, all we need to do is recalculate feed counters | |
5843 | * because labels are not cached */ | |
c98e43db AD |
5844 | |
5845 | if ($feed_id < 0) { | |
43ead405 AD |
5846 | ccache_update_all($link, $owner_uid); |
5847 | return; | |
c98e43db AD |
5848 | } |
5849 | ||
8a4c759e AD |
5850 | if (!$is_cat) { |
5851 | $table = "ttrss_counters_cache"; | |
5852 | } else { | |
5853 | $table = "ttrss_cat_counters_cache"; | |
5854 | } | |
5855 | ||
b6d486a3 | 5856 | if ($is_cat && $feed_id >= 0) { |
37fb651d AD |
5857 | if ($feed_id != 0) { |
5858 | $cat_qpart = "cat_id = '$feed_id'"; | |
5859 | } else { | |
5860 | $cat_qpart = "cat_id IS NULL"; | |
5861 | } | |
5862 | ||
6b49a3dd AD |
5863 | /* Recalculate counters for child feeds */ |
5864 | ||
5865 | $result = db_query($link, "SELECT id FROM ttrss_feeds | |
5866 | WHERE owner_uid = '$owner_uid' AND $cat_qpart"); | |
5867 | ||
5868 | while ($line = db_fetch_assoc($result)) { | |
5869 | ccache_update($link, $line["id"], $owner_uid, false, false); | |
5870 | } | |
5871 | ||
37fb651d AD |
5872 | $result = db_query($link, "SELECT SUM(value) AS sv |
5873 | FROM ttrss_counters_cache, ttrss_feeds | |
5874 | WHERE id = feed_id AND $cat_qpart AND | |
5875 | ttrss_feeds.owner_uid = '$owner_uid'"); | |
5876 | ||
51e196de | 5877 | $unread = (int) db_fetch_result($result, 0, "sv"); |
37fb651d | 5878 | |
f55b0b12 AD |
5879 | } else { |
5880 | $unread = (int) getFeedArticles($link, $feed_id, $is_cat, true, $owner_uid); | |
37fb651d | 5881 | } |
2627f2d0 | 5882 | |
8a4c759e | 5883 | $result = db_query($link, "SELECT feed_id FROM $table |
2627f2d0 AD |
5884 | WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id' LIMIT 1"); |
5885 | ||
5886 | if (db_num_rows($result) == 1) { | |
8a4c759e | 5887 | db_query($link, "UPDATE $table SET |
2627f2d0 AD |
5888 | value = '$unread', updated = NOW() WHERE |
5889 | feed_id = '$feed_id' AND owner_uid = '$owner_uid'"); | |
5890 | ||
5891 | } else { | |
8a4c759e | 5892 | db_query($link, "INSERT INTO $table |
2627f2d0 AD |
5893 | (feed_id, value, owner_uid, updated) |
5894 | VALUES | |
5895 | ($feed_id, $unread, $owner_uid, NOW())"); | |
2627f2d0 AD |
5896 | } |
5897 | ||
6b49a3dd | 5898 | if ($feed_id > 0 && $prev_unread != $unread) { |
8a4c759e | 5899 | |
37fb651d | 5900 | if (!$is_cat) { |
8a4c759e | 5901 | |
6b49a3dd | 5902 | /* Update parent category */ |
8a4c759e | 5903 | |
6b49a3dd | 5904 | if ($update_pcat) { |
37fb651d | 5905 | |
6b49a3dd AD |
5906 | $result = db_query($link, "SELECT cat_id FROM ttrss_feeds |
5907 | WHERE owner_uid = '$owner_uid' AND id = '$feed_id'"); | |
8a4c759e | 5908 | |
6b49a3dd | 5909 | $cat_id = (int) db_fetch_result($result, 0, "cat_id"); |
37fb651d | 5910 | |
6b49a3dd | 5911 | ccache_update($link, $cat_id, $owner_uid, true); |
6c2a9b9e | 5912 | |
6c2a9b9e | 5913 | } |
8a4c759e | 5914 | } |
5f4f7adf AD |
5915 | } else if ($feed_id < 0) { |
5916 | ccache_update_all($link, $owner_uid); | |
8a4c759e AD |
5917 | } |
5918 | ||
5919 | return $unread; | |
2627f2d0 | 5920 | } |
ceb30ba4 AD |
5921 | |
5922 | function label_find_id($link, $label, $owner_uid) { | |
5923 | $result = db_query($link, | |
5924 | "SELECT id FROM ttrss_labels2 WHERE caption = '$label' | |
5925 | AND owner_uid = '$owner_uid' LIMIT 1"); | |
5926 | ||
5927 | if (db_num_rows($result) == 1) { | |
5928 | return db_fetch_result($result, 0, "id"); | |
5929 | } else { | |
5930 | return 0; | |
5931 | } | |
5932 | } | |
5933 | ||
814bff66 AD |
5934 | function get_article_labels($link, $id) { |
5935 | $result = db_query($link, | |
5936 | "SELECT DISTINCT label_id,caption | |
5937 | FROM ttrss_labels2, ttrss_user_labels2 | |
5938 | WHERE id = label_id | |
5939 | AND article_id = '$id' | |
e2549229 AD |
5940 | AND owner_uid = ".$_SESSION["uid"] . " |
5941 | ORDER BY caption"); | |
814bff66 AD |
5942 | |
5943 | $rv = array(); | |
5944 | ||
5945 | while ($line = db_fetch_assoc($result)) { | |
5946 | $rk = array($line["label_id"], $line["caption"]); | |
5947 | array_push($rv, $rk); | |
5948 | } | |
5949 | ||
5950 | return $rv; | |
5951 | } | |
5952 | ||
5953 | ||
b8a637f3 AD |
5954 | function label_find_caption($link, $label, $owner_uid) { |
5955 | $result = db_query($link, | |
5956 | "SELECT caption FROM ttrss_labels2 WHERE id = '$label' | |
5957 | AND owner_uid = '$owner_uid' LIMIT 1"); | |
5958 | ||
5959 | if (db_num_rows($result) == 1) { | |
5960 | return db_fetch_result($result, 0, "caption"); | |
5961 | } else { | |
5962 | return ""; | |
5963 | } | |
5964 | } | |
5965 | ||
933ba4ee AD |
5966 | function label_remove_article($link, $id, $label, $owner_uid) { |
5967 | ||
5968 | $label_id = label_find_id($link, $label, $owner_uid); | |
5969 | ||
5970 | if (!$label_id) return; | |
5971 | ||
5972 | $result = db_query($link, | |
5973 | "DELETE FROM ttrss_user_labels2 | |
5974 | WHERE | |
5975 | label_id = '$label_id' AND | |
5976 | article_id = '$id'"); | |
5977 | } | |
5978 | ||
ceb30ba4 AD |
5979 | function label_add_article($link, $id, $label, $owner_uid) { |
5980 | ||
5981 | $label_id = label_find_id($link, $label, $owner_uid); | |
5982 | ||
5983 | if (!$label_id) return; | |
5984 | ||
5985 | $result = db_query($link, | |
5986 | "SELECT | |
5987 | article_id FROM ttrss_labels2, ttrss_user_labels2 | |
5988 | WHERE | |
5989 | label_id = id AND | |
5990 | label_id = '$label_id' AND | |
5991 | article_id = '$id' AND owner_uid = '$owner_uid' | |
5992 | LIMIT 1"); | |
5993 | ||
5994 | if (db_num_rows($result) == 0) { | |
5995 | db_query($link, "INSERT INTO ttrss_user_labels2 | |
5996 | (label_id, article_id) VALUES ('$label_id', '$id')"); | |
5997 | } | |
5998 | } | |
1380f8ee AD |
5999 | |
6000 | function label_remove($link, $id, $owner_uid) { | |
6001 | ||
6002 | db_query($link, "BEGIN"); | |
6003 | ||
6004 | $result = db_query($link, "SELECT caption FROM ttrss_labels2 | |
6005 | WHERE id = '$id'"); | |
6006 | ||
6007 | $caption = db_fetch_result($result, 0, "caption"); | |
6008 | ||
6009 | $result = db_query($link, "DELETE FROM ttrss_labels2 WHERE id = '$id' | |
6010 | AND owner_uid = " . $_SESSION["uid"]); | |
6011 | ||
6012 | if (db_affected_rows($link, $result) != 0 && $caption) { | |
6013 | ||
6014 | /* Disable filters that reference label being removed */ | |
6015 | ||
6016 | db_query($link, "UPDATE ttrss_filters SET | |
6017 | enabled = false WHERE action_param = '$caption' | |
6018 | AND action_id = 7 | |
6019 | AND owner_uid = " . $_SESSION["uid"]); | |
6020 | } | |
6021 | ||
6022 | db_query($link, "COMMIT"); | |
6023 | } | |
40d13c28 | 6024 | ?> |