]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/misc.php
Wordpress 2.3.2-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / misc.php
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         if (!file_exists( $filename ) || is_writeable( $filename ) ) {
49                 if (!file_exists( $filename ) ) {
50                         $markerdata = '';
51                 } else {
52                         $markerdata = explode( "\n", implode( '', file( $filename ) ) );
53                 }
54
55                 $f = fopen( $filename, 'w' );
56                 $foundit = false;
57                 if ( $markerdata ) {
58                         $state = true;
59                         foreach ( $markerdata as $n => $markerline ) {
60                                 if (strpos($markerline, '# BEGIN ' . $marker) !== false)
61                                         $state = false;
62                                 if ( $state ) {
63                                         if ( $n + 1 < count( $markerdata ) )
64                                                 fwrite( $f, "{$markerline}\n" );
65                                         else
66                                                 fwrite( $f, "{$markerline}" );
67                                 }
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" );
74                                         $state = true;
75                                         $foundit = true;
76                                 }
77                         }
78                 }
79                 if (!$foundit) {
80                         fwrite( $f, "# BEGIN {$marker}\n" );
81                         foreach ( $insertion as $insertline )
82                                 fwrite( $f, "{$insertline}\n" );
83                         fwrite( $f, "# END {$marker}\n" );
84                 }
85                 fclose( $f );
86                 return true;
87         } else {
88                 return false;
89         }
90 }
91
92 /**
93  * Updates the htaccess file with the current rules if it is writable.
94  *
95  * Always writes to the file if it exists and is writable to ensure that we blank out old rules.
96  */
97
98 function save_mod_rewrite_rules() {
99         global $wp_rewrite;
100
101         $home_path = get_home_path();
102         $htaccess_file = $home_path.'.htaccess';
103
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 );
110                 }
111         }
112
113         return false;
114 }
115
116 function update_recently_edited( $file ) {
117         $oldfiles = (array ) get_option( 'recently_edited' );
118         if ( $oldfiles ) {
119                 $oldfiles = array_reverse( $oldfiles );
120                 $oldfiles[] = $file;
121                 $oldfiles = array_reverse( $oldfiles );
122                 $oldfiles = array_unique( $oldfiles );
123                 if ( 5 < count( $oldfiles ))
124                         array_pop( $oldfiles );
125         } else {
126                 $oldfiles[] = $file;
127         }
128         update_option( 'recently_edited', $oldfiles );
129 }
130
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;
134
135         if ( defined( "WP_INSTALLING" ) )
136                 return;
137
138         // If home changed, write rewrite rules to new location.
139         $wp_rewrite->flush_rules();
140         // Clear cookies for old paths.
141         wp_clearcookie();
142         // Set cookies for new paths.
143         wp_setcookie( $user_login, $user_pass_md5, true, get_option( 'home' ), get_option( 'siteurl' ));
144 }
145
146 add_action( 'update_option_home', 'update_home_siteurl', 10, 2 );
147 add_action( 'update_option_siteurl', 'update_home_siteurl', 10, 2 );
148
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 ).'...';
156         return $short_url;
157 }
158
159 function wp_reset_vars( $vars ) {
160         for ( $i=0; $i<count( $vars ); $i += 1 ) {
161                 $var = $vars[$i];
162                 global $$var;
163
164                 if (!isset( $$var ) ) {
165                         if ( empty( $_POST["$var"] ) ) {
166                                 if ( empty( $_GET["$var"] ) )
167                                         $$var = '';
168                                 else
169                                         $$var = $_GET["$var"];
170                         } else {
171                                 $$var = $_POST["$var"];
172                         }
173                 }
174         }
175 }
176
177 ?>