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