/xmlrpc.php

https://github.com/LastRose/MetaWeblog-API-for-CodeIgnitor · PHP · 282 lines · 264 code · 17 blank · 1 comment · 23 complexity · 97a5563caa2c0e82f2be4d4764dceb88 MD5 · raw file

  1. <?php if (!defined('BASEPATH')) {
  2. exit('No direct script access allowed');
  3. }
  4. class Xmlrpc extends CI_Controller {
  5. function __construct() {
  6. parent::__construct();
  7. }
  8. function index() {
  9. $config['functions']['blogger.getUserInfo'] = array('function' => 'xmlrpc.getUserInfo');
  10. $config['functions']['blogger.getUsersBlogs'] = array('function' => 'xmlrpc.getUsersBlogs');
  11. $config['functions']['blogger.deletePost'] = array('function' => 'xmlrpc.deletePost');
  12. $config['functions']['metaWeblog.newPost'] = array('function' => 'xmlrpc.newPost');
  13. $config['functions']['metaWeblog.editPost'] = array('function' => 'xmlrpc.editPost');
  14. $config['functions']['metaWeblog.getPost'] = array('function' => 'xmlrpc.getPost');
  15. $config['functions']['metaWeblog.getCategories'] = array('function' => 'xmlrpc.getCategories');
  16. $config['functions']['metaWeblog.getRecentPosts'] = array('function' => 'xmlrpc.getRecentPosts');
  17. $config['functions']['metaWeblog.newMediaObject'] = array('function' => 'xmlrpc.newMediaObject');
  18. $config['object'] = $this;
  19. $this->xmlrpcs->initialize($config);
  20. $this->xmlrpcs->serve();
  21. }
  22. /*This is a Unit Testing function*/
  23. function test2() {
  24. $server_url = site_url('xmlrpc');
  25. $this->xmlrpc->set_debug(TRUE);
  26. $this->xmlrpc->server($server_url, 80);
  27. $this->xmlrpc->method('metaWeblog.editPost');
  28. $request = array(
  29. '1',
  30. 'user',
  31. 'pass',
  32. array(
  33. array(
  34. 'title' => 'Test',
  35. 'description' => 'test',
  36. 'categories' => array(array('test cat'), 'struct')
  37. ), 'struct'
  38. ),
  39. '1'
  40. );
  41. $this->xmlrpc->request($request);
  42. if (!$this->xmlrpc->send_request()) {
  43. echo $this->xmlrpc->display_error();
  44. }
  45. else
  46. {
  47. echo '<pre>';
  48. print_r($this->xmlrpc->display_response());
  49. echo '</pre>';
  50. }
  51. }
  52. function login($user, $pass) {
  53. $u = 'user';
  54. $p = 'pass';
  55. if ($u == $user AND $p == $pass) {
  56. return true;
  57. }
  58. return false;
  59. }
  60. function getUsersBlogs($request) {
  61. $parameters = $request->output_parameters();
  62. if (!$this->login($parameters['1'], $parameters['2'])) {
  63. return $this->xmlrpc->send_error_message('100', 'Invalid Access');
  64. }
  65. $blogs = $this->blogs->getBlogs();
  66. foreach ($blogs as $blog) {
  67. $array[] = array(
  68. array(
  69. 'url' => array(site_url(), 'string'),
  70. 'blogid' => array($blog['blogId'], 'string'),
  71. 'blogName' => array($blog['BlogName'], 'string')
  72. ),
  73. 'struct'
  74. );
  75. }
  76. $response = array($array, 'array');
  77. return $this->xmlrpc->send_response($response);
  78. }
  79. function getCategories($request) {
  80. $parameters = $request->output_parameters();
  81. if (!$this->login($parameters['1'], $parameters['2'])) {
  82. return $this->xmlrpc->send_error_message('100', 'Invalid Access');
  83. }
  84. $blogid = $parameters['0'];
  85. $categories = $this->blogs->getCats($blogid);
  86. foreach ($categories as $cat) {
  87. $array[] = array(
  88. array(
  89. 'categoryId' => array($cat['catId'], 'string'),
  90. 'title' => array($cat['category'], 'string'),
  91. 'description' => array($cat['category'], 'string'),
  92. 'htmlUrl' => array(site_url(), 'string'),
  93. 'rssUrl' => array(site_url(), 'string'),
  94. ), 'struct'
  95. );
  96. }
  97. $response = array($array, 'array');
  98. return $this->xmlrpc->send_response($response);
  99. }
  100. function getUserInfo($request) { //todo
  101. $parameters = $request->output_parameters();
  102. if (!$this->login($parameters['1'], $parameters['2'])) {
  103. return $this->xmlrpc->send_error_message('100', 'Invalid Access');
  104. }
  105. $response = array(
  106. array(
  107. 'nickname' => array('Your Name', 'string'),
  108. 'userid' => array('userid', 'string'),
  109. 'url' => array('http://your-awsome-website.com', 'string'),
  110. 'email' => array('your@email.com', 'string'),
  111. 'lastname' => array('Last Name', 'string'),
  112. 'firstname' => array('First Name', 'string'),
  113. ), 'struct'
  114. );
  115. return $this->xmlrpc->send_response($response);
  116. }
  117. function getRecentPosts($request) {
  118. $parameters = $request->output_parameters();
  119. if (!$this->login($parameters['1'], $parameters['2'])) {
  120. return $this->xmlrpc->send_error_message('100', 'Invalid Access');
  121. }
  122. $blogid = $parameters['0'];
  123. $numposts = $parameters['3'];
  124. $blogs = $this->blogs->getRecent($blogid, $numposts, '');
  125. foreach ($blogs as $blog) {
  126. $cats = $this->blogs->getPostCats($blog['postId']);
  127. foreach ($cats as $cat) {
  128. $category[] = array($cat['category'], 'string');
  129. }
  130. $categories = array($category, 'array');
  131. $array[] = array(
  132. array(
  133. 'postid' => array($blog['postId'], 'string'),
  134. 'dateCreated' => array(standard_date('DATE_ISO8601', mysql_to_unix($blog['date'])), 'dateTime.iso8601'),
  135. 'title' => array($blog['title'], 'string'),
  136. 'description' => array($blog['description'], 'string'),
  137. 'categories' => $categories,
  138. 'publish' => array($blog['published'], 'boolean'),
  139. ),
  140. 'struct'
  141. );
  142. }
  143. $response = array($array, 'array');
  144. return $this->xmlrpc->send_response($response);
  145. }
  146. function getPost($request) {
  147. $parameters = $request->output_parameters();
  148. if (!$this->login($parameters['1'], $parameters['2'])) {
  149. return $this->xmlrpc->send_error_message('100', 'Invalid Access');
  150. }
  151. $postid = $parameters['0'];
  152. $blogs = $this->blogs->getPost_id($postid);
  153. foreach ($blogs as $blog) {
  154. $cats = $this->blogs->getPostCats($postid);
  155. foreach ($cats as $cat) {
  156. $category[] = array($cat['category'], 'string');
  157. }
  158. $categories = array($category, 'array');
  159. $array = array(
  160. array(
  161. 'postid' => array($blog['postId'], 'string'),
  162. 'dateCreated' => array(standard_date('DATE_ISO8601', mysql_to_unix($blog['date'])), 'dateTime.iso8601'),
  163. 'title' => array($blog['title'], 'string'),
  164. 'description' => array($blog['description'], 'string'),
  165. 'categories' => $categories,
  166. 'publish' => array($blog['published'], 'boolean'),
  167. ),
  168. 'struct'
  169. );
  170. }
  171. $response = $array;
  172. return $this->xmlrpc->send_response($response);
  173. }
  174. function newPost($request) {
  175. $parameters = $request->output_parameters();
  176. if (!$this->login($parameters['1'], $parameters['2'])) {
  177. return $this->xmlrpc->send_error_message('100', 'Invalid Access');
  178. }
  179. $blogid = $parameters['0'];
  180. $content = $parameters['3'];
  181. $publish = $parameters['4'];
  182. $insert = array(
  183. 'blogId' => $blogid,
  184. 'title' => $content['title'],
  185. 'user_id' => $user_id,
  186. 'date' => mdate("%Y-%m-%d %H:%i:%s"),
  187. 'description' => $content['description'],
  188. 'published' => $publish,
  189. 'url_friendly' => url_title($content['title']),
  190. 'publishDate' => (isset($content['dateCreated']) && $content['dateCreated'] ?
  191. mdate("%Y-%m-%d %H:%i:%s", strtotime($content['dateCreated'])) :
  192. mdate("%Y-%m-%d %H:%i:%s")),
  193. );
  194. if (($insertid = $this->blogs->newPost($insert)) && write_file('images/entry.txt', print_r($parameters, true))) {
  195. foreach ($content['categories'] as $cat) {
  196. $this->blogs->newPostCat($insertid, $cat);
  197. }
  198. $response = array($insertid, 'string');
  199. return $this->xmlrpc->send_response($response);
  200. }
  201. return $this->xmlrpc->send_error_message('1', 'Failed to Post');
  202. }
  203. function editPost($request) {
  204. $parameters = $request->output_parameters();
  205. if (!$this->login($parameters['1'], $parameters['2'])) {
  206. return $this->xmlrpc->send_error_message('100', 'Invalid Access');
  207. }
  208. $postid = $parameters['0'];
  209. $content = $parameters['3'];
  210. $publish = $parameters['4'];
  211. $insert = array(
  212. 'title' => $content['title'],
  213. 'updated' => mdate("%Y-%m-%d %H:%i:%s"),
  214. 'description' => $content['description'],
  215. 'published' => $publish,
  216. 'url_friendly' => url_title($content['title']),
  217. 'publishDate' => (isset($content['dateCreated']) && $content['dateCreated'] ?
  218. mdate("%Y-%m-%d %H:%i:%s", strtotime($content['dateCreated'])) :
  219. mdate("%Y-%m-%d %H:%i:%s")),
  220. );
  221. if ($this->blogs->editPost($postid, $insert) && $file = write_file('images/entry.txt', print_r($parameters, true))) {
  222. $categories = (isset($content['categories']) && $content['categories'] ? $content['categories'] : '');
  223. $this->blogs->updatePostCat($postid, $categories);
  224. $response = array(true, 'boolean');
  225. } else {
  226. $response = array(false, 'boolean');
  227. }
  228. return $this->xmlrpc->send_response($response);
  229. }
  230. function deletePost($request) {
  231. $parameters = $request->output_parameters();
  232. if (!$this->login($parameters['2'], $parameters['3'])) {
  233. return $this->xmlrpc->send_error_message('100', 'Invalid Access');
  234. }
  235. $this->blogs->deletePost($parameters['1']);
  236. $response = array(true, 'boolean');
  237. return $this->xmlrpc->send_response($response);
  238. }
  239. function newMediaObject($request) {
  240. $parameters = $request->output_parameters();
  241. if (!$this->login($parameters['1'], $parameters['2'])) {
  242. return $this->xmlrpc->send_error_message('100', 'Invalid Access');
  243. }
  244. $blogid = $parameters['0'];
  245. $file = $parameters['3'];
  246. $filename = $file['name'];
  247. $filename = substr($filename, (strrpos($filename, "/") ? strrpos($filename, "/") + 1 : 0));
  248. if (write_file('images/blog/' . $filename, $file['bits'])) {
  249. $response = array(
  250. array(
  251. 'url' => array('http://www.lastrose.com/blog/images/blog/' . $filename, 'string')
  252. ), 'struct'
  253. );
  254. return $this->xmlrpc->send_response($response);
  255. }
  256. return $this->xmlrpc->send_error_message('2', 'File Failed to Write');
  257. }
  258. }
  259. array(
  260. array(
  261. 'request' => array('login','string'),
  262. 'username' => array('newuser','string'),
  263. 'password' => array(sha1('123123'),'string')
  264. ),'struct'
  265. );