success = false; $operation = false; $showOnlyDatasetMetadata = "false"; $showOnlyUnlinkedOrganizations = "false"; //parse request parameter if (isset($_REQUEST["registratingDepartments"]) & $_REQUEST["registratingDepartments"] != "") { //validate to csv integer list $testMatch = $_REQUEST["registratingDepartments"]; $pattern = '/^[\d,]*$/'; if (!preg_match($pattern,$testMatch)){ //echo 'registratingDepartments: '.$testMatch.' is not valid.
'; $resultObject->error->message = 'Parameter registratingDepartments is not valid (integer or cs integer list).'; echo json_encode($resultObject); die(); } $registratingDepartments = $testMatch; $testMatch = NULL; } if (isset($_REQUEST["syncDepartment"]) && $_REQUEST["syncDepartment"] !== "" && $_REQUEST["syncDepartment"] !== null) { $testMatch = $_REQUEST["syncDepartment"]; $pattern = '/^[0-9]*$/'; if (!preg_match($pattern,$testMatch)){ $resultObject->error->message = 'Parameter syncDepartment is not valid (integer).'; echo json_encode($resultObject); die(); } $syncDepartment = (integer)$testMatch; $testMatch = NULL; } if (isset($_REQUEST["orgaId"]) && $_REQUEST["orgaId"] !== "" && $_REQUEST["orgaId"] !== null) { $testMatch = $_REQUEST["orgaId"]; $pattern = '/^[0-9]*$/'; if (!preg_match($pattern,$testMatch)){ $resultObject->error->message = 'Parameter orgaId is not valid (integer).'; echo json_encode($resultObject); die(); } $orgaId = (integer)$testMatch; $testMatch = NULL; } if (isset($_REQUEST["operation"]) && $_REQUEST["operation"] !== "" && $_REQUEST["operation"] !== null) { $testMatch = $_REQUEST["operation"]; if (!($testMatch == 'listCatalogues' or $testMatch == 'syncCatalogue' or $testMatch == 'syncCsw' or $testMatch == 'syncCkan' or $testMatch == 'syncCkanOrganizations')){ $resultObject->error->message = 'Parameter operation is not valid (listCatalogues, syncCatalogue, syncCsw, syncCkan, syncCkanOrganizations).'; echo json_encode($resultObject); die(); } $operation = $testMatch; $testMatch = NULL; } if (isset($_REQUEST["userId"]) & $_REQUEST["userId"] != "") { $testMatch = $_REQUEST["userId"]; $pattern = '/^[0-9]*$/'; if (!preg_match($pattern,$testMatch)){ $resultObject->error->message = 'Parameter userId is not valid (integer).'; echo json_encode($resultObject); die(); } if ($testMatch !== Mapbender::session()->get("mb_user_id")) { $resultObject->error->message = 'Parameter userId is not equal to the userId from session information - maybe there is no current session!'; echo json_encode($resultObject); die(); } $userId = $testMatch; $testMatch = NULL; } else { $userId = Mapbender::session()->get("mb_user_id"); if ($userId == false) { $userId = PUBLIC_USER; } } if (isset($_REQUEST["compareTimestamps"]) & $_REQUEST["compareTimestamps"] != "") { $testMatch = $_REQUEST["compareTimestamps"]; if (!($testMatch == 'false' or $testMatch == 'true')){ $resultObject->error->message = 'Parameter compareTimestamps is not valid (false, true).'; echo json_encode($resultObject); die(); } switch ($testMatch) { case "false": $compareTimestamps = false; break; case "true": $compareTimestamps = true; break; } $testMatch = NULL; } if (isset($_REQUEST["outputFormat"]) & $_REQUEST["outputFormat"] != "") { $testMatch = $_REQUEST["outputFormat"]; if (!($testMatch == 'json' or $testMatch == 'debug')){ $resultObject->error->message = 'Parameter outputFormat is not valid (json, debug).'; echo json_encode($resultObject); die(); } $outputFormat = $testMatch; $testMatch = NULL; } if (isset($_REQUEST["showOnlyDatasetMetadata"]) & $_REQUEST["showOnlyDatasetMetadata"] != "") { $testMatch = $_REQUEST["showOnlyDatasetMetadata"]; if (!($testMatch == 'true' or $testMatch == 'false')){ echo 'Parameter showOnlyDatasetMetadata is not valid (true,false (default to false)).
'; die(); } switch ($testMatch) { case "true": $showOnlyDatasetMetadata = "true"; break; case "false": $showOnlyDatasetMetadata = "false"; break; } $testMatch = NULL; } if (isset($_REQUEST["showOnlyUnlinkedOrganizations"]) & $_REQUEST["showOnlyUnlinkedOrganizations"] != "") { $testMatch = $_REQUEST["showOnlyUnlinkedOrganizations"]; if (!($testMatch == 'true' or $testMatch == 'false')){ echo 'Parameter showOnlyUnlinkedOrganizations is not valid (true,false (default to false)).
'; die(); } switch ($testMatch) { case "true": $showOnlyUnlinkedOrganizations = "true"; break; case "false": $showOnlyUnlinkedOrganizations = "false"; break; } $testMatch = NULL; } /* Serverside function of the ckan sync tool for metadata / service providers return values should work similar to ckans api functions e.g. { "help": "Creates a package", "success": false, "error": { "message": "Access denied", "__type": "Authorization Error" } } { "help": "Creates a package", "success": true, "result": { "resultObj": "s.th." } } */ $syncCkanClass = new SyncCkan(); $syncCkanClass->mapbenderUserId = $userId; $syncCkanClass->compareTimestamps = $compareTimestamps; if (isset($syncDepartment)) { //write requested orga id to class to prohibit invocation of other orga comparism if one orga should be synced $syncCkanClass->syncOrgaId = $syncDepartment; } if ($operation == 'syncCkanOrganizations') { if ($userId !== "1") { echo "You are not root user and therefor not allowed to sync organizations with ckan!"; die(); } $sql = <<ckanApiKey = API_KEY; while ($row = db_fetch_array($result)) { unset($orga); $orga->serialId = $row['mb_group_id']; $orga->id = $row['mb_group_ckan_uuid']; $orga->department_address = $row['mb_group_address']; $orga->department_city = $row['mb_group_city']; $orga->department_postcode = $row['mb_group_postcode']; $orga->department_email = $row['mb_group_email']; $orga->title = $row['mb_group_title']; $orga->description = $row['mb_group_description']; $orga->image_url = $row['mb_group_logo_path']; $orga->image_display_url = $row['mb_group_logo_path']; $orga->is_organization = true; $orga->display_name = $row['mb_group_title']; $orga->state = "active"; $orga->type = "organization"; $orga->approval_status = "approved"; $orga->package_count = $row['number_of_opendata_ressources']; $orga->updated = $row['timestamp']; $returnObject[] = $orga; //if ($showOnlyUnlinkedOrganizations == "true") { //check if organization exists in connected ckan portal if (isset($orga->id)) { //try to get orga from external ckan and show it $requestPost = new stdClass(); //$requestPost->{'name'} = (string)$ckanOrgaPreferredName; //$requestPost->{'organizations'} = array((string)$ckanOrgaPreferredName); $requestPost->{'id'} = (string)$orga->id; $requestPostJson = json_encode($requestPost); //try to read orga $ckanResult = $syncCkanClass->getRemoteCkanOrga($requestPostJson); $ckanResultObject = json_decode($ckanResult); $e = new mb_notice("php/mod_syncCkan_server.php: check for existing orga in external ckan instance: ".$requestPostJson); if ($ckanResultObject->success == true) { //give back ckan id of organization $e = new mb_notice("php/mod_syncCkan_server.php: identical orga found in ckan"); //get revision_list of organization to get last change date! $ckanResultOrgaRevList = $syncCkanClass->getRemoteCkanOrgaRevList($requestPostJson); //$e = new mb_exception("orga rev list: ".$ckanResultOrgaRevList); //extract last timestamp: $ckanResultOrgaRevListObject = json_decode($ckanResultOrgaRevList); //check for update if needed! if ($ckanResultOrgaRevListObject->success == true) { $dateTimeCkanOrga = new DateTime($ckanResultOrgaRevListObject->result[0]->timestamp); } else { $dateTimeCkanOrga = new DateTime($ckanResultObject->result->created); } //$dateTimeCkanOrga = new DateTime($ckanResultObject->result->created); $dateTimeMapbenderOrga = new DateTime($orga->updated); $e = new mb_notice("php/mod_syncCkan_server.php: compare timestamp of organizations: datetime ckan: ".$dateTimeCkanOrga->format('Y-m-d H:i:s')." - datetime mapbender: ".$dateTimeMapbenderOrga->format('Y-m-d H:i:s')); if (($dateTimeCkanOrga < $dateTimeMapbenderOrga) || $ckanResultObject->result->state == "deleted") { $ckanOrgaRepresentation = $syncCkanClass->getInternalOrgaAsCkan($orga->serialId); $ckanResult = $syncCkanClass->updateRemoteCkanOrga($ckanOrgaRepresentation); $e = new mb_notice("php/mod_syncCkan_server.php: update organization! ".$ckanResult); //id needed!!!! } else { $e = new mb_notice("php/mod_syncCkan_server.php: no update of organization required!"); } } else { $e = new mb_notice("php/mod_syncCkan_server.php: organization not found in ckan - try to create it!"); //try to create it //forgotten to initialize a new group, when the uuid is alread available, but $ckanOrgaRepresentation = $syncCkanClass->getInternalOrgaAsCkan($orga->serialId); //$e = new mb_exception("php/mod_syncCkan_server.php: try to create group id: ".$orga->serialId." ".$ckanOrgaRepresentation); $ckanResult = $syncCkanClass->createRemoteCkanOrga($ckanOrgaRepresentation); //$e = new mb_exception("php/mod_syncCkan_server.php: result of creation: ". $ckanResult); $ckanResultObject = json_decode($ckanResult); if ($ckanResultObject->success == true) { $e = new mb_notice("php/mod_syncCkan_server.php: organization successfully created!"); } else { $e = new mb_notice("php/mod_syncCkan_server.php: an error occured when trying to create organization via ckan api!"); } } } else { //read ckan representation of internal group from registry - sync all ckan orgas with mapbender!!!! $ckanOrgaRepresentation = $syncCkanClass->getInternalOrgaAsCkan($orga->serialId); //$e = new mb_exception("php/mod_syncCkan_server.php: returned ckan orga from mapbender: ".$ckanOrgaRepresentation); $ckanOrgaRepresentationObject = json_decode($ckanOrgaRepresentation); $ckanOrgaPreferredName = $ckanOrgaRepresentationObject->name; $requestPost = new stdClass(); $requestPost->{'id'} = (string)$ckanOrgaPreferredName; $requestPostJson = json_encode($requestPost); //check if already exists and/or state is "deleted"- than update and set to active $ckanResult = $syncCkanClass->getRemoteCkanOrga($requestPostJson); //$e = new mb_exception("php/mod_syncCkan_server.php: remote ckan organization: ".$ckanResult); $ckanResultObject = json_decode($ckanResult); if ($ckanResultObject->success == false) { $e = new mb_notice("php/mod_syncCkan_server.php: organization not found - try to create it!"); //try to create it $ckanResult = $syncCkanClass->createRemoteCkanOrga($ckanOrgaRepresentation); //$e = new mb_exception("php/mod_syncCkan_server.php: result of creation: ". $ckanResult); $ckanResultObject = json_decode($ckanResult); if ($ckanResultObject->success == true) { //$e = new mb_exception("php/mod_syncCkan_server.php: organization successfully created!: "); //store uuid of external created ckan organization into mapbender database as foreign key $sql = "UPDATE mb_group SET mb_group_ckan_uuid = $1 WHERE mb_group_id = $2"; $v = array($ckanResultObject->result->id, $orga->serialId); $t = array('s', 'i'); $update_result = db_prep_query($sql,$v,$t); if(!$update_result) { throw new Exception("Database error updating mb_group table with ckan uuid attribute!"); return false; } //set orga->id for further requests $orga->id = $ckanResultObject->result->id; } else { $e = new mb_exception("an error occured! "); } } else { //a organization was found with the requested name - get the id from this organisation and fill it into the mapbender database before updating $sql = "UPDATE mb_group SET mb_group_ckan_uuid = $1 WHERE mb_group_id = $2"; $v = array($ckanResultObject->result->id, $orga->serialId); $t = array('s', 'i'); $update_result = db_prep_query($sql,$v,$t); if(!$update_result) { throw new Exception("Database error updating mb_group table with ckan uuid attribute!"); return false; } //set orga->id for further requests $orga->id = $ckanResultObject->result->id; //add the id to the json object from mapbender database $ckanOrgaRepresentationObject->id = $ckanResultObject->result->id; $ckanOrgaRepresentation = json_encode($ckanOrgaRepresentationObject); $ckanResultOrgaRevList = $syncCkanClass->getRemoteCkanOrgaRevList($requestPostJson); //$e = new mb_exception("orga rev list: ".$ckanResultOrgaRevList); //extract last timestamp: $ckanResultOrgaRevListObject = json_decode($ckanResultOrgaRevList); //check for update if needed! if ($ckanResultOrgaRevListObject->success == true) { $dateTimeCkanOrga = new DateTime($ckanResultOrgaRevListObject->result[0]->timestamp); } else { $dateTimeCkanOrga = new DateTime($ckanResultObject->result->created); } //check if timestamp older than from mapbender db or state of organization is deleted //compare timestamps $dateTimeCkanOrga = new DateTime($ckanResultObject->result->created); $dateTimeMapbenderOrga = new DateTime($orga->updated); if (($dateTimeCkanOrga < $dateTimeMapbenderOrga) || $ckanResultObject->result->state == "deleted") { $ckanResult = $syncCkanClass->updateRemoteCkanOrga($ckanOrgaRepresentation); //$e = new mb_exception("update organization ! ".$ckanResult); //id needed!!!! } } } //wether ckan orga was identified or created - check editor account ! //check if user for editing exists - if not create him automagically //TODO: add other editor for other template! - data_document_editor_{orga_id} $editingUserName = "geoportal_editor_".$orga->serialId; $requestPost = new stdClass(); $requestPost->{'id'} = $editingUserName; $requestPostJson = json_encode($requestPost); $ckanResultUser = $syncCkanClass->getRemoteCkanUser($requestPostJson); $ckanResultUserObject = json_decode($ckanResultUser); if ($ckanResultUserObject->success == true) { //check if user has already editor role in organization //update user $requestPost = new stdClass(); //get id from user if already exists - is needed for update since ckan 2.8+! $requestPost->{'id'} = $ckanResultUserObject->result->id; $requestPost->{'name'} = $editingUserName; $requestPost->{'email'} = "kontakt@geoportal.rlp.de"; $requestPost->{'password'} = "1234".$editingUserName."5678"; //groups for rlp:(transparenzgesetz,opendata), TODO configure this in ckan.conf //$requestPost->groups[0]->name = "transparenzgesetz"; //$requestPost->groups[1]->name = "opendata"; $requestPostJson = json_encode($requestPost); $ckanResultUser = $syncCkanClass->updateRemoteCkanUser($requestPostJson); $ckanResultUserObject = json_decode($ckanResultUser); } else { //create user $requestPost = new stdClass(); $requestPost->{'name'} = $editingUserName; $requestPost->{'email'} = "kontakt@geoportal.rlp.de"; $requestPost->{'password'} = "1234".$editingUserName."5678"; $requestPostJson = json_encode($requestPost); $ckanResultUser = $syncCkanClass->createRemoteCkanUser($requestPostJson); //$e = new mb_exception("get user: ".$ckanResultUser); $ckanResultUserObject = json_decode($ckanResultUser); } //read apikey: if ($ckanResultUserObject->success == true) { $apiKey = $ckanResultUserObject->result->apikey; $userId = $ckanResultUserObject->result->id; //store key into mapbender group table $sql = "UPDATE mb_group SET mb_group_ckan_api_key = $1 WHERE mb_group_id = $2"; $v = array($apiKey, $orga->serialId); $t = array('s', 'i'); $update_result = db_prep_query($sql,$v,$t); if(!$update_result) { throw new Exception("Database error updating mb_group table with ckan api-key attribute!"); return false; } } //add user with role editor to current organization //get membership if exists $requestPost = new stdClass(); $requestPost->{'id'} = $orga->id; $requestPost->{'object_type'} = "user"; $requestPost->{'capacity'} = "editor"; $requestPostJson = json_encode($requestPost); $ckanResultMember = $syncCkanClass->getRemoteCkanMember($requestPostJson); //$e = new mb_exception("get member: ".$ckanResultMember); $ckanResultMemberObject = json_decode($ckanResultMember); //$e = new mb_exception("number of editors: ".count($ckanResultMemberObject->result)); if (count($ckanResultMemberObject->result == 0)) { //add membership for editor $requestPost = new stdClass(); $requestPost->{'id'} = $orga->id; $requestPost->{'object'} = $userId; $requestPost->{'object_type'} = "user"; $requestPost->{'capacity'} = "editor"; $requestPostJson = json_encode($requestPost); $ckanResultMember = $syncCkanClass->createRemoteCkanMember($requestPostJson); //$e = new mb_exception("get member after creating: ".$ckanResultMember); } //add user with role editor to groups "transparenzgesetz" and "opendata" //add membership for editor if (defined("CKAN_EDITOR_DEFAULT_GROUPS") && is_array(CKAN_EDITOR_DEFAULT_GROUPS)) { foreach(CKAN_EDITOR_DEFAULT_GROUPS as $ckanGroup) { $requestPost = new stdClass(); $requestPost->{'id'} = $ckanGroup; $requestPost->{'object'} = $userId; $requestPost->{'object_type'} = "user"; $requestPost->{'capacity'} = "editor"; $requestPostJson = json_encode($requestPost); $ckanResultMember = $syncCkanClass->createRemoteCkanMember($requestPostJson); //$e = new mb_exception("get member after creating: ".$ckanResultMember); } } } header('Content-Type: application/json; charset='.CHARSET); echo json_encode($returnObject, JSON_NUMERIC_CHECK); die(); } if ($operation == 'listCatalogues') { //get orga id from mapbender group!! - where from? $departmentsArray = $syncCkanClass->getMapbenderOrganizations(); //invoke check for csw $result = new stdClass(); //check for datasets - ckan vs. csw $result->result = json_decode($syncCkanClass->getSyncListCswJson($departmentsArray, $listAllMetadataInJson = true)); $result->success = true; header('Content-type:application/json;charset=utf-8'); echo json_encode($result); die(); } if ($operation == 'syncCkan') { //check if user is allowed to sync for requested organisation //TODO - make code better //overwrite organization from cswId with right orgaId $test = $syncCkanClass->syncOrgaId; $syncCkanClass->syncOrgaId = $orgaId; //$e = new mb_exception($syncCkanClass->syncOrgaId); $departmentsArray = $syncCkanClass->getMapbenderOrganizations(); //rewind orgaid to cswid to get right synclist $syncCkanClass->syncOrgaId = $test; //TODO: alter csw based sync to give orgaId as parameter as this is done here $syncListJsonCkan = $syncCkanClass->getSyncListRemoteCkanJson($departmentsArray, $syncCkanClass->syncOrgaId, true); //for synching use right orga id for getting apikey for invoking $syncCkanClass->syncSingleCsw if (isset($orgaId) && $orgaId !== null) { $syncCkanClass->syncOrgaId = $orgaId; } $syncList = json_decode($syncListJsonCkan); if ($syncList->success = true) { foreach ($syncList->result->external_ckan as $orga) { //TODO try to sync single orga - the class has already set the syncOrgaId if wished! //if ($syncDepartment == $orga->id) { //overwrite result with result from sync process //$syncList = json_decode($syncCkanClass->syncSingleCsw(json_encode($orga))); //new function //$e = new mb_exception($syncListJsonCkan); $syncList = json_decode($syncCkanClass->syncSingleDataSource(json_encode($orga), "ckan")); //} } } //create new syncListJson $syncListJson = json_encode($syncList); header('Content-type:application/json;charset=utf-8'); echo $syncListJson; die(); } if ($operation == 'syncCsw') { //check if user is allowed to sync for requested organisation //TODO - make code better //overwrite organization from cswId with right orgaId $test = $syncCkanClass->syncOrgaId; $syncCkanClass->syncOrgaId = $orgaId; //$e = new mb_exception($syncCkanClass->syncOrgaId); $departmentsArray = $syncCkanClass->getMapbenderOrganizations(); //rewind orgaid to cswid to get right synclist $syncCkanClass->syncOrgaId = $test; $syncListJsonCsw = $syncCkanClass->getSyncListCswJson($departmentsArray, $syncCkanClass->syncOrgaId, true); //for synching use right orga id for getting apikey for invoking $syncCkanClass->syncSingleCsw $syncCkanClass->syncOrgaId = $orgaId; $syncList = json_decode($syncListJsonCsw); if ($syncList->success = true) { foreach ($syncList->result->external_csw as $orga) { //TODO try to sync single orga - the class has already set the syncOrgaId if wished! //if ($syncDepartment == $orga->id) { //overwrite result with result from sync process //$syncList = json_decode($syncCkanClass->syncSingleCsw(json_encode($orga))); //new function $syncList = json_decode($syncCkanClass->syncSingleDataSource(json_encode($orga), "portalucsw")); //} } } //create new syncListJson $syncListJson = json_encode($syncList); header('Content-type:application/json;charset=utf-8'); echo $syncListJson; die(); } $departmentsArray = $syncCkanClass->getMapbenderOrganizations(); //second parameter is listAllMetadataInJson ( = true) - it is needed if we want to sync afterwards. The syncList includes all necessary information about one organization $syncListJson = $syncCkanClass->getSyncListJson($departmentsArray, true); $syncList = json_decode($syncListJson); if ($syncList->success = true) { foreach ($syncList->result->geoportal_organization as $orga) { //try to sync single orga - the class has already set the syncOrgaId if wished! if ($syncDepartment == $orga->id) { //overwrite result with result from sync process //$syncList = json_decode($syncCkanClass->syncSingleOrga(json_encode($orga))); $syncList = json_decode($syncCkanClass->syncSingleDataSource(json_encode($orga), "mapbender", true)); } } } //create new syncListJson $syncListJson = json_encode($syncList); header('Content-type:application/json;charset=utf-8'); echo $syncListJson; ?>