update-functions.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. // Upgrade the installation
  3. function upgradeInstall($branch = 'v2-master', $stage)
  4. {
  5. ini_set('max_execution_time', 0);
  6. set_time_limit(0);
  7. $url = 'https://github.com/causefx/Organizr/archive/'.$branch.'.zip';
  8. $file = "upgrade.zip";
  9. $source = dirname(__DIR__, 2).DIRECTORY_SEPARATOR.'upgrade'.DIRECTORY_SEPARATOR.'Organizr-'.str_replace('v2', '2', $branch).DIRECTORY_SEPARATOR;
  10. $cleanup = dirname(__DIR__, 2) .DIRECTORY_SEPARATOR."upgrade".DIRECTORY_SEPARATOR;
  11. $destination = dirname(__DIR__, 2).DIRECTORY_SEPARATOR;
  12. switch ($stage) {
  13. case '1':
  14. writeLog('success', 'Update Function - Started Upgrade Process', $GLOBALS['organizrUser']['username']);
  15. if (downloadFile($url, $file)) {
  16. writeLog('success', 'Update Function - Downloaded Update File for Branch: '.$branch, $GLOBALS['organizrUser']['username']);
  17. return true;
  18. } else {
  19. writeLog('error', 'Update Function - Downloaded Update File Failed for Branch: '.$branch, $GLOBALS['organizrUser']['username']);
  20. return false;
  21. }
  22. break;
  23. case '2':
  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. 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. if (rrmdir($cleanup)) {
  43. writeLog('success', 'Update Function - Deleted Update Files from Branch: '.$branch, $GLOBALS['organizrUser']['username']);
  44. writeLog('success', 'Update Function - Update Completed', $GLOBALS['organizrUser']['username']);
  45. return true;
  46. } else {
  47. writeLog('error', 'Update Function - Removal of Update Files Failed for Branch: '.$branch, $GLOBALS['organizrUser']['username']);
  48. return false;
  49. }
  50. break;
  51. default:
  52. return false;
  53. break;
  54. }
  55. return false;
  56. }
  57. function downloadFile($url, $path)
  58. {
  59. ini_set('max_execution_time', 0);
  60. set_time_limit(0);
  61. $folderPath = dirname(__DIR__, 2).DIRECTORY_SEPARATOR."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. {
  93. ini_set('max_execution_time', 0);
  94. set_time_limit(0);
  95. $zip = new ZipArchive;
  96. $extractPath = dirname(__DIR__, 2).DIRECTORY_SEPARATOR."upgrade/";
  97. if ($zip->open($extractPath . $zipFile) != "true") {
  98. //writeLog("error", "organizr could not unzip upgrade.zip");
  99. } else {
  100. //writeLog("success", "organizr unzipped upgrade.zip");
  101. }
  102. /* Extract Zip File */
  103. $zip->extractTo($extractPath);
  104. $zip->close();
  105. return true;
  106. }
  107. // Function to remove folders and files
  108. function rrmdir($dir)
  109. {
  110. ini_set('max_execution_time', 0);
  111. set_time_limit(0);
  112. if (is_dir($dir)) {
  113. $files = scandir($dir);
  114. foreach ($files as $file) {
  115. if ($file != "." && $file != "..") {
  116. rrmdir("$dir/$file");
  117. }
  118. }
  119. rmdir($dir);
  120. } elseif (file_exists($dir)) {
  121. unlink($dir);
  122. }
  123. return true;
  124. }
  125. // Function to Copy folders and files
  126. function rcopy($src, $dst)
  127. {
  128. ini_set('max_execution_time', 0);
  129. set_time_limit(0);
  130. if (is_dir($src)) {
  131. if (!file_exists($dst)) : mkdir($dst);
  132. endif;
  133. $files = scandir($src);
  134. foreach ($files as $file) {
  135. if ($file != "." && $file != "..") {
  136. rcopy("$src/$file", "$dst/$file");
  137. }
  138. }
  139. } elseif (file_exists($src)) {
  140. copy($src, $dst);
  141. }
  142. return true;
  143. }