PageRenderTime 362ms CodeModel.GetById 32ms RepoModel.GetById 2ms app.codeStats 0ms

/role-scoper/role-scoper_init.php

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