From 3a6c2771ec64bd8e531ac90541be2b2d61e9bb58 Mon Sep 17 00:00:00 2001 From: Jeroen De Dauw Date: Tue, 20 Nov 2018 04:51:32 +0100 Subject: [PATCH 1/2] Simplify KML formtter --- src/Presentation/KmlFormatter.php | 93 +++++--------------- src/SemanticMW/ResultPrinters/KmlPrinter.php | 8 +- 2 files changed, 23 insertions(+), 78 deletions(-) diff --git a/src/Presentation/KmlFormatter.php b/src/Presentation/KmlFormatter.php index e4357cac0..f33b62812 100644 --- a/src/Presentation/KmlFormatter.php +++ b/src/Presentation/KmlFormatter.php @@ -3,50 +3,18 @@ namespace Maps\Presentation; use Maps\Elements\Location; -use Xml; /** - * Class to format geographical data to KML. - * * @licence GNU GPL v2+ * @author Jeroen De Dauw < jeroendedauw@gmail.com > */ class KmlFormatter { - /** - * @var array - */ - private $params; - - /** - * @var Location[] - */ - private $placemarks; - - public function __construct( array $params = [] ) { - $this->params = $params; - $this->clearElements(); - } - - /** - * Clears all set geographical objects. - */ - public function clearElements() { - $this->clearPlacemarks(); - } - - /** - * Clears all set placemarks. - */ - public function clearPlacemarks() { - $this->placemarks = []; - } - /** * Builds and returns KML representing the set geographical objects. */ - public function getKML(): string { - $elements = $this->getKMLElements(); + public function formatLocationsAsKml( Location ...$locations ): string { + $elements = $this->getKmlForLocations( $locations ); // http://earth.google.com/kml/2.2 return <<getPlaceMarks() ); - - return implode( "\n", $elements ); + private function getKmlForLocations( array $locations ): string { + return implode( + "\n", + array_map( + function( Location $location ) { + return $this->locationToKmlPlacemark( $location ); + }, + $locations + ) + ); } - /** - * Returns KML for all set placemarks in a list, where each element is - * a KML node representing a placemark. - */ - private function getPlaceMarks(): array { - $placeMarks = []; - - foreach ( $this->placemarks as $location ) { - $placeMarks[] = $this->getKMLForLocation( $location ); - } - return $placeMarks; - } - - private function getKMLForLocation( Location $location ): string { + private function locationToKmlPlacemark( Location $location ): string { + // TODO: escaping? $name = 'getTitle() . ']]>'; + // TODO: escaping? $description = 'getText() . ']]>'; - $coordinates = $location->getCoordinates(); - - // lon,lat[,alt] - $coordinates = Xml::element( - 'coordinates', - [], - $coordinates->getLongitude() . ',' . $coordinates->getLatitude() . ',0' - ); + $coordinates = '' . htmlspecialchars( $this->getCoordinateString( $location ) ) . ''; return << @@ -110,13 +61,11 @@ private function getKMLForLocation( Location $location ): string { EOT; } - /** - * @param Location[] $placemarks - */ - public function addPlacemarks( array $placemarks ) { - foreach ( $placemarks as $placemark ) { - $this->placemarks[] = $placemark; - } + private function getCoordinateString( Location $location ): string { + // lon,lat[,alt] + return $location->getCoordinates()->getLongitude() + . ',' . $location->getCoordinates()->getLatitude() + . ',0'; } } diff --git a/src/SemanticMW/ResultPrinters/KmlPrinter.php b/src/SemanticMW/ResultPrinters/KmlPrinter.php index 059eef28c..cad08191a 100644 --- a/src/SemanticMW/ResultPrinters/KmlPrinter.php +++ b/src/SemanticMW/ResultPrinters/KmlPrinter.php @@ -49,12 +49,8 @@ private function getKML( SMWQueryResult $res, $outputmode ) { $queryHandler->setSubjectSeparator( '' ); $queryHandler->setPageLinkText( $this->params['pagelinktext'] ); - $formatter = new KmlFormatter( $this->params ); - - $shapes = $queryHandler->getShapes(); - $formatter->addPlacemarks( $shapes['locations'] ); - - return $formatter->getKML(); + $formatter = new KmlFormatter(); + return $formatter->formatLocationsAsKml( $queryHandler->getShapes()['locations'] ); } /** From afd8e17fa10f968bd87392f2b14c4b4f439f5c70 Mon Sep 17 00:00:00 2001 From: Jeroen De Dauw Date: Tue, 20 Nov 2018 04:54:44 +0100 Subject: [PATCH 2/2] Improve escaping --- src/Presentation/KmlFormatter.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Presentation/KmlFormatter.php b/src/Presentation/KmlFormatter.php index f33b62812..9446be260 100644 --- a/src/Presentation/KmlFormatter.php +++ b/src/Presentation/KmlFormatter.php @@ -47,7 +47,9 @@ private function locationToKmlPlacemark( Location $location ): string { // TODO: escaping? $description = 'getText() . ']]>'; - $coordinates = '' . htmlspecialchars( $this->getCoordinateString( $location ) ) . ''; + $coordinates = '' + . $this->escapeValue( $this->getCoordinateString( $location ) ) + . ''; return << @@ -68,4 +70,8 @@ private function getCoordinateString( Location $location ): string { . ',0'; } + private function escapeValue( string $value ): string { + return htmlspecialchars( $value, ENT_NOQUOTES ); + } + }