| 1 | <?php |
|---|
| 2 | /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */ |
|---|
| 3 | |
|---|
| 4 | /** |
|---|
| 5 | * Storage driver for use against a generic password file |
|---|
| 6 | * |
|---|
| 7 | * PHP versions 4 and 5 |
|---|
| 8 | * |
|---|
| 9 | * LICENSE: This source file is subject to version 3.01 of the PHP license |
|---|
| 10 | * that is available through the world-wide-web at the following URI: |
|---|
| 11 | * http://www.php.net/license/3_01.txt. If you did not receive a copy of |
|---|
| 12 | * the PHP License and are unable to obtain it through the web, please |
|---|
| 13 | * send a note to [email protected] so we can mail you a copy immediately. |
|---|
| 14 | * |
|---|
| 15 | * @category Authentication |
|---|
| 16 | * @package Auth |
|---|
| 17 | * @author Stefan Ekman <[email protected]> |
|---|
| 18 | * @author Martin Jansen <[email protected]> |
|---|
| 19 | * @author Mika Tuupola <[email protected]> |
|---|
| 20 | * @author Michael Wallner <[email protected]> |
|---|
| 21 | * @author Adam Ashley <[email protected]> |
|---|
| 22 | * @copyright 2001-2006 The PHP Group |
|---|
| 23 | * @license http://www.php.net/license/3_01.txt PHP License 3.01 |
|---|
| 24 | * @version CVS: $Id$ |
|---|
| 25 | * @link http://pear.php.net/package/Auth |
|---|
| 26 | */ |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | * Include PEAR File_Passwd package |
|---|
| 30 | */ |
|---|
| 31 | $include_dir = realpath(dirname( __FILE__)); |
|---|
| 32 | require_once $include_dir . "/../../File/Passwd.php"; |
|---|
| 33 | /** |
|---|
| 34 | * Include Auth_Container base class |
|---|
| 35 | */ |
|---|
| 36 | require_once $include_dir . "/../Container.php"; |
|---|
| 37 | /** |
|---|
| 38 | * Include PEAR package for error handling |
|---|
| 39 | */ |
|---|
| 40 | require_once $include_dir . "/../../PEAR.php"; |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * Storage driver for fetching login data from an encrypted password file. |
|---|
| 44 | * |
|---|
| 45 | * This storage container can handle CVS pserver style passwd files. |
|---|
| 46 | * |
|---|
| 47 | * @category Authentication |
|---|
| 48 | * @package Auth |
|---|
| 49 | * @author Stefan Ekman <[email protected]> |
|---|
| 50 | * @author Martin Jansen <[email protected]> |
|---|
| 51 | * @author Mika Tuupola <[email protected]> |
|---|
| 52 | * @author Michael Wallner <[email protected]> |
|---|
| 53 | * @author Adam Ashley <[email protected]> |
|---|
| 54 | * @copyright 2001-2006 The PHP Group |
|---|
| 55 | * @license http://www.php.net/license/3_01.txt PHP License 3.01 |
|---|
| 56 | * @version Release: 1.4.2 File: $Revision$ |
|---|
| 57 | * @link http://pear.php.net/package/Auth |
|---|
| 58 | */ |
|---|
| 59 | class Auth_Container_File extends Auth_Container |
|---|
| 60 | { |
|---|
| 61 | |
|---|
| 62 | // {{{ properties |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * Path to passwd file |
|---|
| 66 | * |
|---|
| 67 | * @var string |
|---|
| 68 | */ |
|---|
| 69 | var $pwfile = ''; |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * Options for container |
|---|
| 73 | * |
|---|
| 74 | * @var array |
|---|
| 75 | */ |
|---|
| 76 | var $options = array(); |
|---|
| 77 | |
|---|
| 78 | // }}} |
|---|
| 79 | // {{{ Auth_Container_File() [constructor] |
|---|
| 80 | |
|---|
| 81 | /** |
|---|
| 82 | * Constructor of the container class |
|---|
| 83 | * |
|---|
| 84 | * @param string $filename path to passwd file |
|---|
| 85 | * @return object Auth_Container_File new Auth_Container_File object |
|---|
| 86 | */ |
|---|
| 87 | function Auth_Container_File($filename) { |
|---|
| 88 | $this->_setDefaults(); |
|---|
| 89 | |
|---|
| 90 | // Only file is a valid option here |
|---|
| 91 | if(is_array($filename)) { |
|---|
| 92 | $this->pwfile = $filename['file']; |
|---|
| 93 | $this->_parseOptions($filename); |
|---|
| 94 | } else { |
|---|
| 95 | $this->pwfile = $filename; |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | // }}} |
|---|
| 100 | // {{{ fetchData() |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * Authenticate an user |
|---|
| 104 | * |
|---|
| 105 | * @param string username |
|---|
| 106 | * @param string password |
|---|
| 107 | * @return mixed boolean|PEAR_Error |
|---|
| 108 | */ |
|---|
| 109 | function fetchData($user, $pass) |
|---|
| 110 | { |
|---|
| 111 | return File_Passwd::staticAuth($this->options['type'], $this->pwfile, $user, $pass); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | // }}} |
|---|
| 115 | // {{{ listUsers() |
|---|
| 116 | |
|---|
| 117 | /** |
|---|
| 118 | * List all available users |
|---|
| 119 | * |
|---|
| 120 | * @return array |
|---|
| 121 | */ |
|---|
| 122 | function listUsers() |
|---|
| 123 | { |
|---|
| 124 | $pw_obj = &$this->_load(); |
|---|
| 125 | if (PEAR::isError($pw_obj)) { |
|---|
| 126 | return array(); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | $users = $pw_obj->listUser(); |
|---|
| 130 | if (!is_array($users)) { |
|---|
| 131 | return array(); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | foreach ($users as $key => $value) { |
|---|
| 135 | $retVal[] = array("username" => $key, |
|---|
| 136 | "password" => $value['passwd'], |
|---|
| 137 | "cvsuser" => $value['system']); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | return $retVal; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | // }}} |
|---|
| 144 | // {{{ addUser() |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | * Add a new user to the storage container |
|---|
| 148 | * |
|---|
| 149 | * @param string username |
|---|
| 150 | * @param string password |
|---|
| 151 | * @param mixed Additional parameters to File_Password_*::addUser() |
|---|
| 152 | * |
|---|
| 153 | * @return boolean |
|---|
| 154 | */ |
|---|
| 155 | function addUser($user, $pass, $additional='') |
|---|
| 156 | { |
|---|
| 157 | $params = array($user, $pass); |
|---|
| 158 | if (is_array($additional)) { |
|---|
| 159 | foreach ($additional as $item) { |
|---|
| 160 | $params[] = $item; |
|---|
| 161 | } |
|---|
| 162 | } else { |
|---|
| 163 | $params[] = $additional; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | $pw_obj = &$this->_load(); |
|---|
| 167 | if (PEAR::isError($pw_obj)) { |
|---|
| 168 | return false; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | $res = call_user_func_array(array(&$pw_obj, 'addUser'), $params); |
|---|
| 172 | if (PEAR::isError($res)) { |
|---|
| 173 | return false; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | $res = $pw_obj->save(); |
|---|
| 177 | if (PEAR::isError($res)) { |
|---|
| 178 | return false; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | return true; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | // }}} |
|---|
| 185 | // {{{ removeUser() |
|---|
| 186 | |
|---|
| 187 | /** |
|---|
| 188 | * Remove user from the storage container |
|---|
| 189 | * |
|---|
| 190 | * @param string Username |
|---|
| 191 | * @return boolean |
|---|
| 192 | */ |
|---|
| 193 | function removeUser($user) |
|---|
| 194 | { |
|---|
| 195 | $pw_obj = &$this->_load(); |
|---|
| 196 | if (PEAR::isError($pw_obj)) { |
|---|
| 197 | return false; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | $res = $pw_obj->delUser($user); |
|---|
| 201 | if (PEAR::isError($res)) { |
|---|
| 202 | return false; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | $res = $pw_obj->save(); |
|---|
| 206 | if (PEAR::isError($res)) { |
|---|
| 207 | return false; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | return true; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | // }}} |
|---|
| 214 | // {{{ changePassword() |
|---|
| 215 | |
|---|
| 216 | /** |
|---|
| 217 | * Change password for user in the storage container |
|---|
| 218 | * |
|---|
| 219 | * @param string Username |
|---|
| 220 | * @param string The new password |
|---|
| 221 | */ |
|---|
| 222 | function changePassword($username, $password) |
|---|
| 223 | { |
|---|
| 224 | $pw_obj = &$this->_load(); |
|---|
| 225 | if (PEAR::isError($pw_obj)) { |
|---|
| 226 | return false; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | $res = $pw_obj->changePasswd($username, $password); |
|---|
| 230 | if (PEAR::isError($res)) { |
|---|
| 231 | return false; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | $res = $pw_obj->save(); |
|---|
| 235 | if (PEAR::isError($res)) { |
|---|
| 236 | return false; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | return true; |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | // }}} |
|---|
| 243 | // {{{ _load() |
|---|
| 244 | |
|---|
| 245 | /** |
|---|
| 246 | * Load and initialize the File_Passwd object |
|---|
| 247 | * |
|---|
| 248 | * @return object File_Passwd_Cvs|PEAR_Error |
|---|
| 249 | */ |
|---|
| 250 | function &_load() |
|---|
| 251 | { |
|---|
| 252 | static $pw_obj; |
|---|
| 253 | |
|---|
| 254 | if (!isset($pw_obj)) { |
|---|
| 255 | $pw_obj = File_Passwd::factory($this->options['type']); |
|---|
| 256 | if (PEAR::isError($pw_obj)) { |
|---|
| 257 | return $pw_obj; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | $pw_obj->setFile($this->pwfile); |
|---|
| 261 | |
|---|
| 262 | $res = $pw_obj->load(); |
|---|
| 263 | if (PEAR::isError($res)) { |
|---|
| 264 | return $res; |
|---|
| 265 | } |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | return $pw_obj; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | // }}} |
|---|
| 272 | // {{{ _setDefaults() |
|---|
| 273 | |
|---|
| 274 | /** |
|---|
| 275 | * Set some default options |
|---|
| 276 | * |
|---|
| 277 | * @access private |
|---|
| 278 | * @return void |
|---|
| 279 | */ |
|---|
| 280 | function _setDefaults() |
|---|
| 281 | { |
|---|
| 282 | $this->options['type'] = 'Cvs'; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | // }}} |
|---|
| 286 | // {{{ _parseOptions() |
|---|
| 287 | |
|---|
| 288 | /** |
|---|
| 289 | * Parse options passed to the container class |
|---|
| 290 | * |
|---|
| 291 | * @access private |
|---|
| 292 | * @param array |
|---|
| 293 | */ |
|---|
| 294 | function _parseOptions($array) |
|---|
| 295 | { |
|---|
| 296 | foreach ($array as $key => $value) { |
|---|
| 297 | if (isset($this->options[$key])) { |
|---|
| 298 | $this->options[$key] = $value; |
|---|
| 299 | } |
|---|
| 300 | } |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | // }}} |
|---|
| 304 | |
|---|
| 305 | } |
|---|
| 306 | ?> |
|---|