diff --git a/DoubanAuth.php b/DoubanAuth.php index 7c74a51..a6137e5 100644 --- a/DoubanAuth.php +++ b/DoubanAuth.php @@ -9,7 +9,8 @@ * Douban OAuth * @author light */ -class DoubanAuth extends OAuth2 implements IAuth { +class DoubanAuth extends OAuth2 implements IAuth +{ /** * @inheritdoc @@ -31,25 +32,35 @@ class DoubanAuth extends OAuth2 implements IAuth { */ public $scope = 'douban_basic_common'; + protected function initUserAttributes() + { + //nothing + return $this->api('v2/user/~me', 'GET'); + } + /** * Get authed user info * * @return array * @see http://developers.douban.com/wiki/?title=user_v2#User */ - public function getUserInfo() { - return $this->api('v2/user/~me', 'GET'); + public function getUserInfo() + { + return $this->getUserAttributes(); } - protected function defaultName() { + protected function defaultName() + { return 'douban'; } - protected function defaultTitle() { + protected function defaultTitle() + { return 'Douban'; } - protected function defaultViewOptions() { + protected function defaultViewOptions() + { return [ 'popupWidth' => 800, 'popupHeight' => 500, @@ -60,7 +71,8 @@ protected function defaultViewOptions() { * * @ineritdoc */ - public function api($apiSubUrl, $method = 'GET', array $params = [], array $headers = []) { + public function api($apiSubUrl, $method = 'GET', array $params = [], array $headers = []) + { if (preg_match('/^https?:\\/\\//is', $apiSubUrl)) { $url = $apiSubUrl; } else { diff --git a/QqAuth.php b/QqAuth.php index 2398d53..5c0fd94 100644 --- a/QqAuth.php +++ b/QqAuth.php @@ -8,13 +8,15 @@ * QQ OAuth * @author xjflyttp */ -class QqAuth extends OAuth2 implements IAuth { +class QqAuth extends OAuth2 implements IAuth +{ public $authUrl = 'https://graph.qq.com/oauth2.0/authorize'; public $tokenUrl = 'https://graph.qq.com/oauth2.0/token'; public $apiBaseUrl = 'https://graph.qq.com'; - public function init() { + public function init() + { parent::init(); if ($this->scope === null) { $this->scope = implode(',', [ @@ -23,7 +25,8 @@ public function init() { } } - protected function initUserAttributes() { + protected function initUserAttributes() + { return $this->api('oauth2.0/me', 'GET'); } @@ -32,7 +35,8 @@ protected function initUserAttributes() { * @return [] * @see http://wiki.connect.qq.com/get_user_info */ - public function getUserInfo() { + public function getUserInfo() + { $openid = $this->getUserAttributes(); return $this->api("user/get_user_info", 'GET', [ 'oauth_consumer_key' => $openid['client_id'], @@ -40,15 +44,18 @@ public function getUserInfo() { ]]); } - protected function defaultName() { + protected function defaultName() + { return 'QQ'; } - protected function defaultTitle() { + protected function defaultTitle() + { return 'QQ'; } - protected function defaultViewOptions() { + protected function defaultViewOptions() + { return [ 'popupWidth' => 800, 'popupHeight' => 500, @@ -62,7 +69,8 @@ protected function defaultViewOptions() { * @throws Exception on failure. * @return array actual response. */ - protected function processResponse($rawResponse, $contentType = self::CONTENT_TYPE_AUTO) { + protected function processResponse($rawResponse, $contentType = self::CONTENT_TYPE_AUTO) + { if ($contentType == self::CONTENT_TYPE_AUTO) { //jsonp to json if (strpos($rawResponse, "callback") === 0) { diff --git a/QqExmailAuth.php b/QqExmailAuth.php new file mode 100644 index 0000000..1ae18dd --- /dev/null +++ b/QqExmailAuth.php @@ -0,0 +1,400 @@ + + * @see PDF + * @see http://exmail.qq.com/cgi-bin/download?path=bizopenapidoc&filename=%cc%da%d1%b6%c6%f3%d2%b5%d3%ca%cf%e4OpenApi%d0%ad%d2%e9v1.4.pdf + */ +class QqExmailAuth extends OAuth2 +{ + + //Action + const ACTION_DEL = 1; + const ACTION_ADD = 2; + const ACTION_MOD = 3; + //Gender + const GENDER_MALE = 1; + const GENDER_FEMALE = 2; + //OpenType + const OPEN_TYPE_UNSET = 0; + const OPEN_TYPE_ENABLE = 1; + const OPEN_TYPE_DISABLE = 2; + //Md5 + const MD5_PLAINTEXT = 0; + const MD5_ENCYPT = 1; + //EmailAvailable + const ACCOUNT_TYPE_UNAVAILABLE = -1; //帐号名无效 + const ACCOUNT_TYPE_AVAILABLE = 0; //帐号名没被占用 + const ACCOUNT_TYPE_MAIN = 1; //主帐号名 + const ACCOUNT_TYPE_ALIAS = 2; //别名账户 + const ACCOUNT_TYPE_MAILGROUP = 3; //群组账户 + //GroupStatus + const GROUP_STATUS_ALL = 'all'; + const GROUP_STATUS_INNER = 'inner'; + const GROUP_STATUS_GROUP = 'group'; + const GROUP_STATUS_LIST = 'list'; + + public $authUrl = ''; + public $tokenUrl = 'https://exmail.qq.com/cgi-bin/token'; + public $apiBaseUrl = 'http://openapi.exmail.qq.com:12211'; + public $templateOneKeyLoginUrl = 'https://exmail.qq.com/cgi-bin/login?fun=bizopenssologin&method=bizauth&agent=&user=&ticket='; + + protected function initUserAttributes() + { + return []; + } + + /** + * Fetches access token + * @param string $authCode ignore in this time + * @param array $params additional request params. + * @return OAuthToken access token. + * @throws InvalidResponseException + */ + public function fetchAccessToken($authCode = null, array $params = []) + { + $defaultParams = [ + 'grant_type' => 'client_credentials', + 'client_id' => $this->clientId, + 'client_secret' => $this->clientSecret, + ]; + + $response = $this->sendRequest('POST', $this->tokenUrl, array_merge($defaultParams, $params)); + $token = $this->createToken(['params' => $response]); + $this->setAccessToken($token); + + return $token; + } + + /** + * @return OAuthToken auth token instance. + * @throws QqExmailException + */ + public function getAccessToken() + { + $accessToken = parent::getAccessToken(); + if (null === $accessToken) { + $accessToken = $this->fetchAccessToken(); + } + if (null === $accessToken) { + throw new QqExmailException('getAccessToken Fail.'); + } + return $accessToken; + } + + /** + * + * @param string $email MemberEmail + * @return string Member Auth Key + * @throws + */ + public function getMemberAuthKey($email) + { + $result = $this->api('openapi/mail/authkey', 'GET', ['alias' => $email]); + + if (false === isset($result['auth_key'])) { + throw new QqExmailException('get Auth Key Fail'); + } + + return $result['auth_key']; + } + + /** + * + * @param string $email login EMAIL + * @param string $ticket getAuthKey() + * @return string Login Web Url + * @throws QqExmailException + */ + public function getOneKeyLogin($email, $ticket = null) + { + $urlTemplate = $this->templateOneKeyLoginUrl; + $agent = $this->clientId; + if (null === $ticket) { + $ticket = $this->getMemberAuthKey($email); + } + $requestUrl = str_replace([ + '', '', '' + ], [ + $agent, $email, $ticket + ], $urlTemplate); + return $requestUrl; + } + + /** + * + * @param string $email MemberEmail + * @return string MemberInfo + * @throws QqExmailException + */ + public function getMemberInfo($email) + { + $result = $this->api('openapi/user/get', 'POST', ['alias' => $email]); + if (isset($result['errcode']) || isset($result['error'])) { + throw new QqExmailException($result['error'], $result['errcode']); + } + return $result; + } + + /** + * Add Mod Del Member + * @param string $email + * @param [] $options + * @return [] + * @throws QqExmailException + */ + public function syncMember($email, $options) + { + $options['alias'] = $email; + $result = $this->api('openapi/user/sync', 'POST', $options); + if (isset($result['errcode']) || isset($result['error'])) { + throw new QqExmailException($result['error'], $result['errcode']); + } + return true; + } + + /** + * + * @param string $email + * @return boolean + */ + public function delMember($email) + { + try { + $this->syncMember($email, ['action' => self::ACTION_DEL]); + } catch (QqExmailException $ex) { + return false; + } + return true; + } + + /** + * Add Mod Del Patry + * @param [] $options Del/Add only DstPath , Mod need SrcPath & DstPath + * @return [] + * @throws QqExmailException + */ + public function syncParty($options) + { + $result = $this->api('openapi/party/sync', 'POST', $options); + if (isset($result['errcode']) || isset($result['error'])) { + throw new QqExmailException($result['error'], $result['errcode']); + } + return true; + } + + /** + * + * @param string $partyPath + * @return [] + * @throws QqExmailException + */ + public function getPartList($partyPath) + { + $options = [ + 'partypath' => $partyPath, + ]; + $result = $this->api('openapi/party/list', 'POST', $options); + if (isset($result['errcode']) || isset($result['error'])) { + throw new QqExmailException($result['error'], $result['errcode']); + } + return $result; + } + + /** + * + * @param string $partyPath + * @return [] + * @throws QqExmailException + */ + public function getMemberListByPartyPath($partyPath) + { + $options = [ + 'partypath' => $partyPath, + ]; + $result = $this->api('openapi/partyuser/list', 'POST', $options); + if (isset($result['errcode']) || isset($result['error'])) { + throw new QqExmailException($result['error'], $result['errcode']); + } + return $result; + } + + /** + * + * @param string $email + * @return bool + * @throws QqExmailException + */ + public function getMemberStatus($email) + { + $options = [ + 'email' => $email, + ]; + $result = $this->api('openapi/user/check', 'POST', $options); + if (isset($result['errcode']) || isset($result['error']) || !isset($result['List'])) { + throw new QqExmailException($result['error'], $result['errcode']); + } + return intval($result['List'][0]['Type']); + } + + /** + * + * @param string $email + * @return bool + */ + public function getMemberStatusAvailable($email) + { + $type = $this->getMemberStatus($email); + return $type === self::ACCOUNT_TYPE_AVAILABLE; + } + + /** + * + * @param int $ver 0=all + * @return [] + * @throws QqExmailException + */ + public function getMemberListByVersion($ver) + { + $options = [ + 'Ver' => $ver, + ]; + $result = $this->api('openapi/user/list', 'POST', $options); + if (isset($result['errcode']) || isset($result['error'])) { + throw new QqExmailException($result['error'], $result['errcode']); + } + return $result; + } + + /** + * + * @param string $email + * @return int + * @throws QqExmailException + */ + public function getMailNewCount($email) + { + $options = [ + 'alias' => $email, + ]; + $result = $this->api('openapi/mail/newcount', 'POST', $options); + if (isset($result['errcode']) || isset($result['error']) || !isset($result['NewCount'])) { + throw new QqExmailException($result['error'], $result['errcode']); + } + return intval($result['NewCount']); + } + + /** + * + * @param string $groupName 组名 + * @param string $groupAdmin 组管理员(需要使用一个域中不存在的邮箱地址) + * @param string $status 组状态 + * @param string $members 成员列表 + * @return bool + * @throws QqExmailException + */ + public function addGroup($groupName, $groupAdmin, $status, $members) + { + $options = [ + 'group_name' => $groupName, + 'group_admin' => $groupAdmin, + 'status' => $status, + 'members' => $members, + ]; + $result = $this->api('openapi/group/add', 'POST', $options); + if (isset($result['errcode']) || isset($result['error'])) { + throw new QqExmailException($result['error'], $result['errcode']); + } + return true; + } + + /** + * + * @param string $groupAlias AdminEmail + * @return boolean + * @throws QqExmailException + */ + public function delGroup($groupAlias) + { + $options = [ + 'group_alias' => $groupAlias, + ]; + $result = $this->api('openapi/group/delete', 'POST', $options); + if (isset($result['errcode']) || isset($result['error'])) { + throw new QqExmailException($result['error'], $result['errcode']); + } + return true; + } + + /** + * + * @param string $groupAlias AdminEmail + * @param string $members MemberEmail + * @return boolean + * @throws QqExmailException + */ + public function addGroupMember($groupAlias, $members) + { + $options = [ + 'group_alias' => $groupAlias, + 'members' => $members, + ]; + $result = $this->api('openapi/group/addmember', 'POST', $options); + if (isset($result['errcode']) || isset($result['error'])) { + throw new QqExmailException($result['error'], $result['errcode']); + } + var_dump($result); + return true; + } + + /** + * + * @param string $groupAlias + * @param string $members + * @return boolean + * @throws QqExmailException + */ + public function deleteGroupMember($groupAlias, $members) + { + $options = [ + 'group_alias' => $groupAlias, + 'members' => $members, + ]; + $result = $this->api('openapi/group/deletemember', 'POST', $options); + if (isset($result['errcode']) || isset($result['error'])) { + throw new QqExmailException($result['error'], $result['errcode']); + } + var_dump($result); + return true; + } + + /** + * + * @param int $ver + * @return [] + * @throws QqExmailException + * @see PDF + */ + public function listen($ver) + { + $options = [ + 'Ver' => $ver, + ]; + $result = $this->api('openapi/listen', 'POST', $options); + if (isset($result['errcode']) || isset($result['error'])) { + throw new QqExmailException($result['error'], $result['errcode']); + } + return $result; + } + +} diff --git a/README.QQExmail b/README.QQExmail new file mode 100644 index 0000000..40daa3a --- /dev/null +++ b/README.QQExmail @@ -0,0 +1,106 @@ +QQ Exmail +=== + +Configure +--- +```php +'components' => [ + 'authClientCollection' => [ + 'class' => 'yii\authclient\Collection', + 'clients' => [ + 'qqexmail' => [ + 'class' => 'xj\oauth\QqExmailAuth', + 'clientId' => '', //enter your id + 'clientSecret' => '', //enter your key + ], + ] + ], +] +``` + +ExampleCode +--- +```php +$testEmail = 'test1@domain.com'; +$testAddEmail = 'test2@domain.com'; +$testAddEmailName = 'testAddName'; +$testGroupName = 'testGroupName'; +$testGroupAdmin = 'testGroupAdmin_MustBeUnused@nfcmag.com'; + +$authClientCollection = Yii::$app->authClientCollection; +/* @var $authClientCollection \yii\authclient\Collection */ +$exmailAuthClient = $authClientCollection->getClient('qqexmail'); +/* @var $exmailAuthClient \xj\oauth\QqExmailAuth */ + +//get Admin AccessToken +$accessToken = $exmailAuthClient->getAccessToken(); +/* @var $accessToken yii\authclient\OAuthToken */ +var_dump('accessToken', $accessToken->getToken()); + +try { + //get OneKey Login Url + $oneKeyLoginUrl = $exmailAuthClient->getOneKeyLogin($testEmail); + var_dump('oneKeyLoginUrl', $oneKeyLoginUrl); + + //get Member AuthKey + $authKey = $exmailAuthClient->getMemberAuthKey($testEmail); + var_dump('authKey', $authKey); + + //get Member Info + $memberInfo = $exmailAuthClient->getMemberInfo($testEmail); + var_dump('memberInfo', $memberInfo); + + $statusAvailableResult = $exmailAuthClient->getMemberStatusAvailable($testAddEmail); + var_dump('statusAvailableResult', $statusAvailableResult); + + //add Member + $syncResult = $exmailAuthClient->syncMember($testAddEmail, [ + 'action' => \xj\oauth\QqExmailAuth::ACTION_ADD, + 'name' => $testAddEmailName, + 'password' => md5(uniqid()), + 'gender' => \xj\oauth\QqExmailAuth::GENDER_MALE, + 'md5' => \xj\oauth\QqExmailAuth::MD5_ENCYPT, + 'OpenType' => \xj\oauth\QqExmailAuth::OPEN_TYPE_ENABLE, + ]); + var_dump('syncResult', $syncResult); + + //get Member Status + $statusResult = $exmailAuthClient->getMemberStatus($testAddEmail); + var_dump('statusResult', $statusResult); + + //get Member List + $memberListByVersionResult = $exmailAuthClient->getMemberListByVersion(0); + var_dump('memberListByVersionResult', $memberListByVersionResult); + + //未读邮件 + $mailUnreadCount = $exmailAuthClient->getMailNewCount($testAddEmail); + var_dump('mailUnreadCount', $mailUnreadCount); + + //Add Group + $addGroupResult = $exmailAuthClient->addGroup($testGroupName, $testGroupAdmin, \xj\oauth\QqExmailAuth::GROUP_STATUS_ALL, $testEmail); + var_dump('addGroupResult', $addGroupResult); + + //Add Group Member + $addGroupMemberResult = $exmailAuthClient->addGroupMember($testGroupAdmin, $testAddEmail); + var_dump('addGroupMemberResult', $addGroupMemberResult); + + //Del Group Member + $delGroupMemberResult = $exmailAuthClient->deleteGroupMember($testGroupAdmin, $testAddEmail); + var_dump('delGroupMemberResult', $delGroupMemberResult); + + //Del Group + $delGroupResult = $exmailAuthClient->delGroup($testGroupAdmin); + var_dump('delGroupResult', $delGroupResult); + + //delete Member + $deleteMemberResult = $exmailAuthClient->delMember($testAddEmail); + var_dump('delete Member', $deleteMemberResult); + +} catch (\xj\oauth\exception\QqExmailException $ex) { + //function Request Fail + var_dump($ex->getMessage(), $ex->getCode()); +} catch (\yii\authclient\InvalidResponseException $ex) { + //fetchAccessToken Fail + var_dump($ex->getMessage(), $ex->getCode()); +} +``` \ No newline at end of file diff --git a/RenrenAuth.php b/RenrenAuth.php index 7e8a876..db6eead 100644 --- a/RenrenAuth.php +++ b/RenrenAuth.php @@ -9,7 +9,8 @@ * @author light * @see http://wiki.dev.renren.com/wiki/Authentication */ -class RenrenAuth extends OAuth2 implements IAuth { +class RenrenAuth extends OAuth2 implements IAuth +{ /** * @inheritdoc @@ -32,7 +33,8 @@ class RenrenAuth extends OAuth2 implements IAuth { * * @inheritdoc */ - protected function initUserAttributes() { + protected function initUserAttributes() + { return $this->getAccessToken()->getParams()['user']; } @@ -42,7 +44,8 @@ protected function initUserAttributes() { * @see http://wiki.dev.renren.com/wiki/V2/user/get * @return array */ - public function getUserInfo() { + public function getUserInfo() + { $user = $this->getUserAttributes(); return $this->api("v2/user/get", 'GET', ['userId' => $user['id']]); } @@ -50,21 +53,24 @@ public function getUserInfo() { /** * @inheritdoc */ - protected function defaultName() { + protected function defaultName() + { return 'renren'; } /** * @inheritdoc */ - protected function defaultTitle() { + protected function defaultTitle() + { return 'Renren'; } /** * @inheritdoc */ - protected function defaultViewOptions() { + protected function defaultViewOptions() + { return [ 'popupWidth' => 800, 'popupHeight' => 500, diff --git a/WeiboAuth.php b/WeiboAuth.php index f13d8ef..744babf 100644 --- a/WeiboAuth.php +++ b/WeiboAuth.php @@ -8,7 +8,8 @@ * Sina Weibo OAuth * @author xjflyttp */ -class WeiboAuth extends OAuth2 implements IAuth { +class WeiboAuth extends OAuth2 implements IAuth +{ public $authUrl = 'https://api.weibo.com/oauth2/authorize'; public $tokenUrl = 'https://api.weibo.com/oauth2/access_token'; @@ -20,7 +21,8 @@ class WeiboAuth extends OAuth2 implements IAuth { * @see http://open.weibo.com/wiki/Oauth2/get_token_info * @see http://open.weibo.com/wiki/2/users/show */ - protected function initUserAttributes() { + protected function initUserAttributes() + { return $this->api('oauth2/get_token_info', 'POST'); } @@ -29,20 +31,24 @@ protected function initUserAttributes() { * @return [] * @see http://open.weibo.com/wiki/2/users/show */ - public function getUserInfo() { + public function getUserInfo() + { $openid = $this->getUserAttributes(); return $this->api("2/users/show.json", 'GET', ['uid' => $openid['uid']]); } - protected function defaultName() { + protected function defaultName() + { return 'Weibo'; } - protected function defaultTitle() { + protected function defaultTitle() + { return 'Weibo'; } - protected function defaultViewOptions() { + protected function defaultViewOptions() + { return [ 'popupWidth' => 800, 'popupHeight' => 500, diff --git a/WeixinAuth.php b/WeixinAuth.php index 1c2ff8b..0958c5b 100644 --- a/WeixinAuth.php +++ b/WeixinAuth.php @@ -9,7 +9,8 @@ * @author xjflyttp * @see https://open.weixin.qq.com/cgi-bin/showdocument?action=doc&id=open1419316505&t=0.1933593254077447 */ -class WeixinAuth extends OAuth2 implements IAuth { +class WeixinAuth extends OAuth2 implements IAuth +{ public $authUrl = 'https://open.weixin.qq.com/connect/qrconnect'; public $tokenUrl = 'https://api.weixin.qq.com/sns/oauth2/access_token'; @@ -22,7 +23,8 @@ class WeixinAuth extends OAuth2 implements IAuth { * @param array $params additional auth GET params. * @return string authorization URL. */ - public function buildAuthUrl(array $params = []) { + public function buildAuthUrl(array $params = []) + { $defaultParams = [ 'appid' => $this->clientId, 'redirect_uri' => $this->getReturnUrl(), @@ -41,7 +43,8 @@ public function buildAuthUrl(array $params = []) { * @param array $params additional request params. * @return OAuthToken access token. */ - public function fetchAccessToken($authCode, array $params = []) { + public function fetchAccessToken($authCode, array $params = []) + { $defaultParams = [ 'appid' => $this->clientId, 'secret' => $this->clientSecret, @@ -59,7 +62,8 @@ public function fetchAccessToken($authCode, array $params = []) { /** * @inheritdoc */ - protected function apiInternal($accessToken, $url, $method, array $params, array $headers) { + protected function apiInternal($accessToken, $url, $method, array $params, array $headers) + { $params['access_token'] = $accessToken->getToken(); $params['openid'] = $this->openid; return $this->sendRequest($method, $url, $params, $headers); @@ -70,7 +74,8 @@ protected function apiInternal($accessToken, $url, $method, array $params, array * @return [] * @see https://open.weixin.qq.com/cgi-bin/showdocument?action=doc&id=open1419316518&t=0.14920092844688204 */ - protected function initUserAttributes() { + protected function initUserAttributes() + { return $this->api('sns/userinfo'); } @@ -79,19 +84,23 @@ protected function initUserAttributes() { * @return [] * @see http://open.weibo.com/wiki/2/users/show */ - public function getUserInfo() { + public function getUserInfo() + { return $this->getUserAttributes(); } - protected function defaultName() { + protected function defaultName() + { return 'Weixin'; } - protected function defaultTitle() { + protected function defaultTitle() + { return 'Weixin'; } - protected function defaultViewOptions() { + protected function defaultViewOptions() + { return [ 'popupWidth' => 800, 'popupHeight' => 500, diff --git a/exception/OAuthException.php b/exception/OAuthException.php new file mode 100644 index 0000000..26dc132 --- /dev/null +++ b/exception/OAuthException.php @@ -0,0 +1,14 @@ + + */ +class OAuthException extends Exception +{ + +} diff --git a/exception/QqExmailException.php b/exception/QqExmailException.php new file mode 100644 index 0000000..8f78e31 --- /dev/null +++ b/exception/QqExmailException.php @@ -0,0 +1,12 @@ + + */ +class QqExmailException extends OAuthException +{ + +}