File: browse.php

package info (click to toggle)
turba2 2.0.2-1sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,996 kB
  • ctags: 1,071
  • sloc: php: 4,725; xml: 617; sql: 136; makefile: 62; sh: 46; perl: 17
file content (410 lines) | stat: -rw-r--r-- 17,127 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
/**
 * $Horde: turba/browse.php,v 1.76.2.4 2005/02/13 12:58:26 jan Exp $
 *
 * Turba: Copyright 2000-2005 Charles J. Hagenbuch <chuck@horde.org>
 *
 * See the enclosed file COPYING for license information (ASL).  If you
 * did not receive this file, see http://www.horde.org/licenses/asl.php.
 */

@define('TURBA_BASE', dirname(__FILE__));
require_once TURBA_BASE . '/lib/base.php';
require_once TURBA_BASE . '/lib/List.php';

/* Sort out the sorting values. */
if (($sortby = Util::getFormData('sortby')) !== null) {
    if ($sortby == 'name') {
        if ($prefs->getValue('name_format') == 'first_last') {
            $sortby = 'name';
        } else {
            $sortby = 'lastname';
        }
    }
    $prefs->setValue('sortby', $sortby);
}
if (($sortdir = Util::getFormData('sortdir')) !== null) {
    $prefs->setValue('sortdir', $sortdir);
}

$title = _("Address Book Listing");

/* Build the directory sources select widget. */
$source = Util::getFormData('source', $prefs->getValue('default_dir'));
$source_options = '';
$add_source_options = '';
$source_count = 0;
foreach ($cfgSources as $key => $curSource) {
    if (!empty($curSource['export'])) {
        $selected = ($key == $source) ? ' selected="selected"' : '';
        $source_options .= '<option value="' . $key . '" ' . $selected;
        $source_options .= '>' . htmlspecialchars($curSource['title']) . '</option>';
        if ($key != $source && empty($curSource['readonly']) || (isset($curSource['admin']) && in_array(Auth::getAuth(), $curSource['admin']))) {
            $add_source_options .= '<option value="' . $key . '">' . htmlspecialchars($curSource['title']) . '</option>';
        }
        $source_count++;
        if (empty($source)) {
            $source = $key;
        }
    }
}

// Hide the list of addressbooks if there is only one available.
if ($source_count == 1) {
    $add_source_options = '';
}

if ($source_count == 0) {
    $notification->push(_("There are no browseable address books."), 'horde.warning');
} else {
    $driver = &Turba_Driver::singleton($source, $cfgSources[$source]);
    if (is_a($driver, 'PEAR_Error')) {
        $notification->push(sprintf(_("Failed to access the address book: %s"), $driver->getMessage()), 'horde.error');
        unset($driver);
    }
}

if (isset($driver)) {
    $actionID = Util::getFormData('actionID');

    /* Run through the action handlers. */
    switch ($actionID) {
    case 'delete':
        /* Remove a contact from a list. */
        $keys = Util::getFormData('objectkeys');
        if (is_array($keys)) {
            $key = Util::getFormData('key', false);
            if ($key && $key != '**search') {
                /* We are removing a contact from a list. */
                $list = $driver->getObject($key);
                foreach ($keys as $sourceKey) {
                    list($source, $objectKey) = explode(':', $sourceKey, 2);
                    if (!$list->removeMember($objectKey)) {
                        $notification->push(_("There was an error removing this object."), 'horde.error');
                    } else {
                        $notification->push(_("Contact removed from list."), 'horde.success');
                    }
                }
            } else {
                /* We are deleting an object. */
                foreach ($keys as $sourceKey) {
                    list($source, $objectKey) = explode(':', $sourceKey, 2);
                    if (!$driver->delete($objectKey)) {
                        $notification->push(_("There was an error deleting this object."), 'horde.error');
                    }
                }
            }

            /* Remove the objects from search results too. */
            if (!empty($_SESSION['turba_search_results'])) {
                require_once TURBA_BASE . '/lib/Object.php';
                $list = Turba_List::unserialize($_SESSION['turba_search_results']);
                foreach ($keys as $sourceKey) {
                    list($source, $objectKey) = explode(':', $sourceKey, 2);
                    $list->remove($objectKey);
                }
                $_SESSION['turba_search_results'] = $list->serialize();
            }
        }
        break;

    case 'move':
    case 'copy':
        $keys = Util::getFormData('objectkeys');
        if (is_array($keys) && count($keys)) {
            // If we have data, try loading the target address book
            // driver.
            $targetSource = Util::getFormData('targetAddressbook');
            $targetDriver = &Turba_Driver::singleton($targetSource, $cfgSources[$targetSource]);
            if (is_a($targetDriver, 'PEAR_Error')) {
                $notification->push(sprintf(_("Failed to access the address book: %s"), $targetDriver->getMessage()), 'horde.error');
            } else {
                foreach ($keys as $sourceKey) {
                    // Split up the key into source and object ids.
                    list($source, $objectKey) = explode(':', $sourceKey, 2);

                    // Ignore this entry if the target is the same as
                    // the source.
                    if ($source == $targetDriver->name) {
                        continue;
                    }

                    // Try and load the driver for the source.
                    $sourceDriver = &Turba_Driver::singleton($source, $cfgSources[$source]);
                    if (is_a($sourceDriver, 'PEAR_Error')) {
                        $notification->push(sprintf(_("Failed to access the address book: %s"), $sourceDriver->getMessage()), 'horde.error');
                    } else {
                        // Get the object.
                        $object = $sourceDriver->getObject($objectKey);
                        if (is_a($object, 'PEAR_Error')) {
                            $notification->push(sprintf(_("Failed to find object to be added: %s"), $object->getMessage()), 'horde.error');
                            continue;
                        } elseif ($object->isGroup()) {
                            if ($actionID == 'move') {
                                $notification->push(sprintf(_("'%s' was not moved because it is a list."), $object->getValue('name')), 'horde.warning');
                            } else {
                                $notification->push(sprintf(_("'%s' was not copied because it is a list."), $object->getValue('name')), 'horde.warning');
                            }
                            continue;
                        }

                        // Try adding to the target.

                        // backport security patch from Turba 2.0.5 for sarge
                        //$result = $targetDriver->add($object->getAttributes());
                        $objAttributes = array();
                        // Get the values through the Turba_Object class.
                        foreach ($targetDriver->getCriteria() as $info_key => $info_val) {
                            $objAttributes[$info_key] = $object->getValue($info_key);
                        }
                        $result = $targetDriver->add($objAttributes);


                        if (is_a($result, 'PEAR_Error')) {
                            $notification->push(sprintf(_("Failed to add %s to %s: %s"), $object->getValue('name'), $targetDriver->title, $result->getMessage()), 'horde.error');
                        } else {
                            $notification->push(sprintf(_("Successfully added %s to %s"), $object->getValue('name'), $targetDriver->title), 'horde.success');

                            // If we're moving objects, and we
                            // succeeded, delete them from the
                            // original source now.
                            if ($actionID == 'move') {
                                if (!$sourceDriver->delete($objectKey)) {
                                    $notification->push(sprintf(_("There was an error deleting %s from the source address book."), $object->getValue('name')), 'horde.error');
                                }
                            }
                        }
                    }
                }
            }
        }
        break;

    case 'add':
        /* Add a contact to a list. */
        $keys = Util::getFormData('objectkeys');
        $targetKey = Util::getFormData('targetList');
        if (empty($targetKey)) {
            break;
        }

        if (!Util::getFormData('targetNew')) {
            $target = $driver->getObject($targetKey);
        }

        if (!empty($target) && is_object($target) && $target->isGroup()) {
            /* Adding contact to an existing list */
            if (is_array($keys)) {
                foreach ($keys as $sourceKey) {
                    list($source, $objectKey) = explode(':', $sourceKey, 2);
                    $target->addMember($objectKey);
                }
                $target->store();
            }
        } else {
            /* Adding contact to a new list. */
            $newList = array();
            $newList['__owner'] = Auth::getAuth();
            $newList['__type'] = 'Group';
            $newList['name'] = $targetKey;

            $targetKey = $driver->add($newList);
            $target = $driver->getObject($targetKey);
            if (!empty($target) && is_object($target) && $target->isGroup()) {
                if (is_array($keys)) {
                    foreach ($keys as $sourceKey) {
                        list($source, $objectKey) = explode(':', $sourceKey, 2);
                        $target->addMember($objectKey);
                    }
                    $target->store();
                }
            } else {
                $notification->push(_("There was an error creating a new list."), 'horde.error');
            }
        }
        break;
    }
}

