--=REKLAMA=--

Security Images/Security Images we własnych projektach dla Joomla 1.5

Z Joomla!WikiPL

< We własnych projektach dla Joomla 1.0

Jako przykładem posłużymy się działem kontaktów w Joomla 1.5.

Zawsze zalecanym rozwiązaniem jest, by włączanie i wyłączanie Security Images w komponencie odbywało się w panelu kontrolnym administratora.

W tym celu wystarczy dodać w pliku administrator/components/com_contact/contact_items.xml poniższy kod:

<param name="useSecurityImages" type="radio" default="1" label="Use SecurityImage Captcha" description="Enable Captcha verification">
            <option value="0">No</option>
            <option value="1">Yes</option>
</param>

Warto również uzupełnić pliki językowe, np. plik /administrator/language/en-GB/en-GB.com_contact.ini o następujące linie

USE SECURITYIMAGE CAPTCHA=Use SecurityImage Captcha
ENABLE CAPTCHA VERIFICATION=Enable Captcha verification

Natomiast w pliku /administrator/language/en-GB/pl-PL.com_contact.ini umieszczamy tłumaczenie:

USE SECURITYIMAGE CAPTCHA=Użyć Captcha z SecurityImage
ENABLE CAPTCHA VERIFICATION=Włącz weryfikację Captcha

Joomla! odczytuje plik xml i tworzy "w locie" graficzny interfejs użytkownika służący w tym przypadku konfiguracji komponentu Kontakty. Plik:Contact settings securityimages 5 0.png

Ponieważ Joomla 1.5 korzysta z paradygmatu Model - Widok - Kontroler, musimy dokonać zmiany w kontrolerze, dodać nowe zadanie displaySecurityImagesCaptcha()w pliku components/com_contact/controller.php:

/**
* Add by www.waltercedric.com
*/
function displaySecurityImagesCaptcha() { 
   global $mainframe; 
 
   //Per contact you can define if the user has to resolve the capctha 
   $contactId = JRequest::getVar('contact_id', 0, '', 'int'); 
   // load the contact details 
   $model    = &$this->getModel('contact'); 
   $qOptions['id'] = $contactId; 
   $contact        = $model->getContact( $qOptions ); 
   $params = new JParameter( $contact->params ); 
 
   if ($params->get('useSecurityImages')) {     
      $check = null; 
      $mainframe->triggerEvent('onSecurityImagesDisplay', array($check)); 
      if (!$check) { 
         echo "<br/>Erreur affichage du Captcha<br/>"; 
      } 
   } 
}

As you can see, the event "onSecurityImagesDisplay" is triggered on a per contact name basis. That mean that some contact can have a Captcha while other have not.

Jak widzisz, zdarzenie "onSecurityImagesDisplay" jest wywoływane z podstawową nazwą kontaktu. Oznacza to, że jeden kontakt może mieć Captcha, podczas gdy inny nie ma.

The next step is to add the task checkSecurityImagesCaptcha() checking the captcha in the components/com_contact/controller.php

W następnym kroku dodajemy zadanie checkSecurityImagesCaptcha() sprawdzające Captcha w pliku components/com_contact/controller.php

/**
* Add by www.waltercedric.com
*/
function checkSecurityImagesCaptcha() { 
   global $mainframe; 
 
   $contactId = JRequest::getVar('id', 0, '', 'int'); 
   // load the contact details 
   $model    = &$this->getModel('contact'); 
   $qOptions['id'] = $contactId; 
   $contact        = $model->getContact( $qOptions ); 
   $params = new JParameter( $contact->params ); 
 
   //check if that user has a capctha 
   if (!$params->get('useSecurityImages')) {  
      return true; 
   } 
   $return = false; 
   $securityImagesJoomlaContactUserTry = JRequest::getVar('securityImagesJoomlaContactUserTry', false, '', 'CMD'); 
   $mainframe->triggerEvent('onSecurityImagesCheck', array($securityImagesJoomlaContactUserTry, &$return));
   return $return;
}

