代码解读--unclient/client.php_1
时间:2009-08-22 来源:sinkingboat
uc_client的主要功能是对UCHome用户的处理,包括:登录、注册、删除等。
uc_client的文件结构
/control 控制文件
/data 一些信息的缓存
/lib 要用到的一些类文件
/model 直接对数据库进行操作
因为uc_client,只是一个接口,让uchome或是bbs来调用,所以他没有MVC中的V。
首先我对uc_client的引入文件开始分析:client.php 文件进行分析 通过这个文件,处理不同的关于用户的信息操作
0001.<?php 0002. 0003./** 0004. * 文件名称:client.php 0005. * 功能:通过调用这个文件的不同函数,寻找不同的控制器与不同的方法 0006. * 0007. * @copyright 2009 ymaozi Site:www.codedesign.cn 0008. * QQ群:83400173 0009. * 分析时间:2009-6-7 0010. */ 0011. 0012.if(!defined('UC_API')) { 0013. exit('Access dened'); 0014.} //是否定义UC_API,在config.php定义的是ucenter的url 0015. 0016.error_reporting(0); //屏蔽所有的错误 0017. 0018.define('IN_UC', TRUE); //定义IN_UC,以防直接访问控制文件 0019.define('UC_CLIENT_VERSION', '1.5.0'); //定义版本 0020.define('UC_CLIENT_RELEASE', '20081212'); //定义版本更新时间 0021.define('UC_ROOT', substr(__FILE__, 0, -10)); //note 用户中心客户端的根目录 UC_CLIENTROOT 0022.define('UC_DATADIR', UC_ROOT.'./data/'); //note 用户中心的数据缓存目录 0023.define('UC_DATAURL', UC_API.'/data'); //note 用户中心的数据 URL 0024.define('UC_API_FUNC', UC_CONNECT == 'mysql' ? 'uc_api_mysql' : 'uc_api_post'); //如果数据库是mysql则执行uc_api_mysql()函数,否则则执行uc_api_post 0025.$GLOBALS['uc_controls'] = array(); //定义一全局数组 0026./** 0027. * 在执行sql语句时,在需要在某些字符前加上了反斜线,这些字符有单引号(')、双引号(")、反斜线(\) 0028. * 0029. * @param string/array $string 如果$string 为字符串,则直接用转换,如果是数组的话,通过foreach循环遂个进行转换 0030. * @param bool $force 0031. * @param bool $strip 0032. * @return string 0033. * get_magic_quotes_gpc():本函式取得 PHP 环境设定的变数 magic_quotes_gpc (GPC, Get/Post/Cookie) 值。如果传回0为关闭,1为打开 0034. * string addslashes(string str) 在某些字符前加上了反斜线 0035. * string stripslashes (string str) 将用addslashes()函数处理后的字符串返回原样。 0036. */<span id="more-11"></span> 0037.function uc_addslashes($string, $force = 0, $strip = FALSE) { 0038. !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc()); //是否定义MAGIC_QUOTES_GPC,没有的话就定义 0039. if(!MAGIC_QUOTES_GPC || $force) { 0040. if(is_array($string)) { 0041. foreach($string as $key => $val) { 0042. $string[$key] = uc_addslashes($val, $force, $strip); 0043. } 0044. } else { 0045. $string = addslashes($strip ? stripslashes($string) : $string); 0046. } 0047. } 0048. return $string; 0049.} 0050.//判断是否存在daddslashes函数,如果不存在则定义 0051.if(!function_exists('daddslashes')) { 0052. 0053. /** 0054. * 在需要在某些字符前加上了反斜线 0055. * 0056. * @param string $string 0057. * @param bool $force 0058. * @return string 0059. */ 0060. function daddslashes($string, $force = 0) { 0061. return uc_addslashes($string, $force); 0062. } 0063.} 0064. 0065./** 0066. * 将用addslashes()函数处理后的字符串返回原样 0067. * 0068. * @param string $string 0069. * @return string 0070. */ 0071.function uc_stripslashes($string) { 0072. !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc()); 0073. if(MAGIC_QUOTES_GPC) { 0074. return stripslashes($string); 0075. } else { 0076. return $string; 0077. } 0078.} 0079. 0080./** 0081. * dfopen 方式取指定的模块和动作的数据 0082. * 0083. * @param string $module 请求的模块 0084. * @param string $action 请求的动作 0085. * @param array $arg 参数(会加密的方式传送) 0086. * @return string 0087. */ 0088.function uc_api_post($module, $action, $arg = array()) { 0089. $s = $sep = ''; 0090. foreach($arg as $k => $v) { 0091. $k = urlencode($k); 0092. if(is_array($v)) { 0093. $s2 = $sep2 = ''; 0094. foreach($v as $k2 => $v2) { 0095. $k2 = urlencode($k2); 0096. $s2 .= "$sep2{$k}[$k2]=".urlencode(uc_stripslashes($v2)); 0097. $sep2 = '&'; 0098. } 0099. $s .= $sep.$s2; 0100. } else { 0101. $s .= "$sep$k=".urlencode(uc_stripslashes($v)); 0102. } 0103. $sep = '&'; 0104. } 0105. $postdata = uc_api_requestdata($module, $action, $s); 0106. return uc_fopen2(UC_API.'/index.php', 500000, $postdata, '', TRUE, UC_IP, 20); 0107.} 0108. 0109./** 0110. * 构造发送给用户中心的请求数据 0111. * 0112. * @param string $module 请求的模块 0113. * @param string $action 请求的动作 0114. * @param string $arg 参数(会加密的方式传送) 0115. * @param string $extra 附加参数(传送时不加密) 0116. * @return string 0117. */ 0118.function uc_api_requestdata($module, $action, $arg='', $extra='') { 0119. $input = uc_api_input($arg); 0120. $post = "m=$module&a=$action&inajax=2&release=".UC_CLIENT_RELEASE."&input=$input&appid=".UC_APPID.$extra; 0121. return $post; 0122.} 0123. 0124.function uc_api_url($module, $action, $arg='', $extra='') { 0125. $url = UC_API.'/index.php?'.uc_api_requestdata($module, $action, $arg, $extra); 0126. return $url; 0127.} 0128. 0129.function uc_api_input($data) { 0130. $s = urlencode(uc_authcode($data.'&agent='.md5($_SERVER['HTTP_USER_AGENT'])."&time=".time(), 'ENCODE', UC_KEY)); 0131. return $s; 0132.} 0133. 0134./** 0135. * MYSQL 方式取指定的模块和动作的数据 0136. * 0137. * @param string $model 请求的模块 0138. * @param string $action 请求的动作 0139. * @param string $args 参数(会加密的方式传送) 0140. * @return mix 0141. */ 0142.function uc_api_mysql($model, $action, $args=array()) { 0143. global $uc_controls; 0144. if(empty($uc_controls[$model])) { 0145. include_once UC_ROOT.'./lib/db.class.php'; 0146. include_once UC_ROOT.'./model/base.php'; 0147. include_once UC_ROOT."./control/$model.php"; 0148. eval("\$uc_controls['$model'] = new {$model}control();"); 0149. } 0150. if($action{0} != '_') { 0151. $args = uc_addslashes($args, 1, TRUE); 0152. $action = 'on'.$action; 0153. 0154. $uc_controls[$model]->input = $args; 0155. return $uc_controls[$model]->$action($args); 0156. } else { 0157. return ''; 0158. } 0159.} 0160. 0161./** 0162. * 将数组转换成xml 0163. * 0164. * @param arrau $arr 0165. * @param bool $htmlon 0166. * @return string 0167. */ 0168.function uc_serialize($arr, $htmlon = 0) { 0169. include_once UC_ROOT.'./lib/xml.class.php'; 0170. return xml_serialize($arr, $htmlon); 0171.} 0172. 0173./** 0174. * 将xml转换成为数组 0175. * 0176. * @param stromg $s 0177. * @return array 0178. */ 0179.function uc_unserialize($s) { 0180. include_once UC_ROOT.'./lib/xml.class.php'; 0181. return xml_unserialize($s); 0182.} 0183. 0184./** 0185. * 字符串加密以及解密函数 0186. * 0187. * @param string $string 原文或者密文 0188. * @param string $operation 操作(ENCODE | DECODE), 默认为 DECODE 0189. * @param string $key 密钥 0190. * @param int $expiry 密文有效期, 加密时候有效, 单位 秒,0 为永久有效 0191. * @return string 处理后的 原文或者 经过 base64_encode 处理后的密文 0192. * 0193. * @example 0194. * 0195. * $a = authcode('abc', 'ENCODE', 'key'); 0196. * $b = authcode($a, 'DECODE', 'key'); // $b(abc) 0197. * 0198. * $a = authcode('abc', 'ENCODE', 'key', 3600); 0199. * $b = authcode('abc', 'DECODE', 'key'); // 在一个小时内,$b(abc),否则 $b 为空 0200. */ 0201.function uc_authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) { 0202. 0203. $ckey_length = 4; //note 随机密钥长度 取值 0-32; 0204. //note 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。 0205. //note 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方 0206. //note 当此值为 0 时,则不产生随机密钥 0207. 0208. $key = md5($key ? $key : UC_KEY); 0209. $keya = md5(substr($key, 0, 16)); 0210. $keyb = md5(substr($key, 16, 16)); 0211. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : ''; 0212. 0213. $cryptkey = $keya.md5($keya.$keyc); 0214. $key_length = strlen($cryptkey); 0215. 0216. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string; 0217. $string_length = strlen($string); 0218. 0219. $result = ''; 0220. $box = range(0, 255); 0221. 0222. $rndkey = array(); 0223. for($i = 0; $i <= 255; $i++) { 0224. $rndkey[$i] = ord($cryptkey[$i % $key_length]); 0225. } 0226. 0227. for($j = $i = 0; $i < 256; $i++) { 0228. $j = ($j + $box[$i] + $rndkey[$i]) % 256; 0229. $tmp = $box[$i]; 0230. $box[$i] = $box[$j]; 0231. $box[$j] = $tmp; 0232. } 0233. 0234. for($a = $j = $i = 0; $i < $string_length; $i++) { 0235. $a = ($a + 1) % 256; 0236. $j = ($j + $box[$a]) % 256; 0237. $tmp = $box[$a]; 0238. $box[$a] = $box[$j]; 0239. $box[$j] = $tmp; 0240. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256])); 0241. } 0242. 0243. if($operation == 'DECODE') { 0244. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) { 0245. return substr($result, 26); 0246. } else { 0247. return ''; 0248. } 0249. } else { 0250. return $keyc.str_replace('=', '', base64_encode($result)); 0251. } 0252.} 0253. 0254./** 0255. * 远程打开URL 0256. * @param string $url 打开的url, 如 http://www.baidu.com/123.htm 0257. * @param int $limit 取返回的数据的长度 0258. * @param string $post 要发送的 POST 数据,如uid=1&password=1234 0259. * @param string $cookie 要模拟的 COOKIE 数据,如uid=123&auth=a2323sd2323 0260. * @param bool $bysocket TRUE/FALSE 是否通过SOCKET打开 0261. * @param string $ip IP地址 0262. * @param int $timeout 连接超时时间 0263. * @param bool $block 是否为阻塞模式 0264. * @return 取到的字符串 0265. */ 0266.function uc_fopen2($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE) { 0267. $__times__ = isset($_GET['__times__']) ? intval($_GET['__times__']) + 1 : 1; 0268. if($__times__ > 2) { 0269. return ''; 0270. } 0271. $url .= (strpos($url, '?') === FALSE ? '?' : '&')."__times__=$__times__"; 0272. return uc_fopen($url, $limit, $post, $cookie, $bysocket, $ip, $timeout, $block); 0273.} 0274. 0275.function uc_fopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE) { 0276. $return = ''; 0277. $matches = parse_url($url); 0278. !isset($matches['host']) && $matches['host'] = ''; 0279. !isset($matches['path']) && $matches['path'] = ''; 0280. !isset($matches['query']) && $matches['query'] = ''; 0281. !isset($matches['port']) && $matches['port'] = ''; 0282. $host = $matches['host']; 0283. $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/'; 0284. $port = !empty($matches['port']) ? $matches['port'] : 80; 0285. if($post) { 0286. $out = "POST $path HTTP/1.0\r\n"; 0287. $out .= "Accept: */*\r\n"; 0288. //$out .= "Referer: $boardurl\r\n"; 0289. $out .= "Accept-Language: zh-cn\r\n"; 0290. $out .= "Content-Type: application/x-www-form-urlencoded\r\n"; 0291. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n"; 0292. $out .= "Host: $host\r\n"; 0293. $out .= 'Content-Length: '.strlen($post)."\r\n"; 0294. $out .= "Connection: Close\r\n"; 0295. $out .= "Cache-Control: no-cache\r\n"; 0296. $out .= "Cookie: $cookie\r\n\r\n"; 0297. $out .= $post; 0298. } else { 0299. $out = "GET $path HTTP/1.0\r\n"; 0300. $out .= "Accept: */*\r\n"; 0301. //$out .= "Referer: $boardurl\r\n"; 0302. $out .= "Accept-Language: zh-cn\r\n"; 0303. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n"; 0304. $out .= "Host: $host\r\n"; 0305. $out .= "Connection: Close\r\n"; 0306. $out .= "Cookie: $cookie\r\n\r\n"; 0307. } 0308. $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout); 0309. if(!$fp) { 0310. return '';//note $errstr : $errno \r\n 0311. } else { 0312. stream_set_blocking($fp, $block); 0313. stream_set_timeout($fp, $timeout); 0314. @fwrite($fp, $out); 0315. $status = stream_get_meta_data($fp); 0316. if(!$status['timed_out']) { 0317. while (!feof($fp)) { 0318. if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) { 0319. break; 0320. } 0321. } 0322. 0323. $stop = false; 0324. while(!feof($fp) && !$stop) { 0325. $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit)); 0326. $return .= $data; 0327. if($limit) { 0328. $limit -= strlen($data); 0329. $stop = $limit <= 0; 0330. } 0331. } 0332. } 0333. @fclose($fp); 0334. return $return; 0335. } 0336.} 0337. 0338.function uc_app_ls() { 0339. $return = call_user_func(UC_API_FUNC, 'app', 'ls', array()); 0340. return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return); 0341.} 0342. 0343./** 0344. * 添加 feed 0345. * 0346. * @param string $icon 图标 0347. * @param string $uid uid 0348. * @param string $username 用户名 0349. * @param string $title_template 标题模板 0350. * @param array $title_data 标题内容 0351. * @param string $body_template 内容模板 0352. * @param array $body_data 内容内容 0353. * @param string $body_general 保留 0354. * @param string $target_ids 保留 0355. * @param array $images 图片 0356. * 格式为: 0357. * array( 0358. * array('url'=>'http://domain1/1.jpg', 'link'=>'http://domain1'), 0359. * array('url'=>'http://domain2/2.jpg', 'link'=>'http://domain2'), 0360. * array('url'=>'http://domain3/3.jpg', 'link'=>'http://domain3'), 0361. * ) 0362. * 示例: 0363. * $feed['images'][] = array('url'=>$vthumb1, 'link'=>$vthumb1); 0364. * $feed['images'][] = array('url'=>$vthumb2, 'link'=>$vthumb2); 0365. * @return int feedid 0366. */ 0367.function uc_feed_add($icon, $uid, $username, $title_template='', $title_data='', $body_template='', $body_data='', $body_general='', $target_ids='', $images = array()) { 0368. return call_user_func(UC_API_FUNC, 'feed', 'add', 0369. array( 'icon'=>$icon, 0370. 'appid'=>UC_APPID, 0371. 'uid'=>$uid, 0372. 'username'=>$username, 0373. 'title_template'=>$title_template, 0374. 'title_data'=>$title_data, 0375. 'body_template'=>$body_template, 0376. 'body_data'=>$body_data, 0377. 'body_general'=>$body_general, 0378. 'target_ids'=>$target_ids, 0379. 'image_1'=>$images[0]['url'], 0380. 'image_1_link'=>$images[0]['link'], 0381. 'image_2'=>$images[1]['url'], 0382. 'image_2_link'=>$images[1]['link'], 0383. 'image_3'=>$images[2]['url'], 0384. 'image_3_link'=>$images[2]['link'], 0385. 'image_4'=>$images[3]['url'], 0386. 'image_4_link'=>$images[3]['link'] 0387. ) 0388. ); 0389.} 0390. 0391./** 0392. * 每次取多少条 0393. * 0394. * @param int $limit 0395. * @return array() 0396. */ 0397.function uc_feed_get($limit = 100, $delete = TRUE) { 0398. $return = call_user_func(UC_API_FUNC, 'feed', 'get', array('limit'=>$limit, 'delete'=>$delete)); 0399. return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return); 0400.} 0401. 0402./** 0403. * 添加好友 0404. * 0405. * @param int $uid 用户ID 0406. * @param int $friendid 好友ID 0407. * @return 0408. * >0 成功 0409. * <=0 失败 0410. */ 0411.function uc_friend_add($uid, $friendid, $comment='') { 0412. return call_user_func(UC_API_FUNC, 'friend', 'add', array('uid'=>$uid, 'friendid'=>$friendid, 'comment'=>$comment)); 0413.} 0414. 0415./** 0416. * 删除好友 0417. * 0418. * @param int $uid 用户ID 0419. * @param array $friendids 好友ID 0420. * @return 0421. * >0 成功 0422. * <=0 失败,或者好友已经删除 0423. */ 0424.function uc_friend_delete($uid, $friendids) { 0425. return call_user_func(UC_API_FUNC, 'friend', 'delete', array('uid'=>$uid, 'friendids'=>$friendids)); 0426.} 0427. 0428./** 0429. * 好友总数 0430. * @param int $uid 用户ID 0431. * @return int 0432. */ 0433.function uc_friend_totalnum($uid, $direction = 0) { 0434. return call_user_func(UC_API_FUNC, 'friend', 'totalnum', array('uid'=>$uid, 'direction'=>$direction)); 0435.} 0436. 0437./** 0438. * 好友列表 0439. * 0440. * @param int $uid 用户ID 0441. * @param int $page 当前页 0442. * @param int $pagesize 每页条目数 0443. * @param int $totalnum 总数 0444. * @param int $direction 默认为正向. 正向:1 , 反向:2 , 双向:3 0445. * @return array 0446. */ 0447.function uc_friend_ls($uid, $page = 1, $pagesize = 10, $totalnum = 10, $direction = 0) { 0448. $return = call_user_func(UC_API_FUNC, 'friend', 'ls', array('uid'=>$uid, 'page'=>$page, 'pagesize'=>$pagesize, 'totalnum'=>$totalnum, 'direction'=>$direction)); 0449. return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return); 0450.} 0451. 0452./** 0453. * 用户注册 0454. * 0455. * @param string $username 用户名 0456. * @param string $password 密码 0457. * @param string $email Email 0458. * @param int $questionid 安全提问 0459. * @param string $answer 安全提问答案 0460. * @return int 0461. -1 : 用户名不合法 0462. -2 : 包含不允许注册的词语 0463. -3 : 用户名已经存在 0464. -4 : email 格式有误 0465. -5 : email 不允许注册 0466. -6 : 该 email 已经被注册 0467. >1 : 表示成功,数值为 UID 0468.*/ 0469.function uc_user_register($username, $password, $email, $questionid = '', $answer = '') { 0470. return call_user_func(UC_API_FUNC, 'user', 'register', array('username'=>$username, 'password'=>$password, 'email'=>$email, 'questionid'=>$questionid, 'answer'=>$answer)); 0471.} 0472. 0473./** 0474. * 用户注册 0475. * 0476. * @param string $username 用户名 0477. * @param string $password 密码 0478. * @param string $email Email 0479. * @param int $questionid 安全提问 0480. * @param string $answer 安全提问答案 0481. * @return int 0482. -1 : 用户名不合法 0483. -2 : 包含不允许注册的词语 0484. -3 : 用户名已经存在 0485. -4 : email 格式有误 0486. -5 : email 不允许注册 0487. -6 : 该 email 已经被注册 0488. >1 : 表示成功,数值为 UID 0489. 0490.function uc_ef007_register($username, $password, $email, $usertype) { 0491. return call_user_func(UC_API_FUNC, 'user', 'register', array('username'=>$username, 'password'=>$password, 'email'=>$email, 'questionid'=>$questionid, 'answer'=>$answer,'usertype'=>$usertype)); 0492.} 0493./** 0494. * 中介机构注册 0495. * 0496. * @param int $city 0497. * @param string $companyName 0498. * @param string $companyEmailk 0499. * @param string $contactTel 0500. * @param string $contactPerson 0501. * @param string $companyAddress 0502. * @param int $companyNo 工商注册号 0503. * @param int $companyRegCount 注册资本 0504. * @param string $companyRegDate 0505. * @param int $companyUnit 0506. * @param string $companyInfo 0507. * @param string $companyRemark 0508. * 0509. * @return int 0510. * -1 : 用户名不合法 0511. * >1 : 表示成功,数值为 UID 0512. */ 0513.function uc_agency_register($city,$companyname,$category,$userName,$companyEmail,$companyPass,$contactTel,$contactPerson,$companyAddress,$companyNo,$companyRegCount,$companyRegDate,$companyUnit,$companyInfo,$companyRemark){ 0514. 0515. return call_user_func(UC_API_FUNC,'agency','shopreg',array('city' => $city, 'companyname' => $companyname, 'category' => $category,'username' => $userName, 'companyEmail' => $companyEmail,'companyPass' => $companyPass, 'contactTel' => $contactTel, 'contactPerson' => $contactPerson, 'companyAddress' => $companyAddress,'companyNo' => $companyNo,'companyRegCount' => $companyRegCount, 'companyRegDate' => $companyRegDate,'companyUnit' => $companyUnit, 'companyInfo' => $companyInfo,'companyRemark' => $companyRemark)); 0516.} 0517. 相关阅读 更多 +