Skip to content

Instantly share code, notes, and snippets.

@andykillen
Last active December 15, 2016 16:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andykillen/93e3dd5e775944401b7388b808129e59 to your computer and use it in GitHub Desktop.
Save andykillen/93e3dd5e775944401b7388b808129e59 to your computer and use it in GitHub Desktop.
Checks through a db looking for posts table and creates a text file with all the wget commands to retrieve the images of the site. To run just load the php file, enter the form fields and then after it has created the txt file run is 'bash wp_posts.txt' at the terminal.
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Search for all images on WordPress and download to directory via WGET task list</title>
</head>
<body>
<h1>Search for all images on WordPress and download to directory via WGET task list</h1>
<?php
$db = 'information_schema';
if(isset($_POST['database']) && !empty($_POST['destination']) ){
$db = $_POST['database'];
}
$database = array('mysql' => array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'db' => $db
));
$pdo = new PDO("mysql:host=".$database['mysql']['host'].";dbname=".$database['mysql']['db'].";charset=utf8"
, $database['mysql']['username'] , $database['mysql']['password']);
if(!isset($_POST['database']) || empty($_POST['destination']) || empty($_POST['domain']) || empty($_POST['upload']) ) {
$select = $pdo->prepare("select schema_name from information_schema.schemata");
$select->execute();
$result = $select->fetchAll();
?>
<form action="" method="POST">
<p><label>Select Database</label>
<select name="database">
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['schema_name'] ?>"><?php echo $row['schema_name'] ?></option>
<?php } ?>
</select></p>
<p><label>source domain</label>
<input type="text" name="domain" /><br />
<small>input your domain like example.com without any protocol or slashes</small>
</p>
<p><label>db table prefix</label>
<input type="text" name="prefix" value="wp_"/><br />
<small>like wp_ but the one you have</small>
</p>
<p><label>uploads directory</label>
<input type="text" name="upload" /><br />
<small>where the wp-content is. i.e. https://example.com/wp-content</small>
</p>
<p><label>destination directory (wp-content)</label>
<input type="text" name="destination" /><br />
<small>go to terminal and type 'pwd', copy the response in here</small>
</p>
<p>
<input type="submit" />
</p>
</form>
<?php } else {
$result = $pdo->query("SHOW TABLES FROM {$_POST['database']} WHERE
`Tables_in_{$_POST['database']}` LIKE '{$_POST['prefix']}%_posts'
OR `Tables_in_{$_POST['database']}` LIKE '{$_POST['prefix']}posts';");
$array_of_tables = $result->fetchAll();
foreach($array_of_tables as $table){
$t = $table[0];
$select = "SELECT guid FROM {$t} WHERE post_type='attachment' AND guid LIKE '%{$_POST['domain']}%' ";
$loopresult = $pdo->query($select);
if($loopresult!= false && $loopresult->rowCount() > 0) {
$output = array();
$values = $loopresult->fetchAll();
foreach ($values as $row){
$output[] = "wget ". $row[0]." -P " . str_replace($_POST['upload'], $_POST['destination'],dirname($row[0]) ) ;
}
file_put_contents($t.'.txt', implode("\n", $output) . "\n", FILE_APPEND);
}
}
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment