~ubuntu-branches/debian/sid/php-horde-turba/sid

« back to all changes in this revision

Viewing changes to turba-4.1.1/lib/Driver/Share.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2013-08-11 13:16:25 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130811131625-z91stjvq51jr9onv
Tags: 4.1.1-1
New upstream version 4.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * The Turba_Driver:: class provides a common abstracted interface to the
 
4
 * various directory search drivers.  It includes functions for searching,
 
5
 * adding, removing, and modifying directory entries.
 
6
 *
 
7
 * Copyright 2009-2013 Horde LLC (http://www.horde.org/)
 
8
 *
 
9
 * See the enclosed file LICENSE for license information (ASL).  If you did
 
10
 * did not receive this file, see http://www.horde.org/licenses/apache.
 
11
 *
 
12
 * @author   Chuck Hagenbuch <chuck@horde.org>
 
13
 * @author   Jon Parise <jon@csh.rit.edu>
 
14
 * @author   Michael J. Rubinsky <mrubinsk@horde.org>
 
15
 * @category Horde
 
16
 * @license  http://www.horde.org/licenses/apache ASL
 
17
 * @package  Turba
 
18
 */
 
19
class Turba_Driver_Share extends Turba_Driver
 
20
{
 
21
    /**
 
22
     * Horde_Share object for this source.
 
23
     *
 
24
     * @var Horde_Share
 
25
     */
 
26
    protected $_share;
 
27
 
 
28
    /**
 
29
     * Underlying driver object for this source.
 
30
     *
 
31
     * @var Turba_Driver
 
32
     */
 
33
    protected $_driver;
 
34
 
 
35
    /**
 
36
     * Constructor
 
37
     *
 
38
     * @param string $name   The source name
 
39
     * @param array $params  The parameter array describing the source
 
40
     *
 
41
     * @return Turba_Driver
 
42
     */
 
43
    public function __construct($name = '', array $params = array())
 
44
    {
 
45
        parent::__construct($name, $params);
 
46
        $this->_share = $this->_params['config']['params']['share'];
 
47
        $this->_driver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver')->create($this->_params['config']);
 
48
        $this->_driver->setContactOwner($this->_getContactOwner());
 
49
        $this->_driver->setSourceName($name);
 
50
    }
 
51
 
 
52
    /**
 
53
     * Proxy to decorated base driver.
 
54
     *
 
55
     * @param string $method  Method name.
 
56
     * @param array $args     Method arguments.
 
57
     *
 
58
     * @return mixed  Method result.
 
59
     */
 
60
    public function __call($method, $args)
 
61
    {
 
62
        return call_user_func_array(array($this->_driver, $method), $args);
 
63
    }
 
64
 
 
65
    /**
 
66
     * Checks if this backend has a certain capability.
 
67
     *
 
68
     * @param string $capability  The capability to check for.
 
69
     *
 
70
     * @return boolean  Supported or not.
 
71
     */
 
72
    public function hasCapability($capability)
 
73
    {
 
74
        return $this->_driver->hasCapability($capability);
 
75
    }
 
76
 
 
77
    /**
 
78
     * Translates the keys of the first hash from the generalized Turba
 
79
     * attributes to the driver-specific fields. The translation is based on
 
80
     * the contents of $this->map.
 
81
     *
 
82
     * @param array $hash  Hash using Turba keys.
 
83
     *
 
84
     * @return array  Translated version of $hash.
 
85
     */
 
86
    public function toDriverKeys(array $hash)
 
87
    {
 
88
        return $this->_driver->toDriverKeys($hash);
 
89
    }
 
90
 
 
91
    /**
 
92
     * Searches the current address book for duplicate entries.
 
93
     *
 
94
     * Duplicates are determined by comparing email and name or last name and
 
95
     * first name values.
 
96
     *
 
97
     * @return array  A hash with the following format:
 
98
     * <code>
 
99
     * array('name' => array('John Doe' => Turba_List, ...), ...)
 
100
     * </code>
 
101
     * @throws Turba_Exception
 
102
     */
 
103
    public function searchDuplicates()
 
104
    {
 
105
        return $this->_driver->searchDuplicates();
 
106
    }
 
107
 
 
108
    /**
 
109
     * Checks if the current user has the requested permissions on this
 
110
     * address book.
 
111
     *
 
112
     * @param integer $perm  The permission to check for.
 
113
     *
 
114
     * @return boolean  True if the user has permission, otherwise false.
 
115
     */
 
116
    public function hasPermission($perm)
 
117
    {
 
118
        return $this->_share->hasPermission($GLOBALS['registry']->getAuth(), $perm);
 
119
    }
 
120
 
 
121
    /**
 
122
     * Return the name of this address book.
 
123
     *
 
124
     * @string Address book name
 
125
     */
 
126
    public function getName()
 
127
    {
 
128
        $share_parts = explode(':', $this->_share->getName());
 
129
        return array_pop($share_parts);
 
130
    }
 
131
 
 
132
    /**
 
133
     * Return the owner to use when searching or creating contacts in
 
134
     * this address book.
 
135
     *
 
136
     * @return string  TODO
 
137
     * @throws Turba_Exception
 
138
     */
 
139
    protected  function _getContactOwner()
 
140
    {
 
141
        $params = @unserialize($this->_share->get('params'));
 
142
        if (!empty($params['name'])) {
 
143
            return $params['name'];
 
144
        }
 
145
 
 
146
        throw new Turba_Exception(_("Unable to find contact owner."));
 
147
    }
 
148
 
 
149
    /**
 
150
     * Runs any actions after setting a new default tasklist.
 
151
     *
 
152
     * @param string $share  The default share ID.
 
153
     */
 
154
    public function setDefaultShare($share)
 
155
    {
 
156
        $this->_driver->setDefaultShare($share);
 
157
    }
 
158
 
 
159
    /**
 
160
     * Creates an object key for a new object.
 
161
     *
 
162
     * @param array $attributes  The attributes (in driver keys) of the
 
163
     *                           object being added.
 
164
     *
 
165
     * @return string  A unique ID for the new object.
 
166
     */
 
167
    protected function _makeKey(array $attributes)
 
168
    {
 
169
        return $this->_driver->_makeKey($attributes);
 
170
    }
 
171
 
 
172
    /**
 
173
     * Creates an object UID for a new object.
 
174
     *
 
175
     * @return string  A unique ID for the new object.
 
176
     */
 
177
    protected function _makeUid()
 
178
    {
 
179
        return $this->_driver->_makeUid();
 
180
    }
 
181
 
 
182
    /**
 
183
     * Searches the address book with the given criteria and returns a
 
184
     * filtered list of results. If the criteria parameter is an empty array,
 
185
     * all records will be returned.
 
186
     *
 
187
     * @param array $criteria       Array containing the search criteria.
 
188
     * @param array $fields         List of fields to return.
 
189
     * @param array $blobFields     Array of fields containing binary data.
 
190
     * @param boolean $count_only   Only return the count of matching entries,
 
191
     *                              not the entries themselves.
 
192
     *
 
193
     * @return array  Hash containing the search results.
 
194
     * @throws Turba_Exception
 
195
     */
 
196
    protected function _search(array $criteria, array $fields, array $blobFields = array(), $count_only = false)
 
197
    {
 
198
        return $this->_driver->_search($criteria, $fields, $blobFields, $count_only);
 
199
    }
 
200
 
 
201
    /**
 
202
     * Reads the given data from the address book and returns the results.
 
203
     *
 
204
     * @param string $key        The primary key field to use.
 
205
     * @param mixed $ids         The ids of the contacts to load.
 
206
     * @param string $owner      Only return contacts owned by this user.
 
207
     * @param array $fields      List of fields to return.
 
208
     * @param array $blobFields  Array of fields containing binary data.
 
209
     *
 
210
     * @return array  Hash containing the search results.
 
211
     * @throws Turba_Exception
 
212
     */
 
213
    protected function _read($key, $ids, $owner, array $fields,
 
214
                             array $blobFields = array())
 
215
    {
 
216
        return $this->_driver->_read($key, $ids, $owner, $fields, $blobFields);
 
217
    }
 
218
 
 
219
    /**
 
220
     * Adds the specified contact to the addressbook.
 
221
     *
 
222
     * @param array $attributes  The attribute values of the contact.
 
223
     * @param array $blob_fields TODO
 
224
     *
 
225
     * @throws Turba_Exception
 
226
     */
 
227
    protected function _add(array $attributes, array $blob_fields = array())
 
228
    {
 
229
        return $this->_driver->_add($attributes, $blob_fields);
 
230
    }
 
231
 
 
232
    /**
 
233
     * Returns ability of the backend to add new contacts.
 
234
     *
 
235
     * @return boolean  Can backend add?
 
236
     */
 
237
    protected function _canAdd()
 
238
    {
 
239
        return $this->_driver->_canAdd();
 
240
    }
 
241
 
 
242
    /**
 
243
     * Deletes the specified contact from the addressbook.
 
244
     *
 
245
     * @param string $object_key TODO
 
246
     * @param string $object_id  TODO
 
247
     *
 
248
     * @throws Turba_Exception
 
249
     */
 
250
    protected function _delete($object_key, $object_id)
 
251
    {
 
252
        return $this->_driver->_delete($object_key, $object_id);
 
253
    }
 
254
 
 
255
    /**
 
256
     * Deletes all contacts from a specific address book.
 
257
     *
 
258
     * @param string $sourceName  The source to remove all contacts from.
 
259
     *
 
260
     * @return array  An array of UIDs
 
261
     * @throws Turba_Exception
 
262
     */
 
263
    protected function _deleteAll($sourceName = null)
 
264
    {
 
265
        if (is_null($sourceName)) {
 
266
            $sourceName = $this->getContactOwner();
 
267
        }
 
268
        return $this->_driver->_deleteAll($sourceName);
 
269
    }
 
270
 
 
271
    /**
 
272
     * Saves the specified object in the SQL database.
 
273
     *
 
274
     * @param Turba_Object $object  The object to save
 
275
     *
 
276
     * @return string  The object id, possibly updated.
 
277
     * @throws Turba_Exception
 
278
     */
 
279
    protected function _save(Turba_Object $object)
 
280
    {
 
281
        return $this->_driver->_save($object);
 
282
    }
 
283
 
 
284
    /**
 
285
     * Remove all data for a specific user.
 
286
     *
 
287
     * @param string $user  The user to remove all data for.
 
288
     */
 
289
    public function removeUserData($user)
 
290
    {
 
291
        // Make sure we are being called by an admin.
 
292
        if (!$GLOBALS['registry']->isAdmin()) {
 
293
            throw new Horde_Exception_PermissionDenied(_("Permission denied"));
 
294
        }
 
295
        $this->_deleteAll();
 
296
        $GLOBALS['injector']
 
297
            ->getInstance('Turba_Shares')
 
298
            ->removeShare($this->_share);
 
299
        unset($this->_share);
 
300
    }
 
301
 
 
302
}