-
Notifications
You must be signed in to change notification settings - Fork 3
/
pi.commentatic.php
353 lines (286 loc) · 12.7 KB
/
pi.commentatic.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
class Plugin_commentatic extends Plugin {
public $meta = array(
'name' => 'Commentatic',
'version' => '1.0',
'author' => 'Olivier Gorzalka', // form part inspired by Eric Barnes (https://github.com/ericbarnes/Statamic-email-form)
'author_url' => 'http://clearideaz.com'
);
public $total_items = false; // total comments
public $total_pages = false; // total pages of comments
public $user_datas = false; // data of logged user
protected $comment_folder = ''; // comment path from _content
protected $raw_comment_folder = ''; // raw comment path including base path
protected $comment_file = ''; // path relative to the comment file
protected $form_datas;
protected $app;
protected $config = array(); // plugin config
protected $options = array(); // options
protected $validation = array(); // validation errors
/**
* init the Slim App and setup the paths
*/
protected function init() {
$this->config = Spyc::YAMLLoad('_config/add-ons/commentatic.yaml');
$this->app = \Slim\Slim::getInstance(); // app instance
if (!array_key_exists('email_field', $this->config)) {
throw new Exception('Hey, don\'t forget to copy the commentatic.yaml file from the plugin folder to _config/add-ons/commentatic.yaml');
}
// comment locations
$this->get_comment_path();
$this->options['class'] = $this->fetch_param('class', '');
$this->options['id'] = $this->fetch_param('id', '');
$this->options['required'] = $this->fetch_param('required');
$this->options['honeypot'] = $this->fetch_param('honeypot', true); #boolen param
}
/**
* Default methods
*/
// comment form
public function form() {
$this->init();
// Set up some default vars.
$output = '';
$vars = array(array());
$flash = $this->app->view()->getData('flash');
if ($flash['success']) {
$vars = array(array('success' => true));
}
// If the page has post data process it.
if ($this->app->request()->isPost()) {
$this->form_datas = (object)$this->app->request()->post();
if ($this->logged_in()) {
$this->form_datas->{$this->config['username_field']} = $this->username();
$this->form_datas->{$this->config['email_field']} = $this->email();
}
if ($this->config['comment_field'] != 'content') {
$this->form_datas->content = $this->form_datas->{$this->config['comment_field']};
unset($this->form_datas->{$this->config['comment_field']});
}
if ( ! $this->validate()) {
$vars = array(array('error' => true, 'errors' => $this->validation));
} elseif ($this->save()) {
$this->app->flash('success', 'Comment sent successfully !');
$url_redirect = rtrim(Statamic::get_site_root(), '/').$this->app->request()->getResourceUri();
if ($this->page_count() > 0 && $this->fetch_param('sort_dir', 'asc') != 'desc') {
$url_redirect .= '?page='.$this->page_count();
}
if( $this->options['id'] != '') {
$url_redirect .= '#'.$this->options['id'];
}
$this->app->redirect($url_redirect, 302);
} else {
$vars = array(array('error' => true, 'errors' => 'Could not send comment'));
}
}
// Display the form on the page.
$output .= '<form method="post"';
$output .= ' action="' . rtrim(Statamic::get_site_root(), '/').$this->app->request()->getResourceUri().'"';
if ( $this->options['class'] != '') { $output .= ' class="' . $this->options['class'] . '"'; }
if ( $this->options['id'] != '') { $output .= ' id="' . $this->options['id'] . '"'; }
$output .= '>';
//inject the honeypot if true
if ($this->options['honeypot']) {
$output .= '<input type="text" class="honeypot" name="wearetherobot" value="" />';
}
$output .= $this->parse_loop($this->content, $vars);
$output .= '</form>';
//inject the honeypot if true
if ($this->options['honeypot']) {
$output .= '<style>.honeypot { display:none; }</style>';
}
return $output;
}
// Comment listing method
public function listing() {
$this->init();
$folder = $this->fetch_param('folder', $this->comment_folder); // defaults to null
if ($folder != $this->comment_folder) {
$this->get_comment_path();
$folder = $this->comment_folder; // resolve new folder path
}
$limit = $this->config['comment_per_page']; // defaults to none
$offset = $this->fetch_param('offset', 0, 'is_numeric'); // defaults to zero
$sort_by = $this->fetch_param('sort_by', 'date'); // defaults to date
$sort_dir = $this->fetch_param('sort_dir', 'asc'); // defaults to desc
$conditions = $this->fetch_param('conditions', null, false, false, false); // defaults to null
$switch = $this->fetch_param('switch', null); // defaults to null
$since = $this->fetch_param('since', null); // defaults to null
$until = $this->fetch_param('until', null); // defaults to null
$paginate = $this->fetch_param('paginate', true, false, true); // defaults to true
if ($this->config['comment_per_page'] && $paginate) {
// override limit/offset if paging
$pagination_variable = Statamic::get_pagination_variable();
$page = $this->app->request()->get($pagination_variable) ? $this->app->request()->get($pagination_variable) : 1;
$offset = (($page * $this->config['comment_per_page']) - $this->config['comment_per_page']) + $offset;
}
if ($this->comment_folder) {
$list = Statamic::get_content_list($folder, $limit, $offset, false, true, $sort_by, $sort_dir, $conditions, $switch, false, false, $since, $until);
if (sizeof($list)) {
foreach ($list as $key => $item) {
/* Begin Gravatar Support */
$gravatar_src = 'http://www.gravatar.com/avatar/';
$gravatar_href = 'http://www.gravatar.com/';
$gravatar_alt = '';
if (array_key_exists($this->config['email_field'], $item)) {
$gravatar_src .= md5( strtolower( trim( $item[$this->config['email_field']] ) ) );
$gravatar_href .= md5( strtolower( trim( $item[$this->config['email_field']] ) ) );
$gravatar_alt = $item[$this->config['email_field']].'\'s Gravatar';
}
$list[$key]['avatar'] = "<img src=\"$gravatar_src\" alt=\"$gravatar_alt\" />";
$list[$key]['gravatar_profile'] = $gravatar_href;
/* End Gravatar Support */
$list[$key]['content'] = Statamic::parse_content($item['content'], $item);
}
return $this->parse_loop($this->content, $list);
} else {
return array('no_results' => true);
}
}
return array();
}
// Pagination method
public function pagination() {
$this->init();
$folder = $this->fetch_param('folder', $this->comment_folder); // defaults to null
if ($folder != $this->comment_folder) {
$this->get_comment_path();
$folder = $this->comment_folder; // resolve new folder path
}
$limit = $this->config['comment_per_page']; // defaults to none
$conditions = $this->fetch_param('conditions', null, false, false, false); // defaults to null
$since = $this->fetch_param('since', null); // defaults to null
$until = $this->fetch_param('until', null); // defaults to null
$style = $this->fetch_param('style', 'prev_next'); // defaults to date
$count = Statamic::get_content_count($this->comment_folder, false, true, $conditions, $since, $until);
$pagination_variable = Statamic::get_pagination_variable();
$page = $this->app->request()->get($pagination_variable) ? $this->app->request()->get($pagination_variable) : 1;
$arr = array();
$arr['total_items'] = (int) max(0, $count);
$arr['items_per_page'] = (int) max(1, $limit);
$arr['total_pages'] = (int) ceil($count / $limit);
$arr['current_page'] = (int) min(max(1, $page), max(1, $page));
$arr['current_first_item'] = (int) min((($page - 1) * $limit) + 1, $count);
$arr['current_last_item'] = (int) min($arr['current_first_item'] + $limit - 1, $count);
$arr['previous_page'] = ($arr['current_page'] > 1) ? "?{$pagination_variable}=".($arr['current_page'] - 1) : FALSE;
$arr['next_page'] = ($arr['current_page'] < $arr['total_pages']) ? "?{$pagination_variable}=".($arr['current_page'] + 1) : FALSE;
$arr['first_page'] = ($arr['current_page'] === 1) ? FALSE : "?{$pagination_variable}=1";
$arr['last_page'] = ($arr['current_page'] >= $arr['total_pages']) ? FALSE : "?{$pagination_variable}=".$arr['total_pages'];
$arr['offset'] = (int) (($arr['current_page'] - 1) * $limit);
return $this->parser->parse($this->content, $arr);
}
/**
* Helpers
*/
// Total comment count
public function comment_count()
{
$this->init();
$folder = $this->fetch_param('folder', $this->comment_folder); // defaults to null
if ($folder != $this->comment_folder) {
$this->get_comment_path();
$folder = $this->comment_folder; // resolve new folder path
}
if (!$this->total_items) {
$this->total_items = Statamic::get_content_count($this->comment_folder, false, true, null, null, null);
}
return $this->total_items;
}
/**
* protected Methods
*/
protected function get_comment_path($folder=false)
{
if (!$folder) {
$folder = $this->app->request()->getResourceUri();
}
$this->comment_folder = '_comments'.Statamic_helper::resolve_path($folder);
$this->raw_comment_folder = '_content/'.$this->comment_folder;
$this->comment_file = $this->raw_comment_folder . '/' . date('Y-m-d').'_'.time().'.md';
}
// Check if logged in
protected function logged_in() {
return Statamic_Auth::is_logged_in();
}
protected function user_datas() {
if (!$this->user_datas) {
$this->user_datas = Statamic_Auth::get_current_user(); // retrieve user datas
}
return $this->user_datas;
}
/**
* Print the username when logged in
*/
protected function username() {
$username = '';
if ($current_user = $this->user_datas()) {
$username = $current_user->get_name(); // username is used by default
// if there's a last name, great !
if ($last_name = $current_user->get_last_name()) {
$username = $last_name;
}
// ok there's a first name, go for it !
if ($first_name = $current_user->get_first_name()) {
$username .= ' '.$first_name;
}
}
return $username;
}
// User email
protected function email() {
if ($current_user = $this->user_datas()) {
return $this->app->config['contact_email'];
}
return '';
}
// Comment page count
protected function page_count() {
if (!$this->total_pages) {
$this->total_pages = (int) ceil(($this->comment_count()) / $this->config['comment_per_page']);
}
return $this->total_pages;
}
// Validate the submitted form data
protected function validate() {
$input = $this->form_datas;
$required = explode('|', str_replace(array('comment',$this->config['email_field'], $this->config['username_field']), '', $this->options['required']));
if ( !isset($input->content) or trim($input->content) === '') {
$this->validation[]['error'] = 'Comment is required';
}
// Email is always required
if ( !$this->logged_in() && ( ! isset($input->{$this->config['email_field']}) or ! filter_var($input->email, FILTER_VALIDATE_EMAIL) ) ) {
$this->validation[]['error'] = 'Email is required';
}
// Username is always required
if ( !$this->logged_in() && ( ! isset($input->{$this->config['username_field']}) ) ) {
$this->validation[]['error'] = 'Username is required';
}
// Honeypot !
if ($this->options['honeypot'] && $input->wearetherobot != '' ) {
$this->validation[]['error'] = 'Hello robot !';
}
foreach ($required as $key => $value) {
if ($value != '' and $input->$value == '') {
$this->validation[]['error'] = ucfirst($value).' is required';
}
}
return empty($this->validation) ? true : false;
}
// Save the comment
protected function save() {
$file_content = "";
$file_content .= "---\n";
foreach($this->form_datas as $key=>$data) {
if ($key != 'content' && $key != 'wearetherobot') {
$file_content .= "{$key}: {$data}\n";
}
}
$file_content .= "---\n";
$file_content .= $this->form_datas->content;
$file_content .= "\n";
if (!file_exists($this->raw_comment_folder)) {
mkdir($this->raw_comment_folder, 0777, true);
}
return file_put_contents($this->comment_file, $file_content);
}
}