PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Faker/Provider/UserAgent.php

http://github.com/fzaninotto/Faker
PHP | 165 lines | 90 code | 26 blank | 49 comment | 3 complexity | b556ff014672c076a6b146aaca0d0413 MD5 | raw file
  1. <?php
  2. namespace Faker\Provider;
  3. class UserAgent extends Base
  4. {
  5. protected static $userAgents = array('firefox', 'chrome', 'internetExplorer', 'opera', 'safari');
  6. protected static $windowsPlatformTokens = array(
  7. 'Windows NT 6.2', 'Windows NT 6.1', 'Windows NT 6.0', 'Windows NT 5.2', 'Windows NT 5.1',
  8. 'Windows NT 5.01', 'Windows NT 5.0', 'Windows NT 4.0', 'Windows 98; Win 9x 4.90', 'Windows 98',
  9. 'Windows 95', 'Windows CE'
  10. );
  11. /**
  12. * Possible processors on Linux
  13. */
  14. protected static $linuxProcessor = array('i686', 'x86_64');
  15. /**
  16. * Mac processors (it also added U;)
  17. */
  18. protected static $macProcessor = array('Intel', 'PPC', 'U; Intel', 'U; PPC');
  19. /**
  20. * Add as many languages as you like.
  21. */
  22. protected static $lang = array('en-US', 'sl-SI');
  23. /**
  24. * Generate mac processor
  25. *
  26. * @return string
  27. */
  28. public static function macProcessor()
  29. {
  30. return static::randomElement(static::$macProcessor);
  31. }
  32. /**
  33. * Generate linux processor
  34. *
  35. * @return string
  36. */
  37. public static function linuxProcessor()
  38. {
  39. return static::randomElement(static::$linuxProcessor);
  40. }
  41. /**
  42. * Generate a random user agent
  43. *
  44. * @example 'Mozilla/5.0 (Windows CE) AppleWebKit/5350 (KHTML, like Gecko) Chrome/13.0.888.0 Safari/5350'
  45. */
  46. public static function userAgent()
  47. {
  48. $userAgentName = static::randomElement(static::$userAgents);
  49. return static::$userAgentName();
  50. }
  51. /**
  52. * Generate Chrome user agent
  53. *
  54. * @example 'Mozilla/5.0 (Macintosh; PPC Mac OS X 10_6_5) AppleWebKit/5312 (KHTML, like Gecko) Chrome/14.0.894.0 Safari/5312'
  55. */
  56. public static function chrome()
  57. {
  58. $saf = mt_rand(531, 536) . mt_rand(0, 2);
  59. $platforms = array(
  60. '(' . static::linuxPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/" . mt_rand(36, 40) . '.0.' . mt_rand(800, 899) . ".0 Mobile Safari/$saf",
  61. '(' . static::windowsPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/" . mt_rand(36, 40) . '.0.' . mt_rand(800, 899) . ".0 Mobile Safari/$saf",
  62. '(' . static::macPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/" . mt_rand(36, 40) . '.0.' . mt_rand(800, 899) . ".0 Mobile Safari/$saf"
  63. );
  64. return 'Mozilla/5.0 ' . static::randomElement($platforms);
  65. }
  66. /**
  67. * Generate Firefox user agent
  68. *
  69. * @example 'Mozilla/5.0 (X11; Linuxi686; rv:7.0) Gecko/20101231 Firefox/3.6'
  70. */
  71. public static function firefox()
  72. {
  73. $ver = 'Gecko/' . date('Ymd', mt_rand(strtotime('2010-1-1'), time())) . ' Firefox/' . mt_rand(35, 37) . '.0';
  74. $platforms = array(
  75. '(' . static::windowsPlatformToken() . '; ' . static::randomElement(static::$lang) . '; rv:1.9.' . mt_rand(0, 2) . '.20) ' . $ver,
  76. '(' . static::linuxPlatformToken() . '; rv:' . mt_rand(5, 7) . '.0) ' . $ver,
  77. '(' . static::macPlatformToken() . ' rv:' . mt_rand(2, 6) . '.0) ' . $ver
  78. );
  79. return "Mozilla/5.0 " . static::randomElement($platforms);
  80. }
  81. /**
  82. * Generate Safari user agent
  83. *
  84. * @example 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_7_1 rv:3.0; en-US) AppleWebKit/534.11.3 (KHTML, like Gecko) Version/4.0 Safari/534.11.3'
  85. */
  86. public static function safari()
  87. {
  88. $saf = mt_rand(531, 535) . '.' . mt_rand(1, 50) . '.' . mt_rand(1, 7);
  89. if (mt_rand(0, 1) == 0) {
  90. $ver = mt_rand(4, 5) . '.' . mt_rand(0, 1);
  91. } else {
  92. $ver = mt_rand(4, 5) . '.0.' . mt_rand(1, 5);
  93. }
  94. $mobileDevices = array(
  95. 'iPhone; CPU iPhone OS',
  96. 'iPad; CPU OS'
  97. );
  98. $platforms = array(
  99. '(Windows; U; ' . static::windowsPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Version/$ver Safari/$saf",
  100. '(' . static::macPlatformToken() . ' rv:' . mt_rand(2, 6) . '.0; ' . static::randomElement(static::$lang) . ") AppleWebKit/$saf (KHTML, like Gecko) Version/$ver Safari/$saf",
  101. '(' . static::randomElement($mobileDevices) . ' ' . mt_rand(7, 8) . '_' . mt_rand(0, 2) . '_' . mt_rand(1, 2) . ' like Mac OS X; ' . static::randomElement(static::$lang) . ") AppleWebKit/$saf (KHTML, like Gecko) Version/" . mt_rand(3, 4) . ".0.5 Mobile/8B" . mt_rand(111, 119) . " Safari/6$saf",
  102. );
  103. return "Mozilla/5.0 " . static::randomElement($platforms);
  104. }
  105. /**
  106. * Generate Opera user agent
  107. *
  108. * @example 'Opera/8.25 (Windows NT 5.1; en-US) Presto/2.9.188 Version/10.00'
  109. */
  110. public static function opera()
  111. {
  112. $platforms = array(
  113. '(' . static::linuxPlatformToken() . '; ' . static::randomElement(static::$lang) . ') Presto/2.' . mt_rand(8, 12) . '.' . mt_rand(160, 355) . ' Version/' . mt_rand(10, 12) . '.00',
  114. '(' . static::windowsPlatformToken() . '; ' . static::randomElement(static::$lang) . ') Presto/2.' . mt_rand(8, 12) . '.' . mt_rand(160, 355) . ' Version/' . mt_rand(10, 12) . '.00'
  115. );
  116. return "Opera/" . mt_rand(8, 9) . '.' . mt_rand(10, 99) . ' ' . static::randomElement($platforms);
  117. }
  118. /**
  119. * Generate Internet Explorer user agent
  120. *
  121. * @example 'Mozilla/5.0 (compatible; MSIE 7.0; Windows 98; Win 9x 4.90; Trident/3.0)'
  122. */
  123. public static function internetExplorer()
  124. {
  125. return 'Mozilla/5.0 (compatible; MSIE ' . mt_rand(5, 11) . '.0; ' . static::windowsPlatformToken() . '; Trident/' . mt_rand(3, 5) . '.' . mt_rand(0, 1) . ')';
  126. }
  127. public static function windowsPlatformToken()
  128. {
  129. return static::randomElement(static::$windowsPlatformTokens);
  130. }
  131. public static function macPlatformToken()
  132. {
  133. return 'Macintosh; ' . static::randomElement(static::$macProcessor) . ' Mac OS X 10_' . mt_rand(5, 8) . '_' . mt_rand(0, 9);
  134. }
  135. public static function linuxPlatformToken()
  136. {
  137. return 'X11; Linux ' . static::randomElement(static::$linuxProcessor);
  138. }
  139. }