Skip to content

Commit 385513a

Browse files
committed
first commit
0 parents  commit 385513a

File tree

22 files changed

+756
-0
lines changed

22 files changed

+756
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/composer.lock
2+
/phpunit.xml
3+
/vendor

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Captcha Demo plugin for CakePHP 3
2+
3+
**Requires** [CakePHP 3 Captcha Plugin](https://github.com/inimist/cakephp3-captcha)
4+
5+
## Installation
6+
7+
You can install this plugin into your CakePHP application using [composer](https://getcomposer.org).
8+
9+
The recommended way to install composer packages is:
10+
11+
```
12+
composer require inimist/captcha-demo
13+
```
14+
15+
And
16+
17+
```
18+
bin/cake plugin load CaptchaDemo -b -r
19+
```
20+
21+
After installation visit `my_app_url/captcha-demo/`
22+
23+
You can delete this plugin using:
24+
25+
```
26+
composer remove inimist/captcha-demo
27+
```

composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "inimist/captcha-demo",
3+
"type": "cakephp-plugin",
4+
"description": "CakePHP Captcha Plugin, Image, Math and Google Recaptcha Support",
5+
"keywords": ["cakephp", "plugin", "captcha", "security", "recaptcha"],
6+
"homepage": "https://github.com/inimist/cakephp-captcha-demo",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Arvind Kumar",
11+
"email": "[email protected]",
12+
"homepage": "https://inimisttech.com",
13+
"role": "Developer"
14+
}
15+
],
16+
"require": {
17+
"cakephp/cakephp": "^3.5.0",
18+
"inimist/cakephp-captcha": "^1.2"
19+
},
20+
"require-dev": {
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"CaptchaDemo\\": "src/"
25+
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"CaptchaDemo\\Test\\": "tests/",
30+
"Cake\\Test\\": "vendor/cakephp/cakephp/tests/"
31+
}
32+
}
33+
}

config/routes.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
use Cake\Routing\RouteBuilder;
3+
use Cake\Routing\Router;
4+
use Cake\Routing\Route\DashedRoute;
5+
6+
Router::plugin(
7+
'CaptchaDemo',
8+
['path' => '/captcha-demo'],
9+
function (RouteBuilder $routes) {
10+
$routes->redirect('/', ['controller' => 'Demo', 'action'=>'image']);
11+
$routes->get('/recaptcha', ['controller' => 'Demo', 'action'=>'recaptcha']);
12+
$routes->get('/image', ['controller' => 'Demo', 'action'=>'image']);
13+
$routes->get('/math', ['controller' => 'Demo', 'action'=>'math']);
14+
$routes->post('/recaptcha', ['controller' => 'Demo', 'action'=>'recaptcha']);
15+
$routes->post('/image', ['controller' => 'Demo', 'action'=>'image']);
16+
$routes->post('/math', ['controller' => 'Demo', 'action'=>'math']);
17+
$routes->fallbacks(DashedRoute::class);
18+
}
19+
);

phpunit.xml.dist

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
colors="true"
4+
processIsolation="false"
5+
stopOnFailure="false"
6+
syntaxCheck="false"
7+
bootstrap="tests/bootstrap.php"
8+
>
9+
<php>
10+
<ini name="memory_limit" value="-1"/>
11+
<ini name="apc.enable_cli" value="1"/>
12+
</php>
13+
14+
<!-- Add any additional test suites you want to run here -->
15+
<testsuites>
16+
<testsuite name="CaptchaDemo">
17+
<directory>tests/TestCase/</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<!-- Setup a listener for fixtures -->
22+
<listeners>
23+
<listener class="\Cake\TestSuite\Fixture\FixtureInjector">
24+
<arguments>
25+
<object class="\Cake\TestSuite\Fixture\FixtureManager"/>
26+
</arguments>
27+
</listener>
28+
</listeners>
29+
30+
<filter>
31+
<whitelist>
32+
<directory suffix=".php">src/</directory>
33+
</whitelist>
34+
</filter>
35+
36+
</phpunit>

src/Controller/AppController.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace CaptchaDemo\Controller;
4+
5+
use App\Controller\AppController as BaseController;
6+
7+
class AppController extends BaseController
8+
{
9+
}

