CommentBoxPlus

Summary: Simple styled form to post comments, plus comment counter
Version: 2023-09-26
Prerequisites: pmwiki 2.2.56 (compatible with PHP 5.5)
Status: Stable
Maintainer: AntonyTemplier

commentboxplus3.php alternative version with website field and gravatar support

Users: +7 (View / Edit)

Questions answered by this recipe

How can I allow site visitors to add comments easily?
How can I create a form to add posts to a diary or a journal?

Description

commentboxplus.zipΔ, formerly distributed as commentboxstyled.php, is an adaptation and development of CommentBox, with some added division markup that allows styling of comments and comment heads, as well as journal and diary entries, plus a post count feature and optional acces code. It places a form on the page for easy posting of content, without need to open the page in edit view.

Note: The latest commentbox.php introduces division styling for the message items as well, but not for the headings above the messages (author, date, time).

Installation

Download commentboxplus.zipΔ, copy to commentboxplus.php to cookbook folder, copy commentboxplus.css to $Farm/pub/commentboxplus/ folder, and add to config.php:

       include_once("$FarmD/cookbook/commentboxplus.php");

Do not install both commentboxplus.php and commentbox.php, you must use only one of the two.

Usage

Place one of the following directives on a page to create a simple form for posting:

  • (:commentbox:) posts comments latest first.
  • (:commentboxchrono:) posts comments latest last.

If you put (:commentbox:) in a GroupHeader all comments get posted at the top of the page. If you put (:commentboxchrono:) in a GroupFooter all comments get posted at the bottom of the page.

If the page name ends in Journal or Diary (configurable), the script automatically inserts a comment box after the GroupHeader and before the text. You don't have to state e.g. (:commentbox:) anywhere for this feature (except loading in local/config.php of course)

Changes from CommentBox

The styled commentbox is similar, but uses some different Fmt strings, so any customisations need to be revised. It also provides class definitions as hooks for css styling: .messagedate, .messagehead, .messsageitem, .journaldate, .journalhead, .journalitem, .diaryhead, .diaryitem. A boxed style with border and background color is defined by pub/css/commentboxplus.css. This could be changed to suit the skin used.

The text entry form for diary and journal pages will be displayed only if edit access is granted.

The commentbox produces styled output according to the following format strings, which can be redefined by the admin.

Format string defaults:

ForVariableDefault
Journal pages$JPDateFmt>>journaldate<<(:nl:)!!!!$Date
 $JPTimeFmt\n>>journalhead<<(:nl:)!!!!!&ndash; \$Time &ndash;\n
 $JPItemStartFmt>>journalitem<<\n
 $JPItemEndFmt
Diary pages$DPDateFmt>>diaryhead<<(:nl:)!!!!\$Date
 $DPItemStartFmt\n>>diaryitem<<\n
 $DPItemEndFmt
Message pages$MPDateFmt>>messagedate<<(:nl:)!!!!$Date
 $MPTimeFmt(:nl:)>>messagehead<<(:nl:)!!!!!\$Author &mdash; [-at \$Time-] \n
 $MPDateTimeFmt(:nl:)>>messagehead<<\n!!!!!\$Author &mdash; [-\$Date, \$Time-] \n
 $MPItemStartFmt>>messageitem<<\n
 $MPItemEndFmt\n>><<
 $MPAnchorFmt(:commentbox:)
 $MPAnchorChronoFmt(:commentboxchrono:)

Author names are displayed as links to a profiles page. If the name should be text only and not a link, set in config.php, before including the script, $MPAuthorLink = 0;

The commentbox can be hidden for users who have no page edit permissions by using conditional markup. Otherwise users can post comments even if they don't have page edit permission, which is most often the desired behaviour, because it allows comments to be posted on a page which has edit restrictions in place. Use this markup:

(:if auth edit:)(:commentbox:)(:if:) or
(:if auth edit:)(:commentboxchrono:)(:if:)

If you do this you should also change the $MPAnchorFmt or $MPAnchorChronoFmt string accordingly:

$MPAnchorFmt = "(:commentbox:)"; or
$MPAnchorChronoFmt = "(:commentboxchrono:)";

You can set $MPAnchorFmt to some different string to use as an anchor for positioning the comments. For instance

$MPAnchorFmt = "[[#comments]]";

