PageRenderTime 106ms CodeModel.GetById 27ms RepoModel.GetById 13ms app.codeStats 0ms

/protected/apiv1/Api_Post.php

https://github.com/smarteng/onenoteme
PHP | 462 lines | 365 code | 83 blank | 14 comment | 40 complexity | 3096025a510b2f8a47a5e5ee33fa98c8 MD5 | raw file
  1. <?php
  2. /**
  3. * Post Api接口
  4. * @author Chris
  5. * @copyright cdcchen@gmail.com
  6. * @package api
  7. */
  8. define('APP_STORE_VERIFY', false);
  9. class Api_Post extends ApiBase
  10. {
  11. const DEFAULT_TIMELINE_MAX_COUNT = 35;
  12. const DEFAULT_HISTORY_MAX_COUNT = 35;
  13. const DEFAULT_RANDOM_MAX_COUNT = 12;
  14. public static function formatRow($row)
  15. {
  16. unset($row['video_ur'], $row['state'], $row['tags']);
  17. if (isset($row['comment_nums']))
  18. $row['visit_count_text'] = '阅:' . $row['comment_nums'];
  19. if (isset($row['comment_nums']))
  20. $row['comment_count_text'] = '评:' . $row['comment_nums'];
  21. if (isset($row['up_score']))
  22. $row['support_count_text'] = '顶:' . $row['up_score'];
  23. if (isset($row['down_score']))
  24. $row['oppose_count_text'] = '踩:' . $row['down_score'];
  25. if (isset($row['create_time']) && $row['create_time'])
  26. $row['create_time_text'] = date(param('formatShortDateTime'), $row['create_time']);
  27. if (isset($row['thumbnail_pic']) || isset($row['bmiddle_pic'])) {
  28. // 这里应该是thumbnail_pic,客户端全部使用的是bmiddle_pic,若换成thumbnail_pic,点击图片后会非常不清楚,所以暂时不使用bmiddle_pic
  29. $pic = $row['bmiddle_pic'];
  30. if (empty($pic))
  31. $pic = $row['bmiddle_pic'];
  32. if (empty($pic))
  33. $thumbnail = '';
  34. else {
  35. if (filter_var($pic, FILTER_VALIDATE_URL) === false){
  36. $pic = fbu($pic);
  37. $thumbnail = (filter_var($pic, FILTER_VALIDATE_URL) === false) ? $pic : '';
  38. }
  39. else
  40. $thumbnail = $pic;
  41. }
  42. $row['thumbnail'] = $thumbnail;
  43. unset($row['bmiddle_pic']);
  44. }
  45. return $row;
  46. }
  47. public static function formatRows($rows)
  48. {
  49. if (empty($rows))
  50. return array();
  51. foreach ($rows as $index => $row)
  52. $rows[$index] = self::formatRow($row);
  53. return $rows;
  54. }
  55. public function timeline()
  56. {
  57. self::requiredParams(array('channelid'));
  58. $params = $this->filterParams(array('channelid', 'count', 'fields', 'lastid', 'token'));
  59. $channelID = (int)$params['channelid'];
  60. // @todo test data
  61. $version = $this->_params['version'];
  62. $rows = self::fetchTestRows($channelID, $version);
  63. if ($rows !== false) return $rows;
  64. try {
  65. $fields = empty($params['fields']) ? '*' : $params['fields'];
  66. $lastid = empty($params['lastid']) ? 0 : (int)$params['lastid'];
  67. $count = (int)$params['count'];
  68. if ($count <= 0 || $count > self::DEFAULT_TIMELINE_MAX_COUNT)
  69. $count = self::DEFAULT_TIMELINE_MAX_COUNT;
  70. $condition = array('and', 'state = :enabled', 'channel_id = :channelid', 'id > :lastid');
  71. $param = array(':enabled'=>POST_STATE_ENABLED, ':channelid' => $channelID, ':lastid'=>$lastid);
  72. $cmd = app()->getDb()->createCommand()
  73. ->select($fields)
  74. ->from(TABLE_POST . ' t')
  75. ->where($condition, $param)
  76. ->order('id desc')
  77. ->limit($count);
  78. $rows = $cmd->queryAll();
  79. foreach ($rows as $index => $row)
  80. $rows[$index] = self::formatRow($row);
  81. self::updateLastRequestTime($token);
  82. return $rows;
  83. }
  84. catch (Exception $e) {
  85. throw new ApiException('系统错误', ApiError::SYSTEM_ERROR, $params['debug']);
  86. }
  87. }
  88. public function history()
  89. {
  90. self::requiredParams(array('channelid', 'beforetime'));
  91. $params = $this->filterParams(array('channelid', 'count', 'fields', 'beforetime'));
  92. $channelID = (int)$params['channelid'];
  93. $beforeTime = (int)$params['beforetime'];
  94. // @todo test data
  95. $version = $this->_params['version'];
  96. $rows = self::fetchTestRows($channelID, $version);
  97. if ($rows !== false) return $rows;
  98. try {
  99. $fields = empty($params['fields']) ? '*' : $params['fields'];
  100. $count = (int)$params['count'];
  101. if ($count <= 0 || $count > self::DEFAULT_HISTORY_MAX_COUNT)
  102. $count = self::DEFAULT_HISTORY_MAX_COUNT;
  103. $condition = array('and', 'state = :enabled', 'channel_id = :channelid', 'create_time < :beforetime');
  104. $param = array(':enabled'=>POST_STATE_ENABLED, ':channelid' => $channelID, ':beforetime'=>$beforeTime);
  105. $cmd = app()->getDb()->createCommand()
  106. ->select($fields)
  107. ->from(TABLE_POST . ' t')
  108. ->where($condition, $param)
  109. ->order('create_time desc, id desc')
  110. ->limit($count);
  111. $rows = $cmd->queryAll();
  112. foreach ($rows as $index => $row)
  113. $rows[$index] = self::formatRow($row);
  114. return $rows;
  115. }
  116. catch (Exception $e) {
  117. throw new ApiException('系统错误', ApiError::SYSTEM_ERROR, $params['debug']);
  118. }
  119. }
  120. public function latest()
  121. {
  122. self::requiredParams(array('channelid'));
  123. $params = $this->filterParams(array('channelid', 'count', 'fields', 'lasttime', 'token'));
  124. $channelID = (int)$params['channelid'];
  125. // @todo test data
  126. $version = $this->_params['version'];
  127. $rows = self::fetchTestRows($channelID, $version);
  128. if ($rows !== false) return $rows;
  129. try {
  130. $fields = empty($params['fields']) ? '*' : $params['fields'];
  131. $lasttime = empty($params['lasttime']) ? 0 : (int)$params['lasttime'];
  132. $count = (int)$params['count'];
  133. if ($count <= 0 || $count > self::DEFAULT_TIMELINE_MAX_COUNT)
  134. $count = self::DEFAULT_TIMELINE_MAX_COUNT;
  135. $condition = array('and', 'state = :enabled', 'channel_id = :channelid', 'create_time > :lasttime');
  136. $param = array(':enabled'=>POST_STATE_ENABLED, ':channelid' => $channelID, ':lasttime'=>$lasttime);
  137. $cmd = app()->getDb()->createCommand()
  138. ->select($fields)
  139. ->from(TABLE_POST . ' t')
  140. ->where($condition, $param)
  141. ->order('create_time desc, id desc')
  142. ->limit($count);
  143. $rows = $cmd->queryAll();
  144. foreach ($rows as $index => $row)
  145. $rows[$index] = self::formatRow($row);
  146. self::updateLastRequestTime($token);
  147. return $rows;
  148. }
  149. catch (Exception $e) {
  150. throw new ApiException('系统错误', ApiError::SYSTEM_ERROR, $params['debug']);
  151. }
  152. }
  153. public function random()
  154. {
  155. self::requiredParams(array('channelid'));
  156. $params = $this->filterParams(array('channelid', 'count', 'fields'));
  157. $channelID = (int)$params['channelid'];
  158. // @todo test data
  159. $version = $this->_params['version'];
  160. $rows = self::fetchTestRows($channelID, $version);
  161. if ($rows !== false) return $rows;
  162. try {
  163. $fields = empty($params['fields']) ? '*' : $params['fields'];
  164. $maxIdMinId = app()->getDb()->createCommand()
  165. ->select(array('max(id) maxid', 'min(id) minid'))
  166. ->from(TABLE_POST . ' t')
  167. ->where(array('and', 't.state = :enalbed', 'channel_id = :channelid'), array(':enalbed' => POST_STATE_ENABLED, ':channelid'=>$channelID))
  168. ->queryRow();
  169. $count = (int)$params['count'];
  170. if ($count <= 0 || $count > self::DEFAULT_RANDOM_MAX_COUNT)
  171. $count = self::DEFAULT_TIMELINE_MAX_COUNT;
  172. $minid = (int)$maxIdMinId['minid'];
  173. $maxid = (int)$maxIdMinId['maxid'];
  174. $conditoins = array('and', 't.state = :enalbed', 'channel_id = :channelid', 'id = :randid');
  175. $param = array(':enalbed' => POST_STATE_ENABLED, ':channelid'=>$channelID, ':randid'=>0);
  176. $rows = array();
  177. for ($i=0; $i<$maxid; $i++) {
  178. $randid = mt_rand($minid, $maxid);
  179. $param['randid'] = $randid;
  180. $cmd = app()->getDb()->createCommand()
  181. ->select($fields)
  182. ->from(TABLE_POST . ' t')
  183. ->where($conditoins, $param)
  184. ->limit(1);
  185. $row = $cmd->queryRow();
  186. if ($row === false || array_key_exists($row['id'], $rows))
  187. continue;
  188. else
  189. $rows[$row['id']] = $row;
  190. if (count($rows) >= $count)
  191. break;
  192. }
  193. $rows = self::formatRows($rows);
  194. $rows = array_values($rows);
  195. return $rows;
  196. }
  197. catch (Exception $e) {
  198. echo $e->getMessage();
  199. throw new ApiException('系统错误', ApiError::SYSTEM_ERROR, $params['debug']);
  200. }
  201. }
  202. public function support()
  203. {
  204. self::requirePost();
  205. $this->requiredParams(array('postid'));
  206. $params = $this->filterParams(array('postid'));
  207. try {
  208. $id = (int)$params['postid'];
  209. $counters = array('up_score'=>1);
  210. $result = Post::model()->updateCounters($counters, 'id=:pid', array(':pid'=>$id));
  211. $data = array('errno'=>0);
  212. }
  213. catch (Exception $e) {
  214. $data = array('errno'=>1);
  215. }
  216. return $data;
  217. }
  218. public function oppose()
  219. {
  220. self::requirePost();
  221. $this->requiredParams(array('postid'));
  222. $params = $this->filterParams(array('postid'));
  223. try {
  224. $id = (int)$params['postid'];
  225. $counters = array('down_score'=>1);
  226. $result = Post::model()->updateCounters($counters, 'id=:pid', array(':pid'=>$id));
  227. $data = array('errno'=>0);
  228. }
  229. catch (Exception $e) {
  230. $data = array('errno'=>1);
  231. }
  232. return $data;
  233. }
  234. public function create()
  235. {
  236. self::requirePost();
  237. // $this->requireLogin();
  238. $this->requiredParams(array('content', 'token', 'channel_id'));
  239. $params = $this->filterParams(array('content', 'tags', 'channel_id', 'category_id', 'pic', 'token'));
  240. $post = new Post('api');
  241. $post->channel_id = (int)$params['channel_id'];
  242. $post->content = $params['content'];
  243. $post->tags = $params['tags'];
  244. $post->create_time = $_SERVER['REQUEST_TIME'];
  245. $post->state = POST_STATE_DISABLED;
  246. $post->up_score = mt_rand(20, 100);
  247. $post->down_score = mt_rand(0, 15);
  248. $post->view_nums = mt_rand(100, 300);
  249. try {
  250. $thumbnailImageSize = array('width'=>150, 'height'=>150);
  251. $url = trim($params['pic']);
  252. if (!empty($url)) {
  253. $path = CDBase::makeUploadPath('pics');
  254. $info = parse_url($url);
  255. $extensionName = pathinfo($info['path'], PATHINFO_EXTENSION);
  256. $file = CDBase::makeUploadFileName('');
  257. $thumbnailFile = 'thubmnail_' . $file;
  258. $thumbnailFileName = $path['path'] . $thumbnailFile;
  259. $middleFileName = $path['path'] . 'bmiddle_' . $file;
  260. $bigFile = 'original_' . $file;
  261. $bigFileName = $path['path'] . $bigFile;
  262. $curl = new CdCurl();
  263. $curl->get($url);
  264. $data = $curl->rawdata();
  265. $curl->close();
  266. $im = new CdImage();
  267. $im->load($data);
  268. unset($data, $curl);
  269. $im->resizeToWidth($thumbnailImageSize['width'])
  270. ->crop($thumbnailImageSize['width'], $thumbnailImageSize['height'])
  271. ->saveAsJpeg($thumbnailFileName);
  272. $post->thumbnail_pic = fbu($path['url'] . $im->filename());
  273. $im->revert()->saveAsJpeg($middleFileName, 75);
  274. $post->bmiddle_pic = fbu($path['url'] . $im->filename());
  275. $im->revert()->saveAsJpeg($bigFileName, 100);
  276. $post->original_pic = fbu($path['url'] . $im->filename());
  277. }
  278. else
  279. $post->thumbnail_pic = $post->bmiddle_pic = $post->original_pic = '';
  280. }
  281. catch (CException $e) {
  282. var_dump($e);
  283. }
  284. try {
  285. return (int)$post->save();
  286. }
  287. catch (ApiException $e) {
  288. throw new ApiException('系统错误', ApiError::SYSTEM_ERROR);
  289. }
  290. }
  291. public function tofavorite()
  292. {
  293. self::requirePost();
  294. $this->requiredParams(array('user_id', 'token', 'post_id'));
  295. $params = $this->filterParams(array('user_id', 'token', 'post_id'));
  296. $userID = (int)$params['user_id'];
  297. $postID = (int)$params['post_id'];
  298. $cmd = app()->getDb()->createCommand()
  299. ->select('id')
  300. ->from(TABLE_POST_FAVORITE)
  301. ->where('user_id = :userid and post_id = :postid', array(':userid' => $userID, ':postid' => $postID));
  302. $row = $cmd->queryRow();
  303. if ($row === false) {
  304. $columns = array(
  305. 'user_id' => $userID,
  306. 'post_id' => $postID,
  307. );
  308. $result = app()->getDb()->createCommand()
  309. ->insert(TABLE_POST_FAVORITE, $columns);
  310. $errno = (int)($result == 0);
  311. }
  312. else
  313. $errno = -1;
  314. $data['errno'] = $errno;
  315. return $data;
  316. }
  317. public function favorite()
  318. {
  319. $count = 5;
  320. $this->requiredParams(array('userid', 'maxid'));
  321. $params = $this->filterParams(array('userid', 'email', 'token', 'fields', 'maxid'));
  322. $uid = (int)$params['userid'];
  323. $maxid = (int)$params['maxid'];
  324. if ($maxid > 0) {
  325. $cmd = app()->getDb()->createCommand()
  326. ->select('id')
  327. ->from(TABLE_POST_FAVORITE)
  328. ->where(array('and', 'user_id = :userid', 'post_id = :postid'), array(':userid' => $uid, ':postid' => $maxid));
  329. // echo $cmd->text;
  330. $rowID = $cmd->queryScalar();
  331. // var_dump($rowID);
  332. }
  333. $cmd = app()->getDb()->createCommand()
  334. ->select('post_id')
  335. ->from(TABLE_POST_FAVORITE)
  336. ->order('id desc')
  337. ->limit($count);
  338. if ($rowID)
  339. $cmd->where(array('and', 'user_id = :userid', 'id < :maxid'), array(':userid' => $uid, ':maxid' => $rowID));
  340. else
  341. $cmd->where('user_id = :userid', array(':userid' => $uid));
  342. $ids = $cmd->queryColumn();
  343. if (empty($ids)) return array();
  344. $fields = empty($params['fields']) ? '*' : $params['fields'];
  345. $conditions = array('and', array('in', 'id', $ids), 'state = :enabled');
  346. $conditionParams = array(':enabled' => POST_STATE_ENABLED);
  347. $cmd = app()->getDb()->createCommand()
  348. ->select($fields)
  349. ->from(TABLE_POST)
  350. ->limit($count)
  351. ->where($conditions, $conditionParams);
  352. $rows = $cmd->queryAll();
  353. $rows = self::formatRows($rows);
  354. return $rows;
  355. }
  356. private static function updateLastRequestTime($token)
  357. {
  358. if (empty($token))
  359. return false;
  360. $token = IOSDevice::convertToken($token);
  361. IOSDevice::model()->updateAll(array('last_time'=>$_SERVER['REQUEST_TIME']), 'device_token = :token', array(':token'=>$token));
  362. }
  363. private static function fetchTestRows($channelID, $version)
  364. {
  365. if (!APP_STORE_VERIFY || $channelID != CHANNEL_GIRL || $version < '2.2.1') {
  366. return false;
  367. }
  368. $ids = array(7207,7208,7209,8177,8178,8179,8180,8181,8182,8183,8184,8185,8186,8187,8188,8189,8190,8191,8192,8193,8194,8195,8196,8197,8198,8199,8200,8201,8202,8204,8205,8206,8207,8208,8209,8210,8211,8212,8213,8214,8215,8216,8217,8218,8219,8220,8221,8222,8223,8224,8225,8226,8227,8228,8229);
  369. shuffle($ids);
  370. $cmd = app()->getDb()->createCommand()
  371. ->from(TABLE_POST . ' t')
  372. ->where(array('in', 'id', $ids));
  373. $rows = $cmd->queryAll();
  374. foreach ($rows as $index => $row)
  375. $rows[$index] = self::formatRow($row);
  376. shuffle($rows);
  377. return $rows;
  378. }
  379. }