src/Controller/DemoController.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
namespace CaptchaDemo\Controller;
3+
4+
use CaptchaDemo\Controller\AppController;
5+
6+
/**
7+
* Demo Controller
8+
*
9+
*
10+
* @method \CaptchaDemo\Model\Entity\Demo[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
11+
*/
12+
class DemoController extends AppController
13+
{
14+
public function initialize()
15+
{
16+
parent::initialize();
17+
$this->loadComponent('Captcha.Captcha');
18+
}
19+
/**
20+
* Index method
21+
*
22+
* @return \Cake\Http\Response|null
23+
*/
24+
public function index()
25+
{
26+
$demo = $this->paginate($this->Demo);
27+
28+
$this->set(compact('demo'));
29+
}
30+
public function image()
31+
{
32+
$demo = $this->Demo->newEntity();
33+
if($this->request->is('post')) {
34+
$this->Demo->setCaptcha('Captcha', $this->Captcha->getCode('Captcha'));
35+
$demo = $this->Demo->newEntity($this->request->getData());
36+
if ($demo->errors()) {
37+
$this->Flash->error(__('Validation failed.'));
38+
} else {
39+
$this->Flash->success(__('Validation success.'));
40+
}
41+
}
42+
$this->set(compact('demo'));
43+
}
44+
public function recaptcha()
45+
{
46+
$demo = $this->Demo->newEntity();
47+
if($this->request->is('post')) {
48+
//$this->Demo->setCaptcha('Captcha', $this->Captcha->getCode('Captcha'));
49+
$demo = $this->Demo->newEntity($this->request->getData());
50+
if ($demo->errors()) {
51+
$this->Flash->error(__('Validation failed.'));
52+
} else {
53+
$this->Flash->success(__('Validation success.'));
54+
}
55+
}
56+
$this->set(compact('demo'));
57+
}
58+
public function math()
59+
{
60+
$demo = $this->Demo->newEntity();
61+
if($this->request->is('post')) {
62+
$this->Demo->setCaptcha('Captcha', $this->Captcha->getCode('Captcha'));
63+
$demo = $this->Demo->newEntity($this->request->getData());
64+
if ($demo->errors()) {
65+
$this->Flash->error(__('Validation failed.'));
66+
} else {
67+
$this->Flash->success(__('Validation success.'));
68+
}
69+
}
70+
$this->set(compact('demo'));
71+
$this->set('_serialize', ['demo']);
72+
}
73+
}

src/Model/Entity/Demo.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
namespace CaptchaDemo\Model\Entity;
3+
4+
use Cake\ORM\Entity;
5+
6+
/**
7+
* Demo Entity
8+
*
9+
* @property int $id
10+
* @property string $name
11+
* @property string $email
12+
*/
13+
class Demo extends Entity
14+
{
15+
/**
16+
* Fields that can be mass assigned using newEntity() or patchEntity().
17+
*
18+
* Note that when '*' is set to true, this allows all unspecified fields to
19+
* be mass assigned. For security purposes, it is advised to set '*' to false
20+
* (or remove it), and explicitly make individual fields accessible as needed.
21+
*
22+
* @var array
23+
*/
24+
protected $_accessible = [
25+
'name' => true,
26+
'email' => true
27+
];
28+
}

src/Model/Table/DemoTable.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
namespace CaptchaDemo\Model\Table;
3+
4+
use Cake\ORM\Query;
5+
use Cake\ORM\RulesChecker;
6+
use Cake\ORM\Table;
7+
use Cake\Validation\Validator;
8+
9+
/**
10+
* Demo Model
11+
*
12+
* @method \CaptchaDemo\Model\Entity\Demo get($primaryKey, $options = [])
13+
* @method \CaptchaDemo\Model\Entity\Demo newEntity($data = null, array $options = [])
14+
* @method \CaptchaDemo\Model\Entity\Demo[] newEntities(array $data, array $options = [])
15+
* @method \CaptchaDemo\Model\Entity\Demo|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
16+
* @method \CaptchaDemo\Model\Entity\Demo saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
17+
* @method \CaptchaDemo\Model\Entity\Demo patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
18+
* @method \CaptchaDemo\Model\Entity\Demo[] patchEntities($entities, array $data, array $options = [])
19+
* @method \CaptchaDemo\Model\Entity\Demo findOrCreate($search, callable $callback = null, $options = [])
20+
*/
21+
class DemoTable extends Table
22+
{
23+
/**
24+
* Initialize method
25+
*
26+
* @param array $config The configuration for the Table.
27+
* @return void
28+
*/
29+
public function initialize(array $config)
30+
{
31+
parent::initialize($config);
32+
33+
$this->setTable('_inimist_captcha_demo');
34+
$this->setDisplayField('name');
35+
$this->setPrimaryKey('id');
36+
37+
$this->addBehavior('Captcha.Captcha', ['field'=>'Captcha', 'secret'=>'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx']);
38+
}
39+
40+
/**
41+
* Default validation rules.
42+
*
43+
* @param \Cake\Validation\Validator $validator Validator instance.
44+
* @return \Cake\Validation\Validator
45+
*/
46+
public function validationDefault(Validator $validator)
47+
{
48+
$validator
49+
->integer('id')
50+
->allowEmptyString('id', null, 'create');
51+
52+
$validator
53+
->scalar('name')
54+
->maxLength('name', 100)
55+
->requirePresence('name', 'create')
56+
->notEmptyString('name');
57+
58+
$validator
59+
->email('email')
60+
->requirePresence('email', 'create')
61+
->notEmptyString('email');
62+
63+
return $validator;
64+
}
65+
66+
/**
67+
* Returns a rules checker object that will be used for validating
68+
* application integrity.
69+
*
70+
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
71+
* @return \Cake\ORM\RulesChecker
72+
*/
73+
public function buildRules(RulesChecker $rules)
74+
{
75+
$rules->add($rules->isUnique(['email']));
76+
77+
return $rules;
78+
}
79+
}

src/Plugin.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace CaptchaDemo;
4+
5+
use Cake\Core\BasePlugin;
6+
7+
/**
8+
* Plugin for CaptchaDemo
9+
*/
10+
class Plugin extends BasePlugin
11+
{
12+
}

0 commit comments

Comments
 (0)