shibboleth_store.class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace Shibboleth;
  3. /**
  4. * Returns Shibboleth user's values based on Shibboleth's configuration.
  5. * Shibboleth returns not only whether a user is authenticated but returns as
  6. * well several paralemeter fields.
  7. *
  8. * If a user is not authenticated nothing is returned.
  9. *
  10. * @license see /license.txt
  11. * @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
  12. */
  13. class ShibbolethStore
  14. {
  15. /**
  16. *
  17. * @return ShibbolethStore
  18. */
  19. public static function instance()
  20. {
  21. static $result = false;
  22. if (empty($result))
  23. {
  24. $result = new self();
  25. }
  26. return $result;
  27. }
  28. /**
  29. *
  30. * @return ShibbolethConfig
  31. */
  32. public static function config()
  33. {
  34. return Shibboleth::config();
  35. }
  36. public function get_unique_id()
  37. {
  38. return $this->get(__FUNCTION__);
  39. }
  40. /**
  41. * If the user has more than one surname, it is possible depending of the user
  42. * home organization that they are all given to the resource.
  43. * In the case of the University of Geneva, with two surnames, three different values
  44. * for the surname are sent. They are:
  45. * 1) "givenname1"
  46. * 2) "givenname2"
  47. * 3) "givenname1 givenname2"
  48. * meaning the string is as follow: "givenname1;givenname2;givenname1 givenname2"
  49. *
  50. * In such a case, the correct surname is the one which is followed by a space.
  51. * This function tests if such a situation is encountered, and returns the first given name.
  52. *
  53. * @author Nicolas Rod
  54. */
  55. public function get_firstname()
  56. {
  57. $result = $this->get(__FUNCTION__);
  58. if (!is_array($result))
  59. {
  60. $result = ucfirst($result);
  61. return $result;
  62. }
  63. foreach ($result as $name)
  64. {
  65. $parts = explode(' ', $name);
  66. if (count($parts) > 1)
  67. {
  68. $result = reset($parts);
  69. $result = ucfirst($result);
  70. return $result;
  71. }
  72. }
  73. $result = reset($result);
  74. $result = ucfirst($result);
  75. return $result;
  76. }
  77. public function get_lastname()
  78. {
  79. $result = $this->get(__FUNCTION__);
  80. $result = ucfirst($result);
  81. return $result;
  82. }
  83. public function get_email()
  84. {
  85. return $this->get(__FUNCTION__);
  86. }
  87. public function get_language()
  88. {
  89. return $this->get(__FUNCTION__);
  90. }
  91. public function get_gender()
  92. {
  93. return $this->get(__FUNCTION__);
  94. }
  95. public function get_address()
  96. {
  97. return $this->get(__FUNCTION__);
  98. }
  99. public function get_staff_category()
  100. {
  101. return $this->get(__FUNCTION__);
  102. }
  103. public function get_home_organization_type()
  104. {
  105. return $this->get(__FUNCTION__);
  106. }
  107. public function get_home_organization()
  108. {
  109. return $this->get(__FUNCTION__);
  110. }
  111. public function get_affiliation()
  112. {
  113. return $this->get(__FUNCTION__);
  114. }
  115. /**
  116. * @return ShibbolethUser
  117. */
  118. public function get_user()
  119. {
  120. $result = new ShibbolethUser();
  121. foreach ($result as $key => $val)
  122. {
  123. $f = array($this, "get_$key");
  124. if (is_callable($f))
  125. {
  126. $result->{$key} = call_user_func($f);
  127. }
  128. }
  129. return $result;
  130. }
  131. /**
  132. * Returns the shibboleth value stored in $_SERVER if it exists or $default if it is not the case.
  133. *
  134. * @param string $name the generic name. I.e. one of the class const.
  135. * @param string $default default value if it is not provided by Shibboleth
  136. * @return string
  137. */
  138. public function get($name = '', $default = '')
  139. {
  140. $config = (array) Shibboleth::config();
  141. if ($name)
  142. {
  143. $name = str_replace('get_', '', $name);
  144. $shib_name = isset($config[$name]) ? $config[$name] : '';
  145. if ($shib_name)
  146. {
  147. $result = isset($_SERVER[$shib_name]) ? $_SERVER[$shib_name] : $default;
  148. $result = explode(';', $result);
  149. if (empty($result))
  150. {
  151. $result = $default;
  152. }
  153. else if (count($result) == 1)
  154. {
  155. $result = reset($result);
  156. }
  157. else
  158. {
  159. $result = $result;
  160. }
  161. return $result;
  162. }
  163. }
  164. $result = array();
  165. foreach ($config as $key => $val)
  166. {
  167. $f = array($this, "get_$key");
  168. if (is_callable($f))
  169. {
  170. $result[$key] = call_user_func($f);
  171. }
  172. }
  173. return $result;
  174. }
  175. }