<?php

/** 
Cette classe permet de générer automatiquement des fichiers CSS
pour IE5 et IE5.5 et IE6 en mode quirk afin de corriger
les erreurs d'interprétation de ceux-ci.

@author  : Batiste Bieler
@company : http://dosimple.ch

Copyright (C) 2004  Bieler Batiste

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
 
class BoxModelHack
{

var 
$userAgent;
var 
$hackedIdentifier;
var 
$currentBox;
var 
$hackedFilePathName;
    
    function 
BoxModelHack()
    {
        
$this->getUserAgent();
        
$this->hackedIdentifier='';
    }
    
    function 
setHackedFilePathName($fileName)
    {
        
$this->hackedFilePathName=$fileName;
        
        if ( !
file_exists($fileName) )
        {
            
mkdir ($fileName0777);
        }
        
    }
    
    function 
getUserAgent()
    {
        
$userAgent $_SERVER["HTTP_USER_AGENT"];
        
        if ( 
preg_match'/MSIE/i'$userAgent ) )
        {
            
$this->userAgent->type 'ie';
        }
        else
        {
            
$this->userAgent->type 'non supporté';
        }
        
        if ( 
preg_match'/Mac/i'$userAgent ) )
        {
            
$this->userAgent->plateforme 'mac';
        }
        else
        {
            
$this->userAgent->plateforme 'pc';
        }
        
        
// version du navigateur 
        
if ( $this->userAgent->type =='ie' )
        {
            
preg_match'/(?<=MSIE )([0-9])\.([0-9])/'$userAgent $this->userAgent->version );
        }
        else
        {
            
$this->userAgent->version false;
        }
    }
    
    function 
returnHackedFileName($styleSheetFileName)
    {
        return 
$this->hackedFilePathName.'/'.basename($styleSheetFileName).$this->hackedIdentifier;
    }
    
    function 
returnStyleSheetFileName $styleSheetFileName )
    {
        if ( 
$this->userAgent->type == 'ie' 
            
&& $this->userAgent->plateforme == 'pc' 
            
&& $this->userAgent->version[1] == )
        {
            
            
$hackedFileName $this->returnHackedFileName($styleSheetFileName);
            
            if (  
file_exists $hackedFileName ) )
            {
            
                
$timeOriginal =  filemtime $styleSheetFileName );
                
$timeHacked =  filemtime $hackedFileName  );
                
                if ( 
true /*$timeOriginal > $timeHacked*/ )
                {                
                    
$this->generateHackedFile $styleSheetFileName );
                }
            }
            else
            {
                
$this->generateHackedFile $styleSheetFileName );
            }
            
            return 
$hackedFileName;
            
        }
        else
        {
            return 
$styleSheetFileName;
        }
    }
    
    function 
generateHackedFile $styleSheetFileName )
    {
        
$handleOriginal fopen $styleSheetFileName "r" );
        
$contents fread $handleOriginalfilesize$styleSheetFileName ) );
        
fclose $handleOriginal );
        
        
// suppresion des comentaires
        
$contents preg_replace('#/\*(.*?)\*/#','',$contents);
        
        
$contents preg_replace_callback'#\{(.*?)\}#s'
            array(&
$this,'searchAndReplaceWidthAndPadding') ,$contents);
        
$contents preg_replace_callback'#\{(.*?)\}#s'
            array(&
$this,'searchAndReplaceHeightAndPadding') ,$contents);
        
        
$this->writeFile $contents$this->returnHackedFileName($styleSheetFileName) );
    }
    
    function 
