Skip to content

Commit abdb266

Browse files
authored
Merge pull request #41 from crowdsecurity/improve-php-doc
improve php doc
2 parents 3970566 + 891cb61 commit abdb266

File tree

4 files changed

+108
-27
lines changed

4 files changed

+108
-27
lines changed

docs/api/ApiCache.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ The cache mecanism to store every decisions from LAPI/CAPI. Symfony Cache compon
7575
**Description**
7676

7777
```php
78-
public configure (void)
78+
public configure (bool $liveMode, string $apiUrl, int $timeout, string $userAgent, string $apiKey, int $cacheExpirationForCleanIp, int $cacheExpirationForBadIp, string $fallbackRemediation)
7979
```
8080

8181
Configure this instance.
@@ -84,7 +84,22 @@ Configure this instance.
8484

8585
**Parameters**
8686

87-
`This function has no parameters.`
87+
* `(bool) $liveMode`
88+
: If we use the live mode (else we use the stream mode)
89+
* `(string) $apiUrl`
90+
: The URL of the LAPI
91+
* `(int) $timeout`
92+
: The timeout well calling LAPI
93+
* `(string) $userAgent`
94+
: The user agent to use when calling LAPI
95+
* `(string) $apiKey`
96+
: The Bouncer API Key to use to connect LAPI
97+
* `(int) $cacheExpirationForCleanIp`
98+
: The duration to cache an IP considered as clean by LAPI
99+
* `(int) $cacheExpirationForBadIp`
100+
: The duration to cache an IP considered as bad by LAPI
101+
* `(string) $fallbackRemediation`
102+
: The remediation to use when the remediation sent by LAPI is not supported by this library
88103

89104
**Return Values**
90105

@@ -165,7 +180,7 @@ Used for the stream mode when we have to update the remediations list.
165180

166181
`array`
167182

168-
> number of deleted and new decisions
183+
> number of deleted and new decisions, and errors when processing decisions
169184
170185

171186
<hr />
@@ -220,9 +235,9 @@ Used when the stream mode has just been activated.
220235

221236
**Return Values**
222237

223-
`int`
238+
`array`
224239

225-
> number of decisions added
240+
> "count": number of decisions added, "errors": decisions not added
226241
227242

228243
<hr />

