1<?php
2
3namespace App\Repository;
4
5/**
6 * Repository class for fetching information about PHP's configuration
7 */
8class PhpInfoRepository
9{
10    public function getInfo(): string
11    {
12        ob_start();
13        phpinfo();
14
15        $phpInfo = $this->replaceSensitiveInformation(ob_get_clean());
16        $phpInfo = $this->cleanHtml($phpInfo);
17
18        return $phpInfo;
19    }
20
21    private function replaceSensitiveInformation(string $phpInfo): string
22    {
23        return str_replace([
24            getenv('AUTH_TOKEN'),
25            getenv('USER_TOKEN'),
26            getenv('USER_PWD_SALT'),
27        ], '&lt;hidden&gt;', $phpInfo);
28    }
29
30    /**
31     * This method unwrap the information from the body tag, removes zend and php logos and fixes sizes
32     * for presentation purposes
33     *
34     * We might want to do it proper at some point using DOM methods
35     */
36    private function cleanHtml(string $phpInfo): string
37    {
38        preg_match('!<body.*?>(.*)</body>!ims', $phpInfo, $m);
39
40        $m[1] = preg_replace('!<a href="http://www.php.net/"><img.*?></a>!ims', '', $m[1]);
41        $m[1] = preg_replace('!<a href="http://www.zend.com/"><img.*?></a>!ims', '', $m[1]);
42        $m[1] = str_replace(' width="600"', ' width="80%"', $m[1]);
43
44        return $m[1];
45    }
46}
47