<?php

/**
 * Http website owner veryfication
 * 
 * @version: 1.0.0
 * @package: Vframe
 * @subpackage: VframeLib
 * @license: GNU
 * @author: Piotr 'Athlan' Pelczar
 * @copyright: Copyright (c) 2007 Vgroup Technologies PL Inc.
 * @url: http://www.vgroup.pl
 * @url: http://framework.vgroup.pl
 * @url: http://athlan.pl
 * 
 */

final class VframeLib_WebVeryfication
{
  public static function 
Hash($sUrl)
  {
    return 
md5(base64_encode($sUrl) . sha1(substr($sUrl, -12)));
  }
  
  public static function 
Metatag($sHash$sTagName)
  {
    return 
'<meta name="' $sTagName '" content="' $sHash '" />';
  }
  
  public function 
CheckFile($sUrl$sFilename)
  {
    try
    {
      return (
self::Hash($sUrl) == self::_Request($sUrl $sFilename));
    }
    catch(
VframeLib_WebVeryfication_Exception $oE)
    {
      return 
false;
    }
  }
  
  public function 
CheckMetatag($sUrl$sTagName)
  {
    try
    {
      return (bool) 
preg_match('/' str_replace('/''\/'preg_quote(self::Metatag(self::Hash($sUrl), $sTagName))) . '/i'self::_Request($sUrl));
    }
    catch(
VframeLib_WebVeryfication_Exception $oE)
    {
      return 
false;
    }
  }
  
  protected static function 
_Request($sUrl)
  {
    try
    {
      
$oRequest = new HttpRequest($sUrl);
      
$oRequest->setOptions(array
      (
        
'redirect' => 5,
      ));
      
      
$oRequest->send();
      
      if((
$iCode $oRequest->getResponseCode()) != 200)
        throw new 
VframeLib_WebVeryfication_Exception('Response code: ' $iCode ', while 200 expected.');
      
      return 
$oRequest->getResponseBody();
    }
    catch(
Exception $oE)
    {
      throw new 
VframeLib_WebVeryfication_Exception($oE->getMessage());
    }
  }
}

// for einvirionment not included in Vframe2 framework
if(!class_exists('Vframe_Exception'false)) { class Vframe_Exception extends Exception {} }

class 
VframeLib_WebVeryfication_Exception extends Vframe_Exception {}

?>