Initial commit

parents
MetaWeblog API controller for CodeIgniter
---------------------------------------------------------------------------------------------------
Find out more at http://www.beseku.com/...
\ No newline at end of file
<?php
/**
* @package MetaWeblog API Demo
* @subpackage Controllers
* @author Ben Sekulowicz-Barclay
* @copyright Copyright 2010
* @version 0.01
*
**/
class DemoController extends Controller {
function DemoController() {
parent::Controller();
$this->load->helper('xmlrpc');
$this->load->library('xmlrpc');
$this->load->library('xmlrpcs');
$this->load->model('DemoModel', 'demoModel');
}
/**
* @access public
* @author Ben Sekulowicz-Barclay
*
**/
public function index() {
// Setup our XMP-RPC server to deal with all of the MetaWeblog API methods that MarsEdit sends ...
$this->xmlrpcs->initialize(array(
'functions' => array(
'blogger.deletePost' => array('function' => 'DemoController.delete_post'),
'metaWeblog.editPost' => array('function' => 'DemoController.edit_post'),
'metaWeblog.getCategories' => array('function' => 'DemoController.get_categories'),
'metaWeblog.getPost' => array('function' => 'DemoController.get_post'),
'metaWeblog.getRecentPosts' => array('function' => 'DemoController.get_posts'),
'metaWeblog.newPost' => array('function' => 'DemoController.add_post'),
),
'object' => $this,
));
$this->xmlrpcs->serve();
}
/**
* @access public
* @param array
* @author Ben Sekulowicz-Barclay
*
**/
public function add_post($request) {
// Get the request data and validate against the user account ...
$request = $request->output_parameters();
// If the user credentials against our (fake) authenticate method ... send an error
if (!$account = $this->demoModel->authenticate_user($request['1'], $request['2'])) {
return $this->xmlrpc->send_error_message('100', 'Invalid Access');
}
if (FALSE === ($post = $this->demoModel->add_post($account, $request['0'], $request['3']))) {
return $this->xmlrpc->send_error_message('101', 'The post could not be created');
}
return $this->xmlrpc->send_response(array($post['id'], 'string'));
}
/**
* @access public
* @param array
* @author Ben Sekulowicz-Barclay
*
**/
public function delete_post($request) {
// Get the request data and validate against the user account ...
$request = $request->output_parameters();
// If the user credentials against our (fake) authenticate method ... send an error
if (!$account = $this->demoModel->authenticate_user($request['2'], $request['3'])) {
return $this->xmlrpc->send_error_message('100', 'Invalid Access');
}
// If the delete method returns an error, send back a message and stop here ...
if (!$this->demoModel->delete_post($account, $request['1'])) {
return $this->xmlrpc->send_error_message('100', 'The post could not be deleted');
}
return $this->xmlrpc->send_response(array(1, 'boolean'));
}
/**
* @access public
* @param array
* @author Ben Sekulowicz-Barclay
*
**/
public function edit_post($request) {
// Get the request data and validate against the user account ...
$request = $request->output_parameters();
// If the user credentials against our (fake) authenticate method ... send an error
if (!$account = $this->demoModel->authenticate_user($request['1'], $request['2'])) {
return $this->xmlrpc->send_error_message('100', 'Invalid Access');
}
// If the edit method returns an error, send back a message and stop here ...
if (!$this->demoModel->edit_post($account, $request['0'], $request['3'])) {
return $this->xmlrpc->send_error_message('100', 'The post could not be edited');
}
return $this->xmlrpc->send_response(array(1, 'boolean'));
}
/**
* @access public
* @param array
* @author Ben Sekulowicz-Barclay
*
**/
public function get_categories($request) {
// Get the request data and validate against the user account ...
$request = $request->output_parameters();
// If the user credentials against our (fake) authenticate method ... send an error
if (!$account = $this->demoModel->authenticate_user($request['1'], $request['2'])) {
return $this->xmlrpc->send_error_message('100', 'Invalid Access');
}
$categories = categories_to_structs($this->demoModel->get_categories($request['0']));
return $this->xmlrpc->send_response($categories);
}
/**
* @access public
* @param array
* @author Ben Sekulowicz-Barclay
*
**/
public function get_post($request) {
// Get the request data and validate against the user account ...
$request = $request->output_parameters();
// If the user credentials against our (fake) authenticate method ... send an error
if (!$account = $this->demoModel->authenticate_user($request['1'], $request['2'])) {
return $this->xmlrpc->send_error_message('100', 'Invalid Access');
}
$post = post_to_struct($this->demoModel->get_post($request['0']));
return $this->xmlrpc->send_response($post);
}
/**
* @access public
* @param array
* @author Ben Sekulowicz-Barclay
*
**/
public function get_posts($request) {
// Get the request data and validate against the user account ...
$request = $request->output_parameters();
// If the user credentials against our (fake) authenticate method ... send an error
if (!$account = $this->demoModel->authenticate_user($request['1'], $request['2'])) {
return $this->xmlrpc->send_error_message('100', 'Invalid Access');
}
$posts = posts_to_structs($this->demoModel->get_posts($request['0'], $request['3']));
return $this->xmlrpc->send_response($posts);
}
}
/* End of file DemoController.php */
/* Location: ./system/application/controllers/DemoController.php */
\ No newline at end of file
<?php
/**
* @param array
* @return array
* @author Ben Sekulowicz-Barclay
*
**/
if (!function_exists('category_to_struct')) {
function category_to_struct($category = '') {
return array(
array(
'categoryId' => array($category['id'], 'string'),
'parentId' => array($category['parent'], 'string'),
'description' => array($category['description'], 'string'),
'categoryDescription' => array('', 'string'),
'categoryName' => array($category['title'], 'string'),
),
'struct'
);
}
}
/**
* @param array
* @return array
* @author Ben Sekulowicz-Barclay
*
**/
if (!function_exists('categories_to_structs')) {
function categories_to_structs($categories = '') {
$temp = array(array(), 'array');
foreach($categories as $category) {
$temp[0][] = category_to_struct($category);
}
return $temp;
}
}
/**
* @param array
* @return array
* @author Ben Sekulowicz-Barclay
*
**/
if (!function_exists('post_to_struct')) {
function post_to_struct($post = '') {
return array(
array(
'postid' => array($post['id'], 'string '),
'dateCreated' => array(date('c', $post['dateCreated']), 'DateTime'),
'title' => array($post['title'], 'string'),
'description' => array($post['description'], 'string'),
'categories' => array($post['categories'], 'array'),
),
'struct'
);
}
}
/**
* @param array
* @return array
* @author Ben Sekulowicz-Barclay
*
**/
if (!function_exists('posts_to_structs')) {
function posts_to_structs($posts = '') {
$temp = array(array(), 'array');
foreach($posts as $post) {
$temp[0][] = post_to_struct($post);
}
return $temp;
}
}
?>
\ No newline at end of file
<?php
/**
* @package MetaWeblog API Demo
* @subpackage Controllers
* @author Ben Sekulowicz-Barclay
* @copyright Copyright 2010
* @version 0.01
*
**/
class DemoModel extends Model {
/**
* @access public
* @param string
* @param string
* @return mixed
* @author Ben Sekulowicz-Barclay
*
**/
public function authenticate_user($user, $pass) {
// This is a fake method... you can only login as user #99 ...
return ($user === 'ben' && $pass === 'sux')? 99: FALSE;
}
/**
* @access public
* @param integer
* @param integer
* @return boolean
* @author Ben Sekulowicz-Barclay
*
**/
public function add_post($account = FALSE, $data = FALSE, $post = FALSE) {
// This is a fake method... you can only add post #10 ...
if (!$account || (int) $account != 99 || !$post) {
return FALSE;
}
// We only need to return the post ID from this method, not the whole post ...
return 11;
}
/**
* @access public
* @param integer
* @param integer
* @return boolean
* @author Ben Sekulowicz-Barclay
*
**/
public function delete_post($account = FALSE, $post = FALSE) {
// This is a fake method... you can only delete post #10 ...
if (!$account || (int) $account != 99 || !$post || (int) $post != 10) {
return FALSE;
}
// We only need to return the true/false from this method ...
return TRUE;
}
/**
* @access public
* @param integer
* @param integer
* @return boolean
* @author Ben Sekulowicz-Barclay
*
**/
public function edit_post($account = FALSE, $post = FALSE) {
// This is a fake method... you can only edit post #9 ...
if (!$account || (int) $account != 99 || !$post || (int) $post != 9) {
return FALSE;
}
// We only need to return the true/false from this method ...
return TRUE;
}
/**
* @access public
* @param integer
* @param integer
* @return array
* @author Ben Sekulowicz-Barclay
*
**/
public function get_categories($blog, $limit = 1000, $offset = 0) {
// The controller deals with formatting this data, using the categories_to_structs helper.
return array(
array(
'id' => 1,
'parent' => 0,
'description' => '',
'title' => 'Arsenal FC',
),
array(
'id' => 2,
'parent' => 0,
'description' => '',
'title' => 'Living in Japan',
),
);
}
/**
* @access public
* @param integer
* @return array
* @author Ben Sekulowicz-Barclay
*
**/
public function get_post($post = FALSE) {
// The controller deals with formatting this data, using the post_to_struct helper.
return array(
'id' => (int) $post,
'dateCreated' => date('U', strtotime('2010-10-18 01:23:45')),
'title' => 'The title of post number 1',
'description' => 'The description of post number 1',
'categories' => array('Arsenal FC'),
);
}
/**
* @access public
* @param integer
* @param integer
* @param integer
* @return array
* @author Ben Sekulowicz-Barclay
*
**/
public function get_posts($blog = FALSE, $limit = 1000, $offset = 0) {
// The controller deals with formatting this data, using the posts_to_structs helper.
return array(
array(
'id' => 1,
'dateCreated' => date('U', strtotime('2010-10-18 01:23:45')),
'title' => 'The title of post number 1',
'description' => 'The description of post number 1',
'categories' => array('Arsenal FC'),
),
array(
'id' => 2,
'dateCreated' => date('U', strtotime('2010-10-19 01:23:45')),
'title' => 'The title of post number 2',
'description' => 'The description of post number 2',
'categories' => array('Living in Japan'),
),
array(
'id' => 3,
'dateCreated' => date('U', strtotime('2010-10-20 01:23:45')),
'title' => 'The title of post number 3',
'description' => 'The description of post number 3',
'categories' => array('Arsenal FC', 'Living in Japan'),
),
array(
'id' => 4,
'dateCreated' => date('U', strtotime('2010-10-21 01:23:45')),
'title' => 'The title of post number 4',
'description' => 'The description of post number 4',
'categories' => array('Arsenal FC'),
),
array(
'id' => 5,
'dateCreated' => date('U', strtotime('2010-10-22 01:23:45')),
'title' => 'The title of post number 5',
'description' => 'The description of post number 5',
'categories' => array('Living in Japan'),
),
array(
'id' => 6,
'dateCreated' => date('U', strtotime('2010-10-23 01:23:45')),
'title' => 'The title of post number 6',
'description' => 'The description of post number 6',
'categories' => array('Arsenal FC'),
),
array(
'id' => 7,
'dateCreated' => date('U', strtotime('2010-10-24 01:23:45')),
'title' => 'The title of post number 7',
'description' => 'The description of post number 7',
'categories' => array('Arsenal FC'),
),
array(
'id' => 8,
'dateCreated' => date('U', strtotime('2010-10-25 01:23:45')),
'title' => 'The title of post number 8',
'description' => 'The description of post number 8',
'categories' => array('Arsenal FC'),
),
array(
'id' => 9,
'dateCreated' => date('U', strtotime('2010-10-26 01:23:45')),
'title' => 'The title of post number 9',
'description' => 'The description of post number 9',
'categories' => array('Arsenal FC'),
),
array(
'id' => 10,
'dateCreated' => date('U', strtotime('2010-10-27 01:23:45')),
'title' => 'The title of post number 10',
'description' => 'The description of post number 10',
'categories' => array('Arsenal FC'),
)
);
}
}
/* End of file DemoModel.php */
/* Location: ./system/application/models/DemoModel.php */
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment