22
33class API extends Handler {
44
5- const API_LEVEL = 7 ;
5+ const API_LEVEL = 9 ;
66
77 const STATUS_OK = 0 ;
88 const STATUS_ERR = 1 ;
@@ -77,6 +77,7 @@ function login() {
7777 $ this ->wrap (self ::STATUS_OK , array ("session_id " => session_id (),
7878 "api_level " => self ::API_LEVEL ));
7979 } else { // else we are not logged in
80+ user_error ("Failed login attempt for $ login from {$ _SERVER ['REMOTE_ADDR ' ]}" , E_USER_WARNING );
8081 $ this ->wrap (self ::STATUS_ERR , array ("error " => "LOGIN_ERROR " ));
8182 }
8283 } else {
@@ -199,11 +200,15 @@ function getHeadlines() {
199200 $ include_nested = sql_bool_to_bool ($ _REQUEST ["include_nested " ]);
200201 $ sanitize_content = !isset ($ _REQUEST ["sanitize " ]) ||
201202 sql_bool_to_bool ($ _REQUEST ["sanitize " ]);
203+ $ force_update = sql_bool_to_bool ($ _REQUEST ["force_update " ]);
202204
203205 $ override_order = false ;
204206 switch ($ _REQUEST ["order_by " ]) {
207+ case "title " :
208+ $ override_order = "ttrss_entries.title " ;
209+ break ;
205210 case "date_reverse " :
206- $ override_order = "date_entered, updated " ;
211+ $ override_order = "score DESC, date_entered, updated " ;
207212 break ;
208213 case "feed_dates " :
209214 $ override_order = "updated DESC " ;
@@ -218,7 +223,7 @@ function getHeadlines() {
218223 $ headlines = $ this ->api_get_headlines ($ feed_id , $ limit , $ offset ,
219224 $ filter , $ is_cat , $ show_excerpt , $ show_content , $ view_mode , $ override_order ,
220225 $ include_attachments , $ since_id , $ search , $ search_mode ,
221- $ include_nested , $ sanitize_content );
226+ $ include_nested , $ sanitize_content, $ force_update );
222227
223228 $ this ->wrap (self ::STATUS_OK , $ headlines );
224229 } else {
@@ -309,8 +314,8 @@ function getArticle() {
309314
310315 if ($ article_id ) {
311316
312- $ query = "SELECT id,title,link,content,cached_content, feed_id,comments,int_id,
313- marked,unread,published,score,
317+ $ query = "SELECT id,title,link,content,feed_id,comments,int_id,
318+ marked,unread,published,score,note,lang,
314319 " .SUBSTRING_FOR_DATE ."(updated,1,16) as updated,
315320 author,(SELECT title FROM ttrss_feeds WHERE id = feed_id) AS feed_title
316321 FROM ttrss_entries,ttrss_user_entries
@@ -338,11 +343,13 @@ function getArticle() {
338343 "comments " => $ line ["comments " ],
339344 "author " => $ line ["author " ],
340345 "updated " => (int ) strtotime ($ line ["updated " ]),
341- "content " => $ line ["cached_content " ] != "" ? $ line [ " cached_content " ] : $ line [ " content " ],
346+ "content " => $ line ["content " ],
342347 "feed_id " => $ line ["feed_id " ],
343348 "attachments " => $ attachments ,
344349 "score " => (int )$ line ["score " ],
345- "feed_title " => $ line ["feed_title " ]
350+ "feed_title " => $ line ["feed_title " ],
351+ "note " => $ line ["note " ],
352+ "lang " => $ line ["lang " ]
346353 );
347354
348355 foreach (PluginHost::getInstance ()->get_hooks (PluginHost::HOOK_RENDER_ARTICLE_API ) as $ p ) {
@@ -423,14 +430,14 @@ function getLabels() {
423430
424431 $ checked = false ;
425432 foreach ($ article_labels as $ al ) {
426- if ($ al [0 ] == $ line ['id ' ]) {
433+ if (feed_to_label_id ( $ al [0 ]) == $ line ['id ' ]) {
427434 $ checked = true ;
428435 break ;
429436 }
430437 }
431438
432439 array_push ($ rv , array (
433- "id " => (int )$ line ['id ' ],
440+ "id " => (int )label_to_feed_id ( $ line ['id ' ]) ,
434441 "caption " => $ line ['caption ' ],
435442 "fg_color " => $ line ['fg_color ' ],
436443 "bg_color " => $ line ['bg_color ' ],
@@ -447,7 +454,7 @@ function setArticleLabel() {
447454 $ assign = (bool ) $ this ->dbh ->escape_string ($ _REQUEST ['assign ' ]) == "true " ;
448455
449456 $ label = $ this ->dbh ->escape_string (label_find_caption (
450- $ label_id , $ _SESSION ["uid " ]));
457+ feed_to_label_id ( $ label_id) , $ _SESSION ["uid " ]));
451458
452459 $ num_updated = 0 ;
453460
@@ -511,7 +518,7 @@ static function api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_n
511518 if ($ unread || !$ unread_only ) {
512519
513520 $ row = array (
514- "id " => $ cv ["id " ],
521+ "id " => ( int ) $ cv ["id " ],
515522 "title " => $ cv ["description " ],
516523 "unread " => $ cv ["counter " ],
517524 "cat_id " => -2 ,
@@ -557,7 +564,7 @@ static function api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_n
557564
558565 if ($ unread || !$ unread_only ) {
559566 $ row = array (
560- "id " => $ line ["id " ],
567+ "id " => ( int ) $ line ["id " ],
561568 "title " => $ line ["title " ],
562569 "unread " => $ unread ,
563570 "is_cat " => true ,
@@ -626,7 +633,28 @@ static function api_get_headlines($feed_id, $limit, $offset,
626633 $ filter , $ is_cat , $ show_excerpt , $ show_content , $ view_mode , $ order ,
627634 $ include_attachments , $ since_id ,
628635 $ search = "" , $ search_mode = "" ,
629- $ include_nested = false , $ sanitize_content = true ) {
636+ $ include_nested = false , $ sanitize_content = true , $ force_update = false ) {
637+
638+ if ($ force_update && $ feed_id > 0 && is_numeric ($ feed_id )) {
639+ // Update the feed if required with some basic flood control
640+
641+ $ result = db_query (
642+ "SELECT cache_images, " .SUBSTRING_FOR_DATE ."(last_updated,1,19) AS last_updated
643+ FROM ttrss_feeds WHERE id = ' $ feed_id' " );
644+
645+ if (db_num_rows ($ result ) != 0 ) {
646+ $ last_updated = strtotime (db_fetch_result ($ result , 0 , "last_updated " ));
647+ $ cache_images = sql_bool_to_bool (db_fetch_result ($ result , 0 , "cache_images " ));
648+
649+ if (!$ cache_images && time () - $ last_updated > 120 ) {
650+ include "rssfuncs.php " ;
651+ update_rss_feed ($ feed_id , true , true );
652+ } else {
653+ db_query ("UPDATE ttrss_feeds SET last_updated = '1970-01-01', last_update_started = '1970-01-01'
654+ WHERE id = ' $ feed_id' " );
655+ }
656+ }
657+ }
630658
631659 $ qfh_ret = queryFeedHeadlines ($ feed_id , $ limit ,
632660 $ view_mode , $ is_cat , $ search , $ search_mode ,
@@ -638,11 +666,31 @@ static function api_get_headlines($feed_id, $limit, $offset,
638666 $ headlines = array ();
639667
640668 while ($ line = db_fetch_assoc ($ result )) {
669+ $ line ["content_preview " ] = truncate_string (strip_tags ($ line ["content " ]), 100 );
670+ foreach (PluginHost::getInstance ()->get_hooks (PluginHost::HOOK_QUERY_HEADLINES ) as $ p ) {
671+ $ line = $ p ->hook_query_headlines ($ line , 100 , true );
672+ }
673+
641674 $ is_updated = ($ line ["last_read " ] == "" &&
642675 ($ line ["unread " ] != "t " && $ line ["unread " ] != "1 " ));
643676
644677 $ tags = explode (", " , $ line ["tag_cache " ]);
645- $ labels = json_decode ($ line ["label_cache " ], true );
678+
679+ $ label_cache = $ line ["label_cache " ];
680+ $ labels = false ;
681+
682+ if ($ label_cache ) {
683+ $ label_cache = json_decode ($ label_cache , true );
684+
685+ if ($ label_cache ) {
686+ if ($ label_cache ["no-labels " ] == 1 )
687+ $ labels = array ();
688+ else
689+ $ labels = $ label_cache ;
690+ }
691+ }
692+
693+ if (!is_array ($ labels )) $ labels = get_article_labels ($ line ["id " ]);
646694
647695 //if (!$tags) $tags = get_article_tags($line["id"]);
648696 //if (!$labels) $labels = get_article_labels($line["id"]);
@@ -660,28 +708,22 @@ static function api_get_headlines($feed_id, $limit, $offset,
660708 "tags " => $ tags ,
661709 );
662710
663- if ($ include_attachments )
664- $ headline_row ['attachments ' ] = get_article_enclosures (
665- $ line ['id ' ]);
711+ if ($ include_attachments )
712+ $ headline_row ['attachments ' ] = get_article_enclosures (
713+ $ line ['id ' ]);
666714
667- if ($ show_excerpt ) {
668- $ excerpt = truncate_string (strip_tags ($ line ["content_preview " ]), 100 );
669- $ headline_row ["excerpt " ] = $ excerpt ;
670- }
715+ if ($ show_excerpt )
716+ $ headline_row ["excerpt " ] = $ line ["content_preview " ];
671717
672718 if ($ show_content ) {
673719
674- if ($ line ["cached_content " ] != "" ) {
675- $ line ["content_preview " ] =& $ line ["cached_content " ];
676- }
677-
678720 if ($ sanitize_content ) {
679721 $ headline_row ["content " ] = sanitize (
680- $ line ["content_preview " ],
722+ $ line ["content " ],
681723 sql_bool_to_bool ($ line ['hide_images ' ]),
682- false , $ line ["site_url " ]);
724+ false , $ line ["site_url " ], false , $ line [ " id " ] );
683725 } else {
684- $ headline_row ["content " ] = $ line ["content_preview " ];
726+ $ headline_row ["content " ] = $ line ["content " ];
685727 }
686728 }
687729
@@ -699,7 +741,10 @@ static function api_get_headlines($feed_id, $limit, $offset,
699741 $ headline_row ["always_display_attachments " ] = sql_bool_to_bool ($ line ["always_display_enclosures " ]);
700742
701743 $ headline_row ["author " ] = $ line ["author " ];
744+
702745 $ headline_row ["score " ] = (int )$ line ["score " ];
746+ $ headline_row ["note " ] = $ line ["note " ];
747+ $ headline_row ["lang " ] = $ line ["lang " ];
703748
704749 foreach (PluginHost::getInstance ()->get_hooks (PluginHost::HOOK_RENDER_ARTICLE_API ) as $ p ) {
705750 $ headline_row = $ p ->hook_render_article_api (array ("headline " => $ headline_row ));
0 commit comments