PluginProbe
wpForo Forum / 1.0.1
wpForo Forum v1.0.1
3.1.6 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 All 138 releases
wpforo / wpf-includes / class-permissions.php

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

250 lines 7.0 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 static $cache = array();
10
11 function __construct( $wpForo ){
12 if(!isset($this->wpforo)) $this->wpforo = $wpForo;
13 }
14
15 /**
16 *
17 * @param string $access
18 *
19 * @return array access row by access key
20 */
21 function get_access($access){
22 $access = sanitize_text_field($access);
23 $sql = "SELECT * FROM `".$this->wpforo->db->prefix."wpforo_accesses` WHERE `access` = '" . esc_sql($access) . "'";
24 return $this->wpforo->db->get_row($sql, ARRAY_A);
25 }
26
27
28 /**
29 * get all accesses from accesses table
30 *
31 * @return assoc array with accesses
32 */
33 function get_accesses(){
34 $sql = "SELECT * FROM ".$this->wpforo->db->prefix."wpforo_accesses";
35 return $this->wpforo->db->get_results($sql, ARRAY_A);
36 }
37
38 function usergroup_cans_form( $groupid = FALSE ){
39
40 $can_data = array();
41 $cans = $this->wpforo->usergroup_cans;
42
43 if( $groupid == FALSE ){
44 foreach($cans as $can => $name){
45 $can_data[$can]['value'] = 0;
46 $can_data[$can]['name'] = $name;
47 }
48 }else{
49 $usegroup = $this->wpforo->usergroup->get_usergroup( $groupid );
50 $ug_cans = unserialize($usegroup['cans']);
51 foreach($cans as $can => $name){
52 $can_data[$can]['value'] = $ug_cans[$can];
53 $can_data[$can]['name'] = $name;
54 }
55 }
56
57 return $can_data;
58 }
59
60 function forum_cans_form( $access = FALSE ){
61
62 $can_data = array();
63 $cans = $this->wpforo->forum_cans;
64
65 if( !$access ){
66 foreach($cans as $can => $name){
67 $can_data[$can]['value'] = 0;
68 $can_data[$can]['name'] = $name;
69 }
70 }else{
71 $access = $this->get_access( $access );
72 $access_cans = unserialize($access['cans']);
73 foreach($cans as $can => $name){
74 $can_data[$can]['value'] = $access_cans[$can];
75 $can_data[$can]['name'] = $name;
76 }
77 }
78
79 return $can_data;
80 }
81
82
83 /**
84 *
85 * @param string (required)
86 * @param array
87 * @param int
88 *
89 * @return affected rows count or false
90 */
91 function add( $title, $cans = array(), $key = '' ){
92 $default = array_map('intval', $this->wpforo->forum_cans);
93 $cans = wpforo_parse_args($cans, $default);
94 if(!$key) $key = $title;
95
96 $i = 2;
97 while( $this->wpforo->db->get_var("SELECT `access` FROM ".$this->wpforo->db->prefix."wpforo_accesses WHERE `access` = '". esc_sql(sanitize_text_field($key)) . "'") ){
98 $key = $key . '-' . $i;
99 $i++;
100 }
101
102 if( $this->wpforo->db->insert(
103 $this->wpforo->db->prefix . 'wpforo_accesses',
104 array(
105 'title' => sanitize_text_field($title),
106 'access' => sanitize_text_field($key),
107 'cans' => serialize($cans)
108 ),
109 array(
110 '%s',
111 '%s',
112 '%s'
113 )
114 )
115 ){
116 $this->wpforo->notice->add( sprintf( __('%s access successfully added', 'wpforo') , esc_html($title)) , 'success');
117 return $this->wpforo->db->insert_id;
118 }
119
120 $this->wpforo->notice->add('Access add error', 'error');
121 return FALSE;
122 }
123
124 function edit( $title, $cans, $key ){
125 $default = array_map('intval', $this->wpforo->forum_cans);
126 $cans = wpforo_parse_args($cans, $default);
127
128 if( FALSE !== $this->wpforo->db->update(
129 $this->wpforo->db->prefix . 'wpforo_accesses',
130 array(
131 'title' => sanitize_text_field($title),
132 'cans' => serialize( $cans ),
133 ),
134 array( 'access' => sanitize_text_field($key) ),
135 array(
136 '%s',
137 '%s'
138 ),
139 array( '%s' ))
140 ){
141 $this->wpforo->notice->add( sprintf( __('%s access successfully edited', 'wpforo'), esc_html($title)) , 'success');
142 return $key;
143 }
144
145 $this->wpforo->notice->add('Access edit error', 'error');
146 return FALSE;
147 }
148
149 function delete($accessid){
150
151 $accessid = intval($accessid);
152
153 if(!$accessid){
154 $this->wpforo->notice->add('Access delete error', 'error');
155 return FALSE;
156 }
157
158 if( FALSE !== $this->wpforo->db->delete( $this->wpforo->db->prefix.'wpforo_accesses', array( 'accessid' => $accessid ), array( '%d' ) ) ){
159 $this->wpforo->notice->add('Access successfully deleted', 'success');
160 return $accessid;
161 }
162
163 $this->wpforo->notice->add('Access delete error', 'error');
164 return FALSE;
165 }
166
167 function forum_can( $forumid, $do ){
168 $can = 0;
169 $forumid = intval($forumid);
170 if( !$this->wpforo->current_user_groupid ) return 0;
171 $forum = $this->wpforo->forum->get_forum($forumid, true);
172 $permissions = unserialize($forum['permissions']);
173 if( isset($permissions[$this->wpforo->current_user_groupid]) ){
174 $access = $permissions[$this->wpforo->current_user_groupid];
175 $access_arr = $this->get_access($access);
176 $cans = unserialize($access_arr['cans']);
177 $can = ( isset($cans[$do]) ? $cans[$do] : 0 );
178 }
179 return $can;
180 }
181
182 function usergroup_can( $usergroupid, $do ){
183 $usergroupid = intval($usergroupid);
184 $usergroup = $this->wpforo->usergroup->get_usergroup( $usergroupid );
185 $cans = unserialize($usergroup['cans']);
186 return ( isset($cans[$do]) ? $cans[$do] : 0 );
187 }
188
189 function user_can_manage_user( $user_id, $managing_user_id ){
190
191 if( !$user_id || !$managing_user_id ) return false;
192 if( $user_id == $managing_user_id ) return true;
193
194 $user = new WP_User( $user_id );
195 $user_level = $this->user_wp_level( $user );
196 if( !empty($user->roles) && is_array($user->roles) ) $user_role = array_shift($user->roles);
197
198 $managing_user = new WP_User( $managing_user_id );
199 $managing_user_level = $this->user_wp_level( $managing_user );
200 if( !empty($managing_user->roles) && is_array($managing_user->roles) ) $managing_user_role = array_shift($managing_user->roles);
201
202 if( (int)$user_level > (int)$managing_user_level ){
203 return true;
204 }
205 elseif( $user_id == 1 && $user_role == 'administrator' ){
206 return true;
207 }
208 elseif( (int)$user_level == (int)$managing_user_level ){
209 $member = $this->wpforo->member->get_member( $user_id );
210 $managing_member = $this->wpforo->member->get_member( $managing_user_id );
211 $user_wpforo_can = $this->usergroup_can( $member['groupid'], 'em' );
212 $managing_user_wpforo_can = $this->usergroup_can( $managing_member['groupid'], 'em' );
213 if( $user_wpforo_can && !$managing_user_wpforo_can ){
214 return true;
215 }
216 else{
217 return false;
218 }
219 }
220 elseif( $user_id != 1 && $managing_user_id == 1 && $managing_user_role == 'administrator' ){
221 return false;
222 }
223 else{
224 return false;
225 }
226 }
227
228 function user_wp_level( $user_object ){
229 $level = 0;
230 $levels = array();
231 if( is_int($user_object) ){
232 $user_object = new WP_User( $user_object );
233 }
234 if( isset($user_object->allcaps) && is_array($user_object->allcaps) && !empty($user_object->allcaps) ){
235 foreach($user_object->allcaps as $level_key => $level_value){
236 if( strpos($level_key, 'level_') !== FALSE && $level_value == 1 ){
237 $levels[] = intval(str_replace('level_', '', $level_key));
238 }
239 }
240 if(!empty($levels)){
241 $level = max($levels);
242 }
243 }
244 return $level;
245 }
246
247
248 }
249
250 ?>