Example #1
0
function save_diy_data($tpldirectory, $primaltplname, $targettplname, $data, $database = false, $optype = '')
{
    global $_G;
    if (empty($data) || !is_array($data)) {
        return false;
    }
    checksecurity($data['spacecss']);
    if (empty($tpldirectory)) {
        $tpldirectory = getdiytpldir($primaltplname);
    }
    $isextphp = false;
    $file = $tpldirectory . '/' . $primaltplname . '.htm';
    if (!file_exists($file)) {
        $file = $tpldirectory . '/' . $primaltplname . '.php';
        if (!file_exists($file)) {
            $file = './template/default/' . $primaltplname . '.htm';
        } else {
            $isextphp = true;
        }
    }
    if (!file_exists($file)) {
        return false;
    }
    $content = file_get_contents(DISCUZ_ROOT . $file);
    if ($isextphp) {
        $content = substr($content, strpos($content, "\n"));
    }
    $content = preg_replace("/\\<\\!\\-\\-\\[name\\].+?\\[\\/name\\]\\-\\-\\>\\s+/is", '', $content);
    $content = preg_replace("/\\<script src\\=\"misc\\.php\\?mod\\=diyhelp\\&action\\=get.+?\\>\\<\\/script\\>/", '', $content);
    foreach ($data['layoutdata'] as $key => $value) {
        $key = trimdxtpllang($key);
        $html = '';
        $html .= '<div id="' . $key . '" class="area">';
        $html .= getframehtml($value);
        $html .= '</div>';
        $content = preg_replace("/(\\<\\!\\-\\-\\[diy\\={$key}\\]\\-\\-\\>).+?(\\<\\!\\-\\-\\[\\/diy\\]\\-\\-\\>)/is", "\\1" . $html . "\\2", $content);
    }
    $data['spacecss'] = str_replace('.content', '.dxb_bc', $data['spacecss']);
    $data['spacecss'] = trimdxtpllang($data['spacecss']);
    $content = preg_replace("/(\\<style id\\=\"diy_style\" type\\=\"text\\/css\"\\>).*?(\\<\\/style\\>)/is", "\\1" . $data['spacecss'] . "\\2", $content);
    if (!empty($data['style'])) {
        $content = preg_replace("/(\\<link id\\=\"style_css\" rel\\=\"stylesheet\" type\\=\"text\\/css\" href\\=\").+?(\"\\>)/is", "\\1" . $data['style'] . "\\2", $content);
    }
    $flag = $optype == 'savecache' ? true : false;
    if ($flag) {
        $targettplname = $targettplname . '_diy_preview';
    } else {
        @unlink('./data/diy/' . $tpldirectory . '/' . $targettplname . '_diy_preview.htm');
    }
    $tplfile = DISCUZ_ROOT . './data/diy/' . $tpldirectory . '/' . $targettplname . '.htm';
    $tplpath = dirname($tplfile);
    if (!is_dir($tplpath)) {
        dmkdir($tplpath);
    } else {
        if (file_exists($tplfile) && !$flag) {
            copy($tplfile, $tplfile . '.bak');
        }
    }
    $r = file_put_contents($tplfile, $content);
    if ($r && $database && !$flag) {
        $diytplname = getdiytplname($targettplname, $tpldirectory);
        C::t('common_diy_data')->insert(array('targettplname' => $targettplname, 'tpldirectory' => $tpldirectory, 'primaltplname' => $primaltplname, 'diycontent' => serialize($data), 'name' => $diytplname, 'uid' => $_G['uid'], 'username' => $_G['username'], 'dateline' => TIMESTAMP), false, true);
    }
    return $r;
}
function save_diy_data($primaltplname, $targettplname, $data, $database = false, $optype = '')
{
    global $_G;
    if (empty($data) || !is_array($data)) {
        return false;
    }
    checksecurity($data['spacecss']);
    $file = ($_G['cache']['style_default']['tpldir'] ? $_G['cache']['style_default']['tpldir'] : './template/default') . '/' . $primaltplname . '.htm';
    if (!file_exists($file)) {
        $file = './template/default/' . $primaltplname . '.htm';
    }
    if (!file_exists($file)) {
        return false;
    }
    $content = file_get_contents(DISCUZ_ROOT . $file);
    $content = preg_replace("/\\<\\!\\-\\-\\[name\\].+?\\[\\/name\\]\\-\\-\\>\\s+/is", '', $content);
    $content = preg_replace("/\\<script src\\=\"misc\\.php\\?mod\\=diyhelp\\&action\\=get.+?\\>\\<\\/script\\>/", '', $content);
    foreach ($data['layoutdata'] as $key => $value) {
        $key = trimdxtpllang($key);
        $html = '';
        $html .= '<div id="' . $key . '" class="area">';
        $html .= getframehtml($value);
        $html .= '</div>';
        $content = preg_replace("/(\\<\\!\\-\\-\\[diy\\={$key}\\]\\-\\-\\>).+?(\\<\\!\\-\\-\\[\\/diy\\]\\-\\-\\>)/is", "\\1" . $html . "\\2", $content);
    }
    $data['spacecss'] = str_replace('.content', '.dxb_bc', $data['spacecss']);
    $data['spacecss'] = trimdxtpllang($data['spacecss']);
    $content = preg_replace("/(\\<style id\\=\"diy_style\" type\\=\"text\\/css\"\\>).*?(\\<\\/style\\>)/is", "\\1" . $data['spacecss'] . "\\2", $content);
    if (!empty($data['style'])) {
        $content = preg_replace("/(\\<link id\\=\"style_css\" rel\\=\"stylesheet\" type\\=\"text\\/css\" href\\=\").+?(\"\\>)/is", "\\1" . $data['style'] . "\\2", $content);
    }
    $flag = $optype == 'savecache' ? true : false;
    if ($flag) {
        $targettplname = $targettplname . '_diy_preview';
    } else {
        @unlink('./data/diy/' . $targettplname . '_diy_preview.htm');
    }
    $tplfile = DISCUZ_ROOT . './data/diy/' . $targettplname . '.htm';
    $tplpath = dirname($tplfile);
    if (!is_dir($tplpath)) {
        dmkdir($tplpath);
    } else {
        if (file_exists($tplfile) && !$flag) {
            copy($tplfile, $tplfile . '.bak');
        }
    }
    $r = file_put_contents($tplfile, $content);
    if ($r && $database && !$flag) {
        $tpldata = daddslashes(serialize($data));
        $diytplname = getdiytplname($targettplname);
        $diytplname = addslashes($diytplname);
        DB::query("REPLACE INTO " . DB::table('common_diy_data') . " (targettplname, primaltplname, diycontent, `name`, uid, username, dateline) VALUES ('{$targettplname}', '{$primaltplname}', '{$tpldata}', '{$diytplname}', '{$_G['uid']}', '{$_G['username']}', '" . TIMESTAMP . "')");
    }
    return $r;
}