示例#1
0
 /**
  * @param \Novutec\WhoisParser\Result\Result $previousResult
  * @param $rawdata
  * @param string|object $query
  */
 public function parse($previousResult, $rawdata, $query)
 {
     $this->result = new Result();
     $parsedAvailable = $this->parseAvailable($rawdata, $this->result);
     $yaml = new Parser();
     $v = $yaml->parse($rawdata);
     $this->flattenYaml($v);
     $this->reformatData();
     $parseMatches = $this->parseKeyValues($this->result, $this->data, $this->regexKeys, true);
     if (strlen($this->availabilityField)) {
         $availabilityValue = null;
         if (isset($this->result->{$this->availabilityField})) {
             $availabilityValue = $this->result->{$this->availabilityField};
         }
         if ($availabilityValue !== null) {
             $status = null;
             if (is_array($availabilityValue) && count($availabilityValue) == 1) {
                 // Copy the array first so we don't affect the result
                 $statusArr = $availabilityValue;
                 $status = array_shift($statusArr);
             } else {
                 if (!is_array($availabilityValue) && strlen($availabilityValue) > 1) {
                     $status = $availabilityValue;
                 }
             }
             if (strlen($status)) {
                 $status = strtolower($status);
                 $isRegistered = null;
                 if (in_array($status, $this->availabilityValues)) {
                     $isRegistered = false;
                 }
                 if ($isRegistered !== null) {
                     $parsedAvailable = true;
                     $this->result->addItem('registered', $isRegistered);
                 }
             }
         }
     }
     if ($parseMatches < 1 && !$parsedAvailable) {
         throw new ReadErrorException("Template " . get_class($this) . " did not correctly parse the response");
     }
     $previousResult->mergeFrom($this->result);
 }
示例#2
0
 /**
  * Parses rawdata from whois server and call postProcess if exists afterwards
  * 
  * @throws NoTemplateException
  * @throws RateLimitException
  * @return void
  */
 private function parse()
 {
     $Config = $this->Config->getCurrent();
     $Template = AbstractTemplate::factory($Config['template'], $this->customTemplateNamespace);
     // If Template is null then we do not have a template for that, but we
     // can still proceed to the end with just the rawdata
     if ($Template instanceof AbstractTemplate) {
         $this->Result->template[$Config['server']] = $Config['template'];
         $this->rawdata = $Template->translateRawData($this->rawdata, $Config);
         try {
             $Template->parse($this->Result, $this->rawdata, $this->Query);
         } catch (RateLimitException $e) {
             $server = $Config['server'];
             if (!in_array($server, $this->rateLimitedServers)) {
                 $this->rateLimitedServers[] = $server;
             }
             throw new RateLimitException("Rate limit exceeded for server: " . $server);
         }
         // set rawdata to Result - this happens here because sometimes we
         // have to fix the rawdata as well in postProcess
         $this->Result->addItem('rawdata', $this->rawdata);
         // set registered to Result
         $this->Result->addItem('registered', isset($this->Result->registered) ? $this->Result->registered : false);
         if (!isset($this->Result->whoisserver)) {
             $this->Result->addItem('whoisserver', $Config['server']);
         }
         // start post processing
         $Template->postProcess($this);
         // set name to Result
         if (isset($this->Query->tld) && !isset($this->Query->fqdn)) {
             $this->Result->addItem('name', $this->Query->tld);
         } elseif (isset($this->Query->ip)) {
             $this->Result->addItem('name', $this->Query->ip);
         } elseif (isset($this->Query->asn)) {
             $this->Result->addItem('name', $this->Query->asn);
         } else {
             $this->Result->addItem('name', $this->Query->fqdn);
             $this->Result->addItem('idnName', $this->Query->idnFqdn);
         }
     } else {
         throw new NoTemplateException('Template ' . $Config['template'] . ' could not be found');
     }
 }
示例#3
0
 /**
  * @param \Novutec\WhoisParser\Result\Result $result
  * @param $rawdata
  * @param string|object $query
  */
 public function parse($result, $rawdata, $query)
 {
     $this->parseRateLimit($rawdata);
     // check if there is a block to be cutted from HTML response
     if (isset($this->htmlBlock)) {
         preg_match($this->htmlBlock, $rawdata, $htmlMatches);
         if (isset($htmlMatches[0])) {
             $rawdata = preg_replace('/\\s\\s+/', "\n", $htmlMatches[0]);
         }
     }
     // lookup all blocks of template
     foreach ($this->blocks as $blockKey => $blockRegEx) {
         // try to match block regex against WHOIS rawdata
         if (!preg_match_all($blockRegEx, $rawdata, $blockMatches)) {
             continue;
         }
         // use matched block to lookup for blockItems
         foreach ($blockMatches[0] as $item) {
             foreach ($this->blockItems[$blockKey] as $itemRegEx => $target) {
                 // try to match blockItem regex against block
                 if (preg_match_all($itemRegEx, $item, $itemMatches)) {
                     // set matched items to Result
                     $value = end($itemMatches);
                     if (is_array($value)) {
                         foreach ($value as $k => $v) {
                             if ($this->convertFromHtml) {
                                 $value[$k] = html_entity_decode(strip_tags($v));
                             }
                             $value[$k] = trim($v);
                             if (strlen($v) < 1) {
                                 unset($value[$k]);
                             }
                         }
                         if (count($value) < 1) {
                             continue;
                         }
                     } else {
                         if ($this->convertFromHtml) {
                             $value = html_entity_decode(strip_tags($value));
                         }
                         $value = trim($value);
                         if (strlen($value) < 1) {
                             continue;
                         }
                     }
                     $result->addItem($target, $value);
                 }
             }
         }
     }
     // if there are still contact handles after parsing then
     // these contacts are used for more types e.g. one handle for admin and
     // tech so we are going to clone this matching handles
     if (isset($result->network->contacts)) {
         // lookup all left over handles in network
         foreach ($result->network->contacts as $type => $handle) {
             if (!is_string($handle)) {
                 continue;
             }
             // lookup all contacts in Result
             foreach ($result->contacts as $contactType => $contactArray) {
                 foreach ($contactArray as $contactObject) {
                     // if contact handle in network matches the one in
                     // Result, we have to clone it
                     if (strtolower($contactObject->handle) !== strtolower($handle)) {
                         continue;
                     }
                     if (empty($result->contacts->{$type})) {
                         $result->contacts->{$type} = array();
                     }
                     array_push($result->contacts->{$type}, $contactObject);
                     unset($result->network->contacts->{$type});
                     break 2;
                 }
             }
         }
     }
     $this->parseAvailable($rawdata, $result);
 }
示例#4
0
 /**
  * Parse the raw data using the available regex list
  * @param string $rawdata
  * @param \Novutec\WhoisParser\Result\Result $result
  * @return bool Parsed & matched?
  */
 protected function parseAvailable($rawdata, $result)
 {
     $parsedAvailable = false;
     if (isset($this->available)) {
         if (!is_array($this->available) && strlen($this->available)) {
             $this->available = array($this->available);
         }
         $isRegistered = true;
         if (is_array($this->available)) {
             foreach ($this->available as $availableRegex) {
                 $matches = array();
                 preg_match_all($availableRegex, $rawdata, $matches);
                 if (count($matches[0])) {
                     $parsedAvailable = true;
                     $isRegistered = false;
                 }
             }
         }
         $result->addItem('registered', $isRegistered);
     }
     return $parsedAvailable;
 }