Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Proxy Pull Request #296

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ module.exports = (grunt) ->
warnings: false
properties: true
sequences: true
cascade: true
#cascade: true
files:
"js/c.min.js":["js/c.js"]
"js/admin.min.js":["js/admin.js"]
Expand Down
55 changes: 30 additions & 25 deletions admin-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,7 @@ function mintExpedition($projectLink, $projectTitle, $publicProject = false, $as
* @return array
***/
global $db;
ini_set("error_log", "./admin-api-fims.log");
$fimsDefaultHeaders = array(
'Content-type: application/x-www-form-urlencoded',
'Accept: application/json',
Expand Down Expand Up @@ -1516,6 +1517,7 @@ function mintExpedition($projectLink, $projectTitle, $publicProject = false, $as
$ctx = stream_context_create($params);
$rawResponse = file_get_contents($fimsAuthUrl, false, $ctx);
if ($rawResponse === false) {
error_log("POST login failed!! Sent post:\n\tTarget URL:\t".$fimsAuthUrl."\n\tParameters:\t".print_r($params, true)."\n\tFull Context:\t".print_r($ctx, True)."\n\nResponse: ".print_r($rawResponse, true));
throw(new Exception("Fatal FIMS communication error 005 (No Response)"));
}
$loginHeaders = $http_response_header;
Expand Down Expand Up @@ -1556,6 +1558,7 @@ function mintExpedition($projectLink, $projectTitle, $publicProject = false, $as
try {
$errorMessageRaw = print_r(error_get_last(), true);
$errorMessage = json_encode(error_get_last());
error_log("POST mint failed!! \nError: $errorMessageRaw \nSent post:\n\tTarget URL:\t".$fimsMintUrl."\n\tParameters:\t".print_r($params, true)."\n\tFull Context:\t".print_r($ctx, True)."\n\nResponse: ".print_r($rawResponse, true));
if (empty($errorMessage)) {
throw(new Exception("BadEncode"));
}
Expand All @@ -1582,6 +1585,7 @@ function mintExpedition($projectLink, $projectTitle, $publicProject = false, $as
$ctx = stream_context_create($params);
$rawReauthResponse = file_get_contents($fimsAuthUrl, false, $ctx);
if ($rawReauthResponse === false) {
error_log("POST reauth failed!! Sent post:\n\tTarget URL:\t".$fimsAuthUrl."\n\tParameters:\t".print_r($params, true)."\n\tFull Context:\t".print_r($ctx, True)."\n\nResponse: ".print_r($rawReauthResponse, true));
throw(new Exception("Fatal FIMS communication error 009 (No Response)"));
}
$loginHeaders = $http_response_header;
Expand All @@ -1596,6 +1600,7 @@ function mintExpedition($projectLink, $projectTitle, $publicProject = false, $as
}
$loginResponse = json_decode($rawReauthResponse, true);
if (empty($loginResponse['url'])) {
error_log("POST login failed (009)!! Sent post:\n\tTarget URL:\t".$fimsAuthUrl."\n\tParameters:\t".print_r($params, true)."\n\tFull Context:\t".print_r($ctx, True)."\n\nResponse: ".print_r($rawRawResponse, true));
throw(new Exception('Invalid Login Response E093'));
}
# Try to fetch the expedition
Expand Down Expand Up @@ -2027,34 +2032,34 @@ function superuserEditUser($get)
);
}
switch ($editAction) {
case "delete":
$dryRun = $uf->forceDeleteCurrentUser();
$targetUid = $dryRun["target_user"];
if ($targetUid != $target) {
# Should never happen
case "delete":
$dryRun = $uf->forceDeleteCurrentUser();
$targetUid = $dryRun["target_user"];
if ($targetUid != $target) {
# Should never happen
return array(
"status" => false,
"error" => "MISMATCHED_TARGETS",
"human_error" => "The system encountered an error confirming target for deletion",
"obj_target" => $targetUid,
"post_target" => $target,
);
}
return $uf->forceDeleteCurrentUser(true);
break;
case "reset":
return array(
"status" => false,
"error" => "MISMATCHED_TARGETS",
"human_error" => "The system encountered an error confirming target for deletion",
"obj_target" => $targetUid,
"post_target" => $target,
"error" => "Incomplete"
);
break;
default:
return array(
"status" => false,
"error" => "INVALID_CHANGE_TYPE",
"human_error" => "We didn't recognize this change type",
"change_type_provided" => $editAction,
);
}
return $uf->forceDeleteCurrentUser(true);
break;
case "reset":
return array(
"status" => false,
"error" => "Incomplete"
);
break;
default:
return array(
"status" => false,
"error" => "INVALID_CHANGE_TYPE",
"human_error" => "We didn't recognize this change type",
"change_type_provided" => $editAction,
);
}
} catch (Exception $e) {
return array(
Expand Down
114 changes: 99 additions & 15 deletions api.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function elapsed($start_time = null)
doCartoSqlApiPush($_REQUEST);
break;
case 'validate':
doAWebValidate($_REQUEST);
returnAjax(doAWebValidate($_REQUEST));
break;
case 'is_human':
validateCaptcha($_REQUEST);
Expand Down Expand Up @@ -601,9 +601,11 @@ function tsvHelper($tsv)
function doAWebValidate($get)
{
/***
* Validate a taxon with Amphibiaweb.
*
* @param array $get ->
***/
$amphibiaWebListTarget = 'http://amphibiaweb.org/amphib_names.txt';
$amphibiaWebListTarget = 'https://amphibiaweb.org/amphib_names.txt';
$localAWebTarget = dirname(__FILE__).'/aweb_list.txt';
$dayOffset = 60 * 60 * 24;
$response = array(
Expand All @@ -625,11 +627,16 @@ function doAWebValidate($get)
if (filemtime($localAWebTarget) + $dayOffset < time()) {
# Fetch a new copy
$aWebList = file_get_contents($amphibiaWebListTarget);
$h = fopen($localAWebTarget, 'w+');
$bytes = fwrite($h, $aWebList);
fclose($h);
if ($bytes === false) {
$response['notices'][] = "Couldn't write updated AmphibiaWeb list to $localAWebTarget";
if (strlen($aWebList) > 0) {
$h = fopen($localAWebTarget, 'w+');
$bytes = fwrite($h, $aWebList);
fclose($h);
if ($bytes === false) {
$response['notices'][] = "Couldn't write updated AmphibiaWeb list to $localAWebTarget";
}
} else {
$response['notices'][] = "Got an empty AmphibiaWeb list, using an outdated list";
$response['notices'][] = "List age: ".time()-filemtime($localAWebTarget)."s";
}
}
$response['aweb_list_age'] = time() - filemtime($localAWebTarget);
Expand Down Expand Up @@ -977,8 +984,8 @@ function doAWebValidate($get)
}
$response['status'] = true;
# Note that Unicode characters may return escaped! eg, \u00e9.
$response['validated_taxon'] = $aWebPretty;
returnAjax($response);
$response['validated_taxon'] = $aWebPretty;
return $response;
}


Expand Down Expand Up @@ -1764,7 +1771,7 @@ function updateTaxonRecordHigherInformation()
function getTaxonData($taxonBase, $skipFetch = false)
{
/***
*
* Fetch the data for a given taxon
***/
global $db;
foreach ($taxonBase as $key => $value) {
Expand Down Expand Up @@ -1813,8 +1820,24 @@ function getTaxonData($taxonBase, $skipFetch = false)
);
}
# Check ours
global $login_status;
$uid = $login_status['detail']['uid'];
$suFlag = $login_status['detail']['userdata']['su_flag'];
$isSu = boolstr($suFlag);
if ($isSu !== true) {
$authorizedIntersectQuery = "SELECT `project_id` FROM `".$db->getTable()."` WHERE `public` is true";
if (!empty($uid)) {
$authorizedIntersectQuery .= " OR `access_data` LIKE '%".$uid."%' OR `author` LIKE '%".$uid."%'";
}
} else {
# A superuser gets to view everything
$authorizedIntersectQuery = "SELECT `project_id` FROM `".$db->getTable()."`";
}

$authorizedIntersect = "INNER JOIN ($authorizedIntersectQuery) AS authorized ON authorized.project_id = ";

$taxonString = $taxonBase["genus"]." ".$taxonBase["species"];
$countQuery = "select `project_id`, `project_title` from `".$db->getTable()."` where sampled_species like '%$taxonString%'";
$countQuery = "select records.project_id, `project_title` from `".$db->getTable()."` AS records $authorizedIntersect records.project_id WHERE records.sampled_species LIKE '%$taxonString%'";
$r = mysqli_query($db->getLink(), $countQuery);
$adpData = array();
if ($r !== false) {
Expand All @@ -1828,11 +1851,13 @@ function getTaxonData($taxonBase, $skipFetch = false)
}
$adpData["samples"] = 0;
$adpData["countries"] = array();
$samplesQuery = "select `country`,`diseasetested`, `diseasedetected`, `fatal` from `records_list` where `genus`='".$taxonBase["genus"]."' and `specificepithet`='".$taxonBase["species"]."' order by `diseasetested`";
$samplesQuery = "SELECT records.project_id, `country`,`diseasetested`, `diseasedetected`, `fatal` FROM `records_list` AS records $authorizedIntersect records.project_id WHERE `genus`='".$taxonBase["genus"]."' AND `specificepithet`='".$taxonBase["species"]."' ORDER BY `diseasetested`";
$r = mysqli_query($db->getLink(), $samplesQuery);
$taxonBreakdown = array();
$debug = array("query" => $samplesQuery);
if ($r !== false) {
while ($row = mysqli_fetch_assoc($r)) {
$debug[] = $row;
if (!in_array($row["country"], $adpData["countries"])) {
$adpData["countries"][] = $row["country"];
}
Expand Down Expand Up @@ -1883,6 +1908,11 @@ function getTaxonData($taxonBase, $skipFetch = false)
}
}
$adpData["samples"] = mysqli_num_rows($r);
} else {
$debug[] = array(
"error" => "BAD_RESULT",
"mysql" => mysqli_error($l),
);
}
$adpData["disease_data"] = $taxonBreakdown;
try {
Expand All @@ -1902,6 +1932,7 @@ function getTaxonData($taxonBase, $skipFetch = false)
);
$response = array(
"status" => true,
# "debug" => $debug,
"taxon" => array(
"genus" => $taxonBase["genus"],
"species" => $taxonBase["species"],
Expand All @@ -1921,7 +1952,7 @@ function getTaxonData($taxonBase, $skipFetch = false)
}


function getTaxonIucnData($taxonBase)
function getTaxonIucnData($taxonBase, $recursed = false)
{
/***
* Get the IUCN result for a given taxon
Expand All @@ -1937,6 +1968,10 @@ function getTaxonIucnData($taxonBase)
"error" => "REQUIRED_COLS_MISSING",
);
}
if ($recursed) {
# Logging
# error_log("Recursed entry calling with ".json_encode($taxonBase));
}
// $params = array(
// "genus" => $taxonBase["genus"],
// "species" => $taxonBase["species"],
Expand Down Expand Up @@ -1970,6 +2005,8 @@ function getTaxonIucnData($taxonBase)
"NE" => "Not Evaluated",
);
# Set up so that we can skip this step if need be
$badIucn = false;
$badTaxon = false;
$doIucn = true;
if ($doIucn === true) {
# IUCN returns an empty result unless "%20" is used to separate the
Expand All @@ -1986,6 +2023,10 @@ function getTaxonIucnData($taxonBase)
$response["iucn_category"] = $iucnCategoryMap[$response["iucn"]["category"]];
if (empty($response["iucn_category"])) {
$response["iucn_category"] = "No Data";
$badTaxon = true;
$response["status"] = false;
} else {
$response["status"] = true;
}
} else {
$response["error"] = "INVALID_IUCN_RESPONSE";
Expand All @@ -1994,11 +2035,50 @@ function getTaxonIucnData($taxonBase)
"raw_response" => $iucnRawResponse,
"parsed_response" => $iucnResponse,
);
$badIucn = true;
$response["status"] = false;
}
} else {
// What are we even doing here
}

if ($badTaxon) {
# Try synonyms
# error_log("Bad taxon, trying synonyms");
$validation = doAWebValidate(array(
"genus" => $taxonBase["genus"],
"species" => $taxonBase["species"],
));
if ($validation["status"]) {
$checkSynonyms = array(
$validation["validated_taxon"]["gaa_name"],
);
$itisEntries = $validation["validated_taxon"]["itis_names"];
$synonymEntries = $validation["validated_taxon"]["synonym_names"];
foreach (explode(",", $itisEntries) as $entry) {
$checkSynonyms[] = $entry;
}
foreach (explode(",", $synonymEntries) as $entry) {
$checkSynonyms[] = $entry;
}
# Now we have a list of all known synonyms
foreach ($checkSynonyms as $taxon) {
# error_log("Checking synonym '".$taxon."' for ".json_encode($taxonBase));
$taxonParts = explode(" ", strtolower($taxon));
$genus = $taxonParts[0];
$species = $taxonParts[1];
set_time_limit(15); # Prevent timing out unless a
# specific lookup actually fails
$responseTmp = getTaxonIucnData(array(
"genus" => $genus,
"species" => $species,
), true);
if ($responseTmp["status"] === true) {
$response = $responseTmp;
break;
}
}
}
}
return $response;
}

Expand Down Expand Up @@ -2030,12 +2110,16 @@ function getTaxonAWebData($taxonBase)
$awebReplacedResponse = str_replace($replaceSearch, "", $awebRawResponse);
$iter = 1;
$awebEscapeTags = preg_replace('%<!\[cdata\[((?:(?!\]\]>).)*?)<(p|i|a)(?:\s*href=.*?)?>(.*?)</\g{2}>(.*?)\]\]>%sim', '<![CDATA[${1}&lt;${2}&gt;${3}&lt;/${2}&gt;${4}]]>', $awebReplacedResponse, -1, $tagCount);
# error_log($awebEscapeTags);
while ($tagCount > 0) {
$replaced = preg_replace('%<!\[cdata\[((?:(?!\]\]>).)*?)<(p|i|a)(?:\s*href=.*?)?>(.*?)</\g{2}>(.*?)\]\]>%sim', '<![CDATA[${1}&lt;${2}&gt;${3}&lt;/${2}&gt;${4}]]>', $awebEscapeTags, -1, $tagCount);
$replaced = preg_replace('%<!\[cdata\[((?:(?!\]\]>).)*?)<(p|i|a)(?:\s*href=.*?)?>(.*?)</\g{2}>(.*?)\]\]>%sim', '<![CDATA[${1}&lt;${2}&gt;${3}&lt;/${2}&gt;${4}]]>', $awebEscapeTags, 500, $tagCount);
if (!empty($replaced)) {
$awebEscapeTags = $replaced;
}
++$iter;
if ($iter > 500) {
break;
}
}
$awebNoCdata = preg_replace('%<!\[cdata\[\s*?([\w\- ,;:\'"\ts\x{0080}-\x{017F}\(\)\/\.\r\n\?\&=]*?)\s*?\]\]>%usim', '${1}', $awebEscapeTags);

Expand Down
Loading