-
Notifications
You must be signed in to change notification settings - Fork 79
/
api.php
92 lines (73 loc) · 1.97 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
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Api extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('blog_lib');
}
public function index()
{
$content = file_get_contents(FCPATH.'api.md');
$html = $this->blog_lib->markdown($content);
echo $html;
}
public function test()
{
echo $this->blog_lib->write_post("test","asdfasdfas");
}
private function check_auth()
{
$api_key = $this->input->post('api_key');
if($this->blog_config['api'] && $this->blog_config['api_key']==$api_key)
{
return True;
}else
{
set_status_header(403);
echo json_encode(array('errorMsg'=>'invalid api key'));
exit;
}
}
public function article($action)
{
if($action=='post')
{
$this->article_post();
}
if($action == 'append'){
$this->article_append();
}
}
/*
curl http://localhost:8080/api/article/post -d api_key=1234561 -d name=test -d text=AAAA
*/
private function article_post()
{
$this->check_auth();
$name = $this->input->post('name');
$text = $this->input->post('text');
$filename = $this->blog_lib->write_post($name,$text);
$link = $this->blog_lib->get_post_link($filename);
$result['name']= $name;
$result['link'] = $link;
echo json_encode($result);
}
/*
curl http://localhost:8080/api/article/append -F api_key=1234561 -F image=@2.png -F name=test -F text=AAAA
*/
private function article_append()
{
$this->check_auth();
$name = $this->input->post('name');
if(isset($_FILES))
{
$image = $_FILES['image'];
}
$text = $this->input->post('text');
$filename = $this->blog_lib->append_post($name,$text,$image);
$link = $this->blog_lib->get_post_link($filename);
$result['name']= $name;
$result['link'] = $link;
echo json_encode($result);
}
}