function __store($gt, $gs, $id) { $gsf = _compact($gs); $connection = mysql_select_db(__data('database'), $con = mysql_connect(__data('server'), __data('user'), __data('pass'))); if ($id === 0) { mysql_query("INSERT INTO PARDATA(PARPIV, PARPAT) VALUES ('" . mysql_real_escape_string($gt) . "', '" . mysql_real_escape_string($gsf) . "');"); $ret = array(mysql_insert_id(), $gsf); } else { mysql_query("UPDATE PARDATA SET PARPIV = '" . mysql_real_escape_string($gt) . "', PARPAT = '" . mysql_real_escape_string($gsf) . "' WHERE PARID = " . mysql_real_escape_string($id) . ";"); $ret = array($id, $gsf); } mysql_close($con); return $ret; }
/** * Recursively compacts a value. This method will compact IRIs to CURIEs or * terms and do reverse type coercion to compact a value. * * @param ctx the context to use. * @param property the property that points to the value, NULL for none. * @param value the value to compact. * @param usedCtx a context to update if a value was used from "ctx". * * @return the compacted value. */ function _compact($ctx, $property, $value, $usedCtx) { $rval; if ($value === null) { // return null, but check coerce type to add to usedCtx $rval = null; _getCoerceType($ctx, $property, $usedCtx); } else { if (is_array($value)) { // recursively add compacted values to array $rval = array(); foreach ($value as $v) { $rval[] = _compact($ctx, $property, $v, $usedCtx); } } else { if (is_object($value) and property_exists($value, __S) and is_array($value->{__S})) { $rval = new stdClass(); $rval->{__S} = _compact($ctx, $property, $value->{__S}, $usedCtx); } else { if (is_object($value) and !property_exists($value, '@literal') and !property_exists($value, '@iri')) { // recursively handle sub-properties that aren't a sub-context $rval = new stdClass(); foreach ($value as $key => $v) { if ($v !== '@context') { // set object to compacted property, only overwrite existing // properties if the property actually compacted $p = _compactIri($ctx, $key, $usedCtx); if ($p !== $key or !property_exists($rval, $p)) { $rval->{$p} = _compact($ctx, $key, $v, $usedCtx); } } } } else { // get coerce type $coerce = _getCoerceType($ctx, $property, $usedCtx); // get type from value, to ensure coercion is valid $type = null; if (is_object($value)) { // type coercion can only occur if language is not specified if (!property_exists($value, '@language')) { // datatype must match coerce type if specified if (property_exists($value, '@datatype')) { $type = $value->{'@datatype'}; } else { if (property_exists($value, '@iri')) { $type = JSONLD_XSD_ANY_URI; } else { $type = $coerce; } } } } else { if (is_string($value)) { $type = $coerce; } } // types that can be auto-coerced from a JSON-builtin if ($coerce === null and ($type === JSONLD_XSD_BOOLEAN or $type === JSONLD_XSD_INTEGER or $type === JSONLD_XSD_DOUBLE)) { $coerce = $type; } // do reverse type-coercion if ($coerce !== null) { // type is only null if a language was specified, which is an error // if type coercion is specified if ($type === null) { throw new Exception('Cannot coerce type when a language is specified. ' . 'The language information would be lost.'); } else { if ($type !== $coerce) { throw new Exception('Cannot coerce type because the datatype does not match.'); } else { if (is_object($value)) { if (property_exists($value, '@iri')) { $rval = $value->{'@iri'}; } else { if (property_exists($value, '@literal')) { $rval = $value->{'@literal'}; } } } else { $rval = $value; } // do basic JSON types conversion if ($coerce === JSONLD_XSD_BOOLEAN) { $rval = ($rval === 'true' or $rval != 0); } else { if ($coerce === JSONLD_XSD_DOUBLE) { $rval = floatval($rval); } else { if ($coerce === JSONLD_XSD_INTEGER) { $rval = intval($rval); } } } } } } else { $rval = _clone($value); } // compact IRI if ($type === JSONLD_XSD_ANY_URI) { if (is_object($rval)) { $rval->{'@iri'} = _compactIri($ctx, $rval->{'@iri'}, $usedCtx); } else { $rval = _compactIri($ctx, $rval, $usedCtx); } } } } } } return $rval; }
function main() { $text = get('t'); $out = get('o'); if ($text === '') { echo "Bad input"; return false; } if (get('k') == 'e') { $memory = get('m'); $oText = $text; $save = "false"; $bin = "false"; if ($m === '') { $memory = 1024; } if (get('b') === '1') { $text = base64_decode($text); $bin = "true"; } $encoded = _go($text, false, $memory); if (get('v') === '1') { $id = __store($encoded[1], $encoded[2], 0); $save = "true"; } else { $id = array(-1, _compact($encoded[2])); } $output = base64_encode($encoded[0]); if ($out === 'JSON') { echo '{"PRAF":{"input":{"kind":"encode","memory":' . $memory . ',"text":"' . $oText . '","save":' . $save . ',"binary":' . $bin . ',"output":"JSON"},"output":{"sorting":"' . $id[1] . '","filling":"' . $encoded[1] . '","id":' . $id[0] . ',"outputText":"' . $output . '"}}}'; } elseif ($out === 'XML') { echo '<?xml version="1.0"?><PRAF><input><kind>encode</kind><memory>' . $memory . '</memory><text>' . $oText . '</text><save>' . $save . '</save><binary>' . $bin . '</binary><output>XML</output></input><output><sorting>' . $id[1] . '</sorting><filling>' . $encoded[1] . '</filling><id>' . $id[0] . '</id><outputText>' . $output . '</outputText></output></PRAF>'; } else { echo $output . "<br />" . $encoded[1] . "<br />" . $id[1] . "<br />" . $id[0]; } } elseif (get('k') == 'd') { $id = get('i'); $save = "false"; $bin = "false"; if ($id === '') { echo "Bad input"; return false; } if ($id === '0') { $temp = array(get('p'), get('s')); } $memory = get('m'); if ($memory === '') { $memory = 1024; } if ($id > 0) { $temp = __get($id); $save = "true"; } if ($temp[0] === '' || $temp[1] === '') { echo "Bad input"; return false; } $plain = _undo(base64_decode($text), $temp[0], $temp[1]); $encoded = _go($plain, false, $memory); if ($id > 0) { $id = __store($encoded[1], $encoded[2], $id); } else { $id = array(-1, _compact($encoded[2])); } if (get('b') === '1') { $plain = base64_encode($plain); $bin = "true"; } $output = base64_encode($encoded[0]); if ($out === 'JSON') { echo '{"PRAF":{"input":{"kind":"decode","memory":' . $memory . ',"id":' . $id[0] . ',"text":"' . $text . '","sorting":"' . $temp[1] . '","filling":"' . $temp[0] . '","save":' . $save . ',"binary":' . $bin . ',"output":"JSON"},"output":{"sorting":"' . $id[1] . '","filling":"' . $encoded[1] . '","id":' . $id[0] . ',"outputText":"' . $output . '"}}}'; } elseif ($out === 'XML') { echo '<?xml version="1.0"?><PRAF><input><kind>decode</kind><memory>' . $memory . '</memory><id>' . $id[0] . '</id><text>' . $text . '</text><sorting>' . $temp[1] . '</sorting><filling>' . $temp[0] . '</filling><save>' . $save . '</save><binary>' . $bin . '<binary><output>XML</output></input><output><sorting>' . $id[1] . '</sorting><filling>' . $encoded[1] . '</filling><id>' . $id[0] . '</id><outputText>' . $output . '</outputText></output></PRAF>'; } else { echo $plain . "<br />" . $output . "<br />" . $encoded[1] . "<br />" . $id[1] . "<br />" . $id[0]; } } else { echo "Bad input"; return false; } }