<?php

define('H', $_SERVER['DOCUMENT_ROOT'] . '/');

require H.'sys/inc/start.php';
require H.'sys/inc/sess.php';
require H.'sys/inc/settings.php';
require H.'sys/inc/db_connect.php';
require H.'sys/inc/ipua.php';
require H.'sys/inc/fnc.php';
require H.'sys/inc/user.php';
/*
if (@$user['id'] != 1) {
  die('Тех. работы!');
}
*/
function strbaselinks($str) {
    return preg_replace('#[GruzOK_Net]#', '[naSIMbe.ru]', base64_decode($str[1]));
}

$set['title'] = 'Загрузки';
require H.'sys/inc/thead.php';
title();
aut();

$url = $tools->keyGlobals('url', 'get', null);

$data = file_get_contents('http://media.gruzok.net/download/' . $url);
$data = preg_replace('#download#', 'loads', $data);
$str = preg_replace('#http://([А-яA-z0-9\-\.\_]+)#', '', $data);

$str2 = preg_replace('#/loads/screen/#', 'http://media.gruzok.net/loads/screen/', $str);


if (!preg_match('/screen/i', $str2)) {
    preg_match('|<a class="menu.*</a></div></div>|sei', $str, $menu);
    $menu = preg_replace('#</div>#', '', $menu);
} else {
    preg_match('|<div class="menu">(.*)</div></div><a|sei', $str, $menu);
}
?>
<div class="loads">
<?

if ($menu) {
    ?>
    <div class="menu"><?= is_array($menu) ? $menu[0] : $menu?></div>
    <?  
} else {
    preg_match('|<div class="menu">(.*)<a href="http://gruzok.net" class="menu_j">|sei', $data, $content);
    
    //print_r($content);
    
    $content = preg_replace('#<div class="menu">#', '', $content[1]);
    //$content = preg_replace('#\[GruzOK.Net\]#', '[naSIMbe.ru]', $content);
    //$content = preg_replace('#download/#', 'loads/', $content);
    $content = preg_replace('#sort#', 'zsort', $content);
    //$content = preg_replace_callback('#http://media.gruzok.net/loads/download/([A-z0-9\=]+)#', 'strbaselinks', $content);
    
    $array = explode('</div>', $content);

    $array[0] = preg_replace('#http://([A-z0-9\-\.\_\:\?\&\/\[\]\(\)\]]+)#', '', $array[0]);
    
    preg_match('#src="([A-z0-9\-\.\_\:\?\&\/]*)"#', $content, $screen);
    
    if (preg_match('#http://([A-z0-9\-\.\_\:\?\&\/\[\]\(\)\]]+).mp4#', $content, $mp4)) {
        $media = array(
            'type' => 'video', 
            'file' => 'http://' . $mp4[1] . '.mp4',
            'screen' => @$screen[1],
        );
    } elseif (preg_match('#http://([A-z0-9\-\.\_\:\?\&\/\[\]\(\)\]]+).mp3#', $content, $mp4)) {
        $media = array(
            'type' => 'audio', 
            'file' => 'http://' . $mp4[1] . '.mp3',
            'screen' => isset($screen[1]) ? $screen[1] : 'http://nasimbe.ru/style/icons/timon_icon.png',
        );
    }
    ?>
    <div class="menu menu-loads"><?= $array[0]?></div>
    <?   
    
    if (isset($media)) {
        $media['file'] = preg_replace('#loads#', 'download', $media['file']);
        ?>
        <div class="menu menu-loads" id="player">
          <img src="<?= $media['screen']?>" />
        </div>
        
        <script type="text/javascript">
        vars = {
          style: {w: '<?= ($media['type'] == 'video' ? "100%" : "110px")?>', h: '<?= ($media['type'] == 'video' ? "360px" : "110px")?>'},
          swf: '/sys/javascript/uppod/uppod-<?= $media['type']?>.swf',
          type: '<?= $media['type']?>', 
          file: '<?= $media['file']?>', 
          id: 'player',
          screen: '<?= $media['screen']?>',
          st: '/sys/javascript/uppod/style-<?= $media['type']?>.txt',
          st5: 'uppod<?= $media['type']?>',
        };		
        new Player(vars);
        </script>
        <?
    }    
    
    unset($array[0]);
    foreach($array AS $value) {
        if (preg_match('/Скачать/i', $value) || !isset($media)) {
            $value = preg_replace('#Скачать#', '<img src="/style/icons/d.gif" /> Скачать', $value);
            
            if (!preg_match('/.html/', $value)) {
              $value = preg_replace('#loads#', 'download', $value);
            }
            
            ?>
            <div class="menu menu-loads"><?= $value?></div>
            <?            
        }
    }
}
?>
</div>
<?

require H.'sys/inc/tfoot.php';