update-functions.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 = dirname(__DIR__,2).DIRECTORY_SEPARATOR."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. $folderPath = "upgrade".DIRECTORY_SEPARATOR;
  57. if(!mkdir($folderPath)){
  58. //writeLog("error", "organizr could not create upgrade folder");
  59. }
  60. $newfname = $folderPath . $path;
  61. $file = fopen ($url, 'rb');
  62. if ($file) {
  63. $newf = fopen ($newfname, 'wb');
  64. if ($newf) {
  65. while(!feof($file)) {
  66. fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
  67. }
  68. }
  69. }else{
  70. //writeLog("error", "organizr could not download $url");
  71. }
  72. if ($file) {
  73. fclose($file);
  74. //writeLog("success", "organizr finished downloading the github zip file");
  75. }else{
  76. //writeLog("error", "organizr could not download the github zip file");
  77. }
  78. if ($newf) {
  79. fclose($newf);
  80. //writeLog("success", "organizr created upgrade zip file from github zip file");
  81. }else{
  82. //writeLog("error", "organizr could not create upgrade zip file from github zip file");
  83. }
  84. return true;
  85. }
  86. function unzipFile($zipFile){
  87. $zip = new ZipArchive;
  88. $extractPath = "upgrade/";
  89. if($zip->open($extractPath . $zipFile) != "true"){
  90. //writeLog("error", "organizr could not unzip upgrade.zip");
  91. }else{
  92. //writeLog("success", "organizr unzipped upgrade.zip");
  93. }
  94. /* Extract Zip File */
  95. $zip->extractTo($extractPath);
  96. $zip->close();
  97. return true;
  98. }
  99. // Function to remove folders and files
  100. function rrmdir($dir) {
  101. if (is_dir($dir)) {
  102. $files = scandir($dir);
  103. foreach ($files as $file)
  104. if ($file != "." && $file != "..") rrmdir("$dir/$file");
  105. rmdir($dir);
  106. }
  107. else if (file_exists($dir)) unlink($dir);
  108. return true;
  109. }
  110. // Function to Copy folders and files
  111. function rcopy($src, $dst) {
  112. if (is_dir ( $src )) {
  113. if (!file_exists($dst)) : mkdir ( $dst ); endif;
  114. $files = scandir ( $src );
  115. foreach ( $files as $file )
  116. if ($file != "." && $file != "..")
  117. rcopy ( "$src/$file", "$dst/$file" );
  118. } else if (file_exists ( $src ))
  119. copy ( $src, $dst );
  120. return true;
  121. }