1: <?php
2: /**
3: * Sam base class.
4: *
5: * Copyright 2002-2012 Horde LLC (http://www.horde.org/)
6: *
7: * See the enclosed file COPYING for license information (GPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/gpl.
9: *
10: * @author Chris Bowlby <cbowlby@tenthpowertech.com>
11: * @author Max Kalika <max@horde.org>
12: * @author Jan Schneider <jan@horde.org>
13: * @package Sam
14: */
15: class Sam
16: {
17: /**
18: * Determines the backend to use.
19: *
20: * This decision is based on the global 'SERVER_NAME' and 'HTTP_HOST'
21: * server variables and the contents of the 'preferred' either field
22: * in the backend's definition. The 'preferred' field may take a
23: * single value or an array of multiple values.
24: *
25: * @return array The backend entry.
26: * @throws Sam_Exception
27: */
28: static public function getPreferredBackend()
29: {
30: $backends = Horde::loadConfiguration('backends.php', 'backends');
31:
32: if (!isset($backends) || !is_array($backends)) {
33: throw new Sam_Exception(_("No backends configured in backends.php"));
34: }
35:
36: foreach ($backends as $temp) {
37: if (!empty($temp['disabled'])) {
38: continue;
39: }
40: if (!isset($backend)) {
41: $backend = $temp;
42: } elseif (!empty($temp['preferred'])) {
43: if (is_array($temp['preferred'])) {
44: foreach ($temp['preferred'] as $val) {
45: if (($val == $_SERVER['SERVER_NAME']) ||
46: ($val == $_SERVER['HTTP_HOST'])) {
47: $backend = $temp;
48: }
49: }
50: } elseif (($temp['preferred'] == $_SERVER['SERVER_NAME']) ||
51: ($temp['preferred'] == $_SERVER['HTTP_HOST'])) {
52: $backend = $temp;
53: }
54: }
55: }
56:
57: /* Check for valid backend configuration. */
58: if (!isset($backend)) {
59: throw new Sam_Exception(_("No backend configured for this host"));
60: }
61: if (empty($backend['driver'])) {
62: throw new Sam_Exception(sprintf(_("No \"%s\" element found in backend configuration."), 'driver'));
63: }
64:
65: /* Make sure the 'params' entry exists. */
66: if (!isset($backend['params'])) {
67: $backend['params'] = array();
68: }
69:
70: return $backend;
71: }
72:
73: /**
74: * Find a list of configured attributes.
75: *
76: * Load the attributes configuration file or uses an already-loaded
77: * cached copy. If loading for the first time, cache it for later use.
78: *
79: * @return array The attributes list.
80: */
81: static public function getAttributes()
82: {
83: static $_attributes;
84:
85: if (!isset($_attributes)) {
86: $_attributes = Horde::loadConfiguration('attributes.php', '_attributes');
87: }
88:
89: return $_attributes;
90: }
91:
92: /**
93: * Find out whether the given attribute type is informational only.
94: *
95: * @param string $attribute The attribute type to check.
96: *
97: * @return boolean Returns true if the given type is known to be
98: * informational only.
99: */
100: static public function infoAttribute($type = '')
101: {
102: return in_array(
103: $type,
104: array('description', 'spacer', 'html', 'header', 'link'));
105: }
106:
107: /**
108: * Converts the current user's name, optionally removing the domain part or
109: * applying any configured hooks.
110: *
111: * @param string|boolean $hordeauth Defines how to use the authenticated
112: * Horde username. If set to 'full',
113: * will initialize the username to
114: * contain the @realm part. Otherwise,
115: * the username will initialize as a
116: * simple login.
117: *
118: * @return string The converted username.
119: */
120: static public function mapUser($hordeauth)
121: {
122: $uid = $GLOBALS['registry']->getAuth($hordeauth === 'full' ? null : 'bare');
123: try {
124: return Horde::callHook('username', array($uid), 'sam');
125: } catch (Horde_Exception_HookNotSet $e) {
126: return $uid;
127: }
128: }
129: }
130: