PageRenderTime 142ms CodeModel.GetById 80ms app.highlight 3ms RepoModel.GetById 57ms app.codeStats 1ms

/lib/Sabre/DAV/Auth/Backend/File.php

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