forked from dignajar/nibbleblog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bludit.php
157 lines (120 loc) · 3.59 KB
/
bludit.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
<?php
header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");
/*
* Nibbleblog -
* http://www.nibbleblog.com
* Author Diego Najar
* All Nibbleblog code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*/
// =====================================================================
// VARIABLES
// =====================================================================
$POSTS_TO_SYNC = 10;
// =====================================================================
// FUNCTIONS
// =====================================================================
// This function make a post recondition, and convert to json
// If you don't want to convert to json, then call the function with false
function post_to_json($post, $tojson=true)
{
global $settings;
global $_DB_TAGS;
global $_DB_CATEGORIES;
// Permalink
$post['permalink'] = Url::post($post, true);
// Get tags
$post['tags'] = $_DB_TAGS->get_by_idpost( array('id_post'=>$post['id']) );
// Category
$category = $_DB_CATEGORIES->get( array('id'=>$post['id_cat']) );
array_push($post['tags'], array('name'=>$category['name'], 'name_human'=>$category['name']));
/*
The above array_push line may need to be rewritten.
$post['tags'] now has the following structure...
Array
(
[0] => Array
(
[name] => startrek
[name_human] => star trek
)
[1] => Array
(
[name] => icecream
[name_human] => ice cream
)
)
*/
// Content
// Src images relatives to absolute
$domain = $settings['url'];
$post['content'] = preg_replace("/(src)\=\"([^(http|data:image)])(\/)?/", "$1=\"$domain$2", $post['content'][0]);
// Unset
unset($post['read_more']);
unset($post['filename']);
unset($post['id_cat']);
unset($post['id_user']);
unset($post['mode']);
unset($post['draft']);
unset($post['visits']);
unset($post['allow_comments']);
if($tojson)
return json_encode($post);
return $post;
}
// =====================================================================
// MAIN
// =====================================================================
// Boot
require('admin/boot/blog.bit');
// Blog Keys
require(FILE_KEYS);
if(!isset($_KEYS[0]))
exit(json_encode(array('error'=>'Nibbleblog: Error key 0')));
if(!isset($_KEYS[1]))
exit(json_encode(array('error'=>'Nibbleblog: Error key 1')));
// This hash represent your blog on Bludit
$mark = Crypt::get_hash($_KEYS[0]);
// This hash is the key for sync
$key_for_sync = Crypt::get_hash($_KEYS[1]);
if($url['sync']!=$key_for_sync)
exit(json_encode(array('error'=>'Nibbleblog: Error key for sync')));
// Prevent flood requests
// $_DB_USERS->set_blacklist();
if($url['other']=='status')
{
$posts = $_DB_POST->get_list_by_page(array('page'=>0, 'amount'=>$POSTS_TO_SYNC));
$posts = array_reverse($posts);
$tmp = array(
'posts'=>array(),
'mark'=>$mark
);
foreach($posts as $post)
{
$time = max($post['pub_date_unix'], $post['mod_date_unix']);
$sync = array();
$sync['id'] = $post['id'];
$sync['time'] = $time;
$sync['hash'] = Crypt::get_hash(json_encode($post));
$sync['post'] = post_to_json($post,false);
array_push($tmp['posts'], $sync);
}
echo json_encode($tmp);
}
elseif($url['other']=='post')
{
// Get the post
$post = $_DB_POST->get(array('id'=>$url['id_post']));
// Post to Json
echo post_to_json($post);
}
elseif($url['other']=='latest')
{
$list = $_DB_POST->get_list_by_page(array('page'=>0, 'amount'=>5));
$tmp = array();
foreach($list as $post)
array_push($tmp, post_to_json($post, false));
echo json_encode($tmp);
}
?>