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
|
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Newsbeuter class
* Helper + config functions for backend and frontend.
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author V.Krishn
* @license MIT
* @link https://github.com/insteps/nbreader
*/
class Newsbeuter
{
// --------------------------------------------------------------------------
/**
* Reference to CodeIgniter instance
*
* @var object
*/
protected $CI;
/**
* Contents of the newsbeuter
*
* @var array
*/
protected $_newsbeuter_contents = array();
/**
* Contents of the newsbeuter
*
* @var array
*/
protected $_newsbeuter_config = array();
/**
* Data to parse
*
* @var mixed
*/
protected $_data = [];
/**
* Newsbeuter Class Constructor
*
* The constructor loads the Session class, used to store the newsbeuter contents.
*
* @param array
* @return void
*/
public function __construct($params = array())
{
// Set the super object to a local variable for use later
$this->CI =& get_instance();
# Load newsbeuter config file/data
$this->CI->load->config('newsbeuter', TRUE);
$this->_newsbeuter_config = $this->CI->config->item('newsbeuter');
// Are any config settings being passed manually? If so, set them
$config = is_array($params) ? $params : array(); # TODO
// Load the Sessions class
$this->CI->load->driver('session', $config);
// Grab the newsbeuter array from the session table # TODO
$this->_newsbeuter_contents = $this->CI->session->userdata('newsbeuter_contents');
if ($this->_newsbeuter_contents === NULL)
{
// No newsbeuter exists so we'll set some base values
$this->_newsbeuter_contents = array('newsbeuter_total' => 0, 'total_items' => 0);
}
log_message('info', 'newsbeuter Class Initialized');
}
// --------------------------------------------------------------------
public function get_version()
{
$file = $this->_newsbeuter_config['newsbeuter']['be']['path'].'/VERSION';
if (file_exists($file))
{
$flist = preg_split( '/[\\n\\s]/', file_get_contents($file) );
return trim($flist[0]);
}
return '';
}
public function get_rss_api_url()
{
return $this->_newsbeuter_config['newsbeuter']['be']['rss_api_url'];
}
public function get_local_feed_baseurl()
{
$file = $this->_newsbeuter_config['newsbeuter']['be']['confpath'].'/feedsurl';
if (file_exists($file))
{
$flist = preg_split( '/[\\n\\s]/', file_get_contents($file) );
return trim($flist[0]);
}
return '';
}
public function get_local_catogory()
{
$c = $this->_newsbeuter_config;
$file = $c['newsbeuter']['be']['path'].'/config/dbname';
if (file_exists($file))
{
$flist = preg_split( '/[\\n]/', file_get_contents($file) );
return $flist;
}
return '';
}
public function check_valid_dbname($name = '')
{
$c = $this->_newsbeuter_config;
$file = $c['newsbeuter']['be']['path'].'/config/dbname';
if ( ! file_exists($file))
{
return '';
}
$file = file_get_contents($file);
$flist = preg_split( '/[\\n\\s]/', $file );
if( in_array( trim($name), $flist))
{
unset($file);
$name = preg_replace('/[^\\w\\-\\.\\_]/i', '', $name);
return ($name == '') ? 0 : 1;
}
unset($file);
return 0;
}
public function get_local_feed_urls($name = '')
{
$file = $this->_newsbeuter_config['newsbeuter']['be']['confpath'].'/url.local/'.$name;
if (file_exists($file))
{
$flist = preg_split( '/[\\n]/', file_get_contents($file) );
return $flist;
}
return '';
}
# move to Newsbeuter helper ?
public function get_parentcategory($cat, $esc=true)
{
$nc = preg_replace('#/[^/]*$#', '', $cat);
if(! $nc || $nc == '') { $nc = '/'; }
return $nc;
}
public function get_frontend_jsconf()
{
$jsconf = array();
$jsconf = $this->_newsbeuter_config['newsbeuter']['fe']['jsconf'];
$jsconf['version'] = $this->get_version();
return json_encode($jsconf);
}
## Apply Security related filter on RSS contents/text
public function apply_security_filter($opts = array(), $str = '')
{
$this->CI->load->helper('security');
foreach($opts as $k => $v)
{
if($k == 'img')
{
$str = strip_image_tags($str);
$str = preg_replace(array('#</img>#i'), '', $str);
}
if($k == 'default')
{
$pat = array('/<iframe/i', '#</iframe#i', '/<img/i', '#</img#i',
'/script language="javascript"/i',
'/script language=\'javascript\'/i',
'/<script/i', '#</script#i',
'/<video/i', '#</video#i',
'/<audio/i', '#</audio#i',
'/<embed/i', '#</embed#i',
'/<object/i', '#</object#i'
);
$rep = array('<xiframe', '</xiframe', '<omg', '</omg',
'script', 'script', '<noscript', '</noscript',
'<novideo', '</novideo',
'<noaudio', '</noaudio',
'<noembed', '</noembed',
'<nobject', '</nobject'
);
$str = preg_replace($pat, $rep, $str);
}
if($k == 'textonly')
{
$str = strip_tags($str);
$str = htmlentities($str, ENT_QUOTES); //of whats leftover
}
if($k == 'hashuri') // paranoia
{
$pat = array('/\#.*$/i', '/(\?|\&)(rss)(\=)/i', '#\/rss$#i'
);
$rep = array('', '$1do$3', ''
);
$str = preg_replace($pat, $rep, $str);
}
if($k == 'xss')
{
}
}
return $str;
}
public function set_meta_cache_file($opts = array(), $tag = '', $all = FALSE)
{
$t = ($tag != '') ? sha1($tag).'.' : '';
$format = @$opts['format'];
$f = $this->_newsbeuter_config['newsbeuter']['be']['datapath'].'/meta.'.$t.$format;
return $f;
}
public function set_icon_cache_file($opts = array(), $tag = '', $all = FALSE)
{
$t = ($tag != '') ? sha1($tag).'.' : '';
$format = @$opts['format'];
$db = (@$opts['dbname'] != '') ? @$opts['dbname'].'.' : '';
$f = $this->_newsbeuter_config['newsbeuter']['be']['datapath'].'/icon.'.$db.$t.$format;
return $f;
}
# get json saved cache
public function get_json_cache($file)
{
if (file_exists($file))
{
$a = file_get_contents($file, TRUE);
$data = json_decode ($a, TRUE);
return $data;
}
return '';
}
public function save_data($file, $data)
{
if(!$fp = @fopen($file, 'wb'))
{
return false;
}
fwrite($fp, $data);
fclose($fp);
unset($data);
return TRUE;
}
public function save_json_cache($file, $data)
{
$d = json_encode($data);
$this->save_data($file, $d);
}
public function save_meta_cache_lastfetch($file)
{
$Now=time();
$file = $file.'.lastfetch';
if(!$fp = @fopen($file, 'wb'))
{
return false;
}
fwrite($fp, $Now);
fclose($fp);
return TRUE;
}
public function is_meta_cache_fresh($allmeta, $meta)
{
$m1 = filemtime($allmeta);
$m2 = filemtime($meta);
if( (int)$m2 >= (int)$m1 )
{
return true;
}
return false;
}
# is FILE1 newer than FILE2
public function is_newer_than($f1, $f2)
{
if( (int)filemtime($f1) > (int)filemtime($f2) )
{
return true;
}
return false;
}
}
|