setup.class.php 8.68 KB
<?php
 
class setup
{
    var $db = null;
    var $tpl = null;
    var $error = array ();
    var $meta = array ();
    var $admin = null;
    var $rules = "rules";
    var $tree = null;
    var $cache = null;
    
    function setup ($admin = false)
    {
        $this->admin = $admin;
        
        switch ($this->admin)
        {
            case false :
                $template_dir = $_SERVER['DOCUMENT_ROOT'] . '/templates/';
                $compile_dir = $_SERVER['DOCUMENT_ROOT'] . '/templates_c/';
                break;
        
            case true :
                $template_dir = $_SERVER['DOCUMENT_ROOT'] . '/templates/admin/';
                $compile_dir = $_SERVER['DOCUMENT_ROOT'] . '/templates_c/admin/';
                break;
        }
        
        $connect = "mysql://" . DB_USER . ":" . DB_PSW . "@" . DB_HOST . "/" . DB_BASE;
        $this->db = DB::connect ($connect);
        if (PEAR::isError ($this->db))
            die ($this->db->getMessage ());
        
        $this->db->query ("set names cp1251");
        
        $this->tree = new CDBTree ($this->db, 'rules', 'id');
        
        $this->cache = new Cache ();
        $this->cache->path_cache = $_SERVER['DOCUMENT_ROOT'] . "/cache/sql/";
        
        $this->tpl = new Smarty ();
        $this->tpl->template_dir = $template_dir;
        $this->tpl->compile_dir = $compile_dir;
        $this->tpl->plugins_dir[] = $_SERVER['DOCUMENT_ROOT'] . '/plugins/';
        $this->assignMainVars();
    }
    
    function setupClass ($nameClass)
    {
        return new $nameClass ($this->db, $this->tpl, $this->error, $this->cache);
    }
    
    function isAdminUser ($user = array())
    {
        if ((isset ($user['login']) && $user['login'] == ADMIN_USER) && (isset ($user['psw']) && $user['psw'] == ADMIN_PSW))
        {
            $_SESSION['admin']['login'] = $user['login'];
            $_SESSION['admin']['psw'] = $user['psw'];
        }
        
        if ((isset ($_SESSION['admin']['login']) && $_SESSION['admin']['login'] == ADMIN_USER) && (isset ($_SESSION['admin']['psw']) && $_SESSION['admin']['psw'] == ADMIN_PSW))
            return true;
        
        return false;
    }
    
    function AdminUserExit ()
    {
        unset ($_SESSION['admin']);
    }
    
    function getRulesOne ($action, $lang = "ru")
    {
        $sql = "SELECT A.id,A.action,M.modul";
        if ($lang == 'ukr')
            $sql .= ",A.name_ukr as name,A.title_ukr as title,A.keywords_ukr as keywords,A.description_ukr as description,A.about_ukr as about";
        else
            $sql .= ",A.name,A.title,A.keywords,A.description,A.about";
        $sql .= " FROM rules A,modules M WHERE A.action=? AND M.id=A.modul_id limit 1";
        $row = $this->db->getRow ($sql, array (
            $action 
        ), DB_FETCHMODE_ASSOC);
        $this->meta = array (
            "title" => $row['title'],
            "keywords" => $row['keywords'],
            "description" => $row['description'],
            "about" => $row['about'] 
        );
        $this->tpl->assign ('rules_info_one', $row);
        $this->tpl->assign ('lang', $lang);
        $lang_prefix = ($lang == "ru") ? null : $lang . '/';
        $this->tpl->assign ('lang_prefix', $lang_prefix);
        return $row;
    }
    
    /*
     * function includesElements(){
     * $dir = ($this->admin==true) ? "./includes/admin/" : "./includes/";
     * $includes_dir = opendir($dir);
     * while ( ($inc_file = readdir($includes_dir)) != false )
     * if (strstr($inc_file,".php"))
     * {
     * include($dir . $inc_file);
     * }
     * }
     */
    function trim (&$data)
    {
        foreach ($data as $key => $value)
        {
            if (is_array ($data[$key]))
                $this->trim ($data[$key]);
            else
                $data[$key] = trim ($value);
        }
    }
    
    function valid ($data)
    {
        if (isset ($data['url_action']))
        {
            if (! preg_match ("/^.{1,20}$/i", $data['url_action']))
                $this->error[] = "Îøèáêà ââîäà ïîëÿ (URL - Íàçâàíèå).";
            elseif ($data['url_action'] == $this->rules)
                $this->error[] = "Îøèáêà. Èñïîëüçîâàíèÿ äàííîãî íàçâàíèÿ çàïðåùåíî!";
        }
        if (isset ($data['name']))
        {
            if (! preg_match ("/^.{1,50}$/i", $data['name']))
                $this->error[] = "Îøèáêà ââîäà ïîëÿ (Íàçâàíèå).";
        }
        return (count ($this->error)) ? true : false;
    }
    
