update-functions.php 5.7 KB

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