]>
Commit | Line | Data |
---|---|---|
1d3a17c7 | 1 | <?php |
f1a80dae | 2 | |
894ebcf5 | 3 | /* if ($_GET["debug"]) { |
cce28758 AD |
4 | define('DEFAULT_ERROR_LEVEL', E_ALL); |
5 | } else { | |
6 | define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE); | |
894ebcf5 | 7 | } */ |
cce28758 | 8 | |
40d13c28 | 9 | require_once 'config.php'; |
b619ff15 | 10 | require_once 'db-prefs.php'; |
5bc0bd27 | 11 | require_once 'compat.php'; |
af106b0e | 12 | require_once 'errors.php'; |
8911ac8b | 13 | require_once 'version.php'; |
40d13c28 | 14 | |
7c5a308d | 15 | if (RSS_BACKEND_TYPE == "magpie") { |
d5f74779 | 16 | define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')'); |
ed891a51 | 17 | require_once "magpierss/rss_fetch.inc"; |
7c5a308d AD |
18 | require_once 'magpierss/rss_utils.inc'; |
19 | } else if (RSS_BACKEND_TYPE == "simplepie") { | |
20 | require_once 'simplepie/simplepie.inc'; | |
21 | } | |
387234f3 | 22 | |
a3ee2a38 AD |
23 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); |
24 | ||
ad507f85 AD |
25 | function purge_feed($link, $feed_id, $purge_interval, $debug = false) { |
26 | ||
27 | $rows = -1; | |
4c193675 | 28 | |
fefa6ca3 | 29 | if (DB_TYPE == "pgsql") { |
44e241cb | 30 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 31 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 32 | (SELECT date_entered FROM ttrss_entries WHERE |
44e241cb AD |
33 | id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */ |
34 | ||
6e7f8d26 AD |
35 | $pg_version = get_pgsql_version($link); |
36 | ||
37 | if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) { | |
1e59ae35 AD |
38 | |
39 | $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
40 | ttrss_entries.id = ref_id AND | |
41 | marked = false AND | |
42 | feed_id = '$feed_id' AND | |
43 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); | |
44 | ||
45 | } else { | |
46 | ||
47 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
48 | USING ttrss_entries | |
49 | WHERE ttrss_entries.id = ref_id AND | |
50 | marked = false AND | |
51 | feed_id = '$feed_id' AND | |
fc774155 | 52 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); |
1e59ae35 | 53 | } |
ad507f85 AD |
54 | |
55 | $rows = pg_affected_rows($result); | |
56 | ||
fefa6ca3 | 57 | } else { |
1e59ae35 | 58 | |
30f1746f | 59 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 60 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 61 | (SELECT date_entered FROM ttrss_entries WHERE |
30f1746f AD |
62 | id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */ |
63 | ||
64 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
65 | USING ttrss_user_entries, ttrss_entries | |
66 | WHERE ttrss_entries.id = ref_id AND | |
67 | marked = false AND | |
68 | feed_id = '$feed_id' AND | |
69 | ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); | |
70 | ||
ad507f85 AD |
71 | $rows = mysql_affected_rows($link); |
72 | ||
73 | } | |
74 | ||
75 | if ($debug) { | |
76 | print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n"; | |
fefa6ca3 AD |
77 | } |
78 | } | |
79 | ||
44e241cb AD |
80 | function global_purge_old_posts($link, $do_output = false, $limit = false) { |
81 | ||
894ebcf5 | 82 | $random_qpart = sql_random_function(); |
fefa6ca3 | 83 | |
44e241cb AD |
84 | if ($limit) { |
85 | $limit_qpart = "LIMIT $limit"; | |
86 | } else { | |
87 | $limit_qpart = ""; | |
88 | } | |
89 | ||
fefa6ca3 | 90 | $result = db_query($link, |
44e241cb AD |
91 | "SELECT id,purge_interval,owner_uid FROM ttrss_feeds |
92 | ORDER BY $random_qpart $limit_qpart"); | |
fefa6ca3 AD |
93 | |
94 | while ($line = db_fetch_assoc($result)) { | |
95 | ||
96 | $feed_id = $line["id"]; | |
97 | $purge_interval = $line["purge_interval"]; | |
98 | $owner_uid = $line["owner_uid"]; | |
99 | ||
100 | if ($purge_interval == 0) { | |
101 | ||
102 | $tmp_result = db_query($link, | |
103 | "SELECT value FROM ttrss_user_prefs WHERE | |
104 | pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'"); | |
105 | ||
106 | if (db_num_rows($tmp_result) != 0) { | |
107 | $purge_interval = db_fetch_result($tmp_result, 0, "value"); | |
108 | } | |
109 | } | |
110 | ||
111 | if ($do_output) { | |
ad507f85 | 112 | // print "Feed $feed_id: purge interval = $purge_interval\n"; |
fefa6ca3 AD |
113 | } |
114 | ||
115 | if ($purge_interval > 0) { | |
ad507f85 | 116 | purge_feed($link, $feed_id, $purge_interval, $do_output); |
fefa6ca3 AD |
117 | } |
118 | } | |
119 | ||
71604ca4 AD |
120 | // purge orphaned posts in main content table |
121 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
122 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
123 | ||
fefa6ca3 AD |
124 | } |
125 | ||
b6eefba5 | 126 | function purge_old_posts($link) { |
5d73494a | 127 | |
f1a80dae AD |
128 | $user_id = $_SESSION["uid"]; |
129 | ||
130 | $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds | |
131 | WHERE owner_uid = '$user_id'"); | |
5d73494a AD |
132 | |
133 | while ($line = db_fetch_assoc($result)) { | |
134 | ||
135 | $feed_id = $line["id"]; | |
136 | $purge_interval = $line["purge_interval"]; | |
137 | ||
b619ff15 | 138 | if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS'); |
5d73494a | 139 | |
140aae81 | 140 | if ($purge_interval > 0) { |
fefa6ca3 | 141 | purge_feed($link, $feed_id, $purge_interval); |
5d73494a AD |
142 | } |
143 | } | |
71604ca4 AD |
144 | |
145 | // purge orphaned posts in main content table | |
146 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
147 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
c3a8d71a AD |
148 | } |
149 | ||
1f2b01ed | 150 | function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) { |
40d13c28 | 151 | |
4769ddaf | 152 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 153 | |
a2770077 AD |
154 | if (!$user_id) { |
155 | $user_id = $_SESSION["uid"]; | |
156 | purge_old_posts($link); | |
157 | } | |
158 | ||
25af8dad | 159 | // db_query($link, "BEGIN"); |
b82af8c3 | 160 | |
cbd8650d AD |
161 | if (MAX_UPDATE_TIME > 0) { |
162 | if (DB_TYPE == "mysql") { | |
163 | $q_order = "RAND()"; | |
164 | } else { | |
165 | $q_order = "RANDOM()"; | |
166 | } | |
167 | } else { | |
168 | $q_order = "last_updated DESC"; | |
169 | } | |
170 | ||
d148926e | 171 | $result = db_query($link, "SELECT feed_url,id, |
798f722b | 172 | SUBSTRING(last_updated,1,19) AS last_updated, |
5c563acd | 173 | update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id' |
cbd8650d AD |
174 | ORDER BY $q_order"); |
175 | ||
176 | $upd_start = time(); | |
40d13c28 | 177 | |
b6eefba5 | 178 | while ($line = db_fetch_assoc($result)) { |
d148926e AD |
179 | $upd_intl = $line["update_interval"]; |
180 | ||
b619ff15 | 181 | if (!$upd_intl || $upd_intl == 0) { |
e289ca71 | 182 | $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false); |
b619ff15 | 183 | } |
d148926e | 184 | |
c1e202b7 AD |
185 | if ($upd_intl < 0) { |
186 | // Updates for this feed are disabled | |
187 | continue; | |
188 | } | |
189 | ||
93d40f50 AD |
190 | if ($fetch || (!$line["last_updated"] || |
191 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) { | |
c5142cca | 192 | |
cbd8650d AD |
193 | // print "<!-- feed: ".$line["feed_url"]." -->"; |
194 | ||
1f2b01ed | 195 | update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon); |
cbd8650d AD |
196 | |
197 | $upd_elapsed = time() - $upd_start; | |
198 | ||
199 | if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) { | |
200 | return; | |
201 | } | |
d148926e | 202 | } |
40d13c28 AD |
203 | } |
204 | ||
25af8dad | 205 | // db_query($link, "COMMIT"); |
b82af8c3 | 206 | |
40d13c28 AD |
207 | } |
208 | ||
4065b60b AD |
209 | function fetch_file_contents($url) { |
210 | if (USE_CURL_FOR_ICONS) { | |
211 | $tmpfile = tempnam(TMP_DIRECTORY, "ttrss-tmp"); | |
212 | ||
213 | $ch = curl_init($url); | |
214 | $fp = fopen($tmpfile, "w"); | |
215 | ||
216 | if ($fp) { | |
217 | curl_setopt($ch, CURLOPT_FILE, $fp); | |
218 | curl_exec($ch); | |
219 | curl_close($ch); | |
220 | fclose($fp); | |
221 | } | |
222 | ||
223 | $contents = file_get_contents($tmpfile); | |
224 | unlink($tmpfile); | |
225 | ||
226 | return $contents; | |
227 | ||
228 | } else { | |
229 | return file_get_contents($url); | |
230 | } | |
231 | ||
232 | } | |
78800912 | 233 | |
4065b60b AD |
234 | // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/) |
235 | // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php | |
78800912 | 236 | |
4065b60b | 237 | function get_favicon_url($url) { |
99331724 | 238 | |
4065b60b | 239 | if ($html = @fetch_file_contents($url)) { |
78800912 | 240 | |
4065b60b AD |
241 | if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) { |
242 | // Attempt to grab a favicon link from their webpage url | |
243 | $linkUrl = html_entity_decode($matches[1]); | |
c798704b | 244 | |
4065b60b AD |
245 | if (substr($linkUrl, 0, 1) == '/') { |
246 | $urlParts = parse_url($url); | |
247 | $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl; | |
248 | } else if (substr($linkUrl, 0, 7) == 'http://') { | |
249 | $faviconURL = $linkUrl; | |
250 | } else if (substr($url, -1, 1) == '/') { | |
251 | $faviconURL = $url.$linkUrl; | |
252 | } else { | |
253 | $faviconURL = $url.'/'.$linkUrl; | |
e695fdc8 | 254 | } |
717f5e64 | 255 | |
c798704b | 256 | } else { |
4065b60b AD |
257 | // If unsuccessful, attempt to "guess" the favicon location |
258 | $urlParts = parse_url($url); | |
259 | $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico'; | |
260 | } | |
261 | } | |
c798704b | 262 | |
4065b60b AD |
263 | // Run a test to see if what we have attempted to get actually exists. |
264 | if(USE_CURL_FOR_ICONS || url_validate($faviconURL)) { | |
265 | return $faviconURL; | |
266 | } else { | |
267 | return false; | |
268 | } | |
269 | } | |
270 | ||
271 | function url_validate($link) { | |
272 | ||
273 | $url_parts = @parse_url($link); | |
274 | ||
275 | if ( empty( $url_parts["host"] ) ) | |
276 | return false; | |
277 | ||
278 | if ( !empty( $url_parts["path"] ) ) { | |
279 | $documentpath = $url_parts["path"]; | |
280 | } else { | |
281 | $documentpath = "/"; | |
282 | } | |
283 | ||
284 | if ( !empty( $url_parts["query"] ) ) | |
285 | $documentpath .= "?" . $url_parts["query"]; | |
286 | ||
287 | $host = $url_parts["host"]; | |
288 | $port = $url_parts["port"]; | |
289 | ||
290 | if ( empty($port) ) | |
291 | $port = "80"; | |
292 | ||
293 | $socket = @fsockopen( $host, $port, $errno, $errstr, 30 ); | |
294 | ||
295 | if ( !$socket ) | |
296 | return false; | |
c798704b | 297 | |
4065b60b AD |
298 | fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n"); |
299 | ||
300 | $http_response = fgets( $socket, 22 ); | |
301 | ||
302 | $responses = "/(200 OK)|(30[0-9] Moved)/"; | |
303 | if ( preg_match($responses, $http_response) ) { | |
304 | fclose($socket); | |
305 | return true; | |
306 | } else { | |
307 | return false; | |
308 | } | |
309 | ||
310 | } | |
311 | ||
312 | function check_feed_favicon($site_url, $feed, $link) { | |
313 | $favicon_url = get_favicon_url($site_url); | |
314 | ||
315 | # print "FAVICON [$site_url]: $favicon_url\n"; | |
316 | ||
317 | error_reporting(0); | |
318 | ||
319 | $icon_file = ICONS_DIR . "/$feed.ico"; | |
320 | ||
321 | if ($favicon_url && !file_exists($icon_file)) { | |
322 | $contents = fetch_file_contents($favicon_url); | |
323 | ||
324 | $fp = fopen($icon_file, "w"); | |
78800912 | 325 | |
4065b60b AD |
326 | if ($fp) { |
327 | fwrite($fp, $contents); | |
328 | fclose($fp); | |
329 | chmod($icon_file, 0644); | |
330 | } | |
78800912 | 331 | } |
4065b60b AD |
332 | |
333 | error_reporting(DEFAULT_ERROR_LEVEL); | |
334 | ||
78800912 AD |
335 | } |
336 | ||
ddb68b81 | 337 | function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) { |
40d13c28 | 338 | |
4769ddaf | 339 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 340 | |
ddb68b81 | 341 | if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) { |
21cfcdf2 AD |
342 | return; |
343 | } | |
344 | ||
47c6c988 | 345 | $result = db_query($link, "SELECT update_interval,auth_login,auth_pass |
a88c1f36 AD |
346 | FROM ttrss_feeds WHERE id = '$feed'"); |
347 | ||
ff25e639 AD |
348 | $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login")); |
349 | $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass")); | |
47c6c988 | 350 | |
a88c1f36 AD |
351 | $update_interval = db_fetch_result($result, 0, "update_interval"); |
352 | ||
353 | if ($update_interval < 0) { return; } | |
354 | ||
ab3d0b99 AD |
355 | $feed = db_escape_string($feed); |
356 | ||
47c6c988 AD |
357 | $fetch_url = $feed_url; |
358 | ||
359 | if ($auth_login && $auth_pass) { | |
360 | $url_parts = array(); | |
361 | preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts); | |
362 | ||
363 | if ($url_parts[1] && $url_parts[2]) { | |
364 | $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2]; | |
365 | } | |
366 | ||
367 | } | |
ab3d0b99 | 368 | |
7c5a308d AD |
369 | if (RSS_BACKEND_TYPE == "magpie") { |
370 | error_reporting(0); | |
371 | $rss = fetch_rss($fetch_url); | |
372 | error_reporting (DEFAULT_ERROR_LEVEL); | |
373 | } else if (RSS_BACKEND_TYPE == "simplepie") { | |
374 | ||
375 | if (!file_exists(SIMPLEPIE_CACHE_DIR)) { | |
376 | mkdir(SIMPLEPIE_CACHE_DIR); | |
377 | } | |
76798ff3 | 378 | |
7c5a308d AD |
379 | $rss = new SimplePie(); |
380 | $rss->feed_url($fetch_url); | |
381 | $rss->cache_location(SIMPLEPIE_CACHE_DIR); | |
382 | $rss->init(); | |
383 | } | |
384 | ||
b6eefba5 | 385 | $feed = db_escape_string($feed); |
dcee8f61 | 386 | |
7c5a308d | 387 | $rss_check = $rss; |
b82af8c3 | 388 | |
7c5a308d AD |
389 | if (RSS_BACKEND_TYPE == "simplepie") { |
390 | $rss_check = $rss->data; | |
391 | } | |
392 | ||
393 | if ($rss_check) { | |
394 | ||
44e241cb | 395 | // db_query($link, "BEGIN"); |
dd8c76a9 | 396 | |
a88c1f36 | 397 | $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid |
f324892e | 398 | FROM ttrss_feeds WHERE id = '$feed'"); |
331900c6 | 399 | |
b6eefba5 AD |
400 | $registered_title = db_fetch_result($result, 0, "title"); |
401 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
f324892e | 402 | $orig_site_url = db_fetch_result($result, 0, "site_url"); |
331900c6 | 403 | |
7fed1940 AD |
404 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); |
405 | ||
8d0ec6fd | 406 | if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) { |
4065b60b | 407 | check_feed_favicon($rss->channel["link"], $feed, $link); |
a2770077 AD |
408 | } |
409 | ||
746b249f | 410 | if (!$registered_title || $registered_title == "[Unknown]") { |
7c5a308d AD |
411 | |
412 | if (RSS_BACKEND_TYPE == "magpie") { | |
413 | $feed_title = db_escape_string($rss->channel["title"]); | |
414 | } else { | |
415 | $feed_title = $rss->get_feed_title(); | |
416 | } | |
417 | ||
f324892e AD |
418 | db_query($link, "UPDATE ttrss_feeds SET |
419 | title = '$feed_title' WHERE id = '$feed'"); | |
420 | } | |
421 | ||
7c5a308d AD |
422 | if (RSS_BACKEND_TYPE == "magpie") { |
423 | $site_url = $rss->channel["link"]; | |
424 | // weird, weird Magpie | |
425 | if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]); | |
426 | } else { | |
427 | $site_url = $rss->get_feed_link(); | |
428 | } | |
147f7691 AD |
429 | |
430 | if ($site_url && $orig_site_url != db_escape_string($site_url)) { | |
f324892e AD |
431 | db_query($link, "UPDATE ttrss_feeds SET |
432 | site_url = '$site_url' WHERE id = '$feed'"); | |
331900c6 | 433 | } |
40d13c28 | 434 | |
b7f4bda2 AD |
435 | // print "I: " . $rss->channel["image"]["url"]; |
436 | ||
7c5a308d AD |
437 | if (RSS_BACKEND_TYPE == "magpie") { |
438 | $icon_url = $rss->image["url"]; | |
439 | } else { | |
440 | $icon_url = $rss->get_image_url(); # FIXME | |
441 | } | |
b7f4bda2 | 442 | |
147f7691 | 443 | if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) { |
b6eefba5 AD |
444 | $icon_url = db_escape_string($icon_url); |
445 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
b7f4bda2 AD |
446 | } |
447 | ||
e6155a06 AD |
448 | |
449 | $filters = array(); | |
450 | ||
4b3dff6e | 451 | $result = db_query($link, "SELECT reg_exp, |
db42b934 | 452 | ttrss_filter_types.name AS name, |
52db9978 | 453 | ttrss_filter_actions.name AS action |
db42b934 | 454 | FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE |
e8b79d16 | 455 | enabled = true AND |
db42b934 AD |
456 | owner_uid = $owner_uid AND |
457 | ttrss_filter_types.id = filter_type AND | |
458 | ttrss_filter_actions.id = action_id AND | |
ead60402 | 459 | (feed_id IS NULL OR feed_id = '$feed')"); |
e6155a06 | 460 | |
b6eefba5 | 461 | while ($line = db_fetch_assoc($result)) { |
e6155a06 | 462 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); |
19c9cb11 AD |
463 | |
464 | $filter["reg_exp"] = $line["reg_exp"]; | |
465 | $filter["action"] = $line["action"]; | |
466 | ||
467 | array_push($filters[$line["name"]], $filter); | |
e6155a06 AD |
468 | } |
469 | ||
7c5a308d AD |
470 | if (RSS_BACKEND_TYPE == "magpie") { |
471 | $iterator = $rss->items; | |
ddb68b81 | 472 | |
7c5a308d AD |
473 | if (!$iterator || !is_array($iterator)) $iterator = $rss->entries; |
474 | if (!$iterator || !is_array($iterator)) $iterator = $rss; | |
475 | ||
476 | } else { | |
477 | $iterator = $rss->get_items(); | |
478 | } | |
c22789da AD |
479 | |
480 | if (!is_array($iterator)) { | |
39541e74 | 481 | /* db_query($link, "UPDATE ttrss_feeds |
75bd0669 | 482 | SET last_error = 'Parse error: can\'t find any articles.' |
39541e74 | 483 | WHERE id = '$feed'"); */ |
c22789da AD |
484 | return; // WTF? |
485 | } | |
ddb68b81 AD |
486 | |
487 | foreach ($iterator as $item) { | |
7c5a308d AD |
488 | |
489 | if (RSS_BACKEND_TYPE == "magpie") { | |
490 | ||
491 | $entry_guid = $item["id"]; | |
492 | ||
493 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
494 | if (!$entry_guid) $entry_guid = $item["link"]; | |
1f64b1be AD |
495 | if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]); |
496 | ||
7c5a308d | 497 | if (!$entry_guid) continue; |
40d13c28 | 498 | |
7c5a308d AD |
499 | $entry_timestamp = ""; |
500 | ||
501 | $rss_2_date = $item['pubdate']; | |
502 | $rss_1_date = $item['dc']['date']; | |
503 | $atom_date = $item['issued']; | |
504 | if (!$atom_date) $atom_date = $item['updated']; | |
b82af8c3 | 505 | |
7c5a308d AD |
506 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); |
507 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
508 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
509 | ||
510 | if ($entry_timestamp == "") { | |
511 | $entry_timestamp = time(); | |
512 | $no_orig_date = 'true'; | |
513 | } else { | |
514 | $no_orig_date = 'false'; | |
515 | } | |
516 | ||
517 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
518 | ||
7d3ab0dd | 519 | $entry_title = trim(strip_tags($item["title"])); |
7c5a308d AD |
520 | |
521 | // strange Magpie workaround | |
522 | $entry_link = $item["link_"]; | |
523 | if (!$entry_link) $entry_link = $item["link"]; | |
524 | ||
525 | if (!$entry_title) continue; | |
1f64b1be | 526 | # if (!$entry_link) continue; |
7d3ab0dd AD |
527 | |
528 | $entry_link = strip_tags($entry_link); | |
529 | ||
7c5a308d AD |
530 | $entry_content = $item["content:escaped"]; |
531 | ||
532 | if (!$entry_content) $entry_content = $item["content:encoded"]; | |
533 | if (!$entry_content) $entry_content = $item["content"]; | |
79b5d2d2 | 534 | if (!$entry_content) $entry_content = $item["atom_content"]; |
7c5a308d AD |
535 | if (!$entry_content) $entry_content = $item["summary"]; |
536 | if (!$entry_content) $entry_content = $item["description"]; | |
537 | ||
538 | // if (!$entry_content) continue; | |
539 | ||
540 | // WTF | |
541 | if (is_array($entry_content)) { | |
542 | $entry_content = $entry_content["encoded"]; | |
543 | if (!$entry_content) $entry_content = $entry_content["escaped"]; | |
544 | } | |
545 | ||
546 | // print_r($item); | |
547 | // print_r(htmlspecialchars($entry_content)); | |
548 | // print "<br>"; | |
549 | ||
550 | $entry_content_unescaped = $entry_content; | |
551 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); | |
552 | ||
7d3ab0dd | 553 | $entry_comments = strip_tags($item["comments"]); |
bbb416e5 | 554 | |
7d3ab0dd | 555 | $entry_author = db_escape_string(strip_tags($item['dc']['creator'])); |
bbb416e5 AD |
556 | |
557 | if (!$entry_author) { | |
558 | $entry_author = db_escape_string(strip_tags($item['author'])); | |
559 | } | |
560 | ||
7d3ab0dd | 561 | $entry_guid = db_escape_string(strip_tags($entry_guid)); |
7c5a308d AD |
562 | |
563 | $result = db_query($link, "SELECT id FROM ttrss_entries | |
564 | WHERE guid = '$entry_guid'"); | |
565 | ||
566 | $entry_content = db_escape_string($entry_content); | |
567 | $entry_title = db_escape_string($entry_title); | |
568 | $entry_link = db_escape_string($entry_link); | |
569 | $entry_comments = db_escape_string($entry_comments); | |
570 | ||
571 | $num_comments = db_escape_string($item["slash"]["comments"]); | |
572 | ||
573 | if (!$num_comments) $num_comments = 0; | |
8add756a AD |
574 | } |
575 | ||
d48d160c | 576 | # sanitize content |
183ad07b AD |
577 | |
578 | $entry_content = sanitize_rss($entry_content); | |
d48d160c | 579 | |
44e241cb AD |
580 | db_query($link, "BEGIN"); |
581 | ||
4c193675 AD |
582 | if (db_num_rows($result) == 0) { |
583 | ||
584 | // base post entry does not exist, create it | |
585 | ||
4c193675 AD |
586 | $result = db_query($link, |
587 | "INSERT INTO ttrss_entries | |
588 | (title, | |
589 | guid, | |
590 | link, | |
591 | updated, | |
592 | content, | |
593 | content_hash, | |
594 | no_orig_date, | |
595 | date_entered, | |
11b0dce2 | 596 | comments, |
b6104dee AD |
597 | num_comments, |
598 | author) | |
4c193675 AD |
599 | VALUES |
600 | ('$entry_title', | |
601 | '$entry_guid', | |
602 | '$entry_link', | |
603 | '$entry_timestamp_fmt', | |
604 | '$entry_content', | |
605 | '$content_hash', | |
606 | $no_orig_date, | |
607 | NOW(), | |
11b0dce2 | 608 | '$entry_comments', |
b6104dee AD |
609 | '$num_comments', |
610 | '$entry_author')"); | |
8926aab8 AD |
611 | } else { |
612 | // we keep encountering the entry in feeds, so we need to | |
613 | // update date_entered column so that we don't get horrible | |
614 | // dupes when the entry gets purged and reinserted again e.g. | |
615 | // in the case of SLOW SLOW OMG SLOW updating feeds | |
616 | ||
617 | $base_entry_id = db_fetch_result($result, 0, "id"); | |
618 | ||
619 | db_query($link, "UPDATE ttrss_entries SET date_entered = NOW() | |
620 | WHERE id = '$base_entry_id'"); | |
4c193675 AD |
621 | } |
622 | ||
623 | // now it should exist, if not - bad luck then | |
624 | ||
6385315d AD |
625 | $result = db_query($link, "SELECT |
626 | id,content_hash,no_orig_date,title, | |
8926aab8 | 627 | substring(date_entered,1,19) as date_entered, |
11b0dce2 AD |
628 | substring(updated,1,19) as updated, |
629 | num_comments | |
6385315d AD |
630 | FROM |
631 | ttrss_entries | |
632 | WHERE guid = '$entry_guid'"); | |
4c193675 AD |
633 | |
634 | if (db_num_rows($result) == 1) { | |
635 | ||
11b0dce2 AD |
636 | // this will be used below in update handler |
637 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); | |
638 | $orig_title = db_fetch_result($result, 0, "title"); | |
639 | $orig_num_comments = db_fetch_result($result, 0, "num_comments"); | |
8926aab8 AD |
640 | $orig_date_entered = strtotime(db_fetch_result($result, |
641 | 0, "date_entered")); | |
6385315d | 642 | |
11b0dce2 | 643 | $ref_id = db_fetch_result($result, 0, "id"); |
4c193675 | 644 | |
11b0dce2 | 645 | // check for user post link to main table |
4c193675 | 646 | |
11b0dce2 | 647 | // do we allow duplicate posts with same GUID in different feeds? |
8d0ec6fd | 648 | if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) { |
11b0dce2 AD |
649 | $dupcheck_qpart = "AND feed_id = '$feed'"; |
650 | } else { | |
651 | $dupcheck_qpart = ""; | |
652 | } | |
71604ca4 | 653 | |
11b0dce2 | 654 | // error_reporting(0); |
19c9cb11 | 655 | |
11b0dce2 AD |
656 | $filter_name = get_filter_name($entry_title, $entry_content, |
657 | $entry_link, $filters); | |
19c9cb11 | 658 | |
11b0dce2 AD |
659 | if ($filter_name == "filter") { |
660 | continue; | |
661 | } | |
19c9cb11 | 662 | |
11b0dce2 | 663 | // error_reporting (DEFAULT_ERROR_LEVEL); |
3a933f22 | 664 | |
11b0dce2 AD |
665 | $result = db_query($link, |
666 | "SELECT ref_id FROM ttrss_user_entries WHERE | |
667 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' | |
668 | $dupcheck_qpart"); | |
669 | ||
670 | // okay it doesn't exist - create user entry | |
671 | if (db_num_rows($result) == 0) { | |
672 | ||
673 | if ($filter_name != 'catchup') { | |
674 | $unread = 'true'; | |
675 | $last_read_qpart = 'NULL'; | |
676 | } else { | |
677 | $unread = 'false'; | |
678 | $last_read_qpart = 'NOW()'; | |
679 | } | |
dd7d3187 AD |
680 | |
681 | if ($filter_name == 'mark') { | |
682 | $marked = 'true'; | |
683 | } else { | |
684 | $marked = 'false'; | |
685 | } | |
19c9cb11 | 686 | |
11b0dce2 AD |
687 | $result = db_query($link, |
688 | "INSERT INTO ttrss_user_entries | |
dd7d3187 | 689 | (ref_id, owner_uid, feed_id, unread, last_read, marked) |
11b0dce2 | 690 | VALUES ('$ref_id', '$owner_uid', '$feed', $unread, |
dd7d3187 | 691 | $last_read_qpart, $marked)"); |
11b0dce2 AD |
692 | } |
693 | ||
6385315d AD |
694 | $post_needs_update = false; |
695 | ||
8d0ec6fd | 696 | if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) && |
6385315d AD |
697 | ($content_hash != $orig_content_hash)) { |
698 | $post_needs_update = true; | |
699 | } | |
700 | ||
701 | if ($orig_title != $entry_title) { | |
702 | $post_needs_update = true; | |
703 | } | |
704 | ||
11b0dce2 AD |
705 | if ($orig_num_comments != $num_comments) { |
706 | $post_needs_update = true; | |
707 | } | |
708 | ||
6385315d AD |
709 | // this doesn't seem to be very reliable |
710 | // | |
711 | // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) { | |
712 | // $post_needs_update = true; | |
713 | // } | |
714 | ||
715 | // if post needs update, update it and mark all user entries | |
1c73bc0c | 716 | // linking to this post as updated |
6385315d AD |
717 | if ($post_needs_update) { |
718 | ||
719 | // print "<!-- post $orig_title needs update : $post_needs_update -->"; | |
720 | ||
6385315d | 721 | db_query($link, "UPDATE ttrss_entries |
11b0dce2 AD |
722 | SET title = '$entry_title', content = '$entry_content', |
723 | num_comments = '$num_comments' | |
6385315d AD |
724 | WHERE id = '$ref_id'"); |
725 | ||
8d0ec6fd | 726 | if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) { |
4919fb42 AD |
727 | db_query($link, "UPDATE ttrss_user_entries |
728 | SET last_read = null, unread = true WHERE ref_id = '$ref_id'"); | |
729 | } else { | |
730 | db_query($link, "UPDATE ttrss_user_entries | |
731 | SET last_read = null WHERE ref_id = '$ref_id' AND unread = false"); | |
732 | } | |
6385315d AD |
733 | |
734 | } | |
4c193675 AD |
735 | } |
736 | ||
44e241cb AD |
737 | db_query($link, "COMMIT"); |
738 | ||
eb36b4eb AD |
739 | /* taaaags */ |
740 | // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, // | |
741 | ||
05732aa0 | 742 | $entry_tags = null; |
eb36b4eb | 743 | |
372ced8b | 744 | preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i", |
ee2c3050 AD |
745 | $entry_content_unescaped, $entry_tags); |
746 | ||
747 | // print "<br>$entry_title : $entry_content_unescaped<br>"; | |
748 | // print_r($entry_tags); | |
372ced8b | 749 | // print "<br>"; |
eb36b4eb AD |
750 | |
751 | $entry_tags = $entry_tags[1]; | |
752 | ||
753 | if (count($entry_tags) > 0) { | |
754 | ||
44e241cb AD |
755 | db_query($link, "BEGIN"); |
756 | ||
05732aa0 AD |
757 | $result = db_query($link, "SELECT id,int_id |
758 | FROM ttrss_entries,ttrss_user_entries | |
25da6909 | 759 | WHERE guid = '$entry_guid' |
05732aa0 | 760 | AND feed_id = '$feed' AND ref_id = id |
7fed1940 | 761 | AND owner_uid = '$owner_uid'"); |
eb36b4eb | 762 | |
fe99ab12 | 763 | if (db_num_rows($result) == 1) { |
eb36b4eb | 764 | |
fe99ab12 AD |
765 | $entry_id = db_fetch_result($result, 0, "id"); |
766 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
767 | ||
768 | foreach ($entry_tags as $tag) { | |
769 | $tag = db_escape_string(strtolower($tag)); | |
31483fc1 AD |
770 | |
771 | $tag = str_replace("+", " ", $tag); | |
fe99ab12 AD |
772 | $tag = str_replace("technorati tag: ", "", $tag); |
773 | ||
774 | $result = db_query($link, "SELECT id FROM ttrss_tags | |
775 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND | |
776 | owner_uid = '$owner_uid' LIMIT 1"); | |
777 | ||
778 | // print db_fetch_result($result, 0, "id"); | |
779 | ||
780 | if ($result && db_num_rows($result) == 0) { | |
781 | ||
782 | // print "tagging $entry_id as $tag<br>"; | |
783 | ||
784 | db_query($link, "INSERT INTO ttrss_tags | |
785 | (owner_uid,tag_name,post_int_id) | |
786 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
787 | } | |
788 | } | |
eb36b4eb | 789 | } |
44e241cb | 790 | db_query($link, "COMMIT"); |
05732aa0 | 791 | } |
4c193675 | 792 | } |
40d13c28 | 793 | |
ab3d0b99 AD |
794 | db_query($link, "UPDATE ttrss_feeds |
795 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
eb36b4eb | 796 | |
44e241cb | 797 | // db_query($link, "COMMIT"); |
dd8c76a9 | 798 | |
ab3d0b99 AD |
799 | } else { |
800 | $error_msg = db_escape_string(magpie_error()); | |
801 | db_query($link, | |
aa5f9f5f AD |
802 | "UPDATE ttrss_feeds SET last_error = '$error_msg', |
803 | last_updated = NOW() WHERE id = '$feed'"); | |
40d13c28 AD |
804 | } |
805 | ||
806 | } | |
807 | ||
f175937c | 808 | function print_select($id, $default, $values, $attributes = "") { |
79f3553b | 809 | print "<select name=\"$id\" id=\"$id\" $attributes>"; |
a0d53889 AD |
810 | foreach ($values as $v) { |
811 | if ($v == $default) | |
812 | $sel = " selected"; | |
813 | else | |
814 | $sel = ""; | |
815 | ||
816 | print "<option$sel>$v</option>"; | |
817 | } | |
818 | print "</select>"; | |
819 | } | |
40d13c28 | 820 | |
79f3553b AD |
821 | function print_select_hash($id, $default, $values, $attributes = "") { |
822 | print "<select name=\"$id\" id='$id' $attributes>"; | |
673d54ca AD |
823 | foreach (array_keys($values) as $v) { |
824 | if ($v == $default) | |
825 | $sel = "selected"; | |
826 | else | |
827 | $sel = ""; | |
828 | ||
829 | print "<option $sel value=\"$v\">".$values[$v]."</option>"; | |
830 | } | |
831 | ||
832 | print "</select>"; | |
833 | } | |
834 | ||
19c9cb11 | 835 | function get_filter_name($title, $content, $link, $filters) { |
e6155a06 AD |
836 | |
837 | if ($filters["title"]) { | |
19c9cb11 AD |
838 | foreach ($filters["title"] as $filter) { |
839 | $reg_exp = $filter["reg_exp"]; | |
840 | if (preg_match("/$reg_exp/i", $title)) { | |
841 | return $filter["action"]; | |
842 | } | |
e6155a06 AD |
843 | } |
844 | } | |
845 | ||
846 | if ($filters["content"]) { | |
19c9cb11 AD |
847 | foreach ($filters["content"] as $filter) { |
848 | $reg_exp = $filter["reg_exp"]; | |
849 | if (preg_match("/$reg_exp/i", $content)) { | |
850 | return $filter["action"]; | |
851 | } | |
e6155a06 AD |
852 | } |
853 | } | |
854 | ||
855 | if ($filters["both"]) { | |
856 | foreach ($filters["both"] as $filter) { | |
19c9cb11 AD |
857 | $reg_exp = $filter["reg_exp"]; |
858 | if (preg_match("/$reg_exp/i", $title) || | |
859 | preg_match("/$reg_exp/i", $content)) { | |
860 | return $filter["action"]; | |
861 | } | |
e6155a06 AD |
862 | } |
863 | } | |
864 | ||
3a933f22 | 865 | if ($filters["link"]) { |
19c9cb11 AD |
866 | $reg_exp = $filter["reg_exp"]; |
867 | foreach ($filters["link"] as $filter) { | |
868 | $reg_exp = $filter["reg_exp"]; | |
869 | if (preg_match("/$reg_exp/i", $link)) { | |
870 | return $filter["action"]; | |
871 | } | |
3a933f22 AD |
872 | } |
873 | } | |
874 | ||
e6155a06 AD |
875 | return false; |
876 | } | |
877 | ||
9323147e | 878 | function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link, |
fb1fb4ab | 879 | $rtl_content = false, $last_updated = false, $last_error = false) { |
254e0e4b AD |
880 | |
881 | if (file_exists($icon_file) && filesize($icon_file) > 0) { | |
023fe037 | 882 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">"; |
254e0e4b | 883 | } else { |
023fe037 | 884 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">"; |
254e0e4b AD |
885 | } |
886 | ||
9323147e AD |
887 | if ($rtl_content) { |
888 | $rtl_tag = "dir=\"rtl\""; | |
889 | } else { | |
890 | $rtl_tag = "dir=\"ltr\""; | |
891 | } | |
892 | ||
78d5212c AD |
893 | $error_notify_msg = ""; |
894 | ||
fb1fb4ab AD |
895 | if ($last_error) { |
896 | $link_title = "Error: $last_error ($last_updated)"; | |
78d5212c | 897 | $error_notify_msg = "(Error)"; |
ad780e9c | 898 | } else if ($last_updated) { |
fb1fb4ab AD |
899 | $link_title = "Updated: $last_updated"; |
900 | } | |
901 | ||
7210613a AD |
902 | $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" |
903 | href=\"#\" onclick=\"javascript:viewfeed('$feed_id', '', false);\">$feed_title</a>"; | |
254e0e4b AD |
904 | |
905 | print "<li id=\"FEEDR-$feed_id\" class=\"$class\">"; | |
b619ff15 | 906 | if (get_pref($link, 'ENABLE_FEED_ICONS')) { |
254e0e4b AD |
907 | print "$feed_icon"; |
908 | } | |
909 | ||
9323147e | 910 | print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>"; |
254e0e4b AD |
911 | |
912 | if ($unread != 0) { | |
913 | $fctr_class = ""; | |
914 | } else { | |
915 | $fctr_class = "class=\"invisible\""; | |
916 | } | |
917 | ||
9323147e | 918 | print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\"> |
254e0e4b | 919 | (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>"; |
78d5212c AD |
920 | |
921 | if (get_pref($link, "EXTENDED_FEEDLIST")) { | |
922 | print "<div class=\"feedExtInfo\"> | |
923 | <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>"; | |
924 | } | |
925 | ||
254e0e4b AD |
926 | print "</li>"; |
927 | ||
928 | } | |
929 | ||
406d9489 AD |
930 | function getmicrotime() { |
931 | list($usec, $sec) = explode(" ",microtime()); | |
932 | return ((float)$usec + (float)$sec); | |
933 | } | |
934 | ||
77e96719 AD |
935 | function print_radio($id, $default, $values, $attributes = "") { |
936 | foreach ($values as $v) { | |
937 | ||
938 | if ($v == $default) | |
5da169d9 | 939 | $sel = "checked"; |
77e96719 | 940 | else |
5da169d9 AD |
941 | $sel = ""; |
942 | ||
943 | if ($v == "Yes") { | |
944 | $sel .= " value=\"1\""; | |
945 | } else { | |
946 | $sel .= " value=\"0\""; | |
947 | } | |
77e96719 | 948 | |
69654950 AD |
949 | print "<input class=\"noborder\" |
950 | type=\"radio\" $sel $attributes name=\"$id\"> $v "; | |
77e96719 AD |
951 | |
952 | } | |
953 | } | |
954 | ||
ff485f1d AD |
955 | function initialize_user_prefs($link, $uid) { |
956 | ||
957 | $uid = db_escape_string($uid); | |
958 | ||
959 | db_query($link, "BEGIN"); | |
960 | ||
961 | $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs"); | |
962 | ||
963 | $u_result = db_query($link, "SELECT pref_name | |
964 | FROM ttrss_user_prefs WHERE owner_uid = '$uid'"); | |
965 | ||
966 | $active_prefs = array(); | |
967 | ||
968 | while ($line = db_fetch_assoc($u_result)) { | |
969 | array_push($active_prefs, $line["pref_name"]); | |
970 | } | |
971 | ||
972 | while ($line = db_fetch_assoc($result)) { | |
973 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
974 | // print "adding " . $line["pref_name"] . "<br>"; | |
975 | ||
976 | db_query($link, "INSERT INTO ttrss_user_prefs | |
977 | (owner_uid,pref_name,value) VALUES | |
978 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); | |
979 | ||
980 | } | |
981 | } | |
982 | ||
983 | db_query($link, "COMMIT"); | |
984 | ||
985 | } | |
956c7629 AD |
986 | |
987 | function lookup_user_id($link, $user) { | |
988 | ||
989 | $result = db_query($link, "SELECT id FROM ttrss_users WHERE | |
990 | login = '$login'"); | |
991 | ||
992 | if (db_num_rows($result) == 1) { | |
993 | return db_fetch_result($result, 0, "id"); | |
994 | } else { | |
995 | return false; | |
996 | } | |
997 | } | |
998 | ||
18664970 AD |
999 | function http_authenticate_user($link) { |
1000 | ||
1001 | if (!$_SERVER["PHP_AUTH_USER"]) { | |
1002 | ||
1003 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"'); | |
1004 | header('HTTP/1.0 401 Unauthorized'); | |
1005 | exit; | |
1006 | ||
1007 | } else { | |
1008 | $auth_result = authenticate_user($link, | |
1009 | $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]); | |
1010 | ||
1011 | if (!$auth_result) { | |
1012 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"'); | |
1013 | header('HTTP/1.0 401 Unauthorized'); | |
1014 | exit; | |
1015 | } | |
1016 | } | |
1017 | ||
1018 | return true; | |
1019 | } | |
1020 | ||
461766f3 | 1021 | function authenticate_user($link, $login, $password, $force_auth = false) { |
c8437f35 | 1022 | |
131b01b3 | 1023 | if (!SINGLE_USER_MODE) { |
c8437f35 | 1024 | |
131b01b3 | 1025 | $pwd_hash = 'SHA1:' . sha1($password); |
461766f3 AD |
1026 | |
1027 | if ($force_auth && defined('_DEBUG_USER_SWITCH')) { | |
1028 | $query = "SELECT id,login,access_level | |
1029 | FROM ttrss_users WHERE | |
1030 | login = '$login'"; | |
1031 | } else { | |
1032 | $query = "SELECT id,login,access_level | |
1033 | FROM ttrss_users WHERE | |
1034 | login = '$login' AND pwd_hash = '$pwd_hash'"; | |
1035 | } | |
1036 | ||
1037 | $result = db_query($link, $query); | |
131b01b3 AD |
1038 | |
1039 | if (db_num_rows($result) == 1) { | |
1040 | $_SESSION["uid"] = db_fetch_result($result, 0, "id"); | |
1041 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); | |
1042 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); | |
1043 | ||
1044 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
1045 | $_SESSION["uid"]); | |
1046 | ||
1047 | $user_theme = get_user_theme_path($link); | |
1048 | ||
1049 | $_SESSION["theme"] = $user_theme; | |
1050 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
1051 | ||
1052 | initialize_user_prefs($link, $_SESSION["uid"]); | |
1053 | ||
1054 | return true; | |
1055 | } | |
1056 | ||
1057 | return false; | |
503eb349 | 1058 | |
131b01b3 | 1059 | } else { |
503eb349 | 1060 | |
131b01b3 AD |
1061 | $_SESSION["uid"] = 1; |
1062 | $_SESSION["name"] = "admin"; | |
f557cd78 | 1063 | |
0bbba72d AD |
1064 | $user_theme = get_user_theme_path($link); |
1065 | ||
1066 | $_SESSION["theme"] = $user_theme; | |
1067 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
1068 | ||
1069 | initialize_user_prefs($link, $_SESSION["uid"]); | |
1070 | ||
c8437f35 AD |
1071 | return true; |
1072 | } | |
c8437f35 AD |
1073 | } |
1074 | ||
e6cb77a0 AD |
1075 | function make_password($length = 8) { |
1076 | ||
1077 | $password = ""; | |
798f722b AD |
1078 | $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ"; |
1079 | ||
1080 | $i = 0; | |
e6cb77a0 AD |
1081 | |
1082 | while ($i < $length) { | |
1083 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
1084 | ||
1085 | if (!strstr($password, $char)) { | |
1086 | $password .= $char; | |
1087 | $i++; | |
1088 | } | |
1089 | } | |
1090 | return $password; | |
1091 | } | |
1092 | ||
1093 | // this is called after user is created to initialize default feeds, labels | |
1094 | // or whatever else | |
1095 | ||
1096 | // user preferences are checked on every login, not here | |
1097 | ||
1098 | function initialize_user($link, $uid) { | |
1099 | ||
1100 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
1101 | values ('$uid','unread = true', 'Unread articles')"); | |
1102 | ||
1103 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
1104 | values ('$uid','last_read is null and unread = false', 'Updated articles')"); | |
1105 | ||
1106 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
74bff337 | 1107 | values ('$uid', 'Tiny Tiny RSS: New Releases', |
628fcd2c | 1108 | 'http://tt-rss.spb.ru/releases.rss')"); |
3b0feb9b AD |
1109 | |
1110 | } | |
e6cb77a0 | 1111 | |
b8aa49bc | 1112 | function logout_user() { |
5ccc1cf5 AD |
1113 | session_destroy(); |
1114 | if (isset($_COOKIE[session_name()])) { | |
1115 | setcookie(session_name(), '', time()-42000, '/'); | |
1116 | } | |
b8aa49bc AD |
1117 | } |
1118 | ||
75836f33 | 1119 | function get_script_urlpath() { |
87a79fa4 | 1120 | return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]); |
75836f33 AD |
1121 | } |
1122 | ||
1123 | function get_login_redirect() { | |
1124 | $server = $_SERVER["SERVER_NAME"]; | |
1125 | ||
1126 | if (ENABLE_LOGIN_SSL) { | |
1127 | $protocol = "https"; | |
1128 | } else { | |
1129 | $protocol = "http"; | |
1130 | } | |
1131 | ||
1132 | $url_path = get_script_urlpath(); | |
1133 | ||
1134 | $redirect_uri = "$protocol://$server$url_path/login.php"; | |
1135 | ||
1136 | return $redirect_uri; | |
1137 | } | |
1138 | ||
916f788a | 1139 | function validate_session($link) { |
a2e9b457 | 1140 | if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) { |
916f788a AD |
1141 | if ($_SESSION["ip_address"]) { |
1142 | if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) { | |
1143 | return false; | |
1144 | } | |
1145 | } | |
1146 | } | |
1147 | return true; | |
1148 | } | |
1149 | ||
7ae65adf AD |
1150 | function basic_nosid_redirect_check() { |
1151 | if (!SINGLE_USER_MODE) { | |
3dd46f19 | 1152 | if (!$_COOKIE[get_session_cookie_name()]) { |
7ae65adf AD |
1153 | $redirect_uri = get_login_redirect(); |
1154 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); | |
1155 | header("Location: $redirect_uri?rt=$return_to"); | |
1156 | exit; | |
1157 | } | |
1158 | } | |
1159 | } | |
1160 | ||
b8aa49bc AD |
1161 | function login_sequence($link) { |
1162 | if (!SINGLE_USER_MODE) { | |
75836f33 | 1163 | |
461766f3 AD |
1164 | if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) { |
1165 | $swu = db_escape_string($_REQUEST["swu"]); | |
1166 | if ($swu) { | |
1167 | $_SESSION["prefs_cache"] = false; | |
1168 | return authenticate_user($link, $swu, null, true); | |
1169 | } | |
1170 | } | |
1171 | ||
916f788a AD |
1172 | if (!validate_session($link)) { |
1173 | logout_user(); | |
1174 | $redirect_uri = get_login_redirect(); | |
1175 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); | |
1176 | header("Location: $redirect_uri?rt=$return_to"); | |
1177 | exit; | |
1178 | } | |
1179 | ||
b8aa49bc AD |
1180 | if (!USE_HTTP_AUTH) { |
1181 | if (!$_SESSION["uid"]) { | |
75836f33 | 1182 | $redirect_uri = get_login_redirect(); |
e31dca14 AD |
1183 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); |
1184 | header("Location: $redirect_uri?rt=$return_to"); | |
b8aa49bc AD |
1185 | exit; |
1186 | } | |
1187 | } else { | |
f557cd78 AD |
1188 | if (!$_SESSION["uid"]) { |
1189 | if (!$_SERVER["PHP_AUTH_USER"]) { | |
1190 | ||
1191 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
1192 | header('HTTP/1.0 401 Unauthorized'); | |
1193 | exit; | |
1194 | ||
1195 | } else { | |
1196 | $auth_result = authenticate_user($link, | |
1197 | $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]); | |
1198 | ||
1199 | if (!$auth_result) { | |
1200 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
1201 | header('HTTP/1.0 401 Unauthorized'); | |
1202 | exit; | |
1203 | } | |
1204 | } | |
1205 | } | |
b8aa49bc AD |
1206 | } |
1207 | } else { | |
0bbba72d | 1208 | return authenticate_user($link, "admin", null); |
b8aa49bc AD |
1209 | } |
1210 | } | |
3547842a AD |
1211 | |
1212 | function truncate_string($str, $max_len) { | |
12db369c AD |
1213 | if (mb_strlen($str, "utf-8") > $max_len - 3) { |
1214 | return mb_substr($str, 0, $max_len, "utf-8") . "..."; | |
3547842a AD |
1215 | } else { |
1216 | return $str; | |
1217 | } | |
1218 | } | |
54a60e1a AD |
1219 | |
1220 | function get_user_theme_path($link) { | |
798f722b AD |
1221 | $result = db_query($link, "SELECT theme_path |
1222 | FROM | |
1223 | ttrss_themes,ttrss_users | |
1224 | WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]); | |
54a60e1a AD |
1225 | if (db_num_rows($result) != 0) { |
1226 | return db_fetch_result($result, 0, "theme_path"); | |
1227 | } else { | |
1228 | return null; | |
1229 | } | |
1230 | } | |
be773442 AD |
1231 | |
1232 | function smart_date_time($timestamp) { | |
1233 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1234 | return date("G:i", $timestamp); | |
f26450f1 | 1235 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
1236 | return date("M d, G:i", $timestamp); |
1237 | } else { | |
b02111c2 | 1238 | return date("Y/m/d G:i", $timestamp); |
be773442 AD |
1239 | } |
1240 | } | |
1241 | ||
1242 | function smart_date($timestamp) { | |
1243 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1244 | return "Today"; | |
f26450f1 | 1245 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
1246 | return date("D m", $timestamp); |
1247 | } else { | |
b02111c2 | 1248 | return date("Y/m/d", $timestamp); |
be773442 AD |
1249 | } |
1250 | } | |
a654a595 AD |
1251 | |
1252 | function sql_bool_to_string($s) { | |
1253 | if ($s == "t" || $s == "1") { | |
1254 | return "true"; | |
1255 | } else { | |
1256 | return "false"; | |
1257 | } | |
1258 | } | |
e3c99f3b AD |
1259 | |
1260 | function sql_bool_to_bool($s) { | |
1261 | if ($s == "t" || $s == "1") { | |
1262 | return true; | |
1263 | } else { | |
1264 | return false; | |
1265 | } | |
1266 | } | |
0ea4fb50 | 1267 | |
e3c99f3b | 1268 | |
0ea4fb50 AD |
1269 | function toggleEvenOdd($a) { |
1270 | if ($a == "even") | |
1271 | return "odd"; | |
1272 | else | |
1273 | return "even"; | |
1274 | } | |
6043fb7e AD |
1275 | |
1276 | function sanity_check($link) { | |
9cbca41f | 1277 | |
aec3ce39 AD |
1278 | error_reporting(0); |
1279 | ||
6043fb7e AD |
1280 | $error_code = 0; |
1281 | $result = db_query($link, "SELECT schema_version FROM ttrss_version"); | |
1282 | $schema_version = db_fetch_result($result, 0, "schema_version"); | |
1283 | ||
1284 | if ($schema_version != SCHEMA_VERSION) { | |
1285 | $error_code = 5; | |
1286 | } | |
1287 | ||
aec3ce39 AD |
1288 | if (DB_TYPE == "mysql") { |
1289 | $result = db_query($link, "SELECT true", false); | |
1290 | if (db_num_rows($result) != 1) { | |
1291 | $error_code = 10; | |
1292 | } | |
1293 | } | |
1294 | ||
1295 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1296 | ||
6043fb7e | 1297 | if ($error_code != 0) { |
aec3ce39 | 1298 | print_error_xml($error_code); |
6043fb7e AD |
1299 | return false; |
1300 | } else { | |
1301 | return true; | |
9cbca41f | 1302 | } |
6043fb7e AD |
1303 | } |
1304 | ||
27981ca3 AD |
1305 | function file_is_locked($filename) { |
1306 | error_reporting(0); | |
1307 | $fp = fopen($filename, "r"); | |
1308 | error_reporting(DEFAULT_ERROR_LEVEL); | |
1309 | if ($fp) { | |
1310 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1311 | flock($fp, LOCK_UN); | |
1312 | fclose($fp); | |
1313 | return false; | |
1314 | } | |
1315 | fclose($fp); | |
1316 | return true; | |
1317 | } | |
1318 | return false; | |
1319 | } | |
1320 | ||
fcb4c0c9 AD |
1321 | function make_lockfile($filename) { |
1322 | $fp = fopen($filename, "w"); | |
1323 | ||
1324 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1325 | return $fp; | |
1326 | } else { | |
1327 | return false; | |
1328 | } | |
1329 | } | |
1330 | ||
894ebcf5 AD |
1331 | function sql_random_function() { |
1332 | if (DB_TYPE == "mysql") { | |
1333 | return "RAND()"; | |
1334 | } else { | |
1335 | return "RANDOM()"; | |
1336 | } | |
1337 | } | |
1338 | ||
23aa0d16 | 1339 | function catchup_feed($link, $feed, $cat_view) { |
88040f57 AD |
1340 | |
1341 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
23aa0d16 AD |
1342 | |
1343 | if ($cat_view) { | |
1344 | ||
1345 | if ($feed > 0) { | |
1346 | $cat_qpart = "cat_id = '$feed'"; | |
1347 | } else { | |
1348 | $cat_qpart = "cat_id IS NULL"; | |
1349 | } | |
1350 | ||
1351 | $tmp_result = db_query($link, "SELECT id | |
1352 | FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " . | |
1353 | $_SESSION["uid"]); | |
1354 | ||
1355 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1356 | ||
1357 | $tmp_feed = $tmp_line["id"]; | |
1358 | ||
1359 | db_query($link, "UPDATE ttrss_user_entries | |
1360 | SET unread = false,last_read = NOW() | |
1361 | WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]); | |
1362 | } | |
1363 | ||
1364 | } else if ($feed > 0) { | |
1365 | ||
1366 | $tmp_result = db_query($link, "SELECT id | |
1367 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
1368 | ORDER BY cat_id,title"); | |
1369 | ||
1370 | $parent_ids = array(); | |
1371 | ||
1372 | if (db_num_rows($tmp_result) > 0) { | |
1373 | while ($p = db_fetch_assoc($tmp_result)) { | |
1374 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
1375 | } | |
1376 | ||
1377 | $children_qpart = implode(" OR ", $parent_ids); | |
1378 | ||
1379 | db_query($link, "UPDATE ttrss_user_entries | |
1380 | SET unread = false,last_read = NOW() | |
1381 | WHERE (feed_id = '$feed' OR $children_qpart) | |
1382 | AND owner_uid = " . $_SESSION["uid"]); | |
1383 | ||
1384 | } else { | |
1385 | db_query($link, "UPDATE ttrss_user_entries | |
1386 | SET unread = false,last_read = NOW() | |
1387 | WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
1388 | } | |
1389 | ||
1390 | } else if ($feed < 0 && $feed > -10) { // special, like starred | |
1391 | ||
1392 | if ($feed == -1) { | |
1393 | db_query($link, "UPDATE ttrss_user_entries | |
1394 | SET unread = false,last_read = NOW() | |
1395 | WHERE marked = true AND owner_uid = ".$_SESSION["uid"]); | |
1396 | } | |
1397 | ||
1398 | } else if ($feed < -10) { // label | |
1399 | ||
1400 | // TODO make this more efficient | |
1401 | ||
1402 | $label_id = -$feed - 11; | |
1403 | ||
1404 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
1405 | WHERE id = '$label_id'"); | |
1406 | ||
1407 | if ($tmp_result) { | |
1408 | $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp"); | |
1409 | ||
1410 | db_query($link, "BEGIN"); | |
1411 | ||
1412 | $tmp2_result = db_query($link, | |
1413 | "SELECT | |
1414 | int_id | |
1415 | FROM | |
88040f57 | 1416 | ttrss_user_entries,ttrss_entries,ttrss_feeds |
23aa0d16 | 1417 | WHERE |
88040f57 AD |
1418 | ref_id = ttrss_entries.id AND |
1419 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
23aa0d16 | 1420 | $sql_exp AND |
88040f57 | 1421 | ttrss_user_entries.owner_uid = " . $_SESSION["uid"]); |
23aa0d16 AD |
1422 | |
1423 | while ($tmp_line = db_fetch_assoc($tmp2_result)) { | |
1424 | db_query($link, "UPDATE | |
1425 | ttrss_user_entries | |
1426 | SET | |
1427 | unread = false, last_read = NOW() | |
1428 | WHERE | |
1429 | int_id = " . $tmp_line["int_id"]); | |
1430 | } | |
1431 | ||
1432 | db_query($link, "COMMIT"); | |
1433 | ||
1434 | /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries | |
1435 | SET unread = false,last_read = NOW() | |
1436 | WHERE $sql_exp | |
1437 | AND ref_id = id | |
1438 | AND owner_uid = ".$_SESSION["uid"]); */ | |
1439 | } | |
1440 | } | |
1441 | } else { // tag | |
1442 | db_query($link, "BEGIN"); | |
1443 | ||
1444 | $tag_name = db_escape_string($feed); | |
1445 | ||
1446 | $result = db_query($link, "SELECT post_int_id FROM ttrss_tags | |
1447 | WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]); | |
1448 | ||
1449 | while ($line = db_fetch_assoc($result)) { | |
1450 | db_query($link, "UPDATE ttrss_user_entries SET | |
1451 | unread = false, last_read = NOW() | |
1452 | WHERE int_id = " . $line["post_int_id"]); | |
1453 | } | |
1454 | db_query($link, "COMMIT"); | |
1455 | } | |
1456 | } | |
1457 | ||
1458 | function update_generic_feed($link, $feed, $cat_view) { | |
1459 | if ($cat_view) { | |
1460 | ||
1461 | if ($feed > 0) { | |
1462 | $cat_qpart = "cat_id = '$feed'"; | |
1463 | } else { | |
1464 | $cat_qpart = "cat_id IS NULL"; | |
1465 | } | |
1466 | ||
1467 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1468 | WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]); | |
1469 | ||
1470 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1471 | $feed_url = $tmp_line["feed_url"]; | |
1472 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1473 | } | |
1474 | ||
1475 | } else { | |
1476 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1477 | WHERE id = '$feed'"); | |
1478 | $feed_url = db_fetch_result($tmp_result, 0, "feed_url"); | |
1479 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1480 | } | |
1481 | } | |
a9cb1f83 AD |
1482 | |
1483 | function getAllCounters($link) { | |
1484 | getLabelCounters($link); | |
1485 | getFeedCounters($link); | |
1486 | getTagCounters($link); | |
1487 | getGlobalCounters($link); | |
1488 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
1489 | getCategoryCounters($link); | |
1490 | } | |
1491 | } | |
1492 | ||
1493 | function getCategoryCounters($link) { | |
1494 | $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id) | |
1495 | FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id | |
1496 | AND unread = true)) AS unread FROM ttrss_feeds | |
1497 | WHERE | |
cfb02131 | 1498 | hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id"); |
a9cb1f83 AD |
1499 | |
1500 | while ($line = db_fetch_assoc($result)) { | |
1501 | $line["cat_id"] = sprintf("%d", $line["cat_id"]); | |
1502 | print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"". | |
1503 | $line["unread"]."\"/>"; | |
1504 | } | |
1505 | } | |
1506 | ||
f295c368 AD |
1507 | function getCategoryUnread($link, $cat) { |
1508 | ||
18664970 AD |
1509 | if ($cat != 0) { |
1510 | $cat_query = "cat_id = '$cat'"; | |
1511 | } else { | |
1512 | $cat_query = "cat_id IS NULL"; | |
1513 | } | |
1514 | ||
1515 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query | |
cfb02131 | 1516 | AND hidden = false |
f295c368 AD |
1517 | AND owner_uid = " . $_SESSION["uid"]); |
1518 | ||
1519 | $cat_feeds = array(); | |
1520 | while ($line = db_fetch_assoc($result)) { | |
1521 | array_push($cat_feeds, "feed_id = " . $line["id"]); | |
1522 | } | |
1523 | ||
18664970 AD |
1524 | if (count($cat_feeds) == 0) return 0; |
1525 | ||
f295c368 AD |
1526 | $match_part = implode(" OR ", $cat_feeds); |
1527 | ||
1528 | $result = db_query($link, "SELECT COUNT(int_id) AS unread | |
1529 | FROM ttrss_user_entries | |
4919fb42 | 1530 | WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]); |
f295c368 AD |
1531 | |
1532 | $unread = 0; | |
1533 | ||
1534 | # this needs to be rewritten | |
1535 | while ($line = db_fetch_assoc($result)) { | |
1536 | $unread += $line["unread"]; | |
1537 | } | |
1538 | ||
1539 | return $unread; | |
1540 | ||
1541 | } | |
1542 | ||
1543 | function getFeedUnread($link, $feed, $is_cat = false) { | |
a9cb1f83 | 1544 | $n_feed = sprintf("%d", $feed); |
f295c368 AD |
1545 | |
1546 | if ($is_cat) { | |
831ff047 | 1547 | return getCategoryUnread($link, $n_feed); |
f295c368 | 1548 | } else if ($n_feed == -1) { |
a9cb1f83 | 1549 | $match_part = "marked = true"; |
4919fb42 | 1550 | } else if ($n_feed > 0) { |
831ff047 | 1551 | |
e8b8485f AD |
1552 | $result = db_query($link, "SELECT id FROM ttrss_feeds |
1553 | WHERE parent_feed = '$n_feed' | |
318260cc | 1554 | AND hidden = false |
4919fb42 | 1555 | AND owner_uid = " . $_SESSION["uid"]); |
831ff047 AD |
1556 | |
1557 | if (db_num_rows($result) > 0) { | |
4919fb42 | 1558 | |
831ff047 AD |
1559 | $linked_feeds = array(); |
1560 | while ($line = db_fetch_assoc($result)) { | |
1561 | array_push($linked_feeds, "feed_id = " . $line["id"]); | |
1562 | } | |
e8b8485f AD |
1563 | |
1564 | array_push($linked_feeds, "feed_id = $n_feed"); | |
831ff047 AD |
1565 | |
1566 | $match_part = implode(" OR ", $linked_feeds); | |
1567 | ||
4919fb42 | 1568 | $result = db_query($link, "SELECT COUNT(int_id) AS unread |
318260cc | 1569 | FROM ttrss_user_entries |
e8b8485f AD |
1570 | WHERE unread = true AND ($match_part) |
1571 | AND owner_uid = " . $_SESSION["uid"]); | |
4919fb42 AD |
1572 | |
1573 | $unread = 0; | |
1574 | ||
1575 | # this needs to be rewritten | |
1576 | while ($line = db_fetch_assoc($result)) { | |
1577 | $unread += $line["unread"]; | |
1578 | } | |
1579 | ||
1580 | return $unread; | |
1581 | ||
831ff047 AD |
1582 | } else { |
1583 | $match_part = "feed_id = '$n_feed'"; | |
1584 | } | |
a9cb1f83 | 1585 | } else if ($feed < -10) { |
318260cc | 1586 | |
a9cb1f83 AD |
1587 | $label_id = -$feed - 11; |
1588 | ||
1589 | $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE | |
1590 | id = '$label_id' AND owner_uid = " . $_SESSION["uid"]); | |
1591 | ||
1592 | $match_part = db_fetch_result($result, 0, "sql_exp"); | |
1593 | } | |
1594 | ||
1595 | if ($match_part) { | |
1596 | ||
1597 | $result = db_query($link, "SELECT count(int_id) AS unread | |
88040f57 AD |
1598 | FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE |
1599 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
1600 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
cfb02131 | 1601 | ttrss_feeds.hidden = false AND |
88040f57 | 1602 | unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]); |
a9cb1f83 AD |
1603 | |
1604 | } else { | |
1605 | ||
1606 | $result = db_query($link, "SELECT COUNT(post_int_id) AS unread | |
1607 | FROM ttrss_tags,ttrss_user_entries | |
1608 | WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND | |
1609 | ttrss_tags.owner_uid = " . $_SESSION["uid"]); | |
1610 | } | |
1611 | ||
1612 | $unread = db_fetch_result($result, 0, "unread"); | |
cfb02131 | 1613 | |
a9cb1f83 AD |
1614 | return $unread; |
1615 | } | |
1616 | ||
1617 | /* FIXME this needs reworking */ | |
1618 | ||
f3acc32e AD |
1619 | function getGlobalUnread($link, $user_id = false) { |
1620 | ||
1621 | if (!$user_id) { | |
1622 | $user_id = $_SESSION["uid"]; | |
1623 | } | |
1624 | ||
3831db41 | 1625 | $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds |
a9cb1f83 | 1626 | WHERE unread = true AND |
3831db41 | 1627 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
a9cb1f83 | 1628 | ttrss_user_entries.ref_id = ttrss_entries.id AND |
3831db41 | 1629 | hidden = false AND |
f3acc32e | 1630 | ttrss_user_entries.owner_uid = '$user_id'"); |
a9cb1f83 AD |
1631 | $c_id = db_fetch_result($result, 0, "c_id"); |
1632 | return $c_id; | |
1633 | } | |
1634 | ||
1635 | function getGlobalCounters($link, $global_unread = -1) { | |
1636 | if ($global_unread == -1) { | |
1637 | $global_unread = getGlobalUnread($link); | |
1638 | } | |
7bf7e4d3 AD |
1639 | print "<counter type=\"global\" id='global-unread' |
1640 | counter='$global_unread'/>"; | |
1641 | ||
1642 | $result = db_query($link, "SELECT COUNT(id) AS fn FROM | |
1643 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
1644 | ||
1645 | $subscribed_feeds = db_fetch_result($result, 0, "fn"); | |
1646 | ||
1647 | print "<counter type=\"global\" id='subscribed-feeds' | |
1648 | counter='$subscribed_feeds'/>"; | |
1649 | ||
a9cb1f83 AD |
1650 | } |
1651 | ||
1652 | function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1653 | ||
1654 | if ($smart_mode) { | |
1655 | if (!$_SESSION["tctr_last_value"]) { | |
1656 | $_SESSION["tctr_last_value"] = array(); | |
1657 | } | |
1658 | } | |
1659 | ||
1660 | $old_counters = $_SESSION["tctr_last_value"]; | |
1661 | ||
1662 | $tctrs_modified = false; | |
1663 | ||
1664 | /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count | |
1665 | FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE | |
1666 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1667 | ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND | |
1668 | post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name | |
1669 | UNION | |
1670 | select tag_name,0 as count FROM ttrss_tags | |
1671 | WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */ | |
1672 | ||
1673 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) | |
1674 | FROM ttrss_user_entries WHERE int_id = post_int_id | |
1675 | AND unread = true)) AS count FROM ttrss_tags | |
1676 | WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name"); | |
1677 | ||
1678 | $tags = array(); | |
1679 | ||
1680 | while ($line = db_fetch_assoc($result)) { | |
1681 | $tags[$line["tag_name"]] += $line["count"]; | |
1682 | } | |
1683 | ||
1684 | foreach (array_keys($tags) as $tag) { | |
1685 | $unread = $tags[$tag]; | |
1686 | ||
1687 | $tag = htmlspecialchars($tag); | |
1688 | ||
1689 | if (!$smart_mode || $old_counters[$tag] != $unread) { | |
1690 | $old_counters[$tag] = $unread; | |
1691 | $tctrs_modified = true; | |
1692 | print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>"; | |
1693 | } | |
1694 | ||
1695 | } | |
1696 | ||
1697 | if ($smart_mode && $tctrs_modified) { | |
1698 | $_SESSION["tctr_last_value"] = $old_counters; | |
1699 | } | |
1700 | ||
1701 | } | |
1702 | ||
ef393de7 | 1703 | function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) { |
a9cb1f83 AD |
1704 | |
1705 | if ($smart_mode) { | |
1706 | if (!$_SESSION["lctr_last_value"]) { | |
1707 | $_SESSION["lctr_last_value"] = array(); | |
1708 | } | |
1709 | } | |
1710 | ||
ef393de7 AD |
1711 | $ret_arr = array(); |
1712 | ||
a9cb1f83 AD |
1713 | $old_counters = $_SESSION["lctr_last_value"]; |
1714 | $lctrs_modified = false; | |
1715 | ||
88040f57 | 1716 | $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds |
a9cb1f83 | 1717 | WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND |
88040f57 AD |
1718 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
1719 | unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]); | |
a9cb1f83 AD |
1720 | |
1721 | $count = db_fetch_result($result, 0, "count"); | |
1722 | ||
ef393de7 AD |
1723 | if (!$ret_mode) { |
1724 | print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>"; | |
1725 | } else { | |
1726 | $ret_arr["-1"]["counter"] = $count; | |
1727 | $ret_arr["-1"]["description"] = "Starred"; | |
1728 | } | |
a9cb1f83 AD |
1729 | |
1730 | $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM | |
1731 | ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description"); | |
1732 | ||
1733 | while ($line = db_fetch_assoc($result)) { | |
1734 | ||
1735 | $id = -$line["id"] - 11; | |
1736 | ||
ef393de7 AD |
1737 | $label_name = $line["description"]; |
1738 | ||
a9cb1f83 AD |
1739 | error_reporting (0); |
1740 | ||
88040f57 | 1741 | $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds |
a9cb1f83 | 1742 | WHERE (" . $line["sql_exp"] . ") AND unread = true AND |
cfb02131 | 1743 | ttrss_feeds.hidden = false AND |
88040f57 | 1744 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
a9cb1f83 | 1745 | ttrss_user_entries.ref_id = ttrss_entries.id AND |
88040f57 | 1746 | ttrss_user_entries.owner_uid = ".$_SESSION["uid"]); |
a9cb1f83 AD |
1747 | |
1748 | $count = db_fetch_result($tmp_result, 0, "count"); | |
1749 | ||
1750 | if (!$smart_mode || $old_counters[$id] != $count) { | |
1751 | $old_counters[$id] = $count; | |
1752 | $lctrs_modified = true; | |
ef393de7 AD |
1753 | if (!$ret_mode) { |
1754 | print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>"; | |
1755 | } else { | |
1756 | $ret_arr[$id]["counter"] = $count; | |
1757 | $ret_arr[$id]["description"] = $label_name; | |
1758 | } | |
a9cb1f83 AD |
1759 | } |
1760 | ||
1761 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1762 | } | |
1763 | ||
1764 | if ($smart_mode && $lctrs_modified) { | |
1765 | $_SESSION["lctr_last_value"] = $old_counters; | |
1766 | } | |
ef393de7 AD |
1767 | |
1768 | return $ret_arr; | |
a9cb1f83 AD |
1769 | } |
1770 | ||
1771 | /* function getFeedCounter($link, $id) { | |
1772 | ||
1773 | $result = db_query($link, "SELECT | |
1774 | count(id) as count,last_error | |
1775 | FROM ttrss_entries,ttrss_user_entries,ttrss_feeds | |
1776 | WHERE feed_id = '$id' AND unread = true | |
1777 | AND ttrss_user_entries.feed_id = ttrss_feeds.id | |
1778 | AND ttrss_user_entries.ref_id = ttrss_entries.id"); | |
1779 | ||
1780 | $count = db_fetch_result($result, 0, "count"); | |
1781 | $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error")); | |
1782 | ||
1783 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>"; | |
1784 | } */ | |
1785 | ||
1786 | function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1787 | ||
1788 | if ($smart_mode) { | |
1789 | if (!$_SESSION["fctr_last_value"]) { | |
1790 | $_SESSION["fctr_last_value"] = array(); | |
1791 | } | |
1792 | } | |
1793 | ||
1794 | $old_counters = $_SESSION["fctr_last_value"]; | |
1795 | ||
1796 | $result = db_query($link, "SELECT id,last_error,parent_feed, | |
fb1fb4ab | 1797 | SUBSTRING(last_updated,1,19) AS last_updated, |
a9cb1f83 AD |
1798 | (SELECT count(id) |
1799 | FROM ttrss_entries,ttrss_user_entries | |
1800 | WHERE feed_id = ttrss_feeds.id AND | |
1801 | ttrss_user_entries.ref_id = ttrss_entries.id | |
1802 | AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count | |
1803 | FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . " | |
1804 | AND parent_feed IS NULL"); | |
1805 | ||
1806 | $fctrs_modified = false; | |
1807 | ||
fb1fb4ab AD |
1808 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); |
1809 | ||
a9cb1f83 AD |
1810 | while ($line = db_fetch_assoc($result)) { |
1811 | ||
1812 | $id = $line["id"]; | |
1813 | $count = $line["count"]; | |
1814 | $last_error = htmlspecialchars($line["last_error"]); | |
fb1fb4ab AD |
1815 | |
1816 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
1817 | $last_updated = smart_date_time(strtotime($line["last_updated"])); | |
1818 | } else { | |
1819 | $last_updated = date($short_date, strtotime($line["last_updated"])); | |
1820 | } | |
1821 | ||
a9cb1f83 AD |
1822 | $has_img = is_file(ICONS_DIR . "/$id.ico"); |
1823 | ||
1824 | $tmp_result = db_query($link, | |
1825 | "SELECT id,COUNT(unread) AS unread | |
1826 | FROM ttrss_feeds LEFT JOIN ttrss_user_entries | |
1827 | ON (ttrss_feeds.id = ttrss_user_entries.feed_id) | |
1828 | WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id"); | |
1829 | ||
1830 | if (db_num_rows($tmp_result) > 0) { | |
1831 | while ($l = db_fetch_assoc($tmp_result)) { | |
1832 | $count += $l["unread"]; | |
1833 | } | |
1834 | } | |
1835 | ||
1836 | if (!$smart_mode || $old_counters[$id] != $count) { | |
1837 | $old_counters[$id] = $count; | |
1838 | $fctrs_modified = true; | |
1839 | ||
1840 | if ($last_error) { | |
1841 | $error_part = "error=\"$last_error\""; | |
1842 | } else { | |
1843 | $error_part = ""; | |
1844 | } | |
1845 | ||
1846 | if ($has_img) { | |
1847 | $has_img_part = "hi=\"$has_img\""; | |
1848 | } else { | |
1849 | $has_img_part = ""; | |
1850 | } | |
1851 | ||
fb1fb4ab | 1852 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>"; |
a9cb1f83 AD |
1853 | } |
1854 | } | |
1855 | ||
1856 | if ($smart_mode && $fctrs_modified) { | |
1857 | $_SESSION["fctr_last_value"] = $old_counters; | |
1858 | } | |
1859 | } | |
1860 | ||
1b758780 AD |
1861 | function get_script_dt_add() { |
1862 | if (strpos(VERSION, "99") === false) { | |
1863 | return VERSION; | |
1864 | } else { | |
1865 | return time(); | |
1866 | } | |
1867 | } | |
1868 | ||
6e7f8d26 AD |
1869 | function get_pgsql_version($link) { |
1870 | $result = db_query($link, "SELECT version() AS version"); | |
1871 | $version = split(" ", db_fetch_result($result, 0, "version")); | |
1872 | return $version[1]; | |
1873 | } | |
1874 | ||
af106b0e AD |
1875 | function print_error_xml($code, $add_msg = "") { |
1876 | global $ERRORS; | |
1877 | ||
1878 | $error_msg = $ERRORS[$code]; | |
1879 | ||
1880 | if ($add_msg) { | |
1881 | $error_msg = "$error_msg; $add_msg"; | |
1882 | } | |
1883 | ||
4c2abbc1 | 1884 | print "<rpc-reply>"; |
af106b0e | 1885 | print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>"; |
4c2abbc1 | 1886 | print "</rpc-reply>"; |
af106b0e | 1887 | } |
956c7629 AD |
1888 | |
1889 | function subscribe_to_feed($link, $feed_link, $cat_id = 0) { | |
bb0f29a4 | 1890 | |
c91c2249 AD |
1891 | $feed_link = trim(preg_replace("/^feed:/", "", $feed_link)); |
1892 | ||
1893 | if ($feed_link == "") return; | |
bb0f29a4 | 1894 | |
956c7629 AD |
1895 | if ($cat_id == "0" || !$cat_id) { |
1896 | $cat_qpart = "NULL"; | |
1897 | } else { | |
1898 | $cat_qpart = "'$cat_id'"; | |
1899 | } | |
1900 | ||
1901 | $result = db_query($link, | |
1902 | "SELECT id FROM ttrss_feeds | |
1903 | WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]); | |
1904 | ||
1905 | if (db_num_rows($result) == 0) { | |
1906 | ||
1907 | $result = db_query($link, | |
1908 | "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id) | |
1909 | VALUES ('".$_SESSION["uid"]."', '$feed_link', | |
1910 | '[Unknown]', $cat_qpart)"); | |
1911 | ||
1912 | $result = db_query($link, | |
1913 | "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link' | |
1914 | AND owner_uid = " . $_SESSION["uid"]); | |
1915 | ||
1916 | $feed_id = db_fetch_result($result, 0, "id"); | |
1917 | ||
1918 | if ($feed_id) { | |
1919 | update_rss_feed($link, $feed_link, $feed_id, true); | |
1920 | } | |
1921 | ||
1922 | return true; | |
1923 | } else { | |
1924 | return false; | |
1925 | } | |
1926 | } | |
1927 | ||
673d54ca AD |
1928 | function print_feed_select($link, $id, $default_id = "", |
1929 | $attributes = "", $include_all_feeds = true) { | |
1930 | ||
79f3553b | 1931 | print "<select id=\"$id\" name=\"$id\" $attributes>"; |
673d54ca | 1932 | if ($include_all_feeds) { |
79f3553b | 1933 | print "<option value=\"0\">All feeds</option>"; |
673d54ca AD |
1934 | } |
1935 | ||
1936 | $result = db_query($link, "SELECT id,title FROM ttrss_feeds | |
1937 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
1938 | ||
1939 | if (db_num_rows($result) > 0 && $include_all_feeds) { | |
1940 | print "<option disabled>--------</option>"; | |
1941 | } | |
1942 | ||
1943 | while ($line = db_fetch_assoc($result)) { | |
1944 | if ($line["id"] == $default_id) { | |
1945 | $is_selected = "selected"; | |
1946 | } else { | |
1947 | $is_selected = ""; | |
1948 | } | |
79f3553b | 1949 | printf("<option $is_selected value='%d'>%s</option>", |
07164479 | 1950 | $line["id"], htmlspecialchars(db_unescape_string($line["title"]))); |
673d54ca AD |
1951 | } |
1952 | ||
1953 | print "</select>"; | |
1954 | } | |
1955 | ||
1956 | function print_feed_cat_select($link, $id, $default_id = "", | |
1957 | $attributes = "", $include_all_cats = true) { | |
1958 | ||
79f3553b | 1959 | print "<select id=\"$id\" name=\"$id\" $attributes>"; |
673d54ca AD |
1960 | |
1961 | if ($include_all_cats) { | |
14f69488 | 1962 | print "<option value=\"0\">Uncategorized</option>"; |
673d54ca AD |
1963 | } |
1964 | ||
1965 | $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories | |
1966 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
1967 | ||
1968 | if (db_num_rows($result) > 0 && $include_all_cats) { | |
1969 | print "<option disabled>--------</option>"; | |
1970 | } | |
1971 | ||
1972 | while ($line = db_fetch_assoc($result)) { | |
1973 | if ($line["id"] == $default_id) { | |
1974 | $is_selected = "selected"; | |
1975 | } else { | |
1976 | $is_selected = ""; | |
1977 | } | |
14f69488 | 1978 | printf("<option $is_selected value='%d'>%s</option>", |
07164479 | 1979 | $line["id"], htmlspecialchars(db_unescape_string($line["title"]))); |
673d54ca AD |
1980 | } |
1981 | ||
1982 | print "</select>"; | |
1983 | } | |
1984 | ||
14f69488 AD |
1985 | function checkbox_to_sql_bool($val) { |
1986 | return ($val == "on") ? "true" : "false"; | |
1987 | } | |
86b682ce AD |
1988 | |
1989 | function getFeedCatTitle($link, $id) { | |
1990 | if ($id == -1) { | |
1991 | return "Special"; | |
1992 | } else if ($id < -10) { | |
1993 | return "Labels"; | |
1994 | } else if ($id > 0) { | |
1995 | $result = db_query($link, "SELECT ttrss_feed_categories.title | |
1996 | FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND | |
1997 | cat_id = ttrss_feed_categories.id"); | |
1998 | if (db_num_rows($result) == 1) { | |
1999 | return db_fetch_result($result, 0, "title"); | |
2000 | } else { | |
eb4311d0 | 2001 | return "Uncategorized"; |
86b682ce AD |
2002 | } |
2003 | } else { | |
2004 | return "getFeedCatTitle($id) failed"; | |
2005 | } | |
2006 | ||
2007 | } | |
2008 | ||
2009 | function getFeedTitle($link, $id) { | |
2010 | if ($id == -1) { | |
2011 | return "Starred articles"; | |
2012 | } else if ($id < -10) { | |
2013 | $label_id = -10 - $id; | |
2014 | $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'"); | |
2015 | if (db_num_rows($result) == 1) { | |
2016 | return db_fetch_result($result, 0, "description"); | |
2017 | } else { | |
2018 | return "Unknown label ($label_id)"; | |
2019 | } | |
2020 | ||
2021 | } else if ($id > 0) { | |
2022 | $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'"); | |
2023 | if (db_num_rows($result) == 1) { | |
2024 | return db_fetch_result($result, 0, "title"); | |
2025 | } else { | |
2026 | return "Unknown feed ($id)"; | |
2027 | } | |
2028 | } else { | |
2029 | return "getFeedTitle($id) failed"; | |
2030 | } | |
2031 | ||
2032 | } | |
3dd46f19 AD |
2033 | |
2034 | function get_session_cookie_name() { | |
2035 | return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME); | |
2036 | } | |
3ac2b520 AD |
2037 | |
2038 | function print_init_params($link) { | |
2039 | print "<init-params>"; | |
2040 | if ($_SESSION["stored-params"]) { | |
2041 | foreach (array_keys($_SESSION["stored-params"]) as $key) { | |
5f57b06d AD |
2042 | if ($key) { |
2043 | $value = htmlspecialchars($_SESSION["stored-params"][$key]); | |
2044 | print "<param key=\"$key\" value=\"$value\"/>"; | |
2045 | } | |
3ac2b520 AD |
2046 | } |
2047 | } | |
2048 | ||
2049 | print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>"; | |
2050 | print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>"; | |
0d51e25d | 2051 | print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>"; |
3ac2b520 AD |
2052 | |
2053 | print "<param key=\"on_catchup_show_next_feed\" value=\"" . | |
2054 | get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>"; | |
2055 | ||
e8bd0da9 AD |
2056 | print "<param key=\"hide_read_feeds\" value=\"" . |
2057 | sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>"; | |
2058 | ||
c9268ed5 AD |
2059 | print "<param key=\"feeds_sort_by_unread\" value=\"" . |
2060 | sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>"; | |
2061 | ||
f6d6e22f AD |
2062 | print "<param key=\"confirm_feed_catchup\" value=\"" . |
2063 | sprintf("%d", get_pref($link, "CONFIRM_FEED_CATCHUP")) . "\"/>"; | |
2064 | ||
3ac2b520 AD |
2065 | print "</init-params>"; |
2066 | } | |
f54f515f AD |
2067 | |
2068 | function print_runtime_info($link) { | |
2069 | print "<runtime-info>"; | |
71ad883b AD |
2070 | if (ENABLE_UPDATE_DAEMON) { |
2071 | print "<param key=\"daemon_is_running\" value=\"". | |
2072 | sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>"; | |
2073 | } | |
f54f515f AD |
2074 | print "</runtime-info>"; |
2075 | } | |
ef393de7 | 2076 | |
88040f57 | 2077 | function getSearchSql($search, $match_on) { |
ef393de7 | 2078 | |
88040f57 | 2079 | $search_query_part = ""; |
e20c9d88 | 2080 | |
88040f57 AD |
2081 | $keywords = split(" ", $search); |
2082 | $query_keywords = array(); | |
e20c9d88 | 2083 | |
88040f57 | 2084 | if ($match_on == "both") { |
e20c9d88 | 2085 | |
88040f57 AD |
2086 | foreach ($keywords as $k) { |
2087 | array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%') | |
2088 | OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))"); | |
2089 | } | |
e20c9d88 | 2090 | |
88040f57 | 2091 | $search_query_part = implode("AND", $query_keywords) . " AND "; |
e20c9d88 | 2092 | |
88040f57 | 2093 | } else if ($match_on == "title") { |
e20c9d88 | 2094 | |
88040f57 AD |
2095 | foreach ($keywords as $k) { |
2096 | array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))"); | |
2097 | } | |
e20c9d88 | 2098 | |
88040f57 | 2099 | $search_query_part = implode("AND", $query_keywords) . " AND "; |
e20c9d88 | 2100 | |
88040f57 AD |
2101 | } else if ($match_on == "content") { |
2102 | ||
2103 | foreach ($keywords as $k) { | |
2104 | array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))"); | |
2105 | } | |
2106 | } | |
2107 | ||
2108 | $search_query_part = implode("AND", $query_keywords); | |
2109 | ||
2110 | return $search_query_part; | |
2111 | } | |
2112 | ||
2113 | function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false) { | |
88040f57 AD |
2114 | if ($search) { |
2115 | ||
2116 | $search_query_part = getSearchSql($search, $match_on); | |
2117 | $search_query_part .= " AND "; | |
e20c9d88 | 2118 | |
ef393de7 AD |
2119 | } else { |
2120 | $search_query_part = ""; | |
2121 | } | |
2122 | ||
2123 | $view_query_part = ""; | |
2124 | ||
2125 | if ($view_mode == "adaptive") { | |
2126 | if ($search) { | |
2127 | $view_query_part = " "; | |
2128 | } else if ($feed != -1) { | |
f295c368 | 2129 | $unread = getFeedUnread($link, $feed, $cat_view); |
ef393de7 AD |
2130 | if ($unread > 0) { |
2131 | $view_query_part = " unread = true AND "; | |
2132 | } | |
2133 | } | |
2134 | } | |
2135 | ||
2136 | if ($view_mode == "marked") { | |
2137 | $view_query_part = " marked = true AND "; | |
2138 | } | |
2139 | ||
2140 | if ($view_mode == "unread") { | |
2141 | $view_query_part = " unread = true AND "; | |
2142 | } | |
2143 | ||
2144 | if ($limit > 0) { | |
2145 | $limit_query_part = "LIMIT " . $limit; | |
2146 | } | |
2147 | ||
2148 | $vfeed_query_part = ""; | |
2149 | ||
2150 | // override query strategy and enable feed display when searching globally | |
2151 | if ($search && $search_mode == "all_feeds") { | |
2152 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2153 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2154 | } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) { | |
2155 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2156 | $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE | |
2157 | id = feed_id) as feed_title,"; | |
2158 | } else if ($feed >= 0 && $search && $search_mode == "this_cat") { | |
2159 | ||
2160 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
0a6c4846 AD |
2161 | |
2162 | $tmp_result = false; | |
2163 | ||
2164 | if ($cat_view) { | |
2165 | $tmp_result = db_query($link, "SELECT id | |
2166 | FROM ttrss_feeds WHERE cat_id = '$feed'"); | |
2167 | } else { | |
2168 | $tmp_result = db_query($link, "SELECT id | |
2169 | FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds | |
2170 | WHERE id = '$feed') AND id != '$feed'"); | |
2171 | } | |
ef393de7 AD |
2172 | |
2173 | $cat_siblings = array(); | |
2174 | ||
2175 | if (db_num_rows($tmp_result) > 0) { | |
2176 | while ($p = db_fetch_assoc($tmp_result)) { | |
2177 | array_push($cat_siblings, "feed_id = " . $p["id"]); | |
2178 | } | |
2179 | ||
2180 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
2181 | $feed, implode(" OR ", $cat_siblings)); | |
2182 | ||
2183 | } else { | |
2184 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2185 | } | |
2186 | ||
2187 | } else if ($feed >= 0) { | |
2188 | ||
2189 | if ($cat_view) { | |
5c365f60 | 2190 | |
ef393de7 AD |
2191 | if ($feed > 0) { |
2192 | $query_strategy_part = "cat_id = '$feed'"; | |
2193 | } else { | |
2194 | $query_strategy_part = "cat_id IS NULL"; | |
2195 | } | |
2196 | ||
2197 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
5c365f60 | 2198 | |
ef393de7 AD |
2199 | } else { |
2200 | $tmp_result = db_query($link, "SELECT id | |
2201 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
2202 | ORDER BY cat_id,title"); | |
2203 | ||
2204 | $parent_ids = array(); | |
2205 | ||
2206 | if (db_num_rows($tmp_result) > 0) { | |
2207 | while ($p = db_fetch_assoc($tmp_result)) { | |
2208 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
2209 | } | |
2210 | ||
2211 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
2212 | $feed, implode(" OR ", $parent_ids)); | |
2213 | ||
2214 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2215 | } else { | |
2216 | $query_strategy_part = "feed_id = '$feed'"; | |
2217 | } | |
2218 | } | |
2219 | } else if ($feed == -1) { // starred virtual feed | |
2220 | $query_strategy_part = "marked = true"; | |
2221 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2222 | } else if ($feed <= -10) { // labels | |
2223 | $label_id = -$feed - 11; | |
2224 | ||
2225 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
2226 | WHERE id = '$label_id'"); | |
2227 | ||
2228 | $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp"); | |
2229 | ||
2230 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2231 | } else { | |
2232 | $query_strategy_part = "id > 0"; // dumb | |
2233 | } | |
d6e5706d AD |
2234 | |
2235 | if (get_pref($link, 'REVERSE_HEADLINES')) { | |
2236 | $order_by = "updated"; | |
2237 | } else { | |
2238 | $order_by = "updated DESC"; | |
2239 | } | |
e939722a AD |
2240 | |
2241 | if ($override_order) { | |
2242 | $order_by = $override_order; | |
2243 | } | |
ef393de7 AD |
2244 | |
2245 | $feed_title = ""; | |
2246 | ||
2247 | if ($search && $search_mode == "all_feeds") { | |
2248 | $feed_title = "Global search results ($search)"; | |
2249 | } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) { | |
e1eb2147 | 2250 | $feed_title = "Tag search results ($search, $feed)"; |
ef393de7 AD |
2251 | } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) { |
2252 | $feed_title = $feed; | |
2253 | } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) { | |
2254 | ||
2255 | if ($cat_view) { | |
5c365f60 | 2256 | |
ef393de7 AD |
2257 | if ($feed != 0) { |
2258 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories | |
2259 | WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
2260 | $feed_title = db_fetch_result($result, 0, "title"); | |
2261 | } else { | |
2262 | $feed_title = "Uncategorized"; | |
2263 | } | |
e1eb2147 AD |
2264 | |
2265 | if ($search) { | |
2266 | $feed_title = "Category search results ($search, $feed_title)"; | |
2267 | } | |
2268 | ||
ef393de7 AD |
2269 | } else { |
2270 | ||
2271 | $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds | |
2272 | WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
2273 | ||
2274 | $feed_title = db_fetch_result($result, 0, "title"); | |
2275 | $feed_site_url = db_fetch_result($result, 0, "site_url"); | |
2276 | $last_error = db_fetch_result($result, 0, "last_error"); | |
e1eb2147 AD |
2277 | |
2278 | if ($search) { | |
2279 | $feed_title = "Feed search results ($search, $feed_title)"; | |
2280 | } | |
ef393de7 AD |
2281 | } |
2282 | ||
2283 | } else if ($feed == -1) { | |
2284 | $feed_title = "Starred articles"; | |
2285 | } else if ($feed < -10) { | |
2286 | $label_id = -$feed - 11; | |
2287 | $result = db_query($link, "SELECT description FROM ttrss_labels | |
2288 | WHERE id = '$label_id'"); | |
2289 | $feed_title = db_fetch_result($result, 0, "description"); | |
88040f57 AD |
2290 | |
2291 | if ($search) { | |
2292 | $feed_title = "Label search results ($search, $feed_title)"; | |
2293 | } | |
ef393de7 AD |
2294 | } else { |
2295 | $feed_title = "?"; | |
2296 | } | |
2297 | ||
2298 | $feed_title = db_unescape_string($feed_title); | |
2299 | ||
2300 | if ($feed < -10) error_reporting (0); | |
2301 | ||
2302 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
2303 | ||
2304 | if ($feed >= 0) { | |
2305 | $feed_kind = "Feeds"; | |
2306 | } else { | |
2307 | $feed_kind = "Labels"; | |
2308 | } | |
2309 | ||
2310 | $content_query_part = "content as content_preview,"; | |
2311 | ||
2312 | $query = "SELECT | |
1f64b1be | 2313 | guid, |
ef393de7 AD |
2314 | ttrss_entries.id,ttrss_entries.title, |
2315 | SUBSTRING(updated,1,16) as updated, | |
2316 | unread,feed_id,marked,link,last_read, | |
2317 | SUBSTRING(last_read,1,19) as last_read_noms, | |
2318 | $vfeed_query_part | |
2319 | $content_query_part | |
d4b4b9de AD |
2320 | SUBSTRING(updated,1,19) as updated_noms, |
2321 | author | |
ef393de7 AD |
2322 | FROM |
2323 | ttrss_entries,ttrss_user_entries,ttrss_feeds | |
2324 | WHERE | |
cfb02131 | 2325 | ttrss_feeds.hidden = false AND |
ef393de7 AD |
2326 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
2327 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2328 | ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND | |
2329 | $search_query_part | |
2330 | $view_query_part | |
2331 | $query_strategy_part ORDER BY $order_by | |
2332 | $limit_query_part"; | |
2333 | ||
2334 | $result = db_query($link, $query); | |
2335 | ||
2336 | if ($_GET["debug"]) print $query; | |
2337 | ||
2338 | } else { | |
2339 | // browsing by tag | |
2340 | ||
2341 | $feed_kind = "Tags"; | |
2342 | ||
2343 | $result = db_query($link, "SELECT | |
1f64b1be | 2344 | guid, |
ef393de7 AD |
2345 | ttrss_entries.id as id,title, |
2346 | SUBSTRING(updated,1,16) as updated, | |
2347 | unread,feed_id, | |
2348 | marked,link,last_read, | |
2349 | SUBSTRING(last_read,1,19) as last_read_noms, | |
2350 | $vfeed_query_part | |
2351 | $content_query_part | |
2352 | SUBSTRING(updated,1,19) as updated_noms | |
2353 | FROM | |
2354 | ttrss_entries,ttrss_user_entries,ttrss_tags | |
2355 | WHERE | |
2356 | ref_id = ttrss_entries.id AND | |
2357 | ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND | |
2358 | post_int_id = int_id AND tag_name = '$feed' AND | |
2359 | $view_query_part | |
2360 | $search_query_part | |
2361 | $query_strategy_part ORDER BY $order_by | |
2362 | $limit_query_part"); | |
2363 | } | |
2364 | ||
c7188969 | 2365 | return array($result, $feed_title, $feed_site_url, $last_error); |
ef393de7 AD |
2366 | |
2367 | } | |
2368 | ||
e1eb2147 | 2369 | function generate_syndicated_feed($link, $feed, $is_cat, |
3baeeeca | 2370 | $search, $search_mode, $match_on) { |
18664970 AD |
2371 | |
2372 | $qfh_ret = queryFeedHeadlines($link, $feed, | |
e939722a | 2373 | 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC"); |
18664970 AD |
2374 | |
2375 | $result = $qfh_ret[0]; | |
59e2aab4 | 2376 | $feed_title = htmlspecialchars($qfh_ret[1]); |
18664970 AD |
2377 | $feed_site_url = $qfh_ret[2]; |
2378 | $last_error = $qfh_ret[3]; | |
2379 | ||
3baeeeca AD |
2380 | print "<rss version=\"2.0\"> |
2381 | <channel> | |
2382 | <title>$feed_title</title> | |
2383 | <link>$feed_site_url</link> | |
2384 | <generator>Tiny Tiny RSS v".VERSION."</generator>"; | |
2385 | ||
2386 | while ($line = db_fetch_assoc($result)) { | |
2387 | print "<item>"; | |
2388 | print "<id>" . htmlspecialchars($line["guid"]) . "</id>"; | |
2389 | print "<link>" . htmlspecialchars($line["link"]) . "</link>"; | |
2390 | ||
2391 | $rfc822_date = date('r', strtotime($line["updated"])); | |
2392 | ||
2393 | print "<pubDate>$rfc822_date</pubDate>"; | |
2394 | ||
2395 | print "<title>" . | |
2396 | htmlspecialchars($line["title"]) . "</title>"; | |
2397 | ||
2398 | print "<description>" . | |
2399 | htmlspecialchars($line["content_preview"]) . "</description>"; | |
2400 | ||
2401 | print "</item>"; | |
2402 | } | |
2403 | ||
2404 | print "</channel></rss>"; | |
18664970 AD |
2405 | |
2406 | } | |
2407 | ||
0a6c4846 AD |
2408 | function getCategoryTitle($link, $cat_id) { |
2409 | ||
2410 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE | |
2411 | id = '$cat_id'"); | |
2412 | ||
2413 | if (db_num_rows($result) == 1) { | |
2414 | return db_fetch_result($result, 0, "title"); | |
2415 | } else { | |
2416 | return "Uncategorized"; | |
2417 | } | |
2418 | } | |
2419 | ||
183ad07b | 2420 | function sanitize_rss($str) { |
60452879 | 2421 | $res = $str; |
183ad07b AD |
2422 | |
2423 | $res = preg_replace('/<script.*?>/i', | |
60452879 | 2424 | "<p class=\"scriptWarn\">Disabled script: ", $res); |
183ad07b | 2425 | |
60452879 AD |
2426 | $res = preg_replace('/<\/script.*?>/i', "</p>", $res); |
2427 | ||
8511f62f | 2428 | /* $res = preg_replace('/<embed.*?>/i', "", $res); |
183ad07b | 2429 | |
a262b161 AD |
2430 | $res = preg_replace('/<object.*?>.*?<\/object>/i', |
2431 | "<p class=\"objectWarn\">(Disabled html object | |
8511f62f | 2432 | - flash or other embedded content)</p>", $res); */ |
a262b161 | 2433 | |
183ad07b AD |
2434 | return $res; |
2435 | } | |
b72c3ef8 | 2436 | |
9cd7c995 AD |
2437 | function send_headlines_digests($link, $limit = 100) { |
2438 | ||
1ddba275 AD |
2439 | if (!DIGEST_ENABLE) return false; |
2440 | ||
9cd7c995 | 2441 | $user_limit = DIGEST_EMAIL_LIMIT; |
5430c959 | 2442 | $days = 1; |
9cd7c995 AD |
2443 | |
2444 | print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n"; | |
2445 | ||
2446 | if (DB_TYPE == "pgsql") { | |
2447 | $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'"; | |
2448 | } else if (DB_TYPE == "mysql") { | |
2449 | $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
2450 | } | |
2451 | ||
2452 | $result = db_query($link, "SELECT id,email FROM ttrss_users | |
2453 | WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)"); | |
2454 | ||
2455 | while ($line = db_fetch_assoc($result)) { | |
2456 | if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) { | |
2457 | print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... "; | |
2458 | ||
2459 | $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit); | |
2460 | $digest = $tuple[0]; | |
2461 | $headlines_count = $tuple[1]; | |
2462 | ||
2463 | if ($headlines_count > 0) { | |
2464 | $rc = mail($line["login"] . " <" . $line["email"] . ">", | |
2465 | "[tt-rss] New headlines for last 24 hours", $digest, | |
3ab3c1f0 AD |
2466 | "From: " . MAIL_FROM . "\n". |
2467 | "Content-Type: text/plain; charset=\"utf-8\"\n". | |
2468 | "Content-Transfer-Encoding: 8bit\n"); | |
9cd7c995 AD |
2469 | print "RC=$rc\n"; |
2470 | db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW() | |
2471 | WHERE id = " . $line["id"]); | |
2472 | } else { | |
2473 | print "No headlines\n"; | |
2474 | } | |
2475 | } | |
2476 | } | |
2477 | ||
2478 | // $digest = prepare_headlines_digest($link, $user_id, $days, $limit); | |
2479 | ||
2480 | } | |
2481 | ||
7e3634d9 AD |
2482 | function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) { |
2483 | $tmp = "New headlines for last 24 hours, as of " . date("Y/m/d H:m") . "\n"; | |
2484 | $tmp .= "=======================================================\n\n"; | |
2485 | ||
2486 | if (DB_TYPE == "pgsql") { | |
9cd7c995 | 2487 | $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'"; |
7e3634d9 AD |
2488 | } else if (DB_TYPE == "mysql") { |
2489 | $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
2490 | } | |
2491 | ||
2492 | $result = db_query($link, "SELECT ttrss_entries.title, | |
2493 | ttrss_feeds.title AS feed_title, | |
2494 | date_entered, | |
2495 | link, | |
2496 | SUBSTRING(last_updated,1,19) AS last_updated | |
2497 | FROM | |
2498 | ttrss_user_entries,ttrss_entries,ttrss_feeds | |
2499 | WHERE | |
2500 | ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id | |
3dd9183c | 2501 | AND include_in_digest = true |
7e3634d9 | 2502 | AND $interval_query |
448b0abd | 2503 | AND ttrss_user_entries.owner_uid = $user_id |
7e3634d9 AD |
2504 | AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC |
2505 | LIMIT $limit"); | |
2506 | ||
2507 | $cur_feed_title = ""; | |
2508 | ||
9cd7c995 AD |
2509 | $headlines_count = db_num_rows($result); |
2510 | ||
7e3634d9 AD |
2511 | while ($line = db_fetch_assoc($result)) { |
2512 | $updated = smart_date_time(strtotime($line["last_updated"])); | |
2513 | $feed_title = $line["feed_title"]; | |
2514 | ||
2515 | if ($cur_feed_title != $feed_title) { | |
2516 | $cur_feed_title = $feed_title; | |
2517 | ||
2518 | $tmp .= "$feed_title\n\n"; | |
2519 | } | |
2520 | ||
2521 | $tmp .= " * " . trim($line["title"]) . " - $updated\n"; | |
2522 | $tmp .= " " . trim($line["link"]) . "\n"; | |
2523 | $tmp .= "\n"; | |
2524 | } | |
2525 | ||
2526 | $tmp .= "--- \n"; | |
dfe6f833 AD |
2527 | $tmp .= "You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at " . |
2528 | DIGEST_HOSTNAME . "\n". | |
2529 | "To unsubscribe, visit your configuration options or contact instance owner.\n"; | |
7e3634d9 AD |
2530 | |
2531 | ||
9cd7c995 | 2532 | return array($tmp, $headlines_count); |
7e3634d9 AD |
2533 | } |
2534 | ||
b72c3ef8 AD |
2535 | function check_for_update($link) { |
2536 | $releases_feed = "http://tt-rss.spb.ru/releases.rss"; | |
2537 | ||
2538 | if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) { | |
2539 | return; | |
2540 | } | |
2541 | ||
2542 | error_reporting(0); | |
2543 | $rss = fetch_rss($releases_feed); | |
2544 | error_reporting (DEFAULT_ERROR_LEVEL); | |
2545 | ||
2546 | if ($rss) { | |
2547 | ||
2548 | $items = $rss->items; | |
2549 | ||
2550 | if (!$items || !is_array($items)) $items = $rss->entries; | |
2551 | if (!$items || !is_array($items)) $items = $rss; | |
2552 | ||
da412ad3 | 2553 | if (!is_array($items) || count($items) == 0) { |
b72c3ef8 | 2554 | return; |
da412ad3 | 2555 | } |
b72c3ef8 | 2556 | |
a41d2c65 | 2557 | $latest_item = $items[0]; |
b72c3ef8 | 2558 | |
a41d2c65 | 2559 | $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"])); |
b72c3ef8 | 2560 | |
48e1a342 AD |
2561 | $release_url = sanitize_rss($latest_item["link"]); |
2562 | $content = sanitize_rss($latest_item["description"]); | |
2563 | ||
a41d2c65 | 2564 | if (version_compare(VERSION, $latest_version) == -1) { |
8a797376 | 2565 | return "<div class=\"notice\"><a href=\"javascript:showBlockElement('milestoneDetails')\"> |
a41d2c65 | 2566 | New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a> |
8a797376 | 2567 | <div id=\"milestoneDetails\">$content</div></div>"; |
da412ad3 | 2568 | } |
b72c3ef8 AD |
2569 | } |
2570 | } | |
472782e8 | 2571 | |
18eddb2c AD |
2572 | function markArticlesById($link, $ids, $cmode) { |
2573 | ||
2574 | $tmp_ids = array(); | |
2575 | ||
2576 | foreach ($ids as $id) { | |
2577 | array_push($tmp_ids, "ref_id = '$id'"); | |
2578 | } | |
2579 | ||
2580 | $ids_qpart = join(" OR ", $tmp_ids); | |
2581 | ||
2582 | if ($cmode == 0) { | |
2583 | db_query($link, "UPDATE ttrss_user_entries SET | |
2584 | marked = false,last_read = NOW() | |
2585 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2586 | } else if ($cmode == 1) { | |
2587 | db_query($link, "UPDATE ttrss_user_entries SET | |
2588 | marked = true | |
2589 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2590 | } else { | |
2591 | db_query($link, "UPDATE ttrss_user_entries SET | |
2592 | marked = NOT marked,last_read = NOW() | |
2593 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2594 | } | |
2595 | } | |
2596 | ||
472782e8 AD |
2597 | function catchupArticlesById($link, $ids, $cmode) { |
2598 | ||
2599 | $tmp_ids = array(); | |
2600 | ||
2601 | foreach ($ids as $id) { | |
2602 | array_push($tmp_ids, "ref_id = '$id'"); | |
2603 | } | |
2604 | ||
2605 | $ids_qpart = join(" OR ", $tmp_ids); | |
2606 | ||
2607 | if ($cmode == 0) { | |
2608 | db_query($link, "UPDATE ttrss_user_entries SET | |
2609 | unread = false,last_read = NOW() | |
2610 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2611 | } else if ($cmode == 1) { | |
2612 | db_query($link, "UPDATE ttrss_user_entries SET | |
2613 | unread = true | |
2614 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2615 | } else { | |
2616 | db_query($link, "UPDATE ttrss_user_entries SET | |
2617 | unread = NOT unread,last_read = NOW() | |
2618 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2619 | } | |
2620 | } | |
2621 | ||
a262b161 AD |
2622 | function escape_for_form($s) { |
2623 | return htmlspecialchars(db_unescape_string($s)); | |
2624 | } | |
2625 | ||
1f64b1be AD |
2626 | function make_guid_from_title($title) { |
2627 | return preg_replace("/[ \"\',.:;]/", "-", | |
2628 | mb_strtolower(strip_tags($title))); | |
2629 | } | |
2630 | ||
11befbb2 AD |
2631 | function print_headline_subtoolbar($link, $feed_site_url, $feed_title, |
2632 | $bottom = false, $rtl_content = false, $feed_id = 0, | |
2633 | $is_cat = false, $search = false, $match_on = false, | |
2634 | $search_mode = false) { | |
2635 | ||
2636 | if (!$bottom) { | |
2637 | $class = "headlinesSubToolbar"; | |
2638 | $tid = "headlineActionsTop"; | |
2639 | } else { | |
2640 | $class = "headlinesSubToolbar"; | |
2641 | $tid = "headlineActionsBottom"; | |
2642 | } | |
2643 | ||
2644 | print "<table class=\"$class\" id=\"$tid\" | |
2645 | width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>"; | |
2646 | ||
2647 | if ($rtl_content) { | |
2648 | $rtl_cpart = "RTL"; | |
2649 | } else { | |
2650 | $rtl_cpart = ""; | |
2651 | } | |
2652 | ||
2653 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) { | |
2654 | ||
2655 | print "<td class=\"headlineActions$rtl_cpart\"> | |
2656 | Select: | |
2657 | <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)\">All</a>, | |
2658 | <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)\">Unread</a>, | |
2659 | <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)\">None</a> | |
2660 | | |
2661 | Toggle: <a href=\"javascript:selectionToggleUnread()\">Unread</a>, | |
2662 | <a href=\"javascript:selectionToggleMarked()\">Starred</a> | |
2663 | | |
2664 | Mark as read: | |
2665 | <a href=\"#\" onclick=\"catchupPage()\">Page</a>, | |
2666 | <a href=\"#\" onclick=\"catchupCurrentFeed()\">Feed</a>"; | |
2667 | print "</td>"; | |
2668 | ||
f2650917 | 2669 | if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) { |
11befbb2 AD |
2670 | print "<td class=\"headlineActions$rtl_cpart\"> |
2671 | <a href=\"javascript:labelFromSearch('$search', '$search_mode', | |
2672 | '$match_on', '$feed_id', '$is_cat');\"> | |
2673 | Convert this search to label</a></td>"; | |
2674 | } | |
2675 | ||
2676 | } else { | |
2677 | ||
2678 | print "<td class=\"headlineActions$rtl_cpart\"> | |
2679 | Select: | |
2680 | <a href=\"javascript:cdmSelectArticles('all')\">All</a>, | |
2681 | <a href=\"javascript:cdmSelectArticles('unread')\">Unread</a>, | |
2682 | <a href=\"javascript:cdmSelectArticles('none')\">None</a> | |
2683 | | |
2684 | Toggle: <a href=\"javascript:selectionToggleUnread(true)\">Unread</a>, | |
b2128f01 AD |
2685 | <a href=\"javascript:selectionToggleMarked(true)\">Starred</a> |
2686 | | |
2687 | Mark as read: | |
2688 | <a href=\"#\" onclick=\"catchupPage()\">Page</a>, | |
2689 | <a href=\"#\" onclick=\"catchupCurrentFeed()\">Feed</a>"; | |
11befbb2 AD |
2690 | |
2691 | print "</td>"; | |
2692 | ||
2693 | } | |
2694 | ||
2695 | print "<td class=\"headlineTitle$rtl_cpart\">"; | |
2696 | ||
2697 | if ($feed_site_url) { | |
2698 | if (!$bottom) { | |
2699 | $target = "target=\"_blank\""; | |
2700 | } | |
2701 | print "<a $target href=\"$feed_site_url\">$feed_title</a>"; | |
2702 | } else { | |
2703 | print $feed_title; | |
2704 | } | |
2705 | ||
2706 | if ($search) { | |
2707 | $search_q = "&q=$search&m=$match_on&smode=$search_mode"; | |
2708 | } | |
2709 | ||
2710 | if (!$bottom) { | |
2711 | print " | |
2712 | <a target=\"_new\" | |
2713 | href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\"> | |
2714 | <img class=\"noborder\" | |
2715 | alt=\"Generated feed\" src=\"images/feed-icon-12x12.png\"> | |
2716 | </a>"; | |
2717 | } | |
2718 | ||
2719 | print "</td>"; | |
2720 | print "</tr></table>"; | |
2721 | ||
2722 | } | |
2723 | ||
f407c086 AD |
2724 | function outputFeedList($link, $tags = false) { |
2725 | ||
2726 | print "<ul class=\"feedList\" id=\"feedList\">\n"; | |
2727 | ||
2728 | $owner_uid = $_SESSION["uid"]; | |
2729 | ||
2730 | if (!$tags) { | |
2731 | ||
2732 | /* virtual feeds */ | |
2733 | ||
2734 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
2735 | print "<li class=\"feedCat\">Special</li>"; | |
2736 | print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">"; | |
2737 | } | |
2738 | ||
2739 | $num_starred = getFeedUnread($link, -1); | |
2740 | ||
2741 | $class = "virt"; | |
2742 | ||
2743 | if ($num_starred > 0) $class .= "Unread"; | |
2744 | ||
2745 | printFeedEntry(-1, $class, "Starred articles", $num_starred, | |
2746 | "images/mark_set.png", $link); | |
2747 | ||
2748 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
2749 | print "</ul>\n"; | |
2750 | } | |
2751 | ||
2752 | if (GLOBAL_ENABLE_LABELS && get_pref($link, 'ENABLE_LABELS')) { | |
2753 | ||
2754 | $result = db_query($link, "SELECT id,sql_exp,description FROM | |
2755 | ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description"); | |
2756 | ||
2757 | if (db_num_rows($result) > 0) { | |
2758 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
2759 | print "<li class=\"feedCat\">Labels</li>"; | |
2760 | print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">"; | |
2761 | } else { | |
2762 | print "<li><hr></li>"; | |
2763 | } | |
2764 | } | |
2765 | ||
2766 | while ($line = db_fetch_assoc($result)) { | |
2767 | ||
2768 | error_reporting (0); | |
2769 | ||
2770 | $label_id = -$line['id'] - 11; | |
2771 | $count = getFeedUnread($link, $label_id); | |
2772 | ||
2773 | $class = "label"; | |
2774 | ||
2775 | if ($count > 0) { | |
2776 | $class .= "Unread"; | |
2777 | } | |
2778 | ||
2779 | error_reporting (DEFAULT_ERROR_LEVEL); | |
2780 | ||
2781 | printFeedEntry($label_id, | |
2782 | $class, db_unescape_string($line["description"]), | |
2783 | $count, "images/label.png", $link); | |
2784 | ||
2785 | } | |
2786 | ||
2787 | if (db_num_rows($result) > 0) { | |
2788 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
2789 | print "</ul>"; | |
2790 | } | |
2791 | } | |
2792 | ||
2793 | } | |
2794 | ||
2795 | if (!get_pref($link, 'ENABLE_FEED_CATS')) { | |
2796 | print "<li><hr></li>"; | |
2797 | } | |
2798 | ||
2799 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
2800 | if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) { | |
2801 | $order_by_qpart = "category,unread DESC,title"; | |
2802 | } else { | |
2803 | $order_by_qpart = "category,title"; | |
2804 | } | |
2805 | } else { | |
2806 | if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) { | |
2807 | $order_by_qpart = "unread DESC,title"; | |
2808 | } else { | |
2809 | $order_by_qpart = "title"; | |
2810 | } | |
2811 | } | |
2812 | ||
2813 | $result = db_query($link, "SELECT ttrss_feeds.*, | |
2814 | SUBSTRING(last_updated,1,19) AS last_updated_noms, | |
2815 | (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries | |
2816 | WHERE feed_id = ttrss_feeds.id AND unread = true | |
2817 | AND ttrss_user_entries.ref_id = ttrss_entries.id | |
2818 | AND owner_uid = '$owner_uid') as unread, | |
2819 | cat_id,last_error, | |
2820 | ttrss_feed_categories.title AS category, | |
2821 | ttrss_feed_categories.collapsed | |
2822 | FROM ttrss_feeds LEFT JOIN ttrss_feed_categories | |
2823 | ON (ttrss_feed_categories.id = cat_id) | |
2824 | WHERE | |
2825 | ttrss_feeds.hidden = false AND | |
2826 | ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL | |
2827 | ORDER BY $order_by_qpart"); | |
2828 | ||
2829 | $actid = $_GET["actid"]; | |
2830 | ||
2831 | /* real feeds */ | |
2832 | ||
2833 | $lnum = 0; | |
2834 | ||
2835 | $total_unread = 0; | |
2836 | ||
2837 | $category = ""; | |
2838 | ||
2839 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); | |
2840 | ||
2841 | while ($line = db_fetch_assoc($result)) { | |
2842 | ||
2843 | $feed = db_unescape_string($line["title"]); | |
2844 | $feed_id = $line["id"]; | |
2845 | ||
2846 | $subop = $_GET["subop"]; | |
2847 | ||
2848 | $unread = $line["unread"]; | |
2849 | ||
2850 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
2851 | $last_updated = smart_date_time(strtotime($line["last_updated_noms"])); | |
2852 | } else { | |
2853 | $last_updated = date($short_date, strtotime($line["last_updated_noms"])); | |
2854 | } | |
2855 | ||
2856 | $rtl_content = sql_bool_to_bool($line["rtl_content"]); | |
2857 | ||
2858 | if ($rtl_content) { | |
2859 | $rtl_tag = "dir=\"RTL\""; | |
2860 | } else { | |
2861 | $rtl_tag = ""; | |
2862 | } | |
2863 | ||
2864 | $tmp_result = db_query($link, | |
2865 | "SELECT id,COUNT(unread) AS unread | |
2866 | FROM ttrss_feeds LEFT JOIN ttrss_user_entries | |
2867 | ON (ttrss_feeds.id = ttrss_user_entries.feed_id) | |
2868 | WHERE parent_feed = '$feed_id' AND unread = true | |
2869 | GROUP BY ttrss_feeds.id"); | |
2870 | ||
2871 | if (db_num_rows($tmp_result) > 0) { | |
2872 | while ($l = db_fetch_assoc($tmp_result)) { | |
2873 | $unread += $l["unread"]; | |
2874 | } | |
2875 | } | |
2876 | ||
2877 | $cat_id = $line["cat_id"]; | |
2878 | ||
2879 | $tmp_category = $line["category"]; | |
2880 | ||
2881 | if (!$tmp_category) { | |
2882 | $tmp_category = "Uncategorized"; | |
2883 | } | |
2884 | ||
2885 | // $class = ($lnum % 2) ? "even" : "odd"; | |
2886 | ||
2887 | if ($line["last_error"]) { | |
2888 | $class = "error"; | |
2889 | } else { | |
2890 | $class = "feed"; | |
2891 | } | |
2892 | ||
2893 | if ($unread > 0) $class .= "Unread"; | |
2894 | ||
2895 | if ($actid == $feed_id) { | |
2896 | $class .= "Selected"; | |
2897 | } | |
2898 | ||
2899 | $total_unread += $unread; | |
2900 | ||
2901 | if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) { | |
2902 | ||
2903 | if ($category) { | |
2904 | print "</ul></li>"; | |
2905 | } | |
2906 | ||
2907 | $category = $tmp_category; | |
2908 | ||
2909 | $collapsed = $line["collapsed"]; | |
2910 | ||
2911 | // workaround for NULL category | |
2912 | if ($category == "Uncategorized") { | |
2913 | if ($_COOKIE["ttrss_vf_uclps"] == 1) { | |
2914 | $collapsed = "t"; | |
2915 | } | |
2916 | } | |
2917 | ||
2918 | if ($collapsed == "t" || $collapsed == "1") { | |
2919 | $holder_class = "invisible"; | |
2920 | $ellipsis = "..."; | |
2921 | } else { | |
2922 | $holder_class = ""; | |
2923 | $ellipsis = ""; | |
2924 | } | |
2925 | ||
2926 | $cat_id = sprintf("%d", $cat_id); | |
2927 | ||
2928 | $cat_unread = getCategoryUnread($link, $cat_id); | |
67dabe1a AD |
2929 | |
2930 | $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread"; | |
2931 | ||
f407c086 | 2932 | print "<li class=\"feedCat\" id=\"FCAT-$cat_id\"> |
67dabe1a | 2933 | <a id=\"FCATN-$cat_id\" href=\"#\"onclick=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a> |
7210613a | 2934 | <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\"> |
67dabe1a AD |
2935 | <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\" |
2936 | class=\"$catctr_class\">($cat_unread)</span> $ellipsis | |
f407c086 AD |
2937 | </a></li>"; |
2938 | ||
2939 | // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function | |
2940 | // -> keyboard navigation, etc. | |
2941 | print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\">"; | |
2942 | } | |
2943 | ||
2944 | printFeedEntry($feed_id, $class, $feed, $unread, | |
2945 | "icons/$feed_id.ico", $link, $rtl_content, | |
2946 | $last_updated, $line["last_error"]); | |
2947 | ||
2948 | ++$lnum; | |
2949 | } | |
2950 | ||
2951 | if (db_num_rows($result) == 0) { | |
2952 | print "<li>No feeds to display.</li>"; | |
2953 | } | |
2954 | ||
2955 | } else { | |
2956 | ||
2957 | // tags | |
2958 | ||
2959 | /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count | |
2960 | FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE | |
2961 | post_int_id = ttrss_user_entries.int_id AND | |
2962 | unread = true AND ref_id = ttrss_entries.id | |
2963 | AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name | |
2964 | UNION | |
2965 | select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid' | |
2966 | ORDER BY tag_name"); */ | |
2967 | ||
2968 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
2969 | print "<li class=\"feedCat\">Tags</li>"; | |
2970 | print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">"; | |
2971 | } | |
2972 | ||
2973 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) | |
2974 | FROM ttrss_user_entries WHERE int_id = post_int_id | |
2975 | AND unread = true)) AS count FROM ttrss_tags | |
2976 | WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name"); | |
2977 | ||
2978 | $tags = array(); | |
2979 | ||
2980 | while ($line = db_fetch_assoc($result)) { | |
2981 | $tags[$line["tag_name"]] += $line["count"]; | |
2982 | } | |
2983 | ||
2984 | foreach (array_keys($tags) as $tag) { | |
2985 | ||
2986 | $unread = $tags[$tag]; | |
2987 | ||
2988 | $class = "tag"; | |
2989 | ||
2990 | if ($unread > 0) { | |
2991 | $class .= "Unread"; | |
2992 | } | |
2993 | ||
2994 | printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link); | |
2995 | ||
2996 | } | |
2997 | ||
2998 | if (db_num_rows($result) == 0) { | |
2999 | print "<li>No tags to display.</li>"; | |
3000 | } | |
3001 | ||
3002 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3003 | print "</ul>\n"; | |
3004 | } | |
3005 | ||
3006 | } | |
3007 | ||
3008 | print "</ul>"; | |
3009 | ||
3010 | } | |
3011 | ||
0b126ac2 AD |
3012 | function get_article_tags($link, $id) { |
3013 | ||
3014 | $a_id = db_escape_string($id); | |
3015 | ||
3016 | $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM | |
3017 | ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE | |
3018 | ref_id = '$a_id' AND owner_uid = '".$_SESSION["uid"]."') ORDER BY tag_name"); | |
3019 | ||
3020 | $tags = array(); | |
3021 | ||
3022 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
3023 | array_push($tags, $tmp_line["tag_name"]); | |
3024 | } | |
3025 | ||
3026 | return $tags; | |
3027 | } | |
3028 | ||
40d13c28 | 3029 | ?> |