示例#1
0
 public function send_message_to_third_party($account_id, $post_data)
 {
     if (!$account_id or !$post_data) {
         return false;
     }
     $rules = $this->get_third_party_api_by_account_id($account_id, true, 'rank ASC');
     if (!$rules) {
         return false;
     }
     $timestamp = time();
     $nonce = mt_rand(1000000000, 9999999999);
     foreach ($rules as $rule) {
         if (!$rule['url'] or !$rule['token']) {
             continue;
         }
         $signature = $this->model('weixin')->generate_signature($rule['token'], $timestamp, $nonce);
         if (!$signature) {
             continue;
         }
         $url = $rule['url'] . '?signature=' . $signature . '&timestamp=' . $timestamp . '&nonce=' . $nonce;
         $response = HTTP::request($url, 'POST', $post_data, 5);
         if ($response) {
             return $response;
         }
     }
     return false;
 }
示例#2
0
 public function douban()
 {
     if ($this->request->get("code")) {
         //已获得code
         $code = $this->request->get("code");
         $params = array('client_id' => DOUBAN_CLIENT_ID, 'client_secret' => DOUBAN_CLIENT_SECRET, 'redirect_uri' => $this->_siteUrl . DOUBAN_REDIRECT_URI, 'grant_type' => 'authorization_code', 'code' => $code);
         $result = json_decode(HTTP::request(DOUBAN_ACCESS_TOKEN_URL, $params, 'POST'));
         if (!isset($result->code)) {
             $set = array('douban_access_token' => $result->access_token, 'douban_refresh_token' => $result->refresh_token, 'douban_expires_in' => $result->expires_in, 'douban_last_time' => time() + $result->expires_in);
             Widget_Plugins_Edit::configPlugin("SyncPost", $set);
             $this->widget('Widget_Notice')->set(_t('豆瓣广播授权成功!'), 'success');
         } else {
             $this->widget('Widget_Notice')->set(_t('豆瓣广播授权失败,异常信息:' . $result->code . ' - ' . $result->msg), 'error');
         }
         header('Location: ' . $this->_siteUrl . __TYPECHO_ADMIN_DIR__ . 'options-plugin.php?config=SyncPost');
         //刷新页面
     } else {
         //获取授权code
         $params = array('client_id' => DOUBAN_CLIENT_ID, 'redirect_uri' => $this->_siteUrl . DOUBAN_REDIRECT_URI, 'response_type' => 'code');
         header('Location: ' . DOUBAN_AUTHORIZATION_CODE_URL . '?' . http_build_query($params));
         //刷新页面
     }
 }
示例#3
0
 public function upload_file($file, $type)
 {
     $app_id = get_setting('weixin_app_id');
     $app_secret = get_setting('weixin_app_secret');
     $file = realpath($file);
     if (!is_readable($file)) {
         return false;
     }
     $file_md5 = md5_file($file);
     $cached_result = AWS_APP::cache()->get('weixin_upload_file_' . $file_md5);
     if ($cached_result) {
         return $cached_result;
     }
     $post_data = array('media' => '@' . $file);
     $result = HTTP::request(self::WEIXIN_FILE_API . 'media/upload?access_token=' . $this->get_access_token($app_id, $app_secret) . '&type=' . $type, 'POST', $post_data);
     if (!$result) {
         return false;
     }
     $result = json_decode($result, true);
     if ($result['errcode']) {
         if ($result['errcode'] == 40001) {
             $this->refresh_access_token($app_id, $app_secret);
             return $this->upload_file($file, $type);
         }
     } else {
         AWS_APP::cache()->set('weixin_upload_file_' . $file_md5, $result, 259200);
     }
     return $result;
 }
示例#4
0
 public function create_comment($access_token, $id, $comment)
 {
     if (!$access_token) {
         return false;
     }
     $args = array('access_token' => $access_token, 'comment' => $comment, 'id' => $id);
     $result = HTTP::request(self::COMMENTS_CREATE_URL, 'POST', http_build_query($args));
     if (!$result) {
         return false;
     }
     return json_decode($result, true);
 }
