Complex classes like Util 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Util, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Util |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var array $ini_array Read the cilogon.ini file into an array |
||
| 31 | */ |
||
| 32 | public static $ini_array = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var TimeIt $timeit Initialize by calling static::startTiming() in |
||
| 36 | * init(). |
||
| 37 | */ |
||
| 38 | public static $timeit; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var IdPList $idplist A 'global' IdpList object since dplist.xml is |
||
| 42 | * large and expensive to create multiple times. |
||
| 43 | */ |
||
| 44 | public static $idplist = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var CSRF $csrf A 'global' CSRF token object to set the CSRF cookie |
||
| 48 | * and print the hidden CSRF form element. Needs to be set only once |
||
| 49 | * to keep the same CSRF value through the session. |
||
| 50 | */ |
||
| 51 | public static $csrf = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var Skin $skin A 'global' Skin object for skin configuration. |
||
| 55 | */ |
||
| 56 | public static $skin = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array $oauth2idps An array of OAuth2 Identity Providers. |
||
| 60 | */ |
||
| 61 | public static $oauth2idps = ['Google', 'GitHub', 'ORCID']; |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * getIdPList |
||
| 66 | * |
||
| 67 | * This function initializes the class $idplist object (if not yet |
||
| 68 | * created) and returns it. This allows for a single 'global' |
||
| 69 | * $idplist to be used by other classes (since creating an IdPList |
||
| 70 | * object is expensive). |
||
| 71 | * |
||
| 72 | * @return IdPList The class instantiated IdPList object. |
||
| 73 | **/ |
||
| 74 | public static function getIdpList() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * getCsrf |
||
| 84 | * |
||
| 85 | * This function initializes the class $csrf object (if not yet |
||
| 86 | * created) and returns it. This allows for a single 'global' |
||
| 87 | * $csrf to be used by other classes (since we want the CSRV value |
||
| 88 | * to be consistent for the current page load). |
||
| 89 | * |
||
| 90 | * @return CSRF The class instantiated CSRF object. |
||
| 91 | */ |
||
| 92 | public static function getCsrf() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * getSkin |
||
| 102 | * |
||
| 103 | * This function initializes the class $skin object (if not yet |
||
| 104 | * created) and returns it. This allows for a single 'global' |
||
| 105 | * $skin to be used by other classes (since loading the skin is |
||
| 106 | * potentially expensive). |
||
| 107 | * |
||
| 108 | * @return The class instantiated Skin object. |
||
| 109 | */ |
||
| 110 | public static function getSkin() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * getConfigVar |
||
| 120 | * |
||
| 121 | * This function returns a sinle configuration vale from the |
||
| 122 | * CILOGON_INI_FILE, or empty string if no such configuration |
||
| 123 | * value is found in the file. |
||
| 124 | * |
||
| 125 | * @param string $config The config parameter to read from the |
||
| 126 | * cilogon.ini file. |
||
| 127 | * @return string The value of the config parameter, or empty string |
||
| 128 | * if no such parameter found in config.ini. |
||
| 129 | */ |
||
| 130 | public static function getConfigVar($config) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * startTiming |
||
| 146 | * |
||
| 147 | * This function initializes the class variable $timeit which is |
||
| 148 | * used for timing/benchmarking purposes. |
||
| 149 | */ |
||
| 150 | public static function startTiming() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * getServerVar |
||
| 157 | * |
||
| 158 | * This function queries a given $_SERVER variable (which is set |
||
| 159 | * by the Apache server) and returns the value. |
||
| 160 | * |
||
| 161 | * @param string $serv The $_SERVER variable to query. |
||
| 162 | * @return string The value of the $_SERVER variable or empty string |
||
| 163 | * if that variable is not set. |
||
| 164 | */ |
||
| 165 | public static function getServerVar($serv) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * getGetVar |
||
| 176 | * |
||
| 177 | * This function queries a given $_GET parameter (which is set in |
||
| 178 | * the URL via a '?parameter=value' parameter) and returns the |
||
| 179 | * value. |
||
| 180 | * |
||
| 181 | * @param string $get The $_GET variable to query. |
||
| 182 | * @return string The value of the $_GET variable or empty string if |
||
| 183 | * that variable is not set. |
||
| 184 | */ |
||
| 185 | public static function getGetVar($get) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * getPostVar |
||
| 196 | * |
||
| 197 | * This function queries a given $_POST variable (which is set when |
||
| 198 | * the user submits a form, for example) and returns the value. |
||
| 199 | * |
||
| 200 | * @param string $post The $_POST variable to query. |
||
| 201 | * @return string The value of the $_POST variable or empty string if |
||
| 202 | * that variable is not set. |
||
| 203 | */ |
||
| 204 | public static function getPostVar($post) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * getGetOrPostVar |
||
| 215 | * |
||
| 216 | * This function looks for a $_GET or $_POST variable, with |
||
| 217 | * preference given to $_GET if both are present. |
||
| 218 | * |
||
| 219 | * @param string $var The $_GET or $_POST variable to query. |
||
| 220 | * @return string The value of the $_GET or $_POST variable |
||
| 221 | * if present. Empty string if variable is not set. |
||
| 222 | */ |
||
| 223 | public static function getGetOrPostVar($var) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * getCookieVar |
||
| 234 | * |
||
| 235 | * This function returns the value of a given cookie. |
||
| 236 | * |
||
| 237 | * @param string $cookie he $_COOKIE variable to query. |
||
| 238 | * @return string The value of the $_COOKIE variable or empty string |
||
| 239 | * if that variable is not set. |
||
| 240 | */ |
||
| 241 | public static function getCookieVar($cookie) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * setCookieVar |
||
| 252 | * |
||
| 253 | * This function sets a cookie. |
||
| 254 | * |
||
| 255 | * @param string $cookie The name of the cookie to set. |
||
| 256 | * @param string $value (Optional) The value to set for the cookie. |
||
| 257 | * Defaults to empty string. |
||
| 258 | * @param int $exp The future expiration time (in seconds) of the |
||
| 259 | * cookie. Defaults to 1 year from now. If set to 0, |
||
| 260 | * the cookie expires at the end of the session. |
||
| 261 | */ |
||
| 262 | public static function setCookieVar($cookie, $value = '', $exp = 31536000) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * unsetCookieVar |
||
| 273 | * |
||
| 274 | * This function unsets a cookie. Strictly speaking, the cookie is |
||
| 275 | * not removed, rather it is set to an empty value with an expired |
||
| 276 | * time. |
||
| 277 | * |
||
| 278 | * @param string $cookie The name of the cookie to unset (delete). |
||
| 279 | */ |
||
| 280 | public static function unsetCookieVar($cookie) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * getPortalOrNormalCookieVar |
||
| 288 | * |
||
| 289 | * This is a convenience function which first checks if there is a |
||
| 290 | * OAuth 1.0a ('delegate') or OIDC ('authorize') session active. |
||
| 291 | * If so, it attempts to get the requested cookie from the |
||
| 292 | * associated portalcookie. If there is not an OAuth/OIDC session |
||
| 293 | * active, it looks for a 'normal' cookie. If you need a |
||
| 294 | * portalcookie object to do multiple get/set method calls from |
||
| 295 | * one function, it is probably better NOT to use this method since |
||
| 296 | * creating the portalcookie object is potentially expensive. |
||
| 297 | * |
||
| 298 | * @param string $cookie The name of the cookie to get. |
||
| 299 | * @return string The cookie value from either the portalcookie |
||
| 300 | * (in the case of an active OAuth session) or the |
||
| 301 | * 'normal' cookie. Return empty string if no matching |
||
| 302 | * cookie in either place. |
||
| 303 | */ |
||
| 304 | public static function getPortalOrNormalCookieVar($cookie) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * getSessionVar |
||
| 319 | * |
||
| 320 | * This function returns the value of a given PHP Session variable. |
||
| 321 | * |
||
| 322 | * @param string $sess The $_SESSION variable to query. |
||
| 323 | * @return string The value of the $_SESSION variable or empty string |
||
| 324 | * if that variable is not set. |
||
| 325 | */ |
||
| 326 | public static function getSessionVar($sess) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * setSessionVar |
||
| 337 | * |
||
| 338 | * This function can set or unset a given PHP session variable. |
||
| 339 | * The first parameter is the PHP session variable to set/unset. |
||
| 340 | * If the second parameter is the empty string, then the session |
||
| 341 | * variable is unset. Otherwise, the session variable is set to |
||
| 342 | * the second parameter. The function returns true if the session |
||
| 343 | * variable was set to a non-empty value, false otherwise. |
||
| 344 | * Normally, the return value can be ignored. |
||
| 345 | * |
||
| 346 | * @param string $key The name of the PHP session variable to set |
||
| 347 | * (or unset). |
||
| 348 | * @param string $value (Optional) The value of the PHP session variable |
||
| 349 | * (to set), or empty string (to unset). Defaults to empty |
||
| 350 | * string (implies unset the session variable). |
||
| 351 | * @return bool True if the PHP session variable was set to a |
||
| 352 | * non-empty string, false if variable was unset or if |
||
| 353 | * the specified session variable was not previously set. |
||
| 354 | */ |
||
| 355 | public static function setSessionVar($key, $value = '') |
||
| 368 | |||
| 369 | /** |
||
| 370 | * unsetSessionVar |
||
| 371 | * |
||
| 372 | * This function clears the given PHP session variable by first |
||
| 373 | * setting it to null and then unsetting it entirely. |
||
| 374 | * |
||
| 375 | * @param string $sess The $_SESSION variable to erase. |
||
| 376 | */ |
||
| 377 | public static function unsetSessionVar($sess) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * removeShibCookies |
||
| 387 | * |
||
| 388 | * This function removes all '_shib*' cookies currently in the |
||
| 389 | * user's browser session. In effect, this logs the user out of |
||
| 390 | * any IdP. Note that you must call this before you output any |
||
| 391 | * HTML. Strictly speaking, the cookies are not removed, rather |
||
| 392 | * they are set to empty values with expired times. |
||
| 393 | */ |
||
| 394 | public static function removeShibCookies() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * startPHPSession |
||
| 405 | * |
||
| 406 | * This function starts a secure PHP session and should be called |
||
| 407 | * at the beginning of each script before any HTML is output. It |
||
| 408 | * does a trick of setting a 'lastaccess' time so that the |
||
| 409 | * $_SESSION variable does not expire without warning. |
||
| 410 | * |
||
| 411 | * @param string $storetype (Optional) Storage location of the PHP |
||
| 412 | * session data, one of 'file' or 'mysql'. Defaults to null, |
||
| 413 | * which means use the value of storage.phpsessions from the |
||
| 414 | * cilogon.ini config file, or 'file' if no such |
||
| 415 | * parameter configured. |
||
| 416 | */ |
||
| 417 | public static function startPHPSession($storetype = null) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * getScriptDir |
||
| 438 | * |
||
| 439 | * This function returns the directory (or full url) of the script |
||
| 440 | * that is currently running. The returned directory/url is |
||
| 441 | * terminated by a '/' character (unless the second parameter is |
||
| 442 | * set to true). This function is useful for those scripts named |
||
| 443 | * index.php where we don't want to actually see 'index.php' in the |
||
| 444 | * address bar (again, unless the second parameter is set to true). |
||
| 445 | * |
||
| 446 | * @param bool $prependhttp (Optional) Boolean to prepend 'http(s)://' to |
||
| 447 | * the script name. Defaults to false. |
||
| 448 | * @param bool $stripfile (Optional) Boolean to strip off the trailing |
||
| 449 | * filename (e.g. index.php) from the path. |
||
| 450 | * Defaults to true (i.e., defaults to directory |
||
| 451 | * only without the trailing filename). |
||
| 452 | * @return string The directory or url of the current script, with or |
||
| 453 | * without the trailing .php filename. |
||
| 454 | */ |
||
| 455 | public static function getScriptDir($prependhttp = false, $stripfile = true) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * readArrayFromFile |
||
| 478 | * |
||
| 479 | * This function reads in the contents of a file into an array. It |
||
| 480 | * is assumed that the file contains lines of the form: |
||
| 481 | * key value |
||
| 482 | * where 'key' and 'value' are separated by whitespace. The 'key' |
||
| 483 | * portion of the string may not contain any whitespace, but the |
||
| 484 | * 'value' part of the line may contain whitespace. Any empty lines |
||
| 485 | * or lines starting with '#' (comments, without leading spaces) |
||
| 486 | * in the file are skipped. Note that this assumes that each 'key' |
||
| 487 | * in the file is unique. If there is any problem reading the |
||
| 488 | * file, the resulting array will be empty. |
||
| 489 | * |
||
| 490 | * @param string $filename The name of the file to read. |
||
| 491 | * @return array An array containing the contents of the file. |
||
| 492 | */ |
||
| 493 | public static function readArrayFromFile($filename) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * writeArrayToFile |
||
| 514 | * |
||
| 515 | * This funtion writes an array (with key=>value pairs) to a file, |
||
| 516 | * each line will be of the form: |
||
| 517 | * key value |
||
| 518 | * The 'key' and 'value' strings are separated by a space. Note |
||
| 519 | * that a 'key' may not contain any whitespace (e.g. tabs), but a |
||
| 520 | * 'value' may contain whitespace. To be super safe, the array is |
||
| 521 | * first written to a temporary file, which is then renamed to the |
||
| 522 | * final desired filename. |
||
| 523 | * |
||
| 524 | * @param string $filename The name of the file to write. |
||
| 525 | * @param array $thearray The array to be written to the file. |
||
| 526 | * @return bool True if successfully wrote file, false otherwise. |
||
| 527 | */ |
||
| 528 | public static function writeArrayToFile($filename, $thearray) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * parseGridShibConf |
||
| 552 | * |
||
| 553 | * This function parses the gridshib-ca.conf file and returns an |
||
| 554 | * array containing the various options. It uses the PHP |
||
| 555 | * PEAR::Config package to parse the config file. The |
||
| 556 | * gridshib-ca.conf file is MOSTLY an Apache-style config file. |
||
| 557 | * However, each option has an extra ' = ' prepended, so you will |
||
| 558 | * need to strip these off each config option. For example, to get |
||
| 559 | * the 'MaximumCredLifetime' value which is in the 'CA' section, |
||
| 560 | * you would do the following: |
||
| 561 | * $gridshibconf = Util::parseGridShibConf(); |
||
| 562 | * $life = preg_replace('%^\s*=\s*%','', |
||
| 563 | * $gridshibconf['root']['CA']['MaximumCredLifetime']); |
||
| 564 | * |
||
| 565 | * @param string $conffile (Optional) Full path location of |
||
| 566 | * gridshib-ca.conf file. Defaults to |
||
| 567 | * '/usr/local/gridshib-ca/conf/gridshib-ca.conf'. |
||
| 568 | * @return array An array containing the various configuration |
||
| 569 | * parameters in the gridshib-ca.conf file. |
||
| 570 | */ |
||
| 571 | public static function parseGridShibConf( |
||
| 582 | |||
| 583 | /** |
||
| 584 | * tempDir |
||
| 585 | * |
||
| 586 | * This function creates a temporary subdirectory within the |
||
| 587 | * specified subdirectory. The new directory name is composed of |
||
| 588 | * 16 hexadecimal letters, plus any prefix if you specify one. The |
||
| 589 | * full path of the the newly created directory is returned. |
||
| 590 | * |
||
| 591 | * @param string $dir The full path to the containing directory. |
||
| 592 | * @param string $prefix (Optional) A prefix for the new temporary |
||
| 593 | * directory. Defaults to empty string. |
||
| 594 | * @param int $mode (Optional) Access permissions for the new |
||
| 595 | * temporary directory. Defaults to 0775. |
||
| 596 | * @return string Full path to the newly created temporary directory. |
||
| 597 | */ |
||
| 598 | public static function tempDir($dir, $prefix = '', $mode = 0775) |
||
| 611 | |||
| 612 | /** |
||
| 613 | * deleteDir |
||
| 614 | * |
||
| 615 | * This function deletes a directory and all of its contents. |
||
| 616 | * |
||
| 617 | * @param string $dir The (possibly non-empty) directory to delete. |
||
| 618 | * @param bool $shred (Optional) Shred the file before deleting? |
||
| 619 | * Defaults to false. |
||
| 620 | */ |
||
| 621 | public static function deleteDir($dir, $shred = false) |
||
| 642 | |||
| 643 | /** |
||
| 644 | * htmlent |
||
| 645 | * |
||
| 646 | * This method is necessary since htmlentities() does not seem to |
||
| 647 | * obey the default arguments as documented in the PHP manual, and |
||
| 648 | * instead encodes accented characters incorrectly. By specifying |
||
| 649 | * the flags and encoding, the problem is solved. |
||
| 650 | * |
||
| 651 | * @param string $str : A string to process with htmlentities(). |
||
| 652 | * @return string The input string processed by htmlentities with |
||
| 653 | * specific options. |
||
| 654 | */ |
||
| 655 | public static function htmlent($str) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * sendErrorAlert |
||
| 662 | * |
||
| 663 | * Use this function to send an error message. The $summary should |
||
| 664 | * be a short description of the error since it is placed in the |
||
| 665 | * subject of the email. Put a more verbose description of the |
||
| 666 | * error in the $detail parameter. Any session variables available |
||
| 667 | * are appended to the body of the message. |
||
| 668 | * |
||
| 669 | * @param string $summary A brief summary of the error (in email subject) |
||
| 670 | * @param string $detail A detailed description of the error (in the |
||
| 671 | * email body) |
||
| 672 | * @param string $mailto (Optional) The destination email address. |
||
| 673 | * Defaults to '[email protected]'. |
||
| 674 | */ |
||
| 675 | public static function sendErrorAlert( |
||
| 730 | |||
| 731 | /** |
||
| 732 | * getFirstAndLastName |
||
| 733 | * |
||
| 734 | * This function attempts to get the first and last name of a user |
||
| 735 | * extracted from the 'full name' (displayName) of the user. |
||
| 736 | * Simply pass in all name info (full, first, and last) and the |
||
| 737 | * function first tries to break up the full name into first/last. |
||
| 738 | * If this is not sufficient, the function checks first and last |
||
| 739 | * name. Finally, if either first or last is blank, the function |
||
| 740 | * duplicates first <=> last so both names have the same value. |
||
| 741 | * Note that even with all this, you still need to check if the |
||
| 742 | * returned (first,last) names are blank. |
||
| 743 | * |
||
| 744 | * @param string $full The 'full name' of the user |
||
| 745 | * @param string $first (Optional) The 'first name' of the user |
||
| 746 | * @param string $last (Optional) The 'last name' of the user |
||
| 747 | * @return array An array 'list(firstname,lastname)' |
||
| 748 | */ |
||
| 749 | public static function getFirstAndLastName($full, $first = '', $last = '') |
||
| 780 | |||
| 781 | /** |
||
| 782 | * getHN |
||
| 783 | * |
||
| 784 | * This function calculates and returns the 'hostname' for the |
||
| 785 | * server. It first checks HTTP_HOST. If not set, it returns |
||
| 786 | * 'cilogon.org'. This is needed by command line scripts. |
||
| 787 | * |
||
| 788 | * @return string The 'Hostname' for the web server. |
||
| 789 | */ |
||
| 790 | public static function getHN() |
||
| 798 | |||
| 799 | /** |
||
| 800 | * getDN |
||
| 801 | * |
||
| 802 | * This function calculates and returns the 'domainname' for the |
||
| 803 | * server. It uses the hostname value calculated by getHN() and |
||
| 804 | * uses the last two segments. |
||
| 805 | * |
||
| 806 | * @return string The 'Domainname' for the web server. |
||
| 807 | */ |
||
| 808 | public static function getDN() |
||
| 816 | |||
| 817 | /** |
||
| 818 | * getAuthzUrl |
||
| 819 | * |
||
| 820 | * This funtion takes in the name of an IdP (e.g., 'Google') and |
||
| 821 | * returns the assoicated OAuth2 authorization URL. |
||
| 822 | * |
||
| 823 | * @param string $idp The name of an OAuth2 Identity Provider. |
||
| 824 | * @return string The authorization URL for the given IdP. |
||
| 825 | */ |
||
| 826 | public static function getAuthzUrl($idp) |
||
| 839 | |||
| 840 | /** |
||
| 841 | * getAuthzIdP |
||
| 842 | * |
||
| 843 | * This function takes in the OAuth2 authorization URL and returns |
||
| 844 | * the associated pretty-print name of the IdP. |
||
| 845 | * |
||
| 846 | * @param string $url The authorization URL of an OAuth2 Identity Provider. |
||
| 847 | * @return string The name of the IdP. |
||
| 848 | */ |
||
| 849 | public static function getAuthzIdP($url) |
||
| 862 | |||
| 863 | /** |
||
| 864 | * saveUserToDataStore |
||
| 865 | * |
||
| 866 | * This function is called when a user logs on to save identity |
||
| 867 | * information to the datastore. As it is used by both Shibboleth |
||
| 868 | * and OpenID Identity Providers, some parameters passed in may |
||
| 869 | * be blank (empty string). If the function verifies that the minimal |
||
| 870 | * sets of parameters are valid, the dbservice servlet is called |
||
| 871 | * to save the user info. Then various session variables are set |
||
| 872 | * for use by the program later on. In case of error, an email |
||
| 873 | * alert is sent showing the missing parameters. |
||
| 874 | * |
||
| 875 | * @param mixed $args Variable number of paramters ordered as follows: |
||
| 876 | * remoteuser -The REMOTE_USER from HTTP headers |
||
| 877 | * idp - The provider IdP Identifier / URL endpoint |
||
| 878 | * idpname - The pretty print provider IdP name |
||
| 879 | * firstname - The user's first name |
||
| 880 | * lastname - The user's last name |
||
| 881 | * displayname - The user's display name |
||
| 882 | * emailaddr- The user's email address |
||
| 883 | * loa - The level of assurance (e.g., openid/basic/silver) |
||
| 884 | * ePPN - User's ePPN (for SAML IdPs) |
||
| 885 | * ePTID - User's ePTID (for SAML IdPs) |
||
| 886 | * openidID - User's OpenID 2.0 Identifier (Google deprecated) |
||
| 887 | * oidcID - User's OpenID Connect Identifier |
||
| 888 | * affiliation - User's affiliation |
||
| 889 | * ou - User's organizational unit (OU) |
||
| 890 | * memberof - User's isMemberOf group info |
||
| 891 | * acr - Authentication Context Class Ref |
||
| 892 | * entitlement - User's entitlement |
||
| 893 | */ |
||
| 894 | public static function saveUserToDataStore(...$args) |
||
| 895 | { |
||
| 896 | $dbs = new DBService(); |
||
| 897 | |||
| 898 | // Save the passed-in variables to the session for later use |
||
| 899 | // (e.g., by the error handler in handleGotUser). Then get these |
||
| 900 | // session variables into local vars for ease of use. |
||
| 901 | static::setUserAttributeSessionVars(...$args); |
||
| 902 | $remoteuser = static::getSessionVar('remoteuser'); |
||
| 903 | $idp = static::getSessionVar('idp'); |
||
| 904 | $idpname = static::getSessionVar('idpname'); |
||
| 905 | $firstname = static::getSessionVar('firstname'); |
||
| 906 | $lastname = static::getSessionVar('lastname'); |
||
| 907 | $displayname = static::getSessionVar('displayname'); |
||
| 908 | $emailaddr = static::getSessionvar('emailaddr'); |
||
| 909 | $loa = static::getSessionVar('loa'); |
||
| 910 | $ePPN = static::getSessionVar('ePPN'); |
||
| 911 | $ePTID = static::getSessionVar('ePTID'); |
||
| 912 | $openidID = static::getSessionVar('openidID'); |
||
| 913 | $oidcID = static::getSessionVar('oidcID'); |
||
| 914 | $affiliation = static::getSessionVar('affiliation'); |
||
| 915 | $ou = static::getSessionVar('ou'); |
||
| 916 | $memberof = static::getSessionVar('memberof'); |
||
| 917 | $acr = static::getSessionVar('acr'); |
||
| 918 | $entitlement = static::getSessionVar('entitlement'); |
||
| 919 | |||
| 920 | static::setSessionVar('submit', static::getSessionVar('responsesubmit')); |
||
| 921 | |||
| 922 | // Make sure parameters are not empty strings, and email is valid |
||
| 923 | // Must have at least one of remoteuser/ePPN/ePTID/openidID/oidcID |
||
| 924 | if (((strlen($remoteuser) > 0) || |
||
| 925 | (strlen($ePPN) > 0) || |
||
| 926 | (strlen($ePTID) > 0) || |
||
| 927 | (strlen($openidID) > 0) || |
||
| 928 | (strlen($oidcID) > 0)) && |
||
| 929 | (strlen($idp) > 0) && |
||
| 930 | (strlen($idpname) > 0) && |
||
| 931 | (strlen($firstname) > 0) && |
||
| 932 | (strlen($lastname) > 0) && |
||
| 933 | (strlen($emailaddr) > 0) && |
||
| 934 | (filter_var($emailaddr, FILTER_VALIDATE_EMAIL))) { |
||
| 935 | // For the new Google OAuth 2.0 endpoint, we want to keep the |
||
| 936 | // old Google OpenID endpoint URL in the database (so user does |
||
| 937 | // not get a new certificate subject DN). Change the idp |
||
| 938 | // and idpname to the old Google OpenID values. |
||
| 939 | if (($idpname == 'Google+') || |
||
| 940 | ($idp == static::getAuthzUrl('Google'))) { |
||
| 941 | $idpname = 'Google'; |
||
| 942 | $idp = 'https://www.google.com/accounts/o8/id'; |
||
| 943 | } |
||
| 944 | |||
| 945 | // In the database, keep a consistent ProviderId format: only |
||
| 946 | // allow 'http' (not 'https') and remove any 'www.' prefix. |
||
| 947 | if ($loa == 'openid') { |
||
| 948 | $idp = preg_replace('%^https://(www\.)?%', 'http://', $idp); |
||
| 949 | } |
||
| 950 | |||
| 951 | $result = $dbs->getUser( |
||
| 952 | $remoteuser, |
||
| 953 | $idp, |
||
| 954 | $idpname, |
||
| 955 | $firstname, |
||
| 956 | $lastname, |
||
| 957 | $displayname, |
||
| 958 | $emailaddr, |
||
| 959 | $ePPN, |
||
| 960 | $ePTID, |
||
| 961 | $openidID, |
||
| 962 | $oidcID, |
||
| 963 | $affiliation, |
||
| 964 | $ou, |
||
| 965 | $memberof, |
||
| 966 | $acr, |
||
| 967 | $entitlement |
||
| 968 | ); |
||
| 969 | static::setSessionVar('uid', $dbs->user_uid); |
||
| 970 | static::setSessionVar('dn', $dbs->distinguished_name); |
||
| 971 | static::setSessionVar('twofactor', $dbs->two_factor); |
||
| 972 | static::setSessionVar('status', $dbs->status); |
||
| 973 | if (!$result) { |
||
| 974 | static::sendErrorAlert( |
||
| 975 | 'dbService Error', |
||
| 976 | 'Error calling dbservice action "getUser" in ' . |
||
| 977 | 'saveUserToDatastore() method.' |
||
| 978 | ); |
||
| 979 | } |
||
| 980 | } else { // Missing one or more required attributes |
||
| 981 | static::setSessionVar( |
||
| 982 | 'status', |
||
| 983 | DBService::$STATUS['STATUS_MISSING_PARAMETER_ERROR'] |
||
| 984 | ); |
||
| 985 | } |
||
| 986 | |||
| 987 | // If 'status' is not STATUS_OK*, then send an error email |
||
| 988 | $status = static::getSessionVar('status'); |
||
| 989 | if ($status & 1) { // Bad status codes are odd |
||
| 990 | // For missing parameter errors, log an error message |
||
| 991 | if ($status == |
||
| 992 | DBService::$STATUS['STATUS_MISSING_PARAMETER_ERROR']) { |
||
| 993 | $log = new Loggit(); |
||
| 994 | $log->error('STATUS_MISSING_PARAMETER_ERROR', true); |
||
| 995 | } |
||
| 996 | |||
| 997 | // For other dbservice errors OR for any error involving |
||
| 998 | // LIGO (e.g., missing parameter error), send email alert. |
||
| 999 | if (($status != |
||
| 1000 | DBService::$STATUS['STATUS_MISSING_PARAMETER_ERROR']) || |
||
| 1001 | (preg_match('/ligo\.org/', $idp))) { |
||
| 1002 | $mailto = '[email protected]'; |
||
| 1003 | |||
| 1004 | // Set $disableligoalerts = true to stop LIGO failures |
||
| 1005 | // from being sent to '[email protected]', but still |
||
| 1006 | // sent to '[email protected]'. |
||
| 1007 | $disableligoalerts = false; |
||
| 1008 | |||
| 1009 | // Fixes CIL-205 - Notify LIGO about IdP login errors |
||
| 1010 | if (preg_match('/ligo\.org/', $idp)) { |
||
| 1011 | if ($disableligoalerts) { |
||
| 1012 | $mailto = ''; |
||
| 1013 | } |
||
| 1014 | $mailto .= ((strlen($mailto) > 0) ? ',' : '') . |
||
| 1015 | '[email protected]'; |
||
| 1016 | } |
||
| 1017 | |||
| 1018 | static::sendErrorAlert( |
||
| 1019 | 'Failure in ' . |
||
| 1020 | (($loa == 'openid') ? '' : '/secure') . '/getuser/', |
||
| 1021 | 'Remote_User = ' . ((strlen($remoteuser) > 0) ? |
||
| 1022 | $remoteuser : '<MISSING>') . "\n" . |
||
| 1023 | 'IdP ID = ' . ((strlen($idp) > 0) ? |
||
| 1024 | $idp : '<MISSING>') . "\n" . |
||
| 1025 | 'IdP Name = ' . ((strlen($idpname) > 0) ? |
||
| 1026 | $idpname : '<MISSING>') . "\n" . |
||
| 1027 | 'First Name = ' . ((strlen($firstname) > 0) ? |
||
| 1028 | $firstname : '<MISSING>') . "\n" . |
||
| 1029 | 'Last Name = ' . ((strlen($lastname) > 0) ? |
||
| 1030 | $lastname : '<MISSING>') . "\n" . |
||
| 1031 | 'Display Name = ' . ((strlen($displayname) > 0) ? |
||
| 1032 | $displayname : '<MISSING>') . "\n" . |
||
| 1033 | 'Email Address = ' . ((strlen($emailaddr) > 0) ? |
||
| 1034 | $emailaddr : '<MISSING>') . "\n" . |
||
| 1035 | 'ePPN = ' . ((strlen($ePPN) > 0) ? |
||
| 1036 | $ePPN : '<MISSING>') . "\n" . |
||
| 1037 | 'ePTID = ' . ((strlen($ePTID) > 0) ? |
||
| 1038 | $ePTID : '<MISSING>') . "\n" . |
||
| 1039 | 'OpenID ID = ' . ((strlen($openidID) > 0) ? |
||
| 1040 | $openidID : '<MISSING>') . "\n" . |
||
| 1041 | 'OIDC ID = ' . ((strlen($oidcID) > 0) ? |
||
| 1042 | $oidcID : '<MISSING>') . "\n" . |
||
| 1043 | 'Affiliation = ' . ((strlen($affiliation) > 0) ? |
||
| 1044 | $affiliation : '<MISSING>') . "\n" . |
||
| 1045 | 'OU = ' . ((strlen($ou) > 0) ? |
||
| 1046 | $ou : '<MISSING>') . "\n" . |
||
| 1047 | 'MemberOf = ' . ((strlen($memberof) > 0) ? |
||
| 1048 | $memberof : '<MISSING>') . "\n" . |
||
| 1049 | 'ACR = ' . ((strlen($acr) > 0) ? |
||
| 1050 | $acr : '<MISSING>') . "\n" . |
||
| 1051 | 'Entitlement = ' . ((strlen($entitlement) > 0) ? |
||
| 1052 | $entitlement : '<MISSING>') . "\n" . |
||
| 1053 | 'Database UID = ' . ((strlen( |
||
| 1054 | $i = static::getSessionVar('uid') |
||
| 1055 | ) > 0) ? $i : '<MISSING>') . "\n" . |
||
| 1056 | 'Status Code = ' . ((strlen( |
||
| 1057 | $i = array_search( |
||
| 1058 | $status, |
||
| 1059 | DBService::$STATUS |
||
| 1060 | ) |
||
| 1061 | ) > 0) ? $i : '<MISSING>'), |
||
| 1062 | $mailto |
||
| 1063 | ); |
||
| 1064 | } |
||
| 1065 | static::unsetSessionVar('authntime'); |
||
| 1066 | } else { // status is okay, set authntime |
||
| 1067 | static::setSessionVar('authntime', time()); |
||
| 1068 | } |
||
| 1069 | |||
| 1070 | static::unsetSessionVar('responsesubmit'); |
||
| 1071 | static::unsetSessionVar('requestsilver'); |
||
| 1072 | |||
| 1073 | static::getCsrf()->setCookieAndSession(); |
||
| 1074 | } |
||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * setUserAttributeSessionVars |
||
| 1078 | * |
||
| 1079 | * This method is called by saveUserToDatastore to put the passsed-in |
||
| 1080 | * variables into the PHP session for later use. |
||
| 1081 | * |
||
| 1082 | * @param mixed $args Variable number of user attribute paramters |
||
| 1083 | * ordered as shown in the $attrs array below. |
||
| 1084 | */ |
||
| 1085 | public static function setUserAttributeSessionVars(...$args) |
||
| 1086 | { |
||
| 1087 | $attrs = array('remoteuser', 'idp', 'idpname', 'firstname', |
||
| 1088 | 'lastname', 'displayname', 'emailaddr', |
||
| 1089 | 'loa', 'ePPN', 'ePITD', 'openidID', 'oidcID', |
||
| 1090 | 'affiliation', 'ou', 'memberof', 'acr', |
||
| 1091 | 'entitlement'); |
||
| 1092 | for ($i = 0; $i < count($args); $i++) { |
||
| 1093 | static::setSessionVar($attrs[$i], $args[$i]); |
||
| 1094 | } |
||
| 1095 | |||
| 1096 | // CACC-238 - Set loa to "silver" if the following are true: |
||
| 1097 | // (1) loa contains https://refeds.org/assurance/profile/cappuccino |
||
| 1098 | // (2) acr is either https://refeds.org/profile/sfa or |
||
| 1099 | // https://refeds.org/profile/mfa |
||
| 1100 | if ((preg_match('%https://refeds.org/assurance/profile/cappuccino%', static::getSessionVar('loa'))) && |
||
| 1101 | (preg_match('%https://refeds.org/profile/[ms]fa%', static::getSessionVar('acr')))) { |
||
| 1102 | static::setSessionVar('loa', 'http://incommonfederation.org/assurance/silver'); |
||
| 1103 | } |
||
| 1104 | } |
||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * unsetClientSessionVars |
||
| 1108 | * |
||
| 1109 | * This function removes all of the PHP session variables related to |
||
| 1110 | * the client session. |
||
| 1111 | */ |
||
| 1112 | public static function unsetClientSessionVars() |
||
| 1133 | |||
| 1134 | /** |
||
| 1135 | * unsetUserSessionVars |
||
| 1136 | * |
||
| 1137 | * This function removes all of the PHP session variables related to |
||
| 1138 | * the user's session. This will force the user to log on (again) |
||
| 1139 | * with their IdP and call the 'getuser' script to repopulate the PHP |
||
| 1140 | * session. |
||
| 1141 | */ |
||
| 1142 | public static function unsetUserSessionVars() |
||
| 1174 | |||
| 1175 | /** |
||
| 1176 | * unsetAllUserSessionVars |
||
| 1177 | * |
||
| 1178 | * This is a convenience method to clear all session variables related |
||
| 1179 | * to the client and the user. |
||
| 1180 | */ |
||
| 1181 | public static function unsetAllUserSessionVars() |
||
| 1186 | |||
| 1187 | /** |
||
| 1188 | * verifySessionAndCall |
||
| 1189 | * |
||
| 1190 | * This function is a convenience method called by several cases in the |
||
| 1191 | * main 'switch' call at the top of the index.php file. I noticed |
||
| 1192 | * a pattern where verifyCurrentUserSession() was called to verify the |
||
| 1193 | * current user session. Upon success, one or two functions were called |
||
| 1194 | * to continue program, flow. Upon failure, cookies and session |
||
| 1195 | * variables were cleared, and the main Logon page was printed. This |
||
| 1196 | * function encapsulates that pattern. If the user's session is valid, |
||
| 1197 | * the passed-in $func is called, possibly with parameters passed in as |
||
| 1198 | * an array. The function returns true if the session is verified, so |
||
| 1199 | * that other functions may be called upon return. |
||
| 1200 | * |
||
| 1201 | * @param function $func The function to call if the current session is |
||
| 1202 | * successfully verified. |
||
| 1203 | * @param array $params (Optional) An array of parameters to pass to the |
||
| 1204 | 8 function. Defaults to empty array, meaning zero parameters. |
||
| 1205 | */ |
||
| 1206 | public static function verifySessionAndCall($func, $params = array()) |
||
| 1217 | } |
||
| 1218 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.