Last active
September 16, 2021 13:44
-
-
Save xewl/f420a90e46eeeb314d4c9ca325188012 to your computer and use it in GitHub Desktop.
WP Breakout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WP Breakout | |
* Version: 0.0.1 | |
* Description: Do not activate. Click "Visit plugin site". | |
* Plugin URI: /wp-content/plugins/breakout/breakout.php | |
* Update URI: # | |
* | |
* Put me in /wp-content/plugins/breakout/breakout.php | |
* or upload me in a "breakout.zip" file. | |
*/ | |
@ini_set('max_execution_time', 1200); | |
@ini_set('memory_limit', '2048M'); | |
@ini_set('display_errors', 1); | |
@ini_set('display_startup_errors', 1); | |
error_reporting(E_ALL); | |
if (!defined('WPINC')) { | |
$zip_available = extension_loaded('zip'); | |
include('../../../wp-config.php'); | |
// actions: | |
if (!empty($_GET['archive'])) { | |
/* creates a compressed zip file */ | |
if ($zip_available) { | |
echo ABSPATH . ' archiving...'; | |
if (zipData(ABSPATH, __DIR__ . '/breakout.zip')) { | |
echo PHP_EOL . '<b>done</b> - <a href="?">back</a>'; | |
exit(); | |
} else { | |
echo PHP_EOL . '<b>failed</b> - <a href="?">back</a>'; | |
exit(); | |
} | |
} else { | |
echo '<div>zip not available</div>'; | |
} | |
} | |
if (!empty($_GET['database'])) { | |
/* creates a sql export file */ | |
// if ($zip_available) { | |
// echo ABSPATH . ' exporting database...'; | |
if (Export_Database(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, $tables = false, $backup_name = 'breakout_database.sql')) { | |
echo PHP_EOL . '<b>done</b> - <a href="?">back</a>'; | |
exit(); | |
} else { | |
echo PHP_EOL . '<b>failed</b> - <a href="?">back</a>'; | |
exit(); | |
} | |
} | |
if (!empty($_GET['adminer'])) { | |
/* downloads latest adminer */ | |
if ( file_put_contents('adminer.php', fopen('https://www.adminer.org/latest-mysql-en.php', 'r')) ) { | |
echo PHP_EOL . '<b>done</b> - <a href="?">back</a>'; | |
exit(); | |
} else { | |
echo PHP_EOL . '<b>failed</b> - <a href="?">back</a>'; | |
exit(); | |
} | |
} | |
// menu/info | |
echo '<pre>', 'PHP Version: ' , phpversion(), '</pre>'; | |
echo '<pre>', 'ZIP enabled?: ', ($zip_available ? 'Y' : 'N'), '</pre>'; | |
echo '<hr/>'; | |
echo '<div>DB credentials:<pre>'; | |
/** The name of the database for WordPress */ | |
echo DB_NAME . PHP_EOL; | |
/** MySQL database username */ | |
echo DB_USER . PHP_EOL; | |
/** MySQL database password */ | |
echo DB_PASSWORD . PHP_EOL; | |
/** MySQL hostname */ | |
echo DB_HOST . PHP_EOL; | |
/** Database Charset to use in creating database tables. */ | |
echo DB_CHARSET . PHP_EOL; | |
/** The Database Collate type. Don't change this if in doubt. */ | |
echo DB_COLLATE . PHP_EOL; | |
echo '</pre></div>'; | |
echo '<hr/>'; | |
echo '<ul>'; | |
if (file_exists(__DIR__ . '/adminer.php')) { | |
echo '<li><a href="adminer.php">open adminer</a></li>'; | |
} | |
echo '<li><a href="?adminer=true">download latest adminer.php</a></li>'; | |
echo '<li><a href="?database=true">database export</a></li>'; | |
if($zip_available) { | |
echo '<li>'; | |
if (file_exists(__DIR__ . '/breakout.zip')) { | |
echo '<a href="?archive=true">archive</a>'; | |
echo '<ul><li><a href="breakout.zip">download breakout.zip</a> (' . human_filesize(filesize(__DIR__ . '/breakout.zip')) . ')</li></ul>'; | |
} else { | |
if ($zip_available) { | |
echo '<a href="?archive=true">archive</a>'; | |
} else { | |
echo '<div> zip unavailable, sorry :(</div>'; | |
} | |
} | |
echo '</li>'; | |
} else { | |
echo '<li><span> zip unavailable, sorry :(</span></li>'; | |
} | |
echo '</ul>'; | |
} else { | |
class breakoutPlugin | |
{ | |
static function activate() | |
{ | |
die('Don\'t activate pls.'); | |
} | |
} | |
register_activation_hook(__FILE__, array('breakoutPlugin', 'activate')); | |
} | |
// helpers | |
function Export_Database($host, $user, $pass, $name, $tables = false, $backup_name = false) | |
{ | |
$mysqli = new mysqli($host, $user, $pass, $name); | |
$mysqli->select_db($name); | |
$mysqli->query("SET NAMES 'utf8'"); | |
$queryTables = $mysqli->query('SHOW TABLES'); | |
while ($row = $queryTables->fetch_row()) { | |
$target_tables[] = $row[0]; | |
} | |
if ($tables !== false) { | |
$target_tables = array_intersect($target_tables, $tables); | |
} | |
foreach ($target_tables as $table) { | |
$result = $mysqli->query('SELECT * FROM ' . $table); | |
$fields_amount = $result->field_count; | |
$rows_num = $mysqli->affected_rows; | |
$res = $mysqli->query('SHOW CREATE TABLE ' . $table); | |
$TableMLine = $res->fetch_row(); | |
$content = (!isset($content) ? '' : $content) . "\n\n" . $TableMLine[1] . ";\n\n"; | |
for ($i = 0, $st_counter = 0; $i < $fields_amount; $i++, $st_counter = 0) { | |
while ($row = $result->fetch_row()) { //when started (and every after 100 command cycle): | |
if ($st_counter % 100 == 0 || $st_counter == 0) { | |
$content .= "\nINSERT INTO " . $table . " VALUES"; | |
} | |
$content .= "\n("; | |
for ($j = 0; $j < $fields_amount; $j++) { | |
$row[$j] = str_replace("\n", "\\n", addslashes($row[$j])); | |
if (isset($row[$j])) { | |
$content .= '"' . $row[$j] . '"'; | |
} else { | |
$content .= '""'; | |
} | |
if ($j < ($fields_amount - 1)) { | |
$content .= ','; | |
} | |
} | |
$content .= ")"; | |
//every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler | |
if ((($st_counter + 1) % 100 == 0 && $st_counter != 0) || $st_counter + 1 == $rows_num) { | |
$content .= ";"; | |
} else { | |
$content .= ","; | |
} | |
$st_counter = $st_counter + 1; | |
} | |
} | |
$content .= "\n\n\n"; | |
} | |
//$backup_name = $backup_name ? $backup_name : $name."___(".date('H-i-s')."_".date('d-m-Y').")__rand".rand(1,11111111).".sql"; | |
$backup_name = $backup_name ? $backup_name : $name . ".sql"; | |
header('Content-Type: application/octet-stream'); | |
header("Content-Transfer-Encoding: Binary"); | |
header("Content-disposition: attachment; filename=\"" . $backup_name . "\""); | |
echo $content; | |
exit; | |
} | |
function zipData($source, $destination) | |
{ | |
if (extension_loaded('zip') === true) { | |
if (file_exists($source) === true) { | |
$zip = new ZipArchive(); | |
if (file_exists($source) === true) { | |
@unlink($destination); | |
} | |
if ($zip->open($destination, ZIPARCHIVE::CREATE) === true) { | |
$source = realpath($source); | |
if (is_dir($source) === true) { | |
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); | |
foreach ($files as $file) { | |
$file = realpath($file); | |
if (is_dir($file) === true) { | |
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); | |
} else if (is_file($file) === true && basename($file) !== 'breakout.zip') { | |
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); | |
} | |
} | |
} else if (is_file($source) === true && basename($source) !== 'breakout.zip') { | |
$zip->addFromString(basename($source), file_get_contents($source)); | |
} | |
} | |
return $zip->close(); | |
} | |
} | |
return false; | |
} | |
function human_filesize($bytes, $decimals = 2) | |
{ | |
$factor = floor((strlen($bytes) - 1) / 3); | |
if ($factor > 0) $sz = 'KMGT'; | |
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor - 1] . 'B'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment