]>
Commit | Line | Data |
---|---|---|
40d13c28 | 1 | <? |
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'; |
40d13c28 | 13 | |
387234f3 AD |
14 | require_once 'magpierss/rss_utils.inc'; |
15 | ||
a3ee2a38 AD |
16 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); |
17 | ||
ad507f85 AD |
18 | function purge_feed($link, $feed_id, $purge_interval, $debug = false) { |
19 | ||
20 | $rows = -1; | |
4c193675 | 21 | |
fefa6ca3 | 22 | if (DB_TYPE == "pgsql") { |
44e241cb | 23 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 24 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 25 | (SELECT date_entered FROM ttrss_entries WHERE |
44e241cb AD |
26 | id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */ |
27 | ||
6e7f8d26 AD |
28 | $pg_version = get_pgsql_version($link); |
29 | ||
30 | if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) { | |
1e59ae35 AD |
31 | |
32 | $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
33 | ttrss_entries.id = ref_id AND | |
34 | marked = false AND | |
35 | feed_id = '$feed_id' AND | |
36 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); | |
37 | ||
38 | } else { | |
39 | ||
40 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
41 | USING ttrss_entries | |
42 | WHERE ttrss_entries.id = ref_id AND | |
43 | marked = false AND | |
44 | feed_id = '$feed_id' AND | |
fc774155 | 45 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); |
1e59ae35 | 46 | } |
ad507f85 AD |
47 | |
48 | $rows = pg_affected_rows($result); | |
49 | ||
fefa6ca3 | 50 | } else { |
1e59ae35 | 51 | |
30f1746f | 52 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 53 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 54 | (SELECT date_entered FROM ttrss_entries WHERE |
30f1746f AD |
55 | id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */ |
56 | ||
57 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
58 | USING ttrss_user_entries, ttrss_entries | |
59 | WHERE ttrss_entries.id = ref_id AND | |
60 | marked = false AND | |
61 | feed_id = '$feed_id' AND | |
62 | ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); | |
63 | ||
ad507f85 AD |
64 | $rows = mysql_affected_rows($link); |
65 | ||
66 | } | |
67 | ||
68 | if ($debug) { | |
69 | print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n"; | |
fefa6ca3 AD |
70 | } |
71 | } | |
72 | ||
44e241cb AD |
73 | function global_purge_old_posts($link, $do_output = false, $limit = false) { |
74 | ||
894ebcf5 | 75 | $random_qpart = sql_random_function(); |
fefa6ca3 | 76 | |
44e241cb AD |
77 | if ($limit) { |
78 | $limit_qpart = "LIMIT $limit"; | |
79 | } else { | |
80 | $limit_qpart = ""; | |
81 | } | |
82 | ||
fefa6ca3 | 83 | $result = db_query($link, |
44e241cb AD |
84 | "SELECT id,purge_interval,owner_uid FROM ttrss_feeds |
85 | ORDER BY $random_qpart $limit_qpart"); | |
fefa6ca3 AD |
86 | |
87 | while ($line = db_fetch_assoc($result)) { | |
88 | ||
89 | $feed_id = $line["id"]; | |
90 | $purge_interval = $line["purge_interval"]; | |
91 | $owner_uid = $line["owner_uid"]; | |
92 | ||
93 | if ($purge_interval == 0) { | |
94 | ||
95 | $tmp_result = db_query($link, | |
96 | "SELECT value FROM ttrss_user_prefs WHERE | |
97 | pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'"); | |
98 | ||
99 | if (db_num_rows($tmp_result) != 0) { | |
100 | $purge_interval = db_fetch_result($tmp_result, 0, "value"); | |
101 | } | |
102 | } | |
103 | ||
104 | if ($do_output) { | |
ad507f85 | 105 | // print "Feed $feed_id: purge interval = $purge_interval\n"; |
fefa6ca3 AD |
106 | } |
107 | ||
108 | if ($purge_interval > 0) { | |
ad507f85 | 109 | purge_feed($link, $feed_id, $purge_interval, $do_output); |
fefa6ca3 AD |
110 | } |
111 | } | |
112 | ||
71604ca4 AD |
113 | // purge orphaned posts in main content table |
114 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
115 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
116 | ||
fefa6ca3 AD |
117 | } |
118 | ||
b6eefba5 | 119 | function purge_old_posts($link) { |
5d73494a | 120 | |
f1a80dae AD |
121 | $user_id = $_SESSION["uid"]; |
122 | ||
123 | $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds | |
124 | WHERE owner_uid = '$user_id'"); | |
5d73494a AD |
125 | |
126 | while ($line = db_fetch_assoc($result)) { | |
127 | ||
128 | $feed_id = $line["id"]; | |
129 | $purge_interval = $line["purge_interval"]; | |
130 | ||
b619ff15 | 131 | if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS'); |
5d73494a | 132 | |
140aae81 | 133 | if ($purge_interval > 0) { |
fefa6ca3 | 134 | purge_feed($link, $feed_id, $purge_interval); |
5d73494a AD |
135 | } |
136 | } | |
71604ca4 AD |
137 | |
138 | // purge orphaned posts in main content table | |
139 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
140 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
c3a8d71a AD |
141 | } |
142 | ||
1f2b01ed | 143 | function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) { |
40d13c28 | 144 | |
4769ddaf | 145 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 146 | |
a2770077 AD |
147 | if (!$user_id) { |
148 | $user_id = $_SESSION["uid"]; | |
149 | purge_old_posts($link); | |
150 | } | |
151 | ||
25af8dad | 152 | // db_query($link, "BEGIN"); |
b82af8c3 | 153 | |
cbd8650d AD |
154 | if (MAX_UPDATE_TIME > 0) { |
155 | if (DB_TYPE == "mysql") { | |
156 | $q_order = "RAND()"; | |
157 | } else { | |
158 | $q_order = "RANDOM()"; | |
159 | } | |
160 | } else { | |
161 | $q_order = "last_updated DESC"; | |
162 | } | |
163 | ||
d148926e | 164 | $result = db_query($link, "SELECT feed_url,id, |
798f722b | 165 | SUBSTRING(last_updated,1,19) AS last_updated, |
5c563acd | 166 | update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id' |
cbd8650d AD |
167 | ORDER BY $q_order"); |
168 | ||
169 | $upd_start = time(); | |
40d13c28 | 170 | |
b6eefba5 | 171 | while ($line = db_fetch_assoc($result)) { |
d148926e AD |
172 | $upd_intl = $line["update_interval"]; |
173 | ||
b619ff15 | 174 | if (!$upd_intl || $upd_intl == 0) { |
a2770077 | 175 | $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id); |
b619ff15 | 176 | } |
d148926e | 177 | |
c1e202b7 AD |
178 | if ($upd_intl < 0) { |
179 | // Updates for this feed are disabled | |
180 | continue; | |
181 | } | |
182 | ||
93d40f50 AD |
183 | if ($fetch || (!$line["last_updated"] || |
184 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) { | |
c5142cca | 185 | |
cbd8650d AD |
186 | // print "<!-- feed: ".$line["feed_url"]." -->"; |
187 | ||
1f2b01ed | 188 | update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon); |
cbd8650d AD |
189 | |
190 | $upd_elapsed = time() - $upd_start; | |
191 | ||
192 | if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) { | |
193 | return; | |
194 | } | |
d148926e | 195 | } |
40d13c28 AD |
196 | } |
197 | ||
25af8dad | 198 | // db_query($link, "COMMIT"); |
b82af8c3 | 199 | |
40d13c28 AD |
200 | } |
201 | ||
9e997874 | 202 | function check_feed_favicon($feed_url, $feed, $link) { |
78800912 AD |
203 | $feed_url = str_replace("http://", "", $feed_url); |
204 | $feed_url = preg_replace("/\/.*$/", "", $feed_url); | |
205 | ||
206 | $icon_url = "http://$feed_url/favicon.ico"; | |
273a2f6b | 207 | $icon_file = ICONS_DIR . "/$feed.ico"; |
78800912 AD |
208 | |
209 | if (!file_exists($icon_file)) { | |
e695fdc8 | 210 | |
78800912 AD |
211 | error_reporting(0); |
212 | $r = fopen($icon_url, "r"); | |
cce28758 | 213 | error_reporting (DEFAULT_ERROR_LEVEL); |
78800912 AD |
214 | |
215 | if ($r) { | |
7d7cbaf5 | 216 | $tmpfname = tempnam(TMP_DIRECTORY, "ttrssicon"); |
78800912 AD |
217 | |
218 | $t = fopen($tmpfname, "w"); | |
219 | ||
220 | while (!feof($r)) { | |
221 | $buf = fread($r, 16384); | |
222 | fwrite($t, $buf); | |
223 | } | |
224 | ||
225 | fclose($r); | |
226 | fclose($t); | |
227 | ||
e695fdc8 AD |
228 | error_reporting(0); |
229 | if (!rename($tmpfname, $icon_file)) { | |
230 | unlink($tmpfname); | |
231 | } | |
717f5e64 AD |
232 | |
233 | chmod($icon_file, 0644); | |
234 | ||
cce28758 | 235 | error_reporting (DEFAULT_ERROR_LEVEL); |
78800912 AD |
236 | |
237 | } | |
238 | } | |
239 | } | |
240 | ||
ddb68b81 | 241 | function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) { |
40d13c28 | 242 | |
4769ddaf | 243 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 244 | |
ddb68b81 | 245 | if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) { |
21cfcdf2 AD |
246 | return; |
247 | } | |
248 | ||
47c6c988 | 249 | $result = db_query($link, "SELECT update_interval,auth_login,auth_pass |
a88c1f36 AD |
250 | FROM ttrss_feeds WHERE id = '$feed'"); |
251 | ||
47c6c988 AD |
252 | $auth_login = db_fetch_result($result, 0, "auth_login"); |
253 | $auth_pass = db_fetch_result($result, 0, "auth_pass"); | |
254 | ||
a88c1f36 AD |
255 | $update_interval = db_fetch_result($result, 0, "update_interval"); |
256 | ||
257 | if ($update_interval < 0) { return; } | |
258 | ||
ab3d0b99 AD |
259 | $feed = db_escape_string($feed); |
260 | ||
47c6c988 AD |
261 | $fetch_url = $feed_url; |
262 | ||
263 | if ($auth_login && $auth_pass) { | |
264 | $url_parts = array(); | |
265 | preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts); | |
266 | ||
267 | if ($url_parts[1] && $url_parts[2]) { | |
268 | $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2]; | |
269 | } | |
270 | ||
271 | } | |
3ad5aa85 | 272 | error_reporting(0); |
47c6c988 | 273 | $rss = fetch_rss($fetch_url); |
ab3d0b99 | 274 | |
cce28758 | 275 | error_reporting (DEFAULT_ERROR_LEVEL); |
76798ff3 | 276 | |
b6eefba5 | 277 | $feed = db_escape_string($feed); |
dcee8f61 | 278 | |
40d13c28 | 279 | if ($rss) { |
b82af8c3 | 280 | |
44e241cb | 281 | // db_query($link, "BEGIN"); |
dd8c76a9 | 282 | |
a88c1f36 | 283 | $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid |
f324892e | 284 | FROM ttrss_feeds WHERE id = '$feed'"); |
331900c6 | 285 | |
b6eefba5 AD |
286 | $registered_title = db_fetch_result($result, 0, "title"); |
287 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
f324892e | 288 | $orig_site_url = db_fetch_result($result, 0, "site_url"); |
331900c6 | 289 | |
7fed1940 AD |
290 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); |
291 | ||
a2770077 AD |
292 | if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) { |
293 | check_feed_favicon($feed_url, $feed, $link); | |
294 | } | |
295 | ||
746b249f | 296 | if (!$registered_title || $registered_title == "[Unknown]") { |
e1305a97 | 297 | $feed_title = db_escape_string($rss->channel["title"]); |
f324892e AD |
298 | db_query($link, "UPDATE ttrss_feeds SET |
299 | title = '$feed_title' WHERE id = '$feed'"); | |
300 | } | |
301 | ||
147f7691 | 302 | $site_url = $rss->channel["link"]; |
832b7bfc AD |
303 | // weird, weird Magpie |
304 | if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]); | |
147f7691 AD |
305 | |
306 | if ($site_url && $orig_site_url != db_escape_string($site_url)) { | |
f324892e AD |
307 | db_query($link, "UPDATE ttrss_feeds SET |
308 | site_url = '$site_url' WHERE id = '$feed'"); | |
331900c6 | 309 | } |
40d13c28 | 310 | |
b7f4bda2 AD |
311 | // print "I: " . $rss->channel["image"]["url"]; |
312 | ||
313 | $icon_url = $rss->image["url"]; | |
314 | ||
147f7691 | 315 | if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) { |
b6eefba5 AD |
316 | $icon_url = db_escape_string($icon_url); |
317 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
b7f4bda2 AD |
318 | } |
319 | ||
e6155a06 AD |
320 | |
321 | $filters = array(); | |
322 | ||
4b3dff6e | 323 | $result = db_query($link, "SELECT reg_exp, |
db42b934 AD |
324 | ttrss_filter_types.name AS name, |
325 | ttrss_filter_actions.name AS action | |
326 | FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE | |
327 | owner_uid = $owner_uid AND | |
328 | ttrss_filter_types.id = filter_type AND | |
329 | ttrss_filter_actions.id = action_id AND | |
ead60402 | 330 | (feed_id IS NULL OR feed_id = '$feed')"); |
e6155a06 | 331 | |
b6eefba5 | 332 | while ($line = db_fetch_assoc($result)) { |
e6155a06 | 333 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); |
19c9cb11 AD |
334 | |
335 | $filter["reg_exp"] = $line["reg_exp"]; | |
336 | $filter["action"] = $line["action"]; | |
337 | ||
338 | array_push($filters[$line["name"]], $filter); | |
e6155a06 AD |
339 | } |
340 | ||
ddb68b81 AD |
341 | $iterator = $rss->items; |
342 | ||
c22789da AD |
343 | if (!$iterator || !is_array($iterator)) $iterator = $rss->entries; |
344 | if (!$iterator || !is_array($iterator)) $iterator = $rss; | |
345 | ||
346 | if (!is_array($iterator)) { | |
9a7a7b41 | 347 | db_query($link, "UPDATE ttrss_feeds |
75bd0669 | 348 | SET last_error = 'Parse error: can\'t find any articles.' |
9a7a7b41 | 349 | WHERE id = '$feed'"); |
c22789da AD |
350 | return; // WTF? |
351 | } | |
ddb68b81 AD |
352 | |
353 | foreach ($iterator as $item) { | |
40d13c28 AD |
354 | |
355 | $entry_guid = $item["id"]; | |
356 | ||
357 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
358 | if (!$entry_guid) $entry_guid = $item["link"]; | |
466001c4 AD |
359 | |
360 | if (!$entry_guid) continue; | |
a116f569 | 361 | |
9c9c7e6b | 362 | $entry_timestamp = ""; |
b82af8c3 | 363 | |
9c9c7e6b AD |
364 | $rss_2_date = $item['pubdate']; |
365 | $rss_1_date = $item['dc']['date']; | |
366 | $atom_date = $item['issued']; | |
59ba2c75 | 367 | if (!$atom_date) $atom_date = $item['updated']; |
b197f117 | 368 | |
9c9c7e6b AD |
369 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); |
370 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
371 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
b82af8c3 AD |
372 | |
373 | if ($entry_timestamp == "") { | |
374 | $entry_timestamp = time(); | |
375 | $no_orig_date = 'true'; | |
466001c4 AD |
376 | } else { |
377 | $no_orig_date = 'false'; | |
b82af8c3 | 378 | } |
b197f117 | 379 | |
466001c4 | 380 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); |
71ad3959 | 381 | |
40d13c28 | 382 | $entry_title = $item["title"]; |
ddb68b81 AD |
383 | |
384 | // strange Magpie workaround | |
385 | $entry_link = $item["link_"]; | |
386 | if (!$entry_link) $entry_link = $item["link"]; | |
71ad3959 AD |
387 | |
388 | if (!$entry_title) continue; | |
389 | if (!$entry_link) continue; | |
390 | ||
1696229f AD |
391 | $entry_content = $item["content:escaped"]; |
392 | ||
393 | if (!$entry_content) $entry_content = $item["content:encoded"]; | |
40d13c28 | 394 | if (!$entry_content) $entry_content = $item["content"]; |
372ced8b | 395 | if (!$entry_content) $entry_content = $item["summary"]; |
1696229f | 396 | if (!$entry_content) $entry_content = $item["description"]; |
a2015351 | 397 | |
a116f569 | 398 | // if (!$entry_content) continue; |
a2015351 | 399 | |
8add756a AD |
400 | // WTF |
401 | if (is_array($entry_content)) { | |
402 | $entry_content = $entry_content["encoded"]; | |
1696229f | 403 | if (!$entry_content) $entry_content = $entry_content["escaped"]; |
8add756a AD |
404 | } |
405 | ||
1696229f | 406 | // print_r($item); |
372ced8b AD |
407 | // print_r(htmlspecialchars($entry_content)); |
408 | // print "<br>"; | |
1696229f | 409 | |
372ced8b | 410 | $entry_content_unescaped = $entry_content; |
466001c4 | 411 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); |
cb0bd8bd | 412 | |
a1ea1e12 AD |
413 | $entry_comments = $item["comments"]; |
414 | ||
b6104dee AD |
415 | $entry_author = db_escape_string($item['dc']['creator']); |
416 | ||
b6eefba5 | 417 | $entry_guid = db_escape_string($entry_guid); |
2651fc4f | 418 | |
05732aa0 AD |
419 | $result = db_query($link, "SELECT id FROM ttrss_entries |
420 | WHERE guid = '$entry_guid'"); | |
4c193675 | 421 | |
b17fcb1a AD |
422 | $entry_content = db_escape_string($entry_content); |
423 | $entry_title = db_escape_string($entry_title); | |
424 | $entry_link = db_escape_string($entry_link); | |
425 | $entry_comments = db_escape_string($entry_comments); | |
426 | ||
27f089dc | 427 | $num_comments = db_escape_string($item["slash"]["comments"]); |
11b0dce2 | 428 | |
e31073bd AD |
429 | if (!$num_comments) $num_comments = 0; |
430 | ||
44e241cb AD |
431 | db_query($link, "BEGIN"); |
432 | ||
4c193675 AD |
433 | if (db_num_rows($result) == 0) { |
434 | ||
435 | // base post entry does not exist, create it | |
436 | ||
4c193675 AD |
437 | $result = db_query($link, |
438 | "INSERT INTO ttrss_entries | |
439 | (title, | |
440 | guid, | |
441 | link, | |
442 | updated, | |
443 | content, | |
444 | content_hash, | |
445 | no_orig_date, | |
446 | date_entered, | |
11b0dce2 | 447 | comments, |
b6104dee AD |
448 | num_comments, |
449 | author) | |
4c193675 AD |
450 | VALUES |
451 | ('$entry_title', | |
452 | '$entry_guid', | |
453 | '$entry_link', | |
454 | '$entry_timestamp_fmt', | |
455 | '$entry_content', | |
456 | '$content_hash', | |
457 | $no_orig_date, | |
458 | NOW(), | |
11b0dce2 | 459 | '$entry_comments', |
b6104dee AD |
460 | '$num_comments', |
461 | '$entry_author')"); | |
8926aab8 AD |
462 | } else { |
463 | // we keep encountering the entry in feeds, so we need to | |
464 | // update date_entered column so that we don't get horrible | |
465 | // dupes when the entry gets purged and reinserted again e.g. | |
466 | // in the case of SLOW SLOW OMG SLOW updating feeds | |
467 | ||
468 | $base_entry_id = db_fetch_result($result, 0, "id"); | |
469 | ||
470 | db_query($link, "UPDATE ttrss_entries SET date_entered = NOW() | |
471 | WHERE id = '$base_entry_id'"); | |
4c193675 AD |
472 | } |
473 | ||
474 | // now it should exist, if not - bad luck then | |
475 | ||
6385315d AD |
476 | $result = db_query($link, "SELECT |
477 | id,content_hash,no_orig_date,title, | |
8926aab8 | 478 | substring(date_entered,1,19) as date_entered, |
11b0dce2 AD |
479 | substring(updated,1,19) as updated, |
480 | num_comments | |
6385315d AD |
481 | FROM |
482 | ttrss_entries | |
483 | WHERE guid = '$entry_guid'"); | |
4c193675 AD |
484 | |
485 | if (db_num_rows($result) == 1) { | |
486 | ||
11b0dce2 AD |
487 | // this will be used below in update handler |
488 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); | |
489 | $orig_title = db_fetch_result($result, 0, "title"); | |
490 | $orig_num_comments = db_fetch_result($result, 0, "num_comments"); | |
8926aab8 AD |
491 | $orig_date_entered = strtotime(db_fetch_result($result, |
492 | 0, "date_entered")); | |
6385315d | 493 | |
11b0dce2 | 494 | $ref_id = db_fetch_result($result, 0, "id"); |
4c193675 | 495 | |
11b0dce2 | 496 | // check for user post link to main table |
4c193675 | 497 | |
11b0dce2 AD |
498 | // do we allow duplicate posts with same GUID in different feeds? |
499 | if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) { | |
500 | $dupcheck_qpart = "AND feed_id = '$feed'"; | |
501 | } else { | |
502 | $dupcheck_qpart = ""; | |
503 | } | |
71604ca4 | 504 | |
11b0dce2 | 505 | // error_reporting(0); |
19c9cb11 | 506 | |
11b0dce2 AD |
507 | $filter_name = get_filter_name($entry_title, $entry_content, |
508 | $entry_link, $filters); | |
19c9cb11 | 509 | |
11b0dce2 AD |
510 | if ($filter_name == "filter") { |
511 | continue; | |
512 | } | |
19c9cb11 | 513 | |
11b0dce2 | 514 | // error_reporting (DEFAULT_ERROR_LEVEL); |
3a933f22 | 515 | |
11b0dce2 AD |
516 | $result = db_query($link, |
517 | "SELECT ref_id FROM ttrss_user_entries WHERE | |
518 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' | |
519 | $dupcheck_qpart"); | |
520 | ||
521 | // okay it doesn't exist - create user entry | |
522 | if (db_num_rows($result) == 0) { | |
523 | ||
524 | if ($filter_name != 'catchup') { | |
525 | $unread = 'true'; | |
526 | $last_read_qpart = 'NULL'; | |
527 | } else { | |
528 | $unread = 'false'; | |
529 | $last_read_qpart = 'NOW()'; | |
530 | } | |
19c9cb11 | 531 | |
11b0dce2 AD |
532 | $result = db_query($link, |
533 | "INSERT INTO ttrss_user_entries | |
534 | (ref_id, owner_uid, feed_id, unread, last_read) | |
535 | VALUES ('$ref_id', '$owner_uid', '$feed', $unread, | |
536 | $last_read_qpart)"); | |
537 | } | |
538 | ||
6385315d AD |
539 | $post_needs_update = false; |
540 | ||
a2770077 | 541 | if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) && |
6385315d AD |
542 | ($content_hash != $orig_content_hash)) { |
543 | $post_needs_update = true; | |
544 | } | |
545 | ||
546 | if ($orig_title != $entry_title) { | |
547 | $post_needs_update = true; | |
548 | } | |
549 | ||
11b0dce2 AD |
550 | if ($orig_num_comments != $num_comments) { |
551 | $post_needs_update = true; | |
552 | } | |
553 | ||
6385315d AD |
554 | // this doesn't seem to be very reliable |
555 | // | |
556 | // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) { | |
557 | // $post_needs_update = true; | |
558 | // } | |
559 | ||
560 | // if post needs update, update it and mark all user entries | |
1c73bc0c | 561 | // linking to this post as updated |
6385315d AD |
562 | if ($post_needs_update) { |
563 | ||
564 | // print "<!-- post $orig_title needs update : $post_needs_update -->"; | |
565 | ||
6385315d | 566 | db_query($link, "UPDATE ttrss_entries |
11b0dce2 AD |
567 | SET title = '$entry_title', content = '$entry_content', |
568 | num_comments = '$num_comments' | |
6385315d AD |
569 | WHERE id = '$ref_id'"); |
570 | ||
571 | db_query($link, "UPDATE ttrss_user_entries | |
572 | SET last_read = null WHERE ref_id = '$ref_id' AND unread = false"); | |
573 | ||
574 | } | |
4c193675 AD |
575 | } |
576 | ||
44e241cb AD |
577 | db_query($link, "COMMIT"); |
578 | ||
eb36b4eb AD |
579 | /* taaaags */ |
580 | // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, // | |
581 | ||
05732aa0 | 582 | $entry_tags = null; |
eb36b4eb | 583 | |
372ced8b | 584 | preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i", |
ee2c3050 AD |
585 | $entry_content_unescaped, $entry_tags); |
586 | ||
587 | // print "<br>$entry_title : $entry_content_unescaped<br>"; | |
588 | // print_r($entry_tags); | |
372ced8b | 589 | // print "<br>"; |
eb36b4eb AD |
590 | |
591 | $entry_tags = $entry_tags[1]; | |
592 | ||
593 | if (count($entry_tags) > 0) { | |
594 | ||
44e241cb AD |
595 | db_query($link, "BEGIN"); |
596 | ||
05732aa0 AD |
597 | $result = db_query($link, "SELECT id,int_id |
598 | FROM ttrss_entries,ttrss_user_entries | |
25da6909 | 599 | WHERE guid = '$entry_guid' |
05732aa0 | 600 | AND feed_id = '$feed' AND ref_id = id |
7fed1940 | 601 | AND owner_uid = '$owner_uid'"); |
eb36b4eb | 602 | |
fe99ab12 | 603 | if (db_num_rows($result) == 1) { |
eb36b4eb | 604 | |
fe99ab12 AD |
605 | $entry_id = db_fetch_result($result, 0, "id"); |
606 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
607 | ||
608 | foreach ($entry_tags as $tag) { | |
609 | $tag = db_escape_string(strtolower($tag)); | |
31483fc1 AD |
610 | |
611 | $tag = str_replace("+", " ", $tag); | |
fe99ab12 AD |
612 | $tag = str_replace("technorati tag: ", "", $tag); |
613 | ||
614 | $result = db_query($link, "SELECT id FROM ttrss_tags | |
615 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND | |
616 | owner_uid = '$owner_uid' LIMIT 1"); | |
617 | ||
618 | // print db_fetch_result($result, 0, "id"); | |
619 | ||
620 | if ($result && db_num_rows($result) == 0) { | |
621 | ||
622 | // print "tagging $entry_id as $tag<br>"; | |
623 | ||
624 | db_query($link, "INSERT INTO ttrss_tags | |
625 | (owner_uid,tag_name,post_int_id) | |
626 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
627 | } | |
628 | } | |
eb36b4eb | 629 | } |
44e241cb | 630 | db_query($link, "COMMIT"); |
05732aa0 | 631 | } |
4c193675 | 632 | } |
40d13c28 | 633 | |
ab3d0b99 AD |
634 | db_query($link, "UPDATE ttrss_feeds |
635 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
eb36b4eb | 636 | |
44e241cb | 637 | // db_query($link, "COMMIT"); |
dd8c76a9 | 638 | |
ab3d0b99 AD |
639 | } else { |
640 | $error_msg = db_escape_string(magpie_error()); | |
641 | db_query($link, | |
aa5f9f5f AD |
642 | "UPDATE ttrss_feeds SET last_error = '$error_msg', |
643 | last_updated = NOW() WHERE id = '$feed'"); | |
40d13c28 AD |
644 | } |
645 | ||
646 | } | |
647 | ||
f175937c AD |
648 | function print_select($id, $default, $values, $attributes = "") { |
649 | print "<select id=\"$id\" $attributes>"; | |
a0d53889 AD |
650 | foreach ($values as $v) { |
651 | if ($v == $default) | |
652 | $sel = " selected"; | |
653 | else | |
654 | $sel = ""; | |
655 | ||
656 | print "<option$sel>$v</option>"; | |
657 | } | |
658 | print "</select>"; | |
659 | } | |
40d13c28 | 660 | |
19c9cb11 | 661 | function get_filter_name($title, $content, $link, $filters) { |
e6155a06 AD |
662 | |
663 | if ($filters["title"]) { | |
19c9cb11 AD |
664 | foreach ($filters["title"] as $filter) { |
665 | $reg_exp = $filter["reg_exp"]; | |
666 | if (preg_match("/$reg_exp/i", $title)) { | |
667 | return $filter["action"]; | |
668 | } | |
e6155a06 AD |
669 | } |
670 | } | |
671 | ||
672 | if ($filters["content"]) { | |
19c9cb11 AD |
673 | foreach ($filters["content"] as $filter) { |
674 | $reg_exp = $filter["reg_exp"]; | |
675 | if (preg_match("/$reg_exp/i", $content)) { | |
676 | return $filter["action"]; | |
677 | } | |
e6155a06 AD |
678 | } |
679 | } | |
680 | ||
681 | if ($filters["both"]) { | |
682 | foreach ($filters["both"] as $filter) { | |
19c9cb11 AD |
683 | $reg_exp = $filter["reg_exp"]; |
684 | if (preg_match("/$reg_exp/i", $title) || | |
685 | preg_match("/$reg_exp/i", $content)) { | |
686 | return $filter["action"]; | |
687 | } | |
e6155a06 AD |
688 | } |
689 | } | |
690 | ||
3a933f22 | 691 | if ($filters["link"]) { |
19c9cb11 AD |
692 | $reg_exp = $filter["reg_exp"]; |
693 | foreach ($filters["link"] as $filter) { | |
694 | $reg_exp = $filter["reg_exp"]; | |
695 | if (preg_match("/$reg_exp/i", $link)) { | |
696 | return $filter["action"]; | |
697 | } | |
3a933f22 AD |
698 | } |
699 | } | |
700 | ||
e6155a06 AD |
701 | return false; |
702 | } | |
703 | ||
9323147e | 704 | function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link, |
fb1fb4ab | 705 | $rtl_content = false, $last_updated = false, $last_error = false) { |
254e0e4b AD |
706 | |
707 | if (file_exists($icon_file) && filesize($icon_file) > 0) { | |
023fe037 | 708 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">"; |
254e0e4b | 709 | } else { |
023fe037 | 710 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">"; |
254e0e4b AD |
711 | } |
712 | ||
9323147e AD |
713 | if ($rtl_content) { |
714 | $rtl_tag = "dir=\"rtl\""; | |
715 | } else { | |
716 | $rtl_tag = "dir=\"ltr\""; | |
717 | } | |
718 | ||
fb1fb4ab AD |
719 | if ($last_error) { |
720 | $link_title = "Error: $last_error ($last_updated)"; | |
ad780e9c | 721 | } else if ($last_updated) { |
fb1fb4ab AD |
722 | $link_title = "Updated: $last_updated"; |
723 | } | |
724 | ||
725 | $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>"; | |
254e0e4b AD |
726 | |
727 | print "<li id=\"FEEDR-$feed_id\" class=\"$class\">"; | |
b619ff15 | 728 | if (get_pref($link, 'ENABLE_FEED_ICONS')) { |
254e0e4b AD |
729 | print "$feed_icon"; |
730 | } | |
731 | ||
9323147e | 732 | print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>"; |
254e0e4b AD |
733 | |
734 | if ($unread != 0) { | |
735 | $fctr_class = ""; | |
736 | } else { | |
737 | $fctr_class = "class=\"invisible\""; | |
738 | } | |
739 | ||
9323147e | 740 | print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\"> |
254e0e4b AD |
741 | (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>"; |
742 | ||
743 | print "</li>"; | |
744 | ||
745 | } | |
746 | ||
406d9489 AD |
747 | function getmicrotime() { |
748 | list($usec, $sec) = explode(" ",microtime()); | |
749 | return ((float)$usec + (float)$sec); | |
750 | } | |
751 | ||
77e96719 AD |
752 | function print_radio($id, $default, $values, $attributes = "") { |
753 | foreach ($values as $v) { | |
754 | ||
755 | if ($v == $default) | |
5da169d9 | 756 | $sel = "checked"; |
77e96719 | 757 | else |
5da169d9 AD |
758 | $sel = ""; |
759 | ||
760 | if ($v == "Yes") { | |
761 | $sel .= " value=\"1\""; | |
762 | } else { | |
763 | $sel .= " value=\"0\""; | |
764 | } | |
77e96719 | 765 | |
69654950 AD |
766 | print "<input class=\"noborder\" |
767 | type=\"radio\" $sel $attributes name=\"$id\"> $v "; | |
77e96719 AD |
768 | |
769 | } | |
770 | } | |
771 | ||
ff485f1d AD |
772 | function initialize_user_prefs($link, $uid) { |
773 | ||
774 | $uid = db_escape_string($uid); | |
775 | ||
776 | db_query($link, "BEGIN"); | |
777 | ||
778 | $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs"); | |
779 | ||
780 | $u_result = db_query($link, "SELECT pref_name | |
781 | FROM ttrss_user_prefs WHERE owner_uid = '$uid'"); | |
782 | ||
783 | $active_prefs = array(); | |
784 | ||
785 | while ($line = db_fetch_assoc($u_result)) { | |
786 | array_push($active_prefs, $line["pref_name"]); | |
787 | } | |
788 | ||
789 | while ($line = db_fetch_assoc($result)) { | |
790 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
791 | // print "adding " . $line["pref_name"] . "<br>"; | |
792 | ||
793 | db_query($link, "INSERT INTO ttrss_user_prefs | |
794 | (owner_uid,pref_name,value) VALUES | |
795 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); | |
796 | ||
797 | } | |
798 | } | |
799 | ||
800 | db_query($link, "COMMIT"); | |
801 | ||
802 | } | |
c8437f35 AD |
803 | |
804 | function authenticate_user($link, $login, $password) { | |
805 | ||
806 | $pwd_hash = 'SHA1:' . sha1($password); | |
807 | ||
203b6d25 | 808 | $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE |
7f16656e | 809 | login = '$login' AND pwd_hash = '$pwd_hash'"); |
c8437f35 AD |
810 | |
811 | if (db_num_rows($result) == 1) { | |
812 | $_SESSION["uid"] = db_fetch_result($result, 0, "id"); | |
813 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); | |
203b6d25 | 814 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); |
c8437f35 | 815 | |
f6f32198 AD |
816 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . |
817 | $_SESSION["uid"]); | |
818 | ||
503eb349 AD |
819 | $user_theme = get_user_theme_path($link); |
820 | ||
821 | $_SESSION["theme"] = $user_theme; | |
916f788a | 822 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; |
503eb349 | 823 | |
f557cd78 AD |
824 | initialize_user_prefs($link, $_SESSION["uid"]); |
825 | ||
c8437f35 AD |
826 | return true; |
827 | } | |
ff485f1d | 828 | |
c8437f35 AD |
829 | return false; |
830 | ||
831 | } | |
832 | ||
e6cb77a0 AD |
833 | function make_password($length = 8) { |
834 | ||
835 | $password = ""; | |
798f722b AD |
836 | $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ"; |
837 | ||
838 | $i = 0; | |
e6cb77a0 AD |
839 | |
840 | while ($i < $length) { | |
841 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
842 | ||
843 | if (!strstr($password, $char)) { | |
844 | $password .= $char; | |
845 | $i++; | |
846 | } | |
847 | } | |
848 | return $password; | |
849 | } | |
850 | ||
851 | // this is called after user is created to initialize default feeds, labels | |
852 | // or whatever else | |
853 | ||
854 | // user preferences are checked on every login, not here | |
855 | ||
856 | function initialize_user($link, $uid) { | |
857 | ||
858 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
859 | values ('$uid','unread = true', 'Unread articles')"); | |
860 | ||
861 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
862 | values ('$uid','last_read is null and unread = false', 'Updated articles')"); | |
863 | ||
864 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
74bff337 | 865 | values ('$uid', 'Tiny Tiny RSS: New Releases', |
628fcd2c | 866 | 'http://tt-rss.spb.ru/releases.rss')"); |
3b0feb9b AD |
867 | |
868 | } | |
e6cb77a0 | 869 | |
b8aa49bc | 870 | function logout_user() { |
5ccc1cf5 AD |
871 | session_destroy(); |
872 | if (isset($_COOKIE[session_name()])) { | |
873 | setcookie(session_name(), '', time()-42000, '/'); | |
874 | } | |
b8aa49bc AD |
875 | } |
876 | ||
75836f33 | 877 | function get_script_urlpath() { |
87a79fa4 | 878 | return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]); |
75836f33 AD |
879 | } |
880 | ||
881 | function get_login_redirect() { | |
882 | $server = $_SERVER["SERVER_NAME"]; | |
883 | ||
884 | if (ENABLE_LOGIN_SSL) { | |
885 | $protocol = "https"; | |
886 | } else { | |
887 | $protocol = "http"; | |
888 | } | |
889 | ||
890 | $url_path = get_script_urlpath(); | |
891 | ||
892 | $redirect_uri = "$protocol://$server$url_path/login.php"; | |
893 | ||
894 | return $redirect_uri; | |
895 | } | |
896 | ||
916f788a | 897 | function validate_session($link) { |
a2e9b457 | 898 | if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) { |
916f788a AD |
899 | if ($_SESSION["ip_address"]) { |
900 | if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) { | |
901 | return false; | |
902 | } | |
903 | } | |
904 | } | |
905 | return true; | |
906 | } | |
907 | ||
7ae65adf AD |
908 | function basic_nosid_redirect_check() { |
909 | if (!SINGLE_USER_MODE) { | |
910 | if (!$_COOKIE["ttrss_sid"]) { | |
911 | $redirect_uri = get_login_redirect(); | |
912 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); | |
913 | header("Location: $redirect_uri?rt=$return_to"); | |
914 | exit; | |
915 | } | |
916 | } | |
917 | } | |
918 | ||
b8aa49bc AD |
919 | function login_sequence($link) { |
920 | if (!SINGLE_USER_MODE) { | |
75836f33 | 921 | |
916f788a AD |
922 | if (!validate_session($link)) { |
923 | logout_user(); | |
924 | $redirect_uri = get_login_redirect(); | |
925 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); | |
926 | header("Location: $redirect_uri?rt=$return_to"); | |
927 | exit; | |
928 | } | |
929 | ||
b8aa49bc AD |
930 | if (!USE_HTTP_AUTH) { |
931 | if (!$_SESSION["uid"]) { | |
75836f33 | 932 | $redirect_uri = get_login_redirect(); |
e31dca14 AD |
933 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); |
934 | header("Location: $redirect_uri?rt=$return_to"); | |
b8aa49bc AD |
935 | exit; |
936 | } | |
937 | } else { | |
f557cd78 AD |
938 | if (!$_SESSION["uid"]) { |
939 | if (!$_SERVER["PHP_AUTH_USER"]) { | |
940 | ||
941 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
942 | header('HTTP/1.0 401 Unauthorized'); | |
943 | exit; | |
944 | ||
945 | } else { | |
946 | $auth_result = authenticate_user($link, | |
947 | $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]); | |
948 | ||
949 | if (!$auth_result) { | |
950 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
951 | header('HTTP/1.0 401 Unauthorized'); | |
952 | exit; | |
953 | } | |
954 | } | |
955 | } | |
b8aa49bc AD |
956 | } |
957 | } else { | |
958 | $_SESSION["uid"] = 1; | |
959 | $_SESSION["name"] = "admin"; | |
c7a03b7a | 960 | initialize_user_prefs($link, 1); |
b8aa49bc AD |
961 | } |
962 | } | |
3547842a AD |
963 | |
964 | function truncate_string($str, $max_len) { | |
12db369c AD |
965 | if (mb_strlen($str, "utf-8") > $max_len - 3) { |
966 | return mb_substr($str, 0, $max_len, "utf-8") . "..."; | |
3547842a AD |
967 | } else { |
968 | return $str; | |
969 | } | |
970 | } | |
54a60e1a AD |
971 | |
972 | function get_user_theme_path($link) { | |
798f722b AD |
973 | $result = db_query($link, "SELECT theme_path |
974 | FROM | |
975 | ttrss_themes,ttrss_users | |
976 | WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]); | |
54a60e1a AD |
977 | if (db_num_rows($result) != 0) { |
978 | return db_fetch_result($result, 0, "theme_path"); | |
979 | } else { | |
980 | return null; | |
981 | } | |
982 | } | |
be773442 AD |
983 | |
984 | function smart_date_time($timestamp) { | |
985 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
986 | return date("G:i", $timestamp); | |
f26450f1 | 987 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
988 | return date("M d, G:i", $timestamp); |
989 | } else { | |
b02111c2 | 990 | return date("Y/m/d G:i", $timestamp); |
be773442 AD |
991 | } |
992 | } | |
993 | ||
994 | function smart_date($timestamp) { | |
995 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
996 | return "Today"; | |
f26450f1 | 997 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
998 | return date("D m", $timestamp); |
999 | } else { | |
b02111c2 | 1000 | return date("Y/m/d", $timestamp); |
be773442 AD |
1001 | } |
1002 | } | |
a654a595 AD |
1003 | |
1004 | function sql_bool_to_string($s) { | |
1005 | if ($s == "t" || $s == "1") { | |
1006 | return "true"; | |
1007 | } else { | |
1008 | return "false"; | |
1009 | } | |
1010 | } | |
e3c99f3b AD |
1011 | |
1012 | function sql_bool_to_bool($s) { | |
1013 | if ($s == "t" || $s == "1") { | |
1014 | return true; | |
1015 | } else { | |
1016 | return false; | |
1017 | } | |
1018 | } | |
0ea4fb50 | 1019 | |
e3c99f3b | 1020 | |
0ea4fb50 AD |
1021 | function toggleEvenOdd($a) { |
1022 | if ($a == "even") | |
1023 | return "odd"; | |
1024 | else | |
1025 | return "even"; | |
1026 | } | |
6043fb7e AD |
1027 | |
1028 | function sanity_check($link) { | |
9cbca41f | 1029 | |
6043fb7e AD |
1030 | $error_code = 0; |
1031 | $result = db_query($link, "SELECT schema_version FROM ttrss_version"); | |
1032 | $schema_version = db_fetch_result($result, 0, "schema_version"); | |
1033 | ||
1034 | if ($schema_version != SCHEMA_VERSION) { | |
1035 | $error_code = 5; | |
1036 | } | |
1037 | ||
6043fb7e | 1038 | if ($error_code != 0) { |
9cbca41f | 1039 | print "<error error-code='$error_code'/>"; |
6043fb7e AD |
1040 | return false; |
1041 | } else { | |
1042 | return true; | |
9cbca41f | 1043 | } |
6043fb7e AD |
1044 | } |
1045 | ||
27981ca3 AD |
1046 | function file_is_locked($filename) { |
1047 | error_reporting(0); | |
1048 | $fp = fopen($filename, "r"); | |
1049 | error_reporting(DEFAULT_ERROR_LEVEL); | |
1050 | if ($fp) { | |
1051 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1052 | flock($fp, LOCK_UN); | |
1053 | fclose($fp); | |
1054 | return false; | |
1055 | } | |
1056 | fclose($fp); | |
1057 | return true; | |
1058 | } | |
1059 | return false; | |
1060 | } | |
1061 | ||
fcb4c0c9 AD |
1062 | function make_lockfile($filename) { |
1063 | $fp = fopen($filename, "w"); | |
1064 | ||
1065 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1066 | return $fp; | |
1067 | } else { | |
1068 | return false; | |
1069 | } | |
1070 | } | |
1071 | ||
894ebcf5 AD |
1072 | function sql_random_function() { |
1073 | if (DB_TYPE == "mysql") { | |
1074 | return "RAND()"; | |
1075 | } else { | |
1076 | return "RANDOM()"; | |
1077 | } | |
1078 | } | |
1079 | ||
23aa0d16 AD |
1080 | function catchup_feed($link, $feed, $cat_view) { |
1081 | if (preg_match("/^[0-9][0-9]*$/", $feed) != false && $feed >= 0) { | |
1082 | ||
1083 | if ($cat_view) { | |
1084 | ||
1085 | if ($feed > 0) { | |
1086 | $cat_qpart = "cat_id = '$feed'"; | |
1087 | } else { | |
1088 | $cat_qpart = "cat_id IS NULL"; | |
1089 | } | |
1090 | ||
1091 | $tmp_result = db_query($link, "SELECT id | |
1092 | FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " . | |
1093 | $_SESSION["uid"]); | |
1094 | ||
1095 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1096 | ||
1097 | $tmp_feed = $tmp_line["id"]; | |
1098 | ||
1099 | db_query($link, "UPDATE ttrss_user_entries | |
1100 | SET unread = false,last_read = NOW() | |
1101 | WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]); | |
1102 | } | |
1103 | ||
1104 | } else if ($feed > 0) { | |
1105 | ||
1106 | $tmp_result = db_query($link, "SELECT id | |
1107 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
1108 | ORDER BY cat_id,title"); | |
1109 | ||
1110 | $parent_ids = array(); | |
1111 | ||
1112 | if (db_num_rows($tmp_result) > 0) { | |
1113 | while ($p = db_fetch_assoc($tmp_result)) { | |
1114 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
1115 | } | |
1116 | ||
1117 | $children_qpart = implode(" OR ", $parent_ids); | |
1118 | ||
1119 | db_query($link, "UPDATE ttrss_user_entries | |
1120 | SET unread = false,last_read = NOW() | |
1121 | WHERE (feed_id = '$feed' OR $children_qpart) | |
1122 | AND owner_uid = " . $_SESSION["uid"]); | |
1123 | ||
1124 | } else { | |
1125 | db_query($link, "UPDATE ttrss_user_entries | |
1126 | SET unread = false,last_read = NOW() | |
1127 | WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
1128 | } | |
1129 | ||
1130 | } else if ($feed < 0 && $feed > -10) { // special, like starred | |
1131 | ||
1132 | if ($feed == -1) { | |
1133 | db_query($link, "UPDATE ttrss_user_entries | |
1134 | SET unread = false,last_read = NOW() | |
1135 | WHERE marked = true AND owner_uid = ".$_SESSION["uid"]); | |
1136 | } | |
1137 | ||
1138 | } else if ($feed < -10) { // label | |
1139 | ||
1140 | // TODO make this more efficient | |
1141 | ||
1142 | $label_id = -$feed - 11; | |
1143 | ||
1144 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
1145 | WHERE id = '$label_id'"); | |
1146 | ||
1147 | if ($tmp_result) { | |
1148 | $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp"); | |
1149 | ||
1150 | db_query($link, "BEGIN"); | |
1151 | ||
1152 | $tmp2_result = db_query($link, | |
1153 | "SELECT | |
1154 | int_id | |
1155 | FROM | |
1156 | ttrss_user_entries,ttrss_entries | |
1157 | WHERE | |
1158 | ref_id = id AND | |
1159 | $sql_exp AND | |
1160 | owner_uid = " . $_SESSION["uid"]); | |
1161 | ||
1162 | while ($tmp_line = db_fetch_assoc($tmp2_result)) { | |
1163 | db_query($link, "UPDATE | |
1164 | ttrss_user_entries | |
1165 | SET | |
1166 | unread = false, last_read = NOW() | |
1167 | WHERE | |
1168 | int_id = " . $tmp_line["int_id"]); | |
1169 | } | |
1170 | ||
1171 | db_query($link, "COMMIT"); | |
1172 | ||
1173 | /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries | |
1174 | SET unread = false,last_read = NOW() | |
1175 | WHERE $sql_exp | |
1176 | AND ref_id = id | |
1177 | AND owner_uid = ".$_SESSION["uid"]); */ | |
1178 | } | |
1179 | } | |
1180 | } else { // tag | |
1181 | db_query($link, "BEGIN"); | |
1182 | ||
1183 | $tag_name = db_escape_string($feed); | |
1184 | ||
1185 | $result = db_query($link, "SELECT post_int_id FROM ttrss_tags | |
1186 | WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]); | |
1187 | ||
1188 | while ($line = db_fetch_assoc($result)) { | |
1189 | db_query($link, "UPDATE ttrss_user_entries SET | |
1190 | unread = false, last_read = NOW() | |
1191 | WHERE int_id = " . $line["post_int_id"]); | |
1192 | } | |
1193 | db_query($link, "COMMIT"); | |
1194 | } | |
1195 | } | |
1196 | ||
1197 | function update_generic_feed($link, $feed, $cat_view) { | |
1198 | if ($cat_view) { | |
1199 | ||
1200 | if ($feed > 0) { | |
1201 | $cat_qpart = "cat_id = '$feed'"; | |
1202 | } else { | |
1203 | $cat_qpart = "cat_id IS NULL"; | |
1204 | } | |
1205 | ||
1206 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1207 | WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]); | |
1208 | ||
1209 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1210 | $feed_url = $tmp_line["feed_url"]; | |
1211 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1212 | } | |
1213 | ||
1214 | } else { | |
1215 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1216 | WHERE id = '$feed'"); | |
1217 | $feed_url = db_fetch_result($tmp_result, 0, "feed_url"); | |
1218 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1219 | } | |
1220 | } | |
a9cb1f83 AD |
1221 | |
1222 | function getAllCounters($link) { | |
1223 | getLabelCounters($link); | |
1224 | getFeedCounters($link); | |
1225 | getTagCounters($link); | |
1226 | getGlobalCounters($link); | |
1227 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
1228 | getCategoryCounters($link); | |
1229 | } | |
1230 | } | |
1231 | ||
1232 | function getCategoryCounters($link) { | |
1233 | $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id) | |
1234 | FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id | |
1235 | AND unread = true)) AS unread FROM ttrss_feeds | |
1236 | WHERE | |
1237 | owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id"); | |
1238 | ||
1239 | while ($line = db_fetch_assoc($result)) { | |
1240 | $line["cat_id"] = sprintf("%d", $line["cat_id"]); | |
1241 | print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"". | |
1242 | $line["unread"]."\"/>"; | |
1243 | } | |
1244 | } | |
1245 | ||
1246 | function getFeedUnread($link, $feed) { | |
1247 | $n_feed = sprintf("%d", $feed); | |
1248 | ||
1249 | if ($n_feed == -1) { | |
1250 | $match_part = "marked = true"; | |
1251 | } else if ($feed > 0) { | |
1252 | $match_part = "feed_id = '$n_feed'"; | |
1253 | } else if ($feed < -10) { | |
1254 | $label_id = -$feed - 11; | |
1255 | ||
1256 | $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE | |
1257 | id = '$label_id' AND owner_uid = " . $_SESSION["uid"]); | |
1258 | ||
1259 | $match_part = db_fetch_result($result, 0, "sql_exp"); | |
1260 | } | |
1261 | ||
1262 | if ($match_part) { | |
1263 | ||
1264 | $result = db_query($link, "SELECT count(int_id) AS unread | |
1265 | FROM ttrss_user_entries | |
1266 | WHERE unread = true AND $match_part AND owner_uid = " . $_SESSION["uid"]); | |
1267 | ||
1268 | } else { | |
1269 | ||
1270 | $result = db_query($link, "SELECT COUNT(post_int_id) AS unread | |
1271 | FROM ttrss_tags,ttrss_user_entries | |
1272 | WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND | |
1273 | ttrss_tags.owner_uid = " . $_SESSION["uid"]); | |
1274 | } | |
1275 | ||
1276 | $unread = db_fetch_result($result, 0, "unread"); | |
1277 | return $unread; | |
1278 | } | |
1279 | ||
1280 | /* FIXME this needs reworking */ | |
1281 | ||
1282 | function getGlobalUnread($link) { | |
1283 | $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries | |
1284 | WHERE unread = true AND | |
1285 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1286 | owner_uid = " . $_SESSION["uid"]); | |
1287 | $c_id = db_fetch_result($result, 0, "c_id"); | |
1288 | return $c_id; | |
1289 | } | |
1290 | ||
1291 | function getGlobalCounters($link, $global_unread = -1) { | |
1292 | if ($global_unread == -1) { | |
1293 | $global_unread = getGlobalUnread($link); | |
1294 | } | |
1295 | print "<counter type=\"global\" id='global-unread' counter='$global_unread'/>"; | |
1296 | } | |
1297 | ||
1298 | function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1299 | ||
1300 | if ($smart_mode) { | |
1301 | if (!$_SESSION["tctr_last_value"]) { | |
1302 | $_SESSION["tctr_last_value"] = array(); | |
1303 | } | |
1304 | } | |
1305 | ||
1306 | $old_counters = $_SESSION["tctr_last_value"]; | |
1307 | ||
1308 | $tctrs_modified = false; | |
1309 | ||
1310 | /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count | |
1311 | FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE | |
1312 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1313 | ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND | |
1314 | post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name | |
1315 | UNION | |
1316 | select tag_name,0 as count FROM ttrss_tags | |
1317 | WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */ | |
1318 | ||
1319 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) | |
1320 | FROM ttrss_user_entries WHERE int_id = post_int_id | |
1321 | AND unread = true)) AS count FROM ttrss_tags | |
1322 | WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name"); | |
1323 | ||
1324 | $tags = array(); | |
1325 | ||
1326 | while ($line = db_fetch_assoc($result)) { | |
1327 | $tags[$line["tag_name"]] += $line["count"]; | |
1328 | } | |
1329 | ||
1330 | foreach (array_keys($tags) as $tag) { | |
1331 | $unread = $tags[$tag]; | |
1332 | ||
1333 | $tag = htmlspecialchars($tag); | |
1334 | ||
1335 | if (!$smart_mode || $old_counters[$tag] != $unread) { | |
1336 | $old_counters[$tag] = $unread; | |
1337 | $tctrs_modified = true; | |
1338 | print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>"; | |
1339 | } | |
1340 | ||
1341 | } | |
1342 | ||
1343 | if ($smart_mode && $tctrs_modified) { | |
1344 | $_SESSION["tctr_last_value"] = $old_counters; | |
1345 | } | |
1346 | ||
1347 | } | |
1348 | ||
1349 | function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1350 | ||
1351 | if ($smart_mode) { | |
1352 | if (!$_SESSION["lctr_last_value"]) { | |
1353 | $_SESSION["lctr_last_value"] = array(); | |
1354 | } | |
1355 | } | |
1356 | ||
1357 | $old_counters = $_SESSION["lctr_last_value"]; | |
1358 | $lctrs_modified = false; | |
1359 | ||
1360 | $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries | |
1361 | WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1362 | unread = true AND owner_uid = ".$_SESSION["uid"]); | |
1363 | ||
1364 | $count = db_fetch_result($result, 0, "count"); | |
1365 | ||
1366 | print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>"; | |
1367 | ||
1368 | $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM | |
1369 | ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description"); | |
1370 | ||
1371 | while ($line = db_fetch_assoc($result)) { | |
1372 | ||
1373 | $id = -$line["id"] - 11; | |
1374 | ||
1375 | error_reporting (0); | |
1376 | ||
1377 | $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries | |
1378 | WHERE (" . $line["sql_exp"] . ") AND unread = true AND | |
1379 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1380 | owner_uid = ".$_SESSION["uid"]); | |
1381 | ||
1382 | $count = db_fetch_result($tmp_result, 0, "count"); | |
1383 | ||
1384 | if (!$smart_mode || $old_counters[$id] != $count) { | |
1385 | $old_counters[$id] = $count; | |
1386 | $lctrs_modified = true; | |
1387 | print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>"; | |
1388 | } | |
1389 | ||
1390 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1391 | } | |
1392 | ||
1393 | if ($smart_mode && $lctrs_modified) { | |
1394 | $_SESSION["lctr_last_value"] = $old_counters; | |
1395 | } | |
1396 | } | |
1397 | ||
1398 | /* function getFeedCounter($link, $id) { | |
1399 | ||
1400 | $result = db_query($link, "SELECT | |
1401 | count(id) as count,last_error | |
1402 | FROM ttrss_entries,ttrss_user_entries,ttrss_feeds | |
1403 | WHERE feed_id = '$id' AND unread = true | |
1404 | AND ttrss_user_entries.feed_id = ttrss_feeds.id | |
1405 | AND ttrss_user_entries.ref_id = ttrss_entries.id"); | |
1406 | ||
1407 | $count = db_fetch_result($result, 0, "count"); | |
1408 | $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error")); | |
1409 | ||
1410 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>"; | |
1411 | } */ | |
1412 | ||
1413 | function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1414 | ||
1415 | if ($smart_mode) { | |
1416 | if (!$_SESSION["fctr_last_value"]) { | |
1417 | $_SESSION["fctr_last_value"] = array(); | |
1418 | } | |
1419 | } | |
1420 | ||
1421 | $old_counters = $_SESSION["fctr_last_value"]; | |
1422 | ||
1423 | $result = db_query($link, "SELECT id,last_error,parent_feed, | |
fb1fb4ab | 1424 | SUBSTRING(last_updated,1,19) AS last_updated, |
a9cb1f83 AD |
1425 | (SELECT count(id) |
1426 | FROM ttrss_entries,ttrss_user_entries | |
1427 | WHERE feed_id = ttrss_feeds.id AND | |
1428 | ttrss_user_entries.ref_id = ttrss_entries.id | |
1429 | AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count | |
1430 | FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . " | |
1431 | AND parent_feed IS NULL"); | |
1432 | ||
1433 | $fctrs_modified = false; | |
1434 | ||
fb1fb4ab AD |
1435 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); |
1436 | ||
a9cb1f83 AD |
1437 | while ($line = db_fetch_assoc($result)) { |
1438 | ||
1439 | $id = $line["id"]; | |
1440 | $count = $line["count"]; | |
1441 | $last_error = htmlspecialchars($line["last_error"]); | |
fb1fb4ab AD |
1442 | |
1443 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
1444 | $last_updated = smart_date_time(strtotime($line["last_updated"])); | |
1445 | } else { | |
1446 | $last_updated = date($short_date, strtotime($line["last_updated"])); | |
1447 | } | |
1448 | ||
a9cb1f83 AD |
1449 | $has_img = is_file(ICONS_DIR . "/$id.ico"); |
1450 | ||
1451 | $tmp_result = db_query($link, | |
1452 | "SELECT id,COUNT(unread) AS unread | |
1453 | FROM ttrss_feeds LEFT JOIN ttrss_user_entries | |
1454 | ON (ttrss_feeds.id = ttrss_user_entries.feed_id) | |
1455 | WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id"); | |
1456 | ||
1457 | if (db_num_rows($tmp_result) > 0) { | |
1458 | while ($l = db_fetch_assoc($tmp_result)) { | |
1459 | $count += $l["unread"]; | |
1460 | } | |
1461 | } | |
1462 | ||
1463 | if (!$smart_mode || $old_counters[$id] != $count) { | |
1464 | $old_counters[$id] = $count; | |
1465 | $fctrs_modified = true; | |
1466 | ||
1467 | if ($last_error) { | |
1468 | $error_part = "error=\"$last_error\""; | |
1469 | } else { | |
1470 | $error_part = ""; | |
1471 | } | |
1472 | ||
1473 | if ($has_img) { | |
1474 | $has_img_part = "hi=\"$has_img\""; | |
1475 | } else { | |
1476 | $has_img_part = ""; | |
1477 | } | |
1478 | ||
fb1fb4ab | 1479 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>"; |
a9cb1f83 AD |
1480 | } |
1481 | } | |
1482 | ||
1483 | if ($smart_mode && $fctrs_modified) { | |
1484 | $_SESSION["fctr_last_value"] = $old_counters; | |
1485 | } | |
1486 | } | |
1487 | ||
1b758780 AD |
1488 | function get_script_dt_add() { |
1489 | if (strpos(VERSION, "99") === false) { | |
1490 | return VERSION; | |
1491 | } else { | |
1492 | return time(); | |
1493 | } | |
1494 | } | |
1495 | ||
6e7f8d26 AD |
1496 | function get_pgsql_version($link) { |
1497 | $result = db_query($link, "SELECT version() AS version"); | |
1498 | $version = split(" ", db_fetch_result($result, 0, "version")); | |
1499 | return $version[1]; | |
1500 | } | |
1501 | ||
af106b0e AD |
1502 | function print_error_xml($code, $add_msg = "") { |
1503 | global $ERRORS; | |
1504 | ||
1505 | $error_msg = $ERRORS[$code]; | |
1506 | ||
1507 | if ($add_msg) { | |
1508 | $error_msg = "$error_msg; $add_msg"; | |
1509 | } | |
1510 | ||
1511 | print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>"; | |
1512 | } | |
40d13c28 | 1513 | ?> |