One more step is to alter the original submit() method of the controller in components/com_contact/controller.php

W kolejnym kroku zastąpimy oryginalną metodę submit() kontrolera w pliku components/com_contact/controller.php

        global $mainframe;    
 
        if (!$this->checkSecurityImagesCaptcha()) {
            JError::raiseWarning("999","Invalid Captcha Code");
            $this->display();
            return false;
        }

W oryginale mamy takie uzupełnienie

   $useSecurityImagesInContact = false;
      if (file_exists(JPATH_SITE.DS."administrator".DS."components".DS."com_securityimages".DS."config.securityimages.php")) {
         include(JPATH_SITE.DS."administrator".DS."components".DS."com_securityimages".DS."config.securityimages.php");
         $useSecurityImagesInContact = $securityImagesInContact;
      } 
 
      if ($useSecurityImagesInContact && !$this->checkSecurityImagesCaptcha()) {
         $this->setError(JText::_( 'SECURITYIMAGES REJECT USER ENTRY'));
         $this->display();
         return false;
      }



And finally altering the view /com_contact/views/contact/tmpl/default_form.php to display the Captcha field

Na koniec zmieniamy widok w pliku /com_contact/views/contact/tmpl/default_form.php, aby wyświetlić pole Captcha

<?php if ($this->params->get('useSecurityImages')) { ?>             
<img src="/index.php?option=com_contact&task=displaySecurityImagesCaptcha&contact_id=<?php echo $this->contact->id; ?>"> 
<br /> 
<input type="text" name="securityImagesJoomlaContactUserTry" /> 
<br /> 
 <?php } ?>

To jest oryginalny kod w łatce:

<!-- add by www.waltercedric.com to protect the form -->
   <?php 
   $useSecurityImagesInContact = false;
   if (file_exists(JPATH_SITE.DS."administrator".DS."components".DS."com_securityimages".DS."config.securityimages.php")) {
      include(JPATH_SITE.DS."administrator".DS."components".DS."com_securityimages".DS."config.securityimages.php");
      $useSecurityImagesInContact = $securityImagesInContact;
   } 
 
   if ($this->params->get('useSecurityImages') && $useSecurityImagesInContact) { ?> 
      <script type="text/javascript" src="<?php echo JURI :: root() ?>components/com_securityimages/js/securityImages.js"></script>
      <label for="contact_securityimages">
         &nbsp;<?php //echo JText::_( 'Message subject' );?>
         <?php echo JText::_('SECURITYIMAGES LABEL') ?>
      </label>
      <br />
      <input type="text" name="securityImagesJoomlaContactUserTry" />&nbsp;&nbsp;   
      <img id='securityImagesContactCaptcha' name='securityImagesContactCaptcha' align="middle" 
           src="<?php echo JURI :: root() ?>index.php?option=com_contact&task=displaySecurityImagesCaptcha&contact_id=<?php echo $this->contact->id; ?>"/> 
      <a href="javascript:askNewSecurityImages('securityImagesContactCaptcha');">
         <img alt='reload.gif' src="<?php echo JURI :: root() ?>components/com_securityimages/buttons/reload.gif" 
              id='securityImagesContactCaptchaReload' name='securityImagesContactCaptchaReload' border="0" />
      </a> 
      <br /> 
   <?php } ?>

As you see a lot of thing have been done, and I am still testing and improving the code.

Jak widzisz, wiele rzeczy zostało już zrobione, nadal testuję i ulepszam kod.

Uwagi końcowe

  • Komponent Securityimages może być udostępniany w trybie diagnostycznym i dezaktywowany.
  • Zalecane jest umieszczenie w kodzie przełącznika, umożliwiającego włączanie lub wyłączanie funkcji antyspamowych w przypadku, gdy komponent nie jest jeszcze zainstalowany.


Miłej zabawy…


Dziękujemy za wkład

» Stefan Wajda [zwiastun],