Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #4 from johnyma22/master

updates.
  • Loading branch information...
commit 10471ec0949d3a0855c6ae6a74fea89ef169b7e2 2 parents 5295b81 + faeb3c8
@TomNomNom authored
Showing with 191 additions and 18 deletions.
  1. +3 −1 README.md
  2. +6 −3 etherpad-lite-client.php
  3. +158 −0 example_big.php
  4. +24 −14 examples.php
View
4 README.md
@@ -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:
View
9 etherpad-lite-client.php
@@ -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");
@@ -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
));
}
View
158 example_big.php
@@ -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>
View
38 examples.php
@@ -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
@@ -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 {
@@ -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');
@@ -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>";
@@ -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 {

0 comments on commit 10471ec

Please sign in to comment.
Something went wrong with that request. Please try again.