summaryrefslogtreecommitdiff
path: root/app/frontend/user/SecurityManager.php
blob: b83174cb8406c06a1544e23a75286cb6d3445fe3 (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
<?php

Prado::using('System.Security.TSecurityManager');
PRado::using('System.Xml.TXmlDocument');

class SecurityManager extends TSecurityManager {

    private $_configFile;
    private $_validationKey;
    private $_encryptionKey;

    public function setConfigFile($path) {
        $this->_configFile = Prado::getPathOfNamespace($path, '.xml');
        $this->_restoreKeys();
    }

    public function getValidationKey() {
        if (!$this->_configFile) {
            return parent::getValidationKey();
        }
        if (!$this->_validationKey) {
            $this->_storeKeys($this->_encryptionKey, $this->generateRandomKey());
        }
        return $this->_validationKey;
    }

    public function setValidationKey($key) {
        parent::setValidationKey($key);
        if ($this->_configFile) {
            $this->_storeKeys($this->_encryptionKey, $key);
        }
    }

    public function getEncryptionKey() {
        if (!$this->_configFile) {
            return parent::getEncryptionKey();
        }
        if (!$this->_encryptionKey) {
            $this->_storeKeys($this->generateRandomKey(), $this->_validationKey);
        }
        return $this->_encryptionKey;
    }

    public function setEncryptionKey($key) {
        parent::setEncryptionKey($key);
        if ($this->_configFile) {
            $this->_storeKeys($key, $this->_validationKey);
        }
    }

    private function _restoreKeys() {
        if ($this->_configFile) {
            try {
                $xml = new TXmlDocument();
                $xml->loadFromFile($this->_configFile);
                foreach ($xml->getELementsByTagName('key') as $key) {
                    $this->{'_' . $key->Attributes['for'] . 'Key'} = $key->Value;
                }
            } catch (TIOException $e) {}
        }
    }

    private function _storeKeys($encryptionKey, $validationKey) {
        $this->_encryptionKey = $encryptionKey;
        $this->_validationKey = $validationKey;
        if ($this->_configFile) {
            $xml = new TXmlDocument();
            $xml->TagName = 'keys';
            $encElement = new TXmlElement('key');
            $encElement->Attributes['for'] = 'encryption';
            $encElement->Value = $this->_encryptionKey;
            $xml->Elements[] = $encElement;
            $valElement = new TXmlElement('key');
            $valElement->Attributes['for'] = 'validation';
            $valElement->Value = $this->_validationKey;
            $xml->Elements[] = $valElement;
            @chmod($this->_configFile, 0600);
            $xml->saveToFile($this->_configFile);
            chmod($this->_configFile, 0400);
        }
    }

}

?>