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

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

https://github.com/gregoryo/nycga2
PHP | 363 lines | 300 code | 56 blank | 7 comment | 23 complexity | 0a15c0b0a3c72da7b925e342f3293a25 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. // Exit if accessed directly
  3. if ( !defined( 'ABSPATH' ) ) exit;
  4. function bp_forums_add_admin_menu() {
  5. global $bp;
  6. if ( !is_super_admin() )
  7. return false;
  8. // Add the administration tab under the "Site Admin" tab for site administrators
  9. $hook = add_submenu_page( 'bp-general-settings', __( 'Forums', 'buddypress' ), __( 'Forums', 'buddypress' ), 'manage_options', 'bb-forums-setup', "bp_forums_bbpress_admin" );
  10. add_action( "admin_print_styles-$hook", 'bp_core_add_admin_menu_styles' );
  11. }
  12. add_action( bp_core_admin_hook(), 'bp_forums_add_admin_menu' );
  13. function bp_forums_bbpress_admin() {
  14. global $bp;
  15. $action = bp_get_admin_url( 'admin.php?page=bb-forums-setup&reinstall=1' ); ?>
  16. <div class="wrap">
  17. <?php screen_icon( 'buddypress' ); ?>
  18. <h2 class="nav-tab-wrapper"><?php bp_core_admin_tabs( __( 'Forums', 'buddypress' ) ); ?></h2>
  19. <?php if ( isset( $_POST['submit'] ) ) : ?>
  20. <div id="message" class="updated fade">
  21. <p><?php _e( 'Settings Saved.', 'buddypress' ) ?></p>
  22. </div>
  23. <?php endif; ?>
  24. <?php
  25. if ( isset( $_REQUEST['reinstall'] ) || !bp_forums_is_installed_correctly() ) :
  26. // Delete the bb-config.php location option
  27. bp_delete_option( 'bb-config-location' );
  28. bp_forums_bbpress_install_wizard();
  29. else : ?>
  30. <div style="width: 45%; float: left; margin-top: 20px;">
  31. <h3><?php _e( '(Installed)', 'buddypress' ); ?> <?php _e( 'Forums for Groups', 'buddypress' ) ?></h3>
  32. <p><?php _e( 'Give each individual group its own discussion forum. Choose this if you\'d like to keep your members\' conversations separated into distinct areas.' , 'buddypress' ); ?></p>
  33. <p class="description"><?php _e( 'You may use an existing bbPress installation if you have one.', 'buddypress' ); ?></p>
  34. <h4 style="margin-bottom: 10px;"><?php _e( 'Features', 'buddypress' ); ?></h4>
  35. <ul class="description" style="list-style: square; margin-left: 30px;">
  36. <li><?php _e( 'Group Integration', 'buddypress' ); ?></p></li>
  37. <li><?php _e( 'Member Profile Integration', 'buddypress' ); ?></p></li>
  38. <li><?php _e( 'Activity Stream Integration', 'buddypress' ); ?></p></li>
  39. <li><?php _e( '@ Mention Integration', 'buddypress' ); ?></p></li>
  40. </ul>
  41. <div>
  42. <a class="button button-primary" href="<?php echo $action ?>"><?php _e( 'Uninstall Group Forums', 'buddypress' ) ?></a> &nbsp;
  43. </div>
  44. </div>
  45. <div style="width: 45%; float: left; margin: 20px 0 20px 20px; padding: 0 20px 20px 20px; border: 1px solid #ddd; background-color: #fff;">
  46. <h3><?php _e( 'New! Site Wide Forums', 'buddypress' ) ?></h3>
  47. <p><?php _e( 'Your site will have central forums that are not isolated to any specific group. Choose this if you\'d like to have a central forum area for your members.', 'buddypress' ) ?></p>
  48. <p class="description"><?php _e( 'You may activate both Group and Site Wide forums, but this may create a poor experience for your members.', 'buddypress' ) ?></p>
  49. <h4 style="margin-bottom: 10px;"><?php _e( 'Features', 'buddypress' ); ?></h4>
  50. <ul class="description" style="list-style: square; margin-left: 30px;">
  51. <li><?php _e( 'Central Discussion Area', 'buddypress' ); ?></p></li>
  52. <li><?php _e( 'Forum Plugins Available', 'buddypress' ); ?></p></li>
  53. <li><?php _e( 'Activity Stream Integration', 'buddypress' ); ?></p></li>
  54. <li><?php _e( '@ Mention Integration', 'buddypress' ); ?></p></li>
  55. </ul>
  56. <div>
  57. <a class="button thickbox button-primary" href="<?php bp_admin_url( add_query_arg( array( 'tab' => 'plugin-information', 'plugin' => 'bbpress', 'TB_iframe' => 'true', 'width' => '640', 'height' => '500' ), 'plugin-install.php' ) ); ?>"><?php _e( 'Install Site Wide Forums', 'buddypress' ) ?></a> &nbsp;
  58. </div>
  59. </div>
  60. <?php endif; ?>
  61. </div>
  62. <?php
  63. }
  64. function bp_forums_bbpress_install_wizard() {
  65. $post_url = network_admin_url( 'admin.php?page=bb-forums-setup' );
  66. $step = isset( $_REQUEST['step'] ) ? $_REQUEST['step'] : '';
  67. switch( $step ) {
  68. case 'existing':
  69. if ( isset( $_REQUEST['doinstall'] ) && ( 1 == (int) $_REQUEST['doinstall'] ) ) {
  70. if ( !bp_forums_configure_existing_install() ) {
  71. _e( 'The bb-config.php file was not found at that location, please try again.', 'buddypress' );
  72. } else {
  73. ?>
  74. <h3><?php _e( 'Forums were set up correctly using your existing bbPress install!', 'buddypress' ) ?></h3>
  75. <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
  76. }
  77. } else { ?>
  78. <form action="" method="post">
  79. <h3><?php _e( 'Existing bbPress Installation', 'buddypress' ) ?></h3>
  80. <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>
  81. <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>
  82. <p><input type="submit" class="button-primary" value="<?php _e( 'Complete Installation', 'buddypress' ) ?>" /></p>
  83. <input type="hidden" name="step" value="existing" />
  84. <input type="hidden" name="doinstall" value="1" />
  85. <?php wp_nonce_field( 'bp_forums_existing_install_init' ) ?>
  86. </form>
  87. <?php
  88. }
  89. break;
  90. case 'new':
  91. if ( isset( $_REQUEST['doinstall'] ) && 1 == (int)$_REQUEST['doinstall'] ) {
  92. $result = bp_forums_bbpress_install();
  93. switch ( $result ) {
  94. case 1:
  95. _e( '<p>All done! Configuration settings have been saved to the file <code>bb-config.php</code> in the root of your WordPress install.</p>', 'buddypress' );
  96. break;
  97. default:
  98. // Just write the contents to screen
  99. _e( '<p>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.</p>', 'buddypress' ); ?>
  100. <textarea style="display:block; margin-top: 30px; width: 80%;" rows="50"><?php echo htmlspecialchars( $result ); ?></textarea>
  101. <?php
  102. break;
  103. }
  104. } else { ?>
  105. <h3><?php _e( 'New bbPress Installation', 'buddypress' ) ?></h3>
  106. <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
  107. process. When you're ready, hit the link below.", 'buddypress' ) ?></p>
  108. <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>
  109. <?php
  110. }
  111. break;
  112. default:
  113. if ( !file_exists( BP_PLUGIN_DIR . '/bp-forums/bbpress/' ) ) { ?>
  114. <div id="message" class="error">
  115. <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>
  116. </div>
  117. <?php } else {
  118. // Include the plugin install
  119. add_thickbox();
  120. wp_enqueue_script( 'plugin-install' );
  121. wp_admin_css( 'plugin-install' );
  122. ?>
  123. <div style="width: 45%; float: left; margin-top: 20px;">
  124. <h3><?php _e( 'Forums for Groups', 'buddypress' ) ?></h3>
  125. <p><?php _e( 'Give each individual group its own discussion forum. Choose this if you\'d like to keep your members\' conversations separated into distinct areas.' , 'buddypress' ); ?></p>
  126. <p class="description"><?php _e( 'You may use an existing bbPress installation if you have one.', 'buddypress' ); ?></p>
  127. <h4 style="margin-bottom: 10px;"><?php _e( 'Features', 'buddypress' ); ?></h4>
  128. <ul class="description" style="list-style: square; margin-left: 30px;">
  129. <li><?php _e( 'Group Integration', 'buddypress' ); ?></p></li>
  130. <li><?php _e( 'Member Profile Integration', 'buddypress' ); ?></p></li>
  131. <li><?php _e( 'Activity Stream Integration', 'buddypress' ); ?></p></li>
  132. <li><?php _e( '@ Mention Integration', 'buddypress' ); ?></p></li>
  133. </ul>
  134. <div>
  135. <a class="button button-primary" href="<?php echo $post_url . '&step=new' ?>"><?php _e( 'Install Group Forums', 'buddypress' ) ?></a> &nbsp;
  136. <a class="button" href="<?php echo $post_url . '&step=existing' ?>"><?php _e( 'Use Existing Installation', 'buddypress' ) ?></a>
  137. </div>
  138. </div>
  139. <div style="width: 45%; float: left; margin: 20px 0 20px 20px; padding: 0 20px 20px 20px; border: 1px solid #ddd; background-color: #fff;">
  140. <h3><?php _e( 'New! Site Wide Forums', 'buddypress' ) ?></h3>
  141. <p><?php _e( 'Your site will have central forums that are not isolated to any specific group. Choose this if you\'d like to have a central forum area for your members.', 'buddypress' ) ?></p>
  142. <p class="description"><?php _e( 'You may activate both Group and Site Wide forums, but this may create a poor experience for your members.', 'buddypress' ) ?></p>
  143. <h4 style="margin-bottom: 10px;"><?php _e( 'Features', 'buddypress' ); ?></h4>
  144. <ul class="description" style="list-style: square; margin-left: 30px;">
  145. <li><?php _e( 'Central Discussion Area', 'buddypress' ); ?></p></li>
  146. <li><?php _e( 'Forum Plugins Available', 'buddypress' ); ?></p></li>
  147. <li><?php _e( 'Activity Stream Integration', 'buddypress' ); ?></p></li>
  148. <li><?php _e( '@ Mention Integration', 'buddypress' ); ?></p></li>
  149. </ul>
  150. <div>
  151. <a class="button thickbox button-primary" href="<?php bp_admin_url( add_query_arg( array( 'tab' => 'plugin-information', 'plugin' => 'bbpress', 'TB_iframe' => 'true', 'width' => '640', 'height' => '500' ), 'plugin-install.php' ) ); ?>"><?php _e( 'Install Site Wide Forums', 'buddypress' ) ?></a> &nbsp;
  152. </div>
  153. </div>
  154. <?php }
  155. break;
  156. }
  157. }
  158. function bp_forums_configure_existing_install() {
  159. global $wpdb, $bbdb;
  160. check_admin_referer( 'bp_forums_existing_install_init' );
  161. // Sanitize $_REQUEST['bbconfigloc']
  162. $_REQUEST['bbconfigloc'] = apply_filters( 'bp_forums_bbconfig_location', $_REQUEST['bbconfigloc'] );
  163. if ( false === strpos( $_REQUEST['bbconfigloc'], 'bb-config.php' ) ) {
  164. if ( '/' != substr( $_REQUEST['bbconfigloc'], -1, 1 ) )
  165. $_REQUEST['bbconfigloc'] .= '/';
  166. $_REQUEST['bbconfigloc'] .= 'bb-config.php';
  167. }
  168. bp_update_option( 'bb-config-location', $_REQUEST['bbconfigloc'] );
  169. if ( !file_exists( $_REQUEST['bbconfigloc'] ) )
  170. return false;
  171. return true;
  172. }
  173. function bp_forums_bbpress_install() {
  174. global $wpdb, $bbdb, $bp;
  175. check_admin_referer( 'bp_forums_new_install_init' );
  176. // Create the bb-config.php file
  177. $initial_write = bp_forums_bbpress_write(
  178. BP_PLUGIN_DIR . '/bp-forums/bbpress/bb-config-sample.php',
  179. ABSPATH . 'bb-config.php',
  180. array(
  181. "define( 'BBDB_NAME'," => array( "'bbpress'", "'" . DB_NAME . "'" ),
  182. "define( 'BBDB_USER'," => array( "'username'", "'" . DB_USER . "'" ),
  183. "define( 'BBDB_PASSWO" => array( "'password'", "'" . DB_PASSWORD . "'" ),
  184. "define( 'BBDB_HOST'," => array( "'localhost'", "'" . DB_HOST . "'" ),
  185. "define( 'BBDB_CHARSE" => array( "'utf8'", "'" . DB_CHARSET . "'" ),
  186. "define( 'BBDB_COLLAT" => array( "''", "'" . DB_COLLATE . "'" ),
  187. "define( 'BB_AUTH_KEY" => array( "'put your unique phrase here'", "'" . addslashes( AUTH_KEY ) . "'" ),
  188. "define( 'BB_SECURE_A" => array( "'put your unique phrase here'", "'" . addslashes( SECURE_AUTH_KEY ) . "'" ),
  189. "define( 'BB_LOGGED_I" => array( "'put your unique phrase here'", "'" . addslashes( LOGGED_IN_KEY ) . "'" ),
  190. "define( 'BB_NONCE_KE" => array( "'put your unique phrase here'", "'" . addslashes( NONCE_KEY ) . "'" ),
  191. "\$bb_table_prefix = '" => array( "'bb_'", "'" . $bp->table_prefix . "bb_'" ),
  192. "define( 'BB_LANG', '" => array( "''", "'" . get_locale() . "'" )
  193. )
  194. );
  195. // Add the custom user and usermeta entries to the config file
  196. if ( $initial_write == 1 )
  197. $file = file_get_contents( ABSPATH . 'bb-config.php' );
  198. else
  199. $file = &$initial_write;
  200. $file = trim( $file );
  201. if ( '?>' == substr( $file, -2, 2 ) )
  202. $file = substr( $file, 0, -2 );
  203. $file .= "\n" . '$bb->custom_user_table = \'' . $wpdb->users . '\';';
  204. $file .= "\n" . '$bb->custom_user_meta_table = \'' . $wpdb->usermeta . '\';';
  205. $file .= "\n\n" . '$bb->uri = \'' . BP_PLUGIN_URL . '/bp-forums/bbpress/\';';
  206. $file .= "\n" . '$bb->name = \'' . get_blog_option( bp_get_root_blog_id(), 'blogname' ) . ' ' . __( 'Forums', 'buddypress' ) . '\';';
  207. if ( is_multisite() )
  208. $file .= "\n" . '$bb->wordpress_mu_primary_blog_id = ' . bp_get_root_blog_id() . ';';
  209. if ( defined( 'AUTH_SALT' ) )
  210. $file .= "\n\n" . 'define(\'BB_AUTH_SALT\', \'' . addslashes( AUTH_SALT ) . '\');';
  211. if ( defined( 'LOGGED_IN_SALT' ) )
  212. $file .= "\n" . 'define(\'BB_LOGGED_IN_SALT\', \'' . addslashes( LOGGED_IN_SALT ) . '\');';
  213. if ( defined( 'SECURE_AUTH_SALT' ) )
  214. $file .= "\n" . 'define(\'BB_SECURE_AUTH_SALT\', \'' . addslashes( SECURE_AUTH_SALT ) . '\');';
  215. $file .= "\n\n" . 'define(\'WP_AUTH_COOKIE_VERSION\', 2);';
  216. $file .= "\n\n" . '?>';
  217. if ( $initial_write == 1 ) {
  218. $file_handle = fopen( ABSPATH . 'bb-config.php', 'w' );
  219. fwrite( $file_handle, $file );
  220. fclose( $file_handle );
  221. } else {
  222. $initial_write = $file;
  223. }
  224. bp_update_option( 'bb-config-location', ABSPATH . 'bb-config.php' );
  225. return $initial_write;
  226. }
  227. function bp_forums_bbpress_write( $file_source, $file_target, $alterations ) {
  228. if ( !$file_source || !file_exists( $file_source ) || !is_file( $file_source ) )
  229. return -1;
  230. if ( !$file_target )
  231. $file_target = $file_source;
  232. if ( !$alterations || !is_array( $alterations ) )
  233. return -2;
  234. // Get the existing lines in the file
  235. $lines = file( $file_source );
  236. // Initialise an array to store the modified lines
  237. $modified_lines = array();
  238. // Loop through the lines and modify them
  239. foreach ( (array)$lines as $line ) {
  240. if ( isset( $alterations[substr( $line, 0, 20 )] ) ) {
  241. $alteration = $alterations[substr( $line, 0, 20 )];
  242. $modified_lines[] = str_replace( $alteration[0], $alteration[1], $line );
  243. } else {
  244. $modified_lines[] = $line;
  245. }
  246. }
  247. $writable = true;
  248. if ( file_exists( $file_target ) ) {
  249. if ( !is_writable( $file_target ) ) {
  250. $writable = false;
  251. }
  252. } else {
  253. $dir_target = dirname( $file_target );
  254. if ( file_exists( $dir_target ) ) {
  255. if ( !is_writable( $dir_target ) || !is_dir( $dir_target ) ) {
  256. $writable = false;
  257. }
  258. } else {
  259. $writable = false;
  260. }
  261. }
  262. if ( empty( $writable ) )
  263. return trim( join( null, $modified_lines ) );
  264. // Open the file for writing - rewrites the whole file
  265. $file_handle = fopen( $file_target, 'w' );
  266. // Write lines one by one to avoid OS specific newline hassles
  267. foreach ( (array)$modified_lines as $modified_line ) {
  268. if ( false !== strpos( $modified_line, '?>' ) ) {
  269. $modified_line = '?>';
  270. }
  271. fwrite( $file_handle, $modified_line );
  272. if ( $modified_line == '?>' ) {
  273. break;
  274. }
  275. }
  276. // Close the config file
  277. fclose( $file_handle );
  278. @chmod( $file_target, 0666 );
  279. return 1;
  280. }
  281. ?>