1: <?php
2:
3: /**
4: * recaptcha modules
5: * @package modules
6: * @subpackage recaptcha
7: */
8:
9: if (!defined('DEBUG_MODE')) { die(); }
10:
11: /**
12: * @subpackage recaptcha/handler
13: */
14: class Hm_Handler_process_recaptcha extends Hm_Handler_Module {
15: public function process() {
16: $rconf = recaptcha_config($this->config);
17: if (!is_array($rconf) || count($rconf) == 0) {
18: $this->out('recaptcha_config', array('site_key' => ''));
19: Hm_Debug::add('Recaptcha module activated, but not configured', 'warning');
20: return;
21: }
22: $this->out('recaptcha_config', $rconf);
23: list($success, $form) = $this->process_form(array('username', 'password'));
24: if (!$success) {
25: return;
26: }
27: if (!array_key_exists('g-recaptcha-response', $this->request->post)) {
28: $this->request->post = array();
29: Hm_Msgs::add('Recaptcha failed', 'danger');
30: return;
31: }
32: if (!check_recaptcha($rconf['secret'], $this->request->post['g-recaptcha-response'],
33: $this->request->server['REMOTE_ADDR'])) {
34: $this->request->post = array();
35: Hm_Msgs::add('Recaptcha failed', 'danger');
36: return;
37: }
38: }
39: }
40:
41: /**
42: * @subpackage recaptcha/output
43: */
44: class Hm_Output_recaptcha_script extends Hm_Output_Module {
45: protected function output() {
46: if (!$this->get('router_login_state') && $this->get('recaptcha_config')) {
47: return "<script src='https://www.google.com/recaptcha/api.js'></script>";
48: }
49: }
50: }
51:
52: /**
53: * @subpackage recaptcha/output
54: */
55: class Hm_Output_recaptcha_form extends Hm_Output_Module {
56: protected function output() {
57: if (!$this->get('router_login_state') && $recaptcha_config = $this->get('recaptcha_config')) {
58: return '<div class="g-recaptcha" data-sitekey="'.$this->html_safe($recaptcha_config['site_key']).'"></div>';
59: }
60: }
61: }
62:
63: /**
64: * @subpackage recaptcha/functions
65: */
66: if (!hm_exists('recaptcha_config')) {
67: function recaptcha_config($config) {
68: return $config->get('recaptcha', array());
69: }}
70:
71: /**
72: * @subpackage recaptcha/functions
73: */
74: if (!hm_exists('check_recaptcha')) {
75: function check_recaptcha($secret, $response, $ip) {
76: $api = new Hm_API_Curl();
77: $url = 'https://www.google.com/recaptcha/api/siteverify';
78: $post = array('secret' => $secret, 'response' => $response, 'remoteip' => $ip);
79: $res = $api->command($url, array(), $post);
80: if (is_array($res) && array_key_exists('success', $res) && $res['success']) {
81: return true;
82: }
83: return false;
84: }}
85: