cfgReadChar('database.host');
$db_name = $savvyext->cfgReadChar('database.name');
$db_user = $savvyext->cfgReadChar('database.user');
$db_pass = $savvyext->cfgReadChar('database.pass');
$db_port = $savvyext->cfgReadLong('database.port');
$connstr = "host=${db_host} port=${db_port} dbname=${db_name} user=${db_user} password=${db_pass}";
$conn = pg_connect($connstr);
//$_GET['id'] = 8946; // DEBUG
if (isset($_GET['id']) && $_GET['id']>0) {
$q = "select a.id,a.duration,a.distance,a.location_start_id,a.location_end_id,";
$q.= "b.address as location_start,b.latitude as location_start_lat,b.longitude AS location_start_lng,";
$q.= "c.address as location_end,c.latitude as location_end_lat,c.longitude AS location_end_lng";
$q.= " from parsedemail_item a, address b, address c ";
$q.= " where a.id=".((int)$_GET['id'])." and b.id=a.location_start_id and c.id=a.location_end_id";
$r = pg_query($conn,$q);
if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
$payload = "{
\"origin\":\"".$f["location_start"]."\",
\"destination\":\"".$f["location_end"]."\",
\"member_id\":13,
\"transport_provider_id\":5,
\"trackedemail_item_id\":".$f["id"].",
\"country\":\"SG\",
\"group_quote_id\":0
}";
$encrypted_payload = bin2hex(
openssl_encrypt(
$payload,
$encryptionAlg,
$encryptionKey,
OPENSSL_RAW_DATA,
$encryptionIV
));
$postdata = "{\"encrypted_payload\": \"${encrypted_payload}\"}";
$url = "http://svrsavvy.sworks.float.sg/SAVVY/trips/api/quote";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($postdata),
'Authorization: Server-Token ' . $httpAuthToken)
);
$body = curl_exec($ch);
$result = json_decode($body,true);
$decrypted = openssl_decrypt(
hex2bin(
$result['payload']
),
$encryptionAlg,
$encryptionKey,
OPENSSL_RAW_DATA,
$encryptionIV
);
$payload = json_decode($decrypted, true);
error_log($body);
if (is_array($payload) && array_key_exists('id',$payload) && $payload['id']>0) {
if (array_key_exists('cost',$payload) && $payload['cost']>0) {
echo $payload['cost'];
exit();
}
if (array_key_exists('completed',$payload) && $payload['completed']!='') {
http_response_code(404);
echo "Quote service failed for " . $_GET['id'];
}
$startTime = time();
while (true) {
sleep(1);
$url = "http://svrsavvy.sworks.float.sg/SAVVY/trips/api/quote/".$payload['id'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Server-Token ' . $httpAuthToken)
);
$body = curl_exec($ch);
$result = json_decode($body,true);
$decrypted = openssl_decrypt(
hex2bin(
$result['payload']
),
$encryptionAlg,
$encryptionKey,
OPENSSL_RAW_DATA,
$encryptionIV
);
$payload = json_decode($decrypted, true);
if (is_array($payload) && array_key_exists('id',$payload) && $payload['id']>0) {
if (array_key_exists('cost',$payload) && $payload['cost']>0) {
echo $payload['cost'];
exit();
}
if (array_key_exists('completed',$payload) && $payload['completed']!='') {
http_response_code(404);
echo "Quote service failed for " . $_GET['id'];
exit();
}
} else {
http_response_code(404);
echo "Quote check failed for " . $_GET['id'];
exit();
}
// >15 minutes? Give up :,(
if ((time()-$startTime)>900) {
http_response_code(404);
echo "Quote takes too long for " . $_GET['id'];
exit();
}
}
} else {
http_response_code(404);
echo "Quote start failed for " . $_GET['id'];
if (is_array($payload)) {
echo "
".json_encode($payload);
} else {
echo "
".$body;
}
}
} else {
http_response_code(404);
echo "Invalid ID " . $_GET['id'];
}
exit();
}
$offset = 100;
$limit = 100;
$q = "select a.id,a.duration,a.distance,a.location_start_id,a.location_end_id,";
$q.= "b.address as location_start,b.latitude as location_start_lat,b.longitude AS location_start_lng,";
$q.= "c.address as location_end,c.latitude as location_end_lat,c.longitude AS location_end_lng";
$q.= " from parsedemail_item a, address b, address c ";
$q.= " where a.dup_id is null and b.id=a.location_start_id and c.id=a.location_end_id";
$q.= " and b.country='SG' AND c.country='SG' offset ${offset} limit ${limit}";
//echo $q;
$r = pg_query($conn,$q);
/*
savvy=> \d address
Table "public.address"
Column | Type | Modifiers
----------------+------------------------+------------------------------------------------------
id | bigint | not null default nextval('address_id_seq'::regclass)
address | character varying(200) |
latitude | numeric |
longitude | numeric |
timezone | integer |
geocoding_date | date |
postal | character varying(40) |
country | character varying(2) | default 'SG'::character varying
geometry | geography(Point,4326) |
description | character varying(100) |
*/
?>
| N# | ID | Duration | Distance | Address From | Address To | Quote | Elapsed | $quotes = []; if ($r && pg_num_rows($r)) { $ps = $pe = NULL; $j=1; while ($f=pg_fetch_assoc($r)) { if ($ps==$f["location_start"] && $pe==$f["location_end"]) continue; if ($f["location_start"]=="" || $f["location_end"]=="") continue; $quotes[] = $f["id"]; ?>
|---|---|---|---|---|---|---|---|
| =$j++;?> | =$f["id"]?> | =sprintf("%0.02f",$f["duration"]/60)?> | =sprintf("%0.02f",$f["distance"]>999?($f["distance"]/1000):$f["distance"])?> | =$f["location_start"]?> | =$f["location_end"]?> | ">N/A |
">0 sec |