mobile-functions.php 15 KB

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