/b2b/core/model/member/mdl.passport.php
PHP | 302 lines | 273 code | 21 blank | 8 comment | 47 complexity | 4c52d7d7b34bf7dbb5a94d81f9a35968 MD5 | raw file
- <?php
- require_once('plugin.php');
- class mdl_passport extends plugin{
- var $plugin_type = 'file';
- var $plugin_name = 'passport';
- var $prefix='passport.';
- var $_passport = null;
- var $_builtin = array('spirit');
- function _verify($plugin=NULL) {
- if ($plugin = empty($plugin)?$this->getCurrentPlugin():$plugin) {
- if ($this->getFile($plugin)) {
- return true;
- }
- }
- return false;
- }
- function &_load($plugin=NULL){
- if ($plugin = empty($plugin)?$this->getCurrentPlugin():$plugin) {
- if(!$this->_passport){
- $obj = &$this->load($plugin);
- $this->_passport = &$obj;
- if(method_exists($obj,'getOptions')||method_exists($obj,'getoptions')){
- $obj->setConfig($this->getOptions($plugin,true));
- }
- }else{
- $obj = &$this->_passport;
- }
- return $obj;
- }
- }
- function login($userId, $url) {
- if ($this->_verify()) {
- $obj = &$this->_load();
- return $obj->login($userId,$url);
- }
- }
- function ssoSignin($plugin=NULL) {
- if ($this->_verify($plugin)) {
- $obj = &$this->_load($plugin);
- return $obj->ssoSignin();
- }
- }
- function logout($userId,$url) {
- if ($this->_verify()) {
- $obj = &$this->_load();
- return $obj->logout($userId,$url);
- }
- }
- function regist($userId,$url) {
- if ($this->_verify()) {
- $status = $this->system->loadModel('system/status');
- $status->add('MEMBER_REG');
- $obj = &$this->_load();
- return $obj->regist($userId,$url);
- }
- }
- function setCurrentPlugin($plugin='') {
- return $this->system->setConf('plugin.'.$this->plugin_name.'.config.current_use',$plugin);
- }
- function getCurrentPlugin() {
- return $this->system->getConf('plugin.'.$this->plugin_name.'.config.current_use');
- }
- function getList() {
- if ($p = parent::getList(array(), false)) {
- $current = $this->getCurrentPlugin();
- foreach($p as $k=>$v) {
- $p[$k]['ifvalid'] = (($current==$k)?'true':'false');
- $p[$k]['passport_type'] = $p[$k]['name'];
- unset($p[$k]['name']);
- }
- }
- foreach ($this->_builtin as $builtin) {
- unset($p[$builtin]);
- }
- return $p;
- }
- function savePassport($aData,&$msg){
- if(!$sType = $aData['passport_type']){
- trigger_error('????',E_USER_ERROR);
- }
- if (!$this->saveCfg($sType, $_POST['config'])){
- return false;
- }
- $sCurrentPlugin = $this->getCurrentPlugin($sType);
- if ($aData['passport_ifvalid']=='true') {
- if ($sType != $sCurrentPlugin){
- if (!$this->setCurrentPlugin($sType)){
- return false;
- }
- }
- }else if ($aData['passport_ifvalid']=='false') {
- if ($sType == $sCurrentPlugin){
- if (!$this->setCurrentPlugin()){
- return false;
- }
- }
- }
- if ($obj=$this->function_judge('implodeUserToUC')){
- $obj->implodeUserToUC();
- }
- return true;
- }
- function passport_encrypt($txt, $key) {
- srand((double)microtime() * 1000000);
- $encrypt_key = md5(rand(0, 32000));
- $ctr = 0;
- $tmp = '';
- for($i = 0;$i < strlen($txt); $i++) {
- $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
- $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
- }
- return base64_encode($this->passport_key($tmp, $key));
- }
- function passport_decrypt($txt, $key) {
- $txt = $this->passport_key(base64_decode($txt), $key);
- $tmp = '';
- for ($i = 0;$i < strlen($txt); $i++) {
- $md5 = $txt[$i];
- $tmp .= $txt[++$i] ^ $md5;
- }
- return $tmp;
- }
- function passport_key($txt, $encrypt_key) {
- $encrypt_key = md5($encrypt_key);
- $ctr = 0;
- $tmp = '';
- for($i = 0; $i < strlen($txt); $i++) {
- $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
- $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
- }
- return $tmp;
- }
- function passport_encode($array) {
- $arrayenc = array();
- foreach($array as $key => $val) {
- $arrayenc[] = $key.'='.urlencode($val);
- }
- return implode('&', $arrayenc);
- }
- function function_judge($func){
- if($this->_verify()){
- $obj=&$this->_load();
- }
- if (is_object($obj)){
- if (method_exists($obj,$func))
- return $obj;
- else
- return false;
- }
- else{
- return false;
- }
- }
- function mKey($len = 12, $type = 'ALNUM')
- {
- $alpha = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
- $ALPHA = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
- $num = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '0');
- $keyVals = array();
- $key = array();
- switch ($type)
- {
- case 'lower' :
- $keyVals = $alpha;
- break;
- case 'upper' :
- $keyVals = $ALPHA;
- break;
- case 'numeric' :
- $keyVals = $num;
- break;
- case 'ALPHA' :
- $keyVals = array_merge($alpha, $ALPHA);
- break;
- case 'ALNUM' :
- $keyVals = array_merge($alpha, $ALPHA, $num);
- break;
- }
- for($i = 0; $i <= $len-1; $i++)
- {
- $r = rand(0,count($keyVals)-1);
- $key[$i] = $keyVals[$r];
- }
- return join("", $key);
- }
- function setCookie($name,$value,$expire=false,$path=null){
- $cookiePath = substr(PHP_SELF, 0, strrpos(PHP_SELF, '/')).'/';
- $cookieLife = 3600;
- setCookie(COOKIE_PFIX.'['.$name.']',$value,($expire===false)?(time()+$cookieLife):$expire,$cookiePath);
- $_COOKIE[$name] = $value;
- }
- function createTaoLoginAc(){
- if( empty($_COOKIE['TaoLoginAuthAc']) ){
- $strkey = md5(time().$this->mKey());
- $this->setCookie('TaoLoginAuthAc',$strkey,null);
- }else{
- $strkey = $_COOKIE['TaoLoginAuthAc'];
- }
- return $strkey;
- }
- function login_taobao($target,$view='0')
- {
- $cert = $this->system->loadModel("service/certificate");
- $certificate_id = $cert->getCerti();
- $token = $cert->getToken();
- $target_url = parse_url($target);
- if( $target_url['query'] && in_array($target_url['query'],array('passport-verify_taobao.html','member-bindAccount.html')) ){
- $url = $target_url['scheme']."://".$target_url['host'].$target_url['path']."?";
- $url .= substr($target_url['query'],0,strpos($target_url['query'],'-')).'-'.$this->createTaoLoginAc().strstr($target_url['query'],'-');
- }else{
- $url = $target;
- }
- $params = array(
- 'act'=>"loginTaobao",
- 'certificate_id'=>$certificate_id,
- 'send_data'=>'json',
- 'target'=>$url,
- 'api_version'=>"3.1",
- 'return_data'=>'json',
- );
- ksort($params);
- $ac = md5(join('',$params).$token);
- $params['ac'] = $ac;
- $url = "http://".SERVER_HOST_PLATFORM.SERVER_PLATFORM_API;
- if( $view == '0' ){
- $str="<div id='loginTaobao'>
- <form id='tbform' action = '{$url}' method='post'>";
- foreach($params as $k=>$v)
- {
- $str .= '<input type="hidden" value="'.$v.'" name="'.$k.'">';
- }
- $login_str = $str."<input type='submit' value='????????'></form></div>";
- }else{
- $strinput = "";
- foreach($params as $k=>$v){
- $strinput .= '<input type="hidden" value="'.$v.'" name="'.$k.'">';
- }
- // $login_str = '<div style="float:left"><a class="ss_btn_login" href="#" onclick="authlogin();">????????</a><div style="margin-left:35px">????????????????</div><script>
- // function authlogin(){
- // //var form=$(\'form_info\').value;
- // var f =new Element(\'form\',{\'id\':\'tbform\',method:\'post\',action:\''.$url.'\'});
- // f.inject(\'shareObjectsStorage_panel\',\'after\');
- // f.set(\'html\',\''.$strinput.'\');
- // $(\'tbform\').submit();}
- // </script></div>';
- $login_str = '<div class="ss_btn_bar"> <a href="#" class="ss_btn_login" onclick="authlogin();">????????</a>
- <script>
- function authlogin(){
- //var form=$(\'form_info\').value;
- var f =new Element(\'form\',{\'id\':\'tbform\',method:\'post\',action:\''.$url.'\'});
- f.inject(\'shareObjectsStorage_panel\',\'after\');
- f.set(\'html\',\''.$strinput.'\');
- $(\'tbform\').submit();}
- </script>
- </div>';
- }
- return $login_str;
- }
- function taobaoVerify($params){
- if( !empty($params) && is_array($params) && $params['session'] && $params['sign'] && $params['taobao_user_nick'] ){
- foreach( $params as $k=>$v ){
- if( empty($v) && strpos($k,'_html') && substr_count($k,'-')==2 ){
- $keys=explode('-',$k);
- $ackeys[] = $keys[1];
- }
- }
- if(is_array($ackeys)&&!empty($ackeys)){
- foreach( $ackeys as $value ){
- if( $value == $_COOKIE['TaoLoginAuthAc'] ){
- $this->setCookie('TaoLoginAuthAc', '', time()-1000);
- unset($_COOKIE['TaoLoginAuthAc']);
- unset($_COOKIE[COOKIE_PFIX.'[TaoLoginAuthAc]']);
- return true;
- }
- }
- }
- }
- return false;
- }
- }
- ?>