aboutsummaryrefslogtreecommitdiff
path: root/public/application/controllers/api/Rss.php
blob: 021ef852842208cb3aff3b006199031b2a08dccf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH . '/libraries/REST_Controller.php';

/**
 * Rest Server for Newsbeuter RSS Feeds application.
 * This is user interaction rest/api methods to get
 * RSS feeds item/info and meta from newsbeuter database and related files.
 *
 * @package         CodeIgniter
 * @subpackage      Controller
 * @category        Controller
 * @author          V.Krishn
 * @license         MIT
 * @link            https://github.com/insteps/nbreader
 */
class Rss extends REST_Controller {

    //protected $newsbeuter = array();

    /**
     * Constructor for the REST API
     *
     * @access public
     *
     */
    public function __construct()
    {
        // Construct the parent class
        parent::__construct();

        // Configure limits on our controller methods # TODO
        // Ensure you have created the 'limits' table and enabled 'limits'
        // within application/config/rest.php
        //$this->methods['item_get']['limit'] = 500; //500 requests per hour per user/key
        //$this->methods['appdb_get']['limit'] = 100; //100 requests per hour per user/key
        //$this->methods['user_delete']['limit'] = 50; //50 requests per hour per user/key

        # newsbeuter load config
        $this->load->config('newsbeuter', TRUE);
        $c = $this->config->item('newsbeuter');

        # newsbeuter set db
        $db = $c['newsbeuter']['db'];
        $dbpath = $c['newsbeuter']['be']['dbpath'];

        # check get/request
        $dbname = $this->get('cat');

        $this->load->library('newsbeuter');

        $class = $this->uri->rsegment(2, 0);

        if ($class == 'appdb' || $class == 'icon')
        {
            # test
            if ( file_exists($c['newsbeuter']['be']['confdb']) )
            {
                $this->load->database($c['newsbeuter']['be']['db']);
            }
            else
            {
                $this->response(['error' => 'Invalid App db'], 404);
            }

            $this->load->database($db);
        }
        if ($class == 'urls' || $class == 'feed'
             || $class == 'item' || $class == 'count'
             || $class == 'unread_'
           )
        {
            # verify valid/active dbname
            if ($this->_check_valid_dbname($dbname)
                && file_exists($dbpath."/$dbname.loc.db")
               )
            {
                $db['database'] = $dbpath."/$dbname.loc.db";
            }
            else
            {
                if ( $class == 'index' )
                {
                    $this->response(['error' => 'Invalid method'], 404);
                }
                $this->response(['error' => 'Invalid category'], 404);
            }

            $this->load->database($db);
        }

        //$this->load->database($db);

    }

    public function version_get()
    {
        ## Api urls example (api baseurl: http://localhost/nbreader/api/rss)
        #    (read contents of VERSION)
        # 
        # /version : 
        # version      = Method/Function call (get application version)
        #
        $data = array();
        $data['version'] = $this->newsbeuter->get_version();
        if ($data)
        {
            $this->response($data, 200);
        }
        else
        {
            $this->response(['error' => 'Version file not found'], 404);
        }
    }

    public function about_get() //OR apimeta_get ?
    {
        $data = array();
        $data['version'] = $this->newsbeuter->get_version();
        $data['backend'] = 'newsbeuter';
        $data['apiurl'] = $this->newsbeuter->get_rss_api_url();
        $data['feedsurl'] = $this->newsbeuter->get_local_feed_baseurl();
        $data['lastfetch'] = '';
        if ($data)
        {
            $this->response($data, 200);
        }
        else
        {
            $this->response(['error' => 'Version file not found'], 404);
        }
    }

    public function template_get()
    {
    }

    public function category_get()
    {
        ## Api urls example (api baseurl: http://localhost/nbreader/api/rss)
        #    (read contents of config/dbname)
        # 
        # /category : 
        # category      = Method/Function call (get list of databases i.e. dbnames)
        #

        $data = array();
        $this->load->library('newsbeuter');
        $data['category'] = $this->newsbeuter->get_local_catogory();
        $data['backend'] = 'newsbeuter';

        if ($data['category'])
        {
            $this->response($data, 200); // 200 being the HTTP response code
        }
        else
        {
            $this->response(['error' => 'Category list not found'], 404);
        }

    }

