PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/api/dbbak.php

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