<?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
$i=0;
$ortnumber = 0;
// jeden in config.ini eingetragenen Ort in file einlesen
while ($ort = $this->config['ort_'.++$ortnumber]) {
$uri = "/tankstelle_liste?spritsorte=7&r=5&ort=".$ort; // spritsorte 3=Diesel, 5=Super E10, 7=Super E5
header("Content-type: text/html");
$sock = fsockopen("www.clever-tanken.de", 80, $errno, $errstr, 10); // 80 = Port, 10 = Timeout
if (!$sock) {
return;//$title = "keine Tankstellen in $orte_fehlen gefunden";
} 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[$i++] = 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.
$ortnumber = 0;
$merken = 0;
for ($j=0;$j<$i;$j++)
{
// Auswertung aller Orte, siehe config.ini
if (stripos($zeile[$j],'"price_entry_table"')>0) // neue Tankstelle
{
$merken = 26;
$first = $j;
}
if($merken>=1) // 25 Zeilen merken
$txt[$j-$first] = strip_tags(trim($zeile[$j])); // sinnlose Zeichen und html-Tags entfernen
if($merken == 1) // Alle Daten vorhanden
{
$tankstelle = htmlspecialchars_decode(utf8_decode($txt[21].'|'.$txt[23].'|'.$txt[22]));
$tankstelle = str_replace('Herm GmbH &Co.KG', 'Herm', $tankstelle);
$adresse = explode('|',$tankstelle);
$preis = $txt[4];
if(strstr($txt[9],"gemeldet Heute"))
$txt[9] = date('d.m.Y');
$datum = $txt[9];
$uhrzeit = $txt[10];
$d_arr = date_parse($datum);
$dat = mktime($d_arr['hour'],$d_arr['minute'],$d_arr['second'],$d_arr['month'],$d_arr['day'],$d_arr['year']);
if($preis)
$this->addStation($preis, $dat, $adresse[0].' - '.$adresse[1].' - '.$adresse[2]);
}
// Zeilenzähler für Datensuche in file
if ($merken>0)
$merken--;
else
$ortnumber = 0; // 10 Zeilen wurden erreicht, dann Orte neu bestimmen
} // Ende for-Schleife
// in Datenbank schreiben
mysql_query("COMMIT", $this->dbconn);
}
public function doOutput($image, $style, $updateData, &$yoffset) {
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 ('.$this->config['sorte'].')';
imagettftextboxopt($image, 18, 0, 50, $yoffset, $style['textcolor'], $style['font'], $text, $opt_header);
$icon = ImageCreateFromPNG ( 'resources/icons/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.-%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/aral.png'; // Aral-Tankstelle
elseif (stripos($text,'agip')>0) $png = 'resources/icons/agip.png'; // Agip-Tankstelle
elseif (stripos($text,'bft')>0) $png = 'resources/icons/bft.png'; // BFT-Tankstelle
elseif (stripos($text,'esso')>0) $png = 'resources/icons/esso.png'; // Esso-Tankstelle
elseif (stripos($text,'heinlein')>0) $png = 'resources/icons/h.png'; // Heinlein-Tankstelle
elseif (stripos($text,'jet')>0) $png = 'resources/icons/jet.png'; // Jet-Tankstelle
elseif (stripos($text,'omv')>0) $png = 'resources/icons/omv.png'; // OMV-Tankstelle
elseif (stripos($text,'shell')>0) $png = 'resources/icons/shell.png'; // Shell-Tankstelle
else $png = 'resources/icons/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());
}
}