%1$s backup could not be generated: probably there is no %2$s in bbPress root folder or it is not writable.', NICER_PERMALINKS_ID ), '.htaccess', 'htaccess.bak' ), 'error' ); return; } if ( false === file_put_contents( BB_PATH . '.htaccess', get_nicer_htaccess() ) ) { // Generate error notice and return if errors occurred $notices[] = array( sprintf( __( '%s could not be updated: probably it is not writable.', NICER_PERMALINKS_ID ), '.htaccess' ), 'error' ); return; } // Save plugin status in db bb_update_option( 'nicer_permalinks_enabled', 1 ); } else { // Try to disable the plugin if ( false === restore_htaccess() ) { // Generate error notice and return if errors occurred $notices[] = array( sprintf( __( '%s could not be restored: probably it is not writable.', NICER_PERMALINKS_ID ), '.htaccess' ), 'error' ); return; } // Save plugin status in db bb_update_option( 'nicer_permalinks_enabled', 0 ); } } else ; // No need to process request if requested status matches plugin current status // Generate notice $notices[] = array( sprintf( __( '%s settings updated.', NICER_PERMALINKS_ID ), NICER_PERMALINKS_NAME ), '' ); } } /** * Returns plugin configuration errors or false if no error is found * * @param int $status Plugin status * * @uses nicer_permalinks_enabled() * @uses php_version_check() * @uses name_based_permalinks_enabled() * @uses bb_get_uri() * * @return array|boolean Errors|false */ function nicer_permalinks_config_errors( $status ) { if ( is_null( $status ) ) // Retrieve plugin status if no param is passed $status = nicer_permalinks_enabled(); $errors = array(); if ( $status ) { // Check if plugin can be disabled if ( !is_writable( BB_PATH . '.htaccess' ) ) // Generate error if .htaccess is not writable $errors[] = sprintf( __( '%s is not writable.', NICER_PERMALINKS_ID ), '.htaccess' ); } else { // Check if plugin can be enabled if ( version_compare( '5.0.0', PHP_VERSION, '>' ) ) // Generate error if PHP version if lower than 5 $errors[] = sprintf( __( 'Your Webserver is running PHP version %1$s, which is no longer receiving security updates. Please contact your host and have them upgrade PHP to its latest stable branch as soon as possible.', NICER_PERMALINKS_ID ), PHP_VERSION, 'http://www.php.net/archive/2007.php#2007-07-13-1' ); if ( !name_based_permalinks_enabled() ) // Generate error if name based permalinks are disabled $errors[] = sprintf( __( 'Name based permalinks are not enabled.', NICER_PERMALINKS_ID ), bb_get_uri( 'bb-admin/options-permalinks.php' ) ); if ( !is_writable( BB_PATH . '.htaccess' ) ) // Generate error if .htaccess is not writable $errors[] = sprintf( __( '%s is not writable.', NICER_PERMALINKS_ID ), '.htaccess' ); if ( !file_exists( BB_PATH . 'htaccess.bak' ) ) // Generate error if there is no htaccess.bak in bbPress root folder $errors[] = sprintf( __( 'There is no %s in bbPress root folder.', NICER_PERMALINKS_ID ), 'htaccess.bak' ); else if ( !is_writable( BB_PATH . 'htaccess.bak' ) ) // Generate error if htaccess.bak is not writable $errors[] = sprintf( __( '%s is not writable.', NICER_PERMALINKS_ID ), 'htaccess.bak' ); } // Return array of errors or false if no error occurred return ( count( $errors ) ) ? $errors : false; } /** * Check if name based permalinks are enabled * * @uses bb_get_option() * * @return boolean */ function name_based_permalinks_enabled() { $mod_rewrite = (string) bb_get_option( 'mod_rewrite' ); return ( 'slugs' == $mod_rewrite ) ? true : false; } /** * Generate nicer .htaccess * * @uses bb_get_option() * * @return string */ function get_nicer_htaccess() { $nicer_htaccess = << RewriteEngine On # %PATH% and bb_get_option( 'path' ) must match RewriteBase %PATH% Options +FollowSymlinks # admin stuff, not processed RewriteRule ^bb-admin/.*$ - [L,QSA] RewriteRule ^page/([0-9]+)/?$ ?page=$1 [L,QSA] RewriteRule ^profile/([^/]+)/([^/]+)/page/([0-9]+)/?$ profile.php?id=$1&tab=$2&page=$3 [L,QSA] RewriteRule ^profile/([^/]+)/page/([0-9]+)/?$ profile.php?id=$1&page=$2 [L,QSA] RewriteRule ^profile/([^/]+)/([^/]+)$ profile.php?id=$1&tab=$2 [L,QSA] RewriteRule ^profile/([^/]+)$ profile.php?id=$1 [L,QSA] RewriteRule ^profile/$ profile.php [L,QSA] RewriteRule ^rss%PATH%([^/]+)/topics/?$ rss.php?forum=$1&topics=1 [L,QSA] RewriteRule ^rss%PATH%([^/]+)/?$ rss.php?forum=$1 [L,QSA] RewriteRule ^rss/profile/([^/]+)$ rss.php?profile=$1 [L,QSA] RewriteRule ^rss/tags/([^/]+)/topics/?$ rss.php?tag=$1&topics=1 [L,QSA] RewriteRule ^rss/tags/([^/]+)$ rss.php?tag=$1 [L,QSA] RewriteRule ^rss/topic/([^/]+)$ rss.php?topic=$1 [L,QSA] RewriteRule ^rss/view/([^/]+)$ rss.php?view=$1 [L,QSA] RewriteRule ^rss/topics/?$ rss.php?topics=1 [L,QSA] RewriteRule ^rss/?$ rss.php [L,QSA] RewriteRule ^tags/([^/]+)/page/([0-9]+)/?$ tags.php?tag=$1&page=$2 [L,QSA] RewriteRule ^tags/([^/]+)$ tags.php?tag=$1 [L,QSA] RewriteRule ^tags/?$ tags.php [L,QSA] RewriteRule ^view/([^/]+)/page/([0-9]+)/?$ view.php?view=$1&page=$2 [L,QSA] RewriteRule ^view/([^/]+)$ view.php?view=$1 [L,QSA] # forums RewriteRule ^([^/]+)/page/([0-9]+)/?$ forum.php?id=$1&page=$2 [L,QSA] RewriteRule ^([^/]+)/$ forum.php?id=$1 [L,QSA] # tailed '/' is mandatory for forum URIs! Props: Mohta # topics RewriteRule ^([^/]+)/([^/]+)/page/([0-9]+)/?$ topic.php?id=$2&page=$3 [L,QSA] RewriteRule ^([^/]+)/([^/]+)$ topic.php?id=$2 [L,QSA] # other pages RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^.*$ index.php [L] # END bbPress EOF; // Process percent-substitution tag before returning nicer .htaccess return str_replace( '%PATH%', bb_get_option( 'path' ), $nicer_htaccess ); } /** * Backup .htaccess * * @return int|boolean Written bytes|false (error) */ function backup_htaccess() { // Files paths $htaccess_path = BB_PATH . '.htaccess'; $htaccess_bak_path = BB_PATH . 'htaccess.bak'; // Load .htaccess content and save it in htaccess.bak $htaccess_content = file_get_contents( $htaccess_path ); return file_put_contents( $htaccess_bak_path, $htaccess_content ); } /** * Restore .htaccess * * @return int|boolean Written bytes|false (error) */ function restore_htaccess() { // Files paths $htaccess_path = BB_PATH . '.htaccess'; $htaccess_bak_path = BB_PATH . 'htaccess.bak'; // Load htaccess.bak content and save it in .htaccess $htaccess_bak_content = file_get_contents( $htaccess_bak_path ); return file_put_contents( $htaccess_path, $htaccess_bak_content ); } /** * Remove plugin traces * * Note: this function is automatically skipped if plugin is off. * * @uses nicer_permalinks_enabled() * @uses nicer_permalinks_config_errors() * @uses bb_die() * @uses bb_get_uri() * @uses restore_htaccess() * @uses bb_delete_option() * * @return void */ function nicer_permalinks_deactivate() { if ( !$status = nicer_permalinks_enabled() ) // Return if plugin is not enabled return; if ( false !== nicer_permalinks_config_errors( $status ) ) { // Die if plugin config is not correct bb_die( sprintf( __( 'Cannot deactivate "%1$s". See "%1$s" configuration page for more info.', NICER_PERMALINKS_ID ), NICER_PERMALINKS_NAME, bb_get_uri( 'bb-admin/admin-base.php?plugin=nicer_permalinks_config_page' ) ) ); exit(); } // Try to restore .htaccess if ( false === restore_htaccess() ) { // Die if errors occurred bb_die( sprintf( __( 'Cannot deactivate "%1$s". See "%1$s" configuration page for more info.', NICER_PERMALINKS_ID ), NICER_PERMALINKS_NAME, bb_get_uri( 'bb-admin/admin-base.php?plugin=nicer_permalinks_config_page' ) ) ); exit(); } // Remove plugin status from db bb_delete_option( 'nicer_permalinks_enabled' ); } ?>