Omeka에서 Google Recaptcha v2 라이브러리 적용 방법


오메카에서는 회원가입이나 댓글 입력시 외부 스팸 로봇의 자동 등록을 막는 도구인 reCaptcha 사용이 가능합니다.
그런 reCaptcha가 구글에 인수된 이후로는 새로운 형태의 리캡챠 기능이 제공되고 있습니다.
하지만 오메카의 기존 코어부 파일들과 플러그인으로는 구 형태인 reCaptcha v1만 사용 가능합니다.
참고로 리캡챠 버젼 1은 아래와 같은 디자인과 기능이 제공됩니다.

# reCAPTCHA v1
image
리캡챠 버젼 1은 꽤 오랜 기간 사용되었으나 타이핑을 직접 해야 한다거나 글자가 잘 보이지 않아서 몇 번 실패하고 다시 입력한 경험이 누구나 한 번 쯤은 있을 것입니다. 이 버젼은 특히 요즘 같은 모바일 환경에선 불편한 점이 많습니다.
하지만 구글이 리캡챠 인수 후 버젼 2가 나오면서 부터는 디자인 측면도 좋아지고 기능적으로도 편리하게 제공되고 있습니다.

아래의 캡쳐 화면은 리캡챠 버젼 2의 인터페이스입니다.
로봇이 아닙니다의 체크 박스를 클릭하면 자동으로 사진 블록이 뜨고 이 중 리캡챠가 지시한 항목과 맞는 블록을 선택하면 인증 처리가 됩니다. 방식은 이외에도 다른 모드도 있지만 가장 일반적으로 많이 사용되는 방식이 블록 맞추기입니다.

# reCAPTCHA v2
image


오메카에서 리캡챠 버젼 2의 인터페이스 사용을 원하는 경우엔 아래의 방법을 사용해 보시기 바랍니다.

# 오메카에서 수정 필요한 소스코드 목록
+ 코어부
- 환경설정 파일 관련 :  /application/config/application.ini
- controller 관련 : /application/controllers/SettingsController.php
- form 관련 : /application/forms/SecuritySettings.php
- libraries의 omeka Captcha 관련 : /application/libraries/Omeka/Captcha.php
- libraries의 omeka Form 관련 : /application/libraries/Omeka/Form.php

+ 플러그인
- 회원가입시 리캡챠 설정 부분 관련 :  /plugins/GuestUser/controllers/UserController.php
- 댓글 입력시 리캡챠 적용 부분 관련 :  /plugins/Commenting/CommentForm.php




+ 오메카 코어부 수정 상세 내용

- 환경설정 파일 관련 :  /application/config/application.ini

; ReCaptcha2 사용을 위한 추가 코드(20171004)
; ZF1 - Google ReCaptcha v2 project : Zend Framework 1에서 ReCaptcha v2의 사용을 위함. 디폴트는 ReCaptcha 1을 지원.
; 추가 코드의 사용을 위해 Ghost 디렉토리를  application > libraries 디렉토리 하위에 복사해 둘 것!!!
; | Include path and autoloader |
includePaths.library = BASE_PATH "/library"
autoloaderNamespaces[] = "Ghost"



- controller 관련 : /application/controllers/SettingsController.php
     // 리캡챠 v1용 디폴트값
     //Omeka_Captcha::PUBLIC_KEY_OPTION,
     //Omeka_Captcha::PRIVATE_KEY_OPTION     
     // 리캡챠 v2용 수정값
     Omeka_Captcha::PUBLIC_KEY_OPTION, 
     Omeka_Captcha::PRIVATE_KEY_OPTION, 
     Omeka_Captcha::VERSION_OPTION 


- form 관련 : /application/forms/SecuritySettings.php  
  // 리캡챠 v2용 추가 코드
        $this->addElement('select', Omeka_Captcha::VERSION_OPTION, 
             array( 
                 'label' => __('ReCaptcha Version'), 
                 'description' => __('Choose which ReCaptcha version you\'re using. Note that ReCaptcha v1 is deprecated and Public and Private Key from ReCaptcha v1 doesn\'t work with ReCaptcha v2.'), 
                 'value' => get_option(Omeka_Captcha::VERSION_OPTION) ?: '', 
                 'multiOptions' => array('' => 'ReCaptcha v1', 'v2' => 'ReCaptcha v2') 
             ) 
         ); 


        $this->addDisplayGroup(
            array(
                Omeka_Captcha::PUBLIC_KEY_OPTION,
                Omeka_Captcha::PRIVATE_KEY_OPTION,
    // 리캡챠 v2용 추가 코드
    Omeka_Captcha::VERSION_OPTION,
            ),
            'captcha', array('legend' => __('Captcha'))
        );



- libraries의 omeka Captcha 관련 : /application/libraries/Omeka/Captcha.php
/**
 * Factory for creating a captcha for use when soliciting public input.
 *
 * @package Omeka\Captcha
 */
class Omeka_Captcha {
    const PUBLIC_KEY_OPTION = 'recaptcha_public_key';
    const PRIVATE_KEY_OPTION = 'recaptcha_private_key';
 // 리캡챠 v2용 추가 코드
 const VERSION_OPTION = 'recaptcha_version';
   
