PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/SypexDumpLib.php

https://code.google.com/p/dcloner/
PHP | 1344 lines | 1272 code | 25 blank | 47 comment | 325 complexity | 7bc1dca5d5e6d414f4ce81409f25abb7 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. if(!ini_get('zlib.output_compression') && function_exists('ob_gzhandler')) ob_start('ob_gzhandler');
  3. //error_reporting(E_ALL);
  4. error_reporting(0);
  5. set_error_handler('sxd_error_handler');
  6. register_shutdown_function('sxd_shutdown');
  7. set_magic_quotes_runtime(0);
  8. $SXD = new Sypex_Dumper();
  9. chdir(dirname(__FILE__));
  10. $SXD->init(!empty($argc) && $argc > 1 ? $argv : false);
  11. class Sypex_Dumper {
  12. function Sypex_Dumper() {
  13. define('C_DEFAULT', 1);
  14. define('C_RESULT', 2);
  15. define('C_ERROR', 3);
  16. define('C_WARNING', 4);
  17. define('SXD_DEBUG', false);
  18. define('TIMER', array_sum(explode(' ', microtime())));
  19. define('V_SXD', 20006);
  20. define('V_PHP', sxd_ver2int(phpversion()));
  21. $this->name = 'Sypex Dumper 2.0.6 Beta';
  22. }
  23. function loadLang($lng_name = 'auto'){
  24. if($lng_name == 'auto'){
  25. include('lang/list.php');
  26. $this->langs = &$langs;
  27. $lng = 'en';
  28. if(preg_match_all('/[a-z]{2}(-[a-z]{2})?/', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $m)) {
  29. foreach($m[0] AS $l){
  30. if(isset($langs[$l])){
  31. $lng_name = $l;
  32. break;
  33. }
  34. }
  35. }
  36. }
  37. if(file_exists("lang/lng_{$lng_name}.php")) include("lang/lng_{$lng_name}.php");
  38. else include("lang/lng_en.php");
  39. $this->LNG = &$LNG;
  40. $this->LNG['name'] = $lng_name;
  41. return true;
  42. }
  43. function init($args = false){
  44. if (get_magic_quotes_gpc()) {
  45. $_POST = sxd_antimagic($_POST);
  46. }
  47. include('cfg.php');
  48. $this->loadLang($CFG['lang']);
  49. if (!ini_get('safe_mode') && function_exists('set_time_limit') && strpos(ini_get('disable_functions'), 'set_time_limit') === false) @set_time_limit($CFG['time_web']);
  50. elseif (ini_get('max_execution_time') < $CFG['time_web']) $CFG['time_web'] = ini_get('max_execution_time');
  51. $this->CFG = &$CFG;
  52. $this->try = false;
  53. $this->virtualize = false;
  54. $this->cron_mode = false;
  55. // ????????? ??????????? ? ?????? ??????? ? ????
  56. if(empty($this->CFG['my_user'])){
  57. $this->CFG['my_host'] = 'localhost';
  58. $this->CFG['my_port'] = 3306;
  59. $this->CFG['my_user'] = 'root';
  60. $this->CFG['my_pass'] = '';
  61. $this->CFG['my_comp'] = 0;
  62. $this->CFG['my_db'] = '';
  63. }
  64. if ($args) { // ?????????? ?????
  65. foreach($args AS $key => $arg){
  66. if (preg_match("/^-([hupoj])=(.*?)$/", $arg, $m)){
  67. switch ($m[1]) {
  68. case 'h': $this->CFG['my_host'] = $m[2]; break; // ????
  69. case 'o': $this->CFG['my_port'] = $m[2]; break; // ????
  70. case 'u': $this->CFG['my_user'] = $m[2]; break; // ?????
  71. case 'p': $this->CFG['my_pass'] = $m[2]; break; // ??????
  72. case 'j': $this->CFG['sjob'] = $m[2]; break; // job-????
  73. }
  74. }
  75. }
  76. set_time_limit($CFG['time_cron']);
  77. // ????????? ?????? ????, ???? ?????
  78. $auth = $this->connect();
  79. if($auth && !empty($this->CFG['sjob'])){
  80. $this->ajax($this->loadJob($this->CFG['sjob']));
  81. echo file_get_contents($this->JOB['file_log']);
  82. if(file_exists($this->JOB['file_log'])) unlink($this->JOB['file_log']);
  83. if(file_exists($this->JOB['file_rtl'])) unlink($this->JOB['file_rtl']);
  84. }
  85. else echo 'Auth error';
  86. exit;
  87. }
  88. elseif(!empty($this->CFG['auth'])){ // ???????????
  89. $auth = false;
  90. $sfile = 'ses.php';
  91. if(!empty($_COOKIE['sxd']) && preg_match('/^[\da-f]{32}$/', $_COOKIE['sxd'])){
  92. include($sfile);
  93. if(isset($SES[$_COOKIE['sxd']])) {
  94. $auth = true;
  95. $this->CFG = $SES[$_COOKIE['sxd']]['cfg'];
  96. $this->SES = &$SES;
  97. $this->loadLang($this->CFG['lang']);
  98. }
  99. }
  100. if(!$auth) {
  101. $user = !empty($_POST['user']) ? $_POST['user'] : '';
  102. $pass = !empty($_POST['pass']) ? $_POST['pass'] : '';
  103. $host = !empty($_POST['host']) ? $_POST['host'] : 'localhost';
  104. $port = !empty($_POST['port']) && is_numeric($_POST['port']) ? $_POST['port'] : 3306;
  105. $temp = preg_split('/\s+/', $this->CFG['auth']);
  106. if(!empty($_REQUEST['lang']) && preg_match('/^[a-z]{2}(-[a-z]{2})?$/', $_REQUEST['lang'])) {$this->loadLang($_REQUEST['lang']);}
  107. foreach($temp AS $a){
  108. switch($a) {
  109. case 'cfg': if(empty($user)) {continue;}
  110. $auth = !empty($CFG['user']) && isset($CFG['pass']) && $CFG['user']== $user && $CFG['pass'] == $pass;
  111. break;
  112. case 'mysql': if(empty($user)) {continue;}
  113. $auth = $this->connect($host, $port, $user, $pass);
  114. break;
  115. default: $file = 'auth_' . $a . '.php';
  116. if(!file_exists($file)) continue;
  117. include $file;
  118. }
  119. if($auth) break;
  120. }
  121. if($auth){
  122. $key = md5(rand(1,100000) . $user . microtime());
  123. $CFG['lang'] = $this->LNG['name'];
  124. $_COOKIE['sxd'] = $key;
  125. $this->saveCFG();
  126. setcookie('sxd', $key, !empty($_POST['save']) ? time() + 31536000 : 0);
  127. header("Location: ./");
  128. exit;
  129. }
  130. foreach(array('user', 'pass', 'host', 'port') AS $key){
  131. $_POST[$key] = !empty($_POST[$key]) ? htmlspecialchars($_POST[$key], ENT_NOQUOTES) : '';
  132. }
  133. $_POST['save'] = !empty($_POST['save']) ? ' CHECKED' : '';
  134. }
  135. if (!$auth) {
  136. if(!empty($_POST['ajax'])){
  137. echo "sxd.hideLoading();alert('Session not found');";
  138. exit;
  139. }
  140. $this->lng_list = '<option value="auto">- auto -</opinion>';
  141. if(!isset($this->langs)) {include('lang/list.php');$this->langs = &$langs;}
  142. foreach($this->langs AS $k => $v){
  143. $this->lng_list .= "<option value=\"{$k}\"" . ($k == (!empty($_REQUEST['lang']) ? $this->LNG['name'] : $this->CFG['lang']) ? ' SELECTED' : '') . ">{$v}</opinion>";
  144. }
  145. echo sxd_tpl_auth();
  146. exit;
  147. }
  148. }
  149. /*if(!empty($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] == 'exit') {
  150. setcookie('sxd', '', 0);
  151. header("Location: {$this->CFG['exitURL']}");
  152. exit;
  153. }*/
  154. if(empty($_POST['ajax']['act']) || $_POST['ajax']['act'] != 'save_connect') $this->connect();
  155. if(isset($_POST['ajax'])) $this->ajax($_POST['ajax']);
  156. else $this->main();
  157. }
  158. function saveToFile($name, $content){
  159. $fp = fopen($name, "w");
  160. fwrite($fp, $content);
  161. fclose($fp);
  162. }
  163. function connect($host = null, $port = null, $user = null, $pass = null){
  164. $this->error = '';
  165. $this->try = true;
  166. if(!empty($user) && isset($pass)) {
  167. $this->CFG['my_host'] = $host;
  168. $this->CFG['my_port'] = $port;
  169. $this->CFG['my_user'] = $user;
  170. $this->CFG['my_pass'] = $pass;
  171. }
  172. if(mysql_connect($this->CFG['my_host'] . ($this->CFG['my_host']{0} != ':' ? ":{$this->CFG['my_port']}" : ''), $this->CFG['my_user'], $this->CFG['my_pass'])) {
  173. mysql_query('SET NAMES utf8') or sxd_my_error();
  174. define('V_MYSQL', sxd_ver2int(mysql_get_server_info()));
  175. }
  176. else {
  177. define('V_MYSQL', 0);
  178. $this->error = "sxd.actions.tab_connects();alert('{" . mysql_escape_string(mysql_error()) . "');";
  179. }
  180. $this->try = false;
  181. return V_MYSQL ? true: false;
  182. }
  183. function main(){
  184. // ??????
  185. $this->VAR['toolbar'] = sxd_php2json(
  186. array(
  187. array('backup', $this->LNG['tbar_backup'], 1, 3),
  188. array('restore', $this->LNG['tbar_restore'], 2, 3),
  189. array('|'),
  190. array('files', $this->LNG['tbar_files'], 3, 1),
  191. array('services', $this->LNG['tbar_services'], 5, 1),
  192. array('|'),
  193. array('createdb', $this->LNG['tbar_createdb'], 7, 0),
  194. array('connects', $this->LNG['tbar_connects'], 6, 0),
  195. array('|'),
  196. array('options', $this->LNG['tbar_options'], 4, 1),
  197. array('|'),
  198. array('exit', $this->LNG['tbar_exit'], 8, 1),
  199. )
  200. );
  201. $this->db = 'temp';
  202. $zip = array($this->LNG['zip_none']);
  203. if (function_exists("gzopen")) {
  204. for($i = 1; $i <10; $i++){
  205. $zip[] = "GZip: {$i}";
  206. }
  207. $zip[1] .= " ({$this->LNG['zip_min']})";
  208. $zip[7] .= " ({$this->LNG['default']})";
  209. }
  210. if (function_exists("bzopen")) {
  211. $zip[10] = "BZip";
  212. }
  213. end($zip);
  214. $zip[key($zip)] .= " ({$this->LNG['zip_max']})";
  215. $this->VAR['combos'] =
  216. $this->addCombo('backup_db', $this->db, 11, 'db', $this->getDBList()) .
  217. $this->addCombo('backup_charset', 0, 9, 'charset', $this->getCharsetList()) .
  218. $this->addCombo('backup_zip', 7, 10, 'zip', $zip) .
  219. $this->addCombo('restore_db', $this->db, 11, 'db') .
  220. $this->addCombo('restore_charset', 0, 9, 'charset') .
  221. $this->addCombo('restore_file', 0, 12, 'files', $this->getFileList()) .
  222. $this->addCombo('restore_type', 0, 13, 'types', array("CREATE + INSERT ({$this->LNG['default']})", 'TRUNCATE + INSERT', 'REPLACE', 'INSERT IGNORE')) .
  223. $this->addCombo('services_db', $this->db, 11, 'db') .
  224. $this->addCombo('services_check', 0, 5, 'check', array("- {$this->LNG['default']} -", 'QUICK', 'FAST', 'CHANGED', 'MEDIUM', 'EXTENDED')) .
  225. $this->addCombo('services_repair', 0, 5, 'repair', array("- {$this->LNG['default']} -", 'QUICK', 'EXTENDED')) .
  226. $this->addCombo('services_charset', 0, 9, 'collation', $this->getCollationList()) .
  227. $this->addCombo('services_charset_col', 0, 15, 'collation:services_charset').
  228. $this->addCombo('db_charset', 0, 9, 'collation') .
  229. $this->addCombo('db_charset_col', 0, 15, 'collation:db_charset')
  230. ;
  231. if (!V_MYSQL) $this->VAR['combos'] .= $this->error;
  232. $this->VAR['combos'] .= $this->getSavedJobs() . "sxd.confirms = {$this->CFG['confirm']};";
  233. $this->LNG['del_date'] = sprintf($this->LNG['del_date'], '<input type="text" id="del_time" class=txt style="width:24px;" maxlength="3">');
  234. $this->LNG['del_count'] = sprintf($this->LNG['del_count'], '<input id="del_count" type="text" class=txt style="width:18px;" maxlength="2">');
  235. echo sxd_tpl_page();
  236. }
  237. function loadJob($job){
  238. $file = $this->CFG['backup_path'] . 'sj_' . (is_array($job) ? $job['job'] : $job) . '.job.php';
  239. if(!file_exists($file)) return;
  240. include($file);
  241. $JOB['act'] = $JOB['type'];
  242. $JOB['type'] = 'run';
  243. return $JOB;
  244. }
  245. function deleteDB($name){
  246. $r = mysql_query('DROP DATABASE `' . mysql_escape_string($name) . '`') or sxd_my_error();
  247. if($r){
  248. echo "sxd.clearOpt('db');sxd.addOpt(" . sxd_php2json(array('db' => $this->getDBList())) . ");sxd.combos.services_db.select(0,'-');";
  249. }
  250. else
  251. echo "alert('" . mysql_escape_string(mysql_error()) . "')";
  252. }
  253. function cfg2js($cfg){
  254. foreach($cfg AS $k => $v){
  255. $cfg[$k] = mysql_escape_string($v);
  256. }
  257. return $cfg;
  258. }
  259. function addDb($req){
  260. $r = mysql_query('CREATE DATABASE `' . mysql_escape_string($req['name']) . '`' . (V_MYSQL > 40100 ? "CHARACTER SET {$req['charset']} COLLATE {$req['collate']}" : ''));
  261. if($r)
  262. echo "sxd.addOpt(" . sxd_php2json(array('db' => array($req['name'] => "{$req['name']} (0)"))) . ");";
  263. else
  264. sxd_my_error();
  265. }
  266. function saveConnect($req){
  267. $this->CFG['my_host'] = $req['host'];
  268. $this->CFG['my_port'] = (int)$req['port'];
  269. $this->CFG['my_user'] = $req['user'];
  270. $this->CFG['my_pass'] = $req['pass'];
  271. $this->CFG['my_comp'] = $req['comp'] ? 1 : 0;
  272. $this->CFG['my_db'] = $req['db'];
  273. $this->saveCFG();
  274. $this->connect();
  275. if (V_MYSQL) {
  276. $tmp = array(
  277. 'db' => $this->getDBList(),
  278. 'charset' => $this->getCharsetList(),
  279. 'collation' => $this->getCollationList()
  280. );
  281. echo "sxd.clearOpt('db');sxd.clearOpt('charset');sxd.clearOpt('collation');sxd.addOpt(" . sxd_php2json($tmp) . ");sxd.combos.backup_db.select(0,'-');sxd.combos.restore_db.select(0,'-');sxd.combos.services_db.select(0,'-');sxd.combos.backup_charset.select(0,'-');sxd.combos.services_db.select(0,'-');sxd.combos.db_charset.select(0,'-');";
  282. }
  283. else {
  284. echo $this->error;
  285. }
  286. }
  287. function saveOptions($req){
  288. $this->CFG['time_web'] = $req['time_web'];
  289. $this->CFG['time_cron'] = $req['time_cron'];
  290. $this->CFG['backup_path'] = $req['backup_path'];
  291. $this->CFG['backup_url'] = $req['backup_url'];
  292. $this->CFG['globstat'] = $req['globstat'] ? 1 : 0;
  293. $this->CFG['charsets'] = $req['charsets'];
  294. $this->CFG['only_create'] = $req['only_create'];
  295. $this->CFG['auth'] = $req['auth'];
  296. $this->CFG['confirm'] = $req['confirm'];
  297. $this->saveCFG();
  298. }
  299. function saveCFG(){
  300. if (isset($_COOKIE['sxd'])) {
  301. $this->SES[$_COOKIE['sxd']] = array('cfg' => $this->CFG, 'time' => time(), 'lng' => $this->LNG['name']);
  302. $this->saveToFile('ses.php', "<?php\n\$SES = " . var_export($this->SES, true) . ";\n" . "?>");
  303. }
  304. if (!$this->virtualize){
  305. $this->saveToFile('cfg.php', "<?php\n\$CFG = " . var_export($this->CFG, true) . ";\n" . "?>");
  306. }
  307. }
  308. function runServices($job) {
  309. $serv = array('optimize' => 'OPTIMIZE', 'analyze' => 'ANALYZE', 'check' => 'CHECK', 'repair' => 'REPAIR');
  310. $add = array('check' => array('', 'QUICK', 'FAST', 'CHANGED', 'MEDIUM', 'EXTENDED'), 'repair' => array('', 'QUICK', 'EXTENDED'));
  311. if(isset($serv[$job['type']])) {
  312. mysql_select_db($job['db']);
  313. $filter = $object = array();
  314. $this->createFilters($job['obj'], $filter, $object);
  315. $r = mysql_query('SHOW TABLE STATUS') or sxd_my_error();
  316. if (!$r) return;
  317. $tables = array();
  318. while($item = mysql_fetch_assoc($r)){
  319. if(V_MYSQL > 40101 && is_null($item['Engine']) && preg_match('/^VIEW/i', $item['Comment'])) continue;
  320. if(sxd_check($item['Name'], $object['TA'], $filter['TA'])) $tables[] = "`{$item['Name']}`";
  321. }
  322. $sql = $serv[$job['type']] . ' TABLE ' . implode(',', $tables);
  323. if ($job['type'] == 'check' || $job['type'] == 'repair') {
  324. $sql .= isset($add[$job['type']][$job[$job['type']]]) ? ' ' . $add[$job['type']][$job[$job['type']]] : '';
  325. }
  326. $r = mysql_query($sql) or sxd_my_error();
  327. if (!$r) return;
  328. $res = array();
  329. while($item = mysql_fetch_row($r)){
  330. $res[] = $item;
  331. }
  332. echo 'sxd.result.add(' . sxd_php2json($res). ');';
  333. }
  334. }
  335. function createFilters(&$obj, &$filter, &$object){
  336. $types = array('TA', 'TC', 'VI', 'PR', 'FU', 'TR', 'EV');
  337. foreach($types AS $type){
  338. $filter[$type] = array();
  339. $object[$type] = array();
  340. if(!empty($obj[$type])){
  341. foreach($obj[$type] AS $v){
  342. if(strpos($v, '*') !== false) {
  343. $filter[$type][] = str_replace('*', '.*?', $v);
  344. }
  345. else {
  346. $object[$type][$v] = true;
  347. }
  348. }
  349. $filter[$type] = count($filter[$type]) > 0 ? '/^(' . implode('|', $filter[$type]) . ')$/i' : '';
  350. }
  351. }
  352. }
  353. function closeConnect(){
  354. //return;
  355. ignore_user_abort(1);
  356. header("SXD: {$this->name}");
  357. header("Connection: close");
  358. header("Content-Length: 0");
  359. ob_end_flush();
  360. flush();
  361. }
  362. function resumeJob($job){
  363. $this->closeConnect();
  364. include($this->CFG['backup_path'] . $job['job'] . '.job.php');
  365. $this->JOB = &$JOB;
  366. if(file_exists($this->JOB['file_stp'])) unlink($this->JOB['file_stp']);
  367. $this->fh_rtl = fopen($this->JOB['file_rtl'], 'r+b');
  368. $this->fh_log = fopen($this->JOB['file_log'], 'ab');
  369. $t = fgets($this->fh_rtl);
  370. if(!empty($t)){
  371. $this->rtl = explode("\t", $t);
  372. }
  373. else {
  374. $this->addLog($this->LNG['not_found_rtl']);
  375. exit;
  376. }
  377. // TODO: ????????? ???????? ?????????
  378. //$this->rtl[6] = '';
  379. fseek($this->fh_rtl, 0);
  380. $this->rtl[1] = time();
  381. $this->rtl[9] = 0;
  382. fwrite($this->fh_rtl, implode("\t", $this->rtl));
  383. if ($this->JOB['act'] == 'backup') $this->runBackupJob(true);
  384. elseif ($this->JOB['act'] == 'restore') $this->runRestoreJob(true);
  385. }
  386. function addRestoreJob($job) {
  387. $this->closeConnect();
  388. $this->JOB = $job;
  389. // ??????? ?????? ???????? ? ??????
  390. $filter = $object = array();
  391. $this->createFilters($this->JOB['obj'], $filter, $object);
  392. $objects = $this->getFileObjects('restore', $this->JOB['file'], false);
  393. $todo = array();
  394. $rows = 0;
  395. $this->tab_rows = array();
  396. foreach($objects AS $t => $list){
  397. if($t == 'TA' && (!empty($object['TC']) || !empty($filter['TC']))) {}
  398. elseif(empty($object[$t]) && empty($filter[$t])) continue;
  399. if (empty($list)) continue;
  400. $todo[$t] = array();
  401. foreach($list AS $item){
  402. switch($t){
  403. case 'TA':
  404. $type = '';
  405. if(sxd_check($item[0], $object['TA'], $filter['TA'])){
  406. $type = empty($item[1]) ? 'TC' : 'TA';
  407. }
  408. elseif(sxd_check($item[0], $object['TC'], $filter['TC'])) {
  409. $type = 'TC';
  410. }
  411. else continue;
  412. $todo['TA'][] = array($type, $item[0], $item[1], $item[2]);
  413. $rows += $type == 'TA' ? $item[1] : 0;
  414. break;
  415. default:
  416. if(sxd_check($item, $object[$t], $filter[$t])) {
  417. $todo[$t][] = array($t, $item);
  418. }
  419. }
  420. }
  421. }
  422. $this->JOB['file_tmp'] = $this->JOB['file_name'] = $this->CFG['backup_path'] . $this->JOB['file'];
  423. $this->JOB['file_rtl'] = $this->CFG['backup_path'] . $this->JOB['job'] . '.rtl';
  424. $this->JOB['file_log'] = $this->CFG['backup_path'] . $this->JOB['job'] . '.log';
  425. $this->JOB['file_stp'] = $this->CFG['backup_path'] . $this->JOB['job'] . '.stp';
  426. if(file_exists($this->JOB['file_stp'])) unlink($this->JOB['file_stp']);
  427. $this->JOB['todo'] = $todo;
  428. $this->saveJob($this->JOB['job'], $this->JOB);
  429. $this->fh_tmp = $this->openFile($this->JOB['file_tmp'], 'r');
  430. $this->fh_rtl = fopen($this->JOB['file_rtl'], 'wb');
  431. $this->fh_log = fopen($this->JOB['file_log'], 'wb');
  432. $this->rtl = array(time(), time(), $rows, 0, '', '', '', 0, 0, 0, 0, TIMER, "\n");
  433. $this->addLog(sprintf($this->LNG['restore_begin'], $this->JOB['db']));
  434. $this->addLog("{$this->LNG['combo_file']} {$this->JOB['file']}");
  435. $this->runRestoreJob();
  436. }
  437. function runRestoreJob($continue = false){
  438. $ei = false;
  439. if($continue){
  440. $this->fh_tmp = $this->openFile($this->JOB['file_tmp'], 'r');
  441. fseek($this->fh_tmp, $this->rtl[3]);
  442. if(!empty($this->rtl[6])) $this->setNames($this->JOB['correct'] == 1 && !empty($this->JOB['charset']) ? $this->JOB['charset'] : $this->rtl[6]);
  443. if($this->rtl[7] < $this->rtl[10]) $ei = true;
  444. }
  445. mysql_select_db($this->JOB['db']);
  446. //mysql_query("SET NAMES 'UTF8'");
  447. $types = array('VI' => 'View', 'PR' => 'Procedure', 'FU' => 'Function', 'TR' => 'Trigger', 'EV' => 'Event');
  448. $fcache = '';
  449. $writes = 0;
  450. $old_charset = '';
  451. $tab = '';
  452. $seek = 0;
  453. $this->rtl[3] = ftell($this->fh_tmp);
  454. fseek($this->fh_rtl, 0);
  455. $this->rtl[1] = time();
  456. fwrite($this->fh_rtl, implode("\t", $this->rtl));
  457. $c = 0;
  458. switch($this->JOB['strategy']){
  459. case 1: $tc = 'TRUNCATE'; $td = 'INSERT'; break;
  460. case 2: $tc = ''; $td = 'REPLACE'; break;
  461. case 3: $tc = ''; $td = 'INSERT IGNORE'; break;
  462. default: $tc = 'DROP TABLE IF EXISTS'; $td = 'INSERT';
  463. }
  464. $tab_exists = array();
  465. if($this->JOB['strategy'] > 0){
  466. $r = mysql_query("SHOW TABLES") or sxd_my_error();
  467. while($item = mysql_fetch_row($r)){
  468. $tab_exists[$item[0]] = true;
  469. }
  470. }
  471. $insert = $continue && $this->rtl[7] < $this->rtl[10] ? "{$td} INTO `{$this->rtl[5]}` VALUES " : '';
  472. //$enable_index = array();
  473. if(V_MYSQL > 40014) {
  474. mysql_query("SET UNIQUE_CHECKS=0");
  475. mysql_query("SET FOREIGN_KEY_CHECKS=0");
  476. if(V_MYSQL > 40101) mysql_query("SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO'");
  477. if(V_MYSQL > 40111) mysql_query("SET SQL_NOTES=0");
  478. }
  479. $log_sql = false;
  480. $fields = '';
  481. $time_old = time();
  482. $exit_time = $time_old + $this->CFG['time_web'] - 1;
  483. while($q = sxd_read_sql($this->fh_tmp, $seek, $ei)){
  484. if($time_old < time()) {
  485. if(file_exists($this->JOB['file_stp'])){
  486. $type = file_get_contents($this->JOB['file_stp']);
  487. $this->rtl[9] = !empty($type) ? $type : 2;
  488. fseek($this->fh_rtl, 0);
  489. $this->rtl[1] = time();
  490. fwrite($this->fh_rtl, implode("\t", $this->rtl));
  491. /*if($type == 1) {
  492. }*/
  493. unset($this->rtl);
  494. exit;
  495. }
  496. $time_old = time();
  497. if($time_old >= $exit_time){
  498. $this->rtl[9] = 3;
  499. fseek($this->fh_rtl, 0);
  500. $this->rtl[1] = time();
  501. fwrite($this->fh_rtl, implode("\t", $this->rtl));
  502. unset($this->rtl);
  503. exit;
  504. }
  505. clearstatcache();
  506. }
  507. switch($q{0}){
  508. case '(':
  509. if($continue) {
  510. $this->addLog(sprintf("{$this->LNG['restore_TC']} {$this->LNG['continue_from']}", $this->rtl[5], $this->rtl[3]));
  511. $continue = false;
  512. }
  513. $q = $insert . $q;
  514. $ex = 1;
  515. $c = 1;
  516. break;
  517. case 'I':
  518. if (preg_match('/^INSERT( INTO `(.+?)`) VALUES/', $q, $m)) {
  519. $insert = $td . $m[1] . $fields . ' VALUES ';
  520. $tab = $m[2];
  521. $this->rtl[7] = 0;
  522. $this->rtl[8] = 0;
  523. foreach($this->JOB['todo']['TA'] AS $t){
  524. if($t[1] == $tab) {
  525. $this->rtl[8] = $t[2];
  526. }
  527. }
  528. if($this->JOB['strategy']) {
  529. $q = substr_replace($q, $insert, 0, strlen($m[0]));
  530. }
  531. //mysql_query("LOCK TABLES `{$tab}` WRITE") or die (mysql_error());
  532. mysql_query("ALTER TABLE `{$tab}` DISABLE KEYS") or sxd_my_error();
  533. if(!empty($this->JOB['autoinc'])) mysql_query("ALTER TABLE `{$tab}` AUTO_INCREMENT = 1") or sxd_my_error();
  534. $ex = 1;
  535. }
  536. break;
  537. case 'C':
  538. $ex = 1;
  539. if (preg_match('/^CREATE TABLE `/', $q)) {
  540. if($this->JOB['strategy'] != 0 && isset($tab_exists[$this->rtl[5]])) $ex = 0;
  541. else {
  542. $ex = 1;
  543. if((!empty($this->JOB['correct']) && !empty($this->JOB['charset']))){
  544. $q = preg_replace('/(DEFAULT)?\s*(CHARSET|CHARACTER SET|COLLATE)[=\s]+\w+/i', '', $q) . (V_MYSQL < 40100 ? '' : 'DEFAULT CHARSET=' . $this->JOB['charset']);
  545. }
  546. }
  547. // ??????? ????? ????? ???????
  548. $fields = $this->JOB['strategy'] > 0 && preg_match_all('/^\s+(`.+?`) /m', $q, $f, PREG_PATTERN_ORDER) ? '(' . implode(',', $f[1]) . ')' : '';
  549. }
  550. break;
  551. case '#': // ??????? ??? ???????
  552. if (preg_match("/\#\t(TC|TD|VI|PR|FU|TR|EV)`(.+?)`(([^_]+?)_.+?)?$/", $q, $m)) {
  553. //if(!empty($tab)) $enable_index[] = $tab;
  554. $this->setNames($this->JOB['correct'] == 1 && !empty($this->JOB['charset']) ? $this->JOB['charset'] : empty($m[3]) ? '' : $m[3]);
  555. if($m[1] == 'TC') {
  556. $this->addLog(sprintf($this->LNG['restore_TC'], $m[2]));
  557. $insert = '';
  558. $tab = '';
  559. $this->rtl[4] = 'TD';
  560. $this->rtl[5] = $m[2];
  561. $ei = 0;
  562. if($tc && ($this->JOB['strategy'] == 0 || isset($tab_exists[$m[2]]))) {
  563. mysql_query("{$tc} `{$m[2]}`") or sxd_my_error();
  564. }
  565. }
  566. elseif($m[1] == 'TD'){
  567. $ei = 1;
  568. }
  569. else {
  570. $this->rtl[4] = $m[1];
  571. $this->rtl[5] = $m[2];
  572. $this->rtl[7] = 0;
  573. $this->rtl[8] = 0;
  574. mysql_query("DROP {$types[$m[1]]} IF EXISTS `{$m[2]}`") or sxd_my_error();
  575. $this->addLog(sprintf($this->LNG["restore_{$m[1]}"], $m[2]));
  576. $ei = 0;
  577. }
  578. }
  579. $ex = 0;
  580. break;
  581. default:
  582. $insert = '';
  583. $ex = 1;
  584. }
  585. if($ex) {
  586. //error_log("[index.php]\n{$q}\n", 3, "error.log");
  587. $this->rtl[3] = ftell($this->fh_tmp) - $seek;
  588. fseek($this->fh_rtl, 0);
  589. $this->rtl[1] = time();
  590. fwrite($this->fh_rtl, implode("\t", $this->rtl));
  591. if(mysql_query($q)) {
  592. if($insert) {
  593. $c = 1;
  594. }
  595. }
  596. else {
  597. error_log("[index.php]\n{$q}\n", 3, "error.log");
  598. sxd_my_error();
  599. }
  600. if($c){
  601. $i = mysql_affected_rows();
  602. $this->rtl[3] = ftell($this->fh_tmp) - $seek;
  603. $this->rtl[7] += $i;
  604. $this->rtl[10] += $i;
  605. fseek($this->fh_rtl, 0);
  606. $this->rtl[1] = time();
  607. fwrite($this->fh_rtl, implode("\t", $this->rtl));
  608. $c = 1;
  609. }
  610. }
  611. }
  612. // ???????? ?????
  613. $this->addLog($this->LNG['restore_keys']);
  614. $this->rtl[4] = 'EK';
  615. $this->rtl[5] = '';
  616. $this->rtl[6] = '';
  617. $this->rtl[7] = 0;
  618. $this->rtl[8] = 0;
  619. foreach($this->JOB['todo']['TA'] AS $tab){
  620. if ($tab[0] == 'TC') continue;
  621. mysql_query("ALTER TABLE `{$tab[1]}` ENABLE KEYS") or sxd_my_error();
  622. $this->rtl[1] = time();
  623. $this->rtl[5] = $tab[1];
  624. fseek($this->fh_rtl, 0);
  625. fwrite($this->fh_rtl, implode("\t", $this->rtl));
  626. }
  627. $this->rtl[4] = 'EOJ';
  628. $this->rtl[5] = round(array_sum(explode(' ', microtime())) - $this->rtl[11], 4);
  629. fseek($this->fh_rtl, 0);
  630. fwrite($this->fh_rtl, implode("\t", $this->rtl));
  631. $this->addLog(sprintf($this->LNG['restore_end'], $this->JOB['db']));
  632. fclose($this->fh_log);
  633. fclose($this->fh_rtl);
  634. }
  635. function addBackupJob($job) {
  636. $this->closeConnect();
  637. // ??????? ????? ???????
  638. $this->JOB = $job;
  639. mysql_select_db($this->JOB['db']);
  640. // ??????? ?????? ???????? ? ??????
  641. $filter = $object = array();
  642. $this->createFilters($this->JOB['obj'], $filter, $object);
  643. $queries = array(
  644. array('TABLE STATUS', 'Name', 'TA')
  645. );
  646. if (V_MYSQL > 50014) {
  647. $queries[] = array("PROCEDURE STATUS WHERE db='{$this->JOB['db']}'", 'Name', 'PR');
  648. $queries[] = array("FUNCTION STATUS WHERE db='{$this->JOB['db']}'", 'Name', 'FU');
  649. $queries[] = array('TRIGGERS', 'Trigger', 'TR');
  650. if(V_MYSQL > 50100) $queries[] = array('EVENTS', 'Name', 'EV');
  651. }
  652. $todo = $header = array();
  653. $tabs = $rows = 0;
  654. $only_create = explode(' ', $this->CFG['only_create']);
  655. foreach($queries AS $query){
  656. $t = $query[2];
  657. if($t == 'TA' && (!empty($object['TC']) || !empty($filter['TC']))) {}
  658. elseif(empty($object[$t]) && empty($filter[$t])) continue;
  659. $r = mysql_query('SHOW ' . $query[0]) or sxd_my_error();
  660. if (!$r) continue;
  661. $todo[$t] = array();
  662. $header[$t] = array();
  663. while($item = mysql_fetch_assoc($r)){
  664. $n = $item[$query[1]];
  665. switch($t){
  666. case 'TA':
  667. case 'TC':
  668. if(V_MYSQL > 40101 && is_null($item['Engine']) && preg_match('/^VIEW/i', $item['Comment'])) {
  669. if(sxd_check($n, $object['VI'], $filter['VI'])){
  670. $todo['VI'] = array();
  671. $header['VI']= array();
  672. }
  673. continue;
  674. }
  675. elseif(sxd_check($n, $object['TA'], $filter['TA'])){
  676. $engine = V_MYSQL > 40101 ? $item['Engine'] : $item['Type'];
  677. $t = in_array($engine, $only_create) ? 'TC' : 'TA';
  678. }
  679. elseif(sxd_check($n, $object['TC'], $filter['TC'])) {
  680. $t = 'TC';
  681. $item['Rows'] = $item['Data_length'] = '';
  682. }
  683. else continue;
  684. $todo['TA'][] = array($t, $n, !empty($item['Collation']) ? $item['Collation'] : '', $item['Auto_increment'], $item['Rows'], $item['Data_length']);
  685. $header['TA'][] = "{$n}`{$item['Rows']}`{$item['Data_length']}";
  686. $tabs++;
  687. $rows += $item['Rows'];
  688. break;
  689. default:
  690. if(sxd_check($n, $object[$t], $filter[$t])) {
  691. $todo[$t][] = array($t, $n, !empty($item['collation_connection']) ? $item['collation_connection'] : '');
  692. $header[$t][] = $n;
  693. }
  694. }
  695. }
  696. }
  697. if (V_MYSQL > 50014 && (!empty($object['VI']) || !empty($filter['VI']))) {
  698. // ????? ???????, ????? ????????????? ?????????
  699. $r = mysql_query("SELECT table_name, view_definition /*!50121 , collation_connection */ FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = '{$this->JOB['db']}'") or sxd_my_error();
  700. $views = $dumped = $views_collation = array();
  701. $re = "/`{$this->JOB['db']}`.`(.+?)`/";
  702. while($item = mysql_fetch_assoc($r)){
  703. preg_match_all($re, preg_replace("/^select.+? from/i", '', $item['view_definition']), $m);
  704. $used = $m[1];
  705. $views_collation[$item['table_name']] = !empty($item['collation_connection']) ? $item['collation_connection'] : '';
  706. $views[$item['table_name']] = $used;
  707. }
  708. while (count($views) > 0) {
  709. foreach($views AS $n => $view) {
  710. $can_dumped = true;
  711. foreach($view AS $k) {
  712. if (isset($views[$k]) && !isset($dumped[$k])) $can_dumped = false;
  713. }
  714. if ($can_dumped) {
  715. if(sxd_check($n, $object['VI'], $filter['VI'])){
  716. $todo['VI'][] = array('VI', $n, $views_collation[$n]);
  717. $header['VI'][] = $n;
  718. }
  719. $dumped[$n] = 1;
  720. unset($views[$n]);
  721. }
  722. }
  723. }
  724. unset($dumped);
  725. unset($views);
  726. unset($views_collation);
  727. }
  728. $this->JOB['file_tmp'] = $this->CFG['backup_path'] . $this->JOB['job'] . '.tmp';
  729. $this->JOB['file_rtl'] = $this->CFG['backup_path'] . $this->JOB['job'] . '.rtl';
  730. $this->JOB['file_log'] = $this->CFG['backup_path'] . $this->JOB['job'] . '.log';
  731. $this->JOB['file_stp'] = $this->CFG['backup_path'] . $this->JOB['job'] . '.stp';
  732. if(file_exists($this->JOB['file_stp'])) unlink($this->JOB['file_stp']);
  733. $this->fh_tmp = $this->openFile($this->JOB['file_tmp'], 'w');
  734. $this->JOB['file'] = sprintf('%s_%s.%s', (isset($this->JOB['title']) ? $this->JOB['job'] : $this->JOB['db']), date('Y-m-d_H-i-s'), $this->JOB['file_ext']);
  735. $this->JOB['file_name'] = $this->CFG['backup_path'] . $this->JOB['file'];
  736. $this->JOB['todo'] = $todo;
  737. $this->saveJob($this->JOB['job'], $this->JOB);
  738. $fcache = implode('|', array('#SXD20', V_SXD, V_MYSQL, V_PHP, date('Y.m.d H:i:s'), $this->JOB['db'], $this->JOB['charset'], $tabs, $rows, mysql_escape_string($this->JOB['comment']))) . "\n";
  739. foreach($header AS $t => $o){
  740. if (!empty($o)) $fcache .= "#{$t} " . implode('|', $o) . "\n";
  741. }
  742. $this->fh_rtl = fopen($this->JOB['file_rtl'], 'wb');
  743. $this->fh_log = fopen($this->JOB['file_log'], 'wb');
  744. $this->rtl = array(time(), time(), $rows, 0, '', '', '', 0, 0, 0, 0, TIMER, "\n");
  745. $fcache .= "#EOH\n\n";
  746. $this->write($fcache);
  747. $this->addLog(sprintf($this->LNG['backup_begin'], $this->JOB['db']));
  748. $this->runBackupJob();
  749. }
  750. function runBackupJob($continue = false){
  751. if($continue){
  752. $this->fh_tmp = $this->openFile($this->JOB['file_tmp'], 'a');
  753. mysql_select_db($this->JOB['db']);
  754. }
  755. mysql_query("SET SQL_QUOTE_SHOW_CREATE = 1");
  756. $types = array('VI' => 'View', 'PR' => 'Procedure', 'FU' => 'Function', 'TR' => 'Trigger', 'EV' => 'Event');
  757. $fcache = '';
  758. $writes = 0;
  759. mysql_query("SET SESSION character_set_results = '" . ($this->JOB['charset'] ? $this->JOB['charset'] : 'binary') ."'") or sxd_my_error();
  760. $time_old = time();
  761. $exit_time = $time_old + $this->CFG['time_web'] - 1;
  762. $no_cache = V_MYSQL < 40101 ? 'SQL_NO_CACHE ' : '';
  763. foreach($this->JOB['todo'] AS $t => $o){
  764. if (empty($this->rtl[4])) $this->rtl[4] = $t;
  765. elseif ($this->rtl[4] != $t) continue;
  766. foreach($o AS $n){
  767. if (empty($this->rtl[5])) {
  768. $this->rtl[5] = $n[1];
  769. $this->rtl[7] = 0;
  770. $this->rtl[8] = !empty($n[4]) ? $n[4] : 0;
  771. }
  772. elseif ($this->rtl[5] != $n[1]) continue;
  773. // ?????? ?????
  774. switch($n[0]){
  775. case 'TC':
  776. case 'TD':
  777. case 'TA':
  778. $from = '';
  779. if ($n[0] == 'TC' || $this->rtl[7] == 0){
  780. // ????? ????????? ???????
  781. $r = mysql_query("SHOW CREATE TABLE `{$n[1]}`") or sxd_my_error();
  782. $item = mysql_fetch_assoc($r);
  783. $fcache .= "#\tTC`{$n[1]}`{$n[2]}\t;\n{$item['Create Table']}\t;\n";
  784. $this->addLog(sprintf($this->LNG['backup_TC'], $n[1]));
  785. $this->rtl[7] = 0;
  786. if($n[0] == 'TC' || !$n[4]) break;
  787. // ??????? ?????? ???????
  788. $fcache .= "#\tTD`{$n[1]}`{$n[2]}\t;\nINSERT INTO `{$n[1]}` VALUES \n";
  789. }
  790. else {
  791. $from = " LIMIT {$this->rtl[7]}, {$this->rtl[8]}";
  792. $this->addLog(sprintf("{$this->LNG['backup_TC']} {$this->LNG['continue_from']}", $n[1], $this->rtl[7]));
  793. }
  794. // ?????????? ???? ?????
  795. $notNum = array();
  796. $r = mysql_query("SHOW COLUMNS FROM `{$n[1]}`") or sxd_my_error();
  797. $fields = 0;
  798. while($col = mysql_fetch_array($r)) {
  799. // TODO: ????????? ???? SET, ENUM ? BIT
  800. $notNum[$fields] = preg_match("/^(tinyint|smallint|mediumint|bigint|int|float|double|real|decimal|numeric|year)/", $col['Type']) ? 0 : 1;
  801. $fields++;
  802. }
  803. $time_old = time();
  804. $z = 0;
  805. // ??????? ??????
  806. $r = mysql_unbuffered_query("SELECT {$no_cache}* FROM `{$n[1]}`{$from}");
  807. while($row = mysql_fetch_row($r)) {
  808. if (strlen($fcache) >= 61440) {
  809. $z = 0;
  810. if($time_old < time()) {
  811. if(file_exists($this->JOB['file_stp'])){
  812. $type = file_get_contents($this->JOB['file_stp']);
  813. $this->rtl[9] = !empty($type) ? $type : 2;
  814. $this->write($fcache);
  815. if($type == 1) {
  816. }
  817. unset($this->rtl);
  818. exit;
  819. }
  820. $time_old = time();
  821. if($time_old >= $exit_time){
  822. $this->rtl[9] = 3;
  823. $this->write($fcache);
  824. unset($this->rtl);
  825. exit;
  826. }
  827. clearstatcache();
  828. }
  829. $this->write($fcache);
  830. }
  831. for($k = 0; $k < $fields; $k++){
  832. if(!isset($row[$k])) {$row[$k] = '\N';}
  833. elseif($notNum[$k]) {$row[$k] = '\'' . mysql_escape_string($row[$k]) . '\'';}
  834. }
  835. $fcache .= '(' . implode(',', $row) . "),\n";
  836. $this->rtl[7]++;
  837. $this->rtl[10]++;
  838. }
  839. unset($row);
  840. mysql_free_result($r);
  841. $fcache = substr_replace($fcache, "\t;\n", -2, 2);
  842. break;
  843. /*case 'VI':
  844. case 'PR':
  845. case 'FU':
  846. case 'EV':
  847. case 'TR':*/
  848. default:
  849. if(V_MYSQL < 50121 && $n[0] == 'TR'){
  850. // SHOW CREATE TRIGGER ??????????? ?? MySQL 5.1.21
  851. $r = mysql_query("SELECT * FROM `INFORMATION_SCHEMA`.`TRIGGERS` WHERE `TRIGGER_SCHEMA` = '{$this->JOB['db']}' AND `TRIGGER_NAME` = '{$n[1]}'") or sxd_my_error();
  852. $item = mysql_fetch_assoc($r);
  853. $fcache .= "#\tTR`{$n[1]}`{$n[2]}\t;\nCREATE TRIGGER `{$item['TRIGGER_NAME']}` {$item['ACTION_TIMING']} {$item['EVENT_MANIPULATION']} ON `{$item['EVENT_OBJECT_TABLE']}` FOR EACH ROW {$item['ACTION_STATEMENT']}\t;\n";
  854. }
  855. else {
  856. $this->addLog(sprintf($this->LNG['backup_' . $n[0]], $n[1]));
  857. $r = mysql_query("SHOW CREATE {$types[$n[0]]} `{$n[1]}`") or sxd_my_error();
  858. $item = mysql_fetch_assoc($r);
  859. $fcache .= "#\t{$n[0]}`{$n[1]}`{$n[2]}\t;\n" . preg_replace("/DEFINER=`.+?`@`.+?` /", '', ($n[0] == 'TR' ? $item['SQL Original Statement'] : $item['Create ' . $types[$n[0]]])) . "\t;\n";
  860. }
  861. }
  862. $this->rtl[5] = '';
  863. }
  864. $this->rtl[4] = '';
  865. }
  866. $this->rtl[4] = 'EOJ';
  867. $this->rtl[5] = round(array_sum(explode(' ', microtime())) - $this->rtl[11], 4);
  868. $this->rtl[6] = '';
  869. $this->rtl[7] = 0;
  870. $this->rtl[8] = 0;
  871. $this->write($fcache);
  872. fclose($this->fh_tmp);
  873. rename($this->JOB['file_tmp'], $this->JOB['file_name']);
  874. $this->addLog(sprintf($this->LNG['backup_end'], $this->JOB['db']));
  875. if ($this->JOB['del_time'] || $this->JOB['del_count']) {
  876. $this->addLog($this->LNG['autodelete']);
  877. $deldate = '';
  878. if (!empty($this->JOB['del_time'])){ // ???????? ?? ????
  879. $deldate = date("Y-m-d_H-i-s", time() - intval($this->JOB['del_time']) * 86400);
  880. }
  881. $deleted = false;
  882. if ($dh = opendir($this->CFG['backup_path'])) {
  883. $files = array();
  884. $name = isset($this->JOB['title']) ? $this->JOB['job'] : $this->JOB['db'];
  885. while (false !== ($file = readdir($dh))) {
  886. if (preg_match("/^{$name}_(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})\.sql/", $file, $m)) {
  887. if ($deldate && $m[1] < $deldate) {
  888. if(unlink($this->CFG['backup_path'] . $file)) $this->addLog(sprintf($this->LNG['del_by_date'], $file));
  889. else $this->addLog(sprintf($this->LNG['del_fail'], $file));
  890. $deleted = true;
  891. }
  892. else {$files[$m[1]] = $file;}
  893. }
  894. }
  895. closedir($dh);
  896. // ????????? ????? ?? ???? ? ??????? ????? ??????
  897. if (!empty($this->JOB['del_count'])){
  898. ksort($files);
  899. $file_to_delete = count($files) - $this->JOB['del_count'];
  900. foreach ($files AS $file){
  901. if ($file_to_delete-- > 0){
  902. if(unlink($this->CFG['backup_path'] . $file)) $this->addLog(sprintf($this->LNG['del_by_count'], $file));
  903. else $this->addLog(sprintf($this->LNG['del_fail'], $file));
  904. $deleted = true;
  905. }
  906. }
  907. }
  908. }
  909. if(!$deleted) $this->addLog($this->LNG['del_nothing']);
  910. }
  911. fclose($this->fh_log);
  912. fclose($this->fh_rtl);
  913. }
  914. function setNames($collation){
  915. if(empty($collation)) return;
  916. if($this->rtl[6] != $collation) {
  917. mysql_query('SET NAMES \'' . preg_replace('/^(\w+?)_/', '\\1\' COLLATE \'\\1_', $collation) . '\'') or sxd_my_error();
  918. /*if(!$this->rtl[7])*/ $this->addLog(sprintf($this->LNG['set_names'], $collation));
  919. $this->rtl[6] = $collation;
  920. }
  921. }
  922. function write(&$str){
  923. fseek($this->fh_rtl, 0);
  924. $this->rtl[1] = time();
  925. $this->rtl[3] += fwrite($this->fh_tmp, $str);
  926. fwrite($this->fh_rtl, implode("\t", $this->rtl));
  927. $str = '';
  928. }
  929. function addLog($str, $type = 1){
  930. fwrite($this->fh_log, date('Y.m.d H:i:s') . "\t{$type}\t{$str}\n");
  931. }
  932. function getDBList(){
  933. $dbs = $items = array();
  934. if (!V_MYSQL) return $dbs;
  935. $qq = (V_MYSQL < 50000) ? '' : '\'';
  936. if ($this->CFG['my_db']) {
  937. $tmp = explode(',', $this->CFG['my_db']);
  938. foreach($tmp AS $d){
  939. $d = trim($d);
  940. $items[] = $qq . mysql_escape_string($d) . $qq;
  941. $dbs[$d] = "{$d} (0)";
  942. }
  943. }
  944. else{
  945. $result = mysql_query("SHOW DATABASES") or sxd_my_error();
  946. while($item = mysql_fetch_row($result)){
  947. if($item[0] == 'information_schema' || $item[0] == 'mysql') continue;
  948. $items[] = $qq . mysql_escape_string($item[0]) . $qq;
  949. $dbs[$item[0]] = "{$item[0]} (0)";
  950. }
  951. }
  952. if(V_MYSQL < 50000){
  953. foreach($items AS $item){
  954. $tables = mysql_query("SHOW TABLES FROM `{$item}`") or sxd_my_error();
  955. if ($tables) {
  956. $tabs = mysql_num_rows($tables);
  957. $dbs[$item] = "{$item} ({$tabs})";
  958. }
  959. }
  960. }
  961. else {
  962. $where = (count($items) > 0) ? 'WHERE `table_schema` IN (' . implode(',', $items) . ')' : '';
  963. $result = mysql_query("SELECT `table_schema`, COUNT(*) FROM `information_schema`.`tables` {$where} GROUP BY `table_schema`") or sxd_my_error();
  964. while($item = mysql_fetch_row($result)){
  965. if($item[0] == 'information_schema' || $item[0] == 'mysql') continue;
  966. $dbs[$item[0]] = "{$item[0]} ({$item[1]})";
  967. }
  968. }
  969. return $dbs;
  970. }
  971. function getCharsetList(){
  972. $tmp = array(0 => '- auto -');
  973. if (!V_MYSQL) return $tmp;
  974. if(V_MYSQL > 40101) {
  975. $def_charsets = '';
  976. if(!empty($this->CFG['charsets'])){
  977. $def_charsets = preg_match_all("/([\w*?]+)\s*/", $this->CFG['charsets'], $m, PREG_PATTERN_ORDER) ? '/^(' . str_replace(array('?','*'), array('.','\w+?'), implode('|', $m[1])) . ')$/i' : '';
  978. }
  979. $r = mysql_query("SHOW CHARACTER SET") or sxd_my_error();
  980. if ($r) {
  981. while($item = mysql_fetch_assoc($r)){
  982. if (empty($def_charsets) || preg_match($def_charsets, $item['Charset'])) $tmp[$item['Charset']] = "{$item['Charset']}"; // ({$item['Description']})
  983. }
  984. }
  985. }
  986. return $tmp;
  987. }
  988. function getCollationList(){
  989. $tmp = array();
  990. if (!V_MYSQL) return $tmp;
  991. if(V_MYSQL > 40101) {
  992. $def_charsets = '';
  993. if(!empty($this->CFG['charsets'])){
  994. $def_charsets =

Large files files are truncated, but you can click here to view the full file