Tengo un onClick jquery $.post() básico que se usa en un complemento de wordpress que estoy desarrollando y que el servidor del sitio niega al archivo php asociado.
Aquí está el jQuery:
$('.pnb-min').click(function(){
var folder = $(this).data('folder');
var url="url to parsing file";
var postit = $.post( url, {folder:folder});
postit.done(function( data ) {alert(data);});
});
… y el html desencadenante
<li id="" style="cursor:pointer;" class="pnb-min pnb-expand" data-folder="css">Refresh Stylesheets</li>
… y el archivo de análisis php:
<?php
# header('Access-Control-Allow-Origin: https://s8w.org/wp-content/plugins/pnbWidgets/widgets/file-manager/dashboard.page.php,Vary: Origin');
header('Access-Control-Allow-Origin: https://s8w.org/wp-admin/admin.php?page=file-manager-dashboard,Vary: Origin');
# header('Access-Control-Allow-Origin:*');
/* CODE MINIMIZER
------------------------------------------------------------------------- */
defined('ABSPATH') or die ('You are not authorized to access this script.');
$asbP = ABSPATH . 'wp-content/plugins/pnbWidgets/core/';
$asbU = 'https://s8w.org/wp-content/plugins/pnbWidgets/core/';
/* aquire folder to minimize from jquery post on button click
---------------------------------------------------------------- */
$folder = $_POST['which'];
/* get code
---------------------------------------------------------------- */
#initialize vars
$common_styles=""; $admin_styles=""; $common_js=""; $admin_js=""; $common_js=""; $admin_js="";
# common styles
foreach(glob($asbP . '/css/common/*.css') as $file) {$common_styles .= file_get_contents($file);}
# admin styles
foreach(glob($asbP . '/css/admin/*.css') as $file) {$admin_styles .= file_get_contents($file);}
# common jquery
foreach(glob($asbP . '/js/common/*.php') as $file) {$common_js .= file_get_contents($file);}
# admin jquery
foreach(glob($asbP . '/js/admin/*.php') as $file) {$admin_js .= file_get_contents($file);}
# common functions
foreach(glob($asbP . '/functions/common/*.php') as $file) {$common_js .= file_get_contents($file);}
# admin functions
foreach(glob($asbP . '/functions/admin/*.php') as $file) {$admin_js .= file_get_contents($file);}
/* stylesheets
---------------------------------------------------------------- */
if($folder == 'css'){
# minimize common styles
$code = str_replace('/*',"~",str_replace('*/',"~",$common_styles)); preg_match_all("'~(.*?)~'si", $code, $match);
foreach($match[1] as $val) {$this_code="~".$val.'~';$code = str_replace($this_code,"",$code);}
$code = str_replace("r","",str_replace("n","",str_replace("t","",$code)));
$common_styles_min = $code;
# minimize admin styles
$code = str_replace('/*',"~",str_replace('*/',"~",$admin_styles)); preg_match_all("'~(.*?)~'si", $code, $match);
foreach($match[1] as $val) {$this_code="~".$val.'~';$code = str_replace($this_code,"",$code);}
$code = str_replace("r","",str_replace("n","",str_replace("t","",$code)));
$admin_styles_min = $common_styles_min.$code; $admin_styles = $common_styles.$admin_styles;
$url = $asbU.'css/';
# backup existing files
copy($url.'common.css', $url.'backup/common-bu.css');
copy($url.'common-min.css', $url.'backup/common.min-bu.css');
copy($url.'admin.css', $url.'backup/admin-bu.css');
copy($url.'admin-min.css', $url.'backup/admin.min-bu.css');
# write common stylesheets
$handle = fopen($url.'common.css', 'w+'); fwrite($handle, $common_styles); fclose($handle);
$handle = fopen($url.'common.min.css', 'w+'); fwrite($handle, $common_styles_min); fclose($handle);
# write admin stylesheets
$handle = fopen($url.'admin.css', 'w+'); fwrite($handle, $admin_styles); fclose($handle);
$handle = fopen($url.'admin.min.css', 'w+'); fwrite($handle, $admin_styles_min); fclose($handle);
$msg = 'All stylesheets have been updated.';
}
/* jQuery
---------------------------------------------------------------- */
if($folder == 'js'){
# minimize common js
$code = str_replace('/*',"~",str_replace('*/',"~",$common_js)); preg_match_all("'~(.*?)~'si", $code, $match);
foreach($match[1] as $val) {$this_code="~".$val.'~';$code = str_replace($this_code,"",$code);}
$code = str_replace("r","",str_replace("n","",str_replace("t","",$code)));
$common_js_min = $code;
# minimize admin js
$code = str_replace('/*',"~",str_replace('*/',"~",$admin_js)); preg_match_all("'~(.*?)~'si", $code, $match);
foreach($match[1] as $val) {$this_code="~".$val.'~';$code = str_replace($this_code,"",$code);}
$code = str_replace("r","",str_replace("n","",str_replace("t","",$code)));
$admin_js_min = $common_js_min.$code;
$admin_js = $common_js.$admin_js;
$url = $asbU.'js/';
# backup existing files
copy($url.'common.php', $url.'backup/common-bu.php');
copy($url.'common-min.php', $url.'backup/common.min-bu.php');
copy($url.'admin.php', $url.'backup/admin-bu.php');
copy($url.'admin-min.php', $url.'backup/admin.min-bu.php');
# write common jQuery
$handle = fopen($url.'common.js', 'w+'); fwrite($handle, $common_js); fclose($handle);
$handle = fopen($url.'common.min.js', 'w+'); fwrite($handle, $common_js_min); fclose($handle);
# write admin jQuery
$handle = fopen($url.'admin.js', 'w+'); fwrite($handle, $admin_js); fclose($handle);
$handle = fopen($url.'admin.min.js', 'w+'); fwrite($handle, $admin_js_min); fclose($handle);
$msg = 'All jQuery files have been updated.';
}
/* js
---------------------------------------------------------------- */
if($folder == 'functions'){
# minimize common functions
$code = str_replace('/*',"~",str_replace('*/',"~",$common_functions)); preg_match_all("'~(.*?)~'si", $code, $match);
foreach($match[1] as $val) {$this_code="~".$val.'~';$code = str_replace($this_code,"",$code);}
$code = str_replace("r","",str_replace("n","",str_replace("t","",$code)));
$common_functions_min = $code;
# minimize admin functions
$code = str_replace('/*',"~",str_replace('*/',"~",$admin_functions)); preg_match_all("'~(.*?)~'si", $code, $match);
foreach($match[1] as $val) {$this_code="~".$val.'~';$code = str_replace($this_code,"",$code);}
$code = str_replace("r","",str_replace("n","",str_replace("t","",$code)));
$admin_functions_min = $common_functions_min.$code;
$admin_functions = $common_functions.$admin_functions;
$url = $asbU.'functions/';
# backup existing files
copy($url.'common.php', $url.'backup/common-bu.php');
copy($url.'common-min.php', $url.'backup/common.min-bu.php');
copy($url.'admin.php', $url.'backup/admin-bu.php');
copy($url.'admin-min.php', $url.'backup/admin.min-bu.php');
# write common functions
$handle = fopen($url.'common.php', 'w+'); fwrite($handle, $common_functions); fclose($handle);
$handle = fopen($url.'common.min.php', 'w+'); fwrite($handle, $common_functions_min); fclose($handle);
# write admin functions
$handle = fopen($url.'admin.php', 'w+'); fwrite($handle, $admin_functions); fclose($handle);
$handle = fopen($url.'admin.min.php', 'w+'); fwrite($handle, $admin_functions_min); fclose($handle);
$msg = 'All function files have been updated.';
}
//echo $msg;
return $msg;
Al hacer clic, en lugar de analizar normalmente, se activa una alerta del servidor del sitio que dice: “No está autorizado para acceder a este script” como se muestra en la imagen de abajo.
Experimenté este tipo de problema hace aproximadamente un año y descubrí que agregar el siguiente código en la parte superior del archivo de análisis de php resolvió el problema:
header('Access-Control-Allow-Origin:*');
También he intentado aislar el archivo desencadenante específico usando este código:
header('Access-Control-Allow-Origin: url to specific triggering file,Vary: Origin');
… y la página de administración real de woprdpress:
header('Access-Control-Allow-Origin: url to specific triggering wordpress admin page,Vary: Origin');
Ninguno de los anteriores ha funcionado.
Tengo el mismo código con diferentes variables y archivos de análisis que funcionan bien en el mismo sitio en otro complemento que desarrollé. Estoy confundido y necesito dirección.
.