PageRenderTime 82ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/buddypress-old/bp-forums/bp-forums-admin.php

https://github.com/pfond/PFOND
PHP | 259 lines | 231 code | 25 blank | 3 comment | 19 complexity | 26617fdc095887f57a1b09de859e7e0a MD5 | raw file
  1. <?php
  2. function bp_forums_bbpress_admin() {
  3. global $bp;
  4. ?>
  5. <div class="wrap">
  6. <h2><?php _e( 'Forums Setup', 'buddypress' ) ?></h2>
  7. <?php if ( isset( $_POST['submit'] ) ) : ?>
  8. <div id="message" class="updated fade">
  9. <p><?php _e( 'Settings Saved.', 'buddypress' ) ?></p>
  10. </div>
  11. <?php endif;
  12. if ( isset( $_REQUEST['reinstall'] ) || !bp_forums_is_installed_correctly() ) {
  13. update_site_option( 'bb-config-location', false );
  14. bp_forums_bbpress_install_wizard();
  15. } else {
  16. $reinstall = network_admin_url( 'admin.php?page=bb-forums-setup&reinstall=1' ); ?>
  17. <p><?php printf( __( 'bbPress forum integration in BuddyPress has been set up correctly. If you are having problems you can <a href="%s" title="Reinstall bbPress">re-install</a>', 'buddypress' ), $reinstall ); ?>
  18. <p><?php _e( 'NOTE: The forums directory will only work if your bbPress tables are in the same database as your WordPress tables. If you are not using an existing bbPress install you can ignore this message.', 'buddypress' ) ?></p>
  19. <?php
  20. }
  21. ?>
  22. </div>
  23. <?php
  24. }
  25. function bp_forums_bbpress_install_wizard() {
  26. $post_url = network_admin_url( 'admin.php?page=bb-forums-setup' );
  27. switch( $_REQUEST['step'] ) {
  28. case 'existing':
  29. if ( 1 == (int)$_REQUEST['doinstall'] ) {
  30. if ( !bp_forums_configure_existing_install() ) {
  31. _e( 'The bb-config.php file was not found at that location, please try again.', 'buddypress' );
  32. } else {
  33. ?>
  34. <h3><?php _e( 'Forums were set up correctly using your existing bbPress install!', 'buddypress' ) ?></h3>
  35. <p><?php _e( 'BuddyPress will now use its internal copy of bbPress to run the forums on your site. If you wish, you can remove your old bbPress installation files, as long as you keep the bb-config.php file in the same location.', 'buddypress' ) ?></p><?php
  36. }
  37. } else {
  38. ?>
  39. <form action="" method="post">
  40. <h3><?php _e( 'Existing bbPress Installation', 'buddypress' ) ?></h3>
  41. <p><?php _e( "BuddyPress can make use of your existing bbPress install. Just provide the location of your <code>bb-config.php</code> file, and BuddyPress will do the rest.", 'buddypress' ) ?></p>
  42. <p><label><code>bb-config.php</code> file location:</label><br /><input style="width: 50%" type="text" name="bbconfigloc" id="bbconfigloc" value="<?php echo str_replace( 'buddypress', '', $_SERVER['DOCUMENT_ROOT'] ) ?>" /></p>
  43. <p><input type="submit" class="button-primary" value="<?php _e( 'Complete Installation', 'buddypress' ) ?>" /></p>
  44. <input type="hidden" name="step" value="existing" />
  45. <input type="hidden" name="doinstall" value="1" />
  46. <?php wp_nonce_field( 'bp_forums_existing_install_init' ) ?>
  47. </form>
  48. <?php
  49. }
  50. break;
  51. case 'new':
  52. if ( 1 == (int)$_REQUEST['doinstall'] ) {
  53. $result = bp_forums_bbpress_install();
  54. switch ( $result ) {
  55. case 1:
  56. _e( 'All done! Configuration settings have been saved to the file <code>bb-config.php</code> in the root of your WordPress install.', 'buddypress' );
  57. break;
  58. default:
  59. // Just write the contents to screen
  60. _e( 'A configuration file could not be created. No problem, but you will need to save the text shown below into a file named <code>bb-config.php</code> in the root directory of your WordPress installation before you can start using the forum functionality.', 'buddypress' );
  61. ?><br /><textarea cols="110" rows="50" style="margin: 10px 0 50px 0;"><?php echo htmlspecialchars( $result ) ?></textarea><?php
  62. break;
  63. }
  64. } else {
  65. ?>
  66. <h3><?php _e( 'New bbPress Installation', 'buddypress' ) ?></h3>
  67. <p><?php _e( "You've decided to set up a new installation of bbPress for forum management in BuddyPress. This is very simple and is usually just a one click
  68. process. When you're ready, hit the link below.", 'buddypress' ) ?></p>
  69. <p><a class="button-primary" href="<?php echo wp_nonce_url( $post_url . '&step=new&doinstall=1', 'bp_forums_new_install_init' ) ?>"><?php _e( 'Complete Installation', 'buddypress' ) ?></a></p>
  70. <?php
  71. }
  72. break;
  73. default:
  74. if ( !file_exists( BP_PLUGIN_DIR . '/bp-forums/bbpress/' ) ) { ?>
  75. <div id="message" class="error">
  76. <p><?php printf( __( 'bbPress files were not found. To install the forums component you must download a copy of bbPress and make sure it is in the folder: "%s"', 'buddypress' ), 'wp-content/plugins/buddypress/bp-forums/bbpress/' ) ?></p>
  77. </div>
  78. <?php } else { ?>
  79. <p><?php _e( 'Forums in BuddyPress make use of a bbPress installation to function. You can choose to either let BuddyPress set up a new bbPress install, or use an already existing bbPress install. Please choose one of the options below.', 'buddypress' ) ?></p>
  80. <a class="button" href="<?php echo $post_url . '&step=new' ?>"><?php _e( 'Set up a new bbPress installation', 'buddypress' ) ?></a> &nbsp;
  81. <a class="button" href="<?php echo $post_url . '&step=existing' ?>"><?php _e( 'Use an existing bbPress installation', 'buddypress' ) ?></a>
  82. <?php }
  83. break;
  84. }
  85. }
  86. function bp_forums_configure_existing_install() {
  87. global $wpdb, $bbdb;
  88. check_admin_referer( 'bp_forums_existing_install_init' );
  89. /* Sanitize $_REQUEST['bbconfigloc'] */
  90. $_REQUEST['bbconfigloc'] = apply_filters( 'bp_forums_bbconfig_location', $_REQUEST['bbconfigloc'] );
  91. if ( false === strpos( $_REQUEST['bbconfigloc'], 'bb-config.php' ) ) {
  92. if ( '/' != substr( $_REQUEST['bbconfigloc'], -1, 1 ) )
  93. $_REQUEST['bbconfigloc'] .= '/';
  94. $_REQUEST['bbconfigloc'] .= 'bb-config.php';
  95. }
  96. if ( !file_exists( $_REQUEST['bbconfigloc'] ) )
  97. return false;
  98. update_site_option( 'bb-config-location', $_REQUEST['bbconfigloc'] );
  99. return true;
  100. }
  101. function bp_forums_bbpress_install() {
  102. global $wpdb, $bbdb, $bp;
  103. check_admin_referer( 'bp_forums_new_install_init' );
  104. /* Create the bb-config.php file */
  105. $initial_write = bp_forums_bbpress_write(
  106. BP_PLUGIN_DIR . '/bp-forums/bbpress/bb-config-sample.php',
  107. ABSPATH . 'bb-config.php',
  108. array(
  109. "define( 'BBDB_NAME'," => array( "'bbpress'", "'" . DB_NAME . "'" ),
  110. "define( 'BBDB_USER'," => array( "'username'", "'" . DB_USER . "'" ),
  111. "define( 'BBDB_PASSWO" => array( "'password'", "'" . DB_PASSWORD . "'" ),
  112. "define( 'BBDB_HOST'," => array( "'localhost'", "'" . DB_HOST . "'" ),
  113. "define( 'BBDB_CHARSE" => array( "'utf8'", "'" . DB_CHARSET . "'" ),
  114. "define( 'BBDB_COLLAT" => array( "''", "'" . DB_COLLATE . "'" ),
  115. "define( 'BB_AUTH_KEY" => array( "'put your unique phrase here'", "'" . addslashes( AUTH_KEY ) . "'" ),
  116. "define( 'BB_SECURE_A" => array( "'put your unique phrase here'", "'" . addslashes( SECURE_AUTH_KEY ) . "'" ),
  117. "define( 'BB_LOGGED_I" => array( "'put your unique phrase here'", "'" . addslashes( LOGGED_IN_KEY ) . "'" ),
  118. "define( 'BB_NONCE_KE" => array( "'put your unique phrase here'", "'" . addslashes( NONCE_KEY ) . "'" ),
  119. "\$bb_table_prefix = '" => array( "'bb_'", "'" . $bp->table_prefix . "bb_'" ),
  120. "define( 'BB_LANG', '" => array( "''", "'" . WPLANG . "'" )
  121. )
  122. );
  123. /* Add the custom user and usermeta entries to the config file */
  124. if ( $initial_write == 1 ) {
  125. $file = file_get_contents( ABSPATH . 'bb-config.php' );
  126. } else {
  127. $file = &$initial_write;
  128. }
  129. $file = substr( $file, 0, -2 );
  130. $file .= "\n" . '$bb->custom_user_table = \'' . $wpdb->users . '\';';
  131. $file .= "\n" . '$bb->custom_user_meta_table = \'' . $wpdb->usermeta . '\';';
  132. $file .= "\n\n" . '$bb->uri = \'' . BP_PLUGIN_URL . '/bp-forums/bbpress/\';';
  133. $file .= "\n" . '$bb->name = \'' . get_blog_option( BP_ROOT_BLOG, 'name' ) . ' ' . __( 'Forums', 'buddypress' ) . '\';';
  134. if ( bp_core_is_multisite() )
  135. $file .= "\n" . '$bb->wordpress_mu_primary_blog_id = ' . BP_ROOT_BLOG . ';';
  136. if ( defined( 'AUTH_SALT' ) )
  137. $file .= "\n\n" . 'define(\'BB_AUTH_SALT\', \'' . addslashes( AUTH_SALT ) . '\');';
  138. if ( defined( 'LOGGED_IN_SALT' ) )
  139. $file .= "\n" . 'define(\'BB_LOGGED_IN_SALT\', \'' . addslashes( LOGGED_IN_SALT ) . '\');';
  140. if ( defined( 'SECURE_AUTH_SALT' ) )
  141. $file .= "\n" . 'define(\'BB_SECURE_AUTH_SALT\', \'' . addslashes( SECURE_AUTH_SALT ) . '\');';
  142. $file .= "\n\n" . 'define(\'WP_AUTH_COOKIE_VERSION\', 2);';
  143. $file .= "\n\n" . '?>';
  144. if ( $initial_write == 1 ) {
  145. $file_handle = fopen( ABSPATH . 'bb-config.php', 'w' );
  146. fwrite( $file_handle, $file );
  147. fclose( $file_handle );
  148. } else {
  149. $initial_write = $file;
  150. }
  151. update_site_option( 'bb-config-location', ABSPATH . 'bb-config.php' );
  152. return $initial_write;
  153. }
  154. function bp_forums_bbpress_write( $file_source, $file_target, $alterations ) {
  155. if ( !$file_source || !file_exists( $file_source ) || !is_file( $file_source ) ) {
  156. return -1;
  157. }
  158. if ( !$file_target ) {
  159. $file_target = $file_source;
  160. }
  161. if ( !$alterations || !is_array( $alterations ) ) {
  162. return -2;
  163. }
  164. // Get the existing lines in the file
  165. $lines = file( $file_source );
  166. // Initialise an array to store the modified lines
  167. $modified_lines = array();
  168. // Loop through the lines and modify them
  169. foreach ( (array)$lines as $line ) {
  170. if ( isset( $alterations[substr( $line, 0, 20 )] ) ) {
  171. $alteration = $alterations[substr( $line, 0, 20 )];
  172. $modified_lines[] = str_replace( $alteration[0], $alteration[1], $line );
  173. } else {
  174. $modified_lines[] = $line;
  175. }
  176. }
  177. $writable = true;
  178. if ( file_exists( $file_target ) ) {
  179. if ( !is_writable( $file_target ) ) {
  180. $writable = false;
  181. }
  182. } else {
  183. $dir_target = dirname( $file_target );
  184. if ( file_exists( $dir_target ) ) {
  185. if ( !is_writable( $dir_target ) || !is_dir( $dir_target ) ) {
  186. $writable = false;
  187. }
  188. } else {
  189. $writable = false;
  190. }
  191. }
  192. if ( !$writable )
  193. return trim( join( null, $modified_lines ) );
  194. // Open the file for writing - rewrites the whole file
  195. $file_handle = fopen( $file_target, 'w' );
  196. // Write lines one by one to avoid OS specific newline hassles
  197. foreach ( (array)$modified_lines as $modified_line ) {
  198. if ( false !== strpos( $modified_line, '?>' ) ) {
  199. $modified_line = '?>';
  200. }
  201. fwrite( $file_handle, $modified_line );
  202. if ( $modified_line == '?>' ) {
  203. break;
  204. }
  205. }
  206. // Close the config file
  207. fclose( $file_handle );
  208. @chmod( $file_target, 0666 );
  209. return 1;
  210. }
  211. ?>