Файл: wboard/source/system/classes/module.php
Строк: 213
<?php
/**
 * Wboard
 * Parent of modules
 * @author Screamer
 * @copyright 2013
 */
abstract class Module
{
    /**
     * @var (mysqli) MySQLi object
     */
    public $db;
    /**
     * @var (int) IP address
     */
    public $ip;
    /**
     * @var (int) IP address via PROXY
     */
    public $ip_via_proxy;
    /**
     * @var (string) HTTP-USER-AGENT
     */
    public $user_agent;
    /**
     * @var (string) HTTP-HOST
     */
    public $http_host;
    /**
     * @var (Language) Languages Handler
     */
    public $lng;
    /**
     * @var (Template) Templates handler
     */
    public $tpl;
    /**
     * @var (Model) Model
     */
     public $model;
    /**
     * @var (Helper) Helper
     */
    public $helper;
    /**
     * @var (int) is root?
     */
    public $is_root = FALSE;
    /**
     * @var (array) System settings
     */
    public $settings = array();
    /**
     * @var (boolean) enable/disable to display data
     */
    public $display = TRUE;
    /**
     * @var (string) Path to root directory of script
     */
    protected $path = '';
    /**
     * Construct
     * @param  (MySQLi) $db MySQLi object
     * @param  (array)  $network IP, IP via PROXY, User-Agent, Http-Referer, Http-Host
     * @param  (string) $rootpath Path to root directory of script
     * @return (void)
     */
    public function __construct(mysqli $db, array $network, $rootpath)
    {
        $this->path         =  $rootpath;                                                                           // Path to root directory of script
        $this->db           =& $db;                                                                                 // MySQLi object
        $this->ip           =  $network['ip'];                                                                      // IP address
        $this->ip_via_proxy =  $network['ip_via_proxy'];                                                            // IP address via PROXY
        $this->user_agent   =  $network['user_agent'];                                                              // HTTP User-Agent
        $this->http_host    =  $network['http_host'];                                                               // HTTP-HOST
        $this->lng          =  new Language($this->path . 'system' . DIRECTORY_SEPARATOR . 'lang.php');             // Languages Handler
        $this->tpl          =  new Template($this->path . 'system' . DIRECTORY_SEPARATOR . 'view', $this->lng);     // Templates handler
        $this->model        =  new Model($this->db, $this->lng, $this->path);                                       // Model
        // Loading settings
        $this->settings = array(
            'meta'                  => array(                                                                       // META-tags
                'keywords'          => '',                                                                          // Keywords
                'description'       => ''                                                                           // Description
            ),
            'title'                 => 'WBoard',                                                                    // Title of page by default
            'password'              => '',                                                                          // Password
            'user'                  => array(                                                                       // User settings by default
                'style'             => 'wboard',                                                                    // CSS Style
                'timeshift'         => 0,                                                                           // Timeshift
            ),
            'captcha'               => 1,                                                                           // Captcha: 1 - on; 0 - off;
        );
        $get_settings = $this->db->query("SELECT `key`, `val` FROM `settings`");
        while ($item = $get_settings->fetch_assoc()) {
            $this->settings[$item['key']] = $item['val'];
        }
        $get_settings->free();
        if (is_string($this->settings['meta']) && !empty($this->settings['meta'])) {
            $this->settings['meta'] = unserialize($this->settings['meta']);
        }
        if (is_string($this->settings['user']) && !empty($this->settings['user'])) {
            $this->settings['user'] = unserialize($this->settings['user']);
        }
        // Load user settings
        if (isset($_COOKIE['css_style'])
            && is_file($this->path . 'files' . DIRECTORY_SEPARATOR . 'styles' . DIRECTORY_SEPARATOR . $_COOKIE['css_style'] . '.css')
        ) {
            $this->settings['user']['style'] = $_COOKIE['css_style'];
        }
        if (isset($_COOKIE['timeshift']) && $_COOKIE['timeshift'] < 12 && $_COOKIE['timeshift'] > -12) {
            $this->settings['user']['timeshift'] = intval($_COOKIE['timeshift']);
        }
        // Authorization (for manage)
        $password = FALSE;
        if (isset($_SESSION['ups'])) {
            $password = $_SESSION['ups'];
        } elseif (isset($_COOKIE['cups'])) {
            $password = md5(trim($_COOKIE['cups']));
            $_SESSION['ups'] = $password;
        }
        if ($password !== FALSE) {
            if ($this->settings['password'] != $password) {
                setcookie("cups", "", 0,  "/");
                unset($_SESSION['ups']);
            } else {
                $this->is_root = TRUE;
                // Logins history
                $get_last_login = $this->db->query("SELECT * FROM `login_history` ORDER BY `time` DESC LIMIT 1");
                $last_login = $get_last_login->fetch_assoc();
                $get_last_login->free();
                if (!is_array($last_login)
                    || (is_array($last_login) && (
                            ($last_login['ip'] != $this->ip)
                            || ($last_login['ip_via_proxy'] != $this->ip_via_proxy)
                            || ($last_login['user_agent'] != $this->user_agent)
                        )
                    )
                ) {
                    $this->db->query(
                        "INSERT INTO `login_history` SET "
                        . "`ip` = '" . intval($this->ip) . "', "
                        . "`ip_via_proxy` = '" . intval($this->ip_via_proxy) . "', "
                        . "`user_agent` = '" . $this->db->real_escape_string($this->user_agent) . "', "
                        . "`time` = '" . time() . "'"
                    );
                }
            }
        }
        // Load helper
        $this->helper = new Helper($this->model, $this->lng, $this->tpl, $network, $this->settings, $this->is_root, $this->path);
        // Users in online
        $session = md5($this->ip . $this->ip_via_proxy . $this->user_agent);
        // Referer
        $referer = isset($_SERVER['HTTP_REFERER']) ? trim($_SERVER['HTTP_REFERER']) : '';
        $this->db->query(
            "INSERT INTO `online` SET `id` = '" . $this->db->real_escape_string($session) . "', "
            . "`user_agent` = '" . $this->db->real_escape_string($this->user_agent) . "', "
            . "`ip` = '" . intval($this->ip) . "', "
            . "`ip_via_proxy` = '" . intval($this->ip_via_proxy) ."', "
            . "`time` = '" . time() . "', "
            . "`referer` = '" . $this->db->real_escape_string($referer) . "' "
            . " ON DUPLICATE KEY UPDATE `time` = '" . time() . "'"
        );
        // Autoclean online table
        if ($this->db->result("SELECT COUNT(*) FROM `online` WHERE `time` < '" . (time() - 86400) . "'") > 0) {
            $this->db->query("DELETE FROM `online` WHERE `time` < '" . (time() - 86400) . "'");
            $this->db->query("OPTIMIZE TABLE `online`");
        }
        // Shutdown
        register_shutdown_function(array($this, 'shutdown'));
    }
    /**
     * Default action
     * @return (void)
     */
    public abstract function index();
    /**
     * Shutdown
     * Output data. Close connection with MySQL server
     * @return (void)
     */
    public function shutdown()
    {
        if ($this->display === TRUE) {
            if (!headers_sent()) {
                header('Content-type: application/xhtml+xml; charset=UTF-8');
            }
            // Define title
            if (empty($this->tpl->title)) {
                $this->tpl->title = $this->settings['title'];
            }
            // Top Navigation (for layot)
            $navigation = array(
                '' => $this->settings['title'],
                'w_action/settings' => $this->lng->settings,
            );
            // Links to static pages
            $pages_dir = $this->path . 'files' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR;
            $pages_conf = $pages_dir . 'pages.json';
            if (is_file($pages_conf)) {
                $pages_conf = json_decode(file_get_contents($pages_conf), TRUE);
                if (isset($pages_conf['show_link']) && isset($pages_conf['titles'])) {
                    foreach ($pages_conf['show_link'] as $page) {
                        $title = isset($pages_conf['titles'][$page]) ? $pages_conf['titles'][$page] : '';
                        if (is_file($pages_dir . $page . '.html') && !empty($title)) {
                            $navigation['w_action/pages/view/' . $page] = $title;
                        }
                    }
                }
            }
            if ($this->is_root) {
                $navigation['w_action/panel'] = $this->lng->control_panel;
            }
            echo $this->tpl->output(array(
                'meta' => $this->settings['meta'],                 // META-tags
                'navigation' => $navigation,                       // Navigation panel
                'css_style' => $this->settings['user']['style'],   // CSS style
                // Online counter
                'online' => $this->lng->online . ': '
                     . ($this->is_root
                         ? anchor('w_action/online', $this->model->online_counter())
                         : $this->model->online_counter()
                     ),
            ));
        }
        $this->db->close();
    }
    /**
     * Redirect to some page
     * @param (string) $uri URI
     * @return (void)
     */
    public function redirect($uri = '')
    {
        $this->display = FALSE;
        header('Location: ' . $this->http_host . $uri);
        exit;
    }
}