2001-01-11 10:52:33 +01:00
< ? php
2006-06-07 01:42:36 +02:00
/**
* API - accounts SQL backend
2009-03-05 16:17:45 +01:00
*
2006-06-07 01:42:36 +02:00
* The SQL backend stores the group memberships via the ACL class ( location 'phpgw_group' )
2009-03-05 16:17:45 +01:00
*
* The ( positive ) account_id ' s of groups are mapped in this class to negative numeric
2006-07-08 02:20:27 +02:00
* account_id ' s , to conform with the way we handle groups in LDAP !
2009-03-05 16:17:45 +01:00
*
2006-06-07 01:42:36 +02:00
* @ link http :// www . egroupware . org
* @ author Ralf Becker < RalfBecker - AT - outdoor - training . de > complete rewrite in 6 / 2006 and
* earlier to use the new DB functions
2009-03-05 16:17:45 +01:00
*
* This class replaces the former accounts_sql class written by
* Joseph Engo < jengo @ phpgroupware . org > , Dan Kuykendall < seek3r @ phpgroupware . org >
2006-06-07 01:42:36 +02:00
* and Bettina Gille < ceb @ phpgroupware . org >.
* Copyright ( C ) 2000 - 2002 Joseph Engo
* Copyright ( C ) 2003 Lars Kneschke , Bettina Gille
2009-03-05 16:17:45 +01:00
*
2006-06-07 01:42:36 +02:00
* @ license http :// opensource . org / licenses / gpl - license . php GPL - GNU General Public License
* @ package api
* @ subpackage accounts
* @ version $Id $
*/
/**
* SQL Backend for accounts
2009-03-05 16:17:45 +01:00
*
2006-06-07 01:42:36 +02:00
* @ author Ralf Becker < RalfBecker - AT - outdoor - training . de >
* @ license http :// opensource . org / licenses / gpl - license . php GPL - GNU General Public License
* @ package api
* @ subpackage accounts
* @ access internal only use the interface provided by the accounts class
*/
2007-12-13 03:32:44 +01:00
class accounts_sql
2006-06-07 01:42:36 +02:00
{
2005-03-24 14:15:12 +01:00
/**
2006-06-07 01:42:36 +02:00
* instance of the db class
*
2007-08-24 12:53:26 +02:00
* @ var egw_db
2005-03-24 14:15:12 +01:00
*/
2006-06-07 01:42:36 +02:00
var $db ;
/**
* table name for the accounts
*
* @ var string
*/
2013-01-25 14:21:31 +01:00
const TABLE = 'egw_accounts' ;
var $table = self :: TABLE ;
2006-06-17 20:47:02 +02:00
/**
* table name for the contacts
*
* @ var string
*/
2009-03-05 16:17:45 +01:00
var $contacts_table = 'egw_addressbook' ;
2006-06-17 20:47:02 +02:00
/**
* Join with the accounts - table used in contacts :: search
*
* @ var string
*/
var $contacts_join = ' RIGHT JOIN egw_accounts ON egw_accounts.account_id=egw_addressbook.account_id' ;
2006-06-07 01:42:36 +02:00
/**
* total number of found entries from get_list method
*
* @ var int
*/
var $total ;
2001-03-01 17:20:48 +01:00
2007-12-13 03:32:44 +01:00
/**
* Reference to our frontend
*
* @ var accounts
*/
2009-01-13 12:41:56 +01:00
private $frontend ;
2007-12-13 03:32:44 +01:00
2013-09-02 15:40:40 +02:00
/**
* does backend allow to change account_lid
*/
const CHANGE_ACCOUNT_LID = true ;
/**
* does backend requires password to be set , before allowing to enable an account
*/
const REQUIRE_PASSWORD_FOR_ENABLE = false ;
2007-12-13 03:32:44 +01:00
/**
* Constructor
*
* @ param accounts $frontend reference to the frontend class , to be able to call it ' s methods if needed
* @ return accounts_sql
*/
function __construct ( accounts $frontend )
2006-06-07 01:42:36 +02:00
{
2007-12-13 03:32:44 +01:00
$this -> frontend = $frontend ;
2006-06-07 01:42:36 +02:00
if ( is_object ( $GLOBALS [ 'egw_setup' ] -> db ))
2001-03-19 21:25:04 +01:00
{
2008-03-13 20:37:09 +01:00
$this -> db = $GLOBALS [ 'egw_setup' ] -> db ;
2001-03-19 21:25:04 +01:00
}
2006-06-07 01:42:36 +02:00
else
2003-08-28 16:31:11 +02:00
{
2008-03-13 20:37:09 +01:00
$this -> db = $GLOBALS [ 'egw' ] -> db ;
2003-08-28 16:31:11 +02:00
}
2006-06-07 01:42:36 +02:00
}
2003-08-28 16:31:11 +02:00
2006-06-07 01:42:36 +02:00
/**
* Reads the data of one account
2009-03-05 16:17:45 +01:00
*
2006-06-17 20:47:02 +02:00
* For performance reasons and because the contacts - object itself depends on the accounts - object ,
* we directly join with the contacts table for reading !
2006-06-07 01:42:36 +02:00
*
* @ param int $account_id numeric account - id
* @ return array / boolean array with account data ( keys : account_id , account_lid , ... ) or false if account not found
*/
function read ( $account_id )
{
if ( ! ( int ) $account_id ) return false ;
2009-03-05 16:17:45 +01:00
2006-06-17 20:47:02 +02:00
if ( $account_id > 0 )
{
$extra_cols = $this -> contacts_table . '.n_given AS account_firstname,' .
$this -> contacts_table . '.n_family AS account_lastname,' .
$this -> contacts_table . '.contact_email AS account_email,' .
$this -> contacts_table . '.n_fn AS account_fullname,' .
2008-04-10 11:06:00 +02:00
$this -> contacts_table . '.contact_id AS person_id,' .
$this -> contacts_table . '.contact_created AS account_created,' .
2011-08-31 14:17:34 +02:00
$this -> contacts_table . '.contact_modified AS account_modified,' .
$this -> contacts_table . '.tel_work AS account_phone,' ;
$join = 'LEFT JOIN ' . $this -> contacts_table . ' ON ' . $this -> table . '.account_id=' . $this -> contacts_table . '.account_id' ;
2006-06-17 20:47:02 +02:00
}
2015-02-17 11:51:16 +01:00
// during setup emailadmin might not yet be installed and running below query
// will abort transaction in PostgreSQL
elseif ( ! isset ( $GLOBALS [ 'egw_setup' ]) || in_array ( emailadmin_smtp_sql :: TABLE , $this -> db -> table_names ( true )))
2012-11-15 16:26:35 +01:00
{
$extra_cols = emailadmin_smtp_sql :: TABLE . '.mail_value AS account_email,' ;
$join = 'LEFT JOIN ' . emailadmin_smtp_sql :: TABLE . ' ON ' . $this -> table . '.account_id=-' . emailadmin_smtp_sql :: TABLE . '.account_id AND mail_type=' . emailadmin_smtp_sql :: TYPE_ALIAS ;
}
2012-11-22 14:25:22 +01:00
try {
$rs = $this -> db -> select ( $this -> table , $extra_cols . $this -> table . '.*' ,
$this -> table . '.account_id=' . abs ( $account_id ),
__LINE__ , __FILE__ , false , '' , false , 0 , $join );
}
2014-05-08 15:35:22 +02:00
catch ( egw_exception_db $e ) {
unset ( $e );
}
2012-11-22 14:25:22 +01:00
if ( ! $rs ) // handle not (yet) existing mailaccounts table
{
$rs = $this -> db -> select ( $this -> table , $this -> table . '.*' ,
$this -> table . '.account_id=' . abs ( $account_id ), __LINE__ , __FILE__ );
}
if ( ! $rs || ! ( $data = $rs -> fetch ()))
2001-03-23 04:10:28 +01:00
{
2006-06-07 01:42:36 +02:00
return false ;
2001-03-23 04:10:28 +01:00
}
2006-06-07 01:42:36 +02:00
if ( $data [ 'account_type' ] == 'g' )
2001-02-17 10:40:29 +01:00
{
2006-06-07 01:42:36 +02:00
$data [ 'account_id' ] = - $data [ 'account_id' ];
2012-11-15 16:26:35 +01:00
$data [ 'mailAllowed' ] = true ;
2001-03-23 04:10:28 +01:00
}
2006-06-17 20:47:02 +02:00
if ( ! $data [ 'account_firstname' ]) $data [ 'account_firstname' ] = $data [ 'account_lid' ];
if ( ! $data [ 'account_lastname' ])
{
$data [ 'account_lastname' ] = $data [ 'account_type' ] == 'g' ? 'Group' : 'User' ;
// if we call lang() before the translation-class is correctly setup,
// we can't switch away from english language anymore!
2012-03-04 14:33:10 +01:00
if ( translation :: $lang_arr )
2006-06-17 20:47:02 +02:00
{
$data [ 'account_lastname' ] = lang ( $data [ 'account_lastname' ]);
}
}
if ( ! $data [ 'account_fullname' ]) $data [ 'account_fullname' ] = $data [ 'account_firstname' ] . ' ' . $data [ 'account_lastname' ];
2001-03-23 04:10:28 +01:00
2006-06-17 20:47:02 +02:00
//echo "accounts_sql::read($account_id)"; _debug_array($data);
2006-06-07 01:42:36 +02:00
return $data ;
}
2001-03-23 04:10:28 +01:00
2006-06-07 01:42:36 +02:00
/**
* Saves / adds the data of one account
2009-03-05 16:17:45 +01:00
*
2006-06-07 01:42:36 +02:00
* If no account_id is set in data the account is added and the new id is set in $data .
*
* @ param array $data array with account - data
* @ return int / boolean the account_id or false on error
*/
function save ( & $data )
{
2006-06-07 07:16:56 +02:00
//echo "<p>accounts_sql::save(".print_r($data,true).")</p>\n";
2006-06-07 01:42:36 +02:00
$to_write = $data ;
unset ( $to_write [ 'account_passwd' ]);
// encrypt password if given or unset it if not
if ( $data [ 'account_passwd' ])
{
2006-06-08 02:25:57 +02:00
// if password it's not already entcrypted, do so now
2009-03-05 16:17:45 +01:00
if ( ! preg_match ( '/^\\{[a-z5]{3,5}\\}.+/i' , $data [ 'account_passwd' ]) &&
2006-06-08 02:25:57 +02:00
! preg_match ( '/^[0-9a-f]{32}$/' , $data [ 'account_passwd' ])) // md5 hash
{
$data [ 'account_passwd' ] = $GLOBALS [ 'egw' ] -> auth -> encrypt_sql ( $data [ 'account_passwd' ]);
}
$to_write [ 'account_pwd' ] = $data [ 'account_passwd' ];
2009-04-09 08:41:41 +02:00
$to_write [ 'account_lastpwd_change' ] = time ();
2001-02-17 10:40:29 +01:00
}
2010-09-27 09:51:32 +02:00
if ( $data [ 'mustchangepassword' ] == 1 ) $to_write [ 'account_lastpwd_change' ] = 0 ;
2006-06-08 02:25:57 +02:00
if ( ! ( int ) $data [ 'account_id' ] || ! $this -> id2name ( $data [ 'account_id' ]))
2001-06-26 11:51:16 +02:00
{
2006-06-08 02:25:57 +02:00
if ( $to_write [ 'account_id' ] < 0 ) $to_write [ 'account_id' ] *= - 1 ;
2006-06-08 23:18:46 +02:00
if ( ! isset ( $to_write [ 'account_pwd' ])) $to_write [ 'account_pwd' ] = '' ; // is NOT NULL!
if ( ! isset ( $to_write [ 'account_status' ])) $to_write [ 'account_status' ] = '' ; // is NOT NULL!
2011-08-31 14:17:34 +02:00
// postgres requires the auto-id field to be unset!
2006-07-14 21:59:16 +02:00
if ( isset ( $to_write [ 'account_id' ]) && ! $to_write [ 'account_id' ]) unset ( $to_write [ 'account_id' ]);
2006-06-07 01:42:36 +02:00
if ( ! in_array ( $to_write [ 'account_type' ], array ( 'u' , 'g' )) ||
! $this -> db -> insert ( $this -> table , $to_write , false , __LINE__ , __FILE__ )) return false ;
2009-03-05 16:17:45 +01:00
2006-06-08 02:25:57 +02:00
if ( ! ( int ) $data [ 'account_id' ])
{
$data [ 'account_id' ] = $this -> db -> get_last_insert_id ( $this -> table , 'account_id' );
if ( $data [ 'account_type' ] == 'g' ) $data [ 'account_id' ] *= - 1 ;
}
2006-06-07 01:42:36 +02:00
}
2006-06-17 20:47:02 +02:00
else // update of existing account
2006-06-07 01:42:36 +02:00
{
2006-06-08 02:25:57 +02:00
unset ( $to_write [ 'account_id' ]);
if ( ! $this -> db -> update ( $this -> table , $to_write , array ( 'account_id' => abs ( $data [ 'account_id' ])), __LINE__ , __FILE__ ))
{
return false ;
}
2006-06-17 20:47:02 +02:00
}
2012-11-15 16:26:35 +01:00
// store group-email in mailaccounts table
2013-05-23 15:48:51 +02:00
if ( $data [ 'account_id' ] < 0 && class_exists ( 'emailadmin_smtp_sql' , isset ( $data [ 'account_email' ])))
2012-11-15 16:26:35 +01:00
{
2012-11-22 15:58:34 +01:00
try {
2014-06-25 14:43:11 +02:00
if ( isset ( $GLOBALS [ 'egw_info' ][ 'apps' ]) && ! isset ( $GLOBALS [ 'egw_info' ][ 'apps' ][ 'emailadmin' ]) ||
isset ( $GLOBALS [ 'egw_setup' ]) && ! in_array ( emailadmin_smtp_sql :: TABLE , $this -> db -> table_names ( true )))
{
// cant store email, if emailadmin not (yet) installed
}
elseif ( empty ( $data [ 'account_email' ]))
2012-11-22 15:58:34 +01:00
{
$this -> db -> delete ( emailadmin_smtp_sql :: TABLE , array (
'account_id' => $data [ 'account_id' ],
'mail_type' => emailadmin_smtp_sql :: TYPE_ALIAS ,
), __LINE__ , __FILE__ , emailadmin_smtp_sql :: APP );
}
else
{
$this -> db -> insert ( emailadmin_smtp_sql :: TABLE , array (
'mail_value' => $data [ 'account_email' ],
), array (
'account_id' => $data [ 'account_id' ],
'mail_type' => emailadmin_smtp_sql :: TYPE_ALIAS ,
), __LINE__ , __FILE__ , emailadmin_smtp_sql :: APP );
}
2012-11-15 16:26:35 +01:00
}
2014-06-25 14:43:11 +02:00
// ignore not (yet) existing mailaccounts table (does NOT work in PostgreSQL, because of transaction!)
2014-05-08 15:35:22 +02:00
catch ( egw_exception_db $e ) {
unset ( $e );
}
2012-11-15 16:26:35 +01:00
}
2006-06-07 01:42:36 +02:00
return $data [ 'account_id' ];
}
2009-03-05 16:17:45 +01:00
2006-06-07 01:42:36 +02:00
/**
* Delete one account , deletes also all acl - entries for that account
*
2014-10-22 19:10:12 +02:00
* @ param int $account_id numeric account_id
2006-06-07 01:42:36 +02:00
* @ return boolean true on success , false otherwise
*/
function delete ( $account_id )
{
if ( ! ( int ) $account_id ) return false ;
2009-03-05 16:17:45 +01:00
2006-06-17 20:47:02 +02:00
$contact_id = $this -> id2name ( $account_id , 'person_id' );
2001-02-20 15:06:32 +01:00
2006-06-17 20:47:02 +02:00
if ( ! $this -> db -> delete ( $this -> table , array ( 'account_id' => abs ( $account_id )), __LINE__ , __FILE__ ))
{
return false ;
}
if ( $contact_id )
{
2007-10-11 08:24:57 +02:00
$GLOBALS [ 'egw' ] -> contacts -> delete ( $contact_id , false ); // false = allow to delete accounts (!)
2006-06-17 20:47:02 +02:00
}
return true ;
2006-06-07 01:42:36 +02:00
}
2001-02-20 15:06:32 +01:00
2006-06-07 01:42:36 +02:00
/**
* Get all memberships of an account $accountid / groups the account is a member off
*
* @ param int $account_id numeric account - id
* @ return array / boolean array with account_id => account_lid pairs or false if account not found
*/
function memberships ( $account_id )
{
if ( ! ( int ) $account_id ) return false ;
2001-02-20 15:06:32 +01:00
2006-06-07 01:42:36 +02:00
$memberships = array ();
if (( $gids = $GLOBALS [ 'egw' ] -> acl -> get_location_list_for_id ( 'phpgw_group' , 1 , $account_id )))
{
foreach ( $gids as $gid )
2001-06-26 11:51:16 +02:00
{
2006-06-07 01:42:36 +02:00
$memberships [( string ) $gid ] = $this -> id2name ( $gid );
2001-06-26 11:51:16 +02:00
}
2006-06-07 01:42:36 +02:00
}
//echo "accounts::memberships($account_id)"; _debug_array($memberships);
return $memberships ;
}
2001-06-26 11:51:16 +02:00
2006-06-07 01:42:36 +02:00
/**
* Sets the memberships of the account this class is instanciated for
*
* @ param array $groups array with gidnumbers
* @ param int $account_id numerical account - id
*/
function set_memberships ( $groups , $account_id )
{
if ( ! ( int ) $account_id ) return ;
2009-03-05 16:17:45 +01:00
2006-06-07 01:42:36 +02:00
$acl =& CreateObject ( 'phpgwapi.acl' , $account_id );
$acl -> read_repository ();
$acl -> delete ( 'phpgw_group' , false );
2003-08-28 16:31:11 +02:00
2006-06-07 01:42:36 +02:00
foreach ( $groups as $group )
{
$acl -> add ( 'phpgw_group' , $group , 1 );
2001-02-14 20:27:37 +01:00
}
2006-06-07 01:42:36 +02:00
$acl -> save_repository ();
}
2001-02-14 20:27:37 +01:00
2006-06-07 01:42:36 +02:00
/**
* Get all members of the group $accountid
*
* @ param int / string $account_id numeric account - id
* @ return array with account_id => account_lid pairs
*/
function members ( $account_id )
{
2013-10-18 16:31:46 +02:00
if ( ! is_numeric ( $account_id )) $account_id = $this -> name2id ( $account_id );
2012-07-16 14:27:01 +02:00
2006-06-07 01:42:36 +02:00
$members = array ();
2012-07-16 14:27:01 +02:00
foreach ( $this -> db -> select ( $this -> table , 'account_id,account_lid' ,
$this -> db -> expression ( acl :: TABLE , array (
'acl_appname' => 'phpgw_group' ,
'acl_location' => $account_id ,
2012-07-16 14:36:53 +02:00
)), __LINE__ , __FILE__ , false , '' , false , 0 ,
'JOIN ' . acl :: TABLE . ' ON account_id=acl_account'
2012-07-16 14:27:01 +02:00
) as $row )
2001-06-26 11:51:16 +02:00
{
2012-07-16 14:27:01 +02:00
$members [ $row [ 'account_id' ]] = $row [ 'account_lid' ];
2001-02-20 15:06:32 +01:00
}
2006-06-07 01:42:36 +02:00
//echo "accounts::members($accountid)"; _debug_array($members);
return $members ;
}
2001-02-20 15:06:32 +01:00
2006-06-07 01:42:36 +02:00
/**
* Set the members of a group
2009-03-05 16:17:45 +01:00
*
2006-06-07 01:42:36 +02:00
* @ param array $members array with uidnumber or uid ' s
* @ param int $gid gidnumber of group to set
*/
function set_members ( $members , $gid )
{
2006-10-19 19:11:35 +02:00
//echo "<p align=right>accounts::set_members(".print_r($members,true).",$gid)</p>\n";
$GLOBALS [ 'egw' ] -> acl -> delete_repository ( 'phpgw_group' , $gid , false );
2009-03-05 16:17:45 +01:00
2006-07-13 21:54:02 +02:00
if ( is_array ( $members ))
2001-02-20 15:06:32 +01:00
{
2006-07-13 21:54:02 +02:00
foreach ( $members as $id )
{
$GLOBALS [ 'egw' ] -> acl -> add_repository ( 'phpgw_group' , $gid , $id , 1 );
}
2006-06-07 01:42:36 +02:00
}
}
2001-08-15 04:14:18 +02:00
2006-06-07 01:42:36 +02:00
/**
2014-05-08 14:30:03 +02:00
* Searches / lists accounts : users and / or groups
2009-03-05 16:17:45 +01:00
*
2014-05-08 14:30:03 +02:00
* @ param array with the following keys :
* @ param $param [ 'type' ] string / int 'accounts' , 'groups' , 'owngroups' ( groups the user is a member of ), 'both' ,
* 'groupmember' or 'groupmembers+memberships'
* or integer group - id for a list of members of that group
* @ param $param [ 'start' ] int first account to return ( returns offset or max_matches entries ) or all if not set
* @ param $param [ 'order' ] string column to sort after , default account_lid if unset
2015-03-27 09:13:41 +01:00
* @ param $param [ 'sort' ] string 'ASC' or 'DESC' , default 'ASC' if not set
2014-05-08 14:30:03 +02:00
* @ param $param [ 'query' ] string to search for , no search if unset or empty
* @ param $param [ 'query_type' ] string :
* 'all' - query all fields for containing $param [ query ]
* 'start' - query all fields starting with $param [ query ]
* 'exact' - query all fields for exact $param [ query ]
* 'lid' , 'firstname' , 'lastname' , 'email' - query only the given field for containing $param [ query ]
* @ param $param [ 'offset' ] int - number of matches to return if start given , default use the value in the prefs
* @ param $param [ 'objectclass' ] boolean return objectclass ( es ) under key 'objectclass' in each account
* @ return array with account_id => data pairs , data is an array with account_id , account_lid , account_firstname ,
* account_lastname , person_id ( id of the linked addressbook entry ), account_status , account_expires , account_primary_group
2006-06-07 01:42:36 +02:00
*/
2014-05-08 14:30:03 +02:00
function search ( $param )
2006-06-07 01:42:36 +02:00
{
2006-06-17 20:47:02 +02:00
static $order2contact = array (
'account_firstname' => 'n_given' ,
'account_lastname' => 'n_family' ,
'account_email' => 'contact_email' ,
);
2010-04-20 14:16:34 +02:00
// fetch order of account_fullname from common::display_fullname
2014-05-08 14:30:03 +02:00
if ( strpos ( $param [ 'order' ], 'account_fullname' ) !== false )
2010-04-20 14:16:34 +02:00
{
2014-05-08 14:30:03 +02:00
$param [ 'order' ] = str_replace ( 'account_fullname' , preg_replace ( '/[ ,]+/' , ',' , str_replace ( array ( '[' , ']' ), '' ,
common :: display_fullname ( 'account_lid' , 'account_firstname' , 'account_lastname' ))), $param [ 'order' ]);
2010-04-20 14:16:34 +02:00
}
2014-05-08 14:30:03 +02:00
$order = str_replace ( array_keys ( $order2contact ), array_values ( $order2contact ), $param [ 'order' ]);
2014-01-21 18:15:08 +01:00
// allways add 'account_lid', as it is only valid one for groups
if ( strpos ( $order , 'account_lid' ) === false )
{
2014-01-21 18:18:29 +01:00
$order .= ( $order ? ',' : '' ) . 'account_lid' ;
2014-01-21 18:15:08 +01:00
}
2014-05-08 14:30:03 +02:00
if ( $param [ 'sort' ]) $order = implode ( ' ' . $param [ 'sort' ] . ',' , explode ( ',' , $order )) . ' ' . $param [ 'sort' ];
2009-03-05 16:17:45 +01:00
2015-03-31 16:18:27 +02:00
$search_cols = array ( 'account_lid' , 'n_family' , 'n_given' , 'email' );
$join = $this -> contacts_join ;
$email_cols = array ( 'email' );
// Add in group email searching
if ( ! isset ( $GLOBALS [ 'egw_setup' ]) || in_array ( emailadmin_smtp_sql :: TABLE , $this -> db -> table_names ( true )))
{
$email_cols = array ( 'coalesce(' . $this -> contacts_table . '.contact_email,' . emailadmin_smtp_sql :: TABLE . '.mail_value) as email' );
2015-07-29 12:35:26 +02:00
if ( $this -> db -> Type == 'mysql' && ! preg_match ( '/[\x80-\xFF]/' , $param [ 'query' ]))
{
$search_cols [] = emailadmin_smtp_sql :: TABLE . '.mail_value' ;
}
2015-03-31 16:18:27 +02:00
$join .= ' LEFT JOIN ' . emailadmin_smtp_sql :: TABLE . ' ON ' . $this -> table . '.account_id=-' . emailadmin_smtp_sql :: TABLE . '.account_id AND mail_type=' . emailadmin_smtp_sql :: TYPE_ALIAS ;
}
2014-05-08 14:30:03 +02:00
$filter = array ();
switch ( $param [ 'type' ])
2001-02-20 15:06:32 +01:00
{
2006-06-07 01:42:36 +02:00
case 'accounts' :
2014-05-08 14:30:03 +02:00
$filter [ 'owner' ] = 0 ;
2006-06-07 01:42:36 +02:00
break ;
case 'groups' :
2014-05-08 15:35:22 +02:00
$filter [] = " account_type='g' " ;
2014-05-08 14:30:03 +02:00
break ;
case 'owngroups' :
$filter [ 'account_id' ] = array_map ( 'abs' , $this -> frontend -> memberships ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ], true ));
2014-05-08 15:35:22 +02:00
$filter [] = " account_type='g' " ;
2014-05-08 14:30:03 +02:00
break ;
case 'groupmembers' :
case 'groupmembers+memberships' :
$members = array ();
foreach (( array ) $this -> memberships ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ], true ) as $grp )
{
$members = array_unique ( array_merge ( $members , ( array ) $this -> members ( $grp , true )));
if ( $param [ 'type' ] == 'groupmembers+memberships' ) $members [] = abs ( $grp );
}
$filter [ 'account_id' ] = $members ;
2006-06-07 01:42:36 +02:00
break ;
default :
2014-05-08 14:30:03 +02:00
if ( is_numeric ( $param [ 'type' ]))
{
2014-10-22 19:10:12 +02:00
$filter [ 'account_id' ] = $this -> frontend -> members ( $param [ 'type' ], true , $param [ 'active' ]);
2014-05-08 15:35:22 +02:00
$filter [ 'owner' ] = 0 ;
2014-05-08 14:30:03 +02:00
break ;
}
// fall-through
2006-06-17 20:47:02 +02:00
case 'both' :
2014-05-08 14:30:03 +02:00
$filter [] = " (egw_addressbook.contact_owner=0 OR egw_addressbook.contact_owner IS NULL) " ;
2006-06-17 20:47:02 +02:00
break ;
2001-02-20 15:06:32 +01:00
}
2014-05-08 15:35:22 +02:00
// fix ambigous account_id (used in accounts and contacts table)
if ( array_key_exists ( 'account_id' , $filter ))
{
if ( ! $filter [ 'account_id' ]) // eg. group without members (would give SQL error)
{
$this -> total = 0 ;
return array ();
}
$filter [] = $this -> db -> expression ( $this -> table , $this -> table . '.' , array (
'account_id' => $filter [ 'account_id' ],
));
unset ( $filter [ 'account_id' ]);
}
2014-05-08 14:30:03 +02:00
if ( $param [ 'active' ])
2013-01-25 14:21:31 +01:00
{
$filter [] = str_replace ( 'UNIX_TIMESTAMP(NOW())' , time (), addressbook_sql :: ACOUNT_ACTIVE_FILTER );
}
2006-06-17 20:47:02 +02:00
$criteria = array ();
2014-05-08 14:30:03 +02:00
$wildcard = $param [ 'query_type' ] == 'start' || $param [ 'query_type' ] == 'exact' ? '' : '%' ;
if (( $query = $param [ 'query' ]))
2001-05-06 15:04:04 +02:00
{
2014-05-08 14:30:03 +02:00
switch ( $param [ 'query_type' ])
2004-07-10 10:22:07 +02:00
{
2006-06-07 01:42:36 +02:00
case 'start' :
2006-06-17 20:47:02 +02:00
$query .= '*' ;
2006-06-07 01:42:36 +02:00
// fall-through
2006-06-17 20:47:02 +02:00
case 'all' :
default :
2006-06-07 01:42:36 +02:00
case 'exact' :
2015-03-31 16:18:27 +02:00
foreach ( $search_cols as $col )
2006-06-17 20:47:02 +02:00
{
$criteria [ $col ] = $query ;
}
2006-06-07 01:42:36 +02:00
break ;
2006-06-17 20:47:02 +02:00
case 'account_firstname' :
2006-06-07 01:42:36 +02:00
case 'firstname' :
2006-06-17 20:47:02 +02:00
$criteria [ 'n_given' ] = $query ;
break ;
case 'account_lastname' :
2006-06-07 01:42:36 +02:00
case 'lastname' :
2006-06-17 20:47:02 +02:00
$criteria [ 'n_family' ] = $query ;
break ;
case 'account_lid' :
2006-06-07 01:42:36 +02:00
case 'lid' :
2006-06-17 20:47:02 +02:00
$criteria [ 'account_lid' ] = $query ;
break ;
case 'account_email' :
2006-06-07 01:42:36 +02:00
case 'email' :
2006-06-17 20:47:02 +02:00
$criteria [ 'email' ] = $query ;
2015-03-31 16:18:27 +02:00
// Group email
if ( in_array ( emailadmin_smtp_sql :: TABLE , $this -> db -> table_names ( true )))
{
$criteria [ emailadmin_smtp_sql :: TABLE . '.mail_value' ] = $query ;
}
2006-06-07 01:42:36 +02:00
break ;
2004-07-10 10:22:07 +02:00
}
2006-06-07 01:42:36 +02:00
}
2008-04-10 11:06:00 +02:00
if ( ! is_object ( $GLOBALS [ 'egw' ] -> contacts )) throw new exception ( 'No $GLOBALS[egw]->contacts!' );
2009-03-05 16:17:45 +01:00
$accounts = array ();
2014-05-08 14:30:03 +02:00
foreach (( array ) $GLOBALS [ 'egw' ] -> contacts -> search ( $criteria ,
2015-03-31 16:18:27 +02:00
array_merge ( array ( 1 , 'n_given' , 'n_family' , 'id' , 'created' , 'modified' , $this -> table . '.account_id AS account_id' ), $email_cols ),
2015-08-13 17:25:31 +02:00
$order , " account_lid,account_type,account_status,account_expires,account_primary_group,account_description " ,
2014-05-08 14:30:03 +02:00
$wildcard , false , $query [ 0 ] == '!' ? 'AND' : 'OR' ,
$param [ 'offset' ] ? array ( $param [ 'start' ], $param [ 'offset' ]) : ( is_null ( $param [ 'start' ]) ? false : $param [ 'start' ]),
2015-03-31 16:18:27 +02:00
$filter , $join ) as $contact )
2006-06-07 01:42:36 +02:00
{
2009-03-05 16:17:45 +01:00
if ( $contact )
2006-06-17 20:47:02 +02:00
{
2014-05-08 14:30:03 +02:00
$account_id = ( $contact [ 'account_type' ] == 'g' ? - 1 : 1 ) * $contact [ 'account_id' ];
$accounts [ $account_id ] = array (
'account_id' => $account_id ,
2006-06-17 20:47:02 +02:00
'account_lid' => $contact [ 'account_lid' ],
'account_type' => $contact [ 'account_type' ],
'account_firstname' => $contact [ 'n_given' ],
'account_lastname' => $contact [ 'n_family' ],
'account_email' => $contact [ 'email' ],
'person_id' => $contact [ 'id' ],
2008-04-10 11:06:00 +02:00
'account_status' => $contact [ 'account_status' ],
2013-07-03 18:26:18 +02:00
'account_expires' => $contact [ 'account_expires' ],
'account_primary_group' => $contact [ 'account_primary_group' ],
2012-03-14 16:22:51 +01:00
// addressbook_bo::search() returns everything in user-time, need to convert to server-time
'account_created' => egw_time :: user2server ( $contact [ 'created' ]),
'account_modified' => egw_time :: user2server ( $contact [ 'modified' ]),
2015-08-13 17:25:31 +02:00
'account_description' => $contact [ 'account_description' ],
2006-06-17 20:47:02 +02:00
);
}
2006-06-07 01:42:36 +02:00
}
2006-06-17 20:47:02 +02:00
$this -> total = $GLOBALS [ 'egw' ] -> contacts -> total ;
2014-05-08 15:35:22 +02:00
//error_log(__METHOD__."(".array2string($param).") returning ".count($accounts).'/'.$this->total);
2006-06-07 01:42:36 +02:00
return $accounts ;
}
2001-09-06 00:46:47 +02:00
2006-06-07 01:42:36 +02:00
/**
2006-06-17 20:47:02 +02:00
* convert an alphanumeric account - value ( account_lid , account_email , account_fullname ) to the account_id
2006-06-07 01:42:36 +02:00
*
* Please note :
* - if a group and an user have the same account_lid the group will be returned ( LDAP only )
* - if multiple user have the same email address , the returned user is undefined
2009-03-05 16:17:45 +01:00
*
2006-06-07 01:42:36 +02:00
* @ param string $name value to convert
2014-10-22 19:10:12 +02:00
* @ param string $which = 'account_lid' type of $name : account_lid ( default ), account_email , person_id , account_fullname
2006-06-07 01:42:36 +02:00
* @ param string $account_type u = user , g = group , default null = try both
* @ return int / false numeric account_id or false on error ( $name not found )
*/
function name2id ( $name , $which = 'account_lid' , $account_type = null )
{
2006-06-17 20:47:02 +02:00
if ( $account_type === 'g' && $which != 'account_lid' ) return false ;
2006-06-07 01:42:36 +02:00
$where = array ();
2006-06-17 20:47:02 +02:00
$cols = 'account_id' ;
2006-06-07 01:42:36 +02:00
switch ( $which )
2001-09-06 00:46:47 +02:00
{
2006-06-07 01:42:36 +02:00
case 'account_fullname' :
2006-06-17 20:47:02 +02:00
$table = $this -> contacts_table ;
$where [ 'n_fn' ] = $name ;
2006-06-07 01:42:36 +02:00
break ;
2006-06-17 20:47:02 +02:00
case 'account_email' :
$table = $this -> contacts_table ;
$where [ 'contact_email' ] = $name ;
break ;
case 'person_id' :
$table = $this -> contacts_table ;
$where [ 'contact_id' ] = $name ;
2009-03-05 16:17:45 +01:00
break ;
2006-06-07 01:42:36 +02:00
default :
2006-06-17 20:47:02 +02:00
$table = $this -> table ;
$cols .= ',account_type' ;
2006-06-07 01:42:36 +02:00
$where [ $which ] = $name ;
2002-03-14 02:42:15 +01:00
}
2006-06-07 01:42:36 +02:00
if ( $account_type )
2005-11-02 12:45:52 +01:00
{
2006-06-07 01:42:36 +02:00
$where [ 'account_type' ] = $account_type ;
2005-11-02 12:45:52 +01:00
}
2007-08-24 12:53:26 +02:00
else
{
2013-01-25 18:52:28 +01:00
$where [] = 'account_id IS NOT NULL' . // otherwise contacts with eg. the same email hide the accounts!
( $table == $this -> contacts_table ? " AND contact_tid != 'D' " : '' ); // ignore deleted accounts contact-data
2007-08-24 12:53:26 +02:00
}
2013-10-15 13:42:28 +02:00
if ( ! ( $rs = $this -> db -> select ( $table , $cols , $where , __LINE__ , __FILE__ )) || ! ( $row = $rs -> fetch ()))
{
2013-10-18 16:31:46 +02:00
//error_log(__METHOD__."('$name', '$which', ".array2string($account_type).") db->select('$table', '$cols', ".array2string($where).") returned ".array2string($rs).' '.function_backtrace());
2013-10-15 13:42:28 +02:00
return false ;
}
2008-03-13 20:37:09 +01:00
return ( $row [ 'account_type' ] == 'g' ? - 1 : 1 ) * $row [ 'account_id' ];
2006-06-07 01:42:36 +02:00
}
2009-03-05 16:17:45 +01:00
2007-12-13 03:32:44 +01:00
/**
* Convert an numeric account_id to any other value of that account ( account_lid , account_email , ... )
2009-03-05 16:17:45 +01:00
*
2007-12-13 03:32:44 +01:00
* Uses the read method to fetch all data .
*
* @ param int $account_id numerica account_id
2014-10-22 19:10:12 +02:00
* @ param string $which = 'account_lid' type to convert to : account_lid ( default ), account_email , ...
2007-12-13 03:32:44 +01:00
* @ return string / false converted value or false on error ( $account_id not found )
*/
function id2name ( $account_id , $which = 'account_lid' )
{
return $this -> frontend -> id2name ( $account_id , $which );
}
2006-06-07 01:42:36 +02:00
/**
* Update the last login timestamps and the IP
*
* @ param int $account_id
* @ param string $ip
* @ return int lastlogin time
*/
function update_lastlogin ( $account_id , $ip )
{
2009-06-08 18:21:14 +02:00
$previous_login = $this -> db -> select ( $this -> table , 'account_lastlogin' , array ( 'account_id' => abs ( $account_id )), __LINE__ , __FILE__ ) -> fetchColumn ();
2006-06-07 01:42:36 +02:00
$this -> db -> update ( $this -> table , array (
'account_lastloginfrom' => $ip ,
'account_lastlogin' => time (),
), array (
'account_id' => abs ( $account_id ),
), __LINE__ , __FILE__ );
2009-03-05 16:17:45 +01:00
2006-06-07 01:42:36 +02:00
return $previous_login ;
2002-03-14 02:42:15 +01:00
}
2006-06-07 01:42:36 +02:00
}