$templates = array();
if (isset($driver)) {
    $templates[] = '/browse/javascript.inc';

    /* Read the columns to display from the preferences. */
    $sources = Turba::string2Columns($prefs->getValue('columns'));
    $columns = isset($sources[$source]) ? $sources[$source] : array();
    $width = floor(90 / (count($columns) + 1));

    /* Determine the name of the column to sort by. */
    $sortcolumn = ($prefs->getValue('sortby') == 0 ||
                   !isset($columns[$prefs->getValue('sortby') - 1])) ?
        (($prefs->getValue('name_format') == 'first_last') ? 'name' : 'lastname') : $columns[$prefs->getValue('sortby') - 1];

    /* Create list of lists for Add to. */
    $addToList = array();
    if (!empty($cfgSources[$source]['map']['__type'])) {
        $listList = $driver->search(array('__type' => 'Group'));
        if (is_a($listList, 'PEAR_Error')) {
            $notification->push($listList, 'horde.error');
        } else {
            $listList->reset();
            while ($listObject = $listList->next()) {
    
                // backport security patch from Turba 2.0.5 for sarge
                //$addToList[] = array('name' => $listObject->getValue('name'), 'key' => $listObject->getValue('__key'));
                $addToList[] = array('name' => htmlspecialchars($listObject->getValue('name')),
                                     'key' => htmlspecialchars($listObject->getValue('__key')));

            }
        }
    }

    if (isset($_SESSION['turba_search_results']) &&
        (Util::getFormData('key') == '**search')) {
        /* We are displaying some search results. */
        $results = Turba_List::unserialize($_SESSION['turba_search_results']);
        $results->sort($sortcolumn, $prefs->getValue('sortdir'));

        $templates[] = '/browse/search.inc';

        if ($_SESSION['turba_search_mode'] == 'advanced') {
            $map = $driver->getCriteria();
            $templates[] = '/browse/search_criteria.inc';
        }

        $templates[] = '/browse/header.inc';
        $templates[] = '/browse/actions.inc';
        $templates[] = '/browse/column_headers.inc';

        $title = _("Search Results");
        $listType = 'search';

        if ($_SESSION['turba_search_mode'] == 'basic') {
            $notification->push('document.directory_search.val.focus();', 'javascript');
        } else {
            $notification->push('document.directory_search.name.focus();', 'javascript');
        }

        require_once TURBA_BASE . '/lib/ListView.php';
        $display = &new Turba_ListView($results, TURBA_TEMPLATES . '/browse/contactrow.inc');
    } elseif (Util::getFormData('key')) {
        /* We are displaying the contents of a list. */
        $list = $driver->getObject(Util::getFormData('key'));
        if (isset($list) && is_object($list) && !is_a($list, 'PEAR_Error') && $list->isGroup()) {
            $title = sprintf(_("Contacts in list: %s"), $list->getValue('name'));
            $templates[] = '/browse/header.inc';

            /* Show List Members. */
            if (!is_object($results = $list->listMembers($sortcolumn, $prefs->getValue('sortdir')))) {
                $notification->push(_("Failed to browse list"), 'horde.error');
            } else {
                $listType = 'list';
                $templates[] = '/browse/actions.inc';
                $templates[] = '/browse/column_headers.inc';

                require_once TURBA_BASE . '/lib/ListView.php';
                $display = &new Turba_ListView($results, TURBA_TEMPLATES . '/browse/contactrow.inc');
            }
        } else {
            $notification->push(_("There was an error displaying the select List"), 'horde.error');
        }
    } else {
        /* We are displaying the contents of the address book. */
        if ($source_count > 1) {
            $templates[] = '/browse/select.inc';
        }
        $title = sprintf(_("Contents of %s"), $cfgSources[$source]['title']);
        $templates[] = '/browse/header.inc';
        if (Util::getFormData('show', 'all') == 'contacts') {
            /* Show Contacts. */
            $results = $driver->search(array('__type' => 'Object'), $sortcolumn, 'AND', $prefs->getValue('sortdir'));
        } elseif (Util::getFormData('show', 'all') == 'lists') {
            /* Show Lists. */
            $results = $driver->search(array('__type' => 'Group'), $sortcolumn, 'AND', $prefs->getValue('sortdir'));
        } else {
            /* Show All. */
            $results = $driver->search(array(), $sortcolumn, 'AND', $prefs->getValue('sortdir'));
        }

        if (!is_object($results)) {
            $notification->push(_("Failed to browse the directory"), 'horde.error');
        } elseif (is_a($results, 'PEAR_Error')) {
            $notification->push($results, 'horde.error');
        } else {
            $listType = 'directory';
            $templates[] = '/browse/actions.inc';
            $templates[] = '/browse/column_headers.inc';

            require_once TURBA_BASE . '/lib/ListView.php';
            $display = &new Turba_ListView($results, TURBA_TEMPLATES . '/browse/contactrow.inc');
        }
    }
} else {
    $templates[] = '/browse/select.inc';
    $templates[] = '/browse/header.inc';
}

