update-functions.php 4.3 KB

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