/classes/ElggPad.php

https://github.com/efault/elggpad-lite · PHP · 163 lines · 116 code · 30 blank · 17 comment · 19 complexity · 9af994700f861e72e9c556d782d2d830 MD5 · raw file

  1. <?php
  2. /**
  3. * Elgg EtherPad
  4. *
  5. *
  6. */
  7. class ElggPad extends ElggObject {
  8. protected $pad;
  9. protected $groupID;
  10. protected $authorID;
  11. /**
  12. * Initialise the attributes array to include the type,
  13. * title, and description.
  14. *
  15. * @return void
  16. */
  17. protected function initializeAttributes() {
  18. parent::initializeAttributes();
  19. $this->attributes['subtype'] = "etherpad";
  20. }
  21. function save(){
  22. $guid = parent::save();
  23. try {
  24. $sessionID = $this->startSession();
  25. $groupID = $this->groupID;
  26. // Create a pad if not exists
  27. if (!$this->pname) {
  28. $name = uniqid();
  29. $this->get_pad_client()->createGroupPad($groupID, $name, elgg_get_plugin_setting('new_pad_text', 'etherpad'));
  30. $this->setMetaData('pname', $groupID . "$" . $name);
  31. }
  32. $padID = $this->getMetadata('pname');
  33. //set etherpad permissions
  34. if($this->access_id == ACCESS_PUBLIC) {
  35. $this->get_pad_client()->setPublicStatus($padID, "true");
  36. } else {
  37. $this->get_pad_client()->setPublicStatus($padID, "false");
  38. }
  39. $this->get_pad_client()->deleteSession($sessionID);
  40. } catch (Exception $e){
  41. return false;
  42. }
  43. return $guid;
  44. }
  45. function delete(){
  46. try {
  47. $this->startSession();
  48. $this->get_pad_client()->deletePad($this->getMetaData('pname'));
  49. } catch(Exception $e) {
  50. return false;
  51. }
  52. return parent::delete();
  53. }
  54. protected function get_pad_client(){
  55. if($this->pad){
  56. return $this->pad;
  57. }
  58. require_once(elgg_get_plugins_path() . 'etherpad/vendors/etherpad-lite-client.php');
  59. // Etherpad: Create an instance
  60. $apikey = elgg_get_plugin_setting('etherpad_key', 'etherpad');
  61. $apiurl = elgg_get_plugin_setting('etherpad_host', 'etherpad') . "/api";
  62. $this->pad = new EtherpadLiteClient($apikey, $apiurl);
  63. return $this->pad;
  64. }
  65. protected function startSession(){
  66. if($this->container_guid) {
  67. $container_guid = $this->container_guid;
  68. } else {
  69. $container_guid = elgg_get_logged_in_user_guid();
  70. }
  71. //Etherpad: Create an etherpad group for the elgg container
  72. $mappedGroup = $this->get_pad_client()->createGroupIfNotExistsFor($container_guid);
  73. $this->groupID = $mappedGroup->groupID;
  74. //Etherpad: Create an author(etherpad user) for logged in user
  75. $author = $this->get_pad_client()->createAuthorIfNotExistsFor(elgg_get_logged_in_user_entity()->username);
  76. $this->authorID = $author->authorID;
  77. //Etherpad: Create session
  78. $validUntil = mktime(date("H"), date("i")+5, 0, date("m"), date("d"), date("y")); // 5 minutes in the future
  79. $session = $this->get_pad_client()->createSession($this->groupID, $this->authorID, $validUntil);
  80. $sessionID = $session->sessionID;
  81. $domain = "." . parse_url(elgg_get_site_url(), PHP_URL_HOST);
  82. if(!setcookie('sessionID', $sessionID, $validUntil, '/', $domain)){
  83. throw new Exception(elgg_echo('etherpad:error:cookies_required'));
  84. }
  85. return $sessionID;
  86. }
  87. protected function getAddress(){
  88. return elgg_get_plugin_setting('etherpad_host', 'etherpad') . "/p/". $this->getMetadata('pname');
  89. }
  90. protected function getTimesliderAddress(){
  91. return $this->getAddress() . "/timeslider";
  92. }
  93. protected function getReadOnlyAddress(){
  94. if($this->getMetadata('readOnlyID')){
  95. $readonly = $this->getMetadata('readOnlyID');
  96. } else {
  97. $padID = $this->getMetadata('pname');
  98. $readonly = $this->get_pad_client()->getReadOnlyID($padID)->readOnlyID;
  99. $this->setMetaData('readOnlyID', $readonly);
  100. }
  101. return elgg_get_plugin_setting('etherpad_host', 'etherpad') . "/ro/". $readonly;
  102. }
  103. function getPadPath($timeslider = false){
  104. $settings = array('show_controls', 'monospace_font', 'show_chat', 'line_numbers');
  105. if(elgg_is_logged_in()) {
  106. $name = elgg_get_logged_in_user_entity()->name;
  107. } else {
  108. $name = 'undefined';
  109. }
  110. array_walk($settings, function(&$setting) {
  111. if(elgg_get_plugin_setting($setting, 'etherpad') == 'no') {
  112. $setting = 'false';
  113. } else {
  114. $setting = 'true';
  115. }
  116. });
  117. $options = '?' . http_build_query(array(
  118. 'userName' => $name,
  119. 'showControls' => $settings[0],
  120. 'useMonospaceFont' => $settings[1],
  121. 'showChat' => $settings[2],
  122. 'showLineNumbers' => $settings[3],
  123. ));
  124. $this->startSession();
  125. if($this->canEdit() && !$timeslider) {
  126. return $this->getAddress() . $options;
  127. } elseif ($this->canEdit() && $timeslider) {
  128. return $this->getTimesliderAddress() . $options;
  129. } else {
  130. return $this->getReadOnlyAddress() . $options;
  131. }
  132. }
  133. }