php动态高亮web源代码

php动态高亮web源代码

注:配置好不允许高亮的文件名,安全第一

#php实现动态展示目录树结构源代码

适用于开放源代码,结合html缓存使用效果更佳,因循环较多不适合放首页

能力有限没实现行号

演示:show source|开放源代码

效果截图

代码

4个文件放统一目录

1.php,1.html,net.js,showsource.php

1.php

<?php
header('Content-Type: text/html; charset=utf-8');

define('root',str_replace('\\','/',realpath(__DIR__.'/../..')));
$dir = root;
$res = [];
$root_dir = $dir;
$res = tree($res,$dir);
//$res = preg_replace('/^'.preg_quote($dir,'/').'/', 'root', $res);
$data = base64_encode(json_encode($res));
require '1.html';
function tree(&$res,$dir)
{
    global $root_dir;
    if(count($res)===0){
        $res[]=[
            'id'=>sha1($dir),
            'pid'=>0,
            'type'=>'dir',
            'handle'=>$dir,
            'name'=>substr($dir,strripos($dir,'/')+1,strlen($dir)),
        ];
    }
    $handles = array_merge(glob($dir . '/.*'), glob($dir . '/*'));
    $handles = preg_replace('/^[\s\S]*\/\.$/','',$handles);
    $handles = preg_replace('/^[\s\S]*\/\.\.$/','',$handles);
    $handles = array_filter($handles);
    sort($handles);
    foreach ($handles as $item){
        if(is_dir($item)){
            $res[]=[
                'id'=>sha1($item),
                'pid'=>sha1($dir),
                'type'=>'dir',
                'handle'=>preg_replace('/^'.preg_quote($root_dir,'/').'/','root',$item),
                'name'=>substr($item,strripos($item,'/')+1,strlen($item)),
            ];
            tree($res,$item);
        }else{
            $res[]=[
                'id'=>sha1($item),
                'pid'=>sha1($dir),
                'type'=>'file',
                'handle'=>preg_replace('/^'.preg_quote($root_dir,'/').'/','root',$item),
                'name'=>substr($item,strripos($item,'/')+1,strlen($item)),
            ];
        }
    }
    return $res;
}

function dump($s=null,$return = false)
{
    ob_start();
    var_dump($s);
    $res = ob_get_clean();
    $res = preg_replace('/'.preg_quote(']=>','/').'\n[\s]+/m', '] => ', $res);
    switch (php_sapi_name()){
        case 'cgi-fcgi':
            $res = preg_replace('/  /U', "\t", $res);
            $res = '<pre><code>'.$res.'</code></pre>';
            if($return)
                return $res;
            echo $res;
            break;
        case 'cli':
            if($return)
                return $res;
            echo $res;
            break;
    }
}

1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>show source</title>
    <style>
        .tree {
            --spacing: 1.5rem;
            --radius: 10px;
            padding-left: 0;
            padding-right: 10px;
        }
        .tree li {
            display: block;
            position: relative;
            padding-left: calc(2 * var(--spacing) - var(--radius) - 2px);
        }
        .tree ul {
            margin-left: calc(var(--radius) - var(--spacing));
            padding-left: 0;
        }
        .tree ul li {
            border-left: 2px solid #ddd;
        }
        .tree ul li:last-child {
            border-color: transparent;
        }
        .tree ul li::before {
            content: '';
            display: block;
            position: absolute;
            top: calc(var(--spacing) / -2);
            left: -2px;
            width: calc(var(--spacing) + 2px);
            height: calc(var(--spacing) + 1px);
            border: solid #ddd;
            border-width: 0 0 2px 2px;
        }
        .tree summary {
            display: block;
            cursor: pointer;
        }


        .tree summary::marker,
        .tree summary::-webkit-details-marker {
            display: none;
        }
        .tree summary:focus {
            outline: none;
        }
        .tree summary:focus-visible {
            outline: 1px dotted #000;
        }
        .tree summary::before {
            content: '';
            display: block;
            position: absolute;
            top: calc(var(--spacing) / 2 - var(--radius));
            left: calc(var(--spacing) - var(--radius) - 1px);
            width: calc(2 * var(--radius));
            height: calc(2 * var(--radius));
            border-radius: 50%;
            background: #ddd;
        }
        .tree li a::after {
            content: '';
            display: block;
            position: absolute;
            top: calc(var(--spacing) / 2 - var(--radius));
            left: calc(var(--spacing) - var(--radius) - 1px);
            width: calc(2 * var(--radius));
            height: calc(2 * var(--radius));
            border-radius: 50%;
            background: #ddd;
        }
        .tree .active a::after {
            content: 'z';
            z-index: 1;
            background: #696;
            color: #fff;
            line-height: calc(2 * var(--radius) - 2px);
            text-align: center;
        }
        .tree .active a{
            color: #0a67fb;
        }
        .tree summary::before {
            content: '+';
            z-index: 1;
            background: #696;
            color: #fff;
            line-height: calc(2 * var(--radius) - 2px);
            text-align: center;
        }
        .tree details[open] > summary::before {
            content: '−';
        }
        .tree ul li a {
            display: block;
            text-decoration: none;
            color: #222222;
        }
        .tree summary:hover{
            color: royalblue;
        }
        .tree a:hover{
            color: royalblue;
        }


        body{
            background: #fff;
        }

        .file-menu{
            background: #eee;
        }

        .source{
            /*background: royalblue;*/
        }
        .main{
            width: 100%;
            display: inline-flex;
        }
        .main .left{
            width: 30%;

        }
        .main .right{
            width: 70%;
        }
        .source{
            padding-left: 10px;
        }

    </style>