    public function urls_get()
    {
        ## Api urls example (api baseurl: http://localhost/nbreader/api/rss)
        #    (read contents of config/url.local/<dbname>)
        # 
        # /urls/cat/dev : 
        # urls      = Method/Function call
        #   cat/<dbname>         = Load category/database named <dbname>
        #

        $data['dbname'] = $this->get('cat');
        $data['feedurls'] = $this->newsbeuter->get_local_feed_urls($data['dbname']);
        $data['backend'] = 'newsbeuter';

        if ($data['feedurls'])
        {
            $this->response($data, 200); // 200 being the HTTP response code
        }
        else
        {
            $this->response(['error' => 'Feed urls could not be found'], 404);
        }

    }

    public function appdb_get() # TODO
    {
        ## Api urls example (api baseurl: http://localhost/nbreader/api/rss)
        #    (get from database config/urls.db)
        # 
        # /appdb/hash/<sha1sum>/cat/dev/tags/news~bbc.co.uk : 
        # appdb      = Method/Function call
        #   hash/<sha1sum>        = Fetch rss item by hash (filter records by hash)
        #   cat/<dbname>          = Load category/database named <dbname>
        #   tags/<rss category>   = Rss category (`~` as separator)
        #

        $this->load->model('newsbeuter/Newsbeuter_rss_url_model', 'rss_url');
        $search_options = array();
        $dbname = trim($this->get('cat'));

        $tags = trim($this->get('tags'));
        $tags = '/'.str_replace('~', '/', $tags);
        $tags = preg_replace('#/+#', '/', $tags);
        $tags = rtrim($tags, '/');

        $limit = 1;

        if ( $this->get('hash') && strlen($this->get('hash')) >= 20 )
        {
            $hash = $this->get('hash');
            $search_options['sha1sum'] = $hash;
            $limit = 1;
        } else {
            if( $dbname !== '' ) {
                $search_options['dbname'] = $dbname;
                $limit = 10; # TODO
            }
            if( $tags !== '' ) {
                $search_options['tags'] = $tags;
                $limit = 0; //no limit, get all by tags
            }
        }

        $data = $this->rss_url->get_rss_url($search_options, $limit, 0, 0);
        $data['backend'] = 'newsbeuter';

        if ($data)
        {
            $this->response($data, 200); // 200 being the HTTP response code
        }
        else
        {
            $this->response(['error' => 'Rss feed info not found'], 404);
        }

    }

    public function feed_get()
    {
        ## Api urls example (api baseurl: http://localhost/nbreader/api/rss)
        # 
        # /feed/cat/dev/row/<limit>-<offset>/hash/<sha1sum> : 
        # feed      = Method/Function call
        #   cat/<dbname>         = Load category/database named <dbname>
        #   row/<limit>-<offset> = Fetch number of rows = <limit> starting at <offset>
        #   hash/<sha1sum>       = Fetch rss feeds list (with hash row values has no effect)
        #

        $this->load->model('newsbeuter/newsbeuter_rss_feed_model', 'rssfeed');
        $data['dbname'] = $this->get('cat');
        $data['feedsurl'] = $this->newsbeuter->get_local_feed_baseurl();
        $search_options = array();
        $search_options['id'] = NULL; //there is not 'id'

        # add feedurl search_options, sha1sum eg. ffb1840d0a0c9bc303887e26277d7e28f7f31cad
        if ( $this->get('hash') && strlen($this->get('hash')) >= 20 )
        {
            $feedurl = $this->get('hash');
            $feedurl = $data['feedsurl'] .'/'. $feedurl[0] .'/'. $feedurl[0].$feedurl[1] .'/'. $feedurl . '.xml';
            $search_options['rssurl'] = $feedurl;
            $rec = explode('-', '0-0-0');
        }
        else
        {
            $rec = ($this->get('row')) ? explode('-', $this->get('row').'-0-0') : explode('-', '0-0-0');
        }
        $limit = ((int)$rec[0] >= 1) ? (int)$rec[0] : 0;
        $offset = ((int)$rec[1] >= 1) ? (int)$rec[1] : 0;
        $total_rows = ((int)$rec[2] >= 1) ? (int)$rec[2] : 0;

        $data['query'] = $this->rssfeed->get_rss_feed($search_options, $limit, $offset, $total_rows);
        $data['backend'] = 'newsbeuter';

        if ($data)
        {
            $this->response($data, 200); // 200 being the HTTP response code
        }
        else
        {
            $this->response(['error' => 'Feed item could not be found'], 404);
        }

    }

