|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 Facebook, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* You are hereby granted a non-exclusive, worldwide, royalty-free license to |
|
6
|
|
|
* use, copy, modify, and distribute this software in source code or binary |
|
7
|
|
|
* form for use in connection with the web services and APIs provided by |
|
8
|
|
|
* Facebook. |
|
9
|
|
|
* |
|
10
|
|
|
* As with any software that integrates with the Facebook platform, your use |
|
11
|
|
|
* of this software is subject to the Facebook Developer Principles and |
|
12
|
|
|
* Policies [http://developers.facebook.com/policy/]. This copyright notice |
|
13
|
|
|
* shall be included in all copies or substantial portions of the software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
|
18
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
20
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|
21
|
|
|
* DEALINGS IN THE SOFTWARE. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
namespace Facebook\PseudoRandomString; |
|
25
|
|
|
|
|
26
|
|
|
use Facebook\Exceptions\FacebookSDKException; |
|
27
|
|
|
use InvalidArgumentException; |
|
28
|
|
|
|
|
29
|
|
|
class PseudoRandomStringGeneratorFactory |
|
30
|
|
|
{ |
|
31
|
|
|
private function __construct() |
|
32
|
|
|
{ |
|
33
|
|
|
// a factory constructor should never be invoked |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Pseudo random string generator creation. |
|
38
|
|
|
* |
|
39
|
|
|
* @param PseudoRandomStringGeneratorInterface|string|null $generator |
|
40
|
|
|
* |
|
41
|
|
|
* @throws InvalidArgumentException If the pseudo random string generator must be set to "mcrypt", "openssl", or "urandom", or be an instance of Facebook\PseudoRandomString\PseudoRandomStringGeneratorInterface. |
|
42
|
|
|
* |
|
43
|
|
|
* @return PseudoRandomStringGeneratorInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function createPseudoRandomStringGenerator($generator) |
|
46
|
|
|
{ |
|
47
|
|
|
if (!$generator) { |
|
48
|
|
|
return self::detectDefaultPseudoRandomStringGenerator(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ($generator instanceof PseudoRandomStringGeneratorInterface) { |
|
52
|
|
|
return $generator; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ('mcrypt' === $generator) { |
|
56
|
|
|
return new McryptPseudoRandomStringGenerator(); |
|
57
|
|
|
} |
|
58
|
|
|
if ('openssl' === $generator) { |
|
59
|
|
|
return new OpenSslPseudoRandomStringGenerator(); |
|
60
|
|
|
} |
|
61
|
|
|
if ('urandom' === $generator) { |
|
62
|
|
|
return new UrandomPseudoRandomStringGenerator(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
throw new InvalidArgumentException('The pseudo random string generator must be set to "mcrypt", "openssl", or "urandom", or be an instance of Facebook\PseudoRandomString\PseudoRandomStringGeneratorInterface'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Detects which pseudo-random string generator to use. |
|
70
|
|
|
* |
|
71
|
|
|
* @throws FacebookSDKException If unable to detect a cryptographically secure pseudo-random string generator. |
|
72
|
|
|
* |
|
73
|
|
|
* @return PseudoRandomStringGeneratorInterface |
|
74
|
|
|
*/ |
|
75
|
|
|
private static function detectDefaultPseudoRandomStringGenerator() |
|
76
|
|
|
{ |
|
77
|
|
|
// Since openssl_random_pseudo_bytes() can sometimes return non-cryptographically |
|
78
|
|
|
// secure pseudo-random strings (in rare cases), we check for mcrypt_create_iv() first. |
|
79
|
|
|
if (function_exists('mcrypt_create_iv')) { |
|
80
|
|
|
return new McryptPseudoRandomStringGenerator(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (function_exists('openssl_random_pseudo_bytes')) { |
|
84
|
|
|
return new OpenSslPseudoRandomStringGenerator(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (!ini_get('open_basedir') && is_readable('/dev/urandom')) { |
|
88
|
|
|
return new UrandomPseudoRandomStringGenerator(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
throw new FacebookSDKException('Unable to detect a cryptographically secure pseudo-random string generator.'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|