3 function got_mod_rewrite() {
6 // take 3 educated guesses as to whether or not mod_rewrite is available
10 if ( function_exists( 'apache_get_modules' ) ) {
11 if ( !in_array( 'mod_rewrite', apache_get_modules() ) )
18 // Returns an array of strings from a file (.htaccess ) from between BEGIN
20 function extract_from_markers( $filename, $marker ) {
23 if (!file_exists( $filename ) ) {
27 if ( $markerdata = explode( "\n", implode( '', file( $filename ) ) ));
30 foreach ( $markerdata as $markerline ) {
31 if (strpos($markerline, '# END ' . $marker) !== false)
34 $result[] = $markerline;
35 if (strpos($markerline, '# BEGIN ' . $marker) !== false)
43 // Inserts an array of strings into a file (.htaccess ), placing it between
44 // BEGIN and END markers. Replaces existing marked info. Retains surrounding
45 // data. Creates file if none exists.
46 // Returns true on write success, false on failure.
47 function insert_with_markers( $filename, $marker, $insertion ) {
48 if (!file_exists( $filename ) || is_writeable( $filename ) ) {
49 if (!file_exists( $filename ) ) {
52 $markerdata = explode( "\n", implode( '', file( $filename ) ) );
55 $f = fopen( $filename, 'w' );
59 foreach ( $markerdata as $n => $markerline ) {
60 if (strpos($markerline, '# BEGIN ' . $marker) !== false)
63 if ( $n + 1 < count( $markerdata ) )
64 fwrite( $f, "{$markerline}\n" );
66 fwrite( $f, "{$markerline}" );
68 if (strpos($markerline, '# END ' . $marker) !== false) {
69 fwrite( $f, "# BEGIN {$marker}\n" );
70 if ( is_array( $insertion ))
71 foreach ( $insertion as $insertline )
72 fwrite( $f, "{$insertline}\n" );
73 fwrite( $f, "# END {$marker}\n" );
80 fwrite( $f, "# BEGIN {$marker}\n" );
81 foreach ( $insertion as $insertline )
82 fwrite( $f, "{$insertline}\n" );
83 fwrite( $f, "# END {$marker}\n" );
93 * Updates the htaccess file with the current rules if it is writable.
95 * Always writes to the file if it exists and is writable to ensure that we blank out old rules.
98 function save_mod_rewrite_rules() {
101 $home_path = get_home_path();
102 $htaccess_file = $home_path.'.htaccess';
104 // If the file doesn't already exists check for write access to the directory and whether of not we have some rules.
105 // else check for write access to the file.
106 if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) {
107 if ( got_mod_rewrite() ) {
108 $rules = explode( "\n", $wp_rewrite->mod_rewrite_rules() );
109 return insert_with_markers( $htaccess_file, 'WordPress', $rules );
116 function update_recently_edited( $file ) {
117 $oldfiles = (array ) get_option( 'recently_edited' );
119 $oldfiles = array_reverse( $oldfiles );
121 $oldfiles = array_reverse( $oldfiles );
122 $oldfiles = array_unique( $oldfiles );
123 if ( 5 < count( $oldfiles ))
124 array_pop( $oldfiles );
128 update_option( 'recently_edited', $oldfiles );
131 // If siteurl or home changed, reset cookies and flush rewrite rules.
132 function update_home_siteurl( $old_value, $value ) {
133 global $wp_rewrite, $user_login, $user_pass_md5;
135 if ( defined( "WP_INSTALLING" ) )
138 // If home changed, write rewrite rules to new location.
139 $wp_rewrite->flush_rules();
140 // Clear cookies for old paths.
142 // Set cookies for new paths.
143 wp_setcookie( $user_login, $user_pass_md5, true, get_option( 'home' ), get_option( 'siteurl' ));
146 add_action( 'update_option_home', 'update_home_siteurl', 10, 2 );
147 add_action( 'update_option_siteurl', 'update_home_siteurl', 10, 2 );
149 function url_shorten( $url ) {
150 $short_url = str_replace( 'http://', '', stripslashes( $url ));
151 $short_url = str_replace( 'www.', '', $short_url );
152 if ('/' == substr( $short_url, -1 ))
153 $short_url = substr( $short_url, 0, -1 );
154 if ( strlen( $short_url ) > 35 )
155 $short_url = substr( $short_url, 0, 32 ).'...';
159 function wp_reset_vars( $vars ) {
160 for ( $i=0; $i<count( $vars ); $i += 1 ) {
164 if (!isset( $$var ) ) {
165 if ( empty( $_POST["$var"] ) ) {
166 if ( empty( $_GET["$var"] ) )
169 $$var = $_GET["$var"];
171 $$var = $_POST["$var"];