<?php
ini_set("display_errors", 0);
error_reporting(0);

/* GENERAL FUNCTIONS (administration) */

/* -------------------- INCLUDE HTML CODE -------------------- */
function include_head($title, $typ = 1, $tinymce = 2) {
  global $theme, $set_lang2, $lang, $jmeno, $skupina, $meta, $meta2, $menu2;
  
  if (!isset($meta)) {$meta = "";};
  if (!isset($meta2)) {$meta2 = "";};
  
  $user_info = mysql_query("SELECT set_tinymce FROM jtcms_uzivatele WHERE id='".intval($_SESSION['userid'])."' LIMIT 1");
  $user_info = mysql_fetch_array($user_info);
  
  if (empty($user_info['set_tinymce'])) {
    $query = mysql_query("SELECT hodnota FROM jtcms_nastaveni WHERE typ='tinymce' LIMIT 1");
    $result = mysql_fetch_array($query);
    $use_tinymce = $result['hodnota'];
  } else {
    $use_tinymce = $user_info['set_tinymce'];
  }; 

  if ($tinymce == 1 && $use_tinymce == 1) {
    $tinymce = '  
  <script type="text/javascript" src="stuff/tools/tinymce/jquery.tinymce.js"></script>
  <script type="text/javascript" src="stuff/tools/tinymce/jt_cms_set.php"></script>
    ';
  } else {
    $tinymce = '';
  };
  
  $query = mysql_query("SELECT hodnota FROM jtcms_nastaveni WHERE typ='pomocne_ikony' LIMIT 1");
  $result = mysql_fetch_array($query);
  
  $query = mysql_query("SELECT * FROM jtcms_skupiny WHERE id='".$skupina."' LIMIT 1");
  $result = mysql_fetch_array($query);

$menu = '<a href="login.php">'.$lang['login_title'].'</a>';
if ($result['clanky'] != 3) {$menu .= '<a href="articles.php">'.$lang['articles_title'].'</a>';}
if ($result['stranky'] != 3) {$menu .= '<a href="pages.php">'.$lang['pages_title'].'</a>';};
if ($result['galerie'] != 3) {$menu .= '<a href="galleries.php">'.$lang['galleries_title'].'</a>';};
if ($result['soubory'] != 3) {$menu .= '<a href="files.php">'.$lang['files_title'].'</a>';};
if ($result['komentare'] != 3) {$menu .= '<a href="comments.php">'.$lang['comments_title'].'</a>';};
if ($result['uzivatele'] != 3 or $result['skupiny'] != 3) {$menu .= '<a href="tools.php">'.$lang['tools_title'].'</a>';};
if ($result['nastaveni'] != 3 or $result['sablony'] != 3 or $result['banlist'] != 3 or $result['zacleneni'] != 3) {$menu .= '<a href="setting.php">'.$lang['setting_title'].'</a>';};

  $set_lang = strtolower(str_replace("_","-",$set_lang2));

  $hranate = array('[LANG]','[TITLE]','[THEME]','[META]','[TINYMCE]','[META2]','[USERNAME]','[WEB]','[LOGOUT]','[MENU]','[UP]','[DOWN]','[MENU2]');
  $kod = array($set_lang,$title,$theme,$meta,$tinymce,$meta2,$jmeno,$lang["head_web"],$lang["head_logout"],$menu,$lang['head_up'],$lang['head_down'], $menu2);

  $soubor = "stuff/themes/".$theme."/head.php";            
  $fopen = file_get_contents($soubor);
  $hlavicka = str_ireplace($hranate, $kod, $fopen);

  if ($tinymce == 1) {$tinymce_p = TRUE;} else {$tinymce_p = FALSE;};
  
   echo $hlavicka;
   
   if ($result[$typ] == 3) {
     echo '<h1>'.$lang['general_permission_error'].'</h1><br />';
     echo '<div class="oznameni_div">';
     echo '<p>'.$lang['general_permission_er_1'].'</p>';
     echo '</div>';
    
     include_end();
     exit();
   };
   
  return $tinymce_p;
}

