PluginProbe
wpForo Forum / 1.4.12
wpForo Forum v1.4.12
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.12, at wpf-includes/class-permissions.php

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