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:
Complex classes like Upgrade_241 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Upgrade_241, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Upgrade_241 extends XoopsUpgrade |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @return bool |
||
| 29 | */ |
||
| 30 | public function check_license() |
||
| 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 | if (substr(XOOPS_LICENSE_KEY, 0, 13) != $this->xoops_getPublicLicenceKey()) { |
||
| 37 | return false; |
||
| 38 | } else { |
||
| 39 | return true; |
||
| 40 | } |
||
| 41 | } elseif (XOOPS_LICENSE_KEY == '000000-000000-000000-000000-000000') { |
||
| 42 | return false; |
||
| 43 | } else { |
||
| 44 | return true; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @return bool|string |
||
| 50 | */ |
||
| 51 | public function apply_license() |
||
| 52 | { |
||
| 53 | set_time_limit(120); |
||
| 54 | chmod('../include/license.php', 0777); |
||
| 55 | if (!is_writable('../include/license.php')) { |
||
| 56 | echo "<p><span style='color:#ff0000;'> include/license.php - is not writeable</span> - Windows Read Only (Off) / UNIX chmod 0777</p>"; |
||
| 57 | |||
| 58 | return false; |
||
| 59 | } |
||
| 60 | if (XOOPS_LICENSE_KEY == '000000-000000-000000-000000-000000') { |
||
| 61 | return @$this->xoops_putLicenseKey($this->xoops_buildLicenceKey(), XOOPS_ROOT_PATH . '/include/license.php', __DIR__ . '/license.dist.php'); |
||
| 62 | } else { |
||
| 63 | return @$this->xoops_upgradeLicenseKey($this->xoops_getPublicLicenceKey(), XOOPS_ROOT_PATH . '/include/license.php', __DIR__ . '/license.dist.php'); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * *#@+ |
||
| 69 | * Xoops Write Licence System Key |
||
| 70 | */ |
||
| 71 | public function xoops_upgradeLicenseKey($public_key, $licensefile, $license_file_dist = 'license.dist.php') |
||
| 72 | { |
||
| 73 | chmod($licensefile, 0777); |
||
| 74 | $fver = fopen($licensefile, 'w'); |
||
| 75 | $fver_buf = file($license_file_dist); |
||
| 76 | $license_key = $public_key . substr(XOOPS_LICENSE_KEY, 13, strlen(XOOPS_LICENSE_KEY) - 13); |
||
| 77 | foreach ($fver_buf as $line => $value) { |
||
| 78 | if (strpos($value, 'XOOPS_LICENSE_KEY') > 0) { |
||
| 79 | $ret = 'define(\'XOOPS_LICENSE_KEY\', \'' . $license_key . "');"; |
||
| 80 | } else { |
||
| 81 | $ret = $value; |
||
| 82 | } |
||
| 83 | fwrite($fver, $ret, strlen($ret)); |
||
| 84 | } |
||
| 85 | fclose($fver); |
||
| 86 | chmod($licensefile, 0444); |
||
| 87 | |||
| 88 | return 'Written License Key: ' . $license_key; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * *#@+ |
||
| 93 | * Xoops Write Licence System Key |
||
| 94 | */ |
||
| 95 | public function xoops_putLicenseKey($system_key, $licensefile, $license_file_dist = 'license.dist.php') |
||
| 96 | { |
||
| 97 | chmod($licensefile, 0777); |
||
| 98 | $fver = fopen($licensefile, 'w'); |
||
| 99 | $fver_buf = file($license_file_dist); |
||
| 100 | foreach ($fver_buf as $line => $value) { |
||
| 101 | if (strpos($value, 'XOOPS_LICENSE_KEY') > 0) { |
||
| 102 | $ret = 'define(\'XOOPS_LICENSE_KEY\', \'' . $system_key . "');"; |
||
| 103 | } else { |
||
| 104 | $ret = $value; |
||
| 105 | } |
||
| 106 | fwrite($fver, $ret, strlen($ret)); |
||
| 107 | } |
||
| 108 | fclose($fver); |
||
| 109 | chmod($licensefile, 0444); |
||
| 110 | |||
| 111 | return 'Written License Key: ' . $system_key; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * *#@+ |
||
| 116 | * Xoops Get Public Checkbit from Licence System Key |
||
| 117 | */ |
||
| 118 | public function xoops_getPublicLicenceKey() |
||
| 119 | { |
||
| 120 | $xoops_key = ''; |
||
| 121 | $xoops_serdat = array(); |
||
| 122 | $checksums = array(1 => 'md5', 2 => 'sha1'); |
||
| 123 | |||
| 124 | // Remember to upgrade versions string with each release there after. |
||
| 125 | $versions = array('XOOPS 2.4.0', 'XOOPS 2.4.1'); |
||
| 126 | |||
| 127 | error_reporting(E_ALL); |
||
| 128 | foreach ($checksums as $funcid => $func) { |
||
| 129 | foreach ($versions as $versionid => $version) { |
||
| 130 | if ($xoops_serdat['version'] = $func($version) && substr(XOOPS_LICENSE_KEY, 0, 6) === substr($func($version), 0, 6)) { |
||
| 131 | $xoops_serdat['version'] = substr($xoops_serdat['version'], 0, 6); |
||
| 132 | $checkbit = $func; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | if (isset($checkbit)) { |
||
| 137 | if ($xoops_serdat['licence'] = $checkbit(XOOPS_LICENSE_CODE)) { |
||
| 138 | $xoops_serdat['licence'] = substr($xoops_serdat['licence'], 0, 2); |
||
| 139 | } |
||
| 140 | if ($xoops_serdat['license_text'] = $checkbit(XOOPS_LICENSE_TEXT)) { |
||
| 141 | $xoops_serdat['license_text'] = substr($xoops_serdat['license_text'], 0, 2); |
||
| 142 | } |
||
| 143 | |||
| 144 | if ($xoops_serdat['domain_host'] = $checkbit($_SERVER['HTTP_HOST'])) { |
||
| 145 | $xoops_serdat['domain_host'] = substr($xoops_serdat['domain_host'], 0, 2); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | foreach ($xoops_serdat as $key => $data) { |
||
| 149 | $xoops_key .= $data; |
||
| 150 | } |
||
| 151 | |||
| 152 | return $this->xoops_stripeKey($xoops_key, 6, 13, 0); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * *#@+ |
||
| 157 | * Xoops Build Licence System Key |
||
| 158 | */ |
||
| 159 | public function xoops_buildLicenceKey() |
||
| 160 | { |
||
| 161 | $xoops_serdat = array(); |
||
| 162 | mt_srand(((float)('0' . substr(microtime(), strpos(microtime(), ' ') + 1, strlen(microtime()) - strpos(microtime(), ' ') + 1))) * mt_rand(30, 99999)); |
||
| 163 | mt_srand(((float)('0' . substr(microtime(), strpos(microtime(), ' ') + 1, strlen(microtime()) - strpos(microtime(), ' ') + 1))) * mt_rand(30, 99999)); |
||
| 164 | $checksums = array(1 => 'md5', 2 => 'sha1'); |
||
| 165 | $type = mt_rand(1, 2); |
||
| 166 | $func = $checksums[$type]; |
||
| 167 | $xoops_key = ''; |
||
| 168 | |||
| 169 | error_reporting(E_ALL); |
||
| 170 | |||
| 171 | // Public Key |
||
| 172 | if ($xoops_serdat['version'] = $func(XOOPS_VERSION)) { |
||
| 173 | $xoops_serdat['version'] = substr($xoops_serdat['version'], 0, 6); |
||
| 174 | } |
||
| 175 | if ($xoops_serdat['licence'] = $func(XOOPS_LICENSE_CODE)) { |
||
| 176 | $xoops_serdat['licence'] = substr($xoops_serdat['licence'], 0, 2); |
||
| 177 | } |
||
| 178 | if ($xoops_serdat['license_text'] = $func(XOOPS_LICENSE_TEXT)) { |
||
| 179 | $xoops_serdat['license_text'] = substr($xoops_serdat['license_text'], 0, 2); |
||
| 180 | } |
||
| 181 | |||
| 182 | if ($xoops_serdat['domain_host'] = $func($_SERVER['HTTP_HOST'])) { |
||
| 183 | $xoops_serdat['domain_host'] = substr($xoops_serdat['domain_host'], 0, 2); |
||
| 184 | } |
||
| 185 | |||
| 186 | // Private Key |
||
| 187 | $xoops_serdat['file'] = $func(__FILE__); |
||
| 188 | $xoops_serdat['basename'] = $func(basename(__FILE__)); |
||
| 189 | $xoops_serdat['path'] = $func(__DIR__); |
||
| 190 | |||
| 191 | foreach ($_SERVER as $key => $data) { |
||
| 192 | $xoops_serdat[$key] = substr($func(serialize($data)), 0, 4); |
||
| 193 | } |
||
| 194 | |||
| 195 | foreach ($xoops_serdat as $key => $data) { |
||
| 196 | $xoops_key .= $data; |
||
| 197 | } |
||
| 198 | while (strlen($xoops_key) > 40) { |
||
| 199 | $lpos = mt_rand(18, strlen($xoops_key)); |
||
| 200 | $xoops_key = substr($xoops_key, 0, $lpos) . substr($xoops_key, $lpos + 1, strlen($xoops_key) - ($lpos + 1)); |
||
| 201 | } |
||
| 202 | |||
| 203 | return $this->xoops_stripeKey($xoops_key); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * *#@+ |
||
| 208 | * Xoops Stripe Licence System Key |
||
| 209 | */ |
||
| 210 | public function xoops_stripeKey($xoops_key, $num = 6, $length = 30, $uu = 0) |
||
| 211 | { |
||
| 212 | $strip = floor(strlen($xoops_key) / 6); |
||
| 213 | $ret = 0; |
||
| 214 | for ($i = 0; $i < strlen($xoops_key); ++$i) { |
||
| 215 | if ($i < $length) { |
||
| 216 | ++$uu; |
||
| 217 | if ($uu == $strip) { |
||
| 218 | $ret .= substr($xoops_key, $i, 1) . '-'; |
||
| 219 | $uu = 0; |
||
| 220 | } else { |
||
| 221 | if (substr($xoops_key, $i, 1) != '-') { |
||
| 222 | $ret .= substr($xoops_key, $i, 1); |
||
| 223 | } else { |
||
| 224 | $uu--; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | } |
||
| 229 | $ret = str_replace('--', '-', $ret); |
||
| 230 | if (substr($ret, 0, 1) == '-') { |
||
| 231 | $ret = substr($ret, 2, strlen($ret)); |
||
| 232 | } |
||
| 233 | if (substr($ret, strlen($ret) - 1, 1) == '-') { |
||
| 234 | $ret = substr($ret, 0, strlen($ret) - 1); |
||
| 235 | } |
||
| 236 | |||
| 237 | return $ret; |
||
| 238 | } |
||
| 239 | |||
| 240 | public function __construct() |
||
| 241 | { |
||
| 242 | parent::__construct(basename(__DIR__)); |
||
| 243 | $this->tasks = array('license'); |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 249 |