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 CKFinder_Connector_Utils_Misc 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 CKFinder_Connector_Utils_Misc, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class CKFinder_Connector_Utils_Misc |
||
| 27 | { |
||
| 28 | function getErrorMessage($number, $arg = "") { |
||
| 29 | $langCode = 'en'; |
||
| 30 | if (!empty($_GET['langCode']) && preg_match("/^[a-z\-]+$/", $_GET['langCode'])) { |
||
| 31 | if (file_exists(CKFINDER_CONNECTOR_LANG_PATH . "/" . $_GET['langCode'] . ".php")) |
||
| 32 | $langCode = $_GET['langCode']; |
||
| 33 | } |
||
| 34 | include CKFINDER_CONNECTOR_LANG_PATH . "/" . $langCode . ".php"; |
||
| 35 | if ($number) { |
||
| 36 | if (!empty ($GLOBALS['CKFLang']['Errors'][$number])) { |
||
| 37 | $errorMessage = str_replace("%1", $arg, $GLOBALS['CKFLang']['Errors'][$number]); |
||
| 38 | } else { |
||
| 39 | $errorMessage = str_replace("%1", $number, $GLOBALS['CKFLang']['ErrorUnknown']); |
||
| 40 | } |
||
| 41 | } else { |
||
| 42 | $errorMessage = ""; |
||
| 43 | } |
||
| 44 | return $errorMessage; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Convert any value to boolean, strings like "false", "FalSE" and "off" are also considered as false |
||
| 49 | * |
||
| 50 | * @static |
||
| 51 | * @access public |
||
| 52 | * @param mixed $value |
||
| 53 | * @return boolean |
||
| 54 | */ |
||
| 55 | function booleanValue($value) |
||
| 56 | { |
||
| 57 | if (strcasecmp("false", $value) == 0 || strcasecmp("off", $value) == 0 || !$value) { |
||
| 58 | return false; |
||
| 59 | } else { |
||
| 60 | return true; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @link http://pl.php.net/manual/en/function.imagecopyresampled.php |
||
| 66 | * replacement to imagecopyresampled that will deliver results that are almost identical except MUCH faster (very typically 30 times faster) |
||
| 67 | * |
||
| 68 | * @static |
||
| 69 | * @access public |
||
| 70 | * @param string $dst_image |
||
| 71 | * @param string $src_image |
||
| 72 | * @param int $dst_x |
||
| 73 | * @param int $dst_y |
||
| 74 | * @param int $src_x |
||
| 75 | * @param int $src_y |
||
| 76 | * @param int $dst_w |
||
| 77 | * @param int $dst_h |
||
| 78 | * @param int $src_w |
||
| 79 | * @param int $src_h |
||
| 80 | * @param int $quality |
||
| 81 | * @return boolean |
||
| 82 | */ |
||
| 83 | function fastImageCopyResampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 3) |
||
| 84 | { |
||
| 85 | if (empty($src_image) || empty($dst_image)) { |
||
| 86 | return false; |
||
| 87 | } |
||
| 88 | |||
| 89 | if ($quality <= 1) { |
||
| 90 | $temp = imagecreatetruecolor ($dst_w + 1, $dst_h + 1); |
||
| 91 | imagecopyresized ($temp, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w + 1, $dst_h + 1, $src_w, $src_h); |
||
| 92 | imagecopyresized ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $dst_w, $dst_h); |
||
| 93 | imagedestroy ($temp); |
||
| 94 | |||
| 95 | } elseif ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) { |
||
| 96 | $tmp_w = $dst_w * $quality; |
||
| 97 | $tmp_h = $dst_h * $quality; |
||
| 98 | $temp = imagecreatetruecolor ($tmp_w + 1, $tmp_h + 1); |
||
| 99 | imagecopyresized ($temp, $src_image, 0, 0, $src_x, $src_y, $tmp_w + 1, $tmp_h + 1, $src_w, $src_h); |
||
| 100 | imagecopyresampled ($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $tmp_w, $tmp_h); |
||
| 101 | imagedestroy ($temp); |
||
| 102 | |||
| 103 | } else { |
||
| 104 | imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); |
||
| 105 | } |
||
| 106 | |||
| 107 | return true; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @link http://pl.php.net/manual/pl/function.imagecreatefromjpeg.php |
||
| 112 | * function posted by e dot a dot schultz at gmail dot com |
||
| 113 | * |
||
| 114 | * @static |
||
| 115 | * @access public |
||
| 116 | * @param string $filename |
||
| 117 | * @return boolean |
||
| 118 | */ |
||
| 119 | function setMemoryForImage($imageWidth, $imageHeight, $imageBits, $imageChannels) |
||
| 120 | { |
||
| 121 | $MB = 1048576; // number of bytes in 1M |
||
| 122 | $K64 = 65536; // number of bytes in 64K |
||
| 123 | $TWEAKFACTOR = 2.4; // Or whatever works for you |
||
| 124 | $memoryNeeded = round( ( $imageWidth * $imageHeight |
||
| 125 | * $imageBits |
||
| 126 | * $imageChannels / 8 |
||
| 127 | + $K64 |
||
| 128 | ) * $TWEAKFACTOR |
||
| 129 | ) + 3*$MB; |
||
| 130 | |||
| 131 | //ini_get('memory_limit') only works if compiled with "--enable-memory-limit" also |
||
| 132 | //Default memory limit is 8MB so well stick with that. |
||
| 133 | //To find out what yours is, view your php.ini file. |
||
| 134 | $memoryLimit = CKFinder_Connector_Utils_Misc::returnBytes(@ini_get('memory_limit'))/$MB; |
||
| 135 | if (!$memoryLimit) { |
||
| 136 | $memoryLimit = 8; |
||
| 137 | } |
||
| 138 | |||
| 139 | $memoryLimitMB = $memoryLimit * $MB; |
||
| 140 | if (function_exists('memory_get_usage')) { |
||
| 141 | if (memory_get_usage() + $memoryNeeded > $memoryLimitMB) { |
||
| 142 | $newLimit = $memoryLimit + ceil( ( memory_get_usage() |
||
| 143 | + $memoryNeeded |
||
| 144 | - $memoryLimitMB |
||
| 145 | ) / $MB |
||
| 146 | ); |
||
| 147 | if (@ini_set( 'memory_limit', $newLimit . 'M' ) === false) { |
||
| 148 | return false; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } else { |
||
| 152 | if ($memoryNeeded + 3*$MB > $memoryLimitMB) { |
||
| 153 | $newLimit = $memoryLimit + ceil(( 3*$MB |
||
| 154 | + $memoryNeeded |
||
| 155 | - $memoryLimitMB |
||
| 156 | ) / $MB |
||
| 157 | ); |
||
| 158 | if (false === @ini_set( 'memory_limit', $newLimit . 'M' )) { |
||
| 159 | return false; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | } |
||
| 163 | return true; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * convert shorthand php.ini notation into bytes, much like how the PHP source does it |
||
| 168 | * @link http://pl.php.net/manual/en/function.ini-get.php |
||
| 169 | * |
||
| 170 | * @static |
||
| 171 | * @access public |
||
| 172 | * @param string $val |
||
| 173 | * @return int |
||
| 174 | */ |
||
| 175 | function returnBytes($val) |
||
| 176 | { |
||
| 177 | $val = trim($val); |
||
| 178 | if (!$val) { |
||
| 179 | return 0; |
||
| 180 | } |
||
| 181 | $last = strtolower($val[strlen($val)-1]); |
||
| 182 | switch($last) { |
||
| 183 | // The 'G' modifier is available since PHP 5.1.0 |
||
| 184 | case 'g': |
||
| 185 | $val *= 1024; |
||
| 186 | case 'm': |
||
| 187 | $val *= 1024; |
||
| 188 | case 'k': |
||
| 189 | $val *= 1024; |
||
| 190 | } |
||
| 191 | |||
| 192 | return $val; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Checks if a value exists in an array (case insensitive) |
||
| 197 | * |
||
| 198 | * @static |
||
| 199 | * @access public |
||
| 200 | * @param string $needle |
||
| 201 | * @param array $haystack |
||
| 202 | * @return boolean |
||
| 203 | */ |
||
| 204 | function inArrayCaseInsensitive($needle, $haystack) |
||
| 205 | { |
||
| 206 | if (!$haystack || !is_array($haystack)) { |
||
| 207 | return false; |
||
| 208 | } |
||
| 209 | $lcase = array(); |
||
| 210 | foreach ($haystack as $key => $val) { |
||
| 211 | $lcase[$key] = strtolower($val); |
||
| 212 | } |
||
| 213 | return in_array($needle, $lcase); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * UTF-8 compatible version of basename() |
||
| 218 | * |
||
| 219 | * @static |
||
| 220 | * @access public |
||
| 221 | * @param string $file |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | function mbBasename($file) |
||
| 225 | { |
||
| 226 | $explode = explode('/', str_replace("\\", "/", $file)); |
||
| 227 | return end($explode); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Source: http://pl.php.net/imagecreate |
||
| 232 | * (optimized for speed and memory usage, but yet not very efficient) |
||
| 233 | * |
||
| 234 | * @static |
||
| 235 | * @access public |
||
| 236 | * @param string $filename |
||
| 237 | * @return resource |
||
| 238 | */ |
||
| 239 | function imageCreateFromBmp($filename) |
||
| 240 | { |
||
| 241 | //20 seconds seems to be a reasonable value to not kill a server and process images up to 1680x1050 |
||
| 242 | @set_time_limit(20); |
||
| 243 | |||
| 244 | if (false === ($f1 = fopen($filename, "rb"))) { |
||
| 245 | return false; |
||
| 246 | } |
||
| 247 | |||
| 248 | $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1, 14)); |
||
| 249 | if ($FILE['file_type'] != 19778) { |
||
| 250 | return false; |
||
| 251 | } |
||
| 252 | |||
| 253 | $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'. |
||
| 254 | '/Vcompression/Vsize_bitmap/Vhoriz_resolution'. |
||
| 255 | '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1, 40)); |
||
| 256 | |||
| 257 | $BMP['colors'] = pow(2,$BMP['bits_per_pixel']); |
||
| 258 | |||
| 259 | if ($BMP['size_bitmap'] == 0) { |
||
| 260 | $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset']; |
||
| 261 | } |
||
| 262 | |||
| 263 | $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8; |
||
| 264 | $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']); |
||
| 265 | $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4); |
||
| 266 | $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4); |
||
| 267 | $BMP['decal'] = 4-(4*$BMP['decal']); |
||
| 268 | |||
| 269 | if ($BMP['decal'] == 4) { |
||
| 270 | $BMP['decal'] = 0; |
||
| 271 | } |
||
| 272 | |||
| 273 | $PALETTE = array(); |
||
| 274 | if ($BMP['colors'] < 16777216) { |
||
| 275 | $PALETTE = unpack('V'.$BMP['colors'], fread($f1, $BMP['colors']*4)); |
||
| 276 | } |
||
| 277 | |||
| 278 | //2048x1536px@24bit don't even try to process larger files as it will probably fail |
||
| 279 | if ($BMP['size_bitmap'] > 3 * 2048 * 1536) { |
||
| 280 | return false; |
||
| 281 | } |
||
| 282 | |||
| 283 | $IMG = fread($f1, $BMP['size_bitmap']); |
||
| 284 | fclose($f1); |
||
| 285 | $VIDE = chr(0); |
||
| 286 | |||
| 287 | $res = imagecreatetruecolor($BMP['width'],$BMP['height']); |
||
| 288 | $P = 0; |
||
| 289 | $Y = $BMP['height']-1; |
||
| 290 | |||
| 291 | $line_length = $BMP['bytes_per_pixel']*$BMP['width']; |
||
| 292 | |||
| 293 | if ($BMP['bits_per_pixel'] == 24) { |
||
| 294 | while ($Y >= 0) |
||
| 295 | { |
||
| 296 | $X=0; |
||
| 297 | $temp = unpack( "C*", substr($IMG, $P, $line_length)); |
||
| 298 | |||
| 299 | while ($X < $BMP['width']) |
||
| 300 | { |
||
| 301 | $offset = $X*3; |
||
| 302 | imagesetpixel($res, $X++, $Y, ($temp[$offset+3] << 16) + ($temp[$offset+2] << 8) + $temp[$offset+1]); |
||
| 303 | } |
||
| 304 | $Y--; |
||
| 305 | $P += $line_length + $BMP['decal']; |
||
| 306 | } |
||
| 307 | } |
||
| 308 | elseif ($BMP['bits_per_pixel'] == 8) |
||
| 309 | { |
||
| 310 | while ($Y >= 0) |
||
| 311 | { |
||
| 312 | $X=0; |
||
| 313 | |||
| 314 | $temp = unpack( "C*", substr($IMG, $P, $line_length)); |
||
| 315 | |||
| 316 | while ($X < $BMP['width']) |
||
| 317 | { |
||
| 318 | imagesetpixel($res, $X++, $Y, $PALETTE[$temp[$X] +1]); |
||
| 319 | } |
||
| 320 | $Y--; |
||
| 321 | $P += $line_length + $BMP['decal']; |
||
| 322 | } |
||
| 323 | } |
||
| 324 | elseif ($BMP['bits_per_pixel'] == 4) |
||
| 325 | { |
||
| 326 | while ($Y >= 0) |
||
| 327 | { |
||
| 328 | $X=0; |
||
| 329 | $i = 1; |
||
| 330 | $low = true; |
||
| 331 | |||
| 332 | $temp = unpack( "C*", substr($IMG, $P, $line_length)); |
||
| 333 | |||
| 334 | while ($X < $BMP['width']) |
||
| 335 | { |
||
| 336 | if ($low) { |
||
| 337 | $index = $temp[$i] >> 4; |
||
| 338 | } |
||
| 339 | else { |
||
| 340 | $index = $temp[$i++] & 0x0F; |
||
| 341 | } |
||
| 342 | $low = !$low; |
||
| 343 | |||
| 344 | imagesetpixel($res, $X++, $Y, $PALETTE[$index +1]); |
||
| 345 | } |
||
| 346 | $Y--; |
||
| 347 | $P += $line_length + $BMP['decal']; |
||
| 348 | } |
||
| 349 | } |
||
| 350 | elseif ($BMP['bits_per_pixel'] == 1) |
||
| 351 | { |
||
| 352 | $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1)); |
||
| 353 | if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7; |
||
| 354 | elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6; |
||
| 355 | elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5; |
||
| 356 | elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4; |
||
| 357 | elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3; |
||
| 358 | elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2; |
||
| 359 | elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1; |
||
| 360 | elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1); |
||
| 361 | $COLOR[1] = $PALETTE[$COLOR[1]+1]; |
||
| 362 | } |
||
| 363 | else { |
||
| 364 | return false; |
||
| 365 | } |
||
| 366 | |||
| 367 | return $res; |
||
| 368 | } |
||
| 369 | } |
||
| 370 |