fclose($fp);
        }
        $resetbutton = $onclickevent = $checkresult = '';
        if ($template['templateid'] != 1) {
            $defaulttpl = DISCUZ_ROOT . "./templates/default/{$fn}";
            if (file_exists($defaulttpl) && md5_file($defaulttpl) != md5_file($filename)) {
                $resetbutton = ' <input  style="vertical-align: middle" type="button" class="btn" value="' . $lang['templates_edit_reset'] . '" accesskey="r" onclick="location.href=\'' . $BASESCRIPT . '?action=templates&operation=edit&templateid=' . $template['templateid'] . '&fn=' . $fn . '&keyword=' . $keywordenc . '&reset=yes\'"> ' . (strtolower(fileext($fn)) == 'htm' ? '<input  style="vertical-align: middle" type="button" class="btn" value="' . $lang['templates_edit_diff'] . '" onclick="location.href=\'' . $BASESCRIPT . '?action=templates&operation=edit&templateid=' . $template['templateid'] . '&fn=' . $fn . '&keyword=' . $keywordenc . '&checktpl=yes\'"> ' : '');
            }
            $dellist = $addlist = array();
            if ($checktpl && strtolower(fileext($fn)) == 'htm') {
                $fp = @fopen($defaulttpl, 'rb');
                $defaultcontent = @fread($fp, filesize($defaulttpl));
                fclose($fp);
                require_once DISCUZ_ROOT . './include/diff.class.php';
                $a = new Diff($content, $defaultcontent);
                $entries = $a->fetch_diff();
                $result = '<br /><table class="tb tb2 nobdb" width="100%" border="0" cellpadding="0" cellspacing="0"><tr class="partition"><td>' . $lang['templates_edit_diff_current'] . '</td><td>&nbsp;</td><td>' . $lang['templates_edit_diff_default'] . '</td></tr>';
                foreach ($entries as $diff_entry) {
                    $result .= '<tr><th width="49.5%" valign="top" class="diff-' . $diff_entry->left_class() . '">' . $diff_entry->diff_text($diff_entry->left) . '</th><th width="1%">&nbsp;</th>' . '<th width="49.5%" valign="top" class="diff-' . $diff_entry->right_class() . '">' . $diff_entry->diff_text($diff_entry->right) . "</th></tr>";
                }
                $result .= '</table><br /><table class="tb tb2 nobdb" width="100%" border="0" cellpadding="4" cellspacing="0">' . '<tr><th class="diff-deleted" style="text-align: center">' . $lang['templates_edit_diff_deleted'] . '</th><th class="diff-notext">&nbsp;</th></tr>' . '<tr><th class="diff-changed" style="text-align: center" colspan="2">' . $lang['templates_edit_diff_changed'] . '&nbsp;</th></tr>' . '<tr><th class="diff-notext">&nbsp;</th><th class="diff-added" style="text-align: center">' . $lang['templates_edit_diff_added'] . '</th></tr></table>';
                $checkresult = $result;
            }
        } else {
            $onclickevent = 'onclick="return confirm(\'' . $lang['templates_edit_default_overwriteconfirm'] . '\')"';
        }
        $content = dhtmlspecialchars($content);
        $filemtime = date("{$dateformat} {$timeformat}", filemtime($filename));
        ?>
<script language="JavaScript">
var n = 0;