searchAndReplaceWidthAndPadding$matches )
    {
        
$css $matches[0];
        
        if ( 
preg_match'#width ?: ?([0-9|.]+)(px|em|%|pt) ?;#si'$css $width ) ) 
        {
            
$this->currentBox->width $width[1];
            
$this->currentBox->widthUnit $width[2];
            
$this->currentBox->paddingLeft 0;
            
$this->currentBox->paddingRight 0;
            
$this->currentBox->paddingLeftUnit $width[2];
            
$this->currentBox->paddingRightUnit $width[2];
            
$this->currentBox->borderLeft 0;
            
$this->currentBox->borderRight 0;
            
$this->currentBox->borderLeftUnit $width[2];
            
$this->currentBox->borderRightUnit $width[2];
        }
        else
        {
            return 
$css
        }
            
        if ( 
preg_match_all
            
'#padding(-left|-right)? ?: ?(([0-9|.]+)(px|em|%|pt| ?) ?){1,4} ?;#si'
            
$css $allPaddingsPREG_SET_ORDER ) )
        {
            foreach( 
$allPaddings as $padding )
            {
                if ( 
$padding[1] == "-left" )
                {
                    
$this->currentBox->paddingLeft $padding[3];
                    
$this->currentBox->paddingLeftUnit $padding[4];
                }
                elseif ( 
$padding[1] == "-right" )
                {
                    
$this->currentBox->paddingRight $padding[3];
                    
$this->currentBox->paddingRightUnit $padding[4];
                }
                else
                {
                    
$this->getWidthPaddingValue$padding[0] );
                }
            }
        }
        
        if ( 
preg_match_all
            
'#border(-left|-right)? ?: ?(([0-9|.]+)(px|em|%|pt| ?) ?){1,4}#si'
            
$css $allBordersPREG_SET_ORDER ) )
        {
        
            foreach( 
$allBorders as $border )
            {
                if ( 
$border[1] == "-left" )
                {
                    
$this->currentBox->borderLeft $border[3];
                    
$this->currentBox->borderLeftUnit $border[4];
                }
                elseif ( 
$border[1] == "-right" )
                {
                    
$this->currentBox->borderRight $border[3];
                    
$this->currentBox->borderRightUnit $border[4];
                }
                else
                {
                    
$this->getWidthBorderValue$border[0] );
                }
            }
        }
        
        
$correctWidth 0;
        
        if ( 
$this->currentBox->borderLeftUnit == $this->currentBox->borderRightUnit
            
&& $this->currentBox->borderLeftUnit == $this->currentBox->widthUnit )
        {
            
$correctWidth += $this->currentBox->borderLeft $this->currentBox->borderRight;
        }
        
        if ( 
$this->currentBox->paddingLeftUnit == $this->currentBox->paddingRightUnit 
            
&& $this->currentBox->paddingLeftUnit == $this->currentBox->widthUnit )
        {
            
$correctWidth += $this->currentBox->paddingRight $this->currentBox->paddingLeft;
        }
        
        
$correctWidth += $this->currentBox->width;
    
        
$css preg_replace'#width ?: ?([0-9|.]+)(px|em|%|pt) ?;#si',  "width:$correctWidth\\2;"$css );
        return 
$css;
    
    
    }
    
    function 
