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