File: Token.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 (200 lines) | stat: -rw-r--r-- 6,670 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/*
 * $Horde: horde/lib/Token.php,v 1.6.2.8 2003/04/28 19:59:08 jan Exp $
 *
 * Copyright 1999-2003 Max Kalika <max@horde.org>
 * Copyright 1999-2003 Chuck Hagenbuch <chuck@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.
 */

// Return codes
/** @constant TOKEN_OK Operation succeeded. */
define('TOKEN_OK', 0);

/** @constant TOKEN_ERROR Operation failed. */
define('TOKEN_ERROR', -1);

/** @constant TOKEN_ERROR_PARAMS Bad or missing parameters ($params). */
define('TOKEN_ERROR_PARAMS', -2);

/** @constant TOKEN_ERROR_CONNECT Connection failure. */
define('TOKEN_ERROR_CONNECT', -3);

/** @constant TOKEN_ERROR_AUTH Authentication failure. */
define('TOKEN_ERROR_AUTH', -4);

/** @constant TOKEN_ERROR_EMPTY Empty retrieval result. */
define('TOKEN_ERROR_EMPTY', -5);


/**
 * The Token:: class provides a common abstracted interface into the
 * various token generation mediums. It also includes all of the
 * functions for retrieving, storing, and checking tokens.
 *
 * @author  Max Kalika <max@horde.org>
 * @author  Chuck Hagenbuch <chuck@horde.org>
 * @version $Revision: 1.6.2.8 $
 * @since   Horde 1.3
 * @package horde.token
 */
class Token {

    /**
     * Attempts to return a concrete Token instance based on $driver.
     *
     * @param mixed $driver  The type of concrete Token subclass to return.
     *                       This is based on the storage driver ($driver). The
     *                       code is dynamically included. If $driver is an array,
     *                       then we will look in $driver[0]/lib/Token/ for
     *                       the subclass implementation named $driver[1].php.
     * @param array $params  (optional) A hash containing any additional
     *                       configuration or connection parameters a subclass
     *                       might need.
     *
     * @return object Token The newly created concrete Token instance, or
     *                      false an error.
     */
    function &factory($driver, $params = array())
    {
        if (is_array($driver)) {
            list($app, $driver) = $driver;
        }

        /* Return a base Token object if no driver is specified. */
        $driver = strtolower($driver);
        if (empty($driver) || (strcmp($driver, 'none') == 0)) {
            return new Token($params);
        }

        if (!empty($app)) {
            include_once $GLOBALS['registry']->getParam('fileroot', $app) . '/lib/Token/' . $driver . '.php';
        } elseif (@file_exists(dirname(__FILE__) . '/Token/' . $driver . '.php')) {
            include_once dirname(__FILE__) . '/Token/' . $driver . '.php';
        } else {
            @include_once 'Horde/Token/' . $driver . '.php';
        }
        $class = 'Token_' . $driver;
        if (class_exists($class)) {
            return new $class($params);
        } else {
            return PEAR::raiseError('Class definition of ' . $class . ' not found.');
        }
    }

    /**
     * Attempts to return a reference to a concrete Token instance
     * based on $driver. It will only create a new instance if no
     * Token instance with the same parameters currently exists.
     *
     * This should be used if multiple types of token generators (and,
     * thus, multiple Token instances) are required.
     *
     * This method must be invoked as: $var = &Token::singleton()
     *
     * @param mixed $driver  The type of concrete Token subclass to return.
     *                       This is based on the storage driver ($driver). The
     *                       code is dynamically included. If $driver is an array,
     *                       then we will look in $driver[0]/lib/Token/ for
     *                       the subclass implementation named $driver[1].php.
     * @param array $params  (optional) A hash containing any additional
     *                       configuration or connection parameters a subclass
     *                       might need.
     *
     * @return object Token  The concrete Token reference, or false on an
     *                       error.
     *
     * @since Horde 2.2
     */
    function &singleton($driver, $params = array())
    {
        static $instances;
        if (!isset($instances)) {
            $instances = array();
        }

        if (is_array($driver)) {
            $drivertag = implode(':', $driver);
        } else {
            $drivertag = $driver;
        }
        $signature = md5(strtolower($drivertag) . '][' . implode('][', $params));
        if (!isset($instances[$signature])) {
            $instances[$signature] = &Token::factory($driver, $params);
        }

        return $instances[$signature];
    }

    function hexRemoteAddr()
    {
        $addr = explode('.', $_SERVER['REMOTE_ADDR']);
        return sprintf("%02x%02x%02x%02x", $addr[0], $addr[1], $addr[2], $addr[3]);
    }

    /**
     * Generates a connection id and returns it.
     *
     * @return string   The generated id string.
     */
    function generateID()
    {
        return md5(time() . '][' . Token::hexRemoteAddr());
    }

    /**
     * Checks if the given token has been previously used.  First
     * purges all expired tokens. Then retreives current tokens for
     * the given ip address. If the specified token was not found,
     * adds it.
     *
     * @param string $token  The value of the token to check.
     *
     * @return boolean       True if the token has not been used,
     *                       false otherwise.
     */
    function verify($token)
    {
        $this->purge();
        if ($this->exists($token)) {
            return false;
        }
        else {
            $this->add($token);
            return true;
        }
    }

    /**
     * This is basically an abstract method that should be overridden by a
     * subclass implementation. It's here to retain code integrity in the
     * case that no subclass is loaded ($driver == 'none').
     */
    function exists()
    {
        return false;
    }

    /**
     * This is basically an abstract method that should be overridden by a
     * subclass implementation. It's here to retain code integrity in the
     * case that no subclass is loaded ($driver == 'none').
     */
    function add()
    {
        return true;
    }

    /**
     * This is basically an abstract method that should be overridden by a
     * subclass implementation. It's here to retain code integrity in the
     * case that no subclass is loaded ($driver == 'none').
     */
    function purge()
    {
        return true;
    }
}
?>