code:
<?php
use Captcha as Captcha;
/*

 *  Class for generating static pages around the MitsubaBBS Project

 *  Copyright (C) 2014  MitsubaBBS Project

 *

 *  This program is free software: you can redistribute it and/or modify

 *  it under the terms of the GNU Affero General Public License as

 *  published by the Free Software Foundation, either version 3 of the

 *  License, or (at your option) any later version.

 *

 *  This program is distributed in the hope that it will be useful,

 *  but WITHOUT ANY WARRANTY; without even the implied warranty of

 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

 *  GNU Affero General Public License for more details.

 *

 *  You should have received a copy of the GNU Affero General Public License

 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.

 *

*/

namespace Mitsuba;

/**
 * Caching
 * Insert description here
 *
 * @category
 * @package
 * @author
 * @copyright
 * @license
 * @version
 * @link
 * @see
 * @since
 */
class Caching
{

    private $conn;

    private $config;

    private $mitsuba;

    /**
     * __construct
     * Insert description here
     *
     * @param $connection
     * @param $mitsuba
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function __construct($connection, &$mitsuba)
    {

        $this->conn = $connection;

        $this->mitsuba = $mitsuba;

        $this->config = $this->mitsuba->config;

    }

    /**
     * generateBoardLinks
     * Insert description here
     *
     * @param $location
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function generateBoardLinks($location = "board")
    {

        $settings = ['o_pinned' => 'Pin board menu', 'o_preview' => 'Enable post preview', 'o_loader' => 'Enable infinite scrolling', 'o_watched' => 'Enable watched threads', 'o_updater' => 'Enable updater', 'o_imgexpand' => 'Enable image expander (RES)', 'o_fastreply' => 'Enable fast reply', 'o_alt_mobile' => 'Mobile menu on the right', 'o_night_css' => 'Nighty Time Mode'];

        $out = '

' . $this->generateLinks($location) . '

Settings
';

        $num = 0;

        foreach ($settings as $name => $desc) {

            $out.= '<input type="checkbox" name="' . $name . '" id="opt_' . $num . '"/>' . $desc . '';

            $num++;

        }

        $out.= '

Custom board links

				<input type="text" value="" name="o_custom_links">

					<input type="button" value="Reset" id="settingsReset"/>

					<input type="button" value="Save" id="settingsSave"/>

';

        return $out;

    }

    /**
     * rebuildBoardLinks
     * Insert description here
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function rebuildBoardLinks()
    {

        $this->mitsuba->updateConfigValue("boardLinks", $this->generateBoardLinks("index"));

        $this->mitsuba->updateConfigValue("boardLinks_thread", $this->generateBoardLinks("thread"));

        $this->mitsuba->updateConfigValue("boardLinks_board", $this->generateBoardLinks("board"));

    }

    /**
     * generateLinks
     * Insert description here
     *
     * @param $location
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function generateLinks($location = 'board')
    {
        $categories = [];
        $data = [];
        $out = '';
        $result = $this->conn->query("SELECT * FROM links");

        while ($row = $result->fetch_assoc()) {
            $id = $row['id'];
            if ($row['parent'] < 0) {
                $categories[$id] = $row['title'];
                $data[$categories[$id]] = [];
            } else {
                array_push($data[$categories[$row['parent']]], $row);
            }
        }

        foreach ($data as $category => $links) {
            $out .= '
';
            $out .= '[';
            $i = 0;

            foreach ($links as $boardLink) {
                $i++;
                if ($boardLink['relative'] == 2) {
                      //if 6 == $i, don't add slash at the end, since it's the last element.
                    if(count($links) ==  $i) {
                        $out .= ' mitsuba->getPath("./".$boardLink['url']."/", $location, 1).'" title="'.$boardLink['title'].'">'.$boardLink['short'].' ';
                    }else{
                        $out .= ' mitsuba->getPath("./".$boardLink['url']."/", $location, 1).'" title="'.$boardLink['title'].'">'.$boardLink['short'].' /';
                    }
                }
            }

            $out .= ']
';
        }
        $out.= '
[ /  /  /  /  /  /  ]  [Images loaded: 0]
';

        return $out;

    }

    /**
     * getBoardLinks
     * Insert description here
     *
     * @param $location
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function getBoardLinks($location = "board")
    {

        if ($location == "board") {

            return $this->config['boardLinks_board'];

        } elseif ($location == "thread") {

            return $this->config['boardLinks_thread'];

        } elseif ($location == "index") {

            return $this->config['boardLinks'];

        }

    }

    /**
     * processComment
     * Insert description here
     *
     * @param $board
     * @param $string
     * @param $parser
     * @param $thread
     * @param $specialchars
     * @param $bbcode
     * @param $identifier
     * @param $resto
     * @param $wordfilter
     * @param $wf_table
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function processComment($board, $string, $parser, $thread = 0, $specialchars = 1, $bbcode = 1, $identifier = 0, $resto = 0, $wordfilter = 1, $wf_table = array())
    {

        global $lang;

        $new = $string;

        $lines = explode("\n", $new);

        $new = "";

        $c_lines = 0;

        while ((count($lines) > 1) && (end($lines) == "")) {

            array_pop($lines);

        }

        foreach ($lines as $line) {

            $c_lines++;

            if ($line == "") {

                $new.= "
";

                if (($c_lines > 15) && ($thread == 0) && (is_numeric($identifier)) && ($identifier > 0)) { break;
                }

                continue;

            }

            if (substr($line, 0, 2) == ">>") {

                $newline = "";

                $space = explode(" ", $line);

                foreach ($space as $word) {

                    $newline.= $this->getQuotelink($board, $word, $specialchars, $thread) . " ";

                }

                $new.= $newline . "
";

            } elseif (substr($line, 0, 1) == ">") {

                if ($specialchars == 1) {

                    $line = htmlspecialchars($line);

                }

                $new.= "" . $line . "
";

            } else {

                $newline = "";

                $space = explode(" ", $line);

                foreach ($space as $word) {

                    $newline.= $this->getQuotelink($board, $word, $specialchars, $thread) . " ";

                }

                $new.= $newline . "
";

            }

            if (($c_lines > 15) && ($thread == 0) && (is_numeric($identifier)) && ($identifier > 0)) {

                break;

            }

        }

        $rexProtocol = '(https?://)?';

        $rexDomain = '((?:[-a-zA-Z0-9]{1,63}\.)+[-a-zA-Z0-9]{2,63}|(?:[0-9]{1,3}\.){3}[0-9]{1,3})';

        $rexPort = '(:[0-9]{1,5})?';

        $rexPath = '(/[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]*?)?';

        $rexQuery = '(\?[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';

        $rexFragment = '(#[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';

        if ($bbcode == 1) {

            $parser->parse($new);

            $new = $parser->getAsHtml();

        }

        $rurl = "&\\b$rexProtocol$rexDomain$rexPort$rexPath$rexQuery$rexFragment(?=[?.!,;:\"]?(\s|$))&";

        /**
         * $match
         * Insert description here
         *
         * @return
         *
         * @access
         * @static
         * @see
         * @since
         */
        $new = preg_replace_callback(
            $rurl, function ($match) {

                if ((substr($match[0], 0, 5) == "http:") || (substr($match[0], 0, 6) == "https:")) {

                    return "" . $match[0] . "";

                } else {

                    return $match[0];

                }

            }, $new
        );

        if (($c_lines > 15) && ($thread == 0) && (is_numeric($identifier)) && ($identifier > 0)) {

            if ($resto == 0) {

                $new.= '
' . sprintf($lang['img/comment_long'], '', '') . '';

            } else {

                $new.= '
' . sprintf($lang['img/comment_long'], '', '') . '';

            }

        }

        /**
         * $match
         * Insert description here
         *
         * @return
         *
         * @access
         * @static
         * @see
         * @since
         */
        $new = preg_replace_callback(
            "/<\+(.*?)\+>/", function ($match) {

                return htmlspecialchars_decode($match[1]);

            }, $new
        );

        /**
         * $match
         * Insert description here
         *
         * @return
         *
         * @access
         * @static
         * @see
         * @since
         */
        $new = preg_replace_callback(
            "/<\+(.*?)\+>/", function ($match) {

                return htmlspecialchars_decode($match[1]);

            }, $new
        );

        $new = str_replace('<\+', "<+", $new);

        $new = str_replace('+\>', "+>", $new);

        $new = str_replace('<\+', "<+", $new);

        $new = str_replace('+\>;', "+>", $new);

        if ($wordfilter == 1 && (is_array($wf_table))) {

            $new = strtr($new, $wf_table);

        }

