-
Notifications
You must be signed in to change notification settings - Fork 37
/
serendipity_event_commentedit.php
277 lines (244 loc) · 11.4 KB
/
serendipity_event_commentedit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php #
if (IN_serendipity !== true) {
die ("Don't hack!");
}
// Probe for a language include with constants. Still include defines later on, if some constants were missing
$probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php';
if (file_exists($probelang)) {
include $probelang;
}
include dirname(__FILE__) . '/lang_en.inc.php';
class serendipity_event_commentedit extends serendipity_event
{
var $title = PLUGIN_EVENT_COMMENTEDIT_NAME;
function introspect(&$propbag)
{
global $serendipity;
$propbag->add('name', PLUGIN_EVENT_COMMENTEDIT_NAME);
$propbag->add('description', PLUGIN_EVENT_COMMENTEDIT_DESC);
$propbag->add('stackable', false);
$propbag->add('author', 'Malte Paskuda');
$propbag->add('requirements', array(
'serendipity' => '1.5',
'php' => '5.2.0'
));
$propbag->add('version', '0.2.3');
$propbag->add('event_hooks', array(
'frontend_saveComment_finish' => true,
'fetchcomments' => true,
'frontend_header' => true,
'external_plugin' => true
));
$propbag->add('groups', array('FRONTEND_VIEWS'));
$propbag->add('configuration', array('path',
'timeout',
'mail'
)
);
if (!$serendipity['capabilities']['jquery']) {
$this->dependencies = array('serendipity_event_jquery' => 'remove');
}
}
function generate_content(&$title) {
$title = $this->title;
}
function introspect_config_item($name, &$propbag) {
global $serendipity;
switch($name) {
case 'path':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_EVENT_COMMENTEDIT_PATH);
$propbag->add('description', PLUGIN_EVENT_COMMENTEDIT_PATH_DESC);
$propbag->add('default', $serendipity['serendipityHTTPPath'] . 'plugins/serendipity_event_commentedit/');
return true;
break;
case 'timeout':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_EVENT_COMMENTEDIT_TIMEOUT);
$propbag->add('description', PLUGIN_EVENT_COMMENTEDIT_TIMEOUT_DESC);
$propbag->add('default', '300');
return true;
break;
case 'mail':
$propbag->add('type', 'boolean');
$propbag->add('name', PLUGIN_EVENT_COMMENTEDIT_MAIL);
$propbag->add('description', PLUGIN_EVENT_COMMENTEDIT_MAIL_DESC);
$propbag->add('default', false);
return true;
break;
}
}
function event_hook($event, &$bag, &$eventData, $addData = null) {
global $serendipity;
$hooks = &$bag->get('event_hooks');
if (isset($hooks[$event])) {
if ($timeout === null) {
$timeout = $this->get_config('timeout', '300');
}
if ($path === null) {
$path = $this->get_config('path', '');
}
if (!empty($path) && $path != 'default' && $path != 'none' && $path != 'empty') {
$path_defined = true;
} else {
$path_defined = false;
}
if ($mail == null) {
$mail = $this->get_config('mail', false);
}
switch($event) {
case 'external_plugin':
switch($eventData) {
case 'commentedit.js':
header('Content-Type: text/javascript');
echo file_get_contents(dirname(__FILE__). '/serendipity_event_commentedit.js');
break;
case 'jquery.jeditable.js':
header('Content-Type: text/javascript');
echo file_get_contents(dirname(__FILE__). '/jquery.jeditable.js');
break;
case 'commentedit':
global $serendipity;
//the js sent us the comment and an id json-encrypted,
//so they are named
$comment = $_REQUEST['comment'];
$comment_id = $_REQUEST['cid'];
$entry_id = $_REQUEST['entry_id'];
//Break when comment_id !=> session
if ($this->get_cached_commentid($timeout) == $comment_id) {
$this->editComment($comment_id, $comment, $entry_id);
$data = array('comment' => $comment);
serendipity_plugin_api::hook_event('frontend_display', $data);
//send mail with edit-notification to blogowner, only if normal notification is enabled and config, too
if ($serendipity['mail_comments'] == 1 && $mail) {
serendipity_sendMail($serendipity['email'], 'Comment ' . $comment_id . ' edited' , 'New comment: ' . $comment, $serendipity['blogMail']);
}
echo $data['comment'];
}
break;
case 'commentedit_load':
//load a comment from the db
$comment = $this->getComment($_REQUEST['cid'], $_REQUEST['entry_id']);
echo $comment[0]['body'];
break;
case 'commentedit_time':
//echo the remaining time
if($_SESSION['comment_made_time'] > time() - $timeout) {
echo $_SESSION['comment_made_time'] + $timeout - time();
} else {
echo 0;
}
break;
case 'commentedit_language':
$language=array('editlink' => EDIT,
'edittooltip' => PLUGIN_EVENT_COMMENTEDIT_EDITTOOLTIP,
'edittimer' => PLUGIN_EVENT_COMMENTEDIT_EDITTIMER,
'editsubmit' => SAVE,
'editcancel' => ABORT_NOW
);
//For json to work, the strings has to be utf8-encoded
echo json_encode(array_map(utf8_encode, $language));
break;
}
return true;
break;
case 'frontend_saveComment_finish':
//save corresponding sessionid because we later fetch a comment
//and check if the current session_id() belongs to the comment_cid
$this->cache_commentid($addData['comment_cid']);
return true;
break;
case 'fetchcomments':
$postBase = false;
$cids = array();
foreach($eventData as $comment) {
if ($this->get_cached_commentid($timeout) == $comment['id']) {
//we now know that the comment is from the
//user and created within the last minutes,
//so add comment_id
$cids[] = $comment['id'];
$postBase = true;
}
}
if ($postBase) {
//cebase is used for the POST of the edited
//comment to the external_plugin-call
echo '<script>var cebase = "'. $serendipity['baseURL'] .'index.php?/plugin/";</script>';
foreach($cids as $cid) {
//add edit-ability:
echo '<script>makeEditable(' . $comment['id'] . ','. $eventData['0']['entry_id'] .') </script>' . "\n";
}
}
return true;
break;
case 'frontend_header':
if($path_defined) {
echo '<script type="text/javascript" src="' . $path . 'jquery.jeditable.js"></script>' . "\n";
echo '<script type="text/javascript" src="' . $path . 'serendipity_event_commentedit.js"></script>' . "\n";
} else {
echo '<script type="text/javascript" src="' . $serendipity['baseURL'] . 'index.php?/plugin/jquery.jeditable.js"></script>' . "\n";
echo '<script type="text/javascript" src="' . $serendipity['baseURL'] . 'index.php?/plugin/commentedit.js"></script>' . "\n";
}
default:
return false;
}
} else {
return false;
}
}
/*
* Get id of the comment in the session
* @param timeout time to edit in seconds
* */
function get_cached_commentid($timeout) {
if ($_SESSION['comment_made_time'] > time() - $timeout) {
return $_SESSION['comment_made'];
} else {
return false;
}
}
/*
* Save which commentid belongs to this session
* data: commentid
* */
function cache_commentid($commentid) {
$_SESSION['comment_made'] = $commentid;
$_SESSION['comment_made_time'] = time();
}
function editComment($comment_id, $comment, $entry_id) {
global $serendipity;
$sql = "UPDATE {$serendipity['dbPrefix']}comments
SET
body = '" . $comment . "'
WHERE id = " . (int)$comment_id . " AND
entry_id = " . (int)$entry_id;
serendipity_db_query($sql);
}
function getComment($comment_id, $entry_id) {
global $serendipity;
$sql = "SELECT body FROM {$serendipity['dbPrefix']}comments
WHERE id = " . (int)$comment_id . " AND
entry_id = " . (int)$entry_id;
return serendipity_db_query($sql);
}
function debugMsg($msg) {
global $serendipity;
$this->debug_fp = @fopen($serendipity['serendipityPath'] . 'templates_c/commentedit.log', 'a');
if (!$this->debug_fp) {
return false;
}
if (empty($msg)) {
fwrite($this->debug_fp, "failure \n");
} else {
fwrite($this->debug_fp, print_r($msg, true));
}
fclose($this->debug_fp);
}
}
/* vim: set sts=4 ts=4 expandtab :
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/