<?php
/***************************************************************************
* InfoFrame (image generator for digital picture frames)
* Copyright (C) 2010
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
***************************************************************************/
class BenzinPlugin implements IPlugin
{
private $dbconn = NULL;
private $config = NULL;
public function __construct($dbconn, $config)
{
$this->dbconn = $dbconn;
$this->config = $config;
}
public function doUpdate()
{
// Preise und Tankstellen lesen - fsockopen-Version
// defaults
$radius = "5";
$ort = "51147";
$lon = "0";
$lat = "0";
$sorte = "Diesel";
$lines = 0;
if (isset($this->config['radius'])) $radius = $this->config['radius'];
if (isset($this->config['sorte'])) $sorte = urlencode($this->config['sorte']);
if (isset($this->config['ort'])) $ort = $this->config['ort'];
if (isset($this->config['lon']) && isset($this->config['lat']))
$uri = "/tankstelle_liste?spritsorte=" . $sorte . "&r=" . $radius . "&lat=" . $this->config['lat'] . "&lon=" . $this->config['lon'];
else
$uri = "/tankstelle_liste?spritsorte=" . $sorte . "&r=" . $radius . "&ort=" . $ort;
header ("Content-type: text/html");
$sock = fsockopen ("www.clever-tanken.de", 80, $errno, $errstr, 10);
if (!$sock)
return;
else
{
fputs ($sock, "GET " . $uri . " HTTP/1.1\r\n");
fputs ($sock, "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021204\r\n");
fputs ($sock, "Host: www.clever-tanken.de\r\n");
fputs ($sock, "Connection: close\r\n\r\n");
while (!feof($sock))
$zeile[$lines++] = utf8_encode (trim (fgets ($sock, 512)));
fclose ($sock);
}
// Datenbankeinträge löschen
$this->dbconn->query ("START TRANSACTION");
$query = "Delete from if_tanken";
$this->dbconn->query ($query) or die('BenzinPlugin.php: Error, delete query failed');
// file zeilenweise einlesen und gefiltert entsprechend den Einträgen in config.ini.
$tank_num = 0;
$get_date = false;
for ($i = 0; $i < $lines; $i++)
{
if ($zeile[$i] != '')
{
if (strpos ($zeile[$i], '<div class="fuel-station-price">') !== false)
$tank_num++;
elseif (strpos ($zeile[$i], '<div class="price">') !== false)
$txt[$tank_num]['price'] = strip_tags(trim($zeile[$i]));
elseif (strpos ($zeile[$i], '<div class="price_date">') !== false or $get_date)
{
$get_date = true;
if (strpos ($zeile[$i], '</div>') !== false)
{
$get_date = false;
}
else
{
if (strpos ($zeile[$i], '<div class="price_date">') === false)
{
if (strpos ($zeile[$i], 'Heute') !== false)
{
$txt[$tank_num]['date'] = strtotime ('now');
}
if (strpos ($zeile[$i], 'Gestern') !== false)
{
$txt[$tank_num]['date'] = strtotime ('-1 day');
}
if (strpos ($zeile[$i], 'vor ') !== false)
{
if(strpos ($zeile[$i], 'Std.') !== false)
{
$aa = str_replace ("Std.", "hours", $zeile[$i]);
}
else
{
$aa = str_replace ("Min.", "minutes", $zeile[$i]);
}
$aa = str_replace("vor", "-", $aa);
$aa = strip_tags($aa);
$aa = str_replace(" ", '', $aa);
$aa = strtotime ($aa);
$txt[$tank_num]['date'] = $aa;
}
}
}
}
elseif (strpos ($zeile[$i], '<div class="row fuel-station-location-name">') !== false)
$txt[$tank_num]['name'] = htmlspecialchars_decode (utf8_decode (strip_tags (trim ($zeile[$i]))));
elseif (strpos ($zeile[$i], '<div id="fuel-station-location-street">') !== false)
$txt[$tank_num]['street'] = htmlspecialchars_decode (utf8_decode (strip_tags (trim ($zeile[$i]))));
elseif (strpos ($zeile[$i], '<div id="fuel-station-location-city">') !== false)
$txt[$tank_num]['city'] = htmlspecialchars_decode (utf8_decode (strip_tags (trim($zeile[$i]))));
elseif (strpos ($zeile[$i], '<div class="fuel-station-location-address-distance">') !== false)
$txt[$tank_num]['dist'] = strip_tags (trim ($zeile[$i+1]));
}
}
for ($i = 1; $i <= $tank_num; $i++)
{
if (isset($txt[$i]['price']) && isset($txt[$i]['date']))
{
$this->addStation ($txt[$i]['price'], $txt[$i]['date'], $txt[$i]['dist'].' - '.$txt[$i]['name'].', '.$txt[$i]['street'].', '.$txt[$i]['city']);
}
}
$this->dbconn->query("COMMIT");
}
public function doOutput($image, $style, $updateData, &$yoffset)
{
if ($updateData) $this->doUpdate();
// define styles
$opt_header = array(
'width' => imagesx($image)-290,
'line_height' => 18,
'align' => ALIGN_LEFT
);
$opt_entry = array(
'width' => imagesx($image)-290,
'height' => 12,
'line_height' => 12,
'align' => ALIGN_LEFT,
'word_wrap_hyphen' => '...',
'aggressive_word_wrap' => false,
);
// defaults
$sorte = "Diesel";
$entrylimit = 5;
if (isset($this->config['sorte'])) $sorte = $this->config['sorte'];
if (isset($this->config['max_displayed_stations'])) $entrylimit = $this->config['max_displayed_stations'];
$query = "SELECT * FROM `if_tanken` ORDER BY `datum` DESC, `preis` ASC";
$result = $this->dbconn->query($query);
if ($result->rowcount() == 0)
return;
// print header
$text = 'Kraftstoffpreise (' . $sorte . ')';
imagettftextboxopt($image, 22, 0, 50, $yoffset, $style['textcolor'], $style['font'], $text, $opt_header);
$icon = ImageCreateFromPNG ('resources/icons/tanken/tank.png');
ImageCopy($image, $icon, 20, $yoffset-3, 0, 0, imagesx($icon), imagesy($icon));
ImageDestroy($icon);
$yoffset += 26;
$counter = 0;
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
$counter++;
$rest = ($result->rowcount() - $counter) + 1;
if (($counter > $entrylimit) || (($yoffset >= (imagesy($image) - 36)) && ($rest > 1)))
break;
// Preis
$text = ' Eur'; // Euro-Zeichen geht nicht
$text = $row['preis'].$text;
// Datum
//$text = $text.' '.strftime( "%d.%m.", strtotime($row['datum']));
$text = $text.' '.strftime( "%d.%m.%y %H:%M", strtotime($row['datum']));
// Tankstelle
$text = $text.' '.$row['station'];
// determine button color
$colorArray = htmlColorToRgb('#424242');
$color = ImageColorAllocate ($image, $colorArray[0], $colorArray[1], $colorArray[2]);
// print appointment text and button
if (stripos($text,'aral')>0) $png = 'resources/icons/tanken/aral.png'; // Aral-Tankstelle
elseif (stripos($text,'agip')>0) $png = 'resources/icons/tanken/agip.png'; // Agip-Tankstelle
elseif (stripos($text,'bft')>0) $png = 'resources/icons/tanken/bft.png'; // BFT-Tankstelle
elseif (stripos($text,'esso')>0) $png = 'resources/icons/tanken/esso.png'; // Esso-Tankstelle
elseif (stripos($text,'heinlein')>0) $png = 'resources/icons/tanken/h.png'; // Heinlein-Tankstelle
elseif (stripos($text,'jet')>0) $png = 'resources/icons/tanken/jet.png'; // Jet-Tankstelle
elseif (stripos($text,'omv')>0) $png = 'resources/icons/tanken/omv.png'; // OMV-Tankstelle
elseif (stripos($text,'shell')>0) $png = 'resources/icons/tanken/shell.png'; // Shell-Tankstelle
else $png = 'resources/icons/tanken/t.png';
$wicon = ImageCreateFromPNG ( $png );
ImageCopy($image, $wicon, 25, $yoffset, 0, 0, imagesx($wicon), imagesy($wicon));
ImageDestroy($wicon);
imagettftextboxopt($image, 12, 0, 50, $yoffset, $style['textcolor'], $style['font'], $text, $opt_entry);
$yoffset += 18;
}
$yoffset += 18;
}
private function addStation($preis, $datum, $station)
{
$query = "REPLACE INTO `if_tanken` SET
`preis`= " . $this->dbconn->quote($preis) . ",
`datum`= FROM_UNIXTIME($datum),
`station`= " . $this->dbconn->quote($station);
$this->dbconn->query($query) or die('BenzinPlugin.php: Error, insert query failed: '. $datum . ' ' . $this->dbconn->errorInfo()[2]);
}
}
?>