Skip to content

Commit

Permalink
support for different natural languages (Dutch for now),
Browse files Browse the repository at this point in the history
they get different captchas and different labels in the comment ui.
  • Loading branch information
twanvl committed Mar 5, 2014
1 parent c55b69b commit 33e5ee4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 24 deletions.
35 changes: 21 additions & 14 deletions lib/Captcha.php
Expand Up @@ -13,38 +13,45 @@ class Captcha {
public $answer; // hashed answer

// construct a new captcha, or get one from the request variables
function __construct() {
function __construct($type = 'en') {
if (Captcha::is_answered()) {
$this->question = $_REQUEST['captcha_question'];
$this->answer = $_REQUEST['captcha_answer'];
} else {
$this->make_question();
$this->make_question($type);
$this->answer = '';
foreach ($this->answers as $a) {
$this->answer .= Captcha::sign($a) . ',';
}
}
}

private function make_question() {
if (false) {
private function make_question($type) {
if ($type == 'math') {
// a math question
$i = rand()%15;
$j = rand()%15;
$this->question = "$i + $j = ";
//$this->question = format_number($i) . " + " . format_number($j) . " = ";
$this->answers = array($i+$j);
} else {
$qa = array(
array('q'=>'What greek letter is usually used for anonymous functions?'
,'a'=>array('lambda','lamda','λ')),
array('q'=>'Name of the lazy functional programming language I write about:'
,'a'=>array('haskell','haskel','agda','coq','ml','ocaml')),
array('q'=>'Name a function of type <tt>(a -> b) -> ([a] -> [b])</tt>:'
,'a'=>array('map','fmap','<$>','(<$>)')),
array('q'=>'Name a function of type <tt>[[a]] -> [a]</tt>:'
,'a'=>array('concat','join','msum','mconcat')),
);
if ($type == 'nl') {
$qa = array(
array('q'=>'Noem een studentenklimvereniging in Nijmegen'
,'a'=>array('nijsac')),
);
} else {
$qa = array(
array('q'=>'What greek letter is usually used for anonymous functions?'
,'a'=>array('lambda','lamda','λ')),
array('q'=>'Name of the lazy functional programming language I write about:'
,'a'=>array('haskell','haskel','agda','coq','ml','ocaml')),
array('q'=>'Name a function of type <tt>(a -> b) -> ([a] -> [b])</tt>:'
,'a'=>array('map','fmap','<$>','(<$>)')),
array('q'=>'Name a function of type <tt>[[a]] -> [a]</tt>:'
,'a'=>array('concat','join','msum','mconcat')),
);
}
$i = rand()%count($qa);
$this->question = $qa[$i]['q'];
$this->answers = $qa[$i]['a'];
Expand Down
51 changes: 41 additions & 10 deletions lib/HtmlTemplate.php
Expand Up @@ -110,12 +110,43 @@ static function write_comments($page) {

static function write_comments_wb($page) {
$comments = Comments::get_all($page->url);
// language
if ($page->natural_language == 'nl') {
$lang = array(
'Comment'=>'Reageren',
'Reply'=>'Reageren',
'Comments'=>'Reacties',
'Name'=>'Naam',
'Homepage'=>'Homepage',
'Email'=>'E-mail',
'optional'=>'optioneel',
'emailnote'=>'optioneel, blijft geheim',
'Subscribe'=>'Houd mij op de hoogte van reacties',
'Human'=>'CAPTCHA',
'codenote'=>'',
);
} else {
$lang = array(
'Comment'=>'Comment',
'Comments'=>'Comments',
'Reply'=>'Reply',
'Name'=>'Name',
'Homepage'=>'Homepage',
'Email'=>'Email',
'optional'=>'optional',
'emailnote'=>'optional, will not be revealed',
'Subscribe'=>'Subscribe to comments on this article',
'Human'=>'Human?',
'codenote'=>'Use <tt>&gt; code</tt> for code blocks, <tt>@code@</tt> for inline code. Some html is also allowed.',
);
}
// comments box
echo "<div id='comments'>\n";
echo " <div id='comments-body'><a name='comments'></a>\n";
if (empty($comments)) {
echo " <h2>Comment</h2>\n";
echo " <h2>{$lang['Comment']}</h2>\n";
} else {
echo " <h2>Comments</h2>\n";
echo " <h2>{$lang['Comments']}</h2>\n";
foreach ($comments as $c) {
echo "<div class='post-comment' id='comment-".htmlspecialchars($c->id)."'>\n";
echo "<a name='comment-".htmlspecialchars($c->id)."'></a>\n";
Expand All @@ -141,26 +172,26 @@ static function write_comments_wb($page) {
echo $c->body_html();
echo "</div>";
}
echo " <h2>Reply</h2>\n";
echo " <h2>$lang[Reply]</h2>\n";
}
echo " <form method='post' action='add-comment/$page->url#new-comment'><a name='new-comment'></a>\n";
echo " <input type='hidden' name='url' value='",htmlspecialchars($page->url),"'>\n";
echo " <table>";
global $invalid_fields;
echo " <tr><td><label for='author_name' >Name</label ><td><input type='text' name='author_name' id='author_name' ".(isset($invalid_fields['author_name']) ?'class="invalid" ':'')."value='",request_var('author_name'),"'>\n";
echo " <tr><td><label for='author_url' >Homepage</label><td><input type='text' name='author_url' id='author_url' ".(isset($invalid_fields['author_url']) ?'class="invalid" ':'')."value='",request_var('author_url'),"'> <span class='help'>(optional)</span>\n";
echo " <tr><td><label for='author_email'>Email</label ><td><input type='email' name='author_email' id='author_email' ".(isset($invalid_fields['author_email'])?'class="invalid" ':'')."value='",request_var('author_email'),"'> <span class='help'>(optional, will not be revealed)</span>\n";
echo " <tr><td><td><label><input type='checkbox' name='author_subscribe' ",request_var_check('author_subscribe'),"> Subscribe to comments on this article</label>\n";
echo " <tr><td><label for='author_name' >$lang[Name]</label ><td><input type='text' name='author_name' id='author_name' ".(isset($invalid_fields['author_name']) ?'class="invalid" ':'')."value='",request_var('author_name'),"'>\n";
echo " <tr><td><label for='author_url' >$lang[Homepage]</label><td><input type='text' name='author_url' id='author_url' ".(isset($invalid_fields['author_url']) ?'class="invalid" ':'')."value='",request_var('author_url'),"'> <span class='help'>($lang[optional])</span>\n";
echo " <tr><td><label for='author_email'>$lang[Email]</label ><td><input type='email' name='author_email' id='author_email' ".(isset($invalid_fields['author_email'])?'class="invalid" ':'')."value='",request_var('author_email'),"'> <span class='help'>($lang[emailnote])</span>\n";
echo " <tr><td><td><label><input type='checkbox' name='author_subscribe' ",request_var_check('author_subscribe'),"> $lang[Subscribe]</label>\n";
// generate captcha
$captcha = new Captcha();
echo " <tr><td><label for='captcha'>Human?</label><td>";
$captcha = new Captcha($page->natural_language);
echo " <tr><td><label for='captcha'>$lang[Human]</label><td>";
echo "<input type='hidden' name='captcha_answer' value='".htmlspecialchars($captcha->answer)."'>";
echo "<input type='hidden' name='captcha_question' value='".htmlspecialchars($captcha->question)."'>";
echo "$captcha->question <input type='text' name='captcha' id='captcha' ".(isset($invalid_fields['captcha'])?'class="invalid" ':'')."value='",request_var('captcha'),"'>\n";
echo " </table>";
echo " <textarea rows='6' name='body'".(isset($invalid_fields['body'])?'class="invalid" ':'').">",htmlspecialchars(@$_REQUEST['body']),"</textarea>\n";
echo " <div class='edit-help'>";
echo " Use <tt>&gt; code</tt> for code blocks, <tt>@code@</tt> for inline code. Some html is also allowed.";
echo " $lang[codenote]";
echo " </div>";
echo " <input type='submit' value='Submit'>\n";
if (isset($invalid_fields[''])) {
Expand Down
1 change: 1 addition & 0 deletions lib/Page.php
Expand Up @@ -20,6 +20,7 @@ class Page {
public $last_modified = 0;
public $source_link = '';
public $license_link = DEFAULT_PAGE_LICENSE;
public $natural_language = 'en';
private $loaded = false;

function __construct($url) {
Expand Down
4 changes: 4 additions & 0 deletions lib/TextFilePage.php
Expand Up @@ -55,10 +55,14 @@ function do_load() {
$this->source_link = "<a href='$this->path'>$ma[2]</a>";
} elseif (preg_match('@^(?:show[ _-]?)?(comments):\s*(.*)@',$attr,$ma)) {
$this->show_comments = true;
} elseif (preg_match('@^show[ _-]?date:\s*(.*)@',$attr,$ma)) {
$this->show_date = true;
} elseif (preg_match('@^literate style: latex@',$attr,$ma)) {
$data = unlit_latex($data);
} elseif (preg_match('@^language: (.*)@',$attr,$ma)) {
$language = ($ma[1]);
} elseif (preg_match('@^natural language: (.*)@',$attr,$ma)) {
$this->natural_language = ($ma[1]);
} elseif (preg_match('@^license: (.*)@',$attr,$ma)) {
$license = ($ma[1]);
} else if ($attr != '') {
Expand Down

0 comments on commit 33e5ee4

Please sign in to comment.