PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/www/app/AdminModule/presenters/AdminPublishPresenter.php

https://github.com/bazo/Mokuji
PHP | 477 lines | 425 code | 42 blank | 10 comment | 26 complexity | 7a793829817cc0dda70d65b818dccd98 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. class Admin_PublishPresenter extends Admin_BasePresenter
  3. {
  4. public $oldLayoutMode = false;
  5. public $oldModuleMode = false;
  6. public $error;
  7. private $methods = array(
  8. // WordPress API
  9. 'wp.getUsersBlogs' => 'this:getUsersBlogs',
  10. 'wp.getPage' => 'this:getPage',
  11. 'wp.getPages' => 'this:getPages',
  12. 'wp.newPage' => 'this:newPage',
  13. 'wp.deletePage' => 'this:deletePage',
  14. 'wp.editPage' => 'this:editPage',
  15. 'wp.getPageList' => 'this:getPageList',
  16. 'wp.getAuthors' => 'this:getAuthors',
  17. 'wp.getCategories' => 'this:getCategories', // Alias
  18. 'wp.getTags' => 'this:getTags',
  19. 'wp.newCategory' => 'this:newCategory',
  20. 'wp.deleteCategory' => 'this:deleteCategory',
  21. 'wp.suggestCategories' => 'this:suggestCategories',
  22. 'wp.uploadFile' => 'this:newMediaObject', // Alias
  23. 'wp.getCommentCount' => 'this:getCommentCount',
  24. 'wp.getPostStatusList' => 'this:getPostStatusList',
  25. 'wp.getPageStatusList' => 'this:getPageStatusList',
  26. 'wp.getPageTemplates' => 'this:getPageTemplates',
  27. 'wp.getOptions' => 'this:getOptions',
  28. 'wp.setOptions' => 'this:setOptions',
  29. 'wp.getComment' => 'this:getComment',
  30. 'wp.getComments' => 'this:getComments',
  31. 'wp.deleteComment' => 'this:deleteComment',
  32. 'wp.editComment' => 'this:editComment',
  33. 'wp.newComment' => 'this:newComment',
  34. 'wp.getCommentStatusList' => 'this:getCommentStatusList',
  35. // Blogger API
  36. 'blogger.getUsersBlogs' => 'this:getUsersBlogs',
  37. 'blogger.getUserInfo' => 'this:getUserInfo',
  38. 'blogger.getPost' => 'this:getPost',
  39. 'blogger.getRecentPosts' => 'this:getRecentPosts',
  40. 'blogger.getTemplate' => 'this:getTemplate',
  41. 'blogger.setTemplate' => 'this:setTemplate',
  42. 'blogger.newPost' => 'this:newPost',
  43. 'blogger.editPost' => 'this:editPost',
  44. 'blogger.deletePost' => 'this:deletePost',
  45. // MetaWeblog API (with MT extensions to structs)
  46. 'metaWeblog.newPost' => 'this:newPost',
  47. 'metaWeblog.editPost' => 'this:editPost',
  48. 'metaWeblog.getPost' => 'this:getPost',
  49. 'metaWeblog.getRecentPosts' => 'this:getRecentPosts',
  50. 'metaWeblog.getCategories' => 'this:getCategories',
  51. 'metaWeblog.newMediaObject' => 'this:newMediaObject',
  52. // MetaWeblog API aliases for Blogger API
  53. // see http://www.xmlrpc.com/stories/storyReader$2460
  54. 'metaWeblog.deletePost' => 'this:deletePost',
  55. 'metaWeblog.getTemplate' => 'this:getTemplate',
  56. 'metaWeblog.setTemplate' => 'this:setTemplate',
  57. 'metaWeblog.getUsersBlogs' => 'this:getUsersBlogs',
  58. // MovableType API
  59. 'mt.getCategoryList' => 'getCategoryList',
  60. 'mt.getRecentPostTitles' => 'getRecentPostTitles',
  61. 'mt.getPostCategories' => 'getPostCategories',
  62. 'mt.setPostCategories' => 'setPostCategories',
  63. 'mt.supportedMethods' => 'supportedMethods',
  64. 'mt.supportedTextFilters' => 'supportedTextFilters',
  65. 'mt.getTrackbackPings' => 'getTrackbackPings',
  66. 'mt.publishPost' => 'publishPost',
  67. );
  68. public function startup()
  69. {
  70. parent::startup();
  71. $this->absoluteUrls = TRUE;
  72. $admin_config = ConfigAdapterIni::load(APP_DIR.'/config/admin.ini');
  73. foreach($admin_config['admin'] as $var => $value)
  74. {
  75. Environment::setVariable($var, $value);
  76. }
  77. Environment::setVariable('themesDir', 'themes');
  78. $method = $this->getRequest()->getMethod();
  79. if($method == 'GET')
  80. {
  81. $data = $method = $this->getRequest()->getParams();
  82. if(isset($data['rsd']))
  83. {
  84. header('Content-Type: text/xml');
  85. $this->view = 'rsd';
  86. }
  87. elseif(isset($data['wlw']))
  88. {
  89. header('Content-Type: text/xml');
  90. $this->view = 'wlw';
  91. }
  92. }
  93. else
  94. {
  95. if ( !isset( $HTTP_RAW_POST_DATA ) ) {
  96. $HTTP_RAW_POST_DATA = file_get_contents( 'php://input' );
  97. }
  98. $data = $HTTP_RAW_POST_DATA;
  99. file_put_contents('xmlrpc.txt', $data , FILE_APPEND);
  100. $this->formatCallbacks($this->methods);
  101. $server = new IXR_Server($this->methods, $data);
  102. }
  103. }
  104. public function nullFilter($s)
  105. {
  106. return $s;
  107. }
  108. private function formatCallbacks(& $callbacks)
  109. {
  110. foreach($callbacks as $key => $callback)
  111. {
  112. $callbacks[$key] = $this->formatCallback($callback);
  113. }
  114. }
  115. private function formatCallback($callback)
  116. {
  117. $method = str_replace('this:', '', $callback);
  118. return array($this, $method);
  119. }
  120. private function login($username, $password)
  121. {
  122. $user = Environment::getUser();
  123. if( !$user->isAuthenticated())
  124. {
  125. $user->setAuthenticationHandler(new Admin_UserModel());
  126. try
  127. {
  128. $user->authenticate($username, $password);
  129. $session_conf = Environment::getVariable('session');
  130. $user->setExpiration($session_conf['expiration'], true);
  131. return $user;
  132. }
  133. catch (AuthenticationException $e)
  134. {
  135. $this->error = new IXR_Error(403, $e->getMessage());
  136. return false;
  137. }
  138. }
  139. return $user;
  140. }
  141. /* API IMPLEMENTATIONS */
  142. /* BLOGGER API */
  143. public function getUsersBlogs($args)
  144. {
  145. $username = $args[1];
  146. $password = $args[2];
  147. $user = $this->login($username, $password);
  148. if ( !$user ) {
  149. return $this->error;
  150. }
  151. $struct = array(
  152. 'isAdmin' => 1,
  153. 'url' => $this->link(':Front:Homepage:homepage'),
  154. 'blogid' => '1',
  155. 'blogName' => 'Mokuji',
  156. 'xmlrpc' => $this->link(':Admin:Publish:default')
  157. );
  158. return array($struct);
  159. }
  160. public function deletePost($args)
  161. {
  162. $pageId = (int)$args[1];
  163. $username = $args[2];
  164. $password = $args[3];
  165. $user = $this->login($username, $password);
  166. if ( !$user ) {
  167. return $this->error;
  168. }
  169. try
  170. {
  171. return $this->model('pages')->deleteById($pageId);
  172. }
  173. catch(Exception $e)
  174. {
  175. return new IXR_Error(500, $e->getMessage());
  176. }
  177. }
  178. public function newPost($args)
  179. {
  180. $values = $this->preparePost($args);
  181. try
  182. {
  183. $pageId = $this->model('pages')->save($values);
  184. }
  185. catch(Exception $e)
  186. {
  187. return new IXR_Error(500, $e->getMessage());
  188. }
  189. return (string)$pageId;
  190. }
  191. public function editPost($args)
  192. {
  193. $values = $this->preparePost($args);
  194. $values['id'] = $args[0];
  195. try
  196. {
  197. $this->model('pages')->update($values);
  198. }
  199. catch(Exception $e)
  200. {
  201. return new IXR_Error(500, $e->getMessage());
  202. }
  203. return true;
  204. }
  205. private function preparePost($args)
  206. {
  207. $blogId = (int)$args[0];
  208. $username = $args[1];
  209. $password = $args[2];
  210. $user = $this->login($username, $password);
  211. if ( !$user ) {
  212. return $this->error;
  213. }
  214. $values = array();
  215. $values['author'] = $user->getIdentity()->getId();
  216. //$values['author'] = $user->getIdentity()->getName();
  217. $values['title'] = $args[3]['title'];
  218. $values['content'] = $args[3]['description'];
  219. if(isset($args[3]['date_created_gmt']))
  220. {
  221. $publish_time = $args[3]['date_created_gmt'];
  222. $timezone = new DateTimeZone((string)date_default_timezone_get());
  223. $datetime = new DateTime('now', $timezone);
  224. $offset = date_offset_get($datetime);
  225. $values['publish_time'] = $publish_time->getTimestamp() + $offset;
  226. }
  227. $values['published'] = $args[4];
  228. if( isset($args[3]['categories'][0]) )
  229. {
  230. $category = $this->model('categories')->getByName($args[3]['categories'][0]);
  231. $values['category'] = (int)$category->id;
  232. }
  233. else $values['category'] = 0;
  234. $values['content_type'] = 'page';
  235. return $values;
  236. }
  237. public function getPost($args)
  238. {
  239. $pageId = (int)$args[0];
  240. $username = $args[1];
  241. $password = $args[2];
  242. $user = $this->login($username, $password);
  243. if ( !$user ) {
  244. return $this->error;
  245. }
  246. try
  247. {
  248. $page = $this->model('pages')->getById($pageId);
  249. if($page == false)
  250. {
  251. return new IXR_Error(500, 'No such post');
  252. }
  253. }
  254. catch(Exception $e)
  255. {
  256. return new IXR_Error(500, $e->getMessage());
  257. }
  258. return $this->fillPost($page, $user);
  259. }
  260. public function getRecentPosts($args)
  261. {
  262. $blogId = (int)$args[0];
  263. $username = $args[1];
  264. $password = $args[2];
  265. $posts_count = (int)$args[3];
  266. $user = $this->login($username, $password);
  267. if ( !$user ) {
  268. return $this->error;
  269. }
  270. try
  271. {
  272. $pages = $this->model('pages')->getRecent($posts_count);
  273. }
  274. catch(Exception $e)
  275. {
  276. return new IXR_Error(500, $e->getMessage());
  277. }
  278. $struct = array();
  279. foreach ($pages as $page) {
  280. $struct[] = $this->fillPost($page, $user);
  281. }
  282. return $struct;
  283. }
  284. private function fillPost($page, $user)
  285. {
  286. $struct = array();
  287. try
  288. {
  289. if($page->category == 0) $struct['link'] = $this->link(':Front:Page:categoryView', array('category' => $page->slug));
  290. else $struct['link'] = $this->link(':Front:Page:pageView', array('category' => $page->category_slug, 'page' => $page->slug));
  291. }
  292. catch(Exception $e)
  293. {
  294. return new IXR_Error(500, $e->getMessage());
  295. }
  296. if($page->category != 0) $struct['link'] = $this->link(':Front:Page:categoryView', array('category' => $page->slug));
  297. else $struct['link'] = $this->link(':Front:Page:pageView', array('category' => $page->category_slug, 'page' => $page->slug));
  298. $struct['permaLink'] = $struct['link'];
  299. $struct['userid'] = $user->getIdentity()->getId();
  300. //$struct['userid'] = (int)$user->getIdentity()->getName();
  301. $struct['postid'] = (string)$page->id;
  302. $struct['dateCreated'] = new IXR_date($page->added);
  303. $struct['title'] = (string)$page->title;
  304. $struct['description'] = (string)$page->content;
  305. $struct['categories'] = array(0 => $page->category_title);
  306. $struct['publish'] = (bool)$page->published;
  307. return $struct;
  308. }
  309. public function newCategory($args)
  310. {
  311. $blogId = (int)$args[0];
  312. $username = $args[1];
  313. $password = $args[2];
  314. $user = $this->login($username, $password);
  315. if ( !$user ) {
  316. return $this->error;
  317. }
  318. $values['title'] = $args[3]['name'];
  319. $values['parent'] = (int)$args[3]['parent_id'];
  320. $values['slug'] = String::webalize($values['title']);
  321. try
  322. {
  323. $cat = $this->model('categories')->save($values);
  324. }
  325. catch(Exception $e)
  326. {
  327. return new IXR_Error(500, $e->getMessage());
  328. }
  329. return $cat['id'];
  330. }
  331. public function getCategories($args)
  332. {
  333. $blogId = (int)$args[0];
  334. $username = $args[1];
  335. $password = $args[2];
  336. $user = $this->login($username, $password);
  337. if ( !$user ) {
  338. return $this->error;
  339. }
  340. $categories_struct = array();
  341. try
  342. {
  343. $categories = $this->model('categories')->getAll();
  344. }
  345. catch(Exception $e)
  346. {
  347. return new IXR_Error(500, $e->getMessage());
  348. }
  349. foreach ( $categories as $cat )
  350. {
  351. $struct['categoryId'] = $cat->id;
  352. $struct['parentId'] = $cat->parent;
  353. $struct['description'] = $cat->title;
  354. $struct['categoryDescription'] = $cat->description;
  355. $struct['categoryName'] = $cat->title;
  356. $struct['htmlUrl'] = $this->link(':Front:Page:categoryView', array('category' => $cat->slug));
  357. $struct['rssUrl'] = $this->link(':Front:Rss:rss', array('category' => $cat->slug));
  358. $categories_struct[] = $struct;
  359. }
  360. return $categories_struct;
  361. }
  362. public function newMediaObject($args)
  363. {
  364. $blogId = (int)$args[0];
  365. $username = $args[1];
  366. $password = $args[2];
  367. $user = $this->login($username, $password);
  368. if ( !$user ) {
  369. return $this->error;
  370. }
  371. $path = 'media/pictures/pages/';
  372. $filename = str_replace('/', '_', $args[3]['name']);
  373. $data = $args[3]['bits'];
  374. $type = $args[3]['type'];
  375. file_put_contents($path.$filename, $data);
  376. $struct = array();
  377. $struct['file'] = $filename;
  378. $uri = $this->getHttpRequest()->getUri();
  379. $uri->baseUri;
  380. $uri->hostUri;
  381. $struct['url'] = $uri->baseUri.$path.$filename;
  382. $struct['type'] = $type;
  383. return $struct;
  384. }
  385. public function setPostCategories($args)
  386. {
  387. return true;
  388. }
  389. public function getPostCategories($args)
  390. {
  391. $pageId = (int)$args[0];
  392. $username = $args[1];
  393. $password = $args[2];
  394. $user = $this->login($username, $password);
  395. if ( !$user ) {
  396. return $this->error;
  397. }
  398. try
  399. {
  400. $page = $this->model('pages')->getById($pageId);
  401. }
  402. catch(Exception $e)
  403. {
  404. return new IXR_Error(500, $e->getMessage());
  405. }
  406. $struct['categoryId'] = $page->category_id;
  407. $struct['categoryName'] = $page->category_title;
  408. return array($struct);
  409. }
  410. public function getTags($args)
  411. {
  412. $pageId = (int)$args[0];
  413. $username = $args[1];
  414. $password = $args[2];
  415. $user = $this->login($username, $password);
  416. if ( !$user ) {
  417. return $this->error;
  418. }
  419. $tags = $this->model('tags')->getAll();
  420. $array = array();
  421. foreach($tags as $tag)
  422. {
  423. $struct = array();
  424. $struct['tag_id'] = (int)$tag->tag_id;
  425. $struct['name'] = (string)$tag->tag_name;
  426. $struct['count'] = 1;
  427. $struct['slug'] = String::webalize($struct['name']);
  428. $struct['html_url'] = '';
  429. $struct['rss_url'] = '';
  430. $array[] = $struct;
  431. }
  432. return $array;
  433. }
  434. }
  435. ?>