]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/copy.inc
8a8379078a761346d26dfe1c8e023f2faf39318c
[roundcube.git] / program / steps / addressbook / copy.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/addressbook/copy.inc                                    |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2007, Roundcube Dev. - Switzerland                      |
9  | Licensed under the GNU GPL                                            |
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
22 // only process ajax requests
23 if (!$OUTPUT->ajax_call)
24   return;
25
26 $cid = get_input_value('_cid', RCUBE_INPUT_POST);
27 $target = get_input_value('_to', RCUBE_INPUT_POST);
28 $target_group = get_input_value('_togid', RCUBE_INPUT_POST);
29
30 if ($cid && preg_match('/^[a-zA-Z0-9\+\/=_-]+(,[a-zA-Z0-9\+\/=_-]+)*$/', $cid) && strlen($target) && $target !== $source)
31 {
32   $success = 0;
33   $TARGET = $RCMAIL->get_address_book($target);
34
35   if ($TARGET && $TARGET->ready && !$TARGET->readonly) {
36     $arr_cids = explode(',', $cid);
37     $ids = array();
38
39     foreach ($arr_cids as $cid) {
40       $a_record = $CONTACTS->get_record($cid, true);
41
42       // check if contact exists, if so, we'll need it's ID
43       $result = $TARGET->search('email', $a_record['email'], true, true);
44
45       // insert contact record
46       if (!$result->count) {
47         $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
48           'record' => $a_record, 'source' => $target, 'group' => $target_group));
49
50         if (!$plugin['abort']) {
51           if ($insert_id = $TARGET->insert($a_record, false)) {
52             $ids[] = $insert_id;
53             $success++;
54           }
55         }
56         else if ($plugin['result']) {
57           $ids = array_merge($ids, $plugin['result']);
58           $success++;
59         }
60       }
61       else {
62         $record = $result->first();
63         $ids[] = $record['ID'];
64       }
65     }
66
67     // assign to group
68     if ($target_group && $TARGET->groups && !empty($ids)) {
69       $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array(
70         'group_id' => $target_group, 'ids' => $ids, 'source' => $target));
71
72       if (!$plugin['abort']) {
73         $TARGET->reset();
74         $TARGET->set_group($target_group);
75
76         if (($maxnum = $RCMAIL->config->get('max_group_members', 0)) && ($TARGET->count()->count + count($plugin['ids']) > $maxnum)) {
77           $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum));
78           $OUTPUT->send();
79         }
80
81         if (($cnt = $TARGET->add_to_group($target_group, $plugin['ids'])) && $cnt > $success)
82           $success = $cnt;
83       }
84       else if ($plugin['result'])
85         $success = $plugin['result'];
86     }
87   }
88
89   if ($success == 0)
90     $OUTPUT->show_message('copyerror', 'error');
91   else
92     $OUTPUT->show_message('copysuccess', 'notice', array('nr' => $success));
93 }
94
95 // send response
96 $OUTPUT->send();
97