Файл: core/functions.php
Строк: 449
<?php
// ===== Получение информации о mp3-файле ======================================
function getMP3data($filename) {
$ret = array();
$ret['filename'] = $filename;
if (!@$f = fopen($filename, 'rb')) {
$ret['error'] = 'Error opening file.';
return $ret;
}
// Определение ID3 тегов
if ($s = getID3v1($filename)) { $ret['id3v1'] = $s; }
if ($s = getID3v2($filename)) { $ret['id3v2'] = $s; }
// Если есть id3v2 тег, то перед поиском mp3 фрейма сдвигаем указатель файла за id3v2 тег
if ($ret['id3v2']['size']) { fseek($f, $ret['id3v2']['size'] + 10); }
// Ищем mp3 фрейм. - 11111111-11111111-1111111? (0xFFF(E))
do {
while (fread($f, 1) != chr(255)) {
if (feof($f)) {
$ret['error'] = 'MP3 frame not found.';
return $ret;
}
}
$s = fread($f, 3);
$header = sprintf('%08b%08b%08b%08b', 255, ord($s[0]), ord($s[1]), ord($s[2]));
} while ($header[0] != 1 && $header[1] != 1 && $header[2] != 1) ;
// Нашли первый mp3 фрейм. Читаем информацию
if ($header[11] == 0) {
$ret['id'] == 'MPEG-2.5';
} else {
if ($header[12] == 1) { $ret['id'] = 'MPEG-1'; } else { $ret['id'] = 'MPEG-2'; }
}
$layers = array(
array(0, 3),
array(2, 1)
);
$ret['layer'] = $layers[$header[13]][$header[14]];
if ($header[15] == 0) { $ret['protect_CRC'] = true; }
$bitrates['MPEG-1'] = array(
1 => array(0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448),// MPEG-1 Layer I
2 => array(0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384),// MPEG-1 Layer II
3 => array(0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320) // MPEG-1 Layer III
);
$bitrates['MPEG-2'] = array(
1 => array(0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448),// MPEG-2 Layer I
2 => array(0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384),// MPEG-2 Layer II
3 => array(0, 8, 16, 24, 32, 64, 80, 56, 64, 128, 160, 112, 128, 256, 320) // MPEG-2 Layer III
);
$bitrates['MPEG-2.5'] = array(
1 => array(0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0),// MPEG-2.5 Layer I
2 => array(0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0),// MPEG-2.5 Layer II
3 => array(0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0) // MPEG-2.5 Layer III
);
$ret['bitrate'] = $bitrates[$ret['id']][$ret['layer']][bindec($header[16].$header[17].$header[18].$header[19])];
$frequency = array(
'MPEG-1' => array(
'0' => array(44100, 48000),
'1' => array(32000, 0),
),
'MPEG-2' => array(
'0' => array(22050, 24000),
'1' => array(16000, 0),
),
'MPEG-2.5' => array(
'0' => array(11025, 12000),
'1' => array(8000, 0),
),
);
$ret['frequency'] = $frequency[$ret['id']][$header[20]][$header[21]];
$ret['padding'] = $header[22];
$samplesPerFrame = array(
'MPEG-1' => array(1 => 384, 2 => 1152, 3 => 1152),
'MPEG-2' => array(1 => 384, 2 => 1152, 3 => 576),
'MPEG-2.5' => array(1 => 384, 2 => 1152, 3 => 576)
);
$modes = array(
0 => array('Stereo', 'Joint stereo'),
1 => array('Dual channel', 'Mono')
);
$ret['mode'] = $modes[$header[24]][$header[25]];
// Если режим = Joint Stereo
if ($header[24] == 0 && $header[25] == 1) {
$ret['Intensity stereo'] = $header[26];
$ret['MS stereo'] = $header[27];
}
$ret['Copyrighted'] = $header[28];
$ret['Original'] = $header[29];
$emphasises = array(
0 => array('None', '50/15ms'),
1 => array('', 'CCITT j.17')
);
$ret['Emphasis'] = $emphasises[$header[30]][$header[31]];
// =============================================================================
if ($ret['Mode'] != 'Mono' && $ret['id'] == 'MPEG-1') { $offset = 32; }
else if ($ret['Mode'] == 'Mono' && $ret['id'] == 'MPEG-1') { $offset = 17; }
else if ($ret['Mode'] == 'Mono' && ($ret['id'] == 'MPEG-2' || $ret['id'] == 'MPEG-2.5')) { $offset = 9; }
else { $offset = 17; }
fseek($f, $offset, SEEK_CUR);
$s = fread($f, 38);
if (substr($s, 0, 4) == 'VBRI') {
$ret['bitrate_mode'] = 'VBR';
$ret['VBR_header'] = 'VBRI';
$numberOfFrames = bindec(sprintf('%08b%08b%08b%08b', ord($s[14]),ord($s[15]),ord($s[16]),ord($s[17])));
$ret['nof'] = $numberOfFrames;
$duration = floor($numberOfFrames * $samplesPerFrame[$ret['id']][$ret['layer']] / $ret['frequency']);
} else
if (substr($s, 0, 4) == 'Xing') {
$ret['bitrate_mode'] = 'VBR';
$ret['VBR_header'] = 'Xing';
$numberOfFrames = bindec(sprintf('%08b%08b%08b%08b', ord($s[8]),ord($s[9]),ord($s[10]),ord($s[11])));
$duration = floor($numberOfFrames * $samplesPerFrame[$ret['id']][$ret['layer']] / $ret['frequency']);
} else {
$ret['bitrate_mode'] = 'CBR';
$datasize = filesize($filename);
if ($ret['id3v1']) { $datasize -= 128; }
if ($ret['id3v2']) { $datasize -= $ret['id3v2']['size'] - 10; }
$duration = @floor($datasize / ($ret['bitrate'] * 1000) * 8);
}
$ret['duration_str'] = sprintf('%02d:%02d',floor($duration/60),floor($duration-(floor($duration/60)*60)));
$ret['duration_str_hour'] = sprintf('%02d:%02d:%02d',floor($duration/3600),floor($duration/60),floor($duration -(floor($duration/60)*60)));
$ret['diration'] = (int)$duration;
fclose($f);
return $ret;
}
// ===== Получение информации о ID3v2 тегах ====================================
function getID3v2($filename) {
$ret = array();
if (!@$f = fopen($filename, 'rb')) {
$ret['error'] = 'Error opening file.';
return $ret;
}
$header = fread($f, 10);
$header = @unpack("a3signature/c1version_major/c1version_minor/c1flags/Nsize", $header);
if ($header['signature'] != 'ID3') {
fclose($f);
return false;
}
$ret['version'] = $header['version_major']. '.' .$header['version_minor'];
$bsize = sprintf('%032b', $header['size']);
$ret['size'] = bindec(substr($bsize, 1, 7).substr($bsize, 9, 7).substr($bsize, 17, 7).substr($bsize, 25, 7));
fclose($f);
return $ret;
}
// ===== Получение информации о ID3v1 тегах ====================================
function getID3v1($filename) {
$ret = array();
$f = fopen($filename, 'rb');
if (!@$f = fopen($filename, 'rb')) {
$ret['error'] = 'Error opening file.';
return $ret;
}
fseek($f, -128, SEEK_END);
$s = fread($f,128);
if ($s[125] == chr(0) && $s[126] != chr(0)) {
// ID3v1.1
$format = 'a3tag/a30name/a30artists/a30album/a4year/a28comment/x1/c1track/c1genreno';
} else {
// ID3v1.0
$format = 'a3tag/a30name/a30artists/a30album/a4year/a30comment/c1genreno';
}
$ret = unpack($format, $s);
fclose($f);
if ($ret['tag'] == 'TAG') { return $ret; }
return false;
}
function head($data) {
global $user;
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?= $data['title'] ?></title>
<link rel="stylesheet" href="<?= HOME ?>style.css" type="text/css" />
<?php if (TRACK_PLAYER): ?>
<script src="<?= HOME ?>/player/audio.min.js"></script>
<script>
audiojs.events.ready(function() {
var as = audiojs.createAll();
});
</script>
<?php endif; ?>
</head>
<body>
<div style="float: right;">
<a href="<?= HOME ?>lang.php?type=ru"><img src="<?= HOME ?>/img/ru.png" alt="[RU]"/></a>
<a href="<?= HOME ?>lang.php?type=en"><img src="<?= HOME ?>/img/en.png" alt="[EN]"/></a>
<span style="margin-top: 5px;"><a href="<?= HOME ?>lang.php?type=ua"><img src="<?= HOME ?>/img/ua.png" alt="[UA]"/></a></span>
</div>
<div class="logo">
<a href="<?= HOME ?>"><img src="<?= HOME ?>img/loggo.gif"/></a>
</div>
<div class="title"><?= $data['title'] ?></div>
<?php if (AUTH_VK) { ?>
<div class="menu">
<?php
if (!empty($_SESSION['user_id'])):
?>
<img src="<?= HOME ?>/users_photos/<?= $user['id'] ?>.jpg" alt="" width="32" height="32" /> <b><?= $user['first_name'] ?></b> <a href="<?= HOME ?>logout.php">[Выход]</a>
<?php
else:
?>
Авторизоваться с помощью:<br /><a href="<?= HOME ?>auth/vk/auth.php" title="Вконтакте"><img src="<?= HOME ?>img/vk.png" width="24"/></a>
<?php
endif;
echo '</div>';
}
?>
<?php
}
function footer() {
?>
<div class="copy">
<div style="float: left; padding-top: 32px; padding-left: 5px;">
© 2014
</div>
<img src="<?= HOME ?>img/coppy.gif"/>
</div>
<div style="clear: both;"></div>
<div class="counts">
<?= counts() ?>
</div>
</body>
</html>
<?php
}
function lang($item) {
if (empty($_COOKIE['lang']) OR $_COOKIE['lang'] == 'ru') {
require(ROOT . '/langs/ru.php');
} else if ($_COOKIE['lang'] == 'ua') {
require(ROOT . '/langs/ua.php');
} else {
require(ROOT . '/langs/en.php');
}
return $array[$item];
}
function redirect($url) {
?>
<script>
window.location = "<?= $url ?>"
</script>
<?php
exit;
}
function rus2translit($string) {
$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',
);
return strtr($string, $converter);
}
function str2url($str) {
$str = rus2translit($str);
$str = strtolower($str);
$str = preg_replace('~[^-a-z0-9_.]+~u', '-', $str);
$str = trim($str, "-");
return $str;
}
function code($str){
if(iconv('UTF-8', 'UTF-8', $str) != $str){
$str = iconv('Windows-1251', 'UTF-8//TRANSLIT', $str);
}
return $str;
}
function create_poster($file,$id,$ext = 'jpg')
{
$getID3 = new getID3;
$ThisFileInfo = $getID3->analyze($file);
$name = $id.'.'.$ext;
if(!empty($ThisFileInfo['id3v2']['APIC']['0']['data']))
{
file_put_contents($name,$ThisFileInfo['id3v2']['APIC']['0']['data']);
return true;
}else{
return false;
}
}
function pagenation($page, $count_pages, $modrew = TRUE) {
/*
$back - ?????? ?????
$forward - ?????? ??????
$startpage - ?????? ? ??????
$endpage - ?????? ? ?????
$page2left - ?????? ???????? ?????
$page1left - ?????? ???????? ?????
$page2right - ?????? ???????? ??????
$page1right - ?????? ???????? ??????
*/
$uri = '?';
if (!$modrew) {
/* ???? ???? ????????? ? ???????? ?????? */
if ($_SERVER['QUERY_STRING']) {
unset($_GET['page']);
foreach ($_GET as $key => $value) {
$uri .= $key . '=' . $value . '&';
}
}
} else {
$url = $_SERVER['REQUEST_URI'];
$url = explode("?", $url);
if (!empty($url[1])) {
$params = explode("&", $url[1]);
foreach ($params as $param) {
if (!preg_match("#page=#", $param)) {
$uri .= $param .'&';
}
}
}
}
if ($page > 1) {
$back = '<a class="nav-link" href="'.$uri.'page='.($page - 1).'"><</a>';
}
if ($page < $count_pages) {
$forward = '<a class="nav-link" href="'.$uri.'page='.($page + 1).'">></a>';
}
if ($page > 3) {
$startpage = '<a class="nav-link" href="'.$uri.'page=1">«</a>';
}
if ($page < ($count_pages - 2)) {
$endpage = '<a class="nav-link" href="'.$uri.'page='.$count_pages.'">»</a>';
}
if (($page - 2) > 0) {
$page2left = '<a class="nav-link" href="'.$uri.'page='.($page - 2).'">'.($page - 2).'</a>';
}
if (($page - 1) > 0) {
$page1left = '<a class="nav-link" href="'.$uri.'page='.($page - 1).'">'.($page - 1).'</a>';
}
if (($page + 2) <= $count_pages) {
$page2right = '<a class="nav-link" href="'.$uri.'page='.($page + 2).'">'.($page + 2).'</a>';
}
if (($page + 1) <= $count_pages) {
$page1right = '<a class="nav-link" href="'.$uri.'page='.($page + 1).'">'.($page + 1).'</a>';
}
return '<div class="pagination">' . $startpage . $back . $page2left . $page1left . '<span class="nav-active">' . $page . '</span>' . $page1right . $page2right . $forward . $endpage . '</div>';
}
function ResizeImage ($filename, $size = 300, $quality = 85, $path_save, $new_filename)
{
/*
* ????? ?????????? ??? ?????????? ????????
*/
$dir = $path_save;
/*
* ????????? ?????? ???????????, ?? ???? ????????
* ??????? ??????????? ????? ????????? ?????
*/
$ext = strtolower(strrchr(basename($filename), "."));
/*
* ?????????? ???????
*/
$extentions = array('.jpg', '.gif', '.png', '.bmp');
if (in_array($ext, $extentions)) {
$percent = $size; // ?????? ??????????? ?????????
list($width, $height) = getimagesize($filename); // ?????????? ?????? ? ??????
$newheight = $height * $percent;
$newwidth = $newheight / $width;
$thumb = imagecreatetruecolor($percent, $newwidth);
switch ($ext) {
case '.jpg':
$source = @imagecreatefromjpeg($filename);
break;
case '.gif':
$source = @imagecreatefromgif($filename);
break;
case '.png':
$source = @imagecreatefrompng($filename);
break;
case '.bmp':
$source = @imagecreatefromwbmp($filename);
}
/*
* ??????? ?????????, ??????????? ???????????
*/
imagecopyresized($thumb, $source, 0, 0, 0, 0, $percent, $newwidth, $width, $height);
/*
* ??????? ???????????
*/
switch ($ext) {
case '.jpg':
imagejpeg($thumb, $dir . $new_filename, $quality);
break;
case '.gif':
imagegif($thumb, $dir . $new_filename);
break;
case '.png':
imagepng($thumb, $dir . $new_filename, $quality);
break;
case '.bmp':
imagewbmp($thumb, $dir . $new_filename);
break;
}
} else {
return false;
}
/*
* ??????? ??????????? ?????? ??????? ?? ????????? ??????,
* ??????? ????????????? ??? ???????? ?????????
*/
@imagedestroy($thumb);
@imagedestroy($source);
return true;
}
function getIP() {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
function getStars($rating = 0) {
if (!$rating or 0.5 > $rating) {
return '<img src="'.HOME.'img/retyng.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>';
}
if ($rating >= 0.5 && 1 > $rating) {
return '<img src="'.HOME.'img/retyng2.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>';
}
if ($rating >= 1 && $rating < 1.5) {
return '<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>';
}
if ($rating >= 1.5 && $rating < 2) {
return '<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng2.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>';
}
##
if ($rating >= 2 && $rating < 2.5) {
return '<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>';
}
if ($rating >= 2.5 && $rating < 3) {
return '<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng2.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>';
}
if ($rating >= 3 && $rating < 3.5) {
return '<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>';
}
if ($rating >= 3.5 && $rating < 4) {
return '<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng2.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>';
}
if ($rating >= 4 && $rating < 4.5) {
return '<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng.png" alt=""/>';
}
if ($rating >= 4.5 && $rating < 5) {
return '<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng2.png" alt=""/>';
}
if ($rating >= 5) {
return '<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng1.png" alt=""/>
<img src="'.HOME.'img/retyng1.png" alt=""/>';
}
return $rating;
}
function size_file($filesize=0)
{
$filesize_ed='B';
if ($filesize>=1024){$filesize= round($filesize/1024 , 2);$filesize_ed='KB';}
if ($filesize>=1024){$filesize= round($filesize/1024 , 2);$filesize_ed='MB';}
if ($filesize>=1024){$filesize= round($filesize/1024 , 2);$filesize_ed='GB';}
if ($filesize>=1024){$filesize= round($filesize/1024 , 2);$filesize_ed='TB';}
return $filesize.$filesize_ed;
}
function get_index_new($str, $limit = 4) {
$array = explode("<br />", $str);
$count = count($array);
$count_for = $count;
$new_str = '';
if ($limit >= $count) {
foreach ($array as $value) {
$new_str .= $value . '<br />';
}
return $new_str;
} else {
for ($i = 0; $i <= $limit; $i++) {
$new_str .= $array[$i] . '<br />';
$count_for--;
}
return $new_str . ($count_for > 0 ? '<a href="news.php" class="button">И еще '. $count_for .'</a>' : NULL);
}
}
?>