mirror of
https://github.com/PrivateBin/PrivateBin.git
synced 2024-10-01 01:26:10 -04:00
working on JSON-LD validity, added CORS headers preparing external API
call support
This commit is contained in:
parent
22d0b1ec22
commit
14d08ec56d
@ -1,17 +1,16 @@
|
|||||||
{
|
{
|
||||||
"@context": {
|
"@context": {
|
||||||
"status": "http://schema.org/Integer",
|
"so": "http://schema.org/",
|
||||||
"id": "http://schema.org/name",
|
"status": "so:Integer",
|
||||||
"parentid": "http://schema.org/name",
|
"id": "so:name",
|
||||||
|
"parentid": "so:name",
|
||||||
"url: {
|
"url: {
|
||||||
"@id": "http://schema.org/url",
|
"@id": "so:url",
|
||||||
"@type": "@id"
|
"@type": "@id"
|
||||||
},
|
},
|
||||||
"data": "http://schema.org/Text",
|
"data": "so:Text",
|
||||||
"meta": {
|
"meta": {
|
||||||
"postdate": "http://schema.org/Integer",
|
"@id": "?jsonld=commentmeta"
|
||||||
"nickname": "http://schema.org/Text",
|
|
||||||
"vizhash": "http://schema.org/Text"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
8
js/commentmeta.jsonld
Normal file
8
js/commentmeta.jsonld
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"@context": {
|
||||||
|
"so": "http://schema.org/",
|
||||||
|
"postdate": "so:Integer",
|
||||||
|
"nickname": "so:Text",
|
||||||
|
"vizhash": "so:Text"
|
||||||
|
}
|
||||||
|
}
|
@ -1,28 +1,24 @@
|
|||||||
{
|
{
|
||||||
"@context": {
|
"@context": {
|
||||||
"status": http://schema.org/Integer",
|
"so": "http://schema.org/",
|
||||||
"id": "http://schema.org/name",
|
"status": {"@id": "so:Integer"},
|
||||||
"deletetoken": "http://schema.org/Text",
|
"id": {"@id": "so:name"},
|
||||||
"url: {
|
"deletetoken": {"@id": "so:Text"},
|
||||||
"@id": "http://schema.org/url",
|
"url": {
|
||||||
"@type": "@id"
|
"@type": "@id",
|
||||||
|
"@id": "so:url"
|
||||||
},
|
},
|
||||||
"data": "http://schema.org/Text",
|
"data": {"@id": "so:Text"},
|
||||||
"attachment": "http://schema.org/Text",
|
"attachment": {"@id": "so:Text"},
|
||||||
"attachmentname": "http://schema.org/Text",
|
"attachmentname": {"@id": "so:Text"},
|
||||||
"meta": {
|
"meta": {
|
||||||
"formatter": "http://schema.org/Text",
|
"@id": "?jsonld=pastemeta"
|
||||||
"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"
|
|
||||||
},
|
},
|
||||||
"comments": {
|
"comments": {
|
||||||
"@id": "comment.jsonld",
|
"@id": "?jsonld=comment",
|
||||||
"@container": "@list"
|
"@container": "@list"
|
||||||
},
|
},
|
||||||
"comment_count": "http://schema.org/Integer",
|
"comment_count": {"@id": "so:Integer"},
|
||||||
"comment_offset": "http://schema.org/Integer"
|
"comment_offset": {"@id": "so:Integer"}
|
||||||
}
|
}
|
||||||
}
|
}
|
11
js/pastemeta.jsonld
Normal file
11
js/pastemeta.jsonld
Normal file
@ -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"}
|
||||||
|
}
|
||||||
|
}
|
@ -38,8 +38,8 @@ class model_comment extends model_abstract
|
|||||||
$comments = $this->_store->readComments($this->getPaste()->getId());
|
$comments = $this->_store->readComments($this->getPaste()->getId());
|
||||||
foreach ($comments as $comment) {
|
foreach ($comments as $comment) {
|
||||||
if (
|
if (
|
||||||
$comment->meta->parentid == $this->getParentId() &&
|
$comment->parentid == $this->getParentId() &&
|
||||||
$comment->meta->commentid == $this->getId()
|
$comment->id == $this->getId()
|
||||||
) {
|
) {
|
||||||
$this->_data = $comment;
|
$this->_data = $comment;
|
||||||
break;
|
break;
|
||||||
|
@ -91,6 +91,7 @@ class request
|
|||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
!array_key_exists('pasteid', $this->_params) &&
|
!array_key_exists('pasteid', $this->_params) &&
|
||||||
|
!array_key_exists('jsonld', $this->_params) &&
|
||||||
array_key_exists('QUERY_STRING', $_SERVER) &&
|
array_key_exists('QUERY_STRING', $_SERVER) &&
|
||||||
!empty($_SERVER['QUERY_STRING'])
|
!empty($_SERVER['QUERY_STRING'])
|
||||||
)
|
)
|
||||||
@ -117,6 +118,10 @@ class request
|
|||||||
$this->_operation = 'read';
|
$this->_operation = 'read';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
elseif (array_key_exists('jsonld', $this->_params) && !empty($this->_params['jsonld']))
|
||||||
|
{
|
||||||
|
$this->_operation = 'jsonld';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -95,6 +95,14 @@ class zerobin
|
|||||||
*/
|
*/
|
||||||
private $_request;
|
private $_request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* URL base
|
||||||
|
*
|
||||||
|
* @access private
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $_urlbase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* constructor
|
* constructor
|
||||||
*
|
*
|
||||||
@ -127,12 +135,18 @@ class zerobin
|
|||||||
case 'read':
|
case 'read':
|
||||||
$this->_read($this->_request->getParam('pasteid'));
|
$this->_read($this->_request->getParam('pasteid'));
|
||||||
break;
|
break;
|
||||||
|
case 'jsonld':
|
||||||
|
$this->_jsonld($this->_request->getParam('jsonld'));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// output JSON or HTML
|
// output JSON or HTML
|
||||||
if ($this->_request->isJsonApiCall())
|
if ($this->_request->isJsonApiCall())
|
||||||
{
|
{
|
||||||
header('Content-type: application/json');
|
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;
|
echo $this->_json;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -162,6 +176,7 @@ class zerobin
|
|||||||
$this->_conf = new configuration;
|
$this->_conf = new configuration;
|
||||||
$this->_model = new model($this->_conf);
|
$this->_model = new model($this->_conf);
|
||||||
$this->_request = new request;
|
$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'));
|
$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
|
* return JSON encoded message and exit
|
||||||
*
|
*
|
||||||
@ -441,9 +482,7 @@ class zerobin
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
$result['id'] = $message;
|
$result['id'] = $message;
|
||||||
$result['url'] = (
|
$result['url'] = $this->_urlbase . '?' . $message;
|
||||||
array_key_exists('REQUEST_URI', $_SERVER) ? $_SERVER['REQUEST_URI'] : '/'
|
|
||||||
) . '?' . $message;
|
|
||||||
}
|
}
|
||||||
$result += $other;
|
$result += $other;
|
||||||
$this->_json = json_encode($result);
|
$this->_json = json_encode($result);
|
||||||
|
@ -163,4 +163,34 @@ class jsonApiTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(0, $response['comment_offset'], 'outputs comment_offset correctly');
|
$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');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -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->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');
|
$this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists after storing it');
|
||||||
$comment = json_decode(json_encode(helper::getComment()));
|
$comment = json_decode(json_encode(helper::getComment()));
|
||||||
$comment->meta->commentid = helper::getCommentId();
|
$comment->id = helper::getCommentId();
|
||||||
$comment->meta->parentid = helper::getPasteId();
|
$comment->parentid = helper::getPasteId();
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array($comment->meta->postdate => $comment),
|
array($comment->meta->postdate => $comment),
|
||||||
$this->_model->readComments(helper::getPasteId())
|
$this->_model->readComments(helper::getPasteId())
|
||||||
|
@ -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->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');
|
$this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists after storing it');
|
||||||
$comment = json_decode(json_encode(helper::getComment()));
|
$comment = json_decode(json_encode(helper::getComment()));
|
||||||
$comment->meta->commentid = helper::getCommentId();
|
$comment->id = helper::getCommentId();
|
||||||
$comment->meta->parentid = helper::getPasteId();
|
$comment->parentid = helper::getPasteId();
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array($comment->meta->postdate => $comment),
|
array($comment->meta->postdate => $comment),
|
||||||
$this->_model->readComments(helper::getPasteId())
|
$this->_model->readComments(helper::getPasteId())
|
||||||
|
Loading…
Reference in New Issue
Block a user