| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- // Upgrade the installation
- function upgradeInstall($branch = 'v2-master', $stage)
- {
- ini_set('max_execution_time', 0);
- set_time_limit(0);
- $url = 'https://github.com/causefx/Organizr/archive/' . $branch . '.zip';
- $file = "upgrade.zip";
- $source = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'upgrade' . DIRECTORY_SEPARATOR . 'Organizr-' . str_replace('v2', '2', $branch) . DIRECTORY_SEPARATOR;
- $cleanup = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . "upgrade" . DIRECTORY_SEPARATOR;
- $destination = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR;
- switch ($stage) {
- case '1':
- writeLog('success', 'Update Function - Started Upgrade Process', $GLOBALS['organizrUser']['username']);
- if (downloadFile($url, $file)) {
- writeLog('success', 'Update Function - Downloaded Update File for Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
- return true;
- } else {
- writeLog('error', 'Update Function - Downloaded Update File Failed for Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
- return false;
- }
- break;
- case '2':
- if (unzipFile($file)) {
- writeLog('success', 'Update Function - Unzipped Update File for Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
- return true;
- } else {
- writeLog('error', 'Update Function - Unzip Failed for Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
- return false;
- }
- break;
- case '3':
- if (rcopy($source, $destination)) {
- writeLog('success', 'Update Function - Overwrited Files using Updated Files from Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
- return true;
- } else {
- writeLog('error', 'Update Function - Overwrite Failed for Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
- return false;
- }
- break;
- case '4':
- if (rrmdir($cleanup)) {
- writeLog('success', 'Update Function - Deleted Update Files from Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
- writeLog('success', 'Update Function - Update Completed', $GLOBALS['organizrUser']['username']);
- return true;
- } else {
- writeLog('error', 'Update Function - Removal of Update Files Failed for Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
- return false;
- }
- break;
- default:
- return false;
- break;
- }
- return false;
- }
- function downloadFile($url, $path)
- {
- ini_set('max_execution_time', 0);
- set_time_limit(0);
- $folderPath = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . "upgrade" . DIRECTORY_SEPARATOR;
- if (!mkdir($folderPath)) {
- //writeLog("error", "organizr could not create upgrade folder");
- }
- $newfname = $folderPath . $path;
- $file = fopen($url, 'rb');
- if ($file) {
- $newf = fopen($newfname, 'wb');
- if ($newf) {
- while (!feof($file)) {
- fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
- }
- }
- } else {
- //writeLog("error", "organizr could not download $url");
- }
- if ($file) {
- fclose($file);
- //writeLog("success", "organizr finished downloading the github zip file");
- } else {
- //writeLog("error", "organizr could not download the github zip file");
- }
- if ($newf) {
- fclose($newf);
- //writeLog("success", "organizr created upgrade zip file from github zip file");
- } else {
- //writeLog("error", "organizr could not create upgrade zip file from github zip file");
- }
- return true;
- }
- function downloadFileToPath($from, $to, $path)
- {
- ini_set('max_execution_time', 0);
- set_time_limit(0);
- if (@!mkdir($path, 0777, true)) {
- //writeLog("error", "organizr could not create upgrade folder");
- }
- $file = fopen($from, 'rb');
- if ($file) {
- $newf = fopen($to, 'wb');
- if ($newf) {
- while (!feof($file)) {
- fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
- }
- }
- } else {
- //writeLog("error", "organizr could not download $url");
- }
- if ($file) {
- fclose($file);
- //writeLog("success", "organizr finished downloading the github zip file");
- } else {
- //writeLog("error", "organizr could not download the github zip file");
- }
- if ($newf) {
- fclose($newf);
- //writeLog("success", "organizr created upgrade zip file from github zip file");
- } else {
- //writeLog("error", "organizr could not create upgrade zip file from github zip file");
- }
- return true;
- }
- function unzipFile($zipFile)
- {
- ini_set('max_execution_time', 0);
- set_time_limit(0);
- $zip = new ZipArchive;
- $extractPath = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . "upgrade/";
- if ($zip->open($extractPath . $zipFile) != "true") {
- //writeLog("error", "organizr could not unzip upgrade.zip");
- } else {
- //writeLog("success", "organizr unzipped upgrade.zip");
- }
- /* Extract Zip File */
- $zip->extractTo($extractPath);
- $zip->close();
- return true;
- }
- // Function to remove folders and files
- function rrmdir($dir)
- {
- ini_set('max_execution_time', 0);
- set_time_limit(0);
- if (is_dir($dir)) {
- $files = scandir($dir);
- foreach ($files as $file) {
- if ($file != "." && $file != "..") {
- rrmdir("$dir/$file");
- }
- }
- rmdir($dir);
- } elseif (file_exists($dir)) {
- unlink($dir);
- }
- return true;
- }
- // Function to Copy folders and files
- function rcopy($src, $dst)
- {
- ini_set('max_execution_time', 0);
- set_time_limit(0);
- if (is_dir($src)) {
- if (!file_exists($dst)) : mkdir($dst);
- endif;
- $files = scandir($src);
- foreach ($files as $file) {
- if ($file != "." && $file != "..") {
- rcopy("$src/$file", "$dst/$file");
- }
- }
- } elseif (file_exists($src)) {
- copy($src, $dst);
- }
- return true;
- }
|