2013-07-16 07:36:48 +02:00
< ? php
class ModelSaasCustomer extends Model
{
2013-07-23 22:44:34 +02:00
public function get ( $id = - 1 ) {
if ( $id > 0 ) {
$query = $this -> db -> query ( " SELECT * FROM " . TABLE_CUSTOMER_SETTINGS . " WHERE id=? " , array ( $id ));
if ( $query -> num_rows > 0 ) { return $query -> row ; }
}
$query = $this -> db -> query ( " SELECT * FROM " . TABLE_CUSTOMER_SETTINGS . " ORDER BY domain ASC " );
if ( $query -> num_rows > 0 ) { return $query -> rows ; }
return array ();
}
public function delete ( $id = 0 , $description = '' ) {
if ( $id == 0 ) { return 0 ; }
$query = $this -> db -> query ( " DELETE FROM " . TABLE_CUSTOMER_SETTINGS . " WHERE id=? " , array ( $id ));
$rc = $this -> db -> countAffected ();
LOGGER ( " remove ldap entry: # $id , $description (rc= $rc ) " );
return $rc ;
}
public function add ( $arr = array ()) {
$branding_logo = '' ;
if ( ! isset ( $arr [ 'domain' ]) || ! isset ( $arr [ 'branding_text' ])) { return 0 ; }
if ( isset ( $_FILES [ 'branding_logo' ][ 'name' ])) {
$branding_logo = $_FILES [ 'branding_logo' ][ 'name' ];
move_uploaded_file ( $_FILES [ 'branding_logo' ][ 'tmp_name' ], DIR_BASE . " /images/ " . $_FILES [ 'branding_logo' ][ 'name' ]);
}
2013-08-09 22:11:16 +02:00
$query = $this -> db -> query ( " INSERT INTO " . TABLE_CUSTOMER_SETTINGS . " (domain, branding_text, branding_url, branding_logo, support_link, background_colour, text_colour) VALUES (?,?,?,?,?,?,?) " , array ( $arr [ 'domain' ], $arr [ 'branding_text' ], $arr [ 'branding_url' ], $branding_logo , $arr [ 'support_link' ], $arr [ 'background_colour' ], $arr [ 'text_colour' ]));
2013-07-23 22:44:34 +02:00
$rc = $this -> db -> countAffected ();
LOGGER ( " add ldap entry: " . $arr [ 'domain' ] . " / " . $arr [ 'branding_text' ] . " / " . $arr [ 'branding_url' ] . " / " . $arr [ 'support_link' ] . " (rc= $rc ) " );
if ( $rc == 1 ){ return 1 ; }
return 0 ;
}
public function update ( $arr = array ()) {
$branding_logo = '' ;
if ( ! isset ( $arr [ 'id' ]) || ! isset ( $arr [ 'domain' ]) || ! isset ( $arr [ 'branding_text' ])) { return 0 ; }
2013-08-09 15:17:02 +02:00
if ( isset ( $_FILES [ 'branding_logo' ][ 'name' ]) && $_FILES [ 'branding_logo' ][ 'name' ]) {
2013-07-23 22:44:34 +02:00
$branding_logo = $_FILES [ 'branding_logo' ][ 'name' ];
move_uploaded_file ( $_FILES [ 'branding_logo' ][ 'tmp_name' ], DIR_BASE . " /images/ " . $_FILES [ 'branding_logo' ][ 'name' ]);
2013-08-09 22:11:16 +02:00
$query = $this -> db -> query ( " UPDATE " . TABLE_CUSTOMER_SETTINGS . " SET domain=?, branding_text=?, branding_url=?, branding_logo=?, support_link=?, background_colour=?, text_colour=? WHERE id=? " , array ( $arr [ 'domain' ], $arr [ 'branding_text' ], $arr [ 'branding_url' ], $branding_logo , $arr [ 'support_link' ], $arr [ 'background_colour' ], $arr [ 'text_colour' ], $arr [ 'id' ]));
2013-07-23 22:44:34 +02:00
}
else {
2013-08-09 22:11:16 +02:00
$query = $this -> db -> query ( " UPDATE " . TABLE_CUSTOMER_SETTINGS . " SET domain=?, branding_text=?, branding_url=?, support_link=?, background_colour=?, text_colour=? WHERE id=? " , array ( $arr [ 'domain' ], $arr [ 'branding_text' ], $arr [ 'branding_url' ], $arr [ 'support_link' ], $arr [ 'background_colour' ], $arr [ 'text_colour' ], $arr [ 'id' ]));
2013-07-23 22:44:34 +02:00
}
return $this -> db -> countAffected ();
}
2013-07-16 07:36:48 +02:00
public function get_customer_settings_by_email () {
$data = array (
'branding_text' => BRANDING_TEXT ,
'branding_url' => BRANDING_URL ,
2013-07-18 17:13:49 +02:00
'branding_logo' => BRANDING_LOGO ,
2013-07-16 07:36:48 +02:00
'support_link' => SUPPORT_LINK ,
2013-08-09 22:11:16 +02:00
'background_colour' => BRANDING_BACKGROUND_COLOUR ,
'text_colour' => BRANDING_TEXT_COLOUR
2013-07-16 07:36:48 +02:00
);
if ( ! isset ( $_SESSION [ 'email' ]) || ! strchr ( $_SESSION [ 'email' ], '@' ) ) { return $data ; }
list ( $user , $domain ) = explode ( " @ " , $_SESSION [ 'email' ]);
2013-07-23 22:44:34 +02:00
if ( MEMCACHED_ENABLED ) {
$cache_key = sha1 ( " customer_settings: " . $domain );
$memcache = Registry :: get ( 'memcache' );
$m = $memcache -> get ( $cache_key );
if ( isset ( $m [ 'data' ])) { return unserialize ( $m [ 'data' ]); }
}
2013-07-16 07:36:48 +02:00
$query = $this -> db -> query ( " SELECT * FROM " . TABLE_CUSTOMER_SETTINGS . " WHERE domain=(SELECT mapped FROM " . TABLE_DOMAIN . " WHERE domain=?) " , array ( $domain ));
if ( $query -> num_rows > 0 ) {
if ( $query -> row [ 'branding_text' ]) { $data [ 'branding_text' ] = $query -> row [ 'branding_text' ]; }
if ( $query -> row [ 'branding_url' ]) { $data [ 'branding_url' ] = $query -> row [ 'branding_url' ]; }
if ( $query -> row [ 'branding_logo' ]) { $data [ 'branding_logo' ] = $query -> row [ 'branding_logo' ]; }
if ( $query -> row [ 'support_link' ]) { $data [ 'support_link' ] = $query -> row [ 'support_link' ]; }
2013-08-09 22:11:16 +02:00
if ( $query -> row [ 'background_colour' ]) { $data [ 'background_colour' ] = $query -> row [ 'background_colour' ]; }
if ( $query -> row [ 'text_colour' ]) { $data [ 'text_colour' ] = $query -> row [ 'text_colour' ]; }
2013-07-16 07:36:48 +02:00
}
2013-07-23 22:44:34 +02:00
if ( MEMCACHED_ENABLED && $cache_key ) {
$memcache -> add ( $cache_key , array ( 'data' => serialize ( $data )), 0 , MEMCACHED_TTL );
}
2013-07-16 07:36:48 +02:00
return $data ;
}
2013-07-24 22:14:05 +02:00
public function online ( $username = '' ) {
if ( $username == '' ) { return 0 ; }
$query = $this -> db -> query ( " INSERT INTO " . TABLE_ONLINE . " (username, ts, last_activity, ipaddr) VALUES(?,?,?,?) " , array ( $username , NOW , NOW , $_SERVER [ 'REMOTE_ADDR' ]));
if ( $this -> db -> countAffected () == 0 ) {
2013-08-09 10:13:54 +02:00
$query = $this -> db -> query ( " UPDATE " . TABLE_ONLINE . " SET ts=?, last_activity=? WHERE username=? AND ipaddr=? " , array ( NOW , NOW , $username , $_SERVER [ 'REMOTE_ADDR' ]));
2013-07-24 22:14:05 +02:00
}
return 1 ;
}
public function offline ( $username = '' ) {
if ( $username == '' ) { return 0 ; }
2013-07-25 11:56:52 +02:00
$query = $this -> db -> query ( " DELETE FROM " . TABLE_ONLINE . " WHERE username=? AND ipaddr=? " , array ( $username , $_SERVER [ 'REMOTE_ADDR' ]));
2013-07-24 22:14:05 +02:00
return 1 ;
}
public function count_online () {
2013-08-07 16:43:00 +02:00
$query = $this -> db -> query ( " DELETE FROM " . TABLE_ONLINE . " WHERE last_activity < ? " , array ( NOW - 3600 ));
2013-07-24 22:14:05 +02:00
$query = $this -> db -> query ( " SELECT COUNT(*) AS num FROM " . TABLE_ONLINE );
return $query -> row [ 'num' ];
}
public function get_online_users () {
2013-08-01 22:28:42 +02:00
$query = $this -> db -> query ( " DELETE FROM " . TABLE_ONLINE . " WHERE last_activity < ? " , array ( NOW - 3600 ));
2013-07-24 22:14:05 +02:00
$query = $this -> db -> query ( " SELECT * FROM " . TABLE_ONLINE . " ORDER BY username ASC " );
return $query -> rows ;
}
2013-07-16 07:36:48 +02:00
}
?>