定制 spaze/phpinfo 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

spaze/phpinfo

Composer 安装命令:

composer require spaze/phpinfo

包简介

Extract phpinfo() output into a variable, sanitize sensitive values, and move inline styles to external CSS.

关键字:

README 文档

README

Extract phpinfo() into a variable and move CSS to external file.

This might be handy when you want to show the output from phpinfo() to authenticated users only in your site's layout for example.

$phpInfo = new \Spaze\PhpInfo\PhpInfo();
$html = $phpInfo->getHtml();

getHtml(int $flags = INFO_ALL): string

The getHtml() method returns the phpinfo() output, without the HTML head and body elements, wrapped in <div id="phpinfo"> & </div>.

All inline CSS will be "externalized" to CSS classes, you can load src/assets/info.css to get the colors back (or vendor/spaze/phpinfo/src/assets/info.css when installed via Composer).

An example usage with Nette Framework (can be used with other frameworks or standalone, too):

$this->template->phpinfo = Html::el()->setHtml($this->phpInfo->getHtml());

The output may be customized by passing one or more of the constants specified in the PHP manual in the optional $flags parameter.

Please note that this will also remove the HTML head element which contains meta name="ROBOTS" tag preventing search engines and other bots indexing the phpinfo() output. You have to add it back somehow, for example by rendering the getHtml() output in your own layout which includes the head element with the meta name="ROBOTS" tag. In general, phpinfo() output should be accessible only for authenticated users.

getFullPageHtml(int $flags = INFO_ALL): string

Sometimes, you may want to display the classic phpinfo() output, with the original HTML head and body elements, meta name="ROBOTS" tag, inline styles etc., but still with the sensitive info sanitized (see below). In that case, you may use getFullPageHtml():

$phpInfo = new \Spaze\PhpInfo\PhpInfo();
echo $phpInfo->getFullPageHtml();

The output of this method may also be customized by passing one or more of the constants specified in the PHP manual in the optional $flags parameter.

Sanitization

By default, session id will be automatically determined and replaced by [***] in the output. This is to prevent some session hijacking attacks that would read the session id from the cookie value reflected in the phpinfo() output (see my blog post describing the attack, HttpOnly bypasses, and the solution). You can disable the sanitization by calling doNotSanitizeSessionId() but it's totally not recommended. Do not disable that. Please.

You can add own strings to be sanitized in the output with

addSanitization(string $sanitize, ?string $with = null): self

If found, the string in $sanitize will be replaced with the string $with; if $with is null then the sanitizer's default replacement string will be used instead. The sanitizer's default replacement is [***] unless you pass a custom string to Spaze\PhpInfo\SensitiveValueSanitizer.

To change the default sanitization from [***] to a custom string, pass the string to Spaze\PhpInfo\SensitiveValueSanitizer and then pass the sanitizer to Spaze\PhpInfo\PhpInfo:

$sanitizer = new \Spaze\PhpInfo\SensitiveValueSanitizer('🦘');
$phpInfo = new \Spaze\PhpInfo\PhpInfo($sanitizer);
$html = $phpInfo->getHtml();

Some of the values in phpinfo() output are printed URL-encoded, so the $sanitize value will also be searched URL-encoded automatically. This means that both foo,bar and foo%2Cbar would be replaced.

The sanitizer will try to determine the session id and sanitize it automatically, you can (but shouldn't) disable it with doNotSanitizeSessionId().

The following values will be used when determining the session id:

  1. session_id() output if not false
  2. $_COOKIE[session_name()] if it's a string

However, it is not recommended to rely solely on the automated way, because for example you may set the session name somewhere in a custom service, and it may not be available for the sanitizer to use. I'd rather suggest you configure the sanitization manually:

$phpInfo->addSanitization($this->sessionHandler->getId(), '[***]'); // where $this->sessionHandler is your custom service for example

or

$phpInfo->addSanitization($_COOKIE['MYSESSID'], '[***]'); // where MYSESSID is your session name

or something like that.

Sanitizing arbitrary strings

If you have your phpinfo() output (or anything really) in a string, you can use the sanitizer standalone, for example:

$sanitizer = new \Spaze\PhpInfo\SensitiveValueSanitizer();
$string = $sanitizer->addSanitization('🍍', '🍌')->sanitize('🍍🍕');

You can then pass the configured sanitizer to PhpInfo class which will then use your configuration for sanitizing the phpinfo() output too:

$phpInfo = new \Spaze\PhpInfo\PhpInfo($sanitizer);
$html = $phpInfo->getHtml();

spaze/phpinfo 适用场景与选型建议

spaze/phpinfo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 41.55k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2020 年 10 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「phpinfo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 spaze/phpinfo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 spaze/phpinfo 我们能提供哪些服务?
定制开发 / 二次开发

基于 spaze/phpinfo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 41.55k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 30
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 13
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-11