Completed
Pull Request — master (#545)
by Richard
09:25
created

Upgrade_240   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 215
Duplicated Lines 59.07 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 127
loc 215
rs 8.3999
c 0
b 0
f 0
wmc 38
lcom 1
cbo 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * Upgrader from 2.3.3 to 2.4.0
14
 *
15
 * See the enclosed file license.txt for licensing information.
16
 * If you did not receive this file, get it at http://www.gnu.org/licenses/gpl-2.0.html
17
 *
18
 * @copyright    (c) 2000-2016 XOOPS Project (www.xoops.org)
19
 * @license          GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
20
 * @package          upgrader
21
 * @since            2.4.0
22
 * @author           Taiwen Jiang <[email protected]>
23
 * @author           trabis <[email protected]>
24
 */
25
class Upgrade_240 extends XoopsUpgrade
26
{
27
    /**
28
     * @return bool
29
     */
30
    public function check_version()
31
    {
32
        if (defined('XOOPS_LICENSE_KEY')) {
33
            return true; // skip setup if license.php was included
34
        }
35
        if (defined('XOOPS_LICENSE_KEY') == false) {
36
            return false;
37
        } elseif (XOOPS_LICENSE_KEY == '000000-000000-000000-000000-000000') {
38
            return false;
39
        } else {
40
            return true;
41
        }
42
    }
43
44
    /**
45
     * @return bool|string
46
     */
47
    public function apply_version()
48
    {
49
        set_time_limit(120);
50
        chmod('../include/license.php', 0777);
51
        if (!is_writable('../include/license.php')) {
52
            echo "<p><span style='color:#ff0000;'>&nbsp;include/license.php - is not writeable</span> - Windows Read Only (Off) / UNIX chmod 0777</p>";
53
54
            return false;
55
        }
56
57
        return @$this->xoops_putLicenseKey($this->xoops_buildLicenceKey(), XOOPS_ROOT_PATH . '/include/license.php', __DIR__ . '/license.dist.php');
58
    }
59
60
    /**
61
     * *#@+
62
     * Xoops Write Licence System Key
63
     */
64
    public function xoops_putLicenseKey($system_key, $licensefile, $license_file_dist = 'license.dist.php')
65
    {
66
        chmod($licensefile, 0777);
67
        $fver     = fopen($licensefile, 'w');
68
        $fver_buf = file($license_file_dist);
69
        foreach ($fver_buf as $line => $value) {
70
            if (strpos($value, 'XOOPS_LICENSE_KEY') > 0) {
71
                $ret = 'define(\'XOOPS_LICENSE_KEY\', \'' . $system_key . "');";
72
            } else {
73
                $ret = $value;
74
            }
75
            fwrite($fver, $ret, strlen($ret));
76
        }
77
        fclose($fver);
78
        chmod($licensefile, 0444);
79
80
        return 'Written License Key: ' . $system_key;
81
    }
82
83
    /**
84
     * *#@+
85
     * Xoops Build Licence System Key
86
     */
87
    public function xoops_buildLicenceKey()
88
    {
89
        $xoops_serdat = array();
90
        mt_srand(((float)('0' . substr(microtime(), strpos(microtime(), ' ') + 1, strlen(microtime()) - strpos(microtime(), ' ') + 1))) * mt_rand(30, 99999));
91
        mt_srand(((float)('0' . substr(microtime(), strpos(microtime(), ' ') + 1, strlen(microtime()) - strpos(microtime(), ' ') + 1))) * mt_rand(30, 99999));
92
        $checksums = array(1 => 'md5', 2 => 'sha1');
93
        $type      = mt_rand(1, 2);
94
        $func      = $checksums[$type];
95
96
        error_reporting(0);
97
98
        // Public Key
99
        if ($xoops_serdat['version'] = $func(XOOPS_VERSION)) {
100
            $xoops_serdat['version'] = substr($xoops_serdat['version'], 0, 6);
101
        }
102
        if ($xoops_serdat['licence'] = $func(XOOPS_LICENSE_CODE)) {
103
            $xoops_serdat['licence'] = substr($xoops_serdat['licence'], 0, 2);
104
        }
105
        if ($xoops_serdat['license_text'] = $func(XOOPS_LICENSE_TEXT)) {
106
            $xoops_serdat['license_text'] = substr($xoops_serdat['license_text'], 0, 2);
107
        }
108
109
        if ($xoops_serdat['domain_host'] = $func($_SERVER['HTTP_HOST'])) {
110
            $xoops_serdat['domain_host'] = substr($xoops_serdat['domain_host'], 0, 2);
111
        }
112
113
        // Private Key
114
        $xoops_serdat['file']     = $func(__FILE__);
115
        $xoops_serdat['basename'] = $func(basename(__FILE__));
116
        $xoops_serdat['path']     = $func(__DIR__);
117
118
        foreach ($_SERVER as $key => $data) {
119
            $xoops_serdat[$key] = substr($func(serialize($data)), 0, 4);
120
        }
121
122
        foreach ($xoops_serdat as $key => $data) {
123
            $xoops_key .= $data;
124
        }
125
        while (strlen($xoops_key) > 40) {
126
            $lpos      = mt_rand(18, strlen($xoops_key));
127
            $xoops_key = substr($xoops_key, 0, $lpos) . substr($xoops_key, $lpos + 1, strlen($xoops_key) - ($lpos + 1));
128
        }
129
130
        return $this->xoops_stripeKey($xoops_key);
131
    }
132
133
    /**
134
     * *#@+
135
     * Xoops Stripe Licence System Key
136
     */
137
    public function xoops_stripeKey($xoops_key)
138
    {
139
        $uu     = 0;
140
        $num    = 6;
141
        $length = 30;
142
        $strip  = floor(strlen($xoops_key) / 6);
143
        for ($i = 0; $i < strlen($xoops_key); ++$i) {
144
            if ($i < $length) {
145
                ++$uu;
146
                if ($uu == $strip) {
147
                    $ret .= substr($xoops_key, $i, 1) . '-';
148
                    $uu = 0;
149
                } else {
150
                    if (substr($xoops_key, $i, 1) != '-') {
151
                        $ret .= substr($xoops_key, $i, 1);
152
                    } else {
153
                        $uu--;
154
                    }
155
                }
156
            }
157
        }
158
        $ret = str_replace('--', '-', $ret);
159
        if (substr($ret, 0, 1) == '-') {
160
            $ret = substr($ret, 2, strlen($ret));
161
        }
162
        if (substr($ret, strlen($ret) - 1, 1) == '-') {
163
            $ret = substr($ret, 0, strlen($ret) - 1);
164
        }
165
166
        return $ret;
167
    }
168
169
    /**
170
     * Check if keys already exist
171
     *
172
     */
173
    public function check_keys()
174
    {
175
        $tables['modules']       = array('isactive', 'weight', 'hascomments');
176
        $tables['users']         = array('level');
177
        $tables['online']        = array('online_updated', 'online_uid');
178
        $tables['config']        = array('conf_order');
179
        $tables['xoopscomments'] = array('com_status');
180
181
        foreach ($tables as $table => $keys) {
182
            $sql = 'SHOW KEYS FROM `' . $GLOBALS['xoopsDB']->prefix($table) . '`';
183
            if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
184
                continue;
185
            }
186
            $existing_keys = array();
187
            while ($row = $GLOBALS['xoopsDB']->fetchArray($result)) {
188
                $existing_keys[] = $row['Key_name'];
189
            }
190
            foreach ($keys as $key) {
191
                if (!in_array($key, $existing_keys)) {
192
                    return false;
193
                }
194
            }
195
        }
196
197
        return true;
198
    }
199
200
    /**
201
     * Apply keys that are missing
202
     *
203
     */
204
    public function apply_keys()
205
    {
206
        $tables['modules']       = array('isactive', 'weight', 'hascomments');
207
        $tables['users']         = array('level');
208
        $tables['online']        = array('online_updated', 'online_uid');
209
        $tables['config']        = array('conf_order');
210
        $tables['xoopscomments'] = array('com_status');
211
212
        foreach ($tables as $table => $keys) {
213
            $sql = 'SHOW KEYS FROM `' . $GLOBALS['xoopsDB']->prefix($table) . '`';
214
            if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
215
                continue;
216
            }
217
            $existing_keys = array();
218
            while ($row = $GLOBALS['xoopsDB']->fetchArray($result)) {
219
                $existing_keys[] = $row['Key_name'];
220
            }
221
            foreach ($keys as $key) {
222
                if (!in_array($key, $existing_keys)) {
223
                    $sql = 'ALTER TABLE `' . $GLOBALS['xoopsDB']->prefix($table) . "` ADD INDEX `{$key}` (`{$key}`)";
224
                    if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
225
                        return false;
226
                    }
227
                }
228
            }
229
        }
230
231
        return true;
232
    }
233
234
    public function __construct()
235
    {
236
        parent::__construct(basename(__DIR__));
237
        $this->tasks = array('keys', 'version');
238
    }
239
}
240
241
$upg = new Upgrade_240();
242
return $upg;
243