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

Revision 7655, 5.0 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Melhorias de performance no codigo do Expresso.

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