File: session.php

package info (click to toggle)
horde2 2.2.8-1sarge3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,832 kB
  • ctags: 2,897
  • sloc: php: 12,784; sh: 954; sql: 149; makefile: 104; perl: 97; xml: 24; pascal: 6
file content (125 lines) | stat: -rw-r--r-- 4,155 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/*
 * $Horde: horde/lib/Prefs/session.php,v 1.11.2.4 2003/01/03 12:48:42 jan Exp $
 *
 * Copyright 1999-2003 Jon Parise <jon@horde.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 */

Horde::functionCheck('session_start', true,
    'Prefs_session: Required session functions were not found.');

/**
 * Preferences storage implementation for PHP's session implementation.
 *
 * @author  Jon Parise <jon@horde.org>
 * @version $Revision: 1.11.2.4 $
 * @since   Horde 1.3.4
 * @package horde.prefs
 */
class Prefs_session extends Prefs {

    /**
     * Constructs a new session preferences object.
     *
     * @param string $user     The user who owns these preferences.
     * @param string $password The password associated with $user. (unused)
     * @param string $scope    The current preferences scope.
     * @param array  $params   A hash containing connection parameters. (unused)
     * @param boolean $caching  (optional) Should caching be used?
     */
    function Prefs_session($user, $password = '', $scope = '',
        $params = array(), $caching = false)
    {
        $this->user = $user;
        $this->scope = $scope;
        $this->caching = $caching;
    }

    /**
     * Retrieves the requested set of preferences from the current session.
     *
     * @param optional array $prefs  An array listing the preferences to
     *                     retrieve. If not specified, retrieve all of the
     *                     preferences listed in the $prefs hash.
     *
     * @return mixed       True on success or a PEAR_Error object on failure.
     */
    function retrieve($prefs = array())
    {
        if (!session_is_registered('horde_prefs')) {
            return (new PEAR_Error(_("No preferences are available.")));
        }

        /* Retrieve global and local preferences from the session variable. */
        $global_prefs = array();
        if (isset($_SESSION['horde_prefs']['horde'])) {
            $global_prefs =
                $_SESSION['horde_prefs']['horde'];
        }
        $local_prefs = array();
        if (isset($_SESSION['horde_prefs'][$this->scope])) {
            $local_prefs =
                $_SESSION['horde_prefs'][$this->scope];
        }

        /* Retrieve and store the local and global preferences. */
        $this->prefs = array_merge($global_prefs, $local_prefs);

        return true;
    }

    /**
     * Stores preferences in the current session.
     *
     * @param array $prefs  (optional) An array listing the preferences to be
     *                      stored. If not specified, store all of the
     *                      preferences listed in the $prefs hash.
     *
     * @return mixed       True on success or a PEAR_Error object on failure.
     */
    function store($prefs = array())
    {
        /* Create and register the preferences array, if necessary. */
        if (!isset($_SESSION['horde_prefs'])) {
            global $horde_prefs;

            $horde_prefs = array();
            $_SESSION['horde_prefs'] = &$horde_prefs;
            if (!session_register('horde_prefs')) {
                Horde::fatal(_("Unable to register preferences in session."), __FILE__, __LINE__);
            }
        }

        /* Copy the current preferences into the session variable. */
        $GLOBALS['horde_prefs'] = &$_SESSION['horde_prefs'];
        foreach ($this->prefs as $name => $pref) {
            $scope = $this->getScope($name);
            $_SESSION['horde_prefs'][$scope][$name] = $pref;
        }

        return true;
    }

    /**
     * Perform cleanup operations.
     *
     * @param boolean  $all    (optional) Cleanup all Horde preferences.
     */
    function cleanup($all = false)
    {
        /* Perform a Horde-wide cleanup? */
        if ($all) {
            $_SESSION['horde_prefs'] = null;
            session_unregister('horde_prefs');
        } else {
            unset($_SESSION['horde_prefs'][$this->scope]);
            $_SESSION['horde_prefs']['_filled'] = false;
        }

        parent::cleanup($all);
    }
}
?>