array(), 'user' => array(), 'guest' => array(), 'avatar' => array() );
function __construct(){
$this->init_defaults();
$this->init_options();
add_action('delete_user_form', array(&$this, 'show_delete_form'), 10, 2);
}
private function init_defaults(){
$this->default = new stdClass;
$this->default->options = array(
'custom_title_is_on' => 1,
'default_title' => 'Member',
'online_status_timeout' => 240,
'url_structure' => 'nicename',
'search_type' => 'search', // can to be 'search' or 'filter'
'login_url' => '',
'register_url' => '',
'lost_password_url' => '',
'redirect_url_after_login' => '',
'redirect_url_after_register' => '',
'redirect_url_after_confirm_sbscrb' => '',
'rating_title_ug' => array ( 1 => '0', 5 => '1', 4 => '1', 2 => '0', 3 => '1' ),
'rating_badge_ug' => array ( 1 => '1', 5 => '1', 4 => '1', 2 => '1', 3 => '1' ),
'title_usergroup' => array ( 1 => '1', 5 => '1', 4 => '1', 2 => '1', 3 => '0' )
);
$this->default->login_min_length = 3;
$this->default->login_max_length = 30;
$this->default->pass_min_length = 6;
$this->default->pass_max_length = 20;
}
private function init_options(){
$this->options = get_wpf_option('wpforo_member_options', $this->default->options);
if( !preg_match('#^https?://[^\r\n\t\s\0]+#isu', $this->options['redirect_url_after_login']) ) $this->options['redirect_url_after_login'] = '';
if( !preg_match('#^https?://[^\r\n\t\s\0]+#isu', $this->options['redirect_url_after_register']) ) $this->options['redirect_url_after_register'] = '';
if( !preg_match('#^https?://[^\r\n\t\s\0]+#isu', $this->options['redirect_url_after_confirm_sbscrb']) ) $this->options['redirect_url_after_confirm_sbscrb'] = '';
$this->login_min_length = $this->default->login_min_length;
$this->login_max_length = $this->default->login_max_length;
$this->pass_min_length = $this->default->pass_min_length;
$this->pass_max_length = $this->default->pass_max_length;
}
public function get_cache( $var ){
if( isset(self::$cache[$var]) ) return self::$cache[$var];
}
public function send_new_user_notifications($user_id, $notify = 'admin'){
wp_send_new_user_notifications( $user_id, $notify );
}
private function add_profile($args){
if(empty($args)) return FALSE;
if(!isset($args['userid']) || !$args['userid'] || !isset($args['username']) || !$args['username'] ) return FALSE;
extract( $args, EXTR_OVERWRITE );
$this->reset($userid);
return WPF()->db->insert(
WPF()->tables->profiles,
array( 'userid' => intval($userid),
'title' => ( isset($title) && $title ? $title : WPF()->member->options['default_title'] ),
'username' => sanitize_user($username),
'groupid' => intval((isset($groupid) && $groupid ? $groupid : WPF()->usergroup->default_groupid)),
'site' => (isset($site) ? sanitize_text_field($site) : '' ),
'timezone' => ( isset($timezone) ? sanitize_text_field($timezone) : 'UTC+0' ),
'about' => ( isset($about) ? stripslashes( wpforo_kses(trim($about), 'user_description') ) : '' ),
'last_login' => ( isset($last_login) ? $last_login : current_time('mysql', 1) ) ),
array( '%d', '%s', '%s', '%d', '%s', '%s', '%s', '%s' )
);
}
function edit_profile($args){
if(empty($args)) return FALSE;
if( !isset($args['userid']) || !$args['userid'] ) return FALSE;
extract( $args, EXTR_OVERWRITE );
$fields = array();
$fields_types = array();
if(isset($last_login) && $last_login){
$fields['last_login'] = sanitize_text_field($last_login);
$fields_types[] = '%s';
}
if(isset($groupid) && $groupid){
$groupid = intval($groupid);
if( !(!WPF()->current_object['user_is_same_current_user'] && (WPF()->current_user_groupid == 1 || current_user_can('administrator') )) ) {
$flds = $this->get_fields();
if( !in_array($groupid, wpforo_parse_args($flds['groupid']['allowedGroupIds'])) ) $groupid = WPF()->usergroup->default_groupid;
}
$fields['groupid'] = $groupid;
$fields_types[] = '%d';
}
if(isset($title) && $title){
$fields['title'] = sanitize_text_field(trim($title));
$fields_types[] = '%s';
}
if(isset($site)){
$fields['site'] = sanitize_text_field(trim($site));
$fields_types[] = '%s';
}
if(isset($icq)){
$fields['icq'] = sanitize_text_field(trim($icq));
$fields_types[] = '%s';
}
if(isset($aim)){
$fields['aim'] = sanitize_text_field(trim($aim));
$fields_types[] = '%s';
}
if(isset($yahoo)){
$fields['yahoo'] = sanitize_text_field(trim($yahoo));
$fields_types[] = '%s';
}
if(isset($msn)){
$fields['msn'] = sanitize_text_field(trim($msn));
$fields_types[] = '%s';
}
if(isset($facebook)){
$fields['facebook'] = sanitize_text_field(trim($facebook));
$fields_types[] = '%s';
}
if(isset($twitter)){
$fields['twitter'] = sanitize_text_field(trim($twitter));
$fields_types[] = '%s';
}
if(isset($gtalk)){
$fields['gtalk'] = sanitize_text_field(trim($gtalk));
$fields_types[] = '%s';
}
if(isset($skype)){
$fields['skype'] = sanitize_text_field(trim($skype));
$fields_types[] = '%s';
}
if(isset($signature)){
$fields['signature'] = stripslashes(wpforo_kses(trim($signature), 'user_description'));
$fields_types[] = '%s';
}
if(isset($about)){
$fields['about'] = stripslashes(wpforo_kses(trim($about), 'user_description'));
$fields_types[] = '%s';
}
if(isset($occupation)){
$fields['occupation'] = stripslashes(sanitize_text_field(trim($occupation)));
$fields_types[] = '%s';
}
if(isset($location)){
$fields['location'] = stripslashes(sanitize_text_field(trim($location)));
$fields_types[] = '%s';
}
if(isset($timezone)){
$fields['timezone'] = sanitize_text_field(trim($timezone));
$fields_types[] = '%s';
}
if(isset($avatar_type) && $avatar_type != 'gravatar' && isset($avatar_url) && $avatar_url){
$fields['avatar'] = esc_url(trim($avatar_url));
$fields_types[] = '%s';
}
if(isset($avatar_type) && $avatar_type == 'gravatar'){
$fields['avatar'] = '';
$fields_types[] = '%s';
}
$this->reset($userid);
$result = true;
if($fields){
$result = WPF()->db->update(
WPF()->tables->profiles,
$fields,
array('userid' => intval($userid)),
$fields_types,
array('%d')
);
if( $result !== FALSE && $userid ){
if(isset($fields['site'])){
WPF()->db->query("UPDATE `".WPF()->db->users."` SET `user_url` = '" . esc_sql($fields['site']) . "' WHERE `ID` = " . intval($userid) );
}
if(isset($fields['about'])){
update_user_meta( $userid, 'description', $fields['about'] );
}
}
}
return $result;
}
function create($args){
if(!wpforo_feature('user-register')){
WPF()->notice->add('User registration is disabled.', 'error');
return FALSE;
}
$args = apply_filters( 'wpforo_create_profile', $args );
if( (isset($args['error']) && $args['error']) || !$args ){
return FALSE;
}
$this->login_min_length = apply_filters('wpforo_login_min_length', $this->login_min_length);
$this->login_max_length = apply_filters('wpforo_login_max_length', $this->login_max_length);
$this->pass_min_length = apply_filters('wpforo_pass_min_length', $this->pass_min_length);
$this->pass_max_length = apply_filters('wpforo_pass_max_length', $this->pass_max_length);
if( !empty($args) && is_array($args) && !empty($args['user_pass1']) ){
remove_action('register_new_user', 'wp_send_new_user_notifications');
add_action('register_new_user', array($this, 'send_new_user_notifications'));
do_action( 'wpforo_create_profile_before', $args );
$errors = new WP_Error();
extract($args, EXTR_OVERWRITE);
$sanitized_user_login = sanitize_user( $user_login );
$user_email = apply_filters( 'user_registration_email', $user_email );
$user_pass1 = trim(substr($user_pass1, 0, 100));
$user_pass2 = trim(substr($user_pass2, 0, 100));
$illegal_user_logins = array_map( 'strtolower', (array) apply_filters( 'illegal_user_logins', array() ) );
if ( $sanitized_user_login == '' ) {
$errors->add( 'empty_username', __( 'ERROR: Please enter a username.' ) );
WPF()->notice->add('Username is missed.', 'error');
return FALSE;
}elseif ( ! validate_username( $user_login ) ) {
$errors->add( 'invalid_username', __( 'ERROR: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
$sanitized_user_login = '';
WPF()->notice->add('Illegal character in username.', 'error');
$user_login = '';
return FALSE;
}elseif( strlen($user_login) < $this->login_min_length || strlen($user_login) > $this->login_max_length ){
WPF()->notice->add( 'Username length must be between %d characters and %d characters.', 'error', array($this->login_min_length, $this->login_max_length) );
return FALSE;
}elseif ( username_exists( $sanitized_user_login ) ) {
$errors->add( 'username_exists', __( 'ERROR: This username is already registered. Please choose another one.' ) );
WPF()->notice->add('Username exists. Please insert another.', 'error');
return FALSE;
}elseif ( in_array( strtolower( $sanitized_user_login ), $illegal_user_logins ) ) {
$errors->add( 'invalid_username', __( 'ERROR: Sorry, that username is not allowed.' ) );
WPF()->notice->add('ERROR: invalid_username. Sorry, that username is not allowed. Please insert another.', 'error');
return FALSE;
}elseif ( $user_email == '' ) {
$errors->add( 'empty_email', __( 'ERROR: Please type your email address.' ) );
WPF()->notice->add('Insert your Email address.', 'error');
return FALSE;
}elseif ( ! is_email( $user_email ) ) {
$errors->add( 'invalid_email', __( 'ERROR: The email address isn’t correct.' ) );
WPF()->notice->add('Invalid Email address', 'error');
$user_email = '';
return FALSE;
}elseif ( email_exists( $user_email ) ) {
$errors->add( 'email_exists', __( 'ERROR: This email is already registered, please choose another one.' ) );
WPF()->notice->add('Email address exists. Please insert another.', 'error');
return FALSE;
}elseif( strlen($user_pass1) < $this->pass_min_length || strlen($user_pass1) > $this->pass_max_length ){
WPF()->notice->add( 'Password length must be between %d characters and %d characters.', 'error', array($this->pass_min_length, $this->pass_max_length) );
return FALSE;
}elseif($user_pass1 != $user_pass2){
WPF()->notice->add('Password mismatch.', 'error');
return FALSE;
}else{
do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
$errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
if ( $errors->get_error_code() ){
$args = array();
foreach($errors->errors as $u_err) $args[] = $u_err[0];
WPF()->notice->add($args, 'error');
return FALSE;
}
$user_id = wp_create_user( $sanitized_user_login, $user_pass1, $user_email );
if ( !is_wp_error( $user_id ) && $user_id ) {
$args['userid'] = $user_id;
$creds = array('user_login' => $sanitized_user_login, 'user_password' => $user_pass1 );
wp_signon($creds);
do_action( 'wpforo_create_profile_after', $args );
do_action( 'register_new_user', $user_id );
WPF()->notice->add('Success! Thank you Dear Friend', 'success');
return $user_id;
}
}
}elseif( !empty($args['user_login']) && !empty($args['user_email']) ){
if( strlen($args['user_login']) < $this->login_min_length || strlen($args['user_login']) > $this->login_max_length ){
WPF()->notice->add( 'Username length must be between %d characters and %d characters.', 'error', array($this->login_min_length, $this->login_max_length) );
return FALSE;
}
$user_id = register_new_user( $args['user_login'], $args['user_email'] );
if ( !is_wp_error( $user_id ) && $user_id ) {
$args['userid'] = $user_id;
do_action( 'wpforo_create_profile_after', $args );
WPF()->notice->add('Success! Please check your mail for confirmation.', 'success');
return $user_id;
}
}
if(!empty($user_id->errors)){
$args = array();
foreach($user_id->errors as $u_err) $args[] = $u_err[0];
WPF()->notice->add($args, 'error');
return FALSE;
}
WPF()->notice->add('Registration Error', 'error');
return FALSE;
}
function edit( $args = array() ){
if( empty($args) && empty($_REQUEST['member']) ) return FALSE;
if( empty($args) && !empty($_REQUEST['member']) ) $args = $_REQUEST['member'];
$args = apply_filters( 'wpforo_edit_profile', $args );
do_action( 'wpforo_edit_profile_before', $args );
if( (isset($args['error']) && $args['error']) || !$args ){
return FALSE;
}
extract($args, EXTR_OVERWRITE);
$fields = array();
$fields_types = array();
if( isset($userid) && $userid ){
$userid = intval($userid);
$isRegister = ( isset($args['template']) && $args['template'] == 'register' ) ? true : false;
if ( !$isRegister && ( !is_user_logged_in() || !WPF()->perm->user_can_manage_user( WPF()->current_userid, $userid ) ) ) {
WPF()->notice->add('Permission denied', 'error');
return FALSE;
}
if( isset($display_name) && $display_name ){
$fields['display_name'] = sanitize_text_field(trim($display_name));
$fields_types[] = '%s';
}
if( isset($user_email) && $user_email ){
$user_email = sanitize_email($user_email);
if ( ! is_email( $user_email ) ) {
WPF()->notice->add('Invalid Email address', 'error');
return FALSE;
}elseif ( ( $owner_id = email_exists( $user_email ) ) && ( $owner_id != $userid ) ) {
WPF()->notice->add('This email address is already registered. Please insert another.', 'error');
return FALSE;
}
$fields['user_email'] = $user_email;
$fields_types[] = '%s';
}
if( isset($user_nicename) && $user_nicename ){
$user_nicename = sanitize_title( trim($user_nicename) );
if( is_numeric($user_nicename) ){
WPF()->notice->add('Numerical nicknames are not allowed. Please insert another.', 'error');
return FALSE;
}
$sql = "SELECT `ID` FROM `".WPF()->db->users."` WHERE `ID` != ". intval($userid) ." AND ( `user_nicename` LIKE '".esc_sql($user_nicename)."' OR `ID` LIKE '".esc_sql($user_nicename)."' )";
if( WPF()->db->get_var($sql)){
WPF()->notice->add('This nickname is already registered. Please insert another.', 'error');
return FALSE;
}
$fields['user_nicename'] = $user_nicename;
$fields_types[] = '%s';
WPF()->db->update(
WPF()->db->usermeta,
array('meta_value' => $user_nicename),
array('user_id' => $userid, 'meta_key' => 'nickname'),
array('%s'),
array('%d', '%s')
);
}
if( $fields ){
WPF()->db->update(
WPF()->db->users,
$fields,
array('ID' => $userid),
$fields_types,
array('%d')
);
$this->reset($userid);
}
if( FALSE !== $this->edit_profile($args) ){
do_action( 'wpforo_edit_profile_after', $args );
WPF()->notice->add('Your profile data have been successfully updated.', 'success');
return $userid;
}
}
WPF()->notice->add('Something wrong with profile data.', 'error');
return FALSE;
}
public function change_password($old_passw, $new_passw, $userid){
if( !$userid = wpforo_bigintval($userid) ){
WPF()->notice->clear();
WPF()->notice->add('Userid is wrong', 'error');
return false;
}
$user = $this->get_member($userid);
if( !apply_filters('wpforo_change_password_validate', true, $old_passw, $new_passw, $user) ) return false;
if ( wp_check_password( $old_passw, $user['user_pass'], $userid) ){
wp_set_password( $new_passw, $userid );
/**
* Login user after change password with new pass
*/
$creds = array('user_login' => sanitize_user( $user['user_login'] ), 'user_password' => $new_passw );
wp_signon($creds);
WPF()->notice->add('Password successfully changed', 'success');
return true;
}
WPF()->notice->clear();
WPF()->notice->add('Old password is wrong', 'error');
return false;
}
function upload_avatar( $userid = 0 ){
$userid = intval($userid);
if( !WPF()->perm->usergroup_can('upa') ) return;
if( !$userid ){
if( !isset($_POST['member']['userid']) || !$userid = intval($_POST['member']['userid']) ) return;
}
if( !$user = $this->get_member($userid) ) return;
$user_nicename = urldecode($user['user_nicename']);
if(isset($_FILES['avatar']) && !empty($_FILES['avatar']) && isset($_FILES['avatar']['name']) && $_FILES['avatar']['name']){
$name = sanitize_file_name($_FILES['avatar']['name']); //myimg.png
$type = sanitize_mime_type($_FILES['avatar']['type']); //image/png
$tmp_name = sanitize_text_field($_FILES['avatar']['tmp_name']); //D:\wamp\tmp\php986B.tmp
$error = sanitize_text_field($_FILES['avatar']['error']); //0
$size = intval($_FILES['avatar']['size']); //6112
if( $size > 2*1048576 ){
WPF()->notice->clear();
WPF()->notice->add('Avatar image is too big maximum allowed size is 2MB', 'error');
return FALSE;
}
if( $error ){
$error = wpforo_file_upload_error($error);
WPF()->notice->clear();
WPF()->notice->add($error, 'error');
return FALSE;
}
$upload_dir = wp_upload_dir();
$uplds_dir = $upload_dir['basedir']."/wpforo";
$avatar_dir = $upload_dir['basedir']."/wpforo/avatars";
if(!is_dir($uplds_dir)) wp_mkdir_p($uplds_dir);
if(!is_dir($avatar_dir)) wp_mkdir_p($avatar_dir);
$ext = pathinfo($name, PATHINFO_EXTENSION);
if( !wpforo_is_image($ext) ){
WPF()->notice->clear();
WPF()->notice->add('Incorrect file format. Allowed formats: jpeg, jpg, png, gif.', 'error');
return FALSE;
}
$fnm = pathinfo($user_nicename, PATHINFO_FILENAME);
$fnm = str_replace(' ', '-', $fnm);
while(strpos($fnm, '--') !== FALSE) $fnm = str_replace('--', '-', $fnm);
$fnm = preg_replace("/[^-a-zA-Z0-9]/", "", $fnm);
$fnm = trim($fnm, "-");
$avatar_fname = $fnm . ( $fnm ? '_' : '' ) . $userid . "." . strtolower($ext);
$avatar_fname_orig = $fnm . ( $fnm ? '_' : '' ) . $userid . "." . $ext;
$avatar_path = $avatar_dir . "/" . $avatar_fname;
$avatar_path_orig = $avatar_dir . "/" . $avatar_fname_orig;
if(is_dir($avatar_dir)){
if(move_uploaded_file($tmp_name, $avatar_path)) {
$image = wp_get_image_editor( $avatar_path );
if ( ! is_wp_error( $image ) ) {
$image->resize( 150, 150, true );
$saved = $image->save( $avatar_path );
if(! is_wp_error( $saved ) && $avatar_fname != $avatar_fname_orig ) {
if ( defined (PHP_OS) && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') unlink( $avatar_path_orig );
}
}
$blog_url = preg_replace('#^https?\:#is', '', $upload_dir['baseurl']);
WPF()->db->update(WPF()->tables->profiles, array('avatar' => $blog_url . "/wpforo/avatars/" . $avatar_fname), array('userid' => intval($userid)), array('%s'), array('%d'));
$this->reset($userid);
}
}
}
}
function synchronize_user($userid){
if(!$userid) return FALSE;
$user = get_userdata($userid);
if( is_super_admin( $userid ) || in_array('administrator', $user->roles) ){
$groupid = 1;
}elseif( in_array('editor', $user->roles) ){
$groupid = 2;
}elseif( in_array('customer', $user->roles) ){
$groupid = 5;
}else{
$groupid = WPF()->usergroup->default_groupid;
}
$insert_groupid = (isset($_POST['wpforo_usergroup'])) ? intval($_POST['wpforo_usergroup']) : $groupid;
$insert_timezone = (isset($_POST['wpforo_usertimezone'])) ? sanitize_text_field($_POST['wpforo_usertimezone']) : '';
$about = get_user_meta( $userid, 'description', true );
return $this->add_profile(
array( 'userid' => wpforo_bigintval($userid),
'username' => sanitize_user($user->user_login),
'groupid' => intval($insert_groupid),
'site' => esc_url($user->user_url),
'timezone' => sanitize_text_field($insert_timezone),
'about' => stripslashes( wpforo_kses(trim($about), 'user_description') ),
'last_login' => sanitize_text_field($user->user_registered) ) );
}
function synchronize_users(){
if( is_multisite() ){
$sql = "SELECT `user_id` FROM `".WPF()->db->usermeta."` WHERE `meta_key` LIKE '".WPF()->blog_prefix."capabilities' AND `user_id` NOT IN( SELECT `userid` FROM `".WPF()->tables->profiles."` )";
} else {
$sql = "SELECT `ID` as user_id FROM `".WPF()->db->users."` WHERE `ID` NOT IN( SELECT `userid` FROM `".WPF()->tables->profiles."` )";
}
$userids = WPF()->db->get_col($sql);
if( !empty($userids) ){
foreach($userids as $userid){
$this->synchronize_user($userid);
}
}
## -- START -- delete profiles where not participant on multisite blog
if( is_multisite() ){
$sql = "DELETE FROM `".WPF()->tables->profiles."` WHERE `userid` NOT IN( SELECT `user_id` FROM `".WPF()->db->usermeta."` WHERE `meta_key` LIKE '".WPF()->blog_prefix."capabilities' )";
WPF()->db->query($sql);
}
## -- END -- delete profiles where not participant on multisite blog
}
function get_member($args){
if(!$args) return $this->get_guest();
$cache = WPF()->cache->on('memory_cashe');
$default = array(
'userid' => NULL, // $userid
'user_nicename' => '' // $user_nicename
);
if( is_numeric($args) ){
$args = array( 'userid' => $args );
}elseif ( !is_array($args) ){
$args = array( 'user_nicename' => $args );
}
$args = wpforo_parse_args( $args, $default );
if(isset($args['userid'])){
if( $cache && isset(self::$cache['user'][$args['userid']]) ){
return self::$cache['user'][$args['userid']];
}
}
extract($args);
$do_db_cache = wpforo_feature('member_cashe');
$userid = wpforo_bigintval($userid);
$user_meta_obj = true;
$member = array();
if( $do_db_cache ){
if( $user_nicename ){
$user_obj = get_user_by( 'user_nicename', $user_nicename );
if( !empty($user_obj) ) $userid = $user_obj->ID;
}
$member = get_user_meta( $userid, '_wpf_member_obj', true );
}
if(empty($member)){
$user_meta_obj = false;
$sql = "SELECT *, ug.name AS groupname, ug.color AS color FROM `".WPF()->db->users."` u
INNER JOIN `".WPF()->tables->profiles."` p ON p.`userid` = u.`ID`
LEFT JOIN `".WPF()->tables->usergroups."` ug ON ug.`groupid` = p.`groupid`";
$wheres = array();
if($userid) $wheres[] = "`ID` = $userid";
if($user_nicename) $wheres[] = "`user_nicename` = '" . esc_sql($user_nicename) . "'";
if( !empty($wheres) ) $sql .= " WHERE " . implode($wheres, " AND ");
$member = WPF()->db->get_row($sql, ARRAY_A);
}
if(!empty($member)) {
$member['profile_url'] = $this->profile_url( $member );
$member['stat'] = $this->get_stat( $member, false, true );
if( $do_db_cache ){
if(!$user_meta_obj) update_user_meta( $userid, '_wpf_member_obj', $member );
}
}
if($cache && isset($userid) && $member){
return self::$cache['user'][$userid] = $member;
}else{
return $member;
}
}
function get_members($args = array(), &$items_count = 0){
$default = array(
'include' => array(), // array( 2, 10, 25 )
'exclude' => array(), // array( 2, 10, 25 )
'status' => array('active', 'banned'), // 'active', 'blocked', 'trashed', 'spamer'
'groupid' => NULL, // groupid
'online_time' => NULL, // groupid
'orderby' => 'userid', //
'order' => 'ASC', // ASC DESC
'offset' => 0, // OFFSET
'row_count' => NULL, // ROW COUNT
'groupids' => array(), // array( 1, 2 )
);
$args = wpforo_parse_args( $args, $default );
if(!empty($args)){
extract($args, EXTR_OVERWRITE);
$include = wpforo_parse_args( $include );
$exclude = wpforo_parse_args( $exclude );
$sql = "SELECT *, ug.name AS groupname, ug.color AS color FROM `".WPF()->db->users."` u
INNER JOIN `".WPF()->tables->profiles."` p ON p.`userid` = u.`ID`
LEFT JOIN `".WPF()->tables->usergroups."` ug ON ug.`groupid` = p.`groupid`";
$wheres = array();
if(!empty($include)) $wheres[] = " u.`ID` IN(" . implode(', ', array_map('intval', $include)) . ")";
if(!empty($exclude)) $wheres[] = " u.`ID` NOT IN(" . implode(', ', array_map('intval', $exclude)) . ")";
if(!empty($status)) $wheres[] = " p.`status` IN('" . implode("','", array_map('esc_sql', array_map('sanitize_text_field', $status)) ) . "')";
if(!empty($groupids)) $wheres[] = " p.`groupid` IN(" . implode(', ', array_map('intval', $groupids)) . ")";
if(!is_null($groupid)) $wheres[] = " p.`groupid` = " . intval($groupid);
if(!is_null($online_time)) $wheres[] = " p.`online_time` > " . intval($online_time);
if(!empty($wheres)) $sql .= " WHERE " . implode($wheres, " AND ");
$item_count_sql = preg_replace('#SELECT.+?FROM#isu', 'SELECT count(*) FROM', $sql);
if( $item_count_sql ) $items_count = WPF()->db->get_var($item_count_sql);
if( $orderby == 'groupid' ) $orderby = 'p.`groupid`';
$sql .= esc_sql(" ORDER BY $orderby " . $order);
if($row_count) $sql .= esc_sql(" LIMIT $offset,$row_count");
return WPF()->db->get_results($sql, ARRAY_A);
}
}
function search($needle, $fields = array(), $limit = NULL){
if($needle != ''){
$needle = sanitize_text_field($needle);
if(empty($fields)){
$fields = array(
'title',
'user_nicename',
'user_email',
'signature'
);
}
$sql = "SELECT `ID` FROM `".WPF()->db->users."` u
INNER JOIN `".WPF()->tables->profiles."` p ON p.`userid` = u.`ID`";
$wheres = array();
foreach($fields as $field){
$field = sanitize_text_field($field);
$wheres[] = "`".esc_sql($field)."` LIKE '%" . esc_sql($needle) ."%'";
}
if(!empty($wheres)){
$sql .= " WHERE " . implode($wheres, " OR ");
if( $limit ) $sql .= " LIMIT " . intval($limit);
return WPF()->db->get_col($sql);
}else{
return array();
}
}else{
return array();
}
}
public function filter($args, $limit = NULL){
if($args && is_array($args)){
$sql = "SELECT `ID` FROM `".WPF()->db->users."` u
INNER JOIN `".WPF()->tables->profiles."` p ON p.`userid` = u.`ID`";
$wheres = array();
foreach($args as $field => $needle){
$field = sanitize_text_field($field);
$needle = sanitize_text_field($needle);
$wheres[] = "`".esc_sql($field)."` LIKE '%" . esc_sql($needle) ."%'";
}
if($wheres){
$sql .= " WHERE " . implode($wheres, " AND ");
if( $limit ) $sql .= " LIMIT " . intval($limit);
return WPF()->db->get_col($sql);
}
}
return array();
}
function ban($userid){
if( $userid == WPF()->current_userid ){
WPF()->notice->add('You can\'t make yourself banned user', 'error');
return FALSE;
}
if( !WPF()->perm->usergroup_can('bm') || !WPF()->perm->user_can_manage_user( WPF()->current_userid, intval( $userid ) )){
WPF()->notice->add('Permission denied for this action', 'error');
return FALSE;
}
if( FALSE !== WPF()->db->update(
WPF()->tables->profiles,
array('status' => 'banned'),
array('userid' => intval( $userid )),
array('%s'),
array('%d')
)
){
$this->reset($userid);
WPF()->notice->add('User successfully banned from wpforo', 'success');
return TRUE;
}
WPF()->notice->add('User ban action error', 'error');
return FALSE;
}
function unban($userid){
if( !WPF()->perm->usergroup_can('bm') || !WPF()->perm->user_can_manage_user( WPF()->current_userid, intval( $userid ) )){
WPF()->notice->add('Permission denied for this action', 'error');
return FALSE;
}
if( FALSE !== WPF()->db->update(
WPF()->tables->profiles,
array('status' => 'active'),
array('userid' => intval( $userid )),
array('%s'),
array('%d')
)
){
$this->reset($userid);
WPF()->notice->add('User successfully unbanned from wpforo', 'success');
return TRUE;
}
WPF()->notice->add('User unban action error', 'error');
return FALSE;
}
/**
*
* @param int $userid
* @param int $reassign
*
* @return bool true | false if user successfully deleted
*/
public function delete( $userid, $reassign = NULL ){
if( !($userid = intval($userid)) ) return FALSE;
if( !WPF()->perm->usergroup_can('dm') || !WPF()->perm->user_can_manage_user( WPF()->current_userid, intval( $userid ) )){
WPF()->notice->add('Permission denied for this action', 'error');
return FALSE;
}
do_action('wpforo_before_delete_user', $userid, $reassign);
if( !($reassign = intval($reassign)) ){
if( $postids = WPF()->db->get_col( WPF()->db->prepare( "SELECT `postid` FROM `".WPF()->tables->posts."` WHERE userid = %d", $userid ) ) ){
foreach( $postids as $postid ) WPF()->post->delete($postid);
}
if( $topicids = WPF()->db->get_col( WPF()->db->prepare( "SELECT `topicid` FROM `".WPF()->tables->topics."` WHERE userid = %d", $userid ) ) ){
foreach( $topicids as $topicid ) WPF()->topic->delete($topicid, false);
}
}else{
WPF()->db->update( WPF()->tables->topics, array('userid' => $reassign), array('userid' => $userid) );
WPF()->db->update( WPF()->tables->posts, array('userid' => $reassign), array('userid' => $userid) );
WPF()->db->update( WPF()->tables->likes, array('post_userid' => $reassign), array('post_userid' => $userid) );
WPF()->db->update( WPF()->tables->votes, array('post_userid' => $reassign), array('post_userid' => $userid) );
if( $user_stats = WPF()->db->get_row(
WPF()->db->prepare( "SELECT
SUM(`posts`) AS posts,
SUM(`questions`) AS questions,
SUM(`answers`) AS answers,
SUM(`comments`) AS comments
FROM `".WPF()->tables->profiles."`
WHERE `userid` IN( %d , %d )", $userid, $reassign
),
ARRAY_A
)
){
WPF()->db->update(
WPF()->tables->profiles,
array(
'posts' => $user_stats['posts'],
'questions' => $user_stats['questions'],
'answers' => $user_stats['answers'],
'comments' => $user_stats['comments']
),
array('userid' => $reassign),
array('%d','%d','%d','%d'),
array('%d')
);
}
}
WPF()->db->delete(
WPF()->tables->subscribes, array( 'userid' => $userid ), array( '%d' )
);
WPF()->db->delete(
WPF()->tables->views, array( 'userid' => $userid ), array( '%d' )
);
WPF()->db->delete(
WPF()->tables->likes, array( 'userid' => $userid ), array( '%d' )
);
WPF()->db->delete(
WPF()->tables->votes, array( 'userid' => $userid ), array( '%d' )
);
if( FALSE !== WPF()->db->delete(
WPF()->tables->profiles, array( 'userid' => $userid ), array( '%d' )
)
){
do_action('wpforo_after_delete_user', $userid, $reassign);
WPF()->notice->add('User successfully deleted from wpforo', 'success');
return TRUE;
}
WPF()->notice->add('User delete error', 'error');
return FALSE;
}
public function avatar($member, $attr = '', $size = ''){
if(!isset($member['userid'])) return '';
$cache = WPF()->cache->on('memory_cashe');
$src = $member['avatar'];
$userid = ( $member['userid'] ? $member['userid'] : $member['user_email'] );
if($cache && isset(self::$cache['avatar'][$userid])){
if(self::$cache['avatar'][$userid]['attr'] == $attr && self::$cache['avatar'][$userid]['size'] == $size){
if(isset(self::$cache['avatar'][$userid]['img'])){
return self::$cache['avatar'][$userid]['img'];
}
}
}
if($src && wpforo_feature('custom-avatars')){
$attr = ($attr ? $attr : 'height="96" width="96"');
$img = '';
}else{
$img = ($size) ? get_avatar($userid, $size) : get_avatar($userid);
if($attr) $img = str_replace('
cache->on('memory_cashe');
if($cache && isset(self::$cache['avatar'][$userid])){
if(self::$cache['avatar'][$userid]['attr'] == $attr && self::$cache['avatar'][$userid]['size'] == $size){
if(isset(self::$cache['avatar'][$userid]['img'])){
return self::$cache['avatar'][$userid]['img'];
}
}
}
$src = $this->get_avatar_url($userid);
if($src && wpforo_feature('custom-avatars')){
$attr = ($attr ? $attr : 'height="96" width="96"');
$img = '
';
}else{
$img = ($size) ? get_avatar($userid, $size) : get_avatar($userid);
if($attr) $img = str_replace('
cache->on('memory_cashe');
if( $cache && array_key_exists($userid, self::$cache['user']) && array_key_exists('avatar', self::$cache['user'][$userid]) ){
return self::$cache['user'][$userid]['avatar'];
}
if( $cache && array_key_exists($userid, self::$cache['avatar']) && array_key_exists('avatar_url', self::$cache['avatar'][$userid]) ){
return self::$cache['avatar'][$userid]['avatar_url'];
}
$avatar_url = WPF()->db->get_var( WPF()->db->prepare("SELECT `avatar` FROM `".WPF()->tables->profiles."` WHERE `userid` = %d", wpforo_bigintval($userid)) );
if($cache) return self::$cache['avatar'][$userid]['avatar_url'] = $avatar_url;
return $avatar_url;
}
function get_topics_count( $userid ){
$count = WPF()->db->get_var("SELECT count(topicid) FROM `".WPF()->tables->topics."` WHERE `userid` = ".intval($userid));
return $count;
}
function get_questions_count( $userid ){
$count = WPF()->db->get_var("SELECT count(topicid) FROM `".WPF()->tables->topics."` WHERE `userid` = ".intval($userid));
return $count;
}
function get_answers_count( $userid ){
$count = WPF()->db->get_var("SELECT count(postid) FROM `".WPF()->tables->posts."` WHERE `is_answer` = 1 AND `userid` = ".intval($userid));
return $count;
}
function get_question_comments_count( $userid ){
$count = WPF()->db->get_var("SELECT count(postid) FROM `".WPF()->tables->posts."` WHERE `parentid` > 0 AND `userid` = ".intval($userid));
return $count;
}
function get_replies_count( $userid ){
$count = WPF()->db->get_var("SELECT count(postid) FROM `".WPF()->tables->posts."` WHERE `userid` = ".intval($userid));
return $count;
}
function get_likes_count( $userid ){
$count = WPF()->db->get_var("SELECT count(likeid) FROM `".WPF()->tables->likes."` WHERE `userid` = ".intval($userid));
return $count;
}
function get_votes_count( $userid ){
$count = WPF()->db->get_var("SELECT count(voteid) FROM `".WPF()->tables->votes."` WHERE `userid` = ".intval($userid));
return $count;
}
// how many times the user like or vote
function get_votes_and_likes_count( $userid ){
return $this->get_votes_count( intval($userid) ) + $this->get_likes_count( intval($userid) );
}
//getting user's posts votes and likes count
function get_user_votes_and_likes_count( $userid ){
$votes_count = WPF()->db->get_var("SELECT count(voteid) FROM `".WPF()->tables->votes."` WHERE `post_userid` = ".intval($userid));
$likes_count = WPF()->db->get_var("SELECT count(likeid) FROM `".WPF()->tables->likes."` WHERE `post_userid` = ".intval($userid));
return $votes_count + $likes_count;
}
function get_profile_url( $arg, $template = 'profile' ){
if(!$arg) return wpforo_home_url();
$userid = intval( basename($arg) );
$member_args = ( $userid ? $userid : array( 'user_nicename' => basename($arg) ) );
$user = $this->get_member( $member_args );
if(empty($user)) return wpforo_home_url();
$user_slug = ( wpfo(WPF()->member->options['url_structure'], false) == 'id' ? $user['ID'] : $user['user_nicename'] );
$profile_url = wpforo_home_url("$template/$user_slug");
return apply_filters('wpforo_member_profile_url', $profile_url, $user, $template);
}
function profile_url( $member = array(), $template = 'profile' ){
if(isset($member['ID']) || isset($member['user_nicename'])){
$user_slug = ( wpfo(WPF()->member->options['url_structure'], false) == 'id' ? $member['ID'] : $member['user_nicename'] );
$profile_url = wpforo_home_url("$template/$user_slug");
$profile_url = apply_filters( 'wpforo_profile_url', $profile_url, $member, $template );
}
else{
$profile_url = wpforo_home_url();
$profile_url = apply_filters( 'wpforo_no_profile_url', $profile_url, $template );
}
return apply_filters('wpforo_member_profile_url', $profile_url, $member, $template);
}
//$args = UserID or Member Object
//$live_count = TRUE / FALSE
function get_stat( $args = array(), $live_count = false, $cache = false ){
$cache = WPF()->cache->on('memory_cashe');
$stat = array( 'points' => 0,
'rating' => 0,
'rating_procent' => 0,
'color' => $this->rating(0, 'color'),
'badge' => $this->rating(0, 'icon'),
'posts' => 0,
'topics' => 0,
'questions' => 0,
'answers' => 0,
'question_comments' => 0,
'likes' => 0,
'liked' => 0,
'title' => $this->rating(0, 'title'));
$userid = ( isset($args['userid']) && $args['userid'] ) ? $args['userid'] : $args;
if( $cache && isset(self::$cache['stat'][$userid]) ){
return self::$cache['stat'][$userid];
}
if( is_array($args) && isset($args['userid']) ){
$userid = $args['userid'];
$stat['topics'] = (int)$this->get_topics_count( $userid );
if(isset($args['questions'])) $stat['questions'] = intval($args['questions']);
if(isset($args['answers'])) $stat['answers'] = intval($args['answers']);
if(isset($args['posts'])) $stat['posts'] = intval($args['posts']);
if(isset($args['comments'])) $stat['question_comments'] = intval($args['comments']);
}
elseif($userid = wpforo_bigintval($args)){
$stat['topics'] = (int)$this->get_topics_count( $userid );
if($live_count){
if($questions = $this->get_questions_count( $userid )) $stat['questions'] = $questions;
if($answers = $this->get_answers_count( $userid )) $stat['answers'] = $answers;
if($posts = $this->get_replies_count( $userid )) $stat['posts'] = $posts;
if($question_comments = $this->get_question_comments_count( $userid )) $stat['question_comments'] = $question_comments;
}
else{
$profile = WPF()->db->get_var("SELECT `posts`, `questions`, `answers`, `comments` FROM `".WPF()->tables->profiles."` WHERE `userid` = ".intval($userid));
if(isset($profile['questions'])) $stat['questions'] = intval($profile['questions']);
if(isset($profile['answers'])) $stat['answers'] = intval($profile['answers']);
if(isset($profile['posts'])) $stat['posts'] = intval($profile['posts']);
if(isset($profile['comments'])) $stat['question_comments'] = intval($profile['comments']);
}
}
if( $userid ){
if($likes = $this->get_votes_and_likes_count( $userid )) $stat['likes'] = $likes;
if($liked = $this->get_user_votes_and_likes_count( $userid )) $stat['liked'] = $liked;
if($stat['posts']) $stat['points'] = $stat['posts']; //TO-DO: Point counter function based on all stat values.
if($stat['points']) $stat['rating'] = $this->rating_level($stat['points'], false);
if($stat['rating']) {
$stat['rating_procent'] = $stat['rating'] * 10;
$stat['title'] = $this->rating(intval($stat['rating']), 'title');
$stat['color'] = $this->rating(intval($stat['rating']), 'color');
$stat['badge'] = $this->rating(intval($stat['rating']), 'icon');
}
}
if($cache && isset($userid)){
return self::$cache['stat'][$userid] = $stat;
}
else{
return $stat;
}
}
function get_count(){
return WPF()->db->get_var( "SELECT COUNT(p.`userid`) FROM `".WPF()->tables->profiles."` p
INNER JOIN `".WPF()->db->users."` u ON u.`ID` = p.`userid` WHERE p.`status` NOT LIKE 'trashed'" );
}
function is_online( $userid, $duration = NULL ){
$cache = WPF()->cache->on('memory_cashe');
if( $cache && isset(self::$cache['online'][$userid]) ){
if(self::$cache['online'][$userid]['durration'] == $duration ){
if(isset(self::$cache['online'][$userid]['status'])){
return self::$cache['online'][$userid]['status'];
}
}
}
if(!$duration) $duration = WPF()->member->options['online_status_timeout'];
$sql = "SELECT `online_time` FROM `".WPF()->tables->profiles."` WHERE `userid` = %d";
$sql = WPF()->db->prepare($sql, $userid);
$online_time = intval( WPF()->db->get_var($sql) );
$current_time = current_time( 'timestamp', 1 );
$online_duration = $current_time - $online_time;
if( $online_duration < $duration ) {
$status = true;
}
else{
$status = false;
}
if( $cache ){
self::$cache['online'][$userid]['durration'] = $duration;
return self::$cache['online'][$userid]['status'] = $status;
}
else{
return $status;
}
}
public function show_online_indicator($userid, $ico = TRUE){
if( $this->is_online($userid)) : ?>
member->options['online_status_timeout'];
$current_time = current_time( 'timestamp', 1 );
$online_timeframe = $current_time - $duration;
return WPF()->db->get_var( "SELECT COUNT(*) FROM `".WPF()->tables->profiles."` WHERE `online_time` > " . intval($online_timeframe) );
}
function get_online_members( $count = 1, $duration = NULL ){
if(!$duration) $duration = WPF()->member->options['online_status_timeout'];
$current_time = current_time( 'timestamp', 1 );
$online_timeframe = $current_time - $duration;
$args = array(
'online_time' => $online_timeframe, // $current_time - $duration
'orderby' => 'userid', // forumid, order, parentid
'row_count' => $count,
'order' => 'ASC', // ASC DESC
);
return $this->get_members($args);
}
function levels(){
$levels = array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
return $levels;
}
function rating( $level = false, $var = false, $default = false ){
$rating = array();
$rating['color'] = array( 0 => '#d2d2d2', 1 => '#4dca5c', 2 => '#4dca5c', 3 => '#4dca5c', 4 => '#4dca5c', 5 => '#4dca5c', 6 => '#E5D600', 7 => '#E5D600', 8 => '#E5D600', 9 => '#FF812D', 10 => '#E04A47' );
$rating['points'] = array( 0 => 0, 1 => 5, 2 => 20, 3 => 50, 4 => 100, 5 => 250, 6 => 500, 7 => 750, 8 => 1000, 9 => 2500, 10 => 5000 );
$rating['title'] = array( 0 => 'New Member', 1 => 'Active Member', 2 => 'Eminent Member', 3 => 'Trusted Member', 4 => 'Estimable Member', 5 => 'Reputable Member', 6 => 'Honorable Member', 7 => 'Prominent Member', 8 => 'Noble Member', 9 => 'Famed Member', 10 => 'Illustrious Member' );
$rating['icon'] = array( 0 => 'far fa-star-half', 1 => 'fas fa-star', 2 => 'fas fa-star', 3 => 'fas fa-star', 4 => 'fas fa-star', 5 => 'fas fa-star', 6 => 'fas fa-certificate', 7 => 'fas fa-certificate', 8 => 'fas fa-certificate', 9 => 'fas fa-shield-alt', 10 => 'fas fa-trophy' );
if(!empty(WPF()->member->options['rating'])){
if($level === false) return WPF()->member->options['rating'];
if(!empty(WPF()->member->options['rating'][$level])){
if(!$var) return WPF()->member->options['rating'][$level];
if(!empty(WPF()->member->options['rating'][$level][$var])){
if( $var == 'icon' && strpos( WPF()->member->options['rating'][$level][$var], ' ' ) === false ) return $rating[$var][$level];
return WPF()->member->options['rating'][$level][$var];
}
}
}
if( $level !== false && $var ) { return $rating[$var][$level]; }
elseif( $level !== false && !$var ){ foreach( $rating as $variable => $values ){ $level_data[$variable] = $values[$level];} return $level_data; }
elseif( $level === false && !$var ) return $rating;
else return array();
}
function rating_level($member_posts, $percent = TRUE){
$bar = 0;
if($member_posts < $this->rating(1, 'points')){$bar = 0;}
elseif($member_posts < $this->rating(2, 'points')){$bar = 10;}
elseif($member_posts < $this->rating(3, 'points')){$bar = 20;}
elseif($member_posts < $this->rating(4, 'points')){$bar = 30;}
elseif($member_posts < $this->rating(5, 'points')){$bar = 40;}
elseif($member_posts < $this->rating(6, 'points')){$bar = 50;}
elseif($member_posts < $this->rating(7, 'points')){$bar = 60;}
elseif($member_posts < $this->rating(8, 'points')){$bar = 70;}
elseif($member_posts < $this->rating(9, 'points')){$bar = 80;}
elseif($member_posts < $this->rating(10, 'points')){$bar = 90;}
else{$bar = 100;}
if($percent){
return $bar;
}else{
return floor($bar/10);
}
}
function rating_badge($level = 0, $view = 'short'){
$level = ( $level > 10 ) ? floor($level/10) : $level;
if($level == 0){
return '';
}
elseif($level > 0 && $level < 6){
if( $view == 'full' ){
return str_repeat(' ', $level);
}
else{
return '' . esc_html($level) . ' ';
}
}
elseif($level > 5 && $level < 9){
if( $view == 'full' ){
return str_repeat(' ', ($level-5));
}
else{
return '' . esc_html($level-5) . ' ';
}
}
elseif($level > 8){
return '';
}
else{
return '';
}
}
public function reset($userid){
if( !$userid ) return;
WPF()->db->query( "DELETE FROM `".WPF()->db->usermeta."` WHERE `meta_key` = '_wpf_member_obj' AND `user_id` = " . intval($userid) );
wpforo_clean_cache( 'user', $userid );
}
public function clear_db_cache(){
WPF()->db->query( "DELETE FROM `".WPF()->db->usermeta."` WHERE `meta_key` = '_wpf_member_obj'" );
}
private function update_online_time($userid = NULL){
if(!$userid) $userid = WPF()->current_userid;
if(!$userid) return false;
$current_timestamp = current_time( 'timestamp', 1 );
$sql = "UPDATE `".WPF()->tables->profiles."` SET `online_time` = %d WHERE `userid` = %d";
$sql = WPF()->db->prepare($sql, $current_timestamp, wpforo_bigintval($userid));
if( false !== WPF()->db->query($sql) ) return $current_timestamp;
return false;
}
public function init_current_user(){
$current_user = wp_get_current_user();
if( $current_user->exists() ){
$user = $this->get_member( $current_user->ID );
$status = ( isset($user['status']) ? $user['status'] : '' );
if( $status == 'active' ){
$user['groupid'] = intval($user['groupid']);
WPF()->current_user = $user;
WPF()->current_user_groupid = WPF()->current_user['groupid'];
WPF()->current_userid = $current_user->ID;
WPF()->current_username = $current_user->user_login;
WPF()->current_user_email = $current_user->user_email;
WPF()->current_user_display_name = $current_user->display_name;
$this->update_online_time();
}
WPF()->current_user_status = $status;
}elseif ( $guest = $this->get_guest_cookies() ){
WPF()->current_user = $this->get_guest($guest);
WPF()->current_user_email = $guest['email'];
WPF()->current_user_display_name = $guest['name'];
}
}
public function blog_posts( $userid ){
if( isset($userid) && $userid ) return count_user_posts( $userid , 'post' );
return 0;
}
public function blog_comments($userid, $user_email){
global $wpdb;
if( !$userid || !$user_email ) return 0;
return (int) $wpdb->get_var("SELECT COUNT(*) FROM " . $wpdb->comments. " WHERE `user_id` = " . intval($userid) . " OR `comment_author_email` = '" . esc_sql($user_email) . "'");
}
public function show_delete_form($current_user, $userids){
if( empty($current_user) || empty($userids) ) return;
$userids = array_diff( $userids, array( $current_user->ID ) );
$users_have_content = false;
if ( WPF()->db->get_var( "SELECT `postid` FROM `".WPF()->tables->posts."` WHERE `userid` IN( " . implode( ',', array_map('intval', $userids) ) . " ) LIMIT 1" ) ) {
$users_have_content = true;
}
?>