docs/api/Bouncer.md

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The main Class of this package. This is the first entry point of any PHP Bouncer
1111
| Name | Description |
1212
|------|-------------|
1313
|[__construct](#bouncer__construct)||
14-
|[buildCaptchaCouple](#bouncerbuildcaptchacouple)||
14+
|[buildCaptchaCouple](#bouncerbuildcaptchacouple)|Build a captcha couple.|
1515
|[checkCaptcha](#bouncercheckcaptcha)||
1616
|[clearCache](#bouncerclearcache)|This method clear the full data in cache.|
1717
|[configure](#bouncerconfigure)|Configure this instance.|
@@ -56,10 +56,10 @@ The main Class of this package. This is the first entry point of any PHP Bouncer
5656
**Description**
5757

5858
```php
59-
buildCaptchaCouple (void)
59+
public static buildCaptchaCouple (void)
6060
```
6161

62-
62+
Build a captcha couple.
6363

6464

6565

@@ -69,7 +69,9 @@ The main Class of this package. This is the first entry point of any PHP Bouncer
6969

7070
**Return Values**
7171

72-
`void`
72+
`array`
73+
74+
> an array composed of two items, a "phrase" string representing the phrase and a "inlineImage" representing the image data
7375
7476

7577
<hr />
@@ -117,7 +119,9 @@ This method clear the full data in cache.
117119

118120
**Return Values**
119121

120-
`void`
122+
`bool`
123+
124+
> If the cache has been successfully cleared or not
121125
122126

123127
<hr />
@@ -128,7 +132,7 @@ This method clear the full data in cache.
128132
**Description**
129133

130134
```php
131-
public configure (void)
135+
public configure (array $config)
132136
```
133137

134138
Configure this instance.
@@ -137,7 +141,8 @@ Configure this instance.
137141

138142
**Parameters**
139143

140-
`This function has no parameters.`
144+
* `(array) $config`
145+
: An array with all configuration parameters
141146

142147
**Return Values**
143148

@@ -152,7 +157,7 @@ Configure this instance.
152157
**Description**
153158

154159
```php
155-
public static getAccessForbiddenHtmlTemplate (void)
160+
public static getAccessForbiddenHtmlTemplate (array $config)
156161
```
157162

158163
Returns a default "CrowdSec 403" HTML template to display to a web browser using a banned IP.
@@ -161,11 +166,14 @@ The input $config should match the TemplateConfiguration input format.
161166

162167
**Parameters**
163168

164-
`This function has no parameters.`
169+
* `(array) $config`
170+
: An array of template configuration parameters
165171

166172
**Return Values**
167173

168-
`void`
174+
`string`
175+
176+
> The HTML compiled template
169177
170178

171179
<hr />
@@ -176,7 +184,7 @@ The input $config should match the TemplateConfiguration input format.
176184
**Description**
177185

178186
```php
179-
public static getCaptchaHtmlTemplate (void)
187+
public static getCaptchaHtmlTemplate (array $config)
180188
```
181189

182190
Returns a default "CrowdSec Captcha" HTML template to display to a web browser using a captchable IP.
@@ -185,11 +193,14 @@ The input $config should match the TemplateConfiguration input format.
185193

186194
**Parameters**
187195

188-
`This function has no parameters.`
196+
* `(array) $config`
197+
: An array of template configuration parameters
189198

190199
**Return Values**
191200

192-
`void`
201+
`string`
202+
203+
> The HTML compiled template
193204
194205

195206
<hr />
@@ -213,7 +224,9 @@ Returns the logger instance.
213224

214225
**Return Values**
215226

216-
`void`
227+
`\LoggerInterface`
228+
229+
> the logger used by this library
217230
218231

219232
<hr />
@@ -224,7 +237,7 @@ Returns the logger instance.
224237
**Description**
225238

226239
```php
227-
public getRemediationForIp (void)
240+
public getRemediationForIp (string $ip)
228241
```
229242

230243
Get the remediation for the specified IP. This method use the cache layer.
@@ -234,7 +247,8 @@ the cache system will call the API to check if there is a decision.
234247

235248
**Parameters**
236249

237-
`This function has no parameters.`
250+
* `(string) $ip`
251+
: The IP to check
238252

239253
**Return Values**
240254

@@ -264,7 +278,9 @@ This method prune the cache: it removes all the expired cache items.
264278

265279
**Return Values**
266280

267-
`void`
281+
`bool`
282+
283+
> If the cache has been successfully pruned or not
268284
269285

270286
<hr />
@@ -290,7 +306,7 @@ This method should be called periodically (ex: crontab) in a asynchronous way to
290306

291307
`array`
292308

293-
> number of deleted and new decisions
309+
> Number of deleted and new decisions, and errors when processing decisions
294310
295311

296312
<hr />
@@ -314,7 +330,9 @@ Test the connection to the cache system (Redis or Memcached).
314330

315331
**Return Values**
316332

317-
`void`
333+
`bool`
334+
335+
> If the connection was successful or not
318336
319337

320338
**Throws Exceptions**
@@ -344,9 +362,9 @@ This method should be called only to force a cache warm up.
344362

345363
**Return Values**
346364

347-
`int`
365+
`array`
348366

349-
> number of decisions added
367+
> "count": number of decisions added, "errors": decisions not added
350368
351369

352370
<hr />

src/ApiCache.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class ApiCache
4848
/** @var bool */
4949
private $warmedUp = null;
5050

51+
/**
52+
* @param LoggerInterface $logger The logger to use
53+
* @param ApiClient $apiClient The APIClient instance to use
54+
* @param AbstractAdapter $adapter The AbstractAdapter adapter to use
55+
*/
5156
public function __construct(LoggerInterface $logger, ApiClient $apiClient = null, AbstractAdapter $adapter = null)
5257
{
5358
$this->logger = $logger;
@@ -57,6 +62,15 @@ public function __construct(LoggerInterface $logger, ApiClient $apiClient = null
5762

5863
/**
5964
* Configure this instance.
65+
*
66+
* @param bool $liveMode If we use the live mode (else we use the stream mode)
67+
* @param string $apiUrl The URL of the LAPI
68+
* @param int $timeout The timeout well calling LAPI
69+
* @param string $userAgent The user agent to use when calling LAPI
70+
* @param string $apiKey The Bouncer API Key to use to connect LAPI
71+
* @param int $cacheExpirationForCleanIp The duration to cache an IP considered as clean by LAPI
72+
* @param int $cacheExpirationForBadIp The duration to cache an IP considered as bad by LAPI
73+
* @param string $fallbackRemediation The remediation to use when the remediation sent by LAPI is not supported by this library
6074
*/
6175
public function configure(
6276
bool $liveMode,

src/Bouncer.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public function __construct(AbstractAdapter $cacheAdapter = null, LoggerInterfac
5050

5151
/**
5252
* Configure this instance.
53+
*
54+
* @param array $config An array with all configuration parameters
5355
*/
5456
public function configure(array $config): void
5557
{
@@ -80,6 +82,10 @@ public function configure(array $config): void
8082

8183
/**
8284
* Cap the remediation to a fixed value given in configuration.
85+
*
86+
* @param string $remediation The maximum remediation that can ban applied (ex: 'ban', 'captcha', 'bypass')
87+
*
88+
* @return string $remediation The resulting remediation to use (ex: 'ban', 'captcha', 'bypass')
8389
*/
8490
private function capRemediationLevel(string $remediation): string
8591
{
@@ -96,6 +102,8 @@ private function capRemediationLevel(string $remediation): string
96102
* In live mode, when no remediation was found in cache,
97103
* the cache system will call the API to check if there is a decision.
98104
*
105+
* @param string $ip The IP to check
106+
*
99107
* @return string the remediation to apply (ex: 'ban', 'captcha', 'bypass')
100108
*/
101109
public function getRemediationForIp(string $ip): string
@@ -112,6 +120,10 @@ public function getRemediationForIp(string $ip): string
112120
/**
113121
* Returns a default "CrowdSec 403" HTML template to display to a web browser using a banned IP.
114122
* The input $config should match the TemplateConfiguration input format.
123+
*
124+
* @param array $config An array of template configuration parameters
125+
*
126+
* @return string The HTML compiled template
115127
*/
116128
public static function getAccessForbiddenHtmlTemplate(array $config): string
117129
{
@@ -129,6 +141,10 @@ public static function getAccessForbiddenHtmlTemplate(array $config): string
129141
/**
130142
* Returns a default "CrowdSec Captcha" HTML template to display to a web browser using a captchable IP.
131143
* The input $config should match the TemplateConfiguration input format.
144+
*
145+
* @param array $config An array of template configuration parameters
146+
*
147+
* @return string The HTML compiled template
132148
*/
133149
public static function getCaptchaHtmlTemplate(bool $error, string $captchaImageSrc, string $captchaResolutionFormUrl, array $config): string
134150
{
@@ -158,7 +174,7 @@ public function warmBlocklistCacheUp(): array
158174
* Used in stream mode only.
159175
* This method should be called periodically (ex: crontab) in a asynchronous way to update the bouncer cache.
160176
*
161-
* @return array number of deleted and new decisions, and errors when processing decisions
177+
* @return array Number of deleted and new decisions, and errors when processing decisions
162178
*/
163179
public function refreshBlocklistCache(): array
164180
{
@@ -167,6 +183,8 @@ public function refreshBlocklistCache(): array
167183

168184
/**
169185
* This method clear the full data in cache.
186+
*
187+
* @return bool If the cache has been successfully cleared or not
170188
*/
171189
public function clearCache(): bool
172190
{
@@ -175,6 +193,8 @@ public function clearCache(): bool
175193

176194
/**
177195
* This method prune the cache: it removes all the expired cache items.
196+
*
197+
* @return bool If the cache has been successfully pruned or not
178198
*/
179199
public function pruneCache(): bool
180200
{
@@ -183,12 +203,19 @@ public function pruneCache(): bool
183203

184204
/**
185205
* Returns the logger instance.
206+
*
207+
* @return LoggerInterface the logger used by this library
186208
*/
187209
public function getLogger(): LoggerInterface
188210
{
189211
return $this->logger;
190212
}
191213

214+
/**
215+
* Build a captcha couple.
216+
*
217+
* @return array an array composed of two items, a "phrase" string representing the phrase and a "inlineImage" representing the image data
218+
*/
192219
public static function buildCaptchaCouple()
193220
{
194221
$captchaBuilder = new CaptchaBuilder();
@@ -199,6 +226,11 @@ public static function buildCaptchaCouple()
199226
];
200227
}
201228

229+
/**
230+
* @param string $expected The expected phrase
231+
* @param string $expected The phrase to check (the user input)
232+
* @param string $ip Th IP of the use (for logging purpose)
233+
*/
202234
public function checkCaptcha(string $expected, string $try, string $ip)
203235
{
204236
$solved = PhraseBuilder::comparePhrases($expected, $try);
@@ -214,6 +246,8 @@ public function checkCaptcha(string $expected, string $try, string $ip)
214246
/**
215247
* Test the connection to the cache system (Redis or Memcached).
216248
*
249+
* @return bool If the connection was successful or not
250+
*
217251
* @throws BouncerException if the connection was not successful
218252
* */
219253
public function testConnection()

0 commit comments

Comments
 (0)