/** * Handles GET requests for this adapter. Overridable. Does nothing if not overridden. */ public function HandleGet($single) { try { //Apply any overrides from query string $ovWidth = $this->app->request->get("width"); $ovHeight = $this->app->request->get("height"); $ovDpi = $this->app->request->get("dpi"); $ovScale = $this->app->request->get("scale"); if ($ovWidth != null) { $this->imgWidth = $ovWidth; } if ($ovHeight != null) { $this->imgHeight = $ovHeight; } if ($ovDpi != null) { $this->dpi = $ovDpi; } if ($ovScale != null) { $this->viewScale = intval($ovScale); } $site = $this->siteConn->GetSite(); $this->sessionId = $site->GetCurrentSession(); $bCreatedSession = false; if ($this->sessionId === "") { $this->sessionId = $site->CreateSession(); $bCreatedSession = true; } $userInfo = new MgUserInformation($this->sessionId); $siteConn = new MgSiteConnection(); $siteConn->Open($userInfo); $this->resSvc = $siteConn->CreateService(MgServiceType::ResourceService); $this->featSvc = $siteConn->CreateService(MgServiceType::FeatureService); $mapName = "MapImageAdapter"; $this->map = new MgMap($siteConn); $this->map->Create($this->mapDefId, $mapName); $this->sel = new MgSelection($this->map); $mapId = new MgResourceIdentifier("Session:" . $this->sessionId . "//{$mapName}.Map"); $this->map->Save($this->resSvc, $mapId); $layers = $this->map->GetLayers(); $idx = $layers->IndexOf($this->selLayerName); if ($idx < 0) { throw new Exception($this->app->localizer->getText("E_LAYER_NOT_FOUND_IN_MAP", $this->selLayerName)); } $layer = $layers->GetItem($idx); if ($layer->GetFeatureSourceId() !== $this->featureSourceId->ToString()) { throw new Exception($this->app->localizer->getText("E_LAYER_NOT_POINTING_TO_EXPECTED_FEATURE_SOURCE", $this->selLayerName, $this->featureSourceId->ToString(), $layer->GetFeatureSourceId())); } $this->selLayer = $layer; $query = $this->CreateQueryOptions($single); $reader = $this->featSvc->SelectFeatures($this->featureSourceId, $this->className, $query); $start = -1; $end = -1; $read = 0; $limit = $this->limit; $pageNo = $this->app->request->get("page"); if ($pageNo == null) { $pageNo = 1; } else { $pageNo = intval($pageNo); } $bEndOfReader = false; if ($this->pageSize > 0) { if ($pageNo > 1) { $skipThisMany = ($pageNo - 1) * $this->pageSize - 1; //echo "skip this many: $skipThisMany<br/>"; $bEndOfReader = true; while ($reader->ReadNext()) { if ($read == $skipThisMany) { $bEndOfReader = false; $limit = min($skipThisMany + $this->pageSize, $this->limit - 1) - $read; break; } $read++; } } else { //first page, set limit to page size $limit = $this->pageSize; } } //echo "read: $read, limit: $limit, pageSize: ".$this->pageSize." result limit: ".$this->limit; //die; $this->sel->AddFeatures($this->selLayer, $reader, $limit); $reader->Close(); $this->sel->Save($this->resSvc, $mapName); $extents = $this->sel->GetExtents($this->featSvc); $extLL = $extents->GetLowerLeftCoordinate(); $extUR = $extents->GetUpperRightCoordinate(); $x = ($extLL->GetX() + $extUR->GetX()) / 2.0; $y = ($extLL->GetY() + $extUR->GetY()) / 2.0; if ($this->viewScale === 0) { $csFactory = new MgCoordinateSystemFactory(); $cs = $csFactory->Create($this->map->GetMapSRS()); $metersPerUnit = $cs->ConvertCoordinateSystemUnitsToMeters(1.0); $mcsH = $extUR->GetY() - $extLL->GetY(); $mcsW = $extUR->GetX() - $extLL->GetX(); $mcsH = $mcsH * $this->zoomFactor; $mcsW = $mcsW * $this->zoomFactor; $metersPerPixel = 0.0254 / $this->dpi; if ($this->imgHeight * $mcsW > $this->imgWidth * $mcsH) { $this->viewScale = $mcsW * $metersPerUnit / ($this->imgWidth * $metersPerPixel); } else { $this->viewScale = $mcsH * $metersPerUnit / ($this->imgHeight * $metersPerPixel); } // height-limited } $req = new MgHttpRequest(""); $param = $req->GetRequestParam(); $param->AddParameter("OPERATION", "GETMAPIMAGE"); $param->AddParameter("VERSION", "1.0.0"); $param->AddParameter("SESSION", $this->sessionId); $param->AddParameter("LOCALE", $this->app->config("Locale")); $param->AddParameter("CLIENTAGENT", "MapGuide REST Extension"); $param->AddParameter("CLIENTIP", $this->GetClientIp()); $param->AddParameter("FORMAT", $this->imgFormat); $param->AddParameter("MAPNAME", $mapName); $param->AddParameter("KEEPSELECTION", "1"); $param->AddParameter("SETDISPLAYWIDTH", $this->imgWidth); $param->AddParameter("SETDISPLAYHEIGHT", $this->imgHeight); $param->AddParameter("SETDISPLAYDPI", $this->dpi); $param->AddParameter("SETVIEWCENTERX", $x); $param->AddParameter("SETVIEWCENTERY", $y); $param->AddParameter("SETVIEWSCALE", $this->viewScale); $param->AddParameter("BEHAVIOR", 3); //Layers + Selection //Apply file download parameters if specified if ($this->app->request->params("download") === "1" || $this->app->request->params("download") === "true") { $param->AddParameter("X-DOWNLOAD-ATTACHMENT", "true"); if ($this->app->request->params("downloadname")) { $param->AddParameter("X-DOWNLOAD-ATTACHMENT-NAME", $this->app->request->params("downloadname")); } } $this->ExecuteHttpRequest($req); if ($bCreatedSession === true) { $conn2 = new MgSiteConnection(); $user2 = new MgUserInformation($this->sessionId); $conn2->Open($user2); $site2 = $conn2->GetSite(); $site2->DestroySession($this->sessionId); } } catch (MgException $ex) { $this->OnException($ex); } }
public function DescribeRuntimeMap($sessionId, $mapName, $format) { $reqFeatures = $this->app->request->params("requestedfeatures"); $iconFormat = $this->app->request->params("iconformat"); $iconWidth = $this->app->request->params("iconwidth"); $iconHeight = $this->app->request->params("iconheight"); $iconsPerScaleRange = $this->app->request->params("iconsperscalerange"); $this->EnsureAuthenticationForSite($sessionId, false); $siteConn = new MgSiteConnection(); $siteConn->Open($this->userInfo); //Assign default values or coerce existing ones to their expected types if ($reqFeatures != null) { $reqFeatures = intval($reqFeatures); } if ($iconFormat == null) { $iconFormat = "PNG"; } if ($iconWidth == null) { $iconWidth = 16; } else { $iconWidth = intval($iconWidth); } if ($iconHeight == null) { $iconHeight = 16; } else { $iconHeight = intval($iconHeight); } if ($iconsPerScaleRange == null) { $iconsPerScaleRange = 25; } else { $iconsPerScaleRange = intval($iconsPerScaleRange); } if ($format == null) { $format = "xml"; } else { $format = strtolower($format); } $admin = new MgServerAdmin(); $admin->Open($this->userInfo); $version = explode(".", $admin->GetSiteVersion()); $bCanUseNative = false; if (intval($version[0]) > 2) { //3.0 or greater $bCanUseNative = true; } else { if (intval($version[0]) == 2 && intval($version[1]) >= 6) { //2.6 or greater $bCanUseNative = true; } } if ($bCanUseNative) { $req = new MgHttpRequest(""); $param = $req->GetRequestParam(); $param->AddParameter("OPERATION", "DESCRIBERUNTIMEMAP"); $param->AddParameter("VERSION", "2.6.0"); $param->AddParameter("SESSION", $sessionId); $param->AddParameter("MAPNAME", $mapName); $param->AddParameter("REQUESTEDFEATURES", $reqFeatures); $param->AddParameter("ICONSPERSCALERANGE", $iconsPerScaleRange); $param->AddParameter("ICONFORMAT", $iconFormat); $param->AddParameter("ICONWIDTH", $iconWidth); $param->AddParameter("ICONHEIGHT", $iconHeight); if ($format === "json") { $param->AddParameter("FORMAT", MgMimeType::Json); } else { $param->AddParameter("FORMAT", MgMimeType::Xml); } $this->ExecuteHttpRequest($req); } else { //Shim the response $resSvc = $siteConn->CreateService(MgServiceType::ResourceService); $mappingSvc = $siteConn->CreateService(MgServiceType::MappingService); $map = new MgMap($siteConn); $map->Open($mapName); $mapDefId = $map->GetMapDefinition(); $mapDefIdStr = $mapDefId->ToString(); $br = $this->DescribeRuntimeMapXml($mapDefIdStr, $map, $sessionId, $mapName, $iconFormat, $iconWidth, $iconHeight, $reqFeatures, $iconsPerScaleRange, $resSvc, $mappingSvc); if ($format == "json") { $this->OutputXmlByteReaderAsJson($br); } else { $this->OutputByteReader($br); } } }
public function RenderRuntimeMapLegend($sessionId, $mapName, $format) { $req = new MgHttpRequest(""); $param = $req->GetRequestParam(); $param->AddParameter("SESSION", $sessionId); $param->AddParameter("LOCALE", $this->app->config("Locale")); $param->AddParameter("CLIENTAGENT", "MapGuide REST Extension"); $param->AddParameter("CLIENTIP", $this->GetClientIp()); $param->AddParameter("OPERATION", "GETMAPLEGENDIMAGE"); $param->AddParameter("VERSION", "1.0.0"); $param->AddParameter("MAPNAME", $mapName); $param->AddParameter("FORMAT", strtoupper($format)); $width = $this->GetRequestParameter("width", null); if ($width != null) { $param->AddParameter("WIDTH", $width); } $height = $this->GetRequestParameter("height", null); if ($height != null) { $param->AddParameter("HEIGHT", $height); } $this->ExecuteHttpRequest($req); }
protected function EnsureAuthenticationForHttp($callback, $allowAnonymous = false, $agentUri = "", $nominatedSessionId = "", $mimeType = MgMimeType::Html) { //agent URI is only required if responses must contain a reference //back to the mapagent. This is not the case for most, if not all //our scenarios so the passed URI can be assumed to be empty most of the //time $req = new MgHttpRequest($agentUri); $param = $req->GetRequestParam(); //Try session id first $session = $this->app->request->params("session"); if ($session != null) { $param->AddParameter("SESSION", $session); } else { if ($nominatedSessionId !== "" && $nominatedSessionId !== null) { $param->AddParameter("SESSION", $nominatedSessionId); } else { $username = null; $password = ""; // Username/password extraction logic ripped from PHP implementation of the MapGuide AJAX viewer //TODO: Ripped from AJAX viewer. Use the abstractions provided by Slim // No session, no credentials explicitely passed. Check for HTTP Auth user/passwd. Under Apache CGI, the // PHP_AUTH_USER and PHP_AUTH_PW are not set. However, the Apache admin may // have rewritten the authentication information to REMOTE_USER. This is a // suggested approach from the Php.net website. // Has REMOTE_USER been rewritten? if (!isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['REMOTE_USER']) && preg_match('/Basic +(.*)$/i', $_SERVER['REMOTE_USER'], $matches)) { list($name, $password) = explode(':', base64_decode($matches[1])); $_SERVER['PHP_AUTH_USER'] = strip_tags($name); $_SERVER['PHP_AUTH_PW'] = strip_tags($password); } // REMOTE_USER may also appear as REDIRECT_REMOTE_USER depending on CGI setup. // Check for this as well. if (!isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['REDIRECT_REMOTE_USER']) && preg_match('/Basic (.*)$/i', $_SERVER['REDIRECT_REMOTE_USER'], $matches)) { list($name, $password) = explode(':', base64_decode($matches[1])); $_SERVER['PHP_AUTH_USER'] = strip_tags($name); $_SERVER['PHP_AUTH_PW'] = strip_tags($password); } // Finally, PHP_AUTH_USER may actually be defined correctly. If it is set, or // has been pulled from REMOTE_USER rewriting then set our USERNAME and PASSWORD // parameters. if (isset($_SERVER['PHP_AUTH_USER']) && strlen($_SERVER['PHP_AUTH_USER']) > 0) { $username = $_SERVER['PHP_AUTH_USER']; if (isset($_SERVER['PHP_AUTH_PW']) && strlen($_SERVER['PHP_AUTH_PW']) > 0) { $password = $_SERVER['PHP_AUTH_PW']; } } //If we have everything we need, put it into the MgHttpRequestParam if ($username != null) { $param->AddParameter("USERNAME", $username); if ($password !== "") { $param->AddParameter("PASSWORD", $password); } } else { if ($allowAnonymous === true) { $username = "******"; $param->AddParameter("USERNAME", $username); } else { $this->Unauthorized($mimeType); } } } } //All good if we get here. Set up common request parameters so upstream callers don't have to $param->AddParameter("LOCALE", $this->app->config("Locale")); $param->AddParameter("CLIENTAGENT", "MapGuide REST Extension"); $param->AddParameter("CLIENTIP", $this->GetClientIp()); //Apply file download parameters if specified if ($this->app->request->params("download") === "1" || $this->app->request->params("download") === "true") { $param->AddParameter("X-DOWNLOAD-ATTACHMENT", "true"); if ($this->app->request->params("downloadname")) { $param->AddParameter("X-DOWNLOAD-ATTACHMENT-NAME", $this->app->request->params("downloadname")); } } $callback($req, $param); }
public function QueryMapFeatures($sessionId, $mapName) { //TODO: Append only works in featurefilter mode. Add append support for geometry-based selections $layerNames = $this->app->request->params("layernames"); $geometry = $this->app->request->params("geometry"); $maxFeatures = $this->app->request->params("maxfeatures"); $selVariant = $this->app->request->params("selectionvariant"); $selColor = $this->app->request->params("selectioncolor"); $selFormat = $this->app->request->params("selectionformat"); $persist = $this->app->request->params("persist"); $reqData = $this->app->request->params("requestdata"); $featFilter = $this->app->request->params("featurefilter"); $bSelectionXml = $this->app->request->params("selectionxml"); $bAppend = $this->app->request->params("append"); $layerAttFilter = $this->app->request->params("layerattributefilter"); $format = $this->app->request->params("format"); //Convert or coerce to defaults if ($format == null) { $format = "xml"; } else { $format = strtolower($format); } if ($maxFeatures == null) { $maxFeatures = -1; } else { $maxFeatures = intval($maxFeatures); } if ($selFormat == null) { $selFormat = "PNG"; } else { $selFormat = strtoupper($selFormat); } if ($layerAttFilter == null) { $layerAttFilter = 3; } else { $layerAttFilter = intval($layerAttFilter); } if ($persist == null) { $persist = true; } else { $persist = $persist == "1" || $persist == "true"; } if ($bSelectionXml == null) { $bSelectionXml = true; } else { $bSelectionXml = $bSelectionXml == "1" || $bSelectionXml == "true"; } if ($bAppend == null) { $bAppend = true; } else { $bAppend = $bAppend == "1" || $bAppend == "true"; } if ($reqData == null) { $reqData = 0; } else { $reqData = intval($reqData); } if ($selColor == null) { $selColor = "0x0000FFFF"; } try { $this->EnsureAuthenticationForSite($sessionId); $siteConn = new MgSiteConnection(); $siteConn->Open($this->userInfo); $admin = new MgServerAdmin(); $admin->Open($this->userInfo); $version = explode(".", $admin->GetSiteVersion()); $bCanUseNative = false; //If appending, we can't use the native operation as that operation does not support appending if (intval($version[0]) > 2) { //3.0 or greater $bCanUseNative = !$bAppend; } else { if (intval($version[0]) == 2 && intval($version[1]) >= 6) { //2.6 or greater $bCanUseNative = !$bAppend; } } //$this->app->log->debug("APPEND: $bAppend"); //$this->app->log->debug("FILTER (Before): $featFilter"); if (!$bSelectionXml) { //Append only works in the absence of geometry if ($geometry == null && $featFilter != null) { $featFilter = $this->TranslateToSelectionXml($siteConn, $mapName, $featFilter, $bAppend); //$this->app->log->debug("FeatFilter: $featFilter"); } } else { //Append only works in the absence of geometry if ($geometry == null && $bAppend) { $featFilter = $this->AppendSelectionXml($siteConn, $mapName, $featFilter); } } //$this->app->log->debug("GEOMETRY: $geometry"); //$this->app->log->debug("FILTER: $featFilter"); //$this->app->log->debug("Can use native: $bCanUseNative"); if ($bCanUseNative) { $req = new MgHttpRequest(""); $param = $req->GetRequestParam(); $param->AddParameter("OPERATION", "QUERYMAPFEATURES"); $param->AddParameter("VERSION", "2.6.0"); $param->AddParameter("SESSION", $sessionId); $param->AddParameter("MAPNAME", $mapName); $param->AddParameter("GEOMETRY", $geometry); $param->AddParameter("SELECTIONVARIANT", $selVariant); $param->AddParameter("MAXFEATURES", $maxFeatures); $param->AddParameter("LAYERNAMES", $layerNames); $param->AddParameter("PERSIST", $persist ? "1" : "0"); $param->AddParameter("LAYERATTRIBUTEFILTER", $layerAttFilter); if ($featFilter != null) { $param->AddParameter("FEATUREFILTER", $featFilter); } $param->AddParameter("REQUESTDATA", $reqData); $param->AddParameter("SELECTIONCOLOR", $selColor); $param->AddParameter("SELECTIONFORMAT", $selFormat); if ($format === "json") { $param->AddParameter("FORMAT", MgMimeType::Json); } else { $param->AddParameter("FORMAT", MgMimeType::Xml); } $this->ExecuteHttpRequest($req); } else { //Shim the response $resSvc = $siteConn->CreateService(MgServiceType::ResourceService); $renderSvc = $siteConn->CreateService(MgServiceType::RenderingService); $layersToQuery = null; if ($layerNames != null) { $layersToQuery = new MgStringCollection(); $names = explode(",", $layerNames); foreach ($names as $name) { $layersToQuery->Add($name); } } $variant = 0; if ($selVariant === "TOUCHES") { $variant = MgFeatureSpatialOperations::Touches; } else { if ($selVariant === "INTERSECTS") { $variant = MgFeatureSpatialOperations::Intersects; } else { if ($selVariant === "WITHIN") { $variant = MgFeatureSpatialOperations::Within; } else { if ($selVariant === "ENVELOPEINTERSECTS") { $variant = MgFeatureSpatialOperations::EnvelopeIntersects; } } } } $map = new MgMap($siteConn); $map->Open($mapName); $selection = new MgSelection($map); $wktRw = new MgWktReaderWriter(); $selectGeom = null; if ($geometry != null) { $selectGeom = $wktRw->Read($geometry); } $featInfo = $renderSvc->QueryFeatures($map, $layersToQuery, $selectGeom, $variant, $featFilter, $maxFeatures, $layerAttFilter); $bHasNewSelection = false; if ($persist) { $sel = $featInfo->GetSelection(); if ($sel != null) { $selXml = $sel->ToXml(); //$this->app->log->debug("Query selection:\n$selXml"); if ($bAppend) { $selOrig = new MgSelection($map); $selOrig->Open($resSvc, $mapName); $selAppend = new MgSelection($map, $selXml); self::MergeSelections($selOrig, $selAppend); $selNewXml = $selOrig->ToXml(); //$this->app->log->debug("Appended selection:\n$selNewXml"); $selection->FromXml($selNewXml); } else { $selection->FromXml($selXml); } $bHasNewSelection = true; } $selection->Save($resSvc, $mapName); } // Render an image of this selection if requested $inlineSelectionImg = null; if (($reqData & self::REQUEST_INLINE_SELECTION) == self::REQUEST_INLINE_SELECTION && $bHasNewSelection) { $color = new MgColor($selColor); $renderOpts = new MgRenderingOptions($selFormat, self::RenderSelection | self::KeepSelection, $color); $inlineSelectionImg = $renderSvc->RenderDynamicOverlay($map, $selection, $renderOpts); } // Collect any attributes of selected features $bRequestAttributes = ($reqData & self::REQUEST_ATTRIBUTES) == self::REQUEST_ATTRIBUTES; $xml = $this->CollectQueryMapFeaturesResult($resSvc, $reqData, $featInfo, $selection, $bRequestAttributes, $inlineSelectionImg); $bs = new MgByteSource($xml, strlen($xml)); $bs->SetMimeType(MgMimeType::Xml); $br = $bs->GetReader(); if ($format == "json") { $this->OutputXmlByteReaderAsJson($br); } else { $this->OutputByteReader($br); } } } catch (MgException $ex) { $this->OnException($ex, $this->GetMimeTypeForFormat($format)); } }