AnuTiger /
F3
| 1 | <?php |
||
| 2 | |||
| 3 | class Captcha extends Prefab |
||
| 4 | { |
||
| 5 | protected $name; |
||
| 6 | protected $code; |
||
| 7 | protected $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||
| 8 | |||
| 9 | public function __construct($name = 'captcha-string') |
||
| 10 | { |
||
| 11 | $this->name = $name; |
||
| 12 | } |
||
| 13 | |||
| 14 | public function text($length = 4, $mock = null) |
||
| 15 | { |
||
| 16 | $this->setSession($code = $this->_generate($length)); |
||
| 17 | return $code; |
||
| 18 | } |
||
| 19 | |||
| 20 | public function img($length = 4, $class = 'captcha-img') |
||
| 21 | { |
||
| 22 | return '<img src="'.$this->text($length).'" class="'.$class.'" />'; |
||
| 23 | } |
||
| 24 | |||
| 25 | public function source($length = 4, $mock = null) |
||
| 26 | { |
||
| 27 | } |
||
| 28 | |||
| 29 | public function input($class = 'captcha-input') |
||
| 30 | { |
||
| 31 | return '<input type="text" class="'.$class.'" name="'.$this->name.'" />'; |
||
| 32 | } |
||
| 33 | |||
| 34 | /* |
||
| 35 | * Captcha::instance()->render(4) |
||
| 36 | * Captcha::instance()->render(array('type'=>'img','length'=>4,'class'=>array('img'=>'captcha-img','input'=>'captcha-input'))); |
||
| 37 | */ |
||
| 38 | public function render($param, $url) |
||
| 39 | { |
||
| 40 | if (is_array($param)) { |
||
| 41 | $func = null; |
||
| 42 | $length = null; |
||
| 43 | $imgClass = null; |
||
| 44 | $inputClass = null; |
||
| 45 | foreach ($param as $key => $val) { |
||
| 46 | if ('type' == $key) { |
||
| 47 | $func = $key; |
||
| 48 | } |
||
| 49 | if ('length' == $key) { |
||
| 50 | $length = $key; |
||
| 51 | } |
||
| 52 | if ('class' == $key && is_array($key)) { |
||
| 53 | if (array_key_exists('img', $key)) { |
||
| 54 | $imgClass = $key['img']; |
||
| 55 | } elseif (array_key_exists('input', $key)) { |
||
| 56 | $inputClass = $key['input']; |
||
| 57 | } |
||
| 58 | } else { |
||
| 59 | $imgClass = $inputClass = $key; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | if (method_exists($this, $func)) { |
||
| 63 | $response = $this->$key($length, $imgClass).$this->input($inputClass); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 64 | } else { |
||
| 65 | throw new Exception("Error Processing Captcha Method", 1); |
||
| 66 | } |
||
| 67 | } elseif (is_numeric($param)) { |
||
| 68 | $response = $this->img($param).$this->input(); |
||
| 69 | } else { |
||
| 70 | throw new Exception("Error Processing Captcha Parameters", 1); |
||
| 71 | } |
||
| 72 | |||
| 73 | return '<form method="post" action="'.$url.'">'.$response.'</form>'; |
||
| 74 | } |
||
| 75 | |||
| 76 | public function name($name) |
||
| 77 | { |
||
| 78 | $this->name = $name; |
||
| 79 | return $this; |
||
| 80 | } |
||
| 81 | |||
| 82 | public function verify($code) |
||
|
0 ignored issues
–
show
verify uses the super-global variable $_SESSION which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
Loading history...
verify uses the super-global variable $_POST which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
Loading history...
|
|||
| 83 | { |
||
| 84 | $this->startSession(); |
||
| 85 | |||
| 86 | $n = $this->name; |
||
| 87 | |||
| 88 | $valid = isset($_SESSION[$n]) |
||
| 89 | && isset($_POST[$n]) |
||
| 90 | && strlen($_POST[$n]) |
||
| 91 | && ($_SESSION[$n] === crypt(strtolower($_POST[$n]), $this->salt())); |
||
| 92 | |||
| 93 | if (isset($_POST[$n])) { |
||
| 94 | unset($_POST[$n]); |
||
| 95 | } |
||
| 96 | |||
| 97 | if ($valid && isset($_SESSION[$n])) { |
||
| 98 | unset($_SESSION[$n]); |
||
| 99 | } |
||
| 100 | |||
| 101 | return $valid; |
||
| 102 | } |
||
| 103 | |||
| 104 | private function startSession() |
||
| 105 | { |
||
| 106 | session_id() || session_start(); |
||
| 107 | } |
||
| 108 | |||
| 109 | private function setSession($string) |
||
|
0 ignored issues
–
show
setSession uses the super-global variable $_SESSION which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
Loading history...
|
|||
| 110 | { |
||
| 111 | $this->startSession(); |
||
| 112 | $_SESSION[$this->name] = crypt(strtolower($string), $this->salt()); |
||
| 113 | } |
||
| 114 | |||
| 115 | private function _generate($length) |
||
| 116 | { |
||
| 117 | return $this->code = substr(str_shuffle(str_repeat($this->pool, 5)), 0, $length); |
||
| 118 | } |
||
| 119 | |||
| 120 | private static function salt() |
||
| 121 | { |
||
| 122 | return md5(__FILE__.filemtime(__FILE__)); |
||
| 123 | } |
||
| 124 | } |
||
| 125 |