PluginProbe
wpForo Forum / 1.4.0
wpForo Forum v1.4.0
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-permissions.php

class-permissions.php in wpForo Forum 1.4.0, at wpf-includes/class-permissions.php

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