/var/Widget/XmlRpc.php
http://typecho.googlecode.com/ · PHP · 2093 lines · 1294 code · 216 blank · 583 comment · 182 complexity · bf2e49967123395911f6fd693f80cd05 MD5 · raw file
Large files are truncated click here to view the full file
- <?php
- /**
- * Typecho Blog Platform
- *
- * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
- * @license GNU General Public License 2.0
- * @version $Id$
- */
- /**
- * XmlRpc??
- *
- * @author blankyao
- * @category typecho
- * @package Widget
- * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
- * @license GNU General Public License 2.0
- */
- class Widget_XmlRpc extends Widget_Abstract_Contents implements Widget_Interface_Do
- {
- /**
- * ????
- *
- * @access private
- * @var IXR_Error
- */
- private $error;
-
- /**
- * wordpress???????
- *
- * @access private
- * @var array
- */
- private $_wpOptions;
-
- /**
- * ??????????
- *
- * @access private
- * @var array
- */
- private $_usedWidgetNameList = array();
-
- /**
- * ??????
- *
- * @access private
- * @param Widget_Abstract_Contents $content
- * @return array
- */
- private function getPostExtended(Widget_Abstract_Contents $content)
- {
- //??????????????html??
- $agent = $this->request->getAgent();
- $text = '';
-
- switch (true) {
- case false !== strpos($agent, 'wp-iphone'): // wordpress iphone???
- case false !== strpos($agent, 'wp-blackberry'): // ??
- case false !== strpos($agent, 'wp-andriod'): // andriod
- case false !== strpos($agent, 'plain-text'): // ??????????????, ??????????????
- $text = $content->text;
- break;
- default:
- $text = $content->content;
- break;
- }
-
- $post = explode('<!--more-->', $text, 2);
- return array(Typecho_Common::fixHtml($post[0]), isset($post[1]) ? Typecho_Common::fixHtml($post[1]) : NULL);
- }
-
- /**
- * ?typecho????????wordperss???
- *
- * @access private
- * @param string $status typecho???
- * @param string $type ????
- * @return string
- */
- private function typechoToWordpressStatus($status, $type = 'post')
- {
- if ('post' == $type) {
- /** ???? */
- switch ($status) {
- case 'waiting':
- return 'pending';
- case 'publish':
- case 'draft':
- case 'private':
- return $status;
- default:
- return 'publish';
- }
- } else if ('page' == $type) {
- switch ($status) {
- case 'publish':
- case 'draft':
- case 'private':
- return $status;
- default:
- return 'publish';
- }
- } else if ('comment' == $type) {
- switch ($status) {
- case 'publish':
- case 'approved':
- return 'approve';
- case 'waiting':
- return 'hold';
- case 'spam':
- return $status;
- default:
- return 'approve';
- }
- }
-
- return '';
- }
-
- /**
- * ?wordpress????????typecho???
- *
- * @access private
- * @param string $status wordpress???
- * @param string $type ????
- * @return string
- */
- private function wordpressToTypechoStatus($status, $type = 'post')
- {
- if ('post' == $type) {
- /** ???? */
- switch ($status) {
- case 'pending':
- return 'waiting';
- case 'publish':
- case 'draft':
- case 'private':
- case 'waiting':
- return $status;
- default:
- return 'publish';
- }
- } else if ('page' == $type) {
- switch ($status) {
- case 'publish':
- case 'draft':
- case 'private':
- return $status;
- default:
- return 'publish';
- }
- } else if ('comment' == $type) {
- switch ($status) {
- case 'approve':
- case 'publish':
- case 'approved':
- return 'approved';
- case 'hold':
- case 'waiting':
- return 'waiting';
- case 'spam':
- return $status;
- default:
- return 'approved';
- }
- }
-
- return '';
- }
-
- /**
- * ??????,???????????
- *
- * @access public
- * @param string $alias ????
- * @param mixed $params ?????
- * @param mixed $request ????
- * @param boolean $enableResponse ????http??
- * @return object
- * @throws Typecho_Exception
- */
- private function singletonWidget($alias, $params = NULL, $request = NULL, $enableResponse = true)
- {
- $this->_usedWidgetNameList[] = $alias;
- return Typecho_Widget::widget($alias, $params, $request, $enableResponse);
- }
- /**
- * ????????, ?????????
- *
- * @access public
- * @param boolen $run ????
- * @return void
- */
- public function execute($run = false)
- {
- if ($run) {
- parent::execute();
- }
-
- $this->_wpOptions = array(
- // Read only options
- 'software_name' => array(
- 'desc' => _t( '????' ),
- 'readonly' => true,
- 'value' => $this->options->software
- ),
- 'software_version' => array(
- 'desc' => _t( '????' ),
- 'readonly' => true,
- 'value' => $this->options->version
- ),
- 'blog_url' => array(
- 'desc' => _t( '????' ),
- 'readonly' => true,
- 'option' => 'siteUrl'
- ),
- // Updatable options
- 'time_zone' => array(
- 'desc' => _t( '??' ),
- 'readonly' => false,
- 'option' => 'timezone'
- ),
- 'blog_title' => array(
- 'desc' => _t( '????' ),
- 'readonly' => false,
- 'option' => 'title'
- ),
- 'blog_tagline' => array(
- 'desc' => _t( '?????' ),
- 'readonly' => false,
- 'option' => 'description'
- ),
- 'date_format' => array(
- 'desc' => _t( '????' ),
- 'readonly' => false,
- 'option' => 'postDateFormat'
- ),
- 'time_format' => array(
- 'desc' => _t( '????' ),
- 'readonly' => false,
- 'option' => 'postDateFormat'
- ),
- 'users_can_register' => array(
- 'desc' => _t( '??????' ),
- 'readonly' => false,
- 'option' => 'allowRegister'
- )
- );
- }
- /**
- * ????
- *
- * @access public
- * @return void
- */
- public function checkAccess($name, $password, $level = 'contributor')
- {
- if ($this->user->login($name, $password, true)) {
- /** ???? */
- if ($this->user->pass($level, true)) {
- return true;
- } else {
- $this->error = new IXR_Error(403, _t('????'));
- return false;
- }
- } else {
- $this->error = new IXR_Error(403, _t('????, ????'));
- return false;
- }
- }
- /** about wp xmlrpc api, you can see http://codex.wordpress.org/XML-RPC*/
- /**
- * ??pageId???page
- *
- * @param int $blogId
- * @param int $pageId
- * @param string $userName
- * @param string $password
- * @access public
- * @return struct $pageStruct
- */
- public function wpGetPage($blogId, $pageId, $userName, $password)
- {
- /** ???? */
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
- /** ???? */
- try {
- /** ??Widget_Contents_Page_Edit??request?????, ??????????flush??request */
- /** widget???????????????????widget?request?? */
- /** ????????????? */
- $page = $this->singletonWidget('Widget_Contents_Page_Edit', NULL, "cid={$pageId}");
- } catch (Typecho_Widget_Exception $e) {
- /** ??????????(?? Widget_Contents_Page_Edit ? execute ??) */
- return new IXR_Error($e->getCode(), $e->getMessage());
- }
- /** ??????????????description?text_more*/
- list($excerpt, $more) = $this->getPostExtended($page);
- $pageStruct = array(
- 'dateCreated' => new IXR_Date($this->options->timezone + $page->created),
- 'userid' => $page->authorId,
- 'page_id' => $page->cid,
- 'page_status' => $this->typechoToWordpressStatus($page->status, 'page'),
- 'description' => $excerpt,
- 'title' => $page->title,
- 'link' => $page->permalink,
- 'permalink' => $page->permalink,
- 'categories' => $page->categories,
- 'excerpt' => $page->description,
- 'text_more' => $more,
- 'mt_allow_comments' => intval($page->allowComment),
- 'mt_allow_pings' => intval($page->allowPing),
- 'wp_slug' => $page->slug,
- 'wp_password' => $page->password,
- 'wp_author' => $page->author->name,
- 'wp_page_parent_id' => '0',
- 'wp_page_parent_title' => '',
- 'wp_page_order' => $page->order, //meta?????, ?page?????
- 'wp_author_id' => $page->authorId,
- 'wp_author_display_name' => $page->author->screenName,
- 'date_created_gmt' => new IXR_Date($page->created),
- 'custom_fields' => array(),
- 'wp_page_template' => $page->template
- );
- return $pageStruct;
- }
- /**
- * ?????page
- *
- * @param int $blogId
- * @param string $userName
- * @param string $password
- * @access public
- * @return array(contains $pageStruct)
- */
- public function wpGetPages($blogId, $userName, $password)
- {
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
- /** ??type?page?contents */
- /** ????flush??, ??????status??? */
- $pages = $this->singletonWidget('Widget_Contents_Page_Admin', NULL, 'status=all');
- /** ??????????? */
- $pageStructs = array();
- while ($pages->next()) {
- /** ??????????????description?text_more*/
- list($excerpt, $more) = $this->getPostExtended($pages);
- $pageStructs[] = array(
- 'dateCreated' => new IXR_Date($this->options->timezone + $pages->created),
- 'userid' => $pages->authorId,
- 'page_id' => $pages->cid,
- /** todo:????? */
- 'page_status' => $this->typechoToWordpressStatus($pages->status, 'page'),
- 'description' => $excerpt,
- 'title' => $pages->title,
- 'link' => $pages->permalink,
- 'permalink' => $pages->permalink,
- 'categories' => $pages->categories,
- 'excerpt' => $pages->description,
- 'text_more' => $more,
- 'mt_allow_comments' => intval($pages->allowComment),
- 'mt_allow_pings' => intval($pages->allowPing),
- 'wp_slug' => $pages->slug,
- 'wp_password' => $pages->password,
- 'wp_author' => $pages->author->name,
- 'wp_page_parent_id' => '0',
- 'wp_page_parent_title' => '',
- 'wp_page_order' => $pages->order, //meta?????, ?page?????
- 'wp_author_id' => $pages->authorId,
- 'wp_author_display_name' => $pages->author->screenName,
- 'date_created_gmt' => new IXR_Date($pages->created),
- 'custom_fields' => array(),
- 'wp_page_template' => $pages->template
- );
- }
- return $pageStructs;
- }
- /**
- * ?????page
- *
- * @param int $blogId
- * @param string $userName
- * @param string $password
- * @param struct $content
- * @param bool $publish
- * @access public
- * @return void
- */
- public function wpNewPage($blogId, $userName, $password, $content, $publish)
- {
- if (!$this->checkAccess($userName, $password, 'editor')) {
- return $this->error;
- }
- $content['post_type'] = 'page';
- $this->mwNewPost($blogId, $userName, $password, $content, $publish);
- }
- /**
- * ??pageId???page
- *
- * @param int $blogId
- * @param string $userName
- * @param string $password
- * @param int $pageId
- * @access public
- * @return bool
- */
- public function wpDeletePage($blogId, $userName, $password, $pageId)
- {
- if (!$this->checkAccess($userName, $password, 'editor')) {
- return $this->error;
- }
- /** ???? */
- try {
- /** ????????????? */
- $this->singletonWidget('Widget_Contents_Page_Edit', NULL, "cid={$pageId}", false)->deletePage();
- } catch (Typecho_Widget_Exception $e) {
- /** ??????????(?? Widget_Contents_Page_Edit ? execute ??) */
- return new IXR_Error($e->getCode(), $e->getMessage());
- }
- return true;
- }
- /**
- * ??pageId???page
- *
- * @param int $blogId
- * @param int $pageId
- * @param string $userName
- * @param string $password
- * @param struct $content
- * @param bool $publish
- * @access public
- * @return bool
- */
- public function wpEditPage($blogId, $pageId, $userName, $password, $content, $publish)
- {
- $content['type'] = 'page';
- $this->mwEditPost($blogId, $pageId, $userName, $password, $content, $publish);
- }
- /**
- * ??page?????wpGetPages?????
- *
- * @param int $blogId
- * @param string $userName
- * @param string $password
- * @access public
- * @return array
- */
- public function wpGetPageList($blogId, $userName, $password)
- {
- if (!$this->checkAccess($userName, $password, 'editor')) {
- return ($this->error);
- }
- $pages = $this->singletonWidget('Widget_Contents_Page_Admin', NULL, 'status=all');
- /**???*/
- $pageStructs = array();
- while ($pages->next()) {
- $pageStructs[] = array(
- 'dateCreated' => new IXR_Date($this->options->timezone + $pages->created),
- 'date_created_gmt' => new IXR_Date($this->options->timezone + $pages->created),
- 'page_id' => $pages->cid,
- 'page_title' => $pages->title,
- 'page_parent_id' => '0',
- );
- }
- return $pageStructs;
- }
- /**
- * ?????blog????????????
- *
- * @param int $blogId
- * @param string $userName
- * @param string $password
- * @access public
- * @return struct
- */
- public function wpGetAuthors($blogId, $userName, $password)
- {
- if (!$this->checkAccess($userName, $password, 'editor')) {
- return ($this->error);
- }
- /** ????*/
- $select = $this->db->select('table.users.uid', 'table.users.name', 'table.users.screenName')->from('table.users');
- $authors = $this->db->fetchAll($select);
- $authorStructs = array();
- foreach ($authors as $author) {
- $authorStructs[] = array(
- 'user_id' => $author['uid'],
- 'user_login' => $author['name'],
- 'display_name' => $author['screenName']
- );
- }
- return $authorStructs;
- }
- /**
- * ????????
- *
- * @param int $blogId
- * @param string $userName
- * @param string $password
- * @param struct $category
- * @access public
- * @return void
- */
- public function wpNewCategory($blogId, $userName, $password, $category)
- {
- if (!$this->checkAccess($userName, $password)) {
- return ($this->error);
- }
- /** ?????? */
- $input['name'] = $category['name'];
- $input['slug'] = Typecho_Common::slugName(empty($category['slug']) ? $category['name'] : $category['slug']);
- $input['type'] = 'category';
- $input['description'] = isset($category['description']) ? $category['description'] : $category['name'];
- $input['do'] = 'insert';
- /** ?????? */
- try {
- /** ?? */
- $this->singletonWidget('Widget_Metas_Category_Edit', NULL, $input, false)->action();
- return $this->singletonWidget('Widget_Notice')->getHighlightId() ? true : false;
- } catch (Typecho_Widget_Exception $e) {
- return new IXR_Error($e->getCode(), $e->getMessage());
- }
- return true;
- }
- /**
- * ??????string??????????
- *
- * @param int $blogId
- * @param string $userName
- * @param string $password
- * @param string $category
- * @param int $max_results
- * @access public
- * @return array
- */
- public function wpSuggestCategories($blogId, $userName, $password, $category, $max_results)
- {
- if (!$this->checkAccess($userName, $password)) {
- return ($this->error);
- }
- $meta = $this->singletonWidget('Widget_Abstract_Metas');
- /** ???????????*/
- $key = Typecho_Common::filterSearchQuery($category);
- $key = '%' . $key . '%';
- $select = $meta->select()->where('table.metas.type = ? AND (table.metas.name LIKE ? OR slug LIKE ?)', 'category', $key, $key);
- /** ??category push?contents???? */
- $categories = $this->db->fetchAll($select);
- /** ???categorise??*/
- $categoryStructs = array();
- foreach ($categories as $category) {
- $categoryStructs[] = array(
- 'category_id' => $category['mid'],
- 'category_name' => $category['name'],
- );
- }
- return $categoryStructs;
- }
-
- /**
- * ????
- *
- * @access public
- * @param string $userName ???
- * @param string $password ??
- * @return array
- */
- public function wpGetUsersBlogs($userName, $password)
- {
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
- $struct = array();
- $struct[] = array(
- 'isAdmin' => $this->user->pass('administrator', true),
- 'url' => $this->options->siteUrl,
- 'blogid' => '1',
- 'blogName' => $this->options->title,
- 'xmlrpc' => $this->options->xmlRpcUrl
- );
-
- return $struct;
- }
-
- /**
- * ??????
- *
- * @access public
- * @param integer $blogId
- * @param string $userName
- * @param string $password
- * @return array
- */
- public function wpGetTags($blogId, $userName, $password)
- {
- /** ????*/
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
-
- $struct = array();
- $tags = $this->singletonWidget('Widget_Metas_Tag_Cloud');
-
- while ($tags->next()) {
- $struct[] = array(
- 'tag_id' => $tags->mid,
- 'name' => $tags->name,
- 'count' => $tags->count,
- 'slug' => $tags->slug,
- 'html_url' => $tags->permalink,
- 'rss_url' => $tags->feedUrl
- );
- }
-
- return $struct;
- }
-
- /**
- * ????
- *
- * @access public
- * @param integer $blogId
- * @param string $userName
- * @param string $password
- * @param integer $categoryId
- * @return array
- */
- public function wpDeleteCategory($blogId, $userName, $password, $categoryId)
- {
- /** ????*/
- if (!$this->checkAccess($userName, $password, 'editor')) {
- return $this->error;
- }
-
- try {
- $this->singletonWidget('Widget_Metas_Category_Edit', NULL, 'do=delete&mid=' . intval($categoryId), false);
- return true;
- } catch (Typecho_Exception $e) {
- return false;
- }
- }
-
- /**
- * ??????
- *
- * @access public
- * @param integer $blogId
- * @param string $userName
- * @param string $password
- * @param integer $postId
- * @return array
- */
- public function wpGetCommentCount($blogId, $userName, $password, $postId)
- {
- /** ????*/
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
-
- $stat = $this->singletonWidget('Widget_Stat', NULL, 'cid=' . intval($postId), false);
-
- return array(
- 'approved' => $stat->currentPublishedCommentsNum,
- 'awaiting_moderation' => $stat->currentWaitingCommentsNum,
- 'spam' => $stat->currentSpamCommentsNum,
- 'total_comments' => $stat->currentCommentsNum
- );
- }
-
- /**
- * ????????
- *
- * @access public
- * @param integer $blogId
- * @param string $userName
- * @param string $password
- * @return array
- */
- public function wpGetPostStatusList($blogId, $userName, $password)
- {
- /** ????*/
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
-
- return array(
- 'draft' => _t('??'),
- 'pending' => _t('???'),
- 'publish' => _t('???')
- );
- }
-
- /**
- * ????????
- *
- * @access public
- * @param integer $blogId
- * @param string $userName
- * @param string $password
- * @return array
- */
- public function wpGetPageStatusList($blogId, $userName, $password)
- {
- /** ????*/
- if (!$this->checkAccess($userName, $password, 'editor')) {
- return $this->error;
- }
-
- return array(
- 'draft' => _t('??'),
- 'publish' => _t('???')
- );
- }
-
- /**
- * ????????
- *
- * @access public
- * @param integer $blogId
- * @param string $userName
- * @param string $password
- * @return array
- */
- public function wpGetCommentStatusList($blogId, $userName, $password)
- {
- /** ????*/
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
-
- return array(
- 'hold' => _t('???'),
- 'approve' => _t('??'),
- 'spam' => _t('??')
- );
- }
-
- /**
- * ??????
- *
- * @access public
- * @param integer $blogId
- * @param string $userName
- * @param string $password
- * @return array
- */
- public function wpGetPageTemplates($blogId, $userName, $password)
- {
- /** ????*/
- if (!$this->checkAccess($userName, $password, 'editor')) {
- return $this->error;
- }
-
- $templates = array_flip($this->getTemplates());
- $templates['Default'] = '';
-
- return $templates;
- }
-
- /**
- * ??????
- *
- * @access public
- * @param integer $blogId
- * @param string $userName
- * @param string $password
- * @param array $options
- * @return array
- */
- public function wpGetOptions($blogId, $userName, $password, $options = array())
- {
- /** ????*/
- if (!$this->checkAccess($userName, $password, 'administrator')) {
- return $this->error;
- }
-
- $struct = array();
- if (empty($options)) {
- $options = array_keys($this->_wpOptions);
- }
-
- foreach ($options as $option) {
- if (isset($this->_wpOptions[$option])) {
- $struct[$option] = $this->_wpOptions[$option];
- if (isset($struct[$option]['option'])) {
- $struct[$option]['value'] = $this->options->{$struct[$option]['option']};
- unset($struct[$option]['option']);
- }
- }
- }
-
- return $struct;
- }
-
- /**
- * ??????
- *
- * @access public
- * @param integer $blogId
- * @param string $userName
- * @param string $password
- * @param array $options
- * @return array
- */
- public function wpSetOptions($blogId, $userName, $password, $options = array())
- {
- /** ????*/
- if (!$this->checkAccess($userName, $password, 'administrator')) {
- return $this->error;
- }
-
- $struct = array();
- foreach ($options as $option => $value) {
- if (isset($this->_wpOptions[$option])) {
- $struct[$option] = $this->_wpOptions[$option];
- if (isset($struct[$option]['option'])) {
- $struct[$option]['value'] = $this->options->{$struct[$option]['option']};
- unset($struct[$option]['option']);
- }
-
- if (!$this->_wpOptions[$option]['readonly'] && isset($this->_wpOptions[$option]['option'])) {
- if ($db->query($db->update('table.options')
- ->rows(array('value' => $value))
- ->where('name = ?', $this->_wpOptions[$option]['option'])) > 0) {
- $struct[$option]['value'] = $value;
- }
- }
- }
- }
-
- return $struct;
- }
-
- /**
- * ????
- *
- * @access public
- * @param integer $blogId
- * @param string $userName
- * @param string $password
- * @param integer $commentId
- * @return array
- */
- public function wpGetComment($blogId, $userName, $password, $commentId)
- {
- /** ????*/
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
-
- $comment = $this->singletonWidget('Widget_Comments_Edit', NULL, 'do=get&coid=' . intval($commentId), false);
-
- if (!$comment->have()) {
- return new IXR_Error(404, _t('?????'));
- }
-
- if (!$comment->commentIsWriteable()) {
- return new IXR_Error(403, _t('?????????'));
- }
-
- return array(
- 'date_created_gmt' => new IXR_Date($this->options->timezone + $comment->created),
- 'user_id' => $comment->authorId,
- 'comment_id' => $comment->coid,
- 'parent' => $comment->parent,
- 'status' => $this->typechoToWordpressStatus($comment->status, 'comment'),
- 'content' => $comment->text,
- 'link' => $comment->permalink,
- 'post_id' => $comment->cid,
- 'post_title' => $comment->title,
- 'author' => $comment->author,
- 'author_url' => $comment->url,
- 'author_email' => $comment->mail,
- 'author_ip' => $comment->ip,
- 'type' => $comment->type
- );
- }
-
- /**
- * ??????
- *
- * @access public
- * @param integer $blogId
- * @param string $userName
- * @param string $password
- * @param array $struct
- * @return array
- */
- public function wpGetComments($blogId, $userName, $password, $struct)
- {
- /** ????*/
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
-
- $input = array();
- if (!empty($struct['status'])) {
- $input['status'] = 'hold' == $input['status'] ? $input['status'] :
- $this->wordpressToTypechoStatus($struct['status']);
- } else {
- $input['__typecho_all_comments'] = 'on';
- }
-
- if (!empty($struct['post_id'])) {
- $input['cid'] = $struct['post_id'];
- }
-
- $pageSize = 10;
- if (!empty($struct['number'])) {
- $pageSize = abs(intval($struct['number']));
- }
-
- if (!empty($struct['offset'])) {
- $offset = abs(intval($struct['offset']));
- $input['page'] = ceil($offset / $pageSize);
- }
-
- $comments = $this->singletonWidget('Widget_Comments_Admin', 'pageSize=' . $pageSize, $input, false);
- $commentsStruct = array();
-
- while ($comments->next()) {
- $commentsStruct[] = array(
- 'date_created_gmt' => new IXR_Date($this->options->timezone + $comments->created),
- 'user_id' => $comments->authorId,
- 'comment_id' => $comments->coid,
- 'parent' => $comments->parent,
- 'status' => $this->typechoToWordpressStatus($comments->status, 'comment'),
- 'content' => $comments->text,
- 'link' => $comments->permalink,
- 'post_id' => $comments->cid,
- 'post_title' => $comments->title,
- 'author' => $comments->author,
- 'author_url' => $comments->url,
- 'author_email' => $comments->mail,
- 'author_ip' => $comments->ip,
- 'type' => $comments->type
- );
- }
-
- return $commentsStruct;
- }
-
- /**
- * ????
- *
- * @access public
- * @param integer $blogId
- * @param string $userName
- * @param string $password
- * @param integer $commentId
- * @return boolean
- */
- public function wpDeleteComment($blogId, $userName, $password, $commentId)
- {
- /** ????*/
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
-
- $commentId = abs(intval($commentId));
- return intval($this->singletonWidget('Widget_Abstract_Comments')->delete(
- $this->db->sql()->where('coid = ?', $commentId))) > 0;
- }
-
- /**
- * ????
- *
- * @access public
- * @param integer $blogId
- * @param string $userName
- * @param string $password
- * @param integer $commentId
- * @param array $struct
- * @return boolean
- */
- public function wpEditComment($blogId, $userName, $password, $commentId, $struct)
- {
- /** ????*/
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
-
- $commentId = abs(intval($commentId));
- $commentWidget = $this->singletonWidget('Widget_Abstract_Comments');
- $where = $this->db->sql()->where('coid = ?', $commentId);
-
- if (!$commentWidget->commentIsWriteable($where)) {
- return new IXR_Error(403, _t('???????'));
- }
-
- $input = array();
-
- if (isset($struct['date_created_gmt'])) {
- $input['created'] = $struct['date_created_gmt']->getTimestamp() - $this->options->timezone + $this->options->serverTimezone;
- }
-
- if (isset($struct['status'])) {
- $input['status'] = $this->wordpressToTypechoStatus($struct['status'], 'comment');
- }
-
- if (isset($struct['content'])) {
- $input['text'] = $struct['content'];
- }
-
- if (isset($struct['author'])) {
- $input['author'] = $struct['author'];
- }
-
- if (isset($struct['author_url'])) {
- $input['url'] = $struct['author_url'];
- }
-
- if (isset($struct['author_email'])) {
- $input['mail'] = $struct['author_email'];
- }
-
- $result = $commentWidget->update((array) $input, $where);
-
- if (!$result) {
- return new IXR_Error(404, _t('?????'));
- }
-
- return true;
- }
-
- /**
- * ????
- *
- * @access public
- * @param integer $blogId
- * @param string $userName
- * @param string $password
- * @param mixed $post
- * @param array $struct
- * @return boolean
- */
- public function wpNewComment($blogId, $userName, $password, $path, $struct)
- {
- /** ????*/
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
-
- if (is_numeric($path)) {
- $post = $this->singletonWidget('Widget_Archive', 'type=single', 'cid=' . $path, false);
- } else {
- /** ??????????*/
- $pathInfo = Typecho_Common::url(substr($path, strlen($this->options->index)), '/');
- $post = Typecho_Router::match($pathInfo);
- }
-
- /** ??????cid??slug*/
- if (!isset($post) || !($post instanceof Widget_Archive) || !$post->have() || !$post->is('single')) {
- return new IXR_Error(404, _t('?????????'));
- }
-
- $input = array();
- $input['permalink'] = $post->pathinfo;
- $input['type'] = 'comment';
-
- if (isset($struct['comment_author'])) {
- $input['author'] = $struct['author'];
- }
-
- if (isset($struct['comment_author_email'])) {
- $input['mail'] = $struct['author_email'];
- }
-
- if (isset($struct['comment_author_url'])) {
- $input['url'] = $struct['author_url'];
- }
-
- if (isset($struct['comment_parent'])) {
- $input['parent'] = $struct['comment_parent'];
- }
-
- if (isset($struct['content'])) {
- $input['text'] = $struct['content'];
- }
-
- try {
- $this->singletonWidget('Widget_Feedback', NULL, $input, false);
- } catch (Typecho_Exception $e) {
- return new IXR_Error(500, $e->getMessage());
- }
-
- return true;
- }
- /**about MetaWeblog API, you can see http://www.xmlrpc.com/metaWeblogApi*/
- /**
- * MetaWeblog API
- *
- * @param int $blogId
- * @param string $userName
- * @param string $password
- * @param struct $content
- * @param bool $publish
- * @access public
- * @return int
- */
- public function mwNewPost($blogId, $userName, $password, $content, $publish)
- {
- /** ????*/
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
- /** ??content?? */
- $input = array();
- $type = isset($content['post_type']) && 'page' == $content['post_type'] ? 'page' : 'post';
-
- $input['title'] = trim($content['title']) == NULL ? _t('?????') : $content['title'];
- if (isset($content['slug'])) {
- $input['slug'] = $content['slug'];
- } else if (isset($content['wp_slug'])) {
- //fix issue 338, wlw?????
- $input['slug'] = $content['wp_slug'];
- }
- $input['text'] = !empty($content['mt_text_more']) ? $content['description']
- . "\n<!--more-->\n" . $content['mt_text_more'] : $content['description'];
- $input['text'] = $this->pluginHandle()->textFilter($input['text'], $this);
-
- $input['password'] = isset($content["wp_password"]) ? $content["wp_password"] : NULL;
- $input['order'] = isset($content["wp_page_order"]) ? $content["wp_page_order"] : NULL;
- $input['tags'] = isset($content['mt_keywords']) ? $content['mt_keywords'] : NULL;
- $input['category'] = array();
- if (isset($content['postId'])) {
- $input['cid'] = $content['postId'];
- }
-
- if ('page' == $type && isset($content['wp_page_template'])) {
- $input['template'] = $content['wp_page_template'];
- }
- if (isset($content['dateCreated'])) {
- /** ?????????????? */
- $input['created'] = $content['dateCreated']->getTimestamp() - $this->options->timezone + $this->options->serverTimezone;
- }
- if (!empty($content['categories']) && is_array($content['categories'])) {
- foreach ($content['categories'] as $category) {
- if (!$this->db->fetchRow($this->db->select('mid')
- ->from('table.metas')->where('type = ? AND name = ?', 'category', $category))) {
- $result = $this->wpNewCategory($blogId, $userName, $password, array('name' => $category));
- if (true !== $result) {
- return $result;
- }
- }
- $input['category'][] = $this->db->fetchObject($this->db->select('mid')
- ->from('table.metas')->where('type = ? AND name = ?', 'category', $category)
- ->limit(1))->mid;
- }
- }
- $input['allowComment'] = (isset($content['mt_allow_comments']) && (1 == $content['mt_allow_comments']
- || 'open' == $content['mt_allow_comments'])) ? 1 : ((isset($content['mt_allow_comments']) && (0 == $content['mt_allow_comments']
- || 'closed' == $content['mt_allow_comments'])) ? 0 : $this->options->defaultAllowComment);
- $input['allowPing'] = (isset($content['mt_allow_pings']) && (1 == $content['mt_allow_pings']
- || 'open' == $content['mt_allow_pings'])) ? 1 : ((isset($content['mt_allow_pings']) && (0 == $content['mt_allow_pings']
- || 'closed' == $content['mt_allow_pings'])) ? 0 : $this->options->defaultAllowPing);
- $input['allowFeed'] = $this->options->defaultAllowFeed;
- $input['do'] = $publish ? 'publish' : 'save';
-
- /** ???? */
- if (isset($content["{$type}_status"])) {
- $status = $this->wordpressToTypechoStatus($content["{$type}_status"], $type);
-
- if ('publish' == $status || 'waiting' == $status || 'private' == $status) {
- $input['do'] = 'publish';
-
- if ('private' == $status) {
- $input['private'] = 1;
- }
- } else {
- $input['do'] = 'save';
- }
- }
- /** ?????????? */
- $unattached = $this->db->fetchAll($this->select()->where('table.contents.type = ? AND
- (table.contents.parent = 0 OR table.contents.parent IS NULL)', 'attachment'), array($this, 'filter'));
- if (!empty($unattached)) {
- foreach ($unattached as $attach) {
- if (false !== strpos($input['text'], $attach['attachment']->url)) {
- if (!isset($input['attachment'])) {
- $input['attachment'] = array();
- }
- $input['attachment'][] = $attach['cid'];
- }
- }
- }
- /** ?????? */
- try {
- /** ?? */
- if ('page' == $type) {
- $this->singletonWidget('Widget_Contents_Page_Edit', NULL, $input, false)->action();
- } else {
- $this->singletonWidget('Widget_Contents_Post_Edit', NULL, $input, false)->action();
- }
- return $this->singletonWidget('Widget_Notice')->getHighlightId();
- } catch (Typecho_Widget_Exception $e) {
- return new IXR_Error($e->getCode(), $e->getMessage());
- }
- }
- /**
- * ??post
- *
- * @param int $postId
- * @param string $userName
- * @param string $password
- * @param struct $content
- * @param bool $publish
- * @access public
- * @return int
- */
- public function mwEditPost($postId, $userName, $password, $content, $publish = true)
- {
- $content['postId'] = $postId;
- return $this->mwNewPost(1, $userName, $password, $content, $publish);
- }
- /**
- * ????id?post
- *
- * @param int $postId
- * @param string $userName
- * @param string $password
- * @access public
- * @return void
- */
- public function mwGetPost($postId, $userName, $password)
- {
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
- try {
- $post = $this->singletonWidget('Widget_Contents_Post_Edit', NULL, "cid={$postId}");
- } catch (Typecho_Widget_Exception $e) {
- return new IXR_Error($e->getCode(), $e->getMessage());
- }
- /** ??????????????description?text_more*/
- list($excerpt, $more) = $this->getPostExtended($post);
- /** ??????name*/
- $categories = Typecho_Common::arrayFlatten($post->categories, 'name');
- $tags = Typecho_Common::arrayFlatten($post->tags, 'name');
- $postStruct = array(
- 'dateCreated' => new IXR_Date($this->options->timezone + $post->created),
- 'userid' => $post->authorId,
- 'postid' => $post->cid,
- 'description' => $excerpt,
- 'title' => $post->title,
- 'link' => $post->permalink,
- 'permalink' => $post->permalink,
- 'categories' => $categories,
- 'mt_excerpt' => $post->description,
- 'mt_text_more' => $more,
- 'mt_allow_comments' => intval($post->allowComment),
- 'mt_allow_pings' => intval($post->allowPing),
- 'mt_keywords' => implode(', ', $tags),
- 'wp_slug' => $post->slug,
- 'wp_password' => $post->password,
- 'wp_author' => $post->author->name,
- 'wp_author_id' => $post->authorId,
- 'wp_author_display_name' => $post->author->screenName,
- 'date_created_gmt' => new IXR_Date($post->created),
- 'post_status' => $this->typechoToWordpressStatus($post->status, 'post'),
- 'custom_fields' => array(),
- 'sticky' => 0
- );
-
- return $postStruct;
- }
- /**
- * ???$postsNum?post
- *
- * @param int $blogId
- * @param string $userName
- * @param string $password
- * @param int $postsNum
- * @access public
- * @return postStructs
- */
- public function mwGetRecentPosts($blogId, $userName, $password, $postsNum)
- {
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
- $posts = $this->singletonWidget('Widget_Contents_Post_Admin', "pageSize={$postsNum}", 'status=all');
- $postStructs = array();
- /** ????post???????????? */
- while ($posts->next()) {
- /** ??????????????description?text_more*/
- list($excerpt, $more) = $this->getPostExtended($posts);
- /** ??????name*/
- /** ???flatten???? */
- $categories = Typecho_Common::arrayFlatten($posts->categories, 'name');
- $tags = Typecho_Common::arrayFlatten($posts->tags, 'name');
- $postStructs[] = array(
- 'dateCreated' => new IXR_Date($this->options->timezone + $posts->created),
- 'userid' => $posts->authorId,
- 'postid' => $posts->cid,
- 'description' => $excerpt,
- 'title' => $posts->title,
- 'link' => $posts->permalink,
- 'permalink' => $posts->permalink,
- 'categories' => $categories,
- 'mt_excerpt' => $posts->description,
- 'mt_text_more' => $more,
- 'mt_allow_comments' => intval($posts->allowComment),
- 'mt_allow_pings' => intval($posts->allowPing),
- 'mt_keywords' => implode(', ', $tags),
- 'wp_slug' => $posts->slug,
- 'wp_password' => $posts->password,
- 'wp_author' => $posts->author->name,
- 'wp_author_id' => $posts->authorId,
- 'wp_author_display_name' => $posts->author->screenName,
- 'date_created_gmt' => new IXR_Date($posts->created),
- 'post_status' => $this->typechoToWordpressStatus($posts->status, 'post'),
- 'custom_fields' => array(),
- 'sticky' => 0
- );
- }
- return $postStructs;
- }
- /**
- * ???????
- *
- * @param int $blogId
- * @param string $userName
- * @param string $password
- * @access public
- * @return categoryStructs
- */
- public function mwGetCategories($blogId, $userName, $password)
- {
- if (!$this->checkAccess($userName, $password)) {
- return ($this->error);
- }
- $categories = $this->singletonWidget('Widget_Metas_Category_List');
- /** ???category??*/
- $categoryStructs = array();
- while ($categories->next()) {
- $categoryStructs[] = array(
- 'categoryId' => $categories->mid,
- 'parentId' => '0',
- 'categoryName' => $categories->name,
- 'categoryDescription' => $categories->description,
- 'description' => $categories->name,
- 'htmlUrl' => $categories->permalink,
- 'rssUrl' => $categories->feedUrl,
- );
- }
- return $categoryStructs;
- }
- /**
- * mwNewMediaObject
- *
- * @param int $blogId
- * @param string $userName
- * @param string $password
- * @param mixed $data
- * @access public
- * @return void
- */
- public function mwNewMediaObject($blogId, $userName, $password, $data)
- {
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
- $result = Widget_Upload::uploadHandle($data);
- if (false === $result) {
- return IXR_Error(500, _t('????'));
- } else {
- $insertId = $this->insert(array(
- 'title' => $result['name'],
- 'slug' => $result['name'],
- 'type' => 'attachment',
- 'status' => 'publish',
- 'text' => serialize($result),
- 'allowComment' => 1,
- 'allowPing' => 0,
- 'allowFeed' => 1
- ));
- $this->db->fetchRow($this->select()->where('table.contents.cid = ?', $insertId)
- ->where('table.contents.type = ?', 'attachment'), array($this, 'push'));
- /** ?????? */
- $this->pluginHandle()->upload($this);
- return array(
- 'file' => $this->attachment->name,
- 'url' => $this->attachment->url
- );
- }
- }
- /**
- * ?? $postNum?post title
- *
- * @param int $blogId
- * @param string $userName
- * @param string $password
- * @param int $postNum
- * @access public
- * @return postTitleStructs
- */
- public function mtGetRecentPostTitles($blogId, $userName, $password, $postsNum)
- {
- if (!$this->checkAccess($userName, $password)) {
- return ($this->error);
- }
- /** ????*/
- $posts = $this->singletonWidget('Widget_Contents_Post_Admin', "pageSize=$postsNum", 'status=all');
- /**???*/
- $postTitleStructs = array();
- while ($posts->next()) {
- $postTitleStructs[] = array(
- 'dateCreated' => new IXR_Date($this->options->timezone + $posts->created),
- 'userid' => $posts->authorId,
- 'postid' => $posts->cid,
- 'title' => $posts->title,
- 'date_created_gmt' => new IXR_Date($this->options->timezone + $posts->created)
- );
- }
- return $postTitleStructs;
- }
- /**
- * ??????
- *
- * @param int $blogId
- * @param string $userName
- * @param string $password
- * @access public
- * @return categories
- */
- public function mtGetCategoryList($blogId, $userName, $password)
- {
- if (!$this->checkAccess($userName, $password)) {
- return ($this->error);
- }
- $categories = $this->singletonWidget('Widget_Metas_Category_List');
- /** ???categorise??*/
- $categoryStructs = array();
- while ($categories->next()) {
- $categoryStructs[] = array(
- 'categoryId' => $categories->mid,
- 'categoryName' => $categories->name,
- );
- }
- return $categoryStructs;
- }
- /**
- * ????post???
- *
- * @param int $postId
- * @param string $userName
- * @param string $password
- * @access public
- * @return void
- */
- public function mtGetPostCategories($postId, $userName, $password)
- {
- if (!$this->checkAccess($userName, $password)) {
- return $this->error;
- }
- try {
- $post = $this->singletonWidget('Widget_Contents_Post_Edit', NULL, "cid={$postId}");
- } catch (Typecho_Widget_Exception $e) {
- return new IXR_Error($e->getCode(), $e->getMessage());
- }
- /** ???categories*/
- $categories = array();
- foreach ($post->categories as $category) {
- $categories[] = array(
- 'categoryName' => $category['name'],
- 'categoryId' => $category['mid'],
- 'isPrimary' => true
- );
- }
- return $categories;
- }
- /**
- * ??post???
- *
- * @param int $postId
- * @param string $userName
- * @param string $password
- * @param string $categories
- * @access public
- * @return bool
- */
- public function mtSetPostCategories($postId, $userName, $password, $categories)
- {
- if (!$this->checkAccess($userName, $password, 'editor')) {
- return $this->error;
- }
- try {
- $post = $this->singletonWidget('Widget_Contents_Post_Edit', NULL, "cid={$postId}");
- } catch (Typecho_Widget_Exception $e) {
- return new IXR_Error($e->getCode(), $e->getMessage());
- }
- $post->setCategories($postId, Typecho_Common::arrayFlatten($categories, 'categoryId'),
- 'publish' == $post->status);
- return true;
- }
- /**
- * ??(??)??
- *
- * @param int $postId
- * @param string…