Autoload.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_') {
  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(dirname(__FILE__)), dirname(dirname(__FILE__)) . '/../test/' );
  33. }
  34. // Declare local variable to store the expected full path to the file
  35. foreach ($include_path as $path) {
  36. $file_path = $path . '/' . str_replace('_', '/', $class) . '.php';
  37. $fp = @fopen($file_path, 'r', true);
  38. if ($fp) {
  39. fclose($fp);
  40. include $file_path;
  41. if (!class_exists($class, false) && !interface_exists($class, false)) {
  42. die(
  43. new Exception(
  44. 'Class ' . $class . ' was not present in ' .
  45. $file_path .
  46. ' [CAS_autoload]'
  47. )
  48. );
  49. }
  50. return true;
  51. }
  52. }
  53. $e = new Exception(
  54. 'Class ' . $class . ' could not be loaded from ' .
  55. $file_path . ', file does not exist (Path="'
  56. . implode(':', $include_path) .'") [CAS_autoload]'
  57. );
  58. $trace = $e->getTrace();
  59. if (isset($trace[2]) && isset($trace[2]['function'])
  60. && in_array($trace[2]['function'], array('class_exists', 'interface_exists'))
  61. ) {
  62. return false;
  63. }
  64. if (isset($trace[1]) && isset($trace[1]['function'])
  65. && in_array($trace[1]['function'], array('class_exists', 'interface_exists'))
  66. ) {
  67. return false;
  68. }
  69. die ((string) $e);
  70. }
  71. // set up __autoload
  72. if (!(spl_autoload_functions())
  73. || !in_array('CAS_autoload', spl_autoload_functions())
  74. ) {
  75. spl_autoload_register('CAS_autoload');
  76. if (function_exists('__autoload')
  77. && !in_array('__autoload', spl_autoload_functions())
  78. ) {
  79. // __autoload() was being used, but now would be ignored, add
  80. // it to the autoload stack
  81. spl_autoload_register('__autoload');
  82. }
  83. }
  84. ?>