| Conditions | 19 |
| Paths | 363 |
| Total Lines | 110 |
| Code Lines | 73 |
| 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 |
||
| 247 | public static function adminInvitationMail($targets, $introtext, $newtoken, $idpPrettyName, $federation, $type) { |
||
| 248 | if (!in_array($introtext, OutsideComm::INVITE_CONTEXTS)) { |
||
| 249 | throw new \Exception("Unknown invite mode!"); |
||
| 250 | } |
||
| 251 | if ($introtext == OutsideComm::INVITE_CONTEXTS[1] && $federation === NULL) { // comes from fed admin, so federation must be set |
||
| 252 | throw new \Exception("Invitation from a fed admin, but we do not know the corresponding federation!"); |
||
| 253 | } |
||
| 254 | $prettyPrintType = ""; |
||
| 255 | switch ($type) { |
||
| 256 | case \core\IdP::TYPE_IDP: |
||
| 257 | $prettyPrintType = Entity::$nomenclature_inst; |
||
| 258 | break; |
||
| 259 | case \core\IdP::TYPE_SP: |
||
| 260 | $prettyPrintType = Entity::$nomenclature_hotspot; |
||
| 261 | break; |
||
| 262 | case \core\IdP::TYPE_IDPSP: |
||
| 263 | $prettyPrintType = sprintf(_("%s and %s"), Entity::$nomenclature_inst, Entity::$nomenclature_hotspot); |
||
| 264 | break; |
||
| 265 | default: |
||
| 266 | throw new Exception("This is controlled vocabulary, impossible."); |
||
|
|
|||
| 267 | } |
||
| 268 | Entity::intoThePotatoes(); |
||
| 269 | $mail = OutsideComm::mailHandle(); |
||
| 270 | new \core\CAT(); // makes sure Entity is initialised |
||
| 271 | // we have a few stock intro texts on file |
||
| 272 | $introTexts = [ |
||
| 273 | OutsideComm::INVITE_CONTEXTS[0] => sprintf(_("a %s of the %s %s \"%s\" has invited you to manage the %s together with him."), Entity::$nomenclature_fed, CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], Entity::$nomenclature_participant, $idpPrettyName, Entity::$nomenclature_participant), |
||
| 274 | OutsideComm::INVITE_CONTEXTS[1] => sprintf(_("a %s %s has invited you to manage the future %s \"%s\" (%s). The organisation will be a %s."), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], Entity::$nomenclature_fed, Entity::$nomenclature_participant, $idpPrettyName, strtoupper($federation->tld), $prettyPrintType), |
||
| 275 | OutsideComm::INVITE_CONTEXTS[2] => sprintf(_("a %s %s has invited you to manage the %s \"%s\". This is a %s."), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], Entity::$nomenclature_fed, Entity::$nomenclature_participant, $idpPrettyName, $prettyPrintType), |
||
| 276 | ]; |
||
| 277 | $validity = sprintf(_("This invitation is valid for 24 hours from now, i.e. until %s."), strftime("%x %X %Z", time() + 86400)); |
||
| 278 | // need some nomenclature |
||
| 279 | // are we on https? |
||
| 280 | $proto = "http://"; |
||
| 281 | if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") { |
||
| 282 | $proto = "https://"; |
||
| 283 | } |
||
| 284 | // then, send out the mail |
||
| 285 | $message = _("Hello,") . "\n\n" . wordwrap($introTexts[$introtext] . " " . $validity, 72) . "\n\n"; |
||
| 286 | // default means we don't have a Reply-To. |
||
| 287 | $replyToMessage = wordwrap(_("manually. Please do not reply to this mail; this is a send-only address.")); |
||
| 288 | |||
| 289 | if ($federation !== NULL) { |
||
| 290 | // see if we are supposed to add a custom message |
||
| 291 | $customtext = $federation->getAttributes('fed:custominvite'); |
||
| 292 | if (count($customtext) > 0) { |
||
| 293 | $message .= wordwrap(sprintf(_("Additional message from your %s administrator:"), Entity::$nomenclature_fed), 72) . "\n---------------------------------" . |
||
| 294 | wordwrap($customtext[0]['value'], 72) . "\n---------------------------------\n\n"; |
||
| 295 | } |
||
| 296 | // and add Reply-To already now |
||
| 297 | foreach ($federation->listFederationAdmins() as $fedadmin_id) { |
||
| 298 | $fedadmin = new \core\User($fedadmin_id); |
||
| 299 | $mailaddrAttrib = $fedadmin->getAttributes("user:email"); |
||
| 300 | $nameAttrib = $fedadmin->getAttributes("user:realname"); |
||
| 301 | $name = $nameAttrib[0]['value'] ?? sprintf(_("%s administrator"), Entity::$nomenclature_fed); |
||
| 302 | if (count($mailaddrAttrib) > 0) { |
||
| 303 | $mail->addReplyTo($mailaddrAttrib[0]['value'], $name); |
||
| 304 | $replyToMessage = wordwrap(sprintf(_("manually. If you reply to this mail, you will reach your %s administrators."), Entity::$nomenclature_fed), 72); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | $message .= wordwrap(sprintf(_("To enlist as an administrator for that %s, please click on the following link:"), Entity::$nomenclature_participant), 72) . "\n\n" . |
||
| 310 | $proto . $_SERVER['SERVER_NAME'] . CONFIG['PATHS']['cat_base_url'] . "admin/action_enrollment.php?token=$newtoken\n\n" . |
||
| 311 | 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" . |
||
| 312 | $proto . $_SERVER['SERVER_NAME'] . CONFIG['PATHS']['cat_base_url'] . "admin/\n\n" . |
||
| 313 | _("and enter the invitation token") . "\n\n" . |
||
| 314 | $newtoken . "\n\n$replyToMessage\n\n" . |
||
| 315 | 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" . |
||
| 316 | wordwrap(sprintf(_("We wish you a lot of fun with the %s."), CONFIG['APPEARANCE']['productname']), 72) . "\n\n" . |
||
| 317 | sprintf(_("Sincerely,\n\nYour friendly folks from %s Operations"), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name']); |
||
| 318 | |||
| 319 | |||
| 320 | // who to whom? |
||
| 321 | $mail->FromName = CONFIG['APPEARANCE']['productname'] . " Invitation System"; |
||
| 322 | |||
| 323 | if (isset(CONFIG['APPEARANCE']['invitation-bcc-mail']) && CONFIG['APPEARANCE']['invitation-bcc-mail'] !== NULL) { |
||
| 324 | $mail->addBCC(CONFIG['APPEARANCE']['invitation-bcc-mail']); |
||
| 325 | } |
||
| 326 | |||
| 327 | // all addresses are wrapped in a string, but PHPMailer needs a structured list of addressees |
||
| 328 | // sigh... so convert as needed |
||
| 329 | // first split multiple into one if needed |
||
| 330 | $recipients = explode(", ", $targets); |
||
| 331 | |||
| 332 | $secStatus = TRUE; |
||
| 333 | $domainStatus = TRUE; |
||
| 334 | |||
| 335 | // fill the destinations in PHPMailer API |
||
| 336 | foreach ($recipients as $recipient) { |
||
| 337 | $mail->addAddress($recipient); |
||
| 338 | $status = OutsideComm::mailAddressValidSecure($recipient); |
||
| 339 | if ($status < OutsideComm::MAILDOMAIN_STARTTLS) { |
||
| 340 | $secStatus = FALSE; |
||
| 341 | } |
||
| 342 | if ($status < 0) { |
||
| 343 | $domainStatus = FALSE; |
||
| 344 | } |
||
| 345 | } |
||
| 346 | Entity::outOfThePotatoes(); |
||
| 347 | if (!$domainStatus) { |
||
| 348 | return ["SENT" => FALSE, "TRANSPORT" => FALSE]; |
||
| 349 | } |
||
| 350 | |||
| 351 | // what do we want to say? |
||
| 352 | Entity::intoThePotatoes(); |
||
| 353 | $mail->Subject = sprintf(_("%s: you have been invited to manage an %s"), CONFIG['APPEARANCE']['productname'], Entity::$nomenclature_participant); |
||
| 354 | Entity::outOfThePotatoes(); |
||
| 355 | $mail->Body = $message; |
||
| 356 | return ["SENT" => $mail->send(), "TRANSPORT" => $secStatus]; |
||
| 357 | } |
||
| 399 |