PageRenderTime 34ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/Doduo/WebKitSite/blog/wp-includes/capabilities.php

https://github.com/weissms/owb-mirror
PHP | 485 lines | 372 code | 73 blank | 40 comment | 90 complexity | 8fa79c0b8692cde23c3528bd2999788e 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. var $use_db = true;
  8. function WP_Roles() {
  9. $this->_init();
  10. }
  11. function _init () {
  12. global $wpdb;
  13. global $wp_user_roles;
  14. $this->role_key = $wpdb->prefix . 'user_roles';
  15. if ( ! empty($wp_user_roles) ) {
  16. $this->roles = $wp_user_roles;
  17. $this->use_db = false;
  18. } else {
  19. $this->roles = get_option($this->role_key);
  20. }
  21. if ( empty($this->roles) )
  22. return;
  23. $this->role_objects = array();
  24. $this->role_names = array();
  25. foreach ($this->roles as $role => $data) {
  26. $this->role_objects[$role] = new WP_Role($role, $this->roles[$role]['capabilities']);
  27. $this->role_names[$role] = $this->roles[$role]['name'];
  28. }
  29. }
  30. function add_role($role, $display_name, $capabilities = '') {
  31. if ( isset($this->roles[$role]) )
  32. return;
  33. $this->roles[$role] = array(
  34. 'name' => $display_name,
  35. 'capabilities' => $capabilities);
  36. if ( $this->use_db )
  37. update_option($this->role_key, $this->roles);
  38. $this->role_objects[$role] = new WP_Role($role, $capabilities);
  39. $this->role_names[$role] = $display_name;
  40. return $this->role_objects[$role];
  41. }
  42. function remove_role($role) {
  43. if ( ! isset($this->role_objects[$role]) )
  44. return;
  45. unset($this->role_objects[$role]);
  46. unset($this->role_names[$role]);
  47. unset($this->roles[$role]);
  48. if ( $this->use_db )
  49. update_option($this->role_key, $this->roles);
  50. }
  51. function add_cap($role, $cap, $grant = true) {
  52. $this->roles[$role]['capabilities'][$cap] = $grant;
  53. if ( $this->use_db )
  54. update_option($this->role_key, $this->roles);
  55. }
  56. function remove_cap($role, $cap) {
  57. unset($this->roles[$role]['capabilities'][$cap]);
  58. if ( $this->use_db )
  59. update_option($this->role_key, $this->roles);
  60. }
  61. function &get_role($role) {
  62. if ( isset($this->role_objects[$role]) )
  63. return $this->role_objects[$role];
  64. else
  65. return null;
  66. }
  67. function get_names() {
  68. return $this->role_names;
  69. }
  70. function is_role($role)
  71. {
  72. return isset($this->role_names[$role]);
  73. }
  74. }
  75. class WP_Role {
  76. var $name;
  77. var $capabilities;
  78. function WP_Role($role, $capabilities) {
  79. $this->name = $role;
  80. $this->capabilities = $capabilities;
  81. }
  82. function add_cap($cap, $grant = true) {
  83. global $wp_roles;
  84. if ( ! isset($wp_roles) )
  85. $wp_roles = new WP_Roles();
  86. $this->capabilities[$cap] = $grant;
  87. $wp_roles->add_cap($this->name, $cap, $grant);
  88. }
  89. function remove_cap($cap) {
  90. global $wp_roles;
  91. if ( ! isset($wp_roles) )
  92. $wp_roles = new WP_Roles();
  93. unset($this->capabilities[$cap]);
  94. $wp_roles->remove_cap($this->name, $cap);
  95. }
  96. function has_cap($cap) {
  97. $capabilities = apply_filters('role_has_cap', $this->capabilities, $cap, $this->name);
  98. if ( !empty($capabilities[$cap]) )
  99. return $capabilities[$cap];
  100. else
  101. return false;
  102. }
  103. }
  104. class WP_User {
  105. var $data;
  106. var $ID = 0;
  107. var $id = 0; // Deprecated, use $ID instead.
  108. var $caps = array();
  109. var $cap_key;
  110. var $roles = array();
  111. var $allcaps = array();
  112. function WP_User($id, $name = '') {
  113. global $wpdb;
  114. if ( empty($id) && empty($name) )
  115. return;
  116. if ( ! is_numeric($id) ) {
  117. $name = $id;
  118. $id = 0;
  119. }
  120. if ( ! empty($id) )
  121. $this->data = get_userdata($id);
  122. else
  123. $this->data = get_userdatabylogin($name);
  124. if ( empty($this->data->ID) )
  125. return;
  126. foreach (get_object_vars($this->data) as $key => $value) {
  127. $this->{$key} = $value;
  128. }
  129. $this->id = $this->ID;
  130. $this->_init_caps();
  131. }
  132. function _init_caps() {
  133. global $wpdb;
  134. $this->cap_key = $wpdb->prefix . 'capabilities';
  135. $this->caps = &$this->{$this->cap_key};
  136. if ( ! is_array($this->caps) )
  137. $this->caps = array();
  138. $this->get_role_caps();
  139. }
  140. function get_role_caps() {
  141. global $wp_roles;
  142. if ( ! isset($wp_roles) )
  143. $wp_roles = new WP_Roles();
  144. //Filter out caps that are not role names and assign to $this->roles
  145. if(is_array($this->caps))
  146. $this->roles = array_filter(array_keys($this->caps), array(&$wp_roles, 'is_role'));
  147. //Build $allcaps from role caps, overlay user's $caps
  148. $this->allcaps = array();
  149. foreach( (array) $this->roles as $role) {
  150. $role = $wp_roles->get_role($role);
  151. $this->allcaps = array_merge($this->allcaps, $role->capabilities);
  152. }
  153. $this->allcaps = array_merge($this->allcaps, $this->caps);
  154. }
  155. function add_role($role) {
  156. $this->caps[$role] = true;
  157. update_usermeta($this->ID, $this->cap_key, $this->caps);
  158. $this->get_role_caps();
  159. $this->update_user_level_from_caps();
  160. }
  161. function remove_role($role) {
  162. if ( empty($this->roles[$role]) || (count($this->roles) <= 1) )
  163. return;
  164. unset($this->caps[$role]);
  165. update_usermeta($this->ID, $this->cap_key, $this->caps);
  166. $this->get_role_caps();
  167. }
  168. function set_role($role) {
  169. foreach($this->roles as $oldrole)
  170. unset($this->caps[$oldrole]);
  171. if ( !empty($role) ) {
  172. $this->caps[$role] = true;
  173. $this->roles = array($role => true);
  174. } else {
  175. $this->roles = false;
  176. }
  177. update_usermeta($this->ID, $this->cap_key, $this->caps);
  178. $this->get_role_caps();
  179. $this->update_user_level_from_caps();
  180. }
  181. function level_reduction($max, $item) {
  182. if(preg_match('/^level_(10|[0-9])$/i', $item, $matches)) {
  183. $level = intval($matches[1]);
  184. return max($max, $level);
  185. } else {
  186. return $max;
  187. }
  188. }
  189. function update_user_level_from_caps() {
  190. global $wpdb;
  191. $this->user_level = array_reduce(array_keys($this->allcaps), array(&$this, 'level_reduction'), 0);
  192. update_usermeta($this->ID, $wpdb->prefix.'user_level', $this->user_level);
  193. }
  194. function add_cap($cap, $grant = true) {
  195. $this->caps[$cap] = $grant;
  196. update_usermeta($this->ID, $this->cap_key, $this->caps);
  197. }
  198. function remove_cap($cap) {
  199. if ( empty($this->caps[$cap]) ) return;
  200. unset($this->caps[$cap]);
  201. update_usermeta($this->ID, $this->cap_key, $this->caps);
  202. }
  203. function remove_all_caps() {
  204. global $wpdb;
  205. $this->caps = array();
  206. update_usermeta($this->ID, $this->cap_key, '');
  207. update_usermeta($this->ID, $wpdb->prefix.'user_level', '');
  208. $this->get_role_caps();
  209. }
  210. //has_cap(capability_or_role_name) or
  211. //has_cap('edit_post', post_id)
  212. function has_cap($cap) {
  213. if ( is_numeric($cap) )
  214. $cap = $this->translate_level_to_cap($cap);
  215. $args = array_slice(func_get_args(), 1);
  216. $args = array_merge(array($cap, $this->ID), $args);
  217. $caps = call_user_func_array('map_meta_cap', $args);
  218. // Must have ALL requested caps
  219. $capabilities = apply_filters('user_has_cap', $this->allcaps, $caps, $args);
  220. foreach ($caps as $cap) {
  221. //echo "Checking cap $cap<br />";
  222. if(empty($capabilities[$cap]) || !$capabilities[$cap])
  223. return false;
  224. }
  225. return true;
  226. }
  227. function translate_level_to_cap($level) {
  228. return 'level_' . $level;
  229. }
  230. }
  231. // Map meta capabilities to primitive capabilities.
  232. function map_meta_cap($cap, $user_id) {
  233. $args = array_slice(func_get_args(), 2);
  234. $caps = array();
  235. switch ($cap) {
  236. case 'delete_user':
  237. $caps[] = 'delete_users';
  238. break;
  239. case 'edit_user':
  240. $caps[] = 'edit_users';
  241. break;
  242. case 'delete_post':
  243. $author_data = get_userdata($user_id);
  244. //echo "post ID: {$args[0]}<br />";
  245. $post = get_post($args[0]);
  246. if ( 'page' == $post->post_type ) {
  247. $args = array_merge(array('delete_page', $user_id), $args);
  248. return call_user_func_array('map_meta_cap', $args);
  249. }
  250. $post_author_data = get_userdata($post->post_author);
  251. //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
  252. // If the user is the author...
  253. if ($user_id == $post_author_data->ID) {
  254. // If the post is published...
  255. if ($post->post_status == 'publish')
  256. $caps[] = 'delete_published_posts';
  257. else
  258. // If the post is draft...
  259. $caps[] = 'delete_posts';
  260. } else {
  261. // The user is trying to edit someone else's post.
  262. $caps[] = 'delete_others_posts';
  263. // The post is published, extra cap required.
  264. if ($post->post_status == 'publish')
  265. $caps[] = 'delete_published_posts';
  266. else if ($post->post_status == 'private')
  267. $caps[] = 'delete_private_posts';
  268. }
  269. break;
  270. case 'delete_page':
  271. $author_data = get_userdata($user_id);
  272. //echo "post ID: {$args[0]}<br />";
  273. $page = get_page($args[0]);
  274. $page_author_data = get_userdata($page->post_author);
  275. //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
  276. // If the user is the author...
  277. if ($user_id == $page_author_data->ID) {
  278. // If the page is published...
  279. if ($page->post_status == 'publish')
  280. $caps[] = 'delete_published_pages';
  281. else
  282. // If the page is draft...
  283. $caps[] = 'delete_pages';
  284. } else {
  285. // The user is trying to edit someone else's page.
  286. $caps[] = 'delete_others_pages';
  287. // The page is published, extra cap required.
  288. if ($page->post_status == 'publish')
  289. $caps[] = 'delete_published_pages';
  290. else if ($page->post_status == 'private')
  291. $caps[] = 'delete_private_pages';
  292. }
  293. break;
  294. // edit_post breaks down to edit_posts, edit_published_posts, or
  295. // edit_others_posts
  296. case 'edit_post':
  297. $author_data = get_userdata($user_id);
  298. //echo "post ID: {$args[0]}<br />";
  299. $post = get_post($args[0]);
  300. if ( 'page' == $post->post_type ) {
  301. $args = array_merge(array('edit_page', $user_id), $args);
  302. return call_user_func_array('map_meta_cap', $args);
  303. }
  304. $post_author_data = get_userdata($post->post_author);
  305. //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
  306. // If the user is the author...
  307. if ($user_id == $post_author_data->ID) {
  308. // If the post is published...
  309. if ($post->post_status == 'publish')
  310. $caps[] = 'edit_published_posts';
  311. else
  312. // If the post is draft...
  313. $caps[] = 'edit_posts';
  314. } else {
  315. // The user is trying to edit someone else's post.
  316. $caps[] = 'edit_others_posts';
  317. // The post is published, extra cap required.
  318. if ($post->post_status == 'publish')
  319. $caps[] = 'edit_published_posts';
  320. else if ($post->post_status == 'private')
  321. $caps[] = 'edit_private_posts';
  322. }
  323. break;
  324. case 'edit_page':
  325. $author_data = get_userdata($user_id);
  326. //echo "post ID: {$args[0]}<br />";
  327. $page = get_page($args[0]);
  328. $page_author_data = get_userdata($page->post_author);
  329. //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
  330. // If the user is the author...
  331. if ($user_id == $page_author_data->ID) {
  332. // If the page is published...
  333. if ($page->post_status == 'publish')
  334. $caps[] = 'edit_published_pages';
  335. else
  336. // If the page is draft...
  337. $caps[] = 'edit_pages';
  338. } else {
  339. // The user is trying to edit someone else's page.
  340. $caps[] = 'edit_others_pages';
  341. // The page is published, extra cap required.
  342. if ($page->post_status == 'publish')
  343. $caps[] = 'edit_published_pages';
  344. else if ($page->post_status == 'private')
  345. $caps[] = 'edit_private_pages';
  346. }
  347. break;
  348. case 'read_post':
  349. $post = get_post($args[0]);
  350. if ( 'page' == $post->post_type ) {
  351. $args = array_merge(array('read_page', $user_id), $args);
  352. return call_user_func_array('map_meta_cap', $args);
  353. }
  354. if ( 'private' != $post->post_status ) {
  355. $caps[] = 'read';
  356. break;
  357. }
  358. $author_data = get_userdata($user_id);
  359. $post_author_data = get_userdata($post->post_author);
  360. if ($user_id == $post_author_data->ID)
  361. $caps[] = 'read';
  362. else
  363. $caps[] = 'read_private_posts';
  364. break;
  365. case 'read_page':
  366. $page = get_page($args[0]);
  367. if ( 'private' != $page->post_status ) {
  368. $caps[] = 'read';
  369. break;
  370. }
  371. $author_data = get_userdata($user_id);
  372. $page_author_data = get_userdata($page->post_author);
  373. if ($user_id == $page_author_data->ID)
  374. $caps[] = 'read';
  375. else
  376. $caps[] = 'read_private_pages';
  377. break;
  378. default:
  379. // If no meta caps match, return the original cap.
  380. $caps[] = $cap;
  381. }
  382. return $caps;
  383. }
  384. // Capability checking wrapper around the global $current_user object.
  385. function current_user_can($capability) {
  386. $current_user = wp_get_current_user();
  387. if ( empty($current_user) )
  388. return false;
  389. $args = array_slice(func_get_args(), 1);
  390. $args = array_merge(array($capability), $args);
  391. return call_user_func_array(array(&$current_user, 'has_cap'), $args);
  392. }
  393. // Convenience wrappers around $wp_roles.
  394. function get_role($role) {
  395. global $wp_roles;
  396. if ( ! isset($wp_roles) )
  397. $wp_roles = new WP_Roles();
  398. return $wp_roles->get_role($role);
  399. }
  400. function add_role($role, $display_name, $capabilities = '') {
  401. global $wp_roles;
  402. if ( ! isset($wp_roles) )
  403. $wp_roles = new WP_Roles();
  404. return $wp_roles->add_role($role, $display_name, $capabilities);
  405. }
  406. function remove_role($role) {
  407. global $wp_roles;
  408. if ( ! isset($wp_roles) )
  409. $wp_roles = new WP_Roles();
  410. return $wp_roles->remove_role($role);
  411. }
  412. ?>