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

XoopsCaptchaImage::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
 * Image Creation class for CAPTCHA
5
 *
6
 * D.J.
7
 */
8
class XoopsCaptchaImage
9
{
10
    public $config = array();
11
12
    /**
13
     * XoopsCaptchaImage constructor.
14
     */
15
    public function __construct()
16
    {
17
        //$this->name = md5( session_id() );
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
18
    }
19
20
    /**
21
     * @return XoopsCaptchaImage
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
    /**
44
     * @return string
45
     */
46
    public function render()
47
    {
48
        $form = "<input type='text' name='" .
49
                $this->config['name'] .
50
                "' id='" .
51
                $this->config['name'] .
52
                "' size='" .
53
                $this->config['num_chars'] .
54
                "' maxlength='" .
55
                $this->config['num_chars'] .
56
                "' value='' /> &nbsp; " .
57
                $this->loadImage();
58
        $rule = htmlspecialchars(XOOPS_CAPTCHA_REFRESH, ENT_QUOTES);
59
        if ($this->config['maxattempt']) {
60
            $rule .= ' | ' . sprintf(constant('XOOPS_CAPTCHA_MAXATTEMPTS'), $this->config['maxattempt']);
61
        }
62
        $form .= "&nbsp;&nbsp;<small>{$rule}</small>";
63
64
        return $form;
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function loadImage()
71
    {
72
        $rule = $this->config['casesensitive'] ? constant('XOOPS_CAPTCHA_RULE_CASESENSITIVE') : constant('XOOPS_CAPTCHA_RULE_CASEINSENSITIVE');
73
        $ret  = "<img id='captcha' src='" .
74
                XOOPS_URL .
75
                '/' .
76
                $this->config['imageurl'] .
77
                "' onclick=\"this.src='" .
78
                XOOPS_URL .
79
                '/' .
80
                $this->config['imageurl'] .
81
                "?refresh='+Math.random()" .
82
                "\" align='absmiddle'  style='cursor: pointer;' alt='" .
83
                htmlspecialchars($rule, ENT_QUOTES) .
84
                "' />";
85
86
        return $ret;
87
    }
88
}
89