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

Revision 5341, 4.1 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2434 - Commit inicial do novo módulo de agenda do Expresso - expressoCalendar

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        if($justthese === false || $justthese === null)
12                $justthese = array('*');
13       
14        if( !isset($criteria["limit"]) )
15                $criteria["limit"] = $this->limit;
16       
17       
18        $sr =  ldap_search( $this->con , $this->config['context'] , self::parseCriteria($criteria) , $justthese , 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 ) );
25    }
26
27    public function read ( $uri, $justthese = false )
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'].')', $justthese );
33
34        if(!$sr) return false;
35
36        $return =  self::_formatEntries( ldap_get_entries( $this->con, $sr ) );
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 )
90    {           
91        if( !$pEntries ) return( false ); 
92
93        $return = array();
94
95        for ($i=0; $i < $pEntries["count"]; $i++)
96        {
97              $entrieTmp = array();
98              foreach ($pEntries[$i] as $index => $value)
99              {
100                  if(!is_numeric($index) && $index != 'count')
101                  {
102                      if(is_array($value))
103                      {
104                          if(count($value) == 2)
105                              $entrieTmp[$index] = $value['0'];
106                          else
107                          {
108                              foreach ($value as $index2 =>$value2)
109                              {
110                                  if($index != 'count')
111                                      $entrieTmp[$index][$index2] = $value2;
112                              }
113                          }
114                      }
115                      else
116                          $entrieTmp[$index] = $value;
117                  }
118              }
119
120              $return[] = $entrieTmp;
121        }
122
123        return( $return );
124    }
125
126    private static function parseCriteria( $criteria )
127    { 
128        $result = "";
129       
130        if( isset($criteria["filter"]) )
131        {
132                /*
133                  * ex: array   (
134                  *               [0] 'OR',
135                  *               [1] array( 'OR', array( array( '=', 'campo', 'valor' ) ),
136                  *               [2] array( '=', 'campo' , 'valor' ),
137                  *               [3] array( 'IN', 'campo', array( '1' , '2' , '3' ) )
138                  *             )
139                  * OR
140                  *         array( '=' , 'campo' , 'valor' )
141                */
142
143
144                $result .= self::parseFilter( $criteria['filter'] );
145        }
146       
147        return $result;
148    }
149
150    private static function parseFilter( $filter )
151    {
152        $result = '';
153     
154        $op = self::parseOperator( array_shift( $filter ) );
155       
156        if( is_array($filter[0]) )
157        {
158            $nested = '';
159
160            foreach( $filter as $i => $f )
161                $nested .= self::parseFilter($f);
162
163            $fil =  $op.$nested;
164        }
165        else
166            $fil = $op[0].$filter[0].$op[1].$filter[1].$op[2];
167                   
168        return '('.$fil.')';
169    }
170   
171    private static function parseOperator( $op )
172    {
173        switch( $op )
174        {
175            case 'AND': return '&';
176            case 'OR': return '|';
177            case '^': return array('', '=*', ''  );
178            case '$': return array('', '=' , '*' );
179            case '*': return array('', '=*', '*' );
180            case '!': return array('!(', '=', ')', );
181            default : return array('', $op , '' );
182        }
183    }
184}
185
186?>
Note: See TracBrowser for help on using the repository browser.