1: <?php
2: /**
3: * Passwd_Driver defines an API for implementing password change systems for
4: * Passwd.
5: *
6: * Copyright 2000-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (GPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/gpl.php.
10: *
11: * @author Mike Cochrane <mike@graftonhall.co.nz>
12: * @author Eric Rostetter <eric.rostetter@physics.utexas.edu>
13: * @package Passwd
14: */
15: abstract class Passwd_Driver
16: {
17: /**
18: * Hash containing configuration parameters.
19: *
20: * @var array
21: */
22: protected $_params = array();
23:
24: /**
25: * Constructor.
26: *
27: * @param $params A hash containing connection parameters.
28: */
29: public function __construct($params = array())
30: {
31: $this->_params = $params;
32: }
33:
34: /**
35: * Compares a plaintext password with an encrypted password.
36: *
37: * @param string $encrypted An encrypted password.
38: * @param string $plaintext An unencrypted password.
39: *
40: * @throws Passwd_Exception if passwords don't match.
41: */
42: protected function _comparePasswords($encrypted, $plaintext)
43: {
44: if (preg_match('/^{([^}]+)}(.*)/', $encrypted, $match)) {
45: $encryption = Horde_String::lower($match[1]);
46: $encrypted = $match[2];
47: } else {
48: $encryption = $this->_params['encryption'];
49: }
50:
51: $hashed = Horde_Auth::getCryptedPassword(
52: $plaintext,
53: $encrypted,
54: $encryption,
55: $this->_params['show_encryption']);
56:
57: if ($this->_params['show_encryption']) {
58: /* Convert the hashing algorithm in both strings to uppercase. */
59: $encrypted = preg_replace(
60: '/^({.*?})/e', "Horde_String::upper('\\1')", $encrypted);
61: $hashed = preg_replace(
62: '/^({.*?})/e', "Horde_String::upper('\\1')", $hashed);
63: }
64:
65: if ($encrypted != $hashed) {
66: throw new Passwd_Exception(_("Incorrect old password."));
67: }
68: }
69:
70: /**
71: * Encrypts a password.
72: *
73: * @param string $plaintext A plaintext password.
74: *
75: * @return string The encrypted password.
76: */
77: protected function _encryptPassword($plaintext)
78: {
79: return Horde_Auth::getCryptedPassword(
80: $plaintext,
81: '',
82: $this->_params['encryption'],
83: $this->_params['show_encryption']);
84: }
85:
86: /**
87: * Changes the user's password.
88: *
89: * @param string $username The user for which to change the password.
90: * @param string $oldpassword The old (current) user password.
91: * @param string $new_password The new user password to set.
92: *
93: * @throws Passwd_Exception
94: */
95: abstract public function changePassword($username, $oldpassword, $new_password);
96: }
97: