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