root/tags/1.3/wp-admin/includes/misc.php

Revision 1069, 4.8 kB (checked in by donncha, 1 year ago)

Merge with WP 2.3 - testing use only!
Move pluggable functions out of wpmu-functions and into pluggable.php, fixes #439

Line 
1 <?php
2
3 function got_mod_rewrite() {
4     global $is_apache;
5
6     // take 3 educated guesses as to whether or not mod_rewrite is available
7     if ( !$is_apache )
8         return false;
9
10     if ( function_exists( 'apache_get_modules' ) ) {
11         if ( !in_array( 'mod_rewrite', apache_get_modules() ) )
12             return false;
13     }
14
15     return true;
16 }
17
18 // Returns an array of strings from a file (.htaccess ) from between BEGIN
19 // and END markers.
20 function extract_from_markers( $filename, $marker ) {
21     $result = array ();
22
23     if (!file_exists( $filename ) ) {
24         return $result;
25     }
26
27     if ( $markerdata = explode( "\n", implode( '', file( $filename ) ) ));
28     {
29         $state = false;
30         foreach ( $markerdata as $markerline ) {
31             if (strpos($markerline, '# END ' . $marker) !== false)
32                 $state = false;
33             if ( $state )
34                 $result[] = $markerline;
35             if (strpos($markerline, '# BEGIN ' . $marker) !== false)
36                 $state = true;
37         }
38     }
39
40     return $result;
41 }
42
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     return;
49     if (!file_exists( $filename ) || is_writeable( $filename ) ) {
50         if (!file_exists( $filename ) ) {
51             $markerdata = '';
52         } else {
53             $markerdata = explode( "\n", implode( '', file( $filename ) ) );
54         }
55
56         $f = fopen( $filename, 'w' );
57         $foundit = false;
58         if ( $markerdata ) {
59             $state = true;
60             foreach ( $markerdata as $n => $markerline ) {
61                 if (strpos($markerline, '# BEGIN ' . $marker) !== false)
62                     $state = false;
63                 if ( $state ) {
64                     if ( $n + 1 < count( $markerdata ) )
65                         fwrite( $f, "{$markerline}\n" );
66                     else
67                         fwrite( $f, "{$markerline}" );
68                 }
69                 if (strpos($markerline, '# END ' . $marker) !== false) {
70                     fwrite( $f, "# BEGIN {$marker}\n" );
71                     if ( is_array( $insertion ))
72                         foreach ( $insertion as $insertline )
73                             fwrite( $f, "{$insertline}\n" );
74                     fwrite( $f, "# END {$marker}\n" );
75                     $state = true;
76                     $foundit = true;
77                 }
78             }
79         }
80         if (!$foundit) {
81             fwrite( $f, "# BEGIN {$marker}\n" );
82             foreach ( $insertion as $insertline )
83                 fwrite( $f, "{$insertline}\n" );
84             fwrite( $f, "# END {$marker}\n" );
85         }
86         fclose( $f );
87         return true;
88     } else {
89         return false;
90     }
91 }
92
93 /**
94  * Updates the htaccess file with the current rules if it is writable.
95  *
96  * Always writes to the file if it exists and is writable to ensure that we blank out old rules.
97  */
98
99 function save_mod_rewrite_rules() {
100     global $wp_rewrite;
101
102     $home_path = get_home_path();
103     $htaccess_file = $home_path.'.htaccess';
104
105     // If the file doesn't already exists check for write access to the directory and whether of not we have some rules.
106     // else check for write access to the file.
107     if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) {
108         if ( got_mod_rewrite() ) {
109             $rules = explode( "\n", $wp_rewrite->mod_rewrite_rules() );
110             return insert_with_markers( $htaccess_file, 'WordPress', $rules );
111         }
112     }
113
114     return false;
115 }
116
117 function update_recently_edited( $file ) {
118     $oldfiles = (array ) get_option( 'recently_edited' );
119     if ( $oldfiles ) {
120         $oldfiles = array_reverse( $oldfiles );
121         $oldfiles[] = $file;
122         $oldfiles = array_reverse( $oldfiles );
123         $oldfiles = array_unique( $oldfiles );
124         if ( 5 < count( $oldfiles ))
125             array_pop( $oldfiles );
126     } else {
127         $oldfiles[] = $file;
128     }
129     update_option( 'recently_edited', $oldfiles );
130 }
131
132 // If siteurl or home changed, reset cookies and flush rewrite rules.
133 function update_home_siteurl( $old_value, $value ) {
134     global $wp_rewrite, $user_login, $user_pass_md5;
135
136     if ( defined( "WP_INSTALLING" ) )
137         return;
138
139     // If home changed, write rewrite rules to new location.
140     $wp_rewrite->flush_rules();
141     // Clear cookies for old paths.
142     wp_clearcookie();
143     // Set cookies for new paths.
144     wp_setcookie( $user_login, $user_pass_md5, true, get_option( 'home' ), get_option( 'siteurl' ));
145 }
146
147 add_action( 'update_option_home', 'update_home_siteurl', 10, 2 );
148 add_action( 'update_option_siteurl', 'update_home_siteurl', 10, 2 );
149
150 function url_shorten( $url ) {
151     $short_url = str_replace( 'http://', '', stripslashes( $url ));
152     $short_url = str_replace( 'www.', '', $short_url );
153     if ('/' == substr( $short_url, -1 ))
154         $short_url = substr( $short_url, 0, -1 );
155     if ( strlen( $short_url ) > 35 )
156         $short_url = substr( $short_url, 0, 32 ).'...';
157     return $short_url;
158 }
159
160 function wp_reset_vars( $vars ) {
161     for ( $i=0; $i<count( $vars ); $i += 1 ) {
162         $var = $vars[$i];
163         global $$var;
164
165         if (!isset( $$var ) ) {
166             if ( empty( $_POST["$var"] ) ) {
167                 if ( empty( $_GET["$var"] ) )
168                     $$var = '';
169                 else
170                     $$var = $_GET["$var"];
171             } else {
172                 $$var = $_POST["$var"];
173             }
174         }
175     }
176 }
177
178 ?>
179
Note: See TracBrowser for help on using the browser.