Passed
Push — master ( 3dc74f...81d6e9 )
by Felipe
04:17
created

AntiSpam::Rand()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 3
nop 1
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * JPGraph v3.6.15
5
 */
6
namespace Amenadiel\JpGraph\Image;
7
8
/**
9
 * // File:        JPGRAPH_ANTISPAM.PHP
10
 * // Description: Genarate anti-spam challenge
11
 * // Created:     2004-10-07
12
 * // Ver:         $Id: jpgraph_antispam.php 1106 2009-02-22 20:16:35Z ljp $
13
 * //
14
 * // Copyright (c) Asial Corporation. All rights reserved.
15
 */
16
class AntiSpam
17
{
18
    private $iData = '';
19
    private $iDD;
20
21 1
    public function __construct($aData = '')
22
    {
23 1
        $this->iData = $aData;
24 1
        $this->iDD   = new HandDigits();
25 1
    }
26
27
    public function Set($aData)
28
    {
29
        $this->iData = $aData;
30
    }
31
32 1
    public function Rand($aLen)
33
    {
34 1
        $d = '';
35 1
        for ($i = 0; $i < $aLen; ++$i) {
36 1
            if (rand(0, 9) < 6) {
37
                // Digits
38 1
                $d .= chr(ord('1') + rand(0, 8));
39
            } else {
40
                // Letters
41
                do {
42 1
                    $offset = rand(0, 25);
43 1
                } while ($offset == 14);
44 1
                $d .= chr(ord('a') + $offset);
45
            }
46
        }
47 1
        $this->iData = $d;
48
49 1
        return $d;
50
    }
51
52 1
    public function Stroke()
53
    {
54 1
        $n = strlen($this->iData);
55 1
        if ($n == 0) {
56
            return false;
57
        }
58
59 1
        for ($i = 0; $i < $n; ++$i) {
60 1
            if ($this->iData[$i] === '0' || strtolower($this->iData[$i]) === 'o') {
61
                return false;
62
            }
63
        }
64
65 1
        $img = @imagecreatetruecolor($n * $this->iDD->iWidth, $this->iDD->iHeight);
66 1
        if ($img < 1) {
67
            return false;
68
        }
69
70 1
        $start = 0;
71 1
        for ($i = 0; $i < $n; ++$i) {
72 1
            $dimg = imagecreatefromstring(base64_decode($this->iDD->chars[strtolower($this->iData[$i])][1], true));
73 1
            imagecopy($img, $dimg, $start, 0, 0, 0, imagesx($dimg), $this->iDD->iHeight);
74 1
            $start += imagesx($dimg);
75
        }
76 1
        $resimg = @imagecreatetruecolor($start + 4, $this->iDD->iHeight + 4);
77 1
        if ($resimg < 1) {
78
            return false;
79
        }
80
81 1
        imagecopy($resimg, $img, 2, 2, 0, 0, $start, $this->iDD->iHeight);
82 1
        header('Content-type: image/jpeg');
83 1
        imagejpeg($resimg);
84
85 1
        return true;
86
    }
87
}
88