| 1 | <?php |
|---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
|---|
| 3 | # |
|---|
| 4 | # This file is part of Dotclear 2. |
|---|
| 5 | # |
|---|
| 6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
|---|
| 7 | # Licensed under the GPL version 2.0 license. |
|---|
| 8 | # See LICENSE file or |
|---|
| 9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
|---|
| 10 | # |
|---|
| 11 | # -- END LICENSE BLOCK ----------------------------------------- |
|---|
| 12 | if (!defined('DC_CONTEXT_ADMIN')) { return; } |
|---|
| 13 | |
|---|
| 14 | dcPage::checkSuper(); |
|---|
| 15 | |
|---|
| 16 | // main class |
|---|
| 17 | |
|---|
| 18 | $maintenance = new dcMaintenance($core); |
|---|
| 19 | $core->blog->settings->addNamespace('maintenance'); |
|---|
| 20 | |
|---|
| 21 | // Set var |
|---|
| 22 | |
|---|
| 23 | $msg = ''; |
|---|
| 24 | $headers = ''; |
|---|
| 25 | $p_url = 'plugin.php?p=maintenance'; |
|---|
| 26 | $task = null; |
|---|
| 27 | $expired = array(); |
|---|
| 28 | |
|---|
| 29 | $code = empty($_POST['code']) ? null : (integer) $_POST['code']; |
|---|
| 30 | $tab = empty($_REQUEST['tab']) ? 'maintenance' : $_REQUEST['tab']; |
|---|
| 31 | |
|---|
| 32 | // Save settings |
|---|
| 33 | |
|---|
| 34 | if (!empty($_POST['settings'])) { |
|---|
| 35 | |
|---|
| 36 | try { |
|---|
| 37 | $core->blog->settings->maintenance->put( |
|---|
| 38 | 'plugin_message', |
|---|
| 39 | !empty($_POST['settings_plugin_message']), |
|---|
| 40 | 'boolean', |
|---|
| 41 | 'Display alert message of expired tasks on plugin page', |
|---|
| 42 | true, |
|---|
| 43 | true |
|---|
| 44 | ); |
|---|
| 45 | |
|---|
| 46 | foreach($maintenance->getTasks() as $t) { |
|---|
| 47 | if (!empty($_POST['settings_recall_time']) && $_POST['settings_recall_time'] == 'seperate') { |
|---|
| 48 | $ts = empty($_POST['settings_ts_'.$t->id()]) ? 0 : $_POST['settings_ts_'.$t->id()]; |
|---|
| 49 | } |
|---|
| 50 | else { |
|---|
| 51 | $ts = $_POST['settings_recall_time']; |
|---|
| 52 | } |
|---|
| 53 | $core->blog->settings->maintenance->put( |
|---|
| 54 | 'ts_'.$t->id(), |
|---|
| 55 | abs((integer) $ts), |
|---|
| 56 | 'integer', |
|---|
| 57 | sprintf('Recall time for task %s', $t->id()), |
|---|
| 58 | true, |
|---|
| 59 | true |
|---|
| 60 | ); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | http::redirect($p_url.'&done=1&tab='.$tab); |
|---|
| 64 | } |
|---|
| 65 | catch(Exception $e) { |
|---|
| 66 | $core->error->add($e->getMessage()); |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | // Get task object |
|---|
| 71 | |
|---|
| 72 | if (!empty($_REQUEST['task'])) { |
|---|
| 73 | $task = $maintenance->getTask($_REQUEST['task']); |
|---|
| 74 | |
|---|
| 75 | if ($task === null) { |
|---|
| 76 | $core->error->add('Unknow task ID'); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | $task->code($code); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | // Execute task |
|---|
| 83 | |
|---|
| 84 | if ($task && !empty($_POST['task']) && $task->id() == $_POST['task']) { |
|---|
| 85 | try { |
|---|
| 86 | $code = $task->execute(); |
|---|
| 87 | if (false === $code) { |
|---|
| 88 | throw new Exception($task->error()); |
|---|
| 89 | } |
|---|
| 90 | if (true === $code) { |
|---|
| 91 | $maintenance->setLog($task->id()); |
|---|
| 92 | http::redirect($p_url.'&task='.$task->id().'&done=1&tab='.$tab); |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | catch (Exception $e) { |
|---|
| 96 | $core->error->add($e->getMessage()); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | // Combos |
|---|
| 101 | |
|---|
| 102 | $combo_ts = array( |
|---|
| 103 | __('Every week') => 604800, |
|---|
| 104 | __('Every two weeks') => 1209600, |
|---|
| 105 | __('Every month') => 2592000, |
|---|
| 106 | __('Every two months') => 5184000 |
|---|
| 107 | ); |
|---|
| 108 | |
|---|
| 109 | $full_combo_ts = array_merge(array( |
|---|
| 110 | __('Use different periods for each task') => 'seperate'), |
|---|
| 111 | $combo_ts |
|---|
| 112 | ); |
|---|
| 113 | |
|---|
| 114 | $task_combo_ts = array_merge(array( |
|---|
| 115 | __('Never') => 0), |
|---|
| 116 | $combo_ts |
|---|
| 117 | ); |
|---|
| 118 | |
|---|
| 119 | // Display page |
|---|
| 120 | |
|---|
| 121 | echo '<html><head> |
|---|
| 122 | <title>'.__('Maintenance').'</title>'. |
|---|
| 123 | dcPage::jsPageTabs($tab). |
|---|
| 124 | dcPage::jsLoad('index.php?pf=maintenance/js/settings.js');; |
|---|
| 125 | |
|---|
| 126 | if ($task && $task->ajax()) { |
|---|
| 127 | echo |
|---|
| 128 | '<script type="text/javascript">'."\n". |
|---|
| 129 | "//<![CDATA[\n". |
|---|
| 130 | dcPage::jsVar('dotclear.msg.wait', __('Please wait...')). |
|---|
| 131 | "//]]>\n". |
|---|
| 132 | '</script>'. |
|---|
| 133 | dcPage::jsLoad('index.php?pf=maintenance/js/dc.maintenance.js'); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | echo |
|---|
| 137 | $maintenance->getHeaders().' |
|---|
| 138 | </head> |
|---|
| 139 | <body>'; |
|---|
| 140 | |
|---|
| 141 | // Success message |
|---|
| 142 | |
|---|
| 143 | if (!empty($_GET['done']) && $tab == 'settings') { |
|---|
| 144 | $msg = dcPage::success(__('Settings successfully updated'), true, true, false); |
|---|
| 145 | } |
|---|
| 146 | elseif (!empty($_GET['done']) && $task) { |
|---|
| 147 | $msg = dcPage::success($task->success(), true, true, false); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | if ($task && ($res = $task->step()) !== null) { |
|---|
| 151 | |
|---|
| 152 | // Page title |
|---|
| 153 | |
|---|
| 154 | echo dcPage::breadcrumb( |
|---|
| 155 | array( |
|---|
| 156 | __('Plugins') => '', |
|---|
| 157 | '<a href="'.$p_url.'">'.__('Maintenance').'</a>' => '', |
|---|
| 158 | '<span class="page-title">'.html::escapeHTML($task->name()).'</span>' => '' |
|---|
| 159 | ) |
|---|
| 160 | ); |
|---|
| 161 | |
|---|
| 162 | echo $msg; |
|---|
| 163 | |
|---|
| 164 | // Intermediate task (task required several steps) |
|---|
| 165 | |
|---|
| 166 | echo |
|---|
| 167 | '<div class="step-box" id="'.$task->id().'">'. |
|---|
| 168 | '<h3>'.html::escapeHTML($task->name()).'</h3>'. |
|---|
| 169 | '<form action="'.$p_url.'" method="post">'. |
|---|
| 170 | '<p class="step-msg">'. |
|---|
| 171 | $res. |
|---|
| 172 | '</p>'. |
|---|
| 173 | '<p class="step-submit">'. |
|---|
| 174 | '<input type="submit" value="'.$task->task().'" /> '. |
|---|
| 175 | form::hidden(array('task'), $task->id()). |
|---|
| 176 | form::hidden(array('code'), (integer) $code). |
|---|
| 177 | $core->formNonce(). |
|---|
| 178 | '</p>'. |
|---|
| 179 | '</form>'. |
|---|
| 180 | '<p class="step-back">'. |
|---|
| 181 | '<a class="back" href="'.$p_url.'&tab='.$task->tab().'">'.__('Back').'</a>'. |
|---|
| 182 | '</p>'. |
|---|
| 183 | '</div>'; |
|---|
| 184 | } |
|---|
| 185 | else { |
|---|
| 186 | |
|---|
| 187 | // Page title |
|---|
| 188 | |
|---|
| 189 | echo dcPage::breadcrumb( |
|---|
| 190 | array( |
|---|
| 191 | __('Plugins') => '', |
|---|
| 192 | '<span class="page-title">'.__('Maintenance').'</span>' => '' |
|---|
| 193 | ) |
|---|
| 194 | ); |
|---|
| 195 | |
|---|
| 196 | echo $msg; |
|---|
| 197 | |
|---|
| 198 | // Simple task (with only a button to start it) |
|---|
| 199 | |
|---|
| 200 | foreach($maintenance->getTabs() as $tab_id => $tab_name) |
|---|
| 201 | { |
|---|
| 202 | $res_group = ''; |
|---|
| 203 | foreach($maintenance->getGroups($core) as $group_id => $group_name) |
|---|
| 204 | { |
|---|
| 205 | $res_task = ''; |
|---|
| 206 | foreach($maintenance->getTasks($core) as $t) |
|---|
| 207 | { |
|---|
| 208 | if ($t->group() != $group_id || $t->tab() != $tab_id) { |
|---|
| 209 | continue; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | $res_task .= |
|---|
| 213 | '<p>'.form::radio(array('task', $t->id()), $t->id()).' '. |
|---|
| 214 | '<label class="classic" for="'.$t->id().'">'. |
|---|
| 215 | html::escapeHTML($t->task()).'</label>'; |
|---|
| 216 | |
|---|
| 217 | // Expired task alert message |
|---|
| 218 | $ts = $t->expired(); |
|---|
| 219 | if ($core->blog->settings->maintenance->plugin_message && $ts !== false) { |
|---|
| 220 | if ($ts === null) { |
|---|
| 221 | $res_task .= |
|---|
| 222 | '<br /> <span class="warn">'. |
|---|
| 223 | __('This task has never been executed.').' '. |
|---|
| 224 | __('You should execute it now.').'</span>'; |
|---|
| 225 | } |
|---|
| 226 | else { |
|---|
| 227 | $res_task .= |
|---|
| 228 | '<br /> <span class="warn">'.sprintf( |
|---|
| 229 | __('Last execution of this task was on %s.'), |
|---|
| 230 | dt::str($core->blog->settings->system->date_format, $ts).' '. |
|---|
| 231 | dt::str($core->blog->settings->system->time_format, $ts) |
|---|
| 232 | ).' '. |
|---|
| 233 | __('You should execute it now.').'</span>'; |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | $res_task .= '</p>'; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | if (!empty($res_task)) { |
|---|
| 241 | $res_group .= |
|---|
| 242 | '<div class="fieldset">'. |
|---|
| 243 | '<h4 id="'.$group_id.'">'.$group_name.'</h4>'. |
|---|
| 244 | $res_task. |
|---|
| 245 | '</div>'; |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | if (!empty($res_group)) { |
|---|
| 250 | echo |
|---|
| 251 | '<div id="'.$tab_id.'" class="multi-part" title="'.$tab_name.'">'. |
|---|
| 252 | '<h3>'.$tab_name.'</h3>'. |
|---|
| 253 | '<form action="'.$p_url.'" method="post">'. |
|---|
| 254 | $res_group. |
|---|
| 255 | '<p><input type="submit" value="'.__('Execute task').'" /> '. |
|---|
| 256 | form::hidden(array('tab'), $tab_id). |
|---|
| 257 | $core->formNonce().'</p>'. |
|---|
| 258 | '<p class="form-note info">'.__('This may take a very long time.').'.</p>'. |
|---|
| 259 | '</form>'. |
|---|
| 260 | '</div>'; |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | // Advanced tasks (that required a tab) |
|---|
| 265 | |
|---|
| 266 | foreach($maintenance->getTasks($core) as $t) |
|---|
| 267 | { |
|---|
| 268 | if ($t->group() !== null) { |
|---|
| 269 | continue; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | echo |
|---|
| 273 | '<div id="'.$t->id().'" class="multi-part" title="'.$t->name().'">'. |
|---|
| 274 | '<h3>'.$t->name().'</h3>'. |
|---|
| 275 | '<form action="'.$p_url.'" method="post">'. |
|---|
| 276 | $t->content(). |
|---|
| 277 | '<p><input type="submit" value="'.__('Execute task').'" /> '. |
|---|
| 278 | form::hidden(array('task'), $t->id()). |
|---|
| 279 | form::hidden(array('tab'), $t->id()). |
|---|
| 280 | $core->formNonce().'</p>'. |
|---|
| 281 | '</form>'. |
|---|
| 282 | '</div>'; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | // Settings |
|---|
| 286 | |
|---|
| 287 | echo |
|---|
| 288 | '<div id="settings" class="multi-part" title="'.__('Settings').'">'. |
|---|
| 289 | '<h3>'.__('Settings').'</h3>'. |
|---|
| 290 | '<form action="'.$p_url.'" method="post">'. |
|---|
| 291 | |
|---|
| 292 | '<p><label for="settings_plugin_message" class="classic">'. |
|---|
| 293 | form::checkbox('settings_plugin_message', 1, $core->blog->settings->maintenance->plugin_message). |
|---|
| 294 | __('Display alert messages on expired tasks').'</label></p>'. |
|---|
| 295 | |
|---|
| 296 | '<p><label for="settings_recall_time">'.__('Recall time for all tasks:').'</label>'. |
|---|
| 297 | form::combo('settings_recall_time', $full_combo_ts, 'seperate', 'recall-for-all'). |
|---|
| 298 | '</p>'. |
|---|
| 299 | |
|---|
| 300 | '<p>'.__('Recall time per task:').'</p>'; |
|---|
| 301 | |
|---|
| 302 | foreach($maintenance->getTasks($core) as $t) |
|---|
| 303 | { |
|---|
| 304 | echo |
|---|
| 305 | '<div class="two-boxes">'. |
|---|
| 306 | |
|---|
| 307 | '<p><label for="settings_ts_'.$t->id().'">'.$t->task().'</label>'. |
|---|
| 308 | form::combo('settings_ts_'.$t->id(), $task_combo_ts, $t->ts(), 'recall-per-task'). |
|---|
| 309 | '</p>'. |
|---|
| 310 | |
|---|
| 311 | '</div>'; |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | echo |
|---|
| 315 | '<p><input type="submit" value="'.__('Save').'" /> '. |
|---|
| 316 | form::hidden(array('tab'), 'settings'). |
|---|
| 317 | form::hidden(array('settings'), 1). |
|---|
| 318 | $core->formNonce().'</p>'. |
|---|
| 319 | '</form>'. |
|---|
| 320 | '<p class="info">'.sprintf( |
|---|
| 321 | __('You can place list of expired tasks on your %s.'), |
|---|
| 322 | '<a href="preferences.php#user-favorites">'.__('Dashboard').'</a>' |
|---|
| 323 | ).'</a></p>'. |
|---|
| 324 | '</div>'; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | dcPage::helpBlock('maintenance'); |
|---|
| 328 | |
|---|
| 329 | echo '</body></html>'; |
|---|