]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/misc.php
Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-admin / includes / misc.php
1 <?php
2
3 function got_mod_rewrite() {
4         $got_rewrite = apache_mod_loaded('mod_rewrite', true);
5         return apply_filters('got_rewrite', $got_rewrite);
6 }
7
8 // Returns an array of strings from a file (.htaccess ) from between BEGIN
9 // and END markers.
10 function extract_from_markers( $filename, $marker ) {
11         $result = array ();
12
13         if (!file_exists( $filename ) ) {
14                 return $result;
15         }
16
17         if ( $markerdata = explode( "\n", implode( '', file( $filename ) ) ));
18         {
19                 $state = false;
20                 foreach ( $markerdata as $markerline ) {
21                         if (strpos($markerline, '# END ' . $marker) !== false)
22                                 $state = false;
23                         if ( $state )
24                                 $result[] = $markerline;
25                         if (strpos($markerline, '# BEGIN ' . $marker) !== false)
26                                 $state = true;
27                 }
28         }
29
30         return $result;
31 }
32
33 // Inserts an array of strings into a file (.htaccess ), placing it between
34 // BEGIN and END markers.  Replaces existing marked info.  Retains surrounding
35 // data.  Creates file if none exists.
36 // Returns true on write success, false on failure.
37 function insert_with_markers( $filename, $marker, $insertion ) {
38         if (!file_exists( $filename ) || is_writeable( $filename ) ) {
39                 if (!file_exists( $filename ) ) {
40                         $markerdata = '';
41                 } else {
42                         $markerdata = explode( "\n", implode( '', file( $filename ) ) );
43                 }
44
45                 $f = fopen( $filename, 'w' );
46                 $foundit = false;
47                 if ( $markerdata ) {
48                         $state = true;
49                         foreach ( $markerdata as $n => $markerline ) {
50                                 if (strpos($markerline, '# BEGIN ' . $marker) !== false)
51                                         $state = false;
52                                 if ( $state ) {
53                                         if ( $n + 1 < count( $markerdata ) )
54                                                 fwrite( $f, "{$markerline}\n" );
55                                         else
56                                                 fwrite( $f, "{$markerline}" );
57                                 }
58                                 if (strpos($markerline, '# END ' . $marker) !== false) {
59                                         fwrite( $f, "# BEGIN {$marker}\n" );
60                                         if ( is_array( $insertion ))
61                                                 foreach ( $insertion as $insertline )
62                                                         fwrite( $f, "{$insertline}\n" );
63                                         fwrite( $f, "# END {$marker}\n" );
64                                         $state = true;
65                                         $foundit = true;
66                                 }
67                         }
68                 }
69                 if (!$foundit) {
70                         fwrite( $f, "# BEGIN {$marker}\n" );
71                         foreach ( $insertion as $insertline )
72                                 fwrite( $f, "{$insertline}\n" );
73                         fwrite( $f, "# END {$marker}\n" );
74                 }
75                 fclose( $f );
76                 return true;
77         } else {
78                 return false;
79         }
80 }
81
82 /**
83  * Updates the htaccess file with the current rules if it is writable.
84  *
85  * Always writes to the file if it exists and is writable to ensure that we blank out old rules.
86  */
87
88 function save_mod_rewrite_rules() {
89         global $wp_rewrite;
90
91         $home_path = get_home_path();
92         $htaccess_file = $home_path.'.htaccess';
93
94         // If the file doesn't already exists check for write access to the directory and whether of not we have some rules.
95         // else check for write access to the file.
96         if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) {
97                 if ( got_mod_rewrite() ) {
98                         $rules = explode( "\n", $wp_rewrite->mod_rewrite_rules() );
99                         return insert_with_markers( $htaccess_file, 'WordPress', $rules );
100                 }
101         }
102
103         return false;
104 }
105
106 function update_recently_edited( $file ) {
107         $oldfiles = (array ) get_option( 'recently_edited' );
108         if ( $oldfiles ) {
109                 $oldfiles = array_reverse( $oldfiles );
110                 $oldfiles[] = $file;
111                 $oldfiles = array_reverse( $oldfiles );
112                 $oldfiles = array_unique( $oldfiles );
113                 if ( 5 < count( $oldfiles ))
114                         array_pop( $oldfiles );
115         } else {
116                 $oldfiles[] = $file;
117         }
118         update_option( 'recently_edited', $oldfiles );
119 }
120
121 // If siteurl or home changed, flush rewrite rules.
122 function update_home_siteurl( $old_value, $value ) {
123         global $wp_rewrite;
124
125         if ( defined( "WP_INSTALLING" ) )
126                 return;
127
128         // If home changed, write rewrite rules to new location.
129         $wp_rewrite->flush_rules();
130 }
131
132 add_action( 'update_option_home', 'update_home_siteurl', 10, 2 );
133 add_action( 'update_option_siteurl', 'update_home_siteurl', 10, 2 );
134
135 function url_shorten( $url ) {
136         $short_url = str_replace( 'http://', '', stripslashes( $url ));
137         $short_url = str_replace( 'www.', '', $short_url );
138         if ('/' == substr( $short_url, -1 ))
139                 $short_url = substr( $short_url, 0, -1 );
140         if ( strlen( $short_url ) > 35 )
141                 $short_url = substr( $short_url, 0, 32 ).'...';
142         return $short_url;
143 }
144
145 function wp_reset_vars( $vars ) {
146         for ( $i=0; $i<count( $vars ); $i += 1 ) {
147                 $var = $vars[$i];
148                 global $$var;
149
150                 if (!isset( $$var ) ) {
151                         if ( empty( $_POST["$var"] ) ) {
152                                 if ( empty( $_GET["$var"] ) )
153                                         $$var = '';
154                                 else
155                                         $$var = $_GET["$var"];
156                         } else {
157                                 $$var = $_POST["$var"];
158                         }
159                 }
160         }
161 }
162
163 ?>