-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquirrelmail-to-roundcube.php
156 lines (139 loc) · 4.99 KB
/
squirrelmail-to-roundcube.php
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
<?php
/*
* This Script copy Address Book from Squirrelmail Database Table address to Roundcube contacts table
* Include create user when not exist
*/
/*
* Configuration begin
*/
/*
* Squirrelmail settings
*/
$SQ_DB_HOST = '127.0.0.1';
$SQ_DB_PORT = '3306';
$SQ_DB_USER = 'USER';
$SQ_DB_PASS = 'PASSWD';
$SQ_DB_DB = 'squirrelmail';
$SQ_DB_AD_TABLE = 'address';
/*
* Roundcube settings
*/
$RQ_DB_HOST = '127.0.0.2';
$RQ_DB_PORT = '3306';
$RQ_DB_USER = 'USER';
$RQ_DB_PASS = 'PASSWD';
$RQ_DB_DB = 'roundcubemail';
$RQ_DB_AD_TABLE = 'contacts';
$RQ_DB_USER_TABLE = 'contacts';
/*
* Configuration end
*/
/*
* Connect to Squirrelmail DB
*/
$mysqli_SQ = new mysqli($SQ_DB_HOST, $SQ_DB_USER, $SQ_DB_PASS, $SQ_DB_DB, $SQ_DB_PORT);
if ($mysqli_SQ->connect_error) {
die('Connect Error (' . $mysqli_SQ->connect_errno . ') ' . $mysqli_SQ->connect_error);
}
echo 'Success... to Squirrelmail DB ' . $mysqli_SQ->host_info . "\n";
/*
* Connect to Roundcube DB
*/
$mysqli_RQ = new mysqli($RQ_DB_HOST, $RQ_DB_USER, $RQ_DB_PASS, $RQ_DB_DB, $RQ_DB_PORT);
if ($mysqli_RQ->connect_error) {
die('Connect Error (' . $mysqli_RQ->connect_errno . ') ' . $mysqli_RQ->connect_error);
}
echo 'Success... to Roundcube DB ' . $mysqli_RQ->host_info . "\n";
/*
* get all Address Book entry's from Squirrelmail
*/
if ($SQ_result = $mysqli_SQ->query("SELECT * FROM address ORDER BY owner")) {
while($SQ_obj = $SQ_result->fetch_object()){
$owner=$SQ_obj->owner;
$nickname=utf8_encode($SQ_obj->nickname);
$firstname=utf8_encode($SQ_obj->firstname);
$lastname=utf8_encode($SQ_obj->lastname);
$email=$SQ_obj->email;
$label=utf8_encode($SQ_obj->label);
/*
* Check if Squirrelmail Addrees Book Owner exist in Roundcube users
*/
if ($RQ_result = $mysqli_RQ->query("SELECT username FROM users WHERE username='".$mysqli_RQ->real_escape_string($owner)."'")) {
/*
* Create user in Roundcube users if not exist
*/
if($RQ_result->num_rows == 0 ) {
echo "Add $owner to Roundcube \n";
$result=$mysqli_RQ->query("
INSERT INTO users (
`username`,
`mail_host`,
`created`,
`language`
) VALUES (
'". $mysqli_RQ->real_escape_string($owner) ."',
'". $mysqli_RQ->real_escape_string("localhost") ."',
'". date('Y-m-d H:i:s') ."',
'". $mysqli_RQ->real_escape_string("DE_de") ."'
)
");
if (!$result) {
echo "Error: " . $mysqli_RQ->error;
}
} else {
/*
* Get userID from Roundcube users
*/
if ($result = $mysqli_RQ->query("SELECT user_id FROM users WHERE username='". $mysqli_RQ->real_escape_string($owner) ."'")) {
$obj = $result->fetch_object();
$user_id = $obj->user_id;
}
/*
* Check if Contact exist
*/
if ($result = $mysqli_RQ->query("SELECT email FROM contacts WHERE user_id='$user_id' AND email='". $mysqli_RQ->real_escape_string($email) ."'")) {
if($result->num_rows == 0 ) {
/*
* Insert data to Roundcube contacs
*/
echo "Add Contact $email from $owner to Roundcube Contacts\n";
$result=$mysqli_RQ->query("
INSERT INTO contacts (
`contact_id`,
`changed`,
`del`,
`name`,
`email`,
`firstname`,
`surname`,
`vcard`,
`user_id`
) VALUES (
NULL,
'". date('Y-m-d H:i:s') ."',
0,
'". $mysqli_RQ->real_escape_string($label) ."',
'". $mysqli_RQ->real_escape_string($email) ."',
'". $mysqli_RQ->real_escape_string($firstname) ."',
'". $mysqli_RQ->real_escape_string($lastname) ."',
NULL,
'". (int)$user_id ."'
)
");
if (!$result) {
echo "Error: " . $mysqli_RQ->error;
}
}
}
}
}
}
}
/*
* Close Connect to Squirrelmail DB
*/
$mysqli_SQ->close();
/*
* Close Connect to Roundcube DB
*/
$mysqli_RQ->close();