-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.php
162 lines (149 loc) · 3.89 KB
/
api.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
<?php
define('BASEPATH', __DIR__);//for CI
require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/application/config/database.php';
$medoo = new medoo(array(
'database_type' => 'mysql',
'database_name' => $db['default']['database'],
'server' => $db['default']['hostname'],
'username' => $db['default']['username'],
'password' => $db['default']['password'],
'charset' => 'utf8'
));
define('DOMAIN', 'pic.ecjtu.net');
define('API_BASE_URL', DOMAIN . '/api');
define('USER_API_URL', 'http://user.ecjtu.net/api/user/');
class JsonHeaders extends \Slim\Middleware
{
public function call()
{
$this->app->response->headers->set(
'Content-Type', 'application/json');
$this->next->call();
}
}
$app = new \Slim\Slim();
$app->add(new JsonHeaders());
$app->get('/list', function () use ($app, $medoo) {
define('PER_PAGE', 3);
$page = intval($app->request->get('page'));
$page = $page<=0 ? 1 : $page;
$before = $app->request->get('before');
$until = intval($app->request->get('until'));
$per_page = intval($app->request->get('$per_page'));
if (!$per_page) {
$per_page = PER_PAGE;
}
$per_page = $per_page<=0 ? PER_PAGE : $per_page;
$offset = ($page - 1) * $per_page;
$condition['LIMIT'] = array($offset, $per_page);
$condition['ORDER'] = 'posts_pubdate DESC';
if ($before) {
$condition['ORDER'] = 'posts_pubdate DESC';
$condition['posts_pubdate[<]'] = $before;
} elseif ($until) {
$condition['ORDER'] = 'posts_id DESC';
$condition['posts_id[<]'] = $until;
}
$data = $medoo->select(
'cyrec_posts',
array(
'posts_id(pid)',
'posts_category(category)',
'posts_title(title)',
'posts_description(description)',
'posts_thumb(thumb)',
'posts_count(count)',
'posts_pubdate(pubdate)',
'posts_hit(click)',
),
$condition
);
$output = array();
foreach ($data as $row) {
$row['thumb'] = DOMAIN . '/' . $row['thumb'];
$output[] = $row;
}
$count = count($output);
echo json_encode(array(
'count' => $count,
'list' => $output,
));
});
$app->get('/post/:pid', function ($pid) use ($app, $medoo) {
$pid = intval($pid);
$data = $medoo->select(
'cyrec_posts',
array(
'posts_id(pid)',
'posts_category(category)',
'posts_title(title)',
'posts_description(description)',
'posts_keywords(keywords)',
'posts_author(author)',
'posts_thumb(thumb)',
'posts_pictures(pictures)',
'posts_count(count)',
'posts_pubdate(pubdate)',
'posts_hit(click)',
'posts_type(type)',
'posts_detail(detail)',
),
array(
'posts_id' => $pid,
)
);
if(!$data)
return $app->response->setStatus(404);
$data = $data[0];
$pictures = unserialize($data['pictures']);
$detail = unserialize($data['detail']);
$out_pictures = array();
foreach ($pictures as $key => $row) {
$out_pictures[$key]['url'] = $row;
if(isset($detail[$key]) && $detail[$key])
$out_pictures[$key]['detail'] = $detail[$key];
else
$out_pictures[$key]['detail'] = '';
}
unset($data['pictures']);
unset($data['detail']);
$comments = $medoo->select(
'cyrec_comments',
array(
'comments_posts_id(pid)',
'comments_time(time)',
'comments_author(author)',
'comments_text(text)',
'comments_author_sid(sid)',
),
array(
'comments_posts_id' => $data['pid'],
'ORDER' => 'comments_time DESC',
)
);
foreach ($comments as $key => $row) {
if(isset($row['sid']) && $row['sid']){
$curl = new Curl();
$user = $curl->get(USER_API_URL . $row['sid']);
$user = json_decode($user);
$user = $user['user'];
$comments[$key]['author'] = array(
'sid' => $row['sid'],
'name' => $user['name'],
'avatar' => $user['avatar'],
);
}else{
$comments[$key]['author'] = array(
'sid' => '',
'name' => $row['author'],
'avatar' => '',
);
}
}
$data['thumb'] = DOMAIN . '/' . $data['thumb'];
$data['pictures'] = $out_pictures;
$data['comments'] = $comments;
echo json_encode($data);
});
$app->run();