| Total Complexity | 78 |
| Total Lines | 460 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like OptionParser 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 OptionParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class OptionParser { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * an instance of the InputValidation class which we use heavily for syntax checks. |
||
| 30 | * |
||
| 31 | * @var \web\lib\common\InputValidation |
||
| 32 | */ |
||
| 33 | private $validator; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * an instance of the UIElements() class to draw some UI widgets from. |
||
| 37 | * |
||
| 38 | * @var UIElements |
||
| 39 | */ |
||
| 40 | private $uiElements; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * a handle for the Options singleton |
||
| 44 | * |
||
| 45 | * @var \core\Options |
||
| 46 | */ |
||
| 47 | private $optioninfoObject; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * initialises the various handles. |
||
| 51 | */ |
||
| 52 | public function __construct() { |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Verifies whether an incoming upload was actually valid data |
||
| 60 | * |
||
| 61 | * @param string $optiontype for which option was the data uploaded |
||
| 62 | * @param string $incomingBinary the uploaded data |
||
| 63 | * @return boolean whether the data was valid |
||
| 64 | */ |
||
| 65 | private function checkUploadSanity(string $optiontype, string $incomingBinary) { |
||
| 66 | switch ($optiontype) { |
||
| 67 | case "general:logo_file": |
||
| 68 | case "fed:logo_file": |
||
| 69 | case "internal:logo_from_url": |
||
| 70 | // we check logo_file with ImageMagick |
||
| 71 | return $this->validator->image($incomingBinary); |
||
| 72 | case "eap:ca_file": |
||
| 73 | // fall-through intended: both CA types are treated the same |
||
| 74 | case "fed:minted_ca_file": |
||
| 75 | // echo "Checking $optiontype with file $filename"; |
||
| 76 | $func = new \core\common\X509; |
||
| 77 | $cert = $func->processCertificate($incomingBinary); |
||
| 78 | if (is_array($cert)) { // could also be FALSE if it was incorrect incoming data |
||
| 79 | return TRUE; |
||
| 80 | } |
||
| 81 | // the certificate seems broken |
||
| 82 | return FALSE; |
||
| 83 | case "support:info_file": |
||
| 84 | $info = new \finfo(); |
||
| 85 | $filetype = $info->buffer($incomingBinary, FILEINFO_MIME_TYPE); |
||
| 86 | |||
| 87 | // we only take plain text files in UTF-8! |
||
| 88 | if ($filetype == "text/plain" && iconv("UTF-8", "UTF-8", $incomingBinary) !== FALSE) { |
||
| 89 | return TRUE; |
||
| 90 | } |
||
| 91 | return FALSE; |
||
| 92 | default: |
||
| 93 | return FALSE; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Known-good options are sometimes converted, this function takes care of that. |
||
| 99 | * |
||
| 100 | * Cases in point: |
||
| 101 | * - CA import by URL reference: fetch cert from URL and store it as CA file instead |
||
| 102 | * - Logo import by URL reference: fetch logo from URL and store it as logo file instead |
||
| 103 | * - CA file: mangle the content so that *only* the valid content remains (raw input may contain line breaks or spaces which are valid, but some supplicants choke upon) |
||
| 104 | * |
||
| 105 | * @param array $options the list of options we got |
||
| 106 | * @param array $good by-reference: the future list of actually imported options |
||
| 107 | * @param array $bad by-reference: the future list of submitted but rejected options |
||
| 108 | * @return array the options, post-processed |
||
| 109 | */ |
||
| 110 | private function postProcessValidAttributes(array $options, array &$good, array &$bad) { |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * extracts a coordinate pair from _POST (if any) and returns it in our |
||
| 170 | * standard attribute notation |
||
| 171 | * |
||
| 172 | * @param array $postArray |
||
| 173 | * @param array $good |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | private function postProcessCoordinates(array $postArray, array &$good) { |
||
| 177 | if (!empty($postArray['geo_long']) && !empty($postArray['geo_lat'])) { |
||
| 178 | |||
| 179 | $lat = $this->validator->coordinate($postArray['geo_lat']); |
||
| 180 | $lon = $this->validator->coordinate($postArray['geo_long']); |
||
| 181 | $good[] = ("general:geo_coordinates"); |
||
| 182 | return [0 => ["general:geo_coordinates" => ['lang' => NULL, 'content' => json_encode(["lon" => $lon, "lat" => $lat])]]]; |
||
| 183 | } |
||
| 184 | return []; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * creates HTML code for a user-readable summary of the imports |
||
| 189 | * @param array $good list of actually imported options |
||
| 190 | * @param array $bad list of submitted but rejected options |
||
| 191 | * @param array $mlAttribsWithC list of language-variant options |
||
| 192 | * @return string HTML code |
||
| 193 | */ |
||
| 194 | private function displaySummaryInUI(array $good, array $bad, array $mlAttribsWithC) { |
||
| 195 | $retval = ""; |
||
| 196 | // don't do your own table - only the <tr>s here |
||
| 197 | // list all attributes that were set correctly |
||
| 198 | $listGood = array_count_values($good); |
||
| 199 | $uiElements = new UIElements(); |
||
| 200 | foreach ($listGood as $name => $count) { |
||
| 201 | /// number of times attribute is present, and its name |
||
| 202 | /// Example: "5x Support E-Mail" |
||
| 203 | $retval .= $this->uiElements->boxOkay(sprintf(_("%dx %s"), $count, $uiElements->displayName($name))); |
||
| 204 | } |
||
| 205 | // list all atributes that had errors |
||
| 206 | $listBad = array_count_values($bad); |
||
| 207 | foreach ($listBad as $name => $count) { |
||
| 208 | $retval .= $this->uiElements->boxError(sprintf(_("%dx %s"), (int) $count, $uiElements->displayName($name))); |
||
| 209 | } |
||
| 210 | // list multilang without default |
||
| 211 | foreach ($mlAttribsWithC as $attribName => $isitsetornot) { |
||
| 212 | if ($isitsetornot == FALSE) { |
||
| 213 | $retval .= $this->uiElements->boxWarning(sprintf(_("You did not set a 'default language' value for %s. This means we can only display this string for installers which are <strong>exactly</strong> in the language you configured. For the sake of all other languages, you may want to edit the profile again and populate the 'default/other' language field."), $uiElements->displayName($attribName))); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | return $retval; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Incoming data is in $_POST and possibly in $_FILES. Collate values into |
||
| 221 | * one array according to our name and numbering scheme. |
||
| 222 | * |
||
| 223 | * @param array $postArray _POST |
||
| 224 | * @param array $filesArray _FILES |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | private function collateOptionArrays(array $postArray, array $filesArray) { |
||
| 228 | |||
| 229 | $optionarray = $postArray['option'] ?? []; |
||
| 230 | $valuearray = $postArray['value'] ?? []; |
||
| 231 | $filesarray = $filesArray['value']['tmp_name'] ?? []; |
||
| 232 | |||
| 233 | $iterator = array_merge($optionarray, $valuearray, $filesarray); |
||
| 234 | |||
| 235 | return $iterator; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * The very end of the processing: clean input data gets sent to the database |
||
| 240 | * for storage |
||
| 241 | * |
||
| 242 | * @param mixed $object for which object are the options |
||
| 243 | * @param array $options the options to store |
||
| 244 | * @param array $pendingattributes list of attributes which are already stored but may need to be deleted |
||
| 245 | * @param string $device when the $object is Profile, this indicates device-specific attributes |
||
| 246 | * @param int $eaptype when the $object is Profile, this indicates eap-specific attributes |
||
| 247 | * @return array list of attributes which were previously stored but are to be deleted now |
||
| 248 | * @throws Exception |
||
| 249 | */ |
||
| 250 | private function sendOptionsToDatabase($object, array $options, array $pendingattributes, string $device = NULL, int $eaptype = NULL) { |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * filters the input to find syntactically correctly submitted attributes |
||
| 286 | * |
||
| 287 | * @param array $listOfEntries list of POST and FILES entries |
||
| 288 | * @param array $multilangAttrsWithC by-reference: future list of language-variant options and their "default lang" state |
||
| 289 | * @param array $bad by-reference: future list of submitted but rejected options |
||
| 290 | * @return array sanitised list of options |
||
| 291 | * @throws Exception |
||
| 292 | */ |
||
| 293 | private function sanitiseInputs(array $listOfEntries, array &$multilangAttrsWithC, array &$bad) { |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * The main function: takes all HTML field inputs, makes sense of them and stores valid data in the database |
||
| 418 | * |
||
| 419 | * @param mixed $object The object for which attributes were submitted |
||
| 420 | * @param array $postArray incoming attribute names and values as submitted with $_POST |
||
| 421 | * @param array $filesArray incoming attribute names and values as submitted with $_FILES |
||
| 422 | * @param int $eaptype for eap-specific attributes (only used where $object is a ProfileRADIUS instance) |
||
| 423 | * @param string $device for device-specific attributes (only used where $object is a ProfileRADIUS instance) |
||
| 424 | * @return string text to be displayed in UI with the summary of attributes added |
||
| 425 | * @throws Exception |
||
| 426 | */ |
||
| 427 | public function processSubmittedFields($object, array $postArray, array $filesArray, int $eaptype = NULL, string $device = NULL) { |
||
| 489 |