<?php

final class Main {

    public $sql;
    public $file;
    var $cleaner = '<div style="clear: both;"></div>';
    var $form_action;
    var $include;
    var $var_paging = 15;
    var $margin = 0;
    var $sekcia = 0;
    var $category;

    public function __construct() {
        global $sql;
        global $file;
        $this->sql = &$sql;
        $this->file = &$file;
        $this->form_action = str_replace('&', '&amp;', $_SERVER['REQUEST_URI']);
        $this->category = true;
    }

public function report_content($id_user, $main_section, $root_section, $version, $dovod) {
    $sql_report_content = "INSERT INTO reported_content (id_user, main_section, root_post, version, dovod, date) VALUES ($id_user, $main_section, $root_section, $version, '$dovod', '" . date("Y-m-d G:i:s") . "')";
    if ( mysql_query($sql_report_content) ) {
        echo 'Príspevok bol úspešne nahlasený administrátorovi... O ďalšom priebehu budete informovaný na e-mail.';
    } else {
        echo $this->return_error_text('Príspevok NEbol úspešne nahlasený administrátorovi... Opakujete voľbu neskôr alebo kontaktuje administrátora.');
    }
}

public function vrat_koren($id) {
    $koren = $id;
    while ( $koren != 0 ) {
        $row = mysql_fetch_assoc(mysql_query('SELECT id_category,id_parent FROM categories WHERE id_category=' . $koren));
        $koren = $row['id_parent'];
    }
    $row = mysql_fetch_assoc(mysql_query('SELECT id_category FROM categories WHERE id_category=' . $row['id_category']));
    return $row['id_category'];
}

public function vrat_nazov_korena($id) {
    $row = mysql_fetch_assoc(mysql_query('SELECT category FROM categories WHERE id_category=' . $id));
    return $row['category'];
}

public function top_10($sql, $sekcia) {
    $result = mysql_query($sql);
    echo '<div style="float: left; width: 30%; margin-bottom: 25px;">';
    echo '<table border="0" class="top10">';
    echo    '<thead>';
    echo        '<tr style="background-color: #00488F; color: #EEEEEE; font-weight: bold;">';
    echo            '<td colspan="3">10 najnovších príspevkov v sekcii ' . $sekcia . '</td>';
    echo        '</tr>';
    echo    '</thead>';
    $i = 1;
    while ( $row = mysql_fetch_assoc($result) ) {
        if ( ($i%2)==0 ) { $bg = ' style="background-color: #EEEEEE;"'; } else { $bg = ''; }
        echo    '<tr' . $bg . '>';
        $nadpis = $this->replace_history_name($row['remake_of']);
        echo        '<td><a href="?id=' . $row['id_branch'] . '&amp;article=' . $row['id'] . '" title="' . $nadpis . '">' . substr($nadpis, 0, 50) . '</a></td>';
        echo        '<td>' . $this->get_user_name($row['add_by']) . '</td>';
        echo        '<td>' . $this->format_date($row['date']) . '</td>';
        echo    '</tr>';
        $i++;
    }
    echo '</table>';
    echo '</div>';
}

public function top_5_rate() {
    echo '<div style="float: left; width: 30%; margin-bottom: 25px;">';
    echo '<table border="0" class="top10">';
    echo    '<thead>';
    echo        '<tr style="background-color: #00488F; color: #EEEEEE; font-weight: bold;">';
    echo            '<td colspan="3">5 najlepšie hodnotených príspevkov</td>';
    echo        '</tr>';
    echo    '</thead>';
    $i = 1;
    $result = mysql_query('SELECT * FROM articles ORDER BY (rate/rated) DESC LIMIT 0,5');
    while ( $row = mysql_fetch_assoc($result)   ) {
        if ( ($i%2)==0 ) { $bg = ' style="background-color: #EEEEEE;"'; } else { $bg = ''; }
        echo    '<tr' . $bg . '>';
        $nadpis = $this->replace_history_name($row['remake_of']);
        echo        '<td><a href="?id=' . $row['id_branch'] . '&amp;article=' . $row['remake_of'] . '&amp;history=' . $row['id'] . '" title="' . $nadpis . '">' . substr($nadpis, 0, 50) . '</a></td>';
        echo        '<td>' . $this->get_user_name($row['add_by']) . '</td>';
        echo        '<td>' . $this->format_date($row['date']) . '</td>';
        echo    '</tr>';
        $i++;
    }
    echo '</table>';
    echo '</div>';
}

private function replace_history_name($id) {
    return mysql_result(mysql_query('SELECT nadpis FROM articles WHERE remake_of=' . $id . ' ORDER BY utime DESC LIMIT 1;'), 0, 'nadpis');
}

public function format_date($date) {
    $date = trim($date);
    if (strlen($date) == 19) {
        $result = substr($date, 8, 2).'.'.substr($date, 5, 2).'.'.substr($date, 0, 4);
        return $result;
    }

    return false;

}

public function format_time($date) {
    $date = trim($date);
    if (strlen($date) == 19) {
        $result = substr($date, 11, 8);
        return $result;
    }

    return false;

}

public function group_right($my_id, $group) { //moja skupina uzivatelska a uzivatelska skupina majuca pravo na danu akciu
    $my_group = mysql_result(mysql_query('SELECT group_rights FROM login.users WHERE id_user=' . $my_id), 0, 'group_rights');
    if ( $my_group >= $group ) {
        return true;
    } else {
        return false;
    }
}

public function splnam_prava($id) {
    $section_rights = mysql_result(mysql_query('SELECT group_rights FROM categories WHERE id_category=' . $id), 0, 'group_rights');
    if ( $this->is_logged($_SESSION['id_user']) ) {
        //$this->sql->sql_connect('login');
        $my_rights = mysql_result(mysql_query('SELECT group_rights FROM login.users WHERE id_user=' . $_SESSION['id_user']), 0, 'group_rights');
        //$this->sql->sql_close('login');
        //$this->sql->sql_close('e_learning');
        if ( $my_rights >= $section_rights ) {
            return true;
        } else {
            return false;
        }
    }
}

public function removeDia($string) {
    $a = array(
    "ö" => "o", "ű" => "u", "ő" => "o", "ü" => "u", "ł" => "l", "ż" => "z", "ń" => "n", "ć" => "c", "ę" => "e", "ś" => "s", "ŕ" => "r", "á" => "a",
    "ä" => "a", "ĺ" => "l", "č" => "c", "é" => "e", "ě" => "e", "í" => "i", "ď" => "d", "�?" => "n", "ó" => "o", "ô" => "o", "ř" => "r", "ů" => "u",
    "ú" => "u", "š" => "s", "ť" => "t", "ž" => "z", "ľ" => "l", "ý" => "y", "Ŕ" => "R", "�?" => "A", "Ä" => "A", "Ĺ" => "L", "Č" => "C", "É" => "E",
    "Ě" => "E", "Í" => "I", "Ď" => "D", "Ň" => "N", "Ó" => "O", "Ô" => "O", "�?" => "R", "Ů" => "U", "Ú" => "U", "Š" => "S", "Ť" => "T","Ž" => "Z",
    "Ľ" => "L", "Ý" => "Y", "Ä" => "A"
    );
    return strtr($string, $a);

    return false;

}

public function prilohy($id) {
    $this->sql->sql_connect('e_learning');
    $sql_prilohy = 'SELECT path FROM articles_attach WHERE id_article=' . $id;
    if ( $result_prilohy = mysql_query($sql_prilohy) ) {
        if ( mysql_num_rows($result_prilohy) > 0 ) {
            while ( $row_prilohy = mysql_fetch_assoc($result_prilohy) ) {
                $vrat .= '
                    <div class="prilohy_riadok">
                        <div class="prilohy_icon">' . $this->return_icon(substr($row_prilohy['path'], strrpos($row_prilohy['path'], '.')+1)) . '</div>
                        <div class="prilohy_link"><a href="' . str_replace(' ', '%20', $row_prilohy['path']) . '" title="stiahnite si súbor: ' . substr($row_prilohy['path'], strrpos($row_prilohy['path'], '/')+1) . '">' . substr($row_prilohy['path'], strrpos($row_prilohy['path'], '/')+1) . '</a></div>
                    </div>' . $this->cleaner;
            }
        } else { $vrat = '<div>prispevok nemá prílohy</div>'; }
    }
    return $vrat;
}

private function return_icon($koncovka) {
    if ( !in_array($koncovka, array("7z", "doc", "gif", "gz", "jpeg", "jpg", "mp3", "odp", "ods", "odt", "pdf", "ppt", "png", "rar", "txt", "wav", "xls", "zip")) ) {
        return '<img src="attach/icons/ine.jpg" alt="no icon" title="' . $koncovka . ' ikona" />';
    } else {
        return '<img src="attach/icons/' . $koncovka . '.jpg" alt="file icon" title="' . $koncovka . ' ikona" />';
    }
}

public function get_subject_id($alt, $id) {
    $this->sql->sql_connect('e_learning');
    $row = mysql_fetch_assoc(mysql_query('SELECT id_category FROM categories WHERE category_alt=\'' . $alt . '\' AND id_parent=' . $id));
    return $row['id_category'];
}

public function je_hlavna_sekcia($id) {
    $this->sql->sql_connect('e_learning');

    $sql_je_hlavna_sekcia = 'SELECT id_parent FROM categories WHERE id_category=' . $id;
    if ( $result_je_hlavna_sekcia = mysql_query($sql_je_hlavna_sekcia) ) {
        if ( $row_je_hlavna_sekcia = mysql_fetch_assoc($result_je_hlavna_sekcia) ) {
            if ( $row_je_hlavna_sekcia['id_parent'] == 0 ) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    } else {
        return false;
    }
}

public function select_boxes() {
    echo '
    <script type="text/javascript">
        function loadSubject() {
        ';
                $this->sql->sql_connect('e_learning');

                $sql_subject = 'SELECT * FROM categories WHERE id_parent=' . $_GET['id'] . ' AND cat_type=1;';
                if ( $result_subject = mysql_query($sql_subject) ) {
                    while ( $row_subject = mysql_fetch_assoc($result_subject) ) {
                        $subjects = '';
                        $sql_sub_subject = 'SELECT * FROM categories WHERE id_parent=' . $row_subject['id_category'] . ';';
                        if ( $result_sub_subject = mysql_query($sql_sub_subject) ) {
                            while ( $row_sub_subject = mysql_fetch_assoc($result_sub_subject) ) {
                                $subjects .= '"' . $row_sub_subject['category_alt'] . '",';
                            }
                            $subjects = substr($subjects, 0, -1);
                        }
                        if ( $subjects == '' ) { $subjects = '""'; } //inak by sa mi nevymazalo pri prechode z roznych option
                        echo 'var p' . $row_subject['id_category'] . '=new Array(' . $subjects . ');';
                    }
                }
    echo '
            var selectedBoxValue=document.main_form.category.value;
            var i;
            var j;

            var subjectLength=eval(selectedBoxValue).length;

            removeSelectboxOption();

            for(i=0;i<document.main_form.category.options.length;i++) {
                if(selectedBoxValue==document.main_form.category.options[i].value) {
                    for(j=0;j<subjectLength;j++) {
                        document.main_form.subject.options[j]=new Option(eval(selectedBoxValue)[j],eval(selectedBoxValue)[j])
                    }
                }
            }
        }

        function removeSelectboxOption() {
            var i;
            for(i=0;i<document.main_form.subject.options.length;i++) {
                document.main_form.subject.remove(i);
            }
        }
    </script>
    ';
}

public function back_button($first, $other) { //ocakava prvy prvok v url adrese a pole ostatnych aby neboli podmienky zbytocne --cize z url sa zrusi vsetko co neposlem v $other
    $url = '?' . $first . '=' . $_GET[$first];
    foreach ( $other as $hodnota ) {
        $url .= '&amp;' . $hodnota . '=' . $_GET[$hodnota];
    }
    return '<div class="back_button">
        <img src="images/back_button.png" />' .
        '<a href="' . $url . '" title="">o úroveň vyššie</a>
        <div style="clear: both;"></div>
    </div>';
}

public function show_hide() {
    $show_hide = '<div class="show_hide">
        <img id="v_toggle_img" src="images/show_button.png" />' .
        '<a id="v_toggle" href="#" title="Zobraz/Skry">viac</a>
        <div style="clear: both;"></div>
    </div>';
    return $show_hide;
}

public function rating_form($rated_id) {
    $this->sql->sql_connect('e_learning');
    if ( isset($_GET['history']) ) {
        $sql_get_rate = 'SELECT rate,rated FROM articles WHERE id=' . $_GET['history'];
    } else {
        $sql_get_rate = 'SELECT rate,rated FROM articles WHERE remake_of=' . $_GET['article'] . ' ORDER BY utime DESC';
    }
    if ( $result_get_rate = mysql_query($sql_get_rate) ) {
        $row_get_rate = mysql_fetch_assoc($result_get_rate);
        if ( $row_get_rate['rated'] != 0 ) {
            $perc = 20*($row_get_rate['rate']/$row_get_rate['rated']);
            $rated = $row_get_rate['rated'];
        } else {
            $perc = 0;
            $rated = 0;
        }
        $hodnotil = $this->was_rated();
        //print_r($hodnotil);
        $list = '<div style="text-align: center;">
            <ul class="star-rating" style="width: 105px;">
                <li class="current-rating" style="width: ' . $perc . '%;">a</li>

                <li>' . $this->rating_star_link(1, "one-star", $rated_id, $hodnotil) . '</li>
                <li>' . $this->rating_star_link(2, "two-stars", $rated_id, $hodnotil) . '</li>
                <li>' . $this->rating_star_link(3, "three-stars", $rated_id, $hodnotil) . '</li>
                <li>' . $this->rating_star_link(4, "four-stars", $rated_id, $hodnotil) . '</li>
                <li>' . $this->rating_star_link(5, "five-stars", $rated_id, $hodnotil) . '</li>
            </ul><div style="float: left; color: #008E14; font-size: 11px; margin-top: 5px;">hodnotené: ' . $rated . 'x</div>';

        if ( in_array($rated_id, $hodnotil) ) {
            $list .= '
                <img style="float: left;" onmouseout="document.getElementById(\'hodnotil\').style.display=\'none\'" onmouseover="document.getElementById(\'hodnotil\').style.display=\'block\'" src="images/wrong_button.png" title="Tento príspevok ste už hodnotili a váš daľší hlas nebude akceptovaný." alt="Nieje moýné znovu hlasoať" />
                <div style="clear: both;"></div>
                <div style="padding: 5px; border: 1px solid #CCCCCC; background-color: #DDDDDD; display: none; position: relative; top: -40px; left: -60px; z-index: 2; font-size: 9px;" id="hodnotil">Príspevok ste už hodnotili.</div>';
        }
        $list .= '
            <div style="clear: both;"></div></div>';
        return $list; //
    } else {

        $this->return_error_text('Príspevok s daným ID neexistuje.');
        return false;
    }
}

private function rating_star_link($number, $class, $rated_id, $hodnotil) {
    if ( in_array($rated_id, $hodnotil) ) {
        return '<span>&nbsp;</span>';
    } else {
        return '<a href="' . str_replace('&', '&amp;', $_SERVER['REQUEST_URI']) . '&amp;rate=' . $number . '&amp;rated_id=' . $rated_id . '" title="' . $number . '/5" class="' . $class . '">' . $number . '</a>';
    }
}

private function was_rated() {
    $this->sql->sql_connect('login');
    $sql_disable_rating = 'SELECT rated_id FROM users WHERE id_user=' . $_SESSION['id_user'];
    $result_disable_rating = mysql_query($sql_disable_rating);
    $row_disable_rating = mysql_fetch_assoc($result_disable_rating);
    $hodnotil = split(',', $row_disable_rating['rated_id']); //pole $hodnotil obsahuje vsetky indexi clankov ktore uzivatel hodnotil
    //print_r($hodnotil);
    return $hodnotil;
}

public function rating_exe($id, $rate) {
    if ( $this->is_logged($_SESSION['id_user']) ) { //ked nieje nalogovany nech to nejde
        //musim skontrolovat ci uz hlasoval za dany clanok
        $this->sql->sql_connect('login');
        $sql_disable_rating = 'SELECT rated_id FROM users WHERE id_user=' . $_SESSION['id_user'];
        $result_disable_rating = mysql_query($sql_disable_rating);
        $row_disable_rating = mysql_fetch_assoc($result_disable_rating);
        $hodnotil = split(',', $row_disable_rating['rated_id']); //pole $hodnotil obsahuje vsetky indexi clankov ktore uzivatel hodnotil

        if ( !in_array($id, $hodnotil) ) {
            $sql_disable_rating = "UPDATE users SET rated_id='" . $row_disable_rating['rated_id']  . "," . $id . "' WHERE id_user=" . $_SESSION['id_user'] . ";";
            $result_disable_rating = mysql_query($sql_disable_rating);
            $this->sql->sql_close();
            $this->sql->sql_connect('e_learning');
            $sql_add_rate = "UPDATE articles SET rated=rated+1, rate=rate+" . $_GET['rate'] . " WHERE id=" . $id . ";";
            $result_add_rate = mysql_query($sql_add_rate);
        } else {
            echo $this->return_error_text('Nemôžete hodnotiť 2x ten istý príspevok');
        }
        $this->sql->sql_close();
        $this->sql->sql_connect('e_learning');
    } else {
        echo $this->return_error_text('Hodnotiť môžete len keď ste prihlásený');
        $this->sql->sql_connect('e_learning');
    }
}

public function return_error_text($text) {
    return '<h4 style="text-align: center; color: red; font-size: 12px;">' . $text . '</h4>';
}

/*** HEADER: START ***/

public function return_login_box() {
    if ( !$this->is_logged($_SESSION['id_user']) ) {
        $logxx = '
                    <table cellpadding="3" cellspacing="0" border="0">
                        <tr>
                            <td>nick:</td>
                            <td><input type="text" name="log_name" value="" /></td>
                            <td>heslo:</td>
                            <td><input type="password" name="log_pass" value="" /> <input type="submit" name="login" value="log in" /></td>
                        </tr>
                        <tr>
                            <td colspan="4"> or [<a href="?id=3">registrácia</a>], [<a href="?id=19">užívateľská príručka</a>]</td>
                        </tr>
                    </table>';
    } else {
        $logxx = '<div><input type="submit" name="logout" value="log out" style="background-color: #FFFFFF;" /></div>';
    }
    ?>
    <form action="<?=str_replace('&','&amp;',$_SERVER['REQUEST_URI'])?>" method="post">
        <?=$logxx?>
    </form>
    <?
}

public function return_head_menu() {
    $this->sql->sql_connect('e_learning');
    $sql = 'SELECT * FROM categories WHERE id_parent=0 AND show_web = 1 ORDER BY rank ASC';
    if ( $result = mysql_query($sql) ) {
        echo '<div style="margin-left: 20px;">';
        echo	'<img src="images/divider.png" style="float: left;" alt="" />';
        while ( $row = mysql_fetch_array($result) ) {
            if ( empty($_GET['id']) ) { //ked je na indexe
                $class = '';
            } elseif ( $_GET['id'] == $row['id_category'] ) { //pokial je zakliknuta jedna z hlavnych kategorii
                $class = ' class="active" ';
            } elseif ( !mysql_query('SELECT * FROM categories WHERE id_category=' . $_GET['id']) ) {
                //echo 'nieje id v zozname';
            } else { //pokial je zakliknuta podkategoria hlavnej kategorie
                $sql_parent = 'SELECT id_parent FROM categories WHERE id_category=' . $_GET['id'];
                if ( $result_parent = mysql_query($sql_parent) ) {
                    if ( mysql_num_rows($result_parent) > 0 ) {
                        $parent = mysql_result($result_parent, 0, 'id_parent');
                    } else {
                        $this->category = false;
                    }
                }
                if ( $parent ) {
                    if ( $parent == $row['id_category'] ) {
                        $class = ' class="active" ';
                    } else {
                        $class = '';
                    }
                } else {
                    $class = '';
                }
            }
            if ( $row['id_category'] != 18 ) {
                echo	'<div style="padding: 18px 0px 18px 0px; text-align: center; float: left;">';
                echo		'<a href="index.php?id=' . $row['id_category'] . '" ' . $class . '>' . $row["category"] . '</a>';
                echo	'</div> ';
                echo	'<img src="images/divider.png" style="float: left;" alt="" />';
            } else {
                if ( $this->is_logged($_SESSION['id_user']) && $this->group_right($_SESSION['id_user'], 2) ) {
                    echo	'<div style="padding: 18px 0px 18px 0px; text-align: center; float: left;">';
                    echo		'<a href="index.php?id=' . $row['id_category'] . '" ' . $class . '>' . $row['category'] . '</a>';
                    echo	'</div> ';
                    echo	'<img src="images/divider.png" style="float: left;" alt="" />';
                }
            }
        }
        echo '</div>';
    }
    echo $this->cleaner;
    $this->sql->sql_free_result($result);
    $this->sql->sql_close();
}

public function return_head_submenu() {
    $this->sql->sql_connect('e_learning');
    if ( isset($_GET['id']) ) {
        $sql = 'SELECT id_parent FROM categories WHERE id_category=' . $_GET['id'];
        if ( $result = mysql_query($sql) ) {
            $id_parent = mysql_result($result, 0, 'id_parent');
        }

        if ( $id_parent > 0 ) {
            $this->include = mysql_result(mysql_query('SELECT include FROM categories WHERE id_category=' . $id_parent), 0, 'include');
            $colcatpar = 'id_category';
        } elseif ( $id_parent == 0 ) {
            $sql = 'SELECT include FROM categories WHERE id_category=' . $_GET['id'];
            if ( $result = mysql_query($sql) ) {
                $this->include = mysql_result($result, 0, 'include');
            }
            $colcatpar = 'id_parent';
        } else {
            //$this->include = 'pages/home.php';
        }
    } else {
        //pokial je homepage
    }

    if ( $id_parent == 0 ) {
        $id = $_GET['id'];
    } else {
        $id = $id_parent;
    }


    $sql = 'SELECT * FROM categories WHERE id_parent=' . $id . ' AND show_web=1 ORDER BY rank ASC';
    $result = mysql_query($sql);
    //echo '<span style="float: left; color: white;">|</span>';
    echo	'<div style="float: left; margin-top: 3px;"><span style="color: white;">|</span>';
    while ( $row = @mysql_fetch_array($result) ) {
        if ( $_GET['id'] == $row['id_category'] ) {
            $class = ' class="sub_active" ';
        } else {
            $class = ' class="sub" ';
        }
        echo		'<a href="?id=' . $row['id_category'] . '"' . $class . ' title="' . $row['category_alt'] . '">' . $row['category'] . '</a><span style="color: white;">|</span>';
    }
    echo	'</div>';
    $this->sql->sql_free_result($result);
    $this->sql->sql_close();
}

/*** HEADER: END ***/

/*** HEADING TEXT: START ***/

public function return_heading_text($text) {
    echo '<h3>' . $text . '</h3>';
}

/*** HEADING TEXT: END ***/

/*** POZNAMKY: START ***/

public function edit_article_exe($nadpis, $text, $id_category, $id_branch, $remake_of, $id_prerabaneho) {
    $this->sql->sql_connect('e_learning');
    $next_id = $this->get_next_autoinc_value('articles');
    
    $sql_insert_article = "
        INSERT INTO articles (id_root, id_category, id_branch, add_by, remake_of, nadpis, popis, date, show_web, utime)
        VALUES (" . $main->vrat_koren(substr($_POST['category'],1)) . ", " . $id_category . ", " . $id_branch . ", " . $_SESSION['id_user'] . ", " . $remake_of . " , '" . $nadpis . "', '" . $text . "', '" . date("Y-m-d G:i:s") . "', 1, " . time() . ")";
    $sql_attach = 'SELECT * FROM articles_attach WHERE id_article=' . $id_prerabaneho;
    if ( $result_attach = mysql_query($sql_attach) ) {
        if ( mysql_num_rows($result_attach) ) {
            while ( $row_attach = mysql_fetch_assoc($result_attach) ) {
                mysql_query('INSERT INTO articles_attach (id_article, path) VALUES (' . $next_id . ', \'' . $row_attach['path'] . '\')');
            }
        }
    }
    mysql_query($sql_insert_article);
    $i = 0;
    while ( $_FILES['defaults_' . $i]['name'] != '' ) {
        if	( isset($_FILES['defaults_' . $i]['name']) ) {
            if ( ($_FILES['defaults_' . $i]['error'] == UPLOAD_ERR_OK) ) {
                    if (is_uploaded_file($_FILES['defaults_' . $i]['tmp_name'])) {
                        $nazov = $_FILES['defaults_' . $i]['name'];

                        if ( !file_exists('attach/' . $next_id) ) {
                            mkdir('attach/' . $next_id);
                        }

                        $location = 'attach/' . $next_id . '/' . $_FILES['defaults_' . $i]['name'];
                        $location = $this->removeDia($location);
                        if ( move_uploaded_file($_FILES['defaults_' . $i]['tmp_name'], $location) ) {
                            echo 'INSERT INTO articles_attach (id_article, path) VALUES (' . $next_id . ',\'' . $location . '\')';
                            mysql_query('INSERT INTO articles_attach (id_article, path) VALUES (' . $next_id . ',\'' . $location . '\')');
                        } else {

                        }
                    }
            } else {
                echo '<p style="color: #FF0000;">zle</p>';
            }
        }
        $i++;
    }
}

public function edit_article($article_id) {

    $this->file->foto_upload_exe();
        
    $sql_edit_article = 'SELECT * FROM articles WHERE remake_of=' . $_GET['article'] . ' ORDER BY utime DESC';
    if ( $result_edit_article = mysql_query($sql_edit_article) ) {
        if ( mysql_num_rows($result_edit_article) > 0 ) {
            $row_edit_article = mysql_fetch_assoc($result_edit_article);
            echo '
                <script src="./text_editor/js/richtext.js" type="text/javascript" language="javascript"></script>
                <script src="./text_editor/js/config.js" type="text/javascript" language="javascript"></script>
                <form id="main_form" name="main_form" enctype="multipart/form-data" action="' . $this->form_action . '" method="post">
                    <div style="float: left;">
                        <div style="margin-bottom: 15px; width: 100px; float: left; padding-top: 2px;">Nadpis: </div><div style="float: left;"><input type="text" name="nadpis" value="' . $row_edit_article['nadpis'] . '" /></div>
                        ' . $this->cleaner . '
                        <script type="text/javascript">
                            initRTE(\'' . $row_edit_article['popis'] . '\', \'example.css\');
                        </script><br />
                        prílohy: <input type="file" name="defaults"><br clear="all" /><br />
                        <input type="submit" name="edit_article_exe" value="Uloz clanok" />
                        <input type="hidden" name="edit_article" value="edit_article" />
                    </div>
                    <div style="float: right; width: 35%; margin-right: 3%; padding: 5px;">
                        <input type="file" accept="image/gif,image/jpeg,image/png,image/jpg" name="pic" value="" />
                        <input type="submit" style="font-size: 11px" value="Pridaj obrazok" name="add_pic" />

                        <h4>Adresy vami uploadnutych obrazkov: <span style="font-size: 10px;">(budete ju potrebovat pri doplnani obrazkov do clankov)</span></h4>
                ';
                if ( ($_SESSION['uploadnute'] != '') && (sizeof($_SESSION['uploadnute']) > 0) )
                    {
                    echo '<div style="width: 320px;">';
                    echo substr($_SESSION['uploadnute'],5);
                    echo '</div>';
                    }
                echo '</div></form>' . $this->cleaner;
        }
    }
}

public function return_subjects($branch) {
    $this->sql->sql_connect('e_learning');
    $sql_sub = 'SELECT DISTINCT category FROM articles AS a, categories AS c WHERE a.id_branch=' . $branch . ' AND a.id_category=c.id_category';
    if ( $result_sub = mysql_query($sql_sub) ) {
        while ( $row_sub = mysql_fetch_assoc($result_sub) ) {
            $sql_get_id = 'SELECT id_category, category_alt FROM categories WHERE category=\'' . $row_sub['category'] . '\' AND id_parent=' . $_GET['id'];
            if ( $result_get_id = mysql_query($sql_get_id) ) {
                $row_get_id = mysql_fetch_assoc($result_get_id);
            }
            echo '[<a href="?id=' . $_GET['id'] . '&amp;sub=' . $row_get_id['id_category'] . '" title="' . $row_get_id['category_alt'] . '">' . $row_sub['category'] . '</a>] ';
        }

    }

    $this->sql->sql_close();
}

public function return_article_list($what) {
    $this->sql->sql_connect('e_learning');
    echo 	'<h4>Zoznam uploadnutých ' . $what . ':</h4>';
    echo	'<ul>';
    $sql_parent = 'SELECT id_parent FROM categories WHERE id_category=' . $_GET['id'];
    if ( $result_parent = mysql_query($sql_parent) ) {
        $row_parent = mysql_fetch_assoc($result_parent);
    }

    if ( ($row_parent['id_parent'] == 0) || ($row_parent['id_parent'] == '') ) {
        if ( isset($_GET['sub']) ) {
            $sql_all = 'SELECT * FROM articles AS a, categories AS c WHERE (a.id_branch=c.id_category) AND (c.id_parent=' . $_GET['id'] . ') AND (c.id_category=' . $_GET['sub'] . ') ORDER BY utime DESC';
        } else {
            $sql_all = 'SELECT * FROM articles AS a, categories AS c WHERE (a.id_branch=c.id_category) AND (c.id_parent=' . $_GET['id'] . ') AND (a.id = a.remake_of) ORDER BY utime DESC';
        }

        if ( isset($_GET['page']) && ($_GET['page']!='') ) {
            $sql_prednasky = 'SELECT * FROM articles AS a, categories AS c WHERE (a.id_branch=c.id_category) AND (c.id_parent=' . $_GET['id'] . ')  AND (a.id = a.remake_of) ORDER BY utime DESC LIMIT ' . ($this->var_paging*$_GET['page']) . ", " . $this->var_paging;
        } else {
            $sql_prednasky = 'SELECT * FROM articles AS a, categories AS c WHERE (a.id_branch=c.id_category) AND (c.id_parent=' . $_GET['id'] . ')  AND (a.id = a.remake_of) ORDER BY utime DESC LIMIT 0,' . $this->var_paging;
        }
    } else {
        if ( isset($_GET['sub']) ) {
            $sql_all = 'SELECT * FROM articles WHERE id_branch=' . $_GET['id'] . ' AND id_category=' . $_GET['sub'] . ' AND (id=remake_of) ORDER BY utime DESC';
        } else {
            $sql_all = 'SELECT * FROM articles WHERE id_branch=' . $_GET['id'] . ' AND (id=remake_of) ORDER BY utime DESC';
        }
        if ( isset($_GET['page']) && ($_GET['page']!='') ) {
            if ( isset($_GET['sub']) ) {
                $page = '&amp;sub=' . $_GET['sub'];
                $sql_prednasky = 'SELECT * FROM articles WHERE id_branch=' . $_GET['id'] . ' AND id_category=' . $_GET['sub'] . '  AND (id=remake_of) ORDER BY utime DESC LIMIT ' . ($this->var_paging*$_GET['page']) . ', ' . $this->var_paging;
            } else {
                $page = '';
                $sql_prednasky = 'SELECT * FROM articles WHERE id_branch=' . $_GET['id'] . ' AND (id=remake_of) ORDER BY utime DESC LIMIT ' . ($this->var_paging*$_GET['page']) . ', ' . $this->var_paging;
            }
            //$sql_prednasky = "SELECT * FROM articles WHERE id_branch=" . $_GET['id'] . " LIMIT " . (5*$_GET['page']) . ", 5";
        } else {
            if ( isset($_GET['sub']) ) {
                $page = '&amp;sub=' . $_GET['sub'];
                $sql_prednasky = 'SELECT * FROM articles WHERE id_branch=' . $_GET['id'] . '  AND id_category=' . $_GET['sub'] . ' AND (id=remake_of) ORDER BY utime DESC LIMIT 0,' . $this->var_paging;
            } else {
                $page = '';
                $sql_prednasky = 'SELECT * FROM articles WHERE id_branch=' . $_GET['id'] . ' AND (id=remake_of) ORDER BY utime DESC LIMIT 0,' . $this->var_paging;
            }
        }
    }

    if ( $result_prednasky = mysql_query($sql_prednasky) ) {
        while ( $row_prednasky = mysql_fetch_assoc($result_prednasky) ) {
            if ( strlen($row_prednasky['nadpis']) > 50 ) {
                $etc = '...';
            } else {
                $etc = '';
            }
            echo '<li>';
            $nazov = mysql_fetch_assoc(mysql_query('SELECT nadpis FROM articles WHERE remake_of=' . $row_prednasky['id'] . ' ORDER BY utime DESC LIMIT 0,1'));
            echo 	'<div><a href="?id=' . $_GET['id'] . '&amp;article=' . $row_prednasky['id'] . '" title="' . $nazov['nadpis'] . '">' . substr($nazov['nadpis'], 0, 50) . $etc . '</a></div>';
            $sql_sekcia = 'SELECT category FROM categories WHERE id_category=' . $row_prednasky['id_category'];
            if ( $result_sekcia = mysql_query($sql_sekcia) ) {
                $row_sekcia = mysql_fetch_assoc($result_sekcia);
                echo '<div>[<a href="?id=' . $row_prednasky['id_category'] . '" title="' . $row_sekcia['category'] . '">' . $row_sekcia['category'] . '</a>]</div>';
            }
            echo '</li>';
        }
    }
    echo	'</ul>';

    $this->paging($sql_all, $page);

    $this->sql->sql_close();
}

public function paging($sql_all, $page) {
    $kolko = mysql_num_rows($result_all = mysql_query($sql_all));
    $pom = 0;
    echo '<div style="clear: both; text-align: center; padding-top: 10px; font-size: 11px;">| ';
    for ( $i = 1; $i <= $kolko; $i = $i+$this->var_paging ) {
        $pom++;
        if ($_GET['page']==($pom-1)){
            echo $pom . ' | ';
        }
        else {
            echo '<a href="?id=' . $_GET['id'] . $page . '&amp;page=' . ($pom-1) . '">' . $pom . '</a> | ';
        }
    }
    echo '</div>';
}

/*** POZNAMKY: END ***/

/*** FORUM: START ***/

public function remove_tema($table, $id) {
    $sql_remofe_forum = 'UPDATE ' . $table . ' SET show_web=0 WHERE id=' . $id;
    if ( $result = mysql_query($sql_remofe_forum) ) {
        echo 'Téma bola úspešne odstránená.';
        echo '<script type="text/javascript">setTimeout("document.location.href=\'?id=' . $_GET['id'] . '\';",1500);</script>';
    } else {
        $this->return_error_text('Téma NEbola úspešne odstránená.');
    }
}

//mysql_result(mysql_query('SELECT nadpis FROM articles WHERE remake_of=' . $id . ' ORDER BY utime DESC LIMIT 1;'), 0, 'nadpis');

public function remove_post($id) {
    $sql_remove_forum = 'UPDATE forum SET show_web=0 WHERE id=' . $id;
    if ( $result = mysql_query($sql_remove_forum) ) {
        if ( $result_2 = mysql_query('SELECT id FROM forum WHERE id_parent=' . $id) ) {
            while ( $row = mysql_fetch_assoc($result_2) ) {
                $this->remove_post($row['id']);
            }
            //echo 'Príspevok bol úspešne odstránený.';
            echo '<script type="text/javascript">setTimeout("document.location.href=\'?id=' . $_GET['id'] . '&tema=' . $_GET['tema'] . '\';",1500);</script>';
        } else {
            $this->return_error_text('Nastal problém pri mazaní dskusie na daný príspevok.');
        }
    } else {
        $this->return_error_text('Príspevok NEbol úspešne odstránený.');
    }
}

public function forum_temy() {
    $sort_arr = array('user_name', 'nazov', 'date', 'prispevkov');
    $order_arr = array('ASC', 'DESC');
    if ( isset($_GET['sort']) && isset($_GET['order']) && in_array($_GET['sort'], $sort_arr) && in_array($_GET['order'], $order_arr) ) {
        $pom = ' ORDER BY ' . $_GET['sort'] . ' ' . $_GET['order'];
        $page = '&order=' . $_GET['order'] . '&sort=' . $_GET['sort'];
    } else { $pom = ' ORDER BY date DESC'; }
    if ( !isset($_GET['page']) ) {
        $sql_temy = 'SELECT * FROM forum_temy WHERE show_web=1' . $pom . ' LIMIT 0,' . $this->var_paging . ';';
    } else {
        $sql_temy = 'SELECT * FROM forum_temy WHERE show_web=1' . $pom . ' LIMIT ' . ($_GET['page']*$this->var_paging) . ',' . $this->var_paging . ';';
    }

    $pocet = mysql_num_rows(mysql_query(substr($sql_temy, 0, strpos($sql_temy, ' LIMIT'))));

    if ( $result_temy = mysql_query($sql_temy) ) {
        echo '<table border="0" class="forum_temy">';
            echo '<tr>';
            if ( isset($_GET['order']) && ($_GET['order'] == 'ASC') ) { $order = 'DESC'; } else { $order = 'ASC'; }
            echo '<td class="first"><a href="?id=' . $_GET['id'] . '&amp;sort=user_name&amp;order=' . $order . '">zalozil</a></td>';
            echo '<td class="first"><a href="?id=' . $_GET['id'] . '&amp;sort=nazov&amp;order=' . $order . '">nazov temy</a></td>';
            echo '<td class="first"><a href="?id=' . $_GET['id'] . '&amp;sort=date&amp;order=' . $order . '">datum zalozenia</a></td>';
            echo '<td class="first"><a href="?id=' . $_GET['id'] . '&amp;sort=prispevkov&amp;order=' . $order . '">pocet prispevkov</a></td>';
            if ( $this->is_logged($_SESSION['id_user']) && $this->group_right($_SESSION['id_user'], 2) ) {
                echo '<td class="first">Del</td>';
            }
            echo '</tr>';
        $i = 1;
        while ( $row_temy = mysql_fetch_assoc($result_temy) ) {
            if ( ($i % 2) == 0 ) { $bg = ' style="background-color: #EEEEEE;"'; } else { $bg = ''; }
            echo '<tr' . $bg . '>';
            echo '<td>' . $row_temy['user_name'] . '</td>';
            echo '<td><a href="?id=' . $_GET['id'] . '&amp;tema=' . $row_temy['id'] . '" title="">' . $row_temy['nazov'] . '</a></td>';
            echo '<td>' . $this->format_date($row_temy['date']) . '</td>';
            echo '<td>' . $this->pocet_postov($row_temy['id']) . '</td>';
            if ( $this->is_logged($_SESSION['id_user']) && $this->group_right($_SESSION['id_user'], 2) ) {
                //echo '<td><a href="?id=' . $_GET['id'] . '&amp;del=forum_temy&amp;id_tema=' . $row_temy['id'] . '" title="zrusit diskusiu \'' . $row_temy['nazov'] . '\'"><img src="images/delete.png" alt="del" /></a></td>';
                echo '
                    <td><a onclick="document.getElementById(\'conf' . $i . '\').style.display=\'block\';" href="#" title="zrusit diskusiu \'' . $row_temy['nazov'] . '\'">
                        <img src="images/delete.png" alt="del" /></a>
                        <div style="display: none;" id="conf' . $i . '"><a href="?id=' . $_GET['id'] . '&amp;del=forum_temy&amp;id_tema=' . $row_temy['id'] . '" title="áno chcem">y</a> / <a onclick="document.getElementById(\'conf' . $i . '\').style.display=\'none\';" href="#">n</a></div>
                    </td>';
            }
            echo '</tr>';
            $i++;
        }
            echo '<tr>';
                echo '<td style="text-align: center;" colspan="4">';
                $this->paging(substr($sql_temy, 0, strpos($sql_temy, ' LIMIT')), $page);
                echo '</td>';
            echo '</tr>';
        echo '</table>';
    }
}

public function pocet_postov($id) {
    return  mysql_result(mysql_query('SELECT COUNT(id) AS pocet FROM forum WHERE id_tema=' . $id . ' AND show_web=1'),0,'pocet');
}

public function forum_najnovsie() {
    echo '<div style="float: left; width: 30%;">';
    echo '<table border="0" class="top10">';
    echo    '<thead>';
    echo        '<tr style="background-color: #00488F; color: #EEEEEE; font-weight: bold;">';
    echo            '<td colspan="3">5 najnovších tém v diskusii</td>';
    echo        '</tr>';
    echo    '</thead>';
    $i = 1;
    $result = mysql_query('SELECT * FROM forum_temy ORDER BY `date` DESC LIMIT 0,5;');
    while ( $row = mysql_fetch_assoc($result) ) {
        if ( ($i%2)==0 ) { $bg = ' style="background-color: #EEEEEE;"'; } else { $bg = ''; }
        echo    '<tr' . $bg . '>';
        echo        '<td><a href="?id=12&amp;tema=' . $row['id'] . '" title="' . $row['nazov'] . '">' . $row['nazov'] . '</a></td>';
        echo        '<td>' . $row['user_name'] . '</td>';
        echo        '<td>' . $this->format_date($row['date']) . '</td>';
        echo    '</tr>';
        $i++;
    }
    echo '</table>';
    echo '</div>';
}

public function forum_add_temu($nazov, $user_id) {
    mysql_query('INSERT INTO forum_temy (nazov, id_user, user_name, date, prispevkov) VALUES (\'' . $nazov . '\', ' . $user_id . ', \'' . $this->get_user_name($user_id) . '\', \'' . date("Y-m-d G:i:s") . '\', 0);');
    echo '<script type="text/javascript">';
    echo     'document.location.href="' . str_replace('&amp;', '&', $this->form_action) . '";';
    echo '</script>';
}

public function main_forum($tema) {
    $sql_main = 'SELECT * FROM forum WHERE id_parent=0 AND id_tema=' . $tema . ' AND show_web=1 ORDER BY id ASC';
    if ( $result_main = mysql_query($sql_main) ) {
        $i = 0;
        while ( $row_main = mysql_fetch_assoc($result_main) ) {
            $this->margin = 0;
            echo '<div style="margin-bottom: 15px; padding: 7px; border: 1px solid red; border: 1px solid #BBBBBB; background-color: #EEEEEE;">';
            echo 	$row_main['text'];
            if ( $this->is_logged($_SESSION['id_user']) ) {
                echo 	'<div style="font-size: 11px; float: left;"><a href="?id=' . $_GET['id'] . '&amp;tema=' . $_GET['tema'] . '&amp;react=' . $row_main['id'] . '" title="">Reagovať</a></div>';
            }
            echo 	'<div style="font-size: 11px; float: right;"><span style="font-weight: bold;">Kedy: </span>' . $row_main['date'] . '</div>';
            echo 	'<div style="font-size: 11px; float: right;margin-right: 15px;"><span style="font-weight: bold;">Pridal: </span>' . $row_main['user_name'] . '</div>';
            echo 	'<div style="clear: both;"></div>';
            if ( $this->is_logged($_SESSION['id_user']) && $this->group_right($_SESSION['id_user'], 2) ) {
                echo '<a href="?id=' . $_GET['id'] . '&amp;del=forum&amp;id_post=' . $row_main['id'] . '" title="zrusit diskusiu \'' . $row_temy['nazov'] . '\'"><img src="images/delete.png" alt="del" /></a><span style="color: #00377E; font-size: 9px;">* pozor príspevok bude po kliknutí odstránený bez konfirmácie</span>';
            }
            if ( isset($_GET['react']) && ($_GET['react'] != '') && ($row_main['id'] == $_GET['react']) && $this->is_logged($_SESSION['id_user']) ) {
                echo '<div style="border-top: 1px solid #00377E; margin-top: 10px; padding-top: 10px;">';
                echo 	'<form method="post" action="' . str_replace('&', '&amp;', substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '&react')) ) . '">';
                echo		'<input type="hidden" name="react_id" value="' . $_GET['react'] . '" />';
                echo 		'<div><textarea name="reakcia" rows="3" cols="10" style="width: 100%;"></textarea></div>';
                echo 		'<div><input style="font-size: 11px;" value="Reaguj" type="submit" /></div>';
                echo 	'</form>';
                echo '</div>';
            }
            echo '</div>';
            $this->sub_forum($row_main['id'],'conf' . $i);
            $i++;
        }
    }
}

public function sub_forum($id_parent) {
    $sql_sub = 'SELECT * FROM forum WHERE id_parent=' . $id_parent . ' AND show_web=1 ORDER BY id ASC';
    if ( $result_sub = mysql_query($sql_sub) ) {
        while ( $row_sub = mysql_fetch_assoc($result_sub) ) {
            $this->margin++;
            echo '<div style="margin-bottom: 15px; padding: 7px; border: 1px solid #BBBBBB; background-color: #EEEEEE; margin-left: ' . ($this->margin*20) . 'px;">';
            echo 	$row_sub['text'];
            if ( $this->is_logged($_SESSION['id_user']) ) {
                echo 	'<div style="font-size: 11px; float: left;"><a href="?id=' . $_GET['id'] . '&amp;tema=' . $_GET['tema'] . '&amp;react=' . $row_sub['id'] . '" title="">Reagovať</a></div>';
            }
            echo 	'<div style="font-size: 11px; float: right;"><span style="font-weight: bold;">Pridané: </span>' . $row_sub['date'] . '</div>';
            echo 	'<div style="font-size: 11px; float: right;margin-right: 15px;"><span style="font-weight: bold;">Pridal: </span>' . $row_sub['user_name'] . '</div>';
            echo 	'<div style="clear: both;"></div>';
            if ( $this->is_logged($_SESSION['id_user']) && $this->group_right($_SESSION['id_user'], 2) ) {
                echo '<a href="?id=' . $_GET['id'] . '&amp;del=forum&amp;id_post=' . $row_sub['id'] . '" title="zrusit diskusiu \'' . $row_temy['nazov'] . '\'"><img src="images/delete.png" alt="del" /></a><span style="color: #00377E; font-size: 9px;">* pozor príspevok bude po kliknutí odstránený bez konfirmácie</span>';
            }
            if ( isset($_GET['react']) && ($_GET['react'] != '') && ($row_sub['id'] == $_GET['react']) && $this->is_logged($_SESSION['id_user']) ) {
                echo '<div style="border-top: 1px solid #00377E; margin-top: 10px; padding-top: 10px;">';
                echo 	'<form method="post" action="' . str_replace('&', '&amp;', substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '&react')) ) . '">';
                echo		'<input type="hidden" name="react_id" value="' . $_GET['react'] . '" />';
                echo 		'<div><textarea name="reakcia" rows="3" cols="10" style="width: 100%;"></textarea></div>';
                echo 		'<div><input style="font-size: 11px;" value="Reaguj" type="submit" /></div>';
                echo 	'</form>';
                echo '</div>';
            }
            echo '</div>';
            $this->sub_forum($row_sub['id']);
            $this->margin--;
        }
    }
}

