<?php
class
EtherpadLiteClient {
const
API_VERSION = 1;
const
CODE_OK = 0;
const
CODE_INVALID_PARAMETERS = 1;
const
CODE_INTERNAL_ERROR = 2;
const
CODE_INVALID_FUNCTION = 3;
const
CODE_INVALID_API_KEY = 4;
protected
$apiKey
=
""
;
protected
$baseUrl
=
"http://localhost:9001/api"
;
public
function
__construct(
$apiKey
,
$baseUrl
= null){
$this
->apiKey =
$apiKey
;
if
(isset(
$baseUrl
)){
$this
->baseUrl =
$baseUrl
;
}
if
(!filter_var(
$this
->baseUrl, FILTER_VALIDATE_URL)){
throw
new
InvalidArgumentException(
"[{$this->baseUrl}] is not a valid URL"
);
}
}
protected
function
call(
$function
,
array
$arguments
=
array
()){
$query
=
array_merge
(
array
(
'apikey'
=>
$this
->apiKey),
$arguments
);
$url
=
$this
->baseUrl.
"/"
.self::API_VERSION.
"/"
.
$function
.
"?"
.http_build_query(
$query
);
if
(function_exists(
'curl_init'
)){
$c
= curl_init(
$url
);
curl_setopt(
$c
, CURLOPT_RETURNTRANSFER, true);
curl_setopt(
$c
, CURLOPT_TIMEOUT, 20);
$result
= curl_exec(
$c
);
curl_close(
$c
);
}
else
{
$result
=
file_get_contents
(
$url
);
}
if
(
$result
==
""
){
throw
new
UnexpectedValueException(
"Empty or No Response from the server"
);
}
$result
= json_decode(
$result
);
if
(
$result
=== null){
throw
new
UnexpectedValueException(
"JSON response could not be decoded"
);
}
return
$this
->handleResult(
$result
);
}
protected
function
handleResult(
$result
){
if
(!isset(
$result
->code)){
throw
new
RuntimeException(
"API response has no code"
);
}
if
(!isset(
$result
->message)){
throw
new
RuntimeException(
"API response has no message"
);
}
if
(!isset(
$result
->data)){
$result
->data = null;
}
switch
(
$result
->code){
case
self::CODE_OK:
return
$result
->data;
case
self::CODE_INVALID_PARAMETERS:
case
self::CODE_INVALID_API_KEY:
throw
new
InvalidArgumentException(
$result
->message);
case
self::CODE_INTERNAL_ERROR:
throw
new
RuntimeException(
$result
->message);
case
self::CODE_INVALID_FUNCTION:
throw
new
BadFunctionCallException(
$result
->message);
default
:
throw
new
RuntimeException(
"An unexpected error occurred whilst handling the response"
);
}
}
public
function
createGroup(){
return
$this
->call(
"createGroup"
);
}
public
function
createGroupIfNotExistsFor(
$groupMapper
){
return
$this
->call(
"createGroupIfNotExistsFor"
,
array
(
"groupMapper"
=>
$groupMapper
));
}
public
function
deleteGroup(
$groupID
){
return
$this
->call(
"deleteGroup"
,
array
(
"groupID"
=>
$groupID
));
}
public
function
listPads(
$groupID
){
return
$this
->call(
"listPads"
,
array
(
"groupID"
=>
$groupID
));
}
public
function
createGroupPad(
$groupID
,
$padName
,
$text
){
return
$this
->call(
"createGroupPad"
,
array
(
"groupID"
=>
$groupID
,
"padName"
=>
$padName
,
"text"
=>
$text
));
}
public
function
createAuthor(
$name
){
return
$this
->call(
"createAuthor"
,
array
(
"name"
=>
$name
));
}
public
function
createAuthorIfNotExistsFor(
$authorMapper
,
$name
){
return
$this
->call(
"createAuthorIfNotExistsFor"
,
array
(
"authorMapper"
=>
$authorMapper
,
"name"
=>
$name
));
}
public
function
createSession(
$groupID
,
$authorID
,
$validUntil
){
return
$this
->call(
"createSession"
,
array
(
"groupID"
=>
$groupID
,
"authorID"
=>
$authorID
,
"validUntil"
=>
$validUntil
));
}
public
function
deleteSession(
$sessionID
){
return
$this
->call(
"deleteSession"
,
array
(
"sessionID"
=>
$sessionID
));
}
public
function
getSessionInfo(
$sessionID
){
return
$this
->call(
"getSessionInfo"
,
array
(
"sessionID"
=>
$sessionID
));
}
public
function
listSessionsOfGroup(
$groupID
){
return
$this
->call(
"listSessionsOfGroup"
,
array
(
"groupID"
=>
$groupID
));
}
public
function
listSessionsOfAuthor(
$authorID
){
return
$this
->call(
"listSessionsOfAuthor"
,
array
(
"authorID"
=>
$authorID
));
}
public
function
getText
(
$padID
){
return
$this
->call(
"getText"
,
array
(
"padID"
=>
$padID
));
}
public
function
setText(
$padID
,
$text
){
return
$this
->call(
"setText"
,
array
(
"padID"
=>
$padID
,
"text"
=>
$text
));
}
public
function
createPad(
$padID
,
$text
){
return
$this
->call(
"createPad"
,
array
(
"padID"
=>
$padID
,
"text"
=>
$text
));
}
public
function
getRevisionsCount(
$padID
){
return
$this
->call(
"getRevisionsCount"
,
array
(
"padID"
=>
$padID
));
}
public
function
deletePad(
$padID
){
return
$this
->call(
"deletePad"
,
array
(
"padID"
=>
$padID
));
}
public
function
getReadOnlyID(
$padID
){
return
$this
->call(
"getReadOnlyID"
,
array
(
"padID"
=>
$padID
));
}
public
function
setPublicStatus(
$padID
,
$publicStatus
){
return
$this
->call(
"setPublicStatus"
,
array
(
"padID"
=>
$padID
,
"publicStatus"
=>
$publicStatus
));
}
public
function
getPublicStatus(
$padID
){
return
$this
->call(
"getPublicStatus"
,
array
(
"padID"
=>
$padID
));
}
public
function
setPassword(
$padID
,
$password
){
return
$this
->call(
"setPassword"
,
array
(
"padID"
=>
$padID
,
"password"
=>
$password
));
}
public
function
isPasswordProtected(
$padID
){
return
$this
->call(
"isPasswordProtected"
,
array
(
"padID"
=>
$padID
));
}
}