update-functions.php 4.3 KB

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