source: trunk/prototype/services/OpenLDAP.php @ 5441

Revision 5441, 5.0 KB checked in by cristiano, 12 years ago (diff)

Ticket #2434 - Atualização modulo agenda e API - Correções de serviços

Line 
1<?php
2
3class 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        $map = Config::get($uri['concept'], 'OpenLDAP.mapping');
12       
13        if( !isset($criteria["limit"]) )
14                $criteria["limit"] = $this->limit;
15       
16        $sr =  ldap_search( $this->con , $this->config['context'] , self::parseCriteria($criteria , $map) , self::parseJustthese($justthese, $map) , 0 , $criteria["limit"]);
17        if(!$sr) return false;
18
19        if( isset($criteria["order"]) )
20            ldap_sort( $this->con, $sr, $criteria["order"] );
21       
22        return  self::_formatEntries( ldap_get_entries( $this->con, $sr ) , $map);
23    }
24
25    public function read ( $uri, $justthese = false )
26    {
27        $map = Config::get($uri['concept'], 'OpenLDAP.mapping');
28       
29        if( $justthese === false || $justthese === null )
30                $sr =  ldap_search( $this->con, $this->config['context'], '('.$this->config['idAtribute'].'='.$uri['id'].')' );
31        else
32                $sr =  ldap_search( $this->con, $this->config['context'], '('.$this->config['idAtribute'].'='.$uri['id'].')', self::parseJustthese($justthese, $map) );
33
34        if(!$sr) return false;
35
36        $return =  self::_formatEntries( ldap_get_entries( $this->con, $sr ) , $map );
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
89    private static function _formatEntries ( $pEntries , &$map )
90    {           
91        if( !$pEntries ) return( false ); 
92           
93        $newMap = array();
94        foreach ($map as $i => &$v)
95            $newMap[strtolower($v)] = $i;
96         
97        $return = array();
98        for ($i=0; $i < $pEntries["count"]; $i++)
99        {
100              $entrieTmp = array();
101              foreach ($pEntries[$i] as $index => $value)
102              {
103                  if(isset($newMap[$index]))
104                  {
105                      if(is_array($value))
106                      {
107                          if(count($value) == 2)
108                              $entrieTmp[$newMap[$index]] = $value['0'];
109                          else
110                          {
111                              foreach ($value as $index2 =>$value2)
112                              {
113                                  if($index2 != 'count')
114                                      $entrieTmp[$newMap[$index]][$index2] = $value2;
115                              }
116                          }
117                      }
118                      else
119                          $entrieTmp[$newMap[$index]] = $value;
120                  }
121              }
122
123              $return[] = $entrieTmp;
124        }
125
126        return( $return );
127    }
128
129    private static function parseCriteria( $criteria  , &$map)
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
147                $result .= self::parseFilter( $criteria['filter'] , $map);
148        }
149               
150        return $result;
151    }
152
153    private static function parseFilter( $filter , &$map)
154    {
155        $result = '';
156        $as = array_shift( $filter );
157        $op = self::parseOperator( $as );
158               
159        if( is_array($filter[0]) )
160        {
161            $nested = '';
162
163            foreach( $filter as $i => $f )
164                $nested .= self::parseFilter($f , $map);
165
166            $fil =  $op.$nested;
167        }
168        else  if( isset($map[$filter[0]]) )
169        {   
170            if($as === '*') $filter[1] = str_replace (' ', '* *', $filter[1]);
171             
172            $fil = $op[0].$map[$filter[0]].$op[1].$filter[1].$op[2];
173        }
174        else
175            return '';
176
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    }
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    }
207}
208
209?>
Note: See TracBrowser for help on using the repository browser.