-
Notifications
You must be signed in to change notification settings - Fork 1
/
vtiger2ldap.php
317 lines (258 loc) · 10.3 KB
/
vtiger2ldap.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
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
<?php
/*
* Open-Future vtiger2ldap
* PHP Code that gets vtiger contacts using vtiger webservices API,
* and imports them into an LDAP tree.
*
* For use with Zarafa, or other software that can read out LDAP data.
*
* For further information, and how to integrate with Zarafa, please see
* the included README file.
*
* @AUTHOR Bert Deferme <[email protected]> www.open-future.be
* @LICENSE GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
* @VERSION 1.0RC1
*
*/
/*
* Do not edit anything in this file unless you know what you are doing.
* Configuration is done in the 'include/config.php' file.
*
*/
// Require the config.php
// Configured in this file: Vtiger Webservices Authentication information, LDAP information, E-mail information.
require_once("include/config.php");
// Requirements (HTTP_CLIENT, Zend framework(included))
require_once("include/requirements.php");
// Require Vtiger Webservices Auth Code
require_once("include/VtigerWebAuth.php");
// Authenticate to vtiger, and get the sessionid for usage
$vauth = new VtigerWebAuth();
if ($vauth->authed()) {
$sessionid = $vauth->getSessionId();
} else {
print $vauth->getErr();
}
// Initialise empty string to store contacts without accounts.
// Searching on account information (Company Name) when the Vtiger contact
// does not have an account makes this script crash. We now catch this,
// store contacts without accounts, and send an e-mail about these contacts.
$noAcc = "";
// Get the vtiger webservices API url from the constant (config.php)
$endpointUrl = VTIGER_APIURL;
// First: get total number of vtiger contacts
$query = "select count(*) from Contacts;";
$queryParam = urlencode($query);
$params = "sessionName=$sessionid&operation=query&query=$queryParam";
// Perform the query
print "[vtiger2ldap] Searching for contacts in Vtiger...\n\n";
$httpc = new HTTP_CLIENT();
$httpc->get("$endpointUrl?$params");
$response = $httpc->currentResponse();
$jsonResponse = Zend_JSON::decode($response['body']);
// Error handling
if($jsonResponse['success']==false)
//handle the failure case.
die('query failed:'.$jsonResponse['errorMsg']);
// Variables used to limit searches to 30 results, this results in
// faster searches and less load on the Vtiger server.
$numContacts = $jsonResponse['result'][0]["count"];
$start = 0;
$limit = 30;
$end = $start + $limit;
// Get the vtiger webservices API url from the constant (config.php)
$endpointUrl = VTIGER_APIURL;
while ($end <= $numContacts) { // while we still need to process contacts
$end = $start + $limit;
// First: get all vtiger contacts with webservices API, limited by $start and $end
$query = "select * from Contacts limit $start, $limit;";
$queryParam = urlencode($query);
$params = "sessionName=$sessionid&operation=query&query=$queryParam";
// Perform the query
print "[vtiger2ldap] Searching for contacts in Vtiger...\n\n";
$httpc = new HTTP_CLIENT();
$httpc->get("$endpointUrl?$params");
$response = $httpc->currentResponse();
$jsonResponse = Zend_JSON::decode($response['body']);
// Error handling
if($jsonResponse['success']==false)
//handle the failure case.
die('query failed:'.$jsonResponse['errorMsg']);
// Array with results
$retrievedObjects = $jsonResponse['result'];
// Loop trough all vtiger contacts, check if they exist in LDAP
// If they exist: modify
// If not: add
foreach ($retrievedObjects as $obj) {
// Get the account ID for this contact, used for getting the Company info
$accountId = $obj["account_id"];
// If no account ID is set, this contact has no account, store it for e-mail and
// skip this account!
if ($accountId == "") {
// Log details for sending an email
$name=trim($obj["firstname"])." ".trim($obj["lastname"]);
$desc="(".$obj["email"]." / ".$obj["contact_no"].")";
$noAcc .= $name ." ". $desc . "\n";
print "[vtiger2ldap] Got contact ".$newContact["cn"].", without account, logging for email...\n";
continue;
}
// Get company info from vtiger webservices API
$query = "select accountname from Accounts where id='".$accountId."';";
$queryParam = urlencode($query);
$params = "sessionName=$sessionid&operation=query&query=$queryParam";
$httpc = new HTTP_CLIENT();
$httpc->get("$endpointUrl?$params");
$response = $httpc->currentResponse();
$jsonResponse = Zend_JSON::decode($response['body']);
// Error handling
if($jsonResponse['success']==false)
//handle the failure case.
die('query failed:'.$jsonResponse['errorMsg']);
// Array with results
$retrievedAccounts = $jsonResponse['result'];
// Setup connection parameters to LDAP, from constants in 'include/config.php'
$ldap_url= LDAP_URI;
$username= LDAP_BIND_DN;
$password= LDAP_BIND_PW;
$ds = ldap_connect($ldap_url);
// Setup LDAP options
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
// Do a bind login to LDAP
$login = ldap_bind($ds, $username, $password);
// Setup search Parameters for LDAP (searching for uidNumbers)
// Needed to make sure we don't create contacts with uidNumbers that already exist
$dn= LDAP_SEARCH_DN;
$filter="(uidNumber=*)";
$getthese=array("uidNumber");
// Perform the search
$sr=ldap_search($ds, $dn, $filter, $getthese);
$ldapResults = ldap_get_entries($ds,$sr);
// Generate an array with already used uidNumbers
// within our contact uidNumbers range (10000-20000)
$uidnumbers = array();
foreach ($ldapResults as $entry) {
if ($entry['uidnumber'][0] >= LDAP_UID_MIN && $entry['uidnumber'][0] <= LDAP_UID_MAX ) {
array_push($uidnumbers,$entry['uidnumber'][0]);
}
}
// Sort the uidNumbers, and get the latest one used
sort($uidnumbers);
$last=end($uidnumbers);
// If there are no uidNumbers within this range yet, set the last uidNumber to be LDAP_UID_MIN - 1
// Else, set the last uidNumber from the array
if($last == "") {
$lastuidNumber= LDAP_UID_MIN - 1;
} else {
$lastuidNumber=end($uidnumbers);
}
// Set the uidNumber to be used for this contact
if (!isset($uidNumber)) {
$uidNumber = $lastuidNumber+1;
}
// Create an array to hold the new contact's data,
// and populate it with the values that exist
$newContact = array();
// The cn is firstname + lastname
$newContact["cn"] = trim($obj["firstname"])." ".trim($obj["lastname"]);
if (!empty($obj["email"])) {
$newContact["mail"] = $obj["email"];
}
$newContact["sn"] = $obj["lastname"];
$newContact["givenName"] = $obj["firstname"];
if (!empty($obj["mailingcity"])) {
$newContact["l"] = $obj["mailingcity"];
$newContact["physicalDeliveryOfficeName"] = $obj["mailingcity"];
}
if (!empty($obj["title"])) {
$newContact["title"] = $obj["title"];
}
if (!empty($obj["mailingstreet"])) {
$newContact["street"] = $obj["mailingstreet"];
}
if (!empty($obj["mailingzip"])) {
$newContact["postalCode"] = $obj["mailingzip"];
}
if (!empty($obj["department"])) {
$newContact["departmentNumber"] = $obj["department"];
} else {
$newContact["departmentNumber"] = $retrievedAccounts[0]["accountname"];
}
if (!empty($obj["mobile"])) {
$newContact["mobile"] = $obj["mobile"];
}
if (!empty($obj["phone"])) {
$newContact["telephoneNumber"] = $obj["phone"];
}
if (!empty($obj["otherphone"])) {
$newContact["pager"] = $obj["otherphone"];
}
if (!empty($obj["fax"])) {
$newContact["facsimileTelephoneNumber"] = $obj["fax"];
}
if (!empty($retrievedAccounts[0]["accountname"])) {
$newContact["o"] = $retrievedAccounts[0]["accountname"];
}
// Set the objectClasses for this contact needed for Zarafa
$newContact["objectClass"][0] = "zarafa-contact";
$newContact["objectClass"][1] = "inetorgperson";
$newContact["objectClass"][2] = "organizationalPerson";
// Set uidNumber, uid and DN
$newContact["uidNumber"] = $uidNumber;
$newContact["uid"] = $obj["firstname"]." ".$obj["lastname"];
$myDn = 'cn='.$newContact["cn"].','.LDAP_CONTACTS_OU;
print "[vtiger2ldap] Got contact ".$newContact["cn"].", proceeding with add/modify...\n";
// Check if contact already exists (search for the CN, and get the uidNumber)
$dn= LDAP_CONTACTS_OU;
$filter='(cn='.$newContact["cn"].')';
$getthese=array("uidNumber");
// Perform the search
$sr=ldap_search($ds, $dn, $filter, $getthese);
$contactArray = ldap_get_entries($ds,$sr);
// If there are no results, the contact does not exist yet -> we can add it...
if ($contactArray["count"] == "0") {
// ! IMPORTANT !
// Check if there are no users in the LDAP tree under ou=users
// Why? -> If BOTH a zarafa-user and zarafa-contact exist with the same
// e-mail address Zarafa will STOP DELIVERING mails for that user.
// If mail = set
if (!empty($newContact["mail"])) {
// Set search variables
$dn= LDAP_USERS_OU;
$filter='(mail='.$newContact["mail"].')';
$getthese=array("mail");
// Perform the search
$sre=ldap_search($ds, $dn, $filter, $getthese);
// Get the results
$userArray = ldap_get_entries($ds,$sre);
// If results are found, the e-mail exists on a user already -> for now: SKIP!
if ($userArray["count"] > 0) {
print "[vtiger2ldap] Was going to add $myDn but: e-mail found for zarafa-user! Skipping...\n\n";
continue;
}
}
// If we got here, there is no email set OR it does not exist on a user already
// -> Add the contact in LDAP
$r = ldap_add($ds, $myDn, $newContact);
// The contact was added, raise the uidNumber for the next contact found.
$uidNumber++;
print "[vtiger2ldap] Contact $myDn does not exist yet, adding...\n\n";
} else {
// Results were found, the contact exists already, use ldap_modify to modify the contact.
// unset the uidNumber so we don't overwrite it!
unset($newContact["uidNumber"]);
// Modify the contact
$r = ldap_modify($ds, $myDn, $newContact);
print "[vtiger2ldap] Contact $myDn exists, modifying...\n\n";
}
}
$start = $start + $limit;
}
// If contacts without an account are found, send an e-mail to the responsible person.
if ($noAcc != "") {
print "[vtiger2ldap] Contacts without an account found, sending email...\n\n";
$to= WARNMAIL_TO;
$subject= WARNMAIL_SUBJECT;
$body="Found vtiger contacts without an account during the last sync!\nThey will not be synced!\nPlease add them to an account!\n\nThe following contacts where not synced because they don't have an account:\n\n$noAcc";
mail($to,$subject,$body);
}