I have this code I have made to check after new release, when there is a new one it ask you for install it, and then installs it if you accept.
The file is a zip file with the new folders and files inside there be unzip and pull into the system.
Can it be improved?
Update.php
<h1>DYNAMIC UPDATE SYSTEM</h1>
<?php
$localVersion = file_get_contents('current-release-versions.php');
ini_set('max_execution_time',60);
//Check For An Update
$getVersions = file_get_contents('http://yousite.com/UPDATE-PACKAGES/current-release-versions.php') or die ('ERROR');
$found = false;
if ($getVersions != '')
{
echo "<p>CURRENT VERSION: {$localVersion}</p>";
echo '<p>Reading Current Releases List</p>';
$versionList = explode("\n", $getVersions);
foreach ($versionList as $aV) {
if ($aV > $localVersion) {
echo '<p>New Update Found: v'.$aV.'</p>';
$found = true;
if(isset($_GET['doUpdate']) && !empty($_GET['doUpdate'])){
if($_GET['doUpdate'] == true) {
// capture the start time
$start_time = microtime(true);
// Download The File If We Do Not Have It
if (!is_file('UPDATES/MMD-CMS-'.$aV.'.zip')) {
echo '<p>Downloading New Update</p>';
$newUpdate = file_get_contents("http://yousite.com/UPDATE-PACKAGES/MMD-CMS-{$aV}.zip");
if (!is_dir('UPDATES/' )) {
mkdir ('UPDATES/');
}
$dlHandler = fopen('UPDATES/MMD-CMS-'.$aV.'.zip', 'w');
if ( !fwrite($dlHandler, $newUpdate)) {
echo '<p>Could not save new update. Operation aborted.</p>'; exit();
}
fclose($dlHandler);
echo '<p>Update Downloaded And Saved</p>';
}
else {
echo '<p>Update already downloaded.</p>';
}
function deleteDirectory($dir) {
if (!file_exists($dir)) return true;
if (!is_dir($dir)) return unlink($dir);
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') continue;
if (!deleteDirectory($dir.DIRECTORY_SEPARATOR.$item)) return false;
}
return rmdir($dir);
}
// Open The File And Do Stuff
$zipHandle = zip_open('UPDATES/MMD-CMS-'.$aV.'.zip');
echo '<ul>';
while ($aF = zip_read($zipHandle) ) {
$thisFileName = zip_entry_name($aF);
$thisFileDir = dirname($thisFileName);
//Continue if its not a file
if (substr($thisFileName, -1, 1) == '/') {
continue;
}
//Make the directory if we need to...
if (!is_dir($thisFileDir))
{
mkdir ($thisFileDir, 0777, true);
echo '<li>Created Directory '.$thisFileDir.'</li>';
}
deleteDirectory(".DS_Store");
//Overwrite the file
if ( !is_dir($thisFileName) ) {
$fileName = str_replace($thisFileDir."/", "", $thisFileName);
echo '<li>'.$fileName.'...........';
$contents = zip_entry_read($aF, zip_entry_filesize($aF));
$contents = str_replace("\r\n", "\n", $contents);
$updateThis = '';
//If we need to run commands, then do it.
if ( $thisFileName == 'upgrade.php' )
{
$upgradeExec = fopen ('upgrade.php','w');
fwrite($upgradeExec, $contents);
fclose($upgradeExec);
include ('upgrade.php');
unlink('upgrade.php');
echo' EXECUTED</li>';
}
else
{
$updateThis = fopen($thisFileName, 'w');
fwrite($updateThis, $contents);
fclose($updateThis);
unset($contents);
echo' UPDATED</li>';
}
}
}
unlink('UPDATES/MMD-CMS-'.$aV.'.zip');
deleteDirectory("__MACOSX");
deleteDirectory(".DS_Store");
echo "<li>
Deleted MMD-CMS-{$aV}.zip
</li>";
echo '</ul>';
// display how long the script took
echo "execution took: ".
(microtime(true) - $start_time).
" seconds.";
$updated = TRUE;
}
else {
echo '<p>Update ready. <a href="?doUpdate=true">» Install Now?</a></p>';
}
}else{
echo '<p>Update ready. <a href="?doUpdate=true">» Install Now?</a></p>';
}
}
}
if (isset($updated) && $updated == true)
{
echo '<p class="success">» CMS Updated to v'.$aV.'</p>';
}
else if ($found != true) echo '<p>» No update is available.</p>';
}
else echo '<p>Could not find latest realeases.</p>';
current-release-versions.php
1.00
1.02