public static function initComment($api) { session_start(); $options = Typecho_Widget::widget('Widget_Options'); $config = $options->plugin('ConnectToTwitter'); //发送请求到twitter if (isset($api->request->connect_to_twitter)) { $to = new TwitterOAuth($config->consumerKey, $config->consumerSecret); $tok = $to->getRequestToken(); Typecho_Cookie::set('oauth_request_token', $tok['oauth_token']); Typecho_Cookie::set('oauth_request_token_secret', $tok['oauth_token_secret']); /* Build the authorization URL */ $request_link = $to->getAuthorizeURL($tok['oauth_token']); header('Location:' . $request_link); } //从twitter返回 if (isset($api->request->oauth_token)) { if (Typecho_Cookie::get('oauth_request_token') && Typecho_Cookie::get('oauth_request_token_secret')) { $to = new TwitterOAuth($config->consumerKey, $config->consumerSecret, Typecho_Cookie::get('oauth_request_token'), Typecho_Cookie::get('oauth_request_token_secret')); $tok = $to->getAccessToken(); Typecho_Cookie::set('oauth_access_token', $tok['oauth_token'], time() + 60 * 60 * 24 * 30); Typecho_Cookie::set('oauth_access_token_secret', $tok['oauth_token_secret'], time() + 60 * 60 * 24 * 30); $info_json = $to->OAuthRequest('https://twitter.com/account/verify_credentials.json', array(), 'GET'); $info = Typecho_Json::decode($info_json, true); self::twitterLogin($info, $api); } } }
public function action() { /** 验证合法性 */ if (!isset($_SERVER['HTTP_GOOGLE_CODE_PROJECT_HOSTING_HOOK_HMAC'])) { return; } $googleSecretInfo = $_SERVER['HTTP_GOOGLE_CODE_PROJECT_HOSTING_HOOK_HMAC']; $revisionData = file_get_contents('php://input'); if (empty($revisionData)) { return; } $secretVerify = hash_hmac("md5", $revisionData, Helper::options()->plugin('GoogleCodeSVN')->secretKey); if ($googleSecretInfo != $secretVerify) { return; } $data = Typecho_Json::decode($revisionData); if (!$data) { return; } /** 登录用户 */ $master = $this->db->fetchRow($this->db->select()->from('table.users')->where('group = ?', 'administrator')->order('uid', Typecho_Db::SORT_ASC)->limit(1)); if (empty($master)) { return false; } else { if (!$this->user->simpleLogin($master['uid'])) { return false; } } if (isset($data->revisions) && is_array($data->revisions)) { foreach ($data->revisions as $revision) { if (!empty($revision->added)) { foreach ($revision->added as $file) { $input = $this->parseFileName($file, $data->repository_path); if ($input) { $this->widget('Widget_Contents_Post_Edit', NULL, $input, false)->action(); } } } if (!empty($revision->modified)) { foreach ($revision->modified as $file) { $input = $this->parseFileName($file, $data->repository_path); if ($input) { $this->widget('Widget_Contents_Post_Edit', NULL, $input, false)->action(); } } } if (!empty($revision->removed)) { foreach ($revision->removed as $file) { $input = $this->parseFileName($file, $data->repository_path); if ($input && isset($input['cid'])) { $postId = $input['cid']; $this->widget('Widget_Contents_Post_Edit', NULL, "cid={$postId}", false)->deletePost(); } } } } } }
/** * 远程请求代理 * * @access public * @return void */ public function feed() { $this->user->pass('subscriber'); $client = Typecho_Http_Client::get(); if ($client) { $client->setHeader('User-Agent', $this->options->generator)->send('http://typecho.org/feed/'); /** 匹配内容体 */ $response = $client->getResponseBody(); preg_match_all("/<item>\\s*<title>([^>]*)<\\/title>\\s*<link>([^>]*)<\\/link>\\s*<guid>[^>]*<\\/guid>\\s*<pubDate>([^>]*)<\\/pubDate>/is", $response, $matches); $data = array(); if ($matches) { foreach ($matches[0] as $key => $val) { $data[] = array('title' => $matches[1][$key], 'link' => $matches[2][$key], 'date' => Typecho_I18n::dateWord(strtotime($matches[3][$key]), $this->options->gmtTime + $this->options->timezone)); if ($key > 3) { break; } } } if (!empty($data)) { Typecho_Cookie::set('__typecho_feed', Typecho_Json::encode($data)); } $this->response->throwJson($data); return; } throw new Typecho_Widget_Exception(_t('禁止访问'), 403); }
$feed = Typecho_Cookie::get('__typecho_feed'); ?> <div id="typecho-message" class="intro-link"> <ul> <?php if (empty($feed)) { ?> <li><?php _e('读取中...'); ?> </li> <?php } else { ?> <?php $feed = Typecho_Json::decode($feed); foreach ($feed as $item) { ?> <?php $item = (array) $item; ?> <li><a href="<?php echo $item['link']; ?> "><?php echo $item['title']; ?> </a> - <span class="date"><?php echo $item['date']; ?> </span></li>
/** * 抛出json回执信息 * * @access public * @param string $message 消息体 * @return void */ public function throwJson($message) { /** 设置http头信息 */ $this->setContentType('application/json'); /** Typecho_Json */ require_once 'Typecho/Json.php'; echo Typecho_Json::encode($message); /** 终止后续输出 */ exit; }