| Total Complexity | 74 |
| Total Lines | 364 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like WindowsCommon 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.
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 WindowsCommon, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | abstract class WindowsCommon extends \core\DeviceConfig { |
||
| 28 | |||
| 29 | public function copyBasicFiles() { |
||
| 30 | if (!($this->copyFile('wlan_test.exe') && |
||
| 31 | $this->copyFile('check_wired.cmd') && |
||
| 32 | $this->copyFile('install_wired.cmd') && |
||
| 33 | $this->copyFile('cat_bg.bmp') && |
||
| 34 | $this->copyFile('base64.nsh'))) { |
||
| 35 | throw new Exception("Copying needed files (part 1) failed for at least one file!"); |
||
| 36 | } |
||
| 37 | |||
| 38 | if (!($this->copyFile('cat32.ico') && |
||
| 39 | $this->copyFile('cat_150.bmp') && |
||
| 40 | $this->copyFile('WLANSetEAPUserData/WLANSetEAPUserData32.exe', 'WLANSetEAPUserData32.exe') && |
||
| 41 | $this->copyFile('WLANSetEAPUserData/WLANSetEAPUserData64.exe', 'WLANSetEAPUserData64.exe'))) { |
||
| 42 | throw new Exception("Copying needed files (part 2) failed for at least one file!"); |
||
| 43 | } |
||
| 44 | if (!$this->translateFile('common.inc', 'common.nsh', $this->codePage)) { |
||
| 45 | throw new Exception("Translating needed file common.inc failed!"); |
||
| 46 | } |
||
| 47 | return; |
||
| 48 | } |
||
| 49 | |||
| 50 | public function copyPwdFiles() { |
||
| 51 | if (!($this->copyFile('Aruba_Networks_EAP-pwd_x32.msi') && |
||
| 52 | $this->copyFile('Aruba_Networks_EAP-pwd_x64.msi'))) { |
||
| 53 | throw new Exception("Copying needed files (EAP-pwd) failed for at least one file!"); |
||
| 54 | } |
||
| 55 | if (!$this->translateFile('pwd.inc', 'cat.NSI', $this->codePage)) { |
||
| 56 | throw new Exception("Translating needed file pwd.inc failed!"); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | public function copyGeantLinkFiles() { |
||
| 61 | if (!($this->copyFile('GEANTLink/GEANTLink32.msi', 'GEANTLink32.msi') && |
||
| 62 | $this->copyFile('GEANTLink/GEANTLink64.msi', 'GEANTLink64.msi') && |
||
| 63 | $this->copyFile('GEANTLink/CredWrite.exe', 'CredWrite.exe') && |
||
| 64 | $this->copyFile('GEANTLink/MsiUseFeature.exe', 'MsiUseFeature.exe'))) { |
||
| 65 | throw new Exception("Copying needed files (GEANTLink) failed for at least one file!"); |
||
| 66 | } |
||
| 67 | if (!$this->translateFile('geant_link.inc', 'cat.NSI', $this->codePage)) { |
||
| 68 | throw new Exception("Translating needed file geant_link.inc failed!"); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * function to escape double quotes in a special NSI-compatible way |
||
| 75 | * |
||
| 76 | * @param string $in input string |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | public static function echo_nsi($in) { |
||
| 80 | echo preg_replace('/"/', '$\"', $in); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param string $input input string |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public static function sprint_nsi($input) { |
||
| 88 | return preg_replace('/"/', '$\"', $input); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function __construct() { |
||
| 92 | parent::__construct(); |
||
| 93 | $this->useGeantLink = (isset($this->options['args']) && $this->options['args'] == 'gl') ? 1 : 0; |
||
| 94 | } |
||
| 95 | |||
| 96 | protected function prepareInstallerLang() { |
||
| 97 | if (isset($this->LANGS[$this->languageInstance->getLang()])) { |
||
| 98 | $language = $this->LANGS[$this->languageInstance->getLang()]; |
||
| 99 | $this->lang = $language['nsis']; |
||
| 100 | $this->codePage = 'cp' . $language['cp']; |
||
| 101 | } else { |
||
| 102 | $this->lang = 'English'; |
||
| 103 | $this->codePage = 'cp1252'; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | public function writeDeviceInfo() { |
||
| 140 | } |
||
| 141 | |||
| 142 | private function scaleLogo($imagePath, $maxSize) { |
||
| 143 | $imageObject = new \Imagick($imagePath); |
||
| 144 | $imageSize = $imageObject->getImageGeometry(); |
||
| 145 | $imageMax = max($imageSize); |
||
| 146 | $this->loggerInstance->debug(5, "Logo size: "); |
||
| 147 | $this->loggerInstance->debug(5, $imageSize); |
||
| 148 | $this->loggerInstance->debug(5, "max=$imageMax\n"); |
||
| 149 | // resize logo if necessary |
||
| 150 | if ($imageMax > $maxSize) { |
||
| 151 | if ($imageMax == $imageSize['width']) { |
||
| 152 | $imageObject->scaleImage($maxSize, 0); |
||
| 153 | } else { |
||
| 154 | $imageObject->scaleImage(0, $maxSize); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | $imageSize = $imageObject->getImageGeometry(); |
||
| 158 | $this->background['freeHeight'] -= $imageSize['height']; |
||
| 159 | return($imageObject); |
||
| 160 | } |
||
| 161 | |||
| 162 | protected function combineLogo($logos = NULL, $fedLogo = NULL) { |
||
| 163 | // maximum size to which we want to resize the logos |
||
| 164 | |||
| 165 | $maxSize = 120; |
||
| 166 | // $freeTop is set to how much vertical space we need to leave at the top |
||
| 167 | // this will depend on the design of the background |
||
| 168 | $freeTop = 70; |
||
| 169 | // $freeBottom is set to how much vertical space we need to leave at the bottom |
||
| 170 | // this will depend on the design of the background |
||
| 171 | $freeBottom = 30; |
||
| 172 | |||
| 173 | $bgImage = new \Imagick('cat_bg.bmp'); |
||
| 174 | $bgImage->setFormat('BMP3'); |
||
| 175 | $bgImageSize = $bgImage->getImageGeometry(); |
||
| 176 | $logosToPlace = []; |
||
| 177 | $this->background = []; |
||
| 178 | $this->background['freeHeight'] = $bgImageSize['height'] - $freeTop - $freeBottom; |
||
| 179 | |||
| 180 | if ($this->getAttibute('fed:include_logo_installers') === NULL) { |
||
| 181 | $fedLogo = NULL; |
||
| 182 | } |
||
| 183 | if ($fedLogo != NULL) { |
||
| 184 | $logosToPlace[] = $this->scaleLogo($fedLogo[0]['name'], $maxSize); |
||
| 185 | } |
||
| 186 | if ($logos != NULL) { |
||
| 187 | $logosToPlace[] = $this->scaleLogo($logos[0]['name'], $maxSize); |
||
| 188 | } |
||
| 189 | |||
| 190 | $logoCount = count($logosToPlace); |
||
| 191 | if ($logoCount > 0) { |
||
| 192 | $voffset = $freeTop; |
||
| 193 | $freeSpace = (int)round($this->background['freeHeight'] / ($logoCount + 1)); |
||
| 194 | foreach ($logosToPlace as $logo) { |
||
| 195 | $voffset += $freeSpace; |
||
| 196 | $logoSize = $logo->getImageGeometry(); |
||
| 197 | $hoffset = (int)round(($bgImageSize['width'] - $logoSize['width']) / 2); |
||
| 198 | $bgImage->compositeImage($logo, $logo->getImageCompose(), $hoffset, $voffset); |
||
| 199 | $voffset += $logoSize['height']; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | //new image is saved as the background |
||
| 203 | $bgImage->writeImage('BMP3:cat_bg.bmp'); |
||
| 204 | } |
||
| 205 | |||
| 206 | protected function signInstaller() { |
||
| 207 | $fileName = $this->installerBasename . '.exe'; |
||
| 208 | if (!$this->sign) { |
||
| 209 | rename("installer.exe", $fileName); |
||
| 210 | return $fileName; |
||
| 211 | } |
||
| 212 | // are actually signing |
||
| 213 | $outputFromSigning = system($this->sign . " installer.exe '$fileName' > /dev/null"); |
||
| 214 | if ($outputFromSigning === FALSE) { |
||
| 215 | $this->loggerInstance->debug(2, "Signing the WindowsCommon installer $fileName FAILED!\n"); |
||
| 216 | } |
||
| 217 | return $fileName; |
||
| 218 | } |
||
| 219 | |||
| 220 | protected function compileNSIS() { |
||
| 221 | if (CONFIG_CONFASSISTANT['NSIS_VERSION'] >= 3) { |
||
| 222 | $makensis = CONFIG_CONFASSISTANT['PATHS']['makensis'] . " -INPUTCHARSET UTF8"; |
||
| 223 | } else { |
||
| 224 | $makensis = CONFIG_CONFASSISTANT['PATHS']['makensis']; |
||
| 225 | } |
||
| 226 | $command = $makensis . ' -V4 cat.NSI > nsis.log 2>&1'; |
||
| 227 | system($command); |
||
| 228 | $this->loggerInstance->debug(4, "compileNSIS:$command\n"); |
||
| 229 | } |
||
| 230 | |||
| 231 | private function getSupport($attr, $type) { |
||
| 232 | $supportString = [ |
||
| 233 | 'email' => 'SUPPORT', |
||
| 234 | 'url' => 'URL', |
||
| 235 | ]; |
||
| 236 | $s = "support_" . $type . "_substitute"; |
||
| 237 | $substitute = $this->translateString($this->$s, $this->codePage); |
||
| 238 | $returnValue = !empty($attr['support:' . $type][0]) ? $attr['support:' . $type][0] : $substitute; |
||
| 239 | return('!define ' . $supportString[$type] . ' "' . $returnValue . '"' . "\n"); |
||
| 240 | } |
||
| 241 | |||
| 242 | |||
| 243 | protected function writeNsisDefines($attr) { |
||
| 244 | $fcontents = "\n" . '!define NSIS_MAJOR_VERSION ' . CONFIG_CONFASSISTANT['NSIS_VERSION']; |
||
| 245 | if ($attr['internal:profile_count'][0] > 1) { |
||
| 246 | $fcontents .= "\n" . '!define USER_GROUP "' . $this->translateString(str_replace('"', '$\\"', $attr['profile:name'][0]), $this->codePage) . '" |
||
| 247 | '; |
||
| 248 | } |
||
| 249 | $fcontents .= ' |
||
| 250 | Caption "' . $this->translateString(sprintf(WindowsCommon::sprint_nsi(_("%s installer for %s")), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], $attr['general:instname'][0]), $this->codePage) . '" |
||
| 251 | !define APPLICATION "' . $this->translateString(sprintf(WindowsCommon::sprint_nsi(_("%s installer for %s")), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], $attr['general:instname'][0]), $this->codePage) . '" |
||
| 252 | !define VERSION "' . \core\CAT::VERSION_MAJOR . '.' . \core\CAT::VERSION_MINOR . '" |
||
| 253 | !define INSTALLER_NAME "installer.exe" |
||
| 254 | !define LANG "' . $this->lang . '" |
||
| 255 | !define LOCALE "' . preg_replace('/\..*$/', '', CONFIG['LANGUAGES'][$this->languageInstance->getLang()]['locale']) . '" |
||
| 256 | ;-------------------------------- |
||
| 257 | !define ORGANISATION "' . $this->translateString($attr['general:instname'][0], $this->codePage) . '" |
||
| 258 | '; |
||
| 259 | $fcontents .= $this->getSupport($attr, 'email'); |
||
| 260 | $fcontents .= $this->getSupport($attr, 'url'); |
||
| 261 | if (\core\common\Entity::getAttributeValue($attr, 'media:wired', 0) == 'on') { |
||
| 262 | $fcontents .= '!define WIRED |
||
| 263 | '; |
||
| 264 | } |
||
| 265 | $fcontents .= '!define PROVIDERID "urn:UUID:' . $this->deviceUUID . '" |
||
| 266 | '; |
||
| 267 | if (!empty($attr['internal:realm'][0])) { |
||
| 268 | $fcontents .= '!define REALM "' . $attr['internal:realm'][0] . '" |
||
| 269 | '; |
||
| 270 | } |
||
| 271 | if(!empty($attr['internal:hint_userinput_suffix'][0]) && $attr['internal:hint_userinput_suffix'][0] == 1) { |
||
| 272 | $fcontents .= '!define HINT_USER_INPUT "' . $attr['internal:hint_userinput_suffix'][0] . '" |
||
| 273 | '; |
||
| 274 | } |
||
| 275 | if(!empty($attr['internal:verify_userinput_suffix'][0]) && $attr['internal:verify_userinput_suffix'][0] == 1) { |
||
| 276 | $fcontents .= '!define VERIFY_USER_REALM_INPUT "' . $attr['internal:verify_userinput_suffix'][0] . '" |
||
| 277 | '; |
||
| 278 | } |
||
| 279 | $fcontents .= $this->msInfoFile($attr); |
||
| 280 | return($fcontents); |
||
| 281 | |||
| 282 | } |
||
| 283 | |||
| 284 | protected function msInfoFile($attr) { |
||
| 285 | $out = ''; |
||
| 286 | if (isset($attr['support:info_file'])) { |
||
| 287 | $out .= '!define EXTERNAL_INFO "'; |
||
| 288 | // $this->loggerInstance->debug(4,"Info file type ".$attr['support:info_file'][0]['mime']."\n"); |
||
| 289 | if ($attr['internal:info_file'][0]['mime'] == 'rtf') { |
||
| 290 | $out = '!define LICENSE_FILE "' . $attr['internal:info_file'][0]['name']; |
||
| 291 | } elseif ($attr['internal:info_file'][0]['mime'] == 'txt') { |
||
| 292 | $infoFile = file_get_contents($attr['internal:info_file'][0]['name']); |
||
| 293 | if ($infoFile === FALSE) { |
||
| 294 | throw new Exception("We were told this file exists. Failing to read it is not really possible."); |
||
| 295 | } |
||
| 296 | if (CONFIG_CONFASSISTANT['NSIS_VERSION'] >= 3) { |
||
| 297 | $infoFileConverted = $infoFile; |
||
| 298 | } else { |
||
| 299 | $infoFileConverted = iconv('UTF-8', $this->codePage . '//TRANSLIT', $infoFile); |
||
| 300 | } |
||
| 301 | if ($infoFileConverted !== FALSE && strlen($infoFileConverted) > 0) { |
||
| 302 | file_put_contents('info_f.txt', $infoFileConverted); |
||
| 303 | $out = '!define LICENSE_FILE " info_f.txt'; |
||
| 304 | } |
||
| 305 | } else { |
||
| 306 | $out = '!define EXTERNAL_INFO "' . $attr['internal:info_file'][0]['name']; |
||
| 307 | } |
||
| 308 | |||
| 309 | $out .= "\"\n"; |
||
| 310 | } |
||
| 311 | $this->loggerInstance->debug(4, "Info file returned: $out"); |
||
| 312 | return $out; |
||
| 313 | } |
||
| 314 | |||
| 315 | protected function writeAdditionalDeletes($profiles) { |
||
| 316 | if (count($profiles) == 0) { |
||
| 317 | return; |
||
| 318 | } |
||
| 319 | $fileHandle = fopen('profiles.nsh', 'a'); |
||
| 320 | if ($fileHandle === FALSE) { |
||
| 321 | throw new Exception("Unable to open possibly pre-existing profiles.nsh to append additional deletes."); |
||
| 322 | } |
||
| 323 | fwrite($fileHandle, "!define AdditionalDeletes\n"); |
||
| 324 | foreach ($profiles as $profile) { |
||
| 325 | fwrite($fileHandle, "!insertmacro define_delete_profile \"$profile\"\n"); |
||
| 326 | } |
||
| 327 | fclose($fileHandle); |
||
| 328 | } |
||
| 329 | |||
| 330 | protected function writeClientP12File() { |
||
| 335 | } |
||
| 336 | |||
| 337 | protected function writeTlsUserProfile() { |
||
| 338 | |||
| 339 | } |
||
| 340 | |||
| 341 | public $LANGS = [ |
||
| 342 | 'fr' => ['nsis' => "French", 'cp' => '1252'], |
||
| 343 | 'de' => ['nsis' => "German", 'cp' => '1252'], |
||
| 344 | 'es' => ['nsis' => "SpanishInternational", 'cp' => '1252'], |
||
| 345 | 'it' => ['nsis' => "Italian", 'cp' => '1252'], |
||
| 346 | 'nl' => ['nsis' => "Dutch", 'cp' => '1252'], |
||
| 347 | 'sv' => ['nsis' => "Swedish", 'cp' => '1252'], |
||
| 348 | 'fi' => ['nsis' => "Finnish", 'cp' => '1252'], |
||
| 349 | 'pl' => ['nsis' => "Polish", 'cp' => '1250'], |
||
| 350 | 'ca' => ['nsis' => "Catalan", 'cp' => '1252'], |
||
| 351 | 'sr' => ['nsis' => "SerbianLatin", 'cp' => '1250'], |
||
| 352 | 'hr' => ['nsis' => "Croatian", 'cp' => '1250'], |
||
| 353 | 'sl' => ['nsis' => "Slovenian", 'cp' => '1250'], |
||
| 354 | 'da' => ['nsis' => "Danish", 'cp' => '1252'], |
||
| 355 | 'nb' => ['nsis' => "Norwegian", 'cp' => '1252'], |
||
| 356 | 'nn' => ['nsis' => "NorwegianNynorsk", 'cp' => '1252'], |
||
| 357 | 'el' => ['nsis' => "Greek", 'cp' => '1253'], |
||
| 358 | 'ru' => ['nsis' => "Russian", 'cp' => '1251'], |
||
| 359 | 'pt' => ['nsis' => "Portuguese", 'cp' => '1252'], |
||
| 360 | 'uk' => ['nsis' => "Ukrainian", 'cp' => '1251'], |
||
| 361 | 'cs' => ['nsis' => "Czech", 'cp' => '1250'], |
||
| 362 | 'sk' => ['nsis' => "Slovak", 'cp' => '1250'], |
||
| 363 | 'bg' => ['nsis' => "Bulgarian", 'cp' => '1251'], |
||
| 364 | 'hu' => ['nsis' => "Hungarian", 'cp' => '1250'], |
||
| 365 | 'ro' => ['nsis' => "Romanian", 'cp' => '1250'], |
||
| 366 | 'lv' => ['nsis' => "Latvian", 'cp' => '1257'], |
||
| 367 | 'mk' => ['nsis' => "Macedonian", 'cp' => '1251'], |
||
| 368 | 'et' => ['nsis' => "Estonian", 'cp' => '1257'], |
||
| 369 | 'tr' => ['nsis' => "Turkish", 'cp' => '1254'], |
||
| 370 | 'lt' => ['nsis' => "Lithuanian", 'cp' => '1257'], |
||
| 371 | 'ar' => ['nsis' => "Arabic", 'cp' => '1256'], |
||
| 372 | 'he' => ['nsis' => "Hebrew", 'cp' => '1255'], |
||
| 373 | 'id' => ['nsis' => "Indonesian", 'cp' => '1252'], |
||
| 374 | 'mn' => ['nsis' => "Mongolian", 'cp' => '1251'], |
||
| 375 | 'sq' => ['nsis' => "Albanian", 'cp' => '1252'], |
||
| 376 | 'br' => ['nsis' => "Breton", 'cp' => '1252'], |
||
| 377 | 'be' => ['nsis' => "Belarusian", 'cp' => '1251'], |
||
| 378 | 'is' => ['nsis' => "Icelandic", 'cp' => '1252'], |
||
| 379 | 'ms' => ['nsis' => "Malay", 'cp' => '1252'], |
||
| 380 | 'bs' => ['nsis' => "Bosnian", 'cp' => '1250'], |
||
| 381 | 'ga' => ['nsis' => "Irish", 'cp' => '1250'], |
||
| 382 | 'uz' => ['nsis' => "Uzbek", 'cp' => '1251'], |
||
| 383 | 'gl' => ['nsis' => "Galician", 'cp' => '1252'], |
||
| 384 | 'af' => ['nsis' => "Afrikaans", 'cp' => '1252'], |
||
| 385 | 'ast' => ['nsis' => "Asturian", 'cp' => '1252'], |
||
| 386 | ]; |
||
| 387 | public $codePage; |
||
| 388 | public $lang; |
||
| 389 | public $useGeantLink; |
||
| 390 | private $background; |
||
| 391 | |||
| 393 |