1: <?php
2: /**
3: * Handler for LDAP server queries.
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: * Handler for LDAP server queries.
16: *
17: * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you
20: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21: *
22: * @category Kolab
23: * @package Kolab_Server
24: * @author Gunnar Wrobel <wrobel@pardus.de>
25: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
26: * @link http://pear.horde.org/index.php?package=Kolab_Server
27: */
28: class Horde_Kolab_Server_Query_Ldap
29: implements Horde_Kolab_Server_Query_Interface
30: {
31: /**
32: * The query criteria.
33: *
34: * @var Horde_Kolab_Server_Query_Element
35: */
36: private $_criteria;
37:
38: /**
39: * The db structure.
40: *
41: * @var Horde_Kolab_Server_Structure
42: */
43: private $_structure;
44:
45: /**
46: * Constructor.
47: *
48: * @param array $criteria The query criteria.
49: */
50: public function __construct(
51: Horde_Kolab_Server_Query_Element_Interface $criteria,
52: Horde_Kolab_Server_Structure_Interface $structure
53: ) {
54: $this->_criteria = $criteria;
55: $this->_structure = $structure;
56: }
57:
58: /**
59: * Return the query as a string.
60: *
61: * @return string The query in string format.
62: *
63: * @throws Horde_Kolab_Server_Exception If the query is malformed.
64: */
65: public function __toString()
66: {
67: try {
68: $filter = $this->_criteria->convert($this);
69: return (string)$filter;
70: } catch (Horde_Kolab_Server_Exception $e) {
71: return '';
72: }
73: }
74:
75: /**
76: * Convert the equals element to query format.
77: *
78: * @param Horde_Kolab_Server_Query_Element_Equals $equals The element to convert.
79: *
80: * @return mixed The query element in query format.
81: *
82: * @throws Horde_Kolab_Server_Exception If the query is malformed.
83: */
84: public function convertEquals(Horde_Kolab_Server_Query_Element_Equals $equals)
85: {
86: return $this->_convertSingle($equals, 'equals');
87: }
88:
89: /**
90: * Convert the begins element to query format.
91: *
92: * @param Horde_Kolab_Server_Query_Element_Begins $begins The element to convert.
93: *
94: * @return mixed The query element in query format.
95: *
96: * @throws Horde_Kolab_Server_Exception If the query is malformed.
97: */
98: public function convertBegins(Horde_Kolab_Server_Query_Element_Begins $begins)
99: {
100: return $this->_convertSingle($begins, 'begins');
101: }
102:
103: /**
104: * Convert the ends element to query format.
105: *
106: * @param Horde_Kolab_Server_Query_Element_Ends $ends The element to convert.
107: *
108: * @return mixed The query element in query format.
109: *
110: * @throws Horde_Kolab_Server_Exception If the query is malformed.
111: */
112: public function convertEnds(Horde_Kolab_Server_Query_Element_Ends $ends)
113: {
114: return $this->_convertSingle($ends, 'ends');
115: }
116:
117: /**
118: * Convert the contains element to query format.
119: *
120: * @param Horde_Kolab_Server_Query_Element_Contains $contains The element to convert.
121: *
122: * @return mixed The query element in query format.
123: *
124: * @throws Horde_Kolab_Server_Exception If the query is malformed.
125: */
126: public function convertContains(Horde_Kolab_Server_Query_Element_Contains $contains)
127: {
128: return $this->_convertSingle($contains, 'contains');
129: }
130:
131: /**
132: * Convert the less element to query format.
133: *
134: * @param Horde_Kolab_Server_Query_Element_Less $less The element to convert.
135: *
136: * @return mixed The query element in query format.
137: *
138: * @throws Horde_Kolab_Server_Exception If the query is malformed.
139: */
140: public function convertLess(Horde_Kolab_Server_Query_Element_Less $less)
141: {
142: return $this->_convertSingle($less, 'less');
143: }
144:
145: /**
146: * Convert the greater element to query format.
147: *
148: * @param Horde_Kolab_Server_Query_Element_Grater $grater The element to convert.
149: *
150: * @return mixed The query element in query format.
151: *
152: * @throws Horde_Kolab_Server_Exception If the query is malformed.
153: */
154: public function convertGreater(Horde_Kolab_Server_Query_Element_Greater $greater)
155: {
156: return $this->_convertSingle($greater, 'greater');
157: }
158:
159: /**
160: * Convert the approx element to query format.
161: *
162: * @param Horde_Kolab_Server_Query_Element_Approx $approx The element to convert.
163: *
164: * @return mixed The query element in query format.
165: *
166: * @throws Horde_Kolab_Server_Exception If the query is malformed.
167: */
168: public function convertApprox(Horde_Kolab_Server_Query_Element_Approx $approx)
169: {
170: return $this->_convertSingle($approx, 'approx');
171: }
172:
173: /**
174: * Convert the single element to query format.
175: *
176: * @param Horde_Kolab_Server_Query_Element_Single $single The element to convert.
177: * @param string $operator The element operation.
178: *
179: * @return mixed The query element in query format.
180: *
181: * @throws Horde_Kolab_Server_Exception If the query is malformed.
182: */
183: private function _convertSingle(
184: Horde_Kolab_Server_Query_Element_Single $single,
185: $operator
186: ) {
187: try {
188: return Horde_Ldap_Filter::create(
189: $this->_structure->mapExternalToInternalAttribute(
190: $single->getName()
191: ),
192: $operator,
193: $single->getValue()
194: );
195: } catch (Horde_Ldap_Exception $e) {
196: throw new Horde_Kolab_Server_Exception(
197: $e->getMessage(),
198: Horde_Kolab_Server_Exception::INVALID_QUERY,
199: $e
200: );
201: }
202: }
203:
204: /**
205: * Convert the not element to query format.
206: *
207: * @param Horde_Kolab_Server_Query_Element_Group $group The element to convert.
208: *
209: * @return mixed The query element in query format.
210: *
211: * @throws Horde_Kolab_Server_Exception If the query is malformed.
212: */
213: public function convertNot(Horde_Kolab_Server_Query_Element_Not $not)
214: {
215: $elements = $not->getElements();
216: try {
217: return Horde_Ldap_Filter::combine('!', $elements[0]->convert($this));
218: } catch (Horde_Ldap_Exception $e) {
219: throw new Horde_Kolab_Server_Exception(
220: $e->getMessage(),
221: Horde_Kolab_Server_Exception::INVALID_QUERY,
222: $e
223: );
224: }
225: }
226:
227: /**
228: * Convert the and element to query format.
229: *
230: * @param Horde_Kolab_Server_Query_Element_Group $group The element to convert.
231: *
232: * @return mixed The query element in query format.
233: *
234: * @throws Horde_Kolab_Server_Exception If the query is malformed.
235: */
236: public function convertAnd(Horde_Kolab_Server_Query_Element_And $and)
237: {
238: return $this->_convertGroup($and, '&');
239: }
240:
241: /**
242: * Convert the or element to query format.
243: *
244: * @param Horde_Kolab_Server_Query_Element_Group $group The element to convert.
245: *
246: * @return mixed The query element in query format.
247: *
248: * @throws Horde_Kolab_Server_Exception If the query is malformed.
249: */
250: public function convertOr(Horde_Kolab_Server_Query_Element_Group $or)
251: {
252: return $this->_convertGroup($or, '|');
253: }
254:
255: /**
256: * Convert the group element to query format.
257: *
258: * @param Horde_Kolab_Server_Query_Element_Group $group The element to convert.
259: * @param string $operator The element operation.
260: *
261: * @return mixed The query element in query format.
262: *
263: * @throws Horde_Kolab_Server_Exception If the query is malformed.
264: */
265: public function _convertGroup(
266: Horde_Kolab_Server_Query_Element_Group $group,
267: $operator
268: ) {
269: $filters = array();
270: foreach ($group->getElements() as $element) {
271: $filters[] = $element->convert($this);
272: }
273: try {
274: return Horde_Ldap_Filter::combine($operator, $filters);
275: } catch (Horde_Ldap_Exception $e) {
276: throw new Horde_Kolab_Server_Exception(
277: $e->getMessage(),
278: Horde_Kolab_Server_Exception::INVALID_QUERY,
279: $e
280: );
281: }
282: }
283: }