~ubuntu-branches/ubuntu/saucy/horde3/saucy

« back to all changes in this revision

Viewing changes to lib/Horde/Maintenance.php

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-05-04 23:08:08 UTC
  • Revision ID: james.westby@ubuntu.com-20050504230808-p4hf3hk28o3v7wir
Tags: upstream-3.0.4
ImportĀ upstreamĀ versionĀ 3.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/** @constant MAINTENANCE_YEARLY Do task yearly (First login after/on January 1). */
 
4
define('MAINTENANCE_YEARLY', 1);
 
5
 
 
6
/** @constant MAINTENANCE_MONTHLY Do task monthly (First login after/on first of month). */
 
7
define('MAINTENANCE_MONTHLY', 2);
 
8
 
 
9
/** @constant MAINTENANCE_WEEKLY Do task weekly (First login after/on a Sunday). */
 
10
define('MAINTENANCE_WEEKLY', 3);
 
11
 
 
12
/** @constant MAINTENANCE_DAILY Do task daily (First login of the day). */
 
13
define('MAINTENANCE_DAILY', 4);
 
14
 
 
15
/** @constant MAINTENANCE_EVERY Do task every login. */
 
16
define('MAINTENANCE_EVERY', 5);
 
17
 
 
18
/** @constant MAINTENANCE_FIRST_LOGIN Do task on first login only. */
 
19
define('MAINTENANCE_FIRST_LOGIN', 6);
 
20
 
 
21
/** @constant MAINTENANCE_OUTPUT_CONFIRM Confirmation-style output for maintenance page. */
 
22
define('MAINTENANCE_OUTPUT_CONFIRM', 7);
 
23
 
 
24
/** @constant MAINTENANCE_OUTPUT_AGREE Agreement-style output for maintenance page. */
 
25
define('MAINTENANCE_OUTPUT_AGREE', 8);
 
26
 
 
27
/** @constant MAINTENANCE_OUTPUT_NOTICE Notice-style output for maintenance page. */
 
28
define('MAINTENANCE_OUTPUT_NOTICE', 9);
 
29
 
 
30
/** @constant MAINTENANCE_DONE_PARAM The name of the URL parameter that indicates that the maintenance tasks are completed. */
 
31
define('MAINTENANCE_DONE_PARAM', 'maintenance_done');
 
32
 
 
33
/* Intervals hash - used to build select tables in preferences menu. */
 
34
$intervals = array();
 
35
$intervals[MAINTENANCE_YEARLY]  = _("Yearly");
 
36
$intervals[MAINTENANCE_MONTHLY] = _("Monthly");
 
37
$intervals[MAINTENANCE_WEEKLY]  = _("Weekly");
 
38
$intervals[MAINTENANCE_DAILY]   = _("Daily");
 
39
$intervals[MAINTENANCE_EVERY]   = _("Every Login");
 
40
$GLOBALS['intervals'] = &$intervals;
 
41
 
 
42
/**
 
43
 * The Maintenance:: class provides a set of methods for dealing with
 
44
 * maintenance operations run upon login to Horde applications.
 
45
 *
 
46
 * $Horde: framework/Maintenance/Maintenance.php,v 1.52.10.2 2005/01/03 12:19:06 jan Exp $
 
47
 *
 
48
 * Copyright 2001-2005 Michael Slusarz <slusarz@bigworm.colorado.edu>
 
49
 *
 
50
 * See the enclosed file COPYING for license information (LGPL).  If you
 
51
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 
52
 *
 
53
 * @author  Michael Slusarz <slusarz@bigworm.colorado.edu>
 
54
 * @version $Revision: 1.52.10.2 $
 
55
 * @since   Horde 1.3.5
 
56
 * @package Horde_Maintenance
 
57
 */
 
