PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/plugins/GoogleCodeSVN/Action.php

http://typecho.googlecode.com/
PHP | 157 lines | 122 code | 31 blank | 4 comment | 32 complexity | 4203e8794f5f8a068349f63e239a258a MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. class GoogleCodeSVN_Action extends Widget_Abstract_Contents implements Widget_Interface_Do
  3. {
  4. private function parseFileName($fileName, $repositoryPath)
  5. {
  6. $result = array(
  7. 'do' => 'publish',
  8. 'allowComment' => $this->options->defaultAllowComment,
  9. 'allowPing' => $this->options->defaultAllowPing,
  10. 'allowFeed' => $this->options->defaultAllowFeed
  11. );
  12. $basePath = Helper::options()->plugin('GoogleCodeSVN')->basePath;
  13. $basePath = '/' . trim($basePath, '/') . '/';
  14. if (0 !== strpos($fileName, $basePath)) {
  15. return false;
  16. }
  17. $path = substr($fileName, strlen($basePath));
  18. $part = explode('/', $path);
  19. if (2 != count($part)) {
  20. return false;
  21. }
  22. list($categoryName, $baseName) = $part;
  23. list($slug) = explode('.', $baseName);
  24. $result['slug'] = $slug;
  25. $post = $this->db->fetchRow($this->db->select()
  26. ->from('table.contents')->where('slug = ?', $slug)->limit(1));
  27. if (!empty($post)) {
  28. if ('post' != $post['type']) {
  29. return false;
  30. } else {
  31. $result['cid'] = $post['cid'];
  32. }
  33. }
  34. /** ???????????? */
  35. $categorySlug = Typecho_Common::slugName($categoryName);
  36. $category = $this->db->fetchRow($this->db->select()
  37. ->from('table.metas')->where('slug = ? OR name = ?', $categorySlug, $categoryName)
  38. ->where('type = ?', 'category')->limit(1));
  39. /** ?????????????? */
  40. if (empty($category)) {
  41. $input['name'] = $categoryName;
  42. $input['slug'] = $categorySlug;
  43. $input['type'] = 'category';
  44. $input['description'] = $categoryName;
  45. $input['do'] = 'insert';
  46. $this->widget('Widget_Metas_Category_Edit', NULL, $input, false)->action();
  47. $result['category'] = array($this->widget('Widget_Notice')->getHighlightId());
  48. } else {
  49. $result['category'] = array($category['mid']);
  50. }
  51. $url = rtrim($repositoryPath, '/') . $fileName;
  52. $client = Typecho_Http_Client::get('Curl', 'Socket');
  53. if (false == $client) {
  54. return false;
  55. }
  56. $client->send($url);
  57. $result['text'] = '';
  58. $result['title'] = '';
  59. if (200 == $client->getResponseStatus() || 304 == $client->getResponseStatus()) {
  60. $response = trim($client->getResponseBody());
  61. list($title, $text) = explode("\n", $response, 2);
  62. $result['title'] = $title;
  63. $result['text'] = $text;
  64. }
  65. return $result;
  66. }
  67. public function action()
  68. {
  69. /** ????? */
  70. if (!isset($_SERVER['HTTP_GOOGLE_CODE_PROJECT_HOSTING_HOOK_HMAC'])) {
  71. return;
  72. }
  73. $googleSecretInfo = $_SERVER['HTTP_GOOGLE_CODE_PROJECT_HOSTING_HOOK_HMAC'];
  74. $revisionData = file_get_contents('php://input');
  75. if (empty($revisionData)) {
  76. return;
  77. }
  78. $secretVerify = hash_hmac("md5", $revisionData, Helper::options()->plugin('GoogleCodeSVN')->secretKey);
  79. if ($googleSecretInfo != $secretVerify) {
  80. return;
  81. }
  82. $data = Typecho_Json::decode($revisionData);
  83. if (!$data) {
  84. return;
  85. }
  86. /** ???? */
  87. $master = $this->db->fetchRow($this->db->select()->from('table.users')
  88. ->where('group = ?', 'administrator')
  89. ->order('uid', Typecho_Db::SORT_ASC)
  90. ->limit(1));
  91. if (empty($master)) {
  92. return false;
  93. } else if (!$this->user->simpleLogin($master['uid'])) {
  94. return false;
  95. }
  96. if (isset($data->revisions) && is_array($data->revisions)) {
  97. foreach ($data->revisions as $revision) {
  98. if (!empty($revision->added)) {
  99. foreach ($revision->added as $file) {
  100. $input = $this->parseFileName($file, $data->repository_path);
  101. if ($input) {
  102. $this->widget('Widget_Contents_Post_Edit', NULL, $input, false)->action();
  103. }
  104. }
  105. }
  106. if (!empty($revision->modified)) {
  107. foreach ($revision->modified as $file) {
  108. $input = $this->parseFileName($file, $data->repository_path);
  109. if ($input) {
  110. $this->widget('Widget_Contents_Post_Edit', NULL, $input, false)->action();
  111. }
  112. }
  113. }
  114. if (!empty($revision->removed)) {
  115. foreach ($revision->removed as $file) {
  116. $input = $this->parseFileName($file, $data->repository_path);
  117. if ($input && isset($input['cid'])) {
  118. $postId = $input['cid'];
  119. $this->widget('Widget_Contents_Post_Edit', NULL, "cid={$postId}", false)->deletePost();
  120. }
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }