source: trunk/phpgwapi/inc/class.contacts_ldap.inc.php @ 2

Revision 2, 32.1 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /**************************************************************************\
3  * eGroupWare API - Accounts manager for LDAP                               *
4  * This file written by Miles Lott <milosch@groupwhere.org>                 *
5  * View and manipulate contact records using LDAP                           *
6  * ------------------------------------------------------------------------ *
7  * This library is part of the eGroupWare API                               *
8  * http://www.egroupware.org/api                                            *
9  * ------------------------------------------------------------------------ *
10  * This library is free software; you can redistribute it and/or modify it  *
11  * under the terms of the GNU Lesser General Public License as published by *
12  * the Free Software Foundation; either version 2.1 of the License,         *
13  * or any later version.                                                    *
14  * This library is distributed in the hope that it will be useful, but      *
15  * WITHOUT ANY WARRANTY; without even the implied warranty of               *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
17  * See the GNU Lesser General Public License for more details.              *
18  * You should have received a copy of the GNU Lesser General Public License *
19  * along with this library; if not, write to the Free Software Foundation,  *
20  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            *
21  \**************************************************************************/
22
23
24        /*!
25         @class contacts
26         @abstract Contact List System
27         @discussion Author: jengo/Milosch <br>
28         This class provides a contact database scheme. <br>
29         It attempts to be based on the vcard 2.1 standard, with mods as needed to make for more reasonable sql storage. <br>
30         The LDAP schema used here may require installation of schema files available in the phpgwapi/doc/ldap dir.
31         Please see the README file there.
32         Syntax: CreateObject('phpgwapi.contacts'); <br>
33         Example1: $contacts = CreateObject('phpgwapi.contacts');
34        */
35        class contacts_
36        {
37                var $db = '';
38                var $ldap = '';
39                var $nextid = '';
40                var $std_table = '';
41                var $ext_table = 'phpgw_addressbook_extra';
42
43                var $account_id;
44                var $adr_types;
45                var $total_records;
46                var $grants;
47
48                /* The left side are the array elements used throughout phpgw, right side are the ldap attributes */
49                var $stock_contact_fields = array(
50                        'fn'                  => 'cn',
51                        'n_given'             => 'givenname',
52                        'n_family'            => 'sn',
53                        'n_middle'            => 'phpgwmiddlename',
54                        'n_prefix'            => 'phpgwprefix',
55                        'n_suffix'            => 'phpgwsuffix',
56                        'sound'               => 'phpgwaudio',
57                        'bday'                => 'phpgwbirthday',
58                        'note'                => 'description',
59                        'tz'                  => 'phpgwtz',
60                        'geo'                 => 'phpgwgeo',
61                        'url'                 => 'phpgwurl',
62                        'pubkey'              => 'phpgwpublickey',
63
64                        'org_name'            => 'o',
65                        'org_unit'            => 'ou',
66                        'title'               => 'title',
67
68                        'adr_one_street'      => 'street',
69                        'adr_one_locality'    => 'l',
70                        'adr_one_region'      => 'st',
71                        'adr_one_postalcode'  => 'postalcode',
72                        'adr_one_countryname' => 'co',
73                        'adr_one_type'        => 'phpgwadronetype',
74                        'label'               => 'phpgwaddresslabel',
75
76                        'adr_two_street'      => 'phpgwadrtwostreet',
77                        'adr_two_locality'    => 'phpgwadrtwolocality',
78                        'adr_two_region'      => 'phpgwadrtworegion',
79                        'adr_two_postalcode'  => 'phpgwadrtwopostalcode',
80                        'adr_two_countryname' => 'phpgwadrtwocountryname',
81                        'adr_two_type'        => 'phpgwadrtwotype',
82
83                        'tel_work'            => 'telephonenumber',
84                        'tel_home'            => 'homephone',
85                        'tel_voice'           => 'phpgwvoicetelephonenumber',
86                        'tel_fax'             => 'facsimiletelephonenumber',
87                        'tel_msg'             => 'phpgwmsgtelephonenumber',
88                        'tel_cell'            => 'phpgwcelltelephonenumber',
89                        'tel_pager'           => 'phpgwpagertelephonenumber',
90                        'tel_bbs'             => 'phpgwbbstelephonenumber',
91                        'tel_modem'           => 'phpgwmodemtelephonenumber',
92                        'tel_car'             => 'phpgwmobiletelephonenumber',
93                        'tel_isdn'            => 'phpgwisdnphonenumber',
94                        'tel_video'           => 'phpgwvideophonenumber',
95                        'tel_prefer'          => 'phpgwpreferphone',
96                        'email'               => 'mail',
97                        'email_type'          => 'phpgwmailtype',
98                        'email_home'          => 'phpgwmailhome',
99                        'email_home_type'     => 'phpgwmailhometype'
100                );
101
102                var $non_contact_fields = array(
103                        'id'     => 'uidnumber',
104                        'lid'    => 'uid',
105                        'tid'    => 'phpgwcontacttypeid',
106                        'cat_id' => 'phpgwcontactcatid',
107                        'access' => 'phpgwcontactaccess',
108                        'owner'  => 'phpgwcontactowner'
109                );
110
111                /* Used to set preferphone field */
112                var $tel_types = array(
113                        'work'  => 'work',
114                        'home'  => 'home',
115                        'voice' => 'voice',
116                        'fax'   => 'fax',
117                        'msg'   => 'msg',
118                        'cell'  => 'cell',
119                        'pager' => 'pager',
120                        'bbs'   => 'bbs',
121                        'modem' => 'modem',
122                        'car'   => 'car',
123                        'isdn'  => 'isdn',
124                        'video' => 'video'
125                );
126
127                /* Used to set mail_type fields */
128                var $email_types = array(
129                        'INTERNET'   => 'INTERNET',
130                        'CompuServe' => 'CompuServe',
131                        'AOL'        => 'AOL',
132                        'Prodigy'    => 'Prodigy',
133                        'eWorld'     => 'eWorld',
134                        'AppleLink'  => 'AppleLink',
135                        'AppleTalk'  => 'AppleTalk',
136                        'PowerShare' => 'PowerShare',
137                        'IBMMail'    => 'IBMMail',
138                        'ATTMail'    => 'ATTMail',
139                        'MCIMail'    => 'MCIMail',
140                        'X.400'      => 'X.400',
141                        'TLX'        => 'TLX'
142                );
143
144                function contacts_()
145                {
146                        $this->db = $GLOBALS['phpgw']->db;
147                        $this->ldap = $GLOBALS['phpgw']->common->ldapConnect(
148                                $GLOBALS['phpgw_info']['server']['ldap_contact_host'],
149                                $GLOBALS['phpgw_info']['server']['ldap_contact_dn'],
150                                $GLOBALS['phpgw_info']['server']['ldap_contact_pw']
151                        );
152                        $this->account_id = $GLOBALS['phpgw_info']['user']['account_id'];
153                        $this->grants     = $GLOBALS['phpgw']->acl->get_grants('addressbook');
154
155                        /* Used to flag an address as being:
156                           domestic OR  international(default)
157                           parcel(default)
158                           postal(default)
159                           work(default) OR home
160                        */
161                        $this->adr_types = array(
162                                'dom'    => lang('Domestic'),
163                                'intl'   => lang('International'),
164                                'parcel' => lang('Parcel'),
165                                'postal' => lang('Postal')
166                        );
167                }
168
169                /* send this the id and whatever fields you want to see */
170                function read_single_entry($id,$fields='')
171                {
172                        if(!$fields || empty($fields))
173                        {
174                                $fields = $this->stock_contact_fields;
175                        }
176                        list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
177
178                        if(count($stock_fieldnames))
179                        {
180                                $t_fields = ',' . implode(',',$stock_fieldnames);
181                                if($t_fields == ',')
182                                {
183                                        unset($t_fields);
184                                }
185                        }
186
187                        $sri = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_contact_context'], 'uidnumber=' . (int)$id);
188                        $ldap_fields = ldap_get_entries($this->ldap, $sri);
189
190                        $return_fields[0]['id']     = $ldap_fields[0]['uidnumber'][0];
191                        $return_fields[0]['lid']    = $ldap_fields[0]['uid'][0];
192                        $return_fields[0]['tid']    = $ldap_fields[0]['phpgwcontacttypeid'][0];
193                        $return_fields[0]['owner']  = $ldap_fields[0]['phpgwcontactowner'][0];
194                        $return_fields[0]['access'] = $ldap_fields[0]['phpgwcontactaccess'][0];
195                        $return_fields[0]['cat_id'] = $ldap_fields[0]['phpgwcontactcatid'][0];
196                        $return_fields[0]['rights'] = (int)$this->grants[$return_fields[0]['owner']];
197                        if(@is_array($stock_fieldnames))
198                        {
199                                foreach($stock_fieldnames as $name => $value)
200                                {
201                                        $return_fields[0][$name] = utf8_decode($ldap_fields[0][$value][0]);
202                                }
203                        }
204
205                        /* Setup address type fields */
206                        if($return_fields[0]['adr_one_type'])
207                        {
208                                $one_type = $return_fields[0]['adr_one_type'];
209                                foreach($this->adr_types as $name => $val)
210                                {
211                                        eval("if(strstr(\$one_type,\$name)) { \$return_fields[0][\"one_\$name\"] = \"on\"; }");
212                                }
213                        }
214                        if($return_fields[0]['adr_two_type'])
215                        {
216                                $two_type = $return_fields[0]['adr_two_type'];
217                                foreach($this->adr_types as $name => $val)
218                                {
219                                        eval("if(strstr(\$two_type,\$name)) { \$return_fields[0][\"two_\$name\"] = \"on\"; }");
220                                }
221                        }
222
223                        $this->db->query("SELECT contact_name,contact_value FROM $this->ext_table WHERE contact_id='"
224                                . (int)$id . "'",__LINE__,__FILE__);
225                        while($this->db->next_record())
226                        {
227                                if($extra_fields[$this->db->f('contact_name')])
228                                {
229                                        $return_fields[0][$this->db->f('contact_name')] = $this->db->f('contact_value');
230                                }
231                        }
232                        return $return_fields;
233                }
234
235                function read_last_entry($fields = '')
236                {
237                        if(!$fields || empty($fields))
238                        {
239                                $fields = $this->stock_contact_fields;
240                        }
241                        list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
242
243                        if(count($stock_fieldnames))
244                        {
245                                $t_fields = ',' . implode(',',$stock_fieldnames);
246                                if($t_fields == ',')
247                                {
248                                        unset($t_fields);
249                                }
250                        }
251
252                        $id = $this->nextid;
253                        if($id == -1)
254                        {
255                                $id = 1;
256                        }
257
258                        $sri = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_contact_context'], 'uidnumber=' . (int)$id);
259                        $ldap_fields = ldap_get_entries($this->ldap, $sri);
260
261                        $return_fields[0]['id']     = $ldap_fields[0]['uidnumber'][0];
262                        $return_fields[0]['lid']    = $ldap_fields[0]['uid'][0];
263                        $return_fields[0]['tid']    = $ldap_fields[0]['phpgwcontacttypeid'][0];
264                        $return_fields[0]['owner']  = $ldap_fields[0]['phpgwcontactowner'][0];
265                        $return_fields[0]['access'] = $ldap_fields[0]['phpgwcontactaccess'][0];
266                        $return_fields[0]['cat_id'] = $ldap_fields[0]['phpgwcontactcatid'][0];
267                        $return_fields[0]['rights'] = (int)$this->grants[$return_fields[0]['owner']];
268
269                        if(@is_array($stock_fieldnames))
270                        {
271                                foreach($stock_fieldnames as $name => $value)
272                                {
273                                        $return_fields[0][$name] = utf8_decode($ldap_fields[0][$value][0]);
274                                }
275                        }
276
277                        /* Setup address type fields */
278                        if($return_fields[0]['adr_one_type'])
279                        {
280                                $one_type = $return_fields[0]['adr_one_type'];
281                                foreach($this->adr_types as $name => $val)
282                                {
283                                        eval("if(strstr(\$one_type,\$name)) { \$return_fields[0][\"one_\$name\"] = \"on\"; }");
284                                }
285                        }
286                        if($return_fields[0]['adr_two_type'])
287                        {
288                                $two_type = $return_fields[0]['adr_two_type'];
289                                foreach($this->adr_types as $name => $val)
290                                {
291                                        eval("if(strstr(\$two_type,\$name)) { \$return_fields[0][\"two_\$name\"] = \"on\"; }");
292                                }
293                        }
294
295                        $this->db->query("SELECT contact_name,contact_value FROM $this->ext_table WHERE contact_id='" . (int)$id . "'",__LINE__,__FILE__);
296                        while($this->db->next_record())
297                        {
298                                if($extra_fields[$this->db->f('contact_name')])
299                                {
300                                        $return_fields[0][$this->db->f('contact_name')] = $this->db->f('contact_value');
301                                }
302                        }
303                        return $return_fields;
304                }
305
306                /* send this the range, query, sort, order and whatever fields you want to see */
307                function read($start=0,$limit=0,$fields='',$query='',$filter='',$sort='',$order='', $lastmod=-1,$cquery='')
308                {
309                        if(!$start)  { $start  = 0; }
310                        if(!$limit)  { $limit  = 0; }
311                        if(!$filter) { $filter = 'tid=n'; }
312
313                        if(!$fields || empty($fields))
314                        {
315                                $fields = $this->stock_contact_fields;
316                        }
317                        $DEBUG = 0;
318
319                        list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
320
321                        $filterfields = array();
322                        /* turn filter's a=b,c=d OR a=b into an array */
323                        if($filter)
324                        {
325                                if($DEBUG) { echo 'DEBUG - Inbound filter is: #'.$filter.'#'; }
326                                $filterarray = split(',',$filter);
327                                if($filterarray[1])
328                                {
329                                        $i=0;
330                                        for($i=0;$i<count($filterarray);$i++)
331                                        {
332                                                list($name,$value) = split("=",$filterarray[$i]);
333                                                if($name)
334                                                {
335                                                        if($DEBUG) { echo '<br>DEBUG - Filter strings: #'.$this->non_contact_fields[$name].'# => #'.$value.'#'; }
336                                                        $filterfields[$this->non_contact_fields[$name]] = $value;
337                                                }
338                                        }
339                                }
340                                else
341                                {
342                                        list($name,$value) = split('=',$filter);
343                                        if($DEBUG)
344                                        {
345                                                echo '<br>DEBUG - Filter strings: #'.$this->non_contact_fields[$name].'# => #'.$value.'#';
346                                        }
347                                        $filterfields = array($this->non_contact_fields[$name] => $value);
348                                }
349                        }
350                        else
351                        {
352                                $filterfields += array('phpgwcontacttypeid' => 'n');
353                                if($DEBUG) { echo "<br>DEBUG - Filter strings: #phpgwcontacttypeid=n#"; }
354                        }
355
356                        /*
357                        need some way of using the lastmod arg in the filter like this:
358                        if($lastmod >= 0)
359                        {
360                                $filterfields += array('last_mod' => (int)$lastmod;
361                        }
362                        or maybe not like this - i am not sure what i am doing :)
363                        */
364
365                        if(@is_array($this->grants))
366                        {
367                                $filterfields['phpgwcontactowner'] = array();
368                                /* this was not listing private entries when show all was selected */
369                                /* $filterfields += array('phpgwcontactaccess' => 'public'); */
370                                if($DEBUG) { echo '<br>DEBUG - My user id is: ' . $this->account_id; }
371                                foreach($this->grants as $user => $right)
372                                {
373                                        if($DEBUG) { echo '<br>DEBUG - Grant from owner: ' . $user; }
374                                        $filterfields['phpgwcontactowner'][] = array('phpgwcontactowner' => $user);
375                                }
376                        }
377                        /*
378                        if($DEBUG)
379                        {
380                                while(list($name,$value) = each($filterfields))
381                                {
382                                        echo '<br>DEBUG - Filter strings: #' . $name . ',' . $value . '#';
383                                }
384                        }
385                        */
386
387                        $sort  = $sort  ? $sort  : 'ASC';
388                        $order = $order ? $order : 'n_family';
389
390                        if($DEBUG && $order)
391                        {
392                                echo "<br>DEBUG - ORDER by $order";
393                        }
394
395                        $ldap_fields = array();
396                        $myfilter = '';
397
398                        if($cquery)
399                        {
400                                $search_filter = array(
401                                        'fn'       => 'cn',
402                                        'n_family' => 'sn',
403                                        'org_name' => 'o'
404                                );
405                                $myfilter = $this->makefilter($filterfields,$search_filter,"$cquery*",$DEBUG);
406                        }
407                        elseif($query)
408                        {
409                                // the old code was searching about all fields
410                                // this was very slow
411                                #reset($this->stock_contact_fields);
412                                #$myfilter = $this->makefilter($filterfields,$this->stock_contact_fields,$query,$DEBUG);
413
414                                if(is_array($query))
415                                {
416                                        // must be fixed somehow Milosch????
417                                        $myfilter = $this->makefilter($filterfields,$query,'',$DEBUG);
418                                }
419                                else
420                                {
421                                        // don't search about any fields any more
422                                        $search_filter = array(
423                                                'fn'            => 'cn',
424                                                'n_given'       => 'givenname',
425                                                'n_family'      => 'sn',
426                                                'email'         => 'mail',
427                                                'org_name'      => 'o',
428                                                'org_unit'      => 'ou'
429                                        );
430                                        $myfilter = $this->makefilter($filterfields,$search_filter,$query,$DEBUG);
431                                }
432                        }
433                        else
434                        {
435                                $myfilter = $this->makefilter($filterfields,'','',$DEBUG);
436                        }
437
438                        $sri = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_contact_context'], $myfilter);
439
440                        $ldap_fields = ldap_get_entries($this->ldap, $sri);
441                        /* _debug_array($ldap_fields);exit; */
442
443                        $this->total_records = ldap_count_entries($this->ldap, $sri);
444                        /* echo '<br>total="'.$this->total_records.'"'; */
445                        if($DEBUG) { echo '<br>Query returned "'.$this->total_records.'" records.'; }
446
447                        /* Use shared sorting routines, based on sort and order */
448                        @set_time_limit(0); /* Try not to die, this can take some time on slow machines... */
449                        if($sort == 'ASC')
450                        {
451                                $ldap_fields = $this->asortbyindex($ldap_fields, $this->stock_contact_fields[$order]);
452                        }
453                        else
454                        {
455                                $ldap_fields = $this->arsortbyindex($ldap_fields, $this->stock_contact_fields[$order]);
456                        }
457
458                        /*
459                        This logic allows you to limit rows, or not.
460                        The export feature, for example, does not limit rows.
461                        This way, it can retrieve all rows at once.
462                        */
463                        if($start && $limit)
464                        {
465                                $limit = $start + $limit;
466                        }
467                        elseif($start && !$limit)
468                        {
469                                $limit = $start;
470                        }
471                        elseif(!$start && !$limit)
472                        {
473                                $limit = $this->total_records;
474                        }
475                        else
476                        {
477                                $start = 0;
478                                $limit = $limit;
479                        }
480                        /* echo '('.$start.','.$limit.')'; */
481
482                        @reset($ldap_fields);
483                        $j = 0;
484                        for($i=$start;$i<$limit;$i++)
485                        {
486                                if($i<$this->total_records && $ldap_fields[$i]['uid'][0])
487                                {
488                                        $return_fields[$j]['id']     = $ldap_fields[$i]['uidnumber'][0];
489                                        $return_fields[$j]['lid']    = $ldap_fields[$i]['uid'][0];
490                                        $return_fields[$j]['tid']    = $ldap_fields[$i]['phpgwcontacttypeid'][0];
491                                        $return_fields[$j]['owner']  = $ldap_fields[$i]['phpgwcontactowner'][0];
492                                        $return_fields[$j]['access'] = $ldap_fields[$i]['phpgwcontactaccess'][0];
493                                        $return_fields[$j]['cat_id'] = $ldap_fields[$i]['phpgwcontactcatid'][0];
494                                        $return_fields[$j]['rights'] = (int)$this->grants[$return_fields[$j]['owner']];
495
496                                        if(@is_array($stock_fieldnames))
497                                        {
498                                                foreach($stock_fieldnames as $f_name => $f_value)
499                                                {
500                                                        $return_fields[$j][$f_name] = utf8_decode($ldap_fields[$i][$f_value][0]);
501                                                }
502                                        }
503                                        $this->db->query("SELECT contact_name,contact_value FROM $this->ext_table WHERE contact_id='"
504                                                . (int)$ldap_fields[$i]['uidnumber'] . "'",__LINE__,__FILE__);
505                                        while($this->db->next_record())
506                                        {
507                                                if($extra_fields[$this->db->f('contact_name')])
508                                                {
509                                                        $return_fields[$j][$this->db->f('contact_name')] = $this->db->f('contact_value');
510                                                }
511                                        }
512                                        $j++;
513                                }
514                        }
515                        return $return_fields;
516                }
517
518                /* Used by read() above to build the ldap filter string */
519                function makefilter($qarray,$extra='',$query='', $DEBUG=False)
520                {
521                        if(!@is_array($qarray))
522                        {
523                                return $qarray;
524                        }
525
526                        $first = $last = "*";
527                        if(strstr($query,"*"))
528                        {
529                                if(substr($query,-1) == "*")
530                                {
531                                        $last = '';
532                                }
533                                if(substr($query,1) == "*")
534                                {
535                                        $first = '';
536                                }
537                        }
538
539                        if(@is_array($extra))
540                        {
541                                if($DEBUG) { echo '<br>Searching...'; }
542                                foreach($extra as $name => $value)
543                                {
544                                        $qarray[] = array($value => $query);
545                                }
546                        }
547                        elseif($extra)
548                        {
549                                $tmp = split('=',$extra);
550                                $qarray[] = array($tmp[0] => $tmp[1]);
551                        }
552
553                        @ksort($qarray);
554
555                        $aquery = '(&';
556                        $oquery = '(|';
557                        $hasor = False;
558
559                        foreach($qarray as $name => $value)
560                        {
561                                if(@is_array($value))
562                                {
563                                        foreach($value as $x => $y)
564                                        {
565                                                if($y == '*')
566                                                {
567                                                        $oquery .= '(' . $x . '=*)';
568                                                        $hasor = True;
569                                                }
570                                                elseif(@is_array($y))
571                                                {
572                                                        /* This was most likely created from acl grants in read() above */
573                                                        foreach($y as $a => $b)
574                                                        {
575                                                                $tmp .= '(' . $a . '=' . $b . ')';
576                                                        }
577                                                }
578                                                else
579                                                {
580                                                        $oquery .= '(' . $x . '=' . $first . $y . $last . ')';
581                                                        $hasor = True;
582                                                }
583                                        }
584                                }
585                                elseif($value == $query)
586                                {
587                                        /* searching */
588                                        $oquery .= '(' . $name . '=' . $first . $value . $last . ')';
589                                        $hasor = True;
590                                }
591                                else
592                                {
593                                        /* exact value (filtering based on tid, etc...) */
594                                        if($name == 'phpgwcontactcatid')
595                                        {
596                                                if (!is_object($GLOBALS['phpgw']->categories))
597                                                {
598                                                        $GLOBALS['phpgw']->categories = CreateObject('phpgwapi.categories');
599                                                }
600                                                $cats = $GLOBALS['phpgw']->categories->return_all_children((int)$value);
601
602                                                $aquery .= '(|';
603                                                foreach($cats as $cat)
604                                                {
605                                                        $aquery .= '(' . $name . '=*,' . $cat . ',*)(' . $name . '=' . $cat . ')';
606                                                }
607                                                $aquery .= ')';
608                                        }
609                                        else
610                                        {
611                                                $aquery .= '(' . $name . '=' . $value . ')';
612                                        }
613                                }
614
615                                if($tmp)
616                                {
617                                        if(strstr($tmp,')('))
618                                        {
619                                                $aquery .= '(|' . $tmp . ')';
620                                        }
621                                        else
622                                        {
623                                                $aquery .= $tmp;
624                                        }
625                                        unset($tmp);
626                                }
627                        }
628                        $aquery .= ')';
629                        $oquery .= ')';
630                        if(!$hasor)
631                        {
632                                $oquery = '';
633                                $fquery = $aquery;
634                        }
635                        else
636                        {
637                                $fquery = '(&' . $aquery . $oquery . ')';
638                        }
639
640                        if($DEBUG)
641                        {
642                                echo '<br>AND query:  "' . $aquery . '"';
643                                echo '<br>OR query:   "' . $oquery . '"';
644                                echo '<br>Full query: "' . $fquery . '"';
645                                echo '<br>Will search in "' . $GLOBALS['phpgw_info']['server']['ldap_contact_context'] . '"';
646                        }
647
648//                      echo $fquery;
649                        return $fquery;
650                }
651
652                function add($owner,$fields,$access=NULL,$cat_id=NULL,$tid=NULL)
653                {
654                        // access, cat_id and tid can be in $fields now or as extra params
655                        foreach(array('access','cat_id','tid') as $extra)
656                        {
657                                if(!is_null($$extra))
658                                {
659                                        $fields[$extra] = $$extra;
660                                }
661                        }
662                        if(empty($fields['tid']))
663                        {
664                                $fields['tid'] = 'n';
665                        }
666
667                        if(!$GLOBALS['phpgw_info']['server']['ldap_contact_context'])
668                        {
669                                return False;
670                        }
671
672                        list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
673
674                        $free = 0;
675                        $this->nextid = $GLOBALS['phpgw']->common->last_id('contacts');
676                        /* Loop until we find a free id */
677                        while(!$free)
678                        {
679                                $ldap_fields = '';
680                                $sri = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_contact_context'], 'uidnumber='.$this->nextid);
681                                $ldap_fields = ldap_get_entries($this->ldap, $sri);
682                                if($ldap_fields[0]['dn'][0])
683                                {
684                                        $this->nextid = $GLOBALS['phpgw']->common->next_id('contacts');
685                                }
686                                else
687                                {
688                                        $free = True;
689                                }
690                        }
691
692                        $ldap_fields = '';
693                        if(@is_array($stock_fieldnames))
694                        {
695                                foreach($stock_fieldnames as $name => $value)
696                                {
697                                        if($stock_fields[$name] != '')
698                                        {
699                                                $ldap_fields[$value] = utf8_encode($stock_fields[$name]);
700                                        }
701                                }
702                        }
703
704                        $time = gettimeofday();
705                        $ldap_fields['uid'] = time().$time['usec'].':'.$ldap_fields['givenname'];
706
707                        $dn = 'uid=' . $ldap_fields['uid'].',' . $GLOBALS['phpgw_info']['server']['ldap_contact_context'];
708                        $ldap_fields['phpgwcontacttypeid']    = $fields['tid'];
709                        $ldap_fields['phpgwcontactowner']     = $owner;
710                        if(!isset($fields['access']))
711                        {
712                                $fields['access'] = 'private';
713                        }
714                        $ldap_fields['phpgwcontactaccess'] = $fields['access'];
715                        $ldap_fields['phpgwcontactcatid']  = $fields['cat_id'] ? $fields['cat_id'] : '0';
716                        $ldap_fields['uidnumber']      = $this->nextid;
717                        /* $ldap_fields['objectclass'][0] = 'person'; */
718                        $ldap_fields['objectclass'][0] = 'organizationalPerson';
719                        $ldap_fields['objectclass'][1] = 'inetOrgPerson';
720                        $ldap_fields['objectclass'][2] = 'phpgwContact';
721                        //$ldap_fields['last_mod'] = $GLOBALS['phpgw']->datetime->gmtnow;
722
723                        $err = $this->validate($ldap_fields);
724                        if(@is_array($err) && @isset($err[0]))
725                        {
726                                return $err;
727                        }
728                        // _debug_array($ldap_fields); exit;
729                        $err = ldap_add($this->ldap, $dn, $ldap_fields);
730                        if(!$err)
731                        {
732                                return False;
733                        }
734
735                        if(count($extra_fields))
736                        {
737                                foreach($extra_fields as $name => $value)
738                                {
739                                        $this->db->query("INSERT INTO $this->ext_table VALUES ('".$this->nextid."','" . $this->account_id . "','"
740                                                . addslashes($name) . "','" . addslashes($value) . "')",__LINE__,__FILE__);
741                                }
742                        }
743                        return $this->nextid;
744                }
745
746                /* LDAP syntaxes require some testing prior to add */
747                function validate(&$entry)
748                {
749                        $errors = array();
750                        foreach($entry as $field => $value)
751                        {
752                                if(strstr($field,'phone'))
753                                {
754                                        /* Regex for testing valid international phone number entries.
755                                         * LDAP may reject bad values here, such as an email address in a phone number.
756                                         * This format is somewhat loose, allowing for optional parenthesis, + sign,
757                                         * and 0-7 numbers between separators.
758                                         */
759                                        $regex = "/^[-0-9\+\(\)\/]/";
760                                        if(!preg_match($regex,$value))
761                                        {
762                                                $errors[] = array($field => $value);
763                                        }
764                                }
765                                elseif(strstr($field,'mailtype') || strstr($field,'mailhometype'))
766                                {
767                                        /* Check for valid mail type */
768                                        if(!@isset($this->email_types[$value]))
769                                        {
770                                                $errors[] = array($field => $value);
771                                        }
772                                }
773                                elseif(strstr($field,'mail'))
774                                {
775                                        /* Check for valid email address - TODO - should depend on mail type */
776                                        $regex = "/[ |\t|\r|\n]*\"?([^\"]+\"?@[^ <>\t]+\.[^ <>\t][^ <>\t]+)[ |\t|\r|\n]*/x";
777                                        if(!preg_match($regex,$value))
778                                        {
779                                                $errors[] = array($field => $value);
780                                        }
781                                }
782                        }
783                        /* Verify sn/cn attrs set */
784                        if(empty($entry['sn']) && !empty($entry['cn']))
785                        {
786                                $entry['sn'] = $entry['cn'];
787                        }
788                        if(empty($entry['cn']) && !empty($entry['sn']))
789                        {
790                                $entry['cn'] = $entry['sn'];
791                        }
792                        $entry['cn'] = $entry['cn'] ? $entry['cn'] : '-';
793                        $entry['sn'] = $entry['sn'] ? $entry['sn'] : '-';
794
795                        return $errors;
796                }
797
798                function field_exists($id,$field_name)
799                {
800                        $this->db->query("SELECT COUNT(*) FROM $this->ext_table where contact_id='" . (int)$id . "' AND contact_name='"
801                        . addslashes($field_name) . "'",__LINE__,__FILE__);
802                        $this->db->next_record();
803                        return $this->db->f(0);
804                }
805
806                function add_single_extra_field($id,$owner,$field_name,$field_value)
807                {
808                        $this->db->query("INSERT INTO $this->ext_table VALUES (" . (int)$id . ",'$owner','" . addslashes($field_name)
809                                . "','" . addslashes($field_value) . "')",__LINE__,__FILE__);
810                }
811
812                function delete_single_extra_field($id,$field_name)
813                {
814                        $this->db->query("DELETE FROM $this->ext_table WHERE contact_id='" . (int)$id . "' AND contact_name='"
815                                . addslashes($field_name) . "'",__LINE__,__FILE__);
816                }
817
818                function update($id,$owner,$fields,$access=NULL,$cat_id=NULL,$tid=NULL)
819                {
820                        // access, cat_id and tid can be in $fields now or as extra params
821                        foreach(array('access','cat_id','tid') as $extra)
822                        {
823                                if(!is_null($$extra))
824                                {
825                                        $fields[$extra] = $$extra;
826                                }
827                                if(isset($fields[$extra]))
828                                {
829                                        $stock_fields[$extra] = $fields[$extra];
830                                }
831                        }
832                        $nonfields = $this->non_contact_fields;
833
834                        if(!$GLOBALS['phpgw_info']['server']['ldap_contact_context'])
835                        {
836                                return False;
837                        }
838
839                        /* First make sure that id number exists */
840                        $sri = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_contact_context'], 'uidnumber=' . (int)$id);
841                        $ldap_fields = ldap_get_entries($this->ldap, $sri);
842
843                        if($ldap_fields[0]['dn'])
844                        {
845                                $dn = $ldap_fields[0]['dn'];
846                                list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
847                                if(@is_array($stock_fieldnames))
848                                {
849                                        /*
850                                        Check each value, add our extra attributes if they are missing, and
851                                        otherwise fix the entry while we can.
852                                        */
853                                        /* Verify uidnumber */
854                                        $stock_fields['id']   = $id;
855                                        if(empty($ldap_fields[0]['uidnumber']))
856                                        {
857                                                $err = ldap_modify($this->ldap,$dn,array('uidnumber'  => $stock_fields['uidnumber']));
858                                        }
859                                        elseif(!$ldap_fields[0]['uidnumber'])
860                                        {
861                                                $err = ldap_mod_add($this->ldap,$dn,array('uidnumber' => $stock_fields['uidnumber']));
862                                        }
863
864                                        /* Verify uid */
865                                        $uids = split(',',$dn);
866                                        $stock_fields['lid'] = $uids[0];
867                                        if(empty($ldap_fields[0]['uid']))
868                                        {
869                                                $err = ldap_modify($this->ldap,$dn,array('uid'  => $stock_fields['lid']));
870                                        }
871                                        elseif(!$ldap_fields[0]['uid'])
872                                        {
873                                                $err = ldap_mod_add($this->ldap,$dn,array('uid' => $stock_fields['lid']));
874                                        }
875
876                                        /* Verify objectclasses are there */
877                                        if(empty($ldap_fields[0]['objectclass']))
878                                        {
879                                                /* $stock_fields['objectclass'][0] = 'person'; */
880                                                $stock_fields['objectclass'][0] = 'organizationalPerson';
881                                                $stock_fields['objectclass'][1] = 'inetOrgPerson';
882                                                $stock_fields['objectclass'][2] = 'phpgwContact';
883                                                $err = ldap_modify($this->ldap,$dn,array('objectclass'  => $stock_fields['objectclass']));
884                                        }
885                                        elseif(!$ldap_fields[0]['objectclass'])
886                                        {
887                                                /* $stock_fields['objectclass'][0] = 'person'; */
888                                                $stock_fields['objectclass'][0] = 'organizationalPerson';
889                                                $stock_fields['objectclass'][1] = 'inetOrgPerson';
890                                                $stock_fields['objectclass'][2] = 'phpgwContact';
891                                                $err = ldap_mod_add($this->ldap,$dn,array('objectclass'  => $stock_fields['objectclass']));
892                                        }
893
894                                        /* Verify owner */
895                                        $stock_fields['owner']  = $owner;
896                                        if(empty($ldap_fields[0]['phpgwcontactowner']))
897                                        {
898                                                $err = ldap_modify($this->ldap,$dn,array('phpgwcontactowner'  => $stock_fields['owner']));
899                                        }
900                                        elseif(!$ldap_fields[0]['phpgwcontactowner'])
901                                        {
902                                                $err = ldap_mod_add($this->ldap,$dn,array('phpgwcontactowner' => $stock_fields['owner']));
903                                        }
904
905                                        /* Verify access */
906                                        $stock_fields['access'] = $fields['access'];
907                                        if(empty($ldap_fields[0]['phpgwcontactaccess']))
908                                        {
909                                                $err = ldap_modify($this->ldap,$dn,array('phpgwcontactaccess'  => $stock_fields['access']));
910                                        }
911                                        elseif(!$ldap_fields[0]['phpgwcontactaccess'])
912                                        {
913                                                $err = ldap_mod_add($this->ldap,$dn,array('phpgwcontactaccess' => $stock_fields['access']));
914                                        }
915
916                                        /* Verify cat_id */
917                                        $stock_fields['cat_id']  = $fields['cat_id'] ? $fields['cat_id'] : ' ';
918                                        if(empty($ldap_fields[0]['phpgwcontactcatid']))
919                                        {
920                                                $err = ldap_modify($this->ldap,$dn,array('phpgwcontactcatid'  => $stock_fields['cat_id']));
921                                        }
922                                        elseif(!$ldap_fields[0]['phpgwcontactcatid'])
923                                        {
924                                                $err = ldap_mod_add($this->ldap,$dn,array('phpgwcontactcatid' => $stock_fields['cat_id']));
925                                        }
926
927                                        /* Verify tid */
928                                        $stock_fields['tid'] = $fields['tid'];
929                                        if(empty($ldap_fields[0]['phpgwcontacttypeid']))
930                                        {
931                                                $err = ldap_modify($this->ldap,$dn,array('phpgwcontacttypeid'  => $stock_fields['tid']));
932                                        }
933                                        elseif(!$ldap_fields[0]['phpgwcontacttypeid'])
934                                        {
935                                                $err = ldap_mod_add($this->ldap,$dn,array('phpgwcontacttypeid' => $stock_fields['tid']));
936                                        }
937
938                                        /* OK, just mod the data already */
939                                        $allfields = $stock_fieldnames + $nonfields;
940                                        /* Don't try to modify the uid, since this affects the dn */
941                                        unset($allfields['lid']);
942                                        foreach($allfields as $fname => $fvalue)
943                                        {
944                                                if($ldap_fields[0][$fvalue] && $stock_fields[$fname] && $ldap_fields[0][$fvalue][0] != $stock_fields[$fname] )
945                                                {
946                                                        //echo "<br>".$fname." => ".$fvalue." was there";
947                                                        $err = ldap_modify($this->ldap,$dn,array($fvalue => utf8_encode($stock_fields[$fname])));
948                                                }
949                                                elseif(!$ldap_fields[0][$fvalue] && $stock_fields[$fname])
950                                                {
951                                                        //echo "<br>".$fname." not there - '".$fvalue."'";
952                                                        $err = ldap_mod_add($this->ldap,$dn,array($fvalue => utf8_encode($stock_fields[$fname])));
953                                                }
954                                                elseif($ldap_fields[0][$fvalue] && !$stock_fields[$fname])
955                                                {
956                                                        //echo "<br>".$fname." gone...  deleting - '".$fvalue."'";
957                                                        /*
958                                                        NOTE: we use the ldap_fields because we need to send the
959                                                        _ORIGINAL_ contents as the value. see:
960                                                        http://www.php.net/manual/en/function.ldap-mod-del.php
961                                                        */
962                                                        $err = ldap_mod_del($this->ldap,$dn,array($fvalue => $ldap_fields[0][$fvalue][0]));
963                                                }
964                                                /* Else we have nothing to do. */
965                                        }
966                                }
967
968                                //something here to update the last_mod from $GLOBALS['phpgw']->datetime->gmtnow
969
970                                foreach($extra_fields as $x_name => $x_value)
971                                {
972                                        if($this->field_exists($id,$x_name))
973                                        {
974                                                if(!$x_value)
975                                                {
976                                                        $this->delete_single_extra_field($id,$x_name);
977                                                }
978                                                else
979                                                {
980                                                        $this->db->query("UPDATE $this->ext_table SET contact_value='" . addslashes($x_value)
981                                                                . "',contact_owner='$owner' WHERE contact_name='" . addslashes($x_name)
982                                                                . "' AND contact_id='" . (int)$id . "'",__LINE__,__FILE__);
983                                                }
984                                        }
985                                        else
986                                        {
987                                                $this->add_single_extra_field($id,$owner,$x_name,$x_value);
988                                        }
989                                }
990                        }
991                        else
992                        {
993                                return False;
994                        }
995                }
996
997                /* Used by admin to change ownership on account delete */
998                function change_owner($old_owner='',$new_owner='')
999                {
1000                        if(!($new_owner && $old_owner))
1001                        {
1002                                return False;
1003                        }
1004
1005                        $sri = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_contact_context'], 'phpgwcontactowner='.$old_owner);
1006                        $ldap_fields = ldap_get_entries($this->ldap, $sri);
1007
1008                        $entry = '';
1009                        foreach($ldap_fields as $nul => $entry)
1010                        {
1011                                $err = ldap_modify($this->ldap,$entry['dn'],array('phpgwcontactowner' => $new_owner));
1012                        }
1013
1014                        $this->db->query("UPDATE $this->ext_table SET contact_owner='$new_owner' WHERE contact_owner=$owner",__LINE__,__FILE__);
1015                        return;
1016                }
1017
1018                /* This is where the real work of delete() is done, shared class file contains calling function */
1019                function delete_($id)
1020                {
1021                        if(!$GLOBALS['phpgw_info']['server']['ldap_contact_context'])
1022                        {
1023                                return False;
1024                        }
1025
1026                        $sri = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_contact_context'], 'uidnumber='.$id);
1027                        $ldap_fields = ldap_get_entries($this->ldap, $sri);
1028
1029                        if($ldap_fields[0]['dn'])
1030                        {
1031                                $err = ldap_delete($this->ldap,$ldap_fields[0]['dn']);
1032
1033                                $this->db->query("DELETE FROM $this->ext_table WHERE contact_id='" . (int)$id . "' AND contact_owner='"
1034                                        . $this->account_id . "'",__LINE__,__FILE__);
1035                        }
1036                        else
1037                        {
1038                                return False;
1039                        }
1040                }
1041
1042                // This is for the admin script deleteaccount.php
1043                function delete_all($owner=0)
1044                {
1045                        if(!$GLOBALS['phpgw_info']['server']['ldap_contact_context'])
1046                        {
1047                                return False;
1048                        }
1049
1050                        if($owner)
1051                        {
1052                                $sri = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_contact_context'], 'phpgwcontactowner='.$owner);
1053                                $ldap_fields = ldap_get_entries($this->ldap, $sri);
1054
1055                                $entry = '';
1056                                foreach($ldap_fields as $nul => $entry)
1057                                {
1058                                        $err = ldap_delete($this->ldap,$entry['dn']);
1059                                }
1060
1061                                $this->db->query("DELETE FROM $this->ext_table WHERE contact_owner=$owner",__LINE__,__FILE__);
1062                        }
1063                        return;
1064                }
1065        }
1066?>
Note: See TracBrowser for help on using the repository browser.