PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/b2b/plugins/passport/passport.discuz.php

http://phpfor.googlecode.com/
PHP | 214 lines | 180 code | 26 blank | 8 comment | 22 complexity | 0384d4e27cd3298efd1545131025bb5e MD5 | raw file
  1. <?php
  2. class passport_discuz extends modelFactory {
  3. var $passport_name = "Discuz??V6.0";
  4. var $passport_memo = "";
  5. var $_config = null;
  6. var $forward=1;
  7. function setConfig($config) {
  8. $this->_config = $config;
  9. }
  10. function verifylogin($login,$passwd){
  11. }
  12. function decode($responseData){
  13. }
  14. function getoptions() {
  15. return array(
  16. 'URL'=>array('label'=>'????URL?','type'=>'input'),
  17. 'PrivateKey'=>array('label'=>'?????','type'=>'input'),
  18. 'encoding'=>array('label'=>'???????','type'=>'select','options'=>array('utf8' => '?????(utf-8)','zh' => '????','big5' => '????','en' => '??')),
  19. );
  20. }
  21. /**
  22. * CHECK DISCUZ INPUT KEY
  23. */
  24. function validKey(){
  25. $passport_key = $this->system->getConf('plugin.passport.discuz.config.PrivateKey');
  26. if($_GET['verify'] != md5($_GET['action'].$_GET['auth'].$_GET['forward'].$passport_key)) {
  27. return false;
  28. }
  29. return $passport_key;
  30. }
  31. function ssoSignin(){
  32. /*
  33. * discuz sign in
  34. */
  35. switch($_GET['action'] ){
  36. case 'login':
  37. return $this->ssoLogin();
  38. case 'logout':
  39. return $this->ssoLoginOut();
  40. }
  41. }
  42. function ssoLoginOut(){
  43. return false;
  44. }
  45. function ssoLogin(){
  46. $passport_key=$this->validKey();
  47. if(!$passport_key){
  48. return false;
  49. }
  50. $oPassport = $this->system->loadModel('member/passport');
  51. parse_str($oPassport->passport_decrypt($_GET['auth'], $passport_key), $member);
  52. if (true || $SITE_EODING=="UTF-8"){
  53. if($this->_config['encoding']!='utf8'){
  54. $charset = $this->system->loadModel('utility/charset');
  55. foreach($member as $index => $value){
  56. $member[$index] = $charset->local2utf($value);
  57. }
  58. }
  59. }
  60. $memberObj = $this->system->loadModel('member/account');
  61. $info=$memberObj->verifyPassportLogin($member);
  62. if(!$info){
  63. $info = $memberObj->createPassport($member);
  64. if(!$info){
  65. return false;
  66. }
  67. return $memberObj->toLogin($member);
  68. }
  69. return $memberObj->toLogin($member);
  70. }
  71. function login($userId, $rurl) {
  72. $oMember = $this->system->loadModel('member/member');
  73. $aMember = $oMember->getFieldById($userId);
  74. $username = $aMember['uname'];
  75. if (true || $SITE_EODING=="UTF-8"){
  76. if($this->_config['encoding']!='utf8'){
  77. $username = utf2local($username,$this->_config['encoding']);
  78. }
  79. }
  80. $member = array(
  81. 'cookietime' => 31536000,
  82. 'time' => time(),
  83. 'username' => $username,
  84. 'password' => $aMember['password'],
  85. 'gender' => $aMember['sex'],
  86. 'email' => $aMember['email'],
  87. 'credits' => $aMember['point'],
  88. 'regip' => $aMember['reg_ip'],
  89. 'regdate' => $aMember['regtime'],
  90. 'qq' => ''
  91. );
  92. $auth = passport_encrypt(passport_encode($member), $this->_config['PrivateKey']);
  93. if (substr($this->_config['URL'],-1)=="/")
  94. $shop_loginapi_url = $this->_config['URL']."api/shopex.php";
  95. else{
  96. if (strtoupper(substr($this->_config['URL'],-10,6))=="SHOPEX")
  97. $shop_loginapi_url=$this->_config['URL'];
  98. else
  99. $shop_loginapi_url = $this->_config['URL']."/api/shopex.php";
  100. }
  101. header('Location: '.$shop_loginapi_url.'?action=login&auth='.rawurlencode($auth).'&forward='.rawurlencode($rurl).'&verify='.md5('login'.$auth.$rurl.$this->_config['PrivateKey']));
  102. exit;
  103. }
  104. function regist($userId,$rurl) {
  105. $oMember = $this->system->loadModel('member/member');
  106. $aMember = $oMember->getFieldById($userId);
  107. $username = $aMember['uname'];
  108. if(true || $SITE_EODING=="UTF-8"){
  109. if($this->_config['encoding']!='utf8'){
  110. $username = utf2local($username,$this->_config['encoding']);
  111. }
  112. }
  113. $member = array(
  114. 'cookietime' => 31536000,
  115. 'time' => time(),
  116. 'username' => $username,
  117. 'password' => $aMember['password'],
  118. 'gender' => $aMember['sex'],
  119. 'email' => $aMember['email'],
  120. 'credits' => $aMember['point'],
  121. 'regip' => $aMember['reg_ip'],
  122. 'regdate' => $aMember['regtime'],
  123. 'qq' => ''
  124. );
  125. $rurl.="index.php?passport-create.html";
  126. $this->setPlugCookie(1);
  127. $auth = passport_encrypt(passport_encode($member), $this->_config['PrivateKey']);
  128. $shop_loginapi_url = substr($this->_config['URL'],-1)=="/"?$this->_config['URL']."api/shopex.php":$this->_config['URL'];
  129. header('Location: '.$shop_loginapi_url.'?action=login&auth='.rawurlencode($auth).'&forward='.rawurlencode($rurl).'&verify='.md5('login'.$auth.$rurl.$this->_config['PrivateKey']));
  130. exit;
  131. }
  132. function logout($userId,$rurl) {
  133. $shop_loginapi_url = substr($this->_config['URL'],-1)=="/"?$this->_config['URL']."api/shopex.php":$this->_config['URL'];
  134. header('Location: '.$shop_loginapi_url.'?action=logout&forward='.rawurlencode($rurl).'&verify='.md5('logout'.$rurl.$this->_config['PrivateKey']));
  135. exit;
  136. }
  137. function getPlugCookie(){
  138. $account = $this->system->loadModel('member/account');
  139. return $account->getPlugCookie('CType');
  140. }
  141. function setPlugCookie($val){
  142. $account = $this->system->loadModel('member/account');
  143. if ($val)
  144. $account->setPlugCookie('CType','discuzz');
  145. else
  146. $account->setPlugCookie('CType','');
  147. }
  148. }
  149. /*******????????*********/
  150. function passport_encrypt($txt, $key) {
  151. srand((double)microtime() * 1000000);
  152. $encrypt_key = md5(rand(0, 32000));
  153. $ctr = 0;
  154. $tmp = '';
  155. for($i = 0;$i < strlen($txt); $i++) {
  156. $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  157. $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
  158. }
  159. return base64_encode(passport_key($tmp, $key));
  160. }
  161. function passport_decrypt($txt, $key) {
  162. $txt = passport_key(base64_decode($txt), $key);
  163. $tmp = '';
  164. for ($i = 0;$i < strlen($txt); $i++) {
  165. $md5 = $txt[$i];
  166. $tmp .= $txt[++$i] ^ $md5;
  167. }
  168. return $tmp;
  169. }
  170. function passport_key($txt, $encrypt_key) {
  171. $encrypt_key = md5($encrypt_key);
  172. $ctr = 0;
  173. $tmp = '';
  174. for($i = 0; $i < strlen($txt); $i++) {
  175. $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  176. $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
  177. }
  178. return $tmp;
  179. }
  180. function passport_encode($array) {
  181. $arrayenc = array();
  182. foreach($array as $key => $val) {
  183. $arrayenc[] = $key.'='.urlencode($val);
  184. }
  185. return implode('&', $arrayenc);
  186. }
  187. /*******end of ????????*********/
  188. ?>