function include_head_short($title) {
  global $theme, $set_lang2, $lang, $meta, $meta2;

  if (!isset($meta)) {$meta = "";};
  if (!isset($meta2)) {$meta2 = "";};
  
  $set_lang = strtolower(str_replace("_","-",$set_lang2));

  $hranate = array('[LANG]','[TITLE]','[THEME]','[META]','[META2]');
  $kod = array($set_lang,$title,$theme,$meta,$meta2);

  $soubor = "stuff/themes/".$theme."/head_short.php";            
  $fopen = file_get_contents($soubor);
  $hlavicka = str_ireplace($hranate, $kod, $fopen);

  echo $hlavicka;
}

function include_end() {
  global $theme, $meta_end;
  
  if (!isset($meta_end)) {$meta_end = "";};
   
  $hranate = array('[META]');
  $kod = array($meta_end);

  $soubor = "stuff/themes/".$theme."/end.php";            
  $fopen = file_get_contents($soubor);
  $konec = str_ireplace($hranate, $kod, $fopen);

  echo $konec;
  mysql_close();
}

function include_end_short() {
  global $theme, $meta_end;
  
  if (!isset($meta_end)) {$meta_end = "";};
 
  $hranate = array('[META]');
  $kod = array($meta_end);

  $soubor = "stuff/themes/".$theme."/end_short.php";            
  $fopen = file_get_contents($soubor);
  $konec = str_ireplace($hranate, $kod, $fopen);

  echo $konec;
  mysql_close();
}

function include_menu ($druh) {
global $lang, $skupina;

  $set_users = "icons_wb";
  $set_user_groups = "icons_wb";

switch ($druh) {
case "uzivatele" :  $set_users = "icons"; break;
case "skupiny" : $set_user_groups = "icons"; break;
};

  $query = mysql_query("SELECT * FROM jtcms_skupiny WHERE id='".$skupina."' LIMIT 1");
  $result = mysql_fetch_array($query); 

$menu2 = '<div class="second_menu">';
if ($result['uzivatele'] != 3) {$menu2 .= '<li><a href="set_users.php" title="'.$lang['set_users_title'].'"><img src="stuff/img/'.$set_users.'/users.png" alt="" />'.$lang['set_users_title'].'</a></li>';};
if ($result['skupiny'] != 3) {$menu2 .= '<li><a href="set_user_groups.php" title="'.$lang['set_user_groups_title'].'"><img src="stuff/img/'.$set_user_groups.'/user group.png" alt="" />'.$lang['set_user_groups_title'].'</a></li>';};
$menu2 .= '<div class="nf"></div>
</div><br /><br />';

return $menu2;
};

function include_menu2 ($druh) {
global $lang, $skupina;

  $set_general = "icons_wb";
  $set_templates = "icons_wb";
  $set_integration = "icons_wb";
  $set_banlist = "icons_wb";

switch ($druh) {
case "nastaveni" :  $set_general = "icons"; break;
case "sablony" : $set_templates = "icons"; break;
case "zacleneni" : $set_integration = "icons"; break;
case "banlist" : $set_banlist = "icons"; break;
};

  $query = mysql_query("SELECT * FROM jtcms_skupiny WHERE id='".$skupina."' LIMIT 1");
  $result = mysql_fetch_array($query); 

$menu2 = '<div class="second_menu">';
if ($result['nastaveni'] != 3) {$menu2 .= '<li><a href="set_general.php" title="'.$lang['set_general_title'].'"><img src="stuff/img/'.$set_general.'/gear.png" alt="" />'.$lang['set_general_title'].'</a></li>';};
if ($result['sablony'] != 3) {$menu2 .= '<li><a href="set_templates.php" title="'.$lang['set_templates_title'].'"><img src="stuff/img/'.$set_templates.'/bricks.png" alt="" />'.$lang['set_templates_title'].'</a></li>';};
if ($result['zacleneni'] != 3) {$menu2 .= '<li><a href="set_integration.php" title="'.$lang['set_integration_title'].'"><img src="stuff/img/'.$set_integration.'/wizard.png" alt="" />'.$lang['set_integration_title'].'</a></li>';};
if ($result['banlist'] != 3) {$menu2 .= '<li><a href="set_banlist.php" title="'.$lang['set_banlist_title'].'"><img src="stuff/img/'.$set_banlist.'/no.png" alt="" />'.$lang['set_banlist_title'].'</a></li>';};
$menu2 .= '<div class="nf"></div>
</div><br /><br />';

return $menu2;
};


