client.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. <?php
  2. class client {
  3. private $ps_api_url, $ps_auth_key, $ps_vsersion;
  4. /**
  5. * 析构函数
  6. * @param $ps_api_url 接口域名
  7. * @param $ps_auth_key 加密密匙
  8. */
  9. public function __construct($ps_api_url = '127.0.0.1', $ps_auth_key = '', $ps_vsersion = '1') {
  10. $this->ps_api_url = $ps_api_url;
  11. $this->ps_auth_key = $ps_auth_key;
  12. $this->ps_vsersion = $ps_vsersion;
  13. }
  14. /**
  15. * 用户注册
  16. * @param string $username 用户名
  17. * @param string $password 密码
  18. * @param string $email email
  19. * @param string $regip 注册ip
  20. * @param string $random 密码随机数
  21. * @return int {-1:用户名已经存在 ;-2:email已存在;-3:email格式错误;-4:用户名禁止注册;-5:邮箱禁止注册;int(uid):成功}
  22. */
  23. public function ps_member_register($username, $password, $email, $regip = '', $random = '') {
  24. if (!$this->_is_email($email)) {
  25. return -3;
  26. }
  27. return $this->_ps_send('register', array('username' => $username, 'password' => $password, 'email' => $email, 'regip' => $regip, 'random' => $random));
  28. }
  29. /**
  30. * 用户登陆
  31. * @param string $username 用户名
  32. * @param string $password 密码
  33. * @param int $isemail email
  34. * @return int {-2;密码错误;-1:用户名不存在;array(userinfo):用户信息}
  35. */
  36. public function zhsso_member_login($username, $password, $isemail = 0) {
  37. if ($isemail == 1) {
  38. if (!$this->_is_email($username)) {
  39. return -3;
  40. }
  41. $return = $this->_ps_send('login', array('email' => $username, 'userpasswd' => $password));
  42. } else if($isemail == 2 ) {
  43. if (!$this->_is_mobile($username)) {
  44. return -3;
  45. }
  46. $return = $this->_ps_send('login', array('mobile' => $username, 'userpasswd' => $password));
  47. } else {
  48. $return = $this->_ps_send('login', array('username' => $username, 'userpasswd' => $password));
  49. }
  50. return $return;
  51. }
  52. /**
  53. * 获取用户信息通过COOKIE UID
  54. */
  55. public function zhsso_getuserinfo_by_uid($uid=0) {
  56. return $this->_ps_send('getuserinfo', array('uid' => $uid));
  57. }
  58. public function zhsso_checkauth($auth="") {
  59. return $this->_ps_send('checkauth', array('auth' => $auth));
  60. }
  61. /**
  62. * 同步登陆
  63. * @param string $uid
  64. * @return string javascript用户同步登陆js
  65. */
  66. public function ps_member_synlogin($uid) {
  67. $uid = intval($uid);
  68. return $this->_ps_send('synlogin', array('uid' => $uid));
  69. }
  70. /**
  71. * 同步退出
  72. * @param string $uid
  73. * @return string javascript用户同步退出js
  74. */
  75. public function ps_member_synlogout() {
  76. return $this->_ps_send('synlogout', array());
  77. }
  78. /**
  79. * 编辑用户
  80. * @param string $username 用户名
  81. * @param string $email email
  82. * @param string $password 旧密码
  83. * @param string $newpassword 新密码
  84. * @param int $uid phpsso用户uid
  85. * @param string $random 密码随机数
  86. * @return int {-1:用户不存在;-2:旧密码错误;-3:email已经存在 ;-4:email格式错误;1:成功;0:未作修改}
  87. */
  88. public function zhsso_member_edit($uid = '', $password = '', $newpassword = '', $useranswer = '') {
  89. /// if ($email && !$this->_is_email($email)) {
  90. // return -4;
  91. // }
  92. // TODO:增加判断输出返回值
  93. return $this->_ps_send('edit', array('uid' => $uid, 'password' => $password, 'newpassword' => $newpassword, 'useranswer' => $useranswer));
  94. }
  95. public function setPayWay($uid, $idcards, $alipay, $tenpay) {
  96. return $this->_ps_send('payway', array('uid' => $uid, 'idbank' => $idcards, 'alipay' => $alipay, 'tenpay' => $tenpay));
  97. }
  98. /**
  99. * 删除用户头像
  100. * @param int $uid phpsso用户uid
  101. * @return int {1:成功;0:失败}
  102. */
  103. public function ps_deleteavatar($uid) {
  104. return $this->_ps_send('deleteavatar', array('uid' => $uid));
  105. }
  106. /**
  107. * 获取用户信息
  108. * @param $mix 用户id/用户名/email
  109. * @param $type {1:用户id;2:用户名;3:email}
  110. * @return $mix {-1:用户不存在;userinfo:用户信息}
  111. */
  112. public function ps_get_member_info($mix, $type = 1) {
  113. if ($type == 1) {
  114. $userinfo = $this->_ps_send('getuserinfo', array('uid' => $mix));
  115. } elseif ($type == 2) {
  116. $userinfo = $this->_ps_send('getuserinfo', array('username' => $mix));
  117. } elseif ($type == 3) {
  118. if (!$this->_is_email($mix)) {
  119. return -4;
  120. }
  121. $userinfo = $this->_ps_send('getuserinfo', array('email' => $mix));
  122. }
  123. if ($userinfo) {
  124. return $userinfo;
  125. } else {
  126. return -1;
  127. }
  128. }
  129. function url_exists($url) {
  130. $h = get_headers($url);
  131. if (!$h || !isset($h[0]))
  132. return false;
  133. $status = $h[0];
  134. return preg_match("/.*200\s{1}OK/i", $status) ? true : false;
  135. }
  136. /**
  137. * 删除用户
  138. * @param mix {1:用户id;2:用户名;3:email} 如果是用户id可以为数组
  139. * @return int {-1:用户不存在;1:删除成功}
  140. */
  141. public function ps_delete_member($mix, $type = 1) {
  142. if ($type == 1) {
  143. $res = $this->_ps_send('delete', array('uid' => $mix));
  144. } elseif ($type == 2) {
  145. $res = $this->_ps_send('delete', array('username' => $mix));
  146. } elseif ($type == 3) {
  147. if (!$this->_is_email($mix)) {
  148. return -4;
  149. }
  150. $res = $this->_ps_send('delete', array('email' => $mix));
  151. }
  152. return $res;
  153. }
  154. /**
  155. * 检查用户是否可以注册
  156. * @param string $username
  157. * @return int {-4:用户名禁止注册;-1:用户名已经存在 ;1:成功}
  158. */
  159. public function ps_checkname($username) {
  160. return $this->_ps_send('checkname', array('username' => $username));
  161. }
  162. /**
  163. * 检查邮箱是否可以注册
  164. * @param string $email
  165. * @return int {-1:email已经存在 ;-5:邮箱禁止注册;1:成功}
  166. */
  167. public function ps_checkemail($email) {
  168. return $this->_ps_send('checkemail', array('email' => $email));
  169. }
  170. /**
  171. * 获取应用列表信息
  172. */
  173. public function zhsso_getuserinfo() {
  174. $uid = $this->authcode($_COOKIE[Doo::conf()->COOKIEPRE_WK . '_userid']);
  175. return $this->_ps_send('getuserinfo', array('uid' => $uid));
  176. }
  177. /**
  178. * 获取积分兑换比例列表
  179. */
  180. public function ps_getcreditlist() {
  181. return $this->_ps_send('getcredit', array());
  182. }
  183. /**
  184. * 兑换积分
  185. * 用于何其他应用之间积分兑换
  186. * @param int $uid phpssouid
  187. * @param int $from 本系统积分类型id
  188. * @param int $toappid 目标系统应用appid
  189. * @param int $to 目标系统积分类型id
  190. * @param int $credit 本系统扣除积分数
  191. * @return bool {1:成功;0:失败}
  192. */
  193. public function ps_changecredit($uid, $from, $toappid, $to, $credit) {
  194. return $this->_ps_send('changecredit', array('uid' => $uid, 'from' => $from, 'toappid' => $toappid, 'to' => $to, 'credit' => $credit));
  195. }
  196. /**
  197. * 根据phpsso uid获取头像url
  198. * @param int $uid 用户id
  199. * @return array 四个尺寸用户头像数组
  200. */
  201. public function ps_getavatar($uid) {
  202. $dir1 = ceil($uid / 10000);
  203. $dir2 = ceil($uid % 10000 / 1000);
  204. $url = $this->ps_api_url . 'data/avatar/' . $dir1 . '/' . $dir2 . '/' . $uid . '/';
  205. $avatar = array('180' => $url . '180x180.jpg', '90' => $url . '90x90.jpg', '45' => $url . '45x45.jpg', '30' => $url . '30x30.jpg');
  206. return $avatar;
  207. }
  208. /**
  209. * 获取上传头像flash的html代码
  210. * @param int $uid 用户id
  211. */
  212. public function ps_getavatar_upload_html($uid) {
  213. $auth_data = $this->authcode($uid, 'ENCODE');
  214. // echo $auth_data;
  215. // list($a, $encodestr) = explode('=', $auth_data);
  216. $upurl = base64_encode($this->ps_api_url . 'api/uploadavatar/' . rawurlencode($auth_data));
  217. // $upurl = base64_encode($this->ps_api_url . '/index.php?m=phpsso&c=index&a=uploadavatar&auth_data=' . $auth_data);
  218. $str = <<<EOF
  219. <div id="phpsso_uploadavatar_flash"></div>
  220. <script language="javascript" type="text/javascript" src="{$this->ps_api_url}global/v1/js/swfobject.js"></script>
  221. <script type="text/javascript">
  222. var flashvars = {
  223. 'upurl':"{$upurl}&callback=return_avatar&"
  224. };
  225. var params = {
  226. 'align':'middle',
  227. 'play':'true',
  228. 'loop':'false',
  229. 'scale':'showall',
  230. 'wmode':'window',
  231. 'devicefont':'true',
  232. 'id':'Main',
  233. 'bgcolor':'#ffffff',
  234. 'name':'Main',
  235. 'allowscriptaccess':'always'
  236. };
  237. var attributes = {
  238. };
  239. swfobject.embedSWF("{$this->ps_api_url}global/v1/swf/main.swf", "phpsso_uploadavatar_flash", "490", "434", "9.0.0","{$this->ps_api_url}global/v1/swf/expressInstall.swf", flashvars, params, attributes);
  240. function return_avatar(data) {
  241. if(data == 1) {
  242. window.location.reload();
  243. }
  244. }
  245. </script>
  246. EOF;
  247. return $str;
  248. }
  249. /**
  250. * 字符串加密、解密函数
  251. *
  252. *
  253. * @param string $txt 字符串
  254. * @param string $operation ENCODE为加密,DECODE为解密,可选参数,默认为ENCODE,
  255. * @param string $key 密钥:数字、字母、下划线
  256. * @param string $expiry 过期时间
  257. * @return string
  258. */
  259. function sys_auth($string, $operation = 'ENCODE', $key = '', $expiry = 0) {
  260. $key_length = 4;
  261. $key = md5($key != '' ? $key : $this->ps_auth_key);
  262. $fixedkey = hash('md5', $key);
  263. $egiskeys = md5(substr($fixedkey, 16, 16));
  264. $runtokey = $key_length ? ($operation == 'ENCODE' ? substr(hash('md5', microtime(true)), -$key_length) : substr($string, 0, $key_length)) : '';
  265. $keys = hash('md5', substr($runtokey, 0, 16) . substr($fixedkey, 0, 16) . substr($runtokey, 16) . substr($fixedkey, 16));
  266. $string = $operation == 'ENCODE' ? sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $egiskeys), 0, 16) . $string : base64_decode(substr($string, $key_length));
  267. $i = 0;
  268. $result = '';
  269. $string_length = strlen($string);
  270. for ($i = 0; $i < $string_length; $i++) {
  271. $result .= chr(ord($string{$i}) ^ ord($keys{$i % 32}));
  272. }
  273. if ($operation == 'ENCODE') {
  274. return $runtokey . str_replace('=', '', base64_encode($result));
  275. } else {
  276. if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $egiskeys), 0, 16)) {
  277. return substr($result, 26);
  278. } else {
  279. return '';
  280. }
  281. }
  282. }
  283. /**
  284. * 获取应用列表信息
  285. */
  286. public function zhsso_getUserbyName($uname) {
  287. return $this->_ps_send('getuserinfo', array('username' => $uname));
  288. }
  289. /**
  290. * 将数组转换为字符串
  291. *
  292. * @param array $data 数组
  293. * @param bool $isformdata 如果为0,则不使用new_stripslashes处理,可选参数,默认为1
  294. * @return string 返回字符串,如果,data为空,则返回空
  295. */
  296. public function array2string($data, $isformdata = 1) {
  297. if ($data == '')
  298. return '';
  299. if ($isformdata)
  300. $data = new_stripslashes($data);
  301. return var_export($data, TRUE);
  302. }
  303. public function auth_data($data) {
  304. $s = $sep = '';
  305. // foreach ($data as $k => $v) {
  306. // if (is_array($v)) {
  307. // $s2 = $sep2 = '';
  308. // foreach ($v as $k2 => $v2) {
  309. // $s2 .= "$sep2{$k}[$k2]=" . $this->_ps_stripslashes($v2);
  310. // $sep2 = '&';
  311. // }
  312. // $s .= $sep . $s2;
  313. // } else {
  314. // $s .= "$sep$k=" . $this->_ps_stripslashes($v);
  315. // }
  316. // $sep = '&';
  317. // }
  318. $s = http_build_query($data);
  319. $auth_s = 'data=' . urlencode($this->authcode($s, 'ENCODE'));
  320. return $auth_s;
  321. }
  322. /**
  323. * 发送数据
  324. * @param $action 操作
  325. * @param $data 数据
  326. */
  327. private function _ps_send($action, $data = null) {
  328. return $this->_ps_post($this->ps_api_url . "api/" . $action, 500000, $this->auth_data($data));
  329. }
  330. /**
  331. * post数据
  332. * @param string $url post的url
  333. * @param int $limit 返回的数据的长度
  334. * @param string $post post数据,字符串形式username='dalarge'&password='123456'
  335. * @param string $cookie 模拟 cookie,字符串形式username='dalarge'&password='123456'
  336. * @param string $ip ip地址
  337. * @param int $timeout 连接超时时间
  338. * @param bool $block 是否为阻塞模式
  339. * @return string 返回字符串
  340. */
  341. private function _ps_post($url, $limit = 0, $post = '', $cookie = '', $ip = '', $timeout = 15, $block = true) {
  342. $return = '';
  343. $matches = parse_url($url);
  344. $host = $matches['host'];
  345. // $path = $matches['path'] ? $matches['path'] . ($matches['query'] ? '?' . $matches['query'] : '') : '/';
  346. $path = $matches['path'] ? $matches['path'] : '/';
  347. $port = !empty($matches['port']) ? $matches['port'] : 80;
  348. $siteurl = $this->_get_url();
  349. if ($post) {
  350. $out = "POST $path HTTP/1.1\r\n";
  351. $out .= "Accept: */*\r\n";
  352. $out .= "Referer: " . $siteurl . "\r\n";
  353. $out .= "Accept-Language: zh-cn\r\n";
  354. $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  355. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  356. $out .= "Host: $host\r\n";
  357. $out .= 'Content-Length: ' . strlen($post) . "\r\n";
  358. $out .= "Connection: Close\r\n";
  359. $out .= "Cache-Control: no-cache\r\n";
  360. $out .= "Cookie: $cookie\r\n\r\n";
  361. $out .= $post;
  362. } else {
  363. $out = "GET $path HTTP/1.1\r\n";
  364. $out .= "Accept: */*\r\n";
  365. $out .= "Referer: " . $siteurl . "\r\n";
  366. $out .= "Accept-Language: zh-cn\r\n";
  367. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  368. $out .= "Host: $host\r\n";
  369. $out .= "Connection: Close\r\n";
  370. $out .= "Cookie: $cookie\r\n\r\n";
  371. }
  372. $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
  373. if (!$fp)
  374. return '';
  375. stream_set_blocking($fp, $block);
  376. stream_set_timeout($fp, $timeout);
  377. @fwrite($fp, $out);
  378. $status = stream_get_meta_data($fp);
  379. if ($status['timed_out'])
  380. return '';
  381. while (!feof($fp)) {
  382. if (($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n"))
  383. break;
  384. }
  385. $stop = false;
  386. while (!feof($fp) && !$stop) {
  387. $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
  388. $return .= $data;
  389. if ($limit) {
  390. $limit -= strlen($data);
  391. $stop = $limit <= 0;
  392. }
  393. }
  394. @fclose($fp);
  395. //部分虚拟主机返回数值有误,暂不确定原因,过滤返回数据格式
  396. $return_arr = explode("\n", $return);
  397. if (isset($return_arr[1])) {
  398. $return = trim($return_arr[1]);
  399. }
  400. unset($return_arr);
  401. return $return;
  402. }
  403. /**
  404. * 过滤字符串
  405. * @param $string
  406. */
  407. private function _ps_stripslashes($string) {
  408. !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
  409. if (MAGIC_QUOTES_GPC) {
  410. return stripslashes($string);
  411. } else {
  412. return $string;
  413. }
  414. }
  415. /**
  416. * 获取当前页面完整URL地址
  417. */
  418. private function _get_url() {
  419. $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
  420. $php_self = $_SERVER['PHP_SELF'] ? $this->_safe_replace($_SERVER['PHP_SELF']) : $this->_safe_replace($_SERVER['SCRIPT_NAME']);
  421. $path_info = isset($_SERVER['PATH_INFO']) ? $this->_safe_replace($_SERVER['PATH_INFO']) : '';
  422. $relate_url = isset($_SERVER['REQUEST_URI']) ? $this->_safe_replace($_SERVER['REQUEST_URI']) : $php_self . (isset($_SERVER['QUERY_STRING']) ? '?' . $this->_safe_replace($_SERVER['QUERY_STRING']) : $path_info);
  423. return $sys_protocal . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . $relate_url;
  424. }
  425. /**
  426. * 安全过滤函数
  427. *
  428. * @param $string
  429. * @return string
  430. */
  431. private function _safe_replace($string) {
  432. $string = str_replace('%20', '', $string);
  433. $string = str_replace('%27', '', $string);
  434. $string = str_replace('%2527', '', $string);
  435. $string = str_replace('*', '', $string);
  436. $string = str_replace('"', '&quot;', $string);
  437. $string = str_replace("'", '', $string);
  438. $string = str_replace('"', '', $string);
  439. $string = str_replace(';', '', $string);
  440. $string = str_replace('<', '&lt;', $string);
  441. $string = str_replace('>', '&gt;', $string);
  442. $string = str_replace("{", '', $string);
  443. $string = str_replace('}', '', $string);
  444. $string = str_replace('\\', '', $string);
  445. return $string;
  446. }
  447. /**
  448. * 金额增加
  449. * @param string $userid 用户唯一标示符
  450. * @param string $m 操作金额
  451. * @param int $appid 应用程序ID,标示操作来源{wenku,ask}
  452. * @return int {0;金额增加失败;1:金额增加成功;}
  453. */
  454. public function zhsso_member_madd($userid, $m, $appid) {
  455. if (empty($userid) || empty($m) || empty($appid))
  456. return false;
  457. return $this->_ps_send('madd', array('uid' => $userid, 'bank' => $m, 'appid' => $appid));
  458. }
  459. /**
  460. * 获取充值订单数据
  461. * @param unknown_type $userid
  462. */
  463. public function getPayOrder($userid) {
  464. if (empty($userid))
  465. return false;
  466. return $this->_ps_send('getPayOrder', array('uid' => $userid));
  467. }
  468. /**
  469. * 金额减少
  470. * @param string $userid 用户唯一标示符
  471. * @param string $m 操作金额
  472. * @param int $appid 应用程序ID,标示操作来源{wenku,ask}
  473. * @return int {-1;金额不够减;0;金额减少失败;1:金额减少成功;}
  474. */
  475. public function zhsso_member_msub($userid, $m, $appid) {
  476. if (empty($userid) || empty($m) || empty($appid))
  477. return false;
  478. return $this->_ps_send('msub', array('uid' => $userid, 'bank' => $m, 'appid' => $appid));
  479. }
  480. /**
  481. * 判断email格式是否正确
  482. * @param $string email
  483. */
  484. private function _is_email($email) {
  485. return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
  486. }
  487. /**
  488. * 判断手机格式是否正确
  489. * @param $string email
  490. */
  491. private function _is_mobile($mobile) {
  492. if(preg_match("/^1[34578]{1}\d{9}$/",$mobile)){
  493. return TRUE;
  494. } else {
  495. return FALSE;
  496. }
  497. }
  498. /**
  499. * 判断email格式是否正确
  500. * @param $string email
  501. */
  502. public function getUserName() {
  503. $username = self::get_cookie('_username');
  504. if ($username) {
  505. return $username;
  506. } else {
  507. return FALSE;
  508. }
  509. }
  510. /**
  511. * 判断email格式是否正确
  512. * @param $string email
  513. */
  514. public function getAuth() {
  515. $auth = self::get_cookie('auth');
  516. if ($auth) {
  517. return $auth;
  518. } else {
  519. return FALSE;
  520. }
  521. }
  522. /**
  523. * 判断email格式是否正确
  524. * @param $string email
  525. */
  526. public function getUserID() {
  527. $userid = self::get_cookie('_userid');
  528. if ($userid) {
  529. return $userid;
  530. } else {
  531. return FALSE;
  532. }
  533. }
  534. public function get_cookie($var, $default = '') {
  535. $varvar = COOKIEPRE_WD . $var;
  536. return isset($_COOKIE[$varvar]) ? $this->authcode($_COOKIE[$varvar]) : $default;
  537. }
  538. /**
  539. *
  540. * 网络传输、cookie加密函数
  541. * @param type $string
  542. * @param type $operation
  543. * @param type $key
  544. * @param type $expiry
  545. * @return string
  546. */
  547. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  548. $ckey_length = 4;
  549. $key = md5($key != '' ? $key : AUTHKEY);
  550. $keya = md5(substr($key, 0, 16));
  551. $keyb = md5(substr($key, 16, 16));
  552. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
  553. $cryptkey = $keya . md5($keya . $keyc);
  554. $key_length = strlen($cryptkey);
  555. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
  556. $string_length = strlen($string);
  557. $result = '';
  558. $box = range(0, 255);
  559. $rndkey = array();
  560. for ($i = 0; $i <= 255; $i++) {
  561. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  562. }
  563. for ($j = $i = 0; $i < 256; $i++) {
  564. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  565. $tmp = $box[$i];
  566. $box[$i] = $box[$j];
  567. $box[$j] = $tmp;
  568. }
  569. for ($a = $j = $i = 0; $i < $string_length; $i++) {
  570. $a = ($a + 1) % 256;
  571. $j = ($j + $box[$a]) % 256;
  572. $tmp = $box[$a];
  573. $box[$a] = $box[$j];
  574. $box[$j] = $tmp;
  575. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  576. }
  577. if ($operation == 'DECODE') {
  578. if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
  579. return substr($result, 26);
  580. } else {
  581. return '';
  582. }
  583. } else {
  584. return $keyc . str_replace('=', '', base64_encode($result));
  585. }
  586. }
  587. public function logoutz() {
  588. // header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
  589. setcookie(Doo::conf()->COOKIEPRE_WK . 'auth', '', 0, '/', 'localhost', 0);
  590. setcookie(Doo::conf()->COOKIEPRE_WK . '_username', '', 0, '/', 'localhost', 0);
  591. setcookie(Doo::conf()->COOKIEPRE_WK . '_userid', '', 0, '/', 'localhost', 0);
  592. }
  593. }
  594. ?>