PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/protected/modules/admin/controllers/PostController.php

https://github.com/smarteng/onenoteme
PHP | 220 lines | 191 code | 29 blank | 0 comment | 22 complexity | 9950ab8cf18cba88b60c06d79479f6e1 MD5 | raw file
  1. <?php
  2. class PostController extends AdminController
  3. {
  4. public function actionWeibo()
  5. {
  6. $pageSize = 20;
  7. $criteria = new CDbCriteria();
  8. $criteria->limit = $pageSize;
  9. $criteria->order = 't.id desc';
  10. $models = PostTemp::model()->findAll($criteria);
  11. $data = array(
  12. 'models' => $models,
  13. );
  14. $this->render('weibo', $data);
  15. }
  16. public function actionWeiboVerify($id, $channel_id, $callback)
  17. {
  18. $id = (int)$id;
  19. $channel_id = (int)$channel_id;
  20. $temp = PostTemp::model()->findByPk($id);
  21. if ($temp === null)
  22. $data = 1;
  23. else {
  24. try {
  25. $username = $temp['username'];
  26. $userid = app()->getDb()->createCommand()
  27. ->select('id')
  28. ->from(TABLE_USER)
  29. ->where('screen_name = :username', array(':username' => $username))
  30. ->queryScalar();
  31. $content = trim($_POST['weibotext']);
  32. $content = empty($content) ? $temp->content : $content;
  33. $post = new Post();
  34. $post->content = $content;
  35. $post->channel_id = $channel_id;
  36. $post->up_score = mt_rand(100, 300);
  37. $post->down_score = mt_rand(10, 40);
  38. $post->view_nums = mt_rand(100, 500);
  39. if ($userid > 0) {
  40. $post->user_id = (int)$userid;
  41. $post->user_name = $username;
  42. }
  43. if ($channel_id == CHANNEL_LENGTU || $channel_id == CHANNEL_GIRL) {
  44. $post->thumbnail_pic = $temp->thumbnail_pic;
  45. $post->bmiddle_pic = $temp->bmiddle_pic;
  46. $post->original_pic = $temp->original_pic;
  47. }
  48. $result = $post->save();
  49. if ($result) {
  50. $temp->delete();
  51. self::saveWeiboComments($post->id, $temp->weibo_id);
  52. }
  53. $data = (int)$result;
  54. }
  55. catch (Exception $e) {
  56. $data = 0;
  57. echo $e->getMessage();
  58. }
  59. }
  60. CDBase::jsonp($callback, $data);
  61. }
  62. private static function saveWeiboComments($pid, $wid)
  63. {
  64. if (empty($pid) || empty($wid))
  65. return false;
  66. $data = self::fetchWeiboComments($wid);
  67. if (empty($data)) return false;
  68. $comments = $data['comments'];
  69. foreach ((array)$comments as $row) {
  70. $text = self::filterComment($row['text']);
  71. if (empty($text)) continue;
  72. self::saveCommentRow($pid, $text);
  73. }
  74. }
  75. private static function filterComment($text)
  76. {
  77. if (mb_strlen($text) < 3) return false;
  78. $text = str_replace(array('互粉', '转发', '微博', '沙发', '回覆'), '', $text);
  79. $pattern = '/\[.+?\]/is';
  80. $text = preg_replace($pattern, '', $text);
  81. $pos = mb_strpos($text, '//', 0, app()->charset);
  82. if ($pos === 0)
  83. return false;
  84. elseif ($pos > 0) {
  85. $text = mb_substr($text, 0, $pos, app()->charset);
  86. }
  87. $pos = mb_strpos($text, '@', 0, app()->charset);
  88. if ($pos === 0)
  89. return false;
  90. elseif ($pos > 0) {
  91. $text = mb_substr($text, 0, $pos, app()->charset);
  92. }
  93. return trim($text);
  94. }
  95. private static function saveCommentRow($pid, $text)
  96. {
  97. $pid = (int)$pid;
  98. if (empty($pid) || empty($text)) return false;
  99. try {
  100. $model = new Comment();
  101. $model->content = $text;
  102. $model->post_id = $pid;
  103. $model->up_score = mt_rand(20, 70);
  104. $model->down_score = mt_rand(0, 10);
  105. $model->state = COMMENT_STATE_ENABLED;
  106. return $model->save();
  107. }
  108. catch (Exception $e) {
  109. echo $e->getMessage();
  110. return false;
  111. }
  112. }
  113. private static function fetchWeiboComments($wid)
  114. {
  115. $url = 'https://api.weibo.com/2/comments/show.json';
  116. $data = array(
  117. 'source' => WEIBO_APP_KEY,
  118. 'access_token' => app()->session['access_token'],
  119. 'id' => $wid,
  120. 'count' => 100,
  121. );
  122. $curl = new CdCurl();
  123. $curl->get($url, $data);
  124. if ($curl->errno() == 0) {
  125. $comments = json_decode($curl->rawdata(), true);
  126. return $comments;
  127. }
  128. else {
  129. echo $curl->error();
  130. return false;
  131. }
  132. }
  133. public function actionWeiboDelete($id, $callback)
  134. {
  135. $id = (int)$id;
  136. $result = PostTemp::model()->findByPk($id)->delete();
  137. CDBase::jsonp($callback, (int)$result);
  138. }
  139. public function actionVerify()
  140. {
  141. $criteria = new CDbCriteria();
  142. $criteria->addColumnCondition(array('state'=>Post::STATE_DISABLED));
  143. $data = self::fetchPostList($criteria, true, true);
  144. $this->render('verify', $data);
  145. }
  146. public function actionToday()
  147. {
  148. $date = getdate();
  149. $timestamp = mktime(0, 0, 0, $date['mon'], $date['mday'], $date['year']);
  150. $criteria = new CDbCriteria();
  151. $criteria->addCondition('create_time > :timestamp');
  152. $criteria->params = array(':timestamp' => $timestamp);
  153. $data = self::fetchPostList($criteria, true, true);
  154. $this->render('list', $data);
  155. }
  156. public function actionList()
  157. {
  158. $data = self::fetchPostList(null, true, true);
  159. $this->render('list', $data);
  160. }
  161. public function actionSearch()
  162. {
  163. }
  164. private static function fetchPostList(CDbCriteria $criteria = null, $pages = true, $sort = false)
  165. {
  166. $pageSize = 30;
  167. $criteria = ($criteria === null) ? new CDbCriteria() : $criteria;
  168. if ($pages) {
  169. $count = Post::model()->count($criteria);
  170. $pages = new CPagination($count);
  171. $pages->setPageSize($pageSize);
  172. $pages->applyLimit($criteria);
  173. }
  174. else
  175. $criteria->limit = $pageSize;
  176. if ($sort) {
  177. $sort = new CSort('Post');
  178. $sort->defaultOrder = 't.id desc';
  179. $sort->applyOrder($criteria);
  180. }
  181. else
  182. $criteria->order = 't.id desc';
  183. $models = Post::model()->findAll($criteria);
  184. $data = array(
  185. 'sort' => $sort,
  186. 'pages' => $pages,
  187. 'models' => $models,
  188. );
  189. return $data;
  190. }
  191. }