| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if( !defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
|
| 6 |
class wpForoPermissions{ |
| 7 |
|
| 8 |
private $wpforo; |
| 9 |
static $cache = array(); |
| 10 |
|
| 11 |
function __construct( $wpForo ){ |
| 12 |
if(!isset($this->wpforo)) $this->wpforo = $wpForo; |
| 13 |
if( isset( $this->wpforo->general_options['lang'] ) && $this->wpforo->general_options['lang'] ){ |
| 14 |
$accesses = $this->get_accesses(); |
| 15 |
if(!empty($accesses)){ |
| 16 |
foreach( $accesses as $access ){ |
| 17 |
$this->wpforo->access[$access['access']] = $access; |
| 18 |
} |
| 19 |
} |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* |
| 25 |
* @param string $access |
| 26 |
* |
| 27 |
* @return array access row by access key |
| 28 |
*/ |
| 29 |
function get_access($access){ |
| 30 |
$access = sanitize_text_field($access); |
| 31 |
if( isset($this->wpforo->access[$access]) && !empty($this->wpforo->access[$access]) ){ |
| 32 |
return $this->wpforo->access[$access]; |
| 33 |
} |
| 34 |
else{ |
| 35 |
$sql = "SELECT * FROM `".$this->wpforo->db->prefix."wpforo_accesses` WHERE `access` = '" . esc_sql($access) . "'"; |
| 36 |
return $this->wpforo->db->get_row($sql, ARRAY_A); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* get all accesses from accesses table |
| 43 |
* |
| 44 |
* @return assoc array with accesses |
| 45 |
*/ |
| 46 |
function get_accesses(){ |
| 47 |
$sql = "SELECT * FROM ".$this->wpforo->db->prefix."wpforo_accesses"; |
| 48 |
return $this->wpforo->db->get_results($sql, ARRAY_A); |
| 49 |
} |
| 50 |
|
| 51 |
function usergroup_cans_form( $groupid = FALSE ){ |
| 52 |
|
| 53 |
$can_data = array(); |
| 54 |
$cans = $this->wpforo->usergroup_cans; |
| 55 |
|
| 56 |
if( $groupid == FALSE ){ |
| 57 |
foreach($cans as $can => $name){ |
| 58 |
@$can_data[$can]['value'] = 0; |
| 59 |
@$can_data[$can]['name'] = $name; |
| 60 |
} |
| 61 |
}else{ |
| 62 |
$usegroup = $this->wpforo->usergroup->get_usergroup( $groupid ); |
| 63 |
$ug_cans = unserialize($usegroup['cans']); |
| 64 |
foreach($cans as $can => $name){ |
| 65 |
@$can_data[$can]['value'] = $ug_cans[$can]; |
| 66 |
@$can_data[$can]['name'] = $name; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return $can_data; |
| 71 |
} |
| 72 |
|
| 73 |
function forum_cans_form( $access = FALSE ){ |
| 74 |
|
| 75 |
$can_data = array(); |
| 76 |
$cans = $this->wpforo->forum_cans; |
| 77 |
|
| 78 |
if( !$access ){ |
| 79 |
foreach($cans as $can => $name){ |
| 80 |
@$can_data[$can]['value'] = 0; |
| 81 |
@$can_data[$can]['name'] = $name; |
| 82 |
} |
| 83 |
}else{ |
| 84 |
$access = $this->get_access( $access ); |
| 85 |
$access_cans = unserialize($access['cans']); |
| 86 |
foreach($cans as $can => $name){ |
| 87 |
@$can_data[$can]['value'] = $access_cans[$can]; |
| 88 |
@$can_data[$can]['name'] = $name; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return $can_data; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
/** |
| 97 |
* |
| 98 |
* @param string (required) |
| 99 |
* @param array |
| 100 |
* @param int |
| 101 |
* |
| 102 |
* @return affected rows count or false |
| 103 |
*/ |
| 104 |
function add( $title, $cans = array(), $key = '' ){ |
| 105 |
$default = array_map('intval', $this->wpforo->forum_cans); |
| 106 |
$cans = wpforo_parse_args($cans, $default); |
| 107 |
if(!$key) $key = $title; |
| 108 |
|
| 109 |
$i = 2; |
| 110 |
while( $this->wpforo->db->get_var("SELECT `access` FROM ".$this->wpforo->db->prefix."wpforo_accesses WHERE `access` = '". esc_sql(sanitize_text_field($key)) . "'") ){ |
| 111 |
$key = $key . '-' . $i; |
| 112 |
$i++; |
| 113 |
} |
| 114 |
|
| 115 |
if( $this->wpforo->db->insert( |
| 116 |
$this->wpforo->db->prefix . 'wpforo_accesses', |
| 117 |
array( |
| 118 |
'title' => sanitize_text_field($title), |
| 119 |
'access' => sanitize_text_field($key), |
| 120 |
'cans' => serialize($cans) |
| 121 |
), |
| 122 |
array( |
| 123 |
'%s', |
| 124 |
'%s', |
| 125 |
'%s' |
| 126 |
) |
| 127 |
) |
| 128 |
){ |
| 129 |
$this->wpforo->notice->add( sprintf( __('%s access successfully added', 'wpforo') , esc_html($title)) , 'success'); |
| 130 |
return $this->wpforo->db->insert_id; |
| 131 |
} |
| 132 |
|
| 133 |
$this->wpforo->notice->add('Access add error', 'error'); |
| 134 |
return FALSE; |
| 135 |
} |
| 136 |
|
| 137 |
function edit( $title, $cans, $key ){ |
| 138 |
$default = array_map('intval', $this->wpforo->forum_cans); |
| 139 |
$cans = wpforo_parse_args($cans, $default); |
| 140 |
|
| 141 |
if( FALSE !== $this->wpforo->db->update( |
| 142 |
$this->wpforo->db->prefix . 'wpforo_accesses', |
| 143 |
array( |
| 144 |
'title' => sanitize_text_field($title), |
| 145 |
'cans' => serialize( $cans ), |
| 146 |
), |
| 147 |
array( 'access' => sanitize_text_field($key) ), |
| 148 |
array( |
| 149 |
'%s', |
| 150 |
'%s' |
| 151 |
), |
| 152 |
array( '%s' )) |
| 153 |
){ |
| 154 |
$this->wpforo->notice->add( sprintf( __('%s access successfully edited', 'wpforo'), esc_html($title)) , 'success'); |
| 155 |
return $key; |
| 156 |
} |
| 157 |
|
| 158 |
$this->wpforo->notice->add('Access edit error', 'error'); |
| 159 |
return FALSE; |
| 160 |
} |
| 161 |
|
| 162 |
function delete($accessid){ |
| 163 |
|
| 164 |
$accessid = intval($accessid); |
| 165 |
|
| 166 |
if(!$accessid){ |
| 167 |
$this->wpforo->notice->add('Access delete error', 'error'); |
| 168 |
return FALSE; |
| 169 |
} |
| 170 |
|
| 171 |
if( FALSE !== $this->wpforo->db->delete( $this->wpforo->db->prefix.'wpforo_accesses', array( 'accessid' => $accessid ), array( '%d' ) ) ){ |
| 172 |
$this->wpforo->notice->add('Access successfully deleted', 'success'); |
| 173 |
return $accessid; |
| 174 |
} |
| 175 |
|
| 176 |
$this->wpforo->notice->add('Access delete error', 'error'); |
| 177 |
return FALSE; |
| 178 |
} |
| 179 |
|
| 180 |
function forum_can( $do, $forumid = NULL, $groupid = NULL ){ |
| 181 |
|
| 182 |
$can = 0; |
| 183 |
if( !$this->wpforo->current_user_groupid ) return 0; |
| 184 |
|
| 185 |
if( is_null($forumid) && isset($this->wpforo->current_object['forumid']) ) { |
| 186 |
$forumid = $this->wpforo->current_object['forumid']; |
| 187 |
} |
| 188 |
$forumid = intval($forumid); |
| 189 |
|
| 190 |
if( is_null($groupid) ) { |
| 191 |
$groupid = $this->wpforo->current_user_groupid; |
| 192 |
} |
| 193 |
|
| 194 |
if( $forum = wpforo_forum($forumid) ){ |
| 195 |
$permissions = unserialize($forum['permissions']); |
| 196 |
if( isset($permissions[$groupid]) ){ |
| 197 |
$access = $permissions[$groupid]; |
| 198 |
$access_arr = $this->get_access($access); |
| 199 |
$cans = unserialize($access_arr['cans']); |
| 200 |
$can = ( isset($cans[$do]) ? $cans[$do] : 0 ); |
| 201 |
} |
| 202 |
} |
| 203 |
return $can; |
| 204 |
} |
| 205 |
|
| 206 |
function usergroup_can( $do, $usergroupid = NULL ){ |
| 207 |
if( is_null($usergroupid) ) $usergroupid = $this->wpforo->current_user_groupid; |
| 208 |
$usergroupid = intval($usergroupid); |
| 209 |
$usergroup = $this->wpforo->usergroup->get_usergroup( $usergroupid ); |
| 210 |
$cans = unserialize($usergroup['cans']); |
| 211 |
return ( isset($cans[$do]) ? $cans[$do] : 0 ); |
| 212 |
} |
| 213 |
|
| 214 |
function user_can_manage_user( $user_id, $managing_user_id ){ |
| 215 |
|
| 216 |
if( !$user_id || !$managing_user_id ) return false; |
| 217 |
if( $user_id == $managing_user_id ) return true; |
| 218 |
|
| 219 |
$user = new WP_User( $user_id ); |
| 220 |
$user_level = $this->user_wp_level( $user ); |
| 221 |
if( !empty($user->roles) && is_array($user->roles) ) $user_role = array_shift($user->roles); |
| 222 |
|
| 223 |
$managing_user = new WP_User( $managing_user_id ); |
| 224 |
$managing_user_level = $this->user_wp_level( $managing_user ); |
| 225 |
if( !empty($managing_user->roles) && is_array($managing_user->roles) ) $managing_user_role = array_shift($managing_user->roles); |
| 226 |
|
| 227 |
if( (int)$user_level > (int)$managing_user_level ){ |
| 228 |
return true; |
| 229 |
} |
| 230 |
elseif( $user_id == 1 && $user_role == 'administrator' ){ |
| 231 |
return true; |
| 232 |
} |
| 233 |
elseif( (int)$user_level == (int)$managing_user_level ){ |
| 234 |
$member = $this->wpforo->member->get_member( $user_id ); |
| 235 |
$managing_member = $this->wpforo->member->get_member( $managing_user_id ); |
| 236 |
$user_wpforo_can = $this->usergroup_can( 'em', $member['groupid'] ); |
| 237 |
$managing_user_wpforo_can = $this->usergroup_can( 'em', $managing_member['groupid'] ); |
| 238 |
if( $user_wpforo_can && !$managing_user_wpforo_can ){ |
| 239 |
return true; |
| 240 |
} |
| 241 |
else{ |
| 242 |
return false; |
| 243 |
} |
| 244 |
} |
| 245 |
elseif( $user_id != 1 && $managing_user_id == 1 && $managing_user_role == 'administrator' ){ |
| 246 |
return false; |
| 247 |
} |
| 248 |
else{ |
| 249 |
return false; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
function user_wp_level( $user_object ){ |
| 254 |
$level = 0; |
| 255 |
$levels = array(); |
| 256 |
if( is_int($user_object) ){ |
| 257 |
$user_object = new WP_User( $user_object ); |
| 258 |
} |
| 259 |
if( isset($user_object->allcaps) && is_array($user_object->allcaps) && !empty($user_object->allcaps) ){ |
| 260 |
foreach($user_object->allcaps as $level_key => $level_value){ |
| 261 |
if( strpos($level_key, 'level_') !== FALSE && $level_value == 1 ){ |
| 262 |
$levels[] = intval(str_replace('level_', '', $level_key)); |
| 263 |
} |
| 264 |
} |
| 265 |
if(!empty($levels)){ |
| 266 |
$level = max($levels); |
| 267 |
} |
| 268 |
} |
| 269 |
return $level; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public function can_link(){ |
| 275 |
if( !$this->wpforo->perm->usergroup_can( 'em' ) ){ |
| 276 |
$posts = $this->wpforo->member->member_approved_posts( $this->wpforo->current_userid ); |
| 277 |
$posts = intval($posts); |
| 278 |
if( isset($this->wpforo->tools_antispam['min_number_post_to_link']) ){ |
| 279 |
$min_posts = intval($this->wpforo->tools_antispam['min_number_post_to_link']); |
| 280 |
if( $min_posts != 0 ){ |
| 281 |
if ( $posts <= $min_posts ) { |
| 282 |
return false; |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
return true; |
| 288 |
} |
| 289 |
|
| 290 |
public function can_attach(){ |
| 291 |
if( !$this->wpforo->perm->usergroup_can( 'em' ) ){ |
| 292 |
$posts = $this->wpforo->member->member_approved_posts( $this->wpforo->current_userid ); |
| 293 |
$posts = intval($posts); |
| 294 |
if( isset($this->wpforo->tools_antispam['min_number_post_to_attach']) ){ |
| 295 |
$min_posts = intval($this->wpforo->tools_antispam['min_number_post_to_attach']); |
| 296 |
if( $min_posts != 0 ){ |
| 297 |
if ( $posts <= $min_posts ) { |
| 298 |
return false; |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
return true; |
| 304 |
} |
| 305 |
|
| 306 |
public function can_attach_file_type( $ext = '' ){ |
| 307 |
if( !$this->wpforo->perm->usergroup_can( 'em' ) ){ |
| 308 |
if( isset($this->wpforo->tools_antispam['limited_file_ext']) && $this->wpforo->member->current_user_is_new() ){ |
| 309 |
$expld = explode('|', $this->wpforo->tools_antispam['limited_file_ext'] ); |
| 310 |
if( in_array($ext, $expld) ){ |
| 311 |
return false; |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
return true; |
| 316 |
} |
| 317 |
|
| 318 |
} |
| 319 |
|
| 320 |
?> |