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

/wp-content/plugins/role-scoper/role-scoper_init.php

https://bitbucket.org/broderboy/nycendurance-wordpress
PHP | 1043 lines | 700 code | 258 blank | 85 comment | 214 complexity | 9a1a0239769042d448eda7390037d362 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0, Apache-2.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. if( basename(__FILE__) == basename($_SERVER['SCRIPT_FILENAME']) )
  3. die();
  4. require_once( dirname(__FILE__).'/hardway/cache-persistent.php');
  5. //if ( ! awp_ver( '3.0' ) )
  6. // require_once( dirname(__FILE__).'/wp-legacy_rs.php' );
  7. // As of WP 3.0, this is not set until admin_header is loaded, and remains unset for non-admin urls. To simplify subsequent checks, set it early and universally.
  8. $GLOBALS['plugin_page_cr'] = ( is_admin() && isset( $_GET['page'] ) ) ? $_GET['page'] : '';
  9. if ( is_admin() )
  10. require_once( dirname(__FILE__).'/admin/admin-init_rs.php' );
  11. if ( IS_MU_RS )
  12. require_once( dirname(__FILE__).'/mu-init_rs.php' );
  13. if ( IS_MU_RS || defined('SCOPER_FORCE_FILE_INCLUSIONS') ) {
  14. // workaround to avoid file error on get_home_path() call
  15. if ( file_exists( ABSPATH . '/wp-admin/includes/file.php' ) )
  16. include_once( ABSPATH . '/wp-admin/includes/file.php' );
  17. }
  18. // If an htaccess regeneration is triggered by somebody else, insert our rules (normal non-MU installations).
  19. if ( ! defined( 'SCOPER_NO_HTACCESS' ) )
  20. add_filter( 'mod_rewrite_rules', 'scoper_mod_rewrite_rules' );
  21. add_action( 'delete_option', 'scoper_maybe_rewrite_inclusions' );
  22. add_action( 'delete_transient_rewrite_rules', 'scoper_rewrite_inclusions' );
  23. // some options can be overridden by constant definition
  24. add_filter( 'site_options_rs', 'scoper_apply_constants', 99 );
  25. add_filter( 'options_rs', 'scoper_apply_constants', 99 );
  26. function scoper_log_init_action() {
  27. define ( 'INIT_ACTION_DONE_RS', true );
  28. require_once( dirname(__FILE__).'/db-config_rs.php');
  29. $func = "require('" . dirname(__FILE__) . "/db-config_rs.php');";
  30. add_action( 'switch_blog', create_function( '', $func ) );
  31. if ( is_admin() )
  32. scoper_load_textdomain();
  33. elseif ( defined('XMLRPC_REQUEST') )
  34. require_once( dirname(__FILE__).'/xmlrpc_rs.php');
  35. }
  36. function scoper_act_set_current_user() {
  37. $id = ( ! empty($GLOBALS['current_user']) ) ? $GLOBALS['current_user']->ID : 0;
  38. if ( $id ) {
  39. require_once( dirname(__FILE__).'/scoped-user.php');
  40. $GLOBALS['current_rs_user'] = new WP_Scoped_User($id);
  41. // other properties (blog_roles, assigned_term_roles, term_roles) will be set as populated
  42. foreach( array( 'groups', 'assigned_blog_roles' ) as $var ) {
  43. $GLOBALS['current_user']->$var = $GLOBALS['current_rs_user']->$var;
  44. }
  45. } else {
  46. require_once( dirname(__FILE__).'/scoped-user_anon.php');
  47. $GLOBALS['current_rs_user'] = new WP_Scoped_User_Anon();
  48. }
  49. // since sequence of set_current_user and init actions seems unreliable, make sure our current_user is loaded first
  50. if ( ! empty( $GLOBALS['scoper'] ) )
  51. return;
  52. elseif ( defined('INIT_ACTION_DONE_RS') )
  53. scoper_init();
  54. else {
  55. static $done = false;
  56. if ( $done ) { return; } else { $done = true; }
  57. $priority = ( defined( 'SCOPER_EARLY_INIT' ) ) ? 3 : 50;
  58. add_action('init', 'scoper_init', $priority);
  59. }
  60. }
  61. function scoper_init() {
  62. global $scoper;
  63. // Work around bug in More Taxonomies (and possibly other plugins) where category taxonomy is overriden without setting it public
  64. foreach( array( 'category', 'post_tag' ) as $taxonomy ) {
  65. if ( isset( $GLOBALS['wp_taxonomies'][$taxonomy] ) )
  66. $GLOBALS['wp_taxonomies'][$taxonomy]->public = true;
  67. }
  68. if ( IS_MU_RS ) {
  69. global $scoper_sitewide_options;
  70. $scoper_sitewide_options = apply_filters( 'sitewide_options_rs' , $scoper_sitewide_options );
  71. }
  72. require_once( dirname(__FILE__).'/wp-cap-helper_cr.php' );
  73. WP_Cap_Helper_CR::establish_status_caps();
  74. WP_Cap_Helper_CR::force_distinct_post_caps();
  75. WP_Cap_Helper_CR::force_distinct_taxonomy_caps();
  76. if ( is_admin() ) {
  77. require_once( dirname(__FILE__).'/admin/admin-init_rs.php' ); // TODO: why is the require statement up top not sufficient for NGG 1.7.2 uploader?
  78. scoper_admin_init();
  79. }
  80. //log_mem_usage_rs( 'scoper_admin_init done' );
  81. require_once( dirname(__FILE__).'/scoped-user.php');
  82. require_once( dirname(__FILE__).'/role-scoper_main.php');
  83. //log_mem_usage_rs( 'require role-scoper_main' );
  84. if ( empty($scoper) ) { // set_current_user may have already triggered scoper creation and role_cap load
  85. $scoper = new Scoper();
  86. //log_mem_usage_rs( 'new Scoper done' );
  87. $scoper->init();
  88. }
  89. // ensure that content administrators (as defined by SCOPER_CONTENT_ADMIN_CAP) have all caps for custom types by default
  90. if ( is_content_administrator_rs() ) {
  91. global $current_rs_user;
  92. if ( ! empty($current_rs_user) ) { // user object not set when scoper_init() is manually invoked to support htaccess rule generation on plugin activation
  93. foreach ( get_post_types( array('public' => true, '_builtin' => false) ) as $name )
  94. $current_rs_user->assigned_blog_roles[ANY_CONTENT_DATE_RS]["rs_{$name}_editor"] = true;
  95. $taxonomies = get_taxonomies( array('public' => true, '_builtin' => false) );
  96. $taxonomies []= 'nav_menu';
  97. foreach ( $taxonomies as $name )
  98. $current_rs_user->assigned_blog_roles[ANY_CONTENT_DATE_RS]["rs_{$name}_manager"] = true;
  99. $current_rs_user->merge_scoped_blogcaps();
  100. $GLOBALS['current_user']->allcaps = $current_rs_user->allcaps;
  101. $GLOBALS['current_user']->assigned_blog_roles = $current_rs_user->assigned_blog_roles;
  102. }
  103. }
  104. if ( ! empty($_GET['action']) && ( 'expire_file_rules' == $_GET['action'] ) ) {
  105. require_once( dirname(__FILE__).'/attachment-helper_rs.php' );
  106. scoper_requested_file_rule_expire();
  107. }
  108. //log_mem_usage_rs( 'scoper->init() done' );
  109. }
  110. function rs_get_user( $user_id, $name = '', $args = array() ) {
  111. if ( ! class_exists( 'WP_Scoped_User' ) )
  112. require_once( dirname(__FILE__).'/scoped-user.php');
  113. return new WP_Scoped_User( $user_id, $name, $args );
  114. }
  115. function scoper_load_textdomain() {
  116. if ( defined( 'SCOPER_TEXTDOMAIN_LOADED' ) )
  117. return;
  118. load_plugin_textdomain( 'scoper', false, SCOPER_FOLDER . '/languages' );
  119. define('SCOPER_TEXTDOMAIN_LOADED', true);
  120. }
  121. function scoper_get_init_options() {
  122. define ( 'SCOPER_CUSTOM_USER_BLOGCAPS', scoper_get_option('custom_user_blogcaps') ); // TODO: eliminate this?
  123. $define_groups = scoper_get_option('define_usergroups');
  124. define ( 'DEFINE_GROUPS_RS', $define_groups );
  125. define ( 'GROUP_ROLES_RS', $define_groups && scoper_get_option('enable_group_roles') );
  126. define ( 'USER_ROLES_RS', scoper_get_option('enable_user_roles') );
  127. if ( ! defined('DISABLE_PERSISTENT_CACHE') && ! scoper_get_option('persistent_cache') )
  128. define ( 'DISABLE_PERSISTENT_CACHE', true );
  129. wpp_cache_init( IS_MU_RS && scoper_establish_group_scope() );
  130. }
  131. function scoper_refresh_options() {
  132. if ( IS_MU_RS ) {
  133. scoper_retrieve_options(true);
  134. scoper_refresh_options_sitewide();
  135. }
  136. scoper_retrieve_options(false);
  137. scoper_refresh_default_options();
  138. }
  139. function scoper_set_conditional_defaults() {
  140. // if the WP installation has 100 or more users at initial Role Scoper installation, default to CSV input of username for role assignment
  141. global $wpdb;
  142. $num_users = $wpdb->get_var( "SELECT COUNT(ID) FROM $wpdb->users" );
  143. if ( $num_users > 99 )
  144. update_option( 'scoper_user_role_assignment_csv', 1 );
  145. }
  146. function scoper_refresh_default_options() {
  147. global $scoper_default_options;
  148. require_once( dirname(__FILE__).'/defaults_rs.php');
  149. $scoper_default_options = apply_filters( 'default_options_rs', scoper_default_options() );
  150. if ( IS_MU_RS )
  151. scoper_apply_custom_default_options( 'scoper_default_options' );
  152. }
  153. function scoper_refresh_default_otype_options() {
  154. global $scoper_default_otype_options;
  155. require_once( dirname(__FILE__).'/defaults_rs.php');
  156. $scoper_default_otype_options = apply_filters( 'default_otype_options_rs', scoper_default_otype_options() );
  157. // compat workaround for old versions of Role Scoping for NGG which use old otype option key structure
  158. if ( isset( $scoper_default_otype_options['use_term_roles']['ngg_gallery:ngg_gallery'] ) && ( ! is_array($scoper_default_otype_options['use_term_roles']['ngg_gallery:ngg_gallery']) ) )
  159. $scoper_default_otype_options['use_term_roles']['ngg_gallery:ngg_gallery'] = array( 'ngg_album' => 1 );
  160. if ( IS_MU_RS )
  161. scoper_apply_custom_default_options( 'scoper_default_otype_options' );
  162. }
  163. function scoper_get_default_otype_options() {
  164. if ( did_action( 'scoper_init') ) {
  165. global $scoper_default_otype_options;
  166. if ( ! isset( $scoper_default_otype_options ) )
  167. scoper_refresh_default_otype_options();
  168. return $scoper_default_otype_options;
  169. } else
  170. return scoper_default_otype_options();
  171. }
  172. function scoper_delete_option( $option_basename, $sitewide = -1 ) {
  173. // allow explicit selection of sitewide / non-sitewide scope for better performance and update security
  174. if ( -1 === $sitewide ) {
  175. global $scoper_options_sitewide;
  176. $sitewide = isset( $scoper_options_sitewide ) && ! empty( $scoper_options_sitewide[$option_basename] );
  177. }
  178. if ( $sitewide ) {
  179. global $wpdb;
  180. scoper_query( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = '$wpdb->siteid' AND meta_key = 'scoper_$option_basename'" );
  181. } else
  182. delete_option( "scoper_$option_basename" );
  183. }
  184. function scoper_update_option( $option_basename, $option_val, $sitewide = -1 ) {
  185. // allow explicit selection of sitewide / non-sitewide scope for better performance and update security
  186. if ( -1 === $sitewide ) {
  187. global $scoper_options_sitewide;
  188. $sitewide = isset( $scoper_options_sitewide ) && ! empty( $scoper_options_sitewide[$option_basename] );
  189. }
  190. if ( $sitewide ) {
  191. global $scoper_site_options;
  192. $scoper_site_options[$option_basename] = $option_val;
  193. //d_echo("<br /><br />sitewide: $option_basename, value : " . maybe_serialize($option_val) );
  194. update_site_option( "scoper_$option_basename", $option_val );
  195. } else {
  196. //d_echo("<br />blogwide: $option_basename" );
  197. global $scoper_blog_options;
  198. $scoper_blog_options[$option_basename] = $option_val;
  199. update_option( "scoper_$option_basename", $option_val );
  200. }
  201. }
  202. function scoper_apply_constants($stored_options) {
  203. // If file filtering option is on but the DISABLE constant has been set, turn the option off and regenerate .htaccess
  204. if ( defined( 'DISABLE_ATTACHMENT_FILTERING' ) && DISABLE_ATTACHMENT_FILTERING ) {
  205. if ( ! empty( $stored_options['scoper_file_filtering'] ) ) {
  206. // in this case, we need to both convert the option value to constant value AND trigger .htaccess regeneration
  207. $stored_options['file_filtering'] = 0;
  208. update_option( 'scoper_file_filtering', 0 );
  209. scoper_flush_site_rules();
  210. scoper_expire_file_rules();
  211. }
  212. }
  213. return $stored_options;
  214. }
  215. function scoper_retrieve_options( $sitewide = false ) {
  216. global $wpdb;
  217. if ( $sitewide ) {
  218. global $scoper_site_options;
  219. $scoper_site_options = array();
  220. if ( $results = scoper_get_results( "SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE site_id = '$wpdb->siteid' AND meta_key LIKE 'scoper_%'" ) )
  221. foreach ( $results as $row )
  222. $scoper_site_options[$row->meta_key] = $row->meta_value;
  223. $scoper_site_options = apply_filters( 'site_options_rs', $scoper_site_options );
  224. return $scoper_site_options;
  225. } else {
  226. global $scoper_blog_options;
  227. $scoper_blog_options = array();
  228. if ( $results = scoper_get_results("SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE 'scoper_%'") )
  229. foreach ( $results as $row )
  230. $scoper_blog_options[$row->option_name] = $row->option_value;
  231. $scoper_blog_options = apply_filters( 'options_rs', $scoper_blog_options );
  232. return $scoper_blog_options;
  233. }
  234. }
  235. function scoper_get_site_option( $option_basename ) {
  236. return scoper_get_option( $option_basename, true );
  237. }
  238. function scoper_get_option($option_basename, $sitewide = -1, $get_default = false) {
  239. global $scoper_default_options;
  240. //if ( empty( $scoper_default_options ) && did_action( 'scoper_init' ) ) // Make sure other plugins have had a chance to apply any filters to default options
  241. if ( empty( $scoper_default_options ) )
  242. scoper_refresh_default_options();
  243. if ( ! $get_default ) {
  244. // allow explicit selection of sitewide / non-sitewide scope for better performance and update security
  245. if ( -1 === $sitewide ) {
  246. global $scoper_options_sitewide;
  247. $sitewide = isset( $scoper_options_sitewide ) && ! empty( $scoper_options_sitewide[$option_basename] );
  248. }
  249. //dump($scoper_options_sitewide);
  250. if ( $sitewide ) {
  251. // this option is set site-wide
  252. global $scoper_site_options;
  253. if ( ! isset($scoper_site_options) || is_null($scoper_site_options) )
  254. $scoper_site_options = scoper_retrieve_options( true );
  255. if ( isset($scoper_site_options["scoper_{$option_basename}"]) )
  256. $optval = $scoper_site_options["scoper_{$option_basename}"];
  257. } else {
  258. //dump($option_basename);
  259. global $scoper_blog_options;
  260. if ( ! isset($scoper_blog_options) || is_null($scoper_blog_options) )
  261. $scoper_blog_options = scoper_retrieve_options( false );
  262. if ( isset($scoper_blog_options["scoper_$option_basename"]) )
  263. $optval = $scoper_blog_options["scoper_$option_basename"];
  264. }
  265. }
  266. //dump($get_default);
  267. //dump($scoper_blog_options);
  268. if ( ! isset( $optval ) ) {
  269. if ( ! empty($scoper_default_options) && ! empty( $scoper_default_options[$option_basename] ) )
  270. $optval = $scoper_default_options[$option_basename];
  271. if ( ! isset($optval) ) {
  272. global $scoper_default_otype_options;
  273. if ( isset( $scoper_default_otype_options[$option_basename] ) )
  274. return $scoper_default_otype_options[$option_basename];
  275. /*
  276. else {
  277. static $hardcode_option_defaults;
  278. if ( empty($hardcode_option_defaults) ) {
  279. require_once( dirname(__FILE__).'/defaults_rs.php');
  280. $hardcode_option_defaults = scoper_default_options();
  281. }
  282. if ( isset($hardcode_option_defaults[$option_basename]) )
  283. $optval = $hardcode_option_defaults[$option_basename];
  284. else {
  285. static $hardcode_otype_option_defaults;
  286. if ( empty($hardcode_otype_option_defaults) )
  287. $hardcode_otype_option_defaults = scoper_default_otype_options();
  288. if ( isset($hardcode_otype_option_defaults[$option_basename]) )
  289. $optval = $hardcode_otype_option_defaults[$option_basename];
  290. }
  291. }
  292. */
  293. }
  294. }
  295. if ( isset($optval) )
  296. $optval = maybe_unserialize($optval);
  297. else
  298. $optval = '';
  299. // merge defaults into stored option array
  300. //if ( ! empty( $GLOBALS['scoper_option_arrays'][$option_basename] ) ) {
  301. if ( 'use_post_types' == $option_basename ) {
  302. static $default_post_types;
  303. if ( empty($default_post_types) || ! did_action('init') ) {
  304. $default_post_types = array();
  305. foreach ( array_diff( get_post_types( array( 'public' => true ) ), array( 'attachment' ) ) as $type )
  306. $default_post_types[$type] = 1;
  307. }
  308. $optval = array_merge( $default_post_types, (array) $optval );
  309. } elseif ( 'use_taxonomies' == $option_basename ) {
  310. static $default_taxonomies;
  311. if ( empty($default_taxonomies) || ! did_action('init') ) {
  312. $default_taxonomies = array();
  313. $taxonomies = get_taxonomies( array( 'public' => true ) );
  314. $taxonomies[] = 'nav_menu';
  315. foreach ( $taxonomies as $taxonomy )
  316. $default_taxonomies[$taxonomy] = ( isset( $GLOBALS['rs_default_disable_taxonomies'][$taxonomy] ) ) ? 0 : 1;
  317. }
  318. $optval = array_diff_key( array_merge( $default_taxonomies, (array) $optval ), $GLOBALS['rs_forbidden_taxonomies'] ); // remove forbidden taxonomies, even if previously stored
  319. } elseif ( 'use_term_roles' == $option_basename ) {
  320. if ( $optval ) {
  321. foreach( array_keys($optval) as $key ) {
  322. $optval[$key] = array_diff_key( $optval[$key], $GLOBALS['rs_forbidden_taxonomies'] ); // remove forbidden taxonomies, even if previously stored
  323. }
  324. }
  325. }
  326. return $optval;
  327. }
  328. function scoper_get_otype_option( $option_main_key, $src_name, $object_type = '', $access_name = '') {
  329. static $otype_options;
  330. // make sure we indicate object roles disabled if object type usage is completely disabled
  331. if ( 'use_object_roles' == $option_main_key ) {
  332. if ( ( 'post' == $src_name ) && $object_type ) {
  333. $use_object_types = scoper_get_option( 'use_post_types' );
  334. if ( ( ! empty($use_object_types) ) && empty( $use_object_types[$object_type] ) ) // since default is to enable all object types, don't interfere if no use_object_types option is stored
  335. return false;
  336. }
  337. }
  338. $key = "$option_main_key,$src_name,$object_type,$access_name";
  339. if ( empty($otype_options) )
  340. $otype_options = array();
  341. elseif ( isset($otype_options[$key]) )
  342. return $otype_options[$key];
  343. $stored_option = scoper_get_option($option_main_key);
  344. $default_otype_options = scoper_get_default_otype_options();
  345. // RS stores all portions of the otype option array together, but blending is needed because RS Extensions or other plugins can filter the default otype options array for specific taxonomies / object types
  346. $optval = awp_blend_option_array( 'scoper_', $option_main_key, $default_otype_options, 1, $stored_option );
  347. // note: access_name-specific entries are not valid for most otype options (but possibly for teaser text front vs. rss)
  348. if ( isset ( $optval[$src_name] ) )
  349. $retval = $optval[$src_name];
  350. if ( $object_type && isset( $optval["$src_name:$object_type"] ) )
  351. $retval = $optval["$src_name:$object_type"];
  352. if ( $object_type && $access_name && isset( $optval["$src_name:$object_type:$access_name"] ) )
  353. $retval = $optval["$src_name:$object_type:$access_name"];
  354. // if no match was found for a source request, accept any non-empty otype match
  355. if ( ! $object_type && ! isset($retval) )
  356. foreach ( $optval as $src_otype => $val )
  357. if ( $val && ( 0 === strpos( $src_otype, "$src_name:" ) ) )
  358. $retval = $val;
  359. if ( ! isset($retval) )
  360. $retval = array();
  361. $otype_options[$key] = $retval;
  362. return $retval;
  363. }
  364. function is_taxonomy_used_rs( $taxonomy ) {
  365. global $scoper_default_otype_options;
  366. $stored_option = (array) scoper_get_option( 'use_term_roles' );
  367. foreach( array_merge( array_keys($scoper_default_otype_options['use_term_roles']), array_keys($stored_option) ) as $key ) {
  368. $term_roles[$key] = ( isset($scoper_default_otype_options['use_term_roles'][$key]) ) ? $scoper_default_otype_options['use_term_roles'][$key] : array();
  369. if ( isset( $stored_option[$key] ) )
  370. $term_roles[$key] = array_merge( $term_roles[$key], $stored_option[$key] );
  371. }
  372. foreach ( $term_roles as $taxonomies ) // keyed by src_otype
  373. if ( ! empty( $taxonomies[$taxonomy] ) )
  374. return true;
  375. }
  376. function scoper_maybe_rewrite_inclusions ( $option_name = '' ) {
  377. if ( $option_name == 'rewrite_rules' )
  378. scoper_rewrite_inclusions();
  379. }
  380. function scoper_rewrite_inclusions ( $option_name = '' ) {
  381. // force inclusion of required files in case flush_rules() is called from outside wp-admin, to prevent error when calling get_home_path() function
  382. if ( file_exists( ABSPATH . '/wp-admin/includes/misc.php' ) )
  383. include_once( ABSPATH . '/wp-admin/includes/misc.php' );
  384. if ( file_exists( ABSPATH . '/wp-admin/includes/file.php' ) )
  385. include_once( ABSPATH . '/wp-admin/includes/file.php' );
  386. }
  387. // htaccess directive intercepts direct access to uploaded files, converts to WP call with custom args to be caught by subsequent parse_query filter
  388. // parse_query filter will return content only if user can read a containing post/page
  389. function scoper_mod_rewrite_rules ( $rules ) {
  390. if ( defined( 'SCOPER_NO_HTACCESS' ) )
  391. return $rules;
  392. $file_filtering = scoper_get_option( 'file_filtering' );
  393. global $scoper;
  394. if ( ! isset($scoper) || is_null($scoper) )
  395. scoper_init();
  396. require_once( dirname(__FILE__).'/rewrite-rules_rs.php' );
  397. if ( IS_MU_RS ) {
  398. if ( $file_filtering ) {
  399. require_once( dirname(__FILE__).'/rewrite-mu_rs.php' );
  400. $rules = ScoperRewriteMU::insert_site_rules( $rules );
  401. }
  402. } else {
  403. if ( ! strpos( $rules, 'BEGIN Role Scoper' ) ) {
  404. $rs_rules = ScoperRewrite::build_site_rules();
  405. $rules .= $rs_rules;
  406. }
  407. }
  408. return $rules;
  409. }
  410. function scoper_flush_site_rules() {
  411. require_once( dirname(__FILE__).'/rewrite-rules_rs.php' );
  412. ScoperRewrite::update_site_rules( true );
  413. }
  414. function scoper_clear_site_rules() {
  415. require_once( dirname(__FILE__).'/rewrite-rules_rs.php' );
  416. remove_filter('mod_rewrite_rules', 'scoper_mod_rewrite_rules');
  417. ScoperRewrite::update_site_rules( false );
  418. }
  419. function scoper_flush_file_rules() {
  420. require_once( dirname(__FILE__).'/rewrite-rules_rs.php' );
  421. ScoperRewrite::update_blog_file_rules();
  422. }
  423. function scoper_clear_all_file_rules() {
  424. if ( IS_MU_RS ) {
  425. require_once( dirname(__FILE__).'/rewrite-mu_rs.php' );
  426. ScoperRewriteMU::clear_all_file_rules();
  427. } else {
  428. require_once( dirname(__FILE__).'/rewrite-rules_rs.php' );
  429. ScoperRewrite::update_blog_file_rules( false );
  430. }
  431. }
  432. // forces content rules to be regenerated in every MU blog at next access
  433. function scoper_expire_file_rules() {
  434. if ( IS_MU_RS )
  435. scoper_update_option( 'file_htaccess_min_date', agp_time_gmt(), true );
  436. else {
  437. if ( did_action( 'scoper_init' ) )
  438. scoper_flush_file_rules(); // for non-MU, just regenerate the file rules (for uploads folder) now
  439. else
  440. add_action( 'scoper_init', 'scoper_flush_file_rules' );
  441. }
  442. }
  443. function scoper_version_check() {
  444. $ver_change = false;
  445. $ver = get_option('scoper_version');
  446. if ( empty($ver['db_version']) || version_compare( SCOPER_DB_VERSION, $ver['db_version'], '!=') ) {
  447. $ver_change = true;
  448. require_once( dirname(__FILE__).'/db-setup_rs.php');
  449. scoper_db_setup($ver['db_version']);
  450. }
  451. // These maintenance operations only apply when a previous version of RS was installed
  452. if ( ! empty($ver['version']) ) {
  453. if ( version_compare( SCOPER_VERSION, $ver['version'], '!=') ) {
  454. $ver_change = true;
  455. require_once( dirname(__FILE__).'/admin/update_rs.php');
  456. scoper_version_updated( $ver['version'] );
  457. scoper_check_revision_settings();
  458. }
  459. } else {
  460. // first-time install (or previous install was totally wiped)
  461. require_once( dirname(__FILE__).'/admin/update_rs.php');
  462. scoper_set_default_rs_roledefs();
  463. }
  464. if ( $ver_change ) {
  465. $ver = array(
  466. 'version' => SCOPER_VERSION,
  467. 'db_version' => SCOPER_DB_VERSION
  468. );
  469. update_option( 'scoper_version', $ver );
  470. }
  471. }
  472. function scoper_get_role_handle($role_name, $role_type) {
  473. return $role_type . '_' . str_replace(' ', '_', $role_name);
  474. }
  475. function scoper_role_names_to_handles($role_names, $role_type, $fill_keys = false) {
  476. $role_names = (array) $role_names;
  477. $role_handles = array();
  478. foreach ( $role_names as $role_name )
  479. if ( $fill_keys )
  480. $role_handles[ $role_type . '_' . str_replace(' ', '_', $role_name) ] = 1;
  481. else
  482. $role_handles[]= $role_type . '_' . str_replace(' ', '_', $role_name);
  483. return $role_handles;
  484. }
  485. function scoper_explode_role_handle($role_handle) {
  486. global $scoper_role_types;
  487. $arr = (object) array();
  488. foreach ( $scoper_role_types as $role_type ) {
  489. if ( 0 === strpos($role_handle, $role_type . '_') ) {
  490. $arr->role_type = $role_type;
  491. $arr->role_name = substr($role_handle, strlen($role_type) + 1);
  492. break;
  493. }
  494. }
  495. return $arr;
  496. }
  497. function scoper_role_handles_to_names($role_handles) {
  498. global $scoper_role_types;
  499. $role_names = array();
  500. foreach ( $role_handles as $role_handle ) {
  501. foreach ( $scoper_role_types as $role_type )
  502. $role_handle = str_replace( $role_type . '_', '', $role_handle);
  503. $role_names[] = $role_handle;
  504. }
  505. return $role_names;
  506. }
  507. function rs_notice($message) {
  508. if ( defined( 'RS_DEBUG' ) ) {
  509. require_once( dirname(__FILE__).'/error_rs.php' );
  510. awp_notice( $message, 'Role Scoper' );
  511. }
  512. }
  513. // db wrapper methods allow us to easily avoid re-filtering our own query
  514. function scoper_db_method($method_name, $query) {
  515. global $wpdb;
  516. //static $buffer;
  517. if ( is_admin() ) { // Low-level query filtering is necessary due to WP API limitations pertaining to admin GUI.
  518. // But make sure we don't chew our own cud (currently not an issue for front end)
  519. global $scoper_status;
  520. if ( empty($scoper_status) )
  521. $scoper_status = (object) array();
  522. /*
  523. $use_buffer = ('query' != $method_name ) && empty($_POST);
  524. if ( $use_buffer ) {
  525. $key = md5($query);
  526. if ( isset($buffer[$key]) )
  527. return $buffer[$key];
  528. }
  529. */
  530. $scoper_status->querying_db = true;
  531. $results = call_user_func( array(&$wpdb, $method_name), $query );
  532. $scoper_status->querying_db = false;
  533. //if ( $use_buffer )
  534. // $buffer[$key] = $results;
  535. return $results;
  536. } else
  537. return call_user_func( array(&$wpdb, $method_name), $query );
  538. }
  539. function scoper_get_results($query) {
  540. return scoper_db_method('get_results', $query);
  541. }
  542. function scoper_get_row($query) {
  543. return scoper_db_method('get_row', $query);
  544. }
  545. function scoper_get_col($query) {
  546. return scoper_db_method('get_col', $query);
  547. }
  548. function scoper_get_var($query) {
  549. return scoper_db_method('get_var', $query);
  550. }
  551. function scoper_query($query) {
  552. return scoper_db_method('query', $query);
  553. }
  554. function scoper_querying_db() {
  555. if ( isset($GLOBALS['scoper_status']) )
  556. return ! empty($GLOBALS['scoper_status']->querying_db);
  557. }
  558. function scoper_any_role_limits() {
  559. global $wpdb;
  560. $any_limits = (object) array( 'date_limited' => false, 'start_date_gmt' => false, 'end_date_gmt' => false, 'content_date_limited' => false, 'content_min_date_gmt' => false, 'content_max_date_gmt' => false );
  561. if ( scoper_get_var( "SELECT assignment_id FROM $wpdb->user2role2object_rs WHERE date_limited > 0 LIMIT 1" ) ) {
  562. $any_limits->date_limited = true;
  563. if ( scoper_get_var( "SELECT assignment_id FROM $wpdb->user2role2object_rs WHERE start_date_gmt > 0 LIMIT 1" ) )
  564. $any_limits->start_date_gmt = true;
  565. if ( scoper_get_var( "SELECT assignment_id FROM $wpdb->user2role2object_rs WHERE end_date_gmt != '" . SCOPER_MAX_DATE_STRING . "' LIMIT 1" ) )
  566. $any_limits->end_date_gmt = true;
  567. }
  568. if ( scoper_get_var( "SELECT assignment_id FROM $wpdb->user2role2object_rs WHERE content_date_limited > 0 LIMIT 1" ) ) {
  569. $any_limits->content_date_limited = true;
  570. if ( scoper_get_var( "SELECT assignment_id FROM $wpdb->user2role2object_rs WHERE content_min_date_gmt > 0 LIMIT 1" ) )
  571. $any_limits->content_min_date_gmt = true;
  572. if ( scoper_get_var( "SELECT assignment_id FROM $wpdb->user2role2object_rs WHERE content_max_date_gmt != '" . SCOPER_MAX_DATE_STRING . "' LIMIT 1" ) )
  573. $any_limits->content_max_date_gmt = true;
  574. }
  575. return $any_limits;
  576. }
  577. function scoper_get_duration_clause( $content_date_comparison = '', $table_prefix = 'uro', $enforce_duration_limits = true ) {
  578. static $any_role_limits;
  579. $clause = '';
  580. if ( $enforce_duration_limits && scoper_get_option( 'role_duration_limits' ) ) {
  581. if ( ! isset($any_role_limits) )
  582. $any_role_limits = scoper_any_role_limits();
  583. if ( $any_role_limits->date_limited ) {
  584. $current_time = current_time( 'mysql', 1 );
  585. $subclauses = array();
  586. if ( $any_role_limits->start_date_gmt )
  587. $subclauses []= "$table_prefix.start_date_gmt <= '$current_time'";
  588. if ( $any_role_limits->end_date_gmt )
  589. $subclauses []= "$table_prefix.end_date_gmt >= '$current_time'";
  590. $role_duration_clause = implode( " AND ", $subclauses );
  591. $clause = " AND ( $table_prefix.date_limited = '0' OR ( $role_duration_clause ) ) ";
  592. }
  593. }
  594. if ( $content_date_comparison && scoper_get_option( 'role_content_date_limits' ) ) {
  595. if ( ! isset($any_role_limits) )
  596. $any_role_limits = scoper_any_role_limits();
  597. if ( $any_role_limits->content_date_limited ) {
  598. $current_time = current_time( 'mysql', 1 );
  599. $subclauses = array();
  600. if ( $any_role_limits->content_min_date_gmt )
  601. $subclauses []= "$content_date_comparison >= $table_prefix.content_min_date_gmt";
  602. if ( $any_role_limits->content_max_date_gmt )
  603. $subclauses []= "$content_date_comparison <= $table_prefix.content_max_date_gmt";
  604. $content_date_clause = implode( " AND ", $subclauses );
  605. $clause .= " AND ( $table_prefix.content_date_limited = '0' OR ( $content_date_clause ) ) ";
  606. }
  607. }
  608. return $clause;
  609. }
  610. function scoper_get_property_array( &$arr, $id_prop, $buffer_prop ) {
  611. if ( ! is_array($arr) )
  612. return;
  613. $buffer = array();
  614. foreach ( array_keys($arr) as $key )
  615. $buffer[ $arr[$key]->$id_prop ] = ( isset($arr[$key]->$buffer_prop) ) ? $arr[$key]->$buffer_prop : '';
  616. return $buffer;
  617. }
  618. function scoper_restore_property_array( &$target_arr, $buffer_arr, $id_prop, $buffer_prop ) {
  619. if ( ! is_array($target_arr) || ! is_array($buffer_arr) )
  620. return;
  621. foreach ( array_keys($target_arr) as $key )
  622. if ( isset( $buffer_arr[ $target_arr[$key]->$id_prop ] ) )
  623. $target_arr[$key]->$buffer_prop = $buffer_arr[ $target_arr[$key]->$id_prop ];
  624. }
  625. function scoper_get_taxonomy_usage( $src_name, $object_types = '' ) {
  626. $taxonomies = array();
  627. $object_types = (array) $object_types;
  628. foreach( $object_types as $object_type ) {
  629. if ( taxonomy_exists( $object_type ) )
  630. $use_taxonomies = array( $object_type => 1 ); // taxonomy management roles are always applied
  631. else
  632. $use_taxonomies = scoper_get_otype_option( 'use_term_roles', $src_name, $object_type );
  633. $taxonomies = array_merge( $taxonomies, array_intersect( (array) $use_taxonomies, array( 1 ) ) ); // array cast prevents PHP warning on first-time execution following update to RS 1.2
  634. }
  635. if ( $taxonomies ) {
  636. // make sure we indicate non-usage of term roles for taxonomies that are completely disabled for RS
  637. if ( 'post' == $src_name ) {
  638. $use_taxonomies = scoper_get_option( 'use_taxonomies' );
  639. $taxonomies = array_intersect_key( $taxonomies, array_intersect( $use_taxonomies, array( 1 ) ) );
  640. }
  641. return array_keys($taxonomies);
  642. } else
  643. return array();
  644. }
  645. function cr_find_post_type( $post_arg = '', $return_default = true ) {
  646. // the post argument is already an object. Just return its post_type property
  647. if ( is_object( $post_arg ) )
  648. return $post_arg->post_type;
  649. if ( $post_arg ) {
  650. // post_arg is not an object. If its value matches global post, use that
  651. if ( ! empty($GLOBALS['post']) && is_object($GLOBALS['post']) && ( $post_arg == $GLOBALS['post']->ID ) && ( 'revision' != $GLOBALS['post']->post_type ) ) {
  652. $_type = $GLOBALS['post']->post_type;
  653. if ( 'revision' == $_type )
  654. $_type = get_post_field( 'post_type', $GLOBALS['post']->post_parent );
  655. }
  656. // we have a post id but it doesn't match global post, so retrieve it
  657. if ( $_post = get_post( $post_arg ) ) {
  658. $_type = $_post->post_type;
  659. if ( 'revision' == $_type )
  660. $_type = get_post_field( 'post_type', $_post->post_parent );
  661. }
  662. if ( ! empty($_type) )
  663. return $_type;
  664. }
  665. // no post id was passed in, or we couldn't retrieve it for some reason, so check $_REQUEST args
  666. global $pagenow;
  667. if ( ! empty( $GLOBALS['post'] ) && is_object($GLOBALS['post']) && ! empty( $GLOBALS['post']->post_type ) ) {
  668. $object_type = $GLOBALS['post']->post_type;
  669. if ( 'revision' == $object_type )
  670. $object_type = get_post_field( 'post_type', $GLOBALS['post']->post_parent );
  671. } elseif ( ! empty( $GLOBALS['wp_query']->queried_object ) && ! empty( $GLOBALS['wp_query']->queried_object->post_type ) ) {
  672. $object_type = $GLOBALS['wp_query']->queried_object->post_type;
  673. } elseif ( ! empty( $GLOBALS['wp_query']->queried_object ) && ! empty( $GLOBALS['wp_query']->queried_object->name ) ) {
  674. $object_type = $GLOBALS['wp_query']->queried_object->name;
  675. } elseif ( in_array( $pagenow, array( 'post-new.php', 'edit.php' ) ) ) {
  676. $object_type = ! empty( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
  677. } elseif ( in_array( $pagenow, array( 'edit-tags.php' ) ) ) {
  678. $object_type = ! empty( $_GET['taxonomy'] ) ? $_GET['taxonomy'] : 'category';
  679. } elseif ( in_array( $pagenow, array( 'admin-ajax.php' ) ) && ! empty( $_REQUEST['taxonomy'] ) ) {
  680. $object_type = $_REQUEST['taxonomy'];
  681. } elseif ( ! empty( $_POST['post_ID'] ) ) {
  682. if ( $_post = get_post( $_POST['post_ID'] ) )
  683. $object_type = $_post->post_type;
  684. } elseif ( ! empty( $_GET['post'] ) ) { // post.php
  685. if ( $_post = get_post( $_GET['post'] ) )
  686. $object_type = $_post->post_type;
  687. } elseif( ! empty( $GLOBALS['scoper_object_type'] ) ) {
  688. $object_type = $GLOBALS['scoper_object_type'];
  689. } elseif( ! empty($_SERVER) && is_array($_SERVER) && isset( $_SERVER['HTTP_REFERER'] ) && is_string($_SERVER['HTTP_REFERER']) ) {
  690. if ( $pos = strpos( $_SERVER['HTTP_REFERER'], '?' ) ) {
  691. $arg_str = substr( $_SERVER['HTTP_REFERER'], $pos + 1 );
  692. $args = wp_parse_args( $arg_str );
  693. if ( ! empty( $args['post'] ) ) {
  694. if ( $_post = get_post( $args['post'] ) ) {
  695. if ( isset( $_post->post_type ) ) {
  696. //rs_errlog( "cr_find_post_type - {$_post->post_type} from http_referer" );
  697. return $_post->post_type;
  698. }
  699. }
  700. }
  701. }
  702. }
  703. if ( empty($object_type) ) {
  704. if ( $return_default ) // default to post type
  705. return 'post';
  706. } elseif ( 'any' != $object_type ) {
  707. return $object_type;
  708. }
  709. }
  710. function cr_find_object_type( $src_name, $object = '' ) {
  711. if ( 'post' == $src_name )
  712. return cr_find_post_type( $object );
  713. global $scoper;
  714. $src = $scoper->data_sources->get( $src_name );
  715. $object_id = ( is_object($object ) ) ? $src->cols->id : $object;
  716. return $scoper->data_sources->detect( 'type', $src_name, $object_id );
  717. }
  718. function cr_get_type_object( $src_name, $object_type ) {
  719. if ( 'post' == $src_name )
  720. return get_post_type_object( $object_type );
  721. global $scoper;
  722. return $scoper->data_sources->member_property( $src_name, 'object_types', $object_type );
  723. }
  724. function scoper_get_object_id($src_name = 'post', $object_type = '') {
  725. global $scoper;
  726. return $scoper->data_sources->detect( 'id', $src_name, 0, $object_type );
  727. }
  728. function is_administrator_rs( $src_or_tx = '', $admin_type = 'content', $user = '' ) {
  729. if ( ! $user ) {
  730. global $current_user;
  731. $user = $current_user;
  732. if ( IS_MU_RS && function_exists('is_super_admin') && is_super_admin() ) {
  733. return true;
  734. }
  735. }
  736. if ( empty($user->ID) )
  737. return false;
  738. $return = '';
  739. $admin_cap_name = scoper_get_administrator_cap( $admin_type );
  740. $return = ! empty( $user->allcaps[$admin_cap_name] );
  741. if ( ! $return && $src_or_tx && ! empty($GLOBALS['scoper']) ) {
  742. if ( ! is_object($src_or_tx) ) {
  743. global $scoper;
  744. if ( ! $src_or_tx = $scoper->data_sources->get( $src_or_tx ) )
  745. $src_or_tx = $scoper->taxonomies->get( $src_or_tx );
  746. }
  747. if ( is_object($src_or_tx) && ! empty($src_or_tx->defining_module) ) {
  748. if ( ! in_array( $src_or_tx->defining_module, array( 'role-scoper', 'wordpress', 'wp' ) ) ) {
  749. // user is not a universal administrator, but are they an administrator for the specified module (plugin-defined data source) ?
  750. static $admin_caps;
  751. if ( ! isset($admin_caps) )
  752. $admin_caps = apply_filters( 'define_administrator_caps_rs', array() );
  753. if ( ! empty( $admin_caps[ $src_or_tx->defining_module ] ) )
  754. $return = ! empty( $user->allcaps[ $admin_caps[ $src_or_tx->defining_module ] ] );
  755. }
  756. }
  757. }
  758. return $return;
  759. }
  760. function is_option_administrator_rs( $user = '' ) {
  761. return is_administrator_rs( '', 'option', $user );
  762. }
  763. function is_user_administrator_rs( $user = '' ) {
  764. return is_administrator_rs( '', 'user', $user );
  765. }
  766. function is_content_administrator_rs( $user = '' ) {
  767. return is_administrator_rs( '', 'content', $user );
  768. }
  769. function scoper_get_administrator_cap( $admin_type ) {
  770. if ( ! $admin_type )
  771. $admin_type = 'content';
  772. // Note: to differentiate content administrator role, define a custom cap such as "administer_all_content", add it to a custom Role, and add the following line to wp-config.php: define( 'SCOPER_CONTENT_ADMIN_CAP', 'cap_name' );
  773. $default_cap = array( 'option' => 'manage_options', 'user' => 'edit_users', 'content' => 'activate_plugins' );
  774. $constant_name = 'SCOPER_' . strtoupper($admin_type) . '_ADMIN_CAP';
  775. $cap_name = ( defined( $constant_name ) ) ? constant( $constant_name ) : $default_cap[$admin_type];
  776. if ( 'read' == $cap_name ) // avoid catostrophic mistakes
  777. $cap_name = $default_cap[$admin_type];
  778. return $cap_name;
  779. }
  780. ?>