58
class Maintenance {
 
59
 
 
60
    /**
 
61
     * Hash holding maintenance preference names.
 
62
     * Syntax:  PREFNAME => interval
 
63
     * Valid intervals are: MAINTENANCE_YEARLY, MAINTENANCE_MONTHLY,
 
64
     *                      MAINTENANCE_WEEKLY, MAINTENANCE_DAILY,
 
65
     *                      MAINTENANCE_EVERY,  MAINTENANCE_FIRST_LOGIN
 
66
     * Operations will be run in the order they appear in the array -
 
67
     *   MAKE SURE FUNCTIONS ARE IN THE CORRECT ORDER!
 
68
     * Operations can appear more than once - they will only be run once per
 
69
     *   login though (the operation will run the first time it is seen in
 
70
     *   the array).
 
71
     *
 
72
     * This array should be filled in for each Horde module that extends
 
73
     *   the Maintenance class.
 
74
     *
 
75
     * @var array $maint_tasks
 
76
     */
 
77
    var $maint_tasks = array();
 
78
 
 
79
    /**
 
80
     * UNIX timestamp of the last maintenance run for user.
 
81
     *
 
82
     * @var integer $_lastRun
 
83
     */
 
84
    var $_lastRun = 0;
 
85
 
 
86
    /**
 
87
     * The Maintenance_Tasklist object for this login.
 
88
     *
 
89
     * @var object Maintenance_Tasklist $_tasklist
 
90
     */
 
91
    var $_tasklist;
 
92
 
 
93
    /**
 
94
     * Array to store Maintenance_Task objects.
 
95
     *
 
96
     * @var array $_modulesCache
 
97
     */
 
98
    var $_taskCache = array();
 
99
 
 
100
    /**
 
101
     * Attempts to return a concrete Maintenance_* object based on the
 
102
     * module name passed into it.
 
103
     *
 
104
     * @access public
 
105
     *
 
106
     * @param string $module           The name of the Horde module.
 
107
     * @param optional array $params   A hash containing additional data
 
108
     *                                 needed by the constructor.
 
109
     *
 
110
     * @return object Maintenance  The Maintenance object.
 
111
     *                             Returns false on error.
 
112
     */
 
113
    function &factory($module, $params = array())
 
114
    {
 
115
        global $registry;
 
116
 
 
117
        /* Spawn the relevant driver, and return it (or false on failure). */
 
118
        include_once $registry->get('fileroot', $module) . '/lib/Maintenance/' . $module . '.php';
 
119
        $class = 'Maintenance_' . $module;
 
120
        if (class_exists($class)) {
 
121
            return $ret = &new $class($params);
 
122
        } else {
 
123
            return false;
 
124
        }
 
125
    }
 
126
 
 
127
    /**
 
128
     * Constructor.
 
129
     *
 
130
     * @access public
 
131
     *
 
132
     * @param array $params  A hash containing the following entries:
 
133
     *                       'last_maintenance' => The last time maintenance was run (unix timestamp).
 
134
     */
 
135
    function Maintenance($params = array())
 
136
    {
 
137
        /* Set the class variable $_lastRun. */
 
138
        if (isset($params['last_maintenance'])) {
 
139
            $this->_lastRun = $params['last_maintenance'];
 
140
        }
 
141
 
 
142
        $this->_retrieveTasklist();
 
143
        $this->_shutdown();
 
144
    }
 
145
 
 
146
    /**
 
147
     * Do maintenance operations needed for this login.  This function
 
148
     * will generate the list of tasks to perform during this login
 
149
     * and will redirect to the maintenance page if necessary.  This
 
150
     * is the function that should be called from the application upon
 
151
     * login.
 
152
     *
 
153
     * @access public
 
154
     */
 
155
    function runMaintenance()
 
156
    {
 
157
        /* Check to see if we are finished with maintenance
 
158
         * operations. */
 
159
        if (!Util::getFormData(MAINTENANCE_DONE_PARAM)) {
 
160
            /* Determine if we should redirect to the maintenance page. */
 
161
            if ($this->_needMaintenancePage() !== null) {
 
162
                header('Location: ' . $this->_getMaintenanceURL(), true);
 
163
                exit;
 
164
            }
 
165
        }
 
166
 
 
167
        /* Finally, run any tasks that need to be executed. */
 
168
        $this->_doMaintenanceTasks();
 
169
    }
 
170
 
 
171
    /**
 
172
     * Do the necessary maintenance tasks for this loading of the
 
173
     * maintenance page.  This is the function that is called from the
 
174
     * maintenance page every time it is loaded.
 
175
     *
 
176
     * @access public
 
177
     *
 
178
     * @return integer  The display required for the maintenance page.
 
179
     */
 
180
    function runMaintenancePage()
 
181
    {
 
182
        /* Should we run any tasks? */
 
183
        $this->_doMaintenanceTasks();
 
184
 
 
185
        /* Get the list of tasks we need to display to the user. */
 
186
        $task_no = $this->_needMaintenancePage();
 
187
        $tasks = $this->_tasklist->getList();
 
188
 
 
189
        /* Remove 'newflag' from first task. */
 
190
        if (!$this->_tasklist->processed(true)) {
 
191
            if (count($tasks)) {
 
192
                reset($tasks);
 
193
                $this->_tasklist->unsetNewPage(key($tasks));
 
194
            }
 
195
        }
 
196
 
 
197
        if (!is_null($task_no)) {
 
198
            $tasks = array_slice($tasks, 0, $task_no + 1);
 
199
        }
 
200
 
 
201
        if (count($tasks)) {
 
202
            reset($tasks);
 
203
            $action = $tasks[key($tasks)]['display'];
 
204
        } else {
 
205
            $action = null;
 
206
        }
 
207
 
 
208
        return array($action, array_keys($tasks));
 
209
    }
 
210
 
 
211
    /**
 
212
     * Returns the informational text message on what the operation is
 
213
     * about to do. Also indicates whether the box should be checked
 
214
     * by default or not. Operations that have been locked by the
 
215
     * admin will return null.
 
216
     *
 
217
     * @access public
 
218
     *
 
219
     * @param string $pref  Name of the operation to get information for.
 
220
     *
 
221
     * @return array  1st element - Description of what the operation is about
 
222
     *                              to do during this login.
 
223
     *                2nd element - Whether the preference is set to on or not.
 
224
     */
 
225
    function infoMaintenance($pref)
 
226
    {
 
227
        global $prefs;
 
228
 
 
229
        /* If the preference has been locked by the admin, do not show
 
230
           the user. */
 
231
        if ($prefs->isLocked($pref)) {
 
232
            return;
 
233
        }
 
234
 
 
235
        $mod = &$this->_loadModule($pref);
 
236
        return array($mod->describeMaintenance(), $prefs->getValue($pref));
 
237
    }
 
238
 
 
239
    /**
 
240
     * Export variable names to use for creating select tables in the
 
241
     * preferences menu.
 
242
     *
 
243
     * @access public
 
244
     *
 
245
     * @return array  An array of variable names to be imported into the
 
246
     *                prefs.php namespace.
 
247
     */
 
248
    function exportIntervalPrefs()
 
249
    {
 
250
        global $prefs;
 
251
 
 
252
        $return_array = array();
 
253
 
 
254
        foreach (array_keys($this->maint_tasks) as $val) {
 
255
            if (!$prefs->isLocked($val . '_interval')) {
 
256
                $return_array[] = $val . '_interval_options';
 
257
            }
 
258
        }
 
259
 
 
260
        return $return_array;
 
261
    }
 
262
 
 
263
    /**
 
264
     * Output hidden for elements for the POST form to ensure the
 
265
     * calling script has the same POST elements as when the
 
266
     * maintenance operations first run.
 
267
     *
 
268
     * @access public
 
269
     *
 
270
     * @return string  The form data.
 
271
     */
 
272
    function getPostData()
 
273
    {
 
274
        $text = '';
 
275
 
 
276
        if (($data = $this->_tasklist->getPostData())) {
 
277
            foreach ($data as $name => $val) {
 
278
                $text .= '<input type="hidden" name="' . htmlspecialchars($name) . '" value="' . htmlspecialchars($val) . '" />' . "\n";
 
279
            }
 
280
        }
 
281
 
 
282
        return $text;
 
283
    }
 
284
 
 
285
    /**
 
286
     * Return the URL needed for the maintenance form.
 
287
     *
 
288
     * @access public
 
289
     *
 
290
     * @return string  The URL to redirect to.
 
291
     */
 
292
    function getMaintenanceFormURL()
 
293
    {
 
294
       if ($this->_needMaintenancePage() !== null) {
 
295
           return $this->_getMaintenanceURL();
 
296
       } else {
 
297
           return $this->_getInitialPageURL();
 
298
       }
 
299
    }
 
300
 
 
301
    /**
 
302
     * Creates the list of maintenance operations that are available
 
303
     * for this session (stored in a Maintenance_Tasklist object).
 
304
     *
 
305
     * @access private
 
306
     *
 
307
     * @return boolean  Returns true if list was created.
 
308
     *                  False if not (e.g. list already exists).
 
309
     */
 
310
    function _createTaskList()
 
311
    {
 
312
        global $prefs;
 
313
 
 
314
        /* Create a new Maintenance_Tasklist object. */
 
315
        $this->_tasklist = &new Maintenance_Tasklist();
 
316
 
 
317
        /* Create time objects for today's date and last login date. */
 
318
        $last_date = getdate($this->_lastRun);
 
319
        $cur_date  = getdate();
 
320
 
 
321
        /* Go through each item in $maint_tasks and determine if we need to
 
322
           run it during this maintenance run. */
 
323
        foreach ($this->maint_tasks as $key => $val) {
 
324
            /* Skip item if it already appears in the tasks list. */
 
325
            if ($this->_tasklist->inList($key)) {
 
326
                continue;
 
327
            }
 
328
 
 
329
            /* Determine the correct interval for the item. */
 
330
            if (($interval = $prefs->getValue($key . '_interval'))) {
 
331
                $val = $interval;
 
332
            }
 
333
 
 
334
            $addTask = false;
 
335
 
 
336
            /* FIRST LOGIN OPERATIONS */
 
337
            /* If $_lastRun is empty (= 0), this is the first time the user
 
338
               has logged in. Don't run any other maintenance operations on
 
339
               the first login. */
 
340
            if (empty($this->_lastRun)) {
 
341
                if ($val == MAINTENANCE_FIRST_LOGIN) {
 
342
                    $addTask = true;
 
343
                }
 
344
            }
 
345
 
 
346
            /* YEARLY_OPERATIONS */
 
347
            elseif (($val == MAINTENANCE_YEARLY) &&
 
348
                    ($cur_date['year'] > $last_date['year'])) {
 
349
                $addTask = true;
 
350
            }
 
351
 
 
352
            /* MONTHLY OPERATIONS */
 
353
            elseif (($val == MAINTENANCE_MONTHLY) &&
 
354
                    (($cur_date['year'] > $last_date['year']) || ($cur_date['mon'] > $last_date['mon']))) {
 
355
                $addTask = true;
 
356
            }
 
357
 
 
358
            /* WEEKLY OPERATIONS */
 
359
            elseif (($val == MAINTENANCE_WEEKLY) &&
 
360
                    (($cur_date['wday'] < $last_date['wday']) || ((time() - 604800) > $this->_lastRun))) {
 
361
                $addTask = true;
 
362
            }
 
363
 
 
364
            /* DAILY OPERATIONS */
 
365
            elseif (($val == MAINTENANCE_DAILY) &&
 
366
                    (($cur_date['year'] > $last_date['year']) || ($cur_date['yday'] > $last_date['yday']))) {
 
367
                $addTask = true;
 
368
            }
 
369
 
 
370
            /* EVERY LOGIN OPERATIONS */
 
371
            elseif ($val == MAINTENANCE_EVERY) {
 
372
                $addTask = true;
 
373
            }
 
374
            /* Skip the task if any of the following:
 
375
               + This task does not need to be run in this login
 
376
               + This task is not set in the preferences */
 
377
            if (!$addTask || !$prefs->getValue($key)) continue;
 
378
 
 
379
            /* Load the task module now. */
 
380
            $mod = &$this->_loadModule($key);
 
381
 
 
382
            /* Determine if this task has already been confirmed/set via some
 
383
               sort of admin setting. Also, if the user/admin has set the
 
384
               'confirm_maintenance' flag, skip confirmation. */
 
385
            $confirmed = false;
 
386
            if ($prefs->isLocked($key) ||
 
387
                !$prefs->getValue('confirm_maintenance')) {
 
388
                $confirmed = true;
 
389
            }
 
390
 
 
391
            /* Add the task to the tasklist. */
 
392
            $this->_tasklist->addTask($key, $confirmed, $mod->getDisplayType());
 
393
        }
 
394
    }
 
395
 
 
396
    /**
 
397
     * Load module (if not already loaded).
 
398
     *
 
399
     * @access private
 
400
     *
 
401
     * @param string $modname  Name of the module to load.
 
402
     *
 
403
     * @return object Maintenance_Task  A reference to the requested module.
 
404
     */
 
405
    function &_loadModule($modname)
 
406
    {
 
407
        global $registry;
 
408
 
 
409
        if (!isset($this->_taskCache[$modname])) {
 
410
            include_once $registry->get('fileroot', $this->_tasklist->getModule()) . '/lib/Maintenance/Task/' . $modname . '.php';
 
411
            $class = 'Maintenance_Task_' . $modname;
 
412
            if (class_exists($class)) {
 
413
                $this->_taskCache[$modname] = &new $class;
 
414
            } else {
 
415
                Horde::fatal(PEAR::raiseError(sprintf(_("Could not open Maintenance_Task module %s"), $class)), __FILE__, __LINE__);
 
416
            }
 
417
        }
 
418
 
 
419
        return $this->_taskCache[$modname];
 
420
    }
 
421
 
 
422
    /**
 
423
     * Get the URL for the initial page.
 
424
     *
 
425
     * @access private
 
426
     *
 
427
     * @return string  The URL for the initial page.
 
428
     */
 
429
    function _getInitialPageURL()
 
430
    {
 
431
        return Util::addParameter($this->_tasklist->getTarget(), MAINTENANCE_DONE_PARAM, 1);
 
432
    }
 
433
 
 
434
    /**
 
435
     * Get the URL for the maintenance page.
 
436
     *
 
437
     * @access private
 
438
     *
 
439
     * @return string  The URL for the maintenance page.
 
440
     */
 
441
    function _getMaintenanceURL()
 
442
    {
 
443
        global $registry;
 
444
 
 
445
        /* We need to pass the cache ID and 'module' parameters. */
 
446
        $url = Horde::url($registry->get('webroot', 'horde') . '/services/maintenance.php', true);
 
447
        $url = Util::addParameter($url, 'module', $this->_tasklist->getModule(), false);
 
448
 
 
449
        return $url;
 
450
    }
 
451
 
 
452
    /**
 
453
     * Register the shutdown function for storing the maintenance
 
454
     * tasklist.
 
455
     *
 
456
     * @access private
 
457
     */
 
458
    function _shutdown()
 
459
    {
 
460
        register_shutdown_function(array(&$this, '_cacheTasklist'));
 
461
    }
 
462
 
 
463
    /**
 
464
     * Cache the maintenance tasklist between page requests.
 
465
     *
 
466
     * @access private
 
467
     */
 
468
    function _cacheTasklist()
 
469
    {
 
470
        $_SESSION['horde_maintenance_tasklist'] = serialize($this->_tasklist);
 
471
    }
 
472
 
 
473
    /**
 
474
     * Retrieves a cached maintenance tasklist or makes sure one is
 
475
     * created.
 
476
     *
 
477
     * @access private
 
478
     */
 
479
    function _retrieveTasklist()
 
480
    {
 
481
        if (isset($_SESSION['horde_maintenance_tasklist'])) {
 
482
            $this->_tasklist = unserialize($_SESSION['horde_maintenance_tasklist']);
 
483
        } else {
 
484
            $this->_createTasklist();
 
485
        }
 
486
    }
 
487
 
 
488
    /**
 
489
     * Execute all confirmed tasks.
 
490
     *
 
491
     * @access private
 
492
     */
 
493
    function _doMaintenanceTasks()
 
494
    {
 
495
        $tasks = $this->_tasklist->getList();
 
496
 
 
497
        foreach ($tasks as $key => $val) {
 
498
            if ($val['newpage']) {
 
499
                if ($this->_tasklist->processed()) {
 
500
                    $this->_tasklist->unsetNewPage($key);
 
501
                }
 
502
                break;
 
503
            } elseif ($val['confirmed'] ||
 
504
                      Util::getFormData($key . '_confirm')) {
 
505
                /* Perform maintenance if confirmed. */
 
506
                $mod = &$this->_loadModule($key);
 
507
                $mod->doMaintenance();
 
508
            }
 
509
            $this->_tasklist->removeTask($key);
 
510
        }
 
511
 
 
512
        /* If we've successfully completed every task in the list (or
 
513
         * skipped it), record now as the last time maintenance was
 
514
         * run. */
 
515
        if (!count($this->_tasklist->getList())) {
 
516
            $GLOBALS['prefs']->setValue('last_maintenance', time());
 
517
        }
 
518
    }
 
519
 
 
520
    /**
 
521
     * Do any of the tasks require the maintenance page?
 
522
     *
 
523
     * @access private
 
524
     *
 
525
     * @return integer  The key of the task that requires the maintenance
 
526
     *                  page. Returns null if the maintenance page is no
 
527
     *                  longer needed.
 
528
     */
 
529
    function _needMaintenancePage()
 
530
    {
 
531
        $i = 0;
 
532
        foreach ($this->_tasklist->getList() as $val) {
 
533
            if ($val['newpage']) {
 
534
                return $i;
 
535
            }
 
536
            $i++;
 
537
        }
 
538
 
 
539
        return null;
 
540
    }
 
541
 
 
542
}
 
