yupmin/magoo 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

yupmin/magoo

Composer 安装命令:

composer require yupmin/magoo

包简介

PHP library to mask (redact) credit card numbers, emails and more.

README 文档

README

Testing

original package pachico/magoo is abandoned. this is for php 7.4,8.0,8.1,8.2,8.3

Magoo is a PHP library that will mask sensitive data in strings. Built-in masks use regex to find credit card numbers, emails, etc. and will mask only those, leaving the rest of the strings intact. This might be useful, for instance, to log sensitive user input.

You can also mask strings that match your own regex or inject masking class as long as they implement a simple interface.

Multidimensional arrays can also be masked and use it can be used to mask PSR-3 compliant logger libraries, such as Monolog.

Use the issues page to request masks to implement.

Table of contents

Install

Via Composer:

composer require pachico/magoo

Usage

Generic

This is a generic usage of the library.

use Pachico\Magoo\Magoo;

$magoo = new Magoo();
$magoo->pushCreditCardMask()
    ->pushEmailMask()
    ->pushByRegexMask('/(email)+/m');

$mySensitiveString = 'My email is roy@trenneman.com and my credit card is 6011792594656742';

echo $magoo->getMasked($mySensitiveString);

// 'My ***** is ***@trenneman.com and my credit card is ************6742'

Mask credit cards

Credit card mask accepts a custom replacement.

use Pachico\Magoo\Magoo;

$magoo = new Magoo();
$magoo->pushCreditCardMask('·');

$mySensitiveString = 'This is my credit card number: 4111 1111 1111 1111.';

echo $magoo->getMasked($mySensitiveString);

// This is my credit card number: ······1111.

Mask emails

Email mask accepts as optional parameters the replacement for local and domain parts.

use Pachico\Magoo\Magoo;

$magoo = new Magoo();
$magoo->pushEmailMask('$', '*');

$mySensitiveString = 'My email is pachicodev@gmail.com but I need privacy.';

echo $magoo->getMasked($mySensitiveString);

// My email is $$$$$$$$$$@********* but I need privacy.

Mask by regex

Regex mask will replace matches with strings that are long as each individual match. It requires a regex and accepts custom replacement.

use Pachico\Magoo\Magoo;

$magoo = new Magoo();
$magoo->pushByRegexMask('(\d+)', '_');

$mySensitiveString = 'My telephone number is 639.639.639. Call me at 19:00!';

echo $magoo->getMasked($mySensitiveString);

// My telephone number is ___.___.___. Call me at __:__!

Reset

You might want to use the same instance of Magoo in your application but not the same masks everytime. You can reset all masks at any time by using the reset() method.

use Pachico\Magoo\Magoo;

$magoo = new Magoo();
$magoo->pushCreditCardMask()->pushByRegexMask('(\d+)', '_');

$mySensitiveString = 'My CC is 4111 1111 1111 1111 and my telephone number is 639.639.639.';

echo $magoo->getMasked($mySensitiveString);

// My CC is ************____ and my telephone number is ___.___.___.

$magoo->reset();

echo $magoo->getMasked($mySensitiveString);

// My CC is 4111 1111 1111 1111 and my telephone number is 639.639.639.

Custom masks

Additionally, you can add your own mask as long as it implements Maskinterface.

use Pachico\Magoo\Magoo;

class FooMask implements \Pachico\Magoo\Mask\MaskInterface
{
    protected $replacement = '*';

    public function __construct(array $params = [])
    {
        if (isset($params['replacement']) && is_string($params['replacement'])) {
            $this->replacement = $params['replacement'];
        }
    }

    public function mask($string)
    {
        return str_replace('foo', $this->replacement, $string);
    }
}
$magoo = new Magoo();
$customMask = new FooMask(['replacement' => 'bar']);
$magoo->pushMask($customMask);

$mySensitiveString = 'It is time to go to the foo.';

echo $magoo->getMasked($mySensitiveString);

// It is time to go to the bar.

Mask arrays

Magoo includes a handy way to mask multidimensional arrays, which can be useful, for instance, for logger contexts.

use Pachico\Magoo\Magoo;
use Pachico\Magoo\MagooArray;

$magoo =new Magoo();
$magoo->pushByRegexMask('(foo)', 'bar');
$magooArray = new MagooArray($magoo);

$mySensitiveArray = [
    'It is time to go to the foo.',
    [
        'It is never too late to go the foo.'
    ],
    new DateTime()
];

$magooArray->getMasked($mySensitiveArray);

will output

Array
(
    [0] => It is time to go to the bar.
    [1] => Array
        (
            [0] => It is never too late to go the bar.
        )

    [2] => DateTime Object
        (
            [date] => 2020-08-21 11:44:33.000000
            [timezone_type] => 3
            [timezone] => UTC
        )

)

##Mask PSR-3 logger You can also use Magoo as a wrapper for PSR-3 compliant loggers. In this example, we use Monolog.

use Pachico\Magoo\Magoo;
use Pachico\Magoo\MagooLogger;
use Monolog\Logger;
use Monolog\Handler\ErrorLogHandler;

$magoo = new Magoo();
$magoo->pushByRegexMask('(foo)', 'bar');

$logger = new Logger('app');
$handler = new ErrorLogHandler();
$logger->pushHandler($handler);
$magooLogger = new MagooLogger($logger, $magoo);

$mySensitiveString = 'It is time to go to the foo.';

$magooLogger->warning($mySensitiveString);

// [2020-08-20 15:54:34] app.WARNING: It is time to go to the bar. [] []

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email pachicodev@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Help

Please report any bug you might find and/or collaborate with your own masks.

yupmin/magoo 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 57.9k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 35
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-18