PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/misc.php

https://bitbucket.org/MaheshDhaduk/androidmobiles
PHP | 617 lines | 372 code | 68 blank | 177 comment | 104 complexity | f2220d3c77a2b61ec64f55f46bbdd9af MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, 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 unknown
  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 unknown
  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 unknown
  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 unknown
  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. global $wp_rewrite;
  139. $home_path = get_home_path();
  140. $web_config_file = $home_path . 'web.config';
  141. // Using win_is_writable() instead of is_writable() because of a bug in Windows PHP
  142. if ( ( ! file_exists($web_config_file) && win_is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks() ) || win_is_writable($web_config_file) ) {
  143. if ( iis7_supports_permalinks() ) {
  144. $rule = $wp_rewrite->iis7_url_rewrite_rules(false, '', '');
  145. if ( ! empty($rule) ) {
  146. return iis7_add_rewrite_rule($web_config_file, $rule);
  147. } else {
  148. return iis7_delete_rewrite_rule($web_config_file);
  149. }
  150. }
  151. }
  152. return false;
  153. }
  154. /**
  155. * {@internal Missing Short Description}}
  156. *
  157. * @since unknown
  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 unknown
  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. * {@internal Missing Short Description}}
  194. *
  195. * @since unknown
  196. *
  197. * @param unknown_type $url
  198. * @return unknown
  199. */
  200. function url_shorten( $url ) {
  201. $short_url = str_replace( 'http://', '', stripslashes( $url ));
  202. $short_url = str_replace( 'www.', '', $short_url );
  203. if ('/' == substr( $short_url, -1 ))
  204. $short_url = substr( $short_url, 0, -1 );
  205. if ( strlen( $short_url ) > 35 )
  206. $short_url = substr( $short_url, 0, 32 ).'...';
  207. return $short_url;
  208. }
  209. /**
  210. * Resets global variables based on $_GET and $_POST
  211. *
  212. * This function resets global variables based on the names passed
  213. * in the $vars array to the value of $_POST[$var] or $_GET[$var] or ''
  214. * if neither is defined.
  215. *
  216. * @since unknown
  217. *
  218. * @param array $vars An array of globals to reset.
  219. */
  220. function wp_reset_vars( $vars ) {
  221. for ( $i=0; $i<count( $vars ); $i += 1 ) {
  222. $var = $vars[$i];
  223. global $$var;
  224. if ( empty( $_POST[$var] ) ) {
  225. if ( empty( $_GET[$var] ) )
  226. $$var = '';
  227. else
  228. $$var = $_GET[$var];
  229. } else {
  230. $$var = $_POST[$var];
  231. }
  232. }
  233. }
  234. /**
  235. * {@internal Missing Short Description}}
  236. *
  237. * @since unknown
  238. *
  239. * @param unknown_type $message
  240. */
  241. function show_message($message) {
  242. if ( is_wp_error($message) ){
  243. if ( $message->get_error_data() )
  244. $message = $message->get_error_message() . ': ' . $message->get_error_data();
  245. else
  246. $message = $message->get_error_message();
  247. }
  248. echo "<p>$message</p>\n";
  249. wp_ob_end_flush_all();
  250. flush();
  251. }
  252. function wp_doc_link_parse( $content ) {
  253. if ( !is_string( $content ) || empty( $content ) )
  254. return array();
  255. if ( !function_exists('token_get_all') )
  256. return array();
  257. $tokens = token_get_all( $content );
  258. $functions = array();
  259. $ignore_functions = array();
  260. for ( $t = 0, $count = count( $tokens ); $t < $count; $t++ ) {
  261. if ( !is_array( $tokens[$t] ) ) continue;
  262. if ( T_STRING == $tokens[$t][0] && ( '(' == $tokens[ $t + 1 ] || '(' == $tokens[ $t + 2 ] ) ) {
  263. // If it's a function or class defined locally, there's not going to be any docs available
  264. 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] ) ) {
  265. $ignore_functions[] = $tokens[$t][1];
  266. }
  267. // Add this to our stack of unique references
  268. $functions[] = $tokens[$t][1];
  269. }
  270. }
  271. $functions = array_unique( $functions );
  272. sort( $functions );
  273. $ignore_functions = apply_filters( 'documentation_ignore_functions', $ignore_functions );
  274. $ignore_functions = array_unique( $ignore_functions );
  275. $out = array();
  276. foreach ( $functions as $function ) {
  277. if ( in_array( $function, $ignore_functions ) )
  278. continue;
  279. $out[] = $function;
  280. }
  281. return $out;
  282. }
  283. /**
  284. * Saves option for number of rows when listing posts, pages, comments, etc.
  285. *
  286. * @since 2.8
  287. **/
  288. function set_screen_options() {
  289. if ( isset($_POST['wp_screen_options']) && is_array($_POST['wp_screen_options']) ) {
  290. check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
  291. if ( !$user = wp_get_current_user() )
  292. return;
  293. $option = $_POST['wp_screen_options']['option'];
  294. $value = $_POST['wp_screen_options']['value'];
  295. if ( !preg_match( '/^[a-z_-]+$/', $option ) )
  296. return;
  297. $option = str_replace('-', '_', $option);
  298. $map_option = $option;
  299. $type = str_replace('edit_', '', $map_option);
  300. $type = str_replace('_per_page', '', $type);
  301. if ( in_array($type, get_post_types()) )
  302. $map_option = 'edit_per_page';
  303. if ( in_array( $type, get_taxonomies()) )
  304. $map_option = 'edit_tags_per_page';
  305. switch ( $map_option ) {
  306. case 'edit_per_page':
  307. case 'ms_sites_per_page':
  308. case 'ms_users_per_page':
  309. case 'edit_comments_per_page':
  310. case 'upload_per_page':
  311. case 'edit_tags_per_page':
  312. case 'plugins_per_page':
  313. $value = (int) $value;
  314. if ( $value < 1 || $value > 999 )
  315. return;
  316. break;
  317. default:
  318. $value = apply_filters('set-screen-option', false, $option, $value);
  319. if ( false === $value )
  320. return;
  321. break;
  322. }
  323. update_user_meta($user->ID, $option, $value);
  324. wp_redirect( remove_query_arg( array('pagenum', 'apage', 'paged'), wp_get_referer() ) );
  325. exit;
  326. }
  327. }
  328. function wp_menu_unfold() {
  329. if ( isset($_GET['unfoldmenu']) ) {
  330. delete_user_setting('mfold');
  331. wp_redirect( remove_query_arg( 'unfoldmenu', stripslashes($_SERVER['REQUEST_URI']) ) );
  332. exit;
  333. }
  334. }
  335. /**
  336. * Check if IIS 7 supports pretty permalinks
  337. *
  338. * @since 2.8.0
  339. *
  340. * @return bool
  341. */
  342. function iis7_supports_permalinks() {
  343. global $is_iis7;
  344. $supports_permalinks = false;
  345. if ( $is_iis7 ) {
  346. /* First we check if the DOMDocument class exists. If it does not exist,
  347. * which is the case for PHP 4.X, then we cannot easily update the xml configuration file,
  348. * hence we just bail out and tell user that pretty permalinks cannot be used.
  349. * This is not a big issue because PHP 4.X is going to be depricated and for IIS it
  350. * is recommended to use PHP 5.X NTS.
  351. * Next we check if the URL Rewrite Module 1.1 is loaded and enabled for the web site. When
  352. * URL Rewrite 1.1 is loaded it always sets a server variable called 'IIS_UrlRewriteModule'.
  353. * Lastly we make sure that PHP is running via FastCGI. This is important because if it runs
  354. * via ISAPI then pretty permalinks will not work.
  355. */
  356. $supports_permalinks = class_exists('DOMDocument') && isset($_SERVER['IIS_UrlRewriteModule']) && ( php_sapi_name() == 'cgi-fcgi' );
  357. }
  358. return apply_filters('iis7_supports_permalinks', $supports_permalinks);
  359. }
  360. /**
  361. * Check if rewrite rule for WordPress already exists in the IIS 7 configuration file
  362. *
  363. * @since 2.8.0
  364. *
  365. * @return bool
  366. * @param string $filename The file path to the configuration file
  367. */
  368. function iis7_rewrite_rule_exists($filename) {
  369. if ( ! file_exists($filename) )
  370. return false;
  371. if ( ! class_exists('DOMDocument') )
  372. return false;
  373. $doc = new DOMDocument();
  374. if ( $doc->load($filename) === false )
  375. return false;
  376. $xpath = new DOMXPath($doc);
  377. $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
  378. if ( $rules->length == 0 )
  379. return false;
  380. else
  381. return true;
  382. }
  383. /**
  384. * Delete WordPress rewrite rule from web.config file if it exists there
  385. *
  386. * @since 2.8.0
  387. *
  388. * @param string $filename Name of the configuration file
  389. * @return bool
  390. */
  391. function iis7_delete_rewrite_rule($filename) {
  392. // If configuration file does not exist then rules also do not exist so there is nothing to delete
  393. if ( ! file_exists($filename) )
  394. return true;
  395. if ( ! class_exists('DOMDocument') )
  396. return false;
  397. $doc = new DOMDocument();
  398. $doc->preserveWhiteSpace = false;
  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. $child = $rules->item(0);
  405. $parent = $child->parentNode;
  406. $parent->removeChild($child);
  407. $doc->formatOutput = true;
  408. saveDomDocument($doc, $filename);
  409. }
  410. return true;
  411. }
  412. /**
  413. * Add WordPress rewrite rule to the IIS 7 configuration file.
  414. *
  415. * @since 2.8.0
  416. *
  417. * @param string $filename The file path to the configuration file
  418. * @param string $rewrite_rule The XML fragment with URL Rewrite rule
  419. * @return bool
  420. */
  421. function iis7_add_rewrite_rule($filename, $rewrite_rule) {
  422. if ( ! class_exists('DOMDocument') )
  423. return false;
  424. // If configuration file does not exist then we create one.
  425. if ( ! file_exists($filename) ) {
  426. $fp = fopen( $filename, 'w');
  427. fwrite($fp, '<configuration/>');
  428. fclose($fp);
  429. }
  430. $doc = new DOMDocument();
  431. $doc->preserveWhiteSpace = false;
  432. if ( $doc->load($filename) === false )
  433. return false;
  434. $xpath = new DOMXPath($doc);
  435. // First check if the rule already exists as in that case there is no need to re-add it
  436. $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
  437. if ( $wordpress_rules->length > 0 )
  438. return true;
  439. // Check the XPath to the rewrite rule and create XML nodes if they do not exist
  440. $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
  441. if ( $xmlnodes->length > 0 ) {
  442. $rules_node = $xmlnodes->item(0);
  443. } else {
  444. $rules_node = $doc->createElement('rules');
  445. $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite');
  446. if ( $xmlnodes->length > 0 ) {
  447. $rewrite_node = $xmlnodes->item(0);
  448. $rewrite_node->appendChild($rules_node);
  449. } else {
  450. $rewrite_node = $doc->createElement('rewrite');
  451. $rewrite_node->appendChild($rules_node);
  452. $xmlnodes = $xpath->query('/configuration/system.webServer');
  453. if ( $xmlnodes->length > 0 ) {
  454. $system_webServer_node = $xmlnodes->item(0);
  455. $system_webServer_node->appendChild($rewrite_node);
  456. } else {
  457. $system_webServer_node = $doc->createElement('system.webServer');
  458. $system_webServer_node->appendChild($rewrite_node);
  459. $xmlnodes = $xpath->query('/configuration');
  460. if ( $xmlnodes->length > 0 ) {
  461. $config_node = $xmlnodes->item(0);
  462. $config_node->appendChild($system_webServer_node);
  463. } else {
  464. $config_node = $doc->createElement('configuration');
  465. $doc->appendChild($config_node);
  466. $config_node->appendChild($system_webServer_node);
  467. }
  468. }
  469. }
  470. }
  471. $rule_fragment = $doc->createDocumentFragment();
  472. $rule_fragment->appendXML($rewrite_rule);
  473. $rules_node->appendChild($rule_fragment);
  474. $doc->encoding = "UTF-8";
  475. $doc->formatOutput = true;
  476. saveDomDocument($doc, $filename);
  477. return true;
  478. }
  479. /**
  480. * Saves the XML document into a file
  481. *
  482. * @since 2.8.0
  483. *
  484. * @param DOMDocument $doc
  485. * @param string $filename
  486. */
  487. function saveDomDocument($doc, $filename) {
  488. $config = $doc->saveXML();
  489. $config = preg_replace("/([^\r])\n/", "$1\r\n", $config);
  490. $fp = fopen($filename, 'w');
  491. fwrite($fp, $config);
  492. fclose($fp);
  493. }
  494. /**
  495. * Workaround for Windows bug in is_writable() function
  496. *
  497. * @since 2.8.0
  498. *
  499. * @param object $path
  500. * @return bool
  501. */
  502. function win_is_writable($path) {
  503. /* will work in despite of Windows ACLs bug
  504. * NOTE: use a trailing slash for folders!!!
  505. * see http://bugs.php.net/bug.php?id=27609
  506. * see http://bugs.php.net/bug.php?id=30931
  507. */
  508. if ( $path{strlen($path)-1} == '/' ) // recursively return a temporary file path
  509. return win_is_writable($path . uniqid(mt_rand()) . '.tmp');
  510. else if ( is_dir($path) )
  511. return win_is_writable($path . '/' . uniqid(mt_rand()) . '.tmp');
  512. // check tmp file for read/write capabilities
  513. $rm = file_exists($path);
  514. $f = @fopen($path, 'a');
  515. if ($f===false)
  516. return false;
  517. fclose($f);
  518. if ( ! $rm )
  519. unlink($path);
  520. return true;
  521. }
  522. /**
  523. * Display the default admin color scheme picker (Used in user-edit.php)
  524. *
  525. * @since 3.0.0
  526. */
  527. function admin_color_scheme_picker() {
  528. global $_wp_admin_css_colors, $user_id; ?>
  529. <fieldset><legend class="screen-reader-text"><span><?php _e('Admin Color Scheme')?></span></legend>
  530. <?php
  531. $current_color = get_user_option('admin_color', $user_id);
  532. if ( empty($current_color) )
  533. $current_color = 'fresh';
  534. foreach ( $_wp_admin_css_colors as $color => $color_info ): ?>
  535. <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); ?> />
  536. <table class="color-palette">
  537. <tr>
  538. <?php foreach ( $color_info->colors as $html_color ): ?>
  539. <td style="background-color: <?php echo $html_color ?>" title="<?php echo $color ?>">&nbsp;</td>
  540. <?php endforeach; ?>
  541. </tr>
  542. </table>
  543. <label for="admin_color_<?php echo $color; ?>"><?php echo $color_info->name ?></label>
  544. </div>
  545. <?php endforeach; ?>
  546. </fieldset>
  547. <?php
  548. }
  549. ?>