require TURBA_TEMPLATES . '/common-header.inc';
require TURBA_TEMPLATES . '/menu.inc';

foreach ($templates as $template) {
    require TURBA_TEMPLATES . $template;
}

$footer = 'footer.inc';

if (isset($display) && is_object($display)) {
    require_once 'Horde/UI/Pager.php';
    require_once 'Horde/Variables.php';

    if (!Util::getFormData('source') == '') {
        $urlSource = Util::getFormData('source');
    } else {
        $urlSource = $source;
    }
    $viewurl = Util::addParameter('browse.php', array(
        'sortby' => $sortby,
        'sortdir' => $sortdir,
        'key' => Util::getFormData('key'),
        'source' => $urlSource
    ));

    if (Util::getFormData('key') == '**search') {
        $page = Util::getFormData('page', 0);
        $numitem = $results->count();
        $maxpage = $prefs->getValue('maxpage');
        $perpage = $prefs->getValue('perpage');

        $min = $page * $perpage;
        while ($min > $numitem) {
            $page--;
            $min = $page * $perpage;
        }
        $max = $min + $perpage;

        $start = ($page * $perpage) + 1;
        $end = min($numitem, $start + $perpage - 1);

        $numDisplayed = $display->display($min, $max);

        $vars = &Variables::getDefaultVariables();
        $pager = &new Horde_UI_Pager('page', $vars, array('num' => $numDisplayed,
                                                          'url' => $viewurl,
                                                          'page_count' => $maxpage,
                                                          'perpage' => $perpage));
    } else {
        $page = Util::getFormData('page', '*');
        if (empty($page) || !preg_match('/^[A-Za-z*]$/', $page)) {
            $page = '*';
        }

        $display->displayAlpha($page);
        $numDisplayed = $results->count();
        $footer = 'footerAlpha.inc';
    }

    require TURBA_TEMPLATES . '/browse/column_footers.inc';
}

require TURBA_TEMPLATES . '/browse/' . $footer;
require $registry->get('templates', 'horde') . '/common-footer.inc';