<?php
set_time_limit(0);
// set up a connection or die
$ftp_server = ftp_connect('calabria.by') or die("Couldn't connect to ftp server");
if (!ftp_login($ftp_server, 'fesor@calabria.by', 'dragon'))
die("Couldn't connect to ftp server");
define("XCMS", true);
define("ROOT", dirname(__FILE__));
$clearcache = false;
//Load configs
$configs = require ROOT.'/data/main.php';
require ROOT.'/system/db/mysql.php';
$db = new db($configs['db']);
//Rename image files
if ($handle = opendir(ROOT.'/import/images/'))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && $file != parsefilename($file))
rename(ROOT.'/import/images/'.$file, ROOT.'/import/images/'.parsefilename($file));
}
closedir($handle);
}
//Search for input data
if ($handle = opendir(ROOT.'/import/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.')+1)) == '1ci')
{
$data = explode("\n", file_get_contents(ROOT.'/import/'.$file));
$keys = explode("\t", $data[0]);
foreach($keys as &$k)
$k = strtolower(trim($k));
unset($data[0]);
if(strtolower($file) == 'ingridients.1ci')
{
//$db->execute("TRUNCATE TABLE Ingridients");
foreach($data as $item)
{
}
}
else
{
//Remove old data and images
$db->execute("TRUNCATE TABLE Items");
$db->execute("TRUNCATE TABLE Groups");
@unlink(ROOT.'/cache/catlist.php');
if ($thandle = opendir(ROOT.'/images/')) {
while (false !== ($tfile = readdir($thandle))) {
if ($tfile != "." && $tfile != ".." && strtolower(substr($tfile, strrpos($tfile, '.')+1)) == 'jpg')
{
if($tfile != 'noimage.jpg'){}
//unlink(ROOT.'/images/'.$tfile);
}
}
}
if ($thandle = opendir(ROOT.'/images/thumbs/')) {
while (false !== ($tfile = readdir($thandle))) {
if ($tfile != "." && $tfile != ".." && strtolower(substr($tfile, strrpos($tfile, '.')+1)) == 'jpg')
{
if($tfile != 'noimage.jpg'){}
//unlink(ROOT.'/images/thumbs/'.$tfile);
}
}
}
//Parse data and make SQL query
$data = explode("\n", file_get_contents(ROOT.'/import/'.$file));
$keys = explode("\t", $data[0]);
foreach($keys as &$k)
$k = strtolower(trim($k));
unset($data[0]);
$sqlitems = "INSERT INTO Items (item_code,menu_code, category, name, weight, price, print_code, ingridients, image, exported, new) VALUES ";
$sqlgroups = "INSERT INTO Groups (`menu_code` , `name` , `category` , `order` ) VALUES ";
foreach($data as $item)
{
$item = explode("\t", $item);
foreach($item as &$l)
$l = trim($l);
$item = array_combine($keys, $item);
if($item['is_group']==1)
{
$item['category'] = $item['category'] == '' ? 0:$item['category'];
$item['order'] = $item['order']==''? '0':intval($item['order']);
$sqlgroups .= "('{$item['menu_code']}', '{$item['name']}', '{$item['category']}', '{$item['order']}'), ";
}
else
{
$item['image'] = parsefilename($item['image']);
//Processing image
if($item['image'] != '.' && file_exists(ROOT.'/import/images/'.$item['image']))
{
if(rename(ROOT.'/import/images/'.$item['image'], ROOT.'/images/'.$item['image']))
img_resize(ROOT.'/images/'.$item['image'], ROOT.'/images/thumbs/'.$item['image'], 170, 170);
}
if($item['image']=='.' || !file_exists(ROOT.'/images/'.$item['image']))
$item['image'] == '';
$sqlitems .= "({$item['item_code']}, {$item['menu_code']}, {$item['category']}, '{$item['name']}', '{$item['weight']}', '{$item['price']}', '{$item['print_code']}', '{$item['ingridients']}', '{$item['image']}', {$item['exported']}, {$item['new']}), ";
}
}
$sqlitems = substr($sqlitems, 0, -2);
$sqlgroups = substr($sqlgroups, 0, -2);
$db->execute($sqlitems);
$db->execute($sqlgroups);
}
chmod(ROOT.'/import/'.$file, 0777);
unlink(ROOT.'/import/'.$file);
}
}
closedir($handle);
}
function parsefilename($filename)
{
$filename = explode('.', $filename);
$ext = '.'.$filename[count($filename)-1];
unset($filename[count($filename)-1]);
$filename = implode('.', $filename);
return strtolower(ru2Lat(strtr($filename,
" *!@#$%^&*",
"----------")).$ext);
}
function ru2Lat($string)
{
$rus = array('ё','ж','ц','ч','ш','щ','ю','я','Ё','Ж','Ц','Ч','Ш','Щ','Ю','Я');
$lat = array('yo','zh','tc','ch','sh','sh','yu','ya','YO','ZH','TC','CH','SH','SH','YU','YA');
$string = str_replace($rus,$lat,$string);
$string = strtr($string,
"АБВГДЕЗИЙКЛМНОПРСТУФХЪЫЬЭабвгдезийклмнопрстуфхъыьэ",
"ABVGDEZIJKLMNOPRSTUFH_I_Eabvgdezijklmnoprstufh_i_e");
return($string);
}
/*
img_resize(root.
*/
function img_resize($src, $dest, $width, $height, $rgb = 0xFFFFFF, $quality = 75) {
if (!file_exists($src)) {
return false;
}
$size = getimagesize($src);
if ($size === false) {
return false;
}
$format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1));
$icfunc = 'imagecreatefrom'.$format;
if (!function_exists($icfunc)) {
return false;
}
$x_ratio = $width / $size[0];
$y_ratio = $height / $size[1];
if ($height == 0) {
$y_ratio = $x_ratio;
$height = $y_ratio * $size[1];
} elseif ($width == 0) {
$x_ratio = $y_ratio;
$width = $x_ratio * $size[0];
}
$ratio = min($x_ratio, $y_ratio);
$use_x_ratio = ($x_ratio == $ratio);
$new_width = $use_x_ratio ? $width : floor($size[0] * $ratio);
$new_height = !$use_x_ratio ? $height : floor($size[1] * $ratio);
$new_left = $use_x_ratio ? 0 : floor(($width - $new_width) / 2);
$new_top = !$use_x_ratio ? 0 : floor(($height - $new_height) / 2);
$isrc = $icfunc($src);
$idest = imagecreatetruecolor($width, $height);
imagefill($idest, 0, 0, $rgb);
imagecopyresampled($idest, $isrc, $new_left, $new_top, 0, 0, $new_width, $new_height, $size[0], $size[1]);
imagejpeg($idest, $dest, $quality);
imagedestroy($isrc);
imagedestroy($idest);
return true;
}
function win2utf($s) {
for($i=0, $m=strlen($s); $i<$m; $i++) {
$c=ord($s[$i]);
if ($c<=127) {$t.=chr($c); continue; }
if ($c>=192 && $c<=207) {$t.=chr(208).chr($c-48); continue; }
if ($c>=208 && $c<=239) {$t.=chr(208).chr($c-48); continue; }
if ($c>=240 && $c<=255) {$t.=chr(209).chr($c-112); continue; }
if ($c==184) { $t.=chr(209).chr(209); continue; };
if ($c==168) { $t.=chr(208).chr(129); continue; };
if ($c==184) { $t.=chr(209).chr(145); continue; }; #ё
if ($c==168) { $t.=chr(208).chr(129); continue; }; #Ё
if ($c==179) { $t.=chr(209).chr(150); continue; }; #і
if ($c==178) { $t.=chr(208).chr(134); continue; }; #І
if ($c==191) { $t.=chr(209).chr(151); continue; }; #ї
if ($c==175) { $t.=chr(208).chr(135); continue; }; #ї
if ($c==186) { $t.=chr(209).chr(148); continue; }; #є
if ($c==170) { $t.=chr(208).chr(132); continue; }; #Є
if ($c==180) { $t.=chr(210).chr(145); continue; }; #ґ
if ($c==165) { $t.=chr(210).chr(144); continue; }; #Ґ
if ($c==184) { $t.=chr(209).chr(145); continue; }; #Ґ
}
return $t;
}