PluginProbe
wpForo Forum / 1.3.1
wpForo Forum v1.3.1
3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 All 137 releases
wpforo / wpf-includes / class-members.php

class-members.php in wpForo Forum 1.3.1, at wpf-includes/class-members.php

1,267 lines 48.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if( !defined( 'ABSPATH' ) ) exit;
4
5
6 class wpForoMember{
7
8 private $wpforo;
9 static $cache = array( 'users' => array(), 'user' => array(), 'guest' => array() );
10
11 function __construct( $wpForo ){
12 if(!isset($this->wpforo)) $this->wpforo = $wpForo;
13 add_action('delete_user_form', array(&$this, 'show_delete_form'), 10, 2);
14 }
15
16 public function get_cache( $var ){
17 if( isset(self::$cache[$var]) ) return self::$cache[$var];
18 }
19
20 public function send_new_user_notifications($user_id, $notify = 'admin'){
21 wp_send_new_user_notifications( $user_id, $notify );
22 }
23
24 private function add_profile($args){
25 if(empty($args)) return FALSE;
26 if(!isset($args['userid']) || !$args['userid'] || !isset($args['username']) || !$args['username'] ) return FALSE;
27 extract( $args, EXTR_OVERWRITE );
28 $this->reset($userid);
29 return $this->wpforo->db->insert(
30 $this->wpforo->db->prefix . 'wpforo_profiles',
31 array( 'userid' => intval($userid),
32 'username' => sanitize_user($username),
33 'groupid' => intval((isset($groupid) && $groupid ? $groupid : $this->wpforo->default_groupid)),
34 'site' => (isset($site) ? sanitize_text_field($site) : '' ),
35 'timezone' => ( isset($timezone) ? sanitize_text_field($timezone) : 'UTC+0' ),
36 'about' => ( isset($about) ? stripslashes( wpforo_kses(trim($about), 'user_description') ) : '' ),
37 'last_login' => ( isset($last_login) ? $last_login : current_time('mysql', 1) ) ),
38 array( '%d', '%s', '%d', '%s', '%s', '%s', '%s' )
39 );
40 }
41
42 function edit_profile($args){
43 if(empty($args)) return FALSE;
44 if( !isset($args['userid']) || !$args['userid'] ) return FALSE;
45 extract( $args, EXTR_OVERWRITE );
46
47 $fields = array();
48 $fields_types = array();
49
50 if(isset($last_login) && $last_login){
51 $fields['last_login'] = sanitize_text_field($last_login);
52 $fields_types[] = '%s';
53 }
54
55 if(isset($groupid) && $groupid){
56 if( $this->wpforo->current_user_groupid == 1 || current_user_can('administrator') ){
57 $fields['groupid'] = intval($groupid);
58 $fields_types[] = '%d';
59 }
60 }
61
62 if(isset($title) && $title){
63 $fields['title'] = sanitize_text_field(trim($title));
64 $fields_types[] = '%s';
65 }
66 if(isset($site)){
67 $fields['site'] = sanitize_text_field(trim($site));
68 $fields_types[] = '%s';
69 }
70 if(isset($icq)){
71 $fields['icq'] = sanitize_text_field(trim($icq));
72 $fields_types[] = '%s';
73 }
74 if(isset($aim)){
75 $fields['aim'] = sanitize_text_field(trim($aim));
76 $fields_types[] = '%s';
77 }
78 if(isset($yahoo)){
79 $fields['yahoo'] = sanitize_text_field(trim($yahoo));
80 $fields_types[] = '%s';
81 }
82 if(isset($msn)){
83 $fields['msn'] = sanitize_text_field(trim($msn));
84 $fields_types[] = '%s';
85 }
86 if(isset($facebook)){
87 $fields['facebook'] = sanitize_text_field(trim($facebook));
88 $fields_types[] = '%s';
89 }
90 if(isset($twitter)){
91 $fields['twitter'] = sanitize_text_field(trim($twitter));
92 $fields_types[] = '%s';
93 }
94 if(isset($gtalk)){
95 $fields['gtalk'] = sanitize_text_field(trim($gtalk));
96 $fields_types[] = '%s';
97 }
98 if(isset($skype)){
99 $fields['skype'] = sanitize_text_field(trim($skype));
100 $fields_types[] = '%s';
101 }
102 if(isset($signature)){
103 $fields['signature'] = stripslashes(wpforo_kses(trim($signature), 'user_description'));
104 $fields_types[] = '%s';
105 }
106 if(isset($about)){
107 $fields['about'] = stripslashes(wpforo_kses(trim($about), 'user_description'));
108 $fields_types[] = '%s';
109 }
110 if(isset($occupation)){
111 $fields['occupation'] = stripslashes(sanitize_text_field(trim($occupation)));
112 $fields_types[] = '%s';
113 }
114 if(isset($location)){
115 $fields['location'] = stripslashes(sanitize_text_field(trim($location)));
116 $fields_types[] = '%s';
117 }
118 if(isset($timezone)){
119 $fields['timezone'] = sanitize_text_field(trim($timezone));
120 $fields_types[] = '%s';
121 }
122 if(isset($avatar_type) && $avatar_type != 'gravatar' && isset($avatar_url) && $avatar_url){
123 $fields['avatar'] = esc_url(trim($avatar_url));
124 $fields_types[] = '%s';
125 }
126 if(isset($avatar_type) && $avatar_type == 'gravatar'){
127 $fields['avatar'] = '';
128 $fields_types[] = '%s';
129 }
130
131 $this->reset($userid);
132
133 return $this->wpforo->db->update(
134 $this->wpforo->db->prefix.'wpforo_profiles',
135 $fields,
136 array('userid' => intval($userid)),
137 $fields_types,
138 array('%d')
139 );
140 }
141
142 function create($args){
143 if(!wpforo_feature('user-register', $this->wpforo)){
144 $this->wpforo->notice->add('User registration is disabled.', 'error');
145 return FALSE;
146 }
147 if( !empty($args) && is_array($args) && !empty($args['user_pass1']) ){
148 remove_action('register_new_user', 'wp_send_new_user_notifications');
149 add_action('register_new_user', array($this, 'send_new_user_notifications'));
150
151 $errors = new WP_Error();
152
153 extract($args, EXTR_OVERWRITE);
154 $sanitized_user_login = sanitize_user( $user_login );
155 $user_email = apply_filters( 'user_registration_email', $user_email );
156 $user_pass1 = trim(substr($user_pass1, 0, 100));
157 $user_pass2 = trim(substr($user_pass2, 0, 100));
158 $illegal_user_logins = array_map( 'strtolower', (array) apply_filters( 'illegal_user_logins', array() ) );
159 if ( $sanitized_user_login == '' ) {
160 $errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) );
161 $this->wpforo->notice->add('Username is missed.', 'error');
162 return FALSE;
163 }elseif ( ! validate_username( $user_login ) ) {
164 $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
165 $sanitized_user_login = '';
166 $this->wpforo->notice->add('Illegal character in username.', 'error');
167 $user_login = '';
168 return FALSE;
169 }elseif( strlen($user_login) < 3 || strlen($user_login) > 30 ){
170 $this->wpforo->notice->add('Username length must be between 3 characters and 30 characters.', 'error');
171 return FALSE;
172 }elseif ( username_exists( $sanitized_user_login ) ) {
173 $errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ) );
174 $this->wpforo->notice->add('Username exists. Please insert another.', 'error');
175 return FALSE;
176 }elseif ( in_array( strtolower( $sanitized_user_login ), $illegal_user_logins ) ) {
177 $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: Sorry, that username is not allowed.' ) );
178 $this->wpforo->notice->add('ERROR: invalid_username. Sorry, that username is not allowed. Please insert another.', 'error');
179 return FALSE;
180 }elseif ( $user_email == '' ) {
181 $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your email address.' ) );
182 $this->wpforo->notice->add('Insert your Email address.', 'error');
183 return FALSE;
184 }elseif ( ! is_email( $user_email ) ) {
185 $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ) );
186 $this->wpforo->notice->add('Invalid Email address', 'error');
187 $user_email = '';
188 return FALSE;
189 }elseif ( email_exists( $user_email ) ) {
190 $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );
191 $this->wpforo->notice->add('Email address exists. Please insert another.', 'error');
192 return FALSE;
193 }elseif( strlen($user_pass1) < 6 || strlen($user_pass1) > 20 ){
194 $this->wpforo->notice->add('Password length must be between 6 characters and 20 characters.', 'error');
195 return FALSE;
196 }elseif($user_pass1 != $user_pass2){
197 $this->wpforo->notice->add('Password mismatch.', 'error');
198 return FALSE;
199 }else{
200 do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
201 $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
202 if ( $errors->get_error_code() ){
203 $args = array();
204 foreach($errors->errors as $u_err) $args[] = $u_err[0];
205 $this->wpforo->notice->add($args, 'error');
206 return FALSE;
207 }
208 $user_id = wp_create_user( $sanitized_user_login, $user_pass1, $user_email );
209 if ( !is_wp_error( $user_id ) && $user_id ) {
210 $creds = array('user_login' => $sanitized_user_login, 'user_password' => $user_pass1 );
211 wp_signon($creds);
212 $this->wpforo->notice->add('Success! Thank you Dear Friend', 'success');
213 do_action( 'register_new_user', $user_id );
214 return $user_id;
215 }
216 }
217 }elseif( !empty($args['user_login']) && !empty($args['user_email']) ){
218 $user_id = register_new_user( $args['user_login'], $args['user_email'] );
219 if ( !is_wp_error( $user_id ) && $user_id ) {
220 $this->wpforo->notice->add('Success! Please check your mail for confirmation.', 'success');
221 return $user_id;
222 }
223 }
224 if(!empty($user_id->errors)){
225 $args = array();
226 foreach($user_id->errors as $u_err) $args[] = $u_err[0];
227 $this->wpforo->notice->add($args, 'error');
228 return FALSE;
229 }
230 $this->wpforo->notice->add('Registration Error', 'error');
231 return FALSE;
232 }
233
234 function edit( $args = array() ){
235 if( empty($args) && empty($_REQUEST['member']) ) return FALSE;
236 if( empty($args) && !empty($_REQUEST['member']) ) $args = $_REQUEST['member'];
237 extract($args, EXTR_OVERWRITE);
238
239 if( isset($userid) && isset($display_name) && isset($user_email) && isset($user_nickname) ){
240 $userid = intval($userid);
241 $user_email = sanitize_email($user_email);
242 if ( ! is_email( $user_email ) ) {
243 $this->wpforo->notice->add('Invalid Email address', 'error');
244 $user_email = '';
245 return FALSE;
246 }elseif ( ( $owner_id = email_exists( $user_email ) ) && ( $owner_id != $userid ) ) {
247 $this->wpforo->notice->add('This email address is already registered. Please insert another.', 'error');
248 return FALSE;
249 }
250
251 $user_nickname = sanitize_title($user_nickname);
252 if( is_numeric($user_nickname) ){
253 $this->wpforo->notice->add('Numerical nicknames are not allowed. Please insert another.', 'error');
254 return FALSE;
255 }
256 $sql = "SELECT `ID` FROM `".$this->wpforo->db->base_prefix."users` WHERE `ID` != ". intval($userid) ." AND ( `user_nicename` LIKE '".esc_sql($user_nickname)."' OR `ID` LIKE '".esc_sql($user_nickname)."' )";
257 if( $this->wpforo->db->get_var($sql)){
258 $this->wpforo->notice->add('This nickname is already registered. Please insert another.', 'error');
259 return FALSE;
260 }
261
262 if ( !is_user_logged_in() || !$this->wpforo->perm->user_can_manage_user( $this->wpforo->current_userid, $userid ) ) {
263 $this->wpforo->notice->add('Permission denied', 'error');
264 return FALSE;
265 }
266
267 if( $display_name && $user_email && $user_nickname ){
268 $this->wpforo->db->update(
269 $this->wpforo->db->base_prefix."users",
270 array(
271 'display_name' => sanitize_text_field($display_name),
272 'user_nicename' => sanitize_title($user_nickname),
273 'user_email' => sanitize_email($user_email)
274 ),
275 array('ID' => $userid),
276 array('%s','%s','%s'),
277 array('%d')
278 );
279 $this->wpforo->db->update(
280 $this->wpforo->db->base_prefix."usermeta",
281 array('meta_value' => sanitize_title($user_nickname)),
282 array('user_id' => $userid, 'meta_key' => 'nickname'),
283 array('%s'),
284 array('%d', '%s')
285 );
286 $this->reset($userid);
287 }
288
289 if( FALSE !== $this->edit_profile($args) ){
290 $this->wpforo->notice->add('Your profile data have been successfully updated.', 'success');
291 return $userid;
292 }
293 }
294
295 $this->wpforo->notice->add('Something wrong with profile data.', 'error');
296 return FALSE;
297 }
298
299 function upload_avatar(){
300 if( !isset($_POST['member']['userid']) || !$userid = intval($_POST['member']['userid']) ) return;
301 if( !$user = $this->get_member($userid) ) return;
302 $username = urldecode($user['user_nicename']);
303 if(isset($_FILES['avatar']) && !empty($_FILES['avatar']) && isset($_FILES['avatar']['name']) && $_FILES['avatar']['name']){
304
305 $name = sanitize_file_name($_FILES['avatar']['name']); //myimg.png
306 $type = sanitize_mime_type($_FILES['avatar']['type']); //image/png
307 $tmp_name = sanitize_text_field($_FILES['avatar']['tmp_name']); //D:\wamp\tmp\php986B.tmp
308 $error = sanitize_text_field($_FILES['avatar']['error']); //0
309 $size = intval($_FILES['avatar']['size']); //6112
310
311 if( $size > 2*1048576 ){
312 $this->wpforo->notice->clear();
313 $this->wpforo->notice->add('Avatar image is too big maximum allowed size is 2MB', 'error');
314 return FALSE;
315 }
316
317 if( $error ){
318 $error = wpforo_file_upload_error($error);
319 $this->wpforo->notice->clear();
320 $this->wpforo->notice->add($error, 'error');
321 return FALSE;
322 }
323
324 $upload_dir = wp_upload_dir();
325 $uplds_dir = $upload_dir['basedir']."/wpforo";
326 $avatar_dir = $upload_dir['basedir']."/wpforo/avatars";
327 if(!is_dir($uplds_dir)) wp_mkdir_p($uplds_dir);
328 if(!is_dir($avatar_dir)) wp_mkdir_p($avatar_dir);
329
330 $ext = pathinfo($name, PATHINFO_EXTENSION);
331 if( !wpforo_is_image($ext) ){
332 $this->wpforo->notice->clear();
333 $this->wpforo->notice->add('Incorrect file format. Allowed formats: jpeg, jpg, png, gif.', 'error');
334 return FALSE;
335 }
336
337 $fnm = pathinfo($username, PATHINFO_FILENAME);
338 $fnm = str_replace(' ', '-', $fnm);
339 while(strpos($fnm, '--') !== FALSE) $fnm = str_replace('--', '-', $fnm);
340 $fnm = preg_replace("/[^-a-zA-Z0-9]/", "", $fnm);
341 $fnm = trim($fnm, "-");
342
343 $avatar_fname = $fnm.( $fnm ? '_' : '' ).$userid.".".$ext;
344 $avatar_path = $avatar_dir."/".$avatar_fname;
345
346 if(is_dir($avatar_dir)){
347 if(move_uploaded_file($tmp_name, $avatar_path)) {
348 $image = wp_get_image_editor( $avatar_path );
349 if ( ! is_wp_error( $image ) ) {
350 $image->resize( 150, 150, true );
351 $image->save( $avatar_path );
352 }
353 $blog_url = preg_replace('#^https?\:#is', '', $upload_dir['baseurl']);
354 $this->wpforo->db->update($this->wpforo->db->prefix.'wpforo_profiles', array('avatar' => $blog_url . "/wpforo/avatars/" . $avatar_fname), array('userid' => intval($userid)), array('%s'), array('%d'));
355 $this->reset($userid);
356 }
357 }
358 }
359 }
360
361 function synchronize_user($userid){
362 if(!$userid) return FALSE;
363 if( $user = get_userdata($userid) ){
364 if( is_super_admin( $userid ) || in_array('administrator', $user->roles) ){
365 $groupid = 1;
366 }elseif( in_array('editor', $user->roles) ){
367 $groupid = 2;
368 }elseif( in_array('customer', $user->roles) ){
369 $groupid = 5;
370 }else{
371 $groupid = $this->wpforo->default_groupid;
372 }
373 $insert_groupid = (isset($_POST['wpforo_usergroup'])) ? intval($_POST['wpforo_usergroup']) : $groupid;
374 $insert_timezone = (isset($_POST['wpforo_usertimezone'])) ? sanitize_text_field($_POST['wpforo_usertimezone']) : '';
375 $about = get_user_meta( $userid, 'description', true );
376 return $this->add_profile(
377 array( 'userid' => intval($userid),
378 'username' => sanitize_user($user->user_login),
379 'groupid' => intval($insert_groupid),
380 'site' => esc_url($user->user_url),
381 'timezone' => sanitize_text_field($insert_timezone),
382 'about' => stripslashes( wpforo_kses(trim($about), 'user_description') ),
383 'last_login' => sanitize_text_field($user->user_registered) ) );
384 }
385 return FALSE;
386 }
387
388 function synchronize_users(){
389 $sql = "SELECT `ID` FROM `".$this->wpforo->db->base_prefix."users` WHERE `ID` NOT IN( SELECT `userid` FROM `".$this->wpforo->db->prefix."wpforo_profiles` )";
390 $userids = $this->wpforo->db->get_col($sql);
391 if( !empty($userids) ){
392 foreach($userids as $userid){
393 $this->synchronize_user($userid);
394 }
395 }
396 }
397
398 function get_member($args = array(), $cache = false){
399
400 $cache = $this->wpforo->cache->on('memory_cashe');
401
402 if(is_array($args)){
403
404 $default = array(
405 'userid' => NULL, // userid
406 'username' => '' // username
407 );
408
409 }else{
410
411 $default = array(
412 'userid' => $args, // userid
413 'username' => '' // username
414 );
415
416 }
417
418 $args = wpforo_parse_args( $args, $default );
419
420 if(isset($args['userid'])){
421 if( $cache && isset(self::$cache['user'][$args['userid']]) ){
422 return self::$cache['user'][$args['userid']];
423 }
424 }
425
426 $user_meta_obj = true;
427
428 if(!empty($args)){
429
430 extract($args, EXTR_OVERWRITE);
431
432 $do_db_cache = wpforo_feature('member_cashe', $this->wpforo);
433
434 $userid = intval($userid);
435 $username = sanitize_user($username);
436
437 if( $do_db_cache ){
438 if( $username ){
439 $user_obj = get_user_by( 'user_nicename', $username );
440 if(!empty($user_obj)) $userid = $user_obj->ID;
441 $member = get_user_meta( $userid, '_wpf_member_obj', true );
442 }elseif( $userid ){
443 $member = get_user_meta( $userid, '_wpf_member_obj', true );
444 }else{
445 $user_meta_obj = true;
446 }
447 }else{
448 $member = array();
449 }
450
451 if(empty($member)){
452 $user_meta_obj = false;
453 $sql = "SELECT *, ug.name AS groupname FROM `".$this->wpforo->db->base_prefix."users` u
454 LEFT JOIN `".$this->wpforo->db->prefix."wpforo_profiles` p ON p.`userid` = u.`ID`
455 LEFT JOIN `".$this->wpforo->db->prefix."wpforo_usergroups` ug ON ug.`groupid` = p.`groupid`";
456 $wheres = array();
457 if($userid) $wheres[] = "`ID` = " . intval($userid);
458 if($username) $wheres[] = "`user_nicename` = '" . esc_sql($username) . "'";
459 if( !empty($wheres) ) $sql .= " WHERE " . implode($wheres, " AND ");
460 $member = $this->wpforo->db->get_row($sql, ARRAY_A);
461 }
462
463 if(!empty($member)) {
464 if( $do_db_cache ){
465 if(!$user_meta_obj) {
466 $member['profile_url'] = $this->profile_url( $member );
467 $member['stat'] = $this->get_stat( $member, false, true );
468 update_user_meta( $userid, '_wpf_member_obj', $member );
469 }
470 }else{
471 $member['profile_url'] = $this->profile_url( $member );
472 $member['stat'] = $this->get_stat( $member, false, true );
473 }
474 }
475
476 if($cache && isset($userid)){
477 return self::$cache['user'][$userid] = $member;
478 }else{
479 return $member;
480 }
481 }
482 }
483
484 function get_members($args = array(), &$items_count = 0){
485
486 $default = array(
487 'include' => array(), // array( 2, 10, 25 )
488 'exclude' => array(), // array( 2, 10, 25 )
489 'status' => array('active', 'banned'), // 'active', 'blocked', 'trashed', 'spamer'
490 'groupid' => NULL, // groupid
491 'orderby' => 'userid', //
492 'order' => 'ASC', // ASC DESC
493 'offset' => 0, // OFFSET
494 'row_count' => NULL // ROW COUNT
495 );
496
497 $args = wpforo_parse_args( $args, $default );
498 if(!empty($args)){
499 extract($args, EXTR_OVERWRITE);
500
501 $include = wpforo_parse_args( $include );
502 $exclude = wpforo_parse_args( $exclude );
503
504 $sql = "SELECT *, ug.name AS groupname FROM `".$this->wpforo->db->base_prefix."users` u
505 LEFT JOIN `".$this->wpforo->db->prefix."wpforo_profiles` p ON p.`userid` = u.`ID`
506 LEFT JOIN `".$this->wpforo->db->prefix."wpforo_usergroups` ug ON ug.`groupid` = p.`groupid`";
507 $wheres = array();
508 if(!empty($include)) $wheres[] = "u.`ID` IN(" . implode(', ', array_map('intval', $include)) . ")";
509 if(!empty($exclude)) $wheres[] = "u.`ID` NOT IN(" . implode(', ', array_map('intval', $exclude)) . ")";
510 if(!empty($status)) $wheres[] = " p.`status` IN('" . implode("','", array_map('esc_sql', array_map('sanitize_text_field', $status)) ) . "')";
511 if($groupid != NULL) $wheres[] = "p.`groupid` = " . intval($groupid);
512
513 if(!empty($wheres)) $sql .= " WHERE " . implode($wheres, " AND ");
514
515 $item_count_sql = preg_replace('#SELECT.+?FROM#isu', 'SELECT count(*) FROM', $sql);
516 if( $item_count_sql ) $items_count = $this->wpforo->db->get_var($item_count_sql);
517
518 if( $orderby == 'groupid' ) $orderby = 'p.`groupid`';
519 $sql .= esc_sql(" ORDER BY $orderby " . $order);
520 if($row_count) $sql .= esc_sql(" LIMIT $offset,$row_count");
521
522 return $this->wpforo->db->get_results($sql, ARRAY_A);
523 }
524 }
525
526 function search($needle, $fields = array(), $limit = NULL){
527
528 if($needle != ''){
529 $needle = sanitize_text_field($needle);
530 if(empty($fields)){
531 $fields = array(
532 'title',
533 'user_nicename',
534 'user_email',
535 'signature'
536 );
537 }
538
539 $sql = "SELECT `ID` FROM `".$this->wpforo->db->base_prefix."users` u LEFT JOIN `".$this->wpforo->db->prefix."wpforo_profiles` p ON p.`userid` = u.`ID`";
540 $wheres = array();
541
542 foreach($fields as $field){
543 $field = sanitize_text_field($field);
544 $wheres[] = "`".esc_sql($field)."` LIKE '%" . esc_sql($needle) ."%'";
545 }
546
547 if(!empty($wheres)){
548 $sql .= " WHERE " . implode($wheres, " OR ");
549 if( $limit ) $sql .= " LIMIT " . intval($limit);
550
551 return $this->wpforo->db->get_col($sql);
552 }else{
553 return array();
554 }
555 }else{
556 return array();
557 }
558
559 }
560
561 function ban($userid){
562 if( $userid == $this->wpforo->current_userid ){
563 $this->wpforo->notice->add('You can\'t make yourself banned user', 'error');
564 return FALSE;
565 }
566 if( !$this->wpforo->perm->usergroup_can('bm') || !$this->wpforo->perm->user_can_manage_user( $this->wpforo->current_userid, intval( $userid ) )){
567 $this->wpforo->notice->add('Permission denied for this action', 'error');
568 return FALSE;
569 }
570 if( FALSE !== $this->wpforo->db->update(
571 $this->wpforo->db->prefix.'wpforo_profiles',
572 array('status' => 'banned'),
573 array('userid' => intval( $userid )),
574 array('%s'),
575 array('%d')
576 )
577 ){
578 $this->reset($userid);
579 $this->wpforo->notice->add('User successfully banned from wpforo', 'success');
580 return TRUE;
581 }
582
583 $this->wpforo->notice->add('User ban action error', 'error');
584 return FALSE;
585 }
586
587 function unban($userid){
588 if( !$this->wpforo->perm->usergroup_can('bm') || !$this->wpforo->perm->user_can_manage_user( $this->wpforo->current_userid, intval( $userid ) )){
589 $this->wpforo->notice->add('Permission denied for this action', 'error');
590 return FALSE;
591 }
592 if( FALSE !== $this->wpforo->db->update(
593 $this->wpforo->db->prefix.'wpforo_profiles',
594 array('status' => 'active'),
595 array('userid' => intval( $userid )),
596 array('%s'),
597 array('%d')
598 )
599 ){
600 $this->reset($userid);
601 $this->wpforo->notice->add('User successfully unbanned from wpforo', 'success');
602 return TRUE;
603 }
604
605 $this->wpforo->notice->add('User unban action error', 'error');
606 return FALSE;
607 }
608
609 /**
610 *
611 * @param int $userid
612 * @param int $reassign
613 *
614 * @return bool true | false if user successfully deleted
615 */
616 public function delete( $userid, $reassign = NULL ){
617 if( !($userid = intval($userid)) ) return FALSE;
618 if( !$this->wpforo->perm->usergroup_can('dm') || !$this->wpforo->perm->user_can_manage_user( $this->wpforo->current_userid, intval( $userid ) )){
619 $this->wpforo->notice->add('Permission denied for this action', 'error');
620 return FALSE;
621 }
622
623 if( !($reassign = intval($reassign)) ){
624 if( $postids = $this->wpforo->db->get_col( $this->wpforo->db->prepare( "SELECT postid FROM {$this->wpforo->db->prefix}wpforo_posts WHERE userid = %d", $userid ) ) ){
625 foreach( $postids as $postid ) $this->wpforo->post->delete($postid);
626 }
627
628 if( $topicids = $this->wpforo->db->get_col( $this->wpforo->db->prepare( "SELECT topicid FROM {$this->wpforo->db->prefix}wpforo_topics WHERE userid = %d", $userid ) ) ){
629 foreach( $topicids as $topicid ) $this->wpforo->topic->delete($topicid, false);
630 }
631 }else{
632 $this->wpforo->db->update( $this->wpforo->db->prefix."wpforo_topics", array('userid' => $reassign), array('userid' => $userid) );
633 $this->wpforo->db->update( $this->wpforo->db->prefix."wpforo_posts", array('userid' => $reassign), array('userid' => $userid) );
634 $this->wpforo->db->update( $this->wpforo->db->prefix."wpforo_likes", array('post_userid' => $reassign), array('post_userid' => $userid) );
635 $this->wpforo->db->update( $this->wpforo->db->prefix."wpforo_votes", array('post_userid' => $reassign), array('post_userid' => $userid) );
636 if( $user_stats = $this->wpforo->db->get_row(
637 $this->wpforo->db->prepare( "SELECT
638 SUM(`posts`) AS posts,
639 SUM(`questions`) AS questions,
640 SUM(`answers`) AS answers,
641 SUM(`comments`) AS comments
642 FROM {$this->wpforo->db->prefix}wpforo_profiles
643 WHERE userid IN( %d , %d )", $userid, $reassign
644 ),
645 ARRAY_A
646 )
647 ){
648 $this->wpforo->db->update(
649 $this->wpforo->db->prefix.'wpforo_profiles',
650 array(
651 'posts' => $user_stats['posts'],
652 'questions' => $user_stats['questions'],
653 'answers' => $user_stats['answers'],
654 'comments' => $user_stats['comments']
655 ),
656 array('userid' => $reassign),
657 array('%d','%d','%d','%d'),
658 array('%d')
659 );
660 }
661 }
662
663 $this->wpforo->db->delete(
664 $this->wpforo->db->prefix.'wpforo_subscribes', array( 'userid' => $userid ), array( '%d' )
665 );
666
667 $this->wpforo->db->delete(
668 $this->wpforo->db->prefix.'wpforo_views', array( 'userid' => $userid ), array( '%d' )
669 );
670
671 $this->wpforo->db->delete(
672 $this->wpforo->db->prefix.'wpforo_likes', array( 'userid' => $userid ), array( '%d' )
673 );
674
675 $this->wpforo->db->delete(
676 $this->wpforo->db->prefix.'wpforo_votes', array( 'userid' => $userid ), array( '%d' )
677 );
678
679 if( FALSE !== $this->wpforo->db->delete(
680 $this->wpforo->db->prefix.'wpforo_profiles', array( 'userid' => $userid ), array( '%d' )
681 )
682 ){
683 wpforo_clean_cache();
684 $this->clear_db_cache();
685 $this->wpforo->notice->add('User successfully deleted from wpforo', 'success');
686 return TRUE;
687 }
688
689 $this->wpforo->notice->add('User delete error', 'error');
690 return FALSE;
691 }
692
693 public function avatar($member, $attr = '', $size = '', $cache = false){
694
695 if(!isset($member['userid'])) return;
696 $cache = $this->wpforo->cache->on('memory_cashe');
697
698 $src = $member['avatar'];
699 $userid = $member['userid'];
700 if($cache && isset(self::$cache['avatar'][$userid])){
701 if(self::$cache['avatar'][$userid]['attr'] == $attr && self::$cache['avatar'][$userid]['size'] == $size){
702 if(isset(self::$cache['avatar'][$userid]['img'])){
703 return self::$cache['avatar'][$userid]['img'];
704 }
705 }
706 }
707 if($src && wpforo_feature('custom-avatars', $this->wpforo)){
708 $attr = ($attr ? $attr : 'height="96" width="96"');
709 $img = '<img class="avatar" src="'.esc_url($src).'" '. $attr .' />';
710 }else{
711 $img = ($size) ? get_avatar($userid, $size) : get_avatar($userid);
712 if($attr) $img = str_replace('<img', '<img ' . $attr, $img);
713 }
714 if($cache){
715 self::$cache['avatar'][$userid]['attr'] = $attr;
716 self::$cache['avatar'][$userid]['size'] = $size;
717 return self::$cache['avatar'][$userid]['img'] = $img;
718 }
719 else{
720 return $img;
721 }
722 }
723
724 function get_avatar($userid, $attr = '', $size = '', $cache = false){
725
726 $cache = $this->wpforo->cache->on('memory_cashe');
727
728 if($cache && isset(self::$cache['avatar'][$userid])){
729 if(self::$cache['avatar'][$userid]['attr'] == $attr && self::$cache['avatar'][$userid]['size'] == $size){
730 if(isset(self::$cache['avatar'][$userid]['img'])){
731 return self::$cache['avatar'][$userid]['img'];
732 }
733 }
734 }
735 $src = $this->wpforo->db->get_var("SELECT `avatar` FROM `".$this->wpforo->db->prefix."wpforo_profiles` WHERE `userid` = ".intval($userid));
736 if($src && wpforo_feature('custom-avatars', $this->wpforo)){
737 $attr = ($attr ? $attr : 'height="96" width="96"');
738 $img = '<img class="avatar" src="'.esc_url($src).'" '. $attr .' />';
739 }else{
740 $img = ($size) ? get_avatar($userid, $size) : get_avatar($userid);
741 if($attr) $img = str_replace('<img', '<img ' . $attr, $img);
742 }
743 if($cache){
744 self::$cache['avatar'][$userid]['attr'] = $attr;
745 self::$cache['avatar'][$userid]['size'] = $size;
746 return self::$cache['avatar'][$userid]['img'] = $img;
747 }
748 else{
749 return $img;
750 }
751 }
752
753 public function get_avatar_url($userid){
754 return $this->wpforo->db->get_var("SELECT `avatar` FROM `".$this->wpforo->db->prefix."wpforo_profiles` WHERE `userid` = ".intval($userid));
755 }
756
757 function get_topics_count( $userid ){
758 $count = $this->wpforo->db->get_var("SELECT count(topicid) FROM `".$this->wpforo->db->prefix."wpforo_topics` WHERE `userid` = ".intval($userid));
759 return $count;
760 }
761
762 function get_questions_count( $userid ){
763 $count = $this->wpforo->db->get_var("SELECT count(topicid) FROM `".$this->wpforo->db->prefix."wpforo_topics` WHERE `userid` = ".intval($userid));
764 return $count;
765 }
766
767 function get_answers_count( $userid ){
768 $count = $this->wpforo->db->get_var("SELECT count(postid) FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `is_answer` = 1 AND `userid` = ".intval($userid));
769 return $count;
770 }
771
772 function get_question_comments_count( $userid ){
773 $count = $this->wpforo->db->get_var("SELECT count(postid) FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `parentid` > 0 AND `userid` = ".intval($userid));
774 return $count;
775 }
776
777 function get_replies_count( $userid ){
778 $count = $this->wpforo->db->get_var("SELECT count(postid) FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `userid` = ".intval($userid));
779 return $count;
780 }
781
782 function get_likes_count( $userid ){
783 $count = $this->wpforo->db->get_var("SELECT count(likeid) FROM `".$this->wpforo->db->prefix."wpforo_likes` WHERE `userid` = ".intval($userid));
784 return $count;
785 }
786
787 function get_votes_count( $userid ){
788 $count = $this->wpforo->db->get_var("SELECT count(voteid) FROM `".$this->wpforo->db->prefix."wpforo_votes` WHERE `userid` = ".intval($userid));
789 return $count;
790 }
791
792 // how many times the user like or vote
793 function get_votes_and_likes_count( $userid ){
794 return $this->get_votes_count( intval($userid) ) + $this->get_likes_count( intval($userid) );
795 }
796
797 //getting user's posts votes and likes count
798 function get_user_votes_and_likes_count( $userid ){
799 $votes_count = $this->wpforo->db->get_var("SELECT count(voteid) FROM `".$this->wpforo->db->prefix."wpforo_votes` WHERE `post_userid` = ".intval($userid));
800 $likes_count = $this->wpforo->db->get_var("SELECT count(likeid) FROM `".$this->wpforo->db->prefix."wpforo_likes` WHERE `post_userid` = ".intval($userid));
801 return $votes_count + $likes_count;
802 }
803
804 function get_profile_url( $arg, $template = 'profile' ){
805 if(!$arg) return wpforo_home_url();
806 $userid = intval( basename($arg) );
807 $member_args = ( $userid ? $userid : array( 'username' => basename($arg) ) );
808 $user = $this->get_member( $member_args );
809 if(empty($user)) return wpforo_home_url();
810 $user_slug = ( wpfo($this->wpforo->member_options['url_structure'], false) == 'id' ? $user['ID'] : $user['user_nicename'] );
811 return wpforo_home_url("$template/$user_slug");
812 }
813
814 function profile_url( $member = array(), $template = 'profile' ){
815 if(isset($member['ID']) || isset($member['user_nicename'])){
816 $user_slug = ( wpfo($this->wpforo->member_options['url_structure'], false) == 'id' ? $member['ID'] : $member['user_nicename'] );
817 $profile_url = wpforo_home_url("$template/$user_slug");
818 $profile_url = apply_filters( 'wpforo_profile_url', $profile_url, $member, $template );
819 }
820 else{
821 $profile_url = wpforo_home_url();
822 $profile_url = apply_filters( 'wpforo_no_profile_url', $profile_url, $template );
823
824 }
825 return $profile_url;
826 }
827
828 //$args = UserID or Member Object
829 //$live_count = TRUE / FALSE
830 function get_stat( $args = array(), $live_count = false, $cache = false ){
831
832 $cache = $this->wpforo->cache->on('memory_cashe');
833
834 $stat = array( 'points' => 0,
835 'rating' => 0,
836 'rating_procent' => 0,
837 'color' => $this->rating(0, 'color'),
838 'badge' => $this->rating(0, 'icon'),
839 'posts' => 0,
840 'topics' => 0,
841 'questions' => 0,
842 'answers' => 0,
843 'question_comments' => 0,
844 'likes' => 0,
845 'liked' => 0,
846 'title' => $this->rating(0, 'title'));
847
848 $userid = ( isset($args['userid']) && $args['userid'] ) ? $args['userid'] : $args;
849
850 if( $cache && isset(self::$cache['stat'][$userid]) ){
851 return self::$cache['stat'][$userid];
852 }
853
854 if( is_array($args) && isset($args['userid']) ){
855 $userid = $args['userid'];
856 $stat['topics'] = (int)$this->get_topics_count( $userid );
857 if(isset($args['questions'])) $stat['questions'] = intval($args['questions']);
858 if(isset($args['answers'])) $stat['answers'] = intval($args['answers']);
859 if(isset($args['posts'])) $stat['posts'] = intval($args['posts']);
860 if(isset($args['comments'])) $stat['question_comments'] = intval($args['comments']);
861 }
862 elseif($userid = wpforo_bigintval($args)){
863 $stat['topics'] = (int)$this->get_topics_count( $userid );
864 if($live_count){
865 if($questions = $this->get_questions_count( $userid )) $stat['questions'] = $questions;
866 if($answers = $this->get_answers_count( $userid )) $stat['answers'] = $answers;
867 if($posts = $this->get_replies_count( $userid )) $stat['posts'] = $posts;
868 if($question_comments = $this->get_question_comments_count( $userid )) $stat['question_comments'] = $question_comments;
869 }
870 else{
871 $profile = $this->wpforo->db->get_var("SELECT `posts`, `questions`, `answers`, `comments` FROM `".$this->wpforo->db->prefix."wpforo_profiles` WHERE `userid` = ".intval($userid));
872 if(isset($profile['questions'])) $stat['questions'] = intval($profile['questions']);
873 if(isset($profile['answers'])) $stat['answers'] = intval($profile['answers']);
874 if(isset($profile['posts'])) $stat['posts'] = intval($profile['posts']);
875 if(isset($profile['comments'])) $stat['question_comments'] = intval($profile['comments']);
876 }
877 }
878
879 if( $userid ){
880 if($likes = $this->get_votes_and_likes_count( $userid )) $stat['likes'] = $likes;
881 if($liked = $this->get_user_votes_and_likes_count( $userid )) $stat['liked'] = $liked;
882 if($stat['posts']) $stat['points'] = $stat['posts']; //TO-DO: Point counter function based on all stat values.
883 if($stat['points']) $stat['rating'] = $this->rating_level($stat['points'], false);
884 if($stat['rating']) {
885 $stat['rating_procent'] = $stat['rating'] * 10;
886 $stat['title'] = $this->rating(intval($stat['rating']), 'title');
887 $stat['color'] = $this->rating(intval($stat['rating']), 'color');
888 $stat['badge'] = $this->rating(intval($stat['rating']), 'icon');
889 }
890 }
891
892 if($cache && isset($userid)){
893 return self::$cache['stat'][$userid] = $stat;
894 }
895 else{
896 return $stat;
897 }
898 }
899
900 function get_count(){
901 return $this->wpforo->db->get_var( "SELECT COUNT(p.`userid`) FROM `".$this->wpforo->db->prefix."wpforo_profiles` p
902 INNER JOIN `".$this->wpforo->db->base_prefix."users` u ON u.`ID` = p.`userid` WHERE p.`status` NOT LIKE 'trashed'" );
903 }
904
905
906 function is_online( $userid, $duration = 240 ){
907
908 $cache = $this->wpforo->cache->on('memory_cashe');
909
910 if( $cache && isset(self::$cache['online'][$userid]) ){
911 if(self::$cache['online'][$userid]['durration'] == $duration ){
912 if(isset(self::$cache['online'][$userid]['status'])){
913 return self::$cache['online'][$userid]['status'];
914 }
915 }
916 }
917 if($duration == 240) $duration = $this->wpforo->member_options['online_status_timeout'];
918 $online_time = intval( get_user_meta($userid, 'wpforo_online_time', TRUE) );
919 $current_time = current_time( 'timestamp', 1 );
920 $online_duration = $current_time - $online_time;
921 if( $online_duration < $duration ) {
922 $status = true;
923 }
924 else{
925 $status = false;
926 }
927 if( $cache ){
928 self::$cache['online'][$userid]['durration'] = $duration;
929 return self::$cache['online'][$userid]['status'] = $status;
930 }
931 else{
932 return $status;
933 }
934 }
935
936 public function show_online_indicator($userid, $ico = TRUE){
937 if( $this->is_online($userid)) : ?>
938
939 <?php if($ico) : ?>
940 <i class="fa fa-lightbulb-o fa-0x wpfcl-8" title="<?php wpforo_phrase('Online') ?>"></i>
941 <?php else : wpforo_phrase('Online'); endif ?>
942
943 <?php else : ?>
944
945 <?php if($ico) : ?>
946 <i class="fa fa-lightbulb-o fa-0x wpfcl-0" title="<?php wpforo_phrase('Offline') ?>"></i>
947 <?php else : wpforo_phrase('Offline'); endif ?>
948
949 <?php endif;
950 }
951
952 function online_members_count( $duration = 240 ){
953 if($duration == 240) $duration = $this->wpforo->member_options['online_status_timeout'];
954 $current_time = current_time( 'timestamp', 1 );
955 $online_timeframe = $current_time - $duration;
956 return $this->wpforo->db->get_var( "SELECT COUNT(`user_id`) FROM `".$this->wpforo->db->base_prefix."usermeta` WHERE meta_key = 'wpforo_online_time' AND meta_value > " . wpforo_bigintval($online_timeframe) );
957
958 }
959
960 function get_online_members( $count = 1, $duration = 240 ){
961 if($duration == 240) $duration = $this->wpforo->member_options['online_status_timeout'];
962 $current_time = current_time( 'timestamp', 1 );
963 $online_timeframe = $current_time - $duration;
964 $onlinemembers_ids = $this->wpforo->db->get_col( "SELECT `user_id` FROM `".$this->wpforo->db->base_prefix."usermeta` WHERE meta_key = 'wpforo_online_time' AND meta_value > " . wpforo_bigintval($online_timeframe) );
965 if(!empty($onlinemembers_ids)){
966 $args = array(
967 'include' => $onlinemembers_ids, // array( 2, 10, 25 )
968 'orderby' => 'userid', // forumid, order, parentid
969 'row_count' => $count,
970 'order' => 'ASC', // ASC DESC
971 );
972 return $this->get_members( $args );
973 }
974 else{
975 return array();
976 }
977 }
978
979 function levels(){
980 $levels = array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
981 return $levels;
982 }
983
984 function rating( $level = false, $var = false, $default = false ){
985
986 $rating = array();
987 $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' );
988 $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 );
989 $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' );
990 $rating['icon'] = array( 0 => 'fa-star-half-o', 1 => 'fa-star', 2 => 'fa-star', 3 => 'fa-star', 4 => 'fa-star', 5 => 'fa-star', 6 => 'fa-certificate', 7 => 'fa-certificate', 8 => 'fa-certificate', 9 => 'fa-shield', 10 => 'fa-trophy' );
991
992 if(!empty($this->wpforo->member_options['rating'])){
993
994 if($level === false) return $this->wpforo->member_options['rating'];
995 if(!empty($this->wpforo->member_options['rating'][$level])){
996
997 if(!$var) return $this->wpforo->member_options['rating'][$level];
998 if(!empty($this->wpforo->member_options['rating'][$level][$var])){
999
1000 return $this->wpforo->member_options['rating'][$level][$var];
1001
1002 }
1003 }
1004 }
1005 if( $level !== false && $var ) { return $rating[$var][$level]; }
1006 elseif( $level !== false && !$var ){ foreach( $rating as $variable => $values ){ $level_data[$variable] = $values[$level];} return $level_data; }
1007 elseif( $level === false && !$var ) return $rating;
1008 else return array();
1009 }
1010
1011 function rating_level($member_posts, $percent = TRUE){
1012 $bar = 0;
1013 if($member_posts < $this->rating(1, 'points')){$bar = 0;}
1014 elseif($member_posts < $this->rating(2, 'points')){$bar = 10;}
1015 elseif($member_posts < $this->rating(3, 'points')){$bar = 20;}
1016 elseif($member_posts < $this->rating(4, 'points')){$bar = 30;}
1017 elseif($member_posts < $this->rating(5, 'points')){$bar = 40;}
1018 elseif($member_posts < $this->rating(6, 'points')){$bar = 50;}
1019 elseif($member_posts < $this->rating(7, 'points')){$bar = 60;}
1020 elseif($member_posts < $this->rating(8, 'points')){$bar = 70;}
1021 elseif($member_posts < $this->rating(9, 'points')){$bar = 80;}
1022 elseif($member_posts < $this->rating(10, 'points')){$bar = 90;}
1023 else{$bar = 100;}
1024 if($percent){
1025 return $bar;
1026 }else{
1027 return floor($bar/10);
1028 }
1029 }
1030
1031 function rating_badge($level = 0, $view = 'short'){
1032
1033 $level = ( $level > 10 ) ? floor($level/10) : $level;
1034
1035 if($level == 0){
1036 return '<i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i>';
1037 }
1038 elseif($level > 0 && $level < 6){
1039 if( $view == 'full' ){
1040 return str_repeat(' <i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i> ', $level);
1041 }
1042 else{
1043 return '<span>' . esc_html($level) . '</span> <i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i>';
1044 }
1045 }
1046 elseif($level > 5 && $level < 9){
1047 if( $view == 'full' ){
1048 return str_repeat(' <i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i> ', ($level-5));
1049 }
1050 else{
1051 return '<span>' . esc_html($level-5) . '</span> <i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i>';
1052 }
1053 }
1054 elseif($level > 8){
1055 return '<i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i>';
1056 }
1057 else{
1058 return;
1059 }
1060 }
1061
1062 public function reset($userid){
1063 if( !$userid ) return;
1064 $this->wpforo->db->query( "DELETE FROM `" . $this->wpforo->db->base_prefix ."usermeta` WHERE `meta_key` = '_wpf_member_obj' AND `user_id` = " . intval($userid) );
1065 wpforo_clean_cache( $userid, 'user' );
1066 }
1067
1068 public function clear_db_cache(){
1069 $this->wpforo->db->query( "DELETE FROM `" . $this->wpforo->db->base_prefix ."usermeta` WHERE `meta_key` = '_wpf_member_obj'" );
1070 }
1071
1072 public function init_current_user(){
1073 $current_user = wp_get_current_user();
1074 if( $current_user->exists() ){
1075 $user = $this->get_member( $current_user->ID );
1076 $status = ( isset($user['status']) ? $user['status'] : '' );
1077 if( $status == 'active' ){
1078 update_user_meta( $current_user->ID, 'wpforo_online_time', current_time( 'timestamp', 1 ) );
1079 $this->wpforo->current_user = $user;
1080 $this->wpforo->current_user_groupid = $this->wpforo->current_user['groupid'];
1081 $this->wpforo->current_userid = $current_user->ID;
1082 $this->wpforo->current_username = $current_user->user_login;
1083 $this->wpforo->current_user_email = $current_user->user_email;
1084 $this->wpforo->current_user_display_name = $current_user->display_name;
1085 }else{
1086 $this->wpforo->current_user = array();
1087 $this->wpforo->current_user_groupid = 4;
1088 $this->wpforo->current_userid = 0;
1089 $this->wpforo->current_username = '';
1090 $this->wpforo->current_user_email = '';
1091 $this->wpforo->current_user_display_name = '';
1092 }
1093 $this->wpforo->current_user_status = $status;
1094 }else{
1095 $this->wpforo->current_user = array();
1096 $this->wpforo->current_user_groupid = 4;
1097 $this->wpforo->current_userid = 0;
1098 $this->wpforo->current_username = '';
1099 $this->wpforo->current_user_email = '';
1100 $this->wpforo->current_user_display_name = '';
1101 $this->wpforo->current_user_status = '';
1102 }
1103 }
1104
1105 public function blog_posts( $userid ){
1106 if( isset($userid) && $userid ) return count_user_posts( $userid , 'post' );
1107 }
1108
1109 public function blog_comments($userid, $user_email){
1110 global $wpdb;
1111 if( !$userid || !$user_email ) return 0;
1112 return (int) $wpdb->get_var("SELECT COUNT(*) FROM " . $wpdb->comments. " WHERE `user_id` = " . intval($userid) . " OR `comment_author_email` = '" . esc_sql($user_email) . "'");
1113 }
1114
1115 public function show_delete_form($current_user, $userids){
1116 if( empty($current_user) || empty($userids) ) return;
1117
1118 $userids = array_diff( $userids, array( $current_user->ID ) );
1119 $users_have_content = false;
1120 if ( $this->wpforo->db->get_var( "SELECT postid FROM {$this->wpforo->db->prefix}wpforo_posts WHERE userid IN( " . implode( ',', array_map('intval', $userids) ) . " ) LIMIT 1" ) ) {
1121 $users_have_content = true;
1122 }
1123 ?>
1124 <hr /><strong>#wpForo</strong>
1125 <?php if ( ! $users_have_content ) : ?>
1126 <input type="hidden" name="wpforo_user_delete_option" value="delete" />
1127 <?php else: ?>
1128 <?php if ( 1 == count($userids) ) : ?>
1129 <fieldset><p><legend><?php _e( 'What should be done with wpForo content owned by this user?', 'wpforo' ); ?></legend></p>
1130 <?php else : ?>
1131 <fieldset><p><legend><?php _e( 'What should be done with wpForo content owned by these users?', 'wpforo' ); ?></legend></p>
1132 <?php endif; ?>
1133 <ul style="list-style:none;">
1134 <li><label><input type="radio" id="wpforo_delete_option0" name="wpforo_user_delete_option" value="delete" />
1135 <?php _e('Delete all wpForo content.', 'wpforo'); ?></label></li>
1136 <li><input type="radio" id="wpforo_delete_option1" name="wpforo_user_delete_option" value="reassign" />
1137 <?php echo '<label for="wpforo_delete_option1">' . __( 'Attribute all content to:' ) . '</label> ';
1138 wp_dropdown_users( array(
1139 'name' => 'wpforo_reassign_user',
1140 'exclude' => $userids,
1141 'show' => 'display_name_with_login',
1142 ) ); ?></li>
1143 </ul></fieldset>
1144 <script type="text/javascript">
1145 jQuery(document).ready( function($) {
1146 $('#wpforo_reassign_user').focus( function() {
1147 $('#wpforo_delete_option1').prop('checked', true).trigger('change');
1148 });
1149 });
1150 </script>
1151 <?php endif;
1152 }
1153
1154
1155
1156 public function autoban($userid){
1157 if( !$this->wpforo->perm->usergroup_can( 'em' ) ){
1158 $this->wpforo->db->update(
1159 $this->wpforo->db->prefix.'wpforo_profiles',
1160 array('status' => 'banned'),
1161 array('userid' => intval( $userid )),
1162 array('%s'),
1163 array('%d')
1164 );
1165 }
1166 }
1167
1168 public function member_approved_posts( $member = array() ){
1169 if(is_numeric($member)){
1170 if( isset($this->wpforo->current_user['posts']) && $this->wpforo->current_user['posts'] && $member == $this->wpforo->current_userid ){
1171 return $this->wpforo->current_user['posts'];
1172 }
1173 else{
1174 return $this->wpforo->db->get_var( "SELECT COUNT(*) as posts FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `status` = 0 AND `userid` = " . intval($member) );
1175 }
1176 }
1177 elseif(is_array($member) && !empty($member)){
1178 return intval($member['posts']);
1179 }
1180 else{
1181 return 0;
1182 }
1183 }
1184
1185 public function current_user_is_new(){
1186 if( $this->wpforo->perm->usergroup_can( 'em' ) ){
1187 //This is an admin or moderator. The number of posts doesn't matter.
1188 return false;
1189 }
1190 else{
1191 $posts = $this->member_approved_posts( $this->wpforo->current_userid );
1192 if ( $posts < $this->wpforo->tools_antispam['new_user_max_posts'] ) {
1193 return true;
1194 }
1195 }
1196 }
1197
1198 public function banned_count(){
1199 $count = $this->wpforo->db->get_var("SELECT count(*) FROM `".$this->wpforo->db->prefix."wpforo_profiles` WHERE `status` = 'banned' " );
1200 return $count;
1201 }
1202
1203 public function get_guest( $args = array() ){
1204
1205 $cache = $this->wpforo->cache->on('memory_cashe');
1206
1207 if( !isset($args['name']) || $args['name'] == '' ) $args['name'] = wpforo_phrase('Anonymous', false);
1208 if( !isset($args['email']) || $args['email'] == '' ) $args['email'] = 'anonymous@example.com';
1209
1210 $args['name'] = strip_tags($args['name']);
1211 $args['email'] = strip_tags($args['email']);
1212 $args['posts'] = 0;
1213 $args['user_registered'] = 0;
1214
1215 if(isset($args['email'])){
1216 if( $cache && isset(self::$cache['guest'][$args['email']]) ){
1217 return self::$cache['guest'][$args['email']];
1218 }
1219 }
1220
1221 if( $args['email'] ){
1222 $post_args = array( 'email' => $args['email'], 'orderby' => 'created', 'order' => 'ASC' );
1223 $posts = $this->wpforo->post->get_posts( $post_args );
1224 if( !empty($posts) ){
1225 $args['posts'] = count($posts);
1226 if( isset($posts[0]['created']) || $posts[0]['created'] ) $args['user_registered'] = $posts[0]['created'];
1227 }
1228 }
1229
1230 $member = array( 'ID' => 0,
1231 'userid' => 0,
1232 'user_login' => $args['name'],
1233 'user_pass' => '',
1234 'user_nicename' => sanitize_text_field($args['name']),
1235 'user_email' => $args['email'],
1236 'user_url' => '',
1237 'user_registered' => $args['user_registered'],
1238 'user_activation_key' => '',
1239 'user_status' => 0,
1240 'display_name' => $args['name'],
1241 'title' => '',
1242 'username' => $args['name'],
1243 'groupid' => 4,
1244 'posts' => $args['posts'], 'questions' => 0, 'answers' => 0, 'comments' => 0, 'site' => '', 'icq' => '', 'aim' => '', 'yahoo' => '', 'msn' => '', 'facebook' => '', 'twitter' => '', 'gtalk' => '', 'skype' => '', 'avatar' => '', 'signature' => '', 'about' => '', 'occupation' => '', 'location' => '', 'last_login' => '', 'rank' => 0, 'like' => 0,
1245 'status' => 'active',
1246 'timezone' => '',
1247 'name' => $args['name'],
1248 'cans' => '',
1249 'description' => '',
1250 'groupname' => wpforo_phrase('Guest', false),
1251 'profile_url' => '',
1252 'stat' => array( 'points' => 0, 'rating' => 0, 'rating_procent' => 0, 'color' => '', 'badge' => '', 'posts' => $args['posts'], 'topics' => 0, 'questions' => 0, 'answers' => 0, 'question_comments' => 0, 'likes' => 0, 'liked' => 0, 'title' => '' ),
1253 );
1254
1255 if( $cache && $args['email'] ){
1256 return self::$cache['guest'][$args['email']] = $member;
1257 }else{
1258 return $member;
1259 }
1260
1261 }
1262
1263
1264
1265 }
1266
1267 ?>