├── .editorconfig ├── LICENSE ├── README.md ├── config ├── autoload.php └── recaptcha.php ├── example ├── controller │ └── test.php └── views │ └── recaptcha.php ├── libraries └── Recaptcha.php └── spark.info /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | 14 | [*.php] 15 | indent_size = 4 16 | 17 | [Makefile] 18 | indent_style = tab 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, Bo-Yi Wu 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of CodeIgniter-reCAPTCHA nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeIgniter-reCAPTCHA 2 | 3 | The FREE anti-abuse service. Easy to add, advanced security, accessible to wide range of users and platforms. This package is compliant with [PSR-1](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md) and [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md). 4 | 5 | # What is reCAPTCHA? 6 | 7 | reCAPTCHA is a free service that protects your site from spam and abuse. It uses advanced risk analysis engine to tell humans and bots apart. With the new API, a significant number of your valid human users will pass the reCAPTCHA challenge without having to solve a CAPTCHA (See blog for more details). reCAPTCHA comes in the form of a widget that you can easily add to your blog, forum, registration form, etc. 8 | 9 | See [the details][1]. 10 | 11 | # Sign up for an API key pair 12 | 13 | To use reCAPTCHA, you need to [sign up for an API key pair][4] for your site. The key pair consists of a site key and secret. The site key is used to display the widget on your site. The secret authorizes communication between your application backend and the reCAPTCHA server to verify the user's response. The secret needs to be kept safe for security purposes. 14 | 15 | # Installation 16 | 17 | You can install via http://getsparks.org/packages/recaptcha-library/show 18 | 19 | ```bash 20 | $ php tools/spark install -v1.0.2 recaptcha-library 21 | ``` 22 | 23 | or manual install 24 | 25 | ```bash 26 | $ cp config/recaptcha.php your_application/config/ 27 | $ cp libraries/recaptcha.php your_application/libraries/ 28 | ``` 29 | 30 | # Usage 31 | 32 | Set your site key and secret on `config/recaptcha.php` file 33 | 34 | ```php 35 | $config['recaptcha_site_key'] = ''; 36 | $config['recaptcha_secret_key'] = ''; 37 | ``` 38 | 39 | ### Render the reCAPTCHA widget 40 | 41 | ```php 42 | $this->load->library('recaptcha'); 43 | echo $this->recaptcha->getWidget(); 44 | ``` 45 | 46 | output: 47 | 48 | ```html 49 |
50 | ``` 51 | 52 | change default theme by pass array parameter 53 | 54 | ```php 55 | echo $this->recaptcha->getWidget(array('data-theme' => 'dark')); 56 | ``` 57 | 58 | change default type by pass array parameter 59 | 60 | ```php 61 | echo $this->recaptcha->getWidget(array('data-theme' => 'dark', 'data-type' => 'audio')); 62 | ``` 63 | 64 | ### Render Script Tag 65 | 66 | ```php 67 | $this->load->library('recaptcha'); 68 | echo $this->recaptcha->getScriptTag(); 69 | ``` 70 | 71 | output: 72 | 73 | ```html 74 | 75 | ``` 76 | 77 | change render value by pass array parameter 78 | 79 | ```php 80 | echo $this->recaptcha->getScriptTag(array('render' => 'explicit')); 81 | ``` 82 | 83 | change default language by pass array parameter 84 | 85 | ```php 86 | echo $this->recaptcha->getScriptTag(array('render' => 'explicit', 'hl' => 'zh-TW')); 87 | ``` 88 | 89 | ### Verify Response 90 | 91 | Calls the reCAPTCHA siteverify API to verify whether the user passes `g-recaptcha-response` POST parameter. 92 | 93 | ```php 94 | $recaptcha = $this->input->post('g-recaptcha-response'); 95 | $response = $this->recaptcha->verifyResponse($recaptcha); 96 | ``` 97 | 98 | check success or fail 99 | 100 | ```php 101 | if (isset($response['success']) and $response['success'] === true) { 102 | echo "You got it!"; 103 | } 104 | ``` 105 | 106 | see the [example controller](example/controller/test.php) and [view](example/views/recaptcha.php) 107 | 108 | # Author 109 | 110 | Bo-Yi Wu [@appleboy](https://twitter.com/appleboy) 111 | 112 | [1]: https://www.google.com/recaptcha/intro/index.html 113 | [2]: http://www.codeigniter.com/ 114 | [3]: https://developers.google.com/recaptcha/ 115 | [4]: http://www.google.com/recaptcha/admin 116 | -------------------------------------------------------------------------------- /config/autoload.php: -------------------------------------------------------------------------------- 1 | load->spark('recaptcha-library/1.0.1'); 9 | // load from CI library 10 | // $this->load->library('recaptcha'); 11 | 12 | $recaptcha = $this->input->post('g-recaptcha-response'); 13 | if (!empty($recaptcha)) { 14 | $response = $this->recaptcha->verifyResponse($recaptcha); 15 | if (isset($response['success']) and $response['success'] === true) { 16 | echo "You got it!"; 17 | } 18 | } 19 | 20 | $data = array( 21 | 'widget' => $this->recaptcha->getWidget(), 22 | 'script' => $this->recaptcha->getScriptTag(), 23 | ); 24 | $this->load->view('recaptcha', $data); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /example/views/recaptcha.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | reCAPTCHA Example 4 | 5 | 6 |
7 | 8 | 9 |
10 | 11 |
12 | 13 | 14 | -------------------------------------------------------------------------------- /libraries/Recaptcha.php: -------------------------------------------------------------------------------- 1 | 8 | * @link https://github.com/appleboy/CodeIgniter-reCAPTCHA 9 | */ 10 | class Recaptcha 11 | { 12 | /** 13 | * ci instance object 14 | * 15 | */ 16 | private $_ci; 17 | 18 | /** 19 | * reCAPTCHA site up, verify and api url. 20 | * 21 | */ 22 | const sign_up_url = 'https://www.google.com/recaptcha/admin'; 23 | const site_verify_url = 'https://www.google.com/recaptcha/api/siteverify'; 24 | const api_url = 'https://www.google.com/recaptcha/api.js'; 25 | 26 | /** 27 | * constructor 28 | * 29 | * @param string $config 30 | */ 31 | public function __construct() 32 | { 33 | $this->_ci = & get_instance(); 34 | $this->_ci->load->config('recaptcha'); 35 | $this->_siteKey = $this->_ci->config->item('recaptcha_site_key'); 36 | $this->_secretKey = $this->_ci->config->item('recaptcha_secret_key'); 37 | $this->_language = $this->_ci->config->item('recaptcha_lang'); 38 | 39 | if (empty($this->_siteKey) or empty($this->_secretKey)) { 40 | die("To use reCAPTCHA you must get an API key from ".self::sign_up_url.""); 42 | } 43 | } 44 | 45 | /** 46 | * Submits an HTTP GET to a reCAPTCHA server. 47 | * 48 | * @param array $data array of parameters to be sent. 49 | * 50 | * @return array response 51 | */ 52 | private function _submitHTTPGet($data) 53 | { 54 | $url = self::site_verify_url.'?'.http_build_query($data); 55 | 56 | if( ini_get('allow_url_fopen') ) { 57 | $response = file_get_contents($url); 58 | } else { 59 | $ch = curl_init(); 60 | 61 | curl_setopt($ch, CURLOPT_AUTOREFERER, true); 62 | curl_setopt($ch, CURLOPT_HEADER, 0); 63 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 64 | curl_setopt($ch, CURLOPT_URL, $url); 65 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 66 | 67 | $response = curl_exec($ch); 68 | curl_close($ch); 69 | } 70 | 71 | return $response; 72 | } 73 | 74 | /** 75 | * Calls the reCAPTCHA siteverify API to verify whether the user passes 76 | * CAPTCHA test. 77 | * 78 | * @param string $response response string from recaptcha verification. 79 | * @param string $remoteIp IP address of end user. 80 | * 81 | * @return ReCaptchaResponse 82 | */ 83 | public function verifyResponse($response, $remoteIp = null) 84 | { 85 | $remoteIp = (!empty($remoteIp)) ? $remoteIp : $this->_ci->input->ip_address(); 86 | 87 | // Discard empty solution submissions 88 | if (empty($response)) { 89 | return array( 90 | 'success' => false, 91 | 'error-codes' => 'missing-input', 92 | ); 93 | } 94 | 95 | $getResponse = $this->_submitHttpGet( 96 | array( 97 | 'secret' => $this->_secretKey, 98 | 'remoteip' => $remoteIp, 99 | 'response' => $response, 100 | ) 101 | ); 102 | 103 | // get reCAPTCHA server response 104 | $responses = json_decode($getResponse, true); 105 | 106 | if (isset($responses['success']) and $responses['success'] == true) { 107 | $status = true; 108 | } else { 109 | $status = false; 110 | $error = (isset($responses['error-codes'])) ? $responses['error-codes'] 111 | : 'invalid-input-response'; 112 | } 113 | 114 | return array( 115 | 'success' => $status, 116 | 'error-codes' => (isset($error)) ? $error : null, 117 | ); 118 | } 119 | 120 | /** 121 | * Render Script Tag 122 | * 123 | * onload: Optional. 124 | * render: [explicit|onload] Optional. 125 | * hl: Optional. 126 | * see: https://developers.google.com/recaptcha/docs/display 127 | * 128 | * @param array parameters. 129 | * 130 | * @return scripts 131 | */ 132 | public function getScriptTag(array $parameters = array()) 133 | { 134 | $default = array( 135 | 'render' => 'onload', 136 | 'hl' => $this->_language, 137 | ); 138 | 139 | $result = array_merge($default, $parameters); 140 | 141 | $scripts = sprintf('', 142 | self::api_url, http_build_query($result)); 143 | 144 | return $scripts; 145 | } 146 | 147 | /** 148 | * render the reCAPTCHA widget 149 | * 150 | * data-theme: dark|light 151 | * data-type: audio|image 152 | * 153 | * @param array parameters. 154 | * 155 | * @return scripts 156 | */ 157 | public function getWidget(array $parameters = array()) 158 | { 159 | $default = array( 160 | 'data-sitekey' => $this->_siteKey, 161 | 'data-theme' => 'light', 162 | 'data-type' => 'image', 163 | 'data-size' => 'normal', 164 | ); 165 | 166 | $result = array_merge($default, $parameters); 167 | 168 | $html = ''; 169 | foreach ($result as $key => $value) { 170 | $html .= sprintf('%s="%s" ', $key, $value); 171 | } 172 | 173 | return '
'; 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /spark.info: -------------------------------------------------------------------------------- 1 | # This is the spark name. This should be the registered name of the spark. 2 | # It is here for informational purposes only. 3 | 4 | name: recaptcha-library 5 | 6 | # This is the current version of this spark. All sparks should be in 7 | # x.x.x format. Validation will fail otherwise. 8 | 9 | version: 1.0.2 10 | 11 | # This is the version of CodeIgniter this spark is compatible up to. It should 12 | # be in x.x.x format 13 | 14 | compatibility: 2.2.0 15 | 16 | tags: ["recaptcha", "google"] 17 | --------------------------------------------------------------------------------