]>
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") { |
ed891a51 | 16 | require_once "magpierss/rss_fetch.inc"; |
7c5a308d AD |
17 | require_once 'magpierss/rss_utils.inc'; |
18 | } else if (RSS_BACKEND_TYPE == "simplepie") { | |
19 | require_once 'simplepie/simplepie.inc'; | |
20 | } | |
387234f3 | 21 | |
a3ee2a38 AD |
22 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); |
23 | ||
ad507f85 AD |
24 | function purge_feed($link, $feed_id, $purge_interval, $debug = false) { |
25 | ||
26 | $rows = -1; | |
4c193675 | 27 | |
fefa6ca3 | 28 | if (DB_TYPE == "pgsql") { |
44e241cb | 29 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 30 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 31 | (SELECT date_entered FROM ttrss_entries WHERE |
44e241cb AD |
32 | id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */ |
33 | ||
6e7f8d26 AD |
34 | $pg_version = get_pgsql_version($link); |
35 | ||
36 | if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) { | |
1e59ae35 AD |
37 | |
38 | $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
39 | ttrss_entries.id = ref_id AND | |
40 | marked = false AND | |
41 | feed_id = '$feed_id' AND | |
42 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); | |
43 | ||
44 | } else { | |
45 | ||
46 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
47 | USING ttrss_entries | |
48 | WHERE ttrss_entries.id = ref_id AND | |
49 | marked = false AND | |
50 | feed_id = '$feed_id' AND | |
fc774155 | 51 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); |
1e59ae35 | 52 | } |
ad507f85 AD |
53 | |
54 | $rows = pg_affected_rows($result); | |
55 | ||
fefa6ca3 | 56 | } else { |
1e59ae35 | 57 | |
30f1746f | 58 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 59 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 60 | (SELECT date_entered FROM ttrss_entries WHERE |
30f1746f AD |
61 | id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */ |
62 | ||
63 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
64 | USING ttrss_user_entries, ttrss_entries | |
65 | WHERE ttrss_entries.id = ref_id AND | |
66 | marked = false AND | |
67 | feed_id = '$feed_id' AND | |
68 | ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); | |
69 | ||
ad507f85 AD |
70 | $rows = mysql_affected_rows($link); |
71 | ||
72 | } | |
73 | ||
74 | if ($debug) { | |
75 | print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n"; | |
fefa6ca3 AD |
76 | } |
77 | } | |
78 | ||
44e241cb AD |
79 | function global_purge_old_posts($link, $do_output = false, $limit = false) { |
80 | ||
894ebcf5 | 81 | $random_qpart = sql_random_function(); |
fefa6ca3 | 82 | |
44e241cb AD |
83 | if ($limit) { |
84 | $limit_qpart = "LIMIT $limit"; | |
85 | } else { | |
86 | $limit_qpart = ""; | |
87 | } | |
88 | ||
fefa6ca3 | 89 | $result = db_query($link, |
44e241cb AD |
90 | "SELECT id,purge_interval,owner_uid FROM ttrss_feeds |
91 | ORDER BY $random_qpart $limit_qpart"); | |
fefa6ca3 AD |
92 | |
93 | while ($line = db_fetch_assoc($result)) { | |
94 | ||
95 | $feed_id = $line["id"]; | |
96 | $purge_interval = $line["purge_interval"]; | |
97 | $owner_uid = $line["owner_uid"]; | |
98 | ||
99 | if ($purge_interval == 0) { | |
100 | ||
101 | $tmp_result = db_query($link, | |
102 | "SELECT value FROM ttrss_user_prefs WHERE | |
103 | pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'"); | |
104 | ||
105 | if (db_num_rows($tmp_result) != 0) { | |
106 | $purge_interval = db_fetch_result($tmp_result, 0, "value"); | |
107 | } | |
108 | } | |
109 | ||
110 | if ($do_output) { | |
ad507f85 | 111 | // print "Feed $feed_id: purge interval = $purge_interval\n"; |
fefa6ca3 AD |
112 | } |
113 | ||
114 | if ($purge_interval > 0) { | |
ad507f85 | 115 | purge_feed($link, $feed_id, $purge_interval, $do_output); |
fefa6ca3 AD |
116 | } |
117 | } | |
118 | ||
71604ca4 AD |
119 | // purge orphaned posts in main content table |
120 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
121 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
122 | ||
fefa6ca3 AD |
123 | } |
124 | ||
b6eefba5 | 125 | function purge_old_posts($link) { |
5d73494a | 126 | |
f1a80dae AD |
127 | $user_id = $_SESSION["uid"]; |
128 | ||
129 | $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds | |
130 | WHERE owner_uid = '$user_id'"); | |
5d73494a AD |
131 | |
132 | while ($line = db_fetch_assoc($result)) { | |
133 | ||
134 | $feed_id = $line["id"]; | |
135 | $purge_interval = $line["purge_interval"]; | |
136 | ||
b619ff15 | 137 | if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS'); |
5d73494a | 138 | |
140aae81 | 139 | if ($purge_interval > 0) { |
fefa6ca3 | 140 | purge_feed($link, $feed_id, $purge_interval); |
5d73494a AD |
141 | } |
142 | } | |
71604ca4 AD |
143 | |
144 | // purge orphaned posts in main content table | |
145 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
146 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
c3a8d71a AD |
147 | } |
148 | ||
1f2b01ed | 149 | function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) { |
40d13c28 | 150 | |
4769ddaf | 151 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 152 | |
a2770077 AD |
153 | if (!$user_id) { |
154 | $user_id = $_SESSION["uid"]; | |
155 | purge_old_posts($link); | |
156 | } | |
157 | ||
25af8dad | 158 | // db_query($link, "BEGIN"); |
b82af8c3 | 159 | |
cbd8650d AD |
160 | if (MAX_UPDATE_TIME > 0) { |
161 | if (DB_TYPE == "mysql") { | |
162 | $q_order = "RAND()"; | |
163 | } else { | |
164 | $q_order = "RANDOM()"; | |
165 | } | |
166 | } else { | |
167 | $q_order = "last_updated DESC"; | |
168 | } | |
169 | ||
d148926e | 170 | $result = db_query($link, "SELECT feed_url,id, |
798f722b | 171 | SUBSTRING(last_updated,1,19) AS last_updated, |
5c563acd | 172 | update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id' |
cbd8650d AD |
173 | ORDER BY $q_order"); |
174 | ||
175 | $upd_start = time(); | |
40d13c28 | 176 | |
b6eefba5 | 177 | while ($line = db_fetch_assoc($result)) { |
d148926e AD |
178 | $upd_intl = $line["update_interval"]; |
179 | ||
b619ff15 | 180 | if (!$upd_intl || $upd_intl == 0) { |
a2770077 | 181 | $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id); |
b619ff15 | 182 | } |
d148926e | 183 | |
c1e202b7 AD |
184 | if ($upd_intl < 0) { |
185 | // Updates for this feed are disabled | |
186 | continue; | |
187 | } | |
188 | ||
93d40f50 AD |
189 | if ($fetch || (!$line["last_updated"] || |
190 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) { | |
c5142cca | 191 | |
cbd8650d AD |
192 | // print "<!-- feed: ".$line["feed_url"]." -->"; |
193 | ||
1f2b01ed | 194 | update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon); |
cbd8650d AD |
195 | |
196 | $upd_elapsed = time() - $upd_start; | |
197 | ||
198 | if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) { | |
199 | return; | |
200 | } | |
d148926e | 201 | } |
40d13c28 AD |
202 | } |
203 | ||
25af8dad | 204 | // db_query($link, "COMMIT"); |
b82af8c3 | 205 | |
40d13c28 AD |
206 | } |
207 | ||
9e997874 | 208 | function check_feed_favicon($feed_url, $feed, $link) { |
78800912 AD |
209 | $feed_url = str_replace("http://", "", $feed_url); |
210 | $feed_url = preg_replace("/\/.*$/", "", $feed_url); | |
211 | ||
212 | $icon_url = "http://$feed_url/favicon.ico"; | |
273a2f6b | 213 | $icon_file = ICONS_DIR . "/$feed.ico"; |
78800912 AD |
214 | |
215 | if (!file_exists($icon_file)) { | |
e695fdc8 | 216 | |
78800912 AD |
217 | error_reporting(0); |
218 | $r = fopen($icon_url, "r"); | |
cce28758 | 219 | error_reporting (DEFAULT_ERROR_LEVEL); |
78800912 AD |
220 | |
221 | if ($r) { | |
7d7cbaf5 | 222 | $tmpfname = tempnam(TMP_DIRECTORY, "ttrssicon"); |
78800912 AD |
223 | |
224 | $t = fopen($tmpfname, "w"); | |
225 | ||
226 | while (!feof($r)) { | |
227 | $buf = fread($r, 16384); | |
228 | fwrite($t, $buf); | |
229 | } | |
230 | ||
231 | fclose($r); | |
232 | fclose($t); | |
233 | ||
e695fdc8 AD |
234 | error_reporting(0); |
235 | if (!rename($tmpfname, $icon_file)) { | |
236 | unlink($tmpfname); | |
237 | } | |
717f5e64 AD |
238 | |
239 | chmod($icon_file, 0644); | |
240 | ||
cce28758 | 241 | error_reporting (DEFAULT_ERROR_LEVEL); |
78800912 AD |
242 | |
243 | } | |
244 | } | |
245 | } | |
246 | ||
ddb68b81 | 247 | function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) { |
40d13c28 | 248 | |
4769ddaf | 249 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 250 | |
ddb68b81 | 251 | if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) { |
21cfcdf2 AD |
252 | return; |
253 | } | |
254 | ||
47c6c988 | 255 | $result = db_query($link, "SELECT update_interval,auth_login,auth_pass |
a88c1f36 AD |
256 | FROM ttrss_feeds WHERE id = '$feed'"); |
257 | ||
ff25e639 AD |
258 | $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login")); |
259 | $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass")); | |
47c6c988 | 260 | |
a88c1f36 AD |
261 | $update_interval = db_fetch_result($result, 0, "update_interval"); |
262 | ||
263 | if ($update_interval < 0) { return; } | |
264 | ||
ab3d0b99 AD |
265 | $feed = db_escape_string($feed); |
266 | ||
47c6c988 AD |
267 | $fetch_url = $feed_url; |
268 | ||
269 | if ($auth_login && $auth_pass) { | |
270 | $url_parts = array(); | |
271 | preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts); | |
272 | ||
273 | if ($url_parts[1] && $url_parts[2]) { | |
274 | $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2]; | |
275 | } | |
276 | ||
277 | } | |
ab3d0b99 | 278 | |
7c5a308d AD |
279 | if (RSS_BACKEND_TYPE == "magpie") { |
280 | error_reporting(0); | |
281 | $rss = fetch_rss($fetch_url); | |
282 | error_reporting (DEFAULT_ERROR_LEVEL); | |
283 | } else if (RSS_BACKEND_TYPE == "simplepie") { | |
284 | ||
285 | if (!file_exists(SIMPLEPIE_CACHE_DIR)) { | |
286 | mkdir(SIMPLEPIE_CACHE_DIR); | |
287 | } | |
76798ff3 | 288 | |
7c5a308d AD |
289 | $rss = new SimplePie(); |
290 | $rss->feed_url($fetch_url); | |
291 | $rss->cache_location(SIMPLEPIE_CACHE_DIR); | |
292 | $rss->init(); | |
293 | } | |
294 | ||
b6eefba5 | 295 | $feed = db_escape_string($feed); |
dcee8f61 | 296 | |
7c5a308d | 297 | $rss_check = $rss; |
b82af8c3 | 298 | |
7c5a308d AD |
299 | if (RSS_BACKEND_TYPE == "simplepie") { |
300 | $rss_check = $rss->data; | |
301 | } | |
302 | ||
303 | if ($rss_check) { | |
304 | ||
44e241cb | 305 | // db_query($link, "BEGIN"); |
dd8c76a9 | 306 | |
a88c1f36 | 307 | $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid |
f324892e | 308 | FROM ttrss_feeds WHERE id = '$feed'"); |
331900c6 | 309 | |
b6eefba5 AD |
310 | $registered_title = db_fetch_result($result, 0, "title"); |
311 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
f324892e | 312 | $orig_site_url = db_fetch_result($result, 0, "site_url"); |
331900c6 | 313 | |
7fed1940 AD |
314 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); |
315 | ||
8d0ec6fd | 316 | if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) { |
a2770077 AD |
317 | check_feed_favicon($feed_url, $feed, $link); |
318 | } | |
319 | ||
746b249f | 320 | if (!$registered_title || $registered_title == "[Unknown]") { |
7c5a308d AD |
321 | |
322 | if (RSS_BACKEND_TYPE == "magpie") { | |
323 | $feed_title = db_escape_string($rss->channel["title"]); | |
324 | } else { | |
325 | $feed_title = $rss->get_feed_title(); | |
326 | } | |
327 | ||
f324892e AD |
328 | db_query($link, "UPDATE ttrss_feeds SET |
329 | title = '$feed_title' WHERE id = '$feed'"); | |
330 | } | |
331 | ||
7c5a308d AD |
332 | if (RSS_BACKEND_TYPE == "magpie") { |
333 | $site_url = $rss->channel["link"]; | |
334 | // weird, weird Magpie | |
335 | if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]); | |
336 | } else { | |
337 | $site_url = $rss->get_feed_link(); | |
338 | } | |
147f7691 AD |
339 | |
340 | if ($site_url && $orig_site_url != db_escape_string($site_url)) { | |
f324892e AD |
341 | db_query($link, "UPDATE ttrss_feeds SET |
342 | site_url = '$site_url' WHERE id = '$feed'"); | |
331900c6 | 343 | } |
40d13c28 | 344 | |
b7f4bda2 AD |
345 | // print "I: " . $rss->channel["image"]["url"]; |
346 | ||
7c5a308d AD |
347 | if (RSS_BACKEND_TYPE == "magpie") { |
348 | $icon_url = $rss->image["url"]; | |
349 | } else { | |
350 | $icon_url = $rss->get_image_url(); # FIXME | |
351 | } | |
b7f4bda2 | 352 | |
147f7691 | 353 | if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) { |
b6eefba5 AD |
354 | $icon_url = db_escape_string($icon_url); |
355 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
b7f4bda2 AD |
356 | } |
357 | ||
e6155a06 AD |
358 | |
359 | $filters = array(); | |
360 | ||
4b3dff6e | 361 | $result = db_query($link, "SELECT reg_exp, |
db42b934 AD |
362 | ttrss_filter_types.name AS name, |
363 | ttrss_filter_actions.name AS action | |
364 | FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE | |
365 | owner_uid = $owner_uid AND | |
366 | ttrss_filter_types.id = filter_type AND | |
367 | ttrss_filter_actions.id = action_id AND | |
ead60402 | 368 | (feed_id IS NULL OR feed_id = '$feed')"); |
e6155a06 | 369 | |
b6eefba5 | 370 | while ($line = db_fetch_assoc($result)) { |
e6155a06 | 371 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); |
19c9cb11 AD |
372 | |
373 | $filter["reg_exp"] = $line["reg_exp"]; | |
374 | $filter["action"] = $line["action"]; | |
375 | ||
376 | array_push($filters[$line["name"]], $filter); | |
e6155a06 AD |
377 | } |
378 | ||
7c5a308d AD |
379 | if (RSS_BACKEND_TYPE == "magpie") { |
380 | $iterator = $rss->items; | |
ddb68b81 | 381 | |
7c5a308d AD |
382 | if (!$iterator || !is_array($iterator)) $iterator = $rss->entries; |
383 | if (!$iterator || !is_array($iterator)) $iterator = $rss; | |
384 | ||
385 | } else { | |
386 | $iterator = $rss->get_items(); | |
387 | } | |
c22789da AD |
388 | |
389 | if (!is_array($iterator)) { | |
39541e74 | 390 | /* db_query($link, "UPDATE ttrss_feeds |
75bd0669 | 391 | SET last_error = 'Parse error: can\'t find any articles.' |
39541e74 | 392 | WHERE id = '$feed'"); */ |
c22789da AD |
393 | return; // WTF? |
394 | } | |
ddb68b81 AD |
395 | |
396 | foreach ($iterator as $item) { | |
7c5a308d AD |
397 | |
398 | if (RSS_BACKEND_TYPE == "magpie") { | |
399 | ||
400 | $entry_guid = $item["id"]; | |
401 | ||
402 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
403 | if (!$entry_guid) $entry_guid = $item["link"]; | |
40d13c28 | 404 | |
7c5a308d | 405 | if (!$entry_guid) continue; |
40d13c28 | 406 | |
7c5a308d AD |
407 | $entry_timestamp = ""; |
408 | ||
409 | $rss_2_date = $item['pubdate']; | |
410 | $rss_1_date = $item['dc']['date']; | |
411 | $atom_date = $item['issued']; | |
412 | if (!$atom_date) $atom_date = $item['updated']; | |
b82af8c3 | 413 | |
7c5a308d AD |
414 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); |
415 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
416 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
417 | ||
418 | if ($entry_timestamp == "") { | |
419 | $entry_timestamp = time(); | |
420 | $no_orig_date = 'true'; | |
421 | } else { | |
422 | $no_orig_date = 'false'; | |
423 | } | |
424 | ||
425 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
426 | ||
427 | $entry_title = $item["title"]; | |
428 | ||
429 | // strange Magpie workaround | |
430 | $entry_link = $item["link_"]; | |
431 | if (!$entry_link) $entry_link = $item["link"]; | |
432 | ||
433 | if (!$entry_title) continue; | |
434 | if (!$entry_link) continue; | |
435 | ||
436 | $entry_content = $item["content:escaped"]; | |
437 | ||
438 | if (!$entry_content) $entry_content = $item["content:encoded"]; | |
439 | if (!$entry_content) $entry_content = $item["content"]; | |
79b5d2d2 | 440 | if (!$entry_content) $entry_content = $item["atom_content"]; |
7c5a308d AD |
441 | if (!$entry_content) $entry_content = $item["summary"]; |
442 | if (!$entry_content) $entry_content = $item["description"]; | |
443 | ||
444 | // if (!$entry_content) continue; | |
445 | ||
446 | // WTF | |
447 | if (is_array($entry_content)) { | |
448 | $entry_content = $entry_content["encoded"]; | |
449 | if (!$entry_content) $entry_content = $entry_content["escaped"]; | |
450 | } | |
451 | ||
452 | // print_r($item); | |
453 | // print_r(htmlspecialchars($entry_content)); | |
454 | // print "<br>"; | |
455 | ||
456 | $entry_content_unescaped = $entry_content; | |
457 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); | |
458 | ||
459 | $entry_comments = $item["comments"]; | |
460 | ||
461 | $entry_author = db_escape_string($item['dc']['creator']); | |
462 | ||
463 | $entry_guid = db_escape_string($entry_guid); | |
464 | ||
465 | $result = db_query($link, "SELECT id FROM ttrss_entries | |
466 | WHERE guid = '$entry_guid'"); | |
467 | ||
468 | $entry_content = db_escape_string($entry_content); | |
469 | $entry_title = db_escape_string($entry_title); | |
470 | $entry_link = db_escape_string($entry_link); | |
471 | $entry_comments = db_escape_string($entry_comments); | |
472 | ||
473 | $num_comments = db_escape_string($item["slash"]["comments"]); | |
474 | ||
475 | if (!$num_comments) $num_comments = 0; | |
71ad3959 | 476 | |
7c5a308d | 477 | } else if (RSS_BACKEND_TYPE == "simplepie") { |
ddb68b81 | 478 | |
7c5a308d | 479 | $entry_guid = $item->get_id(); |
71ad3959 | 480 | |
7c5a308d AD |
481 | if (!$entry_guid) { |
482 | $entry_guid = $item->get_permalink(); | |
483 | } | |
484 | ||
485 | if (!$entry_guid) continue; | |
486 | ||
487 | $entry_timestamp = $item->get_date("U"); | |
488 | ||
489 | if ($entry_timestamp == "") { | |
490 | $entry_timestamp = time(); | |
491 | $no_orig_date = 'true'; | |
492 | } else { | |
493 | $no_orig_date = 'false'; | |
494 | } | |
495 | ||
496 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
497 | ||
498 | $entry_title = $item->get_title(); | |
499 | $entry_link = $item->get_permalink(); | |
500 | ||
501 | if (!$entry_title) continue; | |
502 | if (!$entry_link) continue; | |
71ad3959 | 503 | |
7c5a308d AD |
504 | $entry_content = $item->get_description(); |
505 | ||
506 | // print_r(htmlspecialchars($entry_content)); | |
507 | // print "<br>"; | |
508 | ||
509 | $entry_content_unescaped = $entry_content; | |
510 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); | |
511 | ||
512 | $entry_comments = ""; # FIXME | |
513 | ||
514 | $entry_author = $item->get_author(0); | |
1696229f | 515 | |
7c5a308d AD |
516 | $entry_author = db_escape_string($entry_author->name); |
517 | ||
518 | $entry_guid = db_escape_string($entry_guid); | |
a2015351 | 519 | |
7c5a308d AD |
520 | $result = db_query($link, "SELECT id FROM ttrss_entries |
521 | WHERE guid = '$entry_guid'"); | |
522 | ||
523 | $entry_content = db_escape_string($entry_content); | |
524 | $entry_title = db_escape_string($entry_title); | |
525 | $entry_link = db_escape_string($entry_link); | |
526 | $entry_comments = db_escape_string($entry_comments); | |
527 | ||
528 | $num_comments = 0; # FIXME | |
529 | ||
530 | if (!$num_comments) $num_comments = 0; | |
a2015351 | 531 | |
8add756a AD |
532 | } |
533 | ||
d48d160c | 534 | # sanitize content |
183ad07b AD |
535 | |
536 | $entry_content = sanitize_rss($entry_content); | |
537 | $entry_title = sanitize_rss($entry_title); | |
538 | $entry_link = sanitize_rss($entry_link); | |
539 | $entry_comments = sanitize_rss($entry_comments); | |
d48d160c | 540 | |
44e241cb AD |
541 | db_query($link, "BEGIN"); |
542 | ||
4c193675 AD |
543 | if (db_num_rows($result) == 0) { |
544 | ||
545 | // base post entry does not exist, create it | |
546 | ||
4c193675 AD |
547 | $result = db_query($link, |
548 | "INSERT INTO ttrss_entries | |
549 | (title, | |
550 | guid, | |
551 | link, | |
552 | updated, | |
553 | content, | |
554 | content_hash, | |
555 | no_orig_date, | |
556 | date_entered, | |
11b0dce2 | 557 | comments, |
b6104dee AD |
558 | num_comments, |
559 | author) | |
4c193675 AD |
560 | VALUES |
561 | ('$entry_title', | |
562 | '$entry_guid', | |
563 | '$entry_link', | |
564 | '$entry_timestamp_fmt', | |
565 | '$entry_content', | |
566 | '$content_hash', | |
567 | $no_orig_date, | |
568 | NOW(), | |
11b0dce2 | 569 | '$entry_comments', |
b6104dee AD |
570 | '$num_comments', |
571 | '$entry_author')"); | |
8926aab8 AD |
572 | } else { |
573 | // we keep encountering the entry in feeds, so we need to | |
574 | // update date_entered column so that we don't get horrible | |
575 | // dupes when the entry gets purged and reinserted again e.g. | |
576 | // in the case of SLOW SLOW OMG SLOW updating feeds | |
577 | ||
578 | $base_entry_id = db_fetch_result($result, 0, "id"); | |
579 | ||
580 | db_query($link, "UPDATE ttrss_entries SET date_entered = NOW() | |
581 | WHERE id = '$base_entry_id'"); | |
4c193675 AD |
582 | } |
583 | ||
584 | // now it should exist, if not - bad luck then | |
585 | ||
6385315d AD |
586 | $result = db_query($link, "SELECT |
587 | id,content_hash,no_orig_date,title, | |
8926aab8 | 588 | substring(date_entered,1,19) as date_entered, |
11b0dce2 AD |
589 | substring(updated,1,19) as updated, |
590 | num_comments | |
6385315d AD |
591 | FROM |
592 | ttrss_entries | |
593 | WHERE guid = '$entry_guid'"); | |
4c193675 AD |
594 | |
595 | if (db_num_rows($result) == 1) { | |
596 | ||
11b0dce2 AD |
597 | // this will be used below in update handler |
598 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); | |
599 | $orig_title = db_fetch_result($result, 0, "title"); | |
600 | $orig_num_comments = db_fetch_result($result, 0, "num_comments"); | |
8926aab8 AD |
601 | $orig_date_entered = strtotime(db_fetch_result($result, |
602 | 0, "date_entered")); | |
6385315d | 603 | |
11b0dce2 | 604 | $ref_id = db_fetch_result($result, 0, "id"); |
4c193675 | 605 | |
11b0dce2 | 606 | // check for user post link to main table |
4c193675 | 607 | |
11b0dce2 | 608 | // do we allow duplicate posts with same GUID in different feeds? |
8d0ec6fd | 609 | if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) { |
11b0dce2 AD |
610 | $dupcheck_qpart = "AND feed_id = '$feed'"; |
611 | } else { | |
612 | $dupcheck_qpart = ""; | |
613 | } | |
71604ca4 | 614 | |
11b0dce2 | 615 | // error_reporting(0); |
19c9cb11 | 616 | |
11b0dce2 AD |
617 | $filter_name = get_filter_name($entry_title, $entry_content, |
618 | $entry_link, $filters); | |
19c9cb11 | 619 | |
11b0dce2 AD |
620 | if ($filter_name == "filter") { |
621 | continue; | |
622 | } | |
19c9cb11 | 623 | |
11b0dce2 | 624 | // error_reporting (DEFAULT_ERROR_LEVEL); |
3a933f22 | 625 | |
11b0dce2 AD |
626 | $result = db_query($link, |
627 | "SELECT ref_id FROM ttrss_user_entries WHERE | |
628 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' | |
629 | $dupcheck_qpart"); | |
630 | ||
631 | // okay it doesn't exist - create user entry | |
632 | if (db_num_rows($result) == 0) { | |
633 | ||
634 | if ($filter_name != 'catchup') { | |
635 | $unread = 'true'; | |
636 | $last_read_qpart = 'NULL'; | |
637 | } else { | |
638 | $unread = 'false'; | |
639 | $last_read_qpart = 'NOW()'; | |
640 | } | |
dd7d3187 AD |
641 | |
642 | if ($filter_name == 'mark') { | |
643 | $marked = 'true'; | |
644 | } else { | |
645 | $marked = 'false'; | |
646 | } | |
19c9cb11 | 647 | |
11b0dce2 AD |
648 | $result = db_query($link, |
649 | "INSERT INTO ttrss_user_entries | |
dd7d3187 | 650 | (ref_id, owner_uid, feed_id, unread, last_read, marked) |
11b0dce2 | 651 | VALUES ('$ref_id', '$owner_uid', '$feed', $unread, |
dd7d3187 | 652 | $last_read_qpart, $marked)"); |
11b0dce2 AD |
653 | } |
654 | ||
6385315d AD |
655 | $post_needs_update = false; |
656 | ||
8d0ec6fd | 657 | if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) && |
6385315d AD |
658 | ($content_hash != $orig_content_hash)) { |
659 | $post_needs_update = true; | |
660 | } | |
661 | ||
662 | if ($orig_title != $entry_title) { | |
663 | $post_needs_update = true; | |
664 | } | |
665 | ||
11b0dce2 AD |
666 | if ($orig_num_comments != $num_comments) { |
667 | $post_needs_update = true; | |
668 | } | |
669 | ||
6385315d AD |
670 | // this doesn't seem to be very reliable |
671 | // | |
672 | // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) { | |
673 | // $post_needs_update = true; | |
674 | // } | |
675 | ||
676 | // if post needs update, update it and mark all user entries | |
1c73bc0c | 677 | // linking to this post as updated |
6385315d AD |
678 | if ($post_needs_update) { |
679 | ||
680 | // print "<!-- post $orig_title needs update : $post_needs_update -->"; | |
681 | ||
6385315d | 682 | db_query($link, "UPDATE ttrss_entries |
11b0dce2 AD |
683 | SET title = '$entry_title', content = '$entry_content', |
684 | num_comments = '$num_comments' | |
6385315d AD |
685 | WHERE id = '$ref_id'"); |
686 | ||
8d0ec6fd | 687 | if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) { |
4919fb42 AD |
688 | db_query($link, "UPDATE ttrss_user_entries |
689 | SET last_read = null, unread = true WHERE ref_id = '$ref_id'"); | |
690 | } else { | |
691 | db_query($link, "UPDATE ttrss_user_entries | |
692 | SET last_read = null WHERE ref_id = '$ref_id' AND unread = false"); | |
693 | } | |
6385315d AD |
694 | |
695 | } | |
4c193675 AD |
696 | } |
697 | ||
44e241cb AD |
698 | db_query($link, "COMMIT"); |
699 | ||
eb36b4eb AD |
700 | /* taaaags */ |
701 | // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, // | |
702 | ||
05732aa0 | 703 | $entry_tags = null; |
eb36b4eb | 704 | |
372ced8b | 705 | preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i", |
ee2c3050 AD |
706 | $entry_content_unescaped, $entry_tags); |
707 | ||
708 | // print "<br>$entry_title : $entry_content_unescaped<br>"; | |
709 | // print_r($entry_tags); | |
372ced8b | 710 | // print "<br>"; |
eb36b4eb AD |
711 | |
712 | $entry_tags = $entry_tags[1]; | |
713 | ||
714 | if (count($entry_tags) > 0) { | |
715 | ||
44e241cb AD |
716 | db_query($link, "BEGIN"); |
717 | ||
05732aa0 AD |
718 | $result = db_query($link, "SELECT id,int_id |
719 | FROM ttrss_entries,ttrss_user_entries | |
25da6909 | 720 | WHERE guid = '$entry_guid' |
05732aa0 | 721 | AND feed_id = '$feed' AND ref_id = id |
7fed1940 | 722 | AND owner_uid = '$owner_uid'"); |
eb36b4eb | 723 | |
fe99ab12 | 724 | if (db_num_rows($result) == 1) { |
eb36b4eb | 725 | |
fe99ab12 AD |
726 | $entry_id = db_fetch_result($result, 0, "id"); |
727 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
728 | ||
729 | foreach ($entry_tags as $tag) { | |
730 | $tag = db_escape_string(strtolower($tag)); | |
31483fc1 AD |
731 | |
732 | $tag = str_replace("+", " ", $tag); | |
fe99ab12 AD |
733 | $tag = str_replace("technorati tag: ", "", $tag); |
734 | ||
735 | $result = db_query($link, "SELECT id FROM ttrss_tags | |
736 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND | |
737 | owner_uid = '$owner_uid' LIMIT 1"); | |
738 | ||
739 | // print db_fetch_result($result, 0, "id"); | |
740 | ||
741 | if ($result && db_num_rows($result) == 0) { | |
742 | ||
743 | // print "tagging $entry_id as $tag<br>"; | |
744 | ||
745 | db_query($link, "INSERT INTO ttrss_tags | |
746 | (owner_uid,tag_name,post_int_id) | |
747 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
748 | } | |
749 | } | |
eb36b4eb | 750 | } |
44e241cb | 751 | db_query($link, "COMMIT"); |
05732aa0 | 752 | } |
4c193675 | 753 | } |
40d13c28 | 754 | |
ab3d0b99 AD |
755 | db_query($link, "UPDATE ttrss_feeds |
756 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
eb36b4eb | 757 | |
44e241cb | 758 | // db_query($link, "COMMIT"); |
dd8c76a9 | 759 | |
ab3d0b99 AD |
760 | } else { |
761 | $error_msg = db_escape_string(magpie_error()); | |
762 | db_query($link, | |
aa5f9f5f AD |
763 | "UPDATE ttrss_feeds SET last_error = '$error_msg', |
764 | last_updated = NOW() WHERE id = '$feed'"); | |
40d13c28 AD |
765 | } |
766 | ||
767 | } | |
768 | ||
f175937c | 769 | function print_select($id, $default, $values, $attributes = "") { |
79f3553b | 770 | print "<select name=\"$id\" id=\"$id\" $attributes>"; |
a0d53889 AD |
771 | foreach ($values as $v) { |
772 | if ($v == $default) | |
773 | $sel = " selected"; | |
774 | else | |
775 | $sel = ""; | |
776 | ||
777 | print "<option$sel>$v</option>"; | |
778 | } | |
779 | print "</select>"; | |
780 | } | |
40d13c28 | 781 | |
79f3553b AD |
782 | function print_select_hash($id, $default, $values, $attributes = "") { |
783 | print "<select name=\"$id\" id='$id' $attributes>"; | |
673d54ca AD |
784 | foreach (array_keys($values) as $v) { |
785 | if ($v == $default) | |
786 | $sel = "selected"; | |
787 | else | |
788 | $sel = ""; | |
789 | ||
790 | print "<option $sel value=\"$v\">".$values[$v]."</option>"; | |
791 | } | |
792 | ||
793 | print "</select>"; | |
794 | } | |
795 | ||
19c9cb11 | 796 | function get_filter_name($title, $content, $link, $filters) { |
e6155a06 AD |
797 | |
798 | if ($filters["title"]) { | |
19c9cb11 AD |
799 | foreach ($filters["title"] as $filter) { |
800 | $reg_exp = $filter["reg_exp"]; | |
801 | if (preg_match("/$reg_exp/i", $title)) { | |
802 | return $filter["action"]; | |
803 | } | |
e6155a06 AD |
804 | } |
805 | } | |
806 | ||
807 | if ($filters["content"]) { | |
19c9cb11 AD |
808 | foreach ($filters["content"] as $filter) { |
809 | $reg_exp = $filter["reg_exp"]; | |
810 | if (preg_match("/$reg_exp/i", $content)) { | |
811 | return $filter["action"]; | |
812 | } | |
e6155a06 AD |
813 | } |
814 | } | |
815 | ||
816 | if ($filters["both"]) { | |
817 | foreach ($filters["both"] as $filter) { | |
19c9cb11 AD |
818 | $reg_exp = $filter["reg_exp"]; |
819 | if (preg_match("/$reg_exp/i", $title) || | |
820 | preg_match("/$reg_exp/i", $content)) { | |
821 | return $filter["action"]; | |
822 | } | |
e6155a06 AD |
823 | } |
824 | } | |
825 | ||
3a933f22 | 826 | if ($filters["link"]) { |
19c9cb11 AD |
827 | $reg_exp = $filter["reg_exp"]; |
828 | foreach ($filters["link"] as $filter) { | |
829 | $reg_exp = $filter["reg_exp"]; | |
830 | if (preg_match("/$reg_exp/i", $link)) { | |
831 | return $filter["action"]; | |
832 | } | |
3a933f22 AD |
833 | } |
834 | } | |
835 | ||
e6155a06 AD |
836 | return false; |
837 | } | |
838 | ||
9323147e | 839 | function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link, |
fb1fb4ab | 840 | $rtl_content = false, $last_updated = false, $last_error = false) { |
254e0e4b AD |
841 | |
842 | if (file_exists($icon_file) && filesize($icon_file) > 0) { | |
023fe037 | 843 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">"; |
254e0e4b | 844 | } else { |
023fe037 | 845 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">"; |
254e0e4b AD |
846 | } |
847 | ||
9323147e AD |
848 | if ($rtl_content) { |
849 | $rtl_tag = "dir=\"rtl\""; | |
850 | } else { | |
851 | $rtl_tag = "dir=\"ltr\""; | |
852 | } | |
853 | ||
78d5212c AD |
854 | $error_notify_msg = ""; |
855 | ||
fb1fb4ab AD |
856 | if ($last_error) { |
857 | $link_title = "Error: $last_error ($last_updated)"; | |
78d5212c | 858 | $error_notify_msg = "(Error)"; |
ad780e9c | 859 | } else if ($last_updated) { |
fb1fb4ab AD |
860 | $link_title = "Updated: $last_updated"; |
861 | } | |
862 | ||
767e2486 | 863 | $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', '', false);\">$feed_title</a>"; |
254e0e4b AD |
864 | |
865 | print "<li id=\"FEEDR-$feed_id\" class=\"$class\">"; | |
b619ff15 | 866 | if (get_pref($link, 'ENABLE_FEED_ICONS')) { |
254e0e4b AD |
867 | print "$feed_icon"; |
868 | } | |
869 | ||
9323147e | 870 | print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>"; |
254e0e4b AD |
871 | |
872 | if ($unread != 0) { | |
873 | $fctr_class = ""; | |
874 | } else { | |
875 | $fctr_class = "class=\"invisible\""; | |
876 | } | |
877 | ||
9323147e | 878 | print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\"> |
254e0e4b | 879 | (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>"; |
78d5212c AD |
880 | |
881 | if (get_pref($link, "EXTENDED_FEEDLIST")) { | |
882 | print "<div class=\"feedExtInfo\"> | |
883 | <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>"; | |
884 | } | |
885 | ||
254e0e4b AD |
886 | print "</li>"; |
887 | ||
888 | } | |
889 | ||
406d9489 AD |
890 | function getmicrotime() { |
891 | list($usec, $sec) = explode(" ",microtime()); | |
892 | return ((float)$usec + (float)$sec); | |
893 | } | |
894 | ||
77e96719 AD |
895 | function print_radio($id, $default, $values, $attributes = "") { |
896 | foreach ($values as $v) { | |
897 | ||
898 | if ($v == $default) | |
5da169d9 | 899 | $sel = "checked"; |
77e96719 | 900 | else |
5da169d9 AD |
901 | $sel = ""; |
902 | ||
903 | if ($v == "Yes") { | |
904 | $sel .= " value=\"1\""; | |
905 | } else { | |
906 | $sel .= " value=\"0\""; | |
907 | } | |
77e96719 | 908 | |
69654950 AD |
909 | print "<input class=\"noborder\" |
910 | type=\"radio\" $sel $attributes name=\"$id\"> $v "; | |
77e96719 AD |
911 | |
912 | } | |
913 | } | |
914 | ||
ff485f1d AD |
915 | function initialize_user_prefs($link, $uid) { |
916 | ||
917 | $uid = db_escape_string($uid); | |
918 | ||
919 | db_query($link, "BEGIN"); | |
920 | ||
921 | $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs"); | |
922 | ||
923 | $u_result = db_query($link, "SELECT pref_name | |
924 | FROM ttrss_user_prefs WHERE owner_uid = '$uid'"); | |
925 | ||
926 | $active_prefs = array(); | |
927 | ||
928 | while ($line = db_fetch_assoc($u_result)) { | |
929 | array_push($active_prefs, $line["pref_name"]); | |
930 | } | |
931 | ||
932 | while ($line = db_fetch_assoc($result)) { | |
933 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
934 | // print "adding " . $line["pref_name"] . "<br>"; | |
935 | ||
936 | db_query($link, "INSERT INTO ttrss_user_prefs | |
937 | (owner_uid,pref_name,value) VALUES | |
938 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); | |
939 | ||
940 | } | |
941 | } | |
942 | ||
943 | db_query($link, "COMMIT"); | |
944 | ||
945 | } | |
956c7629 AD |
946 | |
947 | function lookup_user_id($link, $user) { | |
948 | ||
949 | $result = db_query($link, "SELECT id FROM ttrss_users WHERE | |
950 | login = '$login'"); | |
951 | ||
952 | if (db_num_rows($result) == 1) { | |
953 | return db_fetch_result($result, 0, "id"); | |
954 | } else { | |
955 | return false; | |
956 | } | |
957 | } | |
958 | ||
18664970 AD |
959 | function http_authenticate_user($link) { |
960 | ||
961 | if (!$_SERVER["PHP_AUTH_USER"]) { | |
962 | ||
963 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"'); | |
964 | header('HTTP/1.0 401 Unauthorized'); | |
965 | exit; | |
966 | ||
967 | } else { | |
968 | $auth_result = authenticate_user($link, | |
969 | $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]); | |
970 | ||
971 | if (!$auth_result) { | |
972 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"'); | |
973 | header('HTTP/1.0 401 Unauthorized'); | |
974 | exit; | |
975 | } | |
976 | } | |
977 | ||
978 | return true; | |
979 | } | |
980 | ||
c8437f35 AD |
981 | function authenticate_user($link, $login, $password) { |
982 | ||
131b01b3 | 983 | if (!SINGLE_USER_MODE) { |
c8437f35 | 984 | |
131b01b3 AD |
985 | $pwd_hash = 'SHA1:' . sha1($password); |
986 | ||
987 | $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE | |
988 | login = '$login' AND pwd_hash = '$pwd_hash'"); | |
989 | ||
990 | if (db_num_rows($result) == 1) { | |
991 | $_SESSION["uid"] = db_fetch_result($result, 0, "id"); | |
992 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); | |
993 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); | |
994 | ||
995 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
996 | $_SESSION["uid"]); | |
997 | ||
998 | $user_theme = get_user_theme_path($link); | |
999 | ||
1000 | $_SESSION["theme"] = $user_theme; | |
1001 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
1002 | ||
1003 | initialize_user_prefs($link, $_SESSION["uid"]); | |
1004 | ||
1005 | return true; | |
1006 | } | |
1007 | ||
1008 | return false; | |
503eb349 | 1009 | |
131b01b3 | 1010 | } else { |
503eb349 | 1011 | |
131b01b3 AD |
1012 | $_SESSION["uid"] = 1; |
1013 | $_SESSION["name"] = "admin"; | |
f557cd78 | 1014 | |
0bbba72d AD |
1015 | $user_theme = get_user_theme_path($link); |
1016 | ||
1017 | $_SESSION["theme"] = $user_theme; | |
1018 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
1019 | ||
1020 | initialize_user_prefs($link, $_SESSION["uid"]); | |
1021 | ||
c8437f35 AD |
1022 | return true; |
1023 | } | |
c8437f35 AD |
1024 | } |
1025 | ||
e6cb77a0 AD |
1026 | function make_password($length = 8) { |
1027 | ||
1028 | $password = ""; | |
798f722b AD |
1029 | $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ"; |
1030 | ||
1031 | $i = 0; | |
e6cb77a0 AD |
1032 | |
1033 | while ($i < $length) { | |
1034 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
1035 | ||
1036 | if (!strstr($password, $char)) { | |
1037 | $password .= $char; | |
1038 | $i++; | |
1039 | } | |
1040 | } | |
1041 | return $password; | |
1042 | } | |
1043 | ||
1044 | // this is called after user is created to initialize default feeds, labels | |
1045 | // or whatever else | |
1046 | ||
1047 | // user preferences are checked on every login, not here | |
1048 | ||
1049 | function initialize_user($link, $uid) { | |
1050 | ||
1051 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
1052 | values ('$uid','unread = true', 'Unread articles')"); | |
1053 | ||
1054 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
1055 | values ('$uid','last_read is null and unread = false', 'Updated articles')"); | |
1056 | ||
1057 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
74bff337 | 1058 | values ('$uid', 'Tiny Tiny RSS: New Releases', |
628fcd2c | 1059 | 'http://tt-rss.spb.ru/releases.rss')"); |
3b0feb9b AD |
1060 | |
1061 | } | |
e6cb77a0 | 1062 | |
b8aa49bc | 1063 | function logout_user() { |
5ccc1cf5 AD |
1064 | session_destroy(); |
1065 | if (isset($_COOKIE[session_name()])) { | |
1066 | setcookie(session_name(), '', time()-42000, '/'); | |
1067 | } | |
b8aa49bc AD |
1068 | } |
1069 | ||
75836f33 | 1070 | function get_script_urlpath() { |
87a79fa4 | 1071 | return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]); |
75836f33 AD |
1072 | } |
1073 | ||
1074 | function get_login_redirect() { | |
1075 | $server = $_SERVER["SERVER_NAME"]; | |
1076 | ||
1077 | if (ENABLE_LOGIN_SSL) { | |
1078 | $protocol = "https"; | |
1079 | } else { | |
1080 | $protocol = "http"; | |
1081 | } | |
1082 | ||
1083 | $url_path = get_script_urlpath(); | |
1084 | ||
1085 | $redirect_uri = "$protocol://$server$url_path/login.php"; | |
1086 | ||
1087 | return $redirect_uri; | |
1088 | } | |
1089 | ||
916f788a | 1090 | function validate_session($link) { |
a2e9b457 | 1091 | if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) { |
916f788a AD |
1092 | if ($_SESSION["ip_address"]) { |
1093 | if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) { | |
1094 | return false; | |
1095 | } | |
1096 | } | |
1097 | } | |
1098 | return true; | |
1099 | } | |
1100 | ||
7ae65adf AD |
1101 | function basic_nosid_redirect_check() { |
1102 | if (!SINGLE_USER_MODE) { | |
3dd46f19 | 1103 | if (!$_COOKIE[get_session_cookie_name()]) { |
7ae65adf AD |
1104 | $redirect_uri = get_login_redirect(); |
1105 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); | |
1106 | header("Location: $redirect_uri?rt=$return_to"); | |
1107 | exit; | |
1108 | } | |
1109 | } | |
1110 | } | |
1111 | ||
b8aa49bc AD |
1112 | function login_sequence($link) { |
1113 | if (!SINGLE_USER_MODE) { | |
75836f33 | 1114 | |
916f788a AD |
1115 | if (!validate_session($link)) { |
1116 | logout_user(); | |
1117 | $redirect_uri = get_login_redirect(); | |
1118 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); | |
1119 | header("Location: $redirect_uri?rt=$return_to"); | |
1120 | exit; | |
1121 | } | |
1122 | ||
b8aa49bc AD |
1123 | if (!USE_HTTP_AUTH) { |
1124 | if (!$_SESSION["uid"]) { | |
75836f33 | 1125 | $redirect_uri = get_login_redirect(); |
e31dca14 AD |
1126 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); |
1127 | header("Location: $redirect_uri?rt=$return_to"); | |
b8aa49bc AD |
1128 | exit; |
1129 | } | |
1130 | } else { | |
f557cd78 AD |
1131 | if (!$_SESSION["uid"]) { |
1132 | if (!$_SERVER["PHP_AUTH_USER"]) { | |
1133 | ||
1134 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
1135 | header('HTTP/1.0 401 Unauthorized'); | |
1136 | exit; | |
1137 | ||
1138 | } else { | |
1139 | $auth_result = authenticate_user($link, | |
1140 | $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]); | |
1141 | ||
1142 | if (!$auth_result) { | |
1143 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
1144 | header('HTTP/1.0 401 Unauthorized'); | |
1145 | exit; | |
1146 | } | |
1147 | } | |
1148 | } | |
b8aa49bc AD |
1149 | } |
1150 | } else { | |
0bbba72d | 1151 | return authenticate_user($link, "admin", null); |
b8aa49bc AD |
1152 | } |
1153 | } | |
3547842a AD |
1154 | |
1155 | function truncate_string($str, $max_len) { | |
12db369c AD |
1156 | if (mb_strlen($str, "utf-8") > $max_len - 3) { |
1157 | return mb_substr($str, 0, $max_len, "utf-8") . "..."; | |
3547842a AD |
1158 | } else { |
1159 | return $str; | |
1160 | } | |
1161 | } | |
54a60e1a AD |
1162 | |
1163 | function get_user_theme_path($link) { | |
798f722b AD |
1164 | $result = db_query($link, "SELECT theme_path |
1165 | FROM | |
1166 | ttrss_themes,ttrss_users | |
1167 | WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]); | |
54a60e1a AD |
1168 | if (db_num_rows($result) != 0) { |
1169 | return db_fetch_result($result, 0, "theme_path"); | |
1170 | } else { | |
1171 | return null; | |
1172 | } | |
1173 | } | |
be773442 AD |
1174 | |
1175 | function smart_date_time($timestamp) { | |
1176 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1177 | return date("G:i", $timestamp); | |
f26450f1 | 1178 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
1179 | return date("M d, G:i", $timestamp); |
1180 | } else { | |
b02111c2 | 1181 | return date("Y/m/d G:i", $timestamp); |
be773442 AD |
1182 | } |
1183 | } | |
1184 | ||
1185 | function smart_date($timestamp) { | |
1186 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1187 | return "Today"; | |
f26450f1 | 1188 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
1189 | return date("D m", $timestamp); |
1190 | } else { | |
b02111c2 | 1191 | return date("Y/m/d", $timestamp); |
be773442 AD |
1192 | } |
1193 | } | |
a654a595 AD |
1194 | |
1195 | function sql_bool_to_string($s) { | |
1196 | if ($s == "t" || $s == "1") { | |
1197 | return "true"; | |
1198 | } else { | |
1199 | return "false"; | |
1200 | } | |
1201 | } | |
e3c99f3b AD |
1202 | |
1203 | function sql_bool_to_bool($s) { | |
1204 | if ($s == "t" || $s == "1") { | |
1205 | return true; | |
1206 | } else { | |
1207 | return false; | |
1208 | } | |
1209 | } | |
0ea4fb50 | 1210 | |
e3c99f3b | 1211 | |
0ea4fb50 AD |
1212 | function toggleEvenOdd($a) { |
1213 | if ($a == "even") | |
1214 | return "odd"; | |
1215 | else | |
1216 | return "even"; | |
1217 | } | |
6043fb7e AD |
1218 | |
1219 | function sanity_check($link) { | |
9cbca41f | 1220 | |
aec3ce39 AD |
1221 | error_reporting(0); |
1222 | ||
6043fb7e AD |
1223 | $error_code = 0; |
1224 | $result = db_query($link, "SELECT schema_version FROM ttrss_version"); | |
1225 | $schema_version = db_fetch_result($result, 0, "schema_version"); | |
1226 | ||
1227 | if ($schema_version != SCHEMA_VERSION) { | |
1228 | $error_code = 5; | |
1229 | } | |
1230 | ||
aec3ce39 AD |
1231 | if (DB_TYPE == "mysql") { |
1232 | $result = db_query($link, "SELECT true", false); | |
1233 | if (db_num_rows($result) != 1) { | |
1234 | $error_code = 10; | |
1235 | } | |
1236 | } | |
1237 | ||
1238 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1239 | ||
6043fb7e | 1240 | if ($error_code != 0) { |
aec3ce39 | 1241 | print_error_xml($error_code); |
6043fb7e AD |
1242 | return false; |
1243 | } else { | |
1244 | return true; | |
9cbca41f | 1245 | } |
6043fb7e AD |
1246 | } |
1247 | ||
27981ca3 AD |
1248 | function file_is_locked($filename) { |
1249 | error_reporting(0); | |
1250 | $fp = fopen($filename, "r"); | |
1251 | error_reporting(DEFAULT_ERROR_LEVEL); | |
1252 | if ($fp) { | |
1253 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1254 | flock($fp, LOCK_UN); | |
1255 | fclose($fp); | |
1256 | return false; | |
1257 | } | |
1258 | fclose($fp); | |
1259 | return true; | |
1260 | } | |
1261 | return false; | |
1262 | } | |
1263 | ||
fcb4c0c9 AD |
1264 | function make_lockfile($filename) { |
1265 | $fp = fopen($filename, "w"); | |
1266 | ||
1267 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1268 | return $fp; | |
1269 | } else { | |
1270 | return false; | |
1271 | } | |
1272 | } | |
1273 | ||
894ebcf5 AD |
1274 | function sql_random_function() { |
1275 | if (DB_TYPE == "mysql") { | |
1276 | return "RAND()"; | |
1277 | } else { | |
1278 | return "RANDOM()"; | |
1279 | } | |
1280 | } | |
1281 | ||
23aa0d16 | 1282 | function catchup_feed($link, $feed, $cat_view) { |
88040f57 AD |
1283 | |
1284 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
23aa0d16 AD |
1285 | |
1286 | if ($cat_view) { | |
1287 | ||
1288 | if ($feed > 0) { | |
1289 | $cat_qpart = "cat_id = '$feed'"; | |
1290 | } else { | |
1291 | $cat_qpart = "cat_id IS NULL"; | |
1292 | } | |
1293 | ||
1294 | $tmp_result = db_query($link, "SELECT id | |
1295 | FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " . | |
1296 | $_SESSION["uid"]); | |
1297 | ||
1298 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1299 | ||
1300 | $tmp_feed = $tmp_line["id"]; | |
1301 | ||
1302 | db_query($link, "UPDATE ttrss_user_entries | |
1303 | SET unread = false,last_read = NOW() | |
1304 | WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]); | |
1305 | } | |
1306 | ||
1307 | } else if ($feed > 0) { | |
1308 | ||
1309 | $tmp_result = db_query($link, "SELECT id | |
1310 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
1311 | ORDER BY cat_id,title"); | |
1312 | ||
1313 | $parent_ids = array(); | |
1314 | ||
1315 | if (db_num_rows($tmp_result) > 0) { | |
1316 | while ($p = db_fetch_assoc($tmp_result)) { | |
1317 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
1318 | } | |
1319 | ||
1320 | $children_qpart = implode(" OR ", $parent_ids); | |
1321 | ||
1322 | db_query($link, "UPDATE ttrss_user_entries | |
1323 | SET unread = false,last_read = NOW() | |
1324 | WHERE (feed_id = '$feed' OR $children_qpart) | |
1325 | AND owner_uid = " . $_SESSION["uid"]); | |
1326 | ||
1327 | } else { | |
1328 | db_query($link, "UPDATE ttrss_user_entries | |
1329 | SET unread = false,last_read = NOW() | |
1330 | WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
1331 | } | |
1332 | ||
1333 | } else if ($feed < 0 && $feed > -10) { // special, like starred | |
1334 | ||
1335 | if ($feed == -1) { | |
1336 | db_query($link, "UPDATE ttrss_user_entries | |
1337 | SET unread = false,last_read = NOW() | |
1338 | WHERE marked = true AND owner_uid = ".$_SESSION["uid"]); | |
1339 | } | |
1340 | ||
1341 | } else if ($feed < -10) { // label | |
1342 | ||
1343 | // TODO make this more efficient | |
1344 | ||
1345 | $label_id = -$feed - 11; | |
1346 | ||
1347 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
1348 | WHERE id = '$label_id'"); | |
1349 | ||
1350 | if ($tmp_result) { | |
1351 | $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp"); | |
1352 | ||
1353 | db_query($link, "BEGIN"); | |
1354 | ||
1355 | $tmp2_result = db_query($link, | |
1356 | "SELECT | |
1357 | int_id | |
1358 | FROM | |
88040f57 | 1359 | ttrss_user_entries,ttrss_entries,ttrss_feeds |
23aa0d16 | 1360 | WHERE |
88040f57 AD |
1361 | ref_id = ttrss_entries.id AND |
1362 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
23aa0d16 | 1363 | $sql_exp AND |
88040f57 | 1364 | ttrss_user_entries.owner_uid = " . $_SESSION["uid"]); |
23aa0d16 AD |
1365 | |
1366 | while ($tmp_line = db_fetch_assoc($tmp2_result)) { | |
1367 | db_query($link, "UPDATE | |
1368 | ttrss_user_entries | |
1369 | SET | |
1370 | unread = false, last_read = NOW() | |
1371 | WHERE | |
1372 | int_id = " . $tmp_line["int_id"]); | |
1373 | } | |
1374 | ||
1375 | db_query($link, "COMMIT"); | |
1376 | ||
1377 | /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries | |
1378 | SET unread = false,last_read = NOW() | |
1379 | WHERE $sql_exp | |
1380 | AND ref_id = id | |
1381 | AND owner_uid = ".$_SESSION["uid"]); */ | |
1382 | } | |
1383 | } | |
1384 | } else { // tag | |
1385 | db_query($link, "BEGIN"); | |
1386 | ||
1387 | $tag_name = db_escape_string($feed); | |
1388 | ||
1389 | $result = db_query($link, "SELECT post_int_id FROM ttrss_tags | |
1390 | WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]); | |
1391 | ||
1392 | while ($line = db_fetch_assoc($result)) { | |
1393 | db_query($link, "UPDATE ttrss_user_entries SET | |
1394 | unread = false, last_read = NOW() | |
1395 | WHERE int_id = " . $line["post_int_id"]); | |
1396 | } | |
1397 | db_query($link, "COMMIT"); | |
1398 | } | |
1399 | } | |
1400 | ||
1401 | function update_generic_feed($link, $feed, $cat_view) { | |
1402 | if ($cat_view) { | |
1403 | ||
1404 | if ($feed > 0) { | |
1405 | $cat_qpart = "cat_id = '$feed'"; | |
1406 | } else { | |
1407 | $cat_qpart = "cat_id IS NULL"; | |
1408 | } | |
1409 | ||
1410 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1411 | WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]); | |
1412 | ||
1413 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1414 | $feed_url = $tmp_line["feed_url"]; | |
1415 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1416 | } | |
1417 | ||
1418 | } else { | |
1419 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1420 | WHERE id = '$feed'"); | |
1421 | $feed_url = db_fetch_result($tmp_result, 0, "feed_url"); | |
1422 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1423 | } | |
1424 | } | |
a9cb1f83 AD |
1425 | |
1426 | function getAllCounters($link) { | |
1427 | getLabelCounters($link); | |
1428 | getFeedCounters($link); | |
1429 | getTagCounters($link); | |
1430 | getGlobalCounters($link); | |
1431 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
1432 | getCategoryCounters($link); | |
1433 | } | |
1434 | } | |
1435 | ||
1436 | function getCategoryCounters($link) { | |
1437 | $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id) | |
1438 | FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id | |
1439 | AND unread = true)) AS unread FROM ttrss_feeds | |
1440 | WHERE | |
cfb02131 | 1441 | hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id"); |
a9cb1f83 AD |
1442 | |
1443 | while ($line = db_fetch_assoc($result)) { | |
1444 | $line["cat_id"] = sprintf("%d", $line["cat_id"]); | |
1445 | print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"". | |
1446 | $line["unread"]."\"/>"; | |
1447 | } | |
1448 | } | |
1449 | ||
f295c368 AD |
1450 | function getCategoryUnread($link, $cat) { |
1451 | ||
18664970 AD |
1452 | if ($cat != 0) { |
1453 | $cat_query = "cat_id = '$cat'"; | |
1454 | } else { | |
1455 | $cat_query = "cat_id IS NULL"; | |
1456 | } | |
1457 | ||
1458 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query | |
cfb02131 | 1459 | AND hidden = false |
f295c368 AD |
1460 | AND owner_uid = " . $_SESSION["uid"]); |
1461 | ||
1462 | $cat_feeds = array(); | |
1463 | while ($line = db_fetch_assoc($result)) { | |
1464 | array_push($cat_feeds, "feed_id = " . $line["id"]); | |
1465 | } | |
1466 | ||
18664970 AD |
1467 | if (count($cat_feeds) == 0) return 0; |
1468 | ||
f295c368 AD |
1469 | $match_part = implode(" OR ", $cat_feeds); |
1470 | ||
1471 | $result = db_query($link, "SELECT COUNT(int_id) AS unread | |
1472 | FROM ttrss_user_entries | |
4919fb42 | 1473 | WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]); |
f295c368 AD |
1474 | |
1475 | $unread = 0; | |
1476 | ||
1477 | # this needs to be rewritten | |
1478 | while ($line = db_fetch_assoc($result)) { | |
1479 | $unread += $line["unread"]; | |
1480 | } | |
1481 | ||
1482 | return $unread; | |
1483 | ||
1484 | } | |
1485 | ||
1486 | function getFeedUnread($link, $feed, $is_cat = false) { | |
a9cb1f83 | 1487 | $n_feed = sprintf("%d", $feed); |
f295c368 AD |
1488 | |
1489 | if ($is_cat) { | |
831ff047 | 1490 | return getCategoryUnread($link, $n_feed); |
f295c368 | 1491 | } else if ($n_feed == -1) { |
a9cb1f83 | 1492 | $match_part = "marked = true"; |
4919fb42 | 1493 | } else if ($n_feed > 0) { |
831ff047 | 1494 | |
4919fb42 | 1495 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE parent_feed = '$n_feed' |
318260cc | 1496 | AND hidden = false |
4919fb42 | 1497 | AND owner_uid = " . $_SESSION["uid"]); |
831ff047 AD |
1498 | |
1499 | if (db_num_rows($result) > 0) { | |
4919fb42 | 1500 | |
831ff047 AD |
1501 | $linked_feeds = array(); |
1502 | while ($line = db_fetch_assoc($result)) { | |
1503 | array_push($linked_feeds, "feed_id = " . $line["id"]); | |
1504 | } | |
1505 | ||
1506 | $match_part = implode(" OR ", $linked_feeds); | |
1507 | ||
4919fb42 | 1508 | $result = db_query($link, "SELECT COUNT(int_id) AS unread |
318260cc | 1509 | FROM ttrss_user_entries |
4919fb42 AD |
1510 | WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]); |
1511 | ||
1512 | $unread = 0; | |
1513 | ||
1514 | # this needs to be rewritten | |
1515 | while ($line = db_fetch_assoc($result)) { | |
1516 | $unread += $line["unread"]; | |
1517 | } | |
1518 | ||
1519 | return $unread; | |
1520 | ||
831ff047 AD |
1521 | } else { |
1522 | $match_part = "feed_id = '$n_feed'"; | |
1523 | } | |
a9cb1f83 | 1524 | } else if ($feed < -10) { |
318260cc | 1525 | |
a9cb1f83 AD |
1526 | $label_id = -$feed - 11; |
1527 | ||
1528 | $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE | |
1529 | id = '$label_id' AND owner_uid = " . $_SESSION["uid"]); | |
1530 | ||
1531 | $match_part = db_fetch_result($result, 0, "sql_exp"); | |
1532 | } | |
1533 | ||
1534 | if ($match_part) { | |
1535 | ||
1536 | $result = db_query($link, "SELECT count(int_id) AS unread | |
88040f57 AD |
1537 | FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE |
1538 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
1539 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
cfb02131 | 1540 | ttrss_feeds.hidden = false AND |
88040f57 | 1541 | unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]); |
a9cb1f83 AD |
1542 | |
1543 | } else { | |
1544 | ||
1545 | $result = db_query($link, "SELECT COUNT(post_int_id) AS unread | |
1546 | FROM ttrss_tags,ttrss_user_entries | |
1547 | WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND | |
1548 | ttrss_tags.owner_uid = " . $_SESSION["uid"]); | |
1549 | } | |
1550 | ||
1551 | $unread = db_fetch_result($result, 0, "unread"); | |
cfb02131 | 1552 | |
a9cb1f83 AD |
1553 | return $unread; |
1554 | } | |
1555 | ||
1556 | /* FIXME this needs reworking */ | |
1557 | ||
f3acc32e AD |
1558 | function getGlobalUnread($link, $user_id = false) { |
1559 | ||
1560 | if (!$user_id) { | |
1561 | $user_id = $_SESSION["uid"]; | |
1562 | } | |
1563 | ||
3831db41 | 1564 | $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds |
a9cb1f83 | 1565 | WHERE unread = true AND |
3831db41 | 1566 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
a9cb1f83 | 1567 | ttrss_user_entries.ref_id = ttrss_entries.id AND |
3831db41 | 1568 | hidden = false AND |
f3acc32e | 1569 | ttrss_user_entries.owner_uid = '$user_id'"); |
a9cb1f83 AD |
1570 | $c_id = db_fetch_result($result, 0, "c_id"); |
1571 | return $c_id; | |
1572 | } | |
1573 | ||
1574 | function getGlobalCounters($link, $global_unread = -1) { | |
1575 | if ($global_unread == -1) { | |
1576 | $global_unread = getGlobalUnread($link); | |
1577 | } | |
7bf7e4d3 AD |
1578 | print "<counter type=\"global\" id='global-unread' |
1579 | counter='$global_unread'/>"; | |
1580 | ||
1581 | $result = db_query($link, "SELECT COUNT(id) AS fn FROM | |
1582 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
1583 | ||
1584 | $subscribed_feeds = db_fetch_result($result, 0, "fn"); | |
1585 | ||
1586 | print "<counter type=\"global\" id='subscribed-feeds' | |
1587 | counter='$subscribed_feeds'/>"; | |
1588 | ||
a9cb1f83 AD |
1589 | } |
1590 | ||
1591 | function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1592 | ||
1593 | if ($smart_mode) { | |
1594 | if (!$_SESSION["tctr_last_value"]) { | |
1595 | $_SESSION["tctr_last_value"] = array(); | |
1596 | } | |
1597 | } | |
1598 | ||
1599 | $old_counters = $_SESSION["tctr_last_value"]; | |
1600 | ||
1601 | $tctrs_modified = false; | |
1602 | ||
1603 | /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count | |
1604 | FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE | |
1605 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1606 | ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND | |
1607 | post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name | |
1608 | UNION | |
1609 | select tag_name,0 as count FROM ttrss_tags | |
1610 | WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */ | |
1611 | ||
1612 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) | |
1613 | FROM ttrss_user_entries WHERE int_id = post_int_id | |
1614 | AND unread = true)) AS count FROM ttrss_tags | |
1615 | WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name"); | |
1616 | ||
1617 | $tags = array(); | |
1618 | ||
1619 | while ($line = db_fetch_assoc($result)) { | |
1620 | $tags[$line["tag_name"]] += $line["count"]; | |
1621 | } | |
1622 | ||
1623 | foreach (array_keys($tags) as $tag) { | |
1624 | $unread = $tags[$tag]; | |
1625 | ||
1626 | $tag = htmlspecialchars($tag); | |
1627 | ||
1628 | if (!$smart_mode || $old_counters[$tag] != $unread) { | |
1629 | $old_counters[$tag] = $unread; | |
1630 | $tctrs_modified = true; | |
1631 | print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>"; | |
1632 | } | |
1633 | ||
1634 | } | |
1635 | ||
1636 | if ($smart_mode && $tctrs_modified) { | |
1637 | $_SESSION["tctr_last_value"] = $old_counters; | |
1638 | } | |
1639 | ||
1640 | } | |
1641 | ||
ef393de7 | 1642 | function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) { |
a9cb1f83 AD |
1643 | |
1644 | if ($smart_mode) { | |
1645 | if (!$_SESSION["lctr_last_value"]) { | |
1646 | $_SESSION["lctr_last_value"] = array(); | |
1647 | } | |
1648 | } | |
1649 | ||
ef393de7 AD |
1650 | $ret_arr = array(); |
1651 | ||
a9cb1f83 AD |
1652 | $old_counters = $_SESSION["lctr_last_value"]; |
1653 | $lctrs_modified = false; | |
1654 | ||
88040f57 | 1655 | $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds |
a9cb1f83 | 1656 | WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND |
88040f57 AD |
1657 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
1658 | unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]); | |
a9cb1f83 AD |
1659 | |
1660 | $count = db_fetch_result($result, 0, "count"); | |
1661 | ||
ef393de7 AD |
1662 | if (!$ret_mode) { |
1663 | print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>"; | |
1664 | } else { | |
1665 | $ret_arr["-1"]["counter"] = $count; | |
1666 | $ret_arr["-1"]["description"] = "Starred"; | |
1667 | } | |
a9cb1f83 AD |
1668 | |
1669 | $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM | |
1670 | ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description"); | |
1671 | ||
1672 | while ($line = db_fetch_assoc($result)) { | |
1673 | ||
1674 | $id = -$line["id"] - 11; | |
1675 | ||
ef393de7 AD |
1676 | $label_name = $line["description"]; |
1677 | ||
a9cb1f83 AD |
1678 | error_reporting (0); |
1679 | ||
88040f57 | 1680 | $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds |
a9cb1f83 | 1681 | WHERE (" . $line["sql_exp"] . ") AND unread = true AND |
cfb02131 | 1682 | ttrss_feeds.hidden = false AND |
88040f57 | 1683 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
a9cb1f83 | 1684 | ttrss_user_entries.ref_id = ttrss_entries.id AND |
88040f57 | 1685 | ttrss_user_entries.owner_uid = ".$_SESSION["uid"]); |
a9cb1f83 AD |
1686 | |
1687 | $count = db_fetch_result($tmp_result, 0, "count"); | |
1688 | ||
1689 | if (!$smart_mode || $old_counters[$id] != $count) { | |
1690 | $old_counters[$id] = $count; | |
1691 | $lctrs_modified = true; | |
ef393de7 AD |
1692 | if (!$ret_mode) { |
1693 | print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>"; | |
1694 | } else { | |
1695 | $ret_arr[$id]["counter"] = $count; | |
1696 | $ret_arr[$id]["description"] = $label_name; | |
1697 | } | |
a9cb1f83 AD |
1698 | } |
1699 | ||
1700 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1701 | } | |
1702 | ||
1703 | if ($smart_mode && $lctrs_modified) { | |
1704 | $_SESSION["lctr_last_value"] = $old_counters; | |
1705 | } | |
ef393de7 AD |
1706 | |
1707 | return $ret_arr; | |
a9cb1f83 AD |
1708 | } |
1709 | ||
1710 | /* function getFeedCounter($link, $id) { | |
1711 | ||
1712 | $result = db_query($link, "SELECT | |
1713 | count(id) as count,last_error | |
1714 | FROM ttrss_entries,ttrss_user_entries,ttrss_feeds | |
1715 | WHERE feed_id = '$id' AND unread = true | |
1716 | AND ttrss_user_entries.feed_id = ttrss_feeds.id | |
1717 | AND ttrss_user_entries.ref_id = ttrss_entries.id"); | |
1718 | ||
1719 | $count = db_fetch_result($result, 0, "count"); | |
1720 | $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error")); | |
1721 | ||
1722 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>"; | |
1723 | } */ | |
1724 | ||
1725 | function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1726 | ||
1727 | if ($smart_mode) { | |
1728 | if (!$_SESSION["fctr_last_value"]) { | |
1729 | $_SESSION["fctr_last_value"] = array(); | |
1730 | } | |
1731 | } | |
1732 | ||
1733 | $old_counters = $_SESSION["fctr_last_value"]; | |
1734 | ||
1735 | $result = db_query($link, "SELECT id,last_error,parent_feed, | |
fb1fb4ab | 1736 | SUBSTRING(last_updated,1,19) AS last_updated, |
a9cb1f83 AD |
1737 | (SELECT count(id) |
1738 | FROM ttrss_entries,ttrss_user_entries | |
1739 | WHERE feed_id = ttrss_feeds.id AND | |
1740 | ttrss_user_entries.ref_id = ttrss_entries.id | |
1741 | AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count | |
1742 | FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . " | |
1743 | AND parent_feed IS NULL"); | |
1744 | ||
1745 | $fctrs_modified = false; | |
1746 | ||
fb1fb4ab AD |
1747 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); |
1748 | ||
a9cb1f83 AD |
1749 | while ($line = db_fetch_assoc($result)) { |
1750 | ||
1751 | $id = $line["id"]; | |
1752 | $count = $line["count"]; | |
1753 | $last_error = htmlspecialchars($line["last_error"]); | |
fb1fb4ab AD |
1754 | |
1755 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
1756 | $last_updated = smart_date_time(strtotime($line["last_updated"])); | |
1757 | } else { | |
1758 | $last_updated = date($short_date, strtotime($line["last_updated"])); | |
1759 | } | |
1760 | ||
a9cb1f83 AD |
1761 | $has_img = is_file(ICONS_DIR . "/$id.ico"); |
1762 | ||
1763 | $tmp_result = db_query($link, | |
1764 | "SELECT id,COUNT(unread) AS unread | |
1765 | FROM ttrss_feeds LEFT JOIN ttrss_user_entries | |
1766 | ON (ttrss_feeds.id = ttrss_user_entries.feed_id) | |
1767 | WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id"); | |
1768 | ||
1769 | if (db_num_rows($tmp_result) > 0) { | |
1770 | while ($l = db_fetch_assoc($tmp_result)) { | |
1771 | $count += $l["unread"]; | |
1772 | } | |
1773 | } | |
1774 | ||
1775 | if (!$smart_mode || $old_counters[$id] != $count) { | |
1776 | $old_counters[$id] = $count; | |
1777 | $fctrs_modified = true; | |
1778 | ||
1779 | if ($last_error) { | |
1780 | $error_part = "error=\"$last_error\""; | |
1781 | } else { | |
1782 | $error_part = ""; | |
1783 | } | |
1784 | ||
1785 | if ($has_img) { | |
1786 | $has_img_part = "hi=\"$has_img\""; | |
1787 | } else { | |
1788 | $has_img_part = ""; | |
1789 | } | |
1790 | ||
fb1fb4ab | 1791 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>"; |
a9cb1f83 AD |
1792 | } |
1793 | } | |
1794 | ||
1795 | if ($smart_mode && $fctrs_modified) { | |
1796 | $_SESSION["fctr_last_value"] = $old_counters; | |
1797 | } | |
1798 | } | |
1799 | ||
1b758780 AD |
1800 | function get_script_dt_add() { |
1801 | if (strpos(VERSION, "99") === false) { | |
1802 | return VERSION; | |
1803 | } else { | |
1804 | return time(); | |
1805 | } | |
1806 | } | |
1807 | ||
6e7f8d26 AD |
1808 | function get_pgsql_version($link) { |
1809 | $result = db_query($link, "SELECT version() AS version"); | |
1810 | $version = split(" ", db_fetch_result($result, 0, "version")); | |
1811 | return $version[1]; | |
1812 | } | |
1813 | ||
af106b0e AD |
1814 | function print_error_xml($code, $add_msg = "") { |
1815 | global $ERRORS; | |
1816 | ||
1817 | $error_msg = $ERRORS[$code]; | |
1818 | ||
1819 | if ($add_msg) { | |
1820 | $error_msg = "$error_msg; $add_msg"; | |
1821 | } | |
1822 | ||
4c2abbc1 | 1823 | print "<rpc-reply>"; |
af106b0e | 1824 | print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>"; |
4c2abbc1 | 1825 | print "</rpc-reply>"; |
af106b0e | 1826 | } |
956c7629 AD |
1827 | |
1828 | function subscribe_to_feed($link, $feed_link, $cat_id = 0) { | |
1829 | ||
1830 | if ($cat_id == "0" || !$cat_id) { | |
1831 | $cat_qpart = "NULL"; | |
1832 | } else { | |
1833 | $cat_qpart = "'$cat_id'"; | |
1834 | } | |
1835 | ||
1836 | $result = db_query($link, | |
1837 | "SELECT id FROM ttrss_feeds | |
1838 | WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]); | |
1839 | ||
1840 | if (db_num_rows($result) == 0) { | |
1841 | ||
1842 | $result = db_query($link, | |
1843 | "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id) | |
1844 | VALUES ('".$_SESSION["uid"]."', '$feed_link', | |
1845 | '[Unknown]', $cat_qpart)"); | |
1846 | ||
1847 | $result = db_query($link, | |
1848 | "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link' | |
1849 | AND owner_uid = " . $_SESSION["uid"]); | |
1850 | ||
1851 | $feed_id = db_fetch_result($result, 0, "id"); | |
1852 | ||
1853 | if ($feed_id) { | |
1854 | update_rss_feed($link, $feed_link, $feed_id, true); | |
1855 | } | |
1856 | ||
1857 | return true; | |
1858 | } else { | |
1859 | return false; | |
1860 | } | |
1861 | } | |
1862 | ||
673d54ca AD |
1863 | function print_feed_select($link, $id, $default_id = "", |
1864 | $attributes = "", $include_all_feeds = true) { | |
1865 | ||
79f3553b | 1866 | print "<select id=\"$id\" name=\"$id\" $attributes>"; |
673d54ca | 1867 | if ($include_all_feeds) { |
79f3553b | 1868 | print "<option value=\"0\">All feeds</option>"; |
673d54ca AD |
1869 | } |
1870 | ||
1871 | $result = db_query($link, "SELECT id,title FROM ttrss_feeds | |
1872 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
1873 | ||
1874 | if (db_num_rows($result) > 0 && $include_all_feeds) { | |
1875 | print "<option disabled>--------</option>"; | |
1876 | } | |
1877 | ||
1878 | while ($line = db_fetch_assoc($result)) { | |
1879 | if ($line["id"] == $default_id) { | |
1880 | $is_selected = "selected"; | |
1881 | } else { | |
1882 | $is_selected = ""; | |
1883 | } | |
79f3553b | 1884 | printf("<option $is_selected value='%d'>%s</option>", |
07164479 | 1885 | $line["id"], htmlspecialchars(db_unescape_string($line["title"]))); |
673d54ca AD |
1886 | } |
1887 | ||
1888 | print "</select>"; | |
1889 | } | |
1890 | ||
1891 | function print_feed_cat_select($link, $id, $default_id = "", | |
1892 | $attributes = "", $include_all_cats = true) { | |
1893 | ||
79f3553b | 1894 | print "<select id=\"$id\" name=\"$id\" $attributes>"; |
673d54ca AD |
1895 | |
1896 | if ($include_all_cats) { | |
14f69488 | 1897 | print "<option value=\"0\">Uncategorized</option>"; |
673d54ca AD |
1898 | } |
1899 | ||
1900 | $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories | |
1901 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
1902 | ||
1903 | if (db_num_rows($result) > 0 && $include_all_cats) { | |
1904 | print "<option disabled>--------</option>"; | |
1905 | } | |
1906 | ||
1907 | while ($line = db_fetch_assoc($result)) { | |
1908 | if ($line["id"] == $default_id) { | |
1909 | $is_selected = "selected"; | |
1910 | } else { | |
1911 | $is_selected = ""; | |
1912 | } | |
14f69488 | 1913 | printf("<option $is_selected value='%d'>%s</option>", |
07164479 | 1914 | $line["id"], htmlspecialchars(db_unescape_string($line["title"]))); |
673d54ca AD |
1915 | } |
1916 | ||
1917 | print "</select>"; | |
1918 | } | |
1919 | ||
14f69488 AD |
1920 | function checkbox_to_sql_bool($val) { |
1921 | return ($val == "on") ? "true" : "false"; | |
1922 | } | |
86b682ce AD |
1923 | |
1924 | function getFeedCatTitle($link, $id) { | |
1925 | if ($id == -1) { | |
1926 | return "Special"; | |
1927 | } else if ($id < -10) { | |
1928 | return "Labels"; | |
1929 | } else if ($id > 0) { | |
1930 | $result = db_query($link, "SELECT ttrss_feed_categories.title | |
1931 | FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND | |
1932 | cat_id = ttrss_feed_categories.id"); | |
1933 | if (db_num_rows($result) == 1) { | |
1934 | return db_fetch_result($result, 0, "title"); | |
1935 | } else { | |
eb4311d0 | 1936 | return "Uncategorized"; |
86b682ce AD |
1937 | } |
1938 | } else { | |
1939 | return "getFeedCatTitle($id) failed"; | |
1940 | } | |
1941 | ||
1942 | } | |
1943 | ||
1944 | function getFeedTitle($link, $id) { | |
1945 | if ($id == -1) { | |
1946 | return "Starred articles"; | |
1947 | } else if ($id < -10) { | |
1948 | $label_id = -10 - $id; | |
1949 | $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'"); | |
1950 | if (db_num_rows($result) == 1) { | |
1951 | return db_fetch_result($result, 0, "description"); | |
1952 | } else { | |
1953 | return "Unknown label ($label_id)"; | |
1954 | } | |
1955 | ||
1956 | } else if ($id > 0) { | |
1957 | $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'"); | |
1958 | if (db_num_rows($result) == 1) { | |
1959 | return db_fetch_result($result, 0, "title"); | |
1960 | } else { | |
1961 | return "Unknown feed ($id)"; | |
1962 | } | |
1963 | } else { | |
1964 | return "getFeedTitle($id) failed"; | |
1965 | } | |
1966 | ||
1967 | } | |
3dd46f19 AD |
1968 | |
1969 | function get_session_cookie_name() { | |
1970 | return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME); | |
1971 | } | |
3ac2b520 AD |
1972 | |
1973 | function print_init_params($link) { | |
1974 | print "<init-params>"; | |
1975 | if ($_SESSION["stored-params"]) { | |
1976 | foreach (array_keys($_SESSION["stored-params"]) as $key) { | |
5f57b06d AD |
1977 | if ($key) { |
1978 | $value = htmlspecialchars($_SESSION["stored-params"][$key]); | |
1979 | print "<param key=\"$key\" value=\"$value\"/>"; | |
1980 | } | |
3ac2b520 AD |
1981 | } |
1982 | } | |
1983 | ||
1984 | print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>"; | |
1985 | print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>"; | |
0d51e25d | 1986 | print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>"; |
3ac2b520 AD |
1987 | |
1988 | print "<param key=\"on_catchup_show_next_feed\" value=\"" . | |
1989 | get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>"; | |
1990 | ||
e8bd0da9 AD |
1991 | print "<param key=\"hide_read_feeds\" value=\"" . |
1992 | sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>"; | |
1993 | ||
c9268ed5 AD |
1994 | print "<param key=\"feeds_sort_by_unread\" value=\"" . |
1995 | sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>"; | |
1996 | ||
3ac2b520 AD |
1997 | print "</init-params>"; |
1998 | } | |
f54f515f AD |
1999 | |
2000 | function print_runtime_info($link) { | |
2001 | print "<runtime-info>"; | |
71ad883b AD |
2002 | if (ENABLE_UPDATE_DAEMON) { |
2003 | print "<param key=\"daemon_is_running\" value=\"". | |
2004 | sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>"; | |
2005 | } | |
f54f515f AD |
2006 | print "</runtime-info>"; |
2007 | } | |
ef393de7 | 2008 | |
88040f57 | 2009 | function getSearchSql($search, $match_on) { |
ef393de7 | 2010 | |
88040f57 | 2011 | $search_query_part = ""; |
e20c9d88 | 2012 | |
88040f57 AD |
2013 | $keywords = split(" ", $search); |
2014 | $query_keywords = array(); | |
e20c9d88 | 2015 | |
88040f57 | 2016 | if ($match_on == "both") { |
e20c9d88 | 2017 | |
88040f57 AD |
2018 | foreach ($keywords as $k) { |
2019 | array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%') | |
2020 | OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))"); | |
2021 | } | |
e20c9d88 | 2022 | |
88040f57 | 2023 | $search_query_part = implode("AND", $query_keywords) . " AND "; |
e20c9d88 | 2024 | |
88040f57 | 2025 | } else if ($match_on == "title") { |
e20c9d88 | 2026 | |
88040f57 AD |
2027 | foreach ($keywords as $k) { |
2028 | array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))"); | |
2029 | } | |
e20c9d88 | 2030 | |
88040f57 | 2031 | $search_query_part = implode("AND", $query_keywords) . " AND "; |
e20c9d88 | 2032 | |
88040f57 AD |
2033 | } else if ($match_on == "content") { |
2034 | ||
2035 | foreach ($keywords as $k) { | |
2036 | array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))"); | |
2037 | } | |
2038 | } | |
2039 | ||
2040 | $search_query_part = implode("AND", $query_keywords); | |
2041 | ||
2042 | return $search_query_part; | |
2043 | } | |
2044 | ||
2045 | function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false) { | |
88040f57 AD |
2046 | if ($search) { |
2047 | ||
2048 | $search_query_part = getSearchSql($search, $match_on); | |
2049 | $search_query_part .= " AND "; | |
e20c9d88 | 2050 | |
ef393de7 AD |
2051 | } else { |
2052 | $search_query_part = ""; | |
2053 | } | |
2054 | ||
2055 | $view_query_part = ""; | |
2056 | ||
2057 | if ($view_mode == "adaptive") { | |
2058 | if ($search) { | |
2059 | $view_query_part = " "; | |
2060 | } else if ($feed != -1) { | |
f295c368 | 2061 | $unread = getFeedUnread($link, $feed, $cat_view); |
ef393de7 AD |
2062 | if ($unread > 0) { |
2063 | $view_query_part = " unread = true AND "; | |
2064 | } | |
2065 | } | |
2066 | } | |
2067 | ||
2068 | if ($view_mode == "marked") { | |
2069 | $view_query_part = " marked = true AND "; | |
2070 | } | |
2071 | ||
2072 | if ($view_mode == "unread") { | |
2073 | $view_query_part = " unread = true AND "; | |
2074 | } | |
2075 | ||
2076 | if ($limit > 0) { | |
2077 | $limit_query_part = "LIMIT " . $limit; | |
2078 | } | |
2079 | ||
2080 | $vfeed_query_part = ""; | |
2081 | ||
2082 | // override query strategy and enable feed display when searching globally | |
2083 | if ($search && $search_mode == "all_feeds") { | |
2084 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2085 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2086 | } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) { | |
2087 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2088 | $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE | |
2089 | id = feed_id) as feed_title,"; | |
2090 | } else if ($feed >= 0 && $search && $search_mode == "this_cat") { | |
2091 | ||
2092 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
0a6c4846 AD |
2093 | |
2094 | $tmp_result = false; | |
2095 | ||
2096 | if ($cat_view) { | |
2097 | $tmp_result = db_query($link, "SELECT id | |
2098 | FROM ttrss_feeds WHERE cat_id = '$feed'"); | |
2099 | } else { | |
2100 | $tmp_result = db_query($link, "SELECT id | |
2101 | FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds | |
2102 | WHERE id = '$feed') AND id != '$feed'"); | |
2103 | } | |
ef393de7 AD |
2104 | |
2105 | $cat_siblings = array(); | |
2106 | ||
2107 | if (db_num_rows($tmp_result) > 0) { | |
2108 | while ($p = db_fetch_assoc($tmp_result)) { | |
2109 | array_push($cat_siblings, "feed_id = " . $p["id"]); | |
2110 | } | |
2111 | ||
2112 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
2113 | $feed, implode(" OR ", $cat_siblings)); | |
2114 | ||
2115 | } else { | |
2116 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2117 | } | |
2118 | ||
2119 | } else if ($feed >= 0) { | |
2120 | ||
2121 | if ($cat_view) { | |
5c365f60 | 2122 | |
ef393de7 AD |
2123 | if ($feed > 0) { |
2124 | $query_strategy_part = "cat_id = '$feed'"; | |
2125 | } else { | |
2126 | $query_strategy_part = "cat_id IS NULL"; | |
2127 | } | |
2128 | ||
2129 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
5c365f60 | 2130 | |
ef393de7 AD |
2131 | } else { |
2132 | $tmp_result = db_query($link, "SELECT id | |
2133 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
2134 | ORDER BY cat_id,title"); | |
2135 | ||
2136 | $parent_ids = array(); | |
2137 | ||
2138 | if (db_num_rows($tmp_result) > 0) { | |
2139 | while ($p = db_fetch_assoc($tmp_result)) { | |
2140 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
2141 | } | |
2142 | ||
2143 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
2144 | $feed, implode(" OR ", $parent_ids)); | |
2145 | ||
2146 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2147 | } else { | |
2148 | $query_strategy_part = "feed_id = '$feed'"; | |
2149 | } | |
2150 | } | |
2151 | } else if ($feed == -1) { // starred virtual feed | |
2152 | $query_strategy_part = "marked = true"; | |
2153 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2154 | } else if ($feed <= -10) { // labels | |
2155 | $label_id = -$feed - 11; | |
2156 | ||
2157 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
2158 | WHERE id = '$label_id'"); | |
2159 | ||
2160 | $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp"); | |
2161 | ||
2162 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2163 | } else { | |
2164 | $query_strategy_part = "id > 0"; // dumb | |
2165 | } | |
d6e5706d AD |
2166 | |
2167 | if (get_pref($link, 'REVERSE_HEADLINES')) { | |
2168 | $order_by = "updated"; | |
2169 | } else { | |
2170 | $order_by = "updated DESC"; | |
2171 | } | |
e939722a AD |
2172 | |
2173 | if ($override_order) { | |
2174 | $order_by = $override_order; | |
2175 | } | |
ef393de7 AD |
2176 | |
2177 | $feed_title = ""; | |
2178 | ||
2179 | if ($search && $search_mode == "all_feeds") { | |
2180 | $feed_title = "Global search results ($search)"; | |
2181 | } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) { | |
e1eb2147 | 2182 | $feed_title = "Tag search results ($search, $feed)"; |
ef393de7 AD |
2183 | } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) { |
2184 | $feed_title = $feed; | |
2185 | } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) { | |
2186 | ||
2187 | if ($cat_view) { | |
5c365f60 | 2188 | |
ef393de7 AD |
2189 | if ($feed != 0) { |
2190 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories | |
2191 | WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
2192 | $feed_title = db_fetch_result($result, 0, "title"); | |
2193 | } else { | |
2194 | $feed_title = "Uncategorized"; | |
2195 | } | |
e1eb2147 AD |
2196 | |
2197 | if ($search) { | |
2198 | $feed_title = "Category search results ($search, $feed_title)"; | |
2199 | } | |
2200 | ||
ef393de7 AD |
2201 | } else { |
2202 | ||
2203 | $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds | |
2204 | WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
2205 | ||
2206 | $feed_title = db_fetch_result($result, 0, "title"); | |
2207 | $feed_site_url = db_fetch_result($result, 0, "site_url"); | |
2208 | $last_error = db_fetch_result($result, 0, "last_error"); | |
e1eb2147 AD |
2209 | |
2210 | if ($search) { | |
2211 | $feed_title = "Feed search results ($search, $feed_title)"; | |
2212 | } | |
ef393de7 AD |
2213 | } |
2214 | ||
2215 | } else if ($feed == -1) { | |
2216 | $feed_title = "Starred articles"; | |
2217 | } else if ($feed < -10) { | |
2218 | $label_id = -$feed - 11; | |
2219 | $result = db_query($link, "SELECT description FROM ttrss_labels | |
2220 | WHERE id = '$label_id'"); | |
2221 | $feed_title = db_fetch_result($result, 0, "description"); | |
88040f57 AD |
2222 | |
2223 | if ($search) { | |
2224 | $feed_title = "Label search results ($search, $feed_title)"; | |
2225 | } | |
ef393de7 AD |
2226 | } else { |
2227 | $feed_title = "?"; | |
2228 | } | |
2229 | ||
2230 | $feed_title = db_unescape_string($feed_title); | |
2231 | ||
2232 | if ($feed < -10) error_reporting (0); | |
2233 | ||
2234 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
2235 | ||
2236 | if ($feed >= 0) { | |
2237 | $feed_kind = "Feeds"; | |
2238 | } else { | |
2239 | $feed_kind = "Labels"; | |
2240 | } | |
2241 | ||
2242 | $content_query_part = "content as content_preview,"; | |
2243 | ||
2244 | $query = "SELECT | |
2245 | ttrss_entries.id,ttrss_entries.title, | |
2246 | SUBSTRING(updated,1,16) as updated, | |
2247 | unread,feed_id,marked,link,last_read, | |
2248 | SUBSTRING(last_read,1,19) as last_read_noms, | |
2249 | $vfeed_query_part | |
2250 | $content_query_part | |
2251 | SUBSTRING(updated,1,19) as updated_noms | |
2252 | FROM | |
2253 | ttrss_entries,ttrss_user_entries,ttrss_feeds | |
2254 | WHERE | |
cfb02131 | 2255 | ttrss_feeds.hidden = false AND |
ef393de7 AD |
2256 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
2257 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2258 | ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND | |
2259 | $search_query_part | |
2260 | $view_query_part | |
2261 | $query_strategy_part ORDER BY $order_by | |
2262 | $limit_query_part"; | |
2263 | ||
2264 | $result = db_query($link, $query); | |
2265 | ||
2266 | if ($_GET["debug"]) print $query; | |
2267 | ||
2268 | } else { | |
2269 | // browsing by tag | |
2270 | ||
2271 | $feed_kind = "Tags"; | |
2272 | ||
2273 | $result = db_query($link, "SELECT | |
2274 | ttrss_entries.id as id,title, | |
2275 | SUBSTRING(updated,1,16) as updated, | |
2276 | unread,feed_id, | |
2277 | marked,link,last_read, | |
2278 | SUBSTRING(last_read,1,19) as last_read_noms, | |
2279 | $vfeed_query_part | |
2280 | $content_query_part | |
2281 | SUBSTRING(updated,1,19) as updated_noms | |
2282 | FROM | |
2283 | ttrss_entries,ttrss_user_entries,ttrss_tags | |
2284 | WHERE | |
2285 | ref_id = ttrss_entries.id AND | |
2286 | ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND | |
2287 | post_int_id = int_id AND tag_name = '$feed' AND | |
2288 | $view_query_part | |
2289 | $search_query_part | |
2290 | $query_strategy_part ORDER BY $order_by | |
2291 | $limit_query_part"); | |
2292 | } | |
2293 | ||
c7188969 | 2294 | return array($result, $feed_title, $feed_site_url, $last_error); |
ef393de7 AD |
2295 | |
2296 | } | |
2297 | ||
e1eb2147 AD |
2298 | function generate_syndicated_feed($link, $feed, $is_cat, |
2299 | $search, $search_mode, $match_on) { | |
18664970 AD |
2300 | |
2301 | $qfh_ret = queryFeedHeadlines($link, $feed, | |
e939722a | 2302 | 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC"); |
18664970 AD |
2303 | |
2304 | $result = $qfh_ret[0]; | |
59e2aab4 | 2305 | $feed_title = htmlspecialchars($qfh_ret[1]); |
18664970 AD |
2306 | $feed_site_url = $qfh_ret[2]; |
2307 | $last_error = $qfh_ret[3]; | |
2308 | ||
2309 | print "<rss version=\"2.0\"> | |
2310 | <channel> | |
2311 | <title>$feed_title</title> | |
2312 | <link>$feed_site_url</link> | |
2313 | <generator>Tiny Tiny RSS v".VERSION."</generator>"; | |
2314 | ||
2315 | while ($line = db_fetch_assoc($result)) { | |
2316 | print "<item>"; | |
2317 | print "<link>" . htmlspecialchars($line["link"]) . "</link>"; | |
2318 | ||
2319 | $rfc822_date = date('r', strtotime($line["updated"])); | |
2320 | ||
2321 | print "<pubDate>$rfc822_date</pubDate>"; | |
2322 | ||
2323 | print "<title>" . | |
2324 | htmlspecialchars($line["title"]) . "</title>"; | |
2325 | ||
2326 | print "<description>" . | |
2327 | htmlspecialchars($line["content_preview"]) . "</description>"; | |
2328 | ||
2329 | print "</item>"; | |
2330 | } | |
2331 | ||
2332 | print "</channel></rss>"; | |
2333 | ||
2334 | } | |
2335 | ||
0a6c4846 AD |
2336 | function getCategoryTitle($link, $cat_id) { |
2337 | ||
2338 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE | |
2339 | id = '$cat_id'"); | |
2340 | ||
2341 | if (db_num_rows($result) == 1) { | |
2342 | return db_fetch_result($result, 0, "title"); | |
2343 | } else { | |
2344 | return "Uncategorized"; | |
2345 | } | |
2346 | } | |
2347 | ||
183ad07b AD |
2348 | function sanitize_rss($str) { |
2349 | $res = ""; | |
2350 | ||
2351 | $res = preg_replace('/<script.*?>/i', | |
2352 | "<p class=\"scriptWarn\">", $str); | |
2353 | ||
2354 | $res = preg_replace('/<\/script>/i', | |
2355 | "</p>", $res); | |
2356 | ||
2357 | return $res; | |
2358 | } | |
b72c3ef8 AD |
2359 | |
2360 | function check_for_update($link) { | |
2361 | $releases_feed = "http://tt-rss.spb.ru/releases.rss"; | |
2362 | ||
2363 | if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) { | |
2364 | return; | |
2365 | } | |
2366 | ||
2367 | error_reporting(0); | |
2368 | $rss = fetch_rss($releases_feed); | |
2369 | error_reporting (DEFAULT_ERROR_LEVEL); | |
2370 | ||
2371 | if ($rss) { | |
2372 | ||
2373 | $items = $rss->items; | |
2374 | ||
2375 | if (!$items || !is_array($items)) $items = $rss->entries; | |
2376 | if (!$items || !is_array($items)) $items = $rss; | |
2377 | ||
da412ad3 | 2378 | if (!is_array($items) || count($items) == 0) { |
b72c3ef8 | 2379 | return; |
da412ad3 | 2380 | } |
b72c3ef8 AD |
2381 | |
2382 | $last_item = $items[0]; | |
2383 | ||
2384 | $last_version = trim(preg_replace("/(Milestone)|(completed)/", "", $last_item["title"])); | |
2385 | $cur_version = preg_replace("/\.99/", "", VERSION); | |
2386 | ||
2387 | $release_url = $last_item["link"]; | |
2388 | ||
9157e015 | 2389 | if ($cur_version != $last_version) { |
b72c3ef8 | 2390 | return "<div class=\"notice\"><a target=\"_new\" href=\"$release_url\"> |
da412ad3 AD |
2391 | New version of Tiny-Tiny RSS ($last_version) is available.</a></div>"; |
2392 | } | |
b72c3ef8 AD |
2393 | } |
2394 | } | |
40d13c28 | 2395 | ?> |