PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/station-games/vendor/cakephp/cakephp/src/Utility/CookieCryptTrait.php

https://gitlab.com/ViniciusP/project-games
PHP | 172 lines | 88 code | 11 blank | 73 comment | 13 complexity | f90bbd11ea0123701d7b7f492b69a006 MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.1.6
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Utility;
  16. use RuntimeException;
  17. /**
  18. * Cookie Crypt Trait.
  19. *
  20. * Provides the encrypt/decrypt logic for the CookieComponent.
  21. *
  22. * @link http://book.cakephp.org/3.0/en/controllers/components/cookie.html
  23. */
  24. trait CookieCryptTrait
  25. {
  26. /**
  27. * Valid cipher names for encrypted cookies.
  28. *
  29. * @var array
  30. */
  31. protected $_validCiphers = ['aes', 'rijndael'];
  32. /**
  33. * Returns the encryption key to be used.
  34. *
  35. * @return string
  36. */
  37. abstract protected function _getCookieEncryptionKey();
  38. /**
  39. * Encrypts $value using public $type method in Security class
  40. *
  41. * @param string $value Value to encrypt
  42. * @param string|bool $encrypt Encryption mode to use. False
  43. * disabled encryption.
  44. * @param string|null $key Used as the security salt only in this time for tests if specified.
  45. * @return string Encoded values
  46. */
  47. protected function _encrypt($value, $encrypt, $key = null)
  48. {
  49. if (is_array($value)) {
  50. $value = $this->_implode($value);
  51. }
  52. if ($encrypt === false) {
  53. return $value;
  54. }
  55. $this->_checkCipher($encrypt);
  56. $prefix = "Q2FrZQ==.";
  57. $cipher = null;
  58. if (!isset($key)) {
  59. $key = $this->_getCookieEncryptionKey();
  60. }
  61. if ($encrypt === 'rijndael') {
  62. $cipher = Security::rijndael($value, $key, 'encrypt');
  63. }
  64. if ($encrypt === 'aes') {
  65. $cipher = Security::encrypt($value, $key);
  66. }
  67. return $prefix . base64_encode($cipher);
  68. }
  69. /**
  70. * Helper method for validating encryption cipher names.
  71. *
  72. * @param string $encrypt The cipher name.
  73. * @return void
  74. * @throws \RuntimeException When an invalid cipher is provided.
  75. */
  76. protected function _checkCipher($encrypt)
  77. {
  78. if (!in_array($encrypt, $this->_validCiphers)) {
  79. $msg = sprintf(
  80. 'Invalid encryption cipher. Must be one of %s.',
  81. implode(', ', $this->_validCiphers)
  82. );
  83. throw new RuntimeException($msg);
  84. }
  85. }
  86. /**
  87. * Decrypts $value using public $type method in Security class
  88. *
  89. * @param array $values Values to decrypt
  90. * @param string|bool $mode Encryption mode
  91. * @return string decrypted string
  92. */
  93. protected function _decrypt($values, $mode)
  94. {
  95. if (is_string($values)) {
  96. return $this->_decode($values, $mode);
  97. }
  98. $decrypted = [];
  99. foreach ($values as $name => $value) {
  100. $decrypted[$name] = $this->_decode($value, $mode);
  101. }
  102. return $decrypted;
  103. }
  104. /**
  105. * Decodes and decrypts a single value.
  106. *
  107. * @param string $value The value to decode & decrypt.
  108. * @param string|false $encrypt The encryption cipher to use.
  109. * @return string Decoded value.
  110. */
  111. protected function _decode($value, $encrypt)
  112. {
  113. if (!$encrypt) {
  114. return $this->_explode($value);
  115. }
  116. $this->_checkCipher($encrypt);
  117. $prefix = 'Q2FrZQ==.';
  118. $value = base64_decode(substr($value, strlen($prefix)));
  119. if ($encrypt === 'rijndael') {
  120. $value = Security::rijndael($value, $this->_getCookieEncryptionKey(), 'decrypt');
  121. }
  122. if ($encrypt === 'aes') {
  123. $value = Security::decrypt($value, $this->_getCookieEncryptionKey());
  124. }
  125. return $this->_explode($value);
  126. }
  127. /**
  128. * Implode method to keep keys are multidimensional arrays
  129. *
  130. * @param array $array Map of key and values
  131. * @return string A json encoded string.
  132. */
  133. protected function _implode(array $array)
  134. {
  135. return json_encode($array);
  136. }
  137. /**
  138. * Explode method to return array from string set in CookieComponent::_implode()
  139. * Maintains reading backwards compatibility with 1.x CookieComponent::_implode().
  140. *
  141. * @param string $string A string containing JSON encoded data, or a bare string.
  142. * @return array Map of key and values
  143. */
  144. protected function _explode($string)
  145. {
  146. $first = substr($string, 0, 1);
  147. if ($first === '{' || $first === '[') {
  148. $ret = json_decode($string, true);
  149. return ($ret !== null) ? $ret : $string;
  150. }
  151. $array = [];
  152. foreach (explode(',', $string) as $pair) {
  153. $key = explode('|', $pair);
  154. if (!isset($key[1])) {
  155. return $key[0];
  156. }
  157. $array[$key[0]] = $key[1];
  158. }
  159. return $array;
  160. }
  161. }