2012-02-08 23:14:28 +01:00
< ? php
class ModelUserPrefs extends Model {
public function get_user_preferences ( $username = '' ) {
if ( $username == " " ) { return 0 ; }
2013-11-18 19:24:33 +01:00
$session = Registry :: get ( 'session' );
2012-02-08 23:14:28 +01:00
$query = $this -> db -> query ( " SELECT * FROM " . TABLE_USER_SETTINGS . " WHERE username=? " , array ( $username ));
2013-11-18 19:24:33 +01:00
if ( isset ( $query -> row [ 'pagelen' ])) { $session -> set ( " pagelen " , $query -> row [ 'pagelen' ]); } else { $session -> set ( " pagelen " , PAGE_LEN ); }
if ( isset ( $query -> row [ 'theme' ])) { $session -> set ( " theme " , $query -> row [ 'theme' ]); } else { $session -> set ( " theme " , THEME ); }
if ( isset ( $query -> row [ 'lang' ])) { $session -> set ( " lang " , $query -> row [ 'lang' ]); } else { $session -> set ( " lang " , DEFAULT_LANG ); }
2012-02-08 23:14:28 +01:00
return 1 ;
}
public function set_user_preferences ( $username = '' , $prefs = array () ) {
if ( ! isset ( $prefs [ 'pagelen' ]) || ! is_numeric ( $prefs [ 'pagelen' ]) || $prefs [ 'pagelen' ] < 10 || $prefs [ 'pagelen' ] > 100
|| ! isset ( $prefs [ 'theme' ]) || ! preg_match ( " /^([a-zA-Z0-9 \ - \ _]+) $ / " , $prefs [ 'theme' ]) || ! file_exists ( DIR_THEME . $prefs [ 'theme' ]) ) { return 1 ; }
2013-11-18 19:24:33 +01:00
$session = Registry :: get ( 'session' );
2012-02-08 23:14:28 +01:00
$query = $this -> db -> query ( " SELECT COUNT(*) AS num FROM " . TABLE_USER_SETTINGS . " WHERE username=? " , array ( $username ));
if (( int ) @ $query -> row [ 'num' ] == 1 ) {
2013-03-17 21:43:40 +01:00
$query = $this -> db -> query ( " UPDATE " . TABLE_USER_SETTINGS . " SET pagelen=?, theme=?, lang=? WHERE username=? " , array (( int ) @ $prefs [ 'pagelen' ], $prefs [ 'theme' ], $prefs [ 'lang' ], $username ));
2012-02-08 23:14:28 +01:00
}
else {
2013-03-17 21:43:40 +01:00
$query = $this -> db -> query ( " INSERT INTO " . TABLE_USER_SETTINGS . " (username, pagelen, theme, lang) VALUES(?,?,?,?) " , array ( $username , ( int ) @ $prefs [ 'pagelen' ], $prefs [ 'theme' ], $prefs [ 'lang' ]));
2012-02-08 23:14:28 +01:00
}
2013-11-18 19:24:33 +01:00
$session -> set ( " pagelen " , $prefs [ 'pagelen' ]);
$session -> set ( " theme " , $prefs [ 'theme' ]);
$session -> set ( " lang " , $prefs [ 'lang' ]);
2012-02-08 23:14:28 +01:00
LOGGER ( " set user preference " , $username );
return 1 ;
}
2013-10-16 14:55:17 +02:00
public function get_ga_settings ( $username = '' ) {
$data = array ( 'ga_enabled' => 0 , 'ga_secret' => '' );
if ( $username == " " ){ return $data ; }
$GA = new PHPGangsta_GoogleAuthenticator ();
$query = $this -> db -> query ( " SELECT ga_enabled, ga_secret FROM " . TABLE_USER_SETTINGS . " WHERE username=? " , array ( $username ));
if ( isset ( $query -> row [ 'ga_enabled' ])) {
$data [ 'ga_enabled' ] = $query -> row [ 'ga_enabled' ];
$data [ 'ga_secret' ] = $query -> row [ 'ga_secret' ];
if ( $data [ 'ga_secret' ] == '' ) {
$data [ 'ga_secret' ] = $GA -> createSecret ();
$this -> update_ga_secret ( $username , $data [ 'ga_secret' ]);
}
}
else {
$query = $this -> db -> query ( " INSERT INTO " . TABLE_USER_SETTINGS . " (username, ga_enabled, ga_secret) VALUES(?,0,?) " , array ( $username , $GA -> createSecret ()));
}
return $data ;
}
public function update_ga_secret ( $username = '' , $ga_secret = '' ) {
if ( $username == " " || $ga_secret == " " ) { return 0 ; }
$query = $this -> db -> query ( " UPDATE " . TABLE_USER_SETTINGS . " SET ga_secret=? WHERE username=? " , array ( $ga_secret , $username ));
return 1 ;
}
public function toggle_ga ( $username = '' , $ga_enabled = '' ) {
if ( $username == " " || $ga_enabled < 0 || $ga_enabled > 1 ) { return 0 ; }
$query = $this -> db -> query ( " UPDATE " . TABLE_USER_SETTINGS . " SET ga_enabled=? WHERE username=? " , array ( $ga_enabled , $username ));
return 1 ;
}
2012-02-08 23:14:28 +01:00
}
?>