/addons/plugin/Robot/hooks/RobotHooks.class.php
PHP | 116 lines | 89 code | 4 blank | 23 comment | 10 complexity | ff5fd4b303996e46a9afc4e210e80bff MD5 | raw file
Possible License(s): LGPL-2.1
- <?php
- class RobotHooks extends Hooks {
- // _parse_at_by_uname 根据用户昵称获取用户ID [格式化微博与格式化评论专用]
- public function weibo_publish_after($data) {
-
- /* print_r($GLOBALS['ts']['mid']);
- die(); */
- $config = model ( 'AddonData' )->lgetAddons ( 'robot' );
- if ($config ['config']) {
- $user = model ( 'User' )->getUserInfoByName ( $config['config']['uname'] );
- if(!$user){
- return true;
- }
- $uids = model ( 'Atme' )->getUids ( $data ['post'] ['content'] );
- if ($uids) {
- foreach ( $uids as $k => $v ) {
- if ($user ['uid'] == $v) {
- $this->_auto_weibo ( $data, $user );
- }
- }
- }
- }
- }
- public function _auto_weibo($post, $robot) {
- if ($reply = $this->_curl_auto ( $robot, $this->format ( $post ['post'] ['content'] ) )) {
- /*
- * $data['app_name'] = 'public'; $data['table_name'] = 'feed';
- * $data['app_uid'] = $robot['uid']; $data['row_id'] =
- * $post['weibo_id']; $data['app_row_id'] = 0; $data['comment_old']
- * = 0; $data['content'] = $reply; $data['app_row_table'] = 'feed';
- * $data['to_comment_id'] = 0; $data['ifShareFeed'] = 0;
- * $data['comment_old'] = 0; $data['app'] = 'public'; $data['table']
- * = 'feed'; model('Comment')->addComment($data);
- */
- $current_user = model ( 'User' )->getUserInfo($GLOBALS['ts']['mid']);
- $data = array ();
- $data ['uid'] = $robot ['uid'];
-
- $data ['app_name'] = 'public';
- $data ['comment'] = 0;
- $data ['body'] = $reply . '@'.$current_user['uname'];
- $data ['curid'] = $post ['weibo_id'];
- $data ['content'] = '';
- $data ['curtable'] = 'feed';
- $data ['sid'] = $post ['weibo_id'];
- $data ['type'] = 'feed';
-
- $return = model ( 'Share' )->shareFeed ( $data );
- if($return['status'] == 1) {
- $map['feed_id'] = $post ['weibo_id'];
- $map['is_del'] = 0;
- model('Credit')->setUserCredit($GLOBALS['ts']['mid'],'forward_weibo');
- //微博被转发
- $suid = model('Feed')->where($map)->getField('uid');
- model('Credit')->setUserCredit($suid,'forwarded_weibo');
- }
- }
- }
- public function _curl_auto($robot, $msg) {
- $config = model ( 'AddonData' )->lgetAddons ( 'robot' );
- header ( "content-type:text/html;charset=utf-8" );
- // $question = iconv("GB2312", "UTF-8//IGNORE", $_GET ['msg']);
- $url = $config['config']['address'];
- $app_key = $config['config']['app_key'];
- $app_secret = $config['config']['app_secret'];
- $userId = $robot ['uname']; // 现在应该可以乱填,这个是针对后面信息统计的。现在还不涉及到这块
- $nonce = "";
- $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
- for($i = 0; $i < 40; $i ++)
- $nonce .= $chars [mt_rand ( 0, strlen ( $chars ) - 1 )];
- $signature = sha1 ( sha1 ( $app_key . ":xiaoi.com:" . $app_secret ) . ":" . $nonce . ":" . sha1 ( "POST:/robot/ask.do" ) );
- $ch = curl_init ();
- curl_setopt ( $ch, CURLOPT_URL, $url );
- curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
- 'X-Auth: app_key="' . $app_key . '", nonce="' . $nonce . '", signature="' . $signature . '"'
- ) );
- curl_setopt ( $ch, CURLOPT_POST, 1 );
- curl_setopt ( $ch, CURLOPT_POSTFIELDS, "question=" . $msg . "&userId=" . $userId . "&type=0" );
- $output = curl_exec ( $ch );
- curl_close ( $ch );
- return $output;
- }
- public function format($html) {
- $html = h ( $html );
- // @提到某人处理
- $html = preg_replace ( "/@([\w\x{2e80}-\x{9fff}\-]+)/u", "", $html );
- return $html;
- }
- /**
- * 后台设置界面
- */
- public function config() {
- $config = model ( 'AddonData' )->lgetAddons ( 'robot' );
- if (!$config ['config']) {
- $config['config']['address'] = 'http://nlp.xiaoi.com/robot/ask.do';
- }
- $this->assign('config', $config['config']);
- $this->display ( 'config' );
- }
-
- /**
- * 保存后台配置数据
- *
- * @return void
- */
- public function saveConfig() {
- $temp = array ();
- foreach ( $_POST as $key => $value ) {
- $temp [$key] = h ( $value );
- }
- $data ['config'] = $temp;
- model ( 'AddonData' )->lputAddons ( 'robot', $data );
- }
- }
- ?>