update-functions.php 4.7 KB

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