PageRenderTime 21ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/b2b/core/model/member/mdl.passport.php

http://phpfor.googlecode.com/
PHP | 302 lines | 273 code | 21 blank | 8 comment | 47 complexity | 4c52d7d7b34bf7dbb5a94d81f9a35968 MD5 | raw file
  1. <?php
  2. require_once('plugin.php');
  3. class mdl_passport extends plugin{
  4. var $plugin_type = 'file';
  5. var $plugin_name = 'passport';
  6. var $prefix='passport.';
  7. var $_passport = null;
  8. var $_builtin = array('spirit');
  9. function _verify($plugin=NULL) {
  10. if ($plugin = empty($plugin)?$this->getCurrentPlugin():$plugin) {
  11. if ($this->getFile($plugin)) {
  12. return true;
  13. }
  14. }
  15. return false;
  16. }
  17. function &_load($plugin=NULL){
  18. if ($plugin = empty($plugin)?$this->getCurrentPlugin():$plugin) {
  19. if(!$this->_passport){
  20. $obj = &$this->load($plugin);
  21. $this->_passport = &$obj;
  22. if(method_exists($obj,'getOptions')||method_exists($obj,'getoptions')){
  23. $obj->setConfig($this->getOptions($plugin,true));
  24. }
  25. }else{
  26. $obj = &$this->_passport;
  27. }
  28. return $obj;
  29. }
  30. }
  31. function login($userId, $url) {
  32. if ($this->_verify()) {
  33. $obj = &$this->_load();
  34. return $obj->login($userId,$url);
  35. }
  36. }
  37. function ssoSignin($plugin=NULL) {
  38. if ($this->_verify($plugin)) {
  39. $obj = &$this->_load($plugin);
  40. return $obj->ssoSignin();
  41. }
  42. }
  43. function logout($userId,$url) {
  44. if ($this->_verify()) {
  45. $obj = &$this->_load();
  46. return $obj->logout($userId,$url);
  47. }
  48. }
  49. function regist($userId,$url) {
  50. if ($this->_verify()) {
  51. $status = $this->system->loadModel('system/status');
  52. $status->add('MEMBER_REG');
  53. $obj = &$this->_load();
  54. return $obj->regist($userId,$url);
  55. }
  56. }
  57. function setCurrentPlugin($plugin='') {
  58. return $this->system->setConf('plugin.'.$this->plugin_name.'.config.current_use',$plugin);
  59. }
  60. function getCurrentPlugin() {
  61. return $this->system->getConf('plugin.'.$this->plugin_name.'.config.current_use');
  62. }
  63. function getList() {
  64. if ($p = parent::getList(array(), false)) {
  65. $current = $this->getCurrentPlugin();
  66. foreach($p as $k=>$v) {
  67. $p[$k]['ifvalid'] = (($current==$k)?'true':'false');
  68. $p[$k]['passport_type'] = $p[$k]['name'];
  69. unset($p[$k]['name']);
  70. }
  71. }
  72. foreach ($this->_builtin as $builtin) {
  73. unset($p[$builtin]);
  74. }
  75. return $p;
  76. }
  77. function savePassport($aData,&$msg){
  78. if(!$sType = $aData['passport_type']){
  79. trigger_error('????',E_USER_ERROR);
  80. }
  81. if (!$this->saveCfg($sType, $_POST['config'])){
  82. return false;
  83. }
  84. $sCurrentPlugin = $this->getCurrentPlugin($sType);
  85. if ($aData['passport_ifvalid']=='true') {
  86. if ($sType != $sCurrentPlugin){
  87. if (!$this->setCurrentPlugin($sType)){
  88. return false;
  89. }
  90. }
  91. }else if ($aData['passport_ifvalid']=='false') {
  92. if ($sType == $sCurrentPlugin){
  93. if (!$this->setCurrentPlugin()){
  94. return false;
  95. }
  96. }
  97. }
  98. if ($obj=$this->function_judge('implodeUserToUC')){
  99. $obj->implodeUserToUC();
  100. }
  101. return true;
  102. }
  103. function passport_encrypt($txt, $key) {
  104. srand((double)microtime() * 1000000);
  105. $encrypt_key = md5(rand(0, 32000));
  106. $ctr = 0;
  107. $tmp = '';
  108. for($i = 0;$i < strlen($txt); $i++) {
  109. $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  110. $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
  111. }
  112. return base64_encode($this->passport_key($tmp, $key));
  113. }
  114. function passport_decrypt($txt, $key) {
  115. $txt = $this->passport_key(base64_decode($txt), $key);
  116. $tmp = '';
  117. for ($i = 0;$i < strlen($txt); $i++) {
  118. $md5 = $txt[$i];
  119. $tmp .= $txt[++$i] ^ $md5;
  120. }
  121. return $tmp;
  122. }
  123. function passport_key($txt, $encrypt_key) {
  124. $encrypt_key = md5($encrypt_key);
  125. $ctr = 0;
  126. $tmp = '';
  127. for($i = 0; $i < strlen($txt); $i++) {
  128. $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  129. $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
  130. }
  131. return $tmp;
  132. }
  133. function passport_encode($array) {
  134. $arrayenc = array();
  135. foreach($array as $key => $val) {
  136. $arrayenc[] = $key.'='.urlencode($val);
  137. }
  138. return implode('&', $arrayenc);
  139. }
  140. function function_judge($func){
  141. if($this->_verify()){
  142. $obj=&$this->_load();
  143. }
  144. if (is_object($obj)){
  145. if (method_exists($obj,$func))
  146. return $obj;
  147. else
  148. return false;
  149. }
  150. else{
  151. return false;
  152. }
  153. }
  154. function mKey($len = 12, $type = 'ALNUM')
  155. {
  156. $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');
  157. $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');
  158. $num = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '0');
  159. $keyVals = array();
  160. $key = array();
  161. switch ($type)
  162. {
  163. case 'lower' :
  164. $keyVals = $alpha;
  165. break;
  166. case 'upper' :
  167. $keyVals = $ALPHA;
  168. break;
  169. case 'numeric' :
  170. $keyVals = $num;
  171. break;
  172. case 'ALPHA' :
  173. $keyVals = array_merge($alpha, $ALPHA);
  174. break;
  175. case 'ALNUM' :
  176. $keyVals = array_merge($alpha, $ALPHA, $num);
  177. break;
  178. }
  179. for($i = 0; $i <= $len-1; $i++)
  180. {
  181. $r = rand(0,count($keyVals)-1);
  182. $key[$i] = $keyVals[$r];
  183. }
  184. return join("", $key);
  185. }
  186. function setCookie($name,$value,$expire=false,$path=null){
  187. $cookiePath = substr(PHP_SELF, 0, strrpos(PHP_SELF, '/')).'/';
  188. $cookieLife = 3600;
  189. setCookie(COOKIE_PFIX.'['.$name.']',$value,($expire===false)?(time()+$cookieLife):$expire,$cookiePath);
  190. $_COOKIE[$name] = $value;
  191. }
  192. function createTaoLoginAc(){
  193. if( empty($_COOKIE['TaoLoginAuthAc']) ){
  194. $strkey = md5(time().$this->mKey());
  195. $this->setCookie('TaoLoginAuthAc',$strkey,null);
  196. }else{
  197. $strkey = $_COOKIE['TaoLoginAuthAc'];
  198. }
  199. return $strkey;
  200. }
  201. function login_taobao($target,$view='0')
  202. {
  203. $cert = $this->system->loadModel("service/certificate");
  204. $certificate_id = $cert->getCerti();
  205. $token = $cert->getToken();
  206. $target_url = parse_url($target);
  207. if( $target_url['query'] && in_array($target_url['query'],array('passport-verify_taobao.html','member-bindAccount.html')) ){
  208. $url = $target_url['scheme']."://".$target_url['host'].$target_url['path']."?";
  209. $url .= substr($target_url['query'],0,strpos($target_url['query'],'-')).'-'.$this->createTaoLoginAc().strstr($target_url['query'],'-');
  210. }else{
  211. $url = $target;
  212. }
  213. $params = array(
  214. 'act'=>"loginTaobao",
  215. 'certificate_id'=>$certificate_id,
  216. 'send_data'=>'json',
  217. 'target'=>$url,
  218. 'api_version'=>"3.1",
  219. 'return_data'=>'json',
  220. );
  221. ksort($params);
  222. $ac = md5(join('',$params).$token);
  223. $params['ac'] = $ac;
  224. $url = "http://".SERVER_HOST_PLATFORM.SERVER_PLATFORM_API;
  225. if( $view == '0' ){
  226. $str="<div id='loginTaobao'>
  227. <form id='tbform' action = '{$url}' method='post'>";
  228. foreach($params as $k=>$v)
  229. {
  230. $str .= '<input type="hidden" value="'.$v.'" name="'.$k.'">';
  231. }
  232. $login_str = $str."<input type='submit' value='????????'></form></div>";
  233. }else{
  234. $strinput = "";
  235. foreach($params as $k=>$v){
  236. $strinput .= '<input type="hidden" value="'.$v.'" name="'.$k.'">';
  237. }
  238. // $login_str = '<div style="float:left"><a class="ss_btn_login" href="#" onclick="authlogin();">????????</a><div style="margin-left:35px">????????????????</div><script>
  239. // function authlogin(){
  240. // //var form=$(\'form_info\').value;
  241. // var f =new Element(\'form\',{\'id\':\'tbform\',method:\'post\',action:\''.$url.'\'});
  242. // f.inject(\'shareObjectsStorage_panel\',\'after\');
  243. // f.set(\'html\',\''.$strinput.'\');
  244. // $(\'tbform\').submit();}
  245. // </script></div>';
  246. $login_str = '<div class="ss_btn_bar"> <a href="#" class="ss_btn_login" onclick="authlogin();">????????</a>
  247. <script>
  248. function authlogin(){
  249. //var form=$(\'form_info\').value;
  250. var f =new Element(\'form\',{\'id\':\'tbform\',method:\'post\',action:\''.$url.'\'});
  251. f.inject(\'shareObjectsStorage_panel\',\'after\');
  252. f.set(\'html\',\''.$strinput.'\');
  253. $(\'tbform\').submit();}
  254. </script>
  255. </div>';
  256. }
  257. return $login_str;
  258. }
  259. function taobaoVerify($params){
  260. if( !empty($params) && is_array($params) && $params['session'] && $params['sign'] && $params['taobao_user_nick'] ){
  261. foreach( $params as $k=>$v ){
  262. if( empty($v) && strpos($k,'_html') && substr_count($k,'-')==2 ){
  263. $keys=explode('-',$k);
  264. $ackeys[] = $keys[1];
  265. }
  266. }
  267. if(is_array($ackeys)&&!empty($ackeys)){
  268. foreach( $ackeys as $value ){
  269. if( $value == $_COOKIE['TaoLoginAuthAc'] ){
  270. $this->setCookie('TaoLoginAuthAc', '', time()-1000);
  271. unset($_COOKIE['TaoLoginAuthAc']);
  272. unset($_COOKIE[COOKIE_PFIX.'[TaoLoginAuthAc]']);
  273. return true;
  274. }
  275. }
  276. }
  277. }
  278. return false;
  279. }
  280. }
  281. ?>