You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

455 lines
12 KiB

<?php
/**
* Класс работы с модулем
*
* @package AVE.cms
* @subpackage
* @filesource
*/
class Snippets {
//-- Templates directory
public static $tpl_dir;
//-- Fragment directory
public static $fragmentdir;
//-- Fragment filename
public static $fragmentfile;
//-- Fragments limit
public static $limit = 1000;
/* ---------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------- */
/*
|-----------------------------------------------------------------------------------------------------------------------
| self::_json
|-----------------------------------------------------------------------------------------------------------------------
|
*/
public static function _json ($data, $exit = false)
{
header("Content-Type: application/json;charset=utf-8");
$json = json_encode($data);
if ($json === false)
{
$json = json_encode(array("jsonError", json_last_error_msg()));
if ($json === false)
$json = '{"jsonError": "unknown"}';
http_response_code(500);
}
echo $json;
if ($exit)
exit;
}
/*
|-----------------------------------------------------------------------------------------------------------------------
| self::_fragment
|-----------------------------------------------------------------------------------------------------------------------
|
*/
private static function _fragment ($key = null)
{
global $AVE_Template;
$fragment_name = (isset($_POST['fragment_name'])
? $_POST['fragment_name']
: $AVE_Template->get_config_vars('CONSOLE_SAVE_NO_NAME'));
$fragment_code = (isset($_POST['fragment_code'])
? base64_encode($_POST['fragment_code'])
: '');
$fragmentdata = [
'fragment_id' => $key,
'fragment_time' => time(),
'fragment_ip' => $_SERVER['REMOTE_ADDR'],
'fragment_user_id' => (isset($_SESSION['user_id']) ? $_SESSION['user_id'] : '0'),
'fragment_name' => $fragment_name,
'fragment_code' => $fragment_code
];
return $fragmentdata;
}
/*
|-----------------------------------------------------------------------------------------------------------------------
| self::show
|-----------------------------------------------------------------------------------------------------------------------
|
*/
public static function show ()
{
global $AVE_Template;
$fragmentdata = [];
if (file_exists(self::$fragmentdir . self::$fragmentfile))
include self::$fragmentdir . self::$fragmentfile;
$fragmentdata = msort($fragmentdata, 'fragment_name');
$AVE_Template->assign('fragments', $fragmentdata);
$AVE_Template->assign('content', $AVE_Template->fetch(self::$tpl_dir . 'main.tpl'));
}
/*
|-----------------------------------------------------------------------------------------------------------------------
| self::execute
|-----------------------------------------------------------------------------------------------------------------------
|
*/
public static function fragmentExecute ()
{
global $AVE_DB, $AVE_Template;
error_reporting(E_ALL);
ini_set('display_errors', 1);
$profiler = false;
if (isset($_POST['fragment_profiling']) && $_POST['fragment_profiling'] == 1) {
static $list, $time, $count;
$profiler = true;
}
$code = stripslashes($_POST['fragment_code']);
ob_start();
Debug::startMemory('Eval');
Debug::startTime('Eval');
$result = eval(' ?>' . $code . '<?' . 'php ');
$memoryUsage = Debug::endMemory('Eval');
$totalTime = Debug::endTime('Eval');
$out = ob_get_contents();
ob_end_clean();
if ($result)
$out = $result;
if ($profiler)
{
$result_profiler = $AVE_DB->Query("SHOW PROFILES");
$list = "<table width=\"100%\" class=\"tableStatic\" style=\"font-size: 11px; font-family: Consolas, Verdana, Arial;\">"
. "\n\t<col width=\"20\">\n\t<col width=\"70\">";
while ($row = $result_profiler->FetchRow()) {
$time += $row->Duration;
$row->Query = preg_replace('/\t+/', '', $row->Query);
$list .= "\n\t<tr>\n\t\t<td><strong>"
. $row->Query_ID
. "</strong></td>\n\t\t<td><strong>"
. number_format($row->Duration * 1, 6, ',', '')
. "</strong></td>\n\t\t<td style=\"white-space: pre\"><strong>"
. $row->Query
. "</strong></td>\n\t</tr>";
}
$list .= "\n</table>";
$time = number_format($time * 1, 6, ',', '');
$count = $result_profiler->NumRows();
$AVE_DB->Query("SET PROFILING = 0");
$out .= '<ul class="messages"><li class="highlight yellow">';
$out .= "\nВремя выполнения: " . $totalTime;
$out .= "\n<br>Использовано памяти: " . $memoryUsage;
$out .= "\n<br>Количество запросов: " . $count . ' шт. за ' . $time . ' сек.';
$out .= '</li></ul>';
$out .= "\n<br>" . $list;
}
else
{
$out .= '<ul class="messages"><li class="highlight yellow">';
$out .= "\nВремя выполнения: " . $totalTime;
$out .= "\n<br>Использовано памяти: " . $memoryUsage;
$out .= '</li></ul>';
}
echo $out;
exit;
}
/*
|-----------------------------------------------------------------------------------------------------------------------
| self::fragmentInsert
|-----------------------------------------------------------------------------------------------------------------------
|
*/
public static function fragmentInsert ()
{
global $AVE_Template;
$fragment_code = '';
$fragmentdata = [];
$fragment_key = (isset($_POST['fragment_key']) && is_numeric($_POST['fragment_key']) OR $_POST['fragment_key'] != '')
? (int) $_POST['fragment_key']
: null;
if (file_exists(self::$fragmentdir . self::$fragmentfile))
include self::$fragmentdir . self::$fragmentfile;
if (isset($fragmentdata[$fragment_key]))
$fragment_code = stripslashes(base64_decode($fragmentdata[$fragment_key]['fragment_code']));
if (isAjax())
{
$success = $fragment_code
? true
: false;
$return = [
'success' => $success,
'fragment_code' => $fragment_code,
'fragment_key' => $fragment_key,
'message' => $success ? $AVE_Template->get_config_vars('CONSOLE_DOWNLOAD_SAVED') : $AVE_Template->get_config_vars('CONSOLE_DOWNLOAD_FALSE'),
'header' => $success ? $AVE_Template->get_config_vars('CONSOLE_SUCCESS') : $AVE_Template->get_config_vars('CONSOLE_ERROR'),
'theme' => $success ? 'accept' : 'error'
];
self::_json($return, true);
} else {
header('Location:index.php?do=modules&action=modedit&mod=snippets&moduleaction=1&cp=' . SESSION);
exit;
}
}
/*
|-----------------------------------------------------------------------------------------------------------------------
| self::fragmentNew
|-----------------------------------------------------------------------------------------------------------------------
|
*/
public static function fragmentNew ()
{
global $AVE_Template;
$fragmentdata = [];
$fragmentfile = self::$fragmentdir . self::$fragmentfile;
if (file_exists(self::$fragmentdir . self::$fragmentfile))
include self::$fragmentdir . self::$fragmentfile;
if (empty($fragmentdata))
$_key = 0;
else
$_key = array_pop(array_keys($fragmentdata)) + 1;
$fragmentdata[] = self::_fragment($_key);
$fragmentdata = array_slice($fragmentdata, -1 * self::$limit);
end($fragmentdata);
$fragment_key = key($fragmentdata);
$save_data = '<?php';
$save_data .= PHP_EOL;
$save_data .= '$fragmentdata = ' . var_export($fragmentdata, true);
$save_data .= PHP_EOL;
$save_data .= '?>';
file_put_contents($fragmentfile, $save_data);
$fragmentdata = msort($fragmentdata, 'fragment_name');
$fragments = [];
foreach ($fragmentdata AS $k => $v)
{
if ($v['fragment_id'] == $_key)
$_fragment_key = $v['fragment_id'];
$fragments[$k] = [
'fragment_key' => $v['fragment_id'],
'fragment_name' => $v['fragment_name']
];
}
unset ($fragmentdata, $save_data);
if (isAjax()) {
$return = [
'success' => true,
'key' => $_fragment_key,
'fragments' => $fragments,
'message' => $AVE_Template->get_config_vars('CONSOLE_SAVED'),
'header' => $AVE_Template->get_config_vars('CONSOLE_SUCCESS'),
'theme' => 'accept'
];
self::_json($return, true);
} else {
header('Location:index.php?do=modules&action=modedit&mod=snippets&moduleaction=1&cp=' . SESSION);
exit;
}
}
/*
|-----------------------------------------------------------------------------------------------------------------------
| self::fragmentSave
|-----------------------------------------------------------------------------------------------------------------------
|
*/
public static function fragmentSave ()
{
global $AVE_Template;
$fragment_key = (int)$_POST['fragment_key'];
$fragmentdata = [];
$fragmentfile = self::$fragmentdir . self::$fragmentfile;
if (file_exists(self::$fragmentdir . self::$fragmentfile))
include self::$fragmentdir . self::$fragmentfile;
$fragmentdata[$fragment_key]['fragment_code'] = base64_encode($_POST['fragment_code']);
$fragmentdata[$fragment_key]['fragment_changed'] = time();
$fragmentdata = array_slice ($fragmentdata, -1 * self::$limit);
$save_data = '<?php';
$save_data .= PHP_EOL;
$save_data .= '$fragmentdata = ' . var_export($fragmentdata, true);
$save_data .= PHP_EOL;
$save_data .= '?>';
file_put_contents ($fragmentfile, $save_data);
unset ($fragmentdata, $save_data);
if (isAjax())
{
$return = [
'success' => true,
'key' => $fragment_key,
'message' => $AVE_Template->get_config_vars('CONSOLE_SAVED'),
'header' => $AVE_Template->get_config_vars('CONSOLE_SUCCESS'),
'theme' => 'accept'
];
self::_json($return, true);
}
else
{
header('Location:index.php?do=modules&action=modedit&mod=snippets&moduleaction=1&cp=' . SESSION);
exit;
}
}
/*
|-----------------------------------------------------------------------------------------------------------------------
| self::fragmentDelete
|-----------------------------------------------------------------------------------------------------------------------
|
*/
public static function fragmentDelete ()
{
global $AVE_Template;
$fragment_key = (int) $_POST['fragment_key'];
$fragmentdata = [];
$fragmentfile = self::$fragmentdir . self::$fragmentfile;
if (file_exists(self::$fragmentdir . self::$fragmentfile))
include self::$fragmentdir . self::$fragmentfile;
foreach ($fragmentdata AS $k => $v)
{
if ($fragment_key == $v['fragment_id'])
unset ($fragmentdata[$k]);
}
$fragmentdata = array_slice($fragmentdata, -1 * self::$limit);
foreach ($fragmentdata AS $k => $v)
{
$v['fragment_id'] = $k;
$fragmentdata[$k] = $v;
}
$save_data = '<?php';
$save_data .= PHP_EOL;
$save_data .= '$fragmentdata = ' . var_export($fragmentdata, true);
$save_data .= PHP_EOL;
$save_data .= '?>';
file_put_contents ($fragmentfile, $save_data);
$fragmentdata = msort($fragmentdata, 'fragment_name');
$fragments = [];
foreach ($fragmentdata AS $k => $v)
$fragments[] = [
'fragment_key' => $v['fragment_id'],
'fragment_name' => $v['fragment_name']
];
unset ($fragmentdata, $save_data);
if (isAjax()) {
$return = [
'success' => true,
'fragments' => $fragments,
'message' => $AVE_Template->get_config_vars('CONSOLE_DEL_SAVED'),
'header' => $AVE_Template->get_config_vars('CONSOLE_SUCCESS'),
'theme' => 'accept'
];
self::_json($return, true);
} else {
header('Location:index.php?do=modules&action=modedit&mod=snippets&moduleaction=1&cp=' . SESSION);
exit;
}
}
/*
|-----------------------------------------------------------------------------------------------------------------------
| self::fragmentsShow
|-----------------------------------------------------------------------------------------------------------------------
|
*/
public static function fragmentsShow ()
{
//ToDo
}
}
?>