-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
139 lines (96 loc) · 2.77 KB
/
index.php
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
<?php
session_start();
require_once('vendor/autoload.php');
require_once('simplepie/autoloader.php');
$app = new \Slim\Slim();
require_once('config.php');
class MyItemClass extends SimplePie_Item{
public function get_image(){
$doc = phpQuery::newDocument($this->get_description());
$src = "http://lorempixel.com/200/200";
$img = $doc->find('img:first');
$temp_src = $img->attr('src');
if(!empty($temp_src)){
$src = $img->attr('src');
}
else{
$doc = phpQuery::newDocument($this->get_content());
$img = $doc->find('img:first');
$temp_src = $img->attr('src');
if(!empty($temp_src)){
$src = $img->attr('src');
}
}
return $src;
}
}
function get_feed_items(){
// go through all the feed urls
$items = array();
$feeds = ORM::for_table('feeds')->find_many();
foreach($feeds as $feed_loc){
$feed = new SimplePie();
$feed->set_feed_url($feed_loc->location);
$feed->set_cache_location("mysql://".DB_USER.":".DB_PASS."@".DB_HOST.":3306/".DB_NAME);
$feed->set_item_class('MyItemClass');
$feed->init();
$feed->handle_content_type();
$items = array_merge($items, $feed->get_items());
}
return $items;
}
function index(){
$app = \Slim\Slim::getInstance();
$app->view()->appendData(array("items"=>get_feed_items()));
$app->render('landing.twig.html');
}
function admin(){
$app = \Slim\Slim::getInstance();
$feeds = ORM::for_table('feeds')->find_many();
$app->view()->appendData(array('feeds'=>$feeds));
$app->render('admin.html.twig');
}
function add_rss_feed(){
$app = \Slim\Slim::getInstance();
$url = $app->request()->post('url');
// TODO: check if url is empty
// TODO: check if url points actual rss feed
$new_feed = ORM::for_table('feeds')->create();
$new_feed->location = $url;
$new_feed->save();
$app->redirect($app->urlFor('admin'));
}
function remove_rss_feed($id=null){
$app = \Slim\Slim::getInstance();
if($id!==null){
$feed = ORM::for_table('feeds')->find_one($id);
$feed->delete();
}
$app->redirect($app->urlFor('admin'));
}
function get_rss_feed_json(){
$app = \Slim\Slim::getInstance();
$feed = new SimplePie();
$feed->set_feed_url('./rss.xml');
$feed->init();
$feed->handle_content_type();
$app->render('feed.twig.json');
}
function get_rss_feed_html(){
$app = \Slim\Slim::getInstance();
$feed = new SimplePie();
$feed->set_feed_url('./rss.xml');
$feed->init();
$feed->handle_content_type();
$data = array();
$data['feed'] = $feed;
$app->view()->appendData($data);
$app->render('feed.twig.html');
}
$app->get('/admin/remove/:id','remove_rss_feed')->name('remove_rss');
$app->post('/admin/add', 'add_rss_feed')->name('add_rss');
$app->get('/admin', 'admin')->name('admin');
$app->get('/feed', 'get_rss_feed_html')->name('feed');
$app->get('/', 'index')->name('landing');
$app->run();
?>