client.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 ps_member_login($username, $password, $isemail=0) {
  37. if($isemail) {
  38. if(!$this->_is_email($username)) {
  39. return -3;
  40. }
  41. $return = $this->_ps_send('login', array('email'=>$username, 'password'=>$password));
  42. } else {
  43. $return = $this->_ps_send('login', array('username'=>$username, 'password'=>$password));
  44. }
  45. return $return;
  46. }
  47. /**
  48. * 同步登陆
  49. * @param string $uid
  50. * @return string javascript用户同步登陆js
  51. */
  52. public function ps_member_synlogin($uid) {
  53. $uid = intval($uid);
  54. return $this->_ps_send('synlogin', array('uid'=>$uid));
  55. }
  56. /**
  57. * 同步退出
  58. * @param string $uid
  59. * @return string javascript用户同步退出js
  60. */
  61. public function ps_member_synlogout() {
  62. return $this->_ps_send('synlogout', array());
  63. }
  64. /**
  65. * 编辑用户
  66. * @param string $username 用户名
  67. * @param string $email email
  68. * @param string $password 旧密码
  69. * @param string $newpassword 新密码
  70. * @param int $uid phpsso用户uid
  71. * @param string $random 密码随机数
  72. * @return int {-1:用户不存在;-2:旧密码错误;-3:email已经存在 ;-4:email格式错误;1:成功;0:未作修改,-5:参数格式错误}
  73. */
  74. public function ps_member_edit($username, $email, $password='', $newpassword='', $uid='', $random='') {
  75. if($email && !$this->_is_email($email)) {
  76. return -4;
  77. }
  78. if ((!empty($username) && !is_string($username)) || (!empty($email) && !is_string($email)) || (!empty($password) && !is_string($password)) || (!empty($newpassword) && !is_string($newpassword))) {
  79. return -5;
  80. }
  81. return $this->_ps_send('edit', array('username'=>$username, 'password'=>$password, 'newpassword'=>$newpassword, 'email'=>$email, 'uid'=>$uid, 'random'=>$random));
  82. }
  83. /**
  84. * 删除用户头像
  85. * @param int $uid phpsso用户uid
  86. * @return int {1:成功;0:失败}
  87. */
  88. public function ps_deleteavatar($uid) {
  89. return $this->_ps_send('deleteavatar', array('uid'=>$uid));
  90. }
  91. /**
  92. * 获取用户信息
  93. * @param $mix 用户id/用户名/email
  94. * @param $type {1:用户id;2:用户名;3:email}
  95. * @return $mix {-1:用户不存在;userinfo:用户信息}
  96. */
  97. public function ps_get_member_info($mix, $type=1) {
  98. if($type==1) {
  99. $userinfo = $this->_ps_send('getuserinfo', array('uid'=>$mix));
  100. } elseif($type==2) {
  101. $userinfo = $this->_ps_send('getuserinfo', array('username'=>$mix));
  102. } elseif($type==3) {
  103. if(!$this->_is_email($mix)) {
  104. return -4;
  105. }
  106. $userinfo = $this->_ps_send('getuserinfo', array('email'=>$mix));
  107. }
  108. if($userinfo) {
  109. return $userinfo;
  110. } else {
  111. return -1;
  112. }
  113. }
  114. /**
  115. * 删除用户
  116. * @param mix {1:用户id;2:用户名;3:email} 如果是用户id可以为数组
  117. * @return int {-1:用户不存在;1:删除成功}
  118. */
  119. public function ps_delete_member($mix, $type=1) {
  120. if($type==1) {
  121. $res = $this->_ps_send('delete', array('uid'=>$mix));
  122. } elseif($type==2) {
  123. $res = $this->_ps_send('delete', array('username'=>$mix));
  124. } elseif($type==3) {
  125. if(!$this->_is_email($mix)) {
  126. return -4;
  127. }
  128. $res = $this->_ps_send('delete', array('email'=>$mix));
  129. }
  130. return $res;
  131. }
  132. /**
  133. * 检查用户是否可以注册
  134. * @param string $username
  135. * @return int {-4:用户名禁止注册;-1:用户名已经存在 ;1:成功}
  136. */
  137. public function ps_checkname($username) {
  138. return $this->_ps_send('checkname', array('username'=>$username));
  139. }
  140. /**
  141. * 检查邮箱是否可以注册
  142. * @param string $email
  143. * @return int {-1:email已经存在 ;-5:邮箱禁止注册;1:成功}
  144. */
  145. public function ps_checkemail($email) {
  146. return $this->_ps_send('checkemail', array('email'=>$email));
  147. }
  148. /**
  149. * 获取应用列表信息
  150. */
  151. public function ps_getapplist() {
  152. return $this->_ps_send('getapplist', array());
  153. }
  154. /**
  155. * 获取积分兑换比例列表
  156. */
  157. public function ps_getcreditlist() {
  158. return $this->_ps_send('getcredit', array());
  159. }
  160. /**
  161. * 兑换积分
  162. * 用于何其他应用之间积分兑换
  163. * @param int $uid phpssouid
  164. * @param int $from 本系统积分类型id
  165. * @param int $toappid 目标系统应用appid
  166. * @param int $to 目标系统积分类型id
  167. * @param int $credit 本系统扣除积分数
  168. * @return bool {1:成功;0:失败}
  169. */
  170. public function ps_changecredit($uid, $from, $toappid, $to, $credit) {
  171. return $this->_ps_send('changecredit', array('uid'=>$uid, 'from'=>$from, 'toappid'=>$toappid, 'to'=>$to, 'credit'=>$credit));
  172. }
  173. /**
  174. * 根据phpsso uid获取头像url
  175. * @param int $uid 用户id
  176. * @return array 四个尺寸用户头像数组
  177. */
  178. public function ps_getavatar($uid) {
  179. $dir1 = ceil($uid / 10000);
  180. $dir2 = ceil($uid % 10000 / 1000);
  181. $url = $this->ps_api_url.'/uploadfile/avatar/'.$dir1.'/'.$dir2.'/'.$uid.'/';
  182. $avatar = array('180'=>$url.'180x180.jpg', '90'=>$url.'90x90.jpg', '45'=>$url.'45x45.jpg', '30'=>$url.'30x30.jpg');
  183. return $avatar;
  184. }
  185. /**
  186. * 获取上传头像flash的html代码
  187. * @param int $uid 用户id
  188. */
  189. public function ps_getavatar_upload_html($uid) {
  190. $auth_data = $this->auth_data(array('uid'=>$uid, 'ps_auth_key'=>$this->ps_auth_key), '', $this->ps_auth_key);
  191. $upurl = base64_encode($this->ps_api_url.'/index.php?m=phpsso&c=index&a=uploadavatar&auth_data='.$auth_data);
  192. $str = <<<EOF
  193. <div id="phpsso_uploadavatar_flash"></div>
  194. <script language="javascript" type="text/javascript" src="{$this->ps_api_url}/statics/js/swfobject.js"></script>
  195. <script type="text/javascript">
  196. var flashvars = {
  197. 'upurl':"{$upurl}&callback=return_avatar&"
  198. };
  199. var params = {
  200. 'align':'middle',
  201. 'play':'true',
  202. 'loop':'false',
  203. 'scale':'showall',
  204. 'wmode':'window',
  205. 'devicefont':'true',
  206. 'id':'Main',
  207. 'bgcolor':'#ffffff',
  208. 'name':'Main',
  209. 'allowscriptaccess':'always'
  210. };
  211. var attributes = {
  212. };
  213. swfobject.embedSWF("{$this->ps_api_url}/statics/images/main.swf", "phpsso_uploadavatar_flash", "490", "434", "9.0.0","{$this->ps_api_url}/statics/images/expressInstall.swf", flashvars, params, attributes);
  214. function return_avatar(data) {
  215. if(data == 1) {
  216. window.location.reload();
  217. } else {
  218. alert('failure');
  219. }
  220. }
  221. </script>
  222. EOF;
  223. return $str;
  224. }
  225. /**
  226. * 字符串加密、解密函数
  227. *
  228. *
  229. * @param string $txt 字符串
  230. * @param string $operation ENCODE为加密,DECODE为解密,可选参数,默认为ENCODE,
  231. * @param string $key 密钥:数字、字母、下划线
  232. * @param string $expiry 过期时间
  233. * @return string
  234. */
  235. function sys_auth($string, $operation = 'ENCODE', $key = '', $expiry = 0) {
  236. $ckey_length = 4;
  237. $key = md5($key != '' ? $key : $this->ps_auth_key);
  238. $keya = md5(substr($key, 0, 16));
  239. $keyb = md5(substr($key, 16, 16));
  240. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  241. $cryptkey = $keya.md5($keya.$keyc);
  242. $key_length = strlen($cryptkey);
  243. $string = $operation == 'DECODE' ? base64_decode(strtr(substr($string, $ckey_length), '-_', '+/')) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  244. $string_length = strlen($string);
  245. $result = '';
  246. $box = range(0, 255);
  247. $rndkey = array();
  248. for($i = 0; $i <= 255; $i++) {
  249. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  250. }
  251. for($j = $i = 0; $i < 256; $i++) {
  252. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  253. $tmp = $box[$i];
  254. $box[$i] = $box[$j];
  255. $box[$j] = $tmp;
  256. }
  257. for($a = $j = $i = 0; $i < $string_length; $i++) {
  258. $a = ($a + 1) % 256;
  259. $j = ($j + $box[$a]) % 256;
  260. $tmp = $box[$a];
  261. $box[$a] = $box[$j];
  262. $box[$j] = $tmp;
  263. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  264. }
  265. if($operation == 'DECODE') {
  266. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  267. return substr($result, 26);
  268. } else {
  269. return '';
  270. }
  271. } else {
  272. return $keyc.rtrim(strtr(base64_encode($result), '+/', '-_'), '=');
  273. }
  274. }
  275. /**
  276. * 将数组转换为字符串
  277. *
  278. * @param array $data 数组
  279. * @param bool $isformdata 如果为0,则不使用new_stripslashes处理,可选参数,默认为1
  280. * @return string 返回字符串,如果,data为空,则返回空
  281. */
  282. public function array2string($data, $isformdata = 1) {
  283. if($data == '') return '';
  284. if($isformdata) $data = new_stripslashes($data);
  285. return var_export($data, TRUE);
  286. }
  287. public function auth_data($data) {
  288. $s = $sep = '';
  289. foreach($data as $k => $v) {
  290. if(is_array($v)) {
  291. $s2 = $sep2 = '';
  292. foreach($v as $k2 => $v2) {
  293. $s2 .= "$sep2{$k}[$k2]=".$this->_ps_stripslashes($v2);
  294. $sep2 = '&';
  295. }
  296. $s .= $sep.$s2;
  297. } else {
  298. $s .= "$sep$k=".$this->_ps_stripslashes($v);
  299. }
  300. $sep = '&';
  301. }
  302. $auth_s = 'v='.$this->ps_vsersion.'&appid='.APPID.'&data='.urlencode($this->sys_auth($s));
  303. return $auth_s;
  304. }
  305. /**
  306. * 发送数据
  307. * @param $action 操作
  308. * @param $data 数据
  309. */
  310. private function _ps_send($action, $data = null) {
  311. return $this->_ps_post($this->ps_api_url."/index.php?m=phpsso&c=index&a=".$action, 500000, $this->auth_data($data));
  312. }
  313. /**
  314. * post数据
  315. * @param string $url post的url
  316. * @param int $limit 返回的数据的长度
  317. * @param string $post post数据,字符串形式username='dalarge'&password='123456'
  318. * @param string $cookie 模拟 cookie,字符串形式username='dalarge'&password='123456'
  319. * @param string $ip ip地址
  320. * @param int $timeout 连接超时时间
  321. * @param bool $block 是否为阻塞模式
  322. * @return string 返回字符串
  323. */
  324. private function _ps_post($url, $limit = 0, $post = '', $cookie = '', $ip = '', $timeout = 15, $block = true) {
  325. $return = '';
  326. $matches = parse_url($url);
  327. $host = $matches['host'];
  328. $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
  329. $port = !empty($matches['port']) ? $matches['port'] : 80;
  330. $siteurl = $this->_get_url();
  331. if($post) {
  332. $out = "POST $path HTTP/1.1\r\n";
  333. $out .= "Accept: */*\r\n";
  334. $out .= "Referer: ".$siteurl."\r\n";
  335. $out .= "Accept-Language: zh-cn\r\n";
  336. $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  337. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  338. $out .= "Host: $host\r\n" ;
  339. $out .= 'Content-Length: '.strlen($post)."\r\n" ;
  340. $out .= "Connection: Close\r\n" ;
  341. $out .= "Cache-Control: no-cache\r\n" ;
  342. $out .= "Cookie: $cookie\r\n\r\n" ;
  343. $out .= $post ;
  344. } else {
  345. $out = "GET $path HTTP/1.1\r\n";
  346. $out .= "Accept: */*\r\n";
  347. $out .= "Referer: ".$siteurl."\r\n";
  348. $out .= "Accept-Language: zh-cn\r\n";
  349. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  350. $out .= "Host: $host\r\n";
  351. $out .= "Connection: Close\r\n";
  352. $out .= "Cookie: $cookie\r\n\r\n";
  353. }
  354. $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
  355. if(!$fp) return '';
  356. stream_set_blocking($fp, $block);
  357. stream_set_timeout($fp, $timeout);
  358. @fwrite($fp, $out);
  359. $status = stream_get_meta_data($fp);
  360. if($status['timed_out']) return '';
  361. while (!feof($fp)) {
  362. if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) break;
  363. }
  364. $stop = false;
  365. while(!feof($fp) && !$stop) {
  366. $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
  367. $return .= $data;
  368. if($limit) {
  369. $limit -= strlen($data);
  370. $stop = $limit <= 0;
  371. }
  372. }
  373. @fclose($fp);
  374. //部分虚拟主机返回数值有误,暂不确定原因,过滤返回数据格式
  375. $return_arr = explode("\n", $return);
  376. if(isset($return_arr[1])) {
  377. $return = trim($return_arr[1]);
  378. }
  379. unset($return_arr);
  380. return $return;
  381. }
  382. /**
  383. * 过滤字符串
  384. * @param $string
  385. */
  386. private function _ps_stripslashes($string) {
  387. !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
  388. if(MAGIC_QUOTES_GPC) {
  389. return stripslashes($string);
  390. } else {
  391. return $string;
  392. }
  393. }
  394. /**
  395. * 获取当前页面完整URL地址
  396. */
  397. private function _get_url() {
  398. $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
  399. $php_self = $_SERVER['PHP_SELF'] ? $this->_safe_replace($_SERVER['PHP_SELF']) : $this->_safe_replace($_SERVER['SCRIPT_NAME']);
  400. $path_info = isset($_SERVER['PATH_INFO']) ? $this->_safe_replace($_SERVER['PATH_INFO']) : '';
  401. $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);
  402. return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
  403. }
  404. /**
  405. * 安全过滤函数
  406. *
  407. * @param $string
  408. * @return string
  409. */
  410. private function _safe_replace($string) {
  411. $string = str_replace('%20','',$string);
  412. $string = str_replace('%27','',$string);
  413. $string = str_replace('%2527','',$string);
  414. $string = str_replace('*','',$string);
  415. $string = str_replace('"','&quot;',$string);
  416. $string = str_replace("'",'',$string);
  417. $string = str_replace('"','',$string);
  418. $string = str_replace(';','',$string);
  419. $string = str_replace('<','&lt;',$string);
  420. $string = str_replace('>','&gt;',$string);
  421. $string = str_replace("{",'',$string);
  422. $string = str_replace('}','',$string);
  423. $string = str_replace('\\','',$string);
  424. return $string;
  425. }
  426. /**
  427. * 判断email格式是否正确
  428. * @param $string email
  429. */
  430. private function _is_email($email) {
  431. return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
  432. }
  433. }
  434. ?>