| Total Complexity | 44 |
| Total Lines | 301 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like OutsideComm 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 OutsideComm, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class OutsideComm { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * downloads a file from the internet |
||
| 25 | * @param string $url |
||
| 26 | * @return string|boolean the data we got back, or FALSE on failure |
||
| 27 | */ |
||
| 28 | public static function downloadFile($url) { |
||
| 29 | $loggerInstance = new \core\common\Logging(); |
||
| 30 | if (!preg_match("/:\/\//", $url)) { |
||
| 31 | $loggerInstance->debug(3, "The specified string does not seem to be a URL!"); |
||
| 32 | return FALSE; |
||
| 33 | } |
||
| 34 | # we got a URL, download it |
||
| 35 | $download = fopen($url, "rb"); |
||
| 36 | if ($download === FALSE) { |
||
| 37 | $loggerInstance->debug(2, "Failed to open handle for $url"); |
||
| 38 | return FALSE; |
||
| 39 | } |
||
| 40 | $data = stream_get_contents($download); |
||
| 41 | if ($data === FALSE) { |
||
| 42 | $loggerInstance->debug(2, "Failed to download the file from $url"); |
||
| 43 | return FALSE; |
||
| 44 | } |
||
| 45 | return $data; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * create an email handle from PHPMailer for later customisation and sending |
||
| 50 | * @return \PHPMailer\PHPMailer\PHPMailer |
||
| 51 | */ |
||
| 52 | public static function mailHandle() { |
||
| 53 | // use PHPMailer to send the mail |
||
| 54 | $mail = new \PHPMailer\PHPMailer\PHPMailer(); |
||
| 55 | $mail->isSMTP(); |
||
| 56 | $mail->SMTPAuth = true; |
||
| 57 | $mail->Port = 587; |
||
| 58 | $mail->SMTPSecure = 'tls'; |
||
| 59 | $mail->Host = CONFIG['MAILSETTINGS']['host']; |
||
| 60 | $mail->Username = CONFIG['MAILSETTINGS']['user']; |
||
| 61 | $mail->Password = CONFIG['MAILSETTINGS']['pass']; |
||
| 62 | // formatting nitty-gritty |
||
| 63 | $mail->WordWrap = 72; |
||
| 64 | $mail->isHTML(FALSE); |
||
| 65 | $mail->CharSet = 'UTF-8'; |
||
| 66 | $mail->From = CONFIG['APPEARANCE']['from-mail']; |
||
| 67 | // are we fancy? i.e. S/MIME signing? |
||
| 68 | if (isset(CONFIG['MAILSETTINGS']['certfilename'], CONFIG['MAILSETTINGS']['keyfilename'], CONFIG['MAILSETTINGS']['keypass'])) { |
||
| 69 | $mail->sign(CONFIG['MAILSETTINGS']['certfilename'], CONFIG['MAILSETTINGS']['keyfilename'], CONFIG['MAILSETTINGS']['keypass']); |
||
| 70 | } |
||
| 71 | return $mail; |
||
| 72 | } |
||
| 73 | |||
| 74 | const MAILDOMAIN_INVALID = -1000; |
||
| 75 | const MAILDOMAIN_NO_MX = -1001; |
||
| 76 | const MAILDOMAIN_NO_HOST = -1002; |
||
| 77 | const MAILDOMAIN_NO_CONNECT = -1003; |
||
| 78 | const MAILDOMAIN_NO_STARTTLS = 1; |
||
| 79 | const MAILDOMAIN_STARTTLS = 2; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * verifies whether a mail address is in an existing and STARTTLS enabled mail domain |
||
| 83 | * @param string $address |
||
| 84 | * @return int status of the mail domain |
||
| 85 | */ |
||
| 86 | public static function mailAddressValidSecure($address) { |
||
| 87 | $loggerInstance = new \core\common\Logging(); |
||
| 88 | if (!filter_var($address, FILTER_VALIDATE_EMAIL)) { |
||
| 89 | $loggerInstance->debug(4, "OutsideComm::mailAddressValidSecure: invalid mail address."); |
||
| 90 | return OutsideComm::MAILDOMAIN_INVALID; |
||
| 91 | } |
||
| 92 | $domain = substr($address, strpos($address, '@') + 1); // everything after the @ sign |
||
| 93 | // we can be sure that the @ was found (FILTER_VALIDATE_EMAIL succeeded) |
||
| 94 | // but let's be explicit |
||
| 95 | if ($domain === FALSE) { |
||
| 96 | return OutsideComm::MAILDOMAIN_INVALID; |
||
| 97 | } |
||
| 98 | // does the domain have MX records? |
||
| 99 | $mx = dns_get_record($domain, DNS_MX); |
||
| 100 | if ($mx === FALSE) { |
||
| 101 | $loggerInstance->debug(4, "OutsideComm::mailAddressValidSecure: no MX."); |
||
| 102 | return OutsideComm::MAILDOMAIN_NO_MX; |
||
| 103 | } |
||
| 104 | $loggerInstance->debug(5, "Domain: $domain MX: " . print_r($mx, TRUE)); |
||
| 105 | // create a pool of A and AAAA records for all the MXes |
||
| 106 | $ipAddrs = []; |
||
| 107 | foreach ($mx as $onemx) { |
||
| 108 | $v4list = dns_get_record($onemx['target'], DNS_A); |
||
| 109 | $v6list = dns_get_record($onemx['target'], DNS_AAAA); |
||
| 110 | foreach ($v4list as $oneipv4) { |
||
| 111 | $ipAddrs[] = $oneipv4['ip']; |
||
| 112 | } |
||
| 113 | foreach ($v6list as $oneipv6) { |
||
| 114 | $ipAddrs[] = "[" . $oneipv6['ipv6'] . "]"; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | if (count($ipAddrs) == 0) { |
||
| 118 | $loggerInstance->debug(4, "OutsideComm::mailAddressValidSecure: no mailserver hosts."); |
||
| 119 | return OutsideComm::MAILDOMAIN_NO_HOST; |
||
| 120 | } |
||
| 121 | $loggerInstance->debug(5, "Domain: $domain Addrs: " . print_r($ipAddrs, TRUE)); |
||
| 122 | // connect to all hosts. If all can't connect, return MAILDOMAIN_NO_CONNECT. |
||
| 123 | // If at least one does not support STARTTLS or one of the hosts doesn't connect |
||
| 124 | // , return MAILDOMAIN_NO_STARTTLS (one which we can't connect to we also |
||
| 125 | // can't verify if it's doing STARTTLS, so better safe than sorry. |
||
| 126 | $retval = OutsideComm::MAILDOMAIN_NO_CONNECT; |
||
| 127 | $allWithStarttls = TRUE; |
||
| 128 | foreach ($ipAddrs as $oneip) { |
||
| 129 | $loggerInstance->debug(5, "OutsideComm::mailAddressValidSecure: connecting to $oneip."); |
||
| 130 | $smtp = new \PHPMailer\PHPMailer\SMTP; |
||
| 131 | if ($smtp->connect($oneip, 25)) { |
||
| 132 | // host reached! so at least it's not a NO_CONNECT |
||
| 133 | $loggerInstance->debug(4, "OutsideComm::mailAddressValidSecure: connected to $oneip."); |
||
| 134 | $retval = OutsideComm::MAILDOMAIN_NO_STARTTLS; |
||
| 135 | if ($smtp->hello('eduroam.org')) { |
||
| 136 | $extensions = $smtp->getServerExtList(); // Scrutinzer is wrong; is not always null - contains server capabilities |
||
| 137 | if (!is_array($extensions) || !array_key_exists('STARTTLS', $extensions)) { |
||
| 138 | $loggerInstance->debug(4, "OutsideComm::mailAddressValidSecure: no indication for STARTTLS."); |
||
| 139 | $allWithStarttls = FALSE; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } else { |
||
| 143 | // no connect: then we can't claim all targets have STARTTLS |
||
| 144 | $allWithStarttls = FALSE; |
||
| 145 | $loggerInstance->debug(5, "OutsideComm::mailAddressValidSecure: failed $oneip."); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | // did the state $allWithStarttls survive? Then up the response to |
||
| 149 | // appropriate level. |
||
| 150 | if ($retval == OutsideComm::MAILDOMAIN_NO_STARTTLS && $allWithStarttls) { |
||
| 151 | $retval = OutsideComm::MAILDOMAIN_STARTTLS; |
||
| 152 | } |
||
| 153 | return $retval; |
||
| 154 | } |
||
| 155 | |||
| 156 | const SMS_SENT = 100; |
||
| 157 | const SMS_NOTSENT = 101; |
||
| 158 | const SMS_FRAGEMENTSLOST = 102; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Send SMS invitations to end users |
||
| 162 | * |
||
| 163 | * @param string $number the number to send to: with country prefix, but without the + sign ("352123456" for a Luxembourg example) |
||
| 164 | * @param string $content the text to send |
||
| 165 | * @return integer status of the sending process |
||
| 166 | * @throws Exception |
||
| 167 | */ |
||
| 168 | public static function sendSMS($number, $content) { |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | const INVITE_CONTEXTS = [ |
||
| 215 | 0 => "CO-ADMIN", |
||
| 216 | 1 => "NEW-FED", |
||
| 217 | 2 => "EXISTING-FED", |
||
| 218 | ]; |
||
| 219 | |||
| 220 | /** |
||
| 221 | * |
||
| 222 | * @param string $targets one or more mail addresses, comma-separated |
||
| 223 | * @param string $introtext introductory sentence (varies by situation) |
||
| 224 | * @param string $newtoken the token to send |
||
| 225 | * @param \core\Federation $federation if not NULL, indicates that invitation comes from authorised fed admin of that federation |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | public static function adminInvitationMail($targets, $introtext, $newtoken, $idpPrettyName, $federation) { |
||
| 325 |