alecpl
2010-09-25 e019f2d0f2dc2fbfa345ab5d7ae85e67bfdd76b8
commit | author | age
f11541 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/addressbook/copy.inc                                    |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
A 8  | Copyright (C) 2007, Roundcube Dev. - Switzerland                      |
f11541 9  | Licensed under the GNU GPL                                            |
T 10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Copy a contact record from one direcotry to another                 |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: copy.inc 471 2007-02-09 21:25:50Z thomasb $
19
20 */
21
881217 22 // only process ajax requests
T 23 if (!$OUTPUT->ajax_call)
24   return;
25
f11541 26 $cid = get_input_value('_cid', RCUBE_INPUT_POST);
T 27 $target = get_input_value('_to', RCUBE_INPUT_POST);
ca38db 28 $target_group = get_input_value('_togid', RCUBE_INPUT_POST);
757d2b 29
306f15 30 if ($cid && preg_match('/^[a-zA-Z0-9\+\/=_-]+(,[a-zA-Z0-9\+\/=_-]+)*$/', $cid) && strlen($target) && $target !== $source)
f11541 31 {
757d2b 32   $success = 0;
ade8e1 33   $TARGET = $RCMAIL->get_address_book($target);
T 34
6fd71e 35   if ($TARGET && $TARGET->ready && !$TARGET->readonly) {
e09509 36     $arr_cids = explode(',', $cid);
7d43f8 37     $ids = array();
A 38
757d2b 39     foreach ($arr_cids as $cid) {
e6ce00 40       $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
ca38db 41         'record' => $CONTACTS->get_record($cid, true),
T 42         'source' => $target,
43         'group' => $target_group,
44       ));
45       $a_record = $plugin['record'];
6fd71e 46
7d43f8 47       if (!$plugin['abort']) {
A 48         // check if contact exists, if so, we'll need it's ID
49         $result = $TARGET->search('email', $a_record['email'], true, true);
50
51         // insert contact record
52         if (!$result->count) {
53           if ($insert_id = $TARGET->insert($a_record, false)) {
54             $ids[] = $insert_id;
55             $success++;
56           }
57         }
58         else {
59           $record = $result->first();
60           $ids[] = $record['ID'];
61         }
62       }
63     }
64
65     // assign to group
66     if ($target_group && $TARGET->groups && !empty($ids)) {
67       $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array(
68         'group_id' => $target_group, 'ids' => $ids, 'source' => $target));
69
70       if (!$plugin['abort']) {
71         $TARGET->reset();
72         $TARGET->set_group($target_group);
73
74         if (($maxnum = $RCMAIL->config->get('max_group_members', 0)) && ($TARGET->count()->count + count($plugin['ids']) > $maxnum)) {
75           $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum));
76           $OUTPUT->send();
77         }
78
79         if (($cnt = $TARGET->add_to_group($target_group, $plugin['ids'])) && $cnt > $success)
80           $success = $cnt;
81       }
757d2b 82     }
6fd71e 83   }
f11541 84
757d2b 85   if ($success == 0)
f11541 86     $OUTPUT->show_message('copyerror', 'error');
T 87   else
757d2b 88     $OUTPUT->show_message('copysuccess', 'notice', array('nr' => $success));
f11541 89 }
6b603d 90
f11541 91 // send response
T 92 $OUTPUT->send();
93