PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/phpSource/UCenter_1.5.1_SC_UTF8/advanced/examples/api/uc.php

http://phpfor.googlecode.com/
PHP | 337 lines | 277 code | 54 blank | 6 comment | 44 complexity | 9ac18d87ef2426ada2120400aa8017a9 MD5 | raw file
  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('Example_auth', _authcode($uid."\t".$username, 'ENCODE'));
  115. }
  116. function synlogout($get, $post) {
  117. if(!API_SYNLOGOUT) {
  118. return API_RETURN_FORBIDDEN;
  119. }
  120. //note ???? API ??
  121. header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
  122. _setcookie('Example_auth', '', -86400 * 365);
  123. }
  124. function updatepw($get, $post) {
  125. if(!API_UPDATEPW) {
  126. return API_RETURN_FORBIDDEN;
  127. }
  128. $username = $get['username'];
  129. $password = $get['password'];
  130. return API_RETURN_SUCCEED;
  131. }
  132. function updatebadwords($get, $post) {
  133. if(!API_UPDATEBADWORDS) {
  134. return API_RETURN_FORBIDDEN;
  135. }
  136. $cachefile = $this->appdir.'./uc_client/data/cache/badwords.php';
  137. $fp = fopen($cachefile, 'w');
  138. $data = array();
  139. if(is_array($post)) {
  140. foreach($post as $k => $v) {
  141. $data['findpattern'][$k] = $v['findpattern'];
  142. $data['replace'][$k] = $v['replacement'];
  143. }
  144. }
  145. $s = "<?php\r\n";
  146. $s .= '$_CACHE[\'badwords\'] = '.var_export($data, TRUE).";\r\n";
  147. fwrite($fp, $s);
  148. fclose($fp);
  149. return API_RETURN_SUCCEED;
  150. }
  151. function updatehosts($get, $post) {
  152. if(!API_UPDATEHOSTS) {
  153. return API_RETURN_FORBIDDEN;
  154. }
  155. $cachefile = $this->appdir.'./uc_client/data/cache/hosts.php';
  156. $fp = fopen($cachefile, 'w');
  157. $s = "<?php\r\n";
  158. $s .= '$_CACHE[\'hosts\'] = '.var_export($post, TRUE).";\r\n";
  159. fwrite($fp, $s);
  160. fclose($fp);
  161. return API_RETURN_SUCCEED;
  162. }
  163. function updateapps($get, $post) {
  164. if(!API_UPDATEAPPS) {
  165. return API_RETURN_FORBIDDEN;
  166. }
  167. $UC_API = $post['UC_API'];
  168. //note ? app ????
  169. $cachefile = $this->appdir.'./uc_client/data/cache/apps.php';
  170. $fp = fopen($cachefile, 'w');
  171. $s = "<?php\r\n";
  172. $s .= '$_CACHE[\'apps\'] = '.var_export($post, TRUE).";\r\n";
  173. fwrite($fp, $s);
  174. fclose($fp);
  175. //note ?????
  176. if(is_writeable($this->appdir.'./config.inc.php')) {
  177. $configfile = trim(file_get_contents($this->appdir.'./config.inc.php'));
  178. $configfile = substr($configfile, -2) == '?>' ? substr($configfile, 0, -2) : $configfile;
  179. $configfile = preg_replace("/define\('UC_API',\s*'.*?'\);/i", "define('UC_API', '$UC_API');", $configfile);
  180. if($fp = @fopen($this->appdir.'./config.inc.php', 'w')) {
  181. @fwrite($fp, trim($configfile));
  182. @fclose($fp);
  183. }
  184. }
  185. return API_RETURN_SUCCEED;
  186. }
  187. function updateclient($get, $post) {
  188. if(!API_UPDATECLIENT) {
  189. return API_RETURN_FORBIDDEN;
  190. }
  191. $cachefile = $this->appdir.'./uc_client/data/cache/settings.php';
  192. $fp = fopen($cachefile, 'w');
  193. $s = "<?php\r\n";
  194. $s .= '$_CACHE[\'settings\'] = '.var_export($post, TRUE).";\r\n";
  195. fwrite($fp, $s);
  196. fclose($fp);
  197. return API_RETURN_SUCCEED;
  198. }
  199. function updatecredit($get, $post) {
  200. if(!API_UPDATECREDIT) {
  201. return API_RETURN_FORBIDDEN;
  202. }
  203. $credit = $get['credit'];
  204. $amount = $get['amount'];
  205. $uid = $get['uid'];
  206. return API_RETURN_SUCCEED;
  207. }
  208. function getcredit($get, $post) {
  209. if(!API_GETCREDIT) {
  210. return API_RETURN_FORBIDDEN;
  211. }
  212. }
  213. function getcreditsettings($get, $post) {
  214. if(!API_GETCREDITSETTINGS) {
  215. return API_RETURN_FORBIDDEN;
  216. }
  217. $credits = array();
  218. return $this->_serialize($credits);
  219. }
  220. function updatecreditsettings($get, $post) {
  221. if(!API_UPDATECREDITSETTINGS) {
  222. return API_RETURN_FORBIDDEN;
  223. }
  224. return API_RETURN_SUCCEED;
  225. }
  226. }
  227. //note ???????? require_once $this->appdir.'./config.inc.php';
  228. function _setcookie($var, $value, $life = 0, $prefix = 1) {
  229. global $cookiepre, $cookiedomain, $cookiepath, $timestamp, $_SERVER;
  230. setcookie(($prefix ? $cookiepre : '').$var, $value,
  231. $life ? $timestamp + $life : 0, $cookiepath,
  232. $cookiedomain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0);
  233. }
  234. function _authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  235. $ckey_length = 4;
  236. $key = md5($key ? $key : UC_KEY);
  237. $keya = md5(substr($key, 0, 16));
  238. $keyb = md5(substr($key, 16, 16));
  239. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  240. $cryptkey = $keya.md5($keya.$keyc);
  241. $key_length = strlen($cryptkey);
  242. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  243. $string_length = strlen($string);
  244. $result = '';
  245. $box = range(0, 255);
  246. $rndkey = array();
  247. for($i = 0; $i <= 255; $i++) {
  248. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  249. }
  250. for($j = $i = 0; $i < 256; $i++) {
  251. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  252. $tmp = $box[$i];
  253. $box[$i] = $box[$j];
  254. $box[$j] = $tmp;
  255. }
  256. for($a = $j = $i = 0; $i < $string_length; $i++) {
  257. $a = ($a + 1) % 256;
  258. $j = ($j + $box[$a]) % 256;
  259. $tmp = $box[$a];
  260. $box[$a] = $box[$j];
  261. $box[$j] = $tmp;
  262. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  263. }
  264. if($operation == 'DECODE') {
  265. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  266. return substr($result, 26);
  267. } else {
  268. return '';
  269. }
  270. } else {
  271. return $keyc.str_replace('=', '', base64_encode($result));
  272. }
  273. }
  274. function _stripslashes($string) {
  275. if(is_array($string)) {
  276. foreach($string as $key => $val) {
  277. $string[$key] = _stripslashes($val);
  278. }
  279. } else {
  280. $string = stripslashes($string);
  281. }
  282. return $string;
  283. }