@@ -31,23 +31,23 @@ |
||
| 31 | 31 | * @package OC\Security\CSRF |
| 32 | 32 | */ |
| 33 | 33 | class CsrfTokenGenerator { |
| 34 | - /** @var ISecureRandom */ |
|
| 35 | - private $random; |
|
| 34 | + /** @var ISecureRandom */ |
|
| 35 | + private $random; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @param ISecureRandom $random |
|
| 39 | - */ |
|
| 40 | - public function __construct(ISecureRandom $random) { |
|
| 41 | - $this->random = $random; |
|
| 42 | - } |
|
| 37 | + /** |
|
| 38 | + * @param ISecureRandom $random |
|
| 39 | + */ |
|
| 40 | + public function __construct(ISecureRandom $random) { |
|
| 41 | + $this->random = $random; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Generate a new CSRF token. |
|
| 46 | - * |
|
| 47 | - * @param int $length Length of the token in characters. |
|
| 48 | - * @return string |
|
| 49 | - */ |
|
| 50 | - public function generateToken($length = 32) { |
|
| 51 | - return $this->random->generate($length); |
|
| 52 | - } |
|
| 44 | + /** |
|
| 45 | + * Generate a new CSRF token. |
|
| 46 | + * |
|
| 47 | + * @param int $length Length of the token in characters. |
|
| 48 | + * @return string |
|
| 49 | + */ |
|
| 50 | + public function generateToken($length = 32) { |
|
| 51 | + return $this->random->generate($length); |
|
| 52 | + } |
|
| 53 | 53 | } |
@@ -43,93 +43,93 @@ |
||
| 43 | 43 | * @package OC\Security |
| 44 | 44 | */ |
| 45 | 45 | class Crypto implements ICrypto { |
| 46 | - /** @var AES $cipher */ |
|
| 47 | - private $cipher; |
|
| 48 | - /** @var int */ |
|
| 49 | - private $ivLength = 16; |
|
| 50 | - /** @var IConfig */ |
|
| 51 | - private $config; |
|
| 52 | - /** @var ISecureRandom */ |
|
| 53 | - private $random; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @param IConfig $config |
|
| 57 | - * @param ISecureRandom $random |
|
| 58 | - */ |
|
| 59 | - function __construct(IConfig $config, ISecureRandom $random) { |
|
| 60 | - $this->cipher = new AES(); |
|
| 61 | - $this->config = $config; |
|
| 62 | - $this->random = $random; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @param string $message The message to authenticate |
|
| 67 | - * @param string $password Password to use (defaults to `secret` in config.php) |
|
| 68 | - * @return string Calculated HMAC |
|
| 69 | - */ |
|
| 70 | - public function calculateHMAC($message, $password = '') { |
|
| 71 | - if($password === '') { |
|
| 72 | - $password = $this->config->getSystemValue('secret'); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - // Append an "a" behind the password and hash it to prevent reusing the same password as for encryption |
|
| 76 | - $password = hash('sha512', $password . 'a'); |
|
| 77 | - |
|
| 78 | - $hash = new Hash('sha512'); |
|
| 79 | - $hash->setKey($password); |
|
| 80 | - return $hash->hash($message); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Encrypts a value and adds an HMAC (Encrypt-Then-MAC) |
|
| 85 | - * @param string $plaintext |
|
| 86 | - * @param string $password Password to encrypt, if not specified the secret from config.php will be taken |
|
| 87 | - * @return string Authenticated ciphertext |
|
| 88 | - */ |
|
| 89 | - public function encrypt($plaintext, $password = '') { |
|
| 90 | - if($password === '') { |
|
| 91 | - $password = $this->config->getSystemValue('secret'); |
|
| 92 | - } |
|
| 93 | - $this->cipher->setPassword($password); |
|
| 94 | - |
|
| 95 | - $iv = $this->random->generate($this->ivLength); |
|
| 96 | - $this->cipher->setIV($iv); |
|
| 97 | - |
|
| 98 | - $ciphertext = bin2hex($this->cipher->encrypt($plaintext)); |
|
| 99 | - $hmac = bin2hex($this->calculateHMAC($ciphertext.$iv, $password)); |
|
| 100 | - |
|
| 101 | - return $ciphertext.'|'.$iv.'|'.$hmac; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Decrypts a value and verifies the HMAC (Encrypt-Then-Mac) |
|
| 106 | - * @param string $authenticatedCiphertext |
|
| 107 | - * @param string $password Password to encrypt, if not specified the secret from config.php will be taken |
|
| 108 | - * @return string plaintext |
|
| 109 | - * @throws \Exception If the HMAC does not match |
|
| 110 | - */ |
|
| 111 | - public function decrypt($authenticatedCiphertext, $password = '') { |
|
| 112 | - if($password === '') { |
|
| 113 | - $password = $this->config->getSystemValue('secret'); |
|
| 114 | - } |
|
| 115 | - $this->cipher->setPassword($password); |
|
| 116 | - |
|
| 117 | - $parts = explode('|', $authenticatedCiphertext); |
|
| 118 | - if(sizeof($parts) !== 3) { |
|
| 119 | - throw new \Exception('Authenticated ciphertext could not be decoded.'); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - $ciphertext = hex2bin($parts[0]); |
|
| 123 | - $iv = $parts[1]; |
|
| 124 | - $hmac = hex2bin($parts[2]); |
|
| 125 | - |
|
| 126 | - $this->cipher->setIV($iv); |
|
| 127 | - |
|
| 128 | - if(!hash_equals($this->calculateHMAC($parts[0].$parts[1], $password), $hmac)) { |
|
| 129 | - throw new \Exception('HMAC does not match.'); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - return $this->cipher->decrypt($ciphertext); |
|
| 133 | - } |
|
| 46 | + /** @var AES $cipher */ |
|
| 47 | + private $cipher; |
|
| 48 | + /** @var int */ |
|
| 49 | + private $ivLength = 16; |
|
| 50 | + /** @var IConfig */ |
|
| 51 | + private $config; |
|
| 52 | + /** @var ISecureRandom */ |
|
| 53 | + private $random; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @param IConfig $config |
|
| 57 | + * @param ISecureRandom $random |
|
| 58 | + */ |
|
| 59 | + function __construct(IConfig $config, ISecureRandom $random) { |
|
| 60 | + $this->cipher = new AES(); |
|
| 61 | + $this->config = $config; |
|
| 62 | + $this->random = $random; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @param string $message The message to authenticate |
|
| 67 | + * @param string $password Password to use (defaults to `secret` in config.php) |
|
| 68 | + * @return string Calculated HMAC |
|
| 69 | + */ |
|
| 70 | + public function calculateHMAC($message, $password = '') { |
|
| 71 | + if($password === '') { |
|
| 72 | + $password = $this->config->getSystemValue('secret'); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + // Append an "a" behind the password and hash it to prevent reusing the same password as for encryption |
|
| 76 | + $password = hash('sha512', $password . 'a'); |
|
| 77 | + |
|
| 78 | + $hash = new Hash('sha512'); |
|
| 79 | + $hash->setKey($password); |
|
| 80 | + return $hash->hash($message); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Encrypts a value and adds an HMAC (Encrypt-Then-MAC) |
|
| 85 | + * @param string $plaintext |
|
| 86 | + * @param string $password Password to encrypt, if not specified the secret from config.php will be taken |
|
| 87 | + * @return string Authenticated ciphertext |
|
| 88 | + */ |
|
| 89 | + public function encrypt($plaintext, $password = '') { |
|
| 90 | + if($password === '') { |
|
| 91 | + $password = $this->config->getSystemValue('secret'); |
|
| 92 | + } |
|
| 93 | + $this->cipher->setPassword($password); |
|
| 94 | + |
|
| 95 | + $iv = $this->random->generate($this->ivLength); |
|
| 96 | + $this->cipher->setIV($iv); |
|
| 97 | + |
|
| 98 | + $ciphertext = bin2hex($this->cipher->encrypt($plaintext)); |
|
| 99 | + $hmac = bin2hex($this->calculateHMAC($ciphertext.$iv, $password)); |
|
| 100 | + |
|
| 101 | + return $ciphertext.'|'.$iv.'|'.$hmac; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Decrypts a value and verifies the HMAC (Encrypt-Then-Mac) |
|
| 106 | + * @param string $authenticatedCiphertext |
|
| 107 | + * @param string $password Password to encrypt, if not specified the secret from config.php will be taken |
|
| 108 | + * @return string plaintext |
|
| 109 | + * @throws \Exception If the HMAC does not match |
|
| 110 | + */ |
|
| 111 | + public function decrypt($authenticatedCiphertext, $password = '') { |
|
| 112 | + if($password === '') { |
|
| 113 | + $password = $this->config->getSystemValue('secret'); |
|
| 114 | + } |
|
| 115 | + $this->cipher->setPassword($password); |
|
| 116 | + |
|
| 117 | + $parts = explode('|', $authenticatedCiphertext); |
|
| 118 | + if(sizeof($parts) !== 3) { |
|
| 119 | + throw new \Exception('Authenticated ciphertext could not be decoded.'); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + $ciphertext = hex2bin($parts[0]); |
|
| 123 | + $iv = $parts[1]; |
|
| 124 | + $hmac = hex2bin($parts[2]); |
|
| 125 | + |
|
| 126 | + $this->cipher->setIV($iv); |
|
| 127 | + |
|
| 128 | + if(!hash_equals($this->calculateHMAC($parts[0].$parts[1], $password), $hmac)) { |
|
| 129 | + throw new \Exception('HMAC does not match.'); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + return $this->cipher->decrypt($ciphertext); |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | 135 | } |
@@ -68,12 +68,12 @@ discard block |
||
| 68 | 68 | * @return string Calculated HMAC |
| 69 | 69 | */ |
| 70 | 70 | public function calculateHMAC($message, $password = '') { |
| 71 | - if($password === '') { |
|
| 71 | + if ($password === '') { |
|
| 72 | 72 | $password = $this->config->getSystemValue('secret'); |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | // Append an "a" behind the password and hash it to prevent reusing the same password as for encryption |
| 76 | - $password = hash('sha512', $password . 'a'); |
|
| 76 | + $password = hash('sha512', $password.'a'); |
|
| 77 | 77 | |
| 78 | 78 | $hash = new Hash('sha512'); |
| 79 | 79 | $hash->setKey($password); |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | * @return string Authenticated ciphertext |
| 88 | 88 | */ |
| 89 | 89 | public function encrypt($plaintext, $password = '') { |
| 90 | - if($password === '') { |
|
| 90 | + if ($password === '') { |
|
| 91 | 91 | $password = $this->config->getSystemValue('secret'); |
| 92 | 92 | } |
| 93 | 93 | $this->cipher->setPassword($password); |
@@ -109,13 +109,13 @@ discard block |
||
| 109 | 109 | * @throws \Exception If the HMAC does not match |
| 110 | 110 | */ |
| 111 | 111 | public function decrypt($authenticatedCiphertext, $password = '') { |
| 112 | - if($password === '') { |
|
| 112 | + if ($password === '') { |
|
| 113 | 113 | $password = $this->config->getSystemValue('secret'); |
| 114 | 114 | } |
| 115 | 115 | $this->cipher->setPassword($password); |
| 116 | 116 | |
| 117 | 117 | $parts = explode('|', $authenticatedCiphertext); |
| 118 | - if(sizeof($parts) !== 3) { |
|
| 118 | + if (sizeof($parts) !== 3) { |
|
| 119 | 119 | throw new \Exception('Authenticated ciphertext could not be decoded.'); |
| 120 | 120 | } |
| 121 | 121 | |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | |
| 126 | 126 | $this->cipher->setIV($iv); |
| 127 | 127 | |
| 128 | - if(!hash_equals($this->calculateHMAC($parts[0].$parts[1], $password), $hmac)) { |
|
| 128 | + if (!hash_equals($this->calculateHMAC($parts[0].$parts[1], $password), $hmac)) { |
|
| 129 | 129 | throw new \Exception('HMAC does not match.'); |
| 130 | 130 | } |
| 131 | 131 | |
@@ -37,51 +37,51 @@ |
||
| 37 | 37 | * @package OC\Security |
| 38 | 38 | */ |
| 39 | 39 | class SecureRandom implements ISecureRandom { |
| 40 | - /** |
|
| 41 | - * Convenience method to get a low strength random number generator. |
|
| 42 | - * |
|
| 43 | - * Low Strength should be used anywhere that random strings are needed |
|
| 44 | - * in a non-cryptographical setting. They are not strong enough to be |
|
| 45 | - * used as keys or salts. They are however useful for one-time use tokens. |
|
| 46 | - * |
|
| 47 | - * @deprecated 9.0.0 Use \OC\Security\SecureRandom::generate directly or random_bytes() / random_int() |
|
| 48 | - * @return $this |
|
| 49 | - */ |
|
| 50 | - public function getLowStrengthGenerator() { |
|
| 51 | - return $this; |
|
| 52 | - } |
|
| 40 | + /** |
|
| 41 | + * Convenience method to get a low strength random number generator. |
|
| 42 | + * |
|
| 43 | + * Low Strength should be used anywhere that random strings are needed |
|
| 44 | + * in a non-cryptographical setting. They are not strong enough to be |
|
| 45 | + * used as keys or salts. They are however useful for one-time use tokens. |
|
| 46 | + * |
|
| 47 | + * @deprecated 9.0.0 Use \OC\Security\SecureRandom::generate directly or random_bytes() / random_int() |
|
| 48 | + * @return $this |
|
| 49 | + */ |
|
| 50 | + public function getLowStrengthGenerator() { |
|
| 51 | + return $this; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Convenience method to get a medium strength random number generator. |
|
| 56 | - * |
|
| 57 | - * Medium Strength should be used for most needs of a cryptographic nature. |
|
| 58 | - * They are strong enough to be used as keys and salts. However, they do |
|
| 59 | - * take some time and resources to generate, so they should not be over-used |
|
| 60 | - * |
|
| 61 | - * @deprecated 9.0.0 Use \OC\Security\SecureRandom::generate directly or random_bytes() / random_int() |
|
| 62 | - * @return $this |
|
| 63 | - */ |
|
| 64 | - public function getMediumStrengthGenerator() { |
|
| 65 | - return $this; |
|
| 66 | - } |
|
| 54 | + /** |
|
| 55 | + * Convenience method to get a medium strength random number generator. |
|
| 56 | + * |
|
| 57 | + * Medium Strength should be used for most needs of a cryptographic nature. |
|
| 58 | + * They are strong enough to be used as keys and salts. However, they do |
|
| 59 | + * take some time and resources to generate, so they should not be over-used |
|
| 60 | + * |
|
| 61 | + * @deprecated 9.0.0 Use \OC\Security\SecureRandom::generate directly or random_bytes() / random_int() |
|
| 62 | + * @return $this |
|
| 63 | + */ |
|
| 64 | + public function getMediumStrengthGenerator() { |
|
| 65 | + return $this; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Generate a random string of specified length. |
|
| 70 | - * @param int $length The length of the generated string |
|
| 71 | - * @param string $characters An optional list of characters to use if no character list is |
|
| 72 | - * specified all valid base64 characters are used. |
|
| 73 | - * @return string |
|
| 74 | - */ |
|
| 75 | - public function generate($length, |
|
| 76 | - $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/') { |
|
| 77 | - $maxCharIndex = strlen($characters) - 1; |
|
| 78 | - $randomString = ''; |
|
| 68 | + /** |
|
| 69 | + * Generate a random string of specified length. |
|
| 70 | + * @param int $length The length of the generated string |
|
| 71 | + * @param string $characters An optional list of characters to use if no character list is |
|
| 72 | + * specified all valid base64 characters are used. |
|
| 73 | + * @return string |
|
| 74 | + */ |
|
| 75 | + public function generate($length, |
|
| 76 | + $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/') { |
|
| 77 | + $maxCharIndex = strlen($characters) - 1; |
|
| 78 | + $randomString = ''; |
|
| 79 | 79 | |
| 80 | - while($length > 0) { |
|
| 81 | - $randomNumber = \random_int(0, $maxCharIndex); |
|
| 82 | - $randomString .= $characters[$randomNumber]; |
|
| 83 | - $length--; |
|
| 84 | - } |
|
| 85 | - return $randomString; |
|
| 86 | - } |
|
| 80 | + while($length > 0) { |
|
| 81 | + $randomNumber = \random_int(0, $maxCharIndex); |
|
| 82 | + $randomString .= $characters[$randomNumber]; |
|
| 83 | + $length--; |
|
| 84 | + } |
|
| 85 | + return $randomString; |
|
| 86 | + } |
|
| 87 | 87 | } |
@@ -77,7 +77,7 @@ |
||
| 77 | 77 | $maxCharIndex = strlen($characters) - 1; |
| 78 | 78 | $randomString = ''; |
| 79 | 79 | |
| 80 | - while($length > 0) { |
|
| 80 | + while ($length > 0) { |
|
| 81 | 81 | $randomNumber = \random_int(0, $maxCharIndex); |
| 82 | 82 | $randomString .= $characters[$randomNumber]; |
| 83 | 83 | $length--; |
@@ -27,104 +27,104 @@ |
||
| 27 | 27 | use OCP\ICertificate; |
| 28 | 28 | |
| 29 | 29 | class Certificate implements ICertificate { |
| 30 | - protected $name; |
|
| 31 | - |
|
| 32 | - protected $commonName; |
|
| 33 | - |
|
| 34 | - protected $organization; |
|
| 35 | - |
|
| 36 | - protected $serial; |
|
| 37 | - |
|
| 38 | - protected $issueDate; |
|
| 39 | - |
|
| 40 | - protected $expireDate; |
|
| 41 | - |
|
| 42 | - protected $issuerName; |
|
| 43 | - |
|
| 44 | - protected $issuerOrganization; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @param string $data base64 encoded certificate |
|
| 48 | - * @param string $name |
|
| 49 | - * @throws \Exception If the certificate could not get parsed |
|
| 50 | - */ |
|
| 51 | - public function __construct($data, $name) { |
|
| 52 | - $this->name = $name; |
|
| 53 | - $gmt = new \DateTimeZone('GMT'); |
|
| 54 | - |
|
| 55 | - // If string starts with "file://" ignore the certificate |
|
| 56 | - $query = 'file://'; |
|
| 57 | - if(strtolower(substr($data, 0, strlen($query))) === $query) { |
|
| 58 | - throw new \Exception('Certificate could not get parsed.'); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - $info = openssl_x509_parse($data); |
|
| 62 | - if(!is_array($info)) { |
|
| 63 | - throw new \Exception('Certificate could not get parsed.'); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - $this->commonName = isset($info['subject']['CN']) ? $info['subject']['CN'] : null; |
|
| 67 | - $this->organization = isset($info['subject']['O']) ? $info['subject']['O'] : null; |
|
| 68 | - $this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt); |
|
| 69 | - $this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt); |
|
| 70 | - $this->issuerName = isset($info['issuer']['CN']) ? $info['issuer']['CN'] : null; |
|
| 71 | - $this->issuerOrganization = isset($info['issuer']['O']) ? $info['issuer']['O'] : null; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @return string |
|
| 76 | - */ |
|
| 77 | - public function getName() { |
|
| 78 | - return $this->name; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @return string|null |
|
| 83 | - */ |
|
| 84 | - public function getCommonName() { |
|
| 85 | - return $this->commonName; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - public function getOrganization() { |
|
| 92 | - return $this->organization; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @return \DateTime |
|
| 97 | - */ |
|
| 98 | - public function getIssueDate() { |
|
| 99 | - return $this->issueDate; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @return \DateTime |
|
| 104 | - */ |
|
| 105 | - public function getExpireDate() { |
|
| 106 | - return $this->expireDate; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @return bool |
|
| 111 | - */ |
|
| 112 | - public function isExpired() { |
|
| 113 | - $now = new \DateTime(); |
|
| 114 | - return $this->issueDate > $now or $now > $this->expireDate; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @return string|null |
|
| 119 | - */ |
|
| 120 | - public function getIssuerName() { |
|
| 121 | - return $this->issuerName; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @return string|null |
|
| 126 | - */ |
|
| 127 | - public function getIssuerOrganization() { |
|
| 128 | - return $this->issuerOrganization; |
|
| 129 | - } |
|
| 30 | + protected $name; |
|
| 31 | + |
|
| 32 | + protected $commonName; |
|
| 33 | + |
|
| 34 | + protected $organization; |
|
| 35 | + |
|
| 36 | + protected $serial; |
|
| 37 | + |
|
| 38 | + protected $issueDate; |
|
| 39 | + |
|
| 40 | + protected $expireDate; |
|
| 41 | + |
|
| 42 | + protected $issuerName; |
|
| 43 | + |
|
| 44 | + protected $issuerOrganization; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @param string $data base64 encoded certificate |
|
| 48 | + * @param string $name |
|
| 49 | + * @throws \Exception If the certificate could not get parsed |
|
| 50 | + */ |
|
| 51 | + public function __construct($data, $name) { |
|
| 52 | + $this->name = $name; |
|
| 53 | + $gmt = new \DateTimeZone('GMT'); |
|
| 54 | + |
|
| 55 | + // If string starts with "file://" ignore the certificate |
|
| 56 | + $query = 'file://'; |
|
| 57 | + if(strtolower(substr($data, 0, strlen($query))) === $query) { |
|
| 58 | + throw new \Exception('Certificate could not get parsed.'); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + $info = openssl_x509_parse($data); |
|
| 62 | + if(!is_array($info)) { |
|
| 63 | + throw new \Exception('Certificate could not get parsed.'); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + $this->commonName = isset($info['subject']['CN']) ? $info['subject']['CN'] : null; |
|
| 67 | + $this->organization = isset($info['subject']['O']) ? $info['subject']['O'] : null; |
|
| 68 | + $this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt); |
|
| 69 | + $this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt); |
|
| 70 | + $this->issuerName = isset($info['issuer']['CN']) ? $info['issuer']['CN'] : null; |
|
| 71 | + $this->issuerOrganization = isset($info['issuer']['O']) ? $info['issuer']['O'] : null; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @return string |
|
| 76 | + */ |
|
| 77 | + public function getName() { |
|
| 78 | + return $this->name; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @return string|null |
|
| 83 | + */ |
|
| 84 | + public function getCommonName() { |
|
| 85 | + return $this->commonName; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + public function getOrganization() { |
|
| 92 | + return $this->organization; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @return \DateTime |
|
| 97 | + */ |
|
| 98 | + public function getIssueDate() { |
|
| 99 | + return $this->issueDate; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @return \DateTime |
|
| 104 | + */ |
|
| 105 | + public function getExpireDate() { |
|
| 106 | + return $this->expireDate; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @return bool |
|
| 111 | + */ |
|
| 112 | + public function isExpired() { |
|
| 113 | + $now = new \DateTime(); |
|
| 114 | + return $this->issueDate > $now or $now > $this->expireDate; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @return string|null |
|
| 119 | + */ |
|
| 120 | + public function getIssuerName() { |
|
| 121 | + return $this->issuerName; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @return string|null |
|
| 126 | + */ |
|
| 127 | + public function getIssuerOrganization() { |
|
| 128 | + return $this->issuerOrganization; |
|
| 129 | + } |
|
| 130 | 130 | } |
@@ -54,19 +54,19 @@ |
||
| 54 | 54 | |
| 55 | 55 | // If string starts with "file://" ignore the certificate |
| 56 | 56 | $query = 'file://'; |
| 57 | - if(strtolower(substr($data, 0, strlen($query))) === $query) { |
|
| 57 | + if (strtolower(substr($data, 0, strlen($query))) === $query) { |
|
| 58 | 58 | throw new \Exception('Certificate could not get parsed.'); |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | $info = openssl_x509_parse($data); |
| 62 | - if(!is_array($info)) { |
|
| 62 | + if (!is_array($info)) { |
|
| 63 | 63 | throw new \Exception('Certificate could not get parsed.'); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | $this->commonName = isset($info['subject']['CN']) ? $info['subject']['CN'] : null; |
| 67 | 67 | $this->organization = isset($info['subject']['O']) ? $info['subject']['O'] : null; |
| 68 | - $this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt); |
|
| 69 | - $this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt); |
|
| 68 | + $this->issueDate = new \DateTime('@'.$info['validFrom_time_t'], $gmt); |
|
| 69 | + $this->expireDate = new \DateTime('@'.$info['validTo_time_t'], $gmt); |
|
| 70 | 70 | $this->issuerName = isset($info['issuer']['CN']) ? $info['issuer']['CN'] : null; |
| 71 | 71 | $this->issuerOrganization = isset($info['issuer']['O']) ? $info['issuer']['O'] : null; |
| 72 | 72 | } |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | private function getCutoff($expire) { |
| 79 | 79 | $d1 = new \DateTime(); |
| 80 | 80 | $d2 = clone $d1; |
| 81 | - $d2->sub(new \DateInterval('PT' . $expire . 'S')); |
|
| 81 | + $d2->sub(new \DateInterval('PT'.$expire.'S')); |
|
| 82 | 82 | return $d2->diff($d1); |
| 83 | 83 | } |
| 84 | 84 | |
@@ -154,7 +154,7 @@ discard block |
||
| 154 | 154 | $ip, |
| 155 | 155 | array $metadata = []) { |
| 156 | 156 | // No need to log if the bruteforce protection is disabled |
| 157 | - if($this->config->getSystemValue('auth.bruteforce.protection.enabled', true) === false) { |
|
| 157 | + if ($this->config->getSystemValue('auth.bruteforce.protection.enabled', true) === false) { |
|
| 158 | 158 | return; |
| 159 | 159 | } |
| 160 | 160 | |
@@ -179,7 +179,7 @@ discard block |
||
| 179 | 179 | |
| 180 | 180 | $qb = $this->db->getQueryBuilder(); |
| 181 | 181 | $qb->insert('bruteforce_attempts'); |
| 182 | - foreach($values as $column => $value) { |
|
| 182 | + foreach ($values as $column => $value) { |
|
| 183 | 183 | $qb->setValue($column, $qb->createNamedParameter($value)); |
| 184 | 184 | } |
| 185 | 185 | $qb->execute(); |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | |
| 216 | 216 | $maxDelay = 30; |
| 217 | 217 | $firstDelay = 0.1; |
| 218 | - if ($attempts > (8 * PHP_INT_SIZE - 1)) { |
|
| 218 | + if ($attempts > (8 * PHP_INT_SIZE - 1)) { |
|
| 219 | 219 | // Don't ever overflow. Just assume the maxDelay time:s |
| 220 | 220 | $firstDelay = $maxDelay; |
| 221 | 221 | } else { |
@@ -42,191 +42,191 @@ |
||
| 42 | 42 | * @package OC\Security\Bruteforce |
| 43 | 43 | */ |
| 44 | 44 | class Throttler { |
| 45 | - const LOGIN_ACTION = 'login'; |
|
| 46 | - |
|
| 47 | - /** @var IDBConnection */ |
|
| 48 | - private $db; |
|
| 49 | - /** @var ITimeFactory */ |
|
| 50 | - private $timeFactory; |
|
| 51 | - /** @var ILogger */ |
|
| 52 | - private $logger; |
|
| 53 | - /** @var IConfig */ |
|
| 54 | - private $config; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @param IDBConnection $db |
|
| 58 | - * @param ITimeFactory $timeFactory |
|
| 59 | - * @param ILogger $logger |
|
| 60 | - * @param IConfig $config |
|
| 61 | - */ |
|
| 62 | - public function __construct(IDBConnection $db, |
|
| 63 | - ITimeFactory $timeFactory, |
|
| 64 | - ILogger $logger, |
|
| 65 | - IConfig $config) { |
|
| 66 | - $this->db = $db; |
|
| 67 | - $this->timeFactory = $timeFactory; |
|
| 68 | - $this->logger = $logger; |
|
| 69 | - $this->config = $config; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Convert a number of seconds into the appropriate DateInterval |
|
| 74 | - * |
|
| 75 | - * @param int $expire |
|
| 76 | - * @return \DateInterval |
|
| 77 | - */ |
|
| 78 | - private function getCutoff($expire) { |
|
| 79 | - $d1 = new \DateTime(); |
|
| 80 | - $d2 = clone $d1; |
|
| 81 | - $d2->sub(new \DateInterval('PT' . $expire . 'S')); |
|
| 82 | - return $d2->diff($d1); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Return the given subnet for an IPv4 address and mask bits |
|
| 87 | - * |
|
| 88 | - * @param string $ip |
|
| 89 | - * @param int $maskBits |
|
| 90 | - * @return string |
|
| 91 | - */ |
|
| 92 | - private function getIPv4Subnet($ip, |
|
| 93 | - $maskBits = 32) { |
|
| 94 | - $binary = \inet_pton($ip); |
|
| 95 | - for ($i = 32; $i > $maskBits; $i -= 8) { |
|
| 96 | - $j = \intdiv($i, 8) - 1; |
|
| 97 | - $k = (int) \min(8, $i - $maskBits); |
|
| 98 | - $mask = (0xff - ((pow(2, $k)) - 1)); |
|
| 99 | - $int = \unpack('C', $binary[$j]); |
|
| 100 | - $binary[$j] = \pack('C', $int[1] & $mask); |
|
| 101 | - } |
|
| 102 | - return \inet_ntop($binary).'/'.$maskBits; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Return the given subnet for an IPv6 address and mask bits |
|
| 107 | - * |
|
| 108 | - * @param string $ip |
|
| 109 | - * @param int $maskBits |
|
| 110 | - * @return string |
|
| 111 | - */ |
|
| 112 | - private function getIPv6Subnet($ip, $maskBits = 48) { |
|
| 113 | - $binary = \inet_pton($ip); |
|
| 114 | - for ($i = 128; $i > $maskBits; $i -= 8) { |
|
| 115 | - $j = \intdiv($i, 8) - 1; |
|
| 116 | - $k = (int) \min(8, $i - $maskBits); |
|
| 117 | - $mask = (0xff - ((pow(2, $k)) - 1)); |
|
| 118 | - $int = \unpack('C', $binary[$j]); |
|
| 119 | - $binary[$j] = \pack('C', $int[1] & $mask); |
|
| 120 | - } |
|
| 121 | - return \inet_ntop($binary).'/'.$maskBits; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Return the given subnet for an IP and the configured mask bits |
|
| 126 | - * |
|
| 127 | - * Determine if the IP is an IPv4 or IPv6 address, then pass to the correct |
|
| 128 | - * method for handling that specific type. |
|
| 129 | - * |
|
| 130 | - * @param string $ip |
|
| 131 | - * @return string |
|
| 132 | - */ |
|
| 133 | - private function getSubnet($ip) { |
|
| 134 | - if (\preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $ip)) { |
|
| 135 | - return $this->getIPv4Subnet( |
|
| 136 | - $ip, |
|
| 137 | - 32 |
|
| 138 | - ); |
|
| 139 | - } |
|
| 140 | - return $this->getIPv6Subnet( |
|
| 141 | - $ip, |
|
| 142 | - 128 |
|
| 143 | - ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Register a failed attempt to bruteforce a security control |
|
| 148 | - * |
|
| 149 | - * @param string $action |
|
| 150 | - * @param string $ip |
|
| 151 | - * @param array $metadata Optional metadata logged to the database |
|
| 152 | - */ |
|
| 153 | - public function registerAttempt($action, |
|
| 154 | - $ip, |
|
| 155 | - array $metadata = []) { |
|
| 156 | - // No need to log if the bruteforce protection is disabled |
|
| 157 | - if($this->config->getSystemValue('auth.bruteforce.protection.enabled', true) === false) { |
|
| 158 | - return; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - $values = [ |
|
| 162 | - 'action' => $action, |
|
| 163 | - 'occurred' => $this->timeFactory->getTime(), |
|
| 164 | - 'ip' => $ip, |
|
| 165 | - 'subnet' => $this->getSubnet($ip), |
|
| 166 | - 'metadata' => json_encode($metadata), |
|
| 167 | - ]; |
|
| 168 | - |
|
| 169 | - $this->logger->notice( |
|
| 170 | - sprintf( |
|
| 171 | - 'Bruteforce attempt from "%s" detected for action "%s".', |
|
| 172 | - $ip, |
|
| 173 | - $action |
|
| 174 | - ), |
|
| 175 | - [ |
|
| 176 | - 'app' => 'core', |
|
| 177 | - ] |
|
| 178 | - ); |
|
| 179 | - |
|
| 180 | - $qb = $this->db->getQueryBuilder(); |
|
| 181 | - $qb->insert('bruteforce_attempts'); |
|
| 182 | - foreach($values as $column => $value) { |
|
| 183 | - $qb->setValue($column, $qb->createNamedParameter($value)); |
|
| 184 | - } |
|
| 185 | - $qb->execute(); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Get the throttling delay (in milliseconds) |
|
| 190 | - * |
|
| 191 | - * @param string $ip |
|
| 192 | - * @return int |
|
| 193 | - */ |
|
| 194 | - public function getDelay($ip) { |
|
| 195 | - $cutoffTime = (new \DateTime()) |
|
| 196 | - ->sub($this->getCutoff(43200)) |
|
| 197 | - ->getTimestamp(); |
|
| 198 | - |
|
| 199 | - $qb = $this->db->getQueryBuilder(); |
|
| 200 | - $qb->select('*') |
|
| 201 | - ->from('bruteforce_attempts') |
|
| 202 | - ->where($qb->expr()->gt('occurred', $qb->createNamedParameter($cutoffTime))) |
|
| 203 | - ->andWhere($qb->expr()->eq('subnet', $qb->createNamedParameter($this->getSubnet($ip)))); |
|
| 204 | - $attempts = count($qb->execute()->fetchAll()); |
|
| 205 | - |
|
| 206 | - if ($attempts === 0) { |
|
| 207 | - return 0; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - $maxDelay = 30; |
|
| 211 | - $firstDelay = 0.1; |
|
| 212 | - if ($attempts > (8 * PHP_INT_SIZE - 1)) { |
|
| 213 | - // Don't ever overflow. Just assume the maxDelay time:s |
|
| 214 | - $firstDelay = $maxDelay; |
|
| 215 | - } else { |
|
| 216 | - $firstDelay *= pow(2, $attempts); |
|
| 217 | - if ($firstDelay > $maxDelay) { |
|
| 218 | - $firstDelay = $maxDelay; |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - return (int) \ceil($firstDelay * 1000); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Will sleep for the defined amount of time |
|
| 226 | - * |
|
| 227 | - * @param string $ip |
|
| 228 | - */ |
|
| 229 | - public function sleepDelay($ip) { |
|
| 230 | - usleep($this->getDelay($ip) * 1000); |
|
| 231 | - } |
|
| 45 | + const LOGIN_ACTION = 'login'; |
|
| 46 | + |
|
| 47 | + /** @var IDBConnection */ |
|
| 48 | + private $db; |
|
| 49 | + /** @var ITimeFactory */ |
|
| 50 | + private $timeFactory; |
|
| 51 | + /** @var ILogger */ |
|
| 52 | + private $logger; |
|
| 53 | + /** @var IConfig */ |
|
| 54 | + private $config; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @param IDBConnection $db |
|
| 58 | + * @param ITimeFactory $timeFactory |
|
| 59 | + * @param ILogger $logger |
|
| 60 | + * @param IConfig $config |
|
| 61 | + */ |
|
| 62 | + public function __construct(IDBConnection $db, |
|
| 63 | + ITimeFactory $timeFactory, |
|
| 64 | + ILogger $logger, |
|
| 65 | + IConfig $config) { |
|
| 66 | + $this->db = $db; |
|
| 67 | + $this->timeFactory = $timeFactory; |
|
| 68 | + $this->logger = $logger; |
|
| 69 | + $this->config = $config; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Convert a number of seconds into the appropriate DateInterval |
|
| 74 | + * |
|
| 75 | + * @param int $expire |
|
| 76 | + * @return \DateInterval |
|
| 77 | + */ |
|
| 78 | + private function getCutoff($expire) { |
|
| 79 | + $d1 = new \DateTime(); |
|
| 80 | + $d2 = clone $d1; |
|
| 81 | + $d2->sub(new \DateInterval('PT' . $expire . 'S')); |
|
| 82 | + return $d2->diff($d1); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Return the given subnet for an IPv4 address and mask bits |
|
| 87 | + * |
|
| 88 | + * @param string $ip |
|
| 89 | + * @param int $maskBits |
|
| 90 | + * @return string |
|
| 91 | + */ |
|
| 92 | + private function getIPv4Subnet($ip, |
|
| 93 | + $maskBits = 32) { |
|
| 94 | + $binary = \inet_pton($ip); |
|
| 95 | + for ($i = 32; $i > $maskBits; $i -= 8) { |
|
| 96 | + $j = \intdiv($i, 8) - 1; |
|
| 97 | + $k = (int) \min(8, $i - $maskBits); |
|
| 98 | + $mask = (0xff - ((pow(2, $k)) - 1)); |
|
| 99 | + $int = \unpack('C', $binary[$j]); |
|
| 100 | + $binary[$j] = \pack('C', $int[1] & $mask); |
|
| 101 | + } |
|
| 102 | + return \inet_ntop($binary).'/'.$maskBits; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Return the given subnet for an IPv6 address and mask bits |
|
| 107 | + * |
|
| 108 | + * @param string $ip |
|
| 109 | + * @param int $maskBits |
|
| 110 | + * @return string |
|
| 111 | + */ |
|
| 112 | + private function getIPv6Subnet($ip, $maskBits = 48) { |
|
| 113 | + $binary = \inet_pton($ip); |
|
| 114 | + for ($i = 128; $i > $maskBits; $i -= 8) { |
|
| 115 | + $j = \intdiv($i, 8) - 1; |
|
| 116 | + $k = (int) \min(8, $i - $maskBits); |
|
| 117 | + $mask = (0xff - ((pow(2, $k)) - 1)); |
|
| 118 | + $int = \unpack('C', $binary[$j]); |
|
| 119 | + $binary[$j] = \pack('C', $int[1] & $mask); |
|
| 120 | + } |
|
| 121 | + return \inet_ntop($binary).'/'.$maskBits; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Return the given subnet for an IP and the configured mask bits |
|
| 126 | + * |
|
| 127 | + * Determine if the IP is an IPv4 or IPv6 address, then pass to the correct |
|
| 128 | + * method for handling that specific type. |
|
| 129 | + * |
|
| 130 | + * @param string $ip |
|
| 131 | + * @return string |
|
| 132 | + */ |
|
| 133 | + private function getSubnet($ip) { |
|
| 134 | + if (\preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $ip)) { |
|
| 135 | + return $this->getIPv4Subnet( |
|
| 136 | + $ip, |
|
| 137 | + 32 |
|
| 138 | + ); |
|
| 139 | + } |
|
| 140 | + return $this->getIPv6Subnet( |
|
| 141 | + $ip, |
|
| 142 | + 128 |
|
| 143 | + ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Register a failed attempt to bruteforce a security control |
|
| 148 | + * |
|
| 149 | + * @param string $action |
|
| 150 | + * @param string $ip |
|
| 151 | + * @param array $metadata Optional metadata logged to the database |
|
| 152 | + */ |
|
| 153 | + public function registerAttempt($action, |
|
| 154 | + $ip, |
|
| 155 | + array $metadata = []) { |
|
| 156 | + // No need to log if the bruteforce protection is disabled |
|
| 157 | + if($this->config->getSystemValue('auth.bruteforce.protection.enabled', true) === false) { |
|
| 158 | + return; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + $values = [ |
|
| 162 | + 'action' => $action, |
|
| 163 | + 'occurred' => $this->timeFactory->getTime(), |
|
| 164 | + 'ip' => $ip, |
|
| 165 | + 'subnet' => $this->getSubnet($ip), |
|
| 166 | + 'metadata' => json_encode($metadata), |
|
| 167 | + ]; |
|
| 168 | + |
|
| 169 | + $this->logger->notice( |
|
| 170 | + sprintf( |
|
| 171 | + 'Bruteforce attempt from "%s" detected for action "%s".', |
|
| 172 | + $ip, |
|
| 173 | + $action |
|
| 174 | + ), |
|
| 175 | + [ |
|
| 176 | + 'app' => 'core', |
|
| 177 | + ] |
|
| 178 | + ); |
|
| 179 | + |
|
| 180 | + $qb = $this->db->getQueryBuilder(); |
|
| 181 | + $qb->insert('bruteforce_attempts'); |
|
| 182 | + foreach($values as $column => $value) { |
|
| 183 | + $qb->setValue($column, $qb->createNamedParameter($value)); |
|
| 184 | + } |
|
| 185 | + $qb->execute(); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Get the throttling delay (in milliseconds) |
|
| 190 | + * |
|
| 191 | + * @param string $ip |
|
| 192 | + * @return int |
|
| 193 | + */ |
|
| 194 | + public function getDelay($ip) { |
|
| 195 | + $cutoffTime = (new \DateTime()) |
|
| 196 | + ->sub($this->getCutoff(43200)) |
|
| 197 | + ->getTimestamp(); |
|
| 198 | + |
|
| 199 | + $qb = $this->db->getQueryBuilder(); |
|
| 200 | + $qb->select('*') |
|
| 201 | + ->from('bruteforce_attempts') |
|
| 202 | + ->where($qb->expr()->gt('occurred', $qb->createNamedParameter($cutoffTime))) |
|
| 203 | + ->andWhere($qb->expr()->eq('subnet', $qb->createNamedParameter($this->getSubnet($ip)))); |
|
| 204 | + $attempts = count($qb->execute()->fetchAll()); |
|
| 205 | + |
|
| 206 | + if ($attempts === 0) { |
|
| 207 | + return 0; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + $maxDelay = 30; |
|
| 211 | + $firstDelay = 0.1; |
|
| 212 | + if ($attempts > (8 * PHP_INT_SIZE - 1)) { |
|
| 213 | + // Don't ever overflow. Just assume the maxDelay time:s |
|
| 214 | + $firstDelay = $maxDelay; |
|
| 215 | + } else { |
|
| 216 | + $firstDelay *= pow(2, $attempts); |
|
| 217 | + if ($firstDelay > $maxDelay) { |
|
| 218 | + $firstDelay = $maxDelay; |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + return (int) \ceil($firstDelay * 1000); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Will sleep for the defined amount of time |
|
| 226 | + * |
|
| 227 | + * @param string $ip |
|
| 228 | + */ |
|
| 229 | + public function sleepDelay($ip) { |
|
| 230 | + usleep($this->getDelay($ip) * 1000); |
|
| 231 | + } |
|
| 232 | 232 | } |
@@ -34,246 +34,246 @@ |
||
| 34 | 34 | use OCP\ITempManager; |
| 35 | 35 | |
| 36 | 36 | class TempManager implements ITempManager { |
| 37 | - /** @var string[] Current temporary files and folders, used for cleanup */ |
|
| 38 | - protected $current = []; |
|
| 39 | - /** @var string i.e. /tmp on linux systems */ |
|
| 40 | - protected $tmpBaseDir; |
|
| 41 | - /** @var ILogger */ |
|
| 42 | - protected $log; |
|
| 43 | - /** @var IConfig */ |
|
| 44 | - protected $config; |
|
| 37 | + /** @var string[] Current temporary files and folders, used for cleanup */ |
|
| 38 | + protected $current = []; |
|
| 39 | + /** @var string i.e. /tmp on linux systems */ |
|
| 40 | + protected $tmpBaseDir; |
|
| 41 | + /** @var ILogger */ |
|
| 42 | + protected $log; |
|
| 43 | + /** @var IConfig */ |
|
| 44 | + protected $config; |
|
| 45 | 45 | |
| 46 | - /** Prefix */ |
|
| 47 | - const TMP_PREFIX = 'oc_tmp_'; |
|
| 46 | + /** Prefix */ |
|
| 47 | + const TMP_PREFIX = 'oc_tmp_'; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * @param \OCP\ILogger $logger |
|
| 51 | - * @param \OCP\IConfig $config |
|
| 52 | - */ |
|
| 53 | - public function __construct(ILogger $logger, IConfig $config) { |
|
| 54 | - $this->log = $logger; |
|
| 55 | - $this->config = $config; |
|
| 56 | - $this->tmpBaseDir = $this->getTempBaseDir(); |
|
| 57 | - } |
|
| 49 | + /** |
|
| 50 | + * @param \OCP\ILogger $logger |
|
| 51 | + * @param \OCP\IConfig $config |
|
| 52 | + */ |
|
| 53 | + public function __construct(ILogger $logger, IConfig $config) { |
|
| 54 | + $this->log = $logger; |
|
| 55 | + $this->config = $config; |
|
| 56 | + $this->tmpBaseDir = $this->getTempBaseDir(); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Builds the filename with suffix and removes potential dangerous characters |
|
| 61 | - * such as directory separators. |
|
| 62 | - * |
|
| 63 | - * @param string $absolutePath Absolute path to the file / folder |
|
| 64 | - * @param string $postFix Postfix appended to the temporary file name, may be user controlled |
|
| 65 | - * @return string |
|
| 66 | - */ |
|
| 67 | - private function buildFileNameWithSuffix($absolutePath, $postFix = '') { |
|
| 68 | - if($postFix !== '') { |
|
| 69 | - $postFix = '.' . ltrim($postFix, '.'); |
|
| 70 | - $postFix = str_replace(['\\', '/'], '', $postFix); |
|
| 71 | - $absolutePath .= '-'; |
|
| 72 | - } |
|
| 59 | + /** |
|
| 60 | + * Builds the filename with suffix and removes potential dangerous characters |
|
| 61 | + * such as directory separators. |
|
| 62 | + * |
|
| 63 | + * @param string $absolutePath Absolute path to the file / folder |
|
| 64 | + * @param string $postFix Postfix appended to the temporary file name, may be user controlled |
|
| 65 | + * @return string |
|
| 66 | + */ |
|
| 67 | + private function buildFileNameWithSuffix($absolutePath, $postFix = '') { |
|
| 68 | + if($postFix !== '') { |
|
| 69 | + $postFix = '.' . ltrim($postFix, '.'); |
|
| 70 | + $postFix = str_replace(['\\', '/'], '', $postFix); |
|
| 71 | + $absolutePath .= '-'; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - return $absolutePath . $postFix; |
|
| 75 | - } |
|
| 74 | + return $absolutePath . $postFix; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Create a temporary file and return the path |
|
| 79 | - * |
|
| 80 | - * @param string $postFix Postfix appended to the temporary file name |
|
| 81 | - * @return string |
|
| 82 | - */ |
|
| 83 | - public function getTemporaryFile($postFix = '') { |
|
| 84 | - if (is_writable($this->tmpBaseDir)) { |
|
| 85 | - // To create an unique file and prevent the risk of race conditions |
|
| 86 | - // or duplicated temporary files by other means such as collisions |
|
| 87 | - // we need to create the file using `tempnam` and append a possible |
|
| 88 | - // postfix to it later |
|
| 89 | - $file = tempnam($this->tmpBaseDir, self::TMP_PREFIX); |
|
| 90 | - $this->current[] = $file; |
|
| 77 | + /** |
|
| 78 | + * Create a temporary file and return the path |
|
| 79 | + * |
|
| 80 | + * @param string $postFix Postfix appended to the temporary file name |
|
| 81 | + * @return string |
|
| 82 | + */ |
|
| 83 | + public function getTemporaryFile($postFix = '') { |
|
| 84 | + if (is_writable($this->tmpBaseDir)) { |
|
| 85 | + // To create an unique file and prevent the risk of race conditions |
|
| 86 | + // or duplicated temporary files by other means such as collisions |
|
| 87 | + // we need to create the file using `tempnam` and append a possible |
|
| 88 | + // postfix to it later |
|
| 89 | + $file = tempnam($this->tmpBaseDir, self::TMP_PREFIX); |
|
| 90 | + $this->current[] = $file; |
|
| 91 | 91 | |
| 92 | - // If a postfix got specified sanitize it and create a postfixed |
|
| 93 | - // temporary file |
|
| 94 | - if($postFix !== '') { |
|
| 95 | - $fileNameWithPostfix = $this->buildFileNameWithSuffix($file, $postFix); |
|
| 96 | - touch($fileNameWithPostfix); |
|
| 97 | - chmod($fileNameWithPostfix, 0600); |
|
| 98 | - $this->current[] = $fileNameWithPostfix; |
|
| 99 | - return $fileNameWithPostfix; |
|
| 100 | - } |
|
| 92 | + // If a postfix got specified sanitize it and create a postfixed |
|
| 93 | + // temporary file |
|
| 94 | + if($postFix !== '') { |
|
| 95 | + $fileNameWithPostfix = $this->buildFileNameWithSuffix($file, $postFix); |
|
| 96 | + touch($fileNameWithPostfix); |
|
| 97 | + chmod($fileNameWithPostfix, 0600); |
|
| 98 | + $this->current[] = $fileNameWithPostfix; |
|
| 99 | + return $fileNameWithPostfix; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - return $file; |
|
| 103 | - } else { |
|
| 104 | - $this->log->warning( |
|
| 105 | - 'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions', |
|
| 106 | - [ |
|
| 107 | - 'dir' => $this->tmpBaseDir, |
|
| 108 | - ] |
|
| 109 | - ); |
|
| 110 | - return false; |
|
| 111 | - } |
|
| 112 | - } |
|
| 102 | + return $file; |
|
| 103 | + } else { |
|
| 104 | + $this->log->warning( |
|
| 105 | + 'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions', |
|
| 106 | + [ |
|
| 107 | + 'dir' => $this->tmpBaseDir, |
|
| 108 | + ] |
|
| 109 | + ); |
|
| 110 | + return false; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Create a temporary folder and return the path |
|
| 116 | - * |
|
| 117 | - * @param string $postFix Postfix appended to the temporary folder name |
|
| 118 | - * @return string |
|
| 119 | - */ |
|
| 120 | - public function getTemporaryFolder($postFix = '') { |
|
| 121 | - if (is_writable($this->tmpBaseDir)) { |
|
| 122 | - // To create an unique directory and prevent the risk of race conditions |
|
| 123 | - // or duplicated temporary files by other means such as collisions |
|
| 124 | - // we need to create the file using `tempnam` and append a possible |
|
| 125 | - // postfix to it later |
|
| 126 | - $uniqueFileName = tempnam($this->tmpBaseDir, self::TMP_PREFIX); |
|
| 127 | - $this->current[] = $uniqueFileName; |
|
| 114 | + /** |
|
| 115 | + * Create a temporary folder and return the path |
|
| 116 | + * |
|
| 117 | + * @param string $postFix Postfix appended to the temporary folder name |
|
| 118 | + * @return string |
|
| 119 | + */ |
|
| 120 | + public function getTemporaryFolder($postFix = '') { |
|
| 121 | + if (is_writable($this->tmpBaseDir)) { |
|
| 122 | + // To create an unique directory and prevent the risk of race conditions |
|
| 123 | + // or duplicated temporary files by other means such as collisions |
|
| 124 | + // we need to create the file using `tempnam` and append a possible |
|
| 125 | + // postfix to it later |
|
| 126 | + $uniqueFileName = tempnam($this->tmpBaseDir, self::TMP_PREFIX); |
|
| 127 | + $this->current[] = $uniqueFileName; |
|
| 128 | 128 | |
| 129 | - // Build a name without postfix |
|
| 130 | - $path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix); |
|
| 131 | - mkdir($path, 0700); |
|
| 132 | - $this->current[] = $path; |
|
| 129 | + // Build a name without postfix |
|
| 130 | + $path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix); |
|
| 131 | + mkdir($path, 0700); |
|
| 132 | + $this->current[] = $path; |
|
| 133 | 133 | |
| 134 | - return $path . '/'; |
|
| 135 | - } else { |
|
| 136 | - $this->log->warning( |
|
| 137 | - 'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions', |
|
| 138 | - [ |
|
| 139 | - 'dir' => $this->tmpBaseDir, |
|
| 140 | - ] |
|
| 141 | - ); |
|
| 142 | - return false; |
|
| 143 | - } |
|
| 144 | - } |
|
| 134 | + return $path . '/'; |
|
| 135 | + } else { |
|
| 136 | + $this->log->warning( |
|
| 137 | + 'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions', |
|
| 138 | + [ |
|
| 139 | + 'dir' => $this->tmpBaseDir, |
|
| 140 | + ] |
|
| 141 | + ); |
|
| 142 | + return false; |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Remove the temporary files and folders generated during this request |
|
| 148 | - */ |
|
| 149 | - public function clean() { |
|
| 150 | - $this->cleanFiles($this->current); |
|
| 151 | - } |
|
| 146 | + /** |
|
| 147 | + * Remove the temporary files and folders generated during this request |
|
| 148 | + */ |
|
| 149 | + public function clean() { |
|
| 150 | + $this->cleanFiles($this->current); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * @param string[] $files |
|
| 155 | - */ |
|
| 156 | - protected function cleanFiles($files) { |
|
| 157 | - foreach ($files as $file) { |
|
| 158 | - if (file_exists($file)) { |
|
| 159 | - try { |
|
| 160 | - \OC_Helper::rmdirr($file); |
|
| 161 | - } catch (\UnexpectedValueException $ex) { |
|
| 162 | - $this->log->warning( |
|
| 163 | - "Error deleting temporary file/folder: {file} - Reason: {error}", |
|
| 164 | - [ |
|
| 165 | - 'file' => $file, |
|
| 166 | - 'error' => $ex->getMessage(), |
|
| 167 | - ] |
|
| 168 | - ); |
|
| 169 | - } |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - } |
|
| 153 | + /** |
|
| 154 | + * @param string[] $files |
|
| 155 | + */ |
|
| 156 | + protected function cleanFiles($files) { |
|
| 157 | + foreach ($files as $file) { |
|
| 158 | + if (file_exists($file)) { |
|
| 159 | + try { |
|
| 160 | + \OC_Helper::rmdirr($file); |
|
| 161 | + } catch (\UnexpectedValueException $ex) { |
|
| 162 | + $this->log->warning( |
|
| 163 | + "Error deleting temporary file/folder: {file} - Reason: {error}", |
|
| 164 | + [ |
|
| 165 | + 'file' => $file, |
|
| 166 | + 'error' => $ex->getMessage(), |
|
| 167 | + ] |
|
| 168 | + ); |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - /** |
|
| 175 | - * Remove old temporary files and folders that were failed to be cleaned |
|
| 176 | - */ |
|
| 177 | - public function cleanOld() { |
|
| 178 | - $this->cleanFiles($this->getOldFiles()); |
|
| 179 | - } |
|
| 174 | + /** |
|
| 175 | + * Remove old temporary files and folders that were failed to be cleaned |
|
| 176 | + */ |
|
| 177 | + public function cleanOld() { |
|
| 178 | + $this->cleanFiles($this->getOldFiles()); |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - /** |
|
| 182 | - * Get all temporary files and folders generated by oc older than an hour |
|
| 183 | - * |
|
| 184 | - * @return string[] |
|
| 185 | - */ |
|
| 186 | - protected function getOldFiles() { |
|
| 187 | - $cutOfTime = time() - 3600; |
|
| 188 | - $files = []; |
|
| 189 | - $dh = opendir($this->tmpBaseDir); |
|
| 190 | - if ($dh) { |
|
| 191 | - while (($file = readdir($dh)) !== false) { |
|
| 192 | - if (substr($file, 0, 7) === self::TMP_PREFIX) { |
|
| 193 | - $path = $this->tmpBaseDir . '/' . $file; |
|
| 194 | - $mtime = filemtime($path); |
|
| 195 | - if ($mtime < $cutOfTime) { |
|
| 196 | - $files[] = $path; |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - return $files; |
|
| 202 | - } |
|
| 181 | + /** |
|
| 182 | + * Get all temporary files and folders generated by oc older than an hour |
|
| 183 | + * |
|
| 184 | + * @return string[] |
|
| 185 | + */ |
|
| 186 | + protected function getOldFiles() { |
|
| 187 | + $cutOfTime = time() - 3600; |
|
| 188 | + $files = []; |
|
| 189 | + $dh = opendir($this->tmpBaseDir); |
|
| 190 | + if ($dh) { |
|
| 191 | + while (($file = readdir($dh)) !== false) { |
|
| 192 | + if (substr($file, 0, 7) === self::TMP_PREFIX) { |
|
| 193 | + $path = $this->tmpBaseDir . '/' . $file; |
|
| 194 | + $mtime = filemtime($path); |
|
| 195 | + if ($mtime < $cutOfTime) { |
|
| 196 | + $files[] = $path; |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + return $files; |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | - /** |
|
| 205 | - * Get the temporary base directory configured on the server |
|
| 206 | - * |
|
| 207 | - * @return string Path to the temporary directory or null |
|
| 208 | - * @throws \UnexpectedValueException |
|
| 209 | - */ |
|
| 210 | - public function getTempBaseDir() { |
|
| 211 | - if ($this->tmpBaseDir) { |
|
| 212 | - return $this->tmpBaseDir; |
|
| 213 | - } |
|
| 204 | + /** |
|
| 205 | + * Get the temporary base directory configured on the server |
|
| 206 | + * |
|
| 207 | + * @return string Path to the temporary directory or null |
|
| 208 | + * @throws \UnexpectedValueException |
|
| 209 | + */ |
|
| 210 | + public function getTempBaseDir() { |
|
| 211 | + if ($this->tmpBaseDir) { |
|
| 212 | + return $this->tmpBaseDir; |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - $directories = []; |
|
| 216 | - if ($temp = $this->config->getSystemValue('tempdirectory', null)) { |
|
| 217 | - $directories[] = $temp; |
|
| 218 | - } |
|
| 219 | - if ($temp = \OC::$server->getIniWrapper()->get('upload_tmp_dir')) { |
|
| 220 | - $directories[] = $temp; |
|
| 221 | - } |
|
| 222 | - if ($temp = getenv('TMP')) { |
|
| 223 | - $directories[] = $temp; |
|
| 224 | - } |
|
| 225 | - if ($temp = getenv('TEMP')) { |
|
| 226 | - $directories[] = $temp; |
|
| 227 | - } |
|
| 228 | - if ($temp = getenv('TMPDIR')) { |
|
| 229 | - $directories[] = $temp; |
|
| 230 | - } |
|
| 231 | - if ($temp = sys_get_temp_dir()) { |
|
| 232 | - $directories[] = $temp; |
|
| 233 | - } |
|
| 215 | + $directories = []; |
|
| 216 | + if ($temp = $this->config->getSystemValue('tempdirectory', null)) { |
|
| 217 | + $directories[] = $temp; |
|
| 218 | + } |
|
| 219 | + if ($temp = \OC::$server->getIniWrapper()->get('upload_tmp_dir')) { |
|
| 220 | + $directories[] = $temp; |
|
| 221 | + } |
|
| 222 | + if ($temp = getenv('TMP')) { |
|
| 223 | + $directories[] = $temp; |
|
| 224 | + } |
|
| 225 | + if ($temp = getenv('TEMP')) { |
|
| 226 | + $directories[] = $temp; |
|
| 227 | + } |
|
| 228 | + if ($temp = getenv('TMPDIR')) { |
|
| 229 | + $directories[] = $temp; |
|
| 230 | + } |
|
| 231 | + if ($temp = sys_get_temp_dir()) { |
|
| 232 | + $directories[] = $temp; |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | - foreach ($directories as $dir) { |
|
| 236 | - if ($this->checkTemporaryDirectory($dir)) { |
|
| 237 | - return $dir; |
|
| 238 | - } |
|
| 239 | - } |
|
| 235 | + foreach ($directories as $dir) { |
|
| 236 | + if ($this->checkTemporaryDirectory($dir)) { |
|
| 237 | + return $dir; |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - $temp = tempnam(dirname(__FILE__), ''); |
|
| 242 | - if (file_exists($temp)) { |
|
| 243 | - unlink($temp); |
|
| 244 | - return dirname($temp); |
|
| 245 | - } |
|
| 246 | - throw new \UnexpectedValueException('Unable to detect system temporary directory'); |
|
| 247 | - } |
|
| 241 | + $temp = tempnam(dirname(__FILE__), ''); |
|
| 242 | + if (file_exists($temp)) { |
|
| 243 | + unlink($temp); |
|
| 244 | + return dirname($temp); |
|
| 245 | + } |
|
| 246 | + throw new \UnexpectedValueException('Unable to detect system temporary directory'); |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | - /** |
|
| 250 | - * Check if a temporary directory is ready for use |
|
| 251 | - * |
|
| 252 | - * @param mixed $directory |
|
| 253 | - * @return bool |
|
| 254 | - */ |
|
| 255 | - private function checkTemporaryDirectory($directory) { |
|
| 256 | - // suppress any possible errors caused by is_writable |
|
| 257 | - // checks missing or invalid path or characters, wrong permissions etc |
|
| 258 | - try { |
|
| 259 | - if (is_writeable($directory)) { |
|
| 260 | - return true; |
|
| 261 | - } |
|
| 262 | - } catch (\Exception $e) { |
|
| 263 | - } |
|
| 264 | - $this->log->warning('Temporary directory {dir} is not present or writable', |
|
| 265 | - ['dir' => $directory] |
|
| 266 | - ); |
|
| 267 | - return false; |
|
| 268 | - } |
|
| 249 | + /** |
|
| 250 | + * Check if a temporary directory is ready for use |
|
| 251 | + * |
|
| 252 | + * @param mixed $directory |
|
| 253 | + * @return bool |
|
| 254 | + */ |
|
| 255 | + private function checkTemporaryDirectory($directory) { |
|
| 256 | + // suppress any possible errors caused by is_writable |
|
| 257 | + // checks missing or invalid path or characters, wrong permissions etc |
|
| 258 | + try { |
|
| 259 | + if (is_writeable($directory)) { |
|
| 260 | + return true; |
|
| 261 | + } |
|
| 262 | + } catch (\Exception $e) { |
|
| 263 | + } |
|
| 264 | + $this->log->warning('Temporary directory {dir} is not present or writable', |
|
| 265 | + ['dir' => $directory] |
|
| 266 | + ); |
|
| 267 | + return false; |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | - /** |
|
| 271 | - * Override the temporary base directory |
|
| 272 | - * |
|
| 273 | - * @param string $directory |
|
| 274 | - */ |
|
| 275 | - public function overrideTempBaseDir($directory) { |
|
| 276 | - $this->tmpBaseDir = $directory; |
|
| 277 | - } |
|
| 270 | + /** |
|
| 271 | + * Override the temporary base directory |
|
| 272 | + * |
|
| 273 | + * @param string $directory |
|
| 274 | + */ |
|
| 275 | + public function overrideTempBaseDir($directory) { |
|
| 276 | + $this->tmpBaseDir = $directory; |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | 279 | } |
@@ -65,13 +65,13 @@ discard block |
||
| 65 | 65 | * @return string |
| 66 | 66 | */ |
| 67 | 67 | private function buildFileNameWithSuffix($absolutePath, $postFix = '') { |
| 68 | - if($postFix !== '') { |
|
| 69 | - $postFix = '.' . ltrim($postFix, '.'); |
|
| 68 | + if ($postFix !== '') { |
|
| 69 | + $postFix = '.'.ltrim($postFix, '.'); |
|
| 70 | 70 | $postFix = str_replace(['\\', '/'], '', $postFix); |
| 71 | 71 | $absolutePath .= '-'; |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - return $absolutePath . $postFix; |
|
| 74 | + return $absolutePath.$postFix; |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | /** |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | |
| 92 | 92 | // If a postfix got specified sanitize it and create a postfixed |
| 93 | 93 | // temporary file |
| 94 | - if($postFix !== '') { |
|
| 94 | + if ($postFix !== '') { |
|
| 95 | 95 | $fileNameWithPostfix = $this->buildFileNameWithSuffix($file, $postFix); |
| 96 | 96 | touch($fileNameWithPostfix); |
| 97 | 97 | chmod($fileNameWithPostfix, 0600); |
@@ -127,11 +127,11 @@ discard block |
||
| 127 | 127 | $this->current[] = $uniqueFileName; |
| 128 | 128 | |
| 129 | 129 | // Build a name without postfix |
| 130 | - $path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix); |
|
| 130 | + $path = $this->buildFileNameWithSuffix($uniqueFileName.'-folder', $postFix); |
|
| 131 | 131 | mkdir($path, 0700); |
| 132 | 132 | $this->current[] = $path; |
| 133 | 133 | |
| 134 | - return $path . '/'; |
|
| 134 | + return $path.'/'; |
|
| 135 | 135 | } else { |
| 136 | 136 | $this->log->warning( |
| 137 | 137 | 'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions', |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | if ($dh) { |
| 191 | 191 | while (($file = readdir($dh)) !== false) { |
| 192 | 192 | if (substr($file, 0, 7) === self::TMP_PREFIX) { |
| 193 | - $path = $this->tmpBaseDir . '/' . $file; |
|
| 193 | + $path = $this->tmpBaseDir.'/'.$file; |
|
| 194 | 194 | $mtime = filemtime($path); |
| 195 | 195 | if ($mtime < $cutOfTime) { |
| 196 | 196 | $files[] = $path; |
@@ -24,15 +24,15 @@ |
||
| 24 | 24 | namespace OC; |
| 25 | 25 | |
| 26 | 26 | class DatabaseException extends \Exception { |
| 27 | - private $query; |
|
| 27 | + private $query; |
|
| 28 | 28 | |
| 29 | - //FIXME getQuery seems to be unused, maybe use parent constructor with $message, $code and $previous |
|
| 30 | - public function __construct($message, $query = null){ |
|
| 31 | - parent::__construct($message); |
|
| 32 | - $this->query = $query; |
|
| 33 | - } |
|
| 29 | + //FIXME getQuery seems to be unused, maybe use parent constructor with $message, $code and $previous |
|
| 30 | + public function __construct($message, $query = null){ |
|
| 31 | + parent::__construct($message); |
|
| 32 | + $this->query = $query; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - public function getQuery() { |
|
| 36 | - return $this->query; |
|
| 37 | - } |
|
| 35 | + public function getQuery() { |
|
| 36 | + return $this->query; |
|
| 37 | + } |
|
| 38 | 38 | } |
@@ -27,7 +27,7 @@ |
||
| 27 | 27 | private $query; |
| 28 | 28 | |
| 29 | 29 | //FIXME getQuery seems to be unused, maybe use parent constructor with $message, $code and $previous |
| 30 | - public function __construct($message, $query = null){ |
|
| 30 | + public function __construct($message, $query = null) { |
|
| 31 | 31 | parent::__construct($message); |
| 32 | 32 | $this->query = $query; |
| 33 | 33 | } |
@@ -28,20 +28,20 @@ |
||
| 28 | 28 | class Errorlog { |
| 29 | 29 | |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Init class data |
|
| 33 | - */ |
|
| 34 | - public static function init() { |
|
| 35 | - } |
|
| 31 | + /** |
|
| 32 | + * Init class data |
|
| 33 | + */ |
|
| 34 | + public static function init() { |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * write a message in the log |
|
| 39 | - * @param string $app |
|
| 40 | - * @param string $message |
|
| 41 | - * @param int $level |
|
| 42 | - */ |
|
| 43 | - public static function write($app, $message, $level) { |
|
| 44 | - error_log('[owncloud]['.$app.']['.$level.'] '.$message); |
|
| 45 | - } |
|
| 37 | + /** |
|
| 38 | + * write a message in the log |
|
| 39 | + * @param string $app |
|
| 40 | + * @param string $message |
|
| 41 | + * @param int $level |
|
| 42 | + */ |
|
| 43 | + public static function write($app, $message, $level) { |
|
| 44 | + error_log('[owncloud]['.$app.']['.$level.'] '.$message); |
|
| 45 | + } |
|
| 46 | 46 | } |
| 47 | 47 | |
@@ -26,31 +26,31 @@ |
||
| 26 | 26 | namespace OC\Log; |
| 27 | 27 | |
| 28 | 28 | class Syslog { |
| 29 | - static protected $levels = array( |
|
| 30 | - \OCP\Util::DEBUG => LOG_DEBUG, |
|
| 31 | - \OCP\Util::INFO => LOG_INFO, |
|
| 32 | - \OCP\Util::WARN => LOG_WARNING, |
|
| 33 | - \OCP\Util::ERROR => LOG_ERR, |
|
| 34 | - \OCP\Util::FATAL => LOG_CRIT, |
|
| 35 | - ); |
|
| 29 | + static protected $levels = array( |
|
| 30 | + \OCP\Util::DEBUG => LOG_DEBUG, |
|
| 31 | + \OCP\Util::INFO => LOG_INFO, |
|
| 32 | + \OCP\Util::WARN => LOG_WARNING, |
|
| 33 | + \OCP\Util::ERROR => LOG_ERR, |
|
| 34 | + \OCP\Util::FATAL => LOG_CRIT, |
|
| 35 | + ); |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Init class data |
|
| 39 | - */ |
|
| 40 | - public static function init() { |
|
| 41 | - openlog(\OC::$server->getSystemConfig()->getValue("syslog_tag", "ownCloud"), LOG_PID | LOG_CONS, LOG_USER); |
|
| 42 | - // Close at shutdown |
|
| 43 | - register_shutdown_function('closelog'); |
|
| 44 | - } |
|
| 37 | + /** |
|
| 38 | + * Init class data |
|
| 39 | + */ |
|
| 40 | + public static function init() { |
|
| 41 | + openlog(\OC::$server->getSystemConfig()->getValue("syslog_tag", "ownCloud"), LOG_PID | LOG_CONS, LOG_USER); |
|
| 42 | + // Close at shutdown |
|
| 43 | + register_shutdown_function('closelog'); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * write a message in the log |
|
| 48 | - * @param string $app |
|
| 49 | - * @param string $message |
|
| 50 | - * @param int $level |
|
| 51 | - */ |
|
| 52 | - public static function write($app, $message, $level) { |
|
| 53 | - $syslog_level = self::$levels[$level]; |
|
| 54 | - syslog($syslog_level, '{'.$app.'} '.$message); |
|
| 55 | - } |
|
| 46 | + /** |
|
| 47 | + * write a message in the log |
|
| 48 | + * @param string $app |
|
| 49 | + * @param string $message |
|
| 50 | + * @param int $level |
|
| 51 | + */ |
|
| 52 | + public static function write($app, $message, $level) { |
|
| 53 | + $syslog_level = self::$levels[$level]; |
|
| 54 | + syslog($syslog_level, '{'.$app.'} '.$message); |
|
| 55 | + } |
|
| 56 | 56 | } |