pluginstore.php ➔ downloadFile()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 12
nop 1
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
1
<?php
2
if (file_exists("plugins")) {
3
    $plugins = json_decode(file_get_contents('http://cyberbyte.org.uk/hooks/cyberworks/plugins.php?id=' . $settings['id']), true);
4
5
    function downloadFile($url) {
6
        $newfname = realpath('downloading') . '/plugin.zip';
7
        $folder = realpath('plugins');
8
        $file = fopen($url, "rb");
9
        if ($file) {
10
            $newf = fopen($newfname, "wb");
11
            if ($newf) {
12
                while (!feof($file)) {
13
                    fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
14
            }
15
                }
16
        }
17
18
        if ($file) {
19
            fclose($file);
20
        }
21
        if ($newf) {
22
            fclose($newf);
0 ignored issues
show
Bug introduced by
The variable $newf does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
23
        }
24
25
        $zip = new ZipArchive;
26
        $zip->open($newfname);
27
        $zip->extractTo($folder);
28
        $zip->close();
29
        rrmdir(realpath('downloading'));
30
    }
31
32
    /**
33
     * @param string $dir
34
     */
35
    function rrmdir($dir) {
36
        if (is_dir($dir)) {
37
            $objects = scandir($dir);
38
            foreach ($objects as $object) {
39
                if ($object != "." && $object != "..") {
40
                    if (filetype($dir . "/" . $object) == "dir") {
41
                        rrmdir($dir . "/" . $object);
42
                    } else {
43
                        unlink($dir . "/" . $object);
44
                    }
45
                }
46
            }
47
            reset($objects);
48
            rmdir($dir);
49
        }
50
    }
51
52
    if (isset($_GET['install'])) {
53
        $install = $_GET['install'];
54
        $key = array_search($install, array_column($plugins, 'dirname'));
55
        downloadFile($plugins[$key + 1]['url']);
56
    }
57
58
    if (isset($_GET['activate'])) {
59
        if (!in_array($_GET['activate'], $settings['plugins'])) {
60
            array_push($settings['plugins'], $_GET['activate']);
61
            $json = json_decode(file_get_contents('plugins/' . $_GET['activate'] . '/plugin.json'), true);
62
            if (isset($json['language']) && isset($json['short'])) {
63
                $lang = array($json['language'], $json['short']);
64
                array_push($settings['installedLanguage'], $lang);
65
            }
66
            if (isset($json['defaultSettings'])) {
67
                foreach ($json['defaultSettings'] as $key => $setting) {
68
                    $settings[$key] = $setting;
69
70
                }
71
            }
72
            file_put_contents('config/settings.php', '<?php return ' . var_export($settings, true) . ';');
73
            message($lang['pluginActivated']);
74
        } message($lang['activeAlready']);
75
    }
76
77
    if (isset($_GET['deactivate'])) {
78
        if (in_array($_GET['deactivate'], $settings['plugins'])) {
79
            if (array_count_values($settings['plugins']) <= 1) {
80
                $settings['plugins'] = array();
81
            } else {
82
                $settings['plugins'] = array_diff($settings['plugins'], array($_GET['deactivate']));
83
            }
84
            
85
            $json = json_decode(file_get_contents('plugins/' . $_GET['deactivate'] . '/plugin.json'), true);
86
            if (isset($json['language']) && isset($json['short'])) {
87
                $key = array_search($_GET['deactivate'], array_column($settings['installedLanguage'], 0));
88
                unset($settings['installedLanguage'][$key]);
89
            }
90
            file_put_contents('config/settings.php', '<?php return ' . var_export($settings, true) . ';');
91
        } message($lang['notActive']);
92
    }
93
94
    if (isset($_GET['delete'])) {
95
        rrmdir(realpath('plugins') . '/' . $_GET['delete']);
96
    }
97
    ?>
98
99
        <div class="row">
100
            <div class="col-lg-8">
101
                <h1 class="page-header">
102
                    <?php echo $lang['pluginstore']; ?>
103
                </h1>
104
            </div>
105
        </div>
106
    <?php
107
    if (!isset($plugins['verify'])) {
108
        echo "<form method='post' action='plugins' name='plugins'>";
109
110
        $files = scandir("plugins");
111
        unset($files[0]);
112
        unset($files[1]);
113
114
        foreach ($files as &$file) {
115
            $path = 'plugins/' . $file . '/plugin.json';
116
            $installed[] = json_decode(file_get_contents($path), true);
117
        }
118
119
        $dropIn = array_diff($files, array_column($plugins, 'dirname'));
120
        if ($dropIn > 0) echo "<h2 style='margin-top: 0;'>" . $lang['dropIn'] . "</h2>";
121
        
122
        foreach ($dropIn as &$plugin) {
123
            $key = array_search($plugin, array_column($installed, 'dirname'));
124
            echo '<div class="col-sm-6 col-md-4"><div class="thumbnail"><div class="caption">';
125
            echo '<h3>' . $installed[$key]['name'] . ' - ' . $installed[$key]['version'];
126
127
            if (in_array($plugin, $settings['plugins']))
128
            echo '<small style="padding-left: 6px;"><span class="label label-success">' . $lang['active'] . '</span></small>';
129
            else echo '<small style="padding-left: 6px;"><span class="label label-default">' . $lang['installed'] . '</span></small>';
130
131
            echo '</h3><p><strong>' . $installed[$key]['author'] . '</strong> - ' . $installed[$key]['description'] . '</p>';
132
133
            if (in_array($plugin, $settings['plugins']))
134
            echo '<p><a href="?deactivate=' . $plugin . '" class="btn btn-primary" role="button">' . $lang['deactivate'] . '</a>';
135
            else echo '<p><a href="?activate=' . $plugin . '" class="btn btn-primary" role="button">' . $lang['activate'] . '</a>';
136
137
            if (isset($installed[$key]['authurl'])) echo '<a style="margin-left: 5px;" href="' . $installed[$key]['authurl'] . '" class="btn btn-default" role="button">' . $lang['visitSite'] . '</a>';
138
            echo '<a style="float: right;" href="?delete=' . $plugin . '" class="btn btn-danger" role="button">' . $lang['delete'] . '</a>';
139
            echo '</p></div></div></div>';
140
        }
141
        echo "<div class='row'></div><h2>" . $lang['pluginstore'] . "</h2>";
142
143
        foreach ($plugins as &$plugin) {
144
            echo '<div class="col-sm-6 col-md-4"><div class="thumbnail">';
145
            echo '<img  src="' . $plugin['image'] . '" alt="' . $plugin['name'] . '">';
146
            echo '<div class="caption">';
147
            echo '<h3>' . $plugin['name'] . ' - ' . $plugin['version'];
148
149
            $key = array_search($plugin['dirname'], array_column($installed, 'dirname'));
150
151
            if ($key !== false) {
152
                if (in_array($plugin['dirname'], $settings['plugins'])) {
153
                    if ($plugin['version'] > $installed[$key]['version']) {
154
                        echo '<small style="padding-left: 6px;"><span class="label label-info">' . $lang['updateAvalible'] . '</span></small>';
155
                    }
156
                    // Update avalible
157
                    else {
158
                        echo '<small style="padding-left: 6px;"><span class="label label-success">' . $lang['active'] . '</span>';
159
                    }
160
                    // Active plugin
161
                } elseif (in_array($plugin['dirname'], $files)) {
162
                    echo '<small style="padding-left: 6px;"><span class="label label-default">' . $lang['installed'] . '</span></small>';
163
                }
164
                // Installed not active
165
            }
166
            echo '</h3><p><strong>' . $plugin['author'] . '</strong> - ' . $plugin['description'] . '</p>';
167
168
            if ($key !== false) {
169
                if (in_array($plugin['dirname'], $settings['plugins'])) {
170
                    if ($plugin['version'] > $installed[$key]['version']) {
171
                        echo '<div><a href="?install=' . $plugin['dirname'] . '" class="btn btn-primary" role="button">' . $lang['update'] . '</a>';
172
                    }
173
                    // Update avalible
174
                    else {
175
                        echo '<p><a href="?deactivate=' . $plugin['dirname'] . '" class="btn btn-primary" role="button">' . $lang['deactivate'] . '</a>';
176
                    }
177
                    // Active plugin
178 View Code Duplication
                } elseif (in_array($plugin['dirname'], $files)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
                    echo '<p><a href="?activate=' . $plugin['dirname'] . '" class="btn btn-primary" role="button">' . $lang['activate'] . '</a>';
180
                }
181
                // Installed not active
182
            } else {
183
                echo '<p><a href="?install=' . $plugin['dirname'] . '" class="btn btn-primary" role="button">' . $lang['install'] . '</a>';
184
            }
185
            // Not installed
186
187
            if (isset($plugin['authurl'])) {
188
                echo '<a style="margin-left: 5px;" href="' . $plugin['authurl'] . '" class="btn btn-default" role="button">' . $lang['visitSite'] . '</a>';
189
            }
190 View Code Duplication
            if (in_array($plugin['dirname'], $files)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
                echo '<a style="float: right;" href="?delete=' . $plugin['dirname'] . '" class="btn btn-danger" role="button">' . $lang['delete'] . '</a></div>';
192
            }
193
            echo '</div></div></div>';
194
        }
195
196
        echo '</div></div></form>';
197
    } else {
198
        echo '<h2>' . $lang['notverified'] . '</h2>';
199
    }
200
    } else {
201
    echo '<h2>' . $lang['noPlugin'] . '</h2>';
202
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
203