From 14d08ec56dc278a959fd09f48812f785430390e4 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 18 Oct 2015 14:37:58 +0200 Subject: [PATCH] working on JSON-LD validity, added CORS headers preparing external API call support --- js/comment.jsonld | 15 +++++++-------- js/commentmeta.jsonld | 8 ++++++++ js/paste.jsonld | 32 ++++++++++++++---------------- js/pastemeta.jsonld | 11 +++++++++++ lib/model/comment.php | 4 ++-- lib/request.php | 5 +++++ lib/zerobin.php | 45 ++++++++++++++++++++++++++++++++++++++++--- tst/jsonApi.php | 30 +++++++++++++++++++++++++++++ tst/zerobin/data.php | 4 ++-- tst/zerobin/db.php | 4 ++-- 10 files changed, 123 insertions(+), 35 deletions(-) create mode 100644 js/commentmeta.jsonld create mode 100644 js/pastemeta.jsonld diff --git a/js/comment.jsonld b/js/comment.jsonld index bdee3c2d..d2c6d5b7 100644 --- a/js/comment.jsonld +++ b/js/comment.jsonld @@ -1,17 +1,16 @@ { "@context": { - "status": "http://schema.org/Integer", - "id": "http://schema.org/name", - "parentid": "http://schema.org/name", + "so": "http://schema.org/", + "status": "so:Integer", + "id": "so:name", + "parentid": "so:name", "url: { - "@id": "http://schema.org/url", + "@id": "so:url", "@type": "@id" }, - "data": "http://schema.org/Text", + "data": "so:Text", "meta": { - "postdate": "http://schema.org/Integer", - "nickname": "http://schema.org/Text", - "vizhash": "http://schema.org/Text" + "@id": "?jsonld=commentmeta" } } } \ No newline at end of file diff --git a/js/commentmeta.jsonld b/js/commentmeta.jsonld new file mode 100644 index 00000000..1a502b16 --- /dev/null +++ b/js/commentmeta.jsonld @@ -0,0 +1,8 @@ +{ + "@context": { + "so": "http://schema.org/", + "postdate": "so:Integer", + "nickname": "so:Text", + "vizhash": "so:Text" + } +} \ No newline at end of file diff --git a/js/paste.jsonld b/js/paste.jsonld index c3e02947..fd9dba50 100644 --- a/js/paste.jsonld +++ b/js/paste.jsonld @@ -1,28 +1,24 @@ { "@context": { - "status": http://schema.org/Integer", - "id": "http://schema.org/name", - "deletetoken": "http://schema.org/Text", - "url: { - "@id": "http://schema.org/url", - "@type": "@id" + "so": "http://schema.org/", + "status": {"@id": "so:Integer"}, + "id": {"@id": "so:name"}, + "deletetoken": {"@id": "so:Text"}, + "url": { + "@type": "@id", + "@id": "so:url" }, - "data": "http://schema.org/Text", - "attachment": "http://schema.org/Text", - "attachmentname": "http://schema.org/Text", + "data": {"@id": "so:Text"}, + "attachment": {"@id": "so:Text"}, + "attachmentname": {"@id": "so:Text"}, "meta": { - "formatter": "http://schema.org/Text", - "postdate": "http://schema.org/Integer", - "opendiscussion": "http://schema.org/True", - "burnafterreading": "http://schema.org/True", - "expire_date": "http://schema.org/Integer", - "remaining_time": "http://schema.org/Integer" + "@id": "?jsonld=pastemeta" }, "comments": { - "@id": "comment.jsonld", + "@id": "?jsonld=comment", "@container": "@list" }, - "comment_count": "http://schema.org/Integer", - "comment_offset": "http://schema.org/Integer" + "comment_count": {"@id": "so:Integer"}, + "comment_offset": {"@id": "so:Integer"} } } \ No newline at end of file diff --git a/js/pastemeta.jsonld b/js/pastemeta.jsonld new file mode 100644 index 00000000..0fefab37 --- /dev/null +++ b/js/pastemeta.jsonld @@ -0,0 +1,11 @@ +{ + "@context": { + "so": "http://schema.org/", + "formatter": {"@id": "so:Text"}, + "postdate": {"@id": "so:Integer"}, + "opendiscussion": {"@id": "so:True"}, + "burnafterreading": {"@id": "so:True"}, + "expire_date": {"@id": "so:Integer"}, + "remaining_time": {"@id": "so:Integer"} + } +} \ No newline at end of file diff --git a/lib/model/comment.php b/lib/model/comment.php index ba77884c..53d5e5fb 100644 --- a/lib/model/comment.php +++ b/lib/model/comment.php @@ -38,8 +38,8 @@ class model_comment extends model_abstract $comments = $this->_store->readComments($this->getPaste()->getId()); foreach ($comments as $comment) { if ( - $comment->meta->parentid == $this->getParentId() && - $comment->meta->commentid == $this->getId() + $comment->parentid == $this->getParentId() && + $comment->id == $this->getId() ) { $this->_data = $comment; break; diff --git a/lib/request.php b/lib/request.php index 0fcf904d..b5dfb86e 100644 --- a/lib/request.php +++ b/lib/request.php @@ -91,6 +91,7 @@ class request } if ( !array_key_exists('pasteid', $this->_params) && + !array_key_exists('jsonld', $this->_params) && array_key_exists('QUERY_STRING', $_SERVER) && !empty($_SERVER['QUERY_STRING']) ) @@ -117,6 +118,10 @@ class request $this->_operation = 'read'; } } + elseif (array_key_exists('jsonld', $this->_params) && !empty($this->_params['jsonld'])) + { + $this->_operation = 'jsonld'; + } } /** diff --git a/lib/zerobin.php b/lib/zerobin.php index d1d10755..edda4b64 100644 --- a/lib/zerobin.php +++ b/lib/zerobin.php @@ -95,6 +95,14 @@ class zerobin */ private $_request; + /** + * URL base + * + * @access private + * @var string + */ + private $_urlbase; + /** * constructor * @@ -127,12 +135,18 @@ class zerobin case 'read': $this->_read($this->_request->getParam('pasteid')); break; + case 'jsonld': + $this->_jsonld($this->_request->getParam('jsonld')); + return; } // output JSON or HTML if ($this->_request->isJsonApiCall()) { header('Content-type: application/json'); + header('Access-Control-Allow-Origin: *'); + header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE'); + header('Access-Control-Allow-Headers: X-Requested-With, Content-Type'); echo $this->_json; } else @@ -162,6 +176,7 @@ class zerobin $this->_conf = new configuration; $this->_model = new model($this->_conf); $this->_request = new request; + $this->_urlbase = array_key_exists('REQUEST_URI', $_SERVER) ? $_SERVER['REQUEST_URI'] : '/'; } /** @@ -422,6 +437,32 @@ class zerobin $page->draw($this->_conf->getKey('template')); } + private function _jsonld($type) + { + if ( + $type !== 'paste' && $type !== 'comment' && + $type !== 'pastemeta' && $type !== 'commentmeta' + ) + { + $type = ''; + } + $content = '{}'; + $file = PUBLIC_PATH . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . $type . '.jsonld'; + if (is_readable($file)) + { + $content = str_replace( + '?jsonld=', + $this->_urlbase . '?jsonld=', + file_get_contents($file) + ); + } + + header('Content-type: application/ld+json'); + header('Access-Control-Allow-Origin: *'); + header('Access-Control-Allow-Methods: GET'); + echo $content; + } + /** * return JSON encoded message and exit * @@ -441,9 +482,7 @@ class zerobin else { $result['id'] = $message; - $result['url'] = ( - array_key_exists('REQUEST_URI', $_SERVER) ? $_SERVER['REQUEST_URI'] : '/' - ) . '?' . $message; + $result['url'] = $this->_urlbase . '?' . $message; } $result += $other; $this->_json = json_encode($result); diff --git a/tst/jsonApi.php b/tst/jsonApi.php index 65094481..63fc177c 100644 --- a/tst/jsonApi.php +++ b/tst/jsonApi.php @@ -163,4 +163,34 @@ class jsonApiTest extends PHPUnit_Framework_TestCase $this->assertEquals(0, $response['comment_offset'], 'outputs comment_offset correctly'); } + /** + * @runInSeparateProcess + */ + public function testJsonLdPaste() + { + $this->reset(); + $paste = helper::getPasteWithAttachment(); + $this->_model->create(helper::getPasteId(), $paste); + $_GET['jsonld'] = 'paste'; + ob_start(); + new zerobin; + $content = ob_get_contents(); + $this->assertEquals(file_get_contents(PUBLIC_PATH . '/js/paste.jsonld'), $content, 'outputs data correctly'); + } + + /** + * @runInSeparateProcess + */ + public function testJsonLdInvalid() + { + $this->reset(); + $paste = helper::getPasteWithAttachment(); + $this->_model->create(helper::getPasteId(), $paste); + $_GET['jsonld'] = '../cfg/conf.ini'; + ob_start(); + new zerobin; + $content = ob_get_contents(); + $this->assertEquals('{}', $content, 'does not output nasty data'); + } + } \ No newline at end of file diff --git a/tst/zerobin/data.php b/tst/zerobin/data.php index e22f4ed7..87c6afb7 100644 --- a/tst/zerobin/data.php +++ b/tst/zerobin/data.php @@ -35,8 +35,8 @@ class zerobin_dataTest extends PHPUnit_Framework_TestCase $this->assertTrue($this->_model->createComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId(), helper::getComment()) !== false, 'store comment'); $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists after storing it'); $comment = json_decode(json_encode(helper::getComment())); - $comment->meta->commentid = helper::getCommentId(); - $comment->meta->parentid = helper::getPasteId(); + $comment->id = helper::getCommentId(); + $comment->parentid = helper::getPasteId(); $this->assertEquals( array($comment->meta->postdate => $comment), $this->_model->readComments(helper::getPasteId()) diff --git a/tst/zerobin/db.php b/tst/zerobin/db.php index 3697b503..a3f4e0a1 100644 --- a/tst/zerobin/db.php +++ b/tst/zerobin/db.php @@ -33,8 +33,8 @@ class zerobin_dbTest extends PHPUnit_Framework_TestCase $this->assertTrue($this->_model->createComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId(), helper::getComment()) !== false, 'store comment'); $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists after storing it'); $comment = json_decode(json_encode(helper::getComment())); - $comment->meta->commentid = helper::getCommentId(); - $comment->meta->parentid = helper::getPasteId(); + $comment->id = helper::getCommentId(); + $comment->parentid = helper::getPasteId(); $this->assertEquals( array($comment->meta->postdate => $comment), $this->_model->readComments(helper::getPasteId())