public function forum_react($id_user) {
    $this->sql->sql_connect('e_learning');
    if ( isset($_POST['reakcia']) && ($_POST['reakcia']!='') && $this->is_logged($_SESSION['id_user']) ) {
        $sql_react = 'INSERT INTO forum (id_parent, id_tema, add_by, user_name, text, date, utime)
            VALUES (' . $_POST['react_id'] . ', ' . $_GET['tema'] . ', ' . $id_user . ', \'' . $this->get_user_name($id_user) . '\', \'' . nl2br($_POST['reakcia']) . '\', \'' . date("Y-m-d G:i:s") . '\', ' . time() . ')';
        if ( $result_react = mysql_query($sql_react) ) {
            //mysql_query('UPDATE forum_temy SET prispevkov=prispevkov+1 WHERE id=' . $_GET['tema']);
            echo '<script type="text/javascript">';
            echo     'document.location.href="' . str_replace('&amp;', '&', $this->form_action) . '";';
            echo '</script>';
        }
    }
}

public function forum_add_new($id_user, $tema) {
    echo '<div style="border-top: 1px solid #00377E; margin-top: 10px; margin-bottom: 30px; padding-top: 10px;">';
    echo 	'<form method="post" action="' . str_replace('&', '&amp;', substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '&addnew')) ) . '">';
    echo 		'<div><textarea name="prispevok" rows="3" cols="10" style="width: 100%;"></textarea></div>';
    echo 		'<div><input style="font-size: 11px;" value="Pridaj" type="submit" /></div>';
    echo 	'</form>';
    echo '</div>';
}

