Location: PHPKode > scripts > WebNight Commander > webnight-commander-v2/source/includes/14_class_html.php
<?php

class html {

    static function login($error=false) {
        ob_start();

        self::inject_ajax_js("
document.onkeypress = function(e) {
    var e = window.event || e;

    if (e.keyCode == 27) {
        ajax.close_error();
        $('login').elements[1].focus();
    }
};
document.title='Webnight Commander: Login';
$('login').elements[1].focus();");

        if ($error)
            self::error("Incorrect login!");

?><div id="error"></div>
<center>
<div id="dialog" style="display:inline; margin-top:<?= $GLOBALS['h'] ?>px">
<table align="center"><tr><td>
<fieldset>
<legend>Webnight Commander</legend>
<div>
<form id="login" method="post" action="wnc.php" onsubmit="ajax.submit_form(this, 'container'); return false">
<input type="hidden" name="act" value="login" />
<table class="form">
<tr>
<th valign="top">Login:</th>
<td valign="top"><input type="text" name="login" /></td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<th>Password:</th>
<td><input type="password" name="pass" /></td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<th></th>
<td valign="bottom"><a class="button" onclick="ajax.submit_form('login', 'container')">[ Login ]</a><input type="submit" class="hidden" /></td>
</tr>
</table>
</form>
</div>
</fieldset>
</td></tr></table>
</div>
</center><?php

        return self::compress_html(ob_get_clean());
    }

    static function dir($dir) {
        ob_start();

        if (!($dir instanceof dir)) $dir = new dir($dir);

        self::inject_ajax_js("
document.onkeypress = function(e) {
    var e = window.event || e;

    if ((e.keyCode == 27) || (e.keyCode == '13'))
        ajax.close_error();

    if (e.keyCode == 27)
        ajax.close_dialog();
};
document.title='wnc: " . $dir->full_path . "';");

        $sess = $GLOBALS['session']->values;
        if (isset($sess['error']))
            self::error($sess['error']);

        self::js_dir($dir);

?><div id="dialog"></div>
<div id="error"></div>
<table align="center">
<tr><td>
<div class="topic">
<table>
<tr class="title">
<td><a href="http://sourceforge.net/projects/wnc" target="_blank">WEBNIGHT COMMANDER</a><span id="loading"></span></td>
<th>version 2.0</th>
</tr>
<tr>
<td><a class="button" onclick="ajax.post('wnc.php', 'act=chdir&loc=0', 'container')">/</a><?php

        $dirs = explode("/", $dir->full_path);
        foreach ($dirs as $i => $cdir)
            if ($i) {

?><a class="button" onclick="ajax.post('wnc.php', 'act=chdir&loc=<?= $i ?>', 'container')"><?= $cdir . (($i < count($dirs) - 1) ? "/" : "") ?></a><?php

            }

?></td>
<th><a class="button" onclick="ajax.post('wnc.php', 'act=logout', 'container')">Logout</a></th>
</table>
</div>
<table class="list"><tr><td>
<table>
</tr>
<tr>
<th><div class="checkbox" onclick="directory.select_all(this)">[ ]</div></th>
<th width="232">Name</th>
<th>Owner</th>
<th>Group</th>
<th>Permission</th>
<th>Size</th>
<th id="th_mtime">Modified time</th>
<th>Act</th>
</tr><?php

        $items = $dir->content;
        foreach ($items as $i => $item) {
            $tr_class = "";

            // Regular directory
            if ($item['type'] == "d") {
                $tr_class = "dir";
                $size = '<div style="text-align:center">&lt;dir&gt;</div>';
                $name = '<a onclick="ajax.post(\'wnc.php\', \'act=chdir&item='.$i.'\', \'container\')">/'.$item['name'].'</a>';

            // Linked directory
            } elseif (($item['type'] == "l") && ($item['target_info']['type'] == "d")) {
                $tr_class = "dir";
                $size = '<div style="text-align:center" title="' . html::html_value($item['target']) . '">&lt;link&gt;</div>';
                $name = '<a onclick="ajax.post(\'wnc.php\', \'act=chdir&item='.$i.'\', \'container\')">~'.$item['name'].'</a>';

            // Linked file
            } elseif ($item['type'] == "l") {
                $size = '<div style="text-align:center" title="' . html::html_value($item['target']) . '">&lt;link&gt;</div>';
                $name = "<div onclick=\"directory.select($i)\">@{$item['name']}</div>";

            // Character special
            } elseif ($item['type'] == "c") {
                $tr_class = "char";
                $size = '<div style="text-align:center">&lt;char&gt;</div>';
                $name = "<div onclick=\"directory.select($i)\">-{$item['name']}</div>";

            // Block
            } elseif ($item['type'] == "b") {
                $tr_class = "block";
                $size = '<div style="text-align:center">&lt;block&gt;</div>';
                $name = "<div onclick=\"directory.select($i)\">+{$item['name']}</div>";

            // Pipe
            } elseif ($item['type'] == "p") {
                $tr_class = "pipe";
                $size = '<div style="text-align:center">&lt;pipe&gt;</div>';
                $name = "<div onclick=\"directory.select($i)\">={$item['name']}</div>";

            // Socket
            } elseif ($item['type'] == "s") {
                $tr_class = "socket";
                $size = '<div style="text-align:center">&lt;sock&gt;</div>';
                $name = "<div onclick=\"directory.select($i)\">={$item['name']}</div>";

            // Executible file
            } elseif ($item['executable']) {
                $tr_class = "exec";
                $size = '<div style="text-align:right" title="' . $item['size'] . ' bytes">' . $dir->human_size($item['size']) . '</div>';
                $name = "<div onclick=\"directory.select($i)\">*{$item['name']}</div>";

            // Regular file
            } else {
                $size = '<div style="text-align:right" title="' . $item['size'] . ' bytes">' . $dir->human_size($item['size']) . '</div>';
                $name = "<div onclick=\"directory.select($i)\">&nbsp;{$item['name']}</div>";
            }


            $tr_class = trim($tr_class);

?><tr id="tr<?= $i ?>"<?= $tr_class ? " class=\"$tr_class\"" : "" ?>>
<td><?= $i ? "<div id=\"cb$i\" class=\"checkbox\" onclick=\"directory.select($i)\">[ ]</div>" : "" ?></td>
<td><?= $name ?></td>
<td onclick="directory.select(<?= $i ?>)"><?= $item['owner'] ?></td>
<td onclick="directory.select(<?= $i ?>)"><?= $item['group'] ?></td>
<td onclick="directory.select(<?= $i ?>)"><?= $item['perms'] ?></td>
<td onclick="directory.select(<?= $i ?>)"><?= $size ?></td>
<td><div class="context" id="menu<?= $i ?>"></div><div onclick="directory.select(<?= $i ?>)"><?= date("Y-m-d H:m", $item['modified']) ?></div></td>
<td><?= $i ? "<div id=\"act$i\"><a id=\"btn$i\" class=\"button\" onclick=\"directory.show_menu($i, this)\">[+]</a></div>" : "" ?></td>
</tr><?php

        }

?><tr class="last">
<th><div>&nbsp;</div></th>
<th><div>&nbsp;</div></th>
<th><div>&nbsp;</div></th>
<th><div>&nbsp;</div></th>
<th><div>&nbsp;</div></th>
<th><div>&nbsp;</div></th>
<th><div>&nbsp;</div></th>
<th></th>
</tr>
<tr class="bottom">
<th colspan="8"><div id="clipboard"><?php

        if (isset($sess['clipboard'])) {
            if ($dir->is_writable) {

?><a class="button" onclick="ajax.post('wnc.php', 'act=paste', 'container')">Paste <?= count($sess['clipboard']['files']) . ((count($sess['clipboard']['files']) > 1) ? " files" : " file") ?></a><?php

            } else {

?>Cannot paste here<?php

            }
        } else {

?>&nbsp;<?php

        }

?></div><div id="status">&nbsp;</div></th>
</tr>
</table>
</td></tr>
</table>
<div>
<table width="100%">
<tr class="actions">
<td><div id="actions"></div></td>
<th><?php

        if ($dir->is_writable && $dir->is_executable) {

?><form method="post" action="wnc.php" enctype="multipart/form-data">
<input type="hidden" name="act" value="upload"><?php

        }

?><table align="right"><tr><?php

        if ($dir->is_writable && $dir->is_executable) {

?><td><a class="button" onclick="directory.touch()">NewFile</a> <a class="button" onclick="directory.mkdir()">NewDir</a>  <a class="button" onclick="directory.mklnk()">NewLink</a>&nbsp;</td>
<td><div id="div_upload"><input id="fld_upload" type="file" name="file" class="hidden" onchange="this.form.submit()" /></div><a id="btn_upload" class="button">Upload</a></td><?php

        }

?><td>&nbsp;<a class="button" onclick="directory.chdir()">ChDir</a></td>
</tr></table><?php

        if ($dir->is_writable && $dir->is_executable) {

?></form><?php

        }

?></th>
</tr>
</table>
</div>
</td></tr>
</table><?php

        return self::compress_html(ob_get_clean());
    }

    static function editor($dir, $index) {
        $filename = $dir->full_path . "/" . $dir->content[$index]['name'];
        $content = file_get_contents($filename);
        ob_start();

        self::inject_ajax_js("
document.onkeypress = function(e) {
    var e = window.event || e;

    if ((e.keyCode == 27) || (e.keyCode == '13'))
        ajax.close_error();

    if (e.keyCode == 27)
        ajax.close_dialog();
};
directory.changed = false;
document.title = 'wncedit: $filename';
$('editor').style.height = (document.documentElement.clientHeight - 80) + 'px';");

?><div id="dialog"></div>
<div id="error"></div>
<form id="edit" action="wnc.php" method="post">
<input type="hidden" name="act" value="edit" />
<input type="hidden" name="file" value="<?= $index ?>" />
<input type="hidden" name="hash" value="<?= $dir->content[$index]['hash'] ?>" />
<input type="hidden" name="quit" value="0" />
<div class="topic"><table><tr class="title"><td><a href="http://sourceforge.net/projects/wnc" target="_blank">WEBNIGHT COMMANDER</a> editor<span id="loading"></span></td><th>version 2.0</th></tr><tr><td><?= $filename ?><span id="modified"></span></td><th><a class="button" onclick="ajax.post('wnc.php', 'act=logout', 'container')">Logout</a></th></tr></table></div>
<div class="file_content">
<textarea id="editor" name="content" style="width:100%" onkeypress="directory.changed = true; $('modified').innerHTML = '*'"><?= self::textarea_value($content) ?></textarea>
<a class="button" onclick="directory.save_file(<?= $index ?>)">&nbsp;Save&nbsp;</a> <a class="button" onclick="directory.quit_file(<?= $index ?>, directory.changed)">&nbsp;Quit&nbsp;</a>
</div>
</form><?php

        return self::compress_html(ob_get_clean());
    }

    static function viewer($dir, $index) {
        $filename = $dir->full_path . "/" . $dir->content[$index]['name'];
        $content = file_get_contents($filename);
        ob_start();

         self::inject_ajax_js("
document.title = 'wncview: $filename';
$('viewer').style.height = (document.documentElement.clientHeight - 80) + 'px';");

?><div class="topic"><table><tr class="title"><td><a href="http://sourceforge.net/projects/wnc" target="_blank">WEBNIGHT COMMANDER</a> viewer<span id="loading"></span></td><th>version 2.0</th></tr><tr><td><?= $filename ?></td><th><a class="button" onclick="ajax.post('wnc.php', 'act=logout', 'container')">Logout</a></th></tr></table></div>
<div class="file_content">
<textarea id="viewer" style="width:100%" readonly="readonly"><?= self::textarea_value($content) ?></textarea>
<a class="button" onclick="ajax.post('wnc.php', 'act=init', 'container')">&nbsp;Quit&nbsp;</a>
</div><?php

        return self::compress_html(ob_get_clean());
    }

    static function html_value($value) {
        return str_replace('"', "&quot;", $value);
    }

    static function js_value($value) {

        if (is_array($value)) {
            $i = 0;
            $return = "{";
            foreach ($value as $key => $val) {
                $return .= "$key: " . self::js_value($val);
                if (++$i < count($value)) $return .= ", ";
            }
            $return .= "}";

        } elseif (preg_match('/^\d+$/', $value))
            $return = $value;

        elseif (is_bool($value))
            $return = $value ? "true" : "false";

        else
            $return = '"' . str_replace('"', "\\\"", str_replace("\n", "\\\n", $value)) . '"';

        return $return;
    }

    static function textarea_value($value) {
        return str_replace("<", "&lt;", str_replace(">", "&gt;", $value));
    }

    static function inject_ajax_js($js) {
        $header = "<div id=\"ajax_js\">&nbsp;\n<script type=\"text/javascript\">\n";
        $footer = "\n</script>\n</div>";
        $buffer = ob_get_clean();
        if (strpos($buffer, $header) !== false)
            $buffer = str_replace($header, "$header$js\n", $buffer);
        else
            $buffer .= "$header$js$footer";
        ob_start();
        echo $buffer;
    }

    static function js_dir($dir) {
        $js = "directory.content = [";
        $dirs = $dir->content;
        foreach ($dirs as $i => $cdir) {
            $js .= self::js_value($cdir);
            if (++$i < count($dirs)) $js .=  ", ";
        }
        $js .= "];\ndirectory.opened_menu = false;";
        self::inject_ajax_js($js);
    }

    static function error($error) {
        $html = "$error<br /><br /><center><a class=\"button\" onclick=\"$('error').style.display='none'; if ($('login')) $('login').elements[1].focus()\">[ OK ]</a></center>";
        self::inject_ajax_js("ajax.open_error(\"Error!\", " . self::js_value($html) . ");");
    }

    static function compress_html($html) {
        $regex = '/\<textarea([^\>]*)\>([^\<]*)\<\/textarea\s*\>/is';
        if (preg_match_all($regex, $html, $textareas)) {
            $parts = preg_split($regex, $html);
            $return = "";
            foreach ($parts as $i => $part) {
                $compress = trim(preg_replace('/\s+/s', " ", $part));
                $return .= $compress;
                if ($i < count($textareas[0])) {
                    $args = trim(preg_replace('/\s+/s', " ", $textareas[1][$i]));
                    if (strlen($args)) $args = " $args";
                    $return .= "<textarea$args>{$textareas[2][$i]}</textarea>";
                }
            }
            return $return;
        }
        return trim(preg_replace('/\s+/s', " ", $html));
    }
}

?>
Return current item: WebNight Commander