| 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 | // Set env |
|---|
| 15 | |
|---|
| 16 | $core->blog->settings->addNamespace('maintenance'); |
|---|
| 17 | |
|---|
| 18 | $maintenance = new dcMaintenance($core); |
|---|
| 19 | $tasks = $maintenance->getTasks(); |
|---|
| 20 | |
|---|
| 21 | $headers = ''; |
|---|
| 22 | $p_url = $core->adminurl->get('admin.plugin.maintenance'); |
|---|
| 23 | $task = null; |
|---|
| 24 | $expired = array(); |
|---|
| 25 | |
|---|
| 26 | $code = empty($_POST['code']) ? null : (integer) $_POST['code']; |
|---|
| 27 | $tab = empty($_REQUEST['tab']) ? '' : $_REQUEST['tab']; |
|---|
| 28 | |
|---|
| 29 | // Get task object |
|---|
| 30 | |
|---|
| 31 | if (!empty($_REQUEST['task'])) { |
|---|
| 32 | $task = $maintenance->getTask($_REQUEST['task']); |
|---|
| 33 | |
|---|
| 34 | if ($task === null) { |
|---|
| 35 | $core->error->add('Unknow task ID'); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | $task->code($code); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | // Execute task |
|---|
| 42 | |
|---|
| 43 | if ($task && !empty($_POST['task']) && $task->id() == $_POST['task']) { |
|---|
| 44 | try { |
|---|
| 45 | $code = $task->execute(); |
|---|
| 46 | if (false === $code) { |
|---|
| 47 | throw new Exception($task->error()); |
|---|
| 48 | } |
|---|
| 49 | if (true === $code) { |
|---|
| 50 | $maintenance->setLog($task->id()); |
|---|
| 51 | |
|---|
| 52 | dcPage::addSuccessNotice($task->success()); |
|---|
| 53 | http::redirect($p_url.'&task='.$task->id().'&tab='.$tab.'#'.$tab); |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | catch (Exception $e) { |
|---|
| 57 | $core->error->add($e->getMessage()); |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | // Save settings |
|---|
| 62 | |
|---|
| 63 | if (!empty($_POST['save_settings'])) { |
|---|
| 64 | |
|---|
| 65 | try { |
|---|
| 66 | $core->blog->settings->maintenance->put( |
|---|
| 67 | 'plugin_message', |
|---|
| 68 | !empty($_POST['settings_plugin_message']), |
|---|
| 69 | 'boolean', |
|---|
| 70 | 'Display alert message of late tasks on plugin page', |
|---|
| 71 | true, |
|---|
| 72 | true |
|---|
| 73 | ); |
|---|
| 74 | |
|---|
| 75 | foreach($tasks as $t) { |
|---|
| 76 | if (!$t->id()) { |
|---|
| 77 | continue; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | if (!empty($_POST['settings_recall_type']) && $_POST['settings_recall_type'] == 'all') { |
|---|
| 81 | $ts = $_POST['settings_recall_time']; |
|---|
| 82 | } |
|---|
| 83 | else { |
|---|
| 84 | $ts = empty($_POST['settings_ts_'.$t->id()]) ? 0 : $_POST['settings_ts_'.$t->id()]; |
|---|
| 85 | } |
|---|
| 86 | $core->blog->settings->maintenance->put( |
|---|
| 87 | 'ts_'.$t->id(), |
|---|
| 88 | abs((integer) $ts), |
|---|
| 89 | 'integer', |
|---|
| 90 | sprintf('Recall time for task %s', $t->id()), |
|---|
| 91 | true, |
|---|
| 92 | $t->blog() |
|---|
| 93 | ); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | dcPage::addSuccessNotice(__('Maintenance plugin has been successfully configured.')); |
|---|
| 97 | http::redirect($p_url.'&tab='.$tab.'#'.$tab); |
|---|
| 98 | } |
|---|
| 99 | catch(Exception $e) { |
|---|
| 100 | $core->error->add($e->getMessage()); |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | // Combos |
|---|
| 105 | |
|---|
| 106 | $combo_ts = array( |
|---|
| 107 | __('Never') => 0, |
|---|
| 108 | __('Every week') => 604800, |
|---|
| 109 | __('Every two weeks') => 1209600, |
|---|
| 110 | __('Every month') => 2592000, |
|---|
| 111 | __('Every two months') => 5184000 |
|---|
| 112 | ); |
|---|
| 113 | |
|---|
| 114 | // Display page |
|---|
| 115 | |
|---|
| 116 | echo '<html><head> |
|---|
| 117 | <title>'.__('Maintenance').'</title>'. |
|---|
| 118 | dcPage::jsPageTabs($tab). |
|---|
| 119 | dcPage::jsLoad(dcPage::getPF('maintenance/js/settings.js')); |
|---|
| 120 | |
|---|
| 121 | if ($task && $task->ajax()) { |
|---|
| 122 | echo |
|---|
| 123 | '<script type="text/javascript">'."\n". |
|---|
| 124 | dcPage::jsVar('dotclear.msg.wait', __('Please wait...')). |
|---|
| 125 | '</script>'. |
|---|
| 126 | dcPage::jsLoad(dcPage::getPF('maintenance/js/dc.maintenance.js')); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | echo |
|---|
| 130 | $maintenance->getHeaders().' |
|---|
| 131 | </head> |
|---|
| 132 | <body>'; |
|---|
| 133 | |
|---|
| 134 | // Check if there is somthing to display according to user permissions |
|---|
| 135 | if (empty($tasks)) { |
|---|
| 136 | echo dcPage::breadcrumb( |
|---|
| 137 | array( |
|---|
| 138 | __('Plugins') => '', |
|---|
| 139 | __('Maintenance') => '' |
|---|
| 140 | ) |
|---|
| 141 | ). |
|---|
| 142 | '<p class="warn">'.__('You have not sufficient permissions to view this page.').'</p>'. |
|---|
| 143 | '</body></html>'; |
|---|
| 144 | |
|---|
| 145 | return null; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | if ($task && ($res = $task->step()) !== null) { |
|---|
| 149 | |
|---|
| 150 | // Page title |
|---|
| 151 | |
|---|
| 152 | echo dcPage::breadcrumb( |
|---|
| 153 | array( |
|---|
| 154 | __('Plugins') => '', |
|---|
| 155 | '<a href="'.$p_url.'">'.__('Maintenance').'</a>' => '', |
|---|
| 156 | html::escapeHTML($task->name())=> '' |
|---|
| 157 | ) |
|---|
| 158 | ).dcPage::notices(); |
|---|
| 159 | |
|---|
| 160 | // content |
|---|
| 161 | if (substr($res, 0, 1) != '<') { |
|---|
| 162 | $res = sprintf('<p class="step-msg">%s</p>', $res); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | // Intermediate task (task required several steps) |
|---|
| 166 | |
|---|
| 167 | echo |
|---|
| 168 | '<div class="step-box" id="'.$task->id().'">'. |
|---|
| 169 | '<p class="step-back">'. |
|---|
| 170 | '<a class="back" href="'.$p_url.'&tab='.$task->tab().'#'.$task->tab().'">'.__('Back').'</a>'. |
|---|
| 171 | '</p>'. |
|---|
| 172 | '<h3>'.html::escapeHTML($task->name()).'</h3>'. |
|---|
| 173 | '<form action="'.$p_url.'" method="post">'. |
|---|
| 174 | $res. |
|---|
| 175 | '<p class="step-submit">'. |
|---|
| 176 | '<input type="submit" value="'.$task->task().'" /> '. |
|---|
| 177 | form::hidden(array('task'), $task->id()). |
|---|
| 178 | form::hidden(array('code'), (integer) $code). |
|---|
| 179 | $core->formNonce(). |
|---|
| 180 | '</p>'. |
|---|
| 181 | '</form>'. |
|---|
| 182 | '</div>'; |
|---|
| 183 | } |
|---|
| 184 | else { |
|---|
| 185 | |
|---|
| 186 | // Page title |
|---|
| 187 | |
|---|
| 188 | echo dcPage::breadcrumb( |
|---|
| 189 | array( |
|---|
| 190 | __('Plugins') => '', |
|---|
| 191 | __('Maintenance') => '' |
|---|
| 192 | ) |
|---|
| 193 | ).dcPage::notices(); |
|---|
| 194 | |
|---|
| 195 | // Simple task (with only a button to start it) |
|---|
| 196 | |
|---|
| 197 | foreach($maintenance->getTabs() as $tab_obj) |
|---|
| 198 | { |
|---|
| 199 | $res_group = ''; |
|---|
| 200 | foreach($maintenance->getGroups() as $group_obj) |
|---|
| 201 | { |
|---|
| 202 | $res_task = ''; |
|---|
| 203 | foreach($tasks as $t) |
|---|
| 204 | { |
|---|
| 205 | if (!$t->id() |
|---|
| 206 | || $t->group() != $group_obj->id() |
|---|
| 207 | || $t->tab() != $tab_obj->id()) { |
|---|
| 208 | continue; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | $res_task .= |
|---|
| 212 | '<p>'.form::radio(array('task', $t->id()), $t->id()).' '. |
|---|
| 213 | '<label class="classic" for="'.$t->id().'">'. |
|---|
| 214 | html::escapeHTML($t->task()).'</label>'; |
|---|
| 215 | |
|---|
| 216 | // Expired task alert message |
|---|
| 217 | $ts = $t->expired(); |
|---|
| 218 | if ($core->blog->settings->maintenance->plugin_message && $ts !== false) { |
|---|
| 219 | if ($ts === null) { |
|---|
| 220 | $res_task .= |
|---|
| 221 | '<br /> <span class="warn">'. |
|---|
| 222 | __('This task has never been executed.').' '. |
|---|
| 223 | __('You should execute it now.').'</span>'; |
|---|
| 224 | } |
|---|
| 225 | else { |
|---|
| 226 | $res_task .= |
|---|
| 227 | '<br /> <span class="warn">'.sprintf( |
|---|
| 228 | __('Last execution of this task was on %s.'), |
|---|
| 229 | dt::str($core->blog->settings->system->date_format, $ts).' '. |
|---|
| 230 | dt::str($core->blog->settings->system->time_format, $ts) |
|---|
| 231 | ).' '. |
|---|
| 232 | __('You should execute it now.').'</span>'; |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | $res_task .= '</p>'; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | if (!empty($res_task)) { |
|---|
| 240 | $res_group .= |
|---|
| 241 | '<div class="fieldset">'. |
|---|
| 242 | '<h4 id="'.$group_obj->id().'">'.$group_obj->name().'</h4>'. |
|---|
| 243 | $res_task. |
|---|
| 244 | '</div>'; |
|---|
| 245 | } |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | if (!empty($res_group)) { |
|---|
| 249 | echo |
|---|
| 250 | '<div id="'.$tab_obj->id().'" class="multi-part" title="'.$tab_obj->name().'">'. |
|---|
| 251 | '<h3>'.$tab_obj->name().'</h3>'. |
|---|
| 252 | // ($tab_obj->option('summary') ? '<p>'.$tab_obj->option('summary').'</p>' : ''). |
|---|
| 253 | '<form action="'.$p_url.'" method="post">'. |
|---|
| 254 | $res_group. |
|---|
| 255 | '<p><input type="submit" value="'.__('Execute task').'" /> '. |
|---|
| 256 | form::hidden(array('tab'), $tab_obj->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($tasks as $t) |
|---|
| 267 | { |
|---|
| 268 | if (!$t->id() || $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="'.__('Alert settings').'">'. |
|---|
| 289 | '<h3>'.__('Alert settings').'</h3>'. |
|---|
| 290 | '<form action="'.$p_url.'" method="post">'. |
|---|
| 291 | |
|---|
| 292 | '<h4 class="pretty-title">'.__('Activation').'</h4>'. |
|---|
| 293 | '<p><label for="settings_plugin_message" class="classic">'. |
|---|
| 294 | form::checkbox('settings_plugin_message', 1, $core->blog->settings->maintenance->plugin_message). |
|---|
| 295 | __('Display alert messages on late tasks').'</label></p>'. |
|---|
| 296 | |
|---|
| 297 | '<p class="info">'.sprintf( |
|---|
| 298 | __('You can place list of late tasks on your %s.'), |
|---|
| 299 | '<a href="'.$core->adminurl->get('admin.user.preferences').'#user-favorites">'.__('Dashboard').'</a>' |
|---|
| 300 | ).'</p>'. |
|---|
| 301 | |
|---|
| 302 | '<h4 class="pretty-title vertical-separator">'.__('Frequency').'</h4>'. |
|---|
| 303 | |
|---|
| 304 | '<p class="vertical-separator">'.form::radio(array('settings_recall_type', 'settings_recall_all'), 'all').' '. |
|---|
| 305 | '<label class="classic" for="settings_recall_all">'. |
|---|
| 306 | '<strong>'.__('Use one recall time for all tasks').'</strong></label></p>'. |
|---|
| 307 | |
|---|
| 308 | '<p class="field wide vertical-separator"><label for="settings_recall_time">'.__('Recall time for all tasks:').'</label>'. |
|---|
| 309 | form::combo('settings_recall_time', $combo_ts, 'seperate', 'recall-for-all'). |
|---|
| 310 | '</p>'. |
|---|
| 311 | |
|---|
| 312 | '<p class="vertical-separator">'.form::radio(array('settings_recall_type', 'settings_recall_separate'), 'separate', 1).' '. |
|---|
| 313 | '<label class="classic" for="settings_recall_separate">'. |
|---|
| 314 | '<strong>'.__('Use one recall time per task').'</strong></label></p>'; |
|---|
| 315 | |
|---|
| 316 | foreach($tasks as $t) |
|---|
| 317 | { |
|---|
| 318 | if (!$t->id()) { |
|---|
| 319 | continue; |
|---|
| 320 | } |
|---|
| 321 | echo |
|---|
| 322 | '<div class="two-boxes">'. |
|---|
| 323 | |
|---|
| 324 | '<p class="field wide"><label for="settings_ts_'.$t->id().'">'.$t->task().'</label>'. |
|---|
| 325 | form::combo('settings_ts_'.$t->id(), $combo_ts, $t->ts(), 'recall-per-task'). |
|---|
| 326 | '</p>'. |
|---|
| 327 | |
|---|
| 328 | '</div>'; |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | echo |
|---|
| 332 | '<p class="field wide"><input type="submit" value="'.__('Save').'" /> '. |
|---|
| 333 | form::hidden(array('tab'), 'settings'). |
|---|
| 334 | form::hidden(array('save_settings'), 1). |
|---|
| 335 | $core->formNonce().'</p>'. |
|---|
| 336 | '</form>'. |
|---|
| 337 | '</div>'; |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | dcPage::helpBlock('maintenance', 'maintenancetasks'); |
|---|
| 341 | |
|---|
| 342 | echo |
|---|
| 343 | '</body></html>'; |
|---|