PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/wp-includes2/capabilities.php

https://github.com/itspriddle/itt-capstone
PHP | 421 lines | 307 code | 81 blank | 33 comment | 58 complexity | 71047f88f3f5a9919acbd01da9a8834b MD5 | raw file
  1. <?php
  2. class WP_Roles {
  3. var $roles;
  4. var $role_objects = array();
  5. var $role_names = array();
  6. var $role_key;
  7. function WP_Roles() {
  8. global $table_prefix;
  9. $this->role_key = $table_prefix . 'user_roles';
  10. $this->roles = get_option($this->role_key);
  11. if ( empty($this->roles) )
  12. return;
  13. foreach ($this->roles as $role => $data) {
  14. $this->role_objects[$role] = new WP_Role($role, $this->roles[$role]['capabilities']);
  15. $this->role_names[$role] = $this->roles[$role]['name'];
  16. }
  17. }
  18. function add_role($role, $display_name, $capabilities = '') {
  19. if ( isset($this->roles[$role]) )
  20. return;
  21. $this->roles[$role] = array(
  22. 'name' => $display_name,
  23. 'capabilities' => $capabilities);
  24. update_option($this->role_key, $this->roles);
  25. $this->role_objects[$role] = new WP_Role($role, $capabilities);
  26. $this->role_names[$role] = $display_name;
  27. return $this->role_objects[$role];
  28. }
  29. function remove_role($role) {
  30. if ( ! isset($this->role_objects[$role]) )
  31. return;
  32. unset($this->role_objects[$role]);
  33. unset($this->role_names[$role]);
  34. unset($this->roles[$role]);
  35. update_option($this->role_key, $this->roles);
  36. }
  37. function add_cap($role, $cap, $grant = true) {
  38. $this->roles[$role]['capabilities'][$cap] = $grant;
  39. update_option($this->role_key, $this->roles);
  40. }
  41. function remove_cap($role, $cap) {
  42. unset($this->roles[$role]['capabilities'][$cap]);
  43. update_option($this->role_key, $this->roles);
  44. }
  45. function &get_role($role) {
  46. if ( isset($this->role_objects[$role]) )
  47. return $this->role_objects[$role];
  48. else
  49. return null;
  50. }
  51. function get_names() {
  52. return $this->role_names;
  53. }
  54. function is_role($role)
  55. {
  56. return isset($this->role_names[$role]);
  57. }
  58. }
  59. class WP_Role {
  60. var $name;
  61. var $capabilities;
  62. function WP_Role($role, $capabilities) {
  63. $this->name = $role;
  64. $this->capabilities = $capabilities;
  65. }
  66. function add_cap($cap, $grant = true) {
  67. global $wp_roles;
  68. if ( ! isset($wp_roles) )
  69. $wp_roles = new WP_Roles();
  70. $this->capabilities[$cap] = $grant;
  71. $wp_roles->add_cap($this->name, $cap, $grant);
  72. }
  73. function remove_cap($cap) {
  74. global $wp_roles;
  75. if ( ! isset($wp_roles) )
  76. $wp_roles = new WP_Roles();
  77. unset($this->capabilities[$cap]);
  78. $wp_roles->remove_cap($this->name, $cap);
  79. }
  80. function has_cap($cap) {
  81. $capabilities = apply_filters('role_has_cap', $this->capabilities, $cap, $this->name);
  82. if ( !empty($capabilities[$cap]) )
  83. return $capabilities[$cap];
  84. else
  85. return false;
  86. }
  87. }
  88. class WP_User {
  89. var $data;
  90. var $id = 0;
  91. var $caps = array();
  92. var $cap_key;
  93. var $roles = array();
  94. var $allcaps = array();
  95. function WP_User($id, $name = '') {
  96. global $table_prefix;
  97. if ( empty($id) && empty($name) )
  98. return;
  99. if ( ! is_numeric($id) ) {
  100. $name = $id;
  101. $id = 0;
  102. }
  103. if ( ! empty($id) )
  104. $this->data = get_userdata($id);
  105. else
  106. $this->data = get_userdatabylogin($name);
  107. if ( empty($this->data->ID) )
  108. return;
  109. foreach (get_object_vars($this->data) as $key => $value) {
  110. $this->{$key} = $value;
  111. }
  112. $this->id = $this->ID;
  113. $this->cap_key = $table_prefix . 'capabilities';
  114. $this->caps = &$this->{$this->cap_key};
  115. if ( ! is_array($this->caps) )
  116. $this->caps = array();
  117. $this->get_role_caps();
  118. }
  119. function get_role_caps() {
  120. global $wp_roles;
  121. if ( ! isset($wp_roles) )
  122. $wp_roles = new WP_Roles();
  123. //Filter out caps that are not role names and assign to $this->roles
  124. if(is_array($this->caps))
  125. $this->roles = array_filter(array_keys($this->caps), array(&$wp_roles, 'is_role'));
  126. //Build $allcaps from role caps, overlay user's $caps
  127. $this->allcaps = array();
  128. foreach($this->roles as $role) {
  129. $role = $wp_roles->get_role($role);
  130. $this->allcaps = array_merge($this->allcaps, $role->capabilities);
  131. }
  132. $this->allcaps = array_merge($this->allcaps, $this->caps);
  133. }
  134. function add_role($role) {
  135. $this->caps[$role] = true;
  136. update_usermeta($this->id, $this->cap_key, $this->caps);
  137. $this->get_role_caps();
  138. $this->update_user_level_from_caps();
  139. }
  140. function remove_role($role) {
  141. if ( empty($this->roles[$role]) || (count($this->roles) <= 1) )
  142. return;
  143. unset($this->caps[$role]);
  144. update_usermeta($this->id, $this->cap_key, $this->caps);
  145. $this->get_role_caps();
  146. }
  147. function set_role($role) {
  148. foreach($this->roles as $oldrole)
  149. unset($this->caps[$oldrole]);
  150. $this->caps[$role] = true;
  151. $this->roles = array($role => true);
  152. update_usermeta($this->id, $this->cap_key, $this->caps);
  153. $this->get_role_caps();
  154. $this->update_user_level_from_caps();
  155. }
  156. function level_reduction($max, $item) {
  157. if(preg_match('/^level_(10|[0-9])$/i', $item, $matches)) {
  158. $level = intval($matches[1]);
  159. return max($max, $level);
  160. } else {
  161. return $max;
  162. }
  163. }
  164. function update_user_level_from_caps() {
  165. global $table_prefix;
  166. $this->user_level = array_reduce(array_keys($this->allcaps), array(&$this, 'level_reduction'), 0);
  167. update_usermeta($this->id, $table_prefix.'user_level', $this->user_level);
  168. }
  169. function add_cap($cap, $grant = true) {
  170. $this->caps[$cap] = $grant;
  171. update_usermeta($this->id, $this->cap_key, $this->caps);
  172. }
  173. function remove_cap($cap) {
  174. if ( empty($this->caps[$cap]) ) return;
  175. unset($this->caps[$cap]);
  176. update_usermeta($this->id, $this->cap_key, $this->caps);
  177. }
  178. //has_cap(capability_or_role_name) or
  179. //has_cap('edit_post', post_id)
  180. function has_cap($cap) {
  181. if ( is_numeric($cap) )
  182. $cap = $this->translate_level_to_cap($cap);
  183. $args = array_slice(func_get_args(), 1);
  184. $args = array_merge(array($cap, $this->id), $args);
  185. $caps = call_user_func_array('map_meta_cap', $args);
  186. // Must have ALL requested caps
  187. $capabilities = apply_filters('user_has_cap', $this->allcaps, $caps, $args);
  188. foreach ($caps as $cap) {
  189. //echo "Checking cap $cap<br/>";
  190. if(empty($capabilities[$cap]) || !$capabilities[$cap])
  191. return false;
  192. }
  193. return true;
  194. }
  195. function translate_level_to_cap($level) {
  196. return 'level_' . $level;
  197. }
  198. }
  199. // Map meta capabilities to primitive capabilities.
  200. function map_meta_cap($cap, $user_id) {
  201. $args = array_slice(func_get_args(), 2);
  202. $caps = array();
  203. switch ($cap) {
  204. // edit_post breaks down to edit_posts, edit_published_posts, or
  205. // edit_others_posts
  206. case 'edit_post':
  207. $author_data = get_userdata($user_id);
  208. //echo "post ID: {$args[0]}<br/>";
  209. $post = get_post($args[0]);
  210. $post_author_data = get_userdata($post->post_author);
  211. //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br/>";
  212. // If the user is the author...
  213. if ($user_id == $post_author_data->ID) {
  214. // If the post is published...
  215. if ($post->post_status == 'publish')
  216. $caps[] = 'edit_published_posts';
  217. else if ($post->post_status == 'static')
  218. $caps[] = 'edit_pages';
  219. else
  220. // If the post is draft...
  221. $caps[] = 'edit_posts';
  222. } else {
  223. if ($post->post_status == 'static') {
  224. $caps[] = 'edit_pages';
  225. break;
  226. }
  227. // The user is trying to edit someone else's post.
  228. $caps[] = 'edit_others_posts';
  229. // The post is published, extra cap required.
  230. if ($post->post_status == 'publish')
  231. $caps[] = 'edit_published_posts';
  232. }
  233. break;
  234. case 'read_post':
  235. $post = get_post($args[0]);
  236. if ( 'private' != $post->post_status ) {
  237. $caps[] = 'read';
  238. break;
  239. }
  240. $author_data = get_userdata($user_id);
  241. $post_author_data = get_userdata($post->post_author);
  242. if ($user_id == $post_author_data->ID)
  243. $caps[] = 'read';
  244. else
  245. $caps[] = 'read_private_posts';
  246. break;
  247. default:
  248. // If no meta caps match, return the original cap.
  249. $caps[] = $cap;
  250. }
  251. return $caps;
  252. }
  253. // Capability checking wrapper around the global $current_user object.
  254. function current_user_can($capability) {
  255. $current_user = wp_get_current_user();
  256. $args = array_slice(func_get_args(), 1);
  257. $args = array_merge(array($capability), $args);
  258. if ( empty($current_user) )
  259. return false;
  260. return call_user_func_array(array(&$current_user, 'has_cap'), $args);
  261. }
  262. // Convenience wrappers around $wp_roles.
  263. function get_role($role) {
  264. global $wp_roles;
  265. if ( ! isset($wp_roles) )
  266. $wp_roles = new WP_Roles();
  267. return $wp_roles->get_role($role);
  268. }
  269. function add_role($role, $display_name, $capabilities = '') {
  270. global $wp_roles;
  271. if ( ! isset($wp_roles) )
  272. $wp_roles = new WP_Roles();
  273. return $wp_roles->add_role($role, $display_name, $capabilities);
  274. }
  275. function remove_role($role) {
  276. global $wp_roles;
  277. if ( ! isset($wp_roles) )
  278. $wp_roles = new WP_Roles();
  279. return $wp_roles->remove_role($role);
  280. }
  281. //
  282. // These are deprecated. Use current_user_can().
  283. //
  284. /* returns true if $user_id can create a new post */
  285. function user_can_create_post($user_id, $blog_id = 1, $category_id = 'None') {
  286. $author_data = get_userdata($user_id);
  287. return ($author_data->user_level > 1);
  288. }
  289. /* returns true if $user_id can create a new post */
  290. function user_can_create_draft($user_id, $blog_id = 1, $category_id = 'None') {
  291. $author_data = get_userdata($user_id);
  292. return ($author_data->user_level >= 1);
  293. }
  294. /* returns true if $user_id can edit $post_id */
  295. function user_can_edit_post($user_id, $post_id, $blog_id = 1) {
  296. $author_data = get_userdata($user_id);
  297. $post = get_post($post_id);
  298. $post_author_data = get_userdata($post->post_author);
  299. if ( (($user_id == $post_author_data->ID) && !($post->post_status == 'publish' && $author_data->user_level < 2))
  300. || ($author_data->user_level > $post_author_data->user_level)
  301. || ($author_data->user_level >= 10) ) {
  302. return true;
  303. } else {
  304. return false;
  305. }
  306. }
  307. /* returns true if $user_id can delete $post_id */
  308. function user_can_delete_post($user_id, $post_id, $blog_id = 1) {
  309. // right now if one can edit, one can delete
  310. return user_can_edit_post($user_id, $post_id, $blog_id);
  311. }
  312. /* returns true if $user_id can set new posts' dates on $blog_id */
  313. function user_can_set_post_date($user_id, $blog_id = 1, $category_id = 'None') {
  314. $author_data = get_userdata($user_id);
  315. return (($author_data->user_level > 4) && user_can_create_post($user_id, $blog_id, $category_id));
  316. }
  317. /* returns true if $user_id can edit $post_id's date */
  318. function user_can_edit_post_date($user_id, $post_id, $blog_id = 1) {
  319. $author_data = get_userdata($user_id);
  320. return (($author_data->user_level > 4) && user_can_edit_post($user_id, $post_id, $blog_id));
  321. }
  322. /* returns true if $user_id can edit $post_id's comments */
  323. function user_can_edit_post_comments($user_id, $post_id, $blog_id = 1) {
  324. // right now if one can edit a post, one can edit comments made on it
  325. return user_can_edit_post($user_id, $post_id, $blog_id);
  326. }
  327. /* returns true if $user_id can delete $post_id's comments */
  328. function user_can_delete_post_comments($user_id, $post_id, $blog_id = 1) {
  329. // right now if one can edit comments, one can delete comments
  330. return user_can_edit_post_comments($user_id, $post_id, $blog_id);
  331. }
  332. function user_can_edit_user($user_id, $other_user) {
  333. $user = get_userdata($user_id);
  334. $other = get_userdata($other_user);
  335. if ( $user->user_level > $other->user_level || $user->user_level > 8 || $user->ID == $other->ID )
  336. return true;
  337. else
  338. return false;
  339. }
  340. ?>