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": {
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"@context": {
|
||||
"so": "http://schema.org/",
|
||||
"postdate": "so:Integer",
|
||||
"nickname": "so:Text",
|
||||
"vizhash": "so:Text"
|
||||
}
|
||||
}
|
|
@ -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"}
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
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;
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
|
||||
}
|
|
@ -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())
|
||||
|
|
|
@ -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())
|
||||
|
|
Loading…
Reference in New Issue