PageRenderTime 59ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/api/db/dbbak.php

https://github.com/kuaileshike/upload
PHP | 774 lines | 645 code | 123 blank | 6 comment | 157 complexity | 1ad4d4e8aefda671ea925da0028a65b9 MD5 | raw file
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: dbbak.php 29239 2012-03-30 06:37:30Z chenmengshu $
  7. */
  8. @define('IN_API', true);
  9. @define('CURSCRIPT', 'api');
  10. error_reporting(0);
  11. $code = @$_GET['code'];
  12. $apptype = @$_GET['apptype'];
  13. $apptype = strtolower($apptype);
  14. define('IN_COMSENZ', TRUE);
  15. if($apptype == 'discuzx') {
  16. define('ROOT_PATH', dirname(__FILE__).'/../../');
  17. } else {
  18. define('ROOT_PATH', dirname(__FILE__).'/../');
  19. }
  20. define('EXPLOR_SUCCESS', 0);
  21. define('IMPORT_SUCCESS', 0);
  22. define('DELETE_SQLPATH_SUCCESS', 4);
  23. define('MKDIR_ERROR', 1);
  24. define('DATABASE_EXPORT_FILE_INVALID', 2);
  25. define('RUN_SQL_ERROR', 3);
  26. define('SQLPATH_NULL_NOEXISTS', 4);
  27. define('SQLPATH_NOMATCH_BAKFILE', 5);
  28. define('BAK_FILE_LOSE', 6);
  29. define('DIR_NO_EXISTS', 7);
  30. define('DELETE_DUMPFILE_ERROR', 8);
  31. define('DB_API_NO_MATCH', 9);
  32. $sizelimit = 2000;
  33. $usehex = true;
  34. if($apptype == 'discuz') {
  35. require ROOT_PATH.'./config.inc.php';
  36. } elseif($apptype == 'uchome' || $apptype == 'supesite' || $apptype == 'supev') {
  37. require ROOT_PATH.'./config.php';
  38. } elseif($apptype == 'ucenter') {
  39. require ROOT_PATH.'./data/config.inc.php';
  40. } elseif($apptype == 'ecmall') {
  41. require ROOT_PATH.'./data/inc.config.php';
  42. } elseif($apptype == 'ecshop') {
  43. require ROOT_PATH.'./data/config.php';
  44. } elseif($apptype == 'discuzx') {
  45. require ROOT_PATH.'./config/config_global.php';
  46. require ROOT_PATH.'./config/config_ucenter.php';
  47. } else {
  48. api_msg('db_api_no_match', $apptype);
  49. }
  50. parse_str(_authcode($code, 'DECODE', UC_KEY), $get);
  51. if(get_magic_quotes_gpc()) {
  52. $get = _stripslashes($get);
  53. }
  54. if(empty($get)) {
  55. exit('Invalid Request');
  56. }
  57. $timestamp = time();
  58. if($timestamp - $get['time'] > 3600) {
  59. exit('Authracation has expiried');
  60. }
  61. $get['time'] = $timestamp;
  62. class dbstuff {
  63. var $querynum = 0;
  64. var $link;
  65. var $histories;
  66. var $time;
  67. var $tablepre;
  68. function connect($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset, $pconnect = 0, $tablepre='', $time = 0) {
  69. $this->time = $time;
  70. $this->tablepre = $tablepre;
  71. if($pconnect) {
  72. if(!$this->link = mysql_pconnect($dbhost, $dbuser, $dbpw)) {
  73. $this->halt('Can not connect to MySQL server');
  74. }
  75. } else {
  76. if(!$this->link = mysql_connect($dbhost, $dbuser, $dbpw, 1)) {
  77. $this->halt('Can not connect to MySQL server');
  78. }
  79. }
  80. if($this->version() > '4.1') {
  81. if($dbcharset) {
  82. mysql_query("SET character_set_connection=".$dbcharset.", character_set_results=".$dbcharset.", character_set_client=binary", $this->link);
  83. }
  84. if($this->version() > '5.0.1') {
  85. mysql_query("SET sql_mode=''", $this->link);
  86. }
  87. }
  88. if($dbname) {
  89. mysql_select_db($dbname, $this->link);
  90. }
  91. }
  92. function fetch_array($query, $result_type = MYSQL_ASSOC) {
  93. return mysql_fetch_array($query, $result_type);
  94. }
  95. function result_first($sql) {
  96. $query = $this->query($sql);
  97. return $this->result($query, 0);
  98. }
  99. function fetch_first($sql) {
  100. $query = $this->query($sql);
  101. return $this->fetch_array($query);
  102. }
  103. function fetch_all($sql) {
  104. $arr = array();
  105. $query = $this->query($sql);
  106. while($data = $this->fetch_array($query)) {
  107. $arr[] = $data;
  108. }
  109. return $arr;
  110. }
  111. function cache_gc() {
  112. $this->query("DELETE FROM {$this->tablepre}sqlcaches WHERE expiry<$this->time");
  113. }
  114. function query($sql, $type = '', $cachetime = FALSE) {
  115. $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
  116. if(!($query = $func($sql, $this->link)) && $type != 'SILENT') {
  117. $this->halt('MySQL Query Error', $sql);
  118. }
  119. $this->querynum++;
  120. $this->histories[] = $sql;
  121. return $query;
  122. }
  123. function affected_rows() {
  124. return mysql_affected_rows($this->link);
  125. }
  126. function error() {
  127. return (($this->link) ? mysql_error($this->link) : mysql_error());
  128. }
  129. function errno() {
  130. return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
  131. }
  132. function result($query, $row) {
  133. $query = @mysql_result($query, $row);
  134. return $query;
  135. }
  136. function num_rows($query) {
  137. $query = mysql_num_rows($query);
  138. return $query;
  139. }
  140. function num_fields($query) {
  141. return mysql_num_fields($query);
  142. }
  143. function free_result($query) {
  144. return mysql_free_result($query);
  145. }
  146. function insert_id() {
  147. return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
  148. }
  149. function fetch_row($query) {
  150. $query = mysql_fetch_row($query);
  151. return $query;
  152. }
  153. function fetch_fields($query) {
  154. return mysql_fetch_field($query);
  155. }
  156. function version() {
  157. return mysql_get_server_info($this->link);
  158. }
  159. function close() {
  160. return mysql_close($this->link);
  161. }
  162. function halt($message = '', $sql = '') {
  163. api_msg('run_sql_error', $message.'<br /><br />'.$sql.'<br /> '.mysql_error());
  164. }
  165. }
  166. $db = new dbstuff;
  167. $version = '';
  168. if($apptype == 'discuz') {
  169. define('BACKUP_DIR', ROOT_PATH.'forumdata/');
  170. $tablepre = $tablepre;
  171. if(empty($dbcharset)) {
  172. $dbcharset = in_array(strtolower($charset), array('gbk', 'big5', 'utf-8')) ? str_replace('-', '', $charset) : '';
  173. }
  174. $db->connect($dbhost, $dbuser, $dbpw, $dbname, $dbcharset, $pconnect, $tablepre);
  175. define('IN_DISCUZ', true);
  176. include ROOT_PATH.'discuz_version.php';
  177. $version = DISCUZ_VERSION;
  178. } elseif($apptype == 'uchome' || $apptype == 'supesite') {
  179. define('BACKUP_DIR', ROOT_PATH.'./data/');
  180. $tablepre = $_SC['tablepre'];
  181. $dbcharset = $_SC['dbcharset'];
  182. $db->connect($_SC['dbhost'], $_SC['dbuser'], $_SC['dbpw'], $_SC['dbname'], $dbcharset, $_SC['pconnect'], $tablepre);
  183. } elseif($apptype == 'ucenter') {
  184. define('BACKUP_DIR', ROOT_PATH.'./data/backup/');
  185. $tablepre = UC_DBTABLEPRE;
  186. $dbcharset = UC_DBCHARSET;
  187. $db->connect(UC_DBHOST, UC_DBUSER, UC_DBPW, UC_DBNAME, $dbcharset, UC_DBCONNECT, $tablepre);
  188. } elseif($apptype == 'ecmall') {
  189. define('BACKUP_DIR', ROOT_PATH.'./data/backup/');
  190. $tablepre = DB_PREFIX;
  191. $dbcharset = strtolower(str_replace('-', '', strstr(LANG, '-')));
  192. $cfg = parse_url(DB_CONFIG);
  193. if(empty($cfg['pass'])) {
  194. $cfg['pass'] = '';
  195. } else {
  196. $cfg['pass'] = urldecode($cfg['pass']);
  197. }
  198. $cfg['user'] = urldecode($cfg['user']);
  199. $cfg['path'] = str_replace('/', '', $cfg['path']);
  200. $db->connect($cfg['host'].':'.$cfg['port'], $cfg['user'], $cfg['pass'], $cfg['path'], $dbcharset, 0, $tablepre);
  201. } elseif($apptype == 'supev') {
  202. define('BACKUP_DIR', ROOT_PATH.'data/backup/');
  203. $tablepre = $tablepre;
  204. if(empty($dbcharset)) {
  205. $dbcharset = in_array(strtolower($_config['output']['charset']), array('gbk', 'big5', 'utf-8')) ? str_replace('-', '', CHARSET) : '';
  206. }
  207. $db->connect($dbhost, $dbuser, $dbpw, $dbname, $dbcharset, $pconnect, $tablepre);
  208. } elseif($apptype == 'ecshop') {
  209. define('BACKUP_DIR', ROOT_PATH.'data/backup/');
  210. $tablepre = $prefix;
  211. $dbcharset = 'utf8';
  212. $db->connect($db_host, $db_user, $db_pass, $db_name, $dbcharset, 0, $tablepre);
  213. } elseif($apptype == 'discuzx') {
  214. define('BACKUP_DIR', ROOT_PATH.'data/');
  215. extract($_config['db']['1']);
  216. if(empty($dbcharset)) {
  217. $dbcharset = in_array(strtolower(CHARSET), array('gbk', 'big5', 'utf-8')) ? str_replace('-', '', $_config['output']['charset']) : '';
  218. }
  219. $db->connect($dbhost, $dbuser, $dbpw, $dbname, $dbcharset, $pconnect, $tablepre);
  220. define('IN_DISCUZ', true);
  221. include ROOT_PATH.'source/discuz_version.php';
  222. $version = DISCUZ_VERSION;
  223. }
  224. if($get['method'] == 'export') {
  225. $db->query('SET SQL_QUOTE_SHOW_CREATE=0', 'SILENT');
  226. $time = date("Y-m-d H:i:s", $timestamp);
  227. $tables = array();
  228. $tables = arraykeys2(fetchtablelist($tablepre), 'Name');
  229. if($apptype == 'discuz') {
  230. $query = $db->query("SELECT datatables FROM {$tablepre}plugins WHERE datatables<>''");
  231. while($plugin = $db->fetch_array($query)) {
  232. foreach(explode(',', $plugin['datatables']) as $table) {
  233. if($table = trim($table)) {
  234. $tables[] = $table;
  235. }
  236. }
  237. }
  238. }
  239. if($apptype == 'discuzx') {
  240. $query = $db->query("SELECT datatables FROM {$tablepre}common_plugin WHERE datatables<>''");
  241. while($plugin = $db->fetch_array($query)) {
  242. foreach(explode(',', $plugin['datatables']) as $table) {
  243. if($table = trim($table)) {
  244. $tables[] = $table;
  245. }
  246. }
  247. }
  248. }
  249. $memberexist = array_search("{$tablepre}common_member", $tables);
  250. if($memberexist !== FALSE) {
  251. unset($tables[$memberexist]);
  252. array_unshift($tables, "{$tablepre}common_member");
  253. }
  254. $get['volume'] = isset($get['volume']) ? intval($get['volume']) : 0;
  255. $get['volume'] = $get['volume'] + 1;
  256. $version = $version ? $version : $apptype;
  257. $idstring = '# Identify: '.base64_encode("$timestamp,$version,$apptype,multivol,$get[volume]")."\n";
  258. if(!isset($get['sqlpath']) || empty($get['sqlpath'])) {
  259. $get['sqlpath'] = 'backup_'.date('ymd', $timestamp).'_'.random(6);
  260. if(!mkdir(BACKUP_DIR.'./'.$get['sqlpath'], 0777)) {
  261. api_msg('mkdir_error', 'make dir error:'.BACKUP_DIR.'./'.$get['sqlpath']);
  262. }
  263. } elseif(!is_dir(BACKUP_DIR.'./'.$get['sqlpath'])) {
  264. if(!mkdir(BACKUP_DIR.'./'.$get['sqlpath'], 0777)) {
  265. api_msg('mkdir_error', 'make dir error:'.BACKUP_DIR.'./'.$get['sqlpath']);
  266. }
  267. }
  268. if(!isset($get['backupfilename']) || empty($get['backupfilename'])) {
  269. $get['backupfilename'] = date('ymd', $timestamp).'_'.random(6);
  270. }
  271. $sqldump = '';
  272. $get['tableid'] = isset($get['tableid']) ? intval($get['tableid']) : 0;
  273. $get['startfrom'] = isset($get['startfrom']) ? intval($get['startfrom']) : 0;
  274. if(!$get['tableid'] && $get['volume'] == 1) {
  275. foreach($tables as $table) {
  276. $sqldump .= sqldumptablestruct($table);
  277. }
  278. }
  279. $complete = TRUE;
  280. for(; $complete && $get['tableid'] < count($tables) && strlen($sqldump) + 500 < $sizelimit * 1000; $get['tableid']++) {
  281. $sqldump .= sqldumptable($tables[$get['tableid']], strlen($sqldump));
  282. if($complete) {
  283. $get['startfrom'] = 0;
  284. }
  285. }
  286. !$complete && $get['tableid']--;
  287. $dumpfile = BACKUP_DIR.$get['sqlpath'].'/'.$get['backupfilename'].'-'.$get['volume'].'.sql';
  288. if(trim($sqldump)) {
  289. $sqldump = "$idstring".
  290. "# <?exit();?>\n".
  291. "# $apptype Multi-Volume Data Dump Vol.$get[volume]\n".
  292. "# Time: $time\n".
  293. "# Type: $apptype\n".
  294. "# Table Prefix: $tablepre\n".
  295. "# $dbcharset\n".
  296. "# $apptype Home: http://www.comsenz.com\n".
  297. "# Please visit our website for newest infomation about $apptype\n".
  298. "# --------------------------------------------------------\n\n\n".
  299. $sqldump;
  300. @$fp = fopen($dumpfile, 'wb');
  301. @flock($fp, 2);
  302. if(@!fwrite($fp, $sqldump)) {
  303. @fclose($fp);
  304. api_msg('database_export_file_invalid', $dumpfile);
  305. } else {
  306. fclose($fp);
  307. auto_next($get, $dumpfile);
  308. }
  309. } else {
  310. @touch(ROOT_PATH.$get['sqlpath'].'/index.htm');
  311. api_msg('explor_success', 'explor_success');
  312. }
  313. } elseif($get['method'] == 'import') {
  314. if(!isset($get['dumpfile']) || empty($get['dumpfile'])) {
  315. $get['dumpfile'] = get_dumpfile_by_path($get['sqlpath']);
  316. $get['volume'] = 0;
  317. }
  318. $get['volume']++;
  319. $next_dumpfile = preg_replace('/^(\d+)\_(\w+)\-(\d+)\.sql$/', '\\1_\\2-'.$get['volume'].'.sql', $get['dumpfile']);
  320. if(!is_file(BACKUP_DIR.$get['sqlpath'].'/'.$get['dumpfile'])) {
  321. if(is_file(BACKUP_DIR.$get['sqlpath'].'/'.$next_dumpfile)) {
  322. api_msg('bak_file_lose', $get['dumpfile']);
  323. } else {
  324. api_msg('import_success', 'import_success');
  325. }
  326. }
  327. $sqldump = file_get_contents(BACKUP_DIR.$get['sqlpath'].'/'.$get['dumpfile']);
  328. $sqlquery = splitsql($sqldump);
  329. unset($sqldump);
  330. foreach($sqlquery as $sql) {
  331. $sql = syntablestruct(trim($sql), $db->version() > '4.1', $dbcharset);
  332. if($sql != '') {
  333. $db->query($sql, 'SILENT');
  334. if(($sqlerror = $db->error()) && $db->errno() != 1062) {
  335. $db->halt('MySQL Query Error', $sql);
  336. }
  337. }
  338. }
  339. $cur_file = $get['dumpfile'];
  340. $get['dumpfile'] = $next_dumpfile;
  341. auto_next($get, BACKUP_DIR.$get['sqlpath'].'/'.$cur_file);
  342. } elseif($get['method'] == 'ping') {
  343. if($get['dir'] && is_dir(BACKUP_DIR.$get['dir'])) {
  344. echo "1";exit;
  345. } else {
  346. echo "-1";exit;
  347. }
  348. } elseif($get['method'] == 'list') {
  349. $str = "<root>\n";
  350. $directory = dir(BACKUP_DIR);
  351. while($entry = $directory->read()) {
  352. $filename = BACKUP_DIR.$entry;
  353. if(is_dir($filename) && preg_match('/backup_(\d+)_\w+$/', $filename, $match)) {
  354. $str .= "\t<dir>\n";
  355. $str .= "\t\t<dirname>$filename</dirname>\n";
  356. $str .= "\t\t<dirdate>$match[1]</dirdate>\n";
  357. $str .= "\t</dir>\n";
  358. }
  359. }
  360. $directory->close();
  361. $str .= "</root>";
  362. echo $str;
  363. exit;
  364. } elseif($get['method'] == 'view') {
  365. $sqlpath = trim($get['sqlpath']);
  366. if(empty($sqlpath) || !is_dir(BACKUP_DIR.$sqlpath)) {
  367. api_msg('dir_no_exists', $sqlpath);
  368. }
  369. $str = "<root>\n";
  370. $directory = dir(BACKUP_DIR.$sqlpath);
  371. while($entry = $directory->read()) {
  372. $filename = BACKUP_DIR.$sqlpath.'/'.$entry;
  373. if(is_file($filename) && preg_match('/\d+_\w+\-(\d+).sql$/', $filename, $match)) {
  374. $str .= "\t<file>\n";
  375. $str .= "\t\t<file_name>$match[0]</file_name>\n";
  376. $str .= "\t\t<file_size>".filesize($filename)."</file_size>\n";
  377. $str .= "\t\t<file_num>$match[1]</file_num>\n";
  378. $str .= "\t\t<file_url>".str_replace(ROOT_PATH, 'http://'.$_SERVER['HTTP_HOST'].'/', $filename)."</file_url>\n";
  379. $str .= "\t\t<last_modify>".filemtime($filename)."</last_modify>\n";
  380. $str .= "\t</file>\n";
  381. }
  382. }
  383. $directory->close();
  384. $str .= "</root>";
  385. echo $str;
  386. exit;
  387. } elseif($get['method'] == 'delete') {
  388. $sqlpath = trim($get['sqlpath']);
  389. if(empty($sqlpath) || !is_dir(BACKUP_DIR.$sqlpath)) {
  390. api_msg('dir_no_exists', $sqlpath);
  391. }
  392. $directory = dir(BACKUP_DIR.$sqlpath);
  393. while($entry = $directory->read()) {
  394. $filename = BACKUP_DIR.$sqlpath.'/'.$entry;
  395. if(is_file($filename) && preg_match('/\d+_\w+\-(\d+).sql$/', $filename) && !@unlink($filename)) {
  396. api_msg('delete_dumpfile_error', $filename);
  397. }
  398. }
  399. $directory->close();
  400. @rmdir(BACKUP_DIR.$sqlpath);
  401. api_msg('delete_sqlpath_success', 'delete_sqlpath_success');
  402. }
  403. function syntablestruct($sql, $version, $dbcharset) {
  404. if(strpos(trim(substr($sql, 0, 18)), 'CREATE TABLE') === FALSE) {
  405. return $sql;
  406. }
  407. $sqlversion = strpos($sql, 'ENGINE=') === FALSE ? FALSE : TRUE;
  408. if($sqlversion === $version) {
  409. return $sqlversion && $dbcharset ? preg_replace(array('/ character set \w+/i', '/ collate \w+/i', "/DEFAULT CHARSET=\w+/is"), array('', '', "DEFAULT CHARSET=$dbcharset"), $sql) : $sql;
  410. }
  411. if($version) {
  412. return preg_replace(array('/TYPE=HEAP/i', '/TYPE=(\w+)/is'), array("ENGINE=MEMORY DEFAULT CHARSET=$dbcharset", "ENGINE=\\1 DEFAULT CHARSET=$dbcharset"), $sql);
  413. } else {
  414. return preg_replace(array('/character set \w+/i', '/collate \w+/i', '/ENGINE=MEMORY/i', '/\s*DEFAULT CHARSET=\w+/is', '/\s*COLLATE=\w+/is', '/ENGINE=(\w+)(.*)/is'), array('', '', 'ENGINE=HEAP', '', '', 'TYPE=\\1\\2'), $sql);
  415. }
  416. }
  417. function splitsql($sql) {
  418. $sql = str_replace("\r", "\n", $sql);
  419. $ret = array();
  420. $num = 0;
  421. $queriesarray = explode(";\n", trim($sql));
  422. unset($sql);
  423. foreach($queriesarray as $query) {
  424. $ret[$num] = isset($ret[$num]) ? $ret[$num] : '';
  425. $queries = explode("\n", trim($query));
  426. foreach($queries as $query) {
  427. $ret[$num] .= isset($query[0]) && $query[0] == "#" ? NULL : $query;
  428. }
  429. $num++;
  430. }
  431. return($ret);
  432. }
  433. function get_dumpfile_by_path($path) {
  434. if(empty($path) || !is_dir(BACKUP_DIR.$path)) {
  435. api_msg('sqlpath_null_noexists', $path);
  436. }
  437. $directory = dir(BACKUP_DIR.$path);
  438. while($entry = $directory->read()) {
  439. $filename = BACKUP_DIR.$path.'/'.$entry;
  440. if(is_file($filename)) {
  441. if(preg_match('/^\d+\_\w+\-\d+\.sql$/', $entry)) {
  442. $file_bakfile = preg_replace('/^(\d+)\_(\w+)\-(\d+)\.sql$/', '\\1_\\2-1.sql', $entry);
  443. if(is_file(BACKUP_DIR.$path.'/'.$file_bakfile)) {
  444. return $file_bakfile;
  445. } else {
  446. api_msg('sqlpath_nomatch_bakfile', $path);
  447. }
  448. }
  449. }
  450. }
  451. $directory->close();
  452. api_msg('sqlpath_nomatch_bakfile', $path);
  453. }
  454. function api_msg($code, $msg) {
  455. $msg = htmlspecialchars($msg);
  456. $out = "<root>\n";
  457. $out .= "\t<error errorCode=\"".constant(strtoupper($code))."\" errorMessage=\"$msg\" />\n";
  458. $out .= "\t<fileinfo>\n";
  459. $out .= "\t\t<file_num></file_num>\n";
  460. $out .= "\t\t<file_size></file_size>\n";
  461. $out .= "\t\t<file_name></file_name>\n";
  462. $out .= "\t\t<file_url></file_url>\n";
  463. $out .= "\t\t<last_modify></last_modify>\n";
  464. $out .= "\t</fileinfo>\n";
  465. $out .= "\t<nexturl></nexturl>\n";
  466. $out .= "</root>";
  467. echo $out;
  468. exit;
  469. }
  470. function arraykeys2($array, $key2) {
  471. $return = array();
  472. foreach($array as $val) {
  473. $return[] = $val[$key2];
  474. }
  475. return $return;
  476. }
  477. function auto_next($get, $sqlfile) {
  478. $next_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?apptype='.$GLOBALS['apptype'].'&code='.urlencode(encode_arr($get));
  479. $out = "<root>\n";
  480. $out .= "\t<error errorCode=\"0\" errorMessage=\"ok\" />\n";
  481. $out .= "\t<fileinfo>\n";
  482. $out .= "\t\t<file_num>$get[volume]</file_num>\n";
  483. $out .= "\t\t<file_size>".filesize($sqlfile)."</file_size>\n";
  484. $out .= "\t\t<file_name>".basename($sqlfile)."</file_name>\n";
  485. $out .= "\t\t<file_url>".str_replace(ROOT_PATH, 'http://'.$_SERVER['HTTP_HOST'].'/', $sqlfile)."</file_url>\n";
  486. $out .= "\t\t<last_modify>".filemtime($sqlfile)."</last_modify>\n";
  487. $out .= "\t</fileinfo>\n";
  488. $out .= "\t<nexturl><![CDATA[$next_url]]></nexturl>\n";
  489. $out .= "</root>";
  490. echo $out;
  491. exit;
  492. }
  493. function encode_arr($get) {
  494. $tmp = '';
  495. foreach($get as $key => $val) {
  496. $tmp .= '&'.$key.'='.$val;
  497. }
  498. return _authcode($tmp, 'ENCODE', UC_KEY);
  499. }
  500. function sqldumptablestruct($table) {
  501. global $db;
  502. $createtable = $db->query("SHOW CREATE TABLE $table", 'SILENT');
  503. if(!$db->error()) {
  504. $tabledump = "DROP TABLE IF EXISTS $table;\n";
  505. } else {
  506. return '';
  507. }
  508. $create = $db->fetch_row($createtable);
  509. if(strpos($table, '.') !== FALSE) {
  510. $tablename = substr($table, strpos($table, '.') + 1);
  511. $create[1] = str_replace("CREATE TABLE $tablename", 'CREATE TABLE '.$table, $create[1]);
  512. }
  513. $tabledump .= $create[1];
  514. $tablestatus = $db->fetch_first("SHOW TABLE STATUS LIKE '$table'");
  515. $tabledump .= ($tablestatus['Auto_increment'] ? " AUTO_INCREMENT=$tablestatus[Auto_increment]" : '').";\n\n";
  516. return $tabledump;
  517. }
  518. function sqldumptable($table, $currsize = 0) {
  519. global $get, $db, $sizelimit, $startrow, $extendins, $sqlcompat, $sqlcharset, $dumpcharset, $usehex, $complete, $excepttables;
  520. $offset = 300;
  521. $tabledump = '';
  522. $tablefields = array();
  523. $query = $db->query("SHOW FULL COLUMNS FROM $table", 'SILENT');
  524. if(strexists($table, 'adminsessions')) {
  525. return ;
  526. } elseif(!$query && $db->errno() == 1146) {
  527. return;
  528. } elseif(!$query) {
  529. $usehex = FALSE;
  530. } else {
  531. while($fieldrow = $db->fetch_array($query)) {
  532. $tablefields[] = $fieldrow;
  533. }
  534. }
  535. $tabledumped = 0;
  536. $numrows = $offset;
  537. $firstfield = $tablefields[0];
  538. while($currsize + strlen($tabledump) + 500 < $sizelimit * 1000 && $numrows == $offset) {
  539. if($firstfield['Extra'] == 'auto_increment') {
  540. $selectsql = "SELECT * FROM $table WHERE $firstfield[Field] > $get[startfrom] LIMIT $offset";
  541. } else {
  542. $selectsql = "SELECT * FROM $table LIMIT $get[startfrom], $offset";
  543. }
  544. $tabledumped = 1;
  545. $rows = $db->query($selectsql);
  546. $numfields = $db->num_fields($rows);
  547. $numrows = $db->num_rows($rows);
  548. while($row = $db->fetch_row($rows)) {
  549. $comma = $t = '';
  550. for($i = 0; $i < $numfields; $i++) {
  551. $t .= $comma.($usehex && !empty($row[$i]) && (strexists($tablefields[$i]['Type'], 'char') || strexists($tablefields[$i]['Type'], 'text')) ? '0x'.bin2hex($row[$i]) : '\''.mysql_escape_string($row[$i]).'\'');
  552. $comma = ',';
  553. }
  554. if(strlen($t) + $currsize + strlen($tabledump) + 500 < $sizelimit * 1000) {
  555. if($firstfield['Extra'] == 'auto_increment') {
  556. $get['startfrom'] = $row[0];
  557. } else {
  558. $get['startfrom']++;
  559. }
  560. $tabledump .= "INSERT INTO $table VALUES ($t);\n";
  561. } else {
  562. $complete = FALSE;
  563. break 2;
  564. }
  565. }
  566. }
  567. $tabledump .= "\n";
  568. return $tabledump;
  569. }
  570. function random($length, $numeric = 0) {
  571. PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
  572. if($numeric) {
  573. $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
  574. } else {
  575. $hash = '';
  576. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
  577. $max = strlen($chars) - 1;
  578. for($i = 0; $i < $length; $i++) {
  579. $hash .= $chars[mt_rand(0, $max)];
  580. }
  581. }
  582. return $hash;
  583. }
  584. function fetchtablelist($tablepre = '') {
  585. global $db;
  586. $arr = explode('.', $tablepre);
  587. $dbname = isset($arr[1]) && $arr[1] ? $arr[0] : '';
  588. $tablepre = str_replace('_', '\_', $tablepre);
  589. $sqladd = $dbname ? " FROM $dbname LIKE '$arr[1]%'" : "LIKE '$tablepre%'";
  590. $tables = $table = array();
  591. $query = $db->query("SHOW TABLE STATUS $sqladd");
  592. while($table = $db->fetch_array($query)) {
  593. $table['Name'] = ($dbname ? "$dbname." : '').$table['Name'];
  594. $tables[] = $table;
  595. }
  596. return $tables;
  597. }
  598. function _stripslashes($string) {
  599. if(is_array($string)) {
  600. foreach($string as $key => $val) {
  601. $string[$key] = _stripslashes($val);
  602. }
  603. } else {
  604. $string = stripslashes($string);
  605. }
  606. return $string;
  607. }
  608. function _authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  609. $ckey_length = 4;
  610. $key = md5($key ? $key : UC_KEY);
  611. $keya = md5(substr($key, 0, 16));
  612. $keyb = md5(substr($key, 16, 16));
  613. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  614. $cryptkey = $keya.md5($keya.$keyc);
  615. $key_length = strlen($cryptkey);
  616. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  617. $string_length = strlen($string);
  618. $result = '';
  619. $box = range(0, 255);
  620. $rndkey = array();
  621. for($i = 0; $i <= 255; $i++) {
  622. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  623. }
  624. for($j = $i = 0; $i < 256; $i++) {
  625. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  626. $tmp = $box[$i];
  627. $box[$i] = $box[$j];
  628. $box[$j] = $tmp;
  629. }
  630. for($a = $j = $i = 0; $i < $string_length; $i++) {
  631. $a = ($a + 1) % 256;
  632. $j = ($j + $box[$a]) % 256;
  633. $tmp = $box[$a];
  634. $box[$a] = $box[$j];
  635. $box[$j] = $tmp;
  636. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  637. }
  638. if($operation == 'DECODE') {
  639. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  640. return substr($result, 26);
  641. } else {
  642. return '';
  643. }
  644. } else {
  645. return $keyc.str_replace('=', '', base64_encode($result));
  646. }
  647. }
  648. function strexists($haystack, $needle) {
  649. return !(strpos($haystack, $needle) === FALSE);
  650. }
  651. ?>