4
0

update-functions.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. trait UpdateFunctions
  3. {
  4. public function updateOrganizr()
  5. {
  6. if ($this->docker) {
  7. return $this->dockerUpdate();
  8. } elseif ($this->getOS() == 'win') {
  9. return $this->windowsUpdate();
  10. } else {
  11. return $this->linuxUpdate();
  12. }
  13. }
  14. public function dockerUpdate()
  15. {
  16. if (!$this->docker) {
  17. $this->setResponse(409, 'Your install type is not Docker');
  18. return false;
  19. }
  20. $dockerUpdate = null;
  21. ini_set('max_execution_time', 0);
  22. set_time_limit(0);
  23. chdir('/etc/cont-init.d/');
  24. if (file_exists('./30-install')) {
  25. $this->setAPIResponse('error', 'Update failed - OrgTools is deprecated - please use organizr/organizr', 500);
  26. return false;
  27. } elseif (file_exists('./40-install')) {
  28. $dockerUpdate = shell_exec('./40-install');
  29. }
  30. if ($dockerUpdate) {
  31. $this->setAPIResponse('success', $dockerUpdate, 200);
  32. return true;
  33. } else {
  34. $this->setAPIResponse('error', 'Update failed', 500);
  35. return false;
  36. }
  37. }
  38. public function windowsUpdate()
  39. {
  40. if ($this->docker || $this->getOS() !== 'win') {
  41. $this->setResponse(409, 'Your install type is not Windows');
  42. return false;
  43. }
  44. $branch = ($this->config['branch'] == 'v2-master') ? '-m' : '-d';
  45. ini_set('max_execution_time', 0);
  46. set_time_limit(0);
  47. $logFile = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'scripts' . DIRECTORY_SEPARATOR . 'log.txt';
  48. $windowsScript = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'scripts' . DIRECTORY_SEPARATOR . 'windows-update.bat ' . $branch . ' > ' . $logFile . ' 2>&1';
  49. $windowsUpdate = shell_exec($windowsScript);
  50. if ($windowsUpdate) {
  51. $this->setAPIResponse('success', $windowsUpdate, 200);
  52. return true;
  53. } else {
  54. $this->setAPIResponse('success', 'Update Complete - check log.txt for output', 200);
  55. return false;
  56. }
  57. }
  58. public function linuxUpdate()
  59. {
  60. if ($this->docker || $this->getOS() == 'win') {
  61. $this->setResponse(409, 'Your install type is not Linux');
  62. return false;
  63. }
  64. $branch = $this->config['branch'];
  65. ini_set('max_execution_time', 0);
  66. set_time_limit(0);
  67. $logFile = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'scripts' . DIRECTORY_SEPARATOR . 'log.txt';
  68. $script = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'scripts' . DIRECTORY_SEPARATOR . 'linux-update.sh ' . $branch . ' > ' . $logFile . ' 2>&1';
  69. $update = shell_exec($script);
  70. if ($update) {
  71. $this->setAPIResponse('success', $update, 200);
  72. return true;
  73. } else {
  74. $this->setAPIResponse('success', 'Update Complete - check log.txt for output', 200);
  75. return false;
  76. }
  77. }
  78. public function upgradeInstall($branch = 'v2-master', $stage = '1')
  79. {
  80. // may kill this function in place for php script to run elsewhere
  81. if ($this->docker) {
  82. $this->setAPIResponse('error', 'Cannot perform update action on docker install - use script', 500);
  83. return false;
  84. }
  85. if ($this->getOS() == 'win') {
  86. $this->setAPIResponse('error', 'Cannot perform update action on windows install - use script', 500);
  87. return false;
  88. }
  89. $notWritable = array_search(false, $this->pathsWritable($this->paths));
  90. if ($notWritable == false) {
  91. ini_set('max_execution_time', 0);
  92. set_time_limit(0);
  93. $url = 'https://github.com/causefx/Organizr/archive/' . $branch . '.zip';
  94. $file = "upgrade.zip";
  95. $source = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'upgrade' . DIRECTORY_SEPARATOR . 'Organizr-' . str_replace('v2', '2', $branch) . DIRECTORY_SEPARATOR;
  96. $cleanup = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . "upgrade" . DIRECTORY_SEPARATOR;
  97. $destination = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR;
  98. switch ($stage) {
  99. case '1':
  100. $this->writeLog('success', 'Update Function - Started Upgrade Process', $this->user['username']);
  101. if ($this->downloadFile($url, $file)) {
  102. $this->writeLog('success', 'Update Function - Downloaded Update File for Branch: ' . $branch, $this->user['username']);
  103. $this->setAPIResponse('success', 'Downloaded file successfully', 200);
  104. return true;
  105. } else {
  106. $this->writeLog('error', 'Update Function - Downloaded Update File Failed for Branch: ' . $branch, $this->user['username']);
  107. $this->setAPIResponse('error', 'Download failed', 500);
  108. return false;
  109. }
  110. case '2':
  111. if ($this->unzipFile($file)) {
  112. $this->writeLog('success', 'Update Function - Unzipped Update File for Branch: ' . $branch, $this->user['username']);
  113. $this->setAPIResponse('success', 'Unzipped file successfully', 200);
  114. return true;
  115. } else {
  116. $this->writeLog('error', 'Update Function - Unzip Failed for Branch: ' . $branch, $this->user['username']);
  117. $this->setAPIResponse('error', 'Unzip failed', 500);
  118. return false;
  119. }
  120. case '3':
  121. if ($this->rcopy($source, $destination)) {
  122. $this->writeLog('success', 'Update Function - Files overwritten using Updated Files from Branch: ' . $branch, $this->user['username']);
  123. $updateComplete = $this->config['dbLocation'] . 'completed.txt';
  124. if (!file_exists($updateComplete)) {
  125. touch($updateComplete);
  126. }
  127. $this->setAPIResponse('success', 'Files replaced successfully', 200);
  128. return true;
  129. } else {
  130. $this->writeLog('error', 'Update Function - Overwrite Failed for Branch: ' . $branch, $this->user['username']);
  131. $this->setAPIResponse('error', 'File replacement failed', 500);
  132. return false;
  133. }
  134. case '4':
  135. if ($this->rrmdir($cleanup)) {
  136. $this->writeLog('success', 'Update Function - Deleted Update Files from Branch: ' . $branch, $this->user['username']);
  137. $this->writeLog('success', 'Update Function - Update Completed', $this->user['username']);
  138. $this->setAPIResponse('success', 'Removed update files successfully', 200);
  139. return true;
  140. } else {
  141. $this->writeLog('error', 'Update Function - Removal of Update Files Failed for Branch: ' . $branch, $this->user['username']);
  142. $this->setAPIResponse('error', 'File removal failed', 500);
  143. return false;
  144. }
  145. default:
  146. $this->setAPIResponse('error', 'Action not setup', 500);
  147. return false;
  148. }
  149. } else {
  150. $this->setAPIResponse('error', 'File permissions not set correctly', 500);
  151. return false;
  152. }
  153. }
  154. }