update-functions.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. // Upgrade the installation
  3. function upgradeInstall($branch = 'v2-master', $stage) {
  4. ini_set('max_execution_time',0);
  5. set_time_limit(0);
  6. $url = 'https://github.com/causefx/Organizr/archive/'.$branch.'.zip';
  7. $file = "upgrade.zip";
  8. $source = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'upgrade'.DIRECTORY_SEPARATOR.'Organizr-'.str_replace('v2','2',$branch).DIRECTORY_SEPARATOR;
  9. $cleanup = dirname(__DIR__,2) .DIRECTORY_SEPARATOR."upgrade".DIRECTORY_SEPARATOR;
  10. $destination = dirname(__DIR__,2).DIRECTORY_SEPARATOR;
  11. switch ($stage) {
  12. case '1':
  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. 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. if(rcopy($source, $destination)){
  33. writeLog('success', 'Update Function - Overwrited Files using Updated Files from Branch: '.$branch, $GLOBALS['organizrUser']['username']);
  34. return true;
  35. }else{
  36. writeLog('error', 'Update Function - Overwrite Failed for Branch: '.$branch, $GLOBALS['organizrUser']['username']);
  37. return false;
  38. }
  39. break;
  40. case '4':
  41. if(rrmdir($cleanup)){
  42. writeLog('success', 'Update Function - Deleted Update Files from Branch: '.$branch, $GLOBALS['organizrUser']['username']);
  43. writeLog('success', 'Update Function - Update Completed', $GLOBALS['organizrUser']['username']);
  44. return true;
  45. }else{
  46. writeLog('error', 'Update Function - Removal of Update Files Failed for Branch: '.$branch, $GLOBALS['organizrUser']['username']);
  47. return false;
  48. }
  49. break;
  50. default:
  51. return false;
  52. break;
  53. }
  54. return false;
  55. }
  56. function downloadFile($url, $path){
  57. ini_set('max_execution_time',0);
  58. set_time_limit(0);
  59. $folderPath = dirname(__DIR__,2).DIRECTORY_SEPARATOR."upgrade".DIRECTORY_SEPARATOR;
  60. if(!mkdir($folderPath)){
  61. //writeLog("error", "organizr could not create upgrade folder");
  62. }
  63. $newfname = $folderPath . $path;
  64. $file = fopen ($url, 'rb');
  65. if ($file) {
  66. $newf = fopen ($newfname, 'wb');
  67. if ($newf) {
  68. while(!feof($file)) {
  69. fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
  70. }
  71. }
  72. }else{
  73. //writeLog("error", "organizr could not download $url");
  74. }
  75. if ($file) {
  76. fclose($file);
  77. //writeLog("success", "organizr finished downloading the github zip file");
  78. }else{
  79. //writeLog("error", "organizr could not download the github zip file");
  80. }
  81. if ($newf) {
  82. fclose($newf);
  83. //writeLog("success", "organizr created upgrade zip file from github zip file");
  84. }else{
  85. //writeLog("error", "organizr could not create upgrade zip file from github zip file");
  86. }
  87. return true;
  88. }
  89. function unzipFile($zipFile){
  90. ini_set('max_execution_time',0);
  91. set_time_limit(0);
  92. $zip = new ZipArchive;
  93. $extractPath = dirname(__DIR__,2).DIRECTORY_SEPARATOR."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. ini_set('max_execution_time',0);
  107. set_time_limit(0);
  108. if (is_dir($dir)) {
  109. $files = scandir($dir);
  110. foreach ($files as $file)
  111. if ($file != "." && $file != "..") rrmdir("$dir/$file");
  112. rmdir($dir);
  113. }
  114. else if (file_exists($dir)) unlink($dir);
  115. return true;
  116. }
  117. // Function to Copy folders and files
  118. function rcopy($src, $dst) {
  119. ini_set('max_execution_time',0);
  120. set_time_limit(0);
  121. if (is_dir ( $src )) {
  122. if (!file_exists($dst)) : mkdir ( $dst ); endif;
  123. $files = scandir ( $src );
  124. foreach ( $files as $file )
  125. if ($file != "." && $file != "..")
  126. rcopy ( "$src/$file", "$dst/$file" );
  127. } else if (file_exists ( $src ))
  128. copy ( $src, $dst );
  129. return true;
  130. }