update-functions.php 6.8 KB

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