1<html>
2<head>
3<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
4<style type="text/css">
5h1{margin:0;display:inline;}
6h2{display:inline;font-size:81.3%}
7h3{display:inline;margin-right:20px;}
8.pad{background-color:#F7F7F7;padding:20px;border-style:solid;border-width:1px;margin-bottom:10px;}
9.contents{display:none;word-wrap: break-word;}
10#pads{width:700px;}
11</style>
12</head>
13<body>
14<pre>
15<?php
16//error_reporting(E_ALL);
17//ini_set('display_errors', '1');
18
19// Include the Class
20include 'etherpad-lite-client.php';
21$host = "http://beta.etherpad.org";
22
23// By this point the user has authenticated
24
25// Create an instance
26$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
27
28// Get the Params from the URL
29$action = $_GET['action'];
30
31// Step 1, get GroupID of the userID where userID is OUR userID and NOT the userID used by Etherpad
32try {
33  $mappedGroup = $instance->createGroupIfNotExistsFor("John@McLear.co.uk");
34  $groupID = $mappedGroup->groupID;
35} catch (Exception $e) {}
36
37// Create a session
38/* get Mapped Author ID based on a value from your web application such as the userID */
39try {
40  $author = $instance->createAuthorIfNotExistsFor('John McLear', 'Cake');
41  $authorID = $author->authorID;
42} catch (Exception $e) {
43  echo "\n\ncreateAuthorIfNotExistsFor Failed with message ". $e->getMessage();
44}
45$validUntil = mktime(0, 0, 0, date("m"), date("d")+1, date("y")); // One day in the future
46$sessionID = $instance->createSession($groupID, $authorID, $validUntil);
47$sessionID = $sessionID->sessionID;
48setcookie("sessionID",$sessionID); // Set a cookie
49
50// echo "New Session ID is $sessionID->sessionID\n\n";
51
52// Run some logic based on the initial request
53if ($action){ // if an action is set then lets do it.
54
55  if ($action == "newPad") // If the request is to create a new pad
56  {
57    $name = $_GET["name"];
58    if (!$name){
59      function genRandomString() { // A funtion to generate a random name if something doesn't already exist
60        $length = 10;
61        $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
62        $string = '';
63        for ($p = 0; $p < $length; $p++) {
64          $string .= $characters[mt_rand(0, strlen($characters))];
65        }
66        return $string;
67      }
68      $name = genRandomString();
69    }
70    $contents = $_GET["contents"];
71    try {
72      $newPad = $instance->createGroupPad($groupID,$name,$contents);
73      $padID = $newPad->padID;
74      $newlocation = "$host/p/$padID"; // redirect to the new padID location
75      header( "Location: $newlocation" ) ;
76    } catch (Exception $e) {
77      echo "\n\ncreateGroupPad Failed with message ". $e->getMessage();
78    }
79  }
80  if ($action == "deletePad") // If teh request is to delete an existing pad
81  {
82    $name = $_GET["name"];
83    try {
84      // urldecode seems to be a historical problem
85      $name = urldecode($name);
86      $instance->deletePad($groupID . "\$" . $name);
87    } catch (Exception $e) {
88      // the pad doesn't exist?
89      echo "\n\ndeletePad Failed with message ". $e->getMessage();
90    }
91  }
92  if ($action == "makePublic") // If teh request is to delete an existing pad
93  {
94    $name = $_GET["name"];
95    try {
96      $instance->setPublicStatus($name,"true");
97    } catch (Exception $e) {
98    // the pad doesn't exist?
99    echo "\n\nMake Public Failed with message ". $e->getMessage();
100    }
101  }
102  if ($action == "makePrivate") // If teh request is to delete an existing pad
103  {
104    $name = $_GET["name"];
105    try {
106      $instance->setPublicStatus($name,"false");
107    } catch (Exception $e) {
108    // the pad doesn't exist?
109    echo "\n\nMake Private Failed with message ". $e->getMessage();
110    }
111  }
112
113}
114
115// Step 2, list Pads from this Group.
116/* Example: List Pads from a group */
117try {
118  $padList = $instance->listPads($groupID);
119  $padList = $padList->padIDs;
120} catch (Exception $e) {
121  echo "\n\nlistPads Failed: ". $e->getMessage();
122}
123
124
125// Begin writing to the UI
126// echo "<h1>Pads</h1><div id='pads'>";
127echo "Create new Pad <form action='#' style='display:inline;'><input type='hidden' name='action' value='newPad'><input type='text' name='name'><input type='submit'></form>";
128
129$count = 0;
130
131foreach($padList as $pad => $key){  // For each pad in the object
132  // This should really be ordered based on last modified
133  $padname = explode("$",$pad);
134  $group = $padname[0];
135  $padname = $padname[1];
136  $padContents = $instance->getText($pad); // Get the pad contents
137  $contents = $padContents->text;
138  // this should escape whitespaces at $padname. Note: whitespaces dosn't appear in new versions of etherpad-lite
139  $padnameEncoded = urlencode($padname);
140  $padUrl = $group . "\$" . $padnameEncoded;
141  echo "<div class='pad'>";
142  echo "<h1><a href=$host/p/$padUrl>$padname</a></h1>";
143  echo " - <h2><a onClick='$(\"#contents$count\").slideDown();'>Preview</a></h2><br/>";
144  echo "<div class='contents' id=contents$count>$contents</div>";
145  echo "<h3><a href=example_big.php?action=deletePad&name=$padnameEncoded>Delete Pad</a></h3>";
146  echo "<h3><a href=$host/p/$padUrl>Edit Pad</a></h3>";
147  $readOnlyID = $instance->getReadOnlyID($pad);
148  $readOnlyID = $readOnlyID->readOnlyID;
149  echo "<h3><a href=$host/ro/$readOnlyID>Read only view</a>";
150  $getpublicStatus = $instance->getPublicStatus($pad); // get Security status of the pad
151  if ($getpublicStatus->publicStatus === false){
152    echo "<h3><a href=example_big.php?action=makePublic&name=$padUrl>Make pad public</a></h3>";
153  }
154  else{
155    echo "<h3><a href=example_big.php?action=makePrivate&name=$padUrl>Make pad private</a></h3>";
156  }
157  echo "</div>";
158  $count++;
159}
160echo "</div>";
161?>
162</body>
163