searchAndReplaceHeightAndPadding$matches )
    {
        
$css $matches[0];
        
        if ( 
preg_match'#height ?: ?([0-9|.]+)(px|em|%|pt) ?;#si'$css $height ) ) 
        {
            
$this->currentBox->height $height[1];
            
$this->currentBox->heightUnit $height[2];
            
$this->currentBox->paddingBottom 0;
            
$this->currentBox->paddingTop 0;
            
$this->currentBox->paddingBottomUnit $height[2];
            
$this->currentBox->paddingTopUnit $height[2];
            
$this->currentBox->borderTop 0;
            
$this->currentBox->borderBottom 0;
            
$this->currentBox->borderTopUnit $height[2];
            
$this->currentBox->borderBottomUnit $height[2];
        }
        else
        {
            return 
$css
        }
            
        if ( 
preg_match_all
            
'#padding(-bottom|-top)? ?: ?(([0-9|.]+)(px|em|%|pt| ?) ?){1,4} ?;#si'
            
$css $allPaddingsPREG_SET_ORDER ) )
        {
            foreach( 
$allPaddings as $padding )
            {
                if ( 
$padding[1] == "-bottom" )
                {
                    
$this->currentBox->paddingBottom $padding[3];
                    
$this->currentBox->paddingBottomUnit $padding[4];
                }
                elseif ( 
$padding[1] == "-top" )
                {
                    
$this->currentBox->paddingTop $padding[3];
                    
$this->currentBox->paddingTopUnit $padding[4];
                }
                else
                {
                    
$this->getHeightPaddingValue$padding[0], 'height' );
                }
            }
        }
        
        if ( 
preg_match_all
            
'#border(-bottom|-top)? ?: ?(([0-9|.]+)(px|em|%|pt| ?) ?){1,4}#si'
            
$css $allbordersPREG_SET_ORDER ) )
        {
            foreach( 
$allborders as $border )
            {
                if ( 
$border[1] == "-bottom" )
                {
                    
$this->currentBox->borderBottom $border[3];
                    
$this->currentBox->borderBottomUnit $border[4];
                }
                elseif ( 
$border[1] == "-top" )
                {
                    
$this->currentBox->borderTop $border[3];
                    
$this->currentBox->borderTopUnit $border[4];
                }
                else
                {
                    
$this->getHeightBorderValue$border[0], 'height' );
                }
            }
        }
        
        
        
$correctHeight 0;
        
        if ( 
$this->currentBox->borderTopUnit == $this->currentBox->borderBottomUnit
            
&& $this->currentBox->borderTopUnit == $this->currentBox->heightUnit )
        {
            
$correctHeight += $this->currentBox->borderTop $this->currentBox->borderBottom;
            
//echo "hello :D";
        
}
        
        if ( 
$this->currentBox->paddingTopUnit == $this->currentBox->paddingBottomUnit 
            
&& $this->currentBox->paddingTopUnit == $this->currentBox->heightUnit )
        {
            
//echo "hallo ;)";
            
$correctHeight += $this->currentBox->paddingTop $this->currentBox->paddingBottom;
        }
        
        
$correctHeight += $this->currentBox->height;
    
        
$css preg_replace'#height ?: ?([0-9|.]+)(px|em|%|pt) ?;#si',  "height:$correctHeight\\2;"$css );
        return 
$css;
        
    }
    
    function 
getWidthBorderValue $border )
    {    
        
$number preg_match_all"#([0-9|.]+)(px|em|%|pt| ?)#"$border$borderValuePREG_SET_ORDER );
        
        if ( 
$number == )
        {
            
$this->currentBox->borderLeft $borderValue[0][1];
            
$this->currentBox->borderRight $borderValue[0][1];
            
$this->currentBox->borderRightUnit $borderValue[0][2];
            
$this->currentBox->borderLeftUnit $borderValue[0][2];
            return 
true;
        }
        elseif ( 
$number == )
        {
            
$this->currentBox->borderLeft $borderValue[1][1];
            
$this->currentBox->borderRight $borderValue[1][1];
            
$this->currentBox->borderRightUnit $borderValue[1][2];
            
$this->currentBox->borderLeftUnit $borderValue[1][2];
            return 
true;
        }
        elseif ( 
$number == )
        {
            
$this->currentBox->borderLeft $borderValue[1][1];
            
$this->currentBox->borderRight $borderValue[3][1];
            
$this->currentBox->borderLeftUnit $borderValue[1][2];
            
$this->currentBox->borderRightUnit $borderValue[3][2];
            return 
true;
        }
        else
        {
            return 
false;
        }
    }
    
    function 
