RainLoop /
rainloop-webmail
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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) |
||
|
0 ignored issues
–
show
|
|||
| 144 | { |
||
| 145 | View Code Duplication | foreach ($aEmailFields as $sField) |
|
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 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) |
|
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 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) |
|
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 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); |
||
|
0 ignored issues
–
show
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
|
|||
| 202 | |||
| 203 | if (!@\ldap_bind($oCon, $this->sAccessDn, $this->sAccessPassword)) |
||
| 204 | { |
||
| 205 | if (is_null($this->sAccessDn)) |
||
| 206 | { |
||
| 207 | $this->logLdapError($oCon, 'ldap_bind (anonymous)'); |
||
| 208 | } |
||
| 209 | else |
||
| 210 | { |
||
| 211 | $this->logLdapError($oCon, 'ldap_bind'); |
||
| 212 | } |
||
| 213 | |||
| 214 | return $aResult; |
||
| 215 | } |
||
| 216 | |||
| 217 | $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email()); |
||
| 218 | $sSearchDn = \strtr($this->sUsersDn, array( |
||
| 219 | '{domain}' => $sDomain, |
||
| 220 | '{domain:dc}' => 'dc='.\strtr($sDomain, array('.' => ',dc=')), |
||
| 221 | '{email}' => $oAccount->Email(), |
||
| 222 | '{email:user}' => \MailSo\Base\Utils::GetAccountNameFromEmail($oAccount->Email()), |
||
| 223 | '{email:domain}' => $sDomain, |
||
| 224 | '{login}' => $oAccount->Login(), |
||
| 225 | '{imap:login}' => $oAccount->Login(), |
||
| 226 | '{imap:host}' => $oAccount->DomainIncHost(), |
||
| 227 | '{imap:port}' => $oAccount->DomainIncPort() |
||
| 228 | )); |
||
| 229 | |||
| 230 | $aEmails = empty($this->sEmailField) ? array() : \explode(',', $this->sEmailField); |
||
| 231 | $aNames = empty($this->sNameField) ? array() : \explode(',', $this->sNameField); |
||
| 232 | $aUIDs = empty($this->sUidField) ? array() : \explode(',', $this->sUidField); |
||
| 233 | |||
| 234 | $aEmails = \array_map('trim', $aEmails); |
||
| 235 | $aNames = \array_map('trim', $aNames); |
||
| 236 | $aUIDs = \array_map('trim', $aUIDs); |
||
| 237 | |||
| 238 | $aFields = \array_merge($aEmails, $aNames, $aUIDs); |
||
| 239 | |||
| 240 | $aItems = array(); |
||
| 241 | $sSubFilter = ''; |
||
| 242 | foreach ($aFields as $sItem) |
||
| 243 | { |
||
| 244 | if (!empty($sItem)) |
||
| 245 | { |
||
| 246 | $aItems[] = $sItem; |
||
| 247 | $sSubFilter .= '('.$sItem.'=*'.$sSearchEscaped.'*)'; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | $sFilter = '(&(objectclass='.$this->sObjectClass.')'; |
||
| 252 | $sFilter .= (1 < count($aItems) ? '(|' : '').$sSubFilter.(1 < count($aItems) ? ')' : ''); |
||
| 253 | $sFilter .= ')'; |
||
| 254 | |||
| 255 | $this->oLogger->Write('ldap_search: start: '.$sSearchDn.' / '.$sFilter, \MailSo\Log\Enumerations\Type::INFO, 'LDAP'); |
||
| 256 | $oS = @\ldap_search($oCon, $sSearchDn, $sFilter, $aItems, 0, 30, 30); |
||
| 257 | if ($oS) |
||
| 258 | { |
||
| 259 | $aEntries = @\ldap_get_entries($oCon, $oS); |
||
| 260 | if (is_array($aEntries)) |
||
| 261 | { |
||
| 262 | if (isset($aEntries['count'])) |
||
| 263 | { |
||
| 264 | unset($aEntries['count']); |
||
| 265 | } |
||
| 266 | |||
| 267 | foreach ($aEntries as $aItem) |
||
| 268 | { |
||
| 269 | if ($aItem) |
||
| 270 | { |
||
| 271 | $sName = $sEmail = ''; |
||
| 272 | list ($sEmail, $sName) = $this->findNameAndEmail($aItem, $aEmails, $aNames, $aUIDs); |
||
| 273 | if (!empty($sEmail)) |
||
| 274 | { |
||
| 275 | $aResult[] = array($sEmail, $sName); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | } |
||
| 279 | } |
||
| 280 | else |
||
| 281 | { |
||
| 282 | $this->logLdapError($oCon, 'ldap_get_entries'); |
||
| 283 | } |
||
| 284 | } |
||
| 285 | else |
||
| 286 | { |
||
| 287 | $this->logLdapError($oCon, 'ldap_search'); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | else |
||
| 291 | { |
||
| 292 | return $aResult; |
||
| 293 | } |
||
| 294 | |||
| 295 | return $aResult; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $sStr |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | public function escape($sStr) |
||
| 304 | { |
||
| 305 | $aNewChars = array(); |
||
| 306 | $aChars = array('\\', '*', '(', ')', \chr(0)); |
||
| 307 | |||
| 308 | foreach ($aChars as $iIndex => $sValue) |
||
| 309 | { |
||
| 310 | $aNewChars[$iIndex] = '\\'.\str_pad(\dechex(\ord($sValue)), 2, '0'); |
||
| 311 | } |
||
| 312 | |||
| 313 | return \str_replace($aChars, $aNewChars, $sStr); |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param mixed $oCon |
||
| 318 | * @param string $sCmd |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | public function logLdapError($oCon, $sCmd) |
||
| 323 | { |
||
| 324 | if ($this->oLogger) |
||
| 325 | { |
||
| 326 | $sError = $oCon ? @\ldap_error($oCon) : ''; |
||
| 327 | $iErrno = $oCon ? @\ldap_errno($oCon) : 0; |
||
| 328 | |||
| 329 | $this->oLogger->Write($sCmd.' error: '.$sError.' ('.$iErrno.')', |
||
| 330 | \MailSo\Log\Enumerations\Type::WARNING, 'LDAP'); |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param \MailSo\Log\Logger $oLogger |
||
| 336 | * |
||
| 337 | * @return \LdapContactsSuggestions |
||
| 338 | */ |
||
| 339 | public function SetLogger($oLogger) |
||
| 340 | { |
||
| 341 | if ($oLogger instanceof \MailSo\Log\Logger) |
||
|
0 ignored issues
–
show
The class
MailSo\Log\Logger does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed. Loading history...
|
|||
| 342 | { |
||
| 343 | $this->oLogger = $oLogger; |
||
| 344 | } |
||
| 345 | |||
| 346 | return $this; |
||
| 347 | } |
||
| 348 | } |
||
| 349 |
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.