Просмотр файла yahoo/includes/classes/shopping_search_class.php

Размер файла: 5.73Kb
<?

  /*

* Animated Collapsible DIV- © Dynamic Drive DHTML code library (www.pokazuha.tv)

* This notice MUST stay intact for legal use

* Visit Dynamic Drive at http://www.pokazuha.tv/ for this script and 100s more

* Last Update	: Tuesday, November 13 2005


*/

class ShoppingSearch
{
  var $m_url = 'froogle?q={keyword}&sa=N';
  var $m_host = "64.233.187.99";

  var $m_settings;
  var $m_total_found_string;
  var $m_total_found_int;
  var $m_keywords;
  var $m_pager;
  var $m_is_no_resuls = 0;

  function ShoppingSearch($settings,$keywords)
  {
    $this->m_settings = $settings;
    $this->m_keywords = $keywords;
  }
  function file_get_contents($url)
  {
    /*if (check_if_bad_request($this->m_keywords))
    {
      return '';
    }*/
    if (_DEBUG == 1)
      return file_get_contents($url);
    $out = "GET /$url HTTP/1.1\r\n";
    $out .= "Host: {$this->m_host}\r\n";
    $out .= "User-Agent: Mozilla 4.0\r\n";
    $out .= "Connection: close\r\n\r\n";

    $h = fsockopen($this->m_host,80);
    fwrite($h,$out);
    for($a = 0,$r = '';!$a;)
    {
       $b = fread($h,8192);
       $r .= $b;
       $a = (($b=='') ? 1 : 0);
    }
    fclose($h);
    return $r;
  }

  function get_url()
  {
    if (_DEBUG == 1)
      return 'froogle_new/Froogle Search test.htm';
    else
    {
      $this->m_url = str_replace('{keyword}',$this->m_keywords,$this->m_url);
      $curr_page = isset($_GET['pos']) ? $_GET['pos'] : 1;
      $sort = isset($_REQUEST['sort']) ? $_REQUEST['sort'] : '';
      if ($sort == 'p')
      {
        $scoring = '&scoring=p';
      }
      elseif ($sort == 'pd')
      {
        $scoring = '&scoring=pd';
      }
      else
      {
        $scoring = '';
      }
      if ($curr_page != 1)
      {
         $curr_page = $curr_page * 10 - 10;
         $this->m_url .= "&start=$curr_page";
      }
      $this->m_url .= $scoring;
      return $this->m_url;
    }
  }
  function get_total_found_string($content)
  {
    if ($this->m_is_no_resuls == 1)
      return "";
    preg_match('/(Results.*)for/Ui',$content,$ret);
    preg_match_all('/<b>(.*)<\/b>/Ui',$ret[1],$int);
    $this->m_total_found_int = (int)str_replace(',','',$int[1][2]);
    return $ret[1];
  }

  function get_results()
  {
    $url = $this->get_url();
    $content = $this->file_get_contents($url);
    $content = str_replace("\r\n"," ",$content);
    $content = str_replace("\n"," ",$content);
    //Get total found string
    $this->m_total_found_string = $this->get_total_found_string($content);
    //Init pager
    $this->m_pager = new Pager($this->m_total_found_int,10,'shopping',$this->m_keywords);

    $ret_data = '
    <div style="padding-left:20px;padding-top:20px;">
    ';
    $sort = isset($_REQUEST['sort']) ? $_REQUEST['sort'] : '';
    if ($sort == 'p')
    {
      $scoring = '&scoring=p';
      $sort_string = '<a href="?type=shopping&search='.htmlspecialchars($_REQUEST['search']).'&sort=">Best match</a> - <b>Low to high</b> - <a href="?type=shopping&search='.htmlspecialchars($_REQUEST['search']).'&sort=pd">High to low</a>';
    }
    elseif ($sort == 'pd')
    {
      $scoring = '&scoring=pd';
      $sort_string = '<a href="?type=shopping&search='.htmlspecialchars($_REQUEST['search']).'&sort=">Best match</a> - <a href="?type=shopping&search='.htmlspecialchars($_REQUEST['search']).'&sort=p">Low to high</a> - <b>High to low</b>';
    }
    else
    {
      $scoring = '';
      $sort_string = '<b>Best match</b> - <a href="?type=shopping&search='.htmlspecialchars($_REQUEST['search']).'&sort=p">Low to high</a> - <a href="?type=shopping&search='.htmlspecialchars($_REQUEST['search']).'&sort=pd">High to low</a>';
    }
    //Get
   if ($cnt = preg_match_all('|<TABLE cellSpacing=0 cellPadding=0>(.*)</TABLE><br>|Ui',$content,$ret))
    {
      $ret_data .=  '<div align="right"><b><font color="#FF8040">Сортировка:</font></b>&nbsp;'.$sort_string."</div>";
      foreach($ret[0] as $index=>$value)
      {
        $value = preg_replace('|<form.*>.*</form>|Ui','',$value);
        $value = preg_replace('|<DIV class=x>.*</div>|Ui','',$value);
        preg_match_all('|<a.*>(<img.*>.*)</a>|Ui',$value,$td);
        foreach($td[0] as $i=>$td_data)
        {
          $value = str_replace($td[0][$i],$td[1][$i],$value);
        }
        $value = str_replace("/froogle_image","http://64.233.187.99/froogle_image",$value);
        $br = explode("<br>",$value);
        //Title
        preg_match('|<a.*href=".*\?q=(.*)".*>(.*)</a>|Ui',$br[0],$title_value);
        $href = 'href="http://64.233.187.99/froogle_url?q='.($title_value[1]).'" onMouseOver="window.status=\''.($title_value[1]).'\';return true" onMouseOut="window.status=\'\';return true"';
        $ret_data .= str_replace($title_value[0],'<a '.$href.' target="_new" alt="alt">'.$title_value[2].'</a>',$br[0])."<br>";
        //Price
        preg_match('|<FONT.*size=-1>(.*)<A|Ui',$br[1],$price);
        $ret_data .= $price[1].'</FONT><br>';
        //Description
        $ret_data .= $br[2].'<br>';
        //Compare items
        preg_match('|<a.*href=".*pid=(.*)".*class="cpn">(.*)</a>|Ui',$value,$compare);
        $ret_data .= '<a href="?type=compare&search='.htmlspecialchars($_REQUEST['search']).'&pid='.$compare[1].'">'.$compare[2].'</a>';
        $ret_data .= '</td></tr></TABLE><br>';
      }
    }
    else
     {
        $this->m_is_no_resuls = 1;
        return $this->m_settings->get_noresults_string();
     }
    $pager = $this->m_pager->get_pager();
    $ret_data .= '
    </div>
    <div style="padding-left:20px;padding-top:10px;">
    <DIV class=rnav><SPAN class=rnavLabel>Страницы:&nbsp;'.$pager.'</span></div>
    </div>
    ';
    return $ret_data;
  }
}
?>