PageRenderTime 63ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/addons/plugin/Robot/hooks/RobotHooks.class.php

https://github.com/cxc222/weibo
PHP | 116 lines | 89 code | 4 blank | 23 comment | 10 complexity | ff5fd4b303996e46a9afc4e210e80bff MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class RobotHooks extends Hooks {
  3. // _parse_at_by_uname 根据用户昵称获取用户ID [格式化微博与格式化评论专用]
  4. public function weibo_publish_after($data) {
  5. /* print_r($GLOBALS['ts']['mid']);
  6. die(); */
  7. $config = model ( 'AddonData' )->lgetAddons ( 'robot' );
  8. if ($config ['config']) {
  9. $user = model ( 'User' )->getUserInfoByName ( $config['config']['uname'] );
  10. if(!$user){
  11. return true;
  12. }
  13. $uids = model ( 'Atme' )->getUids ( $data ['post'] ['content'] );
  14. if ($uids) {
  15. foreach ( $uids as $k => $v ) {
  16. if ($user ['uid'] == $v) {
  17. $this->_auto_weibo ( $data, $user );
  18. }
  19. }
  20. }
  21. }
  22. }
  23. public function _auto_weibo($post, $robot) {
  24. if ($reply = $this->_curl_auto ( $robot, $this->format ( $post ['post'] ['content'] ) )) {
  25. /*
  26. * $data['app_name'] = 'public'; $data['table_name'] = 'feed';
  27. * $data['app_uid'] = $robot['uid']; $data['row_id'] =
  28. * $post['weibo_id']; $data['app_row_id'] = 0; $data['comment_old']
  29. * = 0; $data['content'] = $reply; $data['app_row_table'] = 'feed';
  30. * $data['to_comment_id'] = 0; $data['ifShareFeed'] = 0;
  31. * $data['comment_old'] = 0; $data['app'] = 'public'; $data['table']
  32. * = 'feed'; model('Comment')->addComment($data);
  33. */
  34. $current_user = model ( 'User' )->getUserInfo($GLOBALS['ts']['mid']);
  35. $data = array ();
  36. $data ['uid'] = $robot ['uid'];
  37. $data ['app_name'] = 'public';
  38. $data ['comment'] = 0;
  39. $data ['body'] = $reply . '@'.$current_user['uname'];
  40. $data ['curid'] = $post ['weibo_id'];
  41. $data ['content'] = '';
  42. $data ['curtable'] = 'feed';
  43. $data ['sid'] = $post ['weibo_id'];
  44. $data ['type'] = 'feed';
  45. $return = model ( 'Share' )->shareFeed ( $data );
  46. if($return['status'] == 1) {
  47. $map['feed_id'] = $post ['weibo_id'];
  48. $map['is_del'] = 0;
  49. model('Credit')->setUserCredit($GLOBALS['ts']['mid'],'forward_weibo');
  50. //微博被转发
  51. $suid = model('Feed')->where($map)->getField('uid');
  52. model('Credit')->setUserCredit($suid,'forwarded_weibo');
  53. }
  54. }
  55. }
  56. public function _curl_auto($robot, $msg) {
  57. $config = model ( 'AddonData' )->lgetAddons ( 'robot' );
  58. header ( "content-type:text/html;charset=utf-8" );
  59. // $question = iconv("GB2312", "UTF-8//IGNORE", $_GET ['msg']);
  60. $url = $config['config']['address'];
  61. $app_key = $config['config']['app_key'];
  62. $app_secret = $config['config']['app_secret'];
  63. $userId = $robot ['uname']; // 现在应该可以乱填,这个是针对后面信息统计的。现在还不涉及到这块
  64. $nonce = "";
  65. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  66. for($i = 0; $i < 40; $i ++)
  67. $nonce .= $chars [mt_rand ( 0, strlen ( $chars ) - 1 )];
  68. $signature = sha1 ( sha1 ( $app_key . ":xiaoi.com:" . $app_secret ) . ":" . $nonce . ":" . sha1 ( "POST:/robot/ask.do" ) );
  69. $ch = curl_init ();
  70. curl_setopt ( $ch, CURLOPT_URL, $url );
  71. curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
  72. curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
  73. 'X-Auth: app_key="' . $app_key . '", nonce="' . $nonce . '", signature="' . $signature . '"'
  74. ) );
  75. curl_setopt ( $ch, CURLOPT_POST, 1 );
  76. curl_setopt ( $ch, CURLOPT_POSTFIELDS, "question=" . $msg . "&userId=" . $userId . "&type=0" );
  77. $output = curl_exec ( $ch );
  78. curl_close ( $ch );
  79. return $output;
  80. }
  81. public function format($html) {
  82. $html = h ( $html );
  83. // @提到某人处理
  84. $html = preg_replace ( "/@([\w\x{2e80}-\x{9fff}\-]+)/u", "", $html );
  85. return $html;
  86. }
  87. /**
  88. * 后台设置界面
  89. */
  90. public function config() {
  91. $config = model ( 'AddonData' )->lgetAddons ( 'robot' );
  92. if (!$config ['config']) {
  93. $config['config']['address'] = 'http://nlp.xiaoi.com/robot/ask.do';
  94. }
  95. $this->assign('config', $config['config']);
  96. $this->display ( 'config' );
  97. }
  98. /**
  99. * 保存后台配置数据
  100. *
  101. * @return void
  102. */
  103. public function saveConfig() {
  104. $temp = array ();
  105. foreach ( $_POST as $key => $value ) {
  106. $temp [$key] = h ( $value );
  107. }
  108. $data ['config'] = $temp;
  109. model ( 'AddonData' )->lputAddons ( 'robot', $data );
  110. }
  111. }
  112. ?>