update-functions.php 4.8 KB

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