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