Ancora non ho risolto o meglio il codice che ho scritto funziona pero' non mi piace per nulla il metodo che ho usato avete qualche suggerimento?
Vi posto il codice completo.
Questo e' il codice del plugin di tipo captha:
<?php
/**
* @package Joomla.Plugin
* @subpackage Captcha
*
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
jimport('joomla.environment.browser');
/**
* captchareadable Plugin.
* Based on the oficial captchareadable library( http://captchareadable.net/plugins/php/ )
*
* @package Joomla.Plugin
* @subpackage Captcha
* @since 2.5
*/
class plgCaptchacaptchareadable extends JPlugin
{
public function __construct($subject, $config)
{
parent::__construct($subject, $config);
$this->loadLanguage();
}
/**
* Initialise the captcha
*
* @param string $id The id of the field.
*
* @return Boolean True on success, false otherwise
*
* @since 2.5
*/
public function onInit($id)
{
$rand = substr(uniqid('', true), -5);
$stringa = $this->randomCode(5);
$stringa = $rand;
$valf = $this->encrypt_string(strtoupper(substr($stringa, 0, 5)));
$session =& JFactory::getSession();
$session->set('myvar', $valf);
echo '<img src="' . JURI::base() . 'plugins/captcha/captchareadable/' . 'aa.php?f=' . $valf . '" name="imc" id="imc">';
echo '<input type ="text" name="risp" id="risp" maxlength="5">';
return true;
}
/**
* Gets the challenge HTML
*
* @return string The HTML to be embedded in the form.
*
* @since 2.5
*/
public function onDisplay($name, $id, $class)
{
return '<div id="dynamic_captchareadable_1"></div>';
}
/**
* Calls an HTTP POST function to verify if the user's guess was correct
*
* @return True if the answer is correct, false otherwise
*
* @since 2.5
*/
public function onCheckAnswer($code)
{
$response = JRequest::getString('risp', '');
$session =& JFactory::getSession();
if ($response != '' && $response == $this->decrypt_string($session->get('myvar', 'empty'))) {
$session->set('myvar', '');
return true;
} else {
$this->_subject->setError(JText::_('Error'));
return false;
}
}
protected function randomCode($length = 4)
{
$session = JFactory::getSession();
$rand = substr(uniqid('', true), -5);
$session->set('captcod', md5($rand));
return $rand;
}
function encrypt_string($input)
{
$inputlen = strlen($input);
$randkey = rand(1, 9);
$i = 0;
while ($i < $inputlen) {
$inputchr[$i] = (ord($input[$i]) - $randkey);
$i++;
}
$encrypted = implode('.', $inputchr) . '.' . (ord($randkey) + 50);
return $encrypted;
}
function decrypt_string($input)
{
$input_count = strlen($input);
$dec = explode(".", $input);
$x = count($dec);
$y = $x - 1;
$calc = $dec[$y] - 50;
$randkey = chr($calc);
$i = 0;
while ($i < $y) {
$array[$i] = $dec[$i] + $randkey;
$real .= chr($array[$i]);
$i++;
}
$input = $real;
return $input;
}
}
Come vedete a riga 34(con echo '<img src="' . JURI::base() . 'plugins/captcha/captchareadable/' . 'aa.php?f=) chiamo lo script PHP che crea l'immagine(di nome aa.php).
Questo il codice dello script che crea l'immagine
<?php
define('_JEXEC',1);
defined('_JEXEC') or die;
function decrypt_string($input)
{
$input_count = strlen($input);
$dec = explode(".", $input);
$x = count($dec);
$y = $x-1;
$calc = $dec[$y]-50;
$randkey = chr($calc);
$i = 0;
while ($i < $y)
{
$array[$i] = $dec[$i]+$randkey;
$real .= chr($array[$i]);
$i++;
};
$input = $real;
return $input;
}
$risultato = decrypt_string($_GET['f'],"mysecretkey");
$immagine = imagecreatefromjpeg("captcha.jpg");
$testo = imagecolorallocate($immagine, 255, 255, 255);
$white = imagecolorallocate($immagine, 255, 255, 255);
$grey = imagecolorallocate($immagine, 128, 128, 128);
$black = imagecolorallocate($immagine, 0, 0, 0);
$text = $risultato;
$font = 'verdana.ttf';
imagettftext($immagine, 15, 0, 10, 20, $white, $font, $text);
header("Content-type: image/jpeg");
imagejpeg($immagine);
?>
In pratica quando io apro una pagina che deve contenere un capthca viene generato un numero casuale e salvato in una variabile di sessione.
Questo numero casuale viene criptato e viene richiamato come fosse un immagine lo script aa.php passandogli in get il codice criptato, lui(aa.php) lo decripta e restituisce in output appunto un immagine del captha con il codice generato.
Come vedete e' un funzionamento abbastanza semplice ma contorto e per nulla MVC
Avete qualche suggerimento?
grazie
Davide