update-functions.php 7.6 KB

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