tool-file-editor.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. if ( ! defined( 'WPSEO_VERSION' ) ) {
  8. header( 'Status: 403 Forbidden' );
  9. header( 'HTTP/1.1 403 Forbidden' );
  10. exit();
  11. }
  12. $yform = Yoast_Form::get_instance();
  13. $robots_file = get_home_path() . 'robots.txt';
  14. $ht_access_file = get_home_path() . '.htaccess';
  15. if ( isset( $_POST['create_robots'] ) ) {
  16. if ( ! current_user_can( 'edit_files' ) ) {
  17. $die_msg = sprintf(
  18. /* translators: %s expands to robots.txt. */
  19. __( 'You cannot create a %s file.', 'wordpress-seo' ),
  20. 'robots.txt'
  21. );
  22. die( esc_html( $die_msg ) );
  23. }
  24. check_admin_referer( 'wpseo_create_robots' );
  25. ob_start();
  26. error_reporting( 0 );
  27. do_robots();
  28. $robots_content = ob_get_clean();
  29. $f = fopen( $robots_file, 'x' );
  30. fwrite( $f, $robots_content );
  31. }
  32. if ( isset( $_POST['submitrobots'] ) ) {
  33. if ( ! current_user_can( 'edit_files' ) ) {
  34. $die_msg = sprintf(
  35. /* translators: %s expands to robots.txt. */
  36. __( 'You cannot edit the %s file.', 'wordpress-seo' ),
  37. 'robots.txt'
  38. );
  39. die( esc_html( $die_msg ) );
  40. }
  41. check_admin_referer( 'wpseo-robotstxt' );
  42. if ( file_exists( $robots_file ) ) {
  43. $robotsnew = stripslashes( $_POST['robotsnew'] );
  44. if ( is_writable( $robots_file ) ) {
  45. $f = fopen( $robots_file, 'w+' );
  46. fwrite( $f, $robotsnew );
  47. fclose( $f );
  48. $msg = sprintf(
  49. /* translators: %s expands to robots.txt. */
  50. __( 'Updated %s', 'wordpress-seo' ),
  51. 'robots.txt'
  52. );
  53. }
  54. }
  55. }
  56. if ( isset( $_POST['submithtaccess'] ) ) {
  57. if ( ! current_user_can( 'edit_files' ) ) {
  58. $die_msg = sprintf(
  59. /* translators: %s expands to ".htaccess". */
  60. __( 'You cannot edit the %s file.', 'wordpress-seo' ),
  61. '.htaccess'
  62. );
  63. die( esc_html( $die_msg ) );
  64. }
  65. check_admin_referer( 'wpseo-htaccess' );
  66. if ( file_exists( $ht_access_file ) ) {
  67. $ht_access_new = stripslashes( $_POST['htaccessnew'] );
  68. if ( is_writeable( $ht_access_file ) ) {
  69. $f = fopen( $ht_access_file, 'w+' );
  70. fwrite( $f, $ht_access_new );
  71. fclose( $f );
  72. }
  73. }
  74. }
  75. if ( is_multisite() ) {
  76. $action_url = network_admin_url( 'admin.php?page=wpseo_files' );
  77. $yform->admin_header( false, 'wpseo_ms' );
  78. }
  79. else {
  80. $action_url = admin_url( 'admin.php?page=wpseo_tools&tool=file-editor' );
  81. }
  82. if ( isset( $msg ) && ! empty( $msg ) ) {
  83. echo '<div id="message" class="notice notice-success"><p>', esc_html( $msg ), '</p></div>';
  84. }
  85. $helpcenter_tab = new WPSEO_Option_Tab( 'bulk-editor', __( 'Bulk editor', 'wordpress-seo' ),
  86. array( 'video_url' => WPSEO_Shortlinker::get( 'https://yoa.st/screencast-tools-file-editor' ) ) );
  87. $helpcenter = new WPSEO_Help_Center( 'bulk-editor', $helpcenter_tab, WPSEO_Utils::is_yoast_seo_premium() );
  88. $helpcenter->localize_data();
  89. $helpcenter->mount();
  90. // N.B.: "robots.txt" is a fixed file name and should not be translatable.
  91. echo '<h2>robots.txt</h2>';
  92. if ( ! file_exists( $robots_file ) ) {
  93. if ( is_writable( get_home_path() ) ) {
  94. echo '<form action="', esc_url( $action_url ), '" method="post" id="robotstxtcreateform">';
  95. wp_nonce_field( 'wpseo_create_robots', '_wpnonce', true, true );
  96. echo '<p>';
  97. printf(
  98. /* translators: %s expands to robots.txt. */
  99. esc_html__( 'You don\'t have a %s file, create one here:', 'wordpress-seo' ),
  100. 'robots.txt'
  101. );
  102. echo '</p>';
  103. printf(
  104. '<input type="submit" class="button" name="create_robots" value="%s">',
  105. sprintf(
  106. /* translators: %s expands to robots.txt. */
  107. esc_attr__( 'Create %s file', 'wordpress-seo' ),
  108. 'robots.txt'
  109. )
  110. );
  111. echo '</form>';
  112. }
  113. else {
  114. echo '<p>';
  115. printf(
  116. /* translators: %s expands to robots.txt. */
  117. esc_html__( 'If you had a %s file and it was editable, you could edit it from here.', 'wordpress-seo' ),
  118. 'robots.txt'
  119. );
  120. echo '</p>';
  121. }
  122. }
  123. else {
  124. $f = fopen( $robots_file, 'r' );
  125. $content = '';
  126. if ( filesize( $robots_file ) > 0 ) {
  127. $content = fread( $f, filesize( $robots_file ) );
  128. }
  129. if ( ! is_writable( $robots_file ) ) {
  130. echo '<p><em>';
  131. printf(
  132. /* translators: %s expands to robots.txt. */
  133. esc_html__( 'If your %s were writable, you could edit it from here.', 'wordpress-seo' ),
  134. 'robots.txt'
  135. );
  136. echo '</em></p>';
  137. echo '<textarea class="large-text code" disabled="disabled" rows="15" name="robotsnew">', esc_textarea( $content ), '</textarea><br/>';
  138. }
  139. else {
  140. echo '<form action="', esc_url( $action_url ), '" method="post" id="robotstxtform">';
  141. wp_nonce_field( 'wpseo-robotstxt', '_wpnonce', true, true );
  142. echo '<p><label for="robotsnew" class="yoast-inline-label">';
  143. printf(
  144. /* translators: %s expands to robots.txt. */
  145. esc_html__( 'Edit the content of your %s:', 'wordpress-seo' ),
  146. 'robots.txt'
  147. );
  148. echo '</label></p>';
  149. echo '<textarea class="large-text code" rows="15" name="robotsnew" id="robotsnew">', esc_textarea( $content ), '</textarea><br/>';
  150. printf(
  151. '<div class="submit"><input class="button" type="submit" name="submitrobots" value="%s" /></div>',
  152. sprintf(
  153. /* translators: %s expands to robots.txt. */
  154. esc_attr__( 'Save changes to %s', 'wordpress-seo' ),
  155. 'robots.txt'
  156. )
  157. );
  158. echo '</form>';
  159. }
  160. }
  161. if ( ( isset( $_SERVER['SERVER_SOFTWARE'] ) && stristr( $_SERVER['SERVER_SOFTWARE'], 'nginx' ) === false ) ) {
  162. echo '<h2>';
  163. printf(
  164. /* translators: %s expands to ".htaccess". */
  165. esc_html__( '%s file', 'wordpress-seo' ),
  166. '.htaccess'
  167. );
  168. echo '</h2>';
  169. if ( file_exists( $ht_access_file ) ) {
  170. $f = fopen( $ht_access_file, 'r' );
  171. $contentht = '';
  172. if ( filesize( $ht_access_file ) > 0 ) {
  173. $contentht = fread( $f, filesize( $ht_access_file ) );
  174. }
  175. if ( ! is_writable( $ht_access_file ) ) {
  176. echo '<p><em>';
  177. printf(
  178. /* translators: %s expands to ".htaccess". */
  179. esc_html__( 'If your %s were writable, you could edit it from here.', 'wordpress-seo' ),
  180. '.htaccess'
  181. );
  182. echo '</em></p>';
  183. echo '<textarea class="large-text code" disabled="disabled" rows="15" name="robotsnew">', esc_textarea( $contentht ), '</textarea><br/>';
  184. }
  185. else {
  186. echo '<form action="', esc_url( $action_url ), '" method="post" id="htaccessform">';
  187. wp_nonce_field( 'wpseo-htaccess', '_wpnonce', true, true );
  188. echo '<p><label for="htaccessnew" class="yoast-inline-label">';
  189. printf(
  190. /* translators: %s expands to ".htaccess". */
  191. esc_html__( 'Edit the content of your %s:', 'wordpress-seo' ),
  192. '.htaccess'
  193. );
  194. echo '</label></p>';
  195. echo '<textarea class="large-text code" rows="15" name="htaccessnew" id="htaccessnew">', esc_textarea( $contentht ), '</textarea><br/>';
  196. printf(
  197. '<div class="submit"><input class="button" type="submit" name="submithtaccess" value="%s" /></div>',
  198. sprintf(
  199. /* translators: %s expands to ".htaccess". */
  200. esc_attr__( 'Save changes to %s', 'wordpress-seo' ),
  201. '.htaccess'
  202. )
  203. );
  204. echo '</form>';
  205. }
  206. }
  207. else {
  208. echo '<p>';
  209. printf(
  210. /* translators: %s expands to ".htaccess". */
  211. esc_html__( 'If you had a %s file and it was editable, you could edit it from here.', 'wordpress-seo' ),
  212. '.htaccess'
  213. );
  214. echo '</p>';
  215. }
  216. }
  217. if ( is_multisite() ) {
  218. $yform->admin_footer( false );
  219. }