PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/temp/wordpress/wp-admin/includes/misc.php

https://bitbucket.org/broderboy/shannonbroder-wordpress
PHP | 592 lines | 367 code | 64 blank | 161 comment | 99 complexity | 61e50ddf2a155982e75e9e064a0c81cd MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, AGPL-1.0
  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 string $old_value
  181. * @param string $value
  182. */
  183. function update_home_siteurl( $old_value, $value ) {
  184. if ( defined( "WP_INSTALLING" ) )
  185. return;
  186. // If home changed, write rewrite rules to new location.
  187. flush_rewrite_rules();
  188. }
  189. add_action( 'update_option_home', 'update_home_siteurl', 10, 2 );
  190. add_action( 'update_option_siteurl', 'update_home_siteurl', 10, 2 );
  191. /**
  192. * Shorten an URL, to be used as link text
  193. *
  194. * @since 1.2.1
  195. *
  196. * @param string $url
  197. * @return string
  198. */
  199. function url_shorten( $url ) {
  200. $short_url = str_replace( 'http://', '', stripslashes( $url ));
  201. $short_url = str_replace( 'www.', '', $short_url );
  202. $short_url = untrailingslashit( $short_url );
  203. if ( strlen( $short_url ) > 35 )
  204. $short_url = substr( $short_url, 0, 32 ) . '...';
  205. return $short_url;
  206. }
  207. /**
  208. * Resets global variables based on $_GET and $_POST
  209. *
  210. * This function resets global variables based on the names passed
  211. * in the $vars array to the value of $_POST[$var] or $_GET[$var] or ''
  212. * if neither is defined.
  213. *
  214. * @since 2.0.0
  215. *
  216. * @param array $vars An array of globals to reset.
  217. */
  218. function wp_reset_vars( $vars ) {
  219. for ( $i=0; $i<count( $vars ); $i += 1 ) {
  220. $var = $vars[$i];
  221. global $$var;
  222. if ( empty( $_POST[$var] ) ) {
  223. if ( empty( $_GET[$var] ) )
  224. $$var = '';
  225. else
  226. $$var = $_GET[$var];
  227. } else {
  228. $$var = $_POST[$var];
  229. }
  230. }
  231. }
  232. /**
  233. * {@internal Missing Short Description}}
  234. *
  235. * @since 2.1.0
  236. *
  237. * @param unknown_type $message
  238. */
  239. function show_message($message) {
  240. if ( is_wp_error($message) ){
  241. if ( $message->get_error_data() )
  242. $message = $message->get_error_message() . ': ' . $message->get_error_data();
  243. else
  244. $message = $message->get_error_message();
  245. }
  246. echo "<p>$message</p>\n";
  247. wp_ob_end_flush_all();
  248. flush();
  249. }
  250. function wp_doc_link_parse( $content ) {
  251. if ( !is_string( $content ) || empty( $content ) )
  252. return array();
  253. if ( !function_exists('token_get_all') )
  254. return array();
  255. $tokens = token_get_all( $content );
  256. $functions = array();
  257. $ignore_functions = array();
  258. for ( $t = 0, $count = count( $tokens ); $t < $count; $t++ ) {
  259. if ( !is_array( $tokens[$t] ) ) continue;
  260. if ( T_STRING == $tokens[$t][0] && ( '(' == $tokens[ $t + 1 ] || '(' == $tokens[ $t + 2 ] ) ) {
  261. // If it's a function or class defined locally, there's not going to be any docs available
  262. 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] ) ) {
  263. $ignore_functions[] = $tokens[$t][1];
  264. }
  265. // Add this to our stack of unique references
  266. $functions[] = $tokens[$t][1];
  267. }
  268. }
  269. $functions = array_unique( $functions );
  270. sort( $functions );
  271. $ignore_functions = apply_filters( 'documentation_ignore_functions', $ignore_functions );
  272. $ignore_functions = array_unique( $ignore_functions );
  273. $out = array();
  274. foreach ( $functions as $function ) {
  275. if ( in_array( $function, $ignore_functions ) )
  276. continue;
  277. $out[] = $function;
  278. }
  279. return $out;
  280. }
  281. /**
  282. * Saves option for number of rows when listing posts, pages, comments, etc.
  283. *
  284. * @since 2.8
  285. **/
  286. function set_screen_options() {
  287. if ( isset($_POST['wp_screen_options']) && is_array($_POST['wp_screen_options']) ) {
  288. check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
  289. if ( !$user = wp_get_current_user() )
  290. return;
  291. $option = $_POST['wp_screen_options']['option'];
  292. $value = $_POST['wp_screen_options']['value'];
  293. if ( !preg_match( '/^[a-z_-]+$/', $option ) )
  294. return;
  295. $option = str_replace('-', '_', $option);
  296. $map_option = $option;
  297. $type = str_replace('edit_', '', $map_option);
  298. $type = str_replace('_per_page', '', $type);
  299. if ( in_array($type, get_post_types()) )
  300. $map_option = 'edit_per_page';
  301. if ( in_array( $type, get_taxonomies()) )
  302. $map_option = 'edit_tags_per_page';
  303. switch ( $map_option ) {
  304. case 'edit_per_page':
  305. case 'users_per_page':
  306. case 'edit_comments_per_page':
  307. case 'upload_per_page':
  308. case 'edit_tags_per_page':
  309. case 'plugins_per_page':
  310. // Network admin
  311. case 'sites_network_per_page':
  312. case 'users_network_per_page':
  313. case 'site_users_network_per_page':
  314. case 'plugins_network_per_page':
  315. case 'themes_network_per_page':
  316. case 'site_themes_network_per_page':
  317. $value = (int) $value;
  318. if ( $value < 1 || $value > 999 )
  319. return;
  320. break;
  321. default:
  322. $value = apply_filters('set-screen-option', false, $option, $value);
  323. if ( false === $value )
  324. return;
  325. break;
  326. }
  327. update_user_meta($user->ID, $option, $value);
  328. wp_safe_redirect( remove_query_arg( array('pagenum', 'apage', 'paged'), wp_get_referer() ) );
  329. exit;
  330. }
  331. }
  332. /**
  333. * Check if rewrite rule for WordPress already exists in the IIS 7 configuration file
  334. *
  335. * @since 2.8.0
  336. *
  337. * @return bool
  338. * @param string $filename The file path to the configuration file
  339. */
  340. function iis7_rewrite_rule_exists($filename) {
  341. if ( ! file_exists($filename) )
  342. return false;
  343. if ( ! class_exists('DOMDocument') )
  344. return false;
  345. $doc = new DOMDocument();
  346. if ( $doc->load($filename) === false )
  347. return false;
  348. $xpath = new DOMXPath($doc);
  349. $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
  350. if ( $rules->length == 0 )
  351. return false;
  352. else
  353. return true;
  354. }
  355. /**
  356. * Delete WordPress rewrite rule from web.config file if it exists there
  357. *
  358. * @since 2.8.0
  359. *
  360. * @param string $filename Name of the configuration file
  361. * @return bool
  362. */
  363. function iis7_delete_rewrite_rule($filename) {
  364. // If configuration file does not exist then rules also do not exist so there is nothing to delete
  365. if ( ! file_exists($filename) )
  366. return true;
  367. if ( ! class_exists('DOMDocument') )
  368. return false;
  369. $doc = new DOMDocument();
  370. $doc->preserveWhiteSpace = false;
  371. if ( $doc -> load($filename) === false )
  372. return false;
  373. $xpath = new DOMXPath($doc);
  374. $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
  375. if ( $rules->length > 0 ) {
  376. $child = $rules->item(0);
  377. $parent = $child->parentNode;
  378. $parent->removeChild($child);
  379. $doc->formatOutput = true;
  380. saveDomDocument($doc, $filename);
  381. }
  382. return true;
  383. }
  384. /**
  385. * Add WordPress rewrite rule to the IIS 7 configuration file.
  386. *
  387. * @since 2.8.0
  388. *
  389. * @param string $filename The file path to the configuration file
  390. * @param string $rewrite_rule The XML fragment with URL Rewrite rule
  391. * @return bool
  392. */
  393. function iis7_add_rewrite_rule($filename, $rewrite_rule) {
  394. if ( ! class_exists('DOMDocument') )
  395. return false;
  396. // If configuration file does not exist then we create one.
  397. if ( ! file_exists($filename) ) {
  398. $fp = fopen( $filename, 'w');
  399. fwrite($fp, '<configuration/>');
  400. fclose($fp);
  401. }
  402. $doc = new DOMDocument();
  403. $doc->preserveWhiteSpace = false;
  404. if ( $doc->load($filename) === false )
  405. return false;
  406. $xpath = new DOMXPath($doc);
  407. // First check if the rule already exists as in that case there is no need to re-add it
  408. $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
  409. if ( $wordpress_rules->length > 0 )
  410. return true;
  411. // Check the XPath to the rewrite rule and create XML nodes if they do not exist
  412. $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
  413. if ( $xmlnodes->length > 0 ) {
  414. $rules_node = $xmlnodes->item(0);
  415. } else {
  416. $rules_node = $doc->createElement('rules');
  417. $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite');
  418. if ( $xmlnodes->length > 0 ) {
  419. $rewrite_node = $xmlnodes->item(0);
  420. $rewrite_node->appendChild($rules_node);
  421. } else {
  422. $rewrite_node = $doc->createElement('rewrite');
  423. $rewrite_node->appendChild($rules_node);
  424. $xmlnodes = $xpath->query('/configuration/system.webServer');
  425. if ( $xmlnodes->length > 0 ) {
  426. $system_webServer_node = $xmlnodes->item(0);
  427. $system_webServer_node->appendChild($rewrite_node);
  428. } else {
  429. $system_webServer_node = $doc->createElement('system.webServer');
  430. $system_webServer_node->appendChild($rewrite_node);
  431. $xmlnodes = $xpath->query('/configuration');
  432. if ( $xmlnodes->length > 0 ) {
  433. $config_node = $xmlnodes->item(0);
  434. $config_node->appendChild($system_webServer_node);
  435. } else {
  436. $config_node = $doc->createElement('configuration');
  437. $doc->appendChild($config_node);
  438. $config_node->appendChild($system_webServer_node);
  439. }
  440. }
  441. }
  442. }
  443. $rule_fragment = $doc->createDocumentFragment();
  444. $rule_fragment->appendXML($rewrite_rule);
  445. $rules_node->appendChild($rule_fragment);
  446. $doc->encoding = "UTF-8";
  447. $doc->formatOutput = true;
  448. saveDomDocument($doc, $filename);
  449. return true;
  450. }
  451. /**
  452. * Saves the XML document into a file
  453. *
  454. * @since 2.8.0
  455. *
  456. * @param DOMDocument $doc
  457. * @param string $filename
  458. */
  459. function saveDomDocument($doc, $filename) {
  460. $config = $doc->saveXML();
  461. $config = preg_replace("/([^\r])\n/", "$1\r\n", $config);
  462. $fp = fopen($filename, 'w');
  463. fwrite($fp, $config);
  464. fclose($fp);
  465. }
  466. /**
  467. * Workaround for Windows bug in is_writable() function
  468. *
  469. * @since 2.8.0
  470. *
  471. * @param string $path
  472. * @return bool
  473. */
  474. function win_is_writable( $path ) {
  475. /* will work in despite of Windows ACLs bug
  476. * NOTE: use a trailing slash for folders!!!
  477. * see http://bugs.php.net/bug.php?id=27609
  478. * see http://bugs.php.net/bug.php?id=30931
  479. */
  480. if ( $path[strlen( $path ) - 1] == '/' ) // recursively return a temporary file path
  481. return win_is_writable( $path . uniqid( mt_rand() ) . '.tmp');
  482. else if ( is_dir( $path ) )
  483. return win_is_writable( $path . '/' . uniqid( mt_rand() ) . '.tmp' );
  484. // check tmp file for read/write capabilities
  485. $should_delete_tmp_file = !file_exists( $path );
  486. $f = @fopen( $path, 'a' );
  487. if ( $f === false )
  488. return false;
  489. fclose( $f );
  490. if ( $should_delete_tmp_file )
  491. unlink( $path );
  492. return true;
  493. }
  494. /**
  495. * Display the default admin color scheme picker (Used in user-edit.php)
  496. *
  497. * @since 3.0.0
  498. */
  499. function admin_color_scheme_picker() {
  500. global $_wp_admin_css_colors, $user_id; ?>
  501. <fieldset><legend class="screen-reader-text"><span><?php _e('Admin Color Scheme')?></span></legend>
  502. <?php
  503. $current_color = get_user_option('admin_color', $user_id);
  504. if ( empty($current_color) )
  505. $current_color = 'fresh';
  506. foreach ( $_wp_admin_css_colors as $color => $color_info ): ?>
  507. <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); ?> />
  508. <table class="color-palette">
  509. <tr>
  510. <?php foreach ( $color_info->colors as $html_color ): ?>
  511. <td style="background-color: <?php echo $html_color ?>" title="<?php echo $color ?>">&nbsp;</td>
  512. <?php endforeach; ?>
  513. </tr>
  514. </table>
  515. <label for="admin_color_<?php echo $color; ?>"><?php echo $color_info->name ?></label>
  516. </div>
  517. <?php endforeach; ?>
  518. </fieldset>
  519. <?php
  520. }
  521. function _ipad_meta() {
  522. if ( wp_is_mobile() ) {
  523. ?>
  524. <meta name="viewport" id="viewport-meta" content="width=device-width, initial-scale=1">
  525. <?php
  526. }
  527. }
  528. add_action('admin_head', '_ipad_meta');