PageRenderTime 78ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Faker/Provider/UserAgent.php

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