</head>
<body>
<div class="main">
    <div class="left">
        <div class="file-menu">
            <ul id="file-list" class="tree">
            </ul>
        </div>
    </div>
    <div class="right">
        <div id="source" class="source"></div>
    </div>
</div>
<script src="/static/js/net.js"></script>
<script>
    net = new Net();
    var data = "<?php echo $data; ?>";
    data = JSON.parse(atob(data));
    var tree = treeFile(data);
    initFileList(tree);
    function showSource(obj) {
        document.querySelectorAll('#file-list .active').forEach(function (active) {
            active.classList.remove('active');
        });
        obj.parentElement.classList.add('active');
        let post_data = {file:obj.dataset.file};
        net.post('showsource.php','data='+JSON.stringify(post_data),function (data) {
            data = JSON.parse(data);
            if(data.code!==200){
                document.getElementById("source").textContent=data.msg;
                return;
            }
            document.getElementById("source").innerHTML='<pre>'+data.data+'</pre>';
        });
    }
    function treeFile(data) {
        for (let i = 0; i < data.length; i++) {
            data[i].childnodes = [];
            data[i].list = [];
            for (let j = 0; j < data.length; j++) {
                if (data[j].pid === data[i].id) {
                    switch (data[j].type) {
                        case 'dir':
                            data[i].childnodes.push(data[j]);
                            break;
                        case 'file':
                            data[i].list.push(data[j]);
                            break;
                    }
                }
            }
        }
        return data[0];
    }

    function initFileList(data) {
        var ul = '';
        ul = getFileList(data, ul);
        document.getElementById("file-list").innerHTML = ul;
        document.querySelectorAll('#file-list a').forEach(function (link) {
            link.addEventListener('click', function (event) {
                event.preventDefault();
                showSource(link);
            });
        });
    }


    function getFileList(obj, ul) {
        ul += '<li>';
        ul += '<details>';
        ul += '<summary>' + obj.name + '</summary>';
        ul += '<ul>';
        if (!(obj.childnodes.length === 0)) {
            for (let key in obj.childnodes) {
                ul = getFileList(obj.childnodes[key], ul);
            }
        }

        if (!(obj.list.length === 0)) {
            for (let key in obj.list) {
                ul += '<li><a href="#" data-file="'+obj.list[key].handle+'">' + obj.list[key].name + '</a></li>';
            }
        }
        ul += '</ul>';
        ul += '</details>';
        ul += '</li>';
        return ul;
    }

</script>
</body>
</html>

net.js

function Net() {
    this.xhr = new XMLHttpRequest();
    this.get = function (url, func, pram = {}) {
        xhr = this.xhr;
        xhr.onreadystatechange = function () {
            if (this.readyState == 4) {
                pram.code = this.status;
                if (this.status == 200) {
                    func(this.responseText, pram);
                } else {
                    func(this.responseText, pram);
                }
            }
        };
        xhr.open('get', url, false);
        xhr.send()
    };
    this.post = function (url, data, func,pram = {}) {
        xhr = this.xhr;
        xhr.onreadystatechange = function () {
            if (this.readyState == 4) {
                pram.code = this.status;
                if (this.status == 200) {
                    func(this.responseText,pram);
                } else {
                    func(this.responseText,pram);
                }
            }
        };
        xhr.open("POST", url, false);
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhr.send(data)
    }
}

showsource.php

<?php
header('Content-Type: text/html; charset=utf-8');

if(empty($_POST['data'])){
    die;
}
$data = $_POST['data'];
$data = json_decode($data,true);
if(empty($data['file'])){
    die;
}

define('root',str_replace('\\','/',realpath(__DIR__.'/../..')));
$dir = root;
$root_dir = $dir;

$pass_files = [
    'root/app/conf.php',
];
$allow_ext = [
    'html','js','css','php','txt','xml','json',
];

$file = $data['file'];

