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 unknown |
---|
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 unknown |
---|
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 | function create_cv_lock_file() |
---|
54 | { |
---|
55 | fopen(getcwd() . '/cv.lock', 'x'); |
---|
56 | } |
---|
57 | |
---|
58 | function delete_cv_lock_file() |
---|
59 | { |
---|
60 | unlink(getcwd() . '/cv.lock'); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | /** |
---|
65 | * {@internal Missing Short Description}} |
---|
66 | * |
---|
67 | * Inserts an array of strings into a file (.htaccess ), placing it between |
---|
68 | * BEGIN and END markers. Replaces existing marked info. Retains surrounding |
---|
69 | * data. Creates file if none exists. |
---|
70 | * |
---|
71 | * @since unknown |
---|
72 | * |
---|
73 | * @param unknown_type $filename |
---|
74 | * @param unknown_type $marker |
---|
75 | * @param unknown_type $insertion |
---|
76 | * @return bool True on write success, false on failure. |
---|
77 | */ |
---|
78 | function insert_with_markers( $filename, $marker, $insertion ) { |
---|
79 | $marker .= ' root'; |
---|
80 | if(file_exists(getcwd().'/cv.lock')) |
---|
81 | { |
---|
82 | return false; |
---|
83 | } |
---|
84 | create_cv_lock_file(); |
---|
85 | if (!file_exists( $filename ) || is_writeable( $filename ) ) { |
---|
86 | if (!file_exists( $filename ) ) { |
---|
87 | $markerdata = ''; |
---|
88 | } else { |
---|
89 | $markerdata = explode( "\n", implode( '', file( $filename ) ) ); |
---|
90 | } |
---|
91 | |
---|
92 | if ( !$f = @fopen( $filename, 'w' ) ) |
---|
93 | { |
---|
94 | delete_cv_lock_file(); |
---|
95 | return false; |
---|
96 | } |
---|
97 | $foundit = false; |
---|
98 | if ( $markerdata ) { |
---|
99 | $state = true; |
---|
100 | foreach ( $markerdata as $n => $markerline ) { |
---|
101 | if (strpos($markerline, '# BEGIN ' . $marker) !== false) |
---|
102 | $state = false; |
---|
103 | if ( $state ) { |
---|
104 | if ( $n + 1 < count( $markerdata ) ) |
---|
105 | fwrite( $f, "{$markerline}\n" ); |
---|
106 | else |
---|
107 | fwrite( $f, "{$markerline}" ); |
---|
108 | } |
---|
109 | if (strpos($markerline, '# END ' . $marker) !== false) { |
---|
110 | fwrite( $f, "# BEGIN {$marker}\n" ); |
---|
111 | if ( is_array( $insertion )) |
---|
112 | foreach ( $insertion as $insertline ) |
---|
113 | fwrite( $f, "{$insertline}\n" ); |
---|
114 | fwrite( $f, "# END {$marker}\n" ); |
---|
115 | $state = true; |
---|
116 | $foundit = true; |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|
120 | if (!$foundit) { |
---|
121 | fwrite( $f, "\n# BEGIN {$marker}\n" ); |
---|
122 | foreach ( $insertion as $insertline ) |
---|
123 | fwrite( $f, "{$insertline}\n" ); |
---|
124 | fwrite( $f, "# END {$marker}\n" ); |
---|
125 | } |
---|
126 | fclose( $f ); |
---|
127 | delete_cv_lock_file(); |
---|
128 | return true; |
---|
129 | } else { |
---|
130 | delete_cv_lock_file(); |
---|
131 | return false; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Updates the htaccess file with the current rules if it is writable. |
---|
137 | * |
---|
138 | * Always writes to the file if it exists and is writable to ensure that we |
---|
139 | * blank out old rules. |
---|
140 | * |
---|
141 | * @since unknown |
---|
142 | */ |
---|
143 | function save_mod_rewrite_rules() { |
---|
144 | global $wp_rewrite; |
---|
145 | |
---|
146 | $home_path = get_home_path(); |
---|
147 | $htaccess_file = $home_path.'.htaccess'; |
---|
148 | |
---|
149 | // If the file doesn't already exists check for write access to the directory and whether of not we have some rules. |
---|
150 | // else check for write access to the file. |
---|
151 | if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) { |
---|
152 | if ( got_mod_rewrite() ) { |
---|
153 | $rules = explode( "\n", $wp_rewrite->mod_rewrite_rules() ); |
---|
154 | return insert_with_markers( $htaccess_file, 'WordPress', $rules ); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | return false; |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * Updates the IIS web.config file with the current rules if it is writable. |
---|
163 | * If the permalinks do not require rewrite rules then the rules are deleted from the web.config file. |
---|
164 | * |
---|
165 | * @since 2.8.0 |
---|
166 | * |
---|
167 | * @return bool True if web.config was updated successfully |
---|
168 | */ |
---|
169 | function iis7_save_url_rewrite_rules(){ |
---|
170 | global $wp_rewrite; |
---|
171 | |
---|
172 | $home_path = get_home_path(); |
---|
173 | $web_config_file = $home_path . 'web.config'; |
---|
174 | |
---|
175 | // Using win_is_writable() instead of is_writable() because of a bug in Windows PHP |
---|
176 | if ( ( ! file_exists($web_config_file) && win_is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks() ) || win_is_writable($web_config_file) ) { |
---|
177 | if ( iis7_supports_permalinks() ) { |
---|
178 | $rule = $wp_rewrite->iis7_url_rewrite_rules(false, '', ''); |
---|
179 | if ( ! empty($rule) ) { |
---|
180 | return iis7_add_rewrite_rule($web_config_file, $rule); |
---|
181 | } else { |
---|
182 | return iis7_delete_rewrite_rule($web_config_file); |
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | return false; |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * {@internal Missing Short Description}} |
---|
191 | * |
---|
192 | * @since unknown |
---|
193 | * |
---|
194 | * @param unknown_type $file |
---|
195 | */ |
---|
196 | function update_recently_edited( $file ) { |
---|
197 | $oldfiles = (array ) get_option( 'recently_edited' ); |
---|
198 | if ( $oldfiles ) { |
---|
199 | $oldfiles = array_reverse( $oldfiles ); |
---|
200 | $oldfiles[] = $file; |
---|
201 | $oldfiles = array_reverse( $oldfiles ); |
---|
202 | $oldfiles = array_unique( $oldfiles ); |
---|
203 | if ( 5 < count( $oldfiles )) |
---|
204 | array_pop( $oldfiles ); |
---|
205 | } else { |
---|
206 | $oldfiles[] = $file; |
---|
207 | } |
---|
208 | update_option( 'recently_edited', $oldfiles ); |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * If siteurl or home changed, flush rewrite rules. |
---|
213 | * |
---|
214 | * @since unknown |
---|
215 | * |
---|
216 | * @param unknown_type $old_value |
---|
217 | * @param unknown_type $value |
---|
218 | */ |
---|
219 | function update_home_siteurl( $old_value, $value ) { |
---|
220 | global $wp_rewrite; |
---|
221 | |
---|
222 | if ( defined( "WP_INSTALLING" ) ) |
---|
223 | return; |
---|
224 | |
---|
225 | // If home changed, write rewrite rules to new location. |
---|
226 | $wp_rewrite->flush_rules(); |
---|
227 | } |
---|
228 | |
---|
229 | add_action( 'update_option_home', 'update_home_siteurl', 10, 2 ); |
---|
230 | add_action( 'update_option_siteurl', 'update_home_siteurl', 10, 2 ); |
---|
231 | |
---|
232 | /** |
---|
233 | * {@internal Missing Short Description}} |
---|
234 | * |
---|
235 | * @since unknown |
---|
236 | * |
---|
237 | * @param unknown_type $url |
---|
238 | * @return unknown |
---|
239 | */ |
---|
240 | function url_shorten( $url ) { |
---|
241 | $short_url = str_replace( 'http://', '', stripslashes( $url )); |
---|
242 | $short_url = str_replace( 'www.', '', $short_url ); |
---|
243 | if ('/' == substr( $short_url, -1 )) |
---|
244 | $short_url = substr( $short_url, 0, -1 ); |
---|
245 | if ( strlen( $short_url ) > 35 ) |
---|
246 | $short_url = substr( $short_url, 0, 32 ).'...'; |
---|
247 | return $short_url; |
---|
248 | } |
---|
249 | |
---|
250 | /** |
---|
251 | * {@internal Missing Short Description}} |
---|
252 | * |
---|
253 | * @since unknown |
---|
254 | * |
---|
255 | * @param unknown_type $vars |
---|
256 | */ |
---|
257 | function wp_reset_vars( $vars ) { |
---|
258 | for ( $i=0; $i<count( $vars ); $i += 1 ) { |
---|
259 | $var = $vars[$i]; |
---|
260 | global $$var; |
---|
261 | |
---|
262 | if (!isset( $$var ) ) { |
---|
263 | if ( empty( $_POST["$var"] ) ) { |
---|
264 | if ( empty( $_GET["$var"] ) ) |
---|
265 | $$var = ''; |
---|
266 | else |
---|
267 | $$var = $_GET["$var"]; |
---|
268 | } else { |
---|
269 | $$var = $_POST["$var"]; |
---|
270 | } |
---|
271 | } |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | /** |
---|
276 | * {@internal Missing Short Description}} |
---|
277 | * |
---|
278 | * @since unknown |
---|
279 | * |
---|
280 | * @param unknown_type $message |
---|
281 | */ |
---|
282 | function show_message($message) { |
---|
283 | if( is_wp_error($message) ){ |
---|
284 | if( $message->get_error_data() ) |
---|
285 | $message = $message->get_error_message() . ': ' . $message->get_error_data(); |
---|
286 | else |
---|
287 | $message = $message->get_error_message(); |
---|
288 | } |
---|
289 | echo "<p>$message</p>\n"; |
---|
290 | } |
---|
291 | |
---|
292 | function wp_doc_link_parse( $content ) { |
---|
293 | if ( !is_string( $content ) || empty( $content ) ) |
---|
294 | return array(); |
---|
295 | |
---|
296 | if ( !function_exists('token_get_all') ) |
---|
297 | return array(); |
---|
298 | |
---|
299 | $tokens = token_get_all( $content ); |
---|
300 | $functions = array(); |
---|
301 | $ignore_functions = array(); |
---|
302 | for ( $t = 0, $count = count( $tokens ); $t < $count; $t++ ) { |
---|
303 | if ( !is_array( $tokens[$t] ) ) continue; |
---|
304 | if ( T_STRING == $tokens[$t][0] && ( '(' == $tokens[ $t + 1 ] || '(' == $tokens[ $t + 2 ] ) ) { |
---|
305 | // If it's a function or class defined locally, there's not going to be any docs available |
---|
306 | 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] ) ) { |
---|
307 | $ignore_functions[] = $tokens[$t][1]; |
---|
308 | } |
---|
309 | // Add this to our stack of unique references |
---|
310 | $functions[] = $tokens[$t][1]; |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | $functions = array_unique( $functions ); |
---|
315 | sort( $functions ); |
---|
316 | $ignore_functions = apply_filters( 'documentation_ignore_functions', $ignore_functions ); |
---|
317 | $ignore_functions = array_unique( $ignore_functions ); |
---|
318 | |
---|
319 | $out = array(); |
---|
320 | foreach ( $functions as $function ) { |
---|
321 | if ( in_array( $function, $ignore_functions ) ) |
---|
322 | continue; |
---|
323 | $out[] = $function; |
---|
324 | } |
---|
325 | |
---|
326 | return $out; |
---|
327 | } |
---|
328 | |
---|
329 | /** |
---|
330 | * Determines the language to use for CodePress syntax highlighting, |
---|
331 | * based only on a filename. |
---|
332 | * |
---|
333 | * @since 2.8 |
---|
334 | * |
---|
335 | * @param string $filename The name of the file to be highlighting |
---|
336 | **/ |
---|
337 | function codepress_get_lang( $filename ) { |
---|
338 | $codepress_supported_langs = apply_filters( 'codepress_supported_langs', |
---|
339 | array( '.css' => 'css', |
---|
340 | '.js' => 'javascript', |
---|
341 | '.php' => 'php', |
---|
342 | '.html' => 'html', |
---|
343 | '.htm' => 'html', |
---|
344 | '.txt' => 'text' |
---|
345 | ) ); |
---|
346 | $extension = substr( $filename, strrpos( $filename, '.' ) ); |
---|
347 | if ( $extension && array_key_exists( $extension, $codepress_supported_langs ) ) |
---|
348 | return $codepress_supported_langs[$extension]; |
---|
349 | |
---|
350 | return 'generic'; |
---|
351 | } |
---|
352 | |
---|
353 | /** |
---|
354 | * Adds Javascript required to make CodePress work on the theme/plugin editors. |
---|
355 | * |
---|
356 | * This code is attached to the action admin_print_footer_scripts. |
---|
357 | * |
---|
358 | * @since 2.8 |
---|
359 | **/ |
---|
360 | function codepress_footer_js() { |
---|
361 | // Script-loader breaks CP's automatic path-detection, thus CodePress.path |
---|
362 | // CP edits in an iframe, so we need to grab content back into normal form |
---|
363 | ?><script type="text/javascript"> |
---|
364 | /* <![CDATA[ */ |
---|
365 | var codepress_path = '<?php echo includes_url('js/codepress/'); ?>'; |
---|
366 | jQuery('#template').submit(function(){ |
---|
367 | if (jQuery('#newcontent_cp').length) |
---|
368 | jQuery('#newcontent_cp').val(newcontent.getCode()).removeAttr('disabled'); |
---|
369 | }); |
---|
370 | jQuery('#codepress-on').hide(); |
---|
371 | jQuery('#codepress-off').show(); |
---|
372 | /* ]]> */ |
---|
373 | </script> |
---|
374 | <?php |
---|
375 | } |
---|
376 | |
---|
377 | /** |
---|
378 | * Determine whether to use CodePress or not. |
---|
379 | * |
---|
380 | * @since 2.8 |
---|
381 | **/ |
---|
382 | function use_codepress() { |
---|
383 | |
---|
384 | if ( isset($_GET['codepress']) ) { |
---|
385 | $on = 'on' == $_GET['codepress'] ? 'on' : 'off'; |
---|
386 | set_user_setting( 'codepress', $on ); |
---|
387 | } else { |
---|
388 | $on = get_user_setting('codepress', 'on'); |
---|
389 | } |
---|
390 | |
---|
391 | if ( 'on' == $on ) { |
---|
392 | add_action( 'admin_print_footer_scripts', 'codepress_footer_js' ); |
---|
393 | return true; |
---|
394 | } |
---|
395 | |
---|
396 | return false; |
---|
397 | } |
---|
398 | |
---|
399 | /** |
---|
400 | * Saves option for number of rows when listing posts, pages, comments, etc. |
---|
401 | * |
---|
402 | * @since 2.8 |
---|
403 | **/ |
---|
404 | function set_screen_options() { |
---|
405 | |
---|
406 | if ( isset($_POST['wp_screen_options']) && is_array($_POST['wp_screen_options']) ) { |
---|
407 | check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' ); |
---|
408 | |
---|
409 | if ( !$user = wp_get_current_user() ) |
---|
410 | return; |
---|
411 | $option = $_POST['wp_screen_options']['option']; |
---|
412 | $value = $_POST['wp_screen_options']['value']; |
---|
413 | |
---|
414 | if ( !preg_match( '/^[a-z_-]+$/', $option ) ) |
---|
415 | return; |
---|
416 | |
---|
417 | $option = str_replace('-', '_', $option); |
---|
418 | |
---|
419 | switch ( $option ) { |
---|
420 | case 'edit_per_page': |
---|
421 | case 'edit_pages_per_page': |
---|
422 | case 'edit_comments_per_page': |
---|
423 | case 'upload_per_page': |
---|
424 | case 'categories_per_page': |
---|
425 | case 'edit_tags_per_page': |
---|
426 | case 'plugins_per_page': |
---|
427 | $value = (int) $value; |
---|
428 | if ( $value < 1 || $value > 999 ) |
---|
429 | return; |
---|
430 | break; |
---|
431 | default: |
---|
432 | $value = apply_filters('set-screen-option', false, $option, $value); |
---|
433 | if ( false === $value ) |
---|
434 | return; |
---|
435 | break; |
---|
436 | } |
---|
437 | |
---|
438 | update_usermeta($user->ID, $option, $value); |
---|
439 | wp_redirect( remove_query_arg( array('pagenum', 'apage', 'paged'), wp_get_referer() ) ); |
---|
440 | exit; |
---|
441 | } |
---|
442 | } |
---|
443 | |
---|
444 | function wp_menu_unfold() { |
---|
445 | if ( isset($_GET['unfoldmenu']) ) { |
---|
446 | delete_user_setting('mfold'); |
---|
447 | wp_redirect( remove_query_arg( 'unfoldmenu', stripslashes($_SERVER['REQUEST_URI']) ) ); |
---|
448 | exit; |
---|
449 | } |
---|
450 | } |
---|
451 | |
---|
452 | /** |
---|
453 | * Check if IIS 7 supports pretty permalinks |
---|
454 | * |
---|
455 | * @since 2.8.0 |
---|
456 | * |
---|
457 | * @return bool |
---|
458 | */ |
---|
459 | function iis7_supports_permalinks() { |
---|
460 | global $is_iis7; |
---|
461 | |
---|
462 | $supports_permalinks = false; |
---|
463 | if ( $is_iis7 ) { |
---|
464 | /* First we check if the DOMDocument class exists. If it does not exist, |
---|
465 | * which is the case for PHP 4.X, then we cannot easily update the xml configuration file, |
---|
466 | * hence we just bail out and tell user that pretty permalinks cannot be used. |
---|
467 | * This is not a big issue because PHP 4.X is going to be depricated and for IIS it |
---|
468 | * is recommended to use PHP 5.X NTS. |
---|
469 | * Next we check if the URL Rewrite Module 1.1 is loaded and enabled for the web site. When |
---|
470 | * URL Rewrite 1.1 is loaded it always sets a server variable called 'IIS_UrlRewriteModule'. |
---|
471 | * Lastly we make sure that PHP is running via FastCGI. This is important because if it runs |
---|
472 | * via ISAPI then pretty permalinks will not work. |
---|
473 | */ |
---|
474 | $supports_permalinks = class_exists('DOMDocument') && isset($_SERVER['IIS_UrlRewriteModule']) && ( php_sapi_name() == 'cgi-fcgi' ); |
---|
475 | } |
---|
476 | |
---|
477 | return apply_filters('iis7_supports_permalinks', $supports_permalinks); |
---|
478 | } |
---|
479 | |
---|
480 | /** |
---|
481 | * Check if rewrite rule for WordPress already exists in the IIS 7 configuration file |
---|
482 | * |
---|
483 | * @since 2.8.0 |
---|
484 | * |
---|
485 | * @return bool |
---|
486 | * @param string $filename The file path to the configuration file |
---|
487 | */ |
---|
488 | function iis7_rewrite_rule_exists($filename) { |
---|
489 | if ( ! file_exists($filename) ) |
---|
490 | return false; |
---|
491 | if ( ! class_exists('DOMDocument') ) |
---|
492 | return false; |
---|
493 | |
---|
494 | $doc = new DOMDocument(); |
---|
495 | if ( $doc->load($filename) === false ) |
---|
496 | return false; |
---|
497 | $xpath = new DOMXPath($doc); |
---|
498 | $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']'); |
---|
499 | if ( $rules->length == 0 ) |
---|
500 | return false; |
---|
501 | else |
---|
502 | return true; |
---|
503 | } |
---|
504 | |
---|
505 | /** |
---|
506 | * Delete WordPress rewrite rule from web.config file if it exists there |
---|
507 | * |
---|
508 | * @since 2.8.0 |
---|
509 | * |
---|
510 | * @param string $filename Name of the configuration file |
---|
511 | * @return bool |
---|
512 | */ |
---|
513 | function iis7_delete_rewrite_rule($filename) { |
---|
514 | // If configuration file does not exist then rules also do not exist so there is nothing to delete |
---|
515 | if ( ! file_exists($filename) ) |
---|
516 | return true; |
---|
517 | |
---|
518 | if ( ! class_exists('DOMDocument') ) |
---|
519 | return false; |
---|
520 | |
---|
521 | $doc = new DOMDocument(); |
---|
522 | $doc->preserveWhiteSpace = false; |
---|
523 | |
---|
524 | if ( $doc -> load($filename) === false ) |
---|
525 | return false; |
---|
526 | $xpath = new DOMXPath($doc); |
---|
527 | $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']'); |
---|
528 | if ( $rules->length > 0 ) { |
---|
529 | $child = $rules->item(0); |
---|
530 | $parent = $child->parentNode; |
---|
531 | $parent->removeChild($child); |
---|
532 | $doc->formatOutput = true; |
---|
533 | saveDomDocument($doc, $filename); |
---|
534 | } |
---|
535 | return true; |
---|
536 | } |
---|
537 | |
---|
538 | /** |
---|
539 | * Add WordPress rewrite rule to the IIS 7 configuration file. |
---|
540 | * |
---|
541 | * @since 2.8.0 |
---|
542 | * |
---|
543 | * @param string $filename The file path to the configuration file |
---|
544 | * @param string $rewrite_rule The XML fragment with URL Rewrite rule |
---|
545 | * @return bool |
---|
546 | */ |
---|
547 | function iis7_add_rewrite_rule($filename, $rewrite_rule) { |
---|
548 | if ( ! class_exists('DOMDocument') ) |
---|
549 | return false; |
---|
550 | |
---|
551 | // If configuration file does not exist then we create one. |
---|
552 | if ( ! file_exists($filename) ) { |
---|
553 | $fp = fopen( $filename, 'w'); |
---|
554 | fwrite($fp, '<configuration/>'); |
---|
555 | fclose($fp); |
---|
556 | } |
---|
557 | |
---|
558 | $doc = new DOMDocument(); |
---|
559 | $doc->preserveWhiteSpace = false; |
---|
560 | |
---|
561 | if ( $doc->load($filename) === false ) |
---|
562 | return false; |
---|
563 | |
---|
564 | $xpath = new DOMXPath($doc); |
---|
565 | |
---|
566 | // First check if the rule already exists as in that case there is no need to re-add it |
---|
567 | $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']'); |
---|
568 | if ( $wordpress_rules->length > 0 ) |
---|
569 | return true; |
---|
570 | |
---|
571 | // Check the XPath to the rewrite rule and create XML nodes if they do not exist |
---|
572 | $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules'); |
---|
573 | if ( $xmlnodes->length > 0 ) { |
---|
574 | $rules_node = $xmlnodes->item(0); |
---|
575 | } else { |
---|
576 | $rules_node = $doc->createElement('rules'); |
---|
577 | |
---|
578 | $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite'); |
---|
579 | if ( $xmlnodes->length > 0 ) { |
---|
580 | $rewrite_node = $xmlnodes->item(0); |
---|
581 | $rewrite_node->appendChild($rules_node); |
---|
582 | } else { |
---|
583 | $rewrite_node = $doc->createElement('rewrite'); |
---|
584 | $rewrite_node->appendChild($rules_node); |
---|
585 | |
---|
586 | $xmlnodes = $xpath->query('/configuration/system.webServer'); |
---|
587 | if ( $xmlnodes->length > 0 ) { |
---|
588 | $system_webServer_node = $xmlnodes->item(0); |
---|
589 | $system_webServer_node->appendChild($rewrite_node); |
---|
590 | } else { |
---|
591 | $system_webServer_node = $doc->createElement('system.webServer'); |
---|
592 | $system_webServer_node->appendChild($rewrite_node); |
---|
593 | |
---|
594 | $xmlnodes = $xpath->query('/configuration'); |
---|
595 | if ( $xmlnodes->length > 0 ) { |
---|
596 | $config_node = $xmlnodes->item(0); |
---|
597 | $config_node->appendChild($system_webServer_node); |
---|
598 | } else { |
---|
599 | $config_node = $doc->createElement('configuration'); |
---|
600 | $doc->appendChild($config_node); |
---|
601 | $config_node->appendChild($system_webServer_node); |
---|
602 | } |
---|
603 | } |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | $rule_fragment = $doc->createDocumentFragment(); |
---|
608 | $rule_fragment->appendXML($rewrite_rule); |
---|
609 | $rules_node->appendChild($rule_fragment); |
---|
610 | |
---|
611 | $doc->encoding = "UTF-8"; |
---|
612 | $doc->formatOutput = true; |
---|
613 | saveDomDocument($doc, $filename); |
---|
614 | |
---|
615 | return true; |
---|
616 | } |
---|
617 | |
---|
618 | /** |
---|
619 | * Saves the XML document into a file |
---|
620 | * |
---|
621 | * @since 2.8.0 |
---|
622 | * |
---|
623 | * @param DOMDocument $doc |
---|
624 | * @param string $filename |
---|
625 | */ |
---|
626 | function saveDomDocument($doc, $filename) { |
---|
627 | $config = $doc->saveXML(); |
---|
628 | $config = preg_replace("/([^\r])\n/", "$1\r\n", $config); |
---|
629 | $fp = fopen($filename, 'w'); |
---|
630 | fwrite($fp, $config); |
---|
631 | fclose($fp); |
---|
632 | } |
---|
633 | |
---|
634 | /** |
---|
635 | * Workaround for Windows bug in is_writable() function |
---|
636 | * |
---|
637 | * @since 2.8.0 |
---|
638 | * |
---|
639 | * @param object $path |
---|
640 | * @return bool |
---|
641 | */ |
---|
642 | function win_is_writable($path) { |
---|
643 | /* will work in despite of Windows ACLs bug |
---|
644 | * NOTE: use a trailing slash for folders!!! |
---|
645 | * see http://bugs.php.net/bug.php?id=27609 |
---|
646 | * see http://bugs.php.net/bug.php?id=30931 |
---|
647 | */ |
---|
648 | |
---|
649 | if ( $path{strlen($path)-1} == '/' ) // recursively return a temporary file path |
---|
650 | return win_is_writable($path . uniqid(mt_rand()) . '.tmp'); |
---|
651 | else if ( is_dir($path) ) |
---|
652 | return win_is_writable($path . '/' . uniqid(mt_rand()) . '.tmp'); |
---|
653 | // check tmp file for read/write capabilities |
---|
654 | $rm = file_exists($path); |
---|
655 | $f = @fopen($path, 'a'); |
---|
656 | if ($f===false) |
---|
657 | return false; |
---|
658 | fclose($f); |
---|
659 | if ( ! $rm ) |
---|
660 | unlink($path); |
---|
661 | return true; |
---|
662 | } |
---|
663 | ?> |
---|