|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class LdapContactsSuggestions implements \RainLoop\Providers\Suggestions\ISuggestions |
|
4
|
|
|
{ |
|
5
|
|
|
/** |
|
6
|
|
|
* @var string |
|
7
|
|
|
*/ |
|
8
|
|
|
private $sHostName = '127.0.0.1'; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @var int |
|
12
|
|
|
*/ |
|
13
|
|
|
private $iHostPort = 389; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $sAccessDn = NULL; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $sAccessPassword = NULL; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $sUsersDn = ''; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $sObjectClass = 'inetOrgPerson'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $sUidField = 'uid'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $sNameField = 'givenname'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
private $sEmailField = 'mail'; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var \MailSo\Log\Logger |
|
52
|
|
|
*/ |
|
53
|
|
|
private $oLogger = null; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
private $sAllowedEmails = ''; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $sHostName |
|
62
|
|
|
* @param int $iHostPort |
|
63
|
|
|
* @param string $sAccessDn |
|
64
|
|
|
* @param string $sAccessPassword |
|
65
|
|
|
* @param string $sUsersDn |
|
66
|
|
|
* @param string $sObjectClass |
|
67
|
|
|
* @param string $sNameField |
|
68
|
|
|
* @param string $sEmailField |
|
69
|
|
|
* |
|
70
|
|
|
* @return \LdapContactsSuggestions |
|
71
|
|
|
*/ |
|
72
|
|
|
public function SetConfig($sHostName, $iHostPort, $sAccessDn, $sAccessPassword, $sUsersDn, $sObjectClass, $sUidField, $sNameField, $sEmailField) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->sHostName = $sHostName; |
|
75
|
|
|
$this->iHostPort = $iHostPort; |
|
76
|
|
|
if (0 < \strlen($sAccessDn)) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->sAccessDn = $sAccessDn; |
|
79
|
|
|
$this->sAccessPassword = $sAccessPassword; |
|
80
|
|
|
} |
|
81
|
|
|
$this->sUsersDn = $sUsersDn; |
|
82
|
|
|
$this->sObjectClass = $sObjectClass; |
|
83
|
|
|
$this->sUidField = $sUidField; |
|
84
|
|
|
$this->sNameField = $sNameField; |
|
85
|
|
|
$this->sEmailField = $sEmailField; |
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $sAllowedEmails |
|
92
|
|
|
* |
|
93
|
|
|
* @return \LdapContactsSuggestions |
|
94
|
|
|
*/ |
|
95
|
|
|
public function SetAllowedEmails($sAllowedEmails) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->sAllowedEmails = $sAllowedEmails; |
|
98
|
|
|
|
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param \RainLoop\Model\Account $oAccount |
|
104
|
|
|
* @param string $sQuery |
|
105
|
|
|
* @param int $iLimit = 20 |
|
106
|
|
|
* |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
public function Process($oAccount, $sQuery, $iLimit = 20) |
|
110
|
|
|
{ |
|
111
|
|
|
$sQuery = \trim($sQuery); |
|
112
|
|
|
|
|
113
|
|
|
if (2 > \strlen($sQuery)) |
|
114
|
|
|
{ |
|
115
|
|
|
return array(); |
|
116
|
|
|
} |
|
117
|
|
|
else if (!$oAccount || !\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails)) |
|
118
|
|
|
{ |
|
119
|
|
|
return array(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$aResult = $this->ldapSearch($oAccount, $sQuery); |
|
123
|
|
|
|
|
124
|
|
|
$aResult = \RainLoop\Utils::RemoveSuggestionDuplicates($aResult); |
|
125
|
|
|
if ($iLimit < \count($aResult)) |
|
126
|
|
|
{ |
|
127
|
|
|
$aResult = \array_slice($aResult, 0, $iLimit); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $aResult; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param array $aLdapItem |
|
135
|
|
|
* @param array $aEmailFields |
|
136
|
|
|
* @param array $aNameFields |
|
137
|
|
|
* |
|
138
|
|
|
* @return array |
|
139
|
|
|
*/ |
|
140
|
|
|
private function findNameAndEmail($aLdapItem, $aEmailFields, $aNameFields, $aUidFields) |
|
141
|
|
|
{ |
|
142
|
|
|
$sEmail = $sName = $sUid = ''; |
|
143
|
|
|
if ($aLdapItem) |
|
|
|
|
|
|
144
|
|
|
{ |
|
145
|
|
View Code Duplication |
foreach ($aEmailFields as $sField) |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
|
|
if (!empty($aLdapItem[$sField][0])) |
|
148
|
|
|
{ |
|
149
|
|
|
$sEmail = \trim($aLdapItem[$sField][0]); |
|
150
|
|
|
if (!empty($sEmail)) |
|
151
|
|
|
{ |
|
152
|
|
|
break; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
View Code Duplication |
foreach ($aNameFields as $sField) |
|
|
|
|
|
|
158
|
|
|
{ |
|
159
|
|
|
if (!empty($aLdapItem[$sField][0])) |
|
160
|
|
|
{ |
|
161
|
|
|
$sName = \trim($aLdapItem[$sField][0]); |
|
162
|
|
|
if (!empty($sName)) |
|
163
|
|
|
{ |
|
164
|
|
|
break; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
View Code Duplication |
foreach ($aUidFields as $sField) |
|
|
|
|
|
|
170
|
|
|
{ |
|
171
|
|
|
if (!empty($aLdapItem[$sField][0])) |
|
172
|
|
|
{ |
|
173
|
|
|
$sUid = \trim($aLdapItem[$sField][0]); |
|
174
|
|
|
if (!empty($sUid)) |
|
175
|
|
|
{ |
|
176
|
|
|
break; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return array($sEmail, $sName, $sUid); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param \RainLoop\Model\Account $oAccount |
|
187
|
|
|
* @param string $sQuery |
|
188
|
|
|
* |
|
189
|
|
|
* @return array |
|
190
|
|
|
*/ |
|
191
|
|
|
private function ldapSearch($oAccount, $sQuery) |
|
192
|
|
|
{ |
|
193
|
|
|
$sSearchEscaped = $this->escape($sQuery); |
|
194
|
|
|
|
|
195
|
|
|
$aResult = array(); |
|
196
|
|
|
$oCon = @\ldap_connect($this->sHostName, $this->iHostPort); |
|
197
|
|
|
if ($oCon) |
|
198
|
|
|
{ |
|
199
|
|
|
$this->oLogger->Write('ldap_connect: connected', \MailSo\Log\Enumerations\Type::INFO, 'LDAP'); |
|
200
|
|
|
|
|
201
|
|
|
@\ldap_set_option($oCon, LDAP_OPT_PROTOCOL_VERSION, 3); |
|
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
if (!@\ldap_bind($oCon, $this->sAccessDn, $this->sAccessPassword)) |
|
204
|
|
|
{ |
|
205
|
|
|
if ( is_null($this->sAccessDn) ) { |
|
206
|
|
|
$this->logLdapError($oCon, 'ldap_bind (anonymous)'); |
|
207
|
|
|
} else { |
|
208
|
|
|
$this->logLdapError($oCon, 'ldap_bind'); |
|
209
|
|
|
} |
|
210
|
|
|
return $aResult; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$sDomain = \MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email()); |
|
214
|
|
|
$sSearchDn = \strtr($this->sUsersDn, array( |
|
215
|
|
|
'{domain}' => $sDomain, |
|
216
|
|
|
'{domain:dc}' => 'dc='.\strtr($sDomain, array('.' => ',dc=')), |
|
217
|
|
|
'{email}' => $oAccount->Email(), |
|
218
|
|
|
'{email:user}' => \MailSo\Base\Utils::GetAccountNameFromEmail($oAccount->Email()), |
|
219
|
|
|
'{email:domain}' => $sDomain, |
|
220
|
|
|
'{login}' => $oAccount->Login(), |
|
221
|
|
|
'{imap:login}' => $oAccount->Login(), |
|
222
|
|
|
'{imap:host}' => $oAccount->DomainIncHost(), |
|
223
|
|
|
'{imap:port}' => $oAccount->DomainIncPort() |
|
224
|
|
|
)); |
|
225
|
|
|
|
|
226
|
|
|
$aEmails = empty($this->sEmailField) ? array() : \explode(',', $this->sEmailField); |
|
227
|
|
|
$aNames = empty($this->sNameField) ? array() : \explode(',', $this->sNameField); |
|
228
|
|
|
$aUIDs = empty($this->sUidField) ? array() : \explode(',', $this->sUidField); |
|
229
|
|
|
|
|
230
|
|
|
$aEmails = \array_map('trim', $aEmails); |
|
231
|
|
|
$aNames = \array_map('trim', $aNames); |
|
232
|
|
|
$aUIDs = \array_map('trim', $aUIDs); |
|
233
|
|
|
|
|
234
|
|
|
$aFields = \array_merge($aEmails, $aNames, $aUIDs); |
|
235
|
|
|
|
|
236
|
|
|
$aItems = array(); |
|
237
|
|
|
$sSubFilter = ''; |
|
238
|
|
|
foreach ($aFields as $sItem) |
|
239
|
|
|
{ |
|
240
|
|
|
if (!empty($sItem)) |
|
241
|
|
|
{ |
|
242
|
|
|
$aItems[] = $sItem; |
|
243
|
|
|
$sSubFilter .= '('.$sItem.'=*'.$sSearchEscaped.'*)'; |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
$sFilter = '(&(objectclass='.$this->sObjectClass.')'; |
|
248
|
|
|
$sFilter .= (1 < count($aItems) ? '(|' : '').$sSubFilter.(1 < count($aItems) ? ')' : ''); |
|
249
|
|
|
$sFilter .= ')'; |
|
250
|
|
|
|
|
251
|
|
|
$this->oLogger->Write('ldap_search: start: '.$sSearchDn.' / '.$sFilter, \MailSo\Log\Enumerations\Type::INFO, 'LDAP'); |
|
252
|
|
|
$oS = @\ldap_search($oCon, $sSearchDn, $sFilter, $aItems, 0, 30, 30); |
|
253
|
|
|
if ($oS) |
|
254
|
|
|
{ |
|
255
|
|
|
$aEntries = @\ldap_get_entries($oCon, $oS); |
|
256
|
|
|
if (is_array($aEntries)) |
|
257
|
|
|
{ |
|
258
|
|
|
if (isset($aEntries['count'])) |
|
259
|
|
|
{ |
|
260
|
|
|
unset($aEntries['count']); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
foreach ($aEntries as $aItem) |
|
264
|
|
|
{ |
|
265
|
|
|
if ($aItem) |
|
266
|
|
|
{ |
|
267
|
|
|
$sName = $sEmail = ''; |
|
268
|
|
|
list ($sEmail, $sName) = $this->findNameAndEmail($aItem, $aEmails, $aNames, $aUIDs); |
|
269
|
|
|
if (!empty($sEmail)) |
|
270
|
|
|
{ |
|
271
|
|
|
$aResult[] = array($sEmail, $sName); |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
else |
|
277
|
|
|
{ |
|
278
|
|
|
$this->logLdapError($oCon, 'ldap_get_entries'); |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
else |
|
282
|
|
|
{ |
|
283
|
|
|
$this->logLdapError($oCon, 'ldap_search'); |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
else |
|
287
|
|
|
{ |
|
288
|
|
|
return $aResult; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
return $aResult; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* @param string $sStr |
|
296
|
|
|
* |
|
297
|
|
|
* @return string |
|
298
|
|
|
*/ |
|
299
|
|
|
public function escape($sStr) |
|
300
|
|
|
{ |
|
301
|
|
|
$aNewChars = array(); |
|
302
|
|
|
$aChars = array('\\', '*', '(', ')', \chr(0)); |
|
303
|
|
|
|
|
304
|
|
|
foreach ($aChars as $iIndex => $sValue) |
|
305
|
|
|
{ |
|
306
|
|
|
$aNewChars[$iIndex] = '\\'.\str_pad(\dechex(\ord($sValue)), 2, '0'); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
return \str_replace($aChars, $aNewChars, $sStr); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* @param mixed $oCon |
|
314
|
|
|
* @param string $sCmd |
|
315
|
|
|
* |
|
316
|
|
|
* @return string |
|
317
|
|
|
*/ |
|
318
|
|
|
public function logLdapError($oCon, $sCmd) |
|
319
|
|
|
{ |
|
320
|
|
|
if ($this->oLogger) |
|
321
|
|
|
{ |
|
322
|
|
|
$sError = $oCon ? @\ldap_error($oCon) : ''; |
|
323
|
|
|
$iErrno = $oCon ? @\ldap_errno($oCon) : 0; |
|
324
|
|
|
|
|
325
|
|
|
$this->oLogger->Write($sCmd.' error: '.$sError.' ('.$iErrno.')', |
|
326
|
|
|
\MailSo\Log\Enumerations\Type::WARNING, 'LDAP'); |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* @param \MailSo\Log\Logger $oLogger |
|
332
|
|
|
* |
|
333
|
|
|
* @return \LdapContactsSuggestions |
|
334
|
|
|
*/ |
|
335
|
|
|
public function SetLogger($oLogger) |
|
336
|
|
|
{ |
|
337
|
|
|
if ($oLogger instanceof \MailSo\Log\Logger) |
|
|
|
|
|
|
338
|
|
|
{ |
|
339
|
|
|
$this->oLogger = $oLogger; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
return $this; |
|
343
|
|
|
} |
|
344
|
|
|
} |
|
345
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.