/* WORKS WITCH POST */

function over_posts() {
global $_POST, $promenne;

  for ($p = 0; $p < count($promenne); ++$p){
    $obsah = $promenne[$p];
    if (!isset($_POST[$obsah])) {$_POST[$obsah] = "";};
  };
}

function set_user_groups_over_post($druh, $cislo) {
  global $_POST;

  if (!empty($_POST[$druh])) {if ($_POST[$druh] == $cislo) {echo ' checked';};};
}

function set_user_groups_over_post_result($druh, $cislo) {
  global $result, $_POST;

  if (empty($_POST[$druh])) {if ($result[$druh] == $cislo) {echo ' checked';};} else {if ($_POST[$druh] == $cislo) {echo ' checked';};};
}

function articles_categories_over_post_datepub($druh, $i, $StrFTime) {
  global $_POST, $dnesnidatum;
  
  if (!empty($_POST) && $_POST['datum_pub'] == "jindy") {if ($_POST[$druh] == $i) {echo ' selected';};} elseif (StrFTime("%$StrFTime", strtotime($dnesnidatum)) == $i) {echo ' selected';};
}

function articles_over_post_result_public($druh, $cislo, $druhform = false) {
  global $_POST, $result;
  
  if (!$druhform) {$druhform = $druh;};

  if (empty($_POST)) {if ($result[$druh] == $cislo) {echo ' checked';};} elseif ($_POST[$druhform] == $cislo) {echo ' checked';};
} 

function articles_categories_over_post_result_datepub($druh, $i, $StrFTime) {
  global $_POST, $dnesnidatum, $result;
  
  if (!empty($_POST) && $_POST['datum_pub'] == "jindy") {if ($_POST[$druh] == $i) {echo ' selected';};} elseif (StrFTime("%$StrFTime", strtotime($result['datum'])) == $i) {echo ' selected';};
}

/* GENERAL FUNCTIONS */
function show_content($obsah) {
  if (preg_match("/\[jtcms_gallery\=([1-9][0-9]*)\]/", $obsah)) {
    
    preg_match_all("/\[jtcms_gallery\=([1-9][0-9]*)\]/", $obsah, $vystup);
    
    $query = mysql_query("SELECT hodnota, hodnota2 FROM jtcms_nastaveni WHERE typ='galerie_nahled' LIMIT 1");
    $result = mysql_fetch_array($query);  
      $sirka = $result['hodnota'];
      $vyska = $result['hodnota2'];
    
    for ($c = 0; $c < count($vystup[1]); $c++) {
      $id = $vystup[1][$c];
      $adresar = opendir("./data/gal/".$id);
      $kod = "";
        while ($soubor = readdir($adresar)){
          if ($soubor != "." && $soubor != ".." && is_file("./data/gal/".$id."/".$soubor)) {       
            $kod .= '<a class="grouped_elements" rel="group'.$id.'" href="./data/gal/'.$id.'/'.$soubor.'"><img src="./data/gal/'.$id.'/'.$soubor.'" alt="'.$soubor.'" style="max-width: '.$sirka.'px; max-height: '.$vyska.'px;" class="gal_adm_gen"/></a>';
          };
        };
    
      $obsah = str_replace("[jtcms_gallery=".$id."]", $kod, $obsah);
    };
    
    echo $obsah;
    
  } else {
    echo $obsah;
  };
}

