Autoload.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Autoloader Class
  4. *
  5. * PHP Version 5
  6. *
  7. * @file CAS/Autoload.php
  8. * @category Authentication
  9. * @package SimpleCAS
  10. * @author Brett Bieber <brett.bieber@gmail.com>
  11. * @copyright 2008 Regents of the University of Nebraska
  12. * @license http://www1.unl.edu/wdn/wiki/Software_License BSD License
  13. * @link http://code.google.com/p/simplecas/
  14. **/
  15. /**
  16. * Autoload a class
  17. *
  18. * @param string $class Classname to load
  19. *
  20. * @return bool
  21. */
  22. function CAS_autoload($class)
  23. {
  24. // Static to hold the Include Path to CAS
  25. static $include_path;
  26. // Check only for CAS classes
  27. if (substr($class, 0, 4) !== 'CAS_' && substr($class, 0, 7) !== 'PhpCas\\') {
  28. return false;
  29. }
  30. // Setup the include path if it's not already set from a previous call
  31. if (empty($include_path)) {
  32. $include_path = array(dirname(__DIR__));
  33. }
  34. // Declare local variable to store the expected full path to the file
  35. foreach ($include_path as $path) {
  36. $class_path = str_replace('_', DIRECTORY_SEPARATOR, $class);
  37. // PhpCas namespace mapping
  38. if (substr($class_path, 0, 7) === 'PhpCas\\') {
  39. $class_path = 'CAS' . DIRECTORY_SEPARATOR . substr($class_path, 7);
  40. }
  41. $file_path = $path . DIRECTORY_SEPARATOR . $class_path . '.php';
  42. $fp = @fopen($file_path, 'r', true);
  43. if ($fp) {
  44. fclose($fp);
  45. include $file_path;
  46. if (!class_exists($class, false) && !interface_exists($class, false)) {
  47. die(
  48. new Exception(
  49. 'Class ' . $class . ' was not present in ' .
  50. $file_path .
  51. ' [CAS_autoload]'
  52. )
  53. );
  54. }
  55. return true;
  56. }
  57. }
  58. $e = new Exception(
  59. 'Class ' . $class . ' could not be loaded from ' .
  60. $file_path . ', file does not exist (Path="'
  61. . implode(':', $include_path) .'") [CAS_autoload]'
  62. );
  63. $trace = $e->getTrace();
  64. if (isset($trace[2]) && isset($trace[2]['function'])
  65. && in_array($trace[2]['function'], array('class_exists', 'interface_exists'))
  66. ) {
  67. return false;
  68. }
  69. if (isset($trace[1]) && isset($trace[1]['function'])
  70. && in_array($trace[1]['function'], array('class_exists', 'interface_exists'))
  71. ) {
  72. return false;
  73. }
  74. die ((string) $e);
  75. }
  76. // Set up autoload if not already configured by composer.
  77. if (!class_exists('CAS_Client'))
  78. {
  79. trigger_error('phpCAS autoloader is deprecated. Install phpCAS using composer instead.', E_USER_DEPRECATED);
  80. spl_autoload_register('CAS_autoload');
  81. if (function_exists('__autoload')
  82. && !in_array('__autoload', spl_autoload_functions())
  83. ) {
  84. // __autoload() was being used, but now would be ignored, add
  85. // it to the autoload stack
  86. spl_autoload_register('__autoload');
  87. }
  88. }