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