| Conditions | 16 |
| Paths | 122 |
| Total Lines | 94 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 228 | public static function adminInvitationMail($targets, $introtext, $newtoken, $idpPrettyName, $federation) { |
||
| 229 | if (!in_array($introtext, OutsideComm::INVITE_CONTEXTS)) { |
||
| 230 | throw new \Exception("Unknown invite mode!"); |
||
| 231 | } |
||
| 232 | if ($introtext == OutsideComm::INVITE_CONTEXTS[1] && $federation === NULL) { // comes from fed admin, so federation must be set |
||
| 233 | throw new \Exception("Invitation from a fed admin, but we do not know the corresponding federation!"); |
||
| 234 | } |
||
| 235 | $mail = OutsideComm::mailHandle(); |
||
| 236 | $cat = new \core\CAT(); |
||
| 237 | // we have a few stock intro texts on file |
||
| 238 | $introTexts = [ |
||
| 239 | OutsideComm::INVITE_CONTEXTS[0] => sprintf(_("a %s of the %s %s \"%s\" has invited you to manage the %s together with him."), $cat->nomenclature_fed, CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], $cat->nomenclature_inst, $idpPrettyName, $cat->nomenclature_inst), |
||
| 240 | OutsideComm::INVITE_CONTEXTS[1] => sprintf(_("a %s %s has invited you to manage the future %s \"%s\" (%s)."), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], $cat->nomenclature_fed, $cat->nomenclature_inst, $idpPrettyName, strtoupper($federation->tld)), |
||
| 241 | OutsideComm::INVITE_CONTEXTS[2] => sprintf(_("a %s %s has invited you to manage the %s \"%s\"."), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], $cat->nomenclature_fed, $cat->nomenclature_inst, $idpPrettyName), |
||
| 242 | ]; |
||
| 243 | $validity = sprintf(_("This invitation is valid for 24 hours from now, i.e. until %s."), strftime("%x %X", time() + 86400)); |
||
| 244 | // need some nomenclature |
||
| 245 | // are we on https? |
||
| 246 | $proto = "http://"; |
||
| 247 | if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") { |
||
| 248 | $proto = "https://"; |
||
| 249 | } |
||
| 250 | // then, send out the mail |
||
| 251 | $message = _("Hello,") . "\n\n" . wordwrap($introTexts[$introtext] . " " . $validity, 72) . "\n\n"; |
||
| 252 | // default means we don't have a Reply-To. |
||
| 253 | $replyToMessage = wordwrap(_("manually. Please do not reply to this mail; this is a send-only address.")); |
||
| 254 | |||
| 255 | if ($federation !== NULL) { |
||
| 256 | // see if we are supposed to add a custom message |
||
| 257 | $customtext = $federation->getAttributes('fed:custominvite'); |
||
| 258 | if (count($customtext) > 0) { |
||
| 259 | $message .= wordwrap(sprintf(_("Additional message from your %s administrator:"), $cat->nomenclature_fed), 72) . "\n---------------------------------" . |
||
| 260 | wordwrap($customtext[0]['value'], 72) . "\n---------------------------------\n\n"; |
||
| 261 | } |
||
| 262 | // and add Reply-To already now |
||
| 263 | foreach ($federation->listFederationAdmins() as $fedadmin_id) { |
||
| 264 | $fedadmin = new \core\User($fedadmin_id); |
||
| 265 | $mailaddrAttrib = $fedadmin->getAttributes("user:email"); |
||
| 266 | $nameAttrib = $fedadmin->getAttributes("user:realname"); |
||
| 267 | $name = $nameAttrib[0]['value'] ?? sprintf(_("%s administrator"), $cat->nomenclature_fed); |
||
| 268 | if (count($mailaddrAttrib) > 0) { |
||
| 269 | $mail->addReplyTo($mailaddrAttrib[0]['value'], $name); |
||
| 270 | $replyToMessage = wordwrap(sprintf(_("manually. If you reply to this mail, you will reach your %s administrators."), $cat->nomenclature_fed), 72); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | $message .= wordwrap(sprintf(_("To enlist as an administrator for that %s, please click on the following link:"), $cat->nomenclature_inst), 72) . "\n\n" . |
||
| 276 | $proto . $_SERVER['SERVER_NAME'] . dirname(dirname($_SERVER['SCRIPT_NAME'])) . "/admin/action_enrollment.php?token=$newtoken\n\n" . |
||
| 277 | wordwrap(sprintf(_("If clicking the link doesn't work, you can also go to the %s Administrator Interface at"), CONFIG['APPEARANCE']['productname']), 72) . "\n\n" . |
||
| 278 | $proto . $_SERVER['SERVER_NAME'] . dirname(dirname($_SERVER['SCRIPT_NAME'])) . "/admin/\n\n" . |
||
| 279 | _("and enter the invitation token") . "\n\n" . |
||
| 280 | $newtoken . "\n\n$replyToMessage\n\n" . |
||
| 281 | wordwrap(_("Do NOT forward the mail before the token has expired - or the recipients may be able to consume the token on your behalf!"), 72) . "\n\n" . |
||
| 282 | wordwrap(sprintf(_("We wish you a lot of fun with the %s."), CONFIG['APPEARANCE']['productname']), 72) . "\n\n" . |
||
| 283 | sprintf(_("Sincerely,\n\nYour friendly folks from %s Operations"), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name']); |
||
| 284 | |||
| 285 | |||
| 286 | // who to whom? |
||
| 287 | $mail->FromName = CONFIG['APPEARANCE']['productname'] . " Invitation System"; |
||
| 288 | |||
| 289 | if (isset(CONFIG['APPEARANCE']['invitation-bcc-mail']) && CONFIG['APPEARANCE']['invitation-bcc-mail'] !== NULL) { |
||
| 290 | $mail->addBCC(CONFIG['APPEARANCE']['invitation-bcc-mail']); |
||
| 291 | } |
||
| 292 | |||
| 293 | // all addresses are wrapped in a string, but PHPMailer needs a structured list of addressees |
||
| 294 | // sigh... so convert as needed |
||
| 295 | // first split multiple into one if needed |
||
| 296 | $recipients = explode(", ", $targets); |
||
| 297 | |||
| 298 | $secStatus = TRUE; |
||
| 299 | $domainStatus = TRUE; |
||
| 300 | |||
| 301 | // fill the destinations in PHPMailer API |
||
| 302 | foreach ($recipients as $recipient) { |
||
| 303 | $mail->addAddress($recipient); |
||
| 304 | $status = OutsideComm::mailAddressValidSecure($recipient); |
||
| 305 | if ($status < OutsideComm::MAILDOMAIN_STARTTLS) { |
||
| 306 | $secStatus = FALSE; |
||
| 307 | } |
||
| 308 | if ($status < 0) { |
||
| 309 | $domainStatus = FALSE; |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | if (!$domainStatus) { |
||
| 314 | return ["SENT" => FALSE, "TRANSPORT" => FALSE]; |
||
| 315 | } |
||
| 316 | |||
| 317 | // what do we want to say? |
||
| 318 | $mail->Subject = sprintf(_("%s: you have been invited to manage an %s"), CONFIG['APPEARANCE']['productname'], $cat->nomenclature_inst); |
||
| 319 | $mail->Body = $message; |
||
| 320 | |||
| 321 | return ["SENT" => $mail->send(), "TRANSPORT" => $secStatus]; |
||
| 322 | } |
||
| 340 |