Last active
May 14, 2018 20:03
-
-
Save RealityRipple/a0e695ec6d0d074cc3785f46f9f6bea8 to your computer and use it in GitHub Desktop.
PHP Script for updating MantisBT. Designed for use in a Cron job.
This file contains 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 | |
ini_set('display_errors',1); | |
ignore_user_abort(true); | |
error_reporting(E_ALL); | |
header('Content-Type: text/html; charset=utf-8'); | |
header('Content-Encoding: none;'); | |
$cron = $argv[1]; | |
if ($cron != 'cron') | |
return('This needs to be run from CRON.'); | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
* * | |
* mantisUpdate.php * | |
* * | |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
* * | |
* * v1.1 June 6, 2017 * | |
* Andrew Sachen <webmaster@RealityRipple.com> * | |
* * | |
* +Added serverType variable * | |
* #Skipped readme and config sample files * | |
* * | |
* * v1.0 May 23, 2017 * | |
* Andrew Sachen <webmaster@RealityRipple.com> * | |
* * | |
* /Conceived and written * | |
* * | |
* * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
/* | |
* siteURL | |
* ======= | |
* Address of your MantisBT installation. | |
* This value is used in the link to /admin/install.php | |
* which is used in the message returned by this script. | |
* This is the only path without a trailing slash! | |
*/ | |
$siteURL = 'http://bugs.example.com'; | |
/* | |
* htDir | |
* ===== | |
* Root web directory. Not used in script. | |
* This is just useful for all the other directory values. | |
*/ | |
$htDir = '/var/www'; | |
/* | |
* saveDir1 | |
* ======== | |
* MantisBT directory. Active installation location. | |
*/ | |
$saveDir1 = $htDir.'bugs/'; | |
/* | |
* saveDir2 | |
* ======== | |
* MantisBT data directory. Active installation location. | |
* This location will house the "core", "lang", "library", | |
* and "api" directories. If this is not the same as | |
* saveDir1, then a "core" directory will be created in | |
* saveDir1, and the constant_inc.php file from saveDir2 | |
* will be copiedto saveDir1's "core" directory. | |
*/ | |
$saveDir2 = '/var/.bugdata/'; | |
/* | |
* tempDir | |
* ======= | |
* Temporary storage directory. | |
* The MantisBT zip file will be downloaded and extracted | |
* to this directory. Make sure no file called "bugs.zip" | |
* is ever created in this directory. It's unlikely a | |
* directory with the same name as the latest MantisBT | |
* version would exist, but to be on the safe side, don't | |
* let any directories that start with "mantisbt-" exist | |
* in this directory either. | |
*/ | |
$tempDir = '/usr/tmp/mantis/'; | |
/* | |
* tempDir1 | |
* ======== | |
* Temporary storage directory for MantisBT installation. | |
* This directory will be completely deleted, so don't | |
* use an existing directory under any circumstances. | |
*/ | |
$tempDir1 = $tempDir.'bugs/'; | |
/* | |
* tempDir2 | |
* ======== | |
* Temporary storage directory for MantisBT data. | |
* This directory will be completely deleted, so don't | |
* use an existing directory under any circumstances. | |
* This must be the same as tempDir1 if saveDir2 is also | |
* the same as saveDir1, and must be different from | |
* tempDir1 if saveDir2 is different from saveDir1. | |
*/ | |
$tempDir2 = $tempDir.'.bugdata/'; | |
/* | |
* specialFiles[] | |
* ============== | |
* Array of files which should be kept. | |
* Do not remove the config/config_inc.php file or you | |
* will lose all your configuration data in the upgrade. | |
*/ | |
$specialFiles = array(); | |
$specialFiles[] = 'config/config_inc.php'; | |
$specialFiles[] = 'images/favicon.ico'; | |
$specialFiles[] = 'images/mantis_logo.gif'; | |
$specialFiles[] = 'images/mantis_logo.png'; | |
$specialFiles[] = 'images/mantis_logo_notext.png'; | |
//Add below this line for safety. | |
/* | |
* specialDirs[] | |
* ============= | |
* Array of directories which should be kept. | |
* This is particularly useful for plugins. No directory | |
* needs to be kept through upgrades by default. | |
*/ | |
$specialDirs = array(); | |
/* | |
* verbose | |
* ======= | |
* Print out every stage of the operation. | |
* Errors will always be displayed. | |
*/ | |
$verbose = true; | |
/* | |
* serverType | |
* ========== | |
* Choose the type of webserver MantisBT is hosted on. | |
* This script will remove files that are not necessary | |
* for this type of server. | |
* | |
* > "Apache" deletes "Web.config" files | |
* | |
* > "ASP.NET" deletes ".htaccess" files | |
* | |
* > Any other value deletes both types of files. | |
* You will need to configure your folders another way. | |
*/ | |
$serverType = 'Apache'; | |
//$serverType = 'ASP.NET'; | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
function compareVersions($sVerA, $sVerB) | |
{ | |
$aV = explode('.', $sVerA); | |
$bV = explode('.', $sVerB); | |
if (count($aV) < count($bV)) | |
{ | |
for ($i = count($aV); $i < count($bV); $i++) | |
$aV[$i] = '0'; | |
} | |
else if (count($aV) > count($bV)) | |
{ | |
for ($i = count($bV); $i < count($aV); $i++) | |
$bV[$i] = '0'; | |
} | |
$segments = count($aV); | |
for ($i = 0; $i < $segments; $i++) | |
{ | |
if (intval($aV[$i]) > intval($bV[$i])) | |
return 1; | |
if ($i == $segments - 1) | |
{ | |
if (intval($aV[$i]) == intval($bV[$i])) | |
return 0; | |
} | |
else if (intval($aV[$i]) < intval($bV[$i])) | |
break; | |
} | |
return -1; | |
} | |
function copyDir($src, $dest) | |
{ | |
$dir = opendir($src); | |
@mkdir($dest); | |
while(($file = readdir($dir)) !== false) | |
{ | |
if (($file == '.') || ($file == '..')) | |
continue; | |
$fPath = "$src/$file"; | |
$fDest = "$dest/$file"; | |
if (is_dir($fPath)) | |
copyDir($fPath, $fDest); | |
else | |
copy($fPath, $fDest); | |
} | |
closedir($dir); | |
} | |
function deleteDir($src) | |
{ | |
$dir = opendir($src); | |
while(($file = readdir($dir)) !== false) | |
{ | |
if (($file == '.') || ($file == '..')) | |
continue; | |
$fPath = "$src/$file"; | |
if (is_dir($fPath)) | |
deleteDir($fPath); | |
else | |
unlink($fPath); | |
} | |
closedir($dir); | |
rmdir($src); | |
} | |
ob_start(); | |
set_time_limit(2000); | |
$time_start = microtime(true); | |
$unique = true; | |
if ($tempDir1 == $tempDir2 && $saveDir1 == $saveDir2) | |
$unique = false; | |
else if ($tempDir1 != $tempDir2 && $saveDir1 != $saveDir2) | |
$unique = true; | |
else if ($tempDir1 != $tempDir2) | |
die('tempDir1 and tempDir2 must be the same because the saveDirs are the same.'); | |
else | |
die('tempDir1 and tempDir2 must be different because the saveDirs are different.'); | |
@mkdir($tempdir); | |
if (file_exists($tempDir.'bugs.zip')) | |
unlink($tempDir.'bugs.zip'); | |
if (file_exists($tempDir1)) | |
deleteDir($tempDir1); | |
if ($unique) | |
{ | |
if (file_exists($tempDir2)) | |
deleteDir($tempDir2); | |
} | |
$myVer = '0.0.0'; | |
if (file_exists($saveDir1.'core/constant_inc.php')) | |
{ | |
$verFile = file_get_contents($saveDir1.'core/constant_inc.php'); | |
if (strpos($verFile, 'MANTIS_VERSION') !== false) | |
{ | |
$verFile = substr($verFile, strpos($verFile, 'MANTIS_VERSION') + 14); | |
$verFile = substr($verFile, strpos($verFile, ' \'') + 2); | |
$myVer = substr($verFile, 0, strpos($verFile, '\'')); | |
} | |
} | |
$feedFile = simplexml_load_file('https://sourceforge.net/projects/mantisbt/rss?path=/mantis-stable'); | |
$feedJSON = json_encode($feedFile); | |
$feed = json_decode($feedJSON, true); | |
$dList = $feed['channel']['item']; | |
$betterV = array(); | |
foreach ($dList as $dEl) | |
{ | |
$dElLink = $dEl['link']; | |
if (strpos($dElLink, 'mantis-stable') === false) | |
continue; | |
if (strpos($dElLink, '.zip/download') === false) | |
continue; | |
$dElVer = substr($dElLink, strpos($dElLink, 'mantis-stable') + 14); | |
$dElVer = substr($dElVer, 0, strpos($dElVer, '/')); | |
if (compareVersions($myVer, $dElVer) > -1) | |
continue; | |
$bEl = array(); | |
$bEl['ver'] = $dElVer; | |
$bEl['url'] = $dElLink; | |
$betterV[] = $bEl; | |
} | |
if (count($betterV) == 0) | |
die(); | |
$getFile = ''; | |
$bestVer = $myVer; | |
if (count($betterV) == 1) | |
{ | |
$bestVer = $betterV[0]['ver']; | |
$getFile = $betterV[0]['url']; | |
} | |
else | |
{ | |
foreach ($betterV as $betterEl) | |
{ | |
if (compareVersions($bestVer, $betterEl['ver']) > -1) | |
continue; | |
$bestVer = $betterEl['ver']; | |
$getFile = $betterEl['url']; | |
} | |
} | |
if ($getFile == '') | |
die('Unable to determine URL for new MantisBT Version!'); | |
if ($verbose) | |
echo "Downloading new MantisBT Version..."; ob_flush(); flush(); | |
$gh = fopen($getFile, 'r'); | |
file_put_contents($tempDir.'bugs.zip', $gh); | |
fclose($gh); | |
if ($verbose) | |
echo(" Complete!\n"); ob_flush(); flush(); | |
$z = new ZipArchive; | |
if ($z->open($tempDir.'bugs.zip') !== true) | |
die('Failed to open MantisBT ZIP'); | |
copy ($saveDir1.'mantis_offline.php.sample', $saveDir1.'mantis_offline.php'); | |
mkdir($tempDir1); | |
if ($unique) | |
mkdir($tempDir2); | |
if ($verbose) | |
echo("Extracting Files..."); ob_flush(); flush(); | |
$zipRoot = ''; | |
for ($i=0; $i < $z->numFiles; $i++) | |
{ | |
$zippedName = $z->getNameIndex($i); | |
$zippedDir = substr($zippedName, 0, strrpos($zippedName, '/') + 1); | |
if (substr($zippedDir, 0, 9) == 'mantisbt-') | |
{ | |
if ($zipRoot == '') | |
$zipRoot = substr($zippedDir, 0, strpos($zippedDir, '/')); | |
$zippedDir = substr($zippedDir, strpos($zippedDir, '/') + 1); | |
} | |
$zippedFName = substr($zippedName, strrpos($zippedName, '/') + 1); | |
if (strpos(strtolower($zippedName), 'core/constant_inc.php') !== false) | |
{ | |
$z->extractTo($tempDir, $zippedName); | |
if (!file_exists($tempDir1.$zippedDir)) | |
mkdir($tempDir1.$zippedDir); | |
rename($tempDir.$zippedName, $tempDir1.$zippedDir.$zippedFName); | |
if ($unique) | |
{ | |
if (!file_exists($tempDir2.$zippedDir)) | |
mkdir($tempDir2.$zippedDir); | |
copy($tempDir1.$zippedDir.$zippedFName, $tempDir2.$zippedDir.$zippedFName); | |
} | |
} | |
else if (strpos(strtolower($zippedName), '/readme.md') !== false) | |
{ | |
// skip the readme file | |
} | |
else if (strpos(strtolower($zippedName), '/config/config_inc.php.sample') !== false) | |
{ | |
// skip the sample config | |
} | |
else if (strpos(strtolower($zippedName), '.htaccess') !== false && strtolower($serverType) != 'apache') | |
{ | |
// skip .htaccess files if not running Apache | |
} | |
else if (strpos(strtolower($zippedName), 'web.config') !== false && strtolower($serverType) != 'asp.net') | |
{ | |
// skip Web.config files if not running ASP.NET | |
} | |
else if (substr(strtolower($zippedDir), 0, 5) == 'core/' || substr(strtolower($zippedDir), 0, 5) == 'lang/' || substr(strtolower($zippedDir), 0, 8) == 'library/' || substr(strtolower($zippedDir), 0, 4) == 'api/') | |
{ | |
$z->extractTo($tempDir, $zippedName); | |
if (!file_exists($tempDir2.$zippedDir)) | |
mkdir($tempDir2.$zippedDir); | |
rename($tempDir.$zippedName, $tempDir2.$zippedDir.$zippedFName); | |
} | |
else if (substr(strtolower($zippedDir), 0, 4) == 'doc/') | |
{ | |
// skip the doc folder | |
} | |
else | |
{ | |
$z->extractTo($tempDir, $zippedName); | |
if (!file_exists($tempDir1.$zippedDir)) | |
mkdir($tempDir1.$zippedDir); | |
rename($tempDir.$zippedName, $tempDir1.$zippedDir.$zippedFName); | |
} | |
} | |
$z->close(); | |
unlink($tempDir.'bugs.zip'); | |
if ($zipRoot != '') | |
deleteDir($tempDir.$zipRoot); | |
if ($verbose) | |
echo("Complete!\n"); ob_flush(); flush(); | |
if ($verbose) | |
echo("Upgrading MantisBT Files..."); ob_flush(); flush(); | |
copy ($tempDir1.'mantis_offline.php.sample', $tempDir1.'mantis_offline.php'); | |
if (isset($specialDirs) && count($specialDirs) > 0) | |
{ | |
foreach ($specialDirs as $specialDir) | |
{ | |
copyDir($saveDir1.$specialDir, $tempDir1.$specialDir); | |
} | |
} | |
if (isset($specialFiles) && count($specialFiles) > 0) | |
{ | |
foreach ($specialFiles as $specialFile) | |
{ | |
copy($saveDir1.$specialFile, $tempDir1.$specialFile); | |
} | |
} | |
deleteDir($saveDir1); | |
if ($unique) | |
deleteDir($saveDir2); | |
copyDir($tempDir1, $saveDir1); | |
if ($unique) | |
copyDir($tempDir2, $saveDir2); | |
deleteDir($tempDir1); | |
if ($unique) | |
deleteDir($tempDir2); | |
if ($verbose) | |
echo(" Complete!\n"); ob_flush(); flush(); | |
$time_end = microtime(true); | |
if ($verbose) | |
echo("Completed in ".($time_end - $time_start)." seconds.\n"); | |
echo("Your MantisBT installation has been upgraded from v$myVer to v$bestVer.\n"); | |
echo("Please visit <$siteURL/admin/install.php> to complete the installation.\n"); | |
echo("If you would like to see the changes in this version, please visit <https://www.mantisbt.org/bugs/changelog_page.php?project=mantisbt&version=$bestVer>."); | |
ob_end_flush(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment