ErrorHandlerTrait.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Tools\Error;
  3. use Cake\Controller\Exception\MissingActionException;
  4. use Cake\Controller\Exception\SecurityException;
  5. use Cake\Core\Configure;
  6. use Cake\Datasource\Exception\InvalidPrimaryKeyException;
  7. use Cake\Datasource\Exception\RecordNotFoundException;
  8. use Cake\Http\Exception\BadRequestException;
  9. use Cake\Http\Exception\ConflictException;
  10. use Cake\Http\Exception\GoneException;
  11. use Cake\Http\Exception\InvalidCsrfTokenException;
  12. use Cake\Http\Exception\MethodNotAllowedException;
  13. use Cake\Http\Exception\NotAcceptableException;
  14. use Cake\Http\Exception\NotFoundException;
  15. use Cake\Http\Exception\UnauthorizedException;
  16. use Cake\Http\Exception\UnavailableForLegalReasonsException;
  17. use Cake\Routing\Exception\MissingControllerException;
  18. use Cake\Routing\Exception\MissingRouteException;
  19. use Cake\View\Exception\MissingTemplateException;
  20. use Cake\View\Exception\MissingViewException;
  21. /**
  22. * @property array $_options
  23. */
  24. trait ErrorHandlerTrait {
  25. /**
  26. * @var array
  27. */
  28. protected static $blacklist = [
  29. InvalidPrimaryKeyException::class,
  30. NotFoundException::class,
  31. MethodNotAllowedException::class,
  32. NotAcceptableException::class,
  33. RecordNotFoundException::class,
  34. BadRequestException::class,
  35. GoneException::class,
  36. ConflictException::class,
  37. InvalidCsrfTokenException::class,
  38. UnauthorizedException::class,
  39. MissingControllerException::class,
  40. MissingActionException::class,
  41. MissingRouteException::class,
  42. MissingViewException::class,
  43. MissingTemplateException::class,
  44. UnavailableForLegalReasonsException::class,
  45. SecurityException::class,
  46. 'Cake\Network\Exception\BadRequestException',
  47. 'Cake\Network\Exception\ConflictException',
  48. 'Cake\Network\Exception\GoneException',
  49. 'Cake\Network\Exception\InvalidCsrfTokenException',
  50. 'Cake\Network\Exception\MethodNotAllowedException',
  51. 'Cake\Network\Exception\NotAcceptableException',
  52. 'Cake\Network\Exception\NotFoundException',
  53. 'Cake\Network\Exception\UnauthorizedException',
  54. 'Cake\Network\Exception\UnavailableForLegalReasonsException',
  55. ];
  56. /**
  57. * @param \Exception $exception
  58. * @param \Psr\Http\Message\ServerRequestInterface|null $request
  59. * @return bool
  60. */
  61. protected function is404($exception, $request = null) {
  62. $blacklist = static::$blacklist;
  63. if (isset($this->_options['log404'])) {
  64. $blacklist = $this->_options['log404'];
  65. }
  66. if (!$blacklist) {
  67. return false;
  68. }
  69. $class = get_class($exception);
  70. if (!$this->isBlacklisted($class, (array)$blacklist)) {
  71. return false;
  72. }
  73. if (!$request) {
  74. return true;
  75. }
  76. $referer = $request->getHeaderLine('Referer');
  77. $baseUrl = Configure::read('App.fullBaseUrl');
  78. if (strpos($referer, $baseUrl) === 0) {
  79. return false;
  80. }
  81. return true;
  82. }
  83. /**
  84. * @param string $class
  85. * @param array $blacklist
  86. * @return bool
  87. */
  88. protected function isBlacklisted($class, array $blacklist) {
  89. // Quick string comparison first
  90. if (in_array($class, $blacklist, true)) {
  91. return true;
  92. }
  93. // Deep instance of checking
  94. foreach ($blacklist as $blacklistedClass) {
  95. if ($class instanceof $blacklistedClass) {
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. }