forked from fossar/selfoss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.php
More file actions
233 lines (196 loc) · 6.6 KB
/
Index.php
File metadata and controls
233 lines (196 loc) · 6.6 KB
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
<?PHP
namespace controllers;
/**
* Controller for root
*
* @package controllers
* @copyright Copyright (c) Tobias Zeising (http://www.aditu.de)
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
* @author Tobias Zeising <tobias.zeising@aditu.de>
*/
class Index extends BaseController {
/**
* home site
* html
*
* @return void
*/
public function home() {
// check login
$this->authentication();
// parse params
$options = array();
if (\F3::get('homepage')!='')
$options = array( 'type' => \F3::get('homepage') );
// use ajax given params?
if(count($_GET)>0)
$options = $_GET;
// get search param
if(isset($options['search']) && strlen($options['search'])>0)
$this->view->search = $options['search'];
// load tags
$tagsDao = new \daos\Tags();
$tags = $tagsDao->get();
// load items
$itemsHtml = $this->loadItems($options, $tags);
$this->view->content = $itemsHtml;
// load stats
$itemsDao = new \daos\Items();
$this->view->statsAll = $itemsDao->numberOfItems();
$this->view->statsUnread = $itemsDao->numberOfUnread();
$this->view->statsStarred = $itemsDao->numberOfStarred();
// prepare tags display list
$tagsController = new \controllers\Tags();
$this->view->tags = $tagsController->renderTags($tags);
// prepare sources display list
$sourcesDao = new \daos\Sources();
$sources = $sourcesDao->get();
$sourcesController = new \controllers\Sources();
$this->view->sources = $sourcesController->renderSources($sources);
// ajax call = only send entries and statistics not full template
if(isset($options['ajax'])) {
$this->view->jsonSuccess(array(
"entries" => $this->view->content,
"all" => $this->view->statsAll,
"unread" => $this->view->statsUnread,
"starred" => $this->view->statsStarred,
"tags" => $this->view->tags,
"sources" => $this->view->sources
));
}
// show as full html page
$this->view->publicMode = \F3::get('auth')->isLoggedin()!==true && \F3::get('public')==1;
$this->view->loggedin = \F3::get('auth')->isLoggedin()===true;
echo $this->view->render('templates/home.phtml');
}
/**
* password hash generator
* html
*
* @return void
*/
public function password() {
$this->view = new \helpers\View();
$this->view->password = true;
if(isset($_POST['password']))
$this->view->hash = hash("sha512", \F3::get('salt') . $_POST['password']);
echo $this->view->render('templates/login.phtml');
}
/**
* check and show login/logout
* html
*
* @return void
*/
private function authentication() {
// logout
if(isset($_GET['logout'])) {
\F3::get('auth')->logout();
\F3::reroute($this->view->base);
}
// login
if(
isset($_GET['login']) || (\F3::get('auth')->isLoggedin()!==true && \F3::get('public')!=1)
) {
// authenticate?
if(count($_POST)>0) {
if(!isset($_POST['username']))
$this->view->error = 'no username given';
else if(!isset($_POST['password']))
$this->view->error = 'no password given';
else {
if(\F3::get('auth')->login($_POST['username'], $_POST['password'])===false)
$this->view->error = 'invalid username/password';
}
}
// show login
if(count($_POST)==0 || isset($this->view->error))
die($this->view->render('templates/login.phtml'));
else
\F3::reroute($this->view->base);
}
}
/**
* login for api json access
* json
*
* @return void
*/
public function login() {
$view = new \helpers\View();
$username = isset($_REQUEST["username"]) ? $_REQUEST["username"] : '';
$password = isset($_REQUEST["password"]) ? $_REQUEST["password"] : '';
if(\F3::get('auth')->login($username,$password)==true)
$view->jsonSuccess(array(
'success' => true
));
$view->jsonSuccess(array(
'success' => false
));
}
/**
* logout for api json access
* json
*
* @return void
*/
public function logout() {
$view = new \helpers\View();
\F3::get('auth')->logout();
$view->jsonSuccess(array(
'success' => true
));
}
/**
* update feeds
* text
*
* @return void
*/
public function update() {
$loader = new \helpers\ContentLoader();
$loader->update();
echo "finished";
}
/**
* load items
*
* @return html with items
*/
private function loadItems($options, $tags) {
$tagColors = $this->convertTagsToAssocArray($tags);
$itemDao = new \daos\Items();
$itemsHtml = "";
foreach($itemDao->get($options) as $item) {
// parse tags and assign tag colors
$itemsTags = explode(",",$item['tags']);
$item['tags'] = array();
foreach($itemsTags as $tag) {
$tag = trim($tag);
if(strlen($tag)>0 && isset($tagColors[$tag]))
$item['tags'][$tag] = $tagColors[$tag];
}
$this->view->item = $item;
$itemsHtml .= $this->view->render('templates/item.phtml');
}
if(strlen($itemsHtml)==0) {
$itemsHtml = '<div class="stream-empty">'. \F3::get('lang_no_entries').'</div>';
} else {
if($itemDao->hasMore())
$itemsHtml .= '<div class="stream-more"><span>'. \F3::get('lang_more').'</span></div>';
}
return $itemsHtml;
}
/**
* return tag => color array
*
* @return tag color array
* @param array $tags
*/
private function convertTagsToAssocArray($tags) {
$assocTags = array();
foreach($tags as $tag)
$assocTags[$tag['tag']] = $tag['color'];
return $assocTags;
}
}