functions.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. <?
  2. /* if ($_GET["debug"]) {
  3. define('DEFAULT_ERROR_LEVEL', E_ALL);
  4. } else {
  5. define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
  6. } */
  7. require_once 'config.php';
  8. require_once 'db-prefs.php';
  9. require_once 'compat.php';
  10. require_once 'errors.php';
  11. require_once 'magpierss/rss_utils.inc';
  12. define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
  13. function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
  14. $rows = -1;
  15. if (DB_TYPE == "pgsql") {
  16. /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
  17. marked = false AND feed_id = '$feed_id' AND
  18. (SELECT date_entered FROM ttrss_entries WHERE
  19. id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
  20. $pg_version = get_pgsql_version($link);
  21. if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
  22. $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
  23. ttrss_entries.id = ref_id AND
  24. marked = false AND
  25. feed_id = '$feed_id' AND
  26. ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
  27. } else {
  28. $result = db_query($link, "DELETE FROM ttrss_user_entries
  29. USING ttrss_entries
  30. WHERE ttrss_entries.id = ref_id AND
  31. marked = false AND
  32. feed_id = '$feed_id' AND
  33. ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
  34. }
  35. $rows = pg_affected_rows($result);
  36. } else {
  37. /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
  38. marked = false AND feed_id = '$feed_id' AND
  39. (SELECT date_entered FROM ttrss_entries WHERE
  40. id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
  41. $result = db_query($link, "DELETE FROM ttrss_user_entries
  42. USING ttrss_user_entries, ttrss_entries
  43. WHERE ttrss_entries.id = ref_id AND
  44. marked = false AND
  45. feed_id = '$feed_id' AND
  46. ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
  47. $rows = mysql_affected_rows($link);
  48. }
  49. if ($debug) {
  50. print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n";
  51. }
  52. }
  53. function global_purge_old_posts($link, $do_output = false, $limit = false) {
  54. $random_qpart = sql_random_function();
  55. if ($limit) {
  56. $limit_qpart = "LIMIT $limit";
  57. } else {
  58. $limit_qpart = "";
  59. }
  60. $result = db_query($link,
  61. "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
  62. ORDER BY $random_qpart $limit_qpart");
  63. while ($line = db_fetch_assoc($result)) {
  64. $feed_id = $line["id"];
  65. $purge_interval = $line["purge_interval"];
  66. $owner_uid = $line["owner_uid"];
  67. if ($purge_interval == 0) {
  68. $tmp_result = db_query($link,
  69. "SELECT value FROM ttrss_user_prefs WHERE
  70. pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
  71. if (db_num_rows($tmp_result) != 0) {
  72. $purge_interval = db_fetch_result($tmp_result, 0, "value");
  73. }
  74. }
  75. if ($do_output) {
  76. // print "Feed $feed_id: purge interval = $purge_interval\n";
  77. }
  78. if ($purge_interval > 0) {
  79. purge_feed($link, $feed_id, $purge_interval, $do_output);
  80. }
  81. }
  82. // purge orphaned posts in main content table
  83. db_query($link, "DELETE FROM ttrss_entries WHERE
  84. (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
  85. }
  86. function purge_old_posts($link) {
  87. $user_id = $_SESSION["uid"];
  88. $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
  89. WHERE owner_uid = '$user_id'");
  90. while ($line = db_fetch_assoc($result)) {
  91. $feed_id = $line["id"];
  92. $purge_interval = $line["purge_interval"];
  93. if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
  94. if ($purge_interval > 0) {
  95. purge_feed($link, $feed_id, $purge_interval);
  96. }
  97. }
  98. // purge orphaned posts in main content table
  99. db_query($link, "DELETE FROM ttrss_entries WHERE
  100. (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
  101. }
  102. function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
  103. if (WEB_DEMO_MODE) return;
  104. if (!$user_id) {
  105. $user_id = $_SESSION["uid"];
  106. purge_old_posts($link);
  107. }
  108. // db_query($link, "BEGIN");
  109. if (MAX_UPDATE_TIME > 0) {
  110. if (DB_TYPE == "mysql") {
  111. $q_order = "RAND()";
  112. } else {
  113. $q_order = "RANDOM()";
  114. }
  115. } else {
  116. $q_order = "last_updated DESC";
  117. }
  118. $result = db_query($link, "SELECT feed_url,id,
  119. SUBSTRING(last_updated,1,19) AS last_updated,
  120. update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
  121. ORDER BY $q_order");
  122. $upd_start = time();
  123. while ($line = db_fetch_assoc($result)) {
  124. $upd_intl = $line["update_interval"];
  125. if (!$upd_intl || $upd_intl == 0) {
  126. $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
  127. }
  128. if ($upd_intl < 0) {
  129. // Updates for this feed are disabled
  130. continue;
  131. }
  132. if ($fetch || (!$line["last_updated"] ||
  133. time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
  134. // print "<!-- feed: ".$line["feed_url"]." -->";
  135. update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
  136. $upd_elapsed = time() - $upd_start;
  137. if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) {
  138. return;
  139. }
  140. }
  141. }
  142. // db_query($link, "COMMIT");
  143. }
  144. function check_feed_favicon($feed_url, $feed, $link) {
  145. $feed_url = str_replace("http://", "", $feed_url);
  146. $feed_url = preg_replace("/\/.*$/", "", $feed_url);
  147. $icon_url = "http://$feed_url/favicon.ico";
  148. $icon_file = ICONS_DIR . "/$feed.ico";
  149. if (!file_exists($icon_file)) {
  150. error_reporting(0);
  151. $r = fopen($icon_url, "r");
  152. error_reporting (DEFAULT_ERROR_LEVEL);
  153. if ($r) {
  154. $tmpfname = tempnam(TMP_DIRECTORY, "ttrssicon");
  155. $t = fopen($tmpfname, "w");
  156. while (!feof($r)) {
  157. $buf = fread($r, 16384);
  158. fwrite($t, $buf);
  159. }
  160. fclose($r);
  161. fclose($t);
  162. error_reporting(0);
  163. if (!rename($tmpfname, $icon_file)) {
  164. unlink($tmpfname);
  165. }
  166. chmod($icon_file, 0644);
  167. error_reporting (DEFAULT_ERROR_LEVEL);
  168. }
  169. }
  170. }
  171. function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
  172. if (WEB_DEMO_MODE) return;
  173. if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) {
  174. return;
  175. }
  176. $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
  177. FROM ttrss_feeds WHERE id = '$feed'");
  178. $auth_login = db_fetch_result($result, 0, "auth_login");
  179. $auth_pass = db_fetch_result($result, 0, "auth_pass");
  180. $update_interval = db_fetch_result($result, 0, "update_interval");
  181. if ($update_interval < 0) { return; }
  182. $feed = db_escape_string($feed);
  183. $fetch_url = $feed_url;
  184. if ($auth_login && $auth_pass) {
  185. $url_parts = array();
  186. preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
  187. if ($url_parts[1] && $url_parts[2]) {
  188. $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
  189. }
  190. }
  191. error_reporting(0);
  192. $rss = fetch_rss($fetch_url);
  193. error_reporting (DEFAULT_ERROR_LEVEL);
  194. $feed = db_escape_string($feed);
  195. if ($rss) {
  196. // db_query($link, "BEGIN");
  197. $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
  198. FROM ttrss_feeds WHERE id = '$feed'");
  199. $registered_title = db_fetch_result($result, 0, "title");
  200. $orig_icon_url = db_fetch_result($result, 0, "icon_url");
  201. $orig_site_url = db_fetch_result($result, 0, "site_url");
  202. $owner_uid = db_fetch_result($result, 0, "owner_uid");
  203. if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) {
  204. check_feed_favicon($feed_url, $feed, $link);
  205. }
  206. if (!$registered_title || $registered_title == "[Unknown]") {
  207. $feed_title = db_escape_string($rss->channel["title"]);
  208. db_query($link, "UPDATE ttrss_feeds SET
  209. title = '$feed_title' WHERE id = '$feed'");
  210. }
  211. $site_url = $rss->channel["link"];
  212. // weird, weird Magpie
  213. if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
  214. if ($site_url && $orig_site_url != db_escape_string($site_url)) {
  215. db_query($link, "UPDATE ttrss_feeds SET
  216. site_url = '$site_url' WHERE id = '$feed'");
  217. }
  218. // print "I: " . $rss->channel["image"]["url"];
  219. $icon_url = $rss->image["url"];
  220. if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
  221. $icon_url = db_escape_string($icon_url);
  222. db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
  223. }
  224. $filters = array();
  225. $result = db_query($link, "SELECT reg_exp,
  226. ttrss_filter_types.name AS name,
  227. ttrss_filter_actions.name AS action
  228. FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
  229. owner_uid = $owner_uid AND
  230. ttrss_filter_types.id = filter_type AND
  231. ttrss_filter_actions.id = action_id AND
  232. (feed_id IS NULL OR feed_id = '$feed')");
  233. while ($line = db_fetch_assoc($result)) {
  234. if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
  235. $filter["reg_exp"] = $line["reg_exp"];
  236. $filter["action"] = $line["action"];
  237. array_push($filters[$line["name"]], $filter);
  238. }
  239. $iterator = $rss->items;
  240. if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
  241. if (!$iterator || !is_array($iterator)) $iterator = $rss;
  242. if (!is_array($iterator)) {
  243. db_query($link, "UPDATE ttrss_feeds
  244. SET last_error = 'Parse error: can\'t find any articles.'
  245. WHERE id = '$feed'");
  246. return; // WTF?
  247. }
  248. foreach ($iterator as $item) {
  249. $entry_guid = $item["id"];
  250. if (!$entry_guid) $entry_guid = $item["guid"];
  251. if (!$entry_guid) $entry_guid = $item["link"];
  252. if (!$entry_guid) continue;
  253. $entry_timestamp = "";
  254. $rss_2_date = $item['pubdate'];
  255. $rss_1_date = $item['dc']['date'];
  256. $atom_date = $item['issued'];
  257. if (!$atom_date) $atom_date = $item['updated'];
  258. if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
  259. if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
  260. if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
  261. if ($entry_timestamp == "") {
  262. $entry_timestamp = time();
  263. $no_orig_date = 'true';
  264. } else {
  265. $no_orig_date = 'false';
  266. }
  267. $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
  268. $entry_title = $item["title"];
  269. // strange Magpie workaround
  270. $entry_link = $item["link_"];
  271. if (!$entry_link) $entry_link = $item["link"];
  272. if (!$entry_title) continue;
  273. if (!$entry_link) continue;
  274. $entry_content = $item["content:escaped"];
  275. if (!$entry_content) $entry_content = $item["content:encoded"];
  276. if (!$entry_content) $entry_content = $item["content"];
  277. if (!$entry_content) $entry_content = $item["summary"];
  278. if (!$entry_content) $entry_content = $item["description"];
  279. // if (!$entry_content) continue;
  280. // WTF
  281. if (is_array($entry_content)) {
  282. $entry_content = $entry_content["encoded"];
  283. if (!$entry_content) $entry_content = $entry_content["escaped"];
  284. }
  285. // print_r($item);
  286. // print_r(htmlspecialchars($entry_content));
  287. // print "<br>";
  288. $entry_content_unescaped = $entry_content;
  289. $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
  290. $entry_comments = $item["comments"];
  291. $entry_author = db_escape_string($item['dc']['creator']);
  292. $entry_guid = db_escape_string($entry_guid);
  293. $result = db_query($link, "SELECT id FROM ttrss_entries
  294. WHERE guid = '$entry_guid'");
  295. $entry_content = db_escape_string($entry_content);
  296. $entry_title = db_escape_string($entry_title);
  297. $entry_link = db_escape_string($entry_link);
  298. $entry_comments = db_escape_string($entry_comments);
  299. $num_comments = db_escape_string($item["slash"]["comments"]);
  300. if (!$num_comments) $num_comments = 0;
  301. db_query($link, "BEGIN");
  302. if (db_num_rows($result) == 0) {
  303. // base post entry does not exist, create it
  304. $result = db_query($link,
  305. "INSERT INTO ttrss_entries
  306. (title,
  307. guid,
  308. link,
  309. updated,
  310. content,
  311. content_hash,
  312. no_orig_date,
  313. date_entered,
  314. comments,
  315. num_comments,
  316. author)
  317. VALUES
  318. ('$entry_title',
  319. '$entry_guid',
  320. '$entry_link',
  321. '$entry_timestamp_fmt',
  322. '$entry_content',
  323. '$content_hash',
  324. $no_orig_date,
  325. NOW(),
  326. '$entry_comments',
  327. '$num_comments',
  328. '$entry_author')");
  329. } else {
  330. // we keep encountering the entry in feeds, so we need to
  331. // update date_entered column so that we don't get horrible
  332. // dupes when the entry gets purged and reinserted again e.g.
  333. // in the case of SLOW SLOW OMG SLOW updating feeds
  334. $base_entry_id = db_fetch_result($result, 0, "id");
  335. db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
  336. WHERE id = '$base_entry_id'");
  337. }
  338. // now it should exist, if not - bad luck then
  339. $result = db_query($link, "SELECT
  340. id,content_hash,no_orig_date,title,
  341. substring(date_entered,1,19) as date_entered,
  342. substring(updated,1,19) as updated,
  343. num_comments
  344. FROM
  345. ttrss_entries
  346. WHERE guid = '$entry_guid'");
  347. if (db_num_rows($result) == 1) {
  348. // this will be used below in update handler
  349. $orig_content_hash = db_fetch_result($result, 0, "content_hash");
  350. $orig_title = db_fetch_result($result, 0, "title");
  351. $orig_num_comments = db_fetch_result($result, 0, "num_comments");
  352. $orig_date_entered = strtotime(db_fetch_result($result,
  353. 0, "date_entered"));
  354. $ref_id = db_fetch_result($result, 0, "id");
  355. // check for user post link to main table
  356. // do we allow duplicate posts with same GUID in different feeds?
  357. if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
  358. $dupcheck_qpart = "AND feed_id = '$feed'";
  359. } else {
  360. $dupcheck_qpart = "";
  361. }
  362. // error_reporting(0);
  363. $filter_name = get_filter_name($entry_title, $entry_content,
  364. $entry_link, $filters);
  365. if ($filter_name == "filter") {
  366. continue;
  367. }
  368. // error_reporting (DEFAULT_ERROR_LEVEL);
  369. $result = db_query($link,
  370. "SELECT ref_id FROM ttrss_user_entries WHERE
  371. ref_id = '$ref_id' AND owner_uid = '$owner_uid'
  372. $dupcheck_qpart");
  373. // okay it doesn't exist - create user entry
  374. if (db_num_rows($result) == 0) {
  375. if ($filter_name != 'catchup') {
  376. $unread = 'true';
  377. $last_read_qpart = 'NULL';
  378. } else {
  379. $unread = 'false';
  380. $last_read_qpart = 'NOW()';
  381. }
  382. $result = db_query($link,
  383. "INSERT INTO ttrss_user_entries
  384. (ref_id, owner_uid, feed_id, unread, last_read)
  385. VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
  386. $last_read_qpart)");
  387. }
  388. $post_needs_update = false;
  389. if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
  390. ($content_hash != $orig_content_hash)) {
  391. $post_needs_update = true;
  392. }
  393. if ($orig_title != $entry_title) {
  394. $post_needs_update = true;
  395. }
  396. if ($orig_num_comments != $num_comments) {
  397. $post_needs_update = true;
  398. }
  399. // this doesn't seem to be very reliable
  400. //
  401. // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
  402. // $post_needs_update = true;
  403. // }
  404. // if post needs update, update it and mark all user entries
  405. // linking to this post as updated
  406. if ($post_needs_update) {
  407. // print "<!-- post $orig_title needs update : $post_needs_update -->";
  408. db_query($link, "UPDATE ttrss_entries
  409. SET title = '$entry_title', content = '$entry_content',
  410. num_comments = '$num_comments'
  411. WHERE id = '$ref_id'");
  412. db_query($link, "UPDATE ttrss_user_entries
  413. SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
  414. }
  415. }
  416. db_query($link, "COMMIT");
  417. /* taaaags */
  418. // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
  419. $entry_tags = null;
  420. preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
  421. $entry_content_unescaped, $entry_tags);
  422. // print "<br>$entry_title : $entry_content_unescaped<br>";
  423. // print_r($entry_tags);
  424. // print "<br>";
  425. $entry_tags = $entry_tags[1];
  426. if (count($entry_tags) > 0) {
  427. db_query($link, "BEGIN");
  428. $result = db_query($link, "SELECT id,int_id
  429. FROM ttrss_entries,ttrss_user_entries
  430. WHERE guid = '$entry_guid'
  431. AND feed_id = '$feed' AND ref_id = id
  432. AND owner_uid = '$owner_uid'");
  433. if (db_num_rows($result) == 1) {
  434. $entry_id = db_fetch_result($result, 0, "id");
  435. $entry_int_id = db_fetch_result($result, 0, "int_id");
  436. foreach ($entry_tags as $tag) {
  437. $tag = db_escape_string(strtolower($tag));
  438. $tag = str_replace("+", " ", $tag);
  439. $tag = str_replace("technorati tag: ", "", $tag);
  440. $result = db_query($link, "SELECT id FROM ttrss_tags
  441. WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
  442. owner_uid = '$owner_uid' LIMIT 1");
  443. // print db_fetch_result($result, 0, "id");
  444. if ($result && db_num_rows($result) == 0) {
  445. // print "tagging $entry_id as $tag<br>";
  446. db_query($link, "INSERT INTO ttrss_tags
  447. (owner_uid,tag_name,post_int_id)
  448. VALUES ('$owner_uid','$tag', '$entry_int_id')");
  449. }
  450. }
  451. }
  452. db_query($link, "COMMIT");
  453. }
  454. }
  455. db_query($link, "UPDATE ttrss_feeds
  456. SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
  457. // db_query($link, "COMMIT");
  458. } else {
  459. $error_msg = db_escape_string(magpie_error());
  460. db_query($link,
  461. "UPDATE ttrss_feeds SET last_error = '$error_msg',
  462. last_updated = NOW() WHERE id = '$feed'");
  463. }
  464. }
  465. function print_select($id, $default, $values, $attributes = "") {
  466. print "<select id=\"$id\" $attributes>";
  467. foreach ($values as $v) {
  468. if ($v == $default)
  469. $sel = " selected";
  470. else
  471. $sel = "";
  472. print "<option$sel>$v</option>";
  473. }
  474. print "</select>";
  475. }
  476. function get_filter_name($title, $content, $link, $filters) {
  477. if ($filters["title"]) {
  478. foreach ($filters["title"] as $filter) {
  479. $reg_exp = $filter["reg_exp"];
  480. if (preg_match("/$reg_exp/i", $title)) {
  481. return $filter["action"];
  482. }
  483. }
  484. }
  485. if ($filters["content"]) {
  486. foreach ($filters["content"] as $filter) {
  487. $reg_exp = $filter["reg_exp"];
  488. if (preg_match("/$reg_exp/i", $content)) {
  489. return $filter["action"];
  490. }
  491. }
  492. }
  493. if ($filters["both"]) {
  494. foreach ($filters["both"] as $filter) {
  495. $reg_exp = $filter["reg_exp"];
  496. if (preg_match("/$reg_exp/i", $title) ||
  497. preg_match("/$reg_exp/i", $content)) {
  498. return $filter["action"];
  499. }
  500. }
  501. }
  502. if ($filters["link"]) {
  503. $reg_exp = $filter["reg_exp"];
  504. foreach ($filters["link"] as $filter) {
  505. $reg_exp = $filter["reg_exp"];
  506. if (preg_match("/$reg_exp/i", $link)) {
  507. return $filter["action"];
  508. }
  509. }
  510. }
  511. return false;
  512. }
  513. function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
  514. $rtl_content = false, $last_updated = false, $last_error = false) {
  515. if (file_exists($icon_file) && filesize($icon_file) > 0) {
  516. $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
  517. } else {
  518. $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
  519. }
  520. if ($rtl_content) {
  521. $rtl_tag = "dir=\"rtl\"";
  522. } else {
  523. $rtl_tag = "dir=\"ltr\"";
  524. }
  525. if ($last_error) {
  526. $link_title = "Error: $last_error ($last_updated)";
  527. } else if ($last_updated) {
  528. $link_title = "Updated: $last_updated";
  529. }
  530. $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
  531. print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
  532. if (get_pref($link, 'ENABLE_FEED_ICONS')) {
  533. print "$feed_icon";
  534. }
  535. print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
  536. if ($unread != 0) {
  537. $fctr_class = "";
  538. } else {
  539. $fctr_class = "class=\"invisible\"";
  540. }
  541. print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
  542. (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
  543. print "</li>";
  544. }
  545. function getmicrotime() {
  546. list($usec, $sec) = explode(" ",microtime());
  547. return ((float)$usec + (float)$sec);
  548. }
  549. function print_radio($id, $default, $values, $attributes = "") {
  550. foreach ($values as $v) {
  551. if ($v == $default)
  552. $sel = "checked";
  553. else
  554. $sel = "";
  555. if ($v == "Yes") {
  556. $sel .= " value=\"1\"";
  557. } else {
  558. $sel .= " value=\"0\"";
  559. }
  560. print "<input class=\"noborder\"
  561. type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
  562. }
  563. }
  564. function initialize_user_prefs($link, $uid) {
  565. $uid = db_escape_string($uid);
  566. db_query($link, "BEGIN");
  567. $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
  568. $u_result = db_query($link, "SELECT pref_name
  569. FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
  570. $active_prefs = array();
  571. while ($line = db_fetch_assoc($u_result)) {
  572. array_push($active_prefs, $line["pref_name"]);
  573. }
  574. while ($line = db_fetch_assoc($result)) {
  575. if (array_search($line["pref_name"], $active_prefs) === FALSE) {
  576. // print "adding " . $line["pref_name"] . "<br>";
  577. db_query($link, "INSERT INTO ttrss_user_prefs
  578. (owner_uid,pref_name,value) VALUES
  579. ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
  580. }
  581. }
  582. db_query($link, "COMMIT");
  583. }
  584. function authenticate_user($link, $login, $password) {
  585. $pwd_hash = 'SHA1:' . sha1($password);
  586. $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
  587. login = '$login' AND pwd_hash = '$pwd_hash'");
  588. if (db_num_rows($result) == 1) {
  589. $_SESSION["uid"] = db_fetch_result($result, 0, "id");
  590. $_SESSION["name"] = db_fetch_result($result, 0, "login");
  591. $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
  592. db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
  593. $_SESSION["uid"]);
  594. $user_theme = get_user_theme_path($link);
  595. $_SESSION["theme"] = $user_theme;
  596. $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
  597. initialize_user_prefs($link, $_SESSION["uid"]);
  598. return true;
  599. }
  600. return false;
  601. }
  602. function make_password($length = 8) {
  603. $password = "";
  604. $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
  605. $i = 0;
  606. while ($i < $length) {
  607. $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
  608. if (!strstr($password, $char)) {
  609. $password .= $char;
  610. $i++;
  611. }
  612. }
  613. return $password;
  614. }
  615. // this is called after user is created to initialize default feeds, labels
  616. // or whatever else
  617. // user preferences are checked on every login, not here
  618. function initialize_user($link, $uid) {
  619. db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
  620. values ('$uid','unread = true', 'Unread articles')");
  621. db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
  622. values ('$uid','last_read is null and unread = false', 'Updated articles')");
  623. db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
  624. values ('$uid', 'Tiny Tiny RSS: New Releases',
  625. 'http://tt-rss.spb.ru/releases.rss')");
  626. }
  627. function logout_user() {
  628. session_destroy();
  629. if (isset($_COOKIE[session_name()])) {
  630. setcookie(session_name(), '', time()-42000, '/');
  631. }
  632. }
  633. function get_script_urlpath() {
  634. return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
  635. }
  636. function get_login_redirect() {
  637. $server = $_SERVER["SERVER_NAME"];
  638. if (ENABLE_LOGIN_SSL) {
  639. $protocol = "https";
  640. } else {
  641. $protocol = "http";
  642. }
  643. $url_path = get_script_urlpath();
  644. $redirect_uri = "$protocol://$server$url_path/login.php";
  645. return $redirect_uri;
  646. }
  647. function validate_session($link) {
  648. if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) {
  649. if ($_SESSION["ip_address"]) {
  650. if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
  651. return false;
  652. }
  653. }
  654. }
  655. return true;
  656. }
  657. function basic_nosid_redirect_check() {
  658. if (!SINGLE_USER_MODE) {
  659. if (!$_COOKIE["ttrss_sid"]) {
  660. $redirect_uri = get_login_redirect();
  661. $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
  662. header("Location: $redirect_uri?rt=$return_to");
  663. exit;
  664. }
  665. }
  666. }
  667. function login_sequence($link) {
  668. if (!SINGLE_USER_MODE) {
  669. if (!validate_session($link)) {
  670. logout_user();
  671. $redirect_uri = get_login_redirect();
  672. $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
  673. header("Location: $redirect_uri?rt=$return_to");
  674. exit;
  675. }
  676. if (!USE_HTTP_AUTH) {
  677. if (!$_SESSION["uid"]) {
  678. $redirect_uri = get_login_redirect();
  679. $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
  680. header("Location: $redirect_uri?rt=$return_to");
  681. exit;
  682. }
  683. } else {
  684. if (!$_SESSION["uid"]) {
  685. if (!$_SERVER["PHP_AUTH_USER"]) {
  686. header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
  687. header('HTTP/1.0 401 Unauthorized');
  688. exit;
  689. } else {
  690. $auth_result = authenticate_user($link,
  691. $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
  692. if (!$auth_result) {
  693. header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
  694. header('HTTP/1.0 401 Unauthorized');
  695. exit;
  696. }
  697. }
  698. }
  699. }
  700. } else {
  701. $_SESSION["uid"] = 1;
  702. $_SESSION["name"] = "admin";
  703. initialize_user_prefs($link, 1);
  704. }
  705. }
  706. function truncate_string($str, $max_len) {
  707. if (mb_strlen($str, "utf-8") > $max_len - 3) {
  708. return mb_substr($str, 0, $max_len, "utf-8") . "...";
  709. } else {
  710. return $str;
  711. }
  712. }
  713. function get_user_theme_path($link) {
  714. $result = db_query($link, "SELECT theme_path
  715. FROM
  716. ttrss_themes,ttrss_users
  717. WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
  718. if (db_num_rows($result) != 0) {
  719. return db_fetch_result($result, 0, "theme_path");
  720. } else {
  721. return null;
  722. }
  723. }
  724. function smart_date_time($timestamp) {
  725. if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
  726. return date("G:i", $timestamp);
  727. } else if (date("Y", $timestamp) == date("Y")) {
  728. return date("M d, G:i", $timestamp);
  729. } else {
  730. return date("Y/m/d G:i", $timestamp);
  731. }
  732. }
  733. function smart_date($timestamp) {
  734. if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
  735. return "Today";
  736. } else if (date("Y", $timestamp) == date("Y")) {
  737. return date("D m", $timestamp);
  738. } else {
  739. return date("Y/m/d", $timestamp);
  740. }
  741. }
  742. function sql_bool_to_string($s) {
  743. if ($s == "t" || $s == "1") {
  744. return "true";
  745. } else {
  746. return "false";
  747. }
  748. }
  749. function sql_bool_to_bool($s) {
  750. if ($s == "t" || $s == "1") {
  751. return true;
  752. } else {
  753. return false;
  754. }
  755. }
  756. function toggleEvenOdd($a) {
  757. if ($a == "even")
  758. return "odd";
  759. else
  760. return "even";
  761. }
  762. function sanity_check($link) {
  763. $error_code = 0;
  764. $result = db_query($link, "SELECT schema_version FROM ttrss_version");
  765. $schema_version = db_fetch_result($result, 0, "schema_version");
  766. if ($schema_version != SCHEMA_VERSION) {
  767. $error_code = 5;
  768. }
  769. if ($error_code != 0) {
  770. print "<error error-code='$error_code'/>";
  771. return false;
  772. } else {
  773. return true;
  774. }
  775. }
  776. function file_is_locked($filename) {
  777. error_reporting(0);
  778. $fp = fopen($filename, "r");
  779. error_reporting(DEFAULT_ERROR_LEVEL);
  780. if ($fp) {
  781. if (flock($fp, LOCK_EX | LOCK_NB)) {
  782. flock($fp, LOCK_UN);
  783. fclose($fp);
  784. return false;
  785. }
  786. fclose($fp);
  787. return true;
  788. }
  789. return false;
  790. }
  791. function make_lockfile($filename) {
  792. $fp = fopen($filename, "w");
  793. if (flock($fp, LOCK_EX | LOCK_NB)) {
  794. return $fp;
  795. } else {
  796. return false;
  797. }
  798. }
  799. function sql_random_function() {
  800. if (DB_TYPE == "mysql") {
  801. return "RAND()";
  802. } else {
  803. return "RANDOM()";
  804. }
  805. }
  806. function catchup_feed($link, $feed, $cat_view) {
  807. if (preg_match("/^[0-9][0-9]*$/", $feed) != false && $feed >= 0) {
  808. if ($cat_view) {
  809. if ($feed > 0) {
  810. $cat_qpart = "cat_id = '$feed'";
  811. } else {
  812. $cat_qpart = "cat_id IS NULL";
  813. }
  814. $tmp_result = db_query($link, "SELECT id
  815. FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
  816. $_SESSION["uid"]);
  817. while ($tmp_line = db_fetch_assoc($tmp_result)) {
  818. $tmp_feed = $tmp_line["id"];
  819. db_query($link, "UPDATE ttrss_user_entries
  820. SET unread = false,last_read = NOW()
  821. WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
  822. }
  823. } else if ($feed > 0) {
  824. $tmp_result = db_query($link, "SELECT id
  825. FROM ttrss_feeds WHERE parent_feed = '$feed'
  826. ORDER BY cat_id,title");
  827. $parent_ids = array();
  828. if (db_num_rows($tmp_result) > 0) {
  829. while ($p = db_fetch_assoc($tmp_result)) {
  830. array_push($parent_ids, "feed_id = " . $p["id"]);
  831. }
  832. $children_qpart = implode(" OR ", $parent_ids);
  833. db_query($link, "UPDATE ttrss_user_entries
  834. SET unread = false,last_read = NOW()
  835. WHERE (feed_id = '$feed' OR $children_qpart)
  836. AND owner_uid = " . $_SESSION["uid"]);
  837. } else {
  838. db_query($link, "UPDATE ttrss_user_entries
  839. SET unread = false,last_read = NOW()
  840. WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
  841. }
  842. } else if ($feed < 0 && $feed > -10) { // special, like starred
  843. if ($feed == -1) {
  844. db_query($link, "UPDATE ttrss_user_entries
  845. SET unread = false,last_read = NOW()
  846. WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
  847. }
  848. } else if ($feed < -10) { // label
  849. // TODO make this more efficient
  850. $label_id = -$feed - 11;
  851. $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
  852. WHERE id = '$label_id'");
  853. if ($tmp_result) {
  854. $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
  855. db_query($link, "BEGIN");
  856. $tmp2_result = db_query($link,
  857. "SELECT
  858. int_id
  859. FROM
  860. ttrss_user_entries,ttrss_entries
  861. WHERE
  862. ref_id = id AND
  863. $sql_exp AND
  864. owner_uid = " . $_SESSION["uid"]);
  865. while ($tmp_line = db_fetch_assoc($tmp2_result)) {
  866. db_query($link, "UPDATE
  867. ttrss_user_entries
  868. SET
  869. unread = false, last_read = NOW()
  870. WHERE
  871. int_id = " . $tmp_line["int_id"]);
  872. }
  873. db_query($link, "COMMIT");
  874. /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
  875. SET unread = false,last_read = NOW()
  876. WHERE $sql_exp
  877. AND ref_id = id
  878. AND owner_uid = ".$_SESSION["uid"]); */
  879. }
  880. }
  881. } else { // tag
  882. db_query($link, "BEGIN");
  883. $tag_name = db_escape_string($feed);
  884. $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
  885. WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
  886. while ($line = db_fetch_assoc($result)) {
  887. db_query($link, "UPDATE ttrss_user_entries SET
  888. unread = false, last_read = NOW()
  889. WHERE int_id = " . $line["post_int_id"]);
  890. }
  891. db_query($link, "COMMIT");
  892. }
  893. }
  894. function update_generic_feed($link, $feed, $cat_view) {
  895. if ($cat_view) {
  896. if ($feed > 0) {
  897. $cat_qpart = "cat_id = '$feed'";
  898. } else {
  899. $cat_qpart = "cat_id IS NULL";
  900. }
  901. $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
  902. WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
  903. while ($tmp_line = db_fetch_assoc($tmp_result)) {
  904. $feed_url = $tmp_line["feed_url"];
  905. update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
  906. }
  907. } else {
  908. $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
  909. WHERE id = '$feed'");
  910. $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
  911. update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
  912. }
  913. }
  914. function getAllCounters($link) {
  915. getLabelCounters($link);
  916. getFeedCounters($link);
  917. getTagCounters($link);
  918. getGlobalCounters($link);
  919. if (get_pref($link, 'ENABLE_FEED_CATS')) {
  920. getCategoryCounters($link);
  921. }
  922. }
  923. function getCategoryCounters($link) {
  924. $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
  925. FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
  926. AND unread = true)) AS unread FROM ttrss_feeds
  927. WHERE
  928. owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
  929. while ($line = db_fetch_assoc($result)) {
  930. $line["cat_id"] = sprintf("%d", $line["cat_id"]);
  931. print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
  932. $line["unread"]."\"/>";
  933. }
  934. }
  935. function getFeedUnread($link, $feed) {
  936. $n_feed = sprintf("%d", $feed);
  937. if ($n_feed == -1) {
  938. $match_part = "marked = true";
  939. } else if ($feed > 0) {
  940. $match_part = "feed_id = '$n_feed'";
  941. } else if ($feed < -10) {
  942. $label_id = -$feed - 11;
  943. $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
  944. id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
  945. $match_part = db_fetch_result($result, 0, "sql_exp");
  946. }
  947. if ($match_part) {
  948. $result = db_query($link, "SELECT count(int_id) AS unread
  949. FROM ttrss_user_entries
  950. WHERE unread = true AND $match_part AND owner_uid = " . $_SESSION["uid"]);
  951. } else {
  952. $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
  953. FROM ttrss_tags,ttrss_user_entries
  954. WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
  955. ttrss_tags.owner_uid = " . $_SESSION["uid"]);
  956. }
  957. $unread = db_fetch_result($result, 0, "unread");
  958. return $unread;
  959. }
  960. /* FIXME this needs reworking */
  961. function getGlobalUnread($link) {
  962. $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
  963. WHERE unread = true AND
  964. ttrss_user_entries.ref_id = ttrss_entries.id AND
  965. owner_uid = " . $_SESSION["uid"]);
  966. $c_id = db_fetch_result($result, 0, "c_id");
  967. return $c_id;
  968. }
  969. function getGlobalCounters($link, $global_unread = -1) {
  970. if ($global_unread == -1) {
  971. $global_unread = getGlobalUnread($link);
  972. }
  973. print "<counter type=\"global\" id='global-unread' counter='$global_unread'/>";
  974. }
  975. function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
  976. if ($smart_mode) {
  977. if (!$_SESSION["tctr_last_value"]) {
  978. $_SESSION["tctr_last_value"] = array();
  979. }
  980. }
  981. $old_counters = $_SESSION["tctr_last_value"];
  982. $tctrs_modified = false;
  983. /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
  984. FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
  985. ttrss_user_entries.ref_id = ttrss_entries.id AND
  986. ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
  987. post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
  988. UNION
  989. select tag_name,0 as count FROM ttrss_tags
  990. WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
  991. $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
  992. FROM ttrss_user_entries WHERE int_id = post_int_id
  993. AND unread = true)) AS count FROM ttrss_tags
  994. WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
  995. $tags = array();
  996. while ($line = db_fetch_assoc($result)) {
  997. $tags[$line["tag_name"]] += $line["count"];
  998. }
  999. foreach (array_keys($tags) as $tag) {
  1000. $unread = $tags[$tag];
  1001. $tag = htmlspecialchars($tag);
  1002. if (!$smart_mode || $old_counters[$tag] != $unread) {
  1003. $old_counters[$tag] = $unread;
  1004. $tctrs_modified = true;
  1005. print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
  1006. }
  1007. }
  1008. if ($smart_mode && $tctrs_modified) {
  1009. $_SESSION["tctr_last_value"] = $old_counters;
  1010. }
  1011. }
  1012. function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
  1013. if ($smart_mode) {
  1014. if (!$_SESSION["lctr_last_value"]) {
  1015. $_SESSION["lctr_last_value"] = array();
  1016. }
  1017. }
  1018. $old_counters = $_SESSION["lctr_last_value"];
  1019. $lctrs_modified = false;
  1020. $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
  1021. WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
  1022. unread = true AND owner_uid = ".$_SESSION["uid"]);
  1023. $count = db_fetch_result($result, 0, "count");
  1024. print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
  1025. $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
  1026. ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
  1027. while ($line = db_fetch_assoc($result)) {
  1028. $id = -$line["id"] - 11;
  1029. error_reporting (0);
  1030. $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries
  1031. WHERE (" . $line["sql_exp"] . ") AND unread = true AND
  1032. ttrss_user_entries.ref_id = ttrss_entries.id AND
  1033. owner_uid = ".$_SESSION["uid"]);
  1034. $count = db_fetch_result($tmp_result, 0, "count");
  1035. if (!$smart_mode || $old_counters[$id] != $count) {
  1036. $old_counters[$id] = $count;
  1037. $lctrs_modified = true;
  1038. print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
  1039. }
  1040. error_reporting (DEFAULT_ERROR_LEVEL);
  1041. }
  1042. if ($smart_mode && $lctrs_modified) {
  1043. $_SESSION["lctr_last_value"] = $old_counters;
  1044. }
  1045. }
  1046. /* function getFeedCounter($link, $id) {
  1047. $result = db_query($link, "SELECT
  1048. count(id) as count,last_error
  1049. FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
  1050. WHERE feed_id = '$id' AND unread = true
  1051. AND ttrss_user_entries.feed_id = ttrss_feeds.id
  1052. AND ttrss_user_entries.ref_id = ttrss_entries.id");
  1053. $count = db_fetch_result($result, 0, "count");
  1054. $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
  1055. print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
  1056. } */
  1057. function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
  1058. if ($smart_mode) {
  1059. if (!$_SESSION["fctr_last_value"]) {
  1060. $_SESSION["fctr_last_value"] = array();
  1061. }
  1062. }
  1063. $old_counters = $_SESSION["fctr_last_value"];
  1064. $result = db_query($link, "SELECT id,last_error,parent_feed,
  1065. SUBSTRING(last_updated,1,19) AS last_updated,
  1066. (SELECT count(id)
  1067. FROM ttrss_entries,ttrss_user_entries
  1068. WHERE feed_id = ttrss_feeds.id AND
  1069. ttrss_user_entries.ref_id = ttrss_entries.id
  1070. AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
  1071. FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
  1072. AND parent_feed IS NULL");
  1073. $fctrs_modified = false;
  1074. $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
  1075. while ($line = db_fetch_assoc($result)) {
  1076. $id = $line["id"];
  1077. $count = $line["count"];
  1078. $last_error = htmlspecialchars($line["last_error"]);
  1079. if (get_pref($link, 'HEADLINES_SMART_DATE')) {
  1080. $last_updated = smart_date_time(strtotime($line["last_updated"]));
  1081. } else {
  1082. $last_updated = date($short_date, strtotime($line["last_updated"]));
  1083. }
  1084. $has_img = is_file(ICONS_DIR . "/$id.ico");
  1085. $tmp_result = db_query($link,
  1086. "SELECT id,COUNT(unread) AS unread
  1087. FROM ttrss_feeds LEFT JOIN ttrss_user_entries
  1088. ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
  1089. WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
  1090. if (db_num_rows($tmp_result) > 0) {
  1091. while ($l = db_fetch_assoc($tmp_result)) {
  1092. $count += $l["unread"];
  1093. }
  1094. }
  1095. if (!$smart_mode || $old_counters[$id] != $count) {
  1096. $old_counters[$id] = $count;
  1097. $fctrs_modified = true;
  1098. if ($last_error) {
  1099. $error_part = "error=\"$last_error\"";
  1100. } else {
  1101. $error_part = "";
  1102. }
  1103. if ($has_img) {
  1104. $has_img_part = "hi=\"$has_img\"";
  1105. } else {
  1106. $has_img_part = "";
  1107. }
  1108. print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
  1109. }
  1110. }
  1111. if ($smart_mode && $fctrs_modified) {
  1112. $_SESSION["fctr_last_value"] = $old_counters;
  1113. }
  1114. }
  1115. function get_script_dt_add() {
  1116. if (strpos(VERSION, "99") === false) {
  1117. return VERSION;
  1118. } else {
  1119. return time();
  1120. }
  1121. }
  1122. function get_pgsql_version($link) {
  1123. $result = db_query($link, "SELECT version() AS version");
  1124. $version = split(" ", db_fetch_result($result, 0, "version"));
  1125. return $version[1];
  1126. }
  1127. function print_error_xml($code, $add_msg = "") {
  1128. global $ERRORS;
  1129. $error_msg = $ERRORS[$code];
  1130. if ($add_msg) {
  1131. $error_msg = "$error_msg; $add_msg";
  1132. }
  1133. print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
  1134. }
  1135. ?>