1<?php
2/**
3 * This is Zenphoto's unified comment handling facility
4 *
5 * Place a call on the function <var>printCommentForm()</var> in your script where you
6 * wish the comment items to appear.
7 *
8 * The plugin uses <var>%ZENFOLDER%/%PLUGIN_FOLDER%/comment_form/comment_form.php</var>.
9 * However, you may override this form by placing a script of the same name in a similar folder in your theme.
10 * This will allow you to customize the appearance of the comments on your site.
11 *
12 * There are several options to tune what the plugin will do.
13 *
14 * @author Stephen Billard (sbillard)
15 * @package comment-form
16 */
17$plugin_is_filter = 5 | CLASS_PLUGIN;
18$plugin_description = gettext("Provides a unified comment handling facility.");
19$plugin_author = "Stephen Billard (sbillard)";
20$plugin_category = gettext('Misc');
21
22$option_interface = 'comment_form';
23
24zp_register_filter('admin_toolbox_global', 'comment_form::toolbox');
25
26require_once(SERVERPATH . '/' . ZENFOLDER . '/' . PLUGIN_FOLDER . '/comment_form/class-comment.php');
27require_once(SERVERPATH . '/' . ZENFOLDER . '/' . PLUGIN_FOLDER . '/comment_form/functions.php');
28
29if (OFFSET_PATH) {
30	zp_register_filter('admin_overview', 'comment_form_print10Most');
31	zp_register_filter('admin_tabs', 'comment_form::admin_tabs');
32} else {
33	zp_register_filter('handle_comment', 'comment_form_postcomment');
34	zp_register_filter('object_addComment', 'comment_form_addComment');
35	if (getOption('comment_form_pagination')) {
36		zp_register_filter('theme_head', 'comment_form_PaginationJS');
37	}
38	if (getOption('tinymce4_comments')) {
39		require_once(SERVERPATH . '/' . ZENFOLDER . '/' . PLUGIN_FOLDER . '/tinymce4.php');
40		zp_register_filter('theme_head', 'comment_form_visualEditor');
41	}
42}
43
44class comment_form {
45
46	/**
47	 * class instantiation function
48	 *
49	 */
50	function __construct() {
51		setOptionDefault('email_new_comments', 1);
52		setOptionDefault('comment_name_required', 'required');
53		setOptionDefault('comment_email_required', 'required');
54		setOptionDefault('comment_web_required', 1);
55		setOptionDefault('Use_Captcha', false);
56		setOptionDefault('comment_form_addresses', 0);
57		setOptionDefault('comment_form_require_addresses', 0);
58		setOptionDefault('comment_form_members_only', 0);
59		setOptionDefault('comment_form_albums', 1);
60		setOptionDefault('comment_form_images', 1);
61		setOptionDefault('comment_form_articles', 1);
62		setOptionDefault('comment_form_pages', 1);
63		setOptionDefault('comment_form_rss', 1);
64		setOptionDefault('comment_form_private', 1);
65		setOptionDefault('comment_form_anon', 1);
66		setOptionDefault('comment_form_showURL', 1);
67		setOptionDefault('comment_form_comments_per_page', 10);
68		setOptionDefault('comment_form_pagination', true);
69		setOptionDefault('comment_form_toggle', 1);
70		setOptionDefault('tinymce4_comments', null);
71		setOptionDefault('comment_form_dataconfirmation', 0);
72	}
73
74	/**
75	 * Reports the supported options
76	 *
77	 * @return array
78	 */
79	function getOptionsSupported() {
80		global $_zp_captcha;
81		require_once(SERVERPATH . '/' . ZENFOLDER . '/' . PLUGIN_FOLDER . '/tinymce4.php');
82		$checkboxes = array(
83				gettext('Albums') => 'comment_form_albums',
84				gettext('Images') => 'comment_form_images');
85		if (extensionEnabled('zenpage')) {
86			$checkboxes = array_merge($checkboxes, array(
87					gettext('Pages') => 'comment_form_pages',
88					gettext('News') => 'comment_form_articles'));
89		}
90		$configarray = getTinyMCE4ConfigFiles('comment');
91
92		$options = array(
93				gettext('Enable comment notification') => array(
94						'key' => 'email_new_comments',
95						'type' => OPTION_TYPE_CHECKBOX,
96						'order' => 0,
97						'desc' => gettext('Email the Admin when new comments are posted')),
98				gettext('Name field') => array(
99						'key' => 'comment_name_required',
100						'type' => OPTION_TYPE_RADIO,
101						'order' => 0.1,
102						'buttons' => array(
103								gettext('Omit') => 0,
104								gettext('Show') => 1,
105								gettext('Require') => 'required'),
106						'desc' => gettext('If the <em>Name</em> field is required, the poster must provide a name.')),
107				gettext('Email field') => array(
108						'key' => 'comment_email_required',
109						'type' => OPTION_TYPE_RADIO,
110						'order' => 0.2,
111						'buttons' => array(
112								gettext('Omit') => 0,
113								gettext('Show') => 1,
114								gettext('Require') => 'required'),
115						'desc' => gettext('If the <em>Email</em> field is required, the poster must provide an email address.')),
116				gettext('Website field') => array(
117						'key' => 'comment_web_required',
118						'type' => OPTION_TYPE_RADIO,
119						'order' => 0.3,
120						'buttons' => array(
121								gettext('Omit') => 0,
122								gettext('Show') => 1,
123								gettext('Require') => 'required'),
124						'desc' => gettext('If the <em>Website</em> field is required, the poster must provide a website.')),
125				gettext('Captcha field') => array(
126						'key' => 'Use_Captcha',
127						'type' => OPTION_TYPE_RADIO,
128						'order' => 0.4,
129						'buttons' => array(
130								gettext('Omit') => 0,
131								gettext('For guests') => 2,
132								gettext('Require') => 1),
133						'desc' => ($_zp_captcha->name) ? gettext('If <em>Captcha</em> is required, the form will include a Captcha verification.') : '<span class="notebox">' . gettext('No captcha handler is enabled.') . '</span>'),
134				gettext('Address fields') => array(
135						'key' => 'comment_form_addresses',
136						'type' => OPTION_TYPE_RADIO,
137						'order' => 7,
138						'buttons' => array(
139								gettext('Omit') => 0,
140								gettext('Show') => 1,
141								gettext('Require') => 'required'),
142						'desc' => gettext('If <em>Address fields</em> are shown or required, the form will include positions for address information. If required, the poster must supply data in each address field.')),
143				gettext('Allow comments on') => array(
144						'key' => 'comment_form_allowed',
145						'type' => OPTION_TYPE_CHECKBOX_ARRAY,
146						'order' => 0.9,
147						'checkboxes' => $checkboxes,
148						'desc' => gettext('Comment forms will be presented on the checked pages.')),
149				gettext('Toggled comment block') => array(
150						'key' => 'comment_form_toggle',
151						'type' => OPTION_TYPE_CHECKBOX,
152						'order' => 2,
153						'desc' => gettext('If checked, existing comments will be initially hidden. Clicking on the provided button will show them.')),
154				gettext('Show author URL') => array(
155						'key' => 'comment_form_showURL',
156						'type' => OPTION_TYPE_CHECKBOX,
157						'order' => 7,
158						'desc' => gettext('To discourage SPAM, uncheck this box and the author URL will not be revealed.')),
159				gettext('Only members can comment') => array(
160						'key' => 'comment_form_members_only',
161						'type' => OPTION_TYPE_CHECKBOX,
162						'order' => 4,
163						'desc' => gettext('If checked, only logged in users will be allowed to post comments.')),
164				gettext('Allow private postings') => array(
165						'key' => 'comment_form_private',
166						'type' => OPTION_TYPE_CHECKBOX,
167						'order' => 6,
168						'desc' => gettext('If checked, posters may mark their comments as private (not for publishing).')),
169				gettext('Allow anonymous posting') => array(
170						'key' => 'comment_form_anon',
171						'type' => OPTION_TYPE_CHECKBOX,
172						'order' => 5,
173						'desc' => gettext('If checked, posters may exclude their personal information from the published post.')),
174				gettext('Include RSS link') => array(
175						'key' => 'comment_form_rss',
176						'type' => OPTION_TYPE_CHECKBOX,
177						'order' => 8,
178						'desc' => gettext('If checked, an RSS link will be included at the bottom of the comment section.')),
179				gettext('Comments per page') => array(
180						'key' => 'comment_form_comments_per_page',
181						'type' => OPTION_TYPE_TEXTBOX,
182						'order' => 9,
183						'desc' => gettext('The comments that should show per page on the admin tab and when using the jQuery pagination')),
184				gettext('Comment editor configuration') => array(
185						'key' => 'tinymce4_comments',
186						'type' => OPTION_TYPE_SELECTOR,
187						'order' => 1,
188						'selections' => $configarray,
189						'null_selection' => gettext('Disabled'),
190						'desc' => gettext('Configuration file for TinyMCE when used for comments. Set to <code>Disabled</code> to disable visual editing.')),
191				gettext('Pagination') => array(
192						'key' => 'comment_form_pagination',
193						'type' => OPTION_TYPE_CHECKBOX,
194						'order' => 3,
195						'desc' => gettext('Uncheck to disable the jQuery pagination of comments. Enabled by default.')),
196				gettext('Data usage confirmation') => array(
197						'key' => 'comment_form_dataconfirmation',
198						'type' => OPTION_TYPE_CHECKBOX,
199						'order' => 11,
200						'desc' => gettext('If checked a mandatory checkbox is added for users to confirm about data storage and handling by your site. This is recommend to comply with the European GDPR.'))
201		);
202		return $options;
203	}
204
205	function handleOption($option, $currentValue) {
206
207	}
208
209	static function admin_tabs($tabs) {
210		if (zp_loggedin(COMMENT_RIGHTS)) {
211			$add = true;
212			$newtabs = array();
213			foreach ($tabs as $key => $tab) {
214				if ($add && !in_array($key, array('overview', 'edit', 'upload', 'pages', 'news', 'tags', 'menu'))) {
215					$newtabs['comments'] = array(
216							'text' => gettext("comments"),
217							'link' => FULLWEBPATH . "/" . ZENFOLDER . '/' . PLUGIN_FOLDER . '/' . 'comment_form/admin-comments.php?page=comments&tab=' . gettext('comments'),
218							'subtabs' => NULL);
219					$add = false;
220				}
221				$newtabs[$key] = $tab;
222			}
223			return $newtabs;
224		}
225		return $tabs;
226	}
227
228	static function toolbox() {
229		if (zp_loggedin(COMMENT_RIGHTS)) {
230			?>
231			<li>
232				<?php printLinkHTML(WEBPATH . "/" . ZENFOLDER . '/' . PLUGIN_FOLDER . '/' . 'comment_form/admin-comments.php?page=comments&amp;tab=' . gettext('comments'), gettext("Comments"), NULL, NULL, NULL); ?>
233			</li>
234			<?php
235		}
236	}
237
238}
239
240/**
241 * Prints a form for posting comments
242 *
243 * @param bool $showcomments defaults to true for showing list of comments
244 * @param string $addcommenttext alternate text for "Add a comment:"
245 * @param bool $addheader set true to display comment count header
246 * @param string $comment_commententry_mod use to add styles, classes to the comment form div
247 * @param bool $desc_order default false, set to true to change the comment order to descending ( = newst to oldest)
248 */
249function printCommentForm($showcomments = true, $addcommenttext = NULL, $addheader = true, $comment_commententry_mod = '', $desc_order = false) {
250	global $_zp_gallery_page, $_zp_current_admin_obj, $_zp_current_comment, $_zp_captcha, $_zp_authority, $_zp_HTML_cache, $_zp_current_image, $_zp_current_album, $_zp_current_zenpage_page, $_zp_current_zenpage_news;
251
252	if (getOption('email_new_comments')) {
253		$email_list = $_zp_authority->getAdminEmail();
254		if (empty($email_list)) {
255			setOption('email_new_comments', 0);
256		}
257	}
258	if (is_null($addcommenttext))
259		$addcommenttext = '<h3>' . gettext('Add a comment:') . '</h3>';
260	switch ($_zp_gallery_page) {
261		case 'album.php':
262			if (!getOption('comment_form_albums'))
263				return;
264			$obj = $_zp_current_album;
265			break;
266		case 'image.php':
267			if (!getOption('comment_form_images'))
268				return;
269			$obj = $_zp_current_image;
270			break;
271		case 'pages.php':
272			if (!getOption('comment_form_pages'))
273				return;
274			$obj = $_zp_current_zenpage_page;
275			break;
276		case 'news.php':
277			if (!getOption('comment_form_articles') || !is_NewsArticle())
278				return;
279			$obj = $_zp_current_zenpage_news;
280			break;
281		default:
282			return;
283			break;
284	}
285	$comments_open = $obj->getCommentsAllowed();
286	?>
287	<!-- printCommentForm -->
288	<div id="commentcontent">
289		<?php
290		$num = getCommentCount();
291		if ($showcomments) {
292			if ($num == 0) {
293				if ($addheader)
294					echo '<h3 class="empty">' . gettext('No Comments') . '</h3>';
295				$display = '';
296			} else {
297				if ($addheader)
298					echo '<h3>' . sprintf(ngettext('%u Comment', '%u Comments', $num), $num) . '</h3>';
299				if (getOption('comment_form_toggle')) {
300					?>
301					<div id="comment_toggle"><!-- place holder for toggle button --></div>
302					<script type="text/javascript">
303						// <!-- <![CDATA[
304						function toggleComments(hide) {
305							if (hide) {
306								$('div.comment').hide();
307								$('.Pagination').hide();
308								$('#comment_toggle').html('<button class="button buttons" onclick="javascript:toggleComments(false);"><?php echo gettext('show comments'); ?></button>');
309							} else {
310								$('div.comment').show();
311								$('.Pagination').show();
312								$('#comment_toggle').html('<button class="button buttons" onclick="javascript:toggleComments(true);"><?php echo gettext('hide comments'); ?></button>');
313							}
314						}
315						$(document).ready(function() {
316							toggleComments(window.location.hash.search(/#zp_comment_id_/));
317						});
318						// ]]> -->
319					</script>
320					<?php
321					$display = ' style="display:none"';
322				} else {
323					$display = '';
324				}
325			}
326			$hideoriginalcomments = '';
327			if (getOption('comment_form_pagination') && COMMENTS_PER_PAGE < $num) {
328				$hideoriginalcomments = ' style="display:none"'; // hide original comment display to be replaced by jQuery pagination
329			}
330			if (getOption('comment_form_pagination') && COMMENTS_PER_PAGE < $num) {
331				?>
332				<div class="Pagination"></div><!-- this is the jquery pagination nav placeholder -->
333				<div id="Commentresult"></div>
334				<?php
335			}
336			?>
337			<div id="comments"<?php echo $hideoriginalcomments; ?>>
338				<?php
339				while (next_comment($desc_order)) {
340					if (!getOption('comment_form_showURL')) {
341						$_zp_current_comment['website'] = '';
342					}
343					?>
344					<div class="comment" <?php echo $display; ?>>
345						<div class="commentinfo">
346							<h4 id="zp_comment_id_<?php echo $_zp_current_comment['id']; ?>"><?php printCommentAuthorLink(); ?>: <?php echo gettext('on'); ?> <?php
347								echo getCommentDateTime();
348								printEditCommentLink(gettext('Edit'), ', ', '');
349								?></h4>
350						</div><!-- class "commentinfo" -->
351						<div class="commenttext"><?php echo html_encodeTagged(getCommentBody(), false); ?></div><!-- class "commenttext" -->
352					</div><!-- class "comment" -->
353					<?php
354				}
355				?>
356			</div><!-- id "comments" -->
357			<?php
358		}
359		if (getOption('comment_form_pagination') && COMMENTS_PER_PAGE < $num) {
360			?>
361			<div class="Pagination"></div><!-- this is the jquery pagination nav placeholder -->
362			<?php
363		}
364		?>
365		<!-- Comment Box -->
366		<?php
367		if ($comments_open) {
368			if (MEMBERS_ONLY_COMMENTS && !zp_loggedin(POST_COMMENT_RIGHTS)) {
369				echo gettext('Only registered users may post comments.');
370			} else {
371				$disabled = array('name'		 => '', 'website'	 => '', 'anon'		 => '', 'private'	 => '', 'comment'	 => '',
372								'street'	 => '', 'city'		 => '', 'state'		 => '', 'country'	 => '', 'postal'	 => '', 'comment_dataconfirmation' => '');
373				$stored = array_merge(array('email' => '', 'custom' => ''), $disabled, getCommentStored());
374				$custom = getSerializedArray($stored['custom']);
375				foreach ($custom as $key => $value) {
376					if (!empty($value))
377						$stored[$key] = $value;
378				}
379
380				foreach ($stored as $key => $value) {
381					$disabled[$key] = false;
382				}
383
384				if (zp_loggedin()) {
385					if (extensionEnabled('userAddressFields')) {
386						$address = userAddressFields::getCustomData($_zp_current_admin_obj);
387						foreach ($address as $key => $value) {
388							if (!empty($value)) {
389								$disabled[$key] = true;
390								$stored[$key] = $value;
391							}
392						}
393					}
394					$name = $_zp_current_admin_obj->getName();
395					if (!empty($name)) {
396						$stored['name'] = $name;
397						$disabled['name'] = ' disabled="disabled"';
398					} else {
399						$user = $_zp_current_admin_obj->getUser();
400						if (!empty($user)) {
401							$stored['name'] = $user;
402							$disabled['name'] = ' disabled="disabled"';
403						}
404					}
405					$email = $_zp_current_admin_obj->getEmail();
406					if (!empty($email)) {
407						$stored['email'] = $email;
408						$disabled['email'] = ' disabled="disabled"';
409					}
410					if (!empty($address['website'])) {
411						$stored['website'] = $address['website'];
412						$disabled['website'] = ' disabled="disabled"';
413					}
414				}
415				$data = zp_apply_filter('comment_form_data', array('data' => $stored, 'disabled' => $disabled));
416				$disabled = $data['disabled'];
417				$stored = $data['data'];
418
419				foreach ($data as $check) {
420					foreach ($check as $v) {
421						if ($v) {
422							$_zp_HTML_cache->disable(); //	shouldn't cache partially filled in pages
423							break 2;
424						}
425					}
426				}
427
428				if (!empty($addcommenttext)) {
429					echo $addcommenttext;
430				}
431				?>
432				<div id="commententry" <?php echo $comment_commententry_mod; ?>>
433					<?php
434					$theme = getCurrentTheme();
435					$form = getPlugin('comment_form/comment_form.php', $theme);
436					require($form);
437					?>
438				</div><!-- id="commententry" -->
439				<?php
440				if (getOption('comment_form_rss') && getOption('RSS_comments')) {
441					?>
442					<br class="clearall" />
443					<?php
444					if (class_exists('RSS')) {
445						switch ($_zp_gallery_page) {
446							case "image.php":
447								printRSSLink("Comments-image", "", gettext("Subscribe to comments"), "");
448								break;
449							case "album.php":
450								printRSSLink("Comments-album", "", gettext("Subscribe to comments"), "");
451								break;
452							case "news.php":
453								printRSSLink("Comments-news", "", gettext("Subscribe to comments"), "");
454								break;
455							case "pages.php":
456								printRSSLink("Comments-page", "", gettext("Subscribe to comments"), "");
457								break;
458						}
459					}
460				}
461			}
462		} else {
463			?>
464			<div id="commententry">
465				<h3><?php echo gettext('Closed for comments.'); ?></h3>
466			</div><!-- id="commententry" -->
467			<?php
468		}
469		?>
470	</div><!-- id="commentcontent" -->
471	<?php
472}
473?>