functions.php 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353
  1. <?php
  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 'version.php';
  12. if (RSS_BACKEND_TYPE == "magpie") {
  13. require_once 'magpierss/rss_utils.inc';
  14. } else if (RSS_BACKEND_TYPE == "simplepie") {
  15. require_once 'simplepie/simplepie.inc';
  16. }
  17. define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
  18. function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
  19. $rows = -1;
  20. if (DB_TYPE == "pgsql") {
  21. /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
  22. marked = false AND feed_id = '$feed_id' AND
  23. (SELECT date_entered FROM ttrss_entries WHERE
  24. id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
  25. $pg_version = get_pgsql_version($link);
  26. if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
  27. $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
  28. ttrss_entries.id = ref_id AND
  29. marked = false AND
  30. feed_id = '$feed_id' AND
  31. ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
  32. } else {
  33. $result = db_query($link, "DELETE FROM ttrss_user_entries
  34. USING ttrss_entries
  35. WHERE ttrss_entries.id = ref_id AND
  36. marked = false AND
  37. feed_id = '$feed_id' AND
  38. ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
  39. }
  40. $rows = pg_affected_rows($result);
  41. } else {
  42. /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
  43. marked = false AND feed_id = '$feed_id' AND
  44. (SELECT date_entered FROM ttrss_entries WHERE
  45. id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
  46. $result = db_query($link, "DELETE FROM ttrss_user_entries
  47. USING ttrss_user_entries, ttrss_entries
  48. WHERE ttrss_entries.id = ref_id AND
  49. marked = false AND
  50. feed_id = '$feed_id' AND
  51. ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
  52. $rows = mysql_affected_rows($link);
  53. }
  54. if ($debug) {
  55. print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n";
  56. }
  57. }
  58. function global_purge_old_posts($link, $do_output = false, $limit = false) {
  59. $random_qpart = sql_random_function();
  60. if ($limit) {
  61. $limit_qpart = "LIMIT $limit";
  62. } else {
  63. $limit_qpart = "";
  64. }
  65. $result = db_query($link,
  66. "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
  67. ORDER BY $random_qpart $limit_qpart");
  68. while ($line = db_fetch_assoc($result)) {
  69. $feed_id = $line["id"];
  70. $purge_interval = $line["purge_interval"];
  71. $owner_uid = $line["owner_uid"];
  72. if ($purge_interval == 0) {
  73. $tmp_result = db_query($link,
  74. "SELECT value FROM ttrss_user_prefs WHERE
  75. pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
  76. if (db_num_rows($tmp_result) != 0) {
  77. $purge_interval = db_fetch_result($tmp_result, 0, "value");
  78. }
  79. }
  80. if ($do_output) {
  81. // print "Feed $feed_id: purge interval = $purge_interval\n";
  82. }
  83. if ($purge_interval > 0) {
  84. purge_feed($link, $feed_id, $purge_interval, $do_output);
  85. }
  86. }
  87. // purge orphaned posts in main content table
  88. db_query($link, "DELETE FROM ttrss_entries WHERE
  89. (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
  90. }
  91. function purge_old_posts($link) {
  92. $user_id = $_SESSION["uid"];
  93. $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
  94. WHERE owner_uid = '$user_id'");
  95. while ($line = db_fetch_assoc($result)) {
  96. $feed_id = $line["id"];
  97. $purge_interval = $line["purge_interval"];
  98. if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
  99. if ($purge_interval > 0) {
  100. purge_feed($link, $feed_id, $purge_interval);
  101. }
  102. }
  103. // purge orphaned posts in main content table
  104. db_query($link, "DELETE FROM ttrss_entries WHERE
  105. (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
  106. }
  107. function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
  108. if (WEB_DEMO_MODE) return;
  109. if (!$user_id) {
  110. $user_id = $_SESSION["uid"];
  111. purge_old_posts($link);
  112. }
  113. // db_query($link, "BEGIN");
  114. if (MAX_UPDATE_TIME > 0) {
  115. if (DB_TYPE == "mysql") {
  116. $q_order = "RAND()";
  117. } else {
  118. $q_order = "RANDOM()";
  119. }
  120. } else {
  121. $q_order = "last_updated DESC";
  122. }
  123. $result = db_query($link, "SELECT feed_url,id,
  124. SUBSTRING(last_updated,1,19) AS last_updated,
  125. update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
  126. ORDER BY $q_order");
  127. $upd_start = time();
  128. while ($line = db_fetch_assoc($result)) {
  129. $upd_intl = $line["update_interval"];
  130. if (!$upd_intl || $upd_intl == 0) {
  131. $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
  132. }
  133. if ($upd_intl < 0) {
  134. // Updates for this feed are disabled
  135. continue;
  136. }
  137. if ($fetch || (!$line["last_updated"] ||
  138. time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
  139. // print "<!-- feed: ".$line["feed_url"]." -->";
  140. update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
  141. $upd_elapsed = time() - $upd_start;
  142. if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) {
  143. return;
  144. }
  145. }
  146. }
  147. // db_query($link, "COMMIT");
  148. }
  149. function check_feed_favicon($feed_url, $feed, $link) {
  150. $feed_url = str_replace("http://", "", $feed_url);
  151. $feed_url = preg_replace("/\/.*$/", "", $feed_url);
  152. $icon_url = "http://$feed_url/favicon.ico";
  153. $icon_file = ICONS_DIR . "/$feed.ico";
  154. if (!file_exists($icon_file)) {
  155. error_reporting(0);
  156. $r = fopen($icon_url, "r");
  157. error_reporting (DEFAULT_ERROR_LEVEL);
  158. if ($r) {
  159. $tmpfname = tempnam(TMP_DIRECTORY, "ttrssicon");
  160. $t = fopen($tmpfname, "w");
  161. while (!feof($r)) {
  162. $buf = fread($r, 16384);
  163. fwrite($t, $buf);
  164. }
  165. fclose($r);
  166. fclose($t);
  167. error_reporting(0);
  168. if (!rename($tmpfname, $icon_file)) {
  169. unlink($tmpfname);
  170. }
  171. chmod($icon_file, 0644);
  172. error_reporting (DEFAULT_ERROR_LEVEL);
  173. }
  174. }
  175. }
  176. function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
  177. if (WEB_DEMO_MODE) return;
  178. if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) {
  179. return;
  180. }
  181. $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
  182. FROM ttrss_feeds WHERE id = '$feed'");
  183. $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login"));
  184. $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass"));
  185. $update_interval = db_fetch_result($result, 0, "update_interval");
  186. if ($update_interval < 0) { return; }
  187. $feed = db_escape_string($feed);
  188. $fetch_url = $feed_url;
  189. if ($auth_login && $auth_pass) {
  190. $url_parts = array();
  191. preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
  192. if ($url_parts[1] && $url_parts[2]) {
  193. $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
  194. }
  195. }
  196. if (RSS_BACKEND_TYPE == "magpie") {
  197. error_reporting(0);
  198. $rss = fetch_rss($fetch_url);
  199. error_reporting (DEFAULT_ERROR_LEVEL);
  200. } else if (RSS_BACKEND_TYPE == "simplepie") {
  201. if (!file_exists(SIMPLEPIE_CACHE_DIR)) {
  202. mkdir(SIMPLEPIE_CACHE_DIR);
  203. }
  204. $rss = new SimplePie();
  205. $rss->feed_url($fetch_url);
  206. $rss->cache_location(SIMPLEPIE_CACHE_DIR);
  207. $rss->init();
  208. }
  209. $feed = db_escape_string($feed);
  210. $rss_check = $rss;
  211. if (RSS_BACKEND_TYPE == "simplepie") {
  212. $rss_check = $rss->data;
  213. }
  214. if ($rss_check) {
  215. // db_query($link, "BEGIN");
  216. $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
  217. FROM ttrss_feeds WHERE id = '$feed'");
  218. $registered_title = db_fetch_result($result, 0, "title");
  219. $orig_icon_url = db_fetch_result($result, 0, "icon_url");
  220. $orig_site_url = db_fetch_result($result, 0, "site_url");
  221. $owner_uid = db_fetch_result($result, 0, "owner_uid");
  222. if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
  223. check_feed_favicon($feed_url, $feed, $link);
  224. }
  225. if (!$registered_title || $registered_title == "[Unknown]") {
  226. if (RSS_BACKEND_TYPE == "magpie") {
  227. $feed_title = db_escape_string($rss->channel["title"]);
  228. } else {
  229. $feed_title = $rss->get_feed_title();
  230. }
  231. db_query($link, "UPDATE ttrss_feeds SET
  232. title = '$feed_title' WHERE id = '$feed'");
  233. }
  234. if (RSS_BACKEND_TYPE == "magpie") {
  235. $site_url = $rss->channel["link"];
  236. // weird, weird Magpie
  237. if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
  238. } else {
  239. $site_url = $rss->get_feed_link();
  240. }
  241. if ($site_url && $orig_site_url != db_escape_string($site_url)) {
  242. db_query($link, "UPDATE ttrss_feeds SET
  243. site_url = '$site_url' WHERE id = '$feed'");
  244. }
  245. // print "I: " . $rss->channel["image"]["url"];
  246. if (RSS_BACKEND_TYPE == "magpie") {
  247. $icon_url = $rss->image["url"];
  248. } else {
  249. $icon_url = $rss->get_image_url(); # FIXME
  250. }
  251. if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
  252. $icon_url = db_escape_string($icon_url);
  253. db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
  254. }
  255. $filters = array();
  256. $result = db_query($link, "SELECT reg_exp,
  257. ttrss_filter_types.name AS name,
  258. ttrss_filter_actions.name AS action
  259. FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
  260. owner_uid = $owner_uid AND
  261. ttrss_filter_types.id = filter_type AND
  262. ttrss_filter_actions.id = action_id AND
  263. (feed_id IS NULL OR feed_id = '$feed')");
  264. while ($line = db_fetch_assoc($result)) {
  265. if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
  266. $filter["reg_exp"] = $line["reg_exp"];
  267. $filter["action"] = $line["action"];
  268. array_push($filters[$line["name"]], $filter);
  269. }
  270. if (RSS_BACKEND_TYPE == "magpie") {
  271. $iterator = $rss->items;
  272. if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
  273. if (!$iterator || !is_array($iterator)) $iterator = $rss;
  274. } else {
  275. $iterator = $rss->get_items();
  276. }
  277. if (!is_array($iterator)) {
  278. /* db_query($link, "UPDATE ttrss_feeds
  279. SET last_error = 'Parse error: can\'t find any articles.'
  280. WHERE id = '$feed'"); */
  281. return; // WTF?
  282. }
  283. foreach ($iterator as $item) {
  284. if (RSS_BACKEND_TYPE == "magpie") {
  285. $entry_guid = $item["id"];
  286. if (!$entry_guid) $entry_guid = $item["guid"];
  287. if (!$entry_guid) $entry_guid = $item["link"];
  288. if (!$entry_guid) continue;
  289. $entry_timestamp = "";
  290. $rss_2_date = $item['pubdate'];
  291. $rss_1_date = $item['dc']['date'];
  292. $atom_date = $item['issued'];
  293. if (!$atom_date) $atom_date = $item['updated'];
  294. if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
  295. if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
  296. if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
  297. if ($entry_timestamp == "") {
  298. $entry_timestamp = time();
  299. $no_orig_date = 'true';
  300. } else {
  301. $no_orig_date = 'false';
  302. }
  303. $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
  304. $entry_title = $item["title"];
  305. // strange Magpie workaround
  306. $entry_link = $item["link_"];
  307. if (!$entry_link) $entry_link = $item["link"];
  308. if (!$entry_title) continue;
  309. if (!$entry_link) continue;
  310. $entry_content = $item["content:escaped"];
  311. if (!$entry_content) $entry_content = $item["content:encoded"];
  312. if (!$entry_content) $entry_content = $item["content"];
  313. if (!$entry_content) $entry_content = $item["atom_content"];
  314. if (!$entry_content) $entry_content = $item["summary"];
  315. if (!$entry_content) $entry_content = $item["description"];
  316. // if (!$entry_content) continue;
  317. // WTF
  318. if (is_array($entry_content)) {
  319. $entry_content = $entry_content["encoded"];
  320. if (!$entry_content) $entry_content = $entry_content["escaped"];
  321. }
  322. // print_r($item);
  323. // print_r(htmlspecialchars($entry_content));
  324. // print "<br>";
  325. $entry_content_unescaped = $entry_content;
  326. $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
  327. $entry_comments = $item["comments"];
  328. $entry_author = db_escape_string($item['dc']['creator']);
  329. $entry_guid = db_escape_string($entry_guid);
  330. $result = db_query($link, "SELECT id FROM ttrss_entries
  331. WHERE guid = '$entry_guid'");
  332. $entry_content = db_escape_string($entry_content);
  333. $entry_title = db_escape_string($entry_title);
  334. $entry_link = db_escape_string($entry_link);
  335. $entry_comments = db_escape_string($entry_comments);
  336. $num_comments = db_escape_string($item["slash"]["comments"]);
  337. if (!$num_comments) $num_comments = 0;
  338. } else if (RSS_BACKEND_TYPE == "simplepie") {
  339. $entry_guid = $item->get_id();
  340. if (!$entry_guid) {
  341. $entry_guid = $item->get_permalink();
  342. }
  343. if (!$entry_guid) continue;
  344. $entry_timestamp = $item->get_date("U");
  345. if ($entry_timestamp == "") {
  346. $entry_timestamp = time();
  347. $no_orig_date = 'true';
  348. } else {
  349. $no_orig_date = 'false';
  350. }
  351. $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
  352. $entry_title = $item->get_title();
  353. $entry_link = $item->get_permalink();
  354. if (!$entry_title) continue;
  355. if (!$entry_link) continue;
  356. $entry_content = $item->get_description();
  357. // print_r(htmlspecialchars($entry_content));
  358. // print "<br>";
  359. $entry_content_unescaped = $entry_content;
  360. $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
  361. $entry_comments = ""; # FIXME
  362. $entry_author = $item->get_author(0);
  363. $entry_author = db_escape_string($entry_author->name);
  364. $entry_guid = db_escape_string($entry_guid);
  365. $result = db_query($link, "SELECT id FROM ttrss_entries
  366. WHERE guid = '$entry_guid'");
  367. $entry_content = db_escape_string($entry_content);
  368. $entry_title = db_escape_string($entry_title);
  369. $entry_link = db_escape_string($entry_link);
  370. $entry_comments = db_escape_string($entry_comments);
  371. $num_comments = 0; # FIXME
  372. if (!$num_comments) $num_comments = 0;
  373. }
  374. # sanitize content
  375. $entry_content = sanitize_rss($entry_content);
  376. $entry_title = sanitize_rss($entry_title);
  377. $entry_link = sanitize_rss($entry_link);
  378. $entry_comments = sanitize_rss($entry_comments);
  379. db_query($link, "BEGIN");
  380. if (db_num_rows($result) == 0) {
  381. // base post entry does not exist, create it
  382. $result = db_query($link,
  383. "INSERT INTO ttrss_entries
  384. (title,
  385. guid,
  386. link,
  387. updated,
  388. content,
  389. content_hash,
  390. no_orig_date,
  391. date_entered,
  392. comments,
  393. num_comments,
  394. author)
  395. VALUES
  396. ('$entry_title',
  397. '$entry_guid',
  398. '$entry_link',
  399. '$entry_timestamp_fmt',
  400. '$entry_content',
  401. '$content_hash',
  402. $no_orig_date,
  403. NOW(),
  404. '$entry_comments',
  405. '$num_comments',
  406. '$entry_author')");
  407. } else {
  408. // we keep encountering the entry in feeds, so we need to
  409. // update date_entered column so that we don't get horrible
  410. // dupes when the entry gets purged and reinserted again e.g.
  411. // in the case of SLOW SLOW OMG SLOW updating feeds
  412. $base_entry_id = db_fetch_result($result, 0, "id");
  413. db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
  414. WHERE id = '$base_entry_id'");
  415. }
  416. // now it should exist, if not - bad luck then
  417. $result = db_query($link, "SELECT
  418. id,content_hash,no_orig_date,title,
  419. substring(date_entered,1,19) as date_entered,
  420. substring(updated,1,19) as updated,
  421. num_comments
  422. FROM
  423. ttrss_entries
  424. WHERE guid = '$entry_guid'");
  425. if (db_num_rows($result) == 1) {
  426. // this will be used below in update handler
  427. $orig_content_hash = db_fetch_result($result, 0, "content_hash");
  428. $orig_title = db_fetch_result($result, 0, "title");
  429. $orig_num_comments = db_fetch_result($result, 0, "num_comments");
  430. $orig_date_entered = strtotime(db_fetch_result($result,
  431. 0, "date_entered"));
  432. $ref_id = db_fetch_result($result, 0, "id");
  433. // check for user post link to main table
  434. // do we allow duplicate posts with same GUID in different feeds?
  435. if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
  436. $dupcheck_qpart = "AND feed_id = '$feed'";
  437. } else {
  438. $dupcheck_qpart = "";
  439. }
  440. // error_reporting(0);
  441. $filter_name = get_filter_name($entry_title, $entry_content,
  442. $entry_link, $filters);
  443. if ($filter_name == "filter") {
  444. continue;
  445. }
  446. // error_reporting (DEFAULT_ERROR_LEVEL);
  447. $result = db_query($link,
  448. "SELECT ref_id FROM ttrss_user_entries WHERE
  449. ref_id = '$ref_id' AND owner_uid = '$owner_uid'
  450. $dupcheck_qpart");
  451. // okay it doesn't exist - create user entry
  452. if (db_num_rows($result) == 0) {
  453. if ($filter_name != 'catchup') {
  454. $unread = 'true';
  455. $last_read_qpart = 'NULL';
  456. } else {
  457. $unread = 'false';
  458. $last_read_qpart = 'NOW()';
  459. }
  460. if ($filter_name == 'mark') {
  461. $marked = 'true';
  462. } else {
  463. $marked = 'false';
  464. }
  465. $result = db_query($link,
  466. "INSERT INTO ttrss_user_entries
  467. (ref_id, owner_uid, feed_id, unread, last_read, marked)
  468. VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
  469. $last_read_qpart, $marked)");
  470. }
  471. $post_needs_update = false;
  472. if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
  473. ($content_hash != $orig_content_hash)) {
  474. $post_needs_update = true;
  475. }
  476. if ($orig_title != $entry_title) {
  477. $post_needs_update = true;
  478. }
  479. if ($orig_num_comments != $num_comments) {
  480. $post_needs_update = true;
  481. }
  482. // this doesn't seem to be very reliable
  483. //
  484. // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
  485. // $post_needs_update = true;
  486. // }
  487. // if post needs update, update it and mark all user entries
  488. // linking to this post as updated
  489. if ($post_needs_update) {
  490. // print "<!-- post $orig_title needs update : $post_needs_update -->";
  491. db_query($link, "UPDATE ttrss_entries
  492. SET title = '$entry_title', content = '$entry_content',
  493. num_comments = '$num_comments'
  494. WHERE id = '$ref_id'");
  495. if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
  496. db_query($link, "UPDATE ttrss_user_entries
  497. SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
  498. } else {
  499. db_query($link, "UPDATE ttrss_user_entries
  500. SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
  501. }
  502. }
  503. }
  504. db_query($link, "COMMIT");
  505. /* taaaags */
  506. // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
  507. $entry_tags = null;
  508. preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
  509. $entry_content_unescaped, $entry_tags);
  510. // print "<br>$entry_title : $entry_content_unescaped<br>";
  511. // print_r($entry_tags);
  512. // print "<br>";
  513. $entry_tags = $entry_tags[1];
  514. if (count($entry_tags) > 0) {
  515. db_query($link, "BEGIN");
  516. $result = db_query($link, "SELECT id,int_id
  517. FROM ttrss_entries,ttrss_user_entries
  518. WHERE guid = '$entry_guid'
  519. AND feed_id = '$feed' AND ref_id = id
  520. AND owner_uid = '$owner_uid'");
  521. if (db_num_rows($result) == 1) {
  522. $entry_id = db_fetch_result($result, 0, "id");
  523. $entry_int_id = db_fetch_result($result, 0, "int_id");
  524. foreach ($entry_tags as $tag) {
  525. $tag = db_escape_string(strtolower($tag));
  526. $tag = str_replace("+", " ", $tag);
  527. $tag = str_replace("technorati tag: ", "", $tag);
  528. $result = db_query($link, "SELECT id FROM ttrss_tags
  529. WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
  530. owner_uid = '$owner_uid' LIMIT 1");
  531. // print db_fetch_result($result, 0, "id");
  532. if ($result && db_num_rows($result) == 0) {
  533. // print "tagging $entry_id as $tag<br>";
  534. db_query($link, "INSERT INTO ttrss_tags
  535. (owner_uid,tag_name,post_int_id)
  536. VALUES ('$owner_uid','$tag', '$entry_int_id')");
  537. }
  538. }
  539. }
  540. db_query($link, "COMMIT");
  541. }
  542. }
  543. db_query($link, "UPDATE ttrss_feeds
  544. SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
  545. // db_query($link, "COMMIT");
  546. } else {
  547. $error_msg = db_escape_string(magpie_error());
  548. db_query($link,
  549. "UPDATE ttrss_feeds SET last_error = '$error_msg',
  550. last_updated = NOW() WHERE id = '$feed'");
  551. }
  552. }
  553. function print_select($id, $default, $values, $attributes = "") {
  554. print "<select name=\"$id\" id=\"$id\" $attributes>";
  555. foreach ($values as $v) {
  556. if ($v == $default)
  557. $sel = " selected";
  558. else
  559. $sel = "";
  560. print "<option$sel>$v</option>";
  561. }
  562. print "</select>";
  563. }
  564. function print_select_hash($id, $default, $values, $attributes = "") {
  565. print "<select name=\"$id\" id='$id' $attributes>";
  566. foreach (array_keys($values) as $v) {
  567. if ($v == $default)
  568. $sel = "selected";
  569. else
  570. $sel = "";
  571. print "<option $sel value=\"$v\">".$values[$v]."</option>";
  572. }
  573. print "</select>";
  574. }
  575. function get_filter_name($title, $content, $link, $filters) {
  576. if ($filters["title"]) {
  577. foreach ($filters["title"] as $filter) {
  578. $reg_exp = $filter["reg_exp"];
  579. if (preg_match("/$reg_exp/i", $title)) {
  580. return $filter["action"];
  581. }
  582. }
  583. }
  584. if ($filters["content"]) {
  585. foreach ($filters["content"] as $filter) {
  586. $reg_exp = $filter["reg_exp"];
  587. if (preg_match("/$reg_exp/i", $content)) {
  588. return $filter["action"];
  589. }
  590. }
  591. }
  592. if ($filters["both"]) {
  593. foreach ($filters["both"] as $filter) {
  594. $reg_exp = $filter["reg_exp"];
  595. if (preg_match("/$reg_exp/i", $title) ||
  596. preg_match("/$reg_exp/i", $content)) {
  597. return $filter["action"];
  598. }
  599. }
  600. }
  601. if ($filters["link"]) {
  602. $reg_exp = $filter["reg_exp"];
  603. foreach ($filters["link"] as $filter) {
  604. $reg_exp = $filter["reg_exp"];
  605. if (preg_match("/$reg_exp/i", $link)) {
  606. return $filter["action"];
  607. }
  608. }
  609. }
  610. return false;
  611. }
  612. function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
  613. $rtl_content = false, $last_updated = false, $last_error = false) {
  614. if (file_exists($icon_file) && filesize($icon_file) > 0) {
  615. $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
  616. } else {
  617. $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
  618. }
  619. if ($rtl_content) {
  620. $rtl_tag = "dir=\"rtl\"";
  621. } else {
  622. $rtl_tag = "dir=\"ltr\"";
  623. }
  624. $error_notify_msg = "";
  625. if ($last_error) {
  626. $link_title = "Error: $last_error ($last_updated)";
  627. $error_notify_msg = "(Error)";
  628. } else if ($last_updated) {
  629. $link_title = "Updated: $last_updated";
  630. }
  631. $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', '', false);\">$feed_title</a>";
  632. print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
  633. if (get_pref($link, 'ENABLE_FEED_ICONS')) {
  634. print "$feed_icon";
  635. }
  636. print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
  637. if ($unread != 0) {
  638. $fctr_class = "";
  639. } else {
  640. $fctr_class = "class=\"invisible\"";
  641. }
  642. print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
  643. (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
  644. if (get_pref($link, "EXTENDED_FEEDLIST")) {
  645. print "<div class=\"feedExtInfo\">
  646. <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
  647. }
  648. print "</li>";
  649. }
  650. function getmicrotime() {
  651. list($usec, $sec) = explode(" ",microtime());
  652. return ((float)$usec + (float)$sec);
  653. }
  654. function print_radio($id, $default, $values, $attributes = "") {
  655. foreach ($values as $v) {
  656. if ($v == $default)
  657. $sel = "checked";
  658. else
  659. $sel = "";
  660. if ($v == "Yes") {
  661. $sel .= " value=\"1\"";
  662. } else {
  663. $sel .= " value=\"0\"";
  664. }
  665. print "<input class=\"noborder\"
  666. type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
  667. }
  668. }
  669. function initialize_user_prefs($link, $uid) {
  670. $uid = db_escape_string($uid);
  671. db_query($link, "BEGIN");
  672. $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
  673. $u_result = db_query($link, "SELECT pref_name
  674. FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
  675. $active_prefs = array();
  676. while ($line = db_fetch_assoc($u_result)) {
  677. array_push($active_prefs, $line["pref_name"]);
  678. }
  679. while ($line = db_fetch_assoc($result)) {
  680. if (array_search($line["pref_name"], $active_prefs) === FALSE) {
  681. // print "adding " . $line["pref_name"] . "<br>";
  682. db_query($link, "INSERT INTO ttrss_user_prefs
  683. (owner_uid,pref_name,value) VALUES
  684. ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
  685. }
  686. }
  687. db_query($link, "COMMIT");
  688. }
  689. function lookup_user_id($link, $user) {
  690. $result = db_query($link, "SELECT id FROM ttrss_users WHERE
  691. login = '$login'");
  692. if (db_num_rows($result) == 1) {
  693. return db_fetch_result($result, 0, "id");
  694. } else {
  695. return false;
  696. }
  697. }
  698. function http_authenticate_user($link) {
  699. if (!$_SERVER["PHP_AUTH_USER"]) {
  700. header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
  701. header('HTTP/1.0 401 Unauthorized');
  702. exit;
  703. } else {
  704. $auth_result = authenticate_user($link,
  705. $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
  706. if (!$auth_result) {
  707. header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
  708. header('HTTP/1.0 401 Unauthorized');
  709. exit;
  710. }
  711. }
  712. return true;
  713. }
  714. function authenticate_user($link, $login, $password) {
  715. if (!SINGLE_USER_MODE) {
  716. $pwd_hash = 'SHA1:' . sha1($password);
  717. $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
  718. login = '$login' AND pwd_hash = '$pwd_hash'");
  719. if (db_num_rows($result) == 1) {
  720. $_SESSION["uid"] = db_fetch_result($result, 0, "id");
  721. $_SESSION["name"] = db_fetch_result($result, 0, "login");
  722. $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
  723. db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
  724. $_SESSION["uid"]);
  725. $user_theme = get_user_theme_path($link);
  726. $_SESSION["theme"] = $user_theme;
  727. $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
  728. initialize_user_prefs($link, $_SESSION["uid"]);
  729. return true;
  730. }
  731. return false;
  732. } else {
  733. $_SESSION["uid"] = 1;
  734. $_SESSION["name"] = "admin";
  735. $user_theme = get_user_theme_path($link);
  736. $_SESSION["theme"] = $user_theme;
  737. $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
  738. initialize_user_prefs($link, $_SESSION["uid"]);
  739. return true;
  740. }
  741. }
  742. function make_password($length = 8) {
  743. $password = "";
  744. $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
  745. $i = 0;
  746. while ($i < $length) {
  747. $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
  748. if (!strstr($password, $char)) {
  749. $password .= $char;
  750. $i++;
  751. }
  752. }
  753. return $password;
  754. }
  755. // this is called after user is created to initialize default feeds, labels
  756. // or whatever else
  757. // user preferences are checked on every login, not here
  758. function initialize_user($link, $uid) {
  759. db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
  760. values ('$uid','unread = true', 'Unread articles')");
  761. db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
  762. values ('$uid','last_read is null and unread = false', 'Updated articles')");
  763. db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
  764. values ('$uid', 'Tiny Tiny RSS: New Releases',
  765. 'http://tt-rss.spb.ru/releases.rss')");
  766. }
  767. function logout_user() {
  768. session_destroy();
  769. if (isset($_COOKIE[session_name()])) {
  770. setcookie(session_name(), '', time()-42000, '/');
  771. }
  772. }
  773. function get_script_urlpath() {
  774. return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
  775. }
  776. function get_login_redirect() {
  777. $server = $_SERVER["SERVER_NAME"];
  778. if (ENABLE_LOGIN_SSL) {
  779. $protocol = "https";
  780. } else {
  781. $protocol = "http";
  782. }
  783. $url_path = get_script_urlpath();
  784. $redirect_uri = "$protocol://$server$url_path/login.php";
  785. return $redirect_uri;
  786. }
  787. function validate_session($link) {
  788. if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) {
  789. if ($_SESSION["ip_address"]) {
  790. if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
  791. return false;
  792. }
  793. }
  794. }
  795. return true;
  796. }
  797. function basic_nosid_redirect_check() {
  798. if (!SINGLE_USER_MODE) {
  799. if (!$_COOKIE[get_session_cookie_name()]) {
  800. $redirect_uri = get_login_redirect();
  801. $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
  802. header("Location: $redirect_uri?rt=$return_to");
  803. exit;
  804. }
  805. }
  806. }
  807. function login_sequence($link) {
  808. if (!SINGLE_USER_MODE) {
  809. if (!validate_session($link)) {
  810. logout_user();
  811. $redirect_uri = get_login_redirect();
  812. $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
  813. header("Location: $redirect_uri?rt=$return_to");
  814. exit;
  815. }
  816. if (!USE_HTTP_AUTH) {
  817. if (!$_SESSION["uid"]) {
  818. $redirect_uri = get_login_redirect();
  819. $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
  820. header("Location: $redirect_uri?rt=$return_to");
  821. exit;
  822. }
  823. } else {
  824. if (!$_SESSION["uid"]) {
  825. if (!$_SERVER["PHP_AUTH_USER"]) {
  826. header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
  827. header('HTTP/1.0 401 Unauthorized');
  828. exit;
  829. } else {
  830. $auth_result = authenticate_user($link,
  831. $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
  832. if (!$auth_result) {
  833. header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
  834. header('HTTP/1.0 401 Unauthorized');
  835. exit;
  836. }
  837. }
  838. }
  839. }
  840. } else {
  841. return authenticate_user($link, "admin", null);
  842. }
  843. }
  844. function truncate_string($str, $max_len) {
  845. if (mb_strlen($str, "utf-8") > $max_len - 3) {
  846. return mb_substr($str, 0, $max_len, "utf-8") . "...";
  847. } else {
  848. return $str;
  849. }
  850. }
  851. function get_user_theme_path($link) {
  852. $result = db_query($link, "SELECT theme_path
  853. FROM
  854. ttrss_themes,ttrss_users
  855. WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
  856. if (db_num_rows($result) != 0) {
  857. return db_fetch_result($result, 0, "theme_path");
  858. } else {
  859. return null;
  860. }
  861. }
  862. function smart_date_time($timestamp) {
  863. if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
  864. return date("G:i", $timestamp);
  865. } else if (date("Y", $timestamp) == date("Y")) {
  866. return date("M d, G:i", $timestamp);
  867. } else {
  868. return date("Y/m/d G:i", $timestamp);
  869. }
  870. }
  871. function smart_date($timestamp) {
  872. if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
  873. return "Today";
  874. } else if (date("Y", $timestamp) == date("Y")) {
  875. return date("D m", $timestamp);
  876. } else {
  877. return date("Y/m/d", $timestamp);
  878. }
  879. }
  880. function sql_bool_to_string($s) {
  881. if ($s == "t" || $s == "1") {
  882. return "true";
  883. } else {
  884. return "false";
  885. }
  886. }
  887. function sql_bool_to_bool($s) {
  888. if ($s == "t" || $s == "1") {
  889. return true;
  890. } else {
  891. return false;
  892. }
  893. }
  894. function toggleEvenOdd($a) {
  895. if ($a == "even")
  896. return "odd";
  897. else
  898. return "even";
  899. }
  900. function sanity_check($link) {
  901. error_reporting(0);
  902. $error_code = 0;
  903. $result = db_query($link, "SELECT schema_version FROM ttrss_version");
  904. $schema_version = db_fetch_result($result, 0, "schema_version");
  905. if ($schema_version != SCHEMA_VERSION) {
  906. $error_code = 5;
  907. }
  908. if (DB_TYPE == "mysql") {
  909. $result = db_query($link, "SELECT true", false);
  910. if (db_num_rows($result) != 1) {
  911. $error_code = 10;
  912. }
  913. }
  914. error_reporting (DEFAULT_ERROR_LEVEL);
  915. if ($error_code != 0) {
  916. print_error_xml($error_code);
  917. return false;
  918. } else {
  919. return true;
  920. }
  921. }
  922. function file_is_locked($filename) {
  923. error_reporting(0);
  924. $fp = fopen($filename, "r");
  925. error_reporting(DEFAULT_ERROR_LEVEL);
  926. if ($fp) {
  927. if (flock($fp, LOCK_EX | LOCK_NB)) {
  928. flock($fp, LOCK_UN);
  929. fclose($fp);
  930. return false;
  931. }
  932. fclose($fp);
  933. return true;
  934. }
  935. return false;
  936. }
  937. function make_lockfile($filename) {
  938. $fp = fopen($filename, "w");
  939. if (flock($fp, LOCK_EX | LOCK_NB)) {
  940. return $fp;
  941. } else {
  942. return false;
  943. }
  944. }
  945. function sql_random_function() {
  946. if (DB_TYPE == "mysql") {
  947. return "RAND()";
  948. } else {
  949. return "RANDOM()";
  950. }
  951. }
  952. function catchup_feed($link, $feed, $cat_view) {
  953. if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
  954. if ($cat_view) {
  955. if ($feed > 0) {
  956. $cat_qpart = "cat_id = '$feed'";
  957. } else {
  958. $cat_qpart = "cat_id IS NULL";
  959. }
  960. $tmp_result = db_query($link, "SELECT id
  961. FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
  962. $_SESSION["uid"]);
  963. while ($tmp_line = db_fetch_assoc($tmp_result)) {
  964. $tmp_feed = $tmp_line["id"];
  965. db_query($link, "UPDATE ttrss_user_entries
  966. SET unread = false,last_read = NOW()
  967. WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
  968. }
  969. } else if ($feed > 0) {
  970. $tmp_result = db_query($link, "SELECT id
  971. FROM ttrss_feeds WHERE parent_feed = '$feed'
  972. ORDER BY cat_id,title");
  973. $parent_ids = array();
  974. if (db_num_rows($tmp_result) > 0) {
  975. while ($p = db_fetch_assoc($tmp_result)) {
  976. array_push($parent_ids, "feed_id = " . $p["id"]);
  977. }
  978. $children_qpart = implode(" OR ", $parent_ids);
  979. db_query($link, "UPDATE ttrss_user_entries
  980. SET unread = false,last_read = NOW()
  981. WHERE (feed_id = '$feed' OR $children_qpart)
  982. AND owner_uid = " . $_SESSION["uid"]);
  983. } else {
  984. db_query($link, "UPDATE ttrss_user_entries
  985. SET unread = false,last_read = NOW()
  986. WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
  987. }
  988. } else if ($feed < 0 && $feed > -10) { // special, like starred
  989. if ($feed == -1) {
  990. db_query($link, "UPDATE ttrss_user_entries
  991. SET unread = false,last_read = NOW()
  992. WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
  993. }
  994. } else if ($feed < -10) { // label
  995. // TODO make this more efficient
  996. $label_id = -$feed - 11;
  997. $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
  998. WHERE id = '$label_id'");
  999. if ($tmp_result) {
  1000. $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
  1001. db_query($link, "BEGIN");
  1002. $tmp2_result = db_query($link,
  1003. "SELECT
  1004. int_id
  1005. FROM
  1006. ttrss_user_entries,ttrss_entries,ttrss_feeds
  1007. WHERE
  1008. ref_id = ttrss_entries.id AND
  1009. ttrss_user_entries.feed_id = ttrss_feeds.id AND
  1010. $sql_exp AND
  1011. ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
  1012. while ($tmp_line = db_fetch_assoc($tmp2_result)) {
  1013. db_query($link, "UPDATE
  1014. ttrss_user_entries
  1015. SET
  1016. unread = false, last_read = NOW()
  1017. WHERE
  1018. int_id = " . $tmp_line["int_id"]);
  1019. }
  1020. db_query($link, "COMMIT");
  1021. /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
  1022. SET unread = false,last_read = NOW()
  1023. WHERE $sql_exp
  1024. AND ref_id = id
  1025. AND owner_uid = ".$_SESSION["uid"]); */
  1026. }
  1027. }
  1028. } else { // tag
  1029. db_query($link, "BEGIN");
  1030. $tag_name = db_escape_string($feed);
  1031. $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
  1032. WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
  1033. while ($line = db_fetch_assoc($result)) {
  1034. db_query($link, "UPDATE ttrss_user_entries SET
  1035. unread = false, last_read = NOW()
  1036. WHERE int_id = " . $line["post_int_id"]);
  1037. }
  1038. db_query($link, "COMMIT");
  1039. }
  1040. }
  1041. function update_generic_feed($link, $feed, $cat_view) {
  1042. if ($cat_view) {
  1043. if ($feed > 0) {
  1044. $cat_qpart = "cat_id = '$feed'";
  1045. } else {
  1046. $cat_qpart = "cat_id IS NULL";
  1047. }
  1048. $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
  1049. WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
  1050. while ($tmp_line = db_fetch_assoc($tmp_result)) {
  1051. $feed_url = $tmp_line["feed_url"];
  1052. update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
  1053. }
  1054. } else {
  1055. $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
  1056. WHERE id = '$feed'");
  1057. $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
  1058. update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
  1059. }
  1060. }
  1061. function getAllCounters($link) {
  1062. getLabelCounters($link);
  1063. getFeedCounters($link);
  1064. getTagCounters($link);
  1065. getGlobalCounters($link);
  1066. if (get_pref($link, 'ENABLE_FEED_CATS')) {
  1067. getCategoryCounters($link);
  1068. }
  1069. }
  1070. function getCategoryCounters($link) {
  1071. $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
  1072. FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
  1073. AND unread = true)) AS unread FROM ttrss_feeds
  1074. WHERE
  1075. hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
  1076. while ($line = db_fetch_assoc($result)) {
  1077. $line["cat_id"] = sprintf("%d", $line["cat_id"]);
  1078. print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
  1079. $line["unread"]."\"/>";
  1080. }
  1081. }
  1082. function getCategoryUnread($link, $cat) {
  1083. if ($cat != 0) {
  1084. $cat_query = "cat_id = '$cat'";
  1085. } else {
  1086. $cat_query = "cat_id IS NULL";
  1087. }
  1088. $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
  1089. AND hidden = false
  1090. AND owner_uid = " . $_SESSION["uid"]);
  1091. $cat_feeds = array();
  1092. while ($line = db_fetch_assoc($result)) {
  1093. array_push($cat_feeds, "feed_id = " . $line["id"]);
  1094. }
  1095. if (count($cat_feeds) == 0) return 0;
  1096. $match_part = implode(" OR ", $cat_feeds);
  1097. $result = db_query($link, "SELECT COUNT(int_id) AS unread
  1098. FROM ttrss_user_entries
  1099. WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
  1100. $unread = 0;
  1101. # this needs to be rewritten
  1102. while ($line = db_fetch_assoc($result)) {
  1103. $unread += $line["unread"];
  1104. }
  1105. return $unread;
  1106. }
  1107. function getFeedUnread($link, $feed, $is_cat = false) {
  1108. $n_feed = sprintf("%d", $feed);
  1109. if ($is_cat) {
  1110. return getCategoryUnread($link, $n_feed);
  1111. } else if ($n_feed == -1) {
  1112. $match_part = "marked = true";
  1113. } else if ($n_feed > 0) {
  1114. $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE parent_feed = '$n_feed'
  1115. AND hidden = false
  1116. AND owner_uid = " . $_SESSION["uid"]);
  1117. if (db_num_rows($result) > 0) {
  1118. $linked_feeds = array();
  1119. while ($line = db_fetch_assoc($result)) {
  1120. array_push($linked_feeds, "feed_id = " . $line["id"]);
  1121. }
  1122. $match_part = implode(" OR ", $linked_feeds);
  1123. $result = db_query($link, "SELECT COUNT(int_id) AS unread
  1124. FROM ttrss_user_entries
  1125. WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
  1126. $unread = 0;
  1127. # this needs to be rewritten
  1128. while ($line = db_fetch_assoc($result)) {
  1129. $unread += $line["unread"];
  1130. }
  1131. return $unread;
  1132. } else {
  1133. $match_part = "feed_id = '$n_feed'";
  1134. }
  1135. } else if ($feed < -10) {
  1136. $label_id = -$feed - 11;
  1137. $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
  1138. id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
  1139. $match_part = db_fetch_result($result, 0, "sql_exp");
  1140. }
  1141. if ($match_part) {
  1142. $result = db_query($link, "SELECT count(int_id) AS unread
  1143. FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
  1144. ttrss_user_entries.feed_id = ttrss_feeds.id AND
  1145. ttrss_user_entries.ref_id = ttrss_entries.id AND
  1146. ttrss_feeds.hidden = false AND
  1147. unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
  1148. } else {
  1149. $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
  1150. FROM ttrss_tags,ttrss_user_entries
  1151. WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
  1152. ttrss_tags.owner_uid = " . $_SESSION["uid"]);
  1153. }
  1154. $unread = db_fetch_result($result, 0, "unread");
  1155. return $unread;
  1156. }
  1157. /* FIXME this needs reworking */
  1158. function getGlobalUnread($link) {
  1159. $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
  1160. WHERE unread = true AND
  1161. ttrss_user_entries.feed_id = ttrss_feeds.id AND
  1162. ttrss_user_entries.ref_id = ttrss_entries.id AND
  1163. hidden = false AND
  1164. ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
  1165. $c_id = db_fetch_result($result, 0, "c_id");
  1166. return $c_id;
  1167. }
  1168. function getGlobalCounters($link, $global_unread = -1) {
  1169. if ($global_unread == -1) {
  1170. $global_unread = getGlobalUnread($link);
  1171. }
  1172. print "<counter type=\"global\" id='global-unread'
  1173. counter='$global_unread'/>";
  1174. $result = db_query($link, "SELECT COUNT(id) AS fn FROM
  1175. ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
  1176. $subscribed_feeds = db_fetch_result($result, 0, "fn");
  1177. print "<counter type=\"global\" id='subscribed-feeds'
  1178. counter='$subscribed_feeds'/>";
  1179. }
  1180. function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
  1181. if ($smart_mode) {
  1182. if (!$_SESSION["tctr_last_value"]) {
  1183. $_SESSION["tctr_last_value"] = array();
  1184. }
  1185. }
  1186. $old_counters = $_SESSION["tctr_last_value"];
  1187. $tctrs_modified = false;
  1188. /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
  1189. FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
  1190. ttrss_user_entries.ref_id = ttrss_entries.id AND
  1191. ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
  1192. post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
  1193. UNION
  1194. select tag_name,0 as count FROM ttrss_tags
  1195. WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
  1196. $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
  1197. FROM ttrss_user_entries WHERE int_id = post_int_id
  1198. AND unread = true)) AS count FROM ttrss_tags
  1199. WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
  1200. $tags = array();
  1201. while ($line = db_fetch_assoc($result)) {
  1202. $tags[$line["tag_name"]] += $line["count"];
  1203. }
  1204. foreach (array_keys($tags) as $tag) {
  1205. $unread = $tags[$tag];
  1206. $tag = htmlspecialchars($tag);
  1207. if (!$smart_mode || $old_counters[$tag] != $unread) {
  1208. $old_counters[$tag] = $unread;
  1209. $tctrs_modified = true;
  1210. print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
  1211. }
  1212. }
  1213. if ($smart_mode && $tctrs_modified) {
  1214. $_SESSION["tctr_last_value"] = $old_counters;
  1215. }
  1216. }
  1217. function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) {
  1218. if ($smart_mode) {
  1219. if (!$_SESSION["lctr_last_value"]) {
  1220. $_SESSION["lctr_last_value"] = array();
  1221. }
  1222. }
  1223. $ret_arr = array();
  1224. $old_counters = $_SESSION["lctr_last_value"];
  1225. $lctrs_modified = false;
  1226. $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
  1227. WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
  1228. ttrss_user_entries.feed_id = ttrss_feeds.id AND
  1229. unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
  1230. $count = db_fetch_result($result, 0, "count");
  1231. if (!$ret_mode) {
  1232. print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
  1233. } else {
  1234. $ret_arr["-1"]["counter"] = $count;
  1235. $ret_arr["-1"]["description"] = "Starred";
  1236. }
  1237. $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
  1238. ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
  1239. while ($line = db_fetch_assoc($result)) {
  1240. $id = -$line["id"] - 11;
  1241. $label_name = $line["description"];
  1242. error_reporting (0);
  1243. $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
  1244. WHERE (" . $line["sql_exp"] . ") AND unread = true AND
  1245. ttrss_feeds.hidden = false AND
  1246. ttrss_user_entries.feed_id = ttrss_feeds.id AND
  1247. ttrss_user_entries.ref_id = ttrss_entries.id AND
  1248. ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
  1249. $count = db_fetch_result($tmp_result, 0, "count");
  1250. if (!$smart_mode || $old_counters[$id] != $count) {
  1251. $old_counters[$id] = $count;
  1252. $lctrs_modified = true;
  1253. if (!$ret_mode) {
  1254. print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
  1255. } else {
  1256. $ret_arr[$id]["counter"] = $count;
  1257. $ret_arr[$id]["description"] = $label_name;
  1258. }
  1259. }
  1260. error_reporting (DEFAULT_ERROR_LEVEL);
  1261. }
  1262. if ($smart_mode && $lctrs_modified) {
  1263. $_SESSION["lctr_last_value"] = $old_counters;
  1264. }
  1265. return $ret_arr;
  1266. }
  1267. /* function getFeedCounter($link, $id) {
  1268. $result = db_query($link, "SELECT
  1269. count(id) as count,last_error
  1270. FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
  1271. WHERE feed_id = '$id' AND unread = true
  1272. AND ttrss_user_entries.feed_id = ttrss_feeds.id
  1273. AND ttrss_user_entries.ref_id = ttrss_entries.id");
  1274. $count = db_fetch_result($result, 0, "count");
  1275. $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
  1276. print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
  1277. } */
  1278. function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
  1279. if ($smart_mode) {
  1280. if (!$_SESSION["fctr_last_value"]) {
  1281. $_SESSION["fctr_last_value"] = array();
  1282. }
  1283. }
  1284. $old_counters = $_SESSION["fctr_last_value"];
  1285. $result = db_query($link, "SELECT id,last_error,parent_feed,
  1286. SUBSTRING(last_updated,1,19) AS last_updated,
  1287. (SELECT count(id)
  1288. FROM ttrss_entries,ttrss_user_entries
  1289. WHERE feed_id = ttrss_feeds.id AND
  1290. ttrss_user_entries.ref_id = ttrss_entries.id
  1291. AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
  1292. FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
  1293. AND parent_feed IS NULL");
  1294. $fctrs_modified = false;
  1295. $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
  1296. while ($line = db_fetch_assoc($result)) {
  1297. $id = $line["id"];
  1298. $count = $line["count"];
  1299. $last_error = htmlspecialchars($line["last_error"]);
  1300. if (get_pref($link, 'HEADLINES_SMART_DATE')) {
  1301. $last_updated = smart_date_time(strtotime($line["last_updated"]));
  1302. } else {
  1303. $last_updated = date($short_date, strtotime($line["last_updated"]));
  1304. }
  1305. $has_img = is_file(ICONS_DIR . "/$id.ico");
  1306. $tmp_result = db_query($link,
  1307. "SELECT id,COUNT(unread) AS unread
  1308. FROM ttrss_feeds LEFT JOIN ttrss_user_entries
  1309. ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
  1310. WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
  1311. if (db_num_rows($tmp_result) > 0) {
  1312. while ($l = db_fetch_assoc($tmp_result)) {
  1313. $count += $l["unread"];
  1314. }
  1315. }
  1316. if (!$smart_mode || $old_counters[$id] != $count) {
  1317. $old_counters[$id] = $count;
  1318. $fctrs_modified = true;
  1319. if ($last_error) {
  1320. $error_part = "error=\"$last_error\"";
  1321. } else {
  1322. $error_part = "";
  1323. }
  1324. if ($has_img) {
  1325. $has_img_part = "hi=\"$has_img\"";
  1326. } else {
  1327. $has_img_part = "";
  1328. }
  1329. print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
  1330. }
  1331. }
  1332. if ($smart_mode && $fctrs_modified) {
  1333. $_SESSION["fctr_last_value"] = $old_counters;
  1334. }
  1335. }
  1336. function get_script_dt_add() {
  1337. if (strpos(VERSION, "99") === false) {
  1338. return VERSION;
  1339. } else {
  1340. return time();
  1341. }
  1342. }
  1343. function get_pgsql_version($link) {
  1344. $result = db_query($link, "SELECT version() AS version");
  1345. $version = split(" ", db_fetch_result($result, 0, "version"));
  1346. return $version[1];
  1347. }
  1348. function print_error_xml($code, $add_msg = "") {
  1349. global $ERRORS;
  1350. $error_msg = $ERRORS[$code];
  1351. if ($add_msg) {
  1352. $error_msg = "$error_msg; $add_msg";
  1353. }
  1354. print "<rpc-reply>";
  1355. print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
  1356. print "</rpc-reply>";
  1357. }
  1358. function subscribe_to_feed($link, $feed_link, $cat_id = 0) {
  1359. if ($cat_id == "0" || !$cat_id) {
  1360. $cat_qpart = "NULL";
  1361. } else {
  1362. $cat_qpart = "'$cat_id'";
  1363. }
  1364. $result = db_query($link,
  1365. "SELECT id FROM ttrss_feeds
  1366. WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
  1367. if (db_num_rows($result) == 0) {
  1368. $result = db_query($link,
  1369. "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
  1370. VALUES ('".$_SESSION["uid"]."', '$feed_link',
  1371. '[Unknown]', $cat_qpart)");
  1372. $result = db_query($link,
  1373. "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
  1374. AND owner_uid = " . $_SESSION["uid"]);
  1375. $feed_id = db_fetch_result($result, 0, "id");
  1376. if ($feed_id) {
  1377. update_rss_feed($link, $feed_link, $feed_id, true);
  1378. }
  1379. return true;
  1380. } else {
  1381. return false;
  1382. }
  1383. }
  1384. function print_feed_select($link, $id, $default_id = "",
  1385. $attributes = "", $include_all_feeds = true) {
  1386. print "<select id=\"$id\" name=\"$id\" $attributes>";
  1387. if ($include_all_feeds) {
  1388. print "<option value=\"0\">All feeds</option>";
  1389. }
  1390. $result = db_query($link, "SELECT id,title FROM ttrss_feeds
  1391. WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
  1392. if (db_num_rows($result) > 0 && $include_all_feeds) {
  1393. print "<option disabled>--------</option>";
  1394. }
  1395. while ($line = db_fetch_assoc($result)) {
  1396. if ($line["id"] == $default_id) {
  1397. $is_selected = "selected";
  1398. } else {
  1399. $is_selected = "";
  1400. }
  1401. printf("<option $is_selected value='%d'>%s</option>",
  1402. $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
  1403. }
  1404. print "</select>";
  1405. }
  1406. function print_feed_cat_select($link, $id, $default_id = "",
  1407. $attributes = "", $include_all_cats = true) {
  1408. print "<select id=\"$id\" name=\"$id\" $attributes>";
  1409. if ($include_all_cats) {
  1410. print "<option value=\"0\">Uncategorized</option>";
  1411. }
  1412. $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
  1413. WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
  1414. if (db_num_rows($result) > 0 && $include_all_cats) {
  1415. print "<option disabled>--------</option>";
  1416. }
  1417. while ($line = db_fetch_assoc($result)) {
  1418. if ($line["id"] == $default_id) {
  1419. $is_selected = "selected";
  1420. } else {
  1421. $is_selected = "";
  1422. }
  1423. printf("<option $is_selected value='%d'>%s</option>",
  1424. $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
  1425. }
  1426. print "</select>";
  1427. }
  1428. function checkbox_to_sql_bool($val) {
  1429. return ($val == "on") ? "true" : "false";
  1430. }
  1431. function getFeedCatTitle($link, $id) {
  1432. if ($id == -1) {
  1433. return "Special";
  1434. } else if ($id < -10) {
  1435. return "Labels";
  1436. } else if ($id > 0) {
  1437. $result = db_query($link, "SELECT ttrss_feed_categories.title
  1438. FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
  1439. cat_id = ttrss_feed_categories.id");
  1440. if (db_num_rows($result) == 1) {
  1441. return db_fetch_result($result, 0, "title");
  1442. } else {
  1443. return "Uncategorized";
  1444. }
  1445. } else {
  1446. return "getFeedCatTitle($id) failed";
  1447. }
  1448. }
  1449. function getFeedTitle($link, $id) {
  1450. if ($id == -1) {
  1451. return "Starred articles";
  1452. } else if ($id < -10) {
  1453. $label_id = -10 - $id;
  1454. $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
  1455. if (db_num_rows($result) == 1) {
  1456. return db_fetch_result($result, 0, "description");
  1457. } else {
  1458. return "Unknown label ($label_id)";
  1459. }
  1460. } else if ($id > 0) {
  1461. $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
  1462. if (db_num_rows($result) == 1) {
  1463. return db_fetch_result($result, 0, "title");
  1464. } else {
  1465. return "Unknown feed ($id)";
  1466. }
  1467. } else {
  1468. return "getFeedTitle($id) failed";
  1469. }
  1470. }
  1471. function get_session_cookie_name() {
  1472. return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME);
  1473. }
  1474. function print_init_params($link) {
  1475. print "<init-params>";
  1476. if ($_SESSION["stored-params"]) {
  1477. foreach (array_keys($_SESSION["stored-params"]) as $key) {
  1478. if ($key) {
  1479. $value = htmlspecialchars($_SESSION["stored-params"][$key]);
  1480. print "<param key=\"$key\" value=\"$value\"/>";
  1481. }
  1482. }
  1483. }
  1484. print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>";
  1485. print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>";
  1486. print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>";
  1487. print "<param key=\"on_catchup_show_next_feed\" value=\"" .
  1488. get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
  1489. print "<param key=\"hide_read_feeds\" value=\"" .
  1490. sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>";
  1491. print "<param key=\"feeds_sort_by_unread\" value=\"" .
  1492. sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>";
  1493. print "</init-params>";
  1494. }
  1495. function print_runtime_info($link) {
  1496. print "<runtime-info>";
  1497. if (ENABLE_UPDATE_DAEMON) {
  1498. print "<param key=\"daemon_is_running\" value=\"".
  1499. sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
  1500. }
  1501. print "</runtime-info>";
  1502. }
  1503. function getSearchSql($search, $match_on) {
  1504. $search_query_part = "";
  1505. $keywords = split(" ", $search);
  1506. $query_keywords = array();
  1507. if ($match_on == "both") {
  1508. foreach ($keywords as $k) {
  1509. array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
  1510. OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
  1511. }
  1512. $search_query_part = implode("AND", $query_keywords) . " AND ";
  1513. } else if ($match_on == "title") {
  1514. foreach ($keywords as $k) {
  1515. array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
  1516. }
  1517. $search_query_part = implode("AND", $query_keywords) . " AND ";
  1518. } else if ($match_on == "content") {
  1519. foreach ($keywords as $k) {
  1520. array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
  1521. }
  1522. }
  1523. $search_query_part = implode("AND", $query_keywords);
  1524. return $search_query_part;
  1525. }
  1526. function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false) {
  1527. if ($search) {
  1528. $search_query_part = getSearchSql($search, $match_on);
  1529. $search_query_part .= " AND ";
  1530. } else {
  1531. $search_query_part = "";
  1532. }
  1533. $view_query_part = "";
  1534. if ($view_mode == "adaptive") {
  1535. if ($search) {
  1536. $view_query_part = " ";
  1537. } else if ($feed != -1) {
  1538. $unread = getFeedUnread($link, $feed, $cat_view);
  1539. if ($unread > 0) {
  1540. $view_query_part = " unread = true AND ";
  1541. }
  1542. }
  1543. }
  1544. if ($view_mode == "marked") {
  1545. $view_query_part = " marked = true AND ";
  1546. }
  1547. if ($view_mode == "unread") {
  1548. $view_query_part = " unread = true AND ";
  1549. }
  1550. if ($limit > 0) {
  1551. $limit_query_part = "LIMIT " . $limit;
  1552. }
  1553. $vfeed_query_part = "";
  1554. // override query strategy and enable feed display when searching globally
  1555. if ($search && $search_mode == "all_feeds") {
  1556. $query_strategy_part = "ttrss_entries.id > 0";
  1557. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1558. } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
  1559. $query_strategy_part = "ttrss_entries.id > 0";
  1560. $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
  1561. id = feed_id) as feed_title,";
  1562. } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
  1563. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1564. $tmp_result = false;
  1565. if ($cat_view) {
  1566. $tmp_result = db_query($link, "SELECT id
  1567. FROM ttrss_feeds WHERE cat_id = '$feed'");
  1568. } else {
  1569. $tmp_result = db_query($link, "SELECT id
  1570. FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
  1571. WHERE id = '$feed') AND id != '$feed'");
  1572. }
  1573. $cat_siblings = array();
  1574. if (db_num_rows($tmp_result) > 0) {
  1575. while ($p = db_fetch_assoc($tmp_result)) {
  1576. array_push($cat_siblings, "feed_id = " . $p["id"]);
  1577. }
  1578. $query_strategy_part = sprintf("(feed_id = %d OR %s)",
  1579. $feed, implode(" OR ", $cat_siblings));
  1580. } else {
  1581. $query_strategy_part = "ttrss_entries.id > 0";
  1582. }
  1583. } else if ($feed >= 0) {
  1584. if ($cat_view) {
  1585. if ($feed > 0) {
  1586. $query_strategy_part = "cat_id = '$feed'";
  1587. } else {
  1588. $query_strategy_part = "cat_id IS NULL";
  1589. }
  1590. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1591. } else {
  1592. $tmp_result = db_query($link, "SELECT id
  1593. FROM ttrss_feeds WHERE parent_feed = '$feed'
  1594. ORDER BY cat_id,title");
  1595. $parent_ids = array();
  1596. if (db_num_rows($tmp_result) > 0) {
  1597. while ($p = db_fetch_assoc($tmp_result)) {
  1598. array_push($parent_ids, "feed_id = " . $p["id"]);
  1599. }
  1600. $query_strategy_part = sprintf("(feed_id = %d OR %s)",
  1601. $feed, implode(" OR ", $parent_ids));
  1602. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1603. } else {
  1604. $query_strategy_part = "feed_id = '$feed'";
  1605. }
  1606. }
  1607. } else if ($feed == -1) { // starred virtual feed
  1608. $query_strategy_part = "marked = true";
  1609. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1610. } else if ($feed <= -10) { // labels
  1611. $label_id = -$feed - 11;
  1612. $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
  1613. WHERE id = '$label_id'");
  1614. $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
  1615. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1616. } else {
  1617. $query_strategy_part = "id > 0"; // dumb
  1618. }
  1619. if (get_pref($link, 'REVERSE_HEADLINES')) {
  1620. $order_by = "updated";
  1621. } else {
  1622. $order_by = "updated DESC";
  1623. }
  1624. if ($override_order) {
  1625. $order_by = $override_order;
  1626. }
  1627. $feed_title = "";
  1628. if ($search && $search_mode == "all_feeds") {
  1629. $feed_title = "Global search results ($search)";
  1630. } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
  1631. $feed_title = "Tag search results ($search, $feed)";
  1632. } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
  1633. $feed_title = $feed;
  1634. } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
  1635. if ($cat_view) {
  1636. if ($feed != 0) {
  1637. $result = db_query($link, "SELECT title FROM ttrss_feed_categories
  1638. WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
  1639. $feed_title = db_fetch_result($result, 0, "title");
  1640. } else {
  1641. $feed_title = "Uncategorized";
  1642. }
  1643. if ($search) {
  1644. $feed_title = "Category search results ($search, $feed_title)";
  1645. }
  1646. } else {
  1647. $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
  1648. WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
  1649. $feed_title = db_fetch_result($result, 0, "title");
  1650. $feed_site_url = db_fetch_result($result, 0, "site_url");
  1651. $last_error = db_fetch_result($result, 0, "last_error");
  1652. if ($search) {
  1653. $feed_title = "Feed search results ($search, $feed_title)";
  1654. }
  1655. }
  1656. } else if ($feed == -1) {
  1657. $feed_title = "Starred articles";
  1658. } else if ($feed < -10) {
  1659. $label_id = -$feed - 11;
  1660. $result = db_query($link, "SELECT description FROM ttrss_labels
  1661. WHERE id = '$label_id'");
  1662. $feed_title = db_fetch_result($result, 0, "description");
  1663. if ($search) {
  1664. $feed_title = "Label search results ($search, $feed_title)";
  1665. }
  1666. } else {
  1667. $feed_title = "?";
  1668. }
  1669. $feed_title = db_unescape_string($feed_title);
  1670. if ($feed < -10) error_reporting (0);
  1671. if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
  1672. if ($feed >= 0) {
  1673. $feed_kind = "Feeds";
  1674. } else {
  1675. $feed_kind = "Labels";
  1676. }
  1677. $content_query_part = "content as content_preview,";
  1678. $query = "SELECT
  1679. ttrss_entries.id,ttrss_entries.title,
  1680. SUBSTRING(updated,1,16) as updated,
  1681. unread,feed_id,marked,link,last_read,
  1682. SUBSTRING(last_read,1,19) as last_read_noms,
  1683. $vfeed_query_part
  1684. $content_query_part
  1685. SUBSTRING(updated,1,19) as updated_noms
  1686. FROM
  1687. ttrss_entries,ttrss_user_entries,ttrss_feeds
  1688. WHERE
  1689. ttrss_feeds.hidden = false AND
  1690. ttrss_user_entries.feed_id = ttrss_feeds.id AND
  1691. ttrss_user_entries.ref_id = ttrss_entries.id AND
  1692. ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
  1693. $search_query_part
  1694. $view_query_part
  1695. $query_strategy_part ORDER BY $order_by
  1696. $limit_query_part";
  1697. $result = db_query($link, $query);
  1698. if ($_GET["debug"]) print $query;
  1699. } else {
  1700. // browsing by tag
  1701. $feed_kind = "Tags";
  1702. $result = db_query($link, "SELECT
  1703. ttrss_entries.id as id,title,
  1704. SUBSTRING(updated,1,16) as updated,
  1705. unread,feed_id,
  1706. marked,link,last_read,
  1707. SUBSTRING(last_read,1,19) as last_read_noms,
  1708. $vfeed_query_part
  1709. $content_query_part
  1710. SUBSTRING(updated,1,19) as updated_noms
  1711. FROM
  1712. ttrss_entries,ttrss_user_entries,ttrss_tags
  1713. WHERE
  1714. ref_id = ttrss_entries.id AND
  1715. ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
  1716. post_int_id = int_id AND tag_name = '$feed' AND
  1717. $view_query_part
  1718. $search_query_part
  1719. $query_strategy_part ORDER BY $order_by
  1720. $limit_query_part");
  1721. }
  1722. return array($result, $feed_title, $feed_site_url, $last_error);
  1723. }
  1724. function generate_syndicated_feed($link, $feed, $is_cat,
  1725. $search, $search_mode, $match_on) {
  1726. $qfh_ret = queryFeedHeadlines($link, $feed,
  1727. 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
  1728. $result = $qfh_ret[0];
  1729. $feed_title = htmlspecialchars($qfh_ret[1]);
  1730. $feed_site_url = $qfh_ret[2];
  1731. $last_error = $qfh_ret[3];
  1732. print "<rss version=\"2.0\">
  1733. <channel>
  1734. <title>$feed_title</title>
  1735. <link>$feed_site_url</link>
  1736. <generator>Tiny Tiny RSS v".VERSION."</generator>";
  1737. while ($line = db_fetch_assoc($result)) {
  1738. print "<item>";
  1739. print "<link>" . htmlspecialchars($line["link"]) . "</link>";
  1740. $rfc822_date = date('r', strtotime($line["updated"]));
  1741. print "<pubDate>$rfc822_date</pubDate>";
  1742. print "<title>" .
  1743. htmlspecialchars($line["title"]) . "</title>";
  1744. print "<description>" .
  1745. htmlspecialchars($line["content_preview"]) . "</description>";
  1746. print "</item>";
  1747. }
  1748. print "</channel></rss>";
  1749. }
  1750. function getCategoryTitle($link, $cat_id) {
  1751. $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
  1752. id = '$cat_id'");
  1753. if (db_num_rows($result) == 1) {
  1754. return db_fetch_result($result, 0, "title");
  1755. } else {
  1756. return "Uncategorized";
  1757. }
  1758. }
  1759. function sanitize_rss($str) {
  1760. $res = "";
  1761. $res = preg_replace('/<script.*?>/i',
  1762. "<p class=\"scriptWarn\">", $str);
  1763. $res = preg_replace('/<\/script>/i',
  1764. "</p>", $res);
  1765. return $res;
  1766. }
  1767. ?>