Skip to content

Commit

Permalink
Merge pull request #5 from jhollinger/1.1
Browse files Browse the repository at this point in the history
Bring client up to version 1.1
  • Loading branch information
tomnomnom committed Dec 27, 2011
2 parents e58e025 + 006aa1d commit 4d6e412
Showing 1 changed file with 74 additions and 36 deletions.
110 changes: 74 additions & 36 deletions etherpad-lite-client.php
Expand Up @@ -22,24 +22,44 @@ public function __construct($apiKey, $baseUrl = null){
}
}

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);
// not all PHP installs have access to curl
protected function get($function, array $arguments = array()){
return $this->call($function, $arguments, 'GET');
}

protected function post($function, array $arguments = array()){
return $this->call($function, $arguments, 'POST');
}

protected function call($function, array $arguments = array(), $method = 'GET'){
$arguments['apikey'] = $this->apiKey;
$arguments = http_build_query($arguments);
$url = $this->baseUrl."/".self::API_VERSION."/".$function;
if ($method !== 'POST'){
$url .= "?".$arguments;
}
// use curl of it's available
if (function_exists('curl_init')){
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_TIMEOUT, 20);
if ($method === 'POST'){
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $arguments);
}
$result = curl_exec($c);
curl_close($c);
// fallback to plain php
} else {
$result = file_get_contents($url);
$params = array('http' => array('method' => $method, 'ignore_errors' => true, 'header' => 'Content-Type:application/x-www-form-urlencoded'));
if ($method === 'POST'){
$params['http']['content'] = $arguments;
}
$context = stream_context_create($params);
$fp = fopen($url, 'rb', false, $context);
$result = $fp ? stream_get_contents($fp) : null;
}

if($result == ""){
if(!$result){
throw new UnexpectedValueException("Empty or No Response from the server");
}

Expand Down Expand Up @@ -81,33 +101,33 @@ protected function handleResult($result){

// creates a new group
public function createGroup(){
return $this->call("createGroup");
return $this->post("createGroup");
}

// this functions helps you to map your application group ids to etherpad lite group ids
public function createGroupIfNotExistsFor($groupMapper){
return $this->call("createGroupIfNotExistsFor", array(
return $this->post("createGroupIfNotExistsFor", array(
"groupMapper" => $groupMapper
));
}

// deletes a group
public function deleteGroup($groupID){
return $this->call("deleteGroup", array(
return $this->post("deleteGroup", array(
"groupID" => $groupID
));
}

// returns all pads of this group
public function listPads($groupID){
return $this->call("listPads", array(
return $this->get("listPads", array(
"groupID" => $groupID
));
}

// creates a new pad in this group
public function createGroupPad($groupID, $padName, $text){
return $this->call("createGroupPad", array(
return $this->post("createGroupPad", array(
"groupID" => $groupID,
"padName" => $padName,
"text" => $text
Expand All @@ -119,14 +139,14 @@ public function createGroupPad($groupID, $padName, $text){

// creates a new author
public function createAuthor($name){
return $this->call("createAuthor", array(
return $this->post("createAuthor", array(
"name" => $name
));
}

// this functions helps you to map your application author ids to etherpad lite author ids
public function createAuthorIfNotExistsFor($authorMapper, $name){
return $this->call("createAuthorIfNotExistsFor", array(
return $this->post("createAuthorIfNotExistsFor", array(
"authorMapper" => $authorMapper,
"name" => $name
));
Expand All @@ -139,7 +159,7 @@ public function createAuthorIfNotExistsFor($authorMapper, $name){

// creates a new session
public function createSession($groupID, $authorID, $validUntil){
return $this->call("createSession", array(
return $this->post("createSession", array(
"groupID" => $groupID,
"authorID" => $authorID,
"validUntil" => $validUntil
Expand All @@ -148,28 +168,28 @@ public function createSession($groupID, $authorID, $validUntil){

// deletes a session
public function deleteSession($sessionID){
return $this->call("deleteSession", array(
return $this->post("deleteSession", array(
"sessionID" => $sessionID
));
}

// returns informations about a session
public function getSessionInfo($sessionID){
return $this->call("getSessionInfo", array(
return $this->get("getSessionInfo", array(
"sessionID" => $sessionID
));
}

// returns all sessions of a group
public function listSessionsOfGroup($groupID){
return $this->call("listSessionsOfGroup", array(
return $this->get("listSessionsOfGroup", array(
"groupID" => $groupID
));
}

// returns all sessions of an author
public function listSessionsOfAuthor($authorID){
return $this->call("listSessionsOfAuthor", array(
return $this->get("listSessionsOfAuthor", array(
"authorID" => $authorID
));
}
Expand All @@ -178,81 +198,99 @@ public function listSessionsOfAuthor($authorID){
// Pad content can be updated and retrieved through the API

// returns the text of a pad
// should take optional $rev
public function getText($padID){
return $this->call("getText", array(
"padID" => $padID
));
public function getText($padID, $rev=null){
$params = array("padID" => $padID);
if (isset($rev)){
$params["rev"] = $rev;
}
return $this->get("getText", $params);
}

// returns the text of a pad as html
public function getHTML($padID, $rev=null){
$params = array("padID" => $padID);
if (isset($rev)){
$params["rev"] = $rev;
}
return $this->get("getHTML", $params);
}

// sets the text of a pad
public function setText($padID, $text){
return $this->call("setText", array(
return $this->post("setText", array(
"padID" => $padID,
"text" => $text
));
}

// sets the html text of a pad
public function setHTML($padID, $html){
return $this->post("setHTML", array(
"padID" => $padID,
"html" => $html
));
}

// PAD
// Group pads are normal pads, but with the name schema
// GROUPID$PADNAME. A security manager controls access of them and its
// forbidden for normal pads to include a $ in the name.

// creates a new pad
public function createPad($padID, $text){
return $this->call("createPad", array(
return $this->post("createPad", array(
"padID" => $padID,
"text" => $text
));
), 'POST');
}

// returns the number of revisions of this pad
public function getRevisionsCount($padID){
return $this->call("getRevisionsCount", array(
return $this->get("getRevisionsCount", array(
"padID" => $padID
));
}

// deletes a pad
public function deletePad($padID){
return $this->call("deletePad", array(
return $this->post("deletePad", array(
"padID" => $padID
));
}

// returns the read only link of a pad
public function getReadOnlyID($padID){
return $this->call("getReadOnlyID", array(
return $this->get("getReadOnlyID", array(
"padID" => $padID
));
}

// sets a boolean for the public status of a pad
public function setPublicStatus($padID, $publicStatus){
return $this->call("setPublicStatus", array(
return $this->post("setPublicStatus", array(
"padID" => $padID,
"publicStatus" => $publicStatus
));
}

// return true of false
public function getPublicStatus($padID){
return $this->call("getPublicStatus", array(
return $this->get("getPublicStatus", array(
"padID" => $padID
));
}

// returns ok or a error message
public function setPassword($padID, $password){
return $this->call("setPassword", array(
return $this->post("setPassword", array(
"padID" => $padID,
"password" => $password
));
}

// returns true or false
public function isPasswordProtected($padID){
return $this->call("isPasswordProtected", array(
return $this->get("isPasswordProtected", array(
"padID" => $padID
));
}
Expand Down

0 comments on commit 4d6e412

Please sign in to comment.