    public function item_get()
    {
        ## Api urls example (api baseurl: http://localhost/nbreader/api/rss)
        # 
        # /item/cat/dev/row/<limit>-<offset>/id/<idnum> : 
        # item      = Method/Function call
        #   cat/<dbname>         = Load category/database named <dbname>
        #   row/<limit>-<offset> = Fetch number of rows = <limit> starting at <offset>
        #   id/<idnum>           = Fetch rss item by id (with id row values has no effect)
        #   hash/<sha1sum>       = Fetch rss item by hash (filter records by hash)
        #   filter/<default>     = Apply security filter on articles
        #   unread               = yes|no (show list that is read or unread)
        #

        $this->load->model('newsbeuter/newsbeuter_rss_item_model', 'rss_item');
        $data['dbname'] = $this->get('cat');
        $data['feedsurl'] = $this->newsbeuter->get_local_feed_baseurl();
        $search_options = array();

        if ( (int)$this->get('id') >= 1 )
        {
            $search_options['id'] = (int)$this->get('id');
            $rec = explode('-', '0-0-0');
        }
        else
        {
            $rec = ($this->get('row')) ? explode('-', $this->get('row').'-0-0') : explode('-', '0-0-0');
        }
        $limit = ((int)$rec[0] >= 1) ? (int)$rec[0] : 1; // minimum 1 latest record
        $limit = ($limit >= 10) ? 10 : $limit; // maxlimit=10 records
        $offset = ((int)$rec[1] >= 1) ? (int)$rec[1] : 0;
        $total_rows = ((int)$rec[2] >= 1) ? (int)$rec[2] : 0;

        # add feedurl search_options for sha1sum
        #   eg. ffb1840d0a0c9bc303887e26277d7e28f7f31cad
        #   you need to know the corrent dbname for sha1sum 
        if ( $this->get('hash') && strlen($this->get('hash')) >= 20 )
        {
            $feedurl = $this->get('hash');
            $feedurl = $data['feedsurl'] .'/'. $feedurl[0] .'/'. $feedurl[0].$feedurl[1] .'/'. $feedurl . '.xml';
            $search_options['feedurl'] = $feedurl;

            ## get items count all by feedurl/hash
            $_d = $this->rss_item->get_rss_item_count($search_options);
            $data['count'] = $_d[$feedurl]['count'];
            ## get items count unread by feedurl/hash
            $search_options['unread'] = '1';
            $_d = $this->rss_item->get_rss_item_count($search_options);
            $data['count_unread'] = @$_d[$feedurl]['count'] ? $_d[$feedurl]['count'] : 0;
            unset($search_options['unread']);
        }

        ## Security/DNT related filter on RSS contents/text
        ## Experimental and may change in future
        if($this->get('filter'))
        {
            $search_options['filter'] = array();
            $temp = explode('~', $this->get('filter'));
            foreach($temp as $v)
            {
                $search_options['filter'][$v] = 'yes';
            }
        }

        $search_options['order_by'] =  'pubDate DESC';
        if($this->get('orderby'))
        {
          $search_options['order_by'] =  str_replace('~', ' ', $this->get('orderby'));
        }

        $unread = strtolower($this->get('unread'));
        if($unread == 'yes' || $unread == 'no')
        {
            $search_options['unread'] = ($unread == 'yes') ? '1' : '0';
        }

        $data['query'] = $this->rss_item->get_rss_item($search_options, $limit, $offset, $total_rows);
        $data['backend'] = 'newsbeuter';

        if ($data)
        {
            $this->response($data, 200); // 200 being the HTTP response code
        }
        else
        {
            $this->response(['error' => 'Rss item not found'], 404);
        }

    }

