Permalink
Browse files

实现Api接口

  • Loading branch information...
1 parent ea5ac65 commit b03c8a98e7d74ebd724407f0e26ffbdbec96eb1a @hjue committed Aug 16, 2014
Showing with 570 additions and 46 deletions.
  1. +53 −0 api.md
  2. +2 −0 application/config/routes.php
  3. +92 −0 application/controllers/api.php
  4. +123 −43 application/libraries/blog_lib.php
  5. +297 −0 css/GitHub2.css
  6. +3 −3 settings.php
View
@@ -0,0 +1,53 @@
+<link rel="stylesheet" type="text/css" href="/css/GitHub2.css">
+JustWriting Api
+============
+
+### Post article
+
+ /api/article/post/
+
+
+Http Method:Post
+
+key|description
+-|-
+api_key|Api的密钥,在settings.php中设置
+name|文章的文件名,去掉.md的部分。若文件存在,会覆盖原先的文章
+text|文章的正文
+
+
+
+返回信息,返回json格式
+
+
+Success ,http status code = 200
+
+ {
+ "name" : "justwriting",
+ "link" : "http://justwriting.sinaapp.com/"
+ }
+
+Failed:invalid api key ,http status code = 403
+
+ {
+ "errorMsg" : "invalid api key"
+ }
+
+
+### Append Image/Text to Article
+
+ /api/article/append/
+
+Content-Type:multipart/form-data
+
+Post parameters
+
+key|description
+-|-
+api_key|Api的密钥,在settings.php中设置
+name|文章的文件名,去掉.md的部分
+text|追加的内容
+image|追加的图片
+
+返回信息,返回json格式,返回信息同Post Article
+
@@ -49,6 +49,8 @@
$route['sync/dropbox'] = "dropbox";
$route['sync/dropbox/(:any)'] = "dropbox/$1";
+$route['api'] = "api/index";
+$route['api/article/post'] = "api/article/post";
$route['404_override'] = '';
@@ -0,0 +1,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);
+
+ }
+}
@@ -20,6 +20,7 @@ public function __construct()
}
+
public function get_help()
{
$content = file_get_contents(FCPATH.'README.md');
@@ -35,6 +36,84 @@ public function markdown($value='')
return $html;
}
+ public function get_post_link($filename)
+ {
+ if(substr($filename,-3)!='.md'){
+ $filename .= '.md';
+ }
+
+ $path = $this->posts_path.$filename;
+ if(file_exists($path)){
+ $slug = str_replace($this->file_ext,'',$filename);
+ return $this->CI->blog_config['base_url']."/post/$slug";
+ }
+ }
+
+ public function write_post($filename,$text)
+ {
+ if(strlen(trim($text))==0) return false;
+ if(substr($filename,-3)!='.md'){
+ $filename .= '.md';
+ }
+
+ $path = $this->posts_path.$filename;
+ file_put_contents($path,"\n".$text);
+ return $filename;
+ }
+
+ public function append_post($filename,$text,$image)
+ {
+ if(empty($text) and empty($image)) return false;
+ if(substr($filename,-3)!='.md'){
+ $filename .= '.md';
+ }
+
+ $path = $this->posts_path.$filename;
+ if(!file_exists($path))
+ {
+ return false;
+ }
+
+ $content = file_get_contents($path);
+
+ if($text){
+ $content .= "\n\n".$text;
+ file_put_contents($path,$content);
+ }
+
+ if($image){
+ $config['upload_path'] = FCPATH.'posts/images';
+ if(!file_exists($config['upload_path']))
+ {
+ mkdir($config['upload_path'],0777,true);
+ }
+ $config['allowed_types'] = 'gif|jpg|png';
+ $config['max_size'] = '0';
+ $config['max_width'] = '0';
+ $config['max_height'] = '0';
+ $config['file_name'] = date("YmdHis").".jpg";
+ $config['encrypt_name'] = TRUE;
+
+ $this->CI->load->library('upload', $config);
+ if ( !$this->CI->upload->do_upload('image'))
+ {
+ $error = array('error' => $this->CI->upload->display_errors());
+ return false;
+ }
+ else
+ {
+ $data = $this->CI->upload->data();
+ $image_filename = $data['file_name'];
+ $content .= "\n\n"."![](images/$image_filename)";
+ file_put_contents($path,$content);
+ }
+ }
+
+
+ return $filename;
+ }
+
+
public function get_post($filename)
{
$prev_post = array();
@@ -90,50 +169,50 @@ private function __get_all_posts()
$post_status='public';
$post_tags=array();
- while(!trim($fcontents[$hi])){
- $hi++;
- continue;
- }
- while(trim($fcontents[$hi])){
- preg_match($pattern, $fcontents[$hi], $matches);
- $hi++;
+ if($fcontents[$hi] and strpos($fcontents[$hi],':')){
+
+ while(trim($fcontents[$hi])){
+ preg_match($pattern, $fcontents[$hi], $matches);
+ $hi++;
- if(empty($matches)) break;
- else{
- switch (trim(strtolower($matches[1]))) {
- case 'title':
- $post_title = $matches[2];
- break;
- case 'date':
- $post_date = trim($matches[2]);
- break;
+ if(empty($matches)) break;
+ else{
+ switch (trim(strtolower($matches[1]))) {
+ case 'title':
+ $post_title = $matches[2];
+ break;
+ case 'date':
+ $post_date = trim($matches[2]);
+ break;
- case 'status':
- $post_status = trim($matches[2]);
- if($post_status!='public'){
- $post_status = 'draft';
- }
- break;
-
- case 'tags':
- $tags = trim($matches[2]);
- if(substr($tags,0,1)=='[') $tags = substr($tags,1);
- if(substr($tags,-1,1)==']') $tags = substr($tags,0,-1);
- $post_tags = preg_split('#[,\s]#',$tags, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
-
-
- break;
- case 'intro':
- case 'description':
- $post_intro = trim($matches[2]);
- break;
+ case 'status':
+ $post_status = trim($matches[2]);
+ if($post_status!='public'){
+ $post_status = 'draft';
+ }
+ break;
+
+ case 'tags':
+ $tags = trim($matches[2]);
+ if(substr($tags,0,1)=='[') $tags = substr($tags,1);
+ if(substr($tags,-1,1)==']') $tags = substr($tags,0,-1);
+ $post_tags = preg_split('#[,\s]#',$tags, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
+
+
+ break;
+ case 'intro':
+ case 'description':
+ $post_intro = trim($matches[2]);
+ break;
- default:
- # code...
- break;
- }
+ default:
+ # code...
+ break;
+ }
+ }
}
}
+
if(empty($post_title)){
$post_title = str_replace('.md','',$entry);
}
@@ -157,8 +236,10 @@ private function __get_all_posts()
$post_date = strtotime($post_date);
}
$post_author = $this->CI->blog_config['author'];
- $post_content_md = trim(join('', array_slice($fcontents, $hi, count($fcontents) -1)));
+
+ $post_content_md = trim(join('', array_slice($fcontents, $hi, count($fcontents))));
$post_content = $post_content_md;
+
if(empty($post_intro)){
$post_text = strip_tags($this->markdown($post_content));
$post_intro = mb_substr($post_text,0,200);
@@ -194,6 +275,7 @@ private function __get_all_posts()
}
}
+
public function get_posts($options = array())
{
return $this->__get_all_posts();
@@ -204,6 +286,7 @@ public function get_posts_tags()
$this->__get_all_posts();
return array_keys($this->_all_tags);
}
+
public function get_posts_by_tag($tag){
$tag = trim($tag);
$posts = $this->__get_all_posts();
@@ -213,9 +296,6 @@ public function get_posts_by_tag($tag){
{
foreach($post['tags'] as $post_tag)
{
- if(strpos(strtolower($post_tag),'mysql')){
- print_r($post);
- }
if(strtolower($tag)==strtolower($post_tag)){
$result[]=$post;
break;
Oops, something went wrong.

0 comments on commit b03c8a9

Please sign in to comment.