update-functions.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 downloadFileToPath($from, $to, $path)
  92. {
  93. ini_set('max_execution_time', 0);
  94. set_time_limit(0);
  95. if (@!mkdir($path, 0777, true)) {
  96. //writeLog("error", "organizr could not create upgrade folder");
  97. }
  98. $file = fopen($from, 'rb');
  99. if ($file) {
  100. $newf = fopen($to, 'wb');
  101. if ($newf) {
  102. while (!feof($file)) {
  103. fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
  104. }
  105. }
  106. } else {
  107. //writeLog("error", "organizr could not download $url");
  108. }
  109. if ($file) {
  110. fclose($file);
  111. //writeLog("success", "organizr finished downloading the github zip file");
  112. } else {
  113. //writeLog("error", "organizr could not download the github zip file");
  114. }
  115. if ($newf) {
  116. fclose($newf);
  117. //writeLog("success", "organizr created upgrade zip file from github zip file");
  118. } else {
  119. //writeLog("error", "organizr could not create upgrade zip file from github zip file");
  120. }
  121. return true;
  122. }
  123. function unzipFile($zipFile)
  124. {
  125. ini_set('max_execution_time', 0);
  126. set_time_limit(0);
  127. $zip = new ZipArchive;
  128. $extractPath = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . "upgrade/";
  129. if ($zip->open($extractPath . $zipFile) != "true") {
  130. //writeLog("error", "organizr could not unzip upgrade.zip");
  131. } else {
  132. //writeLog("success", "organizr unzipped upgrade.zip");
  133. }
  134. /* Extract Zip File */
  135. $zip->extractTo($extractPath);
  136. $zip->close();
  137. return true;
  138. }
  139. // Function to remove folders and files
  140. function rrmdir($dir)
  141. {
  142. ini_set('max_execution_time', 0);
  143. set_time_limit(0);
  144. if (is_dir($dir)) {
  145. $files = scandir($dir);
  146. foreach ($files as $file) {
  147. if ($file != "." && $file != "..") {
  148. rrmdir("$dir/$file");
  149. }
  150. }
  151. rmdir($dir);
  152. } elseif (file_exists($dir)) {
  153. unlink($dir);
  154. }
  155. return true;
  156. }
  157. // Function to Copy folders and files
  158. function rcopy($src, $dst)
  159. {
  160. ini_set('max_execution_time', 0);
  161. set_time_limit(0);
  162. if (is_dir($src)) {
  163. if (!file_exists($dst)) : mkdir($dst);
  164. endif;
  165. $files = scandir($src);
  166. foreach ($files as $file) {
  167. if ($file != "." && $file != "..") {
  168. rcopy("$src/$file", "$dst/$file");
  169. }
  170. }
  171. } elseif (file_exists($src)) {
  172. copy($src, $dst);
  173. }
  174. return true;
  175. }