functions.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. <?php
  2. define('TTRSS_SESSION_NAME', 'ttrss_m_sid');
  3. /* TODO replace with interface to db-prefs */
  4. function mobile_pref_toggled($link, $id) {
  5. if (get_pref($link, "_MOBILE_$id"))
  6. return "true";
  7. else
  8. return "";
  9. }
  10. function mobile_get_pref($link, $id) {
  11. //return $_SESSION["mobile-prefs"][$id];
  12. return get_pref($link, "_MOBILE_$id");
  13. }
  14. function mobile_set_pref($link, $id, $value) {
  15. //$_SESSION["mobile-prefs"][$id] = $value;
  16. return set_pref($link, "_MOBILE_$id", $value);
  17. }
  18. function mobile_feed_has_icon($id) {
  19. $filename = "../".ICONS_DIR."/$id.ico";
  20. return file_exists($filename) && filesize($filename) > 0;
  21. }
  22. function render_flat_feed_list($link, $offset) {
  23. $owner_uid = $_SESSION["uid"];
  24. $limit = 0;
  25. if (!$offset) $offset = 0;
  26. if (mobile_get_pref($link, "SORT_FEEDS_UNREAD")) {
  27. $order_by = "unread DESC, title";
  28. } else {
  29. $order_by = "title";
  30. }
  31. if ($limit > 0) {
  32. $limit_qpart = "LIMIT $limit OFFSET $offset";
  33. } else {
  34. $limit_qpart = "";
  35. }
  36. $result = db_query($link, "SELECT id,
  37. title,
  38. (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
  39. WHERE feed_id = ttrss_feeds.id AND unread = true
  40. AND ttrss_user_entries.ref_id = ttrss_entries.id
  41. AND owner_uid = '$owner_uid') AS unread
  42. FROM ttrss_feeds
  43. WHERE
  44. ttrss_feeds.owner_uid = '$owner_uid'
  45. ORDER BY $order_by $limit_qpart");
  46. if (!$offset) print '<ul id="home" title="'.__('Home').'" selected="true"
  47. myBackLabel="'.__('Logout').'" myBackHref="logout.php" myBackTarget="_self">';
  48. // print "<li><a href='#cat-actions'>".__('Actions...')."</a></li>";
  49. $num_feeds = 0;
  50. while ($line = db_fetch_assoc($result)) {
  51. $id = $line["id"];
  52. $unread = $line["unread"];
  53. // $unread = rand(0, 100);
  54. if ($unread > 0) {
  55. $line["title"] = $line["title"] . " ($unread)";
  56. $class = '';
  57. } else {
  58. $class = 'oldItem';
  59. }
  60. if (mobile_feed_has_icon($id)) {
  61. $icon_url = "../".ICONS_URL."/$id.ico";
  62. } else {
  63. $icon_url = "../images/blank_icon.gif";
  64. }
  65. if ($unread > 0 || !mobile_get_pref($link, "HIDE_READ")) {
  66. print "<li class='$class'><a href='feed.php?id=$id'>" .
  67. "<img class='tinyIcon' src='$icon_url'/>".
  68. $line["title"] . "</a></li>";
  69. }
  70. ++$num_feeds;
  71. }
  72. /* $next_offset = $offset + $num_feeds;
  73. print "<li><a href=\"home.php?skip=$next_offset\"
  74. target=\"_replace\">Show more feeds...</a></li>"; */
  75. if (!$offset) print "</ul>";
  76. }
  77. function render_category($link, $cat_id, $offset) {
  78. $owner_uid = $_SESSION["uid"];
  79. if ($cat_id >= 0) {
  80. if ($cat_id != 0) {
  81. $cat_query = "cat_id = '$cat_id'";
  82. } else {
  83. $cat_query = "cat_id IS NULL";
  84. }
  85. if (mobile_get_pref($link, "SORT_FEEDS_UNREAD")) {
  86. $order_by = "unread DESC, title";
  87. } else {
  88. $order_by = "title";
  89. }
  90. $result = db_query($link, "SELECT id,
  91. title,
  92. (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
  93. WHERE feed_id = ttrss_feeds.id AND unread = true
  94. AND ttrss_user_entries.ref_id = ttrss_entries.id
  95. AND owner_uid = '$owner_uid') as unread
  96. FROM ttrss_feeds
  97. WHERE
  98. ttrss_feeds.owner_uid = '$owner_uid' AND
  99. $cat_query
  100. ORDER BY $order_by");
  101. $title = getCategoryTitle($link, $cat_id);
  102. print "<ul id='cat-$cat_id' title='$title' myBackLabel='".__("Home")."'
  103. myBackHref='home.php'>";
  104. // print "<li><a href='#cat-actions'>".__('Actions...')."</a></li>";
  105. while ($line = db_fetch_assoc($result)) {
  106. $id = $line["id"];
  107. $unread = $line["unread"];
  108. // $unread = rand(0, 100);
  109. if ($unread > 0) {
  110. $line["title"] = $line["title"] . " ($unread)";
  111. $class = '';
  112. } else {
  113. $class = 'oldItem';
  114. }
  115. if (mobile_feed_has_icon($id)) {
  116. $icon_url = "../".ICONS_URL."/$id.ico";
  117. } else {
  118. $icon_url = "../images/blank_icon.gif";
  119. }
  120. if ($unread > 0 || !mobile_get_pref($link, "HIDE_READ")) {
  121. print "<li class='$class'><a href='feed.php?id=$id&cat=$cat_id'>" .
  122. "<img class='tinyIcon' src='$icon_url'/>".
  123. $line["title"] . "</a></li>";
  124. }
  125. }
  126. print "</ul>";
  127. } else if ($cat_id == -1) {
  128. $title = __('Special');
  129. print "<ul id='cat--1' title='$title' myBackLabel='".__("Home")."'
  130. myBackHref='home.php'>";
  131. foreach (array(-4, -3, -1, -2, 0) as $id) {
  132. $title = getFeedTitle($link, $id);
  133. $unread = getFeedUnread($link, $id, false);
  134. $icon = getFeedIcon($id);
  135. if ($unread > 0) {
  136. $title = $title . " ($unread)";
  137. $class = '';
  138. } else {
  139. $class = 'oldItem';
  140. }
  141. if ($unread > 0 || !mobile_get_pref($link, "HIDE_READ")) {
  142. print "<li class='$class'>
  143. <a href='feed.php?id=$id&cat=-1'>
  144. <img class='tinyIcon' src='../$icon'/>$title</a></li>";
  145. }
  146. }
  147. print "</ul>";
  148. } else if ($cat_id == -2) {
  149. $title = __('Labels');
  150. print "<ul id='cat--2' title='$title' myBackLabel='".__("Home")."'
  151. myBackHref='home.php'>";
  152. $result = db_query($link, "SELECT id, caption FROM ttrss_labels2
  153. WHERE owner_uid = '$owner_uid'");
  154. $label_data = array();
  155. while ($line = db_fetch_assoc($result)) {
  156. $id = -$line["id"] - 11;
  157. $unread = getFeedUnread($link, $id);
  158. $title = $line["caption"];
  159. if ($unread > 0) {
  160. $title = $title . " ($unread)";
  161. $class = '';
  162. } else {
  163. $class = 'oldItem';
  164. }
  165. if ($unread > 0 || !mobile_get_pref($link, "HIDE_READ")) {
  166. print "<li class='$class'>
  167. <a href='feed.php?id=$id&cat=-2'>$title</a></li>";
  168. }
  169. }
  170. print "</ul>";
  171. }
  172. }
  173. function render_categories_list($link) {
  174. $owner_uid = $_SESSION["uid"];
  175. $cat_browse = mobile_get_pref($link, "BROWSE_CATS");
  176. print '<ul id="home" title="'.__('Home').'" selected="true"
  177. myBackLabel="'.__('Logout').'" myBackHref="logout.php" myBackTarget="_self">';
  178. // print "<li><a href='#searchForm'>Search...</a></li>";
  179. foreach (array(-1, -2) as $id) {
  180. $title = getCategoryTitle($link, $id);
  181. $unread = getFeedUnread($link, $id, true);
  182. if ($unread > 0) {
  183. $title = $title . " ($unread)";
  184. $class = '';
  185. } else {
  186. $class = 'oldItem';
  187. }
  188. if ($cat_browse)
  189. print "<li class='$class'><a href='cat.php?id=$id'>$title</a></li>";
  190. else
  191. print "<li class='$class'><a href='feed.php?id=$id&is_cat=true'>$title</a></li>";
  192. }
  193. $result = db_query($link, "SELECT
  194. ttrss_feed_categories.id,
  195. ttrss_feed_categories.title,
  196. COUNT(ttrss_feeds.id) AS num_feeds
  197. FROM ttrss_feed_categories, ttrss_feeds
  198. WHERE ttrss_feed_categories.owner_uid = $owner_uid
  199. AND ttrss_feed_categories.id = cat_id
  200. GROUP BY ttrss_feed_categories.id,
  201. ttrss_feed_categories.title
  202. ORDER BY ttrss_feed_categories.title");
  203. while ($line = db_fetch_assoc($result)) {
  204. if ($line["num_feeds"] > 0) {
  205. $unread = getFeedUnread($link, $line["id"], true);
  206. $id = $line["id"];
  207. if ($unread > 0) {
  208. $line["title"] = $line["title"] . " ($unread)";
  209. $class = '';
  210. } else {
  211. $class = 'oldItem';
  212. }
  213. if ($unread > 0 || !mobile_get_pref($link, "HIDE_READ")) {
  214. if ($cat_browse)
  215. print "<li class='$class'><a href='cat.php?id=$id'>" .
  216. $line["title"] . "</a></li>";
  217. else
  218. print "<li class='$class'><a href='feed.php?id=$id&is_cat=true'>".
  219. $line["title"] . "</a></li>";
  220. }
  221. }
  222. }
  223. $result = db_query($link, "SELECT COUNT(*) AS nf FROM ttrss_feeds WHERE
  224. cat_id IS NULL and owner_uid = '$owner_uid'");
  225. $num_feeds = db_fetch_result($result, 0, "nf");
  226. if ($num_feeds > 0) {
  227. $unread = getFeedUnread($link, 0, true);
  228. $title = "Uncategorized";
  229. if ($unread > 0) {
  230. $title = "$title ($unread)";
  231. $class = '';
  232. } else {
  233. $class = 'oldItem';
  234. }
  235. if ($unread > 0 || !mobile_get_pref($link, "HIDE_READ")) {
  236. if ($cat_browse)
  237. print "<li class='$class'><a href='cat.php?id=0'>$title</a></li>";
  238. else
  239. print "<li class='$class'><a href='feed.php?id=0&is_cat=true'>$title</a></li>";
  240. }
  241. }
  242. print "</ul>";
  243. }
  244. function render_headlines_list($link, $feed_id, $cat_id, $offset, $search,
  245. $is_cat = false) {
  246. $feed_id = $feed_id;
  247. $limit = 15;
  248. $filter = '';
  249. if (!mobile_get_pref($link, "HIDE_READ"))
  250. $view_mode = "all_articles";
  251. else
  252. $view_mode = 'adaptive';
  253. if ($search) {
  254. $search_mode = 'this_feed';
  255. $match_on = 'both';
  256. } else {
  257. $search_mode = '';
  258. $match_on = '';
  259. }
  260. $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
  261. $view_mode, $is_cat, $search, $search_mode, $match_on,
  262. "score DESC, date_entered DESC", $offset);
  263. $result = $qfh_ret[0];
  264. $feed_title = $qfh_ret[1];
  265. if (!$offset) {
  266. print "<form id=\"searchForm-$feed_id-$cat_id\" class=\"dialog\" method=\"POST\"
  267. action=\"feed.php\">
  268. <input type=\"hidden\" name=\"id\" value=\"$feed_id\">
  269. <input type=\"hidden\" name=\"cat\" value=\"$cat_id\">
  270. <fieldset>
  271. <h1>Search</h1>
  272. <a class=\"button leftButton\" type=\"cancel\">Cancel</a>
  273. <a class=\"button blueButton\" type=\"submit\">Search</a>
  274. <label>Search:</label>
  275. <input id=\"search\" type=\"text\" name=\"search\"/>
  276. </fieldset>
  277. </form>";
  278. if ($cat_id) {
  279. $cat_title = getCategoryTitle($link, $cat_id);
  280. print "<ul id=\"feed-$feed_id\" title=\"$feed_title\" selected=\"true\"
  281. myBackLabel='$cat_title' myBackHref='cat.php?id=$cat_id'>";
  282. } else {
  283. print "<ul id=\"feed-$feed_id\" title=\"$feed_title\" selected=\"true\"
  284. myBackLabel='".__("Home")."' myBackHref='home.php'>";
  285. }
  286. print "<li><a href='#searchForm-$feed_id-$cat_id'>Search...</a></li>";
  287. }
  288. $num_headlines = 0;
  289. while ($line = db_fetch_assoc($result)) {
  290. $id = $line["id"];
  291. $real_feed_id = $line["feed_id"];
  292. if (sql_bool_to_bool($line["unread"])) {
  293. $class = '';
  294. } else {
  295. $class = 'oldItem';
  296. }
  297. if (mobile_feed_has_icon($real_feed_id)) {
  298. $icon_url = "../".ICONS_URL."/$real_feed_id.ico";
  299. } else {
  300. $icon_url = "../images/blank_icon.gif";
  301. }
  302. print "<li class='$class'><a href='article.php?id=$id&feed=$feed_id&cat=$cat_id&is_cat=$is_cat'>
  303. <img class='tinyIcon' src='$icon_url'>";
  304. print $line["title"];
  305. print "</a></li>";
  306. ++$num_headlines;
  307. }
  308. if ($num_headlines == 0 && $search) {
  309. $articles_url = "feed.php?id=$feed_id&cat=$cat_id&skip=$next_offset";
  310. print "<li><a href=\"$articles_url\">" . __("Nothing found (click to reload feed).") . "</a></li>";
  311. }
  312. // print "<a target='_replace' href='feed.php?id=$feed_id&cat=$cat_id&skip=0'>Next $limit articles...</a>";
  313. $next_offset = $offset + $num_headlines;
  314. $num_unread = getFeedUnread($link, $feed_id, $is_cat);
  315. /* FIXME needs normal implementation */
  316. if ($num_headlines > 0 && ($num_unread == 0 || $num_unread > $next_offset)) {
  317. if ($is_cat) {
  318. $articles_url = "feed.php?id=$feed_id&skip=$next_offset".
  319. "&search=$search&is_cat=true";
  320. } else {
  321. $articles_url = "feed.php?id=$feed_id&cat=$cat_id&skip=$next_offset".
  322. "&search=$search";
  323. }
  324. print "<li><a href=\"$articles_url\"
  325. target=\"_replace\">Get more articles...</a></li>";
  326. }
  327. if (!$offset) print "</ul>";
  328. }
  329. function render_article($link, $id, $feed_id, $cat_id, $is_cat) {
  330. $query = "SELECT title,link,content,feed_id,comments,int_id,
  331. marked,unread,published,
  332. ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
  333. author
  334. FROM ttrss_entries,ttrss_user_entries
  335. WHERE id = '$id' AND ref_id = id AND owner_uid = " .
  336. $_SESSION["uid"] ;
  337. $result = db_query($link, $query);
  338. if (db_num_rows($result) != 0) {
  339. $line = db_fetch_assoc($result);
  340. $tmp_result = db_query($link, "UPDATE ttrss_user_entries
  341. SET unread = false,last_read = NOW()
  342. WHERE ref_id = '$id'
  343. AND owner_uid = " . $_SESSION["uid"]);
  344. $updated_fmt = make_local_datetime($link, $line['updated'], false);
  345. $title = $line["title"];
  346. $article_link = $line["link"];
  347. if (!$is_cat)
  348. $feed_title = getFeedTitle($link, $feed_id);
  349. else
  350. $feed_title = getCategoryTitle($link, $feed_id);
  351. print "<div class=\"panel\" id=\"article-$id\" title=\"$title\"
  352. selected=\"true\"
  353. myBackLabel='$feed_title' myBackHref='feed.php?id=$feed_id&cat=$cat_id&is_cat=$is_cat'>";
  354. if ($line['feed_id'] != $feed_id) {
  355. $real_feed_title = getFeedTitle($link, $line['feed_id']);
  356. $real_feed_id = $line['feed_id'];
  357. $feed_link = "(<a href=\"feed.php?id=$real_feed_id\">$real_feed_title</a>)";
  358. }
  359. // print "<fieldset>";
  360. print "<div style='float : right'>($updated_fmt)</div>";
  361. print "<h2><a target='_blank' href='$article_link'>$title</a> $feed_link</h2>";
  362. print "<hr>";
  363. /* print "<div class=\"row\">";
  364. print "<label id='title'><a target='_blank' href='$article_link'>$title</a></label>";
  365. print "</div>"; */
  366. $is_starred = (sql_bool_to_bool($line["marked"])) ? "true" : "false";
  367. $is_published = (sql_bool_to_bool($line["published"])) ? "true" : "false";
  368. //print "<div class=\"row\">";
  369. //print "<label id='updated'>Updated:</label>";
  370. //print "<input type='text' enabled='false' name='updated' disabled value='$updated_fmt'/>";
  371. //print "</div>";
  372. // print "</fieldset>";
  373. $content = sanitize_rss($link, $line["content"]);
  374. $content = preg_replace("/href=/i", "target=\"_blank\" href=", $content);
  375. if (!mobile_get_pref($link, "SHOW_IMAGES")) {
  376. $content = preg_replace('/<img[^>]+>/is', '', $content);
  377. }
  378. print "<p>$content</p>";
  379. print "<div class='nav'>
  380. <label>Navigation</label>
  381. <div class='button left' onclick='goPrev($id, $feed_id, this)'>Prev</div>
  382. <div class='button right' onclick='goNext($id, $feed_id, this)'>Next</div>
  383. </div>";
  384. print "<fieldset>";
  385. print "<div class=\"row\">
  386. <label>Starred</label>
  387. <div class=\"toggle\" onclick=\"toggleMarked($id, this)\" toggled=\"$is_starred\"><span class=\"thumb\"></span><span class=\"toggleOn\">ON</span><span class=\"toggleOff\">OFF</span></div>
  388. </div>";
  389. print "<div class=\"row\">
  390. <label>Published</label>
  391. <div class=\"toggle\" onclick=\"togglePublished($id, this)\" toggled=\"$is_published\"><span class=\"thumb\"></span><span class=\"toggleOn\">ON</span><span class=\"toggleOff\">OFF</span></div>
  392. </div>";
  393. print "<div class=\"row\">
  394. <label>Unread</label>
  395. <div class=\"toggle\" onclick=\"toggleUnread($id, this)\" toggled=\"$is_unread\"><span class=\"thumb\"></span><span class=\"toggleOn\">ON</span><span class=\"toggleOff\">OFF</span></div>
  396. </div>";
  397. print "</fieldset>";
  398. print "</div>";
  399. }
  400. }
  401. ?>