getWidthPaddingValue $padding )
    {    
        
$number preg_match_all"#([0-9|.]+)(px|em|%|pt| ?)#"$padding$paddingValuePREG_SET_ORDER );
        
        if ( 
$number == )
        {
            
$this->currentBox->paddingLeft $paddingValue[0][1];
            
$this->currentBox->paddingRight $paddingValue[0][1];
            
$this->currentBox->paddingLeftUnit $paddingValue[0][2];
            
$this->currentBox->paddingRightUnit $paddingValue[0][2];
            return 
true;
        }
        elseif ( 
$number == )
        {
            
$this->currentBox->paddingLeft $paddingValue[1][1];
            
$this->currentBox->paddingRight $paddingValue[1][1];
            
$this->currentBox->paddingLeftUnit $paddingValue[1][2];
            
$this->currentBox->paddingRightUnit $paddingValue[1][2];
            return 
true;
        }
        elseif ( 
$number == )
        {
            
$this->currentBox->paddingLeft $paddingValue[1][1];
            
$this->currentBox->paddingRight $paddingValue[3][1];
            
$this->currentBox->paddingLeftUnit $paddingValue[1][2];
            
$this->currentBox->paddingRightUnit $paddingValue[3][2];
            return 
true;
        }
        else
        {
            return 
false;
        }
    }
    
    function 
getHeightBorderValue $border )
    {    
        
$number preg_match_all"#([0-9|.]+)(px|em|%|pt| ?)#"$border$borderValuePREG_SET_ORDER );
        
        if ( 
$number == )
        {
            
$this->currentBox->borderTop $borderValue[0][1];
            
$this->currentBox->borderBottom $borderValue[0][1];
            
$this->currentBox->borderTopUnit $borderValue[0][2];
            
$this->currentBox->borderBottomUnit $borderValue[0][2];
            return 
true;
        }
        elseif ( 
$number == )
        {
            
$this->currentBox->borderTop $borderValue[0][1];
            
$this->currentBox->borderBottom $borderValue[0][1];
            
$this->currentBox->borderTopUnit $borderValue[0][2];
            
$this->currentBox->borderBottomUnit $borderValue[0][2];
            return 
true;
        }
        elseif ( 
$number == )
        {
            
$this->currentBox->borderTop $borderValue[0][1];
            
$this->currentBox->borderBottom $borderValue[2][1];
            
$this->currentBox->borderTopUnit $borderValue[0][2];
            
$this->currentBox->borderBottomUnit $borderValue[2][2];
            return 
true;
        }
        else
        {
            return 
false;
        }
    }
    
    function 
getHeightPaddingValue $padding )
    {    
        
$number preg_match_all"#([0-9|.]+)(px|em|%|pt| ?)#"$padding$paddingValuePREG_SET_ORDER );
        
        if ( 
$number == )
        {
            
$this->currentBox->paddingTop $paddingValue[0][1];
            
$this->currentBox->paddingBottom $paddingValue[0][1];
            
$this->currentBox->paddingTopUnit $paddingValue[0][2];
            
$this->currentBox->paddingBottomUnit $paddingValue[0][2];
            return 
true;
        }
        elseif ( 
$number == )
        {
            
$this->currentBox->paddingTop $paddingValue[0][1];
            
$this->currentBox->paddingBottom $paddingValue[0][1];
            
$this->currentBox->paddingTopUnit $paddingValue[0][2];
            
$this->currentBox->paddingBottomUnit $paddingValue[0][2];
            return 
true;
        }
        elseif ( 
$number == )
        {
            
$this->currentBox->paddingTop $paddingValue[0][1];
            
$this->currentBox->paddingBottom $paddingValue[2][1];
            
$this->currentBox->paddingTopUnit $paddingValue[0][2];
            
$this->currentBox->paddingBottomUnit $paddingValue[2][2];
            return 
true;
        }
        else
        {
            return 
false;
        }
    }
    
    function 
writeFile $contents$fileName )
    {
        if ( 
file_exists $fileName ) )
        {
            
$handle fopen ($fileName"w");
        }
        else
        {
            
$handle fopen ($fileName"a");
            
        }
        
        
chmod$fileName0777 );
        
fwrite $handle  $contents  );
        
fclose $handle );
            
    }
}

?>