admin.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * DokuWiki Plugin upgrade (Admin Component)
  4. *
  5. * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
  6. * @author Andreas Gohr <andi@splitbrain.org>
  7. */
  8. require_once __DIR__ . '/vendor/autoload.php';
  9. class admin_plugin_upgrade extends DokuWiki_Admin_Plugin
  10. {
  11. protected $haderrors = false;
  12. /** @var helper_plugin_upgrade */
  13. protected $helper;
  14. /**
  15. * initialize helper
  16. */
  17. public function __construct()
  18. {
  19. $this->helper = plugin_load('helper', 'upgrade');
  20. $this->helper->setLogger($this);
  21. }
  22. /** @inheritDoc */
  23. public function getMenuSort()
  24. {
  25. return 555;
  26. }
  27. /** @inheritDoc */
  28. public function handle()
  29. {
  30. if (!empty($_REQUEST['step']) && !checkSecurityToken()) {
  31. unset($_REQUEST['step']);
  32. }
  33. }
  34. public function html()
  35. {
  36. $abrt = false;
  37. $next = false;
  38. $ok = true;
  39. echo '<h1>' . $this->getLang('menu') . '</h1>';
  40. echo '<div id="plugin__upgrade">';
  41. // enable auto scroll
  42. ?>
  43. <script type="text/javascript">
  44. var plugin_upgrade = window.setInterval(function () {
  45. var obj = document.getElementById('plugin__upgrade');
  46. if (obj) obj.scrollTop = obj.scrollHeight;
  47. }, 25);
  48. </script>
  49. <?php
  50. // handle current step
  51. $this->nextStep($abrt, $next, $ok);
  52. // disable auto scroll
  53. ?>
  54. <script type="text/javascript">
  55. window.setTimeout(function () {
  56. window.clearInterval(plugin_upgrade);
  57. }, 50);
  58. </script>
  59. <?php
  60. echo '</div>';
  61. $careful = '';
  62. if (!$ok) {
  63. echo '<div id="plugin__upgrade_careful">' . $this->getLang('careful') . '</div>';
  64. $careful = 'careful';
  65. }
  66. $action = script();
  67. echo '<form action="' . $action . '" method="post" id="plugin__upgrade_form">';
  68. echo '<input type="hidden" name="do" value="admin" />';
  69. echo '<input type="hidden" name="page" value="upgrade" />';
  70. echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
  71. if ($next) echo '<button type="submit" name="step[' . $next . ']" value="1" class="button continue ' . $careful . '">' . $this->getLang('btn_continue') . ' ➡</button>';
  72. if ($abrt) echo '<button type="submit" name="step[cancel]" value="1" class="button abort">✖ ' . $this->getLang('btn_abort') . '</button>';
  73. echo '</form>';
  74. $this->displayProgressBar($next);
  75. }
  76. /**
  77. * Display a progress bar of all steps
  78. *
  79. * @param string $next the next step
  80. */
  81. private function displayProgressBar($next)
  82. {
  83. $steps = ['version', 'download', 'unpack', 'check', 'upgrade'];
  84. $active = true;
  85. $count = 0;
  86. echo '<div id="plugin__upgrade_meter"><ol>';
  87. foreach ($steps as $step) {
  88. $count++;
  89. if ($step == $next) $active = false;
  90. if ($active) {
  91. echo '<li class="active">';
  92. echo '<span class="step">✔</span>';
  93. } else {
  94. echo '<li>';
  95. echo '<span class="step">' . $count . '</span>';
  96. }
  97. echo '<span class="stage">' . $this->getLang('step_' . $step) . '</span>';
  98. echo '</li>';
  99. }
  100. echo '</ol></div>';
  101. }
  102. /**
  103. * Decides the current step and executes it
  104. *
  105. * @param bool $abrt
  106. * @param bool $next
  107. * @param bool $ok
  108. */
  109. private function nextStep(&$abrt, &$next, &$ok)
  110. {
  111. if (isset($_REQUEST['step']) && is_array($_REQUEST['step'])) {
  112. $keys = array_keys($_REQUEST['step']);
  113. $step = array_shift($keys);
  114. } else {
  115. $step = '';
  116. }
  117. if ($step == 'cancel' || $step == 'done') {
  118. $ok = $this->helper->cleanup();
  119. if ($step == 'cancel') $step = '';
  120. }
  121. if ($step) {
  122. $abrt = true;
  123. $next = false;
  124. if ($step == 'version') {
  125. $ok = $this->helper->checkVersions();
  126. $next = 'download';
  127. } elseif ($step == 'done') {
  128. echo $this->locale_xhtml('final');
  129. $next = '';
  130. $abrt = '';
  131. } elseif (!file_exists($this->helper->tgzfile)) {
  132. if ($ok = $this->helper->downloadTarball()) $next = 'unpack';
  133. } elseif (!is_dir($this->helper->tgzdir)) {
  134. if ($ok = $this->helper->extractTarball()) $next = 'check';
  135. } elseif ($step != 'upgrade') {
  136. if ($ok = $this->helper->checkPermissions()) $next = 'upgrade';
  137. } elseif ($step == 'upgrade') {
  138. if ($ok = $this->helper->copyFiles()) {
  139. $ok = $this->helper->deleteObsoleteFiles();
  140. $this->helper->cleanup();
  141. $next = 'done';
  142. $abrt = '';
  143. }
  144. } else {
  145. echo 'uhm. what happened? where am I? This should not happen';
  146. }
  147. } else {
  148. # first time run, show intro
  149. echo $this->locale_xhtml('step0');
  150. $this->helper->cleanup(); // make sure we start clean
  151. $abrt = false;
  152. $next = 'version';
  153. }
  154. }
  155. /**
  156. * Print the given message and flush buffers
  157. *
  158. * @param string $level
  159. * @param string $message
  160. */
  161. public function log($level, $message) {
  162. echo '<div class="log-' . $level . '">' . $message . '</div>';
  163. flush();
  164. ob_flush();
  165. }
  166. }