RecaptchaPlugin   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 137
Duplicated Lines 40.88 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 56
loc 137
rs 9.84
c 0
b 0
f 0
wmc 32
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A Init() 0 12 1
A configMapping() 0 14 1
A getCaptchaCacherKey() 0 4 1
A getLimit() 16 16 6
A FilterAppDataPluginSection() 7 7 4
B AjaxActionPreCall() 0 20 8
B FilterAjaxResponse() 33 33 11

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
class RecaptchaPlugin extends \RainLoop\Plugins\AbstractPlugin
4
{
5
	/**
6
	 * @return void
7
	 */
8
	public function Init()
9
	{
10
		$this->UseLangs(true);
11
		
12
		$this->addJs('js/recaptcha.js');
13
		
14
		$this->addHook('ajax.action-pre-call', 'AjaxActionPreCall');
15
		$this->addHook('filter.ajax-response', 'FilterAjaxResponse');
16
17
		$this->addTemplate('templates/PluginLoginReCaptchaGroup.html');
18
		$this->addTemplateHook('Login', 'BottomControlGroup', 'PluginLoginReCaptchaGroup');
19
	}
20
	
21
	/**
22
	 * @return array
23
	 */
24
	public function configMapping()
25
	{
26
		return array(
27
			\RainLoop\Plugins\Property::NewInstance('public_key')->SetLabel('Public Key')
28
				->SetAllowedInJs(true)
29
				->SetDefaultValue(''),
30
			\RainLoop\Plugins\Property::NewInstance('private_key')->SetLabel('Private Key')
31
				->SetDefaultValue(''),
32
			\RainLoop\Plugins\Property::NewInstance('error_limit')->SetLabel('Limit')
33
				->SetType(\RainLoop\Enumerations\PluginPropertyType::SELECTION)
34
				->SetDefaultValue(array(0, 1, 2, 3, 4, 5))
35
				->SetDescription('')
36
		);
37
	}
38
39
	/**
40
	 * @return string
41
	 */
42
	private function getCaptchaCacherKey()
43
	{
44
		return 'Captcha/Login/'.\RainLoop\Utils::GetConnectionToken();
45
	}
46
47
	/**
48
	 * @return int
49
	 */
50 View Code Duplication
	private function getLimit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
	{
52
		$iConfigLimit = $this->Config()->Get('plugin', 'error_limit', 0);
53
		if (0 < $iConfigLimit)
54
		{
55
			$oCacher = $this->Manager()->Actions()->Cacher();
56
			$sLimit = $oCacher && $oCacher->IsInited() ? $oCacher->Get($this->getCaptchaCacherKey()) : '0';
57
			
58
			if (0 < strlen($sLimit) && is_numeric($sLimit))
59
			{
60
				$iConfigLimit -= (int) $sLimit;
61
			}
62
		}
63
64
		return $iConfigLimit;
65
	}
66
67
	/**
68
	 * @return void
69
	 */
70 View Code Duplication
	public function FilterAppDataPluginSection($bAdmin, $bAuth, &$aData)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
	{
72
		if (!$bAdmin && !$bAuth && is_array($aData))
73
		{
74
			$aData['show_captcha_on_login'] = 1 > $this->getLimit();
75
		}
76
	}
77
78
	/**
79
	 * @param string $sAction
80
	 */
81
	public function AjaxActionPreCall($sAction)
82
	{
83
		if ('Login' === $sAction && 0 >= $this->getLimit())
84
		{
85
			require_once __DIR__.'/recaptchalib.php';
86
87
			$oResp = recaptcha_check_answer(
88
				$this->Config()->Get('plugin', 'private_key', ''),
89
				isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '',
90
				$this->Manager()->Actions()->GetActionParam('RecaptchaChallenge', ''),
91
				$this->Manager()->Actions()->GetActionParam('RecaptchaResponse', '')
92
			);
93
94
			if (!$oResp || !isset($oResp->is_valid) || !$oResp->is_valid)
95
			{
96
				$this->Manager()->Actions()->Logger()->WriteDump($oResp);
97
				throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::CaptchaError);
98
			}
99
		}
100
	}
101
102
	/**
103
	 * @param string $sAction
104
	 * @param array $aResponseItem
105
	 */
106 View Code Duplication
	public function FilterAjaxResponse($sAction, &$aResponseItem)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
	{
108
		if ('Login' === $sAction && $aResponseItem && isset($aResponseItem['Result']))
0 ignored issues
show
Bug Best Practice introduced by
The expression $aResponseItem of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
109
		{
110
			$oCacher = $this->Manager()->Actions()->Cacher();
111
			$iConfigLimit = (int) $this->Config()->Get('plugin', 'error_limit', 0);
112
			$sKey = $this->getCaptchaCacherKey();
113
114
			if (0 < $iConfigLimit && $oCacher && $oCacher->IsInited())
115
			{
116
				if (false === $aResponseItem['Result'])
117
				{
118
					$iLimit = 0;
119
					$sLimut = $oCacher->Get($sKey);
120
					if (0 < strlen($sLimut) && is_numeric($sLimut))
121
					{
122
						$iLimit = (int) $sLimut;
123
					}
124
125
					$oCacher->Set($sKey, ++$iLimit);
126
127
					if ($iConfigLimit <= $iLimit)
128
					{
129
						$aResponseItem['Captcha'] = true;
130
					}
131
				}
132
				else
133
				{
134
					$oCacher->Delete($sKey);
135
				}
136
			}
137
		}
138
	}
139
}
140