        return $new;

    }

    /**
     * getQuotelink
     * Insert description here
     *
     * @param $board
     * @param $link
     * @param $specialchars
     * @param $thread
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function getQuotelink($board, $link, $specialchars, $thread)
    {

        $return = $link;

        if ((substr($link, 0, 2) == ">>") && (is_numeric(substr($link, 2)))) {

            $result = $this->conn->query("SELECT * FROM posts WHERE id='" . substr($link, 2) . "' AND board='" . $board . "' AND deleted=0;");

            if ($result->num_rows == 1) {

                $row = $result->fetch_assoc();

                if ($row['resto'] != 0) {

                    if ($thread == 1) {

                        $return = '' . $link . '';

                    } elseif ($thread == 0) {

                        $return = '' . $link . '';

                    } else {

                        $return = '' . $link . '';

                    }

                } else {

                    if ($thread == 1) {

                        $return = '' . $link . '';

                    } elseif ($thread == 0) {

                        $return = '' . $link . '';

                    } else {

                        $return = '' . $link . '';

                    }

                }

            } else {

                $return = "" . $link . "";

            }

        } elseif ((substr($link, 0, 3) == ">>/") || (substr($link, 0, 4) == ">>>/")) {

            $parts = explode("/", $link);

            if ($this->mitsuba->common->isBoard($parts[1])) {

                if (is_numeric($parts[2])) {

                    $result = $this->conn->query("SELECT * FROM posts WHERE id='" . $parts[2] . "' AND board='" . $parts[1] . "' AND deleted=0;");

                    if ($result->num_rows == 1) {

                        $row = $result->fetch_assoc();

                        if ($row['resto'] != 0) {

                            if ($thread == 1) {

                                $return = '' . $link . '';

                            } elseif ($thread == 0) {

                                $return = '' . $link . '';

                            } else {

                                $return = '' . $link . '';

                            }

                        } else {

                            if ($thread == 1) {

                                $return = '' . $link . '';

                            } elseif ($thread == 0) {

                                $return = '' . $link . '';

                            } else {

                                $return = '' . $link . '';

                            }

                        }

                    } else {

                        if ($specialchars == 1) {

                            $link = htmlspecialchars($link);

                        }

                        $return = "" . $link . "";

                    }

                } else {

                    if ($thread == 1) {

                        $return = '' . $link . '';

                    } elseif ($thread == 0) {

                        $return = '' . $link . '';

                    } else {

                        $return = '' . $link . '';

                    }

                }

            } else {

                if ($specialchars == 1) {

                    $link = htmlspecialchars($link);

                }

                $return = "" . $link . "";

            }

        } elseif (substr($link, 0, 1) == ">") {

            if ($specialchars == 1) {

                $link = htmlspecialchars($link);

            }

            $return = "" . $link . "";

        } else {

            if ($specialchars == 1) {

                $link = htmlspecialchars($link);

            }

            $return = $link;

        }

        return $return;

    }

    /**
     * getHtmlDefinition
     * Insert description here
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function getHtmlDefinition()
    {

        $file = ' <!doctype html>

		';

        return $file;

    }

    /**
     * getAds
     * Insert description here
     *
     * @param $board
     * @param $position
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function getAds($board, $position)
    {

        $ads = $this->conn->query("SELECT * FROM ads WHERE board=('" . $this->conn->real_escape_string($board) . "' OR '*' OR '') AND position='" . $position . "' AND `show`=1;");

        $text = "";

        while ($ad = $ads->fetch_assoc()) {

            $text.= $ad['text'];

        }

        return $text;

    }

    /**
     * getBoardHeader
     * Insert description here
     *
     * @param $board
     * @param $boarddata
     * @param $location
     * @param $catalog
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function getBoardHeader($board, $boarddata, $location, $catalog = 0)
    {

        $file = $this->getHtmlDefinition();

        $file.= "<title>/" . $boarddata['short'] . "/ - " . $boarddata['name'] . "</title>";

        $first_default = 1;

        $styles = $this->conn->query("SELECT * FROM styles ORDER BY `default` DESC");

        // --------------------------------------------

        $file.= '<link title="Mobile" rel="stylesheet" href="' . $this->mitsuba->getPath('/css/mobile.css', $location, 0) . '" type="text/css"/>';
        $file.= '<link rel="stylesheet" href="' . $this->mitsuba->getPath('/css/fork-awesome.min.css', $location, 0) . '" type="text/css"/>';
        $file.= '<link rel="stylesheet" href="' . $this->mitsuba->getPath('/css/lity.css', $location, 0) . '" type="text/css"/>';
        $file.= '<link rel="stylesheet" type="text/css" href="'.$this->mitsuba->getPath('/css/captcha.css', $location, 0).'" />';
				$file.= '<link rel="stylesheet" type="text/css" href="'.$this->mitsuba->getPath('/css/glightbox.css', $location, 0).'" />';
        $file.= '';

        // --------------------------------------------

        while ($row = $styles->fetch_assoc()) {

            if ($first_default == 1) {

                $file.= '<link rel="stylesheet" id="switch" href="' . $this->mitsuba->getPath($row['path'], $location, $row['relative']) . '">';

                $first_default = 0;

            }

            $file.= '<link rel="alternate stylesheet" style="text/css" href="' . $this->mitsuba->getPath($row['path'], $location, $row['relative']) . '" title="' . $row['name'] . '">';

        }

        if ($catalog == 1) {

            $file.= '<link rel="stylesheet" href="' . $this->mitsuba->getPath("/css/catalog.css", $location, 0) . '">';

        }

        $file.= "";
        if ($boarddata['type'] == "fileboard") {
          $file.= "";

        }
        if ($boarddata['short'] == 'nig') {
          $file.= "";
          $file.="\r\n";
          $file.='';
        }

        $file.= "";

        if ($catalog == 1) {

            $file.= "";

        } else {

            $file.= "";

            $file.= "";

            $file.= "";

            if ($location == "index") {

                $file.= "";

            }

        }

        $file.= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';

        $file.= '<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes" />';

        $file.= '<meta property="og:boardname" content="' . $boarddata['short'] . '" />';

        $file.= '';

        $file.= '<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />';

        $file.= '<meta http-equiv="Pragma" content="no-cache" />';

        $file.= '<meta http-equiv="Expires" content="0" />';

        $file.= $this->getAds($boarddata['short'], "head");

        if ($location == "index") {

            $file.= "";

        } else {

            $file.= "";

        }

        if ($this->config['enable_meny'] == 1) {

            $file.= '
';

            $file.= $this->getMenu($location);

            $file.= '
';

            $file.= '

';

            $file.= '
';

        }

        $file.= $this->getBoardLinks($location);

        $file.= '
';

        $imagesDir = './rnd/';

        $images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

        $imagesDirBoard = './rnd/' . $board . '/';

        if (is_dir($imagesDirBoard)) {

            $images = array_merge($images, glob($imagesDirBoard . '*.{jpg,jpeg,png,gif}', GLOB_BRACE));

        }

        $randomImage = $images[array_rand($images) ];

        $file.= '';

        $file.= '
/' . $boarddata['short'] . '/ - ' . $boarddata['name'] . '
';

        $file.= '
' . $boarddata['message'] . '
';
        if ($boarddata['catalog'] == 1) {
            $file.= '
Catalog
';
        }

        $file.= '
';

        $file.= '
';

        //$file.= '
';

        $file.= $this->getAds($boarddata['short'], "aboveform");

        return $file;

    }

    /**
     * generateView
     * Insert description here
     *
     * @param $board
     * @param $threadno
     * @param $return
     * @param $mode
     * @param $adm_type
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function generateView($board, $threadno = 0, $return = 0, $mode = 0, $adm_type = 0)
    {

        global $lang;

        $overboard = 0;

        $overboard_boards = array();

        $board = $this->conn->real_escape_string($board);

        if (!$this->mitsuba->common->isBoard($board)) {

            return -16;

        }

        $boarddata = $this->mitsuba->common->getBoardData($board);

        if (($boarddata['hidden'] == 1) && ($return == 0)) {

            return -666;

        }

        if ($boarddata['type'] == "overboard") {

            $overboard = 1;

            $overboard_boards = explode(",", $boarddata['overboard_boards']);

        }

        $wfresult = $this->conn->query("SELECT * FROM wordfilter WHERE active=1");

        $replace_array = array();

        if ($overboard == 0) {

            $replace_array[$board] = array();

        } else {

            foreach ($overboard_boards as $short) {

                $replace_array[$short] = array();

            }

        }

        while ($row = $wfresult->fetch_assoc()) {

            if ($row['boards'] != "%") {

                $boards = explode(",", $row['boards']);

                if ($overboard == 0) {

                    if (in_array($board, $boards)) {

                        $replace_array[$board][$row['search']] = $row['replace'];

                    }

                } else {

                    foreach ($overboard_boards as $short) {

                        if (in_array($short, $boards)) {

                            $replace_array[$short][$row['search']] = $row['replace'];

                        }

                    }

                }

            } else {

                if ($overboard == 0) {

                    $replace_array[$board][$row['search']] = $row['replace'];

                } else {

                    foreach ($overboard_boards as $short) {

                        $replace_array[$short][$row['search']] = $row['replace'];
                    }

                }

            }

        }

        $max_pages = $boarddata['pages'];

        $all_pages = $max_pages + 1;

        $pages = $max_pages + 1;

        $page = 0;

        if (!is_numeric($threadno)) {

            return -15; //error

        }

        if ($return == 1) {

            if (!is_numeric($mode)) {

                return -15; //error

            }

            if (($mode == 0) && ($threadno != 0)) {

                $page = $threadno;

                $threadno = 0;

                $pages = 0;

            }

        }

        if ($threadno != 0) {

            $pages = 0;

        }

        include_once "libs/jbbcode/Parser.php";

        $parser = new \JBBCode\Parser();

        if (($boarddata['bbcode'] == 1) || ($overboard == 1)) {

            $bbcode = $this->conn->query("SELECT * FROM bbcodes;");

            while ($row = $bbcode->fetch_assoc()) {

                $parser->addBBCode($row['name'], $row['code']);

            }

        }

        $embed_table = array();

        $result = $this->conn->query("SELECT * FROM embeds;");

        while ($row = $result->fetch_assoc()) {

            $embed_table[] = $row;

        }

        $extensions = array();

        $result = $this->conn->query("SELECT * FROM extensions;");

        while ($row = $result->fetch_assoc()) {

            $extensions[$row['mimetype']]['image'] = $row['image'];

        }

        if ($return == 1) {

            $pages = $page;

        }

        if ($threadno == 0) {

            $cnt = $this->conn->query("SELECT id FROM posts WHERE resto=0 AND board='" . $board . "' AND deleted=0");

            $all_pages = ceil(($cnt->num_rows) / 10);

            if ($all_pages == 0) {

                $all_pages = 1;

            }

            if (($max_pages + 1) < $all_pages) {

                $all_pages = $max_pages;

            }

            if ($return == 0) {

                $pages = $all_pages;

            }

        }

        $location = "";

        if ($return == 1) {

            $location = "index";

        } elseif ($threadno != 0) {

            $location = "thread";

        } else {

            $location = "board";

        }

        $header = $this->getBoardHeader($board, $boarddata, $location);

        $rules_ads = $this->getAds($boarddata['short'], "rules");

        $underform_ads = $this->getAds($boarddata['short'], "underform");

        $footer_ads = $this->getAds($boarddata['short'], "footer");

        $bottom_ads = $this->getAds($boarddata['short'], "bottom");

        $meny_config = "";

        $locked = 0;

        $postform = "";

        $global = "";

        $sql_boardlist = "";

        if ($this->config['enable_meny'] == 1) {

            $meny_config = $this->getMenyConfig($location);

        }

        if (($return == 1) && ($adm_type >= 1)) {

        } elseif ($threadno != 0) {

            $result = $this->conn->query("SELECT * FROM posts WHERE id=" . $threadno . " AND board='" . $board . "' AND deleted=0;");

            if ($result->num_rows == 1) {

                $tdata = $result->fetch_assoc();

                $locked = $tdata['locked'];

            } else {

                return;

            }

        }

        if (($locked == 0) && ($overboard == 0) && ($boarddata['allow_replies'] == 1)) {

            if ($threadno != 0) {

                $postform.= '
' . $lang['img/posting_mode'] . '
';

                if ($return == 1) {

                    $postform.= '
[' . $lang['img/return_c'] . '] [' . $lang['img/bottom'] . ']
';

                    $postform.= '<form id="postform" action="./imgboard.php?mod=1" method="post" enctype="multipart/form-data">';

                } else {

                    $postform.= '
[' . $lang['img/return_c'] . '] [' . $lang['img/bottom'] . ']';

                    if ($boarddata['catalog'] == 1) {

                        $postform.= ' [Catalog]';

                    }

                    $postform.= '
';

                    $postform.= '<form id="postform" action="../../imgboard.php" method="post" enctype="multipart/form-data">';

                }

            } else {

                if ($return == 1) {

                    $postform.= '<form id="postform" action="/imgboard.php?mod=1" method="post" enctype="multipart/form-data">';

                } else {

                    $postform.= '<form id="postform" action="/imgboard.php" method="post" enctype="multipart/form-data">';

                }

            }

            if ($adm_type <= 0) {

                $postform.= '<input type="hidden" name="MAX_FILE_SIZE" value="' . $boarddata['filesize'] . '" />';

            }

            $postform.= '<input type="hidden" name="mode" value="regist" />

';

            if (($boarddata['noname'] == 0) || ($adm_type >= 2)) {

                $postform.= '

					' . $lang['img/name'] . '
					<input class="board-input" name="name" type="text" board-input autocomplete="off" placeholder="Anonymous (not required)"/>

';

            }

            if (($boarddata['ids'] == 1) && ($adm_type >= 2)) {

                $postform.= '

					' . $lang['img/fake_id'] . '

					<input class="board-input" name="fake_id" type="text" />

';

            }

            $postform.= '

				' . $lang['img/email'] . '

				<input class="board-input" name="email" type="text" placeholder="noko" />

				' . $lang['img/subject'] . '

				<input class="board-input" name="sub" type="text" />';

            $postform.= '<input type="hidden" name="board" value="' . $board . '" />';

            if ($threadno != 0) {

                $postform.= '<input type="hidden" name="resto" value="' . $threadno . '" />';

            }
                          //<button type="submit" name="post" id="post">post</button>
            $postform.= '<input id="postButton" type="submit" value="' . $lang['img/submit'] . '" />

				' . $lang['img/comment'] . '

				<textarea name="com" cols="35" rows="4"></textarea>

';

            $captchaUrl = "";

            if ($boarddata['captcha'] == 1) {
                $postform.= '

					' . $lang['img/captcha'] . '

					';
          //require_once 'piDentify/CaptchaClass.php';
          require_once(dirname(__FILE__) . "/piDentify/CaptchaClass.php");

          $Captcha = new \piDentify\Captcha\Captcha;
          $postform.= $Captcha->createCaptcha();
					$postform.='

';

            }

            $postform .= '

            Dont.

            <input type="text" name="wewlad" />
            	';

            if ((($boarddata['type'] == "imageboard") || ($boarddata['type'] == "fileboard")) && ($boarddata['file_replies'] == 1)) {

                $postform.= '

					' . $lang['img/file'] . '

					<input id="postFile" name="upfile" type="file" />';

                $postform.= '

';

                $fspecials = "";

                if ($boarddata['spoilers'] == 1) {

                    $fspecials.= '<input id="spoiler" type="checkbox" name="spoiler" value="1">' . $lang['img/spoiler'] . '';

                }

                if ($boarddata['nofile'] == 1) {

                    $fspecials.= '<input id="nofile" type="checkbox" name="nofile" value="1">' . $lang['img/mod_nofile'] . '';

                }

                if (!empty($fspecials)) {

                    $postform.= '

						Options

						' . $fspecials . '

';

                }

                if ($boarddata['embeds'] == 1) {

                    $postform.= '

						' . $lang['img/embed'] . '

						<input type="text" name="embed"/>

';

                }

            }

            if ($boarddata['type'] == "linkboard") {

                $postform.= '

					' . $lang['img/url'] . '

					<input type="text" name="url"/>

';

            }

            $postform.= '

				' . $lang['img/password'] . '
				<input id="postPassword" name="pwd" type="password" maxlength="8" autocomplete="off"/> ' . $lang['img/password_used'] . '

';

            if ($adm_type >= 2) {

                $postform.= '

					' . $lang['img/mod'] . '

					<input type="checkbox" name="raw" value=1 />' . $lang['img/mod_raw'] . '<input type="checkbox" name="sticky" value=1 />' . $lang['img/mod_sticky'] . '<input type="checkbox" name="lock" value=1 />' . $lang['img/mod_lock'] . '
';

                $postform.= '<input type="checkbox" name="nolimit" value=1 selected/>' . $lang['img/mod_nolimit'] . '<input type="checkbox" name="ignoresizelimit" value=1 />' . $lang['img/mod_nosizelimit'] . '<input type="checkbox" name="nofile" value=1 />' . $lang['img/mod_nofile'] . '	';

                $postform.= '

					' . $lang['img/mod_capcode'] . '

					<input type="radio" name="capcode" value=0 checked />' . $lang['img/mod_nocapcode'] . '<input type="radio" name="capcode" value=1 />' . $lang['img/mod_capcode'];

                if ($adm_type == 3) {

                    $postform.= '<input type="radio" name="capcode" value=2 id="custom_cc" />' . $lang['img/mod_customcapcode'];

                    $postform.= '
' . $lang['img/text'] . ': <input type="text" name="cc_text" />

                    ' . $lang['img/color'] . ': <input type="text" name="cc_color" />
';

                    $postform.= "";

                }

                $postform.= "
";

            }

            $unique = $this->conn->query("SELECT DISTINCT ip FROM posts WHERE board='" . $boarddata['short'] . "';")->num_rows;

            $AllExtensions = array();

            $result = $this->conn->query("SELECT DISTINCT * FROM extensions;");

            while ($row = $result->fetch_assoc()) {

                $AllExtensions[$row['ext']] = $row['ext'];

            }

            if ($boarddata['extensions'] == "%") {
                $boarddata['extensions'] = implode(", ", $AllExtensions);

            }
            $bdata = $this->mitsuba->common->getBoardData($board);
            $postform.= '

';
            if($bdata['type'] != "textboard") {
                $postform.='
' . $lang['img/supported_types'] . $boarddata['extensions'] . '

' . sprintf($lang['img/max_filesize'], $this->mitsuba->common->human_filesize($boarddata['filesize'])) . '

' . $lang['img/thumbnail'] . '
';
            }else{
                $postform.='';
            }
            $postform.='
' . sprintf($lang['img/unique_user_posts'], $unique) . '

Read the Rules and FAQ before posting.

†: Available options: sage (antibump)

<button id="galBut" type="button" style="background:none!important;color:blue;border:none;padding:0!important;font: inherit;cursor: pointer;">Activate Image Gallery</button> (Requires Javascript)

				' . $rules_ads . '

				</form>';

            if ($boarddata['captcha'] == 1) {

                $postform.= '';

            }

        } elseif ($overboard == 1) {
            //TODO: Overboard stuff
            $postform.= "
I am your overboard. I will provide an overview of all OP posts.
";
        } else {

            $postform.= "

" . $lang['img/locked'] . "

";

        }

        $postform.= $underform_ads . "
";

        if (!empty($this->config['global_message'])) {

            $global = '
';

            $global.= $this->config['global_message'];

            $global.= '
';

        }

        if ($overboard == 1) {

            $sql_boardlist = "(";
            $first = 1;

            foreach ($overboard_boards as $short) {

                if ($first == 1) {

                    $sql_boardlist.= "'" . $short . "'";

                    $first = 0;

                } else {

                    $sql_boardlist.= " OR '" . $short . "'";

                }

                $sql_boardlist .= ")";

            }
        }

        for ($pg = $page;$pg <= $pages;$pg++) {

            $file = $header;

            $file.= $postform;

            $file.= $global;

            $file.= '
';

            if ($return == 1) {

                $file.= '<form id="delform" action="./imgboard.php" method="post">
';

            } elseif ($threadno != 0) {

                $file.= '<form id="delform" action="../../imgboard.php" method="post">
';

            } else {

                $file.= '<form id="delform" action="../imgboard.php" method="post">
';

            }

            if ($overboard == 1) {
                if($sql_boardlist !== "('%')") {
                    $result = $this->conn->query("SELECT * FROM posts WHERE resto=0 AND board='" . $sql_boardlist . "' AND deleted=0 ORDER BY lastbumped DESC LIMIT " . ($pg * 10) . ",10");
                }else{
                    $result = $this->conn->query("SELECT * FROM posts WHERE resto=0 AND deleted=0 ORDER BY lastbumped DESC LIMIT " . ($pg * 10) . ",10");
                }
            } elseif ($threadno != 0) {

                $result = $this->conn->query("SELECT * FROM posts WHERE id=" . $threadno . " AND board='" . $board . "' AND deleted=0;");

            } else {

                $result = $this->conn->query("SELECT * FROM posts WHERE resto=0 AND board='" . $board . "' AND deleted=0 ORDER BY sticky DESC, lastbumped DESC LIMIT " . ($pg * 10) . ",10");

            }

            if ($threadno == 0) {

                if ($boarddata['type'] == "fileboard") {

                    $file.= '

									No.

									Name

									File

                  Embed

									Size

									Subject

									Date

';

                } elseif ($boarddata['type'] == "linkboard") {

                    $file.= '

									No.

									Name

									Link

									Subject

									Date

';

                }

            }

            while ($row = $result->fetch_assoc()) {

                if (($threadno == 0) && (($boarddata['type'] == "linkboard") || ($boarddata['type'] == "fileboard"))) {

                    $file.= '
';

                    $file.= "" . $row['id'] . "	";

                    $trip = "";

                    if (!empty($row['trip'])) {

                        $trip = " !" . $row['trip'] . "";

                    }

                    if (!empty($row['strip'])) {

                        $trip.= " !!" . $row['strip'] . "";

                    }

                    if ((!empty($row['trip'])) || (!empty($row['strip']))) {

                        $trip = "" . $trip . "";

                    }

                    $poster_id = "";

                    if ((!empty($row['poster_id'])) && ($boarddata['ids'] == 1) && (empty($row['capcode_text']))) {

                        $poster_id = '(ID: ' . $row['poster_id'] . ')';

                    }

                    $c_image = "";

                    if (!empty($row['capcode_icon'])) {

                        if (substr($row['capcode_icon'], 0, 1) == ".") {

                            //$c_image = ' mitsuba->getPath($row['capcode_icon'], $location, 1).'" alt="Capcode" style="margin-bottom: -3px;" />';

                        } else {

                            //$c_image = ' ';

                        }

                    }

                    $email_a = "";

                    $email_b = "";

                    if (!empty($row['email'])) {

                        $email_a = '';

                        $email_b = '';

                    }

                    $file.= "";

                    if (!empty($row['capcode_text'])) {

                        $file.= $email_a . '' . $row['name'] . '' . $email_b . $trip . ' ## ' . $row['capcode_text'] . '' . $c_image . '';

                    } else {

                        $file.= $email_a . '' . $row['name'] . '' . $email_b . $trip . ' ' . $poster_id;

                    }

                    $file.= "	";

                    if (empty($row['filename'])) {

                        if ($boarddata['type'] == "linkboard") {

                            $file.= '	';

                        } else {

                            $file.= '		';

                        }

                    } elseif ($row['filename'] == "deleted") {

                        if ($boarddata['type'] == "linkboard") {

                            $file.= 'Link deleted	';

                        } else {

                            $file.= 'File deleted		';

                        }

                    } else {

                        if ($boarddata['type'] == "linkboard") {

                            $file.= '[ ' . htmlspecialchars($row['orig_filename']) . ' ]	';

                        } else {

                            $file.= '';

                            if ($return == 1) {

                                $file.= '[' . htmlspecialchars($row['orig_filename']) . ']';

                            } elseif ($threadno != 0) {

                                $file.= '[' . htmlspecialchars($row['orig_filename']) . ']';

                            } else {

                                $file.= '[' . htmlspecialchars($row['orig_filename']) . ']';

                            }

                            $file.= '	';
                            //EMBEDDING
                            $file.= '[Embed]	';
                            $file.= '' . $row['filesize'] . '	';

                        }

                    }

                    $file.= '' . htmlspecialchars($row['subject']) . '	';

                    $file.= '' . date("d/m/Y(D)H:i:s", $row['date']) . '	';

                    $file.= '[' . $lang['img/reply'] . ']	';

                    $file.= '
';

                } else {

                    if ($overboard == 1) {

                        $file.= "
/" . $row['board'] . "/
";

                    }
                    $file.= $this->getThread($row['board'], $threadno, $return, $adm_type, $parser, $boarddata, $replace_array[$row['board']], $embed_table, $row, $extensions);

                }

            }

            if (($boarddata['type'] == "linkboard") || ($boarddata['type'] == "fileboard")) {

                $file.= '

';

            }

            $file.= "
";

            if ($threadno != 0) {

                if ($return == 1) {

                    $file.= '
[' . $lang['img/return_c'] . '] [' . $lang['img/top'] . ']
';

                } else {

                    $file.= '
[' . $lang['img/return_c'] . '] [' . $lang['img/top'] . ']';

                    if ($boarddata['catalog'] == 1) {

                        $file.= ' [Catalog]';

                    }

                    $file.= '
';

                }

            }

            $file.= '

				<input type="hidden" name="mode" value="usrform" />' . $lang['img/delete_post'] . ' [<input type="checkbox" name="onlyimgdel" value="on" />' . $lang['img/file_only'] . '] ';

            if ($adm_type <= 1) {

                $file.= $lang['img/password'] . ' <input type="password" id="delPassword" name="pwd" maxlength="8" autocomplete="off"/> ';

            }

            $file.= '<input type="submit" name="delete" value="' . $lang['img/delete'] . '" />
';

            if ($adm_type <= 1) {

                $file.= '<input type="hidden" name="board" value="' . $boarddata['short'] . '" /> ';

                $file.= $lang['img/reason'] . ' <input type="text" name="reason" /><input type="submit" name="report" value="' . $lang['img/report'] . '" />';

            }

            $file.= '
' . $lang['img/style'] . ' <select id="stylechanger"></select>

';

            $file.= "</form>
";

            $file.= $footer_ads;

            if (($boarddata['type'] == "imageboard") || ($boarddata['type'] == "textboard") || ($boarddata['type'] == "overboard") || ($boarddata['type'] == "linkboard")) {

                if (($return == 1) && ($threadno == 0)) {

                    $file.= '
';

                    $file.= '
';

                    if ($page != 0) {

                        $file.= '<form action="?/board&b=' . $board . '&p=' . ($page - 1) . '" onsubmit="location=this.action; return false;"><input type="submit" value="' . $lang['img/previous'] . '" /></form>';

                    } else {

                        $file.= '' . $lang['img/previous'] . '';

                    }

                    $file.= '
';

                    $file.= '
';

                    for ($i = 0;$i <= $max_pages;$i++) {

                        if ($i > $all_pages) {

                            $file.= "[" . $i . "] ";

                        } else {

                            if ($i == $page) {

                                //if you're on $i.html (technically)

                                $file.= "[" . $i . "] ";

                            } else {

                                $file.= "[" . $i . "] ";

                            }

                        }

                    }

                    $file.= '
';

                    $file.= '
';

                    if ($page != $all_pages) {

                        $file.= '<form action="?/board&b=' . $board . '&p=' . ($page + 1) . '" onsubmit="location=this.action; return false;"><input type="submit" value="' . $lang['img/next'] . '" /></form>';

                    } else {

                        $file.= '' . $lang['img/next'] . '';

                    }

                    $file.= '
';

                    $file.= '
';

                } elseif ($threadno == 0) {

                    $file.= '
';

                    $file.= '
';

                    if ($pg != 0) {

                        if ($pg != 1) {

                            $file.= '<form action="./' . ($pg - 1) . '.html" onsubmit="location=this.action; return false;"><input type="submit" value="' . $lang['img/previous'] . '" /></form>';

                        } else {

                            $file.= '<form action="./index.html" onsubmit="location=this.action; return false;"><input type="submit" value="' . $lang['img/previous'] . '" /></form>';

                        }

                    } else {

                        $file.= '' . $lang['img/previous'] . '';

                    }

                    $file.= '
';

                    $file.= '
';

                    for ($i = 0;$i <= $max_pages;$i++) {

                        if ($i == $pg) {

                            if ($i == 0) {

                                //if you're on the index.html...

                                $file.= "[" . $i . "] ";

                            } else {

                                //if you're on $i.html...

                                $file.= "[" . $i . "] ";

                            }

                        } else {

                            if ($i+1 > $pages) {

                                // $i.html doesn't exist

                                $file.= "[" . $i . "] ";

                            } else {

                                if ($i != 0) {

                                    $file.= "[" . $i . "] ";

                                } else {

                                    $file.= "[" . $i . "] ";

                                }

                            }

                        }

                    }

                    $file.= '
';

                    $file.= '
';

                    if ($pg+1 != $all_pages) {

                        $file.= '<form action="./' . ($pg + 1) . '.html" onsubmit="location=this.action; return false;"><input type="submit" value="' . $lang['img/next'] . '" /></form>';

                    } else {

                        $file.= '' . $lang['img/next'] . '';

                    }

                    $file.= '
';

                    if ($boarddata['catalog'] == 1) {

                        $file.= '
Catalog
';

                    }

                    $file.= '
';

                }

            }

            $file.= '

- Haruko + mitsuba -

  In memory of 314chan\'s co-founder, goo.
';

            $file.= '

';

            if ($this->config['enable_meny'] == 1) {

                $file.= $meny_config;

            }

            $file.= $bottom_ads;

            $file.= "";
						$file.= "";
            $file.= "

						";
            $file.= "";

            if ($return != 1) {

                if ($threadno != 0) {

                    $handle = fopen("./" . $board . "/res/" . $threadno . ".html", "w");

                } else {

                    if ($pg != 0) {

                        $handle = fopen("./" . $board . "/" . $pg . ".html", "w");

                    } else {

                        $handle = fopen("./" . $board . "/index.html", "w");

                    }

                }

                fwrite($handle, $file);

                fclose($handle);

            } else {

                return $file;

            }

        }

    }

    /**
     * getMenyConfig
     * Insert description here
     *
     * @param $location
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function getMenyConfig($location = "board")
    {

        $file = "
";

        $file.= '<link rel="stylesheet" href="' . $this->mitsuba->getPath("/css/meny.css", $location, 0) . '">';

        $file.= "\n";

        $file.= '\n";

        return $file;

    }

    /**
     * generatePage
     * Insert description here
     *
     * @param $name
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function generatePage($name)
    {

        global $lang;

        //markdown parser

        $result = $this->conn->query("SELECT * FROM pages WHERE name='" . $this->conn->real_escape_string($name) . "'");

        if (($result->num_rows == 0) && (file_exists("./" . $name . ".html"))) {

            unlink("./" . $name . ".html");

            return;

        }

        $row = $result->fetch_assoc();

        $title = $row['title'];

        $text = $row['text'];

        if ($row['raw'] == 1) {

            $file = $text;

        } else {

            $file = '<!doctype html>';

            $file.= '

            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

            <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0"/>

						<meta charset="UTF-8">

				<title>' . $title . '</title>

            <link href="css/MIcons.css" rel="stylesheet">

            <link href="css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection"/>

            <link href="css/style.css" type="text/css" rel="stylesheet" media="screen,projection"/>

' . $this->config['sitename'] . '

Rules

FAQ

News

IRC

Rules

FAQ

News

IRC

              menu

';

            $file.= '

';

            //I need to figure out why class card-title isn't working on this page...for now, just make it an h1

            $file.= '

' . $title . '

				';
            include_once "libs/Parsedown/Parsedown.php";
            $file.= \Parsedown::instance()->text($text);

            $file.= '

				';

            if ($this->config['enable_meny'] == 1) {

                $file.= '
';

            }

            $file.= '

				';

        }

        $handle = fopen("./" . $name . ".html", "w");

        fwrite($handle, $file);

        fclose($handle);

    }

    /**
     * forceGetThread
     * Insert description here
     *
     * @param $board
     * @param $threadno
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function forceGetThread($board, $threadno)
    {

        global $lang;

        if ($boarddata = $this->mitsuba->common->isBoard($board)) {

            $result = $this->conn->query("SELECT * FROM posts WHERE id=" . $threadno . " AND board='" . $board . "' AND deleted=0");

            if ($result->num_rows == 1) {

                $trow = $result->fetch_assoc();

                $wfresult = $this->conn->query("SELECT * FROM wordfilter WHERE active=1");

                $replace_array = array();

                while ($row = $wfresult->fetch_assoc()) {

                    if ($row['boards'] != "%") {

                        $boards = explode(",", $row['boards']);

                        if (in_array($board, $boards)) {

                            $replace_array[$row['search']] = $row['replace'];

                        }

                    } else {

                        $replace_array[$row['search']] = $row['replace'];

                    }

                }

                include_once "libs/jbbcode/Parser.php";

                $parser = new \JBBCode\Parser();

                if ($boarddata['bbcode'] == 1) {

                    $bbcode = $this->conn->query("SELECT * FROM bbcodes;");

                    while ($row = $bbcode->fetch_assoc()) {

                        $parser->addBBCode($row['name'], $row['code']);

                    }

                }

                $embed_table = array();

                $result = $this->conn->query("SELECT * FROM embeds;");

                while ($row = $result->fetch_assoc()) {

                    $embed_table[] = $row;

                }

                $extensions = array();

                $result = $this->conn->query("SELECT * FROM extensions;");

                while ($row = $result->fetch_assoc()) {

                    $extensions[$row['mimetype']]['image'] = $row['image'];

                }

                $file = $this->getThread($trow['board'], 0, 0, 0, $parser, $boarddata, $replace_array, $embed_table, $trow, $extensions, 1);

                $this->_parser = $parser;

                $this->_replace_array = $replace_array;

            }

        }

    }

    /**
     * getThread
     * Insert description here
     *
     * @param $board
     * @param $threadno
     * @param $return
     * @param $adm_type
     * @param $parser
     * @param $boarddata
     * @param $replace_array
     * @param $embed_table
     * @param $row
     * @param $extensions
     * @param $force
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function getThread($board, $threadno, $return, $adm_type, $parser, $boarddata, $replace_array, $embed_table, $row, $extensions, $force = 0)
    {

        global $lang;

        if (($this->config['caching_mode'] == 1) && ($threadno == 0) && ($return == 0) && ($force == 0) && (file_exists("./" . $row['board'] . "/res/" . $row['id'] . "_index.html"))) {

            return file_get_contents("./" . $row['board'] . "/res/" . $row['id'] . "_index.html");

        }

        if ($return == 1) {

            $location = "index";

        } elseif ($threadno != 0) {

            $location = "thread";

        } else {

            $location = "board";

        }

        $file = "";

        $file.= '
';

        $file.= '
';

        $file.= '
';

        $file.= '
';

        $file.= '<input type="checkbox" name="del%' . $row['board'] . '%' . $row['id'] . '" value="delete" />';

        $file.= '' . $row['subject'] . ' ';

        $trip = "";

        if (!empty($row['trip'])) {

            $trip = " !" . $row['trip'] . "";

        }

        if (!empty($row['strip'])) {

            $trip.= " !!" . $row['strip'] . "";

        }

        if ((!empty($row['trip'])) || (!empty($row['strip']))) {

            $trip = "" . $trip . "";

        }

        $poster_id = "";

        if ((!empty($row['poster_id'])) && ($boarddata['ids'] == 1) && (empty($row['capcode_text']))) {

            $poster_id = '(ID: ' . $row['poster_id'] . ')';

        }

        $c_image = "";

        if (!empty($row['capcode_icon'])) {

            if (substr($row['capcode_icon'], 0, 1) == ".") {

                //$c_image = ' mitsuba->getPath($row['capcode_icon'], $location, 1).'" alt="Capcode" style="margin-bottom: -3px;" />';

            } else {

                //$c_image = ' ';

            }

        }

        $email_a = "";

        $email_b = "";

        if (!empty($row['email'])) {

            $email_a = '';

            $email_b = '';

        }

        $file.= "";

        $file.= '';

        if (!empty($row['capcode_text'])) {

            $file.= $email_a . '' . $row['name'] . '' . $email_b . $trip . ' ## ' . $row['capcode_text'] . '' . $c_image . '';

        } else {

            $file.= $email_a . '' . $row['name'] . '' . $email_b . $trip . ' ' . $poster_id;

        }

        $file.= '';

        $opip = $row['ip'];

        if (($adm_type >= 2) && ($return == 1)) {

            $file.= ' (' . $row['ip'] . ')';

            $file.= ' [N] [ OP ]';

        }

        $file.= ' ' . date("d/m/Y(D)H:i:s", $row['date']) . ' ';

        if ($return == 1) {

            $file.= 'No.' . $row['id'] . '';

            if ($row['locked'] == 1) {

                $file.= '';

            }

            if ($row['sticky'] == 1) {

                $file.= '';

            }

            if ($row['sage'] == 1) {

                $file.= '  ';

            }

            if ($adm_type >= 2) {

                $file.= ' [B / & / D';

                if (!empty($row['filename'])) {

                    $file.= ' / F]';

                } else {

                    $file.= ']';

                }

                if ($adm_type >= 3) {

                    $file.= ' [E]';

                }

            } else {

                $file.= ' [B]';

            }

            if ($adm_type >= 2) {

                $file.= ' [S / L / A]';

            }

            if ($threadno == 0) {

                $file.= '  [' . $lang['img/reply'] . ']';

            }

            $file.= '';

        } elseif ($threadno != 0) {

            $file.= 'No.' . $row['id'] . '';

            if ($row['locked'] == 1) {

                $file.= '';

            }

            if ($row['sticky'] == 1) {

                $file.= '';

            }

            if ($row['sage'] == 1) {

                $file.= '  ';

            }

            $file.= '';

        } else {

            $file.= 'No.' . $row['id'] . ' ';

            if ($row['locked'] == 1) {

                $file.= '';

            }

            if ($row['sticky'] == 1) {

                $file.= '';

            }

            if ($row['sage'] == 1) {

                $file.= '  ';

            }

            $file.= '  [' . $lang['img/reply'] . ']';

        }

        $file.= '
';

        $file.= $this->getFiles($row, $row['board'], $return, $threadno, $embed_table, $extensions);

        $file.= '
';

        $wf = 1;

        if (!empty($row['capcode_text'])) {

            $wf = 0;

        }

        if ($row['raw'] != 1) {

            if ($row['raw'] == 2) {

                if ($return == 1) {

                    $file.= $this->processComment($row['board'], $row['comment'], $parser, 2, 0, $boarddata['bbcode'], $row['id'], $row['resto'], $wf, $replace_array);

                } else {

                    $file.= $this->processComment($row['board'], $row['comment'], $parser, $threadno != 0, 0, $boarddata['bbcode'], $row['id'], $row['resto'], $wf, $replace_array);

                }

            } else {

                if ($return == 1) {

                    $file.= $this->processComment($row['board'], $row['comment'], $parser, 2, 1, $boarddata['bbcode'], $row['id'], $row['resto'], $wf, $replace_array);

                } else {

                    $file.= $this->processComment($row['board'], $row['comment'], $parser, $threadno != 0, 1, $boarddata['bbcode'], $row['id'], $row['resto'], $wf, $replace_array);

                }

            }

        } else {

            $file.= $row['comment'];

        }

        $file.= '
';

        $file.= '
';

        $file.= '
';

        if ($threadno != 0) {

            $posts = $this->conn->query("SELECT * FROM posts WHERE resto=" . $row['id'] . " AND board='" . $row['board'] . "' AND deleted=0 ORDER BY id ASC");

        } else {

            $postnos = $this->conn->query("SELECT COUNT(*) FROM posts WHERE resto=" . $row['id'] . " AND board='" . $row['board'] . "' AND deleted=0");

            $row1 = $postnos->fetch_row();

            if ($row1[0] == 0) {

                $file.= '

';

                if (($this->config['caching_mode'] == 1) && ($threadno == 0) && ($return == 0)) {

                    $handle = fopen("./" . $row['board'] . "/res/" . $row['id'] . "_index.html", "w");

                    fwrite($handle, $file);

                    fclose($handle);

                }

                return $file;

            }

            if ($row1[0] > 3) {

                if ($return == 1) {

                    $file.= '' . sprintf($lang['img/posts_omitted'], ($row1[0] - 3), '', '') . '';

                } else {

                    $file.= '' . sprintf($lang['img/posts_omitted'], ($row1[0] - 3), '', '') . '';

                }

            }

            $offset = 0;

            if ($row1[0] > 3) {

                $offset = $row1[0] - 3;

            }

            $posts = $this->conn->query("SELECT * FROM posts WHERE resto=" . $row['id'] . " AND board='" . $row['board'] . "' AND deleted=0 ORDER BY id ASC LIMIT " . $offset . ",3");

        }

        while ($row2 = $posts->fetch_assoc()) {

            $file.= '
';

            $file.= '
>>
';

            $file.= '
';

            $file.= '
';

            $file.= '<input type="checkbox" name="del%' . $row2['board'] . '%' . $row2['id'] . '" value="delete" />';

            $file.= '' . $row2['subject'] . ' ';

            $trip = "";

            if (!empty($row2['trip'])) {

                $trip = " !" . $row2['trip'] . "";

            }

            if (!empty($row2['strip'])) {

                $trip.= " !!" . $row2['strip'] . "";

            }

            if ((!empty($row2['trip'])) || (!empty($row2['strip']))) {

                $trip = "" . $trip . "";

            }

            $poster_id = "";

            if ((!empty($row2['poster_id'])) && ($boarddata['ids'] == 1) && (empty($row2['capcode_text']))) {

                $poster_id = '(ID: ' . $row2['poster_id'] . ')';

            }

            $c_image = "";

            if (!empty($row2['capcode_icon'])) {

                if (substr($row2['capcode_icon'], 0, 1) == ".") {

                    //$c_image = ' mitsuba->getPath($row2['capcode_icon'], $location, 1).'" alt="Capcode" style="margin-bottom: -3px;" />';

                } else {

                    //$c_image = ' ';

                }

            }

            $email_a = "";

            $email_b = "";

            if (!empty($row2['email'])) {

                $email_a = '';

                $email_b = '';

            }

            $file.= "";

            $file.= '';

            if (!empty($row2['capcode_text'])) {

                $file.= $email_a . '' . $row2['name'] . '' . $email_b . $trip . ' ## ' . $row2['capcode_text'] . '' . $c_image . '';

            } else {

                $file.= $email_a . '' . $row2['name'] . '' . $email_b . $trip . ' ' . $poster_id;

            }

            $file.= '';

            if (($adm_type >= 2) && ($return == 1)) {

                $file.= ' (' . $row2['ip'] . ') [N] ';

                if ($row2['ip'] == $opip) {

                    $file.= '[ OP ]';

                }

            }

            $file.= ' ' . date("d/m/Y(D)H:i:s", $row2['date']) . ' ';

            if ($return == 1) {

                $file.= 'No.' . $row2['id'] . '';

                $file.= ' [B / & / D';

                if (!empty($row2['filename'])) {

                    $file.= ' / F] ';

                } else {

                    $file.= ']';

                }

                if ($adm_type >= 3) {

                    $file.= ' [E]';

                }

                $file.= "";

            } elseif ($threadno != 0) {

                $file.= 'No.' . $row2['id'] . '  ';

            } else {

                $file.= 'No.' . $row2['id'] . '  ';

            }

            $file.= '
';

            $file.= $this->getFiles($row2, $row['board'], $return, $threadno, $embed_table, $extensions);

            $file.= '
';

            $wf = 1;

            if (!empty($row2['capcode_text'])) {

                $wf = 0;

            }

            if ($row2['raw'] != 1) {

                if ($row2['raw'] == 2) {

                    if ($return == 1) {

                        $file.= $this->processComment($row['board'], $row2['comment'], $parser, 2, 0, $boarddata['bbcode'], $row2['id'], $row2['resto'], $wf, $replace_array);

                    } else {

                        $file.= $this->processComment($row['board'], $row2['comment'], $parser, $threadno != 0, 0, $boarddata['bbcode'], $row2['id'], $row2['resto'], $wf, $replace_array);

                    }

                } else {

                    if ($return == 1) {

                        $file.= $this->processComment($row['board'], $row2['comment'], $parser, 2, 1, $boarddata['bbcode'], $row2['id'], $row2['resto'], $wf, $replace_array);

                    } else {

                        $file.= $this->processComment($row['board'], $row2['comment'], $parser, $threadno != 0, 1, $boarddata['bbcode'], $row2['id'], $row2['resto'], $wf, $replace_array);

                    }

                }

            } else {

                $file.= $row2['comment'];

            }

            $file.= '
';

            $file.= "
";

            $file.= '
';

        }

        $file.= '
';

        $file.= '
';

        if (($this->config['caching_mode'] == 1) && ($threadno == 0) && ($return == 0)) {

            $handle = fopen("./" . $row['board'] . "/res/" . $row['id'] . "_index.html", "w");

            fwrite($handle, $file);

            fclose($handle);

        }

        return $file;

    }

    /**
     * generateCatalog
     * Insert description here
     *
     * @param $board
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function generateCatalog($board)
    {

        global $lang;

        $board = $this->conn->real_escape_string($board);

        if (!$this->mitsuba->common->isBoard($board)) {

            return -16;

        }

        $boarddata = $this->mitsuba->common->getBoardData($board);

        if ($boarddata['hidden'] == 1) {

            return -666;

        }

        if ($boarddata['catalog'] == 0) {

            return -1;

        }

        $extensions = array();

        $result = $this->conn->query("SELECT * FROM extensions;");

        while ($row = $result->fetch_assoc()) {

            $extensions[$row['mimetype']]['image'] = $row['image'];

        }

        $wfresult = $this->conn->query("SELECT * FROM wordfilter WHERE active=1");

        $replace_array = array();

        while ($row = $wfresult->fetch_assoc()) {

            if ($row['boards'] != "%") {

                $boards = explode(",", $row['boards']);

                if (in_array($board, $boards)) {

                    $replace_array[$row['search']] = $row['replace'];

                }

            } else {

                $replace_array[$row['search']] = $row['replace'];

            }

        }

        $file = $this->getBoardHeader($board, $boarddata, "board", 1);

        $file.= $this->getAds($boarddata['short'], "underform");

        $location = "board";

        $threads = $this->conn->query("SELECT *, id as thread_id, (SELECT COUNT(*) FROM posts WHERE board='$board' AND resto = thread_id AND deleted = 0) as replies FROM posts  WHERE resto=0 AND board='$board' AND deleted=0  ORDER BY sticky DESC, lastbumped DESC");

        $file.= '
[' . $lang['img/return_c'] . '] [' . $lang['img/bottom'] . ']
';

        $file.= '
';

        $file.= '
';

        $bumporder = 0;

        while ($row = $threads->fetch_assoc()) {

            $bumporder++;

            $sticky = $row['sticky'] ? ' sticky' : '';

            $file.= '
';

            if (!empty($row['filename'])) {

                $files = array();

                if (substr($row['filename'], 0, 6) == "multi;") {

                    $filenames = explode(";", $row['filename']);

                    $orig_filenames = explode(";", $row['orig_filename']);

                    $filesizes = explode(";", $row['filesize']);

                    $imagesizes = explode(";", $row['imagesize']);

                    $mimetypes = explode(";", $row['mimetype']);

                    $t_ws = explode(";", $row['t_w']);

                    $t_hs = explode(";", $row['t_h']);

                    $num = 0;

                    foreach ($filenames as $filename) {

                        $files[$num]['filename'] = $filenames[$num + 1];

                        $files[$num]['orig_filename'] = $orig_filenames[$num];

                        $files[$num]['filesize'] = $filesizes[$num];

                        $files[$num]['imagesize'] = $imagesizes[$num];

                        $files[$num]['mimetype'] = $mimetypes[$num];

                        $files[$num]['t_w'] = $t_ws[$num];

                        $files[$num]['t_h'] = $t_hs[$num];

                        $num++;

                    }

                } else {

                    $files[0]['filename'] = $row['filename'];

                    $files[0]['orig_filename'] = $row['orig_filename'];

                    $files[0]['filesize'] = $row['filesize'];

                    $files[0]['imagesize'] = $row['imagesize'];

                    $files[0]['mimetype'] = $row['mimetype'];

                    $files[0]['t_w'] = $row['t_w'];

                    $files[0]['t_h'] = $row['t_h'];

                }

                $filenum = 0;

                foreach ($files as $fileinfo) {

                    if ($fileinfo['filename'] == "deleted") {

                        $file.= '';

                        $file.= 'mitsuba->getPath("./img/deleted.gif", $location, 1) . '">';

                        $file.= '';

                    } elseif (substr($fileinfo['filename'], 0, 8) == "spoiler:") {

                        $file.= '';

                        $file.= 'mitsuba->getPath("./img/spoiler.png", $location, 1) . '">';

                        $file.= '';

                    } elseif (substr($fileinfo['filename'], 0, 6) == "embed:") {

                        $file.= '';

                        $file.= 'Embed';

                        $file.= '';

                    } elseif (substr($fileinfo['filename'], -4) == "webm") {

                        $fileinfo['filename'] = substr($fileinfo['filename'], 0, -4);

                        $thumbpath = './src/thumb/' . $fileinfo['filename'] . 'gif';

                        $w = $fileinfo['t_w'];

                        $h = $fileinfo['t_h'];

                        $new_w = 0;

                        $new_h = 0;

                        if (($w > 150) || ($h > 150)) {

                            if ($w > $h) {

                                $new_w = 150;

                                $new_h = ($new_w / $w) * $h;

                            } elseif ($w < $h) {

                                $new_h = 150;

                                $new_w = ($new_h / $h) * $w;

                            } elseif ($w == $h) {

                                $new_h = 150;

                                $new_w = 150;

                            }

                        }

                        $file.= '';

                        $file.= '';

                        $file.= '';

                    } else {

                        $imgsize = "";

                        if ((isset($extensions[$fileinfo['mimetype']]['image'])) && ($extensions[$fileinfo['mimetype']]['image'] == 1)) {

                            $imgsize = ', ' . $fileinfo['imagesize'];

                        }

                        $thumbpath = './src/thumb/' . $fileinfo['filename'];

                        if (isset($extensions[$fileinfo['mimetype']]['image'])) {

                            if ($extensions[$fileinfo['mimetype']]['image'] == 1) {

                                $w = $fileinfo['t_w'];

                                $h = $fileinfo['t_h'];

                                $new_w = 0;

                                $new_h = 0;

                                if (($w > 150) || ($h > 150)) {

                                    if ($w > $h) {

                                        $new_w = 150;

                                        $new_h = ($new_w / $w) * $h;

                                    } elseif ($w < $h) {

                                        $new_h = 150;

                                        $new_w = ($new_h / $h) * $w;

                                    } elseif ($w == $h) {

                                        $new_h = 150;

                                        $new_w = 150;

                                    }

                                }

                                $file.= '';

                                $file.= '';

                                $file.= '';

                            } elseif ($extensions[$fileinfo['mimetype']]['image'] != 0) {

                                $file.= '';

                                $file.= 'Other file';

                                $file.= '';

                            }

                        }

                    }

                    $filenum++;

                }

            } else {

                $file.= '';

                $file.= 'No file';

                $file.= '';

            }

            //$file .= '
R: '.$row['replies'].' / I: '.$row['img_replies'].'
';

            $file.= '
R: ' . $row['replies'] . '
';

            $subject = "";

            if (!empty($row['subject'])) {

                $subject = "" . $row['subject'] . ": ";

            }

            //processComment($board, $string, $parser, $thread = 0, $specialchars = 1, $bbcode = 1, $identifier = 0, $resto = 0, $wordfilter = 1, $wf_table = array())

            //$file .= $this->processComment($row['board'], $row['comment'], $parser, 2, 0, $boarddata['bbcode'], $row['id'], $row['resto'], $wf, $replace_array);

            //$file.= '
' . $this->processComment($board, $row['comment'], $this->_parser, 2, 0, $boarddata['bbcode'], $row['id'], $row['resto'], 1, $this->_replace_array) . '
';

            $file .= '
'.$subject.htmlspecialchars(strtr($row['comment'], $replace_array)).'
';

            $file.= '
';

        }

        $file.= '
';

        $file.= '
';

        $file.= '
[' . $lang['img/return_c'] . '] [' . $lang['img/top'] . ']
';

        $file.= '
' . $lang['img/style'] . ' <select id="stylechanger"></select>

';

        $file.= $this->getAds($boarddata['short'], "footer");

        $file.= '

- Haruko + mitsuba -
';

        $file.= '

';

        if ($this->config['enable_meny'] == 1) {

            $file.= $this->getMenyConfig("board");

        }

        $file.= $this->getAds($boarddata['short'], "bottom");

        $file.= "";

        $handle = fopen("./" . $board . "/catalog.html", "w");

        fwrite($handle, $file);

        fclose($handle);

    }

    /**
     * updateThreads
     * Insert description here
     *
     * @param $board
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function updateThreads($board)
    {

        $board = $this->conn->real_escape_string($board);

        if (!$this->mitsuba->common->isBoard($board)) {

            return -16;

        }

        $result = $this->conn->query("SELECT id FROM posts WHERE resto=0 AND board='" . $board . "' AND deleted=0");

        while ($row = $result->fetch_assoc()) {

            if ($this->config['caching_mode'] == 1) {

                $this->forceGetThread($board, $row['id']);

            }

            $this->generateView($board, $row['id']);

            if ($this->config['enable_api'] == 1) {

                $this->serializeThread($board, $row['id']);

            }

        }

    }

    /**
     * serializeBoard
     * Insert description here
     *
     * @param $board
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function serializeBoard($board)
    {
        if ($this->config['enable_api'] == 0) {
            return;
        }

        if ($this->mitsuba->common->isBoard($board)) {
            $boarddata = $this->mitsuba->common->getBoardData($board);

            if ($boarddata['bbcode'] == 1) {
                include_once "libs/jbbcode/Parser.php";
                $parser = new \JBBCode\Parser();
                $bbcode = $this->conn->query("SELECT * FROM bbcodes;");
                while ($row = $bbcode->fetch_assoc()) {
                    $parser->addBBCode($row['name'], $row['code']);
                }
            }
            //preactivate the api_posts array, so PHP doesn't complain.
            $api_posts = array();

            // Let's select all posts from $board that aren't deleted.
            $boardposts = $this->conn->query("SELECT * FROM posts WHERE board='" . $board . "' AND deleted=0");

            // while there's posts in the query, continue.
            while ($row = $boardposts->fetch_assoc()) {
                $api_posts[] = $this->serializePost($row, $boarddata, $parser);
            }

            //set the api variable to api_posts
            $api['posts'] = $api_posts;

            //write the file
            $api_handle = fopen("./" . $board . "/board.json", "w");
            fwrite($api_handle, json_encode($api));
            fclose($api_handle);

        }

    }

    /**
     * serializeThread
     * Insert description here
     *
     * @param $board
     * @param $thread_id
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function serializeThread($board, $thread_id)
    {

        if ($this->mitsuba->common->isBoard($board)) {

            $thread = $this->conn->query("SELECT * FROM posts WHERE board='" . $board . "' AND id=" . $thread_id . " AND deleted=0");

            if ($thread->num_rows == 1) {

                $row = $thread->fetch_assoc();

                include_once "libs/jbbcode/Parser.php";

                $parser = new \JBBCode\Parser();

                $boarddata = $this->mitsuba->common->getBoardData($board);

                if ($boarddata['bbcode'] == 1) {

                    $bbcode = $this->conn->query("SELECT * FROM bbcodes;");

                    while ($row = $bbcode->fetch_assoc()) {

                        $parser->addBBCode($row['name'], $row['code']);

                    }

                }

                $api_posts = array();
                $api_posts[] = $this->serializePost($row, $boarddata, $parser);
                $posts = $this->conn->query("SELECT * FROM posts WHERE board='" . $board . "' AND resto=" . $thread_id);
                while ($row2 = $posts->fetch_assoc()) {
                    $api_posts[] = $this->serializePost($row2, $boarddata, $parser);
                    unset($api_posts[0]);
                }

                $OP_api_posts = array();
                $OP_api_posts[] = $this->serializePost($row, $boarddata, $parser);
                $posts2 = $this->conn->query("SELECT * FROM posts WHERE board='" . $board . "' AND id=" . $thread_id);
                while ($row3 = $posts2->fetch_assoc()) {
                    $OP_api_posts[] = $this->serializePost($row3, $boarddata, $parser);
                    unset($OP_api_posts[0]);
                    unset($OP_api_posts[1]['resto']);
                }

                $api_handle = fopen("./" . $board . "/res/" . $thread_id . ".json", "w");

                $api['posts'] = array_merge($OP_api_posts, $api_posts);

                fwrite($api_handle, json_encode($api));

                fclose($api_handle);

            }

        }

    }

    /**
     * regenThumbnails
     * Insert description here
     *
     * @param $board
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function regenThumbnails($board)
    {

        $board = $this->conn->real_escape_string($board);

        if (!$this->mitsuba->common->isBoard($board)) {

            return -16;

        }

        $result = $this->conn->query("SELECT filename, resto, id FROM posts WHERE board='" . $board . "'");

        while ($row = $result->fetch_assoc()) {

            if ((!empty($row['filename'])) && ($row['filename'] != "deleted")) {

                $ext = explode('.', $row['filename']);

                $row['filename'] = $ext[0];

                $ext = '.' . $ext[1];

                if (substr($row['filename'], 0, 8) == "spoiler:") {

                    if ($row['resto'] != 0) {

                        $info = $this->mitsuba->common->thumb($board, substr($row['filename'], 8), $ext, 125);

                        if (!empty($info['width'])) {

                            $this->conn->query("UPDATE posts SET t_w=" . $info['width'] . ", t_h=" . $info['height'] . " WHERE id=" . $row['id'] . " AND board='" . $board . "'");

                        }

                    } else {
						//erroring out at "8"??
                        $info = $this->mitsuba->common->thumb($board, substr($row['filename'], $ext, 8));

                        if (!empty($info['width'])) {

                            $this->conn->query("UPDATE posts SET t_w=" . $info['width'] . ", t_h=" . $info['height'] . " WHERE id=" . $row['id'] . " AND board='" . $board . "'");

                        }

                    }

                } elseif (substr($row['filename'], 0, 6) != "embed:") {

                    if ($row['resto'] != 0) {

                        $info = $this->mitsuba->common->thumb($board, $row['filename'], $ext, 125);

                        if (!empty($info['width'])) {

                            $this->conn->query("UPDATE posts SET t_w=" . $info['width'] . ", t_h=" . $info['height'] . " WHERE id=" . $row['id'] . " AND board='" . $board . "'");

                        }

                    } else {

                        $info = $this->mitsuba->common->thumb($board, $row['filename'], $ext);

                        if (!empty($info['width'])) {

                            $this->conn->query("UPDATE posts SET t_w=" . $info['width'] . ", t_h=" . $info['height'] . " WHERE id=" . $row['id'] . " AND board='" . $board . "'");

                        }

                    }

                }

            }

        }

    }

    /**
     * getMenu
     * Insert description here
     *
     * @param $location
     * @param $target
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function getMenu($location, $target = "")
    {

        if (!empty($target)) {

            $target = ' target="' . $target . '"';

        }

        $menu = "
" . $this->config['sitename'] . "
";

        $cats = $this->conn->query("SELECT * FROM links WHERE parent=-1 ORDER BY short ASC;");

        while ($row = $cats->fetch_assoc()) {

            $menu.= '
' . $row['title'] . '
';

            $menu.= '

';

            $children = $this->conn->query("SELECT * FROM links WHERE parent=" . $row['id']);

            while ($child = $children->fetch_assoc()) {

                if ($child['relative'] == 1) {

                    $menu.= '
mitsuba->getPath($child['url'], $location, 1) . '"' . $target . '>/' . $child['short'] . '/ - ' . $child['title'] . '
';

                } elseif ($row['relative'] == 2) {

                    $menu.= '
mitsuba->getPath("./" . $child['url'] . "/", $location, 1) . '"' . $target . '>/' . $child['short'] . '/ - ' . $child['title'] . '
';

                } else {

                    $menu.= '
/' . $child['short'] . '/ - ' . $child['title'] . '
';

                }

            }

            $menu.= '

';

        }

        return $menu;

    }

    /**
     * generateFrontpage
     * Insert description here
     *
     * @param $action
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function generateFrontpage($action = "none")
    {

        if (file_exists("./inc/frontpage/" . $this->config['frontpage_style'])) {

            include_once "./inc/frontpage/" . $this->config['frontpage_style'];

            $fpage = new Frontpage($this->conn, $this->mitsuba);

            $fpage->generateFrontpage($action);

        }

    }

    /**
     * generateNews
     * Insert description here
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function generateNews()
    {

        if (file_exists("./inc/frontpage/" . $this->config['frontpage_style'])) {

            include_once "./inc/frontpage/" . $this->config['frontpage_style'];

            $fpage = new Frontpage($this->conn, $this->mitsuba);

            $fpage->generateNews();

        }

    }

    /**
     * getFiles
     * Insert description here
     *
     * @param $row
     * @param $board
     * @param $return
     * @param $threadno
     * @param $embed_table
     * @param $extensions
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function getFiles($row, $board, $return, $threadno, $embed_table, $extensions)
    {

        $file = "";

        if ($return == 1) {

            $location = "index";

        } elseif ($threadno != 0) {

            $location = "thread";

        } else {

            $location = "board";

        }

        if (!empty($row['filename'])) {

            $files = array();

            if (substr($row['filename'], 0, 6) == "multi;") {

                $filenames = explode(";", $row['filename']);

                $orig_filenames = explode(";", $row['orig_filename']);

                $filesizes = explode(";", $row['filesize']);

                $imagesizes = explode(";", $row['imagesize']);

                $mimetypes = explode(";", $row['mimetype']);

                $t_ws = explode(";", $row['t_w']);

                $t_hs = explode(";", $row['t_h']);

                $num = 0;

                foreach ($filenames as $filename) {

                    $files[$num]['filename'] = $filenames[$num + 1];

                    $files[$num]['orig_filename'] = $orig_filenames[$num];

                    $files[$num]['filesize'] = $filesizes[$num];

                    $files[$num]['imagesize'] = $imagesizes[$num];

                    $files[$num]['mimetype'] = $mimetypes[$num];

                    $files[$num]['t_w'] = $t_ws[$num];

                    $files[$num]['t_h'] = $t_hs[$num];

                    $num++;

                }

            } else {

                $files[0]['filename'] = $row['filename'];

                $files[0]['orig_filename'] = $row['orig_filename'];

                $files[0]['filesize'] = $row['filesize'];

                $files[0]['imagesize'] = $row['imagesize'];

                $files[0]['mimetype'] = $row['mimetype'];

                $files[0]['t_w'] = $row['t_w'];

                $files[0]['t_h'] = $row['t_h'];

            }

            $filenum = 0;

            foreach ($files as $fileinfo) {

                if (substr($fileinfo['filename'], 0, 4) == "url:") {

                    $file.= '
';

                    $file.= '
';

                    $file.= 'File: ' . htmlspecialchars($fileinfo['orig_filename']) . '';

                    $file.= '
';

                } elseif ($fileinfo['filename'] == "deleted") {

                    $file.= '
';

                    $file.= '
';

                    $file.= 'File: deleted';

                    $file.= '
';

                    if ($return == 1) {

                        $file.= '';

                    } elseif ($threadno != 0) {

                        $file.= '';

                    } else {

                        $file.= '';

                    }

                    $file.= '
';

                } elseif (substr($fileinfo['filename'], 0, 8) == "spoiler:") {

                    $file.= '
';

                    $file.= '
';

                    if ($return == 1) {

                        $file.= 'File: Spoiler';

                    } elseif ($threadno != 0) {

                        $file.= 'File: Spoiler';

                    } else {

                        $file.= 'File: Spoiler';

                    }

                    $file.= '
';

                    $filepath = "";

                    $thumbpath = "";

                    if ($return == 1) {

                        $filepath = './' . $board . '/src/' . substr($fileinfo['filename'], 8);

                        $thumbpath = './' . $board . '/src/thumb/' . substr($fileinfo['filename'], 8);

                    } elseif ($threadno != 0) {

                        $filepath = '../../' . $board . '/src/' . substr($fileinfo['filename'], 8);

                        $thumbpath = '../../' . $board . '/src/thumb/' . substr($fileinfo['filename'], 8);

                    } else {

                        $filepath = './../' . $board . '/src/' . substr($fileinfo['filename'], 8);

                        $thumbpath = './../' . $board . '/src/thumb/' . substr($fileinfo['filename'], 8);

                    }

                    $file.= 'mitsuba->getPath("./img/spoiler.png", $location, 1) . '" alt="Spoiler image" style="width: 170px"/>';

                    $file.= '
';

                } elseif (substr($fileinfo['filename'], 0, 6) == "embed:") {

                    $file.= '
';

                    $file.= '
';

                    $file.= 'File: Embed';

                    $file.= '
';

                    $file.= '' . $this->mitsuba->common->getEmbed(substr($fileinfo['filename'], 6), $embed_table) . '';

                    $file.= '
';

                } elseif (substr($fileinfo['filename'], -4) == "webm") {

                    $fileinfo['filename'] = substr($fileinfo['filename'], 0, -4);

                    if ($return == 1) {

                        $filepath = './' . $board . '/src/' . $fileinfo['filename'];

                        $thumbpath = './' . $board . '/src/thumb/' . $fileinfo['filename'];

                    } elseif ($threadno != 0) {

                        $filepath = '../../' . $board . '/src/' . $fileinfo['filename'];

                        $thumbpath = '../../' . $board . '/src/thumb/' . $fileinfo['filename'];

                    } else {

                        $filepath = './../' . $board . '/src/' . $fileinfo['filename'];

                        $thumbpath = './../' . $board . '/src/thumb/' . $fileinfo['filename'];

                    }

                    $filepath.= "webm";

                    $thumbpath.= "gif";

                    $file.= '

						File: (' . $fileinfo['filesize'] . ', ' . $fileinfo['orig_filename'] . ')

					';

        }elseif (substr($fileinfo['filename'], -3) == "mp4") {

                              $fileinfo['filename'] = substr($fileinfo['filename'], 0, -4);

                              if ($return == 1) {

                                  $filepath = './' . $board . '/src/' . $fileinfo['filename'];

                                  $thumbpath = './' . $board . '/src/thumb/' . $fileinfo['filename'];

                              } elseif ($threadno != 0) {

                                  $filepath = '../../' . $board . '/src/' . $fileinfo['filename'];

                                  $thumbpath = '../../' . $board . '/src/thumb/' . $fileinfo['filename'];

                              } else {

                                  $filepath = './../' . $board . '/src/' . $fileinfo['filename'];

                                  $thumbpath = './../' . $board . '/src/thumb/' . $fileinfo['filename'];

                              }

                              $filepath.= ".mp4";

                              $thumbpath.= ".gif";

                              $file.= '

          						File: (' . $fileinfo['filesize'] . ', ' . $fileinfo['orig_filename'] . ')

          					';

        } else {

                    $file.= '
';

                    $file.= '
';

                    $imgsize = "";

                    if ((isset($extensions[$fileinfo['mimetype']]['image'])) && ($extensions[$fileinfo['mimetype']]['image'] == 1)) {

                        $imgsize = ', ' . $fileinfo['imagesize'];

                    }

                    if ($return == 1) {

                        $file.= 'File: (' . $fileinfo['filesize'] . $imgsize . ', ' . $fileinfo['orig_filename'] . ')';

                    } elseif ($threadno != 0) {

                        //user is in thread

                        $file.= 'File: (' . $fileinfo['filesize'] . $imgsize . ', ' . $fileinfo['orig_filename'] . ')';

                    } else {

                        //board index

                        //$file .= 'File: ('.$fileinfo['filesize'].$imgsize.', '.$fileinfo['orig_filename'].')';

                        $file.= '

						File: ' . $fileinfo['filename'] . ' (' . $fileinfo['filesize'] . $imgsize . ')

						';

                    }

                    $file.= '
';

                    $filepath = "";

                    $thumbpath = "";

                    if ($return == 1) {

                        $filepath = './' . $board . '/src/' . $fileinfo['filename'];

                        $thumbpath = './' . $board . '/src/thumb/' . $fileinfo['filename'];

                    } elseif ($threadno != 0) {

                        $filepath = '../../' . $board . '/src/' . $fileinfo['filename'];

                        $thumbpath = '../../' . $board . '/src/thumb/' . $fileinfo['filename'];

                    } else {

                        $filepath = '../' . $board . '/src/' . $fileinfo['filename'];

                        $thumbpath = '../' . $board . '/src/thumb/' . $fileinfo['filename'];

                    }

                    if (isset($extensions[$fileinfo['mimetype']]['image'])) {

                        if ($extensions[$fileinfo['mimetype']]['image'] == 1) {
                          //js enabled
                            $file.= '';
                         //noscript
                            $file.= '';

                            /*

                            $file .= '';

                            if($fileinfo['t_w'] <= 250 && $fileinfo['t_h'] <= 250) {

                            $file .= '';

                            } else {

                            $file .= '';

                            }*/

                        } elseif ($extensions[$fileinfo['mimetype']]['image'] != 0) {

                            $file.= sprintf($extensions[$fileinfo['mimetype']]['image'], $filepath);

                        }

                    }

                    $file.= '
