<?php
/**
 * Поиск MP3 (ВКонтакте)
 *
 * Автор: byvlad
 * Версия: 2.12
 * Стоимость: 400 WMR
 */

/**
* Функция вывода размера файла
*/
function size($size) {
	return ($size >= 1048576) ? round($size/1048576*100) / 100 . ' Мб' : round($size/1024*100) / 100 . ' Кб';
}

/**
* Функция транслитирования
*/
function tr($str) {
	$translate = array(
	"A"=>"А","a"=>"а","B"=>"Б","b"=>"б","V"=>"В",
	"v"=>"в","G"=>"Г","g"=>"г","D"=>"Д","d"=>"д",
	"E"=>"Е","e"=>"е","yo"=>"Ё","Zh"=>"Ж","zh"=>"ж",
	"Z"=>"З","z"=>"з","I"=>"И","i"=>"и","J"=>"Й",
	"j"=>"й","K"=>"К","k"=>"к","L"=>"Л","l"=>"л",
	"M"=>"М","m"=>"м","N"=>"Н","n"=>"н","O"=>"О",
	"o"=>"о","P"=>"П","p"=>"п","R"=>"Р","r"=>"р",
	"S"=>"С","s"=>"с","T"=>"Т","t"=>"т","U"=>"У",
	"u"=>"у","F"=>"Ф","f"=>"ф","H"=>"Х","h"=>"х",
	"C"=>"Ц","c"=>"ц","Ch"=>"Ч","ch"=>"ч","Sh"=>"Ш",
	"sh"=>"ш","Sch"=>"Щ","sch"=>"щ","''"=>"ъ",
	"Y"=>"Ы","y"=>"ы","'"=>"ь","Ye"=>"Э",
	"ye"=>"э","Yu"=>"Ю","yu"=>"ю","Ya"=>"Я","ya"=>"я",
	"Yo"=>"ё","_"=>"%20","_"=>" ");
	return preg_replace('/[^a-z0-9_\-\.]/i', '', strtr($str, array_flip($translate)));
}

/**
* Функция экранирования опасных знаков
*/
function protect($text)
{
	global $mysqli;

	return trim($mysqli -> real_escape_string(htmlspecialchars($text, ENT_QUOTES, 'UTF-8')));
}

/**
* Функция загрузки страницы через CURL и отправки POST данных
*/
function LoadPage($post_url, $post_data, $refer){
	$ch = curl_init($post_url);

	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_REFERER, $refer);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.2.15 Version/10.00');

	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}

/**
* Функция для получения размера файла по URL
*/
function getSizeFile($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'];
}

/**
* Функция выполнения внешней программы через PROC канал
*/
function runExternal($cmd) {
    $descriptorspec = array(
        0 => array("pipe", "r"),
        1 => array("pipe", "w"),
        2 => array("pipe", "w")
    );
    $pipes = array();
    $process = proc_open($cmd, $descriptorspec, $pipes);

    $output= '';
    if (!is_resource($process))
        return false;

    fclose($pipes[0]);
    stream_set_blocking($pipes[1],false);
    stream_set_blocking($pipes[2],false);
    $todo = array($pipes[1], $pipes[2]);
    while( true ) {
        $read = array();
        if( !feof($pipes[1]) )
            $read[]= $pipes[1];

        if( !feof($pipes[2]) )
            $read[]= $pipes[2];
        if (!$read)
            break;

        $ready = @ stream_select($read, $write = NULL, $ex = NULL, 2);
        if ($ready === false)
            break;

        foreach ($read as $r) {
            $s = fread($r,1024);
            $output.= $s;
        }
    }
    fclose($pipes[1]);
    fclose($pipes[2]);
    $code = proc_close($process);
    return $output;
}

/**
* Функции BB кодов
*/
function BBcode($text)
{
    $bbcode = array(
        '/\[url\](.+)\[\/url\]/isU'                                       =>'<a href="$1">$1</a>',
        '/\[url=(.+)\](.+)\[\/url\]/isU'                                  =>'<a href="$1">$2</a>',
        '/\[br]/isU'                                                       =>'<br />',
        '/\[i\](.+)\[\/i\]/isU'                                           =>'<i>$1</i>',
         '/\[b\](.+)\[\/b\]/isU'                                           =>'<strong>$1</strong>',
        '/\[del\](.+)\[\/del\]/isU'                                       =>'<span style="text-decoration: line-through">$1</span>',
        '/\[u\](.+)\[\/u\]/isU'                                           =>'<span style="text-decoration:underline">$1</span>',
        '/\[big\](.+)\[\/big\]/isU'                                       =>'<big>$1</big>',
        '/\[small\](.+)\[\/small\]/isU'                                   =>'<small>$1</small>',
        '/\[color=(.+)\](.+)\[\/color\]/isU'                              =>'<span style="color:#$1">$2</span>',
        '/\[red\](.+)\[\/red\]/isU'                                       =>'<span style="color:#ff0000">$1</span>',
        '/\[yellow\](.+)\[\/yellow\]/isU'                                 =>'<span style="color:#ffff22">$1</span>',
        '/\[green\](.+)\[\/green\]/isU'                                   =>'<span style="color:#00bb00">$1</span>',
        '/\[blue\](.+)\[\/blue\]/isU'                                     =>'<span style="color:#0000bb">$1</span>');
     return preg_replace(array_keys($bbcode),array_values($bbcode),$text);
}

/**
* Функция вывода времени
*/
function clock($time) {
	$labelTime = date('d.m.Y', $time);

	$arrM = array(
		'01' => 'Января',
		'02' => 'Февраля',
		'03' => 'Марта',
		'04' => 'Апреля',
		'05' => 'Мая',
		'06' => 'Июня',
		'07' => 'Июля',
		'08' => 'Августа',
		'09' => 'Сентября',
		'10' => 'Октября',
		'11' => 'Ноября',
		'12' => 'Декабря'
	);

	if ($labelTime == date('d.m.Y')) {
		return 'Сегодня в '.date('H:i', $time);
	} elseif ($labelTime == (date('d') - 1).'.'.date('m.Y')) {
		return 'Вчера в '.date('H:i', $time);
	} else {
		return date('j '.$arrM[date('m', $time)].' Y в H:i', $time);
	}
}
?>