PageRenderTime 76ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/sxd/index.php

https://code.google.com/p/dcloner/
PHP | 1956 lines | 1849 code | 39 blank | 68 comment | 357 complexity | 27335392d0ba2140b1559026d049fda7 MD5 | raw file

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

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

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