if(in_array($file,$pass_files)){
    outSource(500,'涉及配置不允许高亮','');
    die;
}
if(strpos($file,'.')!==false){
    $file_ext = substr($file,strripos($file,'.')+1,strlen($file));
    if(!in_array($file_ext,$allow_ext)){
        outSource(500,'只允许高亮'.implode('|',$allow_ext).'后缀文件','');
        die;
    }
}

$file = preg_replace('/^root/',$root_dir,$file);
if(!is_file($file)){
    outSource(500,'文件不存在','');
    die;
}

$code = highlight_file($file,true);
outSource(200,'',$code);

function outSource($code,$msg,$data)
{
    echo json_encode([
        'code'=>$code,
        'msg'=>$msg,
        'data'=>$data,
    ],JSON_UNESCAPED_UNICODE);
}

注:配置好不允许高亮的文件名,安全第一

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/576396.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

吉布提国家概况

吉布提国家概况 &#xff08;最近更新时间&#xff1a;2022年10月&#xff09; 【国 名】 吉布提共和国&#xff08;The Republic of Djibouti&#xff0c; La Rpublique de Djibouti&#xff09;。 【面 积】 2.32万平方公里。 【人 口】约100万。主要有伊萨族和阿法尔族。…

认识HTTP

HTTP缺点 通信使用明文&#xff08;不加密&#xff09;&#xff0c;内容可能会被窃听 不验证通信方的身份&#xff0c;可能遭遇伪装 无法证明报文的完整性&#xff0c;所以有可能遭篡改 一、通信使用明文&#xff08;不加密&#xff09;&#xff0c;内容可能会被窃听 TCP/…

鸿蒙OpenHarmony【轻量系统 编译】 (基于Hi3861开发板)

编译 OpenHarmony支持hb和build.sh两种编译方式。此处介绍hb方式&#xff0c;build.sh脚本编译方式请参考[使用build.sh脚本编译源码]。 使用build.sh脚本编译源码 进入源码根目录&#xff0c;执行如下命令进行版本编译。 ./build.sh --product-name name --ccache 说明&…

【算法基础实验】图论-深度优先搜索和深度优先路径

深度优先(DFS) 理论基础 深度优先搜索&#xff08;DFS, Depth-First Search&#xff09;是图和树的遍历算法中的一种&#xff0c;它从一个节点开始&#xff0c;沿着树的边走到尽可能深的分支&#xff0c;直到节点没有子节点为止&#xff0c;然后回溯继续搜索下一个分支。DFS …

python基础之元组、集合和函数的定义与返回值

1.元祖 1.元祖的定义 元组的数据结构跟列表相似 特征&#xff1a;有序、 有序&#xff1a;有&#xff08;索引/下标/index&#xff09; 正序、反序标识符&#xff1a; ( ) 里面的元素是用英文格式的逗号分割开来关键字&#xff1a;tuple 列表和元组有什么区别&#xff1f; 元组…

JavaEE 初阶篇-深入了解 I/O 高级流(缓冲流、交换流、数据流和序列化流)

&#x1f525;博客主页&#xff1a; 【小扳_-CSDN博客】 ❤感谢大家点赞&#x1f44d;收藏⭐评论✍ 文章目录 1.0 缓冲流概述 1.1 缓冲流的工作原理 1.2 使用缓冲流的步骤 1.3 字节缓冲流于字符缓冲流的区别 1.4 字节缓冲流的实例 1.5 字符缓冲流的实例 2.0 转换流概述 2.1 字符…

局部多项式近似与 AMPM 算法

kappa3; %已在您的代码中定义% 定义窗口大小 windowSize (2*kappa1);% 初始化梯度估计值 [rows, cols] size(wrappedPhase); phi_y zeros(rows, cols); phi_x zeros(rows, cols);% 遍历每个窗口 for m 1kappa:rows-kappafor n 1kappa:cols-kappa% 提取局部窗口Z_mn wrap…

Git--基础学习--面向企业--持续更新

一、基础学习 1.1基本命令 //查询基础信息 git config --global --list //选取合适位置创建 mkdir 文件名 //创建文件夹 //全局配置 git config --global user.email "****e***i" git config --global user.name "*** K****"//--------------------进入…

SHViT:具有内存高效宏设计的单头视觉Transformer

文章目录 摘要1、引言2、分析与方法2.1、宏观设计中的冗余分析2.2、微观设计中的冗余分析2.3、单头自注意力2.4、单头视觉转换器 3、实验3.1、实现细节3.2、SHViT在ImageNet-1K分类任务上的表现3.3、SHViT在下游任务中的表现3.4、消融研究 4、相关工作5、结论SHViT&#xff1a;…

python-opencv实现最近邻插值和双线性插值对图片上采样

