<?php

define 
("AFFILIATE_ID""Hier die Affiliate-ID");
define ("USERNAME""Hier der Username");
define ("PASSWORD""Hier das Password");
define ("CACHE""./cache");

class 
DataFile {

    
private $request;
    
private $type;
    
private $file;

    function 
__construct () {
        
throw new Exception ('Konstruktor in der abgeleiteten Klasse fehlt!');
    }

    function 
getRequest () {
        return 
'http://' USERNAME ':' PASSWORD '@www.hotelsclick.com/de/export.php?';
    }

    function 
setType ($str) {
        if (
in_array ($str, array ('json''xml'))) {
            
$this->type $str;
        }
    }

    function 
getType () {
        return (!empty (
$this->type) ? $this->type 'html');
    }

    function 
setFileName ($str) {
        
$this->file CACHE '/' md5 ($str);
    }

    function 
getFileName () {
        return 
$this->file '.' $this->getType ();
    }

    function 
get () {
        
$file $this->getFileName ();
        
$mtime file_exists ($file) ? filemtime ($file) : 0;
        if (
time() - 86400 $mtime) {
            return 
file_get_contents ($file);
        }
        
$content file_get_contents ($this->getRequest ());
        if (!empty (
$content)) {
            
$fp fopen ($file'w');
            
fwrite($fp$content);
            
fclose($fp);
        }
        return 
$content;
    }

}

class 
GeoFile extends DataFile {

    var 
$geo;

    function 
__construct ($lt$lg) {
        if (!
is_numeric ($lt) && !is_numeric ($lg)) {
            
throw new Exception ('Paramter muessen vom numerisch sein!');
        }
        
$this->geo $lt '_' $lg;
        
$this->file $this->setFilename ($this->geo);
    }

    function 
getRequest () {
        
$request parent::getRequest ();
        return 
$request 'geo=' $this->geo '&format=' $this->getType ();
    }

}

class 
hoteldecorator {

    
public $hotel;

    function 
__construct($obj) {
        
$this->hotel $obj;
        
$this->hotel->link .= '&amp;distributor=' AFFILIATE_ID;
    }

    function 
getName () {
        return 
htmlspecialchars ($this->hotel->nameENT_QUOTES);
    }

    function 
getAddress () {
        if (!isset (
$this->_getAddress)) {
            
$temp explode ('<br />'nl2br ($this->hotel->address));
            for (
$i 0$i count ($temp); $i++) {
                
$temp[$i] = htmlspecialchars (trim ($temp[$i]), ENT_QUOTES);
            }
            
$this->_getAddress implode ('<br />'$temp);
        }
        return 
$this->_getAddress;
    }

    function 
getDescription () {
        return 
htmlspecialchars ($this->hotel->descriptionENT_QUOTES);
    }

    function 
getPrice () {
        return 
sprintf ("ab %0.2f &euro; *"$this->hotel->price);
    }

    function 
getAnker () {
        return 
'<a href="' $this->hotel->link '" name="' $this->hotel->code '">' $this->getName () . '</a>';
    }

    function 
getLink () {
        return 
'<a href="' $this->hotel->link '">Weitere Informationen &amp; Buchung &gt;&gt;</a>';
    }

    function 
getLocalLink () {
        return 
'<a href="#' $this->hotel->code '">' $this->getName () . '</a>';
    }

    function 
getImage () {
        
$img = (!empty ($this->hotel->image) ? $this->hotel->image '/images/no_photo.jpg');
        return 
'<img src="' $img '" alt="' $this->getName () . '" />';
    }

    function 
getStarsImage () {
        
$retval '';
        for (
$i 0$i $this->hotel->stars$i++) {
            
$retval .= '<img src="/images/star.gif" alt="" style="right: ' . ($i 16 5) . 'px;" />';
        }
        return 
$retval;
    }

    function 
getDistance () {
        return 
'Entfernung ca. ' $this->hotel->distance ' km';
    }

    function 
getLat () {
        return 
$this->hotel->lt;
    }

    function 
getLong () {
        return 
$this->hotel->lg;
    }

    function 
getMapOverlay () {
        return 
$this->getLocalLink () . '<br />' $this->getAddress ();
    }

}

if (
basename ($_SERVER['PHP_SELF']) == basename(__FILE__)) {
    
$remoteFile = new GeoFile (52.38000113.522500);
    
$remoteFile->setType ('json');

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="description" content="Testausgabe der Schnittstelle zu Hotelsclick.com" />
    </head>
    <body>
<?php

    
foreach (json_decode ($remoteFile->get ()) as $item) {
        
$hotel = new hoteldecorator ($item);

?>
        <div class="hotel">
            <div><?php echo $hotel->getAnker (); ?></div>
            <div><?php echo $hotel->getStarsImage (); ?></div>
            <div><?php echo $hotel->getImage (); ?></div>
            <div><?php echo $hotel->getAddress (); ?></div>
            <div><?php echo $hotel->getPrice (); ?></div>
            <div><?php echo $hotel->getDescription (); ?></div>
            <div><?php echo $hotel->getDistance (); ?></div>
            <div><?php echo $hotel->getLink (); ?></div>
        </div>
<?php

    
}

?>
    </body>
</html>
<?php

}

?>