Файл: wboard/source/system/controller/pages.php
Строк: 159
<?php
/**
 * Wboard
 * Static pages handler
 * @author Screamer
 * @copyright 2013
 */
class Module_Pages extends Module
{
    /**
     * List of all pages
     * @return (void)
     */
    public function index()
    {
        if (!$this->is_root) {
            $this->redirect('w_action/err');
        }
        $config = $this->_load_config();
        if (isset($_POST['ok'])) {
            $show_link = isset($_POST['show_link']) && is_array($_POST['show_link']) ? $_POST['show_link'] : array();
            $show_main = isset($_POST['show_main']) && is_array($_POST['show_main']) ? $_POST['show_main'] : array();
            foreach ($show_link as $key => $item) {
                if (!is_file($this->path . 'files' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . $item . '.html')) {
                    unset($show_link[$key]);
                }
            }
            foreach ($show_main as $key => $item) {
                if (!is_file($this->path . 'files' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . $item . '.html')) {
                    unset($show_main[$key]);
                }
            }
            $config['show_link'] = $show_link;
            $config['show_main'] = $show_main;
            file_put_contents($this->path . 'files' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'pages.json', json_encode($config));
            $this->redirect('w_action/pages');
        }
        $list = scandir($this->path . 'files' . DIRECTORY_SEPARATOR . 'pages');
        $out = '';
        foreach ($list as $item) {
            if ($item != '.' && $item != '..' && $item != 'pages.json') {
                $item = str_replace('.html', '', $item);
                $out .= $this->tpl->load('pages_item', array(
                    'page' => $item,
                    'title' => isset($config['titles'][$item]) ? htmlspecialchars($config['titles'][$item]) : $this->lng->untitled,
                    'show_link' => in_array($item, $config['show_link']),
                    'show_main' => in_array($item, $config['show_main']),
                ));
            }
        }
        $this->tpl->title = $this->lng->static_pages;
        $this->tpl->set_output($this->tpl->load('pages', array('data' => $out)));
    }
    /**
     * Display page
     * @return (void)
     */
    public function view($name = '')
    {
        $file = $this->path . 'files' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . $name . '.html';
        if (!is_file($file)) {
            $this->redirect('w_action/err');
        }
        $this->tpl->title = $this->_load_config($name);
        $this->tpl->set_output($this->tpl->load('_container', array(
            'top_menu' => $this->is_root
                ? array(
                    anchor('w_action/pages/form/' . $name, $this->lng->edit),
                    anchor('w_action/pages/remove/' . $name, $this->lng->delete),
                    anchor('w_action/pages', $this->lng->back),
                ) : array(),
            'data'     => file_get_contents($file),
        )));
    }
    /**
     * Add/Edit page
     * @return (void)
     */
    public function form($name = '')
    {
        if (!$this->is_root) {
            $this->redirect('w_action/err');
        }
        $data = '';
        $error = array();
        if (!empty($name)) {
            // Load page for edit
            $file = $this->path . 'files' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . $name . '.html';
            $data = is_file($file) ? file_get_contents($file) : '';
        }
        if (!empty($_POST)) {
            if (isset($_POST['ok'])) {
                $_POST['data'] = isset($_POST['data']) ? trim($_POST['data']) : $this->lng->about_empty;
                $_POST['title'] = isset($_POST['title']) ? str_replace("rn", " ", trim($_POST['title'])) : '';
                if (empty($name)) {
                    // Check filename
                    if (isset($_POST['name']) && !empty($_POST['name']) && !preg_match('/[^da-z]+/iu', $_POST['name'])) {
                        $name = $_POST['name'];
                    } else {
                        $error['name'] = $this->lng->wrong_chars;
                    }
                }
                if (empty($_POST['data'])) {
                    $error['data'] = sprintf($this->lng->wrong_len_less, 1);
                }
                if (empty($error)) {
                    if (!empty($_POST['title'])) {
                        // Save title
                        $config = $this->_load_config();
                        $config['titles'][$name] = htmlspecialchars($_POST['title']);
                        file_put_contents(
                            $this->path . 'files' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'pages.json',
                            json_encode($config)
                        );
                    }
                    // Save page
                    file_put_contents($this->path . 'files' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . $name . '.html', $_POST['data']);
                }
            }
            if (empty($error)) {
                $this->redirect('w_action/pages');
            }
        }
        $title = empty($name) ? '' : $this->_load_config($name);
        $this->tpl->title = empty($name) ? $this->lng->create_page : $this->lng->edit_page;
        $this->tpl->set_output($this->tpl->load('page_form', array(
            'title' => $title,
            'name' => htmlspecialchars($name), 'data' => htmlspecialchars($data),
            'error' => isset($error) ? $error : '',
        )));
    }
    /**
     * Remove page
     * @return (void)
     */
    public function remove($name = '')
    {
        if (!$this->is_root) {
            $this->redirect('w_action/err');
        }
        $file = $this->path . 'files' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . $name . '.html';
        if (is_file($file)) {
            unlink($file);
        }
        $this->redirect('w_action/pages');
    }
    /**
     * Load title(s)
     * @param (string) $name Name of page for get title only (if empty return all data)
     * @return (string|array) Title|All data
     */
    protected function _load_config($name = '')
    {
        $file = $this->path . 'files' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'pages.json';
        $config = is_file($file) ? file_get_contents($file) : array();
        $config = !empty($config) ? json_decode($config, TRUE) : array();
        return !empty($name) ? (isset($config['titles'][$name]) ? $config['titles'][$name] : $this->lng->untitled) : $config;
    }
}