]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/misc.php
Wordpress 3.1.1-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / misc.php
1 <?php
2 /**
3  * Misc WordPress Administration API.
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /**
10  * {@internal Missing Short Description}}
11  *
12  * @since 2.0.0
13  *
14  * @return unknown
15  */
16 function got_mod_rewrite() {
17         $got_rewrite = apache_mod_loaded('mod_rewrite', true);
18         return apply_filters('got_rewrite', $got_rewrite);
19 }
20
21 /**
22  * {@internal Missing Short Description}}
23  *
24  * @since 1.5.0
25  *
26  * @param unknown_type $filename
27  * @param unknown_type $marker
28  * @return array An array of strings from a file (.htaccess ) from between BEGIN and END markers.
29  */
30 function extract_from_markers( $filename, $marker ) {
31         $result = array ();
32
33         if (!file_exists( $filename ) ) {
34                 return $result;
35         }
36
37         if ( $markerdata = explode( "\n", implode( '', file( $filename ) ) ));
38         {
39                 $state = false;
40                 foreach ( $markerdata as $markerline ) {
41                         if (strpos($markerline, '# END ' . $marker) !== false)
42                                 $state = false;
43                         if ( $state )
44                                 $result[] = $markerline;
45                         if (strpos($markerline, '# BEGIN ' . $marker) !== false)
46                                 $state = true;
47                 }
48         }
49
50         return $result;
51 }
52
53 /**
54  * {@internal Missing Short Description}}
55  *
56  * Inserts an array of strings into a file (.htaccess ), placing it between
57  * BEGIN and END markers. Replaces existing marked info. Retains surrounding
58  * data. Creates file if none exists.
59  *
60  * @since 1.5.0
61  *
62  * @param unknown_type $filename
63  * @param unknown_type $marker
64  * @param unknown_type $insertion
65  * @return bool True on write success, false on failure.
66  */
67 function insert_with_markers( $filename, $marker, $insertion ) {
68         if (!file_exists( $filename ) || is_writeable( $filename ) ) {
69                 if (!file_exists( $filename ) ) {
70                         $markerdata = '';
71                 } else {
72                         $markerdata = explode( "\n", implode( '', file( $filename ) ) );
73                 }
74
75                 if ( !$f = @fopen( $filename, 'w' ) )
76                         return false;
77
78                 $foundit = false;
79                 if ( $markerdata ) {
80                         $state = true;
81                         foreach ( $markerdata as $n => $markerline ) {
82                                 if (strpos($markerline, '# BEGIN ' . $marker) !== false)
83                                         $state = false;
84                                 if ( $state ) {
85                                         if ( $n + 1 < count( $markerdata ) )
86                                                 fwrite( $f, "{$markerline}\n" );
87                                         else
88                                                 fwrite( $f, "{$markerline}" );
89                                 }
90                                 if (strpos($markerline, '# END ' . $marker) !== false) {
91                                         fwrite( $f, "# BEGIN {$marker}\n" );
92                                         if ( is_array( $insertion ))
93                                                 foreach ( $insertion as $insertline )
94                                                         fwrite( $f, "{$insertline}\n" );
95                                         fwrite( $f, "# END {$marker}\n" );
96                                         $state = true;
97                                         $foundit = true;
98                                 }
99                         }
100                 }
101                 if (!$foundit) {
102                         fwrite( $f, "\n# BEGIN {$marker}\n" );
103                         foreach ( $insertion as $insertline )
104                                 fwrite( $f, "{$insertline}\n" );
105                         fwrite( $f, "# END {$marker}\n" );
106                 }
107                 fclose( $f );
108                 return true;
109         } else {
110                 return false;
111         }
112 }
113
114 /**
115  * Updates the htaccess file with the current rules if it is writable.
116  *
117  * Always writes to the file if it exists and is writable to ensure that we
118  * blank out old rules.
119  *
120  * @since 1.5.0
121  */
122 function save_mod_rewrite_rules() {
123         if ( is_multisite() )
124                 return;
125
126         global $wp_rewrite;
127
128         $home_path = get_home_path();
129         $htaccess_file = $home_path.'.htaccess';
130
131         // If the file doesn't already exist check for write access to the directory and whether we have some rules.
132         // else check for write access to the file.
133         if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) {
134                 if ( got_mod_rewrite() ) {
135                         $rules = explode( "\n", $wp_rewrite->mod_rewrite_rules() );
136                         return insert_with_markers( $htaccess_file, 'WordPress', $rules );
137                 }
138         }
139
140         return false;
141 }
142
143 /**
144  * Updates the IIS web.config file with the current rules if it is writable.
145  * If the permalinks do not require rewrite rules then the rules are deleted from the web.config file.
146  *
147  * @since 2.8.0
148  *
149  * @return bool True if web.config was updated successfully
150  */
151 function iis7_save_url_rewrite_rules(){
152         if ( is_multisite() )
153                 return;
154
155         global $wp_rewrite;
156
157         $home_path = get_home_path();
158         $web_config_file = $home_path . 'web.config';
159
160         // Using win_is_writable() instead of is_writable() because of a bug in Windows PHP
161         if ( iis7_supports_permalinks() && ( ( ! file_exists($web_config_file) && win_is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks() ) || win_is_writable($web_config_file) ) ) {
162                 $rule = $wp_rewrite->iis7_url_rewrite_rules(false, '', '');
163                 if ( ! empty($rule) ) {
164                         return iis7_add_rewrite_rule($web_config_file, $rule);
165                 } else {
166                         return iis7_delete_rewrite_rule($web_config_file);
167                 }
168         }
169         return false;
170 }
171
172 /**
173  * {@internal Missing Short Description}}
174  *
175  * @since 1.5.0
176  *
177  * @param unknown_type $file
178  */
179 function update_recently_edited( $file ) {
180         $oldfiles = (array ) get_option( 'recently_edited' );
181         if ( $oldfiles ) {
182                 $oldfiles = array_reverse( $oldfiles );
183                 $oldfiles[] = $file;
184                 $oldfiles = array_reverse( $oldfiles );
185                 $oldfiles = array_unique( $oldfiles );
186                 if ( 5 < count( $oldfiles ))
187                         array_pop( $oldfiles );
188         } else {
189                 $oldfiles[] = $file;
190         }
191         update_option( 'recently_edited', $oldfiles );
192 }
193
194 /**
195  * If siteurl or home changed, flush rewrite rules.
196  *
197  * @since 2.1.0
198  *
199  * @param unknown_type $old_value
200  * @param unknown_type $value
201  */
202 function update_home_siteurl( $old_value, $value ) {
203         global $wp_rewrite;
204
205         if ( defined( "WP_INSTALLING" ) )
206                 return;
207
208         // If home changed, write rewrite rules to new location.
209         $wp_rewrite->flush_rules();
210 }
211
212 add_action( 'update_option_home', 'update_home_siteurl', 10, 2 );
213 add_action( 'update_option_siteurl', 'update_home_siteurl', 10, 2 );
214
215 /**
216  * Shorten an URL, to be used as link text
217  *
218  * @since 1.2.1
219  *
220  * @param string $url
221  * @return string
222  */
223 function url_shorten( $url ) {
224         $short_url = str_replace( 'http://', '', stripslashes( $url ));
225         $short_url = str_replace( 'www.', '', $short_url );
226         if ('/' == substr( $short_url, -1 ))
227                 $short_url = substr( $short_url, 0, -1 );
228         if ( strlen( $short_url ) > 35 )
229                 $short_url = substr( $short_url, 0, 32 ).'...';
230         return $short_url;
231 }
232
233 /**
234  * Resets global variables based on $_GET and $_POST
235  *
236  * This function resets global variables based on the names passed
237  * in the $vars array to the value of $_POST[$var] or $_GET[$var] or ''
238  * if neither is defined.
239  *
240  * @since 2.0.0
241  *
242  * @param array $vars An array of globals to reset.
243  */
244 function wp_reset_vars( $vars ) {
245         for ( $i=0; $i<count( $vars ); $i += 1 ) {
246                 $var = $vars[$i];
247                 global $$var;
248
249                 if ( empty( $_POST[$var] ) ) {
250                         if ( empty( $_GET[$var] ) )
251                                 $$var = '';
252                         else
253                                 $$var = $_GET[$var];
254                 } else {
255                         $$var = $_POST[$var];
256                 }
257         }
258 }
259
260 /**
261  * {@internal Missing Short Description}}
262  *
263  * @since 2.1.0
264  *
265  * @param unknown_type $message
266  */
267 function show_message($message) {
268         if ( is_wp_error($message) ){
269                 if ( $message->get_error_data() )
270                         $message = $message->get_error_message() . ': ' . $message->get_error_data();
271                 else
272                         $message = $message->get_error_message();
273         }
274         echo "<p>$message</p>\n";
275         wp_ob_end_flush_all();
276         flush();
277 }
278
279 function wp_doc_link_parse( $content ) {
280         if ( !is_string( $content ) || empty( $content ) )
281                 return array();
282
283         if ( !function_exists('token_get_all') )
284                 return array();
285
286         $tokens = token_get_all( $content );
287         $functions = array();
288         $ignore_functions = array();
289         for ( $t = 0, $count = count( $tokens ); $t < $count; $t++ ) {
290                 if ( !is_array( $tokens[$t] ) ) continue;
291                 if ( T_STRING == $tokens[$t][0] && ( '(' == $tokens[ $t + 1 ] || '(' == $tokens[ $t + 2 ] ) ) {
292                         // If it's a function or class defined locally, there's not going to be any docs available
293                         if ( ( isset( $tokens[ $t - 2 ][1] ) && in_array( $tokens[ $t - 2 ][1], array( 'function', 'class' ) ) ) || ( isset( $tokens[ $t - 2 ][0] ) && T_OBJECT_OPERATOR == $tokens[ $t - 1 ][0] ) ) {
294                                 $ignore_functions[] = $tokens[$t][1];
295                         }
296                         // Add this to our stack of unique references
297                         $functions[] = $tokens[$t][1];
298                 }
299         }
300
301         $functions = array_unique( $functions );
302         sort( $functions );
303         $ignore_functions = apply_filters( 'documentation_ignore_functions', $ignore_functions );
304         $ignore_functions = array_unique( $ignore_functions );
305
306         $out = array();
307         foreach ( $functions as $function ) {
308                 if ( in_array( $function, $ignore_functions ) )
309                         continue;
310                 $out[] = $function;
311         }
312
313         return $out;
314 }
315
316 /**
317  * Saves option for number of rows when listing posts, pages, comments, etc.
318  *
319  * @since 2.8
320 **/
321 function set_screen_options() {
322
323         if ( isset($_POST['wp_screen_options']) && is_array($_POST['wp_screen_options']) ) {
324                 check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
325
326                 if ( !$user = wp_get_current_user() )
327                         return;
328                 $option = $_POST['wp_screen_options']['option'];
329                 $value = $_POST['wp_screen_options']['value'];
330
331                 if ( !preg_match( '/^[a-z_-]+$/', $option ) )
332                         return;
333
334                 $option = str_replace('-', '_', $option);
335
336                 $map_option = $option;
337                 $type = str_replace('edit_', '', $map_option);
338                 $type = str_replace('_per_page', '', $type);
339                 if ( in_array($type, get_post_types()) )
340                         $map_option = 'edit_per_page';
341                 if ( in_array( $type, get_taxonomies()) )
342                         $map_option = 'edit_tags_per_page';
343
344
345                 switch ( $map_option ) {
346                         case 'edit_per_page':
347                         case 'users_per_page':
348                         case 'edit_comments_per_page':
349                         case 'upload_per_page':
350                         case 'edit_tags_per_page':
351                         case 'plugins_per_page':
352                         // Network admin
353                         case 'sites_network_per_page':
354                         case 'users_network_per_page':
355                         case 'site_users_network_per_page':
356                         case 'plugins_network_per_page':
357                         case 'themes_network_per_page':
358                         case 'site_themes_network_per_page':
359                                 $value = (int) $value;
360                                 if ( $value < 1 || $value > 999 )
361                                         return;
362                                 break;
363                         default:
364                                 $value = apply_filters('set-screen-option', false, $option, $value);
365                                 if ( false === $value )
366                                         return;
367                                 break;
368                 }
369
370                 update_user_meta($user->ID, $option, $value);
371                 wp_redirect( remove_query_arg( array('pagenum', 'apage', 'paged'), wp_get_referer() ) );
372                 exit;
373         }
374 }
375
376 function wp_menu_unfold() {
377         if ( isset($_GET['unfoldmenu']) ) {
378                 delete_user_setting('mfold');
379                 wp_redirect( remove_query_arg( 'unfoldmenu', stripslashes($_SERVER['REQUEST_URI']) ) );
380                 exit;
381         }
382 }
383
384 /**
385  * Check if rewrite rule for WordPress already exists in the IIS 7 configuration file
386  *
387  * @since 2.8.0
388  *
389  * @return bool
390  * @param string $filename The file path to the configuration file
391  */
392 function iis7_rewrite_rule_exists($filename) {
393         if ( ! file_exists($filename) )
394                 return false;
395         if ( ! class_exists('DOMDocument') )
396                 return false;
397
398         $doc = new DOMDocument();
399         if ( $doc->load($filename) === false )
400                 return false;
401         $xpath = new DOMXPath($doc);
402         $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
403         if ( $rules->length == 0 )
404                 return false;
405         else
406                 return true;
407 }
408
409 /**
410  * Delete WordPress rewrite rule from web.config file if it exists there
411  *
412  * @since 2.8.0
413  *
414  * @param string $filename Name of the configuration file
415  * @return bool
416  */
417 function iis7_delete_rewrite_rule($filename) {
418         // If configuration file does not exist then rules also do not exist so there is nothing to delete
419         if ( ! file_exists($filename) )
420                 return true;
421
422         if ( ! class_exists('DOMDocument') )
423                 return false;
424
425         $doc = new DOMDocument();
426         $doc->preserveWhiteSpace = false;
427
428         if ( $doc -> load($filename) === false )
429                 return false;
430         $xpath = new DOMXPath($doc);
431         $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
432         if ( $rules->length > 0 ) {
433                 $child = $rules->item(0);
434                 $parent = $child->parentNode;
435                 $parent->removeChild($child);
436                 $doc->formatOutput = true;
437                 saveDomDocument($doc, $filename);
438         }
439         return true;
440 }
441
442 /**
443  * Add WordPress rewrite rule to the IIS 7 configuration file.
444  *
445  * @since 2.8.0
446  *
447  * @param string $filename The file path to the configuration file
448  * @param string $rewrite_rule The XML fragment with URL Rewrite rule
449  * @return bool
450  */
451 function iis7_add_rewrite_rule($filename, $rewrite_rule) {
452         if ( ! class_exists('DOMDocument') )
453                 return false;
454
455         // If configuration file does not exist then we create one.
456         if ( ! file_exists($filename) ) {
457                 $fp = fopen( $filename, 'w');
458                 fwrite($fp, '<configuration/>');
459                 fclose($fp);
460         }
461
462         $doc = new DOMDocument();
463         $doc->preserveWhiteSpace = false;
464
465         if ( $doc->load($filename) === false )
466                 return false;
467
468         $xpath = new DOMXPath($doc);
469
470         // First check if the rule already exists as in that case there is no need to re-add it
471         $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
472         if ( $wordpress_rules->length > 0 )
473                 return true;
474
475         // Check the XPath to the rewrite rule and create XML nodes if they do not exist
476         $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
477         if ( $xmlnodes->length > 0 ) {
478                 $rules_node = $xmlnodes->item(0);
479         } else {
480                 $rules_node = $doc->createElement('rules');
481
482                 $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite');
483                 if ( $xmlnodes->length > 0 ) {
484                         $rewrite_node = $xmlnodes->item(0);
485                         $rewrite_node->appendChild($rules_node);
486                 } else {
487                         $rewrite_node = $doc->createElement('rewrite');
488                         $rewrite_node->appendChild($rules_node);
489
490                         $xmlnodes = $xpath->query('/configuration/system.webServer');
491                         if ( $xmlnodes->length > 0 ) {
492                                 $system_webServer_node = $xmlnodes->item(0);
493                                 $system_webServer_node->appendChild($rewrite_node);
494                         } else {
495                                 $system_webServer_node = $doc->createElement('system.webServer');
496                                 $system_webServer_node->appendChild($rewrite_node);
497
498                                 $xmlnodes = $xpath->query('/configuration');
499                                 if ( $xmlnodes->length > 0 ) {
500                                         $config_node = $xmlnodes->item(0);
501                                         $config_node->appendChild($system_webServer_node);
502                                 } else {
503                                         $config_node = $doc->createElement('configuration');
504                                         $doc->appendChild($config_node);
505                                         $config_node->appendChild($system_webServer_node);
506                                 }
507                         }
508                 }
509         }
510
511         $rule_fragment = $doc->createDocumentFragment();
512         $rule_fragment->appendXML($rewrite_rule);
513         $rules_node->appendChild($rule_fragment);
514
515         $doc->encoding = "UTF-8";
516         $doc->formatOutput = true;
517         saveDomDocument($doc, $filename);
518
519         return true;
520 }
521
522 /**
523  * Saves the XML document into a file
524  *
525  * @since 2.8.0
526  *
527  * @param DOMDocument $doc
528  * @param string $filename
529  */
530 function saveDomDocument($doc, $filename) {
531         $config = $doc->saveXML();
532         $config = preg_replace("/([^\r])\n/", "$1\r\n", $config);
533         $fp = fopen($filename, 'w');
534         fwrite($fp, $config);
535         fclose($fp);
536 }
537
538 /**
539  * Workaround for Windows bug in is_writable() function
540  *
541  * @since 2.8.0
542  *
543  * @param string $path
544  * @return bool
545  */
546 function win_is_writable( $path ) {
547         /* will work in despite of Windows ACLs bug
548          * NOTE: use a trailing slash for folders!!!
549          * see http://bugs.php.net/bug.php?id=27609
550          * see http://bugs.php.net/bug.php?id=30931
551          */
552
553         if ( $path[strlen( $path ) - 1] == '/' ) // recursively return a temporary file path
554                 return win_is_writable( $path . uniqid( mt_rand() ) . '.tmp');
555         else if ( is_dir( $path ) )
556                 return win_is_writable( $path . '/' . uniqid( mt_rand() ) . '.tmp' );
557         // check tmp file for read/write capabilities
558         $should_delete_tmp_file = !file_exists( $path );
559         $f = @fopen( $path, 'a' );
560         if ( $f === false )
561                 return false;
562         fclose( $f );
563         if ( $should_delete_tmp_file )
564                 unlink( $path );
565         return true;
566 }
567
568 /**
569  * Display the default admin color scheme picker (Used in user-edit.php)
570  *
571  * @since 3.0.0
572  */
573 function admin_color_scheme_picker() {
574         global $_wp_admin_css_colors, $user_id; ?>
575 <fieldset><legend class="screen-reader-text"><span><?php _e('Admin Color Scheme')?></span></legend>
576 <?php
577 $current_color = get_user_option('admin_color', $user_id);
578 if ( empty($current_color) )
579         $current_color = 'fresh';
580 foreach ( $_wp_admin_css_colors as $color => $color_info ): ?>
581 <div class="color-option"><input name="admin_color" id="admin_color_<?php echo $color; ?>" type="radio" value="<?php echo esc_attr($color) ?>" class="tog" <?php checked($color, $current_color); ?> />
582         <table class="color-palette">
583         <tr>
584         <?php foreach ( $color_info->colors as $html_color ): ?>
585         <td style="background-color: <?php echo $html_color ?>" title="<?php echo $color ?>">&nbsp;</td>
586         <?php endforeach; ?>
587         </tr>
588         </table>
589
590         <label for="admin_color_<?php echo $color; ?>"><?php echo $color_info->name ?></label>
591 </div>
592         <?php endforeach; ?>
593 </fieldset>
594 <?php
595 }
596 ?>