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