543
 
 
544
/**
 
545
 * The Maintenance_Tasklist:: class is used to store the list of maintenance
 
546
 * tasks that need to be run during this login.
 
547
 *
 
548
 * Copyright 2002-2005 Michael Slusarz <slusarz@bigworm.colorado.edu>
 
549
 *
 
550
 * See the enclosed file COPYING for license information (LGPL).  If you
 
551
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 
552
 *
 
553
 * @author  Michael Slusarz <slusarz@bigworm.colorado.edu>
 
554
 * @version $Revision: 1.52.10.2 $
 
555
 * @since   Horde 3.0
 
556
 * @package Horde_Maintenance
 
557
 */
 
558
class Maintenance_Tasklist {
 
559
 
 
560
    /**
 
561
     * The Horde module running the maintenance tasks.
 
562
     *
 
563
     * @var string $_module
 
564
     */
 
565
    var $_module;
 
566
 
 
567
    /**
 
568
     * The URL of the web page to load after maintenance is complete.
 
569
     *
 
570
     * @var string $_target
 
571
     */
 
572
    var $_target;
 
573
 
 
574
    /**
 
575
     * POST data for the calling script.
 
576
     *
 
577
     * @var array $_postdata
 
578
     */
 
579
    var $_postdata;
 
580
 
 
581
    /**
 
582
     * The list of tasks to run during this login.
 
583
     *
 
584
     * KEY:    Task name
 
585
     * VALUE:  Array => (
 
586
     *           'confirmed'  =>  boolean,
 
587
     *           'display'    =>  integer,
 
588
     *           'newpage'    =>  boolean
 
589
     *         )
 
590
     *
 
591
     * @var array $_tasks
 
592
     */
 
593
    var $_tasks = array();
 
594
 
 
595
    /**
 
596
     * Internal flag for addTask().
 
597
     *
 
598
     * @var boolean $_addFlag
 
599
     */
 
600
    var $_addFlag = false;
 
601
 
 
602
    /**
 
603
     * Has the tasklist been processed yet?
 
604
     *
 
605
     * @var boolean $_processed
 
606
     */
 
607
    var $_processed = false;
 
608
 
 
609
    /**
 
610
     * Constructor.
 
611
     *
 
612
     * @access public
 
613
     */
 
614
    function Maintenance_Tasklist()
 
615
    {
 
616
        global $registry;
 
617
 
 
618
        $this->_module = $registry->getApp();
 
619
        $this->_target = Horde::selfURL(true);
 
620
    }
 
621
 
 
622
    /**
 
623
     * Adds a task to the tasklist.
 
624
     *
 
625
     * @access public
 
626
     *
 
627
     * @param string $key         The name of the task to perform.
 
628
     * @param boolean $confirmed  Has the task been confirmed?
 
629
     * @param integer $display    The display type of the task.
 
630
     */
 
631
    function addTask($key, $confirmed, $display)
 
632
    {
 
633
        $this->_tasks[$key] = array();
 
634
        $this->_tasks[$key]['confirmed'] = $confirmed;
 
635
        $this->_tasks[$key]['display'] = $display;
 
636
 
 
637
        if (($display == MAINTENANCE_OUTPUT_AGREE) ||
 
638
            ($display == MAINTENANCE_OUTPUT_NOTICE)) {
 
639
            $this->_tasks[$key]['newpage'] = true;
 
640
            $this->_addFlag = false;
 
641
        } elseif (($confirmed == false) && (!$this->_addFlag)) {
 
642
            $this->_tasks[$key]['newpage'] = true;
 
643
            $this->_addFlag = true;
 
644
        } else {
 
645
            $this->_tasks[$key]['newpage'] = false;
 
646
        }
 
647
    }
 
648
 
 
649
    /**
 
650
     * Unsets the newpage flag for a task.
 
651
     *
 
652
     * @access public
 
653
     *
 
654
     * @param string $task  The name of the task to alter.
 
655
     */
 
656
    function unsetNewPage($task)
 
657
    {
 
658
        if ($this->inList($task)) {
 
659
            $this->_tasks[$task]['newpage'] = false;
 
660
        }
 
661
    }
 
662
 
 
663
    /**
 
664
     * Removes the task from the tasklist.
 
665
     *
 
666
     * @access public
 
667
     *
 
668
     * @param string $task  The name of the task to alter.
 
669
     */
 
670
    function removeTask($task)
 
671
    {
 
672
        if ($this->inList($task)) {
 
673
            unset($this->_tasks[$task]);
 
674
        }
 
675
    }
 
676
 
 
677
    /**
 
678
     * Is this task already in the tasklist?
 
679
     *
 
680
     * @access public
 
681
     *
 
682
     * @param string $task  The name of the task.
 
683
     *
 
684
     * @return boolean  Whether the task is already in the tasklist.
 
685
     */
 
686
    function inList($task)
 
687
    {
 
688
        return isset($this->_tasks[$task]);
 
689
    }
 
690
 
 
691
    /**
 
692
     * Return the list of tasks.
 
693
     *
 
694
     * @access public
 
695
     *
 
696
     * @return array  The list of tasks that still need to be done.
 
697
     */
 
698
    function getList()
 
699
    {
 
700
        return $this->_tasks;
 
701
    }
 
702
 
 
703
    /**
 
704
     * Return the Horde module the tasks are running under.
 
705
     *
 
706
     * @access public
 
707
     *
 
708
     * @return string  The Horde module name.
 
709
     */
 
710
    function getModule()
 
711
    {
 
712
        return $this->_module;
 
713
    }
 
714
 
 
715
    /**
 
716
     * Return the POST data.
 
717
     *
 
718
     * @access public
 
719
     *
 
720
     * @return array  The POST data from the initial URL.
 
721
     */
 
722
    function getPostData()
 
723
    {
 
724
        return $this->_postdata;
 
725
    }
 
726
 
 
727
    /**
 
728
     * Return the URL of the web page to load after maintenance is complete.
 
729
     *
 
730
     * @access public
 
731
     *
 
732
     * @return string  The target URL.
 
733
     */
 
734
    function getTarget()
 
735
    {
 
736
        return $this->_target;
 
737
    }
 
738
 
 
739
    /**
 
740
     * Sets/displays the flag to show that tasklist has been processed at
 
741
     * least once.
 
742
     *
 
743
     * @access public
 
744
     *
 
745
     * @param optional boolean $set  Set the flag?
 
746
     *
 
747
     * @return boolean  Has the tasklist been processed before?
 
748
     */
 
749
    function processed($set = false)
 
750
    {
 
751
        $retvalue = $this->_processed;
 
752
        if ($set) {
 
753
            $this->_processed = true;
 
754
        }
 
755
        return $retvalue;
 
756
    }
 
757
 
 
758
}
 