and place [[#comments]] somewhere on the page. The comment posts will appear beneath this anchor. But for chronological posts setting

$MPAnchorChronoFmt = "[[#comments]]";

the posts will appear ABOVE $MPAnchorChronoFmt.

By default a form validation check at user level (via javascript) will ensure that a comment post has content and that an author name is provided. This can be disabled by setting $EnableCommentFormCheck = 0. Warning messages can be customised by setting variables $NoCommentMessage and $NoAuthorMessage. This behaviour works with the built-in comment box form, but not if a custom form is used with (:input form ...:) markup. In any case posting empty comments is not possible. Anonymous posting is still possible if a custom commentbox form is used, or if the user's browser has javascript disabled.

Access Code can be enabled by setting $EnableAccessCode = true; in a local config file, before including the script. This will require users to enter a random generated access code number in order to post. This may prevent robot scripts from posting.

Captcha is now fully supported, and requires installation of Pm's Captcha recipe. Include captcha.php before including commentboxplus.php. I recommend using Captcha rather than the access code feature. The Captcha value and input field is integrated into the comment form, so you don't need to add any captcha markup, only set $EnablePostCaptchaRequired and include_once("$FarmD/cookbook/captcha.php");.

Posting of directives of form (: .... :) can be prevented by setting $EnablePreventDirectives = true;

A Website field can be added and enabled by setting $EnableWebsiteField = true; in a local config file, before including the script. Users can then add their website in addition to their name. There is no check made if entry is valid. If URL approval is in place the generated link will be checked/marked for approval.

Comments can be posted to another page using the syntax (:commentbox Group.PageName:) or (:commentboxchrono Group.PageName:). See Notes below.

Updates / Release Notes

  • 2023-09-26: Update for PHP 8.1. Changed strftime() formatting to date() formatting
  • 2020-02-26: Added missing html tags in comments form(AntonyTemplier)
  • 2017-06-21: Updated markup definitions for PHP 7.2 compatibility (HansB).
  • 2014-10-10: Updated markup definitions for PHP 5.5 compatibility, bug correction.
  • 2014-02-22: Updated markup definitions for PHP 5.5 compatibility.
  • 2013-10-31: Filter Name's author field to prevent injection of directives.
  • 2011-12-03: Id comment anchor based on new GetHigherID() in order to have an easiest moderation when deleting some comments. Minor corrections allowing full translation.
  • 2007-12-21: Fixed commentboxplus.zip (for PmWiki 2.1) which got broken with previous introduction of HandleAuth
  • 2007-12-09a: Added support for message feedback via (:messages:) markup. Added input htmlspecialcharacter filtering. Fixed some php errors.
  • 2007-12-09: Added default $HandleAuth['comment'] = 'read'; and removed BypathAuth function. You can now set in local config for instance: $HandleAuth['comment'] = 'edit'; to restrict commenting to edit authorisation.
  • 2007-11-11: Fixed Farm or non-farm check for css loader.
  • 2007-11-10: Fixed javascript error because of undeclared var. Added Farm or non-farm check for css loader.

Green: Update notes for commentboxplus2.zipΔ for pmwiki 2.2.0-beta and upwards.

  • 2007-11-09: Fixed problem with disappearing backslashes in page content.
  • 2007-11-09: commentboxplus.zipΔ : Added full support for Captcha integrated into the form. Include captcha.php before commenboxplus.php
  • 2007-11-08: Added full support for Captcha integrated into the form. Include captcha.php before commenboxplus.php
  • 2007-11-06: Added PreventPostingDirectives to author field.
  • 2007-05-02: Fixed action=PageUrl
  • 2006-11-29a: Fixed bug which added backslashes to quotes.
  • 2006-11-29: Added $PostToCommentPageFmt array for list of pages to which comments may be posted.
  • 2006-11-28: Release of commentboxplus2.zipΔ, with some extra features (for pmwiki 2.2.0-beta only): Added variables $EnablePostToGroupPage, $EnableRedirectToCommentPage, both default false. Changed edit handler function to use PmWiki's UpdatePage instead of HandleEdit function.
  • 2006-11-01: Fixed bug which prevented stylesheet from loading if $FarmPubDirUrl was not set.
  • 2006-10-31: Added $EnablePreventDirectives (default is false) which when set to true will prevent posting of any kind of directive.
  • 2006-10-31: Discontinued commentboxplus2.php. Optional Website field is now integrated into commentboxplus.php. Styling of comments is now defined in seperate pub/css/commentboxplus.css file, to make page loading more efficient. Form checking javascript code will now only load with commentbox form.
  • 2006-10-28: Added $RecipeInfo
  • 2006-10-24: Security update: Added $EnablePostToAnyPage variable, to allow posting to other pages than the current one. Default now is false (not allowed). Added security note to script, to explain safer use of this feature (added 2006-07-03).
  • 2006-08-25: Changed css style for messagehead margin. Plus small syntax correction of $HTMLStylesFmt['commentbox']
  • 2006-08-20: Added {$AccessCode} page variable, which can be used for custom (:input:) forms.
  • 2006-08-18: updated commentboxplus2.php with added Access Code.
  • 2006-08-17: Added access code [thank you John!]. Changed name of script from commentboxstyled.php to commentboxplus.php
  • 2006-08-09: Added $MPAnchorFmt and $MPAnchorChronoFmt to allow use of specific anchor locations for placement of comments.
  • 2006-08-04: Added better breakpage integration, using breakpage2.phpΔ. Added $EnablePageBreaks variable, default is 0 (disabled).
  • 2006-07-03: Added (:commentbox PageName:) option to post to a specified page (thank you John R.!)
  • 2006-04-24: Added hidden field to form to post summery as "Comment added".
  • 2006-04-04: Changed default message for empty comment box post.
  • 2006-04-01: Added formcheck which will display a warning message if comment message or author name are left empty. This can be disabled with $EnableCommentFormCheck = 0. This form validation is achieved using javascript. Changed code to disable empty comment postings. This disables empty postings even if javascript is not enabled for the user.
  • 2006-03-28: tidied code. Added variable $MPAuthorLink to enable/disable the author name as link to Profiles page.
  • 2006-03-27b: added missing global declarations of some Fmt variables which made it impossible to define the format from config.php.
  • 2006-03-27a: Second fix to stripped backslash bug. Previous did not work for journals.
  • 2006-03-27: Fixed journalhead format $JPTimeFmt.
  • 2006-03-23: Fixed bug which stripped backslashes on some systems, preventing line returns with \\ for instance.
  • 2006-03-21: Added feature which allows to hide the commentbox from users who have no page edit permission, by using conditional markup. Removed $EnableBypassAuth, it was no longer needed and caused trouble if set to 0.
  • 2006-03-04: Fixed margins for message- etc. headers
  • A page variable {$PostCount} may be used to show the number of posts (messages) on the page.
  • 2006-02-22: Added the ability to create an individual anchor for each comment, using consecutive numbers, of form[[#comment1]], [[#comment2]]] etc.
  • 2006-02-19: Added page variable {$PostCount} which will show the number of posts (messages) per page. It needs pmwiki version 2.1.beta15 or higher to show.
  • 2006-01-31: Adapted jr's solution to fix security hole, which is so much simpler.
  • 2006-01-08: commentboxplus.phpΔ updated to introduce additional security feature. This checks if markup (:commentbox:) or (:commentboxchrono:) exists on the page or the GroupFooter or GroupHeader, before proceeding with handling the page edit. Thus it prevents that it can be misused on edit protected sites to edit other pages. - I also added a div for the date head and changed the default layout of the entries slightly. ~HansB

Updates of commentboxplus3.php: Subversion Changlog

See testpages here on my testsite:

Notes

Adding page breaks - Limit Number of Posts

Install breakpage2.phpΔ into the cookbook directory. Set in config.php:

$EnablePageBreaks = 1;
$PostsPerPage = 50; #(for example, default is 20) 
include_once("$FarmD/cookbook/commentboxplus.php");
include_once("$FarmD/cookbook/breakpage2.php");

This will result in automatic inclusion of breakpage markers after the specified number of posts, of form (:comment breakpage:). The breakpage2.php script will display the page one section at a time, with links to the other sections. Disabling page breaks will result in the page being as normal, the break page markers not visible, since they are treated as comments.

Using blocklist

If you want to use a blocklist script you need to use blocklist2.phpΔ, and include it before including commentboxplus.php.

Using CMS Bundle

If you use cmsb-cmsmode.php from CMSBundle you need to add the 'comment' action to the array of allowed actions. Put in config.php:

$CMSAllowedActions = array(
     'browse','print','search','edit','login','rss','atom','addlink','comment');

Deleting comments

Since 2011-12-03 version, the script takes care of preventing duplicate numbers, it checks all anchors and adds one to the highest found value for the new message. This means you can now delete any comments you want without having to number manually the left ones.

Using the Journal or Diary feature

With this recipe installed, any time a page is created with a name that ends in "Journal" or "Diary", that page will automatically be set up with an entry-form for input of posts, and will display all previous posts to the page, below (or above, if you use the alternative configuration) the new post entry-form.

  • An issue: This automatic configuration of the page will not be apparent when the page is first created, if the user first visits the page by following the normal wiki process of 1) creating a link to a non-existent page, and then 2) following that link. Pursuing a link to a non-existent page named SomethingJournal typically takes the user to SomethingJournal?action=edit (i.e. to see the page in "Edit Mode"). From this vantage point, the automatic configuration provided by the commentboxplus recipe is not in evidence. Naive users will not expect the behaviour that will follow upon saving the new page.
  • A solution: Demonstrate the Journal/Diary capability of the wiki by using (or instructing users to create) a link of the form [[Something Journal(?action=browse)]] OR [[SomethingJournal?action=browse | Amazing Journal]]. By following the link to the new page in "View Mode", the user will see the entry-form for posts, and understand how the page should function.

Adding more fields to the form for website, icq etc.

You can take it as a guide for further field additions:
Basicaly you need to add an input field to the form, which in the script is in html under the variable SDV($CommentBoxFmt.., you need to use html for that, or create the whole form using input markup on the wiki page. The text a user enters gets associated with the name of the input element, in this case 'website', which then gets processed in the function HandleMessagePost, which creates the entry. $website = @$_POST['website']; associates the posted value of the field with a variable $website, and that gets used further on. I added the website entry to the comment header, which is in the variables $MPDateFmt and $MPDateTimeFmt. The entry is build up using wiki markup. Hope this helps anyone wanting to add more fields a little.

Preventing site visitors from posting directives

Set in config.php:

$EnablePreventdirectives = true;

This will prevent posting of directives of the form starting (:..

Posting comments to other pages

Posting to another page allows you to have commentbox forms on content pages, and comments posted to separate comments pages.
Using the syntax (:commentbox Group.CommentPageName:) or (:commentboxchrono Group.CommentPageName:) comments posted will be written to Group.CommentPageName.

This behaviour needs to be enabled by either of the following:

  1. $EnablePostToAnyPage = true; comments may be posted to any page in the wiki.
  2. $EnablePostToGroupPage = true; comments may be posted to any page in the current group.
  3. Comment pages needs to be specified in $PostToCommentPageFmt array. Default:
SDVA($PostToCommentPageFmt, array(
    '{$Group}.{$Name}-Comments', // matches pages in current group with PageName-Comments 
    '{$Group}.{$Name}-Discuss',  // matches page in current group  with PageName-Discuss
    'Comments.{$Group}-{$Name}')), // matches pages in Comments group with name Group-PageName
    ;

Example (for pmwiki 2.2.0-beta only):

(:commentbox {$FullName}-Comments:)

(:include {$FullName}-Comments:)

Comments created with the commentbox will appear on the same page, but are stored on PageName-Comments. The user stays on the content page. Postings will appear on RecentChanges pages.

Setting permissions for comment pages:

  • $EnablePostToAnyPage = true; allows posting to any wiki page. This could be dangerous on an open wiki. Please read security note in the script.
  • $EnablePostToGroupPage = true; allows posting to any page in the current group. This is less dangerous as pages in admin groups like Site are protected from having comments written into them.
  • $PostToCommentPageFmt = array('','',''); disallow any comment pages other than the current page.
  • $PostToCommentPageFmt[] = 'Group.CommentPageName'; add individual comment page to be posted to.
  • $PostToCommentPageFmt[] = 'Cookbook.CommentBoxPlus-Talk'; add comment pages in current group with name PageName-Talk.
Redirection:
  • EnableRedirectToCommentPage = true; will redirect a user after posting a comment to the comment page (if a separate page is used). The default is no redirection, and the user will stay on the page with the comment form.

Localization

Here, all the strings you need to translate for a localization. Put these into your favorite XLPage. See PmWiki:Internationalizations for more information about how to edit or load a XLPage.

Exemple of french localization:

  
  #--COMMENTBOXPLUS--------------------------------------------------------------------
  # Feedback messages
  'Please enter a comment to post' => 'Veuillez saisir un commentaire à poster',
  'Please enter your name as author' => "Veuillez saisir un nom d'auteur",
  'Please enter the code number' => "Veuillez saisir le code d'accés",

  #Summaries
  'Entry added' => 'Nouvelle entrée ajoutée',
  'Comment added' => 'Commentaire ajouté',

  #UI
  'New Entry' => 'Nouvelle entrée',
  'Add Comment' => 'Ajouter un commentaire',
  'Sign as Author' => "Nom d'auteur",
  'Website' => 'Site internet',
  'Enter code' => 'Entrer le code',
  'Post' => 'Poster',
  'Reset' => 'Effacer',
  

Date and Time Formats

Customizing how creation time appears on each comment.
See BasicVariables#TimeFmt
and strftime

See Also

Contributors

  • jr - original comment box
  • HansB - modifications
  • sts -
  • JasonLeBrun - a small addition that will show the date of the last posted comment. Useful for Blog summary pages.
  • AntonyTemplier - enhance moderation capacity and translation

commentboxplus3.php version with gravatar support

Comments see CommentBoxPlus-Talk

User notes +7: If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.