public function forum_add_new_execute($id_user, $tema) {
    $this->sql->sql_connect('e_learning');
    if ( isset($_POST['prispevok']) && ($_POST['prispevok']!='') && $this->is_logged($_SESSION['id_user']) ) {
        $sql_post = 'INSERT INTO forum (id_parent, id_tema, add_by, user_name, text, date, utime)
            VALUES (0, ' . $_GET['tema'] . ', ' . $id_user . ', \'' . $this->get_user_name($id_user) . '\' , \'' . $_POST['prispevok'] . '\', \'' . date("Y-m-d G:i:s") . '\', ' . time() . ')';
        if ( $result_post = mysql_query($sql_post) ) {
            //mysql_query('UPDATE forum_temy SET prispevkov=prispevkov+1 WHERE id=' . $_GET['tema']);
            echo '<script type="text/javascript">';
            echo     'document.location.href="' . str_replace('&amp;', '&', $this->form_action) . '";';
            echo '</script>';
        }
    }
    $this->sql->sql_close();
}

/*** FORUM: END ***/

/*** LOGIN: START ***/

public function get_user_name($id_user) {
        $this->sql->sql_connect('login');
        $sql_get_name = 'SELECT nick FROM users WHERE id_user=' . $id_user;
        $result_get_name = mysql_query($sql_get_name);
        $row_get_name = mysql_fetch_assoc($result_get_name);
        $nick = $row_get_name['nick'];
        $this->sql->sql_close();
        $this->sql->sql_connect('e_learning');
        return $nick;
    }

public function get_section_name($id) {
    return mysql_result(mysql_query('SELECT category FROM categories WHERE id_category=' . $id . ' LIMIT 1;'), 0, 'category');
}

public function login($name, $pass) {
    if ( isset($_POST['login']) && isset($_POST['log_name']) && (trim($_POST['log_name'])!='') && isset($_POST['log_pass']) && (trim($_POST['log_pass'])!='') ) {
        $this->sql->sql_connect('login');
        $sql_login = 'SELECT id_user FROM users WHERE nick=\'' . $_POST['log_name'] . '\' AND pass=\'' . md5($_POST['log_pass']) . '\';';
        $result_login = mysql_fetch_assoc(mysql_query($sql_login));
        if ( $result_login ) {
            $_SESSION['id_user'] = $result_login['id_user'];
            $sql_logged = "INSERT INTO logged_users (id_user, timestamp) VALUES ('" . $result_login['id_user'] . "', '" . time() . "')";
            $result_logged = mysql_query($sql_logged);
            ?>
            <script type="text/javascript">
                document.location.href="<?php echo $_SERVER['REQUEST_URI']; ?>";
            </script>
            <?
        } else {
            $_SESSION = array();
            session_destroy();
            //echo 'No such user';
        }
    }
    $this->sql->sql_free_result($result);
    $this->sql->sql_close();
}

public function is_logged($id) {
    //$this->sql->sql_connect('login');
    $sql_log = 'SELECT timestamp FROM login.logged_users WHERE id_user=' . $id;
    if ( $result_log = mysql_query($sql_log) ) {
        if ( mysql_num_rows($result_log) > 0 ) {
            $row_log = mysql_fetch_assoc($result_log);
            $cas = time();
            if ( ($cas-$row_log['timestamp']) < 900 ) {
                return true;
            } else {
                $this->logout($id);
                return false;
            }
        } else {
        $this->logout($id);
        return false;
        }
    }
    //$this->sql->sql_connect('e_learning');
}

public function logout($id) {
    $this->sql->sql_connect('login');
    $sql_logged = "DELETE FROM logged_users WHERE id_user=" . $id;
    $result_logged = mysql_query($sql_logged);
    $_SESSION['id_user'] = array();
    $this->sql->sql_free_result($result);
    $this->sql->sql_close();
}

public function refresh_login($id) {
    $this->sql->sql_connect('login');
    $sql_logged = 'UPDATE logged_users SET timestamp=' . time() . ' WHERE id_user=' . $id;
    $result_logged = mysql_query($sql_logged);
    $this->sql->sql_connect('e_learning');
}

public function get_next_autoinc_value($db_name) {
    $row = mysql_fetch_array(mysql_query("SHOW TABLE STATUS LIKE '" . $db_name . "'"));
    return $row[9];
}

/*** LOGIN: END ***/
}
?>