BrowserId.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace WhichBrowser\Analyser\Header;
  3. use WhichBrowser\Constants;
  4. use WhichBrowser\Data;
  5. use WhichBrowser\Model\Using;
  6. use WhichBrowser\Model\Version;
  7. class BrowserId
  8. {
  9. public function __construct($header, &$data)
  10. {
  11. if ($header == 'XMLHttpRequest') {
  12. return;
  13. }
  14. $this->data =& $data;
  15. /* The X-Requested-With header is send by the WebView, so our browser name is Chrome it is probably the Chromium WebView which is sometimes misidentified. */
  16. if (isset($this->data->browser->name) && $this->data->browser->name == 'Chrome') {
  17. $version = $this->data->browser->getVersion();
  18. $this->data->browser->reset();
  19. $this->data->browser->using = new Using([ 'name' => 'Chromium WebView', 'version' => new Version([ 'value' => explode('.', $version)[0] ]) ]);
  20. }
  21. /* Detect the correct browser based on the header */
  22. $browser = Data\BrowserIds::identify($header);
  23. if ($browser) {
  24. if (!isset($this->data->browser->name)) {
  25. $this->data->browser->name = $browser;
  26. } else {
  27. if (substr($this->data->browser->name, 0, strlen($browser)) != $browser) {
  28. $this->data->browser->name = $browser;
  29. $this->data->browser->version = null;
  30. $this->data->browser->stock = false;
  31. } else {
  32. $this->data->browser->name = $browser;
  33. }
  34. }
  35. }
  36. /* The X-Requested-With header is only send from Android devices */
  37. if (!isset($this->data->os->name) || ($this->data->os->name != 'Android' && (!isset($this->data->os->family) || $this->data->os->family->getName() != 'Android'))) {
  38. $this->data->os->name = 'Android';
  39. $this->data->os->alias = null;
  40. $this->data->os->version = null;
  41. $this->data->device->manufacturer = null;
  42. $this->data->device->model = null;
  43. $this->data->device->identified = Constants\Id::NONE;
  44. if ($this->data->device->type != Constants\DeviceType::MOBILE && $this->data->device->type != Constants\DeviceType::TABLET) {
  45. $this->data->device->type = Constants\DeviceType::MOBILE;
  46. }
  47. }
  48. /* The X-Requested-With header is send by the WebKit or Chromium Webview */
  49. if (!isset($this->data->engine->name) || ($this->data->engine->name != 'Webkit' && $this->data->engine->name != 'Blink')) {
  50. $this->data->engine->name = 'Webkit';
  51. $this->data->engine->version = null;
  52. }
  53. }
  54. }