1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Server\Contexts\StreamContext |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Johann Zelger <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io/server |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Server\Contexts; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Server\Exceptions\ServerException; |
24
|
|
|
use AppserverIo\Server\Interfaces\StreamContextInterface; |
25
|
|
|
|
26
|
|
|
class StreamContext implements StreamContextInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Constructs the stream context object |
30
|
|
|
* |
31
|
|
|
* @param array $defaultOptions The default options to instantiate the context with |
32
|
|
|
*/ |
33
|
|
|
public function __construct(array $defaultOptions = array()) |
34
|
|
|
{ |
35
|
|
|
// setup internal php resource |
36
|
|
|
$this->resource = stream_context_create($defaultOptions); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Sets an options to the internal resource context object |
41
|
|
|
* |
42
|
|
|
* @param string $wrapper The wrapper section of the option |
43
|
|
|
* @param string $option The option key |
44
|
|
|
* @param mixed $value The value to set for option in specific wrapper section |
45
|
|
|
* |
46
|
|
|
* @return bool true on success or false on failure |
47
|
|
|
*/ |
48
|
|
|
public function setOption($wrapper, $option, $value) |
49
|
|
|
{ |
50
|
|
|
return stream_context_set_option($this->getResource(), $wrapper, $option, $value); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns an specific options in certain wrapper section |
55
|
|
|
* |
56
|
|
|
* @param unknown $wrapper The wrapper section of the option |
57
|
|
|
* @param unknown $option The option key to get the value for |
58
|
|
|
* |
59
|
|
|
* @return mixed The options value null if nothing exists |
60
|
|
|
*/ |
61
|
|
|
public function getOption($wrapper, $option) |
62
|
|
|
{ |
63
|
|
|
$value = null; |
64
|
|
|
$options = stream_context_get_options($this->getResource()); |
65
|
|
|
if (isset($options[$wrapper][$option])) { |
66
|
|
|
$value = $options[$wrapper][$option]; |
67
|
|
|
} |
68
|
|
|
return $value; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns all options set on internal stream context resource |
73
|
|
|
* |
74
|
|
|
* @return array all options |
75
|
|
|
*/ |
76
|
|
|
public function getOptions() |
77
|
|
|
{ |
78
|
|
|
return stream_context_get_options($this->getResource()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Adds a server ssl certificate for specific domain using the sni feature |
83
|
|
|
* |
84
|
|
|
* @param string $domain The domain for the certificate to use |
85
|
|
|
* @param string $certPath The path to the bundled certificate file |
86
|
|
|
* @param bool $overwrite If an existing domain entry should be overwritten or not |
87
|
|
|
* |
88
|
|
|
* @return bool true on success or false on failure |
89
|
|
|
*/ |
90
|
|
|
public function addSniServerCert($domain, $certPath, $overwrite = true) |
91
|
|
|
{ |
92
|
|
|
// get existing server certs |
93
|
|
|
$sniServerCerts = $this->getOption('ssl', 'SNI_server_certs'); |
|
|
|
|
94
|
|
|
// check if sni server certs are set already or new should be started |
95
|
|
|
if (!is_array($sniServerCerts)) { |
96
|
|
|
$sniServerCerts = array(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// check if domain key exists and no overwrite is wanted |
100
|
|
|
if (isset($sniServerCerts[$domain]) && $overwrite === false) { |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// check if cert exists |
105
|
|
|
if (!is_file($certPath)) { |
106
|
|
|
throw new ServerException( |
107
|
|
|
sprintf("SSL certificate '%s' does not exist for domain '%s'.", $certPath, $domain) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// check if cert is valid for server usage |
112
|
|
|
$x509_res = openssl_x509_read(file_get_contents($certPath)); |
113
|
|
|
$valid = openssl_x509_checkpurpose($x509_res, X509_PURPOSE_SSL_SERVER, array($certPath)); |
114
|
|
|
if ($valid === true) { |
115
|
|
|
// if its valid, add it to sni server certs |
116
|
|
|
$sniServerCerts[$domain] = $certPath; |
117
|
|
|
} else { |
118
|
|
|
throw new ServerException( |
119
|
|
|
sprintf("SSL certificate '%s' is not valid for domain '%s'.", $certPath, $domain) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// add it to array |
124
|
|
|
$sniServerCerts[$domain] = $certPath; |
125
|
|
|
|
126
|
|
|
// add sni server certs array back to stream context resource instance |
127
|
|
|
return $this->setOption('ssl', 'SNI_server_certs', $sniServerCerts); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns the internal php stream context resource for php stream usage compatibility |
132
|
|
|
* |
133
|
|
|
* @return resource |
134
|
|
|
*/ |
135
|
|
|
public function getResource() |
136
|
|
|
{ |
137
|
|
|
return $this->resource; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: