maandag 16 maart 2015

Wordpress installer PHP script

Example PHP script for installing wordpress in the root of your domain.
This exact script is used in my new upcoming android app Wordpress Installer.
You can save it in a file, upload it to your host and run it in your browser.
It will download Wordpress, unzip the wordpress archive, launch the wordpress first-run script, afterwards this script will delete itself.
the $wpextr variable can be changed to install into a subfolder. Use a leading and a trailing slash.


<?php

// www.bartbosma.eu
// 2015
@ob_start();
function rrmdir($dir) {
   if (is_dir($dir)) {
     $objects = scandir($dir);
     foreach ($objects as $object) {
       if ($object != "." && $object != "..") {
         if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
       }
     }
     reset($objects);
     rmdir($dir);
   }
}
$wpextr="/wp/";
$wpextr="/";
$url = "https://wordpress.org/latest.zip";
$destination = "wordpress_latest.zip";   
if (!file_exists($destination)){
   
    if (function_exists("curl_init")){   
    $fp = fopen ($destination, 'w+');
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
    curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch, CURLOPT_AUTOREFERER, true );

    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 100 );
    curl_setopt( $ch, CURLOPT_FILE, $fp );
    curl_exec( $ch );
    curl_close( $ch );
  fclose( $fp );
}else{
    if (!file_put_contents($destination, fopen($url, "r"))){
        $succes=false;
        die("Cant download file..");   
    }
   
}
if (filesize($destination) > 0) {$succes= true;echo "Archive download succes.<br />";}else{$succes=false;}


}else{
    echo "Archive already exists, no download required<br />";
$succes=true;}   


if ($succes==true)
{
$zip = new ZipArchive;
if ($zip->open($destination) === TRUE) {
    #echo $_SERVER['DOCUMENT_ROOT'].$wpextr;
    $zip->extractTo($_SERVER['DOCUMENT_ROOT'].$wpextr);
    $zip->close();
    echo 'Archive extracted succes';
    sleep(1);

if ($handle = opendir($_SERVER['DOCUMENT_ROOT'].$wpextr.'wordpress')) {
    while (false !== ($fileName = readdir($handle))) {
        if ($fileName!==".." ||$fileName!==".")     @rename($_SERVER['DOCUMENT_ROOT'].$wpextr."wordpress/".$fileName, $_SERVER['DOCUMENT_ROOT'].$wpextr.$fileName);
    }
    closedir($handle);
   

@unlink("wpinst.php");
sleep(1);
rrmdir($_SERVER['DOCUMENT_ROOT'].$wpextr.'wordpress');
header("Location: http://".$_SERVER['HTTP_HOST'].$wpextr."/wp-admin/install.php");

}
   
   
} else {
    echo 'ExtractTo function: Opening the wordpress zip file failed. Your webhost seems ancient.';
}   
   
}
// www.bartbosma.eu xD
?>

Geen opmerkingen:

Een reactie plaatsen