[5341] | 1 | <?php |
---|
| 2 | |
---|
| 3 | class OpenLDAP implements Service |
---|
| 4 | { |
---|
| 5 | var $con; |
---|
| 6 | var $config; |
---|
| 7 | var $limit = 10; |
---|
| 8 | |
---|
| 9 | public function find ( $uri, $justthese = false, $criteria = false ) |
---|
| 10 | { |
---|
[5437] | 11 | $map = Config::get($uri['concept'], 'OpenLDAP.mapping'); |
---|
[5341] | 12 | |
---|
| 13 | if( !isset($criteria["limit"]) ) |
---|
| 14 | $criteria["limit"] = $this->limit; |
---|
| 15 | |
---|
[5437] | 16 | $sr = ldap_search( $this->con , $this->config['context'] , self::parseCriteria($criteria , $map) , self::parseJustthese($justthese, $map) , 0 , $criteria["limit"]); |
---|
[5341] | 17 | if(!$sr) return false; |
---|
| 18 | |
---|
| 19 | if( isset($criteria["order"]) ) |
---|
| 20 | ldap_sort( $this->con, $sr, $criteria["order"] ); |
---|
| 21 | |
---|
[5437] | 22 | return self::_formatEntries( ldap_get_entries( $this->con, $sr ) , $map); |
---|
[5341] | 23 | } |
---|
| 24 | |
---|
| 25 | public function read ( $uri, $justthese = false ) |
---|
| 26 | { |
---|
[5437] | 27 | $map = Config::get($uri['concept'], 'OpenLDAP.mapping'); |
---|
| 28 | |
---|
[5341] | 29 | if( $justthese === false || $justthese === null ) |
---|
| 30 | $sr = ldap_search( $this->con, $this->config['context'], '('.$this->config['idAtribute'].'='.$uri['id'].')' ); |
---|
| 31 | else |
---|
[5437] | 32 | $sr = ldap_search( $this->con, $this->config['context'], '('.$this->config['idAtribute'].'='.$uri['id'].')', self::parseJustthese($justthese, $map) ); |
---|
[5341] | 33 | |
---|
| 34 | if(!$sr) return false; |
---|
| 35 | |
---|
[5437] | 36 | $return = self::_formatEntries( ldap_get_entries( $this->con, $sr ) , $map ); |
---|
[5341] | 37 | |
---|
| 38 | return isset($return[0]) ? $return[0] : array(); |
---|
| 39 | |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | public function deleteAll( $uri, $justthese = false, $criteria = false ){} |
---|
| 43 | |
---|
| 44 | public function delete ( $uri, $justthese = false ) |
---|
| 45 | { |
---|
| 46 | // return ldap_delete ($this->con , $this->config['context'].','.$uri['id'] ); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | public function replace ( $uri, $data, $criteria = false ){} |
---|
| 50 | |
---|
| 51 | public function update ( $uri, $data ){} |
---|
| 52 | |
---|
| 53 | public function create ( $uri, $data ){} |
---|
| 54 | |
---|
| 55 | public function open ( $config ) |
---|
| 56 | { |
---|
| 57 | $this->config = $config; |
---|
| 58 | |
---|
| 59 | $this->con = ldap_connect( $config['host'] ); |
---|
| 60 | |
---|
| 61 | ldap_set_option( $this->con,LDAP_OPT_PROTOCOL_VERSION,3 ); |
---|
| 62 | |
---|
| 63 | if( isset( $config['user'] ) && isset( $config['password'] ) ) |
---|
| 64 | ldap_bind( $this->con, $config['user'], $config['password'] ); |
---|
| 65 | |
---|
| 66 | return( $this->con ); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | public function close() |
---|
| 70 | { |
---|
| 71 | ldap_close($this->con); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | public function setup(){} |
---|
| 75 | |
---|
| 76 | public function teardown(){} |
---|
| 77 | |
---|
| 78 | public function begin( $uri ){ |
---|
| 79 | |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | public function commit( $uri ){ |
---|
| 83 | return( true ); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | public function rollback( $uri ){ |
---|
| 87 | } |
---|
| 88 | |
---|
[5437] | 89 | private static function _formatEntries ( $pEntries , &$map ) |
---|
[5341] | 90 | { |
---|
| 91 | if( !$pEntries ) return( false ); |
---|
[5437] | 92 | |
---|
| 93 | $newMap = array(); |
---|
| 94 | foreach ($map as $i => &$v) |
---|
| 95 | $newMap[strtolower($v)] = $i; |
---|
| 96 | |
---|
[5341] | 97 | $return = array(); |
---|
| 98 | for ($i=0; $i < $pEntries["count"]; $i++) |
---|
| 99 | { |
---|
| 100 | $entrieTmp = array(); |
---|
| 101 | foreach ($pEntries[$i] as $index => $value) |
---|
| 102 | { |
---|
[5437] | 103 | if(isset($newMap[$index])) |
---|
[5341] | 104 | { |
---|
| 105 | if(is_array($value)) |
---|
| 106 | { |
---|
| 107 | if(count($value) == 2) |
---|
[5437] | 108 | $entrieTmp[$newMap[$index]] = $value['0']; |
---|
[5341] | 109 | else |
---|
| 110 | { |
---|
| 111 | foreach ($value as $index2 =>$value2) |
---|
| 112 | { |
---|
[5437] | 113 | if($index2 != 'count') |
---|
| 114 | $entrieTmp[$newMap[$index]][$index2] = $value2; |
---|
[5341] | 115 | } |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | else |
---|
[5437] | 119 | $entrieTmp[$newMap[$index]] = $value; |
---|
[5341] | 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | $return[] = $entrieTmp; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | return( $return ); |
---|
| 127 | } |
---|
| 128 | |
---|
[5437] | 129 | private static function parseCriteria( $criteria , &$map) |
---|
[5341] | 130 | { |
---|
| 131 | $result = ""; |
---|
| 132 | |
---|
| 133 | if( isset($criteria["filter"]) ) |
---|
| 134 | { |
---|
| 135 | /* |
---|
| 136 | * ex: array ( |
---|
| 137 | * [0] 'OR', |
---|
| 138 | * [1] array( 'OR', array( array( '=', 'campo', 'valor' ) ), |
---|
| 139 | * [2] array( '=', 'campo' , 'valor' ), |
---|
| 140 | * [3] array( 'IN', 'campo', array( '1' , '2' , '3' ) ) |
---|
| 141 | * ) |
---|
| 142 | * OR |
---|
| 143 | * array( '=' , 'campo' , 'valor' ) |
---|
| 144 | */ |
---|
| 145 | |
---|
| 146 | |
---|
[5437] | 147 | $result .= self::parseFilter( $criteria['filter'] , $map); |
---|
[5341] | 148 | } |
---|
[5441] | 149 | |
---|
[5341] | 150 | return $result; |
---|
| 151 | } |
---|
| 152 | |
---|
[5437] | 153 | private static function parseFilter( $filter , &$map) |
---|
[5341] | 154 | { |
---|
| 155 | $result = ''; |
---|
[5441] | 156 | $as = array_shift( $filter ); |
---|
| 157 | $op = self::parseOperator( $as ); |
---|
| 158 | |
---|
[5341] | 159 | if( is_array($filter[0]) ) |
---|
| 160 | { |
---|
| 161 | $nested = ''; |
---|
| 162 | |
---|
| 163 | foreach( $filter as $i => $f ) |
---|
[5437] | 164 | $nested .= self::parseFilter($f , $map); |
---|
[5341] | 165 | |
---|
| 166 | $fil = $op.$nested; |
---|
| 167 | } |
---|
[5437] | 168 | else if( isset($map[$filter[0]]) ) |
---|
[5441] | 169 | { |
---|
| 170 | if($as === '*') $filter[1] = str_replace (' ', '* *', $filter[1]); |
---|
| 171 | |
---|
[5437] | 172 | $fil = $op[0].$map[$filter[0]].$op[1].$filter[1].$op[2]; |
---|
[5441] | 173 | } |
---|
[5437] | 174 | else |
---|
| 175 | return ''; |
---|
| 176 | |
---|
[5341] | 177 | return '('.$fil.')'; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | private static function parseOperator( $op ) |
---|
| 181 | { |
---|
| 182 | switch( $op ) |
---|
| 183 | { |
---|
| 184 | case 'AND': return '&'; |
---|
| 185 | case 'OR': return '|'; |
---|
| 186 | case '^': return array('', '=*', '' ); |
---|
| 187 | case '$': return array('', '=' , '*' ); |
---|
| 188 | case '*': return array('', '=*', '*' ); |
---|
| 189 | case '!': return array('!(', '=', ')', ); |
---|
| 190 | default : return array('', $op , '' ); |
---|
| 191 | } |
---|
| 192 | } |
---|
[5437] | 193 | |
---|
| 194 | private static function parseJustthese($justthese , &$map) |
---|
| 195 | { |
---|
| 196 | if(!is_array($justthese)) //Caso seja um full select pegar todas as keys |
---|
| 197 | $justthese = array_keys($map); |
---|
| 198 | |
---|
| 199 | $return = array(); |
---|
| 200 | |
---|
| 201 | foreach ($justthese as &$value) |
---|
| 202 | if(isset($map[$value])) |
---|
| 203 | $return[] = $map[$value]; |
---|
| 204 | |
---|
| 205 | return $return; |
---|
| 206 | } |
---|
[5341] | 207 | } |
---|
| 208 | |
---|
| 209 | ?> |
---|