PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/var/Widget/Ajax.php

https://github.com/wangqin4377/typecho
PHP | 162 lines | 99 code | 17 blank | 46 comment | 14 complexity | 8b231fce1868ab7bbd636928be4bfebd MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. if (!defined('__TYPECHO_ROOT_DIR__')) exit;
  3. /**
  4. * 异步调用组件
  5. *
  6. * @category typecho
  7. * @package Widget
  8. * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
  9. * @license GNU General Public License 2.0
  10. * @version $Id$
  11. */
  12. /**
  13. * 异步调用组件
  14. *
  15. * @author qining
  16. * @category typecho
  17. * @package Widget
  18. */
  19. class Widget_Ajax extends Widget_Abstract_Options implements Widget_Interface_Do
  20. {
  21. /**
  22. * 针对rewrite验证的请求返回
  23. *
  24. * @access public
  25. * @return void
  26. */
  27. public function remoteCallback()
  28. {
  29. if ($this->options->generator == $this->request->getAgent()) {
  30. echo 'OK';
  31. }
  32. }
  33. /**
  34. * 获取最新版本
  35. *
  36. * @throws Typecho_Widget_Exception
  37. */
  38. public function checkVersion()
  39. {
  40. $this->user->pass('editor');
  41. $client = Typecho_Http_Client::get();
  42. if ($client) {
  43. $client->setHeader('User-Agent', $this->options->generator)
  44. ->setTimeout(10)
  45. ->send('https://github.com/typecho/typecho/releases.atom');
  46. /** 匹配内容体 */
  47. $response = $client->getResponseBody();
  48. preg_match_all("/<link rel=\"alternate\"[^>]+href=\"([^>]*)\"\s*\/>/is", $response, $matches);
  49. $result = array('available' => 0);
  50. list($soft, $version) = explode(' ', $this->options->generator);
  51. $current = explode('/', $version);
  52. if ($matches) {
  53. foreach ($matches[0] as $key => $val) {
  54. $title = trim($matches[1][$key]);
  55. if (preg_match("/v([0-9\.]+)\-([0-9\.]+)\-release$/is", $title, $out)) {
  56. if (version_compare($out[1], $current[0], '>=')
  57. && version_compare($out[2], $current[1], '>')) {
  58. $result = array(
  59. 'available' => 1,
  60. 'latest' => $out[1] . '-' . $out[2],
  61. 'current' => $current[0] . '-' . $current[1],
  62. 'link' => 'https://github.com' . $matches[1][$key]
  63. );
  64. break;
  65. }
  66. }
  67. }
  68. }
  69. $this->response->throwJson($result);
  70. return;
  71. }
  72. throw new Typecho_Widget_Exception(_t('禁止访问'), 403);
  73. }
  74. /**
  75. * 远程请求代理
  76. *
  77. * @throws Typecho_Widget_Exception
  78. */
  79. public function feed()
  80. {
  81. $this->user->pass('subscriber');
  82. $client = Typecho_Http_Client::get();
  83. if ($client) {
  84. $client->setHeader('User-Agent', $this->options->generator)
  85. ->setTimeout(10)
  86. ->send('http://typecho.org/feed/');
  87. /** 匹配内容体 */
  88. $response = $client->getResponseBody();
  89. preg_match_all("/<item>\s*<title>([^>]*)<\/title>\s*<link>([^>]*)<\/link>\s*<guid>[^>]*<\/guid>\s*<pubDate>([^>]*)<\/pubDate>/is", $response, $matches);
  90. $data = array();
  91. if ($matches) {
  92. foreach ($matches[0] as $key => $val) {
  93. $data[] = array(
  94. 'title' => $matches[1][$key],
  95. 'link' => $matches[2][$key],
  96. 'date' => date('n.j', strtotime($matches[3][$key]))
  97. );
  98. if ($key > 8) {
  99. break;
  100. }
  101. }
  102. }
  103. $this->response->throwJson($data);
  104. return;
  105. }
  106. throw new Typecho_Widget_Exception(_t('禁止访问'), 403);
  107. }
  108. /**
  109. * 自定义编辑器大小
  110. *
  111. * @access public
  112. * @return void
  113. */
  114. public function editorResize()
  115. {
  116. $this->user->pass('contributor');
  117. if ($this->db->fetchObject($this->db->select(array('COUNT(*)' => 'num'))
  118. ->from('table.options')->where('name = ? AND user = ?', 'editorSize', $this->user->uid))->num > 0) {
  119. $this->widget('Widget_Abstract_Options')
  120. ->update(array('value' => $this->request->size), $this->db->sql()->where('name = ? AND user = ?', 'editorSize', $this->user->uid));
  121. } else {
  122. $this->widget('Widget_Abstract_Options')->insert(array(
  123. 'name' => 'editorSize',
  124. 'value' => $this->request->size,
  125. 'user' => $this->user->uid
  126. ));
  127. }
  128. }
  129. /**
  130. * 异步请求入口
  131. *
  132. * @access public
  133. * @return void
  134. */
  135. public function action()
  136. {
  137. if (!$this->request->isAjax()) {
  138. $this->response->goBack();
  139. }
  140. $this->on($this->request->is('do=remoteCallback'))->remoteCallback();
  141. $this->on($this->request->is('do=feed'))->feed();
  142. $this->on($this->request->is('do=checkVersion'))->checkVersion();
  143. $this->on($this->request->is('do=editorResize'))->editorResize();
  144. }
  145. }