PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/blog/wp-admin/includes/misc.php

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