1<?php
2/**
3 * Copyright 2008-2016 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (LGPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
7 *
8 * @category Kolab
9 * @package  Kolab_Server
10 * @author   Gunnar Wrobel <wrobel@pardus.de>
11 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
12 * @link     http://pear.horde.org/index.php?package=Kolab_Server
13 */
14
15/**
16 * Return the mail addresses of the KolabInetOrgPersons with the given uid or
17 * mail address and include all alias and delegate addresses.
18 *
19 * @category Kolab
20 * @package  Kolab_Server
21 * @author   Gunnar Wrobel <wrobel@pardus.de>
22 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
23 * @link     http://pear.horde.org/index.php?package=Kolab_Server
24 */
25class Horde_Kolab_Server_Search_Operation_Addressesforuidormail
26extends Horde_Kolab_Server_Search_Operation_Base
27{
28    /**
29     * The basic attribute search.
30     *
31     * @var Horde_Kolab_Server_Search_Operation
32     */
33    private $_search;
34
35    /**
36     * Constructor
37     *
38     * @param Horde_Kolab_Server_Composite $composite A link to the composite
39     *                                                server handler.
40     */
41    public function __construct(Horde_Kolab_Server_Composite $composite)
42    {
43        $this->_composite = $composite;
44        $this->_search = new Horde_Kolab_Server_Search_Operation_Attributes(
45            $this->getComposite()
46        );
47    }
48
49    /**
50     * Return the mail addresses of the KolabInetOrgPersons with the given uid
51     * or mail address and include all alias and delegate addresses.
52     *
53     * @param string $uid  The uid to search for.
54     * @param string $mail The mail address to search for.
55     *
56     * @return array The GUID(s).
57     *
58     * @throws Horde_Kolab_Server_Exception
59     */
60    public function searchAddressesForUidOrMail($uid, $mail)
61    {
62        $criteria = new Horde_Kolab_Server_Query_Element_And(
63            new Horde_Kolab_Server_Query_Element_Equals(
64                'Objectclass',
65                Horde_Kolab_Server_Object_Kolabinetorgperson::OBJECTCLASS_KOLABINETORGPERSON
66            ),
67            new Horde_Kolab_Server_Query_Element_Or(
68                new Horde_Kolab_Server_Query_Element_Equals(
69                    'Uid', $uid
70                ),
71                new Horde_Kolab_Server_Query_Element_Equals(
72                    'Mail', $mail
73                )
74            )
75        );
76        $search = new Horde_Kolab_Server_Search_Operation_Constraint_Strict(
77            $this->_search
78        );
79
80        $data = $search->searchAttributes(
81            $criteria, array('Mail', 'Alias')
82        );
83
84        if (empty($data)) {
85            return array();
86        }
87
88        $mail = $this->getComposite()->structure->getAttributeInternal(
89            'Mail'
90        );
91        $alias = $this->getComposite()->structure->getAttributeInternal(
92            'Alias'
93        );
94
95        if (isset($result[$alias])) {
96            $addrs = array_merge($data[$mail], $data[$alias]);
97        } else {
98            $addrs = $data[$mail];
99        }
100
101        $criteria = new Horde_Kolab_Server_Query_Element_And(
102            new Horde_Kolab_Server_Query_Element_Equals(
103                'Objectclass',
104                Horde_Kolab_Server_Object_Kolabinetorgperson::OBJECTCLASS_KOLABINETORGPERSON
105            ),
106            new Horde_Kolab_Server_Query_Element_Equals(
107                'Delegate',  $data[$mail][0]
108            )
109        );
110
111        $data = $this->_search->searchAttributes(
112            $criteria, array('Mail', 'Alias')
113        );
114
115        if (!empty($data)) {
116            foreach ($data as $adr) {
117                if (isset($adr[$mail])) {
118                    $addrs = array_merge($addrs, $adr[$mail]);
119                }
120                if (isset($adr[$alias])) {
121                    $addrs = array_merge($addrs, $adr[$alias]);
122                }
123            }
124        }
125
126        $addrs = array_map('Horde_String::lower', $addrs);
127
128        return $addrs;
129    }
130}