phpDocumentor InternetAccessLog
[ class tree: InternetAccessLog ] [ index: InternetAccessLog ] [ all elements ]

Source for file RandomElements.class.php

Documentation is available at RandomElements.class.php

  1. <?php
  2. /**
  3.  *  RandomElements.class.php
  4.  *  File with the class used to generate random elements (users, URL's ...)
  5.  *  @author José Manuel Ciges Regueiro <jmanuel@ciges.net>, Web page {@link http://www.ciges.net}
  6.  *  @license http://www.gnu.org/copyleft/gpl.html GNU GPLv3
  7.  *  @version 20130313
  8.  *
  9.  *  @package InternetAccessLog
  10.  *  @filesource
  11.  */
  12. /**
  13.  *  This class is used to generate random elements (users, IP's and URL's) to make tests (populate tables in MySQL or collections in MongoDB)
  14.  *  @package InternetAccessLog
  15.  */
  16. class RandomElements {
  17.     /**
  18.      * List of possible HTTP Methods
  19.      * @access private
  20.      * @var array 
  21.      */
  22.     private $listOfHTTPMethods = array("CONNECT", "GET", "HEAD", "MKCOL", "OPTIONS", "POST", "PROPFIN", "PUT");
  23.     /**
  24.      * List of possible FTP Methods
  25.      * @access private
  26.      * @var array 
  27.      */
  28.     private $listOfFTPMethods = array("NOOP", "PWD", "RETR", "SIZE", "STOR", "TYPE", "USER");
  29.     /**
  30.      * List of possible protocolos
  31.      * @access private
  32.      * @var array 
  33.      */
  34.     private $listOfProtocols = array("http", "tunn");
  35.     /**
  36.      * List of possible first level internet domains
  37.      * @access private
  38.      * @var array 
  39.      */
  40.     private $listOfInternetDomains = array(".ar", ".asia", ".biz", ".bo", ".cat", ".cl", ".co", ".cn", ".com", ".cr", ".do", ".ec", ".edu", ".es", ".eu", ".fm", ".fr", ".gov", ".gt", ".hn", ".info", ".int", ".jobs", ".lat", ".mil", ".mobi", ".museum", ".mx", ".ni", ".name", ".net", ".nl", ".org", ".pe", ".pro", ".py", ".ru", ".sv", ".tel", ".tk", ".travel", ".tv", ".ua", ".uy", ".ve", ".web", ".ws", ".xxx", "r", ".asia", ".biz", ".bo", ".cat", ".cl", ".co", ".cn", ".com", ".cr", ".do", ".ec", ".edu", ".es", ".eu", ".fm", ".fr", ".gov", ".gt", ".hn", ".info", ".int", ".jobs", ".lat", ".mil", ".mobi", ".museum", ".mx", ".ni", ".name", ".net", ".nl", ".org", ".pe", ".pro", ".py", ".ru", ".sv", ".tel", ".tk", ".travel", ".tv", ".ua", ".uy", ".ve", ".web", ".ws", ".xxx");
  41.     /**
  42.      * List of possible return codes
  43.      * @access private
  44.      * @var array 
  45.      */
  46.     private $listOfRetourCodes = array(200, 201, 207, 301, 304, 403, 404, 405, 500, 502);
  47.  
  48.     /**
  49.      * Returns a random user, composed of one letter and 6 numbers
  50.      * @return string 
  51.      */
  52.     function getRandomUser() {
  53.         $letter = "abcdefghijklmnopqrstuvwxyz";
  54.         $user = $letter{mt_rand(0, 25)}.sprintf("%06d", mt_rand(1,999999));
  55.  
  56.         return ucfirst($user);
  57.     }
  58.  
  59.     /**
  60.      * Returns a random IP (string with 4 numbers between 0 and 255). No check for validity is made
  61.      * @return string 
  62.      */
  63.     function getRandomIP()    {
  64.         $ip = mt_rand(0,255).".".mt_rand(0,255).".".mt_rand(0,255).".".mt_rand(0,255);
  65.         return $ip;
  66.     }
  67.  
  68.     /**
  69.      * Returns a semi random HTTP method (90% will be GET)
  70.      * @return string 
  71.      */
  72.     function getRandomHTTPMethod()    {
  73.         if (mt_rand(0,9) == 9)    {
  74.             return $this->listOfHTTPMethods[array_rand($this->listOfHTTPMethods)];
  75.         }
  76.         else
  77.             {
  78.             return "GET";
  79.         }
  80.         return ;
  81.     }
  82.  
  83.     /**
  84.      * Returns a semi random HTTP method (90% will be RETR)
  85.      * @return string 
  86.      */
  87.     function getRandomFTPMethod()    {
  88.             if (mt_rand(0,9) == 9)    {
  89.                     return $this->listOfFTPMethods[array_rand($this->listOfFTPMethods)];
  90.             }
  91.             else
  92.                     {
  93.                     return "RETR";
  94.             }
  95.             return ;
  96.     }
  97.  
  98.     /**
  99.      * Returns a random protocol (90% will be http)
  100.      * @return string 
  101.      */
  102.     function getRandomProtocol()    {
  103.         if (mt_rand(0,9) == 9)    {
  104.             return $this->listOfProtocols[array_rand($this->listOfProtocols)];
  105.         }
  106.         else
  107.             {
  108.             return "http";
  109.         }
  110.     }
  111.  
  112.     /**
  113.      * Returns a random Internet domain (a www followed by a random string ended by a valid internet
  114.      * @return string 
  115.      */
  116.     function getRandomDomain()    {
  117.         $word = range('a', 'z');
  118.         $len = mt_rand(1, count($word));
  119.         shuffle($word);
  120.         $domain = "www.".substr(implode($word), 0, $len).$this->listOfInternetDomains[mt_rand(0,count($this->listOfInternetDomains)-1)];
  121.  
  122.         return $domain;
  123.     }
  124.  
  125.     /**
  126.      * Returns a random string of the length demanded (10 characters by default)
  127.      * @param integer $length 
  128.      * @return string 
  129.      */
  130.     function getRandomString($length = 10)    {
  131.         $res = array_merge(range('a', 'z'), range('A', 'Z'), range('0', '9'), array("/"));
  132.         $len = mt_rand(0, $length);
  133.         shuffle($res);
  134.  
  135.         return substr(implode($res), 0, $length);
  136.     }
  137.  
  138.     /**
  139.      * Returns a semirandom return code (90% are 200 return code)
  140.      * @return integer 
  141.      */
  142.     function getRandomRetourCode()    {
  143.         if (mt_rand(0,9) == 9)    {
  144.             return $this->listOfRetourCodes[array_rand($this->listOfRetourCodes)];
  145.         }
  146.         else
  147.             {
  148.             return 200;
  149.         }
  150.     }
  151.  
  152.     /**
  153.      * Return a random size between 0 and 50K
  154.      * @return integer 
  155.      */
  156.     function getRandomSize()    {
  157.         return mt_rand(0,50*1024);
  158.     }
  159.  
  160.     /**
  161.      * Return a random date between the two dates passes as parameters. Parameters and return value are English textual datetime descriptions 'Y-m-d H:i:s'
  162.      * @param string $mindate  English textual datetime description 'Y-m-d H:i:s' of minimun date
  163.      * @param string $maxdate  English textual datetime description 'Y-m-d H:i:s' of maximun date
  164.      * @return string 
  165.      */
  166.     function getRandomDate($mindate, $maxdate)   {
  167.         $min = strtotime($mindate);
  168.         $max = strtotime($maxdate);
  169.         return date('Y-m-d H:i:s', rand($min, $max));
  170.     }
  171.  
  172. }
  173.  
  174. ?>

Documentation generated on Fri, 12 Apr 2013 12:02:21 +0200 by phpDocumentor 1.4.4