aboutsummaryrefslogtreecommitdiff
path: root/src/SemanticScuttle/Model/User/SslClientCert.php
blob: 383b601aa8f727d52ba0811c8352c8a5b1a59967 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
 * SemanticScuttle - your social bookmark manager.
 *
 * PHP version 5.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @license  AGPL http://www.gnu.org/licenses/agpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */

/**
 * SSL client certificate model. Represents one single client certificate
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @license  AGPL http://www.gnu.org/licenses/agpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */
class SemanticScuttle_Model_User_SslClientCert
{
    public $id;
    public $uId;
    public $sslSerial;
    public $sslClientIssuerDn;
    public $sslName;
    public $sslEmail;



    /**
     * Creates and returns a new object and fills it with
     * the passed values from the database.
     *
     * @param array $arCertRow Database row array
     *
     * @return SemanticScuttle_Model_User_SslClientCert
     */
    public static function fromDb($arCertRow)
    {
        $cert = new self();
        foreach (get_object_vars($cert) as $variable => $dummy) {
            if (isset($arCertRow[$variable])) {
                $cert->$variable = $arCertRow[$variable];
            }
        }
        return $cert;
    }



    /**
     * Loads the user's/browser's client certificate information into
     * an object and returns it.
     * Expects that all information is available.
     * Better check with
     * SemanticScuttle_Service_User_SslClientCert::hasValidCert() before.
     *
     * @return SemanticScuttle_Model_User_SslClientCert
     *
     * @see SemanticScuttle_Service_User_SslClientCert::hasValidCert()
     */
    public static function fromCurrentCert()
    {
        $cert = new self();
        $cert->sslSerial         = $_SERVER['SSL_CLIENT_M_SERIAL'];
        $cert->sslClientIssuerDn = $_SERVER['SSL_CLIENT_I_DN'];
        $cert->sslName           = $_SERVER['SSL_CLIENT_S_DN_CN'];
        $cert->sslEmail          = $_SERVER['SSL_CLIENT_S_DN_Email'];
        return $cert;
    }



    /**
     * Tells you if this certificate is the one the user is currently browsing
     * with.
     *
     * @return boolean True if this certificate is the current browser's
     */
    public function isCurrent()
    {
        if (!isset($_SERVER['SSL_CLIENT_M_SERIAL'])
            || !isset($_SERVER['SSL_CLIENT_I_DN'])
        ) {
            return false;
        }

        return $this->sslSerial == $_SERVER['SSL_CLIENT_M_SERIAL']
            && $this->sslClientIssuerDn == $_SERVER['SSL_CLIENT_I_DN'];
    }



    /**
     * Checks if this certificate is registered (exists) in the certificate
     * array
     *
     * @param array $arCertificates Array of certificate objects
     *
     * @return boolean True or false
     */
    public function isRegistered($arCertificates)
    {
        foreach ($arCertificates as $cert) {
            if ($cert->equals($this)) {
                return true;
            }
        }
        return false;
    }



    /**
     * Deletes this certificate from database
     *
     * @return boolean True if all went well, false if not
     */
    public function delete()
    {
        $ok = SemanticScuttle_Service_Factory::get('User_SslClientCert')
            ->delete($this);
        if ($ok) {
            $this->id = null;
        }
        return $ok;
    }



    /**
     * Compares this certificate with the given one.
     *
     * @param SemanticScuttle_Service_Factory $cert Another user certificate
     *
     * @return boolean True if both match.
     */
    public function equals(SemanticScuttle_Model_User_SslClientCert $cert)
    {
        return $this->sslSerial == $cert->sslSerial
            && $this->sslClientIssuerDn == $cert->sslClientIssuerDn;
    }
}
?>