Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.16 KB | None | 0 0
  1. <?php
  2.  
  3.     class BattleNetGuessProductCode implements Iterator {
  4.    
  5.         private $_type;
  6.         private $_code;
  7.         private $_guessCharOffset;
  8.         private $_position;
  9.         private $_guessStr = "ABCDEFGHIJKLMNOPQRSTUVXYWZ0123456789";
  10.    
  11.         function __construct($code)
  12.         {
  13.             //if (preg_match('/\A(?P<code>(?:[^-]{6})-(?:[^-]{4})-(?:[^-]{6})-(?:[^-]{4})-(?:[^-]{6}))\Z/', $code)) {
  14.             if (preg_match('/\A(?<![\w-])(?P<code>(?:[\w\*\#\?]*)-(?:[\w\*\#\?]*)-(?:[\w\*\#\?]*)-(?:[\w\*\#\?]*)-(?:[\w\*\#\?]*))(?![\w-])/', $code)) {
  15.                
  16.                 // Change any odd characters to unknown char
  17.                 $patterns = array();
  18.                 $patterns[] = '/-/';
  19.                 $patterns[] = '/zero/';
  20.                 $patterns[] = '/one/';
  21.                 $patterns[] = '/two/';
  22.                 $patterns[] = '/three/';
  23.                 $patterns[] = '/four/';
  24.                 $patterns[] = '/five/';
  25.                 $patterns[] = '/six/';
  26.                 $patterns[] = '/seven/';
  27.                 $patterns[] = '/eight/';
  28.                 $patterns[] = '/nine/';            
  29.                 $patterns[] = '/[^-A-Z0-9]/';
  30.                
  31.                
  32.                 $replacements = array();
  33.                 $replacements[] = '';
  34.                 $replacements[] = '0';
  35.                 $replacements[] = '1';
  36.                 $replacements[] = '2';
  37.                 $replacements[] = '3';
  38.                 $replacements[] = '4';
  39.                 $replacements[] = '5';
  40.                 $replacements[] = '6';
  41.                 $replacements[] = '7';
  42.                 $replacements[] = '8';
  43.                 $replacements[] = '9';
  44.                 $replacements[] = '*';
  45.                                
  46.                 $code = preg_replace($patterns, $replacements, $code);
  47.  
  48.                 if (!preg_match('/\A(?P<code>(?:[A-Z0-9\*]{26}))\Z/', $code)) {
  49.                     throw new BattleNetCodeInvalidException($code);
  50.                 }
  51.                
  52.                 if (substr_count($code, "*") == 0)
  53.                 {
  54.  
  55.                     $this->_code = $code;
  56.                     $this->_type = BattleNetGuessProductCodeType::SINGLE;
  57.                 }
  58.                 else               
  59.                 {
  60.                     $this->_type = BattleNetGuessProductCodeType::MULTI;
  61.                    
  62.                     if (substr_count($code, "*") > 2)
  63.                         throw new Exception("Too many unknowns");
  64.                        
  65.                     for ($i=0;$i<substr_count($code, "*");$i++)
  66.                     {
  67.                         $offset = ($i > 0) ? $this->_guessCharOffset[$i-1]+1 : 0;
  68.                         $this->_guessCharOffset[] = strpos($code, "*", $offset);
  69.                     }
  70.                        
  71.                     $this->_code = $code;
  72.                 }                          
  73.                
  74.             } else {
  75.                 throw new Exception("Not a valid BattleNet product code");
  76.             }
  77.            
  78.             $this->_position = 0;
  79.         }
  80.        
  81.         function getType()
  82.         {
  83.             return $this->_type;
  84.         }
  85.  
  86.         function rewind() {
  87.             $this->_position = 0;
  88.         }
  89.  
  90.         function current() {
  91.             if (!$this->_guessCharOffset)
  92.                 return new BattleNetProductCode($this->_code);
  93.             else
  94.                 return new BattleNetProductCode($this->getGuessCode($this->_position));
  95.         }      
  96.  
  97.         function key() {
  98.             return $this->_position;
  99.         }
  100.  
  101.         function next() {
  102.             ++$this->_position;
  103.         }
  104.  
  105.         function valid() {
  106.        
  107.             if (!$this->_guessCharOffset)
  108.                 return $this->_position < 1;
  109.             else
  110.                 return $this->_position < pow(strlen($this->_guessStr), count($this->_guessCharOffset));
  111.  
  112.         }      
  113.        
  114.         function getGuessCode($offset)
  115.         {
  116.             $str = $this->_code;
  117.             foreach ($this->_guessCharOffset as $index => $charOffset)
  118.             {
  119.  
  120.                 $str[$charOffset] = $this->_guessStr[($offset / pow(strlen($this->_guessStr), $index)) % strlen($this->_guessStr)];
  121.                    
  122.             }
  123.             return $str;
  124.         }
  125.        
  126.         function get()
  127.         {
  128.             return $this->_code;
  129.         }      
  130.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement