2012-06-22 12:30:55 +02:00
< ? php
class ModelGroupGroup extends Model {
public function get_groups ( $search = '' , $page = 0 , $page_len = 0 , $sort = 'groupname' , $order = 0 ) {
$where_cond = '' ;
$_order = " " ;
$groups = array ();
$Q = array ();
$limit = " " ;
$from = ( int ) $page * ( int ) $page_len ;
$search = preg_replace ( " / \ s { 1,}/ " , " " , $search ) . '%' ;
if ( $search ){
$where_cond .= " WHERE `groupname` like ? " ;
array_push ( $Q , $search );
}
/* sort order */
if ( $order == 0 ) { $order = " ASC " ; }
else { $order = " DESC " ; }
$_order = " ORDER BY ` $sort ` $order " ;
if ( $page_len > 0 ) { $limit = " LIMIT " . ( int ) $from . " , " . ( int ) $page_len ; }
$query = $this -> db -> query ( " SELECT `id`, `groupname` FROM ` " . TABLE_GROUP . " ` $where_cond $_order $limit " , $Q );
foreach ( $query -> rows as $q ) {
$groups [] = array (
'id' => $q [ 'id' ],
'groupname' => $q [ 'groupname' ]
);
}
return $groups ;
}
public function get_emails_by_group_id ( $id = 0 ) {
$emails = '' ;
2014-07-05 17:09:38 +02:00
$query = $this -> db -> query ( " SELECT `email` FROM ` " . TABLE_GROUP_USER . " ` WHERE id=? " , array ( $id ));
foreach ( $query -> rows as $q ) {
$emails .= $q [ 'email' ] . " \n " ;
}
return preg_replace ( " / \n $ / " , " " , $emails );
}
public function get_assigned_emails_by_group_id ( $id = 0 ) {
$emails = '' ;
2012-06-22 12:30:55 +02:00
$query = $this -> db -> query ( " SELECT `email` FROM ` " . TABLE_GROUP_EMAIL . " ` WHERE id=? " , array ( $id ));
foreach ( $query -> rows as $q ) {
$emails .= $q [ 'email' ] . " \n " ;
}
return preg_replace ( " / \n $ / " , " " , $emails );
}
public function count_groups ( $search = '' ) {
$where_cond = " " ;
$Q = array ();
2015-07-02 14:35:45 +02:00
if ( $search ) {
$search = preg_replace ( " / \ s { 1,}/ " , " " , $search ) . '%' ;
2012-06-22 12:30:55 +02:00
$where_cond .= " WHERE `groupname` like '?' " ;
array_push ( $Q , $search );
}
$query = $this -> db -> query ( " SELECT COUNT(*) AS num FROM ` " . TABLE_GROUP . " ` $where_cond " , $Q );
2015-07-02 14:35:45 +02:00
return $query -> row [ 'num' ];
2012-06-22 12:30:55 +02:00
}
public function add_group ( $group = array ()) {
if ( ! isset ( $group [ 'groupname' ]) || $group [ 'groupname' ] == " " ) { return - 1 ; }
$query = $this -> db -> query ( " INSERT INTO ` " . TABLE_GROUP . " ` (groupname) VALUES(?) " , array ( $group [ 'groupname' ]) );
if ( $query -> error == 1 || $this -> db -> countAffected () == 0 ){ return $group [ 'groupname' ]; }
$gid = $this -> db -> getLastId ();
$emails = explode ( " \n " , $group [ 'email' ]);
foreach ( $emails as $email ) {
$email = rtrim ( $email );
if ( validemail ( $email )) {
2014-07-05 17:09:38 +02:00
$query = $this -> db -> query ( " INSERT INTO ` " . TABLE_GROUP_USER . " ` (id, email) VALUES(?,?) " , array ( $gid , $email ));
2012-06-22 12:30:55 +02:00
}
}
2014-07-05 17:09:38 +02:00
$emails = explode ( " \n " , $group [ 'assigned_email' ]);
foreach ( $emails as $email ) {
$email = rtrim ( $email );
if ( validemail ( $email )) {
$query = $this -> db -> query ( " INSERT INTO ` " . TABLE_GROUP_EMAIL . " ` (id, email) VALUES(?,?) " , array ( $gid , $email ));
}
}
2012-06-22 12:30:55 +02:00
LOGGER ( " add group: " . $group [ 'groupname' ] . " , id= " . ( int ) $gid );
return 1 ;
}
public function update_group ( $group = array ()) {
LOGGER ( " update user: " . $group [ 'groupname' ] . " , id= " . ( int ) $group [ 'id' ]);
$query = $this -> db -> query ( " UPDATE ` " . TABLE_GROUP . " ` SET `groupname`=? WHERE id=? " , array ( $group [ 'groupname' ], ( int ) $group [ 'id' ]));
2014-07-05 17:09:38 +02:00
$query = $this -> db -> query ( " DELETE FROM ` " . TABLE_GROUP_USER . " ` WHERE id=? " , array ( $group [ 'id' ]));
2012-06-22 12:30:55 +02:00
$emails = explode ( " \n " , $group [ 'email' ]);
foreach ( $emails as $email ) {
$email = rtrim ( $email );
2014-07-05 17:09:38 +02:00
if ( validemail ( $email )) {
$query = $this -> db -> query ( " INSERT INTO ` " . TABLE_GROUP_USER . " ` (id, email) VALUES(?,?) " , array ( $group [ 'id' ], $email ));
}
}
$query = $this -> db -> query ( " DELETE FROM ` " . TABLE_GROUP_EMAIL . " ` WHERE id=? " , array ( $group [ 'id' ]));
$emails = explode ( " \n " , $group [ 'assigned_email' ]);
foreach ( $emails as $email ) {
$email = rtrim ( $email );
2012-06-22 12:30:55 +02:00
if ( validemail ( $email )) {
$query = $this -> db -> query ( " INSERT INTO ` " . TABLE_GROUP_EMAIL . " ` (id, email) VALUES(?,?) " , array ( $group [ 'id' ], $email ));
}
}
2014-07-05 17:09:38 +02:00
2012-06-22 12:30:55 +02:00
return $this -> db -> countAffected ();
}
public function get_domain_by_id ( $id = 0 ) {
if ( ! is_numeric ( $id ) || ( int ) $id < 0 ){
return array ();
}
$query = $this -> db -> query ( " SELECT * FROM ` " . TABLE_GROUP . " ` WHERE id=? " , array (( int ) $id ));
return $query -> row ;
}
public function delete_group ( $id = 0 ) {
$query = $this -> db -> query ( " DELETE FROM ` " . TABLE_GROUP_EMAIL . " ` WHERE id=? " , array ( $id ));
2014-07-05 17:09:38 +02:00
$query = $this -> db -> query ( " DELETE FROM ` " . TABLE_GROUP_USER . " ` WHERE id=? " , array ( $id ));
2012-06-22 12:30:55 +02:00
$query = $this -> db -> query ( " DELETE FROM ` " . TABLE_GROUP . " ` WHERE id=? " , array (( int ) $id ));
LOGGER ( " remove group: id= $id " );
2014-07-05 17:09:38 +02:00
return 1 ;
2012-06-22 12:30:55 +02:00
}
2012-06-27 11:17:23 +02:00
public function get_emails_by_string ( $s = '' , $page = 0 , $page_len = PAGE_LEN ) {
2013-08-22 11:24:54 +02:00
$emails = array ();
2015-03-10 16:22:19 +01:00
$username_prefix = '' ;
2013-08-22 11:24:54 +02:00
2012-06-27 11:17:23 +02:00
$from = ( int ) $page * ( int ) $page_len ;
2012-06-25 22:14:30 +02:00
2012-06-27 11:17:23 +02:00
if ( strlen ( $s ) < 1 ) { return array (); }
2013-08-22 11:24:54 +02:00
if ( ENABLE_LDAP_AUTH == 1 ) {
2015-03-10 16:22:19 +01:00
if ( LDAP_MAIL_ATTR == 'proxyAddresses' ) { $username_prefix = 'smtp:' ; }
2013-08-22 11:24:54 +02:00
$ldap = new LDAP ( LDAP_HOST , LDAP_HELPER_DN , LDAP_HELPER_PASSWORD );
if ( $ldap -> is_bind_ok ()) {
2015-03-10 16:22:19 +01:00
$query = $ldap -> query ( LDAP_BASE_DN , " (&(objectClass= " . LDAP_ACCOUNT_OBJECTCLASS . " )( " . LDAP_MAIL_ATTR . " = " . $username_prefix . $s . " *)) " , array ());
2013-08-22 11:24:54 +02:00
if ( isset ( $query -> rows )) {
2021-04-15 12:50:41 +02:00
$emails = $this -> model_user_auth -> get_email_array_from_ldap_attr ( $query -> rows , LDAP_DISTRIBUTIONLIST_OBJECTCLASS );
2013-08-22 11:24:54 +02:00
}
}
}
2012-06-25 22:14:30 +02:00
2013-08-22 11:24:54 +02:00
$query = $this -> db -> query ( " SELECT email FROM ` " . TABLE_EMAIL . " ` WHERE email LIKE ? ORDER BY email ASC LIMIT " . ( int ) $from . " , " . ( int ) $page_len , array ( $s . " % " ) );
if ( isset ( $query -> rows )) {
foreach ( $query -> rows as $q ) {
array_push ( $emails , $q [ 'email' ]);
}
}
2013-08-22 22:10:52 +02:00
sort ( $emails );
if ( strlen ( $s ) == 1 && ENABLE_LDAP_AUTH == 1 ) {
$emails = array_slice ( $emails , $page * $page_len , $page_len );
}
2013-08-22 11:24:54 +02:00
return $emails ;
2012-06-25 22:14:30 +02:00
}
2012-06-27 11:17:23 +02:00
public function count_emails ( $s = '' ) {
2013-08-22 22:10:52 +02:00
$count = 0 ;
if ( strlen ( $s ) < 1 ) { return $count ; }
if ( ENABLE_LDAP_AUTH == 1 ) {
$ldap = new LDAP ( LDAP_HOST , LDAP_HELPER_DN , LDAP_HELPER_PASSWORD );
if ( $ldap -> is_bind_ok ()) {
$query = $ldap -> query ( LDAP_BASE_DN , " (&(objectClass= " . LDAP_ACCOUNT_OBJECTCLASS . " )( " . LDAP_MAIL_ATTR . " = " . $s . " *)) " , array ());
if ( isset ( $query -> rows )) {
$count = $query -> num_rows ;
}
}
}
2012-06-27 11:17:23 +02:00
$query = $this -> db -> query ( " SELECT COUNT(*) AS num FROM ` " . TABLE_EMAIL . " ` WHERE email LIKE ? " , array ( $s . " % " ) );
2013-08-22 22:10:52 +02:00
$count += $query -> row [ 'num' ];
return $count ;
2012-06-27 11:17:23 +02:00
}
2012-06-25 22:14:30 +02:00
public function get_groups_by_string ( $s = '' ) {
if ( strlen ( $s ) < 2 ) { return array (); }
$query = $this -> db -> query ( " SELECT groupname FROM ` " . TABLE_GROUP . " ` WHERE groupname LIKE ? ORDER BY groupname ASC " , array ( $s . " % " ) );
if ( isset ( $query -> rows )) { return $query -> rows ; }
return array ();
}
2014-07-05 17:09:38 +02:00
public function get_groups_by_email ( $email = array ()) {
2012-06-25 22:14:30 +02:00
$groups = '' ;
2014-07-05 17:09:38 +02:00
$q = '?' ;
2014-09-22 11:11:54 +02:00
if ( count ( $email ) <= 0 ) { return '' ; }
2014-07-05 17:09:38 +02:00
for ( $i = 1 ; $i < count ( $email ); $i ++ ) {
$q .= ',?' ;
}
2012-06-25 22:14:30 +02:00
2014-07-05 17:09:38 +02:00
$query = $this -> db -> query ( " SELECT ` " . TABLE_GROUP_USER . " `.id, groupname FROM ` " . TABLE_GROUP_USER . " `, ` " . TABLE_GROUP . " ` WHERE ` " . TABLE_GROUP_USER . " `.id=` " . TABLE_GROUP . " `.id AND email IN ( $q ) " , $email );
2012-06-25 22:14:30 +02:00
if ( isset ( $query -> rows )) {
foreach ( $query -> rows as $q ) { $groups .= " \n " . $q [ 'groupname' ]; }
}
return preg_replace ( " /^ \n / " , " " , $groups );
}
2012-06-22 12:30:55 +02:00
}
?>