Cleanser.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * This file is part of the Klarna Core module
  4. *
  5. * (c) Klarna AB
  6. *
  7. * For the full copyright and license information, please view the NOTICE
  8. * and LICENSE files that were distributed with this source code.
  9. */
  10. namespace Klarna\Core\Logger;
  11. /**
  12. * Class Cleanser
  13. *
  14. * @package Klarna\Core\Logger
  15. */
  16. class Cleanser
  17. {
  18. /**
  19. * List of keys that should never be logged
  20. *
  21. * @var string[]
  22. */
  23. public $privateKeys = [
  24. 'password',
  25. 'shared_secret',
  26. 'secret',
  27. 'secret:Klarna\XMLRPC\Klarna:private',
  28. '_secret:Klarna_Checkout_BasicConnector:private',
  29. 'date_of_birth',
  30. '_secret',
  31. 'street',
  32. 'Authorization',
  33. 'given_name',
  34. 'firstname',
  35. 'gender',
  36. 'family_name',
  37. 'lastname',
  38. 'email',
  39. 'street_address',
  40. 'phone',
  41. 'telephone',
  42. 'title'
  43. ];
  44. /**
  45. * Value to be substituted instead of sensitve data
  46. *
  47. * @var string
  48. */
  49. public $replaceValue = '** REMOVED **';
  50. /**
  51. * Search object for sensitive data and replace it if found
  52. *
  53. * @param mixed $input
  54. * @return mixed
  55. */
  56. public function checkForSensitiveData($input = null)
  57. {
  58. if (is_string($input)) {
  59. return $input;
  60. }
  61. if (null === $input) {
  62. return $input;
  63. }
  64. if ($input instanceof \Exception) {
  65. $newinput = [
  66. 'message' => $input->getMessage(),
  67. 'trace' => $input->getTrace()
  68. ];
  69. $input = $newinput;
  70. }
  71. if (is_object($input)) {
  72. $input = $this->processObject($input);
  73. }
  74. if (is_array($input)) {
  75. return $this->checkArrayForSensitiveData($input);
  76. }
  77. if (($json = json_decode($input, true)) && is_array($json)) {
  78. $json = $this->checkForSensitiveData($json);
  79. return json_encode($json);
  80. }
  81. return $input;
  82. }
  83. /**
  84. * Convert object to array
  85. *
  86. * @param \stdClass $input
  87. * @return array
  88. */
  89. private function processObject($input)
  90. {
  91. if (method_exists($input, 'toArray')) {
  92. return $input->toArray();
  93. }
  94. if (method_exists($input, 'getData')) {
  95. return $input->getData();
  96. }
  97. return get_object_vars($input);
  98. }
  99. /**
  100. * Recursively search array for sensitive data and replace it if found
  101. *
  102. * @param array $input
  103. * @return array
  104. */
  105. public function checkArrayForSensitiveData($input)
  106. {
  107. foreach ($input as $key => $value) {
  108. if (is_int($key)) {
  109. continue;
  110. }
  111. if (in_array($key, $this->privateKeys)) {
  112. $input[$key] = $this->replaceValue;
  113. continue;
  114. }
  115. if (!is_array($value)) {
  116. continue;
  117. }
  118. foreach ($value as $k => $v) {
  119. $input[$key][$k] = $this->checkForSensitiveData($v);
  120. }
  121. }
  122. return $input;
  123. }
  124. }