示例#5
0
 public function refresh_access_token($uid)
 {
     $user_info = $this->get_facebook_user_by_uid($uid);
     if (!$user_info) {
         $this->error_msg = AWS_APP::lang()->_t('Facebook 账号未绑定');
         return false;
     }
     if (!$user_info['access_token']) {
         $this->error_msg = AWS_APP::lang()->_t('user access token 为空');
         return false;
     }
     $args = array('client_id' => get_setting('facebook_app_id'), 'client_secret' => get_setting('facebook_app_secret'), 'refresh_token' => htmlspecialchars_decode($user_info['refresh_token']), 'grant_type' => 'refresh_token');
     $result = HTTP::request(self::OAUTH2_TOKEN_URL, 'POST', $args);
     if (!$result) {
         $this->error_msg = AWS_APP::lang()->_t('更新 access token 时,与 Facebook 通信失败');
         return false;
     }
     $result = json_decode($result, true);
     if ($result['error']) {
         if (!$result['error_description']) {
             $result['error_description'] = $result['error'];
         }
         $this->error_msg = AWS_APP::lang()->_t('更新 access token 失败,错误为:%s', $result['error_description']);
         return false;
     }
     $this->update('users_facebook', array('access_token' => htmlspecialchars($result['access_token']), 'expires_time' => time() + intval($result['expires_in'])), 'id = ' . $user_info['id']);
     return true;
 }
示例#6
0
 public static function postRender($contents, $class)
 {
     //echo '<xmp>';
     //var_dump($contents);
     //exit();
     if ($class->request->is('do=publish')) {
         $opstions = Helper::options()->plugin('SyncPost');
         $syncpost = $class->request->get('syncpost');
         foreach ((array) $syncpost as $key => $val) {
             if ($val == 'syncpost-tqq') {
                 $params = array('oauth_consumer_key' => TQQ_CLIENT_ID, 'access_token' => $opstions->tqq_access_token, 'openid' => $opstions->tqq_openid, 'oauth_version' => '2.a', 'format' => 'json', 'content' => str_replace(array('%title%', '%permalink%'), array($contents['title'], $class->permalink), $opstions->postContent));
                 $result = HTTP::request(TQQ_API_URL, $params, 'POST');
             } else {
                 if ($val == 'syncpost-sina') {
                     $params = array('access_token' => $opstions->sina_access_token, 'status' => str_replace(array('%title%', '%permalink%'), array($contents['title'], $class->permalink), $opstions->postContent));
                     $result = HTTP::request(SINA_API_URL, $params, 'POST');
                 } else {
                     if ($val == 'syncpost-douban') {
                         $params = array('source' => $opstions->douban_access_token, 'text' => str_replace(array('%title%', '%permalink%'), array($contents['title'], $class->permalink), $opstions->postContent));
                         $result = HTTP::request(DOUBAN_API_URL, $params, 'POST', false, array('Authorization: Bearer ' . $opstions->douban_access_token));
                     }
                 }
             }
         }
     }
     return $contents;
 }
示例#7
0
 public function verify_credentials()
 {
     $args = array('oauth_token' => $this->access_token['oauth_token'], 'oauth_token_secret' => $this->access_token['oauth_token_secret']);
     $header = array($this->build_auth_header($args, self::OAUTH_AUTH_VALIDATION_URL, 'GET'));
     $result = HTTP::request(self::OAUTH_AUTH_VALIDATION_URL, 'GET', null, 10, $header);
     if (!$result) {
         $this->error_msg = AWS_APP::lang()->_t('获取个人资料时,与 Twitter 通信失败');
         return false;
     }
     $result = json_decode($result, true);
     if ($result['errors']) {
         $this->error_msg = AWS_APP::lang()->_t('获取个人资料失败,错误为:%s', $result['errors']['message']);
         return false;
     }
     $this->user_info = array('id' => $result['id'], 'name' => $result['name'], 'screen_name' => $result['screen_name'], 'location' => $result['location'], 'time_zone' => $result['time_zone'], 'lang' => $result['lang'], 'profile_image_url' => str_ireplace('_normal.', '_400x400.', $result['profile_image_url']), 'access_token' => array('oauth_token' => $this->access_token['oauth_token'], 'oauth_token_secret' => $this->access_token['oauth_token_secret']));
     return true;
 }