update-functions.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. // Upgrade the installation
  3. function upgradeInstall($branch = 'v2-master', $stage)
  4. {
  5. $notWritable = array_search(false, pathsWritable($GLOBALS['paths']));
  6. if ($notWritable == false) {
  7. ini_set('max_execution_time', 0);
  8. set_time_limit(0);
  9. $url = 'https://github.com/causefx/Organizr/archive/' . $branch . '.zip';
  10. $file = "upgrade.zip";
  11. $source = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'upgrade' . DIRECTORY_SEPARATOR . 'Organizr-' . str_replace('v2', '2', $branch) . DIRECTORY_SEPARATOR;
  12. $cleanup = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . "upgrade" . DIRECTORY_SEPARATOR;
  13. $destination = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR;
  14. switch ($stage) {
  15. case '1':
  16. writeLog('success', 'Update Function - Started Upgrade Process', $GLOBALS['organizrUser']['username']);
  17. if (downloadFile($url, $file)) {
  18. writeLog('success', 'Update Function - Downloaded Update File for Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
  19. return true;
  20. } else {
  21. writeLog('error', 'Update Function - Downloaded Update File Failed for Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
  22. return false;
  23. }
  24. break;
  25. case '2':
  26. if (unzipFile($file)) {
  27. writeLog('success', 'Update Function - Unzipped Update File for Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
  28. return true;
  29. } else {
  30. writeLog('error', 'Update Function - Unzip Failed for Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
  31. return false;
  32. }
  33. break;
  34. case '3':
  35. if (rcopy($source, $destination)) {
  36. writeLog('success', 'Update Function - Overwrited Files using Updated Files from Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
  37. $updateComplete = $GLOBALS['dbLocation'] . 'completed.txt';
  38. if (!file_exists($updateComplete)) {
  39. touch($updateComplete);
  40. }
  41. return true;
  42. } else {
  43. writeLog('error', 'Update Function - Overwrite Failed for Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
  44. return false;
  45. }
  46. break;
  47. case '4':
  48. if (rrmdir($cleanup)) {
  49. writeLog('success', 'Update Function - Deleted Update Files from Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
  50. writeLog('success', 'Update Function - Update Completed', $GLOBALS['organizrUser']['username']);
  51. return true;
  52. } else {
  53. writeLog('error', 'Update Function - Removal of Update Files Failed for Branch: ' . $branch, $GLOBALS['organizrUser']['username']);
  54. return false;
  55. }
  56. break;
  57. default:
  58. return false;
  59. break;
  60. }
  61. } else {
  62. return 'permissions';
  63. }
  64. }
  65. function downloadFile($url, $path)
  66. {
  67. ini_set('max_execution_time', 0);
  68. set_time_limit(0);
  69. $folderPath = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . "upgrade" . DIRECTORY_SEPARATOR;
  70. if (!mkdir($folderPath)) {
  71. //writeLog("error", "organizr could not create upgrade folder");
  72. }
  73. $newfname = $folderPath . $path;
  74. $file = fopen($url, 'rb');
  75. if ($file) {
  76. $newf = fopen($newfname, 'wb');
  77. if ($newf) {
  78. while (!feof($file)) {
  79. fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
  80. }
  81. }
  82. } else {
  83. //writeLog("error", "organizr could not download $url");
  84. }
  85. if ($file) {
  86. fclose($file);
  87. //writeLog("success", "organizr finished downloading the github zip file");
  88. } else {
  89. //writeLog("error", "organizr could not download the github zip file");
  90. }
  91. if ($newf) {
  92. fclose($newf);
  93. //writeLog("success", "organizr created upgrade zip file from github zip file");
  94. } else {
  95. //writeLog("error", "organizr could not create upgrade zip file from github zip file");
  96. }
  97. return true;
  98. }
  99. function downloadFileToPath($from, $to, $path)
  100. {
  101. ini_set('max_execution_time', 0);
  102. set_time_limit(0);
  103. if (@!mkdir($path, 0777, true)) {
  104. //writeLog("error", "organizr could not create upgrade folder");
  105. }
  106. $file = fopen($from, 'rb');
  107. if ($file) {
  108. $newf = fopen($to, 'wb');
  109. if ($newf) {
  110. while (!feof($file)) {
  111. fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
  112. }
  113. }
  114. } else {
  115. //writeLog("error", "organizr could not download $url");
  116. }
  117. if ($file) {
  118. fclose($file);
  119. //writeLog("success", "organizr finished downloading the github zip file");
  120. } else {
  121. //writeLog("error", "organizr could not download the github zip file");
  122. }
  123. if ($newf) {
  124. fclose($newf);
  125. //writeLog("success", "organizr created upgrade zip file from github zip file");
  126. } else {
  127. //writeLog("error", "organizr could not create upgrade zip file from github zip file");
  128. }
  129. return true;
  130. }
  131. function unzipFile($zipFile)
  132. {
  133. ini_set('max_execution_time', 0);
  134. set_time_limit(0);
  135. $zip = new ZipArchive;
  136. $extractPath = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . "upgrade/";
  137. if ($zip->open($extractPath . $zipFile) != "true") {
  138. //writeLog("error", "organizr could not unzip upgrade.zip");
  139. } else {
  140. //writeLog("success", "organizr unzipped upgrade.zip");
  141. }
  142. /* Extract Zip File */
  143. $zip->extractTo($extractPath);
  144. $zip->close();
  145. return true;
  146. }
  147. // Function to remove folders and files
  148. function rrmdir($dir)
  149. {
  150. ini_set('max_execution_time', 0);
  151. set_time_limit(0);
  152. if (is_dir($dir)) {
  153. $files = scandir($dir);
  154. foreach ($files as $file) {
  155. if ($file != "." && $file != "..") {
  156. rrmdir("$dir/$file");
  157. }
  158. }
  159. rmdir($dir);
  160. } elseif (file_exists($dir)) {
  161. unlink($dir);
  162. }
  163. return true;
  164. }
  165. // Function to Copy folders and files
  166. function rcopy($src, $dst)
  167. {
  168. ini_set('max_execution_time', 0);
  169. set_time_limit(0);
  170. if (is_dir($src)) {
  171. if (!file_exists($dst)) : mkdir($dst);
  172. endif;
  173. $files = scandir($src);
  174. foreach ($files as $file) {
  175. if ($file != "." && $file != "..") {
  176. rcopy("$src/$file", "$dst/$file");
  177. }
  178. }
  179. } elseif (file_exists($src)) {
  180. copy($src, $dst);
  181. }
  182. return true;
  183. }