759
 
 
760
/**
 
761
 * Abstract class to allow for modularization of specific maintenace tasks.
 
762
 *
 
763
 * For this explanation, the specific Horde application you want to create
 
764
 * maintenance actions for will be labeled HORDEAPP.
 
765
 *
 
766
 * To add a new maintenance task, you need to do the following:
 
767
 * [1] Add preference to "HORDEAPP/config/prefs.php" file.
 
768
 *     (The name of this preference will be referred to as PREFNAME)
 
769
 *     This preference should be of type 'checkbox' (i.e. 1 = on; 0 = off).
 
770
 *     [Optional:]  Add a preference in prefs.php of the name
 
771
 *                  'PREFNAME_interval' to allow the user to set the interval.
 
772
 *                  'default' value should be set to the values of the interval
 
773
 *                  constants above.
 
774
 *                  If this preference doesn't exist, the default interval
 
775
 *                  used will be the one that appears in $maint_tasks.
 
776
 * [2] Create a directory named "HORDEAPP/lib/Maintenance".
 
777
 * [3] Create a class entitled Maintenance_HORDEAPP that extends the
 
778
 *     Maintenance class.
 
779
 *     This class should contain only the application specific definitions of
 
780
 *     $maint_tasks (see above for description).
 
781
 *     Save this file as "HORDEAPP/lib/Maintenance/HORDEAPP.php".
 
782
 * [4] Create a directory titled "HORDEAPP/lib/Maintenance/Task".
 
783
 * [5] Create modules in HORDEAPP/lib/Maintenance/Task named 'PREFNAME.php'
 
784
 *     that extend the Maintenance_Task class.
 
785
 *     The class should be named Maintenance_Task_PREFNAME.
 
786
 *     The class should declare the following two methods:
 
787
 *       'doMaintenance' - This is the function that is run to do the
 
788
 *                         specified maintenance operation.
 
789
 *       'describeMaintenance' - This function sets the preference text
 
790
 *                               and text to be used on the confirmation
 
791
 *                               page.  Should return a description of what
 
792
 *                               your 'doMaintenance' function is about to do.
 
793
 *     Neither function requires any parameters passed in.
 
794
 *
 
795
 * There are 3 different types of maintenance (set via $_display_type):
 
796
 * [1] MAINTENANCE_OUTPUT_CONFIRM
 
797
 *     Each output from describeMaintenance() will have a checkbox associated
 
798
 *     with it. For each checkbox selected, doMaintenance() for that task will
 
799
 *     be run. More than 1 confirmation message can be displayed on the
 
800
 *     maintenance page at once.
 
801
 *
 
802
 * [2] MAINTENANCE_OUTPUT_AGREE
 
803
 *     The output from describeMaintenance() should be text asking the user to
 
804
 *     agree/disagree to specified terms. If 'yes' is selected, the POST
 
805
 *     variable 'agree' will be set. If 'no' is selected, the POST variable
 
806
 *     'not_agree' will be set. In either case, doMaintenance() will ALWAYS be
 
807
 *     run.
 
808
 *     * This style will be displayed on its own, separate maintenance page. *
 
809
 *
 
810
 * [3] MAINTENANCE_OUTPUT_NOTICE
 
811
 *     The output from describeMaintenance() should be any non-interactive text
 
812
 *     desired. There will be a single 'Click to Continue' button below this
 
813
 *     text. doMaintenance() will ALWAYS be run.
 
814
 *     * This style will be displayed on its own, separate maintenance page. *
 
815
 *
 
816
 * Copyright 2001-2005 Michael Slusarz <slusarz@bigworm.colorado.edu>
 
817
 *
 
818
 * See the enclosed file COPYING for license information (LGPL).  If you
 
819
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 
820
 *
 
821
 * @author  Michael Slusarz <slusarz@bigworm.colorado.edu>
 
822
 * @version $Revision: 1.52.10.2 $
 
823
 * @since   Horde 1.3.5
 
824
 * @package Horde_Maintenance
 
825
 */
 
