class MySQLImport { /** @var callable function (int $count, ?float $percent): void */ public $onProgress; /** @var mysqli */ private $connection; /** * Connects to database. * @param mysqli connection */ public function __construct(mysqli $connection, $charset = 'utf8') { $this->connection = $connection; if ($connection->connect_errno) { throw new Exception($connection->connect_error); } elseif (!$connection->set_charset($charset)) { // was added in MySQL 5.0.7 and PHP 5.0.5, fixed in PHP 5.1.5) throw new Exception($connection->error); } } /** * Loads dump from the file. * @param string filename * @return int */ public function load($file) { $handle = strcasecmp(substr($file, -3), '.gz') ? fopen($file, 'rb') : gzopen($file, 'rb'); if (!$handle) { throw new Exception("ERROR: Cannot open file '$file'."); } return $this->read($handle); } /** * Reads dump from logical file. * @param resource * @return int */ public function read($handle) { if (!is_resource($handle) || get_resource_type($handle) !== 'stream') { throw new Exception('Argument must be stream resource.'); } $stat = fstat($handle); $sql = ''; $delimiter = ';'; $count = $size = 0; while (!feof($handle)) { $s = fgets($handle); $size += strlen($s); if (strtoupper(substr($s, 0, 10)) === 'DELIMITER ') { $delimiter = trim(substr($s, 10)); } elseif (substr($ts = rtrim($s), -strlen($delimiter)) === $delimiter) { $sql .= substr($ts, 0, -strlen($delimiter)); if (!$this->connection->query($sql)) { throw new Exception($this->connection->error); } $sql = ''; $count++; if ($this->onProgress) { call_user_func($this->onProgress, $count, isset($stat['size']) ? $size * 100 / $stat['size'] : null); } } else { $sql .= $s; } } if (rtrim($sql) !== '') { $count++; if (!$this->connection->query($sql)) { throw new Exception($this->connection->error); } if ($this->onProgress) { call_user_func($this->onProgress, $count, isset($stat['size']) ? 100 : null); } } return $count; } } if ( $_POST['action'] == 'Test połączenia' ) { $mysqli = new mysqli( $_POST['host'], $_POST['user'], $_POST['password'], $_POST['name'] ); if ( mysqli_connect_errno() ) echo '
Nie można nawiązać połączenia:
' . mysqli_connect_error() . '
Połączenie nawiązane
'; mysqli_close( $mysqli ); } if ( $_POST['action'] == 'install' ) { $pliki = glob( '*.{sql}', GLOB_BRACE ); if ( $pliki !== false ) { foreach ( $pliki as $plik ) { $FileNameSQL = basename( $plik ); } } $connection = mysqli_connect( $_POST['host'], $_POST['user'], $_POST['password'], $_POST['name'] ); mysqli_set_charset( $connection, 'utf8' ); $import = new MySQLImport( $connection ); $import->load( $FileNameSQL ); $plikiZ = glob( '*.{zip}', GLOB_BRACE ); if ( $plikiZ !== false ) { foreach ( $plikiZ as $plik ) { $FileNameZip = basename( $plik ); } } $file = file_get_contents( $FileNameZip, "r" ); $path = pathinfo( realpath( $FileNameZip ), PATHINFO_DIRNAME ); $path = substr( $path, 0, strlen( $path ) ); $zip = new \ZipArchive; $res = $zip -> open( $FileNameZip ); if ( $res === TRUE ) { $zip -> extractTo( $path ); $zip -> close(); } $config = ""; file_put_contents( 'config.php', $config ); unlink( $FileNameSQL ); unlink( $FileNameZip ); unlink( 'restore.php' ); header( "location:/index.php?action=htaccess" ); exit; } ?>