xref: /plugin/davcal/vendor/sabre/dav/lib/DAV/Auth/Backend/File.php (revision a1a3b6794e0e143a4a8b51d3185ce2d339be61ab)
1*a1a3b679SAndreas Boehler<?php
2*a1a3b679SAndreas Boehler
3*a1a3b679SAndreas Boehlernamespace Sabre\DAV\Auth\Backend;
4*a1a3b679SAndreas Boehler
5*a1a3b679SAndreas Boehleruse Sabre\DAV;
6*a1a3b679SAndreas Boehler
7*a1a3b679SAndreas Boehler/**
8*a1a3b679SAndreas Boehler * This is an authentication backend that uses a file to manage passwords.
9*a1a3b679SAndreas Boehler *
10*a1a3b679SAndreas Boehler * The backend file must conform to Apache's htdigest format
11*a1a3b679SAndreas Boehler *
12*a1a3b679SAndreas Boehler * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
13*a1a3b679SAndreas Boehler * @author Evert Pot (http://evertpot.com/)
14*a1a3b679SAndreas Boehler * @license http://sabre.io/license/ Modified BSD License
15*a1a3b679SAndreas Boehler */
16*a1a3b679SAndreas Boehlerclass File extends AbstractDigest {
17*a1a3b679SAndreas Boehler
18*a1a3b679SAndreas Boehler    /**
19*a1a3b679SAndreas Boehler     * List of users
20*a1a3b679SAndreas Boehler     *
21*a1a3b679SAndreas Boehler     * @var array
22*a1a3b679SAndreas Boehler     */
23*a1a3b679SAndreas Boehler    protected $users = [];
24*a1a3b679SAndreas Boehler
25*a1a3b679SAndreas Boehler    /**
26*a1a3b679SAndreas Boehler     * Creates the backend object.
27*a1a3b679SAndreas Boehler     *
28*a1a3b679SAndreas Boehler     * If the filename argument is passed in, it will parse out the specified file fist.
29*a1a3b679SAndreas Boehler     *
30*a1a3b679SAndreas Boehler     * @param string|null $filename
31*a1a3b679SAndreas Boehler     */
32*a1a3b679SAndreas Boehler    function __construct($filename = null) {
33*a1a3b679SAndreas Boehler
34*a1a3b679SAndreas Boehler        if (!is_null($filename))
35*a1a3b679SAndreas Boehler            $this->loadFile($filename);
36*a1a3b679SAndreas Boehler
37*a1a3b679SAndreas Boehler    }
38*a1a3b679SAndreas Boehler
39*a1a3b679SAndreas Boehler    /**
40*a1a3b679SAndreas Boehler     * Loads an htdigest-formatted file. This method can be called multiple times if
41*a1a3b679SAndreas Boehler     * more than 1 file is used.
42*a1a3b679SAndreas Boehler     *
43*a1a3b679SAndreas Boehler     * @param string $filename
44*a1a3b679SAndreas Boehler     * @return void
45*a1a3b679SAndreas Boehler     */
46*a1a3b679SAndreas Boehler    function loadFile($filename) {
47*a1a3b679SAndreas Boehler
48*a1a3b679SAndreas Boehler        foreach (file($filename, FILE_IGNORE_NEW_LINES) as $line) {
49*a1a3b679SAndreas Boehler
50*a1a3b679SAndreas Boehler            if (substr_count($line, ":") !== 2)
51*a1a3b679SAndreas Boehler                throw new DAV\Exception('Malformed htdigest file. Every line should contain 2 colons');
52*a1a3b679SAndreas Boehler
53*a1a3b679SAndreas Boehler            list($username, $realm, $A1) = explode(':', $line);
54*a1a3b679SAndreas Boehler
55*a1a3b679SAndreas Boehler            if (!preg_match('/^[a-zA-Z0-9]{32}$/', $A1))
56*a1a3b679SAndreas Boehler                throw new DAV\Exception('Malformed htdigest file. Invalid md5 hash');
57*a1a3b679SAndreas Boehler
58*a1a3b679SAndreas Boehler            $this->users[$realm . ':' . $username] = $A1;
59*a1a3b679SAndreas Boehler
60*a1a3b679SAndreas Boehler        }
61*a1a3b679SAndreas Boehler
62*a1a3b679SAndreas Boehler    }
63*a1a3b679SAndreas Boehler
64*a1a3b679SAndreas Boehler    /**
65*a1a3b679SAndreas Boehler     * Returns a users' information
66*a1a3b679SAndreas Boehler     *
67*a1a3b679SAndreas Boehler     * @param string $realm
68*a1a3b679SAndreas Boehler     * @param string $username
69*a1a3b679SAndreas Boehler     * @return string
70*a1a3b679SAndreas Boehler     */
71*a1a3b679SAndreas Boehler    function getDigestHash($realm, $username) {
72*a1a3b679SAndreas Boehler
73*a1a3b679SAndreas Boehler        return isset($this->users[$realm . ':' . $username]) ? $this->users[$realm . ':' . $username] : false;
74*a1a3b679SAndreas Boehler
75*a1a3b679SAndreas Boehler    }
76*a1a3b679SAndreas Boehler
77*a1a3b679SAndreas Boehler}
78