使用背景 当我们需要把图像进行放大或者缩小的时候&#xff0c;第一反应是使用resize()实现。很多情况下&#xff0c;我们会调用最近邻插值和双线性插值去放大图片&#xff0c;当然要说没有分辨率的损失那是不可能的&#xff0c;只能说在放大图片的过程中尽可能增加了图片的分…

免费的一键伪原创工具,用来高效写文章真妙!

写文章是一件耗时间、费精力的事&#xff0c;遇到没有写作灵感的时候&#xff0c;写文章就像嚼蜡一样难受&#xff0c;但好在有免费的一键伪原创工具&#xff0c;它能帮助我们高效率实现自动写作文章&#xff0c;并且整个写作的过程我们无需思考内容怎么去写&#xff0c;是可以…

自动驾驶传感器篇: GNSSIMU组合导航

自动驾驶传感器篇&#xff1a; GNSS&IMU组合导航 1.GNSS1.1 GNSS 系统概述1.2 GNSS系统基本组成1. 空间部分&#xff08;Space Segment&#xff09;&#xff1a;2. 地面控制部分&#xff08;Ground Control Segment&#xff09;&#xff1a;3. 用户设备部分&#xff08;Use…

x86 64位的ubuntu环境下汇编(无优化)及函数调用栈的详解

1. 引言 为了深入理解c&#xff0c;决定学习一些简单的汇编语言。使用ubuntu系统下g很容易将一个c的文件编译成汇编语言。本文使用此方法&#xff0c;对一个简单的c文件编译成汇编语言进行理解。 2.示例 文件名&#xff1a;reorder_demo.cpp #include<stdio.h>typede…

摩尔定律仍在延续|从最新1.6nm工艺节点看芯片发展-2

2nm工艺的斗争还没结束&#xff0c;TSMC台积电就又公开宣布了1.6nm&#xff08;TSMC A16TM&#xff09;半导体工艺&#xff0c;太卷了&#xff01; TSMC A16TM技术采用领先的纳米片晶体管&#xff0c;并结合创新的背侧电源轨方案&#xff0c;计划于2026年投入生产。这种设计极…

【项目】YOLOv8/YOLOv5/YOLOv9半监督ssod火灾烟雾检测(YOLOv8_ssod)

假期闲来无事找到一份火灾烟雾数据集&#xff0c;自己又补充标注了一些&#xff0c;通过论文检索发现现在的火灾检测工作主要局限于对新场景的泛化性不够强&#xff0c;所以想着用半监督&#xff0c;扩充数据集的方法解决这个问题&#xff0c;所以本文结合使用现在检测精度较高…

成功案例丨守“鲜”有道 Fortinet为都乐筑就全球安全防护网

作为全球知名的跨国食品企业&#xff0c;都乐业务遍布各大洲。在各种新兴业务模式层出不穷的数字化时代&#xff0c;都乐面临着生产持续性、安全运营、供应链安全等严峻的网络安全挑战。通过采用Fortinet的FortiSIEM、FortiMail等系列Fortinet Security Fabric安全平台生态产品…

DaVinci Resolve Studio 19(达芬奇19调色剪辑)win/mac激活版

DaVinci Resolve Studio是一个结合专业的8k 编辑&#xff0c;颜色混合&#xff0c;视觉效果和音频后期制作的软件。只需点击一下&#xff0c;你就可以立即在编辑、混音、特效和音频流之间切换。此外&#xff0c;达芬奇解决(达芬奇)是一个多用户协作的解决方案&#xff0c;使编辑…

Swift - 基础语法

文章目录 Swift - 基础语法1. 常量1.1 只能赋值1次1.2 它的值不要求在编译时期确定&#xff0c;但使用之前必须赋值1次1.3 常量、变量在初始化之前&#xff0c;都不能使用 2. 标识符3. 常用数据类型4. 字面量4.1 布尔4.2 字符串4.3 整数4.4 浮点数4.5 数组4.6 字典 5. 类型转换…

OpenHarmony音视频—opus

简介 Opus是一种用于在互联网上进行交互式语音和音频传输的编解码器。它可以从低比特率窄带语音扩展到非常高的高品质立体声音乐。 下载安装 直接在OpenHarmony-SIG仓中搜索opus并下载。 使用说明 以OpenHarmony 3.1 Beta的rk3568版本为例 将下载的opus库代码存在以下路径&a…

OSPF的LSA详解

一、什么是LSA&#xff1f;LSA作用&#xff1f; 在OSPF协议中&#xff0c;LSA全称链路状态通告&#xff0c;主要由LSA头部信息&#xff08;LSA摘要&#xff09;和链路状态组成。部分LSA只有LSA头部信息&#xff0c;无链路状态信息。使用LSA来传递路由信息和拓扑信息&#xff0c…