Skip to content

Instantly share code, notes, and snippets.

@csarven
Forked from rhiaro/inbox.php
Created August 13, 2017 15:03
Show Gist options
  • Save csarven/02fc1bd1b7fcfcd3b8671c4d288447f5 to your computer and use it in GitHub Desktop.
Save csarven/02fc1bd1b7fcfcd3b8671c4d288447f5 to your computer and use it in GitHub Desktop.
Basic LDN receiver. Help yourself. See: https://rhiaro.co.uk/2017/08/diy-ldn
<?
/*
Copyright 2017 Amy Guy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------------------------------
Quick and dirty Linked Data Notifications receiver
------------------------------------------------------------------------------------
This PHP script is a minimum viable LDN receiver, or Inbox. You can place this
file on a server, and advertise its presence on any Web resource with an HTTP header:
Link: <https://example.org/inbox.php>; rel="http://www.w3.org/ns/ldp#inbox"
or RDF link, eg. JSON-LD:
{
"@context": "http://www.w3.org/ns/ldp",
"@id": "https://example.org/profile",
"inbox": "https://example.org/inbox.php"
}
eg. RDFa:
<link href="https://example.org/inbox.php" rel="http://www.w3.org/ns/ldp#inbox" />
Notifications sent to this Inbox are stored without modification to a flat file. An
alternative would be to parse the contents as RDF and/or save them in a database. The
notifications are exposed for other client applications (LDN consumers) to discover
and reuse.
This script assumes it lives in the same directory as where your notification
files are stored.
Find out more about Linked Data Notifications:
- Walkthrough of this code: https://rhiaro.co.uk/2017/08/diy-ldn
- W3C Recommendation: https://www.w3.org/TR/ldn
*/
$base = "https://".$_SERVER['HTTP_HOST']; // Your domain
$inboxpath = "inbox"; // The directory where your notification files are stored.
header("Accept-Post: application/ld+json");
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
/* Accepting notifications */
$input = file_get_contents('php://input');
$headers = apache_request_headers();
$data = json_decode($input, true);
if(strpos($headers["Content-Type"], "application/ld+json") === false){
header("HTTP/1.1 415 Unsupported Media Type");
}elseif(!$data){
header("HTTP/1.1 400 Bad Request");
echo "Invalid payload.";
}else{
// Write notification contents to a file
$filename = $inboxpath."/".date("ymd-His")."_".uniqid().".json";
$data["@id"] = $base."/".$filename;
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$h = fopen("../".$filename, 'w');
fwrite($h, $json);
fclose($h);
header("HTTP/1.1 201 Created");
header("Location: ".$base."/".$filename);
}
}else{
/* Serving notifications */
$files = scandir("../".$inboxpath);
$notifications = array();
foreach($files as $file){
if(!is_dir($file) && substr($file, -5) == ".json"){
$notifications[] = array("@id" => $base."/".$inboxpath."/".$file);
}
}
$inbox = array( "@context" => "http://www.w3.org/ns/ldp#"
,"@id" => ""
,"@type" => "ldp:Container"
,"ldp:contains" => $notifications
);
$inboxjson = json_encode($inbox, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
header("Content-Type: application/ld+json");
echo $inboxjson;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment