-
Notifications
You must be signed in to change notification settings - Fork 3
/
backend.php
323 lines (291 loc) · 10.1 KB
/
backend.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
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
<?php
if ( !function_exists( 'add_action' ) ) {
echo "Hi there! I'm just a plugin, not much I can do when called directly.";
exit;
}
if(!function_exists('_log')){
function _log( $message ) {
if( WP_DEBUG === true ){
if( is_array( $message ) || is_object( $message ) ){
error_log("Orbital:");
error_log( print_r( $message, true ) );
} else {
error_log( "Orbital: $message");
}
}
}
}
require_once 'feeds.php';
require_once 'entries.php';
function nonce_dance(){
check_ajax_referer('orbital_actions','orbital_actions_nonce');
}
//TODO return a nonce or something. Nonce dancing should work better
function orbital_list_feeds_die(){
orbital_list_feeds();
exit;
}
function orbital_list_feeds(){
nonce_dance();
$myrows = OrbitalFeeds::get(null);
echo json_encode($myrows);
}
add_action('wp_ajax_orbital_get_feeds','orbital_list_feeds_die');
function orbital_list_tags(){
nonce_dance();
$tag_fragment = filter_input(INPUT_GET, 'q', FILTER_SANITIZE_STRING);
$rows = OrbitalFeeds::getTags($tag_fragment);
echo join($rows,"\n");
//echo json_encode();
exit;
}
add_action('wp_ajax_orbital_get_tags','orbital_list_tags');
//remove feed
function orbital_unsubscribe_feed(){
nonce_dance();
$feed_id = filter_input(INPUT_POST, 'feed_id', FILTER_SANITIZE_NUMBER_INT);
$resp = OrbitalFeeds::remove(null,$feed_id);
echo json_encode($resp);
exit;
}
add_action('wp_ajax_orbital_unsubscribe_feed','orbital_unsubscribe_feed');
//find the details of the feed.
function orbital_find_feed(){
nonce_dance();
$orig_url = filter_input(INPUT_POST, 'url',FILTER_SANITIZE_URL);
$contents = "";
$resp->orig_url = $orig_url;
if( !class_exists( 'WP_Http' ) )
include_once( ABSPATH . WPINC. '/class-http.php' );
$request = new WP_Http;
$result = $request->request( $orig_url);
$contents= $result['body'];
//if( !class_exists( 'WP_Http' ) )
include_once(ABSPATH . WPINC . '/class-feed.php');
$feed = new SimplePie();
//If you're cache isn't writable, this is a big deal
//Better to just disable it for now
$feed->enable_cache(false);
$feed->set_autodiscovery_level(SIMPLEPIE_LOCATOR_ALL);
/*
//TODO: LOOK, I know this is dumb.
//Simplepie doesn't seem to do a proper $feed->get_type unless we pass in contents
//feed autodiscovery doesn't work if you do pass in contents.
//So I'm doing 2 requests to figure out the feed contents
//WE'LL DO IT LATER
//http://knowyourmeme.com/memes/bill-oreilly-rant
$resp->feed_type = $feed->get_type() ;
$resp->feed_none = SIMPLEPIE_TYPE_NONE;
if(($feed->get_type() & SIMPLEPIE_TYPE_NONE) == SIMPLEPIE_TYPE_NONE){
$resp->feed_type = "NONE";
}
if(($feed->get_type() && SIMPLEPIE_TYPE_ALL)>0 ){
$rest->feed_type= "ALL";
} */
//check to see if the url is an html file.
if(stripos($contents, '<html>') === false && stripos($contents,'<html') === false){ //TODO Why check both? don't know, grabbed this from ttrss, need to research
$resp->url_type ='feed';
//$feed->set_feed_url($orig_url);
$feed->set_raw_data($contents);
//If your cache isn't writable, this is a big issue
$feed->enable_cache(false);
$feed->init();
//set the feed_name
$resp->feed_name = $feed->get_title();
//set the site_url to the site_url element on this feed
$resp->site_url = $feed->get_link();
//Simplepie doesn't support favicon anymore
//$resp->favicon = $feed->get_favicon();
//TODO return!
}else{
$resp->url_type = "html";
//if this is an html file, let's see what feeds lurk within.
$feed->set_feed_url($orig_url);
//If your cache isn't writable, this is a big issue
$feed->enable_cache(false);
$feed->init();
//add those feeds to the array of feed
$feeds = $feed->get_all_discovered_feeds();
$resp->feeds = $feeds;
//set the site_url to this url.
$resp->site_url=$orig_url;
//TODO set the feed name to the title element.
/*
$doc = new DOMDocument();
$doc->loadHTML($content);
$xpath = new DOMXPath($doc);
//$entries = $xpath->query('/html/head/title');
//$resp->feed_name = $entries;
//this is how tt-rss gets the discovery feeds
$entries = $xpath->query('/html/head/link[@rel="alternate"]');
$resp->feedEntries = $entries;
$feedUrls = array();
foreach ($entries as $entry) {
if ($entry->hasAttribute('href')) {
$title = $entry->getAttribute('title');
if ($title == '') {
$title = $entry->getAttribute('type');
}
$feedUrl = $entry->getAttribute('href');
//$feedUrl = rewrite_relative_url(
//$baseUrl, $entry->getAttribute('href')
//);
$feedUrls[$feedUrl] = $title;
}
}
$resp->feeds = $feedUrls;
*/
//TODO return!
}
echo json_encode($resp);
exit;
}
add_action('wp_ajax_orbital_find_feed','orbital_find_feed');
//edit feed
function orbital_save_feed(){
nonce_dance();
$feed_id = filter_input(INPUT_POST, 'feed_id', FILTER_SANITIZE_NUMBER_INT);
$feed_url = filter_input(INPUT_POST, 'feed_url',FILTER_SANITIZE_STRING);
$site_url = filter_input(INPUT_POST, 'site_url',FILTER_SANITIZE_STRING);
$feed_name = filter_input(INPUT_POST, 'feed_name',FILTER_SANITIZE_STRING);
$is_private = filter_input(INPUT_POST, 'is_private',FILTER_SANITIZE_STRING);
$tags = filter_input(INPUT_POST, 'tags', FILTER_SANITIZE_STRING);
$table_name = $wpdb->prefix.$tbl_prefix. "feeds ";
$resp = OrbitalFeeds::save(array('feed_id'=>$feed_id,'feed_url'=>$feed_url,'site_url'=>$site_url,'feed_name'=>$feed_name,'is_private'=>$is_private,'tags'=>$tags));
echo json_encode($resp);
exit;
}
add_action('wp_ajax_orbital_save_feed','orbital_save_feed');
//get feed entries
function orbital_get_feed_entries(){
nonce_dance();
$filters = array();
$feed_id = filter_input(INPUT_GET, 'feed_id', FILTER_SANITIZE_NUMBER_INT);
$show_read =filter_input(INPUT_GET, 'show_read', FILTER_SANITIZE_NUMBER_INT);
$tag = filter_input(INPUT_GET, 'tag',FILTER_SANITIZE_STRING);
$sort = filter_input(INPUT_GET, 'sort', FILTER_SANITIZE_NUMBER_INT);
if($tag !=""){
$filters['tag'] = $tag;
}
if($feed_id == ""){
//TODO "" should mean return latest entries
}else{
$filters['feed_id'] = $feed_id;
}
if($show_read=="1"){
//do nothing
}
else{
//only show unread entries
$filters['isRead']=$show_read;
}
$myrows = OrbitalEntries::get($filters);
echo json_encode($myrows);
exit;
}
add_action('wp_ajax_orbital_get_entries','orbital_get_feed_entries');
//update multiple feeds
function orbital_update_feeds(){
//nonce_dance(); //can't do a nonce dance here because this gets called from wp-cron
//get the list of feeds to update that haven't been updated recently
$feeds = OrbitalFeeds::get_stale_feeds();
//TODO Limit it to a reasonable number of feeds in a batch
//TODO Maybe we should schedule wp_cron jobs for each update?
//for each feed call update_feed
foreach( $feeds as $feed){
OrbitalFeeds::refresh($feed->id);
}
}
add_action('wp_ajax_orbital_update_feeds','orbital_update_feeds');
//update single feed
function orbital_update_feed($feed_id="",$feed_url=""){
nonce_dance();
//TODO if we didn't get passed a feed, check to see if it is in the url
if("" == $feed_id){
$feed_id = filter_input(INPUT_POST, 'feed_id',FILTER_SANITIZE_NUMBER_INT);
if("" == $feed_id){
$resp;
$resp->feed_id = $feed_id;
$resp->updated = 0;
$resp->reason = "No feed_id passed";
echo json_encode($resp);
exit;
}
}
//if this is coming from a user call with a user_feeds.id
$resp = OrbitalFeeds::refresh_user_feed($feed_id);
echo json_encode($resp);
exit;
}
add_action('wp_ajax_orbital_update_feed','orbital_update_feed');
//Mark items as read
function orbital_mark_items_read($feed_id){
nonce_dance();
global $wpdb;
global $tbl_prefix;
global $current_user;
//what do we update?
$feed_id = filter_input(INPUT_POST, 'feed_id', FILTER_SANITIZE_NUMBER_INT);
$prefix = $wpdb->prefix.$tbl_prefix;
$ret = $wpdb->update(
$prefix.'user_entries',//the table
array('isRead' =>1),//columns to update
array(//where filters
'feed_id' =>$feed_id, //current feed
'owner_uid'=>$current_user->ID //logged in user
)
);
$returnval;
$returnval->updated = $ret;
$returnval->feed_id = $feed_id;
echo json_encode($returnval);
exit;
}
add_action('wp_ajax_orbital_mark_items_read','orbital_mark_items_read');
//Mark item as read
function orbital_mark_item_read(){
nonce_dance();
$entry_id = filter_input(INPUT_POST, 'entry_id', FILTER_SANITIZE_NUMBER_INT);
$read_status = filter_input(INPUT_POST, 'read_status', FILTER_SANITIZE_NUMBER_INT);
$resp = OrbitalEntries::save(array(
'isRead' =>$read_status,//columns to update
'entry_id' =>$entry_id, //current entry
));
echo json_encode($resp);
exit;
}
add_action('wp_ajax_orbital_mark_item_read','orbital_mark_item_read');
//No non logged in way to mark an item read for me yet
//Get the current settings for this user
function orbital_get_user_settings(){
nonce_dance();
$settings = (array) get_user_option( 'orbital_settings' );
//TODO what if the settings haven't been set? we should default them.
//$sort_order = esc_attr($settings['sort-order']);
echo json_encode($settings);
exit;
}
add_action('wp_ajax_orbital_get_user_settings','orbital_get_user_settings');
//set the current entry sort order for this user
function orbital_set_user_settings(){
nonce_dance();
global $current_user;
//TODO this is the better way, but I can't get it to work.
//$user_orbital_settings = filter_input(INPUT_POST, 'orbital_settings', FILTER_SANITIZE_STRING);
//TODO we should handle if there isn't a setting passed...
$user_orbital_settings = $_POST['orbital_settings'];
$settings = (array) get_user_option( 'orbital_settings' );
//merge arrays
$new_settings = $user_orbital_settings + $settings;
if(update_user_option($current_user->ID, 'orbital_settings', $new_settings)){
// Send back what we now know
echo json_encode($new_settings);
}
else {
echo false;
_log('update failed');
}
exit;
}
add_action('wp_ajax_orbital_set_user_settings','orbital_set_user_settings');
?>