    function rulesSaveAction ($data)
    {
        $id = (isset ($data['action_id']) && $data['action_id'] > 0) ? $data['action_id'] : 1;
        $fields_values = array (
            "parent_id" => $id,
            "action" => $data['url_action'],
            "name" => $data['name'],
            "name_ukr" => $data['name_ukr'],
            "title" => $data['title'],
            "title_ukr" => $data['title_ukr'],
            "about" => $data['about'],
            "about_ukr" => $data['about_ukr'],
            "description" => $data['description'],
            "description_ukr" => $data['description_ukr'],
            "keywords" => $data['keywords'],
            "keywords_ukr" => $data['keywords_ukr'],
            "modul_id" => $data['modul_id'] 
        );
        
        if (isset ($data['update_id']) && $data['update_id'] > 0)
        {
            $this->db->autoExecute ("rules", $fields_values, DB_AUTOQUERY_UPDATE, "id={$data['update_id']}");
        }
        else
        {
            $this->tree->insert ($id, $fields_values);
        }
    }
    
    function viewRules ()
    {
        $sql = "select * from rules where _level>0 ORDER BY _left";
        $row = $this->db->getAll ($sql, array (), DB_FETCHMODE_ASSOC);
        $this->tpl->assign ('rules', $row);
    }
    
    function viewRulesBegin ()
    {
        $sql = "select * from rules where parent_id=1 ORDER BY _left";
        $row = $this->db->getAll ($sql, array (), DB_FETCHMODE_ASSOC);
        $this->tpl->assign ('rules_begin', $row);
    }
    
    function viewRulesParent ($id = 2)
    {
        $sql = "select * from rules where parent_id=? ORDER BY _left";
        $row = $this->db->getAll ($sql, array (
            $id 
        ), DB_FETCHMODE_ASSOC);
        $this->tpl->assign ('rules_parent', $row);
    }
    
    function viewOneRules ($id)
    {
        $sql = "select * from rules where id=? limit 1";
        $row = $this->db->getRow ($sql, array (
            $id 
        ), DB_FETCHMODE_ASSOC);
        $this->tpl->assign ('rules_one', $row);
    }
    
    function linkLang ()
    {
        $REQUEST_URI = substr (str_replace (array (
            "/ukr",
            "/ru",
            "/eng",
            "lang=ukr",
            "lang=ru",
            "lang=eng" 
        ), null, $_SERVER['REQUEST_URI']), 1);
        /*
         * $link_rus = ( preg_match("/(ukr|rus|eng)/i", $_SERVER['REQUEST_URI']) ) ? substr(preg_replace("/(ukr|rus|eng)/i",'',$_SERVER['REQUEST_URI']),1) : 'uvg/';
         * $link_ukr = ( preg_match("/(ukr|rus|eng)/i", $_SERVER['REQUEST_URI']) ) ? substr(preg_replace("/(ukr|rus|eng)/i",'ukr',$_SERVER['REQUEST_URI']),1) : 'uvg/ukr' . $URL;
         * $link_eng = ( preg_match("/(ukr|rus|eng)/i", $_SERVER['REQUEST_URI']) ) ? substr(preg_replace("/(ukr|rus|eng)/i",'eng',$_SERVER['REQUEST_URI']),1) : 'uvg/eng' . $URL;
         */
        $REQUEST_URI = eregi_replace ("^ucse\/", null, $REQUEST_URI);
        $link_ukr = "ukr/" . $REQUEST_URI;
        $link_rus = $REQUEST_URI;
        
        $this->tpl->assign ('link_rus', $link_rus);
        $this->tpl->assign ('link_ukr', $link_ukr);
        
        /*
         * $this->tpl->assign('link_rus', 'http://'.$_SERVER['HTTP_HOST'] .'/'. $link_rus );
         * $this->tpl->assign('link_ukr', 'http://'.$_SERVER['HTTP_HOST'] .'/'. $link_ukr );
         * $this->tpl->assign('link_eng', 'http://'.$_SERVER['HTTP_HOST'] .'/'. $link_eng );
         */
    }
    
    function rulesDelete ($id)
    {
        $this->tree->delete ($id);
    }
    
    /**
     * Çàãðóæàåì ÷òî íàäî äëÿ ñàéòà
     */
    function assignMainVars ()
    {  
        // ÑÍÅÃ
        $objExtra = $this->setupClass ('Extra'); 
        $SNOW = $objExtra->getStatusIdByName('SNOW') == 1 ? $objExtra->displaySnow() : '';

        $this->tpl->assign ('SNOW', $SNOW);
    }
    
    function meta ()
    {
        $this->tpl->assign ('meta', $this->meta);
    }
    
    function error ()
    {
        $this->tpl->assign ('error', $this->error);
    }
    
    function displayRules ()
    {
        $this->tpl->assign ('action_modules', $this->db->getAll ("select * from modules", array (), DB_FETCHMODE_ASSOC));
        $this->tpl->assign ('tpl', "rules.tpl");
    }
    
    function displayAdmin ()
    {
        $this->tpl->display ("admin.tpl");
    }
    
    function display ()
    {
        $this->tpl->display ("index.tpl");
    }
}