link = $link;
}
//обработка входящих параметров
function gparam($name, $default = null, $type = 'str') {
if (isset($_GET[$name])) {
$value = $_GET[$name];
if ($type == 'str')
return $this->xss($value);
elseif($type == 'int')
return (int)$value;
}
else
return $default;
}
//загрузка схемы или ее создание по умолчанию
function load($shema) {
$f_xml = APPPATH.'xml/'. $shema.'.xml';
if (!file_exists($f_xml)){ // если файла нет - пытаемся создать
if (!$this->create($shema, $f_xml)) { // если файл не создался
echo('not found shema');
exit;
}
}
return simplexml_load_file($f_xml);
}
/**
* автоматическое создание файла модели
*/
function create($table, $xfile) {
$result = mysql_query("SHOW COLUMNS FROM `$table`");
if (mysql_num_rows($result) == 0)
return false;
while ($row = mysql_fetch_assoc($result)) {
$xml .= " - \n";
$xml .= " ".$row['Field']."\n
".$row['Field']."\n";
if ($row['Extra'] == 'auto_increment')
$xml .= " increment\n";
elseif ($row['Type'] == 'tinyint(1)')
$xml .= " checkbox\n";
elseif ($row['Type'] == 'varchar(4)')
$xml .= " file\n
85\n";
elseif ($row['Type'] == 'text')
$xml .= " textareatiny\n
8
\n
85\n";
elseif ($row['Type'] == 'datetime')
$xml .= " datetime\n";
else
$xml .= " text\n
85\n";
$xml .= " \n";
$xml .= " \n";
if ($row['Extra'] == 'auto_increment'){
$xml .= " \n";
$increment = $row['Field'];
}
else
$xml .= " \n";
$xml .= " \n";
$xml .= " \n";
}
$xml = ''."\n".
"
$incremen
DESC
$increment
$table
$xml";
//$xfile = site_fold_ad.'xml/'.$table.'.xml';
return file_put_contents($xfile, $xml);
}
function start(){
if (!defined('DEBUG')) {
include_once ('auth.lib.php');
$auth = new auth();
$auth->action();
$user_row = kORM::table('users')->filter('login', $_SERVER['PHP_AUTH_USER'])->one();
/*$user = mysql_query("SELECT * FROM `users` Where `login`='".$_SERVER['PHP_AUTH_USER']."'");
$user_row = mysql_fetch_array($user);*/
if ($user_row == null)
$auth->authorized();
session_start();
$_SESSION['user_id'] = $user_row['user_id'];
setcookie('user_id', $user_row['user_id']);
$group_id = $user_row['group_id'];
$nameuser = $user_row['name'];
$region_id = $user_row['region_id'];
$grrow = kORM::table('groupuser')->filter('group_id', $group_id)->one();
if ($user_row != null) {
$_SESSION['group'] = $grrow['name'];
$_SESSION['readonly'] = 0;
}
else
$_SESSION['readonly'] = 0;
write_log($_SERVER['PHP_AUTH_USER'].':ip='.$_SERVER['REMOTE_ADDR'].':authorized', 'log/edition.log');
}
else
$group_id = 1;
$menufile = file_get_contents(APPPATH.'menu/'.$group_id.'.json');
$menus = json_decode($menufile, true);
include(THEME.'views/layout/main.phtml');
return;
}
function init() {
if (count($_GET) == 0) {
$this->start();
return;
}
session_start();
require_once ('photos.php');
require_once ('day_and_week.php');
require_once ('acess.php');
require_once ('filter.php');
require_once ('file.php');
$filter = new filter;
function tp_quotes($text)
{
$quotes=array('"', '«', '»', '«', '»', '«', '»', '', '', '„', '“');
$text=str_replace($quotes, '"', $text);
$text=preg_replace('/([^=]|\A)""(\.{2,4}[а-яА-Я\w\-]+|[а-яА-Я\w\-]+)/', '$1"$2', $text);
$text=preg_replace('/([^=]|\A)"(\.{2,4}[а-яА-Я\w\-]+|[а-яА-Я\w\-]+)/', '$1$2', $text);
$text=preg_replace('/([а-яА-Я\w\.\-]+)""([\n\.\?\!, \)][^>]{0,1})/', '$1"$2', $text);
$text=preg_replace('/([а-яА-Я\w\.\-]+)"([\n\.\?\!, \)][^>]{0,1})/', '$1$2', $text);
$text=preg_replace('/(<\/typo:quot1>[\.\?\!]{1,3})"([\n\.\?\!, \)][^>]{0,1})/', '$1$2', $text);
$text=preg_replace('/([а-яА-Я\w\.\- \n]*?)(.+?)<\/typo:quot1>/', '$1$2', $text);
$text=preg_replace('/(<\/typo:quot2>.+?)(.+?)<\/typo:quot1>/', '$1$2', $text);
$text=preg_replace('/(.+?<\/typo:quot2>)\.(.+?)/', '$1<\/typo:quot1>.$2', $text);
$text=preg_replace('/(.+?<\/typo:quot2>)\.(?!<\/typo:quot1>)/', '$1.$2$3$4', $text);
$text=preg_replace('/""/', '', $text);
$text=preg_replace('/(?<=)(.+?)(.+?)(?!<\/typo:quot2>)/', '$1$2', $text);
$text=preg_replace('/"/', '', $text);
$text=preg_replace('/(<[^>]+)<\/typo:quot\d>/', '$1"', $text);
$text=preg_replace('/(<[^>]+)<\/typo:quot\d>/', '$1"', $text);
$text=preg_replace('/(<[^>]+)<\/typo:quot\d>/', '$1"', $text);
$text=preg_replace('/(<[^>]+)<\/typo:quot\d>/', '$1"', $text);
$text=preg_replace('/(<[^>]+)<\/typo:quot\d>/', '$1"', $text);
$text=preg_replace('/(<[^>]+)<\/typo:quot\d>/', '$1"', $text);
$text=str_replace('', $this->quot11, $text);
$text=str_replace('', $this->quot12, $text);
$text=str_replace('', $this->quot21, $text);
$text=str_replace('', $this->quot22, $text);
return;
}
//превращает в js синтаксис
function js_func ($namefunc, $params){
$params_line = '';
foreach ($params as $param) {
if ($params_line !== '')
$params_line .= ',';
$params_line .= chr(39).$param.chr(39);
}
return $namefunc.'('.$params_line.');';
}
function delete_cache($fstr, $increment)
{
$files = explode(',',$fstr);
foreach ($files as $file){
$fname = site_fold.str_replace('{%}', $increment, trim($file));
unlink($fname);
}
return;
}
//приводим дату в названии папки
function date_to_url($date, $time = True, $separ = '/')
{
$currdate = explode(' ', $date);
$date = explode('-', $currdate[0]);
$url = $date[0].$separ.$date[1].$separ.$date[2];
if ($time) {
$time = explode(':', $currdate[1]);
$url .= $separ.$time[0].$separ.$time[1].$separ.$time[2];
}
return $url;
}
//пересчет кол-во значений в таблице
function counts($citems, $values)
{
/*
table - таблица, где обновляем данные
t_inc - имя инкрементного поля таблицы в которой обновляем
t_inc_column - ссылка на значение в поле гл таблицы
column - колонка которую обновляем
values - все полученные значения переменных
*/
$col_inc_name = (string)$citems->column_inc;
$count_sql = 'SELECT COUNT(*) FROM '.MAINTABLE.' WHERE '.$col_inc_name.'='.$values[$col_inc_name].' '.$citems->sql_where;
$countres = mysql_query ($count_sql) or write_log('Ошибка MySQL: '.mysql_error()); //подсчет
$sqlrows = mysql_num_rows($countres);
if ($sqlrows > 0) { //запись результатов
$row = mysql_fetch_array($countres, MYSQL_NUM);
$count = $row[0];//получаем кол-во
$sql_update = 'UPDATE '.$citems->table.' SET '. $citems->column.'='.$count.' WHERE '.$citems->t_inc.' = '.$values[$col_inc_name];
mysql_query($sql_update) or write_log('Ошибка MySQL: '.mysql_error());
}
}
function separ($txt)
{
return '`'.$txt.'`';
}
function quote($txt)
{
return "'".$txt."'";
}
function NullErrSession() {
if (isset($_SESSION['errors']))
unset($_SESSION['errors']);
if (isset($_SESSION['values']))
unset($_SESSION['values']);
}
//спецсимволы при сборе заброса
function SqlAddSpec($str, $type)
{
if (mb_strlen($str) > 0) {
if ($type == 0)
return ', ';
else if ($type == 1)
return ' AND ';
else
return '';
}
else
return '';
}
function fileexpansion ($filename)
{
$out = array();
preg_match('/\S+\.(\S+)$/', $filename, $out);
return $out[1];
}
function subfilters($value, $config)
{
}
//построение выпадающих списков и фильтров
function lookup($table, $increment, $column, $value, $params = array(), $attrs = array())
{
$where = '';
$order = '';
$limit = '';
$join = '';
//поля учавствующее в запросе
$fields[] = separ($increment);
$fields[] = separ($column);
if (sizeof($params) > 0){
If (isset($params['where']))
$where = ' WHERE '.$params['where'];
If (isset($params['order']))
$order = ' ORDER BY '.$params['order'];
If (isset($params['limit']))
$limit = ' LIMIT '.$params['limit'];
if (isset($params['subfilter'])){
$subfilter = $params['subfilter'];
//$fields[] = separ($params['subfilter']['column']);
/*if ($subfilter['table'] !== $table) {
$join = ' LEFT JOIN '.separ($subfilter['table']).' ON ('.separ($subfilter['table']).'.'.separ($subfilter['id']).'='.separ($table).'.'.separ($wh_column).') ';
}*/
}
}
$sql_select = 'SELECT '.implode(',', $fields).' FROM '.separ($table).$join.$where.$order.$limit;
//echo $table.': '.$sql_select.'
';
$selectres = mysql_query($sql_select);
if (@mysql_num_rows($selectres) !== 0) {
foreach ($attrs as $key => $attr) //дополнительные атрибуты
$attr_str .= ' '.$key.'="'.$attr.'" ';
$null_txt = (isset($params['null'])) ? $params['null'] : '- не выбрано - ';
$selected = ($value == 0) ? ' selected="selected"': '';
$result = '';
return $result;
}
else
return ' - нет данных - ';
}
//построение списка подзаписей
function subfilter($value, $config, $id)
{
$sql_filter = 'SELECT '.separ($config->increment).', '.separ($config->column).' FROM '.separ($config->table).' WHERE '.separ($config->wh_column).'='.quote($value);
$selectres = mysql_query($sql_filter);
if (@mysql_num_rows($selectres) !== 0) {
$result = '';
return $result;
}
else
return '';
}
function GreateMainFilter($admin, $columname, $currvalue, $nullvalue = 'Нулевые значения')
{
$currvalue = (string)$value_tek;
$titles = array('Все', 'Пустые значения', $nullvalue);
$values = array(
'all'=>array('name'=>'Все', 'value'=>''),
'null'=>array('name'=>$nullvalue, 'value'=>'0')
);
foreach ($values as $value) {
$selected = ($value['value'] == $currvalue) ? $selected = ' selected="selected"' : '';
$result .= '';
}
return $result;
}
function GreateMainLookup($value_tek) {
$value = (string)$value_tek;
$types = array('null', '0');
$titles = array('Пустое значение', 'Нулевое значения');
for ($s = 0; $s < 2; $s++) {
$selected = ($value == $types[$s]) ? 'selected="selected"' : '';
echo '';
}
}
function AnonsText($text, $counts, $counttype)
{
$text = strip_tags($text);
$mb_strlen = mb_strlen($text);
if ($mb_strlen < $counts)
return $text;
else {
for ($i = $counts - 1; $i <= $mb_strlen; $i++){
$s = mb_substr($text,$i,1);
if ($counttype == 0) {
if (($s == ' ') or ( $s == ',') or ($s == '.') or ($s == '!') or ($s == '?')) break; //не обрывать слова.
}
else {
if (( $s == ',') or ($s == '.') or ($s == '!') or ($s == '?')) break; //не обрывать предложения
}
}
if ($i != $mb_strlen ) $countend = $i; else $countend = $counts;
return mb_substr($text, 0, $countend). ' ...';
}
}
#зачитываем параметры
$action = $this->gparam('action', 'selectall');
if ($admin = $this->gparam('admin'))
$this->admin = $admin;
else {
echo 'shema zero';
exit;
}
//доступ группам
/*if (isset($_SESSION['group'])) {
$grname = $_SESSION['group'];
include_once ('acess.php');
if (isset($group[$grname])){
$acs = explode(',', $group[$grname]);
if (!in_array($admin, $ac)){
echo 'нет доступа';
exit();
}
}
}
else {
echo 'нет доступа';
exit();
}*/
//наличие пустых параметров
//if ($f_xml == '' or !file_exists($_SERVER['DOCUMENT_ROOT'].'/xml/'.$f_xml)){
$page = $this->gparam('page', 1, 'int');
$order = (isset($_GET['order'])) ? strip_tags(trim($_GET['order'])) : '';
if ($order !== ''){
$ord_len = mb_strlen($order);
$endpos = mb_substr($order, $ord_len-1, 1);
if ($endpos == '+') {
$order_type = 0;
$order = mb_substr($order, 0 ,$ord_len-1);
}
elseif ($endpos == '-') {
$order_type = 1;
$order = mb_substr($order, 0 ,$ord_len-1);
}
else
$order_type = 0;
}
// $order_type = (isset($_GET['order_type'])) ? (int)($_GET['order_type']) : 0;
$increment_value = $this->gparam('increment', 0 , 'int');
$like = $this->gparam('like', '');
$like_id = $this->gparam('like_id', 0, int);
$maxi = -1;
$xml = $this->load($this->admin); //загружаем схему
// читаем главные параметры
foreach ($xml->xpath('/items/main') as $mainitem) {
$nametable = $mainitem->table;
$maintable = separ($nametable);
$caption = $mainitem->title;
$order_main = (string)$mainitem->order;
$fields_search = (string)$mainitem->search; //поиск по полям
if (isset($mainitem->deleted))
$deleted = (string)$mainitem->deleted;
else
$deleted = 'True';
if($mainitem->order_type) {
$order_main_type = ' '.(string)$mainitem->order_type;
}
if (isset($mainitem->export)){
$ex_table = $mainitem->export->table;
$ex_map = $mainitem->export->map;
}
//убивание файлов кеша
if (isset($mainitem->cache))
$fcache = (string)$mainitem->cache;
//пересчет кол-ва
if(isset($mainitem->count))
$count_items = $mainitem->count;
$increment = (string)$mainitem->increment;
if ($mainitem->where !== '')
$where_main = $mainitem->where;
$link_view = $mainitem->vbutton->link;
$inc_view = $mainitem->vbutton->incname;
}
define('INC', $increment);
define('MAINTABLE', $maintable);
unset($mainitem);
$filters = array();
$filters_count = 0;
switch ($action) {
case 'select':
case 'selectall':
case 'selectrow':
case 'selectpage':
case 'selecttable':
//первоначальные значения
$filters_count = 0;
$increment_num = -1;
$maxi = -1;
$inc_show = false;
//зачитывае данные
$item = $xml->xpath('/items/item');
$item_count = sizeof($item);
$c_type = array();
$chet = False;
if ($action == 'selectall'){ ?>
=$caption?> Редактирование
}
for ($it = 0; $it < $item_count; $it++) {
$nullfilter = False;
$columnname = (string)$item[$it]->column;
if ($item[$it]->filter == 'True'){
$filters[$filters_count]['column'] = $item[$it]->title;
if (isset($_GET[$columnname])){
$colfilter = strip_tags($_GET[$columnname]);
$where_filter .= SqlAddSpec($where_filter, 1).$maintable.'.'.$item[$it]->column.' = '.$colfilter;
if ($colfilter == 'null' || $colfilter == 0)
$nullfilter = True;
}
if ($action == 'selectall'){ //рисуем фильтры
$filters_count ++;
if ($filters_count == 1)
echo 'Фильтрация
';
$selectrow = 'SELECT '.$item[$it]->lookup->id.', '.$item[$it]->lookup->column.' FROM '.$item[$it]->lookup->table;
if ($item[$it]->lookup->where != '')
$selectrow .= ' WHERE '.$item[$it]->lookup->where;
if ($item[$it]->lookup->order != '')
$selectrow .= ' ORDER BY '.$item[$it] ->lookup->order;
$selectres = mysql_query($selectrow);
if (@mysql_num_rows($selectres) != 0) {
$ttitle .= '| '.$item[$it]->title.' | ';
$id_select = $item[$it]->column;
$tselect .= ' | ';
}
}
}
if ($item[$it]->view->table == 'True'){ // если для таблицы активна
$maxi ++;
if ($columnname == $increment) {
$increment_num = $it;
$component[$maxi]['type'] = 'increment';
$component[$maxi]['column'] = $increment;
$inc_show = true;
}
$component[$maxi]['type'] = (string)$item[$it]->type; //запоминаем тип компонета
$component[$maxi]['column'] = (string)$item[$it]->column;
if (isset($item[$it]->link)) $component[$maxi]['link'] = (string)$item[$it]->link;
if (isset($item[$it]->folder)) $component[$maxi]['folder'] = (string)$item[$it]->folder;
if (isset($item[$it]->folder)) $component[$maxi]['width'] = (string)$item[$it]->width;
if (isset($item[$it]->fieldate))
$component[$maxi]['fieldate'] = (string)$item[$it]->fieldate;
/*if (isset($item[$it]->curruser))
$component[$maxi]['curruser'] = (string)$item[$it]->curruser;*/
$new_order_type = '';
if ($columnname == $order) {
$order_value = ' ORDER BY '.$maintable.'.'.$columnname;
if ($order_type == 1){
$ord_title_type = '+';
$order_value .= ' ASC';
$img = '
';
}
else {
$ord_title_type = '-';
$order_value .= ' DESC';
$img = '
';
}
$new_order_type = ($order_type == 1) ? ' ASC' : ' DESC';
$title_class = ' class="order_title"';
}
else {
$title_class = ' class="std_title"';
$ord_title_type = '+';
$img = '';
}
$titles .= 'hint.'" >
'.$item[$it]->title.$img.'
| ';
if ($item[$it]->type == 'lookup' && !$nullfilter){ //формируем запрос
$sql .= SqlAddSpec($sql, 0).$columnname.'.'.$item[$it]->lookup->column;
// $tables .=', '.$item[$it]->lookup->table;
$join .= ' LEFT JOIN '.separ($item[$it]->lookup->table).' '.separ($columnname).' ON ('.separ($columnname).'.'.separ($item[$it]->lookup->id).'='.$maintable.'.'.separ($item[$it]->column).')';
/*$where_lookup .= SqlAddSpec($where_lookup, 1).$item[$it]->lookup->table.'.'.$item[$it]->lookup->id.'='.$maintable.'.'.$item[$it]->column;*/
}
else
$sql .= SqlAddSpec($sql, 0).$maintable.'.'.$item[$it]->column;
}
}
if ($action == 'selectall') {
if ($filters_count > 0):?>
=$ttitle?>
=$tselect?>
$like_id_value = ($like_id == 0) ? '' : $like_id;?>
Поиск
Искать
ID
Данные
}
if ($increment_num == -1) {
$sql .= SqlAddSpec($sql, 0).$maintable.'.'.$increment; //добавляем инекремнт если он не указан
$maxi ++;
$increment_num = $maxi;
$component[$maxi]['type'] = 'increment'; //запоминаем тип компонета
$component[$maxi]['column'] = $increment;
$inc_show = false;
}
if ($action == 'selectrow'){
$where = $increment." = '".$increment_value."'";
//$where .= SqlAddSpec($where, 1).$where_lookup;
//$sqlres = 'SELECT '.$sql.' FROM '.$maintable.$tables.' WHERE '.$where;
$sqlres = 'SELECT '.$sql.' FROM '.$maintable;
if ($tables !== '') $sqlres .= $tables;
if ($join !== '') $sqlres .= $join;
If ($where !=='')$sqlres .= ' WHERE '.$where;
}
else {
if ($order_value == '' && $order_main != '') //если не один не сыграл - фильтр по умолчанию
$order_value = ' ORDER BY '.$order_main.$order_main_type;
if ($where_main !== '')
$where .= SqlAddSpec($where, 1).$where_main;
$where_filter = trim($where_filter);
if ($where_filter !== '')
$where .= SqlAddSpec($where, 1).$where_filter;
if ($like !== '') {
$sqlres .= " LIKE '".$like."'";
$sfields = explode(',',$fields_search);
foreach ($sfields as $field){
$like_where[] = separ(trim($field)). " LIKE '%%".$like."%'";
}
$where .= SqlAddSpec($where, 1).implode(' OR ', $like_where);
}
if ($join != '') $sqlres .= $join;
//if ($where_lookup != '')
// $where .= SqlAddSpec($where, 1).$where_lookup;
if (trim($where) !== '')
$where = ' WHERE '.$where;
$per_page = (isset($_GET['limit'])) ? (int)($_GET['limit']) : 20;
$max_sql = 'SELECT count('.$increment.')'.' FROM '.$maintable.$tables.$where.$order_value;
$maxres = mysql_query($max_sql);
if (@mysql_num_rows($maxres) != 0) {
$maxrow = mysql_fetch_row($maxres);
$total_rows = $maxrow[0];
if ($total_rows > $per_page) {
$num_pages = ceil($total_rows/$per_page);
$vanitem = ($page - 1) * $per_page;
if ($page > 1 && $vanitem > $total_rows)
$vanitem = 1;
$limit = ' LIMIT '.$vanitem.','.$per_page;
}
}
/*
*/
if ($action != 'selecttable'){
echo '
'; ?>
автоматически
0){
$where_like = separ($increment).'= "'.$like_id.'"';
$where .= SqlAddSpec($where, 1).$where_like;
}
if ($where !== '')
$sqlres .= $where;
if ($order_value !== '')
$sqlres .= $order_value;
$sqlres .= $limit;
}
//echo $sqlres;
$selectres = mysql_query($sqlres) or write_log('Ошибка MySQL: '.mysql_error().':'.$sqlres); //подсчет;
if (@mysql_num_rows($selectres) != 0) {
if ($action != 'selectrow')
echo '
'.$titles.'| Редактор | ';
while ($selectrow = mysql_fetch_row($selectres)){
$increment_value = $selectrow[$increment_num];
$edit_active = '"StartLink('.chr(39).$admin.chr(39).", 'edit', 'forms', 'increment', '".$increment_value."'".');"';
$print_active = '"StartLink('.chr(39).$admin.chr(39).", 'print_id', 'forms', 'increment', '".$increment_value."'".');"';
if ($action != 'selectrow') {
$chet = !$chet;
$tr_class = ($chet) ? 'nechet' : 'chet';
echo('');
}
for ($a = 0; $a <= $maxi; $a++) {
switch ($component[$a]['type']) {
case 'checkbox' :
echo('| ');
if ($selectrow[$a] == 1) {
$input_value = 'checked';
$checked_value = '0';
}
else {
$input_value = '';
$checked_value = '1';
}
echo ('');
echo(' | ');
break;
case 'checkdate' :
echo('');
if ($selectrow[$a] == 1) {
$input_value = 'checked';
$checked_value = '0';
}
else {
$input_value = '';
$checked_value = '1';
}
echo ('');
echo(' | ');
break;
case 'increment':
if ($inc_show) {
echo('');
echo (AnonsText($selectrow[$a], 30, 0));
echo(' | ');
}
break;
case 'spin':
echo('');
if ($selectrow[$a] != 0)
echo $selectrow[$a];
echo(' | ');
break;
case 'file':
echo '';
if (in_array($selectrow[$a], array('jpg', 'jpeg', 'gif', 'png', 'JPG'))) {
$wwwname = site.$component[$a]['folder'].'/'.$increment_value.'.'.$selectrow[$a];
$fwidth = 100;
if ($component[$a]['width'])
$fwidth = $component[$a]['width'];
else
$fwidth = 100;
echo' ';
}
echo ' | ';
break;
default:
$fulltext = $selectrow[$a];
$texttitle = '';
if ($component[$a]['maxtext']) {
$maxtext = $component[$a]['maxtext'];
if (mb_strlen($fulltext) > $maxtext) {
$texttitle = 'title="'.$fulltext.'" ';
$val_txt = AnonsText($selectrow[$a], $maxtext, 0);
}
else {
$val_txt = $fulltext;
}
}
else
$val_txt = $fulltext;
echo('');
if (isset($component[$a]['link'])){
$ulink = str_replace('{%}', $selectrow[$a], $component[$a]['link']);
//$link = str_replace('{%inc%}', $increment, $component[$a]['link']);
echo ''.$val_txt.'';
}
else
echo $val_txt;
echo(' | ');
break;
}
}
echo('');
echo(' ');
if ($link_view != '') {
$vlink = site.str_replace('{%}', $increment_value, $link_view);
echo(' ');
}
if (isset($ex_table)):
$export_id=$increment_value.'_ex';?>
';
}
echo(' | ');
if ($action != 'selectrow') echo('
');
}
if ($action != 'selectrow') {
echo('| всего: | '.$total_rows.' |
'); //общее кол-во
echo('
');
if ($action != 'select' && $action != 'selecttable') {
if ($total_rows > $per_page) { //рисуем странички
echo('
Странички:
из '.$num_pages.'');
$olimits = array('5', '10', '15', '20', '25', '30');
/*echo(' Элементы:
} */ ?>
}
}
}
}
break;
//подфильтр
case 'subfilter':
if (isset($_GET['id']))
$id = strip_tags(($_GET['id']));
if (isset($_GET['value']))
$value = $_GET['value'];
$item = $xml->xpath('/items/item');
$item_count = sizeof($item);
for ($it = 0; $it < $item_count; $it++) {
if ($item[$it]->id == $id){
if ($value > 0)
$params['where'] = $item[$it]->lookup->subfilter->wh_column.'='.$value;
$attrs['name'] = $item[$it]->column;
$attrs['id'] = $attr['name'];
echo lookup($item[$it]->lookup->table, $item[$it]->lookup->id, $item[$it]->lookup->column, 0, $params, $attrs);
break;
}
}
break;
//горячий checkbox
case "active":
$result = 0;
if (isset($_GET['increment']))
$increment_value = (int)($_GET['increment']);
else
$increment_value = 0;
if (isset($_GET['active']))
$active = strip_tags(($_GET['active']));
$newactive = ($active == 'true') ? 1 : 0;
if (isset($_GET['field']))
$field = strip_tags($_GET['field']);
if ($increment_value > 0) {
write_log($_GET['fieldate']);
if (isset($_GET['fieldate']) and $newactive == 1)
$sql_active = mysql_query('UPDATE '.$maintable.' SET `'.$field.'` = '.$newactive.', '.'`'.$_GET['fieldate'].'` = '."'".date('Y:m:d G:i:s')."'".' WHERE `'.$increment.'` = '."'".$increment_value."'");
else
$sql_active = mysql_query('UPDATE '.$maintable.' SET `'.$field.'` = '.$newactive.' WHERE `'.$increment.'` = '."'".$increment_value."'");
$result = ($sql_active) ? 1 : 0;
}
echo (int)$result ;
write_log($_SERVER['PHP_AUTH_USER'].': '.'table='.$maintable.':action=active:value='.$newactive.':id='.$increment_value, 'log/edition.log');
break;
case "print_id":?>
if (isset($_GET['increment']))
$increment_value = (int)($_GET['increment']);
else
$increment_value = 0;
$item = $xml->xpath('/items/item');
$item_count = sizeof($item);
for ($i = 0; $i < $item_count; $i++) {
if ($item[$i]->view->printed == 'True'){
$column = (string)$item[$i]->column;
if ($column != $increment){
$sql .= SqlAddSpec($sql, 0).$maintable.'.'.$column;
if ($item[$i]->type == 'lookup'){
$join .= ' LEFT JOIN '.separ($item[$i]->lookup->table).' ON ('.separ($item[$i]->lookup->table).'.'.separ($item[$i]->lookup->id).'='.$maintable.'.'.separ($item[$i]->column).')';
$sql .= SqlAddSpec($sql, 0).$item[$i]->lookup->table.'.'.$item[$i]->lookup->column;
}
else
$sql .= SqlAddSpec($sql, 0).$maintable.'.'.$column;
}
}
}
$editres = mysql_query ('SELECT '.$sql.' FROM '.$maintable.$join.' WHERE '.$increment.' = '.$increment_value);
$sqlrows = mysql_num_rows($editres);
if ($sqlrows > 0)
$editrow = mysql_fetch_array($editres);
for ($f = 0; $f < $item_count; $f++) {
if ($item[$f]->view->printed == 'True'){
echo ''.$item[$f]->title.':
';
if($item[$f]->type == 'lookup'){
$column = (string)$item[$f]->lookup->column;
echo $editrow[$column];
}
else {
$column = (string)$item[$f]->column;
echo $editrow[$column];
}
echo '
';
}
}
break;
case 'export':
if (isset($_GET['increment']))
$increment_value = (int)($_GET['increment']);
else
echo 'no';
$source = array();
$exvalue = array();
$count_element = 0;
$elements = explode(',',$ex_map);
foreach ($elements as $element)
{
$count_element++;
$params = explode('->',trim($element));
$ex_fields[] = trim($params[0]);
$sou_fields[] = separ(trim($params[1]));
}
$sql_select = 'SELECT '.implode(',',$ex_fields).' FROM '.$maintable.' WHERE '.separ($increment).'='."'".$increment_value."' LIMIT 1;";
$data = mysql_query($sql_select) or die("Invalid query");
if (mysql_num_rows($data) == 0) {
echo "error";
break;
}
$row = mysql_fetch_row($data);
for ($f = 0; $f < $count_element; $f++) {
$curr_value = (isset($row[$f])) ? $row[$f] : 0;
$ex_values[] = "'".addslashes($curr_value)."'";
}
$sql_insert = 'INSERT INTO'.separ($ex_table).' ('.implode(',',$sou_fields).') VALUES('.implode(',',$ex_values).');';
write_log(':'.$sql_insert);
mysql_query($sql_insert) or die("Invalid");
write_log($_SERVER['PHP_AUTH_USER'].': '.'table='.$maintable.':action=export :id='.$increment_value, 'log/edition.log');
break;
//формы - добавления и редактирования
case "edit":
case "add":
$act_str = ($action == 'edit') ? 'Изменение': 'Добавление';?>
xpath('/items/item');
$item_count = sizeof($item);
if ($action == 'edit') { //формируем запрос
$f_acton = '&action=update&increment='.$increment_value;
if(!$valid) {
for ($i = 0; $i < $item_count; $i++) {
$column = (string)$item[$i]->column;
if ($column != $increment)
$sql .= SqlAddSpec($sql, 0).$maintable.'.'.$column;
}
$editres = mysql_query ('SELECT '.$sql.' FROM '.$maintable.' WHERE '.$increment.' = '.$increment_value);
$sqlrows = mysql_num_rows($editres);
if ($sqlrows > 0)
$editrow = mysql_fetch_array($editres);
}
}
else
$f_acton = '&action=insert';
$pr_form = '
');
//echo('
//echo('
break;
case "insert":
case "update":
//защита от пустого $POST
if (sizeof($_POST) == 0)
exit;
require_once("validate.php");
require_once("tipograf.php");
$errors = 0;
$errresult = false;
//if ($action == 'insert') {
$div_res = 'content';
$admin_res = 'selectall';
// }
// else {
// $div_res = $increment_value;
// $admin_res = 'selectrow';
// }
NullErrSession();
$item = $xml->xpath('/items/item');
$item_count = sizeof($item);
$fa = 0;
$tip_reg = (isset($_POST['tipogreg'])) ? false : true; /* вкл-выкл типографики */
for ($i = 0; $i <= $item_count - 1; $i++) {
$posts = (string)$item[$i]->column;
if ($posts !== '') {
$column_id = (isset($item[$i]->id)) ? (string)$item[$i]->id : '';
$key = $posts;
$type = (string)$item[$i]->type;
$values = null;
if (isset($item[$i]->view->form))
$view_form = ($item[$i]->view->form == 'True') ? 'True' : 'False';
else
$view_form = 'False';
}
else
$view_form = 'False';
if ($view_form == 'True') {
switch ($type) {
case 'checkbox':
case 'checkdate':
$activation = True;
if (isset($item[$i]->count) and $item[$i]->count !== ''){
$ccolumn =(string)$item[$i]->count;
if (isset($_POST[$ccolumn])) {
$txtonly = $_POST[$ccolumn];
$txtonly = html_entity_decode($txtonly);
$txtonly = str_replace(" ", '', $txtonly);
//$txtonly = str_replace(" ", '', $txtonly);
$txtonly = preg_replace('/&([a-zA-Z0-9]{2,6}|#[0-9]{2,4});/', '', $txtonly);
$txtonly = str_replace('|+|amp|+|', '&', $txtonly);
$txtonly = strip_tags($txtonly);
$txtonly = trim($txtonly);
$len = mb_strlen($txtonly, 'UTF-8');
if ($len > 1200) {
$values = null;
break;
}
}
}
if (isset($_POST[$posts])){
$values = '1';
$checkdated = True;
}
else {
$values = '0';
$checkdated = False;
}
$activation = True;
break;
case 'file':
$file_increment[$fa] = $i;
$activation = False;
$fa ++;
break;
case 'increment':
$activation = False;
break;
case 'lookup':
$values = (isset($_POST[$posts])) ? (int)$_POST[$posts] : 0;
write_log($posts.'='.$values);
$activation = True;
break;
case 'date':
$values = (isset($_POST[$posts])) ? $_POST[$posts] : '';
$activation = True;
break;
case 'user':
$values = (isset($_POST[$posts])) ? (int)$_POST[$posts] : 0;
$activation = True;
break;
default:
$values = (isset($_POST[$posts])) ? $_POST[$posts] : '';
$values = str_replace('img src="../images', 'img src="http://www.argumenti.ru/images', $values); //хак для полного адреса фоток
$values = str_replace('img src="../photo', 'img src="http://www.argumenti.ru/photo', $values); //хак для полного адреса фоток
//$values = preg_replace('//Uis', '', $values);
if ($type == 'text')
$values = $filter->source($values)->entity('html');
$values = addslashes($values);
if (isset($item[$i]->folder)) {
file_put_contents($item[$i]->folder.$increment_value.'.html',$values);
$activation = False;
}
$vald_type = (string)$item[$i]->validate->type;
$errors = validate($values, $vald_type);
if ($errors == 0) {
$err_value = 'error';
if ($item[$i]->validate->critic)
$errresult = true;
}
else {
/*$err_value = 'hidden';
if ($tip_reg) {
$tip_type = ($item[$i]->tipograf->type != '') ? $item[$i]->tipograf->type : 'standart';
$tip_kill = ($item[$i]->tipograf->killstyle == 'true') ? true : false;
$values = tipograf($values, $tip_type, $tip_kill);
}*/
}
$activation = True;
break;
}
//типографика
$tip_reg = False;
/* if ($item[$i]->typograf == 1 and $tip_reg){
$jerrors = null;
$jevix = new Jevix();
$values = $jevix->parse($values, $jerrors);
write_log($jevix->parse('\"Зениит\"', $jerrors));
$values = tp_quotes($values);
}*/
$exxmlwhere = '';
//экслюзивность значения, в остальных полях идет сброс его
if (isset($item[$i]->exclusive) and $item[$i]->exclusive->value !== $values) {
$exclusives[$posts] = array('value'=>$values, 'reset'=>$item[$i]->exclusive->value);
if (isset($item[$i]->exclusive->where))
$exxmlwhere = trim($item[$i]->exclusive->where);
if ($exxmlwhere !== '') {
$exclusives[$posts]['where'] = $item[$i]->exclusive->where;
}
if (isset($item[$i]->exclusive->wherefields)){
$exclusives[$posts]['wherefields'] = explode(',', $item[$i]->exclusive->wherefields);
}
else
$exxmlwhere = '';
}
if ($type != 'file') {
$_SESSION['errors'][$posts] = $err_value;
$_SESSION['value'][$posts] = $values;
}
if (!$errresult) {
if ($activation) {
if ($posts !== '') {
$values = chr(39).$values.chr(39);
$posts = '`'.$posts.'`';
if ($action == 'insert') {
$incolumns .= SqlAddSpec($incolumns, 0).$posts;
$value_insert .= SqlAddSpec($value_insert, 0).$values;
if ($type == 'checkdate' and $checkdated) {
$incolumns .= SqlAddSpec($incolumns, 0). separ($item[$i]->fieldate);
$value_insert .= SqlAddSpec($value_insert, 0).quote(date('Y-m-d G:i:s'));
}
}
else if ($action == 'update') {
// if ($item[$i]->filter) {
// if (isset($_GET[$posts]) && (int)$_GET[$posts] != $values) {
// $div_res = 'content';
// $admin_res = 'selectall';
// }
// }
if ($values !== null)
$sql_update .= SqlAddSpec($sql_update, 0).$posts.' = '.$values;
if ($type == 'checkdate' and $checkdated) { #публикация
$actupdate = separ($item[$i]->fieldate).' = '.quote(date('Y-m-d G:i:s'));
//$actupdate = 'UPDATE '.$maintable.' SET '.$actupdate.' WHERE '.separ($increment).' = '."'".$increment_value."'".' AND '.separ($item[$i]->fieldate).' = '.quote('0000-00-00 00:00:00').' OR '.separ($item[$i]->fieldate).' IS NULL';
$actupdate = 'UPDATE '.$maintable.' SET '.$actupdate.' WHERE '.separ($increment).' = '."'".$increment_value."'".' AND '.separ($item[$i]->fieldate).' IS NULL';
mysql_query($actupdate) or write_log('Ошибка MySQL: '.mysql_error().' SQL:'.$actupdate);
# дата
$actupdate = 'UPDATE '.$maintable.' SET '.$actupdate.' WHERE '.separ($increment).' = '."'".$increment_value."'".' AND '.separ($item[$i]->fieldate).'='.quote('0000-00-00 00:00:00');
mysql_query($actupdate) or write_log('Ошибка MySQL: '.mysql_error().' SQL:'.$actupdate);
# user
if (isset($item[$i]->curruser)) {
$usersql = 'UPDATE '.$maintable.' SET '.separ($item[$i]->curruser).'='.$_SESSION['user_id'].' WHERE '.separ($increment).' = '."'".$increment_value."'";
mysql_query($usersql) or write_log('Ошибка MySQL: '.mysql_error().' SQL:'.$usersql);
}
}
}
}
}
}
}
//копим все полученные значения с ключом
$allvalues[$key] = $values;
}
if ($errresult) {
$_SESSION['ferror'] = 1;
$newaction = ($action == 'update') ? 'edit' : 'add';
echo("");
}
else {
$_SESSION['ferror'] = 0;
if ($action == 'insert')
$sqltext = 'INSERT INTO '.$maintable.' ('.$incolumns.') VALUES ('.$value_insert.')';
else if ($action == 'update')
$sqltext = 'UPDATE '.$maintable.' SET '. $sql_update.' WHERE '.$increment.' = '."'".$increment_value."'";
$sqlres = mysql_query($sqltext) or write_log('Ошибка MySQL: '.mysql_error().' sql:'.$sqltext);
if ($sqlres) { //если запрос прошел успешно
if ($action == 'insert') { // значение инкремента
$inc_indx = mysql_insert_id($this->link);
//echo 'созданный id = '.mysql_insert_id($this->link);
}
else
$inc_indx = $increment_value;
//обработка экслюзивных значений
if (isset($exclusives)) {
foreach ($exclusives as $exc_key => $excl){
$exupdwhere = ' WHERE '.$exc_key.' = '.$excl['value'].' AND '.$increment.' <> '."'".$inc_indx."'";
if (isset($excl['where'])){
$exwhere = $excl['where'];
preg_match_all("{(\w+)}", $exwhere, $exwherearr);
if (isset($exwherearr)) {
foreach ($exwherearr as $exwh){
$key = $exwh[1];
if (isset($allvalues[$key]));
$exwhere = str_replace('{'.$exwh[0].'}', $allvalues[$key], $exwhere);
}
}
$exupdwhere .= ' AND '.$exwhere;
}
if (isset($excl['wherefields'])){
foreach ($excl['wherefields'] as $exfvalue){
$exfvalue = trim($exfvalue);
if ($allvalues[$exfvalue] > 0) {
$exupdwhere .= ' AND '.separ($exfvalue).'='.$allvalues[$exfvalue];
break;
}
}
}
$upd_sql = 'UPDATE '.$maintable.' SET '.$exc_key.'='.$excl['reset'].$exupdwhere;
write_log('exclusive: '.$upd_sql);
mysql_query($upd_sql) or write_log('Ошибка MySQL: '.mysql_error().' SQL:'.$upd_sql);
}
}
for ($l = 0; $l < $fa; $l ++) { //загрузка файло
$indx = $file_increment[$l];
$column = (string)$item[$indx]->column;
if(isset($_FILES[$column])){
if ($_FILES[$column]["name"] != '') {
$f_exp = fileexpansion($_FILES[$column]["name"]);
if (in_array($f_exp, array('jpg','jpeg','JPG', 'gif', 'png', 'swf'))) {
$newfilename = site_fold.$item[$indx]->folder.'/'.$inc_indx.'.'.$f_exp;
if (copy($_FILES[$column]["tmp_name"], $newfilename)) {
$file_update = mysql_query('UPDATE '.$maintable.' SET '.$column.' = '."'".$f_exp."'".' WHERE '.$increment.' = '.$inc_indx);
chmod($newfilename, 0666);
}
else
echo "";
}
unlink($_FILES[$column]["tmp_name"]);
}
}
if (isset($_POST['DFile_'.$column])) //признак или несуществующего файла
$file_update = mysql_query('UPDATE '.$maintable.' SET '.$column.' = '."''".' WHERE '.$increment.' = '.$inc_indx) or write_log('Ошибка MySQL: '.mysql_error());
}
}
$increm = ($action == 'insert') ? $inc_indx : $increment_value;
define('INC_VALUE', $increm);
$history_save = True;
if ($history_save) {
$currdate = date('Y-m-d H:i:s'); //текущая дата
$histoty_file = site_fold_ad.'history/'.$nametable.'/'.date_to_url($currdate, False).'/'.$nametable.'_'.$increm.'_'.date_to_url($currdate, True,'_').'.sql'; //файл истории запроса sql
echo $histoty_file;
save($histoty_file, $sqltext); //сохраняем историю
}
if (isset($fcache))
delete_cache($fcache, $increm);
if (isset($count_items))
counts($count_items, $allvalues);
write_log($_SERVER['PHP_AUTH_USER'].': '.'table='.$maintable.':action='.$action.':id='.$inc_indx.' save:'.$histoty_file, 'log/edition.log');
echo("");
}
break;
case "delete":
if (isset($_GET['increment']))
$increment_value = (int)($_GET['increment']);
else
$increment_value = 0;
if ($increment_value > 0)
$sql_delete = mysql_query ('DELETE FROM '.$maintable.' WHERE '.$increment.' = '."'".$increment_value."'");
if (isset($fcache))
delete_cache($fcache, $increment_value);
write_log($_SERVER['PHP_AUTH_USER'].': '.'table='.$maintable.':action=delete :id='.$increment_value, 'log/edition.log');
echo("");
break;
}
}
}