    public function count_get()
    {
        ## Api urls example (api baseurl: http://localhost/nbreader/api/rss)
        # 
        # /count/cat/dev/unread/yes : 
        # count     = Method/Function call
        #   cat/<dbname>         = Load category/database named <dbname>
        #   unread               = yes|no (show list that is read or unread)
        #

        $this->load->model('newsbeuter/newsbeuter_rss_item_model', 'rss_item');
        $data['dbname'] = $this->get('cat');
        $data['feedsurl'] = $this->newsbeuter->get_local_feed_baseurl();
        $search_options = array();

        $unread = strtolower($this->get('unread'));
        if($unread == 'yes' || $unread == 'no')
        {
            $search_options['unread'] = ($unread == 'yes') ? '1' : '0';
        }

        # add feedurl search_options, sha1sum eg. ffb1840d0a0c9bc303887e26277d7e28f7f31cad
        if ( $this->get('hash') && strlen($this->get('hash')) >= 20 )
        {
            $feedurl = $this->get('hash');
            $feedurl = $data['feedsurl'] .'/'. $feedurl[0] .'/'. $feedurl[0].$feedurl[1] .'/'. $feedurl . '.xml';
            $search_options['feedurl'] = $feedurl;

            # feedurl + count ONE /unread/read
            $data['query'] = $this->rss_item->get_rss_item_count($search_options);
        } else {
            # bygroup feedurl + count ALL /unread/read
            // $data['query'] = $this->rss_item->get_count($search_options); //slow
            $data['query'] = $this->rss_item->get_rss_item_count($search_options);
        }
        $data['backend'] = 'newsbeuter';

        if ($data)
        {
            $this->response($data, 200); // 200 being the HTTP response code
        }
        else
        {
            $this->response(['error' => 'Feed item could not be found'], 404);
        }

    }

    # Get feed/rss url's meta/info
    #   eg. title, url, rssurl(this is id) (for node)
    #   from <dbname> table->rss_feed
    public function meta_get()
    {
        ## Api urls example (api baseurl: http://localhost/nbreader/api/rss)
        # 
        # /meta/cat/dev/tag/<tag>/unread/<yes|no>/refresh/<yes|no> : 
        # meta      = Method/Function call
        #   cat/<dbname>        = Load from category/database named <dbname>
        #   tag/<rss category>  = Rss category (`~` as separator, eg business~seo~seochat.com)
        #   unread/<yes|no>     = yes|no (get list that is either read or unread)
        #   refresh/<yes|no>    = yes|no (use cached data)
        #   icon/<yes|no>       = yes|no (include icon data for items) # TODO
        #

        $this->load->model('newsbeuter/newsbeuter_model', 'newsbeuter_model');

        $opts = array(); $data = array();
        $opts['dbname'] = $this->get('cat');
        $unread = strtolower($this->get('unread'));
        if($unread == 'yes' || $unread == 'no')
        {
            $opts['unread'] = $unread;
        }
        $tag = $this->get('tag');
        $tag = str_replace('~', '/', $tag); //browser js api call

        $refresh = (strtolower($this->get('refresh')) == 'yes') ? 'yes' : 'no';
        $opts['refresh'] = $refresh;

        $formats = array('json', 'xml', 'php');
        $format = $opts['format'] = strtolower($this->get('format'));
        if ( ! $format || ! in_array($format, $formats) )
        {
                $opts['format'] = 'json';
        }

        $data = $this->newsbeuter_model->_get_meta($opts, $tag);
        $data['apiurl'] = $this->newsbeuter->get_rss_api_url();
        $data['feedsurl'] = $this->newsbeuter->get_local_feed_baseurl();
        $data['backend'] = 'newsbeuter';
        if( $tag == '' ) // homepage welcome default call
        {
            $data['jsconf'] = $this->newsbeuter->get_frontend_jsconf();
        }

        if (isset($data['_by_cat']))
        {
            $this->response($data, 200); // 200 being the HTTP response code
        }
        else
        {
            $this->response(['error' => 'Meta list error'], 404);
        }

    }

