Skip to content

Instantly share code, notes, and snippets.

@adampatterson
Created August 31, 2012 16:57
Show Gist options
  • Save adampatterson/3555820 to your computer and use it in GitHub Desktop.
Save adampatterson/3555820 to your computer and use it in GitHub Desktop.
PHP Upgrade Script
<?
define('APP_PATH', __DIR__.'/');
function delete_dir($dir) {
if (substr($dir, strlen($dir)-1, 1) != '/')
$dir .= '/';
if ($handle = opendir($dir))
{
while ($obj = readdir($handle))
{
if ($obj != '.' && $obj != '..')
{
if (is_dir($dir.$obj))
{
if (!delete_dir($dir.$obj))
return false;
}
elseif (is_file($dir.$obj))
{
if (!unlink($dir.$obj))
return false;
}
}
}
closedir($handle);
if (!@rmdir($dir))
return false;
return true;
}
return false;
}
function recursive_glob($pattern='*', $flags = 0, $path='')
{
$paths = glob($path.'*', GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT);
$files = glob($path.$pattern, $flags);
foreach ($paths as $path) {
$files=array_merge($files,recursive_glob($pattern, $flags, $path));
}
return $files;
}
function string_to_parts($file) {
$file_parts = explode('/', $file);
$file_name = end($file_parts);
$path_parts = array();
foreach ($file_parts as $key => $value) {
if ($file_name != $value) {
$path_parts[] = $value;
}
}
$file_path = '';
foreach ($path_parts as $part) {
$file_path .= $part.'/';
}
$file_clean['path'] = $file_path;
$file_clean['name'] = $file_name;
$file_clean['full'] = $file_path.$file_name;
return $file_clean;
}
function array_clean($source)
{
foreach ($source as $key => $val) {
if ($val == '') {
unset($source[$key]);
}
}
return $source;
}
// Path from Serpent API
$client = curl_init('http://localhost/_lab/upgrade/source/update.zip');
curl_setopt($client, CURLOPT_RETURNTRANSFER, 1); //fixed this line
$filedata = curl_exec($client);
file_put_contents(APP_PATH.'update.zip', $filedata);
if (file_exists(APP_PATH.'update.zip')) {
echo '<p>File downloaded!</p>';
}
$zip = new ZipArchive;
if ($zip->open(APP_PATH.'update.zip') === TRUE) {
$zip->extractTo(APP_PATH);
$zip->close();
chdir(APP_PATH.'update');
$update_files = recursive_glob('*.*');
echo '<ul class="list">';
foreach ($update_files as $file) {
$parts = string_to_parts($file);
$update_file_path = APP_PATH.'update/'.$parts['full'];
$site_folder_path = APP_PATH.'site/'.$parts['path'];
$site_file_path = APP_PATH.'site/'.$parts['full'];
if (!is_dir($site_folder_path)) {
echo "<li><strong>Creating folder:</strong> ".$parts['path'].'</li>';
mkdir( $site_folder_path );
}
if (file_exists($site_file_path)) {
echo "<li><strong>Updated:</strong> ".$parts['full']."</li>";
} else {
echo "<li><strong>Added:</strong> ".$parts['full']."</li>";
}
$file = file_get_contents( $update_file_path );
$fp = fopen( $site_file_path, 'w' );
fwrite($fp, $file);
}
echo '</ul>';
# Clean up!
delete_dir(APP_PATH.'update');
delete_dir(APP_PATH.'__MACOSX');
unlink(APP_PATH.'update.zip');
echo '<p>Tentacle CMS was updated successfully!</p>';
} else {
echo 'Something Went Wrong!';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment