Skip to content

Instantly share code, notes, and snippets.

@joelmiguelvalente
Created February 15, 2022 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelmiguelvalente/50bdc91a451f88bac5e164d69314b41a to your computer and use it in GitHub Desktop.
Save joelmiguelvalente/50bdc91a451f88bac5e164d69314b41a to your computer and use it in GitHub Desktop.
<?php if ( ! defined('TS_HEADER')) exit('No se permite el acceso directo al script');
/**
* Controlador AJAX
*
* @name ajax.css.php
* @author Miguel92
*/
/**********************************\
* (VARIABLES POR DEFAULT) *
\*********************************/
// NIVELES DE ACCESO Y PLANTILLAS DE CADA ACCIÓN
$files = [
'css-editar' => ['n' => 2, 'p' => ''],
'css-guardar' => ['n' => 2, 'p' => ''],
];
/**********************************\
* (VARIABLES LOCALES ESTE ARCHIVO) *
\*********************************/
// REDEFINIR VARIABLES
$tsPage = 'ajax/p.css.'.$files[$action]['p'];
$tsLevel = $files[$action]['n'];
$tsAjax = empty($files[$action]['p']) ? 1 : 0;
/**********************************\
* (INSTRUCCIONES DE CODIGO) *
\*********************************/
// DEPENDE EL NIVEL
$tsLevelMsg = $tsCore->setLevel($tsLevel, true);
if($tsLevelMsg != 1) { echo '0: '.$tsLevelMsg; die();}
/**
* Creamos la carpeta "css_backup" para almacenar las copias
*/
$backups = "../../files/uploads/css_backup";
if(!is_dir($backups)) {
mkdir($backups);
chmod($backups, 0777);
}
$myBackUp = $backups . '/$1-backup.css';
switch($action){
case 'css-editar':
$file_css = $tsCore->setSecure($_POST['css'], true);
$notExt = substr($file_css, 0, -4);
if(file_exists($smarty->template_dir["tema"] . $file_css)) {
$css = file_get_contents($smarty->template_dir["tema"] . $file_css);
# Creamos un backup, solo por seguridad
file_put_contents(str_replace('$1', $notExt, $myBackUp), $css);
echo $css;
} elseif(file_exists($smarty->template_dir["css"] . $file_css)) {
$css = file_get_contents($smarty->template_dir["css"] . $file_css);
# Creamos un backup, solo por seguridad
file_put_contents(str_replace('$1', $notExt, $myBackUp), $css);
echo $css;
} else echo 'Error: el archivo no existe';
break;
case 'css-guardar':
$nombre = $tsCore->setSecure($_POST["name"]);
$contenido = $_POST['contenido'];
$contenido = str_replace(
['\\n', "&#039;", '&quot;'],
['\n', "'", '"'],
$contenido
);
$dir = (file_exists($smarty->template_dir["tema"] . $nombre)) ? "tema" : "css";
$css = $smarty->template_dir[$dir] . $nombre;
file_put_contents($css, $contenido);
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment