private static function CheckPermissions($resSvc, $resId)
 {
     $perms = new stdClass();
     $perms->AllowInsert = false;
     $perms->AllowUpdate = false;
     $perms->AllowDelete = false;
     $perms->UseTransaction = false;
     //A session-based user can do whatever they want on a session-based Feature Source
     if ($resId->GetRepositoryType() == MgRepositoryType::Session) {
         $perms->AllowInsert = true;
         $perms->AllowUpdate = true;
         $perms->AllowDelete = true;
         $perms->UseTransaction = false;
         return $perms;
     }
     $resHeader = $resSvc->GetResourceHeader($resId);
     $headerStr = MgUtils::Xml2Json($resHeader->ToString());
     $header = json_decode($headerStr);
     if (isset($header->ResourceDocumentHeader)) {
         if (isset($header->ResourceDocumentHeader->Metadata)) {
             if (isset($header->ResourceDocumentHeader->Metadata->Simple)) {
                 if (isset($header->ResourceDocumentHeader->Metadata->Simple->Property)) {
                     foreach ($header->ResourceDocumentHeader->Metadata->Simple->Property as $prop) {
                         if ($prop->Name === self::PROP_ALLOW_INSERT && $prop->Value === "1") {
                             $perms->AllowInsert = true;
                         } else {
                             if ($prop->Name === self::PROP_ALLOW_UPDATE && $prop->Value === "1") {
                                 $perms->AllowUpdate = true;
                             } else {
                                 if ($prop->Name === self::PROP_ALLOW_DELETE && $prop->Value === "1") {
                                     $perms->AllowDelete = true;
                                 } else {
                                     if ($prop->Name === self::PROP_USE_TRANSACTION && $prop->Value === "1") {
                                         $perms->UseTransaction = true;
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return $perms;
 }
Example #2
0
 public function EnumerateDataFiles($uriParts, $format)
 {
     //Check for unsupported representations
     $fmt = $this->ValidateRepresentation($format, array("xml", "json"));
     $uriPath = self::SanitizeUriPath(implode("/", $uriParts));
     $this->EnsureAuthenticationForSite();
     $this->ValidateAuthorPrivileges($fmt == "json" ? MgMimeType::Json : MgMimeType::Xml);
     $path = realpath($this->app->config("AppRootDir") . "/" . $this->app->config("GeoRest.ConfigPath") . "/{$uriPath}");
     if ($path === false) {
         $this->NotFound($this->app->localizer->getText("E_NO_DATA_CONFIGURATION_FOR_URI", $uriPath));
     } else {
         $resp = "<DataConfigurationFileList>";
         $files = scandir($path);
         foreach ($files as $f) {
             if ($f === "." || $f === ".." || $f === "restcfg.json") {
                 continue;
             }
             $resp .= "<File>" . $f . "</File>";
         }
         $resp .= "</DataConfigurationFileList>";
         if ($fmt == "json") {
             $this->app->response->header("Content-Type", MgMimeType::Json);
             $this->app->response->setBody(MgUtils::Xml2Json($resp));
         } else {
             $this->app->response->header("Content-Type", MgMimeType::Xml);
             $this->app->response->setBody($resp);
         }
     }
 }
Example #3
0
 public function UpdateMapLayersAndGroups($sessionId, $mapName, $format)
 {
     //Check for unsupported representations
     $fmt = $this->ValidateRepresentation($format, array("xml", "json"));
     try {
         $this->EnsureAuthenticationForSite($sessionId);
         $siteConn = new MgSiteConnection();
         $siteConn->Open($this->userInfo);
         $resSvc = $siteConn->CreateService(MgServiceType::ResourceService);
         $map = new MgMap($siteConn);
         $map->Open($mapName);
         if ($fmt == "json") {
             $body = $this->app->request->getBody();
             $json = json_decode($body);
             if ($json == NULL) {
                 throw new Exception($this->app->localizer->getText("E_MALFORMED_JSON_BODY"));
             }
         } else {
             $body = $this->app->request->getBody();
             $jsonStr = MgUtils::Xml2Json($body);
             $json = json_decode($jsonStr);
         }
         if (!isset($json->UpdateMap)) {
             throw new Exception($this->app->localizer->getText("E_MALFORMED_JSON_BODY"));
         }
         /*
         Expected structure
         
         /UpdateMap
             /Operation [1...n]
                 /Type - [AddLayer|UpdateLayer|RemoveLayer|AddGroup|UpdateGroup|RemoveGroup]
                 /Name
                 /ResourceId
                 /SetLegendLabel
                 /SetDisplayInLegend
                 /SetExpandInLegend
                 /SetVisible
                 /SetSelectable
                 /InsertAt
         */
         $layers = $map->GetLayers();
         $groups = $map->GetLayerGroups();
         $um = $json->UpdateMap;
         $updateStats = new stdClass();
         $updateStats->AddedLayers = 0;
         $updateStats->UpdatedLayers = 0;
         $updateStats->RemovedLayers = 0;
         $updateStats->AddedGroups = 0;
         $updateStats->UpdatedGroups = 0;
         $updateStats->RemovedGroups = 0;
         $this->app->log->debug("Operations found: " . count($um->Operation));
         for ($i = 0; $i < count($um->Operation); $i++) {
             $op = $um->Operation[$i];
             switch ($op->Type) {
                 case "AddLayer":
                     $resId = new MgResourceIdentifier($op->ResourceId);
                     $layer = new MgLayer($resId, $resSvc);
                     $layer->SetName($op->Name);
                     self::ApplyCommonLayerProperties($layer, $op, $groups);
                     if (isset($op->InsertAt)) {
                         $layers->Insert(intval($op->InsertAt), $layer);
                     } else {
                         $layers->Add($layer);
                     }
                     $this->app->log->debug("Add Layer: " . $op->Name);
                     $updateStats->AddedLayers++;
                     break;
                 case "UpdateLayer":
                     $layer = $layers->GetItem($op->Name);
                     if (self::ApplyCommonLayerProperties($layer, $op, $groups)) {
                         $this->app->log->debug("Updated Layer: " . $op->Name);
                         $updateStats->UpdatedLayers++;
                     }
                     break;
                 case "RemoveLayer":
                     $layer = $layers->GetItem($op->Name);
                     if ($layers->Remove($layer)) {
                         $this->app->log->debug("Removed Layer: " . $op->Name);
                         $updateStats->RemovedLayers++;
                     }
                     break;
                 case "AddGroup":
                     $group = new MgLayerGroup($op->Name);
                     self::ApplyCommonGroupProperties($group, $op, $groups);
                     if (isset($op->InsertAt)) {
                         $groups->Insert(intval($op->InsertAt), $group);
                     } else {
                         $groups->Add($group);
                     }
                     $this->app->log->debug("Add Group: " . $op->Name);
                     $updateStats->AddedGroups++;
                     break;
                 case "UpdateGroup":
                     $gidx = $groups->IndexOf($op->Name);
                     if ($gidx < 0) {
                         if ($op->AddIfNotExists) {
                             $group = new MgLayerGroup($op->Name);
                             self::ApplyCommonGroupProperties($group, $op, $groups);
                             if (isset($op->InsertAt)) {
                                 $groups->Insert(intval($op->InsertAt), $group);
                             } else {
                                 $groups->Add($group);
                             }
                             $this->app->log->debug("Add Group: " . $op->Name);
                             $updateStats->AddedGroups++;
                         } else {
                             throw new Exception($this->app->localizer->getText("E_GROUP_NOT_FOUND", $op->Name));
                         }
                     } else {
                         $group = $groups->GetItem($gidx);
                         if (self::ApplyCommonGroupProperties($group, $op, $groups)) {
                             $this->app->log->debug("Updated Group: " . $op->Name);
                             $updateStats->UpdatedGroups++;
                         }
                     }
                     break;
                 case "RemoveGroup":
                     $group = $groups->GetItem($op->Name);
                     if ($groups->Remove($group)) {
                         $this->app->log->debug("Removed Group: " . $op->Name);
                         $updateStats->RemovedGroups++;
                     }
                     break;
             }
         }
         if ($updateStats->AddedLayers > 0 || $updateStats->UpdatedLayers > 0 || $updateStats->RemovedLayers > 0 || $updateStats->AddedGroups > 0 || $updateStats->UpdatedGroups > 0 || $updateStats->RemovedGroups > 0) {
             $map->Save();
         }
         $response = "<UpdateMapResult>";
         $response .= "<AddedLayers>";
         $response .= $updateStats->AddedLayers;
         $response .= "</AddedLayers>";
         $response .= "<UpdatedLayers>";
         $response .= $updateStats->UpdatedLayers;
         $response .= "</UpdatedLayers>";
         $response .= "<RemovedLayers>";
         $response .= $updateStats->RemovedLayers;
         $response .= "</RemovedLayers>";
         $response .= "<AddedGroups>";
         $response .= $updateStats->AddedGroups;
         $response .= "</AddedGroups>";
         $response .= "<UpdatedGroups>";
         $response .= $updateStats->UpdatedGroups;
         $response .= "</UpdatedGroups>";
         $response .= "<RemovedGroups>";
         $response .= $updateStats->RemovedGroups;
         $response .= "</RemovedGroups>";
         $response .= "</UpdateMapResult>";
         $bs = new MgByteSource($response, strlen($response));
         $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));
     }
 }
Example #4
0
 protected function OutputMgStringCollection($strCol, $mimeType = MgMimeType::Xml)
 {
     $content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><StringCollection />";
     if ($strCol != null) {
         // MgStringCollection::ToXml() doesn't seem to be reliable in PHP (bug?), so do this manually
         $count = $strCol->GetCount();
         $content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><StringCollection>";
         for ($i = 0; $i < $count; $i++) {
             $value = MgUtils::EscapeXmlChars($strCol->GetItem($i));
             $content .= "<Item>{$value}</Item>";
         }
         $content .= "</StringCollection>";
     }
     if ($mimeType === MgMimeType::Json) {
         $content = MgUtils::Xml2Json($content);
     }
     $this->app->response->header("Content-Type", $mimeType);
     $this->app->response->setBody($content);
 }
 public function TransformCoordinates()
 {
     $source = $this->app->request->post("from");
     $target = $this->app->request->post("to");
     $coordList = $this->app->request->post("coords");
     $format = $this->app->request->post("format");
     if ($format == null) {
         $format = "xml";
     }
     $fmt = $this->ValidateRepresentation($format, array("xml", "json"));
     if ($source == null) {
         $this->BadRequest($this->app->localizer->getText("E_MISSING_REQUIRED_PARAMETER", "from"), $this->GetMimeTypeForFormat($format));
     }
     if ($target == null) {
         $this->BadRequest($this->app->localizer->getText("E_MISSING_REQUIRED_PARAMETER", "to"), $this->GetMimeTypeForFormat($format));
     }
     if ($coordList == null) {
         $this->BadRequest($this->app->localizer->getText("E_MISSING_REQUIRED_PARAMETER", "coords"), $this->GetMimeTypeForFormat($format));
     }
     try {
         $factory = new MgCoordinateSystemFactory();
         $sourceCs = $factory->CreateFromCode($source);
         $targetCs = $factory->CreateFromCode($target);
         $trans = $factory->GetTransform($sourceCs, $targetCs);
         $coords = explode(",", $coordList);
         $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?><CoordinateCollection>";
         foreach ($coords as $coordPair) {
             $tokens = explode(" ", trim($coordPair));
             $tokenCount = count($tokens);
             if ($tokenCount === 2) {
                 $txCoord = $trans->Transform(floatval($tokens[0]), floatval($tokens[1]));
                 $output .= "<Coordinate><X>" . $txCoord->GetX() . "</X><Y>" . $txCoord->GetY() . "</Y></Coordinate>";
             } else {
                 //TODO: We should accept a partial response, but there's currently no way an empty <Coordinate/> tag survives the
                 //XML to JSON conversion, so we have to throw lest we return an inconsisten partial result
                 $this->ServerError($this->app->localizer->getText("E_INVALID_COORDINATE_PAIR", $coordPair, $tokenCount), $this->GetMimeTypeForFormat($format));
             }
         }
         $output .= "</CoordinateCollection>";
         if ($fmt === "json") {
             $this->app->response->header("Content-Type", MgMimeType::Json);
             $json = MgUtils::Xml2Json($output);
             $this->app->response->write($json);
         } else {
             $this->app->response->header("Content-Type", MgMimeType::Xml);
             $this->app->response->write($output);
         }
     } catch (MgException $ex) {
         $this->OnException($ex, $this->GetMimeTypeForFormat($format));
     }
 }