Engine.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace WhichBrowser\Analyser\Header\Useragent;
  3. use WhichBrowser\Model\Version;
  4. trait Engine
  5. {
  6. private function &detectEngine($ua)
  7. {
  8. $this->detectWebkit($ua);
  9. $this->detectKHTML($ua);
  10. $this->detectGecko($ua);
  11. $this->detectServo($ua);
  12. $this->detectGoanna($ua);
  13. $this->detectPresto($ua);
  14. $this->detectTrident($ua);
  15. $this->detectEdgeHTMLUseragent($ua);
  16. return $this;
  17. }
  18. /* WebKit */
  19. private function detectWebkit($ua)
  20. {
  21. if (preg_match('/WebKit\/([0-9.]*)/iu', $ua, $match)) {
  22. $this->data->engine->name = 'Webkit';
  23. $this->data->engine->version = new Version([ 'value' => $match[1] ]);
  24. if (preg_match('/(?:Chrome|Chromium)\/([0-9]*)/u', $ua, $match)) {
  25. if (intval($match[1]) >= 27) {
  26. $this->data->engine->reset([ 'name' => 'Blink' ]);
  27. }
  28. }
  29. }
  30. if (preg_match('/Browser\/AppleWebKit\/?([0-9.]*)/iu', $ua, $match)) {
  31. $this->data->engine->name = 'Webkit';
  32. $this->data->engine->version = new Version([ 'value' => $match[1] ]);
  33. }
  34. if (preg_match('/AppleWebkit\(like Gecko\)/iu', $ua, $match)) {
  35. $this->data->engine->name = 'Webkit';
  36. }
  37. }
  38. /* KHTML */
  39. private function detectKHTML($ua)
  40. {
  41. if (preg_match('/KHTML\/([0-9.]*)/u', $ua, $match)) {
  42. $this->data->engine->name = 'KHTML';
  43. $this->data->engine->version = new Version([ 'value' => $match[1] ]);
  44. }
  45. }
  46. /* Gecko */
  47. private function detectGecko($ua)
  48. {
  49. if (preg_match('/Gecko/u', $ua) && !preg_match('/like Gecko/iu', $ua)) {
  50. $this->data->engine->name = 'Gecko';
  51. if (preg_match('/; rv:([^\);]+)[\);]/u', $ua, $match)) {
  52. $this->data->engine->version = new Version([ 'value' => $match[1], 'details' => 3 ]);
  53. }
  54. }
  55. }
  56. /* Servo */
  57. private function detectServo($ua)
  58. {
  59. if (preg_match('/Servo\/([0-9.]*)/u', $ua, $match)) {
  60. $this->data->engine->name = 'Servo';
  61. $this->data->engine->version = new Version([ 'value' => $match[1] ]);
  62. }
  63. }
  64. /* Goanna */
  65. private function detectGoanna($ua)
  66. {
  67. if (preg_match('/Goanna/u', $ua)) {
  68. $this->data->engine->name = 'Goanna';
  69. if (preg_match('/Goanna\/([0-9]\.[0-9.]+)/u', $ua, $match)) {
  70. $this->data->engine->version = new Version([ 'value' => $match[1] ]);
  71. }
  72. if (preg_match('/Goanna\/20[0-9]{6,6}/u', $ua) && preg_match('/; rv:([^\);]+)[\);]/u', $ua, $match)) {
  73. $this->data->engine->version = new Version([ 'value' => $match[1] ]);
  74. }
  75. }
  76. }
  77. /* Presto */
  78. private function detectPresto($ua)
  79. {
  80. if (preg_match('/Presto\/([0-9.]*)/u', $ua, $match)) {
  81. $this->data->engine->name = 'Presto';
  82. $this->data->engine->version = new Version([ 'value' => $match[1] ]);
  83. }
  84. }
  85. /* Trident */
  86. private function detectTrident($ua)
  87. {
  88. if (preg_match('/Trident\/([0-9.]*)/u', $ua, $match)) {
  89. $this->data->engine->name = 'Trident';
  90. $this->data->engine->version = new Version([ 'value' => $match[1] ]);
  91. if (isset($this->data->browser->version) && isset($this->data->browser->name) && $this->data->browser->name == 'Internet Explorer') {
  92. if ($this->data->engine->version->toNumber() >= 7 && $this->data->browser->version->toFloat() < 11) {
  93. $this->data->browser->version = new Version([ 'value' => '11.0' ]);
  94. $this->data->browser->mode = 'compat';
  95. }
  96. if ($this->data->engine->version->toNumber() == 6 && $this->data->browser->version->toFloat() < 10) {
  97. $this->data->browser->version = new Version([ 'value' => '10.0' ]);
  98. $this->data->browser->mode = 'compat';
  99. }
  100. if ($this->data->engine->version->toNumber() == 5 && $this->data->browser->version->toFloat() < 9) {
  101. $this->data->browser->version = new Version([ 'value' => '9.0' ]);
  102. $this->data->browser->mode = 'compat';
  103. }
  104. if ($this->data->engine->version->toNumber() == 4 && $this->data->browser->version->toFloat() < 8) {
  105. $this->data->browser->version = new Version([ 'value' => '8.0' ]);
  106. $this->data->browser->mode = 'compat';
  107. }
  108. }
  109. if (isset($this->data->os->version) && isset($this->data->os->name) && $this->data->os->name == 'Windows Phone' && isset($this->data->browser->name) && $this->data->browser->name == 'Mobile Internet Explorer') {
  110. if ($this->data->engine->version->toNumber() == 7 && $this->data->os->version->toFloat() < 8.1) {
  111. $this->data->os->version = new Version([ 'value' => '8.1', 'details' => 2 ]);
  112. }
  113. if ($this->data->engine->version->toNumber() == 5 && $this->data->os->version->toFloat() < 7.5) {
  114. $this->data->os->version = new Version([ 'value' => '7.5', 'details' => 2 ]);
  115. }
  116. }
  117. }
  118. }
  119. /* EdgeHTML */
  120. private function detectEdgeHTMLUseragent($ua)
  121. {
  122. if (preg_match('/Edge\/([0-9.]*)/u', $ua, $match)) {
  123. $this->data->engine->name = 'EdgeHTML';
  124. $this->data->engine->version = new Version([ 'value' => $match[1], 'hidden' => true ]);
  125. }
  126. }
  127. }