Completed
Pull Request — develop (#716)
by Agel_Nash
06:39
created

Captcha   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setVeriword() 0 6 1
A output() 0 6 1
A makeText() 0 10 2
A drawText() 0 53 3
B drawImage() 0 30 1
A destroy() 0 3 1
1
<?php namespace EvolutionCMS\Support;
2
3
use EvolutionCMS\Interfaces\CaptchaInterface;
4
5
/**
6
 * ######
7
 * ## Verification Word
8
 * ######
9
 * ## This class generate an image with random text
10
 * ## to be used in form verification. It has visual
11
 * ## elements design to confuse OCR software preventing
12
 * ## the use of BOTS.
13
 * ##
14
 * #######
15
 * ## Author: Huda M Elmatsani
16
 * ## Email:   justhuda at netrada.co.id
17
 * ##
18
 * ## 25/07/2004
19
 * #######
20
 * ## Copyright (c) 2004 Huda M Elmatsani All rights reserved.
21
 * ## This program is free for any purpose use.
22
 * ########
23
 * ##
24
 * ## USAGE
25
 * ## create some image with noise texture, put in image directory,
26
 * ## rename to noise_#, see examples
27
 * ## put some true type font into font directory,
28
 * ## rename to font_#, see exmplae
29
 * ## you can search and put free font you like
30
 * ##
31
 * ## see sample.php for test and usage
32
 * ## sample URL: http://www.program-ruti.org/veriword/
33
 * ####
34
 */
35
class Captcha implements CaptchaInterface
0 ignored issues
show
Coding Style introduced by
The property $dir_font is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $dir_noise is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $im_width is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $im_height is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
36
{
37
    /* path to font directory*/
38
    public $dir_font   = "ttf/";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 3 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
39
    /* path to background image directory*/
40
    public $dir_noise  = "captcha/";
41
    public $word       = "";
42
    public $im_width   = 0;
43
    public $im_height  = 0;
44
    public $im;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $im. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
45
46
    public function __construct($w=200, $h=80) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $w. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $h. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
47
        /* create session to set word for verification */
48
        $this->setVeriword();
49
        $this->dir_font = MODX_BASE_PATH . 'assets/' . $this->dir_font;
50
        $this->im_width         = $w;
51
        $this->im_height        = $h;
52
    }
53
54
    public function setVeriword() {
0 ignored issues
show
Coding Style introduced by
setVeriword 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...
55
        /* create session variable for verification,
56
           you may change the session variable name */
57
        $this->word             = $this->makeText();
58
        $_SESSION['veriword']   = $this->word;
59
    }
60
61
    public function output() {
62
        /* output the image as jpeg */
63
        $this->drawImage();
64
        header("Content-type: image/jpeg");
65
        imagejpeg($this->im);
66
    }
67
68
    public function makeText() {
69
        $modx = evolutionCMS();
70
        // set default words
71
        $words="MODX,Access,Better,BitCode,Chunk,Cache,Desc,Design,Excell,Enjoy,URLs,TechView,Gerald,Griff,Humphrey,Holiday,Intel,Integration,Joystick,Join(),Oscope,Genetic,Light,Likeness,Marit,Maaike,Niche,Netherlands,Ordinance,Oscillo,Parser,Phusion,Query,Question,Regalia,Righteous,Snippet,Sentinel,Template,Thespian,Unity,Enterprise,Verily,Veri,Website,WideWeb,Yap,Yellow,Zebra,Zygote";
72
        $words = $modx->config['captcha_words'] ? $modx->config['captcha_words'] : $words;
73
        $arr_words = array_filter(array_map('trim', explode(',', $words)));
74
75
        /* pick one randomly for text verification */
76
        return (string) $arr_words[array_rand($arr_words)].rand(10,999);
77
    }
78
79
    public function drawText() {
80
        $dir = dir($this->dir_font);
81
        $fontstmp = array();
82
        while (false !== ($file = $dir->read())) {
83
            if(substr($file, -4) == '.ttf') {
84
                $fontstmp[] = $this->dir_font.$file;
85
            }
86
        }
87
        $dir->close();
88
        $text_font = (string) $fontstmp[array_rand($fontstmp)];
89
90
        /* angle for text inclination */
91
        $text_angle = rand(-9,9);
92
        /* initial text size */
93
        $text_size  = 30;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 2 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
94
        /* calculate text width and height */
95
        $box        = imagettfbbox ( $text_size, $text_angle, $text_font, $this->word);
96
        $text_width = $box[2]-$box[0]; //text width
97
        $text_height= $box[5]-$box[3]; //text height
0 ignored issues
show
Unused Code introduced by
$text_height is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
98
99
        /* adjust text size */
100
        $text_size  = round((20 * $this->im_width)/$text_width);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 2 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
101
102
        /* recalculate text width and height */
103
        $box        = imagettfbbox ( $text_size, $text_angle, $text_font, $this->word);
104
        $text_width = $box[2]-$box[0]; //text width
105
        $text_height= $box[5]-$box[3]; //text height
106
107
        /* calculate center position of text */
108
        $text_x         = ($this->im_width - $text_width)/2;
109
        $text_y         = ($this->im_height - $text_height)/2;
110
111
        /* create canvas for text drawing */
112
        $im_text        = imagecreate ($this->im_width, $this->im_height);
113
        $bg_color       = imagecolorallocate ($im_text, 255, 255, 255);
114
115
        /* pick color for text */
116
        $text_color     = imagecolorallocate ($im_text, 0, 51, 153);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 5 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
117
118
        /* draw text into canvas */
119
        imagettftext    (   $im_text,
120
            $text_size,
121
            $text_angle,
122
            $text_x,
123
            $text_y,
124
            $text_color,
125
            $text_font,
126
            $this->word);
127
128
        /* remove background color */
129
        imagecolortransparent($im_text, $bg_color);
130
        return $im_text;
131
    }
132
133
134
    public function drawImage() {
135
136
        /* pick one background image randomly from image directory */
137
        $img_file       = MODX_BASE_PATH . 'assets/' . $this->dir_noise."noise".rand(1,4).".jpg";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 7 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
138
139
        /* create "noise" background image from your image stock*/
140
        $noise_img      = @imagecreatefromjpeg ($img_file);
141
        $noise_width    = imagesx($noise_img);
142
        $noise_height   = imagesy($noise_img);
143
144
        /* resize the background image to fit the size of image output */
145
        $this->im       = imagecreatetruecolor($this->im_width,$this->im_height);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 7 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
146
        imagecopyresampled ($this->im,
147
            $noise_img,
148
            0, 0, 0, 0,
149
            $this->im_width,
150
            $this->im_height,
151
            $noise_width,
152
            $noise_height);
153
154
        /* put text image into background image */
155
        imagecopymerge (    $this->im,
156
            $this->drawText(),
157
            0, 0, 0, 0,
158
            $this->im_width,
159
            $this->im_height,
160
            70 );
161
162
        return $this->im;
163
    }
164
165
    public function destroy() {
166
        imagedestroy($this->im);
167
    }
168
}
169