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