    public function icon_get()
    {
        ## Api urls example (api baseurl: http://localhost/nbreader/api/rss)
        #
        # /icon/cat/dev/tag/<tag>/hash/<sha1sum>/refresh/<yes|no> :
        # icon      = Method/Function call
        #   hash/<sha1sum>              = Fetch icons by hash (filter records by hash)
        #   cat/<dbname>                = Load from category/database named <dbname>
        #   group/<sha1sum 1st letter>  = Fetch icons by hash group (filter records by sha1sum 1st letter) # TODO
        #   tag/<rss category>          = Rss category (`~` as separator, eg business~seo~seochat.com) # TODO
        #   refresh/<yes|no>            = yes|no (use cached data)
        #

        $this->load->model('newsbeuter/newsbeuter_icon', 'newsbeuter_icon');

        $opts = array(); $data = array();

        $opts['dbname'] = $this->get('cat');

        # add hash search_options, sha1sum eg. ffb1840d0a0c9bc303887e26277d7e28f7f31cad
        if ( $this->get('hash') && strlen($this->get('hash')) >= 20 )
        {
            $opts['sha1sum'] = $this->get('hash');
        }

        //$refresh = (strtolower($this->get('refresh')) == 'yes') ? 'yes' : 'no';
        //$opts['refresh'] = $refresh;

        $data = $this->newsbeuter_icon->get_icon($opts);
        $data['opts'] = $opts;

        if (isset($data['icon']))
        {
            $this->response($data, 200); // 200 being the HTTP response code
        }
        else
        {
            $this->response(['error' => 'Icon list error'], 404);
        }

    }


    /**
     * Write routines
     */

    // http://localhost/nbreader/api/rss/unread_/id/458/cat/dev/unread/no
    public function unread__get()
    {
        ## Api urls example (api baseurl: http://localhost/nbreader/api/rss)
        # 
        # /unread_/cat/dev/id/<idnum>/unread/<yes|no> : 
        # unread_      = Method/Function call
        #   cat/<dbname>       = Update category/database named <dbname>
        #   id/<idnum>         = update rss item for <id>
        #   unread/<yes|no>    = update item to read or unread
        #

        $this->load->model('newsbeuter/newsbeuter_rss_item_model', 'rss_item');
        $data['dbname'] = $this->get('cat');
        $data['feedsurl'] = $this->newsbeuter->get_local_feed_baseurl();
        $search_options = array();

        if ( (int)$this->get('id') >= 1 )
        {
            $search_options['id'] = (int)$this->get('id');
        }

        $unread = strtolower($this->get('unread'));
        if($unread == 'yes' || $unread == 'no')
        {
            $search_options['unread'] = ($unread == 'yes') ? '1' : '0';
        }

        $data['result'] = $this->rss_item->update_rss_item_read($search_options);
        $data['backend'] = 'newsbeuter';

        if ($data['result'])
        {
            $this->response($data, 200); // 200 being the HTTP response code
        }
        else
        {
            $this->response(['error' => 'Feed item could not be found'], 404);
        }


    }






    public function item_get_new()
    {
        // if (!$this->get('id'))
        // {
        //     $this->response(NULL, 400);
        // }

    }






    protected function _check_valid_dbname($name)
    {
        $this->load->library('newsbeuter');
        return $this->newsbeuter->check_valid_dbname($name);
    }








}