function show_comment($func_druh, $func_druh2) {
  global $druh, $result, $lang, $prava;     

          switch ($druh) {
          case $func_druh :
            $clanek = mysql_query("SELECT nazev, pridal FROM jtcms_clanky WHERE id='".$result['clanek']."' LIMIT 1");
            $clanek = mysql_fetch_array($clanek);
            $opravneni = mysql_fetch_array($opravneni);
            $opravneni = $opravneni['pridal'];
            
            if (($prava['clanky'] == 2 && $opravneni == $_SESSION['userid']) or $prava['clanky'] == 1) {
                $clanek_stranka = '<a href="articles.php?action=read&id='.$result['clanek'].'">'.$clanek['nazev'].'</a>';
            };
          break;
          case $func_druh2 :
            $stranka = mysql_query("SELECT nazev, pridal FROM jtcms_stranky WHERE id='".$result['stranka']."' LIMIT 1");
            $stranka = mysql_fetch_array($stranka);
            $opravneni = mysql_fetch_array($opravneni);
            $opravneni = $opravneni['pridal'];
            
            if (($prava['stranky'] == 2 && $opravneni == $_SESSION['userid']) or $prava['stranky'] == 1) {
                $clanek_stranka = '<a href="pages.php?action=read&id='.$result['stranka'].'">'.$stranka['nazev'].'</a>';
            };
          break;
          default :
            $clanek = mysql_query("SELECT nazev, pridal FROM jtcms_clanky WHERE id='".$result['clanek']."' LIMIT 1");
            $clanek = mysql_fetch_array($clanek);
            $opravneni = mysql_fetch_array($opravneni);
            $opravneni = $opravneni['pridal'];
            
            if (($prava['clanky'] == 2 && $opravneni == $_SESSION['userid']) or $prava['clanky'] == 1) {
                $clanek_stranka = '<a href="articles.php?action=read&id='.$result['clanek'].'">'.$clanek['nazev'].'</a>';
            };
          };
    echo '<div class="vypis_kom">';     
      echo '<div class="jmeno_datum">';
        echo '<div class="datum">';
           echo $clanek_stranka.'<br />';
           if (!empty($result['mail'])) {echo $result['mail']."&nbsp;&nbsp;&nbsp;&nbsp;";};
           echo StrFTime("%H:%M %d.%m.%Y", strtotime($result["datum"]));
        echo '</div>';
        if (!empty($result['web'])) {echo '<a href="'.$result["web"].'">'.$result["jmeno"].'</a>';} else {echo $result['jmeno'];};
      echo '</div>';
      echo '<div class="nf"></div>';
      echo '<div class="obsah">'.$result["obsah"].'</div>';
      echo '<div class="akce">';
      if (($prava == 2 && $opravneni == $_SESSION['userid']) or $prava == 1) {
          echo '<a href="comments.php?action=edit&amp;id='.$result['id'].'&amp;druh='.$druh.'" title="'.$lang['comments_edit_comment'].'"><img src="stuff/img/icons/edit16.png" alt="'.$lang['comments_edit_comment'].'" /></a>';
          echo '<img src="stuff/img/icons/trash16.png" alt="'.$lang['comments_delete_comment'].'" class="smaz" title="'.$lang['comments_delete_comment'].'" onclick="smaz_kom(\''.$result['id'].'\',\''.$result["jmeno"].'\',\''.$druh.'\');" />';
          if ($prava['banlist'] == 1) {echo '<img src="stuff/img/icons/ban_user16.png" alt="'.$lang['comments_ban_user'].'" class="smaz" title="'.$lang['comments_ban_user'].'" onclick="ban_user(\''.$result["ip"].'\',\''.$result["jmeno"].'\');" />';};
      };
      echo '</div>';
      echo '<div class="nf"></div>';
    echo '</div>';
  echo "\n";
}

function set_zero_is_empty($result) {
  if (empty($result)) {
    $result = 0;
  };
  
  return $result;
}

function get_correct_size($soubor) {
  global $lang;
  
  $velikost = filesize($soubor);
  $delka = strlen($velikost);
  
  switch ($delka) {
  case $delka <= 3 : $hodnota = $velikost.' '.$lang['files_byte']; break;
  case $delka <= 6 : $hodnota = round($velikost/1000, 2).' '.$lang['files_kilobyte']; break;
  case $delka <= 9 : $hodnota = round($velikost/1000000, 2).' '.$lang['files_megabyte']; break;
  default : $hodnota = round($velikost/1000000, 2).' '.$lang['files_megabyte'];
  };
  
  return $hodnota;
}

function my_acc_info_form($cislo) { 
  global $_POST, $result;
  
  $id = "info".$cislo;
  
  if (empty($_POST[$id])) {
    return $result[$id];
  } else {
    return $_POST[$id];
  };
}
?>
