/
snippet.quipreply.php
233 lines (213 loc) · 9.81 KB
/
snippet.quipreply.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
<?php
/**
* Quip
*
* Copyright 2010-11 by Shaun McCormick <shaun@modx.com>
*
* This file is part of Quip, a simple commenting component for MODx Revolution.
*
* Quip is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* Quip is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Quip; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307 USA
*
* @package quip
*/
/**
* Displays a reply form for a thread
*
* @var modX $modx
* @var array $scriptProperties
* @var Quip $quip
*
* @name QuipReply
* @author Shaun McCormick <shaun@modx.com>
* @package quip
*/
$quip = $modx->getService('quip','Quip',$modx->getOption('quip.core_path',null,$modx->getOption('core_path').'components/quip/').'model/quip/',$scriptProperties);
if (!($quip instanceof Quip)) return '';
$quip->initialize($modx->context->get('key'));
$controller = $quip->loadController('ThreadReply');
$output = $controller->run($scriptProperties);
return $output;
/* get thread */
$thread = $modx->getOption('quip_thread',$_REQUEST,$modx->getOption('thread',$scriptProperties,''));
if (empty($thread)) return '';
$thread = $modx->getObject('quipThread',array('name' => $thread));
if (!$thread) return '';
/* sync properties with thread row values */
$thread->sync($scriptProperties);
$ps = $thread->get('quipreply_call_params');
if (!empty($ps)) {
$diff = array_diff_assoc($ps,$scriptProperties);
if (empty($diff)) $diff = array_diff_assoc($scriptProperties,$ps);
}
if (empty($_REQUEST['quip_thread']) && (!empty($diff) || empty($ps))) { /* only sync call params if not on threaded reply page */
$thread->set('quipreply_call_params',$scriptProperties);
$thread->save();
}
/* if in threaded reply page, get the original passing values to QuipReply in the thread's main page and use those */
if (!empty($_REQUEST['quip_thread']) && is_array($ps) && !empty($ps)) $scriptProperties = array_merge($scriptProperties,$ps);
unset($ps,$diff);
/* setup default properties */
$requireAuth = $modx->getOption('requireAuth',$scriptProperties,false);
$requireUsergroups = $modx->getOption('requireUsergroups',$scriptProperties,false);
$addCommentTpl = $modx->getOption('tplAddComment',$scriptProperties,'quipAddComment');
$loginToCommentTpl = $modx->getOption('tplLoginToComment',$scriptProperties,'quipLoginToComment');
$previewTpl = $modx->getOption('tplPreview',$scriptProperties,'quipPreviewComment');
$closeAfter = $modx->getOption('closeAfter',$scriptProperties,14);
$requirePreview = $modx->getOption('requirePreview',$scriptProperties,false);
$previewAction = $modx->getOption('previewAction',$scriptProperties,'quip-preview');
$postAction = $modx->getOption('postAction',$scriptProperties,'quip-post');
$allowedTags = $modx->getOption('quip.allowed_tags',$scriptProperties,'<br><b><i>');
$preHooks = $modx->getOption('preHooks',$scriptProperties,'');
$postHooks = $modx->getOption('postHooks',$scriptProperties,'');
$unsubscribeAction = $modx->getOption('unsubscribeAction',$scriptProperties,'quip_unsubscribe');
/* get parent and auth */
$parent = $modx->getOption('quip_parent',$_REQUEST,$modx->getOption('parent',$scriptProperties,0));
$hasAuth = $modx->user->hasSessionContext($modx->context->get('key')) || $modx->getOption('debug',$scriptProperties,false) || empty($requireAuth);
if (!empty($requireUsergroups)) {
$requireUsergroups = explode(',',$requireUsergroups);
$hasAuth = $modx->user->isMember($requireUsergroups);
}
/* setup default placeholders */
$placeholders = array();
$p = $modx->request->getParameters();
unset($p['reported'],$p['quip_approved']);
$placeholders['parent'] = $parent;
$placeholders['thread'] = $thread->get('name');
$placeholders['url'] = $modx->makeUrl($modx->resource->get('id'),'',$p);
$placeholders['idprefix'] = $thread->get('idprefix');
/* handle POST */
$fields = array();
$hasPreview = false;
if (!empty($_POST)) {
foreach ($_POST as $k => $v) {
$fields[$k] = str_replace(array('[',']'),array('[',']'),$v);
}
$fields['name'] = strip_tags($fields['name']);
$fields['email'] = strip_tags($fields['email']);
$fields['website'] = strip_tags($fields['website']);
/* verify a message was posted */
if (empty($fields['comment'])) $errors['comment'] = $modx->lexicon('quip.message_err_ns');
if (empty($fields['name'])) $errors['name'] = $modx->lexicon('quip.name_err_ns');
if (empty($fields['email'])) $errors['email'] = $modx->lexicon('quip.email_err_ns');
if (!empty($_POST[$postAction]) && empty($errors)) {
$comment = include_once $quip->config['processorsPath'].'web/comment/create.php';
if (is_object($comment) && $comment instanceof quipComment) {
$params = $modx->request->getParameters();
unset($params[$postAction],$params['quip_parent'],$params['quip_thread']);
$params['quip_approved'] = $comment->get('approved') ? 1 : 0;
$url = $comment->makeUrl('',$params);
/* if not approved, remove # and replace with success message #
* since comment is not yet visible
*/
if (!$comment->get('approved')) {
$url = str_replace('#'.$thread->get('idprefix').$comment->get('id'),'#quip-success-'.$thread->get('idprefix'),$url);
}
$modx->sendRedirect($url);
} else if (is_array($comment)) {
$errors = array_merge($errors,$comment);
}
$fields[$previewAction] = true;
}
/* handle preview */
else if (!empty($fields[$previewAction]) && empty($errors)) {
$errors = include_once $quip->config['processorsPath'].'web/comment/preview.php';
}
if (!empty($errors)) {
$placeholders['error'] = implode("<br />\n",$errors);
foreach ($errors as $k => $v) {
$placeholders['error.'.$k] = $v;
}
$placeholders = array_merge($placeholders,$fields);
}
}
/* display moderated success message */
if (isset($_GET['quip_approved']) && $_GET['quip_approved'] == 0) {
$placeholders['successMsg'] = $modx->lexicon('quip.comment_will_be_moderated');
}
/* handle unsubscribing from thread */
if (!empty($_GET[$unsubscribeAction]) && $modx->user->hasSessionContext($modx->context->get('key'))) {
$profile = $modx->user->getOne('Profile');
if ($profile) {
$notify = $modx->getObject('quipCommentNotify',array(
'email' => $profile->get('email'),
'thread' => $thread,
));
if ($notify && $notify->remove()) {
$placeholders['successMsg'] = $modx->lexicon('quip.unsubscribed');
}
}
}
/* if using recaptcha, load recaptcha html if user is not logged in */
$disableRecaptchaWhenLoggedIn = (boolean)$modx->getOption('disableRecaptchaWhenLoggedIn',$scriptProperties,true);
$useRecaptcha = (boolean)$modx->getOption('recaptcha',$scriptProperties,false);
if ($useRecaptcha && !($disableRecaptchaWhenLoggedIn && $hasAuth) && !$hasPreview) {
$recaptcha = $modx->getService('recaptcha','reCaptcha',$quip->config['modelPath'].'recaptcha/');
if ($recaptcha instanceof reCaptcha) {
$recaptchaTheme = $modx->getOption('recaptchaTheme',$scriptProperties,'clean');
$html = $recaptcha->getHtml($recaptchaTheme);
$modx->setPlaceholder('quip.recaptcha_html',$html);
} else {
return $modx->lexicon('quip.recaptcha_err_load');
}
}
/* build reply form */
$replyForm = '';
$stillOpen = $thread->checkIfStillOpen($closeAfter) && !$modx->getOption('closed',$scriptProperties,false);
if ($hasAuth && $stillOpen) {
$phs = array_merge($placeholders,array(
'username' => $modx->user->get('username'),
));
$phs['unsubscribe'] = '';
/* prefill fields */
$profile = $modx->user->getOne('Profile');
if ($profile) {
$phs['name'] = !empty($fields['name']) ? $fields['name'] : $profile->get('fullname');
$phs['email'] = !empty($fields['email']) ? $fields['email'] : $profile->get('email');
$phs['website'] = !empty($fields['website']) ? $fields['website'] : $profile->get('website');
/* allow for unsubscribing for logged-in users */
if ($modx->user->hasSessionContext($modx->context->get('key'))) {
$notify = $modx->getObject('quipCommentNotify',array(
'email' => $profile->get('email'),
'thread' => $thread,
));
if ($notify) {
$phs['notifyId'] = $notify->get('id');
$phs['unsubscribe'] = $quip->getChunk('quipUnsubscribe',$phs);
$params = $modx->request->getParameters();
$params[$unsubscribeAction] = 1;
$phs['unsubscribeUrl'] = $modx->makeUrl($modx->resource->get('id'),'',$params);
}
}
}
/* if requirePreview == false, auto-can post */
if (!$requirePreview) {
$phs['can_post'] = true;
}
$phs['post_action'] = $postAction;
$phs['preview_action'] = $previewAction;
$phs['allowed_tags'] = $allowedTags;
$phs['notifyChecked'] = !empty($fields['notify']) ? ' checked="checked"' : '';
$replyForm = $quip->getChunk($addCommentTpl,$phs);
} else if (!$stillOpen) {
$replyForm = $modx->lexicon('quip.thread_autoclosed');
} else {
$replyForm = $quip->getChunk($loginToCommentTpl,$placeholders);
}
/* output or set to placeholder */
$toPlaceholder = $modx->getOption('toPlaceholder',$scriptProperties,false);
if ($toPlaceholder) {
$modx->setPlaceholder($toPlaceholder,$replyForm);
return '';
}
return $replyForm;