1: <?php
2: /**
3: * The Horde_Auth_Ftp class provides an FTP implementation of the Horde
4: * authentication system.
5: *
6: * Copyright 1999-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (LGPL). If you did
9: * not receive this file, http://www.horde.org/licenses/lgpl21
10: *
11: * @author Chuck Hagenbuch <chuck@horde.org>
12: * @author Max Kalika <max@horde.org>
13: * @category Horde
14: * @license http://www.horde.org/licenses/lgpl21 LGPL-2.1
15: * @package Auth
16: */
17: class Horde_Auth_Ftp extends Horde_Auth_Base
18: {
19: /**
20: * Constructor.
21: *
22: * @param array $params Optional parameters:
23: * <pre>
24: * 'hostspec' - (string) The hostname or IP address of the FTP server.
25: * DEFAULT: 'localhost'
26: * 'port' - (integer) The server port to connect to.
27: * DEFAULT: 21
28: * </pre>
29: *
30: * @throws Horde_Auth_Exception
31: */
32: public function __construct(array $params = array())
33: {
34: if (!Horde_Util::extensionExists('ftp')) {
35: throw new Horde_Auth_Exception(__CLASS__ . ': Required FTP extension not found. Compile PHP with the --enable-ftp switch.');
36: }
37:
38: $params = array_merge(array(
39: 'hostspec' => 'localhost',
40: 'port' => 21
41: ), $params);
42:
43: parent::__construct($params);
44: }
45:
46: /**
47: * Find out if a set of login credentials are valid.
48: *
49: * @param string $userId The userId to check.
50: * @param array $credentials An array of login credentials. For FTP,
51: * this must contain a password entry.
52: *
53: * @throws Horde_Auth_Exception
54: */
55: protected function _authenticate($userId, $credentials)
56: {
57: $ftp = @ftp_connect($this->_params['hostspec'], $this->_params['port']);
58:
59: $res = $ftp && @ftp_login($ftp, $userId, $credentials['password']);
60: @ftp_quit($ftp);
61:
62: if ($res) {
63: throw new Horde_Auth_Exception('', Horde_Auth::REASON_BADLOGIN);
64: }
65: }
66:
67: }
68: