Completed
Push — cli ( eb8d78 )
by Andreas
03:57
created

admin_plugin_popularity::_enableAutosubmit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
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
// must be run within Dokuwiki
9
if(!defined('DOKU_INC')) die();
10
11
/**
12
 * All DokuWiki plugins to extend the admin function
13
 * need to inherit from this class
14
 */
15
class admin_plugin_popularity extends DokuWiki_Admin_Plugin {
16
17
    /**
18
     * @var helper_plugin_popularity
19
     */
20
    var $helper;
21
    var $sentStatus = null;
22
23
    function __construct(){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
24
        $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...
25
    }
26
27
    /**
28
     * return prompt for admin menu
29
     */
30
    function getMenuText($language) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
31
        return $this->getLang('name');
32
    }
33
34
    /**
35
     * return sort order for position in admin menu
36
     */
37
    function getMenuSort() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
38
        return 2000;
39
    }
40
41
    /**
42
     * Accessible for managers
43
     */
44
    function forAdminOnly() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
45
        return false;
46
    }
47
48
49
    /**
50
     * handle user request
51
     */
52
    function handle() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
53
        global $INPUT;
54
55
        //Send the data
56
        if ( $INPUT->has('data') ){
57
            $this->sentStatus = $this->helper->sendData( $INPUT->str('data') );
58
            if ( $this->sentStatus === '' ){
59
                //Update the last time we sent the data
60
                touch ( $this->helper->popularityLastSubmitFile );
61
            }
62
            //Deal with the autosubmit option
63
            $this->_enableAutosubmit( $INPUT->has('autosubmit') );
64
        }
65
    }
66
67
    /**
68
     * Enable or disable autosubmit
69
     * @param bool $enable If TRUE, it will enable autosubmit. Else, it will disable it.
70
     */
71
    function _enableAutosubmit( $enable ){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
72
        if ( $enable ){
73
            io_saveFile( $this->helper->autosubmitFile, ' ');
74
        } else {
75
            @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...
76
        }
77
    }
78
79
    /**
80
     * Output HTML form
81
     */
82
    function html() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
83
        global $INPUT;
84
85
        if ( ! $INPUT->has('data') ){
86
            echo $this->locale_xhtml('intro');
87
88
            //If there was an error the last time we tried to autosubmit, warn the user
89
            if ( $this->helper->isAutoSubmitEnabled() ){
90
                if ( file_exists($this->helper->autosubmitErrorFile) ){
91
                    echo $this->getLang('autosubmitError');
92
                    echo io_readFile( $this->helper->autosubmitErrorFile );
93
                }
94
            }
95
96
            flush();
97
            echo $this->buildForm('server');
98
99
            //Print the last time the data was sent
100
            $lastSent = $this->helper->lastSentTime();
101
            if ( $lastSent !== 0 ){
102
                echo $this->getLang('lastSent') . ' ' . datetime_h($lastSent);
103
            }
104
        } else {
105
            //If we just submitted the form
106
            if ( $this->sentStatus === '' ){
107
                //If we successfully sent the data
108
                echo $this->locale_xhtml('submitted');
109
            } else {
110
                //If we failed to submit the data, try directly with the browser
111
                echo $this->getLang('submissionFailed') . $this->sentStatus . '<br />';
112
                echo $this->getLang('submitDirectly');
113
                echo $this->buildForm('browser', $INPUT->str('data'));
114
            }
115
        }
116
    }
117
118
119
    /**
120
     * Build the form which presents the data to be sent
121
     * @param string $submissionMode How is the data supposed to be sent? (may be: 'browser' or 'server')
122
     * @param string $data   The popularity data, if it has already been computed. NULL otherwise.
123
     * @return string The form, as an html string
124
     */
125
    function buildForm($submissionMode, $data = null){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
126
        $url = ($submissionMode === 'browser' ? $this->helper->submitUrl : script());
127
        if ( is_null($data) ){
128
            $data = $this->helper->gatherAsString();
129
        }
130
131
        $form = '<form method="post" action="'. $url  .'" accept-charset="utf-8">'
132
            .'<fieldset style="width: 60%;">'
133
            .'<textarea class="edit" rows="10" cols="80" readonly="readonly" name="data">'
134
            .$data
135
            .'</textarea><br />';
136
137
        //If we submit via the server, we give the opportunity to suscribe to the autosubmission option
138
        if ( $submissionMode !== 'browser' ){
139
            $form .= '<label for="autosubmit">'
140
                .'<input type="checkbox" name="autosubmit" id="autosubmit" '
141
                .($this->helper->isAutosubmitEnabled() ? 'checked' : '' )
142
                .'/> ' . $this->getLang('autosubmit') .'<br />'
143
                .'</label>'
144
                .'<input type="hidden" name="do" value="admin" />'
145
                .'<input type="hidden" name="page" value="popularity" />';
146
        }
147
        $form .= '<button type="submit">'.$this->getLang('submit').'</button>'
148
            .'</fieldset>'
149
            .'</form>';
150
        return $form;
151
    }
152
}
153