update-functions.php 5.7 KB

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