Voici un petit plugin permettant de générer automatiquement de nouvelles chaînes de caractère pour le captcha dans la base de données. On peut l’utiliser par exemple lors de l’évènement système OnManagerLogin.
J’y ai rajouté 4 paramètres de configuration : le nombre de mots, le nombre maximal de caractères du mot, le nombre minimal, et si on doit tout de même effectuer cette action dans le cas où use_captcha est désactivé.
/********** PARAMETERS ********************/
$wordsCount = 10; // Number of words to create
$MaxcharLenght = 6; // Maximal of chars
$MincharLenght = 4; // Minimal number of chars
$overRideUseCaptcha = false; // If use_captcha is set to 0, must we change captchas
if ( ($modx->config['use_captcha'] == 1) || $overRideUseCaptcha)
{
/********* IMPLEMENTATION ***************/
$strList ="azertyupqsdfghjkmwxcvbnAZERTYUPMLKJHGFDSQWXCVBN23456789";
srand((double)microtime()*1000000);
$captchaArray = array();
for($n = 0; $n < $wordsCount; $n++)
{
$stringReturn = "";
$charLenght = mt_rand($MincharLenght,$MaxcharLenght );
for($i=0; $i<$charLenght; $i++) {
$stringReturn .= $strList[rand()%strlen($strList)];
}
array_push($captchaArray,$stringReturn);
}
$goodStr = implode(',',$captchaArray);
$tableName = $modx->getFullTableName("system_settings");
$fields = array(" setting_value" => $goodStr);
$rowUpdate = $modx->db->update($fields, $tableName, "setting_name = 'captcha_words' ");
}
++