2001-07-05 09:37:36 +02:00
|
|
|
<?php
|
|
|
|
/**************************************************************************\
|
2004-05-05 14:06:13 +02:00
|
|
|
* eGroupWare API - Accounts manager for LDAP *
|
|
|
|
* This file written by Miles Lott <milosch@groupwhere.org> *
|
2001-07-05 09:37:36 +02:00
|
|
|
* View and manipulate contact records using LDAP *
|
2004-02-09 21:18:21 +01:00
|
|
|
* ------------------------------------------------------------------------ *
|
2004-05-05 14:06:13 +02:00
|
|
|
* This library is part of the eGroupWare API *
|
|
|
|
* http://www.egroupware.org/api *
|
2001-07-05 09:37:36 +02:00
|
|
|
* ------------------------------------------------------------------------ *
|
|
|
|
* This library is free software; you can redistribute it and/or modify it *
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2.1 of the License, *
|
|
|
|
* or any later version. *
|
|
|
|
* This library is distributed in the hope that it will be useful, but *
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
|
|
* See the GNU Lesser General Public License for more details. *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this library; if not, write to the Free Software Foundation, *
|
|
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
|
|
|
|
\**************************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@class contacts
|
|
|
|
@abstract Contact List System
|
|
|
|
@discussion Author: jengo/Milosch <br>
|
|
|
|
This class provides a contact database scheme. <br>
|
|
|
|
It attempts to be based on the vcard 2.1 standard, with mods as needed to make for more reasonable sql storage. <br>
|
|
|
|
The LDAP schema used here may require installation of schema files available in the phpgwapi/doc/ldap dir.
|
|
|
|
Please see the README file there.
|
|
|
|
Syntax: CreateObject('phpgwapi.contacts'); <br>
|
|
|
|
Example1: $contacts = CreateObject('phpgwapi.contacts');
|
|
|
|
*/
|
|
|
|
class contacts_
|
|
|
|
{
|
2001-11-23 02:58:19 +01:00
|
|
|
var $db = '';
|
|
|
|
var $ldap = '';
|
|
|
|
var $nextid = '';
|
|
|
|
var $std_table = '';
|
2005-11-13 13:05:35 +01:00
|
|
|
var $ext_table = 'egw_addressbook_extra';
|
2001-07-05 09:37:36 +02:00
|
|
|
|
|
|
|
var $account_id;
|
2001-11-23 02:58:19 +01:00
|
|
|
var $adr_types;
|
2001-07-05 09:37:36 +02:00
|
|
|
var $total_records;
|
|
|
|
var $grants;
|
|
|
|
|
2001-11-23 02:58:19 +01:00
|
|
|
/* The left side are the array elements used throughout phpgw, right side are the ldap attributes */
|
|
|
|
var $stock_contact_fields = array(
|
|
|
|
'fn' => 'cn',
|
|
|
|
'n_given' => 'givenname',
|
|
|
|
'n_family' => 'sn',
|
|
|
|
'n_middle' => 'phpgwmiddlename',
|
|
|
|
'n_prefix' => 'phpgwprefix',
|
|
|
|
'n_suffix' => 'phpgwsuffix',
|
|
|
|
'sound' => 'phpgwaudio',
|
|
|
|
'bday' => 'phpgwbirthday',
|
|
|
|
'note' => 'description',
|
|
|
|
'tz' => 'phpgwtz',
|
|
|
|
'geo' => 'phpgwgeo',
|
|
|
|
'url' => 'phpgwurl',
|
|
|
|
'pubkey' => 'phpgwpublickey',
|
|
|
|
|
|
|
|
'org_name' => 'o',
|
|
|
|
'org_unit' => 'ou',
|
|
|
|
'title' => 'title',
|
|
|
|
|
|
|
|
'adr_one_street' => 'street',
|
2004-02-09 21:18:21 +01:00
|
|
|
'adr_one_locality' => 'l',
|
|
|
|
'adr_one_region' => 'st',
|
2001-11-23 02:58:19 +01:00
|
|
|
'adr_one_postalcode' => 'postalcode',
|
|
|
|
'adr_one_countryname' => 'co',
|
|
|
|
'adr_one_type' => 'phpgwadronetype',
|
|
|
|
'label' => 'phpgwaddresslabel',
|
|
|
|
|
|
|
|
'adr_two_street' => 'phpgwadrtwostreet',
|
2004-02-09 21:18:21 +01:00
|
|
|
'adr_two_locality' => 'phpgwadrtwolocality',
|
|
|
|
'adr_two_region' => 'phpgwadrtworegion',
|
2001-11-23 02:58:19 +01:00
|
|
|
'adr_two_postalcode' => 'phpgwadrtwopostalcode',
|
|
|
|
'adr_two_countryname' => 'phpgwadrtwocountryname',
|
|
|
|
'adr_two_type' => 'phpgwadrtwotype',
|
|
|
|
|
|
|
|
'tel_work' => 'telephonenumber',
|
|
|
|
'tel_home' => 'homephone',
|
|
|
|
'tel_voice' => 'phpgwvoicetelephonenumber',
|
2004-02-09 21:18:21 +01:00
|
|
|
'tel_fax' => 'facsimiletelephonenumber',
|
2001-11-23 02:58:19 +01:00
|
|
|
'tel_msg' => 'phpgwmsgtelephonenumber',
|
|
|
|
'tel_cell' => 'phpgwcelltelephonenumber',
|
|
|
|
'tel_pager' => 'phpgwpagertelephonenumber',
|
|
|
|
'tel_bbs' => 'phpgwbbstelephonenumber',
|
|
|
|
'tel_modem' => 'phpgwmodemtelephonenumber',
|
|
|
|
'tel_car' => 'phpgwmobiletelephonenumber',
|
|
|
|
'tel_isdn' => 'phpgwisdnphonenumber',
|
|
|
|
'tel_video' => 'phpgwvideophonenumber',
|
|
|
|
'tel_prefer' => 'phpgwpreferphone',
|
|
|
|
'email' => 'mail',
|
|
|
|
'email_type' => 'phpgwmailtype',
|
|
|
|
'email_home' => 'phpgwmailhome',
|
|
|
|
'email_home_type' => 'phpgwmailhometype'
|
|
|
|
);
|
|
|
|
|
|
|
|
var $non_contact_fields = array(
|
|
|
|
'id' => 'uidnumber',
|
|
|
|
'lid' => 'uid',
|
|
|
|
'tid' => 'phpgwcontacttypeid',
|
|
|
|
'cat_id' => 'phpgwcontactcatid',
|
|
|
|
'access' => 'phpgwcontactaccess',
|
|
|
|
'owner' => 'phpgwcontactowner'
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Used to set preferphone field */
|
|
|
|
var $tel_types = array(
|
|
|
|
'work' => 'work',
|
|
|
|
'home' => 'home',
|
|
|
|
'voice' => 'voice',
|
|
|
|
'fax' => 'fax',
|
|
|
|
'msg' => 'msg',
|
|
|
|
'cell' => 'cell',
|
|
|
|
'pager' => 'pager',
|
|
|
|
'bbs' => 'bbs',
|
|
|
|
'modem' => 'modem',
|
|
|
|
'car' => 'car',
|
|
|
|
'isdn' => 'isdn',
|
|
|
|
'video' => 'video'
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Used to set mail_type fields */
|
|
|
|
var $email_types = array(
|
|
|
|
'INTERNET' => 'INTERNET',
|
|
|
|
'CompuServe' => 'CompuServe',
|
|
|
|
'AOL' => 'AOL',
|
|
|
|
'Prodigy' => 'Prodigy',
|
|
|
|
'eWorld' => 'eWorld',
|
|
|
|
'AppleLink' => 'AppleLink',
|
|
|
|
'AppleTalk' => 'AppleTalk',
|
|
|
|
'PowerShare' => 'PowerShare',
|
|
|
|
'IBMMail' => 'IBMMail',
|
|
|
|
'ATTMail' => 'ATTMail',
|
|
|
|
'MCIMail' => 'MCIMail',
|
|
|
|
'X.400' => 'X.400',
|
|
|
|
'TLX' => 'TLX'
|
|
|
|
);
|
|
|
|
|
2001-07-05 09:37:36 +02:00
|
|
|
function contacts_()
|
|
|
|
{
|
2005-08-27 14:24:56 +02:00
|
|
|
$this->db = clone($GLOBALS['egw']->db);
|
|
|
|
$this->ldap = $GLOBALS['egw']->common->ldapConnect(
|
|
|
|
$GLOBALS['egw_info']['server']['ldap_contact_host'],
|
|
|
|
$GLOBALS['egw_info']['server']['ldap_contact_dn'],
|
|
|
|
$GLOBALS['egw_info']['server']['ldap_contact_pw']
|
2001-07-05 09:37:36 +02:00
|
|
|
);
|
2005-08-27 14:24:56 +02:00
|
|
|
$this->account_id = $GLOBALS['egw_info']['user']['account_id'];
|
|
|
|
$this->grants = $GLOBALS['egw']->acl->get_grants('addressbook');
|
2001-07-05 09:37:36 +02:00
|
|
|
|
|
|
|
/* Used to flag an address as being:
|
|
|
|
domestic OR international(default)
|
|
|
|
parcel(default)
|
|
|
|
postal(default)
|
|
|
|
work(default) OR home
|
|
|
|
*/
|
|
|
|
$this->adr_types = array(
|
|
|
|
'dom' => lang('Domestic'),
|
|
|
|
'intl' => lang('International'),
|
|
|
|
'parcel' => lang('Parcel'),
|
|
|
|
'postal' => lang('Postal')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send this the id and whatever fields you want to see */
|
2004-01-24 16:57:57 +01:00
|
|
|
function read_single_entry($id,$fields='')
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-01-24 16:57:57 +01:00
|
|
|
if(!$fields || empty($fields))
|
|
|
|
{
|
|
|
|
$fields = $this->stock_contact_fields;
|
|
|
|
}
|
|
|
|
list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
|
2001-07-05 09:37:36 +02:00
|
|
|
|
2004-01-24 16:57:57 +01:00
|
|
|
if(count($stock_fieldnames))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-01-24 16:57:57 +01:00
|
|
|
$t_fields = ',' . implode(',',$stock_fieldnames);
|
|
|
|
if($t_fields == ',')
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
unset($t_fields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-27 14:24:56 +02:00
|
|
|
$sri = ldap_search($this->ldap, $GLOBALS['egw_info']['server']['ldap_contact_context'], 'uidnumber=' . (int)$id);
|
2001-07-05 09:37:36 +02:00
|
|
|
$ldap_fields = ldap_get_entries($this->ldap, $sri);
|
|
|
|
|
|
|
|
$return_fields[0]['id'] = $ldap_fields[0]['uidnumber'][0];
|
|
|
|
$return_fields[0]['lid'] = $ldap_fields[0]['uid'][0];
|
|
|
|
$return_fields[0]['tid'] = $ldap_fields[0]['phpgwcontacttypeid'][0];
|
|
|
|
$return_fields[0]['owner'] = $ldap_fields[0]['phpgwcontactowner'][0];
|
|
|
|
$return_fields[0]['access'] = $ldap_fields[0]['phpgwcontactaccess'][0];
|
|
|
|
$return_fields[0]['cat_id'] = $ldap_fields[0]['phpgwcontactcatid'][0];
|
2004-01-24 16:57:57 +01:00
|
|
|
$return_fields[0]['rights'] = (int)$this->grants[$return_fields[0]['owner']];
|
2003-12-10 12:36:54 +01:00
|
|
|
if(@is_array($stock_fieldnames))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-01-24 16:57:57 +01:00
|
|
|
foreach($stock_fieldnames as $name => $value)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2005-08-27 14:24:56 +02:00
|
|
|
$return_fields[0][$name] = $GLOBALS['egw']->translation->convert(($ldap_fields[0][$value][0]),'utf-8');
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup address type fields */
|
2004-02-16 05:50:26 +01:00
|
|
|
if($return_fields[0]['adr_one_type'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$one_type = $return_fields[0]['adr_one_type'];
|
2004-01-24 16:57:57 +01:00
|
|
|
foreach($this->adr_types as $name => $val)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2005-08-15 00:11:15 +02:00
|
|
|
if(strstr($one_type,$name))
|
|
|
|
{
|
|
|
|
$return_fields[0]['one_'.$name] = 'on';
|
|
|
|
}
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
2004-01-24 16:57:57 +01:00
|
|
|
if($return_fields[0]['adr_two_type'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$two_type = $return_fields[0]['adr_two_type'];
|
2004-01-24 16:57:57 +01:00
|
|
|
foreach($this->adr_types as $name => $val)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2005-08-15 00:11:15 +02:00
|
|
|
if (strstr($two_type,$name))
|
|
|
|
{
|
|
|
|
$return_fields[0]['two_'.$name] = 'on';
|
|
|
|
}
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-09 21:18:21 +01:00
|
|
|
$this->db->query("SELECT contact_name,contact_value FROM $this->ext_table WHERE contact_id='"
|
|
|
|
. (int)$id . "'",__LINE__,__FILE__);
|
2004-01-24 16:57:57 +01:00
|
|
|
while($this->db->next_record())
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-01-24 16:57:57 +01:00
|
|
|
if($extra_fields[$this->db->f('contact_name')])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$return_fields[0][$this->db->f('contact_name')] = $this->db->f('contact_value');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return_fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
function read_last_entry($fields = '')
|
|
|
|
{
|
2004-02-16 05:50:26 +01:00
|
|
|
if(!$fields || empty($fields))
|
2004-01-24 16:57:57 +01:00
|
|
|
{
|
|
|
|
$fields = $this->stock_contact_fields;
|
|
|
|
}
|
|
|
|
list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
|
2001-07-05 09:37:36 +02:00
|
|
|
|
2004-02-16 05:50:26 +01:00
|
|
|
if(count($stock_fieldnames))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-02-16 05:50:26 +01:00
|
|
|
$t_fields = ',' . implode(',',$stock_fieldnames);
|
|
|
|
if($t_fields == ',')
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
unset($t_fields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = $this->nextid;
|
2004-02-09 21:18:21 +01:00
|
|
|
if($id == -1)
|
|
|
|
{
|
|
|
|
$id = 1;
|
|
|
|
}
|
2001-07-05 09:37:36 +02:00
|
|
|
|
2005-08-27 14:24:56 +02:00
|
|
|
$sri = ldap_search($this->ldap, $GLOBALS['egw_info']['server']['ldap_contact_context'], 'uidnumber=' . (int)$id);
|
2001-07-05 09:37:36 +02:00
|
|
|
$ldap_fields = ldap_get_entries($this->ldap, $sri);
|
|
|
|
|
|
|
|
$return_fields[0]['id'] = $ldap_fields[0]['uidnumber'][0];
|
|
|
|
$return_fields[0]['lid'] = $ldap_fields[0]['uid'][0];
|
|
|
|
$return_fields[0]['tid'] = $ldap_fields[0]['phpgwcontacttypeid'][0];
|
|
|
|
$return_fields[0]['owner'] = $ldap_fields[0]['phpgwcontactowner'][0];
|
|
|
|
$return_fields[0]['access'] = $ldap_fields[0]['phpgwcontactaccess'][0];
|
|
|
|
$return_fields[0]['cat_id'] = $ldap_fields[0]['phpgwcontactcatid'][0];
|
2004-01-24 16:57:57 +01:00
|
|
|
$return_fields[0]['rights'] = (int)$this->grants[$return_fields[0]['owner']];
|
2001-07-05 09:37:36 +02:00
|
|
|
|
2003-12-10 12:36:54 +01:00
|
|
|
if(@is_array($stock_fieldnames))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-01-24 16:57:57 +01:00
|
|
|
foreach($stock_fieldnames as $name => $value)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2005-08-27 14:24:56 +02:00
|
|
|
$return_fields[0][$name] = $GLOBALS['egw']->translation->convert(($ldap_fields[0][$value][0]),'utf-8');
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup address type fields */
|
2004-01-24 16:57:57 +01:00
|
|
|
if($return_fields[0]['adr_one_type'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$one_type = $return_fields[0]['adr_one_type'];
|
2004-01-24 16:57:57 +01:00
|
|
|
foreach($this->adr_types as $name => $val)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2005-08-15 00:11:15 +02:00
|
|
|
if(strstr($one_type,$name))
|
|
|
|
{
|
|
|
|
$return_fields[0]['one_'.$name] = 'on';
|
|
|
|
}
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
2004-01-24 16:57:57 +01:00
|
|
|
if($return_fields[0]['adr_two_type'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$two_type = $return_fields[0]['adr_two_type'];
|
2004-01-24 16:57:57 +01:00
|
|
|
foreach($this->adr_types as $name => $val)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2005-08-15 00:11:15 +02:00
|
|
|
if (strstr($two_type,$name))
|
|
|
|
{
|
|
|
|
$return_fields[0]['two_'.$name] = 'on';
|
|
|
|
}
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-09 21:18:21 +01:00
|
|
|
$this->db->query("SELECT contact_name,contact_value FROM $this->ext_table WHERE contact_id='" . (int)$id . "'",__LINE__,__FILE__);
|
2004-01-24 16:57:57 +01:00
|
|
|
while($this->db->next_record())
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-01-24 16:57:57 +01:00
|
|
|
if($extra_fields[$this->db->f('contact_name')])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$return_fields[0][$this->db->f('contact_name')] = $this->db->f('contact_value');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return_fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send this the range, query, sort, order and whatever fields you want to see */
|
2004-01-17 04:21:21 +01:00
|
|
|
function read($start=0,$limit=0,$fields='',$query='',$filter='',$sort='',$order='', $lastmod=-1,$cquery='')
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2003-08-28 16:31:11 +02:00
|
|
|
if(!$start) { $start = 0; }
|
|
|
|
if(!$limit) { $limit = 0; }
|
|
|
|
if(!$filter) { $filter = 'tid=n'; }
|
2001-07-05 09:37:36 +02:00
|
|
|
|
2004-01-24 16:57:57 +01:00
|
|
|
if(!$fields || empty($fields))
|
|
|
|
{
|
|
|
|
$fields = $this->stock_contact_fields;
|
|
|
|
}
|
2001-07-05 09:37:36 +02:00
|
|
|
$DEBUG = 0;
|
|
|
|
|
|
|
|
list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
|
|
|
|
|
|
|
|
$filterfields = array();
|
|
|
|
/* turn filter's a=b,c=d OR a=b into an array */
|
2004-02-16 05:50:26 +01:00
|
|
|
if($filter)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-02-16 05:50:26 +01:00
|
|
|
if($DEBUG) { echo 'DEBUG - Inbound filter is: #'.$filter.'#'; }
|
2006-01-06 03:47:45 +01:00
|
|
|
|
2005-11-03 15:30:40 +01:00
|
|
|
foreach(explode(',',$filter) as $f)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2005-11-03 15:30:40 +01:00
|
|
|
list($name,$value) = explode('=',$f,2);
|
2006-01-06 03:47:45 +01:00
|
|
|
|
2005-11-03 15:30:40 +01:00
|
|
|
if ($this->stock_contact_fields[$name])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2005-11-03 15:30:40 +01:00
|
|
|
$filterfields[$this->stock_contact_fields[$name]] = $value;
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
2005-11-03 15:30:40 +01:00
|
|
|
elseif ($this->non_contact_fields[$name])
|
|
|
|
{
|
|
|
|
$filterfields[$this->non_contact_fields[$name]] = $value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2005-11-03 15:30:40 +01:00
|
|
|
if ($DEBUG) echo "<p>contacts_ldap::read(filter=$filter): Cant filter for '$name', allowed only: ".implode(', ',array_keys($this->non_contact_fields))."</p>\n";
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$filterfields += array('phpgwcontacttypeid' => 'n');
|
2004-02-16 05:50:26 +01:00
|
|
|
if($DEBUG) { echo "<br>DEBUG - Filter strings: #phpgwcontacttypeid=n#"; }
|
2003-08-28 16:31:11 +02:00
|
|
|
}
|
|
|
|
|
2004-01-24 16:57:57 +01:00
|
|
|
/*
|
2003-08-28 16:31:11 +02:00
|
|
|
need some way of using the lastmod arg in the filter like this:
|
|
|
|
if($lastmod >= 0)
|
|
|
|
{
|
2004-02-16 05:50:26 +01:00
|
|
|
$filterfields += array('last_mod' => (int)$lastmod;
|
2002-02-08 06:04:33 +01:00
|
|
|
}
|
2003-08-28 16:31:11 +02:00
|
|
|
or maybe not like this - i am not sure what i am doing :)
|
2004-01-24 16:57:57 +01:00
|
|
|
*/
|
2001-07-05 09:37:36 +02:00
|
|
|
|
2003-12-10 12:36:54 +01:00
|
|
|
if(@is_array($this->grants))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-02-16 05:50:26 +01:00
|
|
|
$filterfields['phpgwcontactowner'] = array();
|
2001-11-23 04:49:56 +01:00
|
|
|
/* this was not listing private entries when show all was selected */
|
|
|
|
/* $filterfields += array('phpgwcontactaccess' => 'public'); */
|
2004-02-16 05:50:26 +01:00
|
|
|
if($DEBUG) { echo '<br>DEBUG - My user id is: ' . $this->account_id; }
|
|
|
|
foreach($this->grants as $user => $right)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-02-16 05:50:26 +01:00
|
|
|
if($DEBUG) { echo '<br>DEBUG - Grant from owner: ' . $user; }
|
|
|
|
$filterfields['phpgwcontactowner'][] = array('phpgwcontactowner' => $user);
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
2004-02-16 05:50:26 +01:00
|
|
|
if($DEBUG)
|
2002-02-08 06:04:33 +01:00
|
|
|
{
|
2003-08-28 16:31:11 +02:00
|
|
|
while(list($name,$value) = each($filterfields))
|
|
|
|
{
|
2004-02-16 05:50:26 +01:00
|
|
|
echo '<br>DEBUG - Filter strings: #' . $name . ',' . $value . '#';
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2004-02-16 05:50:26 +01:00
|
|
|
$sort = $sort ? $sort : 'ASC';
|
|
|
|
$order = $order ? $order : 'n_family';
|
2001-07-05 09:37:36 +02:00
|
|
|
|
2004-02-16 05:50:26 +01:00
|
|
|
if($DEBUG && $order)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
echo "<br>DEBUG - ORDER by $order";
|
|
|
|
}
|
|
|
|
|
2002-04-24 14:15:31 +02:00
|
|
|
$ldap_fields = array();
|
|
|
|
$myfilter = '';
|
|
|
|
|
2004-01-17 04:21:21 +01:00
|
|
|
if($cquery)
|
|
|
|
{
|
|
|
|
$search_filter = array(
|
|
|
|
'fn' => 'cn',
|
|
|
|
'n_family' => 'sn',
|
|
|
|
'org_name' => 'o'
|
|
|
|
);
|
|
|
|
$myfilter = $this->makefilter($filterfields,$search_filter,"$cquery*",$DEBUG);
|
|
|
|
}
|
|
|
|
elseif($query)
|
2004-01-24 16:57:57 +01:00
|
|
|
{
|
2003-09-28 13:06:44 +02:00
|
|
|
// the old code was searching about all fields
|
|
|
|
// this was very slow
|
|
|
|
#reset($this->stock_contact_fields);
|
|
|
|
#$myfilter = $this->makefilter($filterfields,$this->stock_contact_fields,$query,$DEBUG);
|
2004-02-09 21:18:21 +01:00
|
|
|
|
2004-03-22 16:34:32 +01:00
|
|
|
if(is_array($query))
|
|
|
|
{
|
|
|
|
// must be fixed somehow Milosch????
|
2005-08-27 14:24:56 +02:00
|
|
|
$myfilter = $this->makefilter($filterfields,$GLOBALS['egw']->common->ldap_addslashes($query),'',$DEBUG);
|
2004-03-22 16:34:32 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// don't search about any fields any more
|
|
|
|
$search_filter = array(
|
2005-09-29 13:31:02 +02:00
|
|
|
'fn' => 'cn',
|
|
|
|
'n_given' => 'givenname',
|
|
|
|
'n_family' => 'sn',
|
|
|
|
'email' => 'mail',
|
|
|
|
'org_name' => 'o',
|
|
|
|
'org_unit' => 'ou'
|
2004-03-22 16:34:32 +01:00
|
|
|
);
|
2005-08-27 14:24:56 +02:00
|
|
|
$myfilter = $this->makefilter($filterfields,$search_filter,$GLOBALS['egw']->common->ldap_addslashes($query),$DEBUG);
|
2004-03-22 16:34:32 +01:00
|
|
|
}
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-04-24 14:15:31 +02:00
|
|
|
$myfilter = $this->makefilter($filterfields,'','',$DEBUG);
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
2005-08-27 14:24:56 +02:00
|
|
|
$myfilter = $GLOBALS['egw']->translation->convert($myfilter,$GLOBALS['egw']->translation->system_charset,'utf-8');
|
2001-07-05 09:37:36 +02:00
|
|
|
|
2005-08-27 14:24:56 +02:00
|
|
|
$sri = ldap_search($this->ldap, $GLOBALS['egw_info']['server']['ldap_contact_context'], $myfilter);
|
2002-04-24 14:15:31 +02:00
|
|
|
|
|
|
|
$ldap_fields = ldap_get_entries($this->ldap, $sri);
|
|
|
|
/* _debug_array($ldap_fields);exit; */
|
|
|
|
|
|
|
|
$this->total_records = ldap_count_entries($this->ldap, $sri);
|
|
|
|
/* echo '<br>total="'.$this->total_records.'"'; */
|
|
|
|
if($DEBUG) { echo '<br>Query returned "'.$this->total_records.'" records.'; }
|
|
|
|
|
2005-09-28 15:10:22 +02:00
|
|
|
/* Use usort to sort the complete result, since ldap_search can not do that */
|
2003-08-28 16:31:11 +02:00
|
|
|
@set_time_limit(0); /* Try not to die, this can take some time on slow machines... */
|
2006-01-06 03:47:45 +01:00
|
|
|
$order_array = "array('";
|
|
|
|
if(!empty($order)) $order_array .= $this->stock_contact_fields[$order] . "','";
|
|
|
|
$order_array .= $this->stock_contact_fields['n_family'] . "', '"
|
|
|
|
. $this->stock_contact_fields['n_given'] . "', '"
|
|
|
|
. $this->stock_contact_fields['email'] . "')";
|
2005-09-30 18:03:27 +02:00
|
|
|
# remove the "count" field from the array, because usort screws up associative arrays
|
|
|
|
unset($ldap_fields['count']);
|
|
|
|
# sort the array
|
2005-09-28 15:10:22 +02:00
|
|
|
$function = 'return contacts::_cmp(' . $order_array . ",'" . $sort . "'," . '$a, $b);';
|
|
|
|
usort($ldap_fields, create_function('$a, $b', $function));
|
2001-07-05 09:37:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
This logic allows you to limit rows, or not.
|
|
|
|
The export feature, for example, does not limit rows.
|
|
|
|
This way, it can retrieve all rows at once.
|
|
|
|
*/
|
2004-02-16 05:50:26 +01:00
|
|
|
if($start && $limit)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$limit = $start + $limit;
|
|
|
|
}
|
2004-02-16 05:50:26 +01:00
|
|
|
elseif($start && !$limit)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$limit = $start;
|
|
|
|
}
|
|
|
|
elseif(!$start && !$limit)
|
|
|
|
{
|
|
|
|
$limit = $this->total_records;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$start = 0;
|
|
|
|
$limit = $limit;
|
|
|
|
}
|
|
|
|
/* echo '('.$start.','.$limit.')'; */
|
|
|
|
|
|
|
|
@reset($ldap_fields);
|
2004-02-16 05:50:26 +01:00
|
|
|
$j = 0;
|
|
|
|
for($i=$start;$i<$limit;$i++)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-02-16 05:50:26 +01:00
|
|
|
if($i<$this->total_records && $ldap_fields[$i]['uid'][0])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$return_fields[$j]['id'] = $ldap_fields[$i]['uidnumber'][0];
|
|
|
|
$return_fields[$j]['lid'] = $ldap_fields[$i]['uid'][0];
|
|
|
|
$return_fields[$j]['tid'] = $ldap_fields[$i]['phpgwcontacttypeid'][0];
|
|
|
|
$return_fields[$j]['owner'] = $ldap_fields[$i]['phpgwcontactowner'][0];
|
|
|
|
$return_fields[$j]['access'] = $ldap_fields[$i]['phpgwcontactaccess'][0];
|
2003-08-28 16:31:11 +02:00
|
|
|
$return_fields[$j]['cat_id'] = $ldap_fields[$i]['phpgwcontactcatid'][0];
|
2004-01-24 16:57:57 +01:00
|
|
|
$return_fields[$j]['rights'] = (int)$this->grants[$return_fields[$j]['owner']];
|
2001-07-05 09:37:36 +02:00
|
|
|
|
2003-12-10 12:36:54 +01:00
|
|
|
if(@is_array($stock_fieldnames))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-01-24 16:57:57 +01:00
|
|
|
foreach($stock_fieldnames as $f_name => $f_value)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2005-08-27 14:24:56 +02:00
|
|
|
$return_fields[$j][$f_name] = $GLOBALS['egw']->translation->convert(($ldap_fields[$i][$f_value][0]),'utf-8');
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->db->query("SELECT contact_name,contact_value FROM $this->ext_table WHERE contact_id='"
|
2003-12-10 12:36:54 +01:00
|
|
|
. (int)$ldap_fields[$i]['uidnumber'] . "'",__LINE__,__FILE__);
|
2004-02-16 05:50:26 +01:00
|
|
|
while($this->db->next_record())
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-02-16 05:50:26 +01:00
|
|
|
if($extra_fields[$this->db->f('contact_name')])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$return_fields[$j][$this->db->f('contact_name')] = $this->db->f('contact_value');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return_fields;
|
|
|
|
}
|
|
|
|
|
2002-04-24 14:15:31 +02:00
|
|
|
/* Used by read() above to build the ldap filter string */
|
2003-08-28 16:31:11 +02:00
|
|
|
function makefilter($qarray,$extra='',$query='', $DEBUG=False)
|
2002-04-24 14:15:31 +02:00
|
|
|
{
|
2005-11-03 15:30:40 +01:00
|
|
|
if($DEBUG) echo "<p>contacts_ldap::makefilter(".print_r($qarray,true).",'$extra','$query')</p>\n";
|
|
|
|
|
2003-12-10 12:36:54 +01:00
|
|
|
if(!@is_array($qarray))
|
2002-04-24 14:15:31 +02:00
|
|
|
{
|
|
|
|
return $qarray;
|
|
|
|
}
|
|
|
|
|
2004-02-20 17:47:06 +01:00
|
|
|
$first = $last = "*";
|
2005-08-13 23:01:14 +02:00
|
|
|
// this can only be the case, if $query is a character-query.
|
|
|
|
// normal queries don't allow wildcards and escape them
|
|
|
|
if(strstr($query,"*") && !strstr($query,"\*"))
|
2004-02-20 17:47:06 +01:00
|
|
|
{
|
|
|
|
if(substr($query,-1) == "*")
|
|
|
|
{
|
|
|
|
$last = '';
|
|
|
|
}
|
|
|
|
if(substr($query,1) == "*")
|
|
|
|
{
|
|
|
|
$first = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-10 12:36:54 +01:00
|
|
|
if(@is_array($extra))
|
2002-04-24 14:15:31 +02:00
|
|
|
{
|
|
|
|
if($DEBUG) { echo '<br>Searching...'; }
|
2004-01-24 16:57:57 +01:00
|
|
|
foreach($extra as $name => $value)
|
2002-04-24 14:15:31 +02:00
|
|
|
{
|
|
|
|
$qarray[] = array($value => $query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif($extra)
|
|
|
|
{
|
|
|
|
$tmp = split('=',$extra);
|
|
|
|
$qarray[] = array($tmp[0] => $tmp[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@ksort($qarray);
|
|
|
|
|
|
|
|
$aquery = '(&';
|
|
|
|
$oquery = '(|';
|
|
|
|
$hasor = False;
|
|
|
|
|
2004-01-24 16:57:57 +01:00
|
|
|
foreach($qarray as $name => $value)
|
2002-04-24 14:15:31 +02:00
|
|
|
{
|
2003-12-10 12:36:54 +01:00
|
|
|
if(@is_array($value))
|
2002-04-24 14:15:31 +02:00
|
|
|
{
|
2004-01-24 16:57:57 +01:00
|
|
|
foreach($value as $x => $y)
|
2002-04-24 14:15:31 +02:00
|
|
|
{
|
|
|
|
if($y == '*')
|
|
|
|
{
|
|
|
|
$oquery .= '(' . $x . '=*)';
|
|
|
|
$hasor = True;
|
|
|
|
}
|
2003-12-10 12:36:54 +01:00
|
|
|
elseif(@is_array($y))
|
2002-04-24 14:15:31 +02:00
|
|
|
{
|
|
|
|
/* This was most likely created from acl grants in read() above */
|
2004-01-24 16:57:57 +01:00
|
|
|
foreach($y as $a => $b)
|
2002-04-24 14:15:31 +02:00
|
|
|
{
|
|
|
|
$tmp .= '(' . $a . '=' . $b . ')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-02-20 17:47:06 +01:00
|
|
|
$oquery .= '(' . $x . '=' . $first . $y . $last . ')';
|
2002-04-24 14:15:31 +02:00
|
|
|
$hasor = True;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif($value == $query)
|
|
|
|
{
|
|
|
|
/* searching */
|
2004-02-20 17:47:06 +01:00
|
|
|
$oquery .= '(' . $name . '=' . $first . $value . $last . ')';
|
2002-04-24 14:15:31 +02:00
|
|
|
$hasor = True;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* exact value (filtering based on tid, etc...) */
|
2004-02-16 05:50:26 +01:00
|
|
|
if($name == 'phpgwcontactcatid')
|
2002-04-24 14:15:31 +02:00
|
|
|
{
|
2005-08-27 14:24:56 +02:00
|
|
|
if (!is_object($GLOBALS['egw']->categories))
|
2004-04-05 00:52:24 +02:00
|
|
|
{
|
2005-11-03 15:30:40 +01:00
|
|
|
$GLOBALS['egw']->categories =& CreateObject('phpgwapi.categories');
|
2004-04-05 00:52:24 +02:00
|
|
|
}
|
2005-08-27 14:24:56 +02:00
|
|
|
$cats = $GLOBALS['egw']->categories->return_all_children((int)$value);
|
2004-04-05 00:52:24 +02:00
|
|
|
|
|
|
|
$aquery .= '(|';
|
|
|
|
foreach($cats as $cat)
|
|
|
|
{
|
|
|
|
$aquery .= '(' . $name . '=*,' . $cat . ',*)(' . $name . '=' . $cat . ')';
|
|
|
|
}
|
|
|
|
$aquery .= ')';
|
2002-04-24 14:15:31 +02:00
|
|
|
}
|
2005-11-03 15:30:40 +01:00
|
|
|
elseif ($value == "!''") // query for not empty
|
|
|
|
{
|
|
|
|
$aquery .= '(' . $name . '=*)';
|
|
|
|
}
|
2002-04-24 14:15:31 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$aquery .= '(' . $name . '=' . $value . ')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($tmp)
|
|
|
|
{
|
|
|
|
if(strstr($tmp,')('))
|
|
|
|
{
|
|
|
|
$aquery .= '(|' . $tmp . ')';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$aquery .= $tmp;
|
|
|
|
}
|
|
|
|
unset($tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$aquery .= ')';
|
|
|
|
$oquery .= ')';
|
|
|
|
if(!$hasor)
|
|
|
|
{
|
|
|
|
$oquery = '';
|
|
|
|
$fquery = $aquery;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$fquery = '(&' . $aquery . $oquery . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if($DEBUG)
|
|
|
|
{
|
|
|
|
echo '<br>AND query: "' . $aquery . '"';
|
|
|
|
echo '<br>OR query: "' . $oquery . '"';
|
|
|
|
echo '<br>Full query: "' . $fquery . '"';
|
2006-01-06 03:47:45 +01:00
|
|
|
echo '<br>Will search in "' . $GLOBALS['egw_info']['server']['ldap_contact_context'] . '"';
|
2002-04-24 14:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $fquery;
|
|
|
|
}
|
|
|
|
|
2004-02-16 05:50:26 +01:00
|
|
|
function add($owner,$fields,$access=NULL,$cat_id=NULL,$tid=NULL)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-02-16 05:50:26 +01:00
|
|
|
// access, cat_id and tid can be in $fields now or as extra params
|
|
|
|
foreach(array('access','cat_id','tid') as $extra)
|
2001-11-23 02:58:19 +01:00
|
|
|
{
|
2004-02-16 05:50:26 +01:00
|
|
|
if(!is_null($$extra))
|
|
|
|
{
|
|
|
|
$fields[$extra] = $$extra;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(empty($fields['tid']))
|
|
|
|
{
|
|
|
|
$fields['tid'] = 'n';
|
2001-11-23 02:58:19 +01:00
|
|
|
}
|
|
|
|
|
2005-08-27 14:24:56 +02:00
|
|
|
if(!$GLOBALS['egw_info']['server']['ldap_contact_context'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
|
|
|
|
|
|
|
|
$free = 0;
|
2005-08-27 14:24:56 +02:00
|
|
|
$this->nextid = $GLOBALS['egw']->common->last_id('contacts');
|
2001-07-05 09:37:36 +02:00
|
|
|
/* Loop until we find a free id */
|
2004-02-16 05:50:26 +01:00
|
|
|
while(!$free)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$ldap_fields = '';
|
2005-08-27 14:24:56 +02:00
|
|
|
$sri = ldap_search($this->ldap, $GLOBALS['egw_info']['server']['ldap_contact_context'], 'uidnumber='.$this->nextid);
|
2001-07-05 09:37:36 +02:00
|
|
|
$ldap_fields = ldap_get_entries($this->ldap, $sri);
|
2004-02-16 05:50:26 +01:00
|
|
|
if($ldap_fields[0]['dn'][0])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2005-08-27 14:24:56 +02:00
|
|
|
$this->nextid = $GLOBALS['egw']->common->next_id('contacts');
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$free = True;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$ldap_fields = '';
|
2003-12-10 12:36:54 +01:00
|
|
|
if(@is_array($stock_fieldnames))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-01-24 16:57:57 +01:00
|
|
|
foreach($stock_fieldnames as $name => $value)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-02-16 05:50:26 +01:00
|
|
|
if($stock_fields[$name] != '')
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2005-08-27 14:24:56 +02:00
|
|
|
$ldap_fields[$value] = $GLOBALS['egw']->translation->convert($stock_fields[$name],$GLOBALS['egw']->translation->system_charset,'utf-8');
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$time = gettimeofday();
|
2003-08-28 16:31:11 +02:00
|
|
|
$ldap_fields['uid'] = time().$time['usec'].':'.$ldap_fields['givenname'];
|
2001-07-05 09:37:36 +02:00
|
|
|
|
2005-08-27 14:24:56 +02:00
|
|
|
$dn = 'uid=' . $ldap_fields['uid'].',' . $GLOBALS['egw_info']['server']['ldap_contact_context'];
|
2004-02-16 05:50:26 +01:00
|
|
|
$ldap_fields['phpgwcontacttypeid'] = $fields['tid'];
|
2003-08-28 16:31:11 +02:00
|
|
|
$ldap_fields['phpgwcontactowner'] = $owner;
|
2004-02-16 05:50:26 +01:00
|
|
|
if(!isset($fields['access']))
|
|
|
|
{
|
|
|
|
$fields['access'] = 'private';
|
|
|
|
}
|
|
|
|
$ldap_fields['phpgwcontactaccess'] = $fields['access'];
|
|
|
|
$ldap_fields['phpgwcontactcatid'] = $fields['cat_id'] ? $fields['cat_id'] : '0';
|
2003-08-28 16:31:11 +02:00
|
|
|
$ldap_fields['uidnumber'] = $this->nextid;
|
|
|
|
/* $ldap_fields['objectclass'][0] = 'person'; */
|
|
|
|
$ldap_fields['objectclass'][0] = 'organizationalPerson';
|
|
|
|
$ldap_fields['objectclass'][1] = 'inetOrgPerson';
|
|
|
|
$ldap_fields['objectclass'][2] = 'phpgwContact';
|
2005-08-27 14:24:56 +02:00
|
|
|
//$ldap_fields['last_mod'] = $GLOBALS['egw']->datetime->gmtnow;
|
2001-07-05 09:37:36 +02:00
|
|
|
|
2004-01-17 04:21:21 +01:00
|
|
|
$err = $this->validate($ldap_fields);
|
|
|
|
if(@is_array($err) && @isset($err[0]))
|
2003-12-22 13:46:38 +01:00
|
|
|
{
|
2004-01-17 04:21:21 +01:00
|
|
|
return $err;
|
2003-12-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
// _debug_array($ldap_fields); exit;
|
2001-07-05 09:37:36 +02:00
|
|
|
$err = ldap_add($this->ldap, $dn, $ldap_fields);
|
2004-01-17 04:21:21 +01:00
|
|
|
if(!$err)
|
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
2001-07-05 09:37:36 +02:00
|
|
|
|
2004-01-17 04:21:21 +01:00
|
|
|
if(count($extra_fields))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-01-24 16:57:57 +01:00
|
|
|
foreach($extra_fields as $name => $value)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$this->db->query("INSERT INTO $this->ext_table VALUES ('".$this->nextid."','" . $this->account_id . "','"
|
|
|
|
. addslashes($name) . "','" . addslashes($value) . "')",__LINE__,__FILE__);
|
|
|
|
}
|
|
|
|
}
|
2001-08-09 02:32:59 +02:00
|
|
|
return $this->nextid;
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
|
2004-01-17 04:21:21 +01:00
|
|
|
/* LDAP syntaxes require some testing prior to add */
|
|
|
|
function validate(&$entry)
|
|
|
|
{
|
|
|
|
$errors = array();
|
|
|
|
foreach($entry as $field => $value)
|
|
|
|
{
|
|
|
|
if(strstr($field,'phone'))
|
|
|
|
{
|
|
|
|
/* Regex for testing valid international phone number entries.
|
|
|
|
* LDAP may reject bad values here, such as an email address in a phone number.
|
|
|
|
* This format is somewhat loose, allowing for optional parenthesis, + sign,
|
|
|
|
* and 0-7 numbers between separators.
|
|
|
|
*/
|
2004-07-11 15:35:22 +02:00
|
|
|
$regex = "/^[-0-9\+\(\)\/]/";
|
2004-01-17 04:21:21 +01:00
|
|
|
if(!preg_match($regex,$value))
|
|
|
|
{
|
|
|
|
$errors[] = array($field => $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif(strstr($field,'mailtype') || strstr($field,'mailhometype'))
|
|
|
|
{
|
|
|
|
/* Check for valid mail type */
|
|
|
|
if(!@isset($this->email_types[$value]))
|
|
|
|
{
|
|
|
|
$errors[] = array($field => $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif(strstr($field,'mail'))
|
|
|
|
{
|
|
|
|
/* Check for valid email address - TODO - should depend on mail type */
|
|
|
|
$regex = "/[ |\t|\r|\n]*\"?([^\"]+\"?@[^ <>\t]+\.[^ <>\t][^ <>\t]+)[ |\t|\r|\n]*/x";
|
|
|
|
if(!preg_match($regex,$value))
|
|
|
|
{
|
|
|
|
$errors[] = array($field => $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Verify sn/cn attrs set */
|
|
|
|
if(empty($entry['sn']) && !empty($entry['cn']))
|
|
|
|
{
|
|
|
|
$entry['sn'] = $entry['cn'];
|
|
|
|
}
|
|
|
|
if(empty($entry['cn']) && !empty($entry['sn']))
|
|
|
|
{
|
|
|
|
$entry['cn'] = $entry['sn'];
|
|
|
|
}
|
|
|
|
$entry['cn'] = $entry['cn'] ? $entry['cn'] : '-';
|
|
|
|
$entry['sn'] = $entry['sn'] ? $entry['sn'] : '-';
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
2001-07-05 09:37:36 +02:00
|
|
|
function field_exists($id,$field_name)
|
|
|
|
{
|
2004-02-09 21:18:21 +01:00
|
|
|
$this->db->query("SELECT COUNT(*) FROM $this->ext_table where contact_id='" . (int)$id . "' AND contact_name='"
|
2003-08-28 16:31:11 +02:00
|
|
|
. addslashes($field_name) . "'",__LINE__,__FILE__);
|
2001-07-05 09:37:36 +02:00
|
|
|
$this->db->next_record();
|
|
|
|
return $this->db->f(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function add_single_extra_field($id,$owner,$field_name,$field_value)
|
|
|
|
{
|
2004-02-09 21:18:21 +01:00
|
|
|
$this->db->query("INSERT INTO $this->ext_table VALUES (" . (int)$id . ",'$owner','" . addslashes($field_name)
|
|
|
|
. "','" . addslashes($field_value) . "')",__LINE__,__FILE__);
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function delete_single_extra_field($id,$field_name)
|
|
|
|
{
|
2004-02-09 21:18:21 +01:00
|
|
|
$this->db->query("DELETE FROM $this->ext_table WHERE contact_id='" . (int)$id . "' AND contact_name='"
|
|
|
|
. addslashes($field_name) . "'",__LINE__,__FILE__);
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
|
2004-02-15 19:12:47 +01:00
|
|
|
function update($id,$owner,$fields,$access=NULL,$cat_id=NULL,$tid=NULL)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-02-15 19:12:47 +01:00
|
|
|
// access, cat_id and tid can be in $fields now or as extra params
|
|
|
|
foreach(array('access','cat_id','tid') as $extra)
|
|
|
|
{
|
2004-02-16 05:50:26 +01:00
|
|
|
if(!is_null($$extra))
|
2004-02-15 19:12:47 +01:00
|
|
|
{
|
|
|
|
$fields[$extra] = $$extra;
|
|
|
|
}
|
2004-02-16 05:50:26 +01:00
|
|
|
if(isset($fields[$extra]))
|
2004-02-15 19:12:47 +01:00
|
|
|
{
|
|
|
|
$stock_fields[$extra] = $fields[$extra];
|
|
|
|
}
|
|
|
|
}
|
2001-07-05 09:37:36 +02:00
|
|
|
$nonfields = $this->non_contact_fields;
|
|
|
|
|
2005-08-27 14:24:56 +02:00
|
|
|
if(!$GLOBALS['egw_info']['server']['ldap_contact_context'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First make sure that id number exists */
|
2005-08-27 14:24:56 +02:00
|
|
|
$sri = ldap_search($this->ldap, $GLOBALS['egw_info']['server']['ldap_contact_context'], 'uidnumber=' . (int)$id);
|
2001-07-05 09:37:36 +02:00
|
|
|
$ldap_fields = ldap_get_entries($this->ldap, $sri);
|
|
|
|
|
2004-02-16 05:50:26 +01:00
|
|
|
if($ldap_fields[0]['dn'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$dn = $ldap_fields[0]['dn'];
|
|
|
|
list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
|
2003-12-10 12:36:54 +01:00
|
|
|
if(@is_array($stock_fieldnames))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
Check each value, add our extra attributes if they are missing, and
|
|
|
|
otherwise fix the entry while we can.
|
|
|
|
*/
|
|
|
|
/* Verify uidnumber */
|
|
|
|
$stock_fields['id'] = $id;
|
2004-02-16 05:50:26 +01:00
|
|
|
if(empty($ldap_fields[0]['uidnumber']))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$err = ldap_modify($this->ldap,$dn,array('uidnumber' => $stock_fields['uidnumber']));
|
|
|
|
}
|
2004-02-16 05:50:26 +01:00
|
|
|
elseif(!$ldap_fields[0]['uidnumber'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$err = ldap_mod_add($this->ldap,$dn,array('uidnumber' => $stock_fields['uidnumber']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify uid */
|
|
|
|
$uids = split(',',$dn);
|
|
|
|
$stock_fields['lid'] = $uids[0];
|
2004-02-16 05:50:26 +01:00
|
|
|
if(empty($ldap_fields[0]['uid']))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$err = ldap_modify($this->ldap,$dn,array('uid' => $stock_fields['lid']));
|
|
|
|
}
|
2004-02-16 05:50:26 +01:00
|
|
|
elseif(!$ldap_fields[0]['uid'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$err = ldap_mod_add($this->ldap,$dn,array('uid' => $stock_fields['lid']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify objectclasses are there */
|
2004-02-16 05:50:26 +01:00
|
|
|
if(empty($ldap_fields[0]['objectclass']))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
/* $stock_fields['objectclass'][0] = 'person'; */
|
|
|
|
$stock_fields['objectclass'][0] = 'organizationalPerson';
|
|
|
|
$stock_fields['objectclass'][1] = 'inetOrgPerson';
|
|
|
|
$stock_fields['objectclass'][2] = 'phpgwContact';
|
|
|
|
$err = ldap_modify($this->ldap,$dn,array('objectclass' => $stock_fields['objectclass']));
|
|
|
|
}
|
2004-02-16 05:50:26 +01:00
|
|
|
elseif(!$ldap_fields[0]['objectclass'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
/* $stock_fields['objectclass'][0] = 'person'; */
|
|
|
|
$stock_fields['objectclass'][0] = 'organizationalPerson';
|
|
|
|
$stock_fields['objectclass'][1] = 'inetOrgPerson';
|
|
|
|
$stock_fields['objectclass'][2] = 'phpgwContact';
|
|
|
|
$err = ldap_mod_add($this->ldap,$dn,array('objectclass' => $stock_fields['objectclass']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify owner */
|
|
|
|
$stock_fields['owner'] = $owner;
|
2004-02-16 05:50:26 +01:00
|
|
|
if(empty($ldap_fields[0]['phpgwcontactowner']))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$err = ldap_modify($this->ldap,$dn,array('phpgwcontactowner' => $stock_fields['owner']));
|
|
|
|
}
|
2004-02-16 05:50:26 +01:00
|
|
|
elseif(!$ldap_fields[0]['phpgwcontactowner'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$err = ldap_mod_add($this->ldap,$dn,array('phpgwcontactowner' => $stock_fields['owner']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify access */
|
2004-02-15 19:12:47 +01:00
|
|
|
$stock_fields['access'] = $fields['access'];
|
2004-02-16 05:50:26 +01:00
|
|
|
if(empty($ldap_fields[0]['phpgwcontactaccess']))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$err = ldap_modify($this->ldap,$dn,array('phpgwcontactaccess' => $stock_fields['access']));
|
|
|
|
}
|
2004-02-16 05:50:26 +01:00
|
|
|
elseif(!$ldap_fields[0]['phpgwcontactaccess'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$err = ldap_mod_add($this->ldap,$dn,array('phpgwcontactaccess' => $stock_fields['access']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify cat_id */
|
2004-02-15 19:12:47 +01:00
|
|
|
$stock_fields['cat_id'] = $fields['cat_id'] ? $fields['cat_id'] : ' ';
|
2004-02-16 05:50:26 +01:00
|
|
|
if(empty($ldap_fields[0]['phpgwcontactcatid']))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$err = ldap_modify($this->ldap,$dn,array('phpgwcontactcatid' => $stock_fields['cat_id']));
|
|
|
|
}
|
2004-02-16 05:50:26 +01:00
|
|
|
elseif(!$ldap_fields[0]['phpgwcontactcatid'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$err = ldap_mod_add($this->ldap,$dn,array('phpgwcontactcatid' => $stock_fields['cat_id']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify tid */
|
2004-02-15 19:12:47 +01:00
|
|
|
$stock_fields['tid'] = $fields['tid'];
|
2004-02-16 05:50:26 +01:00
|
|
|
if(empty($ldap_fields[0]['phpgwcontacttypeid']))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$err = ldap_modify($this->ldap,$dn,array('phpgwcontacttypeid' => $stock_fields['tid']));
|
|
|
|
}
|
2004-02-16 05:50:26 +01:00
|
|
|
elseif(!$ldap_fields[0]['phpgwcontacttypeid'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$err = ldap_mod_add($this->ldap,$dn,array('phpgwcontacttypeid' => $stock_fields['tid']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* OK, just mod the data already */
|
|
|
|
$allfields = $stock_fieldnames + $nonfields;
|
2004-02-15 19:12:47 +01:00
|
|
|
/* Don't try to modify the uid, since this affects the dn */
|
|
|
|
unset($allfields['lid']);
|
2004-01-24 16:57:57 +01:00
|
|
|
foreach($allfields as $fname => $fvalue)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-01-24 16:57:57 +01:00
|
|
|
if($ldap_fields[0][$fvalue] && $stock_fields[$fname] && $ldap_fields[0][$fvalue][0] != $stock_fields[$fname] )
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-02-15 19:12:47 +01:00
|
|
|
//echo "<br>".$fname." => ".$fvalue." was there";
|
2005-08-27 14:24:56 +02:00
|
|
|
$err = ldap_modify($this->ldap,$dn,array($fvalue => $GLOBALS['egw']->translation->convert($stock_fields[$fname],$GLOBALS['egw']->translation->system_charset,'utf-8')));
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
2004-01-24 16:57:57 +01:00
|
|
|
elseif(!$ldap_fields[0][$fvalue] && $stock_fields[$fname])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-02-15 19:12:47 +01:00
|
|
|
//echo "<br>".$fname." not there - '".$fvalue."'";
|
2005-08-27 14:24:56 +02:00
|
|
|
$err = ldap_mod_add($this->ldap,$dn,array($fvalue => $GLOBALS['egw']->translation->convert($stock_fields[$fname],$GLOBALS['egw']->translation->system_charset,'utf-8')));
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
2004-01-24 16:57:57 +01:00
|
|
|
elseif($ldap_fields[0][$fvalue] && !$stock_fields[$fname])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-02-15 19:12:47 +01:00
|
|
|
//echo "<br>".$fname." gone... deleting - '".$fvalue."'";
|
2001-07-05 09:37:36 +02:00
|
|
|
/*
|
|
|
|
NOTE: we use the ldap_fields because we need to send the
|
|
|
|
_ORIGINAL_ contents as the value. see:
|
|
|
|
http://www.php.net/manual/en/function.ldap-mod-del.php
|
|
|
|
*/
|
|
|
|
$err = ldap_mod_del($this->ldap,$dn,array($fvalue => $ldap_fields[0][$fvalue][0]));
|
|
|
|
}
|
|
|
|
/* Else we have nothing to do. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-27 14:24:56 +02:00
|
|
|
//something here to update the last_mod from $GLOBALS['egw']->datetime->gmtnow
|
2003-08-28 16:31:11 +02:00
|
|
|
|
2004-01-24 16:57:57 +01:00
|
|
|
foreach($extra_fields as $x_name => $x_value)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-01-24 16:57:57 +01:00
|
|
|
if($this->field_exists($id,$x_name))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2004-01-24 16:57:57 +01:00
|
|
|
if(!$x_value)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$this->delete_single_extra_field($id,$x_name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->db->query("UPDATE $this->ext_table SET contact_value='" . addslashes($x_value)
|
2004-01-24 16:57:57 +01:00
|
|
|
. "',contact_owner='$owner' WHERE contact_name='" . addslashes($x_name)
|
2004-02-09 21:18:21 +01:00
|
|
|
. "' AND contact_id='" . (int)$id . "'",__LINE__,__FILE__);
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->add_single_extra_field($id,$owner,$x_name,$x_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Used by admin to change ownership on account delete */
|
|
|
|
function change_owner($old_owner='',$new_owner='')
|
|
|
|
{
|
2004-01-24 16:57:57 +01:00
|
|
|
if(!($new_owner && $old_owner))
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2005-08-27 14:24:56 +02:00
|
|
|
$sri = ldap_search($this->ldap, $GLOBALS['egw_info']['server']['ldap_contact_context'], 'phpgwcontactowner='.$old_owner);
|
2001-07-05 09:37:36 +02:00
|
|
|
$ldap_fields = ldap_get_entries($this->ldap, $sri);
|
|
|
|
|
2001-12-26 06:34:45 +01:00
|
|
|
$entry = '';
|
2005-08-30 22:56:33 +02:00
|
|
|
for($i=0; $i<$ldap_fields['count']; $i++)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2005-08-30 22:56:33 +02:00
|
|
|
$err = ldap_modify($this->ldap,$ldap_fields[$i]['dn'],array('phpgwcontactowner' => $new_owner));
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
|
2005-08-30 22:56:33 +02:00
|
|
|
$this->db->query("UPDATE $this->ext_table SET contact_owner='$new_owner' WHERE contact_owner=$old_owner",__LINE__,__FILE__);
|
2001-07-05 09:37:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is where the real work of delete() is done, shared class file contains calling function */
|
|
|
|
function delete_($id)
|
|
|
|
{
|
2005-08-27 14:24:56 +02:00
|
|
|
if(!$GLOBALS['egw_info']['server']['ldap_contact_context'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2005-08-27 14:24:56 +02:00
|
|
|
$sri = ldap_search($this->ldap, $GLOBALS['egw_info']['server']['ldap_contact_context'], 'uidnumber='.$id);
|
2001-07-05 09:37:36 +02:00
|
|
|
$ldap_fields = ldap_get_entries($this->ldap, $sri);
|
|
|
|
|
2004-02-16 05:50:26 +01:00
|
|
|
if($ldap_fields[0]['dn'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$err = ldap_delete($this->ldap,$ldap_fields[0]['dn']);
|
|
|
|
|
2004-02-09 21:18:21 +01:00
|
|
|
$this->db->query("DELETE FROM $this->ext_table WHERE contact_id='" . (int)$id . "' AND contact_owner='"
|
2004-01-24 16:57:57 +01:00
|
|
|
. $this->account_id . "'",__LINE__,__FILE__);
|
2001-07-05 09:37:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is for the admin script deleteaccount.php
|
|
|
|
function delete_all($owner=0)
|
|
|
|
{
|
2005-08-27 14:24:56 +02:00
|
|
|
if(!$GLOBALS['egw_info']['server']['ldap_contact_context'])
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2004-02-16 05:50:26 +01:00
|
|
|
if($owner)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
2005-08-27 14:24:56 +02:00
|
|
|
$sri = ldap_search($this->ldap, $GLOBALS['egw_info']['server']['ldap_contact_context'], 'phpgwcontactowner='.$owner);
|
2001-07-05 09:37:36 +02:00
|
|
|
$ldap_fields = ldap_get_entries($this->ldap, $sri);
|
|
|
|
|
|
|
|
$entry = '';
|
2004-01-24 16:57:57 +01:00
|
|
|
foreach($ldap_fields as $nul => $entry)
|
2001-07-05 09:37:36 +02:00
|
|
|
{
|
|
|
|
$err = ldap_delete($this->ldap,$entry['dn']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db->query("DELETE FROM $this->ext_table WHERE contact_owner=$owner",__LINE__,__FILE__);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|