    /**
     * Get a captcha object implementing Zend's captcha API.
     *
     * //@internal Currently returns a Zend_Captcha_ReCaptcha object.
     * @internal Currently returns a Zend_Captcha_ReCaptcha or Ghost_Captcha_Recaptcha2 object.
     *
     * @return Zend_Captcha_Adapter|null
     */
  // 리캡챠 v1 방식
 //static function getCaptcha()
    //{
        //$publicKey = get_option(self::PUBLIC_KEY_OPTION);
        //$privateKey = get_option(self::PRIVATE_KEY_OPTION);
        //if (empty($publicKey) || empty($privateKey)) {
           //return null;
        //}
        //$ssl = false;
        //if ($request = Zend_Controller_Front::getInstance()->getRequest()) {
            //$ssl = $request->isSecure();
        //}
        //$captcha = new Zend_Captcha_ReCaptcha(array(
            //'pubKey' => $publicKey,
            //'privKey' => $privateKey,
            //'ssl' => $ssl));
        //return $captcha;
    //} 
 
  // 리캡챠 v2 방식 
    static function getCaptcha()
    {
        $publicKey = get_option(self::PUBLIC_KEY_OPTION);
        $privateKey = get_option(self::PRIVATE_KEY_OPTION);
        if (empty($publicKey) || empty($privateKey)) {
           return null;
        }
  //삭제 코드
        //$ssl = false;
        //if ($request = Zend_Controller_Front::getInstance()->getRequest()) {
            //$ssl = $request->isSecure();
        //} 
  
  //추가 코드
        $version = get_option(self::VERSION_OPTION); 
 
        switch ($version) { 
            case 'v2': 
                $captcha = new Ghost_Captcha_ReCaptcha2(array( 
                    'pubKey' => $publicKey, 
                    'privKey' => $privateKey, 
                )); 
    break; 
  //삭제 코드
        //$captcha = new Zend_Captcha_ReCaptcha(array(
            //'pubKey' => $publicKey,
            //'privKey' => $privateKey,
            //'ssl' => $ssl));
   
            default: 
                // old, deprecated ReCaptcha v1 shipped with ZF 
                $ssl = false; 
                if ($request = Zend_Controller_Front::getInstance()->getRequest()) { 
                    $ssl = $request->isSecure(); 
                } 
 
                $captcha = new Zend_Captcha_ReCaptcha(array( 
                    'pubKey' => $publicKey, 
                    'privKey' => $privateKey, 
                    'ssl' => $ssl)); 
                break; 
        }     
        return $captcha;
    }




- libraries의 omeka Form 관련 : /application/libraries/Omeka/Form.php
        // 리캡챠 v2용 추가 코드
        // add paths for reCAPTCHA v2 
        $this->addElementPrefixPath('Ghost_', 'Ghost/'); 
        $this->addPrefixPath('Ghost_Form_Decorator', 'Ghost/Form/Decorator', self::DECORATOR); 





+ 플러그인 수정

- 회원가입시 리캡챠 설정 부분 관련 :  /plugins/GuestUser/controllers/UserController.php
        if(Omeka_Captcha::isConfigured() && (get_option('guest_user_recaptcha') == 1)) {
            $form->addElement('captcha', 'captcha',  array(
                'class' => 'hidden',
                'style' => 'display: none;',
                'label' => __("로봇자동가입방지용 식별문자입력"),
                'type' => 'hidden',
    
    // ReCaptcha v1 방식 사용시 아래 코드 사용할 것
                //'captcha' => Omeka_Captcha::getCaptcha()
    
    // ReCaptcha v2 방식으로 변경(20171005)
    'captcha' => array(
     // 신규 Recaptcha 레이아웃용(v2)
     'captcha' => 'ReCaptcha2',
     'hl' => 'ko',  //디폴트 값은 en. 영어가 아닌 한국어로 출력하려면 ko 사용
     'theme' => 'light', // see options below 
     'pubkey' => get_option('recaptcha_public_key'),
     'privkey' => get_option('recaptcha_private_key'),
     'ssl' => true  
                )
            ));
        }


- 댓글 입력시 리캡챠 적용 부분 관련 :  /plugins/Commenting/CommentForm.php
        //assume registered users are trusted and don't make them play recaptcha
        if(!$user && get_option('recaptcha_public_key') && get_option('recaptcha_private_key')) {
            $this->addElement('captcha', 'captcha',  array(
                'label' => __("로봇자동가입방지용 식별문자입력"),
    
    // ReCaptcha v1 방식 사용시 아래 코드 사용할 것
                //'captcha' => array(
                    //'captcha' => 'ReCaptcha',
                    //'pubkey' => get_option('recaptcha_public_key'),
                    //'privkey' => get_option('recaptcha_private_key'),
                    //'ssl' => true //make the connection secure so IE8 doesn't complain. if works, should branch around http: vs https:
                //)   
    
    // ReCaptcha v2 방식으로 변경(20171005)    
    'captcha' => array(
     'captcha' => 'ReCaptcha2',
     'hl' => 'ko', // 디폴트 값은 en. 영어가 아닌 한국어로 출력하려면 ko 사용
     'theme' => 'light', // see options below 
     'pubkey' => get_option('recaptcha_public_key'),
     'privkey' => get_option('recaptcha_private_key'),
     'ssl' => true  
                )   
    
            ));
            $this->getElement('captcha')->removeDecorator('ViewHelper');
        }



* 오메카 리캡챠 버젼2 적용 관련 레퍼런스
https://github.com/luku/Omeka/tree/d398a27057e5663e1813be10fbfb8baa46face9f/application/libraries/Ghost

* 리캡챠 소개 및 관련 기사(출처 : 블로터닷넷)
http://www.bloter.net/archives/273960


Sign In or Register to comment.