Skip to content

Commit

Permalink
Merge pull request #4 from johnyma22/master
Browse files Browse the repository at this point in the history
updates.
  • Loading branch information
tomnomnom committed Aug 21, 2011
2 parents 5295b81 + faeb3c8 commit 10471ec
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 18 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ This PHP Etherpad Lite class allows you to easily interact with Etherpad Lite AP

#2 Examples

Examples are located in examples.php
See it live here http://beta.etherpad.org/example.php

Examples are located in examples.php and example_big.php. examples.php contains an example for each API call. example_big.php contains a UI for managing pads.

This example is the most commonly used, the example displays a pads text on the screen:

Expand Down
9 changes: 6 additions & 3 deletions etherpad-lite-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,21 @@ protected function call($function, array $arguments = array()){
$arguments
);
$url = $this->baseUrl."/".self::API_VERSION."/".$function."?".http_build_query($query);

// not all PHP installs have access to curl
if (function_exists('curl_init')){
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_TIMEOUT, 2);
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");
Expand Down Expand Up @@ -159,7 +162,7 @@ public function getSessionInfo($sessionID){

// returns all sessions of a group
public function listSessionsOfGroup($groupID){
return $this->call("listSessionOfGroup", array(
return $this->call("listSessionsOfGroup", array(
"groupID" => $groupID
));
}
Expand Down
158 changes: 158 additions & 0 deletions example_big.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<style type="text/css">
h1{margin:0;display:inline;}
h2{display:inline;font-size:81.3%}
h3{display:inline;margin-right:20px;}
.pad{background-color:#F7F7F7;padding:20px;border-style:solid;border-width:1px;margin-bottom:10px;}
.contents{display:none;word-wrap: break-word;}
#pads{width:700px;}
</style>
</head>
<body>
<pre>
<?php
//error_reporting(E_ALL);
//ini_set('display_errors', '1');

// Include the Class
include 'etherpad-lite-client.php';
$host = "http://beta.etherpad.org";

// By this point the user has authenticated

// Create an instance
$instance = new EtherpadLiteClient('EtherpadFTW','http://beta.etherpad.org/api'); // Example URL: http://your.hostname.tld:8080/api -- All API calls return a JSON value as documented in the API here: https://github.com/Pita/etherpad-lite/wiki/HTTP-API

// Get the Params from the URL
$action = $_GET['action'];

// Step 1, get GroupID of the userID where userID is OUR userID and NOT the userID used by Etherpad
try {
$mappedGroup = $instance->createGroupIfNotExistsFor("John@McLear.co.uk");
$groupID = $mappedGroup->groupID;
} catch (Exception $e) {}

// Create a session
/* get Mapped Author ID based on a value from your web application such as the userID */
try {
$author = $instance->createAuthorIfNotExistsFor('John McLear', 'Cake');
$authorID = $author->authorID;
} catch (Exception $e) {
echo "\n\ncreateAuthorIfNotExistsFor Failed with message ". $e->getMessage();
}
$validUntil = mktime(0, 0, 0, date("m"), date("d")+1, date("y")); // One day in the future
$sessionID = $instance->createSession($groupID, $authorID, $validUntil);
$sessionID = $sessionID->sessionID;
setcookie("sessionID",$sessionID); // Set a cookie

// echo "New Session ID is $sessionID->sessionID\n\n";

// Run some logic based on the initial request
if ($action){ // if an action is set then lets do it.

if ($action == "newPad") // If the request is to create a new pad
{
$name = $_GET["name"];
if (!$name){
function genRandomString() { // A funtion to generate a random name if something doesn't already exist
$length = 10;
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$string = '';
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
return $string;
}
$name = genRandomString();
}
$contents = $_GET["contents"];
try {
$newPad = $instance->createGroupPad($groupID,$name,$contents);
$padID = $newPad->padID;
$newlocation = "$host/p/$padID"; // redirect to the new padID location
header( "Location: $newlocation" ) ;
} catch (Exception $e) {
echo "\n\ncreateGroupPad Failed with message ". $e->getMessage();
}
}
if ($action == "deletePad") // If teh request is to delete an existing pad
{
$name = $_GET["name"];
try {
$name = urldecode($name);
$instance->deletePad($name);
} catch (Exception $e) {
// the pad doesn't exist?
echo "\n\ndeletePad Failed with message ". $e->getMessage();
}
}
if ($action == "makePublic") // If teh request is to delete an existing pad
{
$name = $_GET["name"];
try {
$instance->setPublicStatus($name,"true");
} catch (Exception $e) {
// the pad doesn't exist?
echo "\n\nMake Public Failed with message ". $e->getMessage();
}
}
if ($action == "makePrivate") // If teh request is to delete an existing pad
{
$name = $_GET["name"];
try {
$instance->setPublicStatus($name,"false");
} catch (Exception $e) {
// the pad doesn't exist?
echo "\n\nMake Private Failed with message ". $e->getMessage();
}
}

}

// Step 2, list Pads from this Group.
/* Example: List Pads from a group */
try {
$padList = $instance->listPads($groupID);
$padList = $padList->padIDs;
} catch (Exception $e) {
echo "\n\nlistPads Failed: ". $e->getMessage();
}


// Begin writing to the UI
// echo "<h1>Pads</h1><div id='pads'>";
echo "Create new Pad <form action='#' style='display:inline;'><input type='hidden' name='action' value='newPad'><input type='text' name='name'><input type='submit'></form>";

$count = 0;

foreach($padList as $pad => $key){ // For each pad in the object
// This should really be ordered based on last modified
$padname = explode("$",$pad);
$padname = $padname[1];
$padContents = $instance->getText($pad); // Get the pad contents
$contents = $padContents->text;
$pad = urlencode($pad);
echo "<div class='pad'>";
echo "<h1><a href=$host/p/$pad>$padname</a></h1>";
echo " - <h2><a onClick='$(\"#contents$count\").slideDown();'>Preview</a></h2><br/>";
echo "<div class='contents' id=contents$count>$contents</div>";
echo "<h3><a href=/users.php?action=deletePad&name=$pad>Delete Pad</a></h3>";
echo "<h3><a href=$host/p/$pad>Edit Pad</a></h3>";
$readOnlyID = $instance->getReadOnlyID($pad);
$readOnlyID = $readOnlyID->readOnlyID;
echo "<h3><a href=$host/ro/$readOnlyID>Read only view</a>";
$getpublicStatus = $instance->getPublicStatus($pad); // get Security status of the pad
if ($getpublicStatus->publicStatus === false){
echo "<h3><a href=/users.php?action=makePublic&name=$pad>Make pad public</a></h3>";
}
else{
echo "<h3><a href=/users.php?action=makePrivate&name=$pad>Make pad private</a></h3>";
}
echo "</div>";
$count++;
}
echo "</div>";
?>
</body>
38 changes: 24 additions & 14 deletions examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
include 'etherpad-lite-client.php';

// Create an instance
$instance = new EtherpadLiteClient('EtherpadFTW','http://beta.etherpad.org/api'); // Example URL: http://your.hostname.tld:8080/api
$instance = new EtherpadLiteClient('EtherpadFTW','http://beta.etherpad.org:9001/api'); // Example URL: http://your.hostname.tld:8080/api

// All API calls return a JSON value as documented in the API here: https://github.com/Pita/etherpad-lite/wiki/HTTP-API

Expand All @@ -20,8 +20,15 @@
echo "\n\ncreateAuthor Failed with message ". $e->getMessage();
}

/* Example: get Mapped Author */
// Bug this is not written yet
/* Example: get Mapped Author ID based on a value from your web application such as the userID */
try {
$authormap = $instance->createAuthorIfNotExistsFor('John McLear', 'Cake'); // This would show my local UserID mapping John McLear as Cake on Etherpad
} catch (Exception $e) {
echo "\n\ncreateAuthorIfNotExistsFor Failed with message ". $e->getMessage();
}

//cake


/* Example: Create a new Pad */
try {
Expand All @@ -45,14 +52,6 @@
echo "Failed with message ". $e->getMessage();
}

/* Example: Delete Pad */
try {
$instance->deletePad('testPad');
} catch (Exception $e) {
// the pad doesn't exist?
echo "\n\ndeletePad Failed with message ". $e->getMessage();
}

/* Example: Get Ready Only ID of a pad */
try {
$readOnlyID = $instance->getReadOnlyID('testPad');
Expand Down Expand Up @@ -114,7 +113,15 @@
echo "End of Pad Text\n\n<hr>";
} catch (Exception $e) {
// the pad already exists or something else went wrong
echo "\n\nisgetText Failed with message ". $e->getMessage();
echo "\n\ngetText Failed with message ". $e->getMessage();
}

/* Example: Delete Pad */
try {
$instance->deletePad('testPad');
} catch (Exception $e) {
// the pad doesn't exist?
echo "\n\ndeletePad Failed with message ". $e->getMessage();
}

echo "<h1>Groups</h1>";
Expand Down Expand Up @@ -150,8 +157,11 @@
}

/* Example: Create Mapped Group -- This maps a humanly readable name to a groupID */
// $mapGroup = $instance->getMappedGroup($groupID);
// BUG This bit is confusing as hell and the PHP function doesn't exist in the class - Waitnig on original author to write it
try {
$mapGroup = $instance->createGroupIfNotExistsFor("Guests");
} catch (Exception $e) {
echo "\n\ndeleteGroupFailed: ". $e->getMessage();
}

/* Example: Delete a Group */
try {
Expand Down

0 comments on commit 10471ec

Please sign in to comment.