PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/startZine/src/main/api/uc.php

http://ownerpress.googlecode.com/
PHP | 391 lines | 314 code | 57 blank | 20 comment | 46 complexity | 5514dd48c35483addc04d9e877175b25 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * UCenter API
  4. */
  5. define('UC_CLIENT_VERSION', '1.5.1');
  6. define('UC_CLIENT_RELEASE', '20091001');
  7. define('API_DELETEUSER', 1);
  8. define('API_RENAMEUSER', 1);
  9. define('API_GETTAG', 1);
  10. define('API_SYNLOGIN', 1);
  11. define('API_SYNLOGOUT', 1);
  12. define('API_UPDATEPW', 1);
  13. define('API_UPDATEBADWORDS', 1);
  14. define('API_UPDATEHOSTS', 1);
  15. define('API_UPDATEAPPS', 1);
  16. define('API_UPDATECLIENT', 1);
  17. define('API_UPDATECREDIT', 1);
  18. define('API_GETCREDITSETTINGS', 1);
  19. define('API_GETCREDIT', 1);
  20. define('API_UPDATECREDITSETTINGS', 1);
  21. define('API_RETURN_SUCCEED', '1');
  22. define('API_RETURN_FAILED', '-1');
  23. define('API_RETURN_FORBIDDEN', '-2');
  24. include './init.php';
  25. if(!defined('IN_UC')) {
  26. error_reporting(0);
  27. set_magic_quotes_runtime(0);
  28. defined('MAGIC_QUOTES_GPC') || define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
  29. $_DCACHE = $get = $post = array();
  30. $code = @$_GET['code'];
  31. parse_str(_authcode($code, 'DECODE', UC_KEY), $get);
  32. if(MAGIC_QUOTES_GPC) {
  33. $get = _stripslashes($get);
  34. }
  35. $timestamp = time();
  36. if(empty($get)) {
  37. exit('Invalid Request');
  38. } elseif($timestamp - $get['time'] > 3600) {
  39. exit('Authracation has expiried');
  40. }
  41. }
  42. $action = $get['action'];
  43. include(FANWE_ROOT . 'uc_client/lib/xml.class.php');
  44. $post = xml_unserialize(file_get_contents('php://input'));
  45. if(in_array($get['action'], array('test', 'deleteuser', 'renameuser', 'gettag', 'synlogin', 'synlogout', 'updatepw', 'updatebadwords', 'updatehosts', 'updateapps', 'updateclient', 'updatecredit', 'getcreditsettings', 'updatecreditsettings')))
  46. {
  47. $uc_note = new uc_note();
  48. exit($uc_note->$get['action']($get, $post));
  49. }
  50. else
  51. {
  52. exit(API_RETURN_FAILED);
  53. }
  54. class uc_note
  55. {
  56. var $appdir = '';
  57. /* ???????? */
  58. var $charset = '';
  59. function _serialize($arr, $htmlon = 0)
  60. {
  61. if(!function_exists('xml_serialize'))
  62. {
  63. include($this->appdir . 'uc_client/lib/xml.class.php');
  64. }
  65. return xml_serialize($arr, $htmlon);
  66. }
  67. function uc_note()
  68. {
  69. $this->appdir = FANWE_ROOT;
  70. $this->charset = UC_DBCHARSET;
  71. }
  72. function test($get, $post)
  73. {
  74. return API_RETURN_SUCCEED;
  75. }
  76. function deleteuser($get, $post)
  77. {
  78. $uids = $get['ids'];
  79. if(!API_DELETEUSER)
  80. {
  81. return API_RETURN_FORBIDDEN;
  82. }
  83. return API_RETURN_SUCCEED;
  84. /*
  85. if (FS('User')->setSession($user,0))
  86. {
  87. return API_RETURN_SUCCEED;
  88. }
  89. */
  90. }
  91. function renameuser($get, $post)
  92. {
  93. $uid = $get['uid'];
  94. $usernamenew = $get['newusername'];
  95. if ($this->charset == 'gbk'){
  96. $usernamenew = addslashes(gbToUTF8($usernamenew));
  97. };
  98. if(!API_RENAMEUSER)
  99. {
  100. return API_RETURN_FORBIDDEN;
  101. }
  102. FDB::query("UPDATE " . FDB::table('user') . " SET user_name='$usernamenew' WHERE ucenter_id='$uid' limit 1");
  103. return API_RETURN_SUCCEED;
  104. }
  105. function gettag($get, $post)
  106. {
  107. if(!API_GETTAG)
  108. {
  109. return API_RETURN_FORBIDDEN;
  110. }
  111. }
  112. function synlogin($get, $post)
  113. {
  114. global $_FANWE;
  115. //uc_get_user()
  116. $integrate_id = intval($get['uid']);
  117. $username = $get['username'];
  118. if ($this->charset == 'gbk'){
  119. $username = addslashes(gbToUTF8($username));
  120. };
  121. if(!API_SYNLOGIN)
  122. {
  123. return API_RETURN_FORBIDDEN;
  124. }
  125. //$sql = "update fanwe_user set last_ip = 'aaaa'";
  126. //$GLOBALS['db']->query($sql);
  127. header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
  128. //set_login($uid, $username, $this->charset);
  129. $user_field = $_FANWE['setting']['integrate_field_id'];
  130. $sql = "SELECT uid,password FROM ".FDB::table('user')." WHERE {$user_field} = '$integrate_id'";
  131. $user_info = FDB::fetchFirst($sql);
  132. $password = $user_info['password'];
  133. $uid = intval($user_info['uid']);
  134. //echo 'ddd2<br>'.$uid; exit;
  135. if ($uid <= 0){
  136. include_once(FANWE_ROOT . 'uc_client/client.php');
  137. //echo 'aa';
  138. list($uid, $uname, $email) = uc_get_user($integrate_id,1);
  139. //echo 'aa';
  140. $user = array(
  141. 'integrate_id' => $uid,
  142. 'email' => $email,
  143. 'user_name' => $uname,
  144. 'password' => md5(time().rand(100000, 999999)),
  145. );
  146. //print_r($user);
  147. $uid = FS("Integrate")->addUserToLoacl($username,$password, 1, $user);
  148. }
  149. if ($uid > 0){
  150. $sql = "SELECT password FROM ".FDB::table('user')." WHERE uid = '$uid'";
  151. $password = FDB::resultFirst($sql);
  152. $user = array(
  153. 'uid'=>$uid,
  154. 'password'=>$password,
  155. );
  156. FS('User')->setSession($user,0);
  157. }
  158. }
  159. function synlogout($get, $post)
  160. {
  161. if(!API_SYNLOGOUT)
  162. {
  163. return API_RETURN_FORBIDDEN;
  164. }
  165. header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
  166. //set_cookie();
  167. FS('User')->clearSession();
  168. }
  169. function updatepw($get, $post)
  170. {
  171. if(!API_UPDATEPW)
  172. {
  173. return API_RETURN_FORBIDDEN;
  174. }
  175. $username = $get['username'];
  176. $uid = intval($get['uid']);
  177. if ($this->charset == 'gbk'){
  178. $username = addslashes(gbToUTF8($username));
  179. };
  180. $newpw = md5(time().rand(100000, 999999));
  181. FDB::query("UPDATE " . FDB::table('user') . " SET password='$newpw' WHERE user_name='$username' limit 1");
  182. return API_RETURN_SUCCEED;
  183. }
  184. function updatebadwords($get, $post)
  185. {
  186. if(!API_UPDATEBADWORDS)
  187. {
  188. return API_RETURN_FORBIDDEN;
  189. }
  190. $cachefile = $this->appdir.'uc_client/data/cache/badwords.php';
  191. $fp = fopen($cachefile, 'w');
  192. $data = array();
  193. if(is_array($post)) {
  194. foreach($post as $k => $v) {
  195. $data['findpattern'][$k] = $v['findpattern'];
  196. $data['replace'][$k] = $v['replacement'];
  197. }
  198. }
  199. $s = "<?php\r\n";
  200. $s .= '$_CACHE[\'badwords\'] = '.var_export($data, TRUE).";\r\n";
  201. fwrite($fp, $s);
  202. fclose($fp);
  203. return API_RETURN_SUCCEED;
  204. }
  205. function updatehosts($get, $post)
  206. {
  207. if(!API_UPDATEHOSTS)
  208. {
  209. return API_RETURN_FORBIDDEN;
  210. }
  211. $cachefile = $this->appdir. 'uc_client/data/cache/hosts.php';
  212. $fp = fopen($cachefile, 'w');
  213. $s = "<?php\r\n";
  214. $s .= '$_CACHE[\'hosts\'] = '.var_export($post, TRUE).";\r\n";
  215. fwrite($fp, $s);
  216. fclose($fp);
  217. return API_RETURN_SUCCEED;
  218. }
  219. function updateapps($get, $post)
  220. {
  221. if(!API_UPDATEAPPS)
  222. {
  223. return API_RETURN_FORBIDDEN;
  224. }
  225. $UC_API = $post['UC_API'];
  226. $cachefile = $this->appdir . 'uc_client/data/cache/apps.php';
  227. $fp = fopen($cachefile, 'w');
  228. $s = "<?php\r\n";
  229. $s .= '$_CACHE[\'apps\'] = '.var_export($post, TRUE).";\r\n";
  230. fwrite($fp, $s);
  231. fclose($fp);
  232. #clear_cache_files();
  233. return API_RETURN_SUCCEED;
  234. }
  235. function updateclient($get, $post)
  236. {
  237. if(!API_UPDATECLIENT)
  238. {
  239. return API_RETURN_FORBIDDEN;
  240. }
  241. $cachefile = $this->appdir. 'uc_client/data/cache/settings.php';
  242. $fp = fopen($cachefile, 'w');
  243. $s = "<?php\r\n";
  244. $s .= '$_CACHE[\'settings\'] = '.var_export($post, TRUE).";\r\n";
  245. fwrite($fp, $s);
  246. fclose($fp);
  247. return API_RETURN_SUCCEED;
  248. }
  249. function updatecredit($get, $post)
  250. {
  251. if(!API_UPDATECREDIT)
  252. {
  253. return API_RETURN_FORBIDDEN;
  254. }
  255. }
  256. function getcredit($get, $post)
  257. {
  258. if(!API_GETCREDIT)
  259. {
  260. return API_RETURN_FORBIDDEN;
  261. }
  262. }
  263. function getcreditsettings($get, $post)
  264. {
  265. if(!API_GETCREDITSETTINGS)
  266. {
  267. return API_RETURN_FORBIDDEN;
  268. }
  269. }
  270. function updatecreditsettings($get, $post)
  271. {
  272. if(!API_UPDATECREDITSETTINGS)
  273. {
  274. return API_RETURN_FORBIDDEN;
  275. }
  276. }
  277. }
  278. function _setcookie($var, $value, $life = 0, $prefix = 1) {
  279. global $cookiepre, $cookiedomain, $cookiepath, $timestamp, $_SERVER;
  280. setcookie(($prefix ? $cookiepre : '').$var, $value,
  281. $life ? $timestamp + $life : 0, $cookiepath,
  282. $cookiedomain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0);
  283. }
  284. function _authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  285. $ckey_length = 4;
  286. $key = md5($key ? $key : UC_KEY);
  287. $keya = md5(substr($key, 0, 16));
  288. $keyb = md5(substr($key, 16, 16));
  289. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  290. $cryptkey = $keya.md5($keya.$keyc);
  291. $key_length = strlen($cryptkey);
  292. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + gmtTime() : 0).substr(md5($string.$keyb), 0, 16).$string;
  293. $string_length = strlen($string);
  294. $result = '';
  295. $box = range(0, 255);
  296. $rndkey = array();
  297. for($i = 0; $i <= 255; $i++) {
  298. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  299. }
  300. for($j = $i = 0; $i < 256; $i++) {
  301. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  302. $tmp = $box[$i];
  303. $box[$i] = $box[$j];
  304. $box[$j] = $tmp;
  305. }
  306. for($a = $j = $i = 0; $i < $string_length; $i++) {
  307. $a = ($a + 1) % 256;
  308. $j = ($j + $box[$a]) % 256;
  309. $tmp = $box[$a];
  310. $box[$a] = $box[$j];
  311. $box[$j] = $tmp;
  312. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  313. }
  314. if($operation == 'DECODE') {
  315. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  316. return substr($result, 26);
  317. } else {
  318. return '';
  319. }
  320. } else {
  321. return $keyc.str_replace('=', '', base64_encode($result));
  322. }
  323. }
  324. function _stripslashes($string) {
  325. if(is_array($string)) {
  326. foreach($string as $key => $val) {
  327. $string[$key] = _stripslashes($val);
  328. }
  329. } else {
  330. $string = stripslashes($string);
  331. }
  332. return $string;
  333. }
  334. ?>