Completed
Push — master ( 01b1a5...81f493 )
by Michael
04:03
created

XoopsCaptchaText::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
/**
4
 * Text form for CAPTCHA
5
 *
6
 * D.J.
7
 */
8
class XoopsCaptchaText
9
{
10
    public $config = array();
11
    public $code;
12
13
    /**
14
     * XoopsCaptchaText constructor.
15
     */
16
    public function __construct()
17
    {
18
    }
19
20
    /**
21
     * @return XoopsCaptchaText
22
     */
23
    public static function getInstance()
24
    {
25
        static $instance;
26
        if (null === $instance) {
27
            $instance = new static();
28
        }
29
30
        return $instance;
31
    }
32
33
    /**
34
     * Loading configs from CAPTCHA class
35
     * @param array $config
36
     */
37
    public function loadConfig($config = array())
38
    {
39
        // Loading default preferences
40
        $this->config = $config;
41
    }
42
43
    public function setCode()
44
    {
45
        $_SESSION['XoopsCaptcha_sessioncode'] = (string)$this->code;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function render()
52
    {
53
        $form = $this->loadText() .
54
                "&nbsp;&nbsp; <input type='text' name='" .
55
                $this->config['name'] .
56
                "' id='" .
57
                $this->config['name'] .
58
                "' size='" .
59
                $this->config['num_chars'] .
60
                "' maxlength='" .
61
                $this->config['num_chars'] .
62
                "' value='' />";
63
        $rule = constant('XOOPS_CAPTCHA_RULE_TEXT');
64
        if (!empty($rule)) {
65
            $form .= "&nbsp;&nbsp;<small>{$rule}</small>";
66
        }
67
68
        $this->setCode();
69
70
        return $form;
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function loadText()
77
    {
78
        $val_a = mt_rand(0, 9);
79
        $val_b = mt_rand(0, 9);
80
        if ($val_a > $val_b) {
81
            $expression = "{$val_a} - {$val_b} = ?";
82
            $this->code = $val_a - $val_b;
83
        } else {
84
            $expression = "{$val_a} + {$val_b} = ?";
85
            $this->code = $val_a + $val_b;
86
        }
87
88
        return "<span style='font-style: normal; font-weight: bold; font-size: 100%; font-color: #333; border: 1px solid #333; padding: 1px 5px;'>{$expression}</span>";
89
    }
90
}
91