/wp-content/plugins/wordpress-seo/admin/views/tool-file-editor.php

https://bitbucket.org/wedodigital/wedo-proofs · PHP · 239 lines · 194 code · 26 blank · 19 comment · 29 complexity · 3b56f0401fc832c1d0facdb21c6d7588 MD5 · raw file

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