PageRenderTime 38ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/b2b/core/lib/uc_client/client.php

http://phpfor.googlecode.com/
PHP | 440 lines | 357 code | 64 blank | 19 comment | 58 complexity | efbac4e22b52299c589ff32e82a7b8b9 MD5 | raw file
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2008 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: client.php 12180 2008-01-17 05:56:43Z heyond $
  6. */
  7. if(!defined('UC_API')) {
  8. exit('Access denied');
  9. }
  10. error_reporting(0);
  11. define('IN_UC', TRUE);
  12. define('UC_VERSION', '1.0.0');
  13. define('UC_RELEASE', '20080429');
  14. define('UC_ROOT', substr(__FILE__, 0, -10));
  15. define('UC_DATADIR', UC_ROOT.'./data/');
  16. define('UC_DATAURL', UC_API.'/data');
  17. define('UC_API_FUNC', UC_CONNECT == 'mysql' ? 'uc_api_mysql' : 'uc_api_post');
  18. $GLOBALS['uc_controls'] = array();
  19. function uc_addslashes($string, $force = 0, $strip = FALSE) {
  20. !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
  21. if(!MAGIC_QUOTES_GPC || $force) {
  22. if(is_array($string)) {
  23. foreach($string as $key => $val) {
  24. $string[$key] = uc_addslashes($val, $force, $strip);
  25. }
  26. } else {
  27. $string = addslashes($strip ? stripslashes($string) : $string);
  28. }
  29. }
  30. return $string;
  31. }
  32. function uc_stripslashes($string) {
  33. !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
  34. if(MAGIC_QUOTES_GPC) {
  35. return stripslashes($string);
  36. } else {
  37. return $string;
  38. }
  39. }
  40. function uc_api_post($module, $action, $arg = array()) {
  41. $s = $sep = '';
  42. foreach($arg as $k => $v) {
  43. if(is_array($v)) {
  44. $s2 = $sep2 = '';
  45. foreach($v as $k2=>$v2) {
  46. $s2 .= "$sep2{$k}[$k2]=".urlencode(uc_stripslashes($v2));
  47. $sep2 = '&';
  48. }
  49. $s .= $sep.$s2;
  50. } else {
  51. $s .= "$sep$k=".urlencode(uc_stripslashes($v));
  52. }
  53. $sep = '&';
  54. }
  55. $postdata = uc_api_requestdata($module, $action, $s);
  56. return uc_fopen2(UC_API.'/index.php', 500000, $postdata, '', TRUE, UC_IP, 20);
  57. }
  58. function uc_api_requestdata($module, $action, $arg='', $extra='') {
  59. $input = uc_api_input($arg);
  60. $post = "m=$module&a=$action&inajax=2&input=$input&appid=".UC_APPID.$extra;
  61. return $post;
  62. }
  63. function uc_api_url($module, $action, $arg='', $extra='') {
  64. $url = UC_API.'/index.php?'.uc_api_requestdata($module, $action, $arg, $extra);
  65. return $url;
  66. }
  67. function uc_api_input($data) {
  68. $s = urlencode(uc_authcode($data.'&agent='.md5($_SERVER['HTTP_USER_AGENT'])."&time=".time(), 'ENCODE', UC_KEY));
  69. return $s;
  70. }
  71. function uc_api_mysql($model, $action, $args=array()) {
  72. global $uc_controls;
  73. if(empty($uc_controls[$model])) {
  74. include_once UC_ROOT.'./lib/db.class.php';
  75. include_once UC_ROOT.'./model/base.php';
  76. include_once UC_ROOT."./control/$model.php";
  77. eval("\$uc_controls['$model'] = new {$model}control();");
  78. }
  79. if($action{0} != '_') {
  80. $args = uc_addslashes($args, 1, TRUE);
  81. $action = 'on'.$action;
  82. return $uc_controls[$model]->$action($args);
  83. } else {
  84. return '';
  85. }
  86. }
  87. function uc_serialize($arr, $htmlon = 0) {
  88. include_once UC_ROOT.'./lib/xml.class.php';
  89. return xml_serialize($arr, $htmlon);
  90. }
  91. function uc_unserialize($s) {
  92. include_once UC_ROOT.'./lib/xml.class.php';
  93. return xml_unserialize($s);
  94. }
  95. function uc_authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  96. $ckey_length = 4;
  97. $key = md5($key ? $key : UC_KEY);
  98. $keya = md5(substr($key, 0, 16));
  99. $keyb = md5(substr($key, 16, 16));
  100. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  101. $cryptkey = $keya.md5($keya.$keyc);
  102. $key_length = strlen($cryptkey);
  103. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  104. $string_length = strlen($string);
  105. $result = '';
  106. $box = range(0, 255);
  107. $rndkey = array();
  108. for($i = 0; $i <= 255; $i++) {
  109. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  110. }
  111. for($j = $i = 0; $i < 256; $i++) {
  112. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  113. $tmp = $box[$i];
  114. $box[$i] = $box[$j];
  115. $box[$j] = $tmp;
  116. }
  117. for($a = $j = $i = 0; $i < $string_length; $i++) {
  118. $a = ($a + 1) % 256;
  119. $j = ($j + $box[$a]) % 256;
  120. $tmp = $box[$a];
  121. $box[$a] = $box[$j];
  122. $box[$j] = $tmp;
  123. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  124. }
  125. if($operation == 'DECODE') {
  126. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  127. return substr($result, 26);
  128. } else {
  129. return '';
  130. }
  131. } else {
  132. return $keyc.str_replace('=', '', base64_encode($result));
  133. }
  134. }
  135. function uc_fopen2($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE) {
  136. $__times__ = isset($_GET['__times__']) ? intval($_GET['__times__']) + 1 : 1;
  137. if($__times__ > 2) {
  138. return '';
  139. }
  140. $url .= (strpos($url, '?') === FALSE ? '?' : '&')."__times__=$__times__";
  141. $data = uc_fopen($url, $limit, $post, $cookie, $bysocket, $ip, $timeout, $block);
  142. return $data;
  143. }
  144. function uc_fopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE) {
  145. $return = '';
  146. $matches = parse_url($url);
  147. $host = $matches['host'];
  148. $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
  149. $port = !empty($matches['port']) ? $matches['port'] : 80;
  150. if($post) {
  151. $out = "POST $path HTTP/1.0\r\n";
  152. $out .= "Accept: */*\r\n";
  153. //$out .= "Referer: $boardurl\r\n";
  154. $out .= "Accept-Language: zh-cn\r\n";
  155. $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  156. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  157. $out .= "Host: $host\r\n";
  158. $out .= 'Content-Length: '.strlen($post)."\r\n";
  159. $out .= "Connection: Close\r\n";
  160. $out .= "Cache-Control: no-cache ,no-store \r\n";
  161. $out .= "Cookie: $cookie\r\n\r\n";
  162. $out .= $post;
  163. } else {
  164. $out = "GET $path HTTP/1.0\r\n";
  165. $out .= "Accept: */*\r\n";
  166. //$out .= "Referer: $boardurl\r\n";
  167. $out .= "Accept-Language: zh-cn\r\n";
  168. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  169. $out .= "Host: $host\r\n";
  170. $out .= "Connection: Close\r\n";
  171. $out .= "Cookie: $cookie\r\n\r\n";
  172. }
  173. $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
  174. if(!$fp) {
  175. return '';
  176. } else {
  177. stream_set_blocking($fp, $block);
  178. stream_set_timeout($fp, $timeout);
  179. @fwrite($fp, $out);
  180. $status = stream_get_meta_data($fp);
  181. if(!$status['timed_out']) {
  182. while (!feof($fp)) {
  183. if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) {
  184. break;
  185. }
  186. }
  187. $stop = false;
  188. while(!feof($fp) && !$stop) {
  189. $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
  190. $return .= $data;
  191. if($limit) {
  192. $limit -= strlen($data);
  193. $stop = $limit <= 0;
  194. }
  195. }
  196. }
  197. @fclose($fp);
  198. return $return;
  199. }
  200. }
  201. function uc_app_ls() {
  202. $return = call_user_func(UC_API_FUNC, 'app', 'ls', array());
  203. return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
  204. }
  205. function uc_feed_add($icon, $uid, $username, $title_template='', $title_data='', $body_template='', $body_data='', $body_general='', $target_ids='', $images = array()) {
  206. call_user_func(UC_API_FUNC, 'feed', 'add',
  207. array( 'icon'=>$icon,
  208. 'appid'=>UC_APPID,
  209. 'uid'=>$uid,
  210. 'username'=>$username,
  211. 'title_template'=>$title_template,
  212. 'title_data'=>$title_data,
  213. 'body_template'=>$body_template,
  214. 'body_data'=>$body_data,
  215. 'body_general'=>$body_general,
  216. 'target_ids'=>$target_ids,
  217. 'image_1'=>$images[0]['url'],
  218. 'image_1_link'=>$images[0]['link'],
  219. 'image_2'=>$images[1]['url'],
  220. 'image_2_link'=>$images[1]['link'],
  221. 'image_3'=>$images[2]['url'],
  222. 'image_3_link'=>$images[2]['link'],
  223. 'image_4'=>$images[3]['url'],
  224. 'image_4_link'=>$images[3]['link']
  225. )
  226. );
  227. }
  228. function uc_feed_get($limit = 100) {
  229. $return = call_user_func(UC_API_FUNC, 'feed', 'get', array('limit'=>$limit));
  230. return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
  231. }
  232. function uc_friend_add($uid, $friendid, $comment='') {
  233. return call_user_func(UC_API_FUNC, 'friend', 'add', array('uid'=>$uid, 'friendid'=>$friendid, 'comment'=>$comment));
  234. }
  235. function uc_friend_delete($uid, $friendids) {
  236. return call_user_func(UC_API_FUNC, 'friend', 'delete', array('uid'=>$uid, 'friendids'=>$friendids));
  237. }
  238. function uc_friend_totalnum($uid, $direction = 0) {
  239. return call_user_func(UC_API_FUNC, 'friend', 'totalnum', array('uid'=>$uid, 'direction'=>$direction));
  240. }
  241. function uc_friend_ls($uid, $page = 1, $pagesize = 10, $totalnum = 10, $direction = 0) {
  242. $return = call_user_func(UC_API_FUNC, 'friend', 'ls', array('uid'=>$uid, 'page'=>$page, 'pagesize'=>$pagesize, 'totalnum'=>$totalnum, 'direction'=>$direction));
  243. return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
  244. }
  245. function uc_user_register($username, $password, $email) {
  246. return call_user_func(UC_API_FUNC, 'user', 'register', array('username'=>$username, 'password'=>$password, 'email'=>$email));
  247. }
  248. function uc_user_login($username, $password, $isuid=0) {
  249. $isuid = intval($isuid);
  250. $return = call_user_func(UC_API_FUNC, 'user', 'login', array('username'=>$username, 'password'=>$password, 'isuid'=>$isuid));
  251. return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
  252. }
  253. function uc_user_synlogin($uid) {
  254. return uc_api_post('user', 'synlogin', array('uid'=>$uid));
  255. }
  256. function uc_user_synlogout() {
  257. return uc_api_post('user', 'synlogout', array('uid'=>$uid));
  258. }
  259. function uc_user_edit($username, $oldpw, $newpw, $email, $ignoreoldpw = 0) {
  260. return call_user_func(UC_API_FUNC, 'user', 'edit', array('username'=>$username, 'oldpw'=>$oldpw, 'newpw'=>$newpw, 'email'=>$email, 'ignoreoldpw'=>$ignoreoldpw));
  261. }
  262. function uc_user_delete($uid) {
  263. return call_user_func(UC_API_FUNC, 'user', 'delete', array('uid'=>$uid));
  264. }
  265. function uc_user_checkname($username) {
  266. return call_user_func(UC_API_FUNC, 'user', 'check_username', array('username'=>$username));
  267. }
  268. function uc_user_checkemail($email) {
  269. return call_user_func(UC_API_FUNC, 'user', 'check_email', array('email'=>$email));
  270. }
  271. function uc_user_addprotected($username, $admin='') {
  272. return call_user_func(UC_API_FUNC, 'user', 'addprotected', array('username'=>$username, 'admin'=>$admin));
  273. }
  274. function uc_user_deleteprotected($username) {
  275. return call_user_func(UC_API_FUNC, 'user', 'deleteprotected', array('username'=>$username));
  276. }
  277. function uc_user_getprotected() {
  278. $return = call_user_func(UC_API_FUNC, 'user', 'getprotected', array('1'=>1));
  279. return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
  280. }
  281. function uc_get_user($username, $isuid=0) {
  282. $return = call_user_func(UC_API_FUNC, 'user', 'get_user', array('username'=>$username, 'isuid'=>$isuid));
  283. return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
  284. }
  285. function uc_user_merge($oldusername, $newusername, $uid, $password, $email) {
  286. return call_user_func(UC_API_FUNC, 'user', 'merge', array('oldusername'=>$oldusername, 'newusername'=>$newusername, 'uid'=>$uid, 'password'=>$password, 'email'=>$email));
  287. }
  288. function uc_user_allmerge($data,&$uidGroup){
  289. return call_user_func(UC_API_FUNC, 'user','allmerge',array($data,&$uidGroup));
  290. }
  291. function uc_pm_location($uid, $newpm = 0) {
  292. $apiurl = uc_api_url('pm_client', 'ls', "uid=$uid", ($newpm ? '&folder=newbox' : ''));
  293. @header("Expires: 0");
  294. @header("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
  295. @header("Pragma: no-cache,no-store");
  296. @header("location: $apiurl");
  297. }
  298. function uc_pm_checknew($uid) {
  299. return call_user_func(UC_API_FUNC, 'pm', 'check_newpm', array('uid'=>$uid));
  300. }
  301. function uc_pm_send($fromuid, $msgto, $subject, $message, $instantly = 1, $replypmid = 0, $isusername = 0) {
  302. if($instantly) {
  303. $replypmid = @is_numeric($replypmid) ? $replypmid : 0;
  304. return call_user_func(UC_API_FUNC, 'pm', 'sendpm', array('fromuid'=>$fromuid, 'msgto'=>$msgto, 'subject'=>$subject, 'message'=>$message, 'replypmid'=>$replypmid, 'isusername'=>$isusername));
  305. } else {
  306. $fromuid = intval($fromuid);
  307. $subject = urlencode($subject);
  308. $msgto = urlencode($msgto);
  309. $message = urlencode($message);
  310. $replypmid = @is_numeric($replypmid) ? $replypmid : 0;
  311. $replyadd = $replypmid ? "&pmid=$replypmid&do=reply" : '';
  312. $apiurl = uc_api_url('pm_client', 'send', "uid=$fromuid", "&msgto=$msgto&subject=$subject&message=$message$replyadd");
  313. @header("Expires: 0");
  314. @header("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
  315. @header("Pragma: no-cache,no-store");
  316. @header("location: ".$apiurl);
  317. }
  318. }
  319. function uc_pm_delete($uid, $folder, $pmids) {
  320. return call_user_func(UC_API_FUNC, 'pm', 'delete', array('uid'=>$uid, 'folder'=>$folder, 'pmids'=>$pmids));
  321. }
  322. function uc_pm_list($uid, $page = 1, $pagesize = 10, $folder = 'inbox', $filter = 'newpm', $msglen = 0) {
  323. $uid = intval($uid);
  324. $page = intval($page);
  325. $pagesize = intval($pagesize);
  326. $return = call_user_func(UC_API_FUNC, 'pm', 'ls', array('uid'=>$uid, 'page'=>$page, 'pagesize'=>$pagesize, 'folder'=>$folder, 'filter'=>$filter, 'msglen'=>$msglen));
  327. return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
  328. }
  329. function uc_pm_ignore($uid) {
  330. $uid = intval($uid);
  331. return call_user_func(UC_API_FUNC, 'pm', 'ignore', array('uid'=>$uid));
  332. }
  333. function uc_pm_view($uid, $pmid) {
  334. $uid = intval($uid);
  335. $pmid = @is_numeric($pmid) ? $pmid : 0;
  336. $return = call_user_func(UC_API_FUNC, 'pm', 'view', array('uid'=>$uid, 'pmid'=>$pmid));
  337. return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
  338. }
  339. function uc_pm_viewnode($uid, $type = 0, $pmid = 0) {
  340. $uid = intval($uid);
  341. $pmid = @is_numeric($pmid) ? $pmid : 0;
  342. $return = call_user_func(UC_API_FUNC, 'pm', 'viewnode', array('uid'=>$uid, 'pmid'=>$pmid, 'type'=>$type));
  343. return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
  344. }
  345. function uc_pm_blackls_get($uid) {
  346. $uid = intval($uid);
  347. return call_user_func(UC_API_FUNC, 'pm', 'blackls_get', array('uid'=>$uid));
  348. }
  349. function uc_pm_blackls_set($uid, $blackls) {
  350. $uid = intval($uid);
  351. return call_user_func(UC_API_FUNC, 'pm', 'blackls_set', array('uid'=>$uid, 'blackls'=>$blackls));
  352. }
  353. function uc_domain_ls() {
  354. $return = call_user_func(UC_API_FUNC, 'domain', 'ls', array('1'=>1));
  355. return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
  356. }
  357. function uc_credit_exchange_request($uid, $from, $to, $toappid, $amount) {
  358. $uid = intval($uid);
  359. $from = intval($from);
  360. $toappid = intval($toappid);
  361. $to = intval($to);
  362. $amount = intval($amount);
  363. return uc_api_post('credit', 'request', array('uid'=>$uid, 'from'=>$from, 'to'=>$to, 'toappid'=>$toappid, 'amount'=>$amount));
  364. }
  365. function uc_tag_get($tagname, $nums = 0) {
  366. $return = call_user_func(UC_API_FUNC, 'tag', 'gettag', array('tagname'=>$tagname, 'nums'=>$nums));
  367. return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
  368. }
  369. function uc_avatar($uid) {
  370. $uid = intval($uid);
  371. $uc_input = uc_api_input("uid=$uid");
  372. $uc_avatarflash = UC_API.'/images/camera.swf?inajax=1&appid='.UC_APPID.'&input='.$uc_input.'&agent='.md5($_SERVER['HTTP_USER_AGENT']).'&ucapi='.urlencode(UC_API);
  373. return '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9" width=447 height=477 id="mycamera"><param name="movie" value="'.$uc_avatarflash.'"><param name="quality" value="high"><param name="menu" value="false"><embed src="'.$uc_avatarflash.'" quality="high" menu="false" width="447" height="477" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" name="mycamera" swLiveConnect="true"></embed></object>';
  374. }
  375. ?>