update-functions.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. $updateComplete = $GLOBALS['dbLocation'] . 'completed.txt';
  36. if (!file_exists($updateComplete)) {
  37. touch($updateComplete);
  38. }
  39. return true;
  40. } else {
  41. writeLog('error', 'Update Function - Overwrite Failed for Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
  42. return false;
  43. }
  44. break;
  45. case '4':
  46. if (rrmdir($cleanup)) {
  47. writeLog('success', 'Update Function - Deleted Update Files from Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
  48. writeLog('success', 'Update Function - Update Completed', $GLOBALS['organizrUser']['username']);
  49. return true;
  50. } else {
  51. writeLog('error', 'Update Function - Removal of Update Files Failed for Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
  52. return false;
  53. }
  54. break;
  55. default:
  56. return false;
  57. break;
  58. }
  59. return false;
  60. }
  61. function downloadFile($url, $path)
  62. {
  63. ini_set('max_execution_time', 0);
  64. set_time_limit(0);
  65. $folderPath = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . "upgrade" . DIRECTORY_SEPARATOR;
  66. if (!mkdir($folderPath)) {
  67. //writeLog("error", "organizr could not create upgrade folder");
  68. }
  69. $newfname = $folderPath . $path;
  70. $file = fopen($url, 'rb');
  71. if ($file) {
  72. $newf = fopen($newfname, 'wb');
  73. if ($newf) {
  74. while (!feof($file)) {
  75. fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
  76. }
  77. }
  78. } else {
  79. //writeLog("error", "organizr could not download $url");
  80. }
  81. if ($file) {
  82. fclose($file);
  83. //writeLog("success", "organizr finished downloading the github zip file");
  84. } else {
  85. //writeLog("error", "organizr could not download the github zip file");
  86. }
  87. if ($newf) {
  88. fclose($newf);
  89. //writeLog("success", "organizr created upgrade zip file from github zip file");
  90. } else {
  91. //writeLog("error", "organizr could not create upgrade zip file from github zip file");
  92. }
  93. return true;
  94. }
  95. function downloadFileToPath($from, $to, $path)
  96. {
  97. ini_set('max_execution_time', 0);
  98. set_time_limit(0);
  99. if (@!mkdir($path, 0777, true)) {
  100. //writeLog("error", "organizr could not create upgrade folder");
  101. }
  102. $file = fopen($from, 'rb');
  103. if ($file) {
  104. $newf = fopen($to, 'wb');
  105. if ($newf) {
  106. while (!feof($file)) {
  107. fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
  108. }
  109. }
  110. } else {
  111. //writeLog("error", "organizr could not download $url");
  112. }
  113. if ($file) {
  114. fclose($file);
  115. //writeLog("success", "organizr finished downloading the github zip file");
  116. } else {
  117. //writeLog("error", "organizr could not download the github zip file");
  118. }
  119. if ($newf) {
  120. fclose($newf);
  121. //writeLog("success", "organizr created upgrade zip file from github zip file");
  122. } else {
  123. //writeLog("error", "organizr could not create upgrade zip file from github zip file");
  124. }
  125. return true;
  126. }
  127. function unzipFile($zipFile)
  128. {
  129. ini_set('max_execution_time', 0);
  130. set_time_limit(0);
  131. $zip = new ZipArchive;
  132. $extractPath = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . "upgrade/";
  133. if ($zip->open($extractPath . $zipFile) != "true") {
  134. //writeLog("error", "organizr could not unzip upgrade.zip");
  135. } else {
  136. //writeLog("success", "organizr unzipped upgrade.zip");
  137. }
  138. /* Extract Zip File */
  139. $zip->extractTo($extractPath);
  140. $zip->close();
  141. return true;
  142. }
  143. // Function to remove folders and files
  144. function rrmdir($dir)
  145. {
  146. ini_set('max_execution_time', 0);
  147. set_time_limit(0);
  148. if (is_dir($dir)) {
  149. $files = scandir($dir);
  150. foreach ($files as $file) {
  151. if ($file != "." && $file != "..") {
  152. rrmdir("$dir/$file");
  153. }
  154. }
  155. rmdir($dir);
  156. } elseif (file_exists($dir)) {
  157. unlink($dir);
  158. }
  159. return true;
  160. }
  161. // Function to Copy folders and files
  162. function rcopy($src, $dst)
  163. {
  164. ini_set('max_execution_time', 0);
  165. set_time_limit(0);
  166. if (is_dir($src)) {
  167. if (!file_exists($dst)) : mkdir($dst);
  168. endif;
  169. $files = scandir($src);
  170. foreach ($files as $file) {
  171. if ($file != "." && $file != "..") {
  172. rcopy("$src/$file", "$dst/$file");
  173. }
  174. }
  175. } elseif (file_exists($src)) {
  176. copy($src, $dst);
  177. }
  178. return true;
  179. }