Skip to content

Commit

Permalink
优化权限获取
Browse files Browse the repository at this point in the history
  • Loading branch information
fooleap committed May 1, 2018
1 parent 28ea9dc commit bc11d14
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 78 deletions.
4 changes: 3 additions & 1 deletion api/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* 配置文件
*
* @author fooleap <fooleap@gmail.com>
* @version 2018-04-26 21:03:24
* @version 2018-04-29 12:48:07
* @link https://github.com/fooleap/disqus-php-api
*
*/
Expand All @@ -15,6 +15,7 @@
* DISQUS_PUBKEY Disqus 公钥,无需修改
* PUBLIC_KEY Disqus APP 公钥,在 https://disqus.com/api/applications/ 申请注册后获得
* SECRET_KEY Disqus APP 私钥,在 https://disqus.com/api/applications/ 申请注册后获得
* ACCESS_TOKEN Disqus 管理员 access_token,在 https://disqus.com/api/applications/ 申请注册后获得
* DISQUS_USERNAME Disqus 用户名
* DISQUS_EMAIL Disqus 注册邮箱,重要
* DISQUS_PASSWORD Disqus 密码,重要
Expand All @@ -29,6 +30,7 @@
define('DISQUS_PUBKEY', 'E8Uh5l5fHZ6gD8U3KycjAIAk46f68Zw7C6eW8WSjZvCLXebZ7p0r1yrYDrLilk2F');
define('PUBLIC_KEY', '');
define('SECRET_KEY', '');
define('ACCESS_TOKEN', '');
define('DISQUS_USERNAME', '');
define('DISQUS_EMAIL', '');
define('DISQUS_PASSWORD', '');
Expand Down
154 changes: 87 additions & 67 deletions api/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* 获取权限,简单封装常用函数
*
* @author fooleap <fooleap@gmail.com>
* @version 2018-04-29 12:00:27
* @version 2018-04-29 17:25:43
* @link https://github.com/fooleap/disqus-php-api
*
*/
Expand Down Expand Up @@ -38,64 +38,51 @@ function domain($url){
// 缓存文件
$data_path = sys_get_temp_dir().'/disqus_'.DISQUS_SHORTNAME.'.json';
$forum_data = json_decode(file_get_contents($data_path));
$session = $forum_data -> session -> data;

// 管理员登录
function adminLogin(){
global $session, $data_path, $forum_data;

$cookie_temp = sys_get_temp_dir().'/cookie_temp.txt';
$cookie = sys_get_temp_dir().'/cookie.txt';
global $data_path, $forum_data;

$ch = curl_init();
$fields = (object) array(
'username' => DISQUS_EMAIL,
'password' => DISQUS_PASSWORD
);

// 取得 csrftoken
$options = array(
CURLOPT_URL => 'https://disqus.com/profile/login/',
CURLOPT_HTTPHEADER => array('Host: disqus.com'),
CURLOPT_COOKIEJAR => $cookie_temp,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'https://import.disqus.com/login/',
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => http_build_query($fields)
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$errno = curl_errno($ch);

$curl = curl_init();
curl_setopt_array($curl, $options);
$result = curl_exec($curl);
$errno = curl_errno($curl);

if ($errno == 60 || $errno == 77) {
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cacert.pem');
$response = curl_exec($ch);
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cacert.pem');
$data = curl_exec($curl);
}

preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $response, $matches);
$token = str_replace("Set-Cookie: csrftoken=", "", $matches[0][0]);
curl_close($curl);
preg_match('/^Set-Cookie:\s+(session.*)/mi', $result, $matches);
$cookieArr = explode('; ',$matches[1]);
$cookie = (object) array();

// 登录并取得 session
$params = array(
'csrfmiddlewaretoken' => $token,
'username' => DISQUS_EMAIL,
'password' => DISQUS_PASSWORD
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_REFERER, 'https://disqus.com/profile/login/');
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_temp);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
$result = curl_exec($ch);
preg_match('/^Set-Cookie:\s+(session.*)/mi', $result, $output_match);
preg_match('/(session[^;]*)/mi', $output_match[1], $session_match);
preg_match('/expires=([^;]*)/mi', $output_match[1], $expires_match);
$session = $session_match[0];
$expires = strtotime($expires_match[1]);
foreach( $cookieArr as $value){

if( strpos($value,'=') !== false){
list($key, $val) = explode('=', $value);
$cookie -> $key = $val;
}

curl_close($ch);
if( strpos($session, 'session') !== false ){
// 写入文件
$forum_data -> session = array(
'data' => $session,
'expires' => $expires
);
$forum_data -> passwd = md5(DISQUS_PASSWORD);
file_put_contents($data_path, json_encode($forum_data));
}

$forum_data -> cookie = $cookie;

file_put_contents($data_path, json_encode($forum_data));
}

// 鉴权
Expand Down Expand Up @@ -162,45 +149,65 @@ function fields_format($fields){
}

function curl_get($url, $fields){
global $session;

$fields -> api_key = DISQUS_PUBKEY;
global $forum_data;

if( defined(ACCESS_TOKEN) ){

$fields -> api_secret = SECRET_KEY;
$fields -> access_token = ACCESS_TOKEN;

} else {

$fields -> api_key = DISQUS_PUBKEY;
$cookies = 'sessionid='.$forum_data -> cookie -> sessionid;

}

$fields_string = fields_format($fields);

$curl_url = 'https://disqus.com'.$url.$fields_string;

$options = array(
CURLOPT_URL => $curl_url,
CURLOPT_HTTPHEADER => array('Host: disqus.com','Origin: https://disqus.com'),
CURLOPT_REFERER => 'https://disqus.com',
CURLOPT_COOKIE => $session,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1
);

$curl = curl_init();
curl_setopt_array($curl, $options);

if( isset($cookies)){
curl_setopt($curl, CURLOPT_COOKIE, $cookies);
}

$data = curl_exec($curl);
$errno = curl_errno($curl);
if ($errno == 60 || $errno == 77) {
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cacert.pem');
$data = curl_exec($curl);
}
curl_close($curl);

return json_decode($data);
}

function curl_post($url, $fields){
global $session, $access_token;

if( isset($access_token) && strpos($url, 'media') === false ){
global $access_token, $forum_data;

if( isset($access_token) && strpos($url, 'threads/create') === false && strpos($url, 'media') === false ){

$fields -> api_secret = SECRET_KEY;
$fields -> access_token = $access_token;

} else {

$fields -> api_key = DISQUS_PUBKEY;

$cookies = 'sessionid='.$forum_data -> cookie -> sessionid;
}

if( strpos($url, 'media') !== false ){
Expand All @@ -218,6 +225,7 @@ function curl_post($url, $fields){
$fields_string = fields_format($fields);
}

$curl = curl_init();
$options = array(
CURLOPT_URL => $curl_url,
CURLOPT_HTTPHEADER => array('Host: '.$curl_host,'Origin: https://disqus.com'),
Expand All @@ -228,18 +236,21 @@ function curl_post($url, $fields){
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $fields_string
);
if( !isset($access_token) || strpos($url, 'media') !== false ){
$options[CURLOPT_COOKIE] = $session;
}
$curl = curl_init();

curl_setopt_array($curl, $options);

if( isset($cookies)){
curl_setopt($curl, CURLOPT_COOKIE, $cookies);
}

$data = curl_exec($curl);
$errno = curl_errno($curl);
if ($errno == 60 || $errno == 77) {
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cacert.pem');
$data = curl_exec($curl);
}
curl_close($curl);

return json_decode($data);
}

Expand Down Expand Up @@ -323,10 +334,12 @@ function post_format( $post ){

function getUserData(){
global $access_token;

$fields_data = array(
'api_secret' => SECRET_KEY,
'access_token' => $access_token
);

$url = 'https://disqus.com/api/3.0/users/details.json?'.http_build_query($fields_data);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
Expand All @@ -349,7 +362,9 @@ function getUserData(){
}

function getForumData(){

global $data_path, $forum_data;

$fields = (object) array(
'forum' => DISQUS_SHORTNAME
);
Expand All @@ -369,19 +384,32 @@ function getForumData(){
}
}

// 取得当前目录
function getCurrentDir (){

$isSecure = false;
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$isSecure = true;
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
$isSecure = true;
}

$protocol = $isSecure ? 'https://' : 'http://';

return $protocol.$_SERVER['HTTP_HOST'].substr(__DIR__, strlen($_SERVER['DOCUMENT_ROOT']));

}

if( time() > strtotime($forum_data -> cookie -> expires) || !$forum_data -> cookie){
adminLogin();
}

if( time() > $forum_data -> forum -> expires || !$forum_data -> forum){
getForumData();
}

$user_id = $_COOKIE['user_id'];

if ( isset($user_id) ){

// 取用户授权数据,可能为空
Expand Down Expand Up @@ -409,11 +437,3 @@ function getCurrentDir (){
}
}
}

if( time() > $forum_data -> session -> expires || md5(DISQUS_PASSWORD) != $forum_data -> passwd ){
//adminLogin();
}

if( time() > $forum_data -> forum -> expires || !$forum_data -> forum){
getForumData();
}
8 changes: 4 additions & 4 deletions api/postcomment.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @param url 访客网址,可为空
*
* @author fooleap <fooleap@gmail.com>
* @version 2018-04-29 11:59:39
* @version 2018-05-01 11:29:13
* @link https://github.com/fooleap/disqus-php-api
*
*/
Expand Down Expand Up @@ -55,9 +55,10 @@
'message' => $post_message,
'author_name' => $author_name,
'author_email' => $author_email,
'author_url' => $author_url,
'author_url' => $author_url
);
if( !!$session ){

if(!!$forum_data -> cookie){
$post_data -> state = $approved;
}
}
Expand All @@ -77,7 +78,6 @@
'id'=> $data -> response -> id,
'link'=> $_POST['link'],
'title'=> $_POST['title'],
'session'=> $session
);
$mail = curl_init();
$curl_opt = array(
Expand Down
6 changes: 1 addition & 5 deletions api/sendemail.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@
* @param id 该评论 ID
*
* @author fooleap <fooleap@gmail.com>
* @version 2018-04-26 17:17:32
* @version 2018-04-29 13:13:19
* @link https://github.com/fooleap/disqus-php-api
*
*/
namespace Emojione;
date_default_timezone_set("Asia/Shanghai");
require_once('init.php');

if( $_POST['session'] != $session ){
exit('session 有误!');
}

// 获取被回复人信息
$fields = (object) array(
'post' => $_POST['parent']
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Disqus PHP API

### 重要

必须在 [Disqus API](https://disqus.com/api/applications/) 申请注册一个 App,取得相关的公钥(API Key)和私钥(API Secret,并填写于后端的配置文件 `config.php` 中。
必须在 [Disqus API](https://disqus.com/api/applications/) 申请注册一个 App,取得相关的公钥(**API Key**)、私钥(**API Secret**)以及管理员**access_token**,并填写于后端的配置文件 `config.php` 中。

App 设置方面,回调链接请填写 `login.php` 文件的绝对地址,主要的设置如下图,可根据自己情况填写。

Expand Down

0 comments on commit bc11d14

Please sign in to comment.