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