Failed Conditions
Push — psr2 ( 1cdd00...29fc53 )
by Andreas
06:39 queued 03:24
created

admin_plugin_popularity::enableAutosubmit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Popularity Feedback Plugin
4
 *
5
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6
 * @author     Andreas Gohr <[email protected]>
7
 */
8
class admin_plugin_popularity extends DokuWiki_Admin_Plugin
9
{
10
11
    /** @var helper_plugin_popularity */
12
    protected $helper;
13
    protected $sentStatus = null;
14
15
    /**
16
     * admin_plugin_popularity constructor.
17
     */
18
    public function __construct()
19
    {
20
        $this->helper = $this->loadHelper('popularity', false);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->loadHelper('popularity', false) can also be of type object<DokuWiki_PluginInterface>. However, the property $helper is declared as type object<helper_plugin_popularity>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
21
    }
22
23
    /**
24
     * return prompt for admin menu
25
     * @param $language
26
     * @return string
27
     */
28
    public function getMenuText($language)
29
    {
30
        return $this->getLang('name');
31
    }
32
33
    /**
34
     * return sort order for position in admin menu
35
     */
36
    public function getMenuSort()
37
    {
38
        return 2000;
39
    }
40
41
    /**
42
     * Accessible for managers
43
     */
44
    public function forAdminOnly()
45
    {
46
        return false;
47
    }
48
49
50
    /**
51
     * handle user request
52
     */
53
    public function handle()
54
    {
55
        global $INPUT;
56
57
        //Send the data
58
        if ($INPUT->has('data')) {
59
            $this->sentStatus = $this->helper->sendData($INPUT->str('data'));
60
            if ($this->sentStatus === '') {
61
                //Update the last time we sent the data
62
                touch($this->helper->popularityLastSubmitFile);
63
            }
64
            //Deal with the autosubmit option
65
            $this->enableAutosubmit($INPUT->has('autosubmit'));
66
        }
67
    }
68
69
    /**
70
     * Enable or disable autosubmit
71
     * @param bool $enable If TRUE, it will enable autosubmit. Else, it will disable it.
72
     */
73
    protected function enableAutosubmit($enable)
74
    {
75
        if ($enable) {
76
            io_saveFile($this->helper->autosubmitFile, ' ');
77
        } else {
78
            @unlink($this->helper->autosubmitFile);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
79
        }
80
    }
81
82
    /**
83
     * Output HTML form
84
     */
85
    public function html()
86
    {
87
        global $INPUT;
88
89
        if (! $INPUT->has('data')) {
90
            echo $this->locale_xhtml('intro');
91
92
            //If there was an error the last time we tried to autosubmit, warn the user
93
            if ($this->helper->isAutoSubmitEnabled()) {
94
                if (file_exists($this->helper->autosubmitErrorFile)) {
95
                    echo $this->getLang('autosubmitError');
96
                    echo io_readFile($this->helper->autosubmitErrorFile);
97
                }
98
            }
99
100
            flush();
101
            echo $this->buildForm('server');
102
103
            //Print the last time the data was sent
104
            $lastSent = $this->helper->lastSentTime();
105
            if ($lastSent !== 0) {
106
                echo $this->getLang('lastSent') . ' ' . datetime_h($lastSent);
107
            }
108
        } else {
109
            //If we just submitted the form
110
            if ($this->sentStatus === '') {
111
                //If we successfully sent the data
112
                echo $this->locale_xhtml('submitted');
113
            } else {
114
                //If we failed to submit the data, try directly with the browser
115
                echo $this->getLang('submissionFailed') . $this->sentStatus . '<br />';
116
                echo $this->getLang('submitDirectly');
117
                echo $this->buildForm('browser', $INPUT->str('data'));
118
            }
119
        }
120
    }
121
122
123
    /**
124
     * Build the form which presents the data to be sent
125
     * @param string $submissionMode How is the data supposed to be sent? (may be: 'browser' or 'server')
126
     * @param string $data   The popularity data, if it has already been computed. NULL otherwise.
127
     * @return string The form, as an html string
128
     */
129
    protected function buildForm($submissionMode, $data = null)
130
    {
131
        $url = ($submissionMode === 'browser' ? $this->helper->submitUrl : script());
132
        if (is_null($data)) {
133
            $data = $this->helper->gatherAsString();
134
        }
135
136
        $form = '<form method="post" action="'. $url  .'" accept-charset="utf-8">'
137
            .'<fieldset style="width: 60%;">'
138
            .'<textarea class="edit" rows="10" cols="80" readonly="readonly" name="data">'
139
            .$data
140
            .'</textarea><br />';
141
142
        //If we submit via the server, we give the opportunity to suscribe to the autosubmission option
143
        if ($submissionMode !== 'browser') {
144
            $form .= '<label for="autosubmit">'
145
                .'<input type="checkbox" name="autosubmit" id="autosubmit" '
146
                .($this->helper->isAutosubmitEnabled() ? 'checked' : '' )
147
                .'/> ' . $this->getLang('autosubmit') .'<br />'
148
                .'</label>'
149
                .'<input type="hidden" name="do" value="admin" />'
150
                .'<input type="hidden" name="page" value="popularity" />';
151
        }
152
        $form .= '<button type="submit">'.$this->getLang('submit').'</button>'
153
            .'</fieldset>'
154
            .'</form>';
155
        return $form;
156
    }
157
}
158