<?php

class preventDuplicates {

  var $getValues = array('sort', 'page', 'language', 'currency');
  var $IhaveDuplicateContent = false;
  var $turnServiceOn = false; // true turns on false will turn this service off
  var $meta;
  var $caught;
  var $addedMeta;
  var $finalMeta;
  var $noIndex = '<meta name="ROBOTS" content="NOINDEX, FOLLOW" />';

  function preventDuplicates(){
  }
  /**
  * Sets $this->finalMeta (Final meta to be output based on the methods and properties below
  *
  * @param mixed $meta - $meta - contains the buffered title/meta info prior to being altered by this class
  * @method - $this->targetsExist() - Returns true or false based on whether $getValues was found in $_GET
  * @method - $this->parseMeta() - Ultimately sets $this->finalMeta;
  */
  function checkTarget($meta) {

    $this->meta = $meta;
    if( $this->targetsExist() && (false !== $this->turnServiceOn) ){
      if( false !== $this->IhaveDuplicateContent ){
      $this->parseMeta();
      } else {
        $this->finalMeta = $this->meta . "\n" . $this->noIndex;
      }
    } else {
      $this->finalMeta = $this->meta;
    }
  }
  /**
  * Checks whether any $this->getValues exist in the $_GET array
  *
  * @param - $this->caught - set as an array of keys found in $_GET
  * @return - Returns true or false, if true $this->caught is also set as an array
  */
  function targetsExist(){
    $caught = array();
    foreach( $this->getValues as $value ){
      if( isset($_GET[$value]) ){
        $caught[] = tep_output_string_protected($value);
      }
  }
  if( !empty($caught)) {
    $this->caught = $caught;
    return true;
  } else{
    return false;
  }
  }
  /**
  * Sets $this->addedMeta which is a string made up of matched $_GET key value pairs
  * Key value pairs are seperated with _ (underscore) seperate $_GET are se[erated with - (hyphen)
  *
  * @param $this->addedMeta -  A string made up of matched $_GET key value pairs
  * @method $this->performPCRE() - performs PCRE replace operations on the buffered $meta
  *
  */
  function parseMeta(){

    $addedMeta = '';
    $count = count($this->caught);
    for( $i=0; $i<$count; $i++ ){
      $addedMeta .= $this->caught[$i] . '_' . tep_sanitize_string(tep_output_string_protected($_GET[$this->caught[$i]])) . '-';
    }
    $this->addedMeta = $addedMeta;
    $this->performPCRE();
  }
  /**
  * PCRE replace operations to inject added meta info based on matched $_GET variables
  *
  * @param $this->finalMeta - Sets $this->finalMeta which contains the modified meat data
  */
  function performPCRE() {

    $pattern[]   = '@<title>\s*@i';
    $pattern[]   = '@<meta\s*name\s*=\s*"\s*description\s*"\s*content\s*=\s*"\s*@i';
    $pattern[]   = '@<meta\s*name\s*=\s*"\s*keywords\s*"\s*content\s*=\s*"\s*@i';
    $replace[]   = '<title>' . str_replace('-', ' | ', $this->addedMeta);
    $replace[]   = '<meta name="description" content="' . str_replace('-', ' ', $this->addedMeta);
    $replace[]   = '<meta name="keywords" content="' . str_replace('-', ', ', $this->addedMeta);

    $this->finalMeta = preg_replace($pattern, $replace, $this->meta);
  }

} //End preventDuplicates class
?>