<?php
class func
{
public static function shema_logo()
{
return HOME . '/assets/css/logo.png';
}
public static function passgen($length)
{
$vals = "abcdefghijklmnopqrstuvwxyz0123456789";
$result = '';
for ($i = 1; $i <= $length; $i++) {
$result .= $vals{rand(0, strlen($vals))};
}
return $result;
}
public static function Send_Email($data)
{
$sets = 'support@' . $_SERVER['HTTP_HOST'];
$adds = "From: <" . $sets . ">\r\n";
$adds .= "Content-Type: text/plain; charset=\"utf-8\"\r\n";
mail($data['email'], '=?utf-8?B?' . base64_encode($data['title']) . '?=', $data['msg'], $adds);
}
public static function CompressedImage($source, $destination, $quality = 90)
{
$info = getimagesize($source);
if($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
imagejpeg($image, $destination, $quality);
return $destination;
}
public static function htmlEscaping($str)
{
return $str;
}
public static function getDuration($duration)
{
$h = floor($duration / 3600);
$m = floor(($duration % 3600) / 60);
if($m < 10)
$m = '0' . $m;
$s = ($duration % 3600) % 60;
if($s < 10)
$s = '0' . $s;
return $m . ":" . $s . "";
}
public static function upFirstLetter($str, $encoding = 'UTF-8')
{
return mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding) . mb_substr($str, 1, null, $encoding);
}
public static function multi_server_video_url($id, $ext, $ip, $namevideo = false)
{
global $settings;
$ipservers = require_once FILE_IP_SERVERS;
if($ip == $ipservers[1]) $pdir = 'sfiles';
if($ip == $ipservers[2]) $pdir = 's2';
if($ip == $ipservers[3]) $pdir = 's3';
if($ip == $ipservers[4]) $pdir = 's4';
if($ip == $ipservers[5]) $pdir = 's5';
if($ip == $ipservers[6]) $pdir = 's6';
if($ip == $ipservers[7]) $pdir = 's7';
$name = $id . '.mp4';
$secret = 'cndfaza1541SV';
$time = time() + 18000; //ссылка будет рабочей три часа
$address = $_SERVER['REMOTE_ADDR'];
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
$link = base64_encode(md5($secret . '/video_cdn_catalog/' . $name . $time . $address, true));
$key = str_replace("=", "", strtr($link, "+/", "-_"));
$encoded_url = "/$pdir/cdn_video/$key/$time/$name";
$arr['online'] = $encoded_url;
$arr['download'] = $encoded_url . '?download=video&vname=' . $namevideo . '&vsite=' . $_SERVER['HTTP_HOST'] . '_';
return $arr;
}
public static function server_video_url($id, $ext)
{
global $settings;
return '/sfiles/video/' . $id . '.' . $ext;
}
public static function server_screen_url($id, $type = false, $realid = false)
{
global $settings;
if($type == 1){
return '/files/screen/' . $realid . '.jpg';
}
return '/sfiles/screen/' . $id . '/thumb.jpg';
}
public static function server_screen_original_url($id)
{
global $settings;
return '/sfiles/screen/' . $id . '/original.jpg';
}
public static function displayScreenCategory($id, $cache = false)
{
return HOME . '/files/category/' . $id . '.jpg' . ($cache ? ('?' . fileatime(PATH_CATEGORY . $id . '.jpg')) : '');
}
public static function displayUrl_video($url, $id)
{
return HOME . '/' . $url . '-' . $id . '.html';
}
public static function displayUrl_category($url, $id)
{
return HOME . '/' . $url . '/';
}
public static function displayUrl_tag($url, $id)
{
return HOME . '/tag/' . $url . '/';
}
public static function show_404()
{
global $settings, $document, $db;
header($_SERVER['SERVER_PROTOCOL'] . " 404 Not Found");
$document['title'] = '404 Страница не найдена';
require_once ROOT . 'head.php';
require_once ROOT . '404.php';
require_once ROOT . 'foot.php';
exit;
}
public static function get_category($id, $symbol = false, $temp = false)
{
global $db;
$out = null;
$table = 'video_cat';
$res = $db->where("id_video", $id)->get($table);
if(!$res) return 'Без категорий';
$count = count($res);
foreach ($res as $key => $val) {
$data = $db->where("id", $val['id_cat'])->getOne("cat");
$out .= '<a rel="category tag" href="' . func::displayUrl_category($data['url'], $data['id']) . '">' . $data['name'] . '</a>';
$out .= $count != ($key + 1) ? ($symbol ? ', ' : ' ') : null;
}
return $out;
}
public static function get_category_array($id, $table_cat)
{
global $db;
$res = $db->where("id_video", $id)->get($table_cat);
foreach ($res as $val) {
$result[$val['id_cat']] = $val['id_cat'];
}
return $result;
}
public static function text($msg)
{
$msg = htmlspecialchars(trim($msg), ENT_QUOTES, 'UTF-8');
$msg = stripslashes($msg);
$msg = nl2br($msg);
return $msg;
}
public static function text_h($msg)
{
$msg = ($msg);
return $msg;
}
public static function error_display($msg, $val = false)
{
global $lng;
echo '
<div class="a_block"><center>' . $msg . '</center></div>';
}
public static function ok_delete($url, $text)
{
global $db, $lng, $set_theme, $pref, $settings;
echo '<div class="title">' . $text . '</div>
<a class="menu3" title="' . $lng['delete_yes'] . '" href="' . $url . '&ok_delete=1">' . $lng['delete_yes'] . '</a>
<a class="menu3" href="' . htmlspecialchars(getenv("HTTP_REFERER")) . '">' . $lng['back'] . '</a></div></div>';
require_once ROOT . '/foot.php';
exit;
}
public static function display_message($out = false)
{
if(!empty($_SESSION['message'])) {
$type = (isset($_SESSION['type']) ? $_SESSION['type'] : 'success');
$out = '<div class="alert alert-' . $type . '">' . $_SESSION['message'] . '</div>';
unset($_SESSION['message'], $_SESSION['type']);
}
return $out;
}
public static function goMsg($text = '', $url = '', $type = 'success')
{
$_SESSION['message'] = $text;
$_SESSION['type'] = $type;
header('Location: ' . (empty($url) ? URI : $url)) . exit;
}
public static function escape($msg)
{
global $db;
$msg = htmlspecialchars(trim($msg), ENT_QUOTES, 'UTF-8');
$msg = $db->escape($msg);
$msg = stripslashes($msg);
return $msg;
}
public static function int($var)
{
return abs(intval($var));
}
public static function TimeRu($time = null)
{
if(!$time)
$time = time();
$data = date('j.n.y', $time);
if($data == date('j.n.y'))
$res = 'Сегодня в ' . date('G:i', $time);
elseif ($data == date('j.n.y', time() - 86400))
$res = 'Вчера в ' . date('G:i', $time);
elseif ($data == date('j.n.y', time() - 172800))
$res = 'Позавчера в ' . date('G:i', $time);
else {
$m = array(
'0',
'Янв',
'Фев',
'Мар',
'Апр',
'Май',
'Июн',
'Июл',
'Авг',
'Сен',
'Окт',
'Ноя',
'Дек');
$res = date('j ' . $m[date('n', $time)] . ' Y в G:i', $time);
$res = str_replace(date('Y'), '', $res);
}
return $res;
}
public static function ScanDir($dirname)
{
$list = scandir($dirname);
unset($list[0], $list[1]);
return array_values($list);
}
public static function removeDir($dirname, $rmdir = false)
{
$files = glob($dirname . '/*', GLOB_MARK);
foreach ($files as $file) {
(substr($file, -1) == '/') ? func::removeDir($file) : unlink($file);
}
if($rmdir) {
if(is_dir($dirname))
rmdir($dirname);
}
}
public static function cut_text($text, $col)
{
$count = mb_strlen($text, 'UTF-8');
$res = mb_substr($text, 0, $col, 'UTF-8');
if($count > $col)
$res = $res . '...';
return $res;
}
public static function mb_ucwords($str)
{
$str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
return ($str);
}
public static function DeleteUrl($text)
{
if($text = preg_replace("/(https?:\/\/)?(www\.)?([-а-яa-zёЁцушщхъфырэчстью0-9_\.]{2,}\.)(рф|[a-z]{2,6})((\/[-а-яёЁцушщхъфырэчстьюa-z0-9_]{1,})?\/?([a-z0-9_-]{2,}\.[a-z]{2,6})?(\?[a-z0-9_]{2,}=[-0-9]{1,})?((\&[a-z0-9_]{2,}=[-0-9]{1,}){1,})?)/i",
"*", $text))
return $text;
return false;
}
public static function trans($str)
{
$str = mb_strtolower($str, 'UTF-8');
$str = strtr($str, array(
'а' => 'a',
'б' => 'b',
'в' => 'v',
'г' => 'g',
'д' => 'd',
'е' => 'e',
'ё' => 'e',
'ж' => 'j',
'з' => 'z',
'и' => 'i',
'й' => 'i',
'к' => 'k',
'л' => 'l',
'м' => 'm',
'н' => 'n',
'о' => 'o',
'п' => 'p',
'р' => 'r',
'с' => 's',
'т' => 't',
'у' => 'u',
'ф' => 'f',
'х' => 'h',
'ц' => 'c',
'ч' => 'ch',
'ш' => 'sh',
'щ' => 'sch',
'ъ' => "",
'ы' => 'y',
'ь' => "",
'э' => 'ye',
'ю' => 'yu',
'я' => 'ya',
' ' => '_'));
$str = ltrim($str);
return preg_replace('/[^a-z0-9_\-\.]/i', '', $str);
}
public static function get_size_file($url)
{
$x = array_change_key_case(get_headers($url, 1), CASE_LOWER);
return (strcasecmp($x[0], 'HTTP/1.1 200 OK') != 0) ? $x['content-length'][1] : $x['content-length'];
}
public static function size_file($file, $type = false)
{
if($type) {
$filesize = $file;
} else {
if(!file_exists($file))
return "Файл не найден/File not found";
$filesize = filesize($file);
}
$size = array(
'b',
'Kb',
'Mb',
'Gb');
if($filesize > pow(1024, 3)) {
$n = 3;
} elseif ($filesize > pow(1024, 2)) {
$n = 2;
} elseif ($filesize > 1024) {
$n = 1;
} else {
$n = 0;
}
$filesize = ($filesize / pow(1024, $n));
$filesize = round($filesize, 1);
return $filesize . ' ' . $size[$n];
}
public static function resize($path_to_file, $path_to_save, $width, $height = 0, $quality = 100)
{
if(!file_exists($path_to_file))
return false;
$info = getimagesize($path_to_file);
$format = strtolower(substr($info['mime'], strpos($info['mime'], '/') + 1));
$picfunc = 'imagecreatefrom' . $format;
$old_width = $info[0];
$old_height = $info[1];
$horizontal = $width / $old_width;
$vertical = $height / $old_height;
if($height == 0) {
$vertical = $horizontal;
$height = $vertical * $old_height;
} elseif ($width == 0) {
$horizontal = $vertical;
$width = $horizontal * $old_width;
}
$ratio = min($horizontal, $vertical);
if($horizontal == $ratio)
$use_horizontal = true;
else
$use_horizontal = false;
$new_width = $use_horizontal ? $width : floor($old_width * $ratio);
$new_height = !$use_horizontal ? $height : floor($old_height * $ratio);
$new_left = $use_horizontal ? 0 : floor(($width - $new_width) / 2);
$new_top = !$use_horizontal ? 0 : floor(($height - $new_height) / 2);
$pic_to_src = $picfunc($path_to_file);
$pic_to_save = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($pic_to_save, 0, 0, 0);
imagefill($pic_to_save, 0, 0, $white);
imagecopyresampled($pic_to_save, $pic_to_src, $new_left, $new_top, 0, 0, $new_width, $new_height, $old_width, $old_height);
$ext = @array_pop(explode('.', $path_to_save));
switch ($ext) {
case 'jpg':
case 'jpeg':
imagejpeg($pic_to_save, $path_to_save, $quality);
break;
case 'gif':
imagegif($pic_to_save, $path_to_save);
break;
case 'png':
imagepng($pic_to_save, $path_to_save);
break;
default:
imagepng($pic_to_save, $path_to_save);
break;
}
imagedestroy($pic_to_src);
imagedestroy($pic_to_save);
return true;
}
public static function watermark($file, $watermark)
{
if(empty($file) | empty($watermark))
return false;
$wh = getimagesize($watermark);
$fh = getimagesize($file);
$rwatermark = imagecreatefrompng($watermark);
$rfile = imagecreatefromjpeg($file);
imagecopy($rfile, $rwatermark, $fh[0] - $wh[0], $fh[1] - $wh[1], 0, 0, $wh[0], $wh[1]);
imagejpeg($rfile, $file, '90');
imagedestroy($rwatermark);
imagedestroy($rfile);
return true;
}
public static function is_mkdir($IDIR)
{
if(!is_dir($IDIR)) {
mkdir($IDIR, 0777);
chmod($IDIR, 0777);
}
}
public static function location($url)
{
header('location: ' . $url) . exit;
}
public static function csrfGenerate()
{
return $_SESSION['csrfToken'] = md5(uniqid(null, true));
}
public static function csrfField()
{
return '<input name="csrfToken" type="hidden" value="' . func::csrfGenerate() . '">';
}
public static function csrfHas()
{
if(filter_has_var(INPUT_POST, 'csrfToken') && array_key_exists('csrfToken', $_SESSION)) {
if(filter_input(INPUT_POST, 'csrfToken', FILTER_UNSAFE_RAW) === $_SESSION['csrfToken']) {
return true;
}
}
return false;
}
public static function cryptoJsAesDecrypt($passphrase, $jsonString)
{
$jsondata = json_decode($jsonString, true);
try {
$salt = hex2bin($jsondata["s"]);
$iv = hex2bin($jsondata["iv"]);
}
catch (exception $e) {
return null;
}
$ct = base64_decode($jsondata["ct"]);
$concatedPassphrase = $passphrase . $salt;
$md5 = array();
$md5[0] = md5($concatedPassphrase, true);
$result = $md5[0];
for ($i = 1; $i < 3; $i++) {
$md5[$i] = md5($md5[$i - 1] . $concatedPassphrase, true);
$result .= $md5[$i];
}
$key = substr($result, 0, 32);
$data = openssl_decrypt($ct, 'aes-256-cbc', $key, true, $iv);
return json_decode($data, true);
}
public static function getpages($url)
{
global $settings;
$curl = curl_init();
if($settings['proxy_curl']) {
$proxy_arr = explode(':', $settings['proxy_curl']);
curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($curl, CURLOPT_PROXY, trim($proxy_arr[0] . ':' . $proxy_arr[1]));
curl_setopt($curl, CURLOPT_PROXYUSERPWD, trim($proxy_arr[2] . ':' . $proxy_arr[3]));
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36 OPR/25.0.1614.68');
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_REFERER, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
}