PageRenderTime 24ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/buddypress/bp-forums/bbpress/bb-admin/options-permalinks.php

https://bitbucket.org/Red54/dianjihun
PHP | 188 lines | 154 code | 30 blank | 4 comment | 31 complexity | 681e2d01a6b09c593f4ca1555c61e252 MD5 | raw file
  1. <?php
  2. require_once('admin.php');
  3. $file_source = BB_PATH . 'bb-admin/includes/defaults.bb-htaccess.php';
  4. $file_target = BB_PATH . '.htaccess';
  5. include( $file_source );
  6. $file_source_rules = $_rules; // This is a string
  7. if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) && $_POST['action'] == 'update') {
  8. bb_check_admin_referer( 'options-permalinks-update' );
  9. foreach ( (array) $_POST as $option => $value ) {
  10. if ( !in_array( $option, array('_wpnonce', '_wp_http_referer', 'action', 'submit') ) ) {
  11. $option = trim( $option );
  12. $value = is_array( $value ) ? $value : trim( $value );
  13. $value = stripslashes_deep( $value );
  14. if ( $value ) {
  15. bb_update_option( $option, $value );
  16. } else {
  17. bb_delete_option( $option );
  18. }
  19. }
  20. }
  21. $mod_rewrite = (string) bb_get_option( 'mod_rewrite' );
  22. $goback = remove_query_arg( array( 'updated', 'notapache', 'notmodrewrite' ), wp_get_referer() );
  23. // Make sure mod_rewrite is possible on the server
  24. if ( !$is_apache ) {
  25. bb_delete_option( 'mod_rewrite_writable' );
  26. $goback = add_query_arg( 'notapache', 'true', $goback );
  27. bb_safe_redirect( $goback );
  28. exit;
  29. } elseif ( '0' !== $mod_rewrite && !apache_mod_loaded( 'mod_rewrite', true ) ) {
  30. bb_delete_option( 'mod_rewrite_writable' );
  31. bb_update_option( 'mod_rewrite', '0' );
  32. $goback = add_query_arg( 'notmodrewrite', 'true', $goback );
  33. bb_safe_redirect( $goback );
  34. exit;
  35. }
  36. $file_target_rules = array();
  37. $file_target_exists = false;
  38. $file_target_writable = true;
  39. if ( file_exists( $file_target ) ) {
  40. if ( is_readable( $file_target ) ) {
  41. $file_target_rules = explode( "\n", implode( '', file( $file_target ) ) );
  42. }
  43. $file_target_exists = true;
  44. if ( !is_writable( $file_target ) ) {
  45. $file_target_writable = false;
  46. }
  47. } else {
  48. $file_target_dir = dirname( $file_target );
  49. if ( file_exists( $file_target_dir ) ) {
  50. if ( !is_writable( $file_target_dir ) || !is_dir( $file_target_dir ) ) {
  51. $file_target_writable = false;
  52. }
  53. } else {
  54. $file_target_writable = false;
  55. }
  56. }
  57. // Strip out existing bbPress rules
  58. $_keep_rule = true;
  59. $_kept_rules = array();
  60. foreach ( $file_target_rules as $_rule ) {
  61. if ( false !== strpos( $_rule, '# BEGIN bbPress' ) ) {
  62. $_keep_rule = false;
  63. continue;
  64. } elseif ( false !== strpos( $_rule, '# END bbPress' ) ) {
  65. $_keep_rule = true;
  66. continue;
  67. }
  68. if ( $_keep_rule ) {
  69. $_kept_rules[] = $_rule;
  70. }
  71. }
  72. $file_target_rules = join( "\n", $_kept_rules ) . "\n" . $file_source_rules;
  73. $file_target_written = 0;
  74. if ( $file_target_writable ) {
  75. // Open the file for writing - rewrites the whole file
  76. if ( $file_target_handle = fopen( $file_target, 'w' ) ) {
  77. if ( fwrite( $file_target_handle, $file_target_rules ) ) {
  78. $file_target_written = 1;
  79. }
  80. // Close the file
  81. fclose( $file_target_handle );
  82. @chmod( $file_target, 0666 );
  83. }
  84. }
  85. bb_update_option( 'mod_rewrite_writable', $file_target_writable );
  86. $goback = add_query_arg( 'updated', 'true', $goback );
  87. bb_safe_redirect( $goback );
  88. exit;
  89. }
  90. if ( $is_apache && bb_get_option( 'mod_rewrite' ) && !bb_get_option( 'mod_rewrite_writable' ) ) {
  91. $manual_instructions = true;
  92. }
  93. if ( !empty( $_GET['notmodrewrite'] ) ) {
  94. $manual_instructions = false;
  95. bb_admin_notice( __( '<strong>It appears that your server does not support custom permalink structures.</strong>' ), 'error' );
  96. }
  97. if ( !empty( $_GET['notapache'] ) ) {
  98. $manual_instructions = false;
  99. bb_admin_notice( __( '<strong>Rewriting on webservers other than Apache using mod_rewrite is currently unsupported, but we won&#8217;t stop you from trying.</strong>' ), 'error' );
  100. }
  101. if ( !empty( $_GET['updated'] ) ) {
  102. if ( $manual_instructions ) {
  103. bb_admin_notice( __( '<strong>You should update your .htaccess now.</strong>' ) );
  104. } else {
  105. bb_admin_notice( __( '<strong>Permalink structure updated.</strong>' ) );
  106. }
  107. }
  108. $permalink_options = array(
  109. 'mod_rewrite' => array(
  110. 'title' => __( 'Permalink type' ),
  111. 'type' => 'radio',
  112. 'options' => array(
  113. '0' => sprintf( __( '<span>None</span> <code>%s</code>' ), bb_get_uri( 'forum.php', array( 'id' => 1 ), BB_URI_CONTEXT_TEXT ) ),
  114. '1' => sprintf( __( '<span>Numeric</span> <code>%s</code>' ), bb_get_uri( 'forum/1', null, BB_URI_CONTEXT_TEXT ) ),
  115. 'slugs' => sprintf( __( '<span>Name based</span> <code>%s</code>' ), bb_get_uri( '/forum/first-forum', null, BB_URI_CONTEXT_TEXT ) )
  116. )
  117. )
  118. );
  119. $bb_admin_body_class = ' bb-admin-settings';
  120. bb_get_admin_header();
  121. ?>
  122. <div class="wrap">
  123. <h2><?php _e( 'Permalink Settings' ); ?></h2>
  124. <?php do_action( 'bb_admin_notices' ); ?>
  125. <form class="settings" method="post" action="<?php bb_uri( 'bb-admin/options-permalinks.php', null, BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN ); ?>">
  126. <fieldset>
  127. <p>
  128. <?php _e( 'By default bbPress uses web URLs which have question marks and lots of numbers in them, however bbPress offers you the ability to choose an alternative URL structure for your permalinks. This can improve the aesthetics, usability, and forward-compatibility of your links.' ); ?>
  129. </p>
  130. <?php
  131. foreach ( $permalink_options as $option => $args ) {
  132. bb_option_form_element( $option, $args );
  133. }
  134. ?>
  135. </fieldset>
  136. <fieldset class="submit">
  137. <?php bb_nonce_field( 'options-permalinks-update' ); ?>
  138. <input type="hidden" name="action" value="update" />
  139. <input class="submit" type="submit" name="submit" value="<?php _e('Save Changes') ?>" />
  140. </fieldset>
  141. </form>
  142. <?php
  143. if ( $manual_instructions ) {
  144. ?>
  145. <form class="settings" method="post" action="<?php bb_uri( 'bb-admin/options-permalinks.php', null, BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN ); ?>">
  146. <fieldset>
  147. <p>
  148. <?php _e( 'If your <code>.htaccess</code> file were <a href="http://codex.wordpress.org/Changing_File_Permissions">writable</a>, we could do this automatically, but it isn&#8217;t so these are the mod_rewrite rules you should have in your <code>.htaccess</code> file. Click in the field and press <kbd>CTRL + a</kbd> to select all.' ); ?>
  149. </p>
  150. <textarea dir="ltr" id="rewrite-rules" class="readonly" readonly="readonly" rows="6"><?php echo esc_html( trim( $file_source_rules ) ); ?></textarea>
  151. </fieldset>
  152. </form>
  153. <?php
  154. }
  155. ?>
  156. </div>
  157. <?php
  158. bb_get_admin_footer();