<?php
$druh = "soubory";
include "head.php";
include_head($lang['files_title'], $druh);

if (!isset($_GET['action'])) {$_GET['action'] = "default";};
switch ($_GET['action']) {

case "add-folder" :

if (!empty($_POST)) {

  $nazev = $_POST['nazev'];
  
  if (!empty($nazev)) {
  
    $query = mysql_query("SELECT * FROM jtcms_soubory_slozky WHERE nazev='".mysql_real_escape_string($nazev)."'");
    $radku = mysql_num_rows($query);
    
    if ($radku == 0) {
    
      mysql_query("INSERT INTO jtcms_soubory_slozky (nazev,datum,pridal) VALUES ('".mysql_real_escape_string($nazev)."',NOW(),'".intval($_SESSION['userid'])."')");
      $query = mysql_query("SELECT id FROM jtcms_soubory_slozky WHERE nazev='".mysql_real_escape_string($nazev)."' LIMIT 1");
      $result = mysql_fetch_array($query);
      $id = $result['id'];
      
      $umask_old = umask(0000);
      mkdir("./data/files/".$id, 0777);
      umask($umask_old);

      Header("Location: files.php?action=folder&id=".rawurlencode($id)."&ins=1&edit=edit");
      echo '<script type="text/javascript">location.replace("files.php?action=folder&id='.rawurlencode($id).'&ins=1&edit=edit")</script>';
          
    } else {
      echo '<div class="oznameni_div">';
      echo '<div class="chyba">'.$lang['files_error'].'</div>';
      echo '<p>'.$lang['files_er_1'].'</p>';
      echo '</div>';    
    };  
          
  } else {
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['galleries_error'].'</div>';
    echo '<p>'.$lang['files_er_2'].'</p>';
    echo '</div>';  
  };

};

break;

case "folder" :
$id = $_GET['id']; 
$query = mysql_query("SELECT * FROM jtcms_soubory_slozky WHERE id='".intval($id)."' LIMIT 1");
$radku = mysql_num_rows($query);
$result = mysql_fetch_array($query);
$nazev = $result['nazev'];
  
 if (empty($id) or $radku == 0) {
    echo '<br />';  
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['files_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['files_er_3'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['files_er_4'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  };
  
$delete_but = mysql_query("SELECT COUNT(*) AS pocet FROM jtcms_soubory WHERE slozka='".intval($id)."'");
$delete_but = mysql_fetch_array($delete_but);
$delete_but = $delete_but['pocet'];                       

if (($prava == 1) or ($prava == 2 && $result['pridal'] == $_SESSION['userid'])) {
  echo '<div class="filtr_but" onclick="$(\'#files_add_files_form\').slideToggle();"><img src="stuff/img/icons/edit.png" alt="" /> '.$lang['files_edit_folder'].'</div>';
  if ($delete_but != 0) {echo '<div class="filtr_but" onclick="$(\'.checkbox_obal, .checkbox_submit\').slideToggle();"><img src="stuff/img/icons/trash.png" alt="" /> '.$lang['files_delete_files'].'</div>';};
  echo '<a class="filtr_but" href="files.php?action=synchronization&amp;id='.$result['id'].'"><img src="stuff/img/icons/refresh.png" alt="" /> '.$lang['files_synchronization'].'</a>';
};
 
echo '<h1 class="tal">'.$nazev.'</h1>';
echo '<br />';

if (!isset($_GET['work'])) {$_GET['work'] = "default";};
switch ($_GET['work']) {

case "deleted" :
echo '<div class="oznameni_div">';
echo '<p>'.$lang['files_work_2'].' '.$_GET['pocet'].' '.$lang['files_work_3'].'</p>';
echo '</div>';
break;

case "synchronizationed" :
echo '<div class="oznameni_div">';
echo '<p>'.$lang['files_work_4'].'</p>';
echo '</div>';
break;

};

if (!isset($_GET['ins'])) {$_GET['ins'] = "default";};
switch ($_GET['ins']) {

case "1" :
echo '<div class="oznameni_div">';
echo '<p>'.$lang['files_ins_1'].'</p>';
echo '</div>';
break;

};

if (!empty($_POST)) {
  $nazev = $_POST['nazev'];
  $pole = $_FILES['soubor'];
  
  if (!empty($nazev) && ($prava == 1 or ($prava == 2 && $result['pridal'] == $_SESSION['userid']))) {

    $nazev = mysql_real_escape_string($nazev);
    mysql_query("UPDATE jtcms_soubory_slozky SET nazev='".$nazev."' WHERE id='".intval($id)."' LIMIT 1");
    
    if (!empty($pole['tmp_name'][0])) {
    echo '<div class="gallery_upload_div">';
      $c = 0;
      $query2 = mysql_query("SELECT * FROM jtcms_nastaveni WHERE typ='soubory_max_upload' LIMIT 1");
      $result2 = mysql_fetch_array($query2);
  
      for ($i = 0; $i < count($pole['tmp_name']); $i++) {      
        if ($pole['size'][$i] < ($result2['hodnota']+1) &&  is_file($pole['tmp_name'][$i]) && !file_exists("./data/files/".$id."/".$pole['name'][$i])) {
          move_uploaded_file($pole['tmp_name'][$i], "data/files/".$id."/".$pole['name'][$i]);
          if (file_exists("./data/files/".$id."/".$pole['name'][$i])) {
            $img_nazev = mysql_real_escape_string($pole['name'][$i]);
            $img_typ = mysql_real_escape_string($pole['type'][$i]);
            $img_size = mysql_real_escape_string($pole['size'][$i]);
            
            mysql_query("INSERT INTO jtcms_soubory (nazev,typ,velikost,stazeni,pridal,slozka,datum) VALUES ('".$img_nazev."','".$img_typ."','".$img_size."','0','".intval($_SESSION['userid'])."','".intval($id)."',NOW())");
            echo '<li><img src="stuff/img/icons/tick.png" alt="" /> '.$pole['name'][$i].'</li>';
            $c++;
          };
        } else {
          echo '<li title="';
            if ($pole['size'][$i] > $result2['hodnota']) {echo $lang['files_er_5'].' '.$result2['hodnota'].'. ';};
            if (!is_file($pole['tmp_name'][$i])) {echo $lang['files_er_6'].' ';};
            if (file_exists("./data/gal/".$id."/".$pole['name'][$i])) {echo $lang['files_er_7'];};
          echo '">';
          echo '<img src="stuff/img/icons/delete.png" alt="" /> '.$pole['name'][$i].'</li>';
        };
      };
    echo '<div class="nf"></div>';
    echo '</div><br />';
    };
      
  } else {
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['files_error'].'</div>';
    if (empty($nazev)) {echo '<li>'.$lang['galleries_er_2'].'</li>';};
    if ($prava == 2 && $result['pridal'] != $_SESSION['userid']) {echo '<li>'.$lang['galleries_er_8'].'</li>';};
    echo '</div>';
  };
};

$query = mysql_query("SELECT * FROM jtcms_soubory WHERE slozka='".intval($id)."' ORDER BY id DESC");
$radku = mysql_num_rows($query);
?>

  <form method="post" enctype="multipart/form-data" action="files.php?action=folder&amp;id=<?php echo $id; ?>" id="files_add_files_form"<?php if (!empty($_POST) or (isset($_GET['edit']) && $_GET['edit'] == "edit")) {echo ' style="display: block;"';}; ?>>
    <label for="fil_nazev"><?php echo $lang['files_name']; ?>:</label> <input type="text" name="nazev" id="fil_nazev" class="input" value="<?php if (empty($_POST['nazev'])) {echo $result['nazev'];} else {echo $_POST['nazev'];}; ?>" /><br />
    <label for="fil_soubory"><?php echo $lang['files_select_file']; ?>:</label> <input type="file" min="1" id="fil_soubory" name="soubor[]" max="999" class="file_input" multiple /><br />
    <br />
    <input type="submit" name="submit" value="<?php echo $lang['files_add_files']; ?>" class="submit" />
  </form>

<form method="post" action="files.php?action=delete&amp;id=<?php echo $id; ?>" id="files_delete_files_form">
<div class="tac checkbox_submit">  
  <input type="submit" name="submit" class="submit" value="<?php echo $lang['files_delete_files']; ?>" />
</div>
<br />

<table class="soubory_tabulka">
<tr class="prvni_tr">
  <td class="td1 first">&nbsp;<?php echo $lang['files_id']; ?></td>
  <td class="td2"><?php echo $lang['files_name']; ?></td>
  <td class="td3"><?php echo $lang['files_type']; ?></td>
  <td class="td4"><img src="stuff/img/icons/download.png" alt="<?php echo $lang['files_number_of_download']; ?>" title="<?php echo $lang['files_number_of_download']; ?>" /></td>
  <td class="td5"><?php echo $lang['files_date']; ?></td>
  <td class="td6 last"></td>
</tr>

<?php
if ($radku == 0) {
  echo '<tr><td colspan="6" style="text-align: center; padding: 5px;">'.$lang['files_no_files'].'</td></tr>';
} else {

  while ($result = mysql_fetch_array($query)) {
    if (file_exists("./data/files/".$id."/".$result['nazev'])) {
      $query2 = mysql_query("SELECT jmeno FROM jtcms_uzivatele WHERE id='".$result['pridal']."' LIMIT 1");
      $result2 = mysql_fetch_array($query2);
      
      $pathinfo = pathinfo("./data/files/".$id."/".$result['nazev']);
      $type = mime_content_type("./data/files/".$id."/".$result['nazev']);
      $filesize = get_correct_size("./data/files/".$id."/".$result['nazev']);
    
      echo '<tr>';
      echo '<td class="id">&nbsp;'.$result['id'].'</td>';
      echo '<td><span class="curh" title="'.$lang['files_size'].' '.$filesize.'">'.$pathinfo['filename'].'</span></td>';
      echo '<td><span class="curh" title="'.$type.'">'.$pathinfo['extension'].'</span></td>';
      echo '<td>'.$result['stazeni'].'</td>';
      echo '<td class="datum">';
        echo '<div class="links">';
          echo '<div>';
            echo '<a href="files.php?action=code&amp;id='.$result['id'].'" title="'.$lang['files_get_code'].'"><img src="stuff/img/icons/wizard.png" alt="'.$lang['files_get_code'].'" /></a>';
          echo '</div>';
        echo '</div>';   
        echo '<div class="datum" title="'.StrFTime("%H:%M %d.%m.%Y", strtotime($result["datum"])).'">'.StrFTime("%d.%m.%Y", strtotime($result["datum"])).'</div>';
        echo '<span class="autor">'.$result2['jmeno'].'</span>';
      echo '</td>';
      echo '<td><div class="checkbox_obal"><input type="checkbox" name="soubor[]" value="'.$result['nazev'].'" /></div></td>';
      echo '</tr>';
      echo "\n\r";
    };
  };
};

echo '</table>';
echo '</form>'; 
  
break;

case "delete" :
$id = $_GET['id']; 
$query = mysql_query("SELECT * FROM jtcms_soubory_slozky WHERE id='".intval($id)."' LIMIT 1");
$radku = mysql_num_rows($query);
$result = mysql_fetch_array($query);
$nazev = $result['nazev'];
  
 if (empty($id) or $radku == 0 && ($prava == 1 or ($prava == 2 && $result['pridal'] == $_SESSION['userid']))) {
    echo '<br />';  
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['files_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['files_er_3'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['files_er_4'].'</p>';};
    if (!($prava == 1 or ($prava == 2 && $result['pridal'] == $_SESSION['userid']))) {echo '<p>'.$lang['files_er_8'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  };
  
if (!empty($_POST)) {

  $soubor = $_POST['soubor'];
  
  if (!empty($soubor)) {
    $pocet = 0;
    foreach ($soubor as $val) {
      unlink("./data/files/".$id."/".$val);
      mysql_query("DELETE FROM jtcms_soubory WHERE nazev='".mysql_real_escape_string($val)."' and slozka='".intval($id)."' LIMIT 1");
      $pocet++;
    };

    Header("Location: files.php?action=folder&id=".$id."&work=deleted&pocet=".$pocet);
    echo '<script type="text/javascript">location.replace("files.php?action=folder&id='.$id.'&work=deleted&pocet='.$pocet.'")</script>';
            
  } else {
    echo '<br />';  
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['files_error'].'</div>';
    echo '<p>'.$lang['files_er_9'].'</p>';
    echo '</div>';
  };

} else {
  Header("Location: files.php?action=folder&id=".$id);
  echo '<script type="text/javascript">location.replace("files.php?action=folder&id='.$id.'")</script>';
};

break;

case "delete-folder" :
$id = $_GET['id']; 
$query = mysql_query("SELECT * FROM jtcms_soubory_slozky WHERE id='".intval($id)."' LIMIT 1");
$radku = mysql_num_rows($query);
$result = mysql_fetch_array($query);
$nazev = $result['nazev'];
  
 if (empty($id) or $radku == 0 && ($prava == 1 or ($prava == 2 && $result['pridal'] == $_SESSION['userid']))) {
    echo '<br />';  
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['files_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['files_er_3'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['files_er_4'].'</p>';};
    if (!($prava == 1 or ($prava == 2 && $result['pridal'] == $_SESSION['userid']))) {echo '<p>'.$lang['files_er_8'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  };

    $adresar = opendir("data/files/".$id."/");
    while ($soubor = readdir($adresar)){
        if (!is_dir($soubor)) {
          unlink("./data/files/".$id."/".$soubor);
          mysql_query("DELETE FROM jtcms_soubory WHERE nazev='".mysql_real_escape_string($soubor)."' and slozka='".intval($id)."' LIMIT 1");
        };
    };
    
    rmdir("./data/files/".$id."/");
  
    mysql_query("DELETE FROM jtcms_soubory_slozky WHERE id='".$id."' LIMIT 1");
    Header("Location: files.php?work=deleted&nazev=".rawurlencode($nazev));
    echo '<script type="text/javascript">location.replace("files.php?work=deleted&nazev='.rawurlencode($nazev).'")</script>'; 
          
break;

case "code" :  
  $id = $_GET['id'];
  $query = mysql_query("SELECT * FROM jtcms_soubory WHERE id='".intval($id)."' LIMIT 1");
  $radku = mysql_num_rows($query);
  $result = mysql_fetch_array($query);

 if (empty($id) or $radku == 0) {
    echo '<br />';
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['files_error'].'</div>';
    if (empty($slozka)) {echo '<p>'.$lang['files_er_11'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['files_er_10'].'</p>';};
    echo '</div>';
    
    include_end();                
    exit();               
  };
  
  $soubor = $result['nazev'];                              
?>

<h1><?php echo $lang['files_view_code_1']; ?></h1> 
<br /> 
<div class="kod_vlozeni kod_vlozeni_files" id="kod">
<?php echo $lang['files_view_code_2']; ?> <strong><?php echo $soubor; ?></strong><br /><br />
<?php echo $lang['files_view_code_3']; ?><br /><br />
<textarea wrap="hard">
<?php echo '<a href="jtcms_download.php?id='.$id.'">'.$soubor.'</a>'; ?>
</textarea>
</div>

<?php
break;

case "synchronization" :
$id = $_GET['id']; 
$query = mysql_query("SELECT * FROM jtcms_soubory_slozky WHERE id='".intval($id)."' LIMIT 1");
$radku = mysql_num_rows($query);
$result = mysql_fetch_array($query);
  
 if (empty($id) or $radku == 0) {
    echo '<br />';  
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['files_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['files_er_3'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['files_er_4'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  };
  
  $query = mysql_query("SELECT * FROM jtcms_soubory WHERE slozka='".$id."'");
  
  while ($result = mysql_fetch_array($query)) {
    if (!file_exists("./data/files/".$id."/".$result['nazev'])) {
      mysql_query("DELETE FROM jtcms_soubory WHERE id='".$result['id']."' LIMIT 1");
    };
  };
  
  $adresar = opendir("./data/files/".$id);
  while ($soubor = readdir($adresar)) {
  if ($soubor != "." && $soubor != "..") {
    $query2 = mysql_query("SELECT * FROM jtcms_soubory WHERE slozka='".$id."' and nazev='".mysql_real_escape_string($soubor)."' LIMIT 1");
    $radku2 = mysql_num_rows($query2);
    $result2 = mysql_fetch_array($query2);
    
    if ($radku2 == 0) {
      $nazev = mysql_real_escape_string($soubor);
      $typ = mime_content_type("./data/files/".$id."/".$soubor);
      $velikost = filesize("./data/files/".$id."/".$soubor);
      
      mysql_query("INSERT INTO jtcms_soubory (nazev,typ,velikost,stazeni,pridal,slozka,datum) VALUES ('".$nazev."','".$typ."','".$velikost."','0','".$_SESSION['userid']."','".$id."',NOW())");
    } else {
      $velikost = filesize("./data/files/".$id."/".$soubor);
      
      if ($result['velikost'] != $velikost) {
        mysql_query("UPDATE jtcms_soubory SET velikost='".$velikost."' WHERE id='".$result['id']."' LIMIT 1");
      };
    };
  };
  };

  Header("Location: files.php?action=folder&id=".$id."&work=synchronizationed");
  echo '<script type="text/javascript">location.replace("files.php?action=folder&id='.$id.'&work=synchronizationed")</script>';
    
break;

default :      
?>

<div class="filtr_but" onclick="$('#files_add_folder_form').slideToggle();"><img src="stuff/img/icons/add.png" alt="" /> <?php echo $lang['files_create_new_folder']; ?></div>

<h1><?php echo $lang['files_h1']; ?></h1>
<br />

<?php
if (!isset($_GET['work'])) {$_GET['work'] = "default";};
switch ($_GET['work']) {

case "deleted" :
echo '<div class="oznameni_div">';
echo '<p>'.$lang['files_folder'].' <span class="blue">'.stripslashes($_GET['nazev']).'</span> '.$lang['files_work_1'].'</p>';
echo '</div>';
break;

};
?>

  <form method="post" action="files.php?action=add-folder" id="files_add_folder_form">
    <label for="fil_nazev"><?php echo $lang['files_name']; ?>:</label> <input type="text" name="nazev" id="fil_nazev" class="input" /><br />
    <br />
    <input type="submit" name="submit" value="<?php echo $lang['files_create_new_folder']; ?>" class="submit" />
  </form>

<?php
$query = mysql_query("SELECT * FROM jtcms_soubory_slozky ORDER BY id DESC");
$radku = mysql_num_rows($query);
?>

<table class="soubory_slozky_tabulka">
<tr class="prvni_tr">
  <td class="td1 first">&nbsp;<?php echo $lang['files_id']; ?></td>
  <td class="td2"><?php echo $lang['files_name']; ?></td>
  <td class="td3"><img src="stuff/img/icons/lists.png" alt="<?php echo $lang['files_number_of_files_in_folder']; ?>" title="<?php echo $lang['files_number_of_files_in_folder']; ?>" /></td>
  <td class="td4 last"><?php echo $lang['files_date']; ?></td>
</tr>

<?php
if ($radku == 0) {
  echo '<tr><td colspan="4" style="text-align: center; padding: 5px;">'.$lang['files_no_folders'].'</td></tr>';  
} else {

  while ($result = mysql_fetch_array($query)) {
    $query2 = mysql_query("SELECT jmeno FROM jtcms_uzivatele WHERE id='".intval($result['pridal'])."' LIMIT 1");
    $result2 = mysql_fetch_array($query2);
  
    $query3 = mysql_query("SELECT COUNT(*) AS pocet FROM jtcms_soubory WHERE slozka='".$result['id']."'");
    $result3 = mysql_fetch_array($query3);
    $pocet = $result3['pocet'];
    
    echo '<tr>';
    echo '<td>&nbsp;'.$result['id'].'</td>';
    echo '<td><a href="files.php?action=folder&amp;id='.$result['id'].'">'.$result['nazev'].'</a></td>';
    echo '<td>';
      if ($pocet == 0) {
        echo '<span class="prazdno">'.$pocet.'</span>';
      } else {
        echo $pocet;
      };
    echo '</td>';
    echo '<td class="datum">';
      if (($prava == 2 && $result['pridal'] == $_SESSION['userid']) or $prava == 1) {
        echo '<div class="links">';
          echo '<div>';
            echo '<a href="files.php?action=folder&amp;id='.$result['id'].'&amp;edit=edit" title="'.$lang['files_edit_folder'].'"><img src="stuff/img/icons/edit.png" alt="" /></a>';
            echo '<img src="stuff/img/icons/trash.png" class="smaz" alt="'.$lang['files_delete_folder'].'" title="'.$lang['files_delete_folder'].'" onclick="smaz_slozku(\''.$result['id'].'\',\''.$result['nazev'].'\',\''.$pocet.'\')" />';
          echo '</div>';
        echo '</div>';
      };
      echo '<div class="datum" title="'.StrFTime("%H:%M %d.%m.%Y", strtotime($result["datum"])).'">'.StrFTime("%d.%m.%Y", strtotime($result["datum"])).'</div>';
      echo '<span class="autor">'.$result2['jmeno'].'</span>';
    echo '</td>';
    echo '</tr>';
    echo "\n\r";
  };
};

echo '</table>';
?>
<script type="text/javascript">
function smaz_slozku(id, nazev, pocet){
jConfirm("<?php echo $lang['files_really_del_1']; ?> "+nazev+"<?php echo $lang['files_really_del_2']; ?> "+pocet+" <?php echo $lang['files_really_del_3']; ?>", "<?php echo $lang['files_really_del_headline']; ?>", function(r) {
    if (r == true) {location.replace("files.php?action=delete-folder&id="+id);};
});
};
</script>
<?php
};

include_end();
?>
