/vendors/etherpad-lite-client.php

https://github.com/efault/elggpad-lite · PHP · 260 lines · 185 code · 37 blank · 38 comment · 11 complexity · 6a6fb5cbbd3f3b0a9f3adbee70683fbc MD5 · raw file

  1. <?php
  2. class EtherpadLiteClient {
  3. const API_VERSION = 1;
  4. const CODE_OK = 0;
  5. const CODE_INVALID_PARAMETERS = 1;
  6. const CODE_INTERNAL_ERROR = 2;
  7. const CODE_INVALID_FUNCTION = 3;
  8. const CODE_INVALID_API_KEY = 4;
  9. protected $apiKey = "";
  10. protected $baseUrl = "http://localhost:9001/api";
  11. public function __construct($apiKey, $baseUrl = null){
  12. $this->apiKey = $apiKey;
  13. if (isset($baseUrl)){
  14. $this->baseUrl = $baseUrl;
  15. }
  16. if (!filter_var($this->baseUrl, FILTER_VALIDATE_URL)){
  17. throw new InvalidArgumentException("[{$this->baseUrl}] is not a valid URL");
  18. }
  19. }
  20. protected function call($function, array $arguments = array()){
  21. $query = array_merge(
  22. array('apikey' => $this->apiKey),
  23. $arguments
  24. );
  25. $url = $this->baseUrl."/".self::API_VERSION."/".$function."?".http_build_query($query);
  26. // not all PHP installs have access to curl
  27. if (function_exists('curl_init')){
  28. $c = curl_init($url);
  29. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  30. curl_setopt($c, CURLOPT_TIMEOUT, 20);
  31. $result = curl_exec($c);
  32. curl_close($c);
  33. } else {
  34. $result = file_get_contents($url);
  35. }
  36. if($result == ""){
  37. throw new UnexpectedValueException("Empty or No Response from the server");
  38. }
  39. $result = json_decode($result);
  40. if ($result === null){
  41. throw new UnexpectedValueException("JSON response could not be decoded");
  42. }
  43. return $this->handleResult($result);
  44. }
  45. protected function handleResult($result){
  46. if (!isset($result->code)){
  47. throw new RuntimeException("API response has no code");
  48. }
  49. if (!isset($result->message)){
  50. throw new RuntimeException("API response has no message");
  51. }
  52. if (!isset($result->data)){
  53. $result->data = null;
  54. }
  55. switch ($result->code){
  56. case self::CODE_OK:
  57. return $result->data;
  58. case self::CODE_INVALID_PARAMETERS:
  59. case self::CODE_INVALID_API_KEY:
  60. throw new InvalidArgumentException($result->message);
  61. case self::CODE_INTERNAL_ERROR:
  62. throw new RuntimeException($result->message);
  63. case self::CODE_INVALID_FUNCTION:
  64. throw new BadFunctionCallException($result->message);
  65. default:
  66. throw new RuntimeException("An unexpected error occurred whilst handling the response");
  67. }
  68. }
  69. // GROUPS
  70. // Pads can belong to a group. There will always be public pads that doesnt belong to a group (or we give this group the id 0)
  71. // creates a new group
  72. public function createGroup(){
  73. return $this->call("createGroup");
  74. }
  75. // this functions helps you to map your application group ids to etherpad lite group ids
  76. public function createGroupIfNotExistsFor($groupMapper){
  77. return $this->call("createGroupIfNotExistsFor", array(
  78. "groupMapper" => $groupMapper
  79. ));
  80. }
  81. // deletes a group
  82. public function deleteGroup($groupID){
  83. return $this->call("deleteGroup", array(
  84. "groupID" => $groupID
  85. ));
  86. }
  87. // returns all pads of this group
  88. public function listPads($groupID){
  89. return $this->call("listPads", array(
  90. "groupID" => $groupID
  91. ));
  92. }
  93. // creates a new pad in this group
  94. public function createGroupPad($groupID, $padName, $text){
  95. return $this->call("createGroupPad", array(
  96. "groupID" => $groupID,
  97. "padName" => $padName,
  98. "text" => $text
  99. ));
  100. }
  101. // AUTHORS
  102. // Theses authors are bind to the attributes the users choose (color and name).
  103. // creates a new author
  104. public function createAuthor($name){
  105. return $this->call("createAuthor", array(
  106. "name" => $name
  107. ));
  108. }
  109. // this functions helps you to map your application author ids to etherpad lite author ids
  110. public function createAuthorIfNotExistsFor($authorMapper, $name){
  111. return $this->call("createAuthorIfNotExistsFor", array(
  112. "authorMapper" => $authorMapper,
  113. "name" => $name
  114. ));
  115. }
  116. // SESSIONS
  117. // Sessions can be created between a group and a author. This allows
  118. // an author to access more than one group. The sessionID will be set as
  119. // a cookie to the client and is valid until a certian date.
  120. // creates a new session
  121. public function createSession($groupID, $authorID, $validUntil){
  122. return $this->call("createSession", array(
  123. "groupID" => $groupID,
  124. "authorID" => $authorID,
  125. "validUntil" => $validUntil
  126. ));
  127. }
  128. // deletes a session
  129. public function deleteSession($sessionID){
  130. return $this->call("deleteSession", array(
  131. "sessionID" => $sessionID
  132. ));
  133. }
  134. // returns informations about a session
  135. public function getSessionInfo($sessionID){
  136. return $this->call("getSessionInfo", array(
  137. "sessionID" => $sessionID
  138. ));
  139. }
  140. // returns all sessions of a group
  141. public function listSessionsOfGroup($groupID){
  142. return $this->call("listSessionsOfGroup", array(
  143. "groupID" => $groupID
  144. ));
  145. }
  146. // returns all sessions of an author
  147. public function listSessionsOfAuthor($authorID){
  148. return $this->call("listSessionsOfAuthor", array(
  149. "authorID" => $authorID
  150. ));
  151. }
  152. // PAD CONTENT
  153. // Pad content can be updated and retrieved through the API
  154. // returns the text of a pad
  155. // should take optional $rev
  156. public function getText($padID){
  157. return $this->call("getText", array(
  158. "padID" => $padID
  159. ));
  160. }
  161. // sets the text of a pad
  162. public function setText($padID, $text){
  163. return $this->call("setText", array(
  164. "padID" => $padID,
  165. "text" => $text
  166. ));
  167. }
  168. // PAD
  169. // Group pads are normal pads, but with the name schema
  170. // GROUPID$PADNAME. A security manager controls access of them and its
  171. // forbidden for normal pads to include a $ in the name.
  172. // creates a new pad
  173. public function createPad($padID, $text){
  174. return $this->call("createPad", array(
  175. "padID" => $padID,
  176. "text" => $text
  177. ));
  178. }
  179. // returns the number of revisions of this pad
  180. public function getRevisionsCount($padID){
  181. return $this->call("getRevisionsCount", array(
  182. "padID" => $padID
  183. ));
  184. }
  185. // deletes a pad
  186. public function deletePad($padID){
  187. return $this->call("deletePad", array(
  188. "padID" => $padID
  189. ));
  190. }
  191. // returns the read only link of a pad
  192. public function getReadOnlyID($padID){
  193. return $this->call("getReadOnlyID", array(
  194. "padID" => $padID
  195. ));
  196. }
  197. // sets a boolean for the public status of a pad
  198. public function setPublicStatus($padID, $publicStatus){
  199. return $this->call("setPublicStatus", array(
  200. "padID" => $padID,
  201. "publicStatus" => $publicStatus
  202. ));
  203. }
  204. // return true of false
  205. public function getPublicStatus($padID){
  206. return $this->call("getPublicStatus", array(
  207. "padID" => $padID
  208. ));
  209. }
  210. // returns ok or a error message
  211. public function setPassword($padID, $password){
  212. return $this->call("setPassword", array(
  213. "padID" => $padID,
  214. "password" => $password
  215. ));
  216. }
  217. // returns true or false
  218. public function isPasswordProtected($padID){
  219. return $this->call("isPasswordProtected", array(
  220. "padID" => $padID
  221. ));
  222. }
  223. }