[4445] | 1 | <?php |
---|
[5068] | 2 | /** |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2011 Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 5 | * |
---|
[5130] | 6 | * This program is free software; you can redistribute it and/or |
---|
| 7 | * modify it under the terms of the GNU General Public License |
---|
| 8 | * as published by the Free Software Foundation; either version 2 |
---|
| 9 | * of the License, or (at your option) any later version. |
---|
[5068] | 10 | * |
---|
[5130] | 11 | * This program is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | * GNU General Public License for more details. |
---|
[5068] | 15 | * |
---|
[5130] | 16 | * You should have received a copy of the GNU General Public License |
---|
| 17 | * along with this program; if not, write to the Free Software |
---|
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
[5068] | 19 | * |
---|
| 20 | * You can contact Prognus Software Livre headquarters at Av. Tancredo Neves, |
---|
| 21 | * 6731, PTI, Bl. 05, Esp. 02, Sl. 10, Foz do Iguaçu - PR - Brasil or at |
---|
| 22 | * e-mail address prognus@prognus.com.br. |
---|
| 23 | * |
---|
| 24 | * |
---|
[5130] | 25 | * @package LdapService |
---|
[5068] | 26 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 27 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 28 | * @sponsor Caixa Econômica Federal |
---|
| 29 | * @version 1.0 |
---|
[5130] | 30 | * @since 2.4.0 |
---|
[5068] | 31 | */ |
---|
[5130] | 32 | |
---|
[4445] | 33 | class LdapService |
---|
| 34 | { |
---|
[4828] | 35 | var $limit = 11; |
---|
[4445] | 36 | |
---|
| 37 | var $allTargetTypes = array( 'i', 'g', 'l', 'u', 's' ); |
---|
| 38 | |
---|
| 39 | var $connection; |
---|
| 40 | |
---|
| 41 | function LdapService() |
---|
| 42 | { |
---|
[5130] | 43 | if(isset($GLOBALS['phpgw_info']['server']['ldap_context']) ) |
---|
| 44 | $this->context = $GLOBALS['phpgw_info']['server']['ldap_context']; |
---|
[4445] | 45 | } |
---|
| 46 | |
---|
[5130] | 47 | public function setContext( $pContext ) |
---|
| 48 | { |
---|
| 49 | $this->context = $pContext; |
---|
| 50 | } |
---|
[5068] | 51 | |
---|
[4445] | 52 | function connect($host='', $dn='', $passwd='', $ldapreferral=false) |
---|
| 53 | { |
---|
| 54 | if(!$host || $host == $GLOBALS['phpgw_info']['server']['ldap_host']) |
---|
| 55 | { |
---|
| 56 | $dn = $dn ? $dn : $GLOBALS['phpgw_info']['server']['ldap_root_dn']; |
---|
| 57 | $passwd = $passwd ? $passwd : $GLOBALS['phpgw_info']['server']['ldap_root_pw']; |
---|
| 58 | $host = $host ? $host : $GLOBALS['phpgw_info']['server']['ldap_host']; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | $conection = ldap_connect($host); |
---|
| 62 | |
---|
| 63 | if(!ldap_set_option($conection,LDAP_OPT_PROTOCOL_VERSION,3)) |
---|
| 64 | $GLOBALS['phpgw_info']['server']['ldap_version3'] = False; |
---|
| 65 | |
---|
| 66 | ldap_set_option($conection, LDAP_OPT_REFERRALS, $ldapreferral); |
---|
| 67 | |
---|
| 68 | if($ldapreferral) |
---|
| 69 | { |
---|
| 70 | $GLOBALS['phpgw_info']['server']['user_ldap_referral'] = $dn; |
---|
| 71 | $GLOBALS['phpgw_info']['server']['password_ldap_referral'] = $passwd; |
---|
| 72 | ldap_set_rebind_proc($conection, ldap_rebind); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | if($dn && $passwd && !@ldap_bind($conection,$dn,$passwd)) |
---|
| 76 | @ldap_bind($conection,$dn,$passwd); |
---|
| 77 | |
---|
| 78 | $this->connection = $conection; |
---|
| 79 | |
---|
| 80 | return( $connection ); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | function _or( $toWrap ) |
---|
| 84 | { |
---|
[5068] | 85 | if( !is_array( $toWrap ) ) |
---|
| 86 | return( $toWrap ); |
---|
| 87 | |
---|
| 88 | return $this->wrap( $toWrap, '|' ); |
---|
[4445] | 89 | } |
---|
| 90 | |
---|
| 91 | function _and( $toWrap ) |
---|
| 92 | { |
---|
[5068] | 93 | if( !is_array( $toWrap ) ) |
---|
| 94 | return( $toWrap ); |
---|
| 95 | |
---|
| 96 | return $this->wrap( $toWrap, '&' ); |
---|
[4445] | 97 | } |
---|
| 98 | |
---|
| 99 | function _not( $toWrap ) |
---|
| 100 | { |
---|
[5068] | 101 | return $this->wrap( $toWrap, '!' ); |
---|
[4445] | 102 | } |
---|
| 103 | |
---|
| 104 | function wrap( $toWrap, $conditional = "" ) |
---|
| 105 | { |
---|
[5068] | 106 | if( !is_array( $toWrap ) ) |
---|
| 107 | $toWrap = array( $toWrap ); |
---|
| 108 | |
---|
| 109 | $toWrap = array_unique( $toWrap ); |
---|
[4445] | 110 | |
---|
[5068] | 111 | return "(".$conditional.implode( "", $toWrap ).")"; |
---|
[4445] | 112 | } |
---|
| 113 | |
---|
| 114 | function getSearchFilter( $search, $targetTypes = false, $customFilter = '', $exact = false ) |
---|
| 115 | { |
---|
| 116 | $search = utf8_encode( $search ); |
---|
| 117 | |
---|
[5068] | 118 | if( !$targetTypes ) |
---|
| 119 | $targetTypes = $this->allTargetTypes; |
---|
| 120 | |
---|
| 121 | if( !is_array( $targetTypes ) ) |
---|
| 122 | $targetTypes = array( $targetTypes ); |
---|
| 123 | |
---|
| 124 | $searchFilter = ''; |
---|
| 125 | |
---|
| 126 | foreach( $targetTypes as $targetType ) |
---|
[4445] | 127 | { |
---|
[5068] | 128 | switch( $targetType ) |
---|
| 129 | { |
---|
| 130 | case 'g': |
---|
| 131 | { |
---|
| 132 | //no caso de grupos, a busca so precisa ser feita em cima do CN |
---|
| 133 | $searchFilter = $this->stemFilter( $search, 'cn' ); |
---|
| 134 | } |
---|
| 135 | break; |
---|
| 136 | |
---|
| 137 | default : |
---|
| 138 | { |
---|
| 139 | //parametros que a busca tem que ser sintaticas |
---|
| 140 | $searchFilter = /*array( */$this->stemFilter( $search, array( |
---|
| 141 | // UID e employeeNumber podem ser iguais ou muito similares, dependendo da organizacao |
---|
| 142 | 'uid', /*'employeeNumber', */'cn', |
---|
| 143 | // givenName e SN sao complementares (nome e sobrenome) |
---|
[5130] | 144 | 'givenName', 'sn', 'displayName', 'mail', 'mailAlternateAddress' ) //), |
---|
[5068] | 145 | //parametros que a busca pode ser por aproximacao fonetica |
---|
| 146 | /*$this->approxFilter( $search, array( |
---|
| 147 | // O CN e displayName geralmente sao a mesma coisa |
---|
| 148 | 'cn', 'displayName' ))*/ ); |
---|
| 149 | |
---|
| 150 | $searchFilter = $this->stemFilter( $search, array( 'cn', 'givenName', 'uid', |
---|
[5130] | 151 | 'sn', 'displayName', 'mail', 'mailAlternateAddress' ) ); |
---|
[5068] | 152 | } |
---|
| 153 | break; |
---|
| 154 | } |
---|
[4445] | 155 | } |
---|
| 156 | |
---|
[5068] | 157 | $filter = array(); |
---|
| 158 | |
---|
| 159 | if( $customFilter ) |
---|
| 160 | $filter[] = $customFilter; |
---|
| 161 | if( $search ) |
---|
| 162 | $filter[] = $searchFilter; |
---|
| 163 | |
---|
| 164 | return $this->_and( array( |
---|
| 165 | // Somente objetos relacionados com o Expresso |
---|
| 166 | $this->accountFilter( $targetTypes ), |
---|
| 167 | // Objetos ocultados e/ou desativados pelo Administrador nao podem ser exibidos nas consultas do Expresso |
---|
| 168 | $this->securityFilter( $targetTypes ), |
---|
| 169 | //foco da busca |
---|
| 170 | ( $exact ? $this->_and( $filter ) : $this->_or( $filter ) ) |
---|
| 171 | )); |
---|
| 172 | |
---|
[4445] | 173 | } |
---|
| 174 | |
---|
[5068] | 175 | function securityFilter( $targetTypes ) |
---|
[4445] | 176 | { |
---|
[5068] | 177 | if( !$targetTypes ) |
---|
| 178 | $targetTypes = $this->allTargetTypes; |
---|
[4445] | 179 | |
---|
[5068] | 180 | if( !is_array( $targetTypes ) ) |
---|
| 181 | $targetTypes = array( $targetTypes ); |
---|
| 182 | |
---|
| 183 | $typeFilter = array(); |
---|
| 184 | |
---|
| 185 | foreach( $targetTypes as $targetType ) |
---|
| 186 | { |
---|
| 187 | switch( $targetType ) |
---|
| 188 | { |
---|
| 189 | case 'g': $typeFilter[] = "(objectClass=posixGroup)"; |
---|
| 190 | break; |
---|
| 191 | |
---|
[5130] | 192 | default : $typeFilter[] = "(phpgwAccountStatus=A)(accountStatus=active)"; |
---|
[5068] | 193 | break; |
---|
| 194 | } |
---|
| 195 | } |
---|
[4445] | 196 | |
---|
[5068] | 197 | return $this->_and( array( '(!(phpgwAccountVisible=-1))', $this->_or( $typeFilter ) ) ); |
---|
[4445] | 198 | } |
---|
| 199 | |
---|
| 200 | function accountFilter( $targetTypes ) |
---|
| 201 | { |
---|
| 202 | if( !$targetTypes ) |
---|
| 203 | $targetTypes = $this->allTargetTypes; |
---|
| 204 | |
---|
| 205 | if( !is_array( $targetTypes ) ) |
---|
| 206 | $targetTypes = array( $targetTypes ); |
---|
| 207 | |
---|
| 208 | $typeFilter = array(); |
---|
| 209 | |
---|
| 210 | foreach( $targetTypes as $targetType ) |
---|
| 211 | $typeFilter[] = '(phpgwAccountType='.$targetType.')'; |
---|
| 212 | |
---|
| 213 | return $this->_and( array( '(objectClass=phpgwAccount)', $this->_or( $typeFilter ) ) ); |
---|
| 214 | } |
---|
| 215 | |
---|
[5068] | 216 | function stemFilter( $search, $params ) |
---|
[4445] | 217 | { |
---|
[5068] | 218 | $search = str_replace( ' ', '*', $search ); |
---|
[4445] | 219 | |
---|
[5068] | 220 | if( !is_array( $params ) ) |
---|
| 221 | $params = array( $params ); |
---|
| 222 | |
---|
| 223 | foreach( $params as $i => $param ) |
---|
| 224 | $params[$i] = "($param=*$search*)"; |
---|
| 225 | |
---|
| 226 | return $this->_or( $params ); |
---|
[4445] | 227 | } |
---|
| 228 | |
---|
[5068] | 229 | function phoneticFilter( $search, $params ) |
---|
[4445] | 230 | { |
---|
[5068] | 231 | if( eregi( "\d", $search ) ) |
---|
| 232 | return( "" ); |
---|
[4445] | 233 | |
---|
[5068] | 234 | if( !is_array( $params ) ) |
---|
| 235 | $params = array( $params ); |
---|
| 236 | |
---|
| 237 | foreach( $params as $i => $param ) |
---|
| 238 | $params[$i] = "($param~=$search)"; |
---|
| 239 | |
---|
| 240 | return $this->_or( $params ); |
---|
[4445] | 241 | } |
---|
| 242 | |
---|
[5068] | 243 | |
---|
| 244 | |
---|
| 245 | |
---|
[4445] | 246 | function approxFilter( $search, $params ) |
---|
| 247 | { |
---|
[5068] | 248 | return $this->_or( array( $this->stemFilter( $search, $params ), |
---|
[4445] | 249 | $this->phoneticFilter( $search, $params ) ) ); |
---|
| 250 | } |
---|
| 251 | |
---|
[5068] | 252 | public function accountSearch($search, $justthese = "*", $context = false , $accountType = false, $sort = false) |
---|
[4445] | 253 | { |
---|
| 254 | if( !$this->connection ) |
---|
| 255 | $this->connect(); |
---|
| 256 | |
---|
| 257 | $filter = $this->getSearchFilter($search,$accountType); |
---|
| 258 | |
---|
| 259 | if( !$context ) |
---|
[5130] | 260 | $context = $this->context; |
---|
[4445] | 261 | |
---|
[5130] | 262 | $sr = ldap_search( $this->connection, utf8_encode($context), $filter, $justthese, 0, $this->limit ); |
---|
[4445] | 263 | |
---|
[5130] | 264 | if(!$sr) return false; |
---|
| 265 | |
---|
[4445] | 266 | if($sort) |
---|
[5130] | 267 | ldap_sort( $this->connection, $sr, $sort ); |
---|
[4445] | 268 | |
---|
[5130] | 269 | return $this->formatEntries( ldap_get_entries( $this->connection, $sr ) ); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | private function formatEntries ( $pEntries ) |
---|
| 273 | { |
---|
[4445] | 274 | |
---|
[5130] | 275 | if( !$pEntries ) return( false ); |
---|
[4445] | 276 | |
---|
| 277 | $return = array(); |
---|
| 278 | |
---|
[5130] | 279 | for ($i=0; $i < $pEntries["count"]; $i++) |
---|
[4445] | 280 | { |
---|
| 281 | $entrieTmp = array(); |
---|
[5130] | 282 | foreach ($pEntries[$i] as $index => $value) |
---|
[4445] | 283 | { |
---|
| 284 | if(!is_numeric($index) && $index != 'count') |
---|
| 285 | { |
---|
| 286 | if(is_array($value)) |
---|
| 287 | { |
---|
| 288 | if(count($value) == 2) |
---|
| 289 | $entrieTmp[$index] = utf8_decode($value['0']); |
---|
| 290 | else |
---|
| 291 | { |
---|
| 292 | foreach ($value as $index2 =>$value2) |
---|
| 293 | { |
---|
| 294 | if($index != 'count') |
---|
| 295 | $entrieTmp[$index][$index2] = utf8_decode($value2); |
---|
| 296 | } |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | else |
---|
| 300 | $entrieTmp[$index] = utf8_decode($value); |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | $return[] = $entrieTmp; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | return( $return ); |
---|
| 308 | } |
---|
| 309 | |
---|
[5130] | 310 | /** |
---|
| 311 | * Retorna o endereço de e-mail da conta pelo uidNumber |
---|
| 312 | * |
---|
| 313 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 314 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 315 | * @sponsor Caixa Econômica Federal |
---|
| 316 | * @author Cristiano Corrêa Schmidt |
---|
| 317 | * @param int $pUidNumber uidNumber da conta |
---|
| 318 | * @return string |
---|
| 319 | * @access public |
---|
| 320 | */ |
---|
| 321 | public function getMailByUidNumber( $pUidNumber ) |
---|
| 322 | { |
---|
| 323 | if( !$this->connection ) $this->connect(); |
---|
| 324 | $sr = ldap_search( $this->connection, $this->context, '(uidNumber='.$pUidNumber.')', array('mail') ); |
---|
| 325 | if(!$sr) return false; |
---|
| 326 | |
---|
| 327 | $return = ldap_get_entries( $this->connection, $sr ); |
---|
| 328 | return $return[0]['mail'][0]; |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | /** |
---|
| 332 | * Retorna em um array os endereços de e-mails alternativos da conta pelo uidNumber |
---|
| 333 | * |
---|
| 334 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 335 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 336 | * @sponsor Caixa Econômica Federal |
---|
| 337 | * @author Cristiano Corrêa Schmidt |
---|
| 338 | * @param int $pUidNumber uidNumber da conta |
---|
| 339 | * @return Array |
---|
| 340 | * @access public |
---|
| 341 | */ |
---|
| 342 | public function getMailAlternateByUidNumber( $pUidNumber ) |
---|
| 343 | { |
---|
| 344 | if( !$this->connection ) $this->connect(); |
---|
| 345 | |
---|
| 346 | $sr = ldap_search( $this->connection, $this->context, '(uidNumber='.$pUidNumber.')', array('mailAlternateAddress')); |
---|
| 347 | if(!$sr) return false; |
---|
| 348 | |
---|
| 349 | $returnL = ldap_get_entries( $this->connection,$sr); |
---|
| 350 | $return = array(); |
---|
| 351 | foreach ($returnL[0]['mailalternateaddress'] as $i => $v) |
---|
| 352 | { |
---|
| 353 | if($i === 'count') continue; |
---|
| 354 | $return[] = $v; |
---|
| 355 | } |
---|
| 356 | return $return; |
---|
| 357 | } |
---|
| 358 | |
---|
[4445] | 359 | } |
---|
| 360 | |
---|
| 361 | ServiceLocator::register( 'ldap', new LdapService() ); |
---|
| 362 | |
---|