XmlRpc.php 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  1. <?php
  2. namespace Widget;
  3. use IXR\Date;
  4. use IXR\Error;
  5. use IXR\Exception;
  6. use IXR\Hook;
  7. use IXR\Pingback;
  8. use IXR\Server;
  9. use ReflectionMethod;
  10. use Typecho\Common;
  11. use Typecho\Router;
  12. use Typecho\Widget;
  13. use Typecho\Widget\Exception as WidgetException;
  14. use Widget\Base\Comments;
  15. use Widget\Base\Contents;
  16. use Widget\Base\Metas;
  17. use Widget\Contents\Page\Admin as PageAdmin;
  18. use Widget\Contents\Post\Admin as PostAdmin;
  19. use Widget\Contents\Attachment\Admin as AttachmentAdmin;
  20. use Widget\Contents\Post\Edit as PostEdit;
  21. use Widget\Contents\Page\Edit as PageEdit;
  22. use Widget\Contents\Attachment\Edit as AttachmentEdit;
  23. use Widget\Metas\Category\Edit as CategoryEdit;
  24. use Widget\Metas\Category\Rows as CategoryRows;
  25. use Widget\Metas\Tag\Cloud;
  26. use Widget\Comments\Edit as CommentsEdit;
  27. use Widget\Comments\Admin as CommentsAdmin;
  28. if (!defined('__TYPECHO_ROOT_DIR__')) {
  29. exit;
  30. }
  31. /**
  32. * XmlRpc接口
  33. *
  34. * @author blankyao
  35. * @category typecho
  36. * @package Widget
  37. * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
  38. * @license GNU General Public License 2.0
  39. */
  40. class XmlRpc extends Contents implements ActionInterface, Hook
  41. {
  42. /**
  43. * 当前错误
  44. *
  45. * @var Error
  46. */
  47. private $error;
  48. /**
  49. * wordpress风格的系统选项
  50. *
  51. * @access private
  52. * @var array
  53. */
  54. private $wpOptions;
  55. /**
  56. * 已经使用过的组件列表
  57. *
  58. * @access private
  59. * @var array
  60. */
  61. private $usedWidgetNameList = [];
  62. /**
  63. * 如果这里没有重载, 每次都会被默认执行
  64. *
  65. * @param bool $run 是否执行
  66. */
  67. public function execute(bool $run = false)
  68. {
  69. if ($run) {
  70. parent::execute();
  71. }
  72. // 临时保护模块
  73. $this->security->enable(false);
  74. $this->wpOptions = [
  75. // Read only options
  76. 'software_name' => [
  77. 'desc' => _t('软件名称'),
  78. 'readonly' => true,
  79. 'value' => $this->options->software
  80. ],
  81. 'software_version' => [
  82. 'desc' => _t('软件版本'),
  83. 'readonly' => true,
  84. 'value' => $this->options->version
  85. ],
  86. 'blog_url' => [
  87. 'desc' => _t('博客地址'),
  88. 'readonly' => true,
  89. 'option' => 'siteUrl'
  90. ],
  91. 'home_url' => [
  92. 'desc' => _t('博客首页地址'),
  93. 'readonly' => true,
  94. 'option' => 'siteUrl'
  95. ],
  96. 'login_url' => [
  97. 'desc' => _t('登录地址'),
  98. 'readonly' => true,
  99. 'value' => $this->options->siteUrl . 'admin/login.php'
  100. ],
  101. 'admin_url' => [
  102. 'desc' => _t('管理区域的地址'),
  103. 'readonly' => true,
  104. 'value' => $this->options->siteUrl . 'admin/'
  105. ],
  106. 'post_thumbnail' => [
  107. 'desc' => _t('文章缩略图'),
  108. 'readonly' => true,
  109. 'value' => false
  110. ],
  111. // Updatable options
  112. 'time_zone' => [
  113. 'desc' => _t('时区'),
  114. 'readonly' => false,
  115. 'option' => 'timezone'
  116. ],
  117. 'blog_title' => [
  118. 'desc' => _t('博客标题'),
  119. 'readonly' => false,
  120. 'option' => 'title'
  121. ],
  122. 'blog_tagline' => [
  123. 'desc' => _t('博客关键字'),
  124. 'readonly' => false,
  125. 'option' => 'description'
  126. ],
  127. 'date_format' => [
  128. 'desc' => _t('日期格式'),
  129. 'readonly' => false,
  130. 'option' => 'postDateFormat'
  131. ],
  132. 'time_format' => [
  133. 'desc' => _t('时间格式'),
  134. 'readonly' => false,
  135. 'option' => 'postDateFormat'
  136. ],
  137. 'users_can_register' => [
  138. 'desc' => _t('是否允许注册'),
  139. 'readonly' => false,
  140. 'option' => 'allowRegister'
  141. ]
  142. ];
  143. }
  144. /**
  145. * 获取pageId指定的page
  146. * about wp xmlrpc api, you can see http://codex.wordpress.org/XML-RPC
  147. *
  148. * @param int $blogId
  149. * @param int $pageId
  150. * @param string $userName
  151. * @param string $password
  152. * @return array
  153. */
  154. public function wpGetPage(int $blogId, int $pageId, string $userName, string $password): array
  155. {
  156. /** 获取页面 */
  157. $page = PageEdit::alloc(null, ['cid' => $pageId], false);
  158. /** 对文章内容做截取处理,以获得description和text_more*/
  159. [$excerpt, $more] = $this->getPostExtended($page);
  160. return [
  161. 'dateCreated' => new Date($this->options->timezone + $page->created),
  162. 'userid' => $page->authorId,
  163. 'page_id' => $page->cid,
  164. 'page_status' => $this->typechoToWordpressStatus($page->status, 'page'),
  165. 'description' => $excerpt,
  166. 'title' => $page->title,
  167. 'link' => $page->permalink,
  168. 'permaLink' => $page->permalink,
  169. 'categories' => $page->categories,
  170. 'excerpt' => $page->description,
  171. 'text_more' => $more,
  172. 'mt_allow_comments' => intval($page->allowComment),
  173. 'mt_allow_pings' => intval($page->allowPing),
  174. 'wp_slug' => $page->slug,
  175. 'wp_password' => $page->password,
  176. 'wp_author' => $page->author->name,
  177. 'wp_page_parent_id' => '0',
  178. 'wp_page_parent_title' => '',
  179. 'wp_page_order' => $page->order, //meta是描述字段, 在page时表示顺序
  180. 'wp_author_id' => $page->authorId,
  181. 'wp_author_display_name' => $page->author->screenName,
  182. 'date_created_gmt' => new Date($page->created),
  183. 'custom_fields' => [],
  184. 'wp_page_template' => $page->template
  185. ];
  186. }
  187. /**
  188. * @param string $methodName
  189. * @param ReflectionMethod $reflectionMethod
  190. * @param array $parameters
  191. * @throws Exception
  192. */
  193. public function beforeRpcCall(string $methodName, ReflectionMethod $reflectionMethod, array $parameters)
  194. {
  195. $valid = 2;
  196. $auth = [];
  197. $accesses = [
  198. 'wp.newPage' => 'editor',
  199. 'wp.deletePage' => 'editor',
  200. 'wp.getPageList' => 'editor',
  201. 'wp.getAuthors' => 'editor',
  202. 'wp.deleteCategory' => 'editor',
  203. 'wp.getPageStatusList' => 'editor',
  204. 'wp.getPageTemplates' => 'editor',
  205. 'wp.getOptions' => 'administrator',
  206. 'wp.setOptions' => 'administrator',
  207. 'mt.setPostCategories' => 'editor',
  208. ];
  209. foreach ($reflectionMethod->getParameters() as $key => $parameter) {
  210. $name = $parameter->getName();
  211. if ($name == 'userName' || $name == 'password') {
  212. $auth[$name] = $parameters[$key];
  213. $valid--;
  214. }
  215. }
  216. if ($valid == 0) {
  217. if ($this->user->login($auth['userName'], $auth['password'], true)) {
  218. /** 验证权限 */
  219. if ($this->user->pass($accesses[$methodName] ?? 'contributor', true)) {
  220. $this->user->execute();
  221. } else {
  222. throw new Exception(_t('权限不足'), 403);
  223. }
  224. } else {
  225. throw new Exception(_t('无法登陆, 密码错误'), 403);
  226. }
  227. }
  228. }
  229. /**
  230. * @param string $methodName
  231. * @param mixed $result
  232. */
  233. public function afterRpcCall(string $methodName, &$result): void
  234. {
  235. Widget::destroy();
  236. }
  237. /**
  238. * 获取所有的page
  239. *
  240. * @param int $blogId
  241. * @param string $userName
  242. * @param string $password
  243. * @return array
  244. */
  245. public function wpGetPages(int $blogId, string $userName, string $password): array
  246. {
  247. /** 过滤type为page的contents */
  248. /** 同样需要flush一下, 需要取出所有status的页面 */
  249. $pages = PageAdmin::alloc(null, 'status=all');
  250. /** 初始化要返回的数据结构 */
  251. $pageStructs = [];
  252. while ($pages->next()) {
  253. /** 对文章内容做截取处理,以获得description和text_more*/
  254. [$excerpt, $more] = $this->getPostExtended($pages);
  255. $pageStructs[] = [
  256. 'dateCreated' => new Date($this->options->timezone + $pages->created),
  257. 'userid' => $pages->authorId,
  258. 'page_id' => intval($pages->cid),
  259. 'page_status' => $this->typechoToWordpressStatus(
  260. ($pages->hasSaved || 'page_draft' == $pages->type) ? 'draft' : $pages->status,
  261. 'page'
  262. ),
  263. 'description' => $excerpt,
  264. 'title' => $pages->title,
  265. 'link' => $pages->permalink,
  266. 'permaLink' => $pages->permalink,
  267. 'categories' => $pages->categories,
  268. 'excerpt' => $pages->description,
  269. 'text_more' => $more,
  270. 'mt_allow_comments' => intval($pages->allowComment),
  271. 'mt_allow_pings' => intval($pages->allowPing),
  272. 'wp_slug' => $pages->slug,
  273. 'wp_password' => $pages->password,
  274. 'wp_author' => $pages->author->name,
  275. 'wp_page_parent_id' => 0,
  276. 'wp_page_parent_title' => '',
  277. 'wp_page_order' => intval($pages->order), //meta是描述字段, 在page时表示顺序
  278. 'wp_author_id' => $pages->authorId,
  279. 'wp_author_display_name' => $pages->author->screenName,
  280. 'date_created_gmt' => new Date($pages->created),
  281. 'custom_fields' => [],
  282. 'wp_page_template' => $pages->template
  283. ];
  284. }
  285. return $pageStructs;
  286. }
  287. /**
  288. * 撰写一个新page
  289. *
  290. * @param int $blogId
  291. * @param string $userName
  292. * @param string $password
  293. * @param array $content
  294. * @param bool $publish
  295. * @return int
  296. * @throws \Typecho\Db\Exception
  297. */
  298. public function wpNewPage(int $blogId, string $userName, string $password, array $content, bool $publish): int
  299. {
  300. $content['post_type'] = 'page';
  301. return $this->mwNewPost($blogId, $userName, $password, $content, $publish);
  302. }
  303. /**
  304. * MetaWeblog API
  305. * about MetaWeblog API, you can see http://www.xmlrpc.com/metaWeblogApi
  306. *
  307. * @param int $blogId
  308. * @param string $userName
  309. * @param string $password
  310. * @param array $content
  311. * @param bool $publish
  312. * @return int
  313. * @throws \Typecho\Db\Exception
  314. */
  315. public function mwNewPost(int $blogId, string $userName, string $password, array $content, bool $publish): int
  316. {
  317. /** 取得content内容 */
  318. $input = [];
  319. $type = isset($content['post_type']) && 'page' == $content['post_type'] ? 'page' : 'post';
  320. $input['title'] = trim($content['title']) == null ? _t('未命名文档') : $content['title'];
  321. if (isset($content['slug'])) {
  322. $input['slug'] = $content['slug'];
  323. } elseif (isset($content['wp_slug'])) {
  324. //fix issue 338, wlw只发送这个
  325. $input['slug'] = $content['wp_slug'];
  326. }
  327. $input['text'] = !empty($content['mt_text_more']) ? $content['description']
  328. . "\n<!--more-->\n" . $content['mt_text_more'] : $content['description'];
  329. $input['text'] = self::pluginHandle()->textFilter($input['text'], $this);
  330. $input['password'] = $content["wp_password"] ?? null;
  331. $input['order'] = $content["wp_page_order"] ?? null;
  332. $input['tags'] = $content['mt_keywords'] ?? null;
  333. $input['category'] = [];
  334. if (isset($content['postId'])) {
  335. $input['cid'] = $content['postId'];
  336. }
  337. if ('page' == $type && isset($content['wp_page_template'])) {
  338. $input['template'] = $content['wp_page_template'];
  339. }
  340. if (isset($content['dateCreated'])) {
  341. /** 解决客户端与服务器端时间偏移 */
  342. $input['created'] = $content['dateCreated']->getTimestamp()
  343. - $this->options->timezone + $this->options->serverTimezone;
  344. }
  345. if (!empty($content['categories']) && is_array($content['categories'])) {
  346. foreach ($content['categories'] as $category) {
  347. if (
  348. !$this->db->fetchRow($this->db->select('mid')
  349. ->from('table.metas')->where('type = ? AND name = ?', 'category', $category))
  350. ) {
  351. $this->wpNewCategory($blogId, $userName, $password, ['name' => $category]);
  352. }
  353. $input['category'][] = $this->db->fetchObject($this->db->select('mid')
  354. ->from('table.metas')->where('type = ? AND name = ?', 'category', $category)
  355. ->limit(1))->mid;
  356. }
  357. }
  358. $input['allowComment'] = (isset($content['mt_allow_comments']) && (1 == $content['mt_allow_comments']
  359. || 'open' == $content['mt_allow_comments']))
  360. ? 1 : ((isset($content['mt_allow_comments']) && (0 == $content['mt_allow_comments']
  361. || 'closed' == $content['mt_allow_comments']))
  362. ? 0 : $this->options->defaultAllowComment);
  363. $input['allowPing'] = (isset($content['mt_allow_pings']) && (1 == $content['mt_allow_pings']
  364. || 'open' == $content['mt_allow_pings']))
  365. ? 1 : ((isset($content['mt_allow_pings']) && (0 == $content['mt_allow_pings']
  366. || 'closed' == $content['mt_allow_pings'])) ? 0 : $this->options->defaultAllowPing);
  367. $input['allowFeed'] = $this->options->defaultAllowFeed;
  368. $input['do'] = $publish ? 'publish' : 'save';
  369. $input['markdown'] = $this->options->xmlrpcMarkdown;
  370. /** 调整状态 */
  371. if (isset($content["{$type}_status"])) {
  372. $status = $this->wordpressToTypechoStatus($content["{$type}_status"], $type);
  373. $input['visibility'] = $content["visibility"] ?? $status;
  374. if ('publish' == $status || 'waiting' == $status || 'private' == $status) {
  375. $input['do'] = 'publish';
  376. if ('private' == $status) {
  377. $input['private'] = 1;
  378. }
  379. } else {
  380. $input['do'] = 'save';
  381. }
  382. }
  383. /** 对未归档附件进行归档 */
  384. $unattached = $this->db->fetchAll($this->select()->where('table.contents.type = ? AND
  385. (table.contents.parent = 0 OR table.contents.parent IS NULL)', 'attachment'), [$this, 'filter']);
  386. if (!empty($unattached)) {
  387. foreach ($unattached as $attach) {
  388. if (false !== strpos($input['text'], $attach['attachment']->url)) {
  389. if (!isset($input['attachment'])) {
  390. $input['attachment'] = [];
  391. }
  392. $input['attachment'][] = $attach['cid'];
  393. }
  394. }
  395. }
  396. /** 调用已有组件 */
  397. if ('page' == $type) {
  398. $widget = PageEdit::alloc(null, $input, function (PageEdit $page) {
  399. $page->writePage();
  400. });
  401. } else {
  402. $widget = PostEdit::alloc(null, $input, function (PostEdit $post) {
  403. $post->writePost();
  404. });
  405. }
  406. return $widget->cid;
  407. }
  408. /**
  409. * 添加一个新的分类
  410. *
  411. * @param int $blogId
  412. * @param string $userName
  413. * @param string $password
  414. * @param array $category
  415. * @return int
  416. * @throws \Typecho\Db\Exception
  417. */
  418. public function wpNewCategory(int $blogId, string $userName, string $password, array $category): int
  419. {
  420. /** 开始接受数据 */
  421. $input['name'] = $category['name'];
  422. $input['slug'] = Common::slugName(empty($category['slug']) ? $category['name'] : $category['slug']);
  423. $input['parent'] = $category['parent_id'] ?? ($category['parent'] ?? 0);
  424. $input['description'] = $category['description'] ?? $category['name'];
  425. /** 调用已有组件 */
  426. $categoryWidget = CategoryEdit::alloc(null, $input, function (CategoryEdit $category) {
  427. $category->insertCategory();
  428. });
  429. if (!$categoryWidget->have()) {
  430. throw new Exception(_t('分类不存在'), 404);
  431. }
  432. return $categoryWidget->mid;
  433. }
  434. /**
  435. * 删除pageId指定的page
  436. *
  437. * @param int $blogId
  438. * @param string $userName
  439. * @param string $password
  440. * @param int $pageId
  441. * @return bool
  442. * @throws \Typecho\Db\Exception
  443. */
  444. public function wpDeletePage(int $blogId, string $userName, string $password, int $pageId): bool
  445. {
  446. PageEdit::alloc(null, ['cid' => $pageId], function (PageEdit $page) {
  447. $page->deletePage();
  448. });
  449. return true;
  450. }
  451. /**
  452. * 编辑pageId指定的page
  453. *
  454. * @param int $blogId
  455. * @param int $pageId
  456. * @param string $userName
  457. * @param string $password
  458. * @param array $content
  459. * @param bool $publish
  460. * @return bool
  461. */
  462. public function wpEditPage(
  463. int $blogId,
  464. int $pageId,
  465. string $userName,
  466. string $password,
  467. array $content,
  468. bool $publish
  469. ): bool {
  470. $content['post_type'] = 'page';
  471. $this->mwEditPost($pageId, $userName, $password, $content, $publish);
  472. return true;
  473. }
  474. /**
  475. * 编辑post
  476. *
  477. * @param int $postId
  478. * @param string $userName
  479. * @param string $password
  480. * @param array $content
  481. * @param bool $publish
  482. * @return int
  483. * @throws \Typecho\Db\Exception
  484. */
  485. public function mwEditPost(
  486. int $postId,
  487. string $userName,
  488. string $password,
  489. array $content,
  490. bool $publish = true
  491. ): int {
  492. $content['postId'] = $postId;
  493. return $this->mwNewPost(1, $userName, $password, $content, $publish);
  494. }
  495. /**
  496. * 编辑postId指定的post
  497. *
  498. * @param int $blogId
  499. * @param string $userName
  500. * @param string $password
  501. * @param int $postId
  502. * @param array $content
  503. * @return bool
  504. * @throws \Typecho\Db\Exception
  505. */
  506. public function wpEditPost(int $blogId, string $userName, string $password, int $postId, array $content): bool
  507. {
  508. $post = Archive::alloc('type=single', ['cid' => $postId], false);
  509. if ($post->type == 'attachment') {
  510. $attachment['title'] = $content['post_title'];
  511. $attachment['slug'] = $content['post_excerpt'];
  512. $text = unserialize($post->text);
  513. $text['description'] = $content['description'];
  514. $attachment['text'] = serialize($text);
  515. /** 更新数据 */
  516. $updateRows = $this->update($attachment, $this->db->sql()->where('cid = ?', $postId));
  517. return $updateRows > 0;
  518. }
  519. return $this->mwEditPost($postId, $userName, $password, $content) > 0;
  520. }
  521. /**
  522. * 获取page列表,没有wpGetPages获得的详细
  523. *
  524. * @param int $blogId
  525. * @param string $userName
  526. * @param string $password
  527. * @return array
  528. */
  529. public function wpGetPageList(int $blogId, string $userName, string $password): array
  530. {
  531. $pages = PageAdmin::alloc(null, 'status=all');
  532. $pageStructs = [];
  533. while ($pages->next()) {
  534. $pageStructs[] = [
  535. 'dateCreated' => new Date($this->options->timezone + $pages->created),
  536. 'date_created_gmt' => new Date($this->options->timezone + $pages->created),
  537. 'page_id' => $pages->cid,
  538. 'page_title' => $pages->title,
  539. 'page_parent_id' => '0',
  540. ];
  541. }
  542. return $pageStructs;
  543. }
  544. /**
  545. * 获得一个由blog所有作者的信息组成的数组
  546. *
  547. * @param int $blogId
  548. * @param string $userName
  549. * @param string $password
  550. * @return array
  551. * @throws \Typecho\Db\Exception
  552. */
  553. public function wpGetAuthors(int $blogId, string $userName, string $password): array
  554. {
  555. /** 构建查询*/
  556. $select = $this->db->select('table.users.uid', 'table.users.name', 'table.users.screenName')
  557. ->from('table.users');
  558. $authors = $this->db->fetchAll($select);
  559. $authorStructs = [];
  560. foreach ($authors as $author) {
  561. $authorStructs[] = [
  562. 'user_id' => $author['uid'],
  563. 'user_login' => $author['name'],
  564. 'display_name' => $author['screenName']
  565. ];
  566. }
  567. return $authorStructs;
  568. }
  569. /**
  570. * 获取由给定的string开头的链接组成的数组
  571. *
  572. * @param int $blogId
  573. * @param string $userName
  574. * @param string $password
  575. * @param string $category
  576. * @param int $maxResults
  577. * @return array
  578. * @throws \Typecho\Db\Exception
  579. */
  580. public function wpSuggestCategories(
  581. int $blogId,
  582. string $userName,
  583. string $password,
  584. string $category,
  585. int $maxResults = 0
  586. ): array {
  587. /** 构造出查询语句并且查询*/
  588. $key = Common::filterSearchQuery($category);
  589. $key = '%' . $key . '%';
  590. $select = Metas::alloc()->select()->where(
  591. 'table.metas.type = ? AND (table.metas.name LIKE ? OR slug LIKE ?)',
  592. 'category',
  593. $key,
  594. $key
  595. );
  596. if ($maxResults > 0) {
  597. $select->limit($maxResults);
  598. }
  599. /** 不要category push到contents的容器中 */
  600. $categories = $this->db->fetchAll($select);
  601. /** 初始化categorise数组*/
  602. $categoryStructs = [];
  603. foreach ($categories as $category) {
  604. $categoryStructs[] = [
  605. 'category_id' => $category['mid'],
  606. 'category_name' => $category['name'],
  607. ];
  608. }
  609. return $categoryStructs;
  610. }
  611. /**
  612. * 获取用户
  613. *
  614. * @param string $userName 用户名
  615. * @param string $password 密码
  616. * @return array
  617. */
  618. public function wpGetUsersBlogs(string $userName, string $password): array
  619. {
  620. return [
  621. [
  622. 'isAdmin' => $this->user->pass('administrator', true),
  623. 'url' => $this->options->siteUrl,
  624. 'blogid' => '1',
  625. 'blogName' => $this->options->title,
  626. 'xmlrpc' => $this->options->xmlRpcUrl
  627. ]
  628. ];
  629. }
  630. /**
  631. * 获取用户
  632. *
  633. * @param int $blogId
  634. * @param string $userName 用户名
  635. * @param string $password 密码
  636. * @return array
  637. */
  638. public function wpGetProfile(int $blogId, string $userName, string $password): array
  639. {
  640. return [
  641. 'user_id' => $this->user->uid,
  642. 'username' => $this->user->name,
  643. 'first_name' => '',
  644. 'last_name' => '',
  645. 'registered' => new Date($this->options->timezone + $this->user->created),
  646. 'bio' => '',
  647. 'email' => $this->user->mail,
  648. 'nickname' => $this->user->screenName,
  649. 'url' => $this->user->url,
  650. 'display_name' => $this->user->screenName,
  651. 'roles' => $this->user->group
  652. ];
  653. }
  654. /**
  655. * 获取标签列表
  656. *
  657. * @param integer $blogId
  658. * @param string $userName
  659. * @param string $password
  660. * @return array
  661. */
  662. public function wpGetTags(int $blogId, string $userName, string $password): array
  663. {
  664. $struct = [];
  665. $tags = Cloud::alloc();
  666. while ($tags->next()) {
  667. $struct[] = [
  668. 'tag_id' => $tags->mid,
  669. 'name' => $tags->name,
  670. 'count' => $tags->count,
  671. 'slug' => $tags->slug,
  672. 'html_url' => $tags->permalink,
  673. 'rss_url' => $tags->feedUrl
  674. ];
  675. }
  676. return $struct;
  677. }
  678. /**
  679. * 删除分类
  680. *
  681. * @param integer $blogId
  682. * @param string $userName
  683. * @param string $password
  684. * @param integer $categoryId
  685. * @return bool
  686. */
  687. public function wpDeleteCategory(int $blogId, string $userName, string $password, int $categoryId): bool
  688. {
  689. CategoryEdit::alloc(null, ['mid' => $categoryId], function (CategoryEdit $category) {
  690. $category->deleteCategory();
  691. });
  692. return true;
  693. }
  694. /**
  695. * 获取评论数目
  696. *
  697. * @param integer $blogId
  698. * @param string $userName
  699. * @param string $password
  700. * @param integer $postId
  701. * @return array
  702. */
  703. public function wpGetCommentCount(int $blogId, string $userName, string $password, int $postId): array
  704. {
  705. $stat = Stat::alloc(null, ['cid' => $postId]);
  706. return [
  707. 'approved' => $stat->currentPublishedCommentsNum,
  708. 'awaiting_moderation' => $stat->currentWaitingCommentsNum,
  709. 'spam' => $stat->currentSpamCommentsNum,
  710. 'total_comments' => $stat->currentCommentsNum
  711. ];
  712. }
  713. /**
  714. * 获取文章类型列表
  715. *
  716. * @param integer $blogId
  717. * @param string $userName
  718. * @param string $password
  719. * @return array
  720. */
  721. public function wpGetPostFormats(int $blogId, string $userName, string $password): array
  722. {
  723. return [
  724. 'standard' => _t('标准')
  725. ];
  726. }
  727. /**
  728. * 获取文章状态列表
  729. *
  730. * @param integer $blogId
  731. * @param string $userName
  732. * @param string $password
  733. * @return array
  734. */
  735. public function wpGetPostStatusList(int $blogId, string $userName, string $password): array
  736. {
  737. return [
  738. 'draft' => _t('草稿'),
  739. 'pending' => _t('待审核'),
  740. 'publish' => _t('已发布')
  741. ];
  742. }
  743. /**
  744. * 获取页面状态列表
  745. *
  746. * @param integer $blogId
  747. * @param string $userName
  748. * @param string $password
  749. * @return array
  750. */
  751. public function wpGetPageStatusList(int $blogId, string $userName, string $password): array
  752. {
  753. return [
  754. 'draft' => _t('草稿'),
  755. 'publish' => _t('已发布')
  756. ];
  757. }
  758. /**
  759. * 获取评论状态列表
  760. *
  761. * @param integer $blogId
  762. * @param string $userName
  763. * @param string $password
  764. * @return array
  765. */
  766. public function wpGetCommentStatusList(int $blogId, string $userName, string $password): array
  767. {
  768. return [
  769. 'hold' => _t('待审核'),
  770. 'approve' => _t('显示'),
  771. 'spam' => _t('垃圾')
  772. ];
  773. }
  774. /**
  775. * 获取页面模板
  776. *
  777. * @param integer $blogId
  778. * @param string $userName
  779. * @param string $password
  780. * @return array
  781. */
  782. public function wpGetPageTemplates(int $blogId, string $userName, string $password): array
  783. {
  784. $templates = array_flip($this->getTemplates());
  785. $templates['Default'] = '';
  786. return $templates;
  787. }
  788. /**
  789. * 获取系统选项
  790. *
  791. * @param integer $blogId
  792. * @param string $userName
  793. * @param string $password
  794. * @param array $options
  795. * @return array
  796. */
  797. public function wpGetOptions(int $blogId, string $userName, string $password, array $options = []): array
  798. {
  799. $struct = [];
  800. if (empty($options)) {
  801. $options = array_keys($this->wpOptions);
  802. }
  803. foreach ($options as $option) {
  804. if (isset($this->wpOptions[$option])) {
  805. $struct[$option] = $this->wpOptions[$option];
  806. if (isset($struct[$option]['option'])) {
  807. $struct[$option]['value'] = $this->options->{$struct[$option]['option']};
  808. unset($struct[$option]['option']);
  809. }
  810. }
  811. }
  812. return $struct;
  813. }
  814. /**
  815. * 设置系统选项
  816. *
  817. * @param integer $blogId
  818. * @param string $userName
  819. * @param string $password
  820. * @param array $options
  821. * @return array
  822. * @throws \Typecho\Db\Exception
  823. */
  824. public function wpSetOptions(int $blogId, string $userName, string $password, array $options = []): array
  825. {
  826. $struct = [];
  827. foreach ($options as $option => $value) {
  828. if (isset($this->wpOptions[$option])) {
  829. $struct[$option] = $this->wpOptions[$option];
  830. if (isset($struct[$option]['option'])) {
  831. $struct[$option]['value'] = $this->options->{$struct[$option]['option']};
  832. unset($struct[$option]['option']);
  833. }
  834. if (!$this->wpOptions[$option]['readonly'] && isset($this->wpOptions[$option]['option'])) {
  835. if (
  836. $this->db->query($this->db->update('table.options')
  837. ->rows(['value' => $value])
  838. ->where('name = ?', $this->wpOptions[$option]['option'])) > 0
  839. ) {
  840. $struct[$option]['value'] = $value;
  841. }
  842. }
  843. }
  844. }
  845. return $struct;
  846. }
  847. /**
  848. * 获取评论
  849. *
  850. * @param integer $blogId
  851. * @param string $userName
  852. * @param string $password
  853. * @param integer $commentId
  854. * @return array
  855. * @throws Exception
  856. */
  857. public function wpGetComment(int $blogId, string $userName, string $password, int $commentId): array
  858. {
  859. $comment = CommentsEdit::alloc(null, ['coid' => $commentId], function (CommentsEdit $comment) {
  860. $comment->getComment();
  861. });
  862. if (!$comment->have()) {
  863. throw new Exception(_t('评论不存在'), 404);
  864. }
  865. if (!$comment->commentIsWriteable()) {
  866. throw new Exception(_t('没有获取评论的权限'), 403);
  867. }
  868. return [
  869. 'date_created_gmt' => new Date($this->options->timezone + $comment->created),
  870. 'user_id' => $comment->authorId,
  871. 'comment_id' => $comment->coid,
  872. 'parent' => $comment->parent,
  873. 'status' => $this->typechoToWordpressStatus($comment->status, 'comment'),
  874. 'content' => $comment->text,
  875. 'link' => $comment->permalink,
  876. 'post_id' => $comment->cid,
  877. 'post_title' => $comment->title,
  878. 'author' => $comment->author,
  879. 'author_url' => $comment->url,
  880. 'author_email' => $comment->mail,
  881. 'author_ip' => $comment->ip,
  882. 'type' => $comment->type
  883. ];
  884. }
  885. /**
  886. * 获取评论列表
  887. *
  888. * @param integer $blogId
  889. * @param string $userName
  890. * @param string $password
  891. * @param array $struct
  892. * @return array
  893. */
  894. public function wpGetComments(int $blogId, string $userName, string $password, array $struct): array
  895. {
  896. $input = [];
  897. if (!empty($struct['status'])) {
  898. $input['status'] = $this->wordpressToTypechoStatus($struct['status'], 'comment');
  899. } else {
  900. $input['__typecho_all_comments'] = 'on';
  901. }
  902. if (!empty($struct['post_id'])) {
  903. $input['cid'] = $struct['post_id'];
  904. }
  905. $pageSize = 10;
  906. if (!empty($struct['number'])) {
  907. $pageSize = abs(intval($struct['number']));
  908. }
  909. if (!empty($struct['offset'])) {
  910. $offset = abs(intval($struct['offset']));
  911. $input['page'] = ceil($offset / $pageSize);
  912. }
  913. $comments = CommentsAdmin::alloc('pageSize=' . $pageSize, $input, false);
  914. $commentsStruct = [];
  915. while ($comments->next()) {
  916. $commentsStruct[] = [
  917. 'date_created_gmt' => new Date($this->options->timezone + $comments->created),
  918. 'user_id' => $comments->authorId,
  919. 'comment_id' => $comments->coid,
  920. 'parent' => $comments->parent,
  921. 'status' => $this->typechoToWordpressStatus($comments->status, 'comment'),
  922. 'content' => $comments->text,
  923. 'link' => $comments->permalink,
  924. 'post_id' => $comments->cid,
  925. 'post_title' => $comments->title,
  926. 'author' => $comments->author,
  927. 'author_url' => $comments->url,
  928. 'author_email' => $comments->mail,
  929. 'author_ip' => $comments->ip,
  930. 'type' => $comments->type
  931. ];
  932. }
  933. return $commentsStruct;
  934. }
  935. /**
  936. * 获取评论
  937. *
  938. * @param integer $blogId
  939. * @param string $userName
  940. * @param string $password
  941. * @param integer $commentId
  942. * @return boolean
  943. * @throws \Typecho\Db\Exception
  944. */
  945. public function wpDeleteComment(int $blogId, string $userName, string $password, int $commentId): bool
  946. {
  947. CommentsEdit::alloc(null, ['coid' => $commentId], function (CommentsEdit $comment) {
  948. $comment->deleteComment();
  949. });
  950. return true;
  951. }
  952. /**
  953. * 编辑评论
  954. *
  955. * @param integer $blogId
  956. * @param string $userName
  957. * @param string $password
  958. * @param integer $commentId
  959. * @param array $struct
  960. * @return boolean
  961. * @throws \Typecho\Db\Exception
  962. */
  963. public function wpEditComment(int $blogId, string $userName, string $password, int $commentId, array $struct): bool
  964. {
  965. $input = [];
  966. if (isset($struct['date_created_gmt']) && $struct['date_created_gmt'] instanceof Date) {
  967. $input['created'] = $struct['date_created_gmt']->getTimestamp()
  968. - $this->options->timezone + $this->options->serverTimezone;
  969. }
  970. if (isset($struct['status'])) {
  971. $input['status'] = $this->wordpressToTypechoStatus($struct['status'], 'comment');
  972. }
  973. if (isset($struct['content'])) {
  974. $input['text'] = $struct['content'];
  975. }
  976. if (isset($struct['author'])) {
  977. $input['author'] = $struct['author'];
  978. }
  979. if (isset($struct['author_url'])) {
  980. $input['url'] = $struct['author_url'];
  981. }
  982. if (isset($struct['author_email'])) {
  983. $input['mail'] = $struct['author_email'];
  984. }
  985. $comment = CommentsEdit::alloc(null, $input, function (CommentsEdit $comment) {
  986. $comment->editComment();
  987. });
  988. return $comment->have();
  989. }
  990. /**
  991. * 更新评论
  992. *
  993. * @param integer $blogId
  994. * @param string $userName
  995. * @param string $password
  996. * @param mixed $path
  997. * @param array $struct
  998. * @return int
  999. * @throws \Exception
  1000. */
  1001. public function wpNewComment(int $blogId, string $userName, string $password, $path, array $struct): int
  1002. {
  1003. if (is_numeric($path)) {
  1004. $post = Archive::alloc('type=single', ['cid' => $path], false);
  1005. if ($post->have()) {
  1006. $path = $post->permalink;
  1007. }
  1008. } else {
  1009. $path = Common::url(substr($path, strlen($this->options->index)), '/');
  1010. }
  1011. $input = [
  1012. 'permalink' => $path,
  1013. 'type' => 'comment'
  1014. ];
  1015. if (isset($struct['comment_author'])) {
  1016. $input['author'] = $struct['author'];
  1017. }
  1018. if (isset($struct['comment_author_email'])) {
  1019. $input['mail'] = $struct['author_email'];
  1020. }
  1021. if (isset($struct['comment_author_url'])) {
  1022. $input['url'] = $struct['author_url'];
  1023. }
  1024. if (isset($struct['comment_parent'])) {
  1025. $input['parent'] = $struct['comment_parent'];
  1026. }
  1027. if (isset($struct['content'])) {
  1028. $input['text'] = $struct['content'];
  1029. }
  1030. $comment = Feedback::alloc(['checkReferer' => false], $input, function (Feedback $comment) {
  1031. $comment->action();
  1032. });
  1033. return $comment->have() ? $comment->coid : 0;
  1034. }
  1035. /**
  1036. * 获取媒体文件
  1037. *
  1038. * @param integer $blogId
  1039. * @param string $userName
  1040. * @param string $password
  1041. * @param array $struct
  1042. * @return array
  1043. */
  1044. public function wpGetMediaLibrary(int $blogId, string $userName, string $password, array $struct): array
  1045. {
  1046. $input = [];
  1047. if (!empty($struct['parent_id'])) {
  1048. $input['parent'] = $struct['parent_id'];
  1049. }
  1050. if (!empty($struct['mime_type'])) {
  1051. $input['mime'] = $struct['mime_type'];
  1052. }
  1053. $pageSize = 10;
  1054. if (!empty($struct['number'])) {
  1055. $pageSize = abs(intval($struct['number']));
  1056. }
  1057. if (!empty($struct['offset'])) {
  1058. $input['page'] = abs(intval($struct['offset'])) + 1;
  1059. }
  1060. $attachments = AttachmentAdmin::alloc('pageSize=' . $pageSize, $input, false);
  1061. $attachmentsStruct = [];
  1062. while ($attachments->next()) {
  1063. $attachmentsStruct[] = [
  1064. 'attachment_id' => $attachments->cid,
  1065. 'date_created_gmt' => new Date($this->options->timezone + $attachments->created),
  1066. 'parent' => $attachments->parent,
  1067. 'link' => $attachments->attachment->url,
  1068. 'title' => $attachments->title,
  1069. 'caption' => $attachments->slug,
  1070. 'description' => $attachments->attachment->description,
  1071. 'metadata' => [
  1072. 'file' => $attachments->attachment->path,
  1073. 'size' => $attachments->attachment->size,
  1074. ],
  1075. 'thumbnail' => $attachments->attachment->url,
  1076. ];
  1077. }
  1078. return $attachmentsStruct;
  1079. }
  1080. /**
  1081. * 获取媒体文件
  1082. *
  1083. * @param integer $blogId
  1084. * @param string $userName
  1085. * @param string $password
  1086. * @param int $attachmentId
  1087. * @return array
  1088. */
  1089. public function wpGetMediaItem(int $blogId, string $userName, string $password, int $attachmentId): array
  1090. {
  1091. $attachment = AttachmentEdit::alloc(null, ['cid' => $attachmentId]);
  1092. return [
  1093. 'attachment_id' => $attachment->cid,
  1094. 'date_created_gmt' => new Date($this->options->timezone + $attachment->created),
  1095. 'parent' => $attachment->parent,
  1096. 'link' => $attachment->attachment->url,
  1097. 'title' => $attachment->title,
  1098. 'caption' => $attachment->slug,
  1099. 'description' => $attachment->attachment->description,
  1100. 'metadata' => [
  1101. 'file' => $attachment->attachment->path,
  1102. 'size' => $attachment->attachment->size,
  1103. ],
  1104. 'thumbnail' => $attachment->attachment->url,
  1105. ];
  1106. }
  1107. /**
  1108. * 获取指定id的post
  1109. *
  1110. * @param int $postId
  1111. * @param string $userName
  1112. * @param string $password
  1113. * @return array
  1114. */
  1115. public function mwGetPost(int $postId, string $userName, string $password): array
  1116. {
  1117. $post = PostEdit::alloc(null, ['cid' => $postId], false);
  1118. /** 对文章内容做截取处理,以获得description和text_more*/
  1119. [$excerpt, $more] = $this->getPostExtended($post);
  1120. /** 只需要分类的name*/
  1121. $categories = array_column($post->categories, 'name');
  1122. $tags = array_column($post->tags, 'name');
  1123. return [
  1124. 'dateCreated' => new Date($this->options->timezone + $post->created),
  1125. 'userid' => $post->authorId,
  1126. 'postid' => $post->cid,
  1127. 'description' => $excerpt,
  1128. 'title' => $post->title,
  1129. 'link' => $post->permalink,
  1130. 'permaLink' => $post->permalink,
  1131. 'categories' => $categories,
  1132. 'mt_excerpt' => $post->description,
  1133. 'mt_text_more' => $more,
  1134. 'mt_allow_comments' => intval($post->allowComment),
  1135. 'mt_allow_pings' => intval($post->allowPing),
  1136. 'mt_keywords' => implode(', ', $tags),
  1137. 'wp_slug' => $post->slug,
  1138. 'wp_password' => $post->password,
  1139. 'wp_author' => $post->author->name,
  1140. 'wp_author_id' => $post->authorId,
  1141. 'wp_author_display_name' => $post->author->screenName,
  1142. 'date_created_gmt' => new Date($post->created),
  1143. 'post_status' => $this->typechoToWordpressStatus($post->status, 'post'),
  1144. 'custom_fields' => [],
  1145. 'sticky' => 0
  1146. ];
  1147. }
  1148. /**
  1149. * 获取前$postsNum个post
  1150. *
  1151. * @param int $blogId
  1152. * @param string $userName
  1153. * @param string $password
  1154. * @param int $postsNum
  1155. * @return array
  1156. */
  1157. public function mwGetRecentPosts(int $blogId, string $userName, string $password, int $postsNum): array
  1158. {
  1159. $posts = PostAdmin::alloc('pageSize=' . $postsNum, 'status=all');
  1160. $postStructs = [];
  1161. /** 如果这个post存在则输出,否则输出错误 */
  1162. while ($posts->next()) {
  1163. /** 对文章内容做截取处理,以获得description和text_more*/
  1164. [$excerpt, $more] = $this->getPostExtended($posts);
  1165. /** 只需要分类的name*/
  1166. /** 可以用flatten函数处理 */
  1167. $categories = array_column($posts->categories, 'name');
  1168. $tags = array_column($posts->tags, 'name');
  1169. $postStructs[] = [
  1170. 'dateCreated' => new Date($this->options->timezone + $posts->created),
  1171. 'userid' => $posts->authorId,
  1172. 'postid' => $posts->cid,
  1173. 'description' => $excerpt,
  1174. 'title' => $posts->title,
  1175. 'link' => $posts->permalink,
  1176. 'permaLink' => $posts->permalink,
  1177. 'categories' => $categories,
  1178. 'mt_excerpt' => $posts->description,
  1179. 'mt_text_more' => $more,
  1180. 'wp_more_text' => $more,
  1181. 'mt_allow_comments' => intval($posts->allowComment),
  1182. 'mt_allow_pings' => intval($posts->allowPing),
  1183. 'mt_keywords' => implode(', ', $tags),
  1184. 'wp_slug' => $posts->slug,
  1185. 'wp_password' => $posts->password,
  1186. 'wp_author' => $posts->author->name,
  1187. 'wp_author_id' => $posts->authorId,
  1188. 'wp_author_display_name' => $posts->author->screenName,
  1189. 'date_created_gmt' => new Date($posts->created),
  1190. 'post_status' => $this->typechoToWordpressStatus(
  1191. ($posts->hasSaved || 'post_draft' == $posts->type) ? 'draft' : $posts->status,
  1192. 'post'
  1193. ),
  1194. 'custom_fields' => [],
  1195. 'wp_post_format' => 'standard',
  1196. 'date_modified' => new Date($this->options->timezone + $posts->modified),
  1197. 'date_modified_gmt' => new Date($posts->modified),
  1198. 'wp_post_thumbnail' => '',
  1199. 'sticky' => 0
  1200. ];
  1201. }
  1202. return $postStructs;
  1203. }
  1204. /**
  1205. * 获取所有的分类
  1206. *
  1207. * @param int $blogId
  1208. * @param string $userName
  1209. * @param string $password
  1210. * @return array
  1211. */
  1212. public function mwGetCategories(int $blogId, string $userName, string $password): array
  1213. {
  1214. $categories = CategoryRows::alloc();
  1215. /** 初始化category数组*/
  1216. $categoryStructs = [];
  1217. while ($categories->next()) {
  1218. $categoryStructs[] = [
  1219. 'categoryId' => $categories->mid,
  1220. 'parentId' => $categories->parent,
  1221. 'categoryName' => $categories->name,
  1222. 'categoryDescription' => $categories->description,
  1223. 'description' => $categories->name,
  1224. 'htmlUrl' => $categories->permalink,
  1225. 'rssUrl' => $categories->feedUrl,
  1226. ];
  1227. }
  1228. return $categoryStructs;
  1229. }
  1230. /**
  1231. * mwNewMediaObject
  1232. *
  1233. * @param int $blogId
  1234. * @param string $userName
  1235. * @param string $password
  1236. * @param array $data
  1237. * @return array
  1238. * @throws Exception
  1239. * @throws \Typecho\Db\Exception
  1240. */
  1241. public function mwNewMediaObject(int $blogId, string $userName, string $password, array $data): array
  1242. {
  1243. $result = Upload::uploadHandle($data);
  1244. if (false === $result) {
  1245. throw new Exception('upload failed', -32001);
  1246. } else {
  1247. $insertId = $this->insert([
  1248. 'title' => $result['name'],
  1249. 'slug' => $result['name'],
  1250. 'type' => 'attachment',
  1251. 'status' => 'publish',
  1252. 'text' => serialize($result),
  1253. 'allowComment' => 1,
  1254. 'allowPing' => 0,
  1255. 'allowFeed' => 1
  1256. ]);
  1257. $this->db->fetchRow($this->select()->where('table.contents.cid = ?', $insertId)
  1258. ->where('table.contents.type = ?', 'attachment'), [$this, 'push']);
  1259. /** 增加插件接口 */
  1260. self::pluginHandle()->upload($this);
  1261. return [
  1262. 'file' => $this->attachment->name,
  1263. 'url' => $this->attachment->url
  1264. ];
  1265. }
  1266. }
  1267. /**
  1268. * 获取 $postNum个post title
  1269. *
  1270. * @param int $blogId
  1271. * @param string $userName
  1272. * @param string $password
  1273. * @param int $postsNum
  1274. * @return array
  1275. */
  1276. public function mtGetRecentPostTitles(int $blogId, string $userName, string $password, int $postsNum): array
  1277. {
  1278. /** 读取数据*/
  1279. $posts = PostAdmin::alloc('pageSize=' . $postsNum, 'status=all');
  1280. /**初始化*/
  1281. $postTitleStructs = [];
  1282. while ($posts->next()) {
  1283. $postTitleStructs[] = [
  1284. 'dateCreated' => new Date($this->options->timezone + $posts->created),
  1285. 'userid' => $posts->authorId,
  1286. 'postid' => $posts->cid,
  1287. 'title' => $posts->title,
  1288. 'date_created_gmt' => new Date($this->options->timezone + $posts->created)
  1289. ];
  1290. }
  1291. return $postTitleStructs;
  1292. }
  1293. /**
  1294. * 获取分类列表
  1295. *
  1296. * @param int $blogId
  1297. * @param string $userName
  1298. * @param string $password
  1299. * @return array
  1300. */
  1301. public function mtGetCategoryList(int $blogId, string $userName, string $password): array
  1302. {
  1303. $categories = CategoryRows::alloc();
  1304. /** 初始化categorise数组*/
  1305. $categoryStructs = [];
  1306. while ($categories->next()) {
  1307. $categoryStructs[] = [
  1308. 'categoryId' => $categories->mid,
  1309. 'categoryName' => $categories->name,
  1310. ];
  1311. }
  1312. return $categoryStructs;
  1313. }
  1314. /**
  1315. * 获取指定post的分类
  1316. *
  1317. * @param int $postId
  1318. * @param string $userName
  1319. * @param string $password
  1320. * @return array
  1321. */
  1322. public function mtGetPostCategories(int $postId, string $userName, string $password): array
  1323. {
  1324. $post = PostEdit::alloc(null, ['cid' => $postId], false);
  1325. /** 格式化categories*/
  1326. $categories = [];
  1327. foreach ($post->categories as $category) {
  1328. $categories[] = [
  1329. 'categoryName' => $category['name'],
  1330. 'categoryId' => $category['mid'],
  1331. 'isPrimary' => true
  1332. ];
  1333. }
  1334. return $categories;
  1335. }
  1336. /**
  1337. * 修改post的分类
  1338. *
  1339. * @param int $postId
  1340. * @param string $userName
  1341. * @param string $password
  1342. * @param array $categories
  1343. * @return bool
  1344. * @throws \Typecho\Db\Exception
  1345. */
  1346. public function mtSetPostCategories(int $postId, string $userName, string $password, array $categories): bool
  1347. {
  1348. PostEdit::alloc(null, ['cid' => $postId], function (PostEdit $post) use ($postId, $categories) {
  1349. $post->setCategories($postId, array_column($categories, 'categoryId'), 'publish' == $post->status);
  1350. });
  1351. return true;
  1352. }
  1353. /**
  1354. * 发布(重建)数据
  1355. *
  1356. * @param int $postId
  1357. * @param string $userName
  1358. * @param string $password
  1359. * @return bool
  1360. */
  1361. public function mtPublishPost(int $postId, string $userName, string $password): bool
  1362. {
  1363. PostEdit::alloc(null, ['cid' => $postId, 'status' => 'publish'], function (PostEdit $post) {
  1364. $post->markPost();
  1365. });
  1366. return true;
  1367. }
  1368. /**
  1369. * 取得当前用户的所有blog
  1370. *
  1371. * @param int $blogId
  1372. * @param string $userName
  1373. * @param string $password
  1374. * @return array
  1375. */
  1376. public function bloggerGetUsersBlogs(int $blogId, string $userName, string $password): array
  1377. {
  1378. return [
  1379. [
  1380. 'isAdmin' => $this->user->pass('administrator', true),
  1381. 'url' => $this->options->siteUrl,
  1382. 'blogid' => 1,
  1383. 'blogName' => $this->options->title,
  1384. 'xmlrpc' => $this->options->xmlRpcUrl
  1385. ]
  1386. ];
  1387. }
  1388. /**
  1389. * 返回当前用户的信息
  1390. *
  1391. * @param int $blogId
  1392. * @param string $userName
  1393. * @param string $password
  1394. * @return array
  1395. */
  1396. public function bloggerGetUserInfo(int $blogId, string $userName, string $password): array
  1397. {
  1398. return [
  1399. 'nickname' => $this->user->screenName,
  1400. 'userid' => $this->user->uid,
  1401. 'url' => $this->user->url,
  1402. 'email' => $this->user->mail,
  1403. 'lastname' => '',
  1404. 'firstname' => ''
  1405. ];
  1406. }
  1407. /**
  1408. * 获取当前作者的一个指定id的post的详细信息
  1409. *
  1410. * @param int $blogId
  1411. * @param int $postId
  1412. * @param string $userName
  1413. * @param string $password
  1414. * @return array
  1415. */
  1416. public function bloggerGetPost(int $blogId, int $postId, string $userName, string $password): array
  1417. {
  1418. $post = PostEdit::alloc(null, ['cid' => $postId]);
  1419. $categories = array_column($post->categories, 'name');
  1420. $content = '<title>' . $post->title . '</title>';
  1421. $content .= '<category>' . implode(',', $categories) . '</category>';
  1422. $content .= stripslashes($post->text);
  1423. return [
  1424. 'userid' => $post->authorId,
  1425. 'dateCreated' => new Date($this->options->timezone + $post->created),
  1426. 'content' => $content,
  1427. 'postid' => $post->cid
  1428. ];
  1429. }
  1430. /**
  1431. * bloggerDeletePost
  1432. * 删除文章
  1433. *
  1434. * @param int $blogId
  1435. * @param int $postId
  1436. * @param string $userName
  1437. * @param string $password
  1438. * @param mixed $publish
  1439. * @return bool
  1440. */
  1441. public function bloggerDeletePost(int $blogId, int $postId, string $userName, string $password, $publish): bool
  1442. {
  1443. PostEdit::alloc(null, ['cid' => $postId], function (PostEdit $post) {
  1444. $post->deletePost();
  1445. });
  1446. return true;
  1447. }
  1448. /**
  1449. * 获取当前作者前postsNum个post
  1450. *
  1451. * @param int $blogId
  1452. * @param string $userName
  1453. * @param string $password
  1454. * @param int $postsNum
  1455. * @return array
  1456. */
  1457. public function bloggerGetRecentPosts(int $blogId, string $userName, string $password, int $postsNum): array
  1458. {
  1459. $posts = PostAdmin::alloc('pageSize=' . $postsNum, 'status=all');
  1460. $postStructs = [];
  1461. while ($posts->next()) {
  1462. $categories = array_column($posts->categories, 'name');
  1463. $content = '<title>' . $posts->title . '</title>';
  1464. $content .= '<category>' . implode(',', $categories) . '</category>';
  1465. $content .= stripslashes($posts->text);
  1466. $struct = [
  1467. 'userid' => $posts->authorId,
  1468. 'dateCreated' => new Date($this->options->timezone + $posts->created),
  1469. 'content' => $content,
  1470. 'postid' => $posts->cid,
  1471. ];
  1472. $postStructs[] = $struct;
  1473. }
  1474. return $postStructs;
  1475. }
  1476. /**
  1477. * bloggerGetTemplate
  1478. *
  1479. * @param int $blogId
  1480. * @param string $userName
  1481. * @param string $password
  1482. * @param mixed $template
  1483. * @return bool
  1484. */
  1485. public function bloggerGetTemplate(int $blogId, string $userName, string $password, $template): bool
  1486. {
  1487. /** todo:暂时先返回true*/
  1488. return true;
  1489. }
  1490. /**
  1491. * bloggerSetTemplate
  1492. *
  1493. * @param int $blogId
  1494. * @param string $userName
  1495. * @param string $password
  1496. * @param mixed $content
  1497. * @param mixed $template
  1498. * @return bool
  1499. */
  1500. public function bloggerSetTemplate(int $blogId, string $userName, string $password, $content, $template): bool
  1501. {
  1502. /** todo:暂时先返回true*/
  1503. return true;
  1504. }
  1505. /**
  1506. * pingbackPing
  1507. *
  1508. * @param string $source
  1509. * @param string $target
  1510. * @return int
  1511. * @throws \Exception
  1512. */
  1513. public function pingbackPing(string $source, string $target): int
  1514. {
  1515. /** 检查目标地址是否正确*/
  1516. $pathInfo = Common::url(substr($target, strlen($this->options->index)), '/');
  1517. $post = Router::match($pathInfo);
  1518. /** 检查源地址是否合法 */
  1519. $params = parse_url($source);
  1520. if (false === $params || !in_array($params['scheme'], ['http', 'https'])) {
  1521. throw new Exception(_t('源地址服务器错误'), 16);
  1522. }
  1523. if (!Common::checkSafeHost($params['host'])) {
  1524. throw new Exception(_t('源地址服务器错误'), 16);
  1525. }
  1526. /** 这样可以得到cid或者slug*/
  1527. if (!($post instanceof Archive) || !$post->have() || !$post->is('single')) {
  1528. throw new Exception(_t('这个目标地址不存在'), 33);
  1529. }
  1530. if ($post) {
  1531. /** 检查是否可以ping*/
  1532. if ($post->allowPing) {
  1533. /** 现在可以ping了,但是还得检查下这个pingback是否已经存在了*/
  1534. $pingNum = $this->db->fetchObject($this->db->select(['COUNT(coid)' => 'num'])
  1535. ->from('table.comments')
  1536. ->where(
  1537. 'table.comments.cid = ? AND table.comments.url = ? AND table.comments.type <> ?',
  1538. $post->cid,
  1539. $source,
  1540. 'comment'
  1541. ))->num;
  1542. if ($pingNum <= 0) {
  1543. try {
  1544. $pingbackRequest = new Pingback($source, $target);
  1545. $pingback = [
  1546. 'cid' => $post->cid,
  1547. 'created' => $this->options->time,
  1548. 'agent' => $this->request->getAgent(),
  1549. 'ip' => $this->request->getIp(),
  1550. 'author' => $pingbackRequest->getTitle(),
  1551. 'url' => Common::safeUrl($source),
  1552. 'text' => $pingbackRequest->getContent(),
  1553. 'ownerId' => $post->author->uid,
  1554. 'type' => 'pingback',
  1555. 'status' => $this->options->commentsRequireModeration ? 'waiting' : 'approved'
  1556. ];
  1557. /** 加入plugin */
  1558. $pingback = self::pluginHandle()->pingback($pingback, $post);
  1559. /** 执行插入*/
  1560. $insertId = Comments::alloc()->insert($pingback);
  1561. /** 评论完成接口 */
  1562. self::pluginHandle()->finishPingback($this);
  1563. return $insertId;
  1564. } catch (WidgetException $e) {
  1565. throw new Exception(_t('源地址服务器错误'), 16);
  1566. }
  1567. } else {
  1568. throw new Exception(_t('PingBack已经存在'), 48);
  1569. }
  1570. } else {
  1571. throw new Exception(_t('目标地址禁止Ping'), 49);
  1572. }
  1573. } else {
  1574. throw new Exception(_t('这个目标地址不存在'), 33);
  1575. }
  1576. }
  1577. /**
  1578. * 入口执行方法
  1579. *
  1580. * @throws Exception
  1581. */
  1582. public function action()
  1583. {
  1584. if (0 == $this->options->allowXmlRpc) {
  1585. throw new Exception(_t('请求的地址不存在'), 404);
  1586. }
  1587. if (isset($this->request->rsd)) {
  1588. echo
  1589. <<<EOF
  1590. <?xml version="1.0" encoding="{$this->options->charset}"?>
  1591. <rsd version="1.0" xmlns="http://archipelago.phrasewise.com/rsd">
  1592. <service>
  1593. <engineName>Typecho</engineName>
  1594. <engineLink>http://www.typecho.org/</engineLink>
  1595. <homePageLink>{$this->options->siteUrl}</homePageLink>
  1596. <apis>
  1597. <api name="WordPress" blogID="1" preferred="true" apiLink="{$this->options->xmlRpcUrl}" />
  1598. <api name="Movable Type" blogID="1" preferred="false" apiLink="{$this->options->xmlRpcUrl}" />
  1599. <api name="MetaWeblog" blogID="1" preferred="false" apiLink="{$this->options->xmlRpcUrl}" />
  1600. <api name="Blogger" blogID="1" preferred="false" apiLink="{$this->options->xmlRpcUrl}" />
  1601. </apis>
  1602. </service>
  1603. </rsd>
  1604. EOF;
  1605. } elseif (isset($this->request->wlw)) {
  1606. echo
  1607. <<<EOF
  1608. <?xml version="1.0" encoding="{$this->options->charset}"?>
  1609. <manifest xmlns="http://schemas.microsoft.com/wlw/manifest/weblog">
  1610. <options>
  1611. <supportsKeywords>Yes</supportsKeywords>
  1612. <supportsFileUpload>Yes</supportsFileUpload>
  1613. <supportsExtendedEntries>Yes</supportsExtendedEntries>
  1614. <supportsCustomDate>Yes</supportsCustomDate>
  1615. <supportsCategories>Yes</supportsCategories>
  1616. <supportsCategoriesInline>Yes</supportsCategoriesInline>
  1617. <supportsMultipleCategories>Yes</supportsMultipleCategories>
  1618. <supportsHierarchicalCategories>Yes</supportsHierarchicalCategories>
  1619. <supportsNewCategories>Yes</supportsNewCategories>
  1620. <supportsNewCategoriesInline>Yes</supportsNewCategoriesInline>
  1621. <supportsCommentPolicy>Yes</supportsCommentPolicy>
  1622. <supportsPingPolicy>Yes</supportsPingPolicy>
  1623. <supportsAuthor>Yes</supportsAuthor>
  1624. <supportsSlug>Yes</supportsSlug>
  1625. <supportsPassword>Yes</supportsPassword>
  1626. <supportsExcerpt>Yes</supportsExcerpt>
  1627. <supportsTrackbacks>Yes</supportsTrackbacks>
  1628. <supportsPostAsDraft>Yes</supportsPostAsDraft>
  1629. <supportsPages>Yes</supportsPages>
  1630. <supportsPageParent>No</supportsPageParent>
  1631. <supportsPageOrder>Yes</supportsPageOrder>
  1632. <requiresXHTML>True</requiresXHTML>
  1633. <supportsAutoUpdate>No</supportsAutoUpdate>
  1634. </options>
  1635. </manifest>
  1636. EOF;
  1637. } else {
  1638. $api = [
  1639. /** WordPress API */
  1640. 'wp.getPage' => [$this, 'wpGetPage'],
  1641. 'wp.getPages' => [$this, 'wpGetPages'],
  1642. 'wp.newPage' => [$this, 'wpNewPage'],
  1643. 'wp.deletePage' => [$this, 'wpDeletePage'],
  1644. 'wp.editPage' => [$this, 'wpEditPage'],
  1645. 'wp.getPageList' => [$this, 'wpGetPageList'],
  1646. 'wp.getAuthors' => [$this, 'wpGetAuthors'],
  1647. 'wp.getCategories' => [$this, 'mwGetCategories'],
  1648. 'wp.newCategory' => [$this, 'wpNewCategory'],
  1649. 'wp.suggestCategories' => [$this, 'wpSuggestCategories'],
  1650. 'wp.uploadFile' => [$this, 'mwNewMediaObject'],
  1651. /** New WordPress API since 2.9.2 */
  1652. 'wp.getUsersBlogs' => [$this, 'wpGetUsersBlogs'],
  1653. 'wp.getTags' => [$this, 'wpGetTags'],
  1654. 'wp.deleteCategory' => [$this, 'wpDeleteCategory'],
  1655. 'wp.getCommentCount' => [$this, 'wpGetCommentCount'],
  1656. 'wp.getPostStatusList' => [$this, 'wpGetPostStatusList'],
  1657. 'wp.getPageStatusList' => [$this, 'wpGetPageStatusList'],
  1658. 'wp.getPageTemplates' => [$this, 'wpGetPageTemplates'],
  1659. 'wp.getOptions' => [$this, 'wpGetOptions'],
  1660. 'wp.setOptions' => [$this, 'wpSetOptions'],
  1661. 'wp.getComment' => [$this, 'wpGetComment'],
  1662. 'wp.getComments' => [$this, 'wpGetComments'],
  1663. 'wp.deleteComment' => [$this, 'wpDeleteComment'],
  1664. 'wp.editComment' => [$this, 'wpEditComment'],
  1665. 'wp.newComment' => [$this, 'wpNewComment'],
  1666. 'wp.getCommentStatusList' => [$this, 'wpGetCommentStatusList'],
  1667. /** New Wordpress API after 2.9.2 */
  1668. 'wp.getProfile' => [$this, 'wpGetProfile'],
  1669. 'wp.getPostFormats' => [$this, 'wpGetPostFormats'],
  1670. 'wp.getMediaLibrary' => [$this, 'wpGetMediaLibrary'],
  1671. 'wp.getMediaItem' => [$this, 'wpGetMediaItem'],
  1672. 'wp.editPost' => [$this, 'wpEditPost'],
  1673. /** Blogger API */
  1674. 'blogger.getUsersBlogs' => [$this, 'bloggerGetUsersBlogs'],
  1675. 'blogger.getUserInfo' => [$this, 'bloggerGetUserInfo'],
  1676. 'blogger.getPost' => [$this, 'bloggerGetPost'],
  1677. 'blogger.getRecentPosts' => [$this, 'bloggerGetRecentPosts'],
  1678. 'blogger.getTemplate' => [$this, 'bloggerGetTemplate'],
  1679. 'blogger.setTemplate' => [$this, 'bloggerSetTemplate'],
  1680. 'blogger.deletePost' => [$this, 'bloggerDeletePost'],
  1681. /** MetaWeblog API (with MT extensions to structs) */
  1682. 'metaWeblog.newPost' => [$this, 'mwNewPost'],
  1683. 'metaWeblog.editPost' => [$this, 'mwEditPost'],
  1684. 'metaWeblog.getPost' => [$this, 'mwGetPost'],
  1685. 'metaWeblog.getRecentPosts' => [$this, 'mwGetRecentPosts'],
  1686. 'metaWeblog.getCategories' => [$this, 'mwGetCategories'],
  1687. 'metaWeblog.newMediaObject' => [$this, 'mwNewMediaObject'],
  1688. /** MetaWeblog API aliases for Blogger API */
  1689. 'metaWeblog.deletePost' => [$this, 'bloggerDeletePost'],
  1690. 'metaWeblog.getTemplate' => [$this, 'bloggerGetTemplate'],
  1691. 'metaWeblog.setTemplate' => [$this, 'bloggerSetTemplate'],
  1692. 'metaWeblog.getUsersBlogs' => [$this, 'bloggerGetUsersBlogs'],
  1693. /** MovableType API */
  1694. 'mt.getCategoryList' => [$this, 'mtGetCategoryList'],
  1695. 'mt.getRecentPostTitles' => [$this, 'mtGetRecentPostTitles'],
  1696. 'mt.getPostCategories' => [$this, 'mtGetPostCategories'],
  1697. 'mt.setPostCategories' => [$this, 'mtSetPostCategories'],
  1698. 'mt.publishPost' => [$this, 'mtPublishPost'],
  1699. /** PingBack */
  1700. 'pingback.ping' => [$this, 'pingbackPing'],
  1701. // 'pingback.extensions.getPingbacks' => array($this,'pingbackExtensionsGetPingbacks'),
  1702. ];
  1703. if (1 == $this->options->allowXmlRpc) {
  1704. unset($api['pingback.ping']);
  1705. }
  1706. /** 直接把初始化放到这里 */
  1707. $server = new Server($api);
  1708. $server->setHook($this);
  1709. $server->serve();
  1710. }
  1711. }
  1712. /**
  1713. * 获取扩展字段
  1714. *
  1715. * @param Contents $content
  1716. * @return array
  1717. */
  1718. private function getPostExtended(Contents $content): array
  1719. {
  1720. //根据客户端显示来判断是否显示html代码
  1721. $agent = $this->request->getAgent();
  1722. switch (true) {
  1723. case false !== strpos($agent, 'wp-iphone'): // wordpress iphone客户端
  1724. case false !== strpos($agent, 'wp-blackberry'): // 黑莓
  1725. case false !== strpos($agent, 'wp-andriod'): // andriod
  1726. case false !== strpos($agent, 'plain-text'): // 这是预留给第三方开发者的接口, 用于强行调用非所见即所得数据
  1727. case $this->options->xmlrpcMarkdown:
  1728. $text = $content->text;
  1729. break;
  1730. default:
  1731. $text = $content->content;
  1732. break;
  1733. }
  1734. $post = explode('<!--more-->', $text, 2);
  1735. return [
  1736. $this->options->xmlrpcMarkdown ? $post[0] : Common::fixHtml($post[0]),
  1737. isset($post[1]) ? Common::fixHtml($post[1]) : null
  1738. ];
  1739. }
  1740. /**
  1741. * 将typecho的状态类型转换为wordperss的风格
  1742. *
  1743. * @param string $status typecho的状态
  1744. * @param string $type 内容类型
  1745. * @return string
  1746. */
  1747. private function typechoToWordpressStatus(string $status, string $type = 'post'): string
  1748. {
  1749. if ('post' == $type) {
  1750. /** 文章状态 */
  1751. switch ($status) {
  1752. case 'waiting':
  1753. return 'pending';
  1754. case 'publish':
  1755. case 'draft':
  1756. case 'private':
  1757. return $status;
  1758. default:
  1759. return 'publish';
  1760. }
  1761. } elseif ('page' == $type) {
  1762. switch ($status) {
  1763. case 'publish':
  1764. case 'draft':
  1765. case 'private':
  1766. return $status;
  1767. default:
  1768. return 'publish';
  1769. }
  1770. } elseif ('comment' == $type) {
  1771. switch ($status) {
  1772. case 'waiting':
  1773. return 'hold';
  1774. case 'spam':
  1775. return $status;
  1776. case 'publish':
  1777. case 'approved':
  1778. default:
  1779. return 'approve';
  1780. }
  1781. }
  1782. return '';
  1783. }
  1784. /**
  1785. * 将wordpress的状态类型转换为typecho的风格
  1786. *
  1787. * @access private
  1788. * @param string $status wordpress的状态
  1789. * @param string $type 内容类型
  1790. * @return string
  1791. */
  1792. private function wordpressToTypechoStatus(string $status, string $type = 'post'): string
  1793. {
  1794. if ('post' == $type) {
  1795. /** 文章状态 */
  1796. switch ($status) {
  1797. case 'pending':
  1798. return 'waiting';
  1799. case 'publish':
  1800. case 'draft':
  1801. case 'private':
  1802. case 'waiting':
  1803. return $status;
  1804. default:
  1805. return 'publish';
  1806. }
  1807. } elseif ('page' == $type) {
  1808. switch ($status) {
  1809. case 'publish':
  1810. case 'draft':
  1811. case 'private':
  1812. return $status;
  1813. default:
  1814. return 'publish';
  1815. }
  1816. } elseif ('comment' == $type) {
  1817. switch ($status) {
  1818. case 'hold':
  1819. case 'waiting':
  1820. return 'waiting';
  1821. case 'spam':
  1822. return $status;
  1823. case 'approve':
  1824. case 'publish':
  1825. case 'approved':
  1826. default:
  1827. return 'approved';
  1828. }
  1829. }
  1830. return '';
  1831. }
  1832. }