';

                }

                $filenum++;

            }

        }

        return $file;

    }

    /**
     * serializePost
     * Insert description here
     *
     * @param $row
     * @param $boarddata
     * @param $parser
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function serializePost($row, $boarddata, $parser)
    {
        $post = array();

        if (isset($row['id']) == 1) {
          $post['no'] = (int)$row['id'];
        }

        if (isset($row['resto']) == 1) {
          $post['resto'] = (int)$row['resto'];
        }

        if (isset($row['sticky']) == 1) {
            $post['sticky'] = 1;
        }

        if (isset($row['locked']) == 1) {
          $post['closed'] = 1;
        }

        if (isset($row['date']) == 1) {
          $post['now'] = date("d/m/Y(D)H:i:s", $row['date']);
        }

        if (isset($row['date']) == 1) {
          $post['time'] = (int)$row['date'];
        }

        if (isset($row['name']) == 1) {
          $post['name'] = $row['name'];
        }

        if (!empty($row['trip'])) {
            $post['trip'] = "!" . $row['trip'];
        }

        if (!empty($row['capcode_text'])) {
            $post['id'] = $row['capcode_text'];
        } else {
            if ((!empty($row['poster_id'])) && ($boarddata['ids'] == 1)) {
                $post['id'] = $row['poster_id'];
            }
        }

        if (!empty($row['capcode_text'])) {
            $post['capcode'] = $row['capcode_text'];
        }

        if (!empty($row['email'])) {
            $post['email'] = $row['email'];
        }

        if (!empty($row['subject'])) {
            $post['sub'] = $row['subject'];
        }

        if (isset($row['raw']) != 1) {
            if (isset($row['raw']) == 2) {
                $post['com'] = $this->processComment($boarddata['short'], $row['comment'], $parser, 2, 0, $boarddata['bbcode'], $row['id'], $row['resto']);
            } else {
                $post['com'] = $this->processComment($boarddata['short'], isset($row['comment']), $parser, 2, 1, $boarddata['bbcode'], isset($row['id']), isset($row['resto']));
            }
        } else {
            $post['com'] = $row['comment'];
        }

        if (!empty($row['filename'])) {
            if (substr($row['filename'], 0, 6) == "multi;") {
                $files = array();
                $filenames = explode(";", $row['filename']);
                $orig_filenames = explode(";", $row['orig_filename']);
                $filesizes = explode(";", $row['orig_filesize']);
                $imagesizes = explode(";", $row['imagesize']);
                $mimetypes = explode(";", $row['mimetype']);
                $t_ws = explode(";", $row['t_w']);
                $t_hs = explode(";", $row['t_h']);
                $num = 0;

                foreach ($filenames as $filename) {
                    $files[$num]['filename'] = $filenames[$num + 1];
                    $files[$num]['orig_filename'] = $orig_filenames[$num];
                    $files[$num]['filesize'] = $filesizes[$num];
                    $files[$num]['imagesize'] = $imagesizes[$num];
                    $files[$num]['mimetype'] = $mimetypes[$num];
                    $files[$num]['t_w'] = $t_ws[$num];
                    $files[$num]['t_h'] = $t_hs[$num];
                    $num++;
                }

                $filenum = 0;

                foreach ($files as $fileinfo) {
                    if ($row['filename'] != "deleted") {
                        if (substr($row['filename'], 0, 8) == "spoiler:") {
                            $pinfo = pathinfo(substr($row['filename'], 8));
                            $pinfoo = pathinfo($row['orig_filename']);
                            $file = array();
                            $file['tim'] = $pinfo['filename'];
                            $file['filename'] = $pinfoo['filename'];
                            $file['ext'] = "." . $pinfo['extension'];
                            $file['fsize'] = $row['orig_filesize'];
                            $file['mimetype'] = $row['mimetype'];
                            $sze = explode("x", $row['imagesize']);
                            $file['w'] = $sze[0];
                            $file['h'] = $sze[1];
                            $file['t_w'] = $row['t_w'];
                            $file['t_h'] = $row['t_h'];
                            $file['spoiler'] = 1;
                            $post['files'][] = $file;
                        } elseif (substr($row['filename'], 0, 6) == "embed:") {
                            $file['embed'] = 1;
                            $file['embed_url'] = substr($row['filename'], 6);
                        } else {
                            $pinfo = pathinfo($row['filename']);
                            $pinfoo = pathinfo($row['orig_filename']);

                            $file = array();
                            $file['tim'] = $pinfo['filename'];
                            $file['filename'] = $pinfoo['filename'];
                            $file['ext'] = "." . $pinfo['extension'];
                            $file['fsize'] = $row['orig_filesize'];
                            $file['mimetype'] = $row['mimetype'];
                            $sze = explode("x", $row['imagesize']);
                            $file['w'] = $sze[0];
                            $file['h'] = $sze[1];
                            $file['t_w'] = $row['t_w'];
                            $file['t_h'] = $row['t_h'];
                            $post['files'][] = $file;
                        }
                    } else {
                        $file = array();
                        $file['filedeleted'] = 1;
                        $post['files'][] = $file;
                    }
                }
            } else {
                if ($row['filename'] != "deleted") {
                    if (substr($row['filename'], 0, 8) == "spoiler:") {
                        $pinfo = pathinfo(substr($row['filename'], 8));
                        $pinfoo = pathinfo($row['orig_filename']);
                        $file = array();
                        $file['tim'] = $pinfo['filename'];
                        $file['filename'] = $pinfoo['filename'];
                        $file['ext'] = "." . $pinfo['extension'];
                        $file['fsize'] = $row['orig_filesize'];
                        $file['mimetype'] = $row['mimetype'];
                        $sze = explode("x", $row['imagesize']);
                        $file['w'] = $sze[0];
                        $file['h'] = $sze[1];
                        $file['t_w'] = $row['t_w'];
                        $file['t_h'] = $row['t_h'];
                        $file['spoiler'] = 1;
                        $post['files'][] = $file;
                    } elseif (substr($row['filename'], 0, 6) == "embed:") {
                        $file['embed'] = 1;
                        $file['embed_url'] = substr($row['filename'], 6);
                    } else {
                        $pinfo = pathinfo($row['filename']);
                        $pinfoo = pathinfo($row['orig_filename']);

                        $file = array();
                        $file['tim'] = $pinfo['filename'];
                        $file['filename'] = $pinfoo['filename'];
                        $file['ext'] = "." . $pinfo['extension'];
                        $file['fsize'] = $row['orig_filesize'];
                        $file['mimetype'] = $row['mimetype'];
                        $sze = explode("x", $row['imagesize']);
                        $file['w'] = $sze[0];
                        $file['h'] = $sze[1];
                        $file['t_w'] = $row['t_w'];
                        $file['t_h'] = $row['t_h'];
                        $post['files'][] = $file;
                    }
                } else {
                    $post['filedeleted'] = 1;
                }
            }
        }
        return $post;
    }

    /**
     * rebuildBoardCache
     * Insert description here
     *
     * @param $board
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function rebuildBoardCache($board)
    {

        $this->regenIDs($board);

        $this->updateThreads($board);

        $this->generateView($board);

        $this->generateCatalog($board);

        $this->serializeBoard($board);

    }

    /**
     * regenIDs
     * Insert description here
     *
     * @param $board
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function regenIDs($board)
    {

        if ($this->mitsuba->common->isBoard($board)) {

            $bdata = $this->mitsuba->common->getBoardData($board);

            if ($bdata['ids'] == 1) {

                $result = $this->conn->query("SELECT * FROM posts WHERE board='" . $board . "'");

                while ($row = $result->fetch_assoc()) {

                    $poster_id = "";

                    if (empty($row['poster_id'])) {

                        if ($row['resto'] != 0) {

                            $poster_id = $this->mitsuba->common->mkid($row['ip'], $row['resto'], $board);

                        } else {

                            $poster_id = $this->mitsuba->common->mkid($row['ip'], $row['id'], $board);

                        }

                        $this->conn->query("UPDATE posts SET poster_id='" . $poster_id . "' WHERE id=" . $row['id'] . " AND board='" . $board . "'");

                    }

                }

            }

        }

    }

    /**
     * generatePost
     * Insert description here
     *
     * @param $board
     * @param $identifier
     *
     * @return
     *
     * @access
     * @static
     * @see
     * @since
     */
    function generatePost($board, $identifier)
    {

        if ((empty($identifier)) || (!is_numeric($identifier))) {

            return -15;

        }

        if ((empty($identifier)) || (!$this->mitsuba->common->isBoard($board))) {

            return -16;

        }

        $result = $this->conn->query("SELECT * FROM posts WHERE id=" . $identifier . " AND board='" . $board . "' AND deleted=0");

        if ($result->num_rows == 1) {

            $post = $result->fetch_assoc();

            if ($post['resto'] == 0) {

                $this->generateCatalog($board);

                $this->generateView($board, $post['id']);

            } else {

                $this->generateView($board, $post['resto']);

            }

            $this->generateView($board);

            $this->serializeBoard($board);

        }

    }

}

?>