update-functions.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. // Upgrade the installation
  3. function upgradeInstall($branch = 'v2-master', $stage) {
  4. $url = 'https://github.com/causefx/Organizr/archive/'.$branch.'.zip';
  5. $file = "upgrade.zip";
  6. $source = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'upgrade'.DIRECTORY_SEPARATOR.'Organizr-'.str_replace('v2','2',$branch).DIRECTORY_SEPARATOR;
  7. $cleanup = dirname(__DIR__,2) .DIRECTORY_SEPARATOR."upgrade".DIRECTORY_SEPARATOR;
  8. $destination = dirname(__DIR__,2).DIRECTORY_SEPARATOR;
  9. switch ($stage) {
  10. case '1':
  11. return 'stage1';
  12. writeLog('success', 'Update Function - Started Upgrade Process', $GLOBALS['organizrUser']['username']);
  13. if(downloadFile($url, $file)){
  14. writeLog('success', 'Update Function - Downloaded Update File for Branch: '.$branch, $GLOBALS['organizrUser']['username']);
  15. return true;
  16. }else{
  17. writeLog('error', 'Update Function - Downloaded Update File Failed for Branch: '.$branch, $GLOBALS['organizrUser']['username']);
  18. return false;
  19. }
  20. break;
  21. case '2':
  22. return 'stage2';
  23. if(unzipFile($file)){
  24. writeLog('success', 'Update Function - Unzipped Update File for Branch: '.$branch, $GLOBALS['organizrUser']['username']);
  25. return true;
  26. }else{
  27. writeLog('error', 'Update Function - Unzip Failed for Branch: '.$branch, $GLOBALS['organizrUser']['username']);
  28. return false;
  29. }
  30. break;
  31. case '3':
  32. return 'stage3';
  33. if(rcopy($source, $destination)){
  34. writeLog('success', 'Update Function - Overwrited Files using Updated Files from Branch: '.$branch, $GLOBALS['organizrUser']['username']);
  35. return true;
  36. }else{
  37. writeLog('error', 'Update Function - Overwrite Failed for Branch: '.$branch, $GLOBALS['organizrUser']['username']);
  38. return false;
  39. }
  40. break;
  41. case '4':
  42. return 'stage4';
  43. if(rrmdir($cleanup)){
  44. writeLog('success', 'Update Function - Deleted Update Files from Branch: '.$branch, $GLOBALS['organizrUser']['username']);
  45. writeLog('success', 'Update Function - Update Completed', $GLOBALS['organizrUser']['username']);
  46. return true;
  47. }else{
  48. writeLog('error', 'Update Function - Removal of Update Files Failed for Branch: '.$branch, $GLOBALS['organizrUser']['username']);
  49. return false;
  50. }
  51. break;
  52. default:
  53. # code...
  54. break;
  55. }
  56. return false;
  57. }
  58. function downloadFile($url, $path){
  59. ini_set('max_execution_time',0);
  60. $folderPath = "upgrade".DIRECTORY_SEPARATOR;
  61. if(!mkdir($folderPath)){
  62. //writeLog("error", "organizr could not create upgrade folder");
  63. }
  64. $newfname = $folderPath . $path;
  65. $file = fopen ($url, 'rb');
  66. if ($file) {
  67. $newf = fopen ($newfname, 'wb');
  68. if ($newf) {
  69. while(!feof($file)) {
  70. fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
  71. }
  72. }
  73. }else{
  74. //writeLog("error", "organizr could not download $url");
  75. }
  76. if ($file) {
  77. fclose($file);
  78. //writeLog("success", "organizr finished downloading the github zip file");
  79. }else{
  80. //writeLog("error", "organizr could not download the github zip file");
  81. }
  82. if ($newf) {
  83. fclose($newf);
  84. //writeLog("success", "organizr created upgrade zip file from github zip file");
  85. }else{
  86. //writeLog("error", "organizr could not create upgrade zip file from github zip file");
  87. }
  88. return true;
  89. }
  90. function unzipFile($zipFile){
  91. $zip = new ZipArchive;
  92. $extractPath = "upgrade/";
  93. if($zip->open($extractPath . $zipFile) != "true"){
  94. //writeLog("error", "organizr could not unzip upgrade.zip");
  95. }else{
  96. //writeLog("success", "organizr unzipped upgrade.zip");
  97. }
  98. /* Extract Zip File */
  99. $zip->extractTo($extractPath);
  100. $zip->close();
  101. return true;
  102. }
  103. // Function to remove folders and files
  104. function rrmdir($dir) {
  105. if (is_dir($dir)) {
  106. $files = scandir($dir);
  107. foreach ($files as $file)
  108. if ($file != "." && $file != "..") rrmdir("$dir/$file");
  109. rmdir($dir);
  110. }
  111. else if (file_exists($dir)) unlink($dir);
  112. return true;
  113. }
  114. // Function to Copy folders and files
  115. function rcopy($src, $dst) {
  116. if (is_dir ( $src )) {
  117. if (!file_exists($dst)) : mkdir ( $dst ); endif;
  118. $files = scandir ( $src );
  119. foreach ( $files as $file )
  120. if ($file != "." && $file != "..")
  121. rcopy ( "$src/$file", "$dst/$file" );
  122. } else if (file_exists ( $src ))
  123. copy ( $src, $dst );
  124. return true;
  125. }