826
class Maintenance_Task {
 
827
 
 
828
    /**
 
829
     * The style of the maintenance page output.
 
830
     * Possible values: MAINTENANCE_OUTPUT_CONFIRM,
 
831
     *                  MAINTENANCE_OUTPUT_AGREE,
 
832
     *                  MAINTENANCE_OUTPUT_NOTICE
 
833
     *
 
834
     * @var integer $_display_type
 
835
     */
 
836
    var $_display_type = MAINTENANCE_OUTPUT_CONFIRM;
 
837
 
 
838
    /**
 
839
     * Constructor
 
840
     *
 
841
     * @access public
 
842
     */
 
843
    function Maintenance_Task()
 
844
    {
 
845
    }
 
846
 
 
847
    /**
 
848
     * Do maintenance operation (if it has been confirmed).
 
849
     *
 
850
     * @access public
 
851
     *
 
852
     * @return boolean  Whether the maintenance operation was successful or
 
853
     *                  not.
 
854
     */
 
855
    function doMaintenance()
 
856
    {
 
857
        return false;
 
858
    }
 
859
 
 
860
    /**
 
861
     * Return description information for the maintenance page.
 
862
     *
 
863
     * @access public
 
864
     *
 
865
     * @return string  Description that will be displayed on the maintenance
 
866
     *                 confirmation page.
 
867
     */
 
868
    function describeMaintenance()
 
869
    {
 
870
        return '';
 
871
    }
 
872
 
 
873
    /**
 
874
     * Returns the desired output type for the maintenance page.
 
875
     *
 
876
     * @access public
 
877
     *
 
878
     * @return integer  Desired output type for the maintenance confirmation
 
879
     *                  page.
 
880
     */
 
881
    function getDisplayType()
 
882
    {
 
883
        return $this->_display_type;
 
884
    }
 
885
 
 
886
}