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