update-functions.php 5.9 KB

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