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