PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/php/idc_manager/cidc-read-only/library/uc/api/uc.php

http://timoseven.googlecode.com/
PHP | 359 lines | 293 code | 57 blank | 9 comment | 46 complexity | c9e7722935e7825cd5185046b39ea6a5 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0, AGPL-1.0
  1. <?php
  2. define('IN_DISCUZ', TRUE);
  3. define('UC_CLIENT_VERSION', '1.5.0'); //note UCenter ????
  4. define('UC_CLIENT_RELEASE', '20081031');
  5. define('API_DELETEUSER', 1); //note ???? API ????
  6. define('API_RENAMEUSER', 1); //note ???? API ????
  7. define('API_GETTAG', 1); //note ???? API ????
  8. define('API_SYNLOGIN', 1); //note ???? API ????
  9. define('API_SYNLOGOUT', 1); //note ???? API ????
  10. define('API_UPDATEPW', 1); //note ?????? ??
  11. define('API_UPDATEBADWORDS', 1); //note ??????? ??
  12. define('API_UPDATEHOSTS', 1); //note ???????? ??
  13. define('API_UPDATEAPPS', 1); //note ?????? ??
  14. define('API_UPDATECLIENT', 1); //note ??????? ??
  15. define('API_UPDATECREDIT', 1); //note ?????? ??
  16. define('API_GETCREDITSETTINGS', 1); //note ? UCenter ?????? ??
  17. define('API_GETCREDIT', 1); //note ????????? ??
  18. define('API_UPDATECREDITSETTINGS', 1); //note ???????? ??
  19. define('API_RETURN_SUCCEED', '1');
  20. define('API_RETURN_FAILED', '-1');
  21. define('API_RETURN_FORBIDDEN', '-2');
  22. define('DISCUZ_ROOT', '../');
  23. //note ??? http ????
  24. if(!defined('IN_UC')) {
  25. error_reporting(0);
  26. set_magic_quotes_runtime(0);
  27. defined('MAGIC_QUOTES_GPC') || define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
  28. require_once DISCUZ_ROOT.'./config.inc.php';
  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($timestamp - $get['time'] > 3600) {
  37. exit('Authracation has expiried');
  38. }
  39. if(empty($get)) {
  40. exit('Invalid Request');
  41. }
  42. $action = $get['action'];
  43. require_once DISCUZ_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. require_once DISCUZ_ROOT.'./include/db_mysql.class.php';
  47. $GLOBALS['db'] = new dbstuff;
  48. $GLOBALS['db']->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset);
  49. $GLOBALS['tablepre'] = $tablepre;
  50. unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
  51. $uc_note = new uc_note();
  52. exit($uc_note->$get['action']($get, $post));
  53. } else {
  54. exit(API_RETURN_FAILED);
  55. }
  56. //note include ????
  57. } else {
  58. require_once DISCUZ_ROOT.'./config.inc.php';
  59. require_once DISCUZ_ROOT.'./include/db_mysql.class.php';
  60. $GLOBALS['db'] = new dbstuff;
  61. $GLOBALS['db']->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset);
  62. $GLOBALS['tablepre'] = $tablepre;
  63. unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
  64. }
  65. class uc_note {
  66. var $dbconfig = '';
  67. var $db = '';
  68. var $tablepre = '';
  69. var $appdir = '';
  70. function _serialize($arr, $htmlon = 0) {
  71. if(!function_exists('xml_serialize')) {
  72. include_once DISCUZ_ROOT.'./uc_client/lib/xml.class.php';
  73. }
  74. return xml_serialize($arr, $htmlon);
  75. }
  76. function uc_note() {
  77. $this->appdir = substr(dirname(__FILE__), 0, -3);
  78. $this->dbconfig = $this->appdir.'./config.inc.php';
  79. $this->db = $GLOBALS['db'];
  80. $this->tablepre = $GLOBALS['tablepre'];
  81. }
  82. function test($get, $post) {
  83. return API_RETURN_SUCCEED;
  84. }
  85. function deleteuser($get, $post) {
  86. $uids = $get['ids'];
  87. !API_DELETEUSER && exit(API_RETURN_FORBIDDEN);
  88. return API_RETURN_SUCCEED;
  89. }
  90. function renameuser($get, $post) {
  91. $uid = $get['uid'];
  92. $usernameold = $get['oldusername'];
  93. $usernamenew = $get['newusername'];
  94. if(!API_RENAMEUSER) {
  95. return API_RETURN_FORBIDDEN;
  96. }
  97. return API_RETURN_SUCCEED;
  98. }
  99. function gettag($get, $post) {
  100. $name = $get['id'];
  101. if(!API_GETTAG) {
  102. return API_RETURN_FORBIDDEN;
  103. }
  104. $return = array();
  105. return $this->_serialize($return, 1);
  106. }
  107. function synlogin($get, $post) {
  108. $uid = $get['uid'];
  109. $username = $get['username'];
  110. if(!API_SYNLOGIN) {
  111. return API_RETURN_FORBIDDEN;
  112. }
  113. header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
  114. //_setcookie('Zend_Auth', _authcode($uid."\t".$username, 'ENCODE'));
  115. setcookie('Zend_Auth',serialize(array('uid'=>$uid,'username'=>$username)),time()+60*60*24*30,'/');
  116. }
  117. function synlogin0($get, $post) {
  118. $uid = $get['uid'];
  119. $username = $get['username'];
  120. if(!API_SYNLOGIN) {
  121. return API_RETURN_FORBIDDEN;
  122. }
  123. header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
  124. _setcookie('Example_auth', _authcode($uid."\t".$username, 'ENCODE'));
  125. }
  126. function synlogout($get, $post) {
  127. if(!API_SYNLOGOUT) {
  128. return API_RETURN_FORBIDDEN;
  129. }
  130. //note ???? API ??
  131. header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
  132. //_setcookie('Example_auth', '', -86400 * 365);
  133. setcookie('Zend_Auth','',time()-60*60*24*3,'/');
  134. }
  135. function synlogout0($get, $post) {
  136. if(!API_SYNLOGOUT) {
  137. return API_RETURN_FORBIDDEN;
  138. }
  139. //note ???? API ??
  140. header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
  141. _setcookie('Example_auth', '', -86400 * 365);
  142. }
  143. function updatepw($get, $post) {
  144. if(!API_UPDATEPW) {
  145. return API_RETURN_FORBIDDEN;
  146. }
  147. $username = $get['username'];
  148. $password = $get['password'];
  149. return API_RETURN_SUCCEED;
  150. }
  151. function updatebadwords($get, $post) {
  152. if(!API_UPDATEBADWORDS) {
  153. return API_RETURN_FORBIDDEN;
  154. }
  155. $cachefile = $this->appdir.'./uc_client/data/cache/badwords.php';
  156. $fp = fopen($cachefile, 'w');
  157. $data = array();
  158. if(is_array($post)) {
  159. foreach($post as $k => $v) {
  160. $data['findpattern'][$k] = $v['findpattern'];
  161. $data['replace'][$k] = $v['replacement'];
  162. }
  163. }
  164. $s = "<?php\r\n";
  165. $s .= '$_CACHE[\'badwords\'] = '.var_export($data, TRUE).";\r\n";
  166. fwrite($fp, $s);
  167. fclose($fp);
  168. return API_RETURN_SUCCEED;
  169. }
  170. function updatehosts($get, $post) {
  171. if(!API_UPDATEHOSTS) {
  172. return API_RETURN_FORBIDDEN;
  173. }
  174. $cachefile = $this->appdir.'./uc_client/data/cache/hosts.php';
  175. $fp = fopen($cachefile, 'w');
  176. $s = "<?php\r\n";
  177. $s .= '$_CACHE[\'hosts\'] = '.var_export($post, TRUE).";\r\n";
  178. fwrite($fp, $s);
  179. fclose($fp);
  180. return API_RETURN_SUCCEED;
  181. }
  182. function updateapps($get, $post) {
  183. if(!API_UPDATEAPPS) {
  184. return API_RETURN_FORBIDDEN;
  185. }
  186. $UC_API = $post['UC_API'];
  187. //note ? app ????
  188. $cachefile = $this->appdir.'./uc_client/data/cache/apps.php';
  189. $fp = fopen($cachefile, 'w');
  190. $s = "<?php\r\n";
  191. $s .= '$_CACHE[\'apps\'] = '.var_export($post, TRUE).";\r\n";
  192. fwrite($fp, $s);
  193. fclose($fp);
  194. //note ?????
  195. if(is_writeable($this->appdir.'./config.inc.php')) {
  196. $configfile = trim(file_get_contents($this->appdir.'./config.inc.php'));
  197. $configfile = substr($configfile, -2) == '?>' ? substr($configfile, 0, -2) : $configfile;
  198. $configfile = preg_replace("/define\('UC_API',\s*'.*?'\);/i", "define('UC_API', '$UC_API');", $configfile);
  199. if($fp = @fopen($this->appdir.'./config.inc.php', 'w')) {
  200. @fwrite($fp, trim($configfile));
  201. @fclose($fp);
  202. }
  203. }
  204. return API_RETURN_SUCCEED;
  205. }
  206. function updateclient($get, $post) {
  207. if(!API_UPDATECLIENT) {
  208. return API_RETURN_FORBIDDEN;
  209. }
  210. $cachefile = $this->appdir.'./uc_client/data/cache/settings.php';
  211. $fp = fopen($cachefile, 'w');
  212. $s = "<?php\r\n";
  213. $s .= '$_CACHE[\'settings\'] = '.var_export($post, TRUE).";\r\n";
  214. fwrite($fp, $s);
  215. fclose($fp);
  216. return API_RETURN_SUCCEED;
  217. }
  218. function updatecredit($get, $post) {
  219. if(!API_UPDATECREDIT) {
  220. return API_RETURN_FORBIDDEN;
  221. }
  222. $credit = $get['credit'];
  223. $amount = $get['amount'];
  224. $uid = $get['uid'];
  225. return API_RETURN_SUCCEED;
  226. }
  227. function getcredit($get, $post) {
  228. if(!API_GETCREDIT) {
  229. return API_RETURN_FORBIDDEN;
  230. }
  231. }
  232. function getcreditsettings($get, $post) {
  233. if(!API_GETCREDITSETTINGS) {
  234. return API_RETURN_FORBIDDEN;
  235. }
  236. $credits = array();
  237. return $this->_serialize($credits);
  238. }
  239. function updatecreditsettings($get, $post) {
  240. if(!API_UPDATECREDITSETTINGS) {
  241. return API_RETURN_FORBIDDEN;
  242. }
  243. return API_RETURN_SUCCEED;
  244. }
  245. }
  246. //note ???????? require_once $this->appdir.'./config.inc.php';
  247. function _setcookie($var, $value, $life = 0, $prefix = 1) {
  248. global $cookiepre, $cookiedomain, $cookiepath, $timestamp, $_SERVER;
  249. setcookie(($prefix ? $cookiepre : '').$var, $value,
  250. $life ? $timestamp + $life : 0, $cookiepath,
  251. $cookiedomain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0);
  252. }
  253. function _authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  254. $ckey_length = 4;
  255. $key = md5($key ? $key : UC_KEY);
  256. $keya = md5(substr($key, 0, 16));
  257. $keyb = md5(substr($key, 16, 16));
  258. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  259. $cryptkey = $keya.md5($keya.$keyc);
  260. $key_length = strlen($cryptkey);
  261. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  262. $string_length = strlen($string);
  263. $result = '';
  264. $box = range(0, 255);
  265. $rndkey = array();
  266. for($i = 0; $i <= 255; $i++) {
  267. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  268. }
  269. for($j = $i = 0; $i < 256; $i++) {
  270. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  271. $tmp = $box[$i];
  272. $box[$i] = $box[$j];
  273. $box[$j] = $tmp;
  274. }
  275. for($a = $j = $i = 0; $i < $string_length; $i++) {
  276. $a = ($a + 1) % 256;
  277. $j = ($j + $box[$a]) % 256;
  278. $tmp = $box[$a];
  279. $box[$a] = $box[$j];
  280. $box[$j] = $tmp;
  281. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  282. }
  283. if($operation == 'DECODE') {
  284. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  285. return substr($result, 26);
  286. } else {
  287. return '';
  288. }
  289. } else {
  290. return $keyc.str_replace('=', '', base64_encode($result));
  291. }
  292. }
  293. function _stripslashes($string) {
  294. if(is_array($string)) {
  295. foreach($string as $key => $val) {
  296. $string[$key] = _stripslashes($val);
  297. }
  298. } else {
  299. $string = stripslashes($string);
  300. }
  301. return $string;
  302. }