1: <?php
2: /**
3: * A library for accessing the Kolab user database.
4: *
5: * PHP version 5
6: *
7: * @category Kolab
8: * @package Kolab_Server
9: * @author Gunnar Wrobel <wrobel@pardus.de>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://pear.horde.org/index.php?package=Kolab_Server
12: */
13:
14: /**
15: * This class provides methods to deal with Kolab objects stored in
16: * the Kolab object db.
17: *
18: * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
19: *
20: * See the enclosed file COPYING for license information (LGPL). If you
21: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
22: *
23: * @category Kolab
24: * @package Kolab_Server
25: * @author Gunnar Wrobel <wrobel@pardus.de>
26: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
27: * @link http://pear.horde.org/index.php?package=Kolab_Server
28: */
29: class Horde_Kolab_Server_Objects_Base
30: implements Horde_Kolab_Server_Objects_Interface
31: {
32: /**
33: * A link to the composite server handler.
34: *
35: * @var Horde_Kolab_Server_Composite
36: */
37: private $_composite;
38:
39: /**
40: * Set the composite server reference for this object.
41: *
42: * @param Horde_Kolab_Server_Composite $composite A link to the composite
43: * server handler.
44: *
45: * @return NULL
46: */
47: public function setComposite(
48: Horde_Kolab_Server_Composite $composite
49: ) {
50: $this->_composite = $composite;
51: }
52:
53: /**
54: * Fetch a Kolab object.
55: *
56: * This method will not retrieve any data from the server
57: * immediately. Instead it will simply generate a new instance for the
58: * desired object.
59: *
60: * The server data will only be accessed once you start reading the object
61: * data.
62: *
63: * This method can also be used in order to fetch non-existing objects that
64: * will be saved later. This is however not recommended and you should
65: * rather use the add($info) method for that.
66: *
67: * If you do not provide the object type the server will try to determine it
68: * automatically based on the uid. As this requires reading data from the
69: * server it is recommended to specify the object type whenever it is known.
70: *
71: * If you do not specify a uid the object corresponding to the user bound to
72: * the server will be returned.
73: *
74: * @param string $guid The GUID of the object to fetch.
75: * @param string $type The type of the object to fetch.
76: *
77: * @return Kolab_Object The corresponding Kolab object.
78: *
79: * @throws Horde_Kolab_Server_Exception
80: */
81: public function fetch($guid = null, $type = null)
82: {
83: if (!isset($guid)) {
84: $guid = $this->_composite->server->getGuid();
85: }
86: if (empty($type)) {
87: $type = $this->_composite->structure->determineType($guid);
88: }
89:
90: $object = Horde_Kolab_Server_Object_Factory::factory(
91: $type, $guid, $this->_composite
92: );
93: return $object;
94: }
95:
96: /**
97: * Add a Kolab object.
98: *
99: * @param array $info The object to store.
100: *
101: * @return Kolab_Object The newly created Kolab object.
102: *
103: * @throws Horde_Kolab_Server_Exception If the type of the object to add has
104: * been left undefined or the object
105: * already exists.
106: */
107: public function add(array $info)
108: {
109: if (!isset($info['type'])) {
110: throw new Horde_Kolab_Server_Exception(
111: 'The type of a new object must be specified!');
112: }
113:
114: $type = $info['type'];
115: unset($info['type']);
116: $object = &Horde_Kolab_Server_Object::factory($type, null, $this, $info);
117: if ($object->exists()) {
118: throw new Horde_Kolab_Server_Exception(
119: sprintf("The object with the uid \"%s\" does already exist!",
120: $object->get(Horde_Kolab_Server_Object::ATTRIBUTE_UID)));
121: }
122: $object->save();
123: return $object;
124: }
125:
126: /**
127: * Generate a hash representation for a list of objects.
128: *
129: * The approach taken here is somewhat slow as the server data gets fetched
130: * into objects first which are then converted to hashes again. Since a
131: * server search will usually deliver the result as a hash the intermediate
132: * object conversion is inefficient.
133: *
134: * But as the object classes are able to treat the attributes returned from
135: * the server with custom parsing, this is currently the preferred
136: * method. Especially for large result sets it would be better if this
137: * method would call a static object class function that operate on the
138: * result array returned from the server without using objects.
139: *
140: * @param string $type The type of the objects to be listed
141: * @param array $params Additional parameters.
142: *
143: * @return array An array of Kolab objects.
144: *
145: * @throws Horde_Kolab_Server_Exception
146: *
147: * @todo The LDAP driver needs a more efficient version of this call as it
148: * is not required to generate objects before returning data as a
149: * hash. It can be derived directly from the LDAP result.
150: */
151: public function listHash($type, $params = null)
152: {
153: $list = $this->listObjects($type, $params);
154:
155: if (isset($params['attributes'])) {
156: $attributes = $params['attributes'];
157: } else {
158: $attributes = null;
159: }
160:
161: $hash = array();
162: foreach ($list as $uid => $entry) {
163: $hash[$uid] = $entry->toHash($attributes);
164: }
165:
166: return $hash;
167: }
168:
169:
170: /**
171: * List all objects of a specific type.
172: *
173: * @param string $type The type of the objects to be listed
174: * @param array $params Additional parameters.
175: *
176: * @return array An array of Kolab objects.
177: *
178: * @throws Horde_Kolab_Server_Exception
179: *
180: * @todo Sorting
181: * @todo Is this LDAP specific?
182: */
183: public function listObjects($type, $params = null)
184: {
185: if (empty($params['base_dn'])) {
186: $base = $this->_base_dn;
187: } else {
188: $base = $params['base_dn'];
189: }
190:
191: $result = Horde_Kolab_Server_Object::loadClass($type);
192: $vars = get_class_vars($type);
193: $criteria = call_user_func(array($type, 'getFilter'));
194: $filter = $this->searchQuery($criteria);
195: $sort = $vars['sort_by'];
196:
197: if (isset($params['sort'])) {
198: $sort = $params['sort'];
199: }
200:
201: $options = array('scope' => 'sub');
202: if (isset($params['attributes'])) {
203: $options['attributes'] = $params['attributes'];
204: } else {
205: $options['attributes'] = $this->getAttributes($type);
206: }
207:
208: $data = $this->search($filter, $options, $base);
209: if (empty($data)) {
210: return array();
211: }
212:
213: if ($sort) {
214: /* @todo: sorting */
215: /* $data = $result->as_sorted_struct(); */
216: /*$this->sort($result, $sort); */
217: }
218:
219: if (isset($params['from'])) {
220: $from = $params['from'];
221: } else {
222: $from = -1;
223: }
224:
225: if (isset($params['to'])) {
226: $sort = $params['to'];
227: } else {
228: $to = -1;
229: }
230:
231: if (!empty($vars['required_group'])) {
232: $required_group = new Horde_Kolab_Server_Object_Kolabgroupofnames($this,
233: null,
234: $vars['required_group']);
235: }
236:
237: $objects = array();
238: foreach ($data as $uid => $entry) {
239: if (!empty($vars['required_group'])) {
240: if (!$required_group->exists() || !$required_group->isMember($uid)) {
241: continue;
242: }
243: }
244: $objects[$uid] = &Horde_Kolab_Server_Object::factory($type, $uid,
245: $this, $entry);
246: }
247: return $objects;
248: }
249:
250: }