315 lines
12 KiB
PHP
315 lines
12 KiB
PHP
<?php
|
||
|
||
class class_SYSTEM{
|
||
function __construct(){
|
||
}
|
||
/**
|
||
* Загрузка файла
|
||
* @param $name
|
||
* @param string $ddir
|
||
* @param int $replacefile
|
||
* @return array
|
||
*/
|
||
public function uploadfile($name, $ddir = '', $replacefile = 0){
|
||
GLOBAL $ST;
|
||
$out = array();
|
||
$file = '';
|
||
//$uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/upload/';
|
||
$uploaddir = 'upload/';
|
||
if ($ddir != '') $uploaddir .= $ddir . '/';
|
||
if (!file_exists($uploaddir)) mkdir($uploaddir, 0777, true);
|
||
if (isset($_FILES[$name])) {
|
||
$pi = pathinfo($_FILES[$name]['name']);
|
||
$fx = $pi['extension'];
|
||
$fn = $pi['filename'];
|
||
|
||
if(isset($_SESSION['user'])){
|
||
$rf = $_SESSION['user']['id'].'_'.$this->translit($fn) . '.' . $fx;
|
||
}else{
|
||
$rf = '0_'.$this->translit($fn) . '.' . $fx;
|
||
}
|
||
$uploadfile = $uploaddir . $rf;
|
||
if($replacefile==0) {
|
||
if (file_exists($uploadfile)) {
|
||
if (isset($_SESSION['user'])) {
|
||
$rf = $_SESSION['user']['id'] . '_' . $this->translit($fn) . '_' . mt_rand(1000, 9999) . '.' . $fx;
|
||
} else {
|
||
$rf = '0_' . $this->translit($fn) . '_' . mt_rand(1000, 9999) . '.' . $fx;
|
||
}
|
||
}
|
||
$uploadfile = $uploaddir . $rf;
|
||
}
|
||
$fisset=false;
|
||
if($replacefile==2) {//2ой режим если есть файл то не грузим его
|
||
if(file_exists($uploadfile)) $fisset=true;
|
||
$out['file'] = $rf;
|
||
$out['path'] = $uploaddir;
|
||
$out['err'] = 0;
|
||
$out['msg'] = 'файл существовал';
|
||
}
|
||
if(!$fisset){
|
||
if (move_uploaded_file($_FILES[$name]['tmp_name'], $uploadfile)) {
|
||
$out['file'] = $rf;
|
||
$out['path'] = $uploaddir;
|
||
$out['err'] = 0;
|
||
$out['msg'] = 'файл загрузили';
|
||
} else {
|
||
$out['file'] = '';
|
||
$out['path'] = $uploaddir;
|
||
$out['err'] = 1;
|
||
$out['err1'] = $_FILES[$name]["error"];
|
||
$out['msg'] = 'файл не загрузили';
|
||
}
|
||
}
|
||
}
|
||
return $out;
|
||
}
|
||
public function human_filesize($bytes) {
|
||
$bytes = floatval($bytes);
|
||
$arBytes = array(
|
||
0 => array(
|
||
"UNIT" => "Тб",
|
||
"VALUE" => pow(1024, 4)
|
||
),
|
||
1 => array(
|
||
"UNIT" => "Гб",
|
||
"VALUE" => pow(1024, 3)
|
||
),
|
||
2 => array(
|
||
"UNIT" => "Мб",
|
||
"VALUE" => pow(1024, 2)
|
||
),
|
||
3 => array(
|
||
"UNIT" => "Кб",
|
||
"VALUE" => 1024
|
||
),
|
||
4 => array(
|
||
"UNIT" => "б",
|
||
"VALUE" => 1
|
||
),
|
||
);
|
||
|
||
foreach($arBytes as $arItem)
|
||
{
|
||
if($bytes >= $arItem["VALUE"])
|
||
{
|
||
$result = $bytes / $arItem["VALUE"];
|
||
$result = str_replace(".", "," , strval(round($result, 2)))." ".$arItem["UNIT"];
|
||
break;
|
||
}
|
||
}
|
||
return $result;
|
||
}
|
||
public function translit($st)
|
||
{
|
||
$a = array_merge(array_combine(preg_split('//u', "абвгдеёзийклмнопрстуфхцьыэАБВГДЕЁЗИЙКЛМНОПРСТУФХЦЬЫЭabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"), preg_split('//u', "abvgdeeziyklmnoprstufhc'ieABVGDEEZIYKLMNOPRSTUFHC'IEabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_")), array("ж" => "zh", "ч" => "ch", "ш" => "sh", "щ" => "shch", "ъ" => "", "ю" => "yu", "я" => "ya", "Ж" => "Zh", "Ч" => "Ch", "Ш" => "Sh", "Щ" => "Shch", "Ъ" => "", "Ю" => "Yu", "Я" => "Ya"));
|
||
$r = preg_split('//u', $st);
|
||
$out = '';
|
||
foreach ($r as $v) {
|
||
if (isset($a[$v]))
|
||
$out .= $a[$v];
|
||
}
|
||
return $out;
|
||
}
|
||
function translit_text($value){
|
||
$converter = array(
|
||
'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
|
||
'е' => 'e', 'ё' => 'e', 'ж' => 'zh', 'з' => 'z', 'и' => 'i',
|
||
'й' => 'y', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n',
|
||
'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't',
|
||
'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch',
|
||
'ш' => 'sh', 'щ' => 'sch', 'ь' => '', 'ы' => 'y', 'ъ' => '',
|
||
'э' => 'e', 'ю' => 'yu', 'я' => 'ya',
|
||
|
||
'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
|
||
'Е' => 'E', 'Ё' => 'E', 'Ж' => 'Zh', 'З' => 'Z', 'И' => 'I',
|
||
'Й' => 'Y', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N',
|
||
'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T',
|
||
'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Ch',
|
||
'Ш' => 'Sh', 'Щ' => 'Sch', 'Ь' => '', 'Ы' => 'Y', 'Ъ' => '',
|
||
'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya',
|
||
);
|
||
|
||
$value = strtr($value, $converter);
|
||
return $value;
|
||
}
|
||
|
||
function file_force_download($file) {
|
||
if (file_exists($file)) {
|
||
// сбрасываем буфер вывода PHP, чтобы избежать переполнения памяти выделенной под скрипт
|
||
// если этого не сделать файл будет читаться в память полностью!
|
||
if (ob_get_level()) {
|
||
ob_end_clean();
|
||
}
|
||
// заставляем браузер показать окно сохранения файла
|
||
header('Content-Description: File Transfer');
|
||
header('Content-Type: application/octet-stream');
|
||
header('Content-Disposition: attachment; filename=' . basename($file));
|
||
header('Content-Transfer-Encoding: binary');
|
||
header('Expires: 0');
|
||
header('Cache-Control: must-revalidate');
|
||
header('Pragma: public');
|
||
header('Content-Length: ' . filesize($file));
|
||
// читаем файл и отправляем его пользователю
|
||
readfile($file);
|
||
exit;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Переделываем массив в формат данных
|
||
* @param $head (array)
|
||
* @param $data (array)
|
||
* @return array
|
||
*/
|
||
function format_array_prepare($head,$data=array()){
|
||
$out = array();
|
||
if(!count($data)){//вернем название полей
|
||
foreach ($head as $k => $v){
|
||
$out[] = $v['name'];
|
||
}
|
||
}else{//Вернем уже данные
|
||
$m1 = array(' ','"');
|
||
$m2 = array(' ','"');
|
||
foreach ($head as $k => $v){
|
||
if(isset($data[$k])) {
|
||
if ($v['type'] == 'string') $out[] = str_replace($m1,$m2,$data[$k]);
|
||
if ($v['type'] == 'data') $out[] = date('d.m.Y', $data[$k]);
|
||
if ($v['type'] == 'time') $out[] = date('H:i:s', $data[$k]);
|
||
if ($v['type'] == 'timedata') $out[] = date('H:i:s d.m.Y', $data[$k]);
|
||
}else{
|
||
$out[] = '';
|
||
}
|
||
}
|
||
}
|
||
return $out;
|
||
}
|
||
|
||
function format($data){
|
||
$out = array();
|
||
if($data['format']=='json'){
|
||
$out['json'] = json_encode($data['items'],JSON_UNESCAPED_UNICODE);
|
||
if(!isset($data['file_get'])){
|
||
echo $out['json'];
|
||
}else{
|
||
return $out['json'];
|
||
}
|
||
}
|
||
if($data['format']=='csv'){
|
||
if(isset($data['filename'])){
|
||
$file = 'upload/' . $data['dir'] . '/' . $data['filename'];
|
||
}else {
|
||
$file = 'upload/' . $data['dir'] . '/' . $data['dir'] . '_' . date('H_d.m.Y') . '.csv';
|
||
}
|
||
//if(!file_exists($file)){
|
||
file_put_contents($file,$this->utf8ToWin1251('"'.implode('";"',$this->format_array_prepare($data['head'])).'"').PHP_EOL);
|
||
foreach ($data['items'] as $k => $v){
|
||
file_put_contents($file, $this->utf8ToWin1251('"' . implode('";"', $this->format_array_prepare($data['head'],$v)) . '"').PHP_EOL,FILE_APPEND);
|
||
}
|
||
//}
|
||
$out['file'] = $file;
|
||
if(!isset($data['file_get'])){
|
||
$SYS = new class_SYSTEM();
|
||
$SYS->file_force_download($file);
|
||
}else{
|
||
return $out['file'];
|
||
}
|
||
}
|
||
}
|
||
function utf8ToWin1251($f){
|
||
//return mb_convert_encoding($f, "UTF-8", "WINDOWS-1251");
|
||
return iconv( "UTF-8", "cp1251", $f );
|
||
}
|
||
|
||
|
||
function info_last_files($data){
|
||
$dir = $data['dir'];
|
||
$files = scandir($dir);
|
||
$out = '<b>Список 5 последних выгрузок:</b><ul>';
|
||
$items = array();
|
||
foreach($files as $k => $f)if($f!='.'&&$f!='..'){
|
||
$file = $dir.$f;
|
||
$pi = pathinfo($file);
|
||
$time = filemtime($file);
|
||
$items[$time] = array(
|
||
'time'=>$time,
|
||
'url'=>'/integration/'.$file,
|
||
'name'=>$pi['filename'],
|
||
'size'=>$this->human_filesize(filesize($file))
|
||
);
|
||
}
|
||
krsort($items);
|
||
$items = array_slice($items, 0, 5);
|
||
foreach($items as $time => $f){
|
||
$out .= '<li>'.date('H:i d.m.Y',$f['time']).' <a href="'.$f['url'].'" target="_blank">'.$f['name'].'</a> '.$f['size'].'</li>';
|
||
}
|
||
$out .= '</ul>';
|
||
return $out;
|
||
}
|
||
|
||
|
||
function cCurl($data){
|
||
$out = array();
|
||
$ch = curl_init($data['url']);
|
||
if(count($data['post'])){
|
||
curl_setopt($ch, CURLOPT_POST, 1);
|
||
if(isset($data['post']['files'])) {
|
||
foreach($data['post']['files'] as $k => $file) {
|
||
$data['post']['files_'.$k] = new \CURLFile( $file['path'],$file['mime'],$file['name']);
|
||
}
|
||
unset($data['post']['files']);
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data['post']);
|
||
}else{
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data['post']));
|
||
}
|
||
}
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||
curl_setopt($ch, CURLOPT_HEADER, false);
|
||
$response = curl_exec($ch);
|
||
$err = curl_error($ch);
|
||
curl_close($ch);
|
||
|
||
//$curl = curl_init();
|
||
//$option = array(
|
||
// CURLOPT_URL => $data['url'],
|
||
// CURLOPT_RETURNTRANSFER => true,
|
||
// CURLOPT_ENCODING => "",
|
||
// CURLOPT_MAXREDIRS => 10,
|
||
// CURLOPT_TIMEOUT => 30,
|
||
// CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1
|
||
//);
|
||
//if(isset($data['JWTToken'])){
|
||
// $option[CURLOPT_HTTPHEADER]=array(
|
||
// "authorization: Bearer ".$data['JWTToken'],
|
||
// "content-type: application/json"
|
||
// );
|
||
//}
|
||
//if(count($data['post'])){
|
||
// $option[CURLOPT_CUSTOMREQUEST] = 'POST';
|
||
// if(isset($data['post']['files'])) {
|
||
// $files = array();
|
||
// foreach($data['post']['files'] as $k => $file) {
|
||
// $data['post']['files'][$k] = curl_file_create($file['path'],$file['mime'],$file['name']);
|
||
// }
|
||
// }
|
||
// //echo '<pre>'.print_r($data,1).'</pre>';
|
||
// $option[CURLOPT_POSTFIELDS] = $data['post'];
|
||
//}else{
|
||
// $option[CURLOPT_CUSTOMREQUEST] = 'GET';
|
||
//}
|
||
//curl_setopt_array($curl, $option);
|
||
//$response = curl_exec($curl);
|
||
//$err = curl_error($curl);
|
||
//curl_close($curl);
|
||
if ($err) {
|
||
$out['err']=1;
|
||
$out['msg']='cURL Error #:' . $err;
|
||
} else {
|
||
$out['err']=0;
|
||
$out['msg']='cURL Ok';
|
||
$out['response']=$response;
|
||
}
|
||
return $out;
|
||
}
|
||
} |