Header.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace WhichBrowser\Analyser;
  3. use WhichBrowser\Constants;
  4. use WhichBrowser\Parser;
  5. trait Header
  6. {
  7. private function &analyseHeaders()
  8. {
  9. /* Analyse the main useragent header */
  10. if ($header = $this->getHeader('User-Agent')) {
  11. $this->analyseUserAgent($header);
  12. }
  13. /* Analyse secondary useragent headers */
  14. if ($header = $this->getHeader('X-Original-User-Agent')) {
  15. $this->additionalUserAgent($header);
  16. }
  17. if ($header = $this->getHeader('X-Device-User-Agent')) {
  18. $this->additionalUserAgent($header);
  19. }
  20. if ($header = $this->getHeader('Device-Stock-UA')) {
  21. $this->additionalUserAgent($header);
  22. }
  23. if ($header = $this->getHeader('X-OperaMini-Phone-UA')) {
  24. $this->additionalUserAgent($header);
  25. }
  26. if ($header = $this->getHeader('X-UCBrowser-Device-UA')) {
  27. $this->additionalUserAgent($header);
  28. }
  29. /* Analyse browser specific headers */
  30. if ($header = $this->getHeader('X-OperaMini-Phone')) {
  31. $this->analyseOperaMiniPhone($header);
  32. }
  33. if ($header = $this->getHeader('X-UCBrowser-Phone-UA')) {
  34. $this->analyseOldUCUserAgent($header);
  35. }
  36. if ($header = $this->getHeader('X-UCBrowser-UA')) {
  37. $this->analyseNewUCUserAgent($header);
  38. }
  39. if ($header = $this->getHeader('X-Puffin-UA')) {
  40. $this->analysePuffinUserAgent($header);
  41. }
  42. if ($header = $this->getHeader('Baidu-FlyFlow')) {
  43. $this->analyseBaiduHeader($header);
  44. }
  45. /* Analyse Android WebView browser ids */
  46. if ($header = $this->getHeader('X-Requested-With')) {
  47. $this->analyseBrowserId($header);
  48. }
  49. /* Analyse WAP profile header */
  50. if ($header = $this->getHeader('X-Wap-Profile')) {
  51. $this->analyseWapProfile($header);
  52. }
  53. return $this;
  54. }
  55. private function analyseUserAgent($header)
  56. {
  57. new Header\Useragent($header, $this->data);
  58. }
  59. private function analyseBaiduHeader($header)
  60. {
  61. new Header\Baidu($header, $this->data);
  62. }
  63. private function analyseOperaMiniPhone($header)
  64. {
  65. new Header\OperaMini($header, $this->data);
  66. }
  67. private function analyseBrowserId($header)
  68. {
  69. new Header\BrowserId($header, $this->data);
  70. }
  71. private function analysePuffinUserAgent($header)
  72. {
  73. new Header\Puffin($header, $this->data);
  74. }
  75. private function analyseNewUCUserAgent($header)
  76. {
  77. new Header\UCBrowserNew($header, $this->data);
  78. }
  79. private function analyseOldUCUserAgent($header)
  80. {
  81. new Header\UCBrowserOld($header, $this->data);
  82. }
  83. private function analyseWapProfile($header)
  84. {
  85. new Header\Wap($header, $this->data);
  86. }
  87. private function additionalUserAgent($ua)
  88. {
  89. $extra = new Parser($ua);
  90. if ($extra->device->type != Constants\DeviceType::DESKTOP) {
  91. if (isset($extra->os->name)) {
  92. $this->data->os = $extra->os;
  93. }
  94. if ($extra->device->identified) {
  95. $this->data->device = $extra->device;
  96. }
  97. }
  98. }
  99. private function getHeader($h)
  100. {
  101. foreach ($this->headers as $k => $v) {
  102. if (strtolower($h) == strtolower($k)) {
  103. return $v;
  104. }
  105. }
  106. }
  107. }