Exemplo n.º 1
0
 /**
  * Returns the specified attribute's value for the given user.
  * @param string $cifid The cifID of the user to retrieve data from.
  * @param string $attribute The attribute to return the value for.
  * @return mixed The value of the attribute for the user, or null if it is not set.
  */
 public function get_user_attribute($cifid, $attribute)
 {
     // The array given from LDAP has all keys in lowercase
     $attribute = strtolower($attribute);
     // If this user's data is currently in the cache,
     // we can simply return the cache
     if (!$this->cache_is_for_user($cifid) || !isset(self::$cache[$attribute])) {
         // Do nothing if the user does not exist
         if (!$this->user_exists($cifid)) {
             return array();
         }
         // Reset the cache if it's for a different user
         if (!$this->cache_is_for_user($cifid)) {
             self::$cached_user = $cifid;
             self::$cache_full = false;
             self::$cache = array();
         }
         $dn = $this->get_user_dn($cifid);
         $this->log("Reading attribute `{$attribute}` for `{$dn}`");
         // Accommodate for year field being stored as a group
         if ($attribute == self::YEAR_FIELD) {
             self::$cache[self::YEAR_FIELD] = $this->get_year_group_for($cifid);
             return self::$cache[self::YEAR_FIELD];
         }
         $reader = ldap_read(self::$connection, $dn, self::OBJECT_CLASS_FIELD . '=*', array($attribute));
         if (!$reader) {
             $this->log_and_except('Unable to read from LDAP connection.');
         }
         // Get the entries from our LDAP read
         $values = ldap_get_entries(self::$connection, $reader);
         if (!$values) {
             $this->log_and_except('Unable to get entries from LDAP results.');
         }
         if ($values['count'] != 1) {
             $this->log_and_except('Unable to get single entry from LDAP results.');
         }
         ldap_free_result($reader);
         // Call strtolower on self::$array_fields because the returned keys are all lowercase
         if (in_array($attribute, array_map('strtolower', self::$array_fields))) {
             if (isset($values[0][$attribute])) {
                 // Remove the 'count' index from the array
                 unset($values[0][$attribute]['count']);
                 $values[0] = $values[0][$attribute];
             } else {
                 $values[0] = array();
                 // No value was given, so return an empty array
             }
         } else {
             $values[0] = $values[0][$attribute][0];
         }
         self::$cache[$attribute] = $values[0];
     }
     if (isset(self::$cache[$attribute])) {
         return self::$cache[$attribute];
     }
     return null;
 }