first commit

This commit is contained in:
Ariyutta
2026-08-07 10:47:09 +07:00
commit 1c094fbe7f
11630 changed files with 1580900 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
Copyright (c) 2015 Muharrem ERİN (me@mewebstudio.com)
http://www.opensource.org/licenses/mit-license.php The MIT License
+22
View File
@@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2015 MeWebStudio - Muharrem ERİN
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+263
View File
@@ -0,0 +1,263 @@
# Captcha for Laravel 10/11/12
[![Build Status](https://travis-ci.org/mewebstudio/captcha.svg?branch=master)](https://travis-ci.org/mewebstudio/captcha) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mewebstudio/captcha/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/mewebstudio/captcha/?branch=master)
[![Latest Stable Version](https://poser.pugx.org/mews/captcha/v/stable.svg)](https://packagist.org/packages/mews/captcha)
[![Latest Unstable Version](https://poser.pugx.org/mews/captcha/v/unstable.svg)](https://packagist.org/packages/mews/captcha)
[![License](https://poser.pugx.org/mews/captcha/license.svg)](https://packagist.org/packages/mews/captcha)
[![Total Downloads](https://poser.pugx.org/mews/captcha/downloads.svg)](https://packagist.org/packages/mews/captcha)
A simple [Laravel 5/6/7/8/9/10/11/12](http://www.laravel.com/) service provider for including the [Captcha for Laravel](https://github.com/mewebstudio/captcha).
for Laravel 4 [Captcha for Laravel Laravel 4](https://github.com/mewebstudio/captcha/tree/master-l4)
for Laravel 5 to 12 [Captcha for Laravel Laravel 5 and Newer versions](https://github.com/mewebstudio/captcha/tree/master-l5-l9)
## Preview
![Preview](https://image.ibb.co/kZxMLm/image.png)
- [Captcha for Laravel 5/6/7/8/9/10/11/12](#captcha-for-laravel-5-6-7)
* [Preview](#preview)
* [Installation](#installation)
* [Usage](#usage)
* [Configuration](#configuration)
+ [Custom settings:](#custom-settings)
+ [Disable validation:](#disable-validation)
* [Example Usage](#example-usage)
+ [Session Mode:](#session-mode)
+ [Stateless Mode:](#stateless-mode)
- [Return Image](#return-image)
- [Return URL](#return-url)
- [Return HTML](#return-html)
- [To use different configurations](#to-use-different-configurations)
* [Links](#links)
## Installation
The Captcha Service Provider can be installed via [Composer](http://getcomposer.org) by requiring the
`mews/captcha` package and setting the `minimum-stability` to `dev` (required for Laravel 5) in your
project's `composer.json`.
```json
{
"require": {
"laravel/framework": "5.0.*",
"mews/captcha": "~3.0"
},
"minimum-stability": "stable"
}
```
or
Require this package with composer:
```
composer require mews/captcha
```
Update your packages with ```composer update``` or install with ```composer install```.
In Windows, you'll need to include the GD2 DLL `php_gd2.dll` in php.ini. And you also need include `php_fileinfo.dll` and `php_mbstring.dll` to fit the requirements of `mews/captcha`'s dependencies.
## Usage
To use the Captcha Service Provider, you must register the provider when bootstrapping your Laravel application. There are
essentially two ways to do this.
Find the `providers` key in `config/app.php` and register the Captcha Service Provider.
```php
'providers' => [
// ...
'Mews\Captcha\CaptchaServiceProvider',
]
```
for Laravel 5.1+
```php
'providers' => [
// ...
Mews\Captcha\CaptchaServiceProvider::class,
]
```
For Laravel 11+ you can add the provider to `bootstrap\providers.php`.
```php
return [
// ...
Mews\Captcha\CaptchaServiceProvider::class
];
```
Find the `aliases` key in `config/app.php`.
```php
'aliases' => [
// ...
'Captcha' => 'Mews\Captcha\Facades\Captcha',
]
```
for Laravel 5.1+
```php
'aliases' => [
// ...
'Captcha' => Mews\Captcha\Facades\Captcha::class,
]
```
For Laravel 11+ : you do not need to add the alias, it will be added automatically.
## Configuration
### Custom settings:
To use your own settings, publish config.
```$ php artisan vendor:publish --provider="Mews\Captcha\CaptchaServiceProvider"```
`config/captcha.php`
```php
return [
'default' => [
'length' => 5,
'width' => 120,
'height' => 36,
'quality' => 90,
'math' => true, //Enable Math Captcha
'expire' => 60, //Captcha expiration
],
// ...
];
```
### Images
To use your own custom images for a background, set 'bgImage' to true and change the 'bgsDirectory' setting to your directory you want the image(s) to be used.
If you just want to change the background color, then set 'bgImage' to false and the 'bgColor' will be applied.
### Disable validation:
To disable the captcha validation use `CAPTCHA_DISABLE` environment variable. e.g. **.env** config:
```php
CAPTCHA_DISABLE=true
```
## Example Usage
### Session Mode:
```php
// [your site path]/Http/routes.php
Route::any('captcha-test', function() {
if (request()->getMethod() == 'POST') {
$rules = ['captcha' => 'required|captcha'];
$validator = validator()->make(request()->all(), $rules);
if ($validator->fails()) {
echo '<p style="color: #ff0000;">Incorrect!</p>';
} else {
echo '<p style="color: #00ff30;">Matched :)</p>';
}
}
$form = '<form method="post" action="captcha-test">';
$form .= '<input type="hidden" name="_token" value="' . csrf_token() . '">';
$form .= '<p>' . captcha_img() . '</p>';
$form .= '<p><input type="text" name="captcha"></p>';
$form .= '<p><button type="submit" name="check">Check</button></p>';
$form .= '</form>';
return $form;
});
```
Detailed Example in Laravel way
view files
```html
//register.blade.php
<img src="{{ captcha_src() }}" alt="captcha">
<div class="mt-2"></div>
<input
type="text" name="captcha" class="form-control @error('captcha') is-invalid @enderror" placeholder="Please Insert Captch"
>
@error('captcha')
<div class="invalid-feedback">{{ $message }}</div> @enderror
```
controller files
```php
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => [
'required',
'string',
'email',
'max:255',
Rule::unique(User::class),
],
'password' => $this->passwordRules(),
'captcha' => 'required|captcha'
])->validate();
```
### Stateless Mode:
You get key and img from this url
`http://localhost/captcha/api/math`
and verify the captcha using this method:
```php
//key is the one that you got from json response
// fix validator
// $rules = ['captcha' => 'required|captcha_api:'. request('key')];
$rules = ['captcha' => 'required|captcha_api:'. request('key') . ',math'];
$validator = validator()->make(request()->all(), $rules);
if ($validator->fails()) {
return response()->json([
'message' => 'invalid captcha',
]);
} else {
//do the job
}
```
# Return Image
```php
captcha();
```
or
```php
Captcha::create();
```
# Return URL
```php
captcha_src();
```
or
```
Captcha::src('default');
```
# Return HTML
```php
captcha_img();
```
or
```php
Captcha::img();
```
# To use different configurations
```php
captcha_img('flat');
Captcha::img('inverse');
```
etc.
Based on [Intervention Image](https://github.com/Intervention/image)
^_^
## Links
* [Intervention Image](https://github.com/Intervention/image)
* [L5 Captcha on Github](https://github.com/mewebstudio/captcha)
* [L5 Captcha on Packagist](https://packagist.org/packages/mews/captcha)
* [For L4 on Github](https://github.com/mewebstudio/captcha/tree/master-l4)
* [License](http://www.opensource.org/licenses/mit-license.php)
* [Laravel website](http://laravel.com)
* [Laravel Turkiye website](http://www.laravel.gen.tr)
* [mewebstudio](https://github.com/mewebstudio/captcha)
Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+202
View File
@@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+1
View File
@@ -0,0 +1 @@
Copyright (c) <dates>, <Copyright Holder> (<URL|email>),
@@ -0,0 +1,96 @@
-------------------------------
UBUNTU FONT LICENCE Version 1.0
-------------------------------
PREAMBLE
This licence allows the licensed fonts to be used, studied, modified and
redistributed freely. The fonts, including any derivative works, can be
bundled, embedded, and redistributed provided the terms of this licence
are met. The fonts and derivatives, however, cannot be released under
any other licence. The requirement for fonts to remain under this
licence does not require any document created using the fonts or their
derivatives to be published under this licence, as long as the primary
purpose of the document is not to be a vehicle for the distribution of
the fonts.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this licence and clearly marked as such. This may
include source files, build scripts and documentation.
"Original Version" refers to the collection of Font Software components
as received under this licence.
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to
a new environment.
"Copyright Holder(s)" refers to all individuals and companies who have a
copyright ownership of the Font Software.
"Substantially Changed" refers to Modified Versions which can be easily
identified as dissimilar to the Font Software by users of the Font
Software comparing the Original Version with the Modified Version.
To "Propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification and with or without charging
a redistribution fee), making available to the public, and in some
countries other activities as well.
PERMISSION & CONDITIONS
This licence does not grant any rights under trademark law and all such
rights are reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of the Font Software, to propagate the Font Software, subject to
the below conditions:
1) Each copy of the Font Software must contain the above copyright
notice and this licence. These can be included either as stand-alone
text files, human-readable headers or in the appropriate machine-
readable metadata fields within text or binary files as long as those
fields can be easily viewed by the user.
2) The font name complies with the following:
(a) The Original Version must retain its name, unmodified.
(b) Modified Versions which are Substantially Changed must be renamed to
avoid use of the name of the Original Version or similar names entirely.
(c) Modified Versions which are not Substantially Changed must be
renamed to both (i) retain the name of the Original Version and (ii) add
additional naming elements to distinguish the Modified Version from the
Original Version. The name of such Modified Versions must be the name of
the Original Version, with "derivative X" where X represents the name of
the new work, appended to that name.
3) The name(s) of the Copyright Holder(s) and any contributor to the
Font Software shall not be used to promote, endorse or advertise any
Modified Version, except (i) as required by this licence, (ii) to
acknowledge the contribution(s) of the Copyright Holder(s) or (iii) with
their explicit written permission.
4) The Font Software, modified or unmodified, in part or in whole, must
be distributed entirely under this licence, and must not be distributed
under any other licence. The requirement for fonts to remain under this
licence does not affect any document created using the Font Software,
except any version of the Font Software extracted from a document
created using the Font Software may only be distributed under this
licence.
TERMINATION
This licence becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER
DEALINGS IN THE FONT SOFTWARE.
+63
View File
@@ -0,0 +1,63 @@
{
"name": "mews/captcha",
"type": "package",
"description": "Laravel 5/6/7/8/9/10/11/12 Captcha Package",
"keywords": [
"laravel12 Captcha",
"laravel12 Security",
"laravel12 Captcha",
"laravel5 Security",
"Captcha"
],
"homepage": "https://github.com/mewebstudio/captcha",
"license": "MIT",
"authors": [
{
"name": "Muharrem ERİN",
"email": "me@mewebstudio.com",
"homepage": "https://github.com/mewebstudio",
"role": "Developer"
}
],
"require": {
"php": "^7.2|^8.1|^8.2|^8.3",
"ext-gd": "*",
"illuminate/config": "~5|^6|^7|^8|^9|^10|^11|^12|^13.0",
"illuminate/filesystem": "~5|^6|^7|^8|^9|^10|^11|^12|^13.0",
"illuminate/support": "~5|^6|^7|^8|^9|^10|^11|^12|^13.0",
"illuminate/hashing": "~5|^6|^7|^8|^9|^10|^11|^12|^13.0",
"illuminate/session": "~5|^6|^7|^8|^9|^10|^11|^12|^13.0",
"intervention/image": "^3.7"
},
"require-dev": {
"phpunit/phpunit": "^8.5|^9.5.10|^10.5|^11",
"mockery/mockery": "^1.0"
},
"autoload": {
"psr-4": {
"Mews\\Captcha\\": "src/"
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"classmap": [
"tests"
],
"psr-4": {
"Mews\\Test\\": "tests/"
}
},
"minimum-stability": "stable",
"extra": {
"laravel": {
"providers": [
"Mews\\Captcha\\CaptchaServiceProvider"
],
"aliases": {
"Captcha": "Mews\\Captcha\\Facades\\Captcha"
}
}
}
}
+55
View File
@@ -0,0 +1,55 @@
<?php
return [
'disable' => env('CAPTCHA_DISABLE', false),
'characters' => ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
'fontsDirectory' => dirname(__DIR__) . '/assets/fonts',
'bgsDirectory' => dirname(__DIR__) . '/assets/backgrounds',
'default' => [
'length' => 6,
'width' => 345,
'height' => 65,
'quality' => 90,
'math' => false,
'expire' => 60,
'encrypt' => false,
],
'flat' => [
'length' => 6,
'fontColors' => ['#2c3e50', '#c0392b', '#16a085', '#c0392b', '#8e44ad', '#303f9f', '#f57c00', '#795548'],
'width' => 345,
'height' => 65,
'math' => false,
'quality' => 100,
'lines' => 6,
'bgImage' => true,
'bgColor' => '#28faef',
'contrast' => 0,
],
'mini' => [
'length' => 3,
'width' => 60,
'height' => 32,
],
'inverse' => [
'length' => 5,
'width' => 120,
'height' => 36,
'quality' => 90,
'sensitive' => true,
'angle' => 12,
'sharpen' => 10,
'blur' => 2,
'invert' => false,
'contrast' => -5,
],
'math' => [
'length' => 9,
'width' => 120,
'height' => 36,
'quality' => 90,
],
];
+575
View File
@@ -0,0 +1,575 @@
<?php
namespace Mews\Captcha;
/**
* Laravel 5 & 6 Captcha package
*
* @copyright Copyright (c) 2015 MeWebStudio
* @version 2.x
* @author Muharrem ERİN
* @contact me@mewebstudio.com
* @web http://www.mewebstudio.com
* @date 2015-04-03
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
use Exception;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Hashing\BcryptHasher as Hasher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\File;
use Illuminate\Support\Str;
use Intervention\Image\Gd\Font;
use Intervention\Image\Geometry\Factories\LineFactory;
use Intervention\Image\Image;
use Intervention\Image\ImageManager;
use Illuminate\Session\Store as Session;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Http\Response;
/**
* Class Captcha
* @package Mews\Captcha
*/
class Captcha
{
/**
* @var Filesystem
*/
protected $files;
/**
* @var Repository
*/
protected $config;
/**
* @var ImageManager
*/
protected $imageManager;
/**
* @var Session
*/
protected $session;
/**
* @var Hasher
*/
protected $hasher;
/**
* @var Str
*/
protected $str;
/**
* @var ImageManager->canvas
*/
protected $canvas;
/**
* @var Image
*/
protected $image;
/**
* @var array
*/
protected $backgrounds = [];
/**
* @var array
*/
protected $fonts = [];
/**
* @var array
*/
protected $fontColors = [];
/**
* @var int
*/
protected $length = 5;
/**
* @var int
*/
protected $width = 120;
/**
* @var int
*/
protected $height = 36;
/**
* @var int
*/
protected $angle = 15;
/**
* @var int
*/
protected $lines = 3;
/**
* @var int
*/
protected $lineWidth = 2;
/**
* @var string
*/
protected $lineColor = 'ff00ff';
/**
* @var string
*/
protected $characters;
/**
* @var array
*/
protected $text;
/**
* @var int
*/
protected $contrast = 0;
/**
* @var int
*/
protected $quality = 90;
/**
* @var int
*/
protected $sharpen = 0;
/**
* @var int
*/
protected $blur = 0;
/**
* @var string
*/
protected $bgsDirectory;
/**
* @var bool
*/
protected $bgImage = true;
/**
* @var string
*/
protected $bgColor = '#ffffff';
/**
* @var bool
*/
protected $invert = false;
/**
* @var bool
*/
protected $sensitive = false;
/**
* @var bool
*/
protected $math = false;
/**
* @var int
*/
protected $textLeftPadding = 4;
/**
* @var string
*/
protected $fontsDirectory;
/**
* @var int
*/
protected $expire = 60;
/**
* @var bool
*/
protected $encrypt = true;
/**
* @var int
*/
protected $marginTop = 0;
/**
* Constructor
*
* @param Filesystem $files
* @param Repository $config
* @param ImageManager $imageManager
* @param Session $session
* @param Hasher $hasher
* @param Str $str
* @throws Exception
* @internal param Validator $validator
*/
public function __construct(
Filesystem $files,
Repository $config,
ImageManager $imageManager,
Session $session,
Hasher $hasher,
Str $str
) {
$this->files = $files;
$this->config = $config;
$this->imageManager = $imageManager;
$this->session = $session;
$this->hasher = $hasher;
$this->str = $str;
$this->characters = config('captcha.characters', ['1', '2', '3', '4', '6', '7', '8', '9']);
$this->fontsDirectory = config('captcha.fontsDirectory', dirname(__DIR__) . '/assets/fonts');
$this->bgsDirectory = config('captcha.bgsDirectory', dirname(__DIR__) . '/assets/backgrounds');
}
/**
* @param string $config
* @return void
*/
protected function configure($config)
{
if ($this->config->has('captcha.' . $config)) {
foreach ($this->config->get('captcha.' . $config) as $key => $val) {
$this->{$key} = $val;
}
}
}
/**
* Create captcha image
*
* @param string $config
* @param bool $api
* @return array|mixed
* @throws Exception
*/
public function create(string $config = 'default', bool $api = false)
{
$this->backgrounds = $this->files->files($this->bgsDirectory);
$this->fonts = $this->files->files($this->fontsDirectory);
if (version_compare(app()->version(), '5.5.0', '>=')) {
$this->fonts = array_map(function ($file) {
/* @var File $file */
return $file->getPathName();
}, $this->fonts);
}
$this->fonts = array_values($this->fonts); //reset fonts array index
$this->configure($config);
$generator = $this->generate();
$this->text = $generator['value'];
$this->canvas = $this->imageManager->create($this->width , $this->height)->fill($this->bgColor);
if ($this->bgImage) {
$this->image = $this->imageManager->read($this->background())->resize(
$this->width,
$this->height
);
$this->canvas->place($this->image);
} else {
$this->image = $this->canvas;
}
if ($this->contrast != 0) {
$this->image->contrast($this->contrast);
}
$this->text();
$this->lines();
if ($this->sharpen) {
$this->image->sharpen($this->sharpen);
}
if ($this->invert) {
$this->image->invert();
}
if ($this->blur) {
$this->image->blur($this->blur);
}
Cache::put($this->get_cache_key($generator['key']), $generator['value'], $this->expire);
return $api ? [
'sensitive' => $generator['sensitive'],
'key' => $generator['key'],
'img' => $this->image->toJpg()->toDataUri()
] : new Response($this->image->toJpg(), 200, [
'Content-Type' => 'image/jpeg',
'Content-Disposition' => 'inline; filename="image.jpg"',
]);
}
/**
* Image backgrounds
*
* @return string
*/
protected function background(): string
{
return $this->backgrounds[rand(0, count($this->backgrounds) - 1)];
}
/**
* Generate captcha text
*
* @return array
* @throws Exception
*/
protected function generate(): array
{
$characters = is_string($this->characters) ? str_split($this->characters) : $this->characters;
$bag = [];
if ($this->math) {
$x = random_int(10, 30);
$y = random_int(1, 9);
$bag = "$x + $y = ";
$key = $x + $y;
$key .= '';
} else {
for ($i = 0; $i < $this->length; $i++) {
$char = $characters[rand(0, count($characters) - 1)];
$bag[] = $this->sensitive ? $char : $this->str->lower($char);
}
$key = implode('', $bag);
}
$hash = $this->hasher->make($key);
if($this->encrypt) $hash = Crypt::encrypt($hash);
$this->session->put('captcha', [
'sensitive' => $this->sensitive,
'key' => $hash,
'encrypt' => $this->encrypt
]);
return [
'value' => $bag,
'sensitive' => $this->sensitive,
'key' => $hash
];
}
/**
* Writing captcha text
*
* @return void
*/
protected function text(): void
{
$marginTop = $this->image->height() / $this->length;
if ($this->marginTop !== 0) {
$marginTop = $this->marginTop;
}
$text = $this->text;
if (is_string($text)) {
$text = str_split($text);
}
foreach ($text as $key => $char) {
$marginLeft = $this->textLeftPadding + ($key * ($this->image->width() - $this->textLeftPadding) / $this->length);
$this->image->text($char, (int) $marginLeft, (int) $marginTop, function ($font) {
/* @var Font $font */
$font->file($this->font());
$font->size($this->fontSize());
$font->color($this->fontColor());
$font->align('left');
$font->valign('top');
$font->angle($this->angle());
});
}
}
/**
* Image fonts
*
* @return string
*/
protected function font(): string
{
return $this->fonts[rand(0, count($this->fonts) - 1)];
}
/**
* Random font size
*
* @return int
*/
protected function fontSize(): int
{
return rand($this->image->height() - 10, $this->image->height());
}
/**
* Random font color
*
* @return string
*/
protected function fontColor(): string
{
if (!empty($this->fontColors)) {
$color = $this->fontColors[rand(0, count($this->fontColors) - 1)];
} else {
$color = '#' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
}
return $color;
}
/**
* Angle
*
* @return int
*/
protected function angle(): int
{
return rand((-1 * $this->angle), $this->angle);
}
/**
* Random image lines
*
* @return Image|ImageManager
*/
protected function lines()
{
for ($i = 0; $i <= $this->lines; $i++) {
$this->image->drawLine(function (LineFactory $line) use ($i) {
$line->from(rand(0, $this->image->width()) + $i * rand(0, $this->image->height()) , rand(0, $this->image->height()));
$line->to( rand(0, $this->image->width()), rand(0, $this->image->height()));
$line->color($this->lineColor); // color of line
$line->width($this->lineWidth); // line width in pixels
});
}
return $this->image;
}
/**
* Captcha check
*
* @param string $value
* @return bool
*/
public function check(string $value): bool
{
if (!$this->session->has('captcha')) {
return false;
}
$key = $this->session->get('captcha.key');
$sensitive = $this->session->get('captcha.sensitive');
$encrypt = $this->session->get('captcha.encrypt');
if (!Cache::pull($this->get_cache_key($key))) {
$this->session->remove('captcha');
return false;
}
if (!$sensitive) {
$value = $this->str->lower($value);
}
if($encrypt) $key = Crypt::decrypt($key);
$check = $this->hasher->check($value, $key);
// if verify pass,remove session
if ($check) {
$this->session->remove('captcha');
}
return $check;
}
/**
* Returns the md5 short version of the key for cache
*
* @param string $key
* @return string
*/
protected function get_cache_key($key) {
return 'captcha_' . md5($key);
}
/**
* Captcha check
*
* @param string $value
* @param string $key
* @param string $config
* @return bool
*/
public function check_api($value, $key, $config = 'default'): bool
{
if (!Cache::pull($this->get_cache_key($key))) {
return false;
}
$this->configure($config);
if(!$this->sensitive) $value = $this->str->lower($value);
if($this->encrypt) $key = Crypt::decrypt($key);
return $this->hasher->check($value, $key);
}
/**
* Generate captcha image source
*
* @param string $config
* @return string
*/
public function src(string $config = 'default'): string
{
return url('captcha/' . $config) . '?' . $this->str->random(8);
}
/**
* Generate captcha image html tag
*
* @param string $config
* @param array $attrs
* $attrs -> HTML attributes supplied to the image tag where key is the attribute and the value is the attribute value
* @return string
*/
public function img(string $config = 'default', array $attrs = []): string
{
$attrs_str = '';
foreach ($attrs as $attr => $value) {
if ($attr == 'src') {
//Neglect src attribute
continue;
}
$attrs_str .= $attr . '="' . $value . '" ';
}
return new HtmlString('<img src="' . $this->src($config) . '" ' . trim($attrs_str) . '>');
}
}
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace Mews\Captcha;
use Exception;
use Illuminate\Routing\Controller;
/**
* Class CaptchaController
* @package Mews\Captcha
*/
class CaptchaController extends Controller
{
/**
* get CAPTCHA
*
* @param Captcha $captcha
* @param string $config
* @return array|mixed
* @throws Exception
*/
public function getCaptcha(Captcha $captcha, string $config = 'default')
{
if (ob_get_contents()) {
ob_clean();
}
return $captcha->create($config);
}
/**
* get CAPTCHA api
*
* @param Captcha $captcha
* @param string $config
* @return array|mixed
* @throws Exception
*/
public function getCaptchaApi(Captcha $captcha, string $config = 'default')
{
return $captcha->create($config, true);
}
}
+99
View File
@@ -0,0 +1,99 @@
<?php
namespace Mews\Captcha;
use Illuminate\Routing\Router;
use Illuminate\Validation\Factory;
use Intervention\Image\ImageManager;
use Illuminate\Support\ServiceProvider;
use Intervention\Image\Drivers\Gd\Driver as GdDriver;
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver;
/**
* Class CaptchaServiceProvider
* @package Mews\Captcha
*/
class CaptchaServiceProvider extends ServiceProvider
{
/**
* Boot the service provider.
*
* @return void
*/
public function boot(): void
{
// Publish configuration files
$this->publishes([
__DIR__ . '/../config/captcha.php' => config_path('captcha.php')
], 'config');
// HTTP routing
if(!config('captcha.disable')){
if (strpos($this->app->version(), 'Lumen') !== false) {
/* @var Router $router */
$router = $this->app;
$router->get('captcha[/api/{config}]', 'Mews\Captcha\LumenCaptchaController@getCaptchaApi');
$router->get('captcha[/{config}]', 'Mews\Captcha\LumenCaptchaController@getCaptcha');
} else {
/* @var Router $router */
$router = $this->app['router'];
if ((double)$this->app->version() >= 5.2) {
$router->get('captcha/api/{config?}', '\Mews\Captcha\CaptchaController@getCaptchaApi')->middleware('web');
$router->get('captcha/{config?}', '\Mews\Captcha\CaptchaController@getCaptcha')->middleware('web');
} else {
$router->get('captcha/api/{config?}', '\Mews\Captcha\CaptchaController@getCaptchaApi');
$router->get('captcha/{config?}', '\Mews\Captcha\CaptchaController@getCaptcha');
}
}
}
/* @var Factory $validator */
$validator = $this->app['validator'];
// Validator extensions
$validator->extend('captcha', function ($attribute, $value, $parameters) {
return config('captcha.disable') || ($value && captcha_check($value));
});
// Validator extensions
$validator->extend('captcha_api', function ($attribute, $value, $parameters) {
return config('captcha.disable') || ($value && captcha_api_check($value, $parameters[0], $parameters[1] ?? 'default'));
});
}
/**
* Register the service provider.
*
* @return void
*/
public function register(): void
{
// Merge configs
$this->mergeConfigFrom(
__DIR__ . '/../config/captcha.php',
'captcha'
);
// Bind the ImageManager with an explicit driver
if (!$this->app->bound('Intervention\Image\ImageManager')) {
$this->app->singleton('Intervention\Image\ImageManager', function ($app) {
// Determine which driver to use, defaulting to 'gd'
$driver = config('captcha.driver', 'gd') === 'imagick' ? new ImagickDriver() : new GdDriver();
return new ImageManager($driver);
});
}
// Bind captcha
$this->app->bind('captcha', function ($app) {
return new Captcha(
$app['Illuminate\Filesystem\Filesystem'],
$app['Illuminate\Contracts\Config\Repository'],
$app['Intervention\Image\ImageManager'],
$app['Illuminate\Session\Store'],
$app['Illuminate\Hashing\BcryptHasher'],
$app['Illuminate\Support\Str']
);
});
}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace Mews\Captcha\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @see \Mews\Captcha\Captcha
*/
class Captcha extends Facade
{
/**
* @return string
*/
protected static function getFacadeAccessor()
{
return 'captcha';
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace Mews\Captcha;
use Exception;
use Laravel\Lumen\Routing\Controller;
/**
* Class CaptchaController
* @package Mews\Captcha
*/
class LumenCaptchaController extends Controller
{
/**
* get CAPTCHA
*
* @param Captcha $captcha
* @param string $config
* @return array|mixed
* @throws Exception
*/
public function getCaptcha(Captcha $captcha, $config = 'default')
{
return $captcha->create($config);
}
}
+63
View File
@@ -0,0 +1,63 @@
<?php
use Intervention\Image\ImageManager;
if (!function_exists('captcha')) {
/**
* @param string $config
* @return array|ImageManager|mixed
* @throws Exception
*/
function captcha(string $config = 'default')
{
return app('captcha')->create($config);
}
}
if (!function_exists('captcha_src')) {
/**
* @param string $config
* @return string
*/
function captcha_src(string $config = 'default'): string
{
return app('captcha')->src($config);
}
}
if (!function_exists('captcha_img')) {
/**
* @param string $config
* @param array $attrs
* @return string
*/
function captcha_img(string $config = 'default', array $attrs = []): string
{
return app('captcha')->img($config, $attrs);
}
}
if (!function_exists('captcha_check')) {
/**
* @param string $value
* @return bool
*/
function captcha_check(string $value): bool
{
return app('captcha')->check($value);
}
}
if (!function_exists('captcha_api_check')) {
/**
* @param string $value
* @param string $key
* @param string $config
* @return bool
*/
function captcha_api_check(string $value, string $key, string $config = 'default'): bool
{
return app('captcha')->check_api($value, $key, $config);
}
}