<?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 [url]http://www.gnu.org/licenses/[/url]
***************************************************************************/
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
$lines = 0;
if (($this->config['ort'] != null) && ($this->config['ort'] != ''))
$uri = "/tankstelle_liste?spritsorte=" . $this->config['sorte'] . "&r=" . $this->config['radius'] . "&ort=" . $this->config['ort'];
else
$uri = "/tankstelle_liste?spritsorte=" . $this->config['sorte'] . "&r=" . $this->config['radius'] . "&lat=" . $this->config['lat'] . "&lon=" . $this->config['lon'];
$uri = "/tankstelle_liste?spritsorte=" . $this->config['sorte'] . "&r=" . $this->config['radius'] . "&lat=" . $this->config['lat'] . "&lon=" . $this->config['lon'];
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
mysql_query ("START TRANSACTION", $this->dbconn);
$query = "Delete from if_tanken";
mysql_query ($query, $this->dbconn) or die('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="price_entry_table">') !== 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)
$txt[$tank_num]['date'] = strtotime (str_replace("vor", "-", str_replace ("Std.", "hours", $zeile[$i])));
}
}
}
elseif (strpos ($zeile[$i], '<div class="location_name">') !== false)
$txt[$tank_num]['name'] = htmlspecialchars_decode (utf8_decode (strip_tags (trim ($zeile[$i]))));
elseif (strpos ($zeile[$i], '<div class="location_street_number">') !== false)
$txt[$tank_num]['street'] = htmlspecialchars_decode (utf8_decode (strip_tags (trim ($zeile[$i]))));
elseif (strpos ($zeile[$i], '<div class="location_zip_code_city">') !== false)
$txt[$tank_num]['city'] = htmlspecialchars_decode (utf8_decode (strip_tags (trim($zeile[$i]))));
elseif (strpos ($zeile[$i], '<div class="location_distance">') !== false)
$txt[$tank_num]['dist'] = strip_tags (trim ($zeile[$i]));
}
}
for ($i = 1; $i <= $tank_num; $i++)
{
if ($txt[$i]['price'] != null && $txt[$i]['date'] != null)
{
$this->addStation ($txt[$i]['price'], $txt[$i]['date'], $txt[$i]['dist'].' - '.$txt[$i]['name'].', '.$txt[$i]['street'].', '.$txt[$i]['city']);
}
}
mysql_query("COMMIT", $this->dbconn);
}
public function doOutput($image, $style, $updateData, &$yoffset)
{
$spritsorte[1] = 'Autogas';
$spritsorte[2] = 'LKW-Diesel';
$spritsorte[3] = 'Diesel';
$spritsorte[4] = 'Bioethanol';
$spritsorte[5] = 'Super E10';
$spritsorte[6] = 'SuperPlus';
$spritsorte[7] = 'Super E5';
$spritsorte[8] = 'Erdgas';
$spritsorte[9] = 'Biodiesel';
$spritsorte[10] = 'Pflanzenöl';
$spritsorte[11] = 'Zweitakt';
$spritsorte[12] = 'PremiumDiesel';
$spritsorte[13] = 'AdBlue';
if ($updateData) $this->doUpdate();
$query = "SELECT * FROM `if_tanken` ORDER BY `datum` DESC, `preis` ASC";
$result = mysql_query($query, $this->dbconn);
if (mysql_num_rows($result) == 0) return;
// 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,
);
// print header
$text = 'Kraftstoffpreise (' . $spritsorte[$this->config['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;
while ($row = mysql_fetch_assoc($result))
{
$counter++;
$rest = (mysql_num_rows($result) - $counter) + 1;
$entrylimit = $this->config['max_displayed_stations'];
if ( (($entrylimit) && ($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 += 10; // 26;
mysql_free_result($result);
}
private function addStation($preis, $datum, $station)
{
$query = "REPLACE INTO `if_tanken` SET
`preis`= '".mysql_real_escape_string($preis)."',
`datum`= FROM_UNIXTIME($datum),
`station`= '".mysql_real_escape_string($station)."'";
mysql_query($query, $this->dbconn) or die('Error, insert query failed: '.mysql_error());
}
}
?>