<?php
/**
 * Branches
 * - FF, O, Saf, IE8 get data URIs
 * - IE 6,7 get MHTML
 * - Vista/Win7 and IE6,7 get special treatment - an extra component
 */


class DataSprites {

    var $config = array(
        'classname_prefix' => '.icon-',
        'separator'        => "_MY_BOUNDARY_SEPARATOR",
        'CRLF'             => "\r\n",
        'expires'          => 'next year' 
    );
    
    var $files = array();

    function DataSprites() {

        $ua = $_SERVER['HTTP_USER_AGENT'];
        $this->problem_ie = strstr($ua, "MSIE 6") || strstr($ua, "MSIE 7");
        $this->problem_platform = strstr($ua, "Windows NT 6") || strstr($ua, "Windows NT 7");
        $this->url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
        $this->vistaextra = @$_GET['extra'];

        // force past expiry for vista/ie7
        if ($this->vistaextra) {
            $this->config['expires'] = 'yesterday';
        } 

    }

    function getName($f) {
        $f = pathinfo($f);
        $f = $f['filename'] ? $f['filename'] : $f['basename'];
        $f = explode('.', $f);
        $f = $f[0];
        return preg_replace('/\W/i', '', $f);
    }

    function getClassname($f) {
        return $this->config['classname_prefix'] . $this->getName($f);  
    }

    function get64($f) {
        return base64_encode(file_get_contents($f));    
    }

    function getDeclarationUri($f) {
        $res = $this->getClassName($f);
        $res .= '{';
        $res .= 'background-image:url("data:image/png;base64,' . $this->get64($f) . '");';
        $res .= '}';
        return $res;
    }

    function getDeclarationMHTML($f, $url = '') {
        
        $url = $url ? $url : $this->url;
        $name = $this->getName($f);
        $res = $this->getClassName($f);
        $res .= '{';
        $res .= 'background-image:url(mhtml:' . $url . '!' . $name . ');';
        $res .= '}';
        return $res;    
    }

    function getMHTML() {
        
        $CRLF = $this->config['CRLF'];
        $separator = $this->config['separator'];
        
        $mhtml = 'Content-Type: multipart/related; boundary="' . $separator . '"' . $CRLF . $CRLF;

        foreach ($this->files as $f) {
            $mhtml .= '--' . $separator . $CRLF;
            $mhtml .= 'Content-Location:' . $this->getName($f) . $CRLF;
            $mhtml .= 'Content-Transfer-Encoding:base64'. $CRLF;
            $mhtml .= $CRLF;
            $mhtml .= $this->get64($f) . $CRLF;
        }
        return $mhtml;
    }

    function getETag() {
        $etag = '';
        foreach ($this->files as $f) {
            $etag .= $this->getName($f);
        }
        return $etag;
    }

    function sendHeaders() {

        $etag = $this->getETag($this->files); 
        if (@$_SERVER['HTTP_IF_NONE_MATCH'] === $etag) {
            header('HTTP/1.1 304 Not Modified');
            die();
        }

        header('Expires: ' . gmdate('D, d M Y H:i:s', strtotime($this->config['expires'])) . ' GMT');
        ob_start("ob_gzhandler");
        
        if (!$this->problem_ie || !$this->problem_platform) {
            header('Content-Type: text/css');
            return false;
        }
        
        //header('Cache-Control: public, no-transform');
        header('ETag: ' . $etag);
        header('Vary: Accept-Encoding');
        header('Content-Type: text/plain');
    }


    function getData() {

        $data = '';
        $CRLF = $this->config['CRLF'];
        
        // normal browsers
        if (!$this->problem_ie) {
            foreach ($this->files as $f) {
                $data .= $this->getDeclarationUri($f) . $CRLF;
            }
            return $data;
        }

        // IE 6,7 in Non-Vista, Non-Win7 - use MHTML
        if ($this->problem_ie && !$this->problem_platform) {
            $data .= '/*' . $CRLF;
            $data .= $this->getMHTML();
            $data .= '*/' . $CRLF . $CRLF;

            foreach($this->files as $f) {
                $data .= $this->getDeclarationMHTML($f) . $CRLF;
            }
            return $data;
        }
     
        // IE6, 7 on Vista and Win7
        if ($this->problem_ie && $this->problem_platform) {
            if ($this->vistaextra) {
                $data .= $this->getMHTML();
            } else {
                $vistaurl = $this->url . '?extra=1';
                foreach ($this->files as $f) {
                    $data .= $this->getDeclarationMHTML($f, $vistaurl) . $CRLF;
                }
            }
            return $data;
        }
    }
}

// test
$myfiles = array('aol.png', 'deal.png', 'games.png', 'gmail.png', 'horoscopes.png', 'shop.png', 'travel.png');
$ds = new DataSprites();
$ds->files = $myfiles;
$ds->sendHeaders();
echo $ds->getData();

?>