Guidelines

This site is for tech Q&A. Please keep your posts focused on the subject at hand.

Ask one question at a time. Don't conflate multiple problems into a single question.

Make sure to include all relevant information in your posts. Try to avoid linking to external sites.

Links to documentation are fine, but in addition you should also quote the relevant parts in your posts.

0 votes
8.4k views
8.4k views

I want to allow anonymous posts in my Question2Answer forum. This can be configured under Admin → Permissions by giving the "Asking questions" and "Answering questions" privileges to "Anybody."

However, I also want the terms of service for my forum to apply to anonymous posts as well, meaning that an anonymous user would be required to accept those terms when posting a question or answer. The available options I've seen so far don't really do what I want:

  • Enable a "Terms & Conditions" checkbox on the registration form (under Admin → Users): this only applies to user registration, not to posting questions or answers.
  • Enable custom messages on the ask and answer forms (under Admin → Posting): this shows the message every time the form is displayed, not just for anonymous users.

I realized that with a captcha enabled there is a specific message displayed only when posting a question or answer as an anonymous user, so I guess my best option is to patch that message.

in Sysadmin
by (115)
2 18 33
edit history

Please log in or register to answer this question.

1 Answer

0 votes
 

A quick search through the Question2Answer code showed that the message was defined via this line in qa-include/app/captcha.php:

$notehtml = qa_insert_login_links(qa_lang_html('misc/captcha_login_fix'));

so the simplest way would've been to just patch the additional message text into that line:

$notehtml = "My custom message. " . qa_insert_login_links(qa_lang_html('misc/captcha_login_fix'));

However, since I defined my terms and conditions in a custom page (Admin → Pages) and I didn't want to hard-code the link to that page, I decided to dig a little deeper.

The function qa_lang_html() is looking up message identifiers and returns them as HTML fragments. The message identified by the string "misc/captcha_login_fix" comes from a map data structure defined in the file qa-include/lang/qa-lang-misc.php.

'captcha_login_fix' => 'To avoid this verification in future, please ^1log in^2 or ^3register^4.',

The function qa_insert_login_links() then transforms the text between the markers ^1/^2 and ^3/^4 respectively into links.

In order to get my desired result, I added another lookup term "captcha_terms_fix" to qa-lang-misc.php and extended qa_insert_login_links() (in the file qa-include/app/format.php) to replace my additional markers ^7/^8 with the correct link.

function qa_insert_login_links($htmlmessage, $topage = null, $params = null)
{
  require_once QA_INCLUDE_DIR . 'app/users.php';

  $userlinks = qa_get_login_links(qa_path_to_root(), isset($topage) ? qa_path($topage, $params, '') : null);

  return strtr(
    $htmlmessage,

    array(
      '^1' => empty($userlinks['login']) ? '' : '<a href="' . qa_html($userlinks['login']) . '">',
      '^2' => empty($userlinks['login']) ? '' : '</a>',
      '^3' => empty($userlinks['register']) ? '' : '<a href="' . qa_html($userlinks['register']) . '">',
      '^4' => empty($userlinks['register']) ? '' : '</a>',
      '^5' => empty($userlinks['confirm']) ? '' : '<a href="' . qa_html($userlinks['confirm']) . '">',
      '^6' => empty($userlinks['confirm']) ? '' : '</a>',
      '^7' => empty($userlinks['terms']) ? '' : '<a href="' . qa_html($userlinks['terms']) . '">',
      '^8' => empty($userlinks['terms']) ? '' : '</a>',
    )    
  );
}

For that to work I had to also patch the function qa_get_login_links() in qa-include/app/user.php to produce the correct page reference.

function qa_get_login_links($rooturl, $tourl)
{
  return array(
    'login' => qa_path('login', isset($tourl) ? array('to' => $tourl) : null, $rooturl),
    'register' => qa_path('register', isset($tourl) ? array('to' => $tourl) : null, $rooturl),
    'confirm' => qa_path('confirm', null, $rooturl),
    'logout' => qa_path('logout', null, $rooturl),
    'terms' => qa_path('terms-and-conditions', null, $rooturl),
  );   
}

And because my terms and conditions are defined in a custom page I wanted the additional message (and link) only displayed if that page actually exists. So I added a conditional that checks if the page slug is defined in the database, and prepends the message only if it is.

$notehtml = qa_insert_login_links(qa_lang_html('misc/captcha_login_fix'));
if (qa_db_num_rows(qa_db_query_sub('SELECT tags FROM ^pages WHERE tags LIKE "%terms-and-conditions%"')) > 0) {
  $notehtml = qa_insert_login_links(qa_lang_html('misc/captcha_terms_fix')) . '<br/>' . $notehtml;
}

With those modifications in place, my forum now displays an additional message

By submitting this post you agree to our Terms & Conditions.

along with the captcha for anonymous questions and answers.

If you want this too, a patch for Question2Answer 1.8